blob: 19957fd81dea1bc0e9e5132dd3db7e203a8d7a2e [file] [log] [blame]
junov@google.com4370aed2012-01-18 16:21:08 +00001
2/*
junov@chromium.orgbaa02202013-01-24 14:38:23 +00003 * Copyright 2013 Google Inc.
junov@google.com4370aed2012-01-18 16:21:08 +00004 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "SkDeferredCanvas.h"
10
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000011#include "SkBitmapDevice.h"
junov@chromium.org88e29142012-08-07 16:48:22 +000012#include "SkChunkAlloc.h"
13#include "SkColorFilter.h"
junov@chromium.org88e29142012-08-07 16:48:22 +000014#include "SkDrawFilter.h"
15#include "SkGPipe.h"
junov@google.com4370aed2012-01-18 16:21:08 +000016#include "SkPaint.h"
junov@chromium.orgbaa02202013-01-24 14:38:23 +000017#include "SkPaintPriv.h"
reed@google.com4ed0fb72012-12-12 20:48:18 +000018#include "SkRRect.h"
junov@google.com4370aed2012-01-18 16:21:08 +000019#include "SkShader.h"
junov@chromium.org67d74222013-04-12 13:33:01 +000020#include "SkSurface.h"
junov@google.com4370aed2012-01-18 16:21:08 +000021
junov@chromium.orgbfeddae2012-07-23 13:35:14 +000022enum {
23 // Deferred canvas will auto-flush when recording reaches this limit
24 kDefaultMaxRecordingStorageBytes = 64*1024*1024,
reed@google.com140d7282013-01-07 20:25:04 +000025 kDeferredCanvasBitmapSizeThreshold = ~0U, // Disables this feature
junov@chromium.orgbfeddae2012-07-23 13:35:14 +000026};
27
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +000028enum PlaybackMode {
29 kNormal_PlaybackMode,
30 kSilent_PlaybackMode,
31};
32
reed52d9ac62014-06-30 09:05:34 -070033static bool should_draw_immediately(const SkBitmap* bitmap, const SkPaint* paint,
34 size_t bitmapSizeThreshold) {
sugoi@google.com7775fd52012-11-21 15:47:04 +000035 if (bitmap && ((bitmap->getTexture() && !bitmap->isImmutable()) ||
36 (bitmap->getSize() > bitmapSizeThreshold))) {
junov@chromium.org10f7f972012-07-31 21:01:51 +000037 return true;
38 }
39 if (paint) {
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)) {
47 SkBitmap bm;
rmistry@google.comd6176b02012-08-23 18:14:13 +000048 if (shader->asABitmap(&bm, NULL, NULL) &&
junov@chromium.org10f7f972012-07-31 21:01:51 +000049 NULL != bm.getTexture()) {
50 return true;
51 }
52 }
53 }
54 return false;
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +000055}
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +000056
junov@chromium.org88e29142012-08-07 16:48:22 +000057//-----------------------------------------------------------------------------
58// DeferredPipeController
59//-----------------------------------------------------------------------------
60
61class DeferredPipeController : public SkGPipeController {
62public:
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;
junov@chromium.orgfb103892012-09-20 19:35:43 +000068 void playback(bool silent);
junov@chromium.orga38dfb62012-09-20 22:10:33 +000069 bool hasPendingCommands() const { return fAllocator.blockCount() != 0; }
junov@chromium.org88e29142012-08-07 16:48:22 +000070 size_t storageAllocatedForRecording() const { return fAllocator.totalCapacity(); }
71private:
72 enum {
73 kMinBlockSize = 4096
74 };
75 struct PipeBlock {
76 PipeBlock(void* block, size_t size) { fBlock = block, fSize = size; }
77 void* fBlock;
78 size_t fSize;
79 };
80 void* fBlock;
81 size_t fBytesWritten;
82 SkChunkAlloc fAllocator;
83 SkTDArray<PipeBlock> fBlockList;
84 SkGPipeReader fReader;
85};
86
87DeferredPipeController::DeferredPipeController() :
88 fAllocator(kMinBlockSize) {
89 fBlock = NULL;
90 fBytesWritten = 0;
91}
92
93DeferredPipeController::~DeferredPipeController() {
94 fAllocator.reset();
95}
96
97void DeferredPipeController::setPlaybackCanvas(SkCanvas* canvas) {
98 fReader.setCanvas(canvas);
99}
100
101void* DeferredPipeController::requestBlock(size_t minRequest, size_t *actual) {
102 if (fBlock) {
103 // Save the previous block for later
104 PipeBlock previousBloc(fBlock, fBytesWritten);
105 fBlockList.push(previousBloc);
106 }
robertphillips@google.comadacc702013-10-14 21:53:24 +0000107 size_t blockSize = SkTMax<size_t>(minRequest, kMinBlockSize);
junov@chromium.org88e29142012-08-07 16:48:22 +0000108 fBlock = fAllocator.allocThrow(blockSize);
109 fBytesWritten = 0;
110 *actual = blockSize;
111 return fBlock;
112}
113
114void DeferredPipeController::notifyWritten(size_t bytes) {
115 fBytesWritten += bytes;
116}
117
junov@chromium.orgfb103892012-09-20 19:35:43 +0000118void DeferredPipeController::playback(bool silent) {
119 uint32_t flags = silent ? SkGPipeReader::kSilent_PlaybackFlag : 0;
junov@chromium.org88e29142012-08-07 16:48:22 +0000120 for (int currentBlock = 0; currentBlock < fBlockList.count(); currentBlock++ ) {
junov@chromium.orgfb103892012-09-20 19:35:43 +0000121 fReader.playback(fBlockList[currentBlock].fBlock, fBlockList[currentBlock].fSize,
122 flags);
junov@chromium.org88e29142012-08-07 16:48:22 +0000123 }
124 fBlockList.reset();
125
126 if (fBlock) {
junov@chromium.orgfb103892012-09-20 19:35:43 +0000127 fReader.playback(fBlock, fBytesWritten, flags);
junov@chromium.org88e29142012-08-07 16:48:22 +0000128 fBlock = NULL;
129 }
130
131 // Release all allocated blocks
132 fAllocator.reset();
133}
134
junov@chromium.org88e29142012-08-07 16:48:22 +0000135//-----------------------------------------------------------------------------
reed@google.com9c135db2014-03-12 18:28:35 +0000136// SkDeferredDevice
junov@chromium.org88e29142012-08-07 16:48:22 +0000137//-----------------------------------------------------------------------------
reed@google.com9c135db2014-03-12 18:28:35 +0000138class SkDeferredDevice : public SkBaseDevice {
junov@chromium.org88e29142012-08-07 16:48:22 +0000139public:
reed@google.com9c135db2014-03-12 18:28:35 +0000140 explicit SkDeferredDevice(SkSurface* surface);
141 ~SkDeferredDevice();
junov@chromium.org88e29142012-08-07 16:48:22 +0000142
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000143 void setNotificationClient(SkDeferredCanvas::NotificationClient* notificationClient);
junov@chromium.org88e29142012-08-07 16:48:22 +0000144 SkCanvas* recordingCanvas();
145 SkCanvas* immediateCanvas() const {return fImmediateCanvas;}
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000146 SkBaseDevice* immediateDevice() const {return fImmediateCanvas->getTopDevice();}
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000147 SkImage* newImageSnapshot();
junov@chromium.org7070f762013-05-24 17:13:00 +0000148 void setSurface(SkSurface* surface);
junov@chromium.org88e29142012-08-07 16:48:22 +0000149 bool isFreshFrame();
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000150 bool hasPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000151 size_t storageAllocatedForRecording() const;
152 size_t freeMemoryIfPossible(size_t bytesToFree);
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000153 void flushPendingCommands(PlaybackMode);
junov@chromium.org0a67f962012-09-19 22:48:34 +0000154 void skipPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000155 void setMaxRecordingStorage(size_t);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000156 void recordedDrawCommand();
junov@chromium.org88e29142012-08-07 16:48:22 +0000157
reed@google.com284a84d2014-02-14 15:23:15 +0000158 virtual SkImageInfo imageInfo() const SK_OVERRIDE;
159
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000160 virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE;
junov@chromium.org88e29142012-08-07 16:48:22 +0000161
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000162 virtual SkBaseDevice* onCreateDevice(const SkImageInfo&, Usage) SK_OVERRIDE;
junov@chromium.org88e29142012-08-07 16:48:22 +0000163
reed@google.com76f10a32014-02-05 15:32:21 +0000164 virtual SkSurface* newSurface(const SkImageInfo&) SK_OVERRIDE;
165
junov@chromium.org88e29142012-08-07 16:48:22 +0000166protected:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000167 virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE;
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000168 virtual bool onReadPixels(const SkImageInfo&, void*, size_t, int x, int y) SK_OVERRIDE;
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000169 virtual bool onWritePixels(const SkImageInfo&, const void*, size_t, int x, int y) SK_OVERRIDE;
junov@chromium.org88e29142012-08-07 16:48:22 +0000170
171 // The following methods are no-ops on a deferred device
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000172 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE {
173 return false;
174 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000175
176 // None of the following drawing methods should ever get called on the
177 // deferred device
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000178 virtual void clear(SkColor color) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000179 {SkASSERT(0);}
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000180 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000181 {SkASSERT(0);}
182 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
183 size_t count, const SkPoint[],
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000184 const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000185 {SkASSERT(0);}
186 virtual void drawRect(const SkDraw&, const SkRect& r,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000187 const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000188 {SkASSERT(0);}
reed@google.com284a84d2014-02-14 15:23:15 +0000189 virtual void drawOval(const SkDraw&, const SkRect&, const SkPaint&) SK_OVERRIDE
190 {SkASSERT(0);}
scroggo@google.comcac8d012013-11-12 17:10:02 +0000191 virtual void drawRRect(const SkDraw&, const SkRRect& rr,
192 const SkPaint& paint) SK_OVERRIDE
reed@google.com284a84d2014-02-14 15:23:15 +0000193 {SkASSERT(0);}
junov@chromium.org88e29142012-08-07 16:48:22 +0000194 virtual void drawPath(const SkDraw&, const SkPath& path,
195 const SkPaint& paint,
196 const SkMatrix* prePathMatrix = NULL,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000197 bool pathIsMutable = false) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000198 {SkASSERT(0);}
199 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000200 const SkMatrix& matrix, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000201 {SkASSERT(0);}
reed@google.com284a84d2014-02-14 15:23:15 +0000202 virtual void drawBitmapRect(const SkDraw&, const SkBitmap&, const SkRect*,
203 const SkRect&, const SkPaint&,
204 SkCanvas::DrawBitmapRectFlags) SK_OVERRIDE
205 {SkASSERT(0);}
junov@chromium.org88e29142012-08-07 16:48:22 +0000206 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000207 int x, int y, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000208 {SkASSERT(0);}
209 virtual void drawText(const SkDraw&, const void* text, size_t len,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000210 SkScalar x, SkScalar y, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000211 {SkASSERT(0);}
212 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
213 const SkScalar pos[], SkScalar constY,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000214 int scalarsPerPos, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000215 {SkASSERT(0);}
216 virtual void drawTextOnPath(const SkDraw&, const void* text,
217 size_t len, const SkPath& path,
218 const SkMatrix* matrix,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000219 const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000220 {SkASSERT(0);}
junov@chromium.org88e29142012-08-07 16:48:22 +0000221 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[],
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000225 int indexCount, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000226 {SkASSERT(0);}
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000227 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000228 const SkPaint&) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000229 {SkASSERT(0);}
reed@google.com284a84d2014-02-14 15:23:15 +0000230
231 virtual void lockPixels() SK_OVERRIDE {}
232 virtual void unlockPixels() SK_OVERRIDE {}
233
234 virtual bool allowImageFilter(const SkImageFilter*) SK_OVERRIDE {
235 return false;
236 }
237 virtual bool canHandleImageFilter(const SkImageFilter*) SK_OVERRIDE {
238 return false;
239 }
240 virtual bool filterImage(const SkImageFilter*, const SkBitmap&,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000241 const SkImageFilter::Context&, SkBitmap*, SkIPoint*) SK_OVERRIDE {
reed@google.com284a84d2014-02-14 15:23:15 +0000242 return false;
243 }
244
junov@chromium.org88e29142012-08-07 16:48:22 +0000245private:
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000246 virtual void flush() SK_OVERRIDE;
reed@google.com284a84d2014-02-14 15:23:15 +0000247 virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE {}
junov@chromium.org88e29142012-08-07 16:48:22 +0000248
junov@chromium.org88e29142012-08-07 16:48:22 +0000249 void beginRecording();
junov@chromium.org67d74222013-04-12 13:33:01 +0000250 void init();
junov@chromium.org44324fa2013-08-02 15:36:02 +0000251 void aboutToDraw();
252 void prepareForImmediatePixelWrite();
junov@chromium.org88e29142012-08-07 16:48:22 +0000253
254 DeferredPipeController fPipeController;
255 SkGPipeWriter fPipeWriter;
junov@chromium.org88e29142012-08-07 16:48:22 +0000256 SkCanvas* fImmediateCanvas;
257 SkCanvas* fRecordingCanvas;
junov@chromium.org67d74222013-04-12 13:33:01 +0000258 SkSurface* fSurface;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000259 SkDeferredCanvas::NotificationClient* fNotificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000260 bool fFreshFrame;
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000261 bool fCanDiscardCanvasContents;
junov@chromium.org88e29142012-08-07 16:48:22 +0000262 size_t fMaxRecordingStorageBytes;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000263 size_t fPreviousStorageAllocated;
junov@chromium.org88e29142012-08-07 16:48:22 +0000264};
265
reed@google.com9c135db2014-03-12 18:28:35 +0000266SkDeferredDevice::SkDeferredDevice(SkSurface* surface) {
junov@chromium.org67d74222013-04-12 13:33:01 +0000267 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
268 fNotificationClient = NULL;
junov@chromium.org7070f762013-05-24 17:13:00 +0000269 fImmediateCanvas = NULL;
270 fSurface = NULL;
271 this->setSurface(surface);
junov@chromium.org67d74222013-04-12 13:33:01 +0000272 this->init();
273}
274
reed@google.com9c135db2014-03-12 18:28:35 +0000275void SkDeferredDevice::setSurface(SkSurface* surface) {
junov@chromium.org7070f762013-05-24 17:13:00 +0000276 SkRefCnt_SafeAssign(fImmediateCanvas, surface->getCanvas());
277 SkRefCnt_SafeAssign(fSurface, surface);
278 fPipeController.setPlaybackCanvas(fImmediateCanvas);
279}
280
reed@google.com9c135db2014-03-12 18:28:35 +0000281void SkDeferredDevice::init() {
junov@chromium.org67d74222013-04-12 13:33:01 +0000282 fRecordingCanvas = NULL;
283 fFreshFrame = true;
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000284 fCanDiscardCanvasContents = false;
junov@chromium.org67d74222013-04-12 13:33:01 +0000285 fPreviousStorageAllocated = 0;
junov@chromium.org67d74222013-04-12 13:33:01 +0000286 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
287 fNotificationClient = NULL;
junov@chromium.org88e29142012-08-07 16:48:22 +0000288 this->beginRecording();
289}
290
reed@google.com9c135db2014-03-12 18:28:35 +0000291SkDeferredDevice::~SkDeferredDevice() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000292 this->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000293 SkSafeUnref(fImmediateCanvas);
junov@chromium.org67d74222013-04-12 13:33:01 +0000294 SkSafeUnref(fSurface);
junov@chromium.org88e29142012-08-07 16:48:22 +0000295}
296
reed@google.com9c135db2014-03-12 18:28:35 +0000297void SkDeferredDevice::setMaxRecordingStorage(size_t maxStorage) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000298 fMaxRecordingStorageBytes = maxStorage;
299 this->recordingCanvas(); // Accessing the recording canvas applies the new limit.
300}
301
reed@google.com9c135db2014-03-12 18:28:35 +0000302void SkDeferredDevice::beginRecording() {
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000303 SkASSERT(NULL == fRecordingCanvas);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000304 fRecordingCanvas = fPipeWriter.startRecording(&fPipeController, 0,
junov@chromium.org9becf002013-04-15 18:15:23 +0000305 immediateDevice()->width(), immediateDevice()->height());
junov@chromium.org88e29142012-08-07 16:48:22 +0000306}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000307
reed@google.com9c135db2014-03-12 18:28:35 +0000308void SkDeferredDevice::setNotificationClient(
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000309 SkDeferredCanvas::NotificationClient* notificationClient) {
junov@chromium.org52805482012-08-20 14:25:04 +0000310 fNotificationClient = notificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000311}
312
reed@google.com9c135db2014-03-12 18:28:35 +0000313void SkDeferredDevice::skipPendingCommands() {
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000314 if (!fRecordingCanvas->isDrawingToLayer()) {
315 fCanDiscardCanvasContents = true;
316 if (fPipeController.hasPendingCommands()) {
317 fFreshFrame = true;
318 flushPendingCommands(kSilent_PlaybackMode);
junov@google.com52a00ca2012-10-01 15:27:14 +0000319 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000320 }
321}
322
reed@google.com9c135db2014-03-12 18:28:35 +0000323bool SkDeferredDevice::isFreshFrame() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000324 bool ret = fFreshFrame;
325 fFreshFrame = false;
326 return ret;
327}
328
reed@google.com9c135db2014-03-12 18:28:35 +0000329bool SkDeferredDevice::hasPendingCommands() {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000330 return fPipeController.hasPendingCommands();
331}
332
reed@google.com9c135db2014-03-12 18:28:35 +0000333void SkDeferredDevice::aboutToDraw()
junov@chromium.org44324fa2013-08-02 15:36:02 +0000334{
335 if (NULL != fNotificationClient) {
336 fNotificationClient->prepareForDraw();
337 }
338 if (fCanDiscardCanvasContents) {
339 if (NULL != fSurface) {
340 fSurface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
341 }
skia.committer@gmail.comea4b7972013-08-06 07:01:27 +0000342 fCanDiscardCanvasContents = false;
junov@chromium.org44324fa2013-08-02 15:36:02 +0000343 }
344}
345
reed@google.com9c135db2014-03-12 18:28:35 +0000346void SkDeferredDevice::flushPendingCommands(PlaybackMode playbackMode) {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000347 if (!fPipeController.hasPendingCommands()) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000348 return;
349 }
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000350 if (playbackMode == kNormal_PlaybackMode) {
junov@chromium.org44324fa2013-08-02 15:36:02 +0000351 aboutToDraw();
junov@chromium.org88e29142012-08-07 16:48:22 +0000352 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000353 fPipeWriter.flushRecording(true);
junov@chromium.orgd4501a02012-10-30 19:05:17 +0000354 fPipeController.playback(kSilent_PlaybackMode == playbackMode);
commit-bot@chromium.orgdad009b2014-03-27 15:48:52 +0000355 if (fNotificationClient) {
356 if (playbackMode == kSilent_PlaybackMode) {
357 fNotificationClient->skippedPendingDrawCommands();
358 } else {
359 fNotificationClient->flushedDrawCommands();
360 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000361 }
commit-bot@chromium.orgdad009b2014-03-27 15:48:52 +0000362
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000363 fPreviousStorageAllocated = storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000364}
365
reed@google.com9c135db2014-03-12 18:28:35 +0000366void SkDeferredDevice::flush() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000367 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000368 fImmediateCanvas->flush();
369}
370
reed@google.com9c135db2014-03-12 18:28:35 +0000371size_t SkDeferredDevice::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000372 size_t val = fPipeWriter.freeMemoryIfPossible(bytesToFree);
373 fPreviousStorageAllocated = storageAllocatedForRecording();
374 return val;
junov@chromium.org88e29142012-08-07 16:48:22 +0000375}
376
reed@google.com9c135db2014-03-12 18:28:35 +0000377size_t SkDeferredDevice::storageAllocatedForRecording() const {
junov@chromium.org88e29142012-08-07 16:48:22 +0000378 return (fPipeController.storageAllocatedForRecording()
379 + fPipeWriter.storageAllocatedForRecording());
380}
381
reed@google.com9c135db2014-03-12 18:28:35 +0000382void SkDeferredDevice::recordedDrawCommand() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000383 size_t storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000384
junov@chromium.org88e29142012-08-07 16:48:22 +0000385 if (storageAllocated > fMaxRecordingStorageBytes) {
386 // First, attempt to reduce cache without flushing
387 size_t tryFree = storageAllocated - fMaxRecordingStorageBytes;
388 if (this->freeMemoryIfPossible(tryFree) < tryFree) {
389 // Flush is necessary to free more space.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000390 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000391 // Free as much as possible to avoid oscillating around fMaxRecordingStorageBytes
392 // which could cause a high flushing frequency.
bsalomon@google.com100abf42012-09-05 17:40:04 +0000393 this->freeMemoryIfPossible(~0U);
junov@chromium.org88e29142012-08-07 16:48:22 +0000394 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000395 storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000396 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000397
rmistry@google.comd6176b02012-08-23 18:14:13 +0000398 if (fNotificationClient &&
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000399 storageAllocated != fPreviousStorageAllocated) {
400 fPreviousStorageAllocated = storageAllocated;
401 fNotificationClient->storageAllocatedForRecordingChanged(storageAllocated);
402 }
403}
404
reed@google.com9c135db2014-03-12 18:28:35 +0000405SkCanvas* SkDeferredDevice::recordingCanvas() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000406 return fRecordingCanvas;
407}
408
reed@google.com9c135db2014-03-12 18:28:35 +0000409SkImage* SkDeferredDevice::newImageSnapshot() {
junov@chromium.org67d74222013-04-12 13:33:01 +0000410 this->flush();
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000411 return fSurface ? fSurface->newImageSnapshot() : NULL;
junov@chromium.org67d74222013-04-12 13:33:01 +0000412}
413
reed@google.com9c135db2014-03-12 18:28:35 +0000414SkImageInfo SkDeferredDevice::imageInfo() const {
reed@google.com284a84d2014-02-14 15:23:15 +0000415 return immediateDevice()->imageInfo();
416}
417
reed@google.com9c135db2014-03-12 18:28:35 +0000418GrRenderTarget* SkDeferredDevice::accessRenderTarget() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000419 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000420 return immediateDevice()->accessRenderTarget();
junov@chromium.org88e29142012-08-07 16:48:22 +0000421}
422
reed@google.com9c135db2014-03-12 18:28:35 +0000423void SkDeferredDevice::prepareForImmediatePixelWrite() {
junov@chromium.org44324fa2013-08-02 15:36:02 +0000424 // The purpose of the following code is to make sure commands are flushed, that
425 // aboutToDraw() is called and that notifyContentWillChange is called, without
426 // calling anything redundantly.
427 if (fPipeController.hasPendingCommands()) {
428 this->flushPendingCommands(kNormal_PlaybackMode);
429 } else {
430 bool mustNotifyDirectly = !fCanDiscardCanvasContents;
431 this->aboutToDraw();
432 if (mustNotifyDirectly) {
433 fSurface->notifyContentWillChange(SkSurface::kRetain_ContentChangeMode);
434 }
435 }
436
437 fImmediateCanvas->flush();
438}
439
reed@google.com9c135db2014-03-12 18:28:35 +0000440bool SkDeferredDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000441 int x, int y) {
442 SkASSERT(x >= 0 && y >= 0);
443 SkASSERT(x + info.width() <= width());
444 SkASSERT(y + info.height() <= height());
445
446 this->flushPendingCommands(kNormal_PlaybackMode);
447
448 const SkImageInfo deviceInfo = this->imageInfo();
449 if (info.width() == deviceInfo.width() && info.height() == deviceInfo.height()) {
450 this->skipPendingCommands();
451 }
skia.committer@gmail.come62513f2014-03-08 03:02:06 +0000452
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000453 this->prepareForImmediatePixelWrite();
454 return immediateDevice()->onWritePixels(info, pixels, rowBytes, x, y);
455}
junov@chromium.org88e29142012-08-07 16:48:22 +0000456
reed@google.com9c135db2014-03-12 18:28:35 +0000457const SkBitmap& SkDeferredDevice::onAccessBitmap() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000458 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000459 return immediateDevice()->accessBitmap(false);
junov@chromium.org88e29142012-08-07 16:48:22 +0000460}
461
reed@google.com9c135db2014-03-12 18:28:35 +0000462SkBaseDevice* SkDeferredDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000463 // Save layer usage not supported, and not required by SkDeferredCanvas.
464 SkASSERT(usage != kSaveLayer_Usage);
465 // Create a compatible non-deferred device.
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000466 // We do not create a deferred device because we know the new device
467 // will not be used with a deferred canvas (there is no API for that).
reed@google.com9c135db2014-03-12 18:28:35 +0000468 // And connecting a SkDeferredDevice to non-deferred canvas can result
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000469 // in unpredictable behavior.
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000470 return immediateDevice()->createCompatibleDevice(info);
junov@chromium.org88e29142012-08-07 16:48:22 +0000471}
472
reed@google.com9c135db2014-03-12 18:28:35 +0000473SkSurface* SkDeferredDevice::newSurface(const SkImageInfo& info) {
reed@google.com76f10a32014-02-05 15:32:21 +0000474 return this->immediateDevice()->newSurface(info);
475}
476
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000477bool SkDeferredDevice::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
478 int x, int y) {
479 this->flushPendingCommands(kNormal_PlaybackMode);
480 return fImmediateCanvas->readPixels(info, pixels, rowBytes, x, y);
481}
junov@chromium.org88e29142012-08-07 16:48:22 +0000482
sugoi@google.com7775fd52012-11-21 15:47:04 +0000483class AutoImmediateDrawIfNeeded {
484public:
485 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkBitmap* bitmap,
486 const SkPaint* paint) {
487 this->init(canvas, bitmap, paint);
488 }
489
490 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkPaint* paint) {
491 this->init(canvas, NULL, paint);
492 }
493
494 ~AutoImmediateDrawIfNeeded() {
495 if (fCanvas) {
496 fCanvas->setDeferredDrawing(true);
497 }
498 }
499private:
reed52d9ac62014-06-30 09:05:34 -0700500 void init(SkDeferredCanvas& canvas, const SkBitmap* bitmap, const SkPaint* paint) {
501 if (canvas.isDeferredDrawing() &&
502 should_draw_immediately(bitmap, paint, canvas.getBitmapSizeThreshold())) {
sugoi@google.com7775fd52012-11-21 15:47:04 +0000503 canvas.setDeferredDrawing(false);
504 fCanvas = &canvas;
505 } else {
506 fCanvas = NULL;
507 }
508 }
509
510 SkDeferredCanvas* fCanvas;
511};
junov@chromium.org88e29142012-08-07 16:48:22 +0000512
junov@chromium.org66070a52013-05-28 17:39:08 +0000513SkDeferredCanvas* SkDeferredCanvas::Create(SkSurface* surface) {
reed@google.com9c135db2014-03-12 18:28:35 +0000514 SkAutoTUnref<SkDeferredDevice> deferredDevice(SkNEW_ARGS(SkDeferredDevice, (surface)));
junov@chromium.org66070a52013-05-28 17:39:08 +0000515 return SkNEW_ARGS(SkDeferredCanvas, (deferredDevice));
516}
517
reed@google.com9c135db2014-03-12 18:28:35 +0000518SkDeferredCanvas::SkDeferredCanvas(SkDeferredDevice* device) : SkCanvas (device) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000519 this->init();
520}
junov@chromium.org67d74222013-04-12 13:33:01 +0000521
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000522void SkDeferredCanvas::init() {
reed52d9ac62014-06-30 09:05:34 -0700523 fBitmapSizeThreshold = kDeferredCanvasBitmapSizeThreshold;
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000524 fDeferredDrawing = true; // On by default
junov@google.com4370aed2012-01-18 16:21:08 +0000525}
526
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000527void SkDeferredCanvas::setMaxRecordingStorage(size_t maxStorage) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000528 this->validate();
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000529 this->getDeferredDevice()->setMaxRecordingStorage(maxStorage);
530}
531
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000532size_t SkDeferredCanvas::storageAllocatedForRecording() const {
533 return this->getDeferredDevice()->storageAllocatedForRecording();
534}
535
536size_t SkDeferredCanvas::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000537 return this->getDeferredDevice()->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000538}
539
sugoi@google.com7775fd52012-11-21 15:47:04 +0000540void SkDeferredCanvas::setBitmapSizeThreshold(size_t sizeThreshold) {
reed52d9ac62014-06-30 09:05:34 -0700541 fBitmapSizeThreshold = sizeThreshold;
sugoi@google.com7775fd52012-11-21 15:47:04 +0000542}
543
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000544void SkDeferredCanvas::recordedDrawCommand() {
545 if (fDeferredDrawing) {
546 this->getDeferredDevice()->recordedDrawCommand();
547 }
548}
549
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000550void SkDeferredCanvas::validate() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000551 SkASSERT(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000552}
553
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000554SkCanvas* SkDeferredCanvas::drawingCanvas() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000555 this->validate();
556 return fDeferredDrawing ? this->getDeferredDevice()->recordingCanvas() :
557 this->getDeferredDevice()->immediateCanvas();
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000558}
559
junov@chromium.org88e29142012-08-07 16:48:22 +0000560SkCanvas* SkDeferredCanvas::immediateCanvas() const {
561 this->validate();
562 return this->getDeferredDevice()->immediateCanvas();
563}
564
reed@google.com9c135db2014-03-12 18:28:35 +0000565SkDeferredDevice* SkDeferredCanvas::getDeferredDevice() const {
566 return static_cast<SkDeferredDevice*>(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000567}
568
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000569void SkDeferredCanvas::setDeferredDrawing(bool val) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000570 this->validate(); // Must set device before calling this method
junov@google.com4370aed2012-01-18 16:21:08 +0000571 if (val != fDeferredDrawing) {
572 if (fDeferredDrawing) {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000573 // Going live.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000574 this->getDeferredDevice()->flushPendingCommands(kNormal_PlaybackMode);
junov@google.com4370aed2012-01-18 16:21:08 +0000575 }
576 fDeferredDrawing = val;
577 }
578}
579
junov@chromium.org88e29142012-08-07 16:48:22 +0000580bool SkDeferredCanvas::isDeferredDrawing() const {
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +0000581 return fDeferredDrawing;
582}
583
junov@chromium.org88e29142012-08-07 16:48:22 +0000584bool SkDeferredCanvas::isFreshFrame() const {
585 return this->getDeferredDevice()->isFreshFrame();
586}
587
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000588bool SkDeferredCanvas::hasPendingCommands() const {
589 return this->getDeferredDevice()->hasPendingCommands();
590}
591
junov@chromium.orgfb103892012-09-20 19:35:43 +0000592void SkDeferredCanvas::silentFlush() {
593 if (fDeferredDrawing) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000594 this->getDeferredDevice()->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000595 }
596}
597
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000598SkDeferredCanvas::~SkDeferredCanvas() {
junov@google.com4370aed2012-01-18 16:21:08 +0000599}
600
junov@chromium.org7070f762013-05-24 17:13:00 +0000601SkSurface* SkDeferredCanvas::setSurface(SkSurface* surface) {
reed@google.com9c135db2014-03-12 18:28:35 +0000602 SkDeferredDevice* deferredDevice = this->getDeferredDevice();
junov@chromium.org66070a52013-05-28 17:39:08 +0000603 SkASSERT(NULL != deferredDevice);
604 // By swapping the surface into the existing device, we preserve
605 // all pending commands, which can help to seamlessly recover from
606 // a lost accelerated graphics context.
607 deferredDevice->setSurface(surface);
junov@chromium.org7070f762013-05-24 17:13:00 +0000608 return surface;
609}
610
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000611SkDeferredCanvas::NotificationClient* SkDeferredCanvas::setNotificationClient(
612 NotificationClient* notificationClient) {
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000613
reed@google.com9c135db2014-03-12 18:28:35 +0000614 SkDeferredDevice* deferredDevice = this->getDeferredDevice();
junov@google.com4370aed2012-01-18 16:21:08 +0000615 SkASSERT(deferredDevice);
616 if (deferredDevice) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000617 deferredDevice->setNotificationClient(notificationClient);
junov@google.com4370aed2012-01-18 16:21:08 +0000618 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000619 return notificationClient;
junov@google.com4370aed2012-01-18 16:21:08 +0000620}
621
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000622SkImage* SkDeferredCanvas::newImageSnapshot() {
reed@google.com9c135db2014-03-12 18:28:35 +0000623 SkDeferredDevice* deferredDevice = this->getDeferredDevice();
junov@chromium.org67d74222013-04-12 13:33:01 +0000624 SkASSERT(deferredDevice);
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000625 return deferredDevice ? deferredDevice->newImageSnapshot() : NULL;
junov@chromium.org67d74222013-04-12 13:33:01 +0000626}
627
junov@google.com4370aed2012-01-18 16:21:08 +0000628bool SkDeferredCanvas::isFullFrame(const SkRect* rect,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000629 const SkPaint* paint) const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000630 SkCanvas* canvas = this->drawingCanvas();
631 SkISize canvasSize = this->getDeviceSize();
junov@google.com4370aed2012-01-18 16:21:08 +0000632 if (rect) {
633 if (!canvas->getTotalMatrix().rectStaysRect()) {
634 return false; // conservative
635 }
636
637 SkRect transformedRect;
638 canvas->getTotalMatrix().mapRect(&transformedRect, *rect);
639
640 if (paint) {
641 SkPaint::Style paintStyle = paint->getStyle();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000642 if (!(paintStyle == SkPaint::kFill_Style ||
junov@google.com4370aed2012-01-18 16:21:08 +0000643 paintStyle == SkPaint::kStrokeAndFill_Style)) {
644 return false;
645 }
646 if (paint->getMaskFilter() || paint->getLooper()
647 || paint->getPathEffect() || paint->getImageFilter()) {
648 return false; // conservative
649 }
650 }
651
652 // The following test holds with AA enabled, and is conservative
653 // by a 0.5 pixel margin with AA disabled
rmistry@google.comd6176b02012-08-23 18:14:13 +0000654 if (transformedRect.fLeft > SkIntToScalar(0) ||
655 transformedRect.fTop > SkIntToScalar(0) ||
junov@chromium.orgb1e218e2012-02-13 22:27:58 +0000656 transformedRect.fRight < SkIntToScalar(canvasSize.fWidth) ||
657 transformedRect.fBottom < SkIntToScalar(canvasSize.fHeight)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000658 return false;
659 }
660 }
661
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000662 return this->getClipStack()->quickContains(SkRect::MakeXYWH(0, 0,
663 SkIntToScalar(canvasSize.fWidth), SkIntToScalar(canvasSize.fHeight)));
junov@google.com4370aed2012-01-18 16:21:08 +0000664}
665
Florin Malita5f6102d2014-06-30 10:13:28 -0400666void SkDeferredCanvas::willSave() {
667 this->drawingCanvas()->save();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000668 this->recordedDrawCommand();
Florin Malita5f6102d2014-06-30 10:13:28 -0400669 this->INHERITED::willSave();
junov@google.com4370aed2012-01-18 16:21:08 +0000670}
671
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000672SkCanvas::SaveLayerStrategy SkDeferredCanvas::willSaveLayer(const SkRect* bounds,
673 const SkPaint* paint, SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000674 this->drawingCanvas()->saveLayer(bounds, paint, flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000675 this->recordedDrawCommand();
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000676 this->INHERITED::willSaveLayer(bounds, paint, flags);
677 // No need for a full layer.
678 return kNoLayer_SaveLayerStrategy;
junov@google.com4370aed2012-01-18 16:21:08 +0000679}
680
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000681void SkDeferredCanvas::willRestore() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000682 this->drawingCanvas()->restore();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000683 this->recordedDrawCommand();
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000684 this->INHERITED::willRestore();
junov@google.com4370aed2012-01-18 16:21:08 +0000685}
686
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000687bool SkDeferredCanvas::isDrawingToLayer() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000688 return this->drawingCanvas()->isDrawingToLayer();
junov@google.com4370aed2012-01-18 16:21:08 +0000689}
690
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000691void SkDeferredCanvas::didConcat(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000692 this->drawingCanvas()->concat(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000693 this->recordedDrawCommand();
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000694 this->INHERITED::didConcat(matrix);
junov@google.com4370aed2012-01-18 16:21:08 +0000695}
696
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000697void SkDeferredCanvas::didSetMatrix(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000698 this->drawingCanvas()->setMatrix(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000699 this->recordedDrawCommand();
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000700 this->INHERITED::didSetMatrix(matrix);
junov@google.com4370aed2012-01-18 16:21:08 +0000701}
702
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000703void SkDeferredCanvas::onClipRect(const SkRect& rect,
704 SkRegion::Op op,
705 ClipEdgeStyle edgeStyle) {
706 this->drawingCanvas()->clipRect(rect, op, kSoft_ClipEdgeStyle == edgeStyle);
707 this->INHERITED::onClipRect(rect, op, edgeStyle);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000708 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000709}
710
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000711void SkDeferredCanvas::onClipRRect(const SkRRect& rrect,
712 SkRegion::Op op,
713 ClipEdgeStyle edgeStyle) {
714 this->drawingCanvas()->clipRRect(rrect, op, kSoft_ClipEdgeStyle == edgeStyle);
715 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000716 this->recordedDrawCommand();
reed@google.com4ed0fb72012-12-12 20:48:18 +0000717}
718
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000719void SkDeferredCanvas::onClipPath(const SkPath& path,
720 SkRegion::Op op,
721 ClipEdgeStyle edgeStyle) {
722 this->drawingCanvas()->clipPath(path, op, kSoft_ClipEdgeStyle == edgeStyle);
723 this->INHERITED::onClipPath(path, op, edgeStyle);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000724 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000725}
726
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000727void SkDeferredCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000728 this->drawingCanvas()->clipRegion(deviceRgn, op);
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000729 this->INHERITED::onClipRegion(deviceRgn, op);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000730 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000731}
732
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000733void SkDeferredCanvas::clear(SkColor color) {
junov@google.com4370aed2012-01-18 16:21:08 +0000734 // purge pending commands
735 if (fDeferredDrawing) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000736 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000737 }
738
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000739 this->drawingCanvas()->clear(color);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000740 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000741}
742
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000743void SkDeferredCanvas::drawPaint(const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000744 if (fDeferredDrawing && this->isFullFrame(NULL, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000745 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000746 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000747 }
junov@chromium.org10f7f972012-07-31 21:01:51 +0000748 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000749 this->drawingCanvas()->drawPaint(paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000750 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000751}
752
753void SkDeferredCanvas::drawPoints(PointMode mode, size_t count,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000754 const SkPoint pts[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000755 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000756 this->drawingCanvas()->drawPoints(mode, count, pts, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000757 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000758}
759
reed@google.com4ed0fb72012-12-12 20:48:18 +0000760void SkDeferredCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
761 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
762 this->drawingCanvas()->drawOval(rect, paint);
763 this->recordedDrawCommand();
764}
765
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000766void SkDeferredCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000767 if (fDeferredDrawing && this->isFullFrame(&rect, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000768 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000769 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000770 }
skia.committer@gmail.com306ab9d2012-12-13 02:01:33 +0000771
junov@chromium.org10f7f972012-07-31 21:01:51 +0000772 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000773 this->drawingCanvas()->drawRect(rect, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000774 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000775}
776
reed@google.com4ed0fb72012-12-12 20:48:18 +0000777void SkDeferredCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
778 if (rrect.isRect()) {
779 this->SkDeferredCanvas::drawRect(rrect.getBounds(), paint);
780 } else if (rrect.isOval()) {
781 this->SkDeferredCanvas::drawOval(rrect.getBounds(), paint);
782 } else {
783 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
784 this->drawingCanvas()->drawRRect(rrect, paint);
785 this->recordedDrawCommand();
786 }
787}
788
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000789void SkDeferredCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
790 const SkPaint& paint) {
791 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
792 this->drawingCanvas()->drawDRRect(outer, inner, paint);
793 this->recordedDrawCommand();
794}
795
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000796void SkDeferredCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000797 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000798 this->drawingCanvas()->drawPath(path, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000799 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000800}
801
802void SkDeferredCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000803 SkScalar top, const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000804 SkRect bitmapRect = SkRect::MakeXYWH(left, top,
805 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000806 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000807 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000808 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000809 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000810 }
811
junov@chromium.org10f7f972012-07-31 21:01:51 +0000812 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000813 this->drawingCanvas()->drawBitmap(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000814 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000815}
816
reed@google.com71121732012-09-18 15:14:33 +0000817void SkDeferredCanvas::drawBitmapRectToRect(const SkBitmap& bitmap,
818 const SkRect* src,
819 const SkRect& dst,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000820 const SkPaint* paint,
821 DrawBitmapRectFlags flags) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000822 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000823 this->isFullFrame(&dst, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000824 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000825 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000826 }
827
junov@chromium.org10f7f972012-07-31 21:01:51 +0000828 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000829 this->drawingCanvas()->drawBitmapRectToRect(bitmap, src, dst, paint, flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000830 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000831}
832
833
834void SkDeferredCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
835 const SkMatrix& m,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000836 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000837 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
838 // covers canvas entirely and transformed bitmap covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000839 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000840 this->drawingCanvas()->drawBitmapMatrix(bitmap, m, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000841 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000842}
843
844void SkDeferredCanvas::drawBitmapNine(const SkBitmap& bitmap,
845 const SkIRect& center, const SkRect& dst,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000846 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000847 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
848 // covers canvas entirely and dst covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000849 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000850 this->drawingCanvas()->drawBitmapNine(bitmap, center, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000851 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000852}
853
854void SkDeferredCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000855 const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000856 SkRect bitmapRect = SkRect::MakeXYWH(
857 SkIntToScalar(left),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000858 SkIntToScalar(top),
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000859 SkIntToScalar(bitmap.width()),
860 SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000861 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000862 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000863 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000864 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000865 }
866
junov@chromium.org10f7f972012-07-31 21:01:51 +0000867 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000868 this->drawingCanvas()->drawSprite(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000869 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000870}
871
reed@google.come0d9ce82014-04-23 04:00:17 +0000872void SkDeferredCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
873 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000874 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000875 this->drawingCanvas()->drawText(text, byteLength, x, y, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000876 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000877}
878
reed@google.come0d9ce82014-04-23 04:00:17 +0000879void SkDeferredCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
880 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000881 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000882 this->drawingCanvas()->drawPosText(text, byteLength, pos, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000883 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000884}
885
reed@google.come0d9ce82014-04-23 04:00:17 +0000886void SkDeferredCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
887 SkScalar constY, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000888 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000889 this->drawingCanvas()->drawPosTextH(text, byteLength, xpos, constY, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000890 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000891}
892
reed@google.come0d9ce82014-04-23 04:00:17 +0000893void SkDeferredCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
894 const SkMatrix* matrix, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000895 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000896 this->drawingCanvas()->drawTextOnPath(text, byteLength, path, matrix, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000897 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000898}
899
robertphillips9b14f262014-06-04 05:40:44 -0700900void SkDeferredCanvas::onDrawPicture(const SkPicture* picture) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000901 this->drawingCanvas()->drawPicture(picture);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000902 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000903}
904
905void SkDeferredCanvas::drawVertices(VertexMode vmode, int vertexCount,
906 const SkPoint vertices[],
907 const SkPoint texs[],
908 const SkColor colors[], SkXfermode* xmode,
909 const uint16_t indices[], int indexCount,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000910 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000911 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000912 this->drawingCanvas()->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode,
913 indices, indexCount, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000914 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000915}
916
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000917SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000918 this->drawingCanvas()->setDrawFilter(filter);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000919 this->INHERITED::setDrawFilter(filter);
920 this->recordedDrawCommand();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000921 return filter;
junov@google.com4370aed2012-01-18 16:21:08 +0000922}
923
924SkCanvas* SkDeferredCanvas::canvasForDrawIter() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000925 return this->drawingCanvas();
junov@google.com4370aed2012-01-18 16:21:08 +0000926}