Chromium Code Reviews| OLD | NEW |
|---|---|
| (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; | |
|
Lambros
2015/08/20 19:07:27
You could move makeFloatBuffer to a separate Cardb
shichengfeng
2015/08/20 20:02:59
Will do that in separate CL.
| |
| 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 int mHeightPixels; | |
|
Lambros
2015/08/20 19:07:27
What is this for? mHeightPixels and mWidthPixels a
shichengfeng
2015/08/20 20:02:59
Done.
| |
| 67 private int mWidthPixels; | |
| 68 | |
| 69 public CardboardActivityDesktop() { | |
| 70 mVertexShaderHandle = | |
| 71 ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADE R); | |
| 72 mFragmentShaderHandle = | |
| 73 ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_S HADER); | |
| 74 mProgramHandle = ShaderHelper.createAndLinkProgram(mVertexShaderHandle, | |
| 75 mFragmentShaderHandle, new String[] {"a_Position", "a_TexCoordin ate", | |
| 76 "u_CombinedMatrix", "u_Texture"}); | |
| 77 mCombinedMatrixHandle = | |
| 78 GLES20.glGetUniformLocation(mProgramHandle, "u_CombinedMatrix"); | |
| 79 mTextureUniformHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_T exture"); | |
| 80 mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position "); | |
| 81 mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgramHandle, "a _TexCoordinate"); | |
| 82 mTextureDataHandle = TextureHelper.createTextureHandle(); | |
| 83 } | |
| 84 | |
| 85 /** | |
| 86 * Draw the desktop. Make sure texture, position, and model view projection matrix | |
| 87 * are passed in before calling this method. | |
| 88 */ | |
| 89 public void draw() { | |
| 90 GLES20.glUseProgram(mProgramHandle); | |
| 91 | |
| 92 // Pass in model view project matrix. | |
| 93 GLES20.glUniformMatrix4fv(mCombinedMatrixHandle, 1, false, mCombinedMatr ix, 0); | |
| 94 | |
| 95 // Pass in texture coordinate. | |
| 96 GLES20.glVertexAttribPointer(mTextureCoordinateHandle, TEXTURE_COORDINAT E_DATA_SIZE, | |
| 97 GLES20.GL_FLOAT, false, 0, TEXTURE_COORDINATES); | |
| 98 GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle); | |
| 99 | |
| 100 GLES20.glVertexAttribPointer(mPositionHandle, POSITION_DATA_SIZE, GLES20 .GL_FLOAT, false, | |
| 101 0, mPosition); | |
| 102 GLES20.glEnableVertexAttribArray(mPositionHandle); | |
| 103 | |
| 104 // Link texture data with texture unit. | |
| 105 GLES20.glActiveTexture(GLES20.GL_TEXTURE0); | |
| 106 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle); | |
| 107 GLES20.glUniform1i(mTextureUniformHandle, 0); | |
| 108 | |
| 109 GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, VERTICES_NUMBER); | |
| 110 } | |
| 111 | |
| 112 /** | |
| 113 * Update the desktop frame data based on the new bitmap. Note here we fix the | |
| 114 * height of the desktop and vary width accordingly. | |
| 115 */ | |
| 116 public void updateFrameData(Bitmap bitmap) { | |
| 117 float newHalfDesktopWidth = bitmap.getWidth() * HALF_HEIGHT / bitmap.get Height(); | |
| 118 if (Math.abs(mHalfWidth - newHalfDesktopWidth) > 0.0001) { | |
| 119 mHalfWidth = newHalfDesktopWidth; | |
| 120 mPosition = makeFloatBuffer(new float[] { | |
| 121 // Desktop model coordinates. | |
| 122 -mHalfWidth, HALF_HEIGHT, 0.0f, | |
| 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 }); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 /** | |
| 133 * Clean up opengl data. | |
| 134 */ | |
| 135 public void cleanup() { | |
| 136 GLES20.glDeleteShader(mVertexShaderHandle); | |
| 137 GLES20.glDeleteShader(mFragmentShaderHandle); | |
| 138 GLES20.glDeleteTextures(1, new int[] {mTextureDataHandle}, 0); | |
| 139 } | |
| 140 | |
| 141 public void setTexture(Bitmap texture) { | |
|
Lambros
2015/08/20 19:07:27
Why is setTexture() a separate method from updateF
shichengfeng
2015/08/20 20:02:59
Done.
| |
| 142 TextureHelper.linkTexture(mTextureDataHandle, texture); | |
| 143 } | |
| 144 | |
| 145 public void setCombinedMatrix(float[] combinedMatrix) { | |
| 146 mCombinedMatrix = combinedMatrix.clone(); | |
| 147 } | |
| 148 | |
| 149 /** | |
| 150 * Return true if image frame data are already loaded in. | |
| 151 */ | |
| 152 public boolean hasImageFrame() { | |
| 153 return mPosition != null; | |
| 154 } | |
| 155 | |
| 156 public float getHalfHeight() { | |
| 157 return HALF_HEIGHT; | |
| 158 } | |
| 159 | |
| 160 public float getHalfWidth() { | |
| 161 return mHalfWidth; | |
| 162 } | |
| 163 | |
| 164 public void setHeightPixels(int heightPixels) { | |
| 165 mHeightPixels = heightPixels; | |
| 166 } | |
| 167 | |
| 168 public void setWidthPixels(int widthPixels) { | |
| 169 mWidthPixels = widthPixels; | |
| 170 } | |
| 171 | |
| 172 public int getHeightPixels() { | |
| 173 return mHeightPixels; | |
| 174 } | |
| 175 | |
| 176 public int getWidthPixels() { | |
| 177 return mWidthPixels; | |
| 178 } | |
| 179 } | |
| OLD | NEW |