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.Client; | 16 import org.chromium.chromoting.jni.JniInterface; |
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 | |
57 private FloatBuffer mPositionCoordinates; | 55 private FloatBuffer mPositionCoordinates; |
58 | 56 |
59 private int mVertexShaderHandle; | 57 private int mVertexShaderHandle; |
60 private int mFragmentShaderHandle; | 58 private int mFragmentShaderHandle; |
61 private int mProgramHandle; | 59 private int mProgramHandle; |
62 private int mCombinedMatrixHandle; | 60 private int mCombinedMatrixHandle; |
63 private int mTextureUniformHandle; | 61 private int mTextureUniformHandle; |
64 private int mPositionHandle; | 62 private int mPositionHandle; |
65 private int mTextureDataHandle; | 63 private int mTextureDataHandle; |
66 private int mTextureCoordinateHandle; | 64 private int mTextureCoordinateHandle; |
67 | 65 |
68 // Flag to indicate whether to reload the desktop texture. | 66 // Flag to indicate whether to reload the desktop texture. |
69 private boolean mReloadTexture; | 67 private boolean mReloadTexture; |
70 | 68 |
71 // Lock to allow multithreaded access to mReloadTexture. | 69 // Lock to allow multithreaded access to mReloadTexture. |
72 private final Object mReloadTextureLock = new Object(); | 70 private final Object mReloadTextureLock = new Object(); |
73 | 71 |
74 private Bitmap mCursorBitmap; | 72 private Bitmap mCursorBitmap; |
75 | 73 |
76 // Half width and half height of the cursor. | 74 // Half width and half height of the cursor. |
77 private PointF mHalfFrameSize; | 75 private PointF mHalfFrameSize; |
78 | 76 |
79 private PointF mCursorPosition; | 77 private PointF mCursorPosition; |
80 | 78 |
81 public Cursor(Client client) { | 79 public Cursor() { |
82 mClient = client; | |
83 mHalfFrameSize = new PointF(0.0f, 0.0f); | 80 mHalfFrameSize = new PointF(0.0f, 0.0f); |
84 mCursorPosition = new PointF(0.0f, 0.0f); | 81 mCursorPosition = new PointF(0.0f, 0.0f); |
85 | 82 |
86 mVertexShaderHandle = | 83 mVertexShaderHandle = |
87 ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADE
R); | 84 ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADE
R); |
88 mFragmentShaderHandle = | 85 mFragmentShaderHandle = |
89 ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_S
HADER); | 86 ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_S
HADER); |
90 mProgramHandle = ShaderHelper.createAndLinkProgram(mVertexShaderHandle, | 87 mProgramHandle = ShaderHelper.createAndLinkProgram(mVertexShaderHandle, |
91 mFragmentShaderHandle, new String[] {"a_Position", "a_TexCoordin
ate", | 88 mFragmentShaderHandle, new String[] {"a_Position", "a_TexCoordin
ate", |
92 "u_CombinedMatrix", "u_Texture"}); | 89 "u_CombinedMatrix", "u_Texture"}); |
(...skipping 23 matching lines...) Expand all Loading... |
116 private boolean moveCursor(PointF position) { | 113 private boolean moveCursor(PointF position) { |
117 return Math.abs(mCursorPosition.x - position.x) > CURSOR_MOVE_THRESHOLD | 114 return Math.abs(mCursorPosition.x - position.x) > CURSOR_MOVE_THRESHOLD |
118 || Math.abs(mCursorPosition.y - position.y) > CURSOR_MOVE_THRESH
OLD; | 115 || Math.abs(mCursorPosition.y - position.y) > CURSOR_MOVE_THRESH
OLD; |
119 } | 116 } |
120 | 117 |
121 /** | 118 /** |
122 * Send the mouse move event to host when {@link moveCursor} returns true. | 119 * Send the mouse move event to host when {@link moveCursor} returns true. |
123 */ | 120 */ |
124 public void moveTo(PointF position) { | 121 public void moveTo(PointF position) { |
125 if (moveCursor(position)) { | 122 if (moveCursor(position)) { |
126 mClient.sendMouseEvent((int) position.x, (int) position.y, | 123 JniInterface.sendMouseEvent((int) position.x, (int) position.y, |
127 TouchInputHandler.BUTTON_UNDEFINED, false); | 124 TouchInputHandler.BUTTON_UNDEFINED, false); |
128 } | 125 } |
129 mCursorPosition = position; | 126 mCursorPosition = position; |
130 } | 127 } |
131 | 128 |
132 /** | 129 /** |
133 * Link the texture data for cursor if {@link mReloadTexture} is true. | 130 * Link the texture data for cursor if {@link mReloadTexture} is true. |
134 * Invoked from {@link com.google.vrtoolkit.cardboard.CardboardView.StereoRe
nderer.onNewFrame} | 131 * Invoked from {@link com.google.vrtoolkit.cardboard.CardboardView.StereoRe
nderer.onNewFrame} |
135 */ | 132 */ |
136 public void maybeLoadTexture(Desktop desktop) { | 133 public void maybeLoadTexture(Desktop desktop) { |
137 synchronized (mReloadTextureLock) { | 134 synchronized (mReloadTextureLock) { |
138 if (!mReloadTexture || !desktop.hasVideoFrame()) { | 135 if (!mReloadTexture || !desktop.hasVideoFrame()) { |
139 return; | 136 return; |
140 } | 137 } |
141 } | 138 } |
142 | 139 |
143 Bitmap cursorBitmap = mClient.getCursorBitmap(); | 140 Bitmap cursorBitmap = JniInterface.getCursorBitmap(); |
144 | 141 |
145 if (cursorBitmap == mCursorBitmap) { | 142 if (cursorBitmap == mCursorBitmap) { |
146 // Case when cursor image has not changed. | 143 // Case when cursor image has not changed. |
147 synchronized (mReloadTextureLock) { | 144 synchronized (mReloadTextureLock) { |
148 mReloadTexture = false; | 145 mReloadTexture = false; |
149 } | 146 } |
150 return; | 147 return; |
151 } | 148 } |
152 | 149 |
153 mCursorBitmap = cursorBitmap; | 150 mCursorBitmap = cursorBitmap; |
154 updatePosition(desktop, mCursorBitmap, mClient.getCursorHotspot()); | 151 updatePosition(desktop, mCursorBitmap, JniInterface.getCursorHotspot()); |
155 | 152 |
156 TextureHelper.linkTexture(mTextureDataHandle, cursorBitmap); | 153 TextureHelper.linkTexture(mTextureDataHandle, cursorBitmap); |
157 | 154 |
158 synchronized (mReloadTextureLock) { | 155 synchronized (mReloadTextureLock) { |
159 mReloadTexture = false; | 156 mReloadTexture = false; |
160 } | 157 } |
161 } | 158 } |
162 | 159 |
163 public boolean hasImageFrame() { | 160 public boolean hasImageFrame() { |
164 return mCursorBitmap != null; | 161 return mCursorBitmap != null; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 | 233 |
237 /* | 234 /* |
238 * Clean cursor related opengl data. | 235 * Clean cursor related opengl data. |
239 */ | 236 */ |
240 public void cleanup() { | 237 public void cleanup() { |
241 GLES20.glDeleteShader(mVertexShaderHandle); | 238 GLES20.glDeleteShader(mVertexShaderHandle); |
242 GLES20.glDeleteShader(mFragmentShaderHandle); | 239 GLES20.glDeleteShader(mFragmentShaderHandle); |
243 GLES20.glDeleteTextures(1, new int[] {mTextureDataHandle}, 0); | 240 GLES20.glDeleteTextures(1, new int[] {mTextureDataHandle}, 0); |
244 } | 241 } |
245 } | 242 } |
OLD | NEW |