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 void set_hide_pinch_scrollbars_near_min_scale(bool hide
) {
215 hide_pinch_scrollbars_near_min_scale_
= hide
;
218 gfx::Size
device_viewport_size() const { return device_viewport_size_
; }
220 void ApplyPageScaleDeltaFromImplSide(float page_scale_delta
);
221 void SetPageScaleFactorAndLimits(float page_scale_factor
,
222 float min_page_scale_factor
,
223 float max_page_scale_factor
);
224 float page_scale_factor() const { return page_scale_factor_
; }
225 gfx::Vector2dF
elastic_overscroll() const { return elastic_overscroll_
; }
227 SkColor
background_color() const { return background_color_
; }
228 void set_background_color(SkColor color
) { background_color_
= color
; }
230 void set_has_transparent_background(bool transparent
) {
231 has_transparent_background_
= transparent
;
234 void SetVisible(bool visible
);
235 bool visible() const { return visible_
; }
237 void SetThrottleFrameProduction(bool throttle
);
239 void StartPageScaleAnimation(const gfx::Vector2d
& target_offset
,
242 base::TimeDelta duration
);
244 void ApplyScrollAndScale(ScrollAndScaleSet
* info
);
245 void SetImplTransform(const gfx::Transform
& transform
);
247 // Virtual for tests.
248 virtual void StartRateLimiter();
249 virtual void StopRateLimiter();
253 void SetDeviceScaleFactor(float device_scale_factor
);
254 float device_scale_factor() const { return device_scale_factor_
; }
256 void UpdateTopControlsState(TopControlsState constraints
,
257 TopControlsState current
,
260 HeadsUpDisplayLayer
* hud_layer() const { return hud_layer_
.get(); }
262 Proxy
* proxy() const { return proxy_
.get(); }
263 AnimationRegistrar
* animation_registrar() const {
264 return animation_registrar_
.get();
266 AnimationHost
* animation_host() const { return animation_host_
.get(); }
268 bool in_paint_layer_contents() const { return in_paint_layer_contents_
; }
270 // CreateUIResource creates a resource given a bitmap. The bitmap is
271 // generated via an interface function, which is called when initializing the
272 // resource and when the resource has been lost (due to lost context). The
273 // parameter of the interface is a single boolean, which indicates whether the
274 // resource has been lost or not. CreateUIResource returns an Id of the
275 // resource, which is always positive.
276 virtual UIResourceId
CreateUIResource(UIResourceClient
* client
);
277 // Deletes a UI resource. May safely be called more than once.
278 virtual void DeleteUIResource(UIResourceId id
);
279 // Put the recreation of all UI resources into the resource queue after they
280 // were evicted on the impl thread.
281 void RecreateUIResources();
283 virtual gfx::Size
GetUIResourceSize(UIResourceId id
) const;
285 bool UsingSharedMemoryResources();
286 int id() const { return id_
; }
288 // Returns the id of the benchmark on success, 0 otherwise.
289 int ScheduleMicroBenchmark(const std::string
& benchmark_name
,
290 scoped_ptr
<base::Value
> value
,
291 const MicroBenchmark::DoneCallback
& callback
);
292 // Returns true if the message was successfully delivered and handled.
293 bool SendMessageToMicroBenchmark(int id
, scoped_ptr
<base::Value
> value
);
295 // When a SwapPromiseMonitor is created on the main thread, it calls
296 // InsertSwapPromiseMonitor() to register itself with LayerTreeHost.
297 // When the monitor is destroyed, it calls RemoveSwapPromiseMonitor()
298 // to unregister itself.
299 void InsertSwapPromiseMonitor(SwapPromiseMonitor
* monitor
);
300 void RemoveSwapPromiseMonitor(SwapPromiseMonitor
* monitor
);
302 // Call this function when you expect there to be a swap buffer.
303 // See swap_promise.h for how to use SwapPromise.
304 void QueueSwapPromise(scoped_ptr
<SwapPromise
> swap_promise
);
306 void BreakSwapPromises(SwapPromise::DidNotSwapReason reason
);
308 size_t num_queued_swap_promises() const { return swap_promise_list_
.size(); }
310 void set_surface_id_namespace(uint32_t id_namespace
);
311 SurfaceSequence
CreateSurfaceSequence();
313 void SetChildrenNeedBeginFrames(bool children_need_begin_frames
) const;
314 void SendBeginFramesToChildren(const BeginFrameArgs
& args
) const;
316 void SetAuthoritativeVSyncInterval(const base::TimeDelta
& interval
);
318 PropertyTrees
* property_trees() { return &property_trees_
; }
319 bool needs_meta_info_recomputation() {
320 return needs_meta_info_recomputation_
;
323 void RecordFrameTimingEvents(
324 scoped_ptr
<FrameTimingTracker::CompositeTimingSet
> composite_events
,
325 scoped_ptr
<FrameTimingTracker::MainFrameTimingSet
> main_frame_events
);
327 Layer
* LayerById(int id
) const;
328 void RegisterLayer(Layer
* layer
);
329 void UnregisterLayer(Layer
* layer
);
330 // LayerTreeMutatorsClient implementation.
331 bool IsLayerInTree(int layer_id
, LayerTreeType tree_type
) const override
;
332 void SetMutatorsNeedCommit() override
;
333 void SetLayerFilterMutated(int layer_id
,
334 LayerTreeType tree_type
,
335 const FilterOperations
& filters
) override
;
336 void SetLayerOpacityMutated(int layer_id
,
337 LayerTreeType tree_type
,
338 float opacity
) override
;
339 void SetLayerTransformMutated(int layer_id
,
340 LayerTreeType tree_type
,
341 const gfx::Transform
& transform
) override
;
342 void SetLayerScrollOffsetMutated(
344 LayerTreeType tree_type
,
345 const gfx::ScrollOffset
& scroll_offset
) override
;
346 void LayerTransformIsPotentiallyAnimatingChanged(int layer_id
,
347 LayerTreeType tree_type
,
348 bool is_animating
) override
;
349 void ScrollOffsetAnimationFinished() override
{}
350 gfx::ScrollOffset
GetScrollOffsetForAnimation(int layer_id
) const override
;
352 bool ScrollOffsetAnimationWasInterrupted(const Layer
* layer
) const;
353 bool IsAnimatingFilterProperty(const Layer
* layer
) const;
354 bool IsAnimatingOpacityProperty(const Layer
* layer
) const;
355 bool IsAnimatingTransformProperty(const Layer
* layer
) const;
356 bool HasPotentiallyRunningFilterAnimation(const Layer
* layer
) const;
357 bool HasPotentiallyRunningOpacityAnimation(const Layer
* layer
) const;
358 bool HasPotentiallyRunningTransformAnimation(const Layer
* layer
) const;
359 bool HasOnlyTranslationTransforms(const Layer
* layer
) const;
360 bool MaximumTargetScale(const Layer
* layer
, float* max_scale
) const;
361 bool AnimationStartScale(const Layer
* layer
, float* start_scale
) const;
362 bool HasAnyAnimationTargetingProperty(
364 Animation::TargetProperty property
) const;
365 bool AnimationsPreserveAxisAlignment(const Layer
* layer
) const;
366 bool HasAnyAnimation(const Layer
* layer
) const;
367 bool HasActiveAnimation(const Layer
* layer
) const;
370 explicit LayerTreeHost(InitParams
* params
);
371 void InitializeThreaded(
372 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
373 scoped_refptr
<base::SingleThreadTaskRunner
> impl_task_runner
,
374 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
);
375 void InitializeSingleThreaded(
376 LayerTreeHostSingleThreadClient
* single_thread_client
,
377 scoped_refptr
<base::SingleThreadTaskRunner
> main_task_runner
,
378 scoped_ptr
<BeginFrameSource
> external_begin_frame_source
);
379 void InitializeForTesting(scoped_ptr
<Proxy
> proxy_for_testing
);
380 void SetOutputSurfaceLostForTesting(bool is_lost
) {
381 output_surface_lost_
= is_lost
;
384 // shared_bitmap_manager(), gpu_memory_buffer_manager(), and
385 // task_graph_runner() return valid values only until the LayerTreeHostImpl is
386 // created in CreateLayerTreeHostImpl().
387 SharedBitmapManager
* shared_bitmap_manager() const {
388 return shared_bitmap_manager_
;
390 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager() const {
391 return gpu_memory_buffer_manager_
;
393 TaskGraphRunner
* task_graph_runner() const { return task_graph_runner_
; }
395 MicroBenchmarkController micro_benchmark_controller_
;
397 void OnCommitForSwapPromises();
400 void InitializeProxy(scoped_ptr
<Proxy
> proxy
);
402 bool DoUpdateLayers(Layer
* root_layer
);
403 void UpdateHudLayer();
405 bool AnimateLayersRecursive(Layer
* current
, base::TimeTicks time
);
407 struct UIResourceClientData
{
408 UIResourceClient
* client
;
412 typedef base::hash_map
<UIResourceId
, UIResourceClientData
>
414 UIResourceClientMap ui_resource_client_map_
;
415 int next_ui_resource_id_
;
417 typedef std::vector
<UIResourceRequest
> UIResourceRequestQueue
;
418 UIResourceRequestQueue ui_resource_request_queue_
;
420 void RecordGpuRasterizationHistogram();
421 void CalculateLCDTextMetricsCallback(Layer
* layer
);
423 void NotifySwapPromiseMonitorsOfSetNeedsCommit();
425 void SetPropertyTreesNeedRebuild();
427 bool inside_begin_main_frame_
;
428 bool needs_full_tree_sync_
;
429 bool needs_meta_info_recomputation_
;
431 LayerTreeHostClient
* client_
;
432 scoped_ptr
<Proxy
> proxy_
;
434 int source_frame_number_
;
435 int meta_information_sequence_number_
;
436 scoped_ptr
<RenderingStatsInstrumentation
> rendering_stats_instrumentation_
;
438 bool output_surface_lost_
;
440 scoped_refptr
<Layer
> root_layer_
;
441 scoped_refptr
<HeadsUpDisplayLayer
> hud_layer_
;
443 base::WeakPtr
<InputHandler
> input_handler_weak_ptr_
;
444 base::WeakPtr
<TopControlsManager
> top_controls_manager_weak_ptr_
;
446 const LayerTreeSettings settings_
;
447 LayerTreeDebugState debug_state_
;
449 gfx::Size device_viewport_size_
;
450 bool top_controls_shrink_blink_size_
;
451 float top_controls_height_
;
452 float top_controls_shown_ratio_
;
453 bool hide_pinch_scrollbars_near_min_scale_
;
454 float device_scale_factor_
;
458 base::OneShotTimer
<LayerTreeHost
> rate_limit_timer_
;
460 float page_scale_factor_
;
461 float min_page_scale_factor_
;
462 float max_page_scale_factor_
;
463 gfx::Vector2dF elastic_overscroll_
;
464 bool has_gpu_rasterization_trigger_
;
465 bool content_is_suitable_for_gpu_rasterization_
;
466 bool gpu_rasterization_histogram_recorded_
;
468 SkColor background_color_
;
469 bool has_transparent_background_
;
471 scoped_ptr
<AnimationRegistrar
> animation_registrar_
;
472 scoped_ptr
<AnimationHost
> animation_host_
;
474 scoped_ptr
<PendingPageScaleAnimation
> pending_page_scale_animation_
;
476 // If set, then page scale animation has completed, but the client hasn't been
477 // notified about it yet.
478 bool did_complete_scale_animation_
;
480 bool in_paint_layer_contents_
;
483 bool next_commit_forces_redraw_
;
485 scoped_refptr
<Layer
> overscroll_elasticity_layer_
;
486 scoped_refptr
<Layer
> page_scale_layer_
;
487 scoped_refptr
<Layer
> inner_viewport_scroll_layer_
;
488 scoped_refptr
<Layer
> outer_viewport_scroll_layer_
;
490 LayerSelection selection_
;
492 SharedBitmapManager
* shared_bitmap_manager_
;
493 gpu::GpuMemoryBufferManager
* gpu_memory_buffer_manager_
;
494 TaskGraphRunner
* task_graph_runner_
;
496 ScopedPtrVector
<SwapPromise
> swap_promise_list_
;
497 std::set
<SwapPromiseMonitor
*> swap_promise_monitor_
;
499 PropertyTrees property_trees_
;
501 typedef base::hash_map
<int, Layer
*> LayerIdMap
;
502 LayerIdMap layer_id_map_
;
504 uint32_t surface_id_namespace_
;
505 uint32_t next_surface_sequence_
;
507 DISALLOW_COPY_AND_ASSIGN(LayerTreeHost
);
512 #endif // CC_TREES_LAYER_TREE_HOST_H_