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