[M108 Migration][VD] Avoid pending frame counter becoming negative
[platform/framework/web/chromium-efl.git] / cc / tiles / picture_layer_tiling_set.h
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.
4
5 #ifndef CC_TILES_PICTURE_LAYER_TILING_SET_H_
6 #define CC_TILES_PICTURE_LAYER_TILING_SET_H_
7
8 #include <stddef.h>
9
10 #include <deque>
11 #include <memory>
12 #include <set>
13 #include <vector>
14
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"
19
20 namespace base {
21 namespace trace_event {
22 class TracedValue;
23 }
24 }
25
26 namespace cc {
27
28 class CC_EXPORT PictureLayerTilingSet {
29  public:
30   enum TilingRangeType {
31     HIGHER_THAN_HIGH_RES,
32     HIGH_RES,
33     BETWEEN_HIGH_AND_LOW_RES,
34     LOW_RES,
35     LOWER_THAN_LOW_RES
36   };
37   struct TilingRange {
38     TilingRange(size_t start, size_t end) : start(start), end(end) {}
39
40     size_t start;
41     size_t end;
42   };
43
44   static std::unique_ptr<PictureLayerTilingSet> Create(
45       WhichTree tree,
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);
51
52   PictureLayerTilingSet(const PictureLayerTilingSet&) = delete;
53   ~PictureLayerTilingSet();
54
55   PictureLayerTilingSet& operator=(const PictureLayerTilingSet&) = delete;
56
57   const PictureLayerTilingClient* client() const { return client_; }
58
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();
64
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);
72
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);
79
80   // Invalidates the region on all tilings and recreates the tiles as needed.
81   void Invalidate(const Region& layer_invalidation);
82
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();
91   }
92   WhichTree tree() const { return tree_; }
93
94   PictureLayerTiling* FindTilingWithScaleKey(float scale_key) const;
95   PictureLayerTiling* FindTilingWithResolution(TileResolution resolution) const;
96
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
100   // to |start_scale|.
101   PictureLayerTiling* FindTilingWithNearestScaleKey(
102       float start_scale,
103       float snap_to_existing_tiling_ratio) const;
104
105   void MarkAllTilingsNonIdeal();
106
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
109   // the aspect ratio.
110   float GetMaximumContentsScale() const;
111
112   // Remove one tiling.
113   void Remove(PictureLayerTiling* tiling);
114
115   // Removes all tilings with a contents scale key < |minimum_scale_key|.
116   void RemoveTilingsBelowScaleKey(float minimum_scale_key);
117
118   // Removes all tilings with a contents scale key > |maximum_scale_key|.
119   void RemoveTilingsAboveScaleKey(float maximum_scale);
120
121   // Removes all resources (tilings, raster source).
122   void ReleaseAllResources();
123
124   // Remove all tilings.
125   void RemoveAllTilings();
126
127   // Remove all tiles; keep all tilings.
128   void RemoveAllTiles();
129
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);
137
138   void GetAllPrioritizedTilesForTracing(
139       std::vector<PrioritizedTile>* prioritized_tiles) const;
140
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 {
147    public:
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
150     // identity).
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")
154     // coverage.
155     CoverageIterator(const PictureLayerTilingSet* set,
156                      float coverage_scale,
157                      const gfx::Rect& coverage_rect,
158                      float ideal_contents_scale);
159     ~CoverageIterator();
160
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;
167
168     Tile* operator->() const;
169     Tile* operator*() const;
170
171     CoverageIterator& operator++();
172     operator bool() const;
173
174     TileResolution resolution() const;
175     PictureLayerTiling* CurrentTiling() const;
176
177    private:
178     size_t NextTiling() const;
179
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_;
185
186     Region current_region_;
187     Region missing_region_;
188     Region::Iterator region_iter_;
189   };
190
191   void AsValueInto(base::trace_event::TracedValue* array) const;
192   size_t GPUMemoryUsageInBytes() const;
193
194   TilingRange GetTilingRange(TilingRangeType type) const;
195
196  protected:
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) {}
201
202     gfx::Rect visible_rect_in_layer_space;
203     double frame_time_in_seconds;
204   };
205
206   struct StateSinceLastTilePriorityUpdate {
207     class AutoClear {
208      public:
209       explicit AutoClear(StateSinceLastTilePriorityUpdate* state_to_clear)
210           : state_to_clear_(state_to_clear) {}
211       ~AutoClear() { *state_to_clear_ = StateSinceLastTilePriorityUpdate(); }
212
213      private:
214       raw_ptr<StateSinceLastTilePriorityUpdate> state_to_clear_;
215     };
216
217     StateSinceLastTilePriorityUpdate()
218         : invalidated(false), added_tilings(false) {}
219
220     bool invalidated;
221     bool added_tilings;
222   };
223
224   explicit PictureLayerTilingSet(
225       WhichTree tree,
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);
231
232   void CopyTilingsAndPropertiesFromPendingTwin(
233       const PictureLayerTilingSet* pending_twin_set,
234       scoped_refptr<RasterSource> raster_source,
235       const Region& layer_invalidation);
236
237   void VerifyTilings(const PictureLayerTilingSet* pending_twin_set) const;
238
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);
249
250   std::vector<std::unique_ptr<PictureLayerTiling>> tilings_;
251
252   const int tiling_interest_area_padding_;
253   const float skewport_target_time_in_seconds_;
254   const int skewport_extrapolation_limit_in_screen_pixels_;
255   WhichTree tree_;
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_;
262
263   scoped_refptr<RasterSource> raster_source_;
264
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_;
269
270   friend class Iterator;
271 };
272
273 }  // namespace cc
274
275 #endif  // CC_TILES_PICTURE_LAYER_TILING_SET_H_