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/debug/trace_event.h"
8 #include "base/lazy_instance.h"
9 #include "gpu/command_buffer/client/gles2_lib.h"
10 #include "third_party/skia/include/gpu/GrContext.h"
11 #include "third_party/skia/include/gpu/gl/GrGLInterface.h"
12 #include "webkit/common/gpu/webgraphicscontext3d_impl.h"
19 // Singleton used to initialize and terminate the gles2 library.
20 class GLES2Initializer {
22 GLES2Initializer() { gles2::Initialize(); }
24 ~GLES2Initializer() { gles2::Terminate(); }
27 DISALLOW_COPY_AND_ASSIGN(GLES2Initializer);
30 base::LazyInstance<GLES2Initializer> g_gles2_initializer =
31 LAZY_INSTANCE_INITIALIZER;
33 void BindWebGraphicsContext3DGLContextCallback(const GrGLInterface* interface) {
34 gles2::SetGLContext(reinterpret_cast<WebGraphicsContext3DImpl*>(
35 interface->fCallbackData)->GetGLInterface());
38 } // namespace anonymous
40 GrContextForWebGraphicsContext3D::GrContextForWebGraphicsContext3D(
41 WebGraphicsContext3DImpl* context3d) {
45 // Ensure the gles2 library is initialized first in a thread safe way.
46 g_gles2_initializer.Get();
47 gles2::SetGLContext(context3d->GetGLInterface());
48 skia::RefPtr<GrGLInterface> interface = skia::AdoptRef(
49 context3d->createGrGLInterface());
53 interface->fCallback = BindWebGraphicsContext3DGLContextCallback;
54 interface->fCallbackData =
55 reinterpret_cast<GrGLInterfaceCallbackData>(context3d);
57 gr_context_ = skia::AdoptRef(GrContext::Create(
59 reinterpret_cast<GrBackendContext>(interface.get())));
61 // The limit of the number of textures we hold in the GrContext's
62 // bitmap->texture cache.
63 static const int kMaxGaneshTextureCacheCount = 2048;
64 // The limit of the bytes allocated toward textures in the GrContext's
65 // bitmap->texture cache.
66 static const size_t kMaxGaneshTextureCacheBytes = 96 * 1024 * 1024;
68 gr_context_->setTextureCacheLimits(kMaxGaneshTextureCacheCount,
69 kMaxGaneshTextureCacheBytes);
73 GrContextForWebGraphicsContext3D::~GrContextForWebGraphicsContext3D() {
76 void GrContextForWebGraphicsContext3D::OnLostContext() {
78 gr_context_->contextDestroyed();
81 void GrContextForWebGraphicsContext3D::FreeGpuResources() {
83 TRACE_EVENT_INSTANT0("gpu", "GrContext::freeGpuResources", \
84 TRACE_EVENT_SCOPE_THREAD);
85 gr_context_->freeGpuResources();