1 // Copyright 2012 The Chromium Authors
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_TILES_PICTURE_LAYER_TILING_SET_H_
6 #define CC_TILES_PICTURE_LAYER_TILING_SET_H_
15 #include "base/memory/raw_ptr.h"
16 #include "cc/base/region.h"
17 #include "cc/tiles/picture_layer_tiling.h"
18 #include "ui/gfx/geometry/size.h"
21 namespace trace_event {
28 class CC_EXPORT PictureLayerTilingSet {
30 enum TilingRangeType {
33 BETWEEN_HIGH_AND_LOW_RES,
38 TilingRange(size_t start, size_t end) : start(start), end(end) {}
44 static std::unique_ptr<PictureLayerTilingSet> Create(
46 PictureLayerTilingClient* client,
47 int tiling_interest_area_padding,
48 float skewport_target_time_in_seconds,
49 int skewport_extrapolation_limit_in_screen_pixels,
50 float max_preraster_distance);
52 PictureLayerTilingSet(const PictureLayerTilingSet&) = delete;
53 ~PictureLayerTilingSet();
55 PictureLayerTilingSet& operator=(const PictureLayerTilingSet&) = delete;
57 const PictureLayerTilingClient* client() const { return client_; }
59 void CleanUpTilings(float min_acceptable_high_res_scale_key,
60 float max_acceptable_high_res_scale_key,
61 const std::vector<PictureLayerTiling*>& needed_tilings,
62 PictureLayerTilingSet* twin_set);
63 void RemoveNonIdealTilings();
65 // This function is called on the active tree during activation.
66 void UpdateTilingsToCurrentRasterSourceForActivation(
67 scoped_refptr<RasterSource> raster_source,
68 const PictureLayerTilingSet* pending_twin_set,
69 const Region& layer_invalidation,
70 float minimum_contents_scale,
71 float maximum_contents_scale);
73 // This function is called on the sync tree during commit.
74 void UpdateTilingsToCurrentRasterSourceForCommit(
75 scoped_refptr<RasterSource> raster_source,
76 const Region& layer_invalidation,
77 float minimum_contents_scale,
78 float maximum_contents_scale);
80 // Invalidates the region on all tilings and recreates the tiles as needed.
81 void Invalidate(const Region& layer_invalidation);
83 PictureLayerTiling* AddTiling(const gfx::AxisTransform2d& raster_transform,
84 scoped_refptr<RasterSource> raster_source,
85 bool can_use_lcd_text = false);
86 size_t num_tilings() const { return tilings_.size(); }
87 int NumHighResTilings() const;
88 PictureLayerTiling* tiling_at(size_t idx) { return tilings_[idx].get(); }
89 const PictureLayerTiling* tiling_at(size_t idx) const {
90 return tilings_[idx].get();
92 WhichTree tree() const { return tree_; }
94 PictureLayerTiling* FindTilingWithScaleKey(float scale_key) const;
95 PictureLayerTiling* FindTilingWithResolution(TileResolution resolution) const;
97 // If a tiling exists whose scale is within |snap_to_existing_tiling_ratio|
98 // ratio of |start_scale|, then return that tiling. Otherwise, return null.
99 // If multiple tilings match the criteria, return the one with the least ratio
101 PictureLayerTiling* FindTilingWithNearestScaleKey(
103 float snap_to_existing_tiling_ratio) const;
105 void MarkAllTilingsNonIdeal();
107 // Returns the maximum contents scale of all tilings, or 0 if no tilings
108 // exist. Note that this returns the maximum of x and y scales depending on
110 float GetMaximumContentsScale() const;
112 // Remove one tiling.
113 void Remove(PictureLayerTiling* tiling);
115 // Removes all tilings with a contents scale key < |minimum_scale_key|.
116 void RemoveTilingsBelowScaleKey(float minimum_scale_key);
118 // Removes all tilings with a contents scale key > |maximum_scale_key|.
119 void RemoveTilingsAboveScaleKey(float maximum_scale);
121 // Removes all resources (tilings, raster source).
122 void ReleaseAllResources();
124 // Remove all tilings.
125 void RemoveAllTilings();
127 // Remove all tiles; keep all tilings.
128 void RemoveAllTiles();
130 // Update the rects and priorities for tiles based on the given information.
131 // Returns true if PrepareTiles is required.
132 bool UpdateTilePriorities(const gfx::Rect& required_rect_in_layer_space,
133 float ideal_contents_scale,
134 double current_frame_time_in_seconds,
135 const Occlusion& occlusion_in_layer_space,
136 bool can_require_tiles_for_activation);
138 void GetAllPrioritizedTilesForTracing(
139 std::vector<PrioritizedTile>* prioritized_tiles) const;
141 // For a given rect, iterates through tiles that can fill it. If no
142 // set of tiles with resources can fill the rect, then it will iterate
143 // through null tiles with valid geometry_rect() until the rect is full.
144 // If all tiles have resources, the union of all geometry_rects will
145 // exactly fill rect with no overlap.
146 class CC_EXPORT CoverageIterator {
148 // |coverage_scale| is the scale at which we want to produce the coverage.
149 // This is the scale at which |coverage_rect| is specified (relative to
151 // |coverage_rect| is a rect that we want to cover during this iteration.
152 // |ideal_contents_scale| is the ideal scale that we want, which determines
153 // the order in which tilings are processed to get the best ("crispest")
155 CoverageIterator(const PictureLayerTilingSet* set,
156 float coverage_scale,
157 const gfx::Rect& coverage_rect,
158 float ideal_contents_scale);
161 // Visible rect (no borders), in the space of |coverage_rect| (ie at
162 // |coverage_scale| from identity). This is clipped to the coverage_rect.
163 gfx::Rect geometry_rect() const;
164 // A geometry_rect scaled to the tiling's contents scale, which represents
165 // the texture rect in texels.
166 gfx::RectF texture_rect() const;
168 Tile* operator->() const;
169 Tile* operator*() const;
171 CoverageIterator& operator++();
172 operator bool() const;
174 TileResolution resolution() const;
175 PictureLayerTiling* CurrentTiling() const;
178 size_t NextTiling() const;
180 raw_ptr<const PictureLayerTilingSet> set_;
181 float coverage_scale_;
182 PictureLayerTiling::CoverageIterator tiling_iter_;
183 size_t current_tiling_;
184 size_t ideal_tiling_;
186 Region current_region_;
187 Region missing_region_;
188 Region::Iterator region_iter_;
191 void AsValueInto(base::trace_event::TracedValue* array) const;
192 size_t GPUMemoryUsageInBytes() const;
194 TilingRange GetTilingRange(TilingRangeType type) const;
197 struct FrameVisibleRect {
198 FrameVisibleRect(const gfx::Rect& rect, double time_in_seconds)
199 : visible_rect_in_layer_space(rect),
200 frame_time_in_seconds(time_in_seconds) {}
202 gfx::Rect visible_rect_in_layer_space;
203 double frame_time_in_seconds;
206 struct StateSinceLastTilePriorityUpdate {
209 explicit AutoClear(StateSinceLastTilePriorityUpdate* state_to_clear)
210 : state_to_clear_(state_to_clear) {}
211 ~AutoClear() { *state_to_clear_ = StateSinceLastTilePriorityUpdate(); }
214 raw_ptr<StateSinceLastTilePriorityUpdate> state_to_clear_;
217 StateSinceLastTilePriorityUpdate()
218 : invalidated(false), added_tilings(false) {}
224 explicit PictureLayerTilingSet(
226 PictureLayerTilingClient* client,
227 int tiling_interest_area_padding,
228 float skewport_target_time_in_seconds,
229 int skewport_extrapolation_limit_in_screen_pixels,
230 float max_preraster_distance);
232 void CopyTilingsAndPropertiesFromPendingTwin(
233 const PictureLayerTilingSet* pending_twin_set,
234 scoped_refptr<RasterSource> raster_source,
235 const Region& layer_invalidation);
237 void VerifyTilings(const PictureLayerTilingSet* pending_twin_set) const;
239 bool TilingsNeedUpdate(const gfx::Rect& required_rect_in_layer_space,
240 double current_frame_time_in_Seconds);
241 gfx::Rect ComputeSkewport(const gfx::Rect& visible_rect_in_layer_space,
242 double current_frame_time_in_seconds,
243 float ideal_contents_scale);
244 gfx::Rect ComputeSoonBorderRect(const gfx::Rect& visible_rect_in_layer_space,
245 float ideal_contents_scale);
246 void UpdatePriorityRects(const gfx::Rect& visible_rect_in_layer_space,
247 double current_frame_time_in_seconds,
248 float ideal_contents_scale);
250 std::vector<std::unique_ptr<PictureLayerTiling>> tilings_;
252 const int tiling_interest_area_padding_;
253 const float skewport_target_time_in_seconds_;
254 const int skewport_extrapolation_limit_in_screen_pixels_;
256 raw_ptr<PictureLayerTilingClient> client_;
257 const float max_preraster_distance_;
258 // State saved for computing velocities based on finite differences.
259 // .front() of the deque refers to the most recent FrameVisibleRect.
260 std::deque<FrameVisibleRect> visible_rect_history_;
261 StateSinceLastTilePriorityUpdate state_since_last_tile_priority_update_;
263 scoped_refptr<RasterSource> raster_source_;
265 gfx::Rect visible_rect_in_layer_space_;
266 gfx::Rect skewport_in_layer_space_;
267 gfx::Rect soon_border_rect_in_layer_space_;
268 gfx::Rect eventually_rect_in_layer_space_;
270 friend class Iterator;
275 #endif // CC_TILES_PICTURE_LAYER_TILING_SET_H_