blob: e3b761f0e0ec58985ce173621b4b3b87f3cc2f53 [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//-----------------------------------------------------------------------------
robertphillips@google.com9b051a32013-08-20 20:06:40 +0000140class DeferredDevice : public SkDevice {
junov@chromium.org88e29142012-08-07 16:48:22 +0000141public:
robertphillips@google.com9b051a32013-08-20 20:06:40 +0000142 explicit DeferredDevice(SkDevice* immediateDevice);
junov@chromium.org67d74222013-04-12 13:33:01 +0000143 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;}
robertphillips@google.com9b051a32013-08-20 20:06:40 +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
robertphillips@google.com9b051a32013-08-20 20:06:40 +0000168 virtual SkDevice* onCreateCompatibleDevice(SkBitmap::Config config,
169 int width, int height,
170 bool isOpaque,
171 Usage usage) SK_OVERRIDE;
junov@chromium.org88e29142012-08-07 16:48:22 +0000172
173 virtual void writePixels(const SkBitmap& bitmap, int x, int y,
174 SkCanvas::Config8888 config8888) SK_OVERRIDE;
175
176protected:
robertphillips@google.com9b051a32013-08-20 20:06:40 +0000177 virtual const SkBitmap& onAccessBitmap(SkBitmap*) SK_OVERRIDE;
junov@chromium.org88e29142012-08-07 16:48:22 +0000178 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
robertphillips@google.com9b051a32013-08-20 20:06:40 +0000183 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,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000206 const SkMatrix& matrix, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000207 {SkASSERT(0);}
208 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000209 int x, int y, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000210 {SkASSERT(0);}
211 virtual void drawText(const SkDraw&, const void* text, size_t len,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000212 SkScalar x, SkScalar y, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000213 {SkASSERT(0);}
214 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
215 const SkScalar pos[], SkScalar constY,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000216 int scalarsPerPos, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000217 {SkASSERT(0);}
218 virtual void drawTextOnPath(const SkDraw&, const void* text,
219 size_t len, const SkPath& path,
220 const SkMatrix* matrix,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000221 const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000222 {SkASSERT(0);}
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000223#ifdef SK_BUILD_FOR_ANDROID
junov@chromium.org88e29142012-08-07 16:48:22 +0000224 virtual void drawPosTextOnPath(const SkDraw& draw, const void* text,
225 size_t len, const SkPoint pos[],
226 const SkPaint& paint,
227 const SkPath& path,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000228 const SkMatrix* matrix) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000229 {SkASSERT(0);}
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000230#endif
junov@chromium.org88e29142012-08-07 16:48:22 +0000231 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
232 int vertexCount, const SkPoint verts[],
233 const SkPoint texs[], const SkColor colors[],
234 SkXfermode* xmode, const uint16_t indices[],
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000235 int indexCount, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000236 {SkASSERT(0);}
robertphillips@google.com9b051a32013-08-20 20:06:40 +0000237 virtual void drawDevice(const SkDraw&, SkDevice*, int x, int y,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000238 const SkPaint&) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000239 {SkASSERT(0);}
240private:
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000241 virtual void flush() SK_OVERRIDE;
junov@chromium.org88e29142012-08-07 16:48:22 +0000242
junov@chromium.org88e29142012-08-07 16:48:22 +0000243 void beginRecording();
junov@chromium.org67d74222013-04-12 13:33:01 +0000244 void init();
junov@chromium.org44324fa2013-08-02 15:36:02 +0000245 void aboutToDraw();
246 void prepareForImmediatePixelWrite();
junov@chromium.org88e29142012-08-07 16:48:22 +0000247
248 DeferredPipeController fPipeController;
249 SkGPipeWriter fPipeWriter;
junov@chromium.org88e29142012-08-07 16:48:22 +0000250 SkCanvas* fImmediateCanvas;
251 SkCanvas* fRecordingCanvas;
junov@chromium.org67d74222013-04-12 13:33:01 +0000252 SkSurface* fSurface;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000253 SkDeferredCanvas::NotificationClient* fNotificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000254 bool fFreshFrame;
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000255 bool fCanDiscardCanvasContents;
junov@chromium.org88e29142012-08-07 16:48:22 +0000256 size_t fMaxRecordingStorageBytes;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000257 size_t fPreviousStorageAllocated;
sugoi@google.com7775fd52012-11-21 15:47:04 +0000258 size_t fBitmapSizeThreshold;
junov@chromium.org88e29142012-08-07 16:48:22 +0000259};
260
robertphillips@google.com9b051a32013-08-20 20:06:40 +0000261DeferredDevice::DeferredDevice(SkDevice* immediateDevice)
262 : SkDevice(SkBitmap::kNo_Config,
263 immediateDevice->width(), immediateDevice->height(),
264 immediateDevice->isOpaque(),
265 immediateDevice->getDeviceProperties()) {
junov@chromium.org67d74222013-04-12 13:33:01 +0000266 fSurface = NULL;
junov@chromium.org9becf002013-04-15 18:15:23 +0000267 fImmediateCanvas = SkNEW_ARGS(SkCanvas, (immediateDevice));
junov@chromium.org7070f762013-05-24 17:13:00 +0000268 fPipeController.setPlaybackCanvas(fImmediateCanvas);
junov@chromium.org67d74222013-04-12 13:33:01 +0000269 this->init();
270}
271
272DeferredDevice::DeferredDevice(SkSurface* surface)
robertphillips@google.com9b051a32013-08-20 20:06:40 +0000273 : SkDevice(SkBitmap::kNo_Config,
274 surface->getCanvas()->getDevice()->width(),
275 surface->getCanvas()->getDevice()->height(),
276 surface->getCanvas()->getDevice()->isOpaque(),
277 surface->getCanvas()->getDevice()->getDeviceProperties()) {
junov@chromium.org67d74222013-04-12 13:33:01 +0000278 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
279 fNotificationClient = NULL;
junov@chromium.org7070f762013-05-24 17:13:00 +0000280 fImmediateCanvas = NULL;
281 fSurface = NULL;
282 this->setSurface(surface);
junov@chromium.org67d74222013-04-12 13:33:01 +0000283 this->init();
284}
285
junov@chromium.org7070f762013-05-24 17:13:00 +0000286void DeferredDevice::setSurface(SkSurface* surface) {
287 SkRefCnt_SafeAssign(fImmediateCanvas, surface->getCanvas());
288 SkRefCnt_SafeAssign(fSurface, surface);
289 fPipeController.setPlaybackCanvas(fImmediateCanvas);
290}
291
junov@chromium.org67d74222013-04-12 13:33:01 +0000292void DeferredDevice::init() {
293 fRecordingCanvas = NULL;
294 fFreshFrame = true;
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000295 fCanDiscardCanvasContents = false;
junov@chromium.org67d74222013-04-12 13:33:01 +0000296 fPreviousStorageAllocated = 0;
297 fBitmapSizeThreshold = kDeferredCanvasBitmapSizeThreshold;
298 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
299 fNotificationClient = NULL;
junov@chromium.org88e29142012-08-07 16:48:22 +0000300 this->beginRecording();
301}
302
303DeferredDevice::~DeferredDevice() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000304 this->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000305 SkSafeUnref(fImmediateCanvas);
junov@chromium.org67d74222013-04-12 13:33:01 +0000306 SkSafeUnref(fSurface);
junov@chromium.org88e29142012-08-07 16:48:22 +0000307}
308
309void DeferredDevice::setMaxRecordingStorage(size_t maxStorage) {
310 fMaxRecordingStorageBytes = maxStorage;
311 this->recordingCanvas(); // Accessing the recording canvas applies the new limit.
312}
313
junov@chromium.org88e29142012-08-07 16:48:22 +0000314void DeferredDevice::beginRecording() {
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000315 SkASSERT(NULL == fRecordingCanvas);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000316 fRecordingCanvas = fPipeWriter.startRecording(&fPipeController, 0,
junov@chromium.org9becf002013-04-15 18:15:23 +0000317 immediateDevice()->width(), immediateDevice()->height());
junov@chromium.org88e29142012-08-07 16:48:22 +0000318}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000319
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000320void DeferredDevice::setNotificationClient(
321 SkDeferredCanvas::NotificationClient* notificationClient) {
junov@chromium.org52805482012-08-20 14:25:04 +0000322 fNotificationClient = notificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000323}
324
junov@chromium.org0a67f962012-09-19 22:48:34 +0000325void DeferredDevice::skipPendingCommands() {
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000326 if (!fRecordingCanvas->isDrawingToLayer()) {
327 fCanDiscardCanvasContents = true;
328 if (fPipeController.hasPendingCommands()) {
329 fFreshFrame = true;
330 flushPendingCommands(kSilent_PlaybackMode);
331 if (fNotificationClient) {
332 fNotificationClient->skippedPendingDrawCommands();
333 }
junov@google.com52a00ca2012-10-01 15:27:14 +0000334 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000335 }
336}
337
338bool DeferredDevice::isFreshFrame() {
339 bool ret = fFreshFrame;
340 fFreshFrame = false;
341 return ret;
342}
343
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000344bool DeferredDevice::hasPendingCommands() {
345 return fPipeController.hasPendingCommands();
346}
347
junov@chromium.org44324fa2013-08-02 15:36:02 +0000348void DeferredDevice::aboutToDraw()
349{
350 if (NULL != fNotificationClient) {
351 fNotificationClient->prepareForDraw();
352 }
353 if (fCanDiscardCanvasContents) {
354 if (NULL != fSurface) {
355 fSurface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
356 }
skia.committer@gmail.comea4b7972013-08-06 07:01:27 +0000357 fCanDiscardCanvasContents = false;
junov@chromium.org44324fa2013-08-02 15:36:02 +0000358 }
359}
360
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000361void DeferredDevice::flushPendingCommands(PlaybackMode playbackMode) {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000362 if (!fPipeController.hasPendingCommands()) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000363 return;
364 }
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000365 if (playbackMode == kNormal_PlaybackMode) {
junov@chromium.org44324fa2013-08-02 15:36:02 +0000366 aboutToDraw();
junov@chromium.org88e29142012-08-07 16:48:22 +0000367 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000368 fPipeWriter.flushRecording(true);
junov@chromium.orgd4501a02012-10-30 19:05:17 +0000369 fPipeController.playback(kSilent_PlaybackMode == playbackMode);
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000370 if (playbackMode == kNormal_PlaybackMode && fNotificationClient) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000371 fNotificationClient->flushedDrawCommands();
372 }
373 fPreviousStorageAllocated = storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000374}
375
376void DeferredDevice::flush() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000377 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000378 fImmediateCanvas->flush();
379}
380
381size_t DeferredDevice::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000382 size_t val = fPipeWriter.freeMemoryIfPossible(bytesToFree);
383 fPreviousStorageAllocated = storageAllocatedForRecording();
384 return val;
junov@chromium.org88e29142012-08-07 16:48:22 +0000385}
386
sugoi@google.com7775fd52012-11-21 15:47:04 +0000387size_t DeferredDevice::getBitmapSizeThreshold() const {
388 return fBitmapSizeThreshold;
389}
390
391void DeferredDevice::setBitmapSizeThreshold(size_t sizeThreshold) {
392 fBitmapSizeThreshold = sizeThreshold;
393}
394
junov@chromium.org88e29142012-08-07 16:48:22 +0000395size_t DeferredDevice::storageAllocatedForRecording() const {
396 return (fPipeController.storageAllocatedForRecording()
397 + fPipeWriter.storageAllocatedForRecording());
398}
399
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000400void DeferredDevice::recordedDrawCommand() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000401 size_t storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000402
junov@chromium.org88e29142012-08-07 16:48:22 +0000403 if (storageAllocated > fMaxRecordingStorageBytes) {
404 // First, attempt to reduce cache without flushing
405 size_t tryFree = storageAllocated - fMaxRecordingStorageBytes;
406 if (this->freeMemoryIfPossible(tryFree) < tryFree) {
407 // Flush is necessary to free more space.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000408 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000409 // Free as much as possible to avoid oscillating around fMaxRecordingStorageBytes
410 // which could cause a high flushing frequency.
bsalomon@google.com100abf42012-09-05 17:40:04 +0000411 this->freeMemoryIfPossible(~0U);
junov@chromium.org88e29142012-08-07 16:48:22 +0000412 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000413 storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000414 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000415
rmistry@google.comd6176b02012-08-23 18:14:13 +0000416 if (fNotificationClient &&
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000417 storageAllocated != fPreviousStorageAllocated) {
418 fPreviousStorageAllocated = storageAllocated;
419 fNotificationClient->storageAllocatedForRecordingChanged(storageAllocated);
420 }
421}
422
423SkCanvas* DeferredDevice::recordingCanvas() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000424 return fRecordingCanvas;
425}
426
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000427SkImage* DeferredDevice::newImageSnapshot() {
junov@chromium.org67d74222013-04-12 13:33:01 +0000428 this->flush();
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000429 return fSurface ? fSurface->newImageSnapshot() : NULL;
junov@chromium.org67d74222013-04-12 13:33:01 +0000430}
431
rmistry@google.comd6176b02012-08-23 18:14:13 +0000432uint32_t DeferredDevice::getDeviceCapabilities() {
junov@chromium.org9becf002013-04-15 18:15:23 +0000433 return immediateDevice()->getDeviceCapabilities();
junov@chromium.org88e29142012-08-07 16:48:22 +0000434}
435
rmistry@google.comd6176b02012-08-23 18:14:13 +0000436int DeferredDevice::width() const {
junov@chromium.org9becf002013-04-15 18:15:23 +0000437 return immediateDevice()->width();
junov@chromium.org88e29142012-08-07 16:48:22 +0000438}
439
440int DeferredDevice::height() const {
junov@chromium.org9becf002013-04-15 18:15:23 +0000441 return immediateDevice()->height();
junov@chromium.org88e29142012-08-07 16:48:22 +0000442}
443
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000444GrRenderTarget* DeferredDevice::accessRenderTarget() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000445 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000446 return immediateDevice()->accessRenderTarget();
junov@chromium.org88e29142012-08-07 16:48:22 +0000447}
448
junov@chromium.org44324fa2013-08-02 15:36:02 +0000449void DeferredDevice::prepareForImmediatePixelWrite() {
450 // The purpose of the following code is to make sure commands are flushed, that
451 // aboutToDraw() is called and that notifyContentWillChange is called, without
452 // calling anything redundantly.
453 if (fPipeController.hasPendingCommands()) {
454 this->flushPendingCommands(kNormal_PlaybackMode);
455 } else {
456 bool mustNotifyDirectly = !fCanDiscardCanvasContents;
457 this->aboutToDraw();
458 if (mustNotifyDirectly) {
459 fSurface->notifyContentWillChange(SkSurface::kRetain_ContentChangeMode);
460 }
461 }
462
463 fImmediateCanvas->flush();
464}
465
junov@chromium.org88e29142012-08-07 16:48:22 +0000466void DeferredDevice::writePixels(const SkBitmap& bitmap,
467 int x, int y, SkCanvas::Config8888 config8888) {
468
469 if (x <= 0 && y <= 0 && (x + bitmap.width()) >= width() &&
470 (y + bitmap.height()) >= height()) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000471 this->skipPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000472 }
473
474 if (SkBitmap::kARGB_8888_Config == bitmap.config() &&
475 SkCanvas::kNative_Premul_Config8888 != config8888 &&
476 kPMColorAlias != config8888) {
477 //Special case config: no deferral
junov@chromium.org44324fa2013-08-02 15:36:02 +0000478 prepareForImmediatePixelWrite();
junov@chromium.org9becf002013-04-15 18:15:23 +0000479 immediateDevice()->writePixels(bitmap, x, y, config8888);
junov@chromium.org88e29142012-08-07 16:48:22 +0000480 return;
481 }
482
483 SkPaint paint;
484 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000485 if (shouldDrawImmediately(&bitmap, NULL, getBitmapSizeThreshold())) {
junov@chromium.org44324fa2013-08-02 15:36:02 +0000486 prepareForImmediatePixelWrite();
junov@chromium.org88e29142012-08-07 16:48:22 +0000487 fImmediateCanvas->drawSprite(bitmap, x, y, &paint);
488 } else {
489 this->recordingCanvas()->drawSprite(bitmap, x, y, &paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000490 this->recordedDrawCommand();
491
junov@chromium.org88e29142012-08-07 16:48:22 +0000492 }
493}
494
robertphillips@google.com9b051a32013-08-20 20:06:40 +0000495const SkBitmap& DeferredDevice::onAccessBitmap(SkBitmap*) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000496 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000497 return immediateDevice()->accessBitmap(false);
junov@chromium.org88e29142012-08-07 16:48:22 +0000498}
499
robertphillips@google.com9b051a32013-08-20 20:06:40 +0000500SkDevice* DeferredDevice::onCreateCompatibleDevice(
junov@chromium.org88e29142012-08-07 16:48:22 +0000501 SkBitmap::Config config, int width, int height, bool isOpaque,
502 Usage usage) {
503
504 // Save layer usage not supported, and not required by SkDeferredCanvas.
505 SkASSERT(usage != kSaveLayer_Usage);
506 // Create a compatible non-deferred device.
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000507 // We do not create a deferred device because we know the new device
508 // will not be used with a deferred canvas (there is no API for that).
509 // And connecting a DeferredDevice to non-deferred canvas can result
510 // in unpredictable behavior.
511 return immediateDevice()->createCompatibleDevice(config, width, height, isOpaque);
junov@chromium.org88e29142012-08-07 16:48:22 +0000512}
513
514bool DeferredDevice::onReadPixels(
515 const SkBitmap& bitmap, int x, int y, SkCanvas::Config8888 config8888) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000516 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000517 return fImmediateCanvas->readPixels(const_cast<SkBitmap*>(&bitmap),
518 x, y, config8888);
519}
520
sugoi@google.com7775fd52012-11-21 15:47:04 +0000521class AutoImmediateDrawIfNeeded {
522public:
523 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkBitmap* bitmap,
524 const SkPaint* paint) {
525 this->init(canvas, bitmap, paint);
526 }
527
528 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkPaint* paint) {
529 this->init(canvas, NULL, paint);
530 }
531
532 ~AutoImmediateDrawIfNeeded() {
533 if (fCanvas) {
534 fCanvas->setDeferredDrawing(true);
535 }
536 }
537private:
538 void init(SkDeferredCanvas& canvas, const SkBitmap* bitmap, const SkPaint* paint)
539 {
540 DeferredDevice* device = static_cast<DeferredDevice*>(canvas.getDevice());
541 if (canvas.isDeferredDrawing() && (NULL != device) &&
542 shouldDrawImmediately(bitmap, paint, device->getBitmapSizeThreshold())) {
543 canvas.setDeferredDrawing(false);
544 fCanvas = &canvas;
545 } else {
546 fCanvas = NULL;
547 }
548 }
549
550 SkDeferredCanvas* fCanvas;
551};
junov@chromium.org88e29142012-08-07 16:48:22 +0000552
junov@chromium.org66070a52013-05-28 17:39:08 +0000553SkDeferredCanvas* SkDeferredCanvas::Create(SkSurface* surface) {
554 SkAutoTUnref<DeferredDevice> deferredDevice(SkNEW_ARGS(DeferredDevice, (surface)));
555 return SkNEW_ARGS(SkDeferredCanvas, (deferredDevice));
556}
557
robertphillips@google.com9b051a32013-08-20 20:06:40 +0000558SkDeferredCanvas* SkDeferredCanvas::Create(SkDevice* device) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000559 SkAutoTUnref<DeferredDevice> deferredDevice(SkNEW_ARGS(DeferredDevice, (device)));
560 return SkNEW_ARGS(SkDeferredCanvas, (deferredDevice));
561}
junov@chromium.org66070a52013-05-28 17:39:08 +0000562
563SkDeferredCanvas::SkDeferredCanvas(DeferredDevice* device) : SkCanvas (device) {
564 this->init();
565}
junov@chromium.org67d74222013-04-12 13:33:01 +0000566
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000567void SkDeferredCanvas::init() {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000568 fDeferredDrawing = true; // On by default
junov@google.com4370aed2012-01-18 16:21:08 +0000569}
570
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000571void SkDeferredCanvas::setMaxRecordingStorage(size_t maxStorage) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000572 this->validate();
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000573 this->getDeferredDevice()->setMaxRecordingStorage(maxStorage);
574}
575
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000576size_t SkDeferredCanvas::storageAllocatedForRecording() const {
577 return this->getDeferredDevice()->storageAllocatedForRecording();
578}
579
580size_t SkDeferredCanvas::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000581 return this->getDeferredDevice()->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000582}
583
sugoi@google.com7775fd52012-11-21 15:47:04 +0000584void SkDeferredCanvas::setBitmapSizeThreshold(size_t sizeThreshold) {
585 DeferredDevice* deferredDevice = this->getDeferredDevice();
586 SkASSERT(deferredDevice);
587 deferredDevice->setBitmapSizeThreshold(sizeThreshold);
588}
589
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000590void SkDeferredCanvas::recordedDrawCommand() {
591 if (fDeferredDrawing) {
592 this->getDeferredDevice()->recordedDrawCommand();
593 }
594}
595
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000596void SkDeferredCanvas::validate() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000597 SkASSERT(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000598}
599
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000600SkCanvas* SkDeferredCanvas::drawingCanvas() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000601 this->validate();
602 return fDeferredDrawing ? this->getDeferredDevice()->recordingCanvas() :
603 this->getDeferredDevice()->immediateCanvas();
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000604}
605
junov@chromium.org88e29142012-08-07 16:48:22 +0000606SkCanvas* SkDeferredCanvas::immediateCanvas() const {
607 this->validate();
608 return this->getDeferredDevice()->immediateCanvas();
609}
610
611DeferredDevice* SkDeferredCanvas::getDeferredDevice() const {
612 return static_cast<DeferredDevice*>(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000613}
614
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000615void SkDeferredCanvas::setDeferredDrawing(bool val) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000616 this->validate(); // Must set device before calling this method
junov@google.com4370aed2012-01-18 16:21:08 +0000617 if (val != fDeferredDrawing) {
618 if (fDeferredDrawing) {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000619 // Going live.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000620 this->getDeferredDevice()->flushPendingCommands(kNormal_PlaybackMode);
junov@google.com4370aed2012-01-18 16:21:08 +0000621 }
622 fDeferredDrawing = val;
623 }
624}
625
junov@chromium.org88e29142012-08-07 16:48:22 +0000626bool SkDeferredCanvas::isDeferredDrawing() const {
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +0000627 return fDeferredDrawing;
628}
629
junov@chromium.org88e29142012-08-07 16:48:22 +0000630bool SkDeferredCanvas::isFreshFrame() const {
631 return this->getDeferredDevice()->isFreshFrame();
632}
633
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000634bool SkDeferredCanvas::hasPendingCommands() const {
635 return this->getDeferredDevice()->hasPendingCommands();
636}
637
junov@chromium.orgfb103892012-09-20 19:35:43 +0000638void SkDeferredCanvas::silentFlush() {
639 if (fDeferredDrawing) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000640 this->getDeferredDevice()->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000641 }
642}
643
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000644SkDeferredCanvas::~SkDeferredCanvas() {
junov@google.com4370aed2012-01-18 16:21:08 +0000645}
646
junov@chromium.org7070f762013-05-24 17:13:00 +0000647SkSurface* SkDeferredCanvas::setSurface(SkSurface* surface) {
648 DeferredDevice* deferredDevice = this->getDeferredDevice();
junov@chromium.org66070a52013-05-28 17:39:08 +0000649 SkASSERT(NULL != deferredDevice);
650 // By swapping the surface into the existing device, we preserve
651 // all pending commands, which can help to seamlessly recover from
652 // a lost accelerated graphics context.
653 deferredDevice->setSurface(surface);
junov@chromium.org7070f762013-05-24 17:13:00 +0000654 return surface;
655}
656
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000657SkDeferredCanvas::NotificationClient* SkDeferredCanvas::setNotificationClient(
658 NotificationClient* notificationClient) {
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000659
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000660 DeferredDevice* deferredDevice = this->getDeferredDevice();
junov@google.com4370aed2012-01-18 16:21:08 +0000661 SkASSERT(deferredDevice);
662 if (deferredDevice) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000663 deferredDevice->setNotificationClient(notificationClient);
junov@google.com4370aed2012-01-18 16:21:08 +0000664 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000665 return notificationClient;
junov@google.com4370aed2012-01-18 16:21:08 +0000666}
667
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000668SkImage* SkDeferredCanvas::newImageSnapshot() {
junov@chromium.org67d74222013-04-12 13:33:01 +0000669 DeferredDevice* deferredDevice = this->getDeferredDevice();
670 SkASSERT(deferredDevice);
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000671 return deferredDevice ? deferredDevice->newImageSnapshot() : NULL;
junov@chromium.org67d74222013-04-12 13:33:01 +0000672}
673
junov@google.com4370aed2012-01-18 16:21:08 +0000674bool SkDeferredCanvas::isFullFrame(const SkRect* rect,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000675 const SkPaint* paint) const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000676 SkCanvas* canvas = this->drawingCanvas();
677 SkISize canvasSize = this->getDeviceSize();
junov@google.com4370aed2012-01-18 16:21:08 +0000678 if (rect) {
679 if (!canvas->getTotalMatrix().rectStaysRect()) {
680 return false; // conservative
681 }
682
683 SkRect transformedRect;
684 canvas->getTotalMatrix().mapRect(&transformedRect, *rect);
685
686 if (paint) {
687 SkPaint::Style paintStyle = paint->getStyle();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000688 if (!(paintStyle == SkPaint::kFill_Style ||
junov@google.com4370aed2012-01-18 16:21:08 +0000689 paintStyle == SkPaint::kStrokeAndFill_Style)) {
690 return false;
691 }
692 if (paint->getMaskFilter() || paint->getLooper()
693 || paint->getPathEffect() || paint->getImageFilter()) {
694 return false; // conservative
695 }
696 }
697
698 // The following test holds with AA enabled, and is conservative
699 // by a 0.5 pixel margin with AA disabled
rmistry@google.comd6176b02012-08-23 18:14:13 +0000700 if (transformedRect.fLeft > SkIntToScalar(0) ||
701 transformedRect.fTop > SkIntToScalar(0) ||
junov@chromium.orgb1e218e2012-02-13 22:27:58 +0000702 transformedRect.fRight < SkIntToScalar(canvasSize.fWidth) ||
703 transformedRect.fBottom < SkIntToScalar(canvasSize.fHeight)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000704 return false;
705 }
706 }
707
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000708 return this->getClipStack()->quickContains(SkRect::MakeXYWH(0, 0,
709 SkIntToScalar(canvasSize.fWidth), SkIntToScalar(canvasSize.fHeight)));
junov@google.com4370aed2012-01-18 16:21:08 +0000710}
711
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000712int SkDeferredCanvas::save(SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000713 this->drawingCanvas()->save(flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000714 int val = this->INHERITED::save(flags);
715 this->recordedDrawCommand();
716
717 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000718}
719
720int SkDeferredCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000721 SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000722 this->drawingCanvas()->saveLayer(bounds, paint, flags);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000723 int count = this->INHERITED::save(flags);
724 this->clipRectBounds(bounds, flags, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000725 this->recordedDrawCommand();
726
junov@chromium.orga907ac32012-02-24 21:54:07 +0000727 return count;
junov@google.com4370aed2012-01-18 16:21:08 +0000728}
729
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000730void SkDeferredCanvas::restore() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000731 this->drawingCanvas()->restore();
junov@chromium.orga907ac32012-02-24 21:54:07 +0000732 this->INHERITED::restore();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000733 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000734}
735
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000736bool SkDeferredCanvas::isDrawingToLayer() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000737 return this->drawingCanvas()->isDrawingToLayer();
junov@google.com4370aed2012-01-18 16:21:08 +0000738}
739
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000740bool SkDeferredCanvas::translate(SkScalar dx, SkScalar dy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000741 this->drawingCanvas()->translate(dx, dy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000742 bool val = this->INHERITED::translate(dx, dy);
743 this->recordedDrawCommand();
744 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000745}
746
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000747bool SkDeferredCanvas::scale(SkScalar sx, SkScalar sy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000748 this->drawingCanvas()->scale(sx, sy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000749 bool val = this->INHERITED::scale(sx, sy);
750 this->recordedDrawCommand();
751 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000752}
753
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000754bool SkDeferredCanvas::rotate(SkScalar degrees) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000755 this->drawingCanvas()->rotate(degrees);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000756 bool val = this->INHERITED::rotate(degrees);
757 this->recordedDrawCommand();
758 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000759}
760
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000761bool SkDeferredCanvas::skew(SkScalar sx, SkScalar sy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000762 this->drawingCanvas()->skew(sx, sy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000763 bool val = this->INHERITED::skew(sx, sy);
764 this->recordedDrawCommand();
765 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000766}
767
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000768bool SkDeferredCanvas::concat(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000769 this->drawingCanvas()->concat(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000770 bool val = this->INHERITED::concat(matrix);
771 this->recordedDrawCommand();
772 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000773}
774
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000775void SkDeferredCanvas::setMatrix(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000776 this->drawingCanvas()->setMatrix(matrix);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000777 this->INHERITED::setMatrix(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000778 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000779}
780
781bool SkDeferredCanvas::clipRect(const SkRect& rect,
782 SkRegion::Op op,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000783 bool doAntiAlias) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000784 this->drawingCanvas()->clipRect(rect, op, doAntiAlias);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000785 bool val = this->INHERITED::clipRect(rect, op, doAntiAlias);
786 this->recordedDrawCommand();
787 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000788}
789
reed@google.com4ed0fb72012-12-12 20:48:18 +0000790bool SkDeferredCanvas::clipRRect(const SkRRect& rrect,
791 SkRegion::Op op,
792 bool doAntiAlias) {
793 this->drawingCanvas()->clipRRect(rrect, op, doAntiAlias);
794 bool val = this->INHERITED::clipRRect(rrect, op, doAntiAlias);
795 this->recordedDrawCommand();
796 return val;
797}
798
junov@google.com4370aed2012-01-18 16:21:08 +0000799bool SkDeferredCanvas::clipPath(const SkPath& path,
800 SkRegion::Op op,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000801 bool doAntiAlias) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000802 this->drawingCanvas()->clipPath(path, op, doAntiAlias);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000803 bool val = this->INHERITED::clipPath(path, op, doAntiAlias);
804 this->recordedDrawCommand();
805 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000806}
807
808bool SkDeferredCanvas::clipRegion(const SkRegion& deviceRgn,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000809 SkRegion::Op op) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000810 this->drawingCanvas()->clipRegion(deviceRgn, op);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000811 bool val = this->INHERITED::clipRegion(deviceRgn, op);
812 this->recordedDrawCommand();
813 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000814}
815
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000816void SkDeferredCanvas::clear(SkColor color) {
junov@google.com4370aed2012-01-18 16:21:08 +0000817 // purge pending commands
818 if (fDeferredDrawing) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000819 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000820 }
821
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000822 this->drawingCanvas()->clear(color);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000823 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000824}
825
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000826void SkDeferredCanvas::drawPaint(const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000827 if (fDeferredDrawing && this->isFullFrame(NULL, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000828 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000829 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000830 }
junov@chromium.org10f7f972012-07-31 21:01:51 +0000831 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000832 this->drawingCanvas()->drawPaint(paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000833 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000834}
835
836void SkDeferredCanvas::drawPoints(PointMode mode, size_t count,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000837 const SkPoint pts[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000838 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000839 this->drawingCanvas()->drawPoints(mode, count, pts, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000840 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000841}
842
reed@google.com4ed0fb72012-12-12 20:48:18 +0000843void SkDeferredCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
844 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
845 this->drawingCanvas()->drawOval(rect, paint);
846 this->recordedDrawCommand();
847}
848
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000849void SkDeferredCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000850 if (fDeferredDrawing && this->isFullFrame(&rect, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000851 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000852 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000853 }
skia.committer@gmail.com306ab9d2012-12-13 02:01:33 +0000854
junov@chromium.org10f7f972012-07-31 21:01:51 +0000855 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000856 this->drawingCanvas()->drawRect(rect, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000857 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000858}
859
reed@google.com4ed0fb72012-12-12 20:48:18 +0000860void SkDeferredCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
861 if (rrect.isRect()) {
862 this->SkDeferredCanvas::drawRect(rrect.getBounds(), paint);
863 } else if (rrect.isOval()) {
864 this->SkDeferredCanvas::drawOval(rrect.getBounds(), paint);
865 } else {
866 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
867 this->drawingCanvas()->drawRRect(rrect, paint);
868 this->recordedDrawCommand();
869 }
870}
871
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000872void SkDeferredCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000873 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000874 this->drawingCanvas()->drawPath(path, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000875 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000876}
877
878void SkDeferredCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000879 SkScalar top, const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000880 SkRect bitmapRect = SkRect::MakeXYWH(left, top,
881 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000882 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000883 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000884 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000885 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000886 }
887
junov@chromium.org10f7f972012-07-31 21:01:51 +0000888 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000889 this->drawingCanvas()->drawBitmap(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000890 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000891}
892
reed@google.com71121732012-09-18 15:14:33 +0000893void SkDeferredCanvas::drawBitmapRectToRect(const SkBitmap& bitmap,
894 const SkRect* src,
895 const SkRect& dst,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000896 const SkPaint* paint,
897 DrawBitmapRectFlags flags) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000898 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000899 this->isFullFrame(&dst, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000900 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000901 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000902 }
903
junov@chromium.org10f7f972012-07-31 21:01:51 +0000904 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000905 this->drawingCanvas()->drawBitmapRectToRect(bitmap, src, dst, paint, flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000906 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000907}
908
909
910void SkDeferredCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
911 const SkMatrix& m,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000912 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000913 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
914 // covers canvas entirely and transformed bitmap covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000915 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000916 this->drawingCanvas()->drawBitmapMatrix(bitmap, m, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000917 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000918}
919
920void SkDeferredCanvas::drawBitmapNine(const SkBitmap& bitmap,
921 const SkIRect& center, const SkRect& dst,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000922 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000923 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
924 // covers canvas entirely and dst covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000925 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000926 this->drawingCanvas()->drawBitmapNine(bitmap, center, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000927 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000928}
929
930void SkDeferredCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000931 const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000932 SkRect bitmapRect = SkRect::MakeXYWH(
933 SkIntToScalar(left),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000934 SkIntToScalar(top),
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000935 SkIntToScalar(bitmap.width()),
936 SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000937 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000938 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000939 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000940 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000941 }
942
junov@chromium.org10f7f972012-07-31 21:01:51 +0000943 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000944 this->drawingCanvas()->drawSprite(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000945 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000946}
947
948void SkDeferredCanvas::drawText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000949 SkScalar x, SkScalar y, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000950 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000951 this->drawingCanvas()->drawText(text, byteLength, x, y, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000952 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000953}
954
955void SkDeferredCanvas::drawPosText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000956 const SkPoint pos[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000957 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000958 this->drawingCanvas()->drawPosText(text, byteLength, pos, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000959 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000960}
961
962void SkDeferredCanvas::drawPosTextH(const void* text, size_t byteLength,
963 const SkScalar xpos[], SkScalar constY,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000964 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000965 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000966 this->drawingCanvas()->drawPosTextH(text, byteLength, xpos, constY, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000967 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000968}
969
970void SkDeferredCanvas::drawTextOnPath(const void* text, size_t byteLength,
971 const SkPath& path,
972 const SkMatrix* matrix,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000973 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000974 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000975 this->drawingCanvas()->drawTextOnPath(text, byteLength, path, matrix, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000976 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000977}
978
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000979void SkDeferredCanvas::drawPicture(SkPicture& picture) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000980 this->drawingCanvas()->drawPicture(picture);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000981 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000982}
983
984void SkDeferredCanvas::drawVertices(VertexMode vmode, int vertexCount,
985 const SkPoint vertices[],
986 const SkPoint texs[],
987 const SkColor colors[], SkXfermode* xmode,
988 const uint16_t indices[], int indexCount,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000989 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000990 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000991 this->drawingCanvas()->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode,
992 indices, indexCount, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000993 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000994}
995
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000996SkBounder* SkDeferredCanvas::setBounder(SkBounder* bounder) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000997 this->drawingCanvas()->setBounder(bounder);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000998 this->INHERITED::setBounder(bounder);
999 this->recordedDrawCommand();
1000 return bounder;
junov@google.com4370aed2012-01-18 16:21:08 +00001001}
1002
junov@chromium.orgc16ca922012-02-24 22:06:27 +00001003SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) {
rmistry@google.comd6176b02012-08-23 18:14:13 +00001004 this->drawingCanvas()->setDrawFilter(filter);
junov@chromium.org9ed02b92012-08-14 13:36:26 +00001005 this->INHERITED::setDrawFilter(filter);
1006 this->recordedDrawCommand();
rmistry@google.comd6176b02012-08-23 18:14:13 +00001007 return filter;
junov@google.com4370aed2012-01-18 16:21:08 +00001008}
1009
1010SkCanvas* SkDeferredCanvas::canvasForDrawIter() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +00001011 return this->drawingCanvas();
junov@google.com4370aed2012-01-18 16:21:08 +00001012}