Index: ui/compositor/offscreen_context.cc |
diff --git a/ui/compositor/offscreen_context.cc b/ui/compositor/offscreen_context.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c8e8089d63bcd0e6b1d1e5a3f470eb88908ba32d |
--- /dev/null |
+++ b/ui/compositor/offscreen_context.cc |
@@ -0,0 +1,93 @@ |
+// 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 "ui/compositor/offscreen_context.h" |
+ |
+#include "third_party/skia/include/gpu/GrContext.h" |
+#include "third_party/skia/include/gpu/gl/GrGLInterface.h" |
+#include "ui/compositor/compositor.h" |
+ |
+namespace ui { |
+ |
+OffscreenContext::OffscreenContext( |
+ ContextFactory* context_factory, |
+ OffscreenContextClient* client) |
+ : context_factory_(context_factory), |
+ client_(client) { |
+} |
+ |
+OffscreenContext::~OffscreenContext() { |
+ if (context3d_) |
+ context3d_->setMemoryAllocationChangedCallbackCHROMIUM(NULL); |
+ if (gr_context_) |
+ gr_context_->contextDestroyed(); |
+} |
+ |
+WebKit::WebGraphicsContext3D* OffscreenContext::Context3d() { |
+ if (context3d_) |
+ return context3d_.get(); |
+ |
+ context3d_.reset(context_factory_->CreateOffscreenContext()); |
+ if (context3d_ && |
+ (!context3d_->makeContextCurrent() || |
+ context3d_->getGraphicsResetStatusARB())) |
+ context3d_.reset(); |
+ |
+ DidCreateContext(!!context3d_); |
+ if (!context3d_) |
+ return NULL; |
+ |
+ context3d_->setContextLostCallback(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(); |
+ if (!context3d_) |
+ return NULL; |
+ |
+ skia::RefPtr<GrGLInterface> interface = skia::AdoptRef( |
+ context3d_->createGrGLInterface()); |
+ interface->fCallback = BindWebGraphicsContext3DGLContextCallback; |
+ interface->fCallbackData = |
+ reinterpret_cast<GrGLInterfaceCallbackData>(context3d_.get()); |
+ |
+ gr_context_ = skia::AdoptRef(GrContext::Create( |
+ kOpenGL_Shaders_GrEngine, |
+ reinterpret_cast<GrPlatform3DContext>(interface.get()))); |
+ if (!gr_context_) |
+ return NULL; |
+ |
+ gr_context_->setTextureCacheLimits( |
+ kMaxGaneshTextureCacheCount, kMaxGaneshTextureCacheBytes); |
+ return gr_context_.get(); |
+} |
+ |
+void OffscreenContext::DidCreateContext(bool success) { |
+ client_->DidCreateContext(this, success); |
+} |
+ |
+void OffscreenContext::onContextLost() { |
+ if (context3d_) { |
+ context3d_->setMemoryAllocationChangedCallbackCHROMIUM(NULL); |
+ } |
+ scoped_ptr<WebKit::WebGraphicsContext3D> context3d = context3d_.Pass(); |
+ |
+ skia::RefPtr<class GrContext> gr_context = gr_context_; |
+ if (gr_context_) { |
+ gr_context_->contextDestroyed(); |
+ gr_context_.clear(); |
+ } |
+ |
+ client_->DidLoseContext(this); |
+} |
+ |
+} // namespace ui |