Add GLView.onPause and GLView.onResume

This commit is contained in:
seven332 committed 2016-05-03 00:52:03 +08:00
1 parent 4c3c84f1a6
commit ad9f544fe1
4 files changed
+118 -102

No files matched your search

@@ -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<Object[]> 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;
@@ -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.
@@ -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();