Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.chromoting.jni; | 5 package org.chromium.chromoting.jni; |
| 6 | 6 |
| 7 import android.graphics.Matrix; | |
| 8 import android.graphics.PointF; | |
| 7 import android.view.Surface; | 9 import android.view.Surface; |
| 10 import android.view.SurfaceHolder; | |
| 8 | 11 |
| 9 import org.chromium.base.annotations.CalledByNative; | 12 import org.chromium.base.annotations.CalledByNative; |
| 10 import org.chromium.base.annotations.JNINamespace; | 13 import org.chromium.base.annotations.JNINamespace; |
| 11 import org.chromium.chromoting.Desktop; | 14 import org.chromium.chromoting.Desktop; |
| 12 import org.chromium.chromoting.DesktopView; | 15 import org.chromium.chromoting.DesktopView; |
| 13 import org.chromium.chromoting.DesktopViewFactory; | 16 import org.chromium.chromoting.DesktopViewFactory; |
| 14 import org.chromium.chromoting.Event; | 17 import org.chromium.chromoting.Event; |
| 15 import org.chromium.chromoting.GlDesktopView; | 18 import org.chromium.chromoting.GlDesktopView; |
| 19 import org.chromium.chromoting.RenderCallback; | |
| 20 import org.chromium.chromoting.RenderStub; | |
| 16 import org.chromium.chromoting.SizeChangedEventParameter; | 21 import org.chromium.chromoting.SizeChangedEventParameter; |
| 17 | 22 |
| 18 /** | 23 /** |
| 19 * This class is the JNI interface class that helps bridging GlDesktopView with the OpenGL renderer | 24 * This class is the JNI interface class that helps bridging GlDesktopView with the OpenGL renderer |
| 20 * in native code. The lifetime of this class is managed by the native JniGlDisp layHandler. | 25 * in native code. The lifetime of this class is managed by the native JniGlDisp layHandler. |
| 21 * | 26 * |
| 22 * This class works entirely on the UI thread: | 27 * This class works entirely on the UI thread: |
| 23 * Functions should all be called on UI. | 28 * Functions should all be called on UI. |
| 24 * Events will only be triggered on UI. | 29 * Events will only be triggered on UI. |
| 25 */ | 30 */ |
| 26 @JNINamespace("remoting") | 31 @JNINamespace("remoting") |
| 27 public class GlDisplay { | 32 public class GlDisplay implements SurfaceHolder.Callback, RenderStub, RenderCall back { |
| 28 private volatile long mNativeJniGlDisplay; | 33 private volatile long mNativeJniGlDisplay; |
| 34 private final Event.Raisable<SizeChangedEventParameter> mOnClientSizeChanged = | |
| 35 new Event.Raisable<>(); | |
| 29 private final Event.Raisable<SizeChangedEventParameter> mOnHostSizeChanged = | 36 private final Event.Raisable<SizeChangedEventParameter> mOnHostSizeChanged = |
| 30 new Event.Raisable<>(); | 37 new Event.Raisable<>(); |
| 31 private final Event.Raisable<Void> mOnCanvasRendered = | 38 private final Event.Raisable<Void> mOnCanvasRendered = |
| 32 new Event.Raisable<>(); | 39 new Event.Raisable<>(); |
| 40 private InputFeedbackRadiusMapper mFeedbackRadiusMapper; | |
| 41 private float mScaleFactor; | |
| 33 | 42 |
| 34 private GlDisplay(long nativeJniGlDisplay) { | 43 private GlDisplay(long nativeJniGlDisplay) { |
| 35 mNativeJniGlDisplay = nativeJniGlDisplay; | 44 mNativeJniGlDisplay = nativeJniGlDisplay; |
| 45 mScaleFactor = 0; | |
|
joedow
2016/08/29 22:35:41
Why not set the default value above on line 41 ins
Yuwei
2016/08/30 22:39:12
Done.
| |
| 36 } | 46 } |
| 37 | 47 |
| 38 /** | 48 /** |
| 39 * Invalidates this object and disconnects from the native display handler. Called on the | 49 * Invalidates this object and disconnects from the native display handler. Called on the |
| 40 * display thread by the native code. | 50 * display thread by the native code. |
| 41 */ | 51 */ |
| 42 @CalledByNative | 52 @CalledByNative |
| 43 private void invalidate() { | 53 private void invalidate() { |
| 44 mNativeJniGlDisplay = 0; | 54 mNativeJniGlDisplay = 0; |
| 45 } | 55 } |
| 46 | 56 |
| 57 // SurfaceHolder.Callback overrides. | |
|
joedow
2016/08/29 22:35:41
remove, we don't document this in Java.
Yuwei
2016/08/30 22:39:12
Done.
| |
| 58 | |
| 47 /** | 59 /** |
| 48 * Notifies the OpenGL renderer that a surface for OpenGL to draw is created . | 60 * Notifies the OpenGL renderer that a surface for OpenGL to draw is created . |
| 49 * @param surface the surface to be drawn on | 61 * @param holder the surface holder that holds the surface. |
| 50 */ | 62 */ |
| 51 public void surfaceCreated(Surface surface) { | 63 @Override |
| 64 public void surfaceCreated(SurfaceHolder holder) { | |
| 52 if (mNativeJniGlDisplay != 0) { | 65 if (mNativeJniGlDisplay != 0) { |
| 53 nativeOnSurfaceCreated(mNativeJniGlDisplay, surface); | 66 nativeOnSurfaceCreated(mNativeJniGlDisplay, holder.getSurface()); |
| 54 } | 67 } |
| 55 } | 68 } |
| 56 | 69 |
| 57 /** | 70 /** |
| 58 * Notifies the OpenGL renderer the size of the surface. Should be called af ter surfaceCreated() | 71 * Notifies the OpenGL renderer the size of the surface. Should be called af ter surfaceCreated() |
| 59 * and before surfaceDestroyed(). | 72 * and before surfaceDestroyed(). |
| 60 * @param width the width of the surface | 73 * @param width the width of the surface |
| 61 * @param height the height of the surface | 74 * @param height the height of the surface |
| 62 */ | 75 */ |
| 63 public void surfaceChanged(int width, int height) { | 76 @Override |
| 77 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { | |
| 78 mOnClientSizeChanged.raise(new SizeChangedEventParameter(width, height)) ; | |
| 64 if (mNativeJniGlDisplay != 0) { | 79 if (mNativeJniGlDisplay != 0) { |
| 65 nativeOnSurfaceChanged(mNativeJniGlDisplay, width, height); | 80 nativeOnSurfaceChanged(mNativeJniGlDisplay, width, height); |
| 66 } | 81 } |
| 67 } | 82 } |
| 68 | 83 |
| 69 /** | 84 /** |
| 70 * Notifies the OpenGL renderer that the current surface being used is about to be destroyed. | 85 * Notifies the OpenGL renderer that the current surface being used is about to be destroyed. |
| 71 */ | 86 */ |
| 72 public void surfaceDestroyed() { | 87 @Override |
| 88 public void surfaceDestroyed(SurfaceHolder holder) { | |
| 73 if (mNativeJniGlDisplay != 0) { | 89 if (mNativeJniGlDisplay != 0) { |
| 74 nativeOnSurfaceDestroyed(mNativeJniGlDisplay); | 90 nativeOnSurfaceDestroyed(mNativeJniGlDisplay); |
| 75 } | 91 } |
| 76 } | 92 } |
| 77 | 93 |
| 94 // RenderStub overrides. | |
|
joedow
2016/08/29 22:35:41
remove
| |
| 95 | |
| 96 @Override | |
| 97 public void setDesktopView(DesktopView view) { | |
| 98 mFeedbackRadiusMapper = new InputFeedbackRadiusMapper(view); | |
| 99 } | |
| 100 | |
| 78 /** | 101 /** |
| 79 * Sets the transformation matrix (in pixel coordinates). | 102 * Sets the transformation matrix (in pixel coordinates). |
| 80 * @param matrix the transformation matrix | 103 * @param matrix the transformation matrix |
| 81 */ | 104 */ |
| 82 public void pixelTransformationChanged(float[] matrix) { | 105 @Override |
| 106 public void setTransformation(Matrix matrix) { | |
| 83 if (mNativeJniGlDisplay != 0) { | 107 if (mNativeJniGlDisplay != 0) { |
| 84 nativeOnPixelTransformationChanged(mNativeJniGlDisplay, matrix); | 108 float[] matrixArray = new float[9]; |
| 109 matrix.getValues(matrixArray); | |
| 110 nativeOnPixelTransformationChanged(mNativeJniGlDisplay, matrixArray) ; | |
| 111 mScaleFactor = matrix.mapRadius(1); | |
| 85 } | 112 } |
| 86 } | 113 } |
| 87 | 114 |
| 88 /** Moves the cursor to the corresponding location on the desktop. */ | 115 /** Moves the cursor to the corresponding location on the desktop. */ |
| 89 public void cursorPixelPositionChanged(float x, float y) { | 116 @Override |
| 117 public void moveCursor(PointF position) { | |
| 90 if (mNativeJniGlDisplay != 0) { | 118 if (mNativeJniGlDisplay != 0) { |
| 91 nativeOnCursorPixelPositionChanged(mNativeJniGlDisplay, x, y); | 119 nativeOnCursorPixelPositionChanged(mNativeJniGlDisplay, position.x, position.y); |
| 92 } | 120 } |
| 93 } | 121 } |
| 94 | 122 |
| 95 /** | 123 /** |
| 96 * Decides whether the cursor should be shown on the canvas. | 124 * Decides whether the cursor should be shown on the canvas. |
| 97 */ | 125 */ |
| 98 public void cursorVisibilityChanged(boolean visible) { | 126 @Override |
| 127 public void setCursorVisibility(boolean visible) { | |
| 99 if (mNativeJniGlDisplay != 0) { | 128 if (mNativeJniGlDisplay != 0) { |
| 100 nativeOnCursorVisibilityChanged(mNativeJniGlDisplay, visible); | 129 nativeOnCursorVisibilityChanged(mNativeJniGlDisplay, visible); |
| 101 } | 130 } |
| 102 } | 131 } |
| 103 | 132 |
| 104 /** | 133 /** |
| 134 * Shows the cursor input feedback animation with the given diameter at the given desktop | |
| 135 * location. | |
| 136 */ | |
| 137 @Override | |
| 138 public void showInputFeedback(InputFeedbackType feedbackToShow, PointF pos) { | |
| 139 if (mNativeJniGlDisplay == 0 || feedbackToShow.equals(InputFeedbackType. NONE)) { | |
| 140 return; | |
| 141 } | |
| 142 float diameter = mFeedbackRadiusMapper | |
| 143 .getFeedbackRadius(feedbackToShow, mScaleFactor) * 2.0f; | |
| 144 if (diameter <= 0.0f) { | |
| 145 return; | |
| 146 } | |
| 147 nativeOnCursorInputFeedback(mNativeJniGlDisplay, pos.x, pos.y, diameter) ; | |
| 148 } | |
| 149 | |
| 150 // RenderCallback overrides. | |
| 151 | |
| 152 @Override | |
| 153 public Event<SizeChangedEventParameter> onClientSizeChanged() { | |
| 154 return mOnClientSizeChanged; | |
| 155 } | |
| 156 | |
| 157 @Override | |
| 158 public Event<SizeChangedEventParameter> onHostSizeChanged() { | |
| 159 return mOnHostSizeChanged; | |
| 160 } | |
| 161 | |
| 162 @Override | |
| 163 public Event<Void> onCanvasRendered() { | |
| 164 return mOnCanvasRendered; | |
| 165 } | |
| 166 | |
| 167 /** | |
| 105 * Called by native code to notify GlDisplay that the size of the canvas (=s ize of desktop) has | 168 * Called by native code to notify GlDisplay that the size of the canvas (=s ize of desktop) has |
| 106 * changed. | 169 * changed. |
| 107 * @param width width of the canvas | 170 * @param width width of the canvas |
| 108 * @param height height of the canvas | 171 * @param height height of the canvas |
| 109 */ | 172 */ |
| 110 @CalledByNative | 173 @CalledByNative |
| 111 private void changeCanvasSize(int width, int height) { | 174 private void changeCanvasSize(int width, int height) { |
| 112 mOnHostSizeChanged.raise(new SizeChangedEventParameter(width, height)); | 175 mOnHostSizeChanged.raise(new SizeChangedEventParameter(width, height)); |
| 113 } | 176 } |
| 114 | 177 |
| 115 /** | 178 /** |
| 116 * An {@link Event} triggered when the size of the host desktop is changed. | |
| 117 */ | |
| 118 public Event<SizeChangedEventParameter> onHostSizeChanged() { | |
| 119 return mOnHostSizeChanged; | |
| 120 } | |
| 121 | |
| 122 /** | |
| 123 * Called by native code when a render request has been done by the OpenGL r enderer. This | 179 * Called by native code when a render request has been done by the OpenGL r enderer. This |
| 124 * will only be called when the render event callback is enabled. | 180 * will only be called when the render event callback is enabled. |
| 125 */ | 181 */ |
| 126 @CalledByNative | 182 @CalledByNative |
| 127 private void canvasRendered() { | 183 private void canvasRendered() { |
| 128 mOnCanvasRendered.raise(null); | 184 mOnCanvasRendered.raise(null); |
| 129 } | 185 } |
| 130 | 186 |
| 131 /** | |
| 132 * An {@link} triggered when render event callback is enabled and a render r equest has been done | |
| 133 * by the OpenGL renderer. | |
| 134 */ | |
| 135 public Event<Void> onCanvasRendered() { | |
| 136 return mOnCanvasRendered; | |
| 137 } | |
| 138 | |
| 139 /** | |
| 140 * Shows the cursor input feedback animation with the given diameter at the given desktop | |
| 141 * location. | |
| 142 */ | |
| 143 public void showCursorInputFeedback(float x, float y, float diameter) { | |
| 144 if (mNativeJniGlDisplay != 0) { | |
| 145 nativeOnCursorInputFeedback(mNativeJniGlDisplay, x, y, diameter); | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 @CalledByNative | 187 @CalledByNative |
| 150 private void initializeClient(Client client) { | 188 private void initializeClient(Client client) { |
| 151 client.setDesktopViewFactory(new DesktopViewFactory() { | 189 client.setDesktopViewFactory(new DesktopViewFactory() { |
| 152 @Override | 190 @Override |
| 153 public DesktopView createDesktopView(Desktop desktop, Client client) { | 191 public DesktopView createDesktopView(Desktop desktop, Client client) { |
| 154 return new GlDesktopView(GlDisplay.this, desktop, client); | 192 return new GlDesktopView(GlDisplay.this, desktop, client); |
| 155 } | 193 } |
| 156 }); | 194 }); |
| 157 } | 195 } |
| 158 | 196 |
| 159 @CalledByNative | 197 @CalledByNative |
| 160 private static GlDisplay createJavaDisplayObject(long nativeDisplayHandler) { | 198 private static GlDisplay createJavaDisplayObject(long nativeDisplayHandler) { |
| 161 return new GlDisplay(nativeDisplayHandler); | 199 return new GlDisplay(nativeDisplayHandler); |
| 162 } | 200 } |
| 163 | 201 |
| 164 private native void nativeOnSurfaceCreated(long nativeJniGlDisplayHandler, S urface surface); | 202 private native void nativeOnSurfaceCreated(long nativeJniGlDisplayHandler, S urface surface); |
| 165 private native void nativeOnSurfaceChanged(long nativeJniGlDisplayHandler, | 203 private native void nativeOnSurfaceChanged(long nativeJniGlDisplayHandler, |
| 166 int width, int height); | 204 int width, int height); |
| 167 private native void nativeOnSurfaceDestroyed(long nativeJniGlDisplayHandler) ; | 205 private native void nativeOnSurfaceDestroyed(long nativeJniGlDisplayHandler) ; |
| 168 private native void nativeOnPixelTransformationChanged(long nativeJniGlDispl ayHandler, | 206 private native void nativeOnPixelTransformationChanged(long nativeJniGlDispl ayHandler, |
| 169 float[] matrix); | 207 float[] matrix); |
| 170 private native void nativeOnCursorPixelPositionChanged(long nativeJniGlDispl ayHandler, | 208 private native void nativeOnCursorPixelPositionChanged(long nativeJniGlDispl ayHandler, |
| 171 float x, float y); | 209 float x, float y); |
| 172 private native void nativeOnCursorInputFeedback(long nativeJniGlDisplayHandl er, | 210 private native void nativeOnCursorInputFeedback(long nativeJniGlDisplayHandl er, |
| 173 float x, float y, float diam eter); | 211 float x, float y, float diam eter); |
| 174 private native void nativeOnCursorVisibilityChanged(long nativeJniGlDisplayH andler, | 212 private native void nativeOnCursorVisibilityChanged(long nativeJniGlDisplayH andler, |
| 175 boolean visible); | 213 boolean visible); |
| 176 } | 214 } |
| OLD | NEW |