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

Side by Side Diff: skia/ext/cdl_canvas.cc

Issue 2523673004: [NOT FOR COMMIT] Fully replace SkCanvas uses.
Patch Set: Support Android build. Created 3 years, 12 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 | « skia/ext/cdl_canvas.h ('k') | skia/ext/cdl_common.h » ('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 "cdl_canvas.h"
9
10 #if CDL_ENABLED
11
12 #include "base/memory/ptr_util.h"
13 #include "cdl_no_draw_canvas.h"
14 #include "cdl_paint.h"
15 #include "cdl_picture.h"
16 #include "third_party/skia/include/core/SkCanvas.h"
17 #include "third_party/skia/include/core/SkSurface.h"
18 #include "third_party/skia/include/utils/SkNoDrawCanvas.h"
19 #include "third_party/skia/include/utils/SkNWayCanvas.h"
20
21 // TODO(cdl) LIST
22 // SkRecordNoopSaveLayerDrawRestores
23
24 #define RETURN_ON_NULL(ptr) \
25 do { \
26 if (nullptr == (ptr)) \
27 return; \
28 } while (0)
29
30 CdlNoDrawCanvas::CdlNoDrawCanvas(int width, int height)
31 : CdlCanvas(width, height) {}
32 CdlNoDrawCanvas::~CdlNoDrawCanvas() {}
33
34 sk_sp<CdlCanvas> CdlCanvas::Make(SkCanvas* canvas) {
35 return sk_sp<CdlCanvas>(new CdlCanvas(canvas));
36 }
37
38 CdlCanvas::CdlCanvas(SkCanvas* canvas) : canvas_(canvas) {}
39
40 CdlCanvas::CdlCanvas(SkBaseDevice* device) {
41 owned_canvas_ = base::MakeUnique<SkCanvas>(device);
42 canvas_ = owned_canvas_.get();
43 }
44
45 CdlCanvas::CdlCanvas(const SkBitmap& bitmap) {
46 owned_canvas_ = base::MakeUnique<SkCanvas>(bitmap);
47 canvas_ = owned_canvas_.get();
48 }
49
50 CdlCanvas::CdlCanvas(const SkBitmap& bitmap, const SkSurfaceProps& props) {
51 owned_canvas_ = base::MakeUnique<SkCanvas>(bitmap, props);
52 canvas_ = owned_canvas_.get();
53 }
54
55 CdlCanvas::CdlCanvas(int width, int height)
56 : owned_canvas_(new SkNoDrawCanvas(width, height)),
57 canvas_(owned_canvas_.get()) {}
58
59 CdlCanvas::~CdlCanvas() {}
60
61 ///////////////////////////////////////////////////////////////////////////////
62 // Save / Restore
63 int CdlCanvas::save() {
64 return this->onSave();
65 }
66
67 int CdlCanvas::onSave() {
68 return canvas_->save();
69 }
70
71 int CdlCanvas::saveLayer(const SkRect* bounds, const CdlPaint* paint) {
72 return this->saveLayer(SaveLayerRec(bounds, paint, 0));
73 }
74
75 int CdlCanvas::saveLayer(const SaveLayerRec& rec) {
76 return this->onSaveLayer(rec);
77 }
78
79 int CdlCanvas::onSaveLayer(const SaveLayerRec& rec) {
80 SkPaint sk_paint;
81 if (rec.fPaint)
82 sk_paint = rec.fPaint->toSkPaint();
83
84 SkCanvas::SaveLayerRec sk_rec(rec.fBounds, rec.fPaint ? &sk_paint : nullptr,
85 rec.fBackdrop, rec.fSaveLayerFlags);
86 return canvas_->saveLayer(sk_rec);
87 }
88
89 int CdlCanvas::saveLayerAlpha(const SkRect* bounds, U8CPU alpha) {
90 if (0xFF == alpha) {
91 return this->saveLayer(bounds, nullptr);
92 } else {
93 CdlPaint tmpPaint;
94 tmpPaint.setAlpha(alpha);
95 return this->saveLayer(bounds, &tmpPaint);
96 }
97 }
98
99 int CdlCanvas::saveLayerPreserveLCDTextRequests(const SkRect* bounds,
100 const CdlPaint* paint) {
101 return this->saveLayer(
102 SaveLayerRec(bounds, paint, SkCanvas::kPreserveLCDText_SaveLayerFlag));
103 }
104
105 void CdlCanvas::restore() {
106 this->onRestore();
107 }
108
109 void CdlCanvas::onRestore() {
110 canvas_->restore();
111 }
112
113 int CdlCanvas::getSaveCount() const {
114 return canvas_->getSaveCount();
115 }
116
117 void CdlCanvas::restoreToCount(int count) {
118 // sanity check
119 if (count < 1) {
120 count = 1;
121 }
122
123 int n = this->getSaveCount() - count;
124 for (int i = 0; i < n; ++i) {
125 this->restore();
126 }
127 }
128
129 ///////////////////////////////////////////////////////////////////////////////
130 // Transform
131 void CdlCanvas::concat(const SkMatrix& matrix) {
132 if (matrix.isIdentity()) {
133 return;
134 }
135 this->onConcat(matrix);
136 }
137
138 void CdlCanvas::rotate(SkScalar degrees) {
139 SkMatrix m;
140 m.setRotate(degrees);
141 this->concat(m);
142 }
143
144 void CdlCanvas::scale(SkScalar sx, SkScalar sy) {
145 SkMatrix m;
146 m.setScale(sx, sy);
147 this->concat(m);
148 }
149
150 void CdlCanvas::translate(SkScalar dx, SkScalar dy) {
151 if (dx || dy) {
152 this->onTranslate(dx, dy);
153 }
154 }
155
156 void CdlCanvas::resetMatrix() {
157 this->onSetMatrix(SkMatrix::I());
158 }
159
160 void CdlCanvas::setMatrix(const SkMatrix& matrix) {
161 this->onSetMatrix(matrix);
162 }
163
164 const SkMatrix& CdlCanvas::getTotalMatrix() const {
165 return canvas_->getTotalMatrix();
166 }
167
168 ///////////////////////////////////////////////////////////////////////////////
169 // Clip
170 SkISize CdlCanvas::getBaseLayerSize() const {
171 return canvas_->getBaseLayerSize();
172 }
173
174 bool CdlCanvas::getClipBounds(SkRect* bounds) const {
175 return canvas_->getClipBounds(bounds);
176 }
177
178 bool CdlCanvas::getClipDeviceBounds(SkIRect* bounds) const {
179 return canvas_->getClipDeviceBounds(bounds);
180 }
181
182 const SkClipStack* CdlCanvas::getClipStack() const {
183 return canvas_->getClipStack();
184 }
185
186 bool CdlCanvas::isClipEmpty() const {
187 return canvas_->isClipEmpty();
188 }
189
190 bool CdlCanvas::isClipRect() const {
191 return canvas_->isClipRect();
192 }
193
194 bool CdlCanvas::quickReject(const SkRect& src) const {
195 return canvas_->quickReject(src);
196 }
197
198 void CdlCanvas::clipRect(const SkRect& rect, SkCanvas::ClipOp op, bool doAA) {
199 ClipEdgeStyle edgeStyle = doAA ? kSoft_ClipEdgeStyle : kHard_ClipEdgeStyle;
200 this->onClipRect(rect, op, edgeStyle);
201 }
202
203 void CdlCanvas::clipRRect(const SkRRect& rrect,
204 SkCanvas::ClipOp op,
205 bool doAA) {
206 ClipEdgeStyle edgeStyle = doAA ? kSoft_ClipEdgeStyle : kHard_ClipEdgeStyle;
207 if (rrect.isRect()) {
208 this->onClipRect(rrect.getBounds(), op, edgeStyle);
209 } else {
210 this->onClipRRect(rrect, op, edgeStyle);
211 }
212 }
213
214 void CdlCanvas::clipPath(const SkPath& path, SkCanvas::ClipOp op, bool doAA) {
215 ClipEdgeStyle edgeStyle = doAA ? kSoft_ClipEdgeStyle : kHard_ClipEdgeStyle;
216
217 if (!path.isInverseFillType() && canvas_->getTotalMatrix().rectStaysRect()) {
218 SkRect r;
219 if (path.isRect(&r)) {
220 this->onClipRect(r, op, edgeStyle);
221 return;
222 }
223 SkRRect rrect;
224 if (path.isOval(&r)) {
225 rrect.setOval(r);
226 this->onClipRRect(rrect, op, edgeStyle);
227 return;
228 }
229 if (path.isRRect(&rrect)) {
230 this->onClipRRect(rrect, op, edgeStyle);
231 return;
232 }
233 }
234
235 this->onClipPath(path, op, edgeStyle);
236 }
237
238 void CdlCanvas::clipRegion(const SkRegion& rgn, SkCanvas::ClipOp op) {
239 this->onClipRegion(rgn, op);
240 }
241
242 ///////////////////////////////////////////////////////////////////////////////
243 // Draw
244 void CdlCanvas::drawColor(SkColor color, SkBlendMode mode) {
245 CdlPaint paint;
246 paint.setColor(color);
247 paint.setBlendMode(mode);
248 this->drawPaint(paint);
249 }
250
251 void CdlCanvas::drawPaint(const CdlPaint& paint) {
252 this->onDrawPaint(paint);
253 }
254
255 void CdlCanvas::drawPoint(SkScalar x, SkScalar y, const CdlPaint& paint) {
256 SkPoint pt;
257
258 pt.set(x, y);
259 this->drawPoints(SkCanvas::kPoints_PointMode, 1, &pt, paint);
260 }
261
262 void CdlCanvas::drawPoint(SkScalar x, SkScalar y, SkColor color) {
263 SkPoint pt;
264 CdlPaint paint;
265
266 pt.set(x, y);
267 paint.setColor(color);
268 this->drawPoints(SkCanvas::kPoints_PointMode, 1, &pt, paint);
269 }
270
271 void CdlCanvas::drawPoints(SkCanvas::PointMode mode,
272 size_t count,
273 const SkPoint pts[],
274 const CdlPaint& paint) {
275 this->onDrawPoints(mode, count, pts, paint);
276 }
277
278 void CdlCanvas::drawLine(SkScalar x0,
279 SkScalar y0,
280 SkScalar x1,
281 SkScalar y1,
282 const CdlPaint& paint) {
283 SkPoint pts[2];
284
285 pts[0].set(x0, y0);
286 pts[1].set(x1, y1);
287 this->drawPoints(SkCanvas::kLines_PointMode, 2, pts, paint);
288 }
289
290 void CdlCanvas::drawCircle(SkScalar cx,
291 SkScalar cy,
292 SkScalar radius,
293 const CdlPaint& paint) {
294 if (radius < 0) {
295 radius = 0;
296 }
297
298 SkRect r;
299 r.set(cx - radius, cy - radius, cx + radius, cy + radius);
300 this->drawOval(r, paint);
301 }
302
303 void CdlCanvas::drawOval(const SkRect& r, const CdlPaint& paint) {
304 this->onDrawOval(r, paint);
305 }
306
307 void CdlCanvas::drawRect(const SkRect& r, const CdlPaint& paint) {
308 onDrawRect(r, paint);
309 }
310
311 void CdlCanvas::drawRoundRect(const SkRect& r,
312 SkScalar rx,
313 SkScalar ry,
314 const CdlPaint& paint) {
315 if (rx > 0 && ry > 0) {
316 SkRRect rrect;
317 rrect.setRectXY(r, rx, ry);
318 this->drawRRect(rrect, paint);
319 } else {
320 this->drawRect(r, paint);
321 }
322 }
323
324 void CdlCanvas::drawRectCoords(SkScalar left,
325 SkScalar top,
326 SkScalar right,
327 SkScalar bottom,
328 const CdlPaint& paint) {
329 SkRect r;
330 r.set(left, top, right, bottom);
331 this->drawRect(r, paint);
332 }
333
334 void CdlCanvas::drawRRect(const SkRRect& rrect, const CdlPaint& paint) {
335 this->onDrawRRect(rrect, paint);
336 }
337
338 void CdlCanvas::drawDRRect(const SkRRect& outer,
339 const SkRRect& inner,
340 const CdlPaint& paint) {
341 if (outer.isEmpty()) {
342 return;
343 }
344 if (inner.isEmpty()) {
345 this->drawRRect(outer, paint);
346 return;
347 }
348
349 this->onDrawDRRect(outer, inner, paint);
350 }
351
352 void CdlCanvas::drawPath(const SkPath& path, const CdlPaint& paint) {
353 this->onDrawPath(path, paint);
354 }
355
356 void CdlCanvas::drawBitmap(const SkBitmap& bitmap,
357 SkScalar dx,
358 SkScalar dy,
359 const CdlPaint* paint) {
360 if (bitmap.drawsNothing()) {
361 return;
362 }
363 this->onDrawImage(SkImage::MakeFromBitmap(bitmap).get(), dx, dy, paint);
364 }
365
366 void CdlCanvas::drawImage(const SkImage* image,
367 SkScalar x,
368 SkScalar y,
369 const CdlPaint* paint) {
370 RETURN_ON_NULL(image);
371 this->onDrawImage(image, x, y, paint);
372 }
373
374 void CdlCanvas::drawImageRect(const SkImage* image,
375 const SkRect& src,
376 const SkRect& dst,
377 const CdlPaint* paint,
378 SkCanvas::SrcRectConstraint constraint) {
379 RETURN_ON_NULL(image);
380 if (dst.isEmpty() || src.isEmpty()) {
381 return;
382 }
383 this->onDrawImageRect(image, &src, dst, paint, constraint);
384 }
385
386 void CdlCanvas::drawImageRect(const SkImage* image,
387 const SkRect& dst,
388 const CdlPaint* paint,
389 SkCanvas::SrcRectConstraint constraint) {
390 RETURN_ON_NULL(image);
391 this->drawImageRect(image, SkRect::MakeIWH(image->width(), image->height()),
392 dst, paint, constraint);
393 }
394
395 void CdlCanvas::drawText(const void* text,
396 size_t byteLength,
397 SkScalar x,
398 SkScalar y,
399 const CdlPaint& paint) {
400 this->onDrawText(text, byteLength, x, y, paint);
401 }
402
403 void CdlCanvas::drawPosText(const void* text,
404 size_t byteLength,
405 const SkPoint pos[],
406 const CdlPaint& paint) {
407 if (byteLength) {
408 this->onDrawPosText(text, byteLength, pos, paint);
409 }
410 }
411
412 void CdlCanvas::drawTextBlob(const SkTextBlob* blob,
413 SkScalar x,
414 SkScalar y,
415 const CdlPaint& paint) {
416 RETURN_ON_NULL(blob);
417 this->onDrawTextBlob(blob, x, y, paint);
418 }
419
420 ///////////////////////////////////////////////////////////////////////////////
421 // Misc
422 void CdlCanvas::flush() {
423 canvas_->flush();
424 }
425
426 bool CdlCanvas::readPixels(SkBitmap* bitmap, int srcX, int srcY) {
427 return canvas_->readPixels(bitmap, srcX, srcY);
428 }
429
430 bool CdlCanvas::writePixels(const SkImageInfo& origInfo,
431 const void* pixels,
432 size_t rowBytes,
433 int x,
434 int y) {
435 return canvas_->writePixels(origInfo, pixels, rowBytes, x, y);
436 }
437
438 bool CdlCanvas::writePixels(const SkBitmap& bitmap, int x, int y) {
439 return canvas_->writePixels(bitmap, x, y);
440 }
441
442 ///////////////////////////////////////////////////////////////////////////////
443 // Default pass-through implementation
444 void CdlCanvas::onConcat(SkMatrix const& matrix) {
445 canvas_->concat(matrix);
446 }
447
448 void CdlCanvas::onSetMatrix(SkMatrix const& matrix) {
449 canvas_->setMatrix(matrix);
450 }
451
452 void CdlCanvas::onTranslate(float dx, float dy) {
453 canvas_->translate(dx, dy);
454 }
455
456 void CdlCanvas::onClipRect(SkRect const& r,
457 SkRegion::Op op,
458 CdlCanvas::ClipEdgeStyle style) {
459 canvas_->clipRect(r, op, style == kSoft_ClipEdgeStyle);
460 }
461 void CdlCanvas::onClipRRect(SkRRect const& r,
462 SkRegion::Op op,
463 CdlCanvas::ClipEdgeStyle style) {
464 canvas_->clipRRect(r, op, style == kSoft_ClipEdgeStyle);
465 }
466 void CdlCanvas::onClipPath(SkPath const& p,
467 SkRegion::Op op,
468 CdlCanvas::ClipEdgeStyle style) {
469 canvas_->clipPath(p, op, style == kSoft_ClipEdgeStyle);
470 }
471 void CdlCanvas::onClipRegion(SkRegion const& r, SkRegion::Op op) {
472 canvas_->clipRegion(r, op);
473 }
474 void CdlCanvas::onDiscard() {
475 canvas_->discard();
476 }
477 void CdlCanvas::onDrawPaint(CdlPaint const& paint) {
478 canvas_->drawPaint(paint.toSkPaint());
479 }
480 void CdlCanvas::onDrawPath(SkPath const& p, CdlPaint const& paint) {
481 canvas_->drawPath(p, paint.toSkPaint());
482 }
483 void CdlCanvas::onDrawRect(SkRect const& r, CdlPaint const& paint) {
484 canvas_->drawRect(r, paint.toSkPaint());
485 }
486 void CdlCanvas::onDrawOval(SkRect const& r, CdlPaint const& paint) {
487 canvas_->drawOval(r, paint.toSkPaint());
488 }
489
490 void CdlCanvas::onDrawRRect(SkRRect const& r, CdlPaint const& paint) {
491 canvas_->drawRRect(r, paint.toSkPaint());
492 }
493 void CdlCanvas::onDrawDRRect(const SkRRect& outer,
494 const SkRRect& inner,
495 const CdlPaint& paint) {
496 canvas_->drawDRRect(outer, inner, paint.toSkPaint());
497 }
498
499 void CdlCanvas::drawPicture(const CdlPicture* picture,
500 const SkMatrix* matrix,
501 const CdlPaint* paint) {
502 RETURN_ON_NULL(picture);
503
504 if (matrix && matrix->isIdentity()) {
505 matrix = nullptr;
506 }
507
508 this->onDrawPicture(picture, matrix, paint);
509 }
510
511 class CdlAutoCanvasMatrixPaint {
512 public:
513 CdlAutoCanvasMatrixPaint(CdlCanvas* canvas,
514 const SkMatrix* matrix,
515 const CdlPaint* paint,
516 const SkRect& bounds)
517 : fCanvas(canvas), fSaveCount(canvas->getSaveCount()) {
518 if (paint) {
519 SkRect newBounds = bounds;
520 if (matrix) {
521 matrix->mapRect(&newBounds);
522 }
523 canvas->saveLayer(&newBounds, paint);
524 } else if (matrix) {
525 canvas->save();
526 }
527
528 if (matrix) {
529 canvas->concat(*matrix);
530 }
531 }
532
533 ~CdlAutoCanvasMatrixPaint() { fCanvas->restoreToCount(fSaveCount); }
534
535 private:
536 CdlCanvas* fCanvas;
537 int fSaveCount;
538 };
539
540 void CdlCanvas::onDrawPicture(const CdlPicture* picture,
541 const SkMatrix* matrix,
542 const CdlPaint* paint) {
543 if (!paint || paint->canComputeFastBounds()) {
544 SkRect bounds = picture->cullRect();
545
546 if (paint) {
547 paint->computeFastBounds(bounds, &bounds);
548 }
549 if (matrix) {
550 matrix->mapRect(&bounds);
551 }
552 if (this->quickReject(bounds)) {
553 return;
554 }
555 }
556
557 CdlAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect());
558 picture->playback(this);
559 }
560
561 void CdlCanvas::onDrawAnnotation(SkRect const& r, char const* c, SkData* d) {
562 canvas_->drawAnnotation(r, c, d);
563 }
564
565 void CdlCanvas::onDrawText(const void* text,
566 size_t byteLength,
567 SkScalar x,
568 SkScalar y,
569 const CdlPaint& paint) {
570 canvas_->drawText(text, byteLength, x, y, paint.toSkPaint());
571 }
572
573 void CdlCanvas::onDrawPosText(const void* text,
574 size_t byteLength,
575 const SkPoint pos[],
576 const CdlPaint& paint) {
577 canvas_->drawPosText(text, byteLength, pos, paint.toSkPaint());
578 }
579
580 void CdlCanvas::onDrawTextBlob(const SkTextBlob* blob,
581 SkScalar x,
582 SkScalar y,
583 const CdlPaint& paint) {
584 canvas_->drawTextBlob(blob, x, y, paint.toSkPaint());
585 }
586
587 void CdlCanvas::onDrawImage(const SkImage* image,
588 SkScalar x,
589 SkScalar y,
590 const CdlPaint* paint) {
591 SkPaint sk_paint;
592 if (paint)
593 sk_paint = paint->toSkPaint();
594 canvas_->drawImage(image, x, y, paint ? &sk_paint : nullptr);
595 }
596
597 void CdlCanvas::onDrawImageRect(const SkImage* image,
598 const SkRect* src,
599 const SkRect& dst,
600 const CdlPaint* paint,
601 SkCanvas::SrcRectConstraint constraint) {
602 SkPaint sk_paint;
603 if (paint)
604 sk_paint = paint->toSkPaint();
605 canvas_->drawImageRect(image, *src, dst, paint ? &sk_paint : nullptr,
606 constraint);
607 }
608
609 void CdlCanvas::onDrawPoints(SkCanvas::PointMode mode,
610 size_t count,
611 const SkPoint pts[],
612 const CdlPaint& paint) {
613 canvas_->drawPoints(mode, count, pts, paint.toSkPaint());
614 }
615
616 CdlPassThroughCanvas::CdlPassThroughCanvas(SkCanvas* canvas)
617 : CdlCanvas(canvas) {}
618 CdlPassThroughCanvas::~CdlPassThroughCanvas() {}
619
620 #else // CDL_ENABLED
621
622 #include "third_party/skia/include/utils/SkNWayCanvas.h"
623
624 CdlPassThroughCanvas::CdlPassThroughCanvas(SkCanvas* canvas)
625 : SkNWayCanvas(canvas->getBaseLayerSize().width(),
626 canvas->getBaseLayerSize().height()) {
627 SkIRect raster_bounds;
628 canvas->getClipDeviceBounds(&raster_bounds);
629 this->clipRect(SkRect::MakeFromIRect(raster_bounds));
630 this->setMatrix(canvas->getTotalMatrix());
631 this->addCanvas(canvas);
632 }
633
634 CdlPassThroughCanvas::~CdlPassThroughCanvas() {}
635
636 #endif // CDL_ENABLED
OLDNEW
« no previous file with comments | « skia/ext/cdl_canvas.h ('k') | skia/ext/cdl_common.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698