Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 = | |
|
dcheng
2017/03/08 07:23:39
const char kQuadCopyVertex[], as this is a mutable
klausw
2017/03/08 08:11:08
Done.
| |
| 32 SHADER(attribute vec4 a_Position; attribute vec2 a_TexCoordinate; | |
| 33 varying vec2 v_TexCoordinate; | |
| 34 void main() { | |
| 35 v_TexCoordinate = a_TexCoordinate; | |
| 36 gl_Position = a_Position; | |
| 37 }); | |
| 38 | |
| 39 const char* kQuadCopyFragment = SHADER( | |
| 40 precision highp float; uniform sampler2D u_Texture; | |
| 41 varying vec2 v_TexCoordinate; | |
| 42 void main() { gl_FragColor = texture2D(u_Texture, v_TexCoordinate); }); | |
| 43 | |
| 44 static constexpr float kQuadVertices[16] = { | |
| 45 // x y u, v | |
|
dcheng
2017/03/08 07:23:39
I'd suggest disabling clang-format if you want the
klausw
2017/03/08 08:11:08
Done.
| |
| 46 -1.f, 1.f, 0.f, 1.f, -1.f, -1.f, 0.f, 0.f, | |
| 47 1.f, -1.f, 1.f, 0.f, 1.f, 1.f, 1.f, 1.f}; | |
| 48 static constexpr int kQuadVerticesSize = sizeof(float) * 16; | |
|
dcheng
2017/03/08 07:23:39
Use arraysize() from base/macros.h?
klausw
2017/03/08 08:11:08
Replaced with a simple sizeof(kQuadVertices) since
dcheng
2017/03/08 23:08:05
Yikes, oops.
| |
| 49 | |
| 50 GLuint CompileShader(gpu::gles2::GLES2Interface* gl, | |
| 51 GLenum shader_type, | |
| 52 const GLchar* shader_source) { | |
| 53 GLuint shader_handle = gl->CreateShader(shader_type); | |
|
dcheng
2017/03/08 07:23:39
I'm not sure if it's possible, but perhaps we shou
klausw
2017/03/08 08:11:08
I'd agree in general, but this code is mainly inte
dcheng
2017/03/08 23:08:05
Again, doesn't have to be this CL, but I think it
| |
| 54 if (shader_handle != 0) { | |
| 55 // Pass in the shader source. | |
| 56 int len = strlen(shader_source); | |
|
dcheng
2017/03/08 07:23:39
size_t
klausw
2017/03/08 08:11:08
Changed to GLint since that's what glShaderSource
| |
| 57 gl->ShaderSource(shader_handle, 1, &shader_source, &len); | |
| 58 // Compile the shader. | |
| 59 gl->CompileShader(shader_handle); | |
| 60 // Get the compilation status. | |
| 61 GLint status = 0; | |
| 62 gl->GetShaderiv(shader_handle, GL_COMPILE_STATUS, &status); | |
| 63 if (status == GL_FALSE) { | |
| 64 GLint info_log_length = 0; | |
| 65 gl->GetShaderiv(shader_handle, GL_INFO_LOG_LENGTH, &info_log_length); | |
| 66 auto str_info_log = base::MakeUnique<GLchar[]>(info_log_length + 1); | |
| 67 gl->GetShaderInfoLog(shader_handle, info_log_length, nullptr, | |
| 68 str_info_log.get()); | |
| 69 DLOG(ERROR) << "Error compiling shader: " << str_info_log.get(); | |
| 70 gl->DeleteShader(shader_handle); | |
| 71 shader_handle = 0; | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 return shader_handle; | |
| 76 } | |
| 77 | |
| 78 GLuint CreateAndLinkProgram(gpu::gles2::GLES2Interface* gl, | |
| 79 GLuint vertex_shader_handle, | |
| 80 GLuint fragment_shader_handle) { | |
| 81 GLuint program_handle = gl->CreateProgram(); | |
| 82 | |
| 83 if (program_handle != 0) { | |
| 84 // Bind the vertex shader to the program. | |
| 85 gl->AttachShader(program_handle, vertex_shader_handle); | |
| 86 | |
| 87 // Bind the fragment shader to the program. | |
| 88 gl->AttachShader(program_handle, fragment_shader_handle); | |
| 89 | |
| 90 // Link the two shaders together into a program. | |
| 91 gl->LinkProgram(program_handle); | |
| 92 | |
| 93 // Get the link status. | |
| 94 GLint link_status = 0; | |
| 95 gl->GetProgramiv(program_handle, GL_LINK_STATUS, &link_status); | |
| 96 | |
| 97 // If the link failed, delete the program. | |
| 98 if (link_status == GL_FALSE) { | |
| 99 GLint info_log_length; | |
| 100 gl->GetProgramiv(program_handle, GL_INFO_LOG_LENGTH, &info_log_length); | |
| 101 | |
| 102 auto str_info_log = base::MakeUnique<GLchar[]>(info_log_length + 1); | |
| 103 gl->GetProgramInfoLog(program_handle, info_log_length, nullptr, | |
| 104 str_info_log.get()); | |
| 105 DLOG(ERROR) << "Error compiling program: " << str_info_log.get(); | |
| 106 gl->DeleteProgram(program_handle); | |
| 107 program_handle = 0; | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 return program_handle; | |
| 112 } | |
| 113 | |
| 114 GLuint ConsumeTexture(gpu::gles2::GLES2Interface* gl, | |
| 115 const gpu::MailboxHolder& mailbox) { | |
| 116 TRACE_EVENT0("gpu", "MailboxToSurfaceBridge::ConsumeTexture"); | |
| 117 gl->WaitSyncTokenCHROMIUM(mailbox.sync_token.GetConstData()); | |
| 118 | |
| 119 return gl->CreateAndConsumeTextureCHROMIUM(GL_TEXTURE_2D, | |
| 120 mailbox.mailbox.name); | |
| 121 } | |
| 122 | |
| 123 } // namespace | |
| 124 | |
| 125 namespace vr_shell { | |
| 126 | |
| 127 MailboxToSurfaceBridge::MailboxToSurfaceBridge() : weak_ptr_factory_(this) {} | |
| 128 | |
| 129 MailboxToSurfaceBridge::~MailboxToSurfaceBridge() { | |
| 130 if (surface_handle_) { | |
| 131 // Unregister from the surface tracker to avoid a resource leak. | |
| 132 gpu::GpuSurfaceTracker* tracker = gpu::GpuSurfaceTracker::Get(); | |
| 133 tracker->UnregisterViewSurface(surface_handle_); | |
| 134 } | |
| 135 DestroyContext(); | |
| 136 } | |
| 137 | |
| 138 void MailboxToSurfaceBridge::OnContextAvailable( | |
| 139 scoped_refptr<cc::ContextProvider> provider) { | |
| 140 // Must save a reference to the ContextProvider to keep it alive, | |
| 141 // otherwise the GL context created from it becomes invalid. | |
| 142 context_provider_ = provider; | |
|
dcheng
2017/03/08 07:23:39
Nit: std::move(provider)
klausw
2017/03/08 08:11:08
Done.
| |
| 143 | |
| 144 if (!context_provider_->BindToCurrentThread()) { | |
| 145 DLOG(ERROR) << "Failed to init ContextProvider"; | |
| 146 return; | |
| 147 } | |
| 148 | |
| 149 gl_ = context_provider_->ContextGL(); | |
| 150 | |
| 151 if (!gl_) { | |
| 152 DLOG(ERROR) << "Did not get a GL context"; | |
| 153 return; | |
| 154 } | |
| 155 InitializeRenderer(); | |
| 156 } | |
| 157 | |
| 158 void MailboxToSurfaceBridge::CreateSurface( | |
| 159 scoped_refptr<gl::SurfaceTexture> surface_texture) { | |
|
dcheng
2017/03/08 07:23:39
Nit: pass as a raw pointer since this function doe
klausw
2017/03/08 08:11:08
Done. It just needs the SurfaceTexture briefly to
| |
| 160 ANativeWindow* window = surface_texture->CreateSurface(); | |
| 161 gpu::GpuSurfaceTracker* tracker = gpu::GpuSurfaceTracker::Get(); | |
| 162 ANativeWindow_acquire(window); | |
| 163 // Skip ANativeWindow_setBuffersGeometry, the default size appears to work. | |
| 164 surface_handle_ = tracker->AddSurfaceForNativeWidget(window); | |
| 165 | |
| 166 auto surface = base::MakeUnique<gl::ScopedJavaSurface>(surface_texture.get()); | |
| 167 tracker->RegisterViewSurface(surface_handle_, surface->j_surface().obj()); | |
| 168 // Unregistering happens in the destructor. | |
| 169 ANativeWindow_release(window); | |
| 170 | |
| 171 // Our attributes must be compatible with the shared offscreen | |
| 172 // surface used by virtualized contexts, otherwise mailbox | |
| 173 // synchronization doesn't work properly - it assumes a shared | |
| 174 // underlying GL context. See GetCompositorContextAttributes | |
| 175 // in content/browser/renderer_host/compositor_impl_android.cc | |
| 176 // and crbug.com/699330. | |
| 177 | |
| 178 gpu::gles2::ContextCreationAttribHelper attributes; | |
| 179 attributes.alpha_size = -1; | |
| 180 attributes.red_size = 8; | |
| 181 attributes.green_size = 8; | |
| 182 attributes.blue_size = 8; | |
| 183 attributes.stencil_size = 0; | |
| 184 attributes.depth_size = 0; | |
| 185 attributes.samples = 0; | |
| 186 attributes.sample_buffers = 0; | |
| 187 attributes.bind_generates_resource = false; | |
| 188 if (base::SysInfo::IsLowEndDevice()) { | |
| 189 attributes.alpha_size = 0; | |
| 190 attributes.red_size = 5; | |
| 191 attributes.green_size = 6; | |
| 192 attributes.blue_size = 5; | |
| 193 } | |
| 194 | |
| 195 content::Compositor::CreateContextProvider( | |
| 196 surface_handle_, attributes, gpu::SharedMemoryLimits::ForMailboxContext(), | |
| 197 base::Bind(&MailboxToSurfaceBridge::OnContextAvailable, | |
| 198 weak_ptr_factory_.GetWeakPtr())); | |
| 199 } | |
| 200 | |
| 201 void MailboxToSurfaceBridge::ResizeSurface(int width, int height) { | |
| 202 if (!gl_) { | |
| 203 // We're not initialized yet, save the requested size for later. | |
| 204 needs_resize_ = true; | |
| 205 resize_width_ = width; | |
| 206 resize_height_ = height; | |
| 207 return; | |
| 208 } | |
| 209 gl_->ResizeCHROMIUM(width, height, 1.f, false); | |
| 210 gl_->Viewport(0, 0, width, height); | |
| 211 } | |
| 212 | |
| 213 bool MailboxToSurfaceBridge::CopyMailboxToSurfaceAndSwap( | |
| 214 const gpu::MailboxHolder& mailbox) { | |
| 215 if (!gl_) { | |
| 216 // We may not have a context yet, i.e. due to surface initialization | |
| 217 // being incomplete. This is not an error, but we obviously can't draw | |
| 218 // yet. | |
| 219 return false; | |
| 220 } | |
| 221 | |
| 222 if (needs_resize_) { | |
| 223 ResizeSurface(resize_width_, resize_height_); | |
| 224 needs_resize_ = false; | |
| 225 } | |
| 226 | |
| 227 GLuint sourceTexture = ConsumeTexture(gl_, mailbox); | |
| 228 DrawQuad(sourceTexture); | |
| 229 gl_->SwapBuffers(); | |
| 230 return true; | |
| 231 } | |
| 232 | |
| 233 void MailboxToSurfaceBridge::DestroyContext() { | |
| 234 gl_ = nullptr; | |
| 235 context_provider_ = nullptr; | |
| 236 } | |
| 237 | |
| 238 void MailboxToSurfaceBridge::InitializeRenderer() { | |
| 239 GLuint vertex_shader_handle = | |
| 240 CompileShader(gl_, GL_VERTEX_SHADER, kQuadCopyVertex); | |
| 241 if (!vertex_shader_handle) { | |
| 242 DestroyContext(); | |
| 243 return; | |
| 244 } | |
| 245 | |
| 246 GLuint fragment_shader_handle = | |
| 247 CompileShader(gl_, GL_FRAGMENT_SHADER, kQuadCopyFragment); | |
| 248 if (!fragment_shader_handle) { | |
| 249 DestroyContext(); | |
| 250 return; | |
| 251 } | |
| 252 | |
| 253 GLuint program_handle = | |
| 254 CreateAndLinkProgram(gl_, vertex_shader_handle, fragment_shader_handle); | |
| 255 if (!program_handle) { | |
| 256 DestroyContext(); | |
| 257 return; | |
| 258 } | |
| 259 | |
| 260 // Once the program is linked the shader objects are no longer needed | |
| 261 gl_->DeleteShader(vertex_shader_handle); | |
| 262 gl_->DeleteShader(fragment_shader_handle); | |
| 263 | |
| 264 GLuint position_handle = gl_->GetAttribLocation(program_handle, "a_Position"); | |
| 265 GLuint texCoord_handle = | |
| 266 gl_->GetAttribLocation(program_handle, "a_TexCoordinate"); | |
| 267 GLuint texUniform_handle = | |
| 268 gl_->GetUniformLocation(program_handle, "u_Texture"); | |
| 269 | |
| 270 GLuint vertexBuffer = 0; | |
| 271 gl_->GenBuffers(1, &vertexBuffer); | |
| 272 gl_->BindBuffer(GL_ARRAY_BUFFER, vertexBuffer); | |
| 273 gl_->BufferData(GL_ARRAY_BUFFER, kQuadVerticesSize, kQuadVertices, | |
| 274 GL_STATIC_DRAW); | |
| 275 | |
| 276 // Set state once only, we assume that nobody else modifies GL state in a way | |
| 277 // that would interfere with our operations. | |
| 278 gl_->Disable(GL_CULL_FACE); | |
| 279 gl_->DepthMask(GL_FALSE); | |
| 280 gl_->Disable(GL_DEPTH_TEST); | |
| 281 gl_->Disable(GL_SCISSOR_TEST); | |
| 282 gl_->Disable(GL_BLEND); | |
| 283 gl_->Disable(GL_POLYGON_OFFSET_FILL); | |
| 284 | |
| 285 // Not using gl_->Viewport, we assume that it defaults to the whole | |
| 286 // surface and gets updated by ResizeSurface externally as | |
| 287 // appropriate. | |
| 288 | |
| 289 gl_->UseProgram(program_handle); | |
| 290 | |
| 291 // Bind vertex attributes | |
| 292 gl_->BindBuffer(GL_ARRAY_BUFFER, vertexBuffer); | |
| 293 | |
| 294 gl_->EnableVertexAttribArray(position_handle); | |
| 295 gl_->EnableVertexAttribArray(texCoord_handle); | |
| 296 | |
| 297 static constexpr size_t VERTEX_STRIDE = sizeof(float) * 4; | |
| 298 static constexpr size_t POSITION_ELEMENTS = 2; | |
| 299 static constexpr size_t TEXCOORD_ELEMENTS = 2; | |
| 300 static constexpr size_t POSITION_OFFSET = 0; | |
| 301 static constexpr size_t TEXCOORD_OFFSET = sizeof(float) * 2; | |
| 302 | |
| 303 gl_->VertexAttribPointer(position_handle, POSITION_ELEMENTS, GL_FLOAT, false, | |
| 304 VERTEX_STRIDE, VOID_OFFSET(POSITION_OFFSET)); | |
| 305 gl_->VertexAttribPointer(texCoord_handle, TEXCOORD_ELEMENTS, GL_FLOAT, false, | |
| 306 VERTEX_STRIDE, VOID_OFFSET(TEXCOORD_OFFSET)); | |
| 307 | |
| 308 gl_->ActiveTexture(GL_TEXTURE0); | |
| 309 gl_->Uniform1i(texUniform_handle, 0); | |
| 310 } | |
| 311 | |
| 312 void MailboxToSurfaceBridge::DrawQuad(unsigned int texture_handle) { | |
| 313 // No need to clear since we're redrawing on top of the entire | |
| 314 // viewport, but let the GPU know we don't need the old content | |
| 315 // anymore. | |
| 316 GLenum discards[] = {GL_COLOR_EXT}; | |
| 317 gl_->DiscardFramebufferEXT(GL_FRAMEBUFFER, arraysize(discards), discards); | |
| 318 | |
| 319 // Configure texture. This is a 1:1 pixel copy since the surface | |
| 320 // size is resized to match the source canvas, so we can use | |
| 321 // GL_NEAREST. | |
| 322 gl_->BindTexture(GL_TEXTURE_2D, texture_handle); | |
| 323 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
| 324 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
| 325 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | |
| 326 gl_->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | |
| 327 gl_->DrawArrays(GL_TRIANGLE_FAN, 0, 4); | |
| 328 } | |
| 329 | |
| 330 } // namespace vr_shell | |
| OLD | NEW |