index.d.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import type { AbstractRenderer } from '@pixi/core';
  2. import { BaseTexture } from '@pixi/core';
  3. import { Container } from '@pixi/display';
  4. import type { DisplayObject } from '@pixi/display';
  5. import type { ExtensionMetadata } from '@pixi/core';
  6. import type { Renderer } from '@pixi/core';
  7. import { TextStyle } from '@pixi/text';
  8. import { Texture } from '@pixi/core';
  9. /**
  10. * The prepare manager provides functionality to upload content to the GPU.
  11. *
  12. * BasePrepare handles basic queuing functionality and is extended by
  13. * {@link PIXI.Prepare} and {@link PIXI.CanvasPrepare}
  14. * to provide preparation capabilities specific to their respective renderers.
  15. * @example
  16. * // Create a sprite
  17. * const sprite = PIXI.Sprite.from('something.png');
  18. *
  19. * // Load object into GPU
  20. * app.renderer.plugins.prepare.upload(sprite, () => {
  21. *
  22. * //Texture(s) has been uploaded to GPU
  23. * app.stage.addChild(sprite);
  24. *
  25. * })
  26. * @abstract
  27. * @memberof PIXI
  28. */
  29. export declare class BasePrepare {
  30. /**
  31. * The limiter to be used to control how quickly items are prepared.
  32. * @type {PIXI.CountLimiter|PIXI.TimeLimiter}
  33. */
  34. private limiter;
  35. /** Reference to the renderer. */
  36. protected renderer: AbstractRenderer;
  37. /**
  38. * The only real difference between CanvasPrepare and Prepare is what they pass
  39. * to upload hooks. That different parameter is stored here.
  40. */
  41. protected uploadHookHelper: any;
  42. /** Collection of items to uploads at once. */
  43. protected queue: Array<any>;
  44. /**
  45. * Collection of additional hooks for finding assets.
  46. * @type {Array<Function>}
  47. */
  48. addHooks: Array<any>;
  49. /**
  50. * Collection of additional hooks for processing assets.
  51. * @type {Array<Function>}
  52. */
  53. uploadHooks: Array<any>;
  54. /**
  55. * Callback to call after completed.
  56. * @type {Array<Function>}
  57. */
  58. completes: Array<any>;
  59. /**
  60. * If prepare is ticking (running).
  61. * @type {boolean}
  62. */
  63. ticking: boolean;
  64. /**
  65. * 'bound' call for prepareItems().
  66. * @type {Function}
  67. */
  68. private delayedTick;
  69. /**
  70. * @param {PIXI.AbstractRenderer} renderer - A reference to the current renderer
  71. */
  72. constructor(renderer: AbstractRenderer);
  73. /**
  74. * Upload all the textures and graphics to the GPU.
  75. * @method PIXI.BasePrepare#upload
  76. * @param {PIXI.DisplayObject|PIXI.Container|PIXI.BaseTexture|PIXI.Texture|PIXI.Graphics|PIXI.Text} [item] -
  77. * Container or display object to search for items to upload or the items to upload themselves,
  78. * or optionally ommitted, if items have been added using {@link PIXI.BasePrepare#add `prepare.add`}.
  79. */
  80. upload(item?: IDisplayObjectExtended | Container | BaseTexture | Texture): Promise<void>;
  81. /**
  82. * Use the Promise-based API instead.
  83. * @method PIXI.BasePrepare#upload
  84. * @deprecated since version 6.5.0
  85. * @param {PIXI.DisplayObject|PIXI.Container|PIXI.BaseTexture|PIXI.Texture|PIXI.Graphics|PIXI.Text} item -
  86. * Item to upload.
  87. * @param {Function} [done] - Callback when completed.
  88. */
  89. upload(item?: IDisplayObjectExtended | Container | BaseTexture | Texture, done?: () => void): void;
  90. /**
  91. * Use the Promise-based API instead.
  92. * @method PIXI.BasePrepare#upload
  93. * @deprecated since version 6.5.0
  94. * @param {Function} [done] - Callback when completed.
  95. */
  96. upload(done?: () => void): void;
  97. /**
  98. * Handle tick update
  99. * @private
  100. */
  101. tick(): void;
  102. /**
  103. * Actually prepare items. This is handled outside of the tick because it will take a while
  104. * and we do NOT want to block the current animation frame from rendering.
  105. * @private
  106. */
  107. prepareItems(): void;
  108. /**
  109. * Adds hooks for finding items.
  110. * @param {Function} addHook - Function call that takes two parameters: `item:*, queue:Array`
  111. * function must return `true` if it was able to add item to the queue.
  112. * @returns Instance of plugin for chaining.
  113. */
  114. registerFindHook(addHook: IFindHook): this;
  115. /**
  116. * Adds hooks for uploading items.
  117. * @param {Function} uploadHook - Function call that takes two parameters: `prepare:CanvasPrepare, item:*` and
  118. * function must return `true` if it was able to handle upload of item.
  119. * @returns Instance of plugin for chaining.
  120. */
  121. registerUploadHook(uploadHook: IUploadHook): this;
  122. /**
  123. * Manually add an item to the uploading queue.
  124. * @param {PIXI.DisplayObject|PIXI.Container|PIXI.BaseTexture|PIXI.Texture|PIXI.Graphics|PIXI.Text|*} item - Object to
  125. * add to the queue
  126. * @returns Instance of plugin for chaining.
  127. */
  128. add(item: IDisplayObjectExtended | Container | BaseTexture | Texture): this;
  129. /** Destroys the plugin, don't use after this. */
  130. destroy(): void;
  131. }
  132. /**
  133. * CountLimiter limits the number of items handled by a {@link PIXI.BasePrepare} to a specified
  134. * number of items per frame.
  135. * @memberof PIXI
  136. */
  137. export declare class CountLimiter {
  138. /** The maximum number of items that can be prepared each frame. */
  139. maxItemsPerFrame: number;
  140. /** The number of items that can be prepared in the current frame. */
  141. itemsLeft: number;
  142. /**
  143. * @param maxItemsPerFrame - The maximum number of items that can be prepared each frame.
  144. */
  145. constructor(maxItemsPerFrame: number);
  146. /** Resets any counting properties to start fresh on a new frame. */
  147. beginFrame(): void;
  148. /**
  149. * Checks to see if another item can be uploaded. This should only be called once per item.
  150. * @returns If the item is allowed to be uploaded.
  151. */
  152. allowedToUpload(): boolean;
  153. }
  154. export declare interface IDisplayObjectExtended extends DisplayObject {
  155. _textures?: Array<Texture>;
  156. _texture?: Texture;
  157. style?: TextStyle | Partial<TextStyle>;
  158. }
  159. declare interface IFindHook {
  160. (item: any, queue: Array<any>): boolean;
  161. }
  162. declare interface IUploadHook {
  163. (helper: AbstractRenderer | BasePrepare, item: IDisplayObjectExtended): boolean;
  164. }
  165. /**
  166. * The prepare plugin provides renderer-specific plugins for pre-rendering DisplayObjects. These plugins are useful for
  167. * asynchronously preparing and uploading to the GPU assets, textures, graphics waiting to be displayed.
  168. *
  169. * Do not instantiate this plugin directly. It is available from the `renderer.plugins` property.
  170. * See {@link PIXI.CanvasRenderer#plugins} or {@link PIXI.Renderer#plugins}.
  171. * @example
  172. * // Create a new application
  173. * const app = new PIXI.Application();
  174. * document.body.appendChild(app.view);
  175. *
  176. * // Don't start rendering right away
  177. * app.stop();
  178. *
  179. * // create a display object
  180. * const rect = new PIXI.Graphics()
  181. * .beginFill(0x00ff00)
  182. * .drawRect(40, 40, 200, 200);
  183. *
  184. * // Add to the stage
  185. * app.stage.addChild(rect);
  186. *
  187. * // Don't start rendering until the graphic is uploaded to the GPU
  188. * app.renderer.plugins.prepare.upload(app.stage, () => {
  189. * app.start();
  190. * });
  191. * @memberof PIXI
  192. */
  193. export declare class Prepare extends BasePrepare {
  194. /** @ignore */
  195. static extension: ExtensionMetadata;
  196. /**
  197. * @param {PIXI.Renderer} renderer - A reference to the current renderer
  198. */
  199. constructor(renderer: Renderer);
  200. }
  201. /**
  202. * TimeLimiter limits the number of items handled by a {@link PIXI.BasePrepare} to a specified
  203. * number of milliseconds per frame.
  204. * @memberof PIXI
  205. */
  206. export declare class TimeLimiter {
  207. /** The maximum milliseconds that can be spent preparing items each frame. */
  208. maxMilliseconds: number;
  209. /**
  210. * The start time of the current frame.
  211. * @readonly
  212. */
  213. frameStart: number;
  214. /** @param maxMilliseconds - The maximum milliseconds that can be spent preparing items each frame. */
  215. constructor(maxMilliseconds: number);
  216. /** Resets any counting properties to start fresh on a new frame. */
  217. beginFrame(): void;
  218. /**
  219. * Checks to see if another item can be uploaded. This should only be called once per item.
  220. * @returns - If the item is allowed to be uploaded.
  221. */
  222. allowedToUpload(): boolean;
  223. }
  224. export { }