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)); | |
68 } | 78 } |
69 | 79 |
70 InProcessContextProvider::InProcessContextProvider( | 80 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()); | 81 DCHECK(main_thread_checker_.CalledOnValidThread()); |
81 context_thread_checker_.DetachFromThread(); | |
82 } | 82 } |
83 | 83 |
84 InProcessContextProvider::~InProcessContextProvider() { | 84 bool AwRenderThreadContextProvider::BindToCurrentThread() { |
85 DCHECK(main_thread_checker_.CalledOnValidThread() || | 85 // This is called on the thread the context will be used. |
86 context_thread_checker_.CalledOnValidThread()); | 86 DCHECK(main_thread_checker_.CalledOnValidThread()); |
87 } | |
88 | 87 |
89 bool InProcessContextProvider::BindToCurrentThread() { | 88 context_->SetContextLostCallback(base::Bind( |
90 // This is called on the thread the context will be used. | 89 &AwRenderThreadContextProvider::OnLostContext, base::Unretained(this))); |
91 DCHECK(context_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 | 90 |
111 capabilities_.gpu = context_->GetImplementation()->capabilities(); | 91 capabilities_.gpu = context_->GetImplementation()->capabilities(); |
boliu
2015/04/22 07:59:18
These two lines can move to construtor too. This m
tfarina
2015/04/23 12:07:51
Done.
| |
112 | 92 |
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 | |
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()); |
124 return capabilities_; | 99 return capabilities_; |
125 } | 100 } |
126 | 101 |
127 gpu::gles2::GLES2Interface* InProcessContextProvider::ContextGL() { | 102 gpu::gles2::GLES2Interface* AwRenderThreadContextProvider::ContextGL() { |
128 DCHECK(context_thread_checker_.CalledOnValidThread()); | 103 DCHECK(main_thread_checker_.CalledOnValidThread()); |
129 | 104 |
130 return context_->GetImplementation(); | 105 return context_->GetImplementation(); |
131 } | 106 } |
132 | 107 |
133 gpu::ContextSupport* InProcessContextProvider::ContextSupport() { | 108 gpu::ContextSupport* AwRenderThreadContextProvider::ContextSupport() { |
134 DCHECK(context_thread_checker_.CalledOnValidThread()); | 109 DCHECK(main_thread_checker_.CalledOnValidThread()); |
135 | 110 |
136 return context_->GetImplementation(); | 111 return context_->GetImplementation(); |
137 } | 112 } |
138 | 113 |
139 static void BindGrContextCallback(const GrGLInterface* interface) { | 114 static void BindGrContextCallback(const GrGLInterface* interface) { |
140 cc::ContextProvider* context_provider = | 115 cc::ContextProvider* context_provider = |
141 reinterpret_cast<InProcessContextProvider*>(interface->fCallbackData); | 116 reinterpret_cast<AwRenderThreadContextProvider*>( |
117 interface->fCallbackData); | |
142 | 118 |
143 gles2::SetGLContext(context_provider->ContextGL()); | 119 gles2::SetGLContext(context_provider->ContextGL()); |
144 } | 120 } |
145 | 121 |
146 class GrContext* InProcessContextProvider::GrContext() { | 122 class GrContext* AwRenderThreadContextProvider::GrContext() { |
147 DCHECK(context_thread_checker_.CalledOnValidThread()); | 123 DCHECK(main_thread_checker_.CalledOnValidThread()); |
148 | 124 |
149 if (gr_context_) | 125 if (gr_context_) |
150 return gr_context_.get(); | 126 return gr_context_.get(); |
151 | 127 |
152 // The GrGLInterface factory will make GL calls using the C GLES2 interface. | 128 // 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. | 129 // Make sure the gles2 library is initialized first on exactly one thread. |
154 g_gles2_initializer.Get(); | 130 g_gles2_initializer.Get(); |
155 gles2::SetGLContext(ContextGL()); | 131 gles2::SetGLContext(ContextGL()); |
156 | 132 |
157 skia::RefPtr<GrGLInterface> interface = | 133 skia::RefPtr<GrGLInterface> interface = |
158 skia::AdoptRef(skia_bindings::CreateCommandBufferSkiaGLBinding()); | 134 skia::AdoptRef(skia_bindings::CreateCommandBufferSkiaGLBinding()); |
159 interface->fCallback = BindGrContextCallback; | 135 interface->fCallback = BindGrContextCallback; |
160 interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(this); | 136 interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(this); |
161 | 137 |
162 gr_context_ = skia::AdoptRef(GrContext::Create( | 138 gr_context_ = skia::AdoptRef(GrContext::Create( |
163 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get()))); | 139 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get()))); |
164 | 140 |
165 return gr_context_.get(); | 141 return gr_context_.get(); |
166 } | 142 } |
167 | 143 |
168 void InProcessContextProvider::SetupLock() { | 144 void AwRenderThreadContextProvider::SetupLock() { |
145 context_->SetLock(&context_lock_); | |
169 } | 146 } |
170 | 147 |
171 base::Lock* InProcessContextProvider::GetLock() { | 148 base::Lock* AwRenderThreadContextProvider::GetLock() { |
172 return &context_lock_; | 149 return &context_lock_; |
173 } | 150 } |
174 | 151 |
175 bool InProcessContextProvider::IsContextLost() { | 152 bool AwRenderThreadContextProvider::IsContextLost() { |
176 DCHECK(context_thread_checker_.CalledOnValidThread()); | 153 DCHECK(main_thread_checker_.CalledOnValidThread()); |
177 | 154 |
178 base::AutoLock lock(destroyed_lock_); | 155 base::AutoLock lock(context_lock_); |
boliu
2015/04/22 07:59:18
Should never have to directly lock context_lock_ i
tfarina
2015/04/23 12:07:51
Done.
| |
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_); | 175 base::AutoLock lock(context_lock_); |
199 return destroyed_; | 176 return destroyed_; |
200 } | 177 } |
201 | 178 |
202 void InProcessContextProvider::SetLostContextCallback( | 179 void AwRenderThreadContextProvider::SetLostContextCallback( |
203 const LostContextCallback& lost_context_callback) { | 180 const LostContextCallback& lost_context_callback) { |
204 lost_context_callback_ = lost_context_callback; | 181 lost_context_callback_ = lost_context_callback; |
205 } | 182 } |
206 | 183 |
207 void InProcessContextProvider::SetMemoryPolicyChangedCallback( | 184 void AwRenderThreadContextProvider::SetMemoryPolicyChangedCallback( |
208 const MemoryPolicyChangedCallback& memory_policy_changed_callback) { | 185 const MemoryPolicyChangedCallback& memory_policy_changed_callback) { |
209 // There's no memory manager for the in-process implementation. | 186 // There's no memory manager for the in-process implementation. |
210 } | 187 } |
211 | 188 |
212 void InProcessContextProvider::OnLostContext() { | 189 void AwRenderThreadContextProvider::OnLostContext() { |
213 DCHECK(context_thread_checker_.CalledOnValidThread()); | 190 DCHECK(main_thread_checker_.CalledOnValidThread()); |
214 { | 191 { |
215 base::AutoLock lock(destroyed_lock_); | 192 base::AutoLock lock(context_lock_); |
216 if (destroyed_) | 193 if (destroyed_) |
217 return; | 194 return; |
218 destroyed_ = true; | 195 destroyed_ = true; |
219 } | 196 } |
220 if (!lost_context_callback_.is_null()) | 197 if (!lost_context_callback_.is_null()) |
221 base::ResetAndReturn(&lost_context_callback_).Run(); | 198 base::ResetAndReturn(&lost_context_callback_).Run(); |
222 if (gr_context_) | 199 if (gr_context_) |
223 gr_context_->abandonContext(); | 200 gr_context_->abandonContext(); |
224 } | 201 } |
225 | 202 |
226 } // namespace ui | 203 } // namespace android_webview |
OLD | NEW |