Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,32 @@ public final class Image extends Resource implements Drawable {

private List<Consumer<Image>> onDisposeListeners;

private record CachedHandle(ImageHandle handleContainer, int requestedWidth, int requestedHeight, int handleWidth,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't handleWidth and handleHeight obsolete as it's already contained in ImageHandle handleContainer?

int handleHeight) {

public void destroy() {
if (handleContainer != null) {
handleContainer.destroy();
}
}

public boolean isReusable(int height, int width) {
return (requestedHeight == height && requestedWidth == width)
|| (handleHeight == height && handleWidth == width);

}

public long getHandle() {
if (handleContainer != null) {
return handleContainer.handle;
}
return -1;
}
};

// Initialize lastRequestedHandle with -1 for size-related fields to indicate uninitialized values
CachedHandle lastRequestedHandle = new CachedHandle(null, -1, -1, -1, -1);

private Image (Device device, int type, long handle, int nativeZoom) {
super(device);
this.type = type;
Expand Down Expand Up @@ -826,19 +852,16 @@ interface HandleAtSizeConsumer {
}

void executeOnImageHandleAtSize(HandleAtSizeConsumer handleAtSizeConsumer, int widthHint, int heightHint) {
ImageData imageData;
imageData = this.imageProvider.loadImageDataAtSize(widthHint, heightHint);
executeOnImageHandle(handleAtSizeConsumer, imageData);
}

private void executeOnImageHandle(HandleAtSizeConsumer handleAtSizeConsumer, ImageData imageData) {
ImageHandle handleContainer = init(imageData, -1);
long tempHandle = handleContainer.handle;
try {
handleAtSizeConsumer.accept(tempHandle, new Point(imageData.width, imageData.height));
} finally {
handleContainer.destroy();
if (!lastRequestedHandle.isReusable(heightHint, widthHint)) {
ImageData imageData;
imageData = this.imageProvider.loadImageDataAtSize(widthHint, heightHint);
lastRequestedHandle.destroy();
ImageHandle handleContainer = init(imageData, -1);
lastRequestedHandle = new CachedHandle(handleContainer, widthHint, heightHint, imageData.width,
imageData.height);
}
handleAtSizeConsumer.accept(lastRequestedHandle.getHandle(),
new Point(lastRequestedHandle.handleWidth(), lastRequestedHandle.handleHeight()));
}

/**
Expand Down Expand Up @@ -1073,6 +1096,7 @@ void destroy () {

private void destroyHandles() {
destroyHandles(__ -> true);
lastRequestedHandle.destroy();
}

@Override
Expand Down
Loading