blob: b396a08ed7b07cf6251f3e64d3c4a365d6fc74d9 [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;}
junov@chromium.org9becf002013-04-15 18:15:23 +0000149 SkDevice* immediateDevice() const {return fImmediateCanvas->getTopDevice();}
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000150 SkImage* newImageSnapshot();
junov@chromium.org7070f762013-05-24 17:13:00 +0000151 void setSurface(SkSurface* surface);
junov@chromium.org88e29142012-08-07 16:48:22 +0000152 bool isFreshFrame();
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000153 bool hasPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000154 size_t storageAllocatedForRecording() const;
155 size_t freeMemoryIfPossible(size_t bytesToFree);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000156 size_t getBitmapSizeThreshold() const;
157 void setBitmapSizeThreshold(size_t sizeThreshold);
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000158 void flushPendingCommands(PlaybackMode);
junov@chromium.org0a67f962012-09-19 22:48:34 +0000159 void skipPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000160 void setMaxRecordingStorage(size_t);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000161 void recordedDrawCommand();
junov@chromium.org88e29142012-08-07 16:48:22 +0000162
163 virtual uint32_t getDeviceCapabilities() SK_OVERRIDE;
164 virtual int width() const SK_OVERRIDE;
165 virtual int height() const SK_OVERRIDE;
166 virtual SkGpuRenderTarget* accessRenderTarget() SK_OVERRIDE;
167
168 virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
169 int width, int height,
170 bool isOpaque,
171 Usage usage) SK_OVERRIDE;
172
173 virtual void writePixels(const SkBitmap& bitmap, int x, int y,
174 SkCanvas::Config8888 config8888) SK_OVERRIDE;
175
176protected:
177 virtual const SkBitmap& onAccessBitmap(SkBitmap*) SK_OVERRIDE;
178 virtual bool onReadPixels(const SkBitmap& bitmap,
179 int x, int y,
180 SkCanvas::Config8888 config8888) SK_OVERRIDE;
181
182 // The following methods are no-ops on a deferred device
183 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*)
184 SK_OVERRIDE
185 {return false;}
junov@chromium.org88e29142012-08-07 16:48:22 +0000186
187 // None of the following drawing methods should ever get called on the
188 // deferred device
189 virtual void clear(SkColor color)
190 {SkASSERT(0);}
191 virtual void drawPaint(const SkDraw&, const SkPaint& paint)
192 {SkASSERT(0);}
193 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
194 size_t count, const SkPoint[],
195 const SkPaint& paint)
196 {SkASSERT(0);}
197 virtual void drawRect(const SkDraw&, const SkRect& r,
198 const SkPaint& paint)
199 {SkASSERT(0);}
200 virtual void drawPath(const SkDraw&, const SkPath& path,
201 const SkPaint& paint,
202 const SkMatrix* prePathMatrix = NULL,
203 bool pathIsMutable = false)
204 {SkASSERT(0);}
205 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
206 const SkIRect* srcRectOrNull,
207 const SkMatrix& matrix, const SkPaint& paint)
208 {SkASSERT(0);}
209 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
210 int x, int y, const SkPaint& paint)
211 {SkASSERT(0);}
212 virtual void drawText(const SkDraw&, const void* text, size_t len,
213 SkScalar x, SkScalar y, const SkPaint& paint)
214 {SkASSERT(0);}
215 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
216 const SkScalar pos[], SkScalar constY,
217 int scalarsPerPos, const SkPaint& paint)
218 {SkASSERT(0);}
219 virtual void drawTextOnPath(const SkDraw&, const void* text,
220 size_t len, const SkPath& path,
221 const SkMatrix* matrix,
222 const SkPaint& paint)
223 {SkASSERT(0);}
224 virtual void drawPosTextOnPath(const SkDraw& draw, const void* text,
225 size_t len, const SkPoint pos[],
226 const SkPaint& paint,
227 const SkPath& path,
228 const SkMatrix* matrix)
229 {SkASSERT(0);}
230 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
231 int vertexCount, const SkPoint verts[],
232 const SkPoint texs[], const SkColor colors[],
233 SkXfermode* xmode, const uint16_t indices[],
234 int indexCount, const SkPaint& paint)
235 {SkASSERT(0);}
236 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
237 const SkPaint&)
238 {SkASSERT(0);}
239private:
240 virtual void flush();
241
junov@chromium.org88e29142012-08-07 16:48:22 +0000242 void beginRecording();
junov@chromium.org67d74222013-04-12 13:33:01 +0000243 void init();
junov@chromium.org88e29142012-08-07 16:48:22 +0000244
245 DeferredPipeController fPipeController;
246 SkGPipeWriter fPipeWriter;
junov@chromium.org88e29142012-08-07 16:48:22 +0000247 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;
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000252 bool fCanDiscardCanvasContents;
junov@chromium.org88e29142012-08-07 16:48:22 +0000253 size_t fMaxRecordingStorageBytes;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000254 size_t fPreviousStorageAllocated;
sugoi@google.com7775fd52012-11-21 15:47:04 +0000255 size_t fBitmapSizeThreshold;
junov@chromium.org88e29142012-08-07 16:48:22 +0000256};
257
junov@chromium.org67d74222013-04-12 13:33:01 +0000258DeferredDevice::DeferredDevice(SkDevice* immediateDevice)
259 : SkDevice(SkBitmap::kNo_Config,
260 immediateDevice->width(), immediateDevice->height(),
261 immediateDevice->isOpaque(),
262 immediateDevice->getDeviceProperties()) {
263 fSurface = NULL;
junov@chromium.org9becf002013-04-15 18:15:23 +0000264 fImmediateCanvas = SkNEW_ARGS(SkCanvas, (immediateDevice));
junov@chromium.org7070f762013-05-24 17:13:00 +0000265 fPipeController.setPlaybackCanvas(fImmediateCanvas);
junov@chromium.org67d74222013-04-12 13:33:01 +0000266 this->init();
267}
268
269DeferredDevice::DeferredDevice(SkSurface* surface)
270 : SkDevice(SkBitmap::kNo_Config,
271 surface->getCanvas()->getDevice()->width(),
272 surface->getCanvas()->getDevice()->height(),
273 surface->getCanvas()->getDevice()->isOpaque(),
274 surface->getCanvas()->getDevice()->getDeviceProperties()) {
275 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
276 fNotificationClient = NULL;
junov@chromium.org7070f762013-05-24 17:13:00 +0000277 fImmediateCanvas = NULL;
278 fSurface = NULL;
279 this->setSurface(surface);
junov@chromium.org67d74222013-04-12 13:33:01 +0000280 this->init();
281}
282
junov@chromium.org7070f762013-05-24 17:13:00 +0000283void DeferredDevice::setSurface(SkSurface* surface) {
284 SkRefCnt_SafeAssign(fImmediateCanvas, surface->getCanvas());
285 SkRefCnt_SafeAssign(fSurface, surface);
286 fPipeController.setPlaybackCanvas(fImmediateCanvas);
287}
288
junov@chromium.org67d74222013-04-12 13:33:01 +0000289void DeferredDevice::init() {
290 fRecordingCanvas = NULL;
291 fFreshFrame = true;
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000292 fCanDiscardCanvasContents = false;
junov@chromium.org67d74222013-04-12 13:33:01 +0000293 fPreviousStorageAllocated = 0;
294 fBitmapSizeThreshold = kDeferredCanvasBitmapSizeThreshold;
295 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
296 fNotificationClient = NULL;
junov@chromium.org88e29142012-08-07 16:48:22 +0000297 this->beginRecording();
298}
299
300DeferredDevice::~DeferredDevice() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000301 this->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000302 SkSafeUnref(fImmediateCanvas);
junov@chromium.org67d74222013-04-12 13:33:01 +0000303 SkSafeUnref(fSurface);
junov@chromium.org88e29142012-08-07 16:48:22 +0000304}
305
306void DeferredDevice::setMaxRecordingStorage(size_t maxStorage) {
307 fMaxRecordingStorageBytes = maxStorage;
308 this->recordingCanvas(); // Accessing the recording canvas applies the new limit.
309}
310
junov@chromium.org88e29142012-08-07 16:48:22 +0000311void DeferredDevice::beginRecording() {
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000312 SkASSERT(NULL == fRecordingCanvas);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000313 fRecordingCanvas = fPipeWriter.startRecording(&fPipeController, 0,
junov@chromium.org9becf002013-04-15 18:15:23 +0000314 immediateDevice()->width(), immediateDevice()->height());
junov@chromium.org88e29142012-08-07 16:48:22 +0000315}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000316
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000317void DeferredDevice::setNotificationClient(
318 SkDeferredCanvas::NotificationClient* notificationClient) {
junov@chromium.org52805482012-08-20 14:25:04 +0000319 fNotificationClient = notificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000320}
321
junov@chromium.org0a67f962012-09-19 22:48:34 +0000322void DeferredDevice::skipPendingCommands() {
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000323 if (!fRecordingCanvas->isDrawingToLayer()) {
324 fCanDiscardCanvasContents = true;
325 if (fPipeController.hasPendingCommands()) {
326 fFreshFrame = true;
327 flushPendingCommands(kSilent_PlaybackMode);
328 if (fNotificationClient) {
329 fNotificationClient->skippedPendingDrawCommands();
330 }
junov@google.com52a00ca2012-10-01 15:27:14 +0000331 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000332 }
333}
334
335bool DeferredDevice::isFreshFrame() {
336 bool ret = fFreshFrame;
337 fFreshFrame = false;
338 return ret;
339}
340
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000341bool DeferredDevice::hasPendingCommands() {
342 return fPipeController.hasPendingCommands();
343}
344
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000345void DeferredDevice::flushPendingCommands(PlaybackMode playbackMode) {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000346 if (!fPipeController.hasPendingCommands()) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000347 return;
348 }
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000349 if (playbackMode == kNormal_PlaybackMode) {
350 if (NULL != fNotificationClient) {
351 fNotificationClient->prepareForDraw();
352 }
353 if (fCanDiscardCanvasContents) {
354 if (NULL != fSurface) {
355 // Pre-empt notifyContentChanged(false) calls that will happen
356 // during flush
357 fSurface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
358 }
359 fCanDiscardCanvasContents = false;
360 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000361 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000362 fPipeWriter.flushRecording(true);
junov@chromium.orgd4501a02012-10-30 19:05:17 +0000363 fPipeController.playback(kSilent_PlaybackMode == playbackMode);
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000364 if (playbackMode == kNormal_PlaybackMode && fNotificationClient) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000365 fNotificationClient->flushedDrawCommands();
366 }
367 fPreviousStorageAllocated = storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000368}
369
370void DeferredDevice::flush() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000371 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000372 fImmediateCanvas->flush();
373}
374
375size_t DeferredDevice::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000376 size_t val = fPipeWriter.freeMemoryIfPossible(bytesToFree);
377 fPreviousStorageAllocated = storageAllocatedForRecording();
378 return val;
junov@chromium.org88e29142012-08-07 16:48:22 +0000379}
380
sugoi@google.com7775fd52012-11-21 15:47:04 +0000381size_t DeferredDevice::getBitmapSizeThreshold() const {
382 return fBitmapSizeThreshold;
383}
384
385void DeferredDevice::setBitmapSizeThreshold(size_t sizeThreshold) {
386 fBitmapSizeThreshold = sizeThreshold;
387}
388
junov@chromium.org88e29142012-08-07 16:48:22 +0000389size_t DeferredDevice::storageAllocatedForRecording() const {
390 return (fPipeController.storageAllocatedForRecording()
391 + fPipeWriter.storageAllocatedForRecording());
392}
393
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000394void DeferredDevice::recordedDrawCommand() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000395 size_t storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000396
junov@chromium.org88e29142012-08-07 16:48:22 +0000397 if (storageAllocated > fMaxRecordingStorageBytes) {
398 // First, attempt to reduce cache without flushing
399 size_t tryFree = storageAllocated - fMaxRecordingStorageBytes;
400 if (this->freeMemoryIfPossible(tryFree) < tryFree) {
401 // Flush is necessary to free more space.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000402 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000403 // Free as much as possible to avoid oscillating around fMaxRecordingStorageBytes
404 // which could cause a high flushing frequency.
bsalomon@google.com100abf42012-09-05 17:40:04 +0000405 this->freeMemoryIfPossible(~0U);
junov@chromium.org88e29142012-08-07 16:48:22 +0000406 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000407 storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000408 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000409
rmistry@google.comd6176b02012-08-23 18:14:13 +0000410 if (fNotificationClient &&
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000411 storageAllocated != fPreviousStorageAllocated) {
412 fPreviousStorageAllocated = storageAllocated;
413 fNotificationClient->storageAllocatedForRecordingChanged(storageAllocated);
414 }
415}
416
417SkCanvas* DeferredDevice::recordingCanvas() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000418 return fRecordingCanvas;
419}
420
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000421SkImage* DeferredDevice::newImageSnapshot() {
junov@chromium.org67d74222013-04-12 13:33:01 +0000422 this->flush();
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000423 return fSurface ? fSurface->newImageSnapshot() : NULL;
junov@chromium.org67d74222013-04-12 13:33:01 +0000424}
425
rmistry@google.comd6176b02012-08-23 18:14:13 +0000426uint32_t DeferredDevice::getDeviceCapabilities() {
junov@chromium.org9becf002013-04-15 18:15:23 +0000427 return immediateDevice()->getDeviceCapabilities();
junov@chromium.org88e29142012-08-07 16:48:22 +0000428}
429
rmistry@google.comd6176b02012-08-23 18:14:13 +0000430int DeferredDevice::width() const {
junov@chromium.org9becf002013-04-15 18:15:23 +0000431 return immediateDevice()->width();
junov@chromium.org88e29142012-08-07 16:48:22 +0000432}
433
434int DeferredDevice::height() const {
junov@chromium.org9becf002013-04-15 18:15:23 +0000435 return immediateDevice()->height();
junov@chromium.org88e29142012-08-07 16:48:22 +0000436}
437
438SkGpuRenderTarget* DeferredDevice::accessRenderTarget() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000439 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000440 return immediateDevice()->accessRenderTarget();
junov@chromium.org88e29142012-08-07 16:48:22 +0000441}
442
443void DeferredDevice::writePixels(const SkBitmap& bitmap,
444 int x, int y, SkCanvas::Config8888 config8888) {
445
446 if (x <= 0 && y <= 0 && (x + bitmap.width()) >= width() &&
447 (y + bitmap.height()) >= height()) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000448 this->skipPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000449 }
450
451 if (SkBitmap::kARGB_8888_Config == bitmap.config() &&
452 SkCanvas::kNative_Premul_Config8888 != config8888 &&
453 kPMColorAlias != config8888) {
454 //Special case config: no deferral
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000455 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000456 immediateDevice()->writePixels(bitmap, x, y, config8888);
junov@chromium.org88e29142012-08-07 16:48:22 +0000457 return;
458 }
459
460 SkPaint paint;
461 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000462 if (shouldDrawImmediately(&bitmap, NULL, getBitmapSizeThreshold())) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000463 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000464 fImmediateCanvas->drawSprite(bitmap, x, y, &paint);
465 } else {
466 this->recordingCanvas()->drawSprite(bitmap, x, y, &paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000467 this->recordedDrawCommand();
468
junov@chromium.org88e29142012-08-07 16:48:22 +0000469 }
470}
471
472const SkBitmap& DeferredDevice::onAccessBitmap(SkBitmap*) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000473 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000474 return immediateDevice()->accessBitmap(false);
junov@chromium.org88e29142012-08-07 16:48:22 +0000475}
476
477SkDevice* DeferredDevice::onCreateCompatibleDevice(
478 SkBitmap::Config config, int width, int height, bool isOpaque,
479 Usage usage) {
480
481 // Save layer usage not supported, and not required by SkDeferredCanvas.
482 SkASSERT(usage != kSaveLayer_Usage);
483 // Create a compatible non-deferred device.
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000484 // We do not create a deferred device because we know the new device
485 // will not be used with a deferred canvas (there is no API for that).
486 // And connecting a DeferredDevice to non-deferred canvas can result
487 // in unpredictable behavior.
488 return immediateDevice()->createCompatibleDevice(config, width, height, isOpaque);
junov@chromium.org88e29142012-08-07 16:48:22 +0000489}
490
491bool DeferredDevice::onReadPixels(
492 const SkBitmap& bitmap, int x, int y, SkCanvas::Config8888 config8888) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000493 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000494 return fImmediateCanvas->readPixels(const_cast<SkBitmap*>(&bitmap),
495 x, y, config8888);
496}
497
sugoi@google.com7775fd52012-11-21 15:47:04 +0000498class AutoImmediateDrawIfNeeded {
499public:
500 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkBitmap* bitmap,
501 const SkPaint* paint) {
502 this->init(canvas, bitmap, paint);
503 }
504
505 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkPaint* paint) {
506 this->init(canvas, NULL, paint);
507 }
508
509 ~AutoImmediateDrawIfNeeded() {
510 if (fCanvas) {
511 fCanvas->setDeferredDrawing(true);
512 }
513 }
514private:
515 void init(SkDeferredCanvas& canvas, const SkBitmap* bitmap, const SkPaint* paint)
516 {
517 DeferredDevice* device = static_cast<DeferredDevice*>(canvas.getDevice());
518 if (canvas.isDeferredDrawing() && (NULL != device) &&
519 shouldDrawImmediately(bitmap, paint, device->getBitmapSizeThreshold())) {
520 canvas.setDeferredDrawing(false);
521 fCanvas = &canvas;
522 } else {
523 fCanvas = NULL;
524 }
525 }
526
527 SkDeferredCanvas* fCanvas;
528};
junov@chromium.org88e29142012-08-07 16:48:22 +0000529
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000530SkDeferredCanvas::SkDeferredCanvas() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000531 this->init();
junov@google.com4370aed2012-01-18 16:21:08 +0000532}
533
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000534SkDeferredCanvas::SkDeferredCanvas(SkDevice* device) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000535 this->init();
536 this->setDevice(device);
junov@google.com4370aed2012-01-18 16:21:08 +0000537}
538
junov@chromium.org67d74222013-04-12 13:33:01 +0000539SkDeferredCanvas::SkDeferredCanvas(SkSurface* surface) {
540 this->init();
541 this->INHERITED::setDevice(SkNEW_ARGS(DeferredDevice, (surface)))->unref();
542}
543
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000544void SkDeferredCanvas::init() {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000545 fDeferredDrawing = true; // On by default
junov@google.com4370aed2012-01-18 16:21:08 +0000546}
547
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000548void SkDeferredCanvas::setMaxRecordingStorage(size_t maxStorage) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000549 this->validate();
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000550 this->getDeferredDevice()->setMaxRecordingStorage(maxStorage);
551}
552
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000553size_t SkDeferredCanvas::storageAllocatedForRecording() const {
554 return this->getDeferredDevice()->storageAllocatedForRecording();
555}
556
557size_t SkDeferredCanvas::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000558 return this->getDeferredDevice()->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000559}
560
sugoi@google.com7775fd52012-11-21 15:47:04 +0000561void SkDeferredCanvas::setBitmapSizeThreshold(size_t sizeThreshold) {
562 DeferredDevice* deferredDevice = this->getDeferredDevice();
563 SkASSERT(deferredDevice);
564 deferredDevice->setBitmapSizeThreshold(sizeThreshold);
565}
566
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000567void SkDeferredCanvas::recordedDrawCommand() {
568 if (fDeferredDrawing) {
569 this->getDeferredDevice()->recordedDrawCommand();
570 }
571}
572
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000573void SkDeferredCanvas::validate() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000574 SkASSERT(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000575}
576
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000577SkCanvas* SkDeferredCanvas::drawingCanvas() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000578 this->validate();
579 return fDeferredDrawing ? this->getDeferredDevice()->recordingCanvas() :
580 this->getDeferredDevice()->immediateCanvas();
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000581}
582
junov@chromium.org88e29142012-08-07 16:48:22 +0000583SkCanvas* SkDeferredCanvas::immediateCanvas() const {
584 this->validate();
585 return this->getDeferredDevice()->immediateCanvas();
586}
587
588DeferredDevice* SkDeferredCanvas::getDeferredDevice() const {
589 return static_cast<DeferredDevice*>(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000590}
591
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000592void SkDeferredCanvas::setDeferredDrawing(bool val) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000593 this->validate(); // Must set device before calling this method
junov@google.com4370aed2012-01-18 16:21:08 +0000594 if (val != fDeferredDrawing) {
595 if (fDeferredDrawing) {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000596 // Going live.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000597 this->getDeferredDevice()->flushPendingCommands(kNormal_PlaybackMode);
junov@google.com4370aed2012-01-18 16:21:08 +0000598 }
599 fDeferredDrawing = val;
600 }
601}
602
junov@chromium.org88e29142012-08-07 16:48:22 +0000603bool SkDeferredCanvas::isDeferredDrawing() const {
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +0000604 return fDeferredDrawing;
605}
606
junov@chromium.org88e29142012-08-07 16:48:22 +0000607bool SkDeferredCanvas::isFreshFrame() const {
608 return this->getDeferredDevice()->isFreshFrame();
609}
610
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000611bool SkDeferredCanvas::hasPendingCommands() const {
612 return this->getDeferredDevice()->hasPendingCommands();
613}
614
junov@chromium.orgfb103892012-09-20 19:35:43 +0000615void SkDeferredCanvas::silentFlush() {
616 if (fDeferredDrawing) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000617 this->getDeferredDevice()->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000618 }
619}
620
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000621SkDeferredCanvas::~SkDeferredCanvas() {
junov@google.com4370aed2012-01-18 16:21:08 +0000622}
623
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000624SkDevice* SkDeferredCanvas::setDevice(SkDevice* device) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000625 this->INHERITED::setDevice(SkNEW_ARGS(DeferredDevice, (device)))->unref();
junov@google.com4370aed2012-01-18 16:21:08 +0000626 return device;
627}
628
junov@chromium.org7070f762013-05-24 17:13:00 +0000629SkSurface* SkDeferredCanvas::setSurface(SkSurface* surface) {
630 DeferredDevice* deferredDevice = this->getDeferredDevice();
631 if (NULL != deferredDevice) {
632 // By swapping the surface into the existing device, we preserve
633 // all pending commands, which can help to seamlessly recover from
634 // a lost accelerated graphics context.
635 deferredDevice->setSurface(surface);
636 } else {
637 this->INHERITED::setDevice(SkNEW_ARGS(DeferredDevice, (surface)))->unref();
638 }
639 return surface;
640}
641
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000642SkDeferredCanvas::NotificationClient* SkDeferredCanvas::setNotificationClient(
643 NotificationClient* notificationClient) {
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000644
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000645 DeferredDevice* deferredDevice = this->getDeferredDevice();
junov@google.com4370aed2012-01-18 16:21:08 +0000646 SkASSERT(deferredDevice);
647 if (deferredDevice) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000648 deferredDevice->setNotificationClient(notificationClient);
junov@google.com4370aed2012-01-18 16:21:08 +0000649 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000650 return notificationClient;
junov@google.com4370aed2012-01-18 16:21:08 +0000651}
652
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000653SkImage* SkDeferredCanvas::newImageSnapshot() {
junov@chromium.org67d74222013-04-12 13:33:01 +0000654 DeferredDevice* deferredDevice = this->getDeferredDevice();
655 SkASSERT(deferredDevice);
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000656 return deferredDevice ? deferredDevice->newImageSnapshot() : NULL;
junov@chromium.org67d74222013-04-12 13:33:01 +0000657}
658
junov@google.com4370aed2012-01-18 16:21:08 +0000659bool SkDeferredCanvas::isFullFrame(const SkRect* rect,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000660 const SkPaint* paint) const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000661 SkCanvas* canvas = this->drawingCanvas();
662 SkISize canvasSize = this->getDeviceSize();
junov@google.com4370aed2012-01-18 16:21:08 +0000663 if (rect) {
664 if (!canvas->getTotalMatrix().rectStaysRect()) {
665 return false; // conservative
666 }
667
668 SkRect transformedRect;
669 canvas->getTotalMatrix().mapRect(&transformedRect, *rect);
670
671 if (paint) {
672 SkPaint::Style paintStyle = paint->getStyle();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000673 if (!(paintStyle == SkPaint::kFill_Style ||
junov@google.com4370aed2012-01-18 16:21:08 +0000674 paintStyle == SkPaint::kStrokeAndFill_Style)) {
675 return false;
676 }
677 if (paint->getMaskFilter() || paint->getLooper()
678 || paint->getPathEffect() || paint->getImageFilter()) {
679 return false; // conservative
680 }
681 }
682
683 // The following test holds with AA enabled, and is conservative
684 // by a 0.5 pixel margin with AA disabled
rmistry@google.comd6176b02012-08-23 18:14:13 +0000685 if (transformedRect.fLeft > SkIntToScalar(0) ||
686 transformedRect.fTop > SkIntToScalar(0) ||
junov@chromium.orgb1e218e2012-02-13 22:27:58 +0000687 transformedRect.fRight < SkIntToScalar(canvasSize.fWidth) ||
688 transformedRect.fBottom < SkIntToScalar(canvasSize.fHeight)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000689 return false;
690 }
691 }
692
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000693 return this->getClipStack()->quickContains(SkRect::MakeXYWH(0, 0,
694 SkIntToScalar(canvasSize.fWidth), SkIntToScalar(canvasSize.fHeight)));
junov@google.com4370aed2012-01-18 16:21:08 +0000695}
696
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000697int SkDeferredCanvas::save(SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000698 this->drawingCanvas()->save(flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000699 int val = this->INHERITED::save(flags);
700 this->recordedDrawCommand();
701
702 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000703}
704
705int SkDeferredCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000706 SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000707 this->drawingCanvas()->saveLayer(bounds, paint, flags);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000708 int count = this->INHERITED::save(flags);
709 this->clipRectBounds(bounds, flags, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000710 this->recordedDrawCommand();
711
junov@chromium.orga907ac32012-02-24 21:54:07 +0000712 return count;
junov@google.com4370aed2012-01-18 16:21:08 +0000713}
714
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000715void SkDeferredCanvas::restore() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000716 this->drawingCanvas()->restore();
junov@chromium.orga907ac32012-02-24 21:54:07 +0000717 this->INHERITED::restore();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000718 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000719}
720
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000721bool SkDeferredCanvas::isDrawingToLayer() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000722 return this->drawingCanvas()->isDrawingToLayer();
junov@google.com4370aed2012-01-18 16:21:08 +0000723}
724
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000725bool SkDeferredCanvas::translate(SkScalar dx, SkScalar dy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000726 this->drawingCanvas()->translate(dx, dy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000727 bool val = this->INHERITED::translate(dx, dy);
728 this->recordedDrawCommand();
729 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000730}
731
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000732bool SkDeferredCanvas::scale(SkScalar sx, SkScalar sy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000733 this->drawingCanvas()->scale(sx, sy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000734 bool val = this->INHERITED::scale(sx, sy);
735 this->recordedDrawCommand();
736 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000737}
738
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000739bool SkDeferredCanvas::rotate(SkScalar degrees) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000740 this->drawingCanvas()->rotate(degrees);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000741 bool val = this->INHERITED::rotate(degrees);
742 this->recordedDrawCommand();
743 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000744}
745
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000746bool SkDeferredCanvas::skew(SkScalar sx, SkScalar sy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000747 this->drawingCanvas()->skew(sx, sy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000748 bool val = this->INHERITED::skew(sx, sy);
749 this->recordedDrawCommand();
750 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000751}
752
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000753bool SkDeferredCanvas::concat(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000754 this->drawingCanvas()->concat(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000755 bool val = this->INHERITED::concat(matrix);
756 this->recordedDrawCommand();
757 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000758}
759
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000760void SkDeferredCanvas::setMatrix(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000761 this->drawingCanvas()->setMatrix(matrix);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000762 this->INHERITED::setMatrix(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000763 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000764}
765
766bool SkDeferredCanvas::clipRect(const SkRect& rect,
767 SkRegion::Op op,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000768 bool doAntiAlias) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000769 this->drawingCanvas()->clipRect(rect, op, doAntiAlias);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000770 bool val = this->INHERITED::clipRect(rect, op, doAntiAlias);
771 this->recordedDrawCommand();
772 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000773}
774
reed@google.com4ed0fb72012-12-12 20:48:18 +0000775bool SkDeferredCanvas::clipRRect(const SkRRect& rrect,
776 SkRegion::Op op,
777 bool doAntiAlias) {
778 this->drawingCanvas()->clipRRect(rrect, op, doAntiAlias);
779 bool val = this->INHERITED::clipRRect(rrect, op, doAntiAlias);
780 this->recordedDrawCommand();
781 return val;
782}
783
junov@google.com4370aed2012-01-18 16:21:08 +0000784bool SkDeferredCanvas::clipPath(const SkPath& path,
785 SkRegion::Op op,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000786 bool doAntiAlias) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000787 this->drawingCanvas()->clipPath(path, op, doAntiAlias);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000788 bool val = this->INHERITED::clipPath(path, op, doAntiAlias);
789 this->recordedDrawCommand();
790 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000791}
792
793bool SkDeferredCanvas::clipRegion(const SkRegion& deviceRgn,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000794 SkRegion::Op op) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000795 this->drawingCanvas()->clipRegion(deviceRgn, op);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000796 bool val = this->INHERITED::clipRegion(deviceRgn, op);
797 this->recordedDrawCommand();
798 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000799}
800
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000801void SkDeferredCanvas::clear(SkColor color) {
junov@google.com4370aed2012-01-18 16:21:08 +0000802 // purge pending commands
803 if (fDeferredDrawing) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000804 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000805 }
806
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000807 this->drawingCanvas()->clear(color);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000808 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000809}
810
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000811void SkDeferredCanvas::drawPaint(const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000812 if (fDeferredDrawing && this->isFullFrame(NULL, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000813 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000814 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000815 }
junov@chromium.org10f7f972012-07-31 21:01:51 +0000816 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000817 this->drawingCanvas()->drawPaint(paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000818 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000819}
820
821void SkDeferredCanvas::drawPoints(PointMode mode, size_t count,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000822 const SkPoint pts[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000823 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000824 this->drawingCanvas()->drawPoints(mode, count, pts, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000825 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000826}
827
reed@google.com4ed0fb72012-12-12 20:48:18 +0000828void SkDeferredCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
829 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
830 this->drawingCanvas()->drawOval(rect, paint);
831 this->recordedDrawCommand();
832}
833
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000834void SkDeferredCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000835 if (fDeferredDrawing && this->isFullFrame(&rect, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000836 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000837 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000838 }
skia.committer@gmail.com306ab9d2012-12-13 02:01:33 +0000839
junov@chromium.org10f7f972012-07-31 21:01:51 +0000840 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000841 this->drawingCanvas()->drawRect(rect, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000842 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000843}
844
reed@google.com4ed0fb72012-12-12 20:48:18 +0000845void SkDeferredCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
846 if (rrect.isRect()) {
847 this->SkDeferredCanvas::drawRect(rrect.getBounds(), paint);
848 } else if (rrect.isOval()) {
849 this->SkDeferredCanvas::drawOval(rrect.getBounds(), paint);
850 } else {
851 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
852 this->drawingCanvas()->drawRRect(rrect, paint);
853 this->recordedDrawCommand();
854 }
855}
856
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000857void SkDeferredCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000858 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000859 this->drawingCanvas()->drawPath(path, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000860 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000861}
862
863void SkDeferredCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000864 SkScalar top, const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000865 SkRect bitmapRect = SkRect::MakeXYWH(left, top,
866 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000867 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000868 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000869 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000870 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000871 }
872
junov@chromium.org10f7f972012-07-31 21:01:51 +0000873 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000874 this->drawingCanvas()->drawBitmap(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000875 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000876}
877
reed@google.com71121732012-09-18 15:14:33 +0000878void SkDeferredCanvas::drawBitmapRectToRect(const SkBitmap& bitmap,
879 const SkRect* src,
880 const SkRect& dst,
881 const SkPaint* paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000882 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000883 this->isFullFrame(&dst, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000884 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000885 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000886 }
887
junov@chromium.org10f7f972012-07-31 21:01:51 +0000888 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
reed@google.com71121732012-09-18 15:14:33 +0000889 this->drawingCanvas()->drawBitmapRectToRect(bitmap, src, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000890 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000891}
892
893
894void SkDeferredCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
895 const SkMatrix& m,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000896 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000897 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
898 // covers canvas entirely and transformed bitmap covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000899 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000900 this->drawingCanvas()->drawBitmapMatrix(bitmap, m, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000901 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000902}
903
904void SkDeferredCanvas::drawBitmapNine(const SkBitmap& bitmap,
905 const SkIRect& center, const SkRect& dst,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000906 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000907 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
908 // covers canvas entirely and dst covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000909 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000910 this->drawingCanvas()->drawBitmapNine(bitmap, center, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000911 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000912}
913
914void SkDeferredCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000915 const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000916 SkRect bitmapRect = SkRect::MakeXYWH(
917 SkIntToScalar(left),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000918 SkIntToScalar(top),
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000919 SkIntToScalar(bitmap.width()),
920 SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000921 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000922 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000923 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000924 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000925 }
926
junov@chromium.org10f7f972012-07-31 21:01:51 +0000927 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000928 this->drawingCanvas()->drawSprite(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000929 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000930}
931
932void SkDeferredCanvas::drawText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000933 SkScalar x, SkScalar y, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000934 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000935 this->drawingCanvas()->drawText(text, byteLength, x, y, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000936 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000937}
938
939void SkDeferredCanvas::drawPosText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000940 const SkPoint pos[], 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()->drawPosText(text, byteLength, pos, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000943 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000944}
945
946void SkDeferredCanvas::drawPosTextH(const void* text, size_t byteLength,
947 const SkScalar xpos[], SkScalar constY,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000948 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000949 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000950 this->drawingCanvas()->drawPosTextH(text, byteLength, xpos, constY, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000951 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000952}
953
954void SkDeferredCanvas::drawTextOnPath(const void* text, size_t byteLength,
955 const SkPath& path,
956 const SkMatrix* matrix,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000957 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000958 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000959 this->drawingCanvas()->drawTextOnPath(text, byteLength, path, matrix, 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 +0000963void SkDeferredCanvas::drawPicture(SkPicture& picture) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000964 this->drawingCanvas()->drawPicture(picture);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000965 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000966}
967
968void SkDeferredCanvas::drawVertices(VertexMode vmode, int vertexCount,
969 const SkPoint vertices[],
970 const SkPoint texs[],
971 const SkColor colors[], SkXfermode* xmode,
972 const uint16_t indices[], int indexCount,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000973 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000974 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000975 this->drawingCanvas()->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode,
976 indices, indexCount, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000977 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000978}
979
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000980SkBounder* SkDeferredCanvas::setBounder(SkBounder* bounder) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000981 this->drawingCanvas()->setBounder(bounder);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000982 this->INHERITED::setBounder(bounder);
983 this->recordedDrawCommand();
984 return bounder;
junov@google.com4370aed2012-01-18 16:21:08 +0000985}
986
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000987SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000988 this->drawingCanvas()->setDrawFilter(filter);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000989 this->INHERITED::setDrawFilter(filter);
990 this->recordedDrawCommand();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000991 return filter;
junov@google.com4370aed2012-01-18 16:21:08 +0000992}
993
994SkCanvas* SkDeferredCanvas::canvasForDrawIter() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000995 return this->drawingCanvas();
junov@google.com4370aed2012-01-18 16:21:08 +0000996}