From ac418d17f933908251355f21ad3d6e776e12f738 Mon Sep 17 00:00:00 2001 From: Yanbing Zhao Date: Fri, 13 Aug 2021 15:23:54 +0800 Subject: [PATCH] Move loadImage back --- .../teacon/slides/renderer/SlideState.java | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/teacon/slides/renderer/SlideState.java b/src/main/java/org/teacon/slides/renderer/SlideState.java index 8f95f0e..0284338 100644 --- a/src/main/java/org/teacon/slides/renderer/SlideState.java +++ b/src/main/java/org/teacon/slides/renderer/SlideState.java @@ -22,7 +22,7 @@ import java.net.URI; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.CompletionException; +import java.util.concurrent.CompletableFuture; /** * @author BloCamLimb @@ -114,17 +114,14 @@ public boolean tick(String location) { } private void loadImageRemote(URI uri, boolean releaseOld) { - SlideImageStore.getImage(uri, true).thenAccept(data -> { - final int texture = loadImage(data); - RenderSystem.recordRenderCall(() -> { - if (mState != State.LOADED) { - if (releaseOld) { - mSlide.release(); - } - mSlide = Slide.make(texture); - mState = State.LOADED; + SlideImageStore.getImage(uri, true).thenCompose(this::loadImage).thenAccept(texture -> { + if (mState != State.LOADED) { + if (releaseOld) { + mSlide.release(); } - }); + mSlide = Slide.make(texture); + mState = State.LOADED; + } }).exceptionally(e -> { RenderSystem.recordRenderCall(() -> { if (releaseOld) { @@ -139,8 +136,7 @@ private void loadImageRemote(URI uri, boolean releaseOld) { } private void loadImage(URI uri) { - SlideImageStore.getImage(uri, false).thenAccept(data -> { - final int texture = loadImage(data); + SlideImageStore.getImage(uri, true).thenCompose(this::loadImage).thenAccept(texture -> { RenderSystem.recordRenderCall(() -> { if (mState != State.LOADED) { mSlide = Slide.make(texture); @@ -160,14 +156,19 @@ private void loadImage(URI uri) { mCounter = RECYCLE_TICKS; } - private int loadImage(byte[] data) { - try { - // specifying null will use image source channels - NativeImage image = NativeImage.read(null, new ByteArrayInputStream(data)); - return loadTexture(image); - } catch (Exception e) { - throw new CompletionException(e); - } + private CompletableFuture loadImage(byte[] data) { + CompletableFuture future = new CompletableFuture<>(); + RenderSystem.recordRenderCall(() -> { + try { + // specifying null will use image source channels + ByteArrayInputStream inputStream = new ByteArrayInputStream(data); + NativeImage image = NativeImage.read(null, inputStream); + future.complete(loadTexture(image)); + } catch (Exception e) { + future.completeExceptionally(e); + } + }); + return future; } private int loadTexture(@Nonnull NativeImage image) {