blob: 5119729447bb75e4b3e3ea1a54cba838ddd0c993 [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
junov@chromium.org88e29142012-08-07 16:48:22 +000011#include "SkChunkAlloc.h"
12#include "SkColorFilter.h"
13#include "SkDevice.h"
14#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@google.com4370aed2012-01-18 16:21:08 +000020
junov@chromium.orgbfeddae2012-07-23 13:35:14 +000021enum {
22 // Deferred canvas will auto-flush when recording reaches this limit
23 kDefaultMaxRecordingStorageBytes = 64*1024*1024,
reed@google.com140d7282013-01-07 20:25:04 +000024 kDeferredCanvasBitmapSizeThreshold = ~0U, // Disables this feature
junov@chromium.orgbfeddae2012-07-23 13:35:14 +000025};
26
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +000027enum PlaybackMode {
28 kNormal_PlaybackMode,
29 kSilent_PlaybackMode,
30};
31
junov@google.com4370aed2012-01-18 16:21:08 +000032namespace {
sugoi@google.com7775fd52012-11-21 15:47:04 +000033bool shouldDrawImmediately(const SkBitmap* bitmap, const SkPaint* paint,
34 size_t bitmapSizeThreshold) {
35 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}
56}
57
junov@chromium.org88e29142012-08-07 16:48:22 +000058//-----------------------------------------------------------------------------
59// DeferredPipeController
60//-----------------------------------------------------------------------------
61
62class DeferredPipeController : public SkGPipeController {
63public:
64 DeferredPipeController();
65 void setPlaybackCanvas(SkCanvas*);
66 virtual ~DeferredPipeController();
67 virtual void* requestBlock(size_t minRequest, size_t* actual) SK_OVERRIDE;
68 virtual void notifyWritten(size_t bytes) SK_OVERRIDE;
junov@chromium.orgfb103892012-09-20 19:35:43 +000069 void playback(bool silent);
junov@chromium.orga38dfb62012-09-20 22:10:33 +000070 bool hasPendingCommands() const { return fAllocator.blockCount() != 0; }
junov@chromium.org88e29142012-08-07 16:48:22 +000071 size_t storageAllocatedForRecording() const { return fAllocator.totalCapacity(); }
72private:
73 enum {
74 kMinBlockSize = 4096
75 };
76 struct PipeBlock {
77 PipeBlock(void* block, size_t size) { fBlock = block, fSize = size; }
78 void* fBlock;
79 size_t fSize;
80 };
81 void* fBlock;
82 size_t fBytesWritten;
83 SkChunkAlloc fAllocator;
84 SkTDArray<PipeBlock> fBlockList;
85 SkGPipeReader fReader;
86};
87
88DeferredPipeController::DeferredPipeController() :
89 fAllocator(kMinBlockSize) {
90 fBlock = NULL;
91 fBytesWritten = 0;
92}
93
94DeferredPipeController::~DeferredPipeController() {
95 fAllocator.reset();
96}
97
98void DeferredPipeController::setPlaybackCanvas(SkCanvas* canvas) {
99 fReader.setCanvas(canvas);
100}
101
102void* DeferredPipeController::requestBlock(size_t minRequest, size_t *actual) {
103 if (fBlock) {
104 // Save the previous block for later
105 PipeBlock previousBloc(fBlock, fBytesWritten);
106 fBlockList.push(previousBloc);
107 }
108 int32_t blockSize = SkMax32(minRequest, kMinBlockSize);
109 fBlock = fAllocator.allocThrow(blockSize);
110 fBytesWritten = 0;
111 *actual = blockSize;
112 return fBlock;
113}
114
115void DeferredPipeController::notifyWritten(size_t bytes) {
116 fBytesWritten += bytes;
117}
118
junov@chromium.orgfb103892012-09-20 19:35:43 +0000119void DeferredPipeController::playback(bool silent) {
120 uint32_t flags = silent ? SkGPipeReader::kSilent_PlaybackFlag : 0;
junov@chromium.org88e29142012-08-07 16:48:22 +0000121 for (int currentBlock = 0; currentBlock < fBlockList.count(); currentBlock++ ) {
junov@chromium.orgfb103892012-09-20 19:35:43 +0000122 fReader.playback(fBlockList[currentBlock].fBlock, fBlockList[currentBlock].fSize,
123 flags);
junov@chromium.org88e29142012-08-07 16:48:22 +0000124 }
125 fBlockList.reset();
126
127 if (fBlock) {
junov@chromium.orgfb103892012-09-20 19:35:43 +0000128 fReader.playback(fBlock, fBytesWritten, flags);
junov@chromium.org88e29142012-08-07 16:48:22 +0000129 fBlock = NULL;
130 }
131
132 // Release all allocated blocks
133 fAllocator.reset();
134}
135
junov@chromium.org88e29142012-08-07 16:48:22 +0000136//-----------------------------------------------------------------------------
137// DeferredDevice
138//-----------------------------------------------------------------------------
junov@chromium.org88e29142012-08-07 16:48:22 +0000139class DeferredDevice : public SkDevice {
140public:
141 DeferredDevice(SkDevice* immediateDevice,
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000142 SkDeferredCanvas::NotificationClient* notificationClient = NULL);
junov@chromium.org88e29142012-08-07 16:48:22 +0000143 ~DeferredDevice();
144
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000145 void setNotificationClient(SkDeferredCanvas::NotificationClient* notificationClient);
junov@chromium.org88e29142012-08-07 16:48:22 +0000146 SkCanvas* recordingCanvas();
147 SkCanvas* immediateCanvas() const {return fImmediateCanvas;}
148 SkDevice* immediateDevice() const {return fImmediateDevice;}
149 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);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000153 size_t getBitmapSizeThreshold() const;
154 void setBitmapSizeThreshold(size_t sizeThreshold);
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000155 void flushPendingCommands(PlaybackMode);
junov@chromium.org0a67f962012-09-19 22:48:34 +0000156 void skipPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000157 void setMaxRecordingStorage(size_t);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000158 void recordedDrawCommand();
junov@chromium.org88e29142012-08-07 16:48:22 +0000159
160 virtual uint32_t getDeviceCapabilities() SK_OVERRIDE;
161 virtual int width() const SK_OVERRIDE;
162 virtual int height() const SK_OVERRIDE;
163 virtual SkGpuRenderTarget* accessRenderTarget() SK_OVERRIDE;
164
165 virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
166 int width, int height,
167 bool isOpaque,
168 Usage usage) SK_OVERRIDE;
169
170 virtual void writePixels(const SkBitmap& bitmap, int x, int y,
171 SkCanvas::Config8888 config8888) SK_OVERRIDE;
172
173protected:
174 virtual const SkBitmap& onAccessBitmap(SkBitmap*) SK_OVERRIDE;
175 virtual bool onReadPixels(const SkBitmap& bitmap,
176 int x, int y,
177 SkCanvas::Config8888 config8888) SK_OVERRIDE;
178
179 // The following methods are no-ops on a deferred device
180 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*)
181 SK_OVERRIDE
182 {return false;}
junov@chromium.org88e29142012-08-07 16:48:22 +0000183
184 // None of the following drawing methods should ever get called on the
185 // deferred device
186 virtual void clear(SkColor color)
187 {SkASSERT(0);}
188 virtual void drawPaint(const SkDraw&, const SkPaint& paint)
189 {SkASSERT(0);}
190 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
191 size_t count, const SkPoint[],
192 const SkPaint& paint)
193 {SkASSERT(0);}
194 virtual void drawRect(const SkDraw&, const SkRect& r,
195 const SkPaint& paint)
196 {SkASSERT(0);}
197 virtual void drawPath(const SkDraw&, const SkPath& path,
198 const SkPaint& paint,
199 const SkMatrix* prePathMatrix = NULL,
200 bool pathIsMutable = false)
201 {SkASSERT(0);}
202 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
203 const SkIRect* srcRectOrNull,
204 const SkMatrix& matrix, const SkPaint& paint)
205 {SkASSERT(0);}
206 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
207 int x, int y, const SkPaint& paint)
208 {SkASSERT(0);}
209 virtual void drawText(const SkDraw&, const void* text, size_t len,
210 SkScalar x, SkScalar y, const SkPaint& paint)
211 {SkASSERT(0);}
212 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
213 const SkScalar pos[], SkScalar constY,
214 int scalarsPerPos, const SkPaint& paint)
215 {SkASSERT(0);}
216 virtual void drawTextOnPath(const SkDraw&, const void* text,
217 size_t len, const SkPath& path,
218 const SkMatrix* matrix,
219 const SkPaint& paint)
220 {SkASSERT(0);}
221 virtual void drawPosTextOnPath(const SkDraw& draw, const void* text,
222 size_t len, const SkPoint pos[],
223 const SkPaint& paint,
224 const SkPath& path,
225 const SkMatrix* matrix)
226 {SkASSERT(0);}
227 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
228 int vertexCount, const SkPoint verts[],
229 const SkPoint texs[], const SkColor colors[],
230 SkXfermode* xmode, const uint16_t indices[],
231 int indexCount, const SkPaint& paint)
232 {SkASSERT(0);}
233 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
234 const SkPaint&)
235 {SkASSERT(0);}
236private:
237 virtual void flush();
238
junov@chromium.org88e29142012-08-07 16:48:22 +0000239 void beginRecording();
240
241 DeferredPipeController fPipeController;
242 SkGPipeWriter fPipeWriter;
243 SkDevice* fImmediateDevice;
244 SkCanvas* fImmediateCanvas;
245 SkCanvas* fRecordingCanvas;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000246 SkDeferredCanvas::NotificationClient* fNotificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000247 bool fFreshFrame;
248 size_t fMaxRecordingStorageBytes;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000249 size_t fPreviousStorageAllocated;
sugoi@google.com7775fd52012-11-21 15:47:04 +0000250 size_t fBitmapSizeThreshold;
junov@chromium.org88e29142012-08-07 16:48:22 +0000251};
252
253DeferredDevice::DeferredDevice(
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000254 SkDevice* immediateDevice, SkDeferredCanvas::NotificationClient* notificationClient) :
bungeman@google.com532470f2013-01-22 19:25:14 +0000255 SkDevice(SkBitmap::kNo_Config,
256 immediateDevice->width(), immediateDevice->height(),
257 immediateDevice->isOpaque(),
258 immediateDevice->getDeviceProperties())
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000259 , fRecordingCanvas(NULL)
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000260 , fFreshFrame(true)
sugoi@google.com7775fd52012-11-21 15:47:04 +0000261 , fPreviousStorageAllocated(0)
sugoi@google.com5347de12012-11-21 16:44:45 +0000262 , fBitmapSizeThreshold(kDeferredCanvasBitmapSizeThreshold){
junov@chromium.org88e29142012-08-07 16:48:22 +0000263
264 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000265 fNotificationClient = notificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000266 fImmediateDevice = immediateDevice; // ref counted via fImmediateCanvas
267 fImmediateCanvas = SkNEW_ARGS(SkCanvas, (fImmediateDevice));
268 fPipeController.setPlaybackCanvas(fImmediateCanvas);
269 this->beginRecording();
270}
271
272DeferredDevice::~DeferredDevice() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000273 this->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000274 SkSafeUnref(fImmediateCanvas);
junov@chromium.org88e29142012-08-07 16:48:22 +0000275}
276
277void DeferredDevice::setMaxRecordingStorage(size_t maxStorage) {
278 fMaxRecordingStorageBytes = maxStorage;
279 this->recordingCanvas(); // Accessing the recording canvas applies the new limit.
280}
281
junov@chromium.org88e29142012-08-07 16:48:22 +0000282void DeferredDevice::beginRecording() {
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000283 SkASSERT(NULL == fRecordingCanvas);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000284 fRecordingCanvas = fPipeWriter.startRecording(&fPipeController, 0,
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000285 fImmediateDevice->width(), fImmediateDevice->height());
junov@chromium.org88e29142012-08-07 16:48:22 +0000286}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000287
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000288void DeferredDevice::setNotificationClient(
289 SkDeferredCanvas::NotificationClient* notificationClient) {
junov@chromium.org52805482012-08-20 14:25:04 +0000290 fNotificationClient = notificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000291}
292
junov@chromium.org0a67f962012-09-19 22:48:34 +0000293void DeferredDevice::skipPendingCommands() {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000294 if (!fRecordingCanvas->isDrawingToLayer() && fPipeController.hasPendingCommands()) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000295 fFreshFrame = true;
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000296 flushPendingCommands(kSilent_PlaybackMode);
junov@google.com52a00ca2012-10-01 15:27:14 +0000297 if (fNotificationClient) {
298 fNotificationClient->skippedPendingDrawCommands();
299 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000300 }
301}
302
303bool DeferredDevice::isFreshFrame() {
304 bool ret = fFreshFrame;
305 fFreshFrame = false;
306 return ret;
307}
308
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000309bool DeferredDevice::hasPendingCommands() {
310 return fPipeController.hasPendingCommands();
311}
312
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000313void DeferredDevice::flushPendingCommands(PlaybackMode playbackMode) {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000314 if (!fPipeController.hasPendingCommands()) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000315 return;
316 }
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000317 if (playbackMode == kNormal_PlaybackMode && fNotificationClient) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000318 fNotificationClient->prepareForDraw();
junov@chromium.org88e29142012-08-07 16:48:22 +0000319 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000320 fPipeWriter.flushRecording(true);
junov@chromium.orgd4501a02012-10-30 19:05:17 +0000321 fPipeController.playback(kSilent_PlaybackMode == playbackMode);
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000322 if (playbackMode == kNormal_PlaybackMode && fNotificationClient) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000323 fNotificationClient->flushedDrawCommands();
324 }
325 fPreviousStorageAllocated = storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000326}
327
328void DeferredDevice::flush() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000329 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000330 fImmediateCanvas->flush();
331}
332
333size_t DeferredDevice::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000334 size_t val = fPipeWriter.freeMemoryIfPossible(bytesToFree);
335 fPreviousStorageAllocated = storageAllocatedForRecording();
336 return val;
junov@chromium.org88e29142012-08-07 16:48:22 +0000337}
338
sugoi@google.com7775fd52012-11-21 15:47:04 +0000339size_t DeferredDevice::getBitmapSizeThreshold() const {
340 return fBitmapSizeThreshold;
341}
342
343void DeferredDevice::setBitmapSizeThreshold(size_t sizeThreshold) {
344 fBitmapSizeThreshold = sizeThreshold;
345}
346
junov@chromium.org88e29142012-08-07 16:48:22 +0000347size_t DeferredDevice::storageAllocatedForRecording() const {
348 return (fPipeController.storageAllocatedForRecording()
349 + fPipeWriter.storageAllocatedForRecording());
350}
351
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000352void DeferredDevice::recordedDrawCommand() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000353 size_t storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000354
junov@chromium.org88e29142012-08-07 16:48:22 +0000355 if (storageAllocated > fMaxRecordingStorageBytes) {
356 // First, attempt to reduce cache without flushing
357 size_t tryFree = storageAllocated - fMaxRecordingStorageBytes;
358 if (this->freeMemoryIfPossible(tryFree) < tryFree) {
359 // Flush is necessary to free more space.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000360 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000361 // Free as much as possible to avoid oscillating around fMaxRecordingStorageBytes
362 // which could cause a high flushing frequency.
bsalomon@google.com100abf42012-09-05 17:40:04 +0000363 this->freeMemoryIfPossible(~0U);
junov@chromium.org88e29142012-08-07 16:48:22 +0000364 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000365 storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000366 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000367
rmistry@google.comd6176b02012-08-23 18:14:13 +0000368 if (fNotificationClient &&
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000369 storageAllocated != fPreviousStorageAllocated) {
370 fPreviousStorageAllocated = storageAllocated;
371 fNotificationClient->storageAllocatedForRecordingChanged(storageAllocated);
372 }
373}
374
375SkCanvas* DeferredDevice::recordingCanvas() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000376 return fRecordingCanvas;
377}
378
rmistry@google.comd6176b02012-08-23 18:14:13 +0000379uint32_t DeferredDevice::getDeviceCapabilities() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000380 return fImmediateDevice->getDeviceCapabilities();
381}
382
rmistry@google.comd6176b02012-08-23 18:14:13 +0000383int DeferredDevice::width() const {
junov@chromium.org88e29142012-08-07 16:48:22 +0000384 return fImmediateDevice->width();
385}
386
387int DeferredDevice::height() const {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000388 return fImmediateDevice->height();
junov@chromium.org88e29142012-08-07 16:48:22 +0000389}
390
391SkGpuRenderTarget* DeferredDevice::accessRenderTarget() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000392 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000393 return fImmediateDevice->accessRenderTarget();
394}
395
396void DeferredDevice::writePixels(const SkBitmap& bitmap,
397 int x, int y, SkCanvas::Config8888 config8888) {
398
399 if (x <= 0 && y <= 0 && (x + bitmap.width()) >= width() &&
400 (y + bitmap.height()) >= height()) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000401 this->skipPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000402 }
403
404 if (SkBitmap::kARGB_8888_Config == bitmap.config() &&
405 SkCanvas::kNative_Premul_Config8888 != config8888 &&
406 kPMColorAlias != config8888) {
407 //Special case config: no deferral
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000408 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000409 fImmediateDevice->writePixels(bitmap, x, y, config8888);
410 return;
411 }
412
413 SkPaint paint;
414 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000415 if (shouldDrawImmediately(&bitmap, NULL, getBitmapSizeThreshold())) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000416 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000417 fImmediateCanvas->drawSprite(bitmap, x, y, &paint);
418 } else {
419 this->recordingCanvas()->drawSprite(bitmap, x, y, &paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000420 this->recordedDrawCommand();
421
junov@chromium.org88e29142012-08-07 16:48:22 +0000422 }
423}
424
425const SkBitmap& DeferredDevice::onAccessBitmap(SkBitmap*) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000426 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000427 return fImmediateDevice->accessBitmap(false);
428}
429
430SkDevice* DeferredDevice::onCreateCompatibleDevice(
431 SkBitmap::Config config, int width, int height, bool isOpaque,
432 Usage usage) {
433
434 // Save layer usage not supported, and not required by SkDeferredCanvas.
435 SkASSERT(usage != kSaveLayer_Usage);
436 // Create a compatible non-deferred device.
437 SkAutoTUnref<SkDevice> compatibleDevice
438 (fImmediateDevice->createCompatibleDevice(config, width, height,
439 isOpaque));
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000440 return SkNEW_ARGS(DeferredDevice, (compatibleDevice, fNotificationClient));
junov@chromium.org88e29142012-08-07 16:48:22 +0000441}
442
443bool DeferredDevice::onReadPixels(
444 const SkBitmap& bitmap, int x, int y, SkCanvas::Config8888 config8888) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000445 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000446 return fImmediateCanvas->readPixels(const_cast<SkBitmap*>(&bitmap),
447 x, y, config8888);
448}
449
sugoi@google.com7775fd52012-11-21 15:47:04 +0000450class AutoImmediateDrawIfNeeded {
451public:
452 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkBitmap* bitmap,
453 const SkPaint* paint) {
454 this->init(canvas, bitmap, paint);
455 }
456
457 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkPaint* paint) {
458 this->init(canvas, NULL, paint);
459 }
460
461 ~AutoImmediateDrawIfNeeded() {
462 if (fCanvas) {
463 fCanvas->setDeferredDrawing(true);
464 }
465 }
466private:
467 void init(SkDeferredCanvas& canvas, const SkBitmap* bitmap, const SkPaint* paint)
468 {
469 DeferredDevice* device = static_cast<DeferredDevice*>(canvas.getDevice());
470 if (canvas.isDeferredDrawing() && (NULL != device) &&
471 shouldDrawImmediately(bitmap, paint, device->getBitmapSizeThreshold())) {
472 canvas.setDeferredDrawing(false);
473 fCanvas = &canvas;
474 } else {
475 fCanvas = NULL;
476 }
477 }
478
479 SkDeferredCanvas* fCanvas;
480};
junov@chromium.org88e29142012-08-07 16:48:22 +0000481
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000482SkDeferredCanvas::SkDeferredCanvas() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000483 this->init();
junov@google.com4370aed2012-01-18 16:21:08 +0000484}
485
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000486SkDeferredCanvas::SkDeferredCanvas(SkDevice* device) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000487 this->init();
488 this->setDevice(device);
junov@google.com4370aed2012-01-18 16:21:08 +0000489}
490
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000491void SkDeferredCanvas::init() {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000492 fDeferredDrawing = true; // On by default
junov@google.com4370aed2012-01-18 16:21:08 +0000493}
494
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000495void SkDeferredCanvas::setMaxRecordingStorage(size_t maxStorage) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000496 this->validate();
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000497 this->getDeferredDevice()->setMaxRecordingStorage(maxStorage);
498}
499
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000500size_t SkDeferredCanvas::storageAllocatedForRecording() const {
501 return this->getDeferredDevice()->storageAllocatedForRecording();
502}
503
504size_t SkDeferredCanvas::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000505 return this->getDeferredDevice()->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000506}
507
sugoi@google.com7775fd52012-11-21 15:47:04 +0000508void SkDeferredCanvas::setBitmapSizeThreshold(size_t sizeThreshold) {
509 DeferredDevice* deferredDevice = this->getDeferredDevice();
510 SkASSERT(deferredDevice);
511 deferredDevice->setBitmapSizeThreshold(sizeThreshold);
512}
513
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000514void SkDeferredCanvas::recordedDrawCommand() {
515 if (fDeferredDrawing) {
516 this->getDeferredDevice()->recordedDrawCommand();
517 }
518}
519
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000520void SkDeferredCanvas::validate() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000521 SkASSERT(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000522}
523
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000524SkCanvas* SkDeferredCanvas::drawingCanvas() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000525 this->validate();
526 return fDeferredDrawing ? this->getDeferredDevice()->recordingCanvas() :
527 this->getDeferredDevice()->immediateCanvas();
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000528}
529
junov@chromium.org88e29142012-08-07 16:48:22 +0000530SkCanvas* SkDeferredCanvas::immediateCanvas() const {
531 this->validate();
532 return this->getDeferredDevice()->immediateCanvas();
533}
534
535DeferredDevice* SkDeferredCanvas::getDeferredDevice() const {
536 return static_cast<DeferredDevice*>(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000537}
538
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000539void SkDeferredCanvas::setDeferredDrawing(bool val) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000540 this->validate(); // Must set device before calling this method
junov@google.com4370aed2012-01-18 16:21:08 +0000541 if (val != fDeferredDrawing) {
542 if (fDeferredDrawing) {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000543 // Going live.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000544 this->getDeferredDevice()->flushPendingCommands(kNormal_PlaybackMode);
junov@google.com4370aed2012-01-18 16:21:08 +0000545 }
546 fDeferredDrawing = val;
547 }
548}
549
junov@chromium.org88e29142012-08-07 16:48:22 +0000550bool SkDeferredCanvas::isDeferredDrawing() const {
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +0000551 return fDeferredDrawing;
552}
553
junov@chromium.org88e29142012-08-07 16:48:22 +0000554bool SkDeferredCanvas::isFreshFrame() const {
555 return this->getDeferredDevice()->isFreshFrame();
556}
557
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000558bool SkDeferredCanvas::hasPendingCommands() const {
559 return this->getDeferredDevice()->hasPendingCommands();
560}
561
junov@chromium.orgfb103892012-09-20 19:35:43 +0000562void SkDeferredCanvas::silentFlush() {
563 if (fDeferredDrawing) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000564 this->getDeferredDevice()->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000565 }
566}
567
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000568SkDeferredCanvas::~SkDeferredCanvas() {
junov@google.com4370aed2012-01-18 16:21:08 +0000569}
570
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000571SkDevice* SkDeferredCanvas::setDevice(SkDevice* device) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000572 this->INHERITED::setDevice(SkNEW_ARGS(DeferredDevice, (device)))->unref();
junov@google.com4370aed2012-01-18 16:21:08 +0000573 return device;
574}
575
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000576SkDeferredCanvas::NotificationClient* SkDeferredCanvas::setNotificationClient(
577 NotificationClient* notificationClient) {
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000578
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000579 DeferredDevice* deferredDevice = this->getDeferredDevice();
junov@google.com4370aed2012-01-18 16:21:08 +0000580 SkASSERT(deferredDevice);
581 if (deferredDevice) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000582 deferredDevice->setNotificationClient(notificationClient);
junov@google.com4370aed2012-01-18 16:21:08 +0000583 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000584 return notificationClient;
junov@google.com4370aed2012-01-18 16:21:08 +0000585}
586
587bool SkDeferredCanvas::isFullFrame(const SkRect* rect,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000588 const SkPaint* paint) const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000589 SkCanvas* canvas = this->drawingCanvas();
590 SkISize canvasSize = this->getDeviceSize();
junov@google.com4370aed2012-01-18 16:21:08 +0000591 if (rect) {
592 if (!canvas->getTotalMatrix().rectStaysRect()) {
593 return false; // conservative
594 }
595
596 SkRect transformedRect;
597 canvas->getTotalMatrix().mapRect(&transformedRect, *rect);
598
599 if (paint) {
600 SkPaint::Style paintStyle = paint->getStyle();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000601 if (!(paintStyle == SkPaint::kFill_Style ||
junov@google.com4370aed2012-01-18 16:21:08 +0000602 paintStyle == SkPaint::kStrokeAndFill_Style)) {
603 return false;
604 }
605 if (paint->getMaskFilter() || paint->getLooper()
606 || paint->getPathEffect() || paint->getImageFilter()) {
607 return false; // conservative
608 }
609 }
610
611 // The following test holds with AA enabled, and is conservative
612 // by a 0.5 pixel margin with AA disabled
rmistry@google.comd6176b02012-08-23 18:14:13 +0000613 if (transformedRect.fLeft > SkIntToScalar(0) ||
614 transformedRect.fTop > SkIntToScalar(0) ||
junov@chromium.orgb1e218e2012-02-13 22:27:58 +0000615 transformedRect.fRight < SkIntToScalar(canvasSize.fWidth) ||
616 transformedRect.fBottom < SkIntToScalar(canvasSize.fHeight)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000617 return false;
618 }
619 }
620
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000621 return this->getClipStack()->quickContains(SkRect::MakeXYWH(0, 0,
622 SkIntToScalar(canvasSize.fWidth), SkIntToScalar(canvasSize.fHeight)));
junov@google.com4370aed2012-01-18 16:21:08 +0000623}
624
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000625int SkDeferredCanvas::save(SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000626 this->drawingCanvas()->save(flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000627 int val = this->INHERITED::save(flags);
628 this->recordedDrawCommand();
629
630 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000631}
632
633int SkDeferredCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000634 SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000635 this->drawingCanvas()->saveLayer(bounds, paint, flags);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000636 int count = this->INHERITED::save(flags);
637 this->clipRectBounds(bounds, flags, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000638 this->recordedDrawCommand();
639
junov@chromium.orga907ac32012-02-24 21:54:07 +0000640 return count;
junov@google.com4370aed2012-01-18 16:21:08 +0000641}
642
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000643void SkDeferredCanvas::restore() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000644 this->drawingCanvas()->restore();
junov@chromium.orga907ac32012-02-24 21:54:07 +0000645 this->INHERITED::restore();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000646 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000647}
648
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000649bool SkDeferredCanvas::isDrawingToLayer() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000650 return this->drawingCanvas()->isDrawingToLayer();
junov@google.com4370aed2012-01-18 16:21:08 +0000651}
652
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000653bool SkDeferredCanvas::translate(SkScalar dx, SkScalar dy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000654 this->drawingCanvas()->translate(dx, dy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000655 bool val = this->INHERITED::translate(dx, dy);
656 this->recordedDrawCommand();
657 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000658}
659
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000660bool SkDeferredCanvas::scale(SkScalar sx, SkScalar sy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000661 this->drawingCanvas()->scale(sx, sy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000662 bool val = this->INHERITED::scale(sx, sy);
663 this->recordedDrawCommand();
664 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000665}
666
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000667bool SkDeferredCanvas::rotate(SkScalar degrees) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000668 this->drawingCanvas()->rotate(degrees);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000669 bool val = this->INHERITED::rotate(degrees);
670 this->recordedDrawCommand();
671 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000672}
673
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000674bool SkDeferredCanvas::skew(SkScalar sx, SkScalar sy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000675 this->drawingCanvas()->skew(sx, sy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000676 bool val = this->INHERITED::skew(sx, sy);
677 this->recordedDrawCommand();
678 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000679}
680
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000681bool SkDeferredCanvas::concat(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000682 this->drawingCanvas()->concat(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000683 bool val = this->INHERITED::concat(matrix);
684 this->recordedDrawCommand();
685 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000686}
687
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000688void SkDeferredCanvas::setMatrix(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000689 this->drawingCanvas()->setMatrix(matrix);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000690 this->INHERITED::setMatrix(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000691 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000692}
693
694bool SkDeferredCanvas::clipRect(const SkRect& rect,
695 SkRegion::Op op,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000696 bool doAntiAlias) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000697 this->drawingCanvas()->clipRect(rect, op, doAntiAlias);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000698 bool val = this->INHERITED::clipRect(rect, op, doAntiAlias);
699 this->recordedDrawCommand();
700 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000701}
702
reed@google.com4ed0fb72012-12-12 20:48:18 +0000703bool SkDeferredCanvas::clipRRect(const SkRRect& rrect,
704 SkRegion::Op op,
705 bool doAntiAlias) {
706 this->drawingCanvas()->clipRRect(rrect, op, doAntiAlias);
707 bool val = this->INHERITED::clipRRect(rrect, op, doAntiAlias);
708 this->recordedDrawCommand();
709 return val;
710}
711
junov@google.com4370aed2012-01-18 16:21:08 +0000712bool SkDeferredCanvas::clipPath(const SkPath& path,
713 SkRegion::Op op,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000714 bool doAntiAlias) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000715 this->drawingCanvas()->clipPath(path, op, doAntiAlias);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000716 bool val = this->INHERITED::clipPath(path, op, doAntiAlias);
717 this->recordedDrawCommand();
718 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000719}
720
721bool SkDeferredCanvas::clipRegion(const SkRegion& deviceRgn,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000722 SkRegion::Op op) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000723 this->drawingCanvas()->clipRegion(deviceRgn, op);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000724 bool val = this->INHERITED::clipRegion(deviceRgn, op);
725 this->recordedDrawCommand();
726 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000727}
728
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000729void SkDeferredCanvas::clear(SkColor color) {
junov@google.com4370aed2012-01-18 16:21:08 +0000730 // purge pending commands
731 if (fDeferredDrawing) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000732 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000733 }
734
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000735 this->drawingCanvas()->clear(color);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000736 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000737}
738
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000739void SkDeferredCanvas::drawPaint(const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000740 if (fDeferredDrawing && this->isFullFrame(NULL, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000741 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000742 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000743 }
junov@chromium.org10f7f972012-07-31 21:01:51 +0000744 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000745 this->drawingCanvas()->drawPaint(paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000746 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000747}
748
749void SkDeferredCanvas::drawPoints(PointMode mode, size_t count,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000750 const SkPoint pts[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000751 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000752 this->drawingCanvas()->drawPoints(mode, count, pts, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000753 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000754}
755
reed@google.com4ed0fb72012-12-12 20:48:18 +0000756void SkDeferredCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
757 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
758 this->drawingCanvas()->drawOval(rect, paint);
759 this->recordedDrawCommand();
760}
761
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000762void SkDeferredCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000763 if (fDeferredDrawing && this->isFullFrame(&rect, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000764 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000765 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000766 }
skia.committer@gmail.com306ab9d2012-12-13 02:01:33 +0000767
junov@chromium.org10f7f972012-07-31 21:01:51 +0000768 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000769 this->drawingCanvas()->drawRect(rect, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000770 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000771}
772
reed@google.com4ed0fb72012-12-12 20:48:18 +0000773void SkDeferredCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
774 if (rrect.isRect()) {
775 this->SkDeferredCanvas::drawRect(rrect.getBounds(), paint);
776 } else if (rrect.isOval()) {
777 this->SkDeferredCanvas::drawOval(rrect.getBounds(), paint);
778 } else {
779 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
780 this->drawingCanvas()->drawRRect(rrect, paint);
781 this->recordedDrawCommand();
782 }
783}
784
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000785void SkDeferredCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000786 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000787 this->drawingCanvas()->drawPath(path, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000788 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000789}
790
791void SkDeferredCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000792 SkScalar top, const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000793 SkRect bitmapRect = SkRect::MakeXYWH(left, top,
794 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000795 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000796 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000797 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000798 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000799 }
800
junov@chromium.org10f7f972012-07-31 21:01:51 +0000801 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000802 this->drawingCanvas()->drawBitmap(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000803 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000804}
805
reed@google.com71121732012-09-18 15:14:33 +0000806void SkDeferredCanvas::drawBitmapRectToRect(const SkBitmap& bitmap,
807 const SkRect* src,
808 const SkRect& dst,
809 const SkPaint* paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000810 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000811 this->isFullFrame(&dst, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000812 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000813 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000814 }
815
junov@chromium.org10f7f972012-07-31 21:01:51 +0000816 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
reed@google.com71121732012-09-18 15:14:33 +0000817 this->drawingCanvas()->drawBitmapRectToRect(bitmap, src, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000818 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000819}
820
821
822void SkDeferredCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
823 const SkMatrix& m,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000824 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000825 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
826 // covers canvas entirely and transformed bitmap covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000827 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000828 this->drawingCanvas()->drawBitmapMatrix(bitmap, m, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000829 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000830}
831
832void SkDeferredCanvas::drawBitmapNine(const SkBitmap& bitmap,
833 const SkIRect& center, const SkRect& dst,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000834 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000835 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
836 // covers canvas entirely and dst covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000837 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000838 this->drawingCanvas()->drawBitmapNine(bitmap, center, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000839 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000840}
841
842void SkDeferredCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000843 const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000844 SkRect bitmapRect = SkRect::MakeXYWH(
845 SkIntToScalar(left),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000846 SkIntToScalar(top),
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000847 SkIntToScalar(bitmap.width()),
848 SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000849 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000850 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000851 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000852 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000853 }
854
junov@chromium.org10f7f972012-07-31 21:01:51 +0000855 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000856 this->drawingCanvas()->drawSprite(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000857 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000858}
859
860void SkDeferredCanvas::drawText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000861 SkScalar x, SkScalar y, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000862 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000863 this->drawingCanvas()->drawText(text, byteLength, x, y, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000864 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000865}
866
867void SkDeferredCanvas::drawPosText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000868 const SkPoint pos[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000869 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000870 this->drawingCanvas()->drawPosText(text, byteLength, pos, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000871 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000872}
873
874void SkDeferredCanvas::drawPosTextH(const void* text, size_t byteLength,
875 const SkScalar xpos[], SkScalar constY,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000876 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000877 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000878 this->drawingCanvas()->drawPosTextH(text, byteLength, xpos, constY, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000879 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000880}
881
882void SkDeferredCanvas::drawTextOnPath(const void* text, size_t byteLength,
883 const SkPath& path,
884 const SkMatrix* matrix,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000885 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000886 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000887 this->drawingCanvas()->drawTextOnPath(text, byteLength, path, matrix, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000888 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000889}
890
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000891void SkDeferredCanvas::drawPicture(SkPicture& picture) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000892 this->drawingCanvas()->drawPicture(picture);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000893 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000894}
895
896void SkDeferredCanvas::drawVertices(VertexMode vmode, int vertexCount,
897 const SkPoint vertices[],
898 const SkPoint texs[],
899 const SkColor colors[], SkXfermode* xmode,
900 const uint16_t indices[], int indexCount,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000901 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000902 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000903 this->drawingCanvas()->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode,
904 indices, indexCount, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000905 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000906}
907
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000908SkBounder* SkDeferredCanvas::setBounder(SkBounder* bounder) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000909 this->drawingCanvas()->setBounder(bounder);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000910 this->INHERITED::setBounder(bounder);
911 this->recordedDrawCommand();
912 return bounder;
junov@google.com4370aed2012-01-18 16:21:08 +0000913}
914
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000915SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000916 this->drawingCanvas()->setDrawFilter(filter);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000917 this->INHERITED::setDrawFilter(filter);
918 this->recordedDrawCommand();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000919 return filter;
junov@google.com4370aed2012-01-18 16:21:08 +0000920}
921
922SkCanvas* SkDeferredCanvas::canvasForDrawIter() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000923 return this->drawingCanvas();
junov@google.com4370aed2012-01-18 16:21:08 +0000924}