Index: content/common/gpu/client/offscreen_context.cc |
diff --git a/content/common/gpu/client/offscreen_context.cc b/content/common/gpu/client/offscreen_context.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5ab693edf6dbe1d721f1ed44e50bf8572e89a5f8 |
--- /dev/null |
+++ b/content/common/gpu/client/offscreen_context.cc |
@@ -0,0 +1,113 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/common/gpu/client/offscreen_context.h" |
+ |
+#include "third_party/skia/include/gpu/GrContext.h" |
+#include "third_party/skia/include/gpu/gl/GrGLInterface.h" |
+ |
+namespace content { |
+ |
+OffscreenContext::OffscreenContext(OffscreenContextClient* client) |
+ : client_(client) { |
+} |
+ |
+OffscreenContext::~OffscreenContext() { |
+ if (gr_context_) |
+ gr_context_->contextDestroyed(); |
+ if (context3d_) { |
+ context3d_->setContextLostCallback(NULL); |
+ context3d_->setMemoryAllocationChangedCallbackCHROMIUM(NULL); |
+ } |
+} |
+ |
+WebKit::WebGraphicsContext3D* OffscreenContext::Context3d() { |
+ if (context3d_) |
+ return context3d_.get(); |
+ |
+ context3d_.reset(client_->CreateOffscreenContext()); |
+ if (context3d_ && |
+ (!context3d_->makeContextCurrent() || |
+ context3d_->getGraphicsResetStatusARB())) |
+ context3d_.reset(); |
+ |
+ client_->DidCreateContext(this, !!context3d_); |
+ if (!context3d_) |
+ return NULL; |
+ |
+ context3d_->setContextLostCallback(this); |
+ // TODO(danakj): Talk to the GPU mem manager directly. |
+ context3d_->setMemoryAllocationChangedCallbackCHROMIUM(this); |
+ return context3d_.get(); |
+} |
+ |
+static void BindWebGraphicsContext3DGLContextCallback( |
+ const GrGLInterface* interface) { |
+ reinterpret_cast<WebKit::WebGraphicsContext3D*>( |
+ interface->fCallbackData)->makeContextCurrent(); |
+} |
+ |
+GrContext* OffscreenContext::GrContext() { |
+ if (gr_context_) |
+ return gr_context_.get(); |
+ |
+ WebKit::WebGraphicsContext3D* context3d = Context3d(); |
+ if (!context3d) |
+ return NULL; |
+ |
+ skia::RefPtr<GrGLInterface> interface = skia::AdoptRef( |
+ context3d->createGrGLInterface()); |
+ interface->fCallback = BindWebGraphicsContext3DGLContextCallback; |
+ interface->fCallbackData = |
+ reinterpret_cast<GrGLInterfaceCallbackData>(context3d); |
+ |
+ gr_context_ = skia::AdoptRef(GrContext::Create( |
+ kOpenGL_Shaders_GrEngine, |
+ reinterpret_cast<GrPlatform3DContext>(interface.get()))); |
+ if (!gr_context_) |
+ return NULL; |
+ |
+ bool nonzero_allocation = true; |
+ SetGaneshContextMemoryLimit(nonzero_allocation); |
+ return gr_context_.get(); |
+} |
+ |
+void OffscreenContext::SetGaneshContextMemoryLimit( |
+ bool nonzero_allocation) { |
+ if (!gr_context_) |
+ return; |
+ |
+ if (nonzero_allocation) { |
+ gr_context_->setTextureCacheLimits( |
+ kMaxGaneshTextureCacheCount, kMaxGaneshTextureCacheBytes); |
+ return; |
+ } |
+ gr_context_->freeGpuResources(); |
+ gr_context_->setTextureCacheLimits(0, 0); |
+} |
+ |
+void OffscreenContext::onContextLost() { |
+ // Save the contexts until the notifications are done, but requests for a |
+ // context should create a new one. |
+ scoped_ptr<WebKit::WebGraphicsContext3D> context3d = |
+ context3d_.Pass(); |
+ if (context3d) |
+ context3d->setMemoryAllocationChangedCallbackCHROMIUM(NULL); |
+ |
+ skia::RefPtr<class GrContext> gr_context = gr_context_; |
+ if (gr_context_) { |
+ gr_context_->contextDestroyed(); |
+ gr_context_.clear(); |
+ } |
+ |
+ client_->DidLoseContext(this); |
+} |
+ |
+// WebGraphicsMemoryAllocationChangedCallbackCHROMIUM implementation. |
+void OffscreenContext::onMemoryAllocationChanged( |
+ WebKit::WebGraphicsMemoryAllocation allocation) { |
+ SetGaneshContextMemoryLimit(!!allocation.gpuResourceSizeInBytes); |
+} |
+ |
+} // namespace content |