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 PageScaleAnimation;
52 class PaintTimeCounter;
53 class PictureLayerImpl;
55 class ResourceProvider;
57 class UIResourceRequest;
58 struct RendererCapabilities;
59 struct SelectionHandle;
61 typedef std::list<UIResourceRequest> UIResourceRequestQueue;
63 class CC_EXPORT LayerTreeImpl {
65 static scoped_ptr<LayerTreeImpl> create(
66 LayerTreeHostImpl* layer_tree_host_impl) {
67 return make_scoped_ptr(new LayerTreeImpl(layer_tree_host_impl));
69 virtual ~LayerTreeImpl();
72 void ReleaseResources();
74 // Methods called by the layer tree that pass-through or access LTHI.
75 // ---------------------------------------------------------------------------
76 const LayerTreeSettings& settings() const;
77 const RendererCapabilitiesImpl& GetRendererCapabilities() const;
78 ContextProvider* context_provider() const;
79 OutputSurface* output_surface() const;
80 ResourceProvider* resource_provider() const;
81 TileManager* tile_manager() const;
82 FrameRateCounter* frame_rate_counter() const;
83 PaintTimeCounter* paint_time_counter() const;
84 MemoryHistory* memory_history() const;
85 gfx::Size device_viewport_size() const;
86 bool IsActiveTree() const;
87 bool IsPendingTree() const;
88 bool IsRecycleTree() const;
89 LayerImpl* FindActiveTreeLayerById(int id);
90 LayerImpl* FindPendingTreeLayerById(int id);
91 LayerImpl* FindRecycleTreeLayerById(int id);
92 bool PinchGestureActive() const;
93 BeginFrameArgs CurrentBeginFrameArgs() const;
94 base::TimeDelta begin_impl_frame_interval() const;
95 void SetNeedsCommit();
96 gfx::Rect DeviceViewport() const;
97 gfx::Size DrawViewportSize() const;
98 const gfx::Rect ViewportRectForTilePriority() const;
99 scoped_ptr<ScrollbarAnimationController> CreateScrollbarAnimationController(
100 LayerImpl* scrolling_layer);
101 void DidAnimateScrollOffset();
102 void InputScrollAnimationFinished();
103 bool use_gpu_rasterization() const;
104 bool create_low_res_tiling() const;
105 BlockingTaskRunner* BlockingMainThreadTaskRunner() const;
107 // Tree specific methods exposed to layer-impl tree.
108 // ---------------------------------------------------------------------------
109 void SetNeedsRedraw();
111 // TODO(nduca): These are implemented in cc files temporarily, but will become
112 // trivial accessors in a followup patch.
113 const LayerTreeDebugState& debug_state() const;
114 float device_scale_factor() const;
115 DebugRectHistory* debug_rect_history() const;
116 void GetAllTilesForTracing(std::set<const Tile*>* tiles) const;
117 void AsValueInto(base::debug::TracedValue* dict) const;
119 // Other public methods
120 // ---------------------------------------------------------------------------
121 LayerImpl* root_layer() const { return root_layer_.get(); }
122 void SetRootLayer(scoped_ptr<LayerImpl>);
123 scoped_ptr<LayerImpl> DetachLayerTree();
125 void PushPropertiesTo(LayerTreeImpl* tree_impl);
127 int source_frame_number() const { return source_frame_number_; }
128 void set_source_frame_number(int frame_number) {
129 source_frame_number_ = frame_number;
132 HeadsUpDisplayLayerImpl* hud_layer() { return hud_layer_; }
133 void set_hud_layer(HeadsUpDisplayLayerImpl* layer_impl) {
134 hud_layer_ = layer_impl;
137 LayerImpl* InnerViewportScrollLayer() const;
138 // This function may return NULL, it is the caller's responsibility to check.
139 LayerImpl* OuterViewportScrollLayer() const;
140 gfx::Vector2dF TotalScrollOffset() const;
141 gfx::Vector2dF TotalMaxScrollOffset() const;
142 gfx::Vector2dF TotalScrollDelta() const;
144 LayerImpl* InnerViewportContainerLayer() const;
145 LayerImpl* CurrentlyScrollingLayer() const;
146 void SetCurrentlyScrollingLayer(LayerImpl* layer);
147 void ClearCurrentlyScrollingLayer();
149 void SetViewportLayersFromIds(int page_scale_layer_id,
150 int inner_viewport_scroll_layer_id,
151 int outer_viewport_scroll_layer_id);
152 void ClearViewportLayers();
153 LayerImpl* page_scale_layer() { return page_scale_layer_; }
154 void ApplySentScrollAndScaleDeltasFromAbortedCommit();
155 void ApplyScrollDeltasSinceBeginMainFrame();
157 SkColor background_color() const { return background_color_; }
158 void set_background_color(SkColor color) { background_color_ = color; }
160 bool has_transparent_background() const {
161 return has_transparent_background_;
163 void set_has_transparent_background(bool transparent) {
164 has_transparent_background_ = transparent;
167 void SetPageScaleFactorAndLimits(float page_scale_factor,
168 float min_page_scale_factor, float max_page_scale_factor);
169 void SetPageScaleDelta(float delta);
170 void SetPageScaleValues(float page_scale_factor,
171 float min_page_scale_factor, float max_page_scale_factor,
172 float page_scale_delta);
173 float total_page_scale_factor() const {
174 return page_scale_factor_ * page_scale_delta_;
176 float page_scale_factor() const { return page_scale_factor_; }
177 float min_page_scale_factor() const { return min_page_scale_factor_; }
178 float max_page_scale_factor() const { return max_page_scale_factor_; }
179 float page_scale_delta() const { return page_scale_delta_; }
180 void set_sent_page_scale_delta(float delta) {
181 sent_page_scale_delta_ = delta;
183 float sent_page_scale_delta() const { return sent_page_scale_delta_; }
185 // Updates draw properties and render surface layer list, as well as tile
186 // priorities. Returns false if it was unable to update.
187 bool UpdateDrawProperties();
189 void set_needs_update_draw_properties() {
190 needs_update_draw_properties_ = true;
192 bool needs_update_draw_properties() const {
193 return needs_update_draw_properties_;
196 void set_needs_full_tree_sync(bool needs) { needs_full_tree_sync_ = needs; }
197 bool needs_full_tree_sync() const { return needs_full_tree_sync_; }
199 void ForceRedrawNextActivation() { next_activation_forces_redraw_ = true; }
201 void set_has_ever_been_drawn(bool has_drawn) {
202 has_ever_been_drawn_ = has_drawn;
204 bool has_ever_been_drawn() const { return has_ever_been_drawn_; }
206 void set_ui_resource_request_queue(const UIResourceRequestQueue& queue);
208 const LayerImplList& RenderSurfaceLayerList() const;
210 // These return the size of the root scrollable area and the size of
211 // the user-visible scrolling viewport, in CSS layout coordinates.
212 gfx::Size ScrollableSize() const;
213 gfx::SizeF ScrollableViewportSize() const;
215 gfx::Rect RootScrollLayerDeviceViewportBounds() const;
217 LayerImpl* LayerById(int id);
219 // These should be called by LayerImpl's ctor/dtor.
220 void RegisterLayer(LayerImpl* layer);
221 void UnregisterLayer(LayerImpl* layer);
225 AnimationRegistrar* animationRegistrar() const;
227 void PushPersistedState(LayerTreeImpl* pending_tree);
229 void DidBecomeActive();
231 bool ContentsTexturesPurged() const;
232 void SetContentsTexturesPurged();
233 void ResetContentsTexturesPurged();
235 void SetRequiresHighResToDraw();
236 void ResetRequiresHighResToDraw();
237 bool RequiresHighResToDraw() const;
239 // Set on the active tree when the viewport size recently changed
240 // and the active tree's size is now out of date.
241 bool ViewportSizeInvalid() const;
242 void SetViewportSizeInvalid();
243 void ResetViewportSizeInvalid();
245 // Useful for debug assertions, probably shouldn't be used for anything else.
246 Proxy* proxy() const;
248 void SetRootLayerScrollOffsetDelegate(
249 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate);
250 void UpdateScrollOffsetDelegate();
251 gfx::Vector2dF GetDelegatedScrollOffset(LayerImpl* layer);
253 // Call this function when you expect there to be a swap buffer.
254 // See swap_promise.h for how to use SwapPromise.
255 void QueueSwapPromise(scoped_ptr<SwapPromise> swap_promise);
257 // Take the |new_swap_promise| and append it to |swap_promise_list_|.
258 void PassSwapPromises(ScopedPtrVector<SwapPromise>* new_swap_promise);
259 void FinishSwapPromises(CompositorFrameMetadata* metadata);
260 void BreakSwapPromises(SwapPromise::DidNotSwapReason reason);
262 void DidModifyTilePriorities();
264 ResourceProvider::ResourceId ResourceIdForUIResource(UIResourceId uid) const;
265 void ProcessUIResourceRequestQueue();
267 bool IsUIResourceOpaque(UIResourceId uid) const;
269 void AddLayerWithCopyOutputRequest(LayerImpl* layer);
270 void RemoveLayerWithCopyOutputRequest(LayerImpl* layer);
271 const std::vector<LayerImpl*>& LayersWithCopyOutputRequest() const;
273 int current_render_surface_list_id() const {
274 return render_surface_layer_list_id_;
277 LayerImpl* FindFirstScrollingLayerThatIsHitByPoint(
278 const gfx::PointF& screen_space_point);
280 LayerImpl* FindLayerThatIsHitByPoint(const gfx::PointF& screen_space_point);
282 LayerImpl* FindLayerThatIsHitByPointInTouchHandlerRegion(
283 const gfx::PointF& screen_space_point);
285 void RegisterSelection(const LayerSelectionBound& start,
286 const LayerSelectionBound& end);
288 // Compute the current selection handle location and visbility with respect to
290 void GetViewportSelection(ViewportSelectionBound* start,
291 ViewportSelectionBound* end);
293 void RegisterPictureLayerImpl(PictureLayerImpl* layer);
294 void UnregisterPictureLayerImpl(PictureLayerImpl* layer);
296 void set_top_controls_layout_height(float height) {
297 top_controls_layout_height_ = height;
299 void set_top_controls_content_offset(float offset) {
300 top_controls_content_offset_ = offset;
302 void set_top_controls_delta(float delta) {
303 top_controls_delta_ = delta;
305 void set_sent_top_controls_delta(float sent_delta) {
306 sent_top_controls_delta_ = sent_delta;
309 float top_controls_layout_height() const {
310 return top_controls_layout_height_;
312 float top_controls_content_offset() const {
313 return top_controls_content_offset_;
315 float top_controls_delta() const {
316 return top_controls_delta_;
318 float sent_top_controls_delta() const {
319 return sent_top_controls_delta_;
321 float total_top_controls_content_offset() const {
322 return top_controls_content_offset_ + top_controls_delta_;
325 void SetPageScaleAnimation(const gfx::Vector2d& target_offset,
328 base::TimeDelta duration);
329 scoped_ptr<PageScaleAnimation> TakePageScaleAnimation();
332 explicit LayerTreeImpl(LayerTreeHostImpl* layer_tree_host_impl);
333 void ReleaseResourcesRecursive(LayerImpl* current);
335 LayerTreeHostImpl* layer_tree_host_impl_;
336 int source_frame_number_;
337 scoped_ptr<LayerImpl> root_layer_;
338 HeadsUpDisplayLayerImpl* hud_layer_;
339 LayerImpl* currently_scrolling_layer_;
340 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate_;
341 scoped_ptr<LayerScrollOffsetDelegateProxy>
342 inner_viewport_scroll_delegate_proxy_;
343 scoped_ptr<LayerScrollOffsetDelegateProxy>
344 outer_viewport_scroll_delegate_proxy_;
345 SkColor background_color_;
346 bool has_transparent_background_;
348 LayerImpl* page_scale_layer_;
349 LayerImpl* inner_viewport_scroll_layer_;
350 LayerImpl* outer_viewport_scroll_layer_;
352 LayerSelectionBound selection_start_;
353 LayerSelectionBound selection_end_;
355 float page_scale_factor_;
356 float page_scale_delta_;
357 float sent_page_scale_delta_;
358 float min_page_scale_factor_;
359 float max_page_scale_factor_;
361 typedef base::hash_map<int, LayerImpl*> LayerIdMap;
362 LayerIdMap layer_id_map_;
364 std::vector<LayerImpl*> layers_with_copy_output_request_;
366 // Persisted state for non-impl-side-painting.
367 int scrolling_layer_id_from_previous_tree_;
369 // List of visible layers for the most recently prepared frame.
370 LayerImplList render_surface_layer_list_;
372 bool contents_textures_purged_;
373 bool requires_high_res_to_draw_;
374 bool viewport_size_invalid_;
375 bool needs_update_draw_properties_;
377 // In impl-side painting mode, this is true when the tree may contain
378 // structural differences relative to the active tree.
379 bool needs_full_tree_sync_;
381 bool next_activation_forces_redraw_;
383 bool has_ever_been_drawn_;
385 ScopedPtrVector<SwapPromise> swap_promise_list_;
387 UIResourceRequestQueue ui_resource_request_queue_;
389 int render_surface_layer_list_id_;
391 // The top controls content offset at the time of the last layout (and thus,
392 // viewport resize) in Blink. i.e. How much the viewport was shrunk by the top
394 float top_controls_layout_height_;
396 // The up-to-date content offset of the top controls, i.e. the amount that the
397 // web contents have been shifted down from the top of the device viewport.
398 float top_controls_content_offset_;
399 float top_controls_delta_;
400 float sent_top_controls_delta_;
402 scoped_ptr<PageScaleAnimation> page_scale_animation_;
405 DISALLOW_COPY_AND_ASSIGN(LayerTreeImpl);
410 #endif // CC_TREES_LAYER_TREE_IMPL_H_