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 | |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 8 | #include "SkBitmapDevice.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 10 | #include "SkCanvasPriv.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 11 | #include "SkClipStack.h" |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 12 | #include "SkColorFilter.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkDraw.h" |
reed | 3cb3840 | 2015-02-06 08:36:15 -0800 | [diff] [blame] | 14 | #include "SkDrawable.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include "SkDrawFilter.h" |
| 16 | #include "SkDrawLooper.h" |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 17 | #include "SkImage.h" |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 18 | #include "SkImage_Base.h" |
senorblanco | 900c367 | 2016-04-27 11:31:23 -0700 | [diff] [blame] | 19 | #include "SkImageFilter.h" |
| 20 | #include "SkImageFilterCache.h" |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 21 | #include "SkLatticeIter.h" |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 22 | #include "SkMakeUnique.h" |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 23 | #include "SkMatrixUtils.h" |
mike@reedtribe.org | 74bb77e | 2012-09-26 02:24:45 +0000 | [diff] [blame] | 24 | #include "SkMetaData.h" |
Florin Malita | ee424ac | 2016-12-01 12:47:59 -0500 | [diff] [blame] | 25 | #include "SkNoDrawCanvas.h" |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 26 | #include "SkNx.h" |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 27 | #include "SkPaintPriv.h" |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 28 | #include "SkPatchUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | #include "SkPicture.h" |
vjiaoblack | b2796fd | 2016-09-09 09:22:39 -0700 | [diff] [blame] | 30 | #include "SkRadialShadowMapShader.h" |
reed@google.com | 0017708 | 2011-10-12 14:34:30 +0000 | [diff] [blame] | 31 | #include "SkRasterClip.h" |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 32 | #include "SkReadPixelsRec.h" |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 33 | #include "SkRRect.h" |
vjiaoblack | 904527d | 2016-08-09 09:32:09 -0700 | [diff] [blame] | 34 | #include "SkShadowPaintFilterCanvas.h" |
| 35 | #include "SkShadowShader.h" |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 36 | #include "SkSmallAllocator.h" |
robertphillips | 4418dba | 2016-03-07 12:45:14 -0800 | [diff] [blame] | 37 | #include "SkSpecialImage.h" |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 38 | #include "SkSurface_Base.h" |
fmalita | 7ba7aa7 | 2014-08-29 09:46:36 -0700 | [diff] [blame] | 39 | #include "SkTextBlob.h" |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 40 | #include "SkTextFormatParams.h" |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 41 | #include "SkTLazy.h" |
danakj | 8f757f5 | 2014-11-04 11:48:43 -0800 | [diff] [blame] | 42 | #include "SkTraceEvent.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 43 | #include <new> |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 45 | #if SK_SUPPORT_GPU |
robertphillips | 7354a4b | 2015-12-16 05:08:27 -0800 | [diff] [blame] | 46 | #include "GrContext.h" |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 47 | #include "GrRenderTarget.h" |
bsalomon | 614d8f9 | 2016-07-13 15:42:40 -0700 | [diff] [blame] | 48 | #include "SkGrPriv.h" |
vjiaoblack | e6f5d56 | 2016-08-25 06:30:23 -0700 | [diff] [blame] | 49 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 50 | #endif |
| 51 | |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 52 | #define RETURN_ON_NULL(ptr) do { if (nullptr == (ptr)) return; } while (0) |
| 53 | |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 54 | /* |
| 55 | * Return true if the drawing this rect would hit every pixels in the canvas. |
| 56 | * |
| 57 | * Returns false if |
| 58 | * - rect does not contain the canvas' bounds |
| 59 | * - paint is not fill |
| 60 | * - paint would blur or otherwise change the coverage of the rect |
| 61 | */ |
| 62 | bool SkCanvas::wouldOverwriteEntireSurface(const SkRect* rect, const SkPaint* paint, |
| 63 | ShaderOverrideOpacity overrideOpacity) const { |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 64 | static_assert((int)SkPaintPriv::kNone_ShaderOverrideOpacity == |
| 65 | (int)kNone_ShaderOverrideOpacity, |
| 66 | "need_matching_enums0"); |
| 67 | static_assert((int)SkPaintPriv::kOpaque_ShaderOverrideOpacity == |
| 68 | (int)kOpaque_ShaderOverrideOpacity, |
| 69 | "need_matching_enums1"); |
| 70 | static_assert((int)SkPaintPriv::kNotOpaque_ShaderOverrideOpacity == |
| 71 | (int)kNotOpaque_ShaderOverrideOpacity, |
| 72 | "need_matching_enums2"); |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 73 | |
| 74 | const SkISize size = this->getBaseLayerSize(); |
| 75 | const SkRect bounds = SkRect::MakeIWH(size.width(), size.height()); |
| 76 | if (!this->getClipStack()->quickContains(bounds)) { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | if (rect) { |
halcanary | c5769b2 | 2016-08-10 07:13:21 -0700 | [diff] [blame] | 81 | if (!this->getTotalMatrix().isScaleTranslate()) { |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 82 | return false; // conservative |
| 83 | } |
halcanary | c5769b2 | 2016-08-10 07:13:21 -0700 | [diff] [blame] | 84 | |
| 85 | SkRect devRect; |
| 86 | this->getTotalMatrix().mapRectScaleTranslate(&devRect, *rect); |
| 87 | if (!devRect.contains(bounds)) { |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 88 | return false; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | if (paint) { |
| 93 | SkPaint::Style paintStyle = paint->getStyle(); |
| 94 | if (!(paintStyle == SkPaint::kFill_Style || |
| 95 | paintStyle == SkPaint::kStrokeAndFill_Style)) { |
| 96 | return false; |
| 97 | } |
| 98 | if (paint->getMaskFilter() || paint->getLooper() |
| 99 | || paint->getPathEffect() || paint->getImageFilter()) { |
| 100 | return false; // conservative |
| 101 | } |
| 102 | } |
| 103 | return SkPaintPriv::Overwrites(paint, (SkPaintPriv::ShaderOverrideOpacity)overrideOpacity); |
| 104 | } |
| 105 | |
| 106 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 107 | |
reed | d990e2f | 2014-12-22 11:58:30 -0800 | [diff] [blame] | 108 | static bool gIgnoreSaveLayerBounds; |
| 109 | void SkCanvas::Internal_Private_SetIgnoreSaveLayerBounds(bool ignore) { |
| 110 | gIgnoreSaveLayerBounds = ignore; |
| 111 | } |
| 112 | bool SkCanvas::Internal_Private_GetIgnoreSaveLayerBounds() { |
| 113 | return gIgnoreSaveLayerBounds; |
| 114 | } |
| 115 | |
reed | 0acf1b4 | 2014-12-22 16:12:38 -0800 | [diff] [blame] | 116 | static bool gTreatSpriteAsBitmap; |
| 117 | void SkCanvas::Internal_Private_SetTreatSpriteAsBitmap(bool spriteAsBitmap) { |
| 118 | gTreatSpriteAsBitmap = spriteAsBitmap; |
| 119 | } |
| 120 | bool SkCanvas::Internal_Private_GetTreatSpriteAsBitmap() { |
| 121 | return gTreatSpriteAsBitmap; |
| 122 | } |
| 123 | |
reed@google.com | da17f75 | 2012-08-16 18:27:05 +0000 | [diff] [blame] | 124 | // experimental for faster tiled drawing... |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 125 | //#define SK_TRACE_SAVERESTORE |
| 126 | |
| 127 | #ifdef SK_TRACE_SAVERESTORE |
| 128 | static int gLayerCounter; |
| 129 | static void inc_layer() { ++gLayerCounter; printf("----- inc layer %d\n", gLayerCounter); } |
| 130 | static void dec_layer() { --gLayerCounter; printf("----- dec layer %d\n", gLayerCounter); } |
| 131 | |
| 132 | static int gRecCounter; |
| 133 | static void inc_rec() { ++gRecCounter; printf("----- inc rec %d\n", gRecCounter); } |
| 134 | static void dec_rec() { --gRecCounter; printf("----- dec rec %d\n", gRecCounter); } |
| 135 | |
| 136 | static int gCanvasCounter; |
| 137 | static void inc_canvas() { ++gCanvasCounter; printf("----- inc canvas %d\n", gCanvasCounter); } |
| 138 | static void dec_canvas() { --gCanvasCounter; printf("----- dec canvas %d\n", gCanvasCounter); } |
| 139 | #else |
| 140 | #define inc_layer() |
| 141 | #define dec_layer() |
| 142 | #define inc_rec() |
| 143 | #define dec_rec() |
| 144 | #define inc_canvas() |
| 145 | #define dec_canvas() |
| 146 | #endif |
| 147 | |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 148 | typedef SkTLazy<SkPaint> SkLazyPaint; |
| 149 | |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 150 | void SkCanvas::predrawNotify(bool willOverwritesEntireSurface) { |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 151 | if (fSurfaceBase) { |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 152 | fSurfaceBase->aboutToDraw(willOverwritesEntireSurface |
| 153 | ? SkSurface::kDiscard_ContentChangeMode |
| 154 | : SkSurface::kRetain_ContentChangeMode); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void SkCanvas::predrawNotify(const SkRect* rect, const SkPaint* paint, |
| 159 | ShaderOverrideOpacity overrideOpacity) { |
| 160 | if (fSurfaceBase) { |
| 161 | SkSurface::ContentChangeMode mode = SkSurface::kRetain_ContentChangeMode; |
| 162 | // Since willOverwriteAllPixels() may not be complete free to call, we only do so if |
| 163 | // there is an outstanding snapshot, since w/o that, there will be no copy-on-write |
| 164 | // and therefore we don't care which mode we're in. |
| 165 | // |
| 166 | if (fSurfaceBase->outstandingImageSnapshot()) { |
| 167 | if (this->wouldOverwriteEntireSurface(rect, paint, overrideOpacity)) { |
| 168 | mode = SkSurface::kDiscard_ContentChangeMode; |
| 169 | } |
| 170 | } |
| 171 | fSurfaceBase->aboutToDraw(mode); |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 175 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 176 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 177 | /* 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] | 178 | The clip/matrix/proc are fields that reflect the top of the save/restore |
| 179 | stack. Whenever the canvas changes, it marks a dirty flag, and then before |
| 180 | these are used (assuming we're not on a layer) we rebuild these cache |
| 181 | values: they reflect the top of the save stack, but translated and clipped |
| 182 | by the device's XY offset and bitmap-bounds. |
| 183 | */ |
| 184 | struct DeviceCM { |
| 185 | DeviceCM* fNext; |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 186 | SkBaseDevice* fDevice; |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 187 | SkRasterClip fClip; |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 188 | SkPaint* fPaint; // may be null (in the future) |
reed | 61f501f | 2015-04-29 08:34:00 -0700 | [diff] [blame] | 189 | const SkMatrix* fMatrix; |
| 190 | SkMatrix fMatrixStorage; |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 191 | SkMatrix fStashedMatrix; // original CTM; used by imagefilter in saveLayer |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 192 | |
reed | 96e657d | 2015-03-10 17:30:07 -0700 | [diff] [blame] | 193 | DeviceCM(SkBaseDevice* device, const SkPaint* paint, SkCanvas* canvas, |
reed | 7503d60 | 2016-07-15 14:23:29 -0700 | [diff] [blame] | 194 | bool conservativeRasterClip, const SkMatrix& stashed) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 195 | : fNext(nullptr) |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 196 | , fClip(conservativeRasterClip) |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 197 | , fStashedMatrix(stashed) |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 198 | { |
reed | 2c9e200 | 2016-07-25 08:05:22 -0700 | [diff] [blame] | 199 | SkSafeRef(device); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 200 | fDevice = device; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 201 | fPaint = paint ? new SkPaint(*paint) : nullptr; |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 202 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 203 | |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 204 | ~DeviceCM() { |
reed | 2c9e200 | 2016-07-25 08:05:22 -0700 | [diff] [blame] | 205 | SkSafeUnref(fDevice); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 206 | delete fPaint; |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 207 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 208 | |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 209 | void reset(const SkIRect& bounds) { |
| 210 | SkASSERT(!fPaint); |
| 211 | SkASSERT(!fNext); |
| 212 | SkASSERT(fDevice); |
| 213 | fClip.setRect(bounds); |
| 214 | } |
| 215 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 216 | void updateMC(const SkMatrix& totalMatrix, const SkRasterClip& totalClip, |
reed | de6c531 | 2016-09-02 12:10:07 -0700 | [diff] [blame] | 217 | SkRasterClip* updateClip) { |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 218 | int x = fDevice->getOrigin().x(); |
| 219 | int y = fDevice->getOrigin().y(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 220 | int width = fDevice->width(); |
| 221 | int height = fDevice->height(); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 222 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 223 | if ((x | y) == 0) { |
| 224 | fMatrix = &totalMatrix; |
| 225 | fClip = totalClip; |
| 226 | } else { |
| 227 | fMatrixStorage = totalMatrix; |
| 228 | fMatrixStorage.postTranslate(SkIntToScalar(-x), |
| 229 | SkIntToScalar(-y)); |
| 230 | fMatrix = &fMatrixStorage; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 231 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 232 | totalClip.translate(-x, -y, &fClip); |
| 233 | } |
| 234 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 235 | fClip.op(SkIRect::MakeWH(width, height), SkRegion::kIntersect_Op); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 236 | |
| 237 | // intersect clip, but don't translate it (yet) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 238 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 239 | if (updateClip) { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 240 | updateClip->op(SkIRect::MakeXYWH(x, y, width, height), |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 241 | SkRegion::kDifference_Op); |
| 242 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 243 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 244 | #ifdef SK_DEBUG |
| 245 | if (!fClip.isEmpty()) { |
| 246 | SkIRect deviceR; |
| 247 | deviceR.set(0, 0, width, height); |
| 248 | SkASSERT(deviceR.contains(fClip.getBounds())); |
| 249 | } |
| 250 | #endif |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 251 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | /* This is the record we keep for each save/restore level in the stack. |
| 255 | Since a level optionally copies the matrix and/or stack, we have pointers |
| 256 | for these fields. If the value is copied for this level, the copy is |
| 257 | stored in the ...Storage field, and the pointer points to that. If the |
| 258 | value is not copied for this level, we ignore ...Storage, and just point |
| 259 | at the corresponding value in the previous level in the stack. |
| 260 | */ |
| 261 | class SkCanvas::MCRec { |
| 262 | public: |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 263 | SkDrawFilter* fFilter; // the current filter (or null) |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 264 | DeviceCM* fLayer; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 265 | /* If there are any layers in the stack, this points to the top-most |
| 266 | one that is at or below this level in the stack (so we know what |
| 267 | bitmap/device to draw into from this level. This value is NOT |
| 268 | reference counted, since the real owner is either our fLayer field, |
| 269 | or a previous one in a lower level.) |
| 270 | */ |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 271 | DeviceCM* fTopLayer; |
| 272 | SkRasterClip fRasterClip; |
| 273 | SkMatrix fMatrix; |
| 274 | int fDeferredSaveCount; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 275 | |
vjiaoblack | e5de130 | 2016-07-13 14:05:28 -0700 | [diff] [blame] | 276 | // This is the current cumulative depth (aggregate of all done translateZ calls) |
| 277 | SkScalar fCurDrawDepth; |
| 278 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 279 | MCRec(bool conservativeRasterClip) : fRasterClip(conservativeRasterClip) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 280 | fFilter = nullptr; |
| 281 | fLayer = nullptr; |
| 282 | fTopLayer = nullptr; |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 283 | fMatrix.reset(); |
| 284 | fDeferredSaveCount = 0; |
vjiaoblack | e5de130 | 2016-07-13 14:05:28 -0700 | [diff] [blame] | 285 | fCurDrawDepth = 0; |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 286 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 287 | // don't bother initializing fNext |
| 288 | inc_rec(); |
| 289 | } |
vjiaoblack | e5de130 | 2016-07-13 14:05:28 -0700 | [diff] [blame] | 290 | MCRec(const MCRec& prev) : fRasterClip(prev.fRasterClip), fMatrix(prev.fMatrix), |
| 291 | fCurDrawDepth(prev.fCurDrawDepth) { |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 292 | fFilter = SkSafeRef(prev.fFilter); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 293 | fLayer = nullptr; |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 294 | fTopLayer = prev.fTopLayer; |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 295 | fDeferredSaveCount = 0; |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 296 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 297 | // don't bother initializing fNext |
| 298 | inc_rec(); |
| 299 | } |
| 300 | ~MCRec() { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 301 | SkSafeUnref(fFilter); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 302 | delete fLayer; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 303 | dec_rec(); |
| 304 | } |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 305 | |
| 306 | void reset(const SkIRect& bounds) { |
| 307 | SkASSERT(fLayer); |
| 308 | SkASSERT(fDeferredSaveCount == 0); |
| 309 | |
| 310 | fMatrix.reset(); |
| 311 | fRasterClip.setRect(bounds); |
| 312 | fLayer->reset(bounds); |
| 313 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 314 | }; |
| 315 | |
reed | 02f9ed7 | 2016-09-06 09:06:18 -0700 | [diff] [blame] | 316 | static SkIRect compute_device_bounds(SkBaseDevice* device) { |
| 317 | return SkIRect::MakeXYWH(device->getOrigin().x(), device->getOrigin().y(), |
| 318 | device->width(), device->height()); |
| 319 | } |
| 320 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 321 | class SkDrawIter : public SkDraw { |
| 322 | public: |
reed | 3aafe11 | 2016-08-18 12:45:34 -0700 | [diff] [blame] | 323 | SkDrawIter(SkCanvas* canvas) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 324 | canvas->updateDeviceCMCache(); |
| 325 | |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 326 | fClipStack = canvas->getClipStack(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 327 | fCurrLayer = canvas->fMCRec->fTopLayer; |
reed | 02f9ed7 | 2016-09-06 09:06:18 -0700 | [diff] [blame] | 328 | |
| 329 | fMultiDeviceCS = nullptr; |
| 330 | if (fCurrLayer->fNext) { |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 331 | fMultiDeviceCS = canvas->fClipStack.get(); |
reed | 02f9ed7 | 2016-09-06 09:06:18 -0700 | [diff] [blame] | 332 | fMultiDeviceCS->save(); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | ~SkDrawIter() { |
| 337 | if (fMultiDeviceCS) { |
| 338 | fMultiDeviceCS->restore(); |
| 339 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 340 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 341 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 342 | bool next() { |
reed | 02f9ed7 | 2016-09-06 09:06:18 -0700 | [diff] [blame] | 343 | if (fMultiDeviceCS && fDevice) { |
| 344 | // remove the previous device's bounds |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 345 | fMultiDeviceCS->clipDevRect(compute_device_bounds(fDevice), kDifference_SkClipOp); |
reed | 02f9ed7 | 2016-09-06 09:06:18 -0700 | [diff] [blame] | 346 | } |
| 347 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 348 | // skip over recs with empty clips |
reed | 3aafe11 | 2016-08-18 12:45:34 -0700 | [diff] [blame] | 349 | while (fCurrLayer && fCurrLayer->fClip.isEmpty()) { |
| 350 | fCurrLayer = fCurrLayer->fNext; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 351 | } |
| 352 | |
reed@google.com | f68c5e2 | 2012-02-24 16:38:58 +0000 | [diff] [blame] | 353 | const DeviceCM* rec = fCurrLayer; |
| 354 | if (rec && rec->fDevice) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 355 | |
| 356 | fMatrix = rec->fMatrix; |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 357 | fRC = &rec->fClip; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 358 | fDevice = rec->fDevice; |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 359 | if (!fDevice->accessPixels(&fDst)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 360 | fDst.reset(fDevice->imageInfo(), nullptr, 0); |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 361 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 362 | fPaint = rec->fPaint; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 363 | SkDEBUGCODE(this->validate();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 364 | |
| 365 | fCurrLayer = rec->fNext; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 366 | // fCurrLayer may be nullptr now |
reed@android.com | 199f108 | 2009-06-10 02:12:47 +0000 | [diff] [blame] | 367 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 368 | return true; |
| 369 | } |
| 370 | return false; |
| 371 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 372 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 373 | SkBaseDevice* getDevice() const { return fDevice; } |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 374 | const SkRasterClip& getClip() const { return *fRC; } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 375 | int getX() const { return fDevice->getOrigin().x(); } |
| 376 | int getY() const { return fDevice->getOrigin().y(); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 377 | const SkMatrix& getMatrix() const { return *fMatrix; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 378 | const SkPaint* getPaint() const { return fPaint; } |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 379 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 380 | private: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 381 | const DeviceCM* fCurrLayer; |
| 382 | const SkPaint* fPaint; // May be null. |
reed | 02f9ed7 | 2016-09-06 09:06:18 -0700 | [diff] [blame] | 383 | SkClipStack* fMultiDeviceCS; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 384 | |
| 385 | typedef SkDraw INHERITED; |
| 386 | }; |
| 387 | |
| 388 | ///////////////////////////////////////////////////////////////////////////// |
| 389 | |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 390 | static SkPaint* set_if_needed(SkLazyPaint* lazy, const SkPaint& orig) { |
| 391 | return lazy->isValid() ? lazy->get() : lazy->set(orig); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * 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] | 396 | * colorfilter, else return nullptr. |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 397 | */ |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 398 | static sk_sp<SkColorFilter> image_to_color_filter(const SkPaint& paint) { |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 399 | SkImageFilter* imgf = paint.getImageFilter(); |
| 400 | if (!imgf) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 401 | return nullptr; |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 402 | } |
| 403 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 404 | SkColorFilter* imgCFPtr; |
| 405 | if (!imgf->asAColorFilter(&imgCFPtr)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 406 | return nullptr; |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 407 | } |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 408 | sk_sp<SkColorFilter> imgCF(imgCFPtr); |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 409 | |
| 410 | SkColorFilter* paintCF = paint.getColorFilter(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 411 | if (nullptr == paintCF) { |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 412 | // there is no existing paint colorfilter, so we can just return the imagefilter's |
| 413 | return imgCF; |
| 414 | } |
| 415 | |
| 416 | // The paint has both a colorfilter(paintCF) and an imagefilter-which-is-a-colorfilter(imgCF) |
| 417 | // and we need to combine them into a single colorfilter. |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 418 | return SkColorFilter::MakeComposeFilter(std::move(imgCF), sk_ref_sp(paintCF)); |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 419 | } |
| 420 | |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 421 | /** |
| 422 | * There are many bounds in skia. A circle's bounds is just its center extended by its radius. |
| 423 | * However, if we stroke a circle, then the "bounds" of that is larger, since it will now draw |
| 424 | * outside of its raw-bounds by 1/2 the stroke width. SkPaint has lots of optional |
| 425 | * effects/attributes that can modify the effective bounds of a given primitive -- maskfilters, |
| 426 | * patheffects, stroking, etc. This function takes a raw bounds and a paint, and returns the |
| 427 | * conservative "effective" bounds based on the settings in the paint... with one exception. This |
| 428 | * function does *not* look at the imagefilter, which can also modify the effective bounds. It is |
| 429 | * deliberately ignored. |
| 430 | */ |
| 431 | static const SkRect& apply_paint_to_bounds_sans_imagefilter(const SkPaint& paint, |
| 432 | const SkRect& rawBounds, |
| 433 | SkRect* storage) { |
| 434 | SkPaint tmpUnfiltered(paint); |
| 435 | tmpUnfiltered.setImageFilter(nullptr); |
| 436 | if (tmpUnfiltered.canComputeFastBounds()) { |
| 437 | return tmpUnfiltered.computeFastBounds(rawBounds, storage); |
| 438 | } else { |
| 439 | return rawBounds; |
| 440 | } |
| 441 | } |
| 442 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 443 | class AutoDrawLooper { |
| 444 | public: |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 445 | // "rawBounds" is the original bounds of the primitive about to be drawn, unmodified by the |
| 446 | // paint. It's used to determine the size of the offscreen layer for filters. |
| 447 | // If null, the clip will be used instead. |
reed | 3aafe11 | 2016-08-18 12:45:34 -0700 | [diff] [blame] | 448 | AutoDrawLooper(SkCanvas* canvas, const SkPaint& paint, bool skipLayerForImageFilter = false, |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 449 | const SkRect* rawBounds = nullptr) : fOrigPaint(paint) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 450 | fCanvas = canvas; |
fmalita | 53d9f1c | 2016-01-25 06:23:54 -0800 | [diff] [blame] | 451 | #ifdef SK_SUPPORT_LEGACY_DRAWFILTER |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 452 | fFilter = canvas->getDrawFilter(); |
fmalita | 7765000 | 2016-01-21 18:47:11 -0800 | [diff] [blame] | 453 | #else |
| 454 | fFilter = nullptr; |
| 455 | #endif |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 456 | fPaint = &fOrigPaint; |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 457 | fSaveCount = canvas->getSaveCount(); |
reed | 5c476fb | 2015-04-20 08:04:21 -0700 | [diff] [blame] | 458 | fTempLayerForImageFilter = false; |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 459 | fDone = false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 460 | |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 461 | auto simplifiedCF = image_to_color_filter(fOrigPaint); |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 462 | if (simplifiedCF) { |
| 463 | SkPaint* paint = set_if_needed(&fLazyPaintInit, fOrigPaint); |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 464 | paint->setColorFilter(std::move(simplifiedCF)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 465 | paint->setImageFilter(nullptr); |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 466 | fPaint = paint; |
| 467 | } |
| 468 | |
| 469 | if (!skipLayerForImageFilter && fPaint->getImageFilter()) { |
reed | 5c476fb | 2015-04-20 08:04:21 -0700 | [diff] [blame] | 470 | /** |
| 471 | * We implement ImageFilters for a given draw by creating a layer, then applying the |
| 472 | * imagefilter to the pixels of that layer (its backing surface/image), and then |
| 473 | * we call restore() to xfer that layer to the main canvas. |
| 474 | * |
| 475 | * 1. SaveLayer (with a paint containing the current imagefilter and xfermode) |
| 476 | * 2. Generate the src pixels: |
| 477 | * Remove the imagefilter and the xfermode from the paint that we (AutoDrawLooper) |
| 478 | * return (fPaint). We then draw the primitive (using srcover) into a cleared |
| 479 | * buffer/surface. |
| 480 | * 3. Restore the layer created in #1 |
| 481 | * The imagefilter is passed the buffer/surface from the layer (now filled with the |
| 482 | * src pixels of the primitive). It returns a new "filtered" buffer, which we |
| 483 | * draw onto the previous layer using the xfermode from the original paint. |
| 484 | */ |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 485 | SkPaint tmp; |
Mike Reed | 5e25717 | 2016-11-01 11:22:05 -0400 | [diff] [blame] | 486 | tmp.setImageFilter(sk_ref_sp(fPaint->getImageFilter())); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 487 | tmp.setBlendMode(fPaint->getBlendMode()); |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 488 | SkRect storage; |
| 489 | if (rawBounds) { |
| 490 | // Make rawBounds include all paint outsets except for those due to image filters. |
| 491 | rawBounds = &apply_paint_to_bounds_sans_imagefilter(*fPaint, *rawBounds, &storage); |
| 492 | } |
reed | bfd5f17 | 2016-01-07 11:28:08 -0800 | [diff] [blame] | 493 | (void)canvas->internalSaveLayer(SkCanvas::SaveLayerRec(rawBounds, &tmp), |
reed | 76033be | 2015-03-14 10:54:31 -0700 | [diff] [blame] | 494 | SkCanvas::kFullLayer_SaveLayerStrategy); |
reed | 5c476fb | 2015-04-20 08:04:21 -0700 | [diff] [blame] | 495 | fTempLayerForImageFilter = true; |
| 496 | // we remove the imagefilter/xfermode inside doNext() |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 497 | } |
| 498 | |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 499 | if (SkDrawLooper* looper = paint.getLooper()) { |
herb | bf6d80a | 2016-11-15 06:26:56 -0800 | [diff] [blame] | 500 | fLooperContext = fLooperContextAllocator.createWithIniter( |
| 501 | looper->contextSize(), |
| 502 | [&](void* buffer) { |
| 503 | return looper->createContext(canvas, buffer); |
| 504 | }); |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 505 | fIsSimple = false; |
| 506 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 507 | fLooperContext = nullptr; |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 508 | // can we be marked as simple? |
reed | 5c476fb | 2015-04-20 08:04:21 -0700 | [diff] [blame] | 509 | fIsSimple = !fFilter && !fTempLayerForImageFilter; |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 510 | } |
| 511 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 512 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 513 | ~AutoDrawLooper() { |
reed | 5c476fb | 2015-04-20 08:04:21 -0700 | [diff] [blame] | 514 | if (fTempLayerForImageFilter) { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 515 | fCanvas->internalRestore(); |
| 516 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 517 | SkASSERT(fCanvas->getSaveCount() == fSaveCount); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 518 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 519 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 520 | const SkPaint& paint() const { |
| 521 | SkASSERT(fPaint); |
| 522 | return *fPaint; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 523 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 524 | |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 525 | bool next(SkDrawFilter::Type drawType) { |
| 526 | if (fDone) { |
| 527 | return false; |
| 528 | } else if (fIsSimple) { |
| 529 | fDone = true; |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 530 | return !fPaint->nothingToDraw(); |
| 531 | } else { |
| 532 | return this->doNext(drawType); |
| 533 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 534 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 535 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 536 | private: |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 537 | SkLazyPaint fLazyPaintInit; // base paint storage in case we need to modify it |
| 538 | SkLazyPaint fLazyPaintPerLooper; // per-draw-looper storage, so the looper can modify it |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 539 | SkCanvas* fCanvas; |
| 540 | const SkPaint& fOrigPaint; |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 541 | SkDrawFilter* fFilter; |
| 542 | const SkPaint* fPaint; |
| 543 | int fSaveCount; |
reed | 5c476fb | 2015-04-20 08:04:21 -0700 | [diff] [blame] | 544 | bool fTempLayerForImageFilter; |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 545 | bool fDone; |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 546 | bool fIsSimple; |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 547 | SkDrawLooper::Context* fLooperContext; |
| 548 | SkSmallAllocator<1, 32> fLooperContextAllocator; |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 549 | |
| 550 | bool doNext(SkDrawFilter::Type drawType); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 551 | }; |
| 552 | |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 553 | bool AutoDrawLooper::doNext(SkDrawFilter::Type drawType) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 554 | fPaint = nullptr; |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 555 | SkASSERT(!fIsSimple); |
reed | 5c476fb | 2015-04-20 08:04:21 -0700 | [diff] [blame] | 556 | SkASSERT(fLooperContext || fFilter || fTempLayerForImageFilter); |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 557 | |
reed | dbc3cef | 2015-04-29 12:18:57 -0700 | [diff] [blame] | 558 | SkPaint* paint = fLazyPaintPerLooper.set(fLazyPaintInit.isValid() ? |
| 559 | *fLazyPaintInit.get() : fOrigPaint); |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 560 | |
reed | 5c476fb | 2015-04-20 08:04:21 -0700 | [diff] [blame] | 561 | if (fTempLayerForImageFilter) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 562 | paint->setImageFilter(nullptr); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 563 | paint->setBlendMode(SkBlendMode::kSrcOver); |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 564 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 565 | |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 566 | if (fLooperContext && !fLooperContext->next(fCanvas, paint)) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 567 | fDone = true; |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 568 | return false; |
| 569 | } |
| 570 | if (fFilter) { |
reed@google.com | 971aca7 | 2012-11-26 20:26:54 +0000 | [diff] [blame] | 571 | if (!fFilter->filter(paint, drawType)) { |
| 572 | fDone = true; |
| 573 | return false; |
| 574 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 575 | if (nullptr == fLooperContext) { |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 576 | // no looper means we only draw once |
| 577 | fDone = true; |
| 578 | } |
| 579 | } |
| 580 | fPaint = paint; |
| 581 | |
| 582 | // if we only came in here for the imagefilter, mark us as done |
commit-bot@chromium.org | 79fbb40 | 2014-03-12 09:42:01 +0000 | [diff] [blame] | 583 | if (!fLooperContext && !fFilter) { |
reed@google.com | 129ec22 | 2012-05-15 13:24:09 +0000 | [diff] [blame] | 584 | fDone = true; |
reed@google.com | 632e1a2 | 2011-10-06 12:37:00 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | // call this after any possible paint modifiers |
| 588 | if (fPaint->nothingToDraw()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 589 | fPaint = nullptr; |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 590 | return false; |
| 591 | } |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 592 | return true; |
| 593 | } |
| 594 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 595 | ////////// macros to place around the internal draw calls ////////////////// |
| 596 | |
reed | 3aafe11 | 2016-08-18 12:45:34 -0700 | [diff] [blame] | 597 | #define LOOPER_BEGIN_DRAWBITMAP(paint, skipLayerForFilter, bounds) \ |
| 598 | this->predrawNotify(); \ |
| 599 | AutoDrawLooper looper(this, paint, skipLayerForFilter, bounds); \ |
| 600 | while (looper.next(SkDrawFilter::kBitmap_Type)) { \ |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 601 | SkDrawIter iter(this); |
| 602 | |
| 603 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 604 | #define LOOPER_BEGIN_DRAWDEVICE(paint, type) \ |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 605 | this->predrawNotify(); \ |
reed | 3aafe11 | 2016-08-18 12:45:34 -0700 | [diff] [blame] | 606 | AutoDrawLooper looper(this, paint, true); \ |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 607 | while (looper.next(type)) { \ |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 608 | SkDrawIter iter(this); |
| 609 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 610 | #define LOOPER_BEGIN(paint, type, bounds) \ |
reed@google.com | 97af1a6 | 2012-08-28 12:19:02 +0000 | [diff] [blame] | 611 | this->predrawNotify(); \ |
reed | 3aafe11 | 2016-08-18 12:45:34 -0700 | [diff] [blame] | 612 | AutoDrawLooper looper(this, paint, false, bounds); \ |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 613 | while (looper.next(type)) { \ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 614 | SkDrawIter iter(this); |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 615 | |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 616 | #define LOOPER_BEGIN_CHECK_COMPLETE_OVERWRITE(paint, type, bounds, auxOpaque) \ |
| 617 | this->predrawNotify(bounds, &paint, auxOpaque); \ |
reed | 3aafe11 | 2016-08-18 12:45:34 -0700 | [diff] [blame] | 618 | AutoDrawLooper looper(this, paint, false, bounds); \ |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 619 | while (looper.next(type)) { \ |
| 620 | SkDrawIter iter(this); |
| 621 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 622 | #define LOOPER_END } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 623 | |
| 624 | //////////////////////////////////////////////////////////////////////////// |
| 625 | |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 626 | static inline SkRect qr_clip_bounds(const SkIRect& bounds) { |
| 627 | if (bounds.isEmpty()) { |
| 628 | return SkRect::MakeEmpty(); |
| 629 | } |
| 630 | |
| 631 | // Expand bounds out by 1 in case we are anti-aliasing. We store the |
| 632 | // bounds as floats to enable a faster quick reject implementation. |
| 633 | SkRect dst; |
| 634 | SkNx_cast<float>(Sk4i::Load(&bounds.fLeft) + Sk4i(-1,-1,1,1)).store(&dst.fLeft); |
| 635 | return dst; |
| 636 | } |
| 637 | |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 638 | void SkCanvas::resetForNextPicture(const SkIRect& bounds) { |
| 639 | this->restoreToCount(1); |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 640 | fClipStack->reset(); |
| 641 | fMCRec->reset(bounds); |
| 642 | |
| 643 | // We're peering through a lot of structs here. Only at this scope do we |
| 644 | // know that the device is an SkBitmapDevice (really an SkNoPixelsBitmapDevice). |
| 645 | static_cast<SkBitmapDevice*>(fMCRec->fLayer->fDevice)->setNewSize(bounds.size()); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 646 | fDeviceClipBounds = qr_clip_bounds(bounds); |
msarett | 9637ea9 | 2016-08-18 14:03:30 -0700 | [diff] [blame] | 647 | fIsScaleTranslate = true; |
mtklein | feaadee | 2015-04-08 11:25:48 -0700 | [diff] [blame] | 648 | } |
| 649 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 650 | SkBaseDevice* SkCanvas::init(SkBaseDevice* device, InitFlags flags) { |
reed | 42b73eb | 2015-11-20 13:42:42 -0800 | [diff] [blame] | 651 | if (device && device->forceConservativeRasterClip()) { |
| 652 | flags = InitFlags(flags | kConservativeRasterClip_InitFlag); |
| 653 | } |
| 654 | // Since init() is only called once by our constructors, it is safe to perform this |
| 655 | // const-cast. |
| 656 | *const_cast<bool*>(&fConservativeRasterClip) = SkToBool(flags & kConservativeRasterClip_InitFlag); |
| 657 | |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 658 | fAllowSimplifyClip = false; |
reed | f92c866 | 2014-08-18 08:02:43 -0700 | [diff] [blame] | 659 | fDeviceCMDirty = true; |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 660 | fSaveCount = 1; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 661 | fMetaData = nullptr; |
vjiaoblack | 95302da | 2016-07-21 10:25:54 -0700 | [diff] [blame] | 662 | #ifdef SK_EXPERIMENTAL_SHADOWING |
| 663 | fLights = nullptr; |
| 664 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 665 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 666 | fClipStack.reset(new SkClipStack); |
reed | 687fa1c | 2015-04-07 08:00:56 -0700 | [diff] [blame] | 667 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 668 | fMCRec = (MCRec*)fMCStack.push_back(); |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 669 | new (fMCRec) MCRec(fConservativeRasterClip); |
msarett | 9637ea9 | 2016-08-18 14:03:30 -0700 | [diff] [blame] | 670 | fIsScaleTranslate = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 671 | |
reed | a499f90 | 2015-05-01 09:34:31 -0700 | [diff] [blame] | 672 | SkASSERT(sizeof(DeviceCM) <= sizeof(fDeviceCMStorage)); |
| 673 | fMCRec->fLayer = (DeviceCM*)fDeviceCMStorage; |
reed | 7503d60 | 2016-07-15 14:23:29 -0700 | [diff] [blame] | 674 | new (fDeviceCMStorage) DeviceCM(nullptr, nullptr, nullptr, fConservativeRasterClip, |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 675 | fMCRec->fMatrix); |
reed | b679ca8 | 2015-04-07 04:40:48 -0700 | [diff] [blame] | 676 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 677 | fMCRec->fTopLayer = fMCRec->fLayer; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 678 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 679 | fSurfaceBase = nullptr; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 680 | |
reed | f92c866 | 2014-08-18 08:02:43 -0700 | [diff] [blame] | 681 | if (device) { |
robertphillips | efbffed | 2015-06-22 12:06:08 -0700 | [diff] [blame] | 682 | // The root device and the canvas should always have the same pixel geometry |
| 683 | SkASSERT(fProps.pixelGeometry() == device->surfaceProps().pixelGeometry()); |
reed | f92c866 | 2014-08-18 08:02:43 -0700 | [diff] [blame] | 684 | fMCRec->fLayer->fDevice = SkRef(device); |
reed | 78e2768 | 2014-11-19 08:04:34 -0800 | [diff] [blame] | 685 | fMCRec->fRasterClip.setRect(device->getGlobalBounds()); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 686 | fDeviceClipBounds = qr_clip_bounds(device->getGlobalBounds()); |
reed | f92c866 | 2014-08-18 08:02:43 -0700 | [diff] [blame] | 687 | } |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 688 | |
reed | f92c866 | 2014-08-18 08:02:43 -0700 | [diff] [blame] | 689 | return device; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 690 | } |
| 691 | |
reed@google.com | cde9211 | 2011-07-06 20:00:52 +0000 | [diff] [blame] | 692 | SkCanvas::SkCanvas() |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 693 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 694 | , fProps(SkSurfaceProps::kLegacyFontHost_InitType) |
reed | 42b73eb | 2015-11-20 13:42:42 -0800 | [diff] [blame] | 695 | , fConservativeRasterClip(false) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 696 | { |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 697 | inc_canvas(); |
skia.committer@gmail.com | ba12448 | 2014-02-01 03:01:57 +0000 | [diff] [blame] | 698 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 699 | this->init(nullptr, kDefault_InitFlags); |
vandebo@chromium.org | 8d84fac | 2010-10-13 22:13:05 +0000 | [diff] [blame] | 700 | } |
| 701 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 702 | static SkBitmap make_nopixels(int width, int height) { |
| 703 | SkBitmap bitmap; |
| 704 | bitmap.setInfo(SkImageInfo::MakeUnknown(width, height)); |
| 705 | return bitmap; |
| 706 | } |
| 707 | |
| 708 | class SkNoPixelsBitmapDevice : public SkBitmapDevice { |
| 709 | public: |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 710 | SkNoPixelsBitmapDevice(const SkIRect& bounds, const SkSurfaceProps& surfaceProps) |
| 711 | : INHERITED(make_nopixels(bounds.width(), bounds.height()), surfaceProps) |
reed | 78e2768 | 2014-11-19 08:04:34 -0800 | [diff] [blame] | 712 | { |
| 713 | this->setOrigin(bounds.x(), bounds.y()); |
| 714 | } |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 715 | |
| 716 | private: |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 717 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 718 | typedef SkBitmapDevice INHERITED; |
| 719 | }; |
| 720 | |
reed | 96a857e | 2015-01-25 10:33:58 -0800 | [diff] [blame] | 721 | SkCanvas::SkCanvas(int width, int height, const SkSurfaceProps* props) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 722 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
reed | 96a857e | 2015-01-25 10:33:58 -0800 | [diff] [blame] | 723 | , fProps(SkSurfacePropsCopyOrDefault(props)) |
reed | 42b73eb | 2015-11-20 13:42:42 -0800 | [diff] [blame] | 724 | , fConservativeRasterClip(false) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 725 | { |
| 726 | inc_canvas(); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 727 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 728 | this->init(new SkNoPixelsBitmapDevice(SkIRect::MakeWH(width, height), fProps), |
| 729 | kDefault_InitFlags)->unref(); |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 730 | } |
skia.committer@gmail.com | ba12448 | 2014-02-01 03:01:57 +0000 | [diff] [blame] | 731 | |
reed | 78e2768 | 2014-11-19 08:04:34 -0800 | [diff] [blame] | 732 | SkCanvas::SkCanvas(const SkIRect& bounds, InitFlags flags) |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 733 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 734 | , fProps(SkSurfaceProps::kLegacyFontHost_InitType) |
reed | 42b73eb | 2015-11-20 13:42:42 -0800 | [diff] [blame] | 735 | , fConservativeRasterClip(false) |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 736 | { |
| 737 | inc_canvas(); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 738 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 739 | this->init(new SkNoPixelsBitmapDevice(bounds, fProps), flags)->unref(); |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 740 | } |
| 741 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 742 | SkCanvas::SkCanvas(SkBaseDevice* device) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 743 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
robertphillips | 7b05ff1 | 2015-06-19 14:14:54 -0700 | [diff] [blame] | 744 | , fProps(device->surfaceProps()) |
reed | 42b73eb | 2015-11-20 13:42:42 -0800 | [diff] [blame] | 745 | , fConservativeRasterClip(false) |
commit-bot@chromium.org | e254310 | 2014-01-31 19:42:58 +0000 | [diff] [blame] | 746 | { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 747 | inc_canvas(); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 748 | |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 749 | this->init(device, kDefault_InitFlags); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 750 | } |
| 751 | |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 752 | SkCanvas::SkCanvas(SkBaseDevice* device, InitFlags flags) |
| 753 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
robertphillips | 7b05ff1 | 2015-06-19 14:14:54 -0700 | [diff] [blame] | 754 | , fProps(device->surfaceProps()) |
reed | 42b73eb | 2015-11-20 13:42:42 -0800 | [diff] [blame] | 755 | , fConservativeRasterClip(false) |
robertphillips | fcf7829 | 2015-06-19 11:49:52 -0700 | [diff] [blame] | 756 | { |
| 757 | inc_canvas(); |
| 758 | |
| 759 | this->init(device, flags); |
| 760 | } |
| 761 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 762 | SkCanvas::SkCanvas(const SkBitmap& bitmap, const SkSurfaceProps& props) |
reed | 3716fd0 | 2014-09-21 09:39:55 -0700 | [diff] [blame] | 763 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 764 | , fProps(props) |
reed | 42b73eb | 2015-11-20 13:42:42 -0800 | [diff] [blame] | 765 | , fConservativeRasterClip(false) |
reed | 3716fd0 | 2014-09-21 09:39:55 -0700 | [diff] [blame] | 766 | { |
| 767 | inc_canvas(); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 768 | |
Hal Canary | 704cd32 | 2016-11-07 14:13:52 -0500 | [diff] [blame] | 769 | sk_sp<SkBaseDevice> device(new SkBitmapDevice(bitmap, fProps)); |
| 770 | this->init(device.get(), kDefault_InitFlags); |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 771 | } |
reed | 29c857d | 2014-09-21 10:25:07 -0700 | [diff] [blame] | 772 | |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 773 | SkCanvas::SkCanvas(const SkBitmap& bitmap) |
| 774 | : fMCStack(sizeof(MCRec), fMCRecStorage, sizeof(fMCRecStorage)) |
| 775 | , fProps(SkSurfaceProps::kLegacyFontHost_InitType) |
reed | 42b73eb | 2015-11-20 13:42:42 -0800 | [diff] [blame] | 776 | , fConservativeRasterClip(false) |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 777 | { |
| 778 | inc_canvas(); |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 779 | |
Hal Canary | 704cd32 | 2016-11-07 14:13:52 -0500 | [diff] [blame] | 780 | sk_sp<SkBaseDevice> device(new SkBitmapDevice(bitmap, fProps)); |
| 781 | this->init(device.get(), kDefault_InitFlags); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | SkCanvas::~SkCanvas() { |
| 785 | // free up the contents of our deque |
| 786 | this->restoreToCount(1); // restore everything but the last |
reed@google.com | 7c20293 | 2011-12-14 18:48:05 +0000 | [diff] [blame] | 787 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 788 | this->internalRestore(); // restore the last, since we're going away |
| 789 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 790 | delete fMetaData; |
vandebo@chromium.org | b70ae31 | 2010-10-15 18:58:19 +0000 | [diff] [blame] | 791 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 792 | dec_canvas(); |
| 793 | } |
| 794 | |
fmalita | 53d9f1c | 2016-01-25 06:23:54 -0800 | [diff] [blame] | 795 | #ifdef SK_SUPPORT_LEGACY_DRAWFILTER |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 796 | SkDrawFilter* SkCanvas::getDrawFilter() const { |
| 797 | return fMCRec->fFilter; |
| 798 | } |
| 799 | |
| 800 | SkDrawFilter* SkCanvas::setDrawFilter(SkDrawFilter* filter) { |
reed | 51985e3 | 2015-04-11 08:04:56 -0700 | [diff] [blame] | 801 | this->checkForDeferredSave(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 802 | SkRefCnt_SafeAssign(fMCRec->fFilter, filter); |
| 803 | return filter; |
| 804 | } |
fmalita | 7765000 | 2016-01-21 18:47:11 -0800 | [diff] [blame] | 805 | #endif |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 806 | |
mike@reedtribe.org | 74bb77e | 2012-09-26 02:24:45 +0000 | [diff] [blame] | 807 | SkMetaData& SkCanvas::getMetaData() { |
| 808 | // metadata users are rare, so we lazily allocate it. If that changes we |
| 809 | // can decide to just make it a field in the device (rather than a ptr) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 810 | if (nullptr == fMetaData) { |
mike@reedtribe.org | 74bb77e | 2012-09-26 02:24:45 +0000 | [diff] [blame] | 811 | fMetaData = new SkMetaData; |
| 812 | } |
| 813 | return *fMetaData; |
| 814 | } |
| 815 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 816 | /////////////////////////////////////////////////////////////////////////////// |
| 817 | |
junov@chromium.org | bf6c1e4 | 2012-01-30 14:53:22 +0000 | [diff] [blame] | 818 | void SkCanvas::flush() { |
reed | ea5a651 | 2016-07-07 16:44:27 -0700 | [diff] [blame] | 819 | this->onFlush(); |
| 820 | } |
| 821 | |
| 822 | void SkCanvas::onFlush() { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 823 | SkBaseDevice* device = this->getDevice(); |
junov@chromium.org | bf6c1e4 | 2012-01-30 14:53:22 +0000 | [diff] [blame] | 824 | if (device) { |
| 825 | device->flush(); |
| 826 | } |
| 827 | } |
| 828 | |
bsalomon@google.com | 4ebe382 | 2014-02-26 20:22:32 +0000 | [diff] [blame] | 829 | SkISize SkCanvas::getBaseLayerSize() const { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 830 | SkBaseDevice* d = this->getDevice(); |
reed@google.com | 210ce00 | 2011-11-01 14:24:23 +0000 | [diff] [blame] | 831 | return d ? SkISize::Make(d->width(), d->height()) : SkISize::Make(0, 0); |
| 832 | } |
| 833 | |
senorblanco | afc7cce | 2016-02-02 18:44:15 -0800 | [diff] [blame] | 834 | SkIRect SkCanvas::getTopLayerBounds() const { |
| 835 | SkBaseDevice* d = this->getTopDevice(); |
| 836 | if (!d) { |
| 837 | return SkIRect::MakeEmpty(); |
| 838 | } |
| 839 | return SkIRect::MakeXYWH(d->getOrigin().x(), d->getOrigin().y(), d->width(), d->height()); |
| 840 | } |
| 841 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 842 | SkBaseDevice* SkCanvas::getDevice() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 843 | // return root device |
robertphillips@google.com | c029062 | 2012-07-16 21:20:03 +0000 | [diff] [blame] | 844 | MCRec* rec = (MCRec*) fMCStack.front(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 845 | SkASSERT(rec && rec->fLayer); |
| 846 | return rec->fLayer->fDevice; |
| 847 | } |
| 848 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 849 | SkBaseDevice* SkCanvas::getTopDevice(bool updateMatrixClip) const { |
reed@google.com | 0b53d59 | 2012-03-19 18:26:34 +0000 | [diff] [blame] | 850 | if (updateMatrixClip) { |
| 851 | const_cast<SkCanvas*>(this)->updateDeviceCMCache(); |
| 852 | } |
reed@google.com | 9266fed | 2011-03-30 00:18:03 +0000 | [diff] [blame] | 853 | return fMCRec->fTopLayer->fDevice; |
| 854 | } |
| 855 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 856 | bool SkCanvas::readPixels(SkBitmap* bitmap, int x, int y) { |
reed | c7ec7c9 | 2016-07-25 08:29:10 -0700 | [diff] [blame] | 857 | if (kUnknown_SkColorType == bitmap->colorType()) { |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 858 | return false; |
| 859 | } |
| 860 | |
| 861 | bool weAllocated = false; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 862 | if (nullptr == bitmap->pixelRef()) { |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 863 | if (!bitmap->tryAllocPixels()) { |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 864 | return false; |
| 865 | } |
| 866 | weAllocated = true; |
| 867 | } |
| 868 | |
reed | cf01e31 | 2015-05-23 19:14:51 -0700 | [diff] [blame] | 869 | SkAutoPixmapUnlock unlocker; |
| 870 | if (bitmap->requestLock(&unlocker)) { |
| 871 | const SkPixmap& pm = unlocker.pixmap(); |
| 872 | if (this->readPixels(pm.info(), pm.writable_addr(), pm.rowBytes(), x, y)) { |
| 873 | return true; |
| 874 | } |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | if (weAllocated) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 878 | bitmap->setPixelRef(nullptr); |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 879 | } |
| 880 | return false; |
| 881 | } |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 882 | |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 883 | bool SkCanvas::readPixels(const SkIRect& srcRect, SkBitmap* bitmap) { |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 884 | SkIRect r = srcRect; |
| 885 | const SkISize size = this->getBaseLayerSize(); |
| 886 | if (!r.intersect(0, 0, size.width(), size.height())) { |
| 887 | bitmap->reset(); |
| 888 | return false; |
| 889 | } |
| 890 | |
reed | 8482504 | 2014-09-02 12:50:45 -0700 | [diff] [blame] | 891 | if (!bitmap->tryAllocN32Pixels(r.width(), r.height())) { |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 892 | // bitmap will already be reset. |
| 893 | return false; |
| 894 | } |
| 895 | if (!this->readPixels(bitmap->info(), bitmap->getPixels(), bitmap->rowBytes(), r.x(), r.y())) { |
| 896 | bitmap->reset(); |
| 897 | return false; |
| 898 | } |
| 899 | return true; |
| 900 | } |
| 901 | |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 902 | bool SkCanvas::readPixels(const SkImageInfo& dstInfo, void* dstP, size_t rowBytes, int x, int y) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 903 | SkBaseDevice* device = this->getDevice(); |
djsollen@google.com | ccfee2a | 2012-05-01 16:50:10 +0000 | [diff] [blame] | 904 | if (!device) { |
| 905 | return false; |
| 906 | } |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 907 | const SkISize size = this->getBaseLayerSize(); |
mtklein | f0f1411 | 2014-12-12 08:46:25 -0800 | [diff] [blame] | 908 | |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 909 | SkReadPixelsRec rec(dstInfo, dstP, rowBytes, x, y); |
| 910 | if (!rec.trim(size.width(), size.height())) { |
bsalomon@google.com | daba14b | 2011-11-02 20:10:48 +0000 | [diff] [blame] | 911 | return false; |
bsalomon@google.com | c698097 | 2011-11-02 19:57:21 +0000 | [diff] [blame] | 912 | } |
skia.committer@gmail.com | db0c875 | 2014-03-18 03:02:11 +0000 | [diff] [blame] | 913 | |
commit-bot@chromium.org | a713f9c | 2014-03-17 21:31:26 +0000 | [diff] [blame] | 914 | // The device can assert that the requested area is always contained in its bounds |
reed | 96472de | 2014-12-10 09:53:42 -0800 | [diff] [blame] | 915 | return device->readPixels(rec.fInfo, rec.fPixels, rec.fRowBytes, rec.fX, rec.fY); |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 916 | } |
| 917 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 918 | bool SkCanvas::writePixels(const SkBitmap& bitmap, int x, int y) { |
reed | cf01e31 | 2015-05-23 19:14:51 -0700 | [diff] [blame] | 919 | SkAutoPixmapUnlock unlocker; |
| 920 | if (bitmap.requestLock(&unlocker)) { |
| 921 | const SkPixmap& pm = unlocker.pixmap(); |
| 922 | 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] | 923 | } |
| 924 | return false; |
| 925 | } |
| 926 | |
| 927 | bool SkCanvas::writePixels(const SkImageInfo& origInfo, const void* pixels, size_t rowBytes, |
| 928 | int x, int y) { |
| 929 | switch (origInfo.colorType()) { |
| 930 | case kUnknown_SkColorType: |
| 931 | case kIndex_8_SkColorType: |
| 932 | return false; |
| 933 | default: |
| 934 | break; |
| 935 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 936 | if (nullptr == pixels || rowBytes < origInfo.minRowBytes()) { |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 937 | return false; |
| 938 | } |
| 939 | |
| 940 | const SkISize size = this->getBaseLayerSize(); |
| 941 | SkIRect target = SkIRect::MakeXYWH(x, y, origInfo.width(), origInfo.height()); |
| 942 | if (!target.intersect(0, 0, size.width(), size.height())) { |
| 943 | return false; |
| 944 | } |
| 945 | |
| 946 | SkBaseDevice* device = this->getDevice(); |
| 947 | if (!device) { |
| 948 | return false; |
| 949 | } |
| 950 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 951 | // the intersect may have shrunk info's logical size |
reed | e5ea500 | 2014-09-03 11:54:58 -0700 | [diff] [blame] | 952 | const SkImageInfo info = origInfo.makeWH(target.width(), target.height()); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 953 | |
| 954 | // if x or y are negative, then we have to adjust pixels |
| 955 | if (x > 0) { |
| 956 | x = 0; |
| 957 | } |
| 958 | if (y > 0) { |
| 959 | y = 0; |
| 960 | } |
| 961 | // here x,y are either 0 or negative |
| 962 | pixels = ((const char*)pixels - y * rowBytes - x * info.bytesPerPixel()); |
| 963 | |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 964 | // Tell our owning surface to bump its generation ID |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 965 | const bool completeOverwrite = info.dimensions() == size; |
| 966 | this->predrawNotify(completeOverwrite); |
reed | 4af35f3 | 2014-06-27 17:47:49 -0700 | [diff] [blame] | 967 | |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 968 | // The device can assert that the requested area is always contained in its bounds |
commit-bot@chromium.org | 4ef54f8 | 2014-03-17 17:03:18 +0000 | [diff] [blame] | 969 | return device->writePixels(info, pixels, rowBytes, target.x(), target.y()); |
commit-bot@chromium.org | 4cd9e21 | 2014-03-07 03:25:16 +0000 | [diff] [blame] | 970 | } |
reed@google.com | 51df9e3 | 2010-12-23 19:29:18 +0000 | [diff] [blame] | 971 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 972 | ////////////////////////////////////////////////////////////////////////////// |
| 973 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 974 | void SkCanvas::updateDeviceCMCache() { |
| 975 | if (fDeviceCMDirty) { |
| 976 | const SkMatrix& totalMatrix = this->getTotalMatrix(); |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 977 | const SkRasterClip& totalClip = fMCRec->fRasterClip; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 978 | DeviceCM* layer = fMCRec->fTopLayer; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 979 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 980 | if (nullptr == layer->fNext) { // only one layer |
reed | de6c531 | 2016-09-02 12:10:07 -0700 | [diff] [blame] | 981 | layer->updateMC(totalMatrix, totalClip, nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 982 | } else { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 983 | SkRasterClip clip(totalClip); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 984 | do { |
reed | de6c531 | 2016-09-02 12:10:07 -0700 | [diff] [blame] | 985 | layer->updateMC(totalMatrix, clip, &clip); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 986 | } while ((layer = layer->fNext) != nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 987 | } |
| 988 | fDeviceCMDirty = false; |
| 989 | } |
| 990 | } |
| 991 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 992 | /////////////////////////////////////////////////////////////////////////////// |
| 993 | |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 994 | void SkCanvas::checkForDeferredSave() { |
| 995 | if (fMCRec->fDeferredSaveCount > 0) { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 996 | this->doSave(); |
| 997 | } |
| 998 | } |
| 999 | |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 1000 | int SkCanvas::getSaveCount() const { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1001 | #ifdef SK_DEBUG |
| 1002 | int count = 0; |
| 1003 | SkDeque::Iter iter(fMCStack, SkDeque::Iter::kFront_IterStart); |
| 1004 | for (;;) { |
| 1005 | const MCRec* rec = (const MCRec*)iter.next(); |
| 1006 | if (!rec) { |
| 1007 | break; |
| 1008 | } |
| 1009 | count += 1 + rec->fDeferredSaveCount; |
| 1010 | } |
| 1011 | SkASSERT(count == fSaveCount); |
| 1012 | #endif |
| 1013 | return fSaveCount; |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | int SkCanvas::save() { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1017 | fSaveCount += 1; |
| 1018 | fMCRec->fDeferredSaveCount += 1; |
| 1019 | return this->getSaveCount() - 1; // return our prev value |
| 1020 | } |
| 1021 | |
| 1022 | void SkCanvas::doSave() { |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 1023 | this->willSave(); |
fmalita | a62d32d | 2015-04-28 08:08:57 -0700 | [diff] [blame] | 1024 | |
| 1025 | SkASSERT(fMCRec->fDeferredSaveCount > 0); |
| 1026 | fMCRec->fDeferredSaveCount -= 1; |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1027 | this->internalSave(); |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | void SkCanvas::restore() { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1031 | if (fMCRec->fDeferredSaveCount > 0) { |
| 1032 | SkASSERT(fSaveCount > 1); |
| 1033 | fSaveCount -= 1; |
| 1034 | fMCRec->fDeferredSaveCount -= 1; |
| 1035 | } else { |
| 1036 | // check for underflow |
| 1037 | if (fMCStack.count() > 1) { |
| 1038 | this->willRestore(); |
| 1039 | SkASSERT(fSaveCount > 1); |
reed | a644116 | 2015-03-26 13:40:09 -0700 | [diff] [blame] | 1040 | fSaveCount -= 1; |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1041 | this->internalRestore(); |
| 1042 | this->didRestore(); |
| 1043 | } |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | void SkCanvas::restoreToCount(int count) { |
| 1048 | // sanity check |
| 1049 | if (count < 1) { |
| 1050 | count = 1; |
| 1051 | } |
mtklein | f0f1411 | 2014-12-12 08:46:25 -0800 | [diff] [blame] | 1052 | |
reed | f0090cb | 2014-11-26 08:55:51 -0800 | [diff] [blame] | 1053 | int n = this->getSaveCount() - count; |
| 1054 | for (int i = 0; i < n; ++i) { |
| 1055 | this->restore(); |
| 1056 | } |
| 1057 | } |
| 1058 | |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1059 | void SkCanvas::internalSave() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1060 | MCRec* newTop = (MCRec*)fMCStack.push_back(); |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 1061 | new (newTop) MCRec(*fMCRec); // balanced in restore() |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1062 | fMCRec = newTop; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1063 | |
reed | 687fa1c | 2015-04-07 08:00:56 -0700 | [diff] [blame] | 1064 | fClipStack->save(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 1067 | bool SkCanvas::BoundsAffectsClip(SaveLayerFlags saveLayerFlags) { |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 1068 | return !(saveLayerFlags & SkCanvas::kDontClipToLayer_PrivateSaveLayerFlag); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1069 | } |
| 1070 | |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 1071 | bool SkCanvas::clipRectBounds(const SkRect* bounds, SaveLayerFlags saveLayerFlags, |
reed | 9b3aa54 | 2015-03-11 08:47:12 -0700 | [diff] [blame] | 1072 | SkIRect* intersection, const SkImageFilter* imageFilter) { |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1073 | SkIRect clipBounds; |
| 1074 | if (!this->getClipDeviceBounds(&clipBounds)) { |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 1075 | return false; |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1076 | } |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 1077 | |
reed | 96e657d | 2015-03-10 17:30:07 -0700 | [diff] [blame] | 1078 | const SkMatrix& ctm = fMCRec->fMatrix; // this->getTotalMatrix() |
| 1079 | |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 1080 | if (imageFilter) { |
senorblanco | e5e7984 | 2016-03-21 14:51:59 -0700 | [diff] [blame] | 1081 | clipBounds = imageFilter->filterBounds(clipBounds, ctm); |
senorblanco | db64af3 | 2015-12-09 10:11:43 -0800 | [diff] [blame] | 1082 | if (bounds && !imageFilter->canComputeFastBounds()) { |
| 1083 | bounds = nullptr; |
| 1084 | } |
senorblanco@chromium.org | c4b12f1 | 2014-02-05 17:51:22 +0000 | [diff] [blame] | 1085 | } |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1086 | SkIRect ir; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1087 | if (bounds) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1088 | SkRect r; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1089 | |
reed | 96e657d | 2015-03-10 17:30:07 -0700 | [diff] [blame] | 1090 | ctm.mapRect(&r, *bounds); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1091 | r.roundOut(&ir); |
| 1092 | // early exit if the layer's bounds are clipped out |
| 1093 | if (!ir.intersect(clipBounds)) { |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 1094 | if (BoundsAffectsClip(saveLayerFlags)) { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1095 | fMCRec->fRasterClip.setEmpty(); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1096 | fDeviceClipBounds.setEmpty(); |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1097 | } |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 1098 | return false; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1099 | } |
| 1100 | } else { // no user bounds, so just use the clip |
| 1101 | ir = clipBounds; |
| 1102 | } |
reed | 180aec4 | 2015-03-11 10:39:04 -0700 | [diff] [blame] | 1103 | SkASSERT(!ir.isEmpty()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1104 | |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 1105 | if (BoundsAffectsClip(saveLayerFlags)) { |
reed | 180aec4 | 2015-03-11 10:39:04 -0700 | [diff] [blame] | 1106 | // Simplify the current clips since they will be applied properly during restore() |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1107 | fClipStack->clipDevRect(ir, kReplace_SkClipOp); |
reed | 180aec4 | 2015-03-11 10:39:04 -0700 | [diff] [blame] | 1108 | fMCRec->fRasterClip.setRect(ir); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1109 | fDeviceClipBounds = qr_clip_bounds(ir); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | if (intersection) { |
| 1113 | *intersection = ir; |
| 1114 | } |
| 1115 | return true; |
| 1116 | } |
| 1117 | |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 1118 | |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 1119 | int SkCanvas::saveLayer(const SkRect* bounds, const SkPaint* paint) { |
| 1120 | return this->saveLayer(SaveLayerRec(bounds, paint, 0)); |
commit-bot@chromium.org | d70fa20 | 2014-04-24 21:51:58 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
reed | 70ee31b | 2015-12-10 13:44:45 -0800 | [diff] [blame] | 1123 | int SkCanvas::saveLayerPreserveLCDTextRequests(const SkRect* bounds, const SkPaint* paint) { |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 1124 | return this->saveLayer(SaveLayerRec(bounds, paint, kPreserveLCDText_SaveLayerFlag)); |
| 1125 | } |
| 1126 | |
| 1127 | int SkCanvas::saveLayer(const SaveLayerRec& origRec) { |
| 1128 | SaveLayerRec rec(origRec); |
| 1129 | if (gIgnoreSaveLayerBounds) { |
| 1130 | rec.fBounds = nullptr; |
| 1131 | } |
| 1132 | SaveLayerStrategy strategy = this->getSaveLayerStrategy(rec); |
| 1133 | fSaveCount += 1; |
| 1134 | this->internalSaveLayer(rec, strategy); |
| 1135 | return this->getSaveCount() - 1; |
reed | 70ee31b | 2015-12-10 13:44:45 -0800 | [diff] [blame] | 1136 | } |
| 1137 | |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 1138 | void SkCanvas::DrawDeviceWithFilter(SkBaseDevice* src, const SkImageFilter* filter, |
| 1139 | SkBaseDevice* dst, const SkMatrix& ctm, |
| 1140 | const SkClipStack* clipStack) { |
| 1141 | SkDraw draw; |
| 1142 | SkRasterClip rc; |
| 1143 | rc.setRect(SkIRect::MakeWH(dst->width(), dst->height())); |
| 1144 | if (!dst->accessPixels(&draw.fDst)) { |
| 1145 | draw.fDst.reset(dst->imageInfo(), nullptr, 0); |
robertphillips | 7354a4b | 2015-12-16 05:08:27 -0800 | [diff] [blame] | 1146 | } |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 1147 | draw.fMatrix = &SkMatrix::I(); |
| 1148 | draw.fRC = &rc; |
| 1149 | draw.fClipStack = clipStack; |
| 1150 | draw.fDevice = dst; |
robertphillips | 7354a4b | 2015-12-16 05:08:27 -0800 | [diff] [blame] | 1151 | |
| 1152 | SkPaint p; |
robertphillips | 372177e | 2016-03-30 07:32:28 -0700 | [diff] [blame] | 1153 | p.setImageFilter(filter->makeWithLocalMatrix(ctm)); |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 1154 | |
| 1155 | int x = src->getOrigin().x() - dst->getOrigin().x(); |
| 1156 | int y = src->getOrigin().y() - dst->getOrigin().y(); |
| 1157 | auto special = src->snapSpecial(); |
| 1158 | if (special) { |
| 1159 | dst->drawSpecial(draw, special.get(), x, y, p); |
| 1160 | } |
robertphillips | 7354a4b | 2015-12-16 05:08:27 -0800 | [diff] [blame] | 1161 | } |
reed | 70ee31b | 2015-12-10 13:44:45 -0800 | [diff] [blame] | 1162 | |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 1163 | static SkImageInfo make_layer_info(const SkImageInfo& prev, int w, int h, bool isOpaque, |
| 1164 | const SkPaint* paint) { |
| 1165 | // need to force L32 for now if we have an image filter. Once filters support other colortypes |
| 1166 | // e.g. sRGB or F16, we can remove this check |
brianosman | 52ede1d | 2016-06-20 08:25:02 -0700 | [diff] [blame] | 1167 | // SRGBTODO: Can we remove this check now? |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 1168 | const bool hasImageFilter = paint && paint->getImageFilter(); |
| 1169 | |
| 1170 | SkAlphaType alphaType = isOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType; |
| 1171 | if ((prev.bytesPerPixel() < 4) || hasImageFilter) { |
| 1172 | // force to L32 |
| 1173 | return SkImageInfo::MakeN32(w, h, alphaType); |
| 1174 | } else { |
| 1175 | // keep the same characteristics as the prev |
brianosman | 52ede1d | 2016-06-20 08:25:02 -0700 | [diff] [blame] | 1176 | return SkImageInfo::Make(w, h, prev.colorType(), alphaType, sk_ref_sp(prev.colorSpace())); |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 1177 | } |
| 1178 | } |
| 1179 | |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 1180 | void SkCanvas::internalSaveLayer(const SaveLayerRec& rec, SaveLayerStrategy strategy) { |
| 1181 | const SkRect* bounds = rec.fBounds; |
| 1182 | const SkPaint* paint = rec.fPaint; |
| 1183 | SaveLayerFlags saveLayerFlags = rec.fSaveLayerFlags; |
| 1184 | |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1185 | SkLazyPaint lazyP; |
| 1186 | SkImageFilter* imageFilter = paint ? paint->getImageFilter() : NULL; |
| 1187 | SkMatrix stashedMatrix = fMCRec->fMatrix; |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1188 | SkMatrix remainder; |
| 1189 | SkSize scale; |
| 1190 | /* |
| 1191 | * ImageFilters (so far) do not correctly handle matrices (CTM) that contain rotation/skew/etc. |
| 1192 | * but they do handle scaling. To accommodate this, we do the following: |
| 1193 | * |
| 1194 | * 1. Stash off the current CTM |
| 1195 | * 2. Decompose the CTM into SCALE and REMAINDER |
| 1196 | * 3. Wack the CTM to be just SCALE, and wrap the imagefilter with a MatrixImageFilter that |
| 1197 | * contains the REMAINDER |
| 1198 | * 4. Proceed as usual, allowing the client to draw into the layer (now with a scale-only CTM) |
| 1199 | * 5. During restore, we process the MatrixImageFilter, which applies REMAINDER to the output |
| 1200 | * of the original imagefilter, and draw that (via drawSprite) |
| 1201 | * 6. Unwack the CTM to its original state (i.e. stashedMatrix) |
| 1202 | * |
| 1203 | * Perhaps in the future we could augment #5 to apply REMAINDER as part of the draw (no longer |
| 1204 | * a sprite operation) to avoid the extra buffer/overhead of MatrixImageFilter. |
| 1205 | */ |
reed | 96a04f3 | 2016-04-25 09:25:15 -0700 | [diff] [blame] | 1206 | if (imageFilter && !stashedMatrix.isScaleTranslate() && !imageFilter->canHandleComplexCTM() && |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1207 | stashedMatrix.decomposeScale(&scale, &remainder)) |
| 1208 | { |
| 1209 | // We will restore the matrix (which we are overwriting here) in restore via fStashedMatrix |
| 1210 | this->internalSetMatrix(SkMatrix::MakeScale(scale.width(), scale.height())); |
| 1211 | SkPaint* p = lazyP.set(*paint); |
| 1212 | p->setImageFilter(SkImageFilter::MakeMatrixFilter(remainder, |
| 1213 | SkFilterQuality::kLow_SkFilterQuality, |
| 1214 | sk_ref_sp(imageFilter))); |
| 1215 | imageFilter = p->getImageFilter(); |
| 1216 | paint = p; |
| 1217 | } |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1218 | |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 1219 | // do this before we create the layer. We don't call the public save() since |
| 1220 | // that would invoke a possibly overridden virtual |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1221 | this->internalSave(); |
junov@chromium.org | a907ac3 | 2012-02-24 21:54:07 +0000 | [diff] [blame] | 1222 | |
| 1223 | fDeviceCMDirty = true; |
| 1224 | |
| 1225 | SkIRect ir; |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1226 | if (!this->clipRectBounds(bounds, saveLayerFlags, &ir, imageFilter)) { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1227 | return; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 1230 | // FIXME: do willSaveLayer() overriders returning kNoLayer_SaveLayerStrategy really care about |
| 1231 | // the clipRectBounds() call above? |
| 1232 | if (kNoLayer_SaveLayerStrategy == strategy) { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1233 | return; |
commit-bot@chromium.org | e54a23f | 2014-03-12 20:21:48 +0000 | [diff] [blame] | 1234 | } |
| 1235 | |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 1236 | bool isOpaque = SkToBool(saveLayerFlags & kIsOpaque_SaveLayerFlag); |
reed | 8dc0ccb | 2015-03-20 06:32:52 -0700 | [diff] [blame] | 1237 | SkPixelGeometry geo = fProps.pixelGeometry(); |
| 1238 | if (paint) { |
reed | 76033be | 2015-03-14 10:54:31 -0700 | [diff] [blame] | 1239 | // TODO: perhaps add a query to filters so we might preserve opaqueness... |
reed | daa57bf | 2015-05-15 10:39:17 -0700 | [diff] [blame] | 1240 | if (paint->getImageFilter() || paint->getColorFilter()) { |
reed | 76033be | 2015-03-14 10:54:31 -0700 | [diff] [blame] | 1241 | isOpaque = false; |
reed | 8dc0ccb | 2015-03-20 06:32:52 -0700 | [diff] [blame] | 1242 | geo = kUnknown_SkPixelGeometry; |
reed@google.com | b55deeb | 2012-01-06 14:43:09 +0000 | [diff] [blame] | 1243 | } |
| 1244 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1245 | |
robertphillips | 5139e50 | 2016-07-19 05:10:40 -0700 | [diff] [blame] | 1246 | SkBaseDevice* priorDevice = this->getTopDevice(); |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 1247 | if (nullptr == priorDevice) { |
reed | b2db898 | 2014-11-13 12:41:02 -0800 | [diff] [blame] | 1248 | SkDebugf("Unable to find device for layer."); |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1249 | return; |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1250 | } |
reed | b2db898 | 2014-11-13 12:41:02 -0800 | [diff] [blame] | 1251 | |
robertphillips | 5139e50 | 2016-07-19 05:10:40 -0700 | [diff] [blame] | 1252 | SkImageInfo info = make_layer_info(priorDevice->imageInfo(), ir.width(), ir.height(), isOpaque, |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 1253 | paint); |
| 1254 | |
Hal Canary | 704cd32 | 2016-11-07 14:13:52 -0500 | [diff] [blame] | 1255 | sk_sp<SkBaseDevice> newDevice; |
reed | 61f501f | 2015-04-29 08:34:00 -0700 | [diff] [blame] | 1256 | { |
reed | 70ee31b | 2015-12-10 13:44:45 -0800 | [diff] [blame] | 1257 | const bool preserveLCDText = kOpaque_SkAlphaType == info.alphaType() || |
reed | 4960eee | 2015-12-18 07:09:18 -0800 | [diff] [blame] | 1258 | (saveLayerFlags & kPreserveLCDText_SaveLayerFlag); |
reed | daa57bf | 2015-05-15 10:39:17 -0700 | [diff] [blame] | 1259 | const SkBaseDevice::TileUsage usage = SkBaseDevice::kNever_TileUsage; |
reed | 70ee31b | 2015-12-10 13:44:45 -0800 | [diff] [blame] | 1260 | const SkBaseDevice::CreateInfo createInfo = SkBaseDevice::CreateInfo(info, usage, geo, |
reed | cd4051e | 2016-07-15 09:41:26 -0700 | [diff] [blame] | 1261 | preserveLCDText); |
robertphillips | 5139e50 | 2016-07-19 05:10:40 -0700 | [diff] [blame] | 1262 | newDevice.reset(priorDevice->onCreateDevice(createInfo, paint)); |
| 1263 | if (!newDevice) { |
reed | 7503d60 | 2016-07-15 14:23:29 -0700 | [diff] [blame] | 1264 | return; |
reed | 61f501f | 2015-04-29 08:34:00 -0700 | [diff] [blame] | 1265 | } |
bungeman@google.com | e25c684 | 2011-08-17 14:53:54 +0000 | [diff] [blame] | 1266 | } |
robertphillips | 5139e50 | 2016-07-19 05:10:40 -0700 | [diff] [blame] | 1267 | newDevice->setOrigin(ir.fLeft, ir.fTop); |
robertphillips | 7354a4b | 2015-12-16 05:08:27 -0800 | [diff] [blame] | 1268 | |
Hal Canary | 704cd32 | 2016-11-07 14:13:52 -0500 | [diff] [blame] | 1269 | DeviceCM* layer = |
| 1270 | new DeviceCM(newDevice.get(), paint, this, fConservativeRasterClip, stashedMatrix); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1271 | |
| 1272 | layer->fNext = fMCRec->fTopLayer; |
| 1273 | fMCRec->fLayer = layer; |
| 1274 | fMCRec->fTopLayer = layer; // this field is NOT an owner of layer |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 1275 | |
| 1276 | if (rec.fBackdrop) { |
Hal Canary | 704cd32 | 2016-11-07 14:13:52 -0500 | [diff] [blame] | 1277 | DrawDeviceWithFilter(priorDevice, rec.fBackdrop, newDevice.get(), |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 1278 | fMCRec->fMatrix, this->getClipStack()); |
| 1279 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1280 | } |
| 1281 | |
commit-bot@chromium.org | d70fa20 | 2014-04-24 21:51:58 +0000 | [diff] [blame] | 1282 | int SkCanvas::saveLayerAlpha(const SkRect* bounds, U8CPU alpha) { |
reed | bada188 | 2015-12-21 13:09:44 -0800 | [diff] [blame] | 1283 | if (0xFF == alpha) { |
| 1284 | return this->saveLayer(bounds, nullptr); |
| 1285 | } else { |
| 1286 | SkPaint tmpPaint; |
| 1287 | tmpPaint.setAlpha(alpha); |
| 1288 | return this->saveLayer(bounds, &tmpPaint); |
| 1289 | } |
commit-bot@chromium.org | d70fa20 | 2014-04-24 21:51:58 +0000 | [diff] [blame] | 1290 | } |
| 1291 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1292 | void SkCanvas::internalRestore() { |
| 1293 | SkASSERT(fMCStack.count() != 0); |
| 1294 | |
| 1295 | fDeviceCMDirty = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1296 | |
reed | 687fa1c | 2015-04-07 08:00:56 -0700 | [diff] [blame] | 1297 | fClipStack->restore(); |
commit-bot@chromium.org | 6c15764 | 2013-08-16 00:53:34 +0000 | [diff] [blame] | 1298 | |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 1299 | // reserve our layer (if any) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1300 | DeviceCM* layer = fMCRec->fLayer; // may be null |
| 1301 | // 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] | 1302 | fMCRec->fLayer = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1303 | |
| 1304 | // now do the normal restore() |
| 1305 | fMCRec->~MCRec(); // balanced in save() |
| 1306 | fMCStack.pop_back(); |
| 1307 | fMCRec = (MCRec*)fMCStack.back(); |
| 1308 | |
| 1309 | /* Time to draw the layer's offscreen. We can't call the public drawSprite, |
| 1310 | since if we're being recorded, we don't want to record this (the |
| 1311 | recorder will have already recorded the restore). |
| 1312 | */ |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1313 | if (layer) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1314 | if (layer->fNext) { |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 1315 | const SkIPoint& origin = layer->fDevice->getOrigin(); |
reed | 7503d60 | 2016-07-15 14:23:29 -0700 | [diff] [blame] | 1316 | this->internalDrawDevice(layer->fDevice, origin.x(), origin.y(), layer->fPaint); |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1317 | // restore what we smashed in internalSaveLayer |
| 1318 | fMCRec->fMatrix = layer->fStashedMatrix; |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1319 | // reset this, since internalDrawDevice will have set it to true |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1320 | fDeviceCMDirty = true; |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 1321 | delete layer; |
reed | b679ca8 | 2015-04-07 04:40:48 -0700 | [diff] [blame] | 1322 | } else { |
| 1323 | // we're at the root |
reed | a499f90 | 2015-05-01 09:34:31 -0700 | [diff] [blame] | 1324 | SkASSERT(layer == (void*)fDeviceCMStorage); |
reed | b679ca8 | 2015-04-07 04:40:48 -0700 | [diff] [blame] | 1325 | layer->~DeviceCM(); |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1326 | // no need to update fMCRec, 'cause we're killing the canvas |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1327 | } |
bungeman@google.com | 88edf1e | 2011-08-08 19:41:56 +0000 | [diff] [blame] | 1328 | } |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1329 | |
| 1330 | if (fMCRec) { |
msarett | 9637ea9 | 2016-08-18 14:03:30 -0700 | [diff] [blame] | 1331 | fIsScaleTranslate = fMCRec->fMatrix.isScaleTranslate(); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1332 | fDeviceClipBounds = qr_clip_bounds(fMCRec->fRasterClip.getBounds()); |
| 1333 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1334 | } |
| 1335 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1336 | sk_sp<SkSurface> SkCanvas::makeSurface(const SkImageInfo& info, const SkSurfaceProps* props) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1337 | if (nullptr == props) { |
reed | 4a8126e | 2014-09-22 07:29:03 -0700 | [diff] [blame] | 1338 | props = &fProps; |
| 1339 | } |
| 1340 | return this->onNewSurface(info, *props); |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1343 | sk_sp<SkSurface> SkCanvas::onNewSurface(const SkImageInfo& info, const SkSurfaceProps& props) { |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 1344 | SkBaseDevice* dev = this->getDevice(); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 1345 | return dev ? dev->makeSurface(info, props) : nullptr; |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1348 | SkImageInfo SkCanvas::imageInfo() const { |
reed | ea5a651 | 2016-07-07 16:44:27 -0700 | [diff] [blame] | 1349 | return this->onImageInfo(); |
| 1350 | } |
| 1351 | |
| 1352 | SkImageInfo SkCanvas::onImageInfo() const { |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1353 | SkBaseDevice* dev = this->getDevice(); |
| 1354 | if (dev) { |
| 1355 | return dev->imageInfo(); |
| 1356 | } else { |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 1357 | return SkImageInfo::MakeUnknown(0, 0); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1358 | } |
| 1359 | } |
| 1360 | |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 1361 | bool SkCanvas::getProps(SkSurfaceProps* props) const { |
reed | ea5a651 | 2016-07-07 16:44:27 -0700 | [diff] [blame] | 1362 | return this->onGetProps(props); |
| 1363 | } |
| 1364 | |
| 1365 | bool SkCanvas::onGetProps(SkSurfaceProps* props) const { |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 1366 | SkBaseDevice* dev = this->getDevice(); |
| 1367 | if (dev) { |
| 1368 | if (props) { |
| 1369 | *props = fProps; |
| 1370 | } |
| 1371 | return true; |
| 1372 | } else { |
| 1373 | return false; |
| 1374 | } |
| 1375 | } |
| 1376 | |
reed | 6ceeebd | 2016-03-09 14:26:26 -0800 | [diff] [blame] | 1377 | bool SkCanvas::peekPixels(SkPixmap* pmap) { |
| 1378 | return this->onPeekPixels(pmap); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 1381 | bool SkCanvas::onPeekPixels(SkPixmap* pmap) { |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1382 | SkBaseDevice* dev = this->getDevice(); |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 1383 | return dev && dev->peekPixels(pmap); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
commit-bot@chromium.org | 6b4aaa7 | 2014-04-21 21:09:38 +0000 | [diff] [blame] | 1386 | void* SkCanvas::accessTopLayerPixels(SkImageInfo* info, size_t* rowBytes, SkIPoint* origin) { |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 1387 | SkPixmap pmap; |
| 1388 | if (!this->onAccessTopLayerPixels(&pmap)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1389 | return nullptr; |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 1390 | } |
| 1391 | if (info) { |
| 1392 | *info = pmap.info(); |
| 1393 | } |
| 1394 | if (rowBytes) { |
| 1395 | *rowBytes = pmap.rowBytes(); |
| 1396 | } |
| 1397 | if (origin) { |
commit-bot@chromium.org | 6b4aaa7 | 2014-04-21 21:09:38 +0000 | [diff] [blame] | 1398 | *origin = this->getTopDevice(false)->getOrigin(); |
| 1399 | } |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 1400 | return pmap.writable_addr(); |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 1403 | bool SkCanvas::onAccessTopLayerPixels(SkPixmap* pmap) { |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1404 | SkBaseDevice* dev = this->getTopDevice(); |
reed | 884e97c | 2015-05-26 11:31:54 -0700 | [diff] [blame] | 1405 | return dev && dev->accessPixels(pmap); |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1408 | ///////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1409 | |
reed | 7503d60 | 2016-07-15 14:23:29 -0700 | [diff] [blame] | 1410 | void SkCanvas::internalDrawDevice(SkBaseDevice* srcDev, int x, int y, const SkPaint* paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1411 | SkPaint tmp; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1412 | if (nullptr == paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1413 | paint = &tmp; |
| 1414 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 1415 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 1416 | LOOPER_BEGIN_DRAWDEVICE(*paint, SkDrawFilter::kBitmap_Type) |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 1417 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1418 | while (iter.next()) { |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1419 | SkBaseDevice* dstDev = iter.fDevice; |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1420 | paint = &looper.paint(); |
| 1421 | SkImageFilter* filter = paint->getImageFilter(); |
| 1422 | SkIPoint pos = { x - iter.getX(), y - iter.getY() }; |
Robert Phillips | 833dcf4 | 2016-11-18 08:44:13 -0500 | [diff] [blame] | 1423 | if (filter) { |
| 1424 | sk_sp<SkSpecialImage> specialImage = srcDev->snapSpecial(); |
| 1425 | if (specialImage) { |
| 1426 | dstDev->drawSpecial(iter, specialImage.get(), pos.x(), pos.y(), *paint); |
| 1427 | } |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1428 | } else { |
reed@google.com | b55deeb | 2012-01-06 14:43:09 +0000 | [diff] [blame] | 1429 | dstDev->drawDevice(iter, srcDev, pos.x(), pos.y(), *paint); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 1430 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1431 | } |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 1432 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 1433 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
reed | 3270467 | 2015-12-16 08:27:10 -0800 | [diff] [blame] | 1436 | ///////////////////////////////////////////////////////////////////////////// |
reed | da420b9 | 2015-12-16 08:38:15 -0800 | [diff] [blame] | 1437 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1438 | void SkCanvas::translate(SkScalar dx, SkScalar dy) { |
reed | fe69b50 | 2016-09-12 06:31:48 -0700 | [diff] [blame] | 1439 | if (dx || dy) { |
| 1440 | this->checkForDeferredSave(); |
| 1441 | fDeviceCMDirty = true; |
| 1442 | fMCRec->fMatrix.preTranslate(dx,dy); |
mtklein | cbdf007 | 2016-08-19 09:05:27 -0700 | [diff] [blame] | 1443 | |
reed | fe69b50 | 2016-09-12 06:31:48 -0700 | [diff] [blame] | 1444 | // Translate shouldn't affect the is-scale-translateness of the matrix. |
| 1445 | SkASSERT(fIsScaleTranslate == fMCRec->fMatrix.isScaleTranslate()); |
mtklein | cbdf007 | 2016-08-19 09:05:27 -0700 | [diff] [blame] | 1446 | |
reed | fe69b50 | 2016-09-12 06:31:48 -0700 | [diff] [blame] | 1447 | this->didTranslate(dx,dy); |
| 1448 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1449 | } |
| 1450 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1451 | void SkCanvas::scale(SkScalar sx, SkScalar sy) { |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 1452 | SkMatrix m; |
| 1453 | m.setScale(sx, sy); |
| 1454 | this->concat(m); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1455 | } |
| 1456 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1457 | void SkCanvas::rotate(SkScalar degrees) { |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 1458 | SkMatrix m; |
| 1459 | m.setRotate(degrees); |
| 1460 | this->concat(m); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1461 | } |
| 1462 | |
bungeman | 7438bfc | 2016-07-12 15:01:19 -0700 | [diff] [blame] | 1463 | void SkCanvas::rotate(SkScalar degrees, SkScalar px, SkScalar py) { |
| 1464 | SkMatrix m; |
| 1465 | m.setRotate(degrees, px, py); |
| 1466 | this->concat(m); |
| 1467 | } |
| 1468 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1469 | void SkCanvas::skew(SkScalar sx, SkScalar sy) { |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 1470 | SkMatrix m; |
| 1471 | m.setSkew(sx, sy); |
| 1472 | this->concat(m); |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1475 | void SkCanvas::concat(const SkMatrix& matrix) { |
commit-bot@chromium.org | d9ea09e | 2014-03-25 17:32:26 +0000 | [diff] [blame] | 1476 | if (matrix.isIdentity()) { |
| 1477 | return; |
| 1478 | } |
| 1479 | |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1480 | this->checkForDeferredSave(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1481 | fDeviceCMDirty = true; |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1482 | fMCRec->fMatrix.preConcat(matrix); |
msarett | 9637ea9 | 2016-08-18 14:03:30 -0700 | [diff] [blame] | 1483 | fIsScaleTranslate = fMCRec->fMatrix.isScaleTranslate(); |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 1484 | this->didConcat(matrix); |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1487 | void SkCanvas::internalSetMatrix(const SkMatrix& matrix) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1488 | fDeviceCMDirty = true; |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1489 | fMCRec->fMatrix = matrix; |
msarett | 9da5a5a | 2016-08-19 08:38:36 -0700 | [diff] [blame] | 1490 | fIsScaleTranslate = matrix.isScaleTranslate(); |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | void SkCanvas::setMatrix(const SkMatrix& matrix) { |
| 1494 | this->checkForDeferredSave(); |
| 1495 | this->internalSetMatrix(matrix); |
commit-bot@chromium.org | 44c48d0 | 2014-03-13 20:03:58 +0000 | [diff] [blame] | 1496 | this->didSetMatrix(matrix); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1499 | void SkCanvas::resetMatrix() { |
reed | 8c30a81 | 2016-04-20 16:36:51 -0700 | [diff] [blame] | 1500 | this->setMatrix(SkMatrix::I()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
vjiaoblack | 95302da | 2016-07-21 10:25:54 -0700 | [diff] [blame] | 1503 | #ifdef SK_EXPERIMENTAL_SHADOWING |
vjiaoblack | e5de130 | 2016-07-13 14:05:28 -0700 | [diff] [blame] | 1504 | void SkCanvas::translateZ(SkScalar z) { |
| 1505 | this->checkForDeferredSave(); |
| 1506 | this->fMCRec->fCurDrawDepth += z; |
| 1507 | this->didTranslateZ(z); |
| 1508 | } |
| 1509 | |
| 1510 | SkScalar SkCanvas::getZ() const { |
| 1511 | return this->fMCRec->fCurDrawDepth; |
| 1512 | } |
| 1513 | |
vjiaoblack | 95302da | 2016-07-21 10:25:54 -0700 | [diff] [blame] | 1514 | void SkCanvas::setLights(sk_sp<SkLights> lights) { |
| 1515 | this->fLights = lights; |
| 1516 | } |
| 1517 | |
| 1518 | sk_sp<SkLights> SkCanvas::getLights() const { |
| 1519 | return this->fLights; |
| 1520 | } |
| 1521 | #endif |
| 1522 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1523 | ////////////////////////////////////////////////////////////////////////////// |
| 1524 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1525 | void SkCanvas::clipRect(const SkRect& rect, SkClipOp op, bool doAA) { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1526 | this->checkForDeferredSave(); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1527 | ClipEdgeStyle edgeStyle = doAA ? kSoft_ClipEdgeStyle : kHard_ClipEdgeStyle; |
| 1528 | this->onClipRect(rect, op, edgeStyle); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1531 | void SkCanvas::onClipRect(const SkRect& rect, SkClipOp op, ClipEdgeStyle edgeStyle) { |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1532 | const bool isAA = kSoft_ClipEdgeStyle == edgeStyle; |
reed | c64eff5 | 2015-11-21 12:39:45 -0800 | [diff] [blame] | 1533 | AutoValidateClip avc(this); |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1534 | fClipStack->clipRect(rect, fMCRec->fMatrix, op, isAA); |
| 1535 | fMCRec->fRasterClip.op(rect, fMCRec->fMatrix, this->getTopLayerBounds(), (SkRegion::Op)op, |
| 1536 | isAA); |
reed | c64eff5 | 2015-11-21 12:39:45 -0800 | [diff] [blame] | 1537 | fDeviceCMDirty = true; |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1538 | fDeviceClipBounds = qr_clip_bounds(fMCRec->fRasterClip.getBounds()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1541 | void SkCanvas::clipRRect(const SkRRect& rrect, SkClipOp op, bool doAA) { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1542 | this->checkForDeferredSave(); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1543 | ClipEdgeStyle edgeStyle = doAA ? kSoft_ClipEdgeStyle : kHard_ClipEdgeStyle; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1544 | if (rrect.isRect()) { |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1545 | this->onClipRect(rrect.getBounds(), op, edgeStyle); |
| 1546 | } else { |
| 1547 | this->onClipRRect(rrect, op, edgeStyle); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1548 | } |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1549 | } |
commit-bot@chromium.org | 14e50ae | 2014-02-16 23:35:31 +0000 | [diff] [blame] | 1550 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1551 | void SkCanvas::onClipRRect(const SkRRect& rrect, SkClipOp op, ClipEdgeStyle edgeStyle) { |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1552 | AutoValidateClip avc(this); |
commit-bot@chromium.org | 14e50ae | 2014-02-16 23:35:31 +0000 | [diff] [blame] | 1553 | |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1554 | fDeviceCMDirty = true; |
commit-bot@chromium.org | 14e50ae | 2014-02-16 23:35:31 +0000 | [diff] [blame] | 1555 | |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1556 | bool isAA = kSoft_ClipEdgeStyle == edgeStyle; |
| 1557 | fClipStack->clipRRect(rrect, fMCRec->fMatrix, op, isAA); |
| 1558 | fMCRec->fRasterClip.op(rrect, fMCRec->fMatrix, this->getTopLayerBounds(), (SkRegion::Op)op, |
| 1559 | isAA); |
| 1560 | fDeviceClipBounds = qr_clip_bounds(fMCRec->fRasterClip.getBounds()); |
| 1561 | return; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1564 | void SkCanvas::clipPath(const SkPath& path, SkClipOp op, bool doAA) { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1565 | this->checkForDeferredSave(); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1566 | ClipEdgeStyle edgeStyle = doAA ? kSoft_ClipEdgeStyle : kHard_ClipEdgeStyle; |
robertphillips | 39f0538 | 2015-11-24 09:30:12 -0800 | [diff] [blame] | 1567 | |
| 1568 | if (!path.isInverseFillType() && fMCRec->fMatrix.rectStaysRect()) { |
| 1569 | SkRect r; |
| 1570 | if (path.isRect(&r)) { |
| 1571 | this->onClipRect(r, op, edgeStyle); |
| 1572 | return; |
| 1573 | } |
| 1574 | SkRRect rrect; |
| 1575 | if (path.isOval(&r)) { |
| 1576 | rrect.setOval(r); |
| 1577 | this->onClipRRect(rrect, op, edgeStyle); |
| 1578 | return; |
| 1579 | } |
| 1580 | if (path.isRRect(&rrect)) { |
| 1581 | this->onClipRRect(rrect, op, edgeStyle); |
| 1582 | return; |
| 1583 | } |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1584 | } |
robertphillips | 39f0538 | 2015-11-24 09:30:12 -0800 | [diff] [blame] | 1585 | |
| 1586 | this->onClipPath(path, op, edgeStyle); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1587 | } |
| 1588 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1589 | void SkCanvas::onClipPath(const SkPath& path, SkClipOp op, ClipEdgeStyle edgeStyle) { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1590 | AutoValidateClip avc(this); |
| 1591 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1592 | fDeviceCMDirty = true; |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1593 | bool isAA = kSoft_ClipEdgeStyle == edgeStyle; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1594 | |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1595 | fClipStack->clipPath(path, fMCRec->fMatrix, op, isAA); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1596 | |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1597 | const SkPath* rasterClipPath = &path; |
| 1598 | const SkMatrix* matrix = &fMCRec->fMatrix; |
| 1599 | SkPath tempPath; |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 1600 | if (fAllowSimplifyClip) { |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1601 | isAA = getClipStack()->asPath(&tempPath); |
| 1602 | rasterClipPath = &tempPath; |
| 1603 | matrix = &SkMatrix::I(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1604 | op = kReplace_SkClipOp; |
caryclark@google.com | 45a75fb | 2013-04-25 13:34:40 +0000 | [diff] [blame] | 1605 | } |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1606 | fMCRec->fRasterClip.op(*rasterClipPath, *matrix, this->getTopLayerBounds(), (SkRegion::Op)op, |
| 1607 | isAA); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1608 | fDeviceClipBounds = qr_clip_bounds(fMCRec->fRasterClip.getBounds()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1611 | void SkCanvas::clipRegion(const SkRegion& rgn, SkClipOp op) { |
reed | 2ff1fce | 2014-12-11 07:07:37 -0800 | [diff] [blame] | 1612 | this->checkForDeferredSave(); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1613 | this->onClipRegion(rgn, op); |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 1616 | void SkCanvas::onClipRegion(const SkRegion& rgn, SkClipOp op) { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1617 | AutoValidateClip avc(this); |
| 1618 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1619 | fDeviceCMDirty = true; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1620 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1621 | // todo: signal fClipStack that we have a region, and therefore (I guess) |
| 1622 | // we have to ignore it, and use the region directly? |
reed | 687fa1c | 2015-04-07 08:00:56 -0700 | [diff] [blame] | 1623 | fClipStack->clipDevRect(rgn.getBounds(), op); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1624 | |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 1625 | fMCRec->fRasterClip.op(rgn, (SkRegion::Op)op); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1626 | fDeviceClipBounds = qr_clip_bounds(fMCRec->fRasterClip.getBounds()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1629 | #ifdef SK_DEBUG |
| 1630 | void SkCanvas::validateClip() const { |
| 1631 | // construct clipRgn from the clipstack |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 1632 | const SkBaseDevice* device = this->getDevice(); |
djsollen@google.com | ccfee2a | 2012-05-01 16:50:10 +0000 | [diff] [blame] | 1633 | if (!device) { |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1634 | SkASSERT(this->isClipEmpty()); |
djsollen@google.com | ccfee2a | 2012-05-01 16:50:10 +0000 | [diff] [blame] | 1635 | return; |
| 1636 | } |
| 1637 | |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1638 | SkIRect ir; |
| 1639 | ir.set(0, 0, device->width(), device->height()); |
reed | d954498 | 2014-09-09 18:46:22 -0700 | [diff] [blame] | 1640 | SkRasterClip tmpClip(ir, fConservativeRasterClip); |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1641 | |
reed | 687fa1c | 2015-04-07 08:00:56 -0700 | [diff] [blame] | 1642 | SkClipStack::B2TIter iter(*fClipStack); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1643 | const SkClipStack::Element* element; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1644 | while ((element = iter.next()) != nullptr) { |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1645 | switch (element->getType()) { |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1646 | case SkClipStack::Element::kRect_Type: |
| 1647 | element->getRect().round(&ir); |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 1648 | tmpClip.op(ir, (SkRegion::Op)element->getOp()); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1649 | break; |
| 1650 | case SkClipStack::Element::kEmpty_Type: |
| 1651 | tmpClip.setEmpty(); |
| 1652 | break; |
commit-bot@chromium.org | 9cb671a | 2014-02-16 14:45:45 +0000 | [diff] [blame] | 1653 | default: { |
| 1654 | SkPath path; |
| 1655 | element->asPath(&path); |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 1656 | tmpClip.op(path, SkMatrix::I(), this->getTopLayerBounds(), |
| 1657 | (SkRegion::Op)element->getOp(), element->isAA()); |
commit-bot@chromium.org | 9cb671a | 2014-02-16 14:45:45 +0000 | [diff] [blame] | 1658 | break; |
| 1659 | } |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1660 | } |
| 1661 | } |
reed@google.com | 819c921 | 2011-02-23 18:56:55 +0000 | [diff] [blame] | 1662 | } |
| 1663 | #endif |
| 1664 | |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 1665 | void SkCanvas::replayClips(ClipVisitor* visitor) const { |
reed | 687fa1c | 2015-04-07 08:00:56 -0700 | [diff] [blame] | 1666 | SkClipStack::B2TIter iter(*fClipStack); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1667 | const SkClipStack::Element* element; |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 1668 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1669 | while ((element = iter.next()) != nullptr) { |
fmalita | c3b589a | 2014-06-05 12:40:07 -0700 | [diff] [blame] | 1670 | element->replay(visitor); |
reed@google.com | 90c07ea | 2012-04-13 13:50:27 +0000 | [diff] [blame] | 1671 | } |
| 1672 | } |
| 1673 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 1674 | /////////////////////////////////////////////////////////////////////////////// |
| 1675 | |
reed@google.com | 754de5f | 2014-02-24 19:38:20 +0000 | [diff] [blame] | 1676 | bool SkCanvas::isClipEmpty() const { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1677 | return fMCRec->fRasterClip.isEmpty(); |
reed@google.com | 754de5f | 2014-02-24 19:38:20 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1680 | bool SkCanvas::isClipRect() const { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1681 | return fMCRec->fRasterClip.isRect(); |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1682 | } |
| 1683 | |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1684 | static inline bool is_nan_or_clipped(const Sk4f& devRect, const Sk4f& devClip) { |
| 1685 | #if !defined(SKNX_NO_SIMD) && SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE2 |
| 1686 | __m128 lLtT = _mm_unpacklo_ps(devRect.fVec, devClip.fVec); |
| 1687 | __m128 RrBb = _mm_unpackhi_ps(devClip.fVec, devRect.fVec); |
| 1688 | __m128 mask = _mm_cmplt_ps(lLtT, RrBb); |
| 1689 | return 0xF != _mm_movemask_ps(mask); |
| 1690 | #elif !defined(SKNX_NO_SIMD) && defined(SK_ARM_HAS_NEON) |
| 1691 | float32x4_t lLtT = vzipq_f32(devRect.fVec, devClip.fVec).val[0]; |
| 1692 | float32x4_t RrBb = vzipq_f32(devClip.fVec, devRect.fVec).val[1]; |
| 1693 | uint32x4_t mask = vcltq_f32(lLtT, RrBb); |
| 1694 | return 0xFFFFFFFFFFFFFFFF != (uint64_t) vmovn_u32(mask); |
| 1695 | #else |
| 1696 | SkRect devRectAsRect; |
| 1697 | SkRect devClipAsRect; |
| 1698 | devRect.store(&devRectAsRect.fLeft); |
| 1699 | devClip.store(&devClipAsRect.fLeft); |
| 1700 | return !devRectAsRect.isFinite() || !devRectAsRect.intersect(devClipAsRect); |
| 1701 | #endif |
| 1702 | } |
wjmaclean@chromium.org | 116b2bc | 2011-02-07 17:48:37 +0000 | [diff] [blame] | 1703 | |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1704 | // It's important for this function to not be inlined. Otherwise the compiler will share code |
| 1705 | // between the fast path and the slow path, resulting in two slow paths. |
| 1706 | static SK_NEVER_INLINE bool quick_reject_slow_path(const SkRect& src, const SkRect& deviceClip, |
| 1707 | const SkMatrix& matrix) { |
| 1708 | SkRect deviceRect; |
| 1709 | matrix.mapRect(&deviceRect, src); |
| 1710 | return !deviceRect.isFinite() || !deviceRect.intersect(deviceClip); |
| 1711 | } |
| 1712 | |
| 1713 | bool SkCanvas::quickReject(const SkRect& src) const { |
| 1714 | #ifdef SK_DEBUG |
| 1715 | // Verify that fDeviceClipBounds are set properly. |
| 1716 | SkRect tmp = qr_clip_bounds(fMCRec->fRasterClip.getBounds()); |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1717 | if (fMCRec->fRasterClip.isEmpty()) { |
msarett | 0c685ee | 2016-08-14 13:51:16 -0700 | [diff] [blame] | 1718 | SkASSERT(fDeviceClipBounds.isEmpty()); |
reed@android.com | a380ae4 | 2009-07-21 01:17:02 +0000 | [diff] [blame] | 1719 | } else { |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1720 | SkASSERT(tmp == fDeviceClipBounds); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1721 | } |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1722 | |
msarett | 9637ea9 | 2016-08-18 14:03:30 -0700 | [diff] [blame] | 1723 | // Verify that fIsScaleTranslate is set properly. |
| 1724 | SkASSERT(fIsScaleTranslate == fMCRec->fMatrix.isScaleTranslate()); |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1725 | #endif |
| 1726 | |
msarett | 9637ea9 | 2016-08-18 14:03:30 -0700 | [diff] [blame] | 1727 | if (!fIsScaleTranslate) { |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 1728 | return quick_reject_slow_path(src, fDeviceClipBounds, fMCRec->fMatrix); |
| 1729 | } |
| 1730 | |
| 1731 | // We inline the implementation of mapScaleTranslate() for the fast path. |
| 1732 | float sx = fMCRec->fMatrix.getScaleX(); |
| 1733 | float sy = fMCRec->fMatrix.getScaleY(); |
| 1734 | float tx = fMCRec->fMatrix.getTranslateX(); |
| 1735 | float ty = fMCRec->fMatrix.getTranslateY(); |
| 1736 | Sk4f scale(sx, sy, sx, sy); |
| 1737 | Sk4f trans(tx, ty, tx, ty); |
| 1738 | |
| 1739 | // Apply matrix. |
| 1740 | Sk4f ltrb = Sk4f::Load(&src.fLeft) * scale + trans; |
| 1741 | |
| 1742 | // Make sure left < right, top < bottom. |
| 1743 | Sk4f rblt(ltrb[2], ltrb[3], ltrb[0], ltrb[1]); |
| 1744 | Sk4f min = Sk4f::Min(ltrb, rblt); |
| 1745 | Sk4f max = Sk4f::Max(ltrb, rblt); |
| 1746 | // We can extract either pair [0,1] or [2,3] from min and max and be correct, but on |
| 1747 | // ARM this sequence generates the fastest (a single instruction). |
| 1748 | Sk4f devRect = Sk4f(min[2], min[3], max[0], max[1]); |
| 1749 | |
| 1750 | // Check if the device rect is NaN or outside the clip. |
| 1751 | return is_nan_or_clipped(devRect, Sk4f::Load(&fDeviceClipBounds.fLeft)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1754 | bool SkCanvas::quickReject(const SkPath& path) const { |
| 1755 | return path.isEmpty() || this->quickReject(path.getBounds()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1758 | bool SkCanvas::getClipBounds(SkRect* bounds) const { |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1759 | SkIRect ibounds; |
robertphillips@google.com | 8f90a89 | 2014-02-28 18:19:39 +0000 | [diff] [blame] | 1760 | if (!this->getClipDeviceBounds(&ibounds)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1761 | return false; |
| 1762 | } |
| 1763 | |
reed@android.com | d9c0f0b | 2009-02-06 22:39:37 +0000 | [diff] [blame] | 1764 | SkMatrix inverse; |
| 1765 | // if we can't invert the CTM, we can't return local clip bounds |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1766 | if (!fMCRec->fMatrix.invert(&inverse)) { |
reed@android.com | 72dcd3a | 2009-05-18 15:46:29 +0000 | [diff] [blame] | 1767 | if (bounds) { |
| 1768 | bounds->setEmpty(); |
| 1769 | } |
reed@android.com | d9c0f0b | 2009-02-06 22:39:37 +0000 | [diff] [blame] | 1770 | return false; |
| 1771 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1772 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1773 | if (bounds) { |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1774 | SkRect r; |
reed@google.com | 3b3e895 | 2012-08-16 20:53:31 +0000 | [diff] [blame] | 1775 | // adjust it outwards in case we are antialiasing |
| 1776 | const int inset = 1; |
reed@google.com | fa4d5bd | 2012-01-30 17:36:27 +0000 | [diff] [blame] | 1777 | |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 1778 | r.iset(ibounds.fLeft - inset, ibounds.fTop - inset, |
| 1779 | ibounds.fRight + inset, ibounds.fBottom + inset); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1780 | inverse.mapRect(bounds, r); |
| 1781 | } |
| 1782 | return true; |
| 1783 | } |
| 1784 | |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1785 | bool SkCanvas::getClipDeviceBounds(SkIRect* bounds) const { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1786 | const SkRasterClip& clip = fMCRec->fRasterClip; |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1787 | if (clip.isEmpty()) { |
| 1788 | if (bounds) { |
| 1789 | bounds->setEmpty(); |
| 1790 | } |
| 1791 | return false; |
| 1792 | } |
| 1793 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1794 | if (bounds) { |
tomhudson@google.com | bcb671c | 2011-09-13 15:07:58 +0000 | [diff] [blame] | 1795 | *bounds = clip.getBounds(); |
| 1796 | } |
| 1797 | return true; |
| 1798 | } |
| 1799 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1800 | const SkMatrix& SkCanvas::getTotalMatrix() const { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1801 | return fMCRec->fMatrix; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1804 | const SkRegion& SkCanvas::internal_private_getTotalClip() const { |
reed | 1f836ee | 2014-07-07 07:49:34 -0700 | [diff] [blame] | 1805 | return fMCRec->fRasterClip.forceGetBW(); |
commit-bot@chromium.org | 5c70cdc | 2014-03-08 03:57:19 +0000 | [diff] [blame] | 1806 | } |
| 1807 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1808 | GrRenderTargetContext* SkCanvas::internal_private_accessTopLayerRenderTargetContext() { |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1809 | SkBaseDevice* dev = this->getTopDevice(); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1810 | return dev ? dev->accessRenderTargetContext() : nullptr; |
reed@google.com | 9c135db | 2014-03-12 18:28:35 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 1813 | GrContext* SkCanvas::getGrContext() { |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 1814 | SkBaseDevice* device = this->getTopDevice(); |
reed | 86ae3d1 | 2016-04-26 06:57:31 -0700 | [diff] [blame] | 1815 | return device ? device->context() : nullptr; |
commit-bot@chromium.org | 644629c | 2013-11-21 06:21:58 +0000 | [diff] [blame] | 1816 | } |
bsalomon@google.com | e97f085 | 2011-06-17 13:10:25 +0000 | [diff] [blame] | 1817 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1818 | void SkCanvas::drawDRRect(const SkRRect& outer, const SkRRect& inner, |
| 1819 | const SkPaint& paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 1820 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawDRRect()"); |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 1821 | if (outer.isEmpty()) { |
| 1822 | return; |
| 1823 | } |
| 1824 | if (inner.isEmpty()) { |
| 1825 | this->drawRRect(outer, paint); |
| 1826 | return; |
| 1827 | } |
| 1828 | |
| 1829 | // We don't have this method (yet), but technically this is what we should |
| 1830 | // be able to assert... |
| 1831 | // SkASSERT(outer.contains(inner)); |
| 1832 | // |
| 1833 | // For now at least check for containment of bounds |
| 1834 | SkASSERT(outer.getBounds().contains(inner.getBounds())); |
| 1835 | |
| 1836 | this->onDrawDRRect(outer, inner, paint); |
| 1837 | } |
| 1838 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1839 | // These need to stop being virtual -- clients need to override the onDraw... versions |
| 1840 | |
| 1841 | void SkCanvas::drawPaint(const SkPaint& paint) { |
| 1842 | this->onDrawPaint(paint); |
| 1843 | } |
| 1844 | |
| 1845 | void SkCanvas::drawRect(const SkRect& r, const SkPaint& paint) { |
| 1846 | this->onDrawRect(r, paint); |
| 1847 | } |
| 1848 | |
msarett | dca352e | 2016-08-26 06:37:45 -0700 | [diff] [blame] | 1849 | void SkCanvas::drawRegion(const SkRegion& region, const SkPaint& paint) { |
| 1850 | if (region.isEmpty()) { |
| 1851 | return; |
| 1852 | } |
| 1853 | |
| 1854 | if (region.isRect()) { |
| 1855 | return this->drawIRect(region.getBounds(), paint); |
| 1856 | } |
| 1857 | |
| 1858 | this->onDrawRegion(region, paint); |
| 1859 | } |
| 1860 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1861 | void SkCanvas::drawOval(const SkRect& r, const SkPaint& paint) { |
| 1862 | this->onDrawOval(r, paint); |
| 1863 | } |
| 1864 | |
| 1865 | void SkCanvas::drawRRect(const SkRRect& rrect, const SkPaint& paint) { |
| 1866 | this->onDrawRRect(rrect, paint); |
| 1867 | } |
| 1868 | |
| 1869 | void SkCanvas::drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint& paint) { |
| 1870 | this->onDrawPoints(mode, count, pts, paint); |
| 1871 | } |
| 1872 | |
| 1873 | void SkCanvas::drawVertices(VertexMode vmode, int vertexCount, const SkPoint vertices[], |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 1874 | const SkPoint texs[], const SkColor colors[], SkBlendMode bmode, |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1875 | const uint16_t indices[], int indexCount, const SkPaint& paint) { |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 1876 | this->onDrawVertices(vmode, vertexCount, vertices, texs, colors, bmode, |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1877 | indices, indexCount, paint); |
| 1878 | } |
| 1879 | |
| 1880 | void SkCanvas::drawPath(const SkPath& path, const SkPaint& paint) { |
| 1881 | this->onDrawPath(path, paint); |
| 1882 | } |
| 1883 | |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 1884 | void SkCanvas::drawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) { |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 1885 | RETURN_ON_NULL(image); |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 1886 | this->onDrawImage(image, x, y, paint); |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1887 | } |
| 1888 | |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1889 | void SkCanvas::drawImageRect(const SkImage* image, const SkRect& src, const SkRect& dst, |
| 1890 | const SkPaint* paint, SrcRectConstraint constraint) { |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 1891 | RETURN_ON_NULL(image); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1892 | if (dst.isEmpty() || src.isEmpty()) { |
| 1893 | return; |
| 1894 | } |
| 1895 | this->onDrawImageRect(image, &src, dst, paint, constraint); |
| 1896 | } |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1897 | |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 1898 | void SkCanvas::drawImageRect(const SkImage* image, const SkIRect& isrc, const SkRect& dst, |
| 1899 | const SkPaint* paint, SrcRectConstraint constraint) { |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 1900 | RETURN_ON_NULL(image); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1901 | this->drawImageRect(image, SkRect::Make(isrc), dst, paint, constraint); |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 1902 | } |
| 1903 | |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1904 | void SkCanvas::drawImageRect(const SkImage* image, const SkRect& dst, const SkPaint* paint, |
| 1905 | SrcRectConstraint constraint) { |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 1906 | RETURN_ON_NULL(image); |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1907 | this->drawImageRect(image, SkRect::MakeIWH(image->width(), image->height()), dst, paint, |
| 1908 | constraint); |
| 1909 | } |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1910 | |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 1911 | void SkCanvas::drawImageNine(const SkImage* image, const SkIRect& center, const SkRect& dst, |
| 1912 | const SkPaint* paint) { |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 1913 | RETURN_ON_NULL(image); |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 1914 | if (dst.isEmpty()) { |
| 1915 | return; |
| 1916 | } |
msarett | 552bca9 | 2016-08-03 06:53:26 -0700 | [diff] [blame] | 1917 | if (SkLatticeIter::Valid(image->width(), image->height(), center)) { |
| 1918 | this->onDrawImageNine(image, center, dst, paint); |
| 1919 | } else { |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1920 | this->drawImageRect(image, dst, paint); |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 1921 | } |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 1922 | } |
| 1923 | |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 1924 | void SkCanvas::drawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst, |
| 1925 | const SkPaint* paint) { |
| 1926 | RETURN_ON_NULL(image); |
| 1927 | if (dst.isEmpty()) { |
| 1928 | return; |
| 1929 | } |
msarett | 71df2d7 | 2016-09-30 12:41:42 -0700 | [diff] [blame] | 1930 | |
| 1931 | SkIRect bounds; |
| 1932 | Lattice latticePlusBounds = lattice; |
| 1933 | if (!latticePlusBounds.fBounds) { |
| 1934 | bounds = SkIRect::MakeWH(image->width(), image->height()); |
| 1935 | latticePlusBounds.fBounds = &bounds; |
| 1936 | } |
| 1937 | |
| 1938 | if (SkLatticeIter::Valid(image->width(), image->height(), latticePlusBounds)) { |
| 1939 | this->onDrawImageLattice(image, latticePlusBounds, dst, paint); |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 1940 | } else { |
| 1941 | this->drawImageRect(image, dst, paint); |
| 1942 | } |
| 1943 | } |
| 1944 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1945 | void SkCanvas::drawBitmap(const SkBitmap& bitmap, SkScalar dx, SkScalar dy, const SkPaint* paint) { |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 1946 | if (bitmap.drawsNothing()) { |
tomhudson | 2df6fd6 | 2015-04-09 09:20:19 -0700 | [diff] [blame] | 1947 | return; |
| 1948 | } |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1949 | this->onDrawBitmap(bitmap, dx, dy, paint); |
| 1950 | } |
| 1951 | |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1952 | void SkCanvas::drawBitmapRect(const SkBitmap& bitmap, const SkRect& src, const SkRect& dst, |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 1953 | const SkPaint* paint, SrcRectConstraint constraint) { |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1954 | if (bitmap.drawsNothing() || dst.isEmpty() || src.isEmpty()) { |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 1955 | return; |
| 1956 | } |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1957 | this->onDrawBitmapRect(bitmap, &src, dst, paint, constraint); |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1958 | } |
| 1959 | |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 1960 | void SkCanvas::drawBitmapRect(const SkBitmap& bitmap, const SkIRect& isrc, const SkRect& dst, |
| 1961 | const SkPaint* paint, SrcRectConstraint constraint) { |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1962 | this->drawBitmapRect(bitmap, SkRect::Make(isrc), dst, paint, constraint); |
reed | 84984ef | 2015-07-17 07:09:43 -0700 | [diff] [blame] | 1963 | } |
| 1964 | |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1965 | void SkCanvas::drawBitmapRect(const SkBitmap& bitmap, const SkRect& dst, const SkPaint* paint, |
| 1966 | SrcRectConstraint constraint) { |
| 1967 | this->drawBitmapRect(bitmap, SkRect::MakeIWH(bitmap.width(), bitmap.height()), dst, paint, |
| 1968 | constraint); |
| 1969 | } |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 1970 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1971 | void SkCanvas::drawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst, |
| 1972 | const SkPaint* paint) { |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 1973 | if (bitmap.drawsNothing() || dst.isEmpty()) { |
tomhudson | 2df6fd6 | 2015-04-09 09:20:19 -0700 | [diff] [blame] | 1974 | return; |
| 1975 | } |
msarett | 552bca9 | 2016-08-03 06:53:26 -0700 | [diff] [blame] | 1976 | if (SkLatticeIter::Valid(bitmap.width(), bitmap.height(), center)) { |
| 1977 | this->onDrawBitmapNine(bitmap, center, dst, paint); |
| 1978 | } else { |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 1979 | this->drawBitmapRect(bitmap, dst, paint); |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 1980 | } |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 1981 | } |
| 1982 | |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 1983 | void SkCanvas::drawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice, const SkRect& dst, |
| 1984 | const SkPaint* paint) { |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 1985 | if (bitmap.drawsNothing() || dst.isEmpty()) { |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 1986 | return; |
| 1987 | } |
msarett | 71df2d7 | 2016-09-30 12:41:42 -0700 | [diff] [blame] | 1988 | |
| 1989 | SkIRect bounds; |
| 1990 | Lattice latticePlusBounds = lattice; |
| 1991 | if (!latticePlusBounds.fBounds) { |
| 1992 | bounds = SkIRect::MakeWH(bitmap.width(), bitmap.height()); |
| 1993 | latticePlusBounds.fBounds = &bounds; |
| 1994 | } |
| 1995 | |
| 1996 | if (SkLatticeIter::Valid(bitmap.width(), bitmap.height(), latticePlusBounds)) { |
| 1997 | this->onDrawBitmapLattice(bitmap, latticePlusBounds, dst, paint); |
msarett | 552bca9 | 2016-08-03 06:53:26 -0700 | [diff] [blame] | 1998 | } else { |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 1999 | this->drawBitmapRect(bitmap, dst, paint); |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 2000 | } |
msarett | c573a40 | 2016-08-02 08:05:56 -0700 | [diff] [blame] | 2001 | } |
| 2002 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2003 | void SkCanvas::drawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[], |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 2004 | const SkColor colors[], int count, SkBlendMode mode, |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2005 | const SkRect* cull, const SkPaint* paint) { |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 2006 | RETURN_ON_NULL(atlas); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2007 | if (count <= 0) { |
| 2008 | return; |
| 2009 | } |
| 2010 | SkASSERT(atlas); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2011 | SkASSERT(tex); |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 2012 | this->onDrawAtlas(atlas, xform, tex, colors, count, mode, cull, paint); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2013 | } |
| 2014 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 2015 | void SkCanvas::drawAnnotation(const SkRect& rect, const char key[], SkData* value) { |
| 2016 | if (key) { |
| 2017 | this->onDrawAnnotation(rect, key, value); |
| 2018 | } |
| 2019 | } |
| 2020 | |
reed | e47829b | 2015-08-06 10:02:53 -0700 | [diff] [blame] | 2021 | void SkCanvas::legacy_drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst, |
| 2022 | const SkPaint* paint, SrcRectConstraint constraint) { |
| 2023 | if (src) { |
| 2024 | this->drawImageRect(image, *src, dst, paint, constraint); |
| 2025 | } else { |
| 2026 | this->drawImageRect(image, SkRect::MakeIWH(image->width(), image->height()), |
| 2027 | dst, paint, constraint); |
| 2028 | } |
| 2029 | } |
| 2030 | void SkCanvas::legacy_drawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst, |
| 2031 | const SkPaint* paint, SrcRectConstraint constraint) { |
| 2032 | if (src) { |
| 2033 | this->drawBitmapRect(bitmap, *src, dst, paint, constraint); |
| 2034 | } else { |
| 2035 | this->drawBitmapRect(bitmap, SkRect::MakeIWH(bitmap.width(), bitmap.height()), |
| 2036 | dst, paint, constraint); |
| 2037 | } |
| 2038 | } |
| 2039 | |
tomhudson | cb3bd18 | 2016-05-18 07:24:16 -0700 | [diff] [blame] | 2040 | void SkCanvas::temporary_internal_describeTopLayer(SkMatrix* matrix, SkIRect* clip_bounds) { |
| 2041 | SkIRect layer_bounds = this->getTopLayerBounds(); |
| 2042 | if (matrix) { |
| 2043 | *matrix = this->getTotalMatrix(); |
| 2044 | matrix->preTranslate(-layer_bounds.left(), -layer_bounds.top()); |
| 2045 | } |
| 2046 | if (clip_bounds) { |
| 2047 | this->getClipDeviceBounds(clip_bounds); |
| 2048 | clip_bounds->offset(-layer_bounds.left(), -layer_bounds.top()); |
| 2049 | } |
| 2050 | } |
| 2051 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2052 | ////////////////////////////////////////////////////////////////////////////// |
| 2053 | // These are the virtual drawing methods |
| 2054 | ////////////////////////////////////////////////////////////////////////////// |
| 2055 | |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 2056 | void SkCanvas::onDiscard() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 2057 | if (fSurfaceBase) { |
commit-bot@chromium.org | 28361fa | 2014-03-28 16:08:05 +0000 | [diff] [blame] | 2058 | fSurfaceBase->aboutToDraw(SkSurface::kDiscard_ContentChangeMode); |
| 2059 | } |
| 2060 | } |
| 2061 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2062 | void SkCanvas::onDrawPaint(const SkPaint& paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2063 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawPaint()"); |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 2064 | this->internalDrawPaint(paint); |
| 2065 | } |
| 2066 | |
| 2067 | void SkCanvas::internalDrawPaint(const SkPaint& paint) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2068 | LOOPER_BEGIN_CHECK_COMPLETE_OVERWRITE(paint, SkDrawFilter::kPaint_Type, nullptr, false) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2069 | |
| 2070 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2071 | iter.fDevice->drawPaint(iter, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2072 | } |
| 2073 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2074 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2075 | } |
| 2076 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2077 | void SkCanvas::onDrawPoints(PointMode mode, size_t count, const SkPoint pts[], |
| 2078 | const SkPaint& paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2079 | TRACE_EVENT1("disabled-by-default-skia", "SkCanvas::drawPoints()", "count", static_cast<uint64_t>(count)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2080 | if ((long)count <= 0) { |
| 2081 | return; |
| 2082 | } |
| 2083 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2084 | SkRect r, storage; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2085 | const SkRect* bounds = nullptr; |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 2086 | if (paint.canComputeFastBounds()) { |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 2087 | // special-case 2 points (common for drawing a single line) |
| 2088 | if (2 == count) { |
| 2089 | r.set(pts[0], pts[1]); |
| 2090 | } else { |
commit-bot@chromium.org | a8c7f77 | 2014-01-24 21:46:29 +0000 | [diff] [blame] | 2091 | r.set(pts, SkToInt(count)); |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 2092 | } |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2093 | if (this->quickReject(paint.computeFastStrokeBounds(r, &storage))) { |
| 2094 | return; |
| 2095 | } |
| 2096 | bounds = &r; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 2097 | } |
reed@google.com | a584aed | 2012-05-16 14:06:02 +0000 | [diff] [blame] | 2098 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2099 | SkASSERT(pts != nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2100 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2101 | LOOPER_BEGIN(paint, SkDrawFilter::kPoint_Type, bounds) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2102 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2103 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2104 | iter.fDevice->drawPoints(iter, mode, count, pts, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2105 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2106 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2107 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2108 | } |
| 2109 | |
reed | 4a16717 | 2016-08-18 17:15:25 -0700 | [diff] [blame] | 2110 | static bool needs_autodrawlooper(SkCanvas* canvas, const SkPaint& paint) { |
| 2111 | return ((intptr_t)paint.getImageFilter() | |
| 2112 | #ifdef SK_SUPPORT_LEGACY_DRAWFILTER |
| 2113 | (intptr_t)canvas->getDrawFilter() | |
| 2114 | #endif |
| 2115 | (intptr_t)paint.getLooper() ) != 0; |
| 2116 | } |
| 2117 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2118 | void SkCanvas::onDrawRect(const SkRect& r, const SkPaint& paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2119 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawRect()"); |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2120 | SkRect storage; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2121 | const SkRect* bounds = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2122 | if (paint.canComputeFastBounds()) { |
reed | 8432808 | 2015-02-10 14:18:09 -0800 | [diff] [blame] | 2123 | // Skia will draw an inverted rect, because it explicitly "sorts" it downstream. |
| 2124 | // To prevent accidental rejecting at this stage, we have to sort it before we check. |
| 2125 | SkRect tmp(r); |
| 2126 | tmp.sort(); |
| 2127 | |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2128 | if (this->quickReject(paint.computeFastBounds(tmp, &storage))) { |
| 2129 | return; |
| 2130 | } |
| 2131 | bounds = &r; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2132 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2133 | |
reed | 4a16717 | 2016-08-18 17:15:25 -0700 | [diff] [blame] | 2134 | if (needs_autodrawlooper(this, paint)) { |
| 2135 | LOOPER_BEGIN_CHECK_COMPLETE_OVERWRITE(paint, SkDrawFilter::kRect_Type, bounds, false) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2136 | |
reed | 4a16717 | 2016-08-18 17:15:25 -0700 | [diff] [blame] | 2137 | while (iter.next()) { |
| 2138 | iter.fDevice->drawRect(iter, r, looper.paint()); |
| 2139 | } |
| 2140 | |
| 2141 | LOOPER_END |
| 2142 | } else { |
| 2143 | this->predrawNotify(bounds, &paint, false); |
| 2144 | SkDrawIter iter(this); |
| 2145 | while (iter.next()) { |
| 2146 | iter.fDevice->drawRect(iter, r, paint); |
| 2147 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2148 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
msarett | 44df651 | 2016-08-25 13:54:30 -0700 | [diff] [blame] | 2151 | void SkCanvas::onDrawRegion(const SkRegion& region, const SkPaint& paint) { |
| 2152 | SkRect storage; |
| 2153 | SkRect regionRect = SkRect::Make(region.getBounds()); |
| 2154 | const SkRect* bounds = nullptr; |
| 2155 | if (paint.canComputeFastBounds()) { |
| 2156 | if (this->quickReject(paint.computeFastBounds(regionRect, &storage))) { |
| 2157 | return; |
| 2158 | } |
| 2159 | bounds = ®ionRect; |
| 2160 | } |
| 2161 | |
| 2162 | LOOPER_BEGIN(paint, SkDrawFilter::kRect_Type, bounds) |
| 2163 | |
| 2164 | while (iter.next()) { |
| 2165 | iter.fDevice->drawRegion(iter, region, looper.paint()); |
| 2166 | } |
| 2167 | |
| 2168 | LOOPER_END |
| 2169 | } |
| 2170 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2171 | void SkCanvas::onDrawOval(const SkRect& oval, const SkPaint& paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2172 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawOval()"); |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2173 | SkRect storage; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2174 | const SkRect* bounds = nullptr; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2175 | if (paint.canComputeFastBounds()) { |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2176 | if (this->quickReject(paint.computeFastBounds(oval, &storage))) { |
| 2177 | return; |
| 2178 | } |
| 2179 | bounds = &oval; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2180 | } |
skia.committer@gmail.com | 306ab9d | 2012-12-13 02:01:33 +0000 | [diff] [blame] | 2181 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2182 | LOOPER_BEGIN(paint, SkDrawFilter::kOval_Type, bounds) |
jvanverth@google.com | 46d3d39 | 2013-01-22 13:34:01 +0000 | [diff] [blame] | 2183 | |
| 2184 | while (iter.next()) { |
| 2185 | iter.fDevice->drawOval(iter, oval, looper.paint()); |
| 2186 | } |
| 2187 | |
| 2188 | LOOPER_END |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
bsalomon | ac3aa24 | 2016-08-19 11:25:19 -0700 | [diff] [blame] | 2191 | void SkCanvas::onDrawArc(const SkRect& oval, SkScalar startAngle, |
| 2192 | SkScalar sweepAngle, bool useCenter, |
| 2193 | const SkPaint& paint) { |
| 2194 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawArc()"); |
| 2195 | const SkRect* bounds = nullptr; |
| 2196 | if (paint.canComputeFastBounds()) { |
| 2197 | SkRect storage; |
| 2198 | // Note we're using the entire oval as the bounds. |
| 2199 | if (this->quickReject(paint.computeFastBounds(oval, &storage))) { |
| 2200 | return; |
| 2201 | } |
| 2202 | bounds = &oval; |
| 2203 | } |
| 2204 | |
| 2205 | LOOPER_BEGIN(paint, SkDrawFilter::kOval_Type, bounds) |
| 2206 | |
| 2207 | while (iter.next()) { |
| 2208 | iter.fDevice->drawArc(iter, oval, startAngle, sweepAngle, useCenter, looper.paint()); |
| 2209 | } |
| 2210 | |
| 2211 | LOOPER_END |
| 2212 | } |
| 2213 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2214 | void SkCanvas::onDrawRRect(const SkRRect& rrect, const SkPaint& paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2215 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawRRect()"); |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2216 | SkRect storage; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2217 | const SkRect* bounds = nullptr; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2218 | if (paint.canComputeFastBounds()) { |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2219 | if (this->quickReject(paint.computeFastBounds(rrect.getBounds(), &storage))) { |
| 2220 | return; |
| 2221 | } |
| 2222 | bounds = &rrect.getBounds(); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | if (rrect.isRect()) { |
| 2226 | // call the non-virtual version |
| 2227 | this->SkCanvas::drawRect(rrect.getBounds(), paint); |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2228 | return; |
| 2229 | } else if (rrect.isOval()) { |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2230 | // call the non-virtual version |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2231 | this->SkCanvas::drawOval(rrect.getBounds(), paint); |
| 2232 | return; |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2233 | } |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2234 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2235 | LOOPER_BEGIN(paint, SkDrawFilter::kRRect_Type, bounds) |
commit-bot@chromium.org | f2bfd54 | 2013-04-25 15:27:00 +0000 | [diff] [blame] | 2236 | |
| 2237 | while (iter.next()) { |
| 2238 | iter.fDevice->drawRRect(iter, rrect, looper.paint()); |
| 2239 | } |
| 2240 | |
| 2241 | LOOPER_END |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2242 | } |
| 2243 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 2244 | void SkCanvas::onDrawDRRect(const SkRRect& outer, const SkRRect& inner, |
| 2245 | const SkPaint& paint) { |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 2246 | SkRect storage; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2247 | const SkRect* bounds = nullptr; |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 2248 | if (paint.canComputeFastBounds()) { |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2249 | if (this->quickReject(paint.computeFastBounds(outer.getBounds(), &storage))) { |
| 2250 | return; |
| 2251 | } |
| 2252 | bounds = &outer.getBounds(); |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 2253 | } |
skia.committer@gmail.com | 25c7127 | 2014-02-21 03:02:02 +0000 | [diff] [blame] | 2254 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 2255 | LOOPER_BEGIN(paint, SkDrawFilter::kRRect_Type, bounds) |
skia.committer@gmail.com | 25c7127 | 2014-02-21 03:02:02 +0000 | [diff] [blame] | 2256 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 2257 | while (iter.next()) { |
| 2258 | iter.fDevice->drawDRRect(iter, outer, inner, looper.paint()); |
| 2259 | } |
skia.committer@gmail.com | 25c7127 | 2014-02-21 03:02:02 +0000 | [diff] [blame] | 2260 | |
commit-bot@chromium.org | ed9806f | 2014-02-21 02:32:36 +0000 | [diff] [blame] | 2261 | LOOPER_END |
| 2262 | } |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2263 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2264 | void SkCanvas::onDrawPath(const SkPath& path, const SkPaint& paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2265 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawPath()"); |
reed@google.com | 9364511 | 2012-07-26 16:11:47 +0000 | [diff] [blame] | 2266 | if (!path.isFinite()) { |
| 2267 | return; |
| 2268 | } |
| 2269 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2270 | SkRect storage; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2271 | const SkRect* bounds = nullptr; |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 2272 | if (!path.isInverseFillType() && paint.canComputeFastBounds()) { |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2273 | const SkRect& pathBounds = path.getBounds(); |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2274 | if (this->quickReject(paint.computeFastBounds(pathBounds, &storage))) { |
| 2275 | return; |
| 2276 | } |
| 2277 | bounds = &pathBounds; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2278 | } |
commit-bot@chromium.org | 0b45dc4 | 2014-02-21 05:42:57 +0000 | [diff] [blame] | 2279 | |
| 2280 | const SkRect& r = path.getBounds(); |
| 2281 | if (r.width() <= 0 && r.height() <= 0) { |
commit-bot@chromium.org | 6803c21 | 2014-05-04 18:08:27 +0000 | [diff] [blame] | 2282 | if (path.isInverseFillType()) { |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 2283 | this->internalDrawPaint(paint); |
caryclark | 6651a32 | 2015-09-09 13:20:49 -0700 | [diff] [blame] | 2284 | return; |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 2285 | } |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 2286 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2287 | |
senorblanco@chromium.org | 78cf119 | 2014-01-28 19:22:35 +0000 | [diff] [blame] | 2288 | LOOPER_BEGIN(paint, SkDrawFilter::kPath_Type, bounds) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2289 | |
| 2290 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2291 | iter.fDevice->drawPath(iter, path, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2292 | } |
| 2293 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2294 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2295 | } |
| 2296 | |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2297 | 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] | 2298 | if (!paint.getImageFilter()) { |
| 2299 | return false; |
| 2300 | } |
| 2301 | |
| 2302 | const SkMatrix& ctm = this->getTotalMatrix(); |
fmalita | c7e211a | 2016-01-07 10:34:46 -0800 | [diff] [blame] | 2303 | if (!SkTreatAsSprite(ctm, SkISize::Make(w, h), paint)) { |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2304 | return false; |
| 2305 | } |
| 2306 | |
| 2307 | // Currently we can only use the filterSprite code if we are clipped to the bitmap's bounds. |
| 2308 | // Once we can filter and the filter will return a result larger than itself, we should be |
| 2309 | // able to remove this constraint. |
| 2310 | // skbug.com/4526 |
| 2311 | // |
| 2312 | SkPoint pt; |
| 2313 | ctm.mapXY(x, y, &pt); |
| 2314 | SkIRect ir = SkIRect::MakeXYWH(SkScalarRoundToInt(pt.x()), SkScalarRoundToInt(pt.y()), w, h); |
| 2315 | return ir.contains(fMCRec->fRasterClip.getBounds()); |
| 2316 | } |
| 2317 | |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2318 | void SkCanvas::onDrawImage(const SkImage* image, SkScalar x, SkScalar y, const SkPaint* paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2319 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawImage()"); |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2320 | SkRect bounds = SkRect::MakeXYWH(x, y, |
| 2321 | SkIntToScalar(image->width()), SkIntToScalar(image->height())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2322 | if (nullptr == paint || paint->canComputeFastBounds()) { |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2323 | SkRect tmp = bounds; |
| 2324 | if (paint) { |
| 2325 | paint->computeFastBounds(tmp, &tmp); |
| 2326 | } |
| 2327 | if (this->quickReject(tmp)) { |
| 2328 | return; |
| 2329 | } |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2330 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2331 | |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2332 | SkLazyPaint lazy; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2333 | if (nullptr == paint) { |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2334 | paint = lazy.init(); |
| 2335 | } |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2336 | |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2337 | sk_sp<SkSpecialImage> special; |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 2338 | bool drawAsSprite = this->canDrawBitmapAsSprite(x, y, image->width(), image->height(), |
| 2339 | *paint); |
| 2340 | if (drawAsSprite && paint->getImageFilter()) { |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2341 | special = this->getDevice()->makeSpecial(image); |
| 2342 | if (!special) { |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 2343 | drawAsSprite = false; |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 2344 | } |
| 2345 | } |
| 2346 | |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2347 | LOOPER_BEGIN_DRAWBITMAP(*paint, drawAsSprite, &bounds) |
| 2348 | |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2349 | while (iter.next()) { |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2350 | const SkPaint& pnt = looper.paint(); |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2351 | if (special) { |
| 2352 | SkPoint pt; |
| 2353 | iter.fMatrix->mapXY(x, y, &pt); |
| 2354 | iter.fDevice->drawSpecial(iter, special.get(), |
| 2355 | SkScalarRoundToInt(pt.fX), |
| 2356 | SkScalarRoundToInt(pt.fY), pnt); |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2357 | } else { |
| 2358 | iter.fDevice->drawImage(iter, image, x, y, pnt); |
| 2359 | } |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2360 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2361 | |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2362 | LOOPER_END |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 2363 | } |
| 2364 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2365 | void SkCanvas::onDrawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst, |
reed | 562fe47 | 2015-07-28 07:35:14 -0700 | [diff] [blame] | 2366 | const SkPaint* paint, SrcRectConstraint constraint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2367 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawImageRect()"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2368 | if (nullptr == paint || paint->canComputeFastBounds()) { |
senorblanco | c41e7e1 | 2015-12-07 12:51:30 -0800 | [diff] [blame] | 2369 | SkRect storage = dst; |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2370 | if (paint) { |
| 2371 | paint->computeFastBounds(dst, &storage); |
| 2372 | } |
| 2373 | if (this->quickReject(storage)) { |
| 2374 | return; |
| 2375 | } |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2376 | } |
| 2377 | SkLazyPaint lazy; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2378 | if (nullptr == paint) { |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2379 | paint = lazy.init(); |
| 2380 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2381 | |
senorblanco | c41e7e1 | 2015-12-07 12:51:30 -0800 | [diff] [blame] | 2382 | LOOPER_BEGIN_CHECK_COMPLETE_OVERWRITE(*paint, SkDrawFilter::kBitmap_Type, &dst, |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 2383 | image->isOpaque()) |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2384 | |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2385 | while (iter.next()) { |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 2386 | iter.fDevice->drawImageRect(iter, image, src, dst, looper.paint(), constraint); |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2387 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2388 | |
reed | a85d4d0 | 2015-05-06 12:56:48 -0700 | [diff] [blame] | 2389 | LOOPER_END |
piotaixr | b5fae93 | 2014-09-24 13:03:30 -0700 | [diff] [blame] | 2390 | } |
| 2391 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2392 | void SkCanvas::onDrawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y, const SkPaint* paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2393 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawBitmap()"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2394 | SkDEBUGCODE(bitmap.validate();) |
| 2395 | |
reed | 3336697 | 2015-10-08 09:22:02 -0700 | [diff] [blame] | 2396 | if (bitmap.drawsNothing()) { |
| 2397 | return; |
| 2398 | } |
| 2399 | |
| 2400 | SkLazyPaint lazy; |
| 2401 | if (nullptr == paint) { |
| 2402 | paint = lazy.init(); |
| 2403 | } |
| 2404 | |
| 2405 | const SkMatrix matrix = SkMatrix::MakeTrans(x, y); |
| 2406 | |
| 2407 | SkRect storage; |
| 2408 | const SkRect* bounds = nullptr; |
| 2409 | if (paint->canComputeFastBounds()) { |
| 2410 | bitmap.getBounds(&storage); |
| 2411 | matrix.mapRect(&storage); |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2412 | SkRect tmp = storage; |
| 2413 | if (this->quickReject(paint->computeFastBounds(tmp, &tmp))) { |
| 2414 | return; |
| 2415 | } |
| 2416 | bounds = &storage; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2417 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2418 | |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2419 | sk_sp<SkSpecialImage> special; |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 2420 | bool drawAsSprite = bounds && this->canDrawBitmapAsSprite(x, y, bitmap.width(), bitmap.height(), |
| 2421 | *paint); |
| 2422 | if (drawAsSprite && paint->getImageFilter()) { |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2423 | special = this->getDevice()->makeSpecial(bitmap); |
| 2424 | if (!special) { |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 2425 | drawAsSprite = false; |
| 2426 | } |
| 2427 | } |
| 2428 | |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2429 | LOOPER_BEGIN_DRAWBITMAP(*paint, drawAsSprite, bounds) |
reed | 3336697 | 2015-10-08 09:22:02 -0700 | [diff] [blame] | 2430 | |
| 2431 | while (iter.next()) { |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2432 | const SkPaint& pnt = looper.paint(); |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2433 | if (special) { |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2434 | SkPoint pt; |
| 2435 | iter.fMatrix->mapXY(x, y, &pt); |
reed | a2217ef | 2016-07-20 06:04:34 -0700 | [diff] [blame] | 2436 | iter.fDevice->drawSpecial(iter, special.get(), |
| 2437 | SkScalarRoundToInt(pt.fX), |
| 2438 | SkScalarRoundToInt(pt.fY), pnt); |
reed | 262a71b | 2015-12-05 13:07:27 -0800 | [diff] [blame] | 2439 | } else { |
| 2440 | iter.fDevice->drawBitmap(iter, bitmap, matrix, looper.paint()); |
| 2441 | } |
reed | 3336697 | 2015-10-08 09:22:02 -0700 | [diff] [blame] | 2442 | } |
msarett | fbfa258 | 2016-08-12 08:29:08 -0700 | [diff] [blame] | 2443 | |
reed | 3336697 | 2015-10-08 09:22:02 -0700 | [diff] [blame] | 2444 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2445 | } |
| 2446 | |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2447 | // this one is non-virtual, so it can be called safely by other canvas apis |
reed@google.com | 7112173 | 2012-09-18 15:14:33 +0000 | [diff] [blame] | 2448 | void SkCanvas::internalDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, |
commit-bot@chromium.org | eed779d | 2013-08-16 10:24:37 +0000 | [diff] [blame] | 2449 | const SkRect& dst, const SkPaint* paint, |
reed | a5517e2 | 2015-07-14 10:54:12 -0700 | [diff] [blame] | 2450 | SrcRectConstraint constraint) { |
commit-bot@chromium.org | 50b393a | 2014-02-10 18:29:10 +0000 | [diff] [blame] | 2451 | if (bitmap.drawsNothing() || dst.isEmpty()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2452 | return; |
| 2453 | } |
vandebo@chromium.org | 74b4619 | 2012-01-28 01:45:11 +0000 | [diff] [blame] | 2454 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2455 | if (nullptr == paint || paint->canComputeFastBounds()) { |
senorblanco | c41e7e1 | 2015-12-07 12:51:30 -0800 | [diff] [blame] | 2456 | SkRect storage; |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2457 | if (this->quickReject(paint ? paint->computeFastBounds(dst, &storage) : dst)) { |
| 2458 | return; |
| 2459 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2460 | } |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 2461 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 2462 | SkLazyPaint lazy; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2463 | if (nullptr == paint) { |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 2464 | paint = lazy.init(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2465 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 2466 | |
senorblanco | c41e7e1 | 2015-12-07 12:51:30 -0800 | [diff] [blame] | 2467 | LOOPER_BEGIN_CHECK_COMPLETE_OVERWRITE(*paint, SkDrawFilter::kBitmap_Type, &dst, |
reed | c83a297 | 2015-07-16 07:40:45 -0700 | [diff] [blame] | 2468 | bitmap.isOpaque()) |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 2469 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 2470 | while (iter.next()) { |
reed | 562fe47 | 2015-07-28 07:35:14 -0700 | [diff] [blame] | 2471 | iter.fDevice->drawBitmapRect(iter, bitmap, src, dst, looper.paint(), constraint); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 2472 | } |
skia.committer@gmail.com | 7064e9a | 2012-09-26 02:01:18 +0000 | [diff] [blame] | 2473 | |
reed@google.com | 33535f3 | 2012-09-25 15:37:50 +0000 | [diff] [blame] | 2474 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2475 | } |
| 2476 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2477 | void SkCanvas::onDrawBitmapRect(const SkBitmap& bitmap, const SkRect* src, const SkRect& dst, |
reed | 562fe47 | 2015-07-28 07:35:14 -0700 | [diff] [blame] | 2478 | const SkPaint* paint, SrcRectConstraint constraint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2479 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawBitmapRectToRect()"); |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2480 | SkDEBUGCODE(bitmap.validate();) |
reed | 562fe47 | 2015-07-28 07:35:14 -0700 | [diff] [blame] | 2481 | this->internalDrawBitmapRect(bitmap, src, dst, paint, constraint); |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2484 | void SkCanvas::onDrawImageNine(const SkImage* image, const SkIRect& center, const SkRect& dst, |
| 2485 | const SkPaint* paint) { |
| 2486 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawImageNine()"); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2487 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2488 | if (nullptr == paint || paint->canComputeFastBounds()) { |
senorblanco | c41e7e1 | 2015-12-07 12:51:30 -0800 | [diff] [blame] | 2489 | SkRect storage; |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2490 | if (this->quickReject(paint ? paint->computeFastBounds(dst, &storage) : dst)) { |
| 2491 | return; |
| 2492 | } |
reed@google.com | 3d60812 | 2011-11-21 15:16:16 +0000 | [diff] [blame] | 2493 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2494 | |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2495 | SkLazyPaint lazy; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2496 | if (nullptr == paint) { |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2497 | paint = lazy.init(); |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2498 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2499 | |
senorblanco | c41e7e1 | 2015-12-07 12:51:30 -0800 | [diff] [blame] | 2500 | LOOPER_BEGIN(*paint, SkDrawFilter::kBitmap_Type, &dst) |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2501 | |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2502 | while (iter.next()) { |
| 2503 | iter.fDevice->drawImageNine(iter, image, center, dst, looper.paint()); |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2504 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2505 | |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2506 | LOOPER_END |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2509 | void SkCanvas::onDrawBitmapNine(const SkBitmap& bitmap, const SkIRect& center, const SkRect& dst, |
| 2510 | const SkPaint* paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2511 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawBitmapNine()"); |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2512 | SkDEBUGCODE(bitmap.validate();) |
| 2513 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2514 | if (nullptr == paint || paint->canComputeFastBounds()) { |
senorblanco | c41e7e1 | 2015-12-07 12:51:30 -0800 | [diff] [blame] | 2515 | SkRect storage; |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2516 | if (this->quickReject(paint ? paint->computeFastBounds(dst, &storage) : dst)) { |
| 2517 | return; |
| 2518 | } |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2519 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2520 | |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2521 | SkLazyPaint lazy; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2522 | if (nullptr == paint) { |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2523 | paint = lazy.init(); |
| 2524 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2525 | |
senorblanco | c41e7e1 | 2015-12-07 12:51:30 -0800 | [diff] [blame] | 2526 | LOOPER_BEGIN(*paint, SkDrawFilter::kBitmap_Type, &dst) |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2527 | |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2528 | while (iter.next()) { |
| 2529 | iter.fDevice->drawBitmapNine(iter, bitmap, center, dst, looper.paint()); |
| 2530 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2531 | |
reed | 4c21dc5 | 2015-06-25 12:32:03 -0700 | [diff] [blame] | 2532 | LOOPER_END |
reed@google.com | 9987ec3 | 2011-09-07 11:57:52 +0000 | [diff] [blame] | 2533 | } |
| 2534 | |
msarett | 1688206 | 2016-08-16 09:31:08 -0700 | [diff] [blame] | 2535 | void SkCanvas::onDrawImageLattice(const SkImage* image, const Lattice& lattice, const SkRect& dst, |
| 2536 | const SkPaint* paint) { |
| 2537 | if (nullptr == paint || paint->canComputeFastBounds()) { |
| 2538 | SkRect storage; |
| 2539 | if (this->quickReject(paint ? paint->computeFastBounds(dst, &storage) : dst)) { |
| 2540 | return; |
| 2541 | } |
| 2542 | } |
| 2543 | |
| 2544 | SkLazyPaint lazy; |
| 2545 | if (nullptr == paint) { |
| 2546 | paint = lazy.init(); |
| 2547 | } |
| 2548 | |
| 2549 | LOOPER_BEGIN(*paint, SkDrawFilter::kBitmap_Type, &dst) |
| 2550 | |
| 2551 | while (iter.next()) { |
| 2552 | iter.fDevice->drawImageLattice(iter, image, lattice, dst, looper.paint()); |
| 2553 | } |
| 2554 | |
| 2555 | LOOPER_END |
| 2556 | } |
| 2557 | |
| 2558 | void SkCanvas::onDrawBitmapLattice(const SkBitmap& bitmap, const Lattice& lattice, |
| 2559 | const SkRect& dst, const SkPaint* paint) { |
| 2560 | if (nullptr == paint || paint->canComputeFastBounds()) { |
| 2561 | SkRect storage; |
| 2562 | if (this->quickReject(paint ? paint->computeFastBounds(dst, &storage) : dst)) { |
| 2563 | return; |
| 2564 | } |
| 2565 | } |
| 2566 | |
| 2567 | SkLazyPaint lazy; |
| 2568 | if (nullptr == paint) { |
| 2569 | paint = lazy.init(); |
| 2570 | } |
| 2571 | |
| 2572 | LOOPER_BEGIN(*paint, SkDrawFilter::kBitmap_Type, &dst) |
| 2573 | |
| 2574 | while (iter.next()) { |
| 2575 | iter.fDevice->drawBitmapLattice(iter, bitmap, lattice, dst, looper.paint()); |
| 2576 | } |
| 2577 | |
| 2578 | LOOPER_END |
| 2579 | } |
| 2580 | |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2581 | class SkDeviceFilteredPaint { |
| 2582 | public: |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 2583 | SkDeviceFilteredPaint(SkBaseDevice* device, const SkPaint& paint) { |
fmalita | 112e7e2 | 2014-11-13 14:05:58 -0800 | [diff] [blame] | 2584 | uint32_t filteredFlags = device->filterTextFlags(paint); |
| 2585 | if (filteredFlags != paint.getFlags()) { |
reed@google.com | a076e9b | 2011-04-06 20:17:29 +0000 | [diff] [blame] | 2586 | SkPaint* newPaint = fLazy.set(paint); |
fmalita | 112e7e2 | 2014-11-13 14:05:58 -0800 | [diff] [blame] | 2587 | newPaint->setFlags(filteredFlags); |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2588 | fPaint = newPaint; |
| 2589 | } else { |
| 2590 | fPaint = &paint; |
| 2591 | } |
| 2592 | } |
| 2593 | |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2594 | const SkPaint& paint() const { return *fPaint; } |
| 2595 | |
| 2596 | private: |
mike@reedtribe.org | 2c8fc5a | 2011-04-10 00:35:29 +0000 | [diff] [blame] | 2597 | const SkPaint* fPaint; |
| 2598 | SkLazyPaint fLazy; |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2599 | }; |
| 2600 | |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 2601 | void SkCanvas::DrawRect(const SkDraw& draw, const SkPaint& paint, |
| 2602 | const SkRect& r, SkScalar textSize) { |
epoger@google.com | 17b7894 | 2011-08-26 14:40:38 +0000 | [diff] [blame] | 2603 | if (paint.getStyle() == SkPaint::kFill_Style) { |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 2604 | draw.fDevice->drawRect(draw, r, paint); |
| 2605 | } else { |
| 2606 | SkPaint p(paint); |
epoger@google.com | 17b7894 | 2011-08-26 14:40:38 +0000 | [diff] [blame] | 2607 | p.setStrokeWidth(SkScalarMul(textSize, paint.getStrokeWidth())); |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 2608 | draw.fDevice->drawRect(draw, r, p); |
| 2609 | } |
| 2610 | } |
| 2611 | |
| 2612 | void SkCanvas::DrawTextDecorations(const SkDraw& draw, const SkPaint& paint, |
| 2613 | const char text[], size_t byteLength, |
| 2614 | SkScalar x, SkScalar y) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2615 | SkASSERT(byteLength == 0 || text != nullptr); |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 2616 | |
| 2617 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2618 | if (text == nullptr || byteLength == 0 || |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 2619 | draw.fRC->isEmpty() || |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 2620 | (paint.getAlpha() == 0 && paint.isSrcOver())) { |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 2621 | return; |
| 2622 | } |
| 2623 | |
| 2624 | SkScalar width = 0; |
| 2625 | SkPoint start; |
| 2626 | |
| 2627 | start.set(0, 0); // to avoid warning |
| 2628 | if (paint.getFlags() & (SkPaint::kUnderlineText_Flag | |
| 2629 | SkPaint::kStrikeThruText_Flag)) { |
| 2630 | width = paint.measureText(text, byteLength); |
| 2631 | |
| 2632 | SkScalar offsetX = 0; |
| 2633 | if (paint.getTextAlign() == SkPaint::kCenter_Align) { |
| 2634 | offsetX = SkScalarHalf(width); |
| 2635 | } else if (paint.getTextAlign() == SkPaint::kRight_Align) { |
| 2636 | offsetX = width; |
| 2637 | } |
| 2638 | start.set(x - offsetX, y); |
| 2639 | } |
| 2640 | |
| 2641 | if (0 == width) { |
| 2642 | return; |
| 2643 | } |
| 2644 | |
| 2645 | uint32_t flags = paint.getFlags(); |
| 2646 | |
| 2647 | if (flags & (SkPaint::kUnderlineText_Flag | |
| 2648 | SkPaint::kStrikeThruText_Flag)) { |
| 2649 | SkScalar textSize = paint.getTextSize(); |
| 2650 | SkScalar height = SkScalarMul(textSize, kStdUnderline_Thickness); |
| 2651 | SkRect r; |
| 2652 | |
| 2653 | r.fLeft = start.fX; |
| 2654 | r.fRight = start.fX + width; |
| 2655 | |
| 2656 | if (flags & SkPaint::kUnderlineText_Flag) { |
| 2657 | SkScalar offset = SkScalarMulAdd(textSize, kStdUnderline_Offset, |
| 2658 | start.fY); |
| 2659 | r.fTop = offset; |
| 2660 | r.fBottom = offset + height; |
caryclark | fb56218 | 2015-12-21 08:35:51 -0800 | [diff] [blame] | 2661 | DrawRect(draw, paint, r, 1); |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 2662 | } |
| 2663 | if (flags & SkPaint::kStrikeThruText_Flag) { |
| 2664 | SkScalar offset = SkScalarMulAdd(textSize, kStdStrikeThru_Offset, |
| 2665 | start.fY); |
| 2666 | r.fTop = offset; |
| 2667 | r.fBottom = offset + height; |
caryclark | fb56218 | 2015-12-21 08:35:51 -0800 | [diff] [blame] | 2668 | DrawRect(draw, paint, r, 1); |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 2669 | } |
| 2670 | } |
| 2671 | } |
| 2672 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2673 | void SkCanvas::onDrawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, |
| 2674 | const SkPaint& paint) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2675 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, nullptr) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2676 | |
| 2677 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2678 | SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint()); |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2679 | iter.fDevice->drawText(iter, text, byteLength, x, y, dfp.paint()); |
bungeman@google.com | 52c748b | 2011-08-22 21:30:43 +0000 | [diff] [blame] | 2680 | DrawTextDecorations(iter, dfp.paint(), |
| 2681 | static_cast<const char*>(text), byteLength, x, y); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2682 | } |
| 2683 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2684 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2687 | void SkCanvas::onDrawPosText(const void* text, size_t byteLength, const SkPoint pos[], |
| 2688 | const SkPaint& paint) { |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 2689 | SkPoint textOffset = SkPoint::Make(0, 0); |
| 2690 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2691 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, nullptr) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 2692 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2693 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2694 | SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint()); |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 2695 | iter.fDevice->drawPosText(iter, text, byteLength, &pos->fX, 2, textOffset, |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2696 | dfp.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2697 | } |
skia.committer@gmail.com | b0430d0 | 2014-04-24 03:05:07 +0000 | [diff] [blame] | 2698 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2699 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2700 | } |
| 2701 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2702 | void SkCanvas::onDrawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], |
| 2703 | SkScalar constY, const SkPaint& paint) { |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 2704 | |
| 2705 | SkPoint textOffset = SkPoint::Make(0, constY); |
| 2706 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2707 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, nullptr) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 2708 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2709 | while (iter.next()) { |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2710 | SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint()); |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 2711 | iter.fDevice->drawPosText(iter, text, byteLength, xpos, 1, textOffset, |
reed@google.com | f67e4cf | 2011-03-15 20:56:58 +0000 | [diff] [blame] | 2712 | dfp.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2713 | } |
skia.committer@gmail.com | b0430d0 | 2014-04-24 03:05:07 +0000 | [diff] [blame] | 2714 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2715 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2718 | void SkCanvas::onDrawTextOnPath(const void* text, size_t byteLength, const SkPath& path, |
| 2719 | const SkMatrix* matrix, const SkPaint& paint) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2720 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, nullptr) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 2721 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2722 | while (iter.next()) { |
| 2723 | iter.fDevice->drawTextOnPath(iter, text, byteLength, path, |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2724 | matrix, looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2725 | } |
skia.committer@gmail.com | b0430d0 | 2014-04-24 03:05:07 +0000 | [diff] [blame] | 2726 | |
commit-bot@chromium.org | 945ec3a | 2014-04-22 20:07:30 +0000 | [diff] [blame] | 2727 | LOOPER_END |
commit-bot@chromium.org | 4325d11 | 2014-04-22 19:03:02 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 2730 | void SkCanvas::onDrawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[], |
| 2731 | const SkRect* cullRect, const SkPaint& paint) { |
| 2732 | if (cullRect && this->quickReject(*cullRect)) { |
| 2733 | return; |
| 2734 | } |
| 2735 | |
| 2736 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, nullptr) |
| 2737 | |
| 2738 | while (iter.next()) { |
| 2739 | iter.fDevice->drawTextRSXform(iter, text, byteLength, xform, looper.paint()); |
| 2740 | } |
| 2741 | |
| 2742 | LOOPER_END |
| 2743 | } |
| 2744 | |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2745 | void SkCanvas::onDrawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 2746 | const SkPaint& paint) { |
fmalita | 7ba7aa7 | 2014-08-29 09:46:36 -0700 | [diff] [blame] | 2747 | |
fmalita | 85d5eb9 | 2015-03-04 11:20:12 -0800 | [diff] [blame] | 2748 | SkRect storage; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2749 | const SkRect* bounds = nullptr; |
fmalita | 19653d1 | 2014-10-16 11:53:30 -0700 | [diff] [blame] | 2750 | if (paint.canComputeFastBounds()) { |
fmalita | 85d5eb9 | 2015-03-04 11:20:12 -0800 | [diff] [blame] | 2751 | storage = blob->bounds().makeOffset(x, y); |
senorblanco | 87e066e | 2015-10-28 11:23:36 -0700 | [diff] [blame] | 2752 | SkRect tmp; |
| 2753 | if (this->quickReject(paint.computeFastBounds(storage, &tmp))) { |
| 2754 | return; |
| 2755 | } |
| 2756 | bounds = &storage; |
fmalita | 7ba7aa7 | 2014-08-29 09:46:36 -0700 | [diff] [blame] | 2757 | } |
| 2758 | |
fmalita | 024f996 | 2015-03-03 19:08:17 -0800 | [diff] [blame] | 2759 | // We cannot filter in the looper as we normally do, because the paint is |
| 2760 | // incomplete at this point (text-related attributes are embedded within blob run paints). |
| 2761 | SkDrawFilter* drawFilter = fMCRec->fFilter; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2762 | fMCRec->fFilter = nullptr; |
fmalita | 024f996 | 2015-03-03 19:08:17 -0800 | [diff] [blame] | 2763 | |
fmalita | 85d5eb9 | 2015-03-04 11:20:12 -0800 | [diff] [blame] | 2764 | LOOPER_BEGIN(paint, SkDrawFilter::kText_Type, bounds) |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2765 | |
fmalita | aa1b912 | 2014-08-28 14:32:24 -0700 | [diff] [blame] | 2766 | while (iter.next()) { |
| 2767 | SkDeviceFilteredPaint dfp(iter.fDevice, looper.paint()); |
fmalita | 024f996 | 2015-03-03 19:08:17 -0800 | [diff] [blame] | 2768 | iter.fDevice->drawTextBlob(iter, blob, x, y, dfp.paint(), drawFilter); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2769 | } |
| 2770 | |
fmalita | aa1b912 | 2014-08-28 14:32:24 -0700 | [diff] [blame] | 2771 | LOOPER_END |
fmalita | 024f996 | 2015-03-03 19:08:17 -0800 | [diff] [blame] | 2772 | |
| 2773 | fMCRec->fFilter = drawFilter; |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2774 | } |
| 2775 | |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2776 | // These will become non-virtual, so they always call the (virtual) onDraw... method |
| 2777 | void SkCanvas::drawText(const void* text, size_t byteLength, SkScalar x, SkScalar y, |
| 2778 | const SkPaint& paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2779 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawText()"); |
reed | ac09554 | 2016-08-04 15:54:41 -0700 | [diff] [blame] | 2780 | if (byteLength) { |
| 2781 | this->onDrawText(text, byteLength, x, y, paint); |
| 2782 | } |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2783 | } |
| 2784 | void SkCanvas::drawPosText(const void* text, size_t byteLength, const SkPoint pos[], |
| 2785 | const SkPaint& paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2786 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawPosText()"); |
reed | ac09554 | 2016-08-04 15:54:41 -0700 | [diff] [blame] | 2787 | if (byteLength) { |
| 2788 | this->onDrawPosText(text, byteLength, pos, paint); |
| 2789 | } |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2790 | } |
| 2791 | void SkCanvas::drawPosTextH(const void* text, size_t byteLength, const SkScalar xpos[], |
| 2792 | SkScalar constY, const SkPaint& paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2793 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawPosTextH()"); |
reed | ac09554 | 2016-08-04 15:54:41 -0700 | [diff] [blame] | 2794 | if (byteLength) { |
| 2795 | this->onDrawPosTextH(text, byteLength, xpos, constY, paint); |
| 2796 | } |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2797 | } |
| 2798 | void SkCanvas::drawTextOnPath(const void* text, size_t byteLength, const SkPath& path, |
| 2799 | const SkMatrix* matrix, const SkPaint& paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2800 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawTextOnPath()"); |
reed | ac09554 | 2016-08-04 15:54:41 -0700 | [diff] [blame] | 2801 | if (byteLength) { |
| 2802 | this->onDrawTextOnPath(text, byteLength, path, matrix, paint); |
| 2803 | } |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2804 | } |
reed | 45561a0 | 2016-07-07 12:47:17 -0700 | [diff] [blame] | 2805 | void SkCanvas::drawTextRSXform(const void* text, size_t byteLength, const SkRSXform xform[], |
| 2806 | const SkRect* cullRect, const SkPaint& paint) { |
| 2807 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawTextRSXform()"); |
| 2808 | if (byteLength) { |
| 2809 | this->onDrawTextRSXform(text, byteLength, xform, cullRect, paint); |
| 2810 | } |
| 2811 | } |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2812 | void SkCanvas::drawTextBlob(const SkTextBlob* blob, SkScalar x, SkScalar y, |
| 2813 | const SkPaint& paint) { |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 2814 | RETURN_ON_NULL(blob); |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2815 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawTextBlob()"); |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 2816 | this->onDrawTextBlob(blob, x, y, paint); |
fmalita | 00d5c2c | 2014-08-21 08:53:26 -0700 | [diff] [blame] | 2817 | } |
reed@google.com | e0d9ce8 | 2014-04-23 04:00:17 +0000 | [diff] [blame] | 2818 | |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2819 | void SkCanvas::onDrawVertices(VertexMode vmode, int vertexCount, |
| 2820 | const SkPoint verts[], const SkPoint texs[], |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 2821 | const SkColor colors[], SkBlendMode bmode, |
reed | 41af966 | 2015-01-05 07:49:08 -0800 | [diff] [blame] | 2822 | const uint16_t indices[], int indexCount, |
| 2823 | const SkPaint& paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2824 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawVertices()"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2825 | LOOPER_BEGIN(paint, SkDrawFilter::kPath_Type, nullptr) |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2826 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2827 | while (iter.next()) { |
| 2828 | iter.fDevice->drawVertices(iter, vmode, vertexCount, verts, texs, |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 2829 | colors, bmode, indices, indexCount, |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2830 | looper.paint()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2831 | } |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2832 | |
reed@google.com | 4e2b3d3 | 2011-04-07 14:18:59 +0000 | [diff] [blame] | 2833 | LOOPER_END |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2834 | } |
| 2835 | |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 2836 | void SkCanvas::drawPatch(const SkPoint cubics[12], const SkColor colors[4], |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 2837 | const SkPoint texCoords[4], SkBlendMode bmode, |
| 2838 | const SkPaint& paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2839 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawPatch()"); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2840 | if (nullptr == cubics) { |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 2841 | return; |
| 2842 | } |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 2843 | |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 2844 | this->onDrawPatch(cubics, colors, texCoords, bmode, paint); |
msarett | 9340c26 | 2016-09-22 05:20:21 -0700 | [diff] [blame] | 2845 | } |
| 2846 | |
| 2847 | void SkCanvas::onDrawPatch(const SkPoint cubics[12], const SkColor colors[4], |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 2848 | const SkPoint texCoords[4], SkBlendMode bmode, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 2849 | const SkPaint& paint) { |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2850 | // Since a patch is always within the convex hull of the control points, we discard it when its |
| 2851 | // bounding rectangle is completely outside the current clip. |
| 2852 | SkRect bounds; |
dandov | b3c9d1c | 2014-08-12 08:34:29 -0700 | [diff] [blame] | 2853 | bounds.set(cubics, SkPatchUtils::kNumCtrlPts); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2854 | if (this->quickReject(bounds)) { |
| 2855 | return; |
| 2856 | } |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 2857 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2858 | LOOPER_BEGIN(paint, SkDrawFilter::kPath_Type, nullptr) |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 2859 | |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2860 | while (iter.next()) { |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 2861 | iter.fDevice->drawPatch(iter, cubics, colors, texCoords, bmode, paint); |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2862 | } |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 2863 | |
dandov | ecfff21 | 2014-08-04 10:02:00 -0700 | [diff] [blame] | 2864 | LOOPER_END |
| 2865 | } |
| 2866 | |
reed | a8db728 | 2015-07-07 10:22:31 -0700 | [diff] [blame] | 2867 | void SkCanvas::drawDrawable(SkDrawable* dr, SkScalar x, SkScalar y) { |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 2868 | RETURN_ON_NULL(dr); |
| 2869 | if (x || y) { |
| 2870 | SkMatrix matrix = SkMatrix::MakeTrans(x, y); |
| 2871 | this->onDrawDrawable(dr, &matrix); |
| 2872 | } else { |
| 2873 | this->onDrawDrawable(dr, nullptr); |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 2874 | } |
| 2875 | } |
| 2876 | |
reed | a8db728 | 2015-07-07 10:22:31 -0700 | [diff] [blame] | 2877 | void SkCanvas::drawDrawable(SkDrawable* dr, const SkMatrix* matrix) { |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 2878 | RETURN_ON_NULL(dr); |
| 2879 | if (matrix && matrix->isIdentity()) { |
| 2880 | matrix = nullptr; |
reed | a8db728 | 2015-07-07 10:22:31 -0700 | [diff] [blame] | 2881 | } |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 2882 | this->onDrawDrawable(dr, matrix); |
reed | a8db728 | 2015-07-07 10:22:31 -0700 | [diff] [blame] | 2883 | } |
| 2884 | |
| 2885 | void SkCanvas::onDrawDrawable(SkDrawable* dr, const SkMatrix* matrix) { |
reed | 2a62e85 | 2016-10-03 10:35:37 -0700 | [diff] [blame] | 2886 | // drawable bounds are no longer reliable (e.g. android displaylist) |
| 2887 | // so don't use them for quick-reject |
reed | a8db728 | 2015-07-07 10:22:31 -0700 | [diff] [blame] | 2888 | dr->draw(this, matrix); |
reed | 6a070dc | 2014-11-11 19:36:09 -0800 | [diff] [blame] | 2889 | } |
| 2890 | |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2891 | void SkCanvas::onDrawAtlas(const SkImage* atlas, const SkRSXform xform[], const SkRect tex[], |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 2892 | const SkColor colors[], int count, SkBlendMode bmode, |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2893 | const SkRect* cull, const SkPaint* paint) { |
| 2894 | if (cull && this->quickReject(*cull)) { |
| 2895 | return; |
| 2896 | } |
| 2897 | |
| 2898 | SkPaint pnt; |
| 2899 | if (paint) { |
| 2900 | pnt = *paint; |
| 2901 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 2902 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 2903 | LOOPER_BEGIN(pnt, SkDrawFilter::kPath_Type, nullptr) |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2904 | while (iter.next()) { |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 2905 | iter.fDevice->drawAtlas(iter, atlas, xform, tex, colors, count, bmode, pnt); |
reed | 71c3c76 | 2015-06-24 10:29:17 -0700 | [diff] [blame] | 2906 | } |
| 2907 | LOOPER_END |
| 2908 | } |
| 2909 | |
reed | f70b531 | 2016-03-04 16:36:20 -0800 | [diff] [blame] | 2910 | void SkCanvas::onDrawAnnotation(const SkRect& rect, const char key[], SkData* value) { |
| 2911 | SkASSERT(key); |
| 2912 | |
| 2913 | SkPaint paint; |
| 2914 | LOOPER_BEGIN(paint, SkDrawFilter::kRect_Type, nullptr) |
| 2915 | while (iter.next()) { |
| 2916 | iter.fDevice->drawAnnotation(iter, rect, key, value); |
| 2917 | } |
| 2918 | LOOPER_END |
| 2919 | } |
| 2920 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2921 | ////////////////////////////////////////////////////////////////////////////// |
| 2922 | // These methods are NOT virtual, and therefore must call back into virtual |
| 2923 | // methods, rather than actually drawing themselves. |
| 2924 | ////////////////////////////////////////////////////////////////////////////// |
| 2925 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 2926 | void SkCanvas::drawARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b, SkBlendMode mode) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2927 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawARGB()"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2928 | SkPaint paint; |
| 2929 | |
| 2930 | paint.setARGB(a, r, g, b); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 2931 | paint.setBlendMode(mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2932 | this->drawPaint(paint); |
| 2933 | } |
| 2934 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 2935 | void SkCanvas::drawColor(SkColor c, SkBlendMode mode) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2936 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawColor()"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2937 | SkPaint paint; |
| 2938 | |
| 2939 | paint.setColor(c); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 2940 | paint.setBlendMode(mode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2941 | this->drawPaint(paint); |
| 2942 | } |
| 2943 | |
| 2944 | void SkCanvas::drawPoint(SkScalar x, SkScalar y, const SkPaint& paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2945 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawPoint(SkPaint)"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2946 | SkPoint pt; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2947 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2948 | pt.set(x, y); |
| 2949 | this->drawPoints(kPoints_PointMode, 1, &pt, paint); |
| 2950 | } |
| 2951 | |
| 2952 | void SkCanvas::drawPoint(SkScalar x, SkScalar y, SkColor color) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2953 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawPoint(SkColor)"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2954 | SkPoint pt; |
| 2955 | SkPaint paint; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2956 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2957 | pt.set(x, y); |
| 2958 | paint.setColor(color); |
| 2959 | this->drawPoints(kPoints_PointMode, 1, &pt, paint); |
| 2960 | } |
| 2961 | |
| 2962 | void SkCanvas::drawLine(SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1, |
| 2963 | const SkPaint& paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2964 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawLine()"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2965 | SkPoint pts[2]; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 2966 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2967 | pts[0].set(x0, y0); |
| 2968 | pts[1].set(x1, y1); |
| 2969 | this->drawPoints(kLines_PointMode, 2, pts, paint); |
| 2970 | } |
| 2971 | |
| 2972 | void SkCanvas::drawRectCoords(SkScalar left, SkScalar top, |
| 2973 | SkScalar right, SkScalar bottom, |
| 2974 | const SkPaint& paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2975 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawRectCoords()"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2976 | SkRect r; |
| 2977 | |
| 2978 | r.set(left, top, right, bottom); |
| 2979 | this->drawRect(r, paint); |
| 2980 | } |
| 2981 | |
| 2982 | void SkCanvas::drawCircle(SkScalar cx, SkScalar cy, SkScalar radius, |
| 2983 | const SkPaint& paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2984 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawCircle()"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2985 | if (radius < 0) { |
| 2986 | radius = 0; |
| 2987 | } |
| 2988 | |
| 2989 | SkRect r; |
| 2990 | r.set(cx - radius, cy - radius, cx + radius, cy + radius); |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2991 | this->drawOval(r, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2992 | } |
| 2993 | |
| 2994 | void SkCanvas::drawRoundRect(const SkRect& r, SkScalar rx, SkScalar ry, |
| 2995 | const SkPaint& paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 2996 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawRoundRect()"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2997 | if (rx > 0 && ry > 0) { |
reed@google.com | 4ed0fb7 | 2012-12-12 20:48:18 +0000 | [diff] [blame] | 2998 | SkRRect rrect; |
| 2999 | rrect.setRectXY(r, rx, ry); |
| 3000 | this->drawRRect(rrect, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3001 | } else { |
| 3002 | this->drawRect(r, paint); |
| 3003 | } |
| 3004 | } |
| 3005 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3006 | void SkCanvas::drawArc(const SkRect& oval, SkScalar startAngle, |
| 3007 | SkScalar sweepAngle, bool useCenter, |
| 3008 | const SkPaint& paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 3009 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawArc()"); |
bsalomon | 21af9ca | 2016-08-25 12:29:23 -0700 | [diff] [blame] | 3010 | if (oval.isEmpty() || !sweepAngle) { |
| 3011 | return; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3012 | } |
bsalomon | 21af9ca | 2016-08-25 12:29:23 -0700 | [diff] [blame] | 3013 | this->onDrawArc(oval, startAngle, sweepAngle, useCenter, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3014 | } |
| 3015 | |
| 3016 | void SkCanvas::drawTextOnPathHV(const void* text, size_t byteLength, |
| 3017 | const SkPath& path, SkScalar hOffset, |
| 3018 | SkScalar vOffset, const SkPaint& paint) { |
danakj | 9881d63 | 2014-11-26 12:41:06 -0800 | [diff] [blame] | 3019 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawTextOnPathHV()"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3020 | SkMatrix matrix; |
reed@google.com | 4b22602 | 2011-01-11 18:32:13 +0000 | [diff] [blame] | 3021 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3022 | matrix.setTranslate(hOffset, vOffset); |
| 3023 | this->drawTextOnPath(text, byteLength, path, &matrix, paint); |
| 3024 | } |
| 3025 | |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 3026 | /////////////////////////////////////////////////////////////////////////////// |
reed | 1c2c441 | 2015-04-30 13:09:24 -0700 | [diff] [blame] | 3027 | |
| 3028 | /** |
| 3029 | * This constant is trying to balance the speed of ref'ing a subpicture into a parent picture, |
| 3030 | * against the playback cost of recursing into the subpicture to get at its actual ops. |
| 3031 | * |
| 3032 | * For now we pick a conservatively small value, though measurement (and other heuristics like |
| 3033 | * the type of ops contained) may justify changing this value. |
| 3034 | */ |
| 3035 | #define kMaxPictureOpsToUnrollInsteadOfRef 1 |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 3036 | |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 3037 | void SkCanvas::drawPicture(const SkPicture* picture, const SkMatrix* matrix, const SkPaint* paint) { |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 3038 | RETURN_ON_NULL(picture); |
| 3039 | |
reed | 1c2c441 | 2015-04-30 13:09:24 -0700 | [diff] [blame] | 3040 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawPicture()"); |
reed | e3b38ce | 2016-01-08 09:18:44 -0800 | [diff] [blame] | 3041 | if (matrix && matrix->isIdentity()) { |
| 3042 | matrix = nullptr; |
| 3043 | } |
| 3044 | if (picture->approximateOpCount() <= kMaxPictureOpsToUnrollInsteadOfRef) { |
| 3045 | SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect()); |
| 3046 | picture->playback(this); |
| 3047 | } else { |
| 3048 | this->onDrawPicture(picture, matrix, paint); |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 3049 | } |
| 3050 | } |
robertphillips | 9b14f26 | 2014-06-04 05:40:44 -0700 | [diff] [blame] | 3051 | |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 3052 | void SkCanvas::onDrawPicture(const SkPicture* picture, const SkMatrix* matrix, |
| 3053 | const SkPaint* paint) { |
fmalita | d028180 | 2015-08-20 12:08:18 -0700 | [diff] [blame] | 3054 | if (!paint || paint->canComputeFastBounds()) { |
| 3055 | SkRect bounds = picture->cullRect(); |
| 3056 | if (paint) { |
| 3057 | paint->computeFastBounds(bounds, &bounds); |
| 3058 | } |
| 3059 | if (matrix) { |
| 3060 | matrix->mapRect(&bounds); |
| 3061 | } |
| 3062 | if (this->quickReject(bounds)) { |
| 3063 | return; |
| 3064 | } |
| 3065 | } |
| 3066 | |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 3067 | SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect()); |
robertphillips | c5ba71d | 2014-09-04 08:42:50 -0700 | [diff] [blame] | 3068 | picture->playback(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3069 | } |
| 3070 | |
vjiaoblack | 95302da | 2016-07-21 10:25:54 -0700 | [diff] [blame] | 3071 | #ifdef SK_EXPERIMENTAL_SHADOWING |
| 3072 | void SkCanvas::drawShadowedPicture(const SkPicture* picture, |
| 3073 | const SkMatrix* matrix, |
vjiaoblack | e6f5d56 | 2016-08-25 06:30:23 -0700 | [diff] [blame] | 3074 | const SkPaint* paint, |
| 3075 | const SkShadowParams& params) { |
vjiaoblack | 95302da | 2016-07-21 10:25:54 -0700 | [diff] [blame] | 3076 | RETURN_ON_NULL(picture); |
| 3077 | |
| 3078 | TRACE_EVENT0("disabled-by-default-skia", "SkCanvas::drawShadowedPicture()"); |
| 3079 | |
vjiaoblack | e6f5d56 | 2016-08-25 06:30:23 -0700 | [diff] [blame] | 3080 | this->onDrawShadowedPicture(picture, matrix, paint, params); |
vjiaoblack | 95302da | 2016-07-21 10:25:54 -0700 | [diff] [blame] | 3081 | } |
| 3082 | |
| 3083 | void SkCanvas::onDrawShadowedPicture(const SkPicture* picture, |
| 3084 | const SkMatrix* matrix, |
vjiaoblack | e6f5d56 | 2016-08-25 06:30:23 -0700 | [diff] [blame] | 3085 | const SkPaint* paint, |
| 3086 | const SkShadowParams& params) { |
vjiaoblack | 904527d | 2016-08-09 09:32:09 -0700 | [diff] [blame] | 3087 | if (!paint || paint->canComputeFastBounds()) { |
| 3088 | SkRect bounds = picture->cullRect(); |
| 3089 | if (paint) { |
| 3090 | paint->computeFastBounds(bounds, &bounds); |
| 3091 | } |
| 3092 | if (matrix) { |
| 3093 | matrix->mapRect(&bounds); |
| 3094 | } |
| 3095 | if (this->quickReject(bounds)) { |
| 3096 | return; |
| 3097 | } |
| 3098 | } |
| 3099 | |
| 3100 | SkAutoCanvasMatrixPaint acmp(this, matrix, paint, picture->cullRect()); |
| 3101 | |
vjiaoblack | e6f5d56 | 2016-08-25 06:30:23 -0700 | [diff] [blame] | 3102 | sk_sp<SkImage> povDepthMap; |
| 3103 | sk_sp<SkImage> diffuseMap; |
| 3104 | |
vjiaoblack | 904527d | 2016-08-09 09:32:09 -0700 | [diff] [blame] | 3105 | // povDepthMap |
| 3106 | { |
| 3107 | SkLights::Builder builder; |
vjiaoblack | 772b5ee | 2016-08-12 11:38:47 -0700 | [diff] [blame] | 3108 | builder.add(SkLights::Light::MakeDirectional(SkColor3f::Make(1.0f, 1.0f, 1.0f), |
| 3109 | SkVector3::Make(0.0f, 0.0f, 1.0f))); |
vjiaoblack | 904527d | 2016-08-09 09:32:09 -0700 | [diff] [blame] | 3110 | sk_sp<SkLights> povLight = builder.finish(); |
| 3111 | |
| 3112 | SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(), |
| 3113 | picture->cullRect().height(), |
| 3114 | kBGRA_8888_SkColorType, |
| 3115 | kOpaque_SkAlphaType); |
| 3116 | |
| 3117 | // Create a new surface (that matches the backend of canvas) |
| 3118 | // to create the povDepthMap |
| 3119 | sk_sp<SkSurface> surf(this->makeSurface(info)); |
| 3120 | |
| 3121 | // Wrap another SPFCanvas around the surface |
| 3122 | sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = |
| 3123 | sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); |
| 3124 | |
| 3125 | // set the depth map canvas to have the light as the user's POV |
| 3126 | depthMapCanvas->setLights(std::move(povLight)); |
| 3127 | |
| 3128 | depthMapCanvas->drawPicture(picture); |
vjiaoblack | 904527d | 2016-08-09 09:32:09 -0700 | [diff] [blame] | 3129 | povDepthMap = surf->makeImageSnapshot(); |
| 3130 | } |
| 3131 | |
| 3132 | // diffuseMap |
| 3133 | { |
| 3134 | SkImageInfo info = SkImageInfo::Make(picture->cullRect().width(), |
| 3135 | picture->cullRect().height(), |
| 3136 | kBGRA_8888_SkColorType, |
| 3137 | kOpaque_SkAlphaType); |
| 3138 | |
| 3139 | sk_sp<SkSurface> surf(this->makeSurface(info)); |
| 3140 | surf->getCanvas()->drawPicture(picture); |
| 3141 | |
| 3142 | diffuseMap = surf->makeImageSnapshot(); |
| 3143 | } |
vjiaoblack | 904527d | 2016-08-09 09:32:09 -0700 | [diff] [blame] | 3144 | |
| 3145 | sk_sp<SkShader> povDepthShader = povDepthMap->makeShader(SkShader::kClamp_TileMode, |
| 3146 | SkShader::kClamp_TileMode); |
vjiaoblack | 904527d | 2016-08-09 09:32:09 -0700 | [diff] [blame] | 3147 | sk_sp<SkShader> diffuseShader = diffuseMap->makeShader(SkShader::kClamp_TileMode, |
| 3148 | SkShader::kClamp_TileMode); |
vjiaoblack | b2796fd | 2016-09-09 09:22:39 -0700 | [diff] [blame] | 3149 | |
| 3150 | // TODO: pass the depth to the shader in vertices, or uniforms |
| 3151 | // so we don't have to render depth and color separately |
| 3152 | for (int i = 0; i < fLights->numLights(); ++i) { |
| 3153 | // skip over ambient lights; they don't cast shadows |
| 3154 | // lights that have shadow maps do not need updating (because lights are immutable) |
| 3155 | sk_sp<SkImage> depthMap; |
| 3156 | SkISize shMapSize; |
| 3157 | |
| 3158 | if (fLights->light(i).getShadowMap() != nullptr) { |
| 3159 | continue; |
| 3160 | } |
| 3161 | |
| 3162 | if (fLights->light(i).isRadial()) { |
| 3163 | shMapSize.fHeight = 1; |
| 3164 | shMapSize.fWidth = (int) picture->cullRect().width(); |
| 3165 | |
| 3166 | SkImageInfo info = SkImageInfo::Make(diffuseMap->width(), 1, |
| 3167 | kBGRA_8888_SkColorType, |
| 3168 | kOpaque_SkAlphaType); |
| 3169 | |
| 3170 | // Create new surface (that matches the backend of canvas) |
| 3171 | // for each shadow map |
| 3172 | sk_sp<SkSurface> surf(this->makeSurface(info)); |
| 3173 | |
| 3174 | // Wrap another SPFCanvas around the surface |
| 3175 | SkCanvas* depthMapCanvas = surf->getCanvas(); |
| 3176 | |
| 3177 | SkLights::Builder builder; |
| 3178 | builder.add(fLights->light(i)); |
| 3179 | sk_sp<SkLights> curLight = builder.finish(); |
| 3180 | |
| 3181 | sk_sp<SkShader> shadowMapShader; |
| 3182 | shadowMapShader = SkRadialShadowMapShader::Make( |
| 3183 | povDepthShader, curLight, |
| 3184 | (int) picture->cullRect().width(), |
| 3185 | (int) picture->cullRect().height()); |
| 3186 | |
| 3187 | SkPaint shadowMapPaint; |
| 3188 | shadowMapPaint.setShader(std::move(shadowMapShader)); |
| 3189 | |
| 3190 | depthMapCanvas->setLights(curLight); |
| 3191 | |
| 3192 | depthMapCanvas->drawRect(SkRect::MakeIWH(diffuseMap->width(), |
| 3193 | diffuseMap->height()), |
| 3194 | shadowMapPaint); |
| 3195 | |
| 3196 | depthMap = surf->makeImageSnapshot(); |
| 3197 | |
| 3198 | } else { |
| 3199 | // TODO: compute the correct size of the depth map from the light properties |
| 3200 | // TODO: maybe add a kDepth_8_SkColorType |
| 3201 | // TODO: find actual max depth of picture |
| 3202 | shMapSize = SkShadowPaintFilterCanvas::ComputeDepthMapSize( |
| 3203 | fLights->light(i), 255, |
| 3204 | (int) picture->cullRect().width(), |
| 3205 | (int) picture->cullRect().height()); |
| 3206 | |
| 3207 | SkImageInfo info = SkImageInfo::Make(shMapSize.fWidth, shMapSize.fHeight, |
| 3208 | kBGRA_8888_SkColorType, |
| 3209 | kOpaque_SkAlphaType); |
| 3210 | |
| 3211 | // Create a new surface (that matches the backend of canvas) |
| 3212 | // for each shadow map |
| 3213 | sk_sp<SkSurface> surf(this->makeSurface(info)); |
| 3214 | |
| 3215 | // Wrap another SPFCanvas around the surface |
| 3216 | sk_sp<SkShadowPaintFilterCanvas> depthMapCanvas = |
| 3217 | sk_make_sp<SkShadowPaintFilterCanvas>(surf->getCanvas()); |
| 3218 | depthMapCanvas->setShadowParams(params); |
| 3219 | |
| 3220 | // set the depth map canvas to have the light we're drawing. |
| 3221 | SkLights::Builder builder; |
| 3222 | builder.add(fLights->light(i)); |
| 3223 | sk_sp<SkLights> curLight = builder.finish(); |
| 3224 | depthMapCanvas->setLights(std::move(curLight)); |
| 3225 | |
| 3226 | depthMapCanvas->drawPicture(picture); |
| 3227 | depthMap = surf->makeImageSnapshot(); |
| 3228 | } |
| 3229 | |
| 3230 | if (params.fType == SkShadowParams::kNoBlur_ShadowType) { |
| 3231 | fLights->light(i).setShadowMap(std::move(depthMap)); |
| 3232 | } else if (params.fType == SkShadowParams::kVariance_ShadowType) { |
| 3233 | // we blur the variance map |
| 3234 | SkPaint blurPaint; |
| 3235 | blurPaint.setImageFilter(SkImageFilter::MakeBlur(params.fShadowRadius, |
| 3236 | params.fShadowRadius, nullptr)); |
| 3237 | |
| 3238 | SkImageInfo blurInfo = SkImageInfo::Make(shMapSize.fWidth, shMapSize.fHeight, |
| 3239 | kBGRA_8888_SkColorType, |
| 3240 | kOpaque_SkAlphaType); |
| 3241 | |
| 3242 | sk_sp<SkSurface> blurSurf(this->makeSurface(blurInfo)); |
| 3243 | |
| 3244 | blurSurf->getCanvas()->drawImage(std::move(depthMap), 0, 0, &blurPaint); |
| 3245 | |
| 3246 | fLights->light(i).setShadowMap(blurSurf->makeImageSnapshot()); |
| 3247 | } |
| 3248 | } |
| 3249 | |
| 3250 | SkPaint shadowPaint; |
vjiaoblack | 904527d | 2016-08-09 09:32:09 -0700 | [diff] [blame] | 3251 | sk_sp<SkShader> shadowShader = SkShadowShader::Make(std::move(povDepthShader), |
| 3252 | std::move(diffuseShader), |
vjiaoblack | b2796fd | 2016-09-09 09:22:39 -0700 | [diff] [blame] | 3253 | fLights, |
vjiaoblack | 904527d | 2016-08-09 09:32:09 -0700 | [diff] [blame] | 3254 | diffuseMap->width(), |
vjiaoblack | e6f5d56 | 2016-08-25 06:30:23 -0700 | [diff] [blame] | 3255 | diffuseMap->height(), |
| 3256 | params); |
vjiaoblack | 904527d | 2016-08-09 09:32:09 -0700 | [diff] [blame] | 3257 | |
| 3258 | shadowPaint.setShader(shadowShader); |
| 3259 | |
| 3260 | this->drawRect(SkRect::MakeIWH(diffuseMap->width(), diffuseMap->height()), shadowPaint); |
vjiaoblack | 95302da | 2016-07-21 10:25:54 -0700 | [diff] [blame] | 3261 | } |
| 3262 | #endif |
| 3263 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3264 | /////////////////////////////////////////////////////////////////////////////// |
| 3265 | /////////////////////////////////////////////////////////////////////////////// |
| 3266 | |
reed | 3aafe11 | 2016-08-18 12:45:34 -0700 | [diff] [blame] | 3267 | SkCanvas::LayerIter::LayerIter(SkCanvas* canvas) { |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 3268 | static_assert(sizeof(fStorage) >= sizeof(SkDrawIter), "fStorage_too_small"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3269 | |
| 3270 | SkASSERT(canvas); |
| 3271 | |
reed | 3aafe11 | 2016-08-18 12:45:34 -0700 | [diff] [blame] | 3272 | fImpl = new (fStorage) SkDrawIter(canvas); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3273 | fDone = !fImpl->next(); |
| 3274 | } |
| 3275 | |
| 3276 | SkCanvas::LayerIter::~LayerIter() { |
| 3277 | fImpl->~SkDrawIter(); |
| 3278 | } |
| 3279 | |
| 3280 | void SkCanvas::LayerIter::next() { |
| 3281 | fDone = !fImpl->next(); |
| 3282 | } |
| 3283 | |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 3284 | SkBaseDevice* SkCanvas::LayerIter::device() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3285 | return fImpl->getDevice(); |
| 3286 | } |
| 3287 | |
| 3288 | const SkMatrix& SkCanvas::LayerIter::matrix() const { |
| 3289 | return fImpl->getMatrix(); |
| 3290 | } |
| 3291 | |
| 3292 | const SkPaint& SkCanvas::LayerIter::paint() const { |
| 3293 | const SkPaint* paint = fImpl->getPaint(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 3294 | if (nullptr == paint) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3295 | paint = &fDefaultPaint; |
| 3296 | } |
| 3297 | return *paint; |
| 3298 | } |
| 3299 | |
reed | 1e7f5e7 | 2016-04-27 07:49:17 -0700 | [diff] [blame] | 3300 | const SkRasterClip& SkCanvas::LayerIter::clip() const { return fImpl->getClip(); } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3301 | int SkCanvas::LayerIter::x() const { return fImpl->getX(); } |
| 3302 | int SkCanvas::LayerIter::y() const { return fImpl->getY(); } |
justinlin@google.com | 20a550c | 2012-07-27 17:37:12 +0000 | [diff] [blame] | 3303 | |
| 3304 | /////////////////////////////////////////////////////////////////////////////// |
| 3305 | |
fmalita | c3b589a | 2014-06-05 12:40:07 -0700 | [diff] [blame] | 3306 | SkCanvasClipVisitor::~SkCanvasClipVisitor() { } |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 3307 | |
| 3308 | /////////////////////////////////////////////////////////////////////////////// |
| 3309 | |
| 3310 | static bool supported_for_raster_canvas(const SkImageInfo& info) { |
| 3311 | switch (info.alphaType()) { |
| 3312 | case kPremul_SkAlphaType: |
| 3313 | case kOpaque_SkAlphaType: |
| 3314 | break; |
| 3315 | default: |
| 3316 | return false; |
| 3317 | } |
| 3318 | |
| 3319 | switch (info.colorType()) { |
| 3320 | case kAlpha_8_SkColorType: |
| 3321 | case kRGB_565_SkColorType: |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 3322 | case kN32_SkColorType: |
junov | 9e3dbdf | 2016-10-13 13:14:27 -0700 | [diff] [blame] | 3323 | case kRGBA_F16_SkColorType: |
commit-bot@chromium.org | 3107b6a | 2014-02-27 20:32:51 +0000 | [diff] [blame] | 3324 | break; |
| 3325 | default: |
| 3326 | return false; |
| 3327 | } |
| 3328 | |
| 3329 | return true; |
| 3330 | } |
| 3331 | |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 3332 | std::unique_ptr<SkCanvas> SkCanvas::MakeRasterDirect(const SkImageInfo& info, void* pixels, |
| 3333 | size_t rowBytes) { |
commit-bot@chromium.org | 42b0893 | 2014-03-17 02:13:07 +0000 | [diff] [blame] | 3334 | if (!supported_for_raster_canvas(info)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 3335 | return nullptr; |
commit-bot@chromium.org | 42b0893 | 2014-03-17 02:13:07 +0000 | [diff] [blame] | 3336 | } |
skia.committer@gmail.com | eb849e5 | 2014-03-17 03:02:17 +0000 | [diff] [blame] | 3337 | |
commit-bot@chromium.org | 42b0893 | 2014-03-17 02:13:07 +0000 | [diff] [blame] | 3338 | SkBitmap bitmap; |
| 3339 | if (!bitmap.installPixels(info, pixels, rowBytes)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 3340 | return nullptr; |
commit-bot@chromium.org | 42b0893 | 2014-03-17 02:13:07 +0000 | [diff] [blame] | 3341 | } |
Mike Reed | 5df4934 | 2016-11-12 08:06:55 -0600 | [diff] [blame] | 3342 | return skstd::make_unique<SkCanvas>(bitmap); |
commit-bot@chromium.org | 42b0893 | 2014-03-17 02:13:07 +0000 | [diff] [blame] | 3343 | } |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 3344 | |
| 3345 | /////////////////////////////////////////////////////////////////////////////// |
| 3346 | |
| 3347 | SkAutoCanvasMatrixPaint::SkAutoCanvasMatrixPaint(SkCanvas* canvas, const SkMatrix* matrix, |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 3348 | const SkPaint* paint, const SkRect& bounds) |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 3349 | : fCanvas(canvas) |
| 3350 | , fSaveCount(canvas->getSaveCount()) |
| 3351 | { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 3352 | if (paint) { |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 3353 | SkRect newBounds = bounds; |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 3354 | if (matrix) { |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 3355 | matrix->mapRect(&newBounds); |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 3356 | } |
robertphillips | a8d7f0b | 2014-08-29 08:03:56 -0700 | [diff] [blame] | 3357 | canvas->saveLayer(&newBounds, paint); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 3358 | } else if (matrix) { |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 3359 | canvas->save(); |
| 3360 | } |
mtklein | 6cfa73a | 2014-08-13 13:33:49 -0700 | [diff] [blame] | 3361 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 3362 | if (matrix) { |
reed | d5fa1a4 | 2014-08-09 11:08:05 -0700 | [diff] [blame] | 3363 | canvas->concat(*matrix); |
| 3364 | } |
| 3365 | } |
| 3366 | |
| 3367 | SkAutoCanvasMatrixPaint::~SkAutoCanvasMatrixPaint() { |
| 3368 | fCanvas->restoreToCount(fSaveCount); |
| 3369 | } |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 3370 | |
Florin Malita | ee424ac | 2016-12-01 12:47:59 -0500 | [diff] [blame] | 3371 | /////////////////////////////////////////////////////////////////////////////// |
| 3372 | |
| 3373 | SkNoDrawCanvas::SkNoDrawCanvas(int width, int height) |
| 3374 | : INHERITED(SkIRect::MakeWH(width, height), kConservativeRasterClip_InitFlag) {} |
| 3375 | |
Florin Malita | 439ace9 | 2016-12-02 12:05:41 -0500 | [diff] [blame] | 3376 | SkNoDrawCanvas::SkNoDrawCanvas(const SkIRect& bounds) |
| 3377 | : INHERITED(bounds, kConservativeRasterClip_InitFlag) {} |
| 3378 | |
Florin Malita | ee424ac | 2016-12-01 12:47:59 -0500 | [diff] [blame] | 3379 | SkCanvas::SaveLayerStrategy SkNoDrawCanvas::getSaveLayerStrategy(const SaveLayerRec& rec) { |
| 3380 | (void)this->INHERITED::getSaveLayerStrategy(rec); |
| 3381 | return kNoLayer_SaveLayerStrategy; |
| 3382 | } |
| 3383 | |
| 3384 | /////////////////////////////////////////////////////////////////////////////// |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 3385 | |
reed | 73603f3 | 2016-09-20 08:42:38 -0700 | [diff] [blame] | 3386 | static_assert((int)SkRegion::kDifference_Op == (int)kDifference_SkClipOp, ""); |
| 3387 | static_assert((int)SkRegion::kIntersect_Op == (int)kIntersect_SkClipOp, ""); |
| 3388 | static_assert((int)SkRegion::kUnion_Op == (int)kUnion_SkClipOp, ""); |
| 3389 | static_assert((int)SkRegion::kXOR_Op == (int)kXOR_SkClipOp, ""); |
| 3390 | static_assert((int)SkRegion::kReverseDifference_Op == (int)kReverseDifference_SkClipOp, ""); |
| 3391 | static_assert((int)SkRegion::kReplace_Op == (int)kReplace_SkClipOp, ""); |