blob: 7ea3bbbe309a2d0751a8c966cb9394bc6f8e6d31 [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,
sugoi@google.com5347de12012-11-21 16:44:45 +000022 kDeferredCanvasBitmapSizeThreshold = ~0, // Disables this feature
junov@chromium.orgbfeddae2012-07-23 13:35:14 +000023};
24
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +000025enum PlaybackMode {
26 kNormal_PlaybackMode,
27 kSilent_PlaybackMode,
28};
29
junov@google.com4370aed2012-01-18 16:21:08 +000030namespace {
sugoi@google.com7775fd52012-11-21 15:47:04 +000031bool shouldDrawImmediately(const SkBitmap* bitmap, const SkPaint* paint,
32 size_t bitmapSizeThreshold) {
33 if (bitmap && ((bitmap->getTexture() && !bitmap->isImmutable()) ||
34 (bitmap->getSize() > bitmapSizeThreshold))) {
junov@chromium.org10f7f972012-07-31 21:01:51 +000035 return true;
36 }
37 if (paint) {
38 SkShader* shader = paint->getShader();
39 // Here we detect the case where the shader is an SkBitmapProcShader
40 // with a gpu texture attached. Checking this without RTTI
41 // requires making the assumption that only gradient shaders
42 // and SkBitmapProcShader implement asABitmap(). The following
43 // code may need to be revised if that assumption is ever broken.
44 if (shader && !shader->asAGradient(NULL)) {
45 SkBitmap bm;
rmistry@google.comd6176b02012-08-23 18:14:13 +000046 if (shader->asABitmap(&bm, NULL, NULL) &&
junov@chromium.org10f7f972012-07-31 21:01:51 +000047 NULL != bm.getTexture()) {
48 return true;
49 }
50 }
51 }
52 return false;
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +000053}
54}
55
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +000056namespace {
junov@google.com4370aed2012-01-18 16:21:08 +000057
rmistry@google.comd6176b02012-08-23 18:14:13 +000058bool isPaintOpaque(const SkPaint* paint,
junov@chromium.orgc16ca922012-02-24 22:06:27 +000059 const SkBitmap* bmpReplacesShader = NULL) {
junov@google.com4370aed2012-01-18 16:21:08 +000060 // TODO: SkXfermode should have a virtual isOpaque method, which would
61 // make it possible to test modes that do not have a Coeff representation.
junov@chromium.org87f982c2012-02-23 21:34:34 +000062
63 if (!paint) {
64 return bmpReplacesShader ? bmpReplacesShader->isOpaque() : true;
65 }
66
junov@google.com4370aed2012-01-18 16:21:08 +000067 SkXfermode::Coeff srcCoeff, dstCoeff;
junov@chromium.org87f982c2012-02-23 21:34:34 +000068 if (SkXfermode::AsCoeff(paint->getXfermode(), &srcCoeff, &dstCoeff)){
junov@chromium.org8cef67a2012-10-11 20:19:15 +000069 if (SkXfermode::kDA_Coeff == srcCoeff || SkXfermode::kDC_Coeff == srcCoeff ||
70 SkXfermode::kIDA_Coeff == srcCoeff || SkXfermode::kIDC_Coeff == srcCoeff) {
71 return false;
72 }
junov@google.com4370aed2012-01-18 16:21:08 +000073 switch (dstCoeff) {
74 case SkXfermode::kZero_Coeff:
75 return true;
76 case SkXfermode::kISA_Coeff:
junov@chromium.org87f982c2012-02-23 21:34:34 +000077 if (paint->getAlpha() != 255) {
junov@google.com4370aed2012-01-18 16:21:08 +000078 break;
79 }
80 if (bmpReplacesShader) {
81 if (!bmpReplacesShader->isOpaque()) {
82 break;
83 }
junov@chromium.org87f982c2012-02-23 21:34:34 +000084 } else if (paint->getShader() && !paint->getShader()->isOpaque()) {
junov@google.com4370aed2012-01-18 16:21:08 +000085 break;
86 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000087 if (paint->getColorFilter() &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +000088 ((paint->getColorFilter()->getFlags() &
junov@google.com4370aed2012-01-18 16:21:08 +000089 SkColorFilter::kAlphaUnchanged_Flag) == 0)) {
90 break;
91 }
92 return true;
93 case SkXfermode::kSA_Coeff:
junov@chromium.org87f982c2012-02-23 21:34:34 +000094 if (paint->getAlpha() != 0) {
junov@google.com4370aed2012-01-18 16:21:08 +000095 break;
96 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000097 if (paint->getColorFilter() &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +000098 ((paint->getColorFilter()->getFlags() &
junov@google.com4370aed2012-01-18 16:21:08 +000099 SkColorFilter::kAlphaUnchanged_Flag) == 0)) {
100 break;
101 }
102 return true;
103 case SkXfermode::kSC_Coeff:
junov@chromium.org87f982c2012-02-23 21:34:34 +0000104 if (paint->getColor() != 0) { // all components must be 0
junov@google.com4370aed2012-01-18 16:21:08 +0000105 break;
106 }
junov@chromium.org87f982c2012-02-23 21:34:34 +0000107 if (bmpReplacesShader || paint->getShader()) {
junov@google.com4370aed2012-01-18 16:21:08 +0000108 break;
109 }
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000110 if (paint->getColorFilter() && (
111 (paint->getColorFilter()->getFlags() &
junov@google.com4370aed2012-01-18 16:21:08 +0000112 SkColorFilter::kAlphaUnchanged_Flag) == 0)) {
113 break;
114 }
115 return true;
116 default:
117 break;
118 }
119 }
120 return false;
121}
122
123} // unnamed namespace
124
junov@chromium.org88e29142012-08-07 16:48:22 +0000125//-----------------------------------------------------------------------------
126// DeferredPipeController
127//-----------------------------------------------------------------------------
128
129class DeferredPipeController : public SkGPipeController {
130public:
131 DeferredPipeController();
132 void setPlaybackCanvas(SkCanvas*);
133 virtual ~DeferredPipeController();
134 virtual void* requestBlock(size_t minRequest, size_t* actual) SK_OVERRIDE;
135 virtual void notifyWritten(size_t bytes) SK_OVERRIDE;
junov@chromium.orgfb103892012-09-20 19:35:43 +0000136 void playback(bool silent);
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000137 bool hasPendingCommands() const { return fAllocator.blockCount() != 0; }
junov@chromium.org88e29142012-08-07 16:48:22 +0000138 size_t storageAllocatedForRecording() const { return fAllocator.totalCapacity(); }
139private:
140 enum {
141 kMinBlockSize = 4096
142 };
143 struct PipeBlock {
144 PipeBlock(void* block, size_t size) { fBlock = block, fSize = size; }
145 void* fBlock;
146 size_t fSize;
147 };
148 void* fBlock;
149 size_t fBytesWritten;
150 SkChunkAlloc fAllocator;
151 SkTDArray<PipeBlock> fBlockList;
152 SkGPipeReader fReader;
153};
154
155DeferredPipeController::DeferredPipeController() :
156 fAllocator(kMinBlockSize) {
157 fBlock = NULL;
158 fBytesWritten = 0;
159}
160
161DeferredPipeController::~DeferredPipeController() {
162 fAllocator.reset();
163}
164
165void DeferredPipeController::setPlaybackCanvas(SkCanvas* canvas) {
166 fReader.setCanvas(canvas);
167}
168
169void* DeferredPipeController::requestBlock(size_t minRequest, size_t *actual) {
170 if (fBlock) {
171 // Save the previous block for later
172 PipeBlock previousBloc(fBlock, fBytesWritten);
173 fBlockList.push(previousBloc);
174 }
175 int32_t blockSize = SkMax32(minRequest, kMinBlockSize);
176 fBlock = fAllocator.allocThrow(blockSize);
177 fBytesWritten = 0;
178 *actual = blockSize;
179 return fBlock;
180}
181
182void DeferredPipeController::notifyWritten(size_t bytes) {
183 fBytesWritten += bytes;
184}
185
junov@chromium.orgfb103892012-09-20 19:35:43 +0000186void DeferredPipeController::playback(bool silent) {
187 uint32_t flags = silent ? SkGPipeReader::kSilent_PlaybackFlag : 0;
junov@chromium.org88e29142012-08-07 16:48:22 +0000188 for (int currentBlock = 0; currentBlock < fBlockList.count(); currentBlock++ ) {
junov@chromium.orgfb103892012-09-20 19:35:43 +0000189 fReader.playback(fBlockList[currentBlock].fBlock, fBlockList[currentBlock].fSize,
190 flags);
junov@chromium.org88e29142012-08-07 16:48:22 +0000191 }
192 fBlockList.reset();
193
194 if (fBlock) {
junov@chromium.orgfb103892012-09-20 19:35:43 +0000195 fReader.playback(fBlock, fBytesWritten, flags);
junov@chromium.org88e29142012-08-07 16:48:22 +0000196 fBlock = NULL;
197 }
198
199 // Release all allocated blocks
200 fAllocator.reset();
201}
202
junov@chromium.org88e29142012-08-07 16:48:22 +0000203//-----------------------------------------------------------------------------
204// DeferredDevice
205//-----------------------------------------------------------------------------
junov@chromium.org88e29142012-08-07 16:48:22 +0000206class DeferredDevice : public SkDevice {
207public:
208 DeferredDevice(SkDevice* immediateDevice,
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000209 SkDeferredCanvas::NotificationClient* notificationClient = NULL);
junov@chromium.org88e29142012-08-07 16:48:22 +0000210 ~DeferredDevice();
211
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000212 void setNotificationClient(SkDeferredCanvas::NotificationClient* notificationClient);
junov@chromium.org88e29142012-08-07 16:48:22 +0000213 SkCanvas* recordingCanvas();
214 SkCanvas* immediateCanvas() const {return fImmediateCanvas;}
215 SkDevice* immediateDevice() const {return fImmediateDevice;}
216 bool isFreshFrame();
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000217 bool hasPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000218 size_t storageAllocatedForRecording() const;
219 size_t freeMemoryIfPossible(size_t bytesToFree);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000220 size_t getBitmapSizeThreshold() const;
221 void setBitmapSizeThreshold(size_t sizeThreshold);
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000222 void flushPendingCommands(PlaybackMode);
junov@chromium.org0a67f962012-09-19 22:48:34 +0000223 void skipPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000224 void setMaxRecordingStorage(size_t);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000225 void recordedDrawCommand();
junov@chromium.org88e29142012-08-07 16:48:22 +0000226
227 virtual uint32_t getDeviceCapabilities() SK_OVERRIDE;
228 virtual int width() const SK_OVERRIDE;
229 virtual int height() const SK_OVERRIDE;
230 virtual SkGpuRenderTarget* accessRenderTarget() SK_OVERRIDE;
231
232 virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
233 int width, int height,
234 bool isOpaque,
235 Usage usage) SK_OVERRIDE;
236
237 virtual void writePixels(const SkBitmap& bitmap, int x, int y,
238 SkCanvas::Config8888 config8888) SK_OVERRIDE;
239
240protected:
241 virtual const SkBitmap& onAccessBitmap(SkBitmap*) SK_OVERRIDE;
242 virtual bool onReadPixels(const SkBitmap& bitmap,
243 int x, int y,
244 SkCanvas::Config8888 config8888) SK_OVERRIDE;
245
246 // The following methods are no-ops on a deferred device
247 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*)
248 SK_OVERRIDE
249 {return false;}
junov@chromium.org88e29142012-08-07 16:48:22 +0000250
251 // None of the following drawing methods should ever get called on the
252 // deferred device
253 virtual void clear(SkColor color)
254 {SkASSERT(0);}
255 virtual void drawPaint(const SkDraw&, const SkPaint& paint)
256 {SkASSERT(0);}
257 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
258 size_t count, const SkPoint[],
259 const SkPaint& paint)
260 {SkASSERT(0);}
261 virtual void drawRect(const SkDraw&, const SkRect& r,
262 const SkPaint& paint)
263 {SkASSERT(0);}
264 virtual void drawPath(const SkDraw&, const SkPath& path,
265 const SkPaint& paint,
266 const SkMatrix* prePathMatrix = NULL,
267 bool pathIsMutable = false)
268 {SkASSERT(0);}
269 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
270 const SkIRect* srcRectOrNull,
271 const SkMatrix& matrix, const SkPaint& paint)
272 {SkASSERT(0);}
273 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
274 int x, int y, const SkPaint& paint)
275 {SkASSERT(0);}
276 virtual void drawText(const SkDraw&, const void* text, size_t len,
277 SkScalar x, SkScalar y, const SkPaint& paint)
278 {SkASSERT(0);}
279 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
280 const SkScalar pos[], SkScalar constY,
281 int scalarsPerPos, const SkPaint& paint)
282 {SkASSERT(0);}
283 virtual void drawTextOnPath(const SkDraw&, const void* text,
284 size_t len, const SkPath& path,
285 const SkMatrix* matrix,
286 const SkPaint& paint)
287 {SkASSERT(0);}
288 virtual void drawPosTextOnPath(const SkDraw& draw, const void* text,
289 size_t len, const SkPoint pos[],
290 const SkPaint& paint,
291 const SkPath& path,
292 const SkMatrix* matrix)
293 {SkASSERT(0);}
294 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
295 int vertexCount, const SkPoint verts[],
296 const SkPoint texs[], const SkColor colors[],
297 SkXfermode* xmode, const uint16_t indices[],
298 int indexCount, const SkPaint& paint)
299 {SkASSERT(0);}
300 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
301 const SkPaint&)
302 {SkASSERT(0);}
303private:
304 virtual void flush();
305
junov@chromium.org88e29142012-08-07 16:48:22 +0000306 void beginRecording();
307
308 DeferredPipeController fPipeController;
309 SkGPipeWriter fPipeWriter;
310 SkDevice* fImmediateDevice;
311 SkCanvas* fImmediateCanvas;
312 SkCanvas* fRecordingCanvas;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000313 SkDeferredCanvas::NotificationClient* fNotificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000314 bool fFreshFrame;
315 size_t fMaxRecordingStorageBytes;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000316 size_t fPreviousStorageAllocated;
sugoi@google.com7775fd52012-11-21 15:47:04 +0000317 size_t fBitmapSizeThreshold;
junov@chromium.org88e29142012-08-07 16:48:22 +0000318};
319
320DeferredDevice::DeferredDevice(
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000321 SkDevice* immediateDevice, SkDeferredCanvas::NotificationClient* notificationClient) :
junov@chromium.org88e29142012-08-07 16:48:22 +0000322 SkDevice(SkBitmap::kNo_Config, immediateDevice->width(),
323 immediateDevice->height(), immediateDevice->isOpaque())
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000324 , fRecordingCanvas(NULL)
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000325 , fFreshFrame(true)
sugoi@google.com7775fd52012-11-21 15:47:04 +0000326 , fPreviousStorageAllocated(0)
sugoi@google.com5347de12012-11-21 16:44:45 +0000327 , fBitmapSizeThreshold(kDeferredCanvasBitmapSizeThreshold){
junov@chromium.org88e29142012-08-07 16:48:22 +0000328
329 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000330 fNotificationClient = notificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000331 fImmediateDevice = immediateDevice; // ref counted via fImmediateCanvas
332 fImmediateCanvas = SkNEW_ARGS(SkCanvas, (fImmediateDevice));
333 fPipeController.setPlaybackCanvas(fImmediateCanvas);
334 this->beginRecording();
335}
336
337DeferredDevice::~DeferredDevice() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000338 this->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000339 SkSafeUnref(fImmediateCanvas);
junov@chromium.org88e29142012-08-07 16:48:22 +0000340}
341
342void DeferredDevice::setMaxRecordingStorage(size_t maxStorage) {
343 fMaxRecordingStorageBytes = maxStorage;
344 this->recordingCanvas(); // Accessing the recording canvas applies the new limit.
345}
346
junov@chromium.org88e29142012-08-07 16:48:22 +0000347void DeferredDevice::beginRecording() {
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000348 SkASSERT(NULL == fRecordingCanvas);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000349 fRecordingCanvas = fPipeWriter.startRecording(&fPipeController, 0,
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000350 fImmediateDevice->width(), fImmediateDevice->height());
junov@chromium.org88e29142012-08-07 16:48:22 +0000351}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000352
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000353void DeferredDevice::setNotificationClient(
354 SkDeferredCanvas::NotificationClient* notificationClient) {
junov@chromium.org52805482012-08-20 14:25:04 +0000355 fNotificationClient = notificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000356}
357
junov@chromium.org0a67f962012-09-19 22:48:34 +0000358void DeferredDevice::skipPendingCommands() {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000359 if (!fRecordingCanvas->isDrawingToLayer() && fPipeController.hasPendingCommands()) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000360 fFreshFrame = true;
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000361 flushPendingCommands(kSilent_PlaybackMode);
junov@google.com52a00ca2012-10-01 15:27:14 +0000362 if (fNotificationClient) {
363 fNotificationClient->skippedPendingDrawCommands();
364 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000365 }
366}
367
368bool DeferredDevice::isFreshFrame() {
369 bool ret = fFreshFrame;
370 fFreshFrame = false;
371 return ret;
372}
373
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000374bool DeferredDevice::hasPendingCommands() {
375 return fPipeController.hasPendingCommands();
376}
377
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000378void DeferredDevice::flushPendingCommands(PlaybackMode playbackMode) {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000379 if (!fPipeController.hasPendingCommands()) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000380 return;
381 }
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000382 if (playbackMode == kNormal_PlaybackMode && fNotificationClient) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000383 fNotificationClient->prepareForDraw();
junov@chromium.org88e29142012-08-07 16:48:22 +0000384 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000385 fPipeWriter.flushRecording(true);
junov@chromium.orgd4501a02012-10-30 19:05:17 +0000386 fPipeController.playback(kSilent_PlaybackMode == playbackMode);
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000387 if (playbackMode == kNormal_PlaybackMode && fNotificationClient) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000388 fNotificationClient->flushedDrawCommands();
389 }
390 fPreviousStorageAllocated = storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000391}
392
393void DeferredDevice::flush() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000394 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000395 fImmediateCanvas->flush();
396}
397
398size_t DeferredDevice::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000399 size_t val = fPipeWriter.freeMemoryIfPossible(bytesToFree);
400 fPreviousStorageAllocated = storageAllocatedForRecording();
401 return val;
junov@chromium.org88e29142012-08-07 16:48:22 +0000402}
403
sugoi@google.com7775fd52012-11-21 15:47:04 +0000404size_t DeferredDevice::getBitmapSizeThreshold() const {
405 return fBitmapSizeThreshold;
406}
407
408void DeferredDevice::setBitmapSizeThreshold(size_t sizeThreshold) {
409 fBitmapSizeThreshold = sizeThreshold;
410}
411
junov@chromium.org88e29142012-08-07 16:48:22 +0000412size_t DeferredDevice::storageAllocatedForRecording() const {
413 return (fPipeController.storageAllocatedForRecording()
414 + fPipeWriter.storageAllocatedForRecording());
415}
416
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000417void DeferredDevice::recordedDrawCommand() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000418 size_t storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000419
junov@chromium.org88e29142012-08-07 16:48:22 +0000420 if (storageAllocated > fMaxRecordingStorageBytes) {
421 // First, attempt to reduce cache without flushing
422 size_t tryFree = storageAllocated - fMaxRecordingStorageBytes;
423 if (this->freeMemoryIfPossible(tryFree) < tryFree) {
424 // Flush is necessary to free more space.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000425 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000426 // Free as much as possible to avoid oscillating around fMaxRecordingStorageBytes
427 // which could cause a high flushing frequency.
bsalomon@google.com100abf42012-09-05 17:40:04 +0000428 this->freeMemoryIfPossible(~0U);
junov@chromium.org88e29142012-08-07 16:48:22 +0000429 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000430 storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000431 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000432
rmistry@google.comd6176b02012-08-23 18:14:13 +0000433 if (fNotificationClient &&
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000434 storageAllocated != fPreviousStorageAllocated) {
435 fPreviousStorageAllocated = storageAllocated;
436 fNotificationClient->storageAllocatedForRecordingChanged(storageAllocated);
437 }
438}
439
440SkCanvas* DeferredDevice::recordingCanvas() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000441 return fRecordingCanvas;
442}
443
rmistry@google.comd6176b02012-08-23 18:14:13 +0000444uint32_t DeferredDevice::getDeviceCapabilities() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000445 return fImmediateDevice->getDeviceCapabilities();
446}
447
rmistry@google.comd6176b02012-08-23 18:14:13 +0000448int DeferredDevice::width() const {
junov@chromium.org88e29142012-08-07 16:48:22 +0000449 return fImmediateDevice->width();
450}
451
452int DeferredDevice::height() const {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000453 return fImmediateDevice->height();
junov@chromium.org88e29142012-08-07 16:48:22 +0000454}
455
456SkGpuRenderTarget* DeferredDevice::accessRenderTarget() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000457 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000458 return fImmediateDevice->accessRenderTarget();
459}
460
461void DeferredDevice::writePixels(const SkBitmap& bitmap,
462 int x, int y, SkCanvas::Config8888 config8888) {
463
464 if (x <= 0 && y <= 0 && (x + bitmap.width()) >= width() &&
465 (y + bitmap.height()) >= height()) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000466 this->skipPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000467 }
468
469 if (SkBitmap::kARGB_8888_Config == bitmap.config() &&
470 SkCanvas::kNative_Premul_Config8888 != config8888 &&
471 kPMColorAlias != config8888) {
472 //Special case config: no deferral
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000473 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000474 fImmediateDevice->writePixels(bitmap, x, y, config8888);
475 return;
476 }
477
478 SkPaint paint;
479 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000480 if (shouldDrawImmediately(&bitmap, NULL, getBitmapSizeThreshold())) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000481 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000482 fImmediateCanvas->drawSprite(bitmap, x, y, &paint);
483 } else {
484 this->recordingCanvas()->drawSprite(bitmap, x, y, &paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000485 this->recordedDrawCommand();
486
junov@chromium.org88e29142012-08-07 16:48:22 +0000487 }
488}
489
490const SkBitmap& DeferredDevice::onAccessBitmap(SkBitmap*) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000491 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000492 return fImmediateDevice->accessBitmap(false);
493}
494
495SkDevice* DeferredDevice::onCreateCompatibleDevice(
496 SkBitmap::Config config, int width, int height, bool isOpaque,
497 Usage usage) {
498
499 // Save layer usage not supported, and not required by SkDeferredCanvas.
500 SkASSERT(usage != kSaveLayer_Usage);
501 // Create a compatible non-deferred device.
502 SkAutoTUnref<SkDevice> compatibleDevice
503 (fImmediateDevice->createCompatibleDevice(config, width, height,
504 isOpaque));
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000505 return SkNEW_ARGS(DeferredDevice, (compatibleDevice, fNotificationClient));
junov@chromium.org88e29142012-08-07 16:48:22 +0000506}
507
508bool DeferredDevice::onReadPixels(
509 const SkBitmap& bitmap, int x, int y, SkCanvas::Config8888 config8888) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000510 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000511 return fImmediateCanvas->readPixels(const_cast<SkBitmap*>(&bitmap),
512 x, y, config8888);
513}
514
sugoi@google.com7775fd52012-11-21 15:47:04 +0000515class AutoImmediateDrawIfNeeded {
516public:
517 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkBitmap* bitmap,
518 const SkPaint* paint) {
519 this->init(canvas, bitmap, paint);
520 }
521
522 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkPaint* paint) {
523 this->init(canvas, NULL, paint);
524 }
525
526 ~AutoImmediateDrawIfNeeded() {
527 if (fCanvas) {
528 fCanvas->setDeferredDrawing(true);
529 }
530 }
531private:
532 void init(SkDeferredCanvas& canvas, const SkBitmap* bitmap, const SkPaint* paint)
533 {
534 DeferredDevice* device = static_cast<DeferredDevice*>(canvas.getDevice());
535 if (canvas.isDeferredDrawing() && (NULL != device) &&
536 shouldDrawImmediately(bitmap, paint, device->getBitmapSizeThreshold())) {
537 canvas.setDeferredDrawing(false);
538 fCanvas = &canvas;
539 } else {
540 fCanvas = NULL;
541 }
542 }
543
544 SkDeferredCanvas* fCanvas;
545};
junov@chromium.org88e29142012-08-07 16:48:22 +0000546
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000547SkDeferredCanvas::SkDeferredCanvas() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000548 this->init();
junov@google.com4370aed2012-01-18 16:21:08 +0000549}
550
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000551SkDeferredCanvas::SkDeferredCanvas(SkDevice* device) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000552 this->init();
553 this->setDevice(device);
junov@google.com4370aed2012-01-18 16:21:08 +0000554}
555
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000556void SkDeferredCanvas::init() {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000557 fDeferredDrawing = true; // On by default
junov@google.com4370aed2012-01-18 16:21:08 +0000558}
559
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000560void SkDeferredCanvas::setMaxRecordingStorage(size_t maxStorage) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000561 this->validate();
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000562 this->getDeferredDevice()->setMaxRecordingStorage(maxStorage);
563}
564
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000565size_t SkDeferredCanvas::storageAllocatedForRecording() const {
566 return this->getDeferredDevice()->storageAllocatedForRecording();
567}
568
569size_t SkDeferredCanvas::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000570 return this->getDeferredDevice()->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000571}
572
sugoi@google.com7775fd52012-11-21 15:47:04 +0000573void SkDeferredCanvas::setBitmapSizeThreshold(size_t sizeThreshold) {
574 DeferredDevice* deferredDevice = this->getDeferredDevice();
575 SkASSERT(deferredDevice);
576 deferredDevice->setBitmapSizeThreshold(sizeThreshold);
577}
578
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000579void SkDeferredCanvas::recordedDrawCommand() {
580 if (fDeferredDrawing) {
581 this->getDeferredDevice()->recordedDrawCommand();
582 }
583}
584
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000585void SkDeferredCanvas::validate() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000586 SkASSERT(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000587}
588
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000589SkCanvas* SkDeferredCanvas::drawingCanvas() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000590 this->validate();
591 return fDeferredDrawing ? this->getDeferredDevice()->recordingCanvas() :
592 this->getDeferredDevice()->immediateCanvas();
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000593}
594
junov@chromium.org88e29142012-08-07 16:48:22 +0000595SkCanvas* SkDeferredCanvas::immediateCanvas() const {
596 this->validate();
597 return this->getDeferredDevice()->immediateCanvas();
598}
599
600DeferredDevice* SkDeferredCanvas::getDeferredDevice() const {
601 return static_cast<DeferredDevice*>(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000602}
603
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000604void SkDeferredCanvas::setDeferredDrawing(bool val) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000605 this->validate(); // Must set device before calling this method
junov@google.com4370aed2012-01-18 16:21:08 +0000606 if (val != fDeferredDrawing) {
607 if (fDeferredDrawing) {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000608 // Going live.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000609 this->getDeferredDevice()->flushPendingCommands(kNormal_PlaybackMode);
junov@google.com4370aed2012-01-18 16:21:08 +0000610 }
611 fDeferredDrawing = val;
612 }
613}
614
junov@chromium.org88e29142012-08-07 16:48:22 +0000615bool SkDeferredCanvas::isDeferredDrawing() const {
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +0000616 return fDeferredDrawing;
617}
618
junov@chromium.org88e29142012-08-07 16:48:22 +0000619bool SkDeferredCanvas::isFreshFrame() const {
620 return this->getDeferredDevice()->isFreshFrame();
621}
622
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000623bool SkDeferredCanvas::hasPendingCommands() const {
624 return this->getDeferredDevice()->hasPendingCommands();
625}
626
junov@chromium.orgfb103892012-09-20 19:35:43 +0000627void SkDeferredCanvas::silentFlush() {
628 if (fDeferredDrawing) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000629 this->getDeferredDevice()->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000630 }
631}
632
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000633SkDeferredCanvas::~SkDeferredCanvas() {
junov@google.com4370aed2012-01-18 16:21:08 +0000634}
635
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000636SkDevice* SkDeferredCanvas::setDevice(SkDevice* device) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000637 this->INHERITED::setDevice(SkNEW_ARGS(DeferredDevice, (device)))->unref();
junov@google.com4370aed2012-01-18 16:21:08 +0000638 return device;
639}
640
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000641SkDeferredCanvas::NotificationClient* SkDeferredCanvas::setNotificationClient(
642 NotificationClient* notificationClient) {
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000643
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000644 DeferredDevice* deferredDevice = this->getDeferredDevice();
junov@google.com4370aed2012-01-18 16:21:08 +0000645 SkASSERT(deferredDevice);
646 if (deferredDevice) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000647 deferredDevice->setNotificationClient(notificationClient);
junov@google.com4370aed2012-01-18 16:21:08 +0000648 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000649 return notificationClient;
junov@google.com4370aed2012-01-18 16:21:08 +0000650}
651
652bool SkDeferredCanvas::isFullFrame(const SkRect* rect,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000653 const SkPaint* paint) const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000654 SkCanvas* canvas = this->drawingCanvas();
655 SkISize canvasSize = this->getDeviceSize();
junov@google.com4370aed2012-01-18 16:21:08 +0000656 if (rect) {
657 if (!canvas->getTotalMatrix().rectStaysRect()) {
658 return false; // conservative
659 }
660
661 SkRect transformedRect;
662 canvas->getTotalMatrix().mapRect(&transformedRect, *rect);
663
664 if (paint) {
665 SkPaint::Style paintStyle = paint->getStyle();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000666 if (!(paintStyle == SkPaint::kFill_Style ||
junov@google.com4370aed2012-01-18 16:21:08 +0000667 paintStyle == SkPaint::kStrokeAndFill_Style)) {
668 return false;
669 }
670 if (paint->getMaskFilter() || paint->getLooper()
671 || paint->getPathEffect() || paint->getImageFilter()) {
672 return false; // conservative
673 }
674 }
675
676 // The following test holds with AA enabled, and is conservative
677 // by a 0.5 pixel margin with AA disabled
rmistry@google.comd6176b02012-08-23 18:14:13 +0000678 if (transformedRect.fLeft > SkIntToScalar(0) ||
679 transformedRect.fTop > SkIntToScalar(0) ||
junov@chromium.orgb1e218e2012-02-13 22:27:58 +0000680 transformedRect.fRight < SkIntToScalar(canvasSize.fWidth) ||
681 transformedRect.fBottom < SkIntToScalar(canvasSize.fHeight)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000682 return false;
683 }
684 }
685
686 switch (canvas->getClipType()) {
687 case SkCanvas::kRect_ClipType :
688 {
689 SkIRect bounds;
690 canvas->getClipDeviceBounds(&bounds);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000691 if (bounds.fLeft > 0 || bounds.fTop > 0 ||
692 bounds.fRight < canvasSize.fWidth ||
junov@google.com4370aed2012-01-18 16:21:08 +0000693 bounds.fBottom < canvasSize.fHeight)
694 return false;
695 }
696 break;
697 case SkCanvas::kComplex_ClipType :
698 return false; // conservative
699 case SkCanvas::kEmpty_ClipType:
700 default:
701 break;
702 };
703
704 return true;
705}
706
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000707int SkDeferredCanvas::save(SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000708 this->drawingCanvas()->save(flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000709 int val = this->INHERITED::save(flags);
710 this->recordedDrawCommand();
711
712 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000713}
714
715int SkDeferredCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000716 SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000717 this->drawingCanvas()->saveLayer(bounds, paint, flags);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000718 int count = this->INHERITED::save(flags);
719 this->clipRectBounds(bounds, flags, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000720 this->recordedDrawCommand();
721
junov@chromium.orga907ac32012-02-24 21:54:07 +0000722 return count;
junov@google.com4370aed2012-01-18 16:21:08 +0000723}
724
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000725void SkDeferredCanvas::restore() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000726 this->drawingCanvas()->restore();
junov@chromium.orga907ac32012-02-24 21:54:07 +0000727 this->INHERITED::restore();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000728 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000729}
730
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000731bool SkDeferredCanvas::isDrawingToLayer() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000732 return this->drawingCanvas()->isDrawingToLayer();
junov@google.com4370aed2012-01-18 16:21:08 +0000733}
734
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000735bool SkDeferredCanvas::translate(SkScalar dx, SkScalar dy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000736 this->drawingCanvas()->translate(dx, dy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000737 bool val = this->INHERITED::translate(dx, dy);
738 this->recordedDrawCommand();
739 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000740}
741
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000742bool SkDeferredCanvas::scale(SkScalar sx, SkScalar sy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000743 this->drawingCanvas()->scale(sx, sy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000744 bool val = this->INHERITED::scale(sx, sy);
745 this->recordedDrawCommand();
746 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000747}
748
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000749bool SkDeferredCanvas::rotate(SkScalar degrees) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000750 this->drawingCanvas()->rotate(degrees);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000751 bool val = this->INHERITED::rotate(degrees);
752 this->recordedDrawCommand();
753 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000754}
755
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000756bool SkDeferredCanvas::skew(SkScalar sx, SkScalar sy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000757 this->drawingCanvas()->skew(sx, sy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000758 bool val = this->INHERITED::skew(sx, sy);
759 this->recordedDrawCommand();
760 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000761}
762
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000763bool SkDeferredCanvas::concat(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000764 this->drawingCanvas()->concat(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000765 bool val = this->INHERITED::concat(matrix);
766 this->recordedDrawCommand();
767 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000768}
769
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000770void SkDeferredCanvas::setMatrix(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000771 this->drawingCanvas()->setMatrix(matrix);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000772 this->INHERITED::setMatrix(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000773 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000774}
775
776bool SkDeferredCanvas::clipRect(const SkRect& rect,
777 SkRegion::Op op,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000778 bool doAntiAlias) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000779 this->drawingCanvas()->clipRect(rect, op, doAntiAlias);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000780 bool val = this->INHERITED::clipRect(rect, op, doAntiAlias);
781 this->recordedDrawCommand();
782 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000783}
784
785bool SkDeferredCanvas::clipPath(const SkPath& path,
786 SkRegion::Op op,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000787 bool doAntiAlias) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000788 this->drawingCanvas()->clipPath(path, op, doAntiAlias);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000789 bool val = this->INHERITED::clipPath(path, op, doAntiAlias);
790 this->recordedDrawCommand();
791 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000792}
793
794bool SkDeferredCanvas::clipRegion(const SkRegion& deviceRgn,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000795 SkRegion::Op op) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000796 this->drawingCanvas()->clipRegion(deviceRgn, op);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000797 bool val = this->INHERITED::clipRegion(deviceRgn, op);
798 this->recordedDrawCommand();
799 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000800}
801
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000802void SkDeferredCanvas::clear(SkColor color) {
junov@google.com4370aed2012-01-18 16:21:08 +0000803 // purge pending commands
804 if (fDeferredDrawing) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000805 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000806 }
807
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000808 this->drawingCanvas()->clear(color);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000809 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000810}
811
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000812void SkDeferredCanvas::drawPaint(const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000813 if (fDeferredDrawing && this->isFullFrame(NULL, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000814 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000815 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000816 }
junov@chromium.org10f7f972012-07-31 21:01:51 +0000817 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000818 this->drawingCanvas()->drawPaint(paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000819 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000820}
821
822void SkDeferredCanvas::drawPoints(PointMode mode, size_t count,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000823 const SkPoint pts[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000824 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000825 this->drawingCanvas()->drawPoints(mode, count, pts, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000826 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000827}
828
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000829void SkDeferredCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000830 if (fDeferredDrawing && this->isFullFrame(&rect, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000831 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000832 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000833 }
834
junov@chromium.org10f7f972012-07-31 21:01:51 +0000835 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000836 this->drawingCanvas()->drawRect(rect, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000837 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000838}
839
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000840void SkDeferredCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000841 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000842 this->drawingCanvas()->drawPath(path, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000843 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000844}
845
846void SkDeferredCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000847 SkScalar top, const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000848 SkRect bitmapRect = SkRect::MakeXYWH(left, top,
849 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000850 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000851 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000852 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000853 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000854 }
855
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()->drawBitmap(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000858 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000859}
860
reed@google.com71121732012-09-18 15:14:33 +0000861void SkDeferredCanvas::drawBitmapRectToRect(const SkBitmap& bitmap,
862 const SkRect* src,
863 const SkRect& dst,
864 const SkPaint* paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000865 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000866 this->isFullFrame(&dst, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000867 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000868 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000869 }
870
junov@chromium.org10f7f972012-07-31 21:01:51 +0000871 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
reed@google.com71121732012-09-18 15:14:33 +0000872 this->drawingCanvas()->drawBitmapRectToRect(bitmap, src, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000873 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000874}
875
876
877void SkDeferredCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
878 const SkMatrix& m,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000879 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000880 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
881 // covers canvas entirely and transformed bitmap covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000882 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000883 this->drawingCanvas()->drawBitmapMatrix(bitmap, m, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000884 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000885}
886
887void SkDeferredCanvas::drawBitmapNine(const SkBitmap& bitmap,
888 const SkIRect& center, const SkRect& dst,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000889 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000890 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
891 // covers canvas entirely and dst covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000892 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000893 this->drawingCanvas()->drawBitmapNine(bitmap, center, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000894 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000895}
896
897void SkDeferredCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000898 const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000899 SkRect bitmapRect = SkRect::MakeXYWH(
900 SkIntToScalar(left),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000901 SkIntToScalar(top),
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000902 SkIntToScalar(bitmap.width()),
903 SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000904 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000905 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000906 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000907 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000908 }
909
junov@chromium.org10f7f972012-07-31 21:01:51 +0000910 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000911 this->drawingCanvas()->drawSprite(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000912 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000913}
914
915void SkDeferredCanvas::drawText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000916 SkScalar x, SkScalar y, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000917 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000918 this->drawingCanvas()->drawText(text, byteLength, x, y, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000919 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000920}
921
922void SkDeferredCanvas::drawPosText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000923 const SkPoint pos[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000924 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000925 this->drawingCanvas()->drawPosText(text, byteLength, pos, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000926 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000927}
928
929void SkDeferredCanvas::drawPosTextH(const void* text, size_t byteLength,
930 const SkScalar xpos[], SkScalar constY,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000931 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000932 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000933 this->drawingCanvas()->drawPosTextH(text, byteLength, xpos, constY, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000934 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000935}
936
937void SkDeferredCanvas::drawTextOnPath(const void* text, size_t byteLength,
938 const SkPath& path,
939 const SkMatrix* matrix,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000940 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000941 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000942 this->drawingCanvas()->drawTextOnPath(text, byteLength, path, matrix, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000943 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000944}
945
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000946void SkDeferredCanvas::drawPicture(SkPicture& picture) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000947 this->drawingCanvas()->drawPicture(picture);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000948 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000949}
950
951void SkDeferredCanvas::drawVertices(VertexMode vmode, int vertexCount,
952 const SkPoint vertices[],
953 const SkPoint texs[],
954 const SkColor colors[], SkXfermode* xmode,
955 const uint16_t indices[], int indexCount,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000956 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000957 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000958 this->drawingCanvas()->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode,
959 indices, indexCount, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000960 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000961}
962
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000963SkBounder* SkDeferredCanvas::setBounder(SkBounder* bounder) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000964 this->drawingCanvas()->setBounder(bounder);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000965 this->INHERITED::setBounder(bounder);
966 this->recordedDrawCommand();
967 return bounder;
junov@google.com4370aed2012-01-18 16:21:08 +0000968}
969
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000970SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000971 this->drawingCanvas()->setDrawFilter(filter);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000972 this->INHERITED::setDrawFilter(filter);
973 this->recordedDrawCommand();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000974 return filter;
junov@google.com4370aed2012-01-18 16:21:08 +0000975}
976
977SkCanvas* SkDeferredCanvas::canvasForDrawIter() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000978 return this->drawingCanvas();
junov@google.com4370aed2012-01-18 16:21:08 +0000979}