James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "cc/resources/layer_quad.h" |
| 6 | |
| 7 | #include "base/logging.h" |
James Robinson | 30d547e | 2014-10-23 18:20:06 -0700 | [diff] [blame] | 8 | #include "ui/gfx/geometry/quad_f.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 9 | |
| 10 | namespace cc { |
| 11 | |
| 12 | LayerQuad::Edge::Edge(const gfx::PointF& p, const gfx::PointF& q) { |
Nick Bray | 0bcbd3b | 2015-03-12 16:29:36 -0700 | [diff] [blame] | 13 | if (p == q) { |
| 14 | degenerate_ = true; |
| 15 | return; |
| 16 | } |
| 17 | degenerate_ = false; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 18 | gfx::Vector2dF tangent(p.y() - q.y(), q.x() - p.x()); |
| 19 | float cross2 = p.x() * q.y() - q.x() * p.y(); |
| 20 | |
| 21 | set(tangent.x(), tangent.y(), cross2); |
| 22 | scale(1.0f / tangent.Length()); |
| 23 | } |
| 24 | |
Alhaad Gokhale | 4f51307 | 2015-03-24 10:49:34 -0700 | [diff] [blame] | 25 | gfx::PointF LayerQuad::Edge::Intersect(const LayerQuad::Edge& e) const { |
| 26 | DCHECK(!degenerate()); |
| 27 | DCHECK(!e.degenerate()); |
| 28 | |
| 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())); |
| 31 | } |
| 32 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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()); |
| 39 | |
| 40 | float sign = quad.IsCounterClockwise() ? -1 : 1; |
| 41 | left_.scale(sign); |
| 42 | right_.scale(sign); |
| 43 | top_.scale(sign); |
| 44 | bottom_.scale(sign); |
| 45 | } |
| 46 | |
| 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) {} |
| 55 | |
| 56 | gfx::QuadF LayerQuad::ToQuadF() const { |
Alhaad Gokhale | 4f51307 | 2015-03-24 10:49:34 -0700 | [diff] [blame] | 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(); |
| 61 | } |
| 62 | |
Nick Bray | 0bcbd3b | 2015-03-12 16:29:36 -0700 | [diff] [blame] | 63 | if (left_.degenerate()) { |
| 64 | return gfx::QuadF(top_.Intersect(bottom_), top_.Intersect(right_), |
| 65 | right_.Intersect(bottom_), bottom_.Intersect(top_)); |
| 66 | } |
| 67 | if (right_.degenerate()) { |
| 68 | return gfx::QuadF(left_.Intersect(top_), top_.Intersect(bottom_), |
| 69 | bottom_.Intersect(top_), bottom_.Intersect(left_)); |
| 70 | } |
| 71 | if (top_.degenerate()) { |
| 72 | return gfx::QuadF(left_.Intersect(right_), right_.Intersect(left_), |
| 73 | right_.Intersect(bottom_), bottom_.Intersect(left_)); |
| 74 | } |
| 75 | if (bottom_.degenerate()) { |
| 76 | return gfx::QuadF(left_.Intersect(top_), top_.Intersect(right_), |
| 77 | right_.Intersect(left_), left_.Intersect(right_)); |
| 78 | } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 79 | return gfx::QuadF(left_.Intersect(top_), |
| 80 | top_.Intersect(right_), |
| 81 | right_.Intersect(bottom_), |
| 82 | bottom_.Intersect(left_)); |
| 83 | } |
| 84 | |
| 85 | void LayerQuad::ToFloatArray(float flattened[12]) const { |
Nick Bray | 0bcbd3b | 2015-03-12 16:29:36 -0700 | [diff] [blame] | 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(); |
| 94 | } |
| 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(); |
| 103 | } |
| 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(); |
| 112 | } |
| 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(); |
| 121 | } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | } // namespace cc |