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.
5 #include "cc/priority_calculator.h"
7 #include "ui/gfx/rect.h"
11 static const int nothingPriorityCutoff
= -3;
13 static const int mostHighPriority
= -2;
15 static const int uiDrawsToRootSurfacePriority
= -1;
16 static const int visibleDrawsToRootSurfacePriority
= 0;
17 static const int renderSurfacesPriority
= 1;
18 static const int uiDoesNotDrawToRootSurfacePriority
= 2;
19 static const int visibleDoesNotDrawToRootSurfacePriority
= 3;
21 static const int visibleOnlyPriorityCutoff
= 4;
23 // The lower digits are how far from being visible the texture is,
25 static const int notVisibleBasePriority
= 1000000;
26 static const int notVisibleLimitPriority
= 1900000;
28 // Arbitrarily define "nearby" to be 2000 pixels. A better estimate
29 // would be percent-of-viewport or percent-of-screen.
30 static const int visibleAndNearbyPriorityCutoff
= notVisibleBasePriority
+ 2000;
32 // Small animated layers are treated as though they are 512 pixels
33 // from being visible.
34 static const int smallAnimatedLayerPriority
= notVisibleBasePriority
+ 512;
36 static const int lingeringBasePriority
= 2000000;
37 static const int lingeringLimitPriority
= 2900000;
39 static const int mostLowPriority
= 3000000;
41 static const int everythingPriorityCutoff
= 3000001;
44 int PriorityCalculator::uiPriority(bool drawsToRootSurface
)
46 return drawsToRootSurface
? uiDrawsToRootSurfacePriority
: uiDoesNotDrawToRootSurfacePriority
;
50 int PriorityCalculator::visiblePriority(bool drawsToRootSurface
)
52 return drawsToRootSurface
? visibleDrawsToRootSurfacePriority
: visibleDoesNotDrawToRootSurfacePriority
;
56 int PriorityCalculator::renderSurfacePriority()
58 return renderSurfacesPriority
;
62 int PriorityCalculator::lingeringPriority(int previousPriority
)
64 // FIXME: We should remove this once we have priorities for all
65 // textures (we can't currently calculate distances for
66 // off-screen textures).
67 return std::min(lingeringLimitPriority
,
68 std::max(lingeringBasePriority
, previousPriority
+ 1));
72 int manhattanDistance(const gfx::Rect
& a
, const gfx::Rect
& b
)
74 gfx::Rect c
= gfx::UnionRects(a
, b
);
75 int x
= std::max(0, c
.width() - a
.width() - b
.width() + 1);
76 int y
= std::max(0, c
.height() - a
.height() - b
.height() + 1);
82 int PriorityCalculator::priorityFromDistance(const gfx::Rect
& visibleRect
, const gfx::Rect
& textureRect
, bool drawsToRootSurface
)
84 int distance
= manhattanDistance(visibleRect
, textureRect
);
86 return visiblePriority(drawsToRootSurface
);
87 return std::min(notVisibleLimitPriority
, notVisibleBasePriority
+ distance
);
91 int PriorityCalculator::smallAnimatedLayerMinPriority()
93 return smallAnimatedLayerPriority
;
97 int PriorityCalculator::highestPriority()
99 return mostHighPriority
;
103 int PriorityCalculator::lowestPriority()
105 return mostLowPriority
;
109 int PriorityCalculator::allowNothingCutoff()
111 return nothingPriorityCutoff
;
115 int PriorityCalculator::allowVisibleOnlyCutoff()
117 return visibleOnlyPriorityCutoff
;
121 int PriorityCalculator::allowVisibleAndNearbyCutoff()
123 return visibleAndNearbyPriorityCutoff
;
127 int PriorityCalculator::allowEverythingCutoff()
129 return everythingPriorityCutoff
;