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

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

Powered by Google App Engine
This is Rietveld 408576698