reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2008 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include "SkCanvas.h" |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 9 | #include "SkCanvasDrawable.h" |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 10 | #include "SkCanvasPriv.h" |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 11 | #include "SkBitmapDevice.h" |
senorblanco@chromium.org | 9c39744 | 2012-09-27 21:57:45 +0000 | [diff] [blame] | 12 | #include "SkDeviceImageFilterProxy.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkDraw.h" |
| 14 | #include "SkDrawFilter.h" |
| 15 | #include "SkDrawLooper.h" |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 16 | #include "SkImage.h" |
mike@reedtribe.org | 74bb77e | 2012-09-26 02:24:45 +0000 | [diff] [blame] | 17 | #include "SkMetaData.h" |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 18 | #include "SkPathOps.h" |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 19 | #include "SkPatchUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 20 | #include "SkPicture.h" |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 21 | #include "SkRasterClip.h" |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 22 | #include "SkRRect.h" |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 23 | #include "SkSmallAllocator.h" |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 24 | #include "SkSurface_Base.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | #include "SkTemplates.h" |
fmalita | 7ba7aa7 | 2014-08-29 09:46:36 -0700 | [diff] [blame] | 26 | #include "SkTextBlob.h" |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 27 | #include "SkTextFormatParams.h" |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 28 | #include "SkTLazy.h" |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 29 | #include "SkTraceEvent.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | #include "SkUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 32 | #if SK_SUPPORT_GPU |
| 33 | #include "GrRenderTarget.h" |
| 34 | #endif |
| 35 | |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 36 | // experimental for faster tiled drawing... |
| 37 | //#define SK_ENABLE_CLIP_QUICKREJECT |
robertphillips@google.com | 15e9d3e | 2012-06-21 20:25:03 +0000 | [diff] [blame] | 38 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | //#define SK_TRACE_SAVERESTORE |
| 40 | |
| 41 | #ifdef SK_TRACE_SAVERESTORE |
| 42 | static int gLayerCounter; |
| 43 | static void inc_layer() { ++gLayerCounter; printf("----- inc layer %d\n", gLayerCounter); } |
| 44 | static void dec_layer() { --gLayerCounter; printf("----- dec layer %d\n", gLayerCounter); } |
| 45 | |
| 46 | static int gRecCounter; |
| 47 | static void inc_rec() { ++gRecCounter; printf("----- inc rec %d\n", gRecCounter); } |
| 48 | static void dec_rec() { --gRecCounter; printf("----- dec rec %d\n", gRecCounter); } |
| 49 | |
| 50 | static int gCanvasCounter; |
| 51 | static void inc_canvas() { ++gCanvasCounter; printf("----- inc canvas %d\n", gCanvasCounter); } |
| 52 | static void dec_canvas() { --gCanvasCounter; printf("----- dec canvas %d\n", gCanvasCounter); } |
| 53 | #else |
| 54 | #define inc_layer() |
| 55 | #define dec_layer() |
| 56 | #define inc_rec() |
| 57 | #define dec_rec() |
| 58 | #define inc_canvas() |
| 59 | #define dec_canvas() |
| 60 | #endif |
| 61 | |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 62 | typedef SkTLazy<SkPaint> SkLazyPaint; |
| 63 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 64 | void SkCanvas::predrawNotify() { |
| 65 | if (fSurfaceBase) { |
commit-bot@chromium.org | c4c9870 | 2013-04-22 14:28:01 +0000 | [diff] [blame] | 66 | fSurfaceBase->aboutToDraw(SkSurface::kRetain_ContentChangeMode); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 72 | static uint32_t filter_paint_flags(const SkSurfaceProps& props, uint32_t flags) { |
| 73 | const uint32_t propFlags = props.flags(); |
| 74 | if (propFlags & SkSurfaceProps::kDisallowDither_Flag) { |
| 75 | flags &= ~SkPaint::kDither_Flag; |
| 76 | } |
| 77 | if (propFlags & SkSurfaceProps::kDisallowAntiAlias_Flag) { |
| 78 | flags &= ~SkPaint::kAntiAlias_Flag; |
| 79 | } |
| 80 | return flags; |
| 81 | } |
| 82 | |
| 83 | /////////////////////////////////////////////////////////////////////////////// |
| 84 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 85 | /* This is the record we keep for each SkBaseDevice that the user installs. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | The clip/matrix/proc are fields that reflect the top of the save/restore |
| 87 | stack. Whenever the canvas changes, it marks a dirty flag, and then before |
| 88 | these are used (assuming we're not on a layer) we rebuild these cache |
| 89 | values: they reflect the top of the save stack, but translated and clipped |
| 90 | by the device's XY offset and bitmap-bounds. |
| 91 | */ |
| 92 | struct DeviceCM { |
| 93 | DeviceCM* fNext; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 94 | SkBaseDevice* fDevice; |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 95 | SkRasterClip fClip; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | const SkMatrix* fMatrix; |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 97 | SkPaint* fPaint; // may be null (in the future) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 99 | DeviceCM(SkBaseDevice* device, int x, int y, const SkPaint* paint, SkCanvas* canvas, |
| 100 | bool conservativeRasterClip) |
| 101 | : fNext(NULL) |
| 102 | , fClip(conservativeRasterClip) |
| 103 | { |
| 104 | if (NULL != device) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | device->ref(); |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 106 | device->onAttachToCanvas(canvas); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 108 | fDevice = device; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | fPaint = paint ? SkNEW_ARGS(SkPaint, (*paint)) : NULL; |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 110 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 111 | |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 112 | ~DeviceCM() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 113 | if (fDevice) { |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 114 | fDevice->onDetachFromCanvas(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | fDevice->unref(); |
| 116 | } |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 117 | SkDELETE(fPaint); |
| 118 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 119 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 120 | void updateMC(const SkMatrix& totalMatrix, const SkRasterClip& totalClip, |
| 121 | const SkClipStack& clipStack, SkRasterClip* updateClip) { |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 122 | int x = fDevice->getOrigin().x(); |
| 123 | int y = fDevice->getOrigin().y(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | int width = fDevice->width(); |
| 125 | int height = fDevice->height(); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 126 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 127 | if ((x | y) == 0) { |
| 128 | fMatrix = &totalMatrix; |
| 129 | fClip = totalClip; |
| 130 | } else { |
| 131 | fMatrixStorage = totalMatrix; |
| 132 | fMatrixStorage.postTranslate(SkIntToScalar(-x), |
| 133 | SkIntToScalar(-y)); |
| 134 | fMatrix = &fMatrixStorage; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 135 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 136 | totalClip.translate(-x, -y, &fClip); |
| 137 | } |
| 138 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 139 | fClip.op(SkIRect::MakeWH(width, height), SkRegion::kIntersect_Op); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | |
| 141 | // intersect clip, but don't translate it (yet) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 142 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 143 | if (updateClip) { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 144 | updateClip->op(SkIRect::MakeXYWH(x, y, width, height), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 145 | SkRegion::kDifference_Op); |
| 146 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 147 | |
robertphillips@google.com | 3fffb2e | 2012-10-09 22:30:18 +0000 | [diff] [blame] | 148 | fDevice->setMatrixClip(*fMatrix, fClip.forceGetBW(), clipStack); |
| 149 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | #ifdef SK_DEBUG |
| 151 | if (!fClip.isEmpty()) { |
| 152 | SkIRect deviceR; |
| 153 | deviceR.set(0, 0, width, height); |
| 154 | SkASSERT(deviceR.contains(fClip.getBounds())); |
| 155 | } |
| 156 | #endif |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 157 | } |
| 158 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 159 | private: |
bsalomon@google.com | 0e354aa | 2012-10-08 20:44:25 +0000 | [diff] [blame] | 160 | SkMatrix fMatrixStorage; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | /* This is the record we keep for each save/restore level in the stack. |
| 164 | Since a level optionally copies the matrix and/or stack, we have pointers |
| 165 | for these fields. If the value is copied for this level, the copy is |
| 166 | stored in the ...Storage field, and the pointer points to that. If the |
| 167 | value is not copied for this level, we ignore ...Storage, and just point |
| 168 | at the corresponding value in the previous level in the stack. |
| 169 | */ |
| 170 | class SkCanvas::MCRec { |
| 171 | public: |
reed | 6f09709 | 2014-09-09 12:51:10 -0700 | [diff] [blame] | 172 | SkRasterClip fRasterClip; |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 173 | SkMatrix fMatrix; |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 174 | SkDrawFilter* fFilter; // the current filter (or null) |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 175 | DeviceCM* fLayer; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | /* If there are any layers in the stack, this points to the top-most |
| 177 | one that is at or below this level in the stack (so we know what |
| 178 | bitmap/device to draw into from this level. This value is NOT |
| 179 | reference counted, since the real owner is either our fLayer field, |
| 180 | or a previous one in a lower level.) |
| 181 | */ |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 182 | DeviceCM* fTopLayer; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 183 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 184 | MCRec(bool conservativeRasterClip) : fRasterClip(conservativeRasterClip) { |
| 185 | fMatrix.reset(); |
| 186 | fFilter = NULL; |
| 187 | fLayer = NULL; |
| 188 | fTopLayer = NULL; |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 189 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 190 | // don't bother initializing fNext |
| 191 | inc_rec(); |
| 192 | } |
| 193 | MCRec(const MCRec& prev) : fRasterClip(prev.fRasterClip) { |
| 194 | fMatrix = prev.fMatrix; |
| 195 | fFilter = SkSafeRef(prev.fFilter); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 196 | fLayer = NULL; |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 197 | fTopLayer = prev.fTopLayer; |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 198 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 199 | // don't bother initializing fNext |
| 200 | inc_rec(); |
| 201 | } |
| 202 | ~MCRec() { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 203 | SkSafeUnref(fFilter); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 204 | SkDELETE(fLayer); |
| 205 | dec_rec(); |
| 206 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | class SkDrawIter : public SkDraw { |
| 210 | public: |
tomhudson@google.com | 8a0b029 | 2011-09-13 14:41:06 +0000 | [diff] [blame] | 211 | SkDrawIter(SkCanvas* canvas, bool skipEmptyClips = true) { |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 212 | canvas = canvas->canvasForDrawIter(); |
tomhudson@google.com | 8a0b029 | 2011-09-13 14:41:06 +0000 | [diff] [blame] | 213 | fCanvas = canvas; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 214 | canvas->updateDeviceCMCache(); |
| 215 | |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 216 | fClipStack = &canvas->fClipStack; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 217 | fCurrLayer = canvas->fMCRec->fTopLayer; |
tomhudson@google.com | 8a0b029 | 2011-09-13 14:41:06 +0000 | [diff] [blame] | 218 | fSkipEmptyClips = skipEmptyClips; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 219 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 220 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 221 | bool next() { |
| 222 | // skip over recs with empty clips |
| 223 | if (fSkipEmptyClips) { |
| 224 | while (fCurrLayer && fCurrLayer->fClip.isEmpty()) { |
| 225 | fCurrLayer = fCurrLayer->fNext; |
| 226 | } |
| 227 | } |
| 228 | |
reed@google.com | f68c5e2 | 2012-02-24 16:38:58 +0000 | [diff] [blame] | 229 | const DeviceCM* rec = fCurrLayer; |
| 230 | if (rec && rec->fDevice) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 231 | |
| 232 | fMatrix = rec->fMatrix; |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 233 | fClip = &((SkRasterClip*)&rec->fClip)->forceGetBW(); |
| 234 | fRC = &rec->fClip; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 235 | fDevice = rec->fDevice; |
| 236 | fBitmap = &fDevice->accessBitmap(true); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 237 | fPaint = rec->fPaint; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 238 | SkDEBUGCODE(this->validate();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 239 | |
| 240 | fCurrLayer = rec->fNext; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 241 | // fCurrLayer may be NULL now |
reed@android.com | 199f108 | 2009-06-10 02:12:47 +0000 | [diff] [blame] | 242 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 243 | return true; |
| 244 | } |
| 245 | return false; |
| 246 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 247 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 248 | SkBaseDevice* getDevice() const { return fDevice; } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 249 | int getX() const { return fDevice->getOrigin().x(); } |
| 250 | int getY() const { return fDevice->getOrigin().y(); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 251 | const SkMatrix& getMatrix() const { return *fMatrix; } |
| 252 | const SkRegion& getClip() const { return *fClip; } |
| 253 | const SkPaint* getPaint() const { return fPaint; } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 254 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 255 | private: |
| 256 | SkCanvas* fCanvas; |
| 257 | const DeviceCM* fCurrLayer; |
| 258 | const SkPaint* fPaint; // May be null. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 259 | SkBool8 fSkipEmptyClips; |
| 260 | |
| 261 | typedef SkDraw INHERITED; |
| 262 | }; |
| 263 | |
| 264 | ///////////////////////////////////////////////////////////////////////////// |
| 265 | |
| 266 | class AutoDrawLooper { |
| 267 | public: |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 268 | AutoDrawLooper(SkCanvas* canvas, const SkSurfaceProps& props, const SkPaint& paint, |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 269 | bool skipLayerForImageFilter = false, |
| 270 | const SkRect* bounds = NULL) : fOrigPaint(paint) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 271 | fCanvas = canvas; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 272 | fFilter = canvas->getDrawFilter(); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 273 | fPaint = &fOrigPaint; |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 274 | fSaveCount = canvas->getSaveCount(); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 275 | fDoClearImageFilter = false; |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 276 | fDone = false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 277 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 278 | if (!skipLayerForImageFilter && fOrigPaint.getImageFilter()) { |
| 279 | SkPaint tmp; |
| 280 | tmp.setImageFilter(fOrigPaint.getImageFilter()); |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 281 | (void)canvas->internalSaveLayer(bounds, &tmp, SkCanvas::kARGB_ClipLayer_SaveFlag, |
| 282 | true, SkCanvas::kFullLayer_SaveLayerStrategy); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 283 | // we'll clear the imageFilter for the actual draws in next(), so |
| 284 | // it will only be applied during the restore(). |
| 285 | fDoClearImageFilter = true; |
| 286 | } |
| 287 | |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 288 | if (SkDrawLooper* looper = paint.getLooper()) { |
| 289 | void* buffer = fLooperContextAllocator.reserveT<SkDrawLooper::Context>( |
| 290 | looper->contextSize()); |
| 291 | fLooperContext = looper->createContext(canvas, buffer); |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 292 | fIsSimple = false; |
| 293 | } else { |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 294 | fLooperContext = NULL; |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 295 | // can we be marked as simple? |
| 296 | fIsSimple = !fFilter && !fDoClearImageFilter; |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 297 | } |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 298 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 299 | uint32_t oldFlags = paint.getFlags(); |
| 300 | fNewPaintFlags = filter_paint_flags(props, oldFlags); |
| 301 | if (fIsSimple && (fNewPaintFlags != oldFlags)) { |
| 302 | SkPaint* paint = fLazyPaint.set(fOrigPaint); |
| 303 | paint->setFlags(fNewPaintFlags); |
| 304 | fPaint = paint; |
| 305 | // if we're not simple, doNext() will take care of calling setFlags() |
| 306 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 307 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 308 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 309 | ~AutoDrawLooper() { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 310 | if (fDoClearImageFilter) { |
| 311 | fCanvas->internalRestore(); |
| 312 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 313 | SkASSERT(fCanvas->getSaveCount() == fSaveCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 314 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 315 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 316 | const SkPaint& paint() const { |
| 317 | SkASSERT(fPaint); |
| 318 | return *fPaint; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 319 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 320 | |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 321 | bool next(SkDrawFilter::Type drawType) { |
| 322 | if (fDone) { |
| 323 | return false; |
| 324 | } else if (fIsSimple) { |
| 325 | fDone = true; |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 326 | return !fPaint->nothingToDraw(); |
| 327 | } else { |
| 328 | return this->doNext(drawType); |
| 329 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 330 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 331 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 332 | private: |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 333 | SkLazyPaint fLazyPaint; |
| 334 | SkCanvas* fCanvas; |
| 335 | const SkPaint& fOrigPaint; |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 336 | SkDrawFilter* fFilter; |
| 337 | const SkPaint* fPaint; |
| 338 | int fSaveCount; |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 339 | uint32_t fNewPaintFlags; |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 340 | bool fDoClearImageFilter; |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 341 | bool fDone; |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 342 | bool fIsSimple; |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 343 | SkDrawLooper::Context* fLooperContext; |
| 344 | SkSmallAllocator<1, 32> fLooperContextAllocator; |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 345 | |
| 346 | bool doNext(SkDrawFilter::Type drawType); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 347 | }; |
| 348 | |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 349 | bool AutoDrawLooper::doNext(SkDrawFilter::Type drawType) { |
reed@google.com | 632e1a2 | 2011-10-06 12:37:00 +0000 | [diff] [blame] | 350 | fPaint = NULL; |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 351 | SkASSERT(!fIsSimple); |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 352 | SkASSERT(fLooperContext || fFilter || fDoClearImageFilter); |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 353 | |
| 354 | SkPaint* paint = fLazyPaint.set(fOrigPaint); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 355 | paint->setFlags(fNewPaintFlags); |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 356 | |
| 357 | if (fDoClearImageFilter) { |
| 358 | paint->setImageFilter(NULL); |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 359 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 360 | |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 361 | if (fLooperContext && !fLooperContext->next(fCanvas, paint)) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 362 | fDone = true; |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 363 | return false; |
| 364 | } |
| 365 | if (fFilter) { |
reed@google.com | 971aca7 | 2012-11-26 20:26:54 +0000 | [diff] [blame] | 366 | if (!fFilter->filter(paint, drawType)) { |
| 367 | fDone = true; |
| 368 | return false; |
| 369 | } |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 370 | if (NULL == fLooperContext) { |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 371 | // no looper means we only draw once |
| 372 | fDone = true; |
| 373 | } |
| 374 | } |
| 375 | fPaint = paint; |
| 376 | |
| 377 | // if we only came in here for the imagefilter, mark us as done |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 378 | if (!fLooperContext && !fFilter) { |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 379 | fDone = true; |
reed@google.com | 632e1a2 | 2011-10-06 12:37:00 +0000 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | // call this after any possible paint modifiers |
| 383 | if (fPaint->nothingToDraw()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 384 | fPaint = NULL; |
| 385 | return false; |
| 386 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 387 | return true; |
| 388 | } |
| 389 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 390 | ////////// macros to place around the internal draw calls ////////////////// |
| 391 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 392 | #define LOOPER_BEGIN_DRAWDEVICE(paint, type) \ |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 393 | this->predrawNotify(); \ |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 394 | AutoDrawLooper looper(this, fProps, paint, true); \ |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 395 | while (looper.next(type)) { \ |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 396 | SkDrawIter iter(this); |
| 397 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 398 | #define LOOPER_BEGIN(paint, type, bounds) \ |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 399 | this->predrawNotify(); \ |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 400 | AutoDrawLooper looper(this, fProps, paint, false, bounds); \ |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 401 | while (looper.next(type)) { \ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 402 | SkDrawIter iter(this); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 403 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 404 | #define LOOPER_END } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 405 | |
| 406 | //////////////////////////////////////////////////////////////////////////// |
| 407 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 408 | void SkCanvas::setupDevice(SkBaseDevice* device) { |
| 409 | device->setPixelGeometry(fProps.pixelGeometry()); |
| 410 | } |
| 411 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 412 | SkBaseDevice* SkCanvas::init(SkBaseDevice* device, InitFlags flags) { |
| 413 | fConservativeRasterClip = SkToBool(flags & kConservativeRasterClip_InitFlag); |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 414 | fCachedLocalClipBounds.setEmpty(); |
| 415 | fCachedLocalClipBoundsDirty = true; |
caryclark@google.com | 8f0a7b8 | 2012-11-07 14:54:49 +0000 | [diff] [blame] | 416 | fAllowSoftClip = true; |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 417 | fAllowSimplifyClip = false; |
reed | f92c866 | 2014-08-18 08:02:43 -0700 | [diff] [blame] | 418 | fDeviceCMDirty = true; |
junov@chromium.org | b0a7ace | 2012-04-05 18:33:23 +0000 | [diff] [blame] | 419 | fSaveLayerCount = 0; |
commit-bot@chromium.org | 210ae2a | 2014-02-27 17:40:13 +0000 | [diff] [blame] | 420 | fCullCount = 0; |
mike@reedtribe.org | 74bb77e | 2012-09-26 02:24:45 +0000 | [diff] [blame] | 421 | fMetaData = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 422 | |
| 423 | fMCRec = (MCRec*)fMCStack.push_back(); |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 424 | new (fMCRec) MCRec(fConservativeRasterClip); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 425 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 426 | fMCRec->fLayer = SkNEW_ARGS(DeviceCM, (NULL, 0, 0, NULL, NULL, fConservativeRasterClip)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 427 | fMCRec->fTopLayer = fMCRec->fLayer; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 428 | |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 429 | fSurfaceBase = NULL; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 430 | |
reed | f92c866 | 2014-08-18 08:02:43 -0700 | [diff] [blame] | 431 | if (device) { |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 432 | this->setupDevice(device); |
| 433 | if (device->forceConservativeRasterClip()) { |
| 434 | fConservativeRasterClip = true; |
| 435 | } |
reed | f92c866 | 2014-08-18 08:02:43 -0700 | [diff] [blame] | 436 | device->onAttachToCanvas(this); |
| 437 | fMCRec->fLayer->fDevice = SkRef(device); |
| 438 | fMCRec->fRasterClip.setRect(SkIRect::MakeWH(device->width(), device->height())); |
| 439 | } |
| 440 | return device; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 441 | } |
| 442 | |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 443 | SkCanvas::SkCanvas() |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 444 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 445 | , fProps(SkSurfaceProps::kLegacyFontHost_InitType) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 446 | { |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 447 | inc_canvas(); |
skia.committer@gmail.com | ba12448 | 2014-02-01 03:01:57 +0000 | [diff] [blame] | 448 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 449 | this->init(NULL, kDefault_InitFlags); |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 450 | } |
| 451 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 452 | static SkBitmap make_nopixels(int width, int height) { |
| 453 | SkBitmap bitmap; |
| 454 | bitmap.setInfo(SkImageInfo::MakeUnknown(width, height)); |
| 455 | return bitmap; |
| 456 | } |
| 457 | |
| 458 | class SkNoPixelsBitmapDevice : public SkBitmapDevice { |
| 459 | public: |
| 460 | SkNoPixelsBitmapDevice(int width, int height) : INHERITED(make_nopixels(width, height)) {} |
| 461 | |
| 462 | private: |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 463 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 464 | typedef SkBitmapDevice INHERITED; |
| 465 | }; |
| 466 | |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 467 | SkCanvas::SkCanvas(int width, int height) |
| 468 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 469 | , fProps(SkSurfaceProps::kLegacyFontHost_InitType) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 470 | { |
| 471 | inc_canvas(); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 472 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 473 | this->init(SkNEW_ARGS(SkNoPixelsBitmapDevice, (width, height)), kDefault_InitFlags)->unref(); |
| 474 | } |
skia.committer@gmail.com | ba12448 | 2014-02-01 03:01:57 +0000 | [diff] [blame] | 475 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 476 | SkCanvas::SkCanvas(int width, int height, InitFlags flags) |
| 477 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 478 | , fProps(SkSurfaceProps::kLegacyFontHost_InitType) |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 479 | { |
| 480 | inc_canvas(); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 481 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 482 | this->init(SkNEW_ARGS(SkNoPixelsBitmapDevice, (width, height)), flags)->unref(); |
| 483 | } |
| 484 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 485 | SkCanvas::SkCanvas(SkBaseDevice* device, const SkSurfaceProps* props, InitFlags flags) |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 486 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 487 | , fProps(SkSurfacePropsCopyOrDefault(props)) |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 488 | { |
| 489 | inc_canvas(); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 490 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 491 | this->init(device, flags); |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 492 | } |
| 493 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 494 | SkCanvas::SkCanvas(SkBaseDevice* device) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 495 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 496 | , fProps(SkSurfaceProps::kLegacyFontHost_InitType) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 497 | { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 498 | inc_canvas(); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 499 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 500 | this->init(device, kDefault_InitFlags); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 501 | } |
| 502 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 503 | SkCanvas::SkCanvas(const SkBitmap& bitmap, const SkSurfaceProps& props) |
reed | 3716fd0 | 2014-09-21 09:39:55 -0700 | [diff] [blame] | 504 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 505 | , fProps(props) |
reed | 3716fd0 | 2014-09-21 09:39:55 -0700 | [diff] [blame] | 506 | { |
| 507 | inc_canvas(); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 508 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 509 | SkAutoTUnref<SkBaseDevice> device(SkNEW_ARGS(SkBitmapDevice, (bitmap))); |
| 510 | this->init(device, kDefault_InitFlags); |
| 511 | } |
reed | 29c857d | 2014-09-21 10:25:07 -0700 | [diff] [blame] | 512 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 513 | SkCanvas::SkCanvas(const SkBitmap& bitmap) |
| 514 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
| 515 | , fProps(SkSurfaceProps::kLegacyFontHost_InitType) |
| 516 | { |
| 517 | inc_canvas(); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 518 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 519 | SkAutoTUnref<SkBaseDevice> device(SkNEW_ARGS(SkBitmapDevice, (bitmap))); |
| 520 | this->init(device, kDefault_InitFlags); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | SkCanvas::~SkCanvas() { |
| 524 | // free up the contents of our deque |
| 525 | this->restoreToCount(1); // restore everything but the last |
junov@chromium.org | b0a7ace | 2012-04-05 18:33:23 +0000 | [diff] [blame] | 526 | SkASSERT(0 == fSaveLayerCount); |
reed@google.com | 7c20293 | 2011-12-14 18:48:05 +0000 | [diff] [blame] | 527 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 528 | this->internalRestore(); // restore the last, since we're going away |
| 529 | |
mike@reedtribe.org | 74bb77e | 2012-09-26 02:24:45 +0000 | [diff] [blame] | 530 | SkDELETE(fMetaData); |
vandebo@chromium.org | b70ae31 | 2010-10-15 18:58:19 +0000 | [diff] [blame] | 531 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 532 | dec_canvas(); |
| 533 | } |
| 534 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 535 | SkDrawFilter* SkCanvas::getDrawFilter() const { |
| 536 | return fMCRec->fFilter; |
| 537 | } |
| 538 | |
| 539 | SkDrawFilter* SkCanvas::setDrawFilter(SkDrawFilter* filter) { |
| 540 | SkRefCnt_SafeAssign(fMCRec->fFilter, filter); |
| 541 | return filter; |
| 542 | } |
| 543 | |
mike@reedtribe.org | 74bb77e | 2012-09-26 02:24:45 +0000 | [diff] [blame] | 544 | SkMetaData& SkCanvas::getMetaData() { |
| 545 | // metadata users are rare, so we lazily allocate it. If that changes we |
| 546 | // can decide to just make it a field in the device (rather than a ptr) |
| 547 | if (NULL == fMetaData) { |
| 548 | fMetaData = new SkMetaData; |
| 549 | } |
| 550 | return *fMetaData; |
| 551 | } |
| 552 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 553 | /////////////////////////////////////////////////////////////////////////////// |
| 554 | |
junov@chromium.org | bf6c1e4 | 2012-01-30 14:53:22 +0000 | [diff] [blame] | 555 | void SkCanvas::flush() { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 556 | SkBaseDevice* device = this->getDevice(); |
junov@chromium.org | bf6c1e4 | 2012-01-30 14:53:22 +0000 | [diff] [blame] | 557 | if (device) { |
| 558 | device->flush(); |
| 559 | } |
| 560 | } |
| 561 | |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 562 | SkISize SkCanvas::getTopLayerSize() const { |
| 563 | SkBaseDevice* d = this->getTopDevice(); |
| 564 | return d ? SkISize::Make(d->width(), d->height()) : SkISize::Make(0, 0); |
| 565 | } |
| 566 | |
| 567 | SkIPoint SkCanvas::getTopLayerOrigin() const { |
| 568 | SkBaseDevice* d = this->getTopDevice(); |
| 569 | return d ? d->getOrigin() : SkIPoint::Make(0, 0); |
| 570 | } |
| 571 | |
| 572 | SkISize SkCanvas::getBaseLayerSize() const { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 573 | SkBaseDevice* d = this->getDevice(); |
reed@google.com | 210ce00 | 2011-11-01 14:24:23 +0000 | [diff] [blame] | 574 | return d ? SkISize::Make(d->width(), d->height()) : SkISize::Make(0, 0); |
| 575 | } |
| 576 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 577 | SkBaseDevice* SkCanvas::getDevice() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 578 | // return root device |
robertphillips@google.com | c029062 | 2012-07-16 21:20:03 +0000 | [diff] [blame] | 579 | MCRec* rec = (MCRec*) fMCStack.front(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 580 | SkASSERT(rec && rec->fLayer); |
| 581 | return rec->fLayer->fDevice; |
| 582 | } |
| 583 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 584 | SkBaseDevice* SkCanvas::getTopDevice(bool updateMatrixClip) const { |
reed@google.com | 0b53d59 | 2012-03-19 18:26:34 +0000 | [diff] [blame] | 585 | if (updateMatrixClip) { |
| 586 | const_cast<SkCanvas*>(this)->updateDeviceCMCache(); |
| 587 | } |
reed@google.com | 9266fed | 2011-03-30 00:18:03 +0000 | [diff] [blame] | 588 | return fMCRec->fTopLayer->fDevice; |
| 589 | } |
| 590 | |
commit-bot@chromium.org | 403f8d7 | 2014-02-17 15:24:26 +0000 | [diff] [blame] | 591 | SkBaseDevice* SkCanvas::setRootDevice(SkBaseDevice* device) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 592 | // return root device |
reed@google.com | 4c09d5c | 2011-02-22 13:16:38 +0000 | [diff] [blame] | 593 | SkDeque::F2BIter iter(fMCStack); |
| 594 | MCRec* rec = (MCRec*)iter.next(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 595 | SkASSERT(rec && rec->fLayer); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 596 | SkBaseDevice* rootDevice = rec->fLayer->fDevice; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 597 | |
| 598 | if (rootDevice == device) { |
| 599 | return device; |
| 600 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 601 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 602 | if (device) { |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 603 | device->onAttachToCanvas(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 604 | } |
| 605 | if (rootDevice) { |
robertphillips@google.com | 40a1ae4 | 2012-07-13 15:36:15 +0000 | [diff] [blame] | 606 | rootDevice->onDetachFromCanvas(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | SkRefCnt_SafeAssign(rec->fLayer->fDevice, device); |
| 610 | rootDevice = device; |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 611 | this->setupDevice(device); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 612 | |
| 613 | fDeviceCMDirty = true; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 614 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 615 | /* Now we update our initial region to have the bounds of the new device, |
| 616 | and then intersect all of the clips in our stack with these bounds, |
| 617 | to ensure that we can't draw outside of the device's bounds (and trash |
| 618 | memory). |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 619 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 620 | NOTE: this is only a partial-fix, since if the new device is larger than |
| 621 | the previous one, we don't know how to "enlarge" the clips in our stack, |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 622 | so drawing may be artificially restricted. Without keeping a history of |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 623 | all calls to canvas->clipRect() and canvas->clipPath(), we can't exactly |
| 624 | reconstruct the correct clips, so this approximation will have to do. |
| 625 | The caller really needs to restore() back to the base if they want to |
| 626 | accurately take advantage of the new device bounds. |
| 627 | */ |
| 628 | |
reed@google.com | 42aea28 | 2012-03-28 16:19:15 +0000 | [diff] [blame] | 629 | SkIRect bounds; |
| 630 | if (device) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 631 | bounds.set(0, 0, device->width(), device->height()); |
reed@google.com | 42aea28 | 2012-03-28 16:19:15 +0000 | [diff] [blame] | 632 | } else { |
| 633 | bounds.setEmpty(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 634 | } |
reed@google.com | 42aea28 | 2012-03-28 16:19:15 +0000 | [diff] [blame] | 635 | // now jam our 1st clip to be bounds, and intersect the rest with that |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 636 | rec->fRasterClip.setRect(bounds); |
reed@google.com | 42aea28 | 2012-03-28 16:19:15 +0000 | [diff] [blame] | 637 | while ((rec = (MCRec*)iter.next()) != NULL) { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 638 | (void)rec->fRasterClip.op(bounds, SkRegion::kIntersect_Op); |
reed@google.com | 42aea28 | 2012-03-28 16:19:15 +0000 | [diff] [blame] | 639 | } |
| 640 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 641 | return device; |
| 642 | } |
| 643 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 644 | bool SkCanvas::readPixels(SkBitmap* bitmap, int x, int y) { |
| 645 | if (kUnknown_SkColorType == bitmap->colorType() || bitmap->getTexture()) { |
| 646 | return false; |
| 647 | } |
| 648 | |
| 649 | bool weAllocated = false; |
| 650 | if (NULL == bitmap->pixelRef()) { |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 651 | if (!bitmap->tryAllocPixels()) { |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 652 | return false; |
| 653 | } |
| 654 | weAllocated = true; |
| 655 | } |
| 656 | |
| 657 | SkBitmap bm(*bitmap); |
| 658 | bm.lockPixels(); |
| 659 | if (bm.getPixels() && this->readPixels(bm.info(), bm.getPixels(), bm.rowBytes(), x, y)) { |
| 660 | return true; |
| 661 | } |
| 662 | |
| 663 | if (weAllocated) { |
| 664 | bitmap->setPixelRef(NULL); |
| 665 | } |
| 666 | return false; |
| 667 | } |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 668 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 669 | bool SkCanvas::readPixels(const SkIRect& srcRect, SkBitmap* bitmap) { |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 670 | SkIRect r = srcRect; |
| 671 | const SkISize size = this->getBaseLayerSize(); |
| 672 | if (!r.intersect(0, 0, size.width(), size.height())) { |
| 673 | bitmap->reset(); |
| 674 | return false; |
| 675 | } |
| 676 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 677 | if (!bitmap->tryAllocN32Pixels(r.width(), r.height())) { |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 678 | // bitmap will already be reset. |
| 679 | return false; |
| 680 | } |
| 681 | if (!this->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), r.x(), r.y())) { |
| 682 | bitmap->reset(); |
| 683 | return false; |
| 684 | } |
| 685 | return true; |
| 686 | } |
| 687 | |
| 688 | bool SkCanvas::readPixels(const SkImageInfo& origInfo, void* dstP, size_t rowBytes, int x, int y) { |
| 689 | switch (origInfo.colorType()) { |
| 690 | case kUnknown_SkColorType: |
| 691 | case kIndex_8_SkColorType: |
| 692 | return false; |
| 693 | default: |
| 694 | break; |
| 695 | } |
| 696 | if (NULL == dstP || rowBytes < origInfo.minRowBytes()) { |
| 697 | return false; |
| 698 | } |
| 699 | if (0 == origInfo.width() || 0 == origInfo.height()) { |
| 700 | return false; |
| 701 | } |
| 702 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 703 | SkBaseDevice* device = this->getDevice(); |
djsollen@google.com | ccfee2a | 2012-05-01 16:50:10 +0000 | [diff] [blame] | 704 | if (!device) { |
| 705 | return false; |
| 706 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 707 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 708 | const SkISize size = this->getBaseLayerSize(); |
| 709 | SkIRect srcR = SkIRect::MakeXYWH(x, y, origInfo.width(), origInfo.height()); |
| 710 | if (!srcR.intersect(0, 0, size.width(), size.height())) { |
bsalomon@google.com | daba14b | 2011-11-02 20:10:48 +0000 | [diff] [blame] | 711 | return false; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 712 | } |
skia.committer@gmail.com | db0c875 | 2014-03-18 03:02:11 +0000 | [diff] [blame] | 713 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 714 | // the intersect may have shrunk info's logical size |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 715 | const SkImageInfo info = origInfo.makeWH(srcR.width(), srcR.height()); |
skia.committer@gmail.com | db0c875 | 2014-03-18 03:02:11 +0000 | [diff] [blame] | 716 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 717 | // if x or y are negative, then we have to adjust pixels |
| 718 | if (x > 0) { |
| 719 | x = 0; |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 720 | } |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 721 | if (y > 0) { |
| 722 | y = 0; |
| 723 | } |
| 724 | // here x,y are either 0 or negative |
| 725 | dstP = ((char*)dstP - y * rowBytes - x * info.bytesPerPixel()); |
skia.committer@gmail.com | db0c875 | 2014-03-18 03:02:11 +0000 | [diff] [blame] | 726 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 727 | // The device can assert that the requested area is always contained in its bounds |
| 728 | return device->readPixels(info, dstP, rowBytes, srcR.x(), srcR.y()); |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 729 | } |
| 730 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 731 | bool SkCanvas::writePixels(const SkBitmap& bitmap, int x, int y) { |
| 732 | if (bitmap.getTexture()) { |
| 733 | return false; |
| 734 | } |
| 735 | SkBitmap bm(bitmap); |
| 736 | bm.lockPixels(); |
| 737 | if (bm.getPixels()) { |
| 738 | return this->writePixels(bm.info(), bm.getPixels(), bm.rowBytes(), x, y); |
| 739 | } |
| 740 | return false; |
| 741 | } |
| 742 | |
| 743 | bool SkCanvas::writePixels(const SkImageInfo& origInfo, const void* pixels, size_t rowBytes, |
| 744 | int x, int y) { |
| 745 | switch (origInfo.colorType()) { |
| 746 | case kUnknown_SkColorType: |
| 747 | case kIndex_8_SkColorType: |
| 748 | return false; |
| 749 | default: |
| 750 | break; |
| 751 | } |
| 752 | if (NULL == pixels || rowBytes < origInfo.minRowBytes()) { |
| 753 | return false; |
| 754 | } |
| 755 | |
| 756 | const SkISize size = this->getBaseLayerSize(); |
| 757 | SkIRect target = SkIRect::MakeXYWH(x, y, origInfo.width(), origInfo.height()); |
| 758 | if (!target.intersect(0, 0, size.width(), size.height())) { |
| 759 | return false; |
| 760 | } |
| 761 | |
| 762 | SkBaseDevice* device = this->getDevice(); |
| 763 | if (!device) { |
| 764 | return false; |
| 765 | } |
| 766 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 767 | // the intersect may have shrunk info's logical size |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 768 | const SkImageInfo info = origInfo.makeWH(target.width(), target.height()); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 769 | |
| 770 | // if x or y are negative, then we have to adjust pixels |
| 771 | if (x > 0) { |
| 772 | x = 0; |
| 773 | } |
| 774 | if (y > 0) { |
| 775 | y = 0; |
| 776 | } |
| 777 | // here x,y are either 0 or negative |
| 778 | pixels = ((const char*)pixels - y * rowBytes - x * info.bytesPerPixel()); |
| 779 | |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 780 | // Tell our owning surface to bump its generation ID |
| 781 | this->predrawNotify(); |
| 782 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 783 | // The device can assert that the requested area is always contained in its bounds |
commit-bot@chromium.org | 4ef54f8 | 2014-03-17 17:03:18 +0000 | [diff] [blame] | 784 | return device->writePixels(info, pixels, rowBytes, target.x(), target.y()); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 785 | } |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 786 | |
junov@google.com | 4370aed | 2012-01-18 16:21:08 +0000 | [diff] [blame] | 787 | SkCanvas* SkCanvas::canvasForDrawIter() { |
| 788 | return this; |
| 789 | } |
| 790 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 791 | ////////////////////////////////////////////////////////////////////////////// |
| 792 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 793 | void SkCanvas::updateDeviceCMCache() { |
| 794 | if (fDeviceCMDirty) { |
| 795 | const SkMatrix& totalMatrix = this->getTotalMatrix(); |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 796 | const SkRasterClip& totalClip = fMCRec->fRasterClip; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 797 | DeviceCM* layer = fMCRec->fTopLayer; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 798 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 799 | if (NULL == layer->fNext) { // only one layer |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 800 | layer->updateMC(totalMatrix, totalClip, fClipStack, NULL); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 801 | } else { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 802 | SkRasterClip clip(totalClip); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 803 | do { |
reed@google.com | 46799cd | 2011-02-22 20:56:26 +0000 | [diff] [blame] | 804 | layer->updateMC(totalMatrix, clip, fClipStack, &clip); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 805 | } while ((layer = layer->fNext) != NULL); |
| 806 | } |
| 807 | fDeviceCMDirty = false; |
| 808 | } |
| 809 | } |
| 810 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 811 | /////////////////////////////////////////////////////////////////////////////// |
| 812 | |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 813 | int SkCanvas::internalSave() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 814 | int saveCount = this->getSaveCount(); // record this before the actual save |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 815 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 816 | MCRec* newTop = (MCRec*)fMCStack.push_back(); |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 817 | new (newTop) MCRec(*fMCRec); // balanced in restore() |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 818 | fMCRec = newTop; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 819 | |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 820 | fClipStack.save(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 821 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 822 | return saveCount; |
| 823 | } |
| 824 | |
commit-bot@chromium.org | d70fa20 | 2014-04-24 21:51:58 +0000 | [diff] [blame] | 825 | int SkCanvas::save() { |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 826 | this->willSave(); |
| 827 | return this->internalSave(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 828 | } |
| 829 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 830 | static bool bounds_affects_clip(SkCanvas::SaveFlags flags) { |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 831 | #ifdef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 832 | return (flags & SkCanvas::kClipToLayer_SaveFlag) != 0; |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 833 | #else |
| 834 | return true; |
| 835 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 836 | } |
| 837 | |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 838 | bool SkCanvas::clipRectBounds(const SkRect* bounds, SaveFlags flags, |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 839 | SkIRect* intersection, const SkImageFilter* imageFilter) { |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 840 | SkIRect clipBounds; |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 841 | SkRegion::Op op = SkRegion::kIntersect_Op; |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 842 | if (!this->getClipDeviceBounds(&clipBounds)) { |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 843 | return false; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 844 | } |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 845 | |
| 846 | if (imageFilter) { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 847 | imageFilter->filterBounds(clipBounds, fMCRec->fMatrix, &clipBounds); |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 848 | // Filters may grow the bounds beyond the device bounds. |
| 849 | op = SkRegion::kReplace_Op; |
| 850 | } |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 851 | SkIRect ir; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 852 | if (bounds) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 853 | SkRect r; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 854 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 855 | this->getTotalMatrix().mapRect(&r, *bounds); |
| 856 | r.roundOut(&ir); |
| 857 | // early exit if the layer's bounds are clipped out |
| 858 | if (!ir.intersect(clipBounds)) { |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 859 | if (bounds_affects_clip(flags)) { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 860 | fMCRec->fRasterClip.setEmpty(); |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 861 | } |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 862 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 863 | } |
| 864 | } else { // no user bounds, so just use the clip |
| 865 | ir = clipBounds; |
| 866 | } |
| 867 | |
senorblanco@chromium.org | 89f077c | 2014-02-24 15:16:42 +0000 | [diff] [blame] | 868 | if (bounds_affects_clip(flags)) { |
| 869 | fClipStack.clipDevRect(ir, op); |
| 870 | // early exit if the clip is now empty |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 871 | if (!fMCRec->fRasterClip.op(ir, op)) { |
senorblanco@chromium.org | 89f077c | 2014-02-24 15:16:42 +0000 | [diff] [blame] | 872 | return false; |
| 873 | } |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 874 | } |
| 875 | |
| 876 | if (intersection) { |
| 877 | *intersection = ir; |
| 878 | } |
| 879 | return true; |
| 880 | } |
| 881 | |
commit-bot@chromium.org | d70fa20 | 2014-04-24 21:51:58 +0000 | [diff] [blame] | 882 | int SkCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint) { |
| 883 | SaveLayerStrategy strategy = this->willSaveLayer(bounds, paint, kARGB_ClipLayer_SaveFlag); |
| 884 | return this->internalSaveLayer(bounds, paint, kARGB_ClipLayer_SaveFlag, false, strategy); |
| 885 | } |
| 886 | |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 887 | int SkCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint, |
| 888 | SaveFlags flags) { |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 889 | SaveLayerStrategy strategy = this->willSaveLayer(bounds, paint, flags); |
| 890 | return this->internalSaveLayer(bounds, paint, flags, false, strategy); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 891 | } |
| 892 | |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 893 | int SkCanvas::internalSaveLayer(const SkRect* bounds, const SkPaint* paint, SaveFlags flags, |
| 894 | bool justForImageFilter, SaveLayerStrategy strategy) { |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 895 | #ifndef SK_SUPPORT_LEGACY_CLIPTOLAYERFLAG |
commit-bot@chromium.org | 2a5cd60 | 2014-05-30 20:41:20 +0000 | [diff] [blame] | 896 | flags |= kClipToLayer_SaveFlag; |
reed@google.com | b93ba45 | 2014-03-10 19:47:58 +0000 | [diff] [blame] | 897 | #endif |
| 898 | |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 899 | // do this before we create the layer. We don't call the public save() since |
| 900 | // that would invoke a possibly overridden virtual |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 901 | int count = this->internalSave(); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 902 | |
| 903 | fDeviceCMDirty = true; |
| 904 | |
| 905 | SkIRect ir; |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 906 | if (!this->clipRectBounds(bounds, flags, &ir, paint ? paint->getImageFilter() : NULL)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 907 | return count; |
| 908 | } |
| 909 | |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 910 | // FIXME: do willSaveLayer() overriders returning kNoLayer_SaveLayerStrategy really care about |
| 911 | // the clipRectBounds() call above? |
| 912 | if (kNoLayer_SaveLayerStrategy == strategy) { |
| 913 | return count; |
| 914 | } |
| 915 | |
reed@google.com | b55deeb | 2012-01-06 14:43:09 +0000 | [diff] [blame] | 916 | // Kill the imagefilter if our device doesn't allow it |
| 917 | SkLazyPaint lazyP; |
| 918 | if (paint && paint->getImageFilter()) { |
| 919 | if (!this->getTopDevice()->allowImageFilter(paint->getImageFilter())) { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 920 | if (justForImageFilter) { |
| 921 | // early exit if the layer was just for the imageFilter |
| 922 | return count; |
| 923 | } |
reed@google.com | b55deeb | 2012-01-06 14:43:09 +0000 | [diff] [blame] | 924 | SkPaint* p = lazyP.set(*paint); |
| 925 | p->setImageFilter(NULL); |
| 926 | paint = p; |
| 927 | } |
| 928 | } |
| 929 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 930 | bool isOpaque = !SkToBool(flags & kHasAlphaLayer_SaveFlag); |
| 931 | SkImageInfo info = SkImageInfo::MakeN32(ir.width(), ir.height(), |
| 932 | isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 933 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 934 | SkBaseDevice* device; |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 935 | if (paint && paint->getImageFilter()) { |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 936 | device = this->getDevice(); |
| 937 | if (device) { |
senorblanco | b0e89dc | 2014-10-20 14:03:12 -0700 | [diff] [blame] | 938 | device = device->createCompatibleDeviceForImageFilter(info); |
reed | 52d9ac6 | 2014-06-30 09:05:34 -0700 | [diff] [blame] | 939 | } |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 940 | } else { |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 941 | device = this->createLayerDevice(info); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 942 | } |
bungeman@google.com | e25c684 | 2011-08-17 14:53:54 +0000 | [diff] [blame] | 943 | if (NULL == device) { |
| 944 | SkDebugf("Unable to create device for layer."); |
| 945 | return count; |
| 946 | } |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 947 | this->setupDevice(device); |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 948 | |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 949 | device->setOrigin(ir.fLeft, ir.fTop); |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 950 | DeviceCM* layer = SkNEW_ARGS(DeviceCM, |
| 951 | (device, ir.fLeft, ir.fTop, paint, this, fConservativeRasterClip)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 952 | device->unref(); |
| 953 | |
| 954 | layer->fNext = fMCRec->fTopLayer; |
| 955 | fMCRec->fLayer = layer; |
| 956 | fMCRec->fTopLayer = layer; // this field is NOT an owner of layer |
| 957 | |
junov@chromium.org | b0a7ace | 2012-04-05 18:33:23 +0000 | [diff] [blame] | 958 | fSaveLayerCount += 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 959 | return count; |
| 960 | } |
| 961 | |
commit-bot@chromium.org | d70fa20 | 2014-04-24 21:51:58 +0000 | [diff] [blame] | 962 | int SkCanvas::saveLayerAlpha(const SkRect* bounds, U8CPU alpha) { |
| 963 | return this->saveLayerAlpha(bounds, alpha, kARGB_ClipLayer_SaveFlag); |
| 964 | } |
| 965 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 966 | int SkCanvas::saveLayerAlpha(const SkRect* bounds, U8CPU alpha, |
| 967 | SaveFlags flags) { |
| 968 | if (0xFF == alpha) { |
| 969 | return this->saveLayer(bounds, NULL, flags); |
| 970 | } else { |
| 971 | SkPaint tmpPaint; |
| 972 | tmpPaint.setAlpha(alpha); |
| 973 | return this->saveLayer(bounds, &tmpPaint, flags); |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | void SkCanvas::restore() { |
| 978 | // check for underflow |
| 979 | if (fMCStack.count() > 1) { |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 980 | this->willRestore(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 981 | this->internalRestore(); |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 982 | this->didRestore(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 983 | } |
| 984 | } |
| 985 | |
| 986 | void SkCanvas::internalRestore() { |
| 987 | SkASSERT(fMCStack.count() != 0); |
| 988 | |
| 989 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 990 | fCachedLocalClipBoundsDirty = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 991 | |
Florin Malita | 5f6102d | 2014-06-30 10:13:28 -0400 | [diff] [blame] | 992 | fClipStack.restore(); |
commit-bot@chromium.org | 6c15764 | 2013-08-16 00:53:34 +0000 | [diff] [blame] | 993 | |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 994 | // reserve our layer (if any) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 995 | DeviceCM* layer = fMCRec->fLayer; // may be null |
| 996 | // now detach it from fMCRec so we can pop(). Gets freed after its drawn |
| 997 | fMCRec->fLayer = NULL; |
| 998 | |
| 999 | // now do the normal restore() |
| 1000 | fMCRec->~MCRec(); // balanced in save() |
| 1001 | fMCStack.pop_back(); |
| 1002 | fMCRec = (MCRec*)fMCStack.back(); |
| 1003 | |
| 1004 | /* Time to draw the layer's offscreen. We can't call the public drawSprite, |
| 1005 | since if we're being recorded, we don't want to record this (the |
| 1006 | recorder will have already recorded the restore). |
| 1007 | */ |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1008 | if (layer) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1009 | if (layer->fNext) { |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 1010 | const SkIPoint& origin = layer->fDevice->getOrigin(); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1011 | this->internalDrawDevice(layer->fDevice, origin.x(), origin.y(), |
| 1012 | layer->fPaint); |
| 1013 | // reset this, since internalDrawDevice will have set it to true |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1014 | fDeviceCMDirty = true; |
reed@google.com | 7c20293 | 2011-12-14 18:48:05 +0000 | [diff] [blame] | 1015 | |
junov@chromium.org | b0a7ace | 2012-04-05 18:33:23 +0000 | [diff] [blame] | 1016 | SkASSERT(fSaveLayerCount > 0); |
| 1017 | fSaveLayerCount -= 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1018 | } |
| 1019 | SkDELETE(layer); |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 1020 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | int SkCanvas::getSaveCount() const { |
| 1024 | return fMCStack.count(); |
| 1025 | } |
| 1026 | |
| 1027 | void SkCanvas::restoreToCount(int count) { |
| 1028 | // sanity check |
| 1029 | if (count < 1) { |
| 1030 | count = 1; |
| 1031 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 1032 | |
reed@google.com | b9d1c6a | 2011-11-28 16:09:24 +0000 | [diff] [blame] | 1033 | int n = this->getSaveCount() - count; |
| 1034 | for (int i = 0; i < n; ++i) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1035 | this->restore(); |
| 1036 | } |
| 1037 | } |
| 1038 | |
reed@google.com | 7c20293 | 2011-12-14 18:48:05 +0000 | [diff] [blame] | 1039 | bool SkCanvas::isDrawingToLayer() const { |
junov@chromium.org | b0a7ace | 2012-04-05 18:33:23 +0000 | [diff] [blame] | 1040 | return fSaveLayerCount > 0; |
reed@google.com | 7c20293 | 2011-12-14 18:48:05 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 1043 | SkSurface* SkCanvas::newSurface(const SkImageInfo& info, const SkSurfaceProps* props) { |
| 1044 | if (NULL == props) { |
| 1045 | props = &fProps; |
| 1046 | } |
| 1047 | return this->onNewSurface(info, *props); |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 1048 | } |
| 1049 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 1050 | SkSurface* SkCanvas::onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props) { |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 1051 | SkBaseDevice* dev = this->getDevice(); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 1052 | return dev ? dev->newSurface(info, props) : NULL; |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 1053 | } |
| 1054 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1055 | SkImageInfo SkCanvas::imageInfo() const { |
| 1056 | SkBaseDevice* dev = this->getDevice(); |
| 1057 | if (dev) { |
| 1058 | return dev->imageInfo(); |
| 1059 | } else { |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 1060 | return SkImageInfo::MakeUnknown(0, 0); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | const void* SkCanvas::peekPixels(SkImageInfo* info, size_t* rowBytes) { |
| 1065 | return this->onPeekPixels(info, rowBytes); |
| 1066 | } |
| 1067 | |
| 1068 | const void* SkCanvas::onPeekPixels(SkImageInfo* info, size_t* rowBytes) { |
| 1069 | SkBaseDevice* dev = this->getDevice(); |
| 1070 | return dev ? dev->peekPixels(info, rowBytes) : NULL; |
| 1071 | } |
| 1072 | |
commit-bot@chromium.org | 6b4aaa7 | 2014-04-21 21:09:38 +0000 | [diff] [blame] | 1073 | void* SkCanvas::accessTopLayerPixels(SkImageInfo* info, size_t* rowBytes, SkIPoint* origin) { |
| 1074 | void* pixels = this->onAccessTopLayerPixels(info, rowBytes); |
| 1075 | if (pixels && origin) { |
| 1076 | *origin = this->getTopDevice(false)->getOrigin(); |
| 1077 | } |
| 1078 | return pixels; |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | void* SkCanvas::onAccessTopLayerPixels(SkImageInfo* info, size_t* rowBytes) { |
| 1082 | SkBaseDevice* dev = this->getTopDevice(); |
| 1083 | return dev ? dev->accessPixels(info, rowBytes) : NULL; |
| 1084 | } |
| 1085 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1086 | SkAutoROCanvasPixels::SkAutoROCanvasPixels(SkCanvas* canvas) { |
| 1087 | fAddr = canvas->peekPixels(&fInfo, &fRowBytes); |
| 1088 | if (NULL == fAddr) { |
| 1089 | fInfo = canvas->imageInfo(); |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 1090 | if (kUnknown_SkColorType == fInfo.colorType() || !fBitmap.tryAllocPixels(fInfo)) { |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1091 | return; // failure, fAddr is NULL |
| 1092 | } |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1093 | if (!canvas->readPixels(&fBitmap, 0, 0)) { |
| 1094 | return; // failure, fAddr is NULL |
| 1095 | } |
| 1096 | fAddr = fBitmap.getPixels(); |
| 1097 | fRowBytes = fBitmap.rowBytes(); |
| 1098 | } |
| 1099 | SkASSERT(fAddr); // success |
| 1100 | } |
| 1101 | |
| 1102 | bool SkAutoROCanvasPixels::asROBitmap(SkBitmap* bitmap) const { |
| 1103 | if (fAddr) { |
commit-bot@chromium.org | 00f8d6c | 2014-05-29 15:57:20 +0000 | [diff] [blame] | 1104 | return bitmap->installPixels(fInfo, const_cast<void*>(fAddr), fRowBytes); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1105 | } else { |
| 1106 | bitmap->reset(); |
| 1107 | return false; |
| 1108 | } |
| 1109 | } |
| 1110 | |
commit-bot@chromium.org | 210ae2a | 2014-02-27 17:40:13 +0000 | [diff] [blame] | 1111 | void SkCanvas::onPushCull(const SkRect& cullRect) { |
| 1112 | // do nothing. Subclasses may do something |
| 1113 | } |
| 1114 | |
| 1115 | void SkCanvas::onPopCull() { |
| 1116 | // do nothing. Subclasses may do something |
| 1117 | } |
| 1118 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1119 | ///////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 520cf8b | 2014-03-20 20:25:14 +0000 | [diff] [blame] | 1120 | #ifdef SK_DEBUG |
| 1121 | // Ensure that cull rects are monotonically nested in device space. |
| 1122 | void SkCanvas::validateCull(const SkIRect& devCull) { |
| 1123 | if (fCullStack.isEmpty() |
| 1124 | || devCull.isEmpty() |
| 1125 | || fCullStack.top().contains(devCull)) { |
| 1126 | return; |
| 1127 | } |
| 1128 | |
| 1129 | SkDEBUGF(("Invalid cull: [%d %d %d %d] (previous cull: [%d %d %d %d])\n", |
| 1130 | devCull.x(), devCull.y(), devCull.right(), devCull.bottom(), |
| 1131 | fCullStack.top().x(), fCullStack.top().y(), |
| 1132 | fCullStack.top().right(), fCullStack.top().bottom())); |
| 1133 | |
| 1134 | #ifdef ASSERT_NESTED_CULLING |
| 1135 | SkDEBUGFAIL("Invalid cull."); |
| 1136 | #endif |
| 1137 | } |
| 1138 | #endif |
| 1139 | |
| 1140 | void SkCanvas::pushCull(const SkRect& cullRect) { |
| 1141 | ++fCullCount; |
| 1142 | this->onPushCull(cullRect); |
| 1143 | |
| 1144 | #ifdef SK_DEBUG |
| 1145 | // Map the cull rect into device space. |
| 1146 | SkRect mappedCull; |
| 1147 | this->getTotalMatrix().mapRect(&mappedCull, cullRect); |
| 1148 | |
| 1149 | // Take clipping into account. |
| 1150 | SkIRect devClip, devCull; |
| 1151 | mappedCull.roundOut(&devCull); |
| 1152 | this->getClipDeviceBounds(&devClip); |
| 1153 | if (!devCull.intersect(devClip)) { |
| 1154 | devCull.setEmpty(); |
| 1155 | } |
| 1156 | |
| 1157 | this->validateCull(devCull); |
| 1158 | fCullStack.push(devCull); // balanced in popCull |
| 1159 | #endif |
| 1160 | } |
| 1161 | |
| 1162 | void SkCanvas::popCull() { |
| 1163 | SkASSERT(fCullStack.count() == fCullCount); |
| 1164 | |
| 1165 | if (fCullCount > 0) { |
| 1166 | --fCullCount; |
| 1167 | this->onPopCull(); |
| 1168 | |
| 1169 | SkDEBUGCODE(fCullStack.pop()); |
| 1170 | } |
| 1171 | } |
| 1172 | |
| 1173 | ///////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1174 | |
robertphillips@google.com | 9bf380c | 2013-07-25 12:10:42 +0000 | [diff] [blame] | 1175 | void SkCanvas::internalDrawBitmap(const SkBitmap& bitmap, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1176 | const SkMatrix& matrix, const SkPaint* paint) { |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1177 | if (bitmap.drawsNothing()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1178 | return; |
| 1179 | } |
| 1180 | |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 1181 | SkLazyPaint lazy; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1182 | if (NULL == paint) { |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 1183 | paint = lazy.init(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1184 | } |
robertphillips@google.com | 9bf380c | 2013-07-25 12:10:42 +0000 | [diff] [blame] | 1185 | |
| 1186 | SkDEBUGCODE(bitmap.validate();) |
robertphillips@google.com | 9bf380c | 2013-07-25 12:10:42 +0000 | [diff] [blame] | 1187 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1188 | SkRect storage; |
| 1189 | const SkRect* bounds = NULL; |
| 1190 | if (paint && paint->canComputeFastBounds()) { |
| 1191 | bitmap.getBounds(&storage); |
| 1192 | matrix.mapRect(&storage); |
| 1193 | bounds = &paint->computeFastBounds(storage, &storage); |
| 1194 | } |
| 1195 | |
| 1196 | LOOPER_BEGIN(*paint, SkDrawFilter::kBitmap_Type, bounds) |
robertphillips@google.com | 9bf380c | 2013-07-25 12:10:42 +0000 | [diff] [blame] | 1197 | |
| 1198 | while (iter.next()) { |
| 1199 | iter.fDevice->drawBitmap(iter, bitmap, matrix, looper.paint()); |
| 1200 | } |
| 1201 | |
| 1202 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1205 | void SkCanvas::internalDrawDevice(SkBaseDevice* srcDev, int x, int y, |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1206 | const SkPaint* paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1207 | SkPaint tmp; |
| 1208 | if (NULL == paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1209 | paint = &tmp; |
| 1210 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1211 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1212 | LOOPER_BEGIN_DRAWDEVICE(*paint, SkDrawFilter::kBitmap_Type) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1213 | while (iter.next()) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1214 | SkBaseDevice* dstDev = iter.fDevice; |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1215 | paint = &looper.paint(); |
| 1216 | SkImageFilter* filter = paint->getImageFilter(); |
| 1217 | SkIPoint pos = { x - iter.getX(), y - iter.getY() }; |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1218 | if (filter && !dstDev->canHandleImageFilter(filter)) { |
senorblanco@chromium.org | 9c39744 | 2012-09-27 21:57:45 +0000 | [diff] [blame] | 1219 | SkDeviceImageFilterProxy proxy(dstDev); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1220 | SkBitmap dst; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1221 | SkIPoint offset = SkIPoint::Make(0, 0); |
reed@google.com | b55deeb | 2012-01-06 14:43:09 +0000 | [diff] [blame] | 1222 | const SkBitmap& src = srcDev->accessBitmap(false); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1223 | SkMatrix matrix = *iter.fMatrix; |
senorblanco@chromium.org | d5424a4 | 2014-04-02 19:20:05 +0000 | [diff] [blame] | 1224 | matrix.postTranslate(SkIntToScalar(-pos.x()), SkIntToScalar(-pos.y())); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1225 | SkIRect clipBounds = SkIRect::MakeWH(srcDev->width(), srcDev->height()); |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 1226 | SkAutoTUnref<SkImageFilter::Cache> cache(dstDev->getImageFilterCache()); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 1227 | SkImageFilter::Context ctx(matrix, clipBounds, cache.get()); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1228 | if (filter->filterImage(&proxy, src, ctx, &dst, &offset)) { |
tomhudson@google.com | 5efe0cb | 2012-04-10 19:14:48 +0000 | [diff] [blame] | 1229 | SkPaint tmpUnfiltered(*paint); |
| 1230 | tmpUnfiltered.setImageFilter(NULL); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1231 | dstDev->drawSprite(iter, dst, pos.x() + offset.x(), pos.y() + offset.y(), |
| 1232 | tmpUnfiltered); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1233 | } |
| 1234 | } else { |
reed@google.com | b55deeb | 2012-01-06 14:43:09 +0000 | [diff] [blame] | 1235 | dstDev->drawDevice(iter, srcDev, pos.x(), pos.y(), *paint); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1236 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1237 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1238 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1241 | void SkCanvas::drawSprite(const SkBitmap& bitmap, int x, int y, |
| 1242 | const SkPaint* paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 1243 | TRACE_EVENT0("skia", "SkCanvas::drawSprite()"); |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1244 | if (bitmap.drawsNothing()) { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1245 | return; |
| 1246 | } |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1247 | SkDEBUGCODE(bitmap.validate();) |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1248 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1249 | SkPaint tmp; |
| 1250 | if (NULL == paint) { |
| 1251 | paint = &tmp; |
| 1252 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1253 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1254 | LOOPER_BEGIN_DRAWDEVICE(*paint, SkDrawFilter::kBitmap_Type) |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1255 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1256 | while (iter.next()) { |
| 1257 | paint = &looper.paint(); |
| 1258 | SkImageFilter* filter = paint->getImageFilter(); |
| 1259 | SkIPoint pos = { x - iter.getX(), y - iter.getY() }; |
| 1260 | if (filter && !iter.fDevice->canHandleImageFilter(filter)) { |
senorblanco@chromium.org | 9c39744 | 2012-09-27 21:57:45 +0000 | [diff] [blame] | 1261 | SkDeviceImageFilterProxy proxy(iter.fDevice); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1262 | SkBitmap dst; |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1263 | SkIPoint offset = SkIPoint::Make(0, 0); |
senorblanco@chromium.org | fbaea53 | 2013-08-27 21:37:01 +0000 | [diff] [blame] | 1264 | SkMatrix matrix = *iter.fMatrix; |
senorblanco@chromium.org | d5424a4 | 2014-04-02 19:20:05 +0000 | [diff] [blame] | 1265 | matrix.postTranslate(SkIntToScalar(-pos.x()), SkIntToScalar(-pos.y())); |
halcanary | f622a6c | 2014-10-24 12:54:53 -0700 | [diff] [blame] | 1266 | const SkIRect clipBounds = bitmap.bounds(); |
senorblanco | be129b2 | 2014-08-08 07:14:35 -0700 | [diff] [blame] | 1267 | SkAutoTUnref<SkImageFilter::Cache> cache(iter.fDevice->getImageFilterCache()); |
senorblanco | 55b6d8b | 2014-07-30 11:26:46 -0700 | [diff] [blame] | 1268 | SkImageFilter::Context ctx(matrix, clipBounds, cache.get()); |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 1269 | if (filter->filterImage(&proxy, bitmap, ctx, &dst, &offset)) { |
tomhudson@google.com | 5efe0cb | 2012-04-10 19:14:48 +0000 | [diff] [blame] | 1270 | SkPaint tmpUnfiltered(*paint); |
| 1271 | tmpUnfiltered.setImageFilter(NULL); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 1272 | iter.fDevice->drawSprite(iter, dst, pos.x() + offset.x(), pos.y() + offset.y(), |
tomhudson@google.com | 5efe0cb | 2012-04-10 19:14:48 +0000 | [diff] [blame] | 1273 | tmpUnfiltered); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1274 | } |
| 1275 | } else { |
| 1276 | iter.fDevice->drawSprite(iter, bitmap, pos.x(), pos.y(), *paint); |
| 1277 | } |
| 1278 | } |
| 1279 | LOOPER_END |
| 1280 | } |
| 1281 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1282 | ///////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1283 | void SkCanvas::translate(SkScalar dx, SkScalar dy) { |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 1284 | SkMatrix m; |
| 1285 | m.setTranslate(dx, dy); |
| 1286 | this->concat(m); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1289 | void SkCanvas::scale(SkScalar sx, SkScalar sy) { |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 1290 | SkMatrix m; |
| 1291 | m.setScale(sx, sy); |
| 1292 | this->concat(m); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1295 | void SkCanvas::rotate(SkScalar degrees) { |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 1296 | SkMatrix m; |
| 1297 | m.setRotate(degrees); |
| 1298 | this->concat(m); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1299 | } |
| 1300 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1301 | void SkCanvas::skew(SkScalar sx, SkScalar sy) { |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 1302 | SkMatrix m; |
| 1303 | m.setSkew(sx, sy); |
| 1304 | this->concat(m); |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1307 | void SkCanvas::concat(const SkMatrix& matrix) { |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 1308 | if (matrix.isIdentity()) { |
| 1309 | return; |
| 1310 | } |
| 1311 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1312 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1313 | fCachedLocalClipBoundsDirty = true; |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1314 | fMCRec->fMatrix.preConcat(matrix); |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 1315 | |
| 1316 | this->didConcat(matrix); |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1319 | void SkCanvas::setMatrix(const SkMatrix& matrix) { |
| 1320 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1321 | fCachedLocalClipBoundsDirty = true; |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1322 | fMCRec->fMatrix = matrix; |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 1323 | this->didSetMatrix(matrix); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1324 | } |
| 1325 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1326 | void SkCanvas::resetMatrix() { |
| 1327 | SkMatrix matrix; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1328 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1329 | matrix.reset(); |
| 1330 | this->setMatrix(matrix); |
| 1331 | } |
| 1332 | |
| 1333 | ////////////////////////////////////////////////////////////////////////////// |
| 1334 | |
commit-bot@chromium.org | 759cf48 | 2014-03-06 13:18:07 +0000 | [diff] [blame] | 1335 | void SkCanvas::clipRect(const SkRect& rect, SkRegion::Op op, bool doAA) { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1336 | ClipEdgeStyle edgeStyle = doAA ? kSoft_ClipEdgeStyle : kHard_ClipEdgeStyle; |
| 1337 | this->onClipRect(rect, op, edgeStyle); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | void SkCanvas::onClipRect(const SkRect& rect, SkRegion::Op op, ClipEdgeStyle edgeStyle) { |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1341 | #ifdef SK_ENABLE_CLIP_QUICKREJECT |
| 1342 | if (SkRegion::kIntersect_Op == op) { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1343 | if (fMCRec->fRasterClip.isEmpty()) { |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1344 | return false; |
| 1345 | } |
| 1346 | |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1347 | if (this->quickReject(rect)) { |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1348 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1349 | fCachedLocalClipBoundsDirty = true; |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1350 | |
| 1351 | fClipStack.clipEmpty(); |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1352 | return fMCRec->fRasterClip.setEmpty(); |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1353 | } |
| 1354 | } |
| 1355 | #endif |
| 1356 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1357 | AutoValidateClip avc(this); |
| 1358 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1359 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1360 | fCachedLocalClipBoundsDirty = true; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1361 | if (!fAllowSoftClip) { |
| 1362 | edgeStyle = kHard_ClipEdgeStyle; |
| 1363 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1364 | |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1365 | if (fMCRec->fMatrix.rectStaysRect()) { |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1366 | // for these simpler matrices, we can stay a rect even after applying |
reed@android.com | 98de2bd | 2009-03-02 19:41:36 +0000 | [diff] [blame] | 1367 | // the matrix. This means we don't have to a) make a path, and b) tell |
| 1368 | // the region code to scan-convert the path, only to discover that it |
| 1369 | // is really just a rect. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1370 | SkRect r; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1371 | |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1372 | fMCRec->fMatrix.mapRect(&r, rect); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1373 | fClipStack.clipDevRect(r, op, kSoft_ClipEdgeStyle == edgeStyle); |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 1374 | fMCRec->fRasterClip.op(r, this->getBaseLayerSize(), op, kSoft_ClipEdgeStyle == edgeStyle); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1375 | } else { |
robertphillips@google.com | 1236719 | 2013-10-20 13:11:16 +0000 | [diff] [blame] | 1376 | // since we're rotated or some such thing, we convert the rect to a path |
reed@android.com | 98de2bd | 2009-03-02 19:41:36 +0000 | [diff] [blame] | 1377 | // and clip against that, since it can handle any matrix. However, to |
| 1378 | // avoid recursion in the case where we are subclassed (e.g. Pictures) |
| 1379 | // we explicitly call "our" version of clipPath. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1380 | SkPath path; |
| 1381 | |
| 1382 | path.addRect(rect); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1383 | this->SkCanvas::onClipPath(path, op, edgeStyle); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1384 | } |
| 1385 | } |
| 1386 | |
reed | 73e714e | 2014-09-04 09:02:23 -0700 | [diff] [blame] | 1387 | static void rasterclip_path(SkRasterClip* rc, const SkCanvas* canvas, const SkPath& devPath, |
| 1388 | SkRegion::Op op, bool doAA) { |
reed | d64c948 | 2014-09-05 17:37:38 -0700 | [diff] [blame] | 1389 | rc->op(devPath, canvas->getBaseLayerSize(), op, doAA); |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
commit-bot@chromium.org | 759cf48 | 2014-03-06 13:18:07 +0000 | [diff] [blame] | 1392 | void SkCanvas::clipRRect(const SkRRect& rrect, SkRegion::Op op, bool doAA) { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1393 | ClipEdgeStyle edgeStyle = doAA ? kSoft_ClipEdgeStyle : kHard_ClipEdgeStyle; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1394 | if (rrect.isRect()) { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1395 | this->onClipRect(rrect.getBounds(), op, edgeStyle); |
| 1396 | } else { |
| 1397 | this->onClipRRect(rrect, op, edgeStyle); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1398 | } |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1399 | } |
commit-bot@chromium.org | 14e50ae | 2014-02-16 23:35:31 +0000 | [diff] [blame] | 1400 | |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1401 | void SkCanvas::onClipRRect(const SkRRect& rrect, SkRegion::Op op, ClipEdgeStyle edgeStyle) { |
commit-bot@chromium.org | 14e50ae | 2014-02-16 23:35:31 +0000 | [diff] [blame] | 1402 | SkRRect transformedRRect; |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1403 | if (rrect.transform(fMCRec->fMatrix, &transformedRRect)) { |
commit-bot@chromium.org | 14e50ae | 2014-02-16 23:35:31 +0000 | [diff] [blame] | 1404 | AutoValidateClip avc(this); |
| 1405 | |
| 1406 | fDeviceCMDirty = true; |
| 1407 | fCachedLocalClipBoundsDirty = true; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1408 | if (!fAllowSoftClip) { |
| 1409 | edgeStyle = kHard_ClipEdgeStyle; |
| 1410 | } |
commit-bot@chromium.org | 14e50ae | 2014-02-16 23:35:31 +0000 | [diff] [blame] | 1411 | |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1412 | fClipStack.clipDevRRect(transformedRRect, op, kSoft_ClipEdgeStyle == edgeStyle); |
commit-bot@chromium.org | 14e50ae | 2014-02-16 23:35:31 +0000 | [diff] [blame] | 1413 | |
| 1414 | SkPath devPath; |
| 1415 | devPath.addRRect(transformedRRect); |
| 1416 | |
reed | 73e714e | 2014-09-04 09:02:23 -0700 | [diff] [blame] | 1417 | rasterclip_path(&fMCRec->fRasterClip, this, devPath, op, kSoft_ClipEdgeStyle == edgeStyle); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1418 | return; |
commit-bot@chromium.org | 14e50ae | 2014-02-16 23:35:31 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
| 1421 | SkPath path; |
| 1422 | path.addRRect(rrect); |
| 1423 | // call the non-virtual version |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1424 | this->SkCanvas::onClipPath(path, op, edgeStyle); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
commit-bot@chromium.org | 759cf48 | 2014-03-06 13:18:07 +0000 | [diff] [blame] | 1427 | void SkCanvas::clipPath(const SkPath& path, SkRegion::Op op, bool doAA) { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1428 | ClipEdgeStyle edgeStyle = doAA ? kSoft_ClipEdgeStyle : kHard_ClipEdgeStyle; |
| 1429 | SkRect r; |
| 1430 | if (!path.isInverseFillType() && path.isRect(&r)) { |
| 1431 | this->onClipRect(r, op, edgeStyle); |
| 1432 | } else { |
| 1433 | this->onClipPath(path, op, edgeStyle); |
| 1434 | } |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | void SkCanvas::onClipPath(const SkPath& path, SkRegion::Op op, ClipEdgeStyle edgeStyle) { |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1438 | #ifdef SK_ENABLE_CLIP_QUICKREJECT |
| 1439 | if (SkRegion::kIntersect_Op == op && !path.isInverseFillType()) { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1440 | if (fMCRec->fRasterClip.isEmpty()) { |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1441 | return false; |
| 1442 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1443 | |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1444 | if (this->quickReject(path.getBounds())) { |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1445 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1446 | fCachedLocalClipBoundsDirty = true; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1447 | |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1448 | fClipStack.clipEmpty(); |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1449 | return fMCRec->fRasterClip.setEmpty(); |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 1450 | } |
| 1451 | } |
| 1452 | #endif |
| 1453 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1454 | AutoValidateClip avc(this); |
| 1455 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1456 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1457 | fCachedLocalClipBoundsDirty = true; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1458 | if (!fAllowSoftClip) { |
| 1459 | edgeStyle = kHard_ClipEdgeStyle; |
| 1460 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1461 | |
| 1462 | SkPath devPath; |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1463 | path.transform(fMCRec->fMatrix, &devPath); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1464 | |
reed@google.com | fe70112 | 2011-11-08 19:41:23 +0000 | [diff] [blame] | 1465 | // Check if the transfomation, or the original path itself |
| 1466 | // made us empty. Note this can also happen if we contained NaN |
| 1467 | // values. computing the bounds detects this, and will set our |
| 1468 | // bounds to empty if that is the case. (see SkRect::set(pts, count)) |
| 1469 | if (devPath.getBounds().isEmpty()) { |
| 1470 | // resetting the path will remove any NaN or other wanky values |
| 1471 | // that might upset our scan converter. |
| 1472 | devPath.reset(); |
| 1473 | } |
| 1474 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1475 | // if we called path.swap() we could avoid a deep copy of this path |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1476 | fClipStack.clipDevPath(devPath, op, kSoft_ClipEdgeStyle == edgeStyle); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1477 | |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 1478 | if (fAllowSimplifyClip) { |
| 1479 | devPath.reset(); |
| 1480 | devPath.setFillType(SkPath::kInverseEvenOdd_FillType); |
| 1481 | const SkClipStack* clipStack = getClipStack(); |
| 1482 | SkClipStack::Iter iter(*clipStack, SkClipStack::Iter::kBottom_IterStart); |
| 1483 | const SkClipStack::Element* element; |
| 1484 | while ((element = iter.next())) { |
| 1485 | SkClipStack::Element::Type type = element->getType(); |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 1486 | SkPath operand; |
commit-bot@chromium.org | 2a67e12 | 2014-05-19 13:53:10 +0000 | [diff] [blame] | 1487 | if (type != SkClipStack::Element::kEmpty_Type) { |
| 1488 | element->asPath(&operand); |
| 1489 | } |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 1490 | SkRegion::Op elementOp = element->getOp(); |
| 1491 | if (elementOp == SkRegion::kReplace_Op) { |
| 1492 | devPath = operand; |
| 1493 | } else { |
| 1494 | Op(devPath, operand, (SkPathOp) elementOp, &devPath); |
| 1495 | } |
caryclark@google.com | 96fd344 | 2013-05-07 19:48:31 +0000 | [diff] [blame] | 1496 | // if the prev and curr clips disagree about aa -vs- not, favor the aa request. |
| 1497 | // perhaps we need an API change to avoid this sort of mixed-signals about |
| 1498 | // clipping. |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1499 | if (element->isAA()) { |
| 1500 | edgeStyle = kSoft_ClipEdgeStyle; |
| 1501 | } |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 1502 | } |
| 1503 | op = SkRegion::kReplace_Op; |
| 1504 | } |
| 1505 | |
reed | 73e714e | 2014-09-04 09:02:23 -0700 | [diff] [blame] | 1506 | rasterclip_path(&fMCRec->fRasterClip, this, devPath, op, edgeStyle); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
commit-bot@chromium.org | 759cf48 | 2014-03-06 13:18:07 +0000 | [diff] [blame] | 1509 | void SkCanvas::clipRegion(const SkRegion& rgn, SkRegion::Op op) { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1510 | this->onClipRegion(rgn, op); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | void SkCanvas::onClipRegion(const SkRegion& rgn, SkRegion::Op op) { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1514 | AutoValidateClip avc(this); |
| 1515 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1516 | fDeviceCMDirty = true; |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1517 | fCachedLocalClipBoundsDirty = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1518 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1519 | // todo: signal fClipStack that we have a region, and therefore (I guess) |
| 1520 | // we have to ignore it, and use the region directly? |
reed@google.com | 115d931 | 2012-05-16 18:50:40 +0000 | [diff] [blame] | 1521 | fClipStack.clipDevRect(rgn.getBounds(), op); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1522 | |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1523 | fMCRec->fRasterClip.op(rgn, op); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1526 | #ifdef SK_DEBUG |
| 1527 | void SkCanvas::validateClip() const { |
| 1528 | // construct clipRgn from the clipstack |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1529 | const SkBaseDevice* device = this->getDevice(); |
djsollen@google.com | ccfee2a | 2012-05-01 16:50:10 +0000 | [diff] [blame] | 1530 | if (!device) { |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1531 | SkASSERT(this->isClipEmpty()); |
djsollen@google.com | ccfee2a | 2012-05-01 16:50:10 +0000 | [diff] [blame] | 1532 | return; |
| 1533 | } |
| 1534 | |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1535 | SkIRect ir; |
| 1536 | ir.set(0, 0, device->width(), device->height()); |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 1537 | SkRasterClip tmpClip(ir, fConservativeRasterClip); |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1538 | |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 1539 | SkClipStack::B2TIter iter(fClipStack); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1540 | const SkClipStack::Element* element; |
| 1541 | while ((element = iter.next()) != NULL) { |
| 1542 | switch (element->getType()) { |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1543 | case SkClipStack::Element::kRect_Type: |
| 1544 | element->getRect().round(&ir); |
| 1545 | tmpClip.op(ir, element->getOp()); |
| 1546 | break; |
| 1547 | case SkClipStack::Element::kEmpty_Type: |
| 1548 | tmpClip.setEmpty(); |
| 1549 | break; |
commit-bot@chromium.org | 9cb671a | 2014-02-16 14:45:45 +0000 | [diff] [blame] | 1550 | default: { |
| 1551 | SkPath path; |
| 1552 | element->asPath(&path); |
reed | 73e714e | 2014-09-04 09:02:23 -0700 | [diff] [blame] | 1553 | rasterclip_path(&tmpClip, this, path, element->getOp(), element->isAA()); |
commit-bot@chromium.org | 9cb671a | 2014-02-16 14:45:45 +0000 | [diff] [blame] | 1554 | break; |
| 1555 | } |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1556 | } |
| 1557 | } |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1558 | } |
| 1559 | #endif |
| 1560 | |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 1561 | void SkCanvas::replayClips(ClipVisitor* visitor) const { |
robertphillips@google.com | 80214e2 | 2012-07-20 15:33:18 +0000 | [diff] [blame] | 1562 | SkClipStack::B2TIter iter(fClipStack); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1563 | const SkClipStack::Element* element; |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 1564 | |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1565 | while ((element = iter.next()) != NULL) { |
fmalita | c3b589a | 2014-06-05 12:40:07 -0700 | [diff] [blame] | 1566 | element->replay(visitor); |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 1567 | } |
| 1568 | } |
| 1569 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1570 | /////////////////////////////////////////////////////////////////////////////// |
| 1571 | |
reed@google.com | 754de5f | 2014-02-24 19:38:20 +0000 | [diff] [blame] | 1572 | bool SkCanvas::isClipEmpty() const { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1573 | return fMCRec->fRasterClip.isEmpty(); |
reed@google.com | 754de5f | 2014-02-24 19:38:20 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1576 | bool SkCanvas::isClipRect() const { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1577 | return fMCRec->fRasterClip.isRect(); |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1580 | bool SkCanvas::quickReject(const SkRect& rect) const { |
reed@google.com | 1607863 | 2011-12-06 18:56:37 +0000 | [diff] [blame] | 1581 | if (!rect.isFinite()) |
wjmaclean@chromium.org | 116b2bc | 2011-02-07 17:48:37 +0000 | [diff] [blame] | 1582 | return true; |
| 1583 | |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1584 | if (fMCRec->fRasterClip.isEmpty()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1585 | return true; |
| 1586 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1587 | |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1588 | if (fMCRec->fMatrix.hasPerspective()) { |
reed@android.com | a380ae4 | 2009-07-21 01:17:02 +0000 | [diff] [blame] | 1589 | SkRect dst; |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1590 | fMCRec->fMatrix.mapRect(&dst, rect); |
reed@android.com | a380ae4 | 2009-07-21 01:17:02 +0000 | [diff] [blame] | 1591 | SkIRect idst; |
| 1592 | dst.roundOut(&idst); |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1593 | return !SkIRect::Intersects(idst, fMCRec->fRasterClip.getBounds()); |
reed@android.com | a380ae4 | 2009-07-21 01:17:02 +0000 | [diff] [blame] | 1594 | } else { |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1595 | const SkRect& clipR = this->getLocalClipBounds(); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 1596 | |
reed@android.com | a380ae4 | 2009-07-21 01:17:02 +0000 | [diff] [blame] | 1597 | // for speed, do the most likely reject compares first |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1598 | // TODO: should we use | instead, or compare all 4 at once? |
| 1599 | if (rect.fTop >= clipR.fBottom || rect.fBottom <= clipR.fTop) { |
reed@android.com | a380ae4 | 2009-07-21 01:17:02 +0000 | [diff] [blame] | 1600 | return true; |
| 1601 | } |
reed@google.com | c0784db | 2013-12-13 21:16:12 +0000 | [diff] [blame] | 1602 | if (rect.fLeft >= clipR.fRight || rect.fRight <= clipR.fLeft) { |
reed@android.com | a380ae4 | 2009-07-21 01:17:02 +0000 | [diff] [blame] | 1603 | return true; |
| 1604 | } |
| 1605 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1606 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1609 | bool SkCanvas::quickReject(const SkPath& path) const { |
| 1610 | return path.isEmpty() || this->quickReject(path.getBounds()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1611 | } |
| 1612 | |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1613 | bool SkCanvas::getClipBounds(SkRect* bounds) const { |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1614 | SkIRect ibounds; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1615 | if (!this->getClipDeviceBounds(&ibounds)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1616 | return false; |
| 1617 | } |
| 1618 | |
reed@android.com | d9c0f0b | 2009-02-06 22:39:37 +0000 | [diff] [blame] | 1619 | SkMatrix inverse; |
| 1620 | // if we can't invert the CTM, we can't return local clip bounds |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1621 | if (!fMCRec->fMatrix.invert(&inverse)) { |
reed@android.com | 72dcd3a | 2009-05-18 15:46:29 +0000 | [diff] [blame] | 1622 | if (bounds) { |
| 1623 | bounds->setEmpty(); |
| 1624 | } |
reed@android.com | d9c0f0b | 2009-02-06 22:39:37 +0000 | [diff] [blame] | 1625 | return false; |
| 1626 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1627 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1628 | if (bounds) { |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1629 | SkRect r; |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1630 | // adjust it outwards in case we are antialiasing |
| 1631 | const int inset = 1; |
reed@google.com | fa4d5bd | 2012-01-30 17:36:27 +0000 | [diff] [blame] | 1632 | |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 1633 | r.iset(ibounds.fLeft - inset, ibounds.fTop - inset, |
| 1634 | ibounds.fRight + inset, ibounds.fBottom + inset); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1635 | inverse.mapRect(bounds, r); |
| 1636 | } |
| 1637 | return true; |
| 1638 | } |
| 1639 | |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1640 | bool SkCanvas::getClipDeviceBounds(SkIRect* bounds) const { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1641 | const SkRasterClip& clip = fMCRec->fRasterClip; |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1642 | if (clip.isEmpty()) { |
| 1643 | if (bounds) { |
| 1644 | bounds->setEmpty(); |
| 1645 | } |
| 1646 | return false; |
| 1647 | } |
| 1648 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1649 | if (bounds) { |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1650 | *bounds = clip.getBounds(); |
| 1651 | } |
| 1652 | return true; |
| 1653 | } |
| 1654 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1655 | const SkMatrix& SkCanvas::getTotalMatrix() const { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1656 | return fMCRec->fMatrix; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1659 | const SkRegion& SkCanvas::internal_private_getTotalClip() const { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1660 | return fMCRec->fRasterClip.forceGetBW(); |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1663 | GrRenderTarget* SkCanvas::internal_private_accessTopLayerRenderTarget() { |
| 1664 | SkBaseDevice* dev = this->getTopDevice(); |
| 1665 | return dev ? dev->accessRenderTarget() : NULL; |
| 1666 | } |
| 1667 | |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 1668 | SkBaseDevice* SkCanvas::createLayerDevice(const SkImageInfo& info) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1669 | SkBaseDevice* device = this->getTopDevice(); |
commit-bot@chromium.org | 15a1405 | 2014-02-16 00:59:25 +0000 | [diff] [blame] | 1670 | return device ? device->createCompatibleDeviceForSaveLayer(info) : NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1671 | } |
| 1672 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 1673 | GrContext* SkCanvas::getGrContext() { |
| 1674 | #if SK_SUPPORT_GPU |
| 1675 | SkBaseDevice* device = this->getTopDevice(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1676 | if (device) { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 1677 | GrRenderTarget* renderTarget = device->accessRenderTarget(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1678 | if (renderTarget) { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 1679 | return renderTarget->getContext(); |
| 1680 | } |
| 1681 | } |
| 1682 | #endif |
| 1683 | |
| 1684 | return NULL; |
| 1685 | |
| 1686 | } |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1687 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1688 | void SkCanvas::drawDRRect(const SkRRect& outer, const SkRRect& inner, |
| 1689 | const SkPaint& paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 1690 | TRACE_EVENT0("skia", "SkCanvas::drawDRRect()"); |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1691 | if (outer.isEmpty()) { |
| 1692 | return; |
| 1693 | } |
| 1694 | if (inner.isEmpty()) { |
| 1695 | this->drawRRect(outer, paint); |
| 1696 | return; |
| 1697 | } |
| 1698 | |
| 1699 | // We don't have this method (yet), but technically this is what we should |
| 1700 | // be able to assert... |
| 1701 | // SkASSERT(outer.contains(inner)); |
| 1702 | // |
| 1703 | // For now at least check for containment of bounds |
| 1704 | SkASSERT(outer.getBounds().contains(inner.getBounds())); |
| 1705 | |
| 1706 | this->onDrawDRRect(outer, inner, paint); |
| 1707 | } |
| 1708 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1709 | ////////////////////////////////////////////////////////////////////////////// |
| 1710 | // These are the virtual drawing methods |
| 1711 | ////////////////////////////////////////////////////////////////////////////// |
| 1712 | |
reed@google.com | 2a98181 | 2011-04-14 18:59:28 +0000 | [diff] [blame] | 1713 | void SkCanvas::clear(SkColor color) { |
| 1714 | SkDrawIter iter(this); |
junov@chromium.org | 995beb6 | 2013-03-28 13:49:22 +0000 | [diff] [blame] | 1715 | this->predrawNotify(); |
reed@google.com | 2a98181 | 2011-04-14 18:59:28 +0000 | [diff] [blame] | 1716 | while (iter.next()) { |
| 1717 | iter.fDevice->clear(color); |
| 1718 | } |
| 1719 | } |
| 1720 | |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 1721 | void SkCanvas::onDiscard() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1722 | if (fSurfaceBase) { |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 1723 | fSurfaceBase->aboutToDraw(SkSurface::kDiscard_ContentChangeMode); |
| 1724 | } |
| 1725 | } |
| 1726 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1727 | void SkCanvas::drawPaint(const SkPaint& paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 1728 | TRACE_EVENT0("skia", "SkCanvas::drawPaint()"); |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 1729 | this->internalDrawPaint(paint); |
| 1730 | } |
| 1731 | |
| 1732 | void SkCanvas::internalDrawPaint(const SkPaint& paint) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1733 | LOOPER_BEGIN(paint, SkDrawFilter::kPaint_Type, NULL) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1734 | |
| 1735 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1736 | iter.fDevice->drawPaint(iter, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1739 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | void SkCanvas::drawPoints(PointMode mode, size_t count, const SkPoint pts[], |
| 1743 | const SkPaint& paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 1744 | TRACE_EVENT1("skia", "SkCanvas::drawPoints()", "count", static_cast<uint64_t>(count)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1745 | if ((long)count <= 0) { |
| 1746 | return; |
| 1747 | } |
| 1748 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1749 | SkRect r, storage; |
| 1750 | const SkRect* bounds = NULL; |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 1751 | if (paint.canComputeFastBounds()) { |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 1752 | // special-case 2 points (common for drawing a single line) |
| 1753 | if (2 == count) { |
| 1754 | r.set(pts[0], pts[1]); |
| 1755 | } else { |
commit-bot@chromium.org | a8c7f77 | 2014-01-24 21:46:29 +0000 | [diff] [blame] | 1756 | r.set(pts, SkToInt(count)); |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 1757 | } |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1758 | bounds = &paint.computeFastStrokeBounds(r, &storage); |
| 1759 | if (this->quickReject(*bounds)) { |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 1760 | return; |
| 1761 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1762 | } |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 1763 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1764 | SkASSERT(pts != NULL); |
| 1765 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1766 | LOOPER_BEGIN(paint, SkDrawFilter::kPoint_Type, bounds) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1767 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1768 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1769 | iter.fDevice->drawPoints(iter, mode, count, pts, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1770 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1771 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1772 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1773 | } |
| 1774 | |
bsalomon@google.com | 7ce564c | 2013-10-22 16:54:15 +0000 | [diff] [blame] | 1775 | void SkCanvas::drawRect(const SkRect& r, const SkPaint& paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 1776 | TRACE_EVENT0("skia", "SkCanvas::drawRect()"); |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1777 | SkRect storage; |
| 1778 | const SkRect* bounds = NULL; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1779 | if (paint.canComputeFastBounds()) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1780 | bounds = &paint.computeFastBounds(r, &storage); |
| 1781 | if (this->quickReject(*bounds)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1782 | return; |
| 1783 | } |
| 1784 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1785 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1786 | LOOPER_BEGIN(paint, SkDrawFilter::kRect_Type, bounds) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1787 | |
| 1788 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1789 | iter.fDevice->drawRect(iter, r, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1792 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1795 | void SkCanvas::drawOval(const SkRect& oval, const SkPaint& paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 1796 | TRACE_EVENT0("skia", "SkCanvas::drawOval()"); |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1797 | SkRect storage; |
| 1798 | const SkRect* bounds = NULL; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1799 | if (paint.canComputeFastBounds()) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1800 | bounds = &paint.computeFastBounds(oval, &storage); |
| 1801 | if (this->quickReject(*bounds)) { |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1802 | return; |
| 1803 | } |
| 1804 | } |
skia.committer@gmail.com | 306ab9d | 2012-12-13 02:01:33 +0000 | [diff] [blame] | 1805 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1806 | LOOPER_BEGIN(paint, SkDrawFilter::kOval_Type, bounds) |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 1807 | |
| 1808 | while (iter.next()) { |
| 1809 | iter.fDevice->drawOval(iter, oval, looper.paint()); |
| 1810 | } |
| 1811 | |
| 1812 | LOOPER_END |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | void SkCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 1816 | TRACE_EVENT0("skia", "SkCanvas::drawRRect()"); |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1817 | SkRect storage; |
| 1818 | const SkRect* bounds = NULL; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1819 | if (paint.canComputeFastBounds()) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1820 | bounds = &paint.computeFastBounds(rrect.getBounds(), &storage); |
| 1821 | if (this->quickReject(*bounds)) { |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1822 | return; |
| 1823 | } |
| 1824 | } |
| 1825 | |
| 1826 | if (rrect.isRect()) { |
| 1827 | // call the non-virtual version |
| 1828 | this->SkCanvas::drawRect(rrect.getBounds(), paint); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1829 | return; |
| 1830 | } else if (rrect.isOval()) { |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1831 | // call the non-virtual version |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1832 | this->SkCanvas::drawOval(rrect.getBounds(), paint); |
| 1833 | return; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1834 | } |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1835 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1836 | LOOPER_BEGIN(paint, SkDrawFilter::kRRect_Type, bounds) |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 1837 | |
| 1838 | while (iter.next()) { |
| 1839 | iter.fDevice->drawRRect(iter, rrect, looper.paint()); |
| 1840 | } |
| 1841 | |
| 1842 | LOOPER_END |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1843 | } |
| 1844 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1845 | void SkCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, |
| 1846 | const SkPaint& paint) { |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1847 | SkRect storage; |
| 1848 | const SkRect* bounds = NULL; |
| 1849 | if (paint.canComputeFastBounds()) { |
| 1850 | bounds = &paint.computeFastBounds(outer.getBounds(), &storage); |
| 1851 | if (this->quickReject(*bounds)) { |
| 1852 | return; |
| 1853 | } |
| 1854 | } |
skia.committer@gmail.com | 25c7127 | 2014-02-21 03:02:02 +0000 | [diff] [blame] | 1855 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1856 | LOOPER_BEGIN(paint, SkDrawFilter::kRRect_Type, bounds) |
skia.committer@gmail.com | 25c7127 | 2014-02-21 03:02:02 +0000 | [diff] [blame] | 1857 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1858 | while (iter.next()) { |
| 1859 | iter.fDevice->drawDRRect(iter, outer, inner, looper.paint()); |
| 1860 | } |
skia.committer@gmail.com | 25c7127 | 2014-02-21 03:02:02 +0000 | [diff] [blame] | 1861 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1862 | LOOPER_END |
| 1863 | } |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1864 | |
bsalomon@google.com | 7ce564c | 2013-10-22 16:54:15 +0000 | [diff] [blame] | 1865 | void SkCanvas::drawPath(const SkPath& path, const SkPaint& paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 1866 | TRACE_EVENT0("skia", "SkCanvas::drawPath()"); |
reed@google.com | 9364511 | 2012-07-26 16:11:47 +0000 | [diff] [blame] | 1867 | if (!path.isFinite()) { |
| 1868 | return; |
| 1869 | } |
| 1870 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1871 | SkRect storage; |
| 1872 | const SkRect* bounds = NULL; |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 1873 | if (!path.isInverseFillType() && paint.canComputeFastBounds()) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1874 | const SkRect& pathBounds = path.getBounds(); |
| 1875 | bounds = &paint.computeFastBounds(pathBounds, &storage); |
| 1876 | if (this->quickReject(*bounds)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1877 | return; |
| 1878 | } |
| 1879 | } |
commit-bot@chromium.org | 0b45dc4 | 2014-02-21 05:42:57 +0000 | [diff] [blame] | 1880 | |
| 1881 | const SkRect& r = path.getBounds(); |
| 1882 | if (r.width() <= 0 && r.height() <= 0) { |
commit-bot@chromium.org | 6803c21 | 2014-05-04 18:08:27 +0000 | [diff] [blame] | 1883 | if (path.isInverseFillType()) { |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 1884 | this->internalDrawPaint(paint); |
| 1885 | } |
| 1886 | return; |
| 1887 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1888 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1889 | LOOPER_BEGIN(paint, SkDrawFilter::kPath_Type, bounds) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1890 | |
| 1891 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1892 | iter.fDevice->drawPath(iter, path, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1893 | } |
| 1894 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1895 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 1898 | void SkCanvas::drawImage(const SkImage* image, SkScalar left, SkScalar top, |
| 1899 | const SkPaint* paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 1900 | TRACE_EVENT0("skia", "SkCanvas::drawImage()"); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 1901 | image->draw(this, left, top, paint); |
| 1902 | } |
| 1903 | |
| 1904 | void SkCanvas::drawImageRect(const SkImage* image, const SkRect* src, |
| 1905 | const SkRect& dst, |
| 1906 | const SkPaint* paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 1907 | TRACE_EVENT0("skia", "SkCanvas::drawImageRect()"); |
piotaixr | 5ceff91 | 2014-09-26 07:36:26 -0700 | [diff] [blame] | 1908 | image->drawRect(this, src, dst, paint); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 1909 | } |
| 1910 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1911 | void SkCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y, |
| 1912 | const SkPaint* paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 1913 | TRACE_EVENT0("skia", "SkCanvas::drawBitmap()"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1914 | SkDEBUGCODE(bitmap.validate();) |
| 1915 | |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 1916 | if (NULL == paint || paint->canComputeFastBounds()) { |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 1917 | SkRect bounds = { |
| 1918 | x, y, |
| 1919 | x + SkIntToScalar(bitmap.width()), |
| 1920 | y + SkIntToScalar(bitmap.height()) |
| 1921 | }; |
| 1922 | if (paint) { |
| 1923 | (void)paint->computeFastBounds(bounds, &bounds); |
| 1924 | } |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1925 | if (this->quickReject(bounds)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1926 | return; |
| 1927 | } |
| 1928 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1929 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1930 | SkMatrix matrix; |
| 1931 | matrix.setTranslate(x, y); |
robertphillips@google.com | 9bf380c | 2013-07-25 12:10:42 +0000 | [diff] [blame] | 1932 | this->internalDrawBitmap(bitmap, matrix, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1933 | } |
| 1934 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 1935 | // this one is non-virtual, so it can be called safely by other canvas apis |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 1936 | void SkCanvas::internalDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 1937 | const SkRect& dst, const SkPaint* paint, |
| 1938 | DrawBitmapRectFlags flags) { |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1939 | if (bitmap.drawsNothing() || dst.isEmpty()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1940 | return; |
| 1941 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 1942 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1943 | SkRect storage; |
| 1944 | const SkRect* bounds = &dst; |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 1945 | if (NULL == paint || paint->canComputeFastBounds()) { |
reed@google.com | 9efd9a0 | 2012-01-30 15:41:43 +0000 | [diff] [blame] | 1946 | if (paint) { |
| 1947 | bounds = &paint->computeFastBounds(dst, &storage); |
| 1948 | } |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1949 | if (this->quickReject(*bounds)) { |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 1950 | return; |
| 1951 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1952 | } |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 1953 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 1954 | SkLazyPaint lazy; |
| 1955 | if (NULL == paint) { |
| 1956 | paint = lazy.init(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1957 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 1958 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 1959 | LOOPER_BEGIN(*paint, SkDrawFilter::kBitmap_Type, bounds) |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 1960 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 1961 | while (iter.next()) { |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 1962 | iter.fDevice->drawBitmapRect(iter, bitmap, src, dst, looper.paint(), flags); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1963 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 1964 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 1965 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 1968 | void SkCanvas::drawBitmapRectToRect(const SkBitmap& bitmap, const SkRect* src, |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 1969 | const SkRect& dst, const SkPaint* paint, |
| 1970 | DrawBitmapRectFlags flags) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 1971 | TRACE_EVENT0("skia", "SkCanvas::drawBitmapRectToRect()"); |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 1972 | SkDEBUGCODE(bitmap.validate();) |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 1973 | this->internalDrawBitmapRect(bitmap, src, dst, paint, flags); |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 1974 | } |
| 1975 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1976 | void SkCanvas::drawBitmapMatrix(const SkBitmap& bitmap, const SkMatrix& matrix, |
| 1977 | const SkPaint* paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 1978 | TRACE_EVENT0("skia", "SkCanvas::drawBitmapMatrix()"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1979 | SkDEBUGCODE(bitmap.validate();) |
robertphillips@google.com | 9bf380c | 2013-07-25 12:10:42 +0000 | [diff] [blame] | 1980 | this->internalDrawBitmap(bitmap, matrix, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 1983 | void SkCanvas::internalDrawBitmapNine(const SkBitmap& bitmap, |
| 1984 | const SkIRect& center, const SkRect& dst, |
| 1985 | const SkPaint* paint) { |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 1986 | if (bitmap.drawsNothing()) { |
| 1987 | return; |
| 1988 | } |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 1989 | if (NULL == paint || paint->canComputeFastBounds()) { |
djsollen@google.com | 60abb07 | 2012-02-15 18:49:15 +0000 | [diff] [blame] | 1990 | SkRect storage; |
| 1991 | const SkRect* bounds = &dst; |
| 1992 | if (paint) { |
| 1993 | bounds = &paint->computeFastBounds(dst, &storage); |
| 1994 | } |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1995 | if (this->quickReject(*bounds)) { |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 1996 | return; |
| 1997 | } |
| 1998 | } |
| 1999 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2000 | const int32_t w = bitmap.width(); |
| 2001 | const int32_t h = bitmap.height(); |
| 2002 | |
| 2003 | SkIRect c = center; |
| 2004 | // pin center to the bounds of the bitmap |
| 2005 | c.fLeft = SkMax32(0, center.fLeft); |
| 2006 | c.fTop = SkMax32(0, center.fTop); |
| 2007 | c.fRight = SkPin32(center.fRight, c.fLeft, w); |
| 2008 | c.fBottom = SkPin32(center.fBottom, c.fTop, h); |
| 2009 | |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 2010 | const SkScalar srcX[4] = { |
rmistry@google.com | 7d474f8 | 2013-01-02 22:03:54 +0000 | [diff] [blame] | 2011 | 0, SkIntToScalar(c.fLeft), SkIntToScalar(c.fRight), SkIntToScalar(w) |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 2012 | }; |
| 2013 | const SkScalar srcY[4] = { |
rmistry@google.com | 7d474f8 | 2013-01-02 22:03:54 +0000 | [diff] [blame] | 2014 | 0, SkIntToScalar(c.fTop), SkIntToScalar(c.fBottom), SkIntToScalar(h) |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 2015 | }; |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2016 | SkScalar dstX[4] = { |
| 2017 | dst.fLeft, dst.fLeft + SkIntToScalar(c.fLeft), |
| 2018 | dst.fRight - SkIntToScalar(w - c.fRight), dst.fRight |
| 2019 | }; |
| 2020 | SkScalar dstY[4] = { |
| 2021 | dst.fTop, dst.fTop + SkIntToScalar(c.fTop), |
| 2022 | dst.fBottom - SkIntToScalar(h - c.fBottom), dst.fBottom |
| 2023 | }; |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 2024 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2025 | if (dstX[1] > dstX[2]) { |
| 2026 | dstX[1] = dstX[0] + (dstX[3] - dstX[0]) * c.fLeft / (w - c.width()); |
| 2027 | dstX[2] = dstX[1]; |
| 2028 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 2029 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2030 | if (dstY[1] > dstY[2]) { |
| 2031 | dstY[1] = dstY[0] + (dstY[3] - dstY[0]) * c.fTop / (h - c.height()); |
| 2032 | dstY[2] = dstY[1]; |
| 2033 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 2034 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2035 | for (int y = 0; y < 3; y++) { |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 2036 | SkRect s, d; |
| 2037 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2038 | s.fTop = srcY[y]; |
| 2039 | s.fBottom = srcY[y+1]; |
| 2040 | d.fTop = dstY[y]; |
| 2041 | d.fBottom = dstY[y+1]; |
| 2042 | for (int x = 0; x < 3; x++) { |
| 2043 | s.fLeft = srcX[x]; |
| 2044 | s.fRight = srcX[x+1]; |
| 2045 | d.fLeft = dstX[x]; |
| 2046 | d.fRight = dstX[x+1]; |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 2047 | this->internalDrawBitmapRect(bitmap, &s, d, paint, |
robertphillips@google.com | 31acc11 | 2013-08-20 12:13:48 +0000 | [diff] [blame] | 2048 | kNone_DrawBitmapRectFlag); |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2049 | } |
| 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | void SkCanvas::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, |
| 2054 | const SkRect& dst, const SkPaint* paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 2055 | TRACE_EVENT0("skia", "SkCanvas::drawBitmapNine()"); |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2056 | SkDEBUGCODE(bitmap.validate();) |
| 2057 | |
| 2058 | // Need a device entry-point, so gpu can use a mesh |
| 2059 | this->internalDrawBitmapNine(bitmap, center, dst, paint); |
| 2060 | } |
| 2061 | |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2062 | class SkDeviceFilteredPaint { |
| 2063 | public: |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 2064 | SkDeviceFilteredPaint(SkBaseDevice* device, const SkPaint& paint) { |
| 2065 | SkBaseDevice::TextFlags flags; |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2066 | if (device->filterTextFlags(paint, &flags)) { |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 2067 | SkPaint* newPaint = fLazy.set(paint); |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2068 | newPaint->setFlags(flags.fFlags); |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2069 | fPaint = newPaint; |
| 2070 | } else { |
| 2071 | fPaint = &paint; |
| 2072 | } |
| 2073 | } |
| 2074 | |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2075 | const SkPaint& paint() const { return *fPaint; } |
| 2076 | |
| 2077 | private: |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 2078 | const SkPaint* fPaint; |
| 2079 | SkLazyPaint fLazy; |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2080 | }; |
| 2081 | |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 2082 | void SkCanvas::DrawRect(const SkDraw& draw, const SkPaint& paint, |
| 2083 | const SkRect& r, SkScalar textSize) { |
epoger@google.com | 17b7894 | 2011-08-26 14:40:38 +0000 | [diff] [blame] | 2084 | if (paint.getStyle() == SkPaint::kFill_Style) { |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 2085 | draw.fDevice->drawRect(draw, r, paint); |
| 2086 | } else { |
| 2087 | SkPaint p(paint); |
epoger@google.com | 17b7894 | 2011-08-26 14:40:38 +0000 | [diff] [blame] | 2088 | p.setStrokeWidth(SkScalarMul(textSize, paint.getStrokeWidth())); |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 2089 | draw.fDevice->drawRect(draw, r, p); |
| 2090 | } |
| 2091 | } |
| 2092 | |
| 2093 | void SkCanvas::DrawTextDecorations(const SkDraw& draw, const SkPaint& paint, |
| 2094 | const char text[], size_t byteLength, |
| 2095 | SkScalar x, SkScalar y) { |
| 2096 | SkASSERT(byteLength == 0 || text != NULL); |
| 2097 | |
| 2098 | // nothing to draw |
| 2099 | if (text == NULL || byteLength == 0 || |
| 2100 | draw.fClip->isEmpty() || |
| 2101 | (paint.getAlpha() == 0 && paint.getXfermode() == NULL)) { |
| 2102 | return; |
| 2103 | } |
| 2104 | |
| 2105 | SkScalar width = 0; |
| 2106 | SkPoint start; |
| 2107 | |
| 2108 | start.set(0, 0); // to avoid warning |
| 2109 | if (paint.getFlags() & (SkPaint::kUnderlineText_Flag | |
| 2110 | SkPaint::kStrikeThruText_Flag)) { |
| 2111 | width = paint.measureText(text, byteLength); |
| 2112 | |
| 2113 | SkScalar offsetX = 0; |
| 2114 | if (paint.getTextAlign() == SkPaint::kCenter_Align) { |
| 2115 | offsetX = SkScalarHalf(width); |
| 2116 | } else if (paint.getTextAlign() == SkPaint::kRight_Align) { |
| 2117 | offsetX = width; |
| 2118 | } |
| 2119 | start.set(x - offsetX, y); |
| 2120 | } |
| 2121 | |
| 2122 | if (0 == width) { |
| 2123 | return; |
| 2124 | } |
| 2125 | |
| 2126 | uint32_t flags = paint.getFlags(); |
| 2127 | |
| 2128 | if (flags & (SkPaint::kUnderlineText_Flag | |
| 2129 | SkPaint::kStrikeThruText_Flag)) { |
| 2130 | SkScalar textSize = paint.getTextSize(); |
| 2131 | SkScalar height = SkScalarMul(textSize, kStdUnderline_Thickness); |
| 2132 | SkRect r; |
| 2133 | |
| 2134 | r.fLeft = start.fX; |
| 2135 | r.fRight = start.fX + width; |
| 2136 | |
| 2137 | if (flags & SkPaint::kUnderlineText_Flag) { |
| 2138 | SkScalar offset = SkScalarMulAdd(textSize, kStdUnderline_Offset, |
| 2139 | start.fY); |
| 2140 | r.fTop = offset; |
| 2141 | r.fBottom = offset + height; |
| 2142 | DrawRect(draw, paint, r, textSize); |
| 2143 | } |
| 2144 | if (flags & SkPaint::kStrikeThruText_Flag) { |
| 2145 | SkScalar offset = SkScalarMulAdd(textSize, kStdStrikeThru_Offset, |
| 2146 | start.fY); |
| 2147 | r.fTop = offset; |
| 2148 | r.fBottom = offset + height; |
| 2149 | DrawRect(draw, paint, r, textSize); |
| 2150 | } |
| 2151 | } |
| 2152 | } |
| 2153 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2154 | void SkCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, |
| 2155 | const SkPaint& paint) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2156 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2157 | |
| 2158 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2159 | SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint()); |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2160 | iter.fDevice->drawText(iter, text, byteLength, x, y, dfp.paint()); |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 2161 | DrawTextDecorations(iter, dfp.paint(), |
| 2162 | static_cast<const char*>(text), byteLength, x, y); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2163 | } |
| 2164 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2165 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2168 | void SkCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[], |
| 2169 | const SkPaint& paint) { |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 2170 | SkPoint textOffset = SkPoint::Make(0, 0); |
| 2171 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2172 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 2173 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2174 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2175 | SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint()); |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 2176 | iter.fDevice->drawPosText(iter, text, byteLength, &pos->fX, 2, textOffset, |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2177 | dfp.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2178 | } |
skia.committer@gmail.com | b0430d0 | 2014-04-24 03:05:07 +0000 | [diff] [blame] | 2179 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2180 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2181 | } |
| 2182 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2183 | void SkCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], |
| 2184 | SkScalar constY, const SkPaint& paint) { |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 2185 | |
| 2186 | SkPoint textOffset = SkPoint::Make(0, constY); |
| 2187 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2188 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 2189 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2190 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2191 | SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint()); |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 2192 | iter.fDevice->drawPosText(iter, text, byteLength, xpos, 1, textOffset, |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2193 | dfp.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2194 | } |
skia.committer@gmail.com | b0430d0 | 2014-04-24 03:05:07 +0000 | [diff] [blame] | 2195 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2196 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2199 | void SkCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, |
| 2200 | const SkMatrix* matrix, const SkPaint& paint) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2201 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 2202 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2203 | while (iter.next()) { |
| 2204 | iter.fDevice->drawTextOnPath(iter, text, byteLength, path, |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2205 | matrix, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2206 | } |
skia.committer@gmail.com | b0430d0 | 2014-04-24 03:05:07 +0000 | [diff] [blame] | 2207 | |
commit-bot@chromium.org | 945ec3a | 2014-04-22 20:07:30 +0000 | [diff] [blame] | 2208 | LOOPER_END |
commit-bot@chromium.org | 4325d11 | 2014-04-22 19:03:02 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2211 | void SkCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 2212 | const SkPaint& paint) { |
fmalita | 7ba7aa7 | 2014-08-29 09:46:36 -0700 | [diff] [blame] | 2213 | |
fmalita | 19653d1 | 2014-10-16 11:53:30 -0700 | [diff] [blame] | 2214 | if (paint.canComputeFastBounds()) { |
fmalita | 7ba7aa7 | 2014-08-29 09:46:36 -0700 | [diff] [blame] | 2215 | SkRect storage; |
| 2216 | |
| 2217 | if (this->quickReject(paint.computeFastBounds(blob->bounds().makeOffset(x, y), &storage))) { |
| 2218 | return; |
| 2219 | } |
| 2220 | } |
| 2221 | |
fmalita | aa1b912 | 2014-08-28 14:32:24 -0700 | [diff] [blame] | 2222 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, NULL) |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2223 | |
fmalita | aa1b912 | 2014-08-28 14:32:24 -0700 | [diff] [blame] | 2224 | while (iter.next()) { |
| 2225 | SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint()); |
| 2226 | iter.fDevice->drawTextBlob(iter, blob, x, y, dfp.paint()); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2227 | } |
| 2228 | |
fmalita | aa1b912 | 2014-08-28 14:32:24 -0700 | [diff] [blame] | 2229 | LOOPER_END |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2230 | } |
| 2231 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2232 | // These will become non-virtual, so they always call the (virtual) onDraw... method |
| 2233 | void SkCanvas::drawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, |
| 2234 | const SkPaint& paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 2235 | TRACE_EVENT0("skia", "SkCanvas::drawText()"); |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2236 | this->onDrawText(text, byteLength, x, y, paint); |
| 2237 | } |
| 2238 | void SkCanvas::drawPosText(const void* text, size_t byteLength, const SkPoint pos[], |
| 2239 | const SkPaint& paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 2240 | TRACE_EVENT0("skia", "SkCanvas::drawPosText()"); |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2241 | this->onDrawPosText(text, byteLength, pos, paint); |
| 2242 | } |
| 2243 | void SkCanvas::drawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], |
| 2244 | SkScalar constY, const SkPaint& paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 2245 | TRACE_EVENT0("skia", "SkCanvas::drawPosTextH()"); |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2246 | this->onDrawPosTextH(text, byteLength, xpos, constY, paint); |
| 2247 | } |
| 2248 | void SkCanvas::drawTextOnPath(const void* text, size_t byteLength, const SkPath& path, |
| 2249 | const SkMatrix* matrix, const SkPaint& paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 2250 | TRACE_EVENT0("skia", "SkCanvas::drawTextOnPath()"); |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2251 | this->onDrawTextOnPath(text, byteLength, path, matrix, paint); |
| 2252 | } |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2253 | void SkCanvas::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 2254 | const SkPaint& paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 2255 | TRACE_EVENT0("skia", "SkCanvas::drawTextBlob()"); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 2256 | if (blob) { |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2257 | this->onDrawTextBlob(blob, x, y, paint); |
| 2258 | } |
| 2259 | } |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2260 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2261 | void SkCanvas::drawVertices(VertexMode vmode, int vertexCount, |
| 2262 | const SkPoint verts[], const SkPoint texs[], |
| 2263 | const SkColor colors[], SkXfermode* xmode, |
| 2264 | const uint16_t indices[], int indexCount, |
| 2265 | const SkPaint& paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 2266 | TRACE_EVENT0("skia", "SkCanvas::drawVertices()"); |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2267 | LOOPER_BEGIN(paint, SkDrawFilter::kPath_Type, NULL) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2268 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2269 | while (iter.next()) { |
| 2270 | iter.fDevice->drawVertices(iter, vmode, vertexCount, verts, texs, |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2271 | colors, xmode, indices, indexCount, |
| 2272 | looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2273 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2274 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2275 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 2278 | void SkCanvas::drawPatch(const SkPoint cubics[12], const SkColor colors[4], |
| 2279 | const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 2280 | TRACE_EVENT0("skia", "SkCanvas::drawPatch()"); |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 2281 | if (NULL == cubics) { |
| 2282 | return; |
| 2283 | } |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 2284 | |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2285 | // Since a patch is always within the convex hull of the control points, we discard it when its |
| 2286 | // bounding rectangle is completely outside the current clip. |
| 2287 | SkRect bounds; |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 2288 | bounds.set(cubics, SkPatchUtils::kNumCtrlPts); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2289 | if (this->quickReject(bounds)) { |
| 2290 | return; |
| 2291 | } |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 2292 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 2293 | this->onDrawPatch(cubics, colors, texCoords, xmode, paint); |
| 2294 | } |
| 2295 | |
| 2296 | void SkCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], |
| 2297 | const SkPoint texCoords[4], SkXfermode* xmode, const SkPaint& paint) { |
| 2298 | |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2299 | LOOPER_BEGIN(paint, SkDrawFilter::kPath_Type, NULL) |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 2300 | |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2301 | while (iter.next()) { |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 2302 | iter.fDevice->drawPatch(iter, cubics, colors, texCoords, xmode, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2303 | } |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 2304 | |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2305 | LOOPER_END |
| 2306 | } |
| 2307 | |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 2308 | void SkCanvas::EXPERIMENTAL_drawDrawable(SkCanvasDrawable* dr) { |
| 2309 | if (dr) { |
| 2310 | SkRect bounds; |
| 2311 | if (!dr->getBounds(&bounds) || !this->quickReject(bounds)) { |
| 2312 | this->onDrawDrawable(dr); |
| 2313 | } |
| 2314 | } |
| 2315 | } |
| 2316 | |
| 2317 | void SkCanvas::onDrawDrawable(SkCanvasDrawable* dr) { |
| 2318 | dr->draw(this); |
| 2319 | } |
| 2320 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2321 | ////////////////////////////////////////////////////////////////////////////// |
| 2322 | // These methods are NOT virtual, and therefore must call back into virtual |
| 2323 | // methods, rather than actually drawing themselves. |
| 2324 | ////////////////////////////////////////////////////////////////////////////// |
| 2325 | |
| 2326 | void SkCanvas::drawARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b, |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 2327 | SkXfermode::Mode mode) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 2328 | TRACE_EVENT0("skia", "SkCanvas::drawARGB()"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2329 | SkPaint paint; |
| 2330 | |
| 2331 | paint.setARGB(a, r, g, b); |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 2332 | if (SkXfermode::kSrcOver_Mode != mode) { |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 2333 | paint.setXfermodeMode(mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2334 | } |
| 2335 | this->drawPaint(paint); |
| 2336 | } |
| 2337 | |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 2338 | void SkCanvas::drawColor(SkColor c, SkXfermode::Mode mode) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 2339 | TRACE_EVENT0("skia", "SkCanvas::drawColor()"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2340 | SkPaint paint; |
| 2341 | |
| 2342 | paint.setColor(c); |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 2343 | if (SkXfermode::kSrcOver_Mode != mode) { |
reed@android.com | 0baf193 | 2009-06-24 12:41:42 +0000 | [diff] [blame] | 2344 | paint.setXfermodeMode(mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2345 | } |
| 2346 | this->drawPaint(paint); |
| 2347 | } |
| 2348 | |
| 2349 | void SkCanvas::drawPoint(SkScalar x, SkScalar y, const SkPaint& paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 2350 | TRACE_EVENT0("skia", "SkCanvas::drawPoint(SkPaint)"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2351 | SkPoint pt; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2352 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2353 | pt.set(x, y); |
| 2354 | this->drawPoints(kPoints_PointMode, 1, &pt, paint); |
| 2355 | } |
| 2356 | |
| 2357 | void SkCanvas::drawPoint(SkScalar x, SkScalar y, SkColor color) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 2358 | TRACE_EVENT0("skia", "SkCanvas::drawPoint(SkColor)"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2359 | SkPoint pt; |
| 2360 | SkPaint paint; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2361 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2362 | pt.set(x, y); |
| 2363 | paint.setColor(color); |
| 2364 | this->drawPoints(kPoints_PointMode, 1, &pt, paint); |
| 2365 | } |
| 2366 | |
| 2367 | void SkCanvas::drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, |
| 2368 | const SkPaint& paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 2369 | TRACE_EVENT0("skia", "SkCanvas::drawLine()"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2370 | SkPoint pts[2]; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2371 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2372 | pts[0].set(x0, y0); |
| 2373 | pts[1].set(x1, y1); |
| 2374 | this->drawPoints(kLines_PointMode, 2, pts, paint); |
| 2375 | } |
| 2376 | |
| 2377 | void SkCanvas::drawRectCoords(SkScalar left, SkScalar top, |
| 2378 | SkScalar right, SkScalar bottom, |
| 2379 | const SkPaint& paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 2380 | TRACE_EVENT0("skia", "SkCanvas::drawRectCoords()"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2381 | SkRect r; |
| 2382 | |
| 2383 | r.set(left, top, right, bottom); |
| 2384 | this->drawRect(r, paint); |
| 2385 | } |
| 2386 | |
| 2387 | void SkCanvas::drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, |
| 2388 | const SkPaint& paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 2389 | TRACE_EVENT0("skia", "SkCanvas::drawCircle()"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2390 | if (radius < 0) { |
| 2391 | radius = 0; |
| 2392 | } |
| 2393 | |
| 2394 | SkRect r; |
| 2395 | r.set(cx - radius, cy - radius, cx + radius, cy + radius); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2396 | this->drawOval(r, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2397 | } |
| 2398 | |
| 2399 | void SkCanvas::drawRoundRect(const SkRect& r, SkScalar rx, SkScalar ry, |
| 2400 | const SkPaint& paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 2401 | TRACE_EVENT0("skia", "SkCanvas::drawRoundRect()"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2402 | if (rx > 0 && ry > 0) { |
| 2403 | if (paint.canComputeFastBounds()) { |
| 2404 | SkRect storage; |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 2405 | if (this->quickReject(paint.computeFastBounds(r, &storage))) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2406 | return; |
| 2407 | } |
| 2408 | } |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2409 | SkRRect rrect; |
| 2410 | rrect.setRectXY(r, rx, ry); |
| 2411 | this->drawRRect(rrect, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2412 | } else { |
| 2413 | this->drawRect(r, paint); |
| 2414 | } |
| 2415 | } |
| 2416 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2417 | void SkCanvas::drawArc(const SkRect& oval, SkScalar startAngle, |
| 2418 | SkScalar sweepAngle, bool useCenter, |
| 2419 | const SkPaint& paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 2420 | TRACE_EVENT0("skia", "SkCanvas::drawArc()"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2421 | if (SkScalarAbs(sweepAngle) >= SkIntToScalar(360)) { |
| 2422 | this->drawOval(oval, paint); |
| 2423 | } else { |
| 2424 | SkPath path; |
| 2425 | if (useCenter) { |
| 2426 | path.moveTo(oval.centerX(), oval.centerY()); |
| 2427 | } |
| 2428 | path.arcTo(oval, startAngle, sweepAngle, !useCenter); |
| 2429 | if (useCenter) { |
| 2430 | path.close(); |
| 2431 | } |
| 2432 | this->drawPath(path, paint); |
| 2433 | } |
| 2434 | } |
| 2435 | |
| 2436 | void SkCanvas::drawTextOnPathHV(const void* text, size_t byteLength, |
| 2437 | const SkPath& path, SkScalar hOffset, |
| 2438 | SkScalar vOffset, const SkPaint& paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 2439 | TRACE_EVENT0("skia", "SkCanvas::drawTextOnPathHV()"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2440 | SkMatrix matrix; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2441 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2442 | matrix.setTranslate(hOffset, vOffset); |
| 2443 | this->drawTextOnPath(text, byteLength, path, &matrix, paint); |
| 2444 | } |
| 2445 | |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 2446 | /////////////////////////////////////////////////////////////////////////////// |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 2447 | void SkCanvas::drawPicture(const SkPicture* picture) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 2448 | TRACE_EVENT0("skia", "SkCanvas::drawPicture()"); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 2449 | if (picture) { |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2450 | this->onDrawPicture(picture, NULL, NULL); |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 2451 | } |
| 2452 | } |
| 2453 | |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2454 | void SkCanvas::drawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint) { |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 2455 | TRACE_EVENT0("skia", "SkCanvas::drawPicture(SkMatrix, SkPaint)"); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 2456 | if (picture) { |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2457 | if (matrix && matrix->isIdentity()) { |
| 2458 | matrix = NULL; |
| 2459 | } |
| 2460 | this->onDrawPicture(picture, matrix, paint); |
| 2461 | } |
| 2462 | } |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 2463 | |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2464 | void SkCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, |
| 2465 | const SkPaint* paint) { |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 2466 | SkBaseDevice* device = this->getTopDevice(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 2467 | if (device) { |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 2468 | // Canvas has to first give the device the opportunity to render |
| 2469 | // the picture itself. |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2470 | if (device->EXPERIMENTAL_drawPicture(this, picture, matrix, paint)) { |
commit-bot@chromium.org | 145d1c0 | 2014-03-16 19:46:36 +0000 | [diff] [blame] | 2471 | return; // the device has rendered the entire picture |
| 2472 | } |
| 2473 | } |
| 2474 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 2475 | SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect()); |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2476 | |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 2477 | picture->playback(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2480 | /////////////////////////////////////////////////////////////////////////////// |
| 2481 | /////////////////////////////////////////////////////////////////////////////// |
| 2482 | |
| 2483 | SkCanvas::LayerIter::LayerIter(SkCanvas* canvas, bool skipEmptyClips) { |
reed@google.com | d642329 | 2010-12-20 20:53:13 +0000 | [diff] [blame] | 2484 | SK_COMPILE_ASSERT(sizeof(fStorage) >= sizeof(SkDrawIter), fStorage_too_small); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2485 | |
| 2486 | SkASSERT(canvas); |
| 2487 | |
| 2488 | fImpl = new (fStorage) SkDrawIter(canvas, skipEmptyClips); |
| 2489 | fDone = !fImpl->next(); |
| 2490 | } |
| 2491 | |
| 2492 | SkCanvas::LayerIter::~LayerIter() { |
| 2493 | fImpl->~SkDrawIter(); |
| 2494 | } |
| 2495 | |
| 2496 | void SkCanvas::LayerIter::next() { |
| 2497 | fDone = !fImpl->next(); |
| 2498 | } |
| 2499 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 2500 | SkBaseDevice* SkCanvas::LayerIter::device() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2501 | return fImpl->getDevice(); |
| 2502 | } |
| 2503 | |
| 2504 | const SkMatrix& SkCanvas::LayerIter::matrix() const { |
| 2505 | return fImpl->getMatrix(); |
| 2506 | } |
| 2507 | |
| 2508 | const SkPaint& SkCanvas::LayerIter::paint() const { |
| 2509 | const SkPaint* paint = fImpl->getPaint(); |
| 2510 | if (NULL == paint) { |
| 2511 | paint = &fDefaultPaint; |
| 2512 | } |
| 2513 | return *paint; |
| 2514 | } |
| 2515 | |
| 2516 | const SkRegion& SkCanvas::LayerIter::clip() const { return fImpl->getClip(); } |
| 2517 | int SkCanvas::LayerIter::x() const { return fImpl->getX(); } |
| 2518 | int SkCanvas::LayerIter::y() const { return fImpl->getY(); } |
justinlin@google.com | 20a550c | 2012-07-27 17:37:12 +0000 | [diff] [blame] | 2519 | |
| 2520 | /////////////////////////////////////////////////////////////////////////////// |
| 2521 | |
fmalita | c3b589a | 2014-06-05 12:40:07 -0700 | [diff] [blame] | 2522 | SkCanvasClipVisitor::~SkCanvasClipVisitor() { } |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 2523 | |
| 2524 | /////////////////////////////////////////////////////////////////////////////// |
| 2525 | |
| 2526 | static bool supported_for_raster_canvas(const SkImageInfo& info) { |
| 2527 | switch (info.alphaType()) { |
| 2528 | case kPremul_SkAlphaType: |
| 2529 | case kOpaque_SkAlphaType: |
| 2530 | break; |
| 2531 | default: |
| 2532 | return false; |
| 2533 | } |
| 2534 | |
| 2535 | switch (info.colorType()) { |
| 2536 | case kAlpha_8_SkColorType: |
| 2537 | case kRGB_565_SkColorType: |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 2538 | case kN32_SkColorType: |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 2539 | break; |
| 2540 | default: |
| 2541 | return false; |
| 2542 | } |
| 2543 | |
| 2544 | return true; |
| 2545 | } |
| 2546 | |
| 2547 | SkCanvas* SkCanvas::NewRaster(const SkImageInfo& info) { |
| 2548 | if (!supported_for_raster_canvas(info)) { |
| 2549 | return NULL; |
| 2550 | } |
| 2551 | |
| 2552 | SkBitmap bitmap; |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 2553 | if (!bitmap.tryAllocPixels(info)) { |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 2554 | return NULL; |
| 2555 | } |
| 2556 | |
| 2557 | // should this functionality be moved into allocPixels()? |
| 2558 | if (!bitmap.info().isOpaque()) { |
| 2559 | bitmap.eraseColor(0); |
| 2560 | } |
| 2561 | return SkNEW_ARGS(SkCanvas, (bitmap)); |
| 2562 | } |
commit-bot@chromium.org | 42b0893 | 2014-03-17 02:13:07 +0000 | [diff] [blame] | 2563 | |
| 2564 | SkCanvas* SkCanvas::NewRasterDirect(const SkImageInfo& info, void* pixels, size_t rowBytes) { |
| 2565 | if (!supported_for_raster_canvas(info)) { |
| 2566 | return NULL; |
| 2567 | } |
skia.committer@gmail.com | eb849e5 | 2014-03-17 03:02:17 +0000 | [diff] [blame] | 2568 | |
commit-bot@chromium.org | 42b0893 | 2014-03-17 02:13:07 +0000 | [diff] [blame] | 2569 | SkBitmap bitmap; |
| 2570 | if (!bitmap.installPixels(info, pixels, rowBytes)) { |
| 2571 | return NULL; |
| 2572 | } |
commit-bot@chromium.org | 42b0893 | 2014-03-17 02:13:07 +0000 | [diff] [blame] | 2573 | return SkNEW_ARGS(SkCanvas, (bitmap)); |
| 2574 | } |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2575 | |
| 2576 | /////////////////////////////////////////////////////////////////////////////// |
| 2577 | |
| 2578 | SkAutoCanvasMatrixPaint::SkAutoCanvasMatrixPaint(SkCanvas* canvas, const SkMatrix* matrix, |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 2579 | const SkPaint* paint, const SkRect& bounds) |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2580 | : fCanvas(canvas) |
| 2581 | , fSaveCount(canvas->getSaveCount()) |
| 2582 | { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 2583 | if (paint) { |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 2584 | SkRect newBounds = bounds; |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2585 | if (matrix) { |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 2586 | matrix->mapRect(&newBounds); |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2587 | } |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 2588 | canvas->saveLayer(&newBounds, paint); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 2589 | } else if (matrix) { |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2590 | canvas->save(); |
| 2591 | } |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 2592 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 2593 | if (matrix) { |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2594 | canvas->concat(*matrix); |
| 2595 | } |
| 2596 | } |
| 2597 | |
| 2598 | SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() { |
| 2599 | fCanvas->restoreToCount(fSaveCount); |
| 2600 | } |