OLD | NEW |
1 // Copyright (c) 2013 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 "ui/compositor/test/in_process_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/strings/stringprintf.h" | |
11 #include "base/trace_event/trace_event.h" | 10 #include "base/trace_event/trace_event.h" |
12 #include "cc/output/managed_memory_policy.h" | 11 #include "cc/output/managed_memory_policy.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 ui { | 20 namespace android_webview { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 // Singleton used to initialize and terminate the gles2 library. | 24 // Singleton used to initialize and terminate the gles2 library. |
25 class GLES2Initializer { | 25 class GLES2Initializer { |
26 public: | 26 public: |
27 GLES2Initializer() { gles2::Initialize(); } | 27 GLES2Initializer() { gles2::Initialize(); } |
28 | 28 |
29 ~GLES2Initializer() { gles2::Terminate(); } | 29 ~GLES2Initializer() { gles2::Terminate(); } |
30 | 30 |
31 private: | 31 private: |
32 DISALLOW_COPY_AND_ASSIGN(GLES2Initializer); | 32 DISALLOW_COPY_AND_ASSIGN(GLES2Initializer); |
33 }; | 33 }; |
34 | 34 |
35 base::LazyInstance<GLES2Initializer> g_gles2_initializer = | 35 base::LazyInstance<GLES2Initializer> g_gles2_initializer = |
36 LAZY_INSTANCE_INITIALIZER; | 36 LAZY_INSTANCE_INITIALIZER; |
37 | 37 |
38 } // namespace | 38 } // namespace |
39 | 39 |
40 // static | 40 // static |
41 scoped_refptr<InProcessContextProvider> InProcessContextProvider::Create( | 41 scoped_refptr<AwRenderThreadContextProvider> |
42 const gpu::gles2::ContextCreationAttribHelper& attribs, | 42 AwRenderThreadContextProvider::Create( |
43 bool lose_context_when_out_of_memory, | 43 scoped_refptr<gfx::GLSurface> surface, |
44 gfx::AcceleratedWidget window, | 44 scoped_refptr<gpu::InProcessCommandBuffer::Service> service) { |
45 const std::string& debug_name) { | 45 return new AwRenderThreadContextProvider(surface, service); |
46 return new InProcessContextProvider( | |
47 attribs, lose_context_when_out_of_memory, window, debug_name); | |
48 } | 46 } |
49 | 47 |
50 // static | 48 AwRenderThreadContextProvider::AwRenderThreadContextProvider( |
51 scoped_refptr<InProcessContextProvider> | 49 scoped_refptr<gfx::GLSurface> surface, |
52 InProcessContextProvider::CreateOffscreen( | 50 scoped_refptr<gpu::InProcessCommandBuffer::Service> service) |
53 bool lose_context_when_out_of_memory) { | 51 : destroyed_(false) { |
54 gpu::gles2::ContextCreationAttribHelper attribs; | 52 DCHECK(main_thread_checker_.CalledOnValidThread()); |
55 attribs.alpha_size = 8; | 53 |
56 attribs.blue_size = 8; | 54 blink::WebGraphicsContext3D::Attributes attributes; |
57 attribs.green_size = 8; | 55 attributes.antialias = false; |
58 attribs.red_size = 8; | 56 attributes.depth = false; |
59 attribs.depth_size = 0; | 57 attributes.stencil = false; |
60 attribs.stencil_size = 8; | 58 attributes.shareResources = true; |
61 attribs.samples = 0; | 59 attributes.noAutomaticFlushes = true; |
62 attribs.sample_buffers = 0; | 60 gpu::gles2::ContextCreationAttribHelper attribs_for_gles2; |
63 attribs.fail_if_major_perf_caveat = false; | 61 gpu_blink::WebGraphicsContext3DImpl::ConvertAttributes(attributes, |
64 attribs.bind_generates_resource = false; | 62 &attribs_for_gles2); |
65 return new InProcessContextProvider( | 63 attribs_for_gles2.lose_context_when_out_of_memory = true; |
66 attribs, lose_context_when_out_of_memory, gfx::kNullAcceleratedWidget, | 64 |
67 "Offscreen"); | 65 context_.reset(gpu::GLInProcessContext::Create( |
| 66 service, |
| 67 surface, |
| 68 surface->IsOffscreen(), |
| 69 gfx::kNullAcceleratedWidget, |
| 70 surface->GetSize(), |
| 71 NULL /* share_context */, |
| 72 false /* share_resources */, |
| 73 attribs_for_gles2, |
| 74 gfx::PreferDiscreteGpu, |
| 75 gpu::GLInProcessContextSharedMemoryLimits(), |
| 76 nullptr, |
| 77 nullptr)); |
| 78 |
| 79 context_->SetContextLostCallback(base::Bind( |
| 80 &AwRenderThreadContextProvider::OnLostContext, base::Unretained(this))); |
| 81 |
| 82 capabilities_.gpu = context_->GetImplementation()->capabilities(); |
68 } | 83 } |
69 | 84 |
70 InProcessContextProvider::InProcessContextProvider( | 85 AwRenderThreadContextProvider::~AwRenderThreadContextProvider() { |
71 const gpu::gles2::ContextCreationAttribHelper& attribs, | |
72 bool lose_context_when_out_of_memory, | |
73 gfx::AcceleratedWidget window, | |
74 const std::string& debug_name) | |
75 : attribs_(attribs), | |
76 lose_context_when_out_of_memory_(lose_context_when_out_of_memory), | |
77 window_(window), | |
78 debug_name_(debug_name), | |
79 destroyed_(false) { | |
80 DCHECK(main_thread_checker_.CalledOnValidThread()); | 86 DCHECK(main_thread_checker_.CalledOnValidThread()); |
81 context_thread_checker_.DetachFromThread(); | |
82 } | 87 } |
83 | 88 |
84 InProcessContextProvider::~InProcessContextProvider() { | 89 bool AwRenderThreadContextProvider::BindToCurrentThread() { |
85 DCHECK(main_thread_checker_.CalledOnValidThread() || | |
86 context_thread_checker_.CalledOnValidThread()); | |
87 } | |
88 | |
89 bool InProcessContextProvider::BindToCurrentThread() { | |
90 // This is called on the thread the context will be used. | 90 // This is called on the thread the context will be used. |
91 DCHECK(context_thread_checker_.CalledOnValidThread()); | 91 DCHECK(main_thread_checker_.CalledOnValidThread()); |
92 | |
93 if (!context_) { | |
94 gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu; | |
95 context_.reset(gpu::GLInProcessContext::Create( | |
96 nullptr, /* service */ | |
97 nullptr, /* surface */ | |
98 !window_, /* is_offscreen */ | |
99 window_, gfx::Size(1, 1), nullptr, /* share_context */ | |
100 true, /* share_resources */ | |
101 attribs_, gpu_preference, gpu::GLInProcessContextSharedMemoryLimits(), | |
102 nullptr, nullptr)); | |
103 | |
104 if (!context_) | |
105 return false; | |
106 | |
107 context_->SetContextLostCallback(base::Bind( | |
108 &InProcessContextProvider::OnLostContext, base::Unretained(this))); | |
109 } | |
110 | |
111 capabilities_.gpu = context_->GetImplementation()->capabilities(); | |
112 | |
113 std::string unique_context_name = | |
114 base::StringPrintf("%s-%p", debug_name_.c_str(), context_.get()); | |
115 context_->GetImplementation()->TraceBeginCHROMIUM( | |
116 "gpu_toplevel", unique_context_name.c_str()); | |
117 | 92 |
118 return true; | 93 return true; |
119 } | 94 } |
120 | 95 |
121 cc::ContextProvider::Capabilities | 96 cc::ContextProvider::Capabilities |
122 InProcessContextProvider::ContextCapabilities() { | 97 AwRenderThreadContextProvider::ContextCapabilities() { |
123 DCHECK(context_thread_checker_.CalledOnValidThread()); | 98 DCHECK(main_thread_checker_.CalledOnValidThread()); |
| 99 |
124 return capabilities_; | 100 return capabilities_; |
125 } | 101 } |
126 | 102 |
127 gpu::gles2::GLES2Interface* InProcessContextProvider::ContextGL() { | 103 gpu::gles2::GLES2Interface* AwRenderThreadContextProvider::ContextGL() { |
128 DCHECK(context_thread_checker_.CalledOnValidThread()); | 104 DCHECK(main_thread_checker_.CalledOnValidThread()); |
129 | 105 |
130 return context_->GetImplementation(); | 106 return context_->GetImplementation(); |
131 } | 107 } |
132 | 108 |
133 gpu::ContextSupport* InProcessContextProvider::ContextSupport() { | 109 gpu::ContextSupport* AwRenderThreadContextProvider::ContextSupport() { |
134 DCHECK(context_thread_checker_.CalledOnValidThread()); | 110 DCHECK(main_thread_checker_.CalledOnValidThread()); |
135 | 111 |
136 return context_->GetImplementation(); | 112 return context_->GetImplementation(); |
137 } | 113 } |
138 | 114 |
139 static void BindGrContextCallback(const GrGLInterface* interface) { | 115 static void BindGrContextCallback(const GrGLInterface* interface) { |
140 cc::ContextProvider* context_provider = | 116 cc::ContextProvider* context_provider = |
141 reinterpret_cast<InProcessContextProvider*>(interface->fCallbackData); | 117 reinterpret_cast<AwRenderThreadContextProvider*>( |
| 118 interface->fCallbackData); |
142 | 119 |
143 gles2::SetGLContext(context_provider->ContextGL()); | 120 gles2::SetGLContext(context_provider->ContextGL()); |
144 } | 121 } |
145 | 122 |
146 class GrContext* InProcessContextProvider::GrContext() { | 123 class GrContext* AwRenderThreadContextProvider::GrContext() { |
147 DCHECK(context_thread_checker_.CalledOnValidThread()); | 124 DCHECK(main_thread_checker_.CalledOnValidThread()); |
148 | 125 |
149 if (gr_context_) | 126 if (gr_context_) |
150 return gr_context_.get(); | 127 return gr_context_.get(); |
151 | 128 |
152 // The GrGLInterface factory will make GL calls using the C GLES2 interface. | 129 // The GrGLInterface factory will make GL calls using the C GLES2 interface. |
153 // Make sure the gles2 library is initialized first on exactly one thread. | 130 // Make sure the gles2 library is initialized first on exactly one thread. |
154 g_gles2_initializer.Get(); | 131 g_gles2_initializer.Get(); |
155 gles2::SetGLContext(ContextGL()); | 132 gles2::SetGLContext(ContextGL()); |
156 | 133 |
157 skia::RefPtr<GrGLInterface> interface = | 134 skia::RefPtr<GrGLInterface> interface = |
158 skia::AdoptRef(skia_bindings::CreateCommandBufferSkiaGLBinding()); | 135 skia::AdoptRef(skia_bindings::CreateCommandBufferSkiaGLBinding()); |
159 interface->fCallback = BindGrContextCallback; | 136 interface->fCallback = BindGrContextCallback; |
160 interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(this); | 137 interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(this); |
161 | 138 |
162 gr_context_ = skia::AdoptRef(GrContext::Create( | 139 gr_context_ = skia::AdoptRef(GrContext::Create( |
163 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get()))); | 140 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get()))); |
164 | 141 |
165 return gr_context_.get(); | 142 return gr_context_.get(); |
166 } | 143 } |
167 | 144 |
168 void InProcessContextProvider::SetupLock() { | 145 void AwRenderThreadContextProvider::SetupLock() { |
| 146 context_->SetLock(&context_lock_); |
169 } | 147 } |
170 | 148 |
171 base::Lock* InProcessContextProvider::GetLock() { | 149 base::Lock* AwRenderThreadContextProvider::GetLock() { |
172 return &context_lock_; | 150 return &context_lock_; |
173 } | 151 } |
174 | 152 |
175 bool InProcessContextProvider::IsContextLost() { | 153 bool AwRenderThreadContextProvider::IsContextLost() { |
176 DCHECK(context_thread_checker_.CalledOnValidThread()); | 154 DCHECK(main_thread_checker_.CalledOnValidThread()); |
177 | 155 |
178 base::AutoLock lock(destroyed_lock_); | |
179 return destroyed_; | 156 return destroyed_; |
180 } | 157 } |
181 | 158 |
182 void InProcessContextProvider::VerifyContexts() { | 159 void AwRenderThreadContextProvider::VerifyContexts() { |
183 } | 160 } |
184 | 161 |
185 void InProcessContextProvider::DeleteCachedResources() { | 162 void AwRenderThreadContextProvider::DeleteCachedResources() { |
186 DCHECK(context_thread_checker_.CalledOnValidThread()); | 163 DCHECK(main_thread_checker_.CalledOnValidThread()); |
187 | 164 |
188 if (gr_context_) { | 165 if (gr_context_) { |
189 TRACE_EVENT_INSTANT0("gpu", "GrContext::freeGpuResources", | 166 TRACE_EVENT_INSTANT0("gpu", "GrContext::freeGpuResources", |
190 TRACE_EVENT_SCOPE_THREAD); | 167 TRACE_EVENT_SCOPE_THREAD); |
191 gr_context_->freeGpuResources(); | 168 gr_context_->freeGpuResources(); |
192 } | 169 } |
193 } | 170 } |
194 | 171 |
195 bool InProcessContextProvider::DestroyedOnMainThread() { | 172 bool AwRenderThreadContextProvider::DestroyedOnMainThread() { |
196 DCHECK(main_thread_checker_.CalledOnValidThread()); | 173 DCHECK(main_thread_checker_.CalledOnValidThread()); |
197 | 174 |
198 base::AutoLock lock(destroyed_lock_); | |
199 return destroyed_; | 175 return destroyed_; |
200 } | 176 } |
201 | 177 |
202 void InProcessContextProvider::SetLostContextCallback( | 178 void AwRenderThreadContextProvider::SetLostContextCallback( |
203 const LostContextCallback& lost_context_callback) { | 179 const LostContextCallback& lost_context_callback) { |
204 lost_context_callback_ = lost_context_callback; | 180 lost_context_callback_ = lost_context_callback; |
205 } | 181 } |
206 | 182 |
207 void InProcessContextProvider::SetMemoryPolicyChangedCallback( | 183 void AwRenderThreadContextProvider::SetMemoryPolicyChangedCallback( |
208 const MemoryPolicyChangedCallback& memory_policy_changed_callback) { | 184 const MemoryPolicyChangedCallback& memory_policy_changed_callback) { |
209 // There's no memory manager for the in-process implementation. | 185 // There's no memory manager for the in-process implementation. |
210 } | 186 } |
211 | 187 |
212 void InProcessContextProvider::OnLostContext() { | 188 void AwRenderThreadContextProvider::OnLostContext() { |
213 DCHECK(context_thread_checker_.CalledOnValidThread()); | 189 DCHECK(main_thread_checker_.CalledOnValidThread()); |
214 { | 190 |
215 base::AutoLock lock(destroyed_lock_); | 191 if (destroyed_) |
216 if (destroyed_) | 192 return; |
217 return; | 193 destroyed_ = true; |
218 destroyed_ = true; | 194 |
219 } | |
220 if (!lost_context_callback_.is_null()) | 195 if (!lost_context_callback_.is_null()) |
221 base::ResetAndReturn(&lost_context_callback_).Run(); | 196 base::ResetAndReturn(&lost_context_callback_).Run(); |
222 if (gr_context_) | 197 if (gr_context_) |
223 gr_context_->abandonContext(); | 198 gr_context_->abandonContext(); |
224 } | 199 } |
225 | 200 |
226 } // namespace ui | 201 } // namespace android_webview |
OLD | NEW |