blob: f7f02997dfb149045f98722546e1ec75fcb45184 [file] [log] [blame]
commit-bot@chromium.orgc4b21e62014-04-11 18:33:31 +00001/*
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
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +00008#include "SkRecordDraw.h"
mtklein131a22b2014-08-25 14:16:15 -07009#include "SkPatchUtils.h"
commit-bot@chromium.org506db0b2014-04-08 23:31:35 +000010
mtklein5ad6ee12014-08-11 08:08:43 -070011void SkRecordDraw(const SkRecord& record,
12 SkCanvas* canvas,
13 const SkBBoxHierarchy* bbh,
14 SkDrawPictureCallback* callback) {
Mike Kleinc11530e2014-06-24 11:29:06 -040015 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/);
mtklein5ad6ee12014-08-11 08:08:43 -070016
17 if (NULL != bbh) {
mtklein5ad6ee12014-08-11 08:08:43 -070018 // Draw only ops that affect pixels in the canvas's current clip.
mtklein3e8232b2014-08-18 13:39:11 -070019 // The SkRecord and BBH were recorded in identity space. This canvas
20 // is not necessarily in that same space. getClipBounds() returns us
21 // this canvas' clip bounds transformed back into identity space, which
22 // lets us query the BBH.
mtklein533eb782014-08-27 10:39:42 -070023 SkRect query = { 0, 0, 0, 0 };
24 (void)canvas->getClipBounds(&query);
mtklein3e8232b2014-08-18 13:39:11 -070025
mtklein5ad6ee12014-08-11 08:08:43 -070026 SkTDArray<void*> ops;
mtkleina723b572014-08-15 11:49:49 -070027 bbh->search(query, &ops);
mtklein5ad6ee12014-08-11 08:08:43 -070028
mtklein5ad6ee12014-08-11 08:08:43 -070029 SkRecords::Draw draw(canvas);
30 for (int i = 0; i < ops.count(); i++) {
31 if (NULL != callback && callback->abortDrawing()) {
32 return;
33 }
34 record.visit<void>((uintptr_t)ops[i], draw); // See FillBounds below.
35 }
36 } else {
37 // Draw all ops.
mtklein00f30bd2014-09-02 12:03:31 -070038 SkRecords::Draw draw(canvas);
39 for (unsigned i = 0; i < record.count(); i++) {
mtklein5ad6ee12014-08-11 08:08:43 -070040 if (NULL != callback && callback->abortDrawing()) {
41 return;
42 }
mtklein00f30bd2014-09-02 12:03:31 -070043 record.visit<void>(i, draw);
mtklein5ad6ee12014-08-11 08:08:43 -070044 }
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000045 }
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000046}
commit-bot@chromium.orgd9ce2be2014-04-09 23:30:28 +000047
mtklein00f30bd2014-09-02 12:03:31 -070048void SkRecordPartialDraw(const SkRecord& record,
49 SkCanvas* canvas,
50 const SkRect& clearRect,
51 unsigned start, unsigned stop) {
52 SkAutoCanvasRestore saveRestore(canvas, true /*save now, restore at exit*/);
53
54 stop = SkTMin(stop, record.count());
55 SkRecords::PartialDraw draw(canvas, clearRect);
56 for (unsigned i = start; i < stop; i++) {
57 record.visit<void>(i, draw);
58 }
59}
60
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000061namespace SkRecords {
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000062
mtklein7cdc1ee2014-07-07 10:41:04 -070063// FIXME: SkBitmaps are stateful, so we need to copy them to play back in multiple threads.
64static SkBitmap shallow_copy(const SkBitmap& bitmap) {
65 return bitmap;
66}
67
commit-bot@chromium.org2e0c32a2014-04-28 16:19:45 +000068// NoOps draw nothing.
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000069template <> void Draw::draw(const NoOp&) {}
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000070
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +000071#define DRAW(T, call) template <> void Draw::draw(const T& r) { fCanvas->call; }
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000072DRAW(Restore, restore());
Florin Malita5f6102d2014-06-30 10:13:28 -040073DRAW(Save, save());
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000074DRAW(SaveLayer, saveLayer(r.bounds, r.paint, r.flags));
75DRAW(PopCull, popCull());
commit-bot@chromium.org88c3e272014-04-22 16:57:20 +000076DRAW(PushCull, pushCull(r.rect));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000077DRAW(Clear, clear(r.color));
commit-bot@chromium.org99bd7d82014-05-19 15:51:12 +000078DRAW(SetMatrix, setMatrix(SkMatrix::Concat(fInitialCTM, r.matrix)));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000079
80DRAW(ClipPath, clipPath(r.path, r.op, r.doAA));
81DRAW(ClipRRect, clipRRect(r.rrect, r.op, r.doAA));
82DRAW(ClipRect, clipRect(r.rect, r.op, r.doAA));
83DRAW(ClipRegion, clipRegion(r.region, r.op));
84
mtklein5f0e8222014-08-22 11:44:26 -070085DRAW(BeginCommentGroup, beginCommentGroup(r.description));
86DRAW(AddComment, addComment(r.key, r.value));
87DRAW(EndCommentGroup, endCommentGroup());
88
mtklein7cdc1ee2014-07-07 10:41:04 -070089DRAW(DrawBitmap, drawBitmap(shallow_copy(r.bitmap), r.left, r.top, r.paint));
90DRAW(DrawBitmapMatrix, drawBitmapMatrix(shallow_copy(r.bitmap), r.matrix, r.paint));
91DRAW(DrawBitmapNine, drawBitmapNine(shallow_copy(r.bitmap), r.center, r.dst, r.paint));
92DRAW(DrawBitmapRectToRect,
93 drawBitmapRectToRect(shallow_copy(r.bitmap), r.src, r.dst, r.paint, r.flags));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +000094DRAW(DrawDRRect, drawDRRect(r.outer, r.inner, r.paint));
95DRAW(DrawOval, drawOval(r.oval, r.paint));
96DRAW(DrawPaint, drawPaint(r.paint));
97DRAW(DrawPath, drawPath(r.path, r.paint));
dandovb3c9d1c2014-08-12 08:34:29 -070098DRAW(DrawPatch, drawPatch(r.cubics, r.colors, r.texCoords, r.xmode.get(), r.paint));
reedd5fa1a42014-08-09 11:08:05 -070099DRAW(DrawPicture, drawPicture(r.picture, r.matrix, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000100DRAW(DrawPoints, drawPoints(r.mode, r.count, r.pts, r.paint));
101DRAW(DrawPosText, drawPosText(r.text, r.byteLength, r.pos, r.paint));
102DRAW(DrawPosTextH, drawPosTextH(r.text, r.byteLength, r.xpos, r.y, r.paint));
103DRAW(DrawRRect, drawRRect(r.rrect, r.paint));
104DRAW(DrawRect, drawRect(r.rect, r.paint));
mtklein7cdc1ee2014-07-07 10:41:04 -0700105DRAW(DrawSprite, drawSprite(shallow_copy(r.bitmap), r.left, r.top, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000106DRAW(DrawText, drawText(r.text, r.byteLength, r.x, r.y, r.paint));
fmalita00d5c2c2014-08-21 08:53:26 -0700107DRAW(DrawTextBlob, drawTextBlob(r.blob, r.x, r.y, r.paint));
commit-bot@chromium.org73b55eb2014-04-14 20:35:12 +0000108DRAW(DrawTextOnPath, drawTextOnPath(r.text, r.byteLength, r.path, r.matrix, r.paint));
109DRAW(DrawVertices, drawVertices(r.vmode, r.vertexCount, r.vertices, r.texs, r.colors,
110 r.xmode.get(), r.indices, r.indexCount, r.paint));
111#undef DRAW
112
mtklein5ad6ee12014-08-11 08:08:43 -0700113
114// This is an SkRecord visitor that fills an SkBBoxHierarchy.
mtklein828ce1f2014-08-13 12:58:45 -0700115//
116// The interesting part here is how to calculate bounds for ops which don't
117// have intrinsic bounds. What is the bounds of a Save or a Translate?
118//
119// We answer this by thinking about a particular definition of bounds: if I
120// don't execute this op, pixels in this rectangle might draw incorrectly. So
121// the bounds of a Save, a Translate, a Restore, etc. are the union of the
122// bounds of Draw* ops that they might have an effect on. For any given
123// Save/Restore block, the bounds of the Save, the Restore, and any other
124// non-drawing ("control") ops inside are exactly the union of the bounds of
125// the drawing ops inside that block.
126//
127// To implement this, we keep a stack of active Save blocks. As we consume ops
128// inside the Save/Restore block, drawing ops are unioned with the bounds of
129// the block, and control ops are stashed away for later. When we finish the
130// block with a Restore, our bounds are complete, and we go back and fill them
131// in for all the control ops we stashed away.
mtklein5ad6ee12014-08-11 08:08:43 -0700132class FillBounds : SkNoncopyable {
133public:
mtklein828ce1f2014-08-13 12:58:45 -0700134 FillBounds(const SkRecord& record, SkBBoxHierarchy* bbh) : fBounds(record.count()) {
135 // Calculate bounds for all ops. This won't go quite in order, so we'll need
136 // to store the bounds separately then feed them in to the BBH later in order.
mtklein533eb782014-08-27 10:39:42 -0700137 const Bounds largest = Bounds::MakeLargest();
mtklein6332f1d2014-08-19 07:09:40 -0700138 fCTM = &SkMatrix::I();
mtkleina723b572014-08-15 11:49:49 -0700139 fCurrentClipBounds = largest;
mtklein828ce1f2014-08-13 12:58:45 -0700140 for (fCurrentOp = 0; fCurrentOp < record.count(); fCurrentOp++) {
141 record.visit<void>(fCurrentOp, *this);
142 }
mtklein5ad6ee12014-08-11 08:08:43 -0700143
mtklein828ce1f2014-08-13 12:58:45 -0700144 // If we have any lingering unpaired Saves, simulate restores to make
145 // sure all ops in those Save blocks have their bounds calculated.
146 while (!fSaveStack.isEmpty()) {
147 this->popSaveBlock();
148 }
149
150 // Any control ops not part of any Save/Restore block draw everywhere.
151 while (!fControlIndices.isEmpty()) {
mtkleina723b572014-08-15 11:49:49 -0700152 this->popControl(largest);
mtklein828ce1f2014-08-13 12:58:45 -0700153 }
154
155 // Finally feed all stored bounds into the BBH. They'll be returned in this order.
156 SkASSERT(NULL != bbh);
157 for (uintptr_t i = 0; i < record.count(); i++) {
158 if (!fBounds[i].isEmpty()) {
159 bbh->insert((void*)i, fBounds[i], true/*ok to defer*/);
160 }
161 }
162 bbh->flushDeferredInserts();
163 }
mtklein5ad6ee12014-08-11 08:08:43 -0700164
mtkleina723b572014-08-15 11:49:49 -0700165 template <typename T> void operator()(const T& op) {
166 this->updateCTM(op);
167 this->updateClipBounds(op);
168 this->trackBounds(op);
mtklein5ad6ee12014-08-11 08:08:43 -0700169 }
170
171private:
mtklein533eb782014-08-27 10:39:42 -0700172 // In this file, SkRect are in local coordinates, Bounds are translated back to identity space.
173 typedef SkRect Bounds;
174
mtklein828ce1f2014-08-13 12:58:45 -0700175 struct SaveBounds {
mtkleina723b572014-08-15 11:49:49 -0700176 int controlOps; // Number of control ops in this Save block, including the Save.
mtklein533eb782014-08-27 10:39:42 -0700177 Bounds bounds; // Bounds of everything in the block.
mtkleina723b572014-08-15 11:49:49 -0700178 const SkPaint* paint; // Unowned. If set, adjusts the bounds of all ops in this block.
mtklein828ce1f2014-08-13 12:58:45 -0700179 };
180
mtklein6cfa73a2014-08-13 13:33:49 -0700181 template <typename T> void updateCTM(const T&) { /* most ops don't change the CTM */ }
mtklein6332f1d2014-08-19 07:09:40 -0700182 void updateCTM(const Restore& op) { fCTM = &op.matrix; }
183 void updateCTM(const SetMatrix& op) { fCTM = &op.matrix; }
mtkleina723b572014-08-15 11:49:49 -0700184
185 template <typename T> void updateClipBounds(const T&) { /* most ops don't change the clip */ }
186 // Each of these devBounds fields is the state of the device bounds after the op.
187 // So Restore's devBounds are those bounds saved by its paired Save or SaveLayer.
mtklein533eb782014-08-27 10:39:42 -0700188 void updateClipBounds(const Restore& op) { fCurrentClipBounds = Bounds::Make(op.devBounds); }
189 void updateClipBounds(const ClipPath& op) { fCurrentClipBounds = Bounds::Make(op.devBounds); }
190 void updateClipBounds(const ClipRRect& op) { fCurrentClipBounds = Bounds::Make(op.devBounds); }
191 void updateClipBounds(const ClipRect& op) { fCurrentClipBounds = Bounds::Make(op.devBounds); }
192 void updateClipBounds(const ClipRegion& op) { fCurrentClipBounds = Bounds::Make(op.devBounds); }
mtkleina723b572014-08-15 11:49:49 -0700193 void updateClipBounds(const SaveLayer& op) {
194 if (op.bounds) {
195 fCurrentClipBounds.intersect(this->adjustAndMap(*op.bounds, op.paint));
196 }
197 }
mtklein6cfa73a2014-08-13 13:33:49 -0700198
mtklein828ce1f2014-08-13 12:58:45 -0700199 // The bounds of these ops must be calculated when we hit the Restore
200 // from the bounds of the ops in the same Save block.
mtkleina723b572014-08-15 11:49:49 -0700201 void trackBounds(const Save&) { this->pushSaveBlock(NULL); }
mtkleina723b572014-08-15 11:49:49 -0700202 void trackBounds(const SaveLayer& op) { this->pushSaveBlock(op.paint); }
203 void trackBounds(const Restore&) { fBounds[fCurrentOp] = this->popSaveBlock(); }
mtklein828ce1f2014-08-13 12:58:45 -0700204
mtklein68199a22014-08-25 13:49:29 -0700205 void trackBounds(const SetMatrix&) { this->pushControl(); }
206 void trackBounds(const ClipRect&) { this->pushControl(); }
207 void trackBounds(const ClipRRect&) { this->pushControl(); }
208 void trackBounds(const ClipPath&) { this->pushControl(); }
209 void trackBounds(const ClipRegion&) { this->pushControl(); }
210 void trackBounds(const PushCull&) { this->pushControl(); }
211 void trackBounds(const PopCull&) { this->pushControl(); }
212 void trackBounds(const BeginCommentGroup&) { this->pushControl(); }
213 void trackBounds(const AddComment&) { this->pushControl(); }
214 void trackBounds(const EndCommentGroup&) { this->pushControl(); }
mtklein828ce1f2014-08-13 12:58:45 -0700215
216 // For all other ops, we can calculate and store the bounds directly now.
217 template <typename T> void trackBounds(const T& op) {
218 fBounds[fCurrentOp] = this->bounds(op);
219 this->updateSaveBounds(fBounds[fCurrentOp]);
mtklein5ad6ee12014-08-11 08:08:43 -0700220 }
221
mtkleina723b572014-08-15 11:49:49 -0700222 void pushSaveBlock(const SkPaint* paint) {
mtklein828ce1f2014-08-13 12:58:45 -0700223 // Starting a new Save block. Push a new entry to represent that.
mtklein533eb782014-08-27 10:39:42 -0700224 SaveBounds sb = { 0, Bounds::MakeEmpty(), paint };
mtklein828ce1f2014-08-13 12:58:45 -0700225 fSaveStack.push(sb);
226 this->pushControl();
227 }
228
mtkleind910f542014-08-22 09:06:34 -0700229 static bool PaintMayAffectTransparentBlack(const SkPaint* paint) {
230 // FIXME: this is very conservative
231 return paint && (paint->getImageFilter() || paint->getColorFilter());
232 }
233
mtklein533eb782014-08-27 10:39:42 -0700234 Bounds popSaveBlock() {
mtklein828ce1f2014-08-13 12:58:45 -0700235 // We're done the Save block. Apply the block's bounds to all control ops inside it.
236 SaveBounds sb;
237 fSaveStack.pop(&sb);
mtkleind910f542014-08-22 09:06:34 -0700238
239 // If the paint affects transparent black, we can't trust any of our calculated bounds.
mtklein533eb782014-08-27 10:39:42 -0700240 const Bounds& bounds =
mtkleind910f542014-08-22 09:06:34 -0700241 PaintMayAffectTransparentBlack(sb.paint) ? fCurrentClipBounds : sb.bounds;
242
mtklein828ce1f2014-08-13 12:58:45 -0700243 while (sb.controlOps --> 0) {
mtkleind910f542014-08-22 09:06:34 -0700244 this->popControl(bounds);
mtklein828ce1f2014-08-13 12:58:45 -0700245 }
246
247 // This whole Save block may be part another Save block.
mtkleind910f542014-08-22 09:06:34 -0700248 this->updateSaveBounds(bounds);
mtklein828ce1f2014-08-13 12:58:45 -0700249
250 // If called from a real Restore (not a phony one for balance), it'll need the bounds.
mtkleind910f542014-08-22 09:06:34 -0700251 return bounds;
mtklein828ce1f2014-08-13 12:58:45 -0700252 }
253
254 void pushControl() {
255 fControlIndices.push(fCurrentOp);
256 if (!fSaveStack.isEmpty()) {
257 fSaveStack.top().controlOps++;
258 }
259 }
260
mtklein533eb782014-08-27 10:39:42 -0700261 void popControl(const Bounds& bounds) {
mtklein828ce1f2014-08-13 12:58:45 -0700262 fBounds[fControlIndices.top()] = bounds;
263 fControlIndices.pop();
264 }
265
mtklein533eb782014-08-27 10:39:42 -0700266 void updateSaveBounds(const Bounds& bounds) {
mtklein828ce1f2014-08-13 12:58:45 -0700267 // If we're in a Save block, expand its bounds to cover these bounds too.
268 if (!fSaveStack.isEmpty()) {
269 fSaveStack.top().bounds.join(bounds);
270 }
271 }
272
mtklein131a22b2014-08-25 14:16:15 -0700273 // FIXME: this method could use better bounds
mtklein533eb782014-08-27 10:39:42 -0700274 Bounds bounds(const DrawText&) const { return fCurrentClipBounds; }
mtklein68199a22014-08-25 13:49:29 -0700275
mtklein533eb782014-08-27 10:39:42 -0700276 Bounds bounds(const Clear&) const { return Bounds::MakeLargest(); } // Ignores the clip.
277 Bounds bounds(const DrawPaint&) const { return fCurrentClipBounds; }
278 Bounds bounds(const NoOp&) const { return Bounds::MakeEmpty(); } // NoOps don't draw.
mtklein828ce1f2014-08-13 12:58:45 -0700279
mtklein533eb782014-08-27 10:39:42 -0700280 Bounds bounds(const DrawSprite& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700281 const SkBitmap& bm = op.bitmap;
mtklein533eb782014-08-27 10:39:42 -0700282 return Bounds::MakeXYWH(op.left, op.top, bm.width(), bm.height()); // Ignores the matrix.
mtklein131a22b2014-08-25 14:16:15 -0700283 }
284
mtklein533eb782014-08-27 10:39:42 -0700285 Bounds bounds(const DrawRect& op) const { return this->adjustAndMap(op.rect, &op.paint); }
286 Bounds bounds(const DrawOval& op) const { return this->adjustAndMap(op.oval, &op.paint); }
287 Bounds bounds(const DrawRRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700288 return this->adjustAndMap(op.rrect.rect(), &op.paint);
289 }
mtklein533eb782014-08-27 10:39:42 -0700290 Bounds bounds(const DrawDRRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700291 return this->adjustAndMap(op.outer.rect(), &op.paint);
292 }
293
mtklein533eb782014-08-27 10:39:42 -0700294 Bounds bounds(const DrawBitmapRectToRect& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700295 return this->adjustAndMap(op.dst, op.paint);
296 }
mtklein533eb782014-08-27 10:39:42 -0700297 Bounds bounds(const DrawBitmapNine& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700298 return this->adjustAndMap(op.dst, op.paint);
299 }
mtklein533eb782014-08-27 10:39:42 -0700300 Bounds bounds(const DrawBitmap& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700301 const SkBitmap& bm = op.bitmap;
302 return this->adjustAndMap(SkRect::MakeXYWH(op.left, op.top, bm.width(), bm.height()),
303 op.paint);
304 }
mtklein533eb782014-08-27 10:39:42 -0700305 Bounds bounds(const DrawBitmapMatrix& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700306 const SkBitmap& bm = op.bitmap;
307 SkRect dst = SkRect::MakeWH(bm.width(), bm.height());
308 op.matrix.mapRect(&dst);
309 return this->adjustAndMap(dst, op.paint);
310 }
311
mtklein533eb782014-08-27 10:39:42 -0700312 Bounds bounds(const DrawPath& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700313 return op.path.isInverseFillType() ? fCurrentClipBounds
314 : this->adjustAndMap(op.path.getBounds(), &op.paint);
315 }
mtklein533eb782014-08-27 10:39:42 -0700316 Bounds bounds(const DrawPoints& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700317 SkRect dst;
318 dst.set(op.pts, op.count);
319
320 // Pad the bounding box a little to make sure hairline points' bounds aren't empty.
321 SkScalar stroke = SkMaxScalar(op.paint.getStrokeWidth(), 0.01f);
322 dst.outset(stroke/2, stroke/2);
323
324 return this->adjustAndMap(dst, &op.paint);
325 }
mtklein533eb782014-08-27 10:39:42 -0700326 Bounds bounds(const DrawPatch& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700327 SkRect dst;
328 dst.set(op.cubics, SkPatchUtils::kNumCtrlPts);
329 return this->adjustAndMap(dst, &op.paint);
330 }
mtklein533eb782014-08-27 10:39:42 -0700331 Bounds bounds(const DrawVertices& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700332 SkRect dst;
333 dst.set(op.vertices, op.vertexCount);
334 return this->adjustAndMap(dst, &op.paint);
335 }
336
mtklein533eb782014-08-27 10:39:42 -0700337 Bounds bounds(const DrawPicture& op) const {
robertphillipsa8d7f0b2014-08-29 08:03:56 -0700338 SkRect dst = op.picture->cullRect();
mtklein131a22b2014-08-25 14:16:15 -0700339 if (op.matrix) {
340 op.matrix->mapRect(&dst);
341 }
342 return this->adjustAndMap(dst, op.paint);
343 }
mtklein62b67ae2014-08-18 11:10:37 -0700344
mtklein533eb782014-08-27 10:39:42 -0700345 Bounds bounds(const DrawPosText& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700346 const int N = op.paint.countText(op.text, op.byteLength);
347 if (N == 0) {
mtklein533eb782014-08-27 10:39:42 -0700348 return Bounds::MakeEmpty();
mtklein62b67ae2014-08-18 11:10:37 -0700349 }
350
351 SkRect dst;
352 dst.set(op.pos, op.paint.countText(op.text, N));
353 AdjustTextForFontMetrics(&dst, op.paint);
354 return this->adjustAndMap(dst, &op.paint);
355 }
mtklein533eb782014-08-27 10:39:42 -0700356 Bounds bounds(const DrawPosTextH& op) const {
mtklein62b67ae2014-08-18 11:10:37 -0700357 const int N = op.paint.countText(op.text, op.byteLength);
358 if (N == 0) {
mtklein533eb782014-08-27 10:39:42 -0700359 return Bounds::MakeEmpty();
mtklein62b67ae2014-08-18 11:10:37 -0700360 }
361
362 SkScalar left = op.xpos[0], right = op.xpos[0];
363 for (int i = 1; i < N; i++) {
364 left = SkMinScalar(left, op.xpos[i]);
365 right = SkMaxScalar(right, op.xpos[i]);
366 }
367 SkRect dst = { left, op.y, right, op.y };
368 AdjustTextForFontMetrics(&dst, op.paint);
369 return this->adjustAndMap(dst, &op.paint);
370 }
mtklein533eb782014-08-27 10:39:42 -0700371 Bounds bounds(const DrawTextOnPath& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700372 SkRect dst = op.path.getBounds();
373
374 // Pad all sides by the maximum padding in any direction we'd normally apply.
375 SkRect pad = { 0, 0, 0, 0};
376 AdjustTextForFontMetrics(&pad, op.paint);
377
378 // That maximum padding happens to always be the right pad today.
379 SkASSERT(pad.fLeft == -pad.fRight);
380 SkASSERT(pad.fTop == -pad.fBottom);
381 SkASSERT(pad.fRight > pad.fBottom);
382 dst.outset(pad.fRight, pad.fRight);
383
384 return this->adjustAndMap(dst, &op.paint);
385 }
386
mtklein533eb782014-08-27 10:39:42 -0700387 Bounds bounds(const DrawTextBlob& op) const {
mtklein131a22b2014-08-25 14:16:15 -0700388 SkRect dst = op.blob->bounds();
389 dst.offset(op.x, op.y);
390 // TODO: remove when implicit bounds are plumbed through
391 if (dst.isEmpty()) {
392 return fCurrentClipBounds;
393 }
394 return this->adjustAndMap(dst, &op.paint);
395 }
mtklein62b67ae2014-08-18 11:10:37 -0700396
397 static void AdjustTextForFontMetrics(SkRect* rect, const SkPaint& paint) {
caryclark9a657fa2014-08-20 05:24:29 -0700398#ifdef SK_DEBUG
mtkleina19afb42014-08-19 17:47:14 -0700399 SkRect correct = *rect;
400#endif
mtkleinc8460492014-08-21 16:39:12 -0700401 const SkScalar yPad = 2.0f * paint.getTextSize(), // In practice, this seems to be enough.
mtkleina19afb42014-08-19 17:47:14 -0700402 xPad = 4.0f * yPad; // Hack for very wide Github logo font.
403 rect->outset(xPad, yPad);
caryclark9a657fa2014-08-20 05:24:29 -0700404#ifdef SK_DEBUG
mtklein62b67ae2014-08-18 11:10:37 -0700405 SkPaint::FontMetrics metrics;
406 paint.getFontMetrics(&metrics);
mtkleina19afb42014-08-19 17:47:14 -0700407 correct.fLeft += metrics.fXMin;
408 correct.fTop += metrics.fTop;
409 correct.fRight += metrics.fXMax;
410 correct.fBottom += metrics.fBottom;
mtkleind13291a2014-08-21 14:46:49 -0700411 // See skia:2862 for why we ignore small text sizes.
412 SkASSERTF(paint.getTextSize() < 0.001f || rect->contains(correct),
413 "%f %f %f %f vs. %f %f %f %f\n",
mtkleina19afb42014-08-19 17:47:14 -0700414 -xPad, -yPad, +xPad, +yPad,
415 metrics.fXMin, metrics.fTop, metrics.fXMax, metrics.fBottom);
416#endif
mtklein62b67ae2014-08-18 11:10:37 -0700417 }
418
mtklein479601b2014-08-18 08:45:33 -0700419 // Returns true if rect was meaningfully adjusted for the effects of paint,
420 // false if the paint could affect the rect in unknown ways.
421 static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) {
mtkleina723b572014-08-15 11:49:49 -0700422 if (paint) {
423 if (paint->canComputeFastBounds()) {
mtklein479601b2014-08-18 08:45:33 -0700424 *rect = paint->computeFastBounds(*rect, rect);
425 return true;
mtkleina723b572014-08-15 11:49:49 -0700426 }
mtklein479601b2014-08-18 08:45:33 -0700427 return false;
428 }
429 return true;
430 }
431
mtklein533eb782014-08-27 10:39:42 -0700432 // Adjust rect for all paints that may affect its geometry, then map it to identity space.
433 Bounds adjustAndMap(SkRect rect, const SkPaint* paint) const {
mtklein479601b2014-08-18 08:45:33 -0700434 // Inverted rectangles really confuse our BBHs.
435 rect.sort();
436
437 // Adjust the rect for its own paint.
438 if (!AdjustForPaint(paint, &rect)) {
439 // The paint could do anything to our bounds. The only safe answer is the current clip.
440 return fCurrentClipBounds;
mtkleina723b572014-08-15 11:49:49 -0700441 }
442
443 // Adjust rect for all the paints from the SaveLayers we're inside.
mtkleina723b572014-08-15 11:49:49 -0700444 for (int i = fSaveStack.count() - 1; i >= 0; i--) {
mtklein479601b2014-08-18 08:45:33 -0700445 if (!AdjustForPaint(fSaveStack[i].paint, &rect)) {
446 // Same deal as above.
447 return fCurrentClipBounds;
mtkleina723b572014-08-15 11:49:49 -0700448 }
449 }
450
mtklein533eb782014-08-27 10:39:42 -0700451 // Map the rect back to identity space.
mtklein6332f1d2014-08-19 07:09:40 -0700452 fCTM->mapRect(&rect);
mtklein479601b2014-08-18 08:45:33 -0700453
454 // Nothing can draw outside the current clip.
455 // (Only bounded ops call into this method, so oddballs like Clear don't matter here.)
mtklein533eb782014-08-27 10:39:42 -0700456 rect.intersect(fCurrentClipBounds);
457 return rect;
mtkleina723b572014-08-15 11:49:49 -0700458 }
459
mtklein533eb782014-08-27 10:39:42 -0700460 // Conservative identity-space bounds for each op in the SkRecord.
461 SkAutoTMalloc<Bounds> fBounds;
mtkleina723b572014-08-15 11:49:49 -0700462
463 // We walk fCurrentOp through the SkRecord, as we go using updateCTM()
464 // and updateClipBounds() to maintain the exact CTM (fCTM) and conservative
mtklein533eb782014-08-27 10:39:42 -0700465 // identity-space bounds of the current clip (fCurrentClipBounds).
mtklein828ce1f2014-08-13 12:58:45 -0700466 unsigned fCurrentOp;
mtklein6332f1d2014-08-19 07:09:40 -0700467 const SkMatrix* fCTM;
mtklein533eb782014-08-27 10:39:42 -0700468 Bounds fCurrentClipBounds;
mtkleina723b572014-08-15 11:49:49 -0700469
470 // Used to track the bounds of Save/Restore blocks and the control ops inside them.
mtklein828ce1f2014-08-13 12:58:45 -0700471 SkTDArray<SaveBounds> fSaveStack;
472 SkTDArray<unsigned> fControlIndices;
mtklein5ad6ee12014-08-11 08:08:43 -0700473};
474
commit-bot@chromium.org27f6b0d2014-05-09 14:59:29 +0000475} // namespace SkRecords
mtklein5ad6ee12014-08-11 08:08:43 -0700476
477void SkRecordFillBounds(const SkRecord& record, SkBBoxHierarchy* bbh) {
mtklein828ce1f2014-08-13 12:58:45 -0700478 SkRecords::FillBounds(record, bbh);
mtklein5ad6ee12014-08-11 08:08:43 -0700479}