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

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: Refactor desktop drawing. 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 // Size of the vertexes to draw desktop.
Lambros 2015/08/19 23:17:28 English grammar: You can say "number of items in a
shichengfeng 2015/08/20 01:02:53 Done.
50 private static final int VERTEXES_NUMBER = 6;
Lambros 2015/08/19 23:17:28 s/VERTEXES/VERTICES/ Code-search has only 4 hits
shichengfeng 2015/08/20 01:02:53 Done.
51
52 private int mVertexShaderHandle;
53 private int mFragmentShaderHandle;
54 private int mProgramHandle;
55 private int mCombinedMatrixHandle;
56 private int mTextureUniformHandle;
57 private int mPositionHandle;
58 private int mTextureDataHandle;
59 private int mTextureCoordinateHandle;
60 private FloatBuffer mPosition;
61 private float[] mCombinedMatrix;
62
63 public CardboardActivityDesktop() {
64 mVertexShaderHandle =
65 ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADE R);
66 mFragmentShaderHandle =
67 ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_S HADER);
68 mProgramHandle = ShaderHelper.createAndLinkProgram(mVertexShaderHandle,
69 mFragmentShaderHandle, new String[] {"a_Position", "a_TexCoordin ate",
70 "u_CombinedMatrix", "u_Texture"});
71 mCombinedMatrixHandle =
72 GLES20.glGetUniformLocation(mProgramHandle, "u_CombinedMatrix");
73 mTextureUniformHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_T exture");
74 mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position ");
75 mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgramHandle, "a _TexCoordinate");
76 mTextureDataHandle = TextureHelper.createTextureHandle();
77 }
78
79 public void setTexture(Bitmap texture) {
80 TextureHelper.linkTexture(mTextureDataHandle, texture);
81 }
82
83 public void setPosition(FloatBuffer position) {
Lambros 2015/08/19 23:17:28 You won't need this if you move the FloatBuffer in
shichengfeng 2015/08/20 01:02:53 Done.
84 mPosition = position;
85 }
86
87 public void setCombinedMatrix(float[] combinedMatrix) {
88 mCombinedMatrix = combinedMatrix.clone();
89 }
90
91 /**
92 * Draw the desktop. Make sure texture, position, and model view projection matrix
93 * are passed in before calling this method.
94 */
95 public void draw() {
96 GLES20.glUseProgram(mProgramHandle);
97
98 // Pass in model view project matrix.
99 GLES20.glUniformMatrix4fv(mCombinedMatrixHandle, 1, false, mCombinedMatr ix, 0);
100
101 // Pass in texture coordinate.
102 GLES20.glVertexAttribPointer(mTextureCoordinateHandle, TEXTURE_COORDINAT E_DATA_SIZE,
103 GLES20.GL_FLOAT, false, 0, TEXTURE_COORDINATES);
104 GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);
105
106 GLES20.glVertexAttribPointer(mPositionHandle, POSITION_DATA_SIZE, GLES20 .GL_FLOAT, false,
107 0, mPosition);
108 GLES20.glEnableVertexAttribArray(mPositionHandle);
109
110 // Link texture data with texture unit.
111 GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
112 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle);
113 GLES20.glUniform1i(mTextureUniformHandle, 0);
114
115 GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, VERTEXES_NUMBER);
116 }
117
118 /**
119 * Clean up opengl data.
120 */
121 public void cleanup() {
122 GLES20.glDeleteShader(mVertexShaderHandle);
123 GLES20.glDeleteShader(mFragmentShaderHandle);
124 GLES20.glDeleteTextures(1, new int[] {mTextureDataHandle}, 0);
125 }
126 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698