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 
5 #ifndef ANDROID_WEBVIEW_BROWSER_GFX_ROOT_FRAME_SINK_H_
6 #define ANDROID_WEBVIEW_BROWSER_GFX_ROOT_FRAME_SINK_H_
7 
8 #include <memory>
9 
10 #include "base/callback.h"
11 #include "base/macros.h"
12 #include "base/threading/thread_checker.h"
13 #include "components/viz/common/frame_sinks/begin_frame_source.h"
14 #include "components/viz/common/frame_timing_details_map.h"
15 #include "services/viz/public/mojom/compositing/compositor_frame_sink.mojom.h"
16 
17 namespace viz {
18 class CompositorFrameSinkSupport;
19 class FrameSinkManagerImpl;
20 class ExternalBeginFrameSource;
21 }  // namespace viz
22 
23 namespace android_webview {
24 class ChildFrame;
25 
26 class RootFrameSinkClient {
27  public:
28   virtual ~RootFrameSinkClient() = default;
29 
30   virtual void SetNeedsBeginFrames(bool needs_begin_frame) = 0;
31   virtual void Invalidate() = 0;
32   virtual void ReturnResources(
33       viz::FrameSinkId frame_sink_id,
34       uint32_t layer_tree_frame_sink_id,
35       std::vector<viz::ReturnedResource> resources) = 0;
36 };
37 
38 // This class holds per-AwContents classes on the viz thread that do not need
39 // access to the GPU. It is single-threaded and refcounted on the viz thread.
40 // This needs to be separate from classes for rendering which requires GPU
41 // to enable sending begin frames independently from access to GPU.
42 class RootFrameSink : public base::RefCounted<RootFrameSink>,
43                       public viz::mojom::CompositorFrameSinkClient,
44                       public viz::ExternalBeginFrameSourceClient {
45  public:
46   using SetNeedsBeginFrameCallback = base::RepeatingCallback<void(bool)>;
47   RootFrameSink(RootFrameSinkClient* client);
48 
support()49   viz::CompositorFrameSinkSupport* support() const { return support_.get(); }
root_frame_sink_id()50   const viz::FrameSinkId& root_frame_sink_id() const {
51     return root_frame_sink_id_;
52   }
53   void AddChildFrameSinkId(const viz::FrameSinkId& frame_sink_id);
54   void RemoveChildFrameSinkId(const viz::FrameSinkId& frame_sink_id);
55   bool BeginFrame(const viz::BeginFrameArgs& args, bool had_input_event);
56   void SetBeginFrameSourcePaused(bool paused);
57   void SetNeedsDraw(bool needs_draw);
58   bool IsChildSurface(const viz::FrameSinkId& frame_sink_id);
59   void DettachClient();
60 
61   void SubmitChildCompositorFrame(const viz::LocalSurfaceId& local_surface_id,
62                                   ChildFrame* child_frame);
63   viz::FrameTimingDetailsMap TakeChildFrameTimingDetailsMap();
64   gfx::Size GetChildFrameSize();
65 
66   // viz::mojom::CompositorFrameSinkClient implementation.
67   void DidReceiveCompositorFrameAck(
68       const std::vector<viz::ReturnedResource>& resources) override;
OnBeginFrame(const viz::BeginFrameArgs & args,const viz::FrameTimingDetailsMap & feedbacks)69   void OnBeginFrame(const viz::BeginFrameArgs& args,
70                     const viz::FrameTimingDetailsMap& feedbacks) override {}
OnBeginFramePausedChanged(bool paused)71   void OnBeginFramePausedChanged(bool paused) override {}
72   void ReclaimResources(
73       const std::vector<viz::ReturnedResource>& resources) override;
74 
75   // viz::ExternalBeginFrameSourceClient overrides.
76   void OnNeedsBeginFrames(bool needs_begin_frames) override;
77 
78  private:
79   friend class base::RefCounted<RootFrameSink>;
80   class ChildCompositorFrameSink;
81 
82   ~RootFrameSink() override;
83   viz::FrameSinkManagerImpl* GetFrameSinkManager();
84   void ReturnResources(viz::FrameSinkId frame_sink_id,
85                        uint32_t layer_tree_frame_sink_id,
86                        std::vector<viz::ReturnedResource> resources);
87 
88   const viz::FrameSinkId root_frame_sink_id_;
89   base::flat_set<viz::FrameSinkId> child_frame_sink_ids_;
90   std::unique_ptr<viz::CompositorFrameSinkSupport> support_;
91   std::unique_ptr<viz::ExternalBeginFrameSource> begin_frame_source_;
92 
93   std::unique_ptr<ChildCompositorFrameSink> child_sink_support_;
94 
95   bool needs_begin_frames_ = false;
96   bool needs_draw_ = false;
97   RootFrameSinkClient* client_;
98 
99   THREAD_CHECKER(thread_checker_);
100 
101   DISALLOW_COPY_AND_ASSIGN(RootFrameSink);
102 };
103 
104 using RootFrameSinkGetter =
105     base::RepeatingCallback<scoped_refptr<RootFrameSink>()>;
106 
107 }  // namespace android_webview
108 
109 #endif  // ANDROID_WEBVIEW_BROWSER_GFX_ROOT_FRAME_SINK_H_
110