OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "blimp/client/feature/compositor/blimp_context_provider.h" | 5 #include "blimp/client/feature/compositor/blimp_context_provider.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback_helpers.h" | 8 #include "base/callback_helpers.h" |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "gpu/command_buffer/client/gl_in_process_context.h" | 10 #include "gpu/command_buffer/client/gl_in_process_context.h" |
11 #include "gpu/command_buffer/client/gles2_implementation.h" | 11 #include "gpu/command_buffer/client/gles2_implementation.h" |
12 #include "gpu/command_buffer/client/gles2_lib.h" | 12 #include "gpu/command_buffer/client/gles2_lib.h" |
13 #include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h" | 13 #include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h" |
14 #include "third_party/skia/include/gpu/GrContext.h" | 14 #include "third_party/skia/include/gpu/GrContext.h" |
15 #include "third_party/skia/include/gpu/gl/GrGLInterface.h" | 15 #include "third_party/skia/include/gpu/gl/GrGLInterface.h" |
16 | 16 |
17 namespace blimp { | 17 namespace blimp { |
18 namespace client { | 18 namespace client { |
19 namespace { | |
20 | |
21 // Singleton used to initialize and terminate the gles2 library. | |
22 class GLES2Initializer { | |
23 public: | |
24 GLES2Initializer() { gles2::Initialize(); } | |
25 | |
26 ~GLES2Initializer() { gles2::Terminate(); } | |
27 | |
28 private: | |
29 DISALLOW_COPY_AND_ASSIGN(GLES2Initializer); | |
30 }; | |
31 | |
32 base::LazyInstance<GLES2Initializer> g_gles2_initializer = | |
33 LAZY_INSTANCE_INITIALIZER; | |
34 | |
35 static void BindGrContextCallback(const GrGLInterface* interface) { | |
36 BlimpContextProvider* context_provider = | |
37 reinterpret_cast<BlimpContextProvider*>(interface->fCallbackData); | |
38 | |
39 gles2::SetGLContext(context_provider->ContextGL()); | |
40 } | |
41 | |
42 } // namespace | |
43 | 19 |
44 // static | 20 // static |
45 scoped_refptr<BlimpContextProvider> BlimpContextProvider::Create( | 21 scoped_refptr<BlimpContextProvider> BlimpContextProvider::Create( |
46 gfx::AcceleratedWidget widget, | 22 gfx::AcceleratedWidget widget, |
47 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager) { | 23 gpu::GpuMemoryBufferManager* gpu_memory_buffer_manager) { |
48 return new BlimpContextProvider(widget, gpu_memory_buffer_manager); | 24 return new BlimpContextProvider(widget, gpu_memory_buffer_manager); |
49 } | 25 } |
50 | 26 |
51 BlimpContextProvider::BlimpContextProvider( | 27 BlimpContextProvider::BlimpContextProvider( |
52 gfx::AcceleratedWidget widget, | 28 gfx::AcceleratedWidget widget, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 DCHECK(context_thread_checker_.CalledOnValidThread()); | 80 DCHECK(context_thread_checker_.CalledOnValidThread()); |
105 return context_->GetImplementation(); | 81 return context_->GetImplementation(); |
106 } | 82 } |
107 | 83 |
108 class GrContext* BlimpContextProvider::GrContext() { | 84 class GrContext* BlimpContextProvider::GrContext() { |
109 DCHECK(context_thread_checker_.CalledOnValidThread()); | 85 DCHECK(context_thread_checker_.CalledOnValidThread()); |
110 | 86 |
111 if (gr_context_) | 87 if (gr_context_) |
112 return gr_context_.get(); | 88 return gr_context_.get(); |
113 | 89 |
114 // The GrGLInterface factory will make GL calls using the C GLES2 interface. | |
115 // Make sure the gles2 library is initialized first on exactly one thread. | |
116 g_gles2_initializer.Get(); | |
117 gles2::SetGLContext(ContextGL()); | |
118 | |
119 skia::RefPtr<GrGLInterface> interface = skia::AdoptRef(new GrGLInterface); | 90 skia::RefPtr<GrGLInterface> interface = skia::AdoptRef(new GrGLInterface); |
120 skia_bindings::InitCommandBufferSkiaGLBinding(interface.get()); | 91 skia_bindings::InitGLES2InterfaceBindings(interface.get(), ContextGL()); |
121 interface->fCallback = BindGrContextCallback; | |
122 interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(this); | |
123 | 92 |
124 gr_context_ = skia::AdoptRef(GrContext::Create( | 93 gr_context_ = skia::AdoptRef(GrContext::Create( |
125 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get()))); | 94 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get()))); |
126 | 95 |
127 return gr_context_.get(); | 96 return gr_context_.get(); |
128 } | 97 } |
129 | 98 |
130 void BlimpContextProvider::InvalidateGrContext(uint32_t state) { | 99 void BlimpContextProvider::InvalidateGrContext(uint32_t state) { |
131 DCHECK(context_thread_checker_.CalledOnValidThread()); | 100 DCHECK(context_thread_checker_.CalledOnValidThread()); |
132 | 101 |
(...skipping 25 matching lines...) Expand all Loading... |
158 void BlimpContextProvider::OnLostContext() { | 127 void BlimpContextProvider::OnLostContext() { |
159 DCHECK(context_thread_checker_.CalledOnValidThread()); | 128 DCHECK(context_thread_checker_.CalledOnValidThread()); |
160 if (!lost_context_callback_.is_null()) | 129 if (!lost_context_callback_.is_null()) |
161 base::ResetAndReturn(&lost_context_callback_).Run(); | 130 base::ResetAndReturn(&lost_context_callback_).Run(); |
162 if (gr_context_) | 131 if (gr_context_) |
163 gr_context_->abandonContext(); | 132 gr_context_->abandonContext(); |
164 } | 133 } |
165 | 134 |
166 } // namespace client | 135 } // namespace client |
167 } // namespace blimp | 136 } // namespace blimp |
OLD | NEW |