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"
25 namespace trace_event
{
32 class ContextProvider
;
33 class DebugRectHistory
;
34 class FrameRateCounter
;
35 class HeadsUpDisplayLayerImpl
;
36 class LayerScrollOffsetDelegateProxy
;
37 class LayerTreeDebugState
;
39 class LayerTreeSettings
;
42 class PageScaleAnimation
;
43 class PaintTimeCounter
;
44 class PictureLayerImpl
;
46 class ResourceProvider
;
48 class UIResourceRequest
;
49 struct PendingPageScaleAnimation
;
50 struct RendererCapabilities
;
51 struct SelectionHandle
;
53 typedef std::vector
<UIResourceRequest
> UIResourceRequestQueue
;
54 typedef SyncedProperty
<AdditionGroup
<float>> SyncedTopControls
;
55 typedef SyncedProperty
<AdditionGroup
<gfx::Vector2dF
>> SyncedElasticOverscroll
;
57 class CC_EXPORT LayerTreeImpl
{
59 static scoped_ptr
<LayerTreeImpl
> create(
60 LayerTreeHostImpl
* layer_tree_host_impl
,
61 scoped_refptr
<SyncedProperty
<ScaleGroup
>> page_scale_factor
,
62 scoped_refptr
<SyncedTopControls
> top_controls_shown_ratio
,
63 scoped_refptr
<SyncedElasticOverscroll
> elastic_overscroll
) {
64 return make_scoped_ptr(
65 new LayerTreeImpl(layer_tree_host_impl
, page_scale_factor
,
66 top_controls_shown_ratio
, elastic_overscroll
));
68 virtual ~LayerTreeImpl();
71 void ReleaseResources();
72 void RecreateResources();
74 // Methods called by the layer tree that pass-through or access LTHI.
75 // ---------------------------------------------------------------------------
76 const LayerTreeSettings
& settings() const;
77 const LayerTreeDebugState
& debug_state() const;
78 const RendererCapabilitiesImpl
& GetRendererCapabilities() const;
79 ContextProvider
* context_provider() const;
80 OutputSurface
* output_surface() const;
81 ResourceProvider
* resource_provider() const;
82 TileManager
* tile_manager() const;
83 FrameRateCounter
* frame_rate_counter() const;
84 PaintTimeCounter
* paint_time_counter() const;
85 MemoryHistory
* memory_history() const;
86 gfx::Size
device_viewport_size() const;
87 float device_scale_factor() const;
88 DebugRectHistory
* debug_rect_history() const;
89 bool IsActiveTree() const;
90 bool IsPendingTree() const;
91 bool IsRecycleTree() const;
92 bool IsSyncTree() const;
93 LayerImpl
* FindActiveTreeLayerById(int id
);
94 LayerImpl
* FindPendingTreeLayerById(int id
);
95 bool PinchGestureActive() const;
96 BeginFrameArgs
CurrentBeginFrameArgs() const;
97 base::TimeDelta
begin_impl_frame_interval() const;
98 void SetNeedsCommit();
99 gfx::Rect
DeviceViewport() const;
100 gfx::Size
DrawViewportSize() const;
101 const gfx::Rect
ViewportRectForTilePriority() const;
102 scoped_ptr
<ScrollbarAnimationController
> CreateScrollbarAnimationController(
103 LayerImpl
* scrolling_layer
);
104 void DidAnimateScrollOffset();
105 void InputScrollAnimationFinished();
106 bool use_gpu_rasterization() const;
107 GpuRasterizationStatus
GetGpuRasterizationStatus() const;
108 bool create_low_res_tiling() const;
109 BlockingTaskRunner
* BlockingMainThreadTaskRunner() const;
110 bool RequiresHighResToDraw() const;
111 bool SmoothnessTakesPriority() const;
113 // Tree specific methods exposed to layer-impl tree.
114 // ---------------------------------------------------------------------------
115 void SetNeedsRedraw();
118 // ---------------------------------------------------------------------------
119 void GetAllTilesForTracing(std::set
<const Tile
*>* tiles
) const;
120 void AsValueInto(base::trace_event::TracedValue
* dict
) const;
122 // Other public methods
123 // ---------------------------------------------------------------------------
124 LayerImpl
* root_layer() const { return root_layer_
.get(); }
125 void SetRootLayer(scoped_ptr
<LayerImpl
>);
126 scoped_ptr
<LayerImpl
> DetachLayerTree();
128 void PushPropertiesTo(LayerTreeImpl
* tree_impl
);
130 int source_frame_number() const { return source_frame_number_
; }
131 void set_source_frame_number(int frame_number
) {
132 source_frame_number_
= frame_number
;
135 HeadsUpDisplayLayerImpl
* hud_layer() { return hud_layer_
; }
136 void set_hud_layer(HeadsUpDisplayLayerImpl
* layer_impl
) {
137 hud_layer_
= layer_impl
;
140 LayerImpl
* InnerViewportScrollLayer() const;
141 // This function may return NULL, it is the caller's responsibility to check.
142 LayerImpl
* OuterViewportScrollLayer() const;
143 gfx::ScrollOffset
TotalScrollOffset() const;
144 gfx::ScrollOffset
TotalMaxScrollOffset() const;
146 LayerImpl
* InnerViewportContainerLayer() const;
147 LayerImpl
* OuterViewportContainerLayer() const;
148 LayerImpl
* CurrentlyScrollingLayer() const;
149 void SetCurrentlyScrollingLayer(LayerImpl
* layer
);
150 void ClearCurrentlyScrollingLayer();
152 void SetViewportLayersFromIds(int overscroll_elasticity_layer
,
153 int page_scale_layer_id
,
154 int inner_viewport_scroll_layer_id
,
155 int outer_viewport_scroll_layer_id
);
156 void ClearViewportLayers();
157 LayerImpl
* overscroll_elasticity_layer() {
158 return overscroll_elasticity_layer_
;
160 LayerImpl
* page_scale_layer() { return page_scale_layer_
; }
161 void ApplySentScrollAndScaleDeltasFromAbortedCommit();
163 SkColor
background_color() const { return background_color_
; }
164 void set_background_color(SkColor color
) { background_color_
= color
; }
166 bool has_transparent_background() const {
167 return has_transparent_background_
;
169 void set_has_transparent_background(bool transparent
) {
170 has_transparent_background_
= transparent
;
173 void SetPageScaleOnActiveTree(float active_page_scale
);
174 void PushPageScaleFromMainThread(float page_scale_factor
,
175 float min_page_scale_factor
,
176 float max_page_scale_factor
);
177 float current_page_scale_factor() const {
178 return page_scale_factor()->Current(IsActiveTree());
180 float min_page_scale_factor() const { return min_page_scale_factor_
; }
181 float max_page_scale_factor() const { return max_page_scale_factor_
; }
183 float page_scale_delta() const { return page_scale_factor()->Delta(); }
185 SyncedProperty
<ScaleGroup
>* page_scale_factor();
186 const SyncedProperty
<ScaleGroup
>* page_scale_factor() const;
188 SyncedElasticOverscroll
* elastic_overscroll() {
189 return elastic_overscroll_
.get();
191 const SyncedElasticOverscroll
* elastic_overscroll() const {
192 return elastic_overscroll_
.get();
195 SyncedTopControls
* top_controls_shown_ratio() {
196 return top_controls_shown_ratio_
.get();
198 const SyncedTopControls
* top_controls_shown_ratio() const {
199 return top_controls_shown_ratio_
.get();
202 // Updates draw properties and render surface layer list, as well as tile
203 // priorities. Returns false if it was unable to update. Updating lcd
204 // text may cause invalidations, so should only be done after a commit.
205 bool UpdateDrawProperties(bool update_lcd_text
);
207 void set_needs_update_draw_properties() {
208 needs_update_draw_properties_
= true;
210 bool needs_update_draw_properties() const {
211 return needs_update_draw_properties_
;
214 void set_needs_full_tree_sync(bool needs
) { needs_full_tree_sync_
= needs
; }
215 bool needs_full_tree_sync() const { return needs_full_tree_sync_
; }
217 void ForceRedrawNextActivation() { next_activation_forces_redraw_
= true; }
219 void set_has_ever_been_drawn(bool has_drawn
) {
220 has_ever_been_drawn_
= has_drawn
;
222 bool has_ever_been_drawn() const { return has_ever_been_drawn_
; }
224 void set_ui_resource_request_queue(const UIResourceRequestQueue
& queue
);
226 const LayerImplList
& RenderSurfaceLayerList() const;
227 const Region
& UnoccludedScreenSpaceRegion() const;
229 // These return the size of the root scrollable area and the size of
230 // the user-visible scrolling viewport, in CSS layout coordinates.
231 gfx::Size
ScrollableSize() const;
232 gfx::SizeF
ScrollableViewportSize() const;
234 gfx::Rect
RootScrollLayerDeviceViewportBounds() const;
236 LayerImpl
* LayerById(int id
);
238 // These should be called by LayerImpl's ctor/dtor.
239 void RegisterLayer(LayerImpl
* layer
);
240 void UnregisterLayer(LayerImpl
* layer
);
244 AnimationRegistrar
* GetAnimationRegistrar() const;
246 void PushPersistedState(LayerTreeImpl
* pending_tree
);
248 void DidBecomeActive();
250 bool ContentsTexturesPurged() const;
251 void SetContentsTexturesPurged();
252 void ResetContentsTexturesPurged();
254 // Set on the active tree when the viewport size recently changed
255 // and the active tree's size is now out of date.
256 bool ViewportSizeInvalid() const;
257 void SetViewportSizeInvalid();
258 void ResetViewportSizeInvalid();
260 // Useful for debug assertions, probably shouldn't be used for anything else.
261 Proxy
* proxy() const;
263 void SetRootLayerScrollOffsetDelegate(
264 LayerScrollOffsetDelegate
* root_layer_scroll_offset_delegate
);
265 void OnRootLayerDelegatedScrollOffsetChanged();
266 void UpdateScrollOffsetDelegate();
267 gfx::ScrollOffset
GetDelegatedScrollOffset(LayerImpl
* layer
);
269 // Call this function when you expect there to be a swap buffer.
270 // See swap_promise.h for how to use SwapPromise.
271 void QueueSwapPromise(scoped_ptr
<SwapPromise
> swap_promise
);
273 // Take the |new_swap_promise| and append it to |swap_promise_list_|.
274 void PassSwapPromises(ScopedPtrVector
<SwapPromise
>* new_swap_promise
);
275 void FinishSwapPromises(CompositorFrameMetadata
* metadata
);
276 void BreakSwapPromises(SwapPromise::DidNotSwapReason reason
);
278 void DidModifyTilePriorities();
280 ResourceProvider::ResourceId
ResourceIdForUIResource(UIResourceId uid
) const;
281 void ProcessUIResourceRequestQueue();
283 bool IsUIResourceOpaque(UIResourceId uid
) const;
285 void RegisterPictureLayerImpl(PictureLayerImpl
* layer
);
286 void UnregisterPictureLayerImpl(PictureLayerImpl
* layer
);
287 const std::vector
<PictureLayerImpl
*>& picture_layers() const {
288 return picture_layers_
;
291 void AddLayerWithCopyOutputRequest(LayerImpl
* layer
);
292 void RemoveLayerWithCopyOutputRequest(LayerImpl
* layer
);
293 const std::vector
<LayerImpl
*>& LayersWithCopyOutputRequest() const;
295 int current_render_surface_list_id() const {
296 return render_surface_layer_list_id_
;
299 LayerImpl
* FindFirstScrollingLayerThatIsHitByPoint(
300 const gfx::PointF
& screen_space_point
);
302 LayerImpl
* FindLayerThatIsHitByPoint(const gfx::PointF
& screen_space_point
);
304 LayerImpl
* FindLayerWithWheelHandlerThatIsHitByPoint(
305 const gfx::PointF
& screen_space_point
);
307 LayerImpl
* FindLayerThatIsHitByPointInTouchHandlerRegion(
308 const gfx::PointF
& screen_space_point
);
310 void RegisterSelection(const LayerSelectionBound
& start
,
311 const LayerSelectionBound
& end
);
313 // Compute the current selection handle location and visbility with respect to
315 void GetViewportSelection(ViewportSelectionBound
* start
,
316 ViewportSelectionBound
* end
);
318 void set_top_controls_shrink_blink_size(bool shrink
);
319 bool top_controls_shrink_blink_size() const {
320 return top_controls_shrink_blink_size_
;
322 bool SetCurrentTopControlsShownRatio(float ratio
);
323 float CurrentTopControlsShownRatio() const {
324 return top_controls_shown_ratio_
->Current(IsActiveTree());
326 void set_top_controls_height(float top_controls_height
);
327 float top_controls_height() const { return top_controls_height_
; }
328 void PushTopControlsFromMainThread(float top_controls_shown_ratio
);
330 void SetPendingPageScaleAnimation(
331 scoped_ptr
<PendingPageScaleAnimation
> pending_animation
);
332 scoped_ptr
<PendingPageScaleAnimation
> TakePendingPageScaleAnimation();
335 explicit LayerTreeImpl(
336 LayerTreeHostImpl
* layer_tree_host_impl
,
337 scoped_refptr
<SyncedProperty
<ScaleGroup
>> page_scale_factor
,
338 scoped_refptr
<SyncedTopControls
> top_controls_shown_ratio
,
339 scoped_refptr
<SyncedElasticOverscroll
> elastic_overscroll
);
340 float ClampPageScaleFactorToLimits(float page_scale_factor
) const;
341 void PushPageScaleFactorAndLimits(const float* page_scale_factor
,
342 float min_page_scale_factor
,
343 float max_page_scale_factor
);
344 bool SetPageScaleFactorLimits(float min_page_scale_factor
,
345 float max_page_scale_factor
);
346 void DidUpdatePageScale();
347 void HideInnerViewportScrollbarsIfNearMinimumScale();
348 void PushTopControls(const float* top_controls_shown_ratio
);
349 LayerTreeHostImpl
* layer_tree_host_impl_
;
350 int source_frame_number_
;
351 scoped_ptr
<LayerImpl
> root_layer_
;
352 HeadsUpDisplayLayerImpl
* hud_layer_
;
353 LayerImpl
* currently_scrolling_layer_
;
354 LayerScrollOffsetDelegate
* root_layer_scroll_offset_delegate_
;
355 scoped_ptr
<LayerScrollOffsetDelegateProxy
>
356 inner_viewport_scroll_delegate_proxy_
;
357 scoped_ptr
<LayerScrollOffsetDelegateProxy
>
358 outer_viewport_scroll_delegate_proxy_
;
359 SkColor background_color_
;
360 bool has_transparent_background_
;
362 LayerImpl
* overscroll_elasticity_layer_
;
363 LayerImpl
* page_scale_layer_
;
364 LayerImpl
* inner_viewport_scroll_layer_
;
365 LayerImpl
* outer_viewport_scroll_layer_
;
367 LayerSelectionBound selection_start_
;
368 LayerSelectionBound selection_end_
;
370 scoped_refptr
<SyncedProperty
<ScaleGroup
>> page_scale_factor_
;
371 float min_page_scale_factor_
;
372 float max_page_scale_factor_
;
374 scoped_refptr
<SyncedElasticOverscroll
> elastic_overscroll_
;
376 typedef base::hash_map
<int, LayerImpl
*> LayerIdMap
;
377 LayerIdMap layer_id_map_
;
379 std::vector
<PictureLayerImpl
*> picture_layers_
;
380 std::vector
<LayerImpl
*> layers_with_copy_output_request_
;
382 // Persisted state for non-impl-side-painting.
383 int scrolling_layer_id_from_previous_tree_
;
385 // List of visible layers for the most recently prepared frame.
386 LayerImplList render_surface_layer_list_
;
387 // After drawing the |render_surface_layer_list_| the areas in this region
388 // would not be fully covered by opaque content.
389 Region unoccluded_screen_space_region_
;
391 bool contents_textures_purged_
;
392 bool viewport_size_invalid_
;
393 bool needs_update_draw_properties_
;
395 // In impl-side painting mode, this is true when the tree may contain
396 // structural differences relative to the active tree.
397 bool needs_full_tree_sync_
;
399 bool next_activation_forces_redraw_
;
401 bool has_ever_been_drawn_
;
403 ScopedPtrVector
<SwapPromise
> swap_promise_list_
;
405 UIResourceRequestQueue ui_resource_request_queue_
;
407 int render_surface_layer_list_id_
;
409 // Whether or not Blink's viewport size was shrunk by the height of the top
410 // controls at the time of the last layout.
411 bool top_controls_shrink_blink_size_
;
413 float top_controls_height_
;
415 // The amount that the top controls are shown from 0 (hidden) to 1 (fully
417 scoped_refptr
<SyncedTopControls
> top_controls_shown_ratio_
;
419 scoped_ptr
<PendingPageScaleAnimation
> pending_page_scale_animation_
;
422 DISALLOW_COPY_AND_ASSIGN(LayerTreeImpl
);
427 #endif // CC_TREES_LAYER_TREE_IMPL_H_