task_forwarding_sequence.cc 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2019 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. #include "android_webview/browser/gfx/task_forwarding_sequence.h"
  5. #include "android_webview/browser/gfx/task_queue_webview.h"
  6. #include "base/bind.h"
  7. #include "base/synchronization/waitable_event.h"
  8. #include "base/trace_event/trace_event.h"
  9. #include "gpu/command_buffer/service/sync_point_manager.h"
  10. namespace android_webview {
  11. TaskForwardingSequence::TaskForwardingSequence(
  12. TaskQueueWebView* task_queue,
  13. gpu::SyncPointManager* sync_point_manager)
  14. : gpu::SingleTaskSequence(),
  15. task_queue_(task_queue),
  16. sync_point_manager_(sync_point_manager),
  17. sync_point_order_data_(sync_point_manager_->CreateSyncPointOrderData()) {}
  18. TaskForwardingSequence::~TaskForwardingSequence() {
  19. sync_point_order_data_->Destroy();
  20. }
  21. // CommandBufferTaskExecutor::Sequence implementation.
  22. gpu::SequenceId TaskForwardingSequence::GetSequenceId() {
  23. return sync_point_order_data_->sequence_id();
  24. }
  25. bool TaskForwardingSequence::ShouldYield() {
  26. return false;
  27. }
  28. void TaskForwardingSequence::ScheduleTask(
  29. base::OnceClosure task,
  30. std::vector<gpu::SyncToken> sync_token_fences,
  31. ReportingCallback report_callback) {
  32. uint32_t order_num = sync_point_order_data_->GenerateUnprocessedOrderNumber();
  33. // |sync_point_manager_| is global so it's safe to pass raw pointer.
  34. // sync_point_order_data_ is ThreadSafe.
  35. task_queue_->ScheduleTask(
  36. base::BindOnce(&TaskForwardingSequence::RunTask, std::move(task),
  37. std::move(sync_token_fences), order_num,
  38. sync_point_manager_, sync_point_order_data_),
  39. false /* out_of_order */);
  40. }
  41. void TaskForwardingSequence::ScheduleOrRetainTask(
  42. base::OnceClosure task,
  43. std::vector<gpu::SyncToken> sync_token_fences,
  44. ReportingCallback report_callback) {
  45. uint32_t order_num = sync_point_order_data_->GenerateUnprocessedOrderNumber();
  46. // |sync_point_manager_| is global so it's safe to pass raw pointer.
  47. // sync_point_order_data_ is ThreadSafe.
  48. task_queue_->ScheduleOrRetainTask(
  49. base::BindOnce(&TaskForwardingSequence::RunTask, std::move(task),
  50. std::move(sync_token_fences), order_num,
  51. sync_point_manager_, sync_point_order_data_));
  52. }
  53. // Should not be called because tasks aren't reposted to wait for sync tokens,
  54. // or for yielding execution since ShouldYield() returns false.
  55. void TaskForwardingSequence::ContinueTask(base::OnceClosure task) {
  56. NOTREACHED();
  57. }
  58. // Method to wrap scheduled task with the order number processing required for
  59. // sync tokens.
  60. void TaskForwardingSequence::RunTask(
  61. base::OnceClosure task,
  62. std::vector<gpu::SyncToken> sync_token_fences,
  63. uint32_t order_num,
  64. gpu::SyncPointManager* sync_point_manager,
  65. scoped_refptr<gpu::SyncPointOrderData> sync_point_order_data) {
  66. // Block thread when waiting for sync token. This avoids blocking when we
  67. // encounter the wait command later.
  68. for (const auto& sync_token : sync_token_fences) {
  69. base::WaitableEvent completion;
  70. if (sync_point_manager->Wait(
  71. sync_token, sync_point_order_data->sequence_id(), order_num,
  72. base::BindOnce(&base::WaitableEvent::Signal,
  73. base::Unretained(&completion)))) {
  74. TRACE_EVENT0("android_webview",
  75. "TaskForwardingSequence::RunTask::WaitSyncToken");
  76. completion.Wait();
  77. }
  78. }
  79. sync_point_order_data->BeginProcessingOrderNumber(order_num);
  80. std::move(task).Run();
  81. sync_point_order_data->FinishProcessingOrderNumber(order_num);
  82. }
  83. } // namespace android_webview