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/swap_promise.h"
16 #include "cc/layers/layer_impl.h"
17 #include "cc/output/renderer.h"
18 #include "cc/resources/ui_resource_client.h"
20 #if defined(COMPILER_GCC)
21 namespace BASE_HASH_NAMESPACE {
23 struct hash<cc::LayerImpl*> {
24 size_t operator()(cc::LayerImpl* ptr) const {
25 return hash<size_t>()(reinterpret_cast<size_t>(ptr));
28 } // namespace BASE_HASH_NAMESPACE
33 class ContextProvider;
34 class DebugRectHistory;
35 class FrameRateCounter;
36 class HeadsUpDisplayLayerImpl;
37 class LayerTreeDebugState;
38 class LayerTreeHostImpl;
40 class LayerTreeSettings;
43 class PaintTimeCounter;
45 class ResourceProvider;
47 class UIResourceRequest;
48 struct RendererCapabilities;
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();
60 // Methods called by the layer tree that pass-through or access LTHI.
61 // ---------------------------------------------------------------------------
62 const LayerTreeSettings& settings() const;
63 const RendererCapabilitiesImpl& GetRendererCapabilities() const;
64 ContextProvider* context_provider() const;
65 OutputSurface* output_surface() const;
66 ResourceProvider* resource_provider() const;
67 TileManager* tile_manager() const;
68 FrameRateCounter* frame_rate_counter() const;
69 PaintTimeCounter* paint_time_counter() const;
70 MemoryHistory* memory_history() const;
71 bool device_viewport_valid_for_tile_management() const;
72 bool IsActiveTree() const;
73 bool IsPendingTree() const;
74 bool IsRecycleTree() const;
75 LayerImpl* FindActiveTreeLayerById(int id);
76 LayerImpl* FindPendingTreeLayerById(int id);
77 int MaxTextureSize() const;
78 bool PinchGestureActive() const;
79 base::TimeTicks CurrentFrameTimeTicks() const;
80 base::Time CurrentFrameTime() const;
81 base::TimeTicks CurrentPhysicalTimeTicks() const;
82 void SetNeedsCommit();
83 gfx::Size DrawViewportSize() const;
84 void StartScrollbarAnimation();
85 void DidAnimateScrollOffset();
87 // Tree specific methods exposed to layer-impl tree.
88 // ---------------------------------------------------------------------------
89 void SetNeedsRedraw();
91 // TODO(nduca): These are implemented in cc files temporarily, but will become
92 // trivial accessors in a followup patch.
93 const LayerTreeDebugState& debug_state() const;
94 float device_scale_factor() const;
95 DebugRectHistory* debug_rect_history() const;
96 scoped_ptr<base::Value> AsValue() const;
98 // Other public methods
99 // ---------------------------------------------------------------------------
100 LayerImpl* root_layer() const { return root_layer_.get(); }
101 void SetRootLayer(scoped_ptr<LayerImpl>);
102 scoped_ptr<LayerImpl> DetachLayerTree();
104 void PushPropertiesTo(LayerTreeImpl* tree_impl);
106 int source_frame_number() const { return source_frame_number_; }
107 void set_source_frame_number(int frame_number) {
108 source_frame_number_ = frame_number;
111 HeadsUpDisplayLayerImpl* hud_layer() { return hud_layer_; }
112 void set_hud_layer(HeadsUpDisplayLayerImpl* layer_impl) {
113 hud_layer_ = layer_impl;
116 LayerImpl* RootScrollLayer() const;
117 LayerImpl* RootContainerLayer() const;
118 LayerImpl* CurrentlyScrollingLayer() const;
119 void SetCurrentlyScrollingLayer(LayerImpl* layer);
120 void ClearCurrentlyScrollingLayer();
122 void FindRootScrollLayer();
123 void UpdateMaxScrollOffset();
124 void SetViewportLayersFromIds(int page_scale_layer_id,
125 int inner_viewport_scroll_layer_id,
126 int outer_viewport_scroll_layer_id);
127 void ClearViewportLayers();
128 void ApplySentScrollAndScaleDeltasFromAbortedCommit();
129 void ApplyScrollDeltasSinceBeginMainFrame();
131 SkColor background_color() const { return background_color_; }
132 void set_background_color(SkColor color) { background_color_ = color; }
134 bool has_transparent_background() const {
135 return has_transparent_background_;
137 void set_has_transparent_background(bool transparent) {
138 has_transparent_background_ = transparent;
141 void SetPageScaleFactorAndLimits(float page_scale_factor,
142 float min_page_scale_factor, float max_page_scale_factor);
143 void SetPageScaleDelta(float delta);
144 float total_page_scale_factor() const {
145 return page_scale_factor_ * page_scale_delta_;
147 float page_scale_factor() const { return page_scale_factor_; }
148 float min_page_scale_factor() const { return min_page_scale_factor_; }
149 float max_page_scale_factor() const { return max_page_scale_factor_; }
150 float page_scale_delta() const { return page_scale_delta_; }
151 void set_sent_page_scale_delta(float delta) {
152 sent_page_scale_delta_ = delta;
154 float sent_page_scale_delta() const { return sent_page_scale_delta_; }
156 // Updates draw properties and render surface layer list, as well as tile
158 void UpdateDrawProperties();
160 void set_needs_update_draw_properties() {
161 needs_update_draw_properties_ = true;
163 bool needs_update_draw_properties() const {
164 return needs_update_draw_properties_;
167 void set_needs_full_tree_sync(bool needs) { needs_full_tree_sync_ = needs; }
168 bool needs_full_tree_sync() const { return needs_full_tree_sync_; }
170 void ForceRedrawNextActivation() { next_activation_forces_redraw_ = true; }
172 void set_ui_resource_request_queue(const UIResourceRequestQueue& queue);
174 const LayerImplList& RenderSurfaceLayerList() const;
176 // These return the size of the root scrollable area and the size of
177 // the user-visible scrolling viewport, in CSS layout coordinates.
178 gfx::Size ScrollableSize() const;
179 gfx::SizeF ScrollableViewportSize() const;
181 gfx::Rect RootScrollLayerDeviceViewportBounds() const;
183 LayerImpl* LayerById(int id);
185 // These should be called by LayerImpl's ctor/dtor.
186 void RegisterLayer(LayerImpl* layer);
187 void UnregisterLayer(LayerImpl* layer);
189 AnimationRegistrar* animationRegistrar() const;
191 void PushPersistedState(LayerTreeImpl* pending_tree);
193 void DidBecomeActive();
195 bool ContentsTexturesPurged() const;
196 void SetContentsTexturesPurged();
197 void ResetContentsTexturesPurged();
199 // Set on the active tree when the viewport size recently changed
200 // and the active tree's size is now out of date.
201 bool ViewportSizeInvalid() const;
202 void SetViewportSizeInvalid();
203 void ResetViewportSizeInvalid();
205 // Useful for debug assertions, probably shouldn't be used for anything else.
206 Proxy* proxy() const;
208 void SetRootLayerScrollOffsetDelegate(
209 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate);
211 // Call this function when you expect there to be a swap buffer.
212 // See swap_promise.h for how to use SwapPromise.
213 void QueueSwapPromise(scoped_ptr<SwapPromise> swap_promise);
215 // Take the |new_swap_promise| and append it to |swap_promise_list_|.
216 void PassSwapPromises(ScopedPtrVector<SwapPromise>* new_swap_promise);
217 void FinishSwapPromises(CompositorFrameMetadata* metadata);
218 void BreakSwapPromises(SwapPromise::DidNotSwapReason reason);
220 void DidModifyTilePriorities();
222 ResourceProvider::ResourceId ResourceIdForUIResource(UIResourceId uid) const;
223 void ProcessUIResourceRequestQueue();
225 bool IsUIResourceOpaque(UIResourceId uid) const;
227 void AddLayerWithCopyOutputRequest(LayerImpl* layer);
228 void RemoveLayerWithCopyOutputRequest(LayerImpl* layer);
229 const std::vector<LayerImpl*>& LayersWithCopyOutputRequest() const;
232 explicit LayerTreeImpl(LayerTreeHostImpl* layer_tree_host_impl);
234 void UpdateSolidColorScrollbars();
236 void UpdateRootScrollLayerSizeDelta();
238 LayerTreeHostImpl* layer_tree_host_impl_;
239 int source_frame_number_;
240 scoped_ptr<LayerImpl> root_layer_;
241 HeadsUpDisplayLayerImpl* hud_layer_;
242 LayerImpl* root_scroll_layer_;
243 LayerImpl* currently_scrolling_layer_;
244 LayerScrollOffsetDelegate* root_layer_scroll_offset_delegate_;
245 SkColor background_color_;
246 bool has_transparent_background_;
248 LayerImpl* page_scale_layer_;
249 LayerImpl* inner_viewport_scroll_layer_;
250 LayerImpl* outer_viewport_scroll_layer_;
252 float page_scale_factor_;
253 float page_scale_delta_;
254 float sent_page_scale_delta_;
255 float min_page_scale_factor_;
256 float max_page_scale_factor_;
258 typedef base::hash_map<int, LayerImpl*> LayerIdMap;
259 LayerIdMap layer_id_map_;
261 std::vector<LayerImpl*> layers_with_copy_output_request_;
263 // Persisted state for non-impl-side-painting.
264 int scrolling_layer_id_from_previous_tree_;
266 // List of visible or hit-testable layers for the most recently prepared
267 // frame. Used for rendering and input event hit testing.
268 LayerImplList render_surface_layer_list_;
270 bool contents_textures_purged_;
271 bool viewport_size_invalid_;
272 bool needs_update_draw_properties_;
274 // In impl-side painting mode, this is true when the tree may contain
275 // structural differences relative to the active tree.
276 bool needs_full_tree_sync_;
278 bool next_activation_forces_redraw_;
280 ScopedPtrVector<SwapPromise> swap_promise_list_;
282 UIResourceRequestQueue ui_resource_request_queue_;
285 DISALLOW_COPY_AND_ASSIGN(LayerTreeImpl);
290 #endif // CC_TREES_LAYER_TREE_IMPL_H_