OLD | NEW |
(Empty) | |
| 1 // Copyright 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 "cc/grcontext_provider.h" |
| 6 |
| 7 #include "cc/resource_provider.h" |
| 8 #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3
D.h" |
| 9 #include "third_party/skia/include/gpu/GrContext.h" |
| 10 #include "third_party/skia/include/gpu/gl/GrGLInterface.h" |
| 11 |
| 12 namespace cc { |
| 13 |
| 14 GrContextProvider::GrContextProvider() : context3d_(NULL) {} |
| 15 |
| 16 GrContextProvider::~GrContextProvider() { |
| 17 if (gr_context_) |
| 18 gr_context_->contextDestroyed(); |
| 19 } |
| 20 |
| 21 static void BindWebGraphicsContext3DGLContextCallback( |
| 22 const GrGLInterface* interface) { |
| 23 reinterpret_cast<WebKit::WebGraphicsContext3D*>( |
| 24 interface->fCallbackData)->makeContextCurrent(); |
| 25 } |
| 26 |
| 27 void GrContextProvider::SetContext3d( |
| 28 WebKit::WebGraphicsContext3D* context3d) { |
| 29 if (context3d == context3d_) |
| 30 return; |
| 31 context3d_ = context3d; |
| 32 if (gr_context_) |
| 33 gr_context_->contextDestroyed(); |
| 34 gr_context_.clear(); |
| 35 |
| 36 if (!context3d_) |
| 37 return; |
| 38 |
| 39 skia::RefPtr<GrGLInterface> interface = skia::AdoptRef( |
| 40 context3d->createGrGLInterface()); |
| 41 if (!interface) |
| 42 return; |
| 43 |
| 44 interface->fCallback = BindWebGraphicsContext3DGLContextCallback; |
| 45 interface->fCallbackData = |
| 46 reinterpret_cast<GrGLInterfaceCallbackData>(context3d); |
| 47 |
| 48 gr_context_ = skia::AdoptRef(GrContext::Create( |
| 49 kOpenGL_Shaders_GrEngine, |
| 50 reinterpret_cast<GrPlatform3DContext>(interface.get()))); |
| 51 |
| 52 if (!gr_context_) |
| 53 return; |
| 54 |
| 55 // TODO(danakj): Register the callback with content/. |
| 56 // TODO(danakj): Talk to the GPU mem manager directly. |
| 57 context3d_->setMemoryAllocationChangedCallbackCHROMIUM(this); |
| 58 |
| 59 bool nonzero_allocation = true; |
| 60 SetGaneshContextMemoryLimit(nonzero_allocation); |
| 61 } |
| 62 |
| 63 void GrContextProvider::SetGaneshContextMemoryLimit(bool nonzero_allocation) { |
| 64 if (!gr_context_) |
| 65 return; |
| 66 |
| 67 if (nonzero_allocation) { |
| 68 // The limit of the number of textures we hold in the GrContext's |
| 69 // bitmap->texture cache. |
| 70 const int kMaxGaneshTextureCacheCount = 2048; |
| 71 // The limit of the bytes allocated toward textures in the GrContext's |
| 72 // bitmap->texture cache. |
| 73 const size_t kMaxGaneshTextureCacheBytes = 96 * 1024 * 1024; |
| 74 |
| 75 gr_context_->setTextureCacheLimits( |
| 76 kMaxGaneshTextureCacheCount, kMaxGaneshTextureCacheBytes); |
| 77 return; |
| 78 } |
| 79 gr_context_->freeGpuResources(); |
| 80 gr_context_->setTextureCacheLimits(0, 0); |
| 81 } |
| 82 |
| 83 // WebGraphicsMemoryAllocationChangedCallbackCHROMIUM implementation. |
| 84 void GrContextProvider::onMemoryAllocationChanged( |
| 85 WebKit::WebGraphicsMemoryAllocation allocation) { |
| 86 SetGaneshContextMemoryLimit(!!allocation.gpuResourceSizeInBytes); |
| 87 } |
| 88 |
| 89 GrContextProvider::ScopedContexts::ScopedContexts( |
| 90 GrContextProvider* grcontext_provider) |
| 91 : grcontext_provider_(grcontext_provider) { |
| 92 } |
| 93 |
| 94 GrContextProvider::ScopedContexts::~ScopedContexts() {} |
| 95 |
| 96 GrContextProvider::ScopedContextsFlushed::ScopedContextsFlushed( |
| 97 ResourceProvider* resource_provider, |
| 98 GrContextProvider* grcontext_provider) |
| 99 : ScopedContexts(grcontext_provider) { |
| 100 resource_provider->flush(); |
| 101 } |
| 102 |
| 103 GrContextProvider::ScopedContextsFlushed::~ScopedContextsFlushed() { |
| 104 // Flush gr context so that all the rendered stuff appears on the |
| 105 // texture. |
| 106 if (gr_context()) |
| 107 gr_context()->flush(); |
| 108 // Flush the GL context so rendering results from this context are |
| 109 // visible in the compositor's context. |
| 110 if (context3d()) |
| 111 context3d()->flush(); |
| 112 } |
| 113 |
| 114 } // namespace cc |
OLD | NEW |