blob: 68d06024d11f435656f44a855d53b6a6a7c8a287 [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;
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000166 virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE;
junov@chromium.org88e29142012-08-07 16:48:22 +0000167
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
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000189 virtual void clear(SkColor color) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000190 {SkASSERT(0);}
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000191 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000192 {SkASSERT(0);}
193 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
194 size_t count, const SkPoint[],
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000195 const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000196 {SkASSERT(0);}
197 virtual void drawRect(const SkDraw&, const SkRect& r,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000198 const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000199 {SkASSERT(0);}
200 virtual void drawPath(const SkDraw&, const SkPath& path,
201 const SkPaint& paint,
202 const SkMatrix* prePathMatrix = NULL,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000203 bool pathIsMutable = false) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000204 {SkASSERT(0);}
205 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
206 const SkIRect* srcRectOrNull,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000207 const SkMatrix& matrix, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000208 {SkASSERT(0);}
209 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000210 int x, int y, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000211 {SkASSERT(0);}
212 virtual void drawText(const SkDraw&, const void* text, size_t len,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000213 SkScalar x, SkScalar y, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000214 {SkASSERT(0);}
215 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
216 const SkScalar pos[], SkScalar constY,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000217 int scalarsPerPos, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000218 {SkASSERT(0);}
219 virtual void drawTextOnPath(const SkDraw&, const void* text,
220 size_t len, const SkPath& path,
221 const SkMatrix* matrix,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000222 const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000223 {SkASSERT(0);}
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000224#ifdef SK_BUILD_FOR_ANDROID
junov@chromium.org88e29142012-08-07 16:48:22 +0000225 virtual void drawPosTextOnPath(const SkDraw& draw, const void* text,
226 size_t len, const SkPoint pos[],
227 const SkPaint& paint,
228 const SkPath& path,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000229 const SkMatrix* matrix) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000230 {SkASSERT(0);}
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000231#endif
junov@chromium.org88e29142012-08-07 16:48:22 +0000232 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
233 int vertexCount, const SkPoint verts[],
234 const SkPoint texs[], const SkColor colors[],
235 SkXfermode* xmode, const uint16_t indices[],
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000236 int indexCount, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000237 {SkASSERT(0);}
238 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000239 const SkPaint&) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000240 {SkASSERT(0);}
241private:
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000242 virtual void flush() SK_OVERRIDE;
junov@chromium.org88e29142012-08-07 16:48:22 +0000243
junov@chromium.org88e29142012-08-07 16:48:22 +0000244 void beginRecording();
junov@chromium.org67d74222013-04-12 13:33:01 +0000245 void init();
junov@chromium.org88e29142012-08-07 16:48:22 +0000246
247 DeferredPipeController fPipeController;
248 SkGPipeWriter fPipeWriter;
junov@chromium.org88e29142012-08-07 16:48:22 +0000249 SkCanvas* fImmediateCanvas;
250 SkCanvas* fRecordingCanvas;
junov@chromium.org67d74222013-04-12 13:33:01 +0000251 SkSurface* fSurface;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000252 SkDeferredCanvas::NotificationClient* fNotificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000253 bool fFreshFrame;
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000254 bool fCanDiscardCanvasContents;
junov@chromium.org88e29142012-08-07 16:48:22 +0000255 size_t fMaxRecordingStorageBytes;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000256 size_t fPreviousStorageAllocated;
sugoi@google.com7775fd52012-11-21 15:47:04 +0000257 size_t fBitmapSizeThreshold;
junov@chromium.org88e29142012-08-07 16:48:22 +0000258};
259
junov@chromium.org67d74222013-04-12 13:33:01 +0000260DeferredDevice::DeferredDevice(SkDevice* immediateDevice)
261 : SkDevice(SkBitmap::kNo_Config,
262 immediateDevice->width(), immediateDevice->height(),
263 immediateDevice->isOpaque(),
264 immediateDevice->getDeviceProperties()) {
265 fSurface = NULL;
junov@chromium.org9becf002013-04-15 18:15:23 +0000266 fImmediateCanvas = SkNEW_ARGS(SkCanvas, (immediateDevice));
junov@chromium.org7070f762013-05-24 17:13:00 +0000267 fPipeController.setPlaybackCanvas(fImmediateCanvas);
junov@chromium.org67d74222013-04-12 13:33:01 +0000268 this->init();
269}
270
271DeferredDevice::DeferredDevice(SkSurface* surface)
272 : SkDevice(SkBitmap::kNo_Config,
273 surface->getCanvas()->getDevice()->width(),
274 surface->getCanvas()->getDevice()->height(),
275 surface->getCanvas()->getDevice()->isOpaque(),
276 surface->getCanvas()->getDevice()->getDeviceProperties()) {
277 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
278 fNotificationClient = NULL;
junov@chromium.org7070f762013-05-24 17:13:00 +0000279 fImmediateCanvas = NULL;
280 fSurface = NULL;
281 this->setSurface(surface);
junov@chromium.org67d74222013-04-12 13:33:01 +0000282 this->init();
283}
284
junov@chromium.org7070f762013-05-24 17:13:00 +0000285void DeferredDevice::setSurface(SkSurface* surface) {
286 SkRefCnt_SafeAssign(fImmediateCanvas, surface->getCanvas());
287 SkRefCnt_SafeAssign(fSurface, surface);
288 fPipeController.setPlaybackCanvas(fImmediateCanvas);
289}
290
junov@chromium.org67d74222013-04-12 13:33:01 +0000291void DeferredDevice::init() {
292 fRecordingCanvas = NULL;
293 fFreshFrame = true;
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000294 fCanDiscardCanvasContents = false;
junov@chromium.org67d74222013-04-12 13:33:01 +0000295 fPreviousStorageAllocated = 0;
296 fBitmapSizeThreshold = kDeferredCanvasBitmapSizeThreshold;
297 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
298 fNotificationClient = NULL;
junov@chromium.org88e29142012-08-07 16:48:22 +0000299 this->beginRecording();
300}
301
302DeferredDevice::~DeferredDevice() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000303 this->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000304 SkSafeUnref(fImmediateCanvas);
junov@chromium.org67d74222013-04-12 13:33:01 +0000305 SkSafeUnref(fSurface);
junov@chromium.org88e29142012-08-07 16:48:22 +0000306}
307
308void DeferredDevice::setMaxRecordingStorage(size_t maxStorage) {
309 fMaxRecordingStorageBytes = maxStorage;
310 this->recordingCanvas(); // Accessing the recording canvas applies the new limit.
311}
312
junov@chromium.org88e29142012-08-07 16:48:22 +0000313void DeferredDevice::beginRecording() {
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000314 SkASSERT(NULL == fRecordingCanvas);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000315 fRecordingCanvas = fPipeWriter.startRecording(&fPipeController, 0,
junov@chromium.org9becf002013-04-15 18:15:23 +0000316 immediateDevice()->width(), immediateDevice()->height());
junov@chromium.org88e29142012-08-07 16:48:22 +0000317}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000318
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000319void DeferredDevice::setNotificationClient(
320 SkDeferredCanvas::NotificationClient* notificationClient) {
junov@chromium.org52805482012-08-20 14:25:04 +0000321 fNotificationClient = notificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000322}
323
junov@chromium.org0a67f962012-09-19 22:48:34 +0000324void DeferredDevice::skipPendingCommands() {
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000325 if (!fRecordingCanvas->isDrawingToLayer()) {
326 fCanDiscardCanvasContents = true;
327 if (fPipeController.hasPendingCommands()) {
328 fFreshFrame = true;
329 flushPendingCommands(kSilent_PlaybackMode);
330 if (fNotificationClient) {
331 fNotificationClient->skippedPendingDrawCommands();
332 }
junov@google.com52a00ca2012-10-01 15:27:14 +0000333 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000334 }
335}
336
337bool DeferredDevice::isFreshFrame() {
338 bool ret = fFreshFrame;
339 fFreshFrame = false;
340 return ret;
341}
342
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000343bool DeferredDevice::hasPendingCommands() {
344 return fPipeController.hasPendingCommands();
345}
346
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000347void DeferredDevice::flushPendingCommands(PlaybackMode playbackMode) {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000348 if (!fPipeController.hasPendingCommands()) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000349 return;
350 }
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000351 if (playbackMode == kNormal_PlaybackMode) {
352 if (NULL != fNotificationClient) {
353 fNotificationClient->prepareForDraw();
354 }
355 if (fCanDiscardCanvasContents) {
356 if (NULL != fSurface) {
357 // Pre-empt notifyContentChanged(false) calls that will happen
358 // during flush
359 fSurface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
360 }
361 fCanDiscardCanvasContents = false;
362 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000363 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000364 fPipeWriter.flushRecording(true);
junov@chromium.orgd4501a02012-10-30 19:05:17 +0000365 fPipeController.playback(kSilent_PlaybackMode == playbackMode);
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000366 if (playbackMode == kNormal_PlaybackMode && fNotificationClient) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000367 fNotificationClient->flushedDrawCommands();
368 }
369 fPreviousStorageAllocated = storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000370}
371
372void DeferredDevice::flush() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000373 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000374 fImmediateCanvas->flush();
375}
376
377size_t DeferredDevice::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000378 size_t val = fPipeWriter.freeMemoryIfPossible(bytesToFree);
379 fPreviousStorageAllocated = storageAllocatedForRecording();
380 return val;
junov@chromium.org88e29142012-08-07 16:48:22 +0000381}
382
sugoi@google.com7775fd52012-11-21 15:47:04 +0000383size_t DeferredDevice::getBitmapSizeThreshold() const {
384 return fBitmapSizeThreshold;
385}
386
387void DeferredDevice::setBitmapSizeThreshold(size_t sizeThreshold) {
388 fBitmapSizeThreshold = sizeThreshold;
389}
390
junov@chromium.org88e29142012-08-07 16:48:22 +0000391size_t DeferredDevice::storageAllocatedForRecording() const {
392 return (fPipeController.storageAllocatedForRecording()
393 + fPipeWriter.storageAllocatedForRecording());
394}
395
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000396void DeferredDevice::recordedDrawCommand() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000397 size_t storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000398
junov@chromium.org88e29142012-08-07 16:48:22 +0000399 if (storageAllocated > fMaxRecordingStorageBytes) {
400 // First, attempt to reduce cache without flushing
401 size_t tryFree = storageAllocated - fMaxRecordingStorageBytes;
402 if (this->freeMemoryIfPossible(tryFree) < tryFree) {
403 // Flush is necessary to free more space.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000404 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000405 // Free as much as possible to avoid oscillating around fMaxRecordingStorageBytes
406 // which could cause a high flushing frequency.
bsalomon@google.com100abf42012-09-05 17:40:04 +0000407 this->freeMemoryIfPossible(~0U);
junov@chromium.org88e29142012-08-07 16:48:22 +0000408 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000409 storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000410 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000411
rmistry@google.comd6176b02012-08-23 18:14:13 +0000412 if (fNotificationClient &&
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000413 storageAllocated != fPreviousStorageAllocated) {
414 fPreviousStorageAllocated = storageAllocated;
415 fNotificationClient->storageAllocatedForRecordingChanged(storageAllocated);
416 }
417}
418
419SkCanvas* DeferredDevice::recordingCanvas() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000420 return fRecordingCanvas;
421}
422
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000423SkImage* DeferredDevice::newImageSnapshot() {
junov@chromium.org67d74222013-04-12 13:33:01 +0000424 this->flush();
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000425 return fSurface ? fSurface->newImageSnapshot() : NULL;
junov@chromium.org67d74222013-04-12 13:33:01 +0000426}
427
rmistry@google.comd6176b02012-08-23 18:14:13 +0000428uint32_t DeferredDevice::getDeviceCapabilities() {
junov@chromium.org9becf002013-04-15 18:15:23 +0000429 return immediateDevice()->getDeviceCapabilities();
junov@chromium.org88e29142012-08-07 16:48:22 +0000430}
431
rmistry@google.comd6176b02012-08-23 18:14:13 +0000432int DeferredDevice::width() const {
junov@chromium.org9becf002013-04-15 18:15:23 +0000433 return immediateDevice()->width();
junov@chromium.org88e29142012-08-07 16:48:22 +0000434}
435
436int DeferredDevice::height() const {
junov@chromium.org9becf002013-04-15 18:15:23 +0000437 return immediateDevice()->height();
junov@chromium.org88e29142012-08-07 16:48:22 +0000438}
439
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000440GrRenderTarget* DeferredDevice::accessRenderTarget() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000441 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000442 return immediateDevice()->accessRenderTarget();
junov@chromium.org88e29142012-08-07 16:48:22 +0000443}
444
445void DeferredDevice::writePixels(const SkBitmap& bitmap,
446 int x, int y, SkCanvas::Config8888 config8888) {
447
448 if (x <= 0 && y <= 0 && (x + bitmap.width()) >= width() &&
449 (y + bitmap.height()) >= height()) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000450 this->skipPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000451 }
452
453 if (SkBitmap::kARGB_8888_Config == bitmap.config() &&
454 SkCanvas::kNative_Premul_Config8888 != config8888 &&
455 kPMColorAlias != config8888) {
456 //Special case config: no deferral
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000457 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000458 immediateDevice()->writePixels(bitmap, x, y, config8888);
junov@chromium.org88e29142012-08-07 16:48:22 +0000459 return;
460 }
461
462 SkPaint paint;
463 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000464 if (shouldDrawImmediately(&bitmap, NULL, getBitmapSizeThreshold())) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000465 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000466 fImmediateCanvas->drawSprite(bitmap, x, y, &paint);
467 } else {
468 this->recordingCanvas()->drawSprite(bitmap, x, y, &paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000469 this->recordedDrawCommand();
470
junov@chromium.org88e29142012-08-07 16:48:22 +0000471 }
472}
473
474const SkBitmap& DeferredDevice::onAccessBitmap(SkBitmap*) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000475 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000476 return immediateDevice()->accessBitmap(false);
junov@chromium.org88e29142012-08-07 16:48:22 +0000477}
478
479SkDevice* DeferredDevice::onCreateCompatibleDevice(
480 SkBitmap::Config config, int width, int height, bool isOpaque,
481 Usage usage) {
482
483 // Save layer usage not supported, and not required by SkDeferredCanvas.
484 SkASSERT(usage != kSaveLayer_Usage);
485 // Create a compatible non-deferred device.
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000486 // We do not create a deferred device because we know the new device
487 // will not be used with a deferred canvas (there is no API for that).
488 // And connecting a DeferredDevice to non-deferred canvas can result
489 // in unpredictable behavior.
490 return immediateDevice()->createCompatibleDevice(config, width, height, isOpaque);
junov@chromium.org88e29142012-08-07 16:48:22 +0000491}
492
493bool DeferredDevice::onReadPixels(
494 const SkBitmap& bitmap, int x, int y, SkCanvas::Config8888 config8888) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000495 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000496 return fImmediateCanvas->readPixels(const_cast<SkBitmap*>(&bitmap),
497 x, y, config8888);
498}
499
sugoi@google.com7775fd52012-11-21 15:47:04 +0000500class AutoImmediateDrawIfNeeded {
501public:
502 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkBitmap* bitmap,
503 const SkPaint* paint) {
504 this->init(canvas, bitmap, paint);
505 }
506
507 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkPaint* paint) {
508 this->init(canvas, NULL, paint);
509 }
510
511 ~AutoImmediateDrawIfNeeded() {
512 if (fCanvas) {
513 fCanvas->setDeferredDrawing(true);
514 }
515 }
516private:
517 void init(SkDeferredCanvas& canvas, const SkBitmap* bitmap, const SkPaint* paint)
518 {
519 DeferredDevice* device = static_cast<DeferredDevice*>(canvas.getDevice());
520 if (canvas.isDeferredDrawing() && (NULL != device) &&
521 shouldDrawImmediately(bitmap, paint, device->getBitmapSizeThreshold())) {
522 canvas.setDeferredDrawing(false);
523 fCanvas = &canvas;
524 } else {
525 fCanvas = NULL;
526 }
527 }
528
529 SkDeferredCanvas* fCanvas;
530};
junov@chromium.org88e29142012-08-07 16:48:22 +0000531
junov@chromium.org66070a52013-05-28 17:39:08 +0000532#if !SK_DEFERRED_CANVAS_USES_FACTORIES
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000533SkDeferredCanvas::SkDeferredCanvas() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000534 this->init();
junov@google.com4370aed2012-01-18 16:21:08 +0000535}
536
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000537SkDeferredCanvas::SkDeferredCanvas(SkDevice* device) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000538 this->init();
539 this->setDevice(device);
junov@google.com4370aed2012-01-18 16:21:08 +0000540}
541
junov@chromium.org67d74222013-04-12 13:33:01 +0000542SkDeferredCanvas::SkDeferredCanvas(SkSurface* surface) {
543 this->init();
544 this->INHERITED::setDevice(SkNEW_ARGS(DeferredDevice, (surface)))->unref();
545}
junov@chromium.org66070a52013-05-28 17:39:08 +0000546#endif
547
548SkDeferredCanvas* SkDeferredCanvas::Create(SkSurface* surface) {
549 SkAutoTUnref<DeferredDevice> deferredDevice(SkNEW_ARGS(DeferredDevice, (surface)));
550 return SkNEW_ARGS(SkDeferredCanvas, (deferredDevice));
551}
552
553#ifdef SK_DEVELOPER
554SkDeferredCanvas* SkDeferredCanvas::Create(SkDevice* device) {
555 SkAutoTUnref<DeferredDevice> deferredDevice(SkNEW_ARGS(DeferredDevice, (device)));
556 return SkNEW_ARGS(SkDeferredCanvas, (deferredDevice));
557}
558#endif
559
560SkDeferredCanvas::SkDeferredCanvas(DeferredDevice* device) : SkCanvas (device) {
561 this->init();
562}
junov@chromium.org67d74222013-04-12 13:33:01 +0000563
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000564void SkDeferredCanvas::init() {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000565 fDeferredDrawing = true; // On by default
junov@google.com4370aed2012-01-18 16:21:08 +0000566}
567
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000568void SkDeferredCanvas::setMaxRecordingStorage(size_t maxStorage) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000569 this->validate();
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000570 this->getDeferredDevice()->setMaxRecordingStorage(maxStorage);
571}
572
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000573size_t SkDeferredCanvas::storageAllocatedForRecording() const {
574 return this->getDeferredDevice()->storageAllocatedForRecording();
575}
576
577size_t SkDeferredCanvas::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000578 return this->getDeferredDevice()->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000579}
580
sugoi@google.com7775fd52012-11-21 15:47:04 +0000581void SkDeferredCanvas::setBitmapSizeThreshold(size_t sizeThreshold) {
582 DeferredDevice* deferredDevice = this->getDeferredDevice();
583 SkASSERT(deferredDevice);
584 deferredDevice->setBitmapSizeThreshold(sizeThreshold);
585}
586
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000587void SkDeferredCanvas::recordedDrawCommand() {
588 if (fDeferredDrawing) {
589 this->getDeferredDevice()->recordedDrawCommand();
590 }
591}
592
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000593void SkDeferredCanvas::validate() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000594 SkASSERT(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000595}
596
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000597SkCanvas* SkDeferredCanvas::drawingCanvas() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000598 this->validate();
599 return fDeferredDrawing ? this->getDeferredDevice()->recordingCanvas() :
600 this->getDeferredDevice()->immediateCanvas();
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000601}
602
junov@chromium.org88e29142012-08-07 16:48:22 +0000603SkCanvas* SkDeferredCanvas::immediateCanvas() const {
604 this->validate();
605 return this->getDeferredDevice()->immediateCanvas();
606}
607
608DeferredDevice* SkDeferredCanvas::getDeferredDevice() const {
609 return static_cast<DeferredDevice*>(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000610}
611
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000612void SkDeferredCanvas::setDeferredDrawing(bool val) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000613 this->validate(); // Must set device before calling this method
junov@google.com4370aed2012-01-18 16:21:08 +0000614 if (val != fDeferredDrawing) {
615 if (fDeferredDrawing) {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000616 // Going live.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000617 this->getDeferredDevice()->flushPendingCommands(kNormal_PlaybackMode);
junov@google.com4370aed2012-01-18 16:21:08 +0000618 }
619 fDeferredDrawing = val;
620 }
621}
622
junov@chromium.org88e29142012-08-07 16:48:22 +0000623bool SkDeferredCanvas::isDeferredDrawing() const {
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +0000624 return fDeferredDrawing;
625}
626
junov@chromium.org88e29142012-08-07 16:48:22 +0000627bool SkDeferredCanvas::isFreshFrame() const {
628 return this->getDeferredDevice()->isFreshFrame();
629}
630
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000631bool SkDeferredCanvas::hasPendingCommands() const {
632 return this->getDeferredDevice()->hasPendingCommands();
633}
634
junov@chromium.orgfb103892012-09-20 19:35:43 +0000635void SkDeferredCanvas::silentFlush() {
636 if (fDeferredDrawing) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000637 this->getDeferredDevice()->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000638 }
639}
640
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000641SkDeferredCanvas::~SkDeferredCanvas() {
junov@google.com4370aed2012-01-18 16:21:08 +0000642}
643
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000644SkDevice* SkDeferredCanvas::setDevice(SkDevice* device) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000645#if SK_DEFERRED_CANVAS_USES_FACTORIES
646 SkASSERT(0); // setDevice is deprecated
647#else
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000648 this->INHERITED::setDevice(SkNEW_ARGS(DeferredDevice, (device)))->unref();
junov@chromium.org66070a52013-05-28 17:39:08 +0000649#endif
junov@google.com4370aed2012-01-18 16:21:08 +0000650 return device;
651}
652
junov@chromium.org7070f762013-05-24 17:13:00 +0000653SkSurface* SkDeferredCanvas::setSurface(SkSurface* surface) {
654 DeferredDevice* deferredDevice = this->getDeferredDevice();
junov@chromium.org66070a52013-05-28 17:39:08 +0000655 SkASSERT(NULL != deferredDevice);
656 // By swapping the surface into the existing device, we preserve
657 // all pending commands, which can help to seamlessly recover from
658 // a lost accelerated graphics context.
659 deferredDevice->setSurface(surface);
junov@chromium.org7070f762013-05-24 17:13:00 +0000660 return surface;
661}
662
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000663SkDeferredCanvas::NotificationClient* SkDeferredCanvas::setNotificationClient(
664 NotificationClient* notificationClient) {
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000665
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000666 DeferredDevice* deferredDevice = this->getDeferredDevice();
junov@google.com4370aed2012-01-18 16:21:08 +0000667 SkASSERT(deferredDevice);
668 if (deferredDevice) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000669 deferredDevice->setNotificationClient(notificationClient);
junov@google.com4370aed2012-01-18 16:21:08 +0000670 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000671 return notificationClient;
junov@google.com4370aed2012-01-18 16:21:08 +0000672}
673
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000674SkImage* SkDeferredCanvas::newImageSnapshot() {
junov@chromium.org67d74222013-04-12 13:33:01 +0000675 DeferredDevice* deferredDevice = this->getDeferredDevice();
676 SkASSERT(deferredDevice);
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000677 return deferredDevice ? deferredDevice->newImageSnapshot() : NULL;
junov@chromium.org67d74222013-04-12 13:33:01 +0000678}
679
junov@google.com4370aed2012-01-18 16:21:08 +0000680bool SkDeferredCanvas::isFullFrame(const SkRect* rect,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000681 const SkPaint* paint) const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000682 SkCanvas* canvas = this->drawingCanvas();
683 SkISize canvasSize = this->getDeviceSize();
junov@google.com4370aed2012-01-18 16:21:08 +0000684 if (rect) {
685 if (!canvas->getTotalMatrix().rectStaysRect()) {
686 return false; // conservative
687 }
688
689 SkRect transformedRect;
690 canvas->getTotalMatrix().mapRect(&transformedRect, *rect);
691
692 if (paint) {
693 SkPaint::Style paintStyle = paint->getStyle();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000694 if (!(paintStyle == SkPaint::kFill_Style ||
junov@google.com4370aed2012-01-18 16:21:08 +0000695 paintStyle == SkPaint::kStrokeAndFill_Style)) {
696 return false;
697 }
698 if (paint->getMaskFilter() || paint->getLooper()
699 || paint->getPathEffect() || paint->getImageFilter()) {
700 return false; // conservative
701 }
702 }
703
704 // The following test holds with AA enabled, and is conservative
705 // by a 0.5 pixel margin with AA disabled
rmistry@google.comd6176b02012-08-23 18:14:13 +0000706 if (transformedRect.fLeft > SkIntToScalar(0) ||
707 transformedRect.fTop > SkIntToScalar(0) ||
junov@chromium.orgb1e218e2012-02-13 22:27:58 +0000708 transformedRect.fRight < SkIntToScalar(canvasSize.fWidth) ||
709 transformedRect.fBottom < SkIntToScalar(canvasSize.fHeight)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000710 return false;
711 }
712 }
713
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000714 return this->getClipStack()->quickContains(SkRect::MakeXYWH(0, 0,
715 SkIntToScalar(canvasSize.fWidth), SkIntToScalar(canvasSize.fHeight)));
junov@google.com4370aed2012-01-18 16:21:08 +0000716}
717
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000718int SkDeferredCanvas::save(SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000719 this->drawingCanvas()->save(flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000720 int val = this->INHERITED::save(flags);
721 this->recordedDrawCommand();
722
723 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000724}
725
726int SkDeferredCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000727 SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000728 this->drawingCanvas()->saveLayer(bounds, paint, flags);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000729 int count = this->INHERITED::save(flags);
730 this->clipRectBounds(bounds, flags, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000731 this->recordedDrawCommand();
732
junov@chromium.orga907ac32012-02-24 21:54:07 +0000733 return count;
junov@google.com4370aed2012-01-18 16:21:08 +0000734}
735
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000736void SkDeferredCanvas::restore() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000737 this->drawingCanvas()->restore();
junov@chromium.orga907ac32012-02-24 21:54:07 +0000738 this->INHERITED::restore();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000739 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000740}
741
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000742bool SkDeferredCanvas::isDrawingToLayer() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000743 return this->drawingCanvas()->isDrawingToLayer();
junov@google.com4370aed2012-01-18 16:21:08 +0000744}
745
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000746bool SkDeferredCanvas::translate(SkScalar dx, SkScalar dy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000747 this->drawingCanvas()->translate(dx, dy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000748 bool val = this->INHERITED::translate(dx, dy);
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::scale(SkScalar sx, SkScalar sy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000754 this->drawingCanvas()->scale(sx, sy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000755 bool val = this->INHERITED::scale(sx, sy);
756 this->recordedDrawCommand();
757 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000758}
759
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000760bool SkDeferredCanvas::rotate(SkScalar degrees) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000761 this->drawingCanvas()->rotate(degrees);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000762 bool val = this->INHERITED::rotate(degrees);
763 this->recordedDrawCommand();
764 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000765}
766
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000767bool SkDeferredCanvas::skew(SkScalar sx, SkScalar sy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000768 this->drawingCanvas()->skew(sx, sy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000769 bool val = this->INHERITED::skew(sx, sy);
770 this->recordedDrawCommand();
771 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000772}
773
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000774bool SkDeferredCanvas::concat(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000775 this->drawingCanvas()->concat(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000776 bool val = this->INHERITED::concat(matrix);
777 this->recordedDrawCommand();
778 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000779}
780
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000781void SkDeferredCanvas::setMatrix(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000782 this->drawingCanvas()->setMatrix(matrix);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000783 this->INHERITED::setMatrix(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000784 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000785}
786
787bool SkDeferredCanvas::clipRect(const SkRect& rect,
788 SkRegion::Op op,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000789 bool doAntiAlias) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000790 this->drawingCanvas()->clipRect(rect, op, doAntiAlias);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000791 bool val = this->INHERITED::clipRect(rect, op, doAntiAlias);
792 this->recordedDrawCommand();
793 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000794}
795
reed@google.com4ed0fb72012-12-12 20:48:18 +0000796bool SkDeferredCanvas::clipRRect(const SkRRect& rrect,
797 SkRegion::Op op,
798 bool doAntiAlias) {
799 this->drawingCanvas()->clipRRect(rrect, op, doAntiAlias);
800 bool val = this->INHERITED::clipRRect(rrect, op, doAntiAlias);
801 this->recordedDrawCommand();
802 return val;
803}
804
junov@google.com4370aed2012-01-18 16:21:08 +0000805bool SkDeferredCanvas::clipPath(const SkPath& path,
806 SkRegion::Op op,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000807 bool doAntiAlias) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000808 this->drawingCanvas()->clipPath(path, op, doAntiAlias);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000809 bool val = this->INHERITED::clipPath(path, op, doAntiAlias);
810 this->recordedDrawCommand();
811 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000812}
813
814bool SkDeferredCanvas::clipRegion(const SkRegion& deviceRgn,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000815 SkRegion::Op op) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000816 this->drawingCanvas()->clipRegion(deviceRgn, op);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000817 bool val = this->INHERITED::clipRegion(deviceRgn, op);
818 this->recordedDrawCommand();
819 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000820}
821
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000822void SkDeferredCanvas::clear(SkColor color) {
junov@google.com4370aed2012-01-18 16:21:08 +0000823 // purge pending commands
824 if (fDeferredDrawing) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000825 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000826 }
827
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000828 this->drawingCanvas()->clear(color);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000829 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000830}
831
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000832void SkDeferredCanvas::drawPaint(const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000833 if (fDeferredDrawing && this->isFullFrame(NULL, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000834 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000835 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000836 }
junov@chromium.org10f7f972012-07-31 21:01:51 +0000837 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000838 this->drawingCanvas()->drawPaint(paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000839 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000840}
841
842void SkDeferredCanvas::drawPoints(PointMode mode, size_t count,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000843 const SkPoint pts[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000844 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000845 this->drawingCanvas()->drawPoints(mode, count, pts, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000846 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000847}
848
reed@google.com4ed0fb72012-12-12 20:48:18 +0000849void SkDeferredCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
850 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
851 this->drawingCanvas()->drawOval(rect, paint);
852 this->recordedDrawCommand();
853}
854
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000855void SkDeferredCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000856 if (fDeferredDrawing && this->isFullFrame(&rect, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000857 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000858 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000859 }
skia.committer@gmail.com306ab9d2012-12-13 02:01:33 +0000860
junov@chromium.org10f7f972012-07-31 21:01:51 +0000861 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000862 this->drawingCanvas()->drawRect(rect, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000863 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000864}
865
reed@google.com4ed0fb72012-12-12 20:48:18 +0000866void SkDeferredCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
867 if (rrect.isRect()) {
868 this->SkDeferredCanvas::drawRect(rrect.getBounds(), paint);
869 } else if (rrect.isOval()) {
870 this->SkDeferredCanvas::drawOval(rrect.getBounds(), paint);
871 } else {
872 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
873 this->drawingCanvas()->drawRRect(rrect, paint);
874 this->recordedDrawCommand();
875 }
876}
877
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000878void SkDeferredCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000879 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000880 this->drawingCanvas()->drawPath(path, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000881 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000882}
883
884void SkDeferredCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000885 SkScalar top, const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000886 SkRect bitmapRect = SkRect::MakeXYWH(left, top,
887 SkIntToScalar(bitmap.width()), 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()->drawBitmap(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
reed@google.com71121732012-09-18 15:14:33 +0000899void SkDeferredCanvas::drawBitmapRectToRect(const SkBitmap& bitmap,
900 const SkRect* src,
901 const SkRect& dst,
902 const SkPaint* paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000903 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000904 this->isFullFrame(&dst, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000905 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000906 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000907 }
908
junov@chromium.org10f7f972012-07-31 21:01:51 +0000909 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
reed@google.com71121732012-09-18 15:14:33 +0000910 this->drawingCanvas()->drawBitmapRectToRect(bitmap, src, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000911 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000912}
913
914
915void SkDeferredCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
916 const SkMatrix& m,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000917 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000918 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
919 // covers canvas entirely and transformed bitmap covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000920 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000921 this->drawingCanvas()->drawBitmapMatrix(bitmap, m, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000922 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000923}
924
925void SkDeferredCanvas::drawBitmapNine(const SkBitmap& bitmap,
926 const SkIRect& center, const SkRect& dst,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000927 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000928 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
929 // covers canvas entirely and dst covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000930 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000931 this->drawingCanvas()->drawBitmapNine(bitmap, center, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000932 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000933}
934
935void SkDeferredCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000936 const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000937 SkRect bitmapRect = SkRect::MakeXYWH(
938 SkIntToScalar(left),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000939 SkIntToScalar(top),
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000940 SkIntToScalar(bitmap.width()),
941 SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000942 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000943 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000944 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000945 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000946 }
947
junov@chromium.org10f7f972012-07-31 21:01:51 +0000948 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000949 this->drawingCanvas()->drawSprite(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000950 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000951}
952
953void SkDeferredCanvas::drawText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000954 SkScalar x, SkScalar y, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000955 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000956 this->drawingCanvas()->drawText(text, byteLength, x, y, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000957 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000958}
959
960void SkDeferredCanvas::drawPosText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000961 const SkPoint pos[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000962 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000963 this->drawingCanvas()->drawPosText(text, byteLength, pos, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000964 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000965}
966
967void SkDeferredCanvas::drawPosTextH(const void* text, size_t byteLength,
968 const SkScalar xpos[], SkScalar constY,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000969 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000970 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000971 this->drawingCanvas()->drawPosTextH(text, byteLength, xpos, constY, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000972 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000973}
974
975void SkDeferredCanvas::drawTextOnPath(const void* text, size_t byteLength,
976 const SkPath& path,
977 const SkMatrix* matrix,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000978 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000979 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000980 this->drawingCanvas()->drawTextOnPath(text, byteLength, path, matrix, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000981 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000982}
983
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000984void SkDeferredCanvas::drawPicture(SkPicture& picture) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000985 this->drawingCanvas()->drawPicture(picture);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000986 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000987}
988
989void SkDeferredCanvas::drawVertices(VertexMode vmode, int vertexCount,
990 const SkPoint vertices[],
991 const SkPoint texs[],
992 const SkColor colors[], SkXfermode* xmode,
993 const uint16_t indices[], int indexCount,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000994 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000995 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000996 this->drawingCanvas()->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode,
997 indices, indexCount, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000998 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000999}
1000
junov@chromium.orgc16ca922012-02-24 22:06:27 +00001001SkBounder* SkDeferredCanvas::setBounder(SkBounder* bounder) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +00001002 this->drawingCanvas()->setBounder(bounder);
junov@chromium.org9ed02b92012-08-14 13:36:26 +00001003 this->INHERITED::setBounder(bounder);
1004 this->recordedDrawCommand();
1005 return bounder;
junov@google.com4370aed2012-01-18 16:21:08 +00001006}
1007
junov@chromium.orgc16ca922012-02-24 22:06:27 +00001008SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) {
rmistry@google.comd6176b02012-08-23 18:14:13 +00001009 this->drawingCanvas()->setDrawFilter(filter);
junov@chromium.org9ed02b92012-08-14 13:36:26 +00001010 this->INHERITED::setDrawFilter(filter);
1011 this->recordedDrawCommand();
rmistry@google.comd6176b02012-08-23 18:14:13 +00001012 return filter;
junov@google.com4370aed2012-01-18 16:21:08 +00001013}
1014
1015SkCanvas* SkDeferredCanvas::canvasForDrawIter() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +00001016 return this->drawingCanvas();
junov@google.com4370aed2012-01-18 16:21:08 +00001017}