diff --git a/app/src/main/java/com/hippo/ehviewer/gallery/GalleryProvider.java b/app/src/main/java/com/hippo/ehviewer/gallery/GalleryProvider.java index 97c45720d..d83f3c896 100644 --- a/app/src/main/java/com/hippo/ehviewer/gallery/GalleryProvider.java +++ b/app/src/main/java/com/hippo/ehviewer/gallery/GalleryProvider.java @@ -16,14 +16,14 @@ package com.hippo.ehviewer.gallery; -import android.os.Handler; import android.support.annotation.IntDef; import android.support.annotation.UiThread; +import com.hippo.gl.glrenderer.GLCanvas; +import com.hippo.gl.view.GLRoot; import com.hippo.image.Image; import com.hippo.yorozuya.OSUtils; import com.hippo.yorozuya.Pool; -import com.hippo.yorozuya.SimpleHandler; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -41,9 +41,9 @@ public abstract class GalleryProvider { ".gif", // Graphics Interchange Format }; - private GalleryProviderListener mGalleryProviderListener; - private Pool mNotifyTaskPool = new Pool<>(5); - private Handler mHandler = SimpleHandler.getInstance(); + private final Pool mNotifyTaskPool = new Pool<>(5); + private volatile GalleryProviderListener mGalleryProviderListener; + private volatile GLRoot mGLRoot; private boolean mStarted = false; @@ -62,6 +62,11 @@ public abstract class GalleryProvider { OSUtils.checkMainLoop(); } + public void setGLRoot(GLRoot glRoot) { + mGLRoot = glRoot; + } + + /** * @return {@link #STATE_WAIT} for wait, 0 for empty */ @@ -101,15 +106,20 @@ public abstract class GalleryProvider { return; } + GLRoot glRoot = mGLRoot; + if (glRoot == null) { + return; + } + NotifyTask task = mNotifyTaskPool.pop(); if (task == null) { task = new NotifyTask(listener, mNotifyTaskPool); } task.setData(type, index, percent, image, error); - mHandler.post(task); + glRoot.addOnGLIdleListener(task); } - private static class NotifyTask implements Runnable { + private static class NotifyTask implements GLRoot.OnGLIdleListener { @IntDef({TYPE_DATA_CHANGED, TYPE_PERCENT, TYPE_SUCCEED, TYPE_FAILED}) @Retention(RetentionPolicy.SOURCE) @@ -144,7 +154,7 @@ public abstract class GalleryProvider { } @Override - public void run() { + public boolean onGLIdle(GLCanvas canvas, boolean renderRequested) { switch (mType) { case TYPE_DATA_CHANGED: if (mIndex < 0) { @@ -169,6 +179,8 @@ public abstract class GalleryProvider { mError = null; // Push back mPool.push(this); + + return false; } } } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/GalleryActivity.java b/app/src/main/java/com/hippo/ehviewer/ui/GalleryActivity.java index 65344665d..90f272ff6 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/GalleryActivity.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/GalleryActivity.java @@ -137,6 +137,7 @@ public class GalleryActivity extends AppCompatActivity implements GalleryProvide setContentView(R.layout.activity_gallery); GLRootView glRootView = (GLRootView) findViewById(R.id.gl_root_view); + mGalleryProvider.setGLRoot(glRootView); mUploader = new ImageTexture.Uploader(glRootView); mIndexIdMap = new SparseIntArray(); mPageTextTexture = MovableTextTexture.create(Typeface.DEFAULT,