OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.blimp; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.graphics.Point; |
| 9 import android.os.Build; |
| 10 import android.util.AttributeSet; |
| 11 import android.view.Surface; |
| 12 import android.view.SurfaceHolder; |
| 13 import android.view.SurfaceView; |
| 14 import android.view.WindowManager; |
| 15 |
| 16 import org.chromium.base.annotations.JNINamespace; |
| 17 |
| 18 /** |
| 19 * A {@link View} that will visually represent the Blimp rendered content. This
{@link View} starts |
| 20 * a native compositor. |
| 21 */ |
| 22 @JNINamespace("blimp") |
| 23 public class BlimpView extends SurfaceView implements SurfaceHolder.Callback2 { |
| 24 private long mNativeBlimpViewPtr; |
| 25 |
| 26 /** |
| 27 * Builds a new {@link BlimpView}. |
| 28 * @param context A {@link Context} instance. |
| 29 * @param attrs An {@link AttributeSet} instance. |
| 30 */ |
| 31 public BlimpView(Context context, AttributeSet attrs) { |
| 32 super(context, attrs); |
| 33 } |
| 34 |
| 35 @Override |
| 36 protected void onFinishInflate() { |
| 37 super.onFinishInflate(); |
| 38 |
| 39 setZOrderMediaOverlay(true); |
| 40 setVisibility(GONE); |
| 41 } |
| 42 |
| 43 /** |
| 44 * Starts up rendering for this {@link View}. This will start up the native
compositor and will |
| 45 * display it's contents. |
| 46 */ |
| 47 public void initializeRenderer() { |
| 48 assert mNativeBlimpViewPtr == 0; |
| 49 |
| 50 WindowManager windowManager = |
| 51 (WindowManager) getContext().getSystemService(Context.WINDOW_SER
VICE); |
| 52 Point displaySize = new Point(); |
| 53 windowManager.getDefaultDisplay().getSize(displaySize); |
| 54 Point physicalSize = new Point(); |
| 55 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { |
| 56 windowManager.getDefaultDisplay().getRealSize(physicalSize); |
| 57 } |
| 58 // TODO(dtrainor): Change 1.f to dpToPx once native fully supports dp. |
| 59 float compositorDensity = 1.f; |
| 60 mNativeBlimpViewPtr = nativeInit( |
| 61 physicalSize.x, physicalSize.y, displaySize.x, displaySize.y, co
mpositorDensity); |
| 62 getHolder().addCallback(this); |
| 63 setVisibility(VISIBLE); |
| 64 } |
| 65 |
| 66 /** |
| 67 * Stops rendering for this {@link View} and destroys all internal state. T
his {@link View} |
| 68 * should not be used after this. |
| 69 */ |
| 70 public void destroyRenderer() { |
| 71 getHolder().removeCallback(this); |
| 72 if (mNativeBlimpViewPtr != 0) { |
| 73 nativeDestroy(mNativeBlimpViewPtr); |
| 74 mNativeBlimpViewPtr = 0; |
| 75 } |
| 76 } |
| 77 |
| 78 /** |
| 79 * Triggers a redraw of the native compositor, pushing a new frame. |
| 80 */ |
| 81 public void setNeedsComposite() { |
| 82 if (mNativeBlimpViewPtr == 0) return; |
| 83 nativeSetNeedsComposite(mNativeBlimpViewPtr); |
| 84 } |
| 85 |
| 86 /** |
| 87 * Toggles whether or not the native compositor draws to this {@link View} o
r not. |
| 88 * @param visible Whether or not the compositor should draw or not. |
| 89 */ |
| 90 public void setCompositorVisibility(boolean visible) { |
| 91 if (mNativeBlimpViewPtr == 0) return; |
| 92 nativeSetVisibility(mNativeBlimpViewPtr, visible); |
| 93 } |
| 94 |
| 95 // SurfaceHolder.Callback2 Implementation ----------------------------------
-------------------- |
| 96 @Override |
| 97 public void surfaceChanged(SurfaceHolder holder, int format, int width, int
height) { |
| 98 if (mNativeBlimpViewPtr == 0) return; |
| 99 nativeSurfaceChanged(mNativeBlimpViewPtr, format, width, height, holder.
getSurface()); |
| 100 } |
| 101 |
| 102 @Override |
| 103 public void surfaceCreated(SurfaceHolder holder) { |
| 104 if (mNativeBlimpViewPtr == 0) return; |
| 105 nativeSurfaceCreated(mNativeBlimpViewPtr); |
| 106 } |
| 107 |
| 108 @Override |
| 109 public void surfaceDestroyed(SurfaceHolder holder) { |
| 110 if (mNativeBlimpViewPtr == 0) return; |
| 111 nativeSurfaceDestroyed(mNativeBlimpViewPtr); |
| 112 } |
| 113 |
| 114 @Override |
| 115 public void surfaceRedrawNeeded(SurfaceHolder holder) {} |
| 116 |
| 117 // Native Methods ----------------------------------------------------------
-------------------- |
| 118 private native long nativeInit(int physicalWidth, int physicalHeight, int di
splayWidth, |
| 119 int displayHeight, float dpToPixel); |
| 120 private native void nativeDestroy(long nativeBlimpView); |
| 121 private native void nativeSetNeedsComposite(long nativeBlimpView); |
| 122 private native void nativeSurfaceChanged( |
| 123 long nativeBlimpView, int format, int width, int height, Surface sur
face); |
| 124 private native void nativeSurfaceCreated(long nativeBlimpView); |
| 125 private native void nativeSurfaceDestroyed(long nativeBlimpView); |
| 126 private native void nativeSetVisibility(long nativeBlimpView, boolean visibl
e); |
| 127 } |
OLD | NEW |