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