OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "services/ui/surfaces/surfaces_context_provider.h" | 5 #include "services/ui/surfaces/surfaces_context_provider.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
13 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
15 #include "gpu/command_buffer/client/gles2_cmd_helper.h" | 15 #include "gpu/command_buffer/client/gles2_cmd_helper.h" |
16 #include "gpu/command_buffer/client/gles2_implementation.h" | 16 #include "gpu/command_buffer/client/gles2_implementation.h" |
17 #include "gpu/command_buffer/client/shared_memory_limits.h" | 17 #include "gpu/command_buffer/client/shared_memory_limits.h" |
18 #include "gpu/command_buffer/client/transfer_buffer.h" | 18 #include "gpu/command_buffer/client/transfer_buffer.h" |
19 #include "gpu/ipc/client/command_buffer_proxy_impl.h" | 19 #include "gpu/ipc/client/command_buffer_proxy_impl.h" |
20 #include "services/ui/common/switches.h" | 20 #include "services/ui/common/switches.h" |
21 #include "services/ui/gles2/command_buffer_driver.h" | |
22 #include "services/ui/gles2/command_buffer_impl.h" | |
23 #include "services/ui/gles2/command_buffer_local.h" | |
24 #include "services/ui/gles2/gpu_state.h" | |
25 #include "services/ui/gpu/gpu_service_mus.h" | 21 #include "services/ui/gpu/gpu_service_mus.h" |
26 #include "services/ui/surfaces/surfaces_context_provider_delegate.h" | 22 #include "services/ui/surfaces/surfaces_context_provider_delegate.h" |
27 #include "ui/gl/gpu_preference.h" | 23 #include "ui/gl/gpu_preference.h" |
28 | 24 |
29 namespace ui { | 25 namespace ui { |
30 | 26 |
31 SurfacesContextProvider::SurfacesContextProvider( | 27 SurfacesContextProvider::SurfacesContextProvider(gfx::AcceleratedWidget widget) |
32 gfx::AcceleratedWidget widget, | 28 : delegate_(nullptr), widget_(widget) { |
33 const scoped_refptr<GpuState>& state) | 29 GpuServiceMus* service = GpuServiceMus::GetInstance(); |
34 : use_chrome_gpu_command_buffer_(false), | 30 gpu::CommandBufferProxyImpl* shared_command_buffer = nullptr; |
35 delegate_(nullptr), | 31 gpu::GpuStreamId stream_id = gpu::GpuStreamId::GPU_STREAM_DEFAULT; |
36 widget_(widget), | 32 gpu::GpuStreamPriority stream_priority = gpu::GpuStreamPriority::NORMAL; |
37 command_buffer_local_(nullptr) { | 33 gpu::gles2::ContextCreationAttribHelper attributes; |
38 // TODO(penghuang): Kludge: Use mojo command buffer when running on Windows | 34 attributes.alpha_size = -1; |
39 // since Chrome command buffer breaks unit tests | 35 attributes.depth_size = 0; |
40 #if defined(OS_WIN) | 36 attributes.stencil_size = 0; |
41 use_chrome_gpu_command_buffer_ = false; | 37 attributes.samples = 0; |
42 #else | 38 attributes.sample_buffers = 0; |
43 use_chrome_gpu_command_buffer_ = | 39 attributes.bind_generates_resource = false; |
44 !base::CommandLine::ForCurrentProcess()->HasSwitch( | 40 attributes.lose_context_when_out_of_memory = true; |
45 switches::kUseMojoGpuCommandBufferInMus); | 41 GURL active_url; |
46 #endif | 42 scoped_refptr<base::SingleThreadTaskRunner> task_runner = |
47 if (!use_chrome_gpu_command_buffer_) { | 43 base::ThreadTaskRunnerHandle::Get(); |
48 command_buffer_local_ = new CommandBufferLocal(this, widget_, state); | 44 command_buffer_proxy_impl_ = gpu::CommandBufferProxyImpl::Create( |
49 } else { | 45 service->gpu_channel_local(), widget, shared_command_buffer, stream_id, |
50 GpuServiceMus* service = GpuServiceMus::GetInstance(); | 46 stream_priority, attributes, active_url, task_runner); |
51 gpu::CommandBufferProxyImpl* shared_command_buffer = nullptr; | 47 command_buffer_proxy_impl_->SetSwapBuffersCompletionCallback( |
52 gpu::GpuStreamId stream_id = gpu::GpuStreamId::GPU_STREAM_DEFAULT; | 48 base::Bind(&SurfacesContextProvider::OnGpuSwapBuffersCompleted, |
53 gpu::GpuStreamPriority stream_priority = gpu::GpuStreamPriority::NORMAL; | 49 base::Unretained(this))); |
54 gpu::gles2::ContextCreationAttribHelper attributes; | 50 command_buffer_proxy_impl_->SetUpdateVSyncParametersCallback( |
55 attributes.alpha_size = -1; | 51 base::Bind(&SurfacesContextProvider::OnUpdateVSyncParameters, |
56 attributes.depth_size = 0; | 52 base::Unretained(this))); |
57 attributes.stencil_size = 0; | |
58 attributes.samples = 0; | |
59 attributes.sample_buffers = 0; | |
60 attributes.bind_generates_resource = false; | |
61 attributes.lose_context_when_out_of_memory = true; | |
62 GURL active_url; | |
63 scoped_refptr<base::SingleThreadTaskRunner> task_runner = | |
64 base::ThreadTaskRunnerHandle::Get(); | |
65 command_buffer_proxy_impl_ = gpu::CommandBufferProxyImpl::Create( | |
66 service->gpu_channel_local(), widget, shared_command_buffer, stream_id, | |
67 stream_priority, attributes, active_url, task_runner); | |
68 command_buffer_proxy_impl_->SetSwapBuffersCompletionCallback( | |
69 base::Bind(&SurfacesContextProvider::OnGpuSwapBuffersCompleted, | |
70 base::Unretained(this))); | |
71 command_buffer_proxy_impl_->SetUpdateVSyncParametersCallback( | |
72 base::Bind(&SurfacesContextProvider::OnUpdateVSyncParameters, | |
73 base::Unretained(this))); | |
74 } | |
75 } | 53 } |
76 | 54 |
77 void SurfacesContextProvider::SetDelegate( | 55 void SurfacesContextProvider::SetDelegate( |
78 SurfacesContextProviderDelegate* delegate) { | 56 SurfacesContextProviderDelegate* delegate) { |
79 DCHECK(!delegate_); | 57 DCHECK(!delegate_); |
80 delegate_ = delegate; | 58 delegate_ = delegate; |
81 } | 59 } |
82 | 60 |
83 // This routine needs to be safe to call more than once. | 61 // This routine needs to be safe to call more than once. |
84 // This is called when we have an accelerated widget. | 62 // This is called when we have an accelerated widget. |
85 bool SurfacesContextProvider::BindToCurrentThread() { | 63 bool SurfacesContextProvider::BindToCurrentThread() { |
86 if (implementation_) | 64 if (implementation_) |
87 return true; | 65 return true; |
88 | 66 |
89 // SurfacesContextProvider should always live on the same thread as the | 67 // SurfacesContextProvider should always live on the same thread as the |
90 // Window Manager. | 68 // Window Manager. |
91 DCHECK(CalledOnValidThread()); | 69 DCHECK(CalledOnValidThread()); |
92 gpu::GpuControl* gpu_control = nullptr; | 70 if (!command_buffer_proxy_impl_) |
93 gpu::CommandBuffer* command_buffer = nullptr; | 71 return false; |
94 if (!use_chrome_gpu_command_buffer_) { | 72 gpu::GpuControl* gpu_control = command_buffer_proxy_impl_.get(); |
95 if (!command_buffer_local_->Initialize()) | 73 gpu::CommandBuffer* command_buffer = command_buffer_proxy_impl_.get(); |
96 return false; | |
97 gpu_control = command_buffer_local_; | |
98 command_buffer = command_buffer_local_; | |
99 } else { | |
100 if (!command_buffer_proxy_impl_) | |
101 return false; | |
102 gpu_control = command_buffer_proxy_impl_.get(); | |
103 command_buffer = command_buffer_proxy_impl_.get(); | |
104 } | |
105 | 74 |
106 gles2_helper_.reset(new gpu::gles2::GLES2CmdHelper(command_buffer)); | 75 gles2_helper_.reset(new gpu::gles2::GLES2CmdHelper(command_buffer)); |
107 constexpr gpu::SharedMemoryLimits default_limits; | 76 constexpr gpu::SharedMemoryLimits default_limits; |
108 if (!gles2_helper_->Initialize(default_limits.command_buffer_size)) | 77 if (!gles2_helper_->Initialize(default_limits.command_buffer_size)) |
109 return false; | 78 return false; |
110 gles2_helper_->SetAutomaticFlushes(false); | 79 gles2_helper_->SetAutomaticFlushes(false); |
111 transfer_buffer_.reset(new gpu::TransferBuffer(gles2_helper_.get())); | 80 transfer_buffer_.reset(new gpu::TransferBuffer(gles2_helper_.get())); |
112 capabilities_ = gpu_control->GetCapabilities(); | 81 capabilities_ = gpu_control->GetCapabilities(); |
113 bool bind_generates_resource = | 82 bool bind_generates_resource = |
114 !!capabilities_.bind_generates_resource_chromium; | 83 !!capabilities_.bind_generates_resource_chromium; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 const LostContextCallback& lost_context_callback) { | 125 const LostContextCallback& lost_context_callback) { |
157 implementation_->SetLostContextCallback(lost_context_callback); | 126 implementation_->SetLostContextCallback(lost_context_callback); |
158 } | 127 } |
159 | 128 |
160 SurfacesContextProvider::~SurfacesContextProvider() { | 129 SurfacesContextProvider::~SurfacesContextProvider() { |
161 implementation_->Flush(); | 130 implementation_->Flush(); |
162 implementation_.reset(); | 131 implementation_.reset(); |
163 transfer_buffer_.reset(); | 132 transfer_buffer_.reset(); |
164 gles2_helper_.reset(); | 133 gles2_helper_.reset(); |
165 command_buffer_proxy_impl_.reset(); | 134 command_buffer_proxy_impl_.reset(); |
166 if (command_buffer_local_) { | |
167 command_buffer_local_->Destroy(); | |
168 command_buffer_local_ = nullptr; | |
169 } | |
170 } | |
171 | |
172 void SurfacesContextProvider::UpdateVSyncParameters( | |
173 const base::TimeTicks& timebase, | |
174 const base::TimeDelta& interval) { | |
175 if (delegate_) | |
176 delegate_->OnVSyncParametersUpdated(timebase, interval); | |
177 } | |
178 | |
179 void SurfacesContextProvider::GpuCompletedSwapBuffers(gfx::SwapResult result) { | |
180 if (!swap_buffers_completion_callback_.is_null()) { | |
181 swap_buffers_completion_callback_.Run(result); | |
182 } | |
183 } | 135 } |
184 | 136 |
185 void SurfacesContextProvider::OnGpuSwapBuffersCompleted( | 137 void SurfacesContextProvider::OnGpuSwapBuffersCompleted( |
186 const std::vector<ui::LatencyInfo>& latency_info, | 138 const std::vector<ui::LatencyInfo>& latency_info, |
187 gfx::SwapResult result, | 139 gfx::SwapResult result, |
188 const gpu::GpuProcessHostedCALayerTreeParamsMac* params_mac) { | 140 const gpu::GpuProcessHostedCALayerTreeParamsMac* params_mac) { |
189 if (!swap_buffers_completion_callback_.is_null()) { | 141 if (!swap_buffers_completion_callback_.is_null()) { |
190 swap_buffers_completion_callback_.Run(result); | 142 swap_buffers_completion_callback_.Run(result); |
191 } | 143 } |
192 } | 144 } |
193 | 145 |
194 void SurfacesContextProvider::OnUpdateVSyncParameters( | 146 void SurfacesContextProvider::OnUpdateVSyncParameters( |
195 base::TimeTicks timebase, | 147 base::TimeTicks timebase, |
196 base::TimeDelta interval) { | 148 base::TimeDelta interval) { |
197 if (delegate_) | 149 if (delegate_) |
198 delegate_->OnVSyncParametersUpdated(timebase, interval); | 150 delegate_->OnVSyncParametersUpdated(timebase, interval); |
199 } | 151 } |
200 | 152 |
201 void SurfacesContextProvider::SetSwapBuffersCompletionCallback( | 153 void SurfacesContextProvider::SetSwapBuffersCompletionCallback( |
202 gl::GLSurface::SwapCompletionCallback callback) { | 154 gl::GLSurface::SwapCompletionCallback callback) { |
203 swap_buffers_completion_callback_ = callback; | 155 swap_buffers_completion_callback_ = callback; |
204 } | 156 } |
205 | 157 |
206 } // namespace ui | 158 } // namespace ui |
OLD | NEW |