| OLD | NEW |
| (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 gpu_preferences_, nullptr, nullptr, | |
| 259 new gpu::gles2::ShaderTranslatorCache(gpu_preferences_), | |
| 260 new gpu::gles2::FramebufferCompletenessCache, nullptr, nullptr, nullptr, | |
| 261 true)); | |
| 262 | |
| 263 scoped_ptr<gpu::gles2::GLES2Decoder> decoder( | |
| 264 gpu::gles2::GLES2Decoder::Create(group.get())); | |
| 265 if (!decoder.get()) | |
| 266 return false; | |
| 267 | |
| 268 scoped_ptr<gpu::CommandExecutor> command_executor(new gpu::CommandExecutor( | |
| 269 command_buffer.get(), decoder.get(), decoder.get())); | |
| 270 | |
| 271 decoder->set_engine(command_executor.get()); | |
| 272 | |
| 273 scoped_refptr<gfx::GLContext> gl_context(gfx::GLContext::CreateGLContext( | |
| 274 nullptr, gl_surface, gfx::PreferDiscreteGpu)); | |
| 275 if (!gl_context) | |
| 276 return false; | |
| 277 | |
| 278 gl_context->MakeCurrent(gl_surface); | |
| 279 | |
| 280 gpu::gles2::ContextCreationAttribHelper helper; | |
| 281 config_->GetAttrib(EGL_ALPHA_SIZE, &helper.alpha_size); | |
| 282 config_->GetAttrib(EGL_BLUE_SIZE, &helper.blue_size); | |
| 283 config_->GetAttrib(EGL_GREEN_SIZE, &helper.green_size); | |
| 284 config_->GetAttrib(EGL_RED_SIZE, &helper.red_size); | |
| 285 config_->GetAttrib(EGL_DEPTH_SIZE, &helper.depth_size); | |
| 286 config_->GetAttrib(EGL_STENCIL_SIZE, &helper.stencil_size); | |
| 287 config_->GetAttrib(EGL_SAMPLES, &helper.samples); | |
| 288 config_->GetAttrib(EGL_SAMPLE_BUFFERS, &helper.sample_buffers); | |
| 289 | |
| 290 helper.buffer_preserved = false; | |
| 291 helper.bind_generates_resource = kBindGeneratesResources; | |
| 292 helper.fail_if_major_perf_caveat = false; | |
| 293 helper.lose_context_when_out_of_memory = kLoseContextWhenOutOfMemory; | |
| 294 helper.context_type = gpu::gles2::CONTEXT_TYPE_OPENGLES2; | |
| 295 std::vector<int32_t> attribs; | |
| 296 helper.Serialize(&attribs); | |
| 297 | |
| 298 if (!decoder->Initialize(gl_surface, gl_context.get(), | |
| 299 gl_surface->IsOffscreen(), gl_surface->GetSize(), | |
| 300 gpu::gles2::DisallowedFeatures(), attribs)) { | |
| 301 return false; | |
| 302 } | |
| 303 | |
| 304 command_buffer->SetPutOffsetChangeCallback( | |
| 305 base::Bind(&gpu::CommandExecutor::PutChanged, | |
| 306 base::Unretained(command_executor.get()))); | |
| 307 command_buffer->SetGetBufferChangeCallback( | |
| 308 base::Bind(&gpu::CommandExecutor::SetGetBuffer, | |
| 309 base::Unretained(command_executor.get()))); | |
| 310 | |
| 311 scoped_ptr<gpu::gles2::GLES2CmdHelper> gles2_cmd_helper( | |
| 312 new gpu::gles2::GLES2CmdHelper(command_buffer.get())); | |
| 313 if (!gles2_cmd_helper->Initialize(kCommandBufferSize)) { | |
| 314 decoder->Destroy(true); | |
| 315 return false; | |
| 316 } | |
| 317 | |
| 318 scoped_ptr<gpu::TransferBuffer> transfer_buffer( | |
| 319 new gpu::TransferBuffer(gles2_cmd_helper.get())); | |
| 320 | |
| 321 gles2_cmd_helper_.reset(gles2_cmd_helper.release()); | |
| 322 transfer_buffer_.reset(transfer_buffer.release()); | |
| 323 command_buffer_.reset(command_buffer.release()); | |
| 324 command_executor_.reset(command_executor.release()); | |
| 325 decoder_.reset(decoder.release()); | |
| 326 gl_context_ = gl_context.get(); | |
| 327 | |
| 328 scoped_ptr<gpu::gles2::GLES2Implementation> context( | |
| 329 new gpu::gles2::GLES2Implementation( | |
| 330 gles2_cmd_helper_.get(), nullptr, transfer_buffer_.get(), | |
| 331 kBindGeneratesResources, kLoseContextWhenOutOfMemory, | |
| 332 kSupportClientSideArrays, this)); | |
| 333 | |
| 334 if (!context->Initialize(kTransferBufferSize, kTransferBufferSize / 2, | |
| 335 kTransferBufferSize * 2, | |
| 336 gpu::gles2::GLES2Implementation::kNoLimit)) { | |
| 337 DestroyService(); | |
| 338 return false; | |
| 339 } | |
| 340 | |
| 341 context->EnableFeatureCHROMIUM("pepper3d_allow_buffers_on_multiple_targets"); | |
| 342 context->EnableFeatureCHROMIUM("pepper3d_support_fixed_attribs"); | |
| 343 client_gl_context_.reset(context.release()); | |
| 344 return true; | |
| 345 } | |
| 346 | |
| 347 void Context::DestroyService() { | |
| 348 DCHECK(HasService()); | |
| 349 bool have_context = !WasServiceContextLost(); | |
| 350 // The client gl interface might still be set to current global | |
| 351 // interface. This will be cleaned up in ApplyContextReleased | |
| 352 // with AutoCurrentContextRestore. | |
| 353 client_gl_context_.reset(); | |
| 354 gl_context_ = nullptr; | |
| 355 | |
| 356 transfer_buffer_.reset(); | |
| 357 command_executor_.reset(); | |
| 358 if (decoder_) | |
| 359 decoder_->Destroy(have_context); | |
| 360 gles2_cmd_helper_.reset(); | |
| 361 command_buffer_.reset(); | |
| 362 } | |
| 363 | |
| 364 bool Context::HasService() const { | |
| 365 return decoder_ != nullptr; | |
| 366 } | |
| 367 | |
| 368 void Context::MarkServiceContextLost() { | |
| 369 decoder_->MarkContextLost(gpu::error::kMakeCurrentFailed); | |
| 370 } | |
| 371 | |
| 372 bool Context::WasServiceContextLost() const { | |
| 373 return decoder_->WasContextLost(); | |
| 374 } | |
| 375 | |
| 376 bool Context::IsCompatibleSurface(gfx::GLSurface* gl_surface) { | |
| 377 EGLint value = EGL_NONE; | |
| 378 config_->GetAttrib(EGL_SURFACE_TYPE, &value); | |
| 379 bool config_is_offscreen = (value & EGL_PBUFFER_BIT) != 0; | |
| 380 return gl_surface->IsOffscreen() == config_is_offscreen; | |
| 381 } | |
| 382 | |
| 383 bool Context::Flush(gfx::GLSurface* gl_surface) { | |
| 384 if (WasServiceContextLost()) | |
| 385 return false; | |
| 386 if (!gl_context_->MakeCurrent(gl_surface)) { | |
| 387 MarkServiceContextLost(); | |
| 388 return false; | |
| 389 } | |
| 390 client_gl_context_->Flush(); | |
| 391 return true; | |
| 392 } | |
| 393 | |
| 394 } // namespace egl | |
| OLD | NEW |