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_
13 #include "base/containers/hash_tables.h"
14 #include "base/values.h"
15 #include "cc/base/scoped_ptr_vector.h"
16 #include "cc/base/swap_promise.h"
17 #include "cc/layers/layer_impl.h"
18 #include "cc/output/renderer.h"
19 #include "cc/resources/ui_resource_client.h"
29 class ContextProvider
;
30 class DebugRectHistory
;
31 class FrameRateCounter
;
32 class HeadsUpDisplayLayerImpl
;
33 class LayerScrollOffsetDelegateProxy
;
34 class LayerTreeDebugState
;
35 class LayerTreeHostImpl
;
37 class LayerTreeSettings
;
40 class PageScaleAnimation
;
41 class PaintTimeCounter
;
42 class PictureLayerImpl
;
44 class ResourceProvider
;
46 class UIResourceRequest
;
47 struct RendererCapabilities
;
48 struct SelectionHandle
;
50 typedef std::list
<UIResourceRequest
> UIResourceRequestQueue
;
52 class CC_EXPORT LayerTreeImpl
{
54 static scoped_ptr
<LayerTreeImpl
> create(
55 LayerTreeHostImpl
* layer_tree_host_impl
) {
56 return make_scoped_ptr(new LayerTreeImpl(layer_tree_host_impl
));
58 virtual ~LayerTreeImpl();
61 void ReleaseResources();
63 // Methods called by the layer tree that pass-through or access LTHI.
64 // ---------------------------------------------------------------------------
65 const LayerTreeSettings
& settings() const;
66 const RendererCapabilitiesImpl
& GetRendererCapabilities() const;
67 ContextProvider
* context_provider() const;
68 OutputSurface
* output_surface() const;
69 ResourceProvider
* resource_provider() const;
70 TileManager
* tile_manager() const;
71 FrameRateCounter
* frame_rate_counter() const;
72 PaintTimeCounter
* paint_time_counter() const;
73 MemoryHistory
* memory_history() const;
74 gfx::Size
device_viewport_size() const;
75 bool IsActiveTree() const;
76 bool IsPendingTree() const;
77 bool IsRecycleTree() const;
78 LayerImpl
* FindActiveTreeLayerById(int id
);
79 LayerImpl
* FindPendingTreeLayerById(int id
);
80 bool PinchGestureActive() const;
81 BeginFrameArgs
CurrentBeginFrameArgs() const;
82 base::TimeDelta
begin_impl_frame_interval() const;
83 void SetNeedsCommit();
84 gfx::Rect
DeviceViewport() const;
85 gfx::Size
DrawViewportSize() const;
86 const gfx::Rect
ViewportRectForTilePriority() const;
87 scoped_ptr
<ScrollbarAnimationController
> CreateScrollbarAnimationController(
88 LayerImpl
* scrolling_layer
);
89 void DidAnimateScrollOffset();
90 void InputScrollAnimationFinished();
91 bool use_gpu_rasterization() const;
92 bool create_low_res_tiling() const;
93 BlockingTaskRunner
* BlockingMainThreadTaskRunner() const;
94 bool RequiresHighResToDraw() const;
95 bool SmoothnessTakesPriority() const;
97 // Tree specific methods exposed to layer-impl tree.
98 // ---------------------------------------------------------------------------
99 void SetNeedsRedraw();
101 // TODO(nduca): These are implemented in cc files temporarily, but will become
102 // trivial accessors in a followup patch.
103 const LayerTreeDebugState
& debug_state() const;
104 float device_scale_factor() const;
105 DebugRectHistory
* debug_rect_history() const;
106 void GetAllTilesForTracing(std::set
<const Tile
*>* tiles
) const;
107 void AsValueInto(base::debug::TracedValue
* dict
) const;
109 // Other public methods
110 // ---------------------------------------------------------------------------
111 LayerImpl
* root_layer() const { return root_layer_
.get(); }
112 void SetRootLayer(scoped_ptr
<LayerImpl
>);
113 scoped_ptr
<LayerImpl
> DetachLayerTree();
115 void PushPropertiesTo(LayerTreeImpl
* tree_impl
);
117 int source_frame_number() const { return source_frame_number_
; }
118 void set_source_frame_number(int frame_number
) {
119 source_frame_number_
= frame_number
;
122 HeadsUpDisplayLayerImpl
* hud_layer() { return hud_layer_
; }
123 void set_hud_layer(HeadsUpDisplayLayerImpl
* layer_impl
) {
124 hud_layer_
= layer_impl
;
127 LayerImpl
* InnerViewportScrollLayer() const;
128 // This function may return NULL, it is the caller's responsibility to check.
129 LayerImpl
* OuterViewportScrollLayer() const;
130 gfx::ScrollOffset
TotalScrollOffset() const;
131 gfx::ScrollOffset
TotalMaxScrollOffset() const;
132 gfx::Vector2dF
TotalScrollDelta() const;
134 LayerImpl
* InnerViewportContainerLayer() const;
135 LayerImpl
* OuterViewportContainerLayer() const;
136 LayerImpl
* CurrentlyScrollingLayer() const;
137 void SetCurrentlyScrollingLayer(LayerImpl
* layer
);
138 void ClearCurrentlyScrollingLayer();
140 void SetViewportLayersFromIds(int page_scale_layer_id
,
141 int inner_viewport_scroll_layer_id
,
142 int outer_viewport_scroll_layer_id
);
143 void ClearViewportLayers();
144 LayerImpl
* page_scale_layer() { return page_scale_layer_
; }
145 void ApplySentScrollAndScaleDeltasFromAbortedCommit();
146 void ApplyScrollDeltasSinceBeginMainFrame();
148 SkColor
background_color() const { return background_color_
; }
149 void set_background_color(SkColor color
) { background_color_
= color
; }
151 bool has_transparent_background() const {
152 return has_transparent_background_
;
154 void set_has_transparent_background(bool transparent
) {
155 has_transparent_background_
= transparent
;
158 void SetPageScaleFactorAndLimits(float page_scale_factor
,
159 float min_page_scale_factor
, float max_page_scale_factor
);
160 void SetPageScaleDelta(float delta
);
161 void SetPageScaleValues(float page_scale_factor
,
162 float min_page_scale_factor
, float max_page_scale_factor
,
163 float page_scale_delta
);
164 float total_page_scale_factor() const {
165 return page_scale_factor_
* page_scale_delta_
;
167 float page_scale_factor() const { return page_scale_factor_
; }
168 float min_page_scale_factor() const { return min_page_scale_factor_
; }
169 float max_page_scale_factor() const { return max_page_scale_factor_
; }
170 float page_scale_delta() const { return page_scale_delta_
; }
171 void set_sent_page_scale_delta(float delta
) {
172 sent_page_scale_delta_
= delta
;
174 float sent_page_scale_delta() const { return sent_page_scale_delta_
; }
176 // Updates draw properties and render surface layer list, as well as tile
177 // priorities. Returns false if it was unable to update.
178 bool UpdateDrawProperties();
180 void set_needs_update_draw_properties() {
181 needs_update_draw_properties_
= true;
183 bool needs_update_draw_properties() const {
184 return needs_update_draw_properties_
;
187 void set_needs_full_tree_sync(bool needs
) { needs_full_tree_sync_
= needs
; }
188 bool needs_full_tree_sync() const { return needs_full_tree_sync_
; }
190 void ForceRedrawNextActivation() { next_activation_forces_redraw_
= true; }
192 void set_has_ever_been_drawn(bool has_drawn
) {
193 has_ever_been_drawn_
= has_drawn
;
195 bool has_ever_been_drawn() const { return has_ever_been_drawn_
; }
197 void set_ui_resource_request_queue(const UIResourceRequestQueue
& queue
);
199 const LayerImplList
& RenderSurfaceLayerList() const;
201 // These return the size of the root scrollable area and the size of
202 // the user-visible scrolling viewport, in CSS layout coordinates.
203 gfx::Size
ScrollableSize() const;
204 gfx::SizeF
ScrollableViewportSize() const;
206 gfx::Rect
RootScrollLayerDeviceViewportBounds() const;
208 LayerImpl
* LayerById(int id
);
210 // These should be called by LayerImpl's ctor/dtor.
211 void RegisterLayer(LayerImpl
* layer
);
212 void UnregisterLayer(LayerImpl
* layer
);
216 AnimationRegistrar
* animationRegistrar() const;
218 void PushPersistedState(LayerTreeImpl
* pending_tree
);
220 void DidBecomeActive();
222 bool ContentsTexturesPurged() const;
223 void SetContentsTexturesPurged();
224 void ResetContentsTexturesPurged();
226 // Set on the active tree when the viewport size recently changed
227 // and the active tree's size is now out of date.
228 bool ViewportSizeInvalid() const;
229 void SetViewportSizeInvalid();
230 void ResetViewportSizeInvalid();
232 // Useful for debug assertions, probably shouldn't be used for anything else.
233 Proxy
* proxy() const;
235 void SetRootLayerScrollOffsetDelegate(
236 LayerScrollOffsetDelegate
* root_layer_scroll_offset_delegate
);
237 void OnRootLayerDelegatedScrollOffsetChanged();
238 void UpdateScrollOffsetDelegate();
239 gfx::ScrollOffset
GetDelegatedScrollOffset(LayerImpl
* layer
);
241 // Call this function when you expect there to be a swap buffer.
242 // See swap_promise.h for how to use SwapPromise.
243 void QueueSwapPromise(scoped_ptr
<SwapPromise
> swap_promise
);
245 // Take the |new_swap_promise| and append it to |swap_promise_list_|.
246 void PassSwapPromises(ScopedPtrVector
<SwapPromise
>* new_swap_promise
);
247 void FinishSwapPromises(CompositorFrameMetadata
* metadata
);
248 void BreakSwapPromises(SwapPromise::DidNotSwapReason reason
);
250 void DidModifyTilePriorities();
252 ResourceProvider::ResourceId
ResourceIdForUIResource(UIResourceId uid
) const;
253 void ProcessUIResourceRequestQueue();
255 bool IsUIResourceOpaque(UIResourceId uid
) const;
257 void AddLayerWithCopyOutputRequest(LayerImpl
* layer
);
258 void RemoveLayerWithCopyOutputRequest(LayerImpl
* layer
);
259 const std::vector
<LayerImpl
*>& LayersWithCopyOutputRequest() const;
261 int current_render_surface_list_id() const {
262 return render_surface_layer_list_id_
;
265 LayerImpl
* FindFirstScrollingLayerThatIsHitByPoint(
266 const gfx::PointF
& screen_space_point
);
268 LayerImpl
* FindLayerThatIsHitByPoint(const gfx::PointF
& screen_space_point
);
270 LayerImpl
* FindLayerThatIsHitByPointInTouchHandlerRegion(
271 const gfx::PointF
& screen_space_point
);
273 void RegisterSelection(const LayerSelectionBound
& start
,
274 const LayerSelectionBound
& end
);
276 // Compute the current selection handle location and visbility with respect to
278 void GetViewportSelection(ViewportSelectionBound
* start
,
279 ViewportSelectionBound
* end
);
281 void RegisterPictureLayerImpl(PictureLayerImpl
* layer
);
282 void UnregisterPictureLayerImpl(PictureLayerImpl
* layer
);
284 void set_top_controls_layout_height(float height
) {
285 top_controls_layout_height_
= height
;
287 void set_top_controls_content_offset(float offset
) {
288 top_controls_content_offset_
= offset
;
290 void set_top_controls_delta(float delta
) {
291 top_controls_delta_
= delta
;
293 void set_sent_top_controls_delta(float sent_delta
) {
294 sent_top_controls_delta_
= sent_delta
;
297 float top_controls_layout_height() const {
298 return top_controls_layout_height_
;
300 float top_controls_content_offset() const {
301 return top_controls_content_offset_
;
303 float top_controls_delta() const {
304 return top_controls_delta_
;
306 float sent_top_controls_delta() const {
307 return sent_top_controls_delta_
;
309 float total_top_controls_content_offset() const {
310 return top_controls_content_offset_
+ top_controls_delta_
;
313 void SetPageScaleAnimation(
314 const gfx::Vector2d
& target_offset
,
317 base::TimeDelta duration
);
318 scoped_ptr
<PageScaleAnimation
> TakePageScaleAnimation();
321 explicit LayerTreeImpl(LayerTreeHostImpl
* layer_tree_host_impl
);
322 void ReleaseResourcesRecursive(LayerImpl
* current
);
324 LayerTreeHostImpl
* layer_tree_host_impl_
;
325 int source_frame_number_
;
326 scoped_ptr
<LayerImpl
> root_layer_
;
327 HeadsUpDisplayLayerImpl
* hud_layer_
;
328 LayerImpl
* currently_scrolling_layer_
;
329 LayerScrollOffsetDelegate
* root_layer_scroll_offset_delegate_
;
330 scoped_ptr
<LayerScrollOffsetDelegateProxy
>
331 inner_viewport_scroll_delegate_proxy_
;
332 scoped_ptr
<LayerScrollOffsetDelegateProxy
>
333 outer_viewport_scroll_delegate_proxy_
;
334 SkColor background_color_
;
335 bool has_transparent_background_
;
337 LayerImpl
* page_scale_layer_
;
338 LayerImpl
* inner_viewport_scroll_layer_
;
339 LayerImpl
* outer_viewport_scroll_layer_
;
341 LayerSelectionBound selection_start_
;
342 LayerSelectionBound selection_end_
;
344 float page_scale_factor_
;
345 float page_scale_delta_
;
346 float sent_page_scale_delta_
;
347 float min_page_scale_factor_
;
348 float max_page_scale_factor_
;
350 typedef base::hash_map
<int, LayerImpl
*> LayerIdMap
;
351 LayerIdMap layer_id_map_
;
353 std::vector
<LayerImpl
*> layers_with_copy_output_request_
;
355 // Persisted state for non-impl-side-painting.
356 int scrolling_layer_id_from_previous_tree_
;
358 // List of visible layers for the most recently prepared frame.
359 LayerImplList render_surface_layer_list_
;
361 bool contents_textures_purged_
;
362 bool viewport_size_invalid_
;
363 bool needs_update_draw_properties_
;
365 // In impl-side painting mode, this is true when the tree may contain
366 // structural differences relative to the active tree.
367 bool needs_full_tree_sync_
;
369 bool next_activation_forces_redraw_
;
371 bool has_ever_been_drawn_
;
373 ScopedPtrVector
<SwapPromise
> swap_promise_list_
;
375 UIResourceRequestQueue ui_resource_request_queue_
;
377 int render_surface_layer_list_id_
;
379 // The top controls content offset at the time of the last layout (and thus,
380 // viewport resize) in Blink. i.e. How much the viewport was shrunk by the top
382 float top_controls_layout_height_
;
384 // The up-to-date content offset of the top controls, i.e. the amount that the
385 // web contents have been shifted down from the top of the device viewport.
386 float top_controls_content_offset_
;
387 float top_controls_delta_
;
388 float sent_top_controls_delta_
;
390 scoped_ptr
<PageScaleAnimation
> page_scale_animation_
;
393 DISALLOW_COPY_AND_ASSIGN(LayerTreeImpl
);
398 #endif // CC_TREES_LAYER_TREE_IMPL_H_