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

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

Powered by Google App Engine
This is Rietveld 408576698