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
;
135 PropertyTrees
* property_trees() { return &property_trees_
; }
137 void PushPropertiesTo(LayerTreeImpl
* tree_impl
);
139 int source_frame_number() const { return source_frame_number_
; }
140 void set_source_frame_number(int frame_number
) {
141 source_frame_number_
= frame_number
;
144 HeadsUpDisplayLayerImpl
* hud_layer() { return hud_layer_
; }
145 void set_hud_layer(HeadsUpDisplayLayerImpl
* layer_impl
) {
146 hud_layer_
= layer_impl
;
149 LayerImpl
* InnerViewportScrollLayer() const;
150 // This function may return NULL, it is the caller's responsibility to check.
151 LayerImpl
* OuterViewportScrollLayer() const;
152 gfx::ScrollOffset
TotalScrollOffset() const;
153 gfx::ScrollOffset
TotalMaxScrollOffset() const;
155 LayerImpl
* InnerViewportContainerLayer() const;
156 LayerImpl
* OuterViewportContainerLayer() const;
157 LayerImpl
* CurrentlyScrollingLayer() const;
158 void SetCurrentlyScrollingLayer(LayerImpl
* layer
);
159 void ClearCurrentlyScrollingLayer();
161 void SetViewportLayersFromIds(int overscroll_elasticity_layer
,
162 int page_scale_layer_id
,
163 int inner_viewport_scroll_layer_id
,
164 int outer_viewport_scroll_layer_id
);
165 void ClearViewportLayers();
166 LayerImpl
* overscroll_elasticity_layer() {
167 return overscroll_elasticity_layer_
;
169 LayerImpl
* page_scale_layer() { return page_scale_layer_
; }
170 void ApplySentScrollAndScaleDeltasFromAbortedCommit();
172 SkColor
background_color() const { return background_color_
; }
173 void set_background_color(SkColor color
) { background_color_
= color
; }
175 bool has_transparent_background() const {
176 return has_transparent_background_
;
178 void set_has_transparent_background(bool transparent
) {
179 has_transparent_background_
= transparent
;
182 void SetPageScaleOnActiveTree(float active_page_scale
);
183 void PushPageScaleFromMainThread(float page_scale_factor
,
184 float min_page_scale_factor
,
185 float max_page_scale_factor
);
186 float current_page_scale_factor() const {
187 return page_scale_factor()->Current(IsActiveTree());
189 float min_page_scale_factor() const { return min_page_scale_factor_
; }
190 float max_page_scale_factor() const { return max_page_scale_factor_
; }
192 float page_scale_delta() const { return page_scale_factor()->Delta(); }
194 SyncedProperty
<ScaleGroup
>* page_scale_factor();
195 const SyncedProperty
<ScaleGroup
>* page_scale_factor() const;
197 SyncedElasticOverscroll
* elastic_overscroll() {
198 return elastic_overscroll_
.get();
200 const SyncedElasticOverscroll
* elastic_overscroll() const {
201 return elastic_overscroll_
.get();
204 SyncedTopControls
* top_controls_shown_ratio() {
205 return top_controls_shown_ratio_
.get();
207 const SyncedTopControls
* top_controls_shown_ratio() const {
208 return top_controls_shown_ratio_
.get();
211 // Updates draw properties and render surface layer list, as well as tile
212 // priorities. Returns false if it was unable to update. Updating lcd
213 // text may cause invalidations, so should only be done after a commit.
214 bool UpdateDrawProperties(bool update_lcd_text
);
215 void BuildPropertyTreesForTesting();
217 void set_needs_update_draw_properties() {
218 needs_update_draw_properties_
= true;
220 bool needs_update_draw_properties() const {
221 return needs_update_draw_properties_
;
224 void set_needs_full_tree_sync(bool needs
) { needs_full_tree_sync_
= needs
; }
225 bool needs_full_tree_sync() const { return needs_full_tree_sync_
; }
227 void ForceRedrawNextActivation() { next_activation_forces_redraw_
= true; }
229 void set_has_ever_been_drawn(bool has_drawn
) {
230 has_ever_been_drawn_
= has_drawn
;
232 bool has_ever_been_drawn() const { return has_ever_been_drawn_
; }
234 void set_ui_resource_request_queue(const UIResourceRequestQueue
& queue
);
236 const LayerImplList
& RenderSurfaceLayerList() const;
237 const Region
& UnoccludedScreenSpaceRegion() const;
239 // These return the size of the root scrollable area and the size of
240 // the user-visible scrolling viewport, in CSS layout coordinates.
241 gfx::Size
ScrollableSize() const;
242 gfx::SizeF
ScrollableViewportSize() const;
244 gfx::Rect
RootScrollLayerDeviceViewportBounds() const;
246 LayerImpl
* LayerById(int id
);
248 // These should be called by LayerImpl's ctor/dtor.
249 void RegisterLayer(LayerImpl
* layer
);
250 void UnregisterLayer(LayerImpl
* layer
);
254 AnimationRegistrar
* GetAnimationRegistrar() const;
256 void PushPersistedState(LayerTreeImpl
* pending_tree
);
258 void DidBecomeActive();
260 bool ContentsTexturesPurged() const;
261 void SetContentsTexturesPurged();
262 void ResetContentsTexturesPurged();
264 // Set on the active tree when the viewport size recently changed
265 // and the active tree's size is now out of date.
266 bool ViewportSizeInvalid() const;
267 void SetViewportSizeInvalid();
268 void ResetViewportSizeInvalid();
270 // Useful for debug assertions, probably shouldn't be used for anything else.
271 Proxy
* proxy() const;
273 void SetRootLayerScrollOffsetDelegate(
274 LayerScrollOffsetDelegate
* root_layer_scroll_offset_delegate
);
275 void UpdateRootScrollOffsetDelegate();
276 // Distribute the rool scroll between outer and inner viewport scroll layer.
277 // The outer viewport scroll layer scrolls first.
278 void DistributeRootScrollOffset();
280 // Call this function when you expect there to be a swap buffer.
281 // See swap_promise.h for how to use SwapPromise.
282 void QueueSwapPromise(scoped_ptr
<SwapPromise
> swap_promise
);
284 // Take the |new_swap_promise| and append it to |swap_promise_list_|.
285 void PassSwapPromises(ScopedPtrVector
<SwapPromise
>* new_swap_promise
);
286 void FinishSwapPromises(CompositorFrameMetadata
* metadata
);
287 void BreakSwapPromises(SwapPromise::DidNotSwapReason reason
);
289 void DidModifyTilePriorities();
291 ResourceId
ResourceIdForUIResource(UIResourceId uid
) const;
292 void ProcessUIResourceRequestQueue();
294 bool IsUIResourceOpaque(UIResourceId uid
) const;
296 void RegisterPictureLayerImpl(PictureLayerImpl
* layer
);
297 void UnregisterPictureLayerImpl(PictureLayerImpl
* layer
);
298 const std::vector
<PictureLayerImpl
*>& picture_layers() const {
299 return picture_layers_
;
302 void AddLayerWithCopyOutputRequest(LayerImpl
* layer
);
303 void RemoveLayerWithCopyOutputRequest(LayerImpl
* layer
);
304 const std::vector
<LayerImpl
*>& LayersWithCopyOutputRequest() const;
306 int current_render_surface_list_id() const {
307 return render_surface_layer_list_id_
;
310 LayerImpl
* FindFirstScrollingLayerThatIsHitByPoint(
311 const gfx::PointF
& screen_space_point
);
313 LayerImpl
* FindLayerThatIsHitByPoint(const gfx::PointF
& screen_space_point
);
315 LayerImpl
* FindLayerWithWheelHandlerThatIsHitByPoint(
316 const gfx::PointF
& screen_space_point
);
318 LayerImpl
* FindLayerThatIsHitByPointInTouchHandlerRegion(
319 const gfx::PointF
& screen_space_point
);
321 void RegisterSelection(const LayerSelection
& selection
);
323 // Compute the current selection handle location and visbility with respect to
325 void GetViewportSelection(ViewportSelection
* selection
);
327 void set_top_controls_shrink_blink_size(bool shrink
);
328 bool top_controls_shrink_blink_size() const {
329 return top_controls_shrink_blink_size_
;
331 bool SetCurrentTopControlsShownRatio(float ratio
);
332 float CurrentTopControlsShownRatio() const {
333 return top_controls_shown_ratio_
->Current(IsActiveTree());
335 void set_top_controls_height(float top_controls_height
);
336 float top_controls_height() const { return top_controls_height_
; }
337 void PushTopControlsFromMainThread(float top_controls_shown_ratio
);
339 void SetPendingPageScaleAnimation(
340 scoped_ptr
<PendingPageScaleAnimation
> pending_animation
);
341 scoped_ptr
<PendingPageScaleAnimation
> TakePendingPageScaleAnimation();
343 void GatherFrameTimingRequestIds(std::vector
<int64_t>* request_ids
);
345 bool IsExternalFlingActive() const;
346 void DidUpdateScrollOffset(int layer_id
);
349 explicit LayerTreeImpl(
350 LayerTreeHostImpl
* layer_tree_host_impl
,
351 scoped_refptr
<SyncedProperty
<ScaleGroup
>> page_scale_factor
,
352 scoped_refptr
<SyncedTopControls
> top_controls_shown_ratio
,
353 scoped_refptr
<SyncedElasticOverscroll
> elastic_overscroll
);
354 float ClampPageScaleFactorToLimits(float page_scale_factor
) const;
355 void PushPageScaleFactorAndLimits(const float* page_scale_factor
,
356 float min_page_scale_factor
,
357 float max_page_scale_factor
);
358 bool SetPageScaleFactorLimits(float min_page_scale_factor
,
359 float max_page_scale_factor
);
360 void DidUpdatePageScale();
361 void HideInnerViewportScrollbarsIfNearMinimumScale();
362 void PushTopControls(const float* top_controls_shown_ratio
);
363 LayerTreeHostImpl
* layer_tree_host_impl_
;
364 int source_frame_number_
;
365 scoped_ptr
<LayerImpl
> root_layer_
;
366 HeadsUpDisplayLayerImpl
* hud_layer_
;
367 PropertyTrees property_trees_
;
368 LayerImpl
* currently_scrolling_layer_
;
369 LayerScrollOffsetDelegate
* root_layer_scroll_offset_delegate_
;
370 SkColor background_color_
;
371 bool has_transparent_background_
;
373 LayerImpl
* overscroll_elasticity_layer_
;
374 LayerImpl
* page_scale_layer_
;
375 LayerImpl
* inner_viewport_scroll_layer_
;
376 LayerImpl
* outer_viewport_scroll_layer_
;
378 LayerSelection selection_
;
380 scoped_refptr
<SyncedProperty
<ScaleGroup
>> page_scale_factor_
;
381 float min_page_scale_factor_
;
382 float max_page_scale_factor_
;
384 scoped_refptr
<SyncedElasticOverscroll
> elastic_overscroll_
;
386 typedef base::hash_map
<int, LayerImpl
*> LayerIdMap
;
387 LayerIdMap layer_id_map_
;
389 std::vector
<PictureLayerImpl
*> picture_layers_
;
390 std::vector
<LayerImpl
*> layers_with_copy_output_request_
;
392 // Persisted state for non-impl-side-painting.
393 int scrolling_layer_id_from_previous_tree_
;
395 // List of visible layers for the most recently prepared frame.
396 LayerImplList render_surface_layer_list_
;
397 // After drawing the |render_surface_layer_list_| the areas in this region
398 // would not be fully covered by opaque content.
399 Region unoccluded_screen_space_region_
;
401 bool contents_textures_purged_
;
402 bool viewport_size_invalid_
;
403 bool needs_update_draw_properties_
;
405 // In impl-side painting mode, this is true when the tree may contain
406 // structural differences relative to the active tree.
407 bool needs_full_tree_sync_
;
409 bool next_activation_forces_redraw_
;
411 bool has_ever_been_drawn_
;
413 ScopedPtrVector
<SwapPromise
> swap_promise_list_
;
415 UIResourceRequestQueue ui_resource_request_queue_
;
417 int render_surface_layer_list_id_
;
419 // Whether or not Blink's viewport size was shrunk by the height of the top
420 // controls at the time of the last layout.
421 bool top_controls_shrink_blink_size_
;
423 float top_controls_height_
;
425 // The amount that the top controls are shown from 0 (hidden) to 1 (fully
427 scoped_refptr
<SyncedTopControls
> top_controls_shown_ratio_
;
429 scoped_ptr
<PendingPageScaleAnimation
> pending_page_scale_animation_
;
432 DISALLOW_COPY_AND_ASSIGN(LayerTreeImpl
);
437 #endif // CC_TREES_LAYER_TREE_IMPL_H_