1 // Copyright 2012 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_IMPL_H_
6 #define CC_TREES_LAYER_TREE_IMPL_H_
12 #include "base/containers/hash_tables.h"
13 #include "base/values.h"
14 #include "cc/base/scoped_ptr_vector.h"
15 #include "cc/base/synced_property.h"
16 #include "cc/input/layer_selection_bound.h"
17 #include "cc/layers/layer_impl.h"
18 #include "cc/output/begin_frame_args.h"
19 #include "cc/output/renderer.h"
20 #include "cc/output/swap_promise.h"
21 #include "cc/resources/ui_resource_client.h"
22 #include "cc/trees/layer_tree_host_impl.h"
23 #include "cc/trees/property_tree.h"
26 namespace trace_event
{
33 class ContextProvider
;
34 class DebugRectHistory
;
35 class FrameRateCounter
;
36 class HeadsUpDisplayLayerImpl
;
37 class LayerExternalScrollOffsetListener
;
38 class LayerScrollOffsetDelegate
;
39 class LayerTreeDebugState
;
41 class LayerTreeSettings
;
44 class PageScaleAnimation
;
45 class PaintTimeCounter
;
46 class PictureLayerImpl
;
48 class ResourceProvider
;
50 class UIResourceRequest
;
51 class VideoFrameControllerClient
;
52 struct PendingPageScaleAnimation
;
53 struct RendererCapabilities
;
55 typedef std::vector
<UIResourceRequest
> UIResourceRequestQueue
;
56 typedef SyncedProperty
<AdditionGroup
<float>> SyncedTopControls
;
57 typedef SyncedProperty
<AdditionGroup
<gfx::Vector2dF
>> SyncedElasticOverscroll
;
59 class CC_EXPORT LayerTreeImpl
{
61 static scoped_ptr
<LayerTreeImpl
> create(
62 LayerTreeHostImpl
* layer_tree_host_impl
,
63 scoped_refptr
<SyncedProperty
<ScaleGroup
>> page_scale_factor
,
64 scoped_refptr
<SyncedTopControls
> top_controls_shown_ratio
,
65 scoped_refptr
<SyncedElasticOverscroll
> elastic_overscroll
) {
66 return make_scoped_ptr(
67 new LayerTreeImpl(layer_tree_host_impl
, page_scale_factor
,
68 top_controls_shown_ratio
, elastic_overscroll
));
70 virtual ~LayerTreeImpl();
73 void ReleaseResources();
74 void RecreateResources();
76 // Methods called by the layer tree that pass-through or access LTHI.
77 // ---------------------------------------------------------------------------
78 const LayerTreeSettings
& settings() const;
79 const LayerTreeDebugState
& debug_state() const;
80 const RendererCapabilitiesImpl
& GetRendererCapabilities() const;
81 ContextProvider
* context_provider() const;
82 OutputSurface
* output_surface() const;
83 ResourceProvider
* resource_provider() const;
84 TileManager
* tile_manager() const;
85 FrameRateCounter
* frame_rate_counter() const;
86 PaintTimeCounter
* paint_time_counter() const;
87 MemoryHistory
* memory_history() const;
88 gfx::Size
device_viewport_size() const;
89 float device_scale_factor() const;
90 DebugRectHistory
* debug_rect_history() const;
91 bool IsActiveTree() const;
92 bool IsPendingTree() const;
93 bool IsRecycleTree() const;
94 bool IsSyncTree() const;
95 LayerImpl
* FindActiveTreeLayerById(int id
);
96 LayerImpl
* FindPendingTreeLayerById(int id
);
97 bool PinchGestureActive() const;
98 BeginFrameArgs
CurrentBeginFrameArgs() const;
99 base::TimeDelta
CurrentBeginFrameInterval() const;
100 void SetNeedsCommit();
101 gfx::Rect
DeviceViewport() const;
102 gfx::Size
DrawViewportSize() const;
103 const gfx::Rect
ViewportRectForTilePriority() const;
104 scoped_ptr
<ScrollbarAnimationController
> CreateScrollbarAnimationController(
105 LayerImpl
* scrolling_layer
);
106 void DidAnimateScrollOffset();
107 void InputScrollAnimationFinished();
108 bool use_gpu_rasterization() const;
109 GpuRasterizationStatus
GetGpuRasterizationStatus() const;
110 bool create_low_res_tiling() const;
111 BlockingTaskRunner
* BlockingMainThreadTaskRunner() const;
112 bool RequiresHighResToDraw() const;
113 bool SmoothnessTakesPriority() const;
114 VideoFrameControllerClient
* GetVideoFrameControllerClient() const;
116 // Tree specific methods exposed to layer-impl tree.
117 // ---------------------------------------------------------------------------
118 void SetNeedsRedraw();
121 // ---------------------------------------------------------------------------
122 void GetAllPrioritizedTilesForTracing(
123 std::vector
<PrioritizedTile
>* prioritized_tiles
) const;
124 void AsValueInto(base::trace_event::TracedValue
* dict
) const;
126 // Other public methods
127 // ---------------------------------------------------------------------------
128 LayerImpl
* root_layer() const { return root_layer_
.get(); }
129 void SetRootLayer(scoped_ptr
<LayerImpl
>);
130 scoped_ptr
<LayerImpl
> DetachLayerTree();
132 void SetPropertyTrees(const PropertyTrees
& property_trees
) {
133 property_trees_
= property_trees
;
134 property_trees_
.transform_tree
.set_source_to_parent_updates_allowed(false);
136 PropertyTrees
* property_trees() { return &property_trees_
; }
138 void UpdatePropertyTreesForBoundsDelta();
140 void PushPropertiesTo(LayerTreeImpl
* tree_impl
);
142 int source_frame_number() const { return source_frame_number_
; }
143 void set_source_frame_number(int frame_number
) {
144 source_frame_number_
= frame_number
;
147 HeadsUpDisplayLayerImpl
* hud_layer() { return hud_layer_
; }
148 void set_hud_layer(HeadsUpDisplayLayerImpl
* layer_impl
) {
149 hud_layer_
= layer_impl
;
152 LayerImpl
* InnerViewportScrollLayer() const;
153 // This function may return NULL, it is the caller's responsibility to check.
154 LayerImpl
* OuterViewportScrollLayer() const;
155 gfx::ScrollOffset
TotalScrollOffset() const;
156 gfx::ScrollOffset
TotalMaxScrollOffset() const;
158 LayerImpl
* InnerViewportContainerLayer() const;
159 LayerImpl
* OuterViewportContainerLayer() const;
160 LayerImpl
* CurrentlyScrollingLayer() const;
161 void SetCurrentlyScrollingLayer(LayerImpl
* layer
);
162 void ClearCurrentlyScrollingLayer();
164 void SetViewportLayersFromIds(int overscroll_elasticity_layer
,
165 int page_scale_layer_id
,
166 int inner_viewport_scroll_layer_id
,
167 int outer_viewport_scroll_layer_id
);
168 void ClearViewportLayers();
169 LayerImpl
* OverscrollElasticityLayer() {
170 return LayerById(overscroll_elasticity_layer_id_
);
172 LayerImpl
* PageScaleLayer() { return LayerById(page_scale_layer_id_
); }
173 void ApplySentScrollAndScaleDeltasFromAbortedCommit();
175 SkColor
background_color() const { return background_color_
; }
176 void set_background_color(SkColor color
) { background_color_
= color
; }
178 bool has_transparent_background() const {
179 return has_transparent_background_
;
181 void set_has_transparent_background(bool transparent
) {
182 has_transparent_background_
= transparent
;
185 void UpdatePropertyTreeScrollingAndAnimationFromMainThread();
186 void SetPageScaleOnActiveTree(float active_page_scale
);
187 void PushPageScaleFromMainThread(float page_scale_factor
,
188 float min_page_scale_factor
,
189 float max_page_scale_factor
);
190 float current_page_scale_factor() const {
191 return page_scale_factor()->Current(IsActiveTree());
193 float min_page_scale_factor() const { return min_page_scale_factor_
; }
194 float max_page_scale_factor() const { return max_page_scale_factor_
; }
196 float page_scale_delta() const { return page_scale_factor()->Delta(); }
198 SyncedProperty
<ScaleGroup
>* page_scale_factor();
199 const SyncedProperty
<ScaleGroup
>* page_scale_factor() const;
201 void set_hide_pinch_scrollbars_near_min_scale(bool hide
) {
202 hide_pinch_scrollbars_near_min_scale_
= hide
;
205 SyncedElasticOverscroll
* elastic_overscroll() {
206 return elastic_overscroll_
.get();
208 const SyncedElasticOverscroll
* elastic_overscroll() const {
209 return elastic_overscroll_
.get();
212 SyncedTopControls
* top_controls_shown_ratio() {
213 return top_controls_shown_ratio_
.get();
215 const SyncedTopControls
* top_controls_shown_ratio() const {
216 return top_controls_shown_ratio_
.get();
219 // Updates draw properties and render surface layer list, as well as tile
220 // priorities. Returns false if it was unable to update. Updating lcd
221 // text may cause invalidations, so should only be done after a commit.
222 bool UpdateDrawProperties(bool update_lcd_text
);
223 void BuildPropertyTreesForTesting();
225 void set_needs_update_draw_properties() {
226 needs_update_draw_properties_
= true;
228 bool needs_update_draw_properties() const {
229 return needs_update_draw_properties_
;
232 void set_needs_full_tree_sync(bool needs
) { needs_full_tree_sync_
= needs
; }
233 bool needs_full_tree_sync() const { return needs_full_tree_sync_
; }
235 void ForceRedrawNextActivation() { next_activation_forces_redraw_
= true; }
237 void set_has_ever_been_drawn(bool has_drawn
) {
238 has_ever_been_drawn_
= has_drawn
;
240 bool has_ever_been_drawn() const { return has_ever_been_drawn_
; }
242 void set_ui_resource_request_queue(const UIResourceRequestQueue
& queue
);
244 const LayerImplList
& RenderSurfaceLayerList() const;
245 const Region
& UnoccludedScreenSpaceRegion() const;
247 // These return the size of the root scrollable area and the size of
248 // the user-visible scrolling viewport, in CSS layout coordinates.
249 gfx::SizeF
ScrollableSize() const;
250 gfx::SizeF
ScrollableViewportSize() const;
252 gfx::Rect
RootScrollLayerDeviceViewportBounds() const;
254 LayerImpl
* LayerById(int id
) const;
256 // These should be called by LayerImpl's ctor/dtor.
257 void RegisterLayer(LayerImpl
* layer
);
258 void UnregisterLayer(LayerImpl
* layer
);
262 AnimationRegistrar
* GetAnimationRegistrar() const;
264 void DidBecomeActive();
266 // Set on the active tree when the viewport size recently changed
267 // and the active tree's size is now out of date.
268 bool ViewportSizeInvalid() const;
269 void SetViewportSizeInvalid();
270 void ResetViewportSizeInvalid();
272 // Useful for debug assertions, probably shouldn't be used for anything else.
273 Proxy
* proxy() const;
275 void SetRootLayerScrollOffsetDelegate(
276 LayerScrollOffsetDelegate
* root_layer_scroll_offset_delegate
);
277 void UpdateRootScrollOffsetDelegate();
278 // Distribute the rool scroll between outer and inner viewport scroll layer.
279 // The outer viewport scroll layer scrolls first.
280 void DistributeRootScrollOffset();
282 void ApplyScroll(LayerImpl
* layer
, ScrollState
* scroll_state
) {
283 layer_tree_host_impl_
->ApplyScroll(layer
, scroll_state
);
286 // Call this function when you expect there to be a swap buffer.
287 // See swap_promise.h for how to use SwapPromise.
288 void QueueSwapPromise(scoped_ptr
<SwapPromise
> swap_promise
);
290 // Take the |new_swap_promise| and append it to |swap_promise_list_|.
291 void PassSwapPromises(ScopedPtrVector
<SwapPromise
>* new_swap_promise
);
292 void FinishSwapPromises(CompositorFrameMetadata
* metadata
);
293 void BreakSwapPromises(SwapPromise::DidNotSwapReason reason
);
295 void DidModifyTilePriorities();
297 ResourceId
ResourceIdForUIResource(UIResourceId uid
) const;
298 void ProcessUIResourceRequestQueue();
300 bool IsUIResourceOpaque(UIResourceId uid
) const;
302 void RegisterPictureLayerImpl(PictureLayerImpl
* layer
);
303 void UnregisterPictureLayerImpl(PictureLayerImpl
* layer
);
304 const std::vector
<PictureLayerImpl
*>& picture_layers() const {
305 return picture_layers_
;
308 void AddLayerWithCopyOutputRequest(LayerImpl
* layer
);
309 void RemoveLayerWithCopyOutputRequest(LayerImpl
* layer
);
310 const std::vector
<LayerImpl
*>& LayersWithCopyOutputRequest() const;
312 int current_render_surface_list_id() const {
313 return render_surface_layer_list_id_
;
316 LayerImpl
* FindFirstScrollingLayerThatIsHitByPoint(
317 const gfx::PointF
& screen_space_point
);
319 LayerImpl
* FindLayerThatIsHitByPoint(const gfx::PointF
& screen_space_point
);
321 LayerImpl
* FindLayerWithWheelHandlerThatIsHitByPoint(
322 const gfx::PointF
& screen_space_point
);
324 LayerImpl
* FindLayerThatIsHitByPointInTouchHandlerRegion(
325 const gfx::PointF
& screen_space_point
);
327 void RegisterSelection(const LayerSelection
& selection
);
329 // Compute the current selection handle location and visbility with respect to
331 void GetViewportSelection(ViewportSelection
* selection
);
333 void set_top_controls_shrink_blink_size(bool shrink
);
334 bool top_controls_shrink_blink_size() const {
335 return top_controls_shrink_blink_size_
;
337 bool SetCurrentTopControlsShownRatio(float ratio
);
338 float CurrentTopControlsShownRatio() const {
339 return top_controls_shown_ratio_
->Current(IsActiveTree());
341 void set_top_controls_height(float top_controls_height
);
342 float top_controls_height() const { return top_controls_height_
; }
343 void PushTopControlsFromMainThread(float top_controls_shown_ratio
);
345 void SetPendingPageScaleAnimation(
346 scoped_ptr
<PendingPageScaleAnimation
> pending_animation
);
347 scoped_ptr
<PendingPageScaleAnimation
> TakePendingPageScaleAnimation();
349 void GatherFrameTimingRequestIds(std::vector
<int64_t>* request_ids
);
351 bool IsExternalScrollActive() const;
352 void DidUpdateScrollOffset(int layer_id
);
354 bool IsAnimatingFilterProperty(const LayerImpl
* layer
) const;
355 bool IsAnimatingOpacityProperty(const LayerImpl
* layer
) const;
356 bool IsAnimatingTransformProperty(const LayerImpl
* layer
) const;
358 bool HasPotentiallyRunningFilterAnimation(const LayerImpl
* layer
) const;
359 bool HasPotentiallyRunningOpacityAnimation(const LayerImpl
* layer
) const;
360 bool HasPotentiallyRunningTransformAnimation(const LayerImpl
* layer
) const;
362 bool HasAnyAnimationTargetingProperty(
363 const LayerImpl
* layer
,
364 Animation::TargetProperty property
) const;
366 bool FilterIsAnimatingOnImplOnly(const LayerImpl
* layer
) const;
367 bool OpacityIsAnimatingOnImplOnly(const LayerImpl
* layer
) const;
368 bool TransformIsAnimatingOnImplOnly(const LayerImpl
* layer
) const;
370 bool HasOnlyTranslationTransforms(const LayerImpl
* layer
) const;
372 bool MaximumTargetScale(const LayerImpl
* layer
, float* max_scale
) const;
373 bool AnimationStartScale(const LayerImpl
* layer
, float* start_scale
) const;
375 bool HasFilterAnimationThatInflatesBounds(const LayerImpl
* layer
) const;
376 bool HasTransformAnimationThatInflatesBounds(const LayerImpl
* layer
) const;
377 bool HasAnimationThatInflatesBounds(const LayerImpl
* layer
) const;
379 bool FilterAnimationBoundsForBox(const LayerImpl
* layer
,
380 const gfx::BoxF
& box
,
381 gfx::BoxF
* bounds
) const;
382 bool TransformAnimationBoundsForBox(const LayerImpl
* layer
,
383 const gfx::BoxF
& box
,
384 gfx::BoxF
* bounds
) const;
387 explicit LayerTreeImpl(
388 LayerTreeHostImpl
* layer_tree_host_impl
,
389 scoped_refptr
<SyncedProperty
<ScaleGroup
>> page_scale_factor
,
390 scoped_refptr
<SyncedTopControls
> top_controls_shown_ratio
,
391 scoped_refptr
<SyncedElasticOverscroll
> elastic_overscroll
);
392 float ClampPageScaleFactorToLimits(float page_scale_factor
) const;
393 void PushPageScaleFactorAndLimits(const float* page_scale_factor
,
394 float min_page_scale_factor
,
395 float max_page_scale_factor
);
396 bool SetPageScaleFactorLimits(float min_page_scale_factor
,
397 float max_page_scale_factor
);
398 void DidUpdatePageScale();
399 void HideInnerViewportScrollbarsIfNeeded();
400 void PushTopControls(const float* top_controls_shown_ratio
);
401 LayerTreeHostImpl
* layer_tree_host_impl_
;
402 int source_frame_number_
;
403 scoped_ptr
<LayerImpl
> root_layer_
;
404 HeadsUpDisplayLayerImpl
* hud_layer_
;
405 PropertyTrees property_trees_
;
406 LayerScrollOffsetDelegate
* root_layer_scroll_offset_delegate_
;
407 SkColor background_color_
;
408 bool has_transparent_background_
;
410 int currently_scrolling_layer_id_
;
411 int overscroll_elasticity_layer_id_
;
412 int page_scale_layer_id_
;
413 int inner_viewport_scroll_layer_id_
;
414 int outer_viewport_scroll_layer_id_
;
416 LayerSelection selection_
;
418 scoped_refptr
<SyncedProperty
<ScaleGroup
>> page_scale_factor_
;
419 float min_page_scale_factor_
;
420 float max_page_scale_factor_
;
421 bool hide_pinch_scrollbars_near_min_scale_
;
423 scoped_refptr
<SyncedElasticOverscroll
> elastic_overscroll_
;
425 typedef base::hash_map
<int, LayerImpl
*> LayerIdMap
;
426 LayerIdMap layer_id_map_
;
428 std::vector
<PictureLayerImpl
*> picture_layers_
;
429 std::vector
<LayerImpl
*> layers_with_copy_output_request_
;
431 // List of visible layers for the most recently prepared frame.
432 LayerImplList render_surface_layer_list_
;
433 // After drawing the |render_surface_layer_list_| the areas in this region
434 // would not be fully covered by opaque content.
435 Region unoccluded_screen_space_region_
;
437 bool viewport_size_invalid_
;
438 bool needs_update_draw_properties_
;
440 // In impl-side painting mode, this is true when the tree may contain
441 // structural differences relative to the active tree.
442 bool needs_full_tree_sync_
;
444 bool next_activation_forces_redraw_
;
446 bool has_ever_been_drawn_
;
448 ScopedPtrVector
<SwapPromise
> swap_promise_list_
;
450 UIResourceRequestQueue ui_resource_request_queue_
;
452 int render_surface_layer_list_id_
;
454 // Whether or not Blink's viewport size was shrunk by the height of the top
455 // controls at the time of the last layout.
456 bool top_controls_shrink_blink_size_
;
458 float top_controls_height_
;
460 // The amount that the top controls are shown from 0 (hidden) to 1 (fully
462 scoped_refptr
<SyncedTopControls
> top_controls_shown_ratio_
;
464 scoped_ptr
<PendingPageScaleAnimation
> pending_page_scale_animation_
;
467 DISALLOW_COPY_AND_ASSIGN(LayerTreeImpl
);
472 #endif // CC_TREES_LAYER_TREE_IMPL_H_