Roll WebRTC 9745:9761, Libjingle 9742:9761
[chromium-blink-merge.git] / cc / output / layer_quad.cc
blob537c87bd61dfc0f5f3b74bb405b8f714ed2c6cb5
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/output/layer_quad.h"
7 #include "base/logging.h"
8 #include "ui/gfx/geometry/quad_f.h"
10 namespace cc {
12 LayerQuad::Edge::Edge(const gfx::PointF& p, const gfx::PointF& q) {
13 if (p == q) {
14 degenerate_ = true;
15 return;
17 degenerate_ = false;
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) {
34 // Create edges.
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;
41 left_.scale(sign);
42 right_.scale(sign);
43 top_.scale(sign);
44 bottom_.scale(sign);
47 LayerQuad::LayerQuad(const Edge& left,
48 const Edge& top,
49 const Edge& right,
50 const Edge& bottom)
51 : left_(left),
52 top_(top),
53 right_(right),
54 bottom_(bottom) {}
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) {
60 return gfx::QuadF();
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();
90 } else {
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();
99 } else {
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();
108 } else {
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();
117 } else {
118 flattened[9] = bottom_.x();
119 flattened[10] = bottom_.y();
120 flattened[11] = bottom_.z();
124 } // namespace cc