Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(380)

Side by Side Diff: src/core/SkRecordDraw.cpp

Issue 698643002: Expose FillBounds to allow GrPictureUtils::CollectLayers to be layered on top of it (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Possible layering fix Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "SkRecordDraw.h" 8 #include "SkRecordDraw.h"
9 #include "SkPatchUtils.h" 9 #include "SkPatchUtils.h"
10 10
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 #undef DRAW 117 #undef DRAW
118 118
119 119
120 // This looks silly, I know. Why not just use SkRect::MakeLargest()? 120 // This looks silly, I know. Why not just use SkRect::MakeLargest()?
121 // In practice, this is well large enough, and it has a few extra advantages: 121 // In practice, this is well large enough, and it has a few extra advantages:
122 // it fits in an SkIRect, and we can munge it a little in both SkRect and 122 // it fits in an SkIRect, and we can munge it a little in both SkRect and
123 // SKIRect space without worrying about overflow. 123 // SKIRect space without worrying about overflow.
124 static const SkRect kUnbounded = { -2e9f, -2e9f, 2e9f, 2e9f }; 124 static const SkRect kUnbounded = { -2e9f, -2e9f, 2e9f, 2e9f };
125 125
126 126
127 // This is an SkRecord visitor that fills an SkBBoxHierarchy. 127 FillBounds::FillBounds(const SkRecord& record, SkBBoxHierarchy* bbh)
128 // 128 : fNumRecords(record.count())
129 // The interesting part here is how to calculate bounds for ops which don't 129 , fBBH(bbh)
130 // have intrinsic bounds. What is the bounds of a Save or a Translate? 130 , fBounds(record.count()) {
131 // 131 // Calculate bounds for all ops. This won't go quite in order, so we'll nee d
132 // We answer this by thinking about a particular definition of bounds: if I 132 // to store the bounds separately then feed them in to the BBH later in orde r.
133 // don't execute this op, pixels in this rectangle might draw incorrectly. So 133 fCTM = &SkMatrix::I();
134 // the bounds of a Save, a Translate, a Restore, etc. are the union of the 134 fCurrentClipBounds = kUnbounded;
135 // bounds of Draw* ops that they might have an effect on. For any given 135 }
136 // Save/Restore block, the bounds of the Save, the Restore, and any other 136
137 // non-drawing ("control") ops inside are exactly the union of the bounds of 137 void FillBounds::cleanUp() {
138 // the drawing ops inside that block. 138 // If we have any lingering unpaired Saves, simulate restores to make
139 // 139 // sure all ops in those Save blocks have their bounds calculated.
140 // To implement this, we keep a stack of active Save blocks. As we consume ops 140 while (!fSaveStack.isEmpty()) {
141 // inside the Save/Restore block, drawing ops are unioned with the bounds of 141 this->popSaveBlock();
142 // the block, and control ops are stashed away for later. When we finish the 142 }
143 // block with a Restore, our bounds are complete, and we go back and fill them 143
144 // in for all the control ops we stashed away. 144 // Any control ops not part of any Save/Restore block draw everywhere.
145 class FillBounds : SkNoncopyable { 145 while (!fControlIndices.isEmpty()) {
146 public: 146 this->popControl(kUnbounded);
147 FillBounds(const SkRecord& record, SkBBoxHierarchy* bbh) : fBounds(record.co unt()) { 147 }
148 // Calculate bounds for all ops. This won't go quite in order, so we'll need 148
149 // to store the bounds separately then feed them in to the BBH later in order. 149 // Finally feed all stored bounds into the BBH. They'll be returned in this order.
150 fCTM = &SkMatrix::I(); 150 // TODO: resume use of the assert once saveLayer collection is moved into Sk Picture ctor
151 fCurrentClipBounds = kUnbounded; 151 //SkASSERT(bbh);
152 for (fCurrentOp = 0; fCurrentOp < record.count(); fCurrentOp++) { 152 if (fBBH) {
153 record.visit<void>(fCurrentOp, *this); 153 fBBH->insert(&fBounds, fNumRecords);
154 }
155 }
156
157 // Only Restore and SetMatrix change the CTM.
158 template <typename T> void FillBounds::updateCTM(const T&) {}
159 template <> void FillBounds::updateCTM(const Restore& op) { fCTM = &op.matrix; }
160 template <> void FillBounds::updateCTM(const SetMatrix& op) { fCTM = &op.matrix; }
161
162 // Most ops don't change the clip.
163 template <typename T> void FillBounds::updateClipBounds(const T&) {}
164
165 // Clip{Path,RRect,Rect,Region} obviously change the clip. They all know their bounds already.
166 template <> void FillBounds::updateClipBounds(const ClipPath& op) { this->upda teClipBoundsForClipOp(op.devBounds); }
167 template <> void FillBounds::updateClipBounds(const ClipRRect& op) { this->upda teClipBoundsForClipOp(op.devBounds); }
168 template <> void FillBounds::updateClipBounds(const ClipRect& op) { this->upda teClipBoundsForClipOp(op.devBounds); }
169 template <> void FillBounds::updateClipBounds(const ClipRegion& op) { this->upda teClipBoundsForClipOp(op.devBounds); }
170
171 // The bounds of clip ops need to be adjusted for the paints of saveLayers they' re inside.
172 void FillBounds::updateClipBoundsForClipOp(const SkIRect& devBounds) {
173 Bounds clip = SkRect::Make(devBounds);
174 // We don't call adjustAndMap() because as its last step it would intersect the adjusted
175 // clip bounds with the previous clip, exactly what we can't do when the cli p grows.
176 fCurrentClipBounds = this->adjustForSaveLayerPaints(&clip) ? clip : kUnbound ed;
177 }
178
179 // Restore holds the devBounds for the clip after the {save,saveLayer}/restore b lock completes.
180 template <> void FillBounds::updateClipBounds(const Restore& op) {
181 // This is just like the clip ops above, but we need to skip the effects (if any) of our
182 // paired saveLayer (if it is one); it has not yet been popped off the save stack. Our
183 // devBounds reflect the state of the world after the saveLayer/restore bloc k is done,
184 // so they are not affected by the saveLayer's paint.
185 const int kSavesToIgnore = 1;
186 Bounds clip = SkRect::Make(op.devBounds);
187 fCurrentClipBounds =
188 this->adjustForSaveLayerPaints(&clip, kSavesToIgnore) ? clip : kUnbounde d;
189 }
190
191 // We also take advantage of SaveLayer bounds when present to further cut the cl ip down.
192 template <> void FillBounds::updateClipBounds(const SaveLayer& op) {
193 if (op.bounds) {
194 // adjustAndMap() intersects these layer bounds with the previous clip f or us.
195 fCurrentClipBounds = this->adjustAndMap(*op.bounds, op.paint);
196 }
197 }
198
199 // 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.
201 template <> void FillBounds::trackBounds(const Save&) { this->pushSaveB lock(NULL); }
202 template <> void FillBounds::trackBounds(const SaveLayer& op) { this->pushSaveB lock(op.paint); }
203 template <> void FillBounds::trackBounds(const Restore&) { fBounds[fCurrentOp] = this->popSaveBlock(); }
204
205 template <> void FillBounds::trackBounds(const SetMatrix&) { this->pushC ontrol(); }
206 template <> void FillBounds::trackBounds(const ClipRect&) { this->pushC ontrol(); }
207 template <> void FillBounds::trackBounds(const ClipRRect&) { this->pushC ontrol(); }
208 template <> void FillBounds::trackBounds(const ClipPath&) { this->pushC ontrol(); }
209 template <> void FillBounds::trackBounds(const ClipRegion&) { this->pushC ontrol(); }
210 template <> void FillBounds::trackBounds(const PushCull&) { this->pushC ontrol(); }
211 template <> void FillBounds::trackBounds(const PopCull&) { this->pushC ontrol(); }
212 template <> void FillBounds::trackBounds(const BeginCommentGroup&) { this->pushC ontrol(); }
213 template <> void FillBounds::trackBounds(const AddComment&) { this->pushC ontrol(); }
214 template <> void FillBounds::trackBounds(const EndCommentGroup&) { this->pushC ontrol(); }
215 template <> void FillBounds::trackBounds(const DrawData&) { this->pushC ontrol(); }
216
217 // For all other ops, we can calculate and store the bounds directly now.
218 template <typename T> void FillBounds::trackBounds(const T& op) {
219 fBounds[fCurrentOp] = this->bounds(op);
220 this->updateSaveBounds(fBounds[fCurrentOp]);
221 }
222
223 void FillBounds::pushSaveBlock(const SkPaint* paint) {
224 // Starting a new Save block. Push a new entry to represent that.
225 SaveBounds sb = { 0, Bounds::MakeEmpty(), paint };
226 fSaveStack.push(sb);
227 this->pushControl();
228 }
229
230 static bool paint_may_affect_transparent_black(const SkPaint* paint) {
231 if (paint) {
232 // FIXME: this is very conservative
233 if (paint->getImageFilter() || paint->getColorFilter()) {
234 return true;
154 } 235 }
155 236
156 // If we have any lingering unpaired Saves, simulate restores to make 237 // Unusual Xfermodes require us to process a saved layer
157 // sure all ops in those Save blocks have their bounds calculated. 238 // even with operations outisde the clip.
158 while (!fSaveStack.isEmpty()) { 239 // For example, DstIn is used by masking layers.
159 this->popSaveBlock(); 240 // https://code.google.com/p/skia/issues/detail?id=1291
160 } 241 // https://crbug.com/401593
161 242 SkXfermode* xfermode = paint->getXfermode();
162 // Any control ops not part of any Save/Restore block draw everywhere. 243 SkXfermode::Mode mode;
163 while (!fControlIndices.isEmpty()) { 244 // SrcOver is ok, and is also the common case with a NULL xfermode.
164 this->popControl(kUnbounded); 245 // So we should make that the fast path and bypass the mode extraction
165 } 246 // and test.
166 247 if (xfermode && xfermode->asMode(&mode)) {
167 // Finally feed all stored bounds into the BBH. They'll be returned in this order. 248 switch (mode) {
168 SkASSERT(bbh); 249 // For each of the following transfer modes, if the source
169 bbh->insert(&fBounds, record.count()); 250 // alpha is zero (our transparent black), the resulting
170 } 251 // blended alpha is not necessarily equal to the original
171 252 // destination alpha.
172 template <typename T> void operator()(const T& op) { 253 case SkXfermode::kClear_Mode:
173 this->updateCTM(op); 254 case SkXfermode::kSrc_Mode:
174 this->updateClipBounds(op); 255 case SkXfermode::kSrcIn_Mode:
175 this->trackBounds(op); 256 case SkXfermode::kDstIn_Mode:
176 } 257 case SkXfermode::kSrcOut_Mode:
177 258 case SkXfermode::kDstATop_Mode:
178 private: 259 case SkXfermode::kModulate_Mode:
179 // In this file, SkRect are in local coordinates, Bounds are translated back to identity space. 260 return true;
180 typedef SkRect Bounds; 261 break;
181 262 default:
182 struct SaveBounds { 263 break;
183 int controlOps; // Number of control ops in this Save block, incl uding the Save.
184 Bounds bounds; // Bounds of everything in the block.
185 const SkPaint* paint; // Unowned. If set, adjusts the bounds of all op s in this block.
186 };
187
188 // Only Restore and SetMatrix change the CTM.
189 template <typename T> void updateCTM(const T&) {}
190 void updateCTM(const Restore& op) { fCTM = &op.matrix; }
191 void updateCTM(const SetMatrix& op) { fCTM = &op.matrix; }
192
193 // Most ops don't change the clip.
194 template <typename T> void updateClipBounds(const T&) {}
195
196 // Clip{Path,RRect,Rect,Region} obviously change the clip. They all know th eir bounds already.
197 void updateClipBounds(const ClipPath& op) { this->updateClipBoundsForClipO p(op.devBounds); }
198 void updateClipBounds(const ClipRRect& op) { this->updateClipBoundsForClipO p(op.devBounds); }
199 void updateClipBounds(const ClipRect& op) { this->updateClipBoundsForClipO p(op.devBounds); }
200 void updateClipBounds(const ClipRegion& op) { this->updateClipBoundsForClipO p(op.devBounds); }
201
202 // The bounds of clip ops need to be adjusted for the paints of saveLayers t hey're inside.
203 void updateClipBoundsForClipOp(const SkIRect& devBounds) {
204 Bounds clip = SkRect::Make(devBounds);
205 // We don't call adjustAndMap() because as its last step it would inters ect the adjusted
206 // clip bounds with the previous clip, exactly what we can't do when the clip grows.
207 fCurrentClipBounds = this->adjustForSaveLayerPaints(&clip) ? clip : kUnb ounded;
208 }
209
210 // Restore holds the devBounds for the clip after the {save,saveLayer}/resto re block completes.
211 void updateClipBounds(const Restore& op) {
212 // This is just like the clip ops above, but we need to skip the effects (if any) of our
213 // paired saveLayer (if it is one); it has not yet been popped off the s ave stack. Our
214 // devBounds reflect the state of the world after the saveLayer/restore block is done,
215 // so they are not affected by the saveLayer's paint.
216 const int kSavesToIgnore = 1;
217 Bounds clip = SkRect::Make(op.devBounds);
218 fCurrentClipBounds =
219 this->adjustForSaveLayerPaints(&clip, kSavesToIgnore) ? clip : kUnbo unded;
220 }
221
222 // We also take advantage of SaveLayer bounds when present to further cut th e clip down.
223 void updateClipBounds(const SaveLayer& op) {
224 if (op.bounds) {
225 // adjustAndMap() intersects these layer bounds with the previous cl ip for us.
226 fCurrentClipBounds = this->adjustAndMap(*op.bounds, op.paint);
227 }
228 }
229
230 // The bounds of these ops must be calculated when we hit the Restore
231 // from the bounds of the ops in the same Save block.
232 void trackBounds(const Save&) { this->pushSaveBlock(NULL); }
233 void trackBounds(const SaveLayer& op) { this->pushSaveBlock(op.paint); }
234 void trackBounds(const Restore&) { fBounds[fCurrentOp] = this->popSaveBlock( ); }
235
236 void trackBounds(const SetMatrix&) { this->pushControl(); }
237 void trackBounds(const ClipRect&) { this->pushControl(); }
238 void trackBounds(const ClipRRect&) { this->pushControl(); }
239 void trackBounds(const ClipPath&) { this->pushControl(); }
240 void trackBounds(const ClipRegion&) { this->pushControl(); }
241 void trackBounds(const PushCull&) { this->pushControl(); }
242 void trackBounds(const PopCull&) { this->pushControl(); }
243 void trackBounds(const BeginCommentGroup&) { this->pushControl(); }
244 void trackBounds(const AddComment&) { this->pushControl(); }
245 void trackBounds(const EndCommentGroup&) { this->pushControl(); }
246 void trackBounds(const DrawData&) { this->pushControl(); }
247
248 // For all other ops, we can calculate and store the bounds directly now.
249 template <typename T> void trackBounds(const T& op) {
250 fBounds[fCurrentOp] = this->bounds(op);
251 this->updateSaveBounds(fBounds[fCurrentOp]);
252 }
253
254 void pushSaveBlock(const SkPaint* paint) {
255 // Starting a new Save block. Push a new entry to represent that.
256 SaveBounds sb = { 0, Bounds::MakeEmpty(), paint };
257 fSaveStack.push(sb);
258 this->pushControl();
259 }
260
261 static bool PaintMayAffectTransparentBlack(const SkPaint* paint) {
262 if (paint) {
263 // FIXME: this is very conservative
264 if (paint->getImageFilter() || paint->getColorFilter()) {
265 return true;
266 }
267
268 // Unusual Xfermodes require us to process a saved layer
269 // even with operations outisde the clip.
270 // For example, DstIn is used by masking layers.
271 // https://code.google.com/p/skia/issues/detail?id=1291
272 // https://crbug.com/401593
273 SkXfermode* xfermode = paint->getXfermode();
274 SkXfermode::Mode mode;
275 // SrcOver is ok, and is also the common case with a NULL xfermode.
276 // So we should make that the fast path and bypass the mode extracti on
277 // and test.
278 if (xfermode && xfermode->asMode(&mode)) {
279 switch (mode) {
280 // For each of the following transfer modes, if the source
281 // alpha is zero (our transparent black), the resulting
282 // blended alpha is not necessarily equal to the original
283 // destination alpha.
284 case SkXfermode::kClear_Mode:
285 case SkXfermode::kSrc_Mode:
286 case SkXfermode::kSrcIn_Mode:
287 case SkXfermode::kDstIn_Mode:
288 case SkXfermode::kSrcOut_Mode:
289 case SkXfermode::kDstATop_Mode:
290 case SkXfermode::kModulate_Mode:
291 return true;
292 break;
293 default:
294 break;
295 }
296 } 264 }
297 } 265 }
266 }
267 return false;
268 }
269
270 FillBounds::Bounds FillBounds::popSaveBlock() {
271 // We're done the Save block. Apply the block's bounds to all control ops i nside it.
272 SaveBounds sb;
273 fSaveStack.pop(&sb);
274
275 // If the paint affects transparent black, we can't trust any of our calcula ted bounds.
276 const Bounds& bounds =
277 paint_may_affect_transparent_black(sb.paint) ? fCurrentClipBounds : sb.b ounds;
278
279 while (sb.controlOps --> 0) {
280 this->popControl(bounds);
281 }
282
283 // This whole Save block may be part another Save block.
284 this->updateSaveBounds(bounds);
285
286 // If called from a real Restore (not a phony one for balance), it'll need t he bounds.
287 return bounds;
288 }
289
290 void FillBounds::pushControl() {
291 fControlIndices.push(fCurrentOp);
292 if (!fSaveStack.isEmpty()) {
293 fSaveStack.top().controlOps++;
294 }
295 }
296
297 void FillBounds::popControl(const Bounds& bounds) {
298 fBounds[fControlIndices.top()] = bounds;
299 fControlIndices.pop();
300 }
301
302 void FillBounds::updateSaveBounds(const Bounds& bounds) {
303 // If we're in a Save block, expand its bounds to cover these bounds too.
304 if (!fSaveStack.isEmpty()) {
305 fSaveStack.top().bounds.join(bounds);
306 }
307 }
308
309 // FIXME: this method could use better bounds
310 template <> FillBounds::Bounds FillBounds::bounds(const DrawText&) const { retur n fCurrentClipBounds; }
311
312 template <> FillBounds::Bounds FillBounds::bounds(const Clear&) const { return k Unbounded; } // Ignores the clip.
313 template <> FillBounds::Bounds FillBounds::bounds(const DrawPaint&) const { retu rn fCurrentClipBounds; }
314 template <> FillBounds::Bounds FillBounds::bounds(const NoOp&) const { return B ounds::MakeEmpty(); } // NoOps don't draw.
315
316 template <> FillBounds::Bounds FillBounds::bounds(const DrawSprite& op) const {
317 const SkBitmap& bm = op.bitmap;
318 return Bounds::MakeXYWH(op.left, op.top, bm.width(), bm.height()); // Ignor es the matrix.
319 }
320
321 template <> FillBounds::Bounds FillBounds::bounds(const DrawRect& op) const { re turn this->adjustAndMap(op.rect, &op.paint); }
322 template <> FillBounds::Bounds FillBounds::bounds(const DrawOval& op) const { re turn this->adjustAndMap(op.oval, &op.paint); }
323 template <> FillBounds::Bounds FillBounds::bounds(const DrawRRect& op) const {
324 return this->adjustAndMap(op.rrect.rect(), &op.paint);
325 }
326 template <> FillBounds::Bounds FillBounds::bounds(const DrawDRRect& op) const {
327 return this->adjustAndMap(op.outer.rect(), &op.paint);
328 }
329 template <> FillBounds::Bounds FillBounds::bounds(const DrawImage& op) const {
330 const SkImage* image = op.image;
331 SkRect rect = SkRect::MakeXYWH(op.left, op.top, image->width(), image->heigh t());
332
333 return this->adjustAndMap(rect, op.paint);
334 }
335 template <> FillBounds::Bounds FillBounds::bounds(const DrawImageRect& op) const {
336 return this->adjustAndMap(op.dst, op.paint);
337 }
338 template <> FillBounds::Bounds FillBounds::bounds(const DrawBitmapRectToRect& op ) const {
339 return this->adjustAndMap(op.dst, op.paint);
340 }
341 template <> FillBounds::Bounds FillBounds::bounds(const DrawBitmapNine& op) cons t {
342 return this->adjustAndMap(op.dst, op.paint);
343 }
344 template <> FillBounds::Bounds FillBounds::bounds(const DrawBitmap& op) const {
345 const SkBitmap& bm = op.bitmap;
346 return this->adjustAndMap(SkRect::MakeXYWH(op.left, op.top, bm.width(), bm.h eight()),
347 op.paint);
348 }
349 template <> FillBounds::Bounds FillBounds::bounds(const DrawBitmapMatrix& op) co nst {
350 const SkBitmap& bm = op.bitmap;
351 SkRect dst = SkRect::MakeWH(bm.width(), bm.height());
352 op.matrix.mapRect(&dst);
353 return this->adjustAndMap(dst, op.paint);
354 }
355
356 template <> FillBounds::Bounds FillBounds::bounds(const DrawPath& op) const {
357 return op.path.isInverseFillType() ? fCurrentClipBounds
358 : this->adjustAndMap(op.path.getBounds(), &op.paint);
359 }
360 template <> FillBounds::Bounds FillBounds::bounds(const DrawPoints& op) const {
361 SkRect dst;
362 dst.set(op.pts, op.count);
363
364 // Pad the bounding box a little to make sure hairline points' bounds aren't empty.
365 SkScalar stroke = SkMaxScalar(op.paint.getStrokeWidth(), 0.01f);
366 dst.outset(stroke/2, stroke/2);
367
368 return this->adjustAndMap(dst, &op.paint);
369 }
370 template <> FillBounds::Bounds FillBounds::bounds(const DrawPatch& op) const {
371 SkRect dst;
372 dst.set(op.cubics, SkPatchUtils::kNumCtrlPts);
373 return this->adjustAndMap(dst, &op.paint);
374 }
375 template <> FillBounds::Bounds FillBounds::bounds(const DrawVertices& op) const {
376 SkRect dst;
377 dst.set(op.vertices, op.vertexCount);
378 return this->adjustAndMap(dst, &op.paint);
379 }
380
381 template <> FillBounds::Bounds FillBounds::bounds(const DrawPicture& op) const {
382 SkRect dst = op.picture->cullRect();
383 if (op.matrix) {
384 op.matrix->mapRect(&dst);
385 }
386 return this->adjustAndMap(dst, op.paint);
387 }
388
389 static void adjust_text_for_font_metrics(SkRect* rect, const SkPaint& paint) {
390 #ifdef SK_DEBUG
391 SkRect correct = *rect;
392 #endif
393 // crbug.com/373785 ~~> xPad = 4x yPad
394 // crbug.com/424824 ~~> bump yPad from 2x text size to 2.5x
395 const SkScalar yPad = 2.5f * paint.getTextSize(),
396 xPad = 4.0f * yPad;
397 rect->outset(xPad, yPad);
398 #ifdef SK_DEBUG
399 SkPaint::FontMetrics metrics;
400 paint.getFontMetrics(&metrics);
401 correct.fLeft += metrics.fXMin;
402 correct.fTop += metrics.fTop;
403 correct.fRight += metrics.fXMax;
404 correct.fBottom += metrics.fBottom;
405 // See skia:2862 for why we ignore small text sizes.
406 SkASSERTF(paint.getTextSize() < 0.001f || rect->contains(correct),
407 "%f %f %f %f vs. %f %f %f %f\n",
408 -xPad, -yPad, +xPad, +yPad,
409 metrics.fXMin, metrics.fTop, metrics.fXMax, metrics.fBottom);
410 #endif
411 }
412
413 template <> FillBounds::Bounds FillBounds::bounds(const DrawPosText& op) const {
414 const int N = op.paint.countText(op.text, op.byteLength);
415 if (N == 0) {
416 return Bounds::MakeEmpty();
417 }
418
419 SkRect dst;
420 dst.set(op.pos, N);
421 adjust_text_for_font_metrics(&dst, op.paint);
422 return this->adjustAndMap(dst, &op.paint);
423 }
424 template <> FillBounds::Bounds FillBounds::bounds(const DrawPosTextH& op) const {
425 const int N = op.paint.countText(op.text, op.byteLength);
426 if (N == 0) {
427 return Bounds::MakeEmpty();
428 }
429
430 SkScalar left = op.xpos[0], right = op.xpos[0];
431 for (int i = 1; i < N; i++) {
432 left = SkMinScalar(left, op.xpos[i]);
433 right = SkMaxScalar(right, op.xpos[i]);
434 }
435 SkRect dst = { left, op.y, right, op.y };
436 adjust_text_for_font_metrics(&dst, op.paint);
437 return this->adjustAndMap(dst, &op.paint);
438 }
439 template <> FillBounds::Bounds FillBounds::bounds(const DrawTextOnPath& op) cons t {
440 SkRect dst = op.path.getBounds();
441
442 // Pad all sides by the maximum padding in any direction we'd normally apply .
443 SkRect pad = { 0, 0, 0, 0};
444 adjust_text_for_font_metrics(&pad, op.paint);
445
446 // That maximum padding happens to always be the right pad today.
447 SkASSERT(pad.fLeft == -pad.fRight);
448 SkASSERT(pad.fTop == -pad.fBottom);
449 SkASSERT(pad.fRight > pad.fBottom);
450 dst.outset(pad.fRight, pad.fRight);
451
452 return this->adjustAndMap(dst, &op.paint);
453 }
454
455 template <> FillBounds::Bounds FillBounds::bounds(const DrawTextBlob& op) const {
456 SkRect dst = op.blob->bounds();
457 dst.offset(op.x, op.y);
458 return this->adjustAndMap(dst, &op.paint);
459 }
460
461 // Returns true if rect was meaningfully adjusted for the effects of paint,
462 // false if the paint could affect the rect in unknown ways.
463 static bool adjust_for_paint(const SkPaint* paint, SkRect* rect) {
464 if (paint) {
465 if (paint->canComputeFastBounds()) {
466 *rect = paint->computeFastBounds(*rect, rect);
467 return true;
468 }
298 return false; 469 return false;
299 } 470 }
300 471 return true;
301 Bounds popSaveBlock() { 472 }
302 // We're done the Save block. Apply the block's bounds to all control o ps inside it. 473
303 SaveBounds sb; 474 bool FillBounds::adjustForSaveLayerPaints(SkRect* rect, int savesToIgnore) const {
304 fSaveStack.pop(&sb); 475 for (int i = fSaveStack.count() - 1 - savesToIgnore; i >= 0; i--) {
305 476 if (!adjust_for_paint(fSaveStack[i].paint, rect)) {
306 // If the paint affects transparent black, we can't trust any of our cal culated bounds.
307 const Bounds& bounds =
308 PaintMayAffectTransparentBlack(sb.paint) ? fCurrentClipBounds : sb.b ounds;
309
310 while (sb.controlOps --> 0) {
311 this->popControl(bounds);
312 }
313
314 // This whole Save block may be part another Save block.
315 this->updateSaveBounds(bounds);
316
317 // If called from a real Restore (not a phony one for balance), it'll ne ed the bounds.
318 return bounds;
319 }
320
321 void pushControl() {
322 fControlIndices.push(fCurrentOp);
323 if (!fSaveStack.isEmpty()) {
324 fSaveStack.top().controlOps++;
325 }
326 }
327
328 void popControl(const Bounds& bounds) {
329 fBounds[fControlIndices.top()] = bounds;
330 fControlIndices.pop();
331 }
332
333 void updateSaveBounds(const Bounds& bounds) {
334 // If we're in a Save block, expand its bounds to cover these bounds too .
335 if (!fSaveStack.isEmpty()) {
336 fSaveStack.top().bounds.join(bounds);
337 }
338 }
339
340 // FIXME: this method could use better bounds
341 Bounds bounds(const DrawText&) const { return fCurrentClipBounds; }
342
343 Bounds bounds(const Clear&) const { return kUnbounded; } // Igno res the clip.
344 Bounds bounds(const DrawPaint&) const { return fCurrentClipBounds; }
345 Bounds bounds(const NoOp&) const { return Bounds::MakeEmpty(); } // NoOp s don't draw.
346
347 Bounds bounds(const DrawSprite& op) const {
348 const SkBitmap& bm = op.bitmap;
349 return Bounds::MakeXYWH(op.left, op.top, bm.width(), bm.height()); // I gnores the matrix.
350 }
351
352 Bounds bounds(const DrawRect& op) const { return this->adjustAndMap(op.rect, &op.paint); }
353 Bounds bounds(const DrawOval& op) const { return this->adjustAndMap(op.oval, &op.paint); }
354 Bounds bounds(const DrawRRect& op) const {
355 return this->adjustAndMap(op.rrect.rect(), &op.paint);
356 }
357 Bounds bounds(const DrawDRRect& op) const {
358 return this->adjustAndMap(op.outer.rect(), &op.paint);
359 }
360 Bounds bounds(const DrawImage& op) const {
361 const SkImage* image = op.image;
362 SkRect rect = SkRect::MakeXYWH(op.left, op.top, image->width(), image->h eight());
363
364 return this->adjustAndMap(rect, op.paint);
365 }
366 Bounds bounds(const DrawImageRect& op) const {
367 return this->adjustAndMap(op.dst, op.paint);
368 }
369 Bounds bounds(const DrawBitmapRectToRect& op) const {
370 return this->adjustAndMap(op.dst, op.paint);
371 }
372 Bounds bounds(const DrawBitmapNine& op) const {
373 return this->adjustAndMap(op.dst, op.paint);
374 }
375 Bounds bounds(const DrawBitmap& op) const {
376 const SkBitmap& bm = op.bitmap;
377 return this->adjustAndMap(SkRect::MakeXYWH(op.left, op.top, bm.width(), bm.height()),
378 op.paint);
379 }
380 Bounds bounds(const DrawBitmapMatrix& op) const {
381 const SkBitmap& bm = op.bitmap;
382 SkRect dst = SkRect::MakeWH(bm.width(), bm.height());
383 op.matrix.mapRect(&dst);
384 return this->adjustAndMap(dst, op.paint);
385 }
386
387 Bounds bounds(const DrawPath& op) const {
388 return op.path.isInverseFillType() ? fCurrentClipBounds
389 : this->adjustAndMap(op.path.getBound s(), &op.paint);
390 }
391 Bounds bounds(const DrawPoints& op) const {
392 SkRect dst;
393 dst.set(op.pts, op.count);
394
395 // Pad the bounding box a little to make sure hairline points' bounds ar en't empty.
396 SkScalar stroke = SkMaxScalar(op.paint.getStrokeWidth(), 0.01f);
397 dst.outset(stroke/2, stroke/2);
398
399 return this->adjustAndMap(dst, &op.paint);
400 }
401 Bounds bounds(const DrawPatch& op) const {
402 SkRect dst;
403 dst.set(op.cubics, SkPatchUtils::kNumCtrlPts);
404 return this->adjustAndMap(dst, &op.paint);
405 }
406 Bounds bounds(const DrawVertices& op) const {
407 SkRect dst;
408 dst.set(op.vertices, op.vertexCount);
409 return this->adjustAndMap(dst, &op.paint);
410 }
411
412 Bounds bounds(const DrawPicture& op) const {
413 SkRect dst = op.picture->cullRect();
414 if (op.matrix) {
415 op.matrix->mapRect(&dst);
416 }
417 return this->adjustAndMap(dst, op.paint);
418 }
419
420 Bounds bounds(const DrawPosText& op) const {
421 const int N = op.paint.countText(op.text, op.byteLength);
422 if (N == 0) {
423 return Bounds::MakeEmpty();
424 }
425
426 SkRect dst;
427 dst.set(op.pos, N);
428 AdjustTextForFontMetrics(&dst, op.paint);
429 return this->adjustAndMap(dst, &op.paint);
430 }
431 Bounds bounds(const DrawPosTextH& op) const {
432 const int N = op.paint.countText(op.text, op.byteLength);
433 if (N == 0) {
434 return Bounds::MakeEmpty();
435 }
436
437 SkScalar left = op.xpos[0], right = op.xpos[0];
438 for (int i = 1; i < N; i++) {
439 left = SkMinScalar(left, op.xpos[i]);
440 right = SkMaxScalar(right, op.xpos[i]);
441 }
442 SkRect dst = { left, op.y, right, op.y };
443 AdjustTextForFontMetrics(&dst, op.paint);
444 return this->adjustAndMap(dst, &op.paint);
445 }
446 Bounds bounds(const DrawTextOnPath& op) const {
447 SkRect dst = op.path.getBounds();
448
449 // Pad all sides by the maximum padding in any direction we'd normally a pply.
450 SkRect pad = { 0, 0, 0, 0};
451 AdjustTextForFontMetrics(&pad, op.paint);
452
453 // That maximum padding happens to always be the right pad today.
454 SkASSERT(pad.fLeft == -pad.fRight);
455 SkASSERT(pad.fTop == -pad.fBottom);
456 SkASSERT(pad.fRight > pad.fBottom);
457 dst.outset(pad.fRight, pad.fRight);
458
459 return this->adjustAndMap(dst, &op.paint);
460 }
461
462 Bounds bounds(const DrawTextBlob& op) const {
463 SkRect dst = op.blob->bounds();
464 dst.offset(op.x, op.y);
465 return this->adjustAndMap(dst, &op.paint);
466 }
467
468 static void AdjustTextForFontMetrics(SkRect* rect, const SkPaint& paint) {
469 #ifdef SK_DEBUG
470 SkRect correct = *rect;
471 #endif
472 // crbug.com/373785 ~~> xPad = 4x yPad
473 // crbug.com/424824 ~~> bump yPad from 2x text size to 2.5x
474 const SkScalar yPad = 2.5f * paint.getTextSize(),
475 xPad = 4.0f * yPad;
476 rect->outset(xPad, yPad);
477 #ifdef SK_DEBUG
478 SkPaint::FontMetrics metrics;
479 paint.getFontMetrics(&metrics);
480 correct.fLeft += metrics.fXMin;
481 correct.fTop += metrics.fTop;
482 correct.fRight += metrics.fXMax;
483 correct.fBottom += metrics.fBottom;
484 // See skia:2862 for why we ignore small text sizes.
485 SkASSERTF(paint.getTextSize() < 0.001f || rect->contains(correct),
486 "%f %f %f %f vs. %f %f %f %f\n",
487 -xPad, -yPad, +xPad, +yPad,
488 metrics.fXMin, metrics.fTop, metrics.fXMax, metrics.fBottom);
489 #endif
490 }
491
492 // Returns true if rect was meaningfully adjusted for the effects of paint,
493 // false if the paint could affect the rect in unknown ways.
494 static bool AdjustForPaint(const SkPaint* paint, SkRect* rect) {
495 if (paint) {
496 if (paint->canComputeFastBounds()) {
497 *rect = paint->computeFastBounds(*rect, rect);
498 return true;
499 }
500 return false; 477 return false;
501 } 478 }
502 return true; 479 }
503 } 480 return true;
504 481 }
505 bool adjustForSaveLayerPaints(SkRect* rect, int savesToIgnore = 0) const { 482
506 for (int i = fSaveStack.count() - 1 - savesToIgnore; i >= 0; i--) { 483 // Adjust rect for all paints that may affect its geometry, then map it to ident ity space.
507 if (!AdjustForPaint(fSaveStack[i].paint, rect)) { 484 FillBounds::Bounds FillBounds::adjustAndMap(SkRect rect, const SkPaint* paint) c onst {
508 return false; 485 // Inverted rectangles really confuse our BBHs.
509 } 486 rect.sort();
510 } 487
511 return true; 488 // Adjust the rect for its own paint.
512 } 489 if (!adjust_for_paint(paint, &rect)) {
513 490 // The paint could do anything to our bounds. The only safe answer is t he current clip.
514 // Adjust rect for all paints that may affect its geometry, then map it to i dentity space. 491 return fCurrentClipBounds;
515 Bounds adjustAndMap(SkRect rect, const SkPaint* paint) const { 492 }
516 // Inverted rectangles really confuse our BBHs. 493
517 rect.sort(); 494 // Adjust rect for all the paints from the SaveLayers we're inside.
518 495 if (!this->adjustForSaveLayerPaints(&rect)) {
519 // Adjust the rect for its own paint. 496 // Same deal as above.
520 if (!AdjustForPaint(paint, &rect)) { 497 return fCurrentClipBounds;
521 // The paint could do anything to our bounds. The only safe answer is the current clip. 498 }
522 return fCurrentClipBounds; 499
523 } 500 // Map the rect back to identity space.
524 501 fCTM->mapRect(&rect);
525 // Adjust rect for all the paints from the SaveLayers we're inside. 502
526 if (!this->adjustForSaveLayerPaints(&rect)) { 503 // Nothing can draw outside the current clip.
527 // Same deal as above. 504 // (Only bounded ops call into this method, so oddballs like Clear don't mat ter here.)
528 return fCurrentClipBounds; 505 rect.intersect(fCurrentClipBounds);
529 } 506 return rect;
530 507 }
531 // Map the rect back to identity space.
532 fCTM->mapRect(&rect);
533
534 // Nothing can draw outside the current clip.
535 // (Only bounded ops call into this method, so oddballs like Clear don't matter here.)
536 rect.intersect(fCurrentClipBounds);
537 return rect;
538 }
539
540 // Conservative identity-space bounds for each op in the SkRecord.
541 SkAutoTMalloc<Bounds> fBounds;
542
543 // We walk fCurrentOp through the SkRecord, as we go using updateCTM()
544 // and updateClipBounds() to maintain the exact CTM (fCTM) and conservative
545 // identity-space bounds of the current clip (fCurrentClipBounds).
546 unsigned fCurrentOp;
547 const SkMatrix* fCTM;
548 Bounds fCurrentClipBounds;
549
550 // Used to track the bounds of Save/Restore blocks and the control ops insid e them.
551 SkTDArray<SaveBounds> fSaveStack;
552 SkTDArray<unsigned> fControlIndices;
553 };
554 508
555 } // namespace SkRecords 509 } // namespace SkRecords
556 510
557 void SkRecordFillBounds(const SkRecord& record, SkBBoxHierarchy* bbh) { 511 void SkRecordFillBounds(const SkRecord& record, SkBBoxHierarchy* bbh) {
558 SkRecords::FillBounds(record, bbh); 512 SkRecords::VisitMaster<SkRecords::FillBounds>(record, bbh);
mtklein 2014/11/03 19:46:02 Can we drop VisitMaster and just have this write t
robertphillips 2014/11/06 16:50:32 Done.
559 } 513 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698