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