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

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

Powered by Google App Engine
This is Rietveld 408576698