Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(448)

Side by Side Diff: gpu/gles2_conform_support/egl/context.cc

Issue 1714883002: command_buffer_gles2: Implement EGL default Display as a global object (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@command_buffer_gles2-multiple-contexts
Patch Set: workaround gtf Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2016 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 "gpu/gles2_conform_support/egl/context.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "gpu/command_buffer/client/gles2_implementation.h"
10 #include "gpu/command_buffer/client/gles2_lib.h"
11 #include "gpu/command_buffer/client/transfer_buffer.h"
12 #include "gpu/command_buffer/common/value_state.h"
13 #include "gpu/command_buffer/service/context_group.h"
14 #include "gpu/command_buffer/service/mailbox_manager.h"
15 #include "gpu/command_buffer/service/memory_tracking.h"
16 #include "gpu/command_buffer/service/transfer_buffer_manager.h"
17 #include "gpu/command_buffer/service/valuebuffer_manager.h"
18 #include "gpu/gles2_conform_support/egl/config.h"
19 #include "gpu/gles2_conform_support/egl/display.h"
20 #include "gpu/gles2_conform_support/egl/surface.h"
21 #include "gpu/gles2_conform_support/egl/thread_state.h"
22
23 // The slight complexification in this file comes from following properties:
24 // 1) Command buffer connection (context) can not be established without a
25 // GLSurface. EGL Context can be created independent of a surface. This is why
26 // the connection is created only during first MakeCurrent.
27 // 2) Command buffer MakeCurrent calls need the real gl context and surface be
28 // current.
29 // 3) Client can change real EGL context behind the scenes and then still expect
30 // command buffer MakeCurrent re-set the command buffer context. This is why all
31 // MakeCurrent calls must actually reset the real context, even though command
32 // buffer current context does not change.
33 // 4) EGL context can be destroyed without surface, but command buffer would
34 // need the surface to run various cleanups. If context is destroyed
35 // surfaceless, the context is marked lost before destruction. This is avoided
36 // if possible, since command buffer at the time of writing prints out debug
37 // text in this case.
38
39 namespace {
40 const int32_t kCommandBufferSize = 1024 * 1024;
41 const int32_t kTransferBufferSize = 512 * 1024;
42 const bool kBindGeneratesResources = true;
43 const bool kLoseContextWhenOutOfMemory = false;
44 const bool kSupportClientSideArrays = true;
45 }
46
47 namespace egl {
48 Context::Context(Display* display, const Config* config)
49 : display_(display),
50 config_(config),
51 is_current_in_some_thread_(false),
52 is_destroyed_(false) {}
53
54 Context::~Context() {
55 // We might not have a surface, so we must lose the context. Cleanup will
56 // execute GL commands otherwise. TODO: if shared contexts are ever
57 // implemented, this will leak the GL resources. For pbuffer contexts, one
58 // could track the last current surface or create a surface for destroying
59 // purposes only. Other option would be to make the service usable without
60 // surface.
61 if (HasService()) {
62 if (!WasServiceContextLost())
63 MarkServiceContextLost();
64 DestroyService();
65 }
66 }
67
68 void Context::MarkDestroyed() {
69 is_destroyed_ = true;
70 }
71
72 void Context::FlushAndSwapBuffers(gfx::GLSurface* current_surface) {
73 DCHECK(HasService() && is_current_in_some_thread_);
74 if (!Flush(current_surface))
75 return;
76 current_surface->SwapBuffers();
77 }
78
79 bool Context::MakeCurrent(Context* current_context,
80 gfx::GLSurface* current_surface,
81 Context* new_context,
82 gfx::GLSurface* new_surface) {
83 if (!new_context && !current_context) {
84 return true;
85 }
86
87 bool cleanup_old_current_context = false;
88 if (current_context) {
89 if (current_context->Flush(current_surface))
90 cleanup_old_current_context = new_context != current_context;
91 }
92
93 if (new_context) {
94 if (!new_context->IsCompatibleSurface(new_surface))
95 return false;
96
97 if (new_context->HasService()) {
98 if (new_context->WasServiceContextLost())
99 return false;
100 if (new_context != current_context) {
101 // If Flush did not set the current context, set it now. Otherwise
102 // calling into the decoder is not ok.
103 if (!new_context->gl_context_->MakeCurrent(new_surface)) {
104 new_context->MarkServiceContextLost();
105 return false;
106 }
107 }
108 if (new_context != current_context || new_surface != current_surface)
109 new_context->decoder_->SetSurface(new_surface);
110 if (!new_context->decoder_->MakeCurrent()) {
111 new_context->MarkServiceContextLost();
112 return false;
113 }
114 } else {
115 if (!new_context->CreateService(new_surface)) {
116 return false;
117 }
118 }
119 }
120
121 // The current_surface will be released when MakeCurrent succeeds.
122 // Cleanup in this case only.
123 if (cleanup_old_current_context) {
124 if (current_context->is_destroyed_ && current_surface != new_surface) {
125 current_context->gl_context_->MakeCurrent(current_surface);
126 // If we are releasing the context and we have one ref, it means that the
127 // ref will be lost and the object will be destroyed. Destroy the service
128 // explicitly here, so that cleanup can happen and client GL
129 // implementation does not print errors.
130 current_context->DestroyService();
131 } else {
132 current_context->decoder_->ReleaseSurface();
133 }
134 }
135
136 return true;
137 }
138
139 bool Context::ValidateAttributeList(const EGLint* attrib_list) {
140 if (attrib_list) {
141 for (int i = 0; attrib_list[i] != EGL_NONE; attrib_list += 2) {
142 switch (attrib_list[i]) {
143 case EGL_CONTEXT_CLIENT_VERSION:
144 break;
145 default:
146 return false;
147 }
148 }
149 }
150 return true;
151 }
152
153 gpu::Capabilities Context::GetCapabilities() {
154 return decoder_->GetCapabilities();
155 }
156
157 int32_t Context::CreateImage(ClientBuffer buffer,
158 size_t width,
159 size_t height,
160 unsigned internalformat) {
161 NOTIMPLEMENTED();
162 return -1;
163 }
164
165 void Context::DestroyImage(int32_t id) {
166 NOTIMPLEMENTED();
167 }
168
169 int32_t Context::CreateGpuMemoryBufferImage(size_t width,
170 size_t height,
171 unsigned internalformat,
172 unsigned usage) {
173 NOTIMPLEMENTED();
174 return -1;
175 }
176
177 void Context::SignalQuery(uint32_t query, const base::Closure& callback) {
178 NOTIMPLEMENTED();
179 }
180
181 void Context::SetLock(base::Lock*) {
182 NOTIMPLEMENTED();
183 }
184
185 bool Context::IsGpuChannelLost() {
186 NOTIMPLEMENTED();
187 return false;
188 }
189
190 void Context::EnsureWorkVisible() {
191 // This is only relevant for out-of-process command buffers.
192 }
193
194 gpu::CommandBufferNamespace Context::GetNamespaceID() const {
195 return gpu::CommandBufferNamespace::IN_PROCESS;
196 }
197
198 gpu::CommandBufferId Context::GetCommandBufferID() const {
199 return gpu::CommandBufferId();
200 }
201
202 int32_t Context::GetExtraCommandBufferData() const {
203 return 0;
204 }
205
206 uint64_t Context::GenerateFenceSyncRelease() {
207 return display_->GenerateFenceSyncRelease();
208 }
209
210 bool Context::IsFenceSyncRelease(uint64_t release) {
211 return display_->IsFenceSyncRelease(release);
212 }
213
214 bool Context::IsFenceSyncFlushed(uint64_t release) {
215 return display_->IsFenceSyncFlushed(release);
216 }
217
218 bool Context::IsFenceSyncFlushReceived(uint64_t release) {
219 return display_->IsFenceSyncFlushReceived(release);
220 }
221
222 void Context::SignalSyncToken(const gpu::SyncToken& sync_token,
223 const base::Closure& callback) {
224 NOTIMPLEMENTED();
225 }
226
227 bool Context::CanWaitUnverifiedSyncToken(const gpu::SyncToken* sync_token) {
228 return false;
229 }
230
231 void Context::ApplyCurrentContext(gfx::GLSurface* current_surface) {
232 DCHECK(HasService());
233 // The current_surface will be the same as
234 // the surface of the decoder. We can not DCHECK as there is
235 // no accessor.
236 if (!WasServiceContextLost()) {
237 if (!gl_context_->MakeCurrent(current_surface))
238 MarkServiceContextLost();
239 }
240 gles2::SetGLContext(client_gl_context_.get());
241 }
242
243 void Context::ApplyContextReleased() {
244 gles2::SetGLContext(nullptr);
245 }
246
247 bool Context::CreateService(gfx::GLSurface* gl_surface) {
248 scoped_refptr<gpu::TransferBufferManager> transfer_buffer_manager(
249 new gpu::TransferBufferManager(nullptr));
250 transfer_buffer_manager->Initialize();
251
252 scoped_ptr<gpu::CommandBufferService> command_buffer(
253 new gpu::CommandBufferService(transfer_buffer_manager.get()));
254 if (!command_buffer->Initialize())
255 return false;
256
257 scoped_refptr<gpu::gles2::ContextGroup> group(new gpu::gles2::ContextGroup(
258 NULL, NULL, new gpu::gles2::ShaderTranslatorCache,
259 new gpu::gles2::FramebufferCompletenessCache, NULL, NULL, NULL, true));
260
261 scoped_ptr<gpu::gles2::GLES2Decoder> decoder(
262 gpu::gles2::GLES2Decoder::Create(group.get()));
263 if (!decoder.get())
264 return false;
265
266 scoped_ptr<gpu::GpuScheduler> gpu_scheduler(new gpu::GpuScheduler(
267 command_buffer.get(), decoder.get(), decoder.get()));
268
269 decoder->set_engine(gpu_scheduler.get());
270
271 scoped_refptr<gfx::GLContext> gl_context(gfx::GLContext::CreateGLContext(
272 nullptr, gl_surface, gfx::PreferDiscreteGpu));
273 if (!gl_context)
274 return false;
275
276 gl_context->MakeCurrent(gl_surface);
277
278 gpu::gles2::ContextCreationAttribHelper helper;
279 config_->GetAttrib(EGL_ALPHA_SIZE, &helper.alpha_size);
280 config_->GetAttrib(EGL_BLUE_SIZE, &helper.blue_size);
281 config_->GetAttrib(EGL_GREEN_SIZE, &helper.green_size);
282 config_->GetAttrib(EGL_RED_SIZE, &helper.red_size);
283 config_->GetAttrib(EGL_DEPTH_SIZE, &helper.depth_size);
284 config_->GetAttrib(EGL_STENCIL_SIZE, &helper.stencil_size);
285 config_->GetAttrib(EGL_SAMPLES, &helper.samples);
286 config_->GetAttrib(EGL_SAMPLE_BUFFERS, &helper.sample_buffers);
287
288 helper.buffer_preserved = false;
289 helper.bind_generates_resource = kBindGeneratesResources;
290 helper.fail_if_major_perf_caveat = false;
291 helper.lose_context_when_out_of_memory = kLoseContextWhenOutOfMemory;
292 helper.context_type = gpu::gles2::CONTEXT_TYPE_OPENGLES2;
293 std::vector<int32_t> attribs;
294 helper.Serialize(&attribs);
295
296 if (!decoder->Initialize(gl_surface, gl_context.get(),
297 gl_surface->IsOffscreen(), gl_surface->GetSize(),
298 gpu::gles2::DisallowedFeatures(), attribs)) {
299 return false;
300 }
301
302 command_buffer->SetPutOffsetChangeCallback(base::Bind(
303 &gpu::GpuScheduler::PutChanged, base::Unretained(gpu_scheduler.get())));
304 command_buffer->SetGetBufferChangeCallback(base::Bind(
305 &gpu::GpuScheduler::SetGetBuffer, base::Unretained(gpu_scheduler.get())));
306
307 scoped_ptr<gpu::gles2::GLES2CmdHelper> gles2_cmd_helper(
308 new gpu::gles2::GLES2CmdHelper(command_buffer.get()));
309 if (!gles2_cmd_helper->Initialize(kCommandBufferSize)) {
310 decoder->Destroy(true);
311 return false;
312 }
313
314 scoped_ptr<gpu::TransferBuffer> transfer_buffer(
315 new gpu::TransferBuffer(gles2_cmd_helper.get()));
316
317 gles2_cmd_helper_.reset(gles2_cmd_helper.release());
318 transfer_buffer_.reset(transfer_buffer.release());
319 command_buffer_.reset(command_buffer.release());
320 gpu_scheduler_.reset(gpu_scheduler.release());
321 decoder_.reset(decoder.release());
322 gl_context_ = gl_context.get();
323
324 scoped_ptr<gpu::gles2::GLES2Implementation> context(
325 new gpu::gles2::GLES2Implementation(
326 gles2_cmd_helper_.get(), nullptr, transfer_buffer_.get(),
327 kBindGeneratesResources, kLoseContextWhenOutOfMemory,
328 kSupportClientSideArrays, this));
329
330 if (!context->Initialize(kTransferBufferSize, kTransferBufferSize / 2,
331 kTransferBufferSize * 2,
332 gpu::gles2::GLES2Implementation::kNoLimit)) {
333 DestroyService();
334 return false;
335 }
336
337 context->EnableFeatureCHROMIUM("pepper3d_allow_buffers_on_multiple_targets");
338 context->EnableFeatureCHROMIUM("pepper3d_support_fixed_attribs");
339 client_gl_context_.reset(context.release());
340 return true;
341 }
342
343 void Context::DestroyService() {
344 DCHECK(HasService());
345 bool have_context = !WasServiceContextLost();
346 // The client gl interface might still be set to current global
347 // interface. This will be cleaned up in ApplyContextReleased
348 // with AutoCurrentContextRestore.
349 client_gl_context_.reset();
350 gl_context_ = nullptr;
351
352 transfer_buffer_.reset();
353 gpu_scheduler_.reset();
354 if (decoder_)
355 decoder_->Destroy(have_context);
356 gles2_cmd_helper_.reset();
357 command_buffer_.reset();
358 }
359
360 bool Context::HasService() const {
361 return decoder_ != nullptr;
362 }
363
364 void Context::MarkServiceContextLost() {
365 decoder_->MarkContextLost(gpu::error::kMakeCurrentFailed);
366 }
367
368 bool Context::WasServiceContextLost() const {
369 return decoder_->WasContextLost();
370 }
371
372 bool Context::IsCompatibleSurface(gfx::GLSurface* gl_surface) {
373 EGLint value = EGL_NONE;
374 config_->GetAttrib(EGL_SURFACE_TYPE, &value);
375 bool config_is_offscreen = (value & EGL_PBUFFER_BIT) != 0;
376 return gl_surface->IsOffscreen() == config_is_offscreen;
377 }
378
379 bool Context::Flush(gfx::GLSurface* gl_surface) {
380 if (WasServiceContextLost())
381 return false;
382 if (!gl_context_->MakeCurrent(gl_surface)) {
383 MarkServiceContextLost();
384 return false;
385 }
386 client_gl_context_->Flush();
387 return true;
388 }
389
390 } // namespace egl
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698