Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(223)

Side by Side Diff: remoting/android/java/src/org/chromium/chromoting/CardboardActivityDesktop.java

Issue 1305633002: Refactor drawing desktop. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rename imageFrame with videoFrame. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.chromoting;
6
7 import static org.chromium.chromoting.CardboardDesktopRenderer.makeFloatBuffer;
8
9 import android.graphics.Bitmap;
10 import android.opengl.GLES20;
11
12 import java.nio.FloatBuffer;
13
14 /**
15 * Chromoting Cardboard activity desktop, which is used to display host desktop.
16 */
17 public class CardboardActivityDesktop {
18 private static final String VERTEX_SHADER =
19 "uniform mat4 u_CombinedMatrix;"
20 + "attribute vec4 a_Position;"
21 + "attribute vec2 a_TexCoordinate;"
22 + "varying vec2 v_TexCoordinate;"
23 + "void main() {"
24 + " v_TexCoordinate = a_TexCoordinate;"
25 + " gl_Position = u_CombinedMatrix * a_Position;"
26 + "}";
27
28 private static final String FRAGMENT_SHADER =
29 "precision mediump float;"
30 + "uniform sampler2D u_Texture;"
31 + "varying vec2 v_TexCoordinate;"
32 + "void main() {"
33 + " gl_FragColor = texture2D(u_Texture, v_TexCoordinate);"
34 + "}";
35
36 private static final FloatBuffer TEXTURE_COORDINATES = makeFloatBuffer(new f loat[] {
37 // Texture coordinate data.
38 0.0f, 0.0f,
39 0.0f, 1.0f,
40 1.0f, 0.0f,
41 0.0f, 1.0f,
42 1.0f, 1.0f,
43 1.0f, 0.0f
44 });
45
46 private static final int POSITION_DATA_SIZE = 3;
47 private static final int TEXTURE_COORDINATE_DATA_SIZE = 2;
48
49 // Fix the desktop height and adjust width accordingly.
50 private static final float HALF_HEIGHT = 1.0f;
51
52 // Number of vertices passed to glDrawArrays().
53 private static final int VERTICES_NUMBER = 6;
54
55 private int mVertexShaderHandle;
56 private int mFragmentShaderHandle;
57 private int mProgramHandle;
58 private int mCombinedMatrixHandle;
59 private int mTextureUniformHandle;
60 private int mPositionHandle;
61 private int mTextureDataHandle;
62 private int mTextureCoordinateHandle;
63 private FloatBuffer mPosition;
64 private float[] mCombinedMatrix;
65 private float mHalfWidth;
66 private Bitmap mVideoFrame;
67
68 public CardboardActivityDesktop() {
69 mVertexShaderHandle =
70 ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADE R);
71 mFragmentShaderHandle =
72 ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_S HADER);
73 mProgramHandle = ShaderHelper.createAndLinkProgram(mVertexShaderHandle,
74 mFragmentShaderHandle, new String[] {"a_Position", "a_TexCoordin ate",
75 "u_CombinedMatrix", "u_Texture"});
76 mCombinedMatrixHandle =
77 GLES20.glGetUniformLocation(mProgramHandle, "u_CombinedMatrix");
78 mTextureUniformHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_T exture");
79 mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position ");
80 mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgramHandle, "a _TexCoordinate");
81 mTextureDataHandle = TextureHelper.createTextureHandle();
82 }
83
84 /**
85 * Draw the desktop. Make sure texture, position, and model view projection matrix
86 * are passed in before calling this method.
87 */
88 public void draw() {
89 GLES20.glUseProgram(mProgramHandle);
90
91 // Pass in model view project matrix.
92 GLES20.glUniformMatrix4fv(mCombinedMatrixHandle, 1, false, mCombinedMatr ix, 0);
93
94 // Pass in texture coordinate.
95 GLES20.glVertexAttribPointer(mTextureCoordinateHandle, TEXTURE_COORDINAT E_DATA_SIZE,
96 GLES20.GL_FLOAT, false, 0, TEXTURE_COORDINATES);
97 GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);
98
99 GLES20.glVertexAttribPointer(mPositionHandle, POSITION_DATA_SIZE, GLES20 .GL_FLOAT, false,
100 0, mPosition);
101 GLES20.glEnableVertexAttribArray(mPositionHandle);
102
103 // Link texture data with texture unit.
104 GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
105 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle);
106 GLES20.glUniform1i(mTextureUniformHandle, 0);
107
108 GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, VERTICES_NUMBER);
109 }
110
111 /**
112 * Update the desktop frame data based on the mVideoFrame. Note here we fix the
113 * height of the desktop and vary width accordingly.
114 */
115 public void updateVideoFrameData(Bitmap videoFrame) {
116 mVideoFrame = videoFrame;
117
118 float newHalfDesktopWidth = videoFrame.getWidth() * HALF_HEIGHT / videoF rame.getHeight();
119 if (Math.abs(mHalfWidth - newHalfDesktopWidth) > 0.0001) {
120 mHalfWidth = newHalfDesktopWidth;
121 mPosition = makeFloatBuffer(new float[] {
122 // Desktop model coordinates.
123 -mHalfWidth, HALF_HEIGHT, 0.0f,
124 -mHalfWidth, -HALF_HEIGHT, 0.0f,
125 mHalfWidth, HALF_HEIGHT, 0.0f,
126 -mHalfWidth, -HALF_HEIGHT, 0.0f,
127 mHalfWidth, -HALF_HEIGHT, 0.0f,
128 mHalfWidth, HALF_HEIGHT, 0.0f
129 });
130 }
131
132 TextureHelper.linkTexture(mTextureDataHandle, videoFrame);
133 }
134
135 /**
136 * Clean up opengl data.
137 */
138 public void cleanup() {
139 GLES20.glDeleteShader(mVertexShaderHandle);
140 GLES20.glDeleteShader(mFragmentShaderHandle);
141 GLES20.glDeleteTextures(1, new int[] {mTextureDataHandle}, 0);
142 }
143
144 public void setCombinedMatrix(float[] combinedMatrix) {
145 mCombinedMatrix = combinedMatrix.clone();
146 }
147
148 /**
149 * Return true if video frame data are already loaded in.
150 */
151 public boolean hasVideoFrame() {
152 return mVideoFrame != null;
153 }
154
155 public float getHalfHeight() {
156 return HALF_HEIGHT;
157 }
158
159 public float getHalfWidth() {
160 return mHalfWidth;
161 }
162
163 public int getHeightPixels() {
164 return mVideoFrame == null ? 0 : mVideoFrame.getHeight();
165 }
166
167 public int getWidthPixels() {
168 return mVideoFrame == null ? 0 : mVideoFrame.getWidth();
169 }
170 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698