Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / webkit / common / gpu / grcontext_for_webgraphicscontext3d.cc
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/debug/trace_event.h"
8 #include "third_party/WebKit/public/platform/WebGraphicsContext3D.h"
9 #include "third_party/skia/include/gpu/GrContext.h"
10 #include "third_party/skia/include/gpu/gl/GrGLInterface.h"
11
12 namespace webkit {
13 namespace gpu {
14
15 static void BindWebGraphicsContext3DGLContextCallback(
16     const GrGLInterface* interface) {
17   reinterpret_cast<blink::WebGraphicsContext3D*>(
18       interface->fCallbackData)->makeContextCurrent();
19 }
20
21 GrContextForWebGraphicsContext3D::GrContextForWebGraphicsContext3D(
22     blink::WebGraphicsContext3D* context3d) {
23   if (!context3d)
24     return;
25
26   skia::RefPtr<GrGLInterface> interface = skia::AdoptRef(
27       context3d->createGrGLInterface());
28   if (!interface)
29     return;
30
31   interface->fCallback = BindWebGraphicsContext3DGLContextCallback;
32   interface->fCallbackData =
33       reinterpret_cast<GrGLInterfaceCallbackData>(context3d);
34
35   gr_context_ = skia::AdoptRef(GrContext::Create(
36       kOpenGL_GrBackend,
37       reinterpret_cast<GrBackendContext>(interface.get())));
38   if (gr_context_) {
39     // The limit of the number of textures we hold in the GrContext's
40     // bitmap->texture cache.
41     static const int kMaxGaneshTextureCacheCount = 2048;
42     // The limit of the bytes allocated toward textures in the GrContext's
43     // bitmap->texture cache.
44     static const size_t kMaxGaneshTextureCacheBytes = 96 * 1024 * 1024;
45
46     gr_context_->setTextureCacheLimits(kMaxGaneshTextureCacheCount,
47                                        kMaxGaneshTextureCacheBytes);
48   }
49 }
50
51 GrContextForWebGraphicsContext3D::~GrContextForWebGraphicsContext3D() {
52 }
53
54 void GrContextForWebGraphicsContext3D::OnLostContext() {
55   if (gr_context_)
56     gr_context_->contextDestroyed();
57 }
58
59 void GrContextForWebGraphicsContext3D::FreeGpuResources() {
60   if (gr_context_) {
61     TRACE_EVENT_INSTANT0("gpu", "GrContext::freeGpuResources", \
62         TRACE_EVENT_SCOPE_THREAD);
63     gr_context_->freeGpuResources();
64   }
65 }
66
67 }  // namespace gpu
68 }  // namespace webkit