blob: 02c2c5af285e47d22d224e0a9e84cd9b3ecc5264 [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;
reeda6a8f002014-06-02 05:45:31 -0700162#ifdef SK_SUPPORT_LEGACY_DEVICE_CONFIG
reed@google.com284a84d2014-02-14 15:23:15 +0000163 virtual SkBitmap::Config config() const SK_OVERRIDE;
reeda6a8f002014-06-02 05:45:31 -0700164#endif
reed@google.com284a84d2014-02-14 15:23:15 +0000165 virtual bool isOpaque() const SK_OVERRIDE;
166 virtual SkImageInfo imageInfo() const SK_OVERRIDE;
167
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000168 virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE;
junov@chromium.org88e29142012-08-07 16:48:22 +0000169
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000170 virtual SkBaseDevice* onCreateDevice(const SkImageInfo&, Usage) SK_OVERRIDE;
junov@chromium.org88e29142012-08-07 16:48:22 +0000171
reed@google.com76f10a32014-02-05 15:32:21 +0000172 virtual SkSurface* newSurface(const SkImageInfo&) SK_OVERRIDE;
173
junov@chromium.org88e29142012-08-07 16:48:22 +0000174protected:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000175 virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE;
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000176 virtual bool onReadPixels(const SkImageInfo&, void*, size_t, int x, int y) SK_OVERRIDE;
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000177 virtual bool onWritePixels(const SkImageInfo&, const void*, size_t, int x, int y) SK_OVERRIDE;
junov@chromium.org88e29142012-08-07 16:48:22 +0000178
179 // The following methods are no-ops on a deferred device
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000180 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE {
181 return false;
182 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000183
184 // None of the following drawing methods should ever get called on the
185 // deferred device
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000186 virtual void clear(SkColor color) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000187 {SkASSERT(0);}
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000188 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000189 {SkASSERT(0);}
190 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
191 size_t count, const SkPoint[],
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000192 const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000193 {SkASSERT(0);}
194 virtual void drawRect(const SkDraw&, const SkRect& r,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000195 const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000196 {SkASSERT(0);}
reed@google.com284a84d2014-02-14 15:23:15 +0000197 virtual void drawOval(const SkDraw&, const SkRect&, const SkPaint&) SK_OVERRIDE
198 {SkASSERT(0);}
scroggo@google.comcac8d012013-11-12 17:10:02 +0000199 virtual void drawRRect(const SkDraw&, const SkRRect& rr,
200 const SkPaint& paint) SK_OVERRIDE
reed@google.com284a84d2014-02-14 15:23:15 +0000201 {SkASSERT(0);}
junov@chromium.org88e29142012-08-07 16:48:22 +0000202 virtual void drawPath(const SkDraw&, const SkPath& path,
203 const SkPaint& paint,
204 const SkMatrix* prePathMatrix = NULL,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000205 bool pathIsMutable = false) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000206 {SkASSERT(0);}
207 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000208 const SkMatrix& matrix, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000209 {SkASSERT(0);}
reed@google.com284a84d2014-02-14 15:23:15 +0000210 virtual void drawBitmapRect(const SkDraw&, const SkBitmap&, const SkRect*,
211 const SkRect&, const SkPaint&,
212 SkCanvas::DrawBitmapRectFlags) SK_OVERRIDE
213 {SkASSERT(0);}
junov@chromium.org88e29142012-08-07 16:48:22 +0000214 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000215 int x, int y, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000216 {SkASSERT(0);}
217 virtual void drawText(const SkDraw&, const void* text, size_t len,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000218 SkScalar x, SkScalar y, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000219 {SkASSERT(0);}
220 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
221 const SkScalar pos[], SkScalar constY,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000222 int scalarsPerPos, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000223 {SkASSERT(0);}
224 virtual void drawTextOnPath(const SkDraw&, const void* text,
225 size_t len, const SkPath& path,
226 const SkMatrix* matrix,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000227 const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000228 {SkASSERT(0);}
junov@chromium.org88e29142012-08-07 16:48:22 +0000229 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
230 int vertexCount, const SkPoint verts[],
231 const SkPoint texs[], const SkColor colors[],
232 SkXfermode* xmode, const uint16_t indices[],
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000233 int indexCount, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000234 {SkASSERT(0);}
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000235 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000236 const SkPaint&) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000237 {SkASSERT(0);}
reed@google.com284a84d2014-02-14 15:23:15 +0000238
239 virtual void lockPixels() SK_OVERRIDE {}
240 virtual void unlockPixels() SK_OVERRIDE {}
241
242 virtual bool allowImageFilter(const SkImageFilter*) SK_OVERRIDE {
243 return false;
244 }
245 virtual bool canHandleImageFilter(const SkImageFilter*) SK_OVERRIDE {
246 return false;
247 }
248 virtual bool filterImage(const SkImageFilter*, const SkBitmap&,
senorblanco@chromium.org4cb543d2014-03-14 15:44:01 +0000249 const SkImageFilter::Context&, SkBitmap*, SkIPoint*) SK_OVERRIDE {
reed@google.com284a84d2014-02-14 15:23:15 +0000250 return false;
251 }
252
junov@chromium.org88e29142012-08-07 16:48:22 +0000253private:
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000254 virtual void flush() SK_OVERRIDE;
reed@google.com284a84d2014-02-14 15:23:15 +0000255 virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE {}
junov@chromium.org88e29142012-08-07 16:48:22 +0000256
junov@chromium.org88e29142012-08-07 16:48:22 +0000257 void beginRecording();
junov@chromium.org67d74222013-04-12 13:33:01 +0000258 void init();
junov@chromium.org44324fa2013-08-02 15:36:02 +0000259 void aboutToDraw();
260 void prepareForImmediatePixelWrite();
junov@chromium.org88e29142012-08-07 16:48:22 +0000261
262 DeferredPipeController fPipeController;
263 SkGPipeWriter fPipeWriter;
junov@chromium.org88e29142012-08-07 16:48:22 +0000264 SkCanvas* fImmediateCanvas;
265 SkCanvas* fRecordingCanvas;
junov@chromium.org67d74222013-04-12 13:33:01 +0000266 SkSurface* fSurface;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000267 SkDeferredCanvas::NotificationClient* fNotificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000268 bool fFreshFrame;
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000269 bool fCanDiscardCanvasContents;
junov@chromium.org88e29142012-08-07 16:48:22 +0000270 size_t fMaxRecordingStorageBytes;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000271 size_t fPreviousStorageAllocated;
sugoi@google.com7775fd52012-11-21 15:47:04 +0000272 size_t fBitmapSizeThreshold;
junov@chromium.org88e29142012-08-07 16:48:22 +0000273};
274
reed@google.com9c135db2014-03-12 18:28:35 +0000275SkDeferredDevice::SkDeferredDevice(SkSurface* surface) {
junov@chromium.org67d74222013-04-12 13:33:01 +0000276 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
277 fNotificationClient = NULL;
junov@chromium.org7070f762013-05-24 17:13:00 +0000278 fImmediateCanvas = NULL;
279 fSurface = NULL;
280 this->setSurface(surface);
junov@chromium.org67d74222013-04-12 13:33:01 +0000281 this->init();
282}
283
reed@google.com9c135db2014-03-12 18:28:35 +0000284void SkDeferredDevice::setSurface(SkSurface* surface) {
junov@chromium.org7070f762013-05-24 17:13:00 +0000285 SkRefCnt_SafeAssign(fImmediateCanvas, surface->getCanvas());
286 SkRefCnt_SafeAssign(fSurface, surface);
287 fPipeController.setPlaybackCanvas(fImmediateCanvas);
288}
289
reed@google.com9c135db2014-03-12 18:28:35 +0000290void SkDeferredDevice::init() {
junov@chromium.org67d74222013-04-12 13:33:01 +0000291 fRecordingCanvas = NULL;
292 fFreshFrame = true;
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000293 fCanDiscardCanvasContents = false;
junov@chromium.org67d74222013-04-12 13:33:01 +0000294 fPreviousStorageAllocated = 0;
295 fBitmapSizeThreshold = kDeferredCanvasBitmapSizeThreshold;
296 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
297 fNotificationClient = NULL;
junov@chromium.org88e29142012-08-07 16:48:22 +0000298 this->beginRecording();
299}
300
reed@google.com9c135db2014-03-12 18:28:35 +0000301SkDeferredDevice::~SkDeferredDevice() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000302 this->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000303 SkSafeUnref(fImmediateCanvas);
junov@chromium.org67d74222013-04-12 13:33:01 +0000304 SkSafeUnref(fSurface);
junov@chromium.org88e29142012-08-07 16:48:22 +0000305}
306
reed@google.com9c135db2014-03-12 18:28:35 +0000307void SkDeferredDevice::setMaxRecordingStorage(size_t maxStorage) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000308 fMaxRecordingStorageBytes = maxStorage;
309 this->recordingCanvas(); // Accessing the recording canvas applies the new limit.
310}
311
reed@google.com9c135db2014-03-12 18:28:35 +0000312void SkDeferredDevice::beginRecording() {
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000313 SkASSERT(NULL == fRecordingCanvas);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000314 fRecordingCanvas = fPipeWriter.startRecording(&fPipeController, 0,
junov@chromium.org9becf002013-04-15 18:15:23 +0000315 immediateDevice()->width(), immediateDevice()->height());
junov@chromium.org88e29142012-08-07 16:48:22 +0000316}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000317
reed@google.com9c135db2014-03-12 18:28:35 +0000318void SkDeferredDevice::setNotificationClient(
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000319 SkDeferredCanvas::NotificationClient* notificationClient) {
junov@chromium.org52805482012-08-20 14:25:04 +0000320 fNotificationClient = notificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000321}
322
reed@google.com9c135db2014-03-12 18:28:35 +0000323void SkDeferredDevice::skipPendingCommands() {
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000324 if (!fRecordingCanvas->isDrawingToLayer()) {
325 fCanDiscardCanvasContents = true;
326 if (fPipeController.hasPendingCommands()) {
327 fFreshFrame = true;
328 flushPendingCommands(kSilent_PlaybackMode);
junov@google.com52a00ca2012-10-01 15:27:14 +0000329 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000330 }
331}
332
reed@google.com9c135db2014-03-12 18:28:35 +0000333bool SkDeferredDevice::isFreshFrame() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000334 bool ret = fFreshFrame;
335 fFreshFrame = false;
336 return ret;
337}
338
reed@google.com9c135db2014-03-12 18:28:35 +0000339bool SkDeferredDevice::hasPendingCommands() {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000340 return fPipeController.hasPendingCommands();
341}
342
reed@google.com9c135db2014-03-12 18:28:35 +0000343void SkDeferredDevice::aboutToDraw()
junov@chromium.org44324fa2013-08-02 15:36:02 +0000344{
345 if (NULL != fNotificationClient) {
346 fNotificationClient->prepareForDraw();
347 }
348 if (fCanDiscardCanvasContents) {
349 if (NULL != fSurface) {
350 fSurface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
351 }
skia.committer@gmail.comea4b7972013-08-06 07:01:27 +0000352 fCanDiscardCanvasContents = false;
junov@chromium.org44324fa2013-08-02 15:36:02 +0000353 }
354}
355
reed@google.com9c135db2014-03-12 18:28:35 +0000356void SkDeferredDevice::flushPendingCommands(PlaybackMode playbackMode) {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000357 if (!fPipeController.hasPendingCommands()) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000358 return;
359 }
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000360 if (playbackMode == kNormal_PlaybackMode) {
junov@chromium.org44324fa2013-08-02 15:36:02 +0000361 aboutToDraw();
junov@chromium.org88e29142012-08-07 16:48:22 +0000362 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000363 fPipeWriter.flushRecording(true);
junov@chromium.orgd4501a02012-10-30 19:05:17 +0000364 fPipeController.playback(kSilent_PlaybackMode == playbackMode);
commit-bot@chromium.orgdad009b2014-03-27 15:48:52 +0000365 if (fNotificationClient) {
366 if (playbackMode == kSilent_PlaybackMode) {
367 fNotificationClient->skippedPendingDrawCommands();
368 } else {
369 fNotificationClient->flushedDrawCommands();
370 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000371 }
commit-bot@chromium.orgdad009b2014-03-27 15:48:52 +0000372
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000373 fPreviousStorageAllocated = storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000374}
375
reed@google.com9c135db2014-03-12 18:28:35 +0000376void SkDeferredDevice::flush() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000377 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000378 fImmediateCanvas->flush();
379}
380
reed@google.com9c135db2014-03-12 18:28:35 +0000381size_t SkDeferredDevice::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000382 size_t val = fPipeWriter.freeMemoryIfPossible(bytesToFree);
383 fPreviousStorageAllocated = storageAllocatedForRecording();
384 return val;
junov@chromium.org88e29142012-08-07 16:48:22 +0000385}
386
reed@google.com9c135db2014-03-12 18:28:35 +0000387size_t SkDeferredDevice::getBitmapSizeThreshold() const {
sugoi@google.com7775fd52012-11-21 15:47:04 +0000388 return fBitmapSizeThreshold;
389}
390
reed@google.com9c135db2014-03-12 18:28:35 +0000391void SkDeferredDevice::setBitmapSizeThreshold(size_t sizeThreshold) {
sugoi@google.com7775fd52012-11-21 15:47:04 +0000392 fBitmapSizeThreshold = sizeThreshold;
393}
394
reed@google.com9c135db2014-03-12 18:28:35 +0000395size_t SkDeferredDevice::storageAllocatedForRecording() const {
junov@chromium.org88e29142012-08-07 16:48:22 +0000396 return (fPipeController.storageAllocatedForRecording()
397 + fPipeWriter.storageAllocatedForRecording());
398}
399
reed@google.com9c135db2014-03-12 18:28:35 +0000400void SkDeferredDevice::recordedDrawCommand() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000401 size_t storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000402
junov@chromium.org88e29142012-08-07 16:48:22 +0000403 if (storageAllocated > fMaxRecordingStorageBytes) {
404 // First, attempt to reduce cache without flushing
405 size_t tryFree = storageAllocated - fMaxRecordingStorageBytes;
406 if (this->freeMemoryIfPossible(tryFree) < tryFree) {
407 // Flush is necessary to free more space.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000408 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000409 // Free as much as possible to avoid oscillating around fMaxRecordingStorageBytes
410 // which could cause a high flushing frequency.
bsalomon@google.com100abf42012-09-05 17:40:04 +0000411 this->freeMemoryIfPossible(~0U);
junov@chromium.org88e29142012-08-07 16:48:22 +0000412 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000413 storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000414 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000415
rmistry@google.comd6176b02012-08-23 18:14:13 +0000416 if (fNotificationClient &&
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000417 storageAllocated != fPreviousStorageAllocated) {
418 fPreviousStorageAllocated = storageAllocated;
419 fNotificationClient->storageAllocatedForRecordingChanged(storageAllocated);
420 }
421}
422
reed@google.com9c135db2014-03-12 18:28:35 +0000423SkCanvas* SkDeferredDevice::recordingCanvas() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000424 return fRecordingCanvas;
425}
426
reed@google.com9c135db2014-03-12 18:28:35 +0000427SkImage* SkDeferredDevice::newImageSnapshot() {
junov@chromium.org67d74222013-04-12 13:33:01 +0000428 this->flush();
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000429 return fSurface ? fSurface->newImageSnapshot() : NULL;
junov@chromium.org67d74222013-04-12 13:33:01 +0000430}
431
reed@google.com9c135db2014-03-12 18:28:35 +0000432int SkDeferredDevice::width() const {
junov@chromium.org9becf002013-04-15 18:15:23 +0000433 return immediateDevice()->width();
junov@chromium.org88e29142012-08-07 16:48:22 +0000434}
435
reed@google.com9c135db2014-03-12 18:28:35 +0000436int SkDeferredDevice::height() const {
junov@chromium.org9becf002013-04-15 18:15:23 +0000437 return immediateDevice()->height();
junov@chromium.org88e29142012-08-07 16:48:22 +0000438}
439
reeda6a8f002014-06-02 05:45:31 -0700440#ifdef SK_SUPPORT_LEGACY_DEVICE_CONFIG
reed@google.com9c135db2014-03-12 18:28:35 +0000441SkBitmap::Config SkDeferredDevice::config() const {
reed@google.com284a84d2014-02-14 15:23:15 +0000442 return immediateDevice()->config();
443}
reeda6a8f002014-06-02 05:45:31 -0700444#endif
reed@google.com284a84d2014-02-14 15:23:15 +0000445
reed@google.com9c135db2014-03-12 18:28:35 +0000446bool SkDeferredDevice::isOpaque() const {
reed@google.com284a84d2014-02-14 15:23:15 +0000447 return immediateDevice()->isOpaque();
448}
449
reed@google.com9c135db2014-03-12 18:28:35 +0000450SkImageInfo SkDeferredDevice::imageInfo() const {
reed@google.com284a84d2014-02-14 15:23:15 +0000451 return immediateDevice()->imageInfo();
452}
453
reed@google.com9c135db2014-03-12 18:28:35 +0000454GrRenderTarget* SkDeferredDevice::accessRenderTarget() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000455 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000456 return immediateDevice()->accessRenderTarget();
junov@chromium.org88e29142012-08-07 16:48:22 +0000457}
458
reed@google.com9c135db2014-03-12 18:28:35 +0000459void SkDeferredDevice::prepareForImmediatePixelWrite() {
junov@chromium.org44324fa2013-08-02 15:36:02 +0000460 // The purpose of the following code is to make sure commands are flushed, that
461 // aboutToDraw() is called and that notifyContentWillChange is called, without
462 // calling anything redundantly.
463 if (fPipeController.hasPendingCommands()) {
464 this->flushPendingCommands(kNormal_PlaybackMode);
465 } else {
466 bool mustNotifyDirectly = !fCanDiscardCanvasContents;
467 this->aboutToDraw();
468 if (mustNotifyDirectly) {
469 fSurface->notifyContentWillChange(SkSurface::kRetain_ContentChangeMode);
470 }
471 }
472
473 fImmediateCanvas->flush();
474}
475
reed@google.com9c135db2014-03-12 18:28:35 +0000476bool SkDeferredDevice::onWritePixels(const SkImageInfo& info, const void* pixels, size_t rowBytes,
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000477 int x, int y) {
478 SkASSERT(x >= 0 && y >= 0);
479 SkASSERT(x + info.width() <= width());
480 SkASSERT(y + info.height() <= height());
481
482 this->flushPendingCommands(kNormal_PlaybackMode);
483
484 const SkImageInfo deviceInfo = this->imageInfo();
485 if (info.width() == deviceInfo.width() && info.height() == deviceInfo.height()) {
486 this->skipPendingCommands();
487 }
skia.committer@gmail.come62513f2014-03-08 03:02:06 +0000488
commit-bot@chromium.org4cd9e212014-03-07 03:25:16 +0000489 this->prepareForImmediatePixelWrite();
490 return immediateDevice()->onWritePixels(info, pixels, rowBytes, x, y);
491}
junov@chromium.org88e29142012-08-07 16:48:22 +0000492
reed@google.com9c135db2014-03-12 18:28:35 +0000493const SkBitmap& SkDeferredDevice::onAccessBitmap() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000494 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000495 return immediateDevice()->accessBitmap(false);
junov@chromium.org88e29142012-08-07 16:48:22 +0000496}
497
reed@google.com9c135db2014-03-12 18:28:35 +0000498SkBaseDevice* SkDeferredDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000499 // Save layer usage not supported, and not required by SkDeferredCanvas.
500 SkASSERT(usage != kSaveLayer_Usage);
501 // Create a compatible non-deferred device.
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000502 // We do not create a deferred device because we know the new device
503 // will not be used with a deferred canvas (there is no API for that).
reed@google.com9c135db2014-03-12 18:28:35 +0000504 // And connecting a SkDeferredDevice to non-deferred canvas can result
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000505 // in unpredictable behavior.
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000506 return immediateDevice()->createCompatibleDevice(info);
junov@chromium.org88e29142012-08-07 16:48:22 +0000507}
508
reed@google.com9c135db2014-03-12 18:28:35 +0000509SkSurface* SkDeferredDevice::newSurface(const SkImageInfo& info) {
reed@google.com76f10a32014-02-05 15:32:21 +0000510 return this->immediateDevice()->newSurface(info);
511}
512
commit-bot@chromium.orga713f9c2014-03-17 21:31:26 +0000513bool SkDeferredDevice::onReadPixels(const SkImageInfo& info, void* pixels, size_t rowBytes,
514 int x, int y) {
515 this->flushPendingCommands(kNormal_PlaybackMode);
516 return fImmediateCanvas->readPixels(info, pixels, rowBytes, x, y);
517}
junov@chromium.org88e29142012-08-07 16:48:22 +0000518
sugoi@google.com7775fd52012-11-21 15:47:04 +0000519class AutoImmediateDrawIfNeeded {
520public:
521 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkBitmap* bitmap,
522 const SkPaint* paint) {
523 this->init(canvas, bitmap, paint);
524 }
525
526 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkPaint* paint) {
527 this->init(canvas, NULL, paint);
528 }
529
530 ~AutoImmediateDrawIfNeeded() {
531 if (fCanvas) {
532 fCanvas->setDeferredDrawing(true);
533 }
534 }
535private:
536 void init(SkDeferredCanvas& canvas, const SkBitmap* bitmap, const SkPaint* paint)
537 {
reed@google.com9c135db2014-03-12 18:28:35 +0000538 SkDeferredDevice* device = static_cast<SkDeferredDevice*>(canvas.getDevice());
sugoi@google.com7775fd52012-11-21 15:47:04 +0000539 if (canvas.isDeferredDrawing() && (NULL != device) &&
540 shouldDrawImmediately(bitmap, paint, device->getBitmapSizeThreshold())) {
541 canvas.setDeferredDrawing(false);
542 fCanvas = &canvas;
543 } else {
544 fCanvas = NULL;
545 }
546 }
547
548 SkDeferredCanvas* fCanvas;
549};
junov@chromium.org88e29142012-08-07 16:48:22 +0000550
junov@chromium.org66070a52013-05-28 17:39:08 +0000551SkDeferredCanvas* SkDeferredCanvas::Create(SkSurface* surface) {
reed@google.com9c135db2014-03-12 18:28:35 +0000552 SkAutoTUnref<SkDeferredDevice> deferredDevice(SkNEW_ARGS(SkDeferredDevice, (surface)));
junov@chromium.org66070a52013-05-28 17:39:08 +0000553 return SkNEW_ARGS(SkDeferredCanvas, (deferredDevice));
554}
555
reed@google.com9c135db2014-03-12 18:28:35 +0000556SkDeferredCanvas::SkDeferredCanvas(SkDeferredDevice* device) : SkCanvas (device) {
junov@chromium.org66070a52013-05-28 17:39:08 +0000557 this->init();
558}
junov@chromium.org67d74222013-04-12 13:33:01 +0000559
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000560void SkDeferredCanvas::init() {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000561 fDeferredDrawing = true; // On by default
junov@google.com4370aed2012-01-18 16:21:08 +0000562}
563
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000564void SkDeferredCanvas::setMaxRecordingStorage(size_t maxStorage) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000565 this->validate();
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000566 this->getDeferredDevice()->setMaxRecordingStorage(maxStorage);
567}
568
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000569size_t SkDeferredCanvas::storageAllocatedForRecording() const {
570 return this->getDeferredDevice()->storageAllocatedForRecording();
571}
572
573size_t SkDeferredCanvas::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000574 return this->getDeferredDevice()->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000575}
576
sugoi@google.com7775fd52012-11-21 15:47:04 +0000577void SkDeferredCanvas::setBitmapSizeThreshold(size_t sizeThreshold) {
reed@google.com9c135db2014-03-12 18:28:35 +0000578 SkDeferredDevice* deferredDevice = this->getDeferredDevice();
sugoi@google.com7775fd52012-11-21 15:47:04 +0000579 SkASSERT(deferredDevice);
580 deferredDevice->setBitmapSizeThreshold(sizeThreshold);
581}
582
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000583void SkDeferredCanvas::recordedDrawCommand() {
584 if (fDeferredDrawing) {
585 this->getDeferredDevice()->recordedDrawCommand();
586 }
587}
588
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000589void SkDeferredCanvas::validate() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000590 SkASSERT(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000591}
592
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000593SkCanvas* SkDeferredCanvas::drawingCanvas() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000594 this->validate();
595 return fDeferredDrawing ? this->getDeferredDevice()->recordingCanvas() :
596 this->getDeferredDevice()->immediateCanvas();
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000597}
598
junov@chromium.org88e29142012-08-07 16:48:22 +0000599SkCanvas* SkDeferredCanvas::immediateCanvas() const {
600 this->validate();
601 return this->getDeferredDevice()->immediateCanvas();
602}
603
reed@google.com9c135db2014-03-12 18:28:35 +0000604SkDeferredDevice* SkDeferredCanvas::getDeferredDevice() const {
605 return static_cast<SkDeferredDevice*>(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000606}
607
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000608void SkDeferredCanvas::setDeferredDrawing(bool val) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000609 this->validate(); // Must set device before calling this method
junov@google.com4370aed2012-01-18 16:21:08 +0000610 if (val != fDeferredDrawing) {
611 if (fDeferredDrawing) {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000612 // Going live.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000613 this->getDeferredDevice()->flushPendingCommands(kNormal_PlaybackMode);
junov@google.com4370aed2012-01-18 16:21:08 +0000614 }
615 fDeferredDrawing = val;
616 }
617}
618
junov@chromium.org88e29142012-08-07 16:48:22 +0000619bool SkDeferredCanvas::isDeferredDrawing() const {
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +0000620 return fDeferredDrawing;
621}
622
junov@chromium.org88e29142012-08-07 16:48:22 +0000623bool SkDeferredCanvas::isFreshFrame() const {
624 return this->getDeferredDevice()->isFreshFrame();
625}
626
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000627bool SkDeferredCanvas::hasPendingCommands() const {
628 return this->getDeferredDevice()->hasPendingCommands();
629}
630
junov@chromium.orgfb103892012-09-20 19:35:43 +0000631void SkDeferredCanvas::silentFlush() {
632 if (fDeferredDrawing) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000633 this->getDeferredDevice()->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000634 }
635}
636
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000637SkDeferredCanvas::~SkDeferredCanvas() {
junov@google.com4370aed2012-01-18 16:21:08 +0000638}
639
junov@chromium.org7070f762013-05-24 17:13:00 +0000640SkSurface* SkDeferredCanvas::setSurface(SkSurface* surface) {
reed@google.com9c135db2014-03-12 18:28:35 +0000641 SkDeferredDevice* deferredDevice = this->getDeferredDevice();
junov@chromium.org66070a52013-05-28 17:39:08 +0000642 SkASSERT(NULL != deferredDevice);
643 // By swapping the surface into the existing device, we preserve
644 // all pending commands, which can help to seamlessly recover from
645 // a lost accelerated graphics context.
646 deferredDevice->setSurface(surface);
junov@chromium.org7070f762013-05-24 17:13:00 +0000647 return surface;
648}
649
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000650SkDeferredCanvas::NotificationClient* SkDeferredCanvas::setNotificationClient(
651 NotificationClient* notificationClient) {
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000652
reed@google.com9c135db2014-03-12 18:28:35 +0000653 SkDeferredDevice* deferredDevice = this->getDeferredDevice();
junov@google.com4370aed2012-01-18 16:21:08 +0000654 SkASSERT(deferredDevice);
655 if (deferredDevice) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000656 deferredDevice->setNotificationClient(notificationClient);
junov@google.com4370aed2012-01-18 16:21:08 +0000657 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000658 return notificationClient;
junov@google.com4370aed2012-01-18 16:21:08 +0000659}
660
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000661SkImage* SkDeferredCanvas::newImageSnapshot() {
reed@google.com9c135db2014-03-12 18:28:35 +0000662 SkDeferredDevice* deferredDevice = this->getDeferredDevice();
junov@chromium.org67d74222013-04-12 13:33:01 +0000663 SkASSERT(deferredDevice);
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000664 return deferredDevice ? deferredDevice->newImageSnapshot() : NULL;
junov@chromium.org67d74222013-04-12 13:33:01 +0000665}
666
junov@google.com4370aed2012-01-18 16:21:08 +0000667bool SkDeferredCanvas::isFullFrame(const SkRect* rect,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000668 const SkPaint* paint) const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000669 SkCanvas* canvas = this->drawingCanvas();
670 SkISize canvasSize = this->getDeviceSize();
junov@google.com4370aed2012-01-18 16:21:08 +0000671 if (rect) {
672 if (!canvas->getTotalMatrix().rectStaysRect()) {
673 return false; // conservative
674 }
675
676 SkRect transformedRect;
677 canvas->getTotalMatrix().mapRect(&transformedRect, *rect);
678
679 if (paint) {
680 SkPaint::Style paintStyle = paint->getStyle();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000681 if (!(paintStyle == SkPaint::kFill_Style ||
junov@google.com4370aed2012-01-18 16:21:08 +0000682 paintStyle == SkPaint::kStrokeAndFill_Style)) {
683 return false;
684 }
685 if (paint->getMaskFilter() || paint->getLooper()
686 || paint->getPathEffect() || paint->getImageFilter()) {
687 return false; // conservative
688 }
689 }
690
691 // The following test holds with AA enabled, and is conservative
692 // by a 0.5 pixel margin with AA disabled
rmistry@google.comd6176b02012-08-23 18:14:13 +0000693 if (transformedRect.fLeft > SkIntToScalar(0) ||
694 transformedRect.fTop > SkIntToScalar(0) ||
junov@chromium.orgb1e218e2012-02-13 22:27:58 +0000695 transformedRect.fRight < SkIntToScalar(canvasSize.fWidth) ||
696 transformedRect.fBottom < SkIntToScalar(canvasSize.fHeight)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000697 return false;
698 }
699 }
700
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000701 return this->getClipStack()->quickContains(SkRect::MakeXYWH(0, 0,
702 SkIntToScalar(canvasSize.fWidth), SkIntToScalar(canvasSize.fHeight)));
junov@google.com4370aed2012-01-18 16:21:08 +0000703}
704
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000705void SkDeferredCanvas::willSave(SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000706 this->drawingCanvas()->save(flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000707 this->recordedDrawCommand();
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000708 this->INHERITED::willSave(flags);
junov@google.com4370aed2012-01-18 16:21:08 +0000709}
710
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000711SkCanvas::SaveLayerStrategy SkDeferredCanvas::willSaveLayer(const SkRect* bounds,
712 const SkPaint* paint, SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000713 this->drawingCanvas()->saveLayer(bounds, paint, flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000714 this->recordedDrawCommand();
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000715 this->INHERITED::willSaveLayer(bounds, paint, flags);
716 // No need for a full layer.
717 return kNoLayer_SaveLayerStrategy;
junov@google.com4370aed2012-01-18 16:21:08 +0000718}
719
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000720void SkDeferredCanvas::willRestore() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000721 this->drawingCanvas()->restore();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000722 this->recordedDrawCommand();
commit-bot@chromium.orge54a23f2014-03-12 20:21:48 +0000723 this->INHERITED::willRestore();
junov@google.com4370aed2012-01-18 16:21:08 +0000724}
725
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000726bool SkDeferredCanvas::isDrawingToLayer() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000727 return this->drawingCanvas()->isDrawingToLayer();
junov@google.com4370aed2012-01-18 16:21:08 +0000728}
729
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000730void SkDeferredCanvas::didConcat(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000731 this->drawingCanvas()->concat(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000732 this->recordedDrawCommand();
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000733 this->INHERITED::didConcat(matrix);
junov@google.com4370aed2012-01-18 16:21:08 +0000734}
735
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000736void SkDeferredCanvas::didSetMatrix(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000737 this->drawingCanvas()->setMatrix(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000738 this->recordedDrawCommand();
commit-bot@chromium.org44c48d02014-03-13 20:03:58 +0000739 this->INHERITED::didSetMatrix(matrix);
junov@google.com4370aed2012-01-18 16:21:08 +0000740}
741
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000742void SkDeferredCanvas::onClipRect(const SkRect& rect,
743 SkRegion::Op op,
744 ClipEdgeStyle edgeStyle) {
745 this->drawingCanvas()->clipRect(rect, op, kSoft_ClipEdgeStyle == edgeStyle);
746 this->INHERITED::onClipRect(rect, op, edgeStyle);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000747 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000748}
749
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000750void SkDeferredCanvas::onClipRRect(const SkRRect& rrect,
751 SkRegion::Op op,
752 ClipEdgeStyle edgeStyle) {
753 this->drawingCanvas()->clipRRect(rrect, op, kSoft_ClipEdgeStyle == edgeStyle);
754 this->INHERITED::onClipRRect(rrect, op, edgeStyle);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000755 this->recordedDrawCommand();
reed@google.com4ed0fb72012-12-12 20:48:18 +0000756}
757
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000758void SkDeferredCanvas::onClipPath(const SkPath& path,
759 SkRegion::Op op,
760 ClipEdgeStyle edgeStyle) {
761 this->drawingCanvas()->clipPath(path, op, kSoft_ClipEdgeStyle == edgeStyle);
762 this->INHERITED::onClipPath(path, op, edgeStyle);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000763 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000764}
765
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000766void SkDeferredCanvas::onClipRegion(const SkRegion& deviceRgn, SkRegion::Op op) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000767 this->drawingCanvas()->clipRegion(deviceRgn, op);
robertphillips@google.com8f90a892014-02-28 18:19:39 +0000768 this->INHERITED::onClipRegion(deviceRgn, op);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000769 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000770}
771
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000772void SkDeferredCanvas::clear(SkColor color) {
junov@google.com4370aed2012-01-18 16:21:08 +0000773 // purge pending commands
774 if (fDeferredDrawing) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000775 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000776 }
777
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000778 this->drawingCanvas()->clear(color);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000779 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000780}
781
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000782void SkDeferredCanvas::drawPaint(const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000783 if (fDeferredDrawing && this->isFullFrame(NULL, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000784 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000785 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +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()->drawPaint(paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000789 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000790}
791
792void SkDeferredCanvas::drawPoints(PointMode mode, size_t count,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000793 const SkPoint pts[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000794 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000795 this->drawingCanvas()->drawPoints(mode, count, pts, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000796 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000797}
798
reed@google.com4ed0fb72012-12-12 20:48:18 +0000799void SkDeferredCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
800 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
801 this->drawingCanvas()->drawOval(rect, paint);
802 this->recordedDrawCommand();
803}
804
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000805void SkDeferredCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000806 if (fDeferredDrawing && this->isFullFrame(&rect, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000807 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000808 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000809 }
skia.committer@gmail.com306ab9d2012-12-13 02:01:33 +0000810
junov@chromium.org10f7f972012-07-31 21:01:51 +0000811 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000812 this->drawingCanvas()->drawRect(rect, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000813 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000814}
815
reed@google.com4ed0fb72012-12-12 20:48:18 +0000816void SkDeferredCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
817 if (rrect.isRect()) {
818 this->SkDeferredCanvas::drawRect(rrect.getBounds(), paint);
819 } else if (rrect.isOval()) {
820 this->SkDeferredCanvas::drawOval(rrect.getBounds(), paint);
821 } else {
822 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
823 this->drawingCanvas()->drawRRect(rrect, paint);
824 this->recordedDrawCommand();
825 }
826}
827
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000828void SkDeferredCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
829 const SkPaint& paint) {
830 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
831 this->drawingCanvas()->drawDRRect(outer, inner, paint);
832 this->recordedDrawCommand();
833}
834
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000835void SkDeferredCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000836 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000837 this->drawingCanvas()->drawPath(path, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000838 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000839}
840
841void SkDeferredCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000842 SkScalar top, const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000843 SkRect bitmapRect = SkRect::MakeXYWH(left, top,
844 SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000845 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000846 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000847 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000848 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000849 }
850
junov@chromium.org10f7f972012-07-31 21:01:51 +0000851 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000852 this->drawingCanvas()->drawBitmap(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000853 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000854}
855
reed@google.com71121732012-09-18 15:14:33 +0000856void SkDeferredCanvas::drawBitmapRectToRect(const SkBitmap& bitmap,
857 const SkRect* src,
858 const SkRect& dst,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000859 const SkPaint* paint,
860 DrawBitmapRectFlags flags) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000861 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000862 this->isFullFrame(&dst, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000863 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000864 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000865 }
866
junov@chromium.org10f7f972012-07-31 21:01:51 +0000867 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000868 this->drawingCanvas()->drawBitmapRectToRect(bitmap, src, dst, paint, flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000869 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000870}
871
872
873void SkDeferredCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
874 const SkMatrix& m,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000875 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000876 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
877 // covers canvas entirely and transformed bitmap covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000878 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000879 this->drawingCanvas()->drawBitmapMatrix(bitmap, m, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000880 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000881}
882
883void SkDeferredCanvas::drawBitmapNine(const SkBitmap& bitmap,
884 const SkIRect& center, const SkRect& dst,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000885 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000886 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
887 // covers canvas entirely and dst covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000888 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000889 this->drawingCanvas()->drawBitmapNine(bitmap, center, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000890 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000891}
892
893void SkDeferredCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000894 const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000895 SkRect bitmapRect = SkRect::MakeXYWH(
896 SkIntToScalar(left),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000897 SkIntToScalar(top),
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000898 SkIntToScalar(bitmap.width()),
899 SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000900 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000901 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000902 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000903 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000904 }
905
junov@chromium.org10f7f972012-07-31 21:01:51 +0000906 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000907 this->drawingCanvas()->drawSprite(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000908 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000909}
910
reed@google.come0d9ce82014-04-23 04:00:17 +0000911void SkDeferredCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y,
912 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000913 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000914 this->drawingCanvas()->drawText(text, byteLength, x, y, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000915 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000916}
917
reed@google.come0d9ce82014-04-23 04:00:17 +0000918void SkDeferredCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[],
919 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000920 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000921 this->drawingCanvas()->drawPosText(text, byteLength, pos, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000922 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000923}
924
reed@google.come0d9ce82014-04-23 04:00:17 +0000925void SkDeferredCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[],
926 SkScalar constY, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000927 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000928 this->drawingCanvas()->drawPosTextH(text, byteLength, xpos, constY, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000929 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000930}
931
reed@google.come0d9ce82014-04-23 04:00:17 +0000932void SkDeferredCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path,
933 const SkMatrix* matrix, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000934 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000935 this->drawingCanvas()->drawTextOnPath(text, byteLength, path, matrix, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000936 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000937}
938
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000939void SkDeferredCanvas::drawPicture(SkPicture& picture) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000940 this->drawingCanvas()->drawPicture(picture);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000941 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000942}
943
944void SkDeferredCanvas::drawVertices(VertexMode vmode, int vertexCount,
945 const SkPoint vertices[],
946 const SkPoint texs[],
947 const SkColor colors[], SkXfermode* xmode,
948 const uint16_t indices[], int indexCount,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000949 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000950 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000951 this->drawingCanvas()->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode,
952 indices, indexCount, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000953 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000954}
955
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000956SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000957 this->drawingCanvas()->setDrawFilter(filter);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000958 this->INHERITED::setDrawFilter(filter);
959 this->recordedDrawCommand();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000960 return filter;
junov@google.com4370aed2012-01-18 16:21:08 +0000961}
962
963SkCanvas* SkDeferredCanvas::canvasForDrawIter() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000964 return this->drawingCanvas();
junov@google.com4370aed2012-01-18 16:21:08 +0000965}