OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/exo/buffer.h" | 5 #include "components/exo/buffer.h" |
6 | 6 |
7 #include <GLES2/gl2.h> | 7 #include <GLES2/gl2.h> |
8 #include <GLES2/gl2ext.h> | 8 #include <GLES2/gl2ext.h> |
9 #include <GLES2/gl2extchromium.h> | 9 #include <GLES2/gl2extchromium.h> |
10 #include <stdint.h> | 10 #include <stdint.h> |
11 | 11 |
12 #include <algorithm> | 12 #include <algorithm> |
13 #include <utility> | 13 #include <utility> |
14 | 14 |
15 #include "base/callback_helpers.h" | 15 #include "base/callback_helpers.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/memory/ptr_util.h" | 18 #include "base/memory/ptr_util.h" |
19 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
20 #include "base/threading/thread_task_runner_handle.h" | 20 #include "base/threading/thread_task_runner_handle.h" |
21 #include "base/time/time.h" | 21 #include "base/time/time.h" |
22 #include "base/trace_event/trace_event.h" | 22 #include "base/trace_event/trace_event.h" |
23 #include "base/trace_event/trace_event_argument.h" | 23 #include "base/trace_event/trace_event_argument.h" |
24 #include "cc/output/context_provider.h" | 24 #include "cc/output/context_provider.h" |
25 #include "cc/resources/single_release_callback.h" | 25 #include "cc/resources/single_release_callback.h" |
26 #include "cc/resources/texture_mailbox.h" | 26 #include "cc/resources/texture_mailbox.h" |
| 27 #include "components/exo/compositor_frame_sink_holder.h" |
27 #include "gpu/command_buffer/client/context_support.h" | 28 #include "gpu/command_buffer/client/context_support.h" |
28 #include "gpu/command_buffer/client/gles2_interface.h" | 29 #include "gpu/command_buffer/client/gles2_interface.h" |
29 #include "ui/aura/env.h" | 30 #include "ui/aura/env.h" |
30 #include "ui/compositor/compositor.h" | 31 #include "ui/compositor/compositor.h" |
31 #include "ui/gfx/gpu_memory_buffer.h" | 32 #include "ui/gfx/gpu_memory_buffer.h" |
32 | 33 |
33 namespace exo { | 34 namespace exo { |
34 namespace { | 35 namespace { |
35 | 36 |
36 // The amount of time before we wait for release queries using | 37 // The amount of time before we wait for release queries using |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 if (context_provider_) { | 214 if (context_provider_) { |
214 gpu::gles2::GLES2Interface* gles2 = context_provider_->ContextGL(); | 215 gpu::gles2::GLES2Interface* gles2 = context_provider_->ContextGL(); |
215 return gles2->GetGraphicsResetStatusKHR() != GL_NO_ERROR; | 216 return gles2->GetGraphicsResetStatusKHR() != GL_NO_ERROR; |
216 } | 217 } |
217 return true; | 218 return true; |
218 } | 219 } |
219 | 220 |
220 void Buffer::Texture::Release(const base::Closure& callback, | 221 void Buffer::Texture::Release(const base::Closure& callback, |
221 const gpu::SyncToken& sync_token, | 222 const gpu::SyncToken& sync_token, |
222 bool is_lost) { | 223 bool is_lost) { |
| 224 printf("Buffer::Texture::Release()\n"); |
223 if (context_provider_) { | 225 if (context_provider_) { |
224 gpu::gles2::GLES2Interface* gles2 = context_provider_->ContextGL(); | 226 gpu::gles2::GLES2Interface* gles2 = context_provider_->ContextGL(); |
225 if (sync_token.HasData()) | 227 if (sync_token.HasData()) |
226 gles2->WaitSyncTokenCHROMIUM(sync_token.GetConstData()); | 228 gles2->WaitSyncTokenCHROMIUM(sync_token.GetConstData()); |
227 } | 229 } |
228 | 230 |
229 // Run callback as texture can be reused immediately after waiting for sync | 231 // Run callback as texture can be reused immediately after waiting for sync |
230 // token. | 232 // token. |
231 callback.Run(); | 233 callback.Run(); |
232 } | 234 } |
(...skipping 15 matching lines...) Expand all Loading... |
248 uint64_t fence_sync = gles2->InsertFenceSyncCHROMIUM(); | 250 uint64_t fence_sync = gles2->InsertFenceSyncCHROMIUM(); |
249 gles2->OrderingBarrierCHROMIUM(); | 251 gles2->OrderingBarrierCHROMIUM(); |
250 gles2->GenUnverifiedSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); | 252 gles2->GenUnverifiedSyncTokenCHROMIUM(fence_sync, sync_token.GetData()); |
251 } | 253 } |
252 return sync_token; | 254 return sync_token; |
253 } | 255 } |
254 | 256 |
255 void Buffer::Texture::ReleaseTexImage(const base::Closure& callback, | 257 void Buffer::Texture::ReleaseTexImage(const base::Closure& callback, |
256 const gpu::SyncToken& sync_token, | 258 const gpu::SyncToken& sync_token, |
257 bool is_lost) { | 259 bool is_lost) { |
| 260 printf("Buffer::Texture::ReleaseTexImage()\n"); |
258 if (context_provider_) { | 261 if (context_provider_) { |
259 gpu::gles2::GLES2Interface* gles2 = context_provider_->ContextGL(); | 262 gpu::gles2::GLES2Interface* gles2 = context_provider_->ContextGL(); |
260 if (sync_token.HasData()) | 263 if (sync_token.HasData()) |
261 gles2->WaitSyncTokenCHROMIUM(sync_token.GetConstData()); | 264 gles2->WaitSyncTokenCHROMIUM(sync_token.GetConstData()); |
262 gles2->ActiveTexture(GL_TEXTURE0); | 265 gles2->ActiveTexture(GL_TEXTURE0); |
263 gles2->BindTexture(texture_target_, texture_id_); | 266 gles2->BindTexture(texture_target_, texture_id_); |
264 DCHECK_NE(query_id_, 0u); | 267 DCHECK_NE(query_id_, 0u); |
265 gles2->BeginQueryEXT(query_type_, query_id_); | 268 gles2->BeginQueryEXT(query_type_, query_id_); |
266 gles2->ReleaseTexImage2DCHROMIUM(texture_target_, image_id_); | 269 gles2->ReleaseTexImage2DCHROMIUM(texture_target_, image_id_); |
267 gles2->EndQueryEXT(query_type_); | 270 gles2->EndQueryEXT(query_type_); |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 bool use_zero_copy, | 394 bool use_zero_copy, |
392 bool is_overlay_candidate) | 395 bool is_overlay_candidate) |
393 : gpu_memory_buffer_(std::move(gpu_memory_buffer)), | 396 : gpu_memory_buffer_(std::move(gpu_memory_buffer)), |
394 texture_target_(texture_target), | 397 texture_target_(texture_target), |
395 query_type_(query_type), | 398 query_type_(query_type), |
396 use_zero_copy_(use_zero_copy), | 399 use_zero_copy_(use_zero_copy), |
397 is_overlay_candidate_(is_overlay_candidate) {} | 400 is_overlay_candidate_(is_overlay_candidate) {} |
398 | 401 |
399 Buffer::~Buffer() {} | 402 Buffer::~Buffer() {} |
400 | 403 |
401 std::unique_ptr<cc::SingleReleaseCallback> Buffer::ProduceTextureMailbox( | 404 bool Buffer::ProduceTransferableResource( |
402 cc::TextureMailbox* texture_mailbox, | 405 CompositorFrameSinkHolder* compositor_frame_sink_holder, |
| 406 cc::ResourceId resource_id, |
403 bool secure_output_only, | 407 bool secure_output_only, |
404 bool client_usage) { | 408 bool client_usage, |
| 409 cc::TransferableResource* resource) { |
405 DCHECK(attach_count_); | 410 DCHECK(attach_count_); |
406 DLOG_IF(WARNING, use_count_ && client_usage) | 411 DLOG_IF(WARNING, use_count_ && client_usage) |
407 << "Producing a texture mailbox for a buffer that has not been released"; | 412 << "Producing a texture mailbox for a buffer that has not been released"; |
408 | 413 |
409 // Some clients think that they can reuse a buffer before it's released by | 414 // Some clients think that they can reuse a buffer before it's released by |
410 // performing a fast blit into the buffer. This behavior is bad as it prevents | 415 // performing a fast blit into the buffer. This behavior is bad as it prevents |
411 // the client from knowing when the buffer is actually released (e.g. the | 416 // the client from knowing when the buffer is actually released (e.g. the |
412 // release notification for the previous use of buffer can arrive after the | 417 // release notification for the previous use of buffer can arrive after the |
413 // buffer has been reused). We stop running the release callback when this | 418 // buffer has been reused). We stop running the release callback when this |
414 // type of behavior is detected as having the buffer always be busy will | 419 // type of behavior is detected as having the buffer always be busy will |
(...skipping 11 matching lines...) Expand all Loading... |
426 texture_.reset(); | 431 texture_.reset(); |
427 | 432 |
428 ui::ContextFactory* context_factory = | 433 ui::ContextFactory* context_factory = |
429 aura::Env::GetInstance()->context_factory(); | 434 aura::Env::GetInstance()->context_factory(); |
430 // Note: This can fail if GPU acceleration has been disabled. | 435 // Note: This can fail if GPU acceleration has been disabled. |
431 scoped_refptr<cc::ContextProvider> context_provider = | 436 scoped_refptr<cc::ContextProvider> context_provider = |
432 context_factory->SharedMainThreadContextProvider(); | 437 context_factory->SharedMainThreadContextProvider(); |
433 if (!context_provider) { | 438 if (!context_provider) { |
434 DLOG(WARNING) << "Failed to acquire a context provider"; | 439 DLOG(WARNING) << "Failed to acquire a context provider"; |
435 Release(); // Decrements the use count | 440 Release(); // Decrements the use count |
436 return nullptr; | 441 resource->id = 0; |
| 442 resource->size = gfx::Size(); |
| 443 return false; |
437 } | 444 } |
438 | 445 |
| 446 // The reference to the CompositorFrameSinkHolder keeps it alive until a |
| 447 // release callback is received. |
| 448 compositor_frame_sink_holder_ = compositor_frame_sink_holder; |
| 449 |
| 450 resource->id = resource_id; |
| 451 resource->format = cc::RGBA_8888; |
| 452 resource->filter = GL_LINEAR; |
| 453 resource->size = gpu_memory_buffer_->GetSize(); |
| 454 |
439 // Create a new image texture for |gpu_memory_buffer_| with |texture_target_| | 455 // Create a new image texture for |gpu_memory_buffer_| with |texture_target_| |
440 // if one doesn't already exist. The contents of this buffer are copied to | 456 // if one doesn't already exist. The contents of this buffer are copied to |
441 // |texture| using a call to CopyTexImage. | 457 // |texture| using a call to CopyTexImage. |
442 if (!contents_texture_) { | 458 if (!contents_texture_) { |
443 contents_texture_ = base::MakeUnique<Texture>( | 459 contents_texture_ = base::MakeUnique<Texture>( |
444 context_factory, context_provider.get(), gpu_memory_buffer_.get(), | 460 context_factory, context_provider.get(), gpu_memory_buffer_.get(), |
445 texture_target_, query_type_); | 461 texture_target_, query_type_); |
446 } | 462 } |
447 | 463 |
448 if (use_zero_copy_) { | 464 if (use_zero_copy_) { |
449 // Zero-copy means using the contents texture directly. | 465 // Zero-copy means using the contents texture directly. |
450 Texture* texture = contents_texture_.get(); | 466 Texture* texture = contents_texture_.get(); |
451 | 467 |
452 // This binds the latest contents of this buffer to |texture|. | 468 // This binds the latest contents of this buffer to |texture|. |
453 gpu::SyncToken sync_token = texture->BindTexImage(); | 469 resource->mailbox_holder = gpu::MailboxHolder( |
| 470 texture->mailbox(), texture->BindTexImage(), texture_target_); |
454 | 471 |
455 *texture_mailbox = | 472 resource->is_overlay_candidate = is_overlay_candidate_; |
456 cc::TextureMailbox(texture->mailbox(), sync_token, texture_target_, | 473 |
457 gpu_memory_buffer_->GetSize(), is_overlay_candidate_, | 474 cc::ReleaseCallback cb = |
458 secure_output_only); | 475 base::Bind(&Buffer::Texture::ReleaseTexImage, base::Unretained(texture), |
| 476 base::Bind(&Buffer::ReleaseContentsTexture, AsWeakPtr(), |
| 477 base::Passed(&contents_texture_))); |
459 // The contents texture will be released when no longer used by the | 478 // The contents texture will be released when no longer used by the |
460 // compositor. | 479 // compositor. |
461 return cc::SingleReleaseCallback::Create( | 480 compositor_frame_sink_holder_->SetResourceReleaseCallback( |
462 base::Bind(&Buffer::Texture::ReleaseTexImage, base::Unretained(texture), | 481 resource_id, base::WrapUnique(&cb)); |
463 base::Bind(&Buffer::ReleaseContentsTexture, AsWeakPtr(), | 482 return true; |
464 base::Passed(&contents_texture_)))); | |
465 } | 483 } |
466 | 484 |
467 // Create a mailbox texture that we copy the buffer contents to. | 485 // Create a mailbox texture that we copy the buffer contents to. |
468 if (!texture_) { | 486 if (!texture_) { |
469 texture_ = | 487 texture_ = |
470 base::MakeUnique<Texture>(context_factory, context_provider.get()); | 488 base::MakeUnique<Texture>(context_factory, context_provider.get()); |
471 } | 489 } |
472 | 490 |
473 // Copy the contents of |contents_texture| to |texture| and produce a | 491 // Copy the contents of |contents_texture| to |texture| and produce a |
474 // texture mailbox from the result in |texture|. | 492 // texture mailbox from the result in |texture|. |
475 Texture* contents_texture = contents_texture_.get(); | 493 Texture* contents_texture = contents_texture_.get(); |
476 Texture* texture = texture_.get(); | 494 Texture* texture = texture_.get(); |
477 | 495 |
478 // The contents texture will be released when copy has completed. | 496 // The contents texture will be released when copy has completed. |
479 gpu::SyncToken sync_token = contents_texture->CopyTexImage( | 497 resource->mailbox_holder = gpu::MailboxHolder( |
480 texture, base::Bind(&Buffer::ReleaseContentsTexture, AsWeakPtr(), | 498 texture->mailbox(), |
481 base::Passed(&contents_texture_))); | 499 contents_texture->CopyTexImage( |
482 *texture_mailbox = | 500 texture, base::Bind(&Buffer::ReleaseContentsTexture, AsWeakPtr(), |
483 cc::TextureMailbox(texture->mailbox(), sync_token, GL_TEXTURE_2D, | 501 base::Passed(&contents_texture_))), |
484 gpu_memory_buffer_->GetSize(), | 502 GL_TEXTURE_2D); |
485 false /* is_overlay_candidate */, secure_output_only); | 503 resource->is_overlay_candidate = false; |
| 504 cc::ReleaseCallback callback = |
| 505 base::Bind(&Buffer::Texture::Release, base::Unretained(texture), |
| 506 base::Bind(&Buffer::ReleaseTexture, AsWeakPtr(), |
| 507 base::Passed(&texture_))); |
486 // The mailbox texture will be released when no longer used by the | 508 // The mailbox texture will be released when no longer used by the |
487 // compositor. | 509 // compositor. |
488 return cc::SingleReleaseCallback::Create( | 510 compositor_frame_sink_holder_->SetResourceReleaseCallback( |
489 base::Bind(&Buffer::Texture::Release, base::Unretained(texture), | 511 resource_id, base::WrapUnique(&callback)); |
490 base::Bind(&Buffer::ReleaseTexture, AsWeakPtr(), | 512 return true; |
491 base::Passed(&texture_)))); | |
492 } | 513 } |
493 | 514 |
494 void Buffer::OnAttach() { | 515 void Buffer::OnAttach() { |
495 DLOG_IF(WARNING, attach_count_ > 0u) | 516 DLOG_IF(WARNING, attach_count_ > 0u) |
496 << "Reattaching a buffer that is already attached to another surface."; | 517 << "Reattaching a buffer that is already attached to another surface."; |
497 attach_count_++; | 518 attach_count_++; |
498 } | 519 } |
499 | 520 |
500 void Buffer::OnDetach() { | 521 void Buffer::OnDetach() { |
501 DCHECK_GT(attach_count_, 0u); | 522 DCHECK_GT(attach_count_, 0u); |
(...skipping 25 matching lines...) Expand all Loading... |
527 CheckReleaseCallback(); | 548 CheckReleaseCallback(); |
528 } | 549 } |
529 | 550 |
530 void Buffer::CheckReleaseCallback() { | 551 void Buffer::CheckReleaseCallback() { |
531 if (attach_count_ || use_count_) | 552 if (attach_count_ || use_count_) |
532 return; | 553 return; |
533 | 554 |
534 // Run release callback to notify the client that buffer has been released. | 555 // Run release callback to notify the client that buffer has been released. |
535 if (!release_callback_.is_null()) | 556 if (!release_callback_.is_null()) |
536 release_callback_.Run(); | 557 release_callback_.Run(); |
| 558 |
| 559 compositor_frame_sink_holder_ = nullptr; |
537 } | 560 } |
538 | 561 |
539 void Buffer::ReleaseTexture(std::unique_ptr<Texture> texture) { | 562 void Buffer::ReleaseTexture(std::unique_ptr<Texture> texture) { |
540 texture_ = std::move(texture); | 563 printf("Buffer::ReleaseTexture()\n"); |
| 564 if (this) |
| 565 texture_ = std::move(texture); |
541 } | 566 } |
542 | 567 |
543 void Buffer::ReleaseContentsTexture(std::unique_ptr<Texture> texture) { | 568 void Buffer::ReleaseContentsTexture(std::unique_ptr<Texture> texture) { |
544 TRACE_EVENT0("exo", "Buffer::ReleaseContentsTexture"); | 569 TRACE_EVENT0("exo", "Buffer::ReleaseContentsTexture"); |
545 | 570 printf("Buffer::ReleaseContentsTexture()\n"); |
546 contents_texture_ = std::move(texture); | 571 if (this) { |
547 Release(); | 572 contents_texture_ = std::move(texture); |
| 573 Release(); |
| 574 } |
548 } | 575 } |
549 | 576 |
550 } // namespace exo | 577 } // namespace exo |
OLD | NEW |