| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
| 6 | 6 |
| 7 import android.content.Context; | 7 import android.content.Context; |
| 8 import android.os.Build; | 8 import android.os.Build; |
| 9 import android.view.Surface; | 9 import android.view.Surface; |
| 10 import android.view.SurfaceView; | 10 import android.view.SurfaceView; |
| 11 import android.view.SurfaceHolder; | 11 import android.view.SurfaceHolder; |
| 12 import android.widget.FrameLayout; | 12 import android.widget.FrameLayout; |
| 13 | 13 |
| 14 import org.chromium.base.JNINamespace; | 14 import org.chromium.base.JNINamespace; |
| 15 | 15 |
| 16 /*** | 16 /*** |
| 17 * This view is used by a ContentView to render its content. | 17 * This view is used by a ContentView to render its content. |
| 18 * Call {@link #setCurrentContentView(ContentView)} with the contentView that sh
ould be displayed. | 18 * Call {@link #setCurrentContentView(ContentView)} with the contentView that sh
ould be displayed. |
| 19 * Note that only one ContentView can be shown at a time. | 19 * Note that only one ContentView can be shown at a time. |
| 20 */ | 20 */ |
| 21 @JNINamespace("content") | 21 @JNINamespace("content") |
| 22 public class ContentViewRenderView extends FrameLayout { | 22 public class ContentViewRenderView extends FrameLayout |
| 23 implements ContentViewCore.VSyncProvider { |
| 23 | 24 |
| 24 // The native side of this object. | 25 // The native side of this object. |
| 25 private int mNativeContentViewRenderView = 0; | 26 private int mNativeContentViewRenderView = 0; |
| 26 | 27 |
| 27 private SurfaceView mSurfaceView; | 28 private SurfaceView mSurfaceView; |
| 28 | 29 |
| 29 private ContentView mCurrentContentView; | 30 private ContentView mCurrentContentView; |
| 30 | 31 |
| 31 private final VSyncMonitor mVSyncMonitor; | 32 private final VSyncMonitor mVSyncMonitor; |
| 33 private boolean mVSyncNotificationEnabled; |
| 32 | 34 |
| 33 // The VSyncMonitor gives the timebase for the actual vsync, but we don't wa
nt render until | 35 // The VSyncMonitor gives the timebase for the actual vsync, but we don't wa
nt render until |
| 34 // we have had a chance for input events to propagate to the compositor thre
ad. This takes | 36 // we have had a chance for input events to propagate to the compositor thre
ad. This takes |
| 35 // 3 ms typically, so we adjust the vsync timestamps forward by a bit to giv
e input events a | 37 // 3 ms typically, so we adjust the vsync timestamps forward by a bit to giv
e input events a |
| 36 // chance to arrive. | 38 // chance to arrive. |
| 37 private static final long INPUT_EVENT_LAG_FROM_VSYNC_MICROSECONDS = 3200; | 39 private static final long INPUT_EVENT_LAG_FROM_VSYNC_MICROSECONDS = 3200; |
| 38 | 40 |
| 39 /** | 41 /** |
| 40 * Constructs a new ContentViewRenderView that should be can to a view hiera
rchy. | 42 * Constructs a new ContentViewRenderView that should be can to a view hiera
rchy. |
| 41 * Native code should add/remove the layers to be rendered through the Conte
ntViewLayerRenderer. | 43 * Native code should add/remove the layers to be rendered through the Conte
ntViewLayerRenderer. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 67 @Override | 69 @Override |
| 68 public void surfaceDestroyed(SurfaceHolder holder) { | 70 public void surfaceDestroyed(SurfaceHolder holder) { |
| 69 nativeSurfaceDestroyed(mNativeContentViewRenderView); | 71 nativeSurfaceDestroyed(mNativeContentViewRenderView); |
| 70 } | 72 } |
| 71 }); | 73 }); |
| 72 | 74 |
| 73 mVSyncMonitor = new VSyncMonitor(getContext(), new VSyncMonitor.Listener
() { | 75 mVSyncMonitor = new VSyncMonitor(getContext(), new VSyncMonitor.Listener
() { |
| 74 @Override | 76 @Override |
| 75 public void onVSync(VSyncMonitor monitor, long vsyncTimeMicros) { | 77 public void onVSync(VSyncMonitor monitor, long vsyncTimeMicros) { |
| 76 if (mCurrentContentView == null) return; | 78 if (mCurrentContentView == null) return; |
| 77 // Compensate for input event lag. Input events are delivered im
mediately on | 79 if (mVSyncNotificationEnabled) { |
| 78 // pre-JB releases, so this adjustment is only done for later ve
rsions. | 80 mCurrentContentView.getContentViewCore().sendVSync(vsyncTime
Micros); |
| 79 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { | 81 mVSyncMonitor.requestUpdate(); |
| 80 vsyncTimeMicros += INPUT_EVENT_LAG_FROM_VSYNC_MICROSECONDS; | 82 } else { |
| 83 // Compensate for input event lag. Input events are delivere
d immediately on |
| 84 // pre-JB releases, so this adjustment is only done for late
r versions. |
| 85 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{ |
| 86 vsyncTimeMicros += INPUT_EVENT_LAG_FROM_VSYNC_MICROSECON
DS; |
| 87 } |
| 88 mCurrentContentView.getContentViewCore().updateVSync(vsyncTi
meMicros, |
| 89 mVSyncMonitor.getVSyncPeriodInMicroseconds()); |
| 81 } | 90 } |
| 82 mCurrentContentView.getContentViewCore().updateVSync(vsyncTimeMi
cros, | |
| 83 mVSyncMonitor.getVSyncPeriodInMicroseconds()); | |
| 84 } | 91 } |
| 85 }); | 92 }); |
| 86 | 93 |
| 87 addView(mSurfaceView, | 94 addView(mSurfaceView, |
| 88 new FrameLayout.LayoutParams( | 95 new FrameLayout.LayoutParams( |
| 89 FrameLayout.LayoutParams.MATCH_PARENT, | 96 FrameLayout.LayoutParams.MATCH_PARENT, |
| 90 FrameLayout.LayoutParams.MATCH_PARENT)); | 97 FrameLayout.LayoutParams.MATCH_PARENT)); |
| 91 } | 98 } |
| 92 | 99 |
| 93 /** | 100 /** |
| 94 * Should be called when the ContentViewRenderView is not needed anymore so
its associated | 101 * Should be called when the ContentViewRenderView is not needed anymore so
its associated |
| 95 * native resource can be freed. | 102 * native resource can be freed. |
| 96 */ | 103 */ |
| 97 public void destroy() { | 104 public void destroy() { |
| 98 nativeDestroy(mNativeContentViewRenderView); | 105 nativeDestroy(mNativeContentViewRenderView); |
| 99 } | 106 } |
| 100 | 107 |
| 101 /** | 108 /** |
| 102 * Makes the passed ContentView the one displayed by this ContentViewRenderV
iew. | 109 * Makes the passed ContentView the one displayed by this ContentViewRenderV
iew. |
| 103 */ | 110 */ |
| 104 public void setCurrentContentView(ContentView contentView) { | 111 public void setCurrentContentView(ContentView contentView) { |
| 105 nativeSetCurrentContentView(mNativeContentViewRenderView, | 112 nativeSetCurrentContentView(mNativeContentViewRenderView, |
| 106 contentView.getContentViewCore().getNativeContentViewCore()); | 113 contentView.getContentViewCore().getNativeContentViewCore()); |
| 107 | 114 |
| 108 mCurrentContentView = contentView; | 115 mCurrentContentView = contentView; |
| 109 mCurrentContentView.getContentViewCore().onPhysicalBackingSizeChanged( | 116 mCurrentContentView.getContentViewCore().onPhysicalBackingSizeChanged( |
| 110 getWidth(), getHeight()); | 117 getWidth(), getHeight()); |
| 118 mCurrentContentView.getContentViewCore().setVSyncProvider(this); |
| 111 mVSyncMonitor.requestUpdate(); | 119 mVSyncMonitor.requestUpdate(); |
| 112 } | 120 } |
| 113 | 121 |
| 114 /** | 122 /** |
| 115 * This method should be subclassed to provide actions to be performed once
the view is ready to | 123 * This method should be subclassed to provide actions to be performed once
the view is ready to |
| 116 * render. | 124 * render. |
| 117 */ | 125 */ |
| 118 protected void onReadyToRender() { | 126 protected void onReadyToRender() { |
| 119 } | 127 } |
| 120 | 128 |
| 121 /** | 129 /** |
| 122 * @return whether the surface view is initialized and ready to render. | 130 * @return whether the surface view is initialized and ready to render. |
| 123 */ | 131 */ |
| 124 public boolean isInitialized() { | 132 public boolean isInitialized() { |
| 125 return mSurfaceView.getHolder().getSurface() != null; | 133 return mSurfaceView.getHolder().getSurface() != null; |
| 126 } | 134 } |
| 127 | 135 |
| 136 @Override |
| 137 public void setVSyncNotificationEnabled(ContentViewCore contentViewCore, boo
lean enabled) { |
| 138 if (enabled && !mVSyncNotificationEnabled) mVSyncMonitor.requestUpdate()
; |
| 139 mVSyncNotificationEnabled = enabled; |
| 140 } |
| 141 |
| 128 private static native int nativeInit(); | 142 private static native int nativeInit(); |
| 129 private native void nativeDestroy(int nativeContentViewRenderView); | 143 private native void nativeDestroy(int nativeContentViewRenderView); |
| 130 private native void nativeSetCurrentContentView(int nativeContentViewRenderV
iew, | 144 private native void nativeSetCurrentContentView(int nativeContentViewRenderV
iew, |
| 131 int nativeContentView); | 145 int nativeContentView); |
| 132 private native void nativeSurfaceCreated(int nativeContentViewRenderView, Su
rface surface); | 146 private native void nativeSurfaceCreated(int nativeContentViewRenderView, Su
rface surface); |
| 133 private native void nativeSurfaceDestroyed(int nativeContentViewRenderView); | 147 private native void nativeSurfaceDestroyed(int nativeContentViewRenderView); |
| 134 private native void nativeSurfaceSetSize(int nativeContentViewRenderView, | 148 private native void nativeSurfaceSetSize(int nativeContentViewRenderView, |
| 135 int width, int height); | 149 int width, int height); |
| 136 } | 150 } |
| OLD | NEW |