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 #include "cc/resources/priority_calculator.h"
      6 
      7 #include <algorithm>
      8 
      9 #include "ui/gfx/rect.h"
     10 
     11 namespace cc {
     12 
     13 static const int kNothingPriorityCutoff = -3;
     14 
     15 static const int kMostHighPriority = -2;
     16 
     17 static const int kUIDrawsToRootSurfacePriority = -1;
     18 static const int kVisibleDrawsToRootSurfacePriority = 0;
     19 static const int kRenderSurfacesPriority = 1;
     20 static const int kUIDoesNotDrawToRootSurfacePriority = 2;
     21 static const int kVisibleDoesNotDrawToRootSurfacePriority = 3;
     22 
     23 static const int kVisibleOnlyPriorityCutoff = 4;
     24 
     25 // The lower digits are how far from being visible the texture is,
     26 // in pixels.
     27 static const int kNotVisibleBasePriority = 1000000;
     28 static const int kNotVisibleLimitPriority = 1900000;
     29 
     30 // Arbitrarily define "nearby" to be 2000 pixels. A better estimate
     31 // would be percent-of-viewport or percent-of-screen.
     32 static const int kVisibleAndNearbyPriorityCutoff =
     33     kNotVisibleBasePriority + 2000;
     34 
     35 // Small animated layers are treated as though they are 512 pixels
     36 // from being visible.
     37 static const int kSmallAnimatedLayerPriority = kNotVisibleBasePriority + 512;
     38 
     39 static const int kLingeringBasePriority = 2000000;
     40 static const int kLingeringLimitPriority = 2900000;
     41 
     42 static const int kMostLowPriority = 3000000;
     43 
     44 static const int kEverythingPriorityCutoff = 3000001;
     45 
     46 // static
     47 int PriorityCalculator::UIPriority(bool draws_to_root_surface) {
     48   return draws_to_root_surface ? kUIDrawsToRootSurfacePriority
     49                                : kUIDoesNotDrawToRootSurfacePriority;
     50 }
     51 
     52 // static
     53 int PriorityCalculator::VisiblePriority(bool draws_to_root_surface) {
     54   return draws_to_root_surface ? kVisibleDrawsToRootSurfacePriority
     55                                : kVisibleDoesNotDrawToRootSurfacePriority;
     56 }
     57 
     58 // static
     59 int PriorityCalculator::RenderSurfacePriority() {
     60   return kRenderSurfacesPriority;
     61 }
     62 
     63 // static
     64 int PriorityCalculator::LingeringPriority(int previous_priority) {
     65   // TODO(reveman): We should remove this once we have priorities for all
     66   // textures (we can't currently calculate distances for off-screen textures).
     67   return std::min(kLingeringLimitPriority,
     68                   std::max(kLingeringBasePriority, previous_priority + 1));
     69 }
     70 
     71 namespace {
     72 int ManhattanDistance(gfx::Rect a, gfx::Rect b) {
     73   gfx::Rect c = gfx::UnionRects(a, b);
     74   int x = std::max(0, c.width() - a.width() - b.width() + 1);
     75   int y = std::max(0, c.height() - a.height() - b.height() + 1);
     76   return (x + y);
     77 }
     78 }
     79 
     80 // static
     81 int PriorityCalculator::PriorityFromDistance(gfx::Rect visible_rect,
     82                                              gfx::Rect texture_rect,
     83                                              bool draws_to_root_surface) {
     84   int distance = ManhattanDistance(visible_rect, texture_rect);
     85   if (!distance)
     86     return VisiblePriority(draws_to_root_surface);
     87   return std::min(kNotVisibleLimitPriority, kNotVisibleBasePriority + distance);
     88 }
     89 
     90 // static
     91 int PriorityCalculator::SmallAnimatedLayerMinPriority() {
     92   return kSmallAnimatedLayerPriority;
     93 }
     94 
     95 // static
     96 int PriorityCalculator::HighestPriority() {
     97   return kMostHighPriority;
     98 }
     99 
    100 // static
    101 int PriorityCalculator::LowestPriority() {
    102   return kMostLowPriority;
    103 }
    104 
    105 // static
    106 int PriorityCalculator::AllowNothingCutoff() {
    107   return kNothingPriorityCutoff;
    108 }
    109 
    110 // static
    111 int PriorityCalculator::AllowVisibleOnlyCutoff() {
    112   return kVisibleOnlyPriorityCutoff;
    113 }
    114 
    115 // static
    116 int PriorityCalculator::AllowVisibleAndNearbyCutoff() {
    117   return kVisibleAndNearbyPriorityCutoff;
    118 }
    119 
    120 // static
    121 int PriorityCalculator::AllowEverythingCutoff() {
    122   return kEverythingPriorityCutoff;
    123 }
    124 
    125 }  // namespace cc
    126