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 354 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
391 bool use_zero_copy, | 392 bool use_zero_copy, |
392 bool is_overlay_candidate) | 393 bool is_overlay_candidate) |
393 : gpu_memory_buffer_(std::move(gpu_memory_buffer)), | 394 : gpu_memory_buffer_(std::move(gpu_memory_buffer)), |
394 texture_target_(texture_target), | 395 texture_target_(texture_target), |
395 query_type_(query_type), | 396 query_type_(query_type), |
396 use_zero_copy_(use_zero_copy), | 397 use_zero_copy_(use_zero_copy), |
397 is_overlay_candidate_(is_overlay_candidate) {} | 398 is_overlay_candidate_(is_overlay_candidate) {} |
398 | 399 |
399 Buffer::~Buffer() {} | 400 Buffer::~Buffer() {} |
400 | 401 |
401 std::unique_ptr<cc::SingleReleaseCallback> Buffer::ProduceTextureMailbox( | 402 bool Buffer::ProduceTransferableResource( |
402 cc::TextureMailbox* texture_mailbox, | 403 CompositorFrameSinkHolder* compositor_frame_sink_holder, |
404 cc::ResourceId resource_id, | |
403 bool secure_output_only, | 405 bool secure_output_only, |
404 bool client_usage) { | 406 bool client_usage, |
407 cc::TransferableResource* resource) { | |
405 DCHECK(attach_count_); | 408 DCHECK(attach_count_); |
406 DLOG_IF(WARNING, use_count_ && client_usage) | 409 DLOG_IF(WARNING, use_count_ && client_usage) |
407 << "Producing a texture mailbox for a buffer that has not been released"; | 410 << "Producing a texture mailbox for a buffer that has not been released"; |
408 | 411 |
409 // Some clients think that they can reuse a buffer before it's released by | 412 // 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 | 413 // 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 | 414 // 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 | 415 // 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 | 416 // 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 | 417 // type of behavior is detected as having the buffer always be busy will |
(...skipping 11 matching lines...) Expand all Loading... | |
426 texture_.reset(); | 429 texture_.reset(); |
427 | 430 |
428 ui::ContextFactory* context_factory = | 431 ui::ContextFactory* context_factory = |
429 aura::Env::GetInstance()->context_factory(); | 432 aura::Env::GetInstance()->context_factory(); |
430 // Note: This can fail if GPU acceleration has been disabled. | 433 // Note: This can fail if GPU acceleration has been disabled. |
431 scoped_refptr<cc::ContextProvider> context_provider = | 434 scoped_refptr<cc::ContextProvider> context_provider = |
432 context_factory->SharedMainThreadContextProvider(); | 435 context_factory->SharedMainThreadContextProvider(); |
433 if (!context_provider) { | 436 if (!context_provider) { |
434 DLOG(WARNING) << "Failed to acquire a context provider"; | 437 DLOG(WARNING) << "Failed to acquire a context provider"; |
435 Release(); // Decrements the use count | 438 Release(); // Decrements the use count |
436 return nullptr; | 439 resource->id = 0; |
440 resource->size = gfx::Size(); | |
441 return false; | |
437 } | 442 } |
438 | 443 |
444 // The reference to the CompositorFrameSinkHolder keeps it alive until a | |
445 // release callback is received. | |
446 compositor_frame_sink_holder_ = compositor_frame_sink_holder; | |
447 | |
448 resource->id = resource_id; | |
449 resource->format = cc::RGBA_8888; | |
450 resource->filter = GL_LINEAR; | |
451 resource->size = gpu_memory_buffer_->GetSize(); | |
452 | |
439 // Create a new image texture for |gpu_memory_buffer_| with |texture_target_| | 453 // 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 | 454 // if one doesn't already exist. The contents of this buffer are copied to |
441 // |texture| using a call to CopyTexImage. | 455 // |texture| using a call to CopyTexImage. |
442 if (!contents_texture_) { | 456 if (!contents_texture_) { |
443 contents_texture_ = base::MakeUnique<Texture>( | 457 contents_texture_ = base::MakeUnique<Texture>( |
444 context_factory, context_provider.get(), gpu_memory_buffer_.get(), | 458 context_factory, context_provider.get(), gpu_memory_buffer_.get(), |
445 texture_target_, query_type_); | 459 texture_target_, query_type_); |
446 } | 460 } |
447 | 461 |
448 if (use_zero_copy_) { | 462 if (use_zero_copy_) { |
449 // Zero-copy means using the contents texture directly. | 463 // Zero-copy means using the contents texture directly. |
450 Texture* texture = contents_texture_.get(); | 464 Texture* texture = contents_texture_.get(); |
451 | 465 |
452 // This binds the latest contents of this buffer to |texture|. | 466 // This binds the latest contents of this buffer to |texture|. |
453 gpu::SyncToken sync_token = texture->BindTexImage(); | 467 resource->mailbox_holder = gpu::MailboxHolder( |
468 texture->mailbox(), texture->BindTexImage(), texture_target_); | |
454 | 469 |
455 *texture_mailbox = | 470 resource->is_overlay_candidate = is_overlay_candidate_; |
456 cc::TextureMailbox(texture->mailbox(), sync_token, texture_target_, | 471 |
457 gpu_memory_buffer_->GetSize(), is_overlay_candidate_, | |
458 secure_output_only); | |
459 // The contents texture will be released when no longer used by the | 472 // The contents texture will be released when no longer used by the |
460 // compositor. | 473 // compositor. |
461 return cc::SingleReleaseCallback::Create( | 474 compositor_frame_sink_holder_->SetResourceReleaseCallback( |
475 resource_id, | |
462 base::Bind(&Buffer::Texture::ReleaseTexImage, base::Unretained(texture), | 476 base::Bind(&Buffer::Texture::ReleaseTexImage, base::Unretained(texture), |
463 base::Bind(&Buffer::ReleaseContentsTexture, AsWeakPtr(), | 477 base::Bind(&Buffer::ReleaseContentsTexture, AsWeakPtr(), |
464 base::Passed(&contents_texture_)))); | 478 base::Passed(&contents_texture_)))); |
479 return true; | |
465 } | 480 } |
466 | 481 |
467 // Create a mailbox texture that we copy the buffer contents to. | 482 // Create a mailbox texture that we copy the buffer contents to. |
468 if (!texture_) { | 483 if (!texture_) { |
469 texture_ = | 484 texture_ = |
470 base::MakeUnique<Texture>(context_factory, context_provider.get()); | 485 base::MakeUnique<Texture>(context_factory, context_provider.get()); |
471 } | 486 } |
472 | 487 |
473 // Copy the contents of |contents_texture| to |texture| and produce a | 488 // Copy the contents of |contents_texture| to |texture| and produce a |
474 // texture mailbox from the result in |texture|. | 489 // texture mailbox from the result in |texture|. |
475 Texture* contents_texture = contents_texture_.get(); | 490 Texture* contents_texture = contents_texture_.get(); |
476 Texture* texture = texture_.get(); | 491 Texture* texture = texture_.get(); |
477 | 492 |
478 // The contents texture will be released when copy has completed. | 493 // The contents texture will be released when copy has completed. |
479 gpu::SyncToken sync_token = contents_texture->CopyTexImage( | 494 resource->mailbox_holder = gpu::MailboxHolder( |
480 texture, base::Bind(&Buffer::ReleaseContentsTexture, AsWeakPtr(), | 495 texture->mailbox(), |
481 base::Passed(&contents_texture_))); | 496 contents_texture->CopyTexImage( |
482 *texture_mailbox = | 497 texture, base::Bind(&Buffer::ReleaseContentsTexture, AsWeakPtr(), |
483 cc::TextureMailbox(texture->mailbox(), sync_token, GL_TEXTURE_2D, | 498 base::Passed(&contents_texture_))), |
484 gpu_memory_buffer_->GetSize(), | 499 GL_TEXTURE_2D); |
485 false /* is_overlay_candidate */, secure_output_only); | 500 resource->is_overlay_candidate = false; |
501 | |
486 // The mailbox texture will be released when no longer used by the | 502 // The mailbox texture will be released when no longer used by the |
487 // compositor. | 503 // compositor. |
488 return cc::SingleReleaseCallback::Create( | 504 compositor_frame_sink_holder_->SetResourceReleaseCallback( |
505 resource_id, | |
489 base::Bind(&Buffer::Texture::Release, base::Unretained(texture), | 506 base::Bind(&Buffer::Texture::Release, base::Unretained(texture), |
490 base::Bind(&Buffer::ReleaseTexture, AsWeakPtr(), | 507 base::Bind(&Buffer::ReleaseTexture, AsWeakPtr(), |
491 base::Passed(&texture_)))); | 508 base::Passed(&texture_)))); |
509 return true; | |
492 } | 510 } |
493 | 511 |
494 void Buffer::OnAttach() { | 512 void Buffer::OnAttach() { |
495 DLOG_IF(WARNING, attach_count_ > 0u) | 513 DLOG_IF(WARNING, attach_count_ > 0u) |
496 << "Reattaching a buffer that is already attached to another surface."; | 514 << "Reattaching a buffer that is already attached to another surface."; |
497 attach_count_++; | 515 attach_count_++; |
498 } | 516 } |
499 | 517 |
500 void Buffer::OnDetach() { | 518 void Buffer::OnDetach() { |
501 DCHECK_GT(attach_count_, 0u); | 519 DCHECK_GT(attach_count_, 0u); |
(...skipping 25 matching lines...) Expand all Loading... | |
527 CheckReleaseCallback(); | 545 CheckReleaseCallback(); |
528 } | 546 } |
529 | 547 |
530 void Buffer::CheckReleaseCallback() { | 548 void Buffer::CheckReleaseCallback() { |
531 if (attach_count_ || use_count_) | 549 if (attach_count_ || use_count_) |
532 return; | 550 return; |
533 | 551 |
534 // Run release callback to notify the client that buffer has been released. | 552 // Run release callback to notify the client that buffer has been released. |
535 if (!release_callback_.is_null()) | 553 if (!release_callback_.is_null()) |
536 release_callback_.Run(); | 554 release_callback_.Run(); |
555 | |
556 compositor_frame_sink_holder_ = nullptr; | |
537 } | 557 } |
538 | 558 |
539 void Buffer::ReleaseTexture(std::unique_ptr<Texture> texture) { | 559 void Buffer::ReleaseTexture(std::unique_ptr<Texture> texture) { |
540 texture_ = std::move(texture); | 560 texture_ = std::move(texture); |
541 } | 561 } |
542 | 562 |
543 void Buffer::ReleaseContentsTexture(std::unique_ptr<Texture> texture) { | 563 void Buffer::ReleaseContentsTexture(std::unique_ptr<Texture> texture) { |
544 TRACE_EVENT0("exo", "Buffer::ReleaseContentsTexture"); | 564 TRACE_EVENT0("exo", "Buffer::ReleaseContentsTexture"); |
545 | |
reveman
2017/01/03 23:48:34
nit: keep this line
| |
546 contents_texture_ = std::move(texture); | 565 contents_texture_ = std::move(texture); |
547 Release(); | 566 Release(); |
548 } | 567 } |
549 | 568 |
550 } // namespace exo | 569 } // namespace exo |
OLD | NEW |