gfx/skia/trunk/src/core/SkEdgeBuilder.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1
michael@0 2 /*
michael@0 3 * Copyright 2011 Google Inc.
michael@0 4 *
michael@0 5 * Use of this source code is governed by a BSD-style license that can be
michael@0 6 * found in the LICENSE file.
michael@0 7 */
michael@0 8 #include "SkEdgeBuilder.h"
michael@0 9 #include "SkPath.h"
michael@0 10 #include "SkEdge.h"
michael@0 11 #include "SkEdgeClipper.h"
michael@0 12 #include "SkLineClipper.h"
michael@0 13 #include "SkGeometry.h"
michael@0 14
michael@0 15 template <typename T> static T* typedAllocThrow(SkChunkAlloc& alloc) {
michael@0 16 return static_cast<T*>(alloc.allocThrow(sizeof(T)));
michael@0 17 }
michael@0 18
michael@0 19 ///////////////////////////////////////////////////////////////////////////////
michael@0 20
michael@0 21 SkEdgeBuilder::SkEdgeBuilder() : fAlloc(16*1024) {
michael@0 22 fEdgeList = NULL;
michael@0 23 }
michael@0 24
michael@0 25 void SkEdgeBuilder::addLine(const SkPoint pts[]) {
michael@0 26 SkEdge* edge = typedAllocThrow<SkEdge>(fAlloc);
michael@0 27 if (edge->setLine(pts[0], pts[1], fShiftUp)) {
michael@0 28 fList.push(edge);
michael@0 29 } else {
michael@0 30 // TODO: unallocate edge from storage...
michael@0 31 }
michael@0 32 }
michael@0 33
michael@0 34 void SkEdgeBuilder::addQuad(const SkPoint pts[]) {
michael@0 35 SkQuadraticEdge* edge = typedAllocThrow<SkQuadraticEdge>(fAlloc);
michael@0 36 if (edge->setQuadratic(pts, fShiftUp)) {
michael@0 37 fList.push(edge);
michael@0 38 } else {
michael@0 39 // TODO: unallocate edge from storage...
michael@0 40 }
michael@0 41 }
michael@0 42
michael@0 43 void SkEdgeBuilder::addCubic(const SkPoint pts[]) {
michael@0 44 SkCubicEdge* edge = typedAllocThrow<SkCubicEdge>(fAlloc);
michael@0 45 if (edge->setCubic(pts, NULL, fShiftUp)) {
michael@0 46 fList.push(edge);
michael@0 47 } else {
michael@0 48 // TODO: unallocate edge from storage...
michael@0 49 }
michael@0 50 }
michael@0 51
michael@0 52 void SkEdgeBuilder::addClipper(SkEdgeClipper* clipper) {
michael@0 53 SkPoint pts[4];
michael@0 54 SkPath::Verb verb;
michael@0 55
michael@0 56 while ((verb = clipper->next(pts)) != SkPath::kDone_Verb) {
michael@0 57 switch (verb) {
michael@0 58 case SkPath::kLine_Verb:
michael@0 59 this->addLine(pts);
michael@0 60 break;
michael@0 61 case SkPath::kQuad_Verb:
michael@0 62 this->addQuad(pts);
michael@0 63 break;
michael@0 64 case SkPath::kCubic_Verb:
michael@0 65 this->addCubic(pts);
michael@0 66 break;
michael@0 67 default:
michael@0 68 break;
michael@0 69 }
michael@0 70 }
michael@0 71 }
michael@0 72
michael@0 73 ///////////////////////////////////////////////////////////////////////////////
michael@0 74
michael@0 75 static void setShiftedClip(SkRect* dst, const SkIRect& src, int shift) {
michael@0 76 dst->set(SkIntToScalar(src.fLeft >> shift),
michael@0 77 SkIntToScalar(src.fTop >> shift),
michael@0 78 SkIntToScalar(src.fRight >> shift),
michael@0 79 SkIntToScalar(src.fBottom >> shift));
michael@0 80 }
michael@0 81
michael@0 82 int SkEdgeBuilder::buildPoly(const SkPath& path, const SkIRect* iclip,
michael@0 83 int shiftUp) {
michael@0 84 SkPath::Iter iter(path, true);
michael@0 85 SkPoint pts[4];
michael@0 86 SkPath::Verb verb;
michael@0 87
michael@0 88 int maxEdgeCount = path.countPoints();
michael@0 89 if (iclip) {
michael@0 90 // clipping can turn 1 line into (up to) kMaxClippedLineSegments, since
michael@0 91 // we turn portions that are clipped out on the left/right into vertical
michael@0 92 // segments.
michael@0 93 maxEdgeCount *= SkLineClipper::kMaxClippedLineSegments;
michael@0 94 }
michael@0 95 size_t maxEdgeSize = maxEdgeCount * sizeof(SkEdge);
michael@0 96 size_t maxEdgePtrSize = maxEdgeCount * sizeof(SkEdge*);
michael@0 97
michael@0 98 // lets store the edges and their pointers in the same block
michael@0 99 char* storage = (char*)fAlloc.allocThrow(maxEdgeSize + maxEdgePtrSize);
michael@0 100 SkEdge* edge = reinterpret_cast<SkEdge*>(storage);
michael@0 101 SkEdge** edgePtr = reinterpret_cast<SkEdge**>(storage + maxEdgeSize);
michael@0 102 // Record the beginning of our pointers, so we can return them to the caller
michael@0 103 fEdgeList = edgePtr;
michael@0 104
michael@0 105 if (iclip) {
michael@0 106 SkRect clip;
michael@0 107 setShiftedClip(&clip, *iclip, shiftUp);
michael@0 108
michael@0 109 while ((verb = iter.next(pts, false)) != SkPath::kDone_Verb) {
michael@0 110 switch (verb) {
michael@0 111 case SkPath::kMove_Verb:
michael@0 112 case SkPath::kClose_Verb:
michael@0 113 // we ignore these, and just get the whole segment from
michael@0 114 // the corresponding line/quad/cubic verbs
michael@0 115 break;
michael@0 116 case SkPath::kLine_Verb: {
michael@0 117 SkPoint lines[SkLineClipper::kMaxPoints];
michael@0 118 int lineCount = SkLineClipper::ClipLine(pts, clip, lines);
michael@0 119 SkASSERT(lineCount <= SkLineClipper::kMaxClippedLineSegments);
michael@0 120 for (int i = 0; i < lineCount; i++) {
michael@0 121 if (edge->setLine(lines[i], lines[i + 1], shiftUp)) {
michael@0 122 *edgePtr++ = edge++;
michael@0 123 }
michael@0 124 }
michael@0 125 break;
michael@0 126 }
michael@0 127 default:
michael@0 128 SkDEBUGFAIL("unexpected verb");
michael@0 129 break;
michael@0 130 }
michael@0 131 }
michael@0 132 } else {
michael@0 133 while ((verb = iter.next(pts, false)) != SkPath::kDone_Verb) {
michael@0 134 switch (verb) {
michael@0 135 case SkPath::kMove_Verb:
michael@0 136 case SkPath::kClose_Verb:
michael@0 137 // we ignore these, and just get the whole segment from
michael@0 138 // the corresponding line/quad/cubic verbs
michael@0 139 break;
michael@0 140 case SkPath::kLine_Verb:
michael@0 141 if (edge->setLine(pts[0], pts[1], shiftUp)) {
michael@0 142 *edgePtr++ = edge++;
michael@0 143 }
michael@0 144 break;
michael@0 145 default:
michael@0 146 SkDEBUGFAIL("unexpected verb");
michael@0 147 break;
michael@0 148 }
michael@0 149 }
michael@0 150 }
michael@0 151 SkASSERT((char*)edge <= (char*)fEdgeList);
michael@0 152 SkASSERT(edgePtr - fEdgeList <= maxEdgeCount);
michael@0 153 return SkToInt(edgePtr - fEdgeList);
michael@0 154 }
michael@0 155
michael@0 156 static void handle_quad(SkEdgeBuilder* builder, const SkPoint pts[3]) {
michael@0 157 SkPoint monoX[5];
michael@0 158 int n = SkChopQuadAtYExtrema(pts, monoX);
michael@0 159 for (int i = 0; i <= n; i++) {
michael@0 160 builder->addQuad(&monoX[i * 2]);
michael@0 161 }
michael@0 162 }
michael@0 163
michael@0 164 int SkEdgeBuilder::build(const SkPath& path, const SkIRect* iclip,
michael@0 165 int shiftUp) {
michael@0 166 fAlloc.reset();
michael@0 167 fList.reset();
michael@0 168 fShiftUp = shiftUp;
michael@0 169
michael@0 170 SkScalar conicTol = SK_ScalarHalf * (1 << shiftUp);
michael@0 171
michael@0 172 if (SkPath::kLine_SegmentMask == path.getSegmentMasks()) {
michael@0 173 return this->buildPoly(path, iclip, shiftUp);
michael@0 174 }
michael@0 175
michael@0 176 SkPath::Iter iter(path, true);
michael@0 177 SkPoint pts[4];
michael@0 178 SkPath::Verb verb;
michael@0 179
michael@0 180 if (iclip) {
michael@0 181 SkRect clip;
michael@0 182 setShiftedClip(&clip, *iclip, shiftUp);
michael@0 183 SkEdgeClipper clipper;
michael@0 184
michael@0 185 while ((verb = iter.next(pts, false)) != SkPath::kDone_Verb) {
michael@0 186 switch (verb) {
michael@0 187 case SkPath::kMove_Verb:
michael@0 188 case SkPath::kClose_Verb:
michael@0 189 // we ignore these, and just get the whole segment from
michael@0 190 // the corresponding line/quad/cubic verbs
michael@0 191 break;
michael@0 192 case SkPath::kLine_Verb: {
michael@0 193 SkPoint lines[SkLineClipper::kMaxPoints];
michael@0 194 int lineCount = SkLineClipper::ClipLine(pts, clip, lines);
michael@0 195 for (int i = 0; i < lineCount; i++) {
michael@0 196 this->addLine(&lines[i]);
michael@0 197 }
michael@0 198 break;
michael@0 199 }
michael@0 200 case SkPath::kQuad_Verb:
michael@0 201 if (clipper.clipQuad(pts, clip)) {
michael@0 202 this->addClipper(&clipper);
michael@0 203 }
michael@0 204 break;
michael@0 205 case SkPath::kConic_Verb: {
michael@0 206 const int MAX_POW2 = 4;
michael@0 207 const int MAX_QUADS = 1 << MAX_POW2;
michael@0 208 const int MAX_QUAD_PTS = 1 + 2 * MAX_QUADS;
michael@0 209 SkPoint storage[MAX_QUAD_PTS];
michael@0 210
michael@0 211 SkConic conic;
michael@0 212 conic.set(pts, iter.conicWeight());
michael@0 213 int pow2 = conic.computeQuadPOW2(conicTol);
michael@0 214 pow2 = SkMin32(pow2, MAX_POW2);
michael@0 215 int quadCount = conic.chopIntoQuadsPOW2(storage, pow2);
michael@0 216 SkASSERT(quadCount <= MAX_QUADS);
michael@0 217 for (int i = 0; i < quadCount; ++i) {
michael@0 218 if (clipper.clipQuad(&storage[i * 2], clip)) {
michael@0 219 this->addClipper(&clipper);
michael@0 220 }
michael@0 221 }
michael@0 222 } break;
michael@0 223 case SkPath::kCubic_Verb:
michael@0 224 if (clipper.clipCubic(pts, clip)) {
michael@0 225 this->addClipper(&clipper);
michael@0 226 }
michael@0 227 break;
michael@0 228 default:
michael@0 229 SkDEBUGFAIL("unexpected verb");
michael@0 230 break;
michael@0 231 }
michael@0 232 }
michael@0 233 } else {
michael@0 234 while ((verb = iter.next(pts, false)) != SkPath::kDone_Verb) {
michael@0 235 switch (verb) {
michael@0 236 case SkPath::kMove_Verb:
michael@0 237 case SkPath::kClose_Verb:
michael@0 238 // we ignore these, and just get the whole segment from
michael@0 239 // the corresponding line/quad/cubic verbs
michael@0 240 break;
michael@0 241 case SkPath::kLine_Verb:
michael@0 242 this->addLine(pts);
michael@0 243 break;
michael@0 244 case SkPath::kQuad_Verb: {
michael@0 245 handle_quad(this, pts);
michael@0 246 break;
michael@0 247 }
michael@0 248 case SkPath::kConic_Verb: {
michael@0 249 const int MAX_POW2 = 4;
michael@0 250 const int MAX_QUADS = 1 << MAX_POW2;
michael@0 251 const int MAX_QUAD_PTS = 1 + 2 * MAX_QUADS;
michael@0 252 SkPoint storage[MAX_QUAD_PTS];
michael@0 253
michael@0 254 SkConic conic;
michael@0 255 conic.set(pts, iter.conicWeight());
michael@0 256 int pow2 = conic.computeQuadPOW2(conicTol);
michael@0 257 pow2 = SkMin32(pow2, MAX_POW2);
michael@0 258 int quadCount = conic.chopIntoQuadsPOW2(storage, pow2);
michael@0 259 SkASSERT(quadCount <= MAX_QUADS);
michael@0 260 for (int i = 0; i < quadCount; ++i) {
michael@0 261 handle_quad(this, &storage[i * 2]);
michael@0 262 }
michael@0 263 } break;
michael@0 264 case SkPath::kCubic_Verb: {
michael@0 265 SkPoint monoY[10];
michael@0 266 int n = SkChopCubicAtYExtrema(pts, monoY);
michael@0 267 for (int i = 0; i <= n; i++) {
michael@0 268 this->addCubic(&monoY[i * 3]);
michael@0 269 }
michael@0 270 break;
michael@0 271 }
michael@0 272 default:
michael@0 273 SkDEBUGFAIL("unexpected verb");
michael@0 274 break;
michael@0 275 }
michael@0 276 }
michael@0 277 }
michael@0 278 fEdgeList = fList.begin();
michael@0 279 return fList.count();
michael@0 280 }

mercurial