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
3 // found in the LICENSE file.
4 
5 #ifndef CC_TREES_PROXY_MAIN_H_
6 #define CC_TREES_PROXY_MAIN_H_
7 
8 #include "cc/cc_export.h"
9 #include "cc/input/browser_controls_state.h"
10 #include "cc/trees/layer_tree_host.h"
11 #include "cc/trees/proxy.h"
12 #include "cc/trees/proxy_common.h"
13 
14 namespace cc {
15 
16 class CompletionEvent;
17 class LayerTreeFrameSink;
18 class LayerTreeHost;
19 class LayerTreeMutator;
20 class PaintWorkletLayerPainter;
21 class ProxyImpl;
22 class RenderFrameMetadataObserver;
23 
24 // This class aggregates all interactions that the impl side of the compositor
25 // needs to have with the main side.
26 // The class is created and lives on the main thread.
27 class CC_EXPORT ProxyMain : public Proxy {
28  public:
29   ProxyMain(LayerTreeHost* layer_tree_host,
30             TaskRunnerProvider* task_runner_provider);
31   ProxyMain(const ProxyMain&) = delete;
32   ~ProxyMain() override;
33 
34   ProxyMain& operator=(const ProxyMain&) = delete;
35 
36   // Commits between the main and impl threads are processed through a pipeline
37   // with the following stages. For efficiency we can early out at any stage if
38   // we decide that no further processing is necessary.
39   enum CommitPipelineStage {
40     NO_PIPELINE_STAGE,
41     ANIMATE_PIPELINE_STAGE,
42     UPDATE_LAYERS_PIPELINE_STAGE,
43     COMMIT_PIPELINE_STAGE,
44   };
45 
46   void DidReceiveCompositorFrameAck();
47   void BeginMainFrameNotExpectedSoon();
48   void BeginMainFrameNotExpectedUntil(base::TimeTicks time);
49   void DidCommitAndDrawFrame();
50   void DidLoseLayerTreeFrameSink();
51   void RequestNewLayerTreeFrameSink();
52   void DidInitializeLayerTreeFrameSink(bool success);
53   void DidCompletePageScaleAnimation();
54   void BeginMainFrame(
55       std::unique_ptr<BeginMainFrameAndCommitState> begin_main_frame_state);
56   void DidPresentCompositorFrame(
57       uint32_t frame_token,
58       std::vector<LayerTreeHost::PresentationTimeCallback> callbacks,
59       const gfx::PresentationFeedback& feedback);
60 
max_requested_pipeline_stage()61   CommitPipelineStage max_requested_pipeline_stage() const {
62     return max_requested_pipeline_stage_;
63   }
current_pipeline_stage()64   CommitPipelineStage current_pipeline_stage() const {
65     return current_pipeline_stage_;
66   }
final_pipeline_stage()67   CommitPipelineStage final_pipeline_stage() const {
68     return final_pipeline_stage_;
69   }
70 
71  private:
72   // Proxy implementation.
73   bool IsStarted() const override;
74   void SetLayerTreeFrameSink(
75       LayerTreeFrameSink* layer_tree_frame_sink) override;
76   void SetVisible(bool visible) override;
77   void SetNeedsAnimate() override;
78   void SetNeedsUpdateLayers() override;
79   void SetNeedsCommit() override;
80   void SetNeedsRedraw(const gfx::Rect& damage_rect) override;
81   void SetNextCommitWaitsForActivation() override;
82   bool RequestedAnimatePending() override;
83   void SetDeferMainFrameUpdate(bool defer_main_frame_update) override;
84   void StartDeferringCommits(base::TimeDelta timeout) override;
85   void StopDeferringCommits(PaintHoldingCommitTrigger) override;
86   bool CommitRequested() const override;
87   void Start() override;
88   void Stop() override;
89   bool SupportsImplScrolling() const override;
90   void SetMutator(std::unique_ptr<LayerTreeMutator> mutator) override;
91   void SetPaintWorkletLayerPainter(
92       std::unique_ptr<PaintWorkletLayerPainter> painter) override;
93   bool MainFrameWillHappenForTesting() override;
94   void ReleaseLayerTreeFrameSink() override;
95   void UpdateBrowserControlsState(BrowserControlsState constraints,
96                                   BrowserControlsState current,
97                                   bool animate) override;
98   void RequestBeginMainFrameNotExpected(bool new_state) override;
99   void SetSourceURL(ukm::SourceId source_id, const GURL& url) override;
100   void ClearHistory() override;
101   void SetRenderFrameObserver(
102       std::unique_ptr<RenderFrameMetadataObserver> observer) override;
103 
104   // Returns |true| if the request was actually sent, |false| if one was
105   // already outstanding.
106   bool SendCommitRequestToImplThreadIfNeeded(
107       CommitPipelineStage required_stage);
108   bool IsMainThread() const;
109   bool IsImplThread() const;
110   base::SingleThreadTaskRunner* ImplThreadTaskRunner();
111 
112   void InitializeOnImplThread(CompletionEvent* completion_event);
113   void DestroyProxyImplOnImplThread(CompletionEvent* completion_event);
114 
115   LayerTreeHost* layer_tree_host_;
116 
117   TaskRunnerProvider* task_runner_provider_;
118 
119   const int layer_tree_host_id_;
120 
121   // The furthest pipeline stage which has been requested for the next
122   // commit.
123   CommitPipelineStage max_requested_pipeline_stage_;
124   // The commit pipeline stage that is currently being processed.
125   CommitPipelineStage current_pipeline_stage_;
126   // The commit pipeline stage at which processing for the current commit
127   // will stop. Only valid while we are executing the pipeline (i.e.,
128   // |current_pipeline_stage| is set to a pipeline stage).
129   CommitPipelineStage final_pipeline_stage_;
130   // The final_pipeline_stage_ that was requested before the last commit was
131   // deferred.
132   CommitPipelineStage deferred_final_pipeline_stage_;
133 
134   bool commit_waits_for_activation_;
135 
136   // Set when the Proxy is started using Proxy::Start() and reset when it is
137   // stopped using Proxy::Stop().
138   bool started_;
139 
140   // defer_main_frame_update_ will also cause commits to be deferred, regardless
141   // of the setting for defer_commits_.
142   bool defer_main_frame_update_;
143   bool defer_commits_;
144 
145   // Only used when defer_commits_ is active and must be set in such cases.
146   base::TimeTicks commits_restart_time_;
147 
148   // ProxyImpl is created and destroyed on the impl thread, and should only be
149   // accessed on the impl thread.
150   // It is safe to use base::Unretained to post tasks to ProxyImpl on the impl
151   // thread, since we control its lifetime. Any tasks posted to it are bound to
152   // run before we destroy it on the impl thread.
153   std::unique_ptr<ProxyImpl> proxy_impl_;
154 
155   // WeakPtrs generated by this factory will be invalidated when
156   // LayerTreeFrameSink is released.
157   base::WeakPtrFactory<ProxyMain> frame_sink_bound_weak_factory_{this};
158 
159   base::WeakPtrFactory<ProxyMain> weak_factory_{this};
160 };
161 
162 }  // namespace cc
163 
164 #endif  // CC_TREES_PROXY_MAIN_H_
165