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