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" |
21 #include "services/ui/gpu/gpu_service_mus.h" | 25 #include "services/ui/gpu/gpu_service_mus.h" |
22 #include "services/ui/surfaces/surfaces_context_provider_delegate.h" | 26 #include "services/ui/surfaces/surfaces_context_provider_delegate.h" |
23 #include "ui/gl/gpu_preference.h" | 27 #include "ui/gl/gpu_preference.h" |
24 | 28 |
25 namespace ui { | 29 namespace ui { |
26 | 30 |
27 SurfacesContextProvider::SurfacesContextProvider(gfx::AcceleratedWidget widget) | 31 SurfacesContextProvider::SurfacesContextProvider( |
28 : delegate_(nullptr), widget_(widget) { | 32 gfx::AcceleratedWidget widget, |
29 GpuServiceMus* service = GpuServiceMus::GetInstance(); | 33 const scoped_refptr<GpuState>& state) |
30 gpu::CommandBufferProxyImpl* shared_command_buffer = nullptr; | 34 : use_chrome_gpu_command_buffer_(false), |
31 gpu::GpuStreamId stream_id = gpu::GpuStreamId::GPU_STREAM_DEFAULT; | 35 delegate_(nullptr), |
32 gpu::GpuStreamPriority stream_priority = gpu::GpuStreamPriority::NORMAL; | 36 widget_(widget), |
33 gpu::gles2::ContextCreationAttribHelper attributes; | 37 command_buffer_local_(nullptr) { |
34 attributes.alpha_size = -1; | 38 // TODO(penghuang): Kludge: Use mojo command buffer when running on Windows |
35 attributes.depth_size = 0; | 39 // since Chrome command buffer breaks unit tests |
36 attributes.stencil_size = 0; | 40 #if defined(OS_WIN) |
37 attributes.samples = 0; | 41 use_chrome_gpu_command_buffer_ = false; |
38 attributes.sample_buffers = 0; | 42 #else |
39 attributes.bind_generates_resource = false; | 43 use_chrome_gpu_command_buffer_ = |
40 attributes.lose_context_when_out_of_memory = true; | 44 !base::CommandLine::ForCurrentProcess()->HasSwitch( |
41 GURL active_url; | 45 switches::kUseMojoGpuCommandBufferInMus); |
42 scoped_refptr<base::SingleThreadTaskRunner> task_runner = | 46 #endif |
43 base::ThreadTaskRunnerHandle::Get(); | 47 if (!use_chrome_gpu_command_buffer_) { |
44 command_buffer_proxy_impl_ = gpu::CommandBufferProxyImpl::Create( | 48 command_buffer_local_ = new CommandBufferLocal(this, widget_, state); |
45 service->gpu_channel_local(), widget, shared_command_buffer, stream_id, | 49 } else { |
46 stream_priority, attributes, active_url, task_runner); | 50 GpuServiceMus* service = GpuServiceMus::GetInstance(); |
47 command_buffer_proxy_impl_->SetSwapBuffersCompletionCallback( | 51 gpu::CommandBufferProxyImpl* shared_command_buffer = nullptr; |
48 base::Bind(&SurfacesContextProvider::OnGpuSwapBuffersCompleted, | 52 gpu::GpuStreamId stream_id = gpu::GpuStreamId::GPU_STREAM_DEFAULT; |
49 base::Unretained(this))); | 53 gpu::GpuStreamPriority stream_priority = gpu::GpuStreamPriority::NORMAL; |
50 command_buffer_proxy_impl_->SetUpdateVSyncParametersCallback( | 54 gpu::gles2::ContextCreationAttribHelper attributes; |
51 base::Bind(&SurfacesContextProvider::OnUpdateVSyncParameters, | 55 attributes.alpha_size = -1; |
52 base::Unretained(this))); | 56 attributes.depth_size = 0; |
| 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 } |
53 } | 75 } |
54 | 76 |
55 void SurfacesContextProvider::SetDelegate( | 77 void SurfacesContextProvider::SetDelegate( |
56 SurfacesContextProviderDelegate* delegate) { | 78 SurfacesContextProviderDelegate* delegate) { |
57 DCHECK(!delegate_); | 79 DCHECK(!delegate_); |
58 delegate_ = delegate; | 80 delegate_ = delegate; |
59 } | 81 } |
60 | 82 |
61 // This routine needs to be safe to call more than once. | 83 // This routine needs to be safe to call more than once. |
62 // This is called when we have an accelerated widget. | 84 // This is called when we have an accelerated widget. |
63 bool SurfacesContextProvider::BindToCurrentThread() { | 85 bool SurfacesContextProvider::BindToCurrentThread() { |
64 if (implementation_) | 86 if (implementation_) |
65 return true; | 87 return true; |
66 | 88 |
67 // SurfacesContextProvider should always live on the same thread as the | 89 // SurfacesContextProvider should always live on the same thread as the |
68 // Window Manager. | 90 // Window Manager. |
69 DCHECK(CalledOnValidThread()); | 91 DCHECK(CalledOnValidThread()); |
70 if (!command_buffer_proxy_impl_) | 92 gpu::GpuControl* gpu_control = nullptr; |
71 return false; | 93 gpu::CommandBuffer* command_buffer = nullptr; |
72 gpu::GpuControl* gpu_control = command_buffer_proxy_impl_.get(); | 94 if (!use_chrome_gpu_command_buffer_) { |
73 gpu::CommandBuffer* command_buffer = command_buffer_proxy_impl_.get(); | 95 if (!command_buffer_local_->Initialize()) |
| 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 } |
74 | 105 |
75 gles2_helper_.reset(new gpu::gles2::GLES2CmdHelper(command_buffer)); | 106 gles2_helper_.reset(new gpu::gles2::GLES2CmdHelper(command_buffer)); |
76 constexpr gpu::SharedMemoryLimits default_limits; | 107 constexpr gpu::SharedMemoryLimits default_limits; |
77 if (!gles2_helper_->Initialize(default_limits.command_buffer_size)) | 108 if (!gles2_helper_->Initialize(default_limits.command_buffer_size)) |
78 return false; | 109 return false; |
79 gles2_helper_->SetAutomaticFlushes(false); | 110 gles2_helper_->SetAutomaticFlushes(false); |
80 transfer_buffer_.reset(new gpu::TransferBuffer(gles2_helper_.get())); | 111 transfer_buffer_.reset(new gpu::TransferBuffer(gles2_helper_.get())); |
81 capabilities_ = gpu_control->GetCapabilities(); | 112 capabilities_ = gpu_control->GetCapabilities(); |
82 bool bind_generates_resource = | 113 bool bind_generates_resource = |
83 !!capabilities_.bind_generates_resource_chromium; | 114 !!capabilities_.bind_generates_resource_chromium; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 const LostContextCallback& lost_context_callback) { | 156 const LostContextCallback& lost_context_callback) { |
126 implementation_->SetLostContextCallback(lost_context_callback); | 157 implementation_->SetLostContextCallback(lost_context_callback); |
127 } | 158 } |
128 | 159 |
129 SurfacesContextProvider::~SurfacesContextProvider() { | 160 SurfacesContextProvider::~SurfacesContextProvider() { |
130 implementation_->Flush(); | 161 implementation_->Flush(); |
131 implementation_.reset(); | 162 implementation_.reset(); |
132 transfer_buffer_.reset(); | 163 transfer_buffer_.reset(); |
133 gles2_helper_.reset(); | 164 gles2_helper_.reset(); |
134 command_buffer_proxy_impl_.reset(); | 165 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 } |
135 } | 183 } |
136 | 184 |
137 void SurfacesContextProvider::OnGpuSwapBuffersCompleted( | 185 void SurfacesContextProvider::OnGpuSwapBuffersCompleted( |
138 const std::vector<ui::LatencyInfo>& latency_info, | 186 const std::vector<ui::LatencyInfo>& latency_info, |
139 gfx::SwapResult result, | 187 gfx::SwapResult result, |
140 const gpu::GpuProcessHostedCALayerTreeParamsMac* params_mac) { | 188 const gpu::GpuProcessHostedCALayerTreeParamsMac* params_mac) { |
141 if (!swap_buffers_completion_callback_.is_null()) { | 189 if (!swap_buffers_completion_callback_.is_null()) { |
142 swap_buffers_completion_callback_.Run(result); | 190 swap_buffers_completion_callback_.Run(result); |
143 } | 191 } |
144 } | 192 } |
145 | 193 |
146 void SurfacesContextProvider::OnUpdateVSyncParameters( | 194 void SurfacesContextProvider::OnUpdateVSyncParameters( |
147 base::TimeTicks timebase, | 195 base::TimeTicks timebase, |
148 base::TimeDelta interval) { | 196 base::TimeDelta interval) { |
149 if (delegate_) | 197 if (delegate_) |
150 delegate_->OnVSyncParametersUpdated(timebase, interval); | 198 delegate_->OnVSyncParametersUpdated(timebase, interval); |
151 } | 199 } |
152 | 200 |
153 void SurfacesContextProvider::SetSwapBuffersCompletionCallback( | 201 void SurfacesContextProvider::SetSwapBuffersCompletionCallback( |
154 gl::GLSurface::SwapCompletionCallback callback) { | 202 gl::GLSurface::SwapCompletionCallback callback) { |
155 swap_buffers_completion_callback_ = callback; | 203 swap_buffers_completion_callback_ = callback; |
156 } | 204 } |
157 | 205 |
158 } // namespace ui | 206 } // namespace ui |
OLD | NEW |