OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ui/compositor/test/in_process_context_provider.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/callback_helpers.h" | |
9 #include "base/debug/trace_event.h" | |
10 #include "base/lazy_instance.h" | |
11 #include "base/strings/stringprintf.h" | |
12 #include "cc/output/managed_memory_policy.h" | |
13 #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_lib.h" | |
16 #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/gl/GrGLInterface.h" | |
19 | |
20 namespace ui { | |
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 void ConvertAttributes( | |
36 const blink::WebGraphicsContext3D::Attributes& attributes, | |
37 gpu::gles2::ContextCreationAttribHelper* output_attribs) { | |
38 output_attribs->alpha_size = attributes.alpha ? 8 : 0; | |
39 output_attribs->depth_size = attributes.depth ? 24 : 0; | |
40 output_attribs->stencil_size = attributes.stencil ? 8 : 0; | |
41 output_attribs->samples = attributes.antialias ? 4 : 0; | |
42 output_attribs->sample_buffers = attributes.antialias ? 1 : 0; | |
43 output_attribs->fail_if_major_perf_caveat = | |
44 attributes.failIfMajorPerformanceCaveat; | |
45 output_attribs->bind_generates_resource = false; | |
46 } | |
47 | |
48 base::LazyInstance<GLES2Initializer> g_gles2_initializer = | |
49 LAZY_INSTANCE_INITIALIZER; | |
50 | |
51 } // namespace | |
52 | |
53 // static | |
54 scoped_refptr<InProcessContextProvider> InProcessContextProvider::Create( | |
55 const blink::WebGraphicsContext3D::Attributes& attributes, | |
56 bool lose_context_when_out_of_memory, | |
57 gfx::AcceleratedWidget window, | |
58 const std::string& debug_name) { | |
59 return new InProcessContextProvider( | |
60 attributes, lose_context_when_out_of_memory, window, debug_name); | |
61 } | |
62 | |
63 // static | |
64 scoped_refptr<InProcessContextProvider> | |
65 InProcessContextProvider::CreateOffscreen( | |
66 bool lose_context_when_out_of_memory) { | |
67 blink::WebGraphicsContext3D::Attributes attributes; | |
68 attributes.depth = false; | |
69 attributes.stencil = true; | |
70 attributes.antialias = false; | |
71 attributes.shareResources = true; | |
72 attributes.noAutomaticFlushes = true; | |
73 | |
74 return InProcessContextProvider::Create( | |
75 attributes, lose_context_when_out_of_memory, gfx::kNullAcceleratedWidget, | |
76 "Offscreen"); | |
77 } | |
78 | |
79 InProcessContextProvider::InProcessContextProvider( | |
80 const blink::WebGraphicsContext3D::Attributes& attributes, | |
81 bool lose_context_when_out_of_memory, | |
82 gfx::AcceleratedWidget window, | |
83 const std::string& debug_name) | |
84 : lose_context_when_out_of_memory_(lose_context_when_out_of_memory), | |
85 share_resources_(attributes.shareResources), | |
piman
2015/01/21 22:37:39
This is always true, so you can remove.
tfarina
2015/01/22 00:05:32
Done.
| |
86 window_(window), | |
87 destroyed_(false), | |
88 debug_name_(debug_name) { | |
89 DCHECK(main_thread_checker_.CalledOnValidThread()); | |
90 context_thread_checker_.DetachFromThread(); | |
91 | |
92 ConvertAttributes(attributes, &attributes_); | |
93 } | |
94 | |
95 InProcessContextProvider::~InProcessContextProvider() { | |
96 DCHECK(main_thread_checker_.CalledOnValidThread() || | |
97 context_thread_checker_.CalledOnValidThread()); | |
98 } | |
99 | |
100 bool InProcessContextProvider::BindToCurrentThread() { | |
101 // This is called on the thread the context will be used. | |
102 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
103 | |
104 if (!context_) { | |
105 gfx::GpuPreference gpu_preference = gfx::PreferDiscreteGpu; | |
106 context_.reset(gpu::GLInProcessContext::Create( | |
107 nullptr, /* service */ | |
108 nullptr, /* surface */ | |
109 true, /* is_offscreen */ | |
110 window_, | |
111 gfx::Size(1, 1), | |
112 nullptr, /* share_context */ | |
113 share_resources_, | |
114 attributes_, | |
115 gpu_preference, | |
116 gpu::GLInProcessContextSharedMemoryLimits(), | |
117 nullptr, | |
118 nullptr)); | |
119 } | |
120 | |
121 InitializeCapabilities(); | |
122 | |
123 std::string unique_context_name = | |
124 base::StringPrintf("%s-%p", debug_name_.c_str(), context_.get()); | |
125 context_->GetImplementation()->TraceBeginCHROMIUM( | |
126 "gpu_toplevel", unique_context_name.c_str()); | |
127 | |
128 return true; | |
129 } | |
130 | |
131 void InProcessContextProvider::InitializeCapabilities() { | |
132 capabilities_.gpu = context_->GetImplementation()->capabilities(); | |
piman
2015/01/21 22:37:39
nit: you can simply do this in InProcessContextPro
tfarina
2015/01/22 00:05:32
Done.
| |
133 } | |
134 | |
135 cc::ContextProvider::Capabilities | |
136 InProcessContextProvider::ContextCapabilities() { | |
137 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
138 return capabilities_; | |
139 } | |
140 | |
141 gpu::gles2::GLES2Interface* InProcessContextProvider::ContextGL() { | |
142 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
143 | |
144 return context_->GetImplementation(); | |
145 } | |
146 | |
147 gpu::ContextSupport* InProcessContextProvider::ContextSupport() { | |
148 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
149 | |
150 return context_->GetImplementation(); | |
151 } | |
152 | |
153 static void BindGrContextCallback(const GrGLInterface* interface) { | |
154 cc::ContextProvider* context_provider = | |
155 reinterpret_cast<InProcessContextProvider*>(interface->fCallbackData); | |
156 | |
157 gles2::SetGLContext(context_provider->ContextGL()); | |
158 } | |
159 | |
160 class GrContext* InProcessContextProvider::GrContext() { | |
161 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
162 | |
163 if (gr_context_) | |
164 return gr_context_.get(); | |
165 | |
166 // The GrGLInterface factory will make GL calls using the C GLES2 interface. | |
167 // Make sure the gles2 library is initialized first on exactly one thread. | |
168 g_gles2_initializer.Get(); | |
169 gles2::SetGLContext(ContextGL()); | |
170 | |
171 skia::RefPtr<GrGLInterface> interface = | |
172 skia::AdoptRef(skia_bindings::CreateCommandBufferSkiaGLBinding()); | |
173 interface->fCallback = BindGrContextCallback; | |
174 interface->fCallbackData = reinterpret_cast<GrGLInterfaceCallbackData>(this); | |
175 | |
176 gr_context_ = skia::AdoptRef(GrContext::Create( | |
177 kOpenGL_GrBackend, reinterpret_cast<GrBackendContext>(interface.get()))); | |
178 | |
179 return gr_context_.get(); | |
180 } | |
181 | |
182 bool InProcessContextProvider::IsContextLost() { | |
183 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
184 | |
185 return true; | |
186 } | |
187 | |
188 void InProcessContextProvider::VerifyContexts() { | |
189 } | |
190 | |
191 void InProcessContextProvider::DeleteCachedResources() { | |
192 DCHECK(context_thread_checker_.CalledOnValidThread()); | |
193 | |
194 if (gr_context_) { | |
195 TRACE_EVENT_INSTANT0("gpu", "GrContext::freeGpuResources", | |
196 TRACE_EVENT_SCOPE_THREAD); | |
197 gr_context_->freeGpuResources(); | |
198 } | |
199 } | |
200 | |
201 bool InProcessContextProvider::DestroyedOnMainThread() { | |
202 DCHECK(main_thread_checker_.CalledOnValidThread()); | |
203 | |
204 base::AutoLock lock(destroyed_lock_); | |
205 return destroyed_; | |
206 } | |
207 | |
208 void InProcessContextProvider::SetLostContextCallback( | |
209 const LostContextCallback& lost_context_callback) { | |
piman
2015/01/21 22:37:39
You need do keep the lost_context_callback around
tfarina
2015/01/22 00:05:32
Done.
piman
2015/01/22 00:18:17
I would expect lost_context_callback_ = lost_conte
| |
210 } | |
211 | |
212 void InProcessContextProvider::SetMemoryPolicyChangedCallback( | |
213 const MemoryPolicyChangedCallback& memory_policy_changed_callback) { | |
214 // There's no memory manager for the in-process implementation. | |
215 } | |
216 | |
217 } // namespace ui | |
OLD | NEW |