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 | |
| 68 // Lock to allow multithreaded access to mHalfWidth. | |
| 69 private Object mHalfWidthLock = new Object(); | |
|
Lambros
2015/08/21 00:57:37
final
| |
| 70 | |
| 71 private Bitmap mVideoFrame; | |
| 72 | |
| 73 // Lock to allow multithreaded access to mVideoFrame. | |
| 74 private Object mVideoFrameLock = new Object(); | |
|
Lambros
2015/08/21 00:57:37
final
| |
| 75 | |
| 76 public CardboardActivityDesktop() { | |
| 77 mVertexShaderHandle = | |
| 78 ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADE R); | |
| 79 mFragmentShaderHandle = | |
| 80 ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_S HADER); | |
| 81 mProgramHandle = ShaderHelper.createAndLinkProgram(mVertexShaderHandle, | |
| 82 mFragmentShaderHandle, new String[] {"a_Position", "a_TexCoordin ate", | |
| 83 "u_CombinedMatrix", "u_Texture"}); | |
| 84 mCombinedMatrixHandle = | |
| 85 GLES20.glGetUniformLocation(mProgramHandle, "u_CombinedMatrix"); | |
| 86 mTextureUniformHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_T exture"); | |
| 87 mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position "); | |
| 88 mTextureCoordinateHandle = GLES20.glGetAttribLocation(mProgramHandle, "a _TexCoordinate"); | |
| 89 mTextureDataHandle = TextureHelper.createTextureHandle(); | |
| 90 } | |
| 91 | |
| 92 /** | |
| 93 * Draw the desktop. Make sure texture, position, and model view projection matrix | |
| 94 * are passed in before calling this method. | |
| 95 */ | |
| 96 public void draw() { | |
| 97 GLES20.glUseProgram(mProgramHandle); | |
| 98 | |
| 99 // Pass in model view project matrix. | |
| 100 GLES20.glUniformMatrix4fv(mCombinedMatrixHandle, 1, false, mCombinedMatr ix, 0); | |
| 101 | |
| 102 // Pass in texture coordinate. | |
| 103 GLES20.glVertexAttribPointer(mTextureCoordinateHandle, TEXTURE_COORDINAT E_DATA_SIZE, | |
| 104 GLES20.GL_FLOAT, false, 0, TEXTURE_COORDINATES); | |
| 105 GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle); | |
| 106 | |
| 107 GLES20.glVertexAttribPointer(mPositionHandle, POSITION_DATA_SIZE, GLES20 .GL_FLOAT, false, | |
| 108 0, mPosition); | |
| 109 GLES20.glEnableVertexAttribArray(mPositionHandle); | |
| 110 | |
| 111 // Link texture data with texture unit. | |
| 112 GLES20.glActiveTexture(GLES20.GL_TEXTURE0); | |
| 113 GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle); | |
| 114 GLES20.glUniform1i(mTextureUniformHandle, 0); | |
| 115 | |
| 116 GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, VERTICES_NUMBER); | |
| 117 } | |
| 118 | |
| 119 /** | |
| 120 * Update the desktop frame data based on the mVideoFrame. Note here we fix the | |
| 121 * height of the desktop and vary width accordingly. | |
| 122 */ | |
| 123 public void updateVideoFrame(Bitmap videoFrame) { | |
| 124 float newHalfDesktopWidth; | |
| 125 synchronized (mVideoFrameLock) { | |
| 126 mVideoFrame = videoFrame; | |
| 127 TextureHelper.linkTexture(mTextureDataHandle, videoFrame); | |
| 128 newHalfDesktopWidth = videoFrame.getWidth() * HALF_HEIGHT / videoFra me.getHeight(); | |
| 129 } | |
| 130 | |
| 131 if (Math.abs(mHalfWidth - newHalfDesktopWidth) > 0.0001) { | |
|
Lambros
2015/08/21 00:57:37
mHalfWidth needs to be inside the synchronized blo
| |
| 132 synchronized (mHalfWidthLock) { | |
| 133 mHalfWidth = newHalfDesktopWidth; | |
| 134 } | |
| 135 | |
| 136 mPosition = makeFloatBuffer(new float[] { | |
| 137 // Desktop model coordinates. | |
| 138 -mHalfWidth, HALF_HEIGHT, 0.0f, | |
| 139 -mHalfWidth, -HALF_HEIGHT, 0.0f, | |
| 140 mHalfWidth, HALF_HEIGHT, 0.0f, | |
| 141 -mHalfWidth, -HALF_HEIGHT, 0.0f, | |
| 142 mHalfWidth, -HALF_HEIGHT, 0.0f, | |
| 143 mHalfWidth, HALF_HEIGHT, 0.0f | |
| 144 }); | |
| 145 } | |
| 146 } | |
| 147 | |
| 148 /** | |
| 149 * Clean up opengl data. | |
| 150 */ | |
| 151 public void cleanup() { | |
| 152 GLES20.glDeleteShader(mVertexShaderHandle); | |
| 153 GLES20.glDeleteShader(mFragmentShaderHandle); | |
| 154 GLES20.glDeleteTextures(1, new int[] {mTextureDataHandle}, 0); | |
| 155 } | |
| 156 | |
| 157 public void setCombinedMatrix(float[] combinedMatrix) { | |
| 158 mCombinedMatrix = combinedMatrix.clone(); | |
| 159 } | |
| 160 | |
| 161 /** | |
| 162 * Return true if video frame data are already loaded in. | |
| 163 */ | |
| 164 public boolean hasVideoFrame() { | |
| 165 synchronized (mVideoFrameLock) { | |
| 166 return mVideoFrame != null; | |
| 167 } | |
| 168 } | |
| 169 | |
| 170 public float getHalfHeight() { | |
| 171 return HALF_HEIGHT; | |
| 172 } | |
| 173 | |
| 174 public float getHalfWidth() { | |
| 175 synchronized (mHalfWidthLock) { | |
| 176 return mHalfWidth; | |
| 177 } | |
| 178 } | |
| 179 | |
| 180 /** | |
| 181 * Get desktop height and width in pixels. | |
| 182 */ | |
| 183 public Point getFrameSizePixels() { | |
| 184 synchronized (mVideoFrameLock) { | |
| 185 return new Point(mVideoFrame == null ? 0 : mVideoFrame.getHeight(), | |
| 186 mVideoFrame == null ? 0 : mVideoFrame.getWidth()); | |
| 187 } | |
| 188 } | |
| 189 } | |
| OLD | NEW |