Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/vr_shell/VrShell.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/vr_shell/VrShell.java b/chrome/android/java/src/org/chromium/chrome/browser/vr_shell/VrShell.java |
| index dd6c4d6cd6b96b0c5a2c9f85cfd09e5d1a8d5069..24949a8b478afc1b0f6bedfba4ca836812f614f3 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/vr_shell/VrShell.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/vr_shell/VrShell.java |
| @@ -12,18 +12,26 @@ import static android.opengl.GLES20.glGenTextures; |
| import static android.opengl.GLES20.glTexParameteri; |
| import android.app.Activity; |
| +import android.content.Context; |
| import android.graphics.SurfaceTexture; |
| import android.graphics.SurfaceTexture.OnFrameAvailableListener; |
| import android.opengl.GLES11Ext; |
| import android.opengl.GLSurfaceView; |
| import android.os.StrictMode; |
| import android.view.MotionEvent; |
| +import android.view.Surface; |
| +import android.view.ViewGroup; |
| import android.widget.FrameLayout; |
| import com.google.vr.ndk.base.AndroidCompat; |
| import com.google.vr.ndk.base.GvrLayout; |
| +import org.chromium.base.ThreadUtils; |
| import org.chromium.base.annotations.JNINamespace; |
| +import org.chromium.chrome.browser.tab.Tab; |
| +import org.chromium.content.browser.ContentViewCore; |
| +import org.chromium.content_public.browser.WebContents; |
| +import org.chromium.ui.base.WindowAndroid; |
| import javax.microedition.khronos.egl.EGLConfig; |
| import javax.microedition.khronos.opengles.GL10; |
| @@ -46,9 +54,35 @@ public class VrShell extends GvrLayout implements GLSurfaceView.Renderer, VrShel |
| private int mContentTextureHandle; |
| private FrameListener mContentFrameListener; |
| + private ContentViewCoreHolder mContentViewCoreHolder; |
|
Ted C
2016/09/20 19:57:07
Container is probably a more common word than Hold
mthiesse
2016/09/20 20:56:08
Done.
|
| + |
| + // The tab that holds the main ContentViewCore. |
| + private Tab mContentTab; |
| + // The ContentViewCore for the main content rect in VR. |
| + private ContentViewCore mContentCVC; |
|
Ted C
2016/09/20 19:57:06
mCVC is all you need
mthiesse
2016/09/20 20:56:08
Done.
|
| + // The parent of the @ContentView for mContentContentViewCore. |
|
Ted C
2016/09/20 19:57:07
Non-standard comment.
I think, "The non-VR contai
mthiesse
2016/09/20 20:56:07
Done.
|
| + private ViewGroup mContentCVParent; |
| + private VrWindowAndroid mContentVRWindowAndroid; |
|
Ted C
2016/09/20 19:57:06
mVRWindowAndroid is sufficient
mthiesse
2016/09/20 20:56:07
Done.
|
| + private WindowAndroid mOldContentWindowAndroid; |
| + |
| + private static class ContentViewCoreHolder extends FrameLayout { |
| + public ContentViewCoreHolder(Context context) { |
| + super(context); |
| + } |
| + |
| + @Override |
| + public boolean dispatchTouchEvent(MotionEvent event) { |
| + return true; |
| + } |
| + } |
| + |
| public VrShell(Activity activity) { |
| super(activity); |
| mActivity = activity; |
| + mContentViewCoreHolder = new ContentViewCoreHolder(activity); |
| + addView(mContentViewCoreHolder, 0, new FrameLayout.LayoutParams( |
| + FrameLayout.LayoutParams.MATCH_PARENT, |
| + FrameLayout.LayoutParams.MATCH_PARENT)); |
| mGlSurfaceView = new GLSurfaceView(getContext()); |
| mGlSurfaceView.setEGLContextClientVersion(2); |
| mGlSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 0, 0); |
| @@ -62,8 +96,42 @@ public class VrShell extends GvrLayout implements GLSurfaceView.Renderer, VrShel |
| } |
| @Override |
| - public void onNativeLibraryReady() { |
| - mNativeVrShell = nativeInit(); |
| + public void onNativeLibraryReady(Tab currentTab) { |
| + assert currentTab.getContentViewCore() != null; |
| + mContentTab = currentTab; |
| + mContentCVC = mContentTab.getContentViewCore(); |
| + mContentVRWindowAndroid = new VrWindowAndroid(mActivity); |
| + |
| + mNativeVrShell = nativeInit(mContentCVC.getWebContents(), |
| + mContentVRWindowAndroid.getNativePointer()); |
| + |
| + reparentContentWindow(); |
| + |
| + nativeUpdateCompositorLayers(mNativeVrShell); |
| + } |
| + |
| + private void reparentContentWindow() { |
| + mOldContentWindowAndroid = mContentCVC.getWindowAndroid(); |
| + |
| + // TODO(mthiesse): Update the WindowAndroid in ChromeActivity too? |
| + mContentTab.updateWindowAndroid(mContentVRWindowAndroid); |
| + |
| + ViewGroup contentContentView = mContentCVC.getContainerView(); |
| + mContentCVParent = ((ViewGroup) contentContentView.getParent()); |
| + mContentCVParent.removeView(contentContentView); |
| + |
| + mContentViewCoreHolder.addView(contentContentView, new FrameLayout.LayoutParams( |
| + FrameLayout.LayoutParams.MATCH_PARENT, |
| + FrameLayout.LayoutParams.MATCH_PARENT)); |
| + } |
| + |
| + private void restoreContentWindow() { |
| + ViewGroup contentContentView = mContentCVC.getContainerView(); |
| + mContentTab.updateWindowAndroid(mOldContentWindowAndroid); |
| + mContentViewCoreHolder.removeView(contentContentView); |
| + mContentCVParent.addView(contentContentView, new FrameLayout.LayoutParams( |
|
Ted C
2016/09/20 19:57:07
this is not a safe call to do. You are wiping out
mthiesse
2016/09/20 20:56:08
For now I'll cache with a TODO. I briefly looked i
|
| + FrameLayout.LayoutParams.MATCH_PARENT, |
| + FrameLayout.LayoutParams.MATCH_PARENT)); |
| } |
| private static class FrameListener implements OnFrameAvailableListener { |
| @@ -96,9 +164,21 @@ public class VrShell extends GvrLayout implements GLSurfaceView.Renderer, VrShel |
| @Override |
| public void onSurfaceCreated(GL10 gl, EGLConfig config) { |
| + final int width = mContentCVC.getContainerView().getWidth(); |
| + final int height = mContentCVC.getContainerView().getHeight(); |
| mContentTextureHandle = createExternalTextureHandle(); |
| mContentFrameListener = new FrameListener(new SurfaceTexture(mContentTextureHandle), |
| mGlSurfaceView); |
| + mContentFrameListener.mSurfaceTexture.setDefaultBufferSize(width, height); |
| + |
| + ThreadUtils.postOnUiThread(new Runnable() { |
| + @Override |
| + public void run() { |
| + nativeContentSurfaceChanged(mNativeVrShell, width, height, |
| + new Surface(mContentFrameListener.mSurfaceTexture)); |
| + mContentCVC.onPhysicalBackingSizeChanged(width, height); |
| + } |
| + }); |
| nativeGvrInit(mNativeVrShell, getGvrApi().getNativeGvrContext()); |
| nativeInitializeGl(mNativeVrShell, mContentTextureHandle); |
| @@ -152,6 +232,7 @@ public class VrShell extends GvrLayout implements GLSurfaceView.Renderer, VrShel |
| if (mContentFrameListener != null && mContentFrameListener.mSurfaceTexture != null) { |
| mContentFrameListener.mSurfaceTexture.release(); |
| } |
| + restoreContentWindow(); |
| } |
| @Override |
| @@ -201,7 +282,8 @@ public class VrShell extends GvrLayout implements GLSurfaceView.Renderer, VrShel |
| } |
| } |
| - private native long nativeInit(); |
| + private native long nativeInit(WebContents contentWebContents, |
| + long nativeContentWindowAndroid); |
| private native void nativeGvrInit(long nativeVrShell, long nativeGvrApi); |
| @@ -215,4 +297,11 @@ public class VrShell extends GvrLayout implements GLSurfaceView.Renderer, VrShel |
| private native void nativeOnPause(long nativeVrShell); |
| private native void nativeOnResume(long nativeVrShell); |
| + |
| + private native void nativeContentSurfaceDestroyed(long nativeVrShell); |
| + |
| + private native void nativeContentSurfaceChanged( |
| + long nativeVrShell, int width, int height, Surface surface); |
| + |
| + private native void nativeUpdateCompositorLayers(long nativeVrShell); |
| } |