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"
21 #if defined(COMPILER_GCC)
22 namespace BASE_HASH_NAMESPACE {
24 struct hash<cc::LayerImpl*> {
25 size_t operator()(cc::LayerImpl* ptr) const {
26 return hash<size_t>()(reinterpret_cast<size_t>(ptr));
29 } // namespace BASE_HASH_NAMESPACE
40 class ContextProvider;
41 class DebugRectHistory;
42 class FrameRateCounter;
43 class HeadsUpDisplayLayerImpl;
44 class LayerScrollOffsetDelegateProxy;
45 class LayerTreeDebugState;
46 class LayerTreeHostImpl;
48 class LayerTreeSettings;
51 class PaintTimeCounter;
52 class PictureLayerImpl;
54 class ResourceProvider;
56 class UIResourceRequest;
57 struct RendererCapabilities;
58 struct SelectionHandle;
60 typedef std::list<UIResourceRequest> UIResourceRequestQueue;
62 class CC_EXPORT LayerTreeImpl {
64 static scoped_ptr<LayerTreeImpl> create(
65 LayerTreeHostImpl* layer_tree_host_impl) {
66 return make_scoped_ptr(new LayerTreeImpl(layer_tree_host_impl));
68 virtual ~LayerTreeImpl();
71 void ReleaseResources();
73 // Methods called by the layer tree that pass-through or access LTHI.
74 // ---------------------------------------------------------------------------
75 const LayerTreeSettings& settings() const;
76 const RendererCapabilitiesImpl& GetRendererCapabilities() const;
77 ContextProvider* context_provider() const;
78 OutputSurface* output_surface() const;
79 ResourceProvider* resource_provider() const;
80 TileManager* tile_manager() const;
81 FrameRateCounter* frame_rate_counter() const;
82 PaintTimeCounter* paint_time_counter() const;
83 MemoryHistory* memory_history() const;
84 gfx::Size device_viewport_size() const;
85 bool IsActiveTree() const;
86 bool IsPendingTree() const;
87 bool IsRecycleTree() const;
88 LayerImpl* FindActiveTreeLayerById(int id);
89 LayerImpl* FindPendingTreeLayerById(int id);
90 LayerImpl* FindRecycleTreeLayerById(int id);
91 bool PinchGestureActive() const;
92 BeginFrameArgs CurrentBeginFrameArgs() const;
93 base::TimeDelta begin_impl_frame_interval() const;
94 void SetNeedsCommit();
95 gfx::Rect DeviceViewport() const;
96 gfx::Size DrawViewportSize() const;
97 const gfx::Rect ViewportRectForTilePriority() const;
98 scoped_ptr<ScrollbarAnimationController> CreateScrollbarAnimationController(
99 LayerImpl* scrolling_layer);
100 void DidAnimateScrollOffset();
101 void InputScrollAnimationFinished();
102 bool use_gpu_rasterization() const;
103 bool create_low_res_tiling() const;
104 BlockingTaskRunner* BlockingMainThreadTaskRunner() const;
106 // Tree specific methods exposed to layer-impl tree.
107 // ---------------------------------------------------------------------------
108 void SetNeedsRedraw();
110 // TODO(nduca): These are implemented in cc files temporarily, but will become
111 // trivial accessors in a followup patch.
112 const LayerTreeDebugState& debug_state() const;
113 float device_scale_factor() const;
114 DebugRectHistory* debug_rect_history() const;
115 void GetAllTilesForTracing(std::set<const Tile*>* tiles) const;
116 void AsValueInto(base::debug::TracedValue* dict) const;
118 // Other public methods
119 // ---------------------------------------------------------------------------
120 LayerImpl* root_layer() const { return root_layer_.get(); }
121 void SetRootLayer(scoped_ptr<LayerImpl>);
122 scoped_ptr<LayerImpl> DetachLayerTree();
124 void PushPropertiesTo(LayerTreeImpl* tree_impl);
126 int source_frame_number() const { return source_frame_number_; }
127 void set_source_frame_number(int frame_number) {
128 source_frame_number_ = frame_number;
131 HeadsUpDisplayLayerImpl* hud_layer() { return hud_layer_; }
132 void set_hud_layer(HeadsUpDisplayLayerImpl* layer_impl) {
133 hud_layer_ = layer_impl;
136 LayerImpl* InnerViewportScrollLayer() const;
137 // This function may return NULL, it is the caller's responsibility to check.
138 LayerImpl* OuterViewportScrollLayer() const;
139 gfx::Vector2dF TotalScrollOffset() const;
140 gfx::Vector2dF TotalMaxScrollOffset() const;
141 gfx::Vector2dF TotalScrollDelta() const;
143 LayerImpl* InnerViewportContainerLayer() const;
144 LayerImpl* CurrentlyScrollingLayer() const;
145 void SetCurrentlyScrollingLayer(LayerImpl* layer);
146 void ClearCurrentlyScrollingLayer();
148 void SetViewportLayersFromIds(int page_scale_layer_id,
149 int inner_viewport_scroll_layer_id,
150 int outer_viewport_scroll_layer_id);
151 void ClearViewportLayers();
152 LayerImpl* page_scale_layer() { return page_scale_layer_; }
153 void ApplySentScrollAndScaleDeltasFromAbortedCommit();
154 void ApplyScrollDeltasSinceBeginMainFrame();
156 SkColor background_color() const { return background_color_; }
157 void set_background_color(SkColor color) { background_color_ = color; }
159 bool has_transparent_background() const {
160 return has_transparent_background_;
162 void set_has_transparent_background(bool transparent) {
163 has_transparent_background_ = transparent;
166 void SetPageScaleFactorAndLimits(float page_scale_factor,
167 float min_page_scale_factor, float max_page_scale_factor);
168 void SetPageScaleDelta(float delta);
169 void SetPageScaleValues(float page_scale_factor,
170 float min_page_scale_factor, float max_page_scale_factor,
171 float page_scale_delta);
172 float total_page_scale_factor() const {
173 return page_scale_factor_ * page_scale_delta_;
175 float page_scale_factor() const { return page_scale_factor_; }
176 float min_page_scale_factor() const { return min_page_scale_factor_; }
177 float max_page_scale_factor() const { return max_page_scale_factor_; }
178 float page_scale_delta() const { return page_scale_delta_; }
179 void set_sent_page_scale_delta(float delta) {
180 sent_page_scale_delta_ = delta;
182 float sent_page_scale_delta() const { return sent_page_scale_delta_; }
184 // Updates draw properties and render surface layer list, as well as tile
185 // priorities. Returns false if it was unable to update.
186 bool UpdateDrawProperties();
188 void set_needs_update_draw_properties() {
189 needs_update_draw_properties_ = true;
191 bool needs_update_draw_properties() const {
192 return needs_update_draw_properties_;
195 void set_needs_full_tree_sync(bool needs) { needs_full_tree_sync_ = needs; }
196 bool needs_full_tree_sync() const { return needs_full_tree_sync_; }
198 void ForceRedrawNextActivation() { next_activation_forces_redraw_ = true; }
200 void set_has_ever_been_drawn(bool has_drawn) {
201 has_ever_been_drawn_ = has_drawn;
203 bool has_ever_been_drawn() const { return has_ever_been_drawn_; }
205 void set_ui_resource_request_queue(const UIResourceRequestQueue& queue);
207 const LayerImplList& RenderSurfaceLayerList() const;
209 // These return the size of the root scrollable area and the size of
210 // the user-visible scrolling viewport, in CSS layout coordinates.
211 gfx::Size ScrollableSize() const;
212 gfx::SizeF ScrollableViewportSize() const;
214 gfx::Rect RootScrollLayerDeviceViewportBounds() const;
216 LayerImpl* LayerById(int id);
218 // These should be called by LayerImpl's ctor/dtor.
219 void RegisterLayer(LayerImpl* layer);
220 void UnregisterLayer(LayerImpl* layer);
224 AnimationRegistrar* animationRegistrar() const;
226 void PushPersistedState(LayerTreeImpl* pending_tree);
228 void DidBecomeActive();
230 bool ContentsTexturesPurged() const;
231 void SetContentsTexturesPurged();
232 void ResetContentsTexturesPurged();
234 void SetRequiresHighResToDraw();
235 void ResetRequiresHighResToDraw();
236 bool RequiresHighResToDraw() const;
238 // Set on the active tree when the viewport size recently changed
239 // and the active tree's size is now out of date.
240 bool ViewportSizeInvalid() const;
241 void SetViewportSizeInvalid();
242 void ResetViewportSizeInvalid();
244 // Useful for debug assertions, probably shouldn't be used for anything else.
245 Proxy* proxy() const;
247 void SetRootLayerScrollOffsetDelegate(
248 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate);
249 void UpdateScrollOffsetDelegate();
250 gfx::Vector2dF GetDelegatedScrollOffset(LayerImpl* layer);
252 // Call this function when you expect there to be a swap buffer.
253 // See swap_promise.h for how to use SwapPromise.
254 void QueueSwapPromise(scoped_ptr<SwapPromise> swap_promise);
256 // Take the |new_swap_promise| and append it to |swap_promise_list_|.
257 void PassSwapPromises(ScopedPtrVector<SwapPromise>* new_swap_promise);
258 void FinishSwapPromises(CompositorFrameMetadata* metadata);
259 void BreakSwapPromises(SwapPromise::DidNotSwapReason reason);
261 void DidModifyTilePriorities();
263 ResourceProvider::ResourceId ResourceIdForUIResource(UIResourceId uid) const;
264 void ProcessUIResourceRequestQueue();
266 bool IsUIResourceOpaque(UIResourceId uid) const;
268 void AddLayerWithCopyOutputRequest(LayerImpl* layer);
269 void RemoveLayerWithCopyOutputRequest(LayerImpl* layer);
270 const std::vector<LayerImpl*>& LayersWithCopyOutputRequest() const;
272 int current_render_surface_list_id() const {
273 return render_surface_layer_list_id_;
276 LayerImpl* FindFirstScrollingLayerThatIsHitByPoint(
277 const gfx::PointF& screen_space_point);
279 LayerImpl* FindLayerThatIsHitByPoint(const gfx::PointF& screen_space_point);
281 LayerImpl* FindLayerThatIsHitByPointInTouchHandlerRegion(
282 const gfx::PointF& screen_space_point);
284 void RegisterSelection(const LayerSelectionBound& start,
285 const LayerSelectionBound& end);
287 // Compute the current selection handle location and visbility with respect to
289 void GetViewportSelection(ViewportSelectionBound* start,
290 ViewportSelectionBound* end);
292 void RegisterPictureLayerImpl(PictureLayerImpl* layer);
293 void UnregisterPictureLayerImpl(PictureLayerImpl* layer);
295 void set_top_controls_layout_height(float height) {
296 top_controls_layout_height_ = height;
298 void set_top_controls_content_offset(float offset) {
299 top_controls_content_offset_ = offset;
301 void set_top_controls_delta(float delta) {
302 top_controls_delta_ = delta;
304 void set_sent_top_controls_delta(float sent_delta) {
305 sent_top_controls_delta_ = sent_delta;
308 float top_controls_layout_height() const {
309 return top_controls_layout_height_;
311 float top_controls_content_offset() const {
312 return top_controls_content_offset_;
314 float top_controls_delta() const {
315 return top_controls_delta_;
317 float sent_top_controls_delta() const {
318 return sent_top_controls_delta_;
320 float total_top_controls_content_offset() const {
321 return top_controls_content_offset_ + top_controls_delta_;
325 explicit LayerTreeImpl(LayerTreeHostImpl* layer_tree_host_impl);
326 void ReleaseResourcesRecursive(LayerImpl* current);
328 LayerTreeHostImpl* layer_tree_host_impl_;
329 int source_frame_number_;
330 scoped_ptr<LayerImpl> root_layer_;
331 HeadsUpDisplayLayerImpl* hud_layer_;
332 LayerImpl* currently_scrolling_layer_;
333 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate_;
334 scoped_ptr<LayerScrollOffsetDelegateProxy>
335 inner_viewport_scroll_delegate_proxy_;
336 scoped_ptr<LayerScrollOffsetDelegateProxy>
337 outer_viewport_scroll_delegate_proxy_;
338 SkColor background_color_;
339 bool has_transparent_background_;
341 LayerImpl* page_scale_layer_;
342 LayerImpl* inner_viewport_scroll_layer_;
343 LayerImpl* outer_viewport_scroll_layer_;
345 LayerSelectionBound selection_start_;
346 LayerSelectionBound selection_end_;
348 float page_scale_factor_;
349 float page_scale_delta_;
350 float sent_page_scale_delta_;
351 float min_page_scale_factor_;
352 float max_page_scale_factor_;
354 typedef base::hash_map<int, LayerImpl*> LayerIdMap;
355 LayerIdMap layer_id_map_;
357 std::vector<LayerImpl*> layers_with_copy_output_request_;
359 // Persisted state for non-impl-side-painting.
360 int scrolling_layer_id_from_previous_tree_;
362 // List of visible layers for the most recently prepared frame.
363 LayerImplList render_surface_layer_list_;
365 bool contents_textures_purged_;
366 bool requires_high_res_to_draw_;
367 bool viewport_size_invalid_;
368 bool needs_update_draw_properties_;
370 // In impl-side painting mode, this is true when the tree may contain
371 // structural differences relative to the active tree.
372 bool needs_full_tree_sync_;
374 bool next_activation_forces_redraw_;
376 bool has_ever_been_drawn_;
378 ScopedPtrVector<SwapPromise> swap_promise_list_;
380 UIResourceRequestQueue ui_resource_request_queue_;
382 int render_surface_layer_list_id_;
384 // The top controls content offset at the time of the last layout (and thus,
385 // viewport resize) in Blink. i.e. How much the viewport was shrunk by the top
387 float top_controls_layout_height_;
389 // The up-to-date content offset of the top controls, i.e. the amount that the
390 // web contents have been shifted down from the top of the device viewport.
391 float top_controls_content_offset_;
392 float top_controls_delta_;
393 float sent_top_controls_delta_;
396 DISALLOW_COPY_AND_ASSIGN(LayerTreeImpl);
401 #endif // CC_TREES_LAYER_TREE_IMPL_H_