blob: 9f444e11f3c17bb58e0b5ca298240e4fe4ff341e [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);
junov@google.com52a00ca2012-10-01 15:27:14 +0000327 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000328 }
329}
330
reed@google.com9c135db2014-03-12 18:28:35 +0000331bool SkDeferredDevice::isFreshFrame() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000332 bool ret = fFreshFrame;
333 fFreshFrame = false;
334 return ret;
335}
336
reed@google.com9c135db2014-03-12 18:28:35 +0000337bool SkDeferredDevice::hasPendingCommands() {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000338 return fPipeController.hasPendingCommands();
339}
340
reed@google.com9c135db2014-03-12 18:28:35 +0000341void SkDeferredDevice::aboutToDraw()
junov@chromium.org44324fa2013-08-02 15:36:02 +0000342{
343 if (NULL != fNotificationClient) {
344 fNotificationClient->prepareForDraw();
345 }
346 if (fCanDiscardCanvasContents) {
347 if (NULL != fSurface) {
348 fSurface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
349 }
skia.committer@gmail.comea4b7972013-08-06 07:01:27 +0000350 fCanDiscardCanvasContents = false;
junov@chromium.org44324fa2013-08-02 15:36:02 +0000351 }
352}
353
reed@google.com9c135db2014-03-12 18:28:35 +0000354void SkDeferredDevice::flushPendingCommands(PlaybackMode playbackMode) {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000355 if (!fPipeController.hasPendingCommands()) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000356 return;
357 }
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000358 if (playbackMode == kNormal_PlaybackMode) {
junov@chromium.org44324fa2013-08-02 15:36:02 +0000359 aboutToDraw();
junov@chromium.org88e29142012-08-07 16:48:22 +0000360 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000361 fPipeWriter.flushRecording(true);
junov@chromium.orgd4501a02012-10-30 19:05:17 +0000362 fPipeController.playback(kSilent_PlaybackMode == playbackMode);
commit-bot@chromium.orgdad009b2014-03-27 15:48:52 +0000363 if (fNotificationClient) {
364 if (playbackMode == kSilent_PlaybackMode) {
365 fNotificationClient->skippedPendingDrawCommands();
366 } else {
367 fNotificationClient->flushedDrawCommands();
368 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000369 }
commit-bot@chromium.orgdad009b2014-03-27 15:48:52 +0000370
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000371 fPreviousStorageAllocated = storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000372}
373
reed@google.com9c135db2014-03-12 18:28:35 +0000374void SkDeferredDevice::flush() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000375 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000376 fImmediateCanvas->flush();
377}
378
reed@google.com9c135db2014-03-12 18:28:35 +0000379size_t SkDeferredDevice::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000380 size_t val = fPipeWriter.freeMemoryIfPossible(bytesToFree);
381 fPreviousStorageAllocated = storageAllocatedForRecording();
382 return val;
junov@chromium.org88e29142012-08-07 16:48:22 +0000383}
384
reed@google.com9c135db2014-03-12 18:28:35 +0000385size_t SkDeferredDevice::getBitmapSizeThreshold() const {
sugoi@google.com7775fd52012-11-21 15:47:04 +0000386 return fBitmapSizeThreshold;
387}
388
reed@google.com9c135db2014-03-12 18:28:35 +0000389void SkDeferredDevice::setBitmapSizeThreshold(size_t sizeThreshold) {
sugoi@google.com7775fd52012-11-21 15:47:04 +0000390 fBitmapSizeThreshold = sizeThreshold;
391}
392
reed@google.com9c135db2014-03-12 18:28:35 +0000393size_t SkDeferredDevice::storageAllocatedForRecording() const {
junov@chromium.org88e29142012-08-07 16:48:22 +0000394 return (fPipeController.storageAllocatedForRecording()
395 + fPipeWriter.storageAllocatedForRecording());
396}
397
reed@google.com9c135db2014-03-12 18:28:35 +0000398void SkDeferredDevice::recordedDrawCommand() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000399 size_t storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000400
junov@chromium.org88e29142012-08-07 16:48:22 +0000401 if (storageAllocated > fMaxRecordingStorageBytes) {
402 // First, attempt to reduce cache without flushing
403 size_t tryFree = storageAllocated - fMaxRecordingStorageBytes;
404 if (this->freeMemoryIfPossible(tryFree) < tryFree) {
405 // Flush is necessary to free more space.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000406 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000407 // Free as much as possible to avoid oscillating around fMaxRecordingStorageBytes
408 // which could cause a high flushing frequency.
bsalomon@google.com100abf42012-09-05 17:40:04 +0000409 this->freeMemoryIfPossible(~0U);
junov@chromium.org88e29142012-08-07 16:48:22 +0000410 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000411 storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000412 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000413
rmistry@google.comd6176b02012-08-23 18:14:13 +0000414 if (fNotificationClient &&
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000415 storageAllocated != fPreviousStorageAllocated) {
416 fPreviousStorageAllocated = storageAllocated;
417 fNotificationClient->storageAllocatedForRecordingChanged(storageAllocated);
418 }
419}
420
reed@google.com9c135db2014-03-12 18:28:35 +0000421SkCanvas* SkDeferredDevice::recordingCanvas() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000422 return fRecordingCanvas;
423}
424
reed@google.com9c135db2014-03-12 18:28:35 +0000425SkImage* SkDeferredDevice::newImageSnapshot() {
junov@chromium.org67d74222013-04-12 13:33:01 +0000426 this->flush();
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000427 return fSurface ? fSurface->newImageSnapshot() : NULL;
junov@chromium.org67d74222013-04-12 13:33:01 +0000428}
429
reed@google.com9c135db2014-03-12 18:28:35 +0000430int SkDeferredDevice::width() const {
junov@chromium.org9becf002013-04-15 18:15:23 +0000431 return immediateDevice()->width();
junov@chromium.org88e29142012-08-07 16:48:22 +0000432}
433
reed@google.com9c135db2014-03-12 18:28:35 +0000434int SkDeferredDevice::height() const {
junov@chromium.org9becf002013-04-15 18:15:23 +0000435 return immediateDevice()->height();
junov@chromium.org88e29142012-08-07 16:48:22 +0000436}
437
reed@google.com9c135db2014-03-12 18:28:35 +0000438SkBitmap::Config SkDeferredDevice::config() const {
reed@google.com284a84d2014-02-14 15:23:15 +0000439 return immediateDevice()->config();
440}
441
reed@google.com9c135db2014-03-12 18:28:35 +0000442bool SkDeferredDevice::isOpaque() const {
reed@google.com284a84d2014-02-14 15:23:15 +0000443 return immediateDevice()->isOpaque();
444}
445
reed@google.com9c135db2014-03-12 18:28:35 +0000446SkImageInfo SkDeferredDevice::imageInfo() const {
reed@google.com284a84d2014-02-14 15:23:15 +0000447 return immediateDevice()->imageInfo();
448}
449
reed@google.com9c135db2014-03-12 18:28:35 +0000450GrRenderTarget* SkDeferredDevice::accessRenderTarget() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000451 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000452 return immediateDevice()->accessRenderTarget();
junov@chromium.org88e29142012-08-07 16:48:22 +0000453}
454
reed@google.com9c135db2014-03-12 18:28:35 +0000455void SkDeferredDevice::prepareForImmediatePixelWrite() {
junov@chromium.org44324fa2013-08-02 15:36:02 +0000456 // The purpose of the following code is to make sure commands are flushed, that
457 // aboutToDraw() is called and that notifyContentWillChange is called, without
458 // calling anything redundantly.
459 if (fPipeController.hasPendingCommands()) {
460 this->flushPendingCommands(kNormal_PlaybackMode);
461 } else {
462 bool mustNotifyDirectly = !fCanDiscardCanvasContents;
463 this->aboutToDraw();
464 if (mustNotifyDirectly) {
465 fSurface->notifyContentWillChange(SkSurface::kRetain_ContentChangeMode);
466 }
467 }
468
469 fImmediateCanvas->flush();
470}
471
reed@google.com9c135db2014-03-12 18:28:35 +0000472bool SkDeferredDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000473 int x, int y) {
474 SkASSERT(x >= 0 && y >= 0);
475 SkASSERT(x + info.width() <= width());
476 SkASSERT(y + info.height() <= height());
477
478 this->flushPendingCommands(kNormal_PlaybackMode);
479
480 const SkImageInfo deviceInfo = this->imageInfo();
481 if (info.width() == deviceInfo.width() && info.height() == deviceInfo.height()) {
482 this->skipPendingCommands();
483 }
skia.committer@gmail.come62513f2014-03-08 03:02:06 +0000484
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000485 this->prepareForImmediatePixelWrite();
486 return immediateDevice()->onWritePixels(info, pixels, rowBytes, x, y);
487}
junov@chromium.org88e29142012-08-07 16:48:22 +0000488
reed@google.com9c135db2014-03-12 18:28:35 +0000489const SkBitmap& SkDeferredDevice::onAccessBitmap() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000490 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000491 return immediateDevice()->accessBitmap(false);
junov@chromium.org88e29142012-08-07 16:48:22 +0000492}
493
reed@google.com9c135db2014-03-12 18:28:35 +0000494SkBaseDevice* SkDeferredDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000495 // Save layer usage not supported, and not required by SkDeferredCanvas.
496 SkASSERT(usage != kSaveLayer_Usage);
497 // Create a compatible non-deferred device.
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000498 // We do not create a deferred device because we know the new device
499 // will not be used with a deferred canvas (there is no API for that).
reed@google.com9c135db2014-03-12 18:28:35 +0000500 // And connecting a SkDeferredDevice to non-deferred canvas can result
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000501 // in unpredictable behavior.
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000502 return immediateDevice()->createCompatibleDevice(info);
junov@chromium.org88e29142012-08-07 16:48:22 +0000503}
504
reed@google.com9c135db2014-03-12 18:28:35 +0000505SkSurface* SkDeferredDevice::newSurface(const SkImageInfo& info) {
reed@google.com76f10a32014-02-05 15:32:21 +0000506 return this->immediateDevice()->newSurface(info);
507}
508
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000509bool SkDeferredDevice::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
510 int x, int y) {
511 this->flushPendingCommands(kNormal_PlaybackMode);
512 return fImmediateCanvas->readPixels(info, pixels, rowBytes, x, y);
513}
junov@chromium.org88e29142012-08-07 16:48:22 +0000514
sugoi@google.com7775fd52012-11-21 15:47:04 +0000515class AutoImmediateDrawIfNeeded {
516public:
517 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkBitmap* bitmap,
518 const SkPaint* paint) {
519 this->init(canvas, bitmap, paint);
520 }
521
522 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkPaint* paint) {
523 this->init(canvas, NULL, paint);
524 }
525
526 ~AutoImmediateDrawIfNeeded() {
527 if (fCanvas) {
528 fCanvas->setDeferredDrawing(true);
529 }
530 }
531private:
532 void init(SkDeferredCanvas& canvas, const SkBitmap* bitmap, const SkPaint* paint)
533 {
reed@google.com9c135db2014-03-12 18:28:35 +0000534 SkDeferredDevice* device = static_cast<SkDeferredDevice*>(canvas.getDevice());
sugoi@google.com7775fd52012-11-21 15:47:04 +0000535 if (canvas.isDeferredDrawing() && (NULL != device) &&
536 shouldDrawImmediately(bitmap, paint, device->getBitmapSizeThreshold())) {
537 canvas.setDeferredDrawing(false);
538 fCanvas = &canvas;
539 } else {
540 fCanvas = NULL;
541 }
542 }
543
544 SkDeferredCanvas* fCanvas;
545};
junov@chromium.org88e29142012-08-07 16:48:22 +0000546
junov@chromium.org66070a52013-05-28 17:39:08 +0000547SkDeferredCanvas* SkDeferredCanvas::Create(SkSurface* surface) {
reed@google.com9c135db2014-03-12 18:28:35 +0000548 SkAutoTUnref<SkDeferredDevice> deferredDevice(SkNEW_ARGS(SkDeferredDevice, (surface)));
junov@chromium.org66070a52013-05-28 17:39:08 +0000549 return SkNEW_ARGS(SkDeferredCanvas, (deferredDevice));
550}
551
reed@google.com9c135db2014-03-12 18:28:35 +0000552SkDeferredCanvas::SkDeferredCanvas(SkDeferredDevice* device) : SkCanvas (device) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000553 this->init();
554}
junov@chromium.org67d74222013-04-12 13:33:01 +0000555
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000556void SkDeferredCanvas::init() {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000557 fDeferredDrawing = true; // On by default
junov@google.com4370aed2012-01-18 16:21:08 +0000558}
559
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000560void SkDeferredCanvas::setMaxRecordingStorage(size_t maxStorage) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000561 this->validate();
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000562 this->getDeferredDevice()->setMaxRecordingStorage(maxStorage);
563}
564
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000565size_t SkDeferredCanvas::storageAllocatedForRecording() const {
566 return this->getDeferredDevice()->storageAllocatedForRecording();
567}
568
569size_t SkDeferredCanvas::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000570 return this->getDeferredDevice()->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000571}
572
sugoi@google.com7775fd52012-11-21 15:47:04 +0000573void SkDeferredCanvas::setBitmapSizeThreshold(size_t sizeThreshold) {
reed@google.com9c135db2014-03-12 18:28:35 +0000574 SkDeferredDevice* deferredDevice = this->getDeferredDevice();
sugoi@google.com7775fd52012-11-21 15:47:04 +0000575 SkASSERT(deferredDevice);
576 deferredDevice->setBitmapSizeThreshold(sizeThreshold);
577}
578
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000579void SkDeferredCanvas::recordedDrawCommand() {
580 if (fDeferredDrawing) {
581 this->getDeferredDevice()->recordedDrawCommand();
582 }
583}
584
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000585void SkDeferredCanvas::validate() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000586 SkASSERT(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000587}
588
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000589SkCanvas* SkDeferredCanvas::drawingCanvas() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000590 this->validate();
591 return fDeferredDrawing ? this->getDeferredDevice()->recordingCanvas() :
592 this->getDeferredDevice()->immediateCanvas();
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000593}
594
junov@chromium.org88e29142012-08-07 16:48:22 +0000595SkCanvas* SkDeferredCanvas::immediateCanvas() const {
596 this->validate();
597 return this->getDeferredDevice()->immediateCanvas();
598}
599
reed@google.com9c135db2014-03-12 18:28:35 +0000600SkDeferredDevice* SkDeferredCanvas::getDeferredDevice() const {
601 return static_cast<SkDeferredDevice*>(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000602}
603
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000604void SkDeferredCanvas::setDeferredDrawing(bool val) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000605 this->validate(); // Must set device before calling this method
junov@google.com4370aed2012-01-18 16:21:08 +0000606 if (val != fDeferredDrawing) {
607 if (fDeferredDrawing) {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000608 // Going live.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000609 this->getDeferredDevice()->flushPendingCommands(kNormal_PlaybackMode);
junov@google.com4370aed2012-01-18 16:21:08 +0000610 }
611 fDeferredDrawing = val;
612 }
613}
614
junov@chromium.org88e29142012-08-07 16:48:22 +0000615bool SkDeferredCanvas::isDeferredDrawing() const {
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +0000616 return fDeferredDrawing;
617}
618
junov@chromium.org88e29142012-08-07 16:48:22 +0000619bool SkDeferredCanvas::isFreshFrame() const {
620 return this->getDeferredDevice()->isFreshFrame();
621}
622
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000623bool SkDeferredCanvas::hasPendingCommands() const {
624 return this->getDeferredDevice()->hasPendingCommands();
625}
626
junov@chromium.orgfb103892012-09-20 19:35:43 +0000627void SkDeferredCanvas::silentFlush() {
628 if (fDeferredDrawing) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000629 this->getDeferredDevice()->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000630 }
631}
632
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000633SkDeferredCanvas::~SkDeferredCanvas() {
junov@google.com4370aed2012-01-18 16:21:08 +0000634}
635
junov@chromium.org7070f762013-05-24 17:13:00 +0000636SkSurface* SkDeferredCanvas::setSurface(SkSurface* surface) {
reed@google.com9c135db2014-03-12 18:28:35 +0000637 SkDeferredDevice* deferredDevice = this->getDeferredDevice();
junov@chromium.org66070a52013-05-28 17:39:08 +0000638 SkASSERT(NULL != deferredDevice);
639 // By swapping the surface into the existing device, we preserve
640 // all pending commands, which can help to seamlessly recover from
641 // a lost accelerated graphics context.
642 deferredDevice->setSurface(surface);
junov@chromium.org7070f762013-05-24 17:13:00 +0000643 return surface;
644}
645
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000646SkDeferredCanvas::NotificationClient* SkDeferredCanvas::setNotificationClient(
647 NotificationClient* notificationClient) {
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000648
reed@google.com9c135db2014-03-12 18:28:35 +0000649 SkDeferredDevice* deferredDevice = this->getDeferredDevice();
junov@google.com4370aed2012-01-18 16:21:08 +0000650 SkASSERT(deferredDevice);
651 if (deferredDevice) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000652 deferredDevice->setNotificationClient(notificationClient);
junov@google.com4370aed2012-01-18 16:21:08 +0000653 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000654 return notificationClient;
junov@google.com4370aed2012-01-18 16:21:08 +0000655}
656
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000657SkImage* SkDeferredCanvas::newImageSnapshot() {
reed@google.com9c135db2014-03-12 18:28:35 +0000658 SkDeferredDevice* deferredDevice = this->getDeferredDevice();
junov@chromium.org67d74222013-04-12 13:33:01 +0000659 SkASSERT(deferredDevice);
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000660 return deferredDevice ? deferredDevice->newImageSnapshot() : NULL;
junov@chromium.org67d74222013-04-12 13:33:01 +0000661}
662
junov@google.com4370aed2012-01-18 16:21:08 +0000663bool SkDeferredCanvas::isFullFrame(const SkRect* rect,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000664 const SkPaint* paint) const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000665 SkCanvas* canvas = this->drawingCanvas();
666 SkISize canvasSize = this->getDeviceSize();
junov@google.com4370aed2012-01-18 16:21:08 +0000667 if (rect) {
668 if (!canvas->getTotalMatrix().rectStaysRect()) {
669 return false; // conservative
670 }
671
672 SkRect transformedRect;
673 canvas->getTotalMatrix().mapRect(&transformedRect, *rect);
674
675 if (paint) {
676 SkPaint::Style paintStyle = paint->getStyle();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000677 if (!(paintStyle == SkPaint::kFill_Style ||
junov@google.com4370aed2012-01-18 16:21:08 +0000678 paintStyle == SkPaint::kStrokeAndFill_Style)) {
679 return false;
680 }
681 if (paint->getMaskFilter() || paint->getLooper()
682 || paint->getPathEffect() || paint->getImageFilter()) {
683 return false; // conservative
684 }
685 }
686
687 // The following test holds with AA enabled, and is conservative
688 // by a 0.5 pixel margin with AA disabled
rmistry@google.comd6176b02012-08-23 18:14:13 +0000689 if (transformedRect.fLeft > SkIntToScalar(0) ||
690 transformedRect.fTop > SkIntToScalar(0) ||
junov@chromium.orgb1e218e2012-02-13 22:27:58 +0000691 transformedRect.fRight < SkIntToScalar(canvasSize.fWidth) ||
692 transformedRect.fBottom < SkIntToScalar(canvasSize.fHeight)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000693 return false;
694 }
695 }
696
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000697 return this->getClipStack()->quickContains(SkRect::MakeXYWH(0, 0,
698 SkIntToScalar(canvasSize.fWidth), SkIntToScalar(canvasSize.fHeight)));
junov@google.com4370aed2012-01-18 16:21:08 +0000699}
700
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000701void SkDeferredCanvas::willSave(SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000702 this->drawingCanvas()->save(flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000703 this->recordedDrawCommand();
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000704 this->INHERITED::willSave(flags);
junov@google.com4370aed2012-01-18 16:21:08 +0000705}
706
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000707SkCanvas::SaveLayerStrategy SkDeferredCanvas::willSaveLayer(const SkRect* bounds,
708 const SkPaint* paint, SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000709 this->drawingCanvas()->saveLayer(bounds, paint, flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000710 this->recordedDrawCommand();
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000711 this->INHERITED::willSaveLayer(bounds, paint, flags);
712 // No need for a full layer.
713 return kNoLayer_SaveLayerStrategy;
junov@google.com4370aed2012-01-18 16:21:08 +0000714}
715
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000716void SkDeferredCanvas::willRestore() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000717 this->drawingCanvas()->restore();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000718 this->recordedDrawCommand();
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000719 this->INHERITED::willRestore();
junov@google.com4370aed2012-01-18 16:21:08 +0000720}
721
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000722bool SkDeferredCanvas::isDrawingToLayer() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000723 return this->drawingCanvas()->isDrawingToLayer();
junov@google.com4370aed2012-01-18 16:21:08 +0000724}
725
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000726void SkDeferredCanvas::didConcat(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000727 this->drawingCanvas()->concat(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000728 this->recordedDrawCommand();
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000729 this->INHERITED::didConcat(matrix);
junov@google.com4370aed2012-01-18 16:21:08 +0000730}
731
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000732void SkDeferredCanvas::didSetMatrix(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000733 this->drawingCanvas()->setMatrix(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000734 this->recordedDrawCommand();
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000735 this->INHERITED::didSetMatrix(matrix);
junov@google.com4370aed2012-01-18 16:21:08 +0000736}
737
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000738void SkDeferredCanvas::onClipRect(const SkRect& rect,
739 SkRegion::Op op,
740 ClipEdgeStyle edgeStyle) {
741 this->drawingCanvas()->clipRect(rect, op, kSoft_ClipEdgeStyle == edgeStyle);
742 this->INHERITED::onClipRect(rect, op, edgeStyle);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000743 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000744}
745
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000746void SkDeferredCanvas::onClipRRect(const SkRRect& rrect,
747 SkRegion::Op op,
748 ClipEdgeStyle edgeStyle) {
749 this->drawingCanvas()->clipRRect(rrect, op, kSoft_ClipEdgeStyle == edgeStyle);
750 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000751 this->recordedDrawCommand();
reed@google.com4ed0fb72012-12-12 20:48:18 +0000752}
753
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000754void SkDeferredCanvas::onClipPath(const SkPath& path,
755 SkRegion::Op op,
756 ClipEdgeStyle edgeStyle) {
757 this->drawingCanvas()->clipPath(path, op, kSoft_ClipEdgeStyle == edgeStyle);
758 this->INHERITED::onClipPath(path, op, edgeStyle);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000759 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000760}
761
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000762void SkDeferredCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000763 this->drawingCanvas()->clipRegion(deviceRgn, op);
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000764 this->INHERITED::onClipRegion(deviceRgn, op);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000765 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000766}
767
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000768void SkDeferredCanvas::clear(SkColor color) {
junov@google.com4370aed2012-01-18 16:21:08 +0000769 // purge pending commands
770 if (fDeferredDrawing) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000771 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000772 }
773
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000774 this->drawingCanvas()->clear(color);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000775 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000776}
777
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000778void SkDeferredCanvas::drawPaint(const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000779 if (fDeferredDrawing && this->isFullFrame(NULL, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000780 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000781 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000782 }
junov@chromium.org10f7f972012-07-31 21:01:51 +0000783 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000784 this->drawingCanvas()->drawPaint(paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000785 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000786}
787
788void SkDeferredCanvas::drawPoints(PointMode mode, size_t count,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000789 const SkPoint pts[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000790 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000791 this->drawingCanvas()->drawPoints(mode, count, pts, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000792 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000793}
794
reed@google.com4ed0fb72012-12-12 20:48:18 +0000795void SkDeferredCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
796 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
797 this->drawingCanvas()->drawOval(rect, paint);
798 this->recordedDrawCommand();
799}
800
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000801void SkDeferredCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000802 if (fDeferredDrawing && this->isFullFrame(&rect, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000803 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000804 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000805 }
skia.committer@gmail.com306ab9d2012-12-13 02:01:33 +0000806
junov@chromium.org10f7f972012-07-31 21:01:51 +0000807 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000808 this->drawingCanvas()->drawRect(rect, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000809 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000810}
811
reed@google.com4ed0fb72012-12-12 20:48:18 +0000812void SkDeferredCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
813 if (rrect.isRect()) {
814 this->SkDeferredCanvas::drawRect(rrect.getBounds(), paint);
815 } else if (rrect.isOval()) {
816 this->SkDeferredCanvas::drawOval(rrect.getBounds(), paint);
817 } else {
818 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
819 this->drawingCanvas()->drawRRect(rrect, paint);
820 this->recordedDrawCommand();
821 }
822}
823
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000824void SkDeferredCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
825 const SkPaint& paint) {
826 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
827 this->drawingCanvas()->drawDRRect(outer, inner, paint);
828 this->recordedDrawCommand();
829}
830
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000831void SkDeferredCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000832 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000833 this->drawingCanvas()->drawPath(path, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000834 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000835}
836
837void SkDeferredCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000838 SkScalar top, const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000839 SkRect bitmapRect = SkRect::MakeXYWH(left, top,
840 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000841 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000842 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000843 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000844 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000845 }
846
junov@chromium.org10f7f972012-07-31 21:01:51 +0000847 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000848 this->drawingCanvas()->drawBitmap(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000849 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000850}
851
reed@google.com71121732012-09-18 15:14:33 +0000852void SkDeferredCanvas::drawBitmapRectToRect(const SkBitmap& bitmap,
853 const SkRect* src,
854 const SkRect& dst,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000855 const SkPaint* paint,
856 DrawBitmapRectFlags flags) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000857 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000858 this->isFullFrame(&dst, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000859 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000860 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000861 }
862
junov@chromium.org10f7f972012-07-31 21:01:51 +0000863 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000864 this->drawingCanvas()->drawBitmapRectToRect(bitmap, src, dst, paint, flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000865 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000866}
867
868
869void SkDeferredCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
870 const SkMatrix& m,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000871 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000872 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
873 // covers canvas entirely and transformed bitmap covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000874 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000875 this->drawingCanvas()->drawBitmapMatrix(bitmap, m, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000876 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000877}
878
879void SkDeferredCanvas::drawBitmapNine(const SkBitmap& bitmap,
880 const SkIRect& center, const SkRect& dst,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000881 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000882 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
883 // covers canvas entirely and dst covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000884 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000885 this->drawingCanvas()->drawBitmapNine(bitmap, center, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000886 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000887}
888
889void SkDeferredCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000890 const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000891 SkRect bitmapRect = SkRect::MakeXYWH(
892 SkIntToScalar(left),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000893 SkIntToScalar(top),
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000894 SkIntToScalar(bitmap.width()),
895 SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000896 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000897 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000898 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000899 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000900 }
901
junov@chromium.org10f7f972012-07-31 21:01:51 +0000902 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000903 this->drawingCanvas()->drawSprite(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000904 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000905}
906
reed@google.come0d9ce82014-04-23 04:00:17 +0000907void SkDeferredCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
908 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000909 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000910 this->drawingCanvas()->drawText(text, byteLength, x, y, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000911 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000912}
913
reed@google.come0d9ce82014-04-23 04:00:17 +0000914void SkDeferredCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
915 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000916 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000917 this->drawingCanvas()->drawPosText(text, byteLength, pos, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000918 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000919}
920
reed@google.come0d9ce82014-04-23 04:00:17 +0000921void SkDeferredCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
922 SkScalar constY, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000923 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000924 this->drawingCanvas()->drawPosTextH(text, byteLength, xpos, constY, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000925 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000926}
927
reed@google.come0d9ce82014-04-23 04:00:17 +0000928void SkDeferredCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
929 const SkMatrix* matrix, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000930 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000931 this->drawingCanvas()->drawTextOnPath(text, byteLength, path, matrix, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000932 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000933}
934
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000935void SkDeferredCanvas::drawPicture(SkPicture& picture) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000936 this->drawingCanvas()->drawPicture(picture);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000937 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000938}
939
940void SkDeferredCanvas::drawVertices(VertexMode vmode, int vertexCount,
941 const SkPoint vertices[],
942 const SkPoint texs[],
943 const SkColor colors[], SkXfermode* xmode,
944 const uint16_t indices[], int indexCount,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000945 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000946 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000947 this->drawingCanvas()->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode,
948 indices, indexCount, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000949 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000950}
951
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000952SkBounder* SkDeferredCanvas::setBounder(SkBounder* bounder) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000953 this->drawingCanvas()->setBounder(bounder);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000954 this->INHERITED::setBounder(bounder);
955 this->recordedDrawCommand();
956 return bounder;
junov@google.com4370aed2012-01-18 16:21:08 +0000957}
958
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000959SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000960 this->drawingCanvas()->setDrawFilter(filter);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000961 this->INHERITED::setDrawFilter(filter);
962 this->recordedDrawCommand();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000963 return filter;
junov@google.com4370aed2012-01-18 16:21:08 +0000964}
965
966SkCanvas* SkDeferredCanvas::canvasForDrawIter() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000967 return this->drawingCanvas();
junov@google.com4370aed2012-01-18 16:21:08 +0000968}