blob: c3de3932900f27f6cf2b748b7aa3e0e13de209c6 [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.orga38dfb62012-09-20 22:10:33 +0000160 bool hasPendingCommands() const { return fAllocator.blockCount() != 0; }
junov@chromium.org88e29142012-08-07 16:48:22 +0000161 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();
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000240 bool hasPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000241 size_t storageAllocatedForRecording() const;
242 size_t freeMemoryIfPossible(size_t bytesToFree);
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000243 void flushPendingCommands(PlaybackMode);
junov@chromium.org0a67f962012-09-19 22:48:34 +0000244 void skipPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000245 void setMaxRecordingStorage(size_t);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000246 void recordedDrawCommand();
junov@chromium.org88e29142012-08-07 16:48:22 +0000247
248 virtual uint32_t getDeviceCapabilities() SK_OVERRIDE;
249 virtual int width() const SK_OVERRIDE;
250 virtual int height() const SK_OVERRIDE;
251 virtual SkGpuRenderTarget* accessRenderTarget() SK_OVERRIDE;
252
253 virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
254 int width, int height,
255 bool isOpaque,
256 Usage usage) SK_OVERRIDE;
257
258 virtual void writePixels(const SkBitmap& bitmap, int x, int y,
259 SkCanvas::Config8888 config8888) SK_OVERRIDE;
260
261protected:
262 virtual const SkBitmap& onAccessBitmap(SkBitmap*) SK_OVERRIDE;
263 virtual bool onReadPixels(const SkBitmap& bitmap,
264 int x, int y,
265 SkCanvas::Config8888 config8888) SK_OVERRIDE;
266
267 // The following methods are no-ops on a deferred device
268 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*)
269 SK_OVERRIDE
270 {return false;}
junov@chromium.org88e29142012-08-07 16:48:22 +0000271
272 // None of the following drawing methods should ever get called on the
273 // deferred device
274 virtual void clear(SkColor color)
275 {SkASSERT(0);}
276 virtual void drawPaint(const SkDraw&, const SkPaint& paint)
277 {SkASSERT(0);}
278 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
279 size_t count, const SkPoint[],
280 const SkPaint& paint)
281 {SkASSERT(0);}
282 virtual void drawRect(const SkDraw&, const SkRect& r,
283 const SkPaint& paint)
284 {SkASSERT(0);}
285 virtual void drawPath(const SkDraw&, const SkPath& path,
286 const SkPaint& paint,
287 const SkMatrix* prePathMatrix = NULL,
288 bool pathIsMutable = false)
289 {SkASSERT(0);}
290 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
291 const SkIRect* srcRectOrNull,
292 const SkMatrix& matrix, const SkPaint& paint)
293 {SkASSERT(0);}
294 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
295 int x, int y, const SkPaint& paint)
296 {SkASSERT(0);}
297 virtual void drawText(const SkDraw&, const void* text, size_t len,
298 SkScalar x, SkScalar y, const SkPaint& paint)
299 {SkASSERT(0);}
300 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
301 const SkScalar pos[], SkScalar constY,
302 int scalarsPerPos, const SkPaint& paint)
303 {SkASSERT(0);}
304 virtual void drawTextOnPath(const SkDraw&, const void* text,
305 size_t len, const SkPath& path,
306 const SkMatrix* matrix,
307 const SkPaint& paint)
308 {SkASSERT(0);}
309 virtual void drawPosTextOnPath(const SkDraw& draw, const void* text,
310 size_t len, const SkPoint pos[],
311 const SkPaint& paint,
312 const SkPath& path,
313 const SkMatrix* matrix)
314 {SkASSERT(0);}
315 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
316 int vertexCount, const SkPoint verts[],
317 const SkPoint texs[], const SkColor colors[],
318 SkXfermode* xmode, const uint16_t indices[],
319 int indexCount, const SkPaint& paint)
320 {SkASSERT(0);}
321 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
322 const SkPaint&)
323 {SkASSERT(0);}
324private:
325 virtual void flush();
326
junov@chromium.org88e29142012-08-07 16:48:22 +0000327 void beginRecording();
328
329 DeferredPipeController fPipeController;
330 SkGPipeWriter fPipeWriter;
331 SkDevice* fImmediateDevice;
332 SkCanvas* fImmediateCanvas;
333 SkCanvas* fRecordingCanvas;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000334 SkDeferredCanvas::NotificationClient* fNotificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000335 bool fFreshFrame;
336 size_t fMaxRecordingStorageBytes;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000337 size_t fPreviousStorageAllocated;
junov@chromium.org88e29142012-08-07 16:48:22 +0000338};
339
340DeferredDevice::DeferredDevice(
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000341 SkDevice* immediateDevice, SkDeferredCanvas::NotificationClient* notificationClient) :
junov@chromium.org88e29142012-08-07 16:48:22 +0000342 SkDevice(SkBitmap::kNo_Config, immediateDevice->width(),
343 immediateDevice->height(), immediateDevice->isOpaque())
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000344 , fRecordingCanvas(NULL)
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000345 , fFreshFrame(true)
346 , fPreviousStorageAllocated(0){
junov@chromium.org88e29142012-08-07 16:48:22 +0000347
348 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000349 fNotificationClient = notificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000350 fImmediateDevice = immediateDevice; // ref counted via fImmediateCanvas
351 fImmediateCanvas = SkNEW_ARGS(SkCanvas, (fImmediateDevice));
352 fPipeController.setPlaybackCanvas(fImmediateCanvas);
353 this->beginRecording();
354}
355
356DeferredDevice::~DeferredDevice() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000357 this->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000358 SkSafeUnref(fImmediateCanvas);
junov@chromium.org88e29142012-08-07 16:48:22 +0000359}
360
361void DeferredDevice::setMaxRecordingStorage(size_t maxStorage) {
362 fMaxRecordingStorageBytes = maxStorage;
363 this->recordingCanvas(); // Accessing the recording canvas applies the new limit.
364}
365
junov@chromium.org88e29142012-08-07 16:48:22 +0000366void DeferredDevice::beginRecording() {
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000367 SkASSERT(NULL == fRecordingCanvas);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000368 fRecordingCanvas = fPipeWriter.startRecording(&fPipeController, 0,
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000369 fImmediateDevice->width(), fImmediateDevice->height());
junov@chromium.org88e29142012-08-07 16:48:22 +0000370}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000371
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000372void DeferredDevice::setNotificationClient(
373 SkDeferredCanvas::NotificationClient* notificationClient) {
junov@chromium.org52805482012-08-20 14:25:04 +0000374 fNotificationClient = notificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000375}
376
junov@chromium.org0a67f962012-09-19 22:48:34 +0000377void DeferredDevice::skipPendingCommands() {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000378 if (!fRecordingCanvas->isDrawingToLayer() && fPipeController.hasPendingCommands()) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000379 fFreshFrame = true;
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000380 flushPendingCommands(kSilent_PlaybackMode);
junov@google.com52a00ca2012-10-01 15:27:14 +0000381 if (fNotificationClient) {
382 fNotificationClient->skippedPendingDrawCommands();
383 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000384 }
385}
386
387bool DeferredDevice::isFreshFrame() {
388 bool ret = fFreshFrame;
389 fFreshFrame = false;
390 return ret;
391}
392
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000393bool DeferredDevice::hasPendingCommands() {
394 return fPipeController.hasPendingCommands();
395}
396
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000397void DeferredDevice::flushPendingCommands(PlaybackMode playbackMode) {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000398 if (!fPipeController.hasPendingCommands()) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000399 return;
400 }
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000401 if (playbackMode == kNormal_PlaybackMode && fNotificationClient) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000402 fNotificationClient->prepareForDraw();
junov@chromium.org88e29142012-08-07 16:48:22 +0000403 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000404 fPipeWriter.flushRecording(true);
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000405 fPipeController.playback(playbackMode);
406 if (playbackMode == kNormal_PlaybackMode && fNotificationClient) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000407 fNotificationClient->flushedDrawCommands();
408 }
409 fPreviousStorageAllocated = storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000410}
411
412void DeferredDevice::flush() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000413 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000414 fImmediateCanvas->flush();
415}
416
417size_t DeferredDevice::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000418 size_t val = fPipeWriter.freeMemoryIfPossible(bytesToFree);
419 fPreviousStorageAllocated = storageAllocatedForRecording();
420 return val;
junov@chromium.org88e29142012-08-07 16:48:22 +0000421}
422
423size_t DeferredDevice::storageAllocatedForRecording() const {
424 return (fPipeController.storageAllocatedForRecording()
425 + fPipeWriter.storageAllocatedForRecording());
426}
427
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000428void DeferredDevice::recordedDrawCommand() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000429 size_t storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000430
junov@chromium.org88e29142012-08-07 16:48:22 +0000431 if (storageAllocated > fMaxRecordingStorageBytes) {
432 // First, attempt to reduce cache without flushing
433 size_t tryFree = storageAllocated - fMaxRecordingStorageBytes;
434 if (this->freeMemoryIfPossible(tryFree) < tryFree) {
435 // Flush is necessary to free more space.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000436 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000437 // Free as much as possible to avoid oscillating around fMaxRecordingStorageBytes
438 // which could cause a high flushing frequency.
bsalomon@google.com100abf42012-09-05 17:40:04 +0000439 this->freeMemoryIfPossible(~0U);
junov@chromium.org88e29142012-08-07 16:48:22 +0000440 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000441 storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000442 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000443
rmistry@google.comd6176b02012-08-23 18:14:13 +0000444 if (fNotificationClient &&
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000445 storageAllocated != fPreviousStorageAllocated) {
446 fPreviousStorageAllocated = storageAllocated;
447 fNotificationClient->storageAllocatedForRecordingChanged(storageAllocated);
448 }
449}
450
451SkCanvas* DeferredDevice::recordingCanvas() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000452 return fRecordingCanvas;
453}
454
rmistry@google.comd6176b02012-08-23 18:14:13 +0000455uint32_t DeferredDevice::getDeviceCapabilities() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000456 return fImmediateDevice->getDeviceCapabilities();
457}
458
rmistry@google.comd6176b02012-08-23 18:14:13 +0000459int DeferredDevice::width() const {
junov@chromium.org88e29142012-08-07 16:48:22 +0000460 return fImmediateDevice->width();
461}
462
463int DeferredDevice::height() const {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000464 return fImmediateDevice->height();
junov@chromium.org88e29142012-08-07 16:48:22 +0000465}
466
467SkGpuRenderTarget* DeferredDevice::accessRenderTarget() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000468 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000469 return fImmediateDevice->accessRenderTarget();
470}
471
472void DeferredDevice::writePixels(const SkBitmap& bitmap,
473 int x, int y, SkCanvas::Config8888 config8888) {
474
475 if (x <= 0 && y <= 0 && (x + bitmap.width()) >= width() &&
476 (y + bitmap.height()) >= height()) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000477 this->skipPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000478 }
479
480 if (SkBitmap::kARGB_8888_Config == bitmap.config() &&
481 SkCanvas::kNative_Premul_Config8888 != config8888 &&
482 kPMColorAlias != config8888) {
483 //Special case config: no deferral
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000484 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000485 fImmediateDevice->writePixels(bitmap, x, y, config8888);
486 return;
487 }
488
489 SkPaint paint;
490 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
491 if (shouldDrawImmediately(&bitmap, NULL)) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000492 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000493 fImmediateCanvas->drawSprite(bitmap, x, y, &paint);
494 } else {
495 this->recordingCanvas()->drawSprite(bitmap, x, y, &paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000496 this->recordedDrawCommand();
497
junov@chromium.org88e29142012-08-07 16:48:22 +0000498 }
499}
500
501const SkBitmap& DeferredDevice::onAccessBitmap(SkBitmap*) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000502 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000503 return fImmediateDevice->accessBitmap(false);
504}
505
506SkDevice* DeferredDevice::onCreateCompatibleDevice(
507 SkBitmap::Config config, int width, int height, bool isOpaque,
508 Usage usage) {
509
510 // Save layer usage not supported, and not required by SkDeferredCanvas.
511 SkASSERT(usage != kSaveLayer_Usage);
512 // Create a compatible non-deferred device.
513 SkAutoTUnref<SkDevice> compatibleDevice
514 (fImmediateDevice->createCompatibleDevice(config, width, height,
515 isOpaque));
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000516 return SkNEW_ARGS(DeferredDevice, (compatibleDevice, fNotificationClient));
junov@chromium.org88e29142012-08-07 16:48:22 +0000517}
518
519bool DeferredDevice::onReadPixels(
520 const SkBitmap& bitmap, int x, int y, SkCanvas::Config8888 config8888) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000521 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000522 return fImmediateCanvas->readPixels(const_cast<SkBitmap*>(&bitmap),
523 x, y, config8888);
524}
525
526
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000527SkDeferredCanvas::SkDeferredCanvas() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000528 this->init();
junov@google.com4370aed2012-01-18 16:21:08 +0000529}
530
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000531SkDeferredCanvas::SkDeferredCanvas(SkDevice* device) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000532 this->init();
533 this->setDevice(device);
junov@google.com4370aed2012-01-18 16:21:08 +0000534}
535
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000536void SkDeferredCanvas::init() {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000537 fDeferredDrawing = true; // On by default
junov@google.com4370aed2012-01-18 16:21:08 +0000538}
539
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000540void SkDeferredCanvas::setMaxRecordingStorage(size_t maxStorage) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000541 this->validate();
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000542 this->getDeferredDevice()->setMaxRecordingStorage(maxStorage);
543}
544
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000545size_t SkDeferredCanvas::storageAllocatedForRecording() const {
546 return this->getDeferredDevice()->storageAllocatedForRecording();
547}
548
549size_t SkDeferredCanvas::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000550 return this->getDeferredDevice()->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000551}
552
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000553void SkDeferredCanvas::recordedDrawCommand() {
554 if (fDeferredDrawing) {
555 this->getDeferredDevice()->recordedDrawCommand();
556 }
557}
558
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000559void SkDeferredCanvas::validate() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000560 SkASSERT(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000561}
562
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000563SkCanvas* SkDeferredCanvas::drawingCanvas() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000564 this->validate();
565 return fDeferredDrawing ? this->getDeferredDevice()->recordingCanvas() :
566 this->getDeferredDevice()->immediateCanvas();
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000567}
568
junov@chromium.org88e29142012-08-07 16:48:22 +0000569SkCanvas* SkDeferredCanvas::immediateCanvas() const {
570 this->validate();
571 return this->getDeferredDevice()->immediateCanvas();
572}
573
574DeferredDevice* SkDeferredCanvas::getDeferredDevice() const {
575 return static_cast<DeferredDevice*>(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000576}
577
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000578void SkDeferredCanvas::setDeferredDrawing(bool val) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000579 this->validate(); // Must set device before calling this method
junov@google.com4370aed2012-01-18 16:21:08 +0000580 if (val != fDeferredDrawing) {
581 if (fDeferredDrawing) {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000582 // Going live.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000583 this->getDeferredDevice()->flushPendingCommands(kNormal_PlaybackMode);
junov@google.com4370aed2012-01-18 16:21:08 +0000584 }
585 fDeferredDrawing = val;
586 }
587}
588
junov@chromium.org88e29142012-08-07 16:48:22 +0000589bool SkDeferredCanvas::isDeferredDrawing() const {
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +0000590 return fDeferredDrawing;
591}
592
junov@chromium.org88e29142012-08-07 16:48:22 +0000593bool SkDeferredCanvas::isFreshFrame() const {
594 return this->getDeferredDevice()->isFreshFrame();
595}
596
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000597bool SkDeferredCanvas::hasPendingCommands() const {
598 return this->getDeferredDevice()->hasPendingCommands();
599}
600
junov@chromium.orgfb103892012-09-20 19:35:43 +0000601void SkDeferredCanvas::silentFlush() {
602 if (fDeferredDrawing) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000603 this->getDeferredDevice()->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000604 }
605}
606
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000607SkDeferredCanvas::~SkDeferredCanvas() {
junov@google.com4370aed2012-01-18 16:21:08 +0000608}
609
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000610SkDevice* SkDeferredCanvas::setDevice(SkDevice* device) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000611 this->INHERITED::setDevice(SkNEW_ARGS(DeferredDevice, (device)))->unref();
junov@google.com4370aed2012-01-18 16:21:08 +0000612 return device;
613}
614
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000615SkDeferredCanvas::NotificationClient* SkDeferredCanvas::setNotificationClient(
616 NotificationClient* notificationClient) {
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000617
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000618 DeferredDevice* deferredDevice = this->getDeferredDevice();
junov@google.com4370aed2012-01-18 16:21:08 +0000619 SkASSERT(deferredDevice);
620 if (deferredDevice) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000621 deferredDevice->setNotificationClient(notificationClient);
junov@google.com4370aed2012-01-18 16:21:08 +0000622 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000623 return notificationClient;
junov@google.com4370aed2012-01-18 16:21:08 +0000624}
625
626bool SkDeferredCanvas::isFullFrame(const SkRect* rect,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000627 const SkPaint* paint) const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000628 SkCanvas* canvas = this->drawingCanvas();
629 SkISize canvasSize = this->getDeviceSize();
junov@google.com4370aed2012-01-18 16:21:08 +0000630 if (rect) {
631 if (!canvas->getTotalMatrix().rectStaysRect()) {
632 return false; // conservative
633 }
634
635 SkRect transformedRect;
636 canvas->getTotalMatrix().mapRect(&transformedRect, *rect);
637
638 if (paint) {
639 SkPaint::Style paintStyle = paint->getStyle();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000640 if (!(paintStyle == SkPaint::kFill_Style ||
junov@google.com4370aed2012-01-18 16:21:08 +0000641 paintStyle == SkPaint::kStrokeAndFill_Style)) {
642 return false;
643 }
644 if (paint->getMaskFilter() || paint->getLooper()
645 || paint->getPathEffect() || paint->getImageFilter()) {
646 return false; // conservative
647 }
648 }
649
650 // The following test holds with AA enabled, and is conservative
651 // by a 0.5 pixel margin with AA disabled
rmistry@google.comd6176b02012-08-23 18:14:13 +0000652 if (transformedRect.fLeft > SkIntToScalar(0) ||
653 transformedRect.fTop > SkIntToScalar(0) ||
junov@chromium.orgb1e218e2012-02-13 22:27:58 +0000654 transformedRect.fRight < SkIntToScalar(canvasSize.fWidth) ||
655 transformedRect.fBottom < SkIntToScalar(canvasSize.fHeight)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000656 return false;
657 }
658 }
659
660 switch (canvas->getClipType()) {
661 case SkCanvas::kRect_ClipType :
662 {
663 SkIRect bounds;
664 canvas->getClipDeviceBounds(&bounds);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000665 if (bounds.fLeft > 0 || bounds.fTop > 0 ||
666 bounds.fRight < canvasSize.fWidth ||
junov@google.com4370aed2012-01-18 16:21:08 +0000667 bounds.fBottom < canvasSize.fHeight)
668 return false;
669 }
670 break;
671 case SkCanvas::kComplex_ClipType :
672 return false; // conservative
673 case SkCanvas::kEmpty_ClipType:
674 default:
675 break;
676 };
677
678 return true;
679}
680
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000681int SkDeferredCanvas::save(SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000682 this->drawingCanvas()->save(flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000683 int val = this->INHERITED::save(flags);
684 this->recordedDrawCommand();
685
686 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000687}
688
689int SkDeferredCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000690 SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000691 this->drawingCanvas()->saveLayer(bounds, paint, flags);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000692 int count = this->INHERITED::save(flags);
693 this->clipRectBounds(bounds, flags, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000694 this->recordedDrawCommand();
695
junov@chromium.orga907ac32012-02-24 21:54:07 +0000696 return count;
junov@google.com4370aed2012-01-18 16:21:08 +0000697}
698
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000699void SkDeferredCanvas::restore() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000700 this->drawingCanvas()->restore();
junov@chromium.orga907ac32012-02-24 21:54:07 +0000701 this->INHERITED::restore();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000702 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000703}
704
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000705bool SkDeferredCanvas::isDrawingToLayer() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000706 return this->drawingCanvas()->isDrawingToLayer();
junov@google.com4370aed2012-01-18 16:21:08 +0000707}
708
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000709bool SkDeferredCanvas::translate(SkScalar dx, SkScalar dy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000710 this->drawingCanvas()->translate(dx, dy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000711 bool val = this->INHERITED::translate(dx, dy);
712 this->recordedDrawCommand();
713 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000714}
715
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000716bool SkDeferredCanvas::scale(SkScalar sx, SkScalar sy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000717 this->drawingCanvas()->scale(sx, sy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000718 bool val = this->INHERITED::scale(sx, sy);
719 this->recordedDrawCommand();
720 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000721}
722
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000723bool SkDeferredCanvas::rotate(SkScalar degrees) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000724 this->drawingCanvas()->rotate(degrees);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000725 bool val = this->INHERITED::rotate(degrees);
726 this->recordedDrawCommand();
727 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000728}
729
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000730bool SkDeferredCanvas::skew(SkScalar sx, SkScalar sy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000731 this->drawingCanvas()->skew(sx, sy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000732 bool val = this->INHERITED::skew(sx, sy);
733 this->recordedDrawCommand();
734 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000735}
736
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000737bool SkDeferredCanvas::concat(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000738 this->drawingCanvas()->concat(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000739 bool val = this->INHERITED::concat(matrix);
740 this->recordedDrawCommand();
741 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000742}
743
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000744void SkDeferredCanvas::setMatrix(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000745 this->drawingCanvas()->setMatrix(matrix);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000746 this->INHERITED::setMatrix(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000747 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000748}
749
750bool SkDeferredCanvas::clipRect(const SkRect& rect,
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()->clipRect(rect, op, doAntiAlias);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000754 bool val = this->INHERITED::clipRect(rect, op, doAntiAlias);
755 this->recordedDrawCommand();
756 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000757}
758
759bool SkDeferredCanvas::clipPath(const SkPath& path,
760 SkRegion::Op op,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000761 bool doAntiAlias) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000762 this->drawingCanvas()->clipPath(path, op, doAntiAlias);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000763 bool val = this->INHERITED::clipPath(path, op, doAntiAlias);
764 this->recordedDrawCommand();
765 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000766}
767
768bool SkDeferredCanvas::clipRegion(const SkRegion& deviceRgn,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000769 SkRegion::Op op) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000770 this->drawingCanvas()->clipRegion(deviceRgn, op);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000771 bool val = this->INHERITED::clipRegion(deviceRgn, op);
772 this->recordedDrawCommand();
773 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000774}
775
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000776void SkDeferredCanvas::clear(SkColor color) {
junov@google.com4370aed2012-01-18 16:21:08 +0000777 // purge pending commands
778 if (fDeferredDrawing) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000779 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000780 }
781
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000782 this->drawingCanvas()->clear(color);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000783 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000784}
785
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000786void SkDeferredCanvas::drawPaint(const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000787 if (fDeferredDrawing && this->isFullFrame(NULL, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000788 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000789 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000790 }
junov@chromium.org10f7f972012-07-31 21:01:51 +0000791 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000792 this->drawingCanvas()->drawPaint(paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000793 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000794}
795
796void SkDeferredCanvas::drawPoints(PointMode mode, size_t count,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000797 const SkPoint pts[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000798 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000799 this->drawingCanvas()->drawPoints(mode, count, pts, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000800 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000801}
802
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000803void SkDeferredCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000804 if (fDeferredDrawing && this->isFullFrame(&rect, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000805 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000806 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000807 }
808
junov@chromium.org10f7f972012-07-31 21:01:51 +0000809 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000810 this->drawingCanvas()->drawRect(rect, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000811 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000812}
813
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000814void SkDeferredCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000815 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000816 this->drawingCanvas()->drawPath(path, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000817 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000818}
819
820void SkDeferredCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000821 SkScalar top, const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000822 SkRect bitmapRect = SkRect::MakeXYWH(left, top,
823 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000824 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000825 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000826 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000827 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000828 }
829
junov@chromium.org10f7f972012-07-31 21:01:51 +0000830 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000831 this->drawingCanvas()->drawBitmap(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000832 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000833}
834
reed@google.com71121732012-09-18 15:14:33 +0000835void SkDeferredCanvas::drawBitmapRectToRect(const SkBitmap& bitmap,
836 const SkRect* src,
837 const SkRect& dst,
838 const SkPaint* paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000839 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000840 this->isFullFrame(&dst, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000841 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000842 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000843 }
844
junov@chromium.org10f7f972012-07-31 21:01:51 +0000845 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
reed@google.com71121732012-09-18 15:14:33 +0000846 this->drawingCanvas()->drawBitmapRectToRect(bitmap, src, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000847 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000848}
849
850
851void SkDeferredCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
852 const SkMatrix& m,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000853 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000854 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
855 // covers canvas entirely and transformed bitmap covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000856 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000857 this->drawingCanvas()->drawBitmapMatrix(bitmap, m, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000858 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000859}
860
861void SkDeferredCanvas::drawBitmapNine(const SkBitmap& bitmap,
862 const SkIRect& center, const SkRect& dst,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000863 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000864 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
865 // covers canvas entirely and dst covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000866 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000867 this->drawingCanvas()->drawBitmapNine(bitmap, center, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000868 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000869}
870
871void SkDeferredCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000872 const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000873 SkRect bitmapRect = SkRect::MakeXYWH(
874 SkIntToScalar(left),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000875 SkIntToScalar(top),
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000876 SkIntToScalar(bitmap.width()),
877 SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000878 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000879 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000880 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000881 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000882 }
883
junov@chromium.org10f7f972012-07-31 21:01:51 +0000884 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000885 this->drawingCanvas()->drawSprite(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000886 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000887}
888
889void SkDeferredCanvas::drawText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000890 SkScalar x, SkScalar y, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000891 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000892 this->drawingCanvas()->drawText(text, byteLength, x, y, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000893 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000894}
895
896void SkDeferredCanvas::drawPosText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000897 const SkPoint pos[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000898 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000899 this->drawingCanvas()->drawPosText(text, byteLength, pos, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000900 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000901}
902
903void SkDeferredCanvas::drawPosTextH(const void* text, size_t byteLength,
904 const SkScalar xpos[], SkScalar constY,
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()->drawPosTextH(text, byteLength, xpos, constY, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000908 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000909}
910
911void SkDeferredCanvas::drawTextOnPath(const void* text, size_t byteLength,
912 const SkPath& path,
913 const SkMatrix* matrix,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000914 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000915 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000916 this->drawingCanvas()->drawTextOnPath(text, byteLength, path, matrix, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000917 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000918}
919
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000920void SkDeferredCanvas::drawPicture(SkPicture& picture) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000921 this->drawingCanvas()->drawPicture(picture);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000922 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000923}
924
925void SkDeferredCanvas::drawVertices(VertexMode vmode, int vertexCount,
926 const SkPoint vertices[],
927 const SkPoint texs[],
928 const SkColor colors[], SkXfermode* xmode,
929 const uint16_t indices[], int indexCount,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000930 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000931 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000932 this->drawingCanvas()->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode,
933 indices, indexCount, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000934 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000935}
936
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000937SkBounder* SkDeferredCanvas::setBounder(SkBounder* bounder) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000938 this->drawingCanvas()->setBounder(bounder);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000939 this->INHERITED::setBounder(bounder);
940 this->recordedDrawCommand();
941 return bounder;
junov@google.com4370aed2012-01-18 16:21:08 +0000942}
943
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000944SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000945 this->drawingCanvas()->setDrawFilter(filter);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000946 this->INHERITED::setDrawFilter(filter);
947 this->recordedDrawCommand();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000948 return filter;
junov@google.com4370aed2012-01-18 16:21:08 +0000949}
950
951SkCanvas* SkDeferredCanvas::canvasForDrawIter() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000952 return this->drawingCanvas();
junov@google.com4370aed2012-01-18 16:21:08 +0000953}