Chromium Code Reviews| Index: chrome/browser/android/vr_shell/mailbox_to_surface_bridge.cc |
| diff --git a/chrome/browser/android/vr_shell/mailbox_to_surface_bridge.cc b/chrome/browser/android/vr_shell/mailbox_to_surface_bridge.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f8ae07b047a9b3ee35714041079da4a0087258a2 |
| --- /dev/null |
| +++ b/chrome/browser/android/vr_shell/mailbox_to_surface_bridge.cc |
| @@ -0,0 +1,319 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/android/vr_shell/mailbox_to_surface_bridge.h" |
| + |
| +#include <string> |
| + |
| +#include "base/logging.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "cc/output/context_provider.h" |
| +#include "content/public/browser/android/compositor.h" |
| +#include "gpu/GLES2/gl2extchromium.h" |
| +#include "gpu/command_buffer/client/gles2_interface.h" |
| +#include "gpu/command_buffer/common/mailbox.h" |
| +#include "gpu/command_buffer/common/mailbox_holder.h" |
| +#include "gpu/command_buffer/common/sync_token.h" |
| +#include "gpu/ipc/client/gpu_channel_host.h" |
| +#include "gpu/ipc/common/gpu_surface_tracker.h" |
| +#include "services/ui/public/cpp/gpu/context_provider_command_buffer.h" |
| +#include "ui/gl/android/surface_texture.h" |
| + |
| +#include <android/native_window_jni.h> |
| + |
| +#define VOID_OFFSET(x) reinterpret_cast<void*>(x) |
| +#define SHADER(Src) #Src |
| + |
| +namespace { |
| + |
| +const char* kQuadCopyVertex = |
| + SHADER(attribute vec4 a_Position; attribute vec2 a_TexCoordinate; |
| + varying vec2 v_TexCoordinate; |
| + void main() { |
| + v_TexCoordinate = a_TexCoordinate; |
| + gl_Position = a_Position; |
| + }); |
| + |
| +const char* kQuadCopyFragment = SHADER( |
| + precision highp float; uniform sampler2D u_Texture; |
| + varying vec2 v_TexCoordinate; |
| + void main() { gl_FragColor = texture2D(u_Texture, v_TexCoordinate); }); |
| + |
| +static constexpr float kQuadVertices[16] = { |
| + // x y u, v |
| + -1.f, 1.f, 0.f, 1.f, -1.f, -1.f, 0.f, 0.f, |
| + 1.f, -1.f, 1.f, 0.f, 1.f, 1.f, 1.f, 1.f}; |
| +static constexpr int kQuadVerticesSize = sizeof(float) * 16; |
| + |
| +GLuint CompileShader(gpu::gles2::GLES2Interface* gl, |
| + GLenum shaderType, |
|
piman
2017/03/08 01:20:37
nit: here and other places in this file, should be
klausw
2017/03/08 02:59:22
Oops, done. This code had done a round trip throug
|
| + const GLchar* shaderSource, |
| + std::string& error) { |
| + GLuint shaderHandle = gl->CreateShader(shaderType); |
| + if (shaderHandle != 0) { |
| + // Pass in the shader source. |
| + int len = strlen(shaderSource); |
| + gl->ShaderSource(shaderHandle, 1, &shaderSource, &len); |
| + // Compile the shader. |
| + gl->CompileShader(shaderHandle); |
| + // Get the compilation status. |
| + GLint status = 0; |
| + gl->GetShaderiv(shaderHandle, GL_COMPILE_STATUS, &status); |
| + if (status == GL_FALSE) { |
| + GLint infoLogLength = 0; |
| + gl->GetShaderiv(shaderHandle, GL_INFO_LOG_LENGTH, &infoLogLength); |
| + GLchar* strInfoLog = new GLchar[infoLogLength + 1]; |
|
piman
2017/03/08 01:20:37
nit: use std::unique_ptr<GLchar[]> instead of expl
klausw
2017/03/08 02:59:23
Done.
|
| + gl->GetShaderInfoLog(shaderHandle, infoLogLength, nullptr, strInfoLog); |
| + error = "Error compiling shader: "; |
| + error += strInfoLog; |
| + delete[] strInfoLog; |
| + gl->DeleteShader(shaderHandle); |
| + shaderHandle = 0; |
| + } |
| + } |
| + |
| + return shaderHandle; |
| +} |
| + |
| +GLuint CreateAndLinkProgram(gpu::gles2::GLES2Interface* gl, |
| + GLuint vertextShaderHandle, |
| + GLuint fragmentShaderHandle, |
| + std::string& error) { |
| + GLuint programHandle = gl->CreateProgram(); |
| + |
| + if (programHandle != 0) { |
| + // Bind the vertex shader to the program. |
| + gl->AttachShader(programHandle, vertextShaderHandle); |
| + |
| + // Bind the fragment shader to the program. |
| + gl->AttachShader(programHandle, fragmentShaderHandle); |
| + |
| + // Link the two shaders together into a program. |
| + gl->LinkProgram(programHandle); |
| + |
| + // Get the link status. |
| + GLint linkStatus = 0; |
| + gl->GetProgramiv(programHandle, GL_LINK_STATUS, &linkStatus); |
| + |
| + // If the link failed, delete the program. |
| + if (linkStatus == GL_FALSE) { |
| + GLint infoLogLength; |
| + gl->GetProgramiv(programHandle, GL_INFO_LOG_LENGTH, &infoLogLength); |
| + |
| + GLchar* strInfoLog = new GLchar[infoLogLength + 1]; |
|
piman
2017/03/08 01:20:36
nit: ditto unique_ptr
klausw
2017/03/08 02:59:23
Done.
|
| + gl->GetProgramInfoLog(programHandle, infoLogLength, nullptr, strInfoLog); |
| + error = "Error compiling program: "; |
| + error += strInfoLog; |
| + delete[] strInfoLog; |
| + gl->DeleteProgram(programHandle); |
| + programHandle = 0; |
| + } |
| + } |
| + |
| + return programHandle; |
| +} |
| + |
| +} // namespace |
| + |
| +namespace vr_shell { |
| + |
| +MailboxToSurfaceBridge::MailboxToSurfaceBridge() : weak_ptr_factory_(this) {} |
| + |
| +MailboxToSurfaceBridge::~MailboxToSurfaceBridge() { |
| + if (surface_handle_) { |
| + // Unregister from the surface tracker to avoid a resource leak. |
| + gpu::GpuSurfaceTracker* tracker = gpu::GpuSurfaceTracker::Get(); |
| + tracker->UnregisterViewSurface(surface_handle_); |
| + } |
| +} |
| + |
| +void MailboxToSurfaceBridge::OnContextAvailable( |
| + scoped_refptr<cc::ContextProvider> provider) { |
| + // Must save a reference to the ContextProvider to keep it alive, |
| + // otherwise the GL context becomes invalid. |
| + context_provider_ = provider; |
| + |
| + if (!context_provider_->BindToCurrentThread()) { |
| + LOG(ERROR) << "Failed to init ContextProvider"; |
| + return; |
| + } |
| + |
| + gl_ = context_provider_->ContextGL(); |
| + |
| + VLOG(1) << __FUNCTION__ << ";;; gl=" << gl_; |
|
piman
2017/03/08 01:20:37
nit: DVLOG
|
| + |
| + if (!gl_) { |
| + LOG(ERROR) << "Did not get a GL context"; |
|
piman
2017/03/08 01:20:37
nit: DLOG ?
klausw
2017/03/08 02:59:22
Done.
|
| + return; |
| + } |
| + InitializeRenderer(); |
| +} |
| + |
| +void MailboxToSurfaceBridge::CreateSurface( |
| + scoped_refptr<gl::SurfaceTexture> surface_texture) { |
| + ANativeWindow* window = surface_texture->CreateSurface(); |
| + gpu::GpuSurfaceTracker* tracker = gpu::GpuSurfaceTracker::Get(); |
| + ANativeWindow_acquire(window); |
| + // Skip ANativeWindow_setBuffersGeometry, the default size appears to work. |
| + surface_handle_ = tracker->AddSurfaceForNativeWidget(window); |
| + |
| + auto surface = base::MakeUnique<gl::ScopedJavaSurface>(surface_texture.get()); |
| + tracker->RegisterViewSurface(surface_handle_, surface->j_surface().obj()); |
| + // Unregistering happens in the destructor. |
| + ANativeWindow_release(window); |
| + |
| + // Our attributes must be compatible with the shared offscreen |
| + // surface used by virtualized contexts, otherwise mailbox |
| + // synchronization doesn't work properly - it assumes a shared |
| + // underlying GL context. TODO(klausw): is there a more official |
| + // way to get default-compatible settings? |
| + |
| + gpu::gles2::ContextCreationAttribHelper attributes; |
| + attributes.alpha_size = -1; |
| + attributes.red_size = 8; |
| + attributes.green_size = 8; |
| + attributes.blue_size = 8; |
| + attributes.stencil_size = 0; |
| + attributes.depth_size = 0; |
| + attributes.samples = 0; |
| + attributes.sample_buffers = 0; |
| + attributes.bind_generates_resource = false; |
| + |
| + content::Compositor::CreateContextProvider( |
| + surface_handle_, attributes, gpu::SharedMemoryLimits::ForMailboxContext(), |
| + base::Bind(&MailboxToSurfaceBridge::OnContextAvailable, |
| + weak_ptr_factory_.GetWeakPtr())); |
| +} |
| + |
| +void MailboxToSurfaceBridge::ResizeSurface(int width, int height) { |
| + if (!gl_) { |
| + LOG(ERROR) << "Cannot resize, not initialized"; |
|
piman
2017/03/08 01:20:37
nit: DLOG?
klausw
2017/03/08 02:59:22
Moot, I've changed it to save the size for later s
|
| + return; |
| + } |
| + gl_->ResizeCHROMIUM(width, height, 1.f, false); |
| + gl_->Viewport(0, 0, width, height); |
| +} |
| + |
| +bool MailboxToSurfaceBridge::CopyFrameToSurface( |
| + int frame_index, |
| + const gpu::MailboxHolder& mailbox, |
| + bool discard) { |
| + TRACE_EVENT1("gpu", "MailboxToSurfaceBridge::CopyFrameToSurface", "frame", |
| + frame_index); |
| + if (!gl_) { |
| + // We may not have a context yet, i.e. due to surface initialization |
| + // being incomplete. This is not an error, but we obviously can't draw |
| + // yet. |
| + return false; |
| + } |
| + |
| + { |
| + TRACE_EVENT0("gpu", "MailboxToSurfaceBridge::WaitSyncToken"); |
| + gl_->WaitSyncTokenCHROMIUM(mailbox.sync_token.GetConstData()); |
| + } |
| + |
| + GLuint vrSourceTexture = |
| + gl_->CreateAndConsumeTextureCHROMIUM(GL_TEXTURE_2D, mailbox.mailbox.name); |
| + |
| + if (discard) { |
| + // We've consumed the texture, but the caller requested that we |
| + // don't draw it because the previous one hasn't been consumed |
| + // yet. (Swapping twice on a Surfacewithout consuming one in |
| + // between from the SurfaceTexture can lose frames.) This should |
| + // be rare, it's a waste of resources and can cause jerky |
| + // animation due to the lost frames. |
| + return false; |
| + } else { |
| + DrawQuad(vrSourceTexture); |
| + gl_->SwapBuffers(); |
| + return true; |
| + } |
| +} |
| + |
| +void MailboxToSurfaceBridge::InitializeRenderer() { |
| + std::string error; |
| + GLuint vertexShaderHandle = |
| + CompileShader(gl_, GL_VERTEX_SHADER, kQuadCopyVertex, error); |
| + CHECK(vertexShaderHandle) << error; |
|
piman
2017/03/08 01:20:37
This should either be a DCHECK, or if the error ca
klausw
2017/03/08 02:59:23
Replaced with this:
if (!vertex_shader_handle)
|
| + |
| + GLuint fragmentShaderHandle = |
| + CompileShader(gl_, GL_FRAGMENT_SHADER, kQuadCopyFragment, error); |
| + CHECK(fragmentShaderHandle) << error; |
|
piman
2017/03/08 01:20:37
nit: ditto
klausw
2017/03/08 02:59:23
Done.
|
| + |
| + GLuint programHandle = CreateAndLinkProgram(gl_, vertexShaderHandle, |
| + fragmentShaderHandle, error); |
| + CHECK(programHandle) << error; |
|
piman
2017/03/08 01:20:37
nit: ditto
klausw
2017/03/08 02:59:23
Done.
|
| + |
| + // Once the program is linked the shader objects are no longer needed |
| + gl_->DeleteShader(vertexShaderHandle); |
| + gl_->DeleteShader(fragmentShaderHandle); |
| + |
| + GLuint positionHandle = gl_->GetAttribLocation(programHandle, "a_Position"); |
| + GLuint texCoordHandle = |
| + gl_->GetAttribLocation(programHandle, "a_TexCoordinate"); |
| + GLuint texUniformHandle = gl_->GetUniformLocation(programHandle, "u_Texture"); |
| + |
| + GLuint vertexBuffer = 0; |
| + gl_->GenBuffers(1, &vertexBuffer); |
| + gl_->BindBuffer(GL_ARRAY_BUFFER, vertexBuffer); |
| + gl_->BufferData(GL_ARRAY_BUFFER, kQuadVerticesSize, kQuadVertices, |
| + GL_STATIC_DRAW); |
| + |
| + // Set state once only, we assume that nobody else modifies GL state in a way |
| + // that would interfere with our operations. |
| + gl_->Disable(GL_CULL_FACE); |
| + gl_->DepthMask(GL_FALSE); |
| + gl_->Disable(GL_DEPTH_TEST); |
| + gl_->Disable(GL_SCISSOR_TEST); |
| + gl_->Disable(GL_BLEND); |
| + gl_->Disable(GL_POLYGON_OFFSET_FILL); |
| + |
| + // Not using gl_->Viewport, we assume that it defaults to the whole |
| + // surface and gets updated by ResizeSurface externally as |
| + // appropriate. |
| + |
| + gl_->UseProgram(programHandle); |
| + |
| + // Bind vertex attributes |
| + gl_->BindBuffer(GL_ARRAY_BUFFER, vertexBuffer); |
| + |
| + gl_->EnableVertexAttribArray(positionHandle); |
| + gl_->EnableVertexAttribArray(texCoordHandle); |
| + |
| + static constexpr size_t VERTEX_STRIDE = sizeof(float) * 4; |
| + static constexpr size_t POSITION_ELEMENTS = 2; |
| + static constexpr size_t TEXCOORD_ELEMENTS = 2; |
| + static constexpr size_t POSITION_OFFSET = 0; |
| + static constexpr size_t TEXCOORD_OFFSET = sizeof(float) * 2; |
| + |
| + gl_->VertexAttribPointer(positionHandle, POSITION_ELEMENTS, GL_FLOAT, false, |
| + VERTEX_STRIDE, VOID_OFFSET(POSITION_OFFSET)); |
| + gl_->VertexAttribPointer(texCoordHandle, TEXCOORD_ELEMENTS, GL_FLOAT, false, |
| + VERTEX_STRIDE, VOID_OFFSET(TEXCOORD_OFFSET)); |
| + |
| + gl_->ActiveTexture(GL_TEXTURE0); |
| + gl_->Uniform1i(texUniformHandle, 0); |
| +} |
| + |
| +void MailboxToSurfaceBridge::DrawQuad(unsigned int textureHandle) { |
| + // No need to clear since we're redrawing on top of the entire |
| + // viewport, but let the GPU know we don't need the old content |
| + // anymore. |
| + GLenum discards[] = {GL_COLOR_EXT}; |
| + gl_->DiscardFramebufferEXT(GL_FRAMEBUFFER, arraysize(discards), discards); |
| + |
| + // Configure texture. This is a 1:1 pixel copy since the surface |
| + // size is resized to match the source canvas, so we can use |
| + // GL_NEAREST. |
| + gl_->BindTexture(GL_TEXTURE_2D, textureHandle); |
| + gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| + gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| + gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| + gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| + gl_->DrawArrays(GL_TRIANGLE_FAN, 0, 4); |
| + |
| + gl_->Flush(); |
|
piman
2017/03/08 01:20:37
nit: this may be unnecessary / redundant with the
klausw
2017/03/08 02:59:22
Removed.
|
| +} |
| + |
| +} // namespace vr_shell |