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

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

Issue 2729523002: Re-land^2 WebVR compositor bypass via BrowserMain context + mailbox (Closed)
Patch Set: Rebase to 11e28fd6b9380b77273db51ef0b6ccc7ea341944 Created 3 years, 9 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
(Empty)
1 // Copyright 2017 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 #include "chrome/browser/android/vr_shell/mailbox_to_surface_bridge.h"
6
7 #include <string>
8
9 #include "base/logging.h"
10 #include "base/memory/ptr_util.h"
11 #include "base/sys_info.h"
12 #include "cc/output/context_provider.h"
13 #include "content/public/browser/android/compositor.h"
14 #include "gpu/GLES2/gl2extchromium.h"
15 #include "gpu/command_buffer/client/gles2_interface.h"
16 #include "gpu/command_buffer/common/mailbox.h"
17 #include "gpu/command_buffer/common/mailbox_holder.h"
18 #include "gpu/command_buffer/common/sync_token.h"
19 #include "gpu/ipc/client/gpu_channel_host.h"
20 #include "gpu/ipc/common/gpu_surface_tracker.h"
21 #include "services/ui/public/cpp/gpu/context_provider_command_buffer.h"
22 #include "ui/gl/android/surface_texture.h"
23
24 #include <android/native_window_jni.h>
25
26 #define VOID_OFFSET(x) reinterpret_cast<void*>(x)
27 #define SHADER(Src) #Src
28
29 namespace {
30
31 const char kQuadCopyVertex[] = SHADER(
32 /* clang-format off */
33 attribute vec4 a_Position;
34 attribute vec2 a_TexCoordinate;
35 varying vec2 v_TexCoordinate;
36 void main() {
37 v_TexCoordinate = a_TexCoordinate;
38 gl_Position = a_Position;
39 }
40 ); /* clang-format on */
41
42 const char kQuadCopyFragment[] = SHADER(
43 /* clang-format off */
44 precision highp float;
45 uniform sampler2D u_Texture;
46 varying vec2 v_TexCoordinate;
47 void main() {
48 gl_FragColor = texture2D(u_Texture, v_TexCoordinate);
49 }
50 ); /* clang-format on */
51
52 const float kQuadVertices[] = {
53 // clang-format off
54 // x y u, v
55 -1.f, 1.f, 0.f, 1.f,
56 -1.f, -1.f, 0.f, 0.f,
57 1.f, -1.f, 1.f, 0.f,
58 1.f, 1.f, 1.f, 1.f};
59 static constexpr int kQuadVerticesSize = sizeof(kQuadVertices);
60
61 GLuint CompileShader(gpu::gles2::GLES2Interface* gl,
62 GLenum shader_type,
63 const GLchar* shader_source) {
64 GLuint shader_handle = gl->CreateShader(shader_type);
65 if (shader_handle != 0) {
66 // Pass in the shader source.
67 GLint len = strlen(shader_source);
68 gl->ShaderSource(shader_handle, 1, &shader_source, &len);
69 // Compile the shader.
70 gl->CompileShader(shader_handle);
71 // Get the compilation status.
72 GLint status = 0;
73 gl->GetShaderiv(shader_handle, GL_COMPILE_STATUS, &status);
74 if (status == GL_FALSE) {
75 GLint info_log_length = 0;
76 gl->GetShaderiv(shader_handle, GL_INFO_LOG_LENGTH, &info_log_length);
77 auto str_info_log = base::MakeUnique<GLchar[]>(info_log_length + 1);
78 gl->GetShaderInfoLog(shader_handle, info_log_length, nullptr,
79 str_info_log.get());
80 DLOG(ERROR) << "Error compiling shader: " << str_info_log.get();
81 gl->DeleteShader(shader_handle);
82 shader_handle = 0;
83 }
84 }
85
86 return shader_handle;
87 }
88
89 GLuint CreateAndLinkProgram(gpu::gles2::GLES2Interface* gl,
90 GLuint vertex_shader_handle,
91 GLuint fragment_shader_handle) {
92 GLuint program_handle = gl->CreateProgram();
93
94 if (program_handle != 0) {
95 // Bind the vertex shader to the program.
96 gl->AttachShader(program_handle, vertex_shader_handle);
97
98 // Bind the fragment shader to the program.
99 gl->AttachShader(program_handle, fragment_shader_handle);
100
101 // Link the two shaders together into a program.
102 gl->LinkProgram(program_handle);
103
104 // Get the link status.
105 GLint link_status = 0;
106 gl->GetProgramiv(program_handle, GL_LINK_STATUS, &link_status);
107
108 // If the link failed, delete the program.
109 if (link_status == GL_FALSE) {
110 GLint info_log_length;
111 gl->GetProgramiv(program_handle, GL_INFO_LOG_LENGTH, &info_log_length);
112
113 auto str_info_log = base::MakeUnique<GLchar[]>(info_log_length + 1);
114 gl->GetProgramInfoLog(program_handle, info_log_length, nullptr,
115 str_info_log.get());
116 DLOG(ERROR) << "Error compiling program: " << str_info_log.get();
117 gl->DeleteProgram(program_handle);
118 program_handle = 0;
119 }
120 }
121
122 return program_handle;
123 }
124
125 GLuint ConsumeTexture(gpu::gles2::GLES2Interface* gl,
126 const gpu::MailboxHolder& mailbox) {
127 TRACE_EVENT0("gpu", "MailboxToSurfaceBridge::ConsumeTexture");
128 gl->WaitSyncTokenCHROMIUM(mailbox.sync_token.GetConstData());
129
130 return gl->CreateAndConsumeTextureCHROMIUM(GL_TEXTURE_2D,
131 mailbox.mailbox.name);
132 }
133
134 } // namespace
135
136 namespace vr_shell {
137
138 MailboxToSurfaceBridge::MailboxToSurfaceBridge() : weak_ptr_factory_(this) {}
139
140 MailboxToSurfaceBridge::~MailboxToSurfaceBridge() {
141 if (surface_handle_) {
142 // Unregister from the surface tracker to avoid a resource leak.
143 gpu::GpuSurfaceTracker* tracker = gpu::GpuSurfaceTracker::Get();
144 tracker->UnregisterViewSurface(surface_handle_);
145 }
146 DestroyContext();
147 }
148
149 void MailboxToSurfaceBridge::OnContextAvailable(
150 scoped_refptr<cc::ContextProvider> provider) {
151 // Must save a reference to the ContextProvider to keep it alive,
152 // otherwise the GL context created from it becomes invalid.
153 context_provider_ = std::move(provider);
154
155 if (!context_provider_->BindToCurrentThread()) {
156 DLOG(ERROR) << "Failed to init ContextProvider";
157 return;
158 }
159
160 gl_ = context_provider_->ContextGL();
161
162 if (!gl_) {
163 DLOG(ERROR) << "Did not get a GL context";
164 return;
165 }
166 InitializeRenderer();
167 }
168
169 void MailboxToSurfaceBridge::CreateSurface(
170 gl::SurfaceTexture* surface_texture) {
171 ANativeWindow* window = surface_texture->CreateSurface();
172 gpu::GpuSurfaceTracker* tracker = gpu::GpuSurfaceTracker::Get();
173 ANativeWindow_acquire(window);
174 // Skip ANativeWindow_setBuffersGeometry, the default size appears to work.
175 surface_handle_ = tracker->AddSurfaceForNativeWidget(window);
176
177 auto surface = base::MakeUnique<gl::ScopedJavaSurface>(surface_texture);
178 tracker->RegisterViewSurface(surface_handle_, surface->j_surface().obj());
179 // Unregistering happens in the destructor.
180 ANativeWindow_release(window);
181
182 // Our attributes must be compatible with the shared offscreen
183 // surface used by virtualized contexts, otherwise mailbox
184 // synchronization doesn't work properly - it assumes a shared
185 // underlying GL context. See GetCompositorContextAttributes
186 // in content/browser/renderer_host/compositor_impl_android.cc
187 // and crbug.com/699330.
188
189 gpu::gles2::ContextCreationAttribHelper attributes;
190 attributes.alpha_size = -1;
191 attributes.red_size = 8;
192 attributes.green_size = 8;
193 attributes.blue_size = 8;
194 attributes.stencil_size = 0;
195 attributes.depth_size = 0;
196 attributes.samples = 0;
197 attributes.sample_buffers = 0;
198 attributes.bind_generates_resource = false;
199 if (base::SysInfo::IsLowEndDevice()) {
200 attributes.alpha_size = 0;
201 attributes.red_size = 5;
202 attributes.green_size = 6;
203 attributes.blue_size = 5;
204 }
205
206 content::Compositor::CreateContextProvider(
207 surface_handle_, attributes, gpu::SharedMemoryLimits::ForMailboxContext(),
208 base::Bind(&MailboxToSurfaceBridge::OnContextAvailable,
209 weak_ptr_factory_.GetWeakPtr()));
210 }
211
212 void MailboxToSurfaceBridge::ResizeSurface(int width, int height) {
213 if (!gl_) {
214 // We're not initialized yet, save the requested size for later.
215 needs_resize_ = true;
216 resize_width_ = width;
217 resize_height_ = height;
218 return;
219 }
220 DVLOG(1) << __FUNCTION__ << ": resize Surface to " <<
221 width << "x" << height;
222 gl_->ResizeCHROMIUM(width, height, 1.f, false);
223 gl_->Viewport(0, 0, width, height);
224 }
225
226 bool MailboxToSurfaceBridge::CopyMailboxToSurfaceAndSwap(
227 const gpu::MailboxHolder& mailbox) {
228 if (!gl_) {
229 // We may not have a context yet, i.e. due to surface initialization
230 // being incomplete. This is not an error, but we obviously can't draw
231 // yet.
232 return false;
233 }
234
235 if (needs_resize_) {
236 ResizeSurface(resize_width_, resize_height_);
237 needs_resize_ = false;
238 }
239
240 GLuint sourceTexture = ConsumeTexture(gl_, mailbox);
241 DrawQuad(sourceTexture);
242 gl_->SwapBuffers();
243 return true;
244 }
245
246 void MailboxToSurfaceBridge::DestroyContext() {
247 gl_ = nullptr;
248 context_provider_ = nullptr;
249 }
250
251 void MailboxToSurfaceBridge::InitializeRenderer() {
252 GLuint vertex_shader_handle =
253 CompileShader(gl_, GL_VERTEX_SHADER, kQuadCopyVertex);
254 if (!vertex_shader_handle) {
255 DestroyContext();
256 return;
257 }
258
259 GLuint fragment_shader_handle =
260 CompileShader(gl_, GL_FRAGMENT_SHADER, kQuadCopyFragment);
261 if (!fragment_shader_handle) {
262 DestroyContext();
263 return;
264 }
265
266 GLuint program_handle =
267 CreateAndLinkProgram(gl_, vertex_shader_handle, fragment_shader_handle);
268 if (!program_handle) {
269 DestroyContext();
270 return;
271 }
272
273 // Once the program is linked the shader objects are no longer needed
274 gl_->DeleteShader(vertex_shader_handle);
275 gl_->DeleteShader(fragment_shader_handle);
276
277 GLuint position_handle = gl_->GetAttribLocation(program_handle, "a_Position");
278 GLuint texCoord_handle =
279 gl_->GetAttribLocation(program_handle, "a_TexCoordinate");
280 GLuint texUniform_handle =
281 gl_->GetUniformLocation(program_handle, "u_Texture");
282
283 GLuint vertexBuffer = 0;
284 gl_->GenBuffers(1, &vertexBuffer);
285 gl_->BindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
286 gl_->BufferData(GL_ARRAY_BUFFER, kQuadVerticesSize, kQuadVertices,
287 GL_STATIC_DRAW);
288
289 // Set state once only, we assume that nobody else modifies GL state in a way
290 // that would interfere with our operations.
291 gl_->Disable(GL_CULL_FACE);
292 gl_->DepthMask(GL_FALSE);
293 gl_->Disable(GL_DEPTH_TEST);
294 gl_->Disable(GL_SCISSOR_TEST);
295 gl_->Disable(GL_BLEND);
296 gl_->Disable(GL_POLYGON_OFFSET_FILL);
297
298 // Not using gl_->Viewport, we assume that it defaults to the whole
299 // surface and gets updated by ResizeSurface externally as
300 // appropriate.
301
302 gl_->UseProgram(program_handle);
303
304 // Bind vertex attributes
305 gl_->BindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
306
307 gl_->EnableVertexAttribArray(position_handle);
308 gl_->EnableVertexAttribArray(texCoord_handle);
309
310 static constexpr size_t VERTEX_STRIDE = sizeof(float) * 4;
311 static constexpr size_t POSITION_ELEMENTS = 2;
312 static constexpr size_t TEXCOORD_ELEMENTS = 2;
313 static constexpr size_t POSITION_OFFSET = 0;
314 static constexpr size_t TEXCOORD_OFFSET = sizeof(float) * 2;
315
316 gl_->VertexAttribPointer(position_handle, POSITION_ELEMENTS, GL_FLOAT, false,
317 VERTEX_STRIDE, VOID_OFFSET(POSITION_OFFSET));
318 gl_->VertexAttribPointer(texCoord_handle, TEXCOORD_ELEMENTS, GL_FLOAT, false,
319 VERTEX_STRIDE, VOID_OFFSET(TEXCOORD_OFFSET));
320
321 gl_->ActiveTexture(GL_TEXTURE0);
322 gl_->Uniform1i(texUniform_handle, 0);
323 }
324
325 void MailboxToSurfaceBridge::DrawQuad(unsigned int texture_handle) {
326 // We're redrawing over the entire viewport, but it's generally more
327 // efficient on mobile tiling GPUs to clear anyway as a hint that
328 // we're done with the old content. TODO(klausw,crbug.com/700389):
329 // investigate using gl_->DiscardFramebufferEXT here since that's more
330 // efficient on desktop, but it would need a capability check since
331 // it's not supported on older devices such as Nexus 5X.
332 gl_->Clear(GL_COLOR_BUFFER_BIT);
333
334 // Configure texture. This is a 1:1 pixel copy since the surface
335 // size is resized to match the source canvas, so we can use
336 // GL_NEAREST.
337 gl_->BindTexture(GL_TEXTURE_2D, texture_handle);
338 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
339 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
340 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
341 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
342 gl_->DrawArrays(GL_TRIANGLE_FAN, 0, 4);
343 }
344
345 } // namespace vr_shell
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698