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