OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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.cardboard; | 5 package org.chromium.chromoting.cardboard; |
6 | 6 |
7 import static org.chromium.chromoting.cardboard.CardboardUtil.makeFloatBuffer; | 7 import static org.chromium.chromoting.cardboard.CardboardUtil.makeFloatBuffer; |
8 import static org.chromium.chromoting.cardboard.CardboardUtil.makeRectangularTex
tureBuffer; | 8 import static org.chromium.chromoting.cardboard.CardboardUtil.makeRectangularTex
tureBuffer; |
9 | 9 |
10 import android.graphics.Bitmap; | 10 import android.graphics.Bitmap; |
11 import android.graphics.Point; | 11 import android.graphics.Point; |
12 import android.graphics.PointF; | 12 import android.graphics.PointF; |
13 import android.opengl.GLES20; | 13 import android.opengl.GLES20; |
14 | 14 |
15 import org.chromium.chromoting.TouchInputHandler; | 15 import org.chromium.chromoting.TouchInputHandler; |
16 import org.chromium.chromoting.jni.JniInterface; | 16 import org.chromium.chromoting.jni.Client; |
17 | 17 |
18 import java.nio.FloatBuffer; | 18 import java.nio.FloatBuffer; |
19 | 19 |
20 /** | 20 /** |
21 * Cardboard activity desktop cursor that is used to render the image of the mou
se | 21 * Cardboard activity desktop cursor that is used to render the image of the mou
se |
22 * cursor onto the desktop. | 22 * cursor onto the desktop. |
23 */ | 23 */ |
24 public class Cursor { | 24 public class Cursor { |
25 private static final String VERTEX_SHADER = | 25 private static final String VERTEX_SHADER = |
26 "uniform mat4 u_CombinedMatrix;" | 26 "uniform mat4 u_CombinedMatrix;" |
(...skipping 18 matching lines...) Expand all Loading... |
45 | 45 |
46 private static final int POSITION_COORDINATE_DATA_SIZE = 3; | 46 private static final int POSITION_COORDINATE_DATA_SIZE = 3; |
47 private static final int TEXTURE_COORDINATE_DATA_SIZE = 2; | 47 private static final int TEXTURE_COORDINATE_DATA_SIZE = 2; |
48 | 48 |
49 // Number of vertices passed to glDrawArrays(). | 49 // Number of vertices passed to glDrawArrays(). |
50 private static final int VERTICES_NUMBER = 6; | 50 private static final int VERTICES_NUMBER = 6; |
51 | 51 |
52 // Threshold to determine whether to send the mouse move event. | 52 // Threshold to determine whether to send the mouse move event. |
53 private static final float CURSOR_MOVE_THRESHOLD = 1.0f; | 53 private static final float CURSOR_MOVE_THRESHOLD = 1.0f; |
54 | 54 |
| 55 private final Client mClient; |
| 56 |
55 private FloatBuffer mPositionCoordinates; | 57 private FloatBuffer mPositionCoordinates; |
56 | 58 |
57 private int mVertexShaderHandle; | 59 private int mVertexShaderHandle; |
58 private int mFragmentShaderHandle; | 60 private int mFragmentShaderHandle; |
59 private int mProgramHandle; | 61 private int mProgramHandle; |
60 private int mCombinedMatrixHandle; | 62 private int mCombinedMatrixHandle; |
61 private int mTextureUniformHandle; | 63 private int mTextureUniformHandle; |
62 private int mPositionHandle; | 64 private int mPositionHandle; |
63 private int mTextureDataHandle; | 65 private int mTextureDataHandle; |
64 private int mTextureCoordinateHandle; | 66 private int mTextureCoordinateHandle; |
65 | 67 |
66 // Flag to indicate whether to reload the desktop texture. | 68 // Flag to indicate whether to reload the desktop texture. |
67 private boolean mReloadTexture; | 69 private boolean mReloadTexture; |
68 | 70 |
69 // Lock to allow multithreaded access to mReloadTexture. | 71 // Lock to allow multithreaded access to mReloadTexture. |
70 private final Object mReloadTextureLock = new Object(); | 72 private final Object mReloadTextureLock = new Object(); |
71 | 73 |
72 private Bitmap mCursorBitmap; | 74 private Bitmap mCursorBitmap; |
73 | 75 |
74 // Half width and half height of the cursor. | 76 // Half width and half height of the cursor. |
75 private PointF mHalfFrameSize; | 77 private PointF mHalfFrameSize; |
76 | 78 |
77 private PointF mCursorPosition; | 79 private PointF mCursorPosition; |
78 | 80 |
79 public Cursor() { | 81 public Cursor(Client client) { |
| 82 mClient = client; |
80 mHalfFrameSize = new PointF(0.0f, 0.0f); | 83 mHalfFrameSize = new PointF(0.0f, 0.0f); |
81 mCursorPosition = new PointF(0.0f, 0.0f); | 84 mCursorPosition = new PointF(0.0f, 0.0f); |
82 | 85 |
83 mVertexShaderHandle = | 86 mVertexShaderHandle = |
84 ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADE
R); | 87 ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADE
R); |
85 mFragmentShaderHandle = | 88 mFragmentShaderHandle = |
86 ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_S
HADER); | 89 ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_S
HADER); |
87 mProgramHandle = ShaderHelper.createAndLinkProgram(mVertexShaderHandle, | 90 mProgramHandle = ShaderHelper.createAndLinkProgram(mVertexShaderHandle, |
88 mFragmentShaderHandle, new String[] {"a_Position", "a_TexCoordin
ate", | 91 mFragmentShaderHandle, new String[] {"a_Position", "a_TexCoordin
ate", |
89 "u_CombinedMatrix", "u_Texture"}); | 92 "u_CombinedMatrix", "u_Texture"}); |
(...skipping 23 matching lines...) Expand all Loading... |
113 private boolean moveCursor(PointF position) { | 116 private boolean moveCursor(PointF position) { |
114 return Math.abs(mCursorPosition.x - position.x) > CURSOR_MOVE_THRESHOLD | 117 return Math.abs(mCursorPosition.x - position.x) > CURSOR_MOVE_THRESHOLD |
115 || Math.abs(mCursorPosition.y - position.y) > CURSOR_MOVE_THRESH
OLD; | 118 || Math.abs(mCursorPosition.y - position.y) > CURSOR_MOVE_THRESH
OLD; |
116 } | 119 } |
117 | 120 |
118 /** | 121 /** |
119 * Send the mouse move event to host when {@link moveCursor} returns true. | 122 * Send the mouse move event to host when {@link moveCursor} returns true. |
120 */ | 123 */ |
121 public void moveTo(PointF position) { | 124 public void moveTo(PointF position) { |
122 if (moveCursor(position)) { | 125 if (moveCursor(position)) { |
123 JniInterface.sendMouseEvent((int) position.x, (int) position.y, | 126 mClient.sendMouseEvent((int) position.x, (int) position.y, |
124 TouchInputHandler.BUTTON_UNDEFINED, false); | 127 TouchInputHandler.BUTTON_UNDEFINED, false); |
125 } | 128 } |
126 mCursorPosition = position; | 129 mCursorPosition = position; |
127 } | 130 } |
128 | 131 |
129 /** | 132 /** |
130 * Link the texture data for cursor if {@link mReloadTexture} is true. | 133 * Link the texture data for cursor if {@link mReloadTexture} is true. |
131 * Invoked from {@link com.google.vrtoolkit.cardboard.CardboardView.StereoRe
nderer.onNewFrame} | 134 * Invoked from {@link com.google.vrtoolkit.cardboard.CardboardView.StereoRe
nderer.onNewFrame} |
132 */ | 135 */ |
133 public void maybeLoadTexture(Desktop desktop) { | 136 public void maybeLoadTexture(Desktop desktop) { |
134 synchronized (mReloadTextureLock) { | 137 synchronized (mReloadTextureLock) { |
135 if (!mReloadTexture || !desktop.hasVideoFrame()) { | 138 if (!mReloadTexture || !desktop.hasVideoFrame()) { |
136 return; | 139 return; |
137 } | 140 } |
138 } | 141 } |
139 | 142 |
140 Bitmap cursorBitmap = JniInterface.getCursorBitmap(); | 143 Bitmap cursorBitmap = mClient.getCursorBitmap(); |
141 | 144 |
142 if (cursorBitmap == mCursorBitmap) { | 145 if (cursorBitmap == mCursorBitmap) { |
143 // Case when cursor image has not changed. | 146 // Case when cursor image has not changed. |
144 synchronized (mReloadTextureLock) { | 147 synchronized (mReloadTextureLock) { |
145 mReloadTexture = false; | 148 mReloadTexture = false; |
146 } | 149 } |
147 return; | 150 return; |
148 } | 151 } |
149 | 152 |
150 mCursorBitmap = cursorBitmap; | 153 mCursorBitmap = cursorBitmap; |
151 updatePosition(desktop, mCursorBitmap, JniInterface.getCursorHotspot()); | 154 updatePosition(desktop, mCursorBitmap, mClient.getCursorHotspot()); |
152 | 155 |
153 TextureHelper.linkTexture(mTextureDataHandle, cursorBitmap); | 156 TextureHelper.linkTexture(mTextureDataHandle, cursorBitmap); |
154 | 157 |
155 synchronized (mReloadTextureLock) { | 158 synchronized (mReloadTextureLock) { |
156 mReloadTexture = false; | 159 mReloadTexture = false; |
157 } | 160 } |
158 } | 161 } |
159 | 162 |
160 public boolean hasImageFrame() { | 163 public boolean hasImageFrame() { |
161 return mCursorBitmap != null; | 164 return mCursorBitmap != null; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 | 236 |
234 /* | 237 /* |
235 * Clean cursor related opengl data. | 238 * Clean cursor related opengl data. |
236 */ | 239 */ |
237 public void cleanup() { | 240 public void cleanup() { |
238 GLES20.glDeleteShader(mVertexShaderHandle); | 241 GLES20.glDeleteShader(mVertexShaderHandle); |
239 GLES20.glDeleteShader(mFragmentShaderHandle); | 242 GLES20.glDeleteShader(mFragmentShaderHandle); |
240 GLES20.glDeleteTextures(1, new int[] {mTextureDataHandle}, 0); | 243 GLES20.glDeleteTextures(1, new int[] {mTextureDataHandle}, 0); |
241 } | 244 } |
242 } | 245 } |
OLD | NEW |