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 #if GR_GL_PER_GL_FUNC_CALLBACK 18 reinterpret_cast<blink::WebGraphicsContext3D*>( 19 interface->fCallbackData)->makeContextCurrent(); 20 #endif 21 } 22 23 GrContextForWebGraphicsContext3D::GrContextForWebGraphicsContext3D( 24 blink::WebGraphicsContext3D* context3d) { 25 if (!context3d) 26 return; 27 28 skia::RefPtr<GrGLInterface> interface = skia::AdoptRef( 29 context3d->createGrGLInterface()); 30 if (!interface) 31 return; 32 33 #if GR_GL_PER_GL_FUNC_CALLBACK 34 interface->fCallback = BindWebGraphicsContext3DGLContextCallback; 35 interface->fCallbackData = 36 reinterpret_cast<GrGLInterfaceCallbackData>(context3d); 37 #endif 38 39 gr_context_ = skia::AdoptRef(GrContext::Create( 40 kOpenGL_GrBackend, 41 reinterpret_cast<GrBackendContext>(interface.get()))); 42 if (!gr_context_) 43 return; 44 45 bool nonzero_allocation = true; 46 SetMemoryLimit(nonzero_allocation); 47 } 48 49 GrContextForWebGraphicsContext3D::~GrContextForWebGraphicsContext3D() { 50 if (gr_context_) 51 gr_context_->contextDestroyed(); 52 } 53 54 void GrContextForWebGraphicsContext3D::SetMemoryLimit(bool nonzero_allocation) { 55 if (!gr_context_) 56 return; 57 58 if (nonzero_allocation) { 59 // The limit of the number of textures we hold in the GrContext's 60 // bitmap->texture cache. 61 static const int kMaxGaneshTextureCacheCount = 2048; 62 // The limit of the bytes allocated toward textures in the GrContext's 63 // bitmap->texture cache. 64 static const size_t kMaxGaneshTextureCacheBytes = 96 * 1024 * 1024; 65 66 gr_context_->setTextureCacheLimits( 67 kMaxGaneshTextureCacheCount, kMaxGaneshTextureCacheBytes); 68 } else { 69 TRACE_EVENT_INSTANT0("gpu", "GrContext::freeGpuResources", \ 70 TRACE_EVENT_SCOPE_THREAD); 71 gr_context_->freeGpuResources(); 72 gr_context_->setTextureCacheLimits(0, 0); 73 } 74 } 75 76 } // namespace gpu 77 } // namespace webkit 78