Commit f92aa3f3 authored by kui.zheng@arm.com's avatar kui.zheng@arm.com

Optimize QuadF's IsCounterClockwise function

IsCounterClockwise is a busy function,See performance and discussion:
https://groups.google.com/a/chromium.org/forum/#!topic/graphics-dev/aSvAbzcgbos

This patch reduce IsCounterClockwise() from 8mul+3add+4sub to 4mul+2add+2sub,
and performance doubled in real micro-bench.

R=danakj@chromium.org

Review URL: https://codereview.chromium.org/458113002

Cr-Commit-Position: refs/heads/master@{#289138}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@289138 0039d316-1c4b-4281-b951-d872f2087c98
parent 6f6544eb
...@@ -472,3 +472,4 @@ The MathWorks, Inc. <binod.pant@mathworks.com> ...@@ -472,3 +472,4 @@ The MathWorks, Inc. <binod.pant@mathworks.com>
Torchmobile Inc. Torchmobile Inc.
Venture 3 Systems LLC <*@venture3systems.com> Venture 3 Systems LLC <*@venture3systems.com>
Yandex LLC <*@yandex-team.ru> Yandex LLC <*@yandex-team.ru>
ARM Holdings <*@arm.com>
...@@ -43,19 +43,23 @@ bool QuadF::IsCounterClockwise() const { ...@@ -43,19 +43,23 @@ bool QuadF::IsCounterClockwise() const {
// counter-clockwise. Note carefully: this is backwards from conventional // counter-clockwise. Note carefully: this is backwards from conventional
// math because our geometric space uses screen coordiantes with y-axis // math because our geometric space uses screen coordiantes with y-axis
// pointing downards. // pointing downards.
// Reference: http://mathworld.wolfram.com/PolygonArea.html // Reference: http://mathworld.wolfram.com/PolygonArea.html.
// The equation can be written:
// Signed area = determinant1 + determinant2 + determinant3 + determinant4
// In practise, Refactoring the computation of adding determinants so that
// reducing the number of operations. The equation is:
// Signed area = element1 + element2 - element3 - element4
float p24 = p2_.y() - p4_.y();
float p31 = p3_.y() - p1_.y();
// Up-cast to double so this cannot overflow. // Up-cast to double so this cannot overflow.
double determinant1 = static_cast<double>(p1_.x()) * p2_.y() double element1 = static_cast<double>(p1_.x()) * p24;
- static_cast<double>(p2_.x()) * p1_.y(); double element2 = static_cast<double>(p2_.x()) * p31;
double determinant2 = static_cast<double>(p2_.x()) * p3_.y() double element3 = static_cast<double>(p3_.x()) * p24;
- static_cast<double>(p3_.x()) * p2_.y(); double element4 = static_cast<double>(p4_.x()) * p31;
double determinant3 = static_cast<double>(p3_.x()) * p4_.y()
- static_cast<double>(p4_.x()) * p3_.y(); return element1 + element2 < element3 + element4;
double determinant4 = static_cast<double>(p4_.x()) * p1_.y()
- static_cast<double>(p1_.x()) * p4_.y();
return determinant1 + determinant2 + determinant3 + determinant4 < 0;
} }
static inline bool PointIsInTriangle(const PointF& point, static inline bool PointIsInTriangle(const PointF& point,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment