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
;
21 // Singleton used to initialize and terminate the gles2 library.
22 class GLES2Initializer
{
24 GLES2Initializer() { gles2::Initialize(); }
26 ~GLES2Initializer() { gles2::Terminate(); }
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
) {
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());
55 interface
->fCallback
= BindWebGraphicsContext3DGLContextCallback
;
56 interface
->fCallbackData
=
57 reinterpret_cast<GrGLInterfaceCallbackData
>(context3d
);
59 gr_context_
= skia::AdoptRef(GrContext::Create(
61 reinterpret_cast<GrBackendContext
>(interface
.get())));
63 // The limit of the number of GPU resources we hold in the GrContext's
65 static const int kMaxGaneshResourceCacheCount
= 2048;
66 // The limit of the bytes allocated toward GPU resources in the GrContext's
68 static const size_t kMaxGaneshResourceCacheBytes
= 96 * 1024 * 1024;
70 gr_context_
->setResourceCacheLimits(kMaxGaneshResourceCacheCount
,
71 kMaxGaneshResourceCacheBytes
);
75 GrContextForWebGraphicsContext3D::~GrContextForWebGraphicsContext3D() {
78 void GrContextForWebGraphicsContext3D::OnLostContext() {
80 gr_context_
->abandonContext();
83 void GrContextForWebGraphicsContext3D::FreeGpuResources() {
85 TRACE_EVENT_INSTANT0("gpu", "GrContext::freeGpuResources", \
86 TRACE_EVENT_SCOPE_THREAD
);
87 gr_context_
->freeGpuResources();