Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/skia/ganesh_context.h" | 5 #include "mojo/skia/ganesh_context.h" |
| 6 | 6 |
| 7 #include "mojo/public/c/gpu/MGL/mgl.h" | 7 #include "mojo/public/c/gpu/MGL/mgl.h" |
| 8 #include "mojo/skia/gl_bindings_skia.h" | 8 #include "mojo/skia/gl_bindings_skia.h" |
| 9 #include "third_party/skia/include/gpu/gl/GrGLInterface.h" | 9 #include "third_party/skia/include/gpu/gl/GrGLInterface.h" |
| 10 | 10 |
| 11 namespace mojo { | 11 namespace mojo { |
| 12 namespace { | 12 namespace skia { |
| 13 | 13 |
| 14 // The limit of the number of GPU resources we hold in the GrContext's | 14 // The limit of the number of GPU resources we hold in the GrContext's |
| 15 // GPU cache. | 15 // GPU cache. |
| 16 const int kMaxGaneshResourceCacheCount = 2048; | 16 constexpr int kMaxGaneshResourceCacheCount = 2048; |
| 17 | 17 |
| 18 // The limit of the bytes allocated toward GPU resources in the GrContext's | 18 // The limit of the bytes allocated toward GPU resources in the GrContext's |
| 19 // GPU cache. | 19 // GPU cache. |
| 20 const size_t kMaxGaneshResourceCacheBytes = 96 * 1024 * 1024; | 20 constexpr size_t kMaxGaneshResourceCacheBytes = 96 * 1024 * 1024; |
| 21 } | |
| 22 | 21 |
| 23 GaneshContext::Scope::Scope(GaneshContext* context) | 22 GaneshContext::Scope::Scope(GaneshContext* context) : context_(context) { |
| 24 : previous_(gles2::GetGLContext()) { | 23 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
| |
| 25 auto gl = context->gl_context_->gl(); | 24 context_->EnterScope(); |
| 26 DCHECK(gl); | |
| 27 gles2::SetGLContext(gl); | |
| 28 DCHECK(gles2::GetGLContext()); | |
| 29 } | 25 } |
| 30 | 26 |
| 31 GaneshContext::Scope::~Scope() { | 27 GaneshContext::Scope::~Scope() { |
| 32 gles2::SetGLContext(previous_); | 28 context_->ExitScope(); |
| 33 } | 29 } |
| 34 | 30 |
| 35 GaneshContext::GaneshContext(base::WeakPtr<GLContext> gl_context) | 31 GaneshContext::GaneshContext(base::WeakPtr<GLContext> gl_context) |
| 36 : gl_context_(gl_context) { | 32 : gl_context_(gl_context) { |
| 37 DCHECK(gl_context_); | 33 DCHECK(gl_context_); |
| 38 gl_context_->AddObserver(this); | 34 gl_context_->AddObserver(this); |
| 35 | |
| 39 Scope scope(this); | 36 Scope scope(this); |
| 40 | 37 |
| 41 skia::RefPtr<GrGLInterface> interface = | 38 ::skia::RefPtr<GrGLInterface> interface = |
| 42 skia::AdoptRef(skia_bindings::CreateMojoSkiaGLBinding()); | 39 ::skia::AdoptRef(CreateMojoSkiaGLBinding()); |
| 43 DCHECK(interface); | 40 DCHECK(interface); |
| 44 | 41 |
| 45 context_ = skia::AdoptRef(GrContext::Create( | 42 gr_context_ = ::skia::AdoptRef(GrContext::Create( |
| 46 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get()))); | 43 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get()))); |
| 47 DCHECK(context_); | 44 DCHECK(gr_context_); |
| 48 context_->setResourceCacheLimits(kMaxGaneshResourceCacheCount, | 45 gr_context_->setResourceCacheLimits(kMaxGaneshResourceCacheCount, |
| 49 kMaxGaneshResourceCacheBytes); | 46 kMaxGaneshResourceCacheBytes); |
| 50 } | 47 } |
| 51 | 48 |
| 52 GaneshContext::~GaneshContext() { | 49 GaneshContext::~GaneshContext() { |
| 53 if (context_) { | 50 if (gl_context_) |
| 54 Scope scope(this); | |
| 55 context_.clear(); | |
| 56 } | |
| 57 if (gl_context_.get()) | |
| 58 gl_context_->RemoveObserver(this); | 51 gl_context_->RemoveObserver(this); |
| 52 | |
| 53 ReleaseContext(); | |
| 59 } | 54 } |
| 60 | 55 |
| 61 bool GaneshContext::InScope() const { | 56 bool GaneshContext::InScope() const { |
| 62 return gl_context_->IsCurrent(); | 57 return gl_context_->IsCurrent(); |
| 63 } | 58 } |
| 64 | 59 |
| 65 void GaneshContext::OnContextLost() { | 60 void GaneshContext::OnContextLost() { |
| 66 context_->abandonContext(); | 61 ReleaseContext(); |
| 67 context_.clear(); | 62 } |
| 63 | |
| 64 void GaneshContext::ReleaseContext() { | |
| 65 Scope(this); | |
| 66 gr_context_->abandonContext(); | |
| 67 gr_context_.clear(); | |
| 68 gl_context_.reset(); | 68 gl_context_.reset(); |
| 69 } | 69 } |
| 70 | 70 |
| 71 void GaneshContext::EnterScope() { | |
| 72 if (!scope_nest_count_) { | |
|
viettrungluu
2015/12/18 16:34:48
(jamesr or abarth should probably look at this, an
| |
| 73 previous_mgl_context_ = MGLGetCurrentContext(); | |
| 74 if (gl_context_) | |
| 75 gl_context_->MakeCurrent(); | |
| 76 | |
| 77 // Reset the Ganesh context when entering its scope in case the caller | |
| 78 // performed low-level GL operations which might interfere with Ganesh's | |
| 79 // state expectations. | |
| 80 if (gr_context_) | |
| 81 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.
| |
| 82 } | |
| 83 scope_nest_count_++; | |
| 84 DCHECK(scope_nest_count_); | |
| 85 } | |
| 86 | |
| 87 void GaneshContext::ExitScope() { | |
| 88 DCHECK(scope_nest_count_); | |
| 89 scope_nest_count_--; | |
| 90 if (!scope_nest_count_) { | |
| 91 // Flush the Ganesh context when exiting its scope. | |
| 92 if (gr_context_) | |
| 93 gr_context_->flush(); | |
| 94 | |
| 95 MGLMakeCurrent(previous_mgl_context_); | |
| 96 previous_mgl_context_ = MGL_NO_CONTEXT; | |
| 97 } | |
| 98 } | |
| 99 | |
| 100 } // namespace skia | |
| 71 } // namespace mojo | 101 } // namespace mojo |
| OLD | NEW |