This source file includes following definitions.
- JNINamespace
- onVSync
- registerVSyncListener
- unregisterVSyncListener
- setVSyncListener
- requestUpdate
- setSurfaceViewBackgroundColor
- destroy
- setCurrentContentView
- onReadyToRender
- createSurfaceView
- isInitialized
- setOverlayVideoMode
- requestRender
- onSwapBuffersCompleted
- render
- nativeInit
- nativeDestroy
- nativeSetCurrentContentView
- nativeSurfaceCreated
- nativeSurfaceDestroyed
- nativeSurfaceChanged
- nativeComposite
- nativeCompositeToBitmap
- nativeSetOverlayVideoMode
package org.chromium.content.browser;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Handler;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.FrameLayout;
import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;
import org.chromium.base.ObserverList;
import org.chromium.base.ObserverList.RewindableIterator;
import org.chromium.base.TraceEvent;
import org.chromium.ui.base.WindowAndroid;
@JNINamespace("content")
public class ContentViewRenderView extends FrameLayout {
private static final int MAX_SWAP_BUFFER_COUNT = 2;
private long mNativeContentViewRenderView;
private final SurfaceHolder.Callback mSurfaceCallback;
private final SurfaceView mSurfaceView;
private final VSyncAdapter mVSyncAdapter;
private int mPendingRenders;
private int mPendingSwapBuffers;
private boolean mNeedToRender;
private ContentView mCurrentContentView;
private final Runnable mRenderRunnable = new Runnable() {
@Override
public void run() {
render();
}
};
public ContentViewRenderView(Context context, WindowAndroid rootWindow) {
super(context);
assert rootWindow != null;
mNativeContentViewRenderView = nativeInit(rootWindow.getNativePointer());
assert mNativeContentViewRenderView != 0;
mSurfaceView = createSurfaceView(getContext());
mSurfaceView.setZOrderMediaOverlay(true);
mSurfaceCallback = new SurfaceHolder.Callback() {
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
assert mNativeContentViewRenderView != 0;
nativeSurfaceChanged(mNativeContentViewRenderView,
format, width, height, holder.getSurface());
if (mCurrentContentView != null) {
mCurrentContentView.getContentViewCore().onPhysicalBackingSizeChanged(
width, height);
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
setSurfaceViewBackgroundColor(Color.WHITE);
assert mNativeContentViewRenderView != 0;
nativeSurfaceCreated(mNativeContentViewRenderView);
mPendingSwapBuffers = 0;
mPendingRenders = 0;
onReadyToRender();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
assert mNativeContentViewRenderView != 0;
nativeSurfaceDestroyed(mNativeContentViewRenderView);
}
};
mSurfaceView.getHolder().addCallback(mSurfaceCallback);
mVSyncAdapter = new VSyncAdapter(getContext());
addView(mSurfaceView,
new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT));
}
private class VSyncAdapter implements VSyncManager.Provider, VSyncMonitor.Listener {
private final VSyncMonitor mVSyncMonitor;
private boolean mVSyncNotificationEnabled;
private VSyncManager.Listener mVSyncListener;
private final ObserverList<VSyncManager.Listener> mCurrentVSyncListeners;
private final RewindableIterator<VSyncManager.Listener> mCurrentVSyncListenersIterator;
private static final long INPUT_EVENT_LAG_FROM_VSYNC_MICROSECONDS = 3200;
VSyncAdapter(Context context) {
mVSyncMonitor = new VSyncMonitor(context, this);
mCurrentVSyncListeners = new ObserverList<VSyncManager.Listener>();
mCurrentVSyncListenersIterator = mCurrentVSyncListeners.rewindableIterator();
}
@Override
public void onVSync(VSyncMonitor monitor, long vsyncTimeMicros) {
if (mNeedToRender) {
if (mPendingSwapBuffers + mPendingRenders <= MAX_SWAP_BUFFER_COUNT) {
mNeedToRender = false;
mPendingRenders++;
render();
} else {
TraceEvent.instant("ContentViewRenderView:bail");
}
}
if (mVSyncListener != null) {
if (mVSyncNotificationEnabled) {
for (mCurrentVSyncListenersIterator.rewind();
mCurrentVSyncListenersIterator.hasNext();) {
mCurrentVSyncListenersIterator.next().onVSync(vsyncTimeMicros);
}
mVSyncMonitor.requestUpdate();
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
vsyncTimeMicros += INPUT_EVENT_LAG_FROM_VSYNC_MICROSECONDS;
}
mVSyncListener.updateVSync(vsyncTimeMicros,
mVSyncMonitor.getVSyncPeriodInMicroseconds());
}
}
}
@Override
public void registerVSyncListener(VSyncManager.Listener listener) {
if (!mVSyncNotificationEnabled) mVSyncMonitor.requestUpdate();
mCurrentVSyncListeners.addObserver(listener);
mVSyncNotificationEnabled = true;
}
@Override
public void unregisterVSyncListener(VSyncManager.Listener listener) {
mCurrentVSyncListeners.removeObserver(listener);
if (mCurrentVSyncListeners.isEmpty()) {
mVSyncNotificationEnabled = false;
}
}
void setVSyncListener(VSyncManager.Listener listener) {
mVSyncListener = listener;
if (mVSyncListener != null) mVSyncMonitor.requestUpdate();
}
void requestUpdate() {
mVSyncMonitor.requestUpdate();
}
}
public void setSurfaceViewBackgroundColor(int color) {
if (mSurfaceView != null) {
mSurfaceView.setBackgroundColor(color);
}
}
public void destroy() {
mSurfaceView.getHolder().removeCallback(mSurfaceCallback);
nativeDestroy(mNativeContentViewRenderView);
mNativeContentViewRenderView = 0;
}
public void setCurrentContentView(ContentView contentView) {
assert mNativeContentViewRenderView != 0;
mCurrentContentView = contentView;
ContentViewCore contentViewCore =
contentView != null ? contentView.getContentViewCore() : null;
nativeSetCurrentContentView(mNativeContentViewRenderView,
contentViewCore != null ? contentViewCore.getNativeContentViewCore() : 0);
if (contentViewCore != null) {
contentViewCore.onPhysicalBackingSizeChanged(getWidth(), getHeight());
mVSyncAdapter.setVSyncListener(contentViewCore.getVSyncListener(mVSyncAdapter));
}
}
protected void onReadyToRender() {
}
protected SurfaceView createSurfaceView(Context context) {
return new SurfaceView(context) {
@Override
public void onDraw(Canvas canvas) {
if (canvas.isHardwareAccelerated()) return;
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(),
Bitmap.Config.ARGB_8888);
if (nativeCompositeToBitmap(mNativeContentViewRenderView, bitmap)) {
canvas.drawBitmap(bitmap, 0, 0, null);
}
}
};
}
public boolean isInitialized() {
return mSurfaceView.getHolder().getSurface() != null;
}
public void setOverlayVideoMode(boolean enabled) {
int format = enabled ? PixelFormat.TRANSLUCENT : PixelFormat.OPAQUE;
mSurfaceView.getHolder().setFormat(format);
nativeSetOverlayVideoMode(mNativeContentViewRenderView, enabled);
}
@CalledByNative
private void requestRender() {
ContentViewCore contentViewCore = mCurrentContentView != null ?
mCurrentContentView.getContentViewCore() : null;
boolean rendererHasFrame =
contentViewCore != null && contentViewCore.consumePendingRendererFrame();
if (rendererHasFrame && mPendingSwapBuffers + mPendingRenders < MAX_SWAP_BUFFER_COUNT) {
TraceEvent.instant("requestRender:now");
mNeedToRender = false;
mPendingRenders++;
Handler handler = getHandler();
if (handler != null) {
handler.postAtFrontOfQueue(mRenderRunnable);
} else {
post(mRenderRunnable);
}
mVSyncAdapter.requestUpdate();
} else if (mPendingRenders <= 0) {
assert mPendingRenders == 0;
TraceEvent.instant("requestRender:later");
mNeedToRender = true;
mVSyncAdapter.requestUpdate();
}
}
@CalledByNative
private void onSwapBuffersCompleted() {
TraceEvent.instant("onSwapBuffersCompleted");
if (mPendingSwapBuffers == MAX_SWAP_BUFFER_COUNT && mNeedToRender) requestRender();
if (mPendingSwapBuffers > 0) mPendingSwapBuffers--;
}
private void render() {
if (mPendingRenders > 0) mPendingRenders--;
if (mCurrentContentView == null) return;
ContentViewCore contentViewCore = mCurrentContentView.getContentViewCore();
if (contentViewCore == null || !contentViewCore.isReady()) {
return;
}
boolean didDraw = nativeComposite(mNativeContentViewRenderView);
if (didDraw) {
mPendingSwapBuffers++;
if (mSurfaceView.getBackground() != null) {
post(new Runnable() {
@Override
public void run() {
mSurfaceView.setBackgroundResource(0);
}
});
}
}
}
private native long nativeInit(long rootWindowNativePointer);
private native void nativeDestroy(long nativeContentViewRenderView);
private native void nativeSetCurrentContentView(long nativeContentViewRenderView,
long nativeContentView);
private native void nativeSurfaceCreated(long nativeContentViewRenderView);
private native void nativeSurfaceDestroyed(long nativeContentViewRenderView);
private native void nativeSurfaceChanged(long nativeContentViewRenderView,
int format, int width, int height, Surface surface);
private native boolean nativeComposite(long nativeContentViewRenderView);
private native boolean nativeCompositeToBitmap(long nativeContentViewRenderView, Bitmap bitmap);
private native void nativeSetOverlayVideoMode(long nativeContentViewRenderView,
boolean enabled);
}