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

Side by Side Diff: src/utils/SkDeferredCanvas.cpp

Issue 2120333002: deferred canvas (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: override drawTextRSXform Created 4 years, 5 months 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
« no previous file with comments | « src/utils/SkDeferredCanvas.h ('k') | tests/CanvasTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkDeferredCanvas.h"
9 #include "SkDrawable.h"
10 #include "SkPath.h"
11 #include "SkRRect.h"
12 #include "SkSurface.h"
13 #include "SkTextBlob.h"
14
15 bool SkDeferredCanvas::Rec::isConcat(SkMatrix* m) const {
16 switch (fType) {
17 case kTrans_Type:
18 m->setTranslate(fData.fTranslate.x(), fData.fTranslate.y());
19 return true;
20 case kScaleTrans_Type:
21 m->setScaleTranslate(fData.fScaleTrans.fScale.x(),
22 fData.fScaleTrans.fScale.y(),
23 fData.fScaleTrans.fTrans.x(),
24 fData.fScaleTrans.fTrans.y());
25 return true;
26 default:
27 break;
28 }
29 return false;
30 }
31
32 void SkDeferredCanvas::Rec::setConcat(const SkMatrix& m) {
33 SkASSERT(m.getType() <= (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) ;
34
35 if (m.getType() <= SkMatrix::kTranslate_Mask) {
36 fType = kTrans_Type;
37 fData.fTranslate.set(m.getTranslateX(), m.getTranslateY());
38 } else {
39 fType = kScaleTrans_Type;
40 fData.fScaleTrans.fScale.set(m.getScaleX(), m.getScaleY());
41 fData.fScaleTrans.fTrans.set(m.getTranslateX(), m.getTranslateY());
42 }
43 }
44
45 //////////////////////////////////////////////////////////////////////////////// ///////////////////
46
47 SkDeferredCanvas::SkDeferredCanvas(SkCanvas* canvas)
48 : INHERITED(canvas->getBaseLayerSize().width(), canvas->getBaseLayerSize().h eight())
49 , fCanvas(canvas)
50 {}
51
52 SkDeferredCanvas::~SkDeferredCanvas() {}
53
54 void SkDeferredCanvas::push_save() {
55 Rec* r = fRecs.append();
56 r->fType = kSave_Type;
57 }
58
59 void SkDeferredCanvas::push_cliprect(const SkRect& bounds) {
60 int index = fRecs.count() - 1;
61 if (index >= 0 && fRecs[index].fType == kClipRect_Type) {
62 if (!fRecs[index].fData.fBounds.intersect(bounds)) {
63 fRecs[index].fData.fBounds.setEmpty();
64 }
65 } else {
66 Rec* r = fRecs.append();
67 r->fType = kClipRect_Type;
68 r->fData.fBounds = bounds;
69 }
70 }
71
72 bool SkDeferredCanvas::push_concat(const SkMatrix& mat) {
73 if (mat.getType() > (SkMatrix::kScale_Mask | SkMatrix::kTranslate_Mask)) {
74 return false;
75 }
76 // At the moment, we don't know which ops can scale and which can also flip, so
77 // we reject negative scales for now
78 if (mat.getScaleX() < 0 || mat.getScaleY() < 0) {
79 return false;
80 }
81
82 int index = fRecs.count() - 1;
83 SkMatrix m;
84 if (index >= 0 && fRecs[index].isConcat(&m)) {
85 m.preConcat(mat);
86 fRecs[index].setConcat(m);
87 } else {
88 fRecs.append()->setConcat(mat);
89 }
90 return true;
91 }
92
93 void SkDeferredCanvas::emit(const Rec& rec) {
94 switch (rec.fType) {
95 case kSave_Type:
96 fCanvas->save();
97 this->INHERITED::willSave();
98 break;
99 case kClipRect_Type:
100 fCanvas->clipRect(rec.fData.fBounds);
101 this->INHERITED::onClipRect(rec.fData.fBounds,
102 SkRegion::kIntersect_Op, kHard_ClipEdgeS tyle);
103 break;
104 case kTrans_Type:
105 case kScaleTrans_Type: {
106 SkMatrix mat;
107 rec.getConcat(&mat);
108 fCanvas->concat(mat);
109 this->INHERITED::didConcat(mat);
110 } break;
111 }
112 }
113
114 void SkDeferredCanvas::flush_le(int index) {
115 SkASSERT(index >= -1 && index < fRecs.count());
116
117 int count = index + 1;
118 for (int i = 0; i < count; ++i) {
119 this->emit(fRecs[i]);
120 }
121 fRecs.remove(0, count);
122 }
123
124 void SkDeferredCanvas::flush_all() {
125 this->flush_le(fRecs.count() - 1);
126 }
127
128 void SkDeferredCanvas::flush_before_saves() {
129 int i;
130 for (i = fRecs.count() - 1; i >= 0; --i) {
131 if (kSave_Type != fRecs[i].fType) {
132 break;
133 }
134 }
135 this->flush_le(i);
136 }
137
138 enum Flags {
139 kNoTranslate_Flag = 1 << 0,
140 kNoClip_Flag = 1 << 1,
141 kNoCull_Flag = 1 << 2,
142 kNoScale_Flag = 1 << 3,
143 };
144
145 void SkDeferredCanvas::flush_check(SkRect* bounds, const SkPaint* paint, unsigne d flags) {
146 if (paint) {
147 if (paint->getShader() || paint->getImageFilter()) {
148 flags |= kNoTranslate_Flag | kNoScale_Flag;
149 }
150 // TODO: replace these with code to enlarge the bounds conservatively?
151 if (paint->getStyle() != SkPaint::kFill_Style || paint->getMaskFilter() ||
152 paint->getImageFilter() || paint->getPathEffect())
153 {
154 flags |= kNoCull_Flag | kNoScale_Flag | kNoClip_Flag;
155 }
156 if (paint->getLooper()) {
157 // to be conservative, we disable both, since embedded layers could have shaders
158 // or strokes etc.
159 flags |= kNoTranslate_Flag | kNoCull_Flag | kNoScale_Flag;
160 }
161 }
162 bool canClip = !(flags & kNoClip_Flag);
163 bool canTranslate = !(flags & kNoTranslate_Flag);
164 bool canCull = !(flags & kNoCull_Flag);
165 bool canScale = !(flags & kNoScale_Flag);
166
167 int i;
168 for (i = fRecs.count() - 1; i >= 0; --i) {
169 const Rec& rec = fRecs[i];
170 switch (rec.fType) {
171 case kSave_Type:
172 // continue to the next rec
173 break;
174 case kClipRect_Type:
175 if (!canCull) {
176 goto STOP;
177 }
178 if (canClip) {
179 if (!bounds->intersect(rec.fData.fBounds)) {
180 bounds->setEmpty();
181 return;
182 }
183 // continue to the next rec
184 } else {
185 if (!rec.fData.fBounds.contains(*bounds)) {
186 goto STOP;
187 }
188 // continue to the next rec
189 }
190 break;
191 case kTrans_Type:
192 if (canTranslate) {
193 bounds->offset(rec.fData.fTranslate.x(), rec.fData.fTranslat e.y());
194 // continue to the next rec
195 } else {
196 goto STOP;
197 }
198 break;
199 case kScaleTrans_Type:
200 if (canScale) {
201 SkMatrix m;
202 rec.getConcat(&m);
203 m.mapRectScaleTranslate(bounds, *bounds);
204 } else {
205 goto STOP;
206 }
207 break;
208 }
209 }
210 STOP:
211 this->flush_le(i);
212 }
213
214 void SkDeferredCanvas::flush_translate(SkScalar* x, SkScalar* y, const SkRect& b ounds,
215 const SkPaint* paint) {
216 SkRect tmp = bounds;
217 this->flush_check(&tmp, paint, kNoClip_Flag | kNoScale_Flag);
218 *x += tmp.x() - bounds.x();
219 *y += tmp.y() - bounds.y();
220 }
221
222 void SkDeferredCanvas::flush_translate(SkScalar* x, SkScalar* y, const SkPaint& paint) {
223 SkRect tmp = SkRect::MakeXYWH(*x, *y, 1, 1);
224 this->flush_check(&tmp, &paint, kNoClip_Flag | kNoCull_Flag | kNoScale_Flag) ;
225 *x = tmp.x();
226 *y = tmp.y();
227 }
228
229 //////////////////////////////////////////////////////////////////////////////// ///////////////////
230
231 void SkDeferredCanvas::willSave() {
232 this->push_save();
233 }
234
235 SkCanvas::SaveLayerStrategy SkDeferredCanvas::getSaveLayerStrategy(const SaveLay erRec& rec) {
236 this->flush_all();
237 fCanvas->saveLayer(rec);
238 this->INHERITED::getSaveLayerStrategy(rec);
239 // No need for a layer.
240 return kNoLayer_SaveLayerStrategy;
241 }
242
243 void SkDeferredCanvas::willRestore() {
244 for (int i = fRecs.count() - 1; i >= 0; --i) {
245 if (kSave_Type == fRecs[i].fType) {
246 fRecs.setCount(i); // pop off everything here and later
247 return;
248 }
249 }
250 for (int i = 0; i < fRecs.count(); ++i) {
251 SkASSERT(kSave_Type != fRecs[i].fType);
252 }
253 fRecs.setCount(0);
254 fCanvas->restore();
255 this->INHERITED::willRestore();
256 }
257
258 void SkDeferredCanvas::didConcat(const SkMatrix& matrix) {
259 if (matrix.isIdentity()) {
260 return;
261 }
262 if (!this->push_concat(matrix)) {
263 this->flush_all();
264 fCanvas->concat(matrix);
265 this->INHERITED::didConcat(matrix);
266 }
267 }
268
269 void SkDeferredCanvas::didSetMatrix(const SkMatrix& matrix) {
270 this->flush_all();
271 fCanvas->setMatrix(matrix);
272 this->INHERITED::didSetMatrix(matrix);
273 }
274
275 void SkDeferredCanvas::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeS tyle edgeStyle) {
276 if (SkRegion::kIntersect_Op == op) {
277 this->push_cliprect(rect);
278 } else {
279 this->flush_all();
280 fCanvas->clipRect(rect, op, kSoft_ClipEdgeStyle == edgeStyle);
281 this->INHERITED::onClipRect(rect, op, edgeStyle);
282 }
283 }
284
285 void SkDeferredCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEd geStyle edgeStyle) {
286 this->flush_all();
287 fCanvas->clipRRect(rrect, op, kSoft_ClipEdgeStyle == edgeStyle);
288 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
289 }
290
291 void SkDeferredCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeS tyle edgeStyle) {
292 this->flush_all();
293 fCanvas->clipPath(path, op, kSoft_ClipEdgeStyle == edgeStyle);
294 this->INHERITED::onClipPath(path, op, edgeStyle);
295 }
296
297 void SkDeferredCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
298 this->flush_all();
299 fCanvas->clipRegion(deviceRgn, op);
300 this->INHERITED::onClipRegion(deviceRgn, op);
301 }
302
303 void SkDeferredCanvas::onDrawPaint(const SkPaint& paint) {
304 // TODO: Can we turn this into drawRect?
305 this->flush_all();
306 fCanvas->drawPaint(paint);
307 }
308
309 void SkDeferredCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[],
310 const SkPaint& paint) {
311 this->flush_all();
312 fCanvas->drawPoints(mode, count, pts, paint);
313 }
314
315 void SkDeferredCanvas::onDrawRect(const SkRect& rect, const SkPaint& paint) {
316 SkRect modRect = rect;
317 this->flush_check(&modRect, &paint);
318 fCanvas->drawRect(modRect, paint);
319 }
320
321 void SkDeferredCanvas::onDrawOval(const SkRect& rect, const SkPaint& paint) {
322 SkRect modRect = rect;
323 this->flush_check(&modRect, &paint, kNoClip_Flag);
324 fCanvas->drawOval(modRect, paint);
325 }
326
327 static SkRRect make_offset(const SkRRect& src, SkScalar dx, SkScalar dy) {
328 SkRRect dst = src;
329 dst.offset(dx, dy);
330 return dst;
331 }
332
333 void SkDeferredCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) {
334 SkRect modRect = rrect.getBounds();
335 this->flush_check(&modRect, &paint, kNoClip_Flag);
336 fCanvas->drawRRect(make_offset(rrect,
337 modRect.x() - rrect.getBounds().x(),
338 modRect.y() - rrect.getBounds().y()), paint);
339 }
340
341 void SkDeferredCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) {
342 this->flush_all();
343 fCanvas->drawDRRect(outer, inner, paint);
344 }
345
346 void SkDeferredCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) {
347 if (path.isInverseFillType()) {
348 this->flush_before_saves();
349 } else {
350 SkRect modRect = path.getBounds();
351 this->flush_check(&modRect, &paint, kNoClip_Flag | kNoTranslate_Flag | k NoScale_Flag);
352 }
353 fCanvas->drawPath(path, paint);
354 }
355
356 void SkDeferredCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
357 const SkPaint* paint) {
358 const SkScalar w = SkIntToScalar(bitmap.width());
359 const SkScalar h = SkIntToScalar(bitmap.height());
360 SkRect bounds = SkRect::MakeXYWH(x, y, w, h);
361 this->flush_check(&bounds, paint, kNoClip_Flag);
362 if (bounds.width() == w && bounds.height() == h) {
363 fCanvas->drawBitmap(bitmap, bounds.x(), bounds.y(), paint);
364 } else {
365 fCanvas->drawBitmapRect(bitmap, bounds, paint);
366 }
367 }
368
369 void SkDeferredCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* sr c, const SkRect& dst,
370 const SkPaint* paint, SrcRectConstraint cons traint) {
371 SkRect modRect = dst;
372 this->flush_check(&modRect, paint, kNoClip_Flag);
373 fCanvas->legacy_drawBitmapRect(bitmap, src, modRect, paint, constraint);
374 }
375
376 void SkDeferredCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& c enter,
377 const SkRect& dst, const SkPaint* paint) {
378 SkRect modRect = dst;
379 this->flush_check(&modRect, paint, kNoClip_Flag);
380 fCanvas->drawBitmapNine(bitmap, center, modRect, paint);
381 }
382
383 void SkDeferredCanvas::onDrawImageNine(const SkImage* image, const SkIRect& cent er,
384 const SkRect& dst, const SkPaint* paint) {
385 SkRect modRect = dst;
386 this->flush_check(&modRect, paint, kNoClip_Flag);
387 fCanvas->drawImageNine(image, center, modRect, paint);
388 }
389
390 void SkDeferredCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y,
391 const SkPaint* paint) {
392 const SkScalar w = SkIntToScalar(image->width());
393 const SkScalar h = SkIntToScalar(image->height());
394 SkRect bounds = SkRect::MakeXYWH(x, y, w, h);
395 this->flush_check(&bounds, paint, kNoClip_Flag);
396 if (bounds.width() == w && bounds.height() == h) {
397 fCanvas->drawImage(image, bounds.x(), bounds.y(), paint);
398 } else {
399 fCanvas->drawImageRect(image, bounds, paint);
400 }
401 }
402
403 void SkDeferredCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst,
404 const SkPaint* paint, SrcRectConstraint const raint) {
405 SkRect modRect = dst;
406 this->flush_check(&modRect, paint, kNoClip_Flag);
407 fCanvas->legacy_drawImageRect(image, src, modRect, paint, constraint);
408 }
409
410 void SkDeferredCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
411 const SkPaint& paint) {
412 this->flush_translate(&x, &y, paint);
413 fCanvas->drawText(text, byteLength, x, y, paint);
414 }
415
416 void SkDeferredCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
417 const SkPaint& paint) {
418 this->flush_before_saves();
419 fCanvas->drawPosText(text, byteLength, pos, paint);
420 }
421
422 void SkDeferredCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
423 SkScalar constY, const SkPaint& paint) {
424 this->flush_before_saves();
425 fCanvas->drawPosTextH(text, byteLength, xpos, constY, paint);
426 }
427
428 void SkDeferredCanvas::onDrawTextOnPath(const void* text, size_t byteLength, con st SkPath& path,
429 const SkMatrix* matrix, const SkPaint& paint ) {
430 this->flush_before_saves();
431 fCanvas->drawTextOnPath(text, byteLength, path, matrix, paint);
432 }
433
434 void SkDeferredCanvas::onDrawTextRSXform(const void* text, size_t byteLength,
435 const SkRSXform xform[], const SkRect* cullRect,
436 const SkPaint& paint) {
437 if (cullRect) {
438 SkRect modRect = *cullRect;
439 // only allow culling
440 this->flush_check(&modRect, &paint, kNoClip_Flag | kNoScale_Flag | kNoTr anslate_Flag);
441 } else {
442 this->flush_before_saves();
443 }
444 fCanvas->drawTextRSXform(text, byteLength, xform, cullRect, paint);
445 }
446
447 void SkDeferredCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScal ar y,
448 const SkPaint &paint) {
449 this->flush_translate(&x, &y, blob->bounds(), &paint);
450 fCanvas->drawTextBlob(blob, x, y, paint);
451 }
452
453 #include "SkPicture.h"
454 #include "SkCanvasPriv.h"
455 void SkDeferredCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* m atrix,
456 const SkPaint* paint) {
457 #if 1
458 SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect());
459 picture->playback(this);
460 #else
461 this->flush_before_saves();
462 fCanvas->drawPicture(picture, matrix, paint);
463 #endif
464 }
465
466 void SkDeferredCanvas::onDrawDrawable(SkDrawable* drawable, const SkMatrix* matr ix) {
467 // TODO: investigate culling and applying concat to the matrix
468 #if 1
469 drawable->draw(this, matrix);
470 #else
471 this->flush_before_saves();
472 fCanvas->drawDrawable(drawable, matrix);
473 #endif
474 }
475
476 void SkDeferredCanvas::onDrawAtlas(const SkImage* image, const SkRSXform xform[] ,
477 const SkRect rects[], const SkColor colors[],
478 int count, SkXfermode::Mode mode,
479 const SkRect* cull, const SkPaint* paint) {
480 this->flush_before_saves();
481 fCanvas->drawAtlas(image, xform, rects, colors, count, mode, cull, paint);
482 }
483
484 void SkDeferredCanvas::onDrawVertices(VertexMode vmode, int vertexCount,
485 const SkPoint vertices[], const SkPoint texs[] ,
486 const SkColor colors[], SkXfermode* xmode,
487 const uint16_t indices[], int indexCount,
488 const SkPaint& paint) {
489 this->flush_before_saves();
490 fCanvas->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode,
491 indices, indexCount, paint);
492 }
493
494 void SkDeferredCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor color s[4],
495 const SkPoint texCoords[4], SkXfermode* xmode,
496 const SkPaint& paint) {
497 this->flush_before_saves();
498 fCanvas->drawPatch(cubics, colors, texCoords, xmode, paint);
499 }
500
501 void SkDeferredCanvas::onDrawAnnotation(const SkRect& rect, const char key[], Sk Data* data) {
502 this->flush_all();
503 fCanvas->drawAnnotation(rect, key, data);
504 }
505
506 #ifdef SK_SUPPORT_LEGACY_DRAWFILTER
507 SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) {
508 fCanvas->setDrawFilter(filter);
509 return this->INHERITED::setDrawFilter(filter);
510 }
511 #endif
512
513 //////////////////////////////////////////////////////////////////////////////// ///////////////////
514
515 sk_sp<SkSurface> SkDeferredCanvas::onNewSurface(const SkImageInfo& info,
516 const SkSurfaceProps& props) {
517 return fCanvas->makeSurface(info, &props);
518 }
519 SkISize SkDeferredCanvas::getBaseLayerSize() const { return fCanvas->getBaseLaye rSize(); }
520 bool SkDeferredCanvas::getClipBounds(SkRect* bounds) const {
521 return fCanvas->getClipBounds(bounds);
522 }
523 bool SkDeferredCanvas::getClipDeviceBounds(SkIRect* bounds) const {
524 return fCanvas->getClipDeviceBounds(bounds);
525 }
526 bool SkDeferredCanvas::isClipEmpty() const { return fCanvas->isClipEmpty(); }
527 bool SkDeferredCanvas::isClipRect() const { return fCanvas->isClipRect(); }
528 bool SkDeferredCanvas::onPeekPixels(SkPixmap* pixmap) { return fCanvas->peekPixe ls(pixmap); }
529 bool SkDeferredCanvas::onAccessTopLayerPixels(SkPixmap* pixmap) {
530 SkImageInfo info;
531 size_t rowBytes;
532 SkIPoint* origin = nullptr;
533 void* addr = fCanvas->accessTopLayerPixels(&info, &rowBytes, origin);
534 if (addr) {
535 *pixmap = SkPixmap(info, addr, rowBytes);
536 return true;
537 }
538 return false;
539 }
540 SkImageInfo SkDeferredCanvas::onImageInfo() const { return fCanvas->imageInfo(); }
541 bool SkDeferredCanvas::onGetProps(SkSurfaceProps* props) const { return fCanvas- >getProps(props); }
542 void SkDeferredCanvas::onFlush() {
543 this->flush_all();
544 return fCanvas->flush();
545 }
OLDNEW
« no previous file with comments | « src/utils/SkDeferredCanvas.h ('k') | tests/CanvasTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698