Skip to content

Commit

Permalink
Move loadImage back
Browse files Browse the repository at this point in the history
  • Loading branch information
ustc-zzzz committed Aug 13, 2021
1 parent f2f909a commit ac418d1
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions src/main/java/org/teacon/slides/renderer/SlideState.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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<Integer> loadImage(byte[] data) {
CompletableFuture<Integer> 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) {
Expand Down

0 comments on commit ac418d1

Please sign in to comment.