blob: eaba50342eeb7299c211470c421c83caa81bd780 [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;}
271 virtual void setMatrixClip(const SkMatrix&, const SkRegion&,
272 const SkClipStack&) SK_OVERRIDE
273 {}
274
275 // None of the following drawing methods should ever get called on the
276 // deferred device
277 virtual void clear(SkColor color)
278 {SkASSERT(0);}
279 virtual void drawPaint(const SkDraw&, const SkPaint& paint)
280 {SkASSERT(0);}
281 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
282 size_t count, const SkPoint[],
283 const SkPaint& paint)
284 {SkASSERT(0);}
285 virtual void drawRect(const SkDraw&, const SkRect& r,
286 const SkPaint& paint)
287 {SkASSERT(0);}
288 virtual void drawPath(const SkDraw&, const SkPath& path,
289 const SkPaint& paint,
290 const SkMatrix* prePathMatrix = NULL,
291 bool pathIsMutable = false)
292 {SkASSERT(0);}
293 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
294 const SkIRect* srcRectOrNull,
295 const SkMatrix& matrix, const SkPaint& paint)
296 {SkASSERT(0);}
297 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
298 int x, int y, const SkPaint& paint)
299 {SkASSERT(0);}
300 virtual void drawText(const SkDraw&, const void* text, size_t len,
301 SkScalar x, SkScalar y, const SkPaint& paint)
302 {SkASSERT(0);}
303 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
304 const SkScalar pos[], SkScalar constY,
305 int scalarsPerPos, const SkPaint& paint)
306 {SkASSERT(0);}
307 virtual void drawTextOnPath(const SkDraw&, const void* text,
308 size_t len, const SkPath& path,
309 const SkMatrix* matrix,
310 const SkPaint& paint)
311 {SkASSERT(0);}
312 virtual void drawPosTextOnPath(const SkDraw& draw, const void* text,
313 size_t len, const SkPoint pos[],
314 const SkPaint& paint,
315 const SkPath& path,
316 const SkMatrix* matrix)
317 {SkASSERT(0);}
318 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
319 int vertexCount, const SkPoint verts[],
320 const SkPoint texs[], const SkColor colors[],
321 SkXfermode* xmode, const uint16_t indices[],
322 int indexCount, const SkPaint& paint)
323 {SkASSERT(0);}
324 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
325 const SkPaint&)
326 {SkASSERT(0);}
327private:
328 virtual void flush();
329
junov@chromium.org88e29142012-08-07 16:48:22 +0000330 void beginRecording();
331
332 DeferredPipeController fPipeController;
333 SkGPipeWriter fPipeWriter;
334 SkDevice* fImmediateDevice;
335 SkCanvas* fImmediateCanvas;
336 SkCanvas* fRecordingCanvas;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000337 SkDeferredCanvas::NotificationClient* fNotificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000338 bool fFreshFrame;
339 size_t fMaxRecordingStorageBytes;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000340 size_t fPreviousStorageAllocated;
junov@chromium.org88e29142012-08-07 16:48:22 +0000341};
342
343DeferredDevice::DeferredDevice(
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000344 SkDevice* immediateDevice, SkDeferredCanvas::NotificationClient* notificationClient) :
junov@chromium.org88e29142012-08-07 16:48:22 +0000345 SkDevice(SkBitmap::kNo_Config, immediateDevice->width(),
346 immediateDevice->height(), immediateDevice->isOpaque())
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000347 , fRecordingCanvas(NULL)
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000348 , fFreshFrame(true)
349 , fPreviousStorageAllocated(0){
junov@chromium.org88e29142012-08-07 16:48:22 +0000350
351 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000352 fNotificationClient = notificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000353 fImmediateDevice = immediateDevice; // ref counted via fImmediateCanvas
354 fImmediateCanvas = SkNEW_ARGS(SkCanvas, (fImmediateDevice));
355 fPipeController.setPlaybackCanvas(fImmediateCanvas);
356 this->beginRecording();
357}
358
359DeferredDevice::~DeferredDevice() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000360 this->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000361 SkSafeUnref(fImmediateCanvas);
junov@chromium.org88e29142012-08-07 16:48:22 +0000362}
363
364void DeferredDevice::setMaxRecordingStorage(size_t maxStorage) {
365 fMaxRecordingStorageBytes = maxStorage;
366 this->recordingCanvas(); // Accessing the recording canvas applies the new limit.
367}
368
junov@chromium.org88e29142012-08-07 16:48:22 +0000369void DeferredDevice::beginRecording() {
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000370 SkASSERT(NULL == fRecordingCanvas);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000371 fRecordingCanvas = fPipeWriter.startRecording(&fPipeController, 0,
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000372 fImmediateDevice->width(), fImmediateDevice->height());
junov@chromium.org88e29142012-08-07 16:48:22 +0000373}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000374
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000375void DeferredDevice::setNotificationClient(
376 SkDeferredCanvas::NotificationClient* notificationClient) {
junov@chromium.org52805482012-08-20 14:25:04 +0000377 fNotificationClient = notificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000378}
379
junov@chromium.org0a67f962012-09-19 22:48:34 +0000380void DeferredDevice::skipPendingCommands() {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000381 if (!fRecordingCanvas->isDrawingToLayer() && fPipeController.hasPendingCommands()) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000382 fFreshFrame = true;
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000383 flushPendingCommands(kSilent_PlaybackMode);
junov@google.com52a00ca2012-10-01 15:27:14 +0000384 if (fNotificationClient) {
385 fNotificationClient->skippedPendingDrawCommands();
386 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000387 }
388}
389
390bool DeferredDevice::isFreshFrame() {
391 bool ret = fFreshFrame;
392 fFreshFrame = false;
393 return ret;
394}
395
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000396bool DeferredDevice::hasPendingCommands() {
397 return fPipeController.hasPendingCommands();
398}
399
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000400void DeferredDevice::flushPendingCommands(PlaybackMode playbackMode) {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000401 if (!fPipeController.hasPendingCommands()) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000402 return;
403 }
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000404 if (playbackMode == kNormal_PlaybackMode && fNotificationClient) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000405 fNotificationClient->prepareForDraw();
junov@chromium.org88e29142012-08-07 16:48:22 +0000406 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000407 fPipeWriter.flushRecording(true);
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000408 fPipeController.playback(playbackMode);
409 if (playbackMode == kNormal_PlaybackMode && fNotificationClient) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000410 fNotificationClient->flushedDrawCommands();
411 }
412 fPreviousStorageAllocated = storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000413}
414
415void DeferredDevice::flush() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000416 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000417 fImmediateCanvas->flush();
418}
419
420size_t DeferredDevice::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000421 size_t val = fPipeWriter.freeMemoryIfPossible(bytesToFree);
422 fPreviousStorageAllocated = storageAllocatedForRecording();
423 return val;
junov@chromium.org88e29142012-08-07 16:48:22 +0000424}
425
426size_t DeferredDevice::storageAllocatedForRecording() const {
427 return (fPipeController.storageAllocatedForRecording()
428 + fPipeWriter.storageAllocatedForRecording());
429}
430
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000431void DeferredDevice::recordedDrawCommand() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000432 size_t storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000433
junov@chromium.org88e29142012-08-07 16:48:22 +0000434 if (storageAllocated > fMaxRecordingStorageBytes) {
435 // First, attempt to reduce cache without flushing
436 size_t tryFree = storageAllocated - fMaxRecordingStorageBytes;
437 if (this->freeMemoryIfPossible(tryFree) < tryFree) {
438 // Flush is necessary to free more space.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000439 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000440 // Free as much as possible to avoid oscillating around fMaxRecordingStorageBytes
441 // which could cause a high flushing frequency.
bsalomon@google.com100abf42012-09-05 17:40:04 +0000442 this->freeMemoryIfPossible(~0U);
junov@chromium.org88e29142012-08-07 16:48:22 +0000443 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000444 storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000445 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000446
rmistry@google.comd6176b02012-08-23 18:14:13 +0000447 if (fNotificationClient &&
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000448 storageAllocated != fPreviousStorageAllocated) {
449 fPreviousStorageAllocated = storageAllocated;
450 fNotificationClient->storageAllocatedForRecordingChanged(storageAllocated);
451 }
452}
453
454SkCanvas* DeferredDevice::recordingCanvas() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000455 return fRecordingCanvas;
456}
457
rmistry@google.comd6176b02012-08-23 18:14:13 +0000458uint32_t DeferredDevice::getDeviceCapabilities() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000459 return fImmediateDevice->getDeviceCapabilities();
460}
461
rmistry@google.comd6176b02012-08-23 18:14:13 +0000462int DeferredDevice::width() const {
junov@chromium.org88e29142012-08-07 16:48:22 +0000463 return fImmediateDevice->width();
464}
465
466int DeferredDevice::height() const {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000467 return fImmediateDevice->height();
junov@chromium.org88e29142012-08-07 16:48:22 +0000468}
469
470SkGpuRenderTarget* DeferredDevice::accessRenderTarget() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000471 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000472 return fImmediateDevice->accessRenderTarget();
473}
474
475void DeferredDevice::writePixels(const SkBitmap& bitmap,
476 int x, int y, SkCanvas::Config8888 config8888) {
477
478 if (x <= 0 && y <= 0 && (x + bitmap.width()) >= width() &&
479 (y + bitmap.height()) >= height()) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000480 this->skipPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000481 }
482
483 if (SkBitmap::kARGB_8888_Config == bitmap.config() &&
484 SkCanvas::kNative_Premul_Config8888 != config8888 &&
485 kPMColorAlias != config8888) {
486 //Special case config: no deferral
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000487 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000488 fImmediateDevice->writePixels(bitmap, x, y, config8888);
489 return;
490 }
491
492 SkPaint paint;
493 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
494 if (shouldDrawImmediately(&bitmap, NULL)) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000495 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000496 fImmediateCanvas->drawSprite(bitmap, x, y, &paint);
497 } else {
498 this->recordingCanvas()->drawSprite(bitmap, x, y, &paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000499 this->recordedDrawCommand();
500
junov@chromium.org88e29142012-08-07 16:48:22 +0000501 }
502}
503
504const SkBitmap& DeferredDevice::onAccessBitmap(SkBitmap*) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000505 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000506 return fImmediateDevice->accessBitmap(false);
507}
508
509SkDevice* DeferredDevice::onCreateCompatibleDevice(
510 SkBitmap::Config config, int width, int height, bool isOpaque,
511 Usage usage) {
512
513 // Save layer usage not supported, and not required by SkDeferredCanvas.
514 SkASSERT(usage != kSaveLayer_Usage);
515 // Create a compatible non-deferred device.
516 SkAutoTUnref<SkDevice> compatibleDevice
517 (fImmediateDevice->createCompatibleDevice(config, width, height,
518 isOpaque));
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000519 return SkNEW_ARGS(DeferredDevice, (compatibleDevice, fNotificationClient));
junov@chromium.org88e29142012-08-07 16:48:22 +0000520}
521
522bool DeferredDevice::onReadPixels(
523 const SkBitmap& bitmap, int x, int y, SkCanvas::Config8888 config8888) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000524 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000525 return fImmediateCanvas->readPixels(const_cast<SkBitmap*>(&bitmap),
526 x, y, config8888);
527}
528
529
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000530SkDeferredCanvas::SkDeferredCanvas() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000531 this->init();
junov@google.com4370aed2012-01-18 16:21:08 +0000532}
533
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000534SkDeferredCanvas::SkDeferredCanvas(SkDevice* device) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000535 this->init();
536 this->setDevice(device);
junov@google.com4370aed2012-01-18 16:21:08 +0000537}
538
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000539void SkDeferredCanvas::init() {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000540 fDeferredDrawing = true; // On by default
junov@google.com4370aed2012-01-18 16:21:08 +0000541}
542
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000543void SkDeferredCanvas::setMaxRecordingStorage(size_t maxStorage) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000544 this->validate();
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000545 this->getDeferredDevice()->setMaxRecordingStorage(maxStorage);
546}
547
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000548size_t SkDeferredCanvas::storageAllocatedForRecording() const {
549 return this->getDeferredDevice()->storageAllocatedForRecording();
550}
551
552size_t SkDeferredCanvas::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000553 return this->getDeferredDevice()->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000554}
555
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000556void SkDeferredCanvas::recordedDrawCommand() {
557 if (fDeferredDrawing) {
558 this->getDeferredDevice()->recordedDrawCommand();
559 }
560}
561
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000562void SkDeferredCanvas::validate() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000563 SkASSERT(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000564}
565
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000566SkCanvas* SkDeferredCanvas::drawingCanvas() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000567 this->validate();
568 return fDeferredDrawing ? this->getDeferredDevice()->recordingCanvas() :
569 this->getDeferredDevice()->immediateCanvas();
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000570}
571
junov@chromium.org88e29142012-08-07 16:48:22 +0000572SkCanvas* SkDeferredCanvas::immediateCanvas() const {
573 this->validate();
574 return this->getDeferredDevice()->immediateCanvas();
575}
576
577DeferredDevice* SkDeferredCanvas::getDeferredDevice() const {
578 return static_cast<DeferredDevice*>(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000579}
580
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000581void SkDeferredCanvas::setDeferredDrawing(bool val) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000582 this->validate(); // Must set device before calling this method
junov@google.com4370aed2012-01-18 16:21:08 +0000583 if (val != fDeferredDrawing) {
584 if (fDeferredDrawing) {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000585 // Going live.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000586 this->getDeferredDevice()->flushPendingCommands(kNormal_PlaybackMode);
junov@google.com4370aed2012-01-18 16:21:08 +0000587 }
588 fDeferredDrawing = val;
589 }
590}
591
junov@chromium.org88e29142012-08-07 16:48:22 +0000592bool SkDeferredCanvas::isDeferredDrawing() const {
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +0000593 return fDeferredDrawing;
594}
595
junov@chromium.org88e29142012-08-07 16:48:22 +0000596bool SkDeferredCanvas::isFreshFrame() const {
597 return this->getDeferredDevice()->isFreshFrame();
598}
599
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000600bool SkDeferredCanvas::hasPendingCommands() const {
601 return this->getDeferredDevice()->hasPendingCommands();
602}
603
junov@chromium.orgfb103892012-09-20 19:35:43 +0000604void SkDeferredCanvas::silentFlush() {
605 if (fDeferredDrawing) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000606 this->getDeferredDevice()->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000607 }
608}
609
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000610SkDeferredCanvas::~SkDeferredCanvas() {
junov@google.com4370aed2012-01-18 16:21:08 +0000611}
612
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000613SkDevice* SkDeferredCanvas::setDevice(SkDevice* device) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000614 this->INHERITED::setDevice(SkNEW_ARGS(DeferredDevice, (device)))->unref();
junov@google.com4370aed2012-01-18 16:21:08 +0000615 return device;
616}
617
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000618SkDeferredCanvas::NotificationClient* SkDeferredCanvas::setNotificationClient(
619 NotificationClient* notificationClient) {
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000620
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000621 DeferredDevice* deferredDevice = this->getDeferredDevice();
junov@google.com4370aed2012-01-18 16:21:08 +0000622 SkASSERT(deferredDevice);
623 if (deferredDevice) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000624 deferredDevice->setNotificationClient(notificationClient);
junov@google.com4370aed2012-01-18 16:21:08 +0000625 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000626 return notificationClient;
junov@google.com4370aed2012-01-18 16:21:08 +0000627}
628
629bool SkDeferredCanvas::isFullFrame(const SkRect* rect,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000630 const SkPaint* paint) const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000631 SkCanvas* canvas = this->drawingCanvas();
632 SkISize canvasSize = this->getDeviceSize();
junov@google.com4370aed2012-01-18 16:21:08 +0000633 if (rect) {
634 if (!canvas->getTotalMatrix().rectStaysRect()) {
635 return false; // conservative
636 }
637
638 SkRect transformedRect;
639 canvas->getTotalMatrix().mapRect(&transformedRect, *rect);
640
641 if (paint) {
642 SkPaint::Style paintStyle = paint->getStyle();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000643 if (!(paintStyle == SkPaint::kFill_Style ||
junov@google.com4370aed2012-01-18 16:21:08 +0000644 paintStyle == SkPaint::kStrokeAndFill_Style)) {
645 return false;
646 }
647 if (paint->getMaskFilter() || paint->getLooper()
648 || paint->getPathEffect() || paint->getImageFilter()) {
649 return false; // conservative
650 }
651 }
652
653 // The following test holds with AA enabled, and is conservative
654 // by a 0.5 pixel margin with AA disabled
rmistry@google.comd6176b02012-08-23 18:14:13 +0000655 if (transformedRect.fLeft > SkIntToScalar(0) ||
656 transformedRect.fTop > SkIntToScalar(0) ||
junov@chromium.orgb1e218e2012-02-13 22:27:58 +0000657 transformedRect.fRight < SkIntToScalar(canvasSize.fWidth) ||
658 transformedRect.fBottom < SkIntToScalar(canvasSize.fHeight)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000659 return false;
660 }
661 }
662
663 switch (canvas->getClipType()) {
664 case SkCanvas::kRect_ClipType :
665 {
666 SkIRect bounds;
667 canvas->getClipDeviceBounds(&bounds);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000668 if (bounds.fLeft > 0 || bounds.fTop > 0 ||
669 bounds.fRight < canvasSize.fWidth ||
junov@google.com4370aed2012-01-18 16:21:08 +0000670 bounds.fBottom < canvasSize.fHeight)
671 return false;
672 }
673 break;
674 case SkCanvas::kComplex_ClipType :
675 return false; // conservative
676 case SkCanvas::kEmpty_ClipType:
677 default:
678 break;
679 };
680
681 return true;
682}
683
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000684int SkDeferredCanvas::save(SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000685 this->drawingCanvas()->save(flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000686 int val = this->INHERITED::save(flags);
687 this->recordedDrawCommand();
688
689 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000690}
691
692int SkDeferredCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000693 SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000694 this->drawingCanvas()->saveLayer(bounds, paint, flags);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000695 int count = this->INHERITED::save(flags);
696 this->clipRectBounds(bounds, flags, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000697 this->recordedDrawCommand();
698
junov@chromium.orga907ac32012-02-24 21:54:07 +0000699 return count;
junov@google.com4370aed2012-01-18 16:21:08 +0000700}
701
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000702void SkDeferredCanvas::restore() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000703 this->drawingCanvas()->restore();
junov@chromium.orga907ac32012-02-24 21:54:07 +0000704 this->INHERITED::restore();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000705 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000706}
707
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000708bool SkDeferredCanvas::isDrawingToLayer() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000709 return this->drawingCanvas()->isDrawingToLayer();
junov@google.com4370aed2012-01-18 16:21:08 +0000710}
711
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000712bool SkDeferredCanvas::translate(SkScalar dx, SkScalar dy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000713 this->drawingCanvas()->translate(dx, dy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000714 bool val = this->INHERITED::translate(dx, dy);
715 this->recordedDrawCommand();
716 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000717}
718
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000719bool SkDeferredCanvas::scale(SkScalar sx, SkScalar sy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000720 this->drawingCanvas()->scale(sx, sy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000721 bool val = this->INHERITED::scale(sx, sy);
722 this->recordedDrawCommand();
723 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000724}
725
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000726bool SkDeferredCanvas::rotate(SkScalar degrees) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000727 this->drawingCanvas()->rotate(degrees);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000728 bool val = this->INHERITED::rotate(degrees);
729 this->recordedDrawCommand();
730 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000731}
732
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000733bool SkDeferredCanvas::skew(SkScalar sx, SkScalar sy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000734 this->drawingCanvas()->skew(sx, sy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000735 bool val = this->INHERITED::skew(sx, sy);
736 this->recordedDrawCommand();
737 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000738}
739
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000740bool SkDeferredCanvas::concat(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000741 this->drawingCanvas()->concat(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000742 bool val = this->INHERITED::concat(matrix);
743 this->recordedDrawCommand();
744 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000745}
746
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000747void SkDeferredCanvas::setMatrix(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000748 this->drawingCanvas()->setMatrix(matrix);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000749 this->INHERITED::setMatrix(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000750 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000751}
752
753bool SkDeferredCanvas::clipRect(const SkRect& rect,
754 SkRegion::Op op,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000755 bool doAntiAlias) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000756 this->drawingCanvas()->clipRect(rect, op, doAntiAlias);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000757 bool val = this->INHERITED::clipRect(rect, op, doAntiAlias);
758 this->recordedDrawCommand();
759 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000760}
761
762bool SkDeferredCanvas::clipPath(const SkPath& path,
763 SkRegion::Op op,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000764 bool doAntiAlias) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000765 this->drawingCanvas()->clipPath(path, op, doAntiAlias);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000766 bool val = this->INHERITED::clipPath(path, op, doAntiAlias);
767 this->recordedDrawCommand();
768 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000769}
770
771bool SkDeferredCanvas::clipRegion(const SkRegion& deviceRgn,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000772 SkRegion::Op op) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000773 this->drawingCanvas()->clipRegion(deviceRgn, op);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000774 bool val = this->INHERITED::clipRegion(deviceRgn, op);
775 this->recordedDrawCommand();
776 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000777}
778
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000779void SkDeferredCanvas::clear(SkColor color) {
junov@google.com4370aed2012-01-18 16:21:08 +0000780 // purge pending commands
781 if (fDeferredDrawing) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000782 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000783 }
784
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000785 this->drawingCanvas()->clear(color);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000786 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000787}
788
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000789void SkDeferredCanvas::drawPaint(const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000790 if (fDeferredDrawing && this->isFullFrame(NULL, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000791 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000792 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000793 }
junov@chromium.org10f7f972012-07-31 21:01:51 +0000794 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000795 this->drawingCanvas()->drawPaint(paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000796 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000797}
798
799void SkDeferredCanvas::drawPoints(PointMode mode, size_t count,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000800 const SkPoint pts[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000801 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000802 this->drawingCanvas()->drawPoints(mode, count, pts, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000803 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000804}
805
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000806void SkDeferredCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000807 if (fDeferredDrawing && this->isFullFrame(&rect, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000808 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000809 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000810 }
811
junov@chromium.org10f7f972012-07-31 21:01:51 +0000812 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000813 this->drawingCanvas()->drawRect(rect, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000814 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000815}
816
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000817void SkDeferredCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000818 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000819 this->drawingCanvas()->drawPath(path, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000820 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000821}
822
823void SkDeferredCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000824 SkScalar top, const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000825 SkRect bitmapRect = SkRect::MakeXYWH(left, top,
826 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000827 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000828 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000829 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000830 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000831 }
832
junov@chromium.org10f7f972012-07-31 21:01:51 +0000833 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000834 this->drawingCanvas()->drawBitmap(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000835 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000836}
837
reed@google.com71121732012-09-18 15:14:33 +0000838void SkDeferredCanvas::drawBitmapRectToRect(const SkBitmap& bitmap,
839 const SkRect* src,
840 const SkRect& dst,
841 const SkPaint* paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000842 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000843 this->isFullFrame(&dst, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000844 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000845 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000846 }
847
junov@chromium.org10f7f972012-07-31 21:01:51 +0000848 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
reed@google.com71121732012-09-18 15:14:33 +0000849 this->drawingCanvas()->drawBitmapRectToRect(bitmap, src, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000850 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000851}
852
853
854void SkDeferredCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
855 const SkMatrix& m,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000856 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000857 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
858 // covers canvas entirely and transformed bitmap covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000859 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000860 this->drawingCanvas()->drawBitmapMatrix(bitmap, m, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000861 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000862}
863
864void SkDeferredCanvas::drawBitmapNine(const SkBitmap& bitmap,
865 const SkIRect& center, const SkRect& dst,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000866 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000867 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
868 // covers canvas entirely and dst covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000869 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000870 this->drawingCanvas()->drawBitmapNine(bitmap, center, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000871 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000872}
873
874void SkDeferredCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000875 const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000876 SkRect bitmapRect = SkRect::MakeXYWH(
877 SkIntToScalar(left),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000878 SkIntToScalar(top),
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000879 SkIntToScalar(bitmap.width()),
880 SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000881 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000882 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000883 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000884 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000885 }
886
junov@chromium.org10f7f972012-07-31 21:01:51 +0000887 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000888 this->drawingCanvas()->drawSprite(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000889 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000890}
891
892void SkDeferredCanvas::drawText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000893 SkScalar x, SkScalar y, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000894 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000895 this->drawingCanvas()->drawText(text, byteLength, x, y, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000896 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000897}
898
899void SkDeferredCanvas::drawPosText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000900 const SkPoint pos[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000901 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000902 this->drawingCanvas()->drawPosText(text, byteLength, pos, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000903 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000904}
905
906void SkDeferredCanvas::drawPosTextH(const void* text, size_t byteLength,
907 const SkScalar xpos[], SkScalar constY,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000908 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000909 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000910 this->drawingCanvas()->drawPosTextH(text, byteLength, xpos, constY, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000911 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000912}
913
914void SkDeferredCanvas::drawTextOnPath(const void* text, size_t byteLength,
915 const SkPath& path,
916 const SkMatrix* matrix,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000917 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000918 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000919 this->drawingCanvas()->drawTextOnPath(text, byteLength, path, matrix, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000920 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000921}
922
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000923void SkDeferredCanvas::drawPicture(SkPicture& picture) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000924 this->drawingCanvas()->drawPicture(picture);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000925 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000926}
927
928void SkDeferredCanvas::drawVertices(VertexMode vmode, int vertexCount,
929 const SkPoint vertices[],
930 const SkPoint texs[],
931 const SkColor colors[], SkXfermode* xmode,
932 const uint16_t indices[], int indexCount,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000933 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000934 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000935 this->drawingCanvas()->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode,
936 indices, indexCount, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000937 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000938}
939
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000940SkBounder* SkDeferredCanvas::setBounder(SkBounder* bounder) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000941 this->drawingCanvas()->setBounder(bounder);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000942 this->INHERITED::setBounder(bounder);
943 this->recordedDrawCommand();
944 return bounder;
junov@google.com4370aed2012-01-18 16:21:08 +0000945}
946
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000947SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000948 this->drawingCanvas()->setDrawFilter(filter);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000949 this->INHERITED::setDrawFilter(filter);
950 this->recordedDrawCommand();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000951 return filter;
junov@google.com4370aed2012-01-18 16:21:08 +0000952}
953
954SkCanvas* SkDeferredCanvas::canvasForDrawIter() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000955 return this->drawingCanvas();
junov@google.com4370aed2012-01-18 16:21:08 +0000956}