Roll src/third_party/skia d32087a:1052f51
[chromium-blink-merge.git] / cc / trees / layer_tree_impl.h
blobc9bd64b0573215cc7900e53777d83b9ad6a2be94
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_
8 #include <set>
9 #include <string>
10 #include <vector>
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"
23 #include "cc/trees/property_tree.h"
25 namespace base {
26 namespace trace_event {
27 class TracedValue;
31 namespace cc {
33 class ContextProvider;
34 class DebugRectHistory;
35 class FrameRateCounter;
36 class HeadsUpDisplayLayerImpl;
37 class LayerExternalScrollOffsetListener;
38 class LayerScrollOffsetDelegate;
39 class LayerTreeDebugState;
40 class LayerTreeImpl;
41 class LayerTreeSettings;
42 class MemoryHistory;
43 class OutputSurface;
44 class PageScaleAnimation;
45 class PictureLayerImpl;
46 class Proxy;
47 class ResourceProvider;
48 class TileManager;
49 class UIResourceRequest;
50 class VideoFrameControllerClient;
51 struct PendingPageScaleAnimation;
52 struct RendererCapabilities;
54 typedef std::vector<UIResourceRequest> UIResourceRequestQueue;
55 typedef SyncedProperty<AdditionGroup<float>> SyncedTopControls;
56 typedef SyncedProperty<AdditionGroup<gfx::Vector2dF>> SyncedElasticOverscroll;
58 class CC_EXPORT LayerTreeImpl {
59 public:
60 static scoped_ptr<LayerTreeImpl> create(
61 LayerTreeHostImpl* layer_tree_host_impl,
62 scoped_refptr<SyncedProperty<ScaleGroup>> page_scale_factor,
63 scoped_refptr<SyncedTopControls> top_controls_shown_ratio,
64 scoped_refptr<SyncedElasticOverscroll> elastic_overscroll) {
65 return make_scoped_ptr(
66 new LayerTreeImpl(layer_tree_host_impl, page_scale_factor,
67 top_controls_shown_ratio, elastic_overscroll));
69 virtual ~LayerTreeImpl();
71 void Shutdown();
72 void ReleaseResources();
73 void RecreateResources();
75 // Methods called by the layer tree that pass-through or access LTHI.
76 // ---------------------------------------------------------------------------
77 const LayerTreeSettings& settings() const;
78 const LayerTreeDebugState& debug_state() const;
79 const RendererCapabilitiesImpl& GetRendererCapabilities() const;
80 ContextProvider* context_provider() const;
81 OutputSurface* output_surface() const;
82 ResourceProvider* resource_provider() const;
83 TileManager* tile_manager() const;
84 FrameRateCounter* frame_rate_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 CurrentBeginFrameInterval() 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;
112 VideoFrameControllerClient* GetVideoFrameControllerClient() const;
114 // Tree specific methods exposed to layer-impl tree.
115 // ---------------------------------------------------------------------------
116 void SetNeedsRedraw();
118 // Tracing methods.
119 // ---------------------------------------------------------------------------
120 void GetAllPrioritizedTilesForTracing(
121 std::vector<PrioritizedTile>* prioritized_tiles) const;
122 void AsValueInto(base::trace_event::TracedValue* dict) const;
124 // Other public methods
125 // ---------------------------------------------------------------------------
126 LayerImpl* root_layer() const { return root_layer_.get(); }
127 void SetRootLayer(scoped_ptr<LayerImpl>);
128 scoped_ptr<LayerImpl> DetachLayerTree();
130 void SetPropertyTrees(const PropertyTrees& property_trees) {
131 property_trees_ = property_trees;
132 property_trees_.transform_tree.set_source_to_parent_updates_allowed(false);
134 PropertyTrees* property_trees() { return &property_trees_; }
136 void UpdatePropertyTreesForBoundsDelta();
138 void PushPropertiesTo(LayerTreeImpl* tree_impl);
140 int source_frame_number() const { return source_frame_number_; }
141 void set_source_frame_number(int frame_number) {
142 source_frame_number_ = frame_number;
145 HeadsUpDisplayLayerImpl* hud_layer() { return hud_layer_; }
146 void set_hud_layer(HeadsUpDisplayLayerImpl* layer_impl) {
147 hud_layer_ = layer_impl;
150 LayerImpl* InnerViewportScrollLayer() const;
151 // This function may return NULL, it is the caller's responsibility to check.
152 LayerImpl* OuterViewportScrollLayer() const;
153 gfx::ScrollOffset TotalScrollOffset() const;
154 gfx::ScrollOffset TotalMaxScrollOffset() const;
156 LayerImpl* InnerViewportContainerLayer() const;
157 LayerImpl* OuterViewportContainerLayer() const;
158 LayerImpl* CurrentlyScrollingLayer() const;
159 void SetCurrentlyScrollingLayer(LayerImpl* layer);
160 void ClearCurrentlyScrollingLayer();
162 void SetViewportLayersFromIds(int overscroll_elasticity_layer,
163 int page_scale_layer_id,
164 int inner_viewport_scroll_layer_id,
165 int outer_viewport_scroll_layer_id);
166 void ClearViewportLayers();
167 LayerImpl* OverscrollElasticityLayer() {
168 return LayerById(overscroll_elasticity_layer_id_);
170 LayerImpl* PageScaleLayer() { return LayerById(page_scale_layer_id_); }
171 void ApplySentScrollAndScaleDeltasFromAbortedCommit();
173 SkColor background_color() const { return background_color_; }
174 void set_background_color(SkColor color) { background_color_ = color; }
176 bool has_transparent_background() const {
177 return has_transparent_background_;
179 void set_has_transparent_background(bool transparent) {
180 has_transparent_background_ = transparent;
183 void UpdatePropertyTreeScrollingAndAnimationFromMainThread();
184 void SetPageScaleOnActiveTree(float active_page_scale);
185 void PushPageScaleFromMainThread(float page_scale_factor,
186 float min_page_scale_factor,
187 float max_page_scale_factor);
188 float current_page_scale_factor() const {
189 return page_scale_factor()->Current(IsActiveTree());
191 float min_page_scale_factor() const { return min_page_scale_factor_; }
192 float max_page_scale_factor() const { return max_page_scale_factor_; }
194 float page_scale_delta() const { return page_scale_factor()->Delta(); }
196 SyncedProperty<ScaleGroup>* page_scale_factor();
197 const SyncedProperty<ScaleGroup>* page_scale_factor() const;
199 void set_hide_pinch_scrollbars_near_min_scale(bool hide) {
200 hide_pinch_scrollbars_near_min_scale_ = hide;
203 SyncedElasticOverscroll* elastic_overscroll() {
204 return elastic_overscroll_.get();
206 const SyncedElasticOverscroll* elastic_overscroll() const {
207 return elastic_overscroll_.get();
210 SyncedTopControls* top_controls_shown_ratio() {
211 return top_controls_shown_ratio_.get();
213 const SyncedTopControls* top_controls_shown_ratio() const {
214 return top_controls_shown_ratio_.get();
217 // Updates draw properties and render surface layer list, as well as tile
218 // priorities. Returns false if it was unable to update. Updating lcd
219 // text may cause invalidations, so should only be done after a commit.
220 bool UpdateDrawProperties(bool update_lcd_text);
221 void BuildPropertyTreesForTesting();
223 void set_needs_update_draw_properties() {
224 needs_update_draw_properties_ = true;
226 bool needs_update_draw_properties() const {
227 return needs_update_draw_properties_;
230 void set_needs_full_tree_sync(bool needs) { needs_full_tree_sync_ = needs; }
231 bool needs_full_tree_sync() const { return needs_full_tree_sync_; }
233 void ForceRedrawNextActivation() { next_activation_forces_redraw_ = true; }
235 void set_has_ever_been_drawn(bool has_drawn) {
236 has_ever_been_drawn_ = has_drawn;
238 bool has_ever_been_drawn() const { return has_ever_been_drawn_; }
240 void set_ui_resource_request_queue(const UIResourceRequestQueue& queue);
242 const LayerImplList& RenderSurfaceLayerList() const;
243 const Region& UnoccludedScreenSpaceRegion() const;
245 // These return the size of the root scrollable area and the size of
246 // the user-visible scrolling viewport, in CSS layout coordinates.
247 gfx::SizeF ScrollableSize() const;
248 gfx::SizeF ScrollableViewportSize() const;
250 gfx::Rect RootScrollLayerDeviceViewportBounds() const;
252 LayerImpl* LayerById(int id) const;
254 // These should be called by LayerImpl's ctor/dtor.
255 void RegisterLayer(LayerImpl* layer);
256 void UnregisterLayer(LayerImpl* layer);
258 size_t NumLayers();
260 AnimationRegistrar* GetAnimationRegistrar() const;
262 void DidBecomeActive();
264 // Set on the active tree when the viewport size recently changed
265 // and the active tree's size is now out of date.
266 bool ViewportSizeInvalid() const;
267 void SetViewportSizeInvalid();
268 void ResetViewportSizeInvalid();
270 // Useful for debug assertions, probably shouldn't be used for anything else.
271 Proxy* proxy() const;
273 // Distribute the root scroll between outer and inner viewport scroll layer.
274 // The outer viewport scroll layer scrolls first.
275 void DistributeRootScrollOffset(const gfx::ScrollOffset& root_offset);
277 void ApplyScroll(LayerImpl* layer, ScrollState* scroll_state) {
278 layer_tree_host_impl_->ApplyScroll(layer, scroll_state);
281 // Call this function when you expect there to be a swap buffer.
282 // See swap_promise.h for how to use SwapPromise.
284 // A swap promise queued by QueueSwapPromise travels with the layer
285 // information currently associated with the tree. For example, when
286 // a pending tree is activated, the swap promise is passed to the
287 // active tree along with the layer information. Similarly, when a
288 // new activation overwrites layer information on the active tree,
289 // queued swap promises are broken.
290 void QueueSwapPromise(scoped_ptr<SwapPromise> swap_promise);
292 // Queue a swap promise, pinned to this tree. Pinned swap promises
293 // may only be queued on the active tree.
295 // An active tree pinned swap promise will see only DidSwap() or
296 // DidNotSwap(SWAP_FAILS). No DidActivate() will be seen because
297 // that has already happened prior to queueing of the swap promise.
299 // Pinned active tree swap promises will not be broken prematurely
300 // on the active tree if a new tree is activated.
301 void QueuePinnedSwapPromise(scoped_ptr<SwapPromise> swap_promise);
303 // Take the |new_swap_promise| and append it to |swap_promise_list_|.
304 void PassSwapPromises(ScopedPtrVector<SwapPromise>* new_swap_promise);
305 void FinishSwapPromises(CompositorFrameMetadata* metadata);
306 void BreakSwapPromises(SwapPromise::DidNotSwapReason reason);
308 void DidModifyTilePriorities();
310 ResourceId ResourceIdForUIResource(UIResourceId uid) const;
311 void ProcessUIResourceRequestQueue();
313 bool IsUIResourceOpaque(UIResourceId uid) const;
315 void RegisterPictureLayerImpl(PictureLayerImpl* layer);
316 void UnregisterPictureLayerImpl(PictureLayerImpl* layer);
317 const std::vector<PictureLayerImpl*>& picture_layers() const {
318 return picture_layers_;
321 void AddLayerWithCopyOutputRequest(LayerImpl* layer);
322 void RemoveLayerWithCopyOutputRequest(LayerImpl* layer);
323 const std::vector<LayerImpl*>& LayersWithCopyOutputRequest() const;
325 int current_render_surface_list_id() const {
326 return render_surface_layer_list_id_;
329 LayerImpl* FindFirstScrollingLayerThatIsHitByPoint(
330 const gfx::PointF& screen_space_point);
332 LayerImpl* FindLayerThatIsHitByPoint(const gfx::PointF& screen_space_point);
334 LayerImpl* FindLayerWithWheelHandlerThatIsHitByPoint(
335 const gfx::PointF& screen_space_point);
337 LayerImpl* FindLayerThatIsHitByPointInTouchHandlerRegion(
338 const gfx::PointF& screen_space_point);
340 void RegisterSelection(const LayerSelection& selection);
342 // Compute the current selection handle location and visbility with respect to
343 // the viewport.
344 void GetViewportSelection(ViewportSelection* selection);
346 void set_top_controls_shrink_blink_size(bool shrink);
347 bool top_controls_shrink_blink_size() const {
348 return top_controls_shrink_blink_size_;
350 bool SetCurrentTopControlsShownRatio(float ratio);
351 float CurrentTopControlsShownRatio() const {
352 return top_controls_shown_ratio_->Current(IsActiveTree());
354 void set_top_controls_height(float top_controls_height);
355 float top_controls_height() const { return top_controls_height_; }
356 void PushTopControlsFromMainThread(float top_controls_shown_ratio);
358 void SetPendingPageScaleAnimation(
359 scoped_ptr<PendingPageScaleAnimation> pending_animation);
360 scoped_ptr<PendingPageScaleAnimation> TakePendingPageScaleAnimation();
362 void GatherFrameTimingRequestIds(std::vector<int64_t>* request_ids);
364 bool IsAnimatingFilterProperty(const LayerImpl* layer) const;
365 bool IsAnimatingOpacityProperty(const LayerImpl* layer) const;
366 bool IsAnimatingTransformProperty(const LayerImpl* layer) const;
368 bool HasPotentiallyRunningFilterAnimation(const LayerImpl* layer) const;
369 bool HasPotentiallyRunningOpacityAnimation(const LayerImpl* layer) const;
370 bool HasPotentiallyRunningTransformAnimation(const LayerImpl* layer) const;
372 bool HasAnyAnimationTargetingProperty(
373 const LayerImpl* layer,
374 Animation::TargetProperty property) const;
376 bool FilterIsAnimatingOnImplOnly(const LayerImpl* layer) const;
377 bool OpacityIsAnimatingOnImplOnly(const LayerImpl* layer) const;
378 bool TransformIsAnimatingOnImplOnly(const LayerImpl* layer) const;
380 bool HasOnlyTranslationTransforms(const LayerImpl* layer) const;
382 bool MaximumTargetScale(const LayerImpl* layer, float* max_scale) const;
383 bool AnimationStartScale(const LayerImpl* layer, float* start_scale) const;
385 bool HasFilterAnimationThatInflatesBounds(const LayerImpl* layer) const;
386 bool HasTransformAnimationThatInflatesBounds(const LayerImpl* layer) const;
387 bool HasAnimationThatInflatesBounds(const LayerImpl* layer) const;
389 bool FilterAnimationBoundsForBox(const LayerImpl* layer,
390 const gfx::BoxF& box,
391 gfx::BoxF* bounds) const;
392 bool TransformAnimationBoundsForBox(const LayerImpl* layer,
393 const gfx::BoxF& box,
394 gfx::BoxF* bounds) const;
396 protected:
397 explicit LayerTreeImpl(
398 LayerTreeHostImpl* layer_tree_host_impl,
399 scoped_refptr<SyncedProperty<ScaleGroup>> page_scale_factor,
400 scoped_refptr<SyncedTopControls> top_controls_shown_ratio,
401 scoped_refptr<SyncedElasticOverscroll> elastic_overscroll);
402 float ClampPageScaleFactorToLimits(float page_scale_factor) const;
403 void PushPageScaleFactorAndLimits(const float* page_scale_factor,
404 float min_page_scale_factor,
405 float max_page_scale_factor);
406 bool SetPageScaleFactorLimits(float min_page_scale_factor,
407 float max_page_scale_factor);
408 void DidUpdatePageScale();
409 void HideInnerViewportScrollbarsIfNeeded();
410 void PushTopControls(const float* top_controls_shown_ratio);
411 LayerTreeHostImpl* layer_tree_host_impl_;
412 int source_frame_number_;
413 scoped_ptr<LayerImpl> root_layer_;
414 HeadsUpDisplayLayerImpl* hud_layer_;
415 PropertyTrees property_trees_;
416 SkColor background_color_;
417 bool has_transparent_background_;
419 int currently_scrolling_layer_id_;
420 int overscroll_elasticity_layer_id_;
421 int page_scale_layer_id_;
422 int inner_viewport_scroll_layer_id_;
423 int outer_viewport_scroll_layer_id_;
425 LayerSelection selection_;
427 scoped_refptr<SyncedProperty<ScaleGroup>> page_scale_factor_;
428 float min_page_scale_factor_;
429 float max_page_scale_factor_;
430 bool hide_pinch_scrollbars_near_min_scale_;
432 scoped_refptr<SyncedElasticOverscroll> elastic_overscroll_;
434 typedef base::hash_map<int, LayerImpl*> LayerIdMap;
435 LayerIdMap layer_id_map_;
437 std::vector<PictureLayerImpl*> picture_layers_;
438 std::vector<LayerImpl*> layers_with_copy_output_request_;
440 // List of visible layers for the most recently prepared frame.
441 LayerImplList render_surface_layer_list_;
442 // After drawing the |render_surface_layer_list_| the areas in this region
443 // would not be fully covered by opaque content.
444 Region unoccluded_screen_space_region_;
446 bool viewport_size_invalid_;
447 bool needs_update_draw_properties_;
449 // In impl-side painting mode, this is true when the tree may contain
450 // structural differences relative to the active tree.
451 bool needs_full_tree_sync_;
453 bool next_activation_forces_redraw_;
455 bool has_ever_been_drawn_;
457 ScopedPtrVector<SwapPromise> swap_promise_list_;
458 ScopedPtrVector<SwapPromise> pinned_swap_promise_list_;
460 UIResourceRequestQueue ui_resource_request_queue_;
462 int render_surface_layer_list_id_;
464 // Whether or not Blink's viewport size was shrunk by the height of the top
465 // controls at the time of the last layout.
466 bool top_controls_shrink_blink_size_;
468 float top_controls_height_;
470 // The amount that the top controls are shown from 0 (hidden) to 1 (fully
471 // shown).
472 scoped_refptr<SyncedTopControls> top_controls_shown_ratio_;
474 scoped_ptr<PendingPageScaleAnimation> pending_page_scale_animation_;
476 private:
477 DISALLOW_COPY_AND_ASSIGN(LayerTreeImpl);
480 } // namespace cc
482 #endif // CC_TREES_LAYER_TREE_IMPL_H_