summaryrefslogtreecommitdiffstats
path: root/cc/resources/layer_quad.cc
blob: fdd68662e3526259d4291485ed91ad25cda4b882 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "cc/resources/layer_quad.h"

#include "base/logging.h"
#include "ui/gfx/quad_f.h"

namespace cc {

LayerQuad::Edge::Edge(const gfx::PointF& p, const gfx::PointF& q) {
  DCHECK(p != q);

  gfx::Vector2dF tangent(p.y() - q.y(), q.x() - p.x());
  float cross2 = p.x() * q.y() - q.x() * p.y();

  set(tangent.x(), tangent.y(), cross2);
  scale(1.0f / tangent.Length());
}

LayerQuad::LayerQuad(const gfx::QuadF& quad) {
  // Create edges.
  left_ = Edge(quad.p4(), quad.p1());
  right_ = Edge(quad.p2(), quad.p3());
  top_ = Edge(quad.p1(), quad.p2());
  bottom_ = Edge(quad.p3(), quad.p4());

  float sign = quad.IsCounterClockwise() ? -1 : 1;
  left_.scale(sign);
  right_.scale(sign);
  top_.scale(sign);
  bottom_.scale(sign);
}

LayerQuad::LayerQuad(const Edge& left,
                     const Edge& top,
                     const Edge& right,
                     const Edge& bottom)
    : left_(left),
      top_(top),
      right_(right),
      bottom_(bottom) {}

gfx::QuadF LayerQuad::ToQuadF() const {
  return gfx::QuadF(left_.Intersect(top_),
                    top_.Intersect(right_),
                    right_.Intersect(bottom_),
                    bottom_.Intersect(left_));
}

void LayerQuad::ToFloatArray(float flattened[12]) const {
  flattened[0] = left_.x();
  flattened[1] = left_.y();
  flattened[2] = left_.z();
  flattened[3] = top_.x();
  flattened[4] = top_.y();
  flattened[5] = top_.z();
  flattened[6] = right_.x();
  flattened[7] = right_.y();
  flattened[8] = right_.z();
  flattened[9] = bottom_.x();
  flattened[10] = bottom_.y();
  flattened[11] = bottom_.z();
}

}  // namespace cc