blob: 641974b1ed1e3d1b3e7cb97184a68b169bc9b9f8 [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
reed@google.com284a84d2014-02-14 15:23:15 +0000160 virtual SkImageInfo imageInfo() const SK_OVERRIDE;
161
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000162 virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE;
junov@chromium.org88e29142012-08-07 16:48:22 +0000163
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000164 virtual SkBaseDevice* onCreateDevice(const SkImageInfo&, Usage) SK_OVERRIDE;
junov@chromium.org88e29142012-08-07 16:48:22 +0000165
reed@google.com76f10a32014-02-05 15:32:21 +0000166 virtual SkSurface* newSurface(const SkImageInfo&) SK_OVERRIDE;
167
junov@chromium.org88e29142012-08-07 16:48:22 +0000168protected:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000169 virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE;
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000170 virtual bool onReadPixels(const SkImageInfo&, void*, size_t, int x, int y) SK_OVERRIDE;
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000171 virtual bool onWritePixels(const SkImageInfo&, const void*, size_t, int x, int y) SK_OVERRIDE;
junov@chromium.org88e29142012-08-07 16:48:22 +0000172
173 // The following methods are no-ops on a deferred device
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000174 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE {
175 return false;
176 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000177
178 // None of the following drawing methods should ever get called on the
179 // deferred device
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000180 virtual void clear(SkColor color) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000181 {SkASSERT(0);}
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000182 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000183 {SkASSERT(0);}
184 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
185 size_t count, const SkPoint[],
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000186 const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000187 {SkASSERT(0);}
188 virtual void drawRect(const SkDraw&, const SkRect& r,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000189 const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000190 {SkASSERT(0);}
reed@google.com284a84d2014-02-14 15:23:15 +0000191 virtual void drawOval(const SkDraw&, const SkRect&, const SkPaint&) SK_OVERRIDE
192 {SkASSERT(0);}
scroggo@google.comcac8d012013-11-12 17:10:02 +0000193 virtual void drawRRect(const SkDraw&, const SkRRect& rr,
194 const SkPaint& paint) SK_OVERRIDE
reed@google.com284a84d2014-02-14 15:23:15 +0000195 {SkASSERT(0);}
junov@chromium.org88e29142012-08-07 16:48:22 +0000196 virtual void drawPath(const SkDraw&, const SkPath& path,
197 const SkPaint& paint,
198 const SkMatrix* prePathMatrix = NULL,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000199 bool pathIsMutable = false) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000200 {SkASSERT(0);}
201 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000202 const SkMatrix& matrix, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000203 {SkASSERT(0);}
reed@google.com284a84d2014-02-14 15:23:15 +0000204 virtual void drawBitmapRect(const SkDraw&, const SkBitmap&, const SkRect*,
205 const SkRect&, const SkPaint&,
206 SkCanvas::DrawBitmapRectFlags) SK_OVERRIDE
207 {SkASSERT(0);}
junov@chromium.org88e29142012-08-07 16:48:22 +0000208 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000209 int x, int y, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000210 {SkASSERT(0);}
211 virtual void drawText(const SkDraw&, const void* text, size_t len,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000212 SkScalar x, SkScalar y, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000213 {SkASSERT(0);}
214 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
215 const SkScalar pos[], SkScalar constY,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000216 int scalarsPerPos, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000217 {SkASSERT(0);}
218 virtual void drawTextOnPath(const SkDraw&, const void* text,
219 size_t len, const SkPath& path,
220 const SkMatrix* matrix,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000221 const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000222 {SkASSERT(0);}
junov@chromium.org88e29142012-08-07 16:48:22 +0000223 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
224 int vertexCount, const SkPoint verts[],
225 const SkPoint texs[], const SkColor colors[],
226 SkXfermode* xmode, const uint16_t indices[],
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000227 int indexCount, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000228 {SkASSERT(0);}
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000229 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000230 const SkPaint&) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000231 {SkASSERT(0);}
reed@google.com284a84d2014-02-14 15:23:15 +0000232
233 virtual void lockPixels() SK_OVERRIDE {}
234 virtual void unlockPixels() SK_OVERRIDE {}
235
236 virtual bool allowImageFilter(const SkImageFilter*) SK_OVERRIDE {
237 return false;
238 }
239 virtual bool canHandleImageFilter(const SkImageFilter*) SK_OVERRIDE {
240 return false;
241 }
242 virtual bool filterImage(const SkImageFilter*, const SkBitmap&,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000243 const SkImageFilter::Context&, SkBitmap*, SkIPoint*) SK_OVERRIDE {
reed@google.com284a84d2014-02-14 15:23:15 +0000244 return false;
245 }
246
junov@chromium.org88e29142012-08-07 16:48:22 +0000247private:
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000248 virtual void flush() SK_OVERRIDE;
reed@google.com284a84d2014-02-14 15:23:15 +0000249 virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE {}
junov@chromium.org88e29142012-08-07 16:48:22 +0000250
junov@chromium.org88e29142012-08-07 16:48:22 +0000251 void beginRecording();
junov@chromium.org67d74222013-04-12 13:33:01 +0000252 void init();
junov@chromium.org44324fa2013-08-02 15:36:02 +0000253 void aboutToDraw();
254 void prepareForImmediatePixelWrite();
junov@chromium.org88e29142012-08-07 16:48:22 +0000255
256 DeferredPipeController fPipeController;
257 SkGPipeWriter fPipeWriter;
junov@chromium.org88e29142012-08-07 16:48:22 +0000258 SkCanvas* fImmediateCanvas;
259 SkCanvas* fRecordingCanvas;
junov@chromium.org67d74222013-04-12 13:33:01 +0000260 SkSurface* fSurface;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000261 SkDeferredCanvas::NotificationClient* fNotificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000262 bool fFreshFrame;
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000263 bool fCanDiscardCanvasContents;
junov@chromium.org88e29142012-08-07 16:48:22 +0000264 size_t fMaxRecordingStorageBytes;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000265 size_t fPreviousStorageAllocated;
sugoi@google.com7775fd52012-11-21 15:47:04 +0000266 size_t fBitmapSizeThreshold;
junov@chromium.org88e29142012-08-07 16:48:22 +0000267};
268
reed@google.com9c135db2014-03-12 18:28:35 +0000269SkDeferredDevice::SkDeferredDevice(SkSurface* surface) {
junov@chromium.org67d74222013-04-12 13:33:01 +0000270 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
271 fNotificationClient = NULL;
junov@chromium.org7070f762013-05-24 17:13:00 +0000272 fImmediateCanvas = NULL;
273 fSurface = NULL;
274 this->setSurface(surface);
junov@chromium.org67d74222013-04-12 13:33:01 +0000275 this->init();
276}
277
reed@google.com9c135db2014-03-12 18:28:35 +0000278void SkDeferredDevice::setSurface(SkSurface* surface) {
junov@chromium.org7070f762013-05-24 17:13:00 +0000279 SkRefCnt_SafeAssign(fImmediateCanvas, surface->getCanvas());
280 SkRefCnt_SafeAssign(fSurface, surface);
281 fPipeController.setPlaybackCanvas(fImmediateCanvas);
282}
283
reed@google.com9c135db2014-03-12 18:28:35 +0000284void SkDeferredDevice::init() {
junov@chromium.org67d74222013-04-12 13:33:01 +0000285 fRecordingCanvas = NULL;
286 fFreshFrame = true;
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000287 fCanDiscardCanvasContents = false;
junov@chromium.org67d74222013-04-12 13:33:01 +0000288 fPreviousStorageAllocated = 0;
289 fBitmapSizeThreshold = kDeferredCanvasBitmapSizeThreshold;
290 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
291 fNotificationClient = NULL;
junov@chromium.org88e29142012-08-07 16:48:22 +0000292 this->beginRecording();
293}
294
reed@google.com9c135db2014-03-12 18:28:35 +0000295SkDeferredDevice::~SkDeferredDevice() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000296 this->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000297 SkSafeUnref(fImmediateCanvas);
junov@chromium.org67d74222013-04-12 13:33:01 +0000298 SkSafeUnref(fSurface);
junov@chromium.org88e29142012-08-07 16:48:22 +0000299}
300
reed@google.com9c135db2014-03-12 18:28:35 +0000301void SkDeferredDevice::setMaxRecordingStorage(size_t maxStorage) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000302 fMaxRecordingStorageBytes = maxStorage;
303 this->recordingCanvas(); // Accessing the recording canvas applies the new limit.
304}
305
reed@google.com9c135db2014-03-12 18:28:35 +0000306void SkDeferredDevice::beginRecording() {
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000307 SkASSERT(NULL == fRecordingCanvas);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000308 fRecordingCanvas = fPipeWriter.startRecording(&fPipeController, 0,
junov@chromium.org9becf002013-04-15 18:15:23 +0000309 immediateDevice()->width(), immediateDevice()->height());
junov@chromium.org88e29142012-08-07 16:48:22 +0000310}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000311
reed@google.com9c135db2014-03-12 18:28:35 +0000312void SkDeferredDevice::setNotificationClient(
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000313 SkDeferredCanvas::NotificationClient* notificationClient) {
junov@chromium.org52805482012-08-20 14:25:04 +0000314 fNotificationClient = notificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000315}
316
reed@google.com9c135db2014-03-12 18:28:35 +0000317void SkDeferredDevice::skipPendingCommands() {
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000318 if (!fRecordingCanvas->isDrawingToLayer()) {
319 fCanDiscardCanvasContents = true;
320 if (fPipeController.hasPendingCommands()) {
321 fFreshFrame = true;
322 flushPendingCommands(kSilent_PlaybackMode);
junov@google.com52a00ca2012-10-01 15:27:14 +0000323 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000324 }
325}
326
reed@google.com9c135db2014-03-12 18:28:35 +0000327bool SkDeferredDevice::isFreshFrame() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000328 bool ret = fFreshFrame;
329 fFreshFrame = false;
330 return ret;
331}
332
reed@google.com9c135db2014-03-12 18:28:35 +0000333bool SkDeferredDevice::hasPendingCommands() {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000334 return fPipeController.hasPendingCommands();
335}
336
reed@google.com9c135db2014-03-12 18:28:35 +0000337void SkDeferredDevice::aboutToDraw()
junov@chromium.org44324fa2013-08-02 15:36:02 +0000338{
339 if (NULL != fNotificationClient) {
340 fNotificationClient->prepareForDraw();
341 }
342 if (fCanDiscardCanvasContents) {
343 if (NULL != fSurface) {
344 fSurface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
345 }
skia.committer@gmail.comea4b7972013-08-06 07:01:27 +0000346 fCanDiscardCanvasContents = false;
junov@chromium.org44324fa2013-08-02 15:36:02 +0000347 }
348}
349
reed@google.com9c135db2014-03-12 18:28:35 +0000350void SkDeferredDevice::flushPendingCommands(PlaybackMode playbackMode) {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000351 if (!fPipeController.hasPendingCommands()) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000352 return;
353 }
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000354 if (playbackMode == kNormal_PlaybackMode) {
junov@chromium.org44324fa2013-08-02 15:36:02 +0000355 aboutToDraw();
junov@chromium.org88e29142012-08-07 16:48:22 +0000356 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000357 fPipeWriter.flushRecording(true);
junov@chromium.orgd4501a02012-10-30 19:05:17 +0000358 fPipeController.playback(kSilent_PlaybackMode == playbackMode);
commit-bot@chromium.orgdad009b2014-03-27 15:48:52 +0000359 if (fNotificationClient) {
360 if (playbackMode == kSilent_PlaybackMode) {
361 fNotificationClient->skippedPendingDrawCommands();
362 } else {
363 fNotificationClient->flushedDrawCommands();
364 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000365 }
commit-bot@chromium.orgdad009b2014-03-27 15:48:52 +0000366
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000367 fPreviousStorageAllocated = storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000368}
369
reed@google.com9c135db2014-03-12 18:28:35 +0000370void SkDeferredDevice::flush() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000371 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000372 fImmediateCanvas->flush();
373}
374
reed@google.com9c135db2014-03-12 18:28:35 +0000375size_t SkDeferredDevice::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000376 size_t val = fPipeWriter.freeMemoryIfPossible(bytesToFree);
377 fPreviousStorageAllocated = storageAllocatedForRecording();
378 return val;
junov@chromium.org88e29142012-08-07 16:48:22 +0000379}
380
reed@google.com9c135db2014-03-12 18:28:35 +0000381size_t SkDeferredDevice::getBitmapSizeThreshold() const {
sugoi@google.com7775fd52012-11-21 15:47:04 +0000382 return fBitmapSizeThreshold;
383}
384
reed@google.com9c135db2014-03-12 18:28:35 +0000385void SkDeferredDevice::setBitmapSizeThreshold(size_t sizeThreshold) {
sugoi@google.com7775fd52012-11-21 15:47:04 +0000386 fBitmapSizeThreshold = sizeThreshold;
387}
388
reed@google.com9c135db2014-03-12 18:28:35 +0000389size_t SkDeferredDevice::storageAllocatedForRecording() const {
junov@chromium.org88e29142012-08-07 16:48:22 +0000390 return (fPipeController.storageAllocatedForRecording()
391 + fPipeWriter.storageAllocatedForRecording());
392}
393
reed@google.com9c135db2014-03-12 18:28:35 +0000394void SkDeferredDevice::recordedDrawCommand() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000395 size_t storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000396
junov@chromium.org88e29142012-08-07 16:48:22 +0000397 if (storageAllocated > fMaxRecordingStorageBytes) {
398 // First, attempt to reduce cache without flushing
399 size_t tryFree = storageAllocated - fMaxRecordingStorageBytes;
400 if (this->freeMemoryIfPossible(tryFree) < tryFree) {
401 // Flush is necessary to free more space.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000402 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000403 // Free as much as possible to avoid oscillating around fMaxRecordingStorageBytes
404 // which could cause a high flushing frequency.
bsalomon@google.com100abf42012-09-05 17:40:04 +0000405 this->freeMemoryIfPossible(~0U);
junov@chromium.org88e29142012-08-07 16:48:22 +0000406 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000407 storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000408 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000409
rmistry@google.comd6176b02012-08-23 18:14:13 +0000410 if (fNotificationClient &&
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000411 storageAllocated != fPreviousStorageAllocated) {
412 fPreviousStorageAllocated = storageAllocated;
413 fNotificationClient->storageAllocatedForRecordingChanged(storageAllocated);
414 }
415}
416
reed@google.com9c135db2014-03-12 18:28:35 +0000417SkCanvas* SkDeferredDevice::recordingCanvas() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000418 return fRecordingCanvas;
419}
420
reed@google.com9c135db2014-03-12 18:28:35 +0000421SkImage* SkDeferredDevice::newImageSnapshot() {
junov@chromium.org67d74222013-04-12 13:33:01 +0000422 this->flush();
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000423 return fSurface ? fSurface->newImageSnapshot() : NULL;
junov@chromium.org67d74222013-04-12 13:33:01 +0000424}
425
reed@google.com9c135db2014-03-12 18:28:35 +0000426SkImageInfo SkDeferredDevice::imageInfo() const {
reed@google.com284a84d2014-02-14 15:23:15 +0000427 return immediateDevice()->imageInfo();
428}
429
reed@google.com9c135db2014-03-12 18:28:35 +0000430GrRenderTarget* SkDeferredDevice::accessRenderTarget() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000431 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000432 return immediateDevice()->accessRenderTarget();
junov@chromium.org88e29142012-08-07 16:48:22 +0000433}
434
reed@google.com9c135db2014-03-12 18:28:35 +0000435void SkDeferredDevice::prepareForImmediatePixelWrite() {
junov@chromium.org44324fa2013-08-02 15:36:02 +0000436 // The purpose of the following code is to make sure commands are flushed, that
437 // aboutToDraw() is called and that notifyContentWillChange is called, without
438 // calling anything redundantly.
439 if (fPipeController.hasPendingCommands()) {
440 this->flushPendingCommands(kNormal_PlaybackMode);
441 } else {
442 bool mustNotifyDirectly = !fCanDiscardCanvasContents;
443 this->aboutToDraw();
444 if (mustNotifyDirectly) {
445 fSurface->notifyContentWillChange(SkSurface::kRetain_ContentChangeMode);
446 }
447 }
448
449 fImmediateCanvas->flush();
450}
451
reed@google.com9c135db2014-03-12 18:28:35 +0000452bool SkDeferredDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000453 int x, int y) {
454 SkASSERT(x >= 0 && y >= 0);
455 SkASSERT(x + info.width() <= width());
456 SkASSERT(y + info.height() <= height());
457
458 this->flushPendingCommands(kNormal_PlaybackMode);
459
460 const SkImageInfo deviceInfo = this->imageInfo();
461 if (info.width() == deviceInfo.width() && info.height() == deviceInfo.height()) {
462 this->skipPendingCommands();
463 }
skia.committer@gmail.come62513f2014-03-08 03:02:06 +0000464
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000465 this->prepareForImmediatePixelWrite();
466 return immediateDevice()->onWritePixels(info, pixels, rowBytes, x, y);
467}
junov@chromium.org88e29142012-08-07 16:48:22 +0000468
reed@google.com9c135db2014-03-12 18:28:35 +0000469const SkBitmap& SkDeferredDevice::onAccessBitmap() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000470 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000471 return immediateDevice()->accessBitmap(false);
junov@chromium.org88e29142012-08-07 16:48:22 +0000472}
473
reed@google.com9c135db2014-03-12 18:28:35 +0000474SkBaseDevice* SkDeferredDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000475 // Save layer usage not supported, and not required by SkDeferredCanvas.
476 SkASSERT(usage != kSaveLayer_Usage);
477 // Create a compatible non-deferred device.
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000478 // We do not create a deferred device because we know the new device
479 // will not be used with a deferred canvas (there is no API for that).
reed@google.com9c135db2014-03-12 18:28:35 +0000480 // And connecting a SkDeferredDevice to non-deferred canvas can result
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000481 // in unpredictable behavior.
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000482 return immediateDevice()->createCompatibleDevice(info);
junov@chromium.org88e29142012-08-07 16:48:22 +0000483}
484
reed@google.com9c135db2014-03-12 18:28:35 +0000485SkSurface* SkDeferredDevice::newSurface(const SkImageInfo& info) {
reed@google.com76f10a32014-02-05 15:32:21 +0000486 return this->immediateDevice()->newSurface(info);
487}
488
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000489bool SkDeferredDevice::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
490 int x, int y) {
491 this->flushPendingCommands(kNormal_PlaybackMode);
492 return fImmediateCanvas->readPixels(info, pixels, rowBytes, x, y);
493}
junov@chromium.org88e29142012-08-07 16:48:22 +0000494
sugoi@google.com7775fd52012-11-21 15:47:04 +0000495class AutoImmediateDrawIfNeeded {
496public:
497 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkBitmap* bitmap,
498 const SkPaint* paint) {
499 this->init(canvas, bitmap, paint);
500 }
501
502 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkPaint* paint) {
503 this->init(canvas, NULL, paint);
504 }
505
506 ~AutoImmediateDrawIfNeeded() {
507 if (fCanvas) {
508 fCanvas->setDeferredDrawing(true);
509 }
510 }
511private:
512 void init(SkDeferredCanvas& canvas, const SkBitmap* bitmap, const SkPaint* paint)
513 {
reed@google.com9c135db2014-03-12 18:28:35 +0000514 SkDeferredDevice* device = static_cast<SkDeferredDevice*>(canvas.getDevice());
sugoi@google.com7775fd52012-11-21 15:47:04 +0000515 if (canvas.isDeferredDrawing() && (NULL != device) &&
516 shouldDrawImmediately(bitmap, paint, device->getBitmapSizeThreshold())) {
517 canvas.setDeferredDrawing(false);
518 fCanvas = &canvas;
519 } else {
520 fCanvas = NULL;
521 }
522 }
523
524 SkDeferredCanvas* fCanvas;
525};
junov@chromium.org88e29142012-08-07 16:48:22 +0000526
junov@chromium.org66070a52013-05-28 17:39:08 +0000527SkDeferredCanvas* SkDeferredCanvas::Create(SkSurface* surface) {
reed@google.com9c135db2014-03-12 18:28:35 +0000528 SkAutoTUnref<SkDeferredDevice> deferredDevice(SkNEW_ARGS(SkDeferredDevice, (surface)));
junov@chromium.org66070a52013-05-28 17:39:08 +0000529 return SkNEW_ARGS(SkDeferredCanvas, (deferredDevice));
530}
531
reed@google.com9c135db2014-03-12 18:28:35 +0000532SkDeferredCanvas::SkDeferredCanvas(SkDeferredDevice* device) : SkCanvas (device) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000533 this->init();
534}
junov@chromium.org67d74222013-04-12 13:33:01 +0000535
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000536void SkDeferredCanvas::init() {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000537 fDeferredDrawing = true; // On by default
junov@google.com4370aed2012-01-18 16:21:08 +0000538}
539
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000540void SkDeferredCanvas::setMaxRecordingStorage(size_t maxStorage) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000541 this->validate();
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000542 this->getDeferredDevice()->setMaxRecordingStorage(maxStorage);
543}
544
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000545size_t SkDeferredCanvas::storageAllocatedForRecording() const {
546 return this->getDeferredDevice()->storageAllocatedForRecording();
547}
548
549size_t SkDeferredCanvas::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000550 return this->getDeferredDevice()->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000551}
552
sugoi@google.com7775fd52012-11-21 15:47:04 +0000553void SkDeferredCanvas::setBitmapSizeThreshold(size_t sizeThreshold) {
reed@google.com9c135db2014-03-12 18:28:35 +0000554 SkDeferredDevice* deferredDevice = this->getDeferredDevice();
sugoi@google.com7775fd52012-11-21 15:47:04 +0000555 SkASSERT(deferredDevice);
556 deferredDevice->setBitmapSizeThreshold(sizeThreshold);
557}
558
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000559void SkDeferredCanvas::recordedDrawCommand() {
560 if (fDeferredDrawing) {
561 this->getDeferredDevice()->recordedDrawCommand();
562 }
563}
564
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000565void SkDeferredCanvas::validate() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000566 SkASSERT(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000567}
568
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000569SkCanvas* SkDeferredCanvas::drawingCanvas() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000570 this->validate();
571 return fDeferredDrawing ? this->getDeferredDevice()->recordingCanvas() :
572 this->getDeferredDevice()->immediateCanvas();
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000573}
574
junov@chromium.org88e29142012-08-07 16:48:22 +0000575SkCanvas* SkDeferredCanvas::immediateCanvas() const {
576 this->validate();
577 return this->getDeferredDevice()->immediateCanvas();
578}
579
reed@google.com9c135db2014-03-12 18:28:35 +0000580SkDeferredDevice* SkDeferredCanvas::getDeferredDevice() const {
581 return static_cast<SkDeferredDevice*>(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000582}
583
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000584void SkDeferredCanvas::setDeferredDrawing(bool val) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000585 this->validate(); // Must set device before calling this method
junov@google.com4370aed2012-01-18 16:21:08 +0000586 if (val != fDeferredDrawing) {
587 if (fDeferredDrawing) {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000588 // Going live.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000589 this->getDeferredDevice()->flushPendingCommands(kNormal_PlaybackMode);
junov@google.com4370aed2012-01-18 16:21:08 +0000590 }
591 fDeferredDrawing = val;
592 }
593}
594
junov@chromium.org88e29142012-08-07 16:48:22 +0000595bool SkDeferredCanvas::isDeferredDrawing() const {
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +0000596 return fDeferredDrawing;
597}
598
junov@chromium.org88e29142012-08-07 16:48:22 +0000599bool SkDeferredCanvas::isFreshFrame() const {
600 return this->getDeferredDevice()->isFreshFrame();
601}
602
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000603bool SkDeferredCanvas::hasPendingCommands() const {
604 return this->getDeferredDevice()->hasPendingCommands();
605}
606
junov@chromium.orgfb103892012-09-20 19:35:43 +0000607void SkDeferredCanvas::silentFlush() {
608 if (fDeferredDrawing) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000609 this->getDeferredDevice()->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000610 }
611}
612
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000613SkDeferredCanvas::~SkDeferredCanvas() {
junov@google.com4370aed2012-01-18 16:21:08 +0000614}
615
junov@chromium.org7070f762013-05-24 17:13:00 +0000616SkSurface* SkDeferredCanvas::setSurface(SkSurface* surface) {
reed@google.com9c135db2014-03-12 18:28:35 +0000617 SkDeferredDevice* deferredDevice = this->getDeferredDevice();
junov@chromium.org66070a52013-05-28 17:39:08 +0000618 SkASSERT(NULL != deferredDevice);
619 // By swapping the surface into the existing device, we preserve
620 // all pending commands, which can help to seamlessly recover from
621 // a lost accelerated graphics context.
622 deferredDevice->setSurface(surface);
junov@chromium.org7070f762013-05-24 17:13:00 +0000623 return surface;
624}
625
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000626SkDeferredCanvas::NotificationClient* SkDeferredCanvas::setNotificationClient(
627 NotificationClient* notificationClient) {
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000628
reed@google.com9c135db2014-03-12 18:28:35 +0000629 SkDeferredDevice* deferredDevice = this->getDeferredDevice();
junov@google.com4370aed2012-01-18 16:21:08 +0000630 SkASSERT(deferredDevice);
631 if (deferredDevice) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000632 deferredDevice->setNotificationClient(notificationClient);
junov@google.com4370aed2012-01-18 16:21:08 +0000633 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000634 return notificationClient;
junov@google.com4370aed2012-01-18 16:21:08 +0000635}
636
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000637SkImage* SkDeferredCanvas::newImageSnapshot() {
reed@google.com9c135db2014-03-12 18:28:35 +0000638 SkDeferredDevice* deferredDevice = this->getDeferredDevice();
junov@chromium.org67d74222013-04-12 13:33:01 +0000639 SkASSERT(deferredDevice);
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000640 return deferredDevice ? deferredDevice->newImageSnapshot() : NULL;
junov@chromium.org67d74222013-04-12 13:33:01 +0000641}
642
junov@google.com4370aed2012-01-18 16:21:08 +0000643bool SkDeferredCanvas::isFullFrame(const SkRect* rect,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000644 const SkPaint* paint) const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000645 SkCanvas* canvas = this->drawingCanvas();
646 SkISize canvasSize = this->getDeviceSize();
junov@google.com4370aed2012-01-18 16:21:08 +0000647 if (rect) {
648 if (!canvas->getTotalMatrix().rectStaysRect()) {
649 return false; // conservative
650 }
651
652 SkRect transformedRect;
653 canvas->getTotalMatrix().mapRect(&transformedRect, *rect);
654
655 if (paint) {
656 SkPaint::Style paintStyle = paint->getStyle();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000657 if (!(paintStyle == SkPaint::kFill_Style ||
junov@google.com4370aed2012-01-18 16:21:08 +0000658 paintStyle == SkPaint::kStrokeAndFill_Style)) {
659 return false;
660 }
661 if (paint->getMaskFilter() || paint->getLooper()
662 || paint->getPathEffect() || paint->getImageFilter()) {
663 return false; // conservative
664 }
665 }
666
667 // The following test holds with AA enabled, and is conservative
668 // by a 0.5 pixel margin with AA disabled
rmistry@google.comd6176b02012-08-23 18:14:13 +0000669 if (transformedRect.fLeft > SkIntToScalar(0) ||
670 transformedRect.fTop > SkIntToScalar(0) ||
junov@chromium.orgb1e218e2012-02-13 22:27:58 +0000671 transformedRect.fRight < SkIntToScalar(canvasSize.fWidth) ||
672 transformedRect.fBottom < SkIntToScalar(canvasSize.fHeight)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000673 return false;
674 }
675 }
676
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000677 return this->getClipStack()->quickContains(SkRect::MakeXYWH(0, 0,
678 SkIntToScalar(canvasSize.fWidth), SkIntToScalar(canvasSize.fHeight)));
junov@google.com4370aed2012-01-18 16:21:08 +0000679}
680
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000681void SkDeferredCanvas::willSave(SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000682 this->drawingCanvas()->save(flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000683 this->recordedDrawCommand();
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000684 this->INHERITED::willSave(flags);
junov@google.com4370aed2012-01-18 16:21:08 +0000685}
686
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000687SkCanvas::SaveLayerStrategy SkDeferredCanvas::willSaveLayer(const SkRect* bounds,
688 const SkPaint* paint, SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000689 this->drawingCanvas()->saveLayer(bounds, paint, flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000690 this->recordedDrawCommand();
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000691 this->INHERITED::willSaveLayer(bounds, paint, flags);
692 // No need for a full layer.
693 return kNoLayer_SaveLayerStrategy;
junov@google.com4370aed2012-01-18 16:21:08 +0000694}
695
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000696void SkDeferredCanvas::willRestore() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000697 this->drawingCanvas()->restore();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000698 this->recordedDrawCommand();
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000699 this->INHERITED::willRestore();
junov@google.com4370aed2012-01-18 16:21:08 +0000700}
701
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000702bool SkDeferredCanvas::isDrawingToLayer() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000703 return this->drawingCanvas()->isDrawingToLayer();
junov@google.com4370aed2012-01-18 16:21:08 +0000704}
705
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000706void SkDeferredCanvas::didConcat(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000707 this->drawingCanvas()->concat(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000708 this->recordedDrawCommand();
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000709 this->INHERITED::didConcat(matrix);
junov@google.com4370aed2012-01-18 16:21:08 +0000710}
711
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000712void SkDeferredCanvas::didSetMatrix(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000713 this->drawingCanvas()->setMatrix(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000714 this->recordedDrawCommand();
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000715 this->INHERITED::didSetMatrix(matrix);
junov@google.com4370aed2012-01-18 16:21:08 +0000716}
717
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000718void SkDeferredCanvas::onClipRect(const SkRect& rect,
719 SkRegion::Op op,
720 ClipEdgeStyle edgeStyle) {
721 this->drawingCanvas()->clipRect(rect, op, kSoft_ClipEdgeStyle == edgeStyle);
722 this->INHERITED::onClipRect(rect, op, edgeStyle);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000723 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000724}
725
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000726void SkDeferredCanvas::onClipRRect(const SkRRect& rrect,
727 SkRegion::Op op,
728 ClipEdgeStyle edgeStyle) {
729 this->drawingCanvas()->clipRRect(rrect, op, kSoft_ClipEdgeStyle == edgeStyle);
730 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000731 this->recordedDrawCommand();
reed@google.com4ed0fb72012-12-12 20:48:18 +0000732}
733
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000734void SkDeferredCanvas::onClipPath(const SkPath& path,
735 SkRegion::Op op,
736 ClipEdgeStyle edgeStyle) {
737 this->drawingCanvas()->clipPath(path, op, kSoft_ClipEdgeStyle == edgeStyle);
738 this->INHERITED::onClipPath(path, op, edgeStyle);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000739 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000740}
741
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000742void SkDeferredCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000743 this->drawingCanvas()->clipRegion(deviceRgn, op);
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000744 this->INHERITED::onClipRegion(deviceRgn, op);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000745 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000746}
747
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000748void SkDeferredCanvas::clear(SkColor color) {
junov@google.com4370aed2012-01-18 16:21:08 +0000749 // purge pending commands
750 if (fDeferredDrawing) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000751 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000752 }
753
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000754 this->drawingCanvas()->clear(color);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000755 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000756}
757
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000758void SkDeferredCanvas::drawPaint(const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000759 if (fDeferredDrawing && this->isFullFrame(NULL, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000760 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000761 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000762 }
junov@chromium.org10f7f972012-07-31 21:01:51 +0000763 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000764 this->drawingCanvas()->drawPaint(paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000765 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000766}
767
768void SkDeferredCanvas::drawPoints(PointMode mode, size_t count,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000769 const SkPoint pts[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000770 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000771 this->drawingCanvas()->drawPoints(mode, count, pts, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000772 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000773}
774
reed@google.com4ed0fb72012-12-12 20:48:18 +0000775void SkDeferredCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
776 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
777 this->drawingCanvas()->drawOval(rect, paint);
778 this->recordedDrawCommand();
779}
780
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000781void SkDeferredCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000782 if (fDeferredDrawing && this->isFullFrame(&rect, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000783 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000784 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000785 }
skia.committer@gmail.com306ab9d2012-12-13 02:01:33 +0000786
junov@chromium.org10f7f972012-07-31 21:01:51 +0000787 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000788 this->drawingCanvas()->drawRect(rect, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000789 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000790}
791
reed@google.com4ed0fb72012-12-12 20:48:18 +0000792void SkDeferredCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
793 if (rrect.isRect()) {
794 this->SkDeferredCanvas::drawRect(rrect.getBounds(), paint);
795 } else if (rrect.isOval()) {
796 this->SkDeferredCanvas::drawOval(rrect.getBounds(), paint);
797 } else {
798 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
799 this->drawingCanvas()->drawRRect(rrect, paint);
800 this->recordedDrawCommand();
801 }
802}
803
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000804void SkDeferredCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
805 const SkPaint& paint) {
806 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
807 this->drawingCanvas()->drawDRRect(outer, inner, paint);
808 this->recordedDrawCommand();
809}
810
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000811void SkDeferredCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000812 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000813 this->drawingCanvas()->drawPath(path, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000814 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000815}
816
817void SkDeferredCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000818 SkScalar top, const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000819 SkRect bitmapRect = SkRect::MakeXYWH(left, top,
820 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000821 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000822 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000823 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000824 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000825 }
826
junov@chromium.org10f7f972012-07-31 21:01:51 +0000827 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000828 this->drawingCanvas()->drawBitmap(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000829 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000830}
831
reed@google.com71121732012-09-18 15:14:33 +0000832void SkDeferredCanvas::drawBitmapRectToRect(const SkBitmap& bitmap,
833 const SkRect* src,
834 const SkRect& dst,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000835 const SkPaint* paint,
836 DrawBitmapRectFlags flags) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000837 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000838 this->isFullFrame(&dst, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000839 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000840 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000841 }
842
junov@chromium.org10f7f972012-07-31 21:01:51 +0000843 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000844 this->drawingCanvas()->drawBitmapRectToRect(bitmap, src, dst, paint, flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000845 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000846}
847
848
849void SkDeferredCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
850 const SkMatrix& m,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000851 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000852 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
853 // covers canvas entirely and transformed bitmap covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000854 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000855 this->drawingCanvas()->drawBitmapMatrix(bitmap, m, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000856 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000857}
858
859void SkDeferredCanvas::drawBitmapNine(const SkBitmap& bitmap,
860 const SkIRect& center, const SkRect& dst,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000861 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000862 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
863 // covers canvas entirely and dst covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000864 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000865 this->drawingCanvas()->drawBitmapNine(bitmap, center, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000866 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000867}
868
869void SkDeferredCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000870 const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000871 SkRect bitmapRect = SkRect::MakeXYWH(
872 SkIntToScalar(left),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000873 SkIntToScalar(top),
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000874 SkIntToScalar(bitmap.width()),
875 SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000876 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000877 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000878 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000879 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000880 }
881
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()->drawSprite(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000884 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000885}
886
reed@google.come0d9ce82014-04-23 04:00:17 +0000887void SkDeferredCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
888 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000889 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000890 this->drawingCanvas()->drawText(text, byteLength, x, y, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000891 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000892}
893
reed@google.come0d9ce82014-04-23 04:00:17 +0000894void SkDeferredCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
895 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000896 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000897 this->drawingCanvas()->drawPosText(text, byteLength, pos, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000898 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000899}
900
reed@google.come0d9ce82014-04-23 04:00:17 +0000901void SkDeferredCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
902 SkScalar constY, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000903 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000904 this->drawingCanvas()->drawPosTextH(text, byteLength, xpos, constY, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000905 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000906}
907
reed@google.come0d9ce82014-04-23 04:00:17 +0000908void SkDeferredCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
909 const SkMatrix* matrix, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000910 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000911 this->drawingCanvas()->drawTextOnPath(text, byteLength, path, matrix, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000912 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000913}
914
robertphillips9b14f262014-06-04 05:40:44 -0700915void SkDeferredCanvas::onDrawPicture(const SkPicture* picture) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000916 this->drawingCanvas()->drawPicture(picture);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000917 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000918}
919
920void SkDeferredCanvas::drawVertices(VertexMode vmode, int vertexCount,
921 const SkPoint vertices[],
922 const SkPoint texs[],
923 const SkColor colors[], SkXfermode* xmode,
924 const uint16_t indices[], int indexCount,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000925 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000926 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000927 this->drawingCanvas()->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode,
928 indices, indexCount, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000929 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000930}
931
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000932SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000933 this->drawingCanvas()->setDrawFilter(filter);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000934 this->INHERITED::setDrawFilter(filter);
935 this->recordedDrawCommand();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000936 return filter;
junov@google.com4370aed2012-01-18 16:21:08 +0000937}
938
939SkCanvas* SkDeferredCanvas::canvasForDrawIter() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000940 return this->drawingCanvas();
junov@google.com4370aed2012-01-18 16:21:08 +0000941}