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
5#ifndef CC_RESOURCES_TILE_PRIORITY_H_
6#define CC_RESOURCES_TILE_PRIORITY_H_
7
8#include <algorithm>
9#include <limits>
10#include <string>
11
12#include "base/memory/ref_counted.h"
13#include "base/memory/scoped_ptr.h"
14#include "cc/resources/picture_pile.h"
15#include "ui/gfx/quad_f.h"
16#include "ui/gfx/rect.h"
17#include "ui/gfx/size.h"
18
19namespace base {
20class Value;
21}
22
23namespace cc {
24
25enum WhichTree {
26  // Note: these must be 0 and 1 because we index with them in various places,
27  // e.g. in Tile::priority_.
28  ACTIVE_TREE = 0,
29  PENDING_TREE = 1,
30  NUM_TREES = 2
31  // Be sure to update WhichTreeAsValue when adding new fields.
32};
33scoped_ptr<base::Value> WhichTreeAsValue(WhichTree tree);
34
35enum TileResolution {
36  LOW_RESOLUTION = 0 ,
37  HIGH_RESOLUTION = 1,
38  NON_IDEAL_RESOLUTION = 2,
39};
40std::string TileResolutionToString(TileResolution resolution);
41
42struct CC_EXPORT TilePriority {
43  enum PriorityBin { NOW, SOON, EVENTUALLY };
44
45  TilePriority()
46      : resolution(NON_IDEAL_RESOLUTION),
47        required_for_activation(false),
48        priority_bin(EVENTUALLY),
49        distance_to_visible(std::numeric_limits<float>::infinity()) {}
50
51  TilePriority(TileResolution resolution,
52               PriorityBin bin,
53               float distance_to_visible)
54      : resolution(resolution),
55        required_for_activation(false),
56        priority_bin(bin),
57        distance_to_visible(distance_to_visible) {}
58
59  TilePriority(const TilePriority& active, const TilePriority& pending) {
60    if (active.resolution == HIGH_RESOLUTION ||
61        pending.resolution == HIGH_RESOLUTION)
62      resolution = HIGH_RESOLUTION;
63    else if (active.resolution == LOW_RESOLUTION ||
64             pending.resolution == LOW_RESOLUTION)
65      resolution = LOW_RESOLUTION;
66    else
67      resolution = NON_IDEAL_RESOLUTION;
68
69    required_for_activation =
70        active.required_for_activation || pending.required_for_activation;
71
72    if (active.priority_bin < pending.priority_bin) {
73      priority_bin = active.priority_bin;
74      distance_to_visible = active.distance_to_visible;
75    } else if (active.priority_bin > pending.priority_bin) {
76      priority_bin = pending.priority_bin;
77      distance_to_visible = pending.distance_to_visible;
78    } else {
79      priority_bin = active.priority_bin;
80      distance_to_visible =
81          std::min(active.distance_to_visible, pending.distance_to_visible);
82    }
83  }
84
85  void AsValueInto(base::debug::TracedValue* dict) const;
86
87  bool operator ==(const TilePriority& other) const {
88    return resolution == other.resolution &&
89           priority_bin == other.priority_bin &&
90           distance_to_visible == other.distance_to_visible &&
91           required_for_activation == other.required_for_activation;
92  }
93
94  bool operator !=(const TilePriority& other) const {
95    return !(*this == other);
96  }
97
98  bool IsHigherPriorityThan(const TilePriority& other) const {
99    return priority_bin < other.priority_bin ||
100           (priority_bin == other.priority_bin &&
101            distance_to_visible < other.distance_to_visible);
102  }
103
104  TileResolution resolution;
105  bool required_for_activation;
106  PriorityBin priority_bin;
107  float distance_to_visible;
108};
109
110std::string TilePriorityBinToString(TilePriority::PriorityBin bin);
111
112enum TileMemoryLimitPolicy {
113  // Nothing. This mode is used when visible is set to false.
114  ALLOW_NOTHING = 0,
115
116  // You might be made visible, but you're not being interacted with.
117  ALLOW_ABSOLUTE_MINIMUM = 1,  // Tall.
118
119  // You're being interacted with, but we're low on memory.
120  ALLOW_PREPAINT_ONLY = 2,  // Grande.
121
122  // You're the only thing in town. Go crazy.
123  ALLOW_ANYTHING = 3,  // Venti.
124  NUM_TILE_MEMORY_LIMIT_POLICIES = 4,
125
126  // NOTE: Be sure to update TreePriorityAsValue and kBinPolicyMap when adding
127  // or reordering fields.
128};
129std::string TileMemoryLimitPolicyToString(TileMemoryLimitPolicy policy);
130
131enum TreePriority {
132  SAME_PRIORITY_FOR_BOTH_TREES,
133  SMOOTHNESS_TAKES_PRIORITY,
134  NEW_CONTENT_TAKES_PRIORITY,
135  NUM_TREE_PRIORITIES
136  // Be sure to update TreePriorityAsValue when adding new fields.
137};
138std::string TreePriorityToString(TreePriority prio);
139
140class GlobalStateThatImpactsTilePriority {
141 public:
142  GlobalStateThatImpactsTilePriority()
143      : memory_limit_policy(ALLOW_NOTHING),
144        soft_memory_limit_in_bytes(0),
145        hard_memory_limit_in_bytes(0),
146        num_resources_limit(0),
147        tree_priority(SAME_PRIORITY_FOR_BOTH_TREES) {}
148
149  TileMemoryLimitPolicy memory_limit_policy;
150
151  size_t soft_memory_limit_in_bytes;
152  size_t hard_memory_limit_in_bytes;
153  size_t num_resources_limit;
154
155  TreePriority tree_priority;
156
157  bool operator==(const GlobalStateThatImpactsTilePriority& other) const {
158    return memory_limit_policy == other.memory_limit_policy &&
159           soft_memory_limit_in_bytes == other.soft_memory_limit_in_bytes &&
160           hard_memory_limit_in_bytes == other.hard_memory_limit_in_bytes &&
161           num_resources_limit == other.num_resources_limit &&
162           tree_priority == other.tree_priority;
163  }
164  bool operator!=(const GlobalStateThatImpactsTilePriority& other) const {
165    return !(*this == other);
166  }
167
168  void AsValueInto(base::debug::TracedValue* dict) const;
169};
170
171}  // namespace cc
172
173#endif  // CC_RESOURCES_TILE_PRIORITY_H_
174