OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "webkit/common/gpu/grcontext_for_webgraphicscontext3d.h" | |
6 | |
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" | |
13 | |
14 using gpu_blink::WebGraphicsContext3DImpl; | |
15 | |
16 namespace webkit { | |
17 namespace gpu { | |
18 | |
19 namespace { | |
20 | |
21 // Singleton used to initialize and terminate the gles2 library. | |
22 class GLES2Initializer { | |
23 public: | |
24 GLES2Initializer() { gles2::Initialize(); } | |
25 | |
26 ~GLES2Initializer() { gles2::Terminate(); } | |
27 | |
28 private: | |
29 DISALLOW_COPY_AND_ASSIGN(GLES2Initializer); | |
30 }; | |
31 | |
32 base::LazyInstance<GLES2Initializer> g_gles2_initializer = | |
33 LAZY_INSTANCE_INITIALIZER; | |
34 | |
35 void BindWebGraphicsContext3DGLContextCallback(const GrGLInterface* interface) { | |
36 gles2::SetGLContext(reinterpret_cast<WebGraphicsContext3DImpl*>( | |
37 interface->fCallbackData)->GetGLInterface()); | |
38 } | |
39 | |
40 } // namespace anonymous | |
41 | |
42 GrContextForWebGraphicsContext3D::GrContextForWebGraphicsContext3D( | |
43 WebGraphicsContext3DImpl* context3d) { | |
44 if (!context3d) | |
45 return; | |
46 | |
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; | |
54 | |
55 interface->fCallback = BindWebGraphicsContext3DGLContextCallback; | |
56 interface->fCallbackData = | |
57 reinterpret_cast<GrGLInterfaceCallbackData>(context3d); | |
58 | |
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; | |
69 | |
70 gr_context_->setResourceCacheLimits(kMaxGaneshResourceCacheCount, | |
71 kMaxGaneshResourceCacheBytes); | |
72 } | |
73 } | |
74 | |
75 GrContextForWebGraphicsContext3D::~GrContextForWebGraphicsContext3D() { | |
76 } | |
77 | |
78 void GrContextForWebGraphicsContext3D::OnLostContext() { | |
79 if (gr_context_) | |
80 gr_context_->abandonContext(); | |
81 } | |
82 | |
83 void GrContextForWebGraphicsContext3D::FreeGpuResources() { | |
84 if (gr_context_) { | |
85 TRACE_EVENT_INSTANT0("gpu", "GrContext::freeGpuResources", \ | |
86 TRACE_EVENT_SCOPE_THREAD); | |
87 gr_context_->freeGpuResources(); | |
88 } | |
89 } | |
90 | |
91 } // namespace gpu | |
92 } // namespace webkit | |
OLD | NEW |