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; | |
| 8 | |
| 9 import android.graphics.Bitmap; | |
| 10 import android.graphics.Point; | |
| 11 import android.opengl.GLES20; | |
| 12 | |
| 13 import java.nio.FloatBuffer; | |
| 14 | |
| 15 /** | |
| 16 * Chromoting Cardboard activity desktop, which is used to display host desktop. | |
| 17 */ | |
| 18 public class CardboardActivityDesktop { | |
| 19 private static final String VERTEX_SHADER = | |
| 20 "uniform mat4 u_CombinedMatrix;" | |
| 21 + "attribute vec4 a_Position;" | |
| 22 + "attribute vec2 a_TexCoordinate;" | |
| 23 + "varying vec2 v_TexCoordinate;" | |
| 24 + "void main() {" | |
| 25 + " v_TexCoordinate = a_TexCoordinate;" | |
| 26 + " gl_Position = u_CombinedMatrix * a_Position;" | |
| 27 + "}"; | |
| 28 | |
| 29 private static final String FRAGMENT_SHADER = | |
| 30 "precision mediump float;" | |
| 31 + "uniform sampler2D u_Texture;" | |
| 32 + "varying vec2 v_TexCoordinate;" | |
| 33 + "void main() {" | |
| 34 + " gl_FragColor = texture2D(u_Texture, v_TexCoordinate);" | |
| 35 + "}"; | |
| 36 | |
| 37 private static final FloatBuffer TEXTURE_COORDINATES = makeFloatBuffer(new f loat[] { | |
| 38 // Texture coordinate data. | |
| 39 0.0f, 0.0f, | |
| 40 0.0f, 1.0f, | |
| 41 1.0f, 0.0f, | |
| 42 0.0f, 1.0f, | |
| 43 1.0f, 1.0f, | |
| 44 1.0f, 0.0f | |
| 45 }); | |
| 46 | |
| 47 private static final int POSITION_DATA_SIZE = 3; | |
| 48 private static final int TEXTURE_COORDINATE_DATA_SIZE = 2; | |
| 49 | |
| 50 // Fix the desktop height and adjust width accordingly. | |
| 51 private static final float HALF_HEIGHT = 1.0f; | |
| 52 | |
| 53 // Number of vertices passed to glDrawArrays(). | |
| 54 private static final int VERTICES_NUMBER = 6; | |
| 55 | |
| 56 private int mVertexShaderHandle; | |
| 57 private int mFragmentShaderHandle; | |
| 58 private int mProgramHandle; | |
| 59 private int mCombinedMatrixHandle; | |
| 60 private int mTextureUniformHandle; | |
| 61 private int mPositionHandle; | |
| 62 private int mTextureDataHandle; | |
| 63 private int mTextureCoordinateHandle; | |
| 64 private FloatBuffer mPosition; | |
| 65 private float[] mCombinedMatrix; | |
| 66 private float mHalfWidth; | |
| 67 private Bitmap mVideoFrame; | |
| 68 | |
| 69 // Lock to allow multithreaded access to mVideoFrame. | |
| 70 private Object mVideoFrameLock = new Object(); | |
| 71 | |
| 72 public CardboardActivityDesktop() { | |
| 73 mVertexShaderHandle = | |
| 74 ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADE R); | |
| 75 mFragmentShaderHandle = | |
| 76 ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_S HADER); | |
| 77 mProgramHandle = ShaderHelper.createAndLinkProgram(mVertexShaderHandle, | |
| 78 mFragmentShaderHandle, new String[] {"a_Position", "a_TexCoordin ate", | |
| 79 "u_CombinedMatrix", "u_Texture"}); | |
| 80 mCombinedMatrixHandle = | |
| 81 GLES20.glGetUniformLocation(mProgramHandle, "u_CombinedMatrix"); | |
| 82 mTextureUniformHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_T exture"); | |
| 83 mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position "); | |
| 84 mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgramHandle, "a _TexCoordinate"); | |
| 85 mTextureDataHandle = TextureHelper.createTextureHandle(); | |
| 86 } | |
| 87 | |
| 88 /** | |
| 89 * Draw the desktop. Make sure texture, position, and model view projection matrix | |
| 90 * are passed in before calling this method. | |
| 91 */ | |
| 92 public void draw() { | |
| 93 GLES20.glUseProgram(mProgramHandle); | |
| 94 | |
| 95 // Pass in model view project matrix. | |
| 96 GLES20.glUniformMatrix4fv(mCombinedMatrixHandle, 1, false, mCombinedMatr ix, 0); | |
| 97 | |
| 98 // Pass in texture coordinate. | |
| 99 GLES20.glVertexAttribPointer(mTextureCoordinateHandle, TEXTURE_COORDINAT E_DATA_SIZE, | |
| 100 GLES20.GL_FLOAT, false, 0, TEXTURE_COORDINATES); | |
| 101 GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle); | |
| 102 | |
| 103 GLES20.glVertexAttribPointer(mPositionHandle, POSITION_DATA_SIZE, GLES20 .GL_FLOAT, false, | |
| 104 0, mPosition); | |
| 105 GLES20.glEnableVertexAttribArray(mPositionHandle); | |
| 106 | |
| 107 // Link texture data with texture unit. | |
| 108 GLES20.glActiveTexture(GLES20.GL_TEXTURE0); | |
| 109 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle); | |
| 110 GLES20.glUniform1i(mTextureUniformHandle, 0); | |
| 111 | |
| 112 GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, VERTICES_NUMBER); | |
| 113 } | |
| 114 | |
| 115 /** | |
| 116 * Update the desktop frame data based on the mVideoFrame. Note here we fix the | |
| 117 * height of the desktop and vary width accordingly. | |
| 118 */ | |
| 119 public void updateVideoFrameData(Bitmap videoFrame) { | |
|
Lambros
2015/08/21 00:08:39
You chose hasVideoFrame() instead of hasVideoFrame
shichengfeng
2015/08/21 00:42:53
Done.
| |
| 120 float newHalfDesktopWidth; | |
| 121 synchronized (mVideoFrameLock) { | |
| 122 mVideoFrame = videoFrame; | |
| 123 TextureHelper.linkTexture(mTextureDataHandle, videoFrame); | |
| 124 newHalfDesktopWidth = videoFrame.getWidth() * HALF_HEIGHT / videoFra me.getHeight(); | |
| 125 } | |
| 126 | |
| 127 if (Math.abs(mHalfWidth - newHalfDesktopWidth) > 0.0001) { | |
| 128 mHalfWidth = newHalfDesktopWidth; | |
| 129 mPosition = makeFloatBuffer(new float[] { | |
| 130 // Desktop model coordinates. | |
| 131 -mHalfWidth, HALF_HEIGHT, 0.0f, | |
| 132 -mHalfWidth, -HALF_HEIGHT, 0.0f, | |
| 133 mHalfWidth, HALF_HEIGHT, 0.0f, | |
| 134 -mHalfWidth, -HALF_HEIGHT, 0.0f, | |
| 135 mHalfWidth, -HALF_HEIGHT, 0.0f, | |
| 136 mHalfWidth, HALF_HEIGHT, 0.0f | |
| 137 }); | |
| 138 } | |
| 139 } | |
| 140 | |
| 141 /** | |
| 142 * Clean up opengl data. | |
| 143 */ | |
| 144 public void cleanup() { | |
| 145 GLES20.glDeleteShader(mVertexShaderHandle); | |
| 146 GLES20.glDeleteShader(mFragmentShaderHandle); | |
| 147 GLES20.glDeleteTextures(1, new int[] {mTextureDataHandle}, 0); | |
| 148 } | |
| 149 | |
| 150 public void setCombinedMatrix(float[] combinedMatrix) { | |
| 151 mCombinedMatrix = combinedMatrix.clone(); | |
| 152 } | |
| 153 | |
| 154 /** | |
| 155 * Return true if video frame data are already loaded in. | |
| 156 */ | |
| 157 public boolean hasVideoFrame() { | |
| 158 synchronized (mVideoFrameLock) { | |
| 159 return mVideoFrame != null; | |
| 160 } | |
| 161 } | |
| 162 | |
| 163 public float getHalfHeight() { | |
| 164 return HALF_HEIGHT; | |
| 165 } | |
| 166 | |
| 167 public float getHalfWidth() { | |
| 168 return mHalfWidth; | |
|
Lambros
2015/08/21 00:08:39
Is this called on the UI thread? If so, you need t
shichengfeng
2015/08/21 00:42:53
Done.
| |
| 169 } | |
| 170 | |
| 171 /** | |
| 172 * Get desktop height and width pixels. | |
|
Lambros
2015/08/21 00:08:39
s/pixels/in pixels/
shichengfeng
2015/08/21 00:42:53
Done.
| |
| 173 */ | |
| 174 public Point getShapePixels() { | |
|
Lambros
2015/08/21 00:08:39
"shape" is unclear, I think.
Maybe getFrameSizePix
shichengfeng
2015/08/21 00:42:53
Done.
| |
| 175 synchronized (mVideoFrameLock) { | |
| 176 return new Point(mVideoFrame == null ? 0 : mVideoFrame.getHeight(), | |
| 177 mVideoFrame == null ? 0 : mVideoFrame.getWidth()); | |
| 178 } | |
| 179 } | |
| 180 } | |
| OLD | NEW |