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 "android_webview/browser/aw_render_thread_context_provider.h" | 5 #include "android_webview/browser/aw_render_thread_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 "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
11 #include "cc/output/managed_memory_policy.h" | 11 #include "cc/output/managed_memory_policy.h" |
12 #include "gpu/blink/webgraphicscontext3d_impl.h" | 12 #include "gpu/blink/webgraphicscontext3d_impl.h" |
13 #include "gpu/command_buffer/client/gl_in_process_context.h" | 13 #include "gpu/command_buffer/client/gl_in_process_context.h" |
14 #include "gpu/command_buffer/client/gles2_implementation.h" | 14 #include "gpu/command_buffer/client/gles2_implementation.h" |
15 #include "gpu/command_buffer/client/gles2_lib.h" | 15 #include "gpu/command_buffer/client/gles2_lib.h" |
16 #include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h" | 16 #include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h" |
17 #include "third_party/skia/include/gpu/GrContext.h" | 17 #include "third_party/skia/include/gpu/GrContext.h" |
18 #include "third_party/skia/include/gpu/gl/GrGLInterface.h" | 18 #include "third_party/skia/include/gpu/gl/GrGLInterface.h" |
19 | 19 |
20 namespace android_webview { | 20 namespace android_webview { |
21 | 21 |
22 namespace { | |
23 | |
24 // Singleton used to initialize and terminate the gles2 library. | |
25 class GLES2Initializer { | |
26 public: | |
27 GLES2Initializer() { gles2::Initialize(); } | |
28 | |
29 ~GLES2Initializer() { gles2::Terminate(); } | |
30 | |
31 private: | |
32 DISALLOW_COPY_AND_ASSIGN(GLES2Initializer); | |
33 }; | |
34 | |
35 base::LazyInstance<GLES2Initializer> g_gles2_initializer = | |
36 LAZY_INSTANCE_INITIALIZER; | |
37 | |
38 } // namespace | |
39 | |
40 // static | 22 // static |
41 scoped_refptr<AwRenderThreadContextProvider> | 23 scoped_refptr<AwRenderThreadContextProvider> |
42 AwRenderThreadContextProvider::Create( | 24 AwRenderThreadContextProvider::Create( |
43 scoped_refptr<gfx::GLSurface> surface, | 25 scoped_refptr<gfx::GLSurface> surface, |
44 scoped_refptr<gpu::InProcessCommandBuffer::Service> service) { | 26 scoped_refptr<gpu::InProcessCommandBuffer::Service> service) { |
45 return new AwRenderThreadContextProvider(surface, service); | 27 return new AwRenderThreadContextProvider(surface, service); |
46 } | 28 } |
47 | 29 |
48 AwRenderThreadContextProvider::AwRenderThreadContextProvider( | 30 AwRenderThreadContextProvider::AwRenderThreadContextProvider( |
49 scoped_refptr<gfx::GLSurface> surface, | 31 scoped_refptr<gfx::GLSurface> surface, |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 | 86 |
105 return context_->GetImplementation(); | 87 return context_->GetImplementation(); |
106 } | 88 } |
107 | 89 |
108 gpu::ContextSupport* AwRenderThreadContextProvider::ContextSupport() { | 90 gpu::ContextSupport* AwRenderThreadContextProvider::ContextSupport() { |
109 DCHECK(main_thread_checker_.CalledOnValidThread()); | 91 DCHECK(main_thread_checker_.CalledOnValidThread()); |
110 | 92 |
111 return context_->GetImplementation(); | 93 return context_->GetImplementation(); |
112 } | 94 } |
113 | 95 |
114 static void BindGrContextCallback(const GrGLInterface* interface) { | |
115 cc::ContextProvider* context_provider = | |
116 reinterpret_cast<AwRenderThreadContextProvider*>( | |
117 interface->fCallbackData); | |
118 | |
119 gles2::SetGLContext(context_provider->ContextGL()); | |
120 } | |
121 | |
122 class GrContext* AwRenderThreadContextProvider::GrContext() { | 96 class GrContext* AwRenderThreadContextProvider::GrContext() { |
123 DCHECK(main_thread_checker_.CalledOnValidThread()); | 97 DCHECK(main_thread_checker_.CalledOnValidThread()); |
124 | 98 |
125 if (gr_context_) | 99 if (gr_context_) |
126 return gr_context_.get(); | 100 return gr_context_.get(); |
127 | 101 |
128 // The GrGLInterface factory will make GL calls using the C GLES2 interface. | |
129 // Make sure the gles2 library is initialized first on exactly one thread. | |
130 g_gles2_initializer.Get(); | |
131 gles2::SetGLContext(ContextGL()); | |
132 | |
133 skia::RefPtr<GrGLInterface> interface = skia::AdoptRef(new GrGLInterface); | 102 skia::RefPtr<GrGLInterface> interface = skia::AdoptRef(new GrGLInterface); |
134 skia_bindings::InitCommandBufferSkiaGLBinding(interface.get()); | 103 skia_bindings::InitGLES2InterfaceBindings(interface.get(), ContextGL()); |
135 interface->fCallback = BindGrContextCallback; | |
136 interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(this); | |
137 | 104 |
138 gr_context_ = skia::AdoptRef(GrContext::Create( | 105 gr_context_ = skia::AdoptRef(GrContext::Create( |
139 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get()))); | 106 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get()))); |
140 | 107 |
141 return gr_context_.get(); | 108 return gr_context_.get(); |
142 } | 109 } |
143 | 110 |
144 void AwRenderThreadContextProvider::InvalidateGrContext(uint32_t state) { | 111 void AwRenderThreadContextProvider::InvalidateGrContext(uint32_t state) { |
145 DCHECK(main_thread_checker_.CalledOnValidThread()); | 112 DCHECK(main_thread_checker_.CalledOnValidThread()); |
146 | 113 |
(...skipping 27 matching lines...) Expand all Loading... |
174 void AwRenderThreadContextProvider::OnLostContext() { | 141 void AwRenderThreadContextProvider::OnLostContext() { |
175 DCHECK(main_thread_checker_.CalledOnValidThread()); | 142 DCHECK(main_thread_checker_.CalledOnValidThread()); |
176 | 143 |
177 if (!lost_context_callback_.is_null()) | 144 if (!lost_context_callback_.is_null()) |
178 base::ResetAndReturn(&lost_context_callback_).Run(); | 145 base::ResetAndReturn(&lost_context_callback_).Run(); |
179 if (gr_context_) | 146 if (gr_context_) |
180 gr_context_->abandonContext(); | 147 gr_context_->abandonContext(); |
181 } | 148 } |
182 | 149 |
183 } // namespace android_webview | 150 } // namespace android_webview |
OLD | NEW |