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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkColorFilter.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkImage.h" |
| 12 | #include "include/core/SkImageFilter.h" |
| 13 | #include "include/core/SkPathEffect.h" |
| 14 | #include "include/core/SkPicture.h" |
| 15 | #include "include/core/SkRRect.h" |
| 16 | #include "include/core/SkRasterHandleAllocator.h" |
| 17 | #include "include/core/SkString.h" |
| 18 | #include "include/core/SkTextBlob.h" |
| 19 | #include "include/core/SkVertices.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "include/private/SkNx.h" |
| 21 | #include "include/private/SkTo.h" |
| 22 | #include "include/utils/SkNoDrawCanvas.h" |
Ben Wagner | 729a23f | 2019-05-17 16:29:34 -0400 | [diff] [blame] | 23 | #include "src/core/SkArenaAlloc.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/core/SkBitmapDevice.h" |
| 25 | #include "src/core/SkCanvasPriv.h" |
| 26 | #include "src/core/SkClipOpPriv.h" |
| 27 | #include "src/core/SkClipStack.h" |
| 28 | #include "src/core/SkDraw.h" |
| 29 | #include "src/core/SkGlyphRun.h" |
| 30 | #include "src/core/SkImageFilterCache.h" |
Michael Ludwig | 8ee6cf3 | 2019-08-02 09:57:04 -0400 | [diff] [blame] | 31 | #include "src/core/SkImageFilter_Base.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 32 | #include "src/core/SkLatticeIter.h" |
| 33 | #include "src/core/SkMSAN.h" |
| 34 | #include "src/core/SkMakeUnique.h" |
| 35 | #include "src/core/SkMatrixUtils.h" |
| 36 | #include "src/core/SkPaintPriv.h" |
| 37 | #include "src/core/SkRasterClip.h" |
| 38 | #include "src/core/SkSpecialImage.h" |
| 39 | #include "src/core/SkStrikeCache.h" |
| 40 | #include "src/core/SkTLazy.h" |
| 41 | #include "src/core/SkTextFormatParams.h" |
| 42 | #include "src/core/SkTraceEvent.h" |
| 43 | #include "src/image/SkImage_Base.h" |
| 44 | #include "src/image/SkSurface_Base.h" |
| 45 | #include "src/utils/SkPatchUtils.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 46 | |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 47 | #include <new> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 49 | #if SK_SUPPORT_GPU |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 50 | #include "include/gpu/GrContext.h" |
| 51 | #include "src/gpu/SkGr.h" |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 52 | #endif |
| 53 | |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 54 | #define RETURN_ON_NULL(ptr) do { if (nullptr == (ptr)) return; } while (0) |
Mike Reed | 74d6e11 | 2018-01-23 13:06:12 -0500 | [diff] [blame] | 55 | #define RETURN_ON_FALSE(pred) do { if (!(pred)) return; } while (0) |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 56 | |
Mike Reed | 139e5e0 | 2017-03-08 11:29:33 -0500 | [diff] [blame] | 57 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 58 | |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 59 | /* |
| 60 | * Return true if the drawing this rect would hit every pixels in the canvas. |
| 61 | * |
| 62 | * Returns false if |
| 63 | * - rect does not contain the canvas' bounds |
| 64 | * - paint is not fill |
| 65 | * - paint would blur or otherwise change the coverage of the rect |
| 66 | */ |
| 67 | bool SkCanvas::wouldOverwriteEntireSurface(const SkRect* rect, const SkPaint* paint, |
| 68 | ShaderOverrideOpacity overrideOpacity) const { |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 69 | static_assert((int)SkPaintPriv::kNone_ShaderOverrideOpacity == |
| 70 | (int)kNone_ShaderOverrideOpacity, |
| 71 | "need_matching_enums0"); |
| 72 | static_assert((int)SkPaintPriv::kOpaque_ShaderOverrideOpacity == |
| 73 | (int)kOpaque_ShaderOverrideOpacity, |
| 74 | "need_matching_enums1"); |
| 75 | static_assert((int)SkPaintPriv::kNotOpaque_ShaderOverrideOpacity == |
| 76 | (int)kNotOpaque_ShaderOverrideOpacity, |
| 77 | "need_matching_enums2"); |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 78 | |
| 79 | const SkISize size = this->getBaseLayerSize(); |
| 80 | const SkRect bounds = SkRect::MakeIWH(size.width(), size.height()); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 81 | |
| 82 | // if we're clipped at all, we can't overwrite the entire surface |
| 83 | { |
| 84 | SkBaseDevice* base = this->getDevice(); |
| 85 | SkBaseDevice* top = this->getTopDevice(); |
| 86 | if (base != top) { |
| 87 | return false; // we're in a saveLayer, so conservatively don't assume we'll overwrite |
| 88 | } |
| 89 | if (!base->clipIsWideOpen()) { |
| 90 | return false; |
| 91 | } |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | if (rect) { |
halcanary | c5769b2 | 2016-08-10 07:13:21 -0700 | [diff] [blame] | 95 | if (!this->getTotalMatrix().isScaleTranslate()) { |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 96 | return false; // conservative |
| 97 | } |
halcanary | c5769b2 | 2016-08-10 07:13:21 -0700 | [diff] [blame] | 98 | |
| 99 | SkRect devRect; |
| 100 | this->getTotalMatrix().mapRectScaleTranslate(&devRect, *rect); |
| 101 | if (!devRect.contains(bounds)) { |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 102 | return false; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (paint) { |
| 107 | SkPaint::Style paintStyle = paint->getStyle(); |
| 108 | if (!(paintStyle == SkPaint::kFill_Style || |
| 109 | paintStyle == SkPaint::kStrokeAndFill_Style)) { |
| 110 | return false; |
| 111 | } |
Mike Reed | 9dc0b9e | 2019-07-29 17:52:48 -0400 | [diff] [blame] | 112 | if (paint->getMaskFilter() || paint->getPathEffect() || paint->getImageFilter()) { |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 113 | return false; // conservative |
| 114 | } |
| 115 | } |
| 116 | return SkPaintPriv::Overwrites(paint, (SkPaintPriv::ShaderOverrideOpacity)overrideOpacity); |
| 117 | } |
| 118 | |
| 119 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 120 | |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 121 | // experimental for faster tiled drawing... |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | //#define SK_TRACE_SAVERESTORE |
| 123 | |
| 124 | #ifdef SK_TRACE_SAVERESTORE |
| 125 | static int gLayerCounter; |
| 126 | static void inc_layer() { ++gLayerCounter; printf("----- inc layer %d\n", gLayerCounter); } |
| 127 | static void dec_layer() { --gLayerCounter; printf("----- dec layer %d\n", gLayerCounter); } |
| 128 | |
| 129 | static int gRecCounter; |
| 130 | static void inc_rec() { ++gRecCounter; printf("----- inc rec %d\n", gRecCounter); } |
| 131 | static void dec_rec() { --gRecCounter; printf("----- dec rec %d\n", gRecCounter); } |
| 132 | |
| 133 | static int gCanvasCounter; |
| 134 | static void inc_canvas() { ++gCanvasCounter; printf("----- inc canvas %d\n", gCanvasCounter); } |
| 135 | static void dec_canvas() { --gCanvasCounter; printf("----- dec canvas %d\n", gCanvasCounter); } |
| 136 | #else |
| 137 | #define inc_layer() |
| 138 | #define dec_layer() |
| 139 | #define inc_rec() |
| 140 | #define dec_rec() |
| 141 | #define inc_canvas() |
| 142 | #define dec_canvas() |
| 143 | #endif |
| 144 | |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 145 | typedef SkTLazy<SkPaint> SkLazyPaint; |
| 146 | |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 147 | void SkCanvas::predrawNotify(bool willOverwritesEntireSurface) { |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 148 | if (fSurfaceBase) { |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 149 | fSurfaceBase->aboutToDraw(willOverwritesEntireSurface |
| 150 | ? SkSurface::kDiscard_ContentChangeMode |
| 151 | : SkSurface::kRetain_ContentChangeMode); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void SkCanvas::predrawNotify(const SkRect* rect, const SkPaint* paint, |
| 156 | ShaderOverrideOpacity overrideOpacity) { |
| 157 | if (fSurfaceBase) { |
| 158 | SkSurface::ContentChangeMode mode = SkSurface::kRetain_ContentChangeMode; |
| 159 | // Since willOverwriteAllPixels() may not be complete free to call, we only do so if |
| 160 | // there is an outstanding snapshot, since w/o that, there will be no copy-on-write |
| 161 | // and therefore we don't care which mode we're in. |
| 162 | // |
| 163 | if (fSurfaceBase->outstandingImageSnapshot()) { |
| 164 | if (this->wouldOverwriteEntireSurface(rect, paint, overrideOpacity)) { |
| 165 | mode = SkSurface::kDiscard_ContentChangeMode; |
| 166 | } |
| 167 | } |
| 168 | fSurfaceBase->aboutToDraw(mode); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 172 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 173 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 174 | /* 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] | 175 | The clip/matrix/proc are fields that reflect the top of the save/restore |
| 176 | stack. Whenever the canvas changes, it marks a dirty flag, and then before |
| 177 | these are used (assuming we're not on a layer) we rebuild these cache |
| 178 | values: they reflect the top of the save stack, but translated and clipped |
| 179 | by the device's XY offset and bitmap-bounds. |
| 180 | */ |
| 181 | struct DeviceCM { |
Florin Malita | 713b8ef | 2017-04-28 10:57:24 -0400 | [diff] [blame] | 182 | DeviceCM* fNext; |
| 183 | sk_sp<SkBaseDevice> fDevice; |
| 184 | SkRasterClip fClip; |
| 185 | std::unique_ptr<const SkPaint> fPaint; // may be null (in the future) |
| 186 | SkMatrix fStashedMatrix; // original CTM; used by imagefilter in saveLayer |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 187 | sk_sp<SkImage> fClipImage; |
| 188 | SkMatrix fClipMatrix; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 189 | |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 190 | DeviceCM(sk_sp<SkBaseDevice> device, const SkPaint* paint, const SkMatrix& stashed, |
Mike Klein | b34ab04 | 2017-05-01 21:34:14 +0000 | [diff] [blame] | 191 | const SkImage* clipImage, const SkMatrix* clipMatrix) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 192 | : fNext(nullptr) |
Florin Malita | 713b8ef | 2017-04-28 10:57:24 -0400 | [diff] [blame] | 193 | , fDevice(std::move(device)) |
| 194 | , fPaint(paint ? skstd::make_unique<SkPaint>(*paint) : nullptr) |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 195 | , fStashedMatrix(stashed) |
Mike Klein | b34ab04 | 2017-05-01 21:34:14 +0000 | [diff] [blame] | 196 | , fClipImage(sk_ref_sp(const_cast<SkImage*>(clipImage))) |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 197 | , fClipMatrix(clipMatrix ? *clipMatrix : SkMatrix::I()) |
Florin Malita | 713b8ef | 2017-04-28 10:57:24 -0400 | [diff] [blame] | 198 | {} |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 199 | |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 200 | void reset(const SkIRect& bounds) { |
| 201 | SkASSERT(!fPaint); |
| 202 | SkASSERT(!fNext); |
| 203 | SkASSERT(fDevice); |
| 204 | fClip.setRect(bounds); |
| 205 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 206 | }; |
| 207 | |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 208 | namespace { |
| 209 | // Encapsulate state needed to restore from saveBehind() |
| 210 | struct BackImage { |
| 211 | sk_sp<SkSpecialImage> fImage; |
| 212 | SkIPoint fLoc; |
| 213 | }; |
| 214 | } |
| 215 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 216 | /* This is the record we keep for each save/restore level in the stack. |
| 217 | Since a level optionally copies the matrix and/or stack, we have pointers |
| 218 | for these fields. If the value is copied for this level, the copy is |
| 219 | stored in the ...Storage field, and the pointer points to that. If the |
| 220 | value is not copied for this level, we ignore ...Storage, and just point |
| 221 | at the corresponding value in the previous level in the stack. |
| 222 | */ |
| 223 | class SkCanvas::MCRec { |
| 224 | public: |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 225 | DeviceCM* fLayer; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 226 | /* If there are any layers in the stack, this points to the top-most |
| 227 | one that is at or below this level in the stack (so we know what |
| 228 | bitmap/device to draw into from this level. This value is NOT |
| 229 | reference counted, since the real owner is either our fLayer field, |
| 230 | or a previous one in a lower level.) |
| 231 | */ |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 232 | DeviceCM* fTopLayer; |
| 233 | std::unique_ptr<BackImage> fBackImage; |
| 234 | SkConservativeClip fRasterClip; |
| 235 | SkMatrix fMatrix; |
| 236 | int fDeferredSaveCount; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 237 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 238 | MCRec() { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 239 | fLayer = nullptr; |
| 240 | fTopLayer = nullptr; |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 241 | fMatrix.reset(); |
| 242 | fDeferredSaveCount = 0; |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 243 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 244 | // don't bother initializing fNext |
| 245 | inc_rec(); |
| 246 | } |
Jim Van Verth | 343fe49 | 2017-05-02 16:49:24 -0400 | [diff] [blame] | 247 | MCRec(const MCRec& prev) : fRasterClip(prev.fRasterClip), fMatrix(prev.fMatrix) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 248 | fLayer = nullptr; |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 249 | fTopLayer = prev.fTopLayer; |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 250 | fDeferredSaveCount = 0; |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 251 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 252 | // don't bother initializing fNext |
| 253 | inc_rec(); |
| 254 | } |
| 255 | ~MCRec() { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 256 | delete fLayer; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 257 | dec_rec(); |
| 258 | } |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 259 | |
| 260 | void reset(const SkIRect& bounds) { |
| 261 | SkASSERT(fLayer); |
| 262 | SkASSERT(fDeferredSaveCount == 0); |
| 263 | |
| 264 | fMatrix.reset(); |
| 265 | fRasterClip.setRect(bounds); |
| 266 | fLayer->reset(bounds); |
| 267 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 268 | }; |
| 269 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 270 | class SkDrawIter { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 271 | public: |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 272 | SkDrawIter(SkCanvas* canvas) |
| 273 | : fDevice(nullptr), fCurrLayer(canvas->fMCRec->fTopLayer), fPaint(nullptr) |
| 274 | {} |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 275 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 276 | bool next() { |
reed@google.com | f68c5e2 | 2012-02-24 16:38:58 +0000 | [diff] [blame] | 277 | const DeviceCM* rec = fCurrLayer; |
| 278 | if (rec && rec->fDevice) { |
Florin Malita | 713b8ef | 2017-04-28 10:57:24 -0400 | [diff] [blame] | 279 | fDevice = rec->fDevice.get(); |
| 280 | fPaint = rec->fPaint.get(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 281 | fCurrLayer = rec->fNext; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 282 | // fCurrLayer may be nullptr now |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 283 | return true; |
| 284 | } |
| 285 | return false; |
| 286 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 287 | |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 288 | int getX() const { return fDevice->getOrigin().x(); } |
| 289 | int getY() const { return fDevice->getOrigin().y(); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 290 | const SkPaint* getPaint() const { return fPaint; } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 291 | |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 292 | SkBaseDevice* fDevice; |
| 293 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 294 | private: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 295 | const DeviceCM* fCurrLayer; |
| 296 | const SkPaint* fPaint; // May be null. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 297 | }; |
| 298 | |
Florin Malita | 713b8ef | 2017-04-28 10:57:24 -0400 | [diff] [blame] | 299 | #define FOR_EACH_TOP_DEVICE( code ) \ |
| 300 | do { \ |
| 301 | DeviceCM* layer = fMCRec->fTopLayer; \ |
| 302 | while (layer) { \ |
| 303 | SkBaseDevice* device = layer->fDevice.get(); \ |
| 304 | if (device) { \ |
| 305 | code; \ |
| 306 | } \ |
| 307 | layer = layer->fNext; \ |
| 308 | } \ |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 309 | } while (0) |
| 310 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 311 | ///////////////////////////////////////////////////////////////////////////// |
| 312 | |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 313 | /** |
| 314 | * If the paint has an imagefilter, but it can be simplified to just a colorfilter, return that |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 315 | * colorfilter, else return nullptr. |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 316 | */ |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 317 | static sk_sp<SkColorFilter> image_to_color_filter(const SkPaint& paint) { |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 318 | SkImageFilter* imgf = paint.getImageFilter(); |
| 319 | if (!imgf) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 320 | return nullptr; |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 321 | } |
| 322 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 323 | SkColorFilter* imgCFPtr; |
| 324 | if (!imgf->asAColorFilter(&imgCFPtr)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 325 | return nullptr; |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 326 | } |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 327 | sk_sp<SkColorFilter> imgCF(imgCFPtr); |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 328 | |
| 329 | SkColorFilter* paintCF = paint.getColorFilter(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 330 | if (nullptr == paintCF) { |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 331 | // there is no existing paint colorfilter, so we can just return the imagefilter's |
| 332 | return imgCF; |
| 333 | } |
| 334 | |
| 335 | // The paint has both a colorfilter(paintCF) and an imagefilter-which-is-a-colorfilter(imgCF) |
| 336 | // and we need to combine them into a single colorfilter. |
Mike Reed | 19d7bd6 | 2018-02-19 14:10:57 -0500 | [diff] [blame] | 337 | return imgCF->makeComposed(sk_ref_sp(paintCF)); |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 338 | } |
| 339 | |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 340 | /** |
| 341 | * There are many bounds in skia. A circle's bounds is just its center extended by its radius. |
| 342 | * However, if we stroke a circle, then the "bounds" of that is larger, since it will now draw |
| 343 | * outside of its raw-bounds by 1/2 the stroke width. SkPaint has lots of optional |
| 344 | * effects/attributes that can modify the effective bounds of a given primitive -- maskfilters, |
| 345 | * patheffects, stroking, etc. This function takes a raw bounds and a paint, and returns the |
| 346 | * conservative "effective" bounds based on the settings in the paint... with one exception. This |
| 347 | * function does *not* look at the imagefilter, which can also modify the effective bounds. It is |
| 348 | * deliberately ignored. |
| 349 | */ |
| 350 | static const SkRect& apply_paint_to_bounds_sans_imagefilter(const SkPaint& paint, |
| 351 | const SkRect& rawBounds, |
| 352 | SkRect* storage) { |
| 353 | SkPaint tmpUnfiltered(paint); |
| 354 | tmpUnfiltered.setImageFilter(nullptr); |
| 355 | if (tmpUnfiltered.canComputeFastBounds()) { |
| 356 | return tmpUnfiltered.computeFastBounds(rawBounds, storage); |
| 357 | } else { |
| 358 | return rawBounds; |
| 359 | } |
| 360 | } |
| 361 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 362 | class AutoLayerForImageFilter { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 363 | public: |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 364 | // "rawBounds" is the original bounds of the primitive about to be drawn, unmodified by the |
| 365 | // paint. It's used to determine the size of the offscreen layer for filters. |
| 366 | // If null, the clip will be used instead. |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 367 | AutoLayerForImageFilter(SkCanvas* canvas, const SkPaint& origPaint, |
| 368 | bool skipLayerForImageFilter = false, |
| 369 | const SkRect* rawBounds = nullptr) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 370 | fCanvas = canvas; |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 371 | fPaint = &origPaint; |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 372 | fSaveCount = canvas->getSaveCount(); |
reed | 5c476fb | 2015-04-20 08:04:21 -0700 | [diff] [blame] | 373 | fTempLayerForImageFilter = false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 374 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 375 | if (auto simplifiedCF = image_to_color_filter(origPaint)) { |
| 376 | SkASSERT(!fLazyPaint.isValid()); |
| 377 | SkPaint* paint = fLazyPaint.set(origPaint); |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 378 | paint->setColorFilter(std::move(simplifiedCF)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 379 | paint->setImageFilter(nullptr); |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 380 | fPaint = paint; |
| 381 | } |
| 382 | |
| 383 | if (!skipLayerForImageFilter && fPaint->getImageFilter()) { |
reed | 5c476fb | 2015-04-20 08:04:21 -0700 | [diff] [blame] | 384 | /** |
| 385 | * We implement ImageFilters for a given draw by creating a layer, then applying the |
| 386 | * imagefilter to the pixels of that layer (its backing surface/image), and then |
| 387 | * we call restore() to xfer that layer to the main canvas. |
| 388 | * |
| 389 | * 1. SaveLayer (with a paint containing the current imagefilter and xfermode) |
| 390 | * 2. Generate the src pixels: |
| 391 | * Remove the imagefilter and the xfermode from the paint that we (AutoDrawLooper) |
| 392 | * return (fPaint). We then draw the primitive (using srcover) into a cleared |
| 393 | * buffer/surface. |
| 394 | * 3. Restore the layer created in #1 |
| 395 | * The imagefilter is passed the buffer/surface from the layer (now filled with the |
| 396 | * src pixels of the primitive). It returns a new "filtered" buffer, which we |
| 397 | * draw onto the previous layer using the xfermode from the original paint. |
| 398 | */ |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 399 | |
| 400 | SkPaint restorePaint; |
| 401 | restorePaint.setImageFilter(fPaint->refImageFilter()); |
| 402 | restorePaint.setBlendMode(fPaint->getBlendMode()); |
| 403 | |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 404 | SkRect storage; |
| 405 | if (rawBounds) { |
| 406 | // Make rawBounds include all paint outsets except for those due to image filters. |
| 407 | rawBounds = &apply_paint_to_bounds_sans_imagefilter(*fPaint, *rawBounds, &storage); |
| 408 | } |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 409 | (void)canvas->internalSaveLayer(SkCanvas::SaveLayerRec(rawBounds, &restorePaint), |
reed | 76033be | 2015-03-14 10:54:31 -0700 | [diff] [blame] | 410 | SkCanvas::kFullLayer_SaveLayerStrategy); |
reed | 5c476fb | 2015-04-20 08:04:21 -0700 | [diff] [blame] | 411 | fTempLayerForImageFilter = true; |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 412 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 413 | // Remove the restorePaint fields from our "working" paint |
| 414 | SkASSERT(!fLazyPaint.isValid()); |
| 415 | SkPaint* paint = fLazyPaint.set(origPaint); |
| 416 | paint->setImageFilter(nullptr); |
| 417 | paint->setBlendMode(SkBlendMode::kSrcOver); |
| 418 | fPaint = paint; |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 419 | } |
| 420 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 421 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 422 | ~AutoLayerForImageFilter() { |
reed | 5c476fb | 2015-04-20 08:04:21 -0700 | [diff] [blame] | 423 | if (fTempLayerForImageFilter) { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 424 | fCanvas->internalRestore(); |
| 425 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 426 | SkASSERT(fCanvas->getSaveCount() == fSaveCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 427 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 428 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 429 | const SkPaint& paint() const { |
| 430 | SkASSERT(fPaint); |
| 431 | return *fPaint; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 432 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 433 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 434 | private: |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 435 | SkLazyPaint fLazyPaint; // base paint storage in case we need to modify it |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 436 | SkCanvas* fCanvas; |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 437 | const SkPaint* fPaint; // points to either the original paint, or lazy (if we needed it) |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 438 | int fSaveCount; |
reed | 5c476fb | 2015-04-20 08:04:21 -0700 | [diff] [blame] | 439 | bool fTempLayerForImageFilter; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 440 | }; |
| 441 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 442 | ////////// macros to place around the internal draw calls ////////////////// |
| 443 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 444 | #define DRAW_BEGIN_DRAWBITMAP(paint, skipLayerForFilter, bounds) \ |
reed | 3aafe11 | 2016-08-18 12:45:34 -0700 | [diff] [blame] | 445 | this->predrawNotify(); \ |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 446 | AutoLayerForImageFilter draw(this, paint, skipLayerForFilter, bounds); \ |
| 447 | { SkDrawIter iter(this); |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 448 | |
| 449 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 450 | #define DRAW_BEGIN_DRAWDEVICE(paint) \ |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 451 | this->predrawNotify(); \ |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 452 | AutoLayerForImageFilter draw(this, paint, true); \ |
| 453 | { SkDrawIter iter(this); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 454 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 455 | #define DRAW_BEGIN(paint, bounds) \ |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 456 | this->predrawNotify(); \ |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 457 | AutoLayerForImageFilter draw(this, paint, false, bounds); \ |
| 458 | { SkDrawIter iter(this); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 459 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 460 | #define DRAW_BEGIN_CHECK_COMPLETE_OVERWRITE(paint, bounds, auxOpaque) \ |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 461 | this->predrawNotify(bounds, &paint, auxOpaque); \ |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 462 | AutoLayerForImageFilter draw(this, paint, false, bounds); \ |
| 463 | { SkDrawIter iter(this); |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 464 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 465 | #define DRAW_END } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 466 | |
| 467 | //////////////////////////////////////////////////////////////////////////// |
| 468 | |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 469 | static inline SkRect qr_clip_bounds(const SkIRect& bounds) { |
| 470 | if (bounds.isEmpty()) { |
| 471 | return SkRect::MakeEmpty(); |
| 472 | } |
| 473 | |
| 474 | // Expand bounds out by 1 in case we are anti-aliasing. We store the |
| 475 | // bounds as floats to enable a faster quick reject implementation. |
| 476 | SkRect dst; |
| 477 | SkNx_cast<float>(Sk4i::Load(&bounds.fLeft) + Sk4i(-1,-1,1,1)).store(&dst.fLeft); |
| 478 | return dst; |
| 479 | } |
| 480 | |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 481 | void SkCanvas::resetForNextPicture(const SkIRect& bounds) { |
| 482 | this->restoreToCount(1); |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 483 | fMCRec->reset(bounds); |
| 484 | |
| 485 | // We're peering through a lot of structs here. Only at this scope do we |
Mike Reed | 139e5e0 | 2017-03-08 11:29:33 -0500 | [diff] [blame] | 486 | // know that the device is a SkNoPixelsDevice. |
Florin Malita | 713b8ef | 2017-04-28 10:57:24 -0400 | [diff] [blame] | 487 | static_cast<SkNoPixelsDevice*>(fMCRec->fLayer->fDevice.get())->resetForNextPicture(bounds); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 488 | fDeviceClipBounds = qr_clip_bounds(bounds); |
msarett | 9637ea9 | 2016-08-18 14:03:30 -0700 | [diff] [blame] | 489 | fIsScaleTranslate = true; |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 490 | } |
| 491 | |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 492 | void SkCanvas::init(sk_sp<SkBaseDevice> device) { |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 493 | fAllowSimplifyClip = false; |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 494 | fSaveCount = 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 495 | |
| 496 | fMCRec = (MCRec*)fMCStack.push_back(); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 497 | new (fMCRec) MCRec; |
Stan Iliev | 5f1bb0a | 2016-12-12 17:39:55 -0500 | [diff] [blame] | 498 | fMCRec->fRasterClip.setDeviceClipRestriction(&fClipRestrictionRect); |
msarett | 9637ea9 | 2016-08-18 14:03:30 -0700 | [diff] [blame] | 499 | fIsScaleTranslate = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 500 | |
reed | a499f90 | 2015-05-01 09:34:31 -0700 | [diff] [blame] | 501 | SkASSERT(sizeof(DeviceCM) <= sizeof(fDeviceCMStorage)); |
| 502 | fMCRec->fLayer = (DeviceCM*)fDeviceCMStorage; |
Herb Derby | efe39bc | 2018-05-01 17:06:20 -0400 | [diff] [blame] | 503 | new (fDeviceCMStorage) DeviceCM(device, nullptr, fMCRec->fMatrix, nullptr, nullptr); |
reed | b679ca8 | 2015-04-07 04:40:48 -0700 | [diff] [blame] | 504 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 505 | fMCRec->fTopLayer = fMCRec->fLayer; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 506 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 507 | fSurfaceBase = nullptr; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 508 | |
reed | f92c866 | 2014-08-18 08:02:43 -0700 | [diff] [blame] | 509 | if (device) { |
robertphillips | efbffed | 2015-06-22 12:06:08 -0700 | [diff] [blame] | 510 | // The root device and the canvas should always have the same pixel geometry |
| 511 | SkASSERT(fProps.pixelGeometry() == device->surfaceProps().pixelGeometry()); |
reed | 78e2768 | 2014-11-19 08:04:34 -0800 | [diff] [blame] | 512 | fMCRec->fRasterClip.setRect(device->getGlobalBounds()); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 513 | fDeviceClipBounds = qr_clip_bounds(device->getGlobalBounds()); |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 514 | |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 515 | device->androidFramework_setDeviceClipRestriction(&fClipRestrictionRect); |
reed | f92c866 | 2014-08-18 08:02:43 -0700 | [diff] [blame] | 516 | } |
Herb Derby | 4ffa027 | 2018-06-04 15:49:15 -0400 | [diff] [blame] | 517 | |
Herb Derby | 59d997a | 2018-06-07 12:44:09 -0400 | [diff] [blame] | 518 | fScratchGlyphRunBuilder = skstd::make_unique<SkGlyphRunBuilder>(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 519 | } |
| 520 | |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 521 | SkCanvas::SkCanvas() |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 522 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 523 | , fProps(SkSurfaceProps::kLegacyFontHost_InitType) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 524 | { |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 525 | inc_canvas(); |
skia.committer@gmail.com | ba12448 | 2014-02-01 03:01:57 +0000 | [diff] [blame] | 526 | |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 527 | this->init(nullptr); |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 528 | } |
| 529 | |
reed | 96a857e | 2015-01-25 10:33:58 -0800 | [diff] [blame] | 530 | SkCanvas::SkCanvas(int width, int height, const SkSurfaceProps* props) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 531 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
reed | 96a857e | 2015-01-25 10:33:58 -0800 | [diff] [blame] | 532 | , fProps(SkSurfacePropsCopyOrDefault(props)) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 533 | { |
| 534 | inc_canvas(); |
Herb Derby | efe39bc | 2018-05-01 17:06:20 -0400 | [diff] [blame] | 535 | this->init(sk_make_sp<SkNoPixelsDevice>( |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 536 | SkIRect::MakeWH(SkTMax(width, 0), SkTMax(height, 0)), fProps)); |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 537 | } |
skia.committer@gmail.com | ba12448 | 2014-02-01 03:01:57 +0000 | [diff] [blame] | 538 | |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 539 | SkCanvas::SkCanvas(const SkIRect& bounds) |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 540 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 541 | , fProps(SkSurfaceProps::kLegacyFontHost_InitType) |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 542 | { |
| 543 | inc_canvas(); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 544 | |
Mike Reed | 566e53c | 2017-03-10 10:49:45 -0500 | [diff] [blame] | 545 | SkIRect r = bounds.isEmpty() ? SkIRect::MakeEmpty() : bounds; |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 546 | this->init(sk_make_sp<SkNoPixelsDevice>(r, fProps)); |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Herb Derby | efe39bc | 2018-05-01 17:06:20 -0400 | [diff] [blame] | 549 | SkCanvas::SkCanvas(sk_sp<SkBaseDevice> device) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 550 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
robertphillips | 7b05ff1 | 2015-06-19 14:14:54 -0700 | [diff] [blame] | 551 | , fProps(device->surfaceProps()) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 552 | { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 553 | inc_canvas(); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 554 | |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 555 | this->init(device); |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 556 | } |
| 557 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 558 | SkCanvas::SkCanvas(const SkBitmap& bitmap, const SkSurfaceProps& props) |
reed | 3716fd0 | 2014-09-21 09:39:55 -0700 | [diff] [blame] | 559 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 560 | , fProps(props) |
reed | 3716fd0 | 2014-09-21 09:39:55 -0700 | [diff] [blame] | 561 | { |
| 562 | inc_canvas(); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 563 | |
Mike Reed | 910ca0f | 2018-04-25 13:04:05 -0400 | [diff] [blame] | 564 | sk_sp<SkBaseDevice> device(new SkBitmapDevice(bitmap, fProps, nullptr, nullptr)); |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 565 | this->init(device); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 566 | } |
reed | 29c857d | 2014-09-21 10:25:07 -0700 | [diff] [blame] | 567 | |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 568 | SkCanvas::SkCanvas(const SkBitmap& bitmap, std::unique_ptr<SkRasterHandleAllocator> alloc, |
| 569 | SkRasterHandleAllocator::Handle hndl) |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 570 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
| 571 | , fProps(SkSurfaceProps::kLegacyFontHost_InitType) |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 572 | , fAllocator(std::move(alloc)) |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 573 | { |
| 574 | inc_canvas(); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 575 | |
Mike Reed | 910ca0f | 2018-04-25 13:04:05 -0400 | [diff] [blame] | 576 | sk_sp<SkBaseDevice> device(new SkBitmapDevice(bitmap, fProps, hndl, nullptr)); |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 577 | this->init(device); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 580 | SkCanvas::SkCanvas(const SkBitmap& bitmap) : SkCanvas(bitmap, nullptr, nullptr) {} |
| 581 | |
Matt Sarett | 31f99ce | 2017-04-11 08:46:01 -0400 | [diff] [blame] | 582 | #ifdef SK_BUILD_FOR_ANDROID_FRAMEWORK |
| 583 | SkCanvas::SkCanvas(const SkBitmap& bitmap, ColorBehavior) |
| 584 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
| 585 | , fProps(SkSurfaceProps::kLegacyFontHost_InitType) |
| 586 | , fAllocator(nullptr) |
| 587 | { |
| 588 | inc_canvas(); |
| 589 | |
| 590 | SkBitmap tmp(bitmap); |
| 591 | *const_cast<SkImageInfo*>(&tmp.info()) = tmp.info().makeColorSpace(nullptr); |
Mike Reed | c247a4e | 2018-04-25 15:40:09 -0400 | [diff] [blame] | 592 | sk_sp<SkBaseDevice> device(new SkBitmapDevice(tmp, fProps, nullptr, nullptr)); |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 593 | this->init(device); |
Matt Sarett | 31f99ce | 2017-04-11 08:46:01 -0400 | [diff] [blame] | 594 | } |
| 595 | #endif |
| 596 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 597 | SkCanvas::~SkCanvas() { |
| 598 | // free up the contents of our deque |
| 599 | this->restoreToCount(1); // restore everything but the last |
reed@google.com | 7c20293 | 2011-12-14 18:48:05 +0000 | [diff] [blame] | 600 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 601 | this->internalRestore(); // restore the last, since we're going away |
| 602 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 603 | dec_canvas(); |
| 604 | } |
| 605 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 606 | /////////////////////////////////////////////////////////////////////////////// |
| 607 | |
junov@chromium.org | bf6c1e4 | 2012-01-30 14:53:22 +0000 | [diff] [blame] | 608 | void SkCanvas::flush() { |
reed | ea5a651 | 2016-07-07 16:44:27 -0700 | [diff] [blame] | 609 | this->onFlush(); |
| 610 | } |
| 611 | |
| 612 | void SkCanvas::onFlush() { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 613 | SkBaseDevice* device = this->getDevice(); |
junov@chromium.org | bf6c1e4 | 2012-01-30 14:53:22 +0000 | [diff] [blame] | 614 | if (device) { |
| 615 | device->flush(); |
| 616 | } |
| 617 | } |
| 618 | |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 619 | SkISize SkCanvas::getBaseLayerSize() const { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 620 | SkBaseDevice* d = this->getDevice(); |
reed@google.com | 210ce00 | 2011-11-01 14:24:23 +0000 | [diff] [blame] | 621 | return d ? SkISize::Make(d->width(), d->height()) : SkISize::Make(0, 0); |
| 622 | } |
| 623 | |
senorblanco | afc7cce | 2016-02-02 18:44:15 -0800 | [diff] [blame] | 624 | SkIRect SkCanvas::getTopLayerBounds() const { |
| 625 | SkBaseDevice* d = this->getTopDevice(); |
| 626 | if (!d) { |
| 627 | return SkIRect::MakeEmpty(); |
| 628 | } |
| 629 | return SkIRect::MakeXYWH(d->getOrigin().x(), d->getOrigin().y(), d->width(), d->height()); |
| 630 | } |
| 631 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 632 | SkBaseDevice* SkCanvas::getDevice() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 633 | // return root device |
robertphillips@google.com | c029062 | 2012-07-16 21:20:03 +0000 | [diff] [blame] | 634 | MCRec* rec = (MCRec*) fMCStack.front(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 635 | SkASSERT(rec && rec->fLayer); |
Florin Malita | 713b8ef | 2017-04-28 10:57:24 -0400 | [diff] [blame] | 636 | return rec->fLayer->fDevice.get(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Florin Malita | 0ed3b64 | 2017-01-13 16:56:38 +0000 | [diff] [blame] | 639 | SkBaseDevice* SkCanvas::getTopDevice() const { |
Florin Malita | 713b8ef | 2017-04-28 10:57:24 -0400 | [diff] [blame] | 640 | return fMCRec->fTopLayer->fDevice.get(); |
reed@google.com | 9266fed | 2011-03-30 00:18:03 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Mike Reed | 353196f | 2017-07-21 11:01:18 -0400 | [diff] [blame] | 643 | bool SkCanvas::readPixels(const SkPixmap& pm, int x, int y) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 644 | SkBaseDevice* device = this->getDevice(); |
Mike Reed | 353196f | 2017-07-21 11:01:18 -0400 | [diff] [blame] | 645 | return device && pm.addr() && device->readPixels(pm, x, y); |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Mike Reed | 353196f | 2017-07-21 11:01:18 -0400 | [diff] [blame] | 648 | bool SkCanvas::readPixels(const SkImageInfo& dstInfo, void* dstP, size_t rowBytes, int x, int y) { |
| 649 | return this->readPixels({ dstInfo, dstP, rowBytes}, x, y); |
Mike Reed | 12e946b | 2017-04-17 10:53:29 -0400 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | bool SkCanvas::readPixels(const SkBitmap& bm, int x, int y) { |
| 653 | SkPixmap pm; |
| 654 | return bm.peekPixels(&pm) && this->readPixels(pm, x, y); |
| 655 | } |
| 656 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 657 | bool SkCanvas::writePixels(const SkBitmap& bitmap, int x, int y) { |
Mike Reed | 4edb5d2 | 2017-04-17 11:02:51 -0400 | [diff] [blame] | 658 | SkPixmap pm; |
| 659 | if (bitmap.peekPixels(&pm)) { |
reed | cf01e31 | 2015-05-23 19:14:51 -0700 | [diff] [blame] | 660 | return this->writePixels(pm.info(), pm.addr(), pm.rowBytes(), x, y); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 661 | } |
| 662 | return false; |
| 663 | } |
| 664 | |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 665 | bool SkCanvas::writePixels(const SkImageInfo& srcInfo, const void* pixels, size_t rowBytes, |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 666 | int x, int y) { |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 667 | SkBaseDevice* device = this->getDevice(); |
| 668 | if (!device) { |
| 669 | return false; |
| 670 | } |
| 671 | |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 672 | // This check gives us an early out and prevents generation ID churn on the surface. |
| 673 | // This is purely optional: it is a subset of the checks performed by SkWritePixelsRec. |
| 674 | SkIRect srcRect = SkIRect::MakeXYWH(x, y, srcInfo.width(), srcInfo.height()); |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 675 | if (!srcRect.intersect({0, 0, device->width(), device->height()})) { |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 676 | return false; |
Matt Sarett | 26ecfe0 | 2017-01-23 15:51:01 +0000 | [diff] [blame] | 677 | } |
Matt Sarett | 26ecfe0 | 2017-01-23 15:51:01 +0000 | [diff] [blame] | 678 | |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 679 | // Tell our owning surface to bump its generation ID. |
| 680 | const bool completeOverwrite = |
| 681 | srcRect.size() == SkISize::Make(device->width(), device->height()); |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 682 | this->predrawNotify(completeOverwrite); |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 683 | |
Matt Sarett | 03dd6d5 | 2017-01-23 12:15:09 -0500 | [diff] [blame] | 684 | // This can still fail, most notably in the case of a invalid color type or alpha type |
| 685 | // conversion. We could pull those checks into this function and avoid the unnecessary |
| 686 | // generation ID bump. But then we would be performing those checks twice, since they |
| 687 | // are also necessary at the bitmap/pixmap entry points. |
Mike Reed | 353196f | 2017-07-21 11:01:18 -0400 | [diff] [blame] | 688 | return device->writePixels({srcInfo, pixels, rowBytes}, x, y); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 689 | } |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 690 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 691 | ////////////////////////////////////////////////////////////////////////////// |
| 692 | |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 693 | void SkCanvas::checkForDeferredSave() { |
| 694 | if (fMCRec->fDeferredSaveCount > 0) { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 695 | this->doSave(); |
| 696 | } |
| 697 | } |
| 698 | |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 699 | int SkCanvas::getSaveCount() const { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 700 | #ifdef SK_DEBUG |
| 701 | int count = 0; |
| 702 | SkDeque::Iter iter(fMCStack, SkDeque::Iter::kFront_IterStart); |
| 703 | for (;;) { |
| 704 | const MCRec* rec = (const MCRec*)iter.next(); |
| 705 | if (!rec) { |
| 706 | break; |
| 707 | } |
| 708 | count += 1 + rec->fDeferredSaveCount; |
| 709 | } |
| 710 | SkASSERT(count == fSaveCount); |
| 711 | #endif |
| 712 | return fSaveCount; |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | int SkCanvas::save() { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 716 | fSaveCount += 1; |
| 717 | fMCRec->fDeferredSaveCount += 1; |
| 718 | return this->getSaveCount() - 1; // return our prev value |
| 719 | } |
| 720 | |
| 721 | void SkCanvas::doSave() { |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 722 | this->willSave(); |
fmalita | a62d32d | 2015-04-28 08:08:57 -0700 | [diff] [blame] | 723 | |
| 724 | SkASSERT(fMCRec->fDeferredSaveCount > 0); |
| 725 | fMCRec->fDeferredSaveCount -= 1; |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 726 | this->internalSave(); |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | void SkCanvas::restore() { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 730 | if (fMCRec->fDeferredSaveCount > 0) { |
| 731 | SkASSERT(fSaveCount > 1); |
| 732 | fSaveCount -= 1; |
| 733 | fMCRec->fDeferredSaveCount -= 1; |
| 734 | } else { |
| 735 | // check for underflow |
| 736 | if (fMCStack.count() > 1) { |
| 737 | this->willRestore(); |
| 738 | SkASSERT(fSaveCount > 1); |
reed | a644116 | 2015-03-26 13:40:09 -0700 | [diff] [blame] | 739 | fSaveCount -= 1; |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 740 | this->internalRestore(); |
| 741 | this->didRestore(); |
| 742 | } |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | |
| 746 | void SkCanvas::restoreToCount(int count) { |
| 747 | // sanity check |
| 748 | if (count < 1) { |
| 749 | count = 1; |
| 750 | } |
mtklein | f0f1411 | 2014-12-12 08:46:25 -0800 | [diff] [blame] | 751 | |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 752 | int n = this->getSaveCount() - count; |
| 753 | for (int i = 0; i < n; ++i) { |
| 754 | this->restore(); |
| 755 | } |
| 756 | } |
| 757 | |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 758 | void SkCanvas::internalSave() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 759 | MCRec* newTop = (MCRec*)fMCStack.push_back(); |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 760 | new (newTop) MCRec(*fMCRec); // balanced in restore() |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 761 | fMCRec = newTop; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 762 | |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 763 | FOR_EACH_TOP_DEVICE(device->save()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 764 | } |
| 765 | |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 766 | bool SkCanvas::BoundsAffectsClip(SaveLayerFlags saveLayerFlags) { |
Cary Clark | 7eddfb8 | 2018-03-13 14:41:10 -0400 | [diff] [blame] | 767 | return !(saveLayerFlags & SkCanvasPriv::kDontClipToLayer_SaveLayerFlag); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 768 | } |
| 769 | |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 770 | bool SkCanvas::clipRectBounds(const SkRect* bounds, SaveLayerFlags saveLayerFlags, |
reed | 9b3aa54 | 2015-03-11 08:47:12 -0700 | [diff] [blame] | 771 | SkIRect* intersection, const SkImageFilter* imageFilter) { |
Michael Ludwig | aa861a1 | 2019-07-19 10:13:47 -0400 | [diff] [blame] | 772 | // clipRectBounds() is called to determine the input layer size needed for a given image filter. |
| 773 | // The coordinate space of the rectangle passed to filterBounds(kReverse) is meant to be in the |
| 774 | // filtering layer space. Here, 'clipBounds' is always in the true device space. When an image |
| 775 | // filter does not require a decomposed CTM matrix, the filter space and device space are the |
| 776 | // same. When it has been decomposed, we want the original image filter node to process the |
| 777 | // bounds in the layer space represented by the decomposed scale matrix. 'imageFilter' is no |
| 778 | // longer the original filter, but has the remainder matrix baked into it, and passing in the |
| 779 | // the true device clip bounds ensures that the matrix image filter provides a layer clip bounds |
| 780 | // to the original filter node (barring inflation from consecutive calls to mapRect). While |
| 781 | // initially counter-intuitive given the apparent inconsistency of coordinate spaces, always |
| 782 | // passing getDeviceClipBounds() to 'imageFilter' is correct. |
| 783 | // FIXME (michaelludwig) - When the remainder matrix is instead applied as a final draw, it will |
| 784 | // be important to more accurately calculate the clip bounds in the layer space for the original |
| 785 | // image filter (similar to how matrix image filter does it, but ideally without the inflation). |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 786 | SkIRect clipBounds = this->getDeviceClipBounds(); |
| 787 | if (clipBounds.isEmpty()) { |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 788 | return false; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 789 | } |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 790 | |
reed | 96e657d | 2015-03-10 17:30:07 -0700 | [diff] [blame] | 791 | const SkMatrix& ctm = fMCRec->fMatrix; // this->getTotalMatrix() |
| 792 | |
Robert Phillips | 1207843 | 2018-05-17 11:17:39 -0400 | [diff] [blame] | 793 | if (imageFilter && bounds && !imageFilter->canComputeFastBounds()) { |
| 794 | // If the image filter DAG affects transparent black then we will need to render |
| 795 | // out to the clip bounds |
| 796 | bounds = nullptr; |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 797 | } |
Robert Phillips | 1207843 | 2018-05-17 11:17:39 -0400 | [diff] [blame] | 798 | |
| 799 | SkIRect inputSaveLayerBounds; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 800 | if (bounds) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 801 | SkRect r; |
reed | 96e657d | 2015-03-10 17:30:07 -0700 | [diff] [blame] | 802 | ctm.mapRect(&r, *bounds); |
Robert Phillips | 1207843 | 2018-05-17 11:17:39 -0400 | [diff] [blame] | 803 | r.roundOut(&inputSaveLayerBounds); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 804 | } else { // no user bounds, so just use the clip |
Robert Phillips | 1207843 | 2018-05-17 11:17:39 -0400 | [diff] [blame] | 805 | inputSaveLayerBounds = clipBounds; |
| 806 | } |
| 807 | |
| 808 | if (imageFilter) { |
| 809 | // expand the clip bounds by the image filter DAG to include extra content that might |
| 810 | // be required by the image filters. |
| 811 | clipBounds = imageFilter->filterBounds(clipBounds, ctm, |
| 812 | SkImageFilter::kReverse_MapDirection, |
| 813 | &inputSaveLayerBounds); |
| 814 | } |
| 815 | |
| 816 | SkIRect clippedSaveLayerBounds; |
| 817 | if (bounds) { |
| 818 | // For better or for worse, user bounds currently act as a hard clip on the layer's |
| 819 | // extent (i.e., they implement the CSS filter-effects 'filter region' feature). |
| 820 | clippedSaveLayerBounds = inputSaveLayerBounds; |
| 821 | } else { |
| 822 | // If there are no user bounds, we don't want to artificially restrict the resulting |
| 823 | // layer bounds, so allow the expanded clip bounds free reign. |
| 824 | clippedSaveLayerBounds = clipBounds; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 825 | } |
Xianzhu Wang | 3b428cb | 2018-01-22 09:51:13 -0800 | [diff] [blame] | 826 | |
| 827 | // early exit if the layer's bounds are clipped out |
Robert Phillips | 1207843 | 2018-05-17 11:17:39 -0400 | [diff] [blame] | 828 | if (!clippedSaveLayerBounds.intersect(clipBounds)) { |
Xianzhu Wang | 3b428cb | 2018-01-22 09:51:13 -0800 | [diff] [blame] | 829 | if (BoundsAffectsClip(saveLayerFlags)) { |
| 830 | fMCRec->fTopLayer->fDevice->clipRegion(SkRegion(), SkClipOp::kIntersect); // empty |
| 831 | fMCRec->fRasterClip.setEmpty(); |
| 832 | fDeviceClipBounds.setEmpty(); |
| 833 | } |
| 834 | return false; |
| 835 | } |
Robert Phillips | 1207843 | 2018-05-17 11:17:39 -0400 | [diff] [blame] | 836 | SkASSERT(!clippedSaveLayerBounds.isEmpty()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 837 | |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 838 | if (BoundsAffectsClip(saveLayerFlags)) { |
reed | 180aec4 | 2015-03-11 10:39:04 -0700 | [diff] [blame] | 839 | // Simplify the current clips since they will be applied properly during restore() |
Robert Phillips | 1207843 | 2018-05-17 11:17:39 -0400 | [diff] [blame] | 840 | fMCRec->fRasterClip.setRect(clippedSaveLayerBounds); |
| 841 | fDeviceClipBounds = qr_clip_bounds(clippedSaveLayerBounds); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | if (intersection) { |
Robert Phillips | 1207843 | 2018-05-17 11:17:39 -0400 | [diff] [blame] | 845 | *intersection = clippedSaveLayerBounds; |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 846 | } |
Robert Phillips | 1207843 | 2018-05-17 11:17:39 -0400 | [diff] [blame] | 847 | |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 848 | return true; |
| 849 | } |
| 850 | |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 851 | int SkCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint) { |
| 852 | return this->saveLayer(SaveLayerRec(bounds, paint, 0)); |
commit-bot@chromium.org | d70fa20 | 2014-04-24 21:51:58 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Cary Clark | e041e31 | 2018-03-06 13:00:52 -0500 | [diff] [blame] | 855 | int SkCanvas::saveLayer(const SaveLayerRec& rec) { |
Yuqian Li | f7c723c | 2018-08-29 16:25:47 -0700 | [diff] [blame] | 856 | TRACE_EVENT0("skia", TRACE_FUNC); |
Mike Reed | 49f8da0 | 2018-08-27 10:48:52 -0400 | [diff] [blame] | 857 | if (rec.fPaint && rec.fPaint->nothingToDraw()) { |
| 858 | // no need for the layer (or any of the draws until the matching restore() |
| 859 | this->save(); |
| 860 | this->clipRect({0,0,0,0}); |
| 861 | } else { |
| 862 | SaveLayerStrategy strategy = this->getSaveLayerStrategy(rec); |
| 863 | fSaveCount += 1; |
| 864 | this->internalSaveLayer(rec, strategy); |
| 865 | } |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 866 | return this->getSaveCount() - 1; |
reed | 70ee31b | 2015-12-10 13:44:45 -0800 | [diff] [blame] | 867 | } |
| 868 | |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 869 | int SkCanvas::only_axis_aligned_saveBehind(const SkRect* bounds) { |
| 870 | if (bounds && !this->getLocalClipBounds().intersects(*bounds)) { |
| 871 | // Assuming clips never expand, if the request bounds is outside of the current clip |
| 872 | // there is no need to copy/restore the area, so just devolve back to a regular save. |
| 873 | this->save(); |
| 874 | } else { |
| 875 | bool doTheWork = this->onDoSaveBehind(bounds); |
| 876 | fSaveCount += 1; |
| 877 | this->internalSave(); |
| 878 | if (doTheWork) { |
| 879 | this->internalSaveBehind(bounds); |
| 880 | } |
| 881 | } |
| 882 | return this->getSaveCount() - 1; |
| 883 | } |
| 884 | |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 885 | void SkCanvas::DrawDeviceWithFilter(SkBaseDevice* src, const SkImageFilter* filter, |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 886 | SkBaseDevice* dst, const SkIPoint& dstOrigin, |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 887 | const SkMatrix& ctm) { |
Michael Ludwig | b6e8922 | 2019-09-05 13:10:21 -0400 | [diff] [blame] | 888 | // The local bounds of the src device; all the bounds passed to snapSpecial must be intersected |
| 889 | // with this rect. |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 890 | const SkIRect srcDevRect = SkIRect::MakeWH(src->width(), src->height()); |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 891 | |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 892 | if (!filter) { |
Michael Ludwig | b6e8922 | 2019-09-05 13:10:21 -0400 | [diff] [blame] | 893 | // All non-filtered devices are currently axis aligned, so they only differ by their origin. |
| 894 | // This means that we only have to copy a dst-sized block of pixels out of src and translate |
| 895 | // it to the matching position relative to dst's origin. |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 896 | SkIRect snapBounds = SkIRect::MakeXYWH(dstOrigin.x() - src->getOrigin().x(), |
| 897 | dstOrigin.y() - src->getOrigin().y(), |
| 898 | dst->width(), dst->height()); |
| 899 | if (!snapBounds.intersect(srcDevRect)) { |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 900 | return; |
| 901 | } |
| 902 | |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 903 | auto special = src->snapSpecial(snapBounds); |
| 904 | if (special) { |
Michael Ludwig | b6e8922 | 2019-09-05 13:10:21 -0400 | [diff] [blame] | 905 | // The image is drawn at 1-1 scale with integer translation, so no filtering is needed. |
| 906 | SkPaint p; |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 907 | dst->drawSpecial(special.get(), 0, 0, p, nullptr, SkMatrix::I()); |
| 908 | } |
| 909 | return; |
Michael Ludwig | 7d2ad0f | 2019-05-03 17:09:47 -0400 | [diff] [blame] | 910 | } |
| 911 | |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 912 | // First decompose the ctm into a post-filter transform and a filter matrix that is supported |
| 913 | // by the backdrop filter. |
| 914 | SkMatrix toRoot, layerMatrix; |
| 915 | SkSize scale; |
| 916 | if (ctm.isScaleTranslate() || as_IFB(filter)->canHandleComplexCTM()) { |
| 917 | toRoot = SkMatrix::I(); |
| 918 | layerMatrix = ctm; |
| 919 | } else if (ctm.decomposeScale(&scale, &toRoot)) { |
| 920 | layerMatrix = SkMatrix::MakeScale(scale.fWidth, scale.fHeight); |
| 921 | } else { |
| 922 | // Perspective, for now, do no scaling of the layer itself. |
| 923 | // TODO (michaelludwig) - perhaps it'd be better to explore a heuristic scale pulled from |
| 924 | // the matrix, e.g. based on the midpoint of the near/far planes? |
| 925 | toRoot = ctm; |
| 926 | layerMatrix = SkMatrix::I(); |
| 927 | } |
| 928 | |
| 929 | // We have to map the dst bounds from the root space into the layer space where filtering will |
| 930 | // occur. If we knew the input bounds of the content that defined the original dst bounds, we |
| 931 | // could map that forward by layerMatrix and have tighter bounds, but toRoot^-1 * dst bounds |
| 932 | // is a safe, conservative estimate. |
| 933 | SkMatrix fromRoot; |
| 934 | if (!toRoot.invert(&fromRoot)) { |
| 935 | return; |
| 936 | } |
| 937 | |
| 938 | // This represents what the backdrop filter needs to produce in the layer space, and is sized |
| 939 | // such that drawing it into dst with the toRoot transform will cover the actual dst device. |
| 940 | SkIRect layerTargetBounds = fromRoot.mapRect( |
| 941 | SkRect::MakeXYWH(dstOrigin.x(), dstOrigin.y(), dst->width(), dst->height())).roundOut(); |
| 942 | // While layerTargetBounds is what needs to be output by the filter, the filtering process may |
| 943 | // require some extra input pixels. |
| 944 | SkIRect layerInputBounds = filter->filterBounds( |
| 945 | layerTargetBounds, layerMatrix, SkImageFilter::kReverse_MapDirection, |
| 946 | &layerTargetBounds); |
| 947 | |
| 948 | // Map the required input into the root space, then make relative to the src device. This will |
Michael Ludwig | b6e8922 | 2019-09-05 13:10:21 -0400 | [diff] [blame] | 949 | // be the conservative contents required to fill a layerInputBounds-sized surface with the |
| 950 | // backdrop content (transformed back into the layer space using fromRoot). |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 951 | SkIRect backdropBounds = toRoot.mapRect(SkRect::Make(layerInputBounds)).roundOut(); |
| 952 | backdropBounds.offset(-src->getOrigin().x(), -src->getOrigin().y()); |
| 953 | if (!backdropBounds.intersect(srcDevRect)) { |
| 954 | return; |
| 955 | } |
| 956 | |
| 957 | auto special = src->snapSpecial(backdropBounds); |
| 958 | if (!special) { |
| 959 | return; |
| 960 | } |
| 961 | |
| 962 | SkColorType colorType = src->imageInfo().colorType(); |
| 963 | if (colorType == kUnknown_SkColorType) { |
| 964 | colorType = kRGBA_8888_SkColorType; |
| 965 | } |
| 966 | SkColorSpace* colorSpace = src->imageInfo().colorSpace(); |
Michael Ludwig | b6e8922 | 2019-09-05 13:10:21 -0400 | [diff] [blame] | 967 | |
| 968 | SkPaint p; |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 969 | if (!toRoot.isIdentity()) { |
Michael Ludwig | b6e8922 | 2019-09-05 13:10:21 -0400 | [diff] [blame] | 970 | // Drawing the temporary and final filtered image requires a higher filter quality if the |
| 971 | // 'toRoot' transformation is not identity, in order to minimize the impact on already |
| 972 | // rendered edges/content. |
| 973 | // TODO (michaelludwig) - Explore reducing this quality, identify visual tradeoffs |
| 974 | p.setFilterQuality(kHigh_SkFilterQuality); |
| 975 | |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 976 | // The snapped backdrop content needs to be transformed by fromRoot into the layer space, |
| 977 | // and stored in a temporary surface, which is then used as the input to the actual filter. |
| 978 | auto tmpSurface = special->makeSurface(colorType, colorSpace, layerInputBounds.size()); |
| 979 | if (!tmpSurface) { |
| 980 | return; |
| 981 | } |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 982 | |
| 983 | auto tmpCanvas = tmpSurface->getCanvas(); |
| 984 | tmpCanvas->clear(SK_ColorTRANSPARENT); |
| 985 | // Reading in reverse, this takes the backdrop bounds from src device space into the root |
| 986 | // space, then maps from root space into the layer space, then maps it so the input layer's |
| 987 | // top left corner is (0, 0). This transformation automatically accounts for any cropping |
| 988 | // performed on backdropBounds. |
| 989 | tmpCanvas->translate(-layerInputBounds.fLeft, -layerInputBounds.fTop); |
| 990 | tmpCanvas->concat(fromRoot); |
| 991 | tmpCanvas->translate(src->getOrigin().x(), src->getOrigin().y()); |
Michael Ludwig | b6e8922 | 2019-09-05 13:10:21 -0400 | [diff] [blame] | 992 | |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 993 | tmpCanvas->drawImageRect(special->asImage(), special->subset(), |
| 994 | SkRect::Make(backdropBounds), &p, kStrict_SrcRectConstraint); |
| 995 | special = tmpSurface->makeImageSnapshot(); |
| 996 | } else { |
| 997 | // Since there is no extra transform that was done, update the input bounds to reflect |
| 998 | // cropping of the snapped backdrop image. In this case toRoot = I, so layerInputBounds |
| 999 | // was equal to backdropBounds before it was made relative to the src device and cropped. |
| 1000 | // When we use the original snapped image directly, just map the update backdrop bounds |
| 1001 | // back into the shared layer space |
| 1002 | layerInputBounds = backdropBounds; |
| 1003 | layerInputBounds.offset(src->getOrigin().x(), src->getOrigin().y()); |
Michael Ludwig | b6e8922 | 2019-09-05 13:10:21 -0400 | [diff] [blame] | 1004 | |
| 1005 | // Similar to the unfiltered case above, when toRoot is the identity, then the final |
| 1006 | // draw will be 1-1 so there is no need to increase filter quality. |
| 1007 | p.setFilterQuality(kNone_SkFilterQuality); |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | // Now evaluate the filter on 'special', which contains the backdrop content mapped back into |
| 1011 | // layer space. This has to further offset everything so that filter evaluation thinks the |
| 1012 | // source image's top left corner is (0, 0). |
| 1013 | // TODO (michaelludwig) - Once image filters are robust to non-(0,0) image origins for inputs, |
| 1014 | // this can be simplified. |
| 1015 | layerTargetBounds.offset(-layerInputBounds.fLeft, -layerInputBounds.fTop); |
| 1016 | SkMatrix filterCTM = layerMatrix; |
| 1017 | filterCTM.postTranslate(-layerInputBounds.fLeft, -layerInputBounds.fTop); |
| 1018 | skif::Context ctx(filterCTM, layerTargetBounds, nullptr, colorType, colorSpace, special.get()); |
| 1019 | |
| 1020 | SkIPoint offset; |
| 1021 | special = as_IFB(filter)->filterImage(ctx).imageAndOffset(&offset); |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 1022 | if (special) { |
Michael Ludwig | b6e8922 | 2019-09-05 13:10:21 -0400 | [diff] [blame] | 1023 | // Draw the filtered backdrop content into the dst device. We add layerInputBounds origin |
| 1024 | // to offset because the original value in 'offset' was relative to 'filterCTM'. 'filterCTM' |
| 1025 | // had subtracted the layerInputBounds origin, so adding that back makes 'offset' relative |
| 1026 | // to 'layerMatrix' (what we need it to be when drawing the image by 'toRoot'). |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 1027 | offset += layerInputBounds.topLeft(); |
| 1028 | |
| 1029 | // Manually setting the device's CTM requires accounting for the device's origin. |
| 1030 | // TODO (michaelludwig) - This could be simpler if the dst device had its origin configured |
| 1031 | // before filtering the backdrop device, and if SkAutoDeviceCTMRestore had a way to accept |
| 1032 | // a global CTM instead of a device CTM. |
| 1033 | SkMatrix dstCTM = toRoot; |
| 1034 | dstCTM.postTranslate(-dstOrigin.x(), -dstOrigin.y()); |
| 1035 | SkAutoDeviceCTMRestore acr(dst, dstCTM); |
| 1036 | |
| 1037 | // And because devices don't have a special-image draw function that supports arbitrary |
| 1038 | // matrices, we are abusing the asImage() functionality here... |
| 1039 | SkRect specialSrc = SkRect::Make(special->subset()); |
Michael Ludwig | b6e8922 | 2019-09-05 13:10:21 -0400 | [diff] [blame] | 1040 | auto looseImage = special->asImage(); |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 1041 | dst->drawImageRect( |
Michael Ludwig | b6e8922 | 2019-09-05 13:10:21 -0400 | [diff] [blame] | 1042 | looseImage.get(), &specialSrc, |
Michael Ludwig | 6d1c0d4 | 2019-09-04 17:39:42 -0400 | [diff] [blame] | 1043 | SkRect::MakeXYWH(offset.x(), offset.y(), special->width(), special->height()), |
| 1044 | p, kStrict_SrcRectConstraint); |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 1045 | } |
robertphillips | 7354a4b | 2015-12-16 05:08:27 -0800 | [diff] [blame] | 1046 | } |
reed | 70ee31b | 2015-12-10 13:44:45 -0800 | [diff] [blame] | 1047 | |
Mike Klein | e083f7c | 2018-02-07 12:54:27 -0500 | [diff] [blame] | 1048 | static SkImageInfo make_layer_info(const SkImageInfo& prev, int w, int h, const SkPaint* paint) { |
Mike Klein | 649fb73 | 2018-02-26 15:09:16 -0500 | [diff] [blame] | 1049 | SkColorType ct = prev.colorType(); |
Brian Osman | d7a5975 | 2019-09-20 11:16:58 -0400 | [diff] [blame^] | 1050 | if (prev.bytesPerPixel() <= 4 && |
| 1051 | prev.colorType() != kRGBA_8888_SkColorType && |
| 1052 | prev.colorType() != kBGRA_8888_SkColorType) { |
Mike Klein | 649fb73 | 2018-02-26 15:09:16 -0500 | [diff] [blame] | 1053 | // "Upgrade" A8, G8, 565, 4444, 1010102, 101010x, and 888x to 8888, |
| 1054 | // ensuring plenty of alpha bits for the layer, perhaps losing some color bits in return. |
| 1055 | ct = kN32_SkColorType; |
| 1056 | } |
| 1057 | return SkImageInfo::Make(w, h, ct, kPremul_SkAlphaType, prev.refColorSpace()); |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 1058 | } |
| 1059 | |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 1060 | void SkCanvas::internalSaveLayer(const SaveLayerRec& rec, SaveLayerStrategy strategy) { |
Yuqian Li | f7c723c | 2018-08-29 16:25:47 -0700 | [diff] [blame] | 1061 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 1062 | const SkRect* bounds = rec.fBounds; |
| 1063 | const SkPaint* paint = rec.fPaint; |
| 1064 | SaveLayerFlags saveLayerFlags = rec.fSaveLayerFlags; |
| 1065 | |
Mike Reed | 5532c2a | 2019-02-23 12:00:32 -0500 | [diff] [blame] | 1066 | // If we have a backdrop filter, then we must apply it to the entire layer (clip-bounds) |
| 1067 | // regardless of any hint-rect from the caller. skbug.com/8783 |
| 1068 | if (rec.fBackdrop) { |
| 1069 | bounds = nullptr; |
| 1070 | } |
| 1071 | |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1072 | SkLazyPaint lazyP; |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 1073 | SkImageFilter* imageFilter = paint ? paint->getImageFilter() : nullptr; |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1074 | SkMatrix stashedMatrix = fMCRec->fMatrix; |
Robert Phillips | 3d0e850 | 2018-04-20 10:27:27 -0400 | [diff] [blame] | 1075 | MCRec* modifiedRec = nullptr; |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 1076 | |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1077 | /* |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 1078 | * Many ImageFilters (so far) do not (on their own) correctly handle matrices (CTM) that |
| 1079 | * contain rotation/skew/etc. We rely on applyCTM to create a new image filter DAG as needed to |
| 1080 | * accommodate this, but it requires update the CTM we use when drawing into the layer. |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1081 | * |
| 1082 | * 1. Stash off the current CTM |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 1083 | * 2. Apply the CTM to imagefilter, which decomposes it into simple and complex transforms |
| 1084 | * if necessary. |
| 1085 | * 3. Wack the CTM to be the remaining scale matrix and use the modified imagefilter, which |
| 1086 | * is a MatrixImageFilter that contains the complex matrix. |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1087 | * 4. Proceed as usual, allowing the client to draw into the layer (now with a scale-only CTM) |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 1088 | * 5. During restore, the MatrixImageFilter automatically applies complex stage to the output |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1089 | * of the original imagefilter, and draw that (via drawSprite) |
| 1090 | * 6. Unwack the CTM to its original state (i.e. stashedMatrix) |
| 1091 | * |
| 1092 | * Perhaps in the future we could augment #5 to apply REMAINDER as part of the draw (no longer |
| 1093 | * a sprite operation) to avoid the extra buffer/overhead of MatrixImageFilter. |
| 1094 | */ |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 1095 | if (imageFilter) { |
| 1096 | SkMatrix modifiedCTM; |
Michael Ludwig | 8ee6cf3 | 2019-08-02 09:57:04 -0400 | [diff] [blame] | 1097 | sk_sp<SkImageFilter> modifiedFilter = as_IFB(imageFilter)->applyCTM(stashedMatrix, |
| 1098 | &modifiedCTM); |
| 1099 | if (as_IFB(modifiedFilter)->uniqueID() != as_IFB(imageFilter)->uniqueID()) { |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 1100 | // The original filter couldn't support the CTM entirely |
Michael Ludwig | 8ee6cf3 | 2019-08-02 09:57:04 -0400 | [diff] [blame] | 1101 | SkASSERT(modifiedCTM.isScaleTranslate() || as_IFB(imageFilter)->canHandleComplexCTM()); |
Michael Ludwig | 08b260c | 2019-05-17 11:21:53 -0400 | [diff] [blame] | 1102 | modifiedRec = fMCRec; |
| 1103 | this->internalSetMatrix(modifiedCTM); |
| 1104 | SkPaint* p = lazyP.set(*paint); |
| 1105 | p->setImageFilter(std::move(modifiedFilter)); |
| 1106 | imageFilter = p->getImageFilter(); |
| 1107 | paint = p; |
| 1108 | } |
| 1109 | // Else the filter didn't change, so modifiedCTM == stashedMatrix and there's nothing |
| 1110 | // left to do since the stack already has that as the CTM. |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1111 | } |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1112 | |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 1113 | // do this before we create the layer. We don't call the public save() since |
| 1114 | // that would invoke a possibly overridden virtual |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1115 | this->internalSave(); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 1116 | |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 1117 | SkIRect ir; |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1118 | if (!this->clipRectBounds(bounds, saveLayerFlags, &ir, imageFilter)) { |
Robert Phillips | 3d0e850 | 2018-04-20 10:27:27 -0400 | [diff] [blame] | 1119 | if (modifiedRec) { |
| 1120 | // In this case there will be no layer in which to stash the matrix so we need to |
| 1121 | // revert the prior MCRec to its earlier state. |
| 1122 | modifiedRec->fMatrix = stashedMatrix; |
| 1123 | } |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1124 | return; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 1127 | // FIXME: do willSaveLayer() overriders returning kNoLayer_SaveLayerStrategy really care about |
| 1128 | // the clipRectBounds() call above? |
| 1129 | if (kNoLayer_SaveLayerStrategy == strategy) { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1130 | return; |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
reed | 8dc0ccb | 2015-03-20 06:32:52 -0700 | [diff] [blame] | 1133 | SkPixelGeometry geo = fProps.pixelGeometry(); |
| 1134 | if (paint) { |
reed | 76033be | 2015-03-14 10:54:31 -0700 | [diff] [blame] | 1135 | // TODO: perhaps add a query to filters so we might preserve opaqueness... |
reed | daa57bf | 2015-05-15 10:39:17 -0700 | [diff] [blame] | 1136 | if (paint->getImageFilter() || paint->getColorFilter()) { |
reed | 8dc0ccb | 2015-03-20 06:32:52 -0700 | [diff] [blame] | 1137 | geo = kUnknown_SkPixelGeometry; |
reed@google.com | b55deeb | 2012-01-06 14:43:09 +0000 | [diff] [blame] | 1138 | } |
| 1139 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1140 | |
robertphillips | 5139e50 | 2016-07-19 05:10:40 -0700 | [diff] [blame] | 1141 | SkBaseDevice* priorDevice = this->getTopDevice(); |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 1142 | if (nullptr == priorDevice) { // Do we still need this check??? |
reed | b2db898 | 2014-11-13 12:41:02 -0800 | [diff] [blame] | 1143 | SkDebugf("Unable to find device for layer."); |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1144 | return; |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1145 | } |
reed | b2db898 | 2014-11-13 12:41:02 -0800 | [diff] [blame] | 1146 | |
Mike Klein | e083f7c | 2018-02-07 12:54:27 -0500 | [diff] [blame] | 1147 | SkImageInfo info = make_layer_info(priorDevice->imageInfo(), ir.width(), ir.height(), paint); |
Mike Reed | 1f3548c | 2019-07-12 12:53:11 -0400 | [diff] [blame] | 1148 | if (rec.fSaveLayerFlags & kF16ColorType) { |
| 1149 | info = info.makeColorType(kRGBA_F16_SkColorType); |
| 1150 | } |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 1151 | |
Hal Canary | 704cd32 | 2016-11-07 14:13:52 -0500 | [diff] [blame] | 1152 | sk_sp<SkBaseDevice> newDevice; |
reed | 61f501f | 2015-04-29 08:34:00 -0700 | [diff] [blame] | 1153 | { |
Florin Malita | 4571e49 | 2019-07-16 10:25:58 -0400 | [diff] [blame] | 1154 | SkASSERT(info.alphaType() != kOpaque_SkAlphaType); |
reed | daa57bf | 2015-05-15 10:39:17 -0700 | [diff] [blame] | 1155 | const SkBaseDevice::TileUsage usage = SkBaseDevice::kNever_TileUsage; |
Herb Derby | 41f4f31 | 2018-06-06 17:45:53 +0000 | [diff] [blame] | 1156 | const bool trackCoverage = |
| 1157 | SkToBool(saveLayerFlags & kMaskAgainstCoverage_EXPERIMENTAL_DONT_USE_SaveLayerFlag); |
reed | 70ee31b | 2015-12-10 13:44:45 -0800 | [diff] [blame] | 1158 | const SkBaseDevice::CreateInfo createInfo = SkBaseDevice::CreateInfo(info, usage, geo, |
Mike Reed | 910ca0f | 2018-04-25 13:04:05 -0400 | [diff] [blame] | 1159 | trackCoverage, |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 1160 | fAllocator.get()); |
robertphillips | 5139e50 | 2016-07-19 05:10:40 -0700 | [diff] [blame] | 1161 | newDevice.reset(priorDevice->onCreateDevice(createInfo, paint)); |
| 1162 | if (!newDevice) { |
reed | 7503d60 | 2016-07-15 14:23:29 -0700 | [diff] [blame] | 1163 | return; |
reed | 61f501f | 2015-04-29 08:34:00 -0700 | [diff] [blame] | 1164 | } |
bungeman@google.com | e25c684 | 2011-08-17 14:53:54 +0000 | [diff] [blame] | 1165 | } |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 1166 | DeviceCM* layer = new DeviceCM(newDevice, paint, stashedMatrix, rec.fClipMask, rec.fClipMatrix); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1167 | |
Mike Reed | b43a3e0 | 2017-02-11 10:18:58 -0500 | [diff] [blame] | 1168 | // only have a "next" if this new layer doesn't affect the clip (rare) |
| 1169 | layer->fNext = BoundsAffectsClip(saveLayerFlags) ? nullptr : fMCRec->fTopLayer; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1170 | fMCRec->fLayer = layer; |
| 1171 | fMCRec->fTopLayer = layer; // this field is NOT an owner of layer |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 1172 | |
Mike Reed | c61abee | 2017-02-28 17:45:27 -0500 | [diff] [blame] | 1173 | if ((rec.fSaveLayerFlags & kInitWithPrevious_SaveLayerFlag) || rec.fBackdrop) { |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 1174 | DrawDeviceWithFilter(priorDevice, rec.fBackdrop, newDevice.get(), { ir.fLeft, ir.fTop }, |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1175 | fMCRec->fMatrix); |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 1176 | } |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 1177 | |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 1178 | newDevice->setOrigin(fMCRec->fMatrix, ir.fLeft, ir.fTop); |
| 1179 | |
| 1180 | newDevice->androidFramework_setDeviceClipRestriction(&fClipRestrictionRect); |
| 1181 | if (layer->fNext) { |
| 1182 | // need to punch a hole in the previous device, so we don't draw there, given that |
| 1183 | // the new top-layer will allow drawing to happen "below" it. |
| 1184 | SkRegion hole(ir); |
| 1185 | do { |
| 1186 | layer = layer->fNext; |
| 1187 | layer->fDevice->clipRegion(hole, SkClipOp::kDifference); |
| 1188 | } while (layer->fNext); |
| 1189 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1190 | } |
| 1191 | |
commit-bot@chromium.org | d70fa20 | 2014-04-24 21:51:58 +0000 | [diff] [blame] | 1192 | int SkCanvas::saveLayerAlpha(const SkRect* bounds, U8CPU alpha) { |
reed | bada188 | 2015-12-21 13:09:44 -0800 | [diff] [blame] | 1193 | if (0xFF == alpha) { |
| 1194 | return this->saveLayer(bounds, nullptr); |
| 1195 | } else { |
| 1196 | SkPaint tmpPaint; |
| 1197 | tmpPaint.setAlpha(alpha); |
| 1198 | return this->saveLayer(bounds, &tmpPaint); |
| 1199 | } |
commit-bot@chromium.org | d70fa20 | 2014-04-24 21:51:58 +0000 | [diff] [blame] | 1200 | } |
| 1201 | |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 1202 | void SkCanvas::internalSaveBehind(const SkRect* localBounds) { |
| 1203 | SkIRect devBounds; |
| 1204 | if (localBounds) { |
| 1205 | SkRect tmp; |
| 1206 | fMCRec->fMatrix.mapRect(&tmp, *localBounds); |
| 1207 | if (!devBounds.intersect(tmp.round(), this->getDeviceClipBounds())) { |
| 1208 | devBounds.setEmpty(); |
| 1209 | } |
| 1210 | } else { |
| 1211 | devBounds = this->getDeviceClipBounds(); |
| 1212 | } |
| 1213 | if (devBounds.isEmpty()) { |
| 1214 | return; |
| 1215 | } |
| 1216 | |
| 1217 | SkBaseDevice* device = this->getTopDevice(); |
| 1218 | if (nullptr == device) { // Do we still need this check??? |
| 1219 | return; |
| 1220 | } |
| 1221 | |
| 1222 | // need the bounds relative to the device itself |
| 1223 | devBounds.offset(-device->fOrigin.fX, -device->fOrigin.fY); |
| 1224 | |
Michael Ludwig | ac35212 | 2019-08-28 21:03:05 +0000 | [diff] [blame] | 1225 | // This is getting the special image from the current device, which is then drawn into (both by |
| 1226 | // a client, and the drawClippedToSaveBehind below). Since this is not saving a layer, with its |
| 1227 | // own device, we need to explicitly copy the back image contents so that its original content |
| 1228 | // is available when we splat it back later during restore. |
| 1229 | auto backImage = device->snapSpecial(devBounds, /* copy */ true); |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 1230 | if (!backImage) { |
| 1231 | return; |
| 1232 | } |
| 1233 | |
| 1234 | // we really need the save, so we can wack the fMCRec |
| 1235 | this->checkForDeferredSave(); |
| 1236 | |
| 1237 | fMCRec->fBackImage.reset(new BackImage{std::move(backImage), devBounds.topLeft()}); |
| 1238 | |
| 1239 | SkPaint paint; |
| 1240 | paint.setBlendMode(SkBlendMode::kClear); |
Mike Reed | 9adc82c | 2019-04-23 10:28:13 -0400 | [diff] [blame] | 1241 | this->drawClippedToSaveBehind(paint); |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 1242 | } |
| 1243 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1244 | void SkCanvas::internalRestore() { |
| 1245 | SkASSERT(fMCStack.count() != 0); |
| 1246 | |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 1247 | // reserve our layer (if any) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1248 | DeviceCM* layer = fMCRec->fLayer; // may be null |
| 1249 | // now detach it from fMCRec so we can pop(). Gets freed after its drawn |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1250 | fMCRec->fLayer = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1251 | |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 1252 | // move this out before we do the actual restore |
| 1253 | auto backImage = std::move(fMCRec->fBackImage); |
| 1254 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1255 | // now do the normal restore() |
| 1256 | fMCRec->~MCRec(); // balanced in save() |
| 1257 | fMCStack.pop_back(); |
| 1258 | fMCRec = (MCRec*)fMCStack.back(); |
| 1259 | |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 1260 | if (fMCRec) { |
| 1261 | FOR_EACH_TOP_DEVICE(device->restore(fMCRec->fMatrix)); |
| 1262 | } |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 1263 | |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 1264 | if (backImage) { |
| 1265 | SkPaint paint; |
| 1266 | paint.setBlendMode(SkBlendMode::kDstOver); |
| 1267 | const int x = backImage->fLoc.x(); |
| 1268 | const int y = backImage->fLoc.y(); |
| 1269 | this->getTopDevice()->drawSpecial(backImage->fImage.get(), x, y, paint, |
| 1270 | nullptr, SkMatrix::I()); |
| 1271 | } |
| 1272 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1273 | /* Time to draw the layer's offscreen. We can't call the public drawSprite, |
| 1274 | since if we're being recorded, we don't want to record this (the |
| 1275 | recorder will have already recorded the restore). |
| 1276 | */ |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1277 | if (layer) { |
Mike Reed | b43a3e0 | 2017-02-11 10:18:58 -0500 | [diff] [blame] | 1278 | if (fMCRec) { |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 1279 | const SkIPoint& origin = layer->fDevice->getOrigin(); |
Lee Salzman | 5d8949c | 2018-09-19 15:36:54 -0400 | [diff] [blame] | 1280 | layer->fDevice->setImmutable(); |
Florin Malita | 713b8ef | 2017-04-28 10:57:24 -0400 | [diff] [blame] | 1281 | this->internalDrawDevice(layer->fDevice.get(), origin.x(), origin.y(), |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 1282 | layer->fPaint.get(), |
| 1283 | layer->fClipImage.get(), layer->fClipMatrix); |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1284 | // restore what we smashed in internalSaveLayer |
Ben Wagner | 738a256 | 2018-04-23 17:23:30 -0400 | [diff] [blame] | 1285 | this->internalSetMatrix(layer->fStashedMatrix); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1286 | // reset this, since internalDrawDevice will have set it to true |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1287 | delete layer; |
reed | b679ca8 | 2015-04-07 04:40:48 -0700 | [diff] [blame] | 1288 | } else { |
| 1289 | // we're at the root |
reed | a499f90 | 2015-05-01 09:34:31 -0700 | [diff] [blame] | 1290 | SkASSERT(layer == (void*)fDeviceCMStorage); |
reed | b679ca8 | 2015-04-07 04:40:48 -0700 | [diff] [blame] | 1291 | layer->~DeviceCM(); |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1292 | // no need to update fMCRec, 'cause we're killing the canvas |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1293 | } |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 1294 | } |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1295 | |
| 1296 | if (fMCRec) { |
msarett | 9637ea9 | 2016-08-18 14:03:30 -0700 | [diff] [blame] | 1297 | fIsScaleTranslate = fMCRec->fMatrix.isScaleTranslate(); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1298 | fDeviceClipBounds = qr_clip_bounds(fMCRec->fRasterClip.getBounds()); |
| 1299 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1302 | sk_sp<SkSurface> SkCanvas::makeSurface(const SkImageInfo& info, const SkSurfaceProps* props) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1303 | if (nullptr == props) { |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 1304 | props = &fProps; |
| 1305 | } |
| 1306 | return this->onNewSurface(info, *props); |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1309 | sk_sp<SkSurface> SkCanvas::onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props) { |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 1310 | SkBaseDevice* dev = this->getDevice(); |
Cary Clark | a24712e | 2018-09-05 18:41:40 +0000 | [diff] [blame] | 1311 | return dev ? dev->makeSurface(info, props) : nullptr; |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1314 | SkImageInfo SkCanvas::imageInfo() const { |
reed | ea5a651 | 2016-07-07 16:44:27 -0700 | [diff] [blame] | 1315 | return this->onImageInfo(); |
| 1316 | } |
| 1317 | |
| 1318 | SkImageInfo SkCanvas::onImageInfo() const { |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1319 | SkBaseDevice* dev = this->getDevice(); |
| 1320 | if (dev) { |
| 1321 | return dev->imageInfo(); |
| 1322 | } else { |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 1323 | return SkImageInfo::MakeUnknown(0, 0); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1324 | } |
| 1325 | } |
| 1326 | |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 1327 | bool SkCanvas::getProps(SkSurfaceProps* props) const { |
reed | ea5a651 | 2016-07-07 16:44:27 -0700 | [diff] [blame] | 1328 | return this->onGetProps(props); |
| 1329 | } |
| 1330 | |
| 1331 | bool SkCanvas::onGetProps(SkSurfaceProps* props) const { |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 1332 | SkBaseDevice* dev = this->getDevice(); |
| 1333 | if (dev) { |
| 1334 | if (props) { |
| 1335 | *props = fProps; |
| 1336 | } |
| 1337 | return true; |
| 1338 | } else { |
| 1339 | return false; |
| 1340 | } |
| 1341 | } |
| 1342 | |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 1343 | bool SkCanvas::peekPixels(SkPixmap* pmap) { |
| 1344 | return this->onPeekPixels(pmap); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 1347 | bool SkCanvas::onPeekPixels(SkPixmap* pmap) { |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1348 | SkBaseDevice* dev = this->getDevice(); |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 1349 | return dev && dev->peekPixels(pmap); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
commit-bot@chromium.org | 6b4aaa7 | 2014-04-21 21:09:38 +0000 | [diff] [blame] | 1352 | void* SkCanvas::accessTopLayerPixels(SkImageInfo* info, size_t* rowBytes, SkIPoint* origin) { |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 1353 | SkPixmap pmap; |
| 1354 | if (!this->onAccessTopLayerPixels(&pmap)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1355 | return nullptr; |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 1356 | } |
| 1357 | if (info) { |
| 1358 | *info = pmap.info(); |
| 1359 | } |
| 1360 | if (rowBytes) { |
| 1361 | *rowBytes = pmap.rowBytes(); |
| 1362 | } |
| 1363 | if (origin) { |
Florin Malita | 0ed3b64 | 2017-01-13 16:56:38 +0000 | [diff] [blame] | 1364 | *origin = this->getTopDevice()->getOrigin(); |
commit-bot@chromium.org | 6b4aaa7 | 2014-04-21 21:09:38 +0000 | [diff] [blame] | 1365 | } |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 1366 | return pmap.writable_addr(); |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 1369 | bool SkCanvas::onAccessTopLayerPixels(SkPixmap* pmap) { |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1370 | SkBaseDevice* dev = this->getTopDevice(); |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 1371 | return dev && dev->accessPixels(pmap); |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1374 | ///////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1375 | |
Mike Reed | 8bcd128 | 2019-03-13 16:51:54 -0400 | [diff] [blame] | 1376 | // In our current design/features, we should never have a layer (src) in a different colorspace |
| 1377 | // than its parent (dst), so we assert that here. This is called out from other asserts, in case |
| 1378 | // we add some feature in the future to allow a given layer/imagefilter to operate in a specific |
| 1379 | // colorspace. |
| 1380 | static void check_drawdevice_colorspaces(SkColorSpace* src, SkColorSpace* dst) { |
| 1381 | SkASSERT(src == dst); |
| 1382 | } |
| 1383 | |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 1384 | void SkCanvas::internalDrawDevice(SkBaseDevice* srcDev, int x, int y, const SkPaint* paint, |
| 1385 | SkImage* clipImage, const SkMatrix& clipMatrix) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1386 | SkPaint tmp; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1387 | if (nullptr == paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1388 | paint = &tmp; |
| 1389 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1390 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 1391 | DRAW_BEGIN_DRAWDEVICE(*paint) |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 1392 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1393 | while (iter.next()) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1394 | SkBaseDevice* dstDev = iter.fDevice; |
Mike Reed | 8bcd128 | 2019-03-13 16:51:54 -0400 | [diff] [blame] | 1395 | check_drawdevice_colorspaces(dstDev->imageInfo().colorSpace(), |
| 1396 | srcDev->imageInfo().colorSpace()); |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 1397 | paint = &draw.paint(); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1398 | SkImageFilter* filter = paint->getImageFilter(); |
| 1399 | SkIPoint pos = { x - iter.getX(), y - iter.getY() }; |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 1400 | if (filter || clipImage) { |
Robert Phillips | 833dcf4 | 2016-11-18 08:44:13 -0500 | [diff] [blame] | 1401 | sk_sp<SkSpecialImage> specialImage = srcDev->snapSpecial(); |
| 1402 | if (specialImage) { |
Mike Reed | 8bcd128 | 2019-03-13 16:51:54 -0400 | [diff] [blame] | 1403 | check_drawdevice_colorspaces(dstDev->imageInfo().colorSpace(), |
| 1404 | specialImage->getColorSpace()); |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 1405 | dstDev->drawSpecial(specialImage.get(), pos.x(), pos.y(), *paint, |
| 1406 | clipImage, clipMatrix); |
Robert Phillips | 833dcf4 | 2016-11-18 08:44:13 -0500 | [diff] [blame] | 1407 | } |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1408 | } else { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1409 | dstDev->drawDevice(srcDev, pos.x(), pos.y(), *paint); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1410 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1411 | } |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 1412 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 1413 | DRAW_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
reed | 3270467 | 2015-12-16 08:27:10 -0800 | [diff] [blame] | 1416 | ///////////////////////////////////////////////////////////////////////////// |
reed | da420b9 | 2015-12-16 08:38:15 -0800 | [diff] [blame] | 1417 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1418 | void SkCanvas::translate(SkScalar dx, SkScalar dy) { |
reed | fe69b50 | 2016-09-12 06:31:48 -0700 | [diff] [blame] | 1419 | if (dx || dy) { |
| 1420 | this->checkForDeferredSave(); |
reed | fe69b50 | 2016-09-12 06:31:48 -0700 | [diff] [blame] | 1421 | fMCRec->fMatrix.preTranslate(dx,dy); |
mtklein | cbdf007 | 2016-08-19 09:05:27 -0700 | [diff] [blame] | 1422 | |
reed | fe69b50 | 2016-09-12 06:31:48 -0700 | [diff] [blame] | 1423 | // Translate shouldn't affect the is-scale-translateness of the matrix. |
| 1424 | SkASSERT(fIsScaleTranslate == fMCRec->fMatrix.isScaleTranslate()); |
mtklein | cbdf007 | 2016-08-19 09:05:27 -0700 | [diff] [blame] | 1425 | |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 1426 | FOR_EACH_TOP_DEVICE(device->setGlobalCTM(fMCRec->fMatrix)); |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 1427 | |
reed | fe69b50 | 2016-09-12 06:31:48 -0700 | [diff] [blame] | 1428 | this->didTranslate(dx,dy); |
| 1429 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1432 | void SkCanvas::scale(SkScalar sx, SkScalar sy) { |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 1433 | SkMatrix m; |
| 1434 | m.setScale(sx, sy); |
| 1435 | this->concat(m); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1438 | void SkCanvas::rotate(SkScalar degrees) { |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 1439 | SkMatrix m; |
| 1440 | m.setRotate(degrees); |
| 1441 | this->concat(m); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
bungeman | 7438bfc | 2016-07-12 15:01:19 -0700 | [diff] [blame] | 1444 | void SkCanvas::rotate(SkScalar degrees, SkScalar px, SkScalar py) { |
| 1445 | SkMatrix m; |
| 1446 | m.setRotate(degrees, px, py); |
| 1447 | this->concat(m); |
| 1448 | } |
| 1449 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1450 | void SkCanvas::skew(SkScalar sx, SkScalar sy) { |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 1451 | SkMatrix m; |
| 1452 | m.setSkew(sx, sy); |
| 1453 | this->concat(m); |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1456 | void SkCanvas::concat(const SkMatrix& matrix) { |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 1457 | if (matrix.isIdentity()) { |
| 1458 | return; |
| 1459 | } |
| 1460 | |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1461 | this->checkForDeferredSave(); |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1462 | fMCRec->fMatrix.preConcat(matrix); |
msarett | 9637ea9 | 2016-08-18 14:03:30 -0700 | [diff] [blame] | 1463 | fIsScaleTranslate = fMCRec->fMatrix.isScaleTranslate(); |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 1464 | |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 1465 | FOR_EACH_TOP_DEVICE(device->setGlobalCTM(fMCRec->fMatrix)); |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 1466 | |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 1467 | this->didConcat(matrix); |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1470 | void SkCanvas::internalSetMatrix(const SkMatrix& matrix) { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1471 | fMCRec->fMatrix = matrix; |
msarett | 9da5a5a | 2016-08-19 08:38:36 -0700 | [diff] [blame] | 1472 | fIsScaleTranslate = matrix.isScaleTranslate(); |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 1473 | |
Mike Reed | c42a1cd | 2017-02-14 14:25:14 -0500 | [diff] [blame] | 1474 | FOR_EACH_TOP_DEVICE(device->setGlobalCTM(fMCRec->fMatrix)); |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1475 | } |
| 1476 | |
| 1477 | void SkCanvas::setMatrix(const SkMatrix& matrix) { |
| 1478 | this->checkForDeferredSave(); |
| 1479 | this->internalSetMatrix(matrix); |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 1480 | this->didSetMatrix(matrix); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1483 | void SkCanvas::resetMatrix() { |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1484 | this->setMatrix(SkMatrix::I()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | ////////////////////////////////////////////////////////////////////////////// |
| 1488 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1489 | void SkCanvas::clipRect(const SkRect& rect, SkClipOp op, bool doAA) { |
Mike Reed | 9cec1bc | 2018-01-19 12:57:01 -0500 | [diff] [blame] | 1490 | if (!rect.isFinite()) { |
| 1491 | return; |
| 1492 | } |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1493 | this->checkForDeferredSave(); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1494 | ClipEdgeStyle edgeStyle = doAA ? kSoft_ClipEdgeStyle : kHard_ClipEdgeStyle; |
| 1495 | this->onClipRect(rect, op, edgeStyle); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1498 | void SkCanvas::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle) { |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1499 | const bool isAA = kSoft_ClipEdgeStyle == edgeStyle; |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 1500 | |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 1501 | FOR_EACH_TOP_DEVICE(device->clipRect(rect, op, isAA)); |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 1502 | |
reed | c64eff5 | 2015-11-21 12:39:45 -0800 | [diff] [blame] | 1503 | AutoValidateClip avc(this); |
Mike Reed | 20800c8 | 2017-11-15 16:09:04 -0500 | [diff] [blame] | 1504 | fMCRec->fRasterClip.opRect(rect, fMCRec->fMatrix, this->getTopLayerBounds(), (SkRegion::Op)op, |
| 1505 | isAA); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1506 | fDeviceClipBounds = qr_clip_bounds(fMCRec->fRasterClip.getBounds()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
Stan Iliev | 5f1bb0a | 2016-12-12 17:39:55 -0500 | [diff] [blame] | 1509 | void SkCanvas::androidFramework_setDeviceClipRestriction(const SkIRect& rect) { |
| 1510 | fClipRestrictionRect = rect; |
Mike Reed | d519d48 | 2017-02-16 11:04:52 -0500 | [diff] [blame] | 1511 | if (fClipRestrictionRect.isEmpty()) { |
| 1512 | // we notify the device, but we *dont* resolve deferred saves (since we're just |
| 1513 | // removing the restriction if the rect is empty. how I hate this api. |
Mike Reed | d519d48 | 2017-02-16 11:04:52 -0500 | [diff] [blame] | 1514 | FOR_EACH_TOP_DEVICE(device->androidFramework_setDeviceClipRestriction(&fClipRestrictionRect)); |
Mike Reed | d519d48 | 2017-02-16 11:04:52 -0500 | [diff] [blame] | 1515 | } else { |
Stan Iliev | 5f1bb0a | 2016-12-12 17:39:55 -0500 | [diff] [blame] | 1516 | this->checkForDeferredSave(); |
Mike Reed | d519d48 | 2017-02-16 11:04:52 -0500 | [diff] [blame] | 1517 | FOR_EACH_TOP_DEVICE(device->androidFramework_setDeviceClipRestriction(&fClipRestrictionRect)); |
Stan Iliev | 5f1bb0a | 2016-12-12 17:39:55 -0500 | [diff] [blame] | 1518 | AutoValidateClip avc(this); |
Mike Reed | 20800c8 | 2017-11-15 16:09:04 -0500 | [diff] [blame] | 1519 | fMCRec->fRasterClip.opIRect(fClipRestrictionRect, SkRegion::kIntersect_Op); |
Stan Iliev | 5f1bb0a | 2016-12-12 17:39:55 -0500 | [diff] [blame] | 1520 | fDeviceClipBounds = qr_clip_bounds(fMCRec->fRasterClip.getBounds()); |
| 1521 | } |
| 1522 | } |
| 1523 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1524 | void SkCanvas::clipRRect(const SkRRect& rrect, SkClipOp op, bool doAA) { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1525 | this->checkForDeferredSave(); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1526 | ClipEdgeStyle edgeStyle = doAA ? kSoft_ClipEdgeStyle : kHard_ClipEdgeStyle; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1527 | if (rrect.isRect()) { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1528 | this->onClipRect(rrect.getBounds(), op, edgeStyle); |
| 1529 | } else { |
| 1530 | this->onClipRRect(rrect, op, edgeStyle); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1531 | } |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1532 | } |
commit-bot@chromium.org | 14e50ae | 2014-02-16 23:35:31 +0000 | [diff] [blame] | 1533 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1534 | void SkCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) { |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1535 | AutoValidateClip avc(this); |
commit-bot@chromium.org | 14e50ae | 2014-02-16 23:35:31 +0000 | [diff] [blame] | 1536 | |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1537 | bool isAA = kSoft_ClipEdgeStyle == edgeStyle; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1538 | |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 1539 | FOR_EACH_TOP_DEVICE(device->clipRRect(rrect, op, isAA)); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1540 | |
Mike Reed | 20800c8 | 2017-11-15 16:09:04 -0500 | [diff] [blame] | 1541 | fMCRec->fRasterClip.opRRect(rrect, fMCRec->fMatrix, this->getTopLayerBounds(), (SkRegion::Op)op, |
| 1542 | isAA); |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1543 | fDeviceClipBounds = qr_clip_bounds(fMCRec->fRasterClip.getBounds()); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1546 | void SkCanvas::clipPath(const SkPath& path, SkClipOp op, bool doAA) { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1547 | this->checkForDeferredSave(); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1548 | ClipEdgeStyle edgeStyle = doAA ? kSoft_ClipEdgeStyle : kHard_ClipEdgeStyle; |
robertphillips | 39f0538 | 2015-11-24 09:30:12 -0800 | [diff] [blame] | 1549 | |
| 1550 | if (!path.isInverseFillType() && fMCRec->fMatrix.rectStaysRect()) { |
| 1551 | SkRect r; |
| 1552 | if (path.isRect(&r)) { |
| 1553 | this->onClipRect(r, op, edgeStyle); |
| 1554 | return; |
| 1555 | } |
| 1556 | SkRRect rrect; |
| 1557 | if (path.isOval(&r)) { |
| 1558 | rrect.setOval(r); |
| 1559 | this->onClipRRect(rrect, op, edgeStyle); |
| 1560 | return; |
| 1561 | } |
| 1562 | if (path.isRRect(&rrect)) { |
| 1563 | this->onClipRRect(rrect, op, edgeStyle); |
| 1564 | return; |
| 1565 | } |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1566 | } |
robertphillips | 39f0538 | 2015-11-24 09:30:12 -0800 | [diff] [blame] | 1567 | |
| 1568 | this->onClipPath(path, op, edgeStyle); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1569 | } |
| 1570 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1571 | void SkCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1572 | AutoValidateClip avc(this); |
| 1573 | |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1574 | bool isAA = kSoft_ClipEdgeStyle == edgeStyle; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 1575 | |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 1576 | FOR_EACH_TOP_DEVICE(device->clipPath(path, op, isAA)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1577 | |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1578 | const SkPath* rasterClipPath = &path; |
| 1579 | const SkMatrix* matrix = &fMCRec->fMatrix; |
Mike Reed | 20800c8 | 2017-11-15 16:09:04 -0500 | [diff] [blame] | 1580 | fMCRec->fRasterClip.opPath(*rasterClipPath, *matrix, this->getTopLayerBounds(), |
| 1581 | (SkRegion::Op)op, isAA); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1582 | fDeviceClipBounds = qr_clip_bounds(fMCRec->fRasterClip.getBounds()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1583 | } |
| 1584 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1585 | void SkCanvas::clipRegion(const SkRegion& rgn, SkClipOp op) { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1586 | this->checkForDeferredSave(); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1587 | this->onClipRegion(rgn, op); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1590 | void SkCanvas::onClipRegion(const SkRegion& rgn, SkClipOp op) { |
Mike Reed | 7627fa5 | 2017-02-08 10:07:53 -0500 | [diff] [blame] | 1591 | FOR_EACH_TOP_DEVICE(device->clipRegion(rgn, op)); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1592 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1593 | AutoValidateClip avc(this); |
| 1594 | |
Mike Reed | 20800c8 | 2017-11-15 16:09:04 -0500 | [diff] [blame] | 1595 | fMCRec->fRasterClip.opRegion(rgn, (SkRegion::Op)op); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1596 | fDeviceClipBounds = qr_clip_bounds(fMCRec->fRasterClip.getBounds()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1599 | #ifdef SK_DEBUG |
| 1600 | void SkCanvas::validateClip() const { |
| 1601 | // construct clipRgn from the clipstack |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1602 | const SkBaseDevice* device = this->getDevice(); |
djsollen@google.com | ccfee2a | 2012-05-01 16:50:10 +0000 | [diff] [blame] | 1603 | if (!device) { |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1604 | SkASSERT(this->isClipEmpty()); |
djsollen@google.com | ccfee2a | 2012-05-01 16:50:10 +0000 | [diff] [blame] | 1605 | return; |
| 1606 | } |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1607 | } |
| 1608 | #endif |
| 1609 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1610 | bool SkCanvas::androidFramework_isClipAA() const { |
| 1611 | bool containsAA = false; |
| 1612 | |
| 1613 | FOR_EACH_TOP_DEVICE(containsAA |= device->onClipIsAA()); |
| 1614 | |
| 1615 | return containsAA; |
| 1616 | } |
| 1617 | |
| 1618 | class RgnAccumulator { |
| 1619 | SkRegion* fRgn; |
| 1620 | public: |
| 1621 | RgnAccumulator(SkRegion* total) : fRgn(total) {} |
| 1622 | void accumulate(SkBaseDevice* device, SkRegion* rgn) { |
| 1623 | SkIPoint origin = device->getOrigin(); |
| 1624 | if (origin.x() | origin.y()) { |
| 1625 | rgn->translate(origin.x(), origin.y()); |
| 1626 | } |
| 1627 | fRgn->op(*rgn, SkRegion::kUnion_Op); |
| 1628 | } |
| 1629 | }; |
| 1630 | |
| 1631 | void SkCanvas::temporary_internal_getRgnClip(SkRegion* rgn) { |
| 1632 | RgnAccumulator accum(rgn); |
| 1633 | SkRegion tmp; |
| 1634 | |
| 1635 | rgn->setEmpty(); |
| 1636 | FOR_EACH_TOP_DEVICE(device->onAsRgnClip(&tmp); accum.accumulate(device, &tmp)); |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 1637 | } |
| 1638 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1639 | /////////////////////////////////////////////////////////////////////////////// |
| 1640 | |
reed@google.com | 754de5f | 2014-02-24 19:38:20 +0000 | [diff] [blame] | 1641 | bool SkCanvas::isClipEmpty() const { |
Mike Reed | 02be3c1 | 2017-03-23 12:34:15 -0400 | [diff] [blame] | 1642 | return fMCRec->fRasterClip.isEmpty(); |
| 1643 | |
| 1644 | // TODO: should we only use the conservative answer in a recording canvas? |
| 1645 | #if 0 |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1646 | SkBaseDevice* dev = this->getTopDevice(); |
| 1647 | // if no device we return true |
| 1648 | return !dev || dev->onGetClipType() == SkBaseDevice::kEmpty_ClipType; |
Mike Reed | 02be3c1 | 2017-03-23 12:34:15 -0400 | [diff] [blame] | 1649 | #endif |
reed@google.com | 754de5f | 2014-02-24 19:38:20 +0000 | [diff] [blame] | 1650 | } |
| 1651 | |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1652 | bool SkCanvas::isClipRect() const { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1653 | SkBaseDevice* dev = this->getTopDevice(); |
| 1654 | // if no device we return false |
Dave Tapuska | 7139f13 | 2019-08-15 09:22:11 -0400 | [diff] [blame] | 1655 | return dev && dev->onGetClipType() == SkBaseDevice::ClipType::kRect; |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1656 | } |
| 1657 | |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1658 | static inline bool is_nan_or_clipped(const Sk4f& devRect, const Sk4f& devClip) { |
| 1659 | #if !defined(SKNX_NO_SIMD) && SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 |
| 1660 | __m128 lLtT = _mm_unpacklo_ps(devRect.fVec, devClip.fVec); |
| 1661 | __m128 RrBb = _mm_unpackhi_ps(devClip.fVec, devRect.fVec); |
| 1662 | __m128 mask = _mm_cmplt_ps(lLtT, RrBb); |
| 1663 | return 0xF != _mm_movemask_ps(mask); |
| 1664 | #elif !defined(SKNX_NO_SIMD) && defined(SK_ARM_HAS_NEON) |
| 1665 | float32x4_t lLtT = vzipq_f32(devRect.fVec, devClip.fVec).val[0]; |
| 1666 | float32x4_t RrBb = vzipq_f32(devClip.fVec, devRect.fVec).val[1]; |
| 1667 | uint32x4_t mask = vcltq_f32(lLtT, RrBb); |
| 1668 | return 0xFFFFFFFFFFFFFFFF != (uint64_t) vmovn_u32(mask); |
| 1669 | #else |
| 1670 | SkRect devRectAsRect; |
| 1671 | SkRect devClipAsRect; |
| 1672 | devRect.store(&devRectAsRect.fLeft); |
| 1673 | devClip.store(&devClipAsRect.fLeft); |
| 1674 | return !devRectAsRect.isFinite() || !devRectAsRect.intersect(devClipAsRect); |
| 1675 | #endif |
| 1676 | } |
wjmaclean@chromium.org | 116b2bc | 2011-02-07 17:48:37 +0000 | [diff] [blame] | 1677 | |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1678 | // It's important for this function to not be inlined. Otherwise the compiler will share code |
| 1679 | // between the fast path and the slow path, resulting in two slow paths. |
| 1680 | static SK_NEVER_INLINE bool quick_reject_slow_path(const SkRect& src, const SkRect& deviceClip, |
| 1681 | const SkMatrix& matrix) { |
| 1682 | SkRect deviceRect; |
| 1683 | matrix.mapRect(&deviceRect, src); |
| 1684 | return !deviceRect.isFinite() || !deviceRect.intersect(deviceClip); |
| 1685 | } |
| 1686 | |
| 1687 | bool SkCanvas::quickReject(const SkRect& src) const { |
| 1688 | #ifdef SK_DEBUG |
| 1689 | // Verify that fDeviceClipBounds are set properly. |
| 1690 | SkRect tmp = qr_clip_bounds(fMCRec->fRasterClip.getBounds()); |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1691 | if (fMCRec->fRasterClip.isEmpty()) { |
msarett | 0c685ee | 2016-08-14 13:51:16 -0700 | [diff] [blame] | 1692 | SkASSERT(fDeviceClipBounds.isEmpty()); |
reed@android.com | a380ae4 | 2009-07-21 01:17:02 +0000 | [diff] [blame] | 1693 | } else { |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1694 | SkASSERT(tmp == fDeviceClipBounds); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1695 | } |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1696 | |
msarett | 9637ea9 | 2016-08-18 14:03:30 -0700 | [diff] [blame] | 1697 | // Verify that fIsScaleTranslate is set properly. |
| 1698 | SkASSERT(fIsScaleTranslate == fMCRec->fMatrix.isScaleTranslate()); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1699 | #endif |
| 1700 | |
msarett | 9637ea9 | 2016-08-18 14:03:30 -0700 | [diff] [blame] | 1701 | if (!fIsScaleTranslate) { |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1702 | return quick_reject_slow_path(src, fDeviceClipBounds, fMCRec->fMatrix); |
| 1703 | } |
| 1704 | |
| 1705 | // We inline the implementation of mapScaleTranslate() for the fast path. |
| 1706 | float sx = fMCRec->fMatrix.getScaleX(); |
| 1707 | float sy = fMCRec->fMatrix.getScaleY(); |
| 1708 | float tx = fMCRec->fMatrix.getTranslateX(); |
| 1709 | float ty = fMCRec->fMatrix.getTranslateY(); |
| 1710 | Sk4f scale(sx, sy, sx, sy); |
| 1711 | Sk4f trans(tx, ty, tx, ty); |
| 1712 | |
| 1713 | // Apply matrix. |
| 1714 | Sk4f ltrb = Sk4f::Load(&src.fLeft) * scale + trans; |
| 1715 | |
| 1716 | // Make sure left < right, top < bottom. |
| 1717 | Sk4f rblt(ltrb[2], ltrb[3], ltrb[0], ltrb[1]); |
| 1718 | Sk4f min = Sk4f::Min(ltrb, rblt); |
| 1719 | Sk4f max = Sk4f::Max(ltrb, rblt); |
| 1720 | // We can extract either pair [0,1] or [2,3] from min and max and be correct, but on |
| 1721 | // ARM this sequence generates the fastest (a single instruction). |
| 1722 | Sk4f devRect = Sk4f(min[2], min[3], max[0], max[1]); |
| 1723 | |
| 1724 | // Check if the device rect is NaN or outside the clip. |
| 1725 | return is_nan_or_clipped(devRect, Sk4f::Load(&fDeviceClipBounds.fLeft)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1726 | } |
| 1727 | |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1728 | bool SkCanvas::quickReject(const SkPath& path) const { |
| 1729 | return path.isEmpty() || this->quickReject(path.getBounds()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Mike Klein | 83c8dd9 | 2017-11-28 17:08:45 -0500 | [diff] [blame] | 1732 | SkRect SkCanvas::getLocalClipBounds() const { |
| 1733 | SkIRect ibounds = this->getDeviceClipBounds(); |
Mike Reed | 918e144 | 2017-01-23 11:39:45 -0500 | [diff] [blame] | 1734 | if (ibounds.isEmpty()) { |
Mike Reed | 42e8c53 | 2017-01-23 14:09:13 -0500 | [diff] [blame] | 1735 | return SkRect::MakeEmpty(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1736 | } |
| 1737 | |
reed@android.com | d9c0f0b | 2009-02-06 22:39:37 +0000 | [diff] [blame] | 1738 | SkMatrix inverse; |
| 1739 | // 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] | 1740 | if (!fMCRec->fMatrix.invert(&inverse)) { |
Mike Reed | 42e8c53 | 2017-01-23 14:09:13 -0500 | [diff] [blame] | 1741 | return SkRect::MakeEmpty(); |
reed@android.com | d9c0f0b | 2009-02-06 22:39:37 +0000 | [diff] [blame] | 1742 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1743 | |
Mike Reed | 42e8c53 | 2017-01-23 14:09:13 -0500 | [diff] [blame] | 1744 | SkRect bounds; |
Mike Reed | 42e8c53 | 2017-01-23 14:09:13 -0500 | [diff] [blame] | 1745 | // adjust it outwards in case we are antialiasing |
Mike Reed | b57b931 | 2018-04-23 12:12:54 -0400 | [diff] [blame] | 1746 | const int margin = 1; |
reed@google.com | fa4d5bd | 2012-01-30 17:36:27 +0000 | [diff] [blame] | 1747 | |
Mike Reed | b57b931 | 2018-04-23 12:12:54 -0400 | [diff] [blame] | 1748 | SkRect r = SkRect::Make(ibounds.makeOutset(margin, margin)); |
Mike Reed | 42e8c53 | 2017-01-23 14:09:13 -0500 | [diff] [blame] | 1749 | inverse.mapRect(&bounds, r); |
| 1750 | return bounds; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
Mike Klein | 83c8dd9 | 2017-11-28 17:08:45 -0500 | [diff] [blame] | 1753 | SkIRect SkCanvas::getDeviceClipBounds() const { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1754 | return fMCRec->fRasterClip.getBounds(); |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1755 | } |
| 1756 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1757 | const SkMatrix& SkCanvas::getTotalMatrix() const { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1758 | return fMCRec->fMatrix; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1761 | GrRenderTargetContext* SkCanvas::internal_private_accessTopLayerRenderTargetContext() { |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1762 | SkBaseDevice* dev = this->getTopDevice(); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1763 | return dev ? dev->accessRenderTargetContext() : nullptr; |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 1766 | GrContext* SkCanvas::getGrContext() { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 1767 | SkBaseDevice* device = this->getTopDevice(); |
reed | 86ae3d1 | 2016-04-26 06:57:31 -0700 | [diff] [blame] | 1768 | return device ? device->context() : nullptr; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 1769 | } |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1770 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1771 | void SkCanvas::drawDRRect(const SkRRect& outer, const SkRRect& inner, |
| 1772 | const SkPaint& paint) { |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 1773 | TRACE_EVENT0("skia", TRACE_FUNC); |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1774 | if (outer.isEmpty()) { |
| 1775 | return; |
| 1776 | } |
| 1777 | if (inner.isEmpty()) { |
| 1778 | this->drawRRect(outer, paint); |
| 1779 | return; |
| 1780 | } |
| 1781 | |
| 1782 | // We don't have this method (yet), but technically this is what we should |
Cary Clark | e0b7287 | 2017-04-12 12:03:15 -0400 | [diff] [blame] | 1783 | // be able to return ... |
| 1784 | // if (!outer.contains(inner))) { |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1785 | // |
| 1786 | // For now at least check for containment of bounds |
Cary Clark | e0b7287 | 2017-04-12 12:03:15 -0400 | [diff] [blame] | 1787 | if (!outer.getBounds().contains(inner.getBounds())) { |
| 1788 | return; |
| 1789 | } |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1790 | |
| 1791 | this->onDrawDRRect(outer, inner, paint); |
| 1792 | } |
| 1793 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1794 | void SkCanvas::drawPaint(const SkPaint& paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1795 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1796 | this->onDrawPaint(paint); |
| 1797 | } |
| 1798 | |
| 1799 | void SkCanvas::drawRect(const SkRect& r, const SkPaint& paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1800 | TRACE_EVENT0("skia", TRACE_FUNC); |
Brian Osman | 6e3ce40 | 2017-05-17 15:10:18 -0400 | [diff] [blame] | 1801 | // To avoid redundant logic in our culling code and various backends, we always sort rects |
| 1802 | // before passing them along. |
| 1803 | this->onDrawRect(r.makeSorted(), paint); |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1804 | } |
| 1805 | |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 1806 | void SkCanvas::drawClippedToSaveBehind(const SkPaint& paint) { |
| 1807 | TRACE_EVENT0("skia", TRACE_FUNC); |
| 1808 | this->onDrawBehind(paint); |
| 1809 | } |
| 1810 | |
msarett | dca352e | 2016-08-26 06:37:45 -0700 | [diff] [blame] | 1811 | void SkCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1812 | TRACE_EVENT0("skia", TRACE_FUNC); |
msarett | dca352e | 2016-08-26 06:37:45 -0700 | [diff] [blame] | 1813 | if (region.isEmpty()) { |
| 1814 | return; |
| 1815 | } |
| 1816 | |
| 1817 | if (region.isRect()) { |
| 1818 | return this->drawIRect(region.getBounds(), paint); |
| 1819 | } |
| 1820 | |
| 1821 | this->onDrawRegion(region, paint); |
| 1822 | } |
| 1823 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1824 | void SkCanvas::drawOval(const SkRect& r, const SkPaint& paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1825 | TRACE_EVENT0("skia", TRACE_FUNC); |
Brian Osman | 6e3ce40 | 2017-05-17 15:10:18 -0400 | [diff] [blame] | 1826 | // To avoid redundant logic in our culling code and various backends, we always sort rects |
| 1827 | // before passing them along. |
| 1828 | this->onDrawOval(r.makeSorted(), paint); |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | void SkCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1832 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1833 | this->onDrawRRect(rrect, paint); |
| 1834 | } |
| 1835 | |
| 1836 | void SkCanvas::drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint& paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1837 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1838 | this->onDrawPoints(mode, count, pts, paint); |
| 1839 | } |
| 1840 | |
Mike Reed | e88a1cb | 2017-03-17 09:50:46 -0400 | [diff] [blame] | 1841 | void SkCanvas::drawVertices(const sk_sp<SkVertices>& vertices, SkBlendMode mode, |
| 1842 | const SkPaint& paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1843 | TRACE_EVENT0("skia", TRACE_FUNC); |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 1844 | RETURN_ON_NULL(vertices); |
Brian Salomon | cccafe8 | 2018-04-28 16:13:08 -0400 | [diff] [blame] | 1845 | // We expect fans to be converted to triangles when building or deserializing SkVertices. |
| 1846 | SkASSERT(vertices->mode() != SkVertices::kTriangleFan_VertexMode); |
Ruiqi Mao | f510149 | 2018-06-29 14:32:21 -0400 | [diff] [blame] | 1847 | this->onDrawVerticesObject(vertices.get(), nullptr, 0, mode, paint); |
Mike Reed | e88a1cb | 2017-03-17 09:50:46 -0400 | [diff] [blame] | 1848 | } |
| 1849 | |
| 1850 | void SkCanvas::drawVertices(const SkVertices* vertices, SkBlendMode mode, const SkPaint& paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1851 | TRACE_EVENT0("skia", TRACE_FUNC); |
Mike Reed | e88a1cb | 2017-03-17 09:50:46 -0400 | [diff] [blame] | 1852 | RETURN_ON_NULL(vertices); |
Ruiqi Mao | f510149 | 2018-06-29 14:32:21 -0400 | [diff] [blame] | 1853 | this->onDrawVerticesObject(vertices, nullptr, 0, mode, paint); |
| 1854 | } |
| 1855 | |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 1856 | void SkCanvas::drawVertices(const sk_sp<SkVertices>& vertices, const SkVertices::Bone bones[], |
| 1857 | int boneCount, SkBlendMode mode, const SkPaint& paint) { |
Ruiqi Mao | f510149 | 2018-06-29 14:32:21 -0400 | [diff] [blame] | 1858 | TRACE_EVENT0("skia", TRACE_FUNC); |
| 1859 | RETURN_ON_NULL(vertices); |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1860 | SkASSERT(boneCount <= 80); |
Ruiqi Mao | f510149 | 2018-06-29 14:32:21 -0400 | [diff] [blame] | 1861 | this->onDrawVerticesObject(vertices.get(), bones, boneCount, mode, paint); |
| 1862 | } |
| 1863 | |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 1864 | void SkCanvas::drawVertices(const SkVertices* vertices, const SkVertices::Bone bones[], |
| 1865 | int boneCount, SkBlendMode mode, const SkPaint& paint) { |
Ruiqi Mao | f510149 | 2018-06-29 14:32:21 -0400 | [diff] [blame] | 1866 | TRACE_EVENT0("skia", TRACE_FUNC); |
| 1867 | RETURN_ON_NULL(vertices); |
Ruiqi Mao | b609e6d | 2018-07-17 10:19:38 -0400 | [diff] [blame] | 1868 | SkASSERT(boneCount <= 80); |
Ruiqi Mao | f510149 | 2018-06-29 14:32:21 -0400 | [diff] [blame] | 1869 | this->onDrawVerticesObject(vertices, bones, boneCount, mode, paint); |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | void SkCanvas::drawPath(const SkPath& path, const SkPaint& paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1873 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1874 | this->onDrawPath(path, paint); |
| 1875 | } |
| 1876 | |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 1877 | void SkCanvas::drawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1878 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 1879 | RETURN_ON_NULL(image); |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 1880 | this->onDrawImage(image, x, y, paint); |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1881 | } |
| 1882 | |
Mike Reed | c4e3109 | 2018-01-30 11:15:27 -0500 | [diff] [blame] | 1883 | // Returns true if the rect can be "filled" : non-empty and finite |
| 1884 | static bool fillable(const SkRect& r) { |
| 1885 | SkScalar w = r.width(); |
| 1886 | SkScalar h = r.height(); |
| 1887 | return SkScalarIsFinite(w) && w > 0 && SkScalarIsFinite(h) && h > 0; |
| 1888 | } |
| 1889 | |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1890 | void SkCanvas::drawImageRect(const SkImage* image, const SkRect& src, const SkRect& dst, |
| 1891 | const SkPaint* paint, SrcRectConstraint constraint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1892 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 1893 | RETURN_ON_NULL(image); |
Mike Reed | c4e3109 | 2018-01-30 11:15:27 -0500 | [diff] [blame] | 1894 | if (!fillable(dst) || !fillable(src)) { |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1895 | return; |
| 1896 | } |
| 1897 | this->onDrawImageRect(image, &src, dst, paint, constraint); |
| 1898 | } |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1899 | |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 1900 | void SkCanvas::drawImageRect(const SkImage* image, const SkIRect& isrc, const SkRect& dst, |
| 1901 | const SkPaint* paint, SrcRectConstraint constraint) { |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 1902 | RETURN_ON_NULL(image); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1903 | this->drawImageRect(image, SkRect::Make(isrc), dst, paint, constraint); |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 1904 | } |
| 1905 | |
Brian Salomon | f08002c | 2018-10-26 16:15:46 -0400 | [diff] [blame] | 1906 | void SkCanvas::drawImageRect(const SkImage* image, const SkRect& dst, const SkPaint* paint) { |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 1907 | RETURN_ON_NULL(image); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1908 | this->drawImageRect(image, SkRect::MakeIWH(image->width(), image->height()), dst, paint, |
Brian Salomon | f08002c | 2018-10-26 16:15:46 -0400 | [diff] [blame] | 1909 | kFast_SrcRectConstraint); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1910 | } |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1911 | |
Leon Scroggins III | 57e1f02 | 2018-04-20 14:53:00 -0400 | [diff] [blame] | 1912 | namespace { |
Brian Salomon | 969be1c | 2018-05-21 14:37:49 -0400 | [diff] [blame] | 1913 | class LatticePaint : SkNoncopyable { |
Leon Scroggins III | 57e1f02 | 2018-04-20 14:53:00 -0400 | [diff] [blame] | 1914 | public: |
Brian Salomon | 969be1c | 2018-05-21 14:37:49 -0400 | [diff] [blame] | 1915 | LatticePaint(const SkPaint* origPaint) : fPaint(origPaint) { |
| 1916 | if (!origPaint) { |
| 1917 | return; |
| 1918 | } |
| 1919 | if (origPaint->getFilterQuality() > kLow_SkFilterQuality) { |
| 1920 | fPaint.writable()->setFilterQuality(kLow_SkFilterQuality); |
| 1921 | } |
| 1922 | if (origPaint->getMaskFilter()) { |
| 1923 | fPaint.writable()->setMaskFilter(nullptr); |
| 1924 | } |
| 1925 | if (origPaint->isAntiAlias()) { |
| 1926 | fPaint.writable()->setAntiAlias(false); |
Leon Scroggins III | 57e1f02 | 2018-04-20 14:53:00 -0400 | [diff] [blame] | 1927 | } |
| 1928 | } |
| 1929 | |
| 1930 | const SkPaint* get() const { |
| 1931 | return fPaint; |
| 1932 | } |
| 1933 | |
| 1934 | private: |
Brian Salomon | 969be1c | 2018-05-21 14:37:49 -0400 | [diff] [blame] | 1935 | SkTCopyOnFirstWrite<SkPaint> fPaint; |
Leon Scroggins III | 57e1f02 | 2018-04-20 14:53:00 -0400 | [diff] [blame] | 1936 | }; |
| 1937 | } // namespace |
| 1938 | |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 1939 | void SkCanvas::drawImageNine(const SkImage* image, const SkIRect& center, const SkRect& dst, |
| 1940 | const SkPaint* paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1941 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 1942 | RETURN_ON_NULL(image); |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 1943 | if (dst.isEmpty()) { |
| 1944 | return; |
| 1945 | } |
msarett | 552bca9 | 2016-08-03 06:53:26 -0700 | [diff] [blame] | 1946 | if (SkLatticeIter::Valid(image->width(), image->height(), center)) { |
Brian Salomon | 969be1c | 2018-05-21 14:37:49 -0400 | [diff] [blame] | 1947 | LatticePaint latticePaint(paint); |
| 1948 | this->onDrawImageNine(image, center, dst, latticePaint.get()); |
msarett | 552bca9 | 2016-08-03 06:53:26 -0700 | [diff] [blame] | 1949 | } else { |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1950 | this->drawImageRect(image, dst, paint); |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 1951 | } |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 1952 | } |
| 1953 | |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 1954 | void SkCanvas::drawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst, |
| 1955 | const SkPaint* paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1956 | TRACE_EVENT0("skia", TRACE_FUNC); |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 1957 | RETURN_ON_NULL(image); |
| 1958 | if (dst.isEmpty()) { |
| 1959 | return; |
| 1960 | } |
msarett | 71df2d7 | 2016-09-30 12:41:42 -0700 | [diff] [blame] | 1961 | |
| 1962 | SkIRect bounds; |
| 1963 | Lattice latticePlusBounds = lattice; |
| 1964 | if (!latticePlusBounds.fBounds) { |
| 1965 | bounds = SkIRect::MakeWH(image->width(), image->height()); |
| 1966 | latticePlusBounds.fBounds = &bounds; |
| 1967 | } |
| 1968 | |
| 1969 | if (SkLatticeIter::Valid(image->width(), image->height(), latticePlusBounds)) { |
Brian Salomon | 969be1c | 2018-05-21 14:37:49 -0400 | [diff] [blame] | 1970 | LatticePaint latticePaint(paint); |
| 1971 | this->onDrawImageLattice(image, latticePlusBounds, dst, latticePaint.get()); |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 1972 | } else { |
| 1973 | this->drawImageRect(image, dst, paint); |
| 1974 | } |
| 1975 | } |
| 1976 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1977 | void SkCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar dx, SkScalar dy, const SkPaint* paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1978 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 1979 | if (bitmap.drawsNothing()) { |
tomhudson | 2df6fd6 | 2015-04-09 09:20:19 -0700 | [diff] [blame] | 1980 | return; |
| 1981 | } |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1982 | this->onDrawBitmap(bitmap, dx, dy, paint); |
| 1983 | } |
| 1984 | |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1985 | void SkCanvas::drawBitmapRect(const SkBitmap& bitmap, const SkRect& src, const SkRect& dst, |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 1986 | const SkPaint* paint, SrcRectConstraint constraint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 1987 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1988 | if (bitmap.drawsNothing() || dst.isEmpty() || src.isEmpty()) { |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 1989 | return; |
| 1990 | } |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1991 | this->onDrawBitmapRect(bitmap, &src, dst, paint, constraint); |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1992 | } |
| 1993 | |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 1994 | void SkCanvas::drawBitmapRect(const SkBitmap& bitmap, const SkIRect& isrc, const SkRect& dst, |
| 1995 | const SkPaint* paint, SrcRectConstraint constraint) { |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1996 | this->drawBitmapRect(bitmap, SkRect::Make(isrc), dst, paint, constraint); |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 1997 | } |
| 1998 | |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1999 | void SkCanvas::drawBitmapRect(const SkBitmap& bitmap, const SkRect& dst, const SkPaint* paint, |
| 2000 | SrcRectConstraint constraint) { |
| 2001 | this->drawBitmapRect(bitmap, SkRect::MakeIWH(bitmap.width(), bitmap.height()), dst, paint, |
| 2002 | constraint); |
| 2003 | } |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 2004 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2005 | void SkCanvas::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst, |
| 2006 | const SkPaint* paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 2007 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2008 | if (bitmap.drawsNothing() || dst.isEmpty()) { |
tomhudson | 2df6fd6 | 2015-04-09 09:20:19 -0700 | [diff] [blame] | 2009 | return; |
| 2010 | } |
msarett | 552bca9 | 2016-08-03 06:53:26 -0700 | [diff] [blame] | 2011 | if (SkLatticeIter::Valid(bitmap.width(), bitmap.height(), center)) { |
Brian Salomon | 969be1c | 2018-05-21 14:37:49 -0400 | [diff] [blame] | 2012 | LatticePaint latticePaint(paint); |
| 2013 | this->onDrawBitmapNine(bitmap, center, dst, latticePaint.get()); |
msarett | 552bca9 | 2016-08-03 06:53:26 -0700 | [diff] [blame] | 2014 | } else { |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 2015 | this->drawBitmapRect(bitmap, dst, paint); |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2016 | } |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2017 | } |
| 2018 | |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 2019 | void SkCanvas::drawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice, const SkRect& dst, |
| 2020 | const SkPaint* paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 2021 | TRACE_EVENT0("skia", TRACE_FUNC); |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2022 | if (bitmap.drawsNothing() || dst.isEmpty()) { |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 2023 | return; |
| 2024 | } |
msarett | 71df2d7 | 2016-09-30 12:41:42 -0700 | [diff] [blame] | 2025 | |
| 2026 | SkIRect bounds; |
| 2027 | Lattice latticePlusBounds = lattice; |
| 2028 | if (!latticePlusBounds.fBounds) { |
| 2029 | bounds = SkIRect::MakeWH(bitmap.width(), bitmap.height()); |
| 2030 | latticePlusBounds.fBounds = &bounds; |
| 2031 | } |
| 2032 | |
| 2033 | if (SkLatticeIter::Valid(bitmap.width(), bitmap.height(), latticePlusBounds)) { |
Brian Salomon | 969be1c | 2018-05-21 14:37:49 -0400 | [diff] [blame] | 2034 | LatticePaint latticePaint(paint); |
| 2035 | this->onDrawBitmapLattice(bitmap, latticePlusBounds, dst, latticePaint.get()); |
msarett | 552bca9 | 2016-08-03 06:53:26 -0700 | [diff] [blame] | 2036 | } else { |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2037 | this->drawBitmapRect(bitmap, dst, paint); |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 2038 | } |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 2039 | } |
| 2040 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2041 | void SkCanvas::drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[], |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 2042 | const SkColor colors[], int count, SkBlendMode mode, |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2043 | const SkRect* cull, const SkPaint* paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 2044 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 2045 | RETURN_ON_NULL(atlas); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2046 | if (count <= 0) { |
| 2047 | return; |
| 2048 | } |
Joe Gregorio | c485907 | 2017-01-20 14:21:27 +0000 | [diff] [blame] | 2049 | SkASSERT(atlas); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2050 | SkASSERT(tex); |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 2051 | this->onDrawAtlas(atlas, xform, tex, colors, count, mode, cull, paint); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2052 | } |
| 2053 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 2054 | void SkCanvas::drawAnnotation(const SkRect& rect, const char key[], SkData* value) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 2055 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 2056 | if (key) { |
| 2057 | this->onDrawAnnotation(rect, key, value); |
| 2058 | } |
| 2059 | } |
| 2060 | |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 2061 | void SkCanvas::legacy_drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst, |
| 2062 | const SkPaint* paint, SrcRectConstraint constraint) { |
| 2063 | if (src) { |
| 2064 | this->drawImageRect(image, *src, dst, paint, constraint); |
| 2065 | } else { |
| 2066 | this->drawImageRect(image, SkRect::MakeIWH(image->width(), image->height()), |
| 2067 | dst, paint, constraint); |
| 2068 | } |
| 2069 | } |
| 2070 | void SkCanvas::legacy_drawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst, |
| 2071 | const SkPaint* paint, SrcRectConstraint constraint) { |
| 2072 | if (src) { |
| 2073 | this->drawBitmapRect(bitmap, *src, dst, paint, constraint); |
| 2074 | } else { |
| 2075 | this->drawBitmapRect(bitmap, SkRect::MakeIWH(bitmap.width(), bitmap.height()), |
| 2076 | dst, paint, constraint); |
| 2077 | } |
| 2078 | } |
| 2079 | |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 2080 | void SkCanvas::private_draw_shadow_rec(const SkPath& path, const SkDrawShadowRec& rec) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 2081 | TRACE_EVENT0("skia", TRACE_FUNC); |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 2082 | this->onDrawShadowRec(path, rec); |
| 2083 | } |
| 2084 | |
| 2085 | void SkCanvas::onDrawShadowRec(const SkPath& path, const SkDrawShadowRec& rec) { |
| 2086 | SkPaint paint; |
| 2087 | const SkRect& pathBounds = path.getBounds(); |
| 2088 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2089 | DRAW_BEGIN(paint, &pathBounds) |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 2090 | while (iter.next()) { |
| 2091 | iter.fDevice->drawShadow(path, rec); |
| 2092 | } |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2093 | DRAW_END |
Mike Reed | 4204da2 | 2017-05-17 08:53:36 -0400 | [diff] [blame] | 2094 | } |
| 2095 | |
Michael Ludwig | 390f0cc | 2019-03-19 09:16:38 -0400 | [diff] [blame] | 2096 | void SkCanvas::experimental_DrawEdgeAAQuad(const SkRect& rect, const SkPoint clip[4], |
Michael Ludwig | a595f86 | 2019-08-27 15:25:49 -0400 | [diff] [blame] | 2097 | QuadAAFlags aaFlags, const SkColor4f& color, |
| 2098 | SkBlendMode mode) { |
Michael Ludwig | 390f0cc | 2019-03-19 09:16:38 -0400 | [diff] [blame] | 2099 | TRACE_EVENT0("skia", TRACE_FUNC); |
| 2100 | // Make sure the rect is sorted before passing it along |
| 2101 | this->onDrawEdgeAAQuad(rect.makeSorted(), clip, aaFlags, color, mode); |
| 2102 | } |
| 2103 | |
| 2104 | void SkCanvas::experimental_DrawEdgeAAImageSet(const ImageSetEntry imageSet[], int cnt, |
| 2105 | const SkPoint dstClips[], |
| 2106 | const SkMatrix preViewMatrices[], |
| 2107 | const SkPaint* paint, |
| 2108 | SrcRectConstraint constraint) { |
| 2109 | TRACE_EVENT0("skia", TRACE_FUNC); |
| 2110 | this->onDrawEdgeAAImageSet(imageSet, cnt, dstClips, preViewMatrices, paint, constraint); |
| 2111 | } |
| 2112 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2113 | ////////////////////////////////////////////////////////////////////////////// |
| 2114 | // These are the virtual drawing methods |
| 2115 | ////////////////////////////////////////////////////////////////////////////// |
| 2116 | |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 2117 | void SkCanvas::onDiscard() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 2118 | if (fSurfaceBase) { |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 2119 | fSurfaceBase->aboutToDraw(SkSurface::kDiscard_ContentChangeMode); |
| 2120 | } |
| 2121 | } |
| 2122 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2123 | void SkCanvas::onDrawPaint(const SkPaint& paint) { |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 2124 | this->internalDrawPaint(paint); |
| 2125 | } |
| 2126 | |
| 2127 | void SkCanvas::internalDrawPaint(const SkPaint& paint) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2128 | DRAW_BEGIN_CHECK_COMPLETE_OVERWRITE(paint, nullptr, false) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2129 | |
| 2130 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2131 | iter.fDevice->drawPaint(draw.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2132 | } |
| 2133 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2134 | DRAW_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2135 | } |
| 2136 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2137 | void SkCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[], |
| 2138 | const SkPaint& paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2139 | if ((long)count <= 0) { |
| 2140 | return; |
| 2141 | } |
| 2142 | |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2143 | SkRect r; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2144 | const SkRect* bounds = nullptr; |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 2145 | if (paint.canComputeFastBounds()) { |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 2146 | // special-case 2 points (common for drawing a single line) |
| 2147 | if (2 == count) { |
| 2148 | r.set(pts[0], pts[1]); |
| 2149 | } else { |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 2150 | r.setBounds(pts, SkToInt(count)); |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 2151 | } |
Jim Van Verth | 5d32b83 | 2018-02-21 11:14:32 -0500 | [diff] [blame] | 2152 | if (!r.isFinite()) { |
| 2153 | return; |
| 2154 | } |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2155 | SkRect storage; |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2156 | if (this->quickReject(paint.computeFastStrokeBounds(r, &storage))) { |
| 2157 | return; |
| 2158 | } |
| 2159 | bounds = &r; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2160 | } |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 2161 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2162 | SkASSERT(pts != nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2163 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2164 | DRAW_BEGIN(paint, bounds) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2165 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2166 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2167 | iter.fDevice->drawPoints(mode, count, pts, draw.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2168 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2169 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2170 | DRAW_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2171 | } |
| 2172 | |
reed | 4a16717 | 2016-08-18 17:15:25 -0700 | [diff] [blame] | 2173 | static bool needs_autodrawlooper(SkCanvas* canvas, const SkPaint& paint) { |
Mike Reed | 9dc0b9e | 2019-07-29 17:52:48 -0400 | [diff] [blame] | 2174 | return paint.getImageFilter() != nullptr; |
reed | 4a16717 | 2016-08-18 17:15:25 -0700 | [diff] [blame] | 2175 | } |
| 2176 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2177 | void SkCanvas::onDrawRect(const SkRect& r, const SkPaint& paint) { |
Brian Osman | 6e3ce40 | 2017-05-17 15:10:18 -0400 | [diff] [blame] | 2178 | SkASSERT(r.isSorted()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2179 | if (paint.canComputeFastBounds()) { |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2180 | SkRect storage; |
Brian Osman | 6e3ce40 | 2017-05-17 15:10:18 -0400 | [diff] [blame] | 2181 | if (this->quickReject(paint.computeFastBounds(r, &storage))) { |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2182 | return; |
| 2183 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2184 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2185 | |
reed | 4a16717 | 2016-08-18 17:15:25 -0700 | [diff] [blame] | 2186 | if (needs_autodrawlooper(this, paint)) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2187 | DRAW_BEGIN_CHECK_COMPLETE_OVERWRITE(paint, &r, false) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2188 | |
reed | 4a16717 | 2016-08-18 17:15:25 -0700 | [diff] [blame] | 2189 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2190 | iter.fDevice->drawRect(r, draw.paint()); |
reed | 4a16717 | 2016-08-18 17:15:25 -0700 | [diff] [blame] | 2191 | } |
| 2192 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2193 | DRAW_END |
Mike Reed | 49f8da0 | 2018-08-27 10:48:52 -0400 | [diff] [blame] | 2194 | } else if (!paint.nothingToDraw()) { |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2195 | this->predrawNotify(&r, &paint, false); |
reed | 4a16717 | 2016-08-18 17:15:25 -0700 | [diff] [blame] | 2196 | SkDrawIter iter(this); |
| 2197 | while (iter.next()) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 2198 | iter.fDevice->drawRect(r, paint); |
reed | 4a16717 | 2016-08-18 17:15:25 -0700 | [diff] [blame] | 2199 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2200 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2201 | } |
| 2202 | |
msarett | 44df651 | 2016-08-25 13:54:30 -0700 | [diff] [blame] | 2203 | void SkCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) { |
msarett | 44df651 | 2016-08-25 13:54:30 -0700 | [diff] [blame] | 2204 | SkRect regionRect = SkRect::Make(region.getBounds()); |
msarett | 44df651 | 2016-08-25 13:54:30 -0700 | [diff] [blame] | 2205 | if (paint.canComputeFastBounds()) { |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2206 | SkRect storage; |
msarett | 44df651 | 2016-08-25 13:54:30 -0700 | [diff] [blame] | 2207 | if (this->quickReject(paint.computeFastBounds(regionRect, &storage))) { |
| 2208 | return; |
| 2209 | } |
msarett | 44df651 | 2016-08-25 13:54:30 -0700 | [diff] [blame] | 2210 | } |
| 2211 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2212 | DRAW_BEGIN(paint, ®ionRect) |
msarett | 44df651 | 2016-08-25 13:54:30 -0700 | [diff] [blame] | 2213 | |
| 2214 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2215 | iter.fDevice->drawRegion(region, draw.paint()); |
msarett | 44df651 | 2016-08-25 13:54:30 -0700 | [diff] [blame] | 2216 | } |
| 2217 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2218 | DRAW_END |
msarett | 44df651 | 2016-08-25 13:54:30 -0700 | [diff] [blame] | 2219 | } |
| 2220 | |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 2221 | void SkCanvas::onDrawBehind(const SkPaint& paint) { |
| 2222 | SkIRect bounds; |
| 2223 | SkDeque::Iter iter(fMCStack, SkDeque::Iter::kBack_IterStart); |
| 2224 | for (;;) { |
| 2225 | const MCRec* rec = (const MCRec*)iter.prev(); |
| 2226 | if (!rec) { |
| 2227 | return; // no backimages, so nothing to draw |
| 2228 | } |
| 2229 | if (rec->fBackImage) { |
| 2230 | bounds = SkIRect::MakeXYWH(rec->fBackImage->fLoc.fX, rec->fBackImage->fLoc.fY, |
| 2231 | rec->fBackImage->fImage->width(), |
| 2232 | rec->fBackImage->fImage->height()); |
| 2233 | break; |
| 2234 | } |
| 2235 | } |
| 2236 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2237 | DRAW_BEGIN(paint, nullptr) |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 2238 | |
| 2239 | while (iter.next()) { |
| 2240 | SkBaseDevice* dev = iter.fDevice; |
| 2241 | |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 2242 | dev->save(); |
| 2243 | // We use clipRegion because it is already defined to operate in dev-space |
| 2244 | // (i.e. ignores the ctm). However, it is going to first translate by -origin, |
| 2245 | // but we don't want that, so we undo that before calling in. |
| 2246 | SkRegion rgn(bounds.makeOffset(dev->fOrigin.fX, dev->fOrigin.fY)); |
| 2247 | dev->clipRegion(rgn, SkClipOp::kIntersect); |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2248 | dev->drawPaint(draw.paint()); |
Mike Reed | 9adc82c | 2019-04-23 10:28:13 -0400 | [diff] [blame] | 2249 | dev->restore(fMCRec->fMatrix); |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 2250 | } |
| 2251 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2252 | DRAW_END |
Mike Reed | d567408 | 2019-04-19 15:00:47 -0400 | [diff] [blame] | 2253 | } |
| 2254 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2255 | void SkCanvas::onDrawOval(const SkRect& oval, const SkPaint& paint) { |
Brian Osman | 6e3ce40 | 2017-05-17 15:10:18 -0400 | [diff] [blame] | 2256 | SkASSERT(oval.isSorted()); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2257 | if (paint.canComputeFastBounds()) { |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2258 | SkRect storage; |
Brian Osman | 6e3ce40 | 2017-05-17 15:10:18 -0400 | [diff] [blame] | 2259 | if (this->quickReject(paint.computeFastBounds(oval, &storage))) { |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2260 | return; |
| 2261 | } |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2262 | } |
skia.committer@gmail.com | 306ab9d | 2012-12-13 02:01:33 +0000 | [diff] [blame] | 2263 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2264 | DRAW_BEGIN(paint, &oval) |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 2265 | |
| 2266 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2267 | iter.fDevice->drawOval(oval, draw.paint()); |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 2268 | } |
| 2269 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2270 | DRAW_END |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
bsalomon | ac3aa24 | 2016-08-19 11:25:19 -0700 | [diff] [blame] | 2273 | void SkCanvas::onDrawArc(const SkRect& oval, SkScalar startAngle, |
| 2274 | SkScalar sweepAngle, bool useCenter, |
| 2275 | const SkPaint& paint) { |
Brian Osman | 6e3ce40 | 2017-05-17 15:10:18 -0400 | [diff] [blame] | 2276 | SkASSERT(oval.isSorted()); |
bsalomon | ac3aa24 | 2016-08-19 11:25:19 -0700 | [diff] [blame] | 2277 | if (paint.canComputeFastBounds()) { |
| 2278 | SkRect storage; |
| 2279 | // Note we're using the entire oval as the bounds. |
Brian Osman | 6e3ce40 | 2017-05-17 15:10:18 -0400 | [diff] [blame] | 2280 | if (this->quickReject(paint.computeFastBounds(oval, &storage))) { |
bsalomon | ac3aa24 | 2016-08-19 11:25:19 -0700 | [diff] [blame] | 2281 | return; |
| 2282 | } |
bsalomon | ac3aa24 | 2016-08-19 11:25:19 -0700 | [diff] [blame] | 2283 | } |
| 2284 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2285 | DRAW_BEGIN(paint, &oval) |
bsalomon | ac3aa24 | 2016-08-19 11:25:19 -0700 | [diff] [blame] | 2286 | |
| 2287 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2288 | iter.fDevice->drawArc(oval, startAngle, sweepAngle, useCenter, draw.paint()); |
bsalomon | ac3aa24 | 2016-08-19 11:25:19 -0700 | [diff] [blame] | 2289 | } |
| 2290 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2291 | DRAW_END |
bsalomon | ac3aa24 | 2016-08-19 11:25:19 -0700 | [diff] [blame] | 2292 | } |
| 2293 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2294 | void SkCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) { |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2295 | if (paint.canComputeFastBounds()) { |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2296 | SkRect storage; |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2297 | if (this->quickReject(paint.computeFastBounds(rrect.getBounds(), &storage))) { |
| 2298 | return; |
| 2299 | } |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | if (rrect.isRect()) { |
| 2303 | // call the non-virtual version |
| 2304 | this->SkCanvas::drawRect(rrect.getBounds(), paint); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2305 | return; |
| 2306 | } else if (rrect.isOval()) { |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2307 | // call the non-virtual version |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2308 | this->SkCanvas::drawOval(rrect.getBounds(), paint); |
| 2309 | return; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2310 | } |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2311 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2312 | DRAW_BEGIN(paint, &rrect.getBounds()) |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2313 | |
| 2314 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2315 | iter.fDevice->drawRRect(rrect, draw.paint()); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2318 | DRAW_END |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2321 | void SkCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, const SkPaint& paint) { |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 2322 | if (paint.canComputeFastBounds()) { |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2323 | SkRect storage; |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2324 | if (this->quickReject(paint.computeFastBounds(outer.getBounds(), &storage))) { |
| 2325 | return; |
| 2326 | } |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 2327 | } |
skia.committer@gmail.com | 25c7127 | 2014-02-21 03:02:02 +0000 | [diff] [blame] | 2328 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2329 | DRAW_BEGIN(paint, &outer.getBounds()) |
skia.committer@gmail.com | 25c7127 | 2014-02-21 03:02:02 +0000 | [diff] [blame] | 2330 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 2331 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2332 | iter.fDevice->drawDRRect(outer, inner, draw.paint()); |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 2333 | } |
skia.committer@gmail.com | 25c7127 | 2014-02-21 03:02:02 +0000 | [diff] [blame] | 2334 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2335 | DRAW_END |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 2336 | } |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2337 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2338 | void SkCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) { |
reed@google.com | 9364511 | 2012-07-26 16:11:47 +0000 | [diff] [blame] | 2339 | if (!path.isFinite()) { |
| 2340 | return; |
| 2341 | } |
| 2342 | |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2343 | const SkRect& pathBounds = path.getBounds(); |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 2344 | if (!path.isInverseFillType() && paint.canComputeFastBounds()) { |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2345 | SkRect storage; |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2346 | if (this->quickReject(paint.computeFastBounds(pathBounds, &storage))) { |
| 2347 | return; |
| 2348 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2349 | } |
commit-bot@chromium.org | 0b45dc4 | 2014-02-21 05:42:57 +0000 | [diff] [blame] | 2350 | |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2351 | if (pathBounds.width() <= 0 && pathBounds.height() <= 0) { |
commit-bot@chromium.org | 6803c21 | 2014-05-04 18:08:27 +0000 | [diff] [blame] | 2352 | if (path.isInverseFillType()) { |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 2353 | this->internalDrawPaint(paint); |
caryclark | 6651a32 | 2015-09-09 13:20:49 -0700 | [diff] [blame] | 2354 | return; |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 2355 | } |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 2356 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2357 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2358 | DRAW_BEGIN(paint, &pathBounds) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2359 | |
| 2360 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2361 | iter.fDevice->drawPath(path, draw.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2362 | } |
| 2363 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2364 | DRAW_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2367 | bool SkCanvas::canDrawBitmapAsSprite(SkScalar x, SkScalar y, int w, int h, const SkPaint& paint) { |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2368 | if (!paint.getImageFilter()) { |
| 2369 | return false; |
| 2370 | } |
| 2371 | |
| 2372 | const SkMatrix& ctm = this->getTotalMatrix(); |
fmalita | c7e211a | 2016-01-07 10:34:46 -0800 | [diff] [blame] | 2373 | if (!SkTreatAsSprite(ctm, SkISize::Make(w, h), paint)) { |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2374 | return false; |
| 2375 | } |
| 2376 | |
| 2377 | // Currently we can only use the filterSprite code if we are clipped to the bitmap's bounds. |
| 2378 | // Once we can filter and the filter will return a result larger than itself, we should be |
| 2379 | // able to remove this constraint. |
| 2380 | // skbug.com/4526 |
| 2381 | // |
| 2382 | SkPoint pt; |
| 2383 | ctm.mapXY(x, y, &pt); |
| 2384 | SkIRect ir = SkIRect::MakeXYWH(SkScalarRoundToInt(pt.x()), SkScalarRoundToInt(pt.y()), w, h); |
| 2385 | return ir.contains(fMCRec->fRasterClip.getBounds()); |
| 2386 | } |
| 2387 | |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2388 | // Given storage for a real paint, and an optional paint parameter, clean-up the param (if non-null) |
| 2389 | // given the drawing semantics for drawImage/bitmap (skbug.com/7804) and return it, or the original |
| 2390 | // null. |
| 2391 | static const SkPaint* init_image_paint(SkPaint* real, const SkPaint* paintParam) { |
| 2392 | if (paintParam) { |
| 2393 | *real = *paintParam; |
| 2394 | real->setStyle(SkPaint::kFill_Style); |
| 2395 | real->setPathEffect(nullptr); |
| 2396 | paintParam = real; |
| 2397 | } |
| 2398 | return paintParam; |
| 2399 | } |
| 2400 | |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2401 | void SkCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) { |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2402 | SkPaint realPaint; |
| 2403 | paint = init_image_paint(&realPaint, paint); |
| 2404 | |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2405 | SkRect bounds = SkRect::MakeXYWH(x, y, |
| 2406 | SkIntToScalar(image->width()), SkIntToScalar(image->height())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2407 | if (nullptr == paint || paint->canComputeFastBounds()) { |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2408 | SkRect tmp = bounds; |
| 2409 | if (paint) { |
| 2410 | paint->computeFastBounds(tmp, &tmp); |
| 2411 | } |
| 2412 | if (this->quickReject(tmp)) { |
| 2413 | return; |
| 2414 | } |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2415 | } |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2416 | // At this point we need a real paint object. If the caller passed null, then we should |
| 2417 | // use realPaint (in its default state). If the caller did pass a paint, then we have copied |
| 2418 | // (and modified) it in realPaint. Thus either way, "realPaint" is what we want to use. |
| 2419 | paint = &realPaint; |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2420 | |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2421 | sk_sp<SkSpecialImage> special; |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 2422 | bool drawAsSprite = this->canDrawBitmapAsSprite(x, y, image->width(), image->height(), |
| 2423 | *paint); |
| 2424 | if (drawAsSprite && paint->getImageFilter()) { |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2425 | special = this->getDevice()->makeSpecial(image); |
| 2426 | if (!special) { |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 2427 | drawAsSprite = false; |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 2428 | } |
| 2429 | } |
| 2430 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2431 | DRAW_BEGIN_DRAWBITMAP(*paint, drawAsSprite, &bounds) |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2432 | |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2433 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2434 | const SkPaint& pnt = draw.paint(); |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2435 | if (special) { |
| 2436 | SkPoint pt; |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 2437 | iter.fDevice->ctm().mapXY(x, y, &pt); |
| 2438 | iter.fDevice->drawSpecial(special.get(), |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2439 | SkScalarRoundToInt(pt.fX), |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 2440 | SkScalarRoundToInt(pt.fY), pnt, |
| 2441 | nullptr, SkMatrix::I()); |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2442 | } else { |
Michael Ludwig | b7d64b9 | 2019-02-11 11:09:15 -0500 | [diff] [blame] | 2443 | iter.fDevice->drawImageRect( |
| 2444 | image, nullptr, SkRect::MakeXYWH(x, y, image->width(), image->height()), pnt, |
| 2445 | kStrict_SrcRectConstraint); |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2446 | } |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2447 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2448 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2449 | DRAW_END |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 2450 | } |
| 2451 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2452 | void SkCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst, |
reed | 562fe47 | 2015-07-28 07:35:14 -0700 | [diff] [blame] | 2453 | const SkPaint* paint, SrcRectConstraint constraint) { |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2454 | SkPaint realPaint; |
| 2455 | paint = init_image_paint(&realPaint, paint); |
| 2456 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2457 | if (nullptr == paint || paint->canComputeFastBounds()) { |
senorblanco | c41e7e1 | 2015-12-07 12:51:30 -0800 | [diff] [blame] | 2458 | SkRect storage = dst; |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2459 | if (paint) { |
| 2460 | paint->computeFastBounds(dst, &storage); |
| 2461 | } |
| 2462 | if (this->quickReject(storage)) { |
| 2463 | return; |
| 2464 | } |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2465 | } |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2466 | paint = &realPaint; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2467 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2468 | DRAW_BEGIN_CHECK_COMPLETE_OVERWRITE(*paint, &dst, image->isOpaque()) |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2469 | |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2470 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2471 | iter.fDevice->drawImageRect(image, src, dst, draw.paint(), constraint); |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2472 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2473 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2474 | DRAW_END |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 2475 | } |
| 2476 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2477 | void SkCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y, const SkPaint* paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2478 | SkDEBUGCODE(bitmap.validate();) |
| 2479 | |
reed | 3336697 | 2015-10-08 09:22:02 -0700 | [diff] [blame] | 2480 | if (bitmap.drawsNothing()) { |
| 2481 | return; |
| 2482 | } |
| 2483 | |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2484 | SkPaint realPaint; |
| 2485 | init_image_paint(&realPaint, paint); |
| 2486 | paint = &realPaint; |
reed | 3336697 | 2015-10-08 09:22:02 -0700 | [diff] [blame] | 2487 | |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2488 | SkRect bounds; |
| 2489 | bitmap.getBounds(&bounds); |
| 2490 | bounds.offset(x, y); |
| 2491 | bool canFastBounds = paint->canComputeFastBounds(); |
| 2492 | if (canFastBounds) { |
| 2493 | SkRect storage; |
| 2494 | if (this->quickReject(paint->computeFastBounds(bounds, &storage))) { |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2495 | return; |
| 2496 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2497 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2498 | |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2499 | sk_sp<SkSpecialImage> special; |
Mike Reed | 822128b | 2017-02-28 16:41:03 -0500 | [diff] [blame] | 2500 | bool drawAsSprite = canFastBounds && this->canDrawBitmapAsSprite(x, y, bitmap.width(), |
| 2501 | bitmap.height(), *paint); |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 2502 | if (drawAsSprite && paint->getImageFilter()) { |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2503 | special = this->getDevice()->makeSpecial(bitmap); |
| 2504 | if (!special) { |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 2505 | drawAsSprite = false; |
| 2506 | } |
| 2507 | } |
| 2508 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2509 | DRAW_BEGIN_DRAWBITMAP(*paint, drawAsSprite, &bounds) |
reed | 3336697 | 2015-10-08 09:22:02 -0700 | [diff] [blame] | 2510 | |
| 2511 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2512 | const SkPaint& pnt = draw.paint(); |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2513 | if (special) { |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2514 | SkPoint pt; |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 2515 | iter.fDevice->ctm().mapXY(x, y, &pt); |
| 2516 | iter.fDevice->drawSpecial(special.get(), |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2517 | SkScalarRoundToInt(pt.fX), |
Florin Malita | 53f77bd | 2017-04-28 13:48:37 -0400 | [diff] [blame] | 2518 | SkScalarRoundToInt(pt.fY), pnt, |
| 2519 | nullptr, SkMatrix::I()); |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2520 | } else { |
Michael Ludwig | b7d64b9 | 2019-02-11 11:09:15 -0500 | [diff] [blame] | 2521 | SkRect fullImage = SkRect::MakeWH(bitmap.width(), bitmap.height()); |
| 2522 | iter.fDevice->drawBitmapRect(bitmap, &fullImage, fullImage.makeOffset(x, y), pnt, |
| 2523 | kStrict_SrcRectConstraint); |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2524 | } |
reed | 3336697 | 2015-10-08 09:22:02 -0700 | [diff] [blame] | 2525 | } |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 2526 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2527 | DRAW_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2530 | // 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] | 2531 | void SkCanvas::internalDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 2532 | const SkRect& dst, const SkPaint* paint, |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 2533 | SrcRectConstraint constraint) { |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 2534 | if (bitmap.drawsNothing() || dst.isEmpty()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2535 | return; |
| 2536 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 2537 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2538 | if (nullptr == paint || paint->canComputeFastBounds()) { |
senorblanco | c41e7e1 | 2015-12-07 12:51:30 -0800 | [diff] [blame] | 2539 | SkRect storage; |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2540 | if (this->quickReject(paint ? paint->computeFastBounds(dst, &storage) : dst)) { |
| 2541 | return; |
| 2542 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2543 | } |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 2544 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 2545 | SkLazyPaint lazy; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2546 | if (nullptr == paint) { |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 2547 | paint = lazy.init(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2548 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 2549 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2550 | DRAW_BEGIN_CHECK_COMPLETE_OVERWRITE(*paint, &dst, bitmap.isOpaque()) |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 2551 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 2552 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2553 | iter.fDevice->drawBitmapRect(bitmap, src, dst, draw.paint(), constraint); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 2554 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 2555 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2556 | DRAW_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2557 | } |
| 2558 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2559 | void SkCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst, |
reed | 562fe47 | 2015-07-28 07:35:14 -0700 | [diff] [blame] | 2560 | const SkPaint* paint, SrcRectConstraint constraint) { |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2561 | SkDEBUGCODE(bitmap.validate();) |
reed | 562fe47 | 2015-07-28 07:35:14 -0700 | [diff] [blame] | 2562 | this->internalDrawBitmapRect(bitmap, src, dst, paint, constraint); |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2563 | } |
| 2564 | |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2565 | void SkCanvas::onDrawImageNine(const SkImage* image, const SkIRect& center, const SkRect& dst, |
| 2566 | const SkPaint* paint) { |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2567 | SkPaint realPaint; |
| 2568 | paint = init_image_paint(&realPaint, paint); |
| 2569 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2570 | if (nullptr == paint || paint->canComputeFastBounds()) { |
senorblanco | c41e7e1 | 2015-12-07 12:51:30 -0800 | [diff] [blame] | 2571 | SkRect storage; |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2572 | if (this->quickReject(paint ? paint->computeFastBounds(dst, &storage) : dst)) { |
| 2573 | return; |
| 2574 | } |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 2575 | } |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2576 | paint = &realPaint; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2577 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2578 | DRAW_BEGIN(*paint, &dst) |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2579 | |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2580 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2581 | iter.fDevice->drawImageNine(image, center, dst, draw.paint()); |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2582 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2583 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2584 | DRAW_END |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2587 | void SkCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst, |
| 2588 | const SkPaint* paint) { |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2589 | SkDEBUGCODE(bitmap.validate();) |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2590 | SkPaint realPaint; |
| 2591 | paint = init_image_paint(&realPaint, paint); |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2592 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2593 | if (nullptr == paint || paint->canComputeFastBounds()) { |
senorblanco | c41e7e1 | 2015-12-07 12:51:30 -0800 | [diff] [blame] | 2594 | SkRect storage; |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2595 | if (this->quickReject(paint ? paint->computeFastBounds(dst, &storage) : dst)) { |
| 2596 | return; |
| 2597 | } |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2598 | } |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2599 | paint = &realPaint; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2600 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2601 | DRAW_BEGIN(*paint, &dst) |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2602 | |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2603 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2604 | iter.fDevice->drawBitmapNine(bitmap, center, dst, draw.paint()); |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2605 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2606 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2607 | DRAW_END |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2610 | void SkCanvas::onDrawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst, |
| 2611 | const SkPaint* paint) { |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2612 | SkPaint realPaint; |
| 2613 | paint = init_image_paint(&realPaint, paint); |
| 2614 | |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2615 | if (nullptr == paint || paint->canComputeFastBounds()) { |
| 2616 | SkRect storage; |
| 2617 | if (this->quickReject(paint ? paint->computeFastBounds(dst, &storage) : dst)) { |
| 2618 | return; |
| 2619 | } |
| 2620 | } |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2621 | paint = &realPaint; |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2622 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2623 | DRAW_BEGIN(*paint, &dst) |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2624 | |
| 2625 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2626 | iter.fDevice->drawImageLattice(image, lattice, dst, draw.paint()); |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2627 | } |
| 2628 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2629 | DRAW_END |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2630 | } |
| 2631 | |
| 2632 | void SkCanvas::onDrawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice, |
| 2633 | const SkRect& dst, const SkPaint* paint) { |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2634 | SkPaint realPaint; |
| 2635 | paint = init_image_paint(&realPaint, paint); |
| 2636 | |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2637 | if (nullptr == paint || paint->canComputeFastBounds()) { |
| 2638 | SkRect storage; |
| 2639 | if (this->quickReject(paint ? paint->computeFastBounds(dst, &storage) : dst)) { |
| 2640 | return; |
| 2641 | } |
| 2642 | } |
Mike Reed | f441cfc | 2018-04-11 14:50:16 -0400 | [diff] [blame] | 2643 | paint = &realPaint; |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2644 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2645 | DRAW_BEGIN(*paint, &dst) |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2646 | |
| 2647 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2648 | iter.fDevice->drawBitmapLattice(bitmap, lattice, dst, draw.paint()); |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2649 | } |
| 2650 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2651 | DRAW_END |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2652 | } |
| 2653 | |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2654 | void SkCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 2655 | const SkPaint& paint) { |
fmalita | 85d5eb9 | 2015-03-04 11:20:12 -0800 | [diff] [blame] | 2656 | SkRect storage; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2657 | const SkRect* bounds = nullptr; |
fmalita | 19653d1 | 2014-10-16 11:53:30 -0700 | [diff] [blame] | 2658 | if (paint.canComputeFastBounds()) { |
fmalita | 85d5eb9 | 2015-03-04 11:20:12 -0800 | [diff] [blame] | 2659 | storage = blob->bounds().makeOffset(x, y); |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2660 | SkRect tmp; |
| 2661 | if (this->quickReject(paint.computeFastBounds(storage, &tmp))) { |
| 2662 | return; |
| 2663 | } |
| 2664 | bounds = &storage; |
fmalita | 7ba7aa7 | 2014-08-29 09:46:36 -0700 | [diff] [blame] | 2665 | } |
| 2666 | |
fmalita | 024f996 | 2015-03-03 19:08:17 -0800 | [diff] [blame] | 2667 | // We cannot filter in the looper as we normally do, because the paint is |
| 2668 | // incomplete at this point (text-related attributes are embedded within blob run paints). |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2669 | DRAW_BEGIN(paint, bounds) |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2670 | |
fmalita | aa1b912 | 2014-08-28 14:32:24 -0700 | [diff] [blame] | 2671 | while (iter.next()) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2672 | fScratchGlyphRunBuilder->drawTextBlob(draw.paint(), *blob, {x, y}, iter.fDevice); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2673 | } |
| 2674 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2675 | DRAW_END |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2676 | } |
| 2677 | |
Mike Reed | 358fcad | 2018-11-23 15:27:51 -0500 | [diff] [blame] | 2678 | // These call the (virtual) onDraw... method |
Mike Reed | 7d1eb33 | 2018-12-04 17:35:56 -0500 | [diff] [blame] | 2679 | void SkCanvas::drawSimpleText(const void* text, size_t byteLength, SkTextEncoding encoding, |
Mike Reed | 358fcad | 2018-11-23 15:27:51 -0500 | [diff] [blame] | 2680 | SkScalar x, SkScalar y, const SkFont& font, const SkPaint& paint) { |
| 2681 | TRACE_EVENT0("skia", TRACE_FUNC); |
| 2682 | if (byteLength) { |
| 2683 | sk_msan_assert_initialized(text, SkTAddOffset<const void>(text, byteLength)); |
Mike Reed | 704a342 | 2018-12-06 15:44:14 -0500 | [diff] [blame] | 2684 | this->drawTextBlob(SkTextBlob::MakeFromText(text, byteLength, font, encoding), x, y, paint); |
Mike Reed | 358fcad | 2018-11-23 15:27:51 -0500 | [diff] [blame] | 2685 | } |
| 2686 | } |
Mike Reed | 4f81bb7 | 2019-01-23 09:23:00 -0500 | [diff] [blame] | 2687 | |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2688 | void SkCanvas::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 2689 | const SkPaint& paint) { |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 2690 | TRACE_EVENT0("skia", TRACE_FUNC); |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 2691 | RETURN_ON_NULL(blob); |
Mike Reed | 74d6e11 | 2018-01-23 13:06:12 -0500 | [diff] [blame] | 2692 | RETURN_ON_FALSE(blob->bounds().makeOffset(x, y).isFinite()); |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 2693 | this->onDrawTextBlob(blob, x, y, paint); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2694 | } |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2695 | |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 2696 | void SkCanvas::onDrawVerticesObject(const SkVertices* vertices, const SkVertices::Bone bones[], |
Ruiqi Mao | f510149 | 2018-06-29 14:32:21 -0400 | [diff] [blame] | 2697 | int boneCount, SkBlendMode bmode, const SkPaint& paint) { |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2698 | DRAW_BEGIN(paint, nullptr) |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 2699 | |
| 2700 | while (iter.next()) { |
| 2701 | // In the common case of one iteration we could std::move vertices here. |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2702 | iter.fDevice->drawVertices(vertices, bones, boneCount, bmode, draw.paint()); |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 2703 | } |
| 2704 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2705 | DRAW_END |
Brian Salomon | 199fb87 | 2017-02-06 09:41:10 -0500 | [diff] [blame] | 2706 | } |
| 2707 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 2708 | void SkCanvas::drawPatch(const SkPoint cubics[12], const SkColor colors[4], |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 2709 | const SkPoint texCoords[4], SkBlendMode bmode, |
| 2710 | const SkPaint& paint) { |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 2711 | TRACE_EVENT0("skia", TRACE_FUNC); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2712 | if (nullptr == cubics) { |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 2713 | return; |
| 2714 | } |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 2715 | |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 2716 | this->onDrawPatch(cubics, colors, texCoords, bmode, paint); |
msarett | 9340c26 | 2016-09-22 05:20:21 -0700 | [diff] [blame] | 2717 | } |
| 2718 | |
| 2719 | void SkCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 2720 | const SkPoint texCoords[4], SkBlendMode bmode, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 2721 | const SkPaint& paint) { |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2722 | // Since a patch is always within the convex hull of the control points, we discard it when its |
| 2723 | // bounding rectangle is completely outside the current clip. |
| 2724 | SkRect bounds; |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 2725 | bounds.setBounds(cubics, SkPatchUtils::kNumCtrlPts); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2726 | if (this->quickReject(bounds)) { |
| 2727 | return; |
| 2728 | } |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 2729 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2730 | DRAW_BEGIN(paint, nullptr) |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 2731 | |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2732 | while (iter.next()) { |
Brian Osman | e0a9962 | 2018-07-09 16:12:27 -0400 | [diff] [blame] | 2733 | iter.fDevice->drawPatch(cubics, colors, texCoords, bmode, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2734 | } |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 2735 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2736 | DRAW_END |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2737 | } |
| 2738 | |
reed | a8db728 | 2015-07-07 10:22:31 -0700 | [diff] [blame] | 2739 | void SkCanvas::drawDrawable(SkDrawable* dr, SkScalar x, SkScalar y) { |
Derek Sollenberger | edf3dc0 | 2017-08-16 10:35:28 -0400 | [diff] [blame] | 2740 | #ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 2741 | TRACE_EVENT0("skia", TRACE_FUNC); |
Derek Sollenberger | edf3dc0 | 2017-08-16 10:35:28 -0400 | [diff] [blame] | 2742 | #endif |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 2743 | RETURN_ON_NULL(dr); |
| 2744 | if (x || y) { |
| 2745 | SkMatrix matrix = SkMatrix::MakeTrans(x, y); |
| 2746 | this->onDrawDrawable(dr, &matrix); |
| 2747 | } else { |
| 2748 | this->onDrawDrawable(dr, nullptr); |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 2749 | } |
| 2750 | } |
| 2751 | |
reed | a8db728 | 2015-07-07 10:22:31 -0700 | [diff] [blame] | 2752 | void SkCanvas::drawDrawable(SkDrawable* dr, const SkMatrix* matrix) { |
Derek Sollenberger | edf3dc0 | 2017-08-16 10:35:28 -0400 | [diff] [blame] | 2753 | #ifndef SK_BUILD_FOR_ANDROID_FRAMEWORK |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 2754 | TRACE_EVENT0("skia", TRACE_FUNC); |
Derek Sollenberger | edf3dc0 | 2017-08-16 10:35:28 -0400 | [diff] [blame] | 2755 | #endif |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 2756 | RETURN_ON_NULL(dr); |
| 2757 | if (matrix && matrix->isIdentity()) { |
| 2758 | matrix = nullptr; |
reed | a8db728 | 2015-07-07 10:22:31 -0700 | [diff] [blame] | 2759 | } |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 2760 | this->onDrawDrawable(dr, matrix); |
reed | a8db728 | 2015-07-07 10:22:31 -0700 | [diff] [blame] | 2761 | } |
| 2762 | |
| 2763 | void SkCanvas::onDrawDrawable(SkDrawable* dr, const SkMatrix* matrix) { |
reed | 2a62e85 | 2016-10-03 10:35:37 -0700 | [diff] [blame] | 2764 | // drawable bounds are no longer reliable (e.g. android displaylist) |
| 2765 | // so don't use them for quick-reject |
Greg Daniel | 9ed1a2c | 2018-10-18 12:43:25 -0400 | [diff] [blame] | 2766 | this->getDevice()->drawDrawable(dr, matrix, this); |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 2767 | } |
| 2768 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2769 | void SkCanvas::onDrawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[], |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 2770 | const SkColor colors[], int count, SkBlendMode bmode, |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2771 | const SkRect* cull, const SkPaint* paint) { |
| 2772 | if (cull && this->quickReject(*cull)) { |
| 2773 | return; |
| 2774 | } |
| 2775 | |
| 2776 | SkPaint pnt; |
| 2777 | if (paint) { |
| 2778 | pnt = *paint; |
| 2779 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2780 | |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2781 | DRAW_BEGIN(pnt, nullptr) |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2782 | while (iter.next()) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 2783 | iter.fDevice->drawAtlas(atlas, xform, tex, colors, count, bmode, pnt); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2784 | } |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2785 | DRAW_END |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2786 | } |
| 2787 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 2788 | void SkCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) { |
| 2789 | SkASSERT(key); |
| 2790 | |
| 2791 | SkPaint paint; |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2792 | DRAW_BEGIN(paint, nullptr) |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 2793 | while (iter.next()) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 2794 | iter.fDevice->drawAnnotation(rect, key, value); |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 2795 | } |
Mike Reed | 3899239 | 2019-07-30 10:48:15 -0400 | [diff] [blame] | 2796 | DRAW_END |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 2797 | } |
| 2798 | |
Michael Ludwig | a595f86 | 2019-08-27 15:25:49 -0400 | [diff] [blame] | 2799 | void SkCanvas::onDrawEdgeAAQuad(const SkRect& r, const SkPoint clip[4], QuadAAFlags edgeAA, |
| 2800 | const SkColor4f& color, SkBlendMode mode) { |
Michael Ludwig | 390f0cc | 2019-03-19 09:16:38 -0400 | [diff] [blame] | 2801 | SkASSERT(r.isSorted()); |
| 2802 | |
| 2803 | // If this used a paint, it would be a filled color with blend mode, which does not |
| 2804 | // need to use an autodraw loop, so use SkDrawIter directly. |
| 2805 | if (this->quickReject(r)) { |
| 2806 | return; |
| 2807 | } |
| 2808 | |
Michael Ludwig | a4b4488 | 2019-08-28 14:34:58 -0400 | [diff] [blame] | 2809 | this->predrawNotify(&r, nullptr, false); |
Michael Ludwig | 390f0cc | 2019-03-19 09:16:38 -0400 | [diff] [blame] | 2810 | SkDrawIter iter(this); |
| 2811 | while(iter.next()) { |
| 2812 | iter.fDevice->drawEdgeAAQuad(r, clip, edgeAA, color, mode); |
| 2813 | } |
| 2814 | } |
| 2815 | |
| 2816 | void SkCanvas::onDrawEdgeAAImageSet(const ImageSetEntry imageSet[], int count, |
| 2817 | const SkPoint dstClips[], const SkMatrix preViewMatrices[], |
| 2818 | const SkPaint* paint, SrcRectConstraint constraint) { |
Michael Ludwig | a4b4488 | 2019-08-28 14:34:58 -0400 | [diff] [blame] | 2819 | if (count <= 0) { |
| 2820 | // Nothing to draw |
| 2821 | return; |
| 2822 | } |
| 2823 | |
Michael Ludwig | 390f0cc | 2019-03-19 09:16:38 -0400 | [diff] [blame] | 2824 | SkPaint realPaint; |
| 2825 | init_image_paint(&realPaint, paint); |
| 2826 | |
Michael Ludwig | a4b4488 | 2019-08-28 14:34:58 -0400 | [diff] [blame] | 2827 | // We could calculate the set's dstRect union to always check quickReject(), but we can't reject |
| 2828 | // individual entries and Chromium's occlusion culling already makes it likely that at least one |
| 2829 | // entry will be visible. So, we only calculate the draw bounds when it's trivial (count == 1), |
| 2830 | // or we need it for the autolooper (since it greatly improves image filter perf). |
| 2831 | bool needsAutoLooper = needs_autodrawlooper(this, realPaint); |
| 2832 | bool setBoundsValid = count == 1 || needsAutoLooper; |
| 2833 | SkRect setBounds = imageSet[0].fDstRect; |
| 2834 | if (imageSet[0].fMatrixIndex >= 0) { |
| 2835 | // Account for the per-entry transform that is applied prior to the CTM when drawing |
| 2836 | preViewMatrices[imageSet[0].fMatrixIndex].mapRect(&setBounds); |
Michael Ludwig | 977b50a | 2019-08-27 13:23:20 -0400 | [diff] [blame] | 2837 | } |
Michael Ludwig | a4b4488 | 2019-08-28 14:34:58 -0400 | [diff] [blame] | 2838 | if (needsAutoLooper) { |
| 2839 | for (int i = 1; i < count; ++i) { |
| 2840 | SkRect entryBounds = imageSet[i].fDstRect; |
| 2841 | if (imageSet[i].fMatrixIndex >= 0) { |
| 2842 | preViewMatrices[imageSet[i].fMatrixIndex].mapRect(&entryBounds); |
| 2843 | } |
| 2844 | setBounds.joinPossiblyEmptyRect(entryBounds); |
| 2845 | } |
| 2846 | } |
| 2847 | |
| 2848 | // If we happen to have the draw bounds, though, might as well check quickReject(). |
| 2849 | if (setBoundsValid && realPaint.canComputeFastBounds()) { |
| 2850 | SkRect tmp; |
| 2851 | if (this->quickReject(realPaint.computeFastBounds(setBounds, &tmp))) { |
| 2852 | return; |
| 2853 | } |
| 2854 | } |
| 2855 | |
| 2856 | if (needsAutoLooper) { |
| 2857 | SkASSERT(setBoundsValid); |
| 2858 | DRAW_BEGIN(realPaint, &setBounds) |
| 2859 | while (iter.next()) { |
| 2860 | iter.fDevice->drawEdgeAAImageSet( |
| 2861 | imageSet, count, dstClips, preViewMatrices, draw.paint(), constraint); |
| 2862 | } |
| 2863 | DRAW_END |
| 2864 | } else { |
| 2865 | this->predrawNotify(); |
| 2866 | SkDrawIter iter(this); |
| 2867 | while(iter.next()) { |
| 2868 | iter.fDevice->drawEdgeAAImageSet( |
| 2869 | imageSet, count, dstClips, preViewMatrices, realPaint, constraint); |
| 2870 | } |
| 2871 | } |
Michael Ludwig | 390f0cc | 2019-03-19 09:16:38 -0400 | [diff] [blame] | 2872 | } |
| 2873 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2874 | ////////////////////////////////////////////////////////////////////////////// |
| 2875 | // These methods are NOT virtual, and therefore must call back into virtual |
| 2876 | // methods, rather than actually drawing themselves. |
| 2877 | ////////////////////////////////////////////////////////////////////////////// |
| 2878 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 2879 | void SkCanvas::drawColor(SkColor c, SkBlendMode mode) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2880 | SkPaint paint; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2881 | paint.setColor(c); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 2882 | paint.setBlendMode(mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2883 | this->drawPaint(paint); |
| 2884 | } |
| 2885 | |
| 2886 | void SkCanvas::drawPoint(SkScalar x, SkScalar y, const SkPaint& paint) { |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 2887 | const SkPoint pt = { x, y }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2888 | this->drawPoints(kPoints_PointMode, 1, &pt, paint); |
| 2889 | } |
| 2890 | |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 2891 | void SkCanvas::drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, const SkPaint& paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2892 | SkPoint pts[2]; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2893 | pts[0].set(x0, y0); |
| 2894 | pts[1].set(x1, y1); |
| 2895 | this->drawPoints(kLines_PointMode, 2, pts, paint); |
| 2896 | } |
| 2897 | |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 2898 | void SkCanvas::drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, const SkPaint& paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2899 | if (radius < 0) { |
| 2900 | radius = 0; |
| 2901 | } |
| 2902 | |
| 2903 | SkRect r; |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 2904 | r.setLTRB(cx - radius, cy - radius, cx + radius, cy + radius); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2905 | this->drawOval(r, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2906 | } |
| 2907 | |
| 2908 | void SkCanvas::drawRoundRect(const SkRect& r, SkScalar rx, SkScalar ry, |
| 2909 | const SkPaint& paint) { |
| 2910 | if (rx > 0 && ry > 0) { |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2911 | SkRRect rrect; |
| 2912 | rrect.setRectXY(r, rx, ry); |
| 2913 | this->drawRRect(rrect, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2914 | } else { |
| 2915 | this->drawRect(r, paint); |
| 2916 | } |
| 2917 | } |
| 2918 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2919 | void SkCanvas::drawArc(const SkRect& oval, SkScalar startAngle, |
| 2920 | SkScalar sweepAngle, bool useCenter, |
| 2921 | const SkPaint& paint) { |
Brian Osman | 39c08ac | 2017-07-26 09:36:09 -0400 | [diff] [blame] | 2922 | TRACE_EVENT0("skia", TRACE_FUNC); |
bsalomon | 21af9ca | 2016-08-25 12:29:23 -0700 | [diff] [blame] | 2923 | if (oval.isEmpty() || !sweepAngle) { |
| 2924 | return; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2925 | } |
bsalomon | 21af9ca | 2016-08-25 12:29:23 -0700 | [diff] [blame] | 2926 | this->onDrawArc(oval, startAngle, sweepAngle, useCenter, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2927 | } |
| 2928 | |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 2929 | /////////////////////////////////////////////////////////////////////////////// |
Kevin Lubick | 32dfdbe | 2018-10-18 09:47:01 -0400 | [diff] [blame] | 2930 | #ifdef SK_DISABLE_SKPICTURE |
| 2931 | void SkCanvas::drawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint) {} |
reed | 1c2c441 | 2015-04-30 13:09:24 -0700 | [diff] [blame] | 2932 | |
Kevin Lubick | 32dfdbe | 2018-10-18 09:47:01 -0400 | [diff] [blame] | 2933 | |
| 2934 | void SkCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, |
| 2935 | const SkPaint* paint) {} |
| 2936 | #else |
Mike Klein | 88d9071 | 2018-01-27 17:30:04 +0000 | [diff] [blame] | 2937 | /** |
| 2938 | * This constant is trying to balance the speed of ref'ing a subpicture into a parent picture, |
| 2939 | * against the playback cost of recursing into the subpicture to get at its actual ops. |
| 2940 | * |
| 2941 | * For now we pick a conservatively small value, though measurement (and other heuristics like |
| 2942 | * the type of ops contained) may justify changing this value. |
| 2943 | */ |
| 2944 | #define kMaxPictureOpsToUnrollInsteadOfRef 1 |
| 2945 | |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2946 | void SkCanvas::drawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint) { |
Brian Osman | 5e035ca | 2017-07-26 10:18:57 -0400 | [diff] [blame] | 2947 | TRACE_EVENT0("skia", TRACE_FUNC); |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 2948 | RETURN_ON_NULL(picture); |
| 2949 | |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 2950 | if (matrix && matrix->isIdentity()) { |
| 2951 | matrix = nullptr; |
| 2952 | } |
Mike Klein | 88d9071 | 2018-01-27 17:30:04 +0000 | [diff] [blame] | 2953 | if (picture->approximateOpCount() <= kMaxPictureOpsToUnrollInsteadOfRef) { |
| 2954 | SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect()); |
| 2955 | picture->playback(this); |
| 2956 | } else { |
| 2957 | this->onDrawPicture(picture, matrix, paint); |
| 2958 | } |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2959 | } |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 2960 | |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 2961 | void SkCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, |
| 2962 | const SkPaint* paint) { |
fmalita | d028180 | 2015-08-20 12:08:18 -0700 | [diff] [blame] | 2963 | if (!paint || paint->canComputeFastBounds()) { |
| 2964 | SkRect bounds = picture->cullRect(); |
| 2965 | if (paint) { |
| 2966 | paint->computeFastBounds(bounds, &bounds); |
| 2967 | } |
| 2968 | if (matrix) { |
| 2969 | matrix->mapRect(&bounds); |
| 2970 | } |
| 2971 | if (this->quickReject(bounds)) { |
| 2972 | return; |
| 2973 | } |
| 2974 | } |
| 2975 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 2976 | SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect()); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 2977 | picture->playback(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2978 | } |
Kevin Lubick | 32dfdbe | 2018-10-18 09:47:01 -0400 | [diff] [blame] | 2979 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2980 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2981 | /////////////////////////////////////////////////////////////////////////////// |
| 2982 | /////////////////////////////////////////////////////////////////////////////// |
| 2983 | |
reed | 3aafe11 | 2016-08-18 12:45:34 -0700 | [diff] [blame] | 2984 | SkCanvas::LayerIter::LayerIter(SkCanvas* canvas) { |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 2985 | static_assert(sizeof(fStorage) >= sizeof(SkDrawIter), "fStorage_too_small"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2986 | |
| 2987 | SkASSERT(canvas); |
| 2988 | |
reed | 3aafe11 | 2016-08-18 12:45:34 -0700 | [diff] [blame] | 2989 | fImpl = new (fStorage) SkDrawIter(canvas); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2990 | fDone = !fImpl->next(); |
| 2991 | } |
| 2992 | |
| 2993 | SkCanvas::LayerIter::~LayerIter() { |
| 2994 | fImpl->~SkDrawIter(); |
| 2995 | } |
| 2996 | |
| 2997 | void SkCanvas::LayerIter::next() { |
| 2998 | fDone = !fImpl->next(); |
| 2999 | } |
| 3000 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 3001 | SkBaseDevice* SkCanvas::LayerIter::device() const { |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 3002 | return fImpl->fDevice; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
| 3005 | const SkMatrix& SkCanvas::LayerIter::matrix() const { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 3006 | return fImpl->fDevice->ctm(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3007 | } |
| 3008 | |
| 3009 | const SkPaint& SkCanvas::LayerIter::paint() const { |
| 3010 | const SkPaint* paint = fImpl->getPaint(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 3011 | if (nullptr == paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3012 | paint = &fDefaultPaint; |
| 3013 | } |
| 3014 | return *paint; |
| 3015 | } |
| 3016 | |
Mike Reed | ca37f32 | 2018-03-08 13:22:16 -0500 | [diff] [blame] | 3017 | SkIRect SkCanvas::LayerIter::clipBounds() const { |
| 3018 | return fImpl->fDevice->getGlobalBounds(); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 3019 | } |
| 3020 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3021 | int SkCanvas::LayerIter::x() const { return fImpl->getX(); } |
| 3022 | int SkCanvas::LayerIter::y() const { return fImpl->getY(); } |
justinlin@google.com | 20a550c | 2012-07-27 17:37:12 +0000 | [diff] [blame] | 3023 | |
| 3024 | /////////////////////////////////////////////////////////////////////////////// |
| 3025 | |
Brian Osman | e8a9863 | 2019-04-10 10:26:10 -0400 | [diff] [blame] | 3026 | SkCanvas::ImageSetEntry::ImageSetEntry() = default; |
| 3027 | SkCanvas::ImageSetEntry::~ImageSetEntry() = default; |
| 3028 | SkCanvas::ImageSetEntry::ImageSetEntry(const ImageSetEntry&) = default; |
| 3029 | SkCanvas::ImageSetEntry& SkCanvas::ImageSetEntry::operator=(const ImageSetEntry&) = default; |
| 3030 | |
Michael Ludwig | 390f0cc | 2019-03-19 09:16:38 -0400 | [diff] [blame] | 3031 | SkCanvas::ImageSetEntry::ImageSetEntry(sk_sp<const SkImage> image, const SkRect& srcRect, |
| 3032 | const SkRect& dstRect, int matrixIndex, float alpha, |
| 3033 | unsigned aaFlags, bool hasClip) |
| 3034 | : fImage(std::move(image)) |
| 3035 | , fSrcRect(srcRect) |
| 3036 | , fDstRect(dstRect) |
| 3037 | , fMatrixIndex(matrixIndex) |
| 3038 | , fAlpha(alpha) |
| 3039 | , fAAFlags(aaFlags) |
| 3040 | , fHasClip(hasClip) {} |
| 3041 | |
| 3042 | SkCanvas::ImageSetEntry::ImageSetEntry(sk_sp<const SkImage> image, const SkRect& srcRect, |
| 3043 | const SkRect& dstRect, float alpha, unsigned aaFlags) |
| 3044 | : fImage(std::move(image)) |
| 3045 | , fSrcRect(srcRect) |
| 3046 | , fDstRect(dstRect) |
| 3047 | , fAlpha(alpha) |
| 3048 | , fAAFlags(aaFlags) {} |
| 3049 | |
| 3050 | /////////////////////////////////////////////////////////////////////////////// |
| 3051 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 3052 | std::unique_ptr<SkCanvas> SkCanvas::MakeRasterDirect(const SkImageInfo& info, void* pixels, |
Mike Reed | 12f7734 | 2017-11-08 11:19:52 -0500 | [diff] [blame] | 3053 | size_t rowBytes, const SkSurfaceProps* props) { |
Brian Osman | 431ac56 | 2018-10-10 13:20:38 -0400 | [diff] [blame] | 3054 | if (!SkSurfaceValidateRasterInfo(info, rowBytes)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 3055 | return nullptr; |
commit-bot@chromium.org | 42b0893 | 2014-03-17 02:13:07 +0000 | [diff] [blame] | 3056 | } |
skia.committer@gmail.com | eb849e5 | 2014-03-17 03:02:17 +0000 | [diff] [blame] | 3057 | |
commit-bot@chromium.org | 42b0893 | 2014-03-17 02:13:07 +0000 | [diff] [blame] | 3058 | SkBitmap bitmap; |
| 3059 | if (!bitmap.installPixels(info, pixels, rowBytes)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 3060 | return nullptr; |
commit-bot@chromium.org | 42b0893 | 2014-03-17 02:13:07 +0000 | [diff] [blame] | 3061 | } |
Mike Reed | 12f7734 | 2017-11-08 11:19:52 -0500 | [diff] [blame] | 3062 | |
| 3063 | return props ? |
| 3064 | skstd::make_unique<SkCanvas>(bitmap, *props) : |
| 3065 | skstd::make_unique<SkCanvas>(bitmap); |
commit-bot@chromium.org | 42b0893 | 2014-03-17 02:13:07 +0000 | [diff] [blame] | 3066 | } |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 3067 | |
| 3068 | /////////////////////////////////////////////////////////////////////////////// |
| 3069 | |
Florin Malita | ee424ac | 2016-12-01 12:47:59 -0500 | [diff] [blame] | 3070 | SkNoDrawCanvas::SkNoDrawCanvas(int width, int height) |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 3071 | : INHERITED(SkIRect::MakeWH(width, height)) {} |
Florin Malita | ee424ac | 2016-12-01 12:47:59 -0500 | [diff] [blame] | 3072 | |
Florin Malita | 439ace9 | 2016-12-02 12:05:41 -0500 | [diff] [blame] | 3073 | SkNoDrawCanvas::SkNoDrawCanvas(const SkIRect& bounds) |
Hal Canary | 363a3f8 | 2018-10-04 11:04:48 -0400 | [diff] [blame] | 3074 | : INHERITED(bounds) {} |
Florin Malita | 439ace9 | 2016-12-02 12:05:41 -0500 | [diff] [blame] | 3075 | |
Herb Derby | efe39bc | 2018-05-01 17:06:20 -0400 | [diff] [blame] | 3076 | SkNoDrawCanvas::SkNoDrawCanvas(sk_sp<SkBaseDevice> device) |
Herb Derby | 76d69b4 | 2018-03-15 17:34:40 -0400 | [diff] [blame] | 3077 | : INHERITED(device) {} |
| 3078 | |
Florin Malita | ee424ac | 2016-12-01 12:47:59 -0500 | [diff] [blame] | 3079 | SkCanvas::SaveLayerStrategy SkNoDrawCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) { |
| 3080 | (void)this->INHERITED::getSaveLayerStrategy(rec); |
| 3081 | return kNoLayer_SaveLayerStrategy; |
| 3082 | } |
| 3083 | |
Mike Reed | 148b7fd | 2018-12-18 17:38:18 -0500 | [diff] [blame] | 3084 | bool SkNoDrawCanvas::onDoSaveBehind(const SkRect*) { |
| 3085 | return false; |
| 3086 | } |
| 3087 | |
Florin Malita | ee424ac | 2016-12-01 12:47:59 -0500 | [diff] [blame] | 3088 | /////////////////////////////////////////////////////////////////////////////// |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 3089 | |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 3090 | static_assert((int)SkRegion::kDifference_Op == (int)kDifference_SkClipOp, ""); |
| 3091 | static_assert((int)SkRegion::kIntersect_Op == (int)kIntersect_SkClipOp, ""); |
| 3092 | static_assert((int)SkRegion::kUnion_Op == (int)kUnion_SkClipOp, ""); |
| 3093 | static_assert((int)SkRegion::kXOR_Op == (int)kXOR_SkClipOp, ""); |
| 3094 | static_assert((int)SkRegion::kReverseDifference_Op == (int)kReverseDifference_SkClipOp, ""); |
| 3095 | static_assert((int)SkRegion::kReplace_Op == (int)kReplace_SkClipOp, ""); |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 3096 | |
| 3097 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 3098 | |
| 3099 | SkRasterHandleAllocator::Handle SkCanvas::accessTopRasterHandle() const { |
| 3100 | if (fAllocator && fMCRec->fTopLayer->fDevice) { |
Florin Malita | 713b8ef | 2017-04-28 10:57:24 -0400 | [diff] [blame] | 3101 | const auto& dev = fMCRec->fTopLayer->fDevice; |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 3102 | SkRasterHandleAllocator::Handle handle = dev->getRasterHandle(); |
| 3103 | SkIPoint origin = dev->getOrigin(); |
| 3104 | SkMatrix ctm = this->getTotalMatrix(); |
| 3105 | ctm.preTranslate(SkIntToScalar(-origin.x()), SkIntToScalar(-origin.y())); |
| 3106 | |
| 3107 | SkIRect clip = fMCRec->fRasterClip.getBounds(); |
| 3108 | clip.offset(-origin.x(), -origin.y()); |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 3109 | if (!clip.intersect({0, 0, dev->width(), dev->height()})) { |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 3110 | clip.setEmpty(); |
| 3111 | } |
| 3112 | |
| 3113 | fAllocator->updateHandle(handle, ctm, clip); |
| 3114 | return handle; |
| 3115 | } |
| 3116 | return nullptr; |
| 3117 | } |
| 3118 | |
| 3119 | static bool install(SkBitmap* bm, const SkImageInfo& info, |
| 3120 | const SkRasterHandleAllocator::Rec& rec) { |
Mike Reed | 086a427 | 2017-07-18 10:53:11 -0400 | [diff] [blame] | 3121 | return bm->installPixels(info, rec.fPixels, rec.fRowBytes, rec.fReleaseProc, rec.fReleaseCtx); |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 3122 | } |
| 3123 | |
| 3124 | SkRasterHandleAllocator::Handle SkRasterHandleAllocator::allocBitmap(const SkImageInfo& info, |
| 3125 | SkBitmap* bm) { |
| 3126 | SkRasterHandleAllocator::Rec rec; |
| 3127 | if (!this->allocHandle(info, &rec) || !install(bm, info, rec)) { |
| 3128 | return nullptr; |
| 3129 | } |
| 3130 | return rec.fHandle; |
| 3131 | } |
| 3132 | |
| 3133 | std::unique_ptr<SkCanvas> |
| 3134 | SkRasterHandleAllocator::MakeCanvas(std::unique_ptr<SkRasterHandleAllocator> alloc, |
| 3135 | const SkImageInfo& info, const Rec* rec) { |
Brian Osman | 431ac56 | 2018-10-10 13:20:38 -0400 | [diff] [blame] | 3136 | if (!alloc || !SkSurfaceValidateRasterInfo(info, rec ? rec->fRowBytes : kIgnoreRowBytesValue)) { |
Mike Reed | 356f7c2 | 2017-01-10 11:58:39 -0500 | [diff] [blame] | 3137 | return nullptr; |
| 3138 | } |
| 3139 | |
| 3140 | SkBitmap bm; |
| 3141 | Handle hndl; |
| 3142 | |
| 3143 | if (rec) { |
| 3144 | hndl = install(&bm, info, *rec) ? rec->fHandle : nullptr; |
| 3145 | } else { |
| 3146 | hndl = alloc->allocBitmap(info, &bm); |
| 3147 | } |
| 3148 | return hndl ? std::unique_ptr<SkCanvas>(new SkCanvas(bm, std::move(alloc), hndl)) : nullptr; |
| 3149 | } |
Mike Reed | 7c9c9e4 | 2018-01-03 09:23:34 -0500 | [diff] [blame] | 3150 | |
| 3151 | /////////////////////////////////////////////////////////////////////////////////////////////////// |