[M108 Migration][VD] Avoid pending frame counter becoming negative
[platform/framework/web/chromium-efl.git] / cc / tiles / raster_tile_priority_queue_all.cc
1 // Copyright 2015 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 #include "cc/tiles/raster_tile_priority_queue_all.h"
6
7 #include "base/notreached.h"
8 #include "cc/tiles/tiling_set_raster_queue_all.h"
9
10 namespace cc {
11
12 namespace {
13
14 class RasterOrderComparator {
15  public:
16   explicit RasterOrderComparator(TreePriority tree_priority)
17       : tree_priority_(tree_priority) {}
18
19   // Note that in this function, we have to return true if and only if
20   // a is strictly lower priority than b.
21   bool operator()(
22       const std::unique_ptr<TilingSetRasterQueueAll>& a_queue,
23       const std::unique_ptr<TilingSetRasterQueueAll>& b_queue) const {
24     const TilePriority& a_priority = a_queue->Top().priority();
25     const TilePriority& b_priority = b_queue->Top().priority();
26     bool prioritize_low_res = tree_priority_ == SMOOTHNESS_TAKES_PRIORITY;
27
28     // If the priority bin is the same but one of the tiles is from a
29     // non-drawing layer, then the drawing layer has a higher priority.
30     if (b_priority.priority_bin == a_priority.priority_bin &&
31         b_queue->is_drawing_layer() != a_queue->is_drawing_layer()) {
32       return b_queue->is_drawing_layer();
33     }
34
35     // If the bin is the same but the resolution is not, then the order will be
36     // determined by whether we prioritize low res or not.
37     // TODO(vmpstr): Remove this when TilePriority is no longer a member of Tile
38     // class but instead produced by the iterators.
39     if (b_priority.priority_bin == a_priority.priority_bin &&
40         b_priority.resolution != a_priority.resolution) {
41       // Non ideal resolution should be sorted lower than other resolutions.
42       if (a_priority.resolution == NON_IDEAL_RESOLUTION)
43         return true;
44
45       if (b_priority.resolution == NON_IDEAL_RESOLUTION)
46         return false;
47
48       if (prioritize_low_res)
49         return b_priority.resolution == LOW_RESOLUTION;
50       return b_priority.resolution == HIGH_RESOLUTION;
51     }
52
53     return b_priority.IsHigherPriorityThan(a_priority);
54   }
55
56  private:
57   TreePriority tree_priority_;
58 };
59
60 void CreateTilingSetRasterQueues(
61     const std::vector<PictureLayerImpl*>& layers,
62     TreePriority tree_priority,
63     std::vector<std::unique_ptr<TilingSetRasterQueueAll>>* queues) {
64   DCHECK(queues->empty());
65
66   for (auto* layer : layers) {
67     if (!layer->HasValidTilePriorities())
68       continue;
69
70     PictureLayerTilingSet* tiling_set = layer->picture_layer_tiling_set();
71     bool prioritize_low_res = tree_priority == SMOOTHNESS_TAKES_PRIORITY;
72     std::unique_ptr<TilingSetRasterQueueAll> tiling_set_queue =
73         std::make_unique<TilingSetRasterQueueAll>(
74             tiling_set, prioritize_low_res,
75             layer->contributes_to_drawn_render_surface());
76     // Queues will only contain non empty tiling sets.
77     if (!tiling_set_queue->IsEmpty())
78       queues->push_back(std::move(tiling_set_queue));
79   }
80   std::make_heap(queues->begin(), queues->end(),
81                  RasterOrderComparator(tree_priority));
82 }
83
84 }  // namespace
85
86 RasterTilePriorityQueueAll::RasterTilePriorityQueueAll() = default;
87
88 RasterTilePriorityQueueAll::~RasterTilePriorityQueueAll() = default;
89
90 void RasterTilePriorityQueueAll::Build(
91     const std::vector<PictureLayerImpl*>& active_layers,
92     const std::vector<PictureLayerImpl*>& pending_layers,
93     TreePriority tree_priority) {
94   tree_priority_ = tree_priority;
95
96   CreateTilingSetRasterQueues(active_layers, tree_priority_, &active_queues_);
97   CreateTilingSetRasterQueues(pending_layers, tree_priority_, &pending_queues_);
98 }
99
100 bool RasterTilePriorityQueueAll::IsEmpty() const {
101   return active_queues_.empty() && pending_queues_.empty();
102 }
103
104 const PrioritizedTile& RasterTilePriorityQueueAll::Top() const {
105   DCHECK(!IsEmpty());
106   const auto& next_queues = GetNextQueues();
107   return next_queues.front()->Top();
108 }
109
110 void RasterTilePriorityQueueAll::Pop() {
111   DCHECK(!IsEmpty());
112
113   auto& next_queues = GetNextQueues();
114   std::pop_heap(next_queues.begin(), next_queues.end(),
115                 RasterOrderComparator(tree_priority_));
116   TilingSetRasterQueueAll* queue = next_queues.back().get();
117   queue->Pop();
118
119   // Remove empty queues.
120   if (queue->IsEmpty()) {
121     next_queues.pop_back();
122   } else {
123     std::push_heap(next_queues.begin(), next_queues.end(),
124                    RasterOrderComparator(tree_priority_));
125   }
126 }
127
128 std::vector<std::unique_ptr<TilingSetRasterQueueAll>>&
129 RasterTilePriorityQueueAll::GetNextQueues() {
130   const auto* const_this = static_cast<const RasterTilePriorityQueueAll*>(this);
131   const auto& const_queues = const_this->GetNextQueues();
132   return const_cast<std::vector<std::unique_ptr<TilingSetRasterQueueAll>>&>(
133       const_queues);
134 }
135
136 const std::vector<std::unique_ptr<TilingSetRasterQueueAll>>&
137 RasterTilePriorityQueueAll::GetNextQueues() const {
138   DCHECK(!IsEmpty());
139
140   // If we only have one queue with tiles, return it.
141   if (active_queues_.empty())
142     return pending_queues_;
143   if (pending_queues_.empty())
144     return active_queues_;
145
146   const PrioritizedTile& active_tile = active_queues_.front()->Top();
147   const PrioritizedTile& pending_tile = pending_queues_.front()->Top();
148
149   const TilePriority& active_priority = active_tile.priority();
150   const TilePriority& pending_priority = pending_tile.priority();
151
152   switch (tree_priority_) {
153     case SMOOTHNESS_TAKES_PRIORITY: {
154       // If we're down to soon bin tiles on the active tree and there
155       // is a pending tree, process the now tiles in the pending tree to allow
156       // tiles required for activation to be initialized when memory policy only
157       // allows prepaint. The soon/eventually bin tiles on the active tree are
158       // lowest priority since that work is likely to be thrown away when we
159       // activate.
160       if (active_priority.priority_bin >= TilePriority::SOON &&
161           pending_priority.priority_bin == TilePriority::NOW) {
162         return pending_queues_;
163       }
164       return active_queues_;
165     }
166     case NEW_CONTENT_TAKES_PRIORITY: {
167       // If we're down to soon bin tiles on the pending tree, process the
168       // active tree to allow tiles required for activation to be initialized
169       // when memory policy only allows prepaint. Note that active required for
170       // activation tiles might come from either now or soon bins.
171       if (pending_priority.priority_bin >= TilePriority::SOON &&
172           active_priority.priority_bin <= TilePriority::SOON) {
173         return active_queues_;
174       }
175       return pending_queues_;
176     }
177     case SAME_PRIORITY_FOR_BOTH_TREES: {
178       if (active_priority.IsHigherPriorityThan(pending_priority))
179         return active_queues_;
180       return pending_queues_;
181     }
182     default:
183       NOTREACHED();
184       return active_queues_;
185   }
186 }
187
188 }  // namespace cc