blob: 2c5e5301c01741e0c57815f3ebecda4b4f6cd6a3 [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
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000011#include "SkBitmapDevice.h"
junov@chromium.org88e29142012-08-07 16:48:22 +000012#include "SkChunkAlloc.h"
13#include "SkColorFilter.h"
junov@chromium.org88e29142012-08-07 16:48:22 +000014#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
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +000033static bool shouldDrawImmediately(const SkBitmap* bitmap, const SkPaint* paint,
sugoi@google.com7775fd52012-11-21 15:47:04 +000034 size_t bitmapSizeThreshold) {
35 if (bitmap && ((bitmap->getTexture() && !bitmap->isImmutable()) ||
36 (bitmap->getSize() > bitmapSizeThreshold))) {
junov@chromium.org10f7f972012-07-31 21:01:51 +000037 return true;
38 }
39 if (paint) {
40 SkShader* shader = paint->getShader();
41 // Here we detect the case where the shader is an SkBitmapProcShader
42 // with a gpu texture attached. Checking this without RTTI
43 // requires making the assumption that only gradient shaders
44 // and SkBitmapProcShader implement asABitmap(). The following
45 // code may need to be revised if that assumption is ever broken.
46 if (shader && !shader->asAGradient(NULL)) {
47 SkBitmap bm;
rmistry@google.comd6176b02012-08-23 18:14:13 +000048 if (shader->asABitmap(&bm, NULL, NULL) &&
junov@chromium.org10f7f972012-07-31 21:01:51 +000049 NULL != bm.getTexture()) {
50 return true;
51 }
52 }
53 }
54 return false;
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +000055}
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +000056
junov@chromium.org88e29142012-08-07 16:48:22 +000057//-----------------------------------------------------------------------------
58// DeferredPipeController
59//-----------------------------------------------------------------------------
60
61class DeferredPipeController : public SkGPipeController {
62public:
63 DeferredPipeController();
64 void setPlaybackCanvas(SkCanvas*);
65 virtual ~DeferredPipeController();
66 virtual void* requestBlock(size_t minRequest, size_t* actual) SK_OVERRIDE;
67 virtual void notifyWritten(size_t bytes) SK_OVERRIDE;
junov@chromium.orgfb103892012-09-20 19:35:43 +000068 void playback(bool silent);
junov@chromium.orga38dfb62012-09-20 22:10:33 +000069 bool hasPendingCommands() const { return fAllocator.blockCount() != 0; }
junov@chromium.org88e29142012-08-07 16:48:22 +000070 size_t storageAllocatedForRecording() const { return fAllocator.totalCapacity(); }
71private:
72 enum {
73 kMinBlockSize = 4096
74 };
75 struct PipeBlock {
76 PipeBlock(void* block, size_t size) { fBlock = block, fSize = size; }
77 void* fBlock;
78 size_t fSize;
79 };
80 void* fBlock;
81 size_t fBytesWritten;
82 SkChunkAlloc fAllocator;
83 SkTDArray<PipeBlock> fBlockList;
84 SkGPipeReader fReader;
85};
86
87DeferredPipeController::DeferredPipeController() :
88 fAllocator(kMinBlockSize) {
89 fBlock = NULL;
90 fBytesWritten = 0;
91}
92
93DeferredPipeController::~DeferredPipeController() {
94 fAllocator.reset();
95}
96
97void DeferredPipeController::setPlaybackCanvas(SkCanvas* canvas) {
98 fReader.setCanvas(canvas);
99}
100
101void* DeferredPipeController::requestBlock(size_t minRequest, size_t *actual) {
102 if (fBlock) {
103 // Save the previous block for later
104 PipeBlock previousBloc(fBlock, fBytesWritten);
105 fBlockList.push(previousBloc);
106 }
robertphillips@google.comadacc702013-10-14 21:53:24 +0000107 size_t blockSize = SkTMax<size_t>(minRequest, kMinBlockSize);
junov@chromium.org88e29142012-08-07 16:48:22 +0000108 fBlock = fAllocator.allocThrow(blockSize);
109 fBytesWritten = 0;
110 *actual = blockSize;
111 return fBlock;
112}
113
114void DeferredPipeController::notifyWritten(size_t bytes) {
115 fBytesWritten += bytes;
116}
117
junov@chromium.orgfb103892012-09-20 19:35:43 +0000118void DeferredPipeController::playback(bool silent) {
119 uint32_t flags = silent ? SkGPipeReader::kSilent_PlaybackFlag : 0;
junov@chromium.org88e29142012-08-07 16:48:22 +0000120 for (int currentBlock = 0; currentBlock < fBlockList.count(); currentBlock++ ) {
junov@chromium.orgfb103892012-09-20 19:35:43 +0000121 fReader.playback(fBlockList[currentBlock].fBlock, fBlockList[currentBlock].fSize,
122 flags);
junov@chromium.org88e29142012-08-07 16:48:22 +0000123 }
124 fBlockList.reset();
125
126 if (fBlock) {
junov@chromium.orgfb103892012-09-20 19:35:43 +0000127 fReader.playback(fBlock, fBytesWritten, flags);
junov@chromium.org88e29142012-08-07 16:48:22 +0000128 fBlock = NULL;
129 }
130
131 // Release all allocated blocks
132 fAllocator.reset();
133}
134
junov@chromium.org88e29142012-08-07 16:48:22 +0000135//-----------------------------------------------------------------------------
reed@google.com9c135db2014-03-12 18:28:35 +0000136// SkDeferredDevice
junov@chromium.org88e29142012-08-07 16:48:22 +0000137//-----------------------------------------------------------------------------
reed@google.com9c135db2014-03-12 18:28:35 +0000138class SkDeferredDevice : public SkBaseDevice {
junov@chromium.org88e29142012-08-07 16:48:22 +0000139public:
reed@google.com9c135db2014-03-12 18:28:35 +0000140 explicit SkDeferredDevice(SkSurface* surface);
141 ~SkDeferredDevice();
junov@chromium.org88e29142012-08-07 16:48:22 +0000142
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000143 void setNotificationClient(SkDeferredCanvas::NotificationClient* notificationClient);
junov@chromium.org88e29142012-08-07 16:48:22 +0000144 SkCanvas* recordingCanvas();
145 SkCanvas* immediateCanvas() const {return fImmediateCanvas;}
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000146 SkBaseDevice* immediateDevice() const {return fImmediateCanvas->getTopDevice();}
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000147 SkImage* newImageSnapshot();
junov@chromium.org7070f762013-05-24 17:13:00 +0000148 void setSurface(SkSurface* surface);
junov@chromium.org88e29142012-08-07 16:48:22 +0000149 bool isFreshFrame();
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000150 bool hasPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000151 size_t storageAllocatedForRecording() const;
152 size_t freeMemoryIfPossible(size_t bytesToFree);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000153 size_t getBitmapSizeThreshold() const;
154 void setBitmapSizeThreshold(size_t sizeThreshold);
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000155 void flushPendingCommands(PlaybackMode);
junov@chromium.org0a67f962012-09-19 22:48:34 +0000156 void skipPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000157 void setMaxRecordingStorage(size_t);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000158 void recordedDrawCommand();
junov@chromium.org88e29142012-08-07 16:48:22 +0000159
junov@chromium.org88e29142012-08-07 16:48:22 +0000160 virtual int width() const SK_OVERRIDE;
161 virtual int height() const SK_OVERRIDE;
reed@google.com284a84d2014-02-14 15:23:15 +0000162 virtual SkBitmap::Config config() const SK_OVERRIDE;
163 virtual bool isOpaque() const SK_OVERRIDE;
164 virtual SkImageInfo imageInfo() const SK_OVERRIDE;
165
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000166 virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE;
junov@chromium.org88e29142012-08-07 16:48:22 +0000167
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000168 virtual SkBaseDevice* onCreateDevice(const SkImageInfo&, Usage) SK_OVERRIDE;
junov@chromium.org88e29142012-08-07 16:48:22 +0000169
reed@google.com76f10a32014-02-05 15:32:21 +0000170 virtual SkSurface* newSurface(const SkImageInfo&) SK_OVERRIDE;
171
junov@chromium.org88e29142012-08-07 16:48:22 +0000172protected:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000173 virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE;
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000174 virtual bool onReadPixels(const SkImageInfo&, void*, size_t, int x, int y) SK_OVERRIDE;
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000175 virtual bool onWritePixels(const SkImageInfo&, const void*, size_t, int x, int y) SK_OVERRIDE;
junov@chromium.org88e29142012-08-07 16:48:22 +0000176
177 // The following methods are no-ops on a deferred device
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000178 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE {
179 return false;
180 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000181
182 // None of the following drawing methods should ever get called on the
183 // deferred device
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000184 virtual void clear(SkColor color) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000185 {SkASSERT(0);}
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000186 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000187 {SkASSERT(0);}
188 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
189 size_t count, const SkPoint[],
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000190 const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000191 {SkASSERT(0);}
192 virtual void drawRect(const SkDraw&, const SkRect& r,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000193 const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000194 {SkASSERT(0);}
reed@google.com284a84d2014-02-14 15:23:15 +0000195 virtual void drawOval(const SkDraw&, const SkRect&, const SkPaint&) SK_OVERRIDE
196 {SkASSERT(0);}
scroggo@google.comcac8d012013-11-12 17:10:02 +0000197 virtual void drawRRect(const SkDraw&, const SkRRect& rr,
198 const SkPaint& paint) SK_OVERRIDE
reed@google.com284a84d2014-02-14 15:23:15 +0000199 {SkASSERT(0);}
junov@chromium.org88e29142012-08-07 16:48:22 +0000200 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);}
reed@google.com284a84d2014-02-14 15:23:15 +0000208 virtual void drawBitmapRect(const SkDraw&, const SkBitmap&, const SkRect*,
209 const SkRect&, const SkPaint&,
210 SkCanvas::DrawBitmapRectFlags) SK_OVERRIDE
211 {SkASSERT(0);}
junov@chromium.org88e29142012-08-07 16:48:22 +0000212 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000213 int x, int y, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000214 {SkASSERT(0);}
215 virtual void drawText(const SkDraw&, const void* text, size_t len,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000216 SkScalar x, SkScalar y, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000217 {SkASSERT(0);}
218 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
219 const SkScalar pos[], SkScalar constY,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000220 int scalarsPerPos, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000221 {SkASSERT(0);}
222 virtual void drawTextOnPath(const SkDraw&, const void* text,
223 size_t len, const SkPath& path,
224 const SkMatrix* matrix,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000225 const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000226 {SkASSERT(0);}
junov@chromium.org88e29142012-08-07 16:48:22 +0000227 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
228 int vertexCount, const SkPoint verts[],
229 const SkPoint texs[], const SkColor colors[],
230 SkXfermode* xmode, const uint16_t indices[],
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000231 int indexCount, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000232 {SkASSERT(0);}
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000233 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000234 const SkPaint&) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000235 {SkASSERT(0);}
reed@google.com284a84d2014-02-14 15:23:15 +0000236
237 virtual void lockPixels() SK_OVERRIDE {}
238 virtual void unlockPixels() SK_OVERRIDE {}
239
240 virtual bool allowImageFilter(const SkImageFilter*) SK_OVERRIDE {
241 return false;
242 }
243 virtual bool canHandleImageFilter(const SkImageFilter*) SK_OVERRIDE {
244 return false;
245 }
246 virtual bool filterImage(const SkImageFilter*, const SkBitmap&,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000247 const SkImageFilter::Context&, SkBitmap*, SkIPoint*) SK_OVERRIDE {
reed@google.com284a84d2014-02-14 15:23:15 +0000248 return false;
249 }
250
junov@chromium.org88e29142012-08-07 16:48:22 +0000251private:
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000252 virtual void flush() SK_OVERRIDE;
reed@google.com284a84d2014-02-14 15:23:15 +0000253 virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE {}
junov@chromium.org88e29142012-08-07 16:48:22 +0000254
junov@chromium.org88e29142012-08-07 16:48:22 +0000255 void beginRecording();
junov@chromium.org67d74222013-04-12 13:33:01 +0000256 void init();
junov@chromium.org44324fa2013-08-02 15:36:02 +0000257 void aboutToDraw();
258 void prepareForImmediatePixelWrite();
junov@chromium.org88e29142012-08-07 16:48:22 +0000259
260 DeferredPipeController fPipeController;
261 SkGPipeWriter fPipeWriter;
junov@chromium.org88e29142012-08-07 16:48:22 +0000262 SkCanvas* fImmediateCanvas;
263 SkCanvas* fRecordingCanvas;
junov@chromium.org67d74222013-04-12 13:33:01 +0000264 SkSurface* fSurface;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000265 SkDeferredCanvas::NotificationClient* fNotificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000266 bool fFreshFrame;
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000267 bool fCanDiscardCanvasContents;
junov@chromium.org88e29142012-08-07 16:48:22 +0000268 size_t fMaxRecordingStorageBytes;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000269 size_t fPreviousStorageAllocated;
sugoi@google.com7775fd52012-11-21 15:47:04 +0000270 size_t fBitmapSizeThreshold;
junov@chromium.org88e29142012-08-07 16:48:22 +0000271};
272
reed@google.com9c135db2014-03-12 18:28:35 +0000273SkDeferredDevice::SkDeferredDevice(SkSurface* surface) {
junov@chromium.org67d74222013-04-12 13:33:01 +0000274 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
275 fNotificationClient = NULL;
junov@chromium.org7070f762013-05-24 17:13:00 +0000276 fImmediateCanvas = NULL;
277 fSurface = NULL;
278 this->setSurface(surface);
junov@chromium.org67d74222013-04-12 13:33:01 +0000279 this->init();
280}
281
reed@google.com9c135db2014-03-12 18:28:35 +0000282void SkDeferredDevice::setSurface(SkSurface* surface) {
junov@chromium.org7070f762013-05-24 17:13:00 +0000283 SkRefCnt_SafeAssign(fImmediateCanvas, surface->getCanvas());
284 SkRefCnt_SafeAssign(fSurface, surface);
285 fPipeController.setPlaybackCanvas(fImmediateCanvas);
286}
287
reed@google.com9c135db2014-03-12 18:28:35 +0000288void SkDeferredDevice::init() {
junov@chromium.org67d74222013-04-12 13:33:01 +0000289 fRecordingCanvas = NULL;
290 fFreshFrame = true;
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000291 fCanDiscardCanvasContents = false;
junov@chromium.org67d74222013-04-12 13:33:01 +0000292 fPreviousStorageAllocated = 0;
293 fBitmapSizeThreshold = kDeferredCanvasBitmapSizeThreshold;
294 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
295 fNotificationClient = NULL;
junov@chromium.org88e29142012-08-07 16:48:22 +0000296 this->beginRecording();
297}
298
reed@google.com9c135db2014-03-12 18:28:35 +0000299SkDeferredDevice::~SkDeferredDevice() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000300 this->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000301 SkSafeUnref(fImmediateCanvas);
junov@chromium.org67d74222013-04-12 13:33:01 +0000302 SkSafeUnref(fSurface);
junov@chromium.org88e29142012-08-07 16:48:22 +0000303}
304
reed@google.com9c135db2014-03-12 18:28:35 +0000305void SkDeferredDevice::setMaxRecordingStorage(size_t maxStorage) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000306 fMaxRecordingStorageBytes = maxStorage;
307 this->recordingCanvas(); // Accessing the recording canvas applies the new limit.
308}
309
reed@google.com9c135db2014-03-12 18:28:35 +0000310void SkDeferredDevice::beginRecording() {
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000311 SkASSERT(NULL == fRecordingCanvas);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000312 fRecordingCanvas = fPipeWriter.startRecording(&fPipeController, 0,
junov@chromium.org9becf002013-04-15 18:15:23 +0000313 immediateDevice()->width(), immediateDevice()->height());
junov@chromium.org88e29142012-08-07 16:48:22 +0000314}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000315
reed@google.com9c135db2014-03-12 18:28:35 +0000316void SkDeferredDevice::setNotificationClient(
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000317 SkDeferredCanvas::NotificationClient* notificationClient) {
junov@chromium.org52805482012-08-20 14:25:04 +0000318 fNotificationClient = notificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000319}
320
reed@google.com9c135db2014-03-12 18:28:35 +0000321void SkDeferredDevice::skipPendingCommands() {
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000322 if (!fRecordingCanvas->isDrawingToLayer()) {
323 fCanDiscardCanvasContents = true;
324 if (fPipeController.hasPendingCommands()) {
325 fFreshFrame = true;
326 flushPendingCommands(kSilent_PlaybackMode);
327 if (fNotificationClient) {
328 fNotificationClient->skippedPendingDrawCommands();
329 }
junov@google.com52a00ca2012-10-01 15:27:14 +0000330 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000331 }
332}
333
reed@google.com9c135db2014-03-12 18:28:35 +0000334bool SkDeferredDevice::isFreshFrame() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000335 bool ret = fFreshFrame;
336 fFreshFrame = false;
337 return ret;
338}
339
reed@google.com9c135db2014-03-12 18:28:35 +0000340bool SkDeferredDevice::hasPendingCommands() {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000341 return fPipeController.hasPendingCommands();
342}
343
reed@google.com9c135db2014-03-12 18:28:35 +0000344void SkDeferredDevice::aboutToDraw()
junov@chromium.org44324fa2013-08-02 15:36:02 +0000345{
346 if (NULL != fNotificationClient) {
347 fNotificationClient->prepareForDraw();
348 }
349 if (fCanDiscardCanvasContents) {
350 if (NULL != fSurface) {
351 fSurface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
352 }
skia.committer@gmail.comea4b7972013-08-06 07:01:27 +0000353 fCanDiscardCanvasContents = false;
junov@chromium.org44324fa2013-08-02 15:36:02 +0000354 }
355}
356
reed@google.com9c135db2014-03-12 18:28:35 +0000357void SkDeferredDevice::flushPendingCommands(PlaybackMode playbackMode) {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000358 if (!fPipeController.hasPendingCommands()) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000359 return;
360 }
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000361 if (playbackMode == kNormal_PlaybackMode) {
junov@chromium.org44324fa2013-08-02 15:36:02 +0000362 aboutToDraw();
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
reed@google.com9c135db2014-03-12 18:28:35 +0000372void SkDeferredDevice::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
reed@google.com9c135db2014-03-12 18:28:35 +0000377size_t SkDeferredDevice::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
reed@google.com9c135db2014-03-12 18:28:35 +0000383size_t SkDeferredDevice::getBitmapSizeThreshold() const {
sugoi@google.com7775fd52012-11-21 15:47:04 +0000384 return fBitmapSizeThreshold;
385}
386
reed@google.com9c135db2014-03-12 18:28:35 +0000387void SkDeferredDevice::setBitmapSizeThreshold(size_t sizeThreshold) {
sugoi@google.com7775fd52012-11-21 15:47:04 +0000388 fBitmapSizeThreshold = sizeThreshold;
389}
390
reed@google.com9c135db2014-03-12 18:28:35 +0000391size_t SkDeferredDevice::storageAllocatedForRecording() const {
junov@chromium.org88e29142012-08-07 16:48:22 +0000392 return (fPipeController.storageAllocatedForRecording()
393 + fPipeWriter.storageAllocatedForRecording());
394}
395
reed@google.com9c135db2014-03-12 18:28:35 +0000396void SkDeferredDevice::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
reed@google.com9c135db2014-03-12 18:28:35 +0000419SkCanvas* SkDeferredDevice::recordingCanvas() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000420 return fRecordingCanvas;
421}
422
reed@google.com9c135db2014-03-12 18:28:35 +0000423SkImage* SkDeferredDevice::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
reed@google.com9c135db2014-03-12 18:28:35 +0000428int SkDeferredDevice::width() const {
junov@chromium.org9becf002013-04-15 18:15:23 +0000429 return immediateDevice()->width();
junov@chromium.org88e29142012-08-07 16:48:22 +0000430}
431
reed@google.com9c135db2014-03-12 18:28:35 +0000432int SkDeferredDevice::height() const {
junov@chromium.org9becf002013-04-15 18:15:23 +0000433 return immediateDevice()->height();
junov@chromium.org88e29142012-08-07 16:48:22 +0000434}
435
reed@google.com9c135db2014-03-12 18:28:35 +0000436SkBitmap::Config SkDeferredDevice::config() const {
reed@google.com284a84d2014-02-14 15:23:15 +0000437 return immediateDevice()->config();
438}
439
reed@google.com9c135db2014-03-12 18:28:35 +0000440bool SkDeferredDevice::isOpaque() const {
reed@google.com284a84d2014-02-14 15:23:15 +0000441 return immediateDevice()->isOpaque();
442}
443
reed@google.com9c135db2014-03-12 18:28:35 +0000444SkImageInfo SkDeferredDevice::imageInfo() const {
reed@google.com284a84d2014-02-14 15:23:15 +0000445 return immediateDevice()->imageInfo();
446}
447
reed@google.com9c135db2014-03-12 18:28:35 +0000448GrRenderTarget* SkDeferredDevice::accessRenderTarget() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000449 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000450 return immediateDevice()->accessRenderTarget();
junov@chromium.org88e29142012-08-07 16:48:22 +0000451}
452
reed@google.com9c135db2014-03-12 18:28:35 +0000453void SkDeferredDevice::prepareForImmediatePixelWrite() {
junov@chromium.org44324fa2013-08-02 15:36:02 +0000454 // The purpose of the following code is to make sure commands are flushed, that
455 // aboutToDraw() is called and that notifyContentWillChange is called, without
456 // calling anything redundantly.
457 if (fPipeController.hasPendingCommands()) {
458 this->flushPendingCommands(kNormal_PlaybackMode);
459 } else {
460 bool mustNotifyDirectly = !fCanDiscardCanvasContents;
461 this->aboutToDraw();
462 if (mustNotifyDirectly) {
463 fSurface->notifyContentWillChange(SkSurface::kRetain_ContentChangeMode);
464 }
465 }
466
467 fImmediateCanvas->flush();
468}
469
reed@google.com9c135db2014-03-12 18:28:35 +0000470bool SkDeferredDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000471 int x, int y) {
472 SkASSERT(x >= 0 && y >= 0);
473 SkASSERT(x + info.width() <= width());
474 SkASSERT(y + info.height() <= height());
475
476 this->flushPendingCommands(kNormal_PlaybackMode);
477
478 const SkImageInfo deviceInfo = this->imageInfo();
479 if (info.width() == deviceInfo.width() && info.height() == deviceInfo.height()) {
480 this->skipPendingCommands();
481 }
skia.committer@gmail.come62513f2014-03-08 03:02:06 +0000482
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000483 this->prepareForImmediatePixelWrite();
484 return immediateDevice()->onWritePixels(info, pixels, rowBytes, x, y);
485}
junov@chromium.org88e29142012-08-07 16:48:22 +0000486
reed@google.com9c135db2014-03-12 18:28:35 +0000487const SkBitmap& SkDeferredDevice::onAccessBitmap() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000488 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000489 return immediateDevice()->accessBitmap(false);
junov@chromium.org88e29142012-08-07 16:48:22 +0000490}
491
reed@google.com9c135db2014-03-12 18:28:35 +0000492SkBaseDevice* SkDeferredDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000493 // Save layer usage not supported, and not required by SkDeferredCanvas.
494 SkASSERT(usage != kSaveLayer_Usage);
495 // Create a compatible non-deferred device.
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000496 // We do not create a deferred device because we know the new device
497 // will not be used with a deferred canvas (there is no API for that).
reed@google.com9c135db2014-03-12 18:28:35 +0000498 // And connecting a SkDeferredDevice to non-deferred canvas can result
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000499 // in unpredictable behavior.
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000500 return immediateDevice()->createCompatibleDevice(info);
junov@chromium.org88e29142012-08-07 16:48:22 +0000501}
502
reed@google.com9c135db2014-03-12 18:28:35 +0000503SkSurface* SkDeferredDevice::newSurface(const SkImageInfo& info) {
reed@google.com76f10a32014-02-05 15:32:21 +0000504 return this->immediateDevice()->newSurface(info);
505}
506
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000507bool SkDeferredDevice::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
508 int x, int y) {
509 this->flushPendingCommands(kNormal_PlaybackMode);
510 return fImmediateCanvas->readPixels(info, pixels, rowBytes, x, y);
511}
junov@chromium.org88e29142012-08-07 16:48:22 +0000512
sugoi@google.com7775fd52012-11-21 15:47:04 +0000513class AutoImmediateDrawIfNeeded {
514public:
515 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkBitmap* bitmap,
516 const SkPaint* paint) {
517 this->init(canvas, bitmap, paint);
518 }
519
520 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkPaint* paint) {
521 this->init(canvas, NULL, paint);
522 }
523
524 ~AutoImmediateDrawIfNeeded() {
525 if (fCanvas) {
526 fCanvas->setDeferredDrawing(true);
527 }
528 }
529private:
530 void init(SkDeferredCanvas& canvas, const SkBitmap* bitmap, const SkPaint* paint)
531 {
reed@google.com9c135db2014-03-12 18:28:35 +0000532 SkDeferredDevice* device = static_cast<SkDeferredDevice*>(canvas.getDevice());
sugoi@google.com7775fd52012-11-21 15:47:04 +0000533 if (canvas.isDeferredDrawing() && (NULL != device) &&
534 shouldDrawImmediately(bitmap, paint, device->getBitmapSizeThreshold())) {
535 canvas.setDeferredDrawing(false);
536 fCanvas = &canvas;
537 } else {
538 fCanvas = NULL;
539 }
540 }
541
542 SkDeferredCanvas* fCanvas;
543};
junov@chromium.org88e29142012-08-07 16:48:22 +0000544
junov@chromium.org66070a52013-05-28 17:39:08 +0000545SkDeferredCanvas* SkDeferredCanvas::Create(SkSurface* surface) {
reed@google.com9c135db2014-03-12 18:28:35 +0000546 SkAutoTUnref<SkDeferredDevice> deferredDevice(SkNEW_ARGS(SkDeferredDevice, (surface)));
junov@chromium.org66070a52013-05-28 17:39:08 +0000547 return SkNEW_ARGS(SkDeferredCanvas, (deferredDevice));
548}
549
reed@google.com9c135db2014-03-12 18:28:35 +0000550SkDeferredCanvas::SkDeferredCanvas(SkDeferredDevice* device) : SkCanvas (device) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000551 this->init();
552}
junov@chromium.org67d74222013-04-12 13:33:01 +0000553
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000554void SkDeferredCanvas::init() {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000555 fDeferredDrawing = true; // On by default
junov@google.com4370aed2012-01-18 16:21:08 +0000556}
557
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000558void SkDeferredCanvas::setMaxRecordingStorage(size_t maxStorage) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000559 this->validate();
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000560 this->getDeferredDevice()->setMaxRecordingStorage(maxStorage);
561}
562
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000563size_t SkDeferredCanvas::storageAllocatedForRecording() const {
564 return this->getDeferredDevice()->storageAllocatedForRecording();
565}
566
567size_t SkDeferredCanvas::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000568 return this->getDeferredDevice()->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000569}
570
sugoi@google.com7775fd52012-11-21 15:47:04 +0000571void SkDeferredCanvas::setBitmapSizeThreshold(size_t sizeThreshold) {
reed@google.com9c135db2014-03-12 18:28:35 +0000572 SkDeferredDevice* deferredDevice = this->getDeferredDevice();
sugoi@google.com7775fd52012-11-21 15:47:04 +0000573 SkASSERT(deferredDevice);
574 deferredDevice->setBitmapSizeThreshold(sizeThreshold);
575}
576
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000577void SkDeferredCanvas::recordedDrawCommand() {
578 if (fDeferredDrawing) {
579 this->getDeferredDevice()->recordedDrawCommand();
580 }
581}
582
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000583void SkDeferredCanvas::validate() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000584 SkASSERT(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000585}
586
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000587SkCanvas* SkDeferredCanvas::drawingCanvas() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000588 this->validate();
589 return fDeferredDrawing ? this->getDeferredDevice()->recordingCanvas() :
590 this->getDeferredDevice()->immediateCanvas();
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000591}
592
junov@chromium.org88e29142012-08-07 16:48:22 +0000593SkCanvas* SkDeferredCanvas::immediateCanvas() const {
594 this->validate();
595 return this->getDeferredDevice()->immediateCanvas();
596}
597
reed@google.com9c135db2014-03-12 18:28:35 +0000598SkDeferredDevice* SkDeferredCanvas::getDeferredDevice() const {
599 return static_cast<SkDeferredDevice*>(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000600}
601
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000602void SkDeferredCanvas::setDeferredDrawing(bool val) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000603 this->validate(); // Must set device before calling this method
junov@google.com4370aed2012-01-18 16:21:08 +0000604 if (val != fDeferredDrawing) {
605 if (fDeferredDrawing) {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000606 // Going live.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000607 this->getDeferredDevice()->flushPendingCommands(kNormal_PlaybackMode);
junov@google.com4370aed2012-01-18 16:21:08 +0000608 }
609 fDeferredDrawing = val;
610 }
611}
612
junov@chromium.org88e29142012-08-07 16:48:22 +0000613bool SkDeferredCanvas::isDeferredDrawing() const {
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +0000614 return fDeferredDrawing;
615}
616
junov@chromium.org88e29142012-08-07 16:48:22 +0000617bool SkDeferredCanvas::isFreshFrame() const {
618 return this->getDeferredDevice()->isFreshFrame();
619}
620
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000621bool SkDeferredCanvas::hasPendingCommands() const {
622 return this->getDeferredDevice()->hasPendingCommands();
623}
624
junov@chromium.orgfb103892012-09-20 19:35:43 +0000625void SkDeferredCanvas::silentFlush() {
626 if (fDeferredDrawing) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000627 this->getDeferredDevice()->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000628 }
629}
630
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000631SkDeferredCanvas::~SkDeferredCanvas() {
junov@google.com4370aed2012-01-18 16:21:08 +0000632}
633
junov@chromium.org7070f762013-05-24 17:13:00 +0000634SkSurface* SkDeferredCanvas::setSurface(SkSurface* surface) {
reed@google.com9c135db2014-03-12 18:28:35 +0000635 SkDeferredDevice* deferredDevice = this->getDeferredDevice();
junov@chromium.org66070a52013-05-28 17:39:08 +0000636 SkASSERT(NULL != deferredDevice);
637 // By swapping the surface into the existing device, we preserve
638 // all pending commands, which can help to seamlessly recover from
639 // a lost accelerated graphics context.
640 deferredDevice->setSurface(surface);
junov@chromium.org7070f762013-05-24 17:13:00 +0000641 return surface;
642}
643
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000644SkDeferredCanvas::NotificationClient* SkDeferredCanvas::setNotificationClient(
645 NotificationClient* notificationClient) {
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000646
reed@google.com9c135db2014-03-12 18:28:35 +0000647 SkDeferredDevice* deferredDevice = this->getDeferredDevice();
junov@google.com4370aed2012-01-18 16:21:08 +0000648 SkASSERT(deferredDevice);
649 if (deferredDevice) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000650 deferredDevice->setNotificationClient(notificationClient);
junov@google.com4370aed2012-01-18 16:21:08 +0000651 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000652 return notificationClient;
junov@google.com4370aed2012-01-18 16:21:08 +0000653}
654
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000655SkImage* SkDeferredCanvas::newImageSnapshot() {
reed@google.com9c135db2014-03-12 18:28:35 +0000656 SkDeferredDevice* deferredDevice = this->getDeferredDevice();
junov@chromium.org67d74222013-04-12 13:33:01 +0000657 SkASSERT(deferredDevice);
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000658 return deferredDevice ? deferredDevice->newImageSnapshot() : NULL;
junov@chromium.org67d74222013-04-12 13:33:01 +0000659}
660
junov@google.com4370aed2012-01-18 16:21:08 +0000661bool SkDeferredCanvas::isFullFrame(const SkRect* rect,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000662 const SkPaint* paint) const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000663 SkCanvas* canvas = this->drawingCanvas();
664 SkISize canvasSize = this->getDeviceSize();
junov@google.com4370aed2012-01-18 16:21:08 +0000665 if (rect) {
666 if (!canvas->getTotalMatrix().rectStaysRect()) {
667 return false; // conservative
668 }
669
670 SkRect transformedRect;
671 canvas->getTotalMatrix().mapRect(&transformedRect, *rect);
672
673 if (paint) {
674 SkPaint::Style paintStyle = paint->getStyle();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000675 if (!(paintStyle == SkPaint::kFill_Style ||
junov@google.com4370aed2012-01-18 16:21:08 +0000676 paintStyle == SkPaint::kStrokeAndFill_Style)) {
677 return false;
678 }
679 if (paint->getMaskFilter() || paint->getLooper()
680 || paint->getPathEffect() || paint->getImageFilter()) {
681 return false; // conservative
682 }
683 }
684
685 // The following test holds with AA enabled, and is conservative
686 // by a 0.5 pixel margin with AA disabled
rmistry@google.comd6176b02012-08-23 18:14:13 +0000687 if (transformedRect.fLeft > SkIntToScalar(0) ||
688 transformedRect.fTop > SkIntToScalar(0) ||
junov@chromium.orgb1e218e2012-02-13 22:27:58 +0000689 transformedRect.fRight < SkIntToScalar(canvasSize.fWidth) ||
690 transformedRect.fBottom < SkIntToScalar(canvasSize.fHeight)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000691 return false;
692 }
693 }
694
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000695 return this->getClipStack()->quickContains(SkRect::MakeXYWH(0, 0,
696 SkIntToScalar(canvasSize.fWidth), SkIntToScalar(canvasSize.fHeight)));
junov@google.com4370aed2012-01-18 16:21:08 +0000697}
698
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000699void SkDeferredCanvas::willSave(SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000700 this->drawingCanvas()->save(flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000701 this->recordedDrawCommand();
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000702 this->INHERITED::willSave(flags);
junov@google.com4370aed2012-01-18 16:21:08 +0000703}
704
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000705SkCanvas::SaveLayerStrategy SkDeferredCanvas::willSaveLayer(const SkRect* bounds,
706 const SkPaint* paint, SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000707 this->drawingCanvas()->saveLayer(bounds, paint, flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000708 this->recordedDrawCommand();
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000709 this->INHERITED::willSaveLayer(bounds, paint, flags);
710 // No need for a full layer.
711 return kNoLayer_SaveLayerStrategy;
junov@google.com4370aed2012-01-18 16:21:08 +0000712}
713
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000714void SkDeferredCanvas::willRestore() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000715 this->drawingCanvas()->restore();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000716 this->recordedDrawCommand();
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000717 this->INHERITED::willRestore();
junov@google.com4370aed2012-01-18 16:21:08 +0000718}
719
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000720bool SkDeferredCanvas::isDrawingToLayer() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000721 return this->drawingCanvas()->isDrawingToLayer();
junov@google.com4370aed2012-01-18 16:21:08 +0000722}
723
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000724void SkDeferredCanvas::didConcat(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000725 this->drawingCanvas()->concat(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000726 this->recordedDrawCommand();
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000727 this->INHERITED::didConcat(matrix);
junov@google.com4370aed2012-01-18 16:21:08 +0000728}
729
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000730void SkDeferredCanvas::didSetMatrix(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000731 this->drawingCanvas()->setMatrix(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000732 this->recordedDrawCommand();
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000733 this->INHERITED::didSetMatrix(matrix);
junov@google.com4370aed2012-01-18 16:21:08 +0000734}
735
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000736void SkDeferredCanvas::onClipRect(const SkRect& rect,
737 SkRegion::Op op,
738 ClipEdgeStyle edgeStyle) {
739 this->drawingCanvas()->clipRect(rect, op, kSoft_ClipEdgeStyle == edgeStyle);
740 this->INHERITED::onClipRect(rect, op, edgeStyle);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000741 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000742}
743
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000744void SkDeferredCanvas::onClipRRect(const SkRRect& rrect,
745 SkRegion::Op op,
746 ClipEdgeStyle edgeStyle) {
747 this->drawingCanvas()->clipRRect(rrect, op, kSoft_ClipEdgeStyle == edgeStyle);
748 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000749 this->recordedDrawCommand();
reed@google.com4ed0fb72012-12-12 20:48:18 +0000750}
751
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000752void SkDeferredCanvas::onClipPath(const SkPath& path,
753 SkRegion::Op op,
754 ClipEdgeStyle edgeStyle) {
755 this->drawingCanvas()->clipPath(path, op, kSoft_ClipEdgeStyle == edgeStyle);
756 this->INHERITED::onClipPath(path, op, edgeStyle);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000757 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000758}
759
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000760void SkDeferredCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000761 this->drawingCanvas()->clipRegion(deviceRgn, op);
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000762 this->INHERITED::onClipRegion(deviceRgn, op);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000763 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000764}
765
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000766void SkDeferredCanvas::clear(SkColor color) {
junov@google.com4370aed2012-01-18 16:21:08 +0000767 // purge pending commands
768 if (fDeferredDrawing) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000769 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000770 }
771
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000772 this->drawingCanvas()->clear(color);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000773 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000774}
775
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000776void SkDeferredCanvas::drawPaint(const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000777 if (fDeferredDrawing && this->isFullFrame(NULL, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000778 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000779 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000780 }
junov@chromium.org10f7f972012-07-31 21:01:51 +0000781 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000782 this->drawingCanvas()->drawPaint(paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000783 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000784}
785
786void SkDeferredCanvas::drawPoints(PointMode mode, size_t count,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000787 const SkPoint pts[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000788 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000789 this->drawingCanvas()->drawPoints(mode, count, pts, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000790 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000791}
792
reed@google.com4ed0fb72012-12-12 20:48:18 +0000793void SkDeferredCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
794 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
795 this->drawingCanvas()->drawOval(rect, paint);
796 this->recordedDrawCommand();
797}
798
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000799void SkDeferredCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000800 if (fDeferredDrawing && this->isFullFrame(&rect, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000801 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000802 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000803 }
skia.committer@gmail.com306ab9d2012-12-13 02:01:33 +0000804
junov@chromium.org10f7f972012-07-31 21:01:51 +0000805 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000806 this->drawingCanvas()->drawRect(rect, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000807 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000808}
809
reed@google.com4ed0fb72012-12-12 20:48:18 +0000810void SkDeferredCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
811 if (rrect.isRect()) {
812 this->SkDeferredCanvas::drawRect(rrect.getBounds(), paint);
813 } else if (rrect.isOval()) {
814 this->SkDeferredCanvas::drawOval(rrect.getBounds(), paint);
815 } else {
816 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
817 this->drawingCanvas()->drawRRect(rrect, paint);
818 this->recordedDrawCommand();
819 }
820}
821
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000822void SkDeferredCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
823 const SkPaint& paint) {
824 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
825 this->drawingCanvas()->drawDRRect(outer, inner, paint);
826 this->recordedDrawCommand();
827}
828
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000829void SkDeferredCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000830 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000831 this->drawingCanvas()->drawPath(path, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000832 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000833}
834
835void SkDeferredCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000836 SkScalar top, const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000837 SkRect bitmapRect = SkRect::MakeXYWH(left, top,
838 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000839 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000840 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000841 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000842 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000843 }
844
junov@chromium.org10f7f972012-07-31 21:01:51 +0000845 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000846 this->drawingCanvas()->drawBitmap(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000847 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000848}
849
reed@google.com71121732012-09-18 15:14:33 +0000850void SkDeferredCanvas::drawBitmapRectToRect(const SkBitmap& bitmap,
851 const SkRect* src,
852 const SkRect& dst,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000853 const SkPaint* paint,
854 DrawBitmapRectFlags flags) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000855 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000856 this->isFullFrame(&dst, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000857 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000858 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000859 }
860
junov@chromium.org10f7f972012-07-31 21:01:51 +0000861 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000862 this->drawingCanvas()->drawBitmapRectToRect(bitmap, src, dst, paint, flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000863 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000864}
865
866
867void SkDeferredCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
868 const SkMatrix& m,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000869 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000870 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
871 // covers canvas entirely and transformed bitmap covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000872 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000873 this->drawingCanvas()->drawBitmapMatrix(bitmap, m, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000874 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000875}
876
877void SkDeferredCanvas::drawBitmapNine(const SkBitmap& bitmap,
878 const SkIRect& center, const SkRect& dst,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000879 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000880 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
881 // covers canvas entirely and dst covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000882 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000883 this->drawingCanvas()->drawBitmapNine(bitmap, center, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000884 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000885}
886
887void SkDeferredCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000888 const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000889 SkRect bitmapRect = SkRect::MakeXYWH(
890 SkIntToScalar(left),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000891 SkIntToScalar(top),
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000892 SkIntToScalar(bitmap.width()),
893 SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000894 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000895 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000896 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000897 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000898 }
899
junov@chromium.org10f7f972012-07-31 21:01:51 +0000900 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000901 this->drawingCanvas()->drawSprite(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000902 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000903}
904
905void SkDeferredCanvas::drawText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000906 SkScalar x, SkScalar y, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000907 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000908 this->drawingCanvas()->drawText(text, byteLength, x, y, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000909 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000910}
911
912void SkDeferredCanvas::drawPosText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000913 const SkPoint pos[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000914 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000915 this->drawingCanvas()->drawPosText(text, byteLength, pos, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000916 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000917}
918
919void SkDeferredCanvas::drawPosTextH(const void* text, size_t byteLength,
920 const SkScalar xpos[], SkScalar constY,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000921 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000922 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000923 this->drawingCanvas()->drawPosTextH(text, byteLength, xpos, constY, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000924 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000925}
926
927void SkDeferredCanvas::drawTextOnPath(const void* text, size_t byteLength,
928 const SkPath& path,
929 const SkMatrix* matrix,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000930 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000931 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000932 this->drawingCanvas()->drawTextOnPath(text, byteLength, path, matrix, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000933 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000934}
935
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000936void SkDeferredCanvas::drawPicture(SkPicture& picture) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000937 this->drawingCanvas()->drawPicture(picture);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000938 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000939}
940
941void SkDeferredCanvas::drawVertices(VertexMode vmode, int vertexCount,
942 const SkPoint vertices[],
943 const SkPoint texs[],
944 const SkColor colors[], SkXfermode* xmode,
945 const uint16_t indices[], int indexCount,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000946 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000947 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000948 this->drawingCanvas()->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode,
949 indices, indexCount, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000950 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000951}
952
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000953SkBounder* SkDeferredCanvas::setBounder(SkBounder* bounder) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000954 this->drawingCanvas()->setBounder(bounder);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000955 this->INHERITED::setBounder(bounder);
956 this->recordedDrawCommand();
957 return bounder;
junov@google.com4370aed2012-01-18 16:21:08 +0000958}
959
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000960SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000961 this->drawingCanvas()->setDrawFilter(filter);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000962 this->INHERITED::setDrawFilter(filter);
963 this->recordedDrawCommand();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000964 return filter;
junov@google.com4370aed2012-01-18 16:21:08 +0000965}
966
967SkCanvas* SkDeferredCanvas::canvasForDrawIter() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000968 return this->drawingCanvas();
junov@google.com4370aed2012-01-18 16:21:08 +0000969}