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/vr_shell_command_buffer_gl.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "chrome/browser/android/vr_shell/vr_shell_gpu_renderer.h" |
| 9 #include "content/public/browser/android/compositor.h" |
| 10 #include "gpu/command_buffer/client/gles2_interface.h" |
| 11 #include "gpu/command_buffer/common/mailbox.h" |
| 12 #include "gpu/command_buffer/common/mailbox_holder.h" |
| 13 #include "gpu/command_buffer/common/sync_token.h" |
| 14 #include "gpu/ipc/client/gpu_channel_host.h" |
| 15 #include "gpu/ipc/common/gpu_surface_tracker.h" |
| 16 #include "services/ui/public/cpp/gpu/context_provider_command_buffer.h" |
| 17 #include "ui/gl/android/surface_texture.h" |
| 18 |
| 19 #include <android/native_window_jni.h> |
| 20 |
| 21 namespace vr_shell { |
| 22 |
| 23 VrShellCommandBufferGl::VrShellCommandBufferGl() : weak_ptr_factory_(this) {} |
| 24 |
| 25 VrShellCommandBufferGl::~VrShellCommandBufferGl() { |
| 26 if (surface_handle_) { |
| 27 // Unregister from the surface tracker to avoid a resource leak. |
| 28 gpu::GpuSurfaceTracker* tracker = gpu::GpuSurfaceTracker::Get(); |
| 29 tracker->UnregisterViewSurface(surface_handle_); |
| 30 } |
| 31 } |
| 32 |
| 33 void VrShellCommandBufferGl::OnGpuChannelEstablished( |
| 34 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host) { |
| 35 // Our attributes must be compatible with the shared offscreen |
| 36 // surface used by virtualized contexts, otherwise mailbox |
| 37 // synchronization doesn't work properly - it assumes a shared |
| 38 // underlying GL context. TODO(klausw): is there a more official |
| 39 // way to get default-compatible settings? |
| 40 gpu::gles2::ContextCreationAttribHelper attributes; |
| 41 attributes.alpha_size = -1; |
| 42 attributes.red_size = 8; |
| 43 attributes.green_size = 8; |
| 44 attributes.blue_size = 8; |
| 45 attributes.stencil_size = 0; |
| 46 attributes.depth_size = 0; |
| 47 attributes.samples = 0; |
| 48 attributes.sample_buffers = 0; |
| 49 attributes.bind_generates_resource = false; |
| 50 |
| 51 bool automatic_flushes = false; |
| 52 bool support_locking = false; |
| 53 constexpr ui::ContextProviderCommandBuffer* shared_context_provider = nullptr; |
| 54 context_provider_command_buffer_ = |
| 55 make_scoped_refptr(new ui::ContextProviderCommandBuffer( |
| 56 std::move(gpu_channel_host), gpu::GPU_STREAM_DEFAULT, |
| 57 gpu::GpuStreamPriority::NORMAL, surface_handle_, |
| 58 GURL("chrome://gpu/WebVRContextFactory"), automatic_flushes, |
| 59 support_locking, gpu::SharedMemoryLimits::ForMailboxContext(), |
| 60 attributes, shared_context_provider, |
| 61 ui::command_buffer_metrics::CONTEXT_TYPE_UNKNOWN)); |
| 62 |
| 63 if (!context_provider_command_buffer_->BindToCurrentThread()) { |
| 64 LOG(ERROR) << __FUNCTION__ << ";;; failed to init ContextProvider"; |
| 65 return; |
| 66 } |
| 67 |
| 68 gl_ = context_provider_command_buffer_->ContextGL(); |
| 69 |
| 70 copy_renderer_ = base::MakeUnique<GpuRenderer>(gl_); |
| 71 } |
| 72 |
| 73 std::unique_ptr<gl::ScopedJavaSurface> VrShellCommandBufferGl::CreateSurface( |
| 74 scoped_refptr<gl::SurfaceTexture> surface_texture) { |
| 75 ANativeWindow* window = surface_texture->CreateSurface(); |
| 76 gpu::GpuSurfaceTracker* tracker = gpu::GpuSurfaceTracker::Get(); |
| 77 ANativeWindow_acquire(window); |
| 78 // Skip ANativeWindow_setBuffersGeometry, the default size appears to work. |
| 79 surface_handle_ = tracker->AddSurfaceForNativeWidget(window); |
| 80 |
| 81 auto surface = base::MakeUnique<gl::ScopedJavaSurface>(surface_texture.get()); |
| 82 tracker->RegisterViewSurface(surface_handle_, surface->j_surface().obj()); |
| 83 // Unregistering happens in the destructor. |
| 84 ANativeWindow_release(window); |
| 85 |
| 86 gpu::GpuChannelEstablishFactory* factory = |
| 87 content::Compositor::GetGpuChannelFactory(); |
| 88 |
| 89 factory->EstablishGpuChannel( |
| 90 base::Bind(&VrShellCommandBufferGl::OnGpuChannelEstablished, |
| 91 weak_ptr_factory_.GetWeakPtr())); |
| 92 |
| 93 return surface; |
| 94 } |
| 95 |
| 96 void VrShellCommandBufferGl::ResizeSurface(int width, int height) { |
| 97 if (!gl_) { |
| 98 LOG(ERROR) << "Cannot resize, not initialized"; |
| 99 return; |
| 100 } |
| 101 gl_->ResizeCHROMIUM(width, height, 1.f, false); |
| 102 gl_->Viewport(0, 0, width, height); |
| 103 } |
| 104 |
| 105 bool VrShellCommandBufferGl::CopyFrameToSurface( |
| 106 int frame_index, |
| 107 const gpu::MailboxHolder& mailbox, |
| 108 bool discard) { |
| 109 TRACE_EVENT1("gpu", "VrShellCommandBufferGl::CopyFrameToSurface", "frame", |
| 110 frame_index); |
| 111 if (!gl_) { |
| 112 // We may not have a context yet, i.e. due to surface initialization |
| 113 // being incomplete. This is not an error, but we obviously can't draw |
| 114 // yet. |
| 115 return false; |
| 116 } |
| 117 |
| 118 { |
| 119 TRACE_EVENT0("gpu", "VrShellCommandBufferGl::WaitSyncToken"); |
| 120 gl_->WaitSyncTokenCHROMIUM(mailbox.sync_token.GetConstData()); |
| 121 } |
| 122 |
| 123 GLuint vrSourceTexture = |
| 124 gl_->CreateAndConsumeTextureCHROMIUM(GL_TEXTURE_2D, mailbox.mailbox.name); |
| 125 |
| 126 if (discard) { |
| 127 // We've consumed the texture, but the caller requested that we |
| 128 // don't draw it because the previous one hasn't been consumed |
| 129 // yet. (Swapping twice on a Surfacewithout consuming one in |
| 130 // between from the SurfaceTexture can lose frames.) This should |
| 131 // be rare, it's a waste of resources and can cause jerky |
| 132 // animation due to the lost frames. |
| 133 return false; |
| 134 } else { |
| 135 copy_renderer_->DrawQuad(gl_, vrSourceTexture); |
| 136 gl_->SwapBuffers(); |
| 137 return true; |
| 138 } |
| 139 } |
| 140 |
| 141 } // namespace vr_shell |
OLD | NEW |