blob: 21f717cf530481c35f2d26132b9f8b074f6bf220 [file] [log] [blame]
junov@google.com4370aed2012-01-18 16:21:08 +00001
2/*
junov@chromium.orgbaa02202013-01-24 14:38:23 +00003 * Copyright 2013 Google Inc.
junov@google.com4370aed2012-01-18 16:21:08 +00004 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9#include "SkDeferredCanvas.h"
10
junov@chromium.org88e29142012-08-07 16:48:22 +000011#include "SkChunkAlloc.h"
12#include "SkColorFilter.h"
13#include "SkDevice.h"
14#include "SkDrawFilter.h"
15#include "SkGPipe.h"
junov@google.com4370aed2012-01-18 16:21:08 +000016#include "SkPaint.h"
junov@chromium.orgbaa02202013-01-24 14:38:23 +000017#include "SkPaintPriv.h"
reed@google.com4ed0fb72012-12-12 20:48:18 +000018#include "SkRRect.h"
junov@google.com4370aed2012-01-18 16:21:08 +000019#include "SkShader.h"
junov@chromium.org67d74222013-04-12 13:33:01 +000020#include "SkSurface.h"
junov@google.com4370aed2012-01-18 16:21:08 +000021
junov@chromium.orgbfeddae2012-07-23 13:35:14 +000022enum {
23 // Deferred canvas will auto-flush when recording reaches this limit
24 kDefaultMaxRecordingStorageBytes = 64*1024*1024,
reed@google.com140d7282013-01-07 20:25:04 +000025 kDeferredCanvasBitmapSizeThreshold = ~0U, // Disables this feature
junov@chromium.orgbfeddae2012-07-23 13:35:14 +000026};
27
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +000028enum PlaybackMode {
29 kNormal_PlaybackMode,
30 kSilent_PlaybackMode,
31};
32
junov@google.com4370aed2012-01-18 16:21:08 +000033namespace {
sugoi@google.com7775fd52012-11-21 15:47:04 +000034bool shouldDrawImmediately(const SkBitmap* bitmap, const SkPaint* paint,
35 size_t bitmapSizeThreshold) {
36 if (bitmap && ((bitmap->getTexture() && !bitmap->isImmutable()) ||
37 (bitmap->getSize() > bitmapSizeThreshold))) {
junov@chromium.org10f7f972012-07-31 21:01:51 +000038 return true;
39 }
40 if (paint) {
41 SkShader* shader = paint->getShader();
42 // Here we detect the case where the shader is an SkBitmapProcShader
43 // with a gpu texture attached. Checking this without RTTI
44 // requires making the assumption that only gradient shaders
45 // and SkBitmapProcShader implement asABitmap(). The following
46 // code may need to be revised if that assumption is ever broken.
47 if (shader && !shader->asAGradient(NULL)) {
48 SkBitmap bm;
rmistry@google.comd6176b02012-08-23 18:14:13 +000049 if (shader->asABitmap(&bm, NULL, NULL) &&
junov@chromium.org10f7f972012-07-31 21:01:51 +000050 NULL != bm.getTexture()) {
51 return true;
52 }
53 }
54 }
55 return false;
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +000056}
57}
58
junov@chromium.org88e29142012-08-07 16:48:22 +000059//-----------------------------------------------------------------------------
60// DeferredPipeController
61//-----------------------------------------------------------------------------
62
63class DeferredPipeController : public SkGPipeController {
64public:
65 DeferredPipeController();
66 void setPlaybackCanvas(SkCanvas*);
67 virtual ~DeferredPipeController();
68 virtual void* requestBlock(size_t minRequest, size_t* actual) SK_OVERRIDE;
69 virtual void notifyWritten(size_t bytes) SK_OVERRIDE;
junov@chromium.orgfb103892012-09-20 19:35:43 +000070 void playback(bool silent);
junov@chromium.orga38dfb62012-09-20 22:10:33 +000071 bool hasPendingCommands() const { return fAllocator.blockCount() != 0; }
junov@chromium.org88e29142012-08-07 16:48:22 +000072 size_t storageAllocatedForRecording() const { return fAllocator.totalCapacity(); }
73private:
74 enum {
75 kMinBlockSize = 4096
76 };
77 struct PipeBlock {
78 PipeBlock(void* block, size_t size) { fBlock = block, fSize = size; }
79 void* fBlock;
80 size_t fSize;
81 };
82 void* fBlock;
83 size_t fBytesWritten;
84 SkChunkAlloc fAllocator;
85 SkTDArray<PipeBlock> fBlockList;
86 SkGPipeReader fReader;
87};
88
89DeferredPipeController::DeferredPipeController() :
90 fAllocator(kMinBlockSize) {
91 fBlock = NULL;
92 fBytesWritten = 0;
93}
94
95DeferredPipeController::~DeferredPipeController() {
96 fAllocator.reset();
97}
98
99void DeferredPipeController::setPlaybackCanvas(SkCanvas* canvas) {
100 fReader.setCanvas(canvas);
101}
102
103void* DeferredPipeController::requestBlock(size_t minRequest, size_t *actual) {
104 if (fBlock) {
105 // Save the previous block for later
106 PipeBlock previousBloc(fBlock, fBytesWritten);
107 fBlockList.push(previousBloc);
108 }
109 int32_t blockSize = SkMax32(minRequest, kMinBlockSize);
110 fBlock = fAllocator.allocThrow(blockSize);
111 fBytesWritten = 0;
112 *actual = blockSize;
113 return fBlock;
114}
115
116void DeferredPipeController::notifyWritten(size_t bytes) {
117 fBytesWritten += bytes;
118}
119
junov@chromium.orgfb103892012-09-20 19:35:43 +0000120void DeferredPipeController::playback(bool silent) {
121 uint32_t flags = silent ? SkGPipeReader::kSilent_PlaybackFlag : 0;
junov@chromium.org88e29142012-08-07 16:48:22 +0000122 for (int currentBlock = 0; currentBlock < fBlockList.count(); currentBlock++ ) {
junov@chromium.orgfb103892012-09-20 19:35:43 +0000123 fReader.playback(fBlockList[currentBlock].fBlock, fBlockList[currentBlock].fSize,
124 flags);
junov@chromium.org88e29142012-08-07 16:48:22 +0000125 }
126 fBlockList.reset();
127
128 if (fBlock) {
junov@chromium.orgfb103892012-09-20 19:35:43 +0000129 fReader.playback(fBlock, fBytesWritten, flags);
junov@chromium.org88e29142012-08-07 16:48:22 +0000130 fBlock = NULL;
131 }
132
133 // Release all allocated blocks
134 fAllocator.reset();
135}
136
junov@chromium.org88e29142012-08-07 16:48:22 +0000137//-----------------------------------------------------------------------------
138// DeferredDevice
139//-----------------------------------------------------------------------------
junov@chromium.org88e29142012-08-07 16:48:22 +0000140class DeferredDevice : public SkDevice {
141public:
junov@chromium.org67d74222013-04-12 13:33:01 +0000142 explicit DeferredDevice(SkDevice* immediateDevice);
143 explicit DeferredDevice(SkSurface* surface);
junov@chromium.org88e29142012-08-07 16:48:22 +0000144 ~DeferredDevice();
145
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000146 void setNotificationClient(SkDeferredCanvas::NotificationClient* notificationClient);
junov@chromium.org88e29142012-08-07 16:48:22 +0000147 SkCanvas* recordingCanvas();
148 SkCanvas* immediateCanvas() const {return fImmediateCanvas;}
149 SkDevice* immediateDevice() const {return fImmediateDevice;}
junov@chromium.org67d74222013-04-12 13:33:01 +0000150 SkImage* newImageShapshot();
junov@chromium.org88e29142012-08-07 16:48:22 +0000151 bool isFreshFrame();
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000152 bool hasPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000153 size_t storageAllocatedForRecording() const;
154 size_t freeMemoryIfPossible(size_t bytesToFree);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000155 size_t getBitmapSizeThreshold() const;
156 void setBitmapSizeThreshold(size_t sizeThreshold);
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000157 void flushPendingCommands(PlaybackMode);
junov@chromium.org0a67f962012-09-19 22:48:34 +0000158 void skipPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000159 void setMaxRecordingStorage(size_t);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000160 void recordedDrawCommand();
junov@chromium.org88e29142012-08-07 16:48:22 +0000161
162 virtual uint32_t getDeviceCapabilities() SK_OVERRIDE;
163 virtual int width() const SK_OVERRIDE;
164 virtual int height() const SK_OVERRIDE;
165 virtual SkGpuRenderTarget* accessRenderTarget() SK_OVERRIDE;
166
167 virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
168 int width, int height,
169 bool isOpaque,
170 Usage usage) SK_OVERRIDE;
171
172 virtual void writePixels(const SkBitmap& bitmap, int x, int y,
173 SkCanvas::Config8888 config8888) SK_OVERRIDE;
174
175protected:
176 virtual const SkBitmap& onAccessBitmap(SkBitmap*) SK_OVERRIDE;
177 virtual bool onReadPixels(const SkBitmap& bitmap,
178 int x, int y,
179 SkCanvas::Config8888 config8888) SK_OVERRIDE;
180
181 // The following methods are no-ops on a deferred device
182 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*)
183 SK_OVERRIDE
184 {return false;}
junov@chromium.org88e29142012-08-07 16:48:22 +0000185
186 // None of the following drawing methods should ever get called on the
187 // deferred device
188 virtual void clear(SkColor color)
189 {SkASSERT(0);}
190 virtual void drawPaint(const SkDraw&, const SkPaint& paint)
191 {SkASSERT(0);}
192 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
193 size_t count, const SkPoint[],
194 const SkPaint& paint)
195 {SkASSERT(0);}
196 virtual void drawRect(const SkDraw&, const SkRect& r,
197 const SkPaint& paint)
198 {SkASSERT(0);}
199 virtual void drawPath(const SkDraw&, const SkPath& path,
200 const SkPaint& paint,
201 const SkMatrix* prePathMatrix = NULL,
202 bool pathIsMutable = false)
203 {SkASSERT(0);}
204 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
205 const SkIRect* srcRectOrNull,
206 const SkMatrix& matrix, const SkPaint& paint)
207 {SkASSERT(0);}
208 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
209 int x, int y, const SkPaint& paint)
210 {SkASSERT(0);}
211 virtual void drawText(const SkDraw&, const void* text, size_t len,
212 SkScalar x, SkScalar y, const SkPaint& paint)
213 {SkASSERT(0);}
214 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
215 const SkScalar pos[], SkScalar constY,
216 int scalarsPerPos, const SkPaint& paint)
217 {SkASSERT(0);}
218 virtual void drawTextOnPath(const SkDraw&, const void* text,
219 size_t len, const SkPath& path,
220 const SkMatrix* matrix,
221 const SkPaint& paint)
222 {SkASSERT(0);}
223 virtual void drawPosTextOnPath(const SkDraw& draw, const void* text,
224 size_t len, const SkPoint pos[],
225 const SkPaint& paint,
226 const SkPath& path,
227 const SkMatrix* matrix)
228 {SkASSERT(0);}
229 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
230 int vertexCount, const SkPoint verts[],
231 const SkPoint texs[], const SkColor colors[],
232 SkXfermode* xmode, const uint16_t indices[],
233 int indexCount, const SkPaint& paint)
234 {SkASSERT(0);}
235 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
236 const SkPaint&)
237 {SkASSERT(0);}
238private:
239 virtual void flush();
240
junov@chromium.org88e29142012-08-07 16:48:22 +0000241 void beginRecording();
junov@chromium.org67d74222013-04-12 13:33:01 +0000242 void init();
junov@chromium.org88e29142012-08-07 16:48:22 +0000243
244 DeferredPipeController fPipeController;
245 SkGPipeWriter fPipeWriter;
246 SkDevice* fImmediateDevice;
247 SkCanvas* fImmediateCanvas;
248 SkCanvas* fRecordingCanvas;
junov@chromium.org67d74222013-04-12 13:33:01 +0000249 SkSurface* fSurface;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000250 SkDeferredCanvas::NotificationClient* fNotificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000251 bool fFreshFrame;
252 size_t fMaxRecordingStorageBytes;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000253 size_t fPreviousStorageAllocated;
sugoi@google.com7775fd52012-11-21 15:47:04 +0000254 size_t fBitmapSizeThreshold;
junov@chromium.org88e29142012-08-07 16:48:22 +0000255};
256
junov@chromium.org67d74222013-04-12 13:33:01 +0000257DeferredDevice::DeferredDevice(SkDevice* immediateDevice)
258 : SkDevice(SkBitmap::kNo_Config,
259 immediateDevice->width(), immediateDevice->height(),
260 immediateDevice->isOpaque(),
261 immediateDevice->getDeviceProperties()) {
262 fSurface = NULL;
263 fImmediateDevice = immediateDevice; // ref counted via fImmediateCanvas
junov@chromium.org88e29142012-08-07 16:48:22 +0000264 fImmediateCanvas = SkNEW_ARGS(SkCanvas, (fImmediateDevice));
junov@chromium.org67d74222013-04-12 13:33:01 +0000265 this->init();
266}
267
268DeferredDevice::DeferredDevice(SkSurface* surface)
269 : SkDevice(SkBitmap::kNo_Config,
270 surface->getCanvas()->getDevice()->width(),
271 surface->getCanvas()->getDevice()->height(),
272 surface->getCanvas()->getDevice()->isOpaque(),
273 surface->getCanvas()->getDevice()->getDeviceProperties()) {
274 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
275 fNotificationClient = NULL;
276 fImmediateCanvas = surface->getCanvas();
277 SkSafeRef(fImmediateCanvas);
278 fSurface = surface;
279 SkSafeRef(fSurface);
280 fImmediateDevice = fImmediateCanvas->getDevice(); // ref counted via fImmediateCanvas
281 this->init();
282}
283
284void DeferredDevice::init() {
285 fRecordingCanvas = NULL;
286 fFreshFrame = true;
287 fPreviousStorageAllocated = 0;
288 fBitmapSizeThreshold = kDeferredCanvasBitmapSizeThreshold;
289 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
290 fNotificationClient = NULL;
junov@chromium.org88e29142012-08-07 16:48:22 +0000291 fPipeController.setPlaybackCanvas(fImmediateCanvas);
292 this->beginRecording();
293}
294
295DeferredDevice::~DeferredDevice() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000296 this->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000297 SkSafeUnref(fImmediateCanvas);
junov@chromium.org67d74222013-04-12 13:33:01 +0000298 SkSafeUnref(fSurface);
junov@chromium.org88e29142012-08-07 16:48:22 +0000299}
300
301void DeferredDevice::setMaxRecordingStorage(size_t maxStorage) {
302 fMaxRecordingStorageBytes = maxStorage;
303 this->recordingCanvas(); // Accessing the recording canvas applies the new limit.
304}
305
junov@chromium.org88e29142012-08-07 16:48:22 +0000306void DeferredDevice::beginRecording() {
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000307 SkASSERT(NULL == fRecordingCanvas);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000308 fRecordingCanvas = fPipeWriter.startRecording(&fPipeController, 0,
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000309 fImmediateDevice->width(), fImmediateDevice->height());
junov@chromium.org88e29142012-08-07 16:48:22 +0000310}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000311
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000312void DeferredDevice::setNotificationClient(
313 SkDeferredCanvas::NotificationClient* notificationClient) {
junov@chromium.org52805482012-08-20 14:25:04 +0000314 fNotificationClient = notificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000315}
316
junov@chromium.org0a67f962012-09-19 22:48:34 +0000317void DeferredDevice::skipPendingCommands() {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000318 if (!fRecordingCanvas->isDrawingToLayer() && fPipeController.hasPendingCommands()) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000319 fFreshFrame = true;
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000320 flushPendingCommands(kSilent_PlaybackMode);
junov@google.com52a00ca2012-10-01 15:27:14 +0000321 if (fNotificationClient) {
322 fNotificationClient->skippedPendingDrawCommands();
323 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000324 }
325}
326
327bool DeferredDevice::isFreshFrame() {
328 bool ret = fFreshFrame;
329 fFreshFrame = false;
330 return ret;
331}
332
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000333bool DeferredDevice::hasPendingCommands() {
334 return fPipeController.hasPendingCommands();
335}
336
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000337void DeferredDevice::flushPendingCommands(PlaybackMode playbackMode) {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000338 if (!fPipeController.hasPendingCommands()) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000339 return;
340 }
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000341 if (playbackMode == kNormal_PlaybackMode && fNotificationClient) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000342 fNotificationClient->prepareForDraw();
junov@chromium.org88e29142012-08-07 16:48:22 +0000343 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000344 fPipeWriter.flushRecording(true);
junov@chromium.orgd4501a02012-10-30 19:05:17 +0000345 fPipeController.playback(kSilent_PlaybackMode == playbackMode);
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000346 if (playbackMode == kNormal_PlaybackMode && fNotificationClient) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000347 fNotificationClient->flushedDrawCommands();
348 }
349 fPreviousStorageAllocated = storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000350}
351
352void DeferredDevice::flush() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000353 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000354 fImmediateCanvas->flush();
355}
356
357size_t DeferredDevice::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000358 size_t val = fPipeWriter.freeMemoryIfPossible(bytesToFree);
359 fPreviousStorageAllocated = storageAllocatedForRecording();
360 return val;
junov@chromium.org88e29142012-08-07 16:48:22 +0000361}
362
sugoi@google.com7775fd52012-11-21 15:47:04 +0000363size_t DeferredDevice::getBitmapSizeThreshold() const {
364 return fBitmapSizeThreshold;
365}
366
367void DeferredDevice::setBitmapSizeThreshold(size_t sizeThreshold) {
368 fBitmapSizeThreshold = sizeThreshold;
369}
370
junov@chromium.org88e29142012-08-07 16:48:22 +0000371size_t DeferredDevice::storageAllocatedForRecording() const {
372 return (fPipeController.storageAllocatedForRecording()
373 + fPipeWriter.storageAllocatedForRecording());
374}
375
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000376void DeferredDevice::recordedDrawCommand() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000377 size_t storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000378
junov@chromium.org88e29142012-08-07 16:48:22 +0000379 if (storageAllocated > fMaxRecordingStorageBytes) {
380 // First, attempt to reduce cache without flushing
381 size_t tryFree = storageAllocated - fMaxRecordingStorageBytes;
382 if (this->freeMemoryIfPossible(tryFree) < tryFree) {
383 // Flush is necessary to free more space.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000384 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000385 // Free as much as possible to avoid oscillating around fMaxRecordingStorageBytes
386 // which could cause a high flushing frequency.
bsalomon@google.com100abf42012-09-05 17:40:04 +0000387 this->freeMemoryIfPossible(~0U);
junov@chromium.org88e29142012-08-07 16:48:22 +0000388 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000389 storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000390 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000391
rmistry@google.comd6176b02012-08-23 18:14:13 +0000392 if (fNotificationClient &&
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000393 storageAllocated != fPreviousStorageAllocated) {
394 fPreviousStorageAllocated = storageAllocated;
395 fNotificationClient->storageAllocatedForRecordingChanged(storageAllocated);
396 }
397}
398
399SkCanvas* DeferredDevice::recordingCanvas() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000400 return fRecordingCanvas;
401}
402
junov@chromium.org67d74222013-04-12 13:33:01 +0000403SkImage* DeferredDevice::newImageShapshot() {
404 this->flush();
405 return fSurface ? fSurface->newImageShapshot() : NULL;
406}
407
rmistry@google.comd6176b02012-08-23 18:14:13 +0000408uint32_t DeferredDevice::getDeviceCapabilities() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000409 return fImmediateDevice->getDeviceCapabilities();
410}
411
rmistry@google.comd6176b02012-08-23 18:14:13 +0000412int DeferredDevice::width() const {
junov@chromium.org88e29142012-08-07 16:48:22 +0000413 return fImmediateDevice->width();
414}
415
416int DeferredDevice::height() const {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000417 return fImmediateDevice->height();
junov@chromium.org88e29142012-08-07 16:48:22 +0000418}
419
420SkGpuRenderTarget* DeferredDevice::accessRenderTarget() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000421 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000422 return fImmediateDevice->accessRenderTarget();
423}
424
425void DeferredDevice::writePixels(const SkBitmap& bitmap,
426 int x, int y, SkCanvas::Config8888 config8888) {
427
428 if (x <= 0 && y <= 0 && (x + bitmap.width()) >= width() &&
429 (y + bitmap.height()) >= height()) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000430 this->skipPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000431 }
432
433 if (SkBitmap::kARGB_8888_Config == bitmap.config() &&
434 SkCanvas::kNative_Premul_Config8888 != config8888 &&
435 kPMColorAlias != config8888) {
436 //Special case config: no deferral
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000437 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000438 fImmediateDevice->writePixels(bitmap, x, y, config8888);
439 return;
440 }
441
442 SkPaint paint;
443 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000444 if (shouldDrawImmediately(&bitmap, NULL, getBitmapSizeThreshold())) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000445 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000446 fImmediateCanvas->drawSprite(bitmap, x, y, &paint);
447 } else {
448 this->recordingCanvas()->drawSprite(bitmap, x, y, &paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000449 this->recordedDrawCommand();
450
junov@chromium.org88e29142012-08-07 16:48:22 +0000451 }
452}
453
454const SkBitmap& DeferredDevice::onAccessBitmap(SkBitmap*) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000455 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000456 return fImmediateDevice->accessBitmap(false);
457}
458
459SkDevice* DeferredDevice::onCreateCompatibleDevice(
460 SkBitmap::Config config, int width, int height, bool isOpaque,
461 Usage usage) {
462
463 // Save layer usage not supported, and not required by SkDeferredCanvas.
464 SkASSERT(usage != kSaveLayer_Usage);
465 // Create a compatible non-deferred device.
466 SkAutoTUnref<SkDevice> compatibleDevice
467 (fImmediateDevice->createCompatibleDevice(config, width, height,
468 isOpaque));
junov@chromium.org67d74222013-04-12 13:33:01 +0000469 DeferredDevice* device = SkNEW_ARGS(DeferredDevice, (compatibleDevice));
470 device->setNotificationClient(fNotificationClient);
471 return device;
junov@chromium.org88e29142012-08-07 16:48:22 +0000472}
473
474bool DeferredDevice::onReadPixels(
475 const SkBitmap& bitmap, int x, int y, SkCanvas::Config8888 config8888) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000476 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000477 return fImmediateCanvas->readPixels(const_cast<SkBitmap*>(&bitmap),
478 x, y, config8888);
479}
480
sugoi@google.com7775fd52012-11-21 15:47:04 +0000481class AutoImmediateDrawIfNeeded {
482public:
483 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkBitmap* bitmap,
484 const SkPaint* paint) {
485 this->init(canvas, bitmap, paint);
486 }
487
488 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkPaint* paint) {
489 this->init(canvas, NULL, paint);
490 }
491
492 ~AutoImmediateDrawIfNeeded() {
493 if (fCanvas) {
494 fCanvas->setDeferredDrawing(true);
495 }
496 }
497private:
498 void init(SkDeferredCanvas& canvas, const SkBitmap* bitmap, const SkPaint* paint)
499 {
500 DeferredDevice* device = static_cast<DeferredDevice*>(canvas.getDevice());
501 if (canvas.isDeferredDrawing() && (NULL != device) &&
502 shouldDrawImmediately(bitmap, paint, device->getBitmapSizeThreshold())) {
503 canvas.setDeferredDrawing(false);
504 fCanvas = &canvas;
505 } else {
506 fCanvas = NULL;
507 }
508 }
509
510 SkDeferredCanvas* fCanvas;
511};
junov@chromium.org88e29142012-08-07 16:48:22 +0000512
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000513SkDeferredCanvas::SkDeferredCanvas() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000514 this->init();
junov@google.com4370aed2012-01-18 16:21:08 +0000515}
516
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000517SkDeferredCanvas::SkDeferredCanvas(SkDevice* device) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000518 this->init();
519 this->setDevice(device);
junov@google.com4370aed2012-01-18 16:21:08 +0000520}
521
junov@chromium.org67d74222013-04-12 13:33:01 +0000522SkDeferredCanvas::SkDeferredCanvas(SkSurface* surface) {
523 this->init();
524 this->INHERITED::setDevice(SkNEW_ARGS(DeferredDevice, (surface)))->unref();
525}
526
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000527void SkDeferredCanvas::init() {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000528 fDeferredDrawing = true; // On by default
junov@google.com4370aed2012-01-18 16:21:08 +0000529}
530
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000531void SkDeferredCanvas::setMaxRecordingStorage(size_t maxStorage) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000532 this->validate();
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000533 this->getDeferredDevice()->setMaxRecordingStorage(maxStorage);
534}
535
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000536size_t SkDeferredCanvas::storageAllocatedForRecording() const {
537 return this->getDeferredDevice()->storageAllocatedForRecording();
538}
539
540size_t SkDeferredCanvas::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000541 return this->getDeferredDevice()->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000542}
543
sugoi@google.com7775fd52012-11-21 15:47:04 +0000544void SkDeferredCanvas::setBitmapSizeThreshold(size_t sizeThreshold) {
545 DeferredDevice* deferredDevice = this->getDeferredDevice();
546 SkASSERT(deferredDevice);
547 deferredDevice->setBitmapSizeThreshold(sizeThreshold);
548}
549
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000550void SkDeferredCanvas::recordedDrawCommand() {
551 if (fDeferredDrawing) {
552 this->getDeferredDevice()->recordedDrawCommand();
553 }
554}
555
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000556void SkDeferredCanvas::validate() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000557 SkASSERT(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000558}
559
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000560SkCanvas* SkDeferredCanvas::drawingCanvas() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000561 this->validate();
562 return fDeferredDrawing ? this->getDeferredDevice()->recordingCanvas() :
563 this->getDeferredDevice()->immediateCanvas();
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000564}
565
junov@chromium.org88e29142012-08-07 16:48:22 +0000566SkCanvas* SkDeferredCanvas::immediateCanvas() const {
567 this->validate();
568 return this->getDeferredDevice()->immediateCanvas();
569}
570
571DeferredDevice* SkDeferredCanvas::getDeferredDevice() const {
572 return static_cast<DeferredDevice*>(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000573}
574
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000575void SkDeferredCanvas::setDeferredDrawing(bool val) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000576 this->validate(); // Must set device before calling this method
junov@google.com4370aed2012-01-18 16:21:08 +0000577 if (val != fDeferredDrawing) {
578 if (fDeferredDrawing) {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000579 // Going live.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000580 this->getDeferredDevice()->flushPendingCommands(kNormal_PlaybackMode);
junov@google.com4370aed2012-01-18 16:21:08 +0000581 }
582 fDeferredDrawing = val;
583 }
584}
585
junov@chromium.org88e29142012-08-07 16:48:22 +0000586bool SkDeferredCanvas::isDeferredDrawing() const {
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +0000587 return fDeferredDrawing;
588}
589
junov@chromium.org88e29142012-08-07 16:48:22 +0000590bool SkDeferredCanvas::isFreshFrame() const {
591 return this->getDeferredDevice()->isFreshFrame();
592}
593
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000594bool SkDeferredCanvas::hasPendingCommands() const {
595 return this->getDeferredDevice()->hasPendingCommands();
596}
597
junov@chromium.orgfb103892012-09-20 19:35:43 +0000598void SkDeferredCanvas::silentFlush() {
599 if (fDeferredDrawing) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000600 this->getDeferredDevice()->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000601 }
602}
603
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000604SkDeferredCanvas::~SkDeferredCanvas() {
junov@google.com4370aed2012-01-18 16:21:08 +0000605}
606
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000607SkDevice* SkDeferredCanvas::setDevice(SkDevice* device) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000608 this->INHERITED::setDevice(SkNEW_ARGS(DeferredDevice, (device)))->unref();
junov@google.com4370aed2012-01-18 16:21:08 +0000609 return device;
610}
611
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000612SkDeferredCanvas::NotificationClient* SkDeferredCanvas::setNotificationClient(
613 NotificationClient* notificationClient) {
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000614
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000615 DeferredDevice* deferredDevice = this->getDeferredDevice();
junov@google.com4370aed2012-01-18 16:21:08 +0000616 SkASSERT(deferredDevice);
617 if (deferredDevice) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000618 deferredDevice->setNotificationClient(notificationClient);
junov@google.com4370aed2012-01-18 16:21:08 +0000619 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000620 return notificationClient;
junov@google.com4370aed2012-01-18 16:21:08 +0000621}
622
junov@chromium.org67d74222013-04-12 13:33:01 +0000623SkImage* SkDeferredCanvas::newImageShapshot() {
624 DeferredDevice* deferredDevice = this->getDeferredDevice();
625 SkASSERT(deferredDevice);
626 return deferredDevice ? deferredDevice->newImageShapshot() : NULL;
627}
628
junov@google.com4370aed2012-01-18 16:21:08 +0000629bool 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
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000663 return this->getClipStack()->quickContains(SkRect::MakeXYWH(0, 0,
664 SkIntToScalar(canvasSize.fWidth), SkIntToScalar(canvasSize.fHeight)));
junov@google.com4370aed2012-01-18 16:21:08 +0000665}
666
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000667int SkDeferredCanvas::save(SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000668 this->drawingCanvas()->save(flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000669 int val = this->INHERITED::save(flags);
670 this->recordedDrawCommand();
671
672 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000673}
674
675int SkDeferredCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000676 SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000677 this->drawingCanvas()->saveLayer(bounds, paint, flags);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000678 int count = this->INHERITED::save(flags);
679 this->clipRectBounds(bounds, flags, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000680 this->recordedDrawCommand();
681
junov@chromium.orga907ac32012-02-24 21:54:07 +0000682 return count;
junov@google.com4370aed2012-01-18 16:21:08 +0000683}
684
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000685void SkDeferredCanvas::restore() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000686 this->drawingCanvas()->restore();
junov@chromium.orga907ac32012-02-24 21:54:07 +0000687 this->INHERITED::restore();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000688 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000689}
690
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000691bool SkDeferredCanvas::isDrawingToLayer() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000692 return this->drawingCanvas()->isDrawingToLayer();
junov@google.com4370aed2012-01-18 16:21:08 +0000693}
694
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000695bool SkDeferredCanvas::translate(SkScalar dx, SkScalar dy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000696 this->drawingCanvas()->translate(dx, dy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000697 bool val = this->INHERITED::translate(dx, dy);
698 this->recordedDrawCommand();
699 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000700}
701
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000702bool SkDeferredCanvas::scale(SkScalar sx, SkScalar sy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000703 this->drawingCanvas()->scale(sx, sy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000704 bool val = this->INHERITED::scale(sx, sy);
705 this->recordedDrawCommand();
706 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000707}
708
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000709bool SkDeferredCanvas::rotate(SkScalar degrees) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000710 this->drawingCanvas()->rotate(degrees);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000711 bool val = this->INHERITED::rotate(degrees);
712 this->recordedDrawCommand();
713 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000714}
715
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000716bool SkDeferredCanvas::skew(SkScalar sx, SkScalar sy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000717 this->drawingCanvas()->skew(sx, sy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000718 bool val = this->INHERITED::skew(sx, sy);
719 this->recordedDrawCommand();
720 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000721}
722
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000723bool SkDeferredCanvas::concat(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000724 this->drawingCanvas()->concat(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000725 bool val = this->INHERITED::concat(matrix);
726 this->recordedDrawCommand();
727 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000728}
729
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000730void SkDeferredCanvas::setMatrix(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000731 this->drawingCanvas()->setMatrix(matrix);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000732 this->INHERITED::setMatrix(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000733 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000734}
735
736bool SkDeferredCanvas::clipRect(const SkRect& rect,
737 SkRegion::Op op,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000738 bool doAntiAlias) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000739 this->drawingCanvas()->clipRect(rect, op, doAntiAlias);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000740 bool val = this->INHERITED::clipRect(rect, op, doAntiAlias);
741 this->recordedDrawCommand();
742 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000743}
744
reed@google.com4ed0fb72012-12-12 20:48:18 +0000745bool SkDeferredCanvas::clipRRect(const SkRRect& rrect,
746 SkRegion::Op op,
747 bool doAntiAlias) {
748 this->drawingCanvas()->clipRRect(rrect, op, doAntiAlias);
749 bool val = this->INHERITED::clipRRect(rrect, op, doAntiAlias);
750 this->recordedDrawCommand();
751 return val;
752}
753
junov@google.com4370aed2012-01-18 16:21:08 +0000754bool SkDeferredCanvas::clipPath(const SkPath& path,
755 SkRegion::Op op,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000756 bool doAntiAlias) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000757 this->drawingCanvas()->clipPath(path, op, doAntiAlias);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000758 bool val = this->INHERITED::clipPath(path, op, doAntiAlias);
759 this->recordedDrawCommand();
760 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000761}
762
763bool SkDeferredCanvas::clipRegion(const SkRegion& deviceRgn,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000764 SkRegion::Op op) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000765 this->drawingCanvas()->clipRegion(deviceRgn, op);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000766 bool val = this->INHERITED::clipRegion(deviceRgn, op);
767 this->recordedDrawCommand();
768 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000769}
770
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000771void SkDeferredCanvas::clear(SkColor color) {
junov@google.com4370aed2012-01-18 16:21:08 +0000772 // purge pending commands
773 if (fDeferredDrawing) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000774 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000775 }
776
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000777 this->drawingCanvas()->clear(color);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000778 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000779}
780
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000781void SkDeferredCanvas::drawPaint(const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000782 if (fDeferredDrawing && this->isFullFrame(NULL, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000783 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000784 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000785 }
junov@chromium.org10f7f972012-07-31 21:01:51 +0000786 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000787 this->drawingCanvas()->drawPaint(paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000788 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000789}
790
791void SkDeferredCanvas::drawPoints(PointMode mode, size_t count,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000792 const SkPoint pts[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000793 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000794 this->drawingCanvas()->drawPoints(mode, count, pts, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000795 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000796}
797
reed@google.com4ed0fb72012-12-12 20:48:18 +0000798void SkDeferredCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
799 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
800 this->drawingCanvas()->drawOval(rect, paint);
801 this->recordedDrawCommand();
802}
803
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000804void SkDeferredCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000805 if (fDeferredDrawing && this->isFullFrame(&rect, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000806 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000807 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000808 }
skia.committer@gmail.com306ab9d2012-12-13 02:01:33 +0000809
junov@chromium.org10f7f972012-07-31 21:01:51 +0000810 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000811 this->drawingCanvas()->drawRect(rect, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000812 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000813}
814
reed@google.com4ed0fb72012-12-12 20:48:18 +0000815void SkDeferredCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
816 if (rrect.isRect()) {
817 this->SkDeferredCanvas::drawRect(rrect.getBounds(), paint);
818 } else if (rrect.isOval()) {
819 this->SkDeferredCanvas::drawOval(rrect.getBounds(), paint);
820 } else {
821 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
822 this->drawingCanvas()->drawRRect(rrect, paint);
823 this->recordedDrawCommand();
824 }
825}
826
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000827void SkDeferredCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000828 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000829 this->drawingCanvas()->drawPath(path, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000830 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000831}
832
833void SkDeferredCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000834 SkScalar top, const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000835 SkRect bitmapRect = SkRect::MakeXYWH(left, top,
836 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000837 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000838 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000839 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000840 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000841 }
842
junov@chromium.org10f7f972012-07-31 21:01:51 +0000843 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000844 this->drawingCanvas()->drawBitmap(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000845 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000846}
847
reed@google.com71121732012-09-18 15:14:33 +0000848void SkDeferredCanvas::drawBitmapRectToRect(const SkBitmap& bitmap,
849 const SkRect* src,
850 const SkRect& dst,
851 const SkPaint* paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000852 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000853 this->isFullFrame(&dst, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000854 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000855 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000856 }
857
junov@chromium.org10f7f972012-07-31 21:01:51 +0000858 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
reed@google.com71121732012-09-18 15:14:33 +0000859 this->drawingCanvas()->drawBitmapRectToRect(bitmap, src, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000860 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000861}
862
863
864void SkDeferredCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
865 const SkMatrix& m,
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 transformed bitmap 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()->drawBitmapMatrix(bitmap, m, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000871 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000872}
873
874void SkDeferredCanvas::drawBitmapNine(const SkBitmap& bitmap,
875 const SkIRect& center, const SkRect& dst,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000876 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000877 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
878 // covers canvas entirely and dst covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000879 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000880 this->drawingCanvas()->drawBitmapNine(bitmap, center, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000881 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000882}
883
884void SkDeferredCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000885 const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000886 SkRect bitmapRect = SkRect::MakeXYWH(
887 SkIntToScalar(left),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000888 SkIntToScalar(top),
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000889 SkIntToScalar(bitmap.width()),
890 SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000891 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000892 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000893 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000894 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000895 }
896
junov@chromium.org10f7f972012-07-31 21:01:51 +0000897 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000898 this->drawingCanvas()->drawSprite(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000899 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000900}
901
902void SkDeferredCanvas::drawText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000903 SkScalar x, SkScalar y, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000904 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000905 this->drawingCanvas()->drawText(text, byteLength, x, y, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000906 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000907}
908
909void SkDeferredCanvas::drawPosText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000910 const SkPoint pos[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000911 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000912 this->drawingCanvas()->drawPosText(text, byteLength, pos, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000913 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000914}
915
916void SkDeferredCanvas::drawPosTextH(const void* text, size_t byteLength,
917 const SkScalar xpos[], SkScalar constY,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000918 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000919 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000920 this->drawingCanvas()->drawPosTextH(text, byteLength, xpos, constY, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000921 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000922}
923
924void SkDeferredCanvas::drawTextOnPath(const void* text, size_t byteLength,
925 const SkPath& path,
926 const SkMatrix* matrix,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000927 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000928 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000929 this->drawingCanvas()->drawTextOnPath(text, byteLength, path, matrix, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000930 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000931}
932
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000933void SkDeferredCanvas::drawPicture(SkPicture& picture) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000934 this->drawingCanvas()->drawPicture(picture);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000935 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000936}
937
938void SkDeferredCanvas::drawVertices(VertexMode vmode, int vertexCount,
939 const SkPoint vertices[],
940 const SkPoint texs[],
941 const SkColor colors[], SkXfermode* xmode,
942 const uint16_t indices[], int indexCount,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000943 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000944 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000945 this->drawingCanvas()->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode,
946 indices, indexCount, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000947 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000948}
949
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000950SkBounder* SkDeferredCanvas::setBounder(SkBounder* bounder) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000951 this->drawingCanvas()->setBounder(bounder);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000952 this->INHERITED::setBounder(bounder);
953 this->recordedDrawCommand();
954 return bounder;
junov@google.com4370aed2012-01-18 16:21:08 +0000955}
956
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000957SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000958 this->drawingCanvas()->setDrawFilter(filter);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000959 this->INHERITED::setDrawFilter(filter);
960 this->recordedDrawCommand();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000961 return filter;
junov@google.com4370aed2012-01-18 16:21:08 +0000962}
963
964SkCanvas* SkDeferredCanvas::canvasForDrawIter() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000965 return this->drawingCanvas();
junov@google.com4370aed2012-01-18 16:21:08 +0000966}