Skip to content

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.

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.

ReturnsSignatureSummary
ResourceDatacreate(String uniqueId, IResourceDecoder decoder) factoryCreates the data cached under uniqueId, read by decoder, or by the decoder that detectDecoder finds when it is null.

PropertyTypeDefaultSummary
uniqueIdStringset at creationThe id under which the data is cached and named in the warnings.
decoderIResourceDecoderset at creation; null until the format is detectedThe IResourceDecoder that reads the data; decoder(IResourceDecoder) sets it and calls its init.
assetSource signalAssetnullWhere 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.
texturesITexture[]nullThe GPU textures of the data, one per plane or per frame buffer of the decoder: getTextures() returns a copy, textures(ITexture[]) sets them.
dataint[][]nullThe 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.
generatedbooleanfalseIf true, the decode has started; generated(boolean) sets it.
widthint0The width of the image in pixels, or of the region when one is set; width(int) sets the width of the image.
heightint0The height of the image in pixels, or of the region when one is set; height(int) sets the height of the image.

ReturnsSignatureSummary
booleanisFailed()If true, the data failed; ResourceData.getError tells why.
booleanisDispatching()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.
TgetDecoder(Class clazz)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.
ITexturegetTexture(int index)Returns the texture at index, or null.
intgetTextureCount()Returns the number of textures.
ITexturegetMissingTexture()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.
ThrowablegetError()Returns the error that made the data fail, or null.
booleanisUploaded()If true, the pixels are on the GPU.
booleanisLoaded()If true, the decode ended without error.
ReturnsSignatureSummary
thistexture(ITexture texture) fluentSets one texture, such as a texture borrowed from the application.
thisregion(int x, int y, int width, int height) fluentSets 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.
ResourceDatafork(IResourceDecoder decoder)Returns a data with the same id, read by decoder, for a separate playback of the same file.
voiddispatch(Runnable task)Runs task on a thread of its own named ResourceTask/ and the id, such as reading a remote asset.
voidawait()Waits for the detection and the decode in progress to end.
voidawaitDetection()Waits for the detection of the format in progress to end.
voiddetectDecoder(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.
voidgenerate(boolean async)Starts the decode, on the background pool when async is true; a decode already running is not started again.
voidupload()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.
voiduploadTextures()Allocates each texture at the size of the data and uploads its pixels; a decoder calls it from its own upload.
voidfail(Throwable error)Makes the data fail with error and notifies the onError listeners of every handle; only the first error counts.
voidlisten(Resource resource)Registers a handle to notify when the data loads or fails; the data keeps it alive until then.
voidclear()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.
ITextureplaceholder() staticReturns a new 1 × 1 transparent texture, for a decoder that has nothing to show yet.
thisreload(IResourceDecoder decoder) fluentWaits for a running decode, clears the data and its region, and sets decoder as its decoder.
ReturnsSignatureSummary
voidreleaseCollected() staticClears the data that no handle uses any more; each draw of a UI bridge calls it.

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.

  • this uniqueId(String uniqueId) fluent
  • String getUniqueId()

Default: set at creation

The id under which the data is cached and named in the warnings.

  • this decoder(IResourceDecoder decoder) fluent
  • IResourceDecoder 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.

  • 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.

  • this textures(ITexture[] textures) fluent
  • ITexture[] 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) fluent
  • int[][] 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.

  • this generated(boolean generated) fluent
  • boolean isGenerated()

Default: false

If true, the decode has started; generated(boolean) sets it.

  • this width(int width) fluent
  • int 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.

  • this height(int height) fluent
  • int 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.

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 extends IResourceDecoder> T getDecoder(Class<T> clazz)

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.

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. A failure to read or an unknown format makes the data fail.

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.

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. In dev mode, it warns with the cause.

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.

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

Report a problem with this page