| 1 |
/* |
| 2 |
* Copyright 2014 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "src/core/SkRecordDraw.h" |
| 9 |
|
| 10 |
#include "include/core/SkBBHFactory.h" |
| 11 |
#include "include/core/SkBlendMode.h" |
| 12 |
#include "include/core/SkBlender.h" |
| 13 |
#include "include/core/SkColor.h" |
| 14 |
#include "include/core/SkImage.h" |
| 15 |
#include "include/core/SkMatrix.h" |
| 16 |
#include "include/core/SkMesh.h" |
| 17 |
#include "include/core/SkPaint.h" |
| 18 |
#include "include/core/SkRRect.h" |
| 19 |
#include "include/core/SkRSXform.h" |
| 20 |
#include "include/core/SkRect.h" |
| 21 |
#include "include/core/SkRefCnt.h" |
| 22 |
#include "include/core/SkRegion.h" |
| 23 |
#include "include/core/SkScalar.h" |
| 24 |
#include "include/core/SkShader.h" |
| 25 |
#include "include/core/SkString.h" |
| 26 |
#include "include/core/SkTextBlob.h" |
| 27 |
#include "include/core/SkVertices.h" |
| 28 |
#include "include/private/base/SkAssert.h" |
| 29 |
#include "include/private/base/SkPoint_impl.h" |
| 30 |
#include "include/private/base/SkTDArray.h" |
| 31 |
#include "include/private/base/SkTemplates.h" |
| 32 |
#include "include/private/chromium/Slug.h" |
| 33 |
#include "src/core/SkCanvasPriv.h" |
| 34 |
#include "src/core/SkDrawShadowInfo.h" |
| 35 |
#include "src/core/SkImageFilter_Base.h" |
| 36 |
#include "src/core/SkRecord.h" |
| 37 |
#include "src/core/SkRecords.h" |
| 38 |
#include "src/effects/colorfilters/SkColorFilterBase.h" |
| 39 |
#include "src/utils/SkPatchUtils.h" |
| 40 |
|
| 41 |
#include <algorithm> |
| 42 |
#include <cstddef> |
| 43 |
#include <optional> |
| 44 |
#include <vector> |
| 45 |
|
| 46 |
class SkImageFilter; |
| 47 |
|
| 48 |
void SkRecordDraw(const SkRecord& record, |
| 49 |
SkCanvas* canvas, |
| 50 |
SkPicture const* const drawablePicts[], |
| 51 |
SkDrawable* const drawables[], |
| 52 |
int drawableCount, |
| 53 |
const SkBBoxHierarchy* bbh, |
| 54 |
SkPicture::AbortCallback* callback) { |
| 55 |
SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/); |
| 56 |
|
| 57 |
if (bbh) { |
| 58 |
// Draw only ops that affect pixels in the canvas's current clip. |
| 59 |
// The SkRecord and BBH were recorded in identity space. This canvas |
| 60 |
// is not necessarily in that same space. getLocalClipBounds() returns us |
| 61 |
// this canvas' clip bounds transformed back into identity space, which |
| 62 |
// lets us query the BBH. |
| 63 |
SkRect query = canvas->getLocalClipBounds(); |
| 64 |
|
| 65 |
std::vector<int> ops; |
| 66 |
bbh->search(query, &ops); |
| 67 |
|
| 68 |
SkRecords::Draw draw(canvas, drawablePicts, drawables, drawableCount); |
| 69 |
for (int i = 0; i < (int)ops.size(); i++) { |
| 70 |
if (callback && callback->abort()) { |
| 71 |
return; |
| 72 |
} |
| 73 |
// This visit call uses the SkRecords::Draw::operator() to call |
| 74 |
// methods on the |canvas|, wrapped by methods defined with the |
| 75 |
// DRAW() macro. |
| 76 |
record.visit(ops[i], draw); |
| 77 |
} |
| 78 |
} else { |
| 79 |
// Draw all ops. |
| 80 |
SkRecords::Draw draw(canvas, drawablePicts, drawables, drawableCount); |
| 81 |
for (int i = 0; i < record.count(); i++) { |
| 82 |
if (callback && callback->abort()) { |
| 83 |
return; |
| 84 |
} |
| 85 |
// This visit call uses the SkRecords::Draw::operator() to call |
| 86 |
// methods on the |canvas|, wrapped by methods defined with the |
| 87 |
// DRAW() macro. |
| 88 |
record.visit(i, draw); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
namespace SkRecords { |
| 94 |
|
| 95 |
// NoOps draw nothing. |
| 96 |
template <> void Draw::draw(const NoOp&) {} |
| 97 |
|
| 98 |
#define DRAW(T, call) template <> void Draw::draw(const T& r) { fCanvas->call; } |
| 99 |
DRAW(Restore, restore()) |
| 100 |
DRAW(Save, save()) |
| 101 |
DRAW(SaveLayer, |
| 102 |
saveLayer(SkCanvasPriv::ScaledBackdropLayer( |
| 103 |
r.bounds, |
| 104 |
r.paint, |
| 105 |
r.backdrop.get(), |
| 106 |
r.backdropScale, |
| 107 |
r.backdropTileMode, |
| 108 |
r.saveLayerFlags, |
| 109 |
SkCanvas::FilterSpan{const_cast<sk_sp<SkImageFilter>*>(r.filters.data()), |
| 110 |
r.filters.size()}))) |
| 111 |
|
| 112 |
template <> void Draw::draw(const SaveBehind& r) { |
| 113 |
SkCanvasPriv::SaveBehind(fCanvas, r.subset); |
| 114 |
} |
| 115 |
|
| 116 |
template <> void Draw::draw(const DrawBehind& r) { |
| 117 |
SkCanvasPriv::DrawBehind(fCanvas, r.paint); |
| 118 |
} |
| 119 |
|
| 120 |
DRAW(SetMatrix, setMatrix(fInitialCTM.asM33() * r.matrix)) |
| 121 |
DRAW(SetM44, setMatrix(fInitialCTM * r.matrix)) |
| 122 |
DRAW(Concat44, concat(r.matrix)) |
| 123 |
DRAW(Concat, concat(r.matrix)) |
| 124 |
DRAW(Translate, translate(r.dx, r.dy)) |
| 125 |
DRAW(Scale, scale(r.sx, r.sy)) |
| 126 |
|
| 127 |
DRAW(ClipPath, clipPath(r.path, r.opAA.op(), r.opAA.aa())) |
| 128 |
DRAW(ClipRRect, clipRRect(r.rrect, r.opAA.op(), r.opAA.aa())) |
| 129 |
DRAW(ClipRect, clipRect(r.rect, r.opAA.op(), r.opAA.aa())) |
| 130 |
DRAW(ClipRegion, clipRegion(r.region, r.op)) |
| 131 |
DRAW(ClipShader, clipShader(r.shader, r.op)) |
| 132 |
|
| 133 |
template <> void Draw::draw(const ResetClip& r) { |
| 134 |
SkCanvasPriv::ResetClip(fCanvas); |
| 135 |
} |
| 136 |
|
| 137 |
DRAW(DrawArc, drawArc(r.oval, r.startAngle, r.sweepAngle, r.useCenter, r.paint)) |
| 138 |
DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint)) |
| 139 |
DRAW(DrawImage, drawImage(r.image.get(), r.left, r.top, r.sampling, r.paint)) |
| 140 |
|
| 141 |
template <> void Draw::draw(const DrawImageLattice& r) { |
| 142 |
SkCanvas::Lattice lattice; |
| 143 |
lattice.fXCount = r.xCount; |
| 144 |
lattice.fXDivs = r.xDivs; |
| 145 |
lattice.fYCount = r.yCount; |
| 146 |
lattice.fYDivs = r.yDivs; |
| 147 |
lattice.fRectTypes = (0 == r.flagCount) ? nullptr : r.flags; |
| 148 |
lattice.fColors = (0 == r.flagCount) ? nullptr : r.colors; |
| 149 |
lattice.fBounds = &r.src; |
| 150 |
fCanvas->drawImageLattice(r.image.get(), lattice, r.dst, r.filter, r.paint); |
| 151 |
} |
| 152 |
|
| 153 |
DRAW(DrawImageRect, drawImageRect(r.image.get(), r.src, r.dst, r.sampling, r.paint, r.constraint)) |
| 154 |
DRAW(DrawOval, drawOval(r.oval, r.paint)) |
| 155 |
DRAW(DrawPaint, drawPaint(r.paint)) |
| 156 |
DRAW(DrawPath, drawPath(r.path, r.paint)) |
| 157 |
DRAW(DrawPatch, drawPatch(r.cubics, r.colors, r.texCoords, r.bmode, r.paint)) |
| 158 |
DRAW(DrawPicture, drawPicture(r.picture.get(), &r.matrix, r.paint)) |
| 159 |
DRAW(DrawPoints, drawPoints(r.mode, {r.pts.data(), r.count}, r.paint)) |
| 160 |
DRAW(DrawRRect, drawRRect(r.rrect, r.paint)) |
| 161 |
DRAW(DrawRect, drawRect(r.rect, r.paint)) |
| 162 |
DRAW(DrawRegion, drawRegion(r.region, r.paint)) |
| 163 |
DRAW(DrawTextBlob, drawTextBlob(r.blob.get(), r.x, r.y, r.paint)) |
| 164 |
DRAW(DrawSlug, drawSlug(r.slug.get(), r.paint)) |
| 165 |
DRAW(DrawAtlas, drawAtlas(r.atlas.get(), |
| 166 |
{r.xforms.data(), r.count}, |
| 167 |
{r.texs.data(), r.count}, |
| 168 |
{r.colors.data(), r.colors ? r.count : 0}, |
| 169 |
r.mode, r.sampling, r.cull, r.paint)) |
| 170 |
DRAW(DrawVertices, drawVertices(r.vertices, r.bmode, r.paint)) |
| 171 |
DRAW(DrawMesh, drawMesh(r.mesh, r.blender, r.paint)) |
| 172 |
DRAW(DrawShadowRec, private_draw_shadow_rec(r.path, r.rec)) |
| 173 |
DRAW(DrawAnnotation, drawAnnotation(r.rect, r.key.c_str(), r.value.get())) |
| 174 |
|
| 175 |
DRAW(DrawEdgeAAQuad, experimental_DrawEdgeAAQuad( |
| 176 |
r.rect, r.clip, r.aa, r.color, r.mode)) |
| 177 |
DRAW(DrawEdgeAAImageSet, experimental_DrawEdgeAAImageSet( |
| 178 |
r.set.get(), r.count, r.dstClips, r.preViewMatrices, r.sampling, r.paint, r.constraint)) |
| 179 |
|
| 180 |
#undef DRAW |
| 181 |
|
| 182 |
template <> void Draw::draw(const DrawDrawable& r) { |
| 183 |
SkASSERT(r.index >= 0); |
| 184 |
SkASSERT(r.index < fDrawableCount); |
| 185 |
if (fDrawables) { |
| 186 |
SkASSERT(nullptr == fDrawablePicts); |
| 187 |
fCanvas->drawDrawable(fDrawables[r.index], r.matrix); |
| 188 |
} else { |
| 189 |
fCanvas->drawPicture(fDrawablePicts[r.index], r.matrix, nullptr); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
// This is an SkRecord visitor that fills an SkBBoxHierarchy. |
| 194 |
// |
| 195 |
// The interesting part here is how to calculate bounds for ops which don't |
| 196 |
// have intrinsic bounds. What is the bounds of a Save or a Translate? |
| 197 |
// |
| 198 |
// We answer this by thinking about a particular definition of bounds: if I |
| 199 |
// don't execute this op, pixels in this rectangle might draw incorrectly. So |
| 200 |
// the bounds of a Save, a Translate, a Restore, etc. are the union of the |
| 201 |
// bounds of Draw* ops that they might have an effect on. For any given |
| 202 |
// Save/Restore block, the bounds of the Save, the Restore, and any other |
| 203 |
// non-drawing ("control") ops inside are exactly the union of the bounds of |
| 204 |
// the drawing ops inside that block. |
| 205 |
// |
| 206 |
// To implement this, we keep a stack of active Save blocks. As we consume ops |
| 207 |
// inside the Save/Restore block, drawing ops are unioned with the bounds of |
| 208 |
// the block, and control ops are stashed away for later. When we finish the |
| 209 |
// block with a Restore, our bounds are complete, and we go back and fill them |
| 210 |
// in for all the control ops we stashed away. |
| 211 |
class FillBounds : SkNoncopyable { |
| 212 |
public: |
| 213 |
FillBounds(const SkRect& cullRect, const SkRecord& record, |
| 214 |
SkRect bounds[], SkBBoxHierarchy::Metadata meta[]) |
| 215 |
: fCullRect(cullRect) |
| 216 |
, fBounds(bounds) |
| 217 |
, fMeta(meta) { |
| 218 |
fCTM = SkMatrix::I(); |
| 219 |
|
| 220 |
// We push an extra save block to track the bounds of any top-level control operations. |
| 221 |
fSaveStack.push_back({ 0, Bounds::MakeEmpty(), nullptr, fCTM }); |
| 222 |
} |
| 223 |
|
| 224 |
~FillBounds() { |
| 225 |
// If we have any lingering unpaired Saves, simulate restores to make |
| 226 |
// sure all ops in those Save blocks have their bounds calculated. |
| 227 |
while (!fSaveStack.empty()) { |
| 228 |
this->popSaveBlock(); |
| 229 |
} |
| 230 |
|
| 231 |
// Any control ops not part of any Save/Restore block draw everywhere. |
| 232 |
while (!fControlIndices.empty()) { |
| 233 |
this->popControl(fCullRect); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
void setCurrentOp(int currentOp) { fCurrentOp = currentOp; } |
| 238 |
|
| 239 |
|
| 240 |
template <typename T> void operator()(const T& op) { |
| 241 |
this->updateCTM(op); |
| 242 |
this->trackBounds(op); |
| 243 |
} |
| 244 |
|
| 245 |
// In this file, SkRect are in local coordinates, Bounds are translated back to identity space. |
| 246 |
typedef SkRect Bounds; |
| 247 |
|
| 248 |
// Adjust rect for all paints that may affect its geometry, then map it to identity space. |
| 249 |
Bounds adjustAndMap(SkRect rect, const SkPaint* paint) const { |
| 250 |
// Inverted rectangles really confuse our BBHs. |
| 251 |
rect.sort(); |
| 252 |
|
| 253 |
// Adjust the rect for its own paint. |
| 254 |
if (!AdjustForPaint(paint, &rect)) { |
| 255 |
// The paint could do anything to our bounds. The only safe answer is the cull. |
| 256 |
return fCullRect; |
| 257 |
} |
| 258 |
|
| 259 |
// Adjust rect for all the paints from the SaveLayers we're inside. |
| 260 |
if (!this->adjustForSaveLayerPaints(&rect)) { |
| 261 |
// Same deal as above. |
| 262 |
return fCullRect; |
| 263 |
} |
| 264 |
|
| 265 |
// Map the rect back to identity space. |
| 266 |
fCTM.mapRect(&rect); |
| 267 |
|
| 268 |
// Nothing can draw outside the cull rect. |
| 269 |
if (!rect.intersect(fCullRect)) { |
| 270 |
return Bounds::MakeEmpty(); |
| 271 |
} |
| 272 |
|
| 273 |
return rect; |
| 274 |
} |
| 275 |
|
| 276 |
private: |
| 277 |
struct SaveBounds { |
| 278 |
int controlOps; // Number of control ops in this Save block, including the Save. |
| 279 |
Bounds bounds; // Bounds of everything in the block. |
| 280 |
const SkPaint* paint; // Unowned. If set, adjusts the bounds of all ops in this block. |
| 281 |
SkMatrix ctm; |
| 282 |
}; |
| 283 |
|
| 284 |
// Only Restore, SetMatrix, Concat, and Translate change the CTM. |
| 285 |
template <typename T> void updateCTM(const T&) {} |
| 286 |
void updateCTM(const Restore& op) { fCTM = op.matrix; } |
| 287 |
void updateCTM(const SetMatrix& op) { fCTM = op.matrix; } |
| 288 |
void updateCTM(const SetM44& op) { fCTM = op.matrix.asM33(); } |
| 289 |
void updateCTM(const Concat44& op) { fCTM.preConcat(op.matrix.asM33()); } |
| 290 |
void updateCTM(const Concat& op) { fCTM.preConcat(op.matrix); } |
| 291 |
void updateCTM(const Scale& op) { fCTM.preScale(op.sx, op.sy); } |
| 292 |
void updateCTM(const Translate& op) { fCTM.preTranslate(op.dx, op.dy); } |
| 293 |
|
| 294 |
// The bounds of these ops must be calculated when we hit the Restore |
| 295 |
// from the bounds of the ops in the same Save block. |
| 296 |
void trackBounds(const Save&) { |
| 297 |
this->pushSaveBlock(nullptr, /*hasBackdropFilter=*/false); |
| 298 |
} |
| 299 |
void trackBounds(const SaveLayer& op) { |
| 300 |
this->pushSaveBlock(op.paint, /*hasBackdropFilter=*/op.backdrop != nullptr); |
| 301 |
} |
| 302 |
void trackBounds(const SaveBehind&) { |
| 303 |
this->pushSaveBlock(nullptr, /*hasBackdropFilter=*/false); |
| 304 |
} |
| 305 |
void trackBounds(const Restore&) { |
| 306 |
const bool isSaveLayer = fSaveStack.back().paint != nullptr; |
| 307 |
fBounds[fCurrentOp] = this->popSaveBlock(); |
| 308 |
fMeta [fCurrentOp].isDraw = isSaveLayer; |
| 309 |
} |
| 310 |
|
| 311 |
void trackBounds(const SetMatrix&) { this->pushControl(); } |
| 312 |
void trackBounds(const SetM44&) { this->pushControl(); } |
| 313 |
void trackBounds(const Concat&) { this->pushControl(); } |
| 314 |
void trackBounds(const Concat44&) { this->pushControl(); } |
| 315 |
void trackBounds(const Scale&) { this->pushControl(); } |
| 316 |
void trackBounds(const Translate&) { this->pushControl(); } |
| 317 |
void trackBounds(const ClipRect&) { this->pushControl(); } |
| 318 |
void trackBounds(const ClipRRect&) { this->pushControl(); } |
| 319 |
void trackBounds(const ClipPath&) { this->pushControl(); } |
| 320 |
void trackBounds(const ClipRegion&) { this->pushControl(); } |
| 321 |
void trackBounds(const ClipShader&) { this->pushControl(); } |
| 322 |
void trackBounds(const ResetClip&) { this->pushControl(); } |
| 323 |
|
| 324 |
|
| 325 |
// For all other ops, we can calculate and store the bounds directly now. |
| 326 |
template <typename T> void trackBounds(const T& op) { |
| 327 |
fBounds[fCurrentOp] = this->bounds(op); |
| 328 |
fMeta [fCurrentOp].isDraw = true; |
| 329 |
this->updateSaveBounds(fBounds[fCurrentOp]); |
| 330 |
} |
| 331 |
|
| 332 |
void pushSaveBlock(const SkPaint* paint, bool hasBackdropFilter) { |
| 333 |
// Starting a new Save block. Push a new entry to represent that. |
| 334 |
SaveBounds sb; |
| 335 |
sb.controlOps = 0; |
| 336 |
|
| 337 |
// If the paint affects transparent black, or we have a backdrop filter, |
| 338 |
// the bound shouldn't be smaller than the cull. |
| 339 |
bool affectsFullCullRect = hasBackdropFilter || PaintMayAffectTransparentBlack(paint); |
| 340 |
sb.bounds = affectsFullCullRect ? fCullRect : Bounds::MakeEmpty(); |
| 341 |
sb.paint = paint; |
| 342 |
sb.ctm = this->fCTM; |
| 343 |
|
| 344 |
fSaveStack.push_back(sb); |
| 345 |
this->pushControl(); |
| 346 |
} |
| 347 |
|
| 348 |
static bool PaintMayAffectTransparentBlack(const SkPaint* paint) { |
| 349 |
if (paint) { |
| 350 |
// FIXME: this is very conservative |
| 351 |
if ((paint->getImageFilter() && |
| 352 |
as_IFB(paint->getImageFilter())->affectsTransparentBlack()) || |
| 353 |
(paint->getColorFilter() && |
| 354 |
as_CFB(paint->getColorFilter())->affectsTransparentBlack())) { |
| 355 |
return true; |
| 356 |
} |
| 357 |
const auto bm = paint->asBlendMode(); |
| 358 |
if (!bm) { |
| 359 |
return true; // can we query other blenders for this? |
| 360 |
} |
| 361 |
|
| 362 |
// Unusual blendmodes require us to process a saved layer |
| 363 |
// even with operations outisde the clip. |
| 364 |
// For example, DstIn is used by masking layers. |
| 365 |
// https://code.google.com/p/skia/issues/detail?id=1291 |
| 366 |
// https://crbug.com/401593 |
| 367 |
switch (bm.value()) { |
| 368 |
// For each of the following transfer modes, if the source |
| 369 |
// alpha is zero (our transparent black), the resulting |
| 370 |
// blended alpha is not necessarily equal to the original |
| 371 |
// destination alpha. |
| 372 |
case SkBlendMode::kClear: |
| 373 |
case SkBlendMode::kSrc: |
| 374 |
case SkBlendMode::kSrcIn: |
| 375 |
case SkBlendMode::kDstIn: |
| 376 |
case SkBlendMode::kSrcOut: |
| 377 |
case SkBlendMode::kDstATop: |
| 378 |
case SkBlendMode::kModulate: |
| 379 |
return true; |
| 380 |
default: |
| 381 |
break; |
| 382 |
} |
| 383 |
} |
| 384 |
return false; |
| 385 |
} |
| 386 |
|
| 387 |
Bounds popSaveBlock() { |
| 388 |
// We're done the Save block. Apply the block's bounds to all control ops inside it. |
| 389 |
SaveBounds sb = fSaveStack.back(); |
| 390 |
fSaveStack.pop_back(); |
| 391 |
|
| 392 |
while (sb.controlOps --> 0) { |
| 393 |
this->popControl(sb.bounds); |
| 394 |
} |
| 395 |
|
| 396 |
// This whole Save block may be part another Save block. |
| 397 |
this->updateSaveBounds(sb.bounds); |
| 398 |
|
| 399 |
// If called from a real Restore (not a phony one for balance), it'll need the bounds. |
| 400 |
return sb.bounds; |
| 401 |
} |
| 402 |
|
| 403 |
void pushControl() { |
| 404 |
fControlIndices.push_back(fCurrentOp); |
| 405 |
if (!fSaveStack.empty()) { |
| 406 |
fSaveStack.back().controlOps++; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
void popControl(const Bounds& bounds) { |
| 411 |
fBounds[fControlIndices.back()] = bounds; |
| 412 |
fMeta [fControlIndices.back()].isDraw = false; |
| 413 |
fControlIndices.pop_back(); |
| 414 |
} |
| 415 |
|
| 416 |
void updateSaveBounds(const Bounds& bounds) { |
| 417 |
// If we're in a Save block, expand its bounds to cover these bounds too. |
| 418 |
if (!fSaveStack.empty()) { |
| 419 |
fSaveStack.back().bounds.join(bounds); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
Bounds bounds(const DrawPaint&) const { return fCullRect; } |
| 424 |
Bounds bounds(const DrawBehind&) const { return fCullRect; } |
| 425 |
Bounds bounds(const NoOp&) const { return Bounds::MakeEmpty(); } // NoOps don't draw. |
| 426 |
|
| 427 |
Bounds bounds(const DrawRect& op) const { return this->adjustAndMap(op.rect, &op.paint); } |
| 428 |
Bounds bounds(const DrawRegion& op) const { |
| 429 |
SkRect rect = SkRect::Make(op.region.getBounds()); |
| 430 |
return this->adjustAndMap(rect, &op.paint); |
| 431 |
} |
| 432 |
Bounds bounds(const DrawOval& op) const { return this->adjustAndMap(op.oval, &op.paint); } |
| 433 |
// Tighter arc bounds? |
| 434 |
Bounds bounds(const DrawArc& op) const { return this->adjustAndMap(op.oval, &op.paint); } |
| 435 |
Bounds bounds(const DrawRRect& op) const { |
| 436 |
return this->adjustAndMap(op.rrect.rect(), &op.paint); |
| 437 |
} |
| 438 |
Bounds bounds(const DrawDRRect& op) const { |
| 439 |
return this->adjustAndMap(op.outer.rect(), &op.paint); |
| 440 |
} |
| 441 |
Bounds bounds(const DrawImage& op) const { |
| 442 |
const SkImage* image = op.image.get(); |
| 443 |
SkRect rect = SkRect::MakeXYWH(op.left, op.top, image->width(), image->height()); |
| 444 |
|
| 445 |
return this->adjustAndMap(rect, op.paint); |
| 446 |
} |
| 447 |
Bounds bounds(const DrawImageLattice& op) const { |
| 448 |
return this->adjustAndMap(op.dst, op.paint); |
| 449 |
} |
| 450 |
Bounds bounds(const DrawImageRect& op) const { |
| 451 |
return this->adjustAndMap(op.dst, op.paint); |
| 452 |
} |
| 453 |
Bounds bounds(const DrawPath& op) const { |
| 454 |
return op.path.isInverseFillType() ? fCullRect |
| 455 |
: this->adjustAndMap(op.path.getBounds(), &op.paint); |
| 456 |
} |
| 457 |
Bounds bounds(const DrawPoints& op) const { |
| 458 |
SkRect dst = SkRect::BoundsOrEmpty({op.pts.data(), op.count}); |
| 459 |
|
| 460 |
// Pad the bounding box a little to make sure hairline points' bounds aren't empty. |
| 461 |
SkScalar stroke = std::max(op.paint.getStrokeWidth(), 0.01f); |
| 462 |
dst.outset(stroke/2, stroke/2); |
| 463 |
|
| 464 |
return this->adjustAndMap(dst, &op.paint); |
| 465 |
} |
| 466 |
Bounds bounds(const DrawPatch& op) const { |
| 467 |
const auto dst = SkRect::BoundsOrEmpty({op.cubics.data(), (size_t)SkPatchUtils::kNumCtrlPts}); |
| 468 |
return this->adjustAndMap(dst, &op.paint); |
| 469 |
} |
| 470 |
Bounds bounds(const DrawVertices& op) const { |
| 471 |
return this->adjustAndMap(op.vertices->bounds(), &op.paint); |
| 472 |
} |
| 473 |
Bounds bounds(const DrawMesh& op) const { |
| 474 |
return this->adjustAndMap(op.mesh.bounds(), &op.paint); |
| 475 |
} |
| 476 |
Bounds bounds(const DrawAtlas& op) const { |
| 477 |
if (op.cull) { |
| 478 |
// TODO: <reed> can we pass nullptr for the paint? Isn't cull already "correct" |
| 479 |
// for the paint (by the caller)? |
| 480 |
return this->adjustAndMap(*op.cull, op.paint); |
| 481 |
} else { |
| 482 |
return fCullRect; |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
Bounds bounds(const DrawShadowRec& op) const { |
| 487 |
SkRect bounds; |
| 488 |
SkDrawShadowMetrics::GetLocalBounds(op.path, op.rec, fCTM, &bounds); |
| 489 |
return this->adjustAndMap(bounds, nullptr); |
| 490 |
} |
| 491 |
|
| 492 |
Bounds bounds(const DrawPicture& op) const { |
| 493 |
SkRect dst = op.picture->cullRect(); |
| 494 |
op.matrix.mapRect(&dst); |
| 495 |
return this->adjustAndMap(dst, op.paint); |
| 496 |
} |
| 497 |
|
| 498 |
Bounds bounds(const DrawTextBlob& op) const { |
| 499 |
SkRect dst = op.blob->bounds(); |
| 500 |
dst.offset(op.x, op.y); |
| 501 |
return this->adjustAndMap(dst, &op.paint); |
| 502 |
} |
| 503 |
|
| 504 |
Bounds bounds(const DrawSlug& op) const { |
| 505 |
SkRect dst = op.slug->sourceBoundsWithOrigin(); |
| 506 |
return this->adjustAndMap(dst, &op.paint); |
| 507 |
} |
| 508 |
|
| 509 |
Bounds bounds(const DrawDrawable& op) const { |
| 510 |
return this->adjustAndMap(op.worstCaseBounds, nullptr); |
| 511 |
} |
| 512 |
|
| 513 |
Bounds bounds(const DrawAnnotation& op) const { |
| 514 |
return this->adjustAndMap(op.rect, nullptr); |
| 515 |
} |
| 516 |
Bounds bounds(const DrawEdgeAAQuad& op) const { |
| 517 |
const auto bounds = op.clip ? SkRect::BoundsOrEmpty({op.clip.data(), 4}) : op.rect; |
| 518 |
return this->adjustAndMap(bounds, nullptr); |
| 519 |
} |
| 520 |
Bounds bounds(const DrawEdgeAAImageSet& op) const { |
| 521 |
SkRect rect = SkRect::MakeEmpty(); |
| 522 |
int clipIndex = 0; |
| 523 |
for (int i = 0; i < op.count; ++i) { |
| 524 |
SkRect entryBounds = op.set[i].fDstRect; |
| 525 |
if (op.set[i].fHasClip) { |
| 526 |
entryBounds = SkRect::BoundsOrEmpty({op.dstClips + clipIndex, 4}); |
| 527 |
clipIndex += 4; |
| 528 |
} |
| 529 |
if (op.set[i].fMatrixIndex >= 0) { |
| 530 |
op.preViewMatrices[op.set[i].fMatrixIndex].mapRect(&entryBounds); |
| 531 |
} |
| 532 |
rect.join(this->adjustAndMap(entryBounds, nullptr)); |
| 533 |
} |
| 534 |
return rect; |
| 535 |
} |
| 536 |
|
| 537 |
// Returns true if rect was meaningfully adjusted for the effects of paint, |
| 538 |
// false if the paint could affect the rect in unknown ways. |
| 539 |
static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) { |
| 540 |
if (paint) { |
| 541 |
if (paint->canComputeFastBounds()) { |
| 542 |
*rect = paint->computeFastBounds(*rect, rect); |
| 543 |
return true; |
| 544 |
} |
| 545 |
return false; |
| 546 |
} |
| 547 |
return true; |
| 548 |
} |
| 549 |
|
| 550 |
bool adjustForSaveLayerPaints(SkRect* rect, int savesToIgnore = 0) const { |
| 551 |
for (int i = fSaveStack.size() - 1 - savesToIgnore; i >= 0; i--) { |
| 552 |
auto inverse = fSaveStack[i].ctm.invert(); |
| 553 |
if (!inverse) { |
| 554 |
return false; |
| 555 |
} |
| 556 |
inverse->mapRect(rect); |
| 557 |
if (!AdjustForPaint(fSaveStack[i].paint, rect)) { |
| 558 |
return false; |
| 559 |
} |
| 560 |
fSaveStack[i].ctm.mapRect(rect); |
| 561 |
} |
| 562 |
return true; |
| 563 |
} |
| 564 |
|
| 565 |
// We do not guarantee anything for operations outside of the cull rect |
| 566 |
const SkRect fCullRect; |
| 567 |
|
| 568 |
// Conservative identity-space bounds for each op in the SkRecord. |
| 569 |
Bounds* fBounds; |
| 570 |
|
| 571 |
// Parallel array to fBounds, holding metadata for each bounds rect. |
| 572 |
SkBBoxHierarchy::Metadata* fMeta; |
| 573 |
|
| 574 |
// We walk fCurrentOp through the SkRecord, |
| 575 |
// as we go using updateCTM() to maintain the exact CTM (fCTM). |
| 576 |
int fCurrentOp; |
| 577 |
SkMatrix fCTM; |
| 578 |
|
| 579 |
// Used to track the bounds of Save/Restore blocks and the control ops inside them. |
| 580 |
SkTDArray<SaveBounds> fSaveStack; |
| 581 |
SkTDArray<int> fControlIndices; |
| 582 |
}; |
| 583 |
|
| 584 |
} // namespace SkRecords |
| 585 |
|
| 586 |
void SkRecordFillBounds(const SkRect& cullRect, const SkRecord& record, |
| 587 |
SkRect bounds[], SkBBoxHierarchy::Metadata meta[]) { |
| 588 |
{ |
| 589 |
SkRecords::FillBounds visitor(cullRect, record, bounds, meta); |
| 590 |
for (int i = 0; i < record.count(); i++) { |
| 591 |
visitor.setCurrentOp(i); |
| 592 |
record.visit(i, visitor); |
| 593 |
} |
| 594 |
} |
| 595 |
} |