Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1761)

Unified Diff: mojo/skia/ganesh_context.cc

Issue 1534033002: Improve Ganesh helpers. (Closed) Base URL: git@github.com:domokit/mojo.git@moz-8
Patch Set: rebase Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/skia/ganesh_context.cc
diff --git a/mojo/skia/ganesh_context.cc b/mojo/skia/ganesh_context.cc
index 76b7c597332914472b8d6a52ecb903a2a0fb7e80..735a902ca492579792fb7271fad22466d7bd7555 100644
--- a/mojo/skia/ganesh_context.cc
+++ b/mojo/skia/ganesh_context.cc
@@ -9,63 +9,80 @@
#include "third_party/skia/include/gpu/gl/GrGLInterface.h"
namespace mojo {
-namespace {
+namespace skia {
// The limit of the number of GPU resources we hold in the GrContext's
// GPU cache.
-const int kMaxGaneshResourceCacheCount = 2048;
+constexpr int kMaxGaneshResourceCacheCount = 2048;
// The limit of the bytes allocated toward GPU resources in the GrContext's
// GPU cache.
-const size_t kMaxGaneshResourceCacheBytes = 96 * 1024 * 1024;
-}
-
-GaneshContext::Scope::Scope(GaneshContext* context)
- : previous_(gles2::GetGLContext()) {
- auto gl = context->gl_context_->gl();
- DCHECK(gl);
- gles2::SetGLContext(gl);
- DCHECK(gles2::GetGLContext());
-}
-
-GaneshContext::Scope::~Scope() {
- gles2::SetGLContext(previous_);
-}
+constexpr size_t kMaxGaneshResourceCacheBytes = 96 * 1024 * 1024;
GaneshContext::GaneshContext(base::WeakPtr<GLContext> gl_context)
: gl_context_(gl_context) {
DCHECK(gl_context_);
gl_context_->AddObserver(this);
+
Scope scope(this);
- skia::RefPtr<GrGLInterface> interface =
- skia::AdoptRef(skia_bindings::CreateMojoSkiaGLBinding());
+ ::skia::RefPtr<GrGLInterface> interface =
+ ::skia::AdoptRef(CreateMojoSkiaGLBinding());
DCHECK(interface);
- context_ = skia::AdoptRef(GrContext::Create(
+ gr_context_ = ::skia::AdoptRef(GrContext::Create(
kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get())));
- DCHECK(context_);
- context_->setResourceCacheLimits(kMaxGaneshResourceCacheCount,
- kMaxGaneshResourceCacheBytes);
+ DCHECK(gr_context_);
+
+ gr_context_->setResourceCacheLimits(kMaxGaneshResourceCacheCount,
+ kMaxGaneshResourceCacheBytes);
}
GaneshContext::~GaneshContext() {
- if (context_) {
- Scope scope(this);
- context_.clear();
- }
- if (gl_context_.get())
+ if (gl_context_)
gl_context_->RemoveObserver(this);
-}
-bool GaneshContext::InScope() const {
- return gl_context_->IsCurrent();
+ ReleaseContext();
}
void GaneshContext::OnContextLost() {
- context_->abandonContext();
- context_.clear();
+ ReleaseContext();
+}
+
+void GaneshContext::ReleaseContext() {
+ Scope(this);
+ gr_context_->abandonContext();
+ gr_context_.clear();
gl_context_.reset();
}
+void GaneshContext::EnterScope() {
+ CHECK(!scope_entered_);
+ scope_entered_ = true;
+
+ if (gl_context_) {
+ previous_mgl_context_ = MGLGetCurrentContext();
+ gl_context_->MakeCurrent();
+
+ // Reset the Ganesh context when entering its scope in case the caller
+ // performed low-level GL operations which might interfere with Ganesh's
+ // state expectations.
+ if (gr_context_)
+ gr_context_->resetContext();
+ }
+}
+
+void GaneshContext::ExitScope() {
+ CHECK(scope_entered_);
+ scope_entered_ = false;
+
+ // Flush the Ganesh context when exiting its scope.
+ if (gr_context_)
+ gr_context_->flush();
+
+ MGLMakeCurrent(previous_mgl_context_);
+ previous_mgl_context_ = MGL_NO_CONTEXT;
+}
+
+} // namespace skia
} // namespace mojo

Powered by Google App Engine
This is Rietveld 408576698