blob: 42e95370eb4988eec7a9846d971ee3b430bf66fb [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.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;
junov@chromium.org88e29142012-08-07 16:48:22 +0000246 SkCanvas* fImmediateCanvas;
247 SkCanvas* fRecordingCanvas;
junov@chromium.org67d74222013-04-12 13:33:01 +0000248 SkSurface* fSurface;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000249 SkDeferredCanvas::NotificationClient* fNotificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000250 bool fFreshFrame;
251 size_t fMaxRecordingStorageBytes;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000252 size_t fPreviousStorageAllocated;
sugoi@google.com7775fd52012-11-21 15:47:04 +0000253 size_t fBitmapSizeThreshold;
junov@chromium.org88e29142012-08-07 16:48:22 +0000254};
255
junov@chromium.org67d74222013-04-12 13:33:01 +0000256DeferredDevice::DeferredDevice(SkDevice* immediateDevice)
257 : SkDevice(SkBitmap::kNo_Config,
258 immediateDevice->width(), immediateDevice->height(),
259 immediateDevice->isOpaque(),
260 immediateDevice->getDeviceProperties()) {
261 fSurface = NULL;
junov@chromium.org9becf002013-04-15 18:15:23 +0000262 fImmediateCanvas = SkNEW_ARGS(SkCanvas, (immediateDevice));
junov@chromium.org67d74222013-04-12 13:33:01 +0000263 this->init();
264}
265
266DeferredDevice::DeferredDevice(SkSurface* surface)
267 : SkDevice(SkBitmap::kNo_Config,
268 surface->getCanvas()->getDevice()->width(),
269 surface->getCanvas()->getDevice()->height(),
270 surface->getCanvas()->getDevice()->isOpaque(),
271 surface->getCanvas()->getDevice()->getDeviceProperties()) {
272 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
273 fNotificationClient = NULL;
274 fImmediateCanvas = surface->getCanvas();
275 SkSafeRef(fImmediateCanvas);
276 fSurface = surface;
277 SkSafeRef(fSurface);
junov@chromium.org67d74222013-04-12 13:33:01 +0000278 this->init();
279}
280
281void DeferredDevice::init() {
282 fRecordingCanvas = NULL;
283 fFreshFrame = true;
284 fPreviousStorageAllocated = 0;
285 fBitmapSizeThreshold = kDeferredCanvasBitmapSizeThreshold;
286 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
287 fNotificationClient = NULL;
junov@chromium.org88e29142012-08-07 16:48:22 +0000288 fPipeController.setPlaybackCanvas(fImmediateCanvas);
289 this->beginRecording();
290}
291
292DeferredDevice::~DeferredDevice() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000293 this->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000294 SkSafeUnref(fImmediateCanvas);
junov@chromium.org67d74222013-04-12 13:33:01 +0000295 SkSafeUnref(fSurface);
junov@chromium.org88e29142012-08-07 16:48:22 +0000296}
297
298void DeferredDevice::setMaxRecordingStorage(size_t maxStorage) {
299 fMaxRecordingStorageBytes = maxStorage;
300 this->recordingCanvas(); // Accessing the recording canvas applies the new limit.
301}
302
junov@chromium.org88e29142012-08-07 16:48:22 +0000303void DeferredDevice::beginRecording() {
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000304 SkASSERT(NULL == fRecordingCanvas);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000305 fRecordingCanvas = fPipeWriter.startRecording(&fPipeController, 0,
junov@chromium.org9becf002013-04-15 18:15:23 +0000306 immediateDevice()->width(), immediateDevice()->height());
junov@chromium.org88e29142012-08-07 16:48:22 +0000307}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000308
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000309void DeferredDevice::setNotificationClient(
310 SkDeferredCanvas::NotificationClient* notificationClient) {
junov@chromium.org52805482012-08-20 14:25:04 +0000311 fNotificationClient = notificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000312}
313
junov@chromium.org0a67f962012-09-19 22:48:34 +0000314void DeferredDevice::skipPendingCommands() {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000315 if (!fRecordingCanvas->isDrawingToLayer() && fPipeController.hasPendingCommands()) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000316 fFreshFrame = true;
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000317 flushPendingCommands(kSilent_PlaybackMode);
junov@google.com52a00ca2012-10-01 15:27:14 +0000318 if (fNotificationClient) {
319 fNotificationClient->skippedPendingDrawCommands();
320 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000321 }
322}
323
324bool DeferredDevice::isFreshFrame() {
325 bool ret = fFreshFrame;
326 fFreshFrame = false;
327 return ret;
328}
329
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000330bool DeferredDevice::hasPendingCommands() {
331 return fPipeController.hasPendingCommands();
332}
333
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000334void DeferredDevice::flushPendingCommands(PlaybackMode playbackMode) {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000335 if (!fPipeController.hasPendingCommands()) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000336 return;
337 }
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000338 if (playbackMode == kNormal_PlaybackMode && fNotificationClient) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000339 fNotificationClient->prepareForDraw();
junov@chromium.org88e29142012-08-07 16:48:22 +0000340 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000341 fPipeWriter.flushRecording(true);
junov@chromium.orgd4501a02012-10-30 19:05:17 +0000342 fPipeController.playback(kSilent_PlaybackMode == playbackMode);
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000343 if (playbackMode == kNormal_PlaybackMode && fNotificationClient) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000344 fNotificationClient->flushedDrawCommands();
345 }
346 fPreviousStorageAllocated = storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000347}
348
349void DeferredDevice::flush() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000350 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000351 fImmediateCanvas->flush();
352}
353
354size_t DeferredDevice::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000355 size_t val = fPipeWriter.freeMemoryIfPossible(bytesToFree);
356 fPreviousStorageAllocated = storageAllocatedForRecording();
357 return val;
junov@chromium.org88e29142012-08-07 16:48:22 +0000358}
359
sugoi@google.com7775fd52012-11-21 15:47:04 +0000360size_t DeferredDevice::getBitmapSizeThreshold() const {
361 return fBitmapSizeThreshold;
362}
363
364void DeferredDevice::setBitmapSizeThreshold(size_t sizeThreshold) {
365 fBitmapSizeThreshold = sizeThreshold;
366}
367
junov@chromium.org88e29142012-08-07 16:48:22 +0000368size_t DeferredDevice::storageAllocatedForRecording() const {
369 return (fPipeController.storageAllocatedForRecording()
370 + fPipeWriter.storageAllocatedForRecording());
371}
372
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000373void DeferredDevice::recordedDrawCommand() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000374 size_t storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000375
junov@chromium.org88e29142012-08-07 16:48:22 +0000376 if (storageAllocated > fMaxRecordingStorageBytes) {
377 // First, attempt to reduce cache without flushing
378 size_t tryFree = storageAllocated - fMaxRecordingStorageBytes;
379 if (this->freeMemoryIfPossible(tryFree) < tryFree) {
380 // Flush is necessary to free more space.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000381 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000382 // Free as much as possible to avoid oscillating around fMaxRecordingStorageBytes
383 // which could cause a high flushing frequency.
bsalomon@google.com100abf42012-09-05 17:40:04 +0000384 this->freeMemoryIfPossible(~0U);
junov@chromium.org88e29142012-08-07 16:48:22 +0000385 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000386 storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000387 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000388
rmistry@google.comd6176b02012-08-23 18:14:13 +0000389 if (fNotificationClient &&
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000390 storageAllocated != fPreviousStorageAllocated) {
391 fPreviousStorageAllocated = storageAllocated;
392 fNotificationClient->storageAllocatedForRecordingChanged(storageAllocated);
393 }
394}
395
396SkCanvas* DeferredDevice::recordingCanvas() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000397 return fRecordingCanvas;
398}
399
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000400SkImage* DeferredDevice::newImageSnapshot() {
junov@chromium.org67d74222013-04-12 13:33:01 +0000401 this->flush();
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000402 return fSurface ? fSurface->newImageSnapshot() : NULL;
junov@chromium.org67d74222013-04-12 13:33:01 +0000403}
404
rmistry@google.comd6176b02012-08-23 18:14:13 +0000405uint32_t DeferredDevice::getDeviceCapabilities() {
junov@chromium.org9becf002013-04-15 18:15:23 +0000406 return immediateDevice()->getDeviceCapabilities();
junov@chromium.org88e29142012-08-07 16:48:22 +0000407}
408
rmistry@google.comd6176b02012-08-23 18:14:13 +0000409int DeferredDevice::width() const {
junov@chromium.org9becf002013-04-15 18:15:23 +0000410 return immediateDevice()->width();
junov@chromium.org88e29142012-08-07 16:48:22 +0000411}
412
413int DeferredDevice::height() const {
junov@chromium.org9becf002013-04-15 18:15:23 +0000414 return immediateDevice()->height();
junov@chromium.org88e29142012-08-07 16:48:22 +0000415}
416
417SkGpuRenderTarget* DeferredDevice::accessRenderTarget() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000418 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000419 return immediateDevice()->accessRenderTarget();
junov@chromium.org88e29142012-08-07 16:48:22 +0000420}
421
422void DeferredDevice::writePixels(const SkBitmap& bitmap,
423 int x, int y, SkCanvas::Config8888 config8888) {
424
425 if (x <= 0 && y <= 0 && (x + bitmap.width()) >= width() &&
426 (y + bitmap.height()) >= height()) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000427 this->skipPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000428 }
429
430 if (SkBitmap::kARGB_8888_Config == bitmap.config() &&
431 SkCanvas::kNative_Premul_Config8888 != config8888 &&
432 kPMColorAlias != config8888) {
433 //Special case config: no deferral
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000434 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000435 immediateDevice()->writePixels(bitmap, x, y, config8888);
junov@chromium.org88e29142012-08-07 16:48:22 +0000436 return;
437 }
438
439 SkPaint paint;
440 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000441 if (shouldDrawImmediately(&bitmap, NULL, getBitmapSizeThreshold())) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000442 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000443 fImmediateCanvas->drawSprite(bitmap, x, y, &paint);
444 } else {
445 this->recordingCanvas()->drawSprite(bitmap, x, y, &paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000446 this->recordedDrawCommand();
447
junov@chromium.org88e29142012-08-07 16:48:22 +0000448 }
449}
450
451const SkBitmap& DeferredDevice::onAccessBitmap(SkBitmap*) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000452 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000453 return immediateDevice()->accessBitmap(false);
junov@chromium.org88e29142012-08-07 16:48:22 +0000454}
455
456SkDevice* DeferredDevice::onCreateCompatibleDevice(
457 SkBitmap::Config config, int width, int height, bool isOpaque,
458 Usage usage) {
459
460 // Save layer usage not supported, and not required by SkDeferredCanvas.
461 SkASSERT(usage != kSaveLayer_Usage);
462 // Create a compatible non-deferred device.
463 SkAutoTUnref<SkDevice> compatibleDevice
junov@chromium.org9becf002013-04-15 18:15:23 +0000464 (immediateDevice()->createCompatibleDevice(config, width, height,
junov@chromium.org88e29142012-08-07 16:48:22 +0000465 isOpaque));
junov@chromium.org67d74222013-04-12 13:33:01 +0000466 DeferredDevice* device = SkNEW_ARGS(DeferredDevice, (compatibleDevice));
467 device->setNotificationClient(fNotificationClient);
468 return device;
junov@chromium.org88e29142012-08-07 16:48:22 +0000469}
470
471bool DeferredDevice::onReadPixels(
472 const SkBitmap& bitmap, int x, int y, SkCanvas::Config8888 config8888) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000473 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000474 return fImmediateCanvas->readPixels(const_cast<SkBitmap*>(&bitmap),
475 x, y, config8888);
476}
477
sugoi@google.com7775fd52012-11-21 15:47:04 +0000478class AutoImmediateDrawIfNeeded {
479public:
480 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkBitmap* bitmap,
481 const SkPaint* paint) {
482 this->init(canvas, bitmap, paint);
483 }
484
485 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkPaint* paint) {
486 this->init(canvas, NULL, paint);
487 }
488
489 ~AutoImmediateDrawIfNeeded() {
490 if (fCanvas) {
491 fCanvas->setDeferredDrawing(true);
492 }
493 }
494private:
495 void init(SkDeferredCanvas& canvas, const SkBitmap* bitmap, const SkPaint* paint)
496 {
497 DeferredDevice* device = static_cast<DeferredDevice*>(canvas.getDevice());
498 if (canvas.isDeferredDrawing() && (NULL != device) &&
499 shouldDrawImmediately(bitmap, paint, device->getBitmapSizeThreshold())) {
500 canvas.setDeferredDrawing(false);
501 fCanvas = &canvas;
502 } else {
503 fCanvas = NULL;
504 }
505 }
506
507 SkDeferredCanvas* fCanvas;
508};
junov@chromium.org88e29142012-08-07 16:48:22 +0000509
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000510SkDeferredCanvas::SkDeferredCanvas() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000511 this->init();
junov@google.com4370aed2012-01-18 16:21:08 +0000512}
513
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000514SkDeferredCanvas::SkDeferredCanvas(SkDevice* device) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000515 this->init();
516 this->setDevice(device);
junov@google.com4370aed2012-01-18 16:21:08 +0000517}
518
junov@chromium.org67d74222013-04-12 13:33:01 +0000519SkDeferredCanvas::SkDeferredCanvas(SkSurface* surface) {
520 this->init();
521 this->INHERITED::setDevice(SkNEW_ARGS(DeferredDevice, (surface)))->unref();
522}
523
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000524void SkDeferredCanvas::init() {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000525 fDeferredDrawing = true; // On by default
junov@google.com4370aed2012-01-18 16:21:08 +0000526}
527
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000528void SkDeferredCanvas::setMaxRecordingStorage(size_t maxStorage) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000529 this->validate();
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000530 this->getDeferredDevice()->setMaxRecordingStorage(maxStorage);
531}
532
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000533size_t SkDeferredCanvas::storageAllocatedForRecording() const {
534 return this->getDeferredDevice()->storageAllocatedForRecording();
535}
536
537size_t SkDeferredCanvas::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000538 return this->getDeferredDevice()->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000539}
540
sugoi@google.com7775fd52012-11-21 15:47:04 +0000541void SkDeferredCanvas::setBitmapSizeThreshold(size_t sizeThreshold) {
542 DeferredDevice* deferredDevice = this->getDeferredDevice();
543 SkASSERT(deferredDevice);
544 deferredDevice->setBitmapSizeThreshold(sizeThreshold);
545}
546
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000547void SkDeferredCanvas::recordedDrawCommand() {
548 if (fDeferredDrawing) {
549 this->getDeferredDevice()->recordedDrawCommand();
550 }
551}
552
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000553void SkDeferredCanvas::validate() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000554 SkASSERT(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000555}
556
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000557SkCanvas* SkDeferredCanvas::drawingCanvas() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000558 this->validate();
559 return fDeferredDrawing ? this->getDeferredDevice()->recordingCanvas() :
560 this->getDeferredDevice()->immediateCanvas();
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000561}
562
junov@chromium.org88e29142012-08-07 16:48:22 +0000563SkCanvas* SkDeferredCanvas::immediateCanvas() const {
564 this->validate();
565 return this->getDeferredDevice()->immediateCanvas();
566}
567
568DeferredDevice* SkDeferredCanvas::getDeferredDevice() const {
569 return static_cast<DeferredDevice*>(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000570}
571
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000572void SkDeferredCanvas::setDeferredDrawing(bool val) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000573 this->validate(); // Must set device before calling this method
junov@google.com4370aed2012-01-18 16:21:08 +0000574 if (val != fDeferredDrawing) {
575 if (fDeferredDrawing) {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000576 // Going live.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000577 this->getDeferredDevice()->flushPendingCommands(kNormal_PlaybackMode);
junov@google.com4370aed2012-01-18 16:21:08 +0000578 }
579 fDeferredDrawing = val;
580 }
581}
582
junov@chromium.org88e29142012-08-07 16:48:22 +0000583bool SkDeferredCanvas::isDeferredDrawing() const {
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +0000584 return fDeferredDrawing;
585}
586
junov@chromium.org88e29142012-08-07 16:48:22 +0000587bool SkDeferredCanvas::isFreshFrame() const {
588 return this->getDeferredDevice()->isFreshFrame();
589}
590
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000591bool SkDeferredCanvas::hasPendingCommands() const {
592 return this->getDeferredDevice()->hasPendingCommands();
593}
594
junov@chromium.orgfb103892012-09-20 19:35:43 +0000595void SkDeferredCanvas::silentFlush() {
596 if (fDeferredDrawing) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000597 this->getDeferredDevice()->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000598 }
599}
600
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000601SkDeferredCanvas::~SkDeferredCanvas() {
junov@google.com4370aed2012-01-18 16:21:08 +0000602}
603
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000604SkDevice* SkDeferredCanvas::setDevice(SkDevice* device) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000605 this->INHERITED::setDevice(SkNEW_ARGS(DeferredDevice, (device)))->unref();
junov@google.com4370aed2012-01-18 16:21:08 +0000606 return device;
607}
608
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000609SkDeferredCanvas::NotificationClient* SkDeferredCanvas::setNotificationClient(
610 NotificationClient* notificationClient) {
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000611
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000612 DeferredDevice* deferredDevice = this->getDeferredDevice();
junov@google.com4370aed2012-01-18 16:21:08 +0000613 SkASSERT(deferredDevice);
614 if (deferredDevice) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000615 deferredDevice->setNotificationClient(notificationClient);
junov@google.com4370aed2012-01-18 16:21:08 +0000616 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000617 return notificationClient;
junov@google.com4370aed2012-01-18 16:21:08 +0000618}
619
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000620SkImage* SkDeferredCanvas::newImageSnapshot() {
junov@chromium.org67d74222013-04-12 13:33:01 +0000621 DeferredDevice* deferredDevice = this->getDeferredDevice();
622 SkASSERT(deferredDevice);
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000623 return deferredDevice ? deferredDevice->newImageSnapshot() : NULL;
junov@chromium.org67d74222013-04-12 13:33:01 +0000624}
625
junov@google.com4370aed2012-01-18 16:21:08 +0000626bool SkDeferredCanvas::isFullFrame(const SkRect* rect,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000627 const SkPaint* paint) const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000628 SkCanvas* canvas = this->drawingCanvas();
629 SkISize canvasSize = this->getDeviceSize();
junov@google.com4370aed2012-01-18 16:21:08 +0000630 if (rect) {
631 if (!canvas->getTotalMatrix().rectStaysRect()) {
632 return false; // conservative
633 }
634
635 SkRect transformedRect;
636 canvas->getTotalMatrix().mapRect(&transformedRect, *rect);
637
638 if (paint) {
639 SkPaint::Style paintStyle = paint->getStyle();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000640 if (!(paintStyle == SkPaint::kFill_Style ||
junov@google.com4370aed2012-01-18 16:21:08 +0000641 paintStyle == SkPaint::kStrokeAndFill_Style)) {
642 return false;
643 }
644 if (paint->getMaskFilter() || paint->getLooper()
645 || paint->getPathEffect() || paint->getImageFilter()) {
646 return false; // conservative
647 }
648 }
649
650 // The following test holds with AA enabled, and is conservative
651 // by a 0.5 pixel margin with AA disabled
rmistry@google.comd6176b02012-08-23 18:14:13 +0000652 if (transformedRect.fLeft > SkIntToScalar(0) ||
653 transformedRect.fTop > SkIntToScalar(0) ||
junov@chromium.orgb1e218e2012-02-13 22:27:58 +0000654 transformedRect.fRight < SkIntToScalar(canvasSize.fWidth) ||
655 transformedRect.fBottom < SkIntToScalar(canvasSize.fHeight)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000656 return false;
657 }
658 }
659
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000660 return this->getClipStack()->quickContains(SkRect::MakeXYWH(0, 0,
661 SkIntToScalar(canvasSize.fWidth), SkIntToScalar(canvasSize.fHeight)));
junov@google.com4370aed2012-01-18 16:21:08 +0000662}
663
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000664int SkDeferredCanvas::save(SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000665 this->drawingCanvas()->save(flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000666 int val = this->INHERITED::save(flags);
667 this->recordedDrawCommand();
668
669 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000670}
671
672int SkDeferredCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000673 SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000674 this->drawingCanvas()->saveLayer(bounds, paint, flags);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000675 int count = this->INHERITED::save(flags);
676 this->clipRectBounds(bounds, flags, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000677 this->recordedDrawCommand();
678
junov@chromium.orga907ac32012-02-24 21:54:07 +0000679 return count;
junov@google.com4370aed2012-01-18 16:21:08 +0000680}
681
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000682void SkDeferredCanvas::restore() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000683 this->drawingCanvas()->restore();
junov@chromium.orga907ac32012-02-24 21:54:07 +0000684 this->INHERITED::restore();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000685 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000686}
687
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000688bool SkDeferredCanvas::isDrawingToLayer() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000689 return this->drawingCanvas()->isDrawingToLayer();
junov@google.com4370aed2012-01-18 16:21:08 +0000690}
691
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000692bool SkDeferredCanvas::translate(SkScalar dx, SkScalar dy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000693 this->drawingCanvas()->translate(dx, dy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000694 bool val = this->INHERITED::translate(dx, dy);
695 this->recordedDrawCommand();
696 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000697}
698
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000699bool SkDeferredCanvas::scale(SkScalar sx, SkScalar sy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000700 this->drawingCanvas()->scale(sx, sy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000701 bool val = this->INHERITED::scale(sx, sy);
702 this->recordedDrawCommand();
703 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000704}
705
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000706bool SkDeferredCanvas::rotate(SkScalar degrees) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000707 this->drawingCanvas()->rotate(degrees);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000708 bool val = this->INHERITED::rotate(degrees);
709 this->recordedDrawCommand();
710 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000711}
712
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000713bool SkDeferredCanvas::skew(SkScalar sx, SkScalar sy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000714 this->drawingCanvas()->skew(sx, sy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000715 bool val = this->INHERITED::skew(sx, sy);
716 this->recordedDrawCommand();
717 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000718}
719
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000720bool SkDeferredCanvas::concat(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000721 this->drawingCanvas()->concat(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000722 bool val = this->INHERITED::concat(matrix);
723 this->recordedDrawCommand();
724 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000725}
726
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000727void SkDeferredCanvas::setMatrix(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000728 this->drawingCanvas()->setMatrix(matrix);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000729 this->INHERITED::setMatrix(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000730 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000731}
732
733bool SkDeferredCanvas::clipRect(const SkRect& rect,
734 SkRegion::Op op,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000735 bool doAntiAlias) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000736 this->drawingCanvas()->clipRect(rect, op, doAntiAlias);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000737 bool val = this->INHERITED::clipRect(rect, op, doAntiAlias);
738 this->recordedDrawCommand();
739 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000740}
741
reed@google.com4ed0fb72012-12-12 20:48:18 +0000742bool SkDeferredCanvas::clipRRect(const SkRRect& rrect,
743 SkRegion::Op op,
744 bool doAntiAlias) {
745 this->drawingCanvas()->clipRRect(rrect, op, doAntiAlias);
746 bool val = this->INHERITED::clipRRect(rrect, op, doAntiAlias);
747 this->recordedDrawCommand();
748 return val;
749}
750
junov@google.com4370aed2012-01-18 16:21:08 +0000751bool SkDeferredCanvas::clipPath(const SkPath& path,
752 SkRegion::Op op,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000753 bool doAntiAlias) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000754 this->drawingCanvas()->clipPath(path, op, doAntiAlias);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000755 bool val = this->INHERITED::clipPath(path, op, doAntiAlias);
756 this->recordedDrawCommand();
757 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000758}
759
760bool SkDeferredCanvas::clipRegion(const SkRegion& deviceRgn,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000761 SkRegion::Op op) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000762 this->drawingCanvas()->clipRegion(deviceRgn, op);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000763 bool val = this->INHERITED::clipRegion(deviceRgn, op);
764 this->recordedDrawCommand();
765 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000766}
767
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000768void SkDeferredCanvas::clear(SkColor color) {
junov@google.com4370aed2012-01-18 16:21:08 +0000769 // purge pending commands
770 if (fDeferredDrawing) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000771 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000772 }
773
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000774 this->drawingCanvas()->clear(color);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000775 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000776}
777
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000778void SkDeferredCanvas::drawPaint(const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000779 if (fDeferredDrawing && this->isFullFrame(NULL, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000780 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000781 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000782 }
junov@chromium.org10f7f972012-07-31 21:01:51 +0000783 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000784 this->drawingCanvas()->drawPaint(paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000785 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000786}
787
788void SkDeferredCanvas::drawPoints(PointMode mode, size_t count,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000789 const SkPoint pts[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000790 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000791 this->drawingCanvas()->drawPoints(mode, count, pts, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000792 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000793}
794
reed@google.com4ed0fb72012-12-12 20:48:18 +0000795void SkDeferredCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
796 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
797 this->drawingCanvas()->drawOval(rect, paint);
798 this->recordedDrawCommand();
799}
800
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000801void SkDeferredCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000802 if (fDeferredDrawing && this->isFullFrame(&rect, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000803 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000804 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000805 }
skia.committer@gmail.com306ab9d2012-12-13 02:01:33 +0000806
junov@chromium.org10f7f972012-07-31 21:01:51 +0000807 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000808 this->drawingCanvas()->drawRect(rect, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000809 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000810}
811
reed@google.com4ed0fb72012-12-12 20:48:18 +0000812void SkDeferredCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
813 if (rrect.isRect()) {
814 this->SkDeferredCanvas::drawRect(rrect.getBounds(), paint);
815 } else if (rrect.isOval()) {
816 this->SkDeferredCanvas::drawOval(rrect.getBounds(), paint);
817 } else {
818 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
819 this->drawingCanvas()->drawRRect(rrect, paint);
820 this->recordedDrawCommand();
821 }
822}
823
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000824void SkDeferredCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000825 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000826 this->drawingCanvas()->drawPath(path, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000827 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000828}
829
830void SkDeferredCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000831 SkScalar top, const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000832 SkRect bitmapRect = SkRect::MakeXYWH(left, top,
833 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000834 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000835 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000836 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000837 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000838 }
839
junov@chromium.org10f7f972012-07-31 21:01:51 +0000840 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000841 this->drawingCanvas()->drawBitmap(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000842 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000843}
844
reed@google.com71121732012-09-18 15:14:33 +0000845void SkDeferredCanvas::drawBitmapRectToRect(const SkBitmap& bitmap,
846 const SkRect* src,
847 const SkRect& dst,
848 const SkPaint* paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000849 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000850 this->isFullFrame(&dst, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000851 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000852 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000853 }
854
junov@chromium.org10f7f972012-07-31 21:01:51 +0000855 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
reed@google.com71121732012-09-18 15:14:33 +0000856 this->drawingCanvas()->drawBitmapRectToRect(bitmap, src, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000857 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000858}
859
860
861void SkDeferredCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
862 const SkMatrix& m,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000863 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000864 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
865 // covers canvas entirely and transformed bitmap covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000866 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000867 this->drawingCanvas()->drawBitmapMatrix(bitmap, m, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000868 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000869}
870
871void SkDeferredCanvas::drawBitmapNine(const SkBitmap& bitmap,
872 const SkIRect& center, const SkRect& dst,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000873 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000874 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
875 // covers canvas entirely and dst covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000876 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000877 this->drawingCanvas()->drawBitmapNine(bitmap, center, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000878 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000879}
880
881void SkDeferredCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000882 const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000883 SkRect bitmapRect = SkRect::MakeXYWH(
884 SkIntToScalar(left),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000885 SkIntToScalar(top),
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000886 SkIntToScalar(bitmap.width()),
887 SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000888 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000889 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000890 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000891 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000892 }
893
junov@chromium.org10f7f972012-07-31 21:01:51 +0000894 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000895 this->drawingCanvas()->drawSprite(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000896 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000897}
898
899void SkDeferredCanvas::drawText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000900 SkScalar x, SkScalar y, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000901 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000902 this->drawingCanvas()->drawText(text, byteLength, x, y, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000903 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000904}
905
906void SkDeferredCanvas::drawPosText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000907 const SkPoint pos[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000908 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000909 this->drawingCanvas()->drawPosText(text, byteLength, pos, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000910 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000911}
912
913void SkDeferredCanvas::drawPosTextH(const void* text, size_t byteLength,
914 const SkScalar xpos[], SkScalar constY,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000915 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000916 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000917 this->drawingCanvas()->drawPosTextH(text, byteLength, xpos, constY, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000918 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000919}
920
921void SkDeferredCanvas::drawTextOnPath(const void* text, size_t byteLength,
922 const SkPath& path,
923 const SkMatrix* matrix,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000924 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000925 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000926 this->drawingCanvas()->drawTextOnPath(text, byteLength, path, matrix, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000927 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000928}
929
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000930void SkDeferredCanvas::drawPicture(SkPicture& picture) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000931 this->drawingCanvas()->drawPicture(picture);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000932 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000933}
934
935void SkDeferredCanvas::drawVertices(VertexMode vmode, int vertexCount,
936 const SkPoint vertices[],
937 const SkPoint texs[],
938 const SkColor colors[], SkXfermode* xmode,
939 const uint16_t indices[], int indexCount,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000940 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000941 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000942 this->drawingCanvas()->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode,
943 indices, indexCount, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000944 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000945}
946
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000947SkBounder* SkDeferredCanvas::setBounder(SkBounder* bounder) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000948 this->drawingCanvas()->setBounder(bounder);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000949 this->INHERITED::setBounder(bounder);
950 this->recordedDrawCommand();
951 return bounder;
junov@google.com4370aed2012-01-18 16:21:08 +0000952}
953
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000954SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000955 this->drawingCanvas()->setDrawFilter(filter);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000956 this->INHERITED::setDrawFilter(filter);
957 this->recordedDrawCommand();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000958 return filter;
junov@google.com4370aed2012-01-18 16:21:08 +0000959}
960
961SkCanvas* SkDeferredCanvas::canvasForDrawIter() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000962 return this->drawingCanvas();
junov@google.com4370aed2012-01-18 16:21:08 +0000963}