BookmarkManager: Fix 'new folder text field size changes on clicking it' issue.
[chromium-blink-merge.git] / cc / output / overlay_candidate.cc
blobc05947b3d17956e0980b082d8654ca5582647424
1 // Copyright 2014 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/output/overlay_candidate.h"
7 #include <algorithm>
8 #include <limits>
9 #include "base/logging.h"
10 #include "cc/base/math_util.h"
11 #include "ui/gfx/geometry/rect_conversions.h"
12 #include "ui/gfx/geometry/vector3d_f.h"
14 namespace cc {
16 namespace {
17 // Tolerance for considering axis vector elements to be zero.
18 const SkMScalar kEpsilon = std::numeric_limits<float>::epsilon();
20 enum Axis { NONE, AXIS_POS_X, AXIS_NEG_X, AXIS_POS_Y, AXIS_NEG_Y };
22 Axis VectorToAxis(const gfx::Vector3dF& vec) {
23 if (std::abs(vec.z()) > kEpsilon)
24 return NONE;
25 const bool x_zero = (std::abs(vec.x()) <= kEpsilon);
26 const bool y_zero = (std::abs(vec.y()) <= kEpsilon);
27 if (x_zero && !y_zero)
28 return (vec.y() > 0) ? AXIS_POS_Y : AXIS_NEG_Y;
29 else if (y_zero && !x_zero)
30 return (vec.x() > 0) ? AXIS_POS_X : AXIS_NEG_X;
31 else
32 return NONE;
35 } // namespace
37 OverlayCandidate::OverlayCandidate()
38 : transform(gfx::OVERLAY_TRANSFORM_NONE),
39 format(RGBA_8888),
40 uv_rect(0.f, 0.f, 1.f, 1.f),
41 is_clipped(false),
42 use_output_surface_for_resource(false),
43 resource_id(0),
44 plane_z_order(0),
45 overlay_handled(false) {}
47 OverlayCandidate::~OverlayCandidate() {}
49 // static
50 gfx::OverlayTransform OverlayCandidate::GetOverlayTransform(
51 const gfx::Transform& quad_transform,
52 bool y_flipped) {
53 if (!quad_transform.Preserves2dAxisAlignment()) {
54 return gfx::OVERLAY_TRANSFORM_INVALID;
57 gfx::Vector3dF x_axis = MathUtil::GetXAxis(quad_transform);
58 gfx::Vector3dF y_axis = MathUtil::GetYAxis(quad_transform);
59 if (y_flipped) {
60 y_axis.Scale(-1);
63 Axis x_to = VectorToAxis(x_axis);
64 Axis y_to = VectorToAxis(y_axis);
66 if (x_to == AXIS_POS_X && y_to == AXIS_POS_Y)
67 return gfx::OVERLAY_TRANSFORM_NONE;
68 else if (x_to == AXIS_NEG_X && y_to == AXIS_POS_Y)
69 return gfx::OVERLAY_TRANSFORM_FLIP_HORIZONTAL;
70 else if (x_to == AXIS_POS_X && y_to == AXIS_NEG_Y)
71 return gfx::OVERLAY_TRANSFORM_FLIP_VERTICAL;
72 else if (x_to == AXIS_NEG_Y && y_to == AXIS_POS_X)
73 return gfx::OVERLAY_TRANSFORM_ROTATE_270;
74 else if (x_to == AXIS_NEG_X && y_to == AXIS_NEG_Y)
75 return gfx::OVERLAY_TRANSFORM_ROTATE_180;
76 else if (x_to == AXIS_POS_Y && y_to == AXIS_NEG_X)
77 return gfx::OVERLAY_TRANSFORM_ROTATE_90;
78 else
79 return gfx::OVERLAY_TRANSFORM_INVALID;
82 // static
83 gfx::OverlayTransform OverlayCandidate::ModifyTransform(
84 gfx::OverlayTransform in,
85 gfx::OverlayTransform delta) {
86 // There are 8 different possible transforms. We can characterize these
87 // by looking at where the origin moves and the direction the horizontal goes.
88 // (TL=top-left, BR=bottom-right, H=horizontal, V=vertical).
89 // NONE: TL, H
90 // FLIP_VERTICAL: BL, H
91 // FLIP_HORIZONTAL: TR, H
92 // ROTATE_90: TR, V
93 // ROTATE_180: BR, H
94 // ROTATE_270: BL, V
95 // Missing transforms: TL, V & BR, V
96 // Basic combinations:
97 // Flip X & Y -> Rotate 180 (TL,H -> TR,H -> BR,H or TL,H -> BL,H -> BR,H)
98 // Flip X or Y + Rotate 180 -> other flip (eg, TL,H -> TR,H -> BL,H)
99 // Rotate + Rotate simply adds values.
100 // Rotate 90/270 + flip is invalid because we can only have verticals with
101 // the origin in TR or BL.
102 if (delta == gfx::OVERLAY_TRANSFORM_NONE)
103 return in;
104 switch (in) {
105 case gfx::OVERLAY_TRANSFORM_NONE:
106 return delta;
107 case gfx::OVERLAY_TRANSFORM_FLIP_VERTICAL:
108 switch (delta) {
109 case gfx::OVERLAY_TRANSFORM_FLIP_VERTICAL:
110 return gfx::OVERLAY_TRANSFORM_NONE;
111 case gfx::OVERLAY_TRANSFORM_FLIP_HORIZONTAL:
112 return gfx::OVERLAY_TRANSFORM_ROTATE_180;
113 case gfx::OVERLAY_TRANSFORM_ROTATE_180:
114 return gfx::OVERLAY_TRANSFORM_FLIP_HORIZONTAL;
115 default:
116 return gfx::OVERLAY_TRANSFORM_INVALID;
118 break;
119 case gfx::OVERLAY_TRANSFORM_FLIP_HORIZONTAL:
120 switch (delta) {
121 case gfx::OVERLAY_TRANSFORM_FLIP_HORIZONTAL:
122 return gfx::OVERLAY_TRANSFORM_NONE;
123 case gfx::OVERLAY_TRANSFORM_FLIP_VERTICAL:
124 return gfx::OVERLAY_TRANSFORM_ROTATE_180;
125 case gfx::OVERLAY_TRANSFORM_ROTATE_90:
126 case gfx::OVERLAY_TRANSFORM_ROTATE_180:
127 return gfx::OVERLAY_TRANSFORM_FLIP_VERTICAL;
128 case gfx::OVERLAY_TRANSFORM_ROTATE_270:
129 default:
130 return gfx::OVERLAY_TRANSFORM_INVALID;
132 break;
133 case gfx::OVERLAY_TRANSFORM_ROTATE_90:
134 switch (delta) {
135 case gfx::OVERLAY_TRANSFORM_ROTATE_90:
136 return gfx::OVERLAY_TRANSFORM_ROTATE_180;
137 case gfx::OVERLAY_TRANSFORM_ROTATE_180:
138 return gfx::OVERLAY_TRANSFORM_ROTATE_270;
139 case gfx::OVERLAY_TRANSFORM_ROTATE_270:
140 return gfx::OVERLAY_TRANSFORM_NONE;
141 default:
142 return gfx::OVERLAY_TRANSFORM_INVALID;
144 break;
145 case gfx::OVERLAY_TRANSFORM_ROTATE_180:
146 switch (delta) {
147 case gfx::OVERLAY_TRANSFORM_FLIP_HORIZONTAL:
148 return gfx::OVERLAY_TRANSFORM_FLIP_VERTICAL;
149 case gfx::OVERLAY_TRANSFORM_FLIP_VERTICAL:
150 return gfx::OVERLAY_TRANSFORM_FLIP_HORIZONTAL;
151 case gfx::OVERLAY_TRANSFORM_ROTATE_90:
152 return gfx::OVERLAY_TRANSFORM_ROTATE_270;
153 case gfx::OVERLAY_TRANSFORM_ROTATE_180:
154 return gfx::OVERLAY_TRANSFORM_NONE;
155 case gfx::OVERLAY_TRANSFORM_ROTATE_270:
156 return gfx::OVERLAY_TRANSFORM_ROTATE_90;
157 default:
158 return gfx::OVERLAY_TRANSFORM_INVALID;
160 break;
161 case gfx::OVERLAY_TRANSFORM_ROTATE_270:
162 switch (delta) {
163 case gfx::OVERLAY_TRANSFORM_ROTATE_90:
164 return gfx::OVERLAY_TRANSFORM_NONE;
165 case gfx::OVERLAY_TRANSFORM_ROTATE_180:
166 return gfx::OVERLAY_TRANSFORM_ROTATE_90;
167 case gfx::OVERLAY_TRANSFORM_ROTATE_270:
168 return gfx::OVERLAY_TRANSFORM_ROTATE_180;
169 default:
170 return gfx::OVERLAY_TRANSFORM_INVALID;
172 break;
173 default:
174 return gfx::OVERLAY_TRANSFORM_INVALID;
178 // static
179 gfx::RectF OverlayCandidate::GetOverlayRect(
180 const gfx::Transform& quad_transform,
181 const gfx::Rect& rect) {
182 DCHECK(quad_transform.Preserves2dAxisAlignment());
184 gfx::RectF float_rect(rect);
185 quad_transform.TransformRect(&float_rect);
186 return float_rect;
189 } // namespace cc