Skip to content

Resource resolvers

Resource.of accepts files, URLs, streams and assets, and the textures that a backend lends (Show a texture of your application gives the user side). The host of your backend also has images of its own: a texture it rendered, a sprite of its atlas, a video frame on the GPU. A borrowed texture lets JOID draw them without copying a pixel, and a resource resolver lets Resource.of accept the objects of your engine directly.

  1. Resource.of(input) asks the resolvers of ResourceResolver in turn, the last registered first.
  2. The first one whose supports(input) returns true builds the resource.
  3. When none does, the input goes through the asset locators and the formats (See how JOID loads a resource).

The core registers two resolvers: one for an ITexture, which turns any texture into a resource, and one for a BufferedImage. So a texture that your backend lends is a resource already: Resource.of(texture).

A BorrowedTexture wraps a handle of the host behind a Supplier. JOID reads the supplier each time it draws, so the texture follows the host when it replaces its handle (a resize, a new frame of a video).

public final class EngineBorrowedTexture extends BorrowedTexture<Integer> implements IEngineTexture {
private final EngineDevice device;
public EngineBorrowedTexture(final EngineDevice device, final Supplier<Integer> texture) {
super(texture);
this.device = device;
}
@Override
public int getId() {
return super.getHandle();
}
@Override
protected int getWidth(final Integer texture) {
return this.device.getTextureWidth(texture);
}
@Override
protected int getHeight(final Integer texture) {
return this.device.getTextureHeight(texture);
}
@Override
protected boolean isValid(final Integer texture) {
return this.device.isTexture(texture);
}
@Override
protected boolean isMipmapped(final Integer texture) {
return this.device.getTextureLevels(texture) > 1;
}
}

Full file: EngineBorrowedTexture.java

HookWhat it answers for the handle
getWidth(handle), getHeight(handle)Its size in pixels.
isValid(handle)Whether the host still owns it.
isMipmapped(handle)Whether it has mip levels, so that JOID samples it with a mipmap filter.

A null handle reads as a texture of size 0, which JOID does not draw. JOID never writes into a borrowed texture nor deletes it: allocate and upload throw an UnsupportedOperationException, and delete does nothing.

Your render bridge binds a borrowed texture like its own textures: here both implement IEngineTexture, so drawPrimitive reads the id of either. On OpenGL and Vulkan, GlBorrowedTexture and VulkanBorrowedTexture do it already, created by of(...) once their render bridge is registered.

A resolver implements IResourceResolver and turns an object of the engine into a resource:

public final class EngineSpriteResourceResolver implements IResourceResolver {
private final EngineDevice device;
public EngineSpriteResourceResolver(final EngineDevice device) {
this.device = device;
}
@Override
public boolean supports(final Object input) {
return input instanceof EngineSprite;
}
@Override
public Resource resolve(final ResourceBuilder builder, final Object input) {
final EngineSprite sprite = (EngineSprite) input;
final String uniqueId = "engine_sprite_" + sprite.getTexture();
return builder.compute(uniqueId, () -> {
final EngineBorrowedTexture texture = new EngineBorrowedTexture(this.device, sprite::getTexture);
return ResourceData.create(uniqueId, null).texture(texture);
});
}
}

Full file: EngineSpriteResourceResolver.java

MemberWhat it must do
IResourceResolver.supportsAnswer from the type of the input only, quickly: it runs for every Resource.of.
IResourceResolver.resolveGive the resource a unique id, so that two calls with the same object share one cached texture through builder.compute(...), each in its own Resource.

Register it from registerDefaults() with ResourceResolver.register(...) (Writing a backend): it then comes before the resolvers of the core.

BorrowedTextureContractSuite checks a borrowed texture on a real context (Testing a backend):

RuleTest
JOID draws the texture of the host.drawsABorrowedTexture
Releasing the resource keeps the texture of the host alive.keepsTheBorrowedTextureOnceTheResourceIsReleased
Writing into it fails.refusesToWriteABorrowedTexture
JOID reads whether it has mip levels.readsTheMipLevelsOfTheBorrowedTexture
The host finds its texture parameters unchanged after the frame.restoresTheTextureParametersAfterTheFrame
A new handle from the supplier is drawn on the next frame.followsTheHandleOfItsSupplier

Report a problem with this page