display_webview.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2021 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/display_webview.h"
  5. #include "android_webview/browser/gfx/overlay_processor_webview.h"
  6. #include "base/memory/ptr_util.h"
  7. #include "components/viz/common/features.h"
  8. #include "components/viz/service/display/overlay_processor_stub.h"
  9. #include "components/viz/service/frame_sinks/frame_sink_manager_impl.h"
  10. #include "gpu/config/gpu_finch_features.h"
  11. namespace android_webview {
  12. std::unique_ptr<DisplayWebView> DisplayWebView::Create(
  13. const viz::RendererSettings& settings,
  14. const viz::DebugRendererSettings* debug_settings,
  15. const viz::FrameSinkId& frame_sink_id,
  16. std::unique_ptr<viz::DisplayCompositorMemoryAndTaskController>
  17. gpu_dependency,
  18. std::unique_ptr<viz::OutputSurface> output_surface,
  19. viz::FrameSinkManagerImpl* frame_sink_manager,
  20. RootFrameSink* root_frame_sink) {
  21. std::unique_ptr<viz::OverlayProcessorInterface> overlay_processor;
  22. OverlayProcessorWebView* overlay_processor_webview_raw = nullptr;
  23. if (features::IsAndroidSurfaceControlEnabled()) {
  24. // TODO(crbug.com/1039876): This is to help triage bugs on pre-release
  25. // android. Remove this log once feature is controlled only by feature flag
  26. // or launched.
  27. LOG(WARNING) << "WebView overlays are enabled!";
  28. auto overlay_processor_webview = std::make_unique<OverlayProcessorWebView>(
  29. gpu_dependency.get(), frame_sink_manager);
  30. overlay_processor_webview_raw = overlay_processor_webview.get();
  31. overlay_processor = std::move(overlay_processor_webview);
  32. } else {
  33. overlay_processor = std::make_unique<viz::OverlayProcessorStub>();
  34. }
  35. auto scheduler = std::make_unique<DisplaySchedulerWebView>(
  36. root_frame_sink, overlay_processor_webview_raw);
  37. return base::WrapUnique(new DisplayWebView(
  38. settings, debug_settings, frame_sink_id, std::move(gpu_dependency),
  39. std::move(output_surface), std::move(overlay_processor),
  40. std::move(scheduler), overlay_processor_webview_raw, frame_sink_manager));
  41. }
  42. DisplayWebView::DisplayWebView(
  43. const viz::RendererSettings& settings,
  44. const viz::DebugRendererSettings* debug_settings,
  45. const viz::FrameSinkId& frame_sink_id,
  46. std::unique_ptr<viz::DisplayCompositorMemoryAndTaskController>
  47. gpu_dependency,
  48. std::unique_ptr<viz::OutputSurface> output_surface,
  49. std::unique_ptr<viz::OverlayProcessorInterface> overlay_processor,
  50. std::unique_ptr<viz::DisplaySchedulerBase> scheduler,
  51. OverlayProcessorWebView* overlay_processor_webview,
  52. viz::FrameSinkManagerImpl* frame_sink_manager)
  53. : viz::Display(/*bitmap_manager=*/nullptr,
  54. settings,
  55. debug_settings,
  56. frame_sink_id,
  57. std::move(gpu_dependency),
  58. std::move(output_surface),
  59. std::move(overlay_processor),
  60. std::move(scheduler),
  61. /*current_task_runner=*/nullptr),
  62. overlay_processor_webview_(overlay_processor_webview),
  63. frame_sink_manager_(frame_sink_manager),
  64. use_new_invalidate_heuristic_(base::FeatureList::IsEnabled(
  65. features::kWebViewNewInvalidateHeuristic)) {
  66. if (overlay_processor_webview_) {
  67. frame_sink_manager_observation_.Observe(frame_sink_manager);
  68. }
  69. }
  70. DisplayWebView::~DisplayWebView() = default;
  71. void DisplayWebView::OnFrameSinkDidFinishFrame(
  72. const viz::FrameSinkId& frame_sink_id,
  73. const viz::BeginFrameArgs& args) {
  74. DCHECK(overlay_processor_webview_);
  75. auto surface_id =
  76. overlay_processor_webview_->GetOverlaySurfaceId(frame_sink_id);
  77. if (surface_id.is_valid()) {
  78. auto* surface =
  79. frame_sink_manager_->surface_manager()->GetSurfaceForId(surface_id);
  80. DCHECK(surface);
  81. if (use_new_invalidate_heuristic_) {
  82. // For overlays we are going to display this frame immediately, so commit
  83. // it.
  84. surface->CommitFramesRecursively(
  85. base::BindRepeating([](const viz::SurfaceId&,
  86. const viz::BeginFrameId&) { return true; }));
  87. }
  88. // TODO(vasilyt): We don't need full aggregation here as we don't need
  89. // aggregated frame.
  90. aggregator_->Aggregate(current_surface_id_, base::TimeTicks::Now(),
  91. gfx::OVERLAY_TRANSFORM_NONE, gfx::Rect(),
  92. ++swapped_trace_id_);
  93. auto* resolved_data = aggregator_->GetLatestFrameData(surface_id);
  94. if (resolved_data) {
  95. overlay_processor_webview_->ProcessForFrameSinkId(frame_sink_id,
  96. resolved_data);
  97. }
  98. }
  99. }
  100. const base::flat_set<viz::SurfaceId>& DisplayWebView::GetContainedSurfaceIds() {
  101. return aggregator_->previous_contained_surfaces();
  102. }
  103. } // namespace android_webview