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