1 // Copyright 2011 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/resources/layer_quad.h"
7 #include "base/logging.h"
8 #include "ui/gfx/geometry/quad_f.h"
12 LayerQuad::Edge::Edge(const gfx::PointF
& p
, const gfx::PointF
& q
) {
18 gfx::Vector2dF
tangent(p
.y() - q
.y(), q
.x() - p
.x());
19 float cross2
= p
.x() * q
.y() - q
.x() * p
.y();
21 set(tangent
.x(), tangent
.y(), cross2
);
22 scale(1.0f
/ tangent
.Length());
25 gfx::PointF
LayerQuad::Edge::Intersect(const LayerQuad::Edge
& e
) const {
26 DCHECK(!degenerate());
27 DCHECK(!e
.degenerate());
29 return gfx::PointF((y() * e
.z() - e
.y() * z()) / (x() * e
.y() - e
.x() * y()),
30 (x() * e
.z() - e
.x() * z()) / (e
.x() * y() - x() * e
.y()));
33 LayerQuad::LayerQuad(const gfx::QuadF
& quad
) {
35 left_
= Edge(quad
.p4(), quad
.p1());
36 right_
= Edge(quad
.p2(), quad
.p3());
37 top_
= Edge(quad
.p1(), quad
.p2());
38 bottom_
= Edge(quad
.p3(), quad
.p4());
40 float sign
= quad
.IsCounterClockwise() ? -1 : 1;
47 LayerQuad::LayerQuad(const Edge
& left
,
56 gfx::QuadF
LayerQuad::ToQuadF() const {
57 size_t num_degenerate_edges
= left_
.degenerate() + right_
.degenerate() +
58 top_
.degenerate() + bottom_
.degenerate();
59 if (num_degenerate_edges
> 1) {
63 if (left_
.degenerate()) {
64 return gfx::QuadF(top_
.Intersect(bottom_
), top_
.Intersect(right_
),
65 right_
.Intersect(bottom_
), bottom_
.Intersect(top_
));
67 if (right_
.degenerate()) {
68 return gfx::QuadF(left_
.Intersect(top_
), top_
.Intersect(bottom_
),
69 bottom_
.Intersect(top_
), bottom_
.Intersect(left_
));
71 if (top_
.degenerate()) {
72 return gfx::QuadF(left_
.Intersect(right_
), right_
.Intersect(left_
),
73 right_
.Intersect(bottom_
), bottom_
.Intersect(left_
));
75 if (bottom_
.degenerate()) {
76 return gfx::QuadF(left_
.Intersect(top_
), top_
.Intersect(right_
),
77 right_
.Intersect(left_
), left_
.Intersect(right_
));
79 return gfx::QuadF(left_
.Intersect(top_
),
80 top_
.Intersect(right_
),
81 right_
.Intersect(bottom_
),
82 bottom_
.Intersect(left_
));
85 void LayerQuad::ToFloatArray(float flattened
[12]) const {
86 if (left_
.degenerate()) {
87 flattened
[0] = bottom_
.x();
88 flattened
[1] = bottom_
.y();
89 flattened
[2] = bottom_
.z();
91 flattened
[0] = left_
.x();
92 flattened
[1] = left_
.y();
93 flattened
[2] = left_
.z();
95 if (top_
.degenerate()) {
96 flattened
[3] = left_
.x();
97 flattened
[4] = left_
.y();
98 flattened
[5] = left_
.z();
100 flattened
[3] = top_
.x();
101 flattened
[4] = top_
.y();
102 flattened
[5] = top_
.z();
104 if (right_
.degenerate()) {
105 flattened
[6] = top_
.x();
106 flattened
[7] = top_
.y();
107 flattened
[8] = top_
.z();
109 flattened
[6] = right_
.x();
110 flattened
[7] = right_
.y();
111 flattened
[8] = right_
.z();
113 if (bottom_
.degenerate()) {
114 flattened
[9] = right_
.x();
115 flattened
[10] = right_
.y();
116 flattened
[11] = right_
.z();
118 flattened
[9] = bottom_
.x();
119 flattened
[10] = bottom_
.y();
120 flattened
[11] = bottom_
.z();