Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / common / gpu / client / grcontext_for_webgraphicscontext3d.cc
blob970ec386067ce002b90bd7327adf12fa77f7bc0b
1 // Copyright (c) 2013 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/grcontext_for_webgraphicscontext3d.h"
7 #include "base/lazy_instance.h"
8 #include "base/trace_event/trace_event.h"
9 #include "gpu/blink/webgraphicscontext3d_impl.h"
10 #include "gpu/command_buffer/client/gles2_lib.h"
11 #include "third_party/skia/include/gpu/GrContext.h"
12 #include "third_party/skia/include/gpu/gl/GrGLInterface.h"
14 using gpu_blink::WebGraphicsContext3DImpl;
16 namespace content {
18 namespace {
20 // Singleton used to initialize and terminate the gles2 library.
21 class GLES2Initializer {
22 public:
23 GLES2Initializer() { gles2::Initialize(); }
25 ~GLES2Initializer() { gles2::Terminate(); }
27 private:
28 DISALLOW_COPY_AND_ASSIGN(GLES2Initializer);
31 base::LazyInstance<GLES2Initializer> g_gles2_initializer =
32 LAZY_INSTANCE_INITIALIZER;
34 void BindWebGraphicsContext3DGLContextCallback(const GrGLInterface* interface) {
35 gles2::SetGLContext(reinterpret_cast<WebGraphicsContext3DImpl*>(
36 interface->fCallbackData)->GetGLInterface());
39 } // namespace anonymous
41 GrContextForWebGraphicsContext3D::GrContextForWebGraphicsContext3D(
42 WebGraphicsContext3DImpl* context3d) {
43 if (!context3d)
44 return;
46 // Ensure the gles2 library is initialized first in a thread safe way.
47 g_gles2_initializer.Get();
48 gles2::SetGLContext(context3d->GetGLInterface());
49 skia::RefPtr<GrGLInterface> interface = skia::AdoptRef(
50 context3d->createGrGLInterface());
51 if (!interface)
52 return;
54 interface->fCallback = BindWebGraphicsContext3DGLContextCallback;
55 interface->fCallbackData =
56 reinterpret_cast<GrGLInterfaceCallbackData>(context3d);
58 gr_context_ = skia::AdoptRef(GrContext::Create(
59 kOpenGL_GrBackend,
60 reinterpret_cast<GrBackendContext>(interface.get())));
61 if (gr_context_) {
62 // The limit of the number of GPU resources we hold in the GrContext's
63 // GPU cache.
64 static const int kMaxGaneshResourceCacheCount = 2048;
65 // The limit of the bytes allocated toward GPU resources in the GrContext's
66 // GPU cache.
67 static const size_t kMaxGaneshResourceCacheBytes = 96 * 1024 * 1024;
69 gr_context_->setResourceCacheLimits(kMaxGaneshResourceCacheCount,
70 kMaxGaneshResourceCacheBytes);
74 GrContextForWebGraphicsContext3D::~GrContextForWebGraphicsContext3D() {
77 void GrContextForWebGraphicsContext3D::OnLostContext() {
78 if (gr_context_)
79 gr_context_->abandonContext();
82 void GrContextForWebGraphicsContext3D::FreeGpuResources() {
83 if (gr_context_) {
84 TRACE_EVENT_INSTANT0("gpu", "GrContext::freeGpuResources", \
85 TRACE_EVENT_SCOPE_THREAD);
86 gr_context_->freeGpuResources();
90 } // namespace content