Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / common / gpu / client / webgraphicscontext3d_command_buffer_impl.cc
blob0c77e5bf611a30fa9592381e114cad09680e89b7
1 // Copyright (c) 2012 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.
5 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
7 #include "third_party/khronos/GLES2/gl2.h"
8 #ifndef GL_GLEXT_PROTOTYPES
9 #define GL_GLEXT_PROTOTYPES 1
10 #endif
11 #include "third_party/khronos/GLES2/gl2ext.h"
13 #include <algorithm>
14 #include <map>
16 #include "base/atomicops.h"
17 #include "base/bind.h"
18 #include "base/command_line.h"
19 #include "base/lazy_instance.h"
20 #include "base/logging.h"
21 #include "base/message_loop/message_loop.h"
22 #include "base/metrics/field_trial.h"
23 #include "base/metrics/histogram.h"
24 #include "base/profiler/scoped_tracker.h"
25 #include "base/trace_event/trace_event.h"
26 #include "content/common/gpu/client/gpu_channel_host.h"
27 #include "content/public/common/content_constants.h"
28 #include "content/public/common/content_switches.h"
29 #include "gpu/GLES2/gl2extchromium.h"
30 #include "gpu/command_buffer/client/gles2_cmd_helper.h"
31 #include "gpu/command_buffer/client/gles2_implementation.h"
32 #include "gpu/command_buffer/client/gles2_trace_implementation.h"
33 #include "gpu/command_buffer/client/transfer_buffer.h"
34 #include "gpu/command_buffer/common/constants.h"
35 #include "gpu/command_buffer/common/gpu_memory_allocation.h"
36 #include "gpu/command_buffer/common/mailbox.h"
37 #include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h"
38 #include "third_party/skia/include/core/SkTypes.h"
40 using blink::WGC3Denum;
42 namespace content {
44 namespace {
46 static base::LazyInstance<base::Lock>::Leaky
47 g_default_share_groups_lock = LAZY_INSTANCE_INITIALIZER;
49 typedef std::map<GpuChannelHost*,
50 scoped_refptr<WebGraphicsContext3DCommandBufferImpl::ShareGroup> >
51 ShareGroupMap;
52 static base::LazyInstance<ShareGroupMap> g_default_share_groups =
53 LAZY_INSTANCE_INITIALIZER;
55 scoped_refptr<WebGraphicsContext3DCommandBufferImpl::ShareGroup>
56 GetDefaultShareGroupForHost(GpuChannelHost* host) {
57 base::AutoLock lock(g_default_share_groups_lock.Get());
59 ShareGroupMap& share_groups = g_default_share_groups.Get();
60 ShareGroupMap::iterator it = share_groups.find(host);
61 if (it == share_groups.end()) {
62 scoped_refptr<WebGraphicsContext3DCommandBufferImpl::ShareGroup> group =
63 new WebGraphicsContext3DCommandBufferImpl::ShareGroup();
64 share_groups[host] = group;
65 return group;
67 return it->second;
70 } // namespace anonymous
72 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits::SharedMemoryLimits()
73 : command_buffer_size(kDefaultCommandBufferSize),
74 start_transfer_buffer_size(kDefaultStartTransferBufferSize),
75 min_transfer_buffer_size(kDefaultMinTransferBufferSize),
76 max_transfer_buffer_size(kDefaultMaxTransferBufferSize),
77 mapped_memory_reclaim_limit(gpu::gles2::GLES2Implementation::kNoLimit) {}
79 WebGraphicsContext3DCommandBufferImpl::ShareGroup::ShareGroup() {
82 WebGraphicsContext3DCommandBufferImpl::ShareGroup::~ShareGroup() {
83 DCHECK(contexts_.empty());
86 WebGraphicsContext3DCommandBufferImpl::WebGraphicsContext3DCommandBufferImpl(
87 int surface_id,
88 const GURL& active_url,
89 GpuChannelHost* host,
90 const Attributes& attributes,
91 bool lose_context_when_out_of_memory,
92 const SharedMemoryLimits& limits,
93 WebGraphicsContext3DCommandBufferImpl* share_context)
94 : lose_context_when_out_of_memory_(lose_context_when_out_of_memory),
95 attributes_(attributes),
96 visible_(false),
97 host_(host),
98 surface_id_(surface_id),
99 active_url_(active_url),
100 context_type_(CONTEXT_TYPE_UNKNOWN),
101 gpu_preference_(attributes.preferDiscreteGPU ? gfx::PreferDiscreteGpu
102 : gfx::PreferIntegratedGpu),
103 mem_limits_(limits),
104 weak_ptr_factory_(this) {
105 if (attributes_.webGL)
106 context_type_ = OFFSCREEN_CONTEXT_FOR_WEBGL;
107 if (share_context) {
108 DCHECK(!attributes_.shareResources);
109 share_group_ = share_context->share_group_;
110 } else {
111 share_group_ = attributes_.shareResources
112 ? GetDefaultShareGroupForHost(host)
113 : scoped_refptr<WebGraphicsContext3DCommandBufferImpl::ShareGroup>(
114 new ShareGroup());
118 WebGraphicsContext3DCommandBufferImpl::
119 ~WebGraphicsContext3DCommandBufferImpl() {
120 if (real_gl_) {
121 real_gl_->SetErrorMessageCallback(NULL);
124 Destroy();
127 bool WebGraphicsContext3DCommandBufferImpl::MaybeInitializeGL() {
128 if (initialized_)
129 return true;
131 if (initialize_failed_)
132 return false;
134 TRACE_EVENT0("gpu", "WebGfxCtx3DCmdBfrImpl::MaybeInitializeGL");
136 // TODO(vadimt): Remove ScopedTracker below once crbug.com/125248 is fixed.
137 tracked_objects::ScopedTracker tracking_profile(
138 FROM_HERE_WITH_EXPLICIT_FUNCTION(
139 "125248 WebGraphicsContext3DCommandBufferImpl::MaybeInitializeGL"));
141 if (!CreateContext(surface_id_ != 0)) {
142 Destroy();
144 initialize_failed_ = true;
145 return false;
148 command_buffer_->SetContextLostCallback(
149 base::Bind(&WebGraphicsContext3DCommandBufferImpl::OnContextLost,
150 weak_ptr_factory_.GetWeakPtr()));
152 command_buffer_->SetOnConsoleMessageCallback(
153 base::Bind(&WebGraphicsContext3DCommandBufferImpl::OnErrorMessage,
154 weak_ptr_factory_.GetWeakPtr()));
156 real_gl_->SetErrorMessageCallback(getErrorMessageCallback());
158 visible_ = true;
159 initialized_ = true;
160 return true;
163 bool WebGraphicsContext3DCommandBufferImpl::InitializeCommandBuffer(
164 bool onscreen, WebGraphicsContext3DCommandBufferImpl* share_context) {
165 if (!host_.get())
166 return false;
168 CommandBufferProxyImpl* share_group_command_buffer = NULL;
170 if (share_context) {
171 share_group_command_buffer = share_context->command_buffer_.get();
174 ::gpu::gles2::ContextCreationAttribHelper attribs_for_gles2;
175 ConvertAttributes(attributes_, &attribs_for_gles2);
176 attribs_for_gles2.lose_context_when_out_of_memory =
177 lose_context_when_out_of_memory_;
178 DCHECK(attribs_for_gles2.buffer_preserved);
179 std::vector<int32> attribs;
180 attribs_for_gles2.Serialize(&attribs);
182 // Create a proxy to a command buffer in the GPU process.
183 if (onscreen) {
184 command_buffer_.reset(host_->CreateViewCommandBuffer(
185 surface_id_,
186 share_group_command_buffer,
187 attribs,
188 active_url_,
189 gpu_preference_));
190 } else {
191 command_buffer_.reset(host_->CreateOffscreenCommandBuffer(
192 gfx::Size(1, 1),
193 share_group_command_buffer,
194 attribs,
195 active_url_,
196 gpu_preference_));
199 if (!command_buffer_) {
200 DLOG(ERROR) << "GpuChannelHost failed to create command buffer.";
201 UmaRecordContextInitFailed(context_type_);
202 return false;
205 DVLOG_IF(1, gpu::error::IsError(command_buffer_->GetLastError()))
206 << "Context dead on arrival. Last error: "
207 << command_buffer_->GetLastError();
208 // Initialize the command buffer.
209 bool result = command_buffer_->Initialize();
210 LOG_IF(ERROR, !result) << "CommandBufferProxy::Initialize failed.";
211 if (!result)
212 UmaRecordContextInitFailed(context_type_);
213 return result;
216 bool WebGraphicsContext3DCommandBufferImpl::CreateContext(bool onscreen) {
217 TRACE_EVENT0("gpu", "WebGfxCtx3DCmdBfrImpl::CreateContext");
218 scoped_refptr<gpu::gles2::ShareGroup> gles2_share_group;
220 scoped_ptr<base::AutoLock> share_group_lock;
221 bool add_to_share_group = false;
222 if (!command_buffer_) {
223 WebGraphicsContext3DCommandBufferImpl* share_context = NULL;
225 share_group_lock.reset(new base::AutoLock(share_group_->lock()));
226 share_context = share_group_->GetAnyContextLocked();
228 if (!InitializeCommandBuffer(onscreen, share_context)) {
229 LOG(ERROR) << "Failed to initialize command buffer.";
230 return false;
233 if (share_context)
234 gles2_share_group = share_context->GetImplementation()->share_group();
236 add_to_share_group = true;
239 // Create the GLES2 helper, which writes the command buffer protocol.
240 gles2_helper_.reset(new gpu::gles2::GLES2CmdHelper(command_buffer_.get()));
241 if (!gles2_helper_->Initialize(mem_limits_.command_buffer_size)) {
242 LOG(ERROR) << "Failed to initialize GLES2CmdHelper.";
243 return false;
246 if (attributes_.noAutomaticFlushes)
247 gles2_helper_->SetAutomaticFlushes(false);
248 // Create a transfer buffer used to copy resources between the renderer
249 // process and the GPU process.
250 transfer_buffer_ .reset(new gpu::TransferBuffer(gles2_helper_.get()));
252 DCHECK(host_.get());
254 // Create the object exposing the OpenGL API.
255 const bool bind_generates_resources = false;
256 const bool support_client_side_arrays = false;
258 real_gl_.reset(
259 new gpu::gles2::GLES2Implementation(gles2_helper_.get(),
260 gles2_share_group.get(),
261 transfer_buffer_.get(),
262 bind_generates_resources,
263 lose_context_when_out_of_memory_,
264 support_client_side_arrays,
265 command_buffer_.get()));
266 setGLInterface(real_gl_.get());
268 if (!real_gl_->Initialize(
269 mem_limits_.start_transfer_buffer_size,
270 mem_limits_.min_transfer_buffer_size,
271 mem_limits_.max_transfer_buffer_size,
272 mem_limits_.mapped_memory_reclaim_limit)) {
273 LOG(ERROR) << "Failed to initialize GLES2Implementation.";
274 return false;
277 if (add_to_share_group)
278 share_group_->AddContextLocked(this);
280 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
281 switches::kEnableGpuClientTracing)) {
282 trace_gl_.reset(new gpu::gles2::GLES2TraceImplementation(GetGLInterface()));
283 setGLInterface(trace_gl_.get());
285 return true;
288 bool WebGraphicsContext3DCommandBufferImpl::InitializeOnCurrentThread() {
289 if (!MaybeInitializeGL()) {
290 DLOG(ERROR) << "Failed to initialize context.";
291 return false;
293 if (gpu::error::IsError(command_buffer_->GetLastError())) {
294 LOG(ERROR) << "Context dead on arrival. Last error: "
295 << command_buffer_->GetLastError();
296 return false;
299 return true;
302 void WebGraphicsContext3DCommandBufferImpl::Destroy() {
303 share_group_->RemoveContext(this);
305 gpu::gles2::GLES2Interface* gl = GetGLInterface();
306 if (gl) {
307 // First flush the context to ensure that any pending frees of resources
308 // are completed. Otherwise, if this context is part of a share group,
309 // those resources might leak. Also, any remaining side effects of commands
310 // issued on this context might not be visible to other contexts in the
311 // share group.
312 gl->Flush();
313 setGLInterface(NULL);
316 trace_gl_.reset();
317 real_gl_.reset();
318 transfer_buffer_.reset();
319 gles2_helper_.reset();
320 real_gl_.reset();
322 if (command_buffer_) {
323 if (host_.get())
324 host_->DestroyCommandBuffer(command_buffer_.release());
325 command_buffer_.reset();
328 host_ = NULL;
331 gpu::ContextSupport*
332 WebGraphicsContext3DCommandBufferImpl::GetContextSupport() {
333 return real_gl_.get();
336 bool WebGraphicsContext3DCommandBufferImpl::IsCommandBufferContextLost() {
337 // If the channel shut down unexpectedly, let that supersede the
338 // command buffer's state.
339 if (host_.get() && host_->IsLost())
340 return true;
341 gpu::CommandBuffer::State state = command_buffer_->GetLastState();
342 return gpu::error::IsError(state.error);
345 // static
346 WebGraphicsContext3DCommandBufferImpl*
347 WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
348 GpuChannelHost* host,
349 const WebGraphicsContext3D::Attributes& attributes,
350 bool lose_context_when_out_of_memory,
351 const GURL& active_url,
352 const SharedMemoryLimits& limits,
353 WebGraphicsContext3DCommandBufferImpl* share_context) {
354 if (!host)
355 return NULL;
357 if (share_context && share_context->IsCommandBufferContextLost())
358 return NULL;
360 return new WebGraphicsContext3DCommandBufferImpl(
362 active_url,
363 host,
364 attributes,
365 lose_context_when_out_of_memory,
366 limits,
367 share_context);
370 void WebGraphicsContext3DCommandBufferImpl::OnContextLost() {
371 if (context_lost_callback_)
372 context_lost_callback_->onContextLost();
374 share_group_->RemoveAllContexts();
376 DCHECK(host_.get());
378 base::AutoLock lock(g_default_share_groups_lock.Get());
379 g_default_share_groups.Get().erase(host_.get());
382 gpu::CommandBuffer::State state = command_buffer_->GetLastState();
383 UmaRecordContextLost(context_type_, state.error, state.context_lost_reason);
386 } // namespace content