Skip to content

Commit

Permalink
Fetch network request although the image is cached
Browse files Browse the repository at this point in the history
  • Loading branch information
ustc-zzzz committed Aug 12, 2021
1 parent a990745 commit 90824cf
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 32 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ apply plugin: 'net.minecraftforge.gradle'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'

version = '0.5.1'
version = '0.5.2'
group = 'org.teaconmc'
archivesBaseName = 'SlideShow'

Expand Down
76 changes: 45 additions & 31 deletions src/main/java/org/teacon/slides/renderer/SlideState.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,48 +113,63 @@ public boolean tick(String location) {
return false;
}

private void loadImageCache(URI uri) {
if (mState != State.LOADED) {
SlideImageStore.getImage(uri, true).thenAccept(this::loadImage).exceptionally(e -> {
RenderSystem.recordRenderCall(() -> {
mSlide = Slide.failed();
mState = State.FAILED_OR_EMPTY;
mCounter = RETRY_INTERVAL_TICKS;
});
return null;
private void loadImageRemote(URI uri, boolean releaseOld) {
SlideImageStore.getImage(uri, true).thenAccept(data -> {
RenderSystem.recordRenderCall(() -> {
if (mState != State.LOADED) {
if (releaseOld) {
mSlide.release();
}
mSlide = Slide.make(loadImage(data));
mState = State.LOADED;
}
});
}
}).exceptionally(e -> {
RenderSystem.recordRenderCall(() -> {
if (releaseOld) {
mSlide.release();
}
mSlide = Slide.failed();
mState = State.FAILED_OR_EMPTY;
mCounter = RETRY_INTERVAL_TICKS;
});
return null;
});
}

private void loadImage(URI uri) {
SlideImageStore.getImage(uri, false).thenAccept(this::loadImage).exceptionally(e -> {
RenderSystem.recordRenderCall(() -> loadImageCache(uri));
SlideImageStore.getImage(uri, false).thenAccept(data -> {
RenderSystem.recordRenderCall(() -> {
if (mState != State.LOADED) {
mSlide = Slide.make(loadImage(data));
loadImageRemote(uri, true);
}
});
}).exceptionally(e -> {
RenderSystem.recordRenderCall(() -> {
if (mState != State.LOADED) {
loadImageRemote(uri, false);
}
});
return null;
});
mSlide = Slide.loading();
mState = State.LOADING;
mCounter = RECYCLE_TICKS;
}

private void loadImage(byte[] data) {
RenderSystem.recordRenderCall(() -> {
if (mState != State.LOADED) {
try {
// specifying null will use image source channels
// vanilla minecraft did this on render thread, so it should be ok
NativeImage image = NativeImage.read(null, new ByteArrayInputStream(data));
loadTexture(image);
} catch (Exception e) {
throw new CompletionException(e);
}
}
});
private int loadImage(byte[] data) {
try {
// specifying null will use image source channels
// vanilla minecraft did this on render thread, so it should be ok
NativeImage image = NativeImage.read(null, new ByteArrayInputStream(data));
return loadTexture(image);
} catch (Exception e) {
throw new CompletionException(e);
}
}

private void loadTexture(@Nonnull NativeImage image) {
if (mState == State.LOADED) {
return;
}
private int loadTexture(@Nonnull NativeImage image) {
int texture = TextureUtil.generateTextureId();
// specify maximum mipmap level to 2
TextureUtil.prepareImage(image.getFormat() == NativeImage.PixelFormat.RGB ?
Expand Down Expand Up @@ -188,8 +203,7 @@ private void loadTexture(@Nonnull NativeImage image) {
// auto generate mipmap
GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);

mSlide = Slide.make(texture);
mState = State.LOADED;
return texture;
}

@Nullable
Expand Down

0 comments on commit 90824cf

Please sign in to comment.