[email protected] | 18134fc | 2012-11-08 22:42:34 | [diff] [blame] | 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. |
| 4 | |
[email protected] | ffaa2a63 | 2012-11-11 14:47:50 | [diff] [blame] | 5 | #ifndef CC_TILE_PRIORITY_H_ |
| 6 | #define CC_TILE_PRIORITY_H_ |
[email protected] | 18134fc | 2012-11-08 22:42:34 | [diff] [blame] | 7 | |
[email protected] | 4cbadc88 | 2012-12-04 08:52:58 | [diff] [blame] | 8 | #include <limits> |
| 9 | |
[email protected] | 18134fc | 2012-11-08 22:42:34 | [diff] [blame] | 10 | #include "base/memory/ref_counted.h" |
[email protected] | 4f0a500 | 2013-01-28 13:02:27 | [diff] [blame] | 11 | #include "base/memory/scoped_ptr.h" |
[email protected] | 18134fc | 2012-11-08 22:42:34 | [diff] [blame] | 12 | #include "cc/picture_pile.h" |
[email protected] | 131a0c2 | 2013-02-12 18:31:08 | [diff] [blame] | 13 | #include "ui/gfx/quad_f.h" |
[email protected] | 18134fc | 2012-11-08 22:42:34 | [diff] [blame] | 14 | #include "ui/gfx/rect.h" |
| 15 | #include "ui/gfx/size.h" |
| 16 | |
[email protected] | 4f0a500 | 2013-01-28 13:02:27 | [diff] [blame] | 17 | namespace base { |
| 18 | class Value; |
| 19 | } |
| 20 | |
[email protected] | 18134fc | 2012-11-08 22:42:34 | [diff] [blame] | 21 | namespace cc { |
| 22 | |
[email protected] | 8947cbe | 2012-11-28 05:27:43 | [diff] [blame] | 23 | enum WhichTree { |
| 24 | // Note: these must be 0 and 1 because we index with them in various places, |
| 25 | // e.g. in Tile::priority_. |
| 26 | ACTIVE_TREE = 0, |
[email protected] | ee371e0 | 2012-12-16 20:13:44 | [diff] [blame] | 27 | PENDING_TREE = 1, |
| 28 | NUM_TREES = 2 |
[email protected] | 4f0a500 | 2013-01-28 13:02:27 | [diff] [blame] | 29 | // Be sure to update WhichTreeAsValue when adding new fields. |
[email protected] | 8947cbe | 2012-11-28 05:27:43 | [diff] [blame] | 30 | }; |
[email protected] | 4f0a500 | 2013-01-28 13:02:27 | [diff] [blame] | 31 | scoped_ptr<base::Value> WhichTreeAsValue( |
| 32 | WhichTree tree); |
[email protected] | 8947cbe | 2012-11-28 05:27:43 | [diff] [blame] | 33 | |
[email protected] | 0f0609c | 2012-11-16 10:00:47 | [diff] [blame] | 34 | enum TileResolution { |
| 35 | LOW_RESOLUTION = 0 , |
| 36 | HIGH_RESOLUTION = 1, |
[email protected] | 63db922 | 2013-01-29 22:13:47 | [diff] [blame] | 37 | NON_IDEAL_RESOLUTION = 2, |
[email protected] | 0f0609c | 2012-11-16 10:00:47 | [diff] [blame] | 38 | }; |
[email protected] | 131a0c2 | 2013-02-12 18:31:08 | [diff] [blame] | 39 | scoped_ptr<base::Value> TileResolutionAsValue( |
| 40 | TileResolution resolution); |
[email protected] | 0f0609c | 2012-11-16 10:00:47 | [diff] [blame] | 41 | |
[email protected] | 4cbadc88 | 2012-12-04 08:52:58 | [diff] [blame] | 42 | struct CC_EXPORT TilePriority { |
[email protected] | 0f0609c | 2012-11-16 10:00:47 | [diff] [blame] | 43 | TilePriority() |
[email protected] | 63db922 | 2013-01-29 22:13:47 | [diff] [blame] | 44 | : is_live(false), |
| 45 | resolution(NON_IDEAL_RESOLUTION), |
[email protected] | 91735ee | 2013-02-12 05:17:43 | [diff] [blame] | 46 | time_to_visible_in_seconds(std::numeric_limits<float>::infinity()), |
| 47 | distance_to_visible_in_pixels(std::numeric_limits<float>::infinity()) {} |
[email protected] | 76858dd38 | 2012-11-10 09:38:07 | [diff] [blame] | 48 | |
[email protected] | 63db922 | 2013-01-29 22:13:47 | [diff] [blame] | 49 | TilePriority( |
| 50 | TileResolution resolution, |
| 51 | float time_to_visible_in_seconds, |
| 52 | float distance_to_visible_in_pixels) |
| 53 | : is_live(true), |
| 54 | resolution(resolution), |
| 55 | time_to_visible_in_seconds(time_to_visible_in_seconds), |
| 56 | distance_to_visible_in_pixels(distance_to_visible_in_pixels) {} |
| 57 | |
[email protected] | 0f0609c | 2012-11-16 10:00:47 | [diff] [blame] | 58 | TilePriority(const TilePriority& active, const TilePriority& pending) { |
[email protected] | 63db922 | 2013-01-29 22:13:47 | [diff] [blame] | 59 | if (!pending.is_live) { |
| 60 | if (!active.is_live) { |
| 61 | is_live = false; |
| 62 | return; |
| 63 | } |
| 64 | is_live = true; |
| 65 | resolution = active.resolution; |
| 66 | time_to_visible_in_seconds = active.time_to_visible_in_seconds; |
| 67 | distance_to_visible_in_pixels = active.distance_to_visible_in_pixels; |
| 68 | return; |
| 69 | } else if (!active.is_live) { |
| 70 | is_live = true; |
| 71 | resolution = pending.resolution; |
| 72 | time_to_visible_in_seconds = pending.time_to_visible_in_seconds; |
| 73 | distance_to_visible_in_pixels = pending.distance_to_visible_in_pixels; |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | is_live = true; |
[email protected] | 0f0609c | 2012-11-16 10:00:47 | [diff] [blame] | 78 | if (active.resolution == HIGH_RESOLUTION || |
| 79 | pending.resolution == HIGH_RESOLUTION) |
| 80 | resolution = HIGH_RESOLUTION; |
| 81 | else if (active.resolution == LOW_RESOLUTION || |
| 82 | pending.resolution == LOW_RESOLUTION) |
| 83 | resolution = LOW_RESOLUTION; |
| 84 | else |
| 85 | resolution = NON_IDEAL_RESOLUTION; |
[email protected] | 18134fc | 2012-11-08 22:42:34 | [diff] [blame] | 86 | |
[email protected] | 0f0609c | 2012-11-16 10:00:47 | [diff] [blame] | 87 | time_to_visible_in_seconds = |
| 88 | std::min(active.time_to_visible_in_seconds, |
| 89 | pending.time_to_visible_in_seconds); |
[email protected] | 0f0609c | 2012-11-16 10:00:47 | [diff] [blame] | 90 | distance_to_visible_in_pixels = |
| 91 | std::min(active.distance_to_visible_in_pixels, |
| 92 | pending.distance_to_visible_in_pixels); |
| 93 | } |
[email protected] | 131a0c2 | 2013-02-12 18:31:08 | [diff] [blame] | 94 | void set_current_screen_quad(const gfx::QuadF& q) { current_screen_quad = q; } |
| 95 | |
| 96 | scoped_ptr<base::Value> AsValue() const; |
[email protected] | 18134fc | 2012-11-08 22:42:34 | [diff] [blame] | 97 | |
[email protected] | a3bca5f3 | 2013-02-02 01:51:58 | [diff] [blame] | 98 | static const float kMaxDistanceInContentSpace; |
[email protected] | 8b981a9 | 2013-02-20 02:47:39 | [diff] [blame] | 99 | static const int64 kNumTilesToCoverWithInflatedViewportRectForPrioritization; |
[email protected] | 4cbadc88 | 2012-12-04 08:52:58 | [diff] [blame] | 100 | |
[email protected] | a3bca5f3 | 2013-02-02 01:51:58 | [diff] [blame] | 101 | static inline float manhattanDistance(const gfx::RectF& a, const gfx::RectF& b) { |
| 102 | // Compute the union explicitly. |
| 103 | gfx::RectF c = gfx::RectF( |
| 104 | std::min(a.x(), b.x()), |
| 105 | std::min(a.y(), b.y()), |
| 106 | std::max(a.right(), b.right()) - std::min(a.x(), b.x()), |
| 107 | std::max(a.bottom(), b.bottom()) - std::min(a.y(), b.y())); |
| 108 | |
| 109 | // Rects touching the edge of the screen should not be considered visible. |
| 110 | // So we add 1 pixel here to avoid that situation. |
| 111 | float x = std::max(0.0f, c.width() - a.width() - b.width() + 1.0f); |
| 112 | float y = std::max(0.0f, c.height() - a.height() - b.height() + 1.0f); |
| 113 | return (x + y); |
| 114 | } |
[email protected] | 4cbadc88 | 2012-12-04 08:52:58 | [diff] [blame] | 115 | |
| 116 | // Calculate the time for the |current_bounds| to intersect with the |
| 117 | // |target_bounds| given its previous location and time delta. |
| 118 | // This function should work for both scaling and scrolling case. |
[email protected] | a3bca5f3 | 2013-02-02 01:51:58 | [diff] [blame] | 119 | static float TimeForBoundsToIntersect(const gfx::RectF& previous_bounds, |
| 120 | const gfx::RectF& current_bounds, |
| 121 | float time_delta, |
| 122 | const gfx::RectF& target_bounds); |
[email protected] | 4cbadc88 | 2012-12-04 08:52:58 | [diff] [blame] | 123 | |
[email protected] | 63db922 | 2013-01-29 22:13:47 | [diff] [blame] | 124 | // If a tile is not live, then all other fields are invalid. |
| 125 | bool is_live; |
[email protected] | 0f0609c | 2012-11-16 10:00:47 | [diff] [blame] | 126 | TileResolution resolution; |
| 127 | float time_to_visible_in_seconds; |
[email protected] | 0f0609c | 2012-11-16 10:00:47 | [diff] [blame] | 128 | float distance_to_visible_in_pixels; |
[email protected] | 131a0c2 | 2013-02-12 18:31:08 | [diff] [blame] | 129 | |
| 130 | private: |
| 131 | gfx::QuadF current_screen_quad; |
[email protected] | 18134fc | 2012-11-08 22:42:34 | [diff] [blame] | 132 | }; |
| 133 | |
[email protected] | 76858dd38 | 2012-11-10 09:38:07 | [diff] [blame] | 134 | enum TileMemoryLimitPolicy { |
| 135 | // Nothing. |
| 136 | ALLOW_NOTHING, |
| 137 | |
[email protected] | 0f0609c | 2012-11-16 10:00:47 | [diff] [blame] | 138 | // You might be made visible, but you're not being interacted with. |
| 139 | ALLOW_ABSOLUTE_MINIMUM, // Tall. |
[email protected] | 76858dd38 | 2012-11-10 09:38:07 | [diff] [blame] | 140 | |
[email protected] | 0f0609c | 2012-11-16 10:00:47 | [diff] [blame] | 141 | // You're being interacted with, but we're low on memory. |
| 142 | ALLOW_PREPAINT_ONLY, // Grande. |
[email protected] | 76858dd38 | 2012-11-10 09:38:07 | [diff] [blame] | 143 | |
[email protected] | 0f0609c | 2012-11-16 10:00:47 | [diff] [blame] | 144 | // You're the only thing in town. Go crazy. |
| 145 | ALLOW_ANYTHING, // Venti. |
[email protected] | 4f0a500 | 2013-01-28 13:02:27 | [diff] [blame] | 146 | |
| 147 | // Be sure to update TreePriorityAsValue when adding new fields. |
[email protected] | 76858dd38 | 2012-11-10 09:38:07 | [diff] [blame] | 148 | }; |
[email protected] | 4f0a500 | 2013-01-28 13:02:27 | [diff] [blame] | 149 | scoped_ptr<base::Value> TileMemoryLimitPolicyAsValue( |
| 150 | TileMemoryLimitPolicy policy); |
[email protected] | 76858dd38 | 2012-11-10 09:38:07 | [diff] [blame] | 151 | |
[email protected] | 362f1e8b | 2013-01-21 16:54:30 | [diff] [blame] | 152 | enum TreePriority { |
| 153 | SAME_PRIORITY_FOR_BOTH_TREES, |
| 154 | SMOOTHNESS_TAKES_PRIORITY, |
| 155 | NEW_CONTENT_TAKES_PRIORITY |
[email protected] | 4f0a500 | 2013-01-28 13:02:27 | [diff] [blame] | 156 | |
| 157 | // Be sure to update TreePriorityAsValue when adding new fields. |
[email protected] | 362f1e8b | 2013-01-21 16:54:30 | [diff] [blame] | 158 | }; |
[email protected] | 4f0a500 | 2013-01-28 13:02:27 | [diff] [blame] | 159 | scoped_ptr<base::Value> TreePriorityAsValue(TreePriority prio); |
[email protected] | 362f1e8b | 2013-01-21 16:54:30 | [diff] [blame] | 160 | |
[email protected] | 76858dd38 | 2012-11-10 09:38:07 | [diff] [blame] | 161 | class GlobalStateThatImpactsTilePriority { |
[email protected] | ffaa2a63 | 2012-11-11 14:47:50 | [diff] [blame] | 162 | public: |
[email protected] | 76858dd38 | 2012-11-10 09:38:07 | [diff] [blame] | 163 | GlobalStateThatImpactsTilePriority() |
| 164 | : memory_limit_policy(ALLOW_NOTHING) |
| 165 | , memory_limit_in_bytes(0) |
[email protected] | 362f1e8b | 2013-01-21 16:54:30 | [diff] [blame] | 166 | , tree_priority(SAME_PRIORITY_FOR_BOTH_TREES) { |
[email protected] | 76858dd38 | 2012-11-10 09:38:07 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | TileMemoryLimitPolicy memory_limit_policy; |
| 170 | |
| 171 | size_t memory_limit_in_bytes; |
| 172 | |
[email protected] | 362f1e8b | 2013-01-21 16:54:30 | [diff] [blame] | 173 | TreePriority tree_priority; |
[email protected] | 4f0a500 | 2013-01-28 13:02:27 | [diff] [blame] | 174 | |
| 175 | scoped_ptr<base::Value> AsValue() const; |
[email protected] | 18134fc | 2012-11-08 22:42:34 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | } // namespace cc |
[email protected] | ffaa2a63 | 2012-11-11 14:47:50 | [diff] [blame] | 179 | |
| 180 | #endif // CC_TILE_PRIORITY_H_ |