| 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 | |
| 23 GaneshContext::Scope::Scope(GaneshContext* context) | |
| 24 : previous_(gles2::GetGLContext()) { | |
| 25 auto gl = context->gl_context_->gl(); | |
| 26 DCHECK(gl); | |
| 27 gles2::SetGLContext(gl); | |
| 28 DCHECK(gles2::GetGLContext()); | |
| 29 } | |
| 30 | |
| 31 GaneshContext::Scope::~Scope() { | |
| 32 gles2::SetGLContext(previous_); | |
| 33 } | |
| 34 | 21 |
| 35 GaneshContext::GaneshContext(base::WeakPtr<GLContext> gl_context) | 22 GaneshContext::GaneshContext(base::WeakPtr<GLContext> gl_context) |
| 36 : gl_context_(gl_context) { | 23 : gl_context_(gl_context) { |
| 37 DCHECK(gl_context_); | 24 DCHECK(gl_context_); |
| 38 gl_context_->AddObserver(this); | 25 gl_context_->AddObserver(this); |
| 26 |
| 39 Scope scope(this); | 27 Scope scope(this); |
| 40 | 28 |
| 41 skia::RefPtr<GrGLInterface> interface = | 29 ::skia::RefPtr<GrGLInterface> interface = |
| 42 skia::AdoptRef(skia_bindings::CreateMojoSkiaGLBinding()); | 30 ::skia::AdoptRef(CreateMojoSkiaGLBinding()); |
| 43 DCHECK(interface); | 31 DCHECK(interface); |
| 44 | 32 |
| 45 context_ = skia::AdoptRef(GrContext::Create( | 33 gr_context_ = ::skia::AdoptRef(GrContext::Create( |
| 46 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get()))); | 34 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get()))); |
| 47 DCHECK(context_); | 35 DCHECK(gr_context_); |
| 48 context_->setResourceCacheLimits(kMaxGaneshResourceCacheCount, | 36 |
| 49 kMaxGaneshResourceCacheBytes); | 37 gr_context_->setResourceCacheLimits(kMaxGaneshResourceCacheCount, |
| 38 kMaxGaneshResourceCacheBytes); |
| 50 } | 39 } |
| 51 | 40 |
| 52 GaneshContext::~GaneshContext() { | 41 GaneshContext::~GaneshContext() { |
| 53 if (context_) { | 42 if (gl_context_) |
| 54 Scope scope(this); | |
| 55 context_.clear(); | |
| 56 } | |
| 57 if (gl_context_.get()) | |
| 58 gl_context_->RemoveObserver(this); | 43 gl_context_->RemoveObserver(this); |
| 59 } | |
| 60 | 44 |
| 61 bool GaneshContext::InScope() const { | 45 ReleaseContext(); |
| 62 return gl_context_->IsCurrent(); | |
| 63 } | 46 } |
| 64 | 47 |
| 65 void GaneshContext::OnContextLost() { | 48 void GaneshContext::OnContextLost() { |
| 66 context_->abandonContext(); | 49 ReleaseContext(); |
| 67 context_.clear(); | 50 } |
| 51 |
| 52 void GaneshContext::ReleaseContext() { |
| 53 Scope(this); |
| 54 gr_context_->abandonContext(); |
| 55 gr_context_.clear(); |
| 68 gl_context_.reset(); | 56 gl_context_.reset(); |
| 69 } | 57 } |
| 70 | 58 |
| 59 void GaneshContext::EnterScope() { |
| 60 CHECK(!scope_entered_); |
| 61 scope_entered_ = true; |
| 62 |
| 63 if (gl_context_) { |
| 64 previous_mgl_context_ = MGLGetCurrentContext(); |
| 65 gl_context_->MakeCurrent(); |
| 66 |
| 67 // Reset the Ganesh context when entering its scope in case the caller |
| 68 // performed low-level GL operations which might interfere with Ganesh's |
| 69 // state expectations. |
| 70 if (gr_context_) |
| 71 gr_context_->resetContext(); |
| 72 } |
| 73 } |
| 74 |
| 75 void GaneshContext::ExitScope() { |
| 76 CHECK(scope_entered_); |
| 77 scope_entered_ = false; |
| 78 |
| 79 // Flush the Ganesh context when exiting its scope. |
| 80 if (gr_context_) |
| 81 gr_context_->flush(); |
| 82 |
| 83 MGLMakeCurrent(previous_mgl_context_); |
| 84 previous_mgl_context_ = MGL_NO_CONTEXT; |
| 85 } |
| 86 |
| 87 } // namespace skia |
| 71 } // namespace mojo | 88 } // namespace mojo |
| OLD | NEW |