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