SkRecordDraw.cpp 20 KB

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