blob: 5beaa5bc925279243ee3d22e28c93b81aa9174a6 [file] [log] [blame]
junov@google.com4370aed2012-01-18 16:21:08 +00001
2/*
junov@chromium.org10f7f972012-07-31 21:01:51 +00003 * Copyright 2012 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"
17#include "SkShader.h"
junov@google.com4370aed2012-01-18 16:21:08 +000018
junov@chromium.orgbfeddae2012-07-23 13:35:14 +000019enum {
20 // Deferred canvas will auto-flush when recording reaches this limit
21 kDefaultMaxRecordingStorageBytes = 64*1024*1024,
22};
23
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +000024enum PlaybackMode {
25 kNormal_PlaybackMode,
26 kSilent_PlaybackMode,
27};
28
junov@google.com4370aed2012-01-18 16:21:08 +000029namespace {
junov@chromium.org10f7f972012-07-31 21:01:51 +000030bool shouldDrawImmediately(const SkBitmap* bitmap, const SkPaint* paint) {
31 if (bitmap && bitmap->getTexture() && !bitmap->isImmutable()) {
32 return true;
33 }
34 if (paint) {
35 SkShader* shader = paint->getShader();
36 // Here we detect the case where the shader is an SkBitmapProcShader
37 // with a gpu texture attached. Checking this without RTTI
38 // requires making the assumption that only gradient shaders
39 // and SkBitmapProcShader implement asABitmap(). The following
40 // code may need to be revised if that assumption is ever broken.
41 if (shader && !shader->asAGradient(NULL)) {
42 SkBitmap bm;
rmistry@google.comd6176b02012-08-23 18:14:13 +000043 if (shader->asABitmap(&bm, NULL, NULL) &&
junov@chromium.org10f7f972012-07-31 21:01:51 +000044 NULL != bm.getTexture()) {
45 return true;
46 }
47 }
48 }
49 return false;
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +000050}
51}
52
53class AutoImmediateDrawIfNeeded {
54public:
rmistry@google.comd6176b02012-08-23 18:14:13 +000055 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkBitmap* bitmap,
junov@chromium.org10f7f972012-07-31 21:01:51 +000056 const SkPaint* paint) {
57 this->init(canvas, bitmap, paint);
58 }
59
60 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkPaint* paint) {
61 this->init(canvas, NULL, paint);
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +000062 }
63
64 ~AutoImmediateDrawIfNeeded() {
65 if (fCanvas) {
66 fCanvas->setDeferredDrawing(true);
67 }
68 }
69private:
junov@chromium.org10f7f972012-07-31 21:01:51 +000070 void init(SkDeferredCanvas& canvas, const SkBitmap* bitmap, const SkPaint* paint)
71 {
72 if (canvas.isDeferredDrawing() && shouldDrawImmediately(bitmap, paint)) {
73 canvas.setDeferredDrawing(false);
74 fCanvas = &canvas;
75 } else {
76 fCanvas = NULL;
77 }
78 }
79
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +000080 SkDeferredCanvas* fCanvas;
81};
82
83namespace {
junov@google.com4370aed2012-01-18 16:21:08 +000084
rmistry@google.comd6176b02012-08-23 18:14:13 +000085bool isPaintOpaque(const SkPaint* paint,
junov@chromium.orgc16ca922012-02-24 22:06:27 +000086 const SkBitmap* bmpReplacesShader = NULL) {
junov@google.com4370aed2012-01-18 16:21:08 +000087 // TODO: SkXfermode should have a virtual isOpaque method, which would
88 // make it possible to test modes that do not have a Coeff representation.
junov@chromium.org87f982c2012-02-23 21:34:34 +000089
90 if (!paint) {
91 return bmpReplacesShader ? bmpReplacesShader->isOpaque() : true;
92 }
93
junov@google.com4370aed2012-01-18 16:21:08 +000094 SkXfermode::Coeff srcCoeff, dstCoeff;
junov@chromium.org87f982c2012-02-23 21:34:34 +000095 if (SkXfermode::AsCoeff(paint->getXfermode(), &srcCoeff, &dstCoeff)){
junov@google.com4370aed2012-01-18 16:21:08 +000096 switch (dstCoeff) {
97 case SkXfermode::kZero_Coeff:
98 return true;
99 case SkXfermode::kISA_Coeff:
junov@chromium.org87f982c2012-02-23 21:34:34 +0000100 if (paint->getAlpha() != 255) {
junov@google.com4370aed2012-01-18 16:21:08 +0000101 break;
102 }
103 if (bmpReplacesShader) {
104 if (!bmpReplacesShader->isOpaque()) {
105 break;
106 }
junov@chromium.org87f982c2012-02-23 21:34:34 +0000107 } else if (paint->getShader() && !paint->getShader()->isOpaque()) {
junov@google.com4370aed2012-01-18 16:21:08 +0000108 break;
109 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000110 if (paint->getColorFilter() &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000111 ((paint->getColorFilter()->getFlags() &
junov@google.com4370aed2012-01-18 16:21:08 +0000112 SkColorFilter::kAlphaUnchanged_Flag) == 0)) {
113 break;
114 }
115 return true;
116 case SkXfermode::kSA_Coeff:
junov@chromium.org87f982c2012-02-23 21:34:34 +0000117 if (paint->getAlpha() != 0) {
junov@google.com4370aed2012-01-18 16:21:08 +0000118 break;
119 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000120 if (paint->getColorFilter() &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000121 ((paint->getColorFilter()->getFlags() &
junov@google.com4370aed2012-01-18 16:21:08 +0000122 SkColorFilter::kAlphaUnchanged_Flag) == 0)) {
123 break;
124 }
125 return true;
126 case SkXfermode::kSC_Coeff:
junov@chromium.org87f982c2012-02-23 21:34:34 +0000127 if (paint->getColor() != 0) { // all components must be 0
junov@google.com4370aed2012-01-18 16:21:08 +0000128 break;
129 }
junov@chromium.org87f982c2012-02-23 21:34:34 +0000130 if (bmpReplacesShader || paint->getShader()) {
junov@google.com4370aed2012-01-18 16:21:08 +0000131 break;
132 }
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000133 if (paint->getColorFilter() && (
134 (paint->getColorFilter()->getFlags() &
junov@google.com4370aed2012-01-18 16:21:08 +0000135 SkColorFilter::kAlphaUnchanged_Flag) == 0)) {
136 break;
137 }
138 return true;
139 default:
140 break;
141 }
142 }
143 return false;
144}
145
146} // unnamed namespace
147
junov@chromium.org88e29142012-08-07 16:48:22 +0000148//-----------------------------------------------------------------------------
149// DeferredPipeController
150//-----------------------------------------------------------------------------
151
152class DeferredPipeController : public SkGPipeController {
153public:
154 DeferredPipeController();
155 void setPlaybackCanvas(SkCanvas*);
156 virtual ~DeferredPipeController();
157 virtual void* requestBlock(size_t minRequest, size_t* actual) SK_OVERRIDE;
158 virtual void notifyWritten(size_t bytes) SK_OVERRIDE;
junov@chromium.orgfb103892012-09-20 19:35:43 +0000159 void playback(bool silent);
junov@chromium.org88e29142012-08-07 16:48:22 +0000160 bool hasRecorded() const { return fAllocator.blockCount() != 0; }
161 size_t storageAllocatedForRecording() const { return fAllocator.totalCapacity(); }
162private:
163 enum {
164 kMinBlockSize = 4096
165 };
166 struct PipeBlock {
167 PipeBlock(void* block, size_t size) { fBlock = block, fSize = size; }
168 void* fBlock;
169 size_t fSize;
170 };
171 void* fBlock;
172 size_t fBytesWritten;
173 SkChunkAlloc fAllocator;
174 SkTDArray<PipeBlock> fBlockList;
175 SkGPipeReader fReader;
176};
177
178DeferredPipeController::DeferredPipeController() :
179 fAllocator(kMinBlockSize) {
180 fBlock = NULL;
181 fBytesWritten = 0;
182}
183
184DeferredPipeController::~DeferredPipeController() {
185 fAllocator.reset();
186}
187
188void DeferredPipeController::setPlaybackCanvas(SkCanvas* canvas) {
189 fReader.setCanvas(canvas);
190}
191
192void* DeferredPipeController::requestBlock(size_t minRequest, size_t *actual) {
193 if (fBlock) {
194 // Save the previous block for later
195 PipeBlock previousBloc(fBlock, fBytesWritten);
196 fBlockList.push(previousBloc);
197 }
198 int32_t blockSize = SkMax32(minRequest, kMinBlockSize);
199 fBlock = fAllocator.allocThrow(blockSize);
200 fBytesWritten = 0;
201 *actual = blockSize;
202 return fBlock;
203}
204
205void DeferredPipeController::notifyWritten(size_t bytes) {
206 fBytesWritten += bytes;
207}
208
junov@chromium.orgfb103892012-09-20 19:35:43 +0000209void DeferredPipeController::playback(bool silent) {
210 uint32_t flags = silent ? SkGPipeReader::kSilent_PlaybackFlag : 0;
junov@chromium.org88e29142012-08-07 16:48:22 +0000211 for (int currentBlock = 0; currentBlock < fBlockList.count(); currentBlock++ ) {
junov@chromium.orgfb103892012-09-20 19:35:43 +0000212 fReader.playback(fBlockList[currentBlock].fBlock, fBlockList[currentBlock].fSize,
213 flags);
junov@chromium.org88e29142012-08-07 16:48:22 +0000214 }
215 fBlockList.reset();
216
217 if (fBlock) {
junov@chromium.orgfb103892012-09-20 19:35:43 +0000218 fReader.playback(fBlock, fBytesWritten, flags);
junov@chromium.org88e29142012-08-07 16:48:22 +0000219 fBlock = NULL;
220 }
221
222 // Release all allocated blocks
223 fAllocator.reset();
224}
225
junov@chromium.org88e29142012-08-07 16:48:22 +0000226//-----------------------------------------------------------------------------
227// DeferredDevice
228//-----------------------------------------------------------------------------
junov@chromium.org88e29142012-08-07 16:48:22 +0000229class DeferredDevice : public SkDevice {
230public:
231 DeferredDevice(SkDevice* immediateDevice,
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000232 SkDeferredCanvas::NotificationClient* notificationClient = NULL);
junov@chromium.org88e29142012-08-07 16:48:22 +0000233 ~DeferredDevice();
234
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000235 void setNotificationClient(SkDeferredCanvas::NotificationClient* notificationClient);
junov@chromium.org88e29142012-08-07 16:48:22 +0000236 SkCanvas* recordingCanvas();
237 SkCanvas* immediateCanvas() const {return fImmediateCanvas;}
238 SkDevice* immediateDevice() const {return fImmediateDevice;}
239 bool isFreshFrame();
240 size_t storageAllocatedForRecording() const;
241 size_t freeMemoryIfPossible(size_t bytesToFree);
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000242 void flushPendingCommands(PlaybackMode);
junov@chromium.org0a67f962012-09-19 22:48:34 +0000243 void skipPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000244 void setMaxRecordingStorage(size_t);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000245 void recordedDrawCommand();
junov@chromium.org88e29142012-08-07 16:48:22 +0000246
247 virtual uint32_t getDeviceCapabilities() SK_OVERRIDE;
248 virtual int width() const SK_OVERRIDE;
249 virtual int height() const SK_OVERRIDE;
250 virtual SkGpuRenderTarget* accessRenderTarget() SK_OVERRIDE;
251
252 virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
253 int width, int height,
254 bool isOpaque,
255 Usage usage) SK_OVERRIDE;
256
257 virtual void writePixels(const SkBitmap& bitmap, int x, int y,
258 SkCanvas::Config8888 config8888) SK_OVERRIDE;
259
260protected:
261 virtual const SkBitmap& onAccessBitmap(SkBitmap*) SK_OVERRIDE;
262 virtual bool onReadPixels(const SkBitmap& bitmap,
263 int x, int y,
264 SkCanvas::Config8888 config8888) SK_OVERRIDE;
265
266 // The following methods are no-ops on a deferred device
267 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*)
268 SK_OVERRIDE
269 {return false;}
270 virtual void setMatrixClip(const SkMatrix&, const SkRegion&,
271 const SkClipStack&) SK_OVERRIDE
272 {}
273
274 // None of the following drawing methods should ever get called on the
275 // deferred device
276 virtual void clear(SkColor color)
277 {SkASSERT(0);}
278 virtual void drawPaint(const SkDraw&, const SkPaint& paint)
279 {SkASSERT(0);}
280 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
281 size_t count, const SkPoint[],
282 const SkPaint& paint)
283 {SkASSERT(0);}
284 virtual void drawRect(const SkDraw&, const SkRect& r,
285 const SkPaint& paint)
286 {SkASSERT(0);}
287 virtual void drawPath(const SkDraw&, const SkPath& path,
288 const SkPaint& paint,
289 const SkMatrix* prePathMatrix = NULL,
290 bool pathIsMutable = false)
291 {SkASSERT(0);}
292 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
293 const SkIRect* srcRectOrNull,
294 const SkMatrix& matrix, const SkPaint& paint)
295 {SkASSERT(0);}
296 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
297 int x, int y, const SkPaint& paint)
298 {SkASSERT(0);}
299 virtual void drawText(const SkDraw&, const void* text, size_t len,
300 SkScalar x, SkScalar y, const SkPaint& paint)
301 {SkASSERT(0);}
302 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
303 const SkScalar pos[], SkScalar constY,
304 int scalarsPerPos, const SkPaint& paint)
305 {SkASSERT(0);}
306 virtual void drawTextOnPath(const SkDraw&, const void* text,
307 size_t len, const SkPath& path,
308 const SkMatrix* matrix,
309 const SkPaint& paint)
310 {SkASSERT(0);}
311 virtual void drawPosTextOnPath(const SkDraw& draw, const void* text,
312 size_t len, const SkPoint pos[],
313 const SkPaint& paint,
314 const SkPath& path,
315 const SkMatrix* matrix)
316 {SkASSERT(0);}
317 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
318 int vertexCount, const SkPoint verts[],
319 const SkPoint texs[], const SkColor colors[],
320 SkXfermode* xmode, const uint16_t indices[],
321 int indexCount, const SkPaint& paint)
322 {SkASSERT(0);}
323 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
324 const SkPaint&)
325 {SkASSERT(0);}
326private:
327 virtual void flush();
328
junov@chromium.org88e29142012-08-07 16:48:22 +0000329 void beginRecording();
330
331 DeferredPipeController fPipeController;
332 SkGPipeWriter fPipeWriter;
333 SkDevice* fImmediateDevice;
334 SkCanvas* fImmediateCanvas;
335 SkCanvas* fRecordingCanvas;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000336 SkDeferredCanvas::NotificationClient* fNotificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000337 bool fFreshFrame;
338 size_t fMaxRecordingStorageBytes;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000339 size_t fPreviousStorageAllocated;
junov@chromium.org88e29142012-08-07 16:48:22 +0000340};
341
342DeferredDevice::DeferredDevice(
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000343 SkDevice* immediateDevice, SkDeferredCanvas::NotificationClient* notificationClient) :
junov@chromium.org88e29142012-08-07 16:48:22 +0000344 SkDevice(SkBitmap::kNo_Config, immediateDevice->width(),
345 immediateDevice->height(), immediateDevice->isOpaque())
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000346 , fRecordingCanvas(NULL)
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000347 , fFreshFrame(true)
348 , fPreviousStorageAllocated(0){
junov@chromium.org88e29142012-08-07 16:48:22 +0000349
350 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000351 fNotificationClient = notificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000352 fImmediateDevice = immediateDevice; // ref counted via fImmediateCanvas
353 fImmediateCanvas = SkNEW_ARGS(SkCanvas, (fImmediateDevice));
354 fPipeController.setPlaybackCanvas(fImmediateCanvas);
355 this->beginRecording();
356}
357
358DeferredDevice::~DeferredDevice() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000359 this->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000360 SkSafeUnref(fImmediateCanvas);
junov@chromium.org88e29142012-08-07 16:48:22 +0000361}
362
363void DeferredDevice::setMaxRecordingStorage(size_t maxStorage) {
364 fMaxRecordingStorageBytes = maxStorage;
365 this->recordingCanvas(); // Accessing the recording canvas applies the new limit.
366}
367
junov@chromium.org88e29142012-08-07 16:48:22 +0000368void DeferredDevice::beginRecording() {
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000369 SkASSERT(NULL == fRecordingCanvas);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000370 fRecordingCanvas = fPipeWriter.startRecording(&fPipeController, 0,
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000371 fImmediateDevice->width(), fImmediateDevice->height());
junov@chromium.org88e29142012-08-07 16:48:22 +0000372}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000373
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000374void DeferredDevice::setNotificationClient(
375 SkDeferredCanvas::NotificationClient* notificationClient) {
junov@chromium.org52805482012-08-20 14:25:04 +0000376 fNotificationClient = notificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000377}
378
junov@chromium.org0a67f962012-09-19 22:48:34 +0000379void DeferredDevice::skipPendingCommands() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000380 if (!fRecordingCanvas->isDrawingToLayer() && fPipeController.hasRecorded()) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000381 fFreshFrame = true;
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000382 flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000383 }
384}
385
386bool DeferredDevice::isFreshFrame() {
387 bool ret = fFreshFrame;
388 fFreshFrame = false;
389 return ret;
390}
391
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000392void DeferredDevice::flushPendingCommands(PlaybackMode playbackMode) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000393 if (!fPipeController.hasRecorded()) {
394 return;
395 }
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000396 if (playbackMode == kNormal_PlaybackMode && fNotificationClient) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000397 fNotificationClient->prepareForDraw();
junov@chromium.org88e29142012-08-07 16:48:22 +0000398 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000399 fPipeWriter.flushRecording(true);
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000400 fPipeController.playback(playbackMode);
401 if (playbackMode == kNormal_PlaybackMode && fNotificationClient) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000402 fNotificationClient->flushedDrawCommands();
403 }
404 fPreviousStorageAllocated = storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000405}
406
407void DeferredDevice::flush() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000408 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000409 fImmediateCanvas->flush();
410}
411
412size_t DeferredDevice::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000413 size_t val = fPipeWriter.freeMemoryIfPossible(bytesToFree);
414 fPreviousStorageAllocated = storageAllocatedForRecording();
415 return val;
junov@chromium.org88e29142012-08-07 16:48:22 +0000416}
417
418size_t DeferredDevice::storageAllocatedForRecording() const {
419 return (fPipeController.storageAllocatedForRecording()
420 + fPipeWriter.storageAllocatedForRecording());
421}
422
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000423void DeferredDevice::recordedDrawCommand() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000424 size_t storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000425
junov@chromium.org88e29142012-08-07 16:48:22 +0000426 if (storageAllocated > fMaxRecordingStorageBytes) {
427 // First, attempt to reduce cache without flushing
428 size_t tryFree = storageAllocated - fMaxRecordingStorageBytes;
429 if (this->freeMemoryIfPossible(tryFree) < tryFree) {
430 // Flush is necessary to free more space.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000431 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000432 // Free as much as possible to avoid oscillating around fMaxRecordingStorageBytes
433 // which could cause a high flushing frequency.
bsalomon@google.com100abf42012-09-05 17:40:04 +0000434 this->freeMemoryIfPossible(~0U);
junov@chromium.org88e29142012-08-07 16:48:22 +0000435 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000436 storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000437 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000438
rmistry@google.comd6176b02012-08-23 18:14:13 +0000439 if (fNotificationClient &&
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000440 storageAllocated != fPreviousStorageAllocated) {
441 fPreviousStorageAllocated = storageAllocated;
442 fNotificationClient->storageAllocatedForRecordingChanged(storageAllocated);
443 }
444}
445
446SkCanvas* DeferredDevice::recordingCanvas() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000447 return fRecordingCanvas;
448}
449
rmistry@google.comd6176b02012-08-23 18:14:13 +0000450uint32_t DeferredDevice::getDeviceCapabilities() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000451 return fImmediateDevice->getDeviceCapabilities();
452}
453
rmistry@google.comd6176b02012-08-23 18:14:13 +0000454int DeferredDevice::width() const {
junov@chromium.org88e29142012-08-07 16:48:22 +0000455 return fImmediateDevice->width();
456}
457
458int DeferredDevice::height() const {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000459 return fImmediateDevice->height();
junov@chromium.org88e29142012-08-07 16:48:22 +0000460}
461
462SkGpuRenderTarget* DeferredDevice::accessRenderTarget() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000463 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000464 return fImmediateDevice->accessRenderTarget();
465}
466
467void DeferredDevice::writePixels(const SkBitmap& bitmap,
468 int x, int y, SkCanvas::Config8888 config8888) {
469
470 if (x <= 0 && y <= 0 && (x + bitmap.width()) >= width() &&
471 (y + bitmap.height()) >= height()) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000472 this->skipPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000473 }
474
475 if (SkBitmap::kARGB_8888_Config == bitmap.config() &&
476 SkCanvas::kNative_Premul_Config8888 != config8888 &&
477 kPMColorAlias != config8888) {
478 //Special case config: no deferral
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000479 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000480 fImmediateDevice->writePixels(bitmap, x, y, config8888);
481 return;
482 }
483
484 SkPaint paint;
485 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
486 if (shouldDrawImmediately(&bitmap, NULL)) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000487 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000488 fImmediateCanvas->drawSprite(bitmap, x, y, &paint);
489 } else {
490 this->recordingCanvas()->drawSprite(bitmap, x, y, &paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000491 this->recordedDrawCommand();
492
junov@chromium.org88e29142012-08-07 16:48:22 +0000493 }
494}
495
496const SkBitmap& DeferredDevice::onAccessBitmap(SkBitmap*) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000497 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000498 return fImmediateDevice->accessBitmap(false);
499}
500
501SkDevice* DeferredDevice::onCreateCompatibleDevice(
502 SkBitmap::Config config, int width, int height, bool isOpaque,
503 Usage usage) {
504
505 // Save layer usage not supported, and not required by SkDeferredCanvas.
506 SkASSERT(usage != kSaveLayer_Usage);
507 // Create a compatible non-deferred device.
508 SkAutoTUnref<SkDevice> compatibleDevice
509 (fImmediateDevice->createCompatibleDevice(config, width, height,
510 isOpaque));
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000511 return SkNEW_ARGS(DeferredDevice, (compatibleDevice, fNotificationClient));
junov@chromium.org88e29142012-08-07 16:48:22 +0000512}
513
514bool DeferredDevice::onReadPixels(
515 const SkBitmap& bitmap, int x, int y, SkCanvas::Config8888 config8888) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000516 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000517 return fImmediateCanvas->readPixels(const_cast<SkBitmap*>(&bitmap),
518 x, y, config8888);
519}
520
521
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000522SkDeferredCanvas::SkDeferredCanvas() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000523 this->init();
junov@google.com4370aed2012-01-18 16:21:08 +0000524}
525
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000526SkDeferredCanvas::SkDeferredCanvas(SkDevice* device) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000527 this->init();
528 this->setDevice(device);
junov@google.com4370aed2012-01-18 16:21:08 +0000529}
530
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000531void SkDeferredCanvas::init() {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000532 fDeferredDrawing = true; // On by default
junov@google.com4370aed2012-01-18 16:21:08 +0000533}
534
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000535void SkDeferredCanvas::setMaxRecordingStorage(size_t maxStorage) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000536 this->validate();
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000537 this->getDeferredDevice()->setMaxRecordingStorage(maxStorage);
538}
539
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000540size_t SkDeferredCanvas::storageAllocatedForRecording() const {
541 return this->getDeferredDevice()->storageAllocatedForRecording();
542}
543
544size_t SkDeferredCanvas::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000545 return this->getDeferredDevice()->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000546}
547
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000548void SkDeferredCanvas::recordedDrawCommand() {
549 if (fDeferredDrawing) {
550 this->getDeferredDevice()->recordedDrawCommand();
551 }
552}
553
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000554void SkDeferredCanvas::validate() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000555 SkASSERT(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000556}
557
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000558SkCanvas* SkDeferredCanvas::drawingCanvas() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000559 this->validate();
560 return fDeferredDrawing ? this->getDeferredDevice()->recordingCanvas() :
561 this->getDeferredDevice()->immediateCanvas();
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000562}
563
junov@chromium.org88e29142012-08-07 16:48:22 +0000564SkCanvas* SkDeferredCanvas::immediateCanvas() const {
565 this->validate();
566 return this->getDeferredDevice()->immediateCanvas();
567}
568
569DeferredDevice* SkDeferredCanvas::getDeferredDevice() const {
570 return static_cast<DeferredDevice*>(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000571}
572
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000573void SkDeferredCanvas::setDeferredDrawing(bool val) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000574 this->validate(); // Must set device before calling this method
junov@google.com4370aed2012-01-18 16:21:08 +0000575 if (val != fDeferredDrawing) {
576 if (fDeferredDrawing) {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000577 // Going live.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000578 this->getDeferredDevice()->flushPendingCommands(kNormal_PlaybackMode);
junov@google.com4370aed2012-01-18 16:21:08 +0000579 }
580 fDeferredDrawing = val;
581 }
582}
583
junov@chromium.org88e29142012-08-07 16:48:22 +0000584bool SkDeferredCanvas::isDeferredDrawing() const {
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +0000585 return fDeferredDrawing;
586}
587
junov@chromium.org88e29142012-08-07 16:48:22 +0000588bool SkDeferredCanvas::isFreshFrame() const {
589 return this->getDeferredDevice()->isFreshFrame();
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.orgc16ca922012-02-24 22:06:27 +0000601SkDevice* SkDeferredCanvas::setDevice(SkDevice* device) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000602 this->INHERITED::setDevice(SkNEW_ARGS(DeferredDevice, (device)))->unref();
junov@google.com4370aed2012-01-18 16:21:08 +0000603 return device;
604}
605
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000606SkDeferredCanvas::NotificationClient* SkDeferredCanvas::setNotificationClient(
607 NotificationClient* notificationClient) {
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000608
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000609 DeferredDevice* deferredDevice = this->getDeferredDevice();
junov@google.com4370aed2012-01-18 16:21:08 +0000610 SkASSERT(deferredDevice);
611 if (deferredDevice) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000612 deferredDevice->setNotificationClient(notificationClient);
junov@google.com4370aed2012-01-18 16:21:08 +0000613 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000614 return notificationClient;
junov@google.com4370aed2012-01-18 16:21:08 +0000615}
616
617bool SkDeferredCanvas::isFullFrame(const SkRect* rect,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000618 const SkPaint* paint) const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000619 SkCanvas* canvas = this->drawingCanvas();
620 SkISize canvasSize = this->getDeviceSize();
junov@google.com4370aed2012-01-18 16:21:08 +0000621 if (rect) {
622 if (!canvas->getTotalMatrix().rectStaysRect()) {
623 return false; // conservative
624 }
625
626 SkRect transformedRect;
627 canvas->getTotalMatrix().mapRect(&transformedRect, *rect);
628
629 if (paint) {
630 SkPaint::Style paintStyle = paint->getStyle();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000631 if (!(paintStyle == SkPaint::kFill_Style ||
junov@google.com4370aed2012-01-18 16:21:08 +0000632 paintStyle == SkPaint::kStrokeAndFill_Style)) {
633 return false;
634 }
635 if (paint->getMaskFilter() || paint->getLooper()
636 || paint->getPathEffect() || paint->getImageFilter()) {
637 return false; // conservative
638 }
639 }
640
641 // The following test holds with AA enabled, and is conservative
642 // by a 0.5 pixel margin with AA disabled
rmistry@google.comd6176b02012-08-23 18:14:13 +0000643 if (transformedRect.fLeft > SkIntToScalar(0) ||
644 transformedRect.fTop > SkIntToScalar(0) ||
junov@chromium.orgb1e218e2012-02-13 22:27:58 +0000645 transformedRect.fRight < SkIntToScalar(canvasSize.fWidth) ||
646 transformedRect.fBottom < SkIntToScalar(canvasSize.fHeight)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000647 return false;
648 }
649 }
650
651 switch (canvas->getClipType()) {
652 case SkCanvas::kRect_ClipType :
653 {
654 SkIRect bounds;
655 canvas->getClipDeviceBounds(&bounds);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000656 if (bounds.fLeft > 0 || bounds.fTop > 0 ||
657 bounds.fRight < canvasSize.fWidth ||
junov@google.com4370aed2012-01-18 16:21:08 +0000658 bounds.fBottom < canvasSize.fHeight)
659 return false;
660 }
661 break;
662 case SkCanvas::kComplex_ClipType :
663 return false; // conservative
664 case SkCanvas::kEmpty_ClipType:
665 default:
666 break;
667 };
668
669 return true;
670}
671
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000672int SkDeferredCanvas::save(SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000673 this->drawingCanvas()->save(flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000674 int val = this->INHERITED::save(flags);
675 this->recordedDrawCommand();
676
677 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000678}
679
680int SkDeferredCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000681 SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000682 this->drawingCanvas()->saveLayer(bounds, paint, flags);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000683 int count = this->INHERITED::save(flags);
684 this->clipRectBounds(bounds, flags, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000685 this->recordedDrawCommand();
686
junov@chromium.orga907ac32012-02-24 21:54:07 +0000687 return count;
junov@google.com4370aed2012-01-18 16:21:08 +0000688}
689
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000690void SkDeferredCanvas::restore() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000691 this->drawingCanvas()->restore();
junov@chromium.orga907ac32012-02-24 21:54:07 +0000692 this->INHERITED::restore();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000693 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000694}
695
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000696bool SkDeferredCanvas::isDrawingToLayer() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000697 return this->drawingCanvas()->isDrawingToLayer();
junov@google.com4370aed2012-01-18 16:21:08 +0000698}
699
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000700bool SkDeferredCanvas::translate(SkScalar dx, SkScalar dy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000701 this->drawingCanvas()->translate(dx, dy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000702 bool val = this->INHERITED::translate(dx, dy);
703 this->recordedDrawCommand();
704 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000705}
706
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000707bool SkDeferredCanvas::scale(SkScalar sx, SkScalar sy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000708 this->drawingCanvas()->scale(sx, sy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000709 bool val = this->INHERITED::scale(sx, sy);
710 this->recordedDrawCommand();
711 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000712}
713
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000714bool SkDeferredCanvas::rotate(SkScalar degrees) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000715 this->drawingCanvas()->rotate(degrees);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000716 bool val = this->INHERITED::rotate(degrees);
717 this->recordedDrawCommand();
718 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000719}
720
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000721bool SkDeferredCanvas::skew(SkScalar sx, SkScalar sy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000722 this->drawingCanvas()->skew(sx, sy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000723 bool val = this->INHERITED::skew(sx, sy);
724 this->recordedDrawCommand();
725 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000726}
727
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000728bool SkDeferredCanvas::concat(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000729 this->drawingCanvas()->concat(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000730 bool val = this->INHERITED::concat(matrix);
731 this->recordedDrawCommand();
732 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000733}
734
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000735void SkDeferredCanvas::setMatrix(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000736 this->drawingCanvas()->setMatrix(matrix);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000737 this->INHERITED::setMatrix(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000738 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000739}
740
741bool SkDeferredCanvas::clipRect(const SkRect& rect,
742 SkRegion::Op op,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000743 bool doAntiAlias) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000744 this->drawingCanvas()->clipRect(rect, op, doAntiAlias);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000745 bool val = this->INHERITED::clipRect(rect, op, doAntiAlias);
746 this->recordedDrawCommand();
747 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000748}
749
750bool SkDeferredCanvas::clipPath(const SkPath& path,
751 SkRegion::Op op,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000752 bool doAntiAlias) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000753 this->drawingCanvas()->clipPath(path, op, doAntiAlias);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000754 bool val = this->INHERITED::clipPath(path, op, doAntiAlias);
755 this->recordedDrawCommand();
756 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000757}
758
759bool SkDeferredCanvas::clipRegion(const SkRegion& deviceRgn,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000760 SkRegion::Op op) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000761 this->drawingCanvas()->clipRegion(deviceRgn, op);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000762 bool val = this->INHERITED::clipRegion(deviceRgn, op);
763 this->recordedDrawCommand();
764 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000765}
766
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000767void SkDeferredCanvas::clear(SkColor color) {
junov@google.com4370aed2012-01-18 16:21:08 +0000768 // purge pending commands
769 if (fDeferredDrawing) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000770 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000771 }
772
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000773 this->drawingCanvas()->clear(color);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000774 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000775}
776
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000777void SkDeferredCanvas::drawPaint(const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000778 if (fDeferredDrawing && this->isFullFrame(NULL, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000779 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000780 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000781 }
junov@chromium.org10f7f972012-07-31 21:01:51 +0000782 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000783 this->drawingCanvas()->drawPaint(paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000784 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000785}
786
787void SkDeferredCanvas::drawPoints(PointMode mode, size_t count,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000788 const SkPoint pts[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000789 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000790 this->drawingCanvas()->drawPoints(mode, count, pts, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000791 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000792}
793
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000794void SkDeferredCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000795 if (fDeferredDrawing && this->isFullFrame(&rect, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000796 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000797 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000798 }
799
junov@chromium.org10f7f972012-07-31 21:01:51 +0000800 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000801 this->drawingCanvas()->drawRect(rect, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000802 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000803}
804
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000805void SkDeferredCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000806 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000807 this->drawingCanvas()->drawPath(path, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000808 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000809}
810
811void SkDeferredCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000812 SkScalar top, const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000813 SkRect bitmapRect = SkRect::MakeXYWH(left, top,
814 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000815 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000816 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000817 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000818 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000819 }
820
junov@chromium.org10f7f972012-07-31 21:01:51 +0000821 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000822 this->drawingCanvas()->drawBitmap(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000823 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000824}
825
reed@google.com71121732012-09-18 15:14:33 +0000826void SkDeferredCanvas::drawBitmapRectToRect(const SkBitmap& bitmap,
827 const SkRect* src,
828 const SkRect& dst,
829 const SkPaint* paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000830 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000831 this->isFullFrame(&dst, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000832 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000833 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000834 }
835
junov@chromium.org10f7f972012-07-31 21:01:51 +0000836 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
reed@google.com71121732012-09-18 15:14:33 +0000837 this->drawingCanvas()->drawBitmapRectToRect(bitmap, src, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000838 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000839}
840
841
842void SkDeferredCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
843 const SkMatrix& m,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000844 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000845 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
846 // covers canvas entirely and transformed bitmap covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000847 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000848 this->drawingCanvas()->drawBitmapMatrix(bitmap, m, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000849 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000850}
851
852void SkDeferredCanvas::drawBitmapNine(const SkBitmap& bitmap,
853 const SkIRect& center, const SkRect& dst,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000854 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000855 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
856 // covers canvas entirely and dst covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000857 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000858 this->drawingCanvas()->drawBitmapNine(bitmap, center, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000859 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000860}
861
862void SkDeferredCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000863 const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000864 SkRect bitmapRect = SkRect::MakeXYWH(
865 SkIntToScalar(left),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000866 SkIntToScalar(top),
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000867 SkIntToScalar(bitmap.width()),
868 SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000869 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000870 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000871 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000872 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000873 }
874
junov@chromium.org10f7f972012-07-31 21:01:51 +0000875 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000876 this->drawingCanvas()->drawSprite(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000877 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000878}
879
880void SkDeferredCanvas::drawText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000881 SkScalar x, SkScalar y, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000882 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000883 this->drawingCanvas()->drawText(text, byteLength, x, y, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000884 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000885}
886
887void SkDeferredCanvas::drawPosText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000888 const SkPoint pos[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000889 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000890 this->drawingCanvas()->drawPosText(text, byteLength, pos, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000891 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000892}
893
894void SkDeferredCanvas::drawPosTextH(const void* text, size_t byteLength,
895 const SkScalar xpos[], SkScalar constY,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000896 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000897 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000898 this->drawingCanvas()->drawPosTextH(text, byteLength, xpos, constY, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000899 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000900}
901
902void SkDeferredCanvas::drawTextOnPath(const void* text, size_t byteLength,
903 const SkPath& path,
904 const SkMatrix* matrix,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000905 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000906 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000907 this->drawingCanvas()->drawTextOnPath(text, byteLength, path, matrix, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000908 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000909}
910
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000911void SkDeferredCanvas::drawPicture(SkPicture& picture) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000912 this->drawingCanvas()->drawPicture(picture);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000913 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000914}
915
916void SkDeferredCanvas::drawVertices(VertexMode vmode, int vertexCount,
917 const SkPoint vertices[],
918 const SkPoint texs[],
919 const SkColor colors[], SkXfermode* xmode,
920 const uint16_t indices[], int indexCount,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000921 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000922 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000923 this->drawingCanvas()->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode,
924 indices, indexCount, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000925 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000926}
927
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000928SkBounder* SkDeferredCanvas::setBounder(SkBounder* bounder) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000929 this->drawingCanvas()->setBounder(bounder);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000930 this->INHERITED::setBounder(bounder);
931 this->recordedDrawCommand();
932 return bounder;
junov@google.com4370aed2012-01-18 16:21:08 +0000933}
934
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000935SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000936 this->drawingCanvas()->setDrawFilter(filter);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000937 this->INHERITED::setDrawFilter(filter);
938 this->recordedDrawCommand();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000939 return filter;
junov@google.com4370aed2012-01-18 16:21:08 +0000940}
941
942SkCanvas* SkDeferredCanvas::canvasForDrawIter() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000943 return this->drawingCanvas();
junov@google.com4370aed2012-01-18 16:21:08 +0000944}