Simplify web_view.js
[chromium-blink-merge.git] / cc / trees / thread_proxy.h
blob2f3e7c56a671bb48257713001e072a385b398412
1 // Copyright 2011 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.
5 #ifndef CC_TREES_THREAD_PROXY_H_
6 #define CC_TREES_THREAD_PROXY_H_
8 #include <string>
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/time/time.h"
13 #include "cc/animation/animation_events.h"
14 #include "cc/base/completion_event.h"
15 #include "cc/base/delayed_unique_notifier.h"
16 #include "cc/resources/resource_update_controller.h"
17 #include "cc/scheduler/scheduler.h"
18 #include "cc/trees/layer_tree_host_impl.h"
19 #include "cc/trees/proxy.h"
20 #include "cc/trees/proxy_timing_history.h"
22 namespace base {
23 class SingleThreadTaskRunner;
26 namespace cc {
28 class ContextProvider;
29 class InputHandlerClient;
30 class LayerTreeHost;
31 class ResourceUpdateQueue;
32 class Scheduler;
33 class ScopedThreadProxy;
35 class CC_EXPORT ThreadProxy : public Proxy,
36 NON_EXPORTED_BASE(LayerTreeHostImplClient),
37 NON_EXPORTED_BASE(SchedulerClient),
38 NON_EXPORTED_BASE(ResourceUpdateControllerClient) {
39 public:
40 static scoped_ptr<Proxy> Create(
41 LayerTreeHost* layer_tree_host,
42 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
43 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner);
45 ~ThreadProxy() override;
47 struct BeginMainFrameAndCommitState {
48 BeginMainFrameAndCommitState();
49 ~BeginMainFrameAndCommitState();
51 unsigned int begin_frame_id;
52 BeginFrameArgs begin_frame_args;
53 scoped_ptr<ScrollAndScaleSet> scroll_info;
54 size_t memory_allocation_limit_bytes;
55 int memory_allocation_priority_cutoff;
56 bool evicted_ui_resources;
59 struct MainThreadOnly {
60 MainThreadOnly(ThreadProxy* proxy, int layer_tree_host_id);
61 ~MainThreadOnly();
63 const int layer_tree_host_id;
65 // Set only when SetNeedsAnimate is called.
66 bool animate_requested;
67 // Set only when SetNeedsCommit is called.
68 bool commit_requested;
69 // Set by SetNeedsAnimate, SetNeedsUpdateLayers, and SetNeedsCommit.
70 bool commit_request_sent_to_impl_thread;
72 bool started;
73 bool manage_tiles_pending;
74 bool can_cancel_commit;
75 bool defer_commits;
77 RendererCapabilities renderer_capabilities_main_thread_copy;
79 scoped_ptr<BeginMainFrameAndCommitState> pending_deferred_commit;
80 base::WeakPtrFactory<ThreadProxy> weak_factory;
83 // Accessed on the main thread, or when main thread is blocked.
84 struct MainThreadOrBlockedMainThread {
85 explicit MainThreadOrBlockedMainThread(LayerTreeHost* host);
86 ~MainThreadOrBlockedMainThread();
88 PrioritizedResourceManager* contents_texture_manager();
90 LayerTreeHost* layer_tree_host;
91 bool commit_waits_for_activation;
92 bool main_thread_inside_commit;
95 struct CompositorThreadOnly {
96 CompositorThreadOnly(
97 ThreadProxy* proxy,
98 int layer_tree_host_id,
99 RenderingStatsInstrumentation* rendering_stats_instrumentation);
100 ~CompositorThreadOnly();
102 const int layer_tree_host_id;
104 // Copy of the main thread side contents texture manager for work
105 // that needs to be done on the compositor thread.
106 PrioritizedResourceManager* contents_texture_manager;
108 scoped_ptr<Scheduler> scheduler;
110 // Set when the main thread is waiting on a
111 // ScheduledActionSendBeginMainFrame to be issued.
112 CompletionEvent* begin_main_frame_sent_completion_event;
114 // Set when the main thread is waiting on a commit to complete.
115 CompletionEvent* commit_completion_event;
117 // Set when the main thread is waiting on a pending tree activation.
118 CompletionEvent* completion_event_for_commit_held_on_tree_activation;
120 scoped_ptr<ResourceUpdateController> current_resource_update_controller;
122 // Set when the next draw should post DidCommitAndDrawFrame to the main
123 // thread.
124 bool next_frame_is_newly_committed_frame;
126 bool inside_draw;
128 bool input_throttled_until_commit;
130 base::TimeTicks animation_time;
132 // Whether a commit has been completed since the last time animations were
133 // ticked. If this happens, we need to animate again.
134 bool did_commit_after_animating;
136 DelayedUniqueNotifier smoothness_priority_expiration_notifier;
138 ProxyTimingHistory timing_history;
140 scoped_ptr<LayerTreeHostImpl> layer_tree_host_impl;
141 base::WeakPtrFactory<ThreadProxy> weak_factory;
144 const MainThreadOnly& main() const;
145 const MainThreadOrBlockedMainThread& blocked_main() const;
146 const CompositorThreadOnly& impl() const;
148 // Proxy implementation
149 void FinishAllRendering() override;
150 bool IsStarted() const override;
151 void SetOutputSurface(scoped_ptr<OutputSurface>) override;
152 void SetLayerTreeHostClientReady() override;
153 void SetVisible(bool visible) override;
154 const RendererCapabilities& GetRendererCapabilities() const override;
155 void SetNeedsAnimate() override;
156 void SetNeedsUpdateLayers() override;
157 void SetNeedsCommit() override;
158 void SetNeedsRedraw(const gfx::Rect& damage_rect) override;
159 void SetNextCommitWaitsForActivation() override;
160 void NotifyInputThrottledUntilCommit() override;
161 void SetDeferCommits(bool defer_commits) override;
162 bool CommitRequested() const override;
163 bool BeginMainFrameRequested() const override;
164 void MainThreadHasStoppedFlinging() override;
165 void Start() override;
166 void Stop() override;
167 size_t MaxPartialTextureUpdates() const override;
168 void ForceSerializeOnSwapBuffers() override;
169 bool SupportsImplScrolling() const override;
170 void SetDebugState(const LayerTreeDebugState& debug_state) override;
171 void AsValueInto(base::debug::TracedValue* value) const override;
172 bool MainFrameWillHappenForTesting() override;
174 // LayerTreeHostImplClient implementation
175 void UpdateRendererCapabilitiesOnImplThread() override;
176 void DidLoseOutputSurfaceOnImplThread() override;
177 void CommitVSyncParameters(base::TimeTicks timebase,
178 base::TimeDelta interval) override;
179 void SetEstimatedParentDrawTime(base::TimeDelta draw_time) override;
180 void SetMaxSwapsPendingOnImplThread(int max) override;
181 void DidSwapBuffersOnImplThread() override;
182 void DidSwapBuffersCompleteOnImplThread() override;
183 void OnCanDrawStateChanged(bool can_draw) override;
184 void NotifyReadyToActivate() override;
185 // Please call these 3 functions through
186 // LayerTreeHostImpl's SetNeedsRedraw(), SetNeedsRedrawRect() and
187 // SetNeedsAnimate().
188 void SetNeedsRedrawOnImplThread() override;
189 void SetNeedsRedrawRectOnImplThread(const gfx::Rect& dirty_rect) override;
190 void SetNeedsAnimateOnImplThread() override;
191 void SetNeedsManageTilesOnImplThread() override;
192 void DidInitializeVisibleTileOnImplThread() override;
193 void SetNeedsCommitOnImplThread() override;
194 void PostAnimationEventsToMainThreadOnImplThread(
195 scoped_ptr<AnimationEventsVector> queue) override;
196 bool ReduceContentsTextureMemoryOnImplThread(size_t limit_bytes,
197 int priority_cutoff) override;
198 bool IsInsideDraw() override;
199 void RenewTreePriority() override;
200 void PostDelayedScrollbarFadeOnImplThread(const base::Closure& start_fade,
201 base::TimeDelta delay) override;
202 void DidActivateSyncTree() override;
203 void DidManageTiles() override;
205 // SchedulerClient implementation
206 BeginFrameSource* ExternalBeginFrameSource() override;
207 void WillBeginImplFrame(const BeginFrameArgs& args) override;
208 void ScheduledActionSendBeginMainFrame() override;
209 DrawResult ScheduledActionDrawAndSwapIfPossible() override;
210 DrawResult ScheduledActionDrawAndSwapForced() override;
211 void ScheduledActionAnimate() override;
212 void ScheduledActionCommit() override;
213 void ScheduledActionUpdateVisibleTiles() override;
214 void ScheduledActionActivateSyncTree() override;
215 void ScheduledActionBeginOutputSurfaceCreation() override;
216 void ScheduledActionManageTiles() override;
217 void DidAnticipatedDrawTimeChange(base::TimeTicks time) override;
218 base::TimeDelta DrawDurationEstimate() override;
219 base::TimeDelta BeginMainFrameToCommitDurationEstimate() override;
220 base::TimeDelta CommitToActivateDurationEstimate() override;
221 void DidBeginImplFrameDeadline() override;
223 // ResourceUpdateControllerClient implementation
224 void ReadyToFinalizeTextureUpdates() override;
226 protected:
227 ThreadProxy(LayerTreeHost* layer_tree_host,
228 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
229 scoped_refptr<base::SingleThreadTaskRunner> impl_task_runner);
231 private:
232 // Called on main thread.
233 void SetRendererCapabilitiesMainThreadCopy(
234 const RendererCapabilities& capabilities);
235 void BeginMainFrame(
236 scoped_ptr<BeginMainFrameAndCommitState> begin_main_frame_state);
237 void DidCommitAndDrawFrame();
238 void DidCompleteSwapBuffers();
239 void SetAnimationEvents(scoped_ptr<AnimationEventsVector> queue);
240 void DidLoseOutputSurface();
241 void RequestNewOutputSurface();
242 void DidInitializeOutputSurface(bool success,
243 const RendererCapabilities& capabilities);
244 void SendCommitRequestToImplThreadIfNeeded();
246 // Called on impl thread.
247 struct SchedulerStateRequest;
249 void StartCommitOnImplThread(CompletionEvent* completion,
250 ResourceUpdateQueue* queue);
251 void BeginMainFrameAbortedOnImplThread(bool did_handle);
252 void FinishAllRenderingOnImplThread(CompletionEvent* completion);
253 void InitializeImplOnImplThread(CompletionEvent* completion);
254 void SetLayerTreeHostClientReadyOnImplThread();
255 void SetVisibleOnImplThread(CompletionEvent* completion, bool visible);
256 void UpdateBackgroundAnimateTicking();
257 void HasInitializedOutputSurfaceOnImplThread(
258 CompletionEvent* completion,
259 bool* has_initialized_output_surface);
260 void DeleteContentsTexturesOnImplThread(CompletionEvent* completion);
261 void InitializeOutputSurfaceOnImplThread(
262 scoped_ptr<OutputSurface> output_surface);
263 void FinishGLOnImplThread(CompletionEvent* completion);
264 void LayerTreeHostClosedOnImplThread(CompletionEvent* completion);
265 DrawResult DrawSwapInternal(bool forced_draw);
266 void ForceSerializeOnSwapBuffersOnImplThread(CompletionEvent* completion);
267 void MainFrameWillHappenOnImplThreadForTesting(CompletionEvent* completion,
268 bool* main_frame_will_happen);
269 void AsValueOnImplThread(CompletionEvent* completion,
270 base::debug::TracedValue* state) const;
271 void SetSwapUsedIncompleteTileOnImplThread(bool used_incomplete_tile);
272 void MainThreadHasStoppedFlingingOnImplThread();
273 void SetInputThrottledUntilCommitOnImplThread(bool is_throttled);
274 void SetDebugStateOnImplThread(const LayerTreeDebugState& debug_state);
276 LayerTreeHost* layer_tree_host();
277 const LayerTreeHost* layer_tree_host() const;
279 // Use accessors instead of this variable directly.
280 MainThreadOnly main_thread_only_vars_unsafe_;
281 MainThreadOnly& main();
283 // Use accessors instead of this variable directly.
284 MainThreadOrBlockedMainThread main_thread_or_blocked_vars_unsafe_;
285 MainThreadOrBlockedMainThread& blocked_main();
287 // Use accessors instead of this variable directly.
288 CompositorThreadOnly compositor_thread_vars_unsafe_;
289 CompositorThreadOnly& impl();
291 base::WeakPtr<ThreadProxy> main_thread_weak_ptr_;
292 base::WeakPtr<ThreadProxy> impl_thread_weak_ptr_;
294 DISALLOW_COPY_AND_ASSIGN(ThreadProxy);
297 } // namespace cc
299 #endif // CC_TREES_THREAD_PROXY_H_