1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140
  141
  142
  143
  144
  145
  146

android_webview / browser / gfx / render_thread_manager.h [blame]

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ANDROID_WEBVIEW_BROWSER_GFX_RENDER_THREAD_MANAGER_H_
#define ANDROID_WEBVIEW_BROWSER_GFX_RENDER_THREAD_MANAGER_H_

#include <map>

#include "android_webview/browser/gfx/compositor_frame_consumer.h"
#include "android_webview/browser/gfx/hardware_renderer.h"
#include "android_webview/browser/gfx/parent_compositor_draw_constraints.h"
#include "android_webview/browser/gfx/root_frame_sink.h"
#include "base/check.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/synchronization/lock.h"
#include "base/task/single_thread_task_runner.h"
#include "components/viz/common/surfaces/frame_sink_id.h"
#include "ui/gfx/geometry/point.h"

namespace android_webview {

class AwVulkanContextProvider;
class ChildFrame;
class CompositorFrameProducer;

// This class is used to pass data between UI thread and RenderThread.
//
// Lifetime: WebView
class RenderThreadManager : public CompositorFrameConsumer {
 public:
  explicit RenderThreadManager(
      const scoped_refptr<base::SingleThreadTaskRunner>& ui_loop);

  RenderThreadManager(const RenderThreadManager&) = delete;
  RenderThreadManager& operator=(const RenderThreadManager&) = delete;

  ~RenderThreadManager() override;

  void SetRootFrameSinkGetterForTesting(
      RootFrameSinkGetter root_frame_sink_getter);
  // CompositorFrameConsumer methods.
  void SetCompositorFrameProducer(
      CompositorFrameProducer* compositor_frame_producer,
      RootFrameSinkGetter root_frame_sink_getter) override;
  void SetScrollOffsetOnUI(gfx::Point scroll_offset) override;
  std::unique_ptr<ChildFrame> SetFrameOnUI(
      std::unique_ptr<ChildFrame> frame) override;
  void TakeParentDrawDataOnUI(
      ParentCompositorDrawConstraints* constraints,
      viz::FrameSinkId* frame_sink_id,
      viz::FrameTimingDetailsMap* timing_details,
      uint32_t* frame_token,
      base::TimeDelta* preferred_frame_interval) override;
  ChildFrameQueue PassUncommittedFrameOnUI() override;

  void RemoveFromCompositorFrameProducerOnUI();

  // Render thread methods.
  gfx::Point GetScrollOffsetOnRT();
  ChildFrameQueue PassFramesOnRT();
  void PostParentDrawDataToChildCompositorOnRT(
      const ParentCompositorDrawConstraints& parent_draw_constraints,
      const viz::FrameSinkId& frame_sink_id,
      viz::FrameTimingDetailsMap timing_details,
      uint32_t frame_token,
      base::TimeDelta preferred_frame_interval);
  void InsertReturnedResourcesOnRT(std::vector<viz::ReturnedResource> resources,
                                   const viz::FrameSinkId& frame_sink_id,
                                   uint32_t layer_tree_frame_sink_id);

  void CommitFrameOnRT();
  void SetVulkanContextProviderOnRT(AwVulkanContextProvider* context_provider);
  void UpdateViewTreeForceDarkStateOnRT(bool view_tree_force_dark_state);
  void DrawOnRT(bool save_restore,
                const HardwareRendererDrawParams& params,
                const OverlaysParams& overlays_params,
                ReportRenderingThreadsCallback report_rendering_threads);
  void DestroyHardwareRendererOnRT(bool save_restore, bool abandon_context);
  void RemoveOverlaysOnRT(OverlaysParams::MergeTransactionFn merge_transaction);

  // May be created on either thread.
  class InsideHardwareReleaseReset {
   public:
    explicit InsideHardwareReleaseReset(
        RenderThreadManager* render_thread_manager);
    ~InsideHardwareReleaseReset();

   private:
    raw_ptr<RenderThreadManager> render_thread_manager_;
  };

 private:
  static std::unique_ptr<ChildFrame> GetSynchronousCompositorFrame(
      scoped_refptr<content::SynchronousCompositor::FrameFuture> frame_future,
      std::unique_ptr<ChildFrame> child_frame);

  // RT thread method.
  bool HasFrameForHardwareRendererOnRT() const;
  bool IsInsideHardwareRelease() const;
  void SetInsideHardwareRelease(bool inside);

  // UI thread methods.
  void UpdateParentDrawDataOnUI();
  void ViewTreeForceDarkStateChangedOnUI(bool view_tree_force_dark_state);
  void CheckUiCallsAllowed() const {
#if DCHECK_IS_ON()
    DCHECK(ui_calls_allowed_);
#endif  // DCHECK_IS_ON()
  }

  // Accessed by UI thread.
  scoped_refptr<base::SingleThreadTaskRunner> ui_loop_;
  base::WeakPtr<CompositorFrameProducer> producer_weak_ptr_;
  base::WeakPtr<RenderThreadManager> ui_thread_weak_ptr_;
#if DCHECK_IS_ON()
  // Becomes false after lifetime of this object transitions from being
  // shared by both UI and RT, to being owned by a single thread and only RT
  // methods and destructor are allowed after.
  bool ui_calls_allowed_ = true;
#endif  // DCHECK_IS_ON()

  // Accessed by RT thread.
  std::unique_ptr<HardwareRenderer> hardware_renderer_;
  bool view_tree_force_dark_state_ = false;
  raw_ptr<AwVulkanContextProvider> vulkan_context_provider_ = nullptr;

  // Accessed by both UI and RT thread.
  mutable base::Lock lock_;
  RootFrameSinkGetter root_frame_sink_getter_;
  gfx::Point scroll_offset_;
  ChildFrameQueue child_frames_;
  bool mark_hardware_release_;
  ParentCompositorDrawConstraints parent_draw_constraints_;
  viz::FrameSinkId frame_sink_id_for_presentation_feedbacks_;
  viz::FrameTimingDetailsMap timing_details_;
  uint32_t presented_frame_token_ = 0u;
  base::TimeDelta preferred_frame_interval_;

  base::WeakPtrFactory<RenderThreadManager> weak_factory_on_ui_thread_{this};
};

}  // namespace android_webview

#endif  // ANDROID_WEBVIEW_BROWSER_GFX_RENDER_THREAD_MANAGER_H_