| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "GrPictureUtils.h" | 8 #include "GrPictureUtils.h" |
| 9 | 9 |
| 10 #include "SkPaintPriv.h" | 10 #include "SkPaintPriv.h" |
| 11 #include "SkPatchUtils.h" | 11 #include "SkPatchUtils.h" |
| 12 #include "SkRecord.h" | 12 #include "SkRecord.h" |
| 13 #include "SkRecordDraw.h" |
| 13 #include "SkRecords.h" | 14 #include "SkRecords.h" |
| 14 | 15 |
| 15 SkPicture::AccelData::Key GrAccelData::ComputeAccelDataKey() { | 16 SkPicture::AccelData::Key GrAccelData::ComputeAccelDataKey() { |
| 16 static const SkPicture::AccelData::Key gGPUID = SkPicture::AccelData::Genera
teDomain(); | 17 static const SkPicture::AccelData::Key gGPUID = SkPicture::AccelData::Genera
teDomain(); |
| 17 | 18 |
| 18 return gGPUID; | 19 return gGPUID; |
| 19 } | 20 } |
| 20 | 21 |
| 21 // This looks silly, I know. Why not just use SkRect::MakeLargest()? | |
| 22 // In practice, this is well large enough, and it has a few extra advantages: | |
| 23 // it fits in an SkIRect, and we can munge it a little in both SkRect and | |
| 24 // SKIRect space without worrying about overflow. | |
| 25 static const SkRect kUnbounded = { -2e9f, -2e9f, 2e9f, 2e9f }; | |
| 26 | |
| 27 namespace SkRecords { | 22 namespace SkRecords { |
| 28 | 23 |
| 29 // SkRecord visitor to gather saveLayer/restore information. | 24 // SkRecord visitor to gather saveLayer/restore information. |
| 30 class CollectLayers : SkNoncopyable { | 25 class CollectLayers : SkNoncopyable { |
| 31 public: | 26 public: |
| 32 CollectLayers(const SkPicture* pict, GrAccelData* accelData) | 27 CollectLayers(const SkPicture* pict, SkBBoxHierarchy* bbh, GrAccelData* acce
lData) |
| 33 : fPictureID(pict->uniqueID()) | 28 : fPictureID(pict->uniqueID()) |
| 34 , fSaveLayersInStack(0) | 29 , fSaveLayersInStack(0) |
| 35 , fAccelData(accelData) { | 30 , fAccelData(accelData) |
| 31 , fFillBounds(pict->cullRect(), *pict->fRecord, bbh) { |
| 32 } |
| 36 | 33 |
| 37 // Calculate bounds for all ops. This won't go quite in order, so we'll
need | 34 void setCurrentOp(unsigned currentOp) { fFillBounds.setCurrentOp(currentOp);
} |
| 38 // to store the bounds separately then feed them in to the BBH later in
order. | 35 void cleanUp() { |
| 39 fCTM = &SkMatrix::I(); | 36 fFillBounds.cleanUp(); |
| 40 fCurrentClipBounds = kUnbounded; | |
| 41 | 37 |
| 42 if (NULL == pict->fRecord.get()) { | |
| 43 return; | |
| 44 } | |
| 45 | |
| 46 fBounds.reset(pict->fRecord->count()); | |
| 47 | |
| 48 for (fCurrentOp = 0; fCurrentOp < pict->fRecord->count(); ++fCurrentOp)
{ | |
| 49 pict->fRecord->visit<void>(fCurrentOp, *this); | |
| 50 } | |
| 51 | |
| 52 // If we have any lingering unpaired Saves, simulate restores to make | |
| 53 // sure all ops in those Save blocks have their bounds calculated. | |
| 54 while (!fSaveStack.isEmpty()) { | |
| 55 this->popSaveBlock(); | |
| 56 } | |
| 57 | |
| 58 // Any control ops not part of any Save/Restore block draw everywhere. | |
| 59 while (!fControlIndices.isEmpty()) { | |
| 60 this->popControl(kUnbounded); | |
| 61 } | |
| 62 | |
| 63 //--------- LAYER HOISTING | |
| 64 while (!fSaveLayerStack.isEmpty()) { | 38 while (!fSaveLayerStack.isEmpty()) { |
| 65 this->popSaveLayerInfo(); | 39 this->popSaveLayerInfo(); |
| 66 } | 40 } |
| 67 //--------- LAYER HOISTING | |
| 68 } | 41 } |
| 69 | 42 |
| 70 template <typename T> void operator()(const T& op) { | 43 template <typename T> void operator()(const T& op) { |
| 71 this->updateCTM(op); | 44 fFillBounds(op); |
| 72 this->updateClipBounds(op); | |
| 73 this->trackBounds(op); | |
| 74 //--------- LAYER HOISTING | |
| 75 this->trackSaveLayers(op); | 45 this->trackSaveLayers(op); |
| 76 //--------- LAYER HOISTING | |
| 77 } | 46 } |
| 78 | 47 |
| 79 private: | 48 private: |
| 80 // In this file, SkRect are in local coordinates, Bounds are translated back
to identity space. | |
| 81 typedef SkRect Bounds; | |
| 82 | |
| 83 struct SaveBounds { | |
| 84 int controlOps; // Number of control ops in this Save block, incl
uding the Save. | |
| 85 Bounds bounds; // Bounds of everything in the block. | |
| 86 const SkPaint* paint; // Unowned. If set, adjusts the bounds of all op
s in this block. | |
| 87 }; | |
| 88 | |
| 89 //--------- LAYER HOISTING | |
| 90 class SaveLayerInfo { | 49 class SaveLayerInfo { |
| 91 public: | 50 public: |
| 92 SaveLayerInfo() { } | 51 SaveLayerInfo() { } |
| 93 SaveLayerInfo(int opIndex, bool isSaveLayer, const SkPaint* paint, const
Bounds& clipBound) | 52 SaveLayerInfo(int opIndex, bool isSaveLayer, const SkPaint* paint, const
FillBounds::Bounds& clipBound) |
| 94 : fStartIndex(opIndex) | 53 : fStartIndex(opIndex) |
| 95 , fIsSaveLayer(isSaveLayer) | 54 , fIsSaveLayer(isSaveLayer) |
| 96 , fHasNestedSaveLayer(false) | 55 , fHasNestedSaveLayer(false) |
| 97 , fPaint(paint) | 56 , fPaint(paint) |
| 98 , fClipBound(clipBound) { | 57 , fClipBound(clipBound) { |
| 99 } | 58 } |
| 100 | 59 |
| 101 int fStartIndex; | 60 int fStartIndex; |
| 102 bool fIsSaveLayer; | 61 bool fIsSaveLayer; |
| 103 bool fHasNestedSaveLayer; | 62 bool fHasNestedSaveLayer; |
| 104 const SkPaint* fPaint; | 63 const SkPaint* fPaint; |
| 105 Bounds fClipBound; | 64 FillBounds::Bounds fClipBound; |
| 106 }; | 65 }; |
| 107 //--------- LAYER HOISTING | |
| 108 | 66 |
| 109 // Only Restore and SetMatrix change the CTM. | |
| 110 template <typename T> void updateCTM(const T&) {} | |
| 111 void updateCTM(const Restore& op) { fCTM = &op.matrix; } | |
| 112 void updateCTM(const SetMatrix& op) { fCTM = &op.matrix; } | |
| 113 | |
| 114 // Most ops don't change the clip. | |
| 115 template <typename T> void updateClipBounds(const T&) {} | |
| 116 | |
| 117 // Clip{Path,RRect,Rect,Region} obviously change the clip. They all know th
eir bounds already. | |
| 118 void updateClipBounds(const ClipPath& op) { this->updateClipBoundsForClipO
p(op.devBounds); } | |
| 119 void updateClipBounds(const ClipRRect& op) { this->updateClipBoundsForClipO
p(op.devBounds); } | |
| 120 void updateClipBounds(const ClipRect& op) { this->updateClipBoundsForClipO
p(op.devBounds); } | |
| 121 void updateClipBounds(const ClipRegion& op) { this->updateClipBoundsForClipO
p(op.devBounds); } | |
| 122 | |
| 123 // The bounds of clip ops need to be adjusted for the paints of saveLayers t
hey're inside. | |
| 124 void updateClipBoundsForClipOp(const SkIRect& devBounds) { | |
| 125 Bounds clip = SkRect::Make(devBounds); | |
| 126 // We don't call adjustAndMap() because as its last step it would inters
ect the adjusted | |
| 127 // clip bounds with the previous clip, exactly what we can't do when the
clip grows. | |
| 128 fCurrentClipBounds = this->adjustForSaveLayerPaints(&clip) ? clip : kUnb
ounded; | |
| 129 } | |
| 130 | |
| 131 // Restore holds the devBounds for the clip after the {save,saveLayer}/resto
re block completes. | |
| 132 void updateClipBounds(const Restore& op) { | |
| 133 // This is just like the clip ops above, but we need to skip the effects
(if any) of our | |
| 134 // paired saveLayer (if it is one); it has not yet been popped off the s
ave stack. Our | |
| 135 // devBounds reflect the state of the world after the saveLayer/restore
block is done, | |
| 136 // so they are not affected by the saveLayer's paint. | |
| 137 const int kSavesToIgnore = 1; | |
| 138 Bounds clip = SkRect::Make(op.devBounds); | |
| 139 fCurrentClipBounds = | |
| 140 this->adjustForSaveLayerPaints(&clip, kSavesToIgnore) ? clip : kUnbo
unded; | |
| 141 } | |
| 142 | |
| 143 // We also take advantage of SaveLayer bounds when present to further cut th
e clip down. | |
| 144 void updateClipBounds(const SaveLayer& op) { | |
| 145 if (op.bounds) { | |
| 146 // adjustAndMap() intersects these layer bounds with the previous cl
ip for us. | |
| 147 fCurrentClipBounds = this->adjustAndMap(*op.bounds, op.paint); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 // The bounds of these ops must be calculated when we hit the Restore | |
| 152 // from the bounds of the ops in the same Save block. | |
| 153 void trackBounds(const Save&) { this->pushSaveBlock(NULL); } | |
| 154 void trackBounds(const SaveLayer& op) { this->pushSaveBlock(op.paint); } | |
| 155 void trackBounds(const Restore&) { fBounds[fCurrentOp] = this->popSaveBlock(
); } | |
| 156 | |
| 157 void trackBounds(const SetMatrix&) { this->pushControl(); } | |
| 158 void trackBounds(const ClipRect&) { this->pushControl(); } | |
| 159 void trackBounds(const ClipRRect&) { this->pushControl(); } | |
| 160 void trackBounds(const ClipPath&) { this->pushControl(); } | |
| 161 void trackBounds(const ClipRegion&) { this->pushControl(); } | |
| 162 void trackBounds(const PushCull&) { this->pushControl(); } | |
| 163 void trackBounds(const PopCull&) { this->pushControl(); } | |
| 164 void trackBounds(const BeginCommentGroup&) { this->pushControl(); } | |
| 165 void trackBounds(const AddComment&) { this->pushControl(); } | |
| 166 void trackBounds(const EndCommentGroup&) { this->pushControl(); } | |
| 167 void trackBounds(const DrawData&) { this->pushControl(); } | |
| 168 | |
| 169 // For all other ops, we can calculate and store the bounds directly now. | |
| 170 template <typename T> void trackBounds(const T& op) { | |
| 171 fBounds[fCurrentOp] = this->bounds(op); | |
| 172 this->updateSaveBounds(fBounds[fCurrentOp]); | |
| 173 } | |
| 174 | |
| 175 void pushSaveBlock(const SkPaint* paint) { | |
| 176 // Starting a new Save block. Push a new entry to represent that. | |
| 177 SaveBounds sb = { 0, Bounds::MakeEmpty(), paint }; | |
| 178 fSaveStack.push(sb); | |
| 179 this->pushControl(); | |
| 180 } | |
| 181 | |
| 182 static bool PaintMayAffectTransparentBlack(const SkPaint* paint) { | |
| 183 if (paint) { | |
| 184 // FIXME: this is very conservative | |
| 185 if (paint->getImageFilter() || paint->getColorFilter()) { | |
| 186 return true; | |
| 187 } | |
| 188 | |
| 189 // Unusual Xfermodes require us to process a saved layer | |
| 190 // even with operations outisde the clip. | |
| 191 // For example, DstIn is used by masking layers. | |
| 192 // https://code.google.com/p/skia/issues/detail?id=1291 | |
| 193 // https://crbug.com/401593 | |
| 194 SkXfermode* xfermode = paint->getXfermode(); | |
| 195 SkXfermode::Mode mode; | |
| 196 // SrcOver is ok, and is also the common case with a NULL xfermode. | |
| 197 // So we should make that the fast path and bypass the mode extracti
on | |
| 198 // and test. | |
| 199 if (xfermode && xfermode->asMode(&mode)) { | |
| 200 switch (mode) { | |
| 201 // For each of the following transfer modes, if the source | |
| 202 // alpha is zero (our transparent black), the resulting | |
| 203 // blended alpha is not necessarily equal to the original | |
| 204 // destination alpha. | |
| 205 case SkXfermode::kClear_Mode: | |
| 206 case SkXfermode::kSrc_Mode: | |
| 207 case SkXfermode::kSrcIn_Mode: | |
| 208 case SkXfermode::kDstIn_Mode: | |
| 209 case SkXfermode::kSrcOut_Mode: | |
| 210 case SkXfermode::kDstATop_Mode: | |
| 211 case SkXfermode::kModulate_Mode: | |
| 212 return true; | |
| 213 break; | |
| 214 default: | |
| 215 break; | |
| 216 } | |
| 217 } | |
| 218 } | |
| 219 return false; | |
| 220 } | |
| 221 | |
| 222 Bounds popSaveBlock() { | |
| 223 // We're done the Save block. Apply the block's bounds to all control o
ps inside it. | |
| 224 SaveBounds sb; | |
| 225 fSaveStack.pop(&sb); | |
| 226 | |
| 227 // If the paint affects transparent black, we can't trust any of our cal
culated bounds. | |
| 228 const Bounds& bounds = | |
| 229 PaintMayAffectTransparentBlack(sb.paint) ? fCurrentClipBounds : sb.b
ounds; | |
| 230 | |
| 231 while (sb.controlOps-- > 0) { | |
| 232 this->popControl(bounds); | |
| 233 } | |
| 234 | |
| 235 // This whole Save block may be part another Save block. | |
| 236 this->updateSaveBounds(bounds); | |
| 237 | |
| 238 // If called from a real Restore (not a phony one for balance), it'll ne
ed the bounds. | |
| 239 return bounds; | |
| 240 } | |
| 241 | |
| 242 void pushControl() { | |
| 243 fControlIndices.push(fCurrentOp); | |
| 244 if (!fSaveStack.isEmpty()) { | |
| 245 fSaveStack.top().controlOps++; | |
| 246 } | |
| 247 } | |
| 248 | |
| 249 void popControl(const Bounds& bounds) { | |
| 250 fBounds[fControlIndices.top()] = bounds; | |
| 251 fControlIndices.pop(); | |
| 252 } | |
| 253 | |
| 254 void updateSaveBounds(const Bounds& bounds) { | |
| 255 // If we're in a Save block, expand its bounds to cover these bounds too
. | |
| 256 if (!fSaveStack.isEmpty()) { | |
| 257 fSaveStack.top().bounds.join(bounds); | |
| 258 } | |
| 259 } | |
| 260 | |
| 261 // FIXME: this method could use better bounds | |
| 262 Bounds bounds(const DrawText&) const { return fCurrentClipBounds; } | |
| 263 | |
| 264 Bounds bounds(const Clear&) const { return kUnbounded; } // Igno
res the clip. | |
| 265 Bounds bounds(const DrawPaint&) const { return fCurrentClipBounds; } | |
| 266 Bounds bounds(const NoOp&) const { return Bounds::MakeEmpty(); } // NoOp
s don't draw. | |
| 267 | |
| 268 Bounds bounds(const DrawSprite& op) const { | |
| 269 const SkBitmap& bm = op.bitmap; | |
| 270 | |
| 271 return Bounds::Make(SkIRect::MakeXYWH(op.left, op.top, bm.width(), bm.he
ight())); // Ignores the matrix. | |
| 272 } | |
| 273 | |
| 274 Bounds bounds(const DrawRect& op) const { return this->adjustAndMap(op.rect,
&op.paint); } | |
| 275 Bounds bounds(const DrawOval& op) const { return this->adjustAndMap(op.oval,
&op.paint); } | |
| 276 Bounds bounds(const DrawRRect& op) const { | |
| 277 return this->adjustAndMap(op.rrect.rect(), &op.paint); | |
| 278 } | |
| 279 Bounds bounds(const DrawDRRect& op) const { | |
| 280 return this->adjustAndMap(op.outer.rect(), &op.paint); | |
| 281 } | |
| 282 Bounds bounds(const DrawImage& op) const { | |
| 283 const SkImage* image = op.image; | |
| 284 SkRect rect = SkRect::MakeXYWH(op.left, op.top, | |
| 285 SkIntToScalar(image->width()), SkIntToSc
alar(image->height())); | |
| 286 | |
| 287 return this->adjustAndMap(rect, op.paint); | |
| 288 } | |
| 289 Bounds bounds(const DrawImageRect& op) const { | |
| 290 return this->adjustAndMap(op.dst, op.paint); | |
| 291 } | |
| 292 Bounds bounds(const DrawBitmapRectToRect& op) const { | |
| 293 return this->adjustAndMap(op.dst, op.paint); | |
| 294 } | |
| 295 Bounds bounds(const DrawBitmapNine& op) const { | |
| 296 return this->adjustAndMap(op.dst, op.paint); | |
| 297 } | |
| 298 Bounds bounds(const DrawBitmap& op) const { | |
| 299 const SkBitmap& bm = op.bitmap; | |
| 300 return this->adjustAndMap(SkRect::MakeXYWH(op.left, op.top, SkIntToScala
r(bm.width()), SkIntToScalar(bm.height())), | |
| 301 op.paint); | |
| 302 } | |
| 303 Bounds bounds(const DrawBitmapMatrix& op) const { | |
| 304 const SkBitmap& bm = op.bitmap; | |
| 305 SkRect dst = SkRect::Make(SkIRect::MakeWH(bm.width(), bm.height())); | |
| 306 op.matrix.mapRect(&dst); | |
| 307 return this->adjustAndMap(dst, op.paint); | |
| 308 } | |
| 309 | |
| 310 Bounds bounds(const DrawPath& op) const { | |
| 311 return op.path.isInverseFillType() ? fCurrentClipBounds | |
| 312 : this->adjustAndMap(op.path.getBounds(), &op.paint); | |
| 313 } | |
| 314 Bounds bounds(const DrawPoints& op) const { | |
| 315 SkRect dst; | |
| 316 dst.set(op.pts, op.count); | |
| 317 | |
| 318 // Pad the bounding box a little to make sure hairline points' bounds ar
en't empty. | |
| 319 SkScalar stroke = SkMaxScalar(op.paint.getStrokeWidth(), 0.01f); | |
| 320 dst.outset(stroke / 2, stroke / 2); | |
| 321 | |
| 322 return this->adjustAndMap(dst, &op.paint); | |
| 323 } | |
| 324 Bounds bounds(const DrawPatch& op) const { | |
| 325 SkRect dst; | |
| 326 dst.set(op.cubics, SkPatchUtils::kNumCtrlPts); | |
| 327 return this->adjustAndMap(dst, &op.paint); | |
| 328 } | |
| 329 Bounds bounds(const DrawVertices& op) const { | |
| 330 SkRect dst; | |
| 331 dst.set(op.vertices, op.vertexCount); | |
| 332 return this->adjustAndMap(dst, &op.paint); | |
| 333 } | |
| 334 | |
| 335 Bounds bounds(const DrawPicture& op) const { | |
| 336 SkRect dst = op.picture->cullRect(); | |
| 337 if (op.matrix) { | |
| 338 op.matrix->mapRect(&dst); | |
| 339 } | |
| 340 return this->adjustAndMap(dst, op.paint); | |
| 341 } | |
| 342 | |
| 343 Bounds bounds(const DrawPosText& op) const { | |
| 344 const int N = op.paint.countText(op.text, op.byteLength); | |
| 345 if (N == 0) { | |
| 346 return Bounds::MakeEmpty(); | |
| 347 } | |
| 348 | |
| 349 SkRect dst; | |
| 350 dst.set(op.pos, N); | |
| 351 AdjustTextForFontMetrics(&dst, op.paint); | |
| 352 return this->adjustAndMap(dst, &op.paint); | |
| 353 } | |
| 354 Bounds bounds(const DrawPosTextH& op) const { | |
| 355 const int N = op.paint.countText(op.text, op.byteLength); | |
| 356 if (N == 0) { | |
| 357 return Bounds::MakeEmpty(); | |
| 358 } | |
| 359 | |
| 360 SkScalar left = op.xpos[0], right = op.xpos[0]; | |
| 361 for (int i = 1; i < N; i++) { | |
| 362 left = SkMinScalar(left, op.xpos[i]); | |
| 363 right = SkMaxScalar(right, op.xpos[i]); | |
| 364 } | |
| 365 SkRect dst = { left, op.y, right, op.y }; | |
| 366 AdjustTextForFontMetrics(&dst, op.paint); | |
| 367 return this->adjustAndMap(dst, &op.paint); | |
| 368 } | |
| 369 Bounds bounds(const DrawTextOnPath& op) const { | |
| 370 SkRect dst = op.path.getBounds(); | |
| 371 | |
| 372 // Pad all sides by the maximum padding in any direction we'd normally a
pply. | |
| 373 SkRect pad = { 0, 0, 0, 0 }; | |
| 374 AdjustTextForFontMetrics(&pad, op.paint); | |
| 375 | |
| 376 // That maximum padding happens to always be the right pad today. | |
| 377 SkASSERT(pad.fLeft == -pad.fRight); | |
| 378 SkASSERT(pad.fTop == -pad.fBottom); | |
| 379 SkASSERT(pad.fRight > pad.fBottom); | |
| 380 dst.outset(pad.fRight, pad.fRight); | |
| 381 | |
| 382 return this->adjustAndMap(dst, &op.paint); | |
| 383 } | |
| 384 | |
| 385 Bounds bounds(const DrawTextBlob& op) const { | |
| 386 SkRect dst = op.blob->bounds(); | |
| 387 dst.offset(op.x, op.y); | |
| 388 return this->adjustAndMap(dst, &op.paint); | |
| 389 } | |
| 390 | |
| 391 static void AdjustTextForFontMetrics(SkRect* rect, const SkPaint& paint) { | |
| 392 #ifdef SK_DEBUG | |
| 393 SkRect correct = *rect; | |
| 394 #endif | |
| 395 // crbug.com/373785 ~~> xPad = 4x yPad | |
| 396 // crbug.com/424824 ~~> bump yPad from 2x text size to 2.5x | |
| 397 const SkScalar yPad = 2.5f * paint.getTextSize(), | |
| 398 xPad = 4.0f * yPad; | |
| 399 rect->outset(xPad, yPad); | |
| 400 #ifdef SK_DEBUG | |
| 401 SkPaint::FontMetrics metrics; | |
| 402 paint.getFontMetrics(&metrics); | |
| 403 correct.fLeft += metrics.fXMin; | |
| 404 correct.fTop += metrics.fTop; | |
| 405 correct.fRight += metrics.fXMax; | |
| 406 correct.fBottom += metrics.fBottom; | |
| 407 // See skia:2862 for why we ignore small text sizes. | |
| 408 SkASSERTF(paint.getTextSize() < 0.001f || rect->contains(correct), | |
| 409 "%f %f %f %f vs. %f %f %f %f\n", | |
| 410 -xPad, -yPad, +xPad, +yPad, | |
| 411 metrics.fXMin, metrics.fTop, metrics.fXMax, metrics.fBottom)
; | |
| 412 #endif | |
| 413 } | |
| 414 | |
| 415 //--------- LAYER HOISTING | |
| 416 template <typename T> void trackSaveLayers(const T& op) { | 67 template <typename T> void trackSaveLayers(const T& op) { |
| 417 /* most ops aren't involved in saveLayers */ | 68 /* most ops aren't involved in saveLayers */ |
| 418 } | 69 } |
| 419 void trackSaveLayers(const Save& s) { this->pushSaveLayerInfo(false, NULL);
} | 70 void trackSaveLayers(const Save& s) { this->pushSaveLayerInfo(false, NULL);
} |
| 420 void trackSaveLayers(const SaveLayer& sl) { | 71 void trackSaveLayers(const SaveLayer& sl) { this->pushSaveLayerInfo(true, sl
.paint); } |
| 421 this->pushSaveLayerInfo(true, sl.paint); | |
| 422 } | |
| 423 void trackSaveLayers(const Restore& r) { this->popSaveLayerInfo(); } | 72 void trackSaveLayers(const Restore& r) { this->popSaveLayerInfo(); } |
| 73 |
| 424 void trackSaveLayers(const DrawPicture& dp) { | 74 void trackSaveLayers(const DrawPicture& dp) { |
| 425 // For sub-pictures, we wrap their layer information within the parent | 75 // For sub-pictures, we wrap their layer information within the parent |
| 426 // picture's rendering hierarchy | 76 // picture's rendering hierarchy |
| 427 const GrAccelData* childData = GPUOptimize(dp.picture); | 77 const GrAccelData* childData = GPUOptimize(dp.picture); |
| 428 | 78 |
| 429 for (int i = 0; i < childData->numSaveLayers(); ++i) { | 79 for (int i = 0; i < childData->numSaveLayers(); ++i) { |
| 430 const GrAccelData::SaveLayerInfo& src = childData->saveLayerInfo(i); | 80 const GrAccelData::SaveLayerInfo& src = childData->saveLayerInfo(i); |
| 431 | 81 |
| 432 Bounds newClip(fCurrentClipBounds); | 82 FillBounds::Bounds newClip(fFillBounds.currentClipBounds()); |
| 433 | 83 |
| 434 if (!newClip.intersect(this->adjustAndMap(src.fBounds, dp.paint))) { | 84 if (!newClip.intersect(fFillBounds.adjustAndMap(src.fBounds, dp.pain
t))) { |
| 435 continue; | 85 continue; |
| 436 } | 86 } |
| 437 | 87 |
| 438 this->updateStackForSaveLayer(); | 88 this->updateStackForSaveLayer(); |
| 439 | 89 |
| 440 GrAccelData::SaveLayerInfo& dst = fAccelData->addSaveLayerInfo(); | 90 GrAccelData::SaveLayerInfo& dst = fAccelData->addSaveLayerInfo(); |
| 441 | 91 |
| 442 // If src.fPicture is NULL the layer is in dp.picture; otherwise | 92 // If src.fPicture is NULL the layer is in dp.picture; otherwise |
| 443 // it belongs to a sub-picture. | 93 // it belongs to a sub-picture. |
| 444 dst.fPicture = src.fPicture ? src.fPicture : static_cast<const SkPic
ture*>(dp.picture); | 94 dst.fPicture = src.fPicture ? src.fPicture : static_cast<const SkPic
ture*>(dp.picture); |
| 445 dst.fPicture->ref(); | 95 dst.fPicture->ref(); |
| 446 dst.fBounds = newClip; | 96 dst.fBounds = newClip; |
| 447 dst.fLocalMat = src.fLocalMat; | 97 dst.fLocalMat = src.fLocalMat; |
| 448 dst.fPreMat = src.fPreMat; | 98 dst.fPreMat = src.fPreMat; |
| 449 dst.fPreMat.postConcat(*fCTM); | 99 dst.fPreMat.postConcat(fFillBounds.ctm()); |
| 450 if (src.fPaint) { | 100 if (src.fPaint) { |
| 451 dst.fPaint = SkNEW_ARGS(SkPaint, (*src.fPaint)); | 101 dst.fPaint = SkNEW_ARGS(SkPaint, (*src.fPaint)); |
| 452 } | 102 } |
| 453 dst.fSaveLayerOpID = src.fSaveLayerOpID; | 103 dst.fSaveLayerOpID = src.fSaveLayerOpID; |
| 454 dst.fRestoreOpID = src.fRestoreOpID; | 104 dst.fRestoreOpID = src.fRestoreOpID; |
| 455 dst.fHasNestedLayers = src.fHasNestedLayers; | 105 dst.fHasNestedLayers = src.fHasNestedLayers; |
| 456 dst.fIsNested = fSaveLayersInStack > 0 || src.fIsNested; | 106 dst.fIsNested = fSaveLayersInStack > 0 || src.fIsNested; |
| 457 } | 107 } |
| 458 } | 108 } |
| 459 | 109 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 470 } | 120 } |
| 471 } | 121 } |
| 472 } | 122 } |
| 473 | 123 |
| 474 void pushSaveLayerInfo(bool isSaveLayer, const SkPaint* paint) { | 124 void pushSaveLayerInfo(bool isSaveLayer, const SkPaint* paint) { |
| 475 if (isSaveLayer) { | 125 if (isSaveLayer) { |
| 476 this->updateStackForSaveLayer(); | 126 this->updateStackForSaveLayer(); |
| 477 ++fSaveLayersInStack; | 127 ++fSaveLayersInStack; |
| 478 } | 128 } |
| 479 | 129 |
| 480 fSaveLayerStack.push(SaveLayerInfo(fCurrentOp, isSaveLayer, paint, fCurr
entClipBounds)); | 130 fSaveLayerStack.push(SaveLayerInfo(fFillBounds.currentOp(), isSaveLayer,
paint, |
| 131 fFillBounds.currentClipBounds())); |
| 481 } | 132 } |
| 482 | 133 |
| 483 void popSaveLayerInfo() { | 134 void popSaveLayerInfo() { |
| 484 if (fSaveLayerStack.count() <= 0) { | 135 if (fSaveLayerStack.count() <= 0) { |
| 485 SkASSERT(false); | 136 SkASSERT(false); |
| 486 return; | 137 return; |
| 487 } | 138 } |
| 488 | 139 |
| 489 SaveLayerInfo sli; | 140 SaveLayerInfo sli; |
| 490 fSaveLayerStack.pop(&sli); | 141 fSaveLayerStack.pop(&sli); |
| 491 | 142 |
| 492 if (!sli.fIsSaveLayer) { | 143 if (!sli.fIsSaveLayer) { |
| 493 return; | 144 return; |
| 494 } | 145 } |
| 495 | 146 |
| 496 --fSaveLayersInStack; | 147 --fSaveLayersInStack; |
| 497 | 148 |
| 498 GrAccelData::SaveLayerInfo& slInfo = fAccelData->addSaveLayerInfo(); | 149 GrAccelData::SaveLayerInfo& slInfo = fAccelData->addSaveLayerInfo(); |
| 499 | 150 |
| 500 SkASSERT(NULL == slInfo.fPicture); // This layer is in the top-most pic
ture | 151 SkASSERT(NULL == slInfo.fPicture); // This layer is in the top-most pic
ture |
| 501 | 152 |
| 502 slInfo.fBounds = fBounds[sli.fStartIndex]; | 153 slInfo.fBounds = fFillBounds.getBounds(sli.fStartIndex); |
| 503 slInfo.fBounds.intersect(sli.fClipBound); | 154 slInfo.fBounds.intersect(sli.fClipBound); |
| 504 slInfo.fLocalMat = *fCTM; | 155 slInfo.fLocalMat = fFillBounds.ctm(); |
| 505 slInfo.fPreMat = SkMatrix::I(); | 156 slInfo.fPreMat = SkMatrix::I(); |
| 506 if (sli.fPaint) { | 157 if (sli.fPaint) { |
| 507 slInfo.fPaint = SkNEW_ARGS(SkPaint, (*sli.fPaint)); | 158 slInfo.fPaint = SkNEW_ARGS(SkPaint, (*sli.fPaint)); |
| 508 } | 159 } |
| 509 slInfo.fSaveLayerOpID = sli.fStartIndex; | 160 slInfo.fSaveLayerOpID = sli.fStartIndex; |
| 510 slInfo.fRestoreOpID = fCurrentOp; | 161 slInfo.fRestoreOpID = fFillBounds.currentOp(); |
| 511 slInfo.fHasNestedLayers = sli.fHasNestedSaveLayer; | 162 slInfo.fHasNestedLayers = sli.fHasNestedSaveLayer; |
| 512 slInfo.fIsNested = fSaveLayersInStack > 0; | 163 slInfo.fIsNested = fSaveLayersInStack > 0; |
| 513 } | 164 } |
| 514 //--------- LAYER HOISTING | |
| 515 | 165 |
| 516 // Returns true if rect was meaningfully adjusted for the effects of paint, | |
| 517 // false if the paint could affect the rect in unknown ways. | |
| 518 static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) { | |
| 519 if (paint) { | |
| 520 if (paint->canComputeFastBounds()) { | |
| 521 *rect = paint->computeFastBounds(*rect, rect); | |
| 522 return true; | |
| 523 } | |
| 524 return false; | |
| 525 } | |
| 526 return true; | |
| 527 } | |
| 528 | |
| 529 bool adjustForSaveLayerPaints(SkRect* rect, int savesToIgnore = 0) const { | |
| 530 for (int i = fSaveStack.count() - 1 - savesToIgnore; i >= 0; i--) { | |
| 531 if (!AdjustForPaint(fSaveStack[i].paint, rect)) { | |
| 532 return false; | |
| 533 } | |
| 534 } | |
| 535 return true; | |
| 536 } | |
| 537 | |
| 538 // Adjust rect for all paints that may affect its geometry, then map it to i
dentity space. | |
| 539 Bounds adjustAndMap(SkRect rect, const SkPaint* paint) const { | |
| 540 // Inverted rectangles really confuse our BBHs. | |
| 541 rect.sort(); | |
| 542 | |
| 543 // Adjust the rect for its own paint. | |
| 544 if (!AdjustForPaint(paint, &rect)) { | |
| 545 // The paint could do anything to our bounds. The only safe answer
is the current clip. | |
| 546 return fCurrentClipBounds; | |
| 547 } | |
| 548 | |
| 549 // Adjust rect for all the paints from the SaveLayers we're inside. | |
| 550 if (!this->adjustForSaveLayerPaints(&rect)) { | |
| 551 // Same deal as above. | |
| 552 return fCurrentClipBounds; | |
| 553 } | |
| 554 | |
| 555 // Map the rect back to identity space. | |
| 556 fCTM->mapRect(&rect); | |
| 557 | |
| 558 // Nothing can draw outside the current clip. | |
| 559 // (Only bounded ops call into this method, so oddballs like Clear don't
matter here.) | |
| 560 rect.intersect(fCurrentClipBounds); | |
| 561 return rect; | |
| 562 } | |
| 563 | |
| 564 // Conservative identity-space bounds for each op in the SkRecord. | |
| 565 SkAutoTMalloc<Bounds> fBounds; | |
| 566 | |
| 567 // We walk fCurrentOp through the SkRecord, as we go using updateCTM() | |
| 568 // and updateClipBounds() to maintain the exact CTM (fCTM) and conservative | |
| 569 // identity-space bounds of the current clip (fCurrentClipBounds). | |
| 570 unsigned fCurrentOp; | |
| 571 const SkMatrix* fCTM; | |
| 572 Bounds fCurrentClipBounds; | |
| 573 | |
| 574 // Used to track the bounds of Save/Restore blocks and the control ops insid
e them. | |
| 575 SkTDArray<SaveBounds> fSaveStack; | |
| 576 SkTDArray<unsigned> fControlIndices; | |
| 577 | |
| 578 //--------- LAYER HOISTING | |
| 579 // Used to collect saveLayer information for layer hoisting | 166 // Used to collect saveLayer information for layer hoisting |
| 580 uint32_t fPictureID; | 167 uint32_t fPictureID; |
| 581 int fSaveLayersInStack; | 168 int fSaveLayersInStack; |
| 582 SkTDArray<SaveLayerInfo> fSaveLayerStack; | 169 SkTDArray<SaveLayerInfo> fSaveLayerStack; |
| 583 GrAccelData* fAccelData; | 170 GrAccelData* fAccelData; |
| 584 //--------- LAYER HOISTING | 171 |
| 172 SkRecords::FillBounds fFillBounds; |
| 585 }; | 173 }; |
| 586 | 174 |
| 587 } // namespace SkRecords | 175 } // namespace SkRecords |
| 588 | 176 |
| 589 // GPUOptimize is only intended to be called within the context of SkGpuDevice's | 177 // GPUOptimize is only intended to be called within the context of SkGpuDevice's |
| 590 // EXPERIMENTAL_optimize method. | 178 // EXPERIMENTAL_optimize method. |
| 591 const GrAccelData* GPUOptimize(const SkPicture* pict) { | 179 const GrAccelData* GPUOptimize(const SkPicture* pict) { |
| 592 if (NULL == pict || pict->cullRect().isEmpty()) { | 180 if (NULL == pict || pict->cullRect().isEmpty()) { |
| 593 return NULL; | 181 return NULL; |
| 594 } | 182 } |
| 595 | 183 |
| 596 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey(); | 184 SkPicture::AccelData::Key key = GrAccelData::ComputeAccelDataKey(); |
| 597 | 185 |
| 598 const GrAccelData* existing = | 186 const GrAccelData* existing = |
| 599 static_cast<const GrAccelData*>(pict->EXPERIMENTAL_g
etAccelData(key)); | 187 static_cast<const GrAccelData*>(pict->EXPERIMENTAL_g
etAccelData(key)); |
| 600 if (existing) { | 188 if (existing) { |
| 601 return existing; | 189 return existing; |
| 602 } | 190 } |
| 603 | 191 |
| 604 SkAutoTUnref<GrAccelData> data(SkNEW_ARGS(GrAccelData, (key))); | 192 SkAutoTUnref<GrAccelData> data(SkNEW_ARGS(GrAccelData, (key))); |
| 605 | 193 |
| 606 pict->EXPERIMENTAL_addAccelData(data); | 194 pict->EXPERIMENTAL_addAccelData(data); |
| 607 | 195 |
| 608 SkRecords::CollectLayers collector(pict, data); | 196 SkRecords::CollectLayers visitor(pict, NULL, data); |
| 197 |
| 198 const SkRecord& record = *pict->fRecord; |
| 199 for (unsigned curOp = 0; curOp < record.count(); curOp++) { |
| 200 visitor.setCurrentOp(curOp); |
| 201 record.visit<void>(curOp, visitor); |
| 202 } |
| 203 |
| 204 visitor.cleanUp(); |
| 609 | 205 |
| 610 return data; | 206 return data; |
| 611 } | 207 } |
| OLD | NEW |