blob: 3911fd0dfa373636bd3f587231142a720c9dcfa8 [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
junov@google.com4370aed2012-01-18 16:21:08 +000033namespace {
sugoi@google.com7775fd52012-11-21 15:47:04 +000034bool shouldDrawImmediately(const SkBitmap* bitmap, const SkPaint* paint,
35 size_t bitmapSizeThreshold) {
36 if (bitmap && ((bitmap->getTexture() && !bitmap->isImmutable()) ||
37 (bitmap->getSize() > bitmapSizeThreshold))) {
junov@chromium.org10f7f972012-07-31 21:01:51 +000038 return true;
39 }
40 if (paint) {
41 SkShader* shader = paint->getShader();
42 // Here we detect the case where the shader is an SkBitmapProcShader
43 // with a gpu texture attached. Checking this without RTTI
44 // requires making the assumption that only gradient shaders
45 // and SkBitmapProcShader implement asABitmap(). The following
46 // code may need to be revised if that assumption is ever broken.
47 if (shader && !shader->asAGradient(NULL)) {
48 SkBitmap bm;
rmistry@google.comd6176b02012-08-23 18:14:13 +000049 if (shader->asABitmap(&bm, NULL, NULL) &&
junov@chromium.org10f7f972012-07-31 21:01:51 +000050 NULL != bm.getTexture()) {
51 return true;
52 }
53 }
54 }
55 return false;
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +000056}
57}
58
junov@chromium.org88e29142012-08-07 16:48:22 +000059//-----------------------------------------------------------------------------
60// DeferredPipeController
61//-----------------------------------------------------------------------------
62
63class DeferredPipeController : public SkGPipeController {
64public:
65 DeferredPipeController();
66 void setPlaybackCanvas(SkCanvas*);
67 virtual ~DeferredPipeController();
68 virtual void* requestBlock(size_t minRequest, size_t* actual) SK_OVERRIDE;
69 virtual void notifyWritten(size_t bytes) SK_OVERRIDE;
junov@chromium.orgfb103892012-09-20 19:35:43 +000070 void playback(bool silent);
junov@chromium.orga38dfb62012-09-20 22:10:33 +000071 bool hasPendingCommands() const { return fAllocator.blockCount() != 0; }
junov@chromium.org88e29142012-08-07 16:48:22 +000072 size_t storageAllocatedForRecording() const { return fAllocator.totalCapacity(); }
73private:
74 enum {
75 kMinBlockSize = 4096
76 };
77 struct PipeBlock {
78 PipeBlock(void* block, size_t size) { fBlock = block, fSize = size; }
79 void* fBlock;
80 size_t fSize;
81 };
82 void* fBlock;
83 size_t fBytesWritten;
84 SkChunkAlloc fAllocator;
85 SkTDArray<PipeBlock> fBlockList;
86 SkGPipeReader fReader;
87};
88
89DeferredPipeController::DeferredPipeController() :
90 fAllocator(kMinBlockSize) {
91 fBlock = NULL;
92 fBytesWritten = 0;
93}
94
95DeferredPipeController::~DeferredPipeController() {
96 fAllocator.reset();
97}
98
99void DeferredPipeController::setPlaybackCanvas(SkCanvas* canvas) {
100 fReader.setCanvas(canvas);
101}
102
103void* DeferredPipeController::requestBlock(size_t minRequest, size_t *actual) {
104 if (fBlock) {
105 // Save the previous block for later
106 PipeBlock previousBloc(fBlock, fBytesWritten);
107 fBlockList.push(previousBloc);
108 }
robertphillips@google.comadacc702013-10-14 21:53:24 +0000109 size_t blockSize = SkTMax<size_t>(minRequest, kMinBlockSize);
junov@chromium.org88e29142012-08-07 16:48:22 +0000110 fBlock = fAllocator.allocThrow(blockSize);
111 fBytesWritten = 0;
112 *actual = blockSize;
113 return fBlock;
114}
115
116void DeferredPipeController::notifyWritten(size_t bytes) {
117 fBytesWritten += bytes;
118}
119
junov@chromium.orgfb103892012-09-20 19:35:43 +0000120void DeferredPipeController::playback(bool silent) {
121 uint32_t flags = silent ? SkGPipeReader::kSilent_PlaybackFlag : 0;
junov@chromium.org88e29142012-08-07 16:48:22 +0000122 for (int currentBlock = 0; currentBlock < fBlockList.count(); currentBlock++ ) {
junov@chromium.orgfb103892012-09-20 19:35:43 +0000123 fReader.playback(fBlockList[currentBlock].fBlock, fBlockList[currentBlock].fSize,
124 flags);
junov@chromium.org88e29142012-08-07 16:48:22 +0000125 }
126 fBlockList.reset();
127
128 if (fBlock) {
junov@chromium.orgfb103892012-09-20 19:35:43 +0000129 fReader.playback(fBlock, fBytesWritten, flags);
junov@chromium.org88e29142012-08-07 16:48:22 +0000130 fBlock = NULL;
131 }
132
133 // Release all allocated blocks
134 fAllocator.reset();
135}
136
junov@chromium.org88e29142012-08-07 16:48:22 +0000137//-----------------------------------------------------------------------------
138// DeferredDevice
139//-----------------------------------------------------------------------------
reed@google.com284a84d2014-02-14 15:23:15 +0000140class DeferredDevice : public SkBaseDevice {
junov@chromium.org88e29142012-08-07 16:48:22 +0000141public:
junov@chromium.org67d74222013-04-12 13:33:01 +0000142 explicit DeferredDevice(SkSurface* surface);
junov@chromium.org88e29142012-08-07 16:48:22 +0000143 ~DeferredDevice();
144
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000145 void setNotificationClient(SkDeferredCanvas::NotificationClient* notificationClient);
junov@chromium.org88e29142012-08-07 16:48:22 +0000146 SkCanvas* recordingCanvas();
147 SkCanvas* immediateCanvas() const {return fImmediateCanvas;}
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000148 SkBaseDevice* immediateDevice() const {return fImmediateCanvas->getTopDevice();}
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000149 SkImage* newImageSnapshot();
junov@chromium.org7070f762013-05-24 17:13:00 +0000150 void setSurface(SkSurface* surface);
junov@chromium.org88e29142012-08-07 16:48:22 +0000151 bool isFreshFrame();
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000152 bool hasPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000153 size_t storageAllocatedForRecording() const;
154 size_t freeMemoryIfPossible(size_t bytesToFree);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000155 size_t getBitmapSizeThreshold() const;
156 void setBitmapSizeThreshold(size_t sizeThreshold);
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000157 void flushPendingCommands(PlaybackMode);
junov@chromium.org0a67f962012-09-19 22:48:34 +0000158 void skipPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000159 void setMaxRecordingStorage(size_t);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000160 void recordedDrawCommand();
junov@chromium.org88e29142012-08-07 16:48:22 +0000161
162 virtual uint32_t getDeviceCapabilities() SK_OVERRIDE;
163 virtual int width() const SK_OVERRIDE;
164 virtual int height() const SK_OVERRIDE;
reed@google.com284a84d2014-02-14 15:23:15 +0000165 virtual SkBitmap::Config config() const SK_OVERRIDE;
166 virtual bool isOpaque() const SK_OVERRIDE;
167 virtual SkImageInfo imageInfo() const SK_OVERRIDE;
168
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000169 virtual GrRenderTarget* accessRenderTarget() SK_OVERRIDE;
junov@chromium.org88e29142012-08-07 16:48:22 +0000170
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000171 virtual SkBaseDevice* onCreateDevice(const SkImageInfo&, Usage) SK_OVERRIDE;
junov@chromium.org88e29142012-08-07 16:48:22 +0000172
173 virtual void writePixels(const SkBitmap& bitmap, int x, int y,
174 SkCanvas::Config8888 config8888) SK_OVERRIDE;
175
reed@google.com76f10a32014-02-05 15:32:21 +0000176 virtual SkSurface* newSurface(const SkImageInfo&) SK_OVERRIDE;
177
junov@chromium.org88e29142012-08-07 16:48:22 +0000178protected:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000179 virtual const SkBitmap& onAccessBitmap() SK_OVERRIDE;
junov@chromium.org88e29142012-08-07 16:48:22 +0000180 virtual bool onReadPixels(const SkBitmap& bitmap,
181 int x, int y,
182 SkCanvas::Config8888 config8888) SK_OVERRIDE;
183
184 // The following methods are no-ops on a deferred device
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000185 virtual bool filterTextFlags(const SkPaint& paint, TextFlags*) SK_OVERRIDE {
186 return false;
187 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000188
189 // None of the following drawing methods should ever get called on the
190 // deferred device
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000191 virtual void clear(SkColor color) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000192 {SkASSERT(0);}
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000193 virtual void drawPaint(const SkDraw&, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000194 {SkASSERT(0);}
195 virtual void drawPoints(const SkDraw&, SkCanvas::PointMode mode,
196 size_t count, const SkPoint[],
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000197 const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000198 {SkASSERT(0);}
199 virtual void drawRect(const SkDraw&, const SkRect& r,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000200 const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000201 {SkASSERT(0);}
reed@google.com284a84d2014-02-14 15:23:15 +0000202 virtual void drawOval(const SkDraw&, const SkRect&, const SkPaint&) SK_OVERRIDE
203 {SkASSERT(0);}
scroggo@google.comcac8d012013-11-12 17:10:02 +0000204 virtual void drawRRect(const SkDraw&, const SkRRect& rr,
205 const SkPaint& paint) SK_OVERRIDE
reed@google.com284a84d2014-02-14 15:23:15 +0000206 {SkASSERT(0);}
junov@chromium.org88e29142012-08-07 16:48:22 +0000207 virtual void drawPath(const SkDraw&, const SkPath& path,
208 const SkPaint& paint,
209 const SkMatrix* prePathMatrix = NULL,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000210 bool pathIsMutable = false) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000211 {SkASSERT(0);}
212 virtual void drawBitmap(const SkDraw&, const SkBitmap& bitmap,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000213 const SkMatrix& matrix, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000214 {SkASSERT(0);}
reed@google.com284a84d2014-02-14 15:23:15 +0000215 virtual void drawBitmapRect(const SkDraw&, const SkBitmap&, const SkRect*,
216 const SkRect&, const SkPaint&,
217 SkCanvas::DrawBitmapRectFlags) SK_OVERRIDE
218 {SkASSERT(0);}
junov@chromium.org88e29142012-08-07 16:48:22 +0000219 virtual void drawSprite(const SkDraw&, const SkBitmap& bitmap,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000220 int x, int y, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000221 {SkASSERT(0);}
222 virtual void drawText(const SkDraw&, const void* text, size_t len,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000223 SkScalar x, SkScalar y, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000224 {SkASSERT(0);}
225 virtual void drawPosText(const SkDraw&, const void* text, size_t len,
226 const SkScalar pos[], SkScalar constY,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000227 int scalarsPerPos, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000228 {SkASSERT(0);}
229 virtual void drawTextOnPath(const SkDraw&, const void* text,
230 size_t len, const SkPath& path,
231 const SkMatrix* matrix,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000232 const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000233 {SkASSERT(0);}
junov@chromium.org88e29142012-08-07 16:48:22 +0000234 virtual void drawVertices(const SkDraw&, SkCanvas::VertexMode,
235 int vertexCount, const SkPoint verts[],
236 const SkPoint texs[], const SkColor colors[],
237 SkXfermode* xmode, const uint16_t indices[],
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000238 int indexCount, const SkPaint& paint) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000239 {SkASSERT(0);}
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000240 virtual void drawDevice(const SkDraw&, SkBaseDevice*, int x, int y,
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000241 const SkPaint&) SK_OVERRIDE
junov@chromium.org88e29142012-08-07 16:48:22 +0000242 {SkASSERT(0);}
reed@google.com284a84d2014-02-14 15:23:15 +0000243
244 virtual void lockPixels() SK_OVERRIDE {}
245 virtual void unlockPixels() SK_OVERRIDE {}
246
247 virtual bool allowImageFilter(const SkImageFilter*) SK_OVERRIDE {
248 return false;
249 }
250 virtual bool canHandleImageFilter(const SkImageFilter*) SK_OVERRIDE {
251 return false;
252 }
253 virtual bool filterImage(const SkImageFilter*, const SkBitmap&,
254 const SkMatrix&, SkBitmap*, SkIPoint*) SK_OVERRIDE {
255 return false;
256 }
257
junov@chromium.org88e29142012-08-07 16:48:22 +0000258private:
commit-bot@chromium.org3e2ea252013-07-23 11:28:45 +0000259 virtual void flush() SK_OVERRIDE;
reed@google.com284a84d2014-02-14 15:23:15 +0000260 virtual void replaceBitmapBackendForRasterSurface(const SkBitmap&) SK_OVERRIDE {}
junov@chromium.org88e29142012-08-07 16:48:22 +0000261
junov@chromium.org88e29142012-08-07 16:48:22 +0000262 void beginRecording();
junov@chromium.org67d74222013-04-12 13:33:01 +0000263 void init();
junov@chromium.org44324fa2013-08-02 15:36:02 +0000264 void aboutToDraw();
265 void prepareForImmediatePixelWrite();
junov@chromium.org88e29142012-08-07 16:48:22 +0000266
267 DeferredPipeController fPipeController;
268 SkGPipeWriter fPipeWriter;
junov@chromium.org88e29142012-08-07 16:48:22 +0000269 SkCanvas* fImmediateCanvas;
270 SkCanvas* fRecordingCanvas;
junov@chromium.org67d74222013-04-12 13:33:01 +0000271 SkSurface* fSurface;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000272 SkDeferredCanvas::NotificationClient* fNotificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000273 bool fFreshFrame;
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000274 bool fCanDiscardCanvasContents;
junov@chromium.org88e29142012-08-07 16:48:22 +0000275 size_t fMaxRecordingStorageBytes;
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000276 size_t fPreviousStorageAllocated;
sugoi@google.com7775fd52012-11-21 15:47:04 +0000277 size_t fBitmapSizeThreshold;
junov@chromium.org88e29142012-08-07 16:48:22 +0000278};
279
reed@google.com284a84d2014-02-14 15:23:15 +0000280DeferredDevice::DeferredDevice(SkSurface* surface) {
junov@chromium.org67d74222013-04-12 13:33:01 +0000281 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
282 fNotificationClient = NULL;
junov@chromium.org7070f762013-05-24 17:13:00 +0000283 fImmediateCanvas = NULL;
284 fSurface = NULL;
285 this->setSurface(surface);
junov@chromium.org67d74222013-04-12 13:33:01 +0000286 this->init();
287}
288
junov@chromium.org7070f762013-05-24 17:13:00 +0000289void DeferredDevice::setSurface(SkSurface* surface) {
290 SkRefCnt_SafeAssign(fImmediateCanvas, surface->getCanvas());
291 SkRefCnt_SafeAssign(fSurface, surface);
292 fPipeController.setPlaybackCanvas(fImmediateCanvas);
293}
294
junov@chromium.org67d74222013-04-12 13:33:01 +0000295void DeferredDevice::init() {
296 fRecordingCanvas = NULL;
297 fFreshFrame = true;
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000298 fCanDiscardCanvasContents = false;
junov@chromium.org67d74222013-04-12 13:33:01 +0000299 fPreviousStorageAllocated = 0;
300 fBitmapSizeThreshold = kDeferredCanvasBitmapSizeThreshold;
301 fMaxRecordingStorageBytes = kDefaultMaxRecordingStorageBytes;
302 fNotificationClient = NULL;
junov@chromium.org88e29142012-08-07 16:48:22 +0000303 this->beginRecording();
304}
305
306DeferredDevice::~DeferredDevice() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000307 this->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000308 SkSafeUnref(fImmediateCanvas);
junov@chromium.org67d74222013-04-12 13:33:01 +0000309 SkSafeUnref(fSurface);
junov@chromium.org88e29142012-08-07 16:48:22 +0000310}
311
312void DeferredDevice::setMaxRecordingStorage(size_t maxStorage) {
313 fMaxRecordingStorageBytes = maxStorage;
314 this->recordingCanvas(); // Accessing the recording canvas applies the new limit.
315}
316
junov@chromium.org88e29142012-08-07 16:48:22 +0000317void DeferredDevice::beginRecording() {
junov@chromium.orga8db8fe2012-08-15 19:49:22 +0000318 SkASSERT(NULL == fRecordingCanvas);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000319 fRecordingCanvas = fPipeWriter.startRecording(&fPipeController, 0,
junov@chromium.org9becf002013-04-15 18:15:23 +0000320 immediateDevice()->width(), immediateDevice()->height());
junov@chromium.org88e29142012-08-07 16:48:22 +0000321}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000322
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000323void DeferredDevice::setNotificationClient(
324 SkDeferredCanvas::NotificationClient* notificationClient) {
junov@chromium.org52805482012-08-20 14:25:04 +0000325 fNotificationClient = notificationClient;
junov@chromium.org88e29142012-08-07 16:48:22 +0000326}
327
junov@chromium.org0a67f962012-09-19 22:48:34 +0000328void DeferredDevice::skipPendingCommands() {
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000329 if (!fRecordingCanvas->isDrawingToLayer()) {
330 fCanDiscardCanvasContents = true;
331 if (fPipeController.hasPendingCommands()) {
332 fFreshFrame = true;
333 flushPendingCommands(kSilent_PlaybackMode);
334 if (fNotificationClient) {
335 fNotificationClient->skippedPendingDrawCommands();
336 }
junov@google.com52a00ca2012-10-01 15:27:14 +0000337 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000338 }
339}
340
341bool DeferredDevice::isFreshFrame() {
342 bool ret = fFreshFrame;
343 fFreshFrame = false;
344 return ret;
345}
346
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000347bool DeferredDevice::hasPendingCommands() {
348 return fPipeController.hasPendingCommands();
349}
350
junov@chromium.org44324fa2013-08-02 15:36:02 +0000351void DeferredDevice::aboutToDraw()
352{
353 if (NULL != fNotificationClient) {
354 fNotificationClient->prepareForDraw();
355 }
356 if (fCanDiscardCanvasContents) {
357 if (NULL != fSurface) {
358 fSurface->notifyContentWillChange(SkSurface::kDiscard_ContentChangeMode);
359 }
skia.committer@gmail.comea4b7972013-08-06 07:01:27 +0000360 fCanDiscardCanvasContents = false;
junov@chromium.org44324fa2013-08-02 15:36:02 +0000361 }
362}
363
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000364void DeferredDevice::flushPendingCommands(PlaybackMode playbackMode) {
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000365 if (!fPipeController.hasPendingCommands()) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000366 return;
367 }
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000368 if (playbackMode == kNormal_PlaybackMode) {
junov@chromium.org44324fa2013-08-02 15:36:02 +0000369 aboutToDraw();
junov@chromium.org88e29142012-08-07 16:48:22 +0000370 }
junov@chromium.org88e29142012-08-07 16:48:22 +0000371 fPipeWriter.flushRecording(true);
junov@chromium.orgd4501a02012-10-30 19:05:17 +0000372 fPipeController.playback(kSilent_PlaybackMode == playbackMode);
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000373 if (playbackMode == kNormal_PlaybackMode && fNotificationClient) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000374 fNotificationClient->flushedDrawCommands();
375 }
376 fPreviousStorageAllocated = storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000377}
378
379void DeferredDevice::flush() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000380 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000381 fImmediateCanvas->flush();
382}
383
384size_t DeferredDevice::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000385 size_t val = fPipeWriter.freeMemoryIfPossible(bytesToFree);
386 fPreviousStorageAllocated = storageAllocatedForRecording();
387 return val;
junov@chromium.org88e29142012-08-07 16:48:22 +0000388}
389
sugoi@google.com7775fd52012-11-21 15:47:04 +0000390size_t DeferredDevice::getBitmapSizeThreshold() const {
391 return fBitmapSizeThreshold;
392}
393
394void DeferredDevice::setBitmapSizeThreshold(size_t sizeThreshold) {
395 fBitmapSizeThreshold = sizeThreshold;
396}
397
junov@chromium.org88e29142012-08-07 16:48:22 +0000398size_t DeferredDevice::storageAllocatedForRecording() const {
399 return (fPipeController.storageAllocatedForRecording()
400 + fPipeWriter.storageAllocatedForRecording());
401}
402
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000403void DeferredDevice::recordedDrawCommand() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000404 size_t storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000405
junov@chromium.org88e29142012-08-07 16:48:22 +0000406 if (storageAllocated > fMaxRecordingStorageBytes) {
407 // First, attempt to reduce cache without flushing
408 size_t tryFree = storageAllocated - fMaxRecordingStorageBytes;
409 if (this->freeMemoryIfPossible(tryFree) < tryFree) {
410 // Flush is necessary to free more space.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000411 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000412 // Free as much as possible to avoid oscillating around fMaxRecordingStorageBytes
413 // which could cause a high flushing frequency.
bsalomon@google.com100abf42012-09-05 17:40:04 +0000414 this->freeMemoryIfPossible(~0U);
junov@chromium.org88e29142012-08-07 16:48:22 +0000415 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000416 storageAllocated = this->storageAllocatedForRecording();
junov@chromium.org88e29142012-08-07 16:48:22 +0000417 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000418
rmistry@google.comd6176b02012-08-23 18:14:13 +0000419 if (fNotificationClient &&
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000420 storageAllocated != fPreviousStorageAllocated) {
421 fPreviousStorageAllocated = storageAllocated;
422 fNotificationClient->storageAllocatedForRecordingChanged(storageAllocated);
423 }
424}
425
426SkCanvas* DeferredDevice::recordingCanvas() {
junov@chromium.org88e29142012-08-07 16:48:22 +0000427 return fRecordingCanvas;
428}
429
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000430SkImage* DeferredDevice::newImageSnapshot() {
junov@chromium.org67d74222013-04-12 13:33:01 +0000431 this->flush();
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000432 return fSurface ? fSurface->newImageSnapshot() : NULL;
junov@chromium.org67d74222013-04-12 13:33:01 +0000433}
434
rmistry@google.comd6176b02012-08-23 18:14:13 +0000435uint32_t DeferredDevice::getDeviceCapabilities() {
junov@chromium.org9becf002013-04-15 18:15:23 +0000436 return immediateDevice()->getDeviceCapabilities();
junov@chromium.org88e29142012-08-07 16:48:22 +0000437}
438
rmistry@google.comd6176b02012-08-23 18:14:13 +0000439int DeferredDevice::width() const {
junov@chromium.org9becf002013-04-15 18:15:23 +0000440 return immediateDevice()->width();
junov@chromium.org88e29142012-08-07 16:48:22 +0000441}
442
443int DeferredDevice::height() const {
junov@chromium.org9becf002013-04-15 18:15:23 +0000444 return immediateDevice()->height();
junov@chromium.org88e29142012-08-07 16:48:22 +0000445}
446
reed@google.com284a84d2014-02-14 15:23:15 +0000447SkBitmap::Config DeferredDevice::config() const {
448 return immediateDevice()->config();
449}
450
451bool DeferredDevice::isOpaque() const {
452 return immediateDevice()->isOpaque();
453}
454
455SkImageInfo DeferredDevice::imageInfo() const {
456 return immediateDevice()->imageInfo();
457}
458
commit-bot@chromium.orgb8d00db2013-06-26 19:18:23 +0000459GrRenderTarget* DeferredDevice::accessRenderTarget() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000460 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000461 return immediateDevice()->accessRenderTarget();
junov@chromium.org88e29142012-08-07 16:48:22 +0000462}
463
junov@chromium.org44324fa2013-08-02 15:36:02 +0000464void DeferredDevice::prepareForImmediatePixelWrite() {
465 // The purpose of the following code is to make sure commands are flushed, that
466 // aboutToDraw() is called and that notifyContentWillChange is called, without
467 // calling anything redundantly.
468 if (fPipeController.hasPendingCommands()) {
469 this->flushPendingCommands(kNormal_PlaybackMode);
470 } else {
471 bool mustNotifyDirectly = !fCanDiscardCanvasContents;
472 this->aboutToDraw();
473 if (mustNotifyDirectly) {
474 fSurface->notifyContentWillChange(SkSurface::kRetain_ContentChangeMode);
475 }
476 }
477
478 fImmediateCanvas->flush();
479}
480
junov@chromium.org88e29142012-08-07 16:48:22 +0000481void DeferredDevice::writePixels(const SkBitmap& bitmap,
482 int x, int y, SkCanvas::Config8888 config8888) {
483
484 if (x <= 0 && y <= 0 && (x + bitmap.width()) >= width() &&
485 (y + bitmap.height()) >= height()) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000486 this->skipPendingCommands();
junov@chromium.org88e29142012-08-07 16:48:22 +0000487 }
488
489 if (SkBitmap::kARGB_8888_Config == bitmap.config() &&
490 SkCanvas::kNative_Premul_Config8888 != config8888 &&
491 kPMColorAlias != config8888) {
492 //Special case config: no deferral
junov@chromium.org44324fa2013-08-02 15:36:02 +0000493 prepareForImmediatePixelWrite();
junov@chromium.org9becf002013-04-15 18:15:23 +0000494 immediateDevice()->writePixels(bitmap, x, y, config8888);
junov@chromium.org88e29142012-08-07 16:48:22 +0000495 return;
496 }
497
498 SkPaint paint;
499 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
sugoi@google.com7775fd52012-11-21 15:47:04 +0000500 if (shouldDrawImmediately(&bitmap, NULL, getBitmapSizeThreshold())) {
junov@chromium.org44324fa2013-08-02 15:36:02 +0000501 prepareForImmediatePixelWrite();
junov@chromium.org88e29142012-08-07 16:48:22 +0000502 fImmediateCanvas->drawSprite(bitmap, x, y, &paint);
503 } else {
504 this->recordingCanvas()->drawSprite(bitmap, x, y, &paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000505 this->recordedDrawCommand();
506
junov@chromium.org88e29142012-08-07 16:48:22 +0000507 }
508}
509
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000510const SkBitmap& DeferredDevice::onAccessBitmap() {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000511 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org9becf002013-04-15 18:15:23 +0000512 return immediateDevice()->accessBitmap(false);
junov@chromium.org88e29142012-08-07 16:48:22 +0000513}
514
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000515SkBaseDevice* DeferredDevice::onCreateDevice(const SkImageInfo& info, Usage usage) {
junov@chromium.org88e29142012-08-07 16:48:22 +0000516 // Save layer usage not supported, and not required by SkDeferredCanvas.
517 SkASSERT(usage != kSaveLayer_Usage);
518 // Create a compatible non-deferred device.
junov@chromium.orgb1c725a2013-05-21 20:16:17 +0000519 // We do not create a deferred device because we know the new device
520 // will not be used with a deferred canvas (there is no API for that).
521 // And connecting a DeferredDevice to non-deferred canvas can result
522 // in unpredictable behavior.
commit-bot@chromium.org15a14052014-02-16 00:59:25 +0000523 return immediateDevice()->createCompatibleDevice(info);
junov@chromium.org88e29142012-08-07 16:48:22 +0000524}
525
reed@google.com76f10a32014-02-05 15:32:21 +0000526SkSurface* DeferredDevice::newSurface(const SkImageInfo& info) {
527 return this->immediateDevice()->newSurface(info);
528}
529
junov@chromium.org88e29142012-08-07 16:48:22 +0000530bool DeferredDevice::onReadPixels(
531 const SkBitmap& bitmap, int x, int y, SkCanvas::Config8888 config8888) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000532 this->flushPendingCommands(kNormal_PlaybackMode);
junov@chromium.org88e29142012-08-07 16:48:22 +0000533 return fImmediateCanvas->readPixels(const_cast<SkBitmap*>(&bitmap),
534 x, y, config8888);
535}
536
sugoi@google.com7775fd52012-11-21 15:47:04 +0000537class AutoImmediateDrawIfNeeded {
538public:
539 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkBitmap* bitmap,
540 const SkPaint* paint) {
541 this->init(canvas, bitmap, paint);
542 }
543
544 AutoImmediateDrawIfNeeded(SkDeferredCanvas& canvas, const SkPaint* paint) {
545 this->init(canvas, NULL, paint);
546 }
547
548 ~AutoImmediateDrawIfNeeded() {
549 if (fCanvas) {
550 fCanvas->setDeferredDrawing(true);
551 }
552 }
553private:
554 void init(SkDeferredCanvas& canvas, const SkBitmap* bitmap, const SkPaint* paint)
555 {
556 DeferredDevice* device = static_cast<DeferredDevice*>(canvas.getDevice());
557 if (canvas.isDeferredDrawing() && (NULL != device) &&
558 shouldDrawImmediately(bitmap, paint, device->getBitmapSizeThreshold())) {
559 canvas.setDeferredDrawing(false);
560 fCanvas = &canvas;
561 } else {
562 fCanvas = NULL;
563 }
564 }
565
566 SkDeferredCanvas* fCanvas;
567};
junov@chromium.org88e29142012-08-07 16:48:22 +0000568
junov@chromium.org66070a52013-05-28 17:39:08 +0000569SkDeferredCanvas* SkDeferredCanvas::Create(SkSurface* surface) {
570 SkAutoTUnref<DeferredDevice> deferredDevice(SkNEW_ARGS(DeferredDevice, (surface)));
571 return SkNEW_ARGS(SkDeferredCanvas, (deferredDevice));
572}
573
junov@chromium.org66070a52013-05-28 17:39:08 +0000574SkDeferredCanvas::SkDeferredCanvas(DeferredDevice* device) : SkCanvas (device) {
575 this->init();
576}
junov@chromium.org67d74222013-04-12 13:33:01 +0000577
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000578void SkDeferredCanvas::init() {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000579 fDeferredDrawing = true; // On by default
junov@google.com4370aed2012-01-18 16:21:08 +0000580}
581
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000582void SkDeferredCanvas::setMaxRecordingStorage(size_t maxStorage) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000583 this->validate();
junov@chromium.orgbfeddae2012-07-23 13:35:14 +0000584 this->getDeferredDevice()->setMaxRecordingStorage(maxStorage);
585}
586
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000587size_t SkDeferredCanvas::storageAllocatedForRecording() const {
588 return this->getDeferredDevice()->storageAllocatedForRecording();
589}
590
591size_t SkDeferredCanvas::freeMemoryIfPossible(size_t bytesToFree) {
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000592 return this->getDeferredDevice()->freeMemoryIfPossible(bytesToFree);
junov@chromium.org2e14ba82012-08-07 14:26:57 +0000593}
594
sugoi@google.com7775fd52012-11-21 15:47:04 +0000595void SkDeferredCanvas::setBitmapSizeThreshold(size_t sizeThreshold) {
596 DeferredDevice* deferredDevice = this->getDeferredDevice();
597 SkASSERT(deferredDevice);
598 deferredDevice->setBitmapSizeThreshold(sizeThreshold);
599}
600
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000601void SkDeferredCanvas::recordedDrawCommand() {
602 if (fDeferredDrawing) {
603 this->getDeferredDevice()->recordedDrawCommand();
604 }
605}
606
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000607void SkDeferredCanvas::validate() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000608 SkASSERT(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000609}
610
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000611SkCanvas* SkDeferredCanvas::drawingCanvas() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000612 this->validate();
613 return fDeferredDrawing ? this->getDeferredDevice()->recordingCanvas() :
614 this->getDeferredDevice()->immediateCanvas();
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000615}
616
junov@chromium.org88e29142012-08-07 16:48:22 +0000617SkCanvas* SkDeferredCanvas::immediateCanvas() const {
618 this->validate();
619 return this->getDeferredDevice()->immediateCanvas();
620}
621
622DeferredDevice* SkDeferredCanvas::getDeferredDevice() const {
623 return static_cast<DeferredDevice*>(this->getDevice());
junov@google.com4370aed2012-01-18 16:21:08 +0000624}
625
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000626void SkDeferredCanvas::setDeferredDrawing(bool val) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000627 this->validate(); // Must set device before calling this method
junov@google.com4370aed2012-01-18 16:21:08 +0000628 if (val != fDeferredDrawing) {
629 if (fDeferredDrawing) {
junov@chromium.org5e5a0952012-02-28 15:27:59 +0000630 // Going live.
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000631 this->getDeferredDevice()->flushPendingCommands(kNormal_PlaybackMode);
junov@google.com4370aed2012-01-18 16:21:08 +0000632 }
633 fDeferredDrawing = val;
634 }
635}
636
junov@chromium.org88e29142012-08-07 16:48:22 +0000637bool SkDeferredCanvas::isDeferredDrawing() const {
junov@chromium.orgb10a6bd2012-07-25 17:27:13 +0000638 return fDeferredDrawing;
639}
640
junov@chromium.org88e29142012-08-07 16:48:22 +0000641bool SkDeferredCanvas::isFreshFrame() const {
642 return this->getDeferredDevice()->isFreshFrame();
643}
644
junov@chromium.orga38dfb62012-09-20 22:10:33 +0000645bool SkDeferredCanvas::hasPendingCommands() const {
646 return this->getDeferredDevice()->hasPendingCommands();
647}
648
junov@chromium.orgfb103892012-09-20 19:35:43 +0000649void SkDeferredCanvas::silentFlush() {
650 if (fDeferredDrawing) {
junov@chromium.orgeeaf47f2012-09-20 20:42:44 +0000651 this->getDeferredDevice()->flushPendingCommands(kSilent_PlaybackMode);
junov@chromium.orgfb103892012-09-20 19:35:43 +0000652 }
653}
654
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000655SkDeferredCanvas::~SkDeferredCanvas() {
junov@google.com4370aed2012-01-18 16:21:08 +0000656}
657
junov@chromium.org7070f762013-05-24 17:13:00 +0000658SkSurface* SkDeferredCanvas::setSurface(SkSurface* surface) {
659 DeferredDevice* deferredDevice = this->getDeferredDevice();
junov@chromium.org66070a52013-05-28 17:39:08 +0000660 SkASSERT(NULL != deferredDevice);
661 // By swapping the surface into the existing device, we preserve
662 // all pending commands, which can help to seamlessly recover from
663 // a lost accelerated graphics context.
664 deferredDevice->setSurface(surface);
junov@chromium.org7070f762013-05-24 17:13:00 +0000665 return surface;
666}
667
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000668SkDeferredCanvas::NotificationClient* SkDeferredCanvas::setNotificationClient(
669 NotificationClient* notificationClient) {
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000670
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000671 DeferredDevice* deferredDevice = this->getDeferredDevice();
junov@google.com4370aed2012-01-18 16:21:08 +0000672 SkASSERT(deferredDevice);
673 if (deferredDevice) {
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000674 deferredDevice->setNotificationClient(notificationClient);
junov@google.com4370aed2012-01-18 16:21:08 +0000675 }
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000676 return notificationClient;
junov@google.com4370aed2012-01-18 16:21:08 +0000677}
678
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000679SkImage* SkDeferredCanvas::newImageSnapshot() {
junov@chromium.org67d74222013-04-12 13:33:01 +0000680 DeferredDevice* deferredDevice = this->getDeferredDevice();
681 SkASSERT(deferredDevice);
junov@chromium.org5ee449a2013-04-12 20:20:50 +0000682 return deferredDevice ? deferredDevice->newImageSnapshot() : NULL;
junov@chromium.org67d74222013-04-12 13:33:01 +0000683}
684
junov@google.com4370aed2012-01-18 16:21:08 +0000685bool SkDeferredCanvas::isFullFrame(const SkRect* rect,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000686 const SkPaint* paint) const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000687 SkCanvas* canvas = this->drawingCanvas();
688 SkISize canvasSize = this->getDeviceSize();
junov@google.com4370aed2012-01-18 16:21:08 +0000689 if (rect) {
690 if (!canvas->getTotalMatrix().rectStaysRect()) {
691 return false; // conservative
692 }
693
694 SkRect transformedRect;
695 canvas->getTotalMatrix().mapRect(&transformedRect, *rect);
696
697 if (paint) {
698 SkPaint::Style paintStyle = paint->getStyle();
rmistry@google.comd6176b02012-08-23 18:14:13 +0000699 if (!(paintStyle == SkPaint::kFill_Style ||
junov@google.com4370aed2012-01-18 16:21:08 +0000700 paintStyle == SkPaint::kStrokeAndFill_Style)) {
701 return false;
702 }
703 if (paint->getMaskFilter() || paint->getLooper()
704 || paint->getPathEffect() || paint->getImageFilter()) {
705 return false; // conservative
706 }
707 }
708
709 // The following test holds with AA enabled, and is conservative
710 // by a 0.5 pixel margin with AA disabled
rmistry@google.comd6176b02012-08-23 18:14:13 +0000711 if (transformedRect.fLeft > SkIntToScalar(0) ||
712 transformedRect.fTop > SkIntToScalar(0) ||
junov@chromium.orgb1e218e2012-02-13 22:27:58 +0000713 transformedRect.fRight < SkIntToScalar(canvasSize.fWidth) ||
714 transformedRect.fBottom < SkIntToScalar(canvasSize.fHeight)) {
junov@google.com4370aed2012-01-18 16:21:08 +0000715 return false;
716 }
717 }
718
junov@chromium.org8f0ca062012-12-13 16:30:39 +0000719 return this->getClipStack()->quickContains(SkRect::MakeXYWH(0, 0,
720 SkIntToScalar(canvasSize.fWidth), SkIntToScalar(canvasSize.fHeight)));
junov@google.com4370aed2012-01-18 16:21:08 +0000721}
722
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000723int SkDeferredCanvas::save(SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000724 this->drawingCanvas()->save(flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000725 int val = this->INHERITED::save(flags);
726 this->recordedDrawCommand();
727
728 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000729}
730
731int SkDeferredCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000732 SaveFlags flags) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000733 this->drawingCanvas()->saveLayer(bounds, paint, flags);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000734 int count = this->INHERITED::save(flags);
735 this->clipRectBounds(bounds, flags, NULL);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000736 this->recordedDrawCommand();
737
junov@chromium.orga907ac32012-02-24 21:54:07 +0000738 return count;
junov@google.com4370aed2012-01-18 16:21:08 +0000739}
740
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000741void SkDeferredCanvas::restore() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000742 this->drawingCanvas()->restore();
junov@chromium.orga907ac32012-02-24 21:54:07 +0000743 this->INHERITED::restore();
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000744 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000745}
746
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000747bool SkDeferredCanvas::isDrawingToLayer() const {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000748 return this->drawingCanvas()->isDrawingToLayer();
junov@google.com4370aed2012-01-18 16:21:08 +0000749}
750
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000751bool SkDeferredCanvas::translate(SkScalar dx, SkScalar dy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000752 this->drawingCanvas()->translate(dx, dy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000753 bool val = this->INHERITED::translate(dx, dy);
754 this->recordedDrawCommand();
755 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000756}
757
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000758bool SkDeferredCanvas::scale(SkScalar sx, SkScalar sy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000759 this->drawingCanvas()->scale(sx, sy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000760 bool val = this->INHERITED::scale(sx, sy);
761 this->recordedDrawCommand();
762 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000763}
764
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000765bool SkDeferredCanvas::rotate(SkScalar degrees) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000766 this->drawingCanvas()->rotate(degrees);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000767 bool val = this->INHERITED::rotate(degrees);
768 this->recordedDrawCommand();
769 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000770}
771
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000772bool SkDeferredCanvas::skew(SkScalar sx, SkScalar sy) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000773 this->drawingCanvas()->skew(sx, sy);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000774 bool val = this->INHERITED::skew(sx, sy);
775 this->recordedDrawCommand();
776 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000777}
778
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000779bool SkDeferredCanvas::concat(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000780 this->drawingCanvas()->concat(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000781 bool val = this->INHERITED::concat(matrix);
782 this->recordedDrawCommand();
783 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000784}
785
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000786void SkDeferredCanvas::setMatrix(const SkMatrix& matrix) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000787 this->drawingCanvas()->setMatrix(matrix);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000788 this->INHERITED::setMatrix(matrix);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000789 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000790}
791
robertphillips@google.com03fc3b42014-02-28 15:28:02 +0000792bool SkDeferredCanvas::clipRect(const SkRect& rect,
793 SkRegion::Op op,
794 bool doAntiAlias) {
795 this->drawingCanvas()->clipRect(rect, op, doAntiAlias);
796 bool val = this->INHERITED::clipRect(rect, op, doAntiAlias);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000797 this->recordedDrawCommand();
robertphillips@google.com03fc3b42014-02-28 15:28:02 +0000798 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000799}
800
robertphillips@google.com03fc3b42014-02-28 15:28:02 +0000801bool SkDeferredCanvas::clipRRect(const SkRRect& rrect,
802 SkRegion::Op op,
803 bool doAntiAlias) {
804 this->drawingCanvas()->clipRRect(rrect, op, doAntiAlias);
805 bool val = this->INHERITED::clipRRect(rrect, op, doAntiAlias);
reed@google.com4ed0fb72012-12-12 20:48:18 +0000806 this->recordedDrawCommand();
robertphillips@google.com03fc3b42014-02-28 15:28:02 +0000807 return val;
reed@google.com4ed0fb72012-12-12 20:48:18 +0000808}
809
robertphillips@google.com03fc3b42014-02-28 15:28:02 +0000810bool SkDeferredCanvas::clipPath(const SkPath& path,
811 SkRegion::Op op,
812 bool doAntiAlias) {
813 this->drawingCanvas()->clipPath(path, op, doAntiAlias);
814 bool val = this->INHERITED::clipPath(path, op, doAntiAlias);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000815 this->recordedDrawCommand();
robertphillips@google.com03fc3b42014-02-28 15:28:02 +0000816 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000817}
818
robertphillips@google.com03fc3b42014-02-28 15:28:02 +0000819bool SkDeferredCanvas::clipRegion(const SkRegion& deviceRgn,
820 SkRegion::Op op) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000821 this->drawingCanvas()->clipRegion(deviceRgn, op);
robertphillips@google.com03fc3b42014-02-28 15:28:02 +0000822 bool val = this->INHERITED::clipRegion(deviceRgn, op);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000823 this->recordedDrawCommand();
robertphillips@google.com03fc3b42014-02-28 15:28:02 +0000824 return val;
junov@google.com4370aed2012-01-18 16:21:08 +0000825}
826
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000827void SkDeferredCanvas::clear(SkColor color) {
junov@google.com4370aed2012-01-18 16:21:08 +0000828 // purge pending commands
829 if (fDeferredDrawing) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000830 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000831 }
832
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000833 this->drawingCanvas()->clear(color);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000834 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000835}
836
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000837void SkDeferredCanvas::drawPaint(const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000838 if (fDeferredDrawing && this->isFullFrame(NULL, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000839 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000840 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000841 }
junov@chromium.org10f7f972012-07-31 21:01:51 +0000842 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000843 this->drawingCanvas()->drawPaint(paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000844 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000845}
846
847void SkDeferredCanvas::drawPoints(PointMode mode, size_t count,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000848 const SkPoint pts[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000849 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000850 this->drawingCanvas()->drawPoints(mode, count, pts, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000851 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000852}
853
reed@google.com4ed0fb72012-12-12 20:48:18 +0000854void SkDeferredCanvas::drawOval(const SkRect& rect, const SkPaint& paint) {
855 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
856 this->drawingCanvas()->drawOval(rect, paint);
857 this->recordedDrawCommand();
858}
859
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000860void SkDeferredCanvas::drawRect(const SkRect& rect, const SkPaint& paint) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000861 if (fDeferredDrawing && this->isFullFrame(&rect, &paint) &&
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000862 isPaintOpaque(&paint)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000863 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000864 }
skia.committer@gmail.com306ab9d2012-12-13 02:01:33 +0000865
junov@chromium.org10f7f972012-07-31 21:01:51 +0000866 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000867 this->drawingCanvas()->drawRect(rect, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000868 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000869}
870
reed@google.com4ed0fb72012-12-12 20:48:18 +0000871void SkDeferredCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
872 if (rrect.isRect()) {
873 this->SkDeferredCanvas::drawRect(rrect.getBounds(), paint);
874 } else if (rrect.isOval()) {
875 this->SkDeferredCanvas::drawOval(rrect.getBounds(), paint);
876 } else {
877 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
878 this->drawingCanvas()->drawRRect(rrect, paint);
879 this->recordedDrawCommand();
880 }
881}
882
commit-bot@chromium.orgab582732014-02-21 12:20:45 +0000883void SkDeferredCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner,
884 const SkPaint& paint) {
885 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
886 this->drawingCanvas()->drawDRRect(outer, inner, paint);
887 this->recordedDrawCommand();
888}
889
bsalomon@google.com7ce564c2013-10-22 16:54:15 +0000890void SkDeferredCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000891 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000892 this->drawingCanvas()->drawPath(path, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000893 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000894}
895
896void SkDeferredCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar left,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000897 SkScalar top, const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000898 SkRect bitmapRect = SkRect::MakeXYWH(left, top,
899 SkIntToScalar(bitmap.width()), 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()->drawBitmap(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.com71121732012-09-18 15:14:33 +0000911void SkDeferredCanvas::drawBitmapRectToRect(const SkBitmap& bitmap,
912 const SkRect* src,
913 const SkRect& dst,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000914 const SkPaint* paint,
915 DrawBitmapRectFlags flags) {
rmistry@google.comd6176b02012-08-23 18:14:13 +0000916 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000917 this->isFullFrame(&dst, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000918 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000919 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000920 }
921
junov@chromium.org10f7f972012-07-31 21:01:51 +0000922 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +0000923 this->drawingCanvas()->drawBitmapRectToRect(bitmap, src, dst, paint, flags);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000924 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000925}
926
927
928void SkDeferredCanvas::drawBitmapMatrix(const SkBitmap& bitmap,
929 const SkMatrix& m,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000930 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000931 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
932 // covers canvas entirely and transformed bitmap covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000933 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000934 this->drawingCanvas()->drawBitmapMatrix(bitmap, m, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000935 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000936}
937
938void SkDeferredCanvas::drawBitmapNine(const SkBitmap& bitmap,
939 const SkIRect& center, const SkRect& dst,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000940 const SkPaint* paint) {
junov@google.com4370aed2012-01-18 16:21:08 +0000941 // TODO: reset recording canvas if paint+bitmap is opaque and clip rect
942 // covers canvas entirely and dst covers canvas entirely
junov@chromium.org10f7f972012-07-31 21:01:51 +0000943 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000944 this->drawingCanvas()->drawBitmapNine(bitmap, center, dst, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000945 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000946}
947
948void SkDeferredCanvas::drawSprite(const SkBitmap& bitmap, int left, int top,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000949 const SkPaint* paint) {
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000950 SkRect bitmapRect = SkRect::MakeXYWH(
951 SkIntToScalar(left),
rmistry@google.comd6176b02012-08-23 18:14:13 +0000952 SkIntToScalar(top),
junov@chromium.org8f9ecbd2012-02-13 21:53:45 +0000953 SkIntToScalar(bitmap.width()),
954 SkIntToScalar(bitmap.height()));
rmistry@google.comd6176b02012-08-23 18:14:13 +0000955 if (fDeferredDrawing &&
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000956 this->isFullFrame(&bitmapRect, paint) &&
junov@chromium.org87f982c2012-02-23 21:34:34 +0000957 isPaintOpaque(paint, &bitmap)) {
junov@chromium.org0a67f962012-09-19 22:48:34 +0000958 this->getDeferredDevice()->skipPendingCommands();
junov@google.com4370aed2012-01-18 16:21:08 +0000959 }
960
junov@chromium.org10f7f972012-07-31 21:01:51 +0000961 AutoImmediateDrawIfNeeded autoDraw(*this, &bitmap, paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000962 this->drawingCanvas()->drawSprite(bitmap, left, top, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000963 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000964}
965
966void SkDeferredCanvas::drawText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000967 SkScalar x, SkScalar y, const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000968 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000969 this->drawingCanvas()->drawText(text, byteLength, x, y, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000970 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000971}
972
973void SkDeferredCanvas::drawPosText(const void* text, size_t byteLength,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000974 const SkPoint pos[], const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000975 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000976 this->drawingCanvas()->drawPosText(text, byteLength, pos, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000977 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000978}
979
980void SkDeferredCanvas::drawPosTextH(const void* text, size_t byteLength,
981 const SkScalar xpos[], SkScalar constY,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000982 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000983 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000984 this->drawingCanvas()->drawPosTextH(text, byteLength, xpos, constY, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000985 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000986}
987
988void SkDeferredCanvas::drawTextOnPath(const void* text, size_t byteLength,
989 const SkPath& path,
990 const SkMatrix* matrix,
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000991 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +0000992 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000993 this->drawingCanvas()->drawTextOnPath(text, byteLength, path, matrix, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000994 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +0000995}
996
junov@chromium.orgc16ca922012-02-24 22:06:27 +0000997void SkDeferredCanvas::drawPicture(SkPicture& picture) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +0000998 this->drawingCanvas()->drawPicture(picture);
junov@chromium.org9ed02b92012-08-14 13:36:26 +0000999 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +00001000}
1001
1002void SkDeferredCanvas::drawVertices(VertexMode vmode, int vertexCount,
1003 const SkPoint vertices[],
1004 const SkPoint texs[],
1005 const SkColor colors[], SkXfermode* xmode,
1006 const uint16_t indices[], int indexCount,
junov@chromium.orgc16ca922012-02-24 22:06:27 +00001007 const SkPaint& paint) {
junov@chromium.org10f7f972012-07-31 21:01:51 +00001008 AutoImmediateDrawIfNeeded autoDraw(*this, &paint);
junov@chromium.org9060c9b2012-08-07 15:14:01 +00001009 this->drawingCanvas()->drawVertices(vmode, vertexCount, vertices, texs, colors, xmode,
1010 indices, indexCount, paint);
junov@chromium.org9ed02b92012-08-14 13:36:26 +00001011 this->recordedDrawCommand();
junov@google.com4370aed2012-01-18 16:21:08 +00001012}
1013
junov@chromium.orgc16ca922012-02-24 22:06:27 +00001014SkBounder* SkDeferredCanvas::setBounder(SkBounder* bounder) {
junov@chromium.org9060c9b2012-08-07 15:14:01 +00001015 this->drawingCanvas()->setBounder(bounder);
junov@chromium.org9ed02b92012-08-14 13:36:26 +00001016 this->INHERITED::setBounder(bounder);
1017 this->recordedDrawCommand();
1018 return bounder;
junov@google.com4370aed2012-01-18 16:21:08 +00001019}
1020
junov@chromium.orgc16ca922012-02-24 22:06:27 +00001021SkDrawFilter* SkDeferredCanvas::setDrawFilter(SkDrawFilter* filter) {
rmistry@google.comd6176b02012-08-23 18:14:13 +00001022 this->drawingCanvas()->setDrawFilter(filter);
junov@chromium.org9ed02b92012-08-14 13:36:26 +00001023 this->INHERITED::setDrawFilter(filter);
1024 this->recordedDrawCommand();
rmistry@google.comd6176b02012-08-23 18:14:13 +00001025 return filter;
junov@google.com4370aed2012-01-18 16:21:08 +00001026}
1027
1028SkCanvas* SkDeferredCanvas::canvasForDrawIter() {
junov@chromium.org9060c9b2012-08-07 15:14:01 +00001029 return this->drawingCanvas();
junov@google.com4370aed2012-01-18 16:21:08 +00001030}