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