Index: chrome/browser/android/vr_shell/vr_shell_command_buffer_gl.cc |
diff --git a/chrome/browser/android/vr_shell/vr_shell_command_buffer_gl.cc b/chrome/browser/android/vr_shell/vr_shell_command_buffer_gl.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..941da8f3e91cf8f60c67109b5b76d3e405f94897 |
--- /dev/null |
+++ b/chrome/browser/android/vr_shell/vr_shell_command_buffer_gl.cc |
@@ -0,0 +1,179 @@ |
+// 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/vr_shell_command_buffer_gl.h" |
+ |
+#include "chrome/browser/android/vr_shell/vr_shell_gpu_renderer.h" |
+#include "gpu/command_buffer/client/gles2_interface.h" |
+#include "gpu/command_buffer/common/mailbox.h" |
+#include "gpu/ipc/common/gpu_surface_tracker.h" |
+#include "services/ui/public/cpp/gpu/context_provider_command_buffer.h" |
+#include "ui/android/context_provider_factory.h" |
+#include "ui/gl/android/surface_texture.h" |
+ |
+#if defined(OS_ANDROID) |
mthiesse
2017/03/02 15:55:16
nit: Remove this for now? We're still in the Andro
klausw
2017/03/07 02:55:54
Done.
|
+#include <android/native_window_jni.h> |
+#endif |
+namespace vr_shell { |
+ |
+VrShellCommandBufferGl::VrShellCommandBufferGl() : |
+ weak_ptr_factory_(this) {} |
+ |
+VrShellCommandBufferGl::~VrShellCommandBufferGl() {} |
+ |
+void VrShellCommandBufferGl::OnGpuChannelEstablished( |
+ scoped_refptr<gpu::GpuChannelHost> gpu_channel_host, |
+ ui::ContextProviderFactory::GpuChannelHostResult result) { |
+ |
+ VLOG(1) << __FUNCTION__ << ";;;"; |
+ switch (result) { |
+ // Don't retry if we are shutting down. |
mthiesse
2017/03/02 15:55:16
Also don't retry if initialization fails?
klausw
2017/03/07 02:55:55
Moot, the switch no longer exists.
|
+ case ui::ContextProviderFactory::GpuChannelHostResult:: |
+ FAILURE_FACTORY_SHUTDOWN: |
+ break; |
+ case ui::ContextProviderFactory::GpuChannelHostResult:: |
+ FAILURE_GPU_PROCESS_INITIALIZATION_FAILED: |
+ break; |
+ case ui::ContextProviderFactory::GpuChannelHostResult::SUCCESS: |
+ VLOG(1) << __FUNCTION__ << ";;; Success!"; |
+ |
+ const size_t full_screen_texture_size_in_bytes = 4000 * 3000 * 4; |
mthiesse
2017/03/02 15:55:16
Lots of magic numbers, where do these come from?
klausw
2017/03/07 02:55:54
Replaced with gpu::SharedMemoryLimits::ForMailboxC
|
+ gpu::SharedMemoryLimits limits; |
+ limits.command_buffer_size = 64 * 1024; |
+ limits.start_transfer_buffer_size = 64 * 1024; |
+ limits.min_transfer_buffer_size = 64 * 1024; |
+ limits.max_transfer_buffer_size = full_screen_texture_size_in_bytes; |
+ limits.mapped_memory_reclaim_limit = full_screen_texture_size_in_bytes; |
+ |
+ gpu::gles2::ContextCreationAttribHelper attributes; |
+ attributes.alpha_size = 0; |
+ attributes.stencil_size = 0; |
+ attributes.depth_size = 0; |
+ attributes.samples = 0; |
+ attributes.sample_buffers = 0; |
+ attributes.bind_generates_resource = false; |
+ |
+ bool automatic_flushes = false; |
+ bool support_locking = false; |
+ constexpr ui::ContextProviderCommandBuffer* shared_context_provider = |
+ nullptr; |
+ context_provider_command_buffer_ = make_scoped_refptr( |
+ new ui::ContextProviderCommandBuffer( |
+ std::move(gpu_channel_host), gpu::GPU_STREAM_DEFAULT, |
+ gpu::GpuStreamPriority::NORMAL, surface_handle_, |
+ GURL("chrome://gpu/MusContextFactory"), automatic_flushes, |
+ support_locking, limits, attributes, |
+ shared_context_provider, |
+ ui::command_buffer_metrics::MUS_CLIENT_CONTEXT)); |
+ |
+ VLOG(1) << __FUNCTION__ << ";;; context_provider_command_buffer=" << |
+ context_provider_command_buffer_; |
+ if (!context_provider_command_buffer_->BindToCurrentThread()) { |
+ LOG(ERROR) << __FUNCTION__ << ";;; failed to init ContextProvider"; |
+ break; |
+ } |
+ VLOG(1) << __FUNCTION__ << ";;; bind succeeded"; |
+ |
+ gl_ = context_provider_command_buffer_->ContextGL(); |
+ VLOG(1) << __FUNCTION__ << ";;; gl_=" << gl_; |
+ |
+ break; |
+ } |
+} |
+ |
+void VrShellCommandBufferGl::CreateContext( |
+ scoped_refptr<gl::SurfaceTexture> surface_texture) { |
+ ANativeWindow* window = surface_texture->CreateSurface(); |
+ gpu::GpuSurfaceTracker* tracker = gpu::GpuSurfaceTracker::Get(); |
+ ANativeWindow_acquire(window); |
+ // setBuffersGeometry seems optional, it uses the SurfaceTexture's |
+ // default size by default? |
+ // ANativeWindow_setBuffersGeometry( |
+ // window, 2048, 1024, WINDOW_FORMAT_RGBA_8888); // FIXME! |
+ surface_handle_ = tracker->AddSurfaceForNativeWidget(window); |
+ VLOG(1) << __FUNCTION__ << ";;;: surface_handle_=" << surface_handle_; |
+ |
+ auto surface = base::MakeUnique<gl::ScopedJavaSurface>(surface_texture.get()); |
+ tracker->RegisterViewSurface( |
+ surface_handle_, surface->j_surface().obj()); |
+ // When should this be released? Does registering it keep it alive? |
mthiesse
2017/03/02 15:55:16
Is this relevant? https://codereview.chromium.org/
klausw
2017/03/07 02:55:54
It is indeed, added unregistration to destructor.
|
+ ANativeWindow_release(window); |
+ |
+ VLOG(1) << __FUNCTION__ << ";;; RequestGpuChannelHost start"; |
+ ui::ContextProviderFactory::GetInstance()->RequestGpuChannelHost(base::Bind( |
+ &VrShellCommandBufferGl::OnGpuChannelEstablished, |
+ weak_ptr_factory_.GetWeakPtr())); |
+ VLOG(1) << __FUNCTION__ << ";;; RequestGpuChannelHost end"; |
+} |
+ |
+bool VrShellCommandBufferGl::CopyFrameToSurface( |
+ int frame_index, const gpu::Mailbox& mailbox, bool discard) { |
+ auto gl = GetContext();; |
+ if (!gl) { |
+ VLOG(1) << __FUNCTION__ << ";;; drop, no GL context"; |
+ return false; |
+ } |
+ VLOG(1) << __FUNCTION__ << ";;; frame=" << frame_index << " gl=" << gl; |
+ |
+ // Default viewport size seems ok, don't set one here and leave it |
+ // alone elsewhere. |
+ // gl->Viewport(0, 0, 2560, 1440); // FIXME! |
+ |
+ // TODO(klausw): make this a proper member |
+ static GpuRenderer* copyRenderer = nullptr; |
+ if (!copyRenderer) { |
+ copyRenderer = new GpuRenderer(gl); |
+ } |
+ |
+ VLOG(1) << __FUNCTION__ << ";;; got mailbox, name=" << |
+ static_cast<int>(mailbox.name[0]) << "," << |
+ static_cast<int>(mailbox.name[1]) << "," << |
+ static_cast<int>(mailbox.name[2]) << "," << |
+ static_cast<int>(mailbox.name[3]) << "," << |
+ static_cast<int>(mailbox.name[4]) << "," << |
+ static_cast<int>(mailbox.name[5]) << "," << |
+ static_cast<int>(mailbox.name[6]) << "," << |
+ static_cast<int>(mailbox.name[7]) << "," << |
+ static_cast<int>(mailbox.name[8]) << "," << |
+ static_cast<int>(mailbox.name[9]) << "," << |
+ static_cast<int>(mailbox.name[10]) << "," << |
+ static_cast<int>(mailbox.name[11]) << "," << |
+ static_cast<int>(mailbox.name[12]) << "," << |
+ static_cast<int>(mailbox.name[13]) << "," << |
+ static_cast<int>(mailbox.name[14]) << "," << |
+ static_cast<int>(mailbox.name[15]); |
+ GLuint vrSourceTexture = gl->CreateAndConsumeTextureCHROMIUM( |
+ GL_TEXTURE_2D, mailbox.name); |
+ VLOG(1) << __FUNCTION__ << ";;; vrSourceTexture=" << vrSourceTexture; |
+ |
+ bool skip_this_frame = false; |
+ |
+ if (discard) { |
+ // We've consumed the texture, but we can't draw it to the surface, the |
+ // previous one hasn't been consumed yet. Swapping twice loses frames. |
+ // This should be rare, it's a waste of resources and can suppress useful |
+ // frames. |
+ VLOG(1) << __FUNCTION__ << ";;; Dropping frame, got one queued already."; |
+ skip_this_frame = true; |
+ } |
+ |
+ VLOG(1) << __FUNCTION__ << ";;; Draw start"; |
+ copyRenderer->DrawQuad(gl, vrSourceTexture); |
+ VLOG(1) << __FUNCTION__ << ";;; Draw done"; |
+ |
+ if (skip_this_frame) { |
+ VLOG(1) << __FUNCTION__ << ";;; drop frame=" << frame_index; |
+ return false; |
+ } |
+ |
+ // This is a load-bearing glFlush(). Removing it breaks rendering. WTF. |
+ // FIXME: check if this is still true. |
+ gl->Flush(); |
+ gl->SwapBuffers(); |
+ gl->Flush(); |
+ VLOG(1) << __FUNCTION__ << ";;; Swap/flush done"; |
+ return true; |
+} |
+ |
+} // namespace vr_shell |