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.
5 #include "cc/tiles/raster_tile_priority_queue_all.h"
7 #include "base/notreached.h"
8 #include "cc/tiles/tiling_set_raster_queue_all.h"
14 class RasterOrderComparator {
16 explicit RasterOrderComparator(TreePriority tree_priority)
17 : tree_priority_(tree_priority) {}
19 // Note that in this function, we have to return true if and only if
20 // a is strictly lower priority than b.
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;
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();
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)
45 if (b_priority.resolution == NON_IDEAL_RESOLUTION)
48 if (prioritize_low_res)
49 return b_priority.resolution == LOW_RESOLUTION;
50 return b_priority.resolution == HIGH_RESOLUTION;
53 return b_priority.IsHigherPriorityThan(a_priority);
57 TreePriority tree_priority_;
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());
66 for (auto* layer : layers) {
67 if (!layer->HasValidTilePriorities())
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));
80 std::make_heap(queues->begin(), queues->end(),
81 RasterOrderComparator(tree_priority));
86 RasterTilePriorityQueueAll::RasterTilePriorityQueueAll() = default;
88 RasterTilePriorityQueueAll::~RasterTilePriorityQueueAll() = default;
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;
96 CreateTilingSetRasterQueues(active_layers, tree_priority_, &active_queues_);
97 CreateTilingSetRasterQueues(pending_layers, tree_priority_, &pending_queues_);
100 bool RasterTilePriorityQueueAll::IsEmpty() const {
101 return active_queues_.empty() && pending_queues_.empty();
104 const PrioritizedTile& RasterTilePriorityQueueAll::Top() const {
106 const auto& next_queues = GetNextQueues();
107 return next_queues.front()->Top();
110 void RasterTilePriorityQueueAll::Pop() {
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();
119 // Remove empty queues.
120 if (queue->IsEmpty()) {
121 next_queues.pop_back();
123 std::push_heap(next_queues.begin(), next_queues.end(),
124 RasterOrderComparator(tree_priority_));
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>>&>(
136 const std::vector<std::unique_ptr<TilingSetRasterQueueAll>>&
137 RasterTilePriorityQueueAll::GetNextQueues() const {
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_;
146 const PrioritizedTile& active_tile = active_queues_.front()->Top();
147 const PrioritizedTile& pending_tile = pending_queues_.front()->Top();
149 const TilePriority& active_priority = active_tile.priority();
150 const TilePriority& pending_priority = pending_tile.priority();
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
160 if (active_priority.priority_bin >= TilePriority::SOON &&
161 pending_priority.priority_bin == TilePriority::NOW) {
162 return pending_queues_;
164 return active_queues_;
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_;
175 return pending_queues_;
177 case SAME_PRIORITY_FOR_BOTH_TREES: {
178 if (active_priority.IsHigherPriorityThan(pending_priority))
179 return active_queues_;
180 return pending_queues_;
184 return active_queues_;