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
34 bool shouldDrawImmediately(const SkBitmap* bitmap, const SkPaint* paint,
35 size_t bitmapSizeThreshold) {
36 if (bitmap && ((bitmap->getTexture() && !bitmap->isImmutable()) ||
37 (bitmap->getSize() > bitmapSizeThreshold))) {
41 SkShader* shader = paint->getShader();
42 // Here we detect the case where the shader is an SkBitmapProcShader
43 // with a gpu texture attached. Checking this without RTTI
44 // requires making the assumption that only gradient shaders
45 // and SkBitmapProcShader implement asABitmap(). The following
46 // code may need to be revised if that assumption is ever broken.
47 if (shader && !shader->asAGradient(NULL)) {
49 if (shader->asABitmap(&bm, NULL, NULL) &&
50 NULL != bm.getTexture()) {
59 //-----------------------------------------------------------------------------
60 // DeferredPipeController
61 //-----------------------------------------------------------------------------
63 class DeferredPipeController : public SkGPipeController {
65 DeferredPipeController();
66 void setPlaybackCanvas(SkCanvas*);
67 virtual ~DeferredPipeController();
68 virtual void* requestBlock(size_t minRequest, size_t* actual) SK_OVERRIDE;
69 virtual void notifyWritten(size_t bytes) SK_OVERRIDE;
70 void playback(bool silent);
71 bool hasPendingCommands() const { return fAllocator.blockCount() != 0; }
72 size_t storageAllocatedForRecording() const { return fAllocator.totalCapacity(); }
78 PipeBlock(void* block, size_t size) { fBlock = block, fSize = size; }
84 SkChunkAlloc fAllocator;
85 SkTDArray<PipeBlock> fBlockList;
86 SkGPipeReader fReader;
89 DeferredPipeController::DeferredPipeController() :
90 fAllocator(kMinBlockSize) {
95 DeferredPipeController::~DeferredPipeController() {
99 void DeferredPipeController::setPlaybackCanvas(SkCanvas* canvas) {
100 fReader.setCanvas(canvas);
103 void* DeferredPipeController::requestBlock(size_t minRequest, size_t *actual) {
105 // Save the previous block for later
106 PipeBlock previousBloc(fBlock, fBytesWritten);
107 fBlockList.push(previousBloc);
109 size_t blockSize = SkTMax<size_t>(minRequest, kMinBlockSize);
110 fBlock = fAllocator.allocThrow(blockSize);
116 void DeferredPipeController::notifyWritten(size_t bytes) {
117 fBytesWritten += bytes;
120 void DeferredPipeController::playback(bool silent) {
121 uint32_t flags = silent ? SkGPipeReader::kSilent_PlaybackFlag : 0;
122 for (int currentBlock = 0; currentBlock < fBlockList.count(); currentBlock++ ) {
123 fReader.playback(fBlockList[currentBlock].fBlock, fBlockList[currentBlock].fSize,
129 fReader.playback(fBlock, fBytesWritten, flags);
133 // Release all allocated blocks
137 //-----------------------------------------------------------------------------
139 //-----------------------------------------------------------------------------
140 // FIXME: Derive from SkBaseDevice.
141 class DeferredDevice : public SkBitmapDevice {
143 explicit DeferredDevice(SkSurface* surface);
146 void setNotificationClient(SkDeferredCanvas::NotificationClient* notificationClient);
147 SkCanvas* recordingCanvas();
148 SkCanvas* immediateCanvas() const {return fImmediateCanvas;}
149 SkBaseDevice* immediateDevice() const {return fImmediateCanvas->getTopDevice();}
150 SkImage* newImageSnapshot();
151 void setSurface(SkSurface* surface);
153 bool hasPendingCommands();
154 size_t storageAllocatedForRecording() const;
155 size_t freeMemoryIfPossible(size_t bytesToFree);
156 size_t getBitmapSizeThreshold() const;
157 void setBitmapSizeThreshold(size_t sizeThreshold);
158 void flushPendingCommands(PlaybackMode);
159 void skipPendingCommands();
160 void setMaxRecordingStorage(size_t);
161 void recordedDrawCommand();
163 virtual uint32_t getDeviceCapabilities() SK_OVERRIDE;
164 virtual int width() const SK_OVERRIDE;
165 virtual int height() const SK_OVERRIDE;
166 virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE;
168 virtual SkBaseDevice* onCreateCompatibleDevice(SkBitmap::Config config,
169 int width, int height,
171 Usage usage) SK_OVERRIDE;
173 virtual void writePixels(const SkBitmap& bitmap, int x, int y,
174 SkCanvas::Config8888 config8888) SK_OVERRIDE;
176 virtual SkSurface* newSurface(const SkImageInfo&) SK_OVERRIDE;
179 virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE;
180 virtual bool onReadPixels(const SkBitmap& bitmap,
182 SkCanvas::Config8888 config8888) SK_OVERRIDE;
184 // The following methods are no-ops on a deferred device
185 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE {
189 // None of the following drawing methods should ever get called on the
191 virtual void clear(SkColor color) SK_OVERRIDE
193 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE
195 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
196 size_t count, const SkPoint[],
197 const SkPaint& paint) SK_OVERRIDE
199 virtual void drawRect(const SkDraw&, const SkRect& r,
200 const SkPaint& paint) SK_OVERRIDE
202 virtual void drawRRect(const SkDraw&, const SkRRect& rr,
203 const SkPaint& paint) SK_OVERRIDE
205 virtual void drawPath(const SkDraw&, const SkPath& path,
206 const SkPaint& paint,
207 const SkMatrix* prePathMatrix = NULL,
208 bool pathIsMutable = false) SK_OVERRIDE
210 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
211 const SkMatrix& matrix, const SkPaint& paint) SK_OVERRIDE
213 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
214 int x, int y, const SkPaint& paint) SK_OVERRIDE
216 virtual void drawText(const SkDraw&, const void* text, size_t len,
217 SkScalar x, SkScalar y, const SkPaint& paint) SK_OVERRIDE
219 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
220 const SkScalar pos[], SkScalar constY,
221 int scalarsPerPos, const SkPaint& paint) SK_OVERRIDE
223 virtual void drawTextOnPath(const SkDraw&, const void* text,
224 size_t len, const SkPath& path,
225 const SkMatrix* matrix,
226 const SkPaint& paint) SK_OVERRIDE
228 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
229 int vertexCount, const SkPoint verts[],
230 const SkPoint texs[], const SkColor colors[],
231 SkXfermode* xmode, const uint16_t indices[],
232 int indexCount, const SkPaint& paint) SK_OVERRIDE
234 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
235 const SkPaint&) SK_OVERRIDE
238 virtual void flush() SK_OVERRIDE;
240 void beginRecording();
243 void prepareForImmediatePixelWrite();
245 DeferredPipeController fPipeController;
246 SkGPipeWriter fPipeWriter;
247 SkCanvas* fImmediateCanvas;
248 SkCanvas* fRecordingCanvas;
250 SkDeferredCanvas::NotificationClient* fNotificationClient;
252 bool fCanDiscardCanvasContents;
253 size_t fMaxRecordingStorageBytes;
254 size_t fPreviousStorageAllocated;
255 size_t fBitmapSizeThreshold;
258 DeferredDevice::DeferredDevice(SkSurface* surface)
259 : SkBitmapDevice(SkBitmap::kNo_Config,
260 surface->getCanvas()->getDevice()->width(),
261 surface->getCanvas()->getDevice()->height(),
262 surface->getCanvas()->getDevice()->isOpaque(),
263 surface->getCanvas()->getDevice()->getDeviceProperties()) {
264 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
265 fNotificationClient = NULL;
266 fImmediateCanvas = NULL;
268 this->setSurface(surface);
272 void DeferredDevice::setSurface(SkSurface* surface) {
273 SkRefCnt_SafeAssign(fImmediateCanvas, surface->getCanvas());
274 SkRefCnt_SafeAssign(fSurface, surface);
275 fPipeController.setPlaybackCanvas(fImmediateCanvas);
278 void DeferredDevice::init() {
279 fRecordingCanvas = NULL;
281 fCanDiscardCanvasContents = false;
282 fPreviousStorageAllocated = 0;
283 fBitmapSizeThreshold = kDeferredCanvasBitmapSizeThreshold;
284 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
285 fNotificationClient = NULL;
286 this->beginRecording();
289 DeferredDevice::~DeferredDevice() {
290 this->flushPendingCommands(kSilent_PlaybackMode);
291 SkSafeUnref(fImmediateCanvas);
292 SkSafeUnref(fSurface);
295 void DeferredDevice::setMaxRecordingStorage(size_t maxStorage) {
296 fMaxRecordingStorageBytes = maxStorage;
297 this->recordingCanvas(); // Accessing the recording canvas applies the new limit.
300 void DeferredDevice::beginRecording() {
301 SkASSERT(NULL == fRecordingCanvas);
302 fRecordingCanvas = fPipeWriter.startRecording(&fPipeController, 0,
303 immediateDevice()->width(), immediateDevice()->height());
306 void DeferredDevice::setNotificationClient(
307 SkDeferredCanvas::NotificationClient* notificationClient) {
308 fNotificationClient = notificationClient;
311 void DeferredDevice::skipPendingCommands() {
312 if (!fRecordingCanvas->isDrawingToLayer()) {
313 fCanDiscardCanvasContents = true;
314 if (fPipeController.hasPendingCommands()) {
316 flushPendingCommands(kSilent_PlaybackMode);
317 if (fNotificationClient) {
318 fNotificationClient->skippedPendingDrawCommands();
324 bool DeferredDevice::isFreshFrame() {
325 bool ret = fFreshFrame;
330 bool DeferredDevice::hasPendingCommands() {
331 return fPipeController.hasPendingCommands();
334 void DeferredDevice::aboutToDraw()
336 if (NULL != fNotificationClient) {
337 fNotificationClient->prepareForDraw();
339 if (fCanDiscardCanvasContents) {
340 if (NULL != fSurface) {
341 fSurface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
343 fCanDiscardCanvasContents = false;
347 void DeferredDevice::flushPendingCommands(PlaybackMode playbackMode) {
348 if (!fPipeController.hasPendingCommands()) {
351 if (playbackMode == kNormal_PlaybackMode) {
354 fPipeWriter.flushRecording(true);
355 fPipeController.playback(kSilent_PlaybackMode == playbackMode);
356 if (playbackMode == kNormal_PlaybackMode && fNotificationClient) {
357 fNotificationClient->flushedDrawCommands();
359 fPreviousStorageAllocated = storageAllocatedForRecording();
362 void DeferredDevice::flush() {
363 this->flushPendingCommands(kNormal_PlaybackMode);
364 fImmediateCanvas->flush();
367 size_t DeferredDevice::freeMemoryIfPossible(size_t bytesToFree) {
368 size_t val = fPipeWriter.freeMemoryIfPossible(bytesToFree);
369 fPreviousStorageAllocated = storageAllocatedForRecording();
373 size_t DeferredDevice::getBitmapSizeThreshold() const {
374 return fBitmapSizeThreshold;
377 void DeferredDevice::setBitmapSizeThreshold(size_t sizeThreshold) {
378 fBitmapSizeThreshold = sizeThreshold;
381 size_t DeferredDevice::storageAllocatedForRecording() const {
382 return (fPipeController.storageAllocatedForRecording()
383 + fPipeWriter.storageAllocatedForRecording());
386 void DeferredDevice::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* DeferredDevice::recordingCanvas() {
410 return fRecordingCanvas;
413 SkImage* DeferredDevice::newImageSnapshot() {
415 return fSurface ? fSurface->newImageSnapshot() : NULL;
418 uint32_t DeferredDevice::getDeviceCapabilities() {
419 return immediateDevice()->getDeviceCapabilities();
422 int DeferredDevice::width() const {
423 return immediateDevice()->width();
426 int DeferredDevice::height() const {
427 return immediateDevice()->height();
430 GrRenderTarget* DeferredDevice::accessRenderTarget() {
431 this->flushPendingCommands(kNormal_PlaybackMode);
432 return immediateDevice()->accessRenderTarget();
435 void DeferredDevice::prepareForImmediatePixelWrite() {
436 // The purpose of the following code is to make sure commands are flushed, that
437 // aboutToDraw() is called and that notifyContentWillChange is called, without
438 // calling anything redundantly.
439 if (fPipeController.hasPendingCommands()) {
440 this->flushPendingCommands(kNormal_PlaybackMode);
442 bool mustNotifyDirectly = !fCanDiscardCanvasContents;
444 if (mustNotifyDirectly) {
445 fSurface->notifyContentWillChange(SkSurface::kRetain_ContentChangeMode);
449 fImmediateCanvas->flush();
452 void DeferredDevice::writePixels(const SkBitmap& bitmap,
453 int x, int y, SkCanvas::Config8888 config8888) {
455 if (x <= 0 && y <= 0 && (x + bitmap.width()) >= width() &&
456 (y + bitmap.height()) >= height()) {
457 this->skipPendingCommands();
460 if (SkBitmap::kARGB_8888_Config == bitmap.config() &&
461 SkCanvas::kNative_Premul_Config8888 != config8888 &&
462 kPMColorAlias != config8888) {
463 //Special case config: no deferral
464 prepareForImmediatePixelWrite();
465 immediateDevice()->writePixels(bitmap, x, y, config8888);
470 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
471 if (shouldDrawImmediately(&bitmap, NULL, getBitmapSizeThreshold())) {
472 prepareForImmediatePixelWrite();
473 fImmediateCanvas->drawSprite(bitmap, x, y, &paint);
475 this->recordingCanvas()->drawSprite(bitmap, x, y, &paint);
476 this->recordedDrawCommand();
481 const SkBitmap& DeferredDevice::onAccessBitmap() {
482 this->flushPendingCommands(kNormal_PlaybackMode);
483 return immediateDevice()->accessBitmap(false);
486 SkBaseDevice* DeferredDevice::onCreateCompatibleDevice(
487 SkBitmap::Config config, int width, int height, bool isOpaque,
490 // Save layer usage not supported, and not required by SkDeferredCanvas.
491 SkASSERT(usage != kSaveLayer_Usage);
492 // Create a compatible non-deferred device.
493 // We do not create a deferred device because we know the new device
494 // will not be used with a deferred canvas (there is no API for that).
495 // And connecting a DeferredDevice to non-deferred canvas can result
496 // in unpredictable behavior.
497 return immediateDevice()->createCompatibleDevice(config, width, height, isOpaque);
500 SkSurface* DeferredDevice::newSurface(const SkImageInfo& info) {
501 return this->immediateDevice()->newSurface(info);
504 bool DeferredDevice::onReadPixels(
505 const SkBitmap& bitmap, int x, int y, SkCanvas::Config8888 config8888) {
506 this->flushPendingCommands(kNormal_PlaybackMode);
507 return fImmediateCanvas->readPixels(const_cast<SkBitmap*>(&bitmap),
511 class AutoImmediateDrawIfNeeded {
513 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkBitmap* bitmap,
514 const SkPaint* paint) {
515 this->init(canvas, bitmap, paint);
518 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkPaint* paint) {
519 this->init(canvas, NULL, paint);
522 ~AutoImmediateDrawIfNeeded() {
524 fCanvas->setDeferredDrawing(true);
528 void init(SkDeferredCanvas& canvas, const SkBitmap* bitmap, const SkPaint* paint)
530 DeferredDevice* device = static_cast<DeferredDevice*>(canvas.getDevice());
531 if (canvas.isDeferredDrawing() && (NULL != device) &&
532 shouldDrawImmediately(bitmap, paint, device->getBitmapSizeThreshold())) {
533 canvas.setDeferredDrawing(false);
540 SkDeferredCanvas* fCanvas;
543 SkDeferredCanvas* SkDeferredCanvas::Create(SkSurface* surface) {
544 SkAutoTUnref<DeferredDevice> deferredDevice(SkNEW_ARGS(DeferredDevice, (surface)));
545 return SkNEW_ARGS(SkDeferredCanvas, (deferredDevice));
548 SkDeferredCanvas::SkDeferredCanvas(DeferredDevice* device) : SkCanvas (device) {
552 void SkDeferredCanvas::init() {
553 fDeferredDrawing = true; // On by default
556 void SkDeferredCanvas::setMaxRecordingStorage(size_t maxStorage) {
558 this->getDeferredDevice()->setMaxRecordingStorage(maxStorage);
561 size_t SkDeferredCanvas::storageAllocatedForRecording() const {
562 return this->getDeferredDevice()->storageAllocatedForRecording();
565 size_t SkDeferredCanvas::freeMemoryIfPossible(size_t bytesToFree) {
566 return this->getDeferredDevice()->freeMemoryIfPossible(bytesToFree);
569 void SkDeferredCanvas::setBitmapSizeThreshold(size_t sizeThreshold) {
570 DeferredDevice* deferredDevice = this->getDeferredDevice();
571 SkASSERT(deferredDevice);
572 deferredDevice->setBitmapSizeThreshold(sizeThreshold);
575 void SkDeferredCanvas::recordedDrawCommand() {
576 if (fDeferredDrawing) {
577 this->getDeferredDevice()->recordedDrawCommand();
581 void SkDeferredCanvas::validate() const {
582 SkASSERT(this->getDevice());
585 SkCanvas* SkDeferredCanvas::drawingCanvas() const {
587 return fDeferredDrawing ? this->getDeferredDevice()->recordingCanvas() :
588 this->getDeferredDevice()->immediateCanvas();
591 SkCanvas* SkDeferredCanvas::immediateCanvas() const {
593 return this->getDeferredDevice()->immediateCanvas();
596 DeferredDevice* SkDeferredCanvas::getDeferredDevice() const {
597 return static_cast<DeferredDevice*>(this->getDevice());
600 void SkDeferredCanvas::setDeferredDrawing(bool val) {
601 this->validate(); // Must set device before calling this method
602 if (val != fDeferredDrawing) {
603 if (fDeferredDrawing) {
605 this->getDeferredDevice()->flushPendingCommands(kNormal_PlaybackMode);
607 fDeferredDrawing = val;
611 bool SkDeferredCanvas::isDeferredDrawing() const {
612 return fDeferredDrawing;
615 bool SkDeferredCanvas::isFreshFrame() const {
616 return this->getDeferredDevice()->isFreshFrame();
619 bool SkDeferredCanvas::hasPendingCommands() const {
620 return this->getDeferredDevice()->hasPendingCommands();
623 void SkDeferredCanvas::silentFlush() {
624 if (fDeferredDrawing) {
625 this->getDeferredDevice()->flushPendingCommands(kSilent_PlaybackMode);
629 SkDeferredCanvas::~SkDeferredCanvas() {
632 SkSurface* SkDeferredCanvas::setSurface(SkSurface* surface) {
633 DeferredDevice* deferredDevice = this->getDeferredDevice();
634 SkASSERT(NULL != deferredDevice);
635 // By swapping the surface into the existing device, we preserve
636 // all pending commands, which can help to seamlessly recover from
637 // a lost accelerated graphics context.
638 deferredDevice->setSurface(surface);
642 SkDeferredCanvas::NotificationClient* SkDeferredCanvas::setNotificationClient(
643 NotificationClient* notificationClient) {
645 DeferredDevice* deferredDevice = this->getDeferredDevice();
646 SkASSERT(deferredDevice);
647 if (deferredDevice) {
648 deferredDevice->setNotificationClient(notificationClient);
650 return notificationClient;
653 SkImage* SkDeferredCanvas::newImageSnapshot() {
654 DeferredDevice* deferredDevice = this->getDeferredDevice();
655 SkASSERT(deferredDevice);
656 return deferredDevice ? deferredDevice->newImageSnapshot() : NULL;
659 bool SkDeferredCanvas::isFullFrame(const SkRect* rect,
660 const SkPaint* paint) const {
661 SkCanvas* canvas = this->drawingCanvas();
662 SkISize canvasSize = this->getDeviceSize();
664 if (!canvas->getTotalMatrix().rectStaysRect()) {
665 return false; // conservative
668 SkRect transformedRect;
669 canvas->getTotalMatrix().mapRect(&transformedRect, *rect);
672 SkPaint::Style paintStyle = paint->getStyle();
673 if (!(paintStyle == SkPaint::kFill_Style ||
674 paintStyle == SkPaint::kStrokeAndFill_Style)) {
677 if (paint->getMaskFilter() || paint->getLooper()
678 || paint->getPathEffect() || paint->getImageFilter()) {
679 return false; // conservative
683 // The following test holds with AA enabled, and is conservative
684 // by a 0.5 pixel margin with AA disabled
685 if (transformedRect.fLeft > SkIntToScalar(0) ||
686 transformedRect.fTop > SkIntToScalar(0) ||
687 transformedRect.fRight < SkIntToScalar(canvasSize.fWidth) ||
688 transformedRect.fBottom < SkIntToScalar(canvasSize.fHeight)) {
693 return this->getClipStack()->quickContains(SkRect::MakeXYWH(0, 0,
694 SkIntToScalar(canvasSize.fWidth), SkIntToScalar(canvasSize.fHeight)));
697 int SkDeferredCanvas::save(SaveFlags flags) {
698 this->drawingCanvas()->save(flags);
699 int val = this->INHERITED::save(flags);
700 this->recordedDrawCommand();
705 int SkDeferredCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
707 this->drawingCanvas()->saveLayer(bounds, paint, flags);
708 int count = this->INHERITED::save(flags);
709 this->clipRectBounds(bounds, flags, NULL);
710 this->recordedDrawCommand();
715 void SkDeferredCanvas::restore() {
716 this->drawingCanvas()->restore();
717 this->INHERITED::restore();
718 this->recordedDrawCommand();
721 bool SkDeferredCanvas::isDrawingToLayer() const {
722 return this->drawingCanvas()->isDrawingToLayer();
725 bool SkDeferredCanvas::translate(SkScalar dx, SkScalar dy) {
726 this->drawingCanvas()->translate(dx, dy);
727 bool val = this->INHERITED::translate(dx, dy);
728 this->recordedDrawCommand();
732 bool SkDeferredCanvas::scale(SkScalar sx, SkScalar sy) {
733 this->drawingCanvas()->scale(sx, sy);
734 bool val = this->INHERITED::scale(sx, sy);
735 this->recordedDrawCommand();
739 bool SkDeferredCanvas::rotate(SkScalar degrees) {
740 this->drawingCanvas()->rotate(degrees);
741 bool val = this->INHERITED::rotate(degrees);
742 this->recordedDrawCommand();
746 bool SkDeferredCanvas::skew(SkScalar sx, SkScalar sy) {
747 this->drawingCanvas()->skew(sx, sy);
748 bool val = this->INHERITED::skew(sx, sy);
749 this->recordedDrawCommand();
753 bool SkDeferredCanvas::concat(const SkMatrix& matrix) {
754 this->drawingCanvas()->concat(matrix);
755 bool val = this->INHERITED::concat(matrix);
756 this->recordedDrawCommand();
760 void SkDeferredCanvas::setMatrix(const SkMatrix& matrix) {
761 this->drawingCanvas()->setMatrix(matrix);
762 this->INHERITED::setMatrix(matrix);
763 this->recordedDrawCommand();
766 bool SkDeferredCanvas::clipRect(const SkRect& rect,
769 this->drawingCanvas()->clipRect(rect, op, doAntiAlias);
770 bool val = this->INHERITED::clipRect(rect, op, doAntiAlias);
771 this->recordedDrawCommand();
775 bool SkDeferredCanvas::clipRRect(const SkRRect& rrect,
778 this->drawingCanvas()->clipRRect(rrect, op, doAntiAlias);
779 bool val = this->INHERITED::clipRRect(rrect, op, doAntiAlias);
780 this->recordedDrawCommand();
784 bool SkDeferredCanvas::clipPath(const SkPath& path,
787 this->drawingCanvas()->clipPath(path, op, doAntiAlias);
788 bool val = this->INHERITED::clipPath(path, op, doAntiAlias);
789 this->recordedDrawCommand();
793 bool SkDeferredCanvas::clipRegion(const SkRegion& deviceRgn,
795 this->drawingCanvas()->clipRegion(deviceRgn, op);
796 bool val = this->INHERITED::clipRegion(deviceRgn, op);
797 this->recordedDrawCommand();
801 void SkDeferredCanvas::clear(SkColor color) {
802 // purge pending commands
803 if (fDeferredDrawing) {
804 this->getDeferredDevice()->skipPendingCommands();
807 this->drawingCanvas()->clear(color);
808 this->recordedDrawCommand();
811 void SkDeferredCanvas::drawPaint(const SkPaint& paint) {
812 if (fDeferredDrawing && this->isFullFrame(NULL, &paint) &&
813 isPaintOpaque(&paint)) {
814 this->getDeferredDevice()->skipPendingCommands();
816 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
817 this->drawingCanvas()->drawPaint(paint);
818 this->recordedDrawCommand();
821 void SkDeferredCanvas::drawPoints(PointMode mode, size_t count,
822 const SkPoint pts[], const SkPaint& paint) {
823 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
824 this->drawingCanvas()->drawPoints(mode, count, pts, paint);
825 this->recordedDrawCommand();
828 void SkDeferredCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
829 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
830 this->drawingCanvas()->drawOval(rect, paint);
831 this->recordedDrawCommand();
834 void SkDeferredCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
835 if (fDeferredDrawing && this->isFullFrame(&rect, &paint) &&
836 isPaintOpaque(&paint)) {
837 this->getDeferredDevice()->skipPendingCommands();
840 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
841 this->drawingCanvas()->drawRect(rect, paint);
842 this->recordedDrawCommand();
845 void SkDeferredCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
846 if (rrect.isRect()) {
847 this->SkDeferredCanvas::drawRect(rrect.getBounds(), paint);
848 } else if (rrect.isOval()) {
849 this->SkDeferredCanvas::drawOval(rrect.getBounds(), paint);
851 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
852 this->drawingCanvas()->drawRRect(rrect, paint);
853 this->recordedDrawCommand();
857 void SkDeferredCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
858 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
859 this->drawingCanvas()->drawPath(path, paint);
860 this->recordedDrawCommand();
863 void SkDeferredCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
864 SkScalar top, const SkPaint* paint) {
865 SkRect bitmapRect = SkRect::MakeXYWH(left, top,
866 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
867 if (fDeferredDrawing &&
868 this->isFullFrame(&bitmapRect, paint) &&
869 isPaintOpaque(paint, &bitmap)) {
870 this->getDeferredDevice()->skipPendingCommands();
873 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
874 this->drawingCanvas()->drawBitmap(bitmap, left, top, paint);
875 this->recordedDrawCommand();
878 void SkDeferredCanvas::drawBitmapRectToRect(const SkBitmap& bitmap,
881 const SkPaint* paint,
882 DrawBitmapRectFlags flags) {
883 if (fDeferredDrawing &&
884 this->isFullFrame(&dst, paint) &&
885 isPaintOpaque(paint, &bitmap)) {
886 this->getDeferredDevice()->skipPendingCommands();
889 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
890 this->drawingCanvas()->drawBitmapRectToRect(bitmap, src, dst, paint, flags);
891 this->recordedDrawCommand();
895 void SkDeferredCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
897 const SkPaint* paint) {
898 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
899 // covers canvas entirely and transformed bitmap covers canvas entirely
900 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
901 this->drawingCanvas()->drawBitmapMatrix(bitmap, m, paint);
902 this->recordedDrawCommand();
905 void SkDeferredCanvas::drawBitmapNine(const SkBitmap& bitmap,
906 const SkIRect& center, const SkRect& dst,
907 const SkPaint* paint) {
908 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
909 // covers canvas entirely and dst covers canvas entirely
910 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
911 this->drawingCanvas()->drawBitmapNine(bitmap, center, dst, paint);
912 this->recordedDrawCommand();
915 void SkDeferredCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
916 const SkPaint* paint) {
917 SkRect bitmapRect = SkRect::MakeXYWH(
920 SkIntToScalar(bitmap.width()),
921 SkIntToScalar(bitmap.height()));
922 if (fDeferredDrawing &&
923 this->isFullFrame(&bitmapRect, paint) &&
924 isPaintOpaque(paint, &bitmap)) {
925 this->getDeferredDevice()->skipPendingCommands();
928 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
929 this->drawingCanvas()->drawSprite(bitmap, left, top, paint);
930 this->recordedDrawCommand();
933 void SkDeferredCanvas::drawText(const void* text, size_t byteLength,
934 SkScalar x, SkScalar y, const SkPaint& paint) {
935 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
936 this->drawingCanvas()->drawText(text, byteLength, x, y, paint);
937 this->recordedDrawCommand();
940 void SkDeferredCanvas::drawPosText(const void* text, size_t byteLength,
941 const SkPoint pos[], const SkPaint& paint) {
942 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
943 this->drawingCanvas()->drawPosText(text, byteLength, pos, paint);
944 this->recordedDrawCommand();
947 void SkDeferredCanvas::drawPosTextH(const void* text, size_t byteLength,
948 const SkScalar xpos[], SkScalar constY,
949 const SkPaint& paint) {
950 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
951 this->drawingCanvas()->drawPosTextH(text, byteLength, xpos, constY, paint);
952 this->recordedDrawCommand();
955 void SkDeferredCanvas::drawTextOnPath(const void* text, size_t byteLength,
957 const SkMatrix* matrix,
958 const SkPaint& paint) {
959 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
960 this->drawingCanvas()->drawTextOnPath(text, byteLength, path, matrix, paint);
961 this->recordedDrawCommand();
964 void SkDeferredCanvas::drawPicture(SkPicture& picture) {
965 this->drawingCanvas()->drawPicture(picture);
966 this->recordedDrawCommand();
969 void SkDeferredCanvas::drawVertices(VertexMode vmode, int vertexCount,
970 const SkPoint vertices[],
971 const SkPoint texs[],
972 const SkColor colors[], SkXfermode* xmode,
973 const uint16_t indices[], int indexCount,
974 const SkPaint& paint) {
975 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
976 this->drawingCanvas()->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode,
977 indices, indexCount, paint);
978 this->recordedDrawCommand();
981 SkBounder* SkDeferredCanvas::setBounder(SkBounder* bounder) {
982 this->drawingCanvas()->setBounder(bounder);
983 this->INHERITED::setBounder(bounder);
984 this->recordedDrawCommand();
988 SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) {
989 this->drawingCanvas()->setDrawFilter(filter);
990 this->INHERITED::setDrawFilter(filter);
991 this->recordedDrawCommand();
995 SkCanvas* SkDeferredCanvas::canvasForDrawIter() {
996 return this->drawingCanvas();