Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "content/renderer/gpu/renderer_gl_context.h" | 5 #include "content/renderer/gpu/renderer_gl_context.h" |
| 6 | 6 |
| 7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 154 } | 154 } |
| 155 | 155 |
| 156 RendererGLContext* RendererGLContext::CreateViewContext( | 156 RendererGLContext* RendererGLContext::CreateViewContext( |
| 157 GpuChannelHost* channel, | 157 GpuChannelHost* channel, |
| 158 gfx::PluginWindowHandle render_surface, | 158 gfx::PluginWindowHandle render_surface, |
| 159 int render_view_id, | 159 int render_view_id, |
| 160 const char* allowed_extensions, | 160 const char* allowed_extensions, |
| 161 const int32* attrib_list, | 161 const int32* attrib_list, |
| 162 const GURL& active_url) { | 162 const GURL& active_url) { |
| 163 #if defined(ENABLE_GPU) | 163 #if defined(ENABLE_GPU) |
| 164 scoped_ptr<RendererGLContext> context(new RendererGLContext(channel, NULL)); | 164 scoped_ptr<RendererGLContext> context(new RendererGLContext(channel)); |
| 165 if (!context->Initialize( | 165 if (!context->Initialize( |
| 166 true, | 166 true, |
| 167 render_surface, | 167 render_surface, |
| 168 render_view_id, | 168 render_view_id, |
| 169 gfx::Size(), | 169 gfx::Size(), |
| 170 allowed_extensions, | 170 allowed_extensions, |
| 171 attrib_list, | 171 attrib_list, |
| 172 active_url)) | 172 active_url)) |
| 173 return NULL; | 173 return NULL; |
| 174 | 174 |
| 175 return context.release(); | 175 return context.release(); |
| 176 #else | 176 #else |
| 177 return NULL; | 177 return NULL; |
| 178 #endif | 178 #endif |
| 179 } | 179 } |
| 180 | 180 |
| 181 #if defined(OS_MACOSX) | 181 #if defined(OS_MACOSX) |
| 182 void RendererGLContext::ResizeOnscreen(const gfx::Size& size) { | 182 void RendererGLContext::ResizeOnscreen(const gfx::Size& size) { |
| 183 DCHECK(size.width() > 0 && size.height() > 0); | 183 DCHECK(size.width() > 0 && size.height() > 0); |
| 184 size_ = size; | 184 size_ = size; |
| 185 command_buffer_->SetWindowSize(size); | 185 command_buffer_->SetWindowSize(size); |
| 186 } | 186 } |
| 187 #endif | 187 #endif |
| 188 | 188 |
| 189 RendererGLContext* RendererGLContext::CreateOffscreenContext( | 189 RendererGLContext* RendererGLContext::CreateOffscreenContext( |
| 190 GpuChannelHost* channel, | 190 GpuChannelHost* channel, |
| 191 RendererGLContext* parent, | |
| 192 const gfx::Size& size, | 191 const gfx::Size& size, |
| 193 const char* allowed_extensions, | 192 const char* allowed_extensions, |
| 194 const int32* attrib_list, | 193 const int32* attrib_list, |
| 195 const GURL& active_url) { | 194 const GURL& active_url) { |
| 196 #if defined(ENABLE_GPU) | 195 #if defined(ENABLE_GPU) |
| 197 scoped_ptr<RendererGLContext> context(new RendererGLContext(channel, parent)); | 196 scoped_ptr<RendererGLContext> context(new RendererGLContext(channel)); |
| 198 if (!context->Initialize( | 197 if (!context->Initialize( |
| 199 false, | 198 false, |
| 200 gfx::kNullPluginWindow, | 199 gfx::kNullPluginWindow, |
| 201 0, | 200 0, |
| 202 size, | 201 size, |
| 203 allowed_extensions, | 202 allowed_extensions, |
| 204 attrib_list, | 203 attrib_list, |
| 205 active_url)) | 204 active_url)) |
| 206 return NULL; | 205 return NULL; |
| 207 | 206 |
| 208 return context.release(); | 207 return context.release(); |
| 209 #else | 208 #else |
| 210 return NULL; | 209 return NULL; |
| 211 #endif | 210 #endif |
| 212 } | 211 } |
| 213 | 212 |
| 213 bool RendererGLContext::SetParent(RendererGLContext* new_parent) { | |
| 214 // Allocate a texture ID with respect to the parent and change the parent. | |
| 215 uint32 new_parent_texture_id = 0; | |
| 216 if (new_parent) { | |
| 217 TRACE_EVENT0("gpu", "RendererGLContext::SetParent::flushParent"); | |
| 218 // Flush any remaining commands in the parent context to make sure the | |
| 219 // texture id accounting stays consistent. | |
| 220 int32 token = new_parent->gles2_helper_->InsertToken(); | |
| 221 new_parent->gles2_helper_->WaitForToken(token); | |
| 222 new_parent_texture_id = new_parent->gles2_implementation_->MakeTextureId(); | |
| 223 | |
| 224 if (!command_buffer_->SetParent(new_parent->command_buffer_, | |
| 225 new_parent_texture_id)) { | |
|
piman
2011/06/22 00:10:59
Drive-by: since this happens out-of-band (with a m
apatrick_chromium
2011/06/22 00:21:03
It should be alright at the moment because SetPare
| |
| 226 new_parent->gles2_implementation_->FreeTextureId(parent_texture_id_); | |
| 227 return false; | |
| 228 } | |
| 229 } else { | |
| 230 if (!command_buffer_->SetParent(NULL, 0)) | |
| 231 return false; | |
| 232 } | |
| 233 | |
| 234 // Free the previous parent's texture ID. | |
| 235 if (parent_.get() && parent_texture_id_ != 0) | |
| 236 parent_->gles2_implementation_->FreeTextureId(parent_texture_id_); | |
| 237 | |
| 238 if (new_parent) { | |
| 239 parent_ = new_parent->AsWeakPtr(); | |
| 240 parent_texture_id_ = new_parent_texture_id; | |
| 241 } else { | |
| 242 parent_.reset(); | |
| 243 parent_texture_id_ = 0; | |
| 244 } | |
| 245 | |
| 246 return true; | |
| 247 } | |
| 248 | |
| 214 void RendererGLContext::ResizeOffscreen(const gfx::Size& size) { | 249 void RendererGLContext::ResizeOffscreen(const gfx::Size& size) { |
| 215 DCHECK(size.width() > 0 && size.height() > 0); | 250 DCHECK(size.width() > 0 && size.height() > 0); |
| 216 if (size_ != size) { | 251 if (size_ != size) { |
| 217 command_buffer_->ResizeOffscreenFrameBuffer(size); | 252 command_buffer_->ResizeOffscreenFrameBuffer(size); |
| 218 size_ = size; | 253 size_ = size; |
| 219 } | 254 } |
| 220 } | 255 } |
| 221 | 256 |
| 222 uint32 RendererGLContext::GetParentTextureId() { | 257 uint32 RendererGLContext::GetParentTextureId() { |
| 223 return parent_texture_id_; | 258 return parent_texture_id_; |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 335 // TODO(gman): Remove This | 370 // TODO(gman): Remove This |
| 336 void RendererGLContext::DisableShaderTranslation() { | 371 void RendererGLContext::DisableShaderTranslation() { |
| 337 gles2_implementation_->CommandBufferEnableCHROMIUM( | 372 gles2_implementation_->CommandBufferEnableCHROMIUM( |
| 338 PEPPER3D_SKIP_GLSL_TRANSLATION); | 373 PEPPER3D_SKIP_GLSL_TRANSLATION); |
| 339 } | 374 } |
| 340 | 375 |
| 341 gpu::gles2::GLES2Implementation* RendererGLContext::GetImplementation() { | 376 gpu::gles2::GLES2Implementation* RendererGLContext::GetImplementation() { |
| 342 return gles2_implementation_; | 377 return gles2_implementation_; |
| 343 } | 378 } |
| 344 | 379 |
| 345 RendererGLContext::RendererGLContext(GpuChannelHost* channel, | 380 RendererGLContext::RendererGLContext(GpuChannelHost* channel) |
| 346 RendererGLContext* parent) | |
| 347 : channel_(channel), | 381 : channel_(channel), |
| 348 parent_(parent ? | 382 parent_(base::WeakPtr<RendererGLContext>()), |
| 349 parent->AsWeakPtr() : base::WeakPtr<RendererGLContext>()), | |
| 350 parent_texture_id_(0), | 383 parent_texture_id_(0), |
| 351 child_to_parent_latch_(kInvalidLatchId), | 384 child_to_parent_latch_(kInvalidLatchId), |
| 352 parent_to_child_latch_(kInvalidLatchId), | 385 parent_to_child_latch_(kInvalidLatchId), |
| 353 latch_transfer_buffer_id_(-1), | 386 latch_transfer_buffer_id_(-1), |
| 354 command_buffer_(NULL), | 387 command_buffer_(NULL), |
| 355 gles2_helper_(NULL), | 388 gles2_helper_(NULL), |
| 356 transfer_buffer_id_(-1), | 389 transfer_buffer_id_(-1), |
| 357 gles2_implementation_(NULL), | 390 gles2_implementation_(NULL), |
| 358 last_error_(SUCCESS), | 391 last_error_(SUCCESS), |
| 359 frame_number_(0) { | 392 frame_number_(0) { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 370 DCHECK(size.width() >= 0 && size.height() >= 0); | 403 DCHECK(size.width() >= 0 && size.height() >= 0); |
| 371 TRACE_EVENT2("gpu", "RendererGLContext::Initialize", | 404 TRACE_EVENT2("gpu", "RendererGLContext::Initialize", |
| 372 "on_screen", onscreen, "num_pixels", size.GetArea()); | 405 "on_screen", onscreen, "num_pixels", size.GetArea()); |
| 373 | 406 |
| 374 if (channel_->state() != GpuChannelHost::kConnected) | 407 if (channel_->state() != GpuChannelHost::kConnected) |
| 375 return false; | 408 return false; |
| 376 | 409 |
| 377 // Ensure the gles2 library is initialized first in a thread safe way. | 410 // Ensure the gles2 library is initialized first in a thread safe way. |
| 378 g_gles2_initializer.Get(); | 411 g_gles2_initializer.Get(); |
| 379 | 412 |
| 380 // Allocate a frame buffer ID with respect to the parent. | |
| 381 if (parent_.get()) { | |
| 382 TRACE_EVENT0("gpu", "RendererGLContext::Initialize::flushParent"); | |
| 383 // Flush any remaining commands in the parent context to make sure the | |
| 384 // texture id accounting stays consistent. | |
| 385 int32 token = parent_->gles2_helper_->InsertToken(); | |
| 386 parent_->gles2_helper_->WaitForToken(token); | |
| 387 parent_texture_id_ = parent_->gles2_implementation_->MakeTextureId(); | |
| 388 } | |
| 389 | |
| 390 std::vector<int32> attribs; | 413 std::vector<int32> attribs; |
| 391 while (attrib_list) { | 414 while (attrib_list) { |
| 392 int32 attrib = *attrib_list++; | 415 int32 attrib = *attrib_list++; |
| 393 switch (attrib) { | 416 switch (attrib) { |
| 394 // Known attributes | 417 // Known attributes |
| 395 case ALPHA_SIZE: | 418 case ALPHA_SIZE: |
| 396 case BLUE_SIZE: | 419 case BLUE_SIZE: |
| 397 case GREEN_SIZE: | 420 case GREEN_SIZE: |
| 398 case RED_SIZE: | 421 case RED_SIZE: |
| 399 case DEPTH_SIZE: | 422 case DEPTH_SIZE: |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 424 TRACE_EVENT0("gpu", | 447 TRACE_EVENT0("gpu", |
| 425 "RendererGLContext::Initialize::CreateViewCommandBuffer"); | 448 "RendererGLContext::Initialize::CreateViewCommandBuffer"); |
| 426 command_buffer_ = channel_->CreateViewCommandBuffer( | 449 command_buffer_ = channel_->CreateViewCommandBuffer( |
| 427 render_surface, | 450 render_surface, |
| 428 render_view_id, | 451 render_view_id, |
| 429 allowed_extensions, | 452 allowed_extensions, |
| 430 attribs, | 453 attribs, |
| 431 active_url); | 454 active_url); |
| 432 } | 455 } |
| 433 } else { | 456 } else { |
| 434 CommandBufferProxy* parent_command_buffer = | |
| 435 parent_.get() ? parent_->command_buffer_ : NULL; | |
| 436 command_buffer_ = channel_->CreateOffscreenCommandBuffer( | 457 command_buffer_ = channel_->CreateOffscreenCommandBuffer( |
| 437 parent_command_buffer, | |
| 438 size, | 458 size, |
| 439 allowed_extensions, | 459 allowed_extensions, |
| 440 attribs, | 460 attribs, |
| 441 parent_texture_id_, | |
| 442 active_url); | 461 active_url); |
| 443 } | 462 } |
| 444 if (!command_buffer_) { | 463 if (!command_buffer_) { |
| 445 Destroy(); | 464 Destroy(); |
| 446 return false; | 465 return false; |
| 447 } | 466 } |
| 448 | 467 |
| 449 { | 468 { |
| 450 TRACE_EVENT0("gpu", | 469 TRACE_EVENT0("gpu", |
| 451 "RendererGLContext::Initialize::InitializeCommandBuffer"); | 470 "RendererGLContext::Initialize::InitializeCommandBuffer"); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 494 latch_transfer_buffer_id_ = command_buffer_->RegisterTransferBuffer( | 513 latch_transfer_buffer_id_ = command_buffer_->RegisterTransferBuffer( |
| 495 latch_shm->shared_memory(), LatchAllocator::size(), | 514 latch_shm->shared_memory(), LatchAllocator::size(), |
| 496 gpu::kLatchSharedMemoryId); | 515 gpu::kLatchSharedMemoryId); |
| 497 if (latch_transfer_buffer_id_ != gpu::kLatchSharedMemoryId) { | 516 if (latch_transfer_buffer_id_ != gpu::kLatchSharedMemoryId) { |
| 498 Destroy(); | 517 Destroy(); |
| 499 return false; | 518 return false; |
| 500 } | 519 } |
| 501 | 520 |
| 502 // If this is a child context, setup latches for synchronization between child | 521 // If this is a child context, setup latches for synchronization between child |
| 503 // and parent. | 522 // and parent. |
| 504 if (parent_.get()) { | 523 if (!CreateLatch(&child_to_parent_latch_) || |
| 505 if (!CreateLatch(&child_to_parent_latch_) || | 524 !CreateLatch(&parent_to_child_latch_)) { |
| 506 !CreateLatch(&parent_to_child_latch_)) { | 525 Destroy(); |
| 507 Destroy(); | 526 return false; |
| 508 return false; | |
| 509 } | |
| 510 } | 527 } |
| 511 | 528 |
| 512 // Create the object exposing the OpenGL API. | 529 // Create the object exposing the OpenGL API. |
| 513 gles2_implementation_ = new gpu::gles2::GLES2Implementation( | 530 gles2_implementation_ = new gpu::gles2::GLES2Implementation( |
| 514 gles2_helper_, | 531 gles2_helper_, |
| 515 transfer_buffer.size, | 532 transfer_buffer.size, |
| 516 transfer_buffer.ptr, | 533 transfer_buffer.ptr, |
| 517 transfer_buffer_id_, | 534 transfer_buffer_id_, |
| 518 false); | 535 false); |
| 519 | 536 |
| 520 size_ = size; | 537 size_ = size; |
| 521 | 538 |
| 522 return true; | 539 return true; |
| 523 } | 540 } |
| 524 | 541 |
| 525 void RendererGLContext::Destroy() { | 542 void RendererGLContext::Destroy() { |
| 526 if (parent_.get() && parent_texture_id_ != 0) { | 543 SetParent(NULL); |
| 527 parent_->gles2_implementation_->FreeTextureId(parent_texture_id_); | |
| 528 parent_texture_id_ = 0; | |
| 529 } | |
| 530 | 544 |
| 531 delete gles2_implementation_; | 545 delete gles2_implementation_; |
| 532 gles2_implementation_ = NULL; | 546 gles2_implementation_ = NULL; |
| 533 | 547 |
| 534 if (child_to_parent_latch_ != kInvalidLatchId) { | 548 if (child_to_parent_latch_ != kInvalidLatchId) { |
| 535 DestroyLatch(child_to_parent_latch_); | 549 DestroyLatch(child_to_parent_latch_); |
| 536 child_to_parent_latch_ = kInvalidLatchId; | 550 child_to_parent_latch_ = kInvalidLatchId; |
| 537 } | 551 } |
| 538 if (parent_to_child_latch_ != kInvalidLatchId) { | 552 if (parent_to_child_latch_ != kInvalidLatchId) { |
| 539 DestroyLatch(parent_to_child_latch_); | 553 DestroyLatch(parent_to_child_latch_); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 586 return false; | 600 return false; |
| 587 } | 601 } |
| 588 | 602 |
| 589 bool RendererGLContext::GetChildToParentLatch(uint32* child_to_parent_latch) { | 603 bool RendererGLContext::GetChildToParentLatch(uint32* child_to_parent_latch) { |
| 590 if (parent_.get()) { | 604 if (parent_.get()) { |
| 591 *child_to_parent_latch = child_to_parent_latch_; | 605 *child_to_parent_latch = child_to_parent_latch_; |
| 592 return true; | 606 return true; |
| 593 } | 607 } |
| 594 return false; | 608 return false; |
| 595 } | 609 } |
| OLD | NEW |