3 * Copyright 2013 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
9 #include "SkDeferredCanvas.h"
11 #include "SkBitmapDevice.h"
12 #include "SkChunkAlloc.h"
13 #include "SkColorFilter.h"
14 #include "SkDrawFilter.h"
17 #include "SkPaintPriv.h"
20 #include "SkSurface.h"
23 // Deferred canvas will auto-flush when recording reaches this limit
24 kDefaultMaxRecordingStorageBytes = 64*1024*1024,
25 kDeferredCanvasBitmapSizeThreshold = ~0U, // Disables this feature
33 static bool should_draw_immediately(const SkBitmap* bitmap, const SkPaint* paint,
34 size_t bitmapSizeThreshold) {
35 if (bitmap && ((bitmap->getTexture() && !bitmap->isImmutable()) ||
36 (bitmap->getSize() > bitmapSizeThreshold))) {
40 SkShader* shader = paint->getShader();
41 // Here we detect the case where the shader is an SkBitmapProcShader
42 // with a gpu texture attached. Checking this without RTTI
43 // requires making the assumption that only gradient shaders
44 // and SkBitmapProcShader implement asABitmap(). The following
45 // code may need to be revised if that assumption is ever broken.
46 if (shader && !shader->asAGradient(NULL)) {
48 if (shader->asABitmap(&bm, NULL, NULL) &&
57 //-----------------------------------------------------------------------------
58 // DeferredPipeController
59 //-----------------------------------------------------------------------------
61 class DeferredPipeController : public SkGPipeController {
63 DeferredPipeController();
64 void setPlaybackCanvas(SkCanvas*);
65 virtual ~DeferredPipeController();
66 virtual void* requestBlock(size_t minRequest, size_t* actual) SK_OVERRIDE;
67 virtual void notifyWritten(size_t bytes) SK_OVERRIDE;
68 void playback(bool silent);
69 bool hasPendingCommands() const { return fAllocator.blockCount() != 0; }
70 size_t storageAllocatedForRecording() const { return fAllocator.totalCapacity(); }
76 PipeBlock(void* block, size_t size) { fBlock = block, fSize = size; }
82 SkChunkAlloc fAllocator;
83 SkTDArray<PipeBlock> fBlockList;
84 SkGPipeReader fReader;
87 DeferredPipeController::DeferredPipeController() :
88 fAllocator(kMinBlockSize) {
93 DeferredPipeController::~DeferredPipeController() {
97 void DeferredPipeController::setPlaybackCanvas(SkCanvas* canvas) {
98 fReader.setCanvas(canvas);
101 void* DeferredPipeController::requestBlock(size_t minRequest, size_t *actual) {
103 // Save the previous block for later
104 PipeBlock previousBloc(fBlock, fBytesWritten);
105 fBlockList.push(previousBloc);
107 size_t blockSize = SkTMax<size_t>(minRequest, kMinBlockSize);
108 fBlock = fAllocator.allocThrow(blockSize);
114 void DeferredPipeController::notifyWritten(size_t bytes) {
115 fBytesWritten += bytes;
118 void DeferredPipeController::playback(bool silent) {
119 uint32_t flags = silent ? SkGPipeReader::kSilent_PlaybackFlag : 0;
120 for (int currentBlock = 0; currentBlock < fBlockList.count(); currentBlock++ ) {
121 fReader.playback(fBlockList[currentBlock].fBlock, fBlockList[currentBlock].fSize,
127 fReader.playback(fBlock, fBytesWritten, flags);
131 // Release all allocated blocks
135 //-----------------------------------------------------------------------------
137 //-----------------------------------------------------------------------------
138 class SkDeferredDevice : public SkBaseDevice {
140 explicit SkDeferredDevice(SkSurface* surface);
143 void setNotificationClient(SkDeferredCanvas::NotificationClient* notificationClient);
144 SkCanvas* recordingCanvas();
145 SkCanvas* immediateCanvas() const {return fImmediateCanvas;}
146 SkBaseDevice* immediateDevice() const {return fImmediateCanvas->getTopDevice();}
147 SkImage* newImageSnapshot();
148 void setSurface(SkSurface* surface);
150 bool hasPendingCommands();
151 size_t storageAllocatedForRecording() const;
152 size_t freeMemoryIfPossible(size_t bytesToFree);
153 void flushPendingCommands(PlaybackMode);
154 void skipPendingCommands();
155 void setMaxRecordingStorage(size_t);
156 void recordedDrawCommand();
158 virtual SkImageInfo imageInfo() const SK_OVERRIDE;
160 virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE;
162 virtual SkBaseDevice* onCreateDevice(const SkImageInfo&, Usage) SK_OVERRIDE;
164 virtual SkSurface* newSurface(const SkImageInfo&, const SkSurfaceProps&) SK_OVERRIDE;
167 virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE;
168 virtual bool onReadPixels(const SkImageInfo&, void*, size_t, int x, int y) SK_OVERRIDE;
169 virtual bool onWritePixels(const SkImageInfo&, const void*, size_t, int x, int y) SK_OVERRIDE;
171 // The following methods are no-ops on a deferred device
172 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE {
176 // None of the following drawing methods should ever get called on the
178 virtual void clear(SkColor color) SK_OVERRIDE
180 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE
182 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
183 size_t count, const SkPoint[],
184 const SkPaint& paint) SK_OVERRIDE
186 virtual void drawRect(const SkDraw&, const SkRect& r,
187 const SkPaint& paint) SK_OVERRIDE
189 virtual void drawOval(const SkDraw&, const SkRect&, const SkPaint&) SK_OVERRIDE
191 virtual void drawRRect(const SkDraw&, const SkRRect& rr,
192 const SkPaint& paint) SK_OVERRIDE
194 virtual void drawPath(const SkDraw&, const SkPath& path,
195 const SkPaint& paint,
196 const SkMatrix* prePathMatrix = NULL,
197 bool pathIsMutable = false) SK_OVERRIDE
199 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
200 const SkMatrix& matrix, const SkPaint& paint) SK_OVERRIDE
202 virtual void drawBitmapRect(const SkDraw&, const SkBitmap&, const SkRect*,
203 const SkRect&, const SkPaint&,
204 SkCanvas::DrawBitmapRectFlags) SK_OVERRIDE
206 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
207 int x, int y, const SkPaint& paint) SK_OVERRIDE
209 virtual void drawText(const SkDraw&, const void* text, size_t len,
210 SkScalar x, SkScalar y, const SkPaint& paint) SK_OVERRIDE
212 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
213 const SkScalar pos[], int scalarsPerPos,
214 const SkPoint& offset, const SkPaint& paint) SK_OVERRIDE
216 virtual void drawTextOnPath(const SkDraw&, const void* text,
217 size_t len, const SkPath& path,
218 const SkMatrix* matrix,
219 const SkPaint& paint) SK_OVERRIDE
221 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
222 int vertexCount, const SkPoint verts[],
223 const SkPoint texs[], const SkColor colors[],
224 SkXfermode* xmode, const uint16_t indices[],
225 int indexCount, const SkPaint& paint) SK_OVERRIDE
227 virtual void drawPatch(const SkDraw&, const SkPoint cubics[12], const SkColor colors[4],
228 const SkPoint texCoords[4], SkXfermode* xmode,
229 const SkPaint& paint) SK_OVERRIDE
231 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
232 const SkPaint&) SK_OVERRIDE
235 virtual void lockPixels() SK_OVERRIDE {}
236 virtual void unlockPixels() SK_OVERRIDE {}
238 virtual bool allowImageFilter(const SkImageFilter*) SK_OVERRIDE {
241 virtual bool canHandleImageFilter(const SkImageFilter*) SK_OVERRIDE {
244 virtual bool filterImage(const SkImageFilter*, const SkBitmap&,
245 const SkImageFilter::Context&, SkBitmap*, SkIPoint*) SK_OVERRIDE {
250 virtual void flush() SK_OVERRIDE;
251 virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE {}
253 void beginRecording();
256 void prepareForImmediatePixelWrite();
258 DeferredPipeController fPipeController;
259 SkGPipeWriter fPipeWriter;
260 SkCanvas* fImmediateCanvas;
261 SkCanvas* fRecordingCanvas;
263 SkDeferredCanvas::NotificationClient* fNotificationClient;
265 bool fCanDiscardCanvasContents;
266 size_t fMaxRecordingStorageBytes;
267 size_t fPreviousStorageAllocated;
270 SkDeferredDevice::SkDeferredDevice(SkSurface* surface) {
271 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
272 fNotificationClient = NULL;
273 fImmediateCanvas = NULL;
275 this->setSurface(surface);
279 void SkDeferredDevice::setSurface(SkSurface* surface) {
280 SkRefCnt_SafeAssign(fImmediateCanvas, surface->getCanvas());
281 SkRefCnt_SafeAssign(fSurface, surface);
282 fPipeController.setPlaybackCanvas(fImmediateCanvas);
285 void SkDeferredDevice::init() {
286 fRecordingCanvas = NULL;
288 fCanDiscardCanvasContents = false;
289 fPreviousStorageAllocated = 0;
290 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
291 fNotificationClient = NULL;
292 this->beginRecording();
295 SkDeferredDevice::~SkDeferredDevice() {
296 this->flushPendingCommands(kSilent_PlaybackMode);
297 SkSafeUnref(fImmediateCanvas);
298 SkSafeUnref(fSurface);
301 void SkDeferredDevice::setMaxRecordingStorage(size_t maxStorage) {
302 fMaxRecordingStorageBytes = maxStorage;
303 this->recordingCanvas(); // Accessing the recording canvas applies the new limit.
306 void SkDeferredDevice::beginRecording() {
307 SkASSERT(NULL == fRecordingCanvas);
308 fRecordingCanvas = fPipeWriter.startRecording(&fPipeController, 0,
309 immediateDevice()->width(), immediateDevice()->height());
312 void SkDeferredDevice::setNotificationClient(
313 SkDeferredCanvas::NotificationClient* notificationClient) {
314 fNotificationClient = notificationClient;
317 void SkDeferredDevice::skipPendingCommands() {
318 if (!fRecordingCanvas->isDrawingToLayer()) {
319 fCanDiscardCanvasContents = true;
320 if (fPipeController.hasPendingCommands()) {
322 flushPendingCommands(kSilent_PlaybackMode);
327 bool SkDeferredDevice::isFreshFrame() {
328 bool ret = fFreshFrame;
333 bool SkDeferredDevice::hasPendingCommands() {
334 return fPipeController.hasPendingCommands();
337 void SkDeferredDevice::aboutToDraw()
339 if (fNotificationClient) {
340 fNotificationClient->prepareForDraw();
342 if (fCanDiscardCanvasContents) {
344 fSurface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
346 fCanDiscardCanvasContents = false;
350 void SkDeferredDevice::flushPendingCommands(PlaybackMode playbackMode) {
351 if (!fPipeController.hasPendingCommands()) {
354 if (playbackMode == kNormal_PlaybackMode) {
357 fPipeWriter.flushRecording(true);
358 fPipeController.playback(kSilent_PlaybackMode == playbackMode);
359 if (fNotificationClient) {
360 if (playbackMode == kSilent_PlaybackMode) {
361 fNotificationClient->skippedPendingDrawCommands();
363 fNotificationClient->flushedDrawCommands();
367 fPreviousStorageAllocated = storageAllocatedForRecording();
370 void SkDeferredDevice::flush() {
371 this->flushPendingCommands(kNormal_PlaybackMode);
372 fImmediateCanvas->flush();
375 size_t SkDeferredDevice::freeMemoryIfPossible(size_t bytesToFree) {
376 size_t val = fPipeWriter.freeMemoryIfPossible(bytesToFree);
377 fPreviousStorageAllocated = storageAllocatedForRecording();
381 size_t SkDeferredDevice::storageAllocatedForRecording() const {
382 return (fPipeController.storageAllocatedForRecording()
383 + fPipeWriter.storageAllocatedForRecording());
386 void SkDeferredDevice::recordedDrawCommand() {
387 size_t storageAllocated = this->storageAllocatedForRecording();
389 if (storageAllocated > fMaxRecordingStorageBytes) {
390 // First, attempt to reduce cache without flushing
391 size_t tryFree = storageAllocated - fMaxRecordingStorageBytes;
392 if (this->freeMemoryIfPossible(tryFree) < tryFree) {
393 // Flush is necessary to free more space.
394 this->flushPendingCommands(kNormal_PlaybackMode);
395 // Free as much as possible to avoid oscillating around fMaxRecordingStorageBytes
396 // which could cause a high flushing frequency.
397 this->freeMemoryIfPossible(~0U);
399 storageAllocated = this->storageAllocatedForRecording();
402 if (fNotificationClient &&
403 storageAllocated != fPreviousStorageAllocated) {
404 fPreviousStorageAllocated = storageAllocated;
405 fNotificationClient->storageAllocatedForRecordingChanged(storageAllocated);
409 SkCanvas* SkDeferredDevice::recordingCanvas() {
410 return fRecordingCanvas;
413 SkImage* SkDeferredDevice::newImageSnapshot() {
415 return fSurface ? fSurface->newImageSnapshot() : NULL;
418 SkImageInfo SkDeferredDevice::imageInfo() const {
419 return immediateDevice()->imageInfo();
422 GrRenderTarget* SkDeferredDevice::accessRenderTarget() {
423 this->flushPendingCommands(kNormal_PlaybackMode);
424 return immediateDevice()->accessRenderTarget();
427 void SkDeferredDevice::prepareForImmediatePixelWrite() {
428 // The purpose of the following code is to make sure commands are flushed, that
429 // aboutToDraw() is called and that notifyContentWillChange is called, without
430 // calling anything redundantly.
431 if (fPipeController.hasPendingCommands()) {
432 this->flushPendingCommands(kNormal_PlaybackMode);
434 bool mustNotifyDirectly = !fCanDiscardCanvasContents;
436 if (mustNotifyDirectly) {
437 fSurface->notifyContentWillChange(SkSurface::kRetain_ContentChangeMode);
441 fImmediateCanvas->flush();
444 bool SkDeferredDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
446 SkASSERT(x >= 0 && y >= 0);
447 SkASSERT(x + info.width() <= width());
448 SkASSERT(y + info.height() <= height());
450 this->flushPendingCommands(kNormal_PlaybackMode);
452 const SkImageInfo deviceInfo = this->imageInfo();
453 if (info.width() == deviceInfo.width() && info.height() == deviceInfo.height()) {
454 this->skipPendingCommands();
457 this->prepareForImmediatePixelWrite();
458 return immediateDevice()->onWritePixels(info, pixels, rowBytes, x, y);
461 const SkBitmap& SkDeferredDevice::onAccessBitmap() {
462 this->flushPendingCommands(kNormal_PlaybackMode);
463 return immediateDevice()->accessBitmap(false);
466 SkBaseDevice* SkDeferredDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
467 // Save layer usage not supported, and not required by SkDeferredCanvas.
468 SkASSERT(usage != kSaveLayer_Usage);
469 // Create a compatible non-deferred device.
470 // We do not create a deferred device because we know the new device
471 // will not be used with a deferred canvas (there is no API for that).
472 // And connecting a SkDeferredDevice to non-deferred canvas can result
473 // in unpredictable behavior.
474 return immediateDevice()->createCompatibleDevice(info);
477 SkSurface* SkDeferredDevice::newSurface(const SkImageInfo& info, const SkSurfaceProps& props) {
478 return this->immediateDevice()->newSurface(info, props);
481 bool SkDeferredDevice::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
483 this->flushPendingCommands(kNormal_PlaybackMode);
484 return fImmediateCanvas->readPixels(info, pixels, rowBytes, x, y);
487 class AutoImmediateDrawIfNeeded {
489 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkBitmap* bitmap,
490 const SkPaint* paint) {
491 this->init(canvas, bitmap, paint);
494 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkPaint* paint) {
495 this->init(canvas, NULL, paint);
498 ~AutoImmediateDrawIfNeeded() {
500 fCanvas->setDeferredDrawing(true);
504 void init(SkDeferredCanvas& canvas, const SkBitmap* bitmap, const SkPaint* paint) {
505 if (canvas.isDeferredDrawing() &&
506 should_draw_immediately(bitmap, paint, canvas.getBitmapSizeThreshold())) {
507 canvas.setDeferredDrawing(false);
514 SkDeferredCanvas* fCanvas;
517 SkDeferredCanvas* SkDeferredCanvas::Create(SkSurface* surface) {
518 SkAutoTUnref<SkDeferredDevice> deferredDevice(SkNEW_ARGS(SkDeferredDevice, (surface)));
519 return SkNEW_ARGS(SkDeferredCanvas, (deferredDevice));
522 SkDeferredCanvas::SkDeferredCanvas(SkDeferredDevice* device) : SkCanvas (device) {
526 void SkDeferredCanvas::init() {
527 fBitmapSizeThreshold = kDeferredCanvasBitmapSizeThreshold;
528 fDeferredDrawing = true; // On by default
529 fCachedCanvasSize.setEmpty();
530 fCachedCanvasSizeDirty = true;
533 void SkDeferredCanvas::setMaxRecordingStorage(size_t maxStorage) {
535 this->getDeferredDevice()->setMaxRecordingStorage(maxStorage);
538 size_t SkDeferredCanvas::storageAllocatedForRecording() const {
539 return this->getDeferredDevice()->storageAllocatedForRecording();
542 size_t SkDeferredCanvas::freeMemoryIfPossible(size_t bytesToFree) {
543 return this->getDeferredDevice()->freeMemoryIfPossible(bytesToFree);
546 void SkDeferredCanvas::setBitmapSizeThreshold(size_t sizeThreshold) {
547 fBitmapSizeThreshold = sizeThreshold;
550 void SkDeferredCanvas::recordedDrawCommand() {
551 if (fDeferredDrawing) {
552 this->getDeferredDevice()->recordedDrawCommand();
556 void SkDeferredCanvas::validate() const {
557 SkASSERT(this->getDevice());
560 SkCanvas* SkDeferredCanvas::drawingCanvas() const {
562 return fDeferredDrawing ? this->getDeferredDevice()->recordingCanvas() :
563 this->getDeferredDevice()->immediateCanvas();
566 SkCanvas* SkDeferredCanvas::immediateCanvas() const {
568 return this->getDeferredDevice()->immediateCanvas();
571 SkDeferredDevice* SkDeferredCanvas::getDeferredDevice() const {
572 return static_cast<SkDeferredDevice*>(this->getDevice());
575 void SkDeferredCanvas::setDeferredDrawing(bool val) {
576 this->validate(); // Must set device before calling this method
577 if (val != fDeferredDrawing) {
578 if (fDeferredDrawing) {
580 this->getDeferredDevice()->flushPendingCommands(kNormal_PlaybackMode);
582 fDeferredDrawing = val;
586 bool SkDeferredCanvas::isDeferredDrawing() const {
587 return fDeferredDrawing;
590 bool SkDeferredCanvas::isFreshFrame() const {
591 return this->getDeferredDevice()->isFreshFrame();
594 SkISize SkDeferredCanvas::getCanvasSize() const {
595 if (fCachedCanvasSizeDirty) {
596 fCachedCanvasSize = this->getBaseLayerSize();
597 fCachedCanvasSizeDirty = false;
599 return fCachedCanvasSize;
602 bool SkDeferredCanvas::hasPendingCommands() const {
603 return this->getDeferredDevice()->hasPendingCommands();
606 void SkDeferredCanvas::silentFlush() {
607 if (fDeferredDrawing) {
608 this->getDeferredDevice()->flushPendingCommands(kSilent_PlaybackMode);
612 SkDeferredCanvas::~SkDeferredCanvas() {
615 SkSurface* SkDeferredCanvas::setSurface(SkSurface* surface) {
616 SkDeferredDevice* deferredDevice = this->getDeferredDevice();
617 SkASSERT(deferredDevice);
618 // By swapping the surface into the existing device, we preserve
619 // all pending commands, which can help to seamlessly recover from
620 // a lost accelerated graphics context.
621 deferredDevice->setSurface(surface);
622 fCachedCanvasSizeDirty = true;
626 SkDeferredCanvas::NotificationClient* SkDeferredCanvas::setNotificationClient(
627 NotificationClient* notificationClient) {
629 SkDeferredDevice* deferredDevice = this->getDeferredDevice();
630 SkASSERT(deferredDevice);
631 if (deferredDevice) {
632 deferredDevice->setNotificationClient(notificationClient);
634 return notificationClient;
637 SkImage* SkDeferredCanvas::newImageSnapshot() {
638 SkDeferredDevice* deferredDevice = this->getDeferredDevice();
639 SkASSERT(deferredDevice);
640 return deferredDevice ? deferredDevice->newImageSnapshot() : NULL;
643 bool SkDeferredCanvas::isFullFrame(const SkRect* rect,
644 const SkPaint* paint) const {
645 SkCanvas* canvas = this->drawingCanvas();
646 SkISize canvasSize = this->getCanvasSize();
648 if (!canvas->getTotalMatrix().rectStaysRect()) {
649 return false; // conservative
652 SkRect transformedRect;
653 canvas->getTotalMatrix().mapRect(&transformedRect, *rect);
656 SkPaint::Style paintStyle = paint->getStyle();
657 if (!(paintStyle == SkPaint::kFill_Style ||
658 paintStyle == SkPaint::kStrokeAndFill_Style)) {
661 if (paint->getMaskFilter() || paint->getLooper()
662 || paint->getPathEffect() || paint->getImageFilter()) {
663 return false; // conservative
667 // The following test holds with AA enabled, and is conservative
668 // by a 0.5 pixel margin with AA disabled
669 if (transformedRect.fLeft > SkIntToScalar(0) ||
670 transformedRect.fTop > SkIntToScalar(0) ||
671 transformedRect.fRight < SkIntToScalar(canvasSize.fWidth) ||
672 transformedRect.fBottom < SkIntToScalar(canvasSize.fHeight)) {
677 return this->getClipStack()->quickContains(SkRect::MakeXYWH(0, 0,
678 SkIntToScalar(canvasSize.fWidth), SkIntToScalar(canvasSize.fHeight)));
681 void SkDeferredCanvas::willSave() {
682 this->drawingCanvas()->save();
683 this->recordedDrawCommand();
684 this->INHERITED::willSave();
687 SkCanvas::SaveLayerStrategy SkDeferredCanvas::willSaveLayer(const SkRect* bounds,
688 const SkPaint* paint, SaveFlags flags) {
689 this->drawingCanvas()->saveLayer(bounds, paint, flags);
690 this->recordedDrawCommand();
691 this->INHERITED::willSaveLayer(bounds, paint, flags);
692 // No need for a full layer.
693 return kNoLayer_SaveLayerStrategy;
696 void SkDeferredCanvas::willRestore() {
697 this->drawingCanvas()->restore();
698 this->recordedDrawCommand();
699 this->INHERITED::willRestore();
702 bool SkDeferredCanvas::isDrawingToLayer() const {
703 return this->drawingCanvas()->isDrawingToLayer();
706 void SkDeferredCanvas::didConcat(const SkMatrix& matrix) {
707 this->drawingCanvas()->concat(matrix);
708 this->recordedDrawCommand();
709 this->INHERITED::didConcat(matrix);
712 void SkDeferredCanvas::didSetMatrix(const SkMatrix& matrix) {
713 this->drawingCanvas()->setMatrix(matrix);
714 this->recordedDrawCommand();
715 this->INHERITED::didSetMatrix(matrix);
718 void SkDeferredCanvas::onClipRect(const SkRect& rect,
720 ClipEdgeStyle edgeStyle) {
721 this->drawingCanvas()->clipRect(rect, op, kSoft_ClipEdgeStyle == edgeStyle);
722 this->INHERITED::onClipRect(rect, op, edgeStyle);
723 this->recordedDrawCommand();
726 void SkDeferredCanvas::onClipRRect(const SkRRect& rrect,
728 ClipEdgeStyle edgeStyle) {
729 this->drawingCanvas()->clipRRect(rrect, op, kSoft_ClipEdgeStyle == edgeStyle);
730 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
731 this->recordedDrawCommand();
734 void SkDeferredCanvas::onClipPath(const SkPath& path,
736 ClipEdgeStyle edgeStyle) {
737 this->drawingCanvas()->clipPath(path, op, kSoft_ClipEdgeStyle == edgeStyle);
738 this->INHERITED::onClipPath(path, op, edgeStyle);
739 this->recordedDrawCommand();
742 void SkDeferredCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
743 this->drawingCanvas()->clipRegion(deviceRgn, op);
744 this->INHERITED::onClipRegion(deviceRgn, op);
745 this->recordedDrawCommand();
748 void SkDeferredCanvas::clear(SkColor color) {
749 // purge pending commands
750 if (fDeferredDrawing) {
751 this->getDeferredDevice()->skipPendingCommands();
754 this->drawingCanvas()->clear(color);
755 this->recordedDrawCommand();
758 void SkDeferredCanvas::drawPaint(const SkPaint& paint) {
759 if (fDeferredDrawing && this->isFullFrame(NULL, &paint) &&
760 isPaintOpaque(&paint)) {
761 this->getDeferredDevice()->skipPendingCommands();
763 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
764 this->drawingCanvas()->drawPaint(paint);
765 this->recordedDrawCommand();
768 void SkDeferredCanvas::drawPoints(PointMode mode, size_t count,
769 const SkPoint pts[], const SkPaint& paint) {
770 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
771 this->drawingCanvas()->drawPoints(mode, count, pts, paint);
772 this->recordedDrawCommand();
775 void SkDeferredCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
776 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
777 this->drawingCanvas()->drawOval(rect, paint);
778 this->recordedDrawCommand();
781 void SkDeferredCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
782 if (fDeferredDrawing && this->isFullFrame(&rect, &paint) &&
783 isPaintOpaque(&paint)) {
784 this->getDeferredDevice()->skipPendingCommands();
787 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
788 this->drawingCanvas()->drawRect(rect, paint);
789 this->recordedDrawCommand();
792 void SkDeferredCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
793 if (rrect.isRect()) {
794 this->SkDeferredCanvas::drawRect(rrect.getBounds(), paint);
795 } else if (rrect.isOval()) {
796 this->SkDeferredCanvas::drawOval(rrect.getBounds(), paint);
798 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
799 this->drawingCanvas()->drawRRect(rrect, paint);
800 this->recordedDrawCommand();
804 void SkDeferredCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
805 const SkPaint& paint) {
806 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
807 this->drawingCanvas()->drawDRRect(outer, inner, paint);
808 this->recordedDrawCommand();
811 void SkDeferredCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
812 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
813 this->drawingCanvas()->drawPath(path, paint);
814 this->recordedDrawCommand();
817 void SkDeferredCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
818 SkScalar top, const SkPaint* paint) {
819 SkRect bitmapRect = SkRect::MakeXYWH(left, top,
820 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
821 if (fDeferredDrawing &&
822 this->isFullFrame(&bitmapRect, paint) &&
823 isPaintOpaque(paint, &bitmap)) {
824 this->getDeferredDevice()->skipPendingCommands();
827 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
828 this->drawingCanvas()->drawBitmap(bitmap, left, top, paint);
829 this->recordedDrawCommand();
832 void SkDeferredCanvas::drawBitmapRectToRect(const SkBitmap& bitmap,
835 const SkPaint* paint,
836 DrawBitmapRectFlags flags) {
837 if (fDeferredDrawing &&
838 this->isFullFrame(&dst, paint) &&
839 isPaintOpaque(paint, &bitmap)) {
840 this->getDeferredDevice()->skipPendingCommands();
843 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
844 this->drawingCanvas()->drawBitmapRectToRect(bitmap, src, dst, paint, flags);
845 this->recordedDrawCommand();
849 void SkDeferredCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
851 const SkPaint* paint) {
852 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
853 // covers canvas entirely and transformed bitmap covers canvas entirely
854 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
855 this->drawingCanvas()->drawBitmapMatrix(bitmap, m, paint);
856 this->recordedDrawCommand();
859 void SkDeferredCanvas::drawBitmapNine(const SkBitmap& bitmap,
860 const SkIRect& center, const SkRect& dst,
861 const SkPaint* paint) {
862 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
863 // covers canvas entirely and dst covers canvas entirely
864 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
865 this->drawingCanvas()->drawBitmapNine(bitmap, center, dst, paint);
866 this->recordedDrawCommand();
869 void SkDeferredCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
870 const SkPaint* paint) {
871 SkRect bitmapRect = SkRect::MakeXYWH(
874 SkIntToScalar(bitmap.width()),
875 SkIntToScalar(bitmap.height()));
876 if (fDeferredDrawing &&
877 this->isFullFrame(&bitmapRect, paint) &&
878 isPaintOpaque(paint, &bitmap)) {
879 this->getDeferredDevice()->skipPendingCommands();
882 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
883 this->drawingCanvas()->drawSprite(bitmap, left, top, paint);
884 this->recordedDrawCommand();
887 void SkDeferredCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
888 const SkPaint& paint) {
889 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
890 this->drawingCanvas()->drawText(text, byteLength, x, y, paint);
891 this->recordedDrawCommand();
894 void SkDeferredCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
895 const SkPaint& paint) {
896 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
897 this->drawingCanvas()->drawPosText(text, byteLength, pos, paint);
898 this->recordedDrawCommand();
901 void SkDeferredCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
902 SkScalar constY, const SkPaint& paint) {
903 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
904 this->drawingCanvas()->drawPosTextH(text, byteLength, xpos, constY, paint);
905 this->recordedDrawCommand();
908 void SkDeferredCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
909 const SkMatrix* matrix, const SkPaint& paint) {
910 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
911 this->drawingCanvas()->drawTextOnPath(text, byteLength, path, matrix, paint);
912 this->recordedDrawCommand();
915 void SkDeferredCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y,
916 const SkPaint& paint) {
917 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
918 this->drawingCanvas()->drawTextBlob(blob, x, y, paint);
919 this->recordedDrawCommand();
922 void SkDeferredCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix,
923 const SkPaint* paint) {
924 this->drawingCanvas()->drawPicture(picture, matrix, paint);
925 this->recordedDrawCommand();
928 void SkDeferredCanvas::drawVertices(VertexMode vmode, int vertexCount,
929 const SkPoint vertices[],
930 const SkPoint texs[],
931 const SkColor colors[], SkXfermode* xmode,
932 const uint16_t indices[], int indexCount,
933 const SkPaint& paint) {
934 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
935 this->drawingCanvas()->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode,
936 indices, indexCount, paint);
937 this->recordedDrawCommand();
940 void SkDeferredCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4],
941 const SkPoint texCoords[4], SkXfermode* xmode,
942 const SkPaint& paint) {
943 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
944 this->drawingCanvas()->drawPatch(cubics, colors, texCoords, xmode, paint);
945 this->recordedDrawCommand();
948 SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) {
949 this->drawingCanvas()->setDrawFilter(filter);
950 this->INHERITED::setDrawFilter(filter);
951 this->recordedDrawCommand();
955 SkCanvas* SkDeferredCanvas::canvasForDrawIter() {
956 return this->drawingCanvas();