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

Side by Side Diff: chrome/browser/android/vr_shell/vr_shell_renderer.cc

Issue 2301633002: Refactor Vr activity into ChromeTabbedActivity. (Closed)
Patch Set: UiElements are now structs Created 4 years, 3 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h" 5 #include "chrome/browser/android/vr_shell/vr_shell_renderer.h"
6 6
7 #include "chrome/browser/android/vr_shell/vr_util.h" 7 #include "chrome/browser/android/vr_shell/vr_util.h"
8 #include "ui/gl/gl_bindings.h" 8 #include "ui/gl/gl_bindings.h"
9 9
10 namespace vr_shell {
11
12 namespace { 10 namespace {
13 11
14 const float kHalfHeight = 0.5f; 12 #define RECTANGULAR_TEXTURE_BUFFER(left, right, bottom, top) \
15 const float kHalfWidth = 0.5f; 13 { left, bottom, left, top, right, bottom, left, top, right, top, right, \
16 const float kTextureQuadPosition[18] = { 14 bottom }
15
16 static constexpr float kHalfHeight = 0.5f;
17 static constexpr float kHalfWidth = 0.5f;
18 static constexpr float kTextureQuadPosition[18] = {
17 -kHalfWidth, kHalfHeight, 0.0f, -kHalfWidth, -kHalfHeight, 0.0f, 19 -kHalfWidth, kHalfHeight, 0.0f, -kHalfWidth, -kHalfHeight, 0.0f,
18 kHalfWidth, kHalfHeight, 0.0f, -kHalfWidth, -kHalfHeight, 0.0f, 20 kHalfWidth, kHalfHeight, 0.0f, -kHalfWidth, -kHalfHeight, 0.0f,
19 kHalfWidth, -kHalfHeight, 0.0f, kHalfWidth, kHalfHeight, 0.0f}; 21 kHalfWidth, -kHalfHeight, 0.0f, kHalfWidth, kHalfHeight, 0.0f};
20 const int kPositionDataSize = 3; 22 static constexpr int kPositionDataSize = 3;
21 // Number of vertices passed to glDrawArrays(). 23 // Number of vertices passed to glDrawArrays().
22 const int kVerticesNumber = 6; 24 static constexpr int kVerticesNumber = 6;
23 25
24 const float kTexturedQuadTextureCoordinates[12] = { 26 static constexpr float kTexturedQuadTextureCoordinates[12] =
25 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f}; 27 RECTANGULAR_TEXTURE_BUFFER(0.0f, 1.0f, 0.0f, 1.0f);
26 const int kTextureCoordinateDataSize = 2; 28
29 static constexpr int kTextureCoordinateDataSize = 2;
27 30
28 const float kWebVrVertices[32] = { 31 const float kWebVrVertices[32] = {
29 // x y u, v 32 // x y u, v
30 -1.f, 1.f, 0.f, 0.f, // Left Eye 33 -1.f, 1.f, 0.f, 0.f, // Left Eye
31 -1.f, -1.f, 0.f, 1.f, 34 -1.f, -1.f, 0.f, 1.f,
32 0.f, -1.f, 1.f, 1.f, 35 0.f, -1.f, 1.f, 1.f,
33 0.f, 1.f, 1.f, 0.f, 36 0.f, 1.f, 1.f, 0.f,
34 37
35 0.f, 1.f, 0.f, 0.f, // Right Eye 38 0.f, 1.f, 0.f, 0.f, // Right Eye
36 0.f, -1.f, 0.f, 1.f, 39 0.f, -1.f, 0.f, 1.f,
37 1.f, -1.f, 1.f, 1.f, 40 1.f, -1.f, 1.f, 1.f,
38 1.f, 1.f, 1.f, 0.f }; 41 1.f, 1.f, 1.f, 0.f };
39 const int kWebVrVerticesSize = sizeof(float) * 32; 42 const int kWebVrVerticesSize = sizeof(float) * 32;
40 43
41 #define SHADER(Src) #Src 44 #define SHADER(Src) #Src
42 #define OEIE_SHADER(Src) "#extension GL_OES_EGL_image_external : require\n" #Src 45 #define OEIE_SHADER(Src) "#extension GL_OES_EGL_image_external : require\n" #Src
43 #define VOID_OFFSET(x) reinterpret_cast<void*>(x) 46 #define VOID_OFFSET(x) reinterpret_cast<void*>(x)
44 47
45 const char* GetShaderSource(ShaderID shader) { 48 const char* GetShaderSource(vr_shell::ShaderID shader) {
46 switch (shader) { 49 switch (shader) {
47 case TEXTURE_QUAD_VERTEX_SHADER: 50 case vr_shell::ShaderID::TEXTURE_QUAD_VERTEX_SHADER:
48 return SHADER(uniform mat4 u_CombinedMatrix; attribute vec4 a_Position; 51 return SHADER(uniform mat4 u_CombinedMatrix;
52 attribute vec4 a_Position;
49 attribute vec2 a_TexCoordinate; 53 attribute vec2 a_TexCoordinate;
50 varying vec2 v_TexCoordinate; void main() { 54 varying vec2 v_TexCoordinate;
55 void main() {
51 v_TexCoordinate = a_TexCoordinate; 56 v_TexCoordinate = a_TexCoordinate;
52 gl_Position = u_CombinedMatrix * a_Position; 57 gl_Position = u_CombinedMatrix * a_Position;
53 }); 58 });
54 case TEXTURE_QUAD_FRAGMENT_SHADER: 59 case vr_shell::ShaderID::TEXTURE_QUAD_FRAGMENT_SHADER:
55 return OEIE_SHADER( 60 return OEIE_SHADER(
56 precision highp float; uniform samplerExternalOES u_Texture; 61 precision highp float;
57 varying vec2 v_TexCoordinate; void main() { 62 uniform samplerExternalOES u_Texture;
58 vec4 texture = texture2D(u_Texture, v_TexCoordinate); 63 uniform vec4 u_CopyRect; // rectangle
59 gl_FragColor = vec4(texture.r, texture.g, texture.b, 1.0); 64 varying vec2 v_TexCoordinate;
65 void main() {
66 vec2 scaledTex = vec2(
67 u_CopyRect[0] + v_TexCoordinate.x * u_CopyRect[2],
68 u_CopyRect[1] + v_TexCoordinate.y * u_CopyRect[3]);
69 gl_FragColor = texture2D(u_Texture, scaledTex);
60 }); 70 });
61 case vr_shell::ShaderID::WEBVR_VERTEX_SHADER: 71 case vr_shell::ShaderID::WEBVR_VERTEX_SHADER:
62 return SHADER( 72 return SHADER(
63 attribute vec2 a_Position; 73 attribute vec2 a_Position;
64 attribute vec2 a_TexCoordinate; 74 attribute vec2 a_TexCoordinate;
65 uniform vec4 u_SrcRect; 75 uniform vec4 u_SrcRect;
66 varying vec2 v_TexCoordinate; 76 varying vec2 v_TexCoordinate;
67 77
68 void main() { 78 void main() {
69 v_TexCoordinate = u_SrcRect.xy + (a_TexCoordinate * u_SrcRect.zw); 79 v_TexCoordinate = u_SrcRect.xy + (a_TexCoordinate * u_SrcRect.zw);
70 gl_Position = vec4(a_Position, 0.0, 1.0); 80 gl_Position = vec4(a_Position, 0.0, 1.0);
71 }); 81 });
72 case vr_shell::ShaderID::WEBVR_FRAGMENT_SHADER: 82 case vr_shell::ShaderID::WEBVR_FRAGMENT_SHADER:
73 return OEIE_SHADER( 83 return OEIE_SHADER(
74 precision highp float; 84 precision highp float;
75 uniform samplerExternalOES u_Texture; 85 uniform samplerExternalOES u_Texture;
76 varying vec2 v_TexCoordinate; 86 varying vec2 v_TexCoordinate;
77 87
78 void main() { 88 void main() {
79 gl_FragColor = texture2D(u_Texture, v_TexCoordinate); 89 gl_FragColor = texture2D(u_Texture, v_TexCoordinate);
80 }); 90 });
81 default: 91 default:
82 LOG(ERROR) << "Shader source requested for unknown shader"; 92 LOG(ERROR) << "Shader source requested for unknown shader";
83 return ""; 93 return "";
84 } 94 }
85 } 95 }
86 96
97 #undef RECTANGULAR_TEXTURE_BUFFER
87 } // namespace 98 } // namespace
88 99
100 namespace vr_shell {
101
89 TexturedQuadRenderer::TexturedQuadRenderer() { 102 TexturedQuadRenderer::TexturedQuadRenderer() {
90 std::string error; 103 std::string error;
91 vertex_shader_handle_ = CompileShader( 104 vertex_shader_handle_ = CompileShader(
92 GL_VERTEX_SHADER, GetShaderSource(TEXTURE_QUAD_VERTEX_SHADER), error); 105 GL_VERTEX_SHADER, GetShaderSource(TEXTURE_QUAD_VERTEX_SHADER), error);
93 CHECK(vertex_shader_handle_) << error; 106 CHECK(vertex_shader_handle_) << error;
94 107
95 fragment_shader_handle_ = CompileShader( 108 fragment_shader_handle_ = CompileShader(
96 GL_FRAGMENT_SHADER, GetShaderSource(TEXTURE_QUAD_FRAGMENT_SHADER), error); 109 GL_FRAGMENT_SHADER, GetShaderSource(TEXTURE_QUAD_FRAGMENT_SHADER), error);
97 CHECK(fragment_shader_handle_) << error; 110 CHECK(fragment_shader_handle_) << error;
98 111
99 program_handle_ = CreateAndLinkProgram( 112 program_handle_ = CreateAndLinkProgram(
100 vertex_shader_handle_, fragment_shader_handle_, 4, nullptr, error); 113 vertex_shader_handle_, fragment_shader_handle_, 5, nullptr, error);
101 CHECK(program_handle_) << error; 114 CHECK(program_handle_) << error;
102 115
103 combined_matrix_handle_ = 116 combined_matrix_handle_ =
104 glGetUniformLocation(program_handle_, "u_CombinedMatrix"); 117 glGetUniformLocation(program_handle_, "u_CombinedMatrix");
105 texture_uniform_handle_ = glGetUniformLocation(program_handle_, "u_Texture"); 118 texture_uniform_handle_ = glGetUniformLocation(program_handle_, "u_Texture");
119 copy_rect_uniform_handle_ = glGetUniformLocation(program_handle_,
120 "u_CopyRect");
106 position_handle_ = glGetAttribLocation(program_handle_, "a_Position"); 121 position_handle_ = glGetAttribLocation(program_handle_, "a_Position");
107 texture_coordinate_handle_ = 122 texture_coordinate_handle_ =
108 glGetAttribLocation(program_handle_, "a_TexCoordinate"); 123 glGetAttribLocation(program_handle_, "a_TexCoordinate");
109 } 124 }
110 125
111 void TexturedQuadRenderer::Draw(int texture_data_handle, 126 void TexturedQuadRenderer::Draw(int texture_data_handle,
112 const gvr::Mat4f& combined_matrix) { 127 const gvr::Mat4f& combined_matrix,
128 const Rectf& copy_rect) {
113 glUseProgram(program_handle_); 129 glUseProgram(program_handle_);
114 130
115 // Pass in model view project matrix. 131 // Pass in model view project matrix.
116 glUniformMatrix4fv(combined_matrix_handle_, 1, false, 132 glUniformMatrix4fv(combined_matrix_handle_, 1, false,
117 MatrixToGLArray(combined_matrix).data()); 133 MatrixToGLArray(combined_matrix).data());
118 134
119 // Pass in texture coordinate. 135 // Pass in texture coordinate.
120 glVertexAttribPointer(texture_coordinate_handle_, kTextureCoordinateDataSize, 136 glVertexAttribPointer(texture_coordinate_handle_, kTextureCoordinateDataSize,
121 GL_FLOAT, false, 0, kTexturedQuadTextureCoordinates); 137 GL_FLOAT, false, 0, kTexturedQuadTextureCoordinates);
122 glEnableVertexAttribArray(texture_coordinate_handle_); 138 glEnableVertexAttribArray(texture_coordinate_handle_);
123 139
124 glVertexAttribPointer(position_handle_, kPositionDataSize, GL_FLOAT, false, 0, 140 glVertexAttribPointer(position_handle_, kPositionDataSize, GL_FLOAT, false, 0,
125 kTextureQuadPosition); 141 kTextureQuadPosition);
126 glEnableVertexAttribArray(position_handle_); 142 glEnableVertexAttribArray(position_handle_);
127 143
144 glEnable(GL_BLEND);
145 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
146
128 // Link texture data with texture unit. 147 // Link texture data with texture unit.
129 glActiveTexture(GL_TEXTURE0); 148 glActiveTexture(GL_TEXTURE0);
130 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_data_handle); 149 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture_data_handle);
131 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 150 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
132 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 151 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
133 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 152 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
134 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 153 glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
154
135 glUniform1i(texture_uniform_handle_, 0); 155 glUniform1i(texture_uniform_handle_, 0);
156 glUniform4fv(copy_rect_uniform_handle_, 1, (float*)(&copy_rect));
136 157
137 glDrawArrays(GL_TRIANGLES, 0, kVerticesNumber); 158 glDrawArrays(GL_TRIANGLES, 0, kVerticesNumber);
138 159
139 glDisableVertexAttribArray(position_handle_); 160 glDisableVertexAttribArray(position_handle_);
140 glDisableVertexAttribArray(texture_coordinate_handle_); 161 glDisableVertexAttribArray(texture_coordinate_handle_);
141 } 162 }
142 163
143 TexturedQuadRenderer::~TexturedQuadRenderer() { 164 TexturedQuadRenderer::~TexturedQuadRenderer() = default;
144 glDeleteShader(vertex_shader_handle_);
145 glDeleteShader(fragment_shader_handle_);
146 }
147 165
148 WebVrRenderer::WebVrRenderer() { 166 WebVrRenderer::WebVrRenderer() {
149 left_bounds_ = { 0.0f, 0.0f, 0.5f, 1.0f }; 167 left_bounds_ = { 0.0f, 0.0f, 0.5f, 1.0f };
150 right_bounds_ = { 0.5f, 0.0f, 0.5f, 1.0f }; 168 right_bounds_ = { 0.5f, 0.0f, 0.5f, 1.0f };
151 169
152 std::string error; 170 std::string error;
153 GLuint vertex_shader_handle = CompileShader( 171 GLuint vertex_shader_handle = CompileShader(
154 GL_VERTEX_SHADER, GetShaderSource(WEBVR_VERTEX_SHADER), error); 172 GL_VERTEX_SHADER, GetShaderSource(WEBVR_VERTEX_SHADER), error);
155 CHECK(vertex_shader_handle) << error; 173 CHECK(vertex_shader_handle) << error;
156 174
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 } 241 }
224 242
225 void WebVrRenderer::UpdateTextureBounds(int eye, const gvr::Rectf& bounds) { 243 void WebVrRenderer::UpdateTextureBounds(int eye, const gvr::Rectf& bounds) {
226 if (eye == 0) { 244 if (eye == 0) {
227 left_bounds_ = bounds; 245 left_bounds_ = bounds;
228 } else if (eye == 1) { 246 } else if (eye == 1) {
229 right_bounds_ = bounds; 247 right_bounds_ = bounds;
230 } 248 }
231 } 249 }
232 250
233 WebVrRenderer::~WebVrRenderer() { 251 // Note that we don't explicitly delete gl objects here, they're deleted
234 glDeleteBuffersARB(1, &vertex_buffer_); 252 // automatically when we call ClearGLBindings, and deleting them here leads to
235 glDeleteProgram(program_handle_); 253 // segfaults.
236 } 254 WebVrRenderer::~WebVrRenderer() = default;
237 255
238 VrShellRenderer::VrShellRenderer() 256 VrShellRenderer::VrShellRenderer()
239 : textured_quad_renderer_(new TexturedQuadRenderer), 257 : textured_quad_renderer_(new TexturedQuadRenderer),
240 webvr_renderer_(new WebVrRenderer) {} 258 webvr_renderer_(new WebVrRenderer) {}
241 259
242 VrShellRenderer::~VrShellRenderer() {} 260 VrShellRenderer::~VrShellRenderer() = default;
243 261
244 } // namespace vr_shell 262 } // namespace vr_shell
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698