gfx/skia/trunk/src/utils/SkCullPoints.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * Copyright 2006 The Android Open Source Project
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #include "SkCullPoints.h"
michael@0 9
michael@0 10 static bool cross_product_is_neg(const SkIPoint& v, int dx, int dy) {
michael@0 11 #if 0
michael@0 12 return v.fX * dy - v.fY * dx < 0;
michael@0 13 #else
michael@0 14 return sk_64_mul(v.fX, dy) < sk_64_mul(dx, v.fY);
michael@0 15 #endif
michael@0 16 }
michael@0 17
michael@0 18 bool SkCullPoints::sect_test(int x0, int y0, int x1, int y1) const {
michael@0 19 const SkIRect& r = fR;
michael@0 20
michael@0 21 if ((x0 < r.fLeft && x1 < r.fLeft) ||
michael@0 22 (x0 > r.fRight && x1 > r.fRight) ||
michael@0 23 (y0 < r.fTop && y1 < r.fTop) ||
michael@0 24 (y0 > r.fBottom && y1 > r.fBottom)) {
michael@0 25 return false;
michael@0 26 }
michael@0 27
michael@0 28 // since the crossprod test is a little expensive, check for easy-in cases first
michael@0 29 if (r.contains(x0, y0) || r.contains(x1, y1)) {
michael@0 30 return true;
michael@0 31 }
michael@0 32
michael@0 33 // At this point we're not sure, so we do a crossprod test
michael@0 34 SkIPoint vec;
michael@0 35 const SkIPoint* rAsQuad = fAsQuad;
michael@0 36
michael@0 37 vec.set(x1 - x0, y1 - y0);
michael@0 38 bool isNeg = cross_product_is_neg(vec, x0 - rAsQuad[0].fX, y0 - rAsQuad[0].fY);
michael@0 39 for (int i = 1; i < 4; i++) {
michael@0 40 if (cross_product_is_neg(vec, x0 - rAsQuad[i].fX, y0 - rAsQuad[i].fY) != isNeg) {
michael@0 41 return true;
michael@0 42 }
michael@0 43 }
michael@0 44 return false; // we didn't intersect
michael@0 45 }
michael@0 46
michael@0 47 static void toQuad(const SkIRect& r, SkIPoint quad[4]) {
michael@0 48 SkASSERT(quad);
michael@0 49
michael@0 50 quad[0].set(r.fLeft, r.fTop);
michael@0 51 quad[1].set(r.fRight, r.fTop);
michael@0 52 quad[2].set(r.fRight, r.fBottom);
michael@0 53 quad[3].set(r.fLeft, r.fBottom);
michael@0 54 }
michael@0 55
michael@0 56 SkCullPoints::SkCullPoints() {
michael@0 57 SkIRect r;
michael@0 58 r.setEmpty();
michael@0 59 this->reset(r);
michael@0 60 }
michael@0 61
michael@0 62 SkCullPoints::SkCullPoints(const SkIRect& r) {
michael@0 63 this->reset(r);
michael@0 64 }
michael@0 65
michael@0 66 void SkCullPoints::reset(const SkIRect& r) {
michael@0 67 fR = r;
michael@0 68 toQuad(fR, fAsQuad);
michael@0 69 fPrevPt.set(0, 0);
michael@0 70 fPrevResult = kNo_Result;
michael@0 71 }
michael@0 72
michael@0 73 void SkCullPoints::moveTo(int x, int y) {
michael@0 74 fPrevPt.set(x, y);
michael@0 75 fPrevResult = kNo_Result; // so we trigger a movetolineto later
michael@0 76 }
michael@0 77
michael@0 78 SkCullPoints::LineToResult SkCullPoints::lineTo(int x, int y, SkIPoint line[]) {
michael@0 79 SkASSERT(line != NULL);
michael@0 80
michael@0 81 LineToResult result = kNo_Result;
michael@0 82 int x0 = fPrevPt.fX;
michael@0 83 int y0 = fPrevPt.fY;
michael@0 84
michael@0 85 // need to upgrade sect_test to chop the result
michael@0 86 // and to correctly return kLineTo_Result when the result is connected
michael@0 87 // to the previous call-out
michael@0 88 if (this->sect_test(x0, y0, x, y)) {
michael@0 89 line[0].set(x0, y0);
michael@0 90 line[1].set(x, y);
michael@0 91
michael@0 92 if (fPrevResult != kNo_Result && fPrevPt.equals(x0, y0)) {
michael@0 93 result = kLineTo_Result;
michael@0 94 } else {
michael@0 95 result = kMoveToLineTo_Result;
michael@0 96 }
michael@0 97 }
michael@0 98
michael@0 99 fPrevPt.set(x, y);
michael@0 100 fPrevResult = result;
michael@0 101
michael@0 102 return result;
michael@0 103 }
michael@0 104
michael@0 105 /////////////////////////////////////////////////////////////////////////////////////////////////
michael@0 106
michael@0 107 #include "SkPath.h"
michael@0 108
michael@0 109 SkCullPointsPath::SkCullPointsPath()
michael@0 110 : fCP(), fPath(NULL) {
michael@0 111 }
michael@0 112
michael@0 113 SkCullPointsPath::SkCullPointsPath(const SkIRect& r, SkPath* dst)
michael@0 114 : fCP(r), fPath(dst) {
michael@0 115 }
michael@0 116
michael@0 117 void SkCullPointsPath::reset(const SkIRect& r, SkPath* dst) {
michael@0 118 fCP.reset(r);
michael@0 119 fPath = dst;
michael@0 120 }
michael@0 121
michael@0 122 void SkCullPointsPath::moveTo(int x, int y) {
michael@0 123 fCP.moveTo(x, y);
michael@0 124 }
michael@0 125
michael@0 126 void SkCullPointsPath::lineTo(int x, int y) {
michael@0 127 SkIPoint pts[2];
michael@0 128
michael@0 129 switch (fCP.lineTo(x, y, pts)) {
michael@0 130 case SkCullPoints::kMoveToLineTo_Result:
michael@0 131 fPath->moveTo(SkIntToScalar(pts[0].fX), SkIntToScalar(pts[0].fY));
michael@0 132 // fall through to the lineto case
michael@0 133 case SkCullPoints::kLineTo_Result:
michael@0 134 fPath->lineTo(SkIntToScalar(pts[1].fX), SkIntToScalar(pts[1].fY));
michael@0 135 break;
michael@0 136 default:
michael@0 137 break;
michael@0 138 }
michael@0 139 }
michael@0 140
michael@0 141 ///////////////////////////////////////////////////////////////////////////////
michael@0 142
michael@0 143 #include "SkMatrix.h"
michael@0 144 #include "SkRegion.h"
michael@0 145
michael@0 146 bool SkHitTestPath(const SkPath& path, SkRect& target, bool hires) {
michael@0 147 if (target.isEmpty()) {
michael@0 148 return false;
michael@0 149 }
michael@0 150
michael@0 151 bool isInverse = path.isInverseFillType();
michael@0 152 if (path.isEmpty()) {
michael@0 153 return isInverse;
michael@0 154 }
michael@0 155
michael@0 156 SkRect bounds = path.getBounds();
michael@0 157
michael@0 158 bool sects = SkRect::Intersects(target, bounds);
michael@0 159 if (isInverse) {
michael@0 160 if (!sects) {
michael@0 161 return true;
michael@0 162 }
michael@0 163 } else {
michael@0 164 if (!sects) {
michael@0 165 return false;
michael@0 166 }
michael@0 167 if (target.contains(bounds)) {
michael@0 168 return true;
michael@0 169 }
michael@0 170 }
michael@0 171
michael@0 172 SkPath devPath;
michael@0 173 const SkPath* pathPtr;
michael@0 174 SkRect devTarget;
michael@0 175
michael@0 176 if (hires) {
michael@0 177 const SkScalar coordLimit = SkIntToScalar(16384);
michael@0 178 const SkRect limit = { 0, 0, coordLimit, coordLimit };
michael@0 179
michael@0 180 SkMatrix matrix;
michael@0 181 matrix.setRectToRect(bounds, limit, SkMatrix::kFill_ScaleToFit);
michael@0 182
michael@0 183 path.transform(matrix, &devPath);
michael@0 184 matrix.mapRect(&devTarget, target);
michael@0 185
michael@0 186 pathPtr = &devPath;
michael@0 187 } else {
michael@0 188 devTarget = target;
michael@0 189 pathPtr = &path;
michael@0 190 }
michael@0 191
michael@0 192 SkIRect iTarget;
michael@0 193 devTarget.round(&iTarget);
michael@0 194 if (iTarget.isEmpty()) {
michael@0 195 iTarget.fLeft = SkScalarFloorToInt(devTarget.fLeft);
michael@0 196 iTarget.fTop = SkScalarFloorToInt(devTarget.fTop);
michael@0 197 iTarget.fRight = iTarget.fLeft + 1;
michael@0 198 iTarget.fBottom = iTarget.fTop + 1;
michael@0 199 }
michael@0 200
michael@0 201 SkRegion clip(iTarget);
michael@0 202 SkRegion rgn;
michael@0 203 return rgn.setPath(*pathPtr, clip) ^ isInverse;
michael@0 204 }
michael@0 205
michael@0 206 bool SkHitTestPath(const SkPath& path, SkScalar x, SkScalar y, bool hires) {
michael@0 207 const SkScalar half = SK_ScalarHalf;
michael@0 208 const SkScalar one = SK_Scalar1;
michael@0 209 SkRect r = SkRect::MakeXYWH(x - half, y - half, one, one);
michael@0 210 return SkHitTestPath(path, r, hires);
michael@0 211 }

mercurial