Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / webkit / common / gpu / grcontext_for_webgraphicscontext3d.cc
blobdb313a1710a775669e26635da96ad6a2324ce0c7
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 "webkit/common/gpu/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 webkit {
17 namespace gpu {
19 namespace {
21 // Singleton used to initialize and terminate the gles2 library.
22 class GLES2Initializer {
23 public:
24 GLES2Initializer() { gles2::Initialize(); }
26 ~GLES2Initializer() { gles2::Terminate(); }
28 private:
29 DISALLOW_COPY_AND_ASSIGN(GLES2Initializer);
32 base::LazyInstance<GLES2Initializer> g_gles2_initializer =
33 LAZY_INSTANCE_INITIALIZER;
35 void BindWebGraphicsContext3DGLContextCallback(const GrGLInterface* interface) {
36 gles2::SetGLContext(reinterpret_cast<WebGraphicsContext3DImpl*>(
37 interface->fCallbackData)->GetGLInterface());
40 } // namespace anonymous
42 GrContextForWebGraphicsContext3D::GrContextForWebGraphicsContext3D(
43 WebGraphicsContext3DImpl* context3d) {
44 if (!context3d)
45 return;
47 // Ensure the gles2 library is initialized first in a thread safe way.
48 g_gles2_initializer.Get();
49 gles2::SetGLContext(context3d->GetGLInterface());
50 skia::RefPtr<GrGLInterface> interface = skia::AdoptRef(
51 context3d->createGrGLInterface());
52 if (!interface)
53 return;
55 interface->fCallback = BindWebGraphicsContext3DGLContextCallback;
56 interface->fCallbackData =
57 reinterpret_cast<GrGLInterfaceCallbackData>(context3d);
59 gr_context_ = skia::AdoptRef(GrContext::Create(
60 kOpenGL_GrBackend,
61 reinterpret_cast<GrBackendContext>(interface.get())));
62 if (gr_context_) {
63 // The limit of the number of GPU resources we hold in the GrContext's
64 // GPU cache.
65 static const int kMaxGaneshResourceCacheCount = 2048;
66 // The limit of the bytes allocated toward GPU resources in the GrContext's
67 // GPU cache.
68 static const size_t kMaxGaneshResourceCacheBytes = 96 * 1024 * 1024;
70 gr_context_->setResourceCacheLimits(kMaxGaneshResourceCacheCount,
71 kMaxGaneshResourceCacheBytes);
75 GrContextForWebGraphicsContext3D::~GrContextForWebGraphicsContext3D() {
78 void GrContextForWebGraphicsContext3D::OnLostContext() {
79 if (gr_context_)
80 gr_context_->abandonContext();
83 void GrContextForWebGraphicsContext3D::FreeGpuResources() {
84 if (gr_context_) {
85 TRACE_EVENT_INSTANT0("gpu", "GrContext::freeGpuResources", \
86 TRACE_EVENT_SCOPE_THREAD);
87 gr_context_->freeGpuResources();
91 } // namespace gpu
92 } // namespace webkit