blob: a14bb63e5dc30eb61b80c5ab6ea664f6c02dea51 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2008 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkCanvas.h"
robertphillips@google.com1f2f3382013-08-29 11:54:56 +000011#include "SkBitmapDevice.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012#include "SkBounder.h"
senorblanco@chromium.org9c397442012-09-27 21:57:45 +000013#include "SkDeviceImageFilterProxy.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkDraw.h"
15#include "SkDrawFilter.h"
16#include "SkDrawLooper.h"
mike@reedtribe.org74bb77e2012-09-26 02:24:45 +000017#include "SkMetaData.h"
caryclark@google.com45a75fb2013-04-25 13:34:40 +000018#include "SkPathOps.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkPicture.h"
reed@google.com00177082011-10-12 14:34:30 +000020#include "SkRasterClip.h"
reed@google.com4ed0fb72012-12-12 20:48:18 +000021#include "SkRRect.h"
reed@google.com97af1a62012-08-28 12:19:02 +000022#include "SkSurface_Base.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000023#include "SkTemplates.h"
bungeman@google.com52c748b2011-08-22 21:30:43 +000024#include "SkTextFormatParams.h"
reed@google.coma076e9b2011-04-06 20:17:29 +000025#include "SkTLazy.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000026#include "SkUtils.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000027
commit-bot@chromium.org644629c2013-11-21 06:21:58 +000028#if SK_SUPPORT_GPU
29#include "GrRenderTarget.h"
30#endif
31
reed@google.comda17f752012-08-16 18:27:05 +000032// experimental for faster tiled drawing...
33//#define SK_ENABLE_CLIP_QUICKREJECT
robertphillips@google.com15e9d3e2012-06-21 20:25:03 +000034
reed@android.com8a1c16f2008-12-17 15:59:43 +000035//#define SK_TRACE_SAVERESTORE
36
37#ifdef SK_TRACE_SAVERESTORE
38 static int gLayerCounter;
39 static void inc_layer() { ++gLayerCounter; printf("----- inc layer %d\n", gLayerCounter); }
40 static void dec_layer() { --gLayerCounter; printf("----- dec layer %d\n", gLayerCounter); }
41
42 static int gRecCounter;
43 static void inc_rec() { ++gRecCounter; printf("----- inc rec %d\n", gRecCounter); }
44 static void dec_rec() { --gRecCounter; printf("----- dec rec %d\n", gRecCounter); }
45
46 static int gCanvasCounter;
47 static void inc_canvas() { ++gCanvasCounter; printf("----- inc canvas %d\n", gCanvasCounter); }
48 static void dec_canvas() { --gCanvasCounter; printf("----- dec canvas %d\n", gCanvasCounter); }
49#else
50 #define inc_layer()
51 #define dec_layer()
52 #define inc_rec()
53 #define dec_rec()
54 #define inc_canvas()
55 #define dec_canvas()
56#endif
57
reed@google.comea033602012-12-14 13:13:55 +000058#ifdef SK_DEBUG
59#include "SkPixelRef.h"
60
reed@google.comf53d0a92013-01-30 13:17:32 +000061/*
62 * Some pixelref subclasses can support being "locked" from another thread
63 * during the lock-scope of skia calling them. In these instances, this balance
64 * check will fail, but may not be indicative of a problem, so we allow a build
65 * flag to disable this check.
66 *
67 * Potentially another fix would be to have a (debug-only) virtual or flag on
68 * pixelref, which could tell us at runtime if this check is valid. That would
69 * eliminate the need for this heavy-handed build check.
70 */
71#ifdef SK_DISABLE_PIXELREF_LOCKCOUNT_BALANCE_CHECK
72class AutoCheckLockCountBalance {
73public:
74 AutoCheckLockCountBalance(const SkBitmap&) { /* do nothing */ }
75};
76#else
reed@google.comea033602012-12-14 13:13:55 +000077class AutoCheckLockCountBalance {
78public:
79 AutoCheckLockCountBalance(const SkBitmap& bm) : fPixelRef(bm.pixelRef()) {
80 fLockCount = fPixelRef ? fPixelRef->getLockCount() : 0;
81 }
82 ~AutoCheckLockCountBalance() {
83 const int count = fPixelRef ? fPixelRef->getLockCount() : 0;
84 SkASSERT(count == fLockCount);
85 }
86
87private:
88 const SkPixelRef* fPixelRef;
89 int fLockCount;
90};
reed@google.comf53d0a92013-01-30 13:17:32 +000091#endif
reed@google.comea033602012-12-14 13:13:55 +000092
93class AutoCheckNoSetContext {
94public:
95 AutoCheckNoSetContext(const SkPaint& paint) : fPaint(paint) {
96 this->assertNoSetContext(fPaint);
97 }
98 ~AutoCheckNoSetContext() {
99 this->assertNoSetContext(fPaint);
100 }
101
102private:
103 const SkPaint& fPaint;
104
105 void assertNoSetContext(const SkPaint& paint) {
106 SkShader* s = paint.getShader();
107 if (s) {
108 SkASSERT(!s->setContextHasBeenCalled());
109 }
110 }
111};
112
113#define CHECK_LOCKCOUNT_BALANCE(bitmap) AutoCheckLockCountBalance clcb(bitmap)
114#define CHECK_SHADER_NOSETCONTEXT(paint) AutoCheckNoSetContext cshsc(paint)
115
116#else
117 #define CHECK_LOCKCOUNT_BALANCE(bitmap)
118 #define CHECK_SHADER_NOSETCONTEXT(paint)
119#endif
120
mike@reedtribe.org2c8fc5a2011-04-10 00:35:29 +0000121typedef SkTLazy<SkPaint> SkLazyPaint;
122
reed@google.com97af1a62012-08-28 12:19:02 +0000123void SkCanvas::predrawNotify() {
124 if (fSurfaceBase) {
commit-bot@chromium.orgc4c98702013-04-22 14:28:01 +0000125 fSurfaceBase->aboutToDraw(SkSurface::kRetain_ContentChangeMode);
reed@google.com97af1a62012-08-28 12:19:02 +0000126 }
127}
128
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000131/* This is the record we keep for each SkBaseDevice that the user installs.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 The clip/matrix/proc are fields that reflect the top of the save/restore
133 stack. Whenever the canvas changes, it marks a dirty flag, and then before
134 these are used (assuming we're not on a layer) we rebuild these cache
135 values: they reflect the top of the save stack, but translated and clipped
136 by the device's XY offset and bitmap-bounds.
137*/
138struct DeviceCM {
139 DeviceCM* fNext;
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000140 SkBaseDevice* fDevice;
reed@google.com045e62d2011-10-24 12:19:46 +0000141 SkRasterClip fClip;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142 const SkMatrix* fMatrix;
reed@google.com6f8f2922011-03-04 22:27:10 +0000143 SkPaint* fPaint; // may be null (in the future)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000145 DeviceCM(SkBaseDevice* device, int x, int y, const SkPaint* paint, SkCanvas* canvas)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 : fNext(NULL) {
147 if (NULL != device) {
148 device->ref();
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000149 device->onAttachToCanvas(canvas);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000150 }
reed@google.com4b226022011-01-11 18:32:13 +0000151 fDevice = device;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000152 fPaint = paint ? SkNEW_ARGS(SkPaint, (*paint)) : NULL;
bungeman@google.com88edf1e2011-08-08 19:41:56 +0000153 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000154
bungeman@google.com88edf1e2011-08-08 19:41:56 +0000155 ~DeviceCM() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000156 if (NULL != fDevice) {
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000157 fDevice->onDetachFromCanvas();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158 fDevice->unref();
159 }
bungeman@google.com88edf1e2011-08-08 19:41:56 +0000160 SkDELETE(fPaint);
161 }
reed@google.com4b226022011-01-11 18:32:13 +0000162
reed@google.com045e62d2011-10-24 12:19:46 +0000163 void updateMC(const SkMatrix& totalMatrix, const SkRasterClip& totalClip,
164 const SkClipStack& clipStack, SkRasterClip* updateClip) {
reed@google.com6f8f2922011-03-04 22:27:10 +0000165 int x = fDevice->getOrigin().x();
166 int y = fDevice->getOrigin().y();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167 int width = fDevice->width();
168 int height = fDevice->height();
reed@google.com4b226022011-01-11 18:32:13 +0000169
reed@android.com8a1c16f2008-12-17 15:59:43 +0000170 if ((x | y) == 0) {
171 fMatrix = &totalMatrix;
172 fClip = totalClip;
173 } else {
174 fMatrixStorage = totalMatrix;
175 fMatrixStorage.postTranslate(SkIntToScalar(-x),
176 SkIntToScalar(-y));
177 fMatrix = &fMatrixStorage;
reed@google.com4b226022011-01-11 18:32:13 +0000178
reed@android.com8a1c16f2008-12-17 15:59:43 +0000179 totalClip.translate(-x, -y, &fClip);
180 }
181
reed@google.com045e62d2011-10-24 12:19:46 +0000182 fClip.op(SkIRect::MakeWH(width, height), SkRegion::kIntersect_Op);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183
184 // intersect clip, but don't translate it (yet)
reed@google.com4b226022011-01-11 18:32:13 +0000185
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186 if (updateClip) {
reed@google.com045e62d2011-10-24 12:19:46 +0000187 updateClip->op(SkIRect::MakeXYWH(x, y, width, height),
reed@android.com8a1c16f2008-12-17 15:59:43 +0000188 SkRegion::kDifference_Op);
189 }
reed@google.com4b226022011-01-11 18:32:13 +0000190
robertphillips@google.com3fffb2e2012-10-09 22:30:18 +0000191 fDevice->setMatrixClip(*fMatrix, fClip.forceGetBW(), clipStack);
192
reed@android.com8a1c16f2008-12-17 15:59:43 +0000193#ifdef SK_DEBUG
194 if (!fClip.isEmpty()) {
195 SkIRect deviceR;
196 deviceR.set(0, 0, width, height);
197 SkASSERT(deviceR.contains(fClip.getBounds()));
198 }
199#endif
reed@android.comf2b98d62010-12-20 18:26:13 +0000200 }
201
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202private:
bsalomon@google.com0e354aa2012-10-08 20:44:25 +0000203 SkMatrix fMatrixStorage;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204};
205
206/* This is the record we keep for each save/restore level in the stack.
207 Since a level optionally copies the matrix and/or stack, we have pointers
208 for these fields. If the value is copied for this level, the copy is
209 stored in the ...Storage field, and the pointer points to that. If the
210 value is not copied for this level, we ignore ...Storage, and just point
211 at the corresponding value in the previous level in the stack.
212*/
213class SkCanvas::MCRec {
214public:
215 MCRec* fNext;
commit-bot@chromium.org6c157642013-08-16 00:53:34 +0000216 int fFlags;
reed@google.com00177082011-10-12 14:34:30 +0000217 SkMatrix* fMatrix; // points to either fMatrixStorage or prev MCRec
218 SkRasterClip* fRasterClip; // points to either fRegionStorage or prev MCRec
219 SkDrawFilter* fFilter; // the current filter (or null)
reed@google.com4b226022011-01-11 18:32:13 +0000220
reed@android.com8a1c16f2008-12-17 15:59:43 +0000221 DeviceCM* fLayer;
222 /* If there are any layers in the stack, this points to the top-most
223 one that is at or below this level in the stack (so we know what
224 bitmap/device to draw into from this level. This value is NOT
225 reference counted, since the real owner is either our fLayer field,
226 or a previous one in a lower level.)
227 */
bungeman@google.com88edf1e2011-08-08 19:41:56 +0000228 DeviceCM* fTopLayer;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229
commit-bot@chromium.org6c157642013-08-16 00:53:34 +0000230 MCRec(const MCRec* prev, int flags) : fFlags(flags) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000231 if (NULL != prev) {
232 if (flags & SkCanvas::kMatrix_SaveFlag) {
233 fMatrixStorage = *prev->fMatrix;
234 fMatrix = &fMatrixStorage;
235 } else {
236 fMatrix = prev->fMatrix;
237 }
reed@google.com4b226022011-01-11 18:32:13 +0000238
reed@android.com8a1c16f2008-12-17 15:59:43 +0000239 if (flags & SkCanvas::kClip_SaveFlag) {
reed@google.com00177082011-10-12 14:34:30 +0000240 fRasterClipStorage = *prev->fRasterClip;
241 fRasterClip = &fRasterClipStorage;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000242 } else {
reed@google.com00177082011-10-12 14:34:30 +0000243 fRasterClip = prev->fRasterClip;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000244 }
245
246 fFilter = prev->fFilter;
reed@google.com82065d62011-02-07 15:30:46 +0000247 SkSafeRef(fFilter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000248
249 fTopLayer = prev->fTopLayer;
250 } else { // no prev
251 fMatrixStorage.reset();
reed@google.com4b226022011-01-11 18:32:13 +0000252
reed@android.com8a1c16f2008-12-17 15:59:43 +0000253 fMatrix = &fMatrixStorage;
reed@google.com00177082011-10-12 14:34:30 +0000254 fRasterClip = &fRasterClipStorage;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000255 fFilter = NULL;
256 fTopLayer = NULL;
257 }
258 fLayer = NULL;
259
260 // don't bother initializing fNext
261 inc_rec();
262 }
263 ~MCRec() {
reed@google.com82065d62011-02-07 15:30:46 +0000264 SkSafeUnref(fFilter);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000265 SkDELETE(fLayer);
266 dec_rec();
267 }
reed@google.com4b226022011-01-11 18:32:13 +0000268
reed@android.com8a1c16f2008-12-17 15:59:43 +0000269private:
reed@google.com00177082011-10-12 14:34:30 +0000270 SkMatrix fMatrixStorage;
271 SkRasterClip fRasterClipStorage;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000272};
273
274class SkDrawIter : public SkDraw {
275public:
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000276 SkDrawIter(SkCanvas* canvas, bool skipEmptyClips = true) {
junov@google.com4370aed2012-01-18 16:21:08 +0000277 canvas = canvas->canvasForDrawIter();
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000278 fCanvas = canvas;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000279 canvas->updateDeviceCMCache();
280
reed@google.com90c07ea2012-04-13 13:50:27 +0000281 fClipStack = &canvas->fClipStack;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000282 fBounder = canvas->getBounder();
283 fCurrLayer = canvas->fMCRec->fTopLayer;
tomhudson@google.com8a0b0292011-09-13 14:41:06 +0000284 fSkipEmptyClips = skipEmptyClips;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285 }
reed@google.com4b226022011-01-11 18:32:13 +0000286
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287 bool next() {
288 // skip over recs with empty clips
289 if (fSkipEmptyClips) {
290 while (fCurrLayer && fCurrLayer->fClip.isEmpty()) {
291 fCurrLayer = fCurrLayer->fNext;
292 }
293 }
294
reed@google.comf68c5e22012-02-24 16:38:58 +0000295 const DeviceCM* rec = fCurrLayer;
296 if (rec && rec->fDevice) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000297
298 fMatrix = rec->fMatrix;
reed@google.com045e62d2011-10-24 12:19:46 +0000299 fClip = &((SkRasterClip*)&rec->fClip)->forceGetBW();
300 fRC = &rec->fClip;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000301 fDevice = rec->fDevice;
302 fBitmap = &fDevice->accessBitmap(true);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000303 fPaint = rec->fPaint;
reed@android.comf2b98d62010-12-20 18:26:13 +0000304 SkDEBUGCODE(this->validate();)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000305
306 fCurrLayer = rec->fNext;
307 if (fBounder) {
308 fBounder->setClip(fClip);
309 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000310 // fCurrLayer may be NULL now
reed@android.com199f1082009-06-10 02:12:47 +0000311
reed@android.com8a1c16f2008-12-17 15:59:43 +0000312 return true;
313 }
314 return false;
315 }
reed@google.com4b226022011-01-11 18:32:13 +0000316
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000317 SkBaseDevice* getDevice() const { return fDevice; }
reed@google.com6f8f2922011-03-04 22:27:10 +0000318 int getX() const { return fDevice->getOrigin().x(); }
319 int getY() const { return fDevice->getOrigin().y(); }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000320 const SkMatrix& getMatrix() const { return *fMatrix; }
321 const SkRegion& getClip() const { return *fClip; }
322 const SkPaint* getPaint() const { return fPaint; }
reed@google.com6f8f2922011-03-04 22:27:10 +0000323
reed@android.com8a1c16f2008-12-17 15:59:43 +0000324private:
325 SkCanvas* fCanvas;
326 const DeviceCM* fCurrLayer;
327 const SkPaint* fPaint; // May be null.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000328 SkBool8 fSkipEmptyClips;
329
330 typedef SkDraw INHERITED;
331};
332
333/////////////////////////////////////////////////////////////////////////////
334
335class AutoDrawLooper {
336public:
reed@google.com8926b162012-03-23 15:36:36 +0000337 AutoDrawLooper(SkCanvas* canvas, const SkPaint& paint,
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +0000338 bool skipLayerForImageFilter = false,
339 const SkRect* bounds = NULL) : fOrigPaint(paint) {
reed@google.com4e2b3d32011-04-07 14:18:59 +0000340 fCanvas = canvas;
341 fLooper = paint.getLooper();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000342 fFilter = canvas->getDrawFilter();
reed@google.com4e2b3d32011-04-07 14:18:59 +0000343 fPaint = NULL;
344 fSaveCount = canvas->getSaveCount();
reed@google.com8926b162012-03-23 15:36:36 +0000345 fDoClearImageFilter = false;
reed@google.com4e2b3d32011-04-07 14:18:59 +0000346 fDone = false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000347
reed@google.com8926b162012-03-23 15:36:36 +0000348 if (!skipLayerForImageFilter && fOrigPaint.getImageFilter()) {
349 SkPaint tmp;
350 tmp.setImageFilter(fOrigPaint.getImageFilter());
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +0000351 (void)canvas->internalSaveLayer(bounds, &tmp,
reed@google.com8926b162012-03-23 15:36:36 +0000352 SkCanvas::kARGB_ClipLayer_SaveFlag, true);
353 // we'll clear the imageFilter for the actual draws in next(), so
354 // it will only be applied during the restore().
355 fDoClearImageFilter = true;
356 }
357
reed@google.com4e2b3d32011-04-07 14:18:59 +0000358 if (fLooper) {
359 fLooper->init(canvas);
reed@google.com129ec222012-05-15 13:24:09 +0000360 fIsSimple = false;
361 } else {
362 // can we be marked as simple?
363 fIsSimple = !fFilter && !fDoClearImageFilter;
reed@google.com4e2b3d32011-04-07 14:18:59 +0000364 }
365 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000366
reed@android.com8a1c16f2008-12-17 15:59:43 +0000367 ~AutoDrawLooper() {
reed@google.com8926b162012-03-23 15:36:36 +0000368 if (fDoClearImageFilter) {
369 fCanvas->internalRestore();
370 }
reed@google.com4e2b3d32011-04-07 14:18:59 +0000371 SkASSERT(fCanvas->getSaveCount() == fSaveCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000372 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000373
reed@google.com4e2b3d32011-04-07 14:18:59 +0000374 const SkPaint& paint() const {
375 SkASSERT(fPaint);
376 return *fPaint;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000377 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000378
reed@google.com129ec222012-05-15 13:24:09 +0000379 bool next(SkDrawFilter::Type drawType) {
380 if (fDone) {
381 return false;
382 } else if (fIsSimple) {
383 fDone = true;
384 fPaint = &fOrigPaint;
385 return !fPaint->nothingToDraw();
386 } else {
387 return this->doNext(drawType);
388 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000389 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000390
reed@android.com8a1c16f2008-12-17 15:59:43 +0000391private:
mike@reedtribe.org2c8fc5a2011-04-10 00:35:29 +0000392 SkLazyPaint fLazyPaint;
393 SkCanvas* fCanvas;
394 const SkPaint& fOrigPaint;
395 SkDrawLooper* fLooper;
396 SkDrawFilter* fFilter;
397 const SkPaint* fPaint;
398 int fSaveCount;
reed@google.com8926b162012-03-23 15:36:36 +0000399 bool fDoClearImageFilter;
mike@reedtribe.org2c8fc5a2011-04-10 00:35:29 +0000400 bool fDone;
reed@google.com129ec222012-05-15 13:24:09 +0000401 bool fIsSimple;
402
403 bool doNext(SkDrawFilter::Type drawType);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000404};
405
reed@google.com129ec222012-05-15 13:24:09 +0000406bool AutoDrawLooper::doNext(SkDrawFilter::Type drawType) {
reed@google.com632e1a22011-10-06 12:37:00 +0000407 fPaint = NULL;
reed@google.com129ec222012-05-15 13:24:09 +0000408 SkASSERT(!fIsSimple);
409 SkASSERT(fLooper || fFilter || fDoClearImageFilter);
410
411 SkPaint* paint = fLazyPaint.set(fOrigPaint);
412
413 if (fDoClearImageFilter) {
414 paint->setImageFilter(NULL);
reed@google.com4e2b3d32011-04-07 14:18:59 +0000415 }
reed@google.com4e2b3d32011-04-07 14:18:59 +0000416
reed@google.com129ec222012-05-15 13:24:09 +0000417 if (fLooper && !fLooper->next(fCanvas, paint)) {
reed@google.com4e2b3d32011-04-07 14:18:59 +0000418 fDone = true;
reed@google.com129ec222012-05-15 13:24:09 +0000419 return false;
420 }
421 if (fFilter) {
reed@google.com971aca72012-11-26 20:26:54 +0000422 if (!fFilter->filter(paint, drawType)) {
423 fDone = true;
424 return false;
425 }
reed@google.com129ec222012-05-15 13:24:09 +0000426 if (NULL == fLooper) {
427 // no looper means we only draw once
428 fDone = true;
429 }
430 }
431 fPaint = paint;
432
433 // if we only came in here for the imagefilter, mark us as done
434 if (!fLooper && !fFilter) {
435 fDone = true;
reed@google.com632e1a22011-10-06 12:37:00 +0000436 }
437
438 // call this after any possible paint modifiers
439 if (fPaint->nothingToDraw()) {
reed@google.com4e2b3d32011-04-07 14:18:59 +0000440 fPaint = NULL;
441 return false;
442 }
reed@google.com4e2b3d32011-04-07 14:18:59 +0000443 return true;
444}
445
reed@android.com8a1c16f2008-12-17 15:59:43 +0000446/* Stack helper for managing a SkBounder. In the destructor, if we were
447 given a bounder, we call its commit() method, signifying that we are
448 done accumulating bounds for that draw.
449*/
450class SkAutoBounderCommit {
451public:
452 SkAutoBounderCommit(SkBounder* bounder) : fBounder(bounder) {}
453 ~SkAutoBounderCommit() {
454 if (NULL != fBounder) {
455 fBounder->commit();
456 }
457 }
458private:
459 SkBounder* fBounder;
460};
commit-bot@chromium.orge61a86c2013-11-18 16:03:59 +0000461#define SkAutoBounderCommit(...) SK_REQUIRE_LOCAL_VAR(SkAutoBounderCommit)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000462
463#include "SkColorPriv.h"
464
reed@android.com8a1c16f2008-12-17 15:59:43 +0000465////////// macros to place around the internal draw calls //////////////////
466
reed@google.com8926b162012-03-23 15:36:36 +0000467#define LOOPER_BEGIN_DRAWDEVICE(paint, type) \
reed@google.com97af1a62012-08-28 12:19:02 +0000468 this->predrawNotify(); \
reed@google.com8926b162012-03-23 15:36:36 +0000469 AutoDrawLooper looper(this, paint, true); \
470 while (looper.next(type)) { \
471 SkAutoBounderCommit ac(fBounder); \
472 SkDrawIter iter(this);
473
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +0000474#define LOOPER_BEGIN(paint, type, bounds) \
reed@google.com97af1a62012-08-28 12:19:02 +0000475 this->predrawNotify(); \
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +0000476 AutoDrawLooper looper(this, paint, false, bounds); \
reed@google.com4e2b3d32011-04-07 14:18:59 +0000477 while (looper.next(type)) { \
reed@android.com8a1c16f2008-12-17 15:59:43 +0000478 SkAutoBounderCommit ac(fBounder); \
479 SkDrawIter iter(this);
reed@google.com4b226022011-01-11 18:32:13 +0000480
reed@google.com4e2b3d32011-04-07 14:18:59 +0000481#define LOOPER_END }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000482
483////////////////////////////////////////////////////////////////////////////
484
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000485SkBaseDevice* SkCanvas::init(SkBaseDevice* device) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000486 fBounder = NULL;
reed@google.comc0784db2013-12-13 21:16:12 +0000487 fCachedLocalClipBounds.setEmpty();
488 fCachedLocalClipBoundsDirty = true;
caryclark@google.com8f0a7b82012-11-07 14:54:49 +0000489 fAllowSoftClip = true;
caryclark@google.com45a75fb2013-04-25 13:34:40 +0000490 fAllowSimplifyClip = false;
agl@chromium.org447bcfa2009-12-12 00:06:17 +0000491 fDeviceCMDirty = false;
junov@chromium.orgb0a7ace2012-04-05 18:33:23 +0000492 fSaveLayerCount = 0;
mike@reedtribe.org74bb77e2012-09-26 02:24:45 +0000493 fMetaData = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000494
495 fMCRec = (MCRec*)fMCStack.push_back();
496 new (fMCRec) MCRec(NULL, 0);
497
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000498 fMCRec->fLayer = SkNEW_ARGS(DeviceCM, (NULL, 0, 0, NULL, NULL));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000499 fMCRec->fTopLayer = fMCRec->fLayer;
500 fMCRec->fNext = NULL;
501
reed@google.com97af1a62012-08-28 12:19:02 +0000502 fSurfaceBase = NULL;
reed@android.comf2b98d62010-12-20 18:26:13 +0000503
reed@android.com8a1c16f2008-12-17 15:59:43 +0000504 return this->setDevice(device);
505}
506
reed@google.comcde92112011-07-06 20:00:52 +0000507SkCanvas::SkCanvas()
commit-bot@chromium.orge2543102014-01-31 19:42:58 +0000508 : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage))
509{
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +0000510 inc_canvas();
skia.committer@gmail.comba124482014-02-01 03:01:57 +0000511
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +0000512 this->init(NULL);
513}
514
commit-bot@chromium.orge2543102014-01-31 19:42:58 +0000515SkCanvas::SkCanvas(int width, int height)
516 : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage))
517{
518 inc_canvas();
skia.committer@gmail.comba124482014-02-01 03:01:57 +0000519
commit-bot@chromium.orge2543102014-01-31 19:42:58 +0000520 SkBitmap bitmap;
521 bitmap.setConfig(SkBitmap::kNo_Config, width, height);
522 this->init(SkNEW_ARGS(SkBitmapDevice, (bitmap)))->unref();
523}
524
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000525SkCanvas::SkCanvas(SkBaseDevice* device)
commit-bot@chromium.orge2543102014-01-31 19:42:58 +0000526 : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage))
527{
reed@android.com8a1c16f2008-12-17 15:59:43 +0000528 inc_canvas();
529
530 this->init(device);
531}
532
533SkCanvas::SkCanvas(const SkBitmap& bitmap)
commit-bot@chromium.orge2543102014-01-31 19:42:58 +0000534 : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage))
535{
reed@android.com8a1c16f2008-12-17 15:59:43 +0000536 inc_canvas();
537
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000538 this->init(SkNEW_ARGS(SkBitmapDevice, (bitmap)))->unref();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000539}
540
541SkCanvas::~SkCanvas() {
542 // free up the contents of our deque
543 this->restoreToCount(1); // restore everything but the last
junov@chromium.orgb0a7ace2012-04-05 18:33:23 +0000544 SkASSERT(0 == fSaveLayerCount);
reed@google.com7c202932011-12-14 18:48:05 +0000545
reed@android.com8a1c16f2008-12-17 15:59:43 +0000546 this->internalRestore(); // restore the last, since we're going away
547
vandebo@chromium.org8d84fac2010-10-13 22:13:05 +0000548 SkSafeUnref(fBounder);
mike@reedtribe.org74bb77e2012-09-26 02:24:45 +0000549 SkDELETE(fMetaData);
vandebo@chromium.orgb70ae312010-10-15 18:58:19 +0000550
reed@android.com8a1c16f2008-12-17 15:59:43 +0000551 dec_canvas();
552}
553
554SkBounder* SkCanvas::setBounder(SkBounder* bounder) {
555 SkRefCnt_SafeAssign(fBounder, bounder);
556 return bounder;
557}
558
559SkDrawFilter* SkCanvas::getDrawFilter() const {
560 return fMCRec->fFilter;
561}
562
563SkDrawFilter* SkCanvas::setDrawFilter(SkDrawFilter* filter) {
564 SkRefCnt_SafeAssign(fMCRec->fFilter, filter);
565 return filter;
566}
567
mike@reedtribe.org74bb77e2012-09-26 02:24:45 +0000568SkMetaData& SkCanvas::getMetaData() {
569 // metadata users are rare, so we lazily allocate it. If that changes we
570 // can decide to just make it a field in the device (rather than a ptr)
571 if (NULL == fMetaData) {
572 fMetaData = new SkMetaData;
573 }
574 return *fMetaData;
575}
576
reed@android.com8a1c16f2008-12-17 15:59:43 +0000577///////////////////////////////////////////////////////////////////////////////
578
junov@chromium.orgbf6c1e42012-01-30 14:53:22 +0000579void SkCanvas::flush() {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000580 SkBaseDevice* device = this->getDevice();
junov@chromium.orgbf6c1e42012-01-30 14:53:22 +0000581 if (device) {
582 device->flush();
583 }
584}
585
reed@google.com210ce002011-11-01 14:24:23 +0000586SkISize SkCanvas::getDeviceSize() const {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000587 SkBaseDevice* d = this->getDevice();
reed@google.com210ce002011-11-01 14:24:23 +0000588 return d ? SkISize::Make(d->width(), d->height()) : SkISize::Make(0, 0);
589}
590
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000591SkBaseDevice* SkCanvas::getDevice() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000592 // return root device
robertphillips@google.comc0290622012-07-16 21:20:03 +0000593 MCRec* rec = (MCRec*) fMCStack.front();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000594 SkASSERT(rec && rec->fLayer);
595 return rec->fLayer->fDevice;
596}
597
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000598SkBaseDevice* SkCanvas::getTopDevice(bool updateMatrixClip) const {
reed@google.com0b53d592012-03-19 18:26:34 +0000599 if (updateMatrixClip) {
600 const_cast<SkCanvas*>(this)->updateDeviceCMCache();
601 }
reed@google.com9266fed2011-03-30 00:18:03 +0000602 return fMCRec->fTopLayer->fDevice;
603}
604
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000605SkBaseDevice* SkCanvas::setDevice(SkBaseDevice* device) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000606 // return root device
reed@google.com4c09d5c2011-02-22 13:16:38 +0000607 SkDeque::F2BIter iter(fMCStack);
608 MCRec* rec = (MCRec*)iter.next();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000609 SkASSERT(rec && rec->fLayer);
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000610 SkBaseDevice* rootDevice = rec->fLayer->fDevice;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000611
612 if (rootDevice == device) {
613 return device;
614 }
reed@google.com4b226022011-01-11 18:32:13 +0000615
reed@android.com8a1c16f2008-12-17 15:59:43 +0000616 if (device) {
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000617 device->onAttachToCanvas(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000618 }
619 if (rootDevice) {
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000620 rootDevice->onDetachFromCanvas();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000621 }
622
623 SkRefCnt_SafeAssign(rec->fLayer->fDevice, device);
624 rootDevice = device;
625
626 fDeviceCMDirty = true;
reed@google.com4b226022011-01-11 18:32:13 +0000627
reed@android.com8a1c16f2008-12-17 15:59:43 +0000628 /* Now we update our initial region to have the bounds of the new device,
629 and then intersect all of the clips in our stack with these bounds,
630 to ensure that we can't draw outside of the device's bounds (and trash
631 memory).
reed@google.com4b226022011-01-11 18:32:13 +0000632
reed@android.com8a1c16f2008-12-17 15:59:43 +0000633 NOTE: this is only a partial-fix, since if the new device is larger than
634 the previous one, we don't know how to "enlarge" the clips in our stack,
reed@google.com4b226022011-01-11 18:32:13 +0000635 so drawing may be artificially restricted. Without keeping a history of
reed@android.com8a1c16f2008-12-17 15:59:43 +0000636 all calls to canvas->clipRect() and canvas->clipPath(), we can't exactly
637 reconstruct the correct clips, so this approximation will have to do.
638 The caller really needs to restore() back to the base if they want to
639 accurately take advantage of the new device bounds.
640 */
641
reed@google.com42aea282012-03-28 16:19:15 +0000642 SkIRect bounds;
643 if (device) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000644 bounds.set(0, 0, device->width(), device->height());
reed@google.com42aea282012-03-28 16:19:15 +0000645 } else {
646 bounds.setEmpty();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000647 }
reed@google.com42aea282012-03-28 16:19:15 +0000648 // now jam our 1st clip to be bounds, and intersect the rest with that
649 rec->fRasterClip->setRect(bounds);
650 while ((rec = (MCRec*)iter.next()) != NULL) {
651 (void)rec->fRasterClip->op(bounds, SkRegion::kIntersect_Op);
652 }
653
reed@android.com8a1c16f2008-12-17 15:59:43 +0000654 return device;
655}
656
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000657bool SkCanvas::readPixels(SkBitmap* bitmap,
658 int x, int y,
659 Config8888 config8888) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000660 SkBaseDevice* device = this->getDevice();
reed@google.com51df9e32010-12-23 19:29:18 +0000661 if (!device) {
662 return false;
663 }
bsalomon@google.com6850eab2011-11-03 20:29:47 +0000664 return device->readPixels(bitmap, x, y, config8888);
reed@google.com51df9e32010-12-23 19:29:18 +0000665}
666
bsalomon@google.comc6980972011-11-02 19:57:21 +0000667bool SkCanvas::readPixels(const SkIRect& srcRect, SkBitmap* bitmap) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000668 SkBaseDevice* device = this->getDevice();
djsollen@google.comccfee2a2012-05-01 16:50:10 +0000669 if (!device) {
670 return false;
671 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000672
bsalomon@google.comdaba14b2011-11-02 20:10:48 +0000673 SkIRect bounds;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000674 bounds.set(0, 0, device->width(), device->height());
bsalomon@google.comdaba14b2011-11-02 20:10:48 +0000675 if (!bounds.intersect(srcRect)) {
676 return false;
bsalomon@google.comc6980972011-11-02 19:57:21 +0000677 }
678
679 SkBitmap tmp;
680 tmp.setConfig(SkBitmap::kARGB_8888_Config, bounds.width(),
681 bounds.height());
682 if (this->readPixels(&tmp, bounds.fLeft, bounds.fTop)) {
683 bitmap->swap(tmp);
684 return true;
685 } else {
reed@google.com51df9e32010-12-23 19:29:18 +0000686 return false;
687 }
reed@google.com51df9e32010-12-23 19:29:18 +0000688}
689
bsalomon@google.comd58a1cd2011-11-10 20:57:43 +0000690void SkCanvas::writePixels(const SkBitmap& bitmap, int x, int y,
691 Config8888 config8888) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000692 SkBaseDevice* device = this->getDevice();
reed@google.com51df9e32010-12-23 19:29:18 +0000693 if (device) {
bsalomon@google.com405d0f42012-08-29 21:26:13 +0000694 if (SkIRect::Intersects(SkIRect::MakeSize(this->getDeviceSize()),
695 SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height()))) {
696 device->accessBitmap(true);
697 device->writePixels(bitmap, x, y, config8888);
698 }
reed@google.com51df9e32010-12-23 19:29:18 +0000699 }
700}
701
junov@google.com4370aed2012-01-18 16:21:08 +0000702SkCanvas* SkCanvas::canvasForDrawIter() {
703 return this;
704}
705
reed@android.com8a1c16f2008-12-17 15:59:43 +0000706//////////////////////////////////////////////////////////////////////////////
707
reed@android.com8a1c16f2008-12-17 15:59:43 +0000708void SkCanvas::updateDeviceCMCache() {
709 if (fDeviceCMDirty) {
710 const SkMatrix& totalMatrix = this->getTotalMatrix();
reed@google.com045e62d2011-10-24 12:19:46 +0000711 const SkRasterClip& totalClip = *fMCRec->fRasterClip;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000712 DeviceCM* layer = fMCRec->fTopLayer;
reed@google.com4b226022011-01-11 18:32:13 +0000713
reed@android.com8a1c16f2008-12-17 15:59:43 +0000714 if (NULL == layer->fNext) { // only one layer
reed@google.com46799cd2011-02-22 20:56:26 +0000715 layer->updateMC(totalMatrix, totalClip, fClipStack, NULL);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000716 } else {
reed@google.com045e62d2011-10-24 12:19:46 +0000717 SkRasterClip clip(totalClip);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000718 do {
reed@google.com46799cd2011-02-22 20:56:26 +0000719 layer->updateMC(totalMatrix, clip, fClipStack, &clip);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000720 } while ((layer = layer->fNext) != NULL);
721 }
722 fDeviceCMDirty = false;
723 }
724}
725
reed@android.com8a1c16f2008-12-17 15:59:43 +0000726///////////////////////////////////////////////////////////////////////////////
727
728int SkCanvas::internalSave(SaveFlags flags) {
729 int saveCount = this->getSaveCount(); // record this before the actual save
reed@google.com4b226022011-01-11 18:32:13 +0000730
reed@android.com8a1c16f2008-12-17 15:59:43 +0000731 MCRec* newTop = (MCRec*)fMCStack.push_back();
732 new (newTop) MCRec(fMCRec, flags); // balanced in restore()
reed@google.com4b226022011-01-11 18:32:13 +0000733
reed@android.com8a1c16f2008-12-17 15:59:43 +0000734 newTop->fNext = fMCRec;
735 fMCRec = newTop;
reed@google.com4b226022011-01-11 18:32:13 +0000736
commit-bot@chromium.org6c157642013-08-16 00:53:34 +0000737 if (SkCanvas::kClip_SaveFlag & flags) {
738 fClipStack.save();
739 }
reed@google.com5c3d1472011-02-22 19:12:23 +0000740
reed@android.com8a1c16f2008-12-17 15:59:43 +0000741 return saveCount;
742}
743
744int SkCanvas::save(SaveFlags flags) {
745 // call shared impl
746 return this->internalSave(flags);
747}
748
749#define C32MASK (1 << SkBitmap::kARGB_8888_Config)
750#define C16MASK (1 << SkBitmap::kRGB_565_Config)
751#define C8MASK (1 << SkBitmap::kA8_Config)
752
753static SkBitmap::Config resolve_config(SkCanvas* canvas,
754 const SkIRect& bounds,
755 SkCanvas::SaveFlags flags,
756 bool* isOpaque) {
757 *isOpaque = (flags & SkCanvas::kHasAlphaLayer_SaveFlag) == 0;
758
759#if 0
760 // loop through and union all the configs we may draw into
761 uint32_t configMask = 0;
762 for (int i = canvas->countLayerDevices() - 1; i >= 0; --i)
763 {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000764 SkBaseDevice* device = canvas->getLayerDevice(i);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000765 if (device->intersects(bounds))
766 configMask |= 1 << device->config();
767 }
768
769 // if the caller wants alpha or fullcolor, we can't return 565
770 if (flags & (SkCanvas::kFullColorLayer_SaveFlag |
771 SkCanvas::kHasAlphaLayer_SaveFlag))
772 configMask &= ~C16MASK;
773
774 switch (configMask) {
775 case C8MASK: // if we only have A8, return that
776 return SkBitmap::kA8_Config;
777
778 case C16MASK: // if we only have 565, return that
779 return SkBitmap::kRGB_565_Config;
780
781 default:
782 return SkBitmap::kARGB_8888_Config; // default answer
783 }
784#else
785 return SkBitmap::kARGB_8888_Config; // default answer
786#endif
787}
788
789static bool bounds_affects_clip(SkCanvas::SaveFlags flags) {
790 return (flags & SkCanvas::kClipToLayer_SaveFlag) != 0;
791}
792
junov@chromium.orga907ac32012-02-24 21:54:07 +0000793bool SkCanvas::clipRectBounds(const SkRect* bounds, SaveFlags flags,
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +0000794 SkIRect* intersection, const SkImageFilter* imageFilter) {
tomhudson@google.combcb671c2011-09-13 15:07:58 +0000795 SkIRect clipBounds;
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +0000796 SkRegion::Op op = SkRegion::kIntersect_Op;
tomhudson@google.combcb671c2011-09-13 15:07:58 +0000797 if (!this->getClipDeviceBounds(&clipBounds)) {
junov@chromium.orga907ac32012-02-24 21:54:07 +0000798 return false;
reed@android.comf2b98d62010-12-20 18:26:13 +0000799 }
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +0000800
801 if (imageFilter) {
802 imageFilter->filterBounds(clipBounds, *fMCRec->fMatrix, &clipBounds);
803 // Filters may grow the bounds beyond the device bounds.
804 op = SkRegion::kReplace_Op;
805 }
tomhudson@google.combcb671c2011-09-13 15:07:58 +0000806 SkIRect ir;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000807 if (NULL != bounds) {
808 SkRect r;
reed@google.com4b226022011-01-11 18:32:13 +0000809
reed@android.com8a1c16f2008-12-17 15:59:43 +0000810 this->getTotalMatrix().mapRect(&r, *bounds);
811 r.roundOut(&ir);
812 // early exit if the layer's bounds are clipped out
813 if (!ir.intersect(clipBounds)) {
tomhudson@google.combcb671c2011-09-13 15:07:58 +0000814 if (bounds_affects_clip(flags)) {
reed@google.com00177082011-10-12 14:34:30 +0000815 fMCRec->fRasterClip->setEmpty();
tomhudson@google.combcb671c2011-09-13 15:07:58 +0000816 }
junov@chromium.orga907ac32012-02-24 21:54:07 +0000817 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000818 }
819 } else { // no user bounds, so just use the clip
820 ir = clipBounds;
821 }
822
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +0000823 fClipStack.clipDevRect(ir, op);
junov@chromium.orga907ac32012-02-24 21:54:07 +0000824
reed@android.com8a1c16f2008-12-17 15:59:43 +0000825 // early exit if the clip is now empty
826 if (bounds_affects_clip(flags) &&
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +0000827 !fMCRec->fRasterClip->op(ir, op)) {
junov@chromium.orga907ac32012-02-24 21:54:07 +0000828 return false;
829 }
830
831 if (intersection) {
832 *intersection = ir;
833 }
834 return true;
835}
836
837int SkCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint,
838 SaveFlags flags) {
reed@google.com8926b162012-03-23 15:36:36 +0000839 return this->internalSaveLayer(bounds, paint, flags, false);
840}
841
reed@google.com76f10a32014-02-05 15:32:21 +0000842static SkBaseDevice* createCompatibleDevice(SkCanvas* canvas,
843 SkBitmap::Config config,
844 int width, int height,
845 bool isOpaque) {
846 SkBaseDevice* device = canvas->getDevice();
847 if (device) {
848 return device->createCompatibleDevice(config, width, height, isOpaque);
849 } else {
850 return NULL;
851 }
852}
853
854#ifdef SK_SUPPORT_LEGACY_CANVAS_CREATECOMPATIBLEDEVICE
855SkBaseDevice* SkCanvas::createCompatibleDevice(SkBitmap::Config config,
856 int width, int height,
857 bool isOpaque) {
858 return createCompatibleDevice(this, config, width, height, isOpaque);
859}
860#endif
861
reed@google.com8926b162012-03-23 15:36:36 +0000862int SkCanvas::internalSaveLayer(const SkRect* bounds, const SkPaint* paint,
863 SaveFlags flags, bool justForImageFilter) {
junov@chromium.orga907ac32012-02-24 21:54:07 +0000864 // do this before we create the layer. We don't call the public save() since
865 // that would invoke a possibly overridden virtual
866 int count = this->internalSave(flags);
867
868 fDeviceCMDirty = true;
869
870 SkIRect ir;
senorblanco@chromium.orgc4b12f12014-02-05 17:51:22 +0000871 if (!this->clipRectBounds(bounds, flags, &ir, paint ? paint->getImageFilter() : NULL)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000872 return count;
873 }
874
reed@google.comb55deeb2012-01-06 14:43:09 +0000875 // Kill the imagefilter if our device doesn't allow it
876 SkLazyPaint lazyP;
877 if (paint && paint->getImageFilter()) {
878 if (!this->getTopDevice()->allowImageFilter(paint->getImageFilter())) {
reed@google.com8926b162012-03-23 15:36:36 +0000879 if (justForImageFilter) {
880 // early exit if the layer was just for the imageFilter
881 return count;
882 }
reed@google.comb55deeb2012-01-06 14:43:09 +0000883 SkPaint* p = lazyP.set(*paint);
884 p->setImageFilter(NULL);
885 paint = p;
886 }
887 }
888
reed@android.com8a1c16f2008-12-17 15:59:43 +0000889 bool isOpaque;
890 SkBitmap::Config config = resolve_config(this, ir, flags, &isOpaque);
891
robertphillips@google.com1f2f3382013-08-29 11:54:56 +0000892 SkBaseDevice* device;
reed@google.com76dd2772012-01-05 21:15:07 +0000893 if (paint && paint->getImageFilter()) {
reed@google.com76f10a32014-02-05 15:32:21 +0000894 device = createCompatibleDevice(this, config, ir.width(), ir.height(),
895 isOpaque);
reed@google.com76dd2772012-01-05 21:15:07 +0000896 } else {
897 device = this->createLayerDevice(config, ir.width(), ir.height(),
898 isOpaque);
899 }
bungeman@google.come25c6842011-08-17 14:53:54 +0000900 if (NULL == device) {
901 SkDebugf("Unable to create device for layer.");
902 return count;
903 }
bsalomon@google.come97f0852011-06-17 13:10:25 +0000904
reed@google.com6f8f2922011-03-04 22:27:10 +0000905 device->setOrigin(ir.fLeft, ir.fTop);
robertphillips@google.com40a1ae42012-07-13 15:36:15 +0000906 DeviceCM* layer = SkNEW_ARGS(DeviceCM, (device, ir.fLeft, ir.fTop, paint, this));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000907 device->unref();
908
909 layer->fNext = fMCRec->fTopLayer;
910 fMCRec->fLayer = layer;
911 fMCRec->fTopLayer = layer; // this field is NOT an owner of layer
912
junov@chromium.orgb0a7ace2012-04-05 18:33:23 +0000913 fSaveLayerCount += 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000914 return count;
915}
916
917int SkCanvas::saveLayerAlpha(const SkRect* bounds, U8CPU alpha,
918 SaveFlags flags) {
919 if (0xFF == alpha) {
920 return this->saveLayer(bounds, NULL, flags);
921 } else {
922 SkPaint tmpPaint;
923 tmpPaint.setAlpha(alpha);
924 return this->saveLayer(bounds, &tmpPaint, flags);
925 }
926}
927
928void SkCanvas::restore() {
929 // check for underflow
930 if (fMCStack.count() > 1) {
931 this->internalRestore();
932 }
933}
934
935void SkCanvas::internalRestore() {
936 SkASSERT(fMCStack.count() != 0);
937
938 fDeviceCMDirty = true;
reed@google.comc0784db2013-12-13 21:16:12 +0000939 fCachedLocalClipBoundsDirty = true;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000940
commit-bot@chromium.org6c157642013-08-16 00:53:34 +0000941 if (SkCanvas::kClip_SaveFlag & fMCRec->fFlags) {
942 fClipStack.restore();
943 }
944
bungeman@google.com88edf1e2011-08-08 19:41:56 +0000945 // reserve our layer (if any)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000946 DeviceCM* layer = fMCRec->fLayer; // may be null
947 // now detach it from fMCRec so we can pop(). Gets freed after its drawn
948 fMCRec->fLayer = NULL;
949
950 // now do the normal restore()
951 fMCRec->~MCRec(); // balanced in save()
952 fMCStack.pop_back();
953 fMCRec = (MCRec*)fMCStack.back();
954
955 /* Time to draw the layer's offscreen. We can't call the public drawSprite,
956 since if we're being recorded, we don't want to record this (the
957 recorder will have already recorded the restore).
958 */
959 if (NULL != layer) {
960 if (layer->fNext) {
reed@google.com6f8f2922011-03-04 22:27:10 +0000961 const SkIPoint& origin = layer->fDevice->getOrigin();
reed@google.com8926b162012-03-23 15:36:36 +0000962 this->internalDrawDevice(layer->fDevice, origin.x(), origin.y(),
963 layer->fPaint);
964 // reset this, since internalDrawDevice will have set it to true
reed@android.com8a1c16f2008-12-17 15:59:43 +0000965 fDeviceCMDirty = true;
reed@google.com7c202932011-12-14 18:48:05 +0000966
junov@chromium.orgb0a7ace2012-04-05 18:33:23 +0000967 SkASSERT(fSaveLayerCount > 0);
968 fSaveLayerCount -= 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000969 }
970 SkDELETE(layer);
bungeman@google.com88edf1e2011-08-08 19:41:56 +0000971 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000972}
973
974int SkCanvas::getSaveCount() const {
975 return fMCStack.count();
976}
977
978void SkCanvas::restoreToCount(int count) {
979 // sanity check
980 if (count < 1) {
981 count = 1;
982 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +0000983
reed@google.comb9d1c6a2011-11-28 16:09:24 +0000984 int n = this->getSaveCount() - count;
985 for (int i = 0; i < n; ++i) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000986 this->restore();
987 }
988}
989
reed@google.com7c202932011-12-14 18:48:05 +0000990bool SkCanvas::isDrawingToLayer() const {
junov@chromium.orgb0a7ace2012-04-05 18:33:23 +0000991 return fSaveLayerCount > 0;
reed@google.com7c202932011-12-14 18:48:05 +0000992}
993
reed@google.com76f10a32014-02-05 15:32:21 +0000994SkSurface* SkCanvas::newSurface(const SkImageInfo& info) {
995 return this->onNewSurface(info);
996}
997
998SkSurface* SkCanvas::onNewSurface(const SkImageInfo& info) {
999 SkBaseDevice* dev = this->getDevice();
1000 return dev ? dev->newSurface(info) : NULL;
1001}
1002
reed@android.com8a1c16f2008-12-17 15:59:43 +00001003/////////////////////////////////////////////////////////////////////////////
1004
1005// can't draw it if its empty, or its too big for a fixed-point width or height
1006static bool reject_bitmap(const SkBitmap& bitmap) {
reed@google.com28534292013-07-23 22:03:26 +00001007 return bitmap.width() <= 0 || bitmap.height() <= 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001008}
1009
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001010void SkCanvas::internalDrawBitmap(const SkBitmap& bitmap,
reed@android.com8a1c16f2008-12-17 15:59:43 +00001011 const SkMatrix& matrix, const SkPaint* paint) {
1012 if (reject_bitmap(bitmap)) {
1013 return;
1014 }
1015
mike@reedtribe.org2c8fc5a2011-04-10 00:35:29 +00001016 SkLazyPaint lazy;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001017 if (NULL == paint) {
mike@reedtribe.org2c8fc5a2011-04-10 00:35:29 +00001018 paint = lazy.init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001019 }
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001020
1021 SkDEBUGCODE(bitmap.validate();)
1022 CHECK_LOCKCOUNT_BALANCE(bitmap);
1023
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00001024 SkRect storage;
1025 const SkRect* bounds = NULL;
1026 if (paint && paint->canComputeFastBounds()) {
1027 bitmap.getBounds(&storage);
1028 matrix.mapRect(&storage);
1029 bounds = &paint->computeFastBounds(storage, &storage);
1030 }
1031
1032 LOOPER_BEGIN(*paint, SkDrawFilter::kBitmap_Type, bounds)
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001033
1034 while (iter.next()) {
1035 iter.fDevice->drawBitmap(iter, bitmap, matrix, looper.paint());
1036 }
1037
1038 LOOPER_END
reed@android.com8a1c16f2008-12-17 15:59:43 +00001039}
1040
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001041void SkCanvas::internalDrawDevice(SkBaseDevice* srcDev, int x, int y,
reed@google.com8926b162012-03-23 15:36:36 +00001042 const SkPaint* paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001043 SkPaint tmp;
1044 if (NULL == paint) {
1045 tmp.setDither(true);
1046 paint = &tmp;
1047 }
reed@google.com4b226022011-01-11 18:32:13 +00001048
reed@google.com8926b162012-03-23 15:36:36 +00001049 LOOPER_BEGIN_DRAWDEVICE(*paint, SkDrawFilter::kBitmap_Type)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001050 while (iter.next()) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001051 SkBaseDevice* dstDev = iter.fDevice;
reed@google.com76dd2772012-01-05 21:15:07 +00001052 paint = &looper.paint();
1053 SkImageFilter* filter = paint->getImageFilter();
1054 SkIPoint pos = { x - iter.getX(), y - iter.getY() };
reed@google.com8926b162012-03-23 15:36:36 +00001055 if (filter && !dstDev->canHandleImageFilter(filter)) {
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001056 SkDeviceImageFilterProxy proxy(dstDev);
reed@google.com76dd2772012-01-05 21:15:07 +00001057 SkBitmap dst;
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001058 SkIPoint offset = SkIPoint::Make(0, 0);
reed@google.comb55deeb2012-01-06 14:43:09 +00001059 const SkBitmap& src = srcDev->accessBitmap(false);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +00001060 SkMatrix matrix = *iter.fMatrix;
1061 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001062 if (filter->filterImage(&proxy, src, matrix, &dst, &offset)) {
tomhudson@google.com5efe0cb2012-04-10 19:14:48 +00001063 SkPaint tmpUnfiltered(*paint);
1064 tmpUnfiltered.setImageFilter(NULL);
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001065 dstDev->drawSprite(iter, dst, pos.x() + offset.x(), pos.y() + offset.y(),
1066 tmpUnfiltered);
reed@google.com76dd2772012-01-05 21:15:07 +00001067 }
1068 } else {
reed@google.comb55deeb2012-01-06 14:43:09 +00001069 dstDev->drawDevice(iter, srcDev, pos.x(), pos.y(), *paint);
reed@google.com76dd2772012-01-05 21:15:07 +00001070 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001071 }
reed@google.com4e2b3d32011-04-07 14:18:59 +00001072 LOOPER_END
reed@android.com8a1c16f2008-12-17 15:59:43 +00001073}
1074
reed@google.com8926b162012-03-23 15:36:36 +00001075void SkCanvas::drawSprite(const SkBitmap& bitmap, int x, int y,
1076 const SkPaint* paint) {
1077 SkDEBUGCODE(bitmap.validate();)
reed@google.comea033602012-12-14 13:13:55 +00001078 CHECK_LOCKCOUNT_BALANCE(bitmap);
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001079
reed@google.com8926b162012-03-23 15:36:36 +00001080 if (reject_bitmap(bitmap)) {
1081 return;
1082 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001083
reed@google.com8926b162012-03-23 15:36:36 +00001084 SkPaint tmp;
1085 if (NULL == paint) {
1086 paint = &tmp;
1087 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001088
reed@google.com8926b162012-03-23 15:36:36 +00001089 LOOPER_BEGIN_DRAWDEVICE(*paint, SkDrawFilter::kBitmap_Type)
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001090
reed@google.com8926b162012-03-23 15:36:36 +00001091 while (iter.next()) {
1092 paint = &looper.paint();
1093 SkImageFilter* filter = paint->getImageFilter();
1094 SkIPoint pos = { x - iter.getX(), y - iter.getY() };
1095 if (filter && !iter.fDevice->canHandleImageFilter(filter)) {
senorblanco@chromium.org9c397442012-09-27 21:57:45 +00001096 SkDeviceImageFilterProxy proxy(iter.fDevice);
reed@google.com8926b162012-03-23 15:36:36 +00001097 SkBitmap dst;
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001098 SkIPoint offset = SkIPoint::Make(0, 0);
senorblanco@chromium.orgfbaea532013-08-27 21:37:01 +00001099 SkMatrix matrix = *iter.fMatrix;
1100 matrix.postTranslate(SkIntToScalar(-x), SkIntToScalar(-y));
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001101 if (filter->filterImage(&proxy, bitmap, matrix, &dst, &offset)) {
tomhudson@google.com5efe0cb2012-04-10 19:14:48 +00001102 SkPaint tmpUnfiltered(*paint);
1103 tmpUnfiltered.setImageFilter(NULL);
senorblanco@chromium.org6776b822014-01-03 21:48:22 +00001104 iter.fDevice->drawSprite(iter, dst, pos.x() + offset.x(), pos.y() + offset.y(),
tomhudson@google.com5efe0cb2012-04-10 19:14:48 +00001105 tmpUnfiltered);
reed@google.com8926b162012-03-23 15:36:36 +00001106 }
1107 } else {
1108 iter.fDevice->drawSprite(iter, bitmap, pos.x(), pos.y(), *paint);
1109 }
1110 }
1111 LOOPER_END
1112}
1113
reed@android.com8a1c16f2008-12-17 15:59:43 +00001114/////////////////////////////////////////////////////////////////////////////
1115
1116bool SkCanvas::translate(SkScalar dx, SkScalar dy) {
1117 fDeviceCMDirty = true;
reed@google.comc0784db2013-12-13 21:16:12 +00001118 fCachedLocalClipBoundsDirty = true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001119 return fMCRec->fMatrix->preTranslate(dx, dy);
1120}
1121
1122bool SkCanvas::scale(SkScalar sx, SkScalar sy) {
1123 fDeviceCMDirty = true;
reed@google.comc0784db2013-12-13 21:16:12 +00001124 fCachedLocalClipBoundsDirty = true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001125 return fMCRec->fMatrix->preScale(sx, sy);
1126}
1127
1128bool SkCanvas::rotate(SkScalar degrees) {
1129 fDeviceCMDirty = true;
reed@google.comc0784db2013-12-13 21:16:12 +00001130 fCachedLocalClipBoundsDirty = true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001131 return fMCRec->fMatrix->preRotate(degrees);
1132}
1133
1134bool SkCanvas::skew(SkScalar sx, SkScalar sy) {
1135 fDeviceCMDirty = true;
reed@google.comc0784db2013-12-13 21:16:12 +00001136 fCachedLocalClipBoundsDirty = true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001137 return fMCRec->fMatrix->preSkew(sx, sy);
1138}
1139
1140bool SkCanvas::concat(const SkMatrix& matrix) {
1141 fDeviceCMDirty = true;
reed@google.comc0784db2013-12-13 21:16:12 +00001142 fCachedLocalClipBoundsDirty = true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001143 return fMCRec->fMatrix->preConcat(matrix);
1144}
1145
1146void SkCanvas::setMatrix(const SkMatrix& matrix) {
1147 fDeviceCMDirty = true;
reed@google.comc0784db2013-12-13 21:16:12 +00001148 fCachedLocalClipBoundsDirty = true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001149 *fMCRec->fMatrix = matrix;
1150}
1151
1152// this is not virtual, so it must call a virtual method so that subclasses
1153// will see its action
1154void SkCanvas::resetMatrix() {
1155 SkMatrix matrix;
reed@google.com4b226022011-01-11 18:32:13 +00001156
reed@android.com8a1c16f2008-12-17 15:59:43 +00001157 matrix.reset();
1158 this->setMatrix(matrix);
1159}
1160
1161//////////////////////////////////////////////////////////////////////////////
1162
reed@google.comc42d35d2011-10-12 11:57:42 +00001163bool SkCanvas::clipRect(const SkRect& rect, SkRegion::Op op, bool doAA) {
reed@google.comda17f752012-08-16 18:27:05 +00001164#ifdef SK_ENABLE_CLIP_QUICKREJECT
1165 if (SkRegion::kIntersect_Op == op) {
1166 if (fMCRec->fRasterClip->isEmpty()) {
1167 return false;
1168 }
1169
reed@google.com3b3e8952012-08-16 20:53:31 +00001170 if (this->quickReject(rect)) {
reed@google.comda17f752012-08-16 18:27:05 +00001171 fDeviceCMDirty = true;
reed@google.comc0784db2013-12-13 21:16:12 +00001172 fCachedLocalClipBoundsDirty = true;
reed@google.comda17f752012-08-16 18:27:05 +00001173
1174 fClipStack.clipEmpty();
1175 return fMCRec->fRasterClip->setEmpty();
1176 }
1177 }
1178#endif
1179
reed@google.com5c3d1472011-02-22 19:12:23 +00001180 AutoValidateClip avc(this);
1181
reed@android.com8a1c16f2008-12-17 15:59:43 +00001182 fDeviceCMDirty = true;
reed@google.comc0784db2013-12-13 21:16:12 +00001183 fCachedLocalClipBoundsDirty = true;
caryclark@google.com8f0a7b82012-11-07 14:54:49 +00001184 doAA &= fAllowSoftClip;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001185
1186 if (fMCRec->fMatrix->rectStaysRect()) {
robertphillips@google.com12367192013-10-20 13:11:16 +00001187 // for these simpler matrices, we can stay a rect even after applying
reed@android.com98de2bd2009-03-02 19:41:36 +00001188 // the matrix. This means we don't have to a) make a path, and b) tell
1189 // the region code to scan-convert the path, only to discover that it
1190 // is really just a rect.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001191 SkRect r;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001192
1193 fMCRec->fMatrix->mapRect(&r, rect);
reed@google.com00177082011-10-12 14:34:30 +00001194 fClipStack.clipDevRect(r, op, doAA);
1195 return fMCRec->fRasterClip->op(r, op, doAA);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001196 } else {
robertphillips@google.com12367192013-10-20 13:11:16 +00001197 // since we're rotated or some such thing, we convert the rect to a path
reed@android.com98de2bd2009-03-02 19:41:36 +00001198 // and clip against that, since it can handle any matrix. However, to
1199 // avoid recursion in the case where we are subclassed (e.g. Pictures)
1200 // we explicitly call "our" version of clipPath.
reed@android.com8a1c16f2008-12-17 15:59:43 +00001201 SkPath path;
1202
1203 path.addRect(rect);
reed@google.comc42d35d2011-10-12 11:57:42 +00001204 return this->SkCanvas::clipPath(path, op, doAA);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001205 }
1206}
1207
reed@google.com00177082011-10-12 14:34:30 +00001208static bool clipPathHelper(const SkCanvas* canvas, SkRasterClip* currClip,
reed@google.comc42d35d2011-10-12 11:57:42 +00001209 const SkPath& devPath, SkRegion::Op op, bool doAA) {
reed@google.com759876a2011-03-03 14:32:51 +00001210 // base is used to limit the size (and therefore memory allocation) of the
1211 // region that results from scan converting devPath.
1212 SkRegion base;
1213
reed@google.com819c9212011-02-23 18:56:55 +00001214 if (SkRegion::kIntersect_Op == op) {
reed@google.com759876a2011-03-03 14:32:51 +00001215 // since we are intersect, we can do better (tighter) with currRgn's
1216 // bounds, than just using the device. However, if currRgn is complex,
1217 // our region blitter may hork, so we do that case in two steps.
reed@google.com00177082011-10-12 14:34:30 +00001218 if (currClip->isRect()) {
commit-bot@chromium.orgb446fc72013-07-10 20:55:39 +00001219 // FIXME: we should also be able to do this when currClip->isBW(),
1220 // but relaxing the test above triggers GM asserts in
1221 // SkRgnBuilder::blitH(). We need to investigate what's going on.
1222 return currClip->setPath(devPath, currClip->bwRgn(), doAA);
reed@google.com759876a2011-03-03 14:32:51 +00001223 } else {
reed@google.com00177082011-10-12 14:34:30 +00001224 base.setRect(currClip->getBounds());
1225 SkRasterClip clip;
1226 clip.setPath(devPath, base, doAA);
1227 return currClip->op(clip, op);
reed@google.com759876a2011-03-03 14:32:51 +00001228 }
reed@google.com819c9212011-02-23 18:56:55 +00001229 } else {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001230 const SkBaseDevice* device = canvas->getDevice();
djsollen@google.comccfee2a2012-05-01 16:50:10 +00001231 if (!device) {
1232 return currClip->setEmpty();
1233 }
1234
junov@chromium.orga907ac32012-02-24 21:54:07 +00001235 base.setRect(0, 0, device->width(), device->height());
reed@google.com819c9212011-02-23 18:56:55 +00001236
1237 if (SkRegion::kReplace_Op == op) {
reed@google.com00177082011-10-12 14:34:30 +00001238 return currClip->setPath(devPath, base, doAA);
reed@google.com819c9212011-02-23 18:56:55 +00001239 } else {
reed@google.com00177082011-10-12 14:34:30 +00001240 SkRasterClip clip;
1241 clip.setPath(devPath, base, doAA);
1242 return currClip->op(clip, op);
reed@google.com819c9212011-02-23 18:56:55 +00001243 }
1244 }
1245}
1246
reed@google.com4ed0fb72012-12-12 20:48:18 +00001247bool SkCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) {
1248 if (rrect.isRect()) {
1249 // call the non-virtual version
1250 return this->SkCanvas::clipRect(rrect.getBounds(), op, doAA);
1251 } else {
1252 SkPath path;
1253 path.addRRect(rrect);
1254 // call the non-virtual version
1255 return this->SkCanvas::clipPath(path, op, doAA);
1256 }
1257}
1258
reed@google.comc42d35d2011-10-12 11:57:42 +00001259bool SkCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) {
reed@google.comda17f752012-08-16 18:27:05 +00001260#ifdef SK_ENABLE_CLIP_QUICKREJECT
1261 if (SkRegion::kIntersect_Op == op && !path.isInverseFillType()) {
1262 if (fMCRec->fRasterClip->isEmpty()) {
1263 return false;
1264 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001265
reed@google.com3b3e8952012-08-16 20:53:31 +00001266 if (this->quickReject(path.getBounds())) {
reed@google.comda17f752012-08-16 18:27:05 +00001267 fDeviceCMDirty = true;
reed@google.comc0784db2013-12-13 21:16:12 +00001268 fCachedLocalClipBoundsDirty = true;
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001269
reed@google.comda17f752012-08-16 18:27:05 +00001270 fClipStack.clipEmpty();
1271 return fMCRec->fRasterClip->setEmpty();
1272 }
1273 }
1274#endif
1275
reed@google.com5c3d1472011-02-22 19:12:23 +00001276 AutoValidateClip avc(this);
1277
reed@android.com8a1c16f2008-12-17 15:59:43 +00001278 fDeviceCMDirty = true;
reed@google.comc0784db2013-12-13 21:16:12 +00001279 fCachedLocalClipBoundsDirty = true;
caryclark@google.com8f0a7b82012-11-07 14:54:49 +00001280 doAA &= fAllowSoftClip;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001281
1282 SkPath devPath;
1283 path.transform(*fMCRec->fMatrix, &devPath);
1284
reed@google.comfe701122011-11-08 19:41:23 +00001285 // Check if the transfomation, or the original path itself
1286 // made us empty. Note this can also happen if we contained NaN
1287 // values. computing the bounds detects this, and will set our
1288 // bounds to empty if that is the case. (see SkRect::set(pts, count))
1289 if (devPath.getBounds().isEmpty()) {
1290 // resetting the path will remove any NaN or other wanky values
1291 // that might upset our scan converter.
1292 devPath.reset();
1293 }
1294
reed@google.com5c3d1472011-02-22 19:12:23 +00001295 // if we called path.swap() we could avoid a deep copy of this path
reed@google.com00177082011-10-12 14:34:30 +00001296 fClipStack.clipDevPath(devPath, op, doAA);
reed@google.com5c3d1472011-02-22 19:12:23 +00001297
caryclark@google.com45a75fb2013-04-25 13:34:40 +00001298 if (fAllowSimplifyClip) {
1299 devPath.reset();
1300 devPath.setFillType(SkPath::kInverseEvenOdd_FillType);
1301 const SkClipStack* clipStack = getClipStack();
1302 SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart);
1303 const SkClipStack::Element* element;
1304 while ((element = iter.next())) {
1305 SkClipStack::Element::Type type = element->getType();
1306 if (type == SkClipStack::Element::kEmpty_Type) {
1307 continue;
1308 }
1309 SkPath operand;
1310 if (type == SkClipStack::Element::kRect_Type) {
1311 operand.addRect(element->getRect());
1312 } else if (type == SkClipStack::Element::kPath_Type) {
1313 operand = element->getPath();
1314 } else {
1315 SkDEBUGFAIL("Unexpected type.");
1316 }
1317 SkRegion::Op elementOp = element->getOp();
1318 if (elementOp == SkRegion::kReplace_Op) {
1319 devPath = operand;
1320 } else {
1321 Op(devPath, operand, (SkPathOp) elementOp, &devPath);
1322 }
caryclark@google.com96fd3442013-05-07 19:48:31 +00001323 // if the prev and curr clips disagree about aa -vs- not, favor the aa request.
1324 // perhaps we need an API change to avoid this sort of mixed-signals about
1325 // clipping.
1326 doAA |= element->isAA();
caryclark@google.com45a75fb2013-04-25 13:34:40 +00001327 }
1328 op = SkRegion::kReplace_Op;
1329 }
1330
reed@google.com00177082011-10-12 14:34:30 +00001331 return clipPathHelper(this, fMCRec->fRasterClip, devPath, op, doAA);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001332}
1333
junov@chromium.orged8d6bb2013-05-29 19:09:48 +00001334bool SkCanvas::updateClipConservativelyUsingBounds(const SkRect& bounds, SkRegion::Op op,
1335 bool inverseFilled) {
1336 // This is for updating the clip conservatively using only bounds
1337 // information.
1338 // Contract:
1339 // The current clip must contain the true clip. The true
1340 // clip is the clip that would have normally been computed
1341 // by calls to clipPath and clipRRect
1342 // Objective:
1343 // Keep the current clip as small as possible without
1344 // breaking the contract, using only clip bounding rectangles
1345 // (for performance).
1346
1347 // N.B.: This *never* calls back through a virtual on canvas, so subclasses
1348 // don't have to worry about getting caught in a loop. Thus anywhere
1349 // we call a virtual method, we explicitly prefix it with
1350 // SkCanvas:: to be sure to call the base-class.
skia.committer@gmail.coma5d3e772013-05-30 07:01:29 +00001351
junov@chromium.orged8d6bb2013-05-29 19:09:48 +00001352 if (inverseFilled) {
1353 switch (op) {
1354 case SkRegion::kIntersect_Op:
1355 case SkRegion::kDifference_Op:
1356 // These ops can only shrink the current clip. So leaving
1357 // the clip unchanges conservatively respects the contract.
1358 return this->getClipDeviceBounds(NULL);
1359 case SkRegion::kUnion_Op:
1360 case SkRegion::kReplace_Op:
1361 case SkRegion::kReverseDifference_Op:
1362 case SkRegion::kXOR_Op:
1363 {
1364 // These ops can grow the current clip up to the extents of
1365 // the input clip, which is inverse filled, so we just set
1366 // the current clip to the device bounds.
1367 SkRect deviceBounds;
1368 SkIRect deviceIBounds;
1369 this->getDevice()->getGlobalBounds(&deviceIBounds);
reed@google.com44699382013-10-31 17:28:30 +00001370 deviceBounds = SkRect::Make(deviceIBounds);
junov@chromium.orged8d6bb2013-05-29 19:09:48 +00001371 this->SkCanvas::save(SkCanvas::kMatrix_SaveFlag);
1372 // set the clip in device space
1373 this->SkCanvas::setMatrix(SkMatrix::I());
1374 bool result = this->SkCanvas::clipRect(deviceBounds,
1375 SkRegion::kReplace_Op, false);
1376 this->SkCanvas::restore(); //pop the matrix, but keep the clip
1377 return result;
1378 }
1379 default:
1380 SkASSERT(0); // unhandled op?
1381 }
1382 } else {
1383 // Not inverse filled
1384 switch (op) {
1385 case SkRegion::kIntersect_Op:
1386 case SkRegion::kUnion_Op:
1387 case SkRegion::kReplace_Op:
1388 return this->SkCanvas::clipRect(bounds, op, false);
1389 case SkRegion::kDifference_Op:
1390 // Difference can only shrink the current clip.
1391 // Leaving clip unchanged conservatively fullfills the contract.
1392 return this->getClipDeviceBounds(NULL);
1393 case SkRegion::kReverseDifference_Op:
1394 // To reverse, we swap in the bounds with a replace op.
1395 // As with difference, leave it unchanged.
1396 return this->SkCanvas::clipRect(bounds, SkRegion::kReplace_Op, false);
1397 case SkRegion::kXOR_Op:
1398 // Be conservative, based on (A XOR B) always included in (A union B),
1399 // which is always included in (bounds(A) union bounds(B))
1400 return this->SkCanvas::clipRect(bounds, SkRegion::kUnion_Op, false);
1401 default:
1402 SkASSERT(0); // unhandled op?
1403 }
1404 }
1405 return true;
1406}
1407
reed@android.com8a1c16f2008-12-17 15:59:43 +00001408bool SkCanvas::clipRegion(const SkRegion& rgn, SkRegion::Op op) {
reed@google.com5c3d1472011-02-22 19:12:23 +00001409 AutoValidateClip avc(this);
1410
reed@android.com8a1c16f2008-12-17 15:59:43 +00001411 fDeviceCMDirty = true;
reed@google.comc0784db2013-12-13 21:16:12 +00001412 fCachedLocalClipBoundsDirty = true;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001413
reed@google.com5c3d1472011-02-22 19:12:23 +00001414 // todo: signal fClipStack that we have a region, and therefore (I guess)
1415 // we have to ignore it, and use the region directly?
reed@google.com115d9312012-05-16 18:50:40 +00001416 fClipStack.clipDevRect(rgn.getBounds(), op);
reed@google.com5c3d1472011-02-22 19:12:23 +00001417
reed@google.com00177082011-10-12 14:34:30 +00001418 return fMCRec->fRasterClip->op(rgn, op);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001419}
1420
reed@google.com819c9212011-02-23 18:56:55 +00001421#ifdef SK_DEBUG
1422void SkCanvas::validateClip() const {
1423 // construct clipRgn from the clipstack
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001424 const SkBaseDevice* device = this->getDevice();
djsollen@google.comccfee2a2012-05-01 16:50:10 +00001425 if (!device) {
1426 SkASSERT(this->getTotalClip().isEmpty());
1427 return;
1428 }
1429
reed@google.com819c9212011-02-23 18:56:55 +00001430 SkIRect ir;
1431 ir.set(0, 0, device->width(), device->height());
reed@google.com00177082011-10-12 14:34:30 +00001432 SkRasterClip tmpClip(ir);
reed@google.com819c9212011-02-23 18:56:55 +00001433
robertphillips@google.com80214e22012-07-20 15:33:18 +00001434 SkClipStack::B2TIter iter(fClipStack);
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001435 const SkClipStack::Element* element;
1436 while ((element = iter.next()) != NULL) {
1437 switch (element->getType()) {
1438 case SkClipStack::Element::kPath_Type:
1439 clipPathHelper(this,
1440 &tmpClip,
1441 element->getPath(),
1442 element->getOp(),
1443 element->isAA());
1444 break;
1445 case SkClipStack::Element::kRect_Type:
1446 element->getRect().round(&ir);
1447 tmpClip.op(ir, element->getOp());
1448 break;
1449 case SkClipStack::Element::kEmpty_Type:
1450 tmpClip.setEmpty();
1451 break;
reed@google.com819c9212011-02-23 18:56:55 +00001452 }
1453 }
1454
reed@google.com6f8f2922011-03-04 22:27:10 +00001455#if 0 // enable this locally for testing
reed@google.com819c9212011-02-23 18:56:55 +00001456 // now compare against the current rgn
1457 const SkRegion& rgn = this->getTotalClip();
reed@google.com00177082011-10-12 14:34:30 +00001458 SkASSERT(rgn == tmpClip);
reed@google.com02878b82011-02-24 13:04:42 +00001459#endif
reed@google.com819c9212011-02-23 18:56:55 +00001460}
1461#endif
1462
reed@google.com90c07ea2012-04-13 13:50:27 +00001463void SkCanvas::replayClips(ClipVisitor* visitor) const {
robertphillips@google.com80214e22012-07-20 15:33:18 +00001464 SkClipStack::B2TIter iter(fClipStack);
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001465 const SkClipStack::Element* element;
reed@google.com90c07ea2012-04-13 13:50:27 +00001466
bsalomon@google.com8182fa02012-12-04 14:06:06 +00001467 static const SkRect kEmpty = { 0, 0, 0, 0 };
1468 while ((element = iter.next()) != NULL) {
1469 switch (element->getType()) {
1470 case SkClipStack::Element::kPath_Type:
1471 visitor->clipPath(element->getPath(), element->getOp(), element->isAA());
1472 break;
1473 case SkClipStack::Element::kRect_Type:
1474 visitor->clipRect(element->getRect(), element->getOp(), element->isAA());
1475 break;
1476 case SkClipStack::Element::kEmpty_Type:
1477 visitor->clipRect(kEmpty, SkRegion::kIntersect_Op, false);
1478 break;
reed@google.com90c07ea2012-04-13 13:50:27 +00001479 }
1480 }
1481}
1482
reed@google.com5c3d1472011-02-22 19:12:23 +00001483///////////////////////////////////////////////////////////////////////////////
1484
reed@google.com3b3e8952012-08-16 20:53:31 +00001485bool SkCanvas::quickReject(const SkRect& rect) const {
wjmaclean@chromium.org116b2bc2011-02-07 17:48:37 +00001486
reed@google.com16078632011-12-06 18:56:37 +00001487 if (!rect.isFinite())
wjmaclean@chromium.org116b2bc2011-02-07 17:48:37 +00001488 return true;
1489
reed@google.com00177082011-10-12 14:34:30 +00001490 if (fMCRec->fRasterClip->isEmpty()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001491 return true;
1492 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001493
tomhudson@google.com8d430182011-06-06 19:11:19 +00001494 if (fMCRec->fMatrix->hasPerspective()) {
reed@android.coma380ae42009-07-21 01:17:02 +00001495 SkRect dst;
1496 fMCRec->fMatrix->mapRect(&dst, rect);
1497 SkIRect idst;
1498 dst.roundOut(&idst);
reed@google.com00177082011-10-12 14:34:30 +00001499 return !SkIRect::Intersects(idst, fMCRec->fRasterClip->getBounds());
reed@android.coma380ae42009-07-21 01:17:02 +00001500 } else {
reed@google.comc0784db2013-12-13 21:16:12 +00001501 const SkRect& clipR = this->getLocalClipBounds();
reed@android.comd252db02009-04-01 18:31:44 +00001502
reed@android.coma380ae42009-07-21 01:17:02 +00001503 // for speed, do the most likely reject compares first
reed@google.comc0784db2013-12-13 21:16:12 +00001504 // TODO: should we use | instead, or compare all 4 at once?
1505 if (rect.fTop >= clipR.fBottom || rect.fBottom <= clipR.fTop) {
reed@android.coma380ae42009-07-21 01:17:02 +00001506 return true;
1507 }
reed@google.comc0784db2013-12-13 21:16:12 +00001508 if (rect.fLeft >= clipR.fRight || rect.fRight <= clipR.fLeft) {
reed@android.coma380ae42009-07-21 01:17:02 +00001509 return true;
1510 }
1511 return false;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001512 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001513}
1514
reed@google.com3b3e8952012-08-16 20:53:31 +00001515bool SkCanvas::quickReject(const SkPath& path) const {
1516 return path.isEmpty() || this->quickReject(path.getBounds());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001517}
1518
reed@google.com3b3e8952012-08-16 20:53:31 +00001519bool SkCanvas::getClipBounds(SkRect* bounds) const {
tomhudson@google.combcb671c2011-09-13 15:07:58 +00001520 SkIRect ibounds;
1521 if (!getClipDeviceBounds(&ibounds)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001522 return false;
1523 }
1524
reed@android.comd9c0f0b2009-02-06 22:39:37 +00001525 SkMatrix inverse;
1526 // if we can't invert the CTM, we can't return local clip bounds
1527 if (!fMCRec->fMatrix->invert(&inverse)) {
reed@android.com72dcd3a2009-05-18 15:46:29 +00001528 if (bounds) {
1529 bounds->setEmpty();
1530 }
reed@android.comd9c0f0b2009-02-06 22:39:37 +00001531 return false;
1532 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001533
reed@android.comd9c0f0b2009-02-06 22:39:37 +00001534 if (NULL != bounds) {
tomhudson@google.combcb671c2011-09-13 15:07:58 +00001535 SkRect r;
reed@google.com3b3e8952012-08-16 20:53:31 +00001536 // adjust it outwards in case we are antialiasing
1537 const int inset = 1;
reed@google.comfa4d5bd2012-01-30 17:36:27 +00001538
reed@google.com8f4d2302013-12-17 16:44:46 +00001539 r.iset(ibounds.fLeft - inset, ibounds.fTop - inset,
1540 ibounds.fRight + inset, ibounds.fBottom + inset);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001541 inverse.mapRect(bounds, r);
1542 }
1543 return true;
1544}
1545
tomhudson@google.combcb671c2011-09-13 15:07:58 +00001546bool SkCanvas::getClipDeviceBounds(SkIRect* bounds) const {
reed@google.com00177082011-10-12 14:34:30 +00001547 const SkRasterClip& clip = *fMCRec->fRasterClip;
tomhudson@google.combcb671c2011-09-13 15:07:58 +00001548 if (clip.isEmpty()) {
1549 if (bounds) {
1550 bounds->setEmpty();
1551 }
1552 return false;
1553 }
1554
1555 if (NULL != bounds) {
1556 *bounds = clip.getBounds();
1557 }
1558 return true;
1559}
1560
reed@android.com8a1c16f2008-12-17 15:59:43 +00001561const SkMatrix& SkCanvas::getTotalMatrix() const {
1562 return *fMCRec->fMatrix;
1563}
1564
tomhudson@google.combcb671c2011-09-13 15:07:58 +00001565SkCanvas::ClipType SkCanvas::getClipType() const {
reed@google.com00177082011-10-12 14:34:30 +00001566 if (fMCRec->fRasterClip->isEmpty()) return kEmpty_ClipType;
1567 if (fMCRec->fRasterClip->isRect()) return kRect_ClipType;
tomhudson@google.combcb671c2011-09-13 15:07:58 +00001568 return kComplex_ClipType;
1569}
1570
reed@android.com8a1c16f2008-12-17 15:59:43 +00001571const SkRegion& SkCanvas::getTotalClip() const {
reed@google.com00177082011-10-12 14:34:30 +00001572 return fMCRec->fRasterClip->forceGetBW();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001573}
1574
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001575SkBaseDevice* SkCanvas::createLayerDevice(SkBitmap::Config config,
bsalomon@google.come97f0852011-06-17 13:10:25 +00001576 int width, int height,
1577 bool isOpaque) {
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001578 SkBaseDevice* device = this->getTopDevice();
reed@google.comcde92112011-07-06 20:00:52 +00001579 if (device) {
1580 return device->createCompatibleDeviceForSaveLayer(config, width, height,
1581 isOpaque);
bsalomon@google.come97f0852011-06-17 13:10:25 +00001582 } else {
reed@google.comcde92112011-07-06 20:00:52 +00001583 return NULL;
bsalomon@google.come97f0852011-06-17 13:10:25 +00001584 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001585}
1586
commit-bot@chromium.org644629c2013-11-21 06:21:58 +00001587GrContext* SkCanvas::getGrContext() {
1588#if SK_SUPPORT_GPU
1589 SkBaseDevice* device = this->getTopDevice();
1590 if (NULL != device) {
1591 GrRenderTarget* renderTarget = device->accessRenderTarget();
1592 if (NULL != renderTarget) {
1593 return renderTarget->getContext();
1594 }
1595 }
1596#endif
1597
1598 return NULL;
1599
1600}
bsalomon@google.come97f0852011-06-17 13:10:25 +00001601
reed@android.com8a1c16f2008-12-17 15:59:43 +00001602//////////////////////////////////////////////////////////////////////////////
1603// These are the virtual drawing methods
1604//////////////////////////////////////////////////////////////////////////////
1605
reed@google.com2a981812011-04-14 18:59:28 +00001606void SkCanvas::clear(SkColor color) {
1607 SkDrawIter iter(this);
junov@chromium.org995beb62013-03-28 13:49:22 +00001608 this->predrawNotify();
reed@google.com2a981812011-04-14 18:59:28 +00001609 while (iter.next()) {
1610 iter.fDevice->clear(color);
1611 }
1612}
1613
reed@android.com8a1c16f2008-12-17 15:59:43 +00001614void SkCanvas::drawPaint(const SkPaint& paint) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001615 this->internalDrawPaint(paint);
1616}
1617
1618void SkCanvas::internalDrawPaint(const SkPaint& paint) {
reed@google.comea033602012-12-14 13:13:55 +00001619 CHECK_SHADER_NOSETCONTEXT(paint);
1620
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00001621 LOOPER_BEGIN(paint, SkDrawFilter::kPaint_Type, NULL)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001622
1623 while (iter.next()) {
reed@google.com4e2b3d32011-04-07 14:18:59 +00001624 iter.fDevice->drawPaint(iter, looper.paint());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001625 }
1626
reed@google.com4e2b3d32011-04-07 14:18:59 +00001627 LOOPER_END
reed@android.com8a1c16f2008-12-17 15:59:43 +00001628}
1629
1630void SkCanvas::drawPoints(PointMode mode, size_t count, const SkPoint pts[],
1631 const SkPaint& paint) {
1632 if ((long)count <= 0) {
1633 return;
1634 }
1635
reed@google.comea033602012-12-14 13:13:55 +00001636 CHECK_SHADER_NOSETCONTEXT(paint);
1637
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00001638 SkRect r, storage;
1639 const SkRect* bounds = NULL;
reed@google.coma584aed2012-05-16 14:06:02 +00001640 if (paint.canComputeFastBounds()) {
reed@google.coma584aed2012-05-16 14:06:02 +00001641 // special-case 2 points (common for drawing a single line)
1642 if (2 == count) {
1643 r.set(pts[0], pts[1]);
1644 } else {
commit-bot@chromium.orga8c7f772014-01-24 21:46:29 +00001645 r.set(pts, SkToInt(count));
reed@google.coma584aed2012-05-16 14:06:02 +00001646 }
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00001647 bounds = &paint.computeFastStrokeBounds(r, &storage);
1648 if (this->quickReject(*bounds)) {
reed@google.coma584aed2012-05-16 14:06:02 +00001649 return;
1650 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001651 }
reed@google.coma584aed2012-05-16 14:06:02 +00001652
reed@android.com8a1c16f2008-12-17 15:59:43 +00001653 SkASSERT(pts != NULL);
1654
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00001655 LOOPER_BEGIN(paint, SkDrawFilter::kPoint_Type, bounds)
reed@google.com4b226022011-01-11 18:32:13 +00001656
reed@android.com8a1c16f2008-12-17 15:59:43 +00001657 while (iter.next()) {
reed@google.com4e2b3d32011-04-07 14:18:59 +00001658 iter.fDevice->drawPoints(iter, mode, count, pts, looper.paint());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001659 }
reed@google.com4b226022011-01-11 18:32:13 +00001660
reed@google.com4e2b3d32011-04-07 14:18:59 +00001661 LOOPER_END
reed@android.com8a1c16f2008-12-17 15:59:43 +00001662}
1663
bsalomon@google.com7ce564c2013-10-22 16:54:15 +00001664void SkCanvas::drawRect(const SkRect& r, const SkPaint& paint) {
reed@google.comea033602012-12-14 13:13:55 +00001665 CHECK_SHADER_NOSETCONTEXT(paint);
1666
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00001667 SkRect storage;
1668 const SkRect* bounds = NULL;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001669 if (paint.canComputeFastBounds()) {
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00001670 bounds = &paint.computeFastBounds(r, &storage);
1671 if (this->quickReject(*bounds)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001672 return;
1673 }
1674 }
reed@google.com4b226022011-01-11 18:32:13 +00001675
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00001676 LOOPER_BEGIN(paint, SkDrawFilter::kRect_Type, bounds)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001677
1678 while (iter.next()) {
reed@google.com4e2b3d32011-04-07 14:18:59 +00001679 iter.fDevice->drawRect(iter, r, looper.paint());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001680 }
1681
reed@google.com4e2b3d32011-04-07 14:18:59 +00001682 LOOPER_END
reed@android.com8a1c16f2008-12-17 15:59:43 +00001683}
1684
reed@google.com4ed0fb72012-12-12 20:48:18 +00001685void SkCanvas::drawOval(const SkRect& oval, const SkPaint& paint) {
reed@google.comea033602012-12-14 13:13:55 +00001686 CHECK_SHADER_NOSETCONTEXT(paint);
1687
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00001688 SkRect storage;
1689 const SkRect* bounds = NULL;
reed@google.com4ed0fb72012-12-12 20:48:18 +00001690 if (paint.canComputeFastBounds()) {
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00001691 bounds = &paint.computeFastBounds(oval, &storage);
1692 if (this->quickReject(*bounds)) {
reed@google.com4ed0fb72012-12-12 20:48:18 +00001693 return;
1694 }
1695 }
skia.committer@gmail.com306ab9d2012-12-13 02:01:33 +00001696
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00001697 LOOPER_BEGIN(paint, SkDrawFilter::kOval_Type, bounds)
jvanverth@google.com46d3d392013-01-22 13:34:01 +00001698
1699 while (iter.next()) {
1700 iter.fDevice->drawOval(iter, oval, looper.paint());
1701 }
1702
1703 LOOPER_END
reed@google.com4ed0fb72012-12-12 20:48:18 +00001704}
1705
1706void SkCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) {
reed@google.comea033602012-12-14 13:13:55 +00001707 CHECK_SHADER_NOSETCONTEXT(paint);
1708
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00001709 SkRect storage;
1710 const SkRect* bounds = NULL;
reed@google.com4ed0fb72012-12-12 20:48:18 +00001711 if (paint.canComputeFastBounds()) {
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00001712 bounds = &paint.computeFastBounds(rrect.getBounds(), &storage);
1713 if (this->quickReject(*bounds)) {
reed@google.com4ed0fb72012-12-12 20:48:18 +00001714 return;
1715 }
1716 }
1717
1718 if (rrect.isRect()) {
1719 // call the non-virtual version
1720 this->SkCanvas::drawRect(rrect.getBounds(), paint);
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001721 return;
1722 } else if (rrect.isOval()) {
reed@google.com4ed0fb72012-12-12 20:48:18 +00001723 // call the non-virtual version
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001724 this->SkCanvas::drawOval(rrect.getBounds(), paint);
1725 return;
reed@google.com4ed0fb72012-12-12 20:48:18 +00001726 }
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001727
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00001728 LOOPER_BEGIN(paint, SkDrawFilter::kRRect_Type, bounds)
commit-bot@chromium.orgf2bfd542013-04-25 15:27:00 +00001729
1730 while (iter.next()) {
1731 iter.fDevice->drawRRect(iter, rrect, looper.paint());
1732 }
1733
1734 LOOPER_END
reed@google.com4ed0fb72012-12-12 20:48:18 +00001735}
1736
1737
bsalomon@google.com7ce564c2013-10-22 16:54:15 +00001738void SkCanvas::drawPath(const SkPath& path, const SkPaint& paint) {
reed@google.comea033602012-12-14 13:13:55 +00001739 CHECK_SHADER_NOSETCONTEXT(paint);
1740
reed@google.com93645112012-07-26 16:11:47 +00001741 if (!path.isFinite()) {
1742 return;
1743 }
1744
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00001745 SkRect storage;
1746 const SkRect* bounds = NULL;
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001747 if (!path.isInverseFillType() && paint.canComputeFastBounds()) {
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00001748 const SkRect& pathBounds = path.getBounds();
1749 bounds = &paint.computeFastBounds(pathBounds, &storage);
1750 if (this->quickReject(*bounds)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001751 return;
1752 }
1753 }
bsalomon@google.comfa6ac932011-10-05 19:57:55 +00001754 if (path.isEmpty()) {
1755 if (path.isInverseFillType()) {
1756 this->internalDrawPaint(paint);
1757 }
1758 return;
1759 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001760
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00001761 LOOPER_BEGIN(paint, SkDrawFilter::kPath_Type, bounds)
reed@android.com8a1c16f2008-12-17 15:59:43 +00001762
1763 while (iter.next()) {
reed@google.com4e2b3d32011-04-07 14:18:59 +00001764 iter.fDevice->drawPath(iter, path, looper.paint());
reed@android.com8a1c16f2008-12-17 15:59:43 +00001765 }
1766
reed@google.com4e2b3d32011-04-07 14:18:59 +00001767 LOOPER_END
reed@android.com8a1c16f2008-12-17 15:59:43 +00001768}
1769
1770void SkCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y,
1771 const SkPaint* paint) {
1772 SkDEBUGCODE(bitmap.validate();)
1773
reed@google.com3d608122011-11-21 15:16:16 +00001774 if (NULL == paint || paint->canComputeFastBounds()) {
reed@google.com9efd9a02012-01-30 15:41:43 +00001775 SkRect bounds = {
1776 x, y,
1777 x + SkIntToScalar(bitmap.width()),
1778 y + SkIntToScalar(bitmap.height())
1779 };
1780 if (paint) {
1781 (void)paint->computeFastBounds(bounds, &bounds);
1782 }
reed@google.com3b3e8952012-08-16 20:53:31 +00001783 if (this->quickReject(bounds)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001784 return;
1785 }
1786 }
reed@google.com4b226022011-01-11 18:32:13 +00001787
reed@android.com8a1c16f2008-12-17 15:59:43 +00001788 SkMatrix matrix;
1789 matrix.setTranslate(x, y);
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001790 this->internalDrawBitmap(bitmap, matrix, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001791}
1792
reed@google.com9987ec32011-09-07 11:57:52 +00001793// this one is non-virtual, so it can be called safely by other canvas apis
reed@google.com71121732012-09-18 15:14:33 +00001794void SkCanvas::internalDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001795 const SkRect& dst, const SkPaint* paint,
1796 DrawBitmapRectFlags flags) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00001797 if (bitmap.width() == 0 || bitmap.height() == 0 || dst.isEmpty()) {
1798 return;
1799 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001800
reed@google.comea033602012-12-14 13:13:55 +00001801 CHECK_LOCKCOUNT_BALANCE(bitmap);
1802
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00001803 SkRect storage;
1804 const SkRect* bounds = &dst;
reed@google.com3d608122011-11-21 15:16:16 +00001805 if (NULL == paint || paint->canComputeFastBounds()) {
reed@google.com9efd9a02012-01-30 15:41:43 +00001806 if (paint) {
1807 bounds = &paint->computeFastBounds(dst, &storage);
1808 }
reed@google.com3b3e8952012-08-16 20:53:31 +00001809 if (this->quickReject(*bounds)) {
reed@google.com3d608122011-11-21 15:16:16 +00001810 return;
1811 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001812 }
reed@google.com3d608122011-11-21 15:16:16 +00001813
reed@google.com33535f32012-09-25 15:37:50 +00001814 SkLazyPaint lazy;
1815 if (NULL == paint) {
1816 paint = lazy.init();
reed@android.com8a1c16f2008-12-17 15:59:43 +00001817 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +00001818
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00001819 LOOPER_BEGIN(*paint, SkDrawFilter::kBitmap_Type, bounds)
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +00001820
reed@google.com33535f32012-09-25 15:37:50 +00001821 while (iter.next()) {
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001822 iter.fDevice->drawBitmapRect(iter, bitmap, src, dst, looper.paint(), flags);
reed@android.comf2b98d62010-12-20 18:26:13 +00001823 }
skia.committer@gmail.com7064e9a2012-09-26 02:01:18 +00001824
reed@google.com33535f32012-09-25 15:37:50 +00001825 LOOPER_END
reed@android.com8a1c16f2008-12-17 15:59:43 +00001826}
1827
reed@google.com71121732012-09-18 15:14:33 +00001828void SkCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src,
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001829 const SkRect& dst, const SkPaint* paint,
1830 DrawBitmapRectFlags flags) {
reed@google.com9987ec32011-09-07 11:57:52 +00001831 SkDEBUGCODE(bitmap.validate();)
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001832 this->internalDrawBitmapRect(bitmap, src, dst, paint, flags);
reed@google.com9987ec32011-09-07 11:57:52 +00001833}
1834
reed@android.com8a1c16f2008-12-17 15:59:43 +00001835void SkCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& matrix,
1836 const SkPaint* paint) {
1837 SkDEBUGCODE(bitmap.validate();)
robertphillips@google.com9bf380c2013-07-25 12:10:42 +00001838 this->internalDrawBitmap(bitmap, matrix, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001839}
1840
reed@google.com9987ec32011-09-07 11:57:52 +00001841void SkCanvas::internalDrawBitmapNine(const SkBitmap& bitmap,
1842 const SkIRect& center, const SkRect& dst,
1843 const SkPaint* paint) {
reed@google.com3d608122011-11-21 15:16:16 +00001844 if (NULL == paint || paint->canComputeFastBounds()) {
djsollen@google.com60abb072012-02-15 18:49:15 +00001845 SkRect storage;
1846 const SkRect* bounds = &dst;
1847 if (paint) {
1848 bounds = &paint->computeFastBounds(dst, &storage);
1849 }
reed@google.com3b3e8952012-08-16 20:53:31 +00001850 if (this->quickReject(*bounds)) {
reed@google.com3d608122011-11-21 15:16:16 +00001851 return;
1852 }
1853 }
1854
reed@google.com9987ec32011-09-07 11:57:52 +00001855 const int32_t w = bitmap.width();
1856 const int32_t h = bitmap.height();
1857
1858 SkIRect c = center;
1859 // pin center to the bounds of the bitmap
1860 c.fLeft = SkMax32(0, center.fLeft);
1861 c.fTop = SkMax32(0, center.fTop);
1862 c.fRight = SkPin32(center.fRight, c.fLeft, w);
1863 c.fBottom = SkPin32(center.fBottom, c.fTop, h);
1864
reed@google.com71121732012-09-18 15:14:33 +00001865 const SkScalar srcX[4] = {
rmistry@google.com7d474f82013-01-02 22:03:54 +00001866 0, SkIntToScalar(c.fLeft), SkIntToScalar(c.fRight), SkIntToScalar(w)
reed@google.com71121732012-09-18 15:14:33 +00001867 };
1868 const SkScalar srcY[4] = {
rmistry@google.com7d474f82013-01-02 22:03:54 +00001869 0, SkIntToScalar(c.fTop), SkIntToScalar(c.fBottom), SkIntToScalar(h)
reed@google.com71121732012-09-18 15:14:33 +00001870 };
reed@google.com9987ec32011-09-07 11:57:52 +00001871 SkScalar dstX[4] = {
1872 dst.fLeft, dst.fLeft + SkIntToScalar(c.fLeft),
1873 dst.fRight - SkIntToScalar(w - c.fRight), dst.fRight
1874 };
1875 SkScalar dstY[4] = {
1876 dst.fTop, dst.fTop + SkIntToScalar(c.fTop),
1877 dst.fBottom - SkIntToScalar(h - c.fBottom), dst.fBottom
1878 };
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001879
reed@google.com9987ec32011-09-07 11:57:52 +00001880 if (dstX[1] > dstX[2]) {
1881 dstX[1] = dstX[0] + (dstX[3] - dstX[0]) * c.fLeft / (w - c.width());
1882 dstX[2] = dstX[1];
1883 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001884
reed@google.com9987ec32011-09-07 11:57:52 +00001885 if (dstY[1] > dstY[2]) {
1886 dstY[1] = dstY[0] + (dstY[3] - dstY[0]) * c.fTop / (h - c.height());
1887 dstY[2] = dstY[1];
1888 }
vandebo@chromium.org74b46192012-01-28 01:45:11 +00001889
reed@google.com9987ec32011-09-07 11:57:52 +00001890 for (int y = 0; y < 3; y++) {
reed@google.com71121732012-09-18 15:14:33 +00001891 SkRect s, d;
1892
reed@google.com9987ec32011-09-07 11:57:52 +00001893 s.fTop = srcY[y];
1894 s.fBottom = srcY[y+1];
1895 d.fTop = dstY[y];
1896 d.fBottom = dstY[y+1];
1897 for (int x = 0; x < 3; x++) {
1898 s.fLeft = srcX[x];
1899 s.fRight = srcX[x+1];
1900 d.fLeft = dstX[x];
1901 d.fRight = dstX[x+1];
commit-bot@chromium.orgeed779d2013-08-16 10:24:37 +00001902 this->internalDrawBitmapRect(bitmap, &s, d, paint,
robertphillips@google.com31acc112013-08-20 12:13:48 +00001903 kNone_DrawBitmapRectFlag);
reed@google.com9987ec32011-09-07 11:57:52 +00001904 }
1905 }
1906}
1907
1908void SkCanvas::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center,
1909 const SkRect& dst, const SkPaint* paint) {
1910 SkDEBUGCODE(bitmap.validate();)
1911
1912 // Need a device entry-point, so gpu can use a mesh
1913 this->internalDrawBitmapNine(bitmap, center, dst, paint);
1914}
1915
reed@google.comf67e4cf2011-03-15 20:56:58 +00001916class SkDeviceFilteredPaint {
1917public:
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00001918 SkDeviceFilteredPaint(SkBaseDevice* device, const SkPaint& paint) {
1919 SkBaseDevice::TextFlags flags;
reed@google.comf67e4cf2011-03-15 20:56:58 +00001920 if (device->filterTextFlags(paint, &flags)) {
reed@google.coma076e9b2011-04-06 20:17:29 +00001921 SkPaint* newPaint = fLazy.set(paint);
reed@google.comf67e4cf2011-03-15 20:56:58 +00001922 newPaint->setFlags(flags.fFlags);
1923 newPaint->setHinting(flags.fHinting);
1924 fPaint = newPaint;
1925 } else {
1926 fPaint = &paint;
1927 }
1928 }
1929
reed@google.comf67e4cf2011-03-15 20:56:58 +00001930 const SkPaint& paint() const { return *fPaint; }
1931
1932private:
mike@reedtribe.org2c8fc5a2011-04-10 00:35:29 +00001933 const SkPaint* fPaint;
1934 SkLazyPaint fLazy;
reed@google.comf67e4cf2011-03-15 20:56:58 +00001935};
1936
bungeman@google.com52c748b2011-08-22 21:30:43 +00001937void SkCanvas::DrawRect(const SkDraw& draw, const SkPaint& paint,
1938 const SkRect& r, SkScalar textSize) {
epoger@google.com17b78942011-08-26 14:40:38 +00001939 if (paint.getStyle() == SkPaint::kFill_Style) {
bungeman@google.com52c748b2011-08-22 21:30:43 +00001940 draw.fDevice->drawRect(draw, r, paint);
1941 } else {
1942 SkPaint p(paint);
epoger@google.com17b78942011-08-26 14:40:38 +00001943 p.setStrokeWidth(SkScalarMul(textSize, paint.getStrokeWidth()));
bungeman@google.com52c748b2011-08-22 21:30:43 +00001944 draw.fDevice->drawRect(draw, r, p);
1945 }
1946}
1947
1948void SkCanvas::DrawTextDecorations(const SkDraw& draw, const SkPaint& paint,
1949 const char text[], size_t byteLength,
1950 SkScalar x, SkScalar y) {
1951 SkASSERT(byteLength == 0 || text != NULL);
1952
1953 // nothing to draw
1954 if (text == NULL || byteLength == 0 ||
1955 draw.fClip->isEmpty() ||
1956 (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) {
1957 return;
1958 }
1959
1960 SkScalar width = 0;
1961 SkPoint start;
1962
1963 start.set(0, 0); // to avoid warning
1964 if (paint.getFlags() & (SkPaint::kUnderlineText_Flag |
1965 SkPaint::kStrikeThruText_Flag)) {
1966 width = paint.measureText(text, byteLength);
1967
1968 SkScalar offsetX = 0;
1969 if (paint.getTextAlign() == SkPaint::kCenter_Align) {
1970 offsetX = SkScalarHalf(width);
1971 } else if (paint.getTextAlign() == SkPaint::kRight_Align) {
1972 offsetX = width;
1973 }
1974 start.set(x - offsetX, y);
1975 }
1976
1977 if (0 == width) {
1978 return;
1979 }
1980
1981 uint32_t flags = paint.getFlags();
1982
1983 if (flags & (SkPaint::kUnderlineText_Flag |
1984 SkPaint::kStrikeThruText_Flag)) {
1985 SkScalar textSize = paint.getTextSize();
1986 SkScalar height = SkScalarMul(textSize, kStdUnderline_Thickness);
1987 SkRect r;
1988
1989 r.fLeft = start.fX;
1990 r.fRight = start.fX + width;
1991
1992 if (flags & SkPaint::kUnderlineText_Flag) {
1993 SkScalar offset = SkScalarMulAdd(textSize, kStdUnderline_Offset,
1994 start.fY);
1995 r.fTop = offset;
1996 r.fBottom = offset + height;
1997 DrawRect(draw, paint, r, textSize);
1998 }
1999 if (flags & SkPaint::kStrikeThruText_Flag) {
2000 SkScalar offset = SkScalarMulAdd(textSize, kStdStrikeThru_Offset,
2001 start.fY);
2002 r.fTop = offset;
2003 r.fBottom = offset + height;
2004 DrawRect(draw, paint, r, textSize);
2005 }
2006 }
2007}
2008
reed@android.com8a1c16f2008-12-17 15:59:43 +00002009void SkCanvas::drawText(const void* text, size_t byteLength,
2010 SkScalar x, SkScalar y, const SkPaint& paint) {
reed@google.comea033602012-12-14 13:13:55 +00002011 CHECK_SHADER_NOSETCONTEXT(paint);
2012
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00002013 LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL)
reed@android.com8a1c16f2008-12-17 15:59:43 +00002014
2015 while (iter.next()) {
reed@google.com4e2b3d32011-04-07 14:18:59 +00002016 SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint());
reed@google.comf67e4cf2011-03-15 20:56:58 +00002017 iter.fDevice->drawText(iter, text, byteLength, x, y, dfp.paint());
bungeman@google.com52c748b2011-08-22 21:30:43 +00002018 DrawTextDecorations(iter, dfp.paint(),
2019 static_cast<const char*>(text), byteLength, x, y);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002020 }
2021
reed@google.com4e2b3d32011-04-07 14:18:59 +00002022 LOOPER_END
reed@android.com8a1c16f2008-12-17 15:59:43 +00002023}
2024
2025void SkCanvas::drawPosText(const void* text, size_t byteLength,
2026 const SkPoint pos[], const SkPaint& paint) {
reed@google.comea033602012-12-14 13:13:55 +00002027 CHECK_SHADER_NOSETCONTEXT(paint);
2028
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00002029 LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL)
reed@google.com4b226022011-01-11 18:32:13 +00002030
reed@android.com8a1c16f2008-12-17 15:59:43 +00002031 while (iter.next()) {
reed@google.com4e2b3d32011-04-07 14:18:59 +00002032 SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002033 iter.fDevice->drawPosText(iter, text, byteLength, &pos->fX, 0, 2,
reed@google.comf67e4cf2011-03-15 20:56:58 +00002034 dfp.paint());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002035 }
reed@google.com4b226022011-01-11 18:32:13 +00002036
reed@google.com4e2b3d32011-04-07 14:18:59 +00002037 LOOPER_END
reed@android.com8a1c16f2008-12-17 15:59:43 +00002038}
2039
2040void SkCanvas::drawPosTextH(const void* text, size_t byteLength,
2041 const SkScalar xpos[], SkScalar constY,
2042 const SkPaint& paint) {
reed@google.comea033602012-12-14 13:13:55 +00002043 CHECK_SHADER_NOSETCONTEXT(paint);
2044
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00002045 LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL)
reed@google.com4b226022011-01-11 18:32:13 +00002046
reed@android.com8a1c16f2008-12-17 15:59:43 +00002047 while (iter.next()) {
reed@google.com4e2b3d32011-04-07 14:18:59 +00002048 SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002049 iter.fDevice->drawPosText(iter, text, byteLength, xpos, constY, 1,
reed@google.comf67e4cf2011-03-15 20:56:58 +00002050 dfp.paint());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002051 }
reed@google.com4b226022011-01-11 18:32:13 +00002052
reed@google.com4e2b3d32011-04-07 14:18:59 +00002053 LOOPER_END
reed@android.com8a1c16f2008-12-17 15:59:43 +00002054}
2055
2056void SkCanvas::drawTextOnPath(const void* text, size_t byteLength,
2057 const SkPath& path, const SkMatrix* matrix,
2058 const SkPaint& paint) {
reed@google.comea033602012-12-14 13:13:55 +00002059 CHECK_SHADER_NOSETCONTEXT(paint);
2060
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00002061 LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL)
reed@android.com8a1c16f2008-12-17 15:59:43 +00002062
2063 while (iter.next()) {
2064 iter.fDevice->drawTextOnPath(iter, text, byteLength, path,
reed@google.com4e2b3d32011-04-07 14:18:59 +00002065 matrix, looper.paint());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002066 }
2067
reed@google.com4e2b3d32011-04-07 14:18:59 +00002068 LOOPER_END
reed@android.com8a1c16f2008-12-17 15:59:43 +00002069}
2070
2071void SkCanvas::drawVertices(VertexMode vmode, int vertexCount,
2072 const SkPoint verts[], const SkPoint texs[],
2073 const SkColor colors[], SkXfermode* xmode,
2074 const uint16_t indices[], int indexCount,
2075 const SkPaint& paint) {
reed@google.comea033602012-12-14 13:13:55 +00002076 CHECK_SHADER_NOSETCONTEXT(paint);
2077
senorblanco@chromium.org78cf1192014-01-28 19:22:35 +00002078 LOOPER_BEGIN(paint, SkDrawFilter::kPath_Type, NULL)
reed@google.com4b226022011-01-11 18:32:13 +00002079
reed@android.com8a1c16f2008-12-17 15:59:43 +00002080 while (iter.next()) {
2081 iter.fDevice->drawVertices(iter, vmode, vertexCount, verts, texs,
reed@google.com4e2b3d32011-04-07 14:18:59 +00002082 colors, xmode, indices, indexCount,
2083 looper.paint());
reed@android.com8a1c16f2008-12-17 15:59:43 +00002084 }
reed@google.com4b226022011-01-11 18:32:13 +00002085
reed@google.com4e2b3d32011-04-07 14:18:59 +00002086 LOOPER_END
reed@android.com8a1c16f2008-12-17 15:59:43 +00002087}
2088
2089//////////////////////////////////////////////////////////////////////////////
2090// These methods are NOT virtual, and therefore must call back into virtual
2091// methods, rather than actually drawing themselves.
2092//////////////////////////////////////////////////////////////////////////////
2093
2094void SkCanvas::drawARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b,
reed@android.com845fdac2009-06-23 03:01:32 +00002095 SkXfermode::Mode mode) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002096 SkPaint paint;
2097
2098 paint.setARGB(a, r, g, b);
reed@android.com845fdac2009-06-23 03:01:32 +00002099 if (SkXfermode::kSrcOver_Mode != mode) {
reed@android.com0baf1932009-06-24 12:41:42 +00002100 paint.setXfermodeMode(mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002101 }
2102 this->drawPaint(paint);
2103}
2104
reed@android.com845fdac2009-06-23 03:01:32 +00002105void SkCanvas::drawColor(SkColor c, SkXfermode::Mode mode) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002106 SkPaint paint;
2107
2108 paint.setColor(c);
reed@android.com845fdac2009-06-23 03:01:32 +00002109 if (SkXfermode::kSrcOver_Mode != mode) {
reed@android.com0baf1932009-06-24 12:41:42 +00002110 paint.setXfermodeMode(mode);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002111 }
2112 this->drawPaint(paint);
2113}
2114
2115void SkCanvas::drawPoint(SkScalar x, SkScalar y, const SkPaint& paint) {
2116 SkPoint pt;
reed@google.com4b226022011-01-11 18:32:13 +00002117
reed@android.com8a1c16f2008-12-17 15:59:43 +00002118 pt.set(x, y);
2119 this->drawPoints(kPoints_PointMode, 1, &pt, paint);
2120}
2121
2122void SkCanvas::drawPoint(SkScalar x, SkScalar y, SkColor color) {
2123 SkPoint pt;
2124 SkPaint paint;
reed@google.com4b226022011-01-11 18:32:13 +00002125
reed@android.com8a1c16f2008-12-17 15:59:43 +00002126 pt.set(x, y);
2127 paint.setColor(color);
2128 this->drawPoints(kPoints_PointMode, 1, &pt, paint);
2129}
2130
2131void SkCanvas::drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1,
2132 const SkPaint& paint) {
2133 SkPoint pts[2];
reed@google.com4b226022011-01-11 18:32:13 +00002134
reed@android.com8a1c16f2008-12-17 15:59:43 +00002135 pts[0].set(x0, y0);
2136 pts[1].set(x1, y1);
2137 this->drawPoints(kLines_PointMode, 2, pts, paint);
2138}
2139
2140void SkCanvas::drawRectCoords(SkScalar left, SkScalar top,
2141 SkScalar right, SkScalar bottom,
2142 const SkPaint& paint) {
2143 SkRect r;
2144
2145 r.set(left, top, right, bottom);
2146 this->drawRect(r, paint);
2147}
2148
2149void SkCanvas::drawCircle(SkScalar cx, SkScalar cy, SkScalar radius,
2150 const SkPaint& paint) {
2151 if (radius < 0) {
2152 radius = 0;
2153 }
2154
2155 SkRect r;
2156 r.set(cx - radius, cy - radius, cx + radius, cy + radius);
reed@google.com4ed0fb72012-12-12 20:48:18 +00002157 this->drawOval(r, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002158}
2159
2160void SkCanvas::drawRoundRect(const SkRect& r, SkScalar rx, SkScalar ry,
2161 const SkPaint& paint) {
2162 if (rx > 0 && ry > 0) {
2163 if (paint.canComputeFastBounds()) {
2164 SkRect storage;
reed@google.com3b3e8952012-08-16 20:53:31 +00002165 if (this->quickReject(paint.computeFastBounds(r, &storage))) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002166 return;
2167 }
2168 }
reed@google.com4ed0fb72012-12-12 20:48:18 +00002169 SkRRect rrect;
2170 rrect.setRectXY(r, rx, ry);
2171 this->drawRRect(rrect, paint);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002172 } else {
2173 this->drawRect(r, paint);
2174 }
2175}
2176
reed@android.com8a1c16f2008-12-17 15:59:43 +00002177void SkCanvas::drawArc(const SkRect& oval, SkScalar startAngle,
2178 SkScalar sweepAngle, bool useCenter,
2179 const SkPaint& paint) {
2180 if (SkScalarAbs(sweepAngle) >= SkIntToScalar(360)) {
2181 this->drawOval(oval, paint);
2182 } else {
2183 SkPath path;
2184 if (useCenter) {
2185 path.moveTo(oval.centerX(), oval.centerY());
2186 }
2187 path.arcTo(oval, startAngle, sweepAngle, !useCenter);
2188 if (useCenter) {
2189 path.close();
2190 }
2191 this->drawPath(path, paint);
2192 }
2193}
2194
2195void SkCanvas::drawTextOnPathHV(const void* text, size_t byteLength,
2196 const SkPath& path, SkScalar hOffset,
2197 SkScalar vOffset, const SkPaint& paint) {
2198 SkMatrix matrix;
reed@google.com4b226022011-01-11 18:32:13 +00002199
reed@android.com8a1c16f2008-12-17 15:59:43 +00002200 matrix.setTranslate(hOffset, vOffset);
2201 this->drawTextOnPath(text, byteLength, path, &matrix, paint);
2202}
2203
reed@android.comf76bacf2009-05-13 14:00:33 +00002204///////////////////////////////////////////////////////////////////////////////
2205
reed@android.com8a1c16f2008-12-17 15:59:43 +00002206void SkCanvas::drawPicture(SkPicture& picture) {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002207 picture.draw(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002208}
2209
reed@android.com8a1c16f2008-12-17 15:59:43 +00002210///////////////////////////////////////////////////////////////////////////////
2211///////////////////////////////////////////////////////////////////////////////
2212
2213SkCanvas::LayerIter::LayerIter(SkCanvas* canvas, bool skipEmptyClips) {
reed@google.comd6423292010-12-20 20:53:13 +00002214 SK_COMPILE_ASSERT(sizeof(fStorage) >= sizeof(SkDrawIter), fStorage_too_small);
reed@android.com8a1c16f2008-12-17 15:59:43 +00002215
2216 SkASSERT(canvas);
2217
2218 fImpl = new (fStorage) SkDrawIter(canvas, skipEmptyClips);
2219 fDone = !fImpl->next();
2220}
2221
2222SkCanvas::LayerIter::~LayerIter() {
2223 fImpl->~SkDrawIter();
2224}
2225
2226void SkCanvas::LayerIter::next() {
2227 fDone = !fImpl->next();
2228}
2229
robertphillips@google.com1f2f3382013-08-29 11:54:56 +00002230SkBaseDevice* SkCanvas::LayerIter::device() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +00002231 return fImpl->getDevice();
2232}
2233
2234const SkMatrix& SkCanvas::LayerIter::matrix() const {
2235 return fImpl->getMatrix();
2236}
2237
2238const SkPaint& SkCanvas::LayerIter::paint() const {
2239 const SkPaint* paint = fImpl->getPaint();
2240 if (NULL == paint) {
2241 paint = &fDefaultPaint;
2242 }
2243 return *paint;
2244}
2245
2246const SkRegion& SkCanvas::LayerIter::clip() const { return fImpl->getClip(); }
2247int SkCanvas::LayerIter::x() const { return fImpl->getX(); }
2248int SkCanvas::LayerIter::y() const { return fImpl->getY(); }
justinlin@google.com20a550c2012-07-27 17:37:12 +00002249
2250///////////////////////////////////////////////////////////////////////////////
2251
2252SkCanvas::ClipVisitor::~ClipVisitor() { }