Chromium Code Reviews| Index: mojo/skia/ganesh_context.cc |
| diff --git a/mojo/skia/ganesh_context.cc b/mojo/skia/ganesh_context.cc |
| index 76b7c597332914472b8d6a52ecb903a2a0fb7e80..8a4057c187a77a400d5178db2d16c77b40ab7aa1 100644 |
| --- a/mojo/skia/ganesh_context.cc |
| +++ b/mojo/skia/ganesh_context.cc |
| @@ -9,53 +9,48 @@ |
| #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; |
| -} |
| +constexpr 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(GaneshContext* context) : context_(context) { |
| + DCHECK(context_); |
|
viettrungluu
2015/12/18 16:34:48
I wonder if we shouldn't just inline the definitio
jeffbrown
2015/12/18 17:48:24
Yeah that's a good idea. Now that it's just calli
|
| + context_->EnterScope(); |
| } |
| GaneshContext::Scope::~Scope() { |
| - gles2::SetGLContext(previous_); |
| + context_->ExitScope(); |
| } |
| 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); |
| + |
| + ReleaseContext(); |
| } |
| bool GaneshContext::InScope() const { |
| @@ -63,9 +58,44 @@ bool GaneshContext::InScope() const { |
| } |
| 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() { |
| + if (!scope_nest_count_) { |
|
viettrungluu
2015/12/18 16:34:48
(jamesr or abarth should probably look at this, an
|
| + previous_mgl_context_ = MGLGetCurrentContext(); |
| + if (gl_context_) |
| + 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(); |
|
viettrungluu
2015/12/18 16:34:48
If the intent is to allow scopes from different Ga
jeffbrown
2015/12/18 17:48:24
FWIW, I'm with you on recursive mutexes (yuck). O
viettrungluu
2015/12/18 18:05:41
I think my tendency would be to go with something
jeffbrown
2015/12/18 18:12:57
Sounds good. I'll do that.
|
| + } |
| + scope_nest_count_++; |
| + DCHECK(scope_nest_count_); |
| +} |
| + |
| +void GaneshContext::ExitScope() { |
| + DCHECK(scope_nest_count_); |
| + scope_nest_count_--; |
| + if (!scope_nest_count_) { |
| + // 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 |