Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: cc/quads/draw_polygon_unittest.cc

Issue 1497153002: Replace inverse transform with Cross-product computation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « cc/quads/draw_polygon.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <limits> 5 #include <limits>
6 #include <vector> 6 #include <vector>
7 7
8 #include "cc/output/bsp_compare_result.h" 8 #include "cc/output/bsp_compare_result.h"
9 #include "cc/quads/draw_polygon.h" 9 #include "cc/quads/draw_polygon.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/gfx/transform.h" 11 #include "ui/gfx/transform.h"
12 12
13 namespace cc { 13 namespace cc {
14
15 void DrawPolygon::RecomputeNormalForTesting() {
16 ConstructNormal();
17 }
18
14 namespace { 19 namespace {
15 20
16 #define CREATE_NEW_DRAW_POLYGON(name, points_vector, normal, polygon_id) \ 21 #define CREATE_NEW_DRAW_POLYGON(name, points_vector, normal, polygon_id) \
17 DrawPolygon name(NULL, points_vector, normal, polygon_id) 22 DrawPolygon name(NULL, points_vector, normal, polygon_id)
18 23
24 #define CREATE_TEST_DRAW_POLYGON(name, points_vector, polygon_id) \
25 DrawPolygon name(NULL, points_vector, gfx::Vector3dF(1, 2, 3), polygon_id); \
26 name.RecomputeNormalForTesting()
27
19 #define EXPECT_FLOAT_WITHIN_EPSILON_OF(a, b) \ 28 #define EXPECT_FLOAT_WITHIN_EPSILON_OF(a, b) \
20 EXPECT_TRUE(std::abs(a - b) < std::numeric_limits<float>::epsilon()); 29 EXPECT_TRUE(std::abs(a - b) < std::numeric_limits<float>::epsilon());
21 30
22 #define EXPECT_POINT_EQ(point_a, point_b) \ 31 #define EXPECT_POINT_EQ(point_a, point_b) \
23 EXPECT_FLOAT_EQ(point_a.x(), point_b.x()); \ 32 EXPECT_FLOAT_EQ(point_a.x(), point_b.x()); \
24 EXPECT_FLOAT_EQ(point_a.y(), point_b.y()); \ 33 EXPECT_FLOAT_EQ(point_a.y(), point_b.y()); \
25 EXPECT_FLOAT_EQ(point_a.z(), point_b.z()); 34 EXPECT_FLOAT_EQ(point_a.z(), point_b.z());
26 35
36 #define EXPECT_NORMAL(poly, n_x, n_y, n_z) \
37 EXPECT_FLOAT_WITHIN_EPSILON_OF(poly.normal().x(), n_x); \
38 EXPECT_FLOAT_WITHIN_EPSILON_OF(poly.normal().y(), n_y); \
39 EXPECT_FLOAT_WITHIN_EPSILON_OF(poly.normal().z(), n_z);
40
27 static void ValidatePoints(const DrawPolygon& polygon, 41 static void ValidatePoints(const DrawPolygon& polygon,
28 const std::vector<gfx::Point3F>& points) { 42 const std::vector<gfx::Point3F>& points) {
29 EXPECT_EQ(polygon.points().size(), points.size()); 43 EXPECT_EQ(polygon.points().size(), points.size());
30 for (size_t i = 0; i < points.size(); i++) { 44 for (size_t i = 0; i < points.size(); i++) {
31 EXPECT_POINT_EQ(polygon.points()[i], points[i]); 45 EXPECT_POINT_EQ(polygon.points()[i], points[i]);
32 } 46 }
33 } 47 }
34 48
49 // A simple square in a plane.
50 TEST(DrawPolygonConstructionTest, NormalNormal) {
51 std::vector<gfx::Point3F> vertices;
52 vertices.push_back(gfx::Point3F(0.0f, 10.0f, 0.0f));
53 vertices.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f));
54 vertices.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f));
55 vertices.push_back(gfx::Point3F(10.0f, 10.0f, 0.0f));
56
57 gfx::Transform Identity;
58 DrawPolygon polygon(NULL, gfx::RectF(10.0, 10.0), Identity, 1);
59 EXPECT_NORMAL(polygon, 0.0, 0.0, 1.0);
60 }
61
62 // More complicated shapes.
63 TEST(DrawPolygonConstructionTest, TestNormal) {
64 std::vector<gfx::Point3F> vertices;
65 vertices.push_back(gfx::Point3F(0.0f, 10.0f, 0.0f));
66 vertices.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f));
67 vertices.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f));
68 vertices.push_back(gfx::Point3F(10.0f, 10.0f, 0.0f));
69
70 CREATE_TEST_DRAW_POLYGON(polygon, vertices, 1);
71 EXPECT_NORMAL(polygon, 0.0, 0.0, 1.0);
72 }
73
74 TEST(DrawPolygonConstructionTest, ClippedNormal) {
75 std::vector<gfx::Point3F> vertices;
76 vertices.push_back(gfx::Point3F(0.1f, 10.0f, 0.0f));
77 vertices.push_back(gfx::Point3F(0.0f, 9.9f, 0.0f));
78 vertices.push_back(gfx::Point3F(0.0f, 10.0f, 0.0f));
79 vertices.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f));
80 vertices.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f));
81 vertices.push_back(gfx::Point3F(10.0f, 10.0f, 0.0f));
82
83 CREATE_TEST_DRAW_POLYGON(polygon, vertices, 1);
84 EXPECT_NORMAL(polygon, 0.0, 0.0, 1.0);
85 }
86
87 TEST(DrawPolygonConstructionTest, SlimTriangleNormal) {
88 std::vector<gfx::Point3F> vertices;
89 vertices.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f));
90 vertices.push_back(gfx::Point3F(5000.0f, 0.0f, 0.0f));
91 vertices.push_back(gfx::Point3F(10000.0f, 1.0f, 0.0f));
92
93 CREATE_TEST_DRAW_POLYGON(polygon, vertices, 2);
94 EXPECT_NORMAL(polygon, 0.0, 0.0, 1.0);
Peter Mayo 2015/12/04 04:33:56 Perhaps one that isn't axis-aligned?
Peter Mayo 2015/12/04 05:21:34 Added in the transform test.
95 }
96
97 TEST(DrawPolygonConstructionTest, ManyVertexNormal) {
98 std::vector<gfx::Point3F> vertices_c;
99 std::vector<gfx::Point3F> vertices_d;
100 for (int i = 0; i < 100; i++) {
101 vertices_c.push_back(
102 gfx::Point3F(cos(i * M_PI / 50), sin(i * M_PI / 50), 0.0f));
103 vertices_d.push_back(gfx::Point3F(cos(i * M_PI / 50) + 99.0,
104 sin(i * M_PI / 50) + 99.0, 100.0f));
105 }
106 CREATE_TEST_DRAW_POLYGON(polygon_c, vertices_c, 3);
107 EXPECT_NORMAL(polygon_c, 0.0, 0.0, 1.0);
108
109 CREATE_TEST_DRAW_POLYGON(polygon_d, vertices_d, 4);
110 EXPECT_NORMAL(polygon_c, 0.0, 0.0, 1.0);
111 }
112
113 // A simple rect being transformed.
114 TEST(DrawPolygonConstructionTest, DizzyNormal) {
115 gfx::RectF src(-0.1, -10.0, 0.2, 20.0);
116
117 gfx::Transform transform_i(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
118 DrawPolygon polygon_i(NULL, src, transform_i, 1);
119
120 EXPECT_NORMAL(polygon_i, 0.0, 0.0, 1.0);
121
122 gfx::Transform tranform_a(0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
123 DrawPolygon polygon_a(NULL, src, tranform_a, 2);
124 EXPECT_NORMAL(polygon_a, 0.0, 0.0, -1.0);
125
126 gfx::Transform tranform_b(0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1);
127 DrawPolygon polygon_b(NULL, src, tranform_b, 3);
128 EXPECT_NORMAL(polygon_b, -1.0, 0.0, 0.0);
129
130 gfx::Transform tranform_c(1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1);
131 DrawPolygon polygon_c(NULL, src, tranform_c, 4);
132 EXPECT_NORMAL(polygon_c, 0.0, -1.0, 0.0);
133
134 gfx::Transform tranform_d(-1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
135 DrawPolygon polygon_d(NULL, src, tranform_d, 5);
136 EXPECT_NORMAL(polygon_d, 0.0, 0.0, -1.0);
137 }
138
35 // Two quads are nearly touching but definitely ordered. Second one should 139 // Two quads are nearly touching but definitely ordered. Second one should
36 // compare in front. 140 // compare in front.
37 TEST(DrawPolygonSplitTest, NearlyTouchingOrder) { 141 TEST(DrawPolygonSplitTest, NearlyTouchingOrder) {
38 std::vector<gfx::Point3F> vertices_a; 142 std::vector<gfx::Point3F> vertices_a;
39 vertices_a.push_back(gfx::Point3F(0.0f, 10.0f, 0.0f)); 143 vertices_a.push_back(gfx::Point3F(0.0f, 10.0f, 0.0f));
40 vertices_a.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f)); 144 vertices_a.push_back(gfx::Point3F(0.0f, 0.0f, 0.0f));
41 vertices_a.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f)); 145 vertices_a.push_back(gfx::Point3F(10.0f, 0.0f, 0.0f));
42 vertices_a.push_back(gfx::Point3F(10.0f, 10.0f, 0.0f)); 146 vertices_a.push_back(gfx::Point3F(10.0f, 10.0f, 0.0f));
43 std::vector<gfx::Point3F> vertices_b; 147 std::vector<gfx::Point3F> vertices_b;
44 vertices_b.push_back(gfx::Point3F(0.0f, 10.0f, -1.0f)); 148 vertices_b.push_back(gfx::Point3F(0.0f, 10.0f, -1.0f));
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 transform.RotateAboutYAxis(45.0); 325 transform.RotateAboutYAxis(45.0);
222 // This would transform the vertices as well, but we are transforming a 326 // This would transform the vertices as well, but we are transforming a
223 // DrawPolygon with 0 vertices just to make sure our normal transformation 327 // DrawPolygon with 0 vertices just to make sure our normal transformation
224 // using the inverse tranpose matrix gives us the right result. 328 // using the inverse tranpose matrix gives us the right result.
225 polygon_a.TransformToScreenSpace(transform); 329 polygon_a.TransformToScreenSpace(transform);
226 330
227 // Note: We use EXPECT_FLOAT_WITHIN_EPSILON instead of EXPECT_FLOAT_EQUAL here 331 // Note: We use EXPECT_FLOAT_WITHIN_EPSILON instead of EXPECT_FLOAT_EQUAL here
228 // because some architectures (e.g., Arm64) employ a fused multiply-add 332 // because some architectures (e.g., Arm64) employ a fused multiply-add
229 // instruction which causes rounding asymmetry and reduces precision. 333 // instruction which causes rounding asymmetry and reduces precision.
230 // http://crbug.com/401117. 334 // http://crbug.com/401117.
231 EXPECT_FLOAT_WITHIN_EPSILON_OF(polygon_a.normal().x(), 0); 335 EXPECT_NORMAL(polygon_a, 0.0, 0.0, -1.0);
232 EXPECT_FLOAT_WITHIN_EPSILON_OF(polygon_a.normal().y(), 0);
233 EXPECT_FLOAT_WITHIN_EPSILON_OF(polygon_a.normal().z(), -1);
234 } 336 }
235 337
236 } // namespace 338 } // namespace
237 } // namespace cc 339 } // namespace cc
OLDNEW
« no previous file with comments | « cc/quads/draw_polygon.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698