From ad9f544fe132be17bf9e4b41021f9cdac087f1ac Mon Sep 17 00:00:00 2001 From: seven332 Date: Tue, 3 May 2016 00:52:03 +0800 Subject: [PATCH] Add GLView.onPause and GLView.onResume --- app/build.gradle | 2 +- .../ehviewer/gallery/gl/GalleryView.java | 6 +- .../java/com/hippo/gl/view/GLRootView.java | 188 +++++++++--------- .../main/java/com/hippo/gl/view/GLView.java | 24 ++- 4 files changed, 118 insertions(+), 102 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 107b457cc..34b67a7c8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -68,7 +68,7 @@ dependencies { compile 'com.github.seven332:refreshlayout:0.1.0' compile 'com.github.seven332:ripple:0.1.1' compile 'com.github.seven332:streampipe:0.1.0' - compile 'com.github.seven332:tuxiang:0.1.1' + compile 'com.github.seven332:tuxiang:0.1.2' compile 'com.github.seven332:unifile:0.1.3' compile 'com.github.seven332:yorozuya-collect:0.1.2' compile 'com.github.seven332:yorozuya-thread:0.1.0' diff --git a/app/src/main/java/com/hippo/ehviewer/gallery/gl/GalleryView.java b/app/src/main/java/com/hippo/ehviewer/gallery/gl/GalleryView.java index 3bdf445f6..4a9bde080 100644 --- a/app/src/main/java/com/hippo/ehviewer/gallery/gl/GalleryView.java +++ b/app/src/main/java/com/hippo/ehviewer/gallery/gl/GalleryView.java @@ -116,7 +116,6 @@ public class GalleryView extends GLView implements GestureRecognizer.Listener { private boolean mScale = false; private boolean mScroll = false; private boolean mFirstScroll = false; - private boolean mTouched = false; private final Rect mLeftArea = new Rect(); private final Rect mRightArea = new Rect(); @@ -142,7 +141,6 @@ public class GalleryView extends GLView implements GestureRecognizer.Listener { private final List mArgsListTemp = new ArrayList<>(5); private final AtomicInteger mCurrentIndex = new AtomicInteger(GalleryPageView.INVALID_INDEX); - private boolean mPause; @SuppressWarnings("deprecation") public GalleryView(@NonNull Context context, @NonNull Adapter adapter, @@ -547,7 +545,6 @@ public class GalleryView extends GLView implements GestureRecognizer.Listener { } private void onDownInternal(float x, float y) { - mTouched = true; mScale = false; mScroll = false; mFirstScroll = true; @@ -557,7 +554,6 @@ public class GalleryView extends GLView implements GestureRecognizer.Listener { } private void onUpInternal() { - mTouched = false; if (mLayoutManager != null) { mLayoutManager.onUp(); } @@ -803,7 +799,7 @@ public class GalleryView extends GLView implements GestureRecognizer.Listener { @RenderThread public GalleryPageView findPageByIndex(int id) { - if (!mPause && mLayoutManager != null) { + if (mLayoutManager != null) { return mLayoutManager.findPageByIndex(id); } else { return null; diff --git a/app/src/main/java/com/hippo/gl/view/GLRootView.java b/app/src/main/java/com/hippo/gl/view/GLRootView.java index 51abb5cd0..b59be50ec 100644 --- a/app/src/main/java/com/hippo/gl/view/GLRootView.java +++ b/app/src/main/java/com/hippo/gl/view/GLRootView.java @@ -63,7 +63,7 @@ import javax.microedition.khronos.opengles.GL11; // (2) The public methods of CameraHeadUpDisplay // (3) The overridden methods in GLRootView. public class GLRootView extends GLSurfaceView - implements Renderer, GLRoot { + implements GLRoot { private static final String TAG = "GLRootView"; private static final boolean DEBUG_FPS = false; @@ -107,9 +107,98 @@ public class GLRootView extends GLSurfaceView mRenderLock.newCondition(); private boolean mFreeze; - private long mLastDrawFinishTime; private boolean mInDownState = false; - //private boolean mFirstDraw = true; + + private class GLRootRenderer implements Renderer { + + /** + * Called when the context is created, possibly after automatic destruction. + */ + @Override + public void onSurfaceCreated(GL10 gl1, EGLConfig config) { + GL11 gl = (GL11) gl1; + if (mGL != null) { + // The GL Object has changed + Log.i(TAG, "GLObject has changed from " + mGL + " to " + gl); + } + mRenderLock.lock(); + try { + mGL = gl; + mCanvas = new GLES20Canvas(); + BasicTexture.invalidateAllTextures(); + } finally { + mRenderLock.unlock(); + } + + if (DEBUG_FPS) { + setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY); + } else { + setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); + } + } + + /** + * Called when the OpenGL surface is recreated without destroying the + * context. + */ + @Override + public void onSurfaceChanged(GL10 gl1, int width, int height) { + Log.i(TAG, "onSurfaceChanged: " + width + "x" + height + ", gl10: " + gl1.toString()); + Process.setThreadPriority(Process.THREAD_PRIORITY_DISPLAY); + GalleryUtils.setRenderThread(); + GL11 gl = (GL11) gl1; + AssertUtils.assertTrue(mGL == gl); + + mCanvas.setSize(width, height); + } + + @Override + public void onDrawFrame(GL10 gl) { + AnimationTime.update(); + + long t0 = System.nanoTime(); + + mRenderLock.lock(); + + while (mFreeze) { + mFreezeCondition.awaitUninterruptibly(); + } + + try { + onDrawFrameLocked(gl); + } finally { + mRenderLock.unlock(); + } + + long t = System.nanoTime(); + long duration = (t - t0) / 1000000; + + if (duration > 34) { + Log.v(TAG, "--- " + duration + " ---"); + } + } + + @Override + public void onSurfaceDestroyed() { + if (mContentView != null && mContentView.isAttachedToRoot()) { + mContentView.detachFromRoot(); + } + } + + @Override + public void onPause() { + if (mContentView != null) { + mContentView.pause(); + } + } + + @Override + public void onResume() { + if (mContentView != null) { + mContentView.resume(); + } + } + } public GLRootView(Context context) { this(context, null); @@ -124,7 +213,7 @@ public class GLRootView extends GLSurfaceView final int eglContextClientVersion = 2; setEGLContextClientVersion(eglContextClientVersion); setEGLConfigChooser(new BestConfigChooser(eglContextClientVersion)); - setRenderer(this); + setRenderer(new GLRootRenderer()); getHolder().setFormat(PixelFormat.RGB_888); // Uncomment this to enable gl error check. @@ -275,56 +364,6 @@ public class GLRootView extends GLSurfaceView if (changed) requestLayoutContentPane(); } - /** - * Called when the context is created, possibly after automatic destruction. - */ - // This is a GLSurfaceView.Renderer callback - @Override - public void onSurfaceCreated(GL10 gl1, EGLConfig config) { - GL11 gl = (GL11) gl1; - if (mGL != null) { - // The GL Object has changed - Log.i(TAG, "GLObject has changed from " + mGL + " to " + gl); - } - mRenderLock.lock(); - try { - mGL = gl; - mCanvas = new GLES20Canvas(); - BasicTexture.invalidateAllTextures(); - } finally { - mRenderLock.unlock(); - } - - if (DEBUG_FPS) { - setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY); - } else { - setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); - } - } - - /** - * Called when the OpenGL surface is recreated without destroying the - * context. - */ - // This is a GLSurfaceView.Renderer callback - @Override - public void onSurfaceChanged(GL10 gl1, int width, int height) { - Log.i(TAG, "onSurfaceChanged: " + width + "x" + height + ", gl10: " + gl1.toString()); - Process.setThreadPriority(Process.THREAD_PRIORITY_DISPLAY); - GalleryUtils.setRenderThread(); - GL11 gl = (GL11) gl1; - AssertUtils.assertTrue(mGL == gl); - - mCanvas.setSize(width, height); - } - - @Override - public void onSurfaceDestroyed() { - if (mContentView != null && mContentView.isAttachedToRoot()) { - mContentView.detachFromRoot(); - } - } - private void outputFps() { long now = System.nanoTime(); if (mFrameCountingStart == 0) { @@ -338,47 +377,6 @@ public class GLRootView extends GLSurfaceView ++mFrameCount; } - @Override - public void onDrawFrame(GL10 gl) { - AnimationTime.update(); - - long t0 = System.nanoTime(); - - mRenderLock.lock(); - - while (mFreeze) { - mFreezeCondition.awaitUninterruptibly(); - } - - try { - onDrawFrameLocked(gl); - } finally { - mRenderLock.unlock(); - } - - // We put a black cover View in front of the SurfaceView and hide it - // after the first draw. This prevents the SurfaceView being transparent - // before the first draw. - //if (mFirstDraw) { - // mFirstDraw = false; - // post(new Runnable() { - // @Override - // public void run() { - // View root = getRootView(); - // View cover = root.findViewById(R.id.gl_root_cover); - // cover.setVisibility(GONE); - // } - // }); - //} - - long t = System.nanoTime(); - long duration = (t - t0) / 1000000; - - if (duration > 34) { - Log.v(TAG, "--- " + duration + " ---"); - } - } - private void onDrawFrameLocked(GL10 gl) { if (DEBUG_FPS) outputFps(); // release the unbound textures and deleted buffers. diff --git a/app/src/main/java/com/hippo/gl/view/GLView.java b/app/src/main/java/com/hippo/gl/view/GLView.java index d8f205b57..edfdfa369 100644 --- a/app/src/main/java/com/hippo/gl/view/GLView.java +++ b/app/src/main/java/com/hippo/gl/view/GLView.java @@ -212,7 +212,7 @@ public class GLView implements TouchOwner { } // This should only be called on the content pane (the topmost GLView). - public void attachToRoot(GLRoot root) { + void attachToRoot(GLRoot root) { AssertUtils.assertTrue(mParent == null && mRoot == null); onAttachToRoot(root); } @@ -223,6 +223,16 @@ public class GLView implements TouchOwner { onDetachFromRoot(); } + // This should only be called on the content pane (the topmost GLView). + void pause() { + onPause(); + } + + // This should only be called on the content pane (the topmost GLView). + void resume() { + onResume(); + } + public boolean isAttachedToRoot() { return mRoot != null; } @@ -1115,6 +1125,18 @@ public class GLView implements TouchOwner { mRoot = null; } + public void onPause() { + for (int i = 0, n = getComponentCount(); i < n; ++i) { + getComponent(i).onPause(); + } + } + + public void onResume() { + for (int i = 0, n = getComponentCount(); i < n; ++i) { + getComponent(i).onResume(); + } + } + public void lockRendering() { if (mRoot != null) { mRoot.lockRenderThread();