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_LAYER_TREE_HOST_H_
6 #define CC_TREES_LAYER_TREE_HOST_H_
13 #include "base/basictypes.h"
14 #include "base/cancelable_callback.h"
15 #include "base/containers/hash_tables.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/time/time.h"
20 #include "base/timer/timer.h"
21 #include "cc/animation/animation_events.h"
22 #include "cc/base/cc_export.h"
23 #include "cc/base/scoped_ptr_vector.h"
24 #include "cc/debug/frame_timing_tracker.h"
25 #include "cc/debug/micro_benchmark.h"
26 #include "cc/debug/micro_benchmark_controller.h"
27 #include "cc/input/input_handler.h"
28 #include "cc/input/layer_selection_bound.h"
29 #include "cc/input/scrollbar.h"
30 #include "cc/input/top_controls_state.h"
31 #include "cc/layers/layer_lists.h"
32 #include "cc/output/output_surface.h"
33 #include "cc/output/renderer_capabilities.h"
34 #include "cc/output/swap_promise.h"
35 #include "cc/resources/resource_format.h"
36 #include "cc/resources/scoped_ui_resource.h"
37 #include "cc/surfaces/surface_sequence.h"
38 #include "cc/trees/layer_tree_host_client.h"
39 #include "cc/trees/layer_tree_host_common.h"
40 #include "cc/trees/layer_tree_settings.h"
41 #include "cc/trees/mutator_host_client.h"
42 #include "cc/trees/proxy.h"
43 #include "cc/trees/swap_promise_monitor.h"
44 #include "third_party/skia/include/core/SkColor.h"
45 #include "ui/gfx/geometry/rect.h"
48 class GpuMemoryBufferManager
;
52 class AnimationRegistrar
;
54 class BeginFrameSource
;
55 class HeadsUpDisplayLayer
;
57 class LayerTreeHostImpl
;
58 class LayerTreeHostImplClient
;
59 class LayerTreeHostSingleThreadClient
;
62 class RenderingStatsInstrumentation
;
63 class ResourceProvider
;
64 class ResourceUpdateQueue
;
65 class SharedBitmapManager
;
66 class TaskGraphRunner
;
67 class TopControlsManager
;
68 class UIResourceRequest
;
69 struct PendingPageScaleAnimation
;
70 struct RenderingStats
;
71 struct ScrollAndScaleSet
;
73 class CC_EXPORT LayerTreeHost
: public MutatorHostClient
{
75 // TODO(sad): InitParams should be a movable type so that it can be
76 // std::move()d to the Create* functions.
77 struct CC_EXPORT InitParams
{
78 LayerTreeHostClient
* client
= nullptr;
79 SharedBitmapManager
* shared_bitmap_manager
= nullptr;
80 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager
= nullptr;
81 TaskGraphRunner
* task_graph_runner
= nullptr;
82 LayerTreeSettings
const* settings
= nullptr;
83 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
;
84 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
;
90 // The SharedBitmapManager will be used on the compositor thread.
91 static scoped_ptr
<LayerTreeHost
> CreateThreaded(
92 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
,
95 static scoped_ptr
<LayerTreeHost
> CreateSingleThreaded(
96 LayerTreeHostSingleThreadClient
* single_thread_client
,
98 virtual ~LayerTreeHost();
100 void SetLayerTreeHostClientReady();
102 // LayerTreeHost interface to Proxy.
103 void WillBeginMainFrame();
104 void DidBeginMainFrame();
105 void BeginMainFrame(const BeginFrameArgs
& args
);
106 void BeginMainFrameNotExpectedSoon();
107 void AnimateLayers(base::TimeTicks monotonic_frame_begin_time
);
108 void DidStopFlinging();
110 void BeginCommitOnImplThread(LayerTreeHostImpl
* host_impl
);
111 void FinishCommitOnImplThread(LayerTreeHostImpl
* host_impl
);
113 void CommitComplete();
114 void SetOutputSurface(scoped_ptr
<OutputSurface
> output_surface
);
115 void RequestNewOutputSurface();
116 void DidInitializeOutputSurface();
117 void DidFailToInitializeOutputSurface();
118 virtual scoped_ptr
<LayerTreeHostImpl
> CreateLayerTreeHostImpl(
119 LayerTreeHostImplClient
* client
);
120 void DidLoseOutputSurface();
121 bool output_surface_lost() const { return output_surface_lost_
; }
122 void DidCommitAndDrawFrame() { client_
->DidCommitAndDrawFrame(); }
123 void DidCompleteSwapBuffers() { client_
->DidCompleteSwapBuffers(); }
126 // Called when the compositor completed page scale animation.
127 void DidCompletePageScaleAnimation();
129 LayerTreeHostClient
* client() { return client_
; }
130 const base::WeakPtr
<InputHandler
>& GetInputHandler() {
131 return input_handler_weak_ptr_
;
134 void NotifyInputThrottledUntilCommit();
136 void LayoutAndUpdateLayers();
137 void Composite(base::TimeTicks frame_begin_time
);
139 void FinishAllRendering();
141 void SetDeferCommits(bool defer_commits
);
143 int source_frame_number() const { return source_frame_number_
; }
145 int meta_information_sequence_number() {
146 return meta_information_sequence_number_
;
149 void IncrementMetaInformationSequenceNumber() {
150 meta_information_sequence_number_
++;
153 void SetNeedsDisplayOnAllLayers();
155 void CollectRenderingStats(RenderingStats
* stats
) const;
157 RenderingStatsInstrumentation
* rendering_stats_instrumentation() const {
158 return rendering_stats_instrumentation_
.get();
161 const RendererCapabilities
& GetRendererCapabilities() const;
163 void SetNeedsAnimate();
164 virtual void SetNeedsUpdateLayers();
165 virtual void SetNeedsCommit();
166 virtual void SetNeedsFullTreeSync();
167 virtual void SetNeedsMetaInfoRecomputation(
168 bool needs_meta_info_recomputation
);
169 void SetNeedsRedraw();
170 void SetNeedsRedrawRect(const gfx::Rect
& damage_rect
);
171 bool CommitRequested() const;
172 bool BeginMainFrameRequested() const;
174 void SetNextCommitWaitsForActivation();
176 void SetNextCommitForcesRedraw();
178 void SetAnimationEvents(scoped_ptr
<AnimationEventsVector
> events
);
180 void SetRootLayer(scoped_refptr
<Layer
> root_layer
);
181 Layer
* root_layer() { return root_layer_
.get(); }
182 const Layer
* root_layer() const { return root_layer_
.get(); }
183 const Layer
* overscroll_elasticity_layer() const {
184 return overscroll_elasticity_layer_
.get();
186 const Layer
* page_scale_layer() const { return page_scale_layer_
.get(); }
187 void RegisterViewportLayers(scoped_refptr
<Layer
> overscroll_elasticity_layer
,
188 scoped_refptr
<Layer
> page_scale_layer
,
189 scoped_refptr
<Layer
> inner_viewport_scroll_layer
,
190 scoped_refptr
<Layer
> outer_viewport_scroll_layer
);
191 Layer
* inner_viewport_scroll_layer() const {
192 return inner_viewport_scroll_layer_
.get();
194 Layer
* outer_viewport_scroll_layer() const {
195 return outer_viewport_scroll_layer_
.get();
198 void RegisterSelection(const LayerSelection
& selection
);
200 const LayerTreeSettings
& settings() const { return settings_
; }
202 void SetDebugState(const LayerTreeDebugState
& debug_state
);
203 const LayerTreeDebugState
& debug_state() const { return debug_state_
; }
205 bool has_gpu_rasterization_trigger() const {
206 return has_gpu_rasterization_trigger_
;
208 void SetHasGpuRasterizationTrigger(bool has_trigger
);
210 void SetViewportSize(const gfx::Size
& device_viewport_size
);
211 void SetTopControlsHeight(float height
, bool shrink
);
212 void SetTopControlsShownRatio(float ratio
);
214 gfx::Size
device_viewport_size() const { return device_viewport_size_
; }
216 void ApplyPageScaleDeltaFromImplSide(float page_scale_delta
);
217 void SetPageScaleFactorAndLimits(float page_scale_factor
,
218 float min_page_scale_factor
,
219 float max_page_scale_factor
);
220 float page_scale_factor() const { return page_scale_factor_
; }
221 gfx::Vector2dF
elastic_overscroll() const { return elastic_overscroll_
; }
223 SkColor
background_color() const { return background_color_
; }
224 void set_background_color(SkColor color
) { background_color_
= color
; }
226 void set_has_transparent_background(bool transparent
) {
227 has_transparent_background_
= transparent
;
230 void SetVisible(bool visible
);
231 bool visible() const { return visible_
; }
233 void SetThrottleFrameProduction(bool throttle
);
235 void StartPageScaleAnimation(const gfx::Vector2d
& target_offset
,
238 base::TimeDelta duration
);
240 void ApplyScrollAndScale(ScrollAndScaleSet
* info
);
241 void SetImplTransform(const gfx::Transform
& transform
);
243 // Virtual for tests.
244 virtual void StartRateLimiter();
245 virtual void StopRateLimiter();
249 void SetDeviceScaleFactor(float device_scale_factor
);
250 float device_scale_factor() const { return device_scale_factor_
; }
252 void UpdateTopControlsState(TopControlsState constraints
,
253 TopControlsState current
,
256 HeadsUpDisplayLayer
* hud_layer() const { return hud_layer_
.get(); }
258 Proxy
* proxy() const { return proxy_
.get(); }
259 AnimationRegistrar
* animation_registrar() const {
260 return animation_registrar_
.get();
262 AnimationHost
* animation_host() const { return animation_host_
.get(); }
264 bool in_paint_layer_contents() const { return in_paint_layer_contents_
; }
266 // CreateUIResource creates a resource given a bitmap. The bitmap is
267 // generated via an interface function, which is called when initializing the
268 // resource and when the resource has been lost (due to lost context). The
269 // parameter of the interface is a single boolean, which indicates whether the
270 // resource has been lost or not. CreateUIResource returns an Id of the
271 // resource, which is always positive.
272 virtual UIResourceId
CreateUIResource(UIResourceClient
* client
);
273 // Deletes a UI resource. May safely be called more than once.
274 virtual void DeleteUIResource(UIResourceId id
);
275 // Put the recreation of all UI resources into the resource queue after they
276 // were evicted on the impl thread.
277 void RecreateUIResources();
279 virtual gfx::Size
GetUIResourceSize(UIResourceId id
) const;
281 bool UsingSharedMemoryResources();
282 int id() const { return id_
; }
284 // Returns the id of the benchmark on success, 0 otherwise.
285 int ScheduleMicroBenchmark(const std::string
& benchmark_name
,
286 scoped_ptr
<base::Value
> value
,
287 const MicroBenchmark::DoneCallback
& callback
);
288 // Returns true if the message was successfully delivered and handled.
289 bool SendMessageToMicroBenchmark(int id
, scoped_ptr
<base::Value
> value
);
291 // When a SwapPromiseMonitor is created on the main thread, it calls
292 // InsertSwapPromiseMonitor() to register itself with LayerTreeHost.
293 // When the monitor is destroyed, it calls RemoveSwapPromiseMonitor()
294 // to unregister itself.
295 void InsertSwapPromiseMonitor(SwapPromiseMonitor
* monitor
);
296 void RemoveSwapPromiseMonitor(SwapPromiseMonitor
* monitor
);
298 // Call this function when you expect there to be a swap buffer.
299 // See swap_promise.h for how to use SwapPromise.
300 void QueueSwapPromise(scoped_ptr
<SwapPromise
> swap_promise
);
302 void BreakSwapPromises(SwapPromise::DidNotSwapReason reason
);
304 size_t num_queued_swap_promises() const { return swap_promise_list_
.size(); }
306 void set_surface_id_namespace(uint32_t id_namespace
);
307 SurfaceSequence
CreateSurfaceSequence();
309 void SetChildrenNeedBeginFrames(bool children_need_begin_frames
) const;
310 void SendBeginFramesToChildren(const BeginFrameArgs
& args
) const;
312 void SetAuthoritativeVSyncInterval(const base::TimeDelta
& interval
);
314 PropertyTrees
* property_trees() { return &property_trees_
; }
315 bool needs_meta_info_recomputation() {
316 return needs_meta_info_recomputation_
;
319 void RecordFrameTimingEvents(
320 scoped_ptr
<FrameTimingTracker::CompositeTimingSet
> composite_events
,
321 scoped_ptr
<FrameTimingTracker::MainFrameTimingSet
> main_frame_events
);
323 Layer
* LayerById(int id
) const;
324 void RegisterLayer(Layer
* layer
);
325 void UnregisterLayer(Layer
* layer
);
326 // LayerTreeMutatorsClient implementation.
327 bool IsLayerInTree(int layer_id
, LayerTreeType tree_type
) const override
;
328 void SetMutatorsNeedCommit() override
;
329 void SetLayerFilterMutated(int layer_id
,
330 LayerTreeType tree_type
,
331 const FilterOperations
& filters
) override
;
332 void SetLayerOpacityMutated(int layer_id
,
333 LayerTreeType tree_type
,
334 float opacity
) override
;
335 void SetLayerTransformMutated(int layer_id
,
336 LayerTreeType tree_type
,
337 const gfx::Transform
& transform
) override
;
338 void SetLayerScrollOffsetMutated(
340 LayerTreeType tree_type
,
341 const gfx::ScrollOffset
& scroll_offset
) override
;
342 void ScrollOffsetAnimationFinished() override
{}
343 gfx::ScrollOffset
GetScrollOffsetForAnimation(int layer_id
) const override
;
345 bool ScrollOffsetAnimationWasInterrupted(const Layer
* layer
) const;
346 bool IsAnimatingFilterProperty(const Layer
* layer
) const;
347 bool IsAnimatingOpacityProperty(const Layer
* layer
) const;
348 bool IsAnimatingTransformProperty(const Layer
* layer
) const;
349 bool HasPotentiallyRunningFilterAnimation(const Layer
* layer
) const;
350 bool HasPotentiallyRunningOpacityAnimation(const Layer
* layer
) const;
351 bool HasPotentiallyRunningTransformAnimation(const Layer
* layer
) const;
352 bool AnimationsPreserveAxisAlignment(const Layer
* layer
) const;
353 bool HasAnyAnimation(const Layer
* layer
) const;
354 bool HasActiveAnimation(const Layer
* layer
) const;
357 explicit LayerTreeHost(InitParams
* params
);
358 void InitializeThreaded(
359 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
360 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
,
361 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
);
362 void InitializeSingleThreaded(
363 LayerTreeHostSingleThreadClient
* single_thread_client
,
364 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
365 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
);
366 void InitializeForTesting(scoped_ptr
<Proxy
> proxy_for_testing
);
367 void SetOutputSurfaceLostForTesting(bool is_lost
) {
368 output_surface_lost_
= is_lost
;
371 // shared_bitmap_manager(), gpu_memory_buffer_manager(), and
372 // task_graph_runner() return valid values only until the LayerTreeHostImpl is
373 // created in CreateLayerTreeHostImpl().
374 SharedBitmapManager
* shared_bitmap_manager() const {
375 return shared_bitmap_manager_
;
377 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager() const {
378 return gpu_memory_buffer_manager_
;
380 TaskGraphRunner
* task_graph_runner() const { return task_graph_runner_
; }
382 MicroBenchmarkController micro_benchmark_controller_
;
384 void OnCommitForSwapPromises();
387 void InitializeProxy(scoped_ptr
<Proxy
> proxy
);
389 bool DoUpdateLayers(Layer
* root_layer
);
390 void UpdateHudLayer();
392 void ReduceMemoryUsage();
394 bool AnimateLayersRecursive(Layer
* current
, base::TimeTicks time
);
396 struct UIResourceClientData
{
397 UIResourceClient
* client
;
401 typedef base::hash_map
<UIResourceId
, UIResourceClientData
>
403 UIResourceClientMap ui_resource_client_map_
;
404 int next_ui_resource_id_
;
406 typedef std::vector
<UIResourceRequest
> UIResourceRequestQueue
;
407 UIResourceRequestQueue ui_resource_request_queue_
;
409 void RecordGpuRasterizationHistogram();
410 void CalculateLCDTextMetricsCallback(Layer
* layer
);
412 void NotifySwapPromiseMonitorsOfSetNeedsCommit();
414 bool inside_begin_main_frame_
;
415 bool needs_full_tree_sync_
;
416 bool needs_meta_info_recomputation_
;
418 LayerTreeHostClient
* client_
;
419 scoped_ptr
<Proxy
> proxy_
;
421 int source_frame_number_
;
422 int meta_information_sequence_number_
;
423 scoped_ptr
<RenderingStatsInstrumentation
> rendering_stats_instrumentation_
;
425 bool output_surface_lost_
;
427 scoped_refptr
<Layer
> root_layer_
;
428 scoped_refptr
<HeadsUpDisplayLayer
> hud_layer_
;
430 base::WeakPtr
<InputHandler
> input_handler_weak_ptr_
;
431 base::WeakPtr
<TopControlsManager
> top_controls_manager_weak_ptr_
;
433 const LayerTreeSettings settings_
;
434 LayerTreeDebugState debug_state_
;
436 gfx::Size device_viewport_size_
;
437 bool top_controls_shrink_blink_size_
;
438 float top_controls_height_
;
439 float top_controls_shown_ratio_
;
440 float device_scale_factor_
;
444 base::OneShotTimer
<LayerTreeHost
> rate_limit_timer_
;
446 float page_scale_factor_
;
447 float min_page_scale_factor_
;
448 float max_page_scale_factor_
;
449 gfx::Vector2dF elastic_overscroll_
;
450 bool has_gpu_rasterization_trigger_
;
451 bool content_is_suitable_for_gpu_rasterization_
;
452 bool gpu_rasterization_histogram_recorded_
;
454 SkColor background_color_
;
455 bool has_transparent_background_
;
457 scoped_ptr
<AnimationRegistrar
> animation_registrar_
;
458 scoped_ptr
<AnimationHost
> animation_host_
;
460 scoped_ptr
<PendingPageScaleAnimation
> pending_page_scale_animation_
;
462 // If set, then page scale animation has completed, but the client hasn't been
463 // notified about it yet.
464 bool did_complete_scale_animation_
;
466 bool in_paint_layer_contents_
;
469 bool next_commit_forces_redraw_
;
471 scoped_refptr
<Layer
> overscroll_elasticity_layer_
;
472 scoped_refptr
<Layer
> page_scale_layer_
;
473 scoped_refptr
<Layer
> inner_viewport_scroll_layer_
;
474 scoped_refptr
<Layer
> outer_viewport_scroll_layer_
;
476 LayerSelection selection_
;
478 SharedBitmapManager
* shared_bitmap_manager_
;
479 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager_
;
480 TaskGraphRunner
* task_graph_runner_
;
482 ScopedPtrVector
<SwapPromise
> swap_promise_list_
;
483 std::set
<SwapPromiseMonitor
*> swap_promise_monitor_
;
485 PropertyTrees property_trees_
;
487 typedef base::hash_map
<int, Layer
*> LayerIdMap
;
488 LayerIdMap layer_id_map_
;
490 uint32_t surface_id_namespace_
;
491 uint32_t next_surface_sequence_
;
493 DISALLOW_COPY_AND_ASSIGN(LayerTreeHost
);
498 #endif // CC_TREES_LAYER_TREE_HOST_H_