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 "gpu/command_buffer/client/gles2_lib.h" | 7 #include "mojo/public/c/gpu/MGL/mgl.h" |
8 #include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.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 { |
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 const 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 const size_t kMaxGaneshResourceCacheBytes = 96 * 1024 * 1024; |
21 | |
22 void EnsureInitialized() { | |
23 static bool initialized; | |
24 if (initialized) | |
25 return; | |
26 gles2::Initialize(); | |
27 initialized = true; | |
28 } | |
29 } | 21 } |
30 | 22 |
31 GaneshContext::Scope::Scope(GaneshContext* context) | 23 GaneshContext::Scope::Scope(GaneshContext* context) |
32 : previous_(gles2::GetGLContext()) { | 24 : previous_(gles2::GetGLContext()) { |
33 auto gl = context->gl_context_->gl(); | 25 auto gl = context->gl_context_->gl(); |
34 DCHECK(gl); | 26 DCHECK(gl); |
35 gles2::SetGLContext(gl); | 27 gles2::SetGLContext(gl); |
36 DCHECK(gles2::GetGLContext()); | 28 DCHECK(gles2::GetGLContext()); |
37 } | 29 } |
38 | 30 |
39 GaneshContext::Scope::~Scope() { | 31 GaneshContext::Scope::~Scope() { |
40 gles2::SetGLContext(previous_); | 32 gles2::SetGLContext(previous_); |
41 } | 33 } |
42 | 34 |
43 GaneshContext::GaneshContext(base::WeakPtr<GLContext> gl_context) | 35 GaneshContext::GaneshContext(base::WeakPtr<GLContext> gl_context) |
44 : gl_context_(gl_context) { | 36 : gl_context_(gl_context) { |
45 EnsureInitialized(); | |
46 | |
47 DCHECK(gl_context_); | 37 DCHECK(gl_context_); |
48 gl_context_->AddObserver(this); | 38 gl_context_->AddObserver(this); |
49 Scope scope(this); | 39 Scope scope(this); |
50 | 40 |
51 skia::RefPtr<GrGLInterface> interface = | 41 skia::RefPtr<GrGLInterface> interface = |
52 skia::AdoptRef(skia_bindings::CreateCommandBufferSkiaGLBinding()); | 42 skia::AdoptRef(skia_bindings::CreateMojoSkiaGLBinding()); |
53 DCHECK(interface); | 43 DCHECK(interface); |
54 | 44 |
55 context_ = skia::AdoptRef(GrContext::Create( | 45 context_ = skia::AdoptRef(GrContext::Create( |
56 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get()))); | 46 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get()))); |
57 DCHECK(context_); | 47 DCHECK(context_); |
58 context_->setResourceCacheLimits(kMaxGaneshResourceCacheCount, | 48 context_->setResourceCacheLimits(kMaxGaneshResourceCacheCount, |
59 kMaxGaneshResourceCacheBytes); | 49 kMaxGaneshResourceCacheBytes); |
60 } | 50 } |
61 | 51 |
62 GaneshContext::~GaneshContext() { | 52 GaneshContext::~GaneshContext() { |
63 if (context_) { | 53 if (context_) { |
64 Scope scope(this); | 54 Scope scope(this); |
65 context_.clear(); | 55 context_.clear(); |
66 } | 56 } |
67 if (gl_context_.get()) | 57 if (gl_context_.get()) |
68 gl_context_->RemoveObserver(this); | 58 gl_context_->RemoveObserver(this); |
69 } | 59 } |
70 | 60 |
71 bool GaneshContext::InScope() const { | 61 bool GaneshContext::InScope() const { |
72 return gles2::GetGLContext() == gl_context_->gl(); | 62 return gl_context_->IsCurrent(); |
73 } | 63 } |
74 | 64 |
75 void GaneshContext::OnContextLost() { | 65 void GaneshContext::OnContextLost() { |
76 context_->abandonContext(); | 66 context_->abandonContext(); |
77 context_.clear(); | 67 context_.clear(); |
78 gl_context_.reset(); | 68 gl_context_.reset(); |
79 } | 69 } |
80 | 70 |
81 } // namespace mojo | 71 } // namespace mojo |
OLD | NEW |