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