Index: remoting/android/java/src/org/chromium/chromoting/CardboardActivityDesktop.java |
diff --git a/remoting/android/java/src/org/chromium/chromoting/CardboardActivityDesktop.java b/remoting/android/java/src/org/chromium/chromoting/CardboardActivityDesktop.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0960b56c09824fc73cd8de56fce9606632296090 |
--- /dev/null |
+++ b/remoting/android/java/src/org/chromium/chromoting/CardboardActivityDesktop.java |
@@ -0,0 +1,126 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.chromoting; |
+ |
+import static org.chromium.chromoting.CardboardDesktopRenderer.makeFloatBuffer; |
+ |
+import android.graphics.Bitmap; |
+import android.opengl.GLES20; |
+ |
+import java.nio.FloatBuffer; |
+ |
+/** |
+ * Chromoting Cardboard activity desktop, which is used to display host desktop. |
+ */ |
+public class CardboardActivityDesktop { |
+ private static final String VERTEX_SHADER = |
+ "uniform mat4 u_CombinedMatrix;" |
+ + "attribute vec4 a_Position;" |
+ + "attribute vec2 a_TexCoordinate;" |
+ + "varying vec2 v_TexCoordinate;" |
+ + "void main() {" |
+ + " v_TexCoordinate = a_TexCoordinate;" |
+ + " gl_Position = u_CombinedMatrix * a_Position;" |
+ + "}"; |
+ |
+ private static final String FRAGMENT_SHADER = |
+ "precision mediump float;" |
+ + "uniform sampler2D u_Texture;" |
+ + "varying vec2 v_TexCoordinate;" |
+ + "void main() {" |
+ + " gl_FragColor = texture2D(u_Texture, v_TexCoordinate);" |
+ + "}"; |
+ |
+ private static final FloatBuffer TEXTURE_COORDINATES = makeFloatBuffer(new float[] { |
+ // Texture coordinate data. |
+ 0.0f, 0.0f, |
+ 0.0f, 1.0f, |
+ 1.0f, 0.0f, |
+ 0.0f, 1.0f, |
+ 1.0f, 1.0f, |
+ 1.0f, 0.0f |
+ }); |
+ |
+ private static final int POSITION_DATA_SIZE = 3; |
+ private static final int TEXTURE_COORDINATE_DATA_SIZE = 2; |
+ |
+ // 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.
|
+ 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.
|
+ |
+ private int mVertexShaderHandle; |
+ private int mFragmentShaderHandle; |
+ private int mProgramHandle; |
+ private int mCombinedMatrixHandle; |
+ private int mTextureUniformHandle; |
+ private int mPositionHandle; |
+ private int mTextureDataHandle; |
+ private int mTextureCoordinateHandle; |
+ private FloatBuffer mPosition; |
+ private float[] mCombinedMatrix; |
+ |
+ public CardboardActivityDesktop() { |
+ mVertexShaderHandle = |
+ ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADER); |
+ mFragmentShaderHandle = |
+ ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SHADER); |
+ mProgramHandle = ShaderHelper.createAndLinkProgram(mVertexShaderHandle, |
+ mFragmentShaderHandle, new String[] {"a_Position", "a_TexCoordinate", |
+ "u_CombinedMatrix", "u_Texture"}); |
+ mCombinedMatrixHandle = |
+ GLES20.glGetUniformLocation(mProgramHandle, "u_CombinedMatrix"); |
+ mTextureUniformHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_Texture"); |
+ mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position"); |
+ mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_TexCoordinate"); |
+ mTextureDataHandle = TextureHelper.createTextureHandle(); |
+ } |
+ |
+ public void setTexture(Bitmap texture) { |
+ TextureHelper.linkTexture(mTextureDataHandle, texture); |
+ } |
+ |
+ 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.
|
+ mPosition = position; |
+ } |
+ |
+ public void setCombinedMatrix(float[] combinedMatrix) { |
+ mCombinedMatrix = combinedMatrix.clone(); |
+ } |
+ |
+ /** |
+ * Draw the desktop. Make sure texture, position, and model view projection matrix |
+ * are passed in before calling this method. |
+ */ |
+ public void draw() { |
+ GLES20.glUseProgram(mProgramHandle); |
+ |
+ // Pass in model view project matrix. |
+ GLES20.glUniformMatrix4fv(mCombinedMatrixHandle, 1, false, mCombinedMatrix, 0); |
+ |
+ // Pass in texture coordinate. |
+ GLES20.glVertexAttribPointer(mTextureCoordinateHandle, TEXTURE_COORDINATE_DATA_SIZE, |
+ GLES20.GL_FLOAT, false, 0, TEXTURE_COORDINATES); |
+ GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle); |
+ |
+ GLES20.glVertexAttribPointer(mPositionHandle, POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, |
+ 0, mPosition); |
+ GLES20.glEnableVertexAttribArray(mPositionHandle); |
+ |
+ // Link texture data with texture unit. |
+ GLES20.glActiveTexture(GLES20.GL_TEXTURE0); |
+ GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle); |
+ GLES20.glUniform1i(mTextureUniformHandle, 0); |
+ |
+ GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, VERTEXES_NUMBER); |
+ } |
+ |
+ /** |
+ * Clean up opengl data. |
+ */ |
+ public void cleanup() { |
+ GLES20.glDeleteShader(mVertexShaderHandle); |
+ GLES20.glDeleteShader(mFragmentShaderHandle); |
+ GLES20.glDeleteTextures(1, new int[] {mTextureDataHandle}, 0); |
+ } |
+} |