ResourceData
final class ResourceData
Package: dev.joid.lib.resource · Module: core · Source: ResourceData.java
The shared state of one resource, its decoder, pixels, textures, size and errors, behind every Resource handle on the same input.
Description
Section titled “Description”You meet it when you write a decoder or a resolver: the IResourceDecoder fills it, the Resource handles draw it.
Decoding runs on a pool of 16 background threads; the upload and the listeners run on the render thread.
When no handle uses it any more, its textures are freed at the next frame.
Learn more
Section titled “Learn more”Creation
Section titled “Creation”| Returns | Signature | Summary |
|---|---|---|
ResourceData | create(String uniqueId, IResourceDecoder decoder) factory | Creates the data cached under uniqueId, read by decoder, or by the decoder that detectDecoder finds when it is null. |
Properties
Section titled “Properties”| Property | Type | Default | Summary |
|---|---|---|---|
| uniqueId | String | set at creation | The id under which the data is cached and named in the warnings. |
| decoder | IResourceDecoder | set at creation; null until the format is detected | The IResourceDecoder that reads the data; decoder(IResourceDecoder) sets it and calls its init. |
| assetSource signal | Asset | null | Where the data reads its asset when it detects its format: assetSource(Supplier) sets it, read again on each detection, so that a reload finds a file that appeared since. |
| textures | ITexture[] | null | The GPU textures of the data, one per plane or per frame buffer of the decoder: getTextures() returns a copy, textures(ITexture[]) sets them. |
| data | int[][] | null | The decoded pixels, one 0xAARRGGBB array per texture, waiting for the upload: getData() returns a copy of the outer array, data(int[][]) sets it; null once uploaded. |
| generated | boolean | false | If true, the decode has started; generated(boolean) sets it. |
| width | int | 0 | The width of the image in pixels, or of the region when one is set; width(int) sets the width of the image. |
| height | int | 0 | The height of the image in pixels, or of the region when one is set; height(int) sets the height of the image. |
Methods
Section titled “Methods”Queries
Section titled “Queries”| Returns | Signature | Summary |
|---|---|---|
boolean | isFailed() | If true, the data failed; ResourceData.getError tells why. |
boolean | isDispatching() | If true, a background task started by dispatch, such as the detection of a remote format, is still running. |
List<ResourceData> | getForks() | Returns the data of the separate playbacks forked from this one, as a new list. |
T | getDecoder(Class | Returns the decoder when it is an instance of clazz, otherwise null. |
int[] | getRegion() | Returns a copy of the region of the textures that holds the image, as x, y, width, height in texture pixels, or null for the whole texture. |
int[] | getData(int index) | Returns the pixels waiting for the texture at index, or null. |
ITexture | getTexture(int index) | Returns the texture at index, or null. |
int | getTextureCount() | Returns the number of textures. |
ITexture | getMissingTexture() | Returns the 8 × 8 magenta and black checkerboard drawn for a failed resource in dev mode, created on the render thread at its first use. |
Throwable | getError() | Returns the error that made the data fail, or null. |
boolean | isUploaded() | If true, the pixels are on the GPU. |
boolean | isLoaded() | If true, the decode ended without error. |
Actions
Section titled “Actions”| Returns | Signature | Summary |
|---|---|---|
this | texture(ITexture texture) fluent | Sets one texture, such as a texture borrowed from the application. |
this | region(int x, int y, int width, int height) fluent | Sets the part of the textures at x, y of width × height texture pixels that holds the image; the size of the data becomes that of the region. |
ResourceData | fork(IResourceDecoder decoder) | Returns a data with the same id, read by decoder, for a separate playback of the same file. |
void | dispatch(Runnable task) | Runs task on a thread of its own named ResourceTask/ and the id, such as reading a remote asset. |
void | await() | Waits for the detection and the decode in progress to end. |
void | awaitDetection() | Waits for the detection of the format in progress to end. |
void | detectDecoder(boolean async) | Reads the first bytes of the asset and picks the decoder of its format, on a background thread when async is true and the asset is remote. |
void | generate(boolean async) | Starts the decode, on the background pool when async is true; a decode already running is not started again. |
void | upload() | Sends the decoded pixels to the GPU, through the decoder when it has one, then drops them from memory; JOID calls it on the render thread. |
void | uploadTextures() | Allocates each texture at the size of the data and uploads its pixels; a decoder calls it from its own upload. |
void | fail(Throwable error) | Makes the data fail with error and notifies the onError listeners of every handle; only the first error counts. |
void | listen(Resource resource) | Registers a handle to notify when the data loads or fails; the data keeps it alive until then. |
void | clear() | Frees the textures and the pixels, through the decoder, and resets the state so that the next draw decodes again; textures still read by a running decode are freed when it ends. |
ITexture | placeholder() static | Returns a new 1 × 1 transparent texture, for a decoder that has nothing to show yet. |
this | reload(IResourceDecoder decoder) fluent | Waits for a running decode, clears the data and its region, and sets decoder as its decoder. |
Static methods
Section titled “Static methods”| Returns | Signature | Summary |
|---|---|---|
void | releaseCollected() static | Clears the data that no handle uses any more; each draw of a UI bridge calls it. |
Creation details
Section titled “Creation details”create
Section titled “create”ResourceData create(String uniqueId, IResourceDecoder decoder) factory
Creates the data cached under uniqueId, read by decoder, or by the decoder that detectDecoder finds when it is null.
Property details
Section titled “Property details”uniqueId
Section titled “uniqueId”this uniqueId(String uniqueId)fluentString getUniqueId()
Default: set at creation
The id under which the data is cached and named in the warnings.
decoder
Section titled “decoder”this decoder(IResourceDecoder decoder)fluentIResourceDecoder getDecoder()
Default: set at creation; null until the format is detected
The IResourceDecoder that reads the data; decoder(IResourceDecoder) sets it and calls its init.
assetSource
Section titled “assetSource”this assetSource(Supplier<Asset> assetSource)fluent signal
Default: null
Where the data reads its asset when it detects its format: assetSource(Supplier) sets it, read again on each detection, so that a reload finds a file that appeared since.
textures
Section titled “textures”this textures(ITexture[] textures)fluentITexture[] getTextures()
Default: null
The GPU textures of the data, one per plane or per frame buffer of the decoder: getTextures() returns a copy, textures(ITexture[]) sets them.
this data(int[][] data)fluentint[][] getData()
Default: null
The decoded pixels, one 0xAARRGGBB array per texture, waiting for the upload: getData() returns a copy of the outer array, data(int[][]) sets it; null once uploaded.
generated
Section titled “generated”this generated(boolean generated)fluentboolean isGenerated()
Default: false
If true, the decode has started; generated(boolean) sets it.
this width(int width)fluentint getWidth()
Default: 0
The width of the image in pixels, or of the region when one is set; width(int) sets the width of the image.
height
Section titled “height”this height(int height)fluentint getHeight()
Default: 0
The height of the image in pixels, or of the region when one is set; height(int) sets the height of the image.
Method details
Section titled “Method details”isFailed
Section titled “isFailed”boolean isFailed()
If true, the data failed; ResourceData.getError tells why.
isDispatching
Section titled “isDispatching”boolean isDispatching()
If true, a background task started by dispatch, such as the detection of a remote format, is still running.
getForks
Section titled “getForks”List<ResourceData> getForks()
Returns the data of the separate playbacks forked from this one, as a new list.
getDecoder
Section titled “getDecoder”<T extends IResourceDecoder> T getDecoder(Class<T> clazz)
Returns the decoder when it is an instance of clazz, otherwise null.
getRegion
Section titled “getRegion”int[] getRegion()
Returns a copy of the region of the textures that holds the image, as x, y, width, height in texture pixels, or null for the whole texture.
getData
Section titled “getData”int[] getData(int index)
Returns the pixels waiting for the texture at index, or null.
getTexture
Section titled “getTexture”ITexture getTexture(int index)
Returns the texture at index, or null.
getTextureCount
Section titled “getTextureCount”int getTextureCount()
Returns the number of textures.
getMissingTexture
Section titled “getMissingTexture”ITexture getMissingTexture()
Returns the 8 × 8 magenta and black checkerboard drawn for a failed resource in dev mode, created on the render thread at its first use.
getError
Section titled “getError”Throwable getError()
Returns the error that made the data fail, or null.
isUploaded
Section titled “isUploaded”boolean isUploaded()
If true, the pixels are on the GPU.
isLoaded
Section titled “isLoaded”boolean isLoaded()
If true, the decode ended without error.
texture
Section titled “texture”this texture(ITexture texture) fluent
Sets one texture, such as a texture borrowed from the application.
region
Section titled “region”this region(int x, int y, int width, int height) fluent
Sets the part of the textures at x, y of width × height texture pixels that holds the image; the size of the data becomes that of the region.
ResourceData fork(IResourceDecoder decoder)
Returns a data with the same id, read by decoder, for a separate playback of the same file.
dispatch
Section titled “dispatch”void dispatch(Runnable task)
Runs task on a thread of its own named ResourceTask/ and the id, such as reading a remote asset.
void await()
Waits for the detection and the decode in progress to end.
awaitDetection
Section titled “awaitDetection”void awaitDetection()
Waits for the detection of the format in progress to end.
detectDecoder
Section titled “detectDecoder”void detectDecoder(boolean async)
Reads the first bytes of the asset and picks the decoder of its format, on a background thread when async is true and the asset is remote.
A failure to read or an unknown format makes the data fail.
generate
Section titled “generate”void generate(boolean async)
Starts the decode, on the background pool when async is true; a decode already running is not started again.
Data built from textures only, without a decoder, loads at once.
upload
Section titled “upload”void upload()
Sends the decoded pixels to the GPU, through the decoder when it has one, then drops them from memory; JOID calls it on the render thread.
uploadTextures
Section titled “uploadTextures”void uploadTextures()
Allocates each texture at the size of the data and uploads its pixels; a decoder calls it from its own upload.
void fail(Throwable error)
Makes the data fail with error and notifies the onError listeners of every handle; only the first error counts.
In dev mode, it warns with the cause.
listen
Section titled “listen”void listen(Resource resource)
Registers a handle to notify when the data loads or fails; the data keeps it alive until then.
void clear()
Frees the textures and the pixels, through the decoder, and resets the state so that the next draw decodes again; textures still read by a running decode are freed when it ends.
placeholder
Section titled “placeholder”ITexture placeholder() static
Returns a new 1 × 1 transparent texture, for a decoder that has nothing to show yet.
reload
Section titled “reload”this reload(IResourceDecoder decoder) fluent
Waits for a running decode, clears the data and its region, and sets decoder as its decoder.
releaseCollected
Section titled “releaseCollected”void releaseCollected() static
Clears the data that no handle uses any more; each draw of a UI bridge calls it.
fluent returns the instance, typed for chaining · factory creates an instance · signal also accepts a value that follows signals, as a signal, an expression or a lambda (When does JOID compute this value again?) · hook called by JOID, override it · abstract you implement it · default interface method with a body · static called on the class · protected visible to subclasses
