epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 7 | |
bungeman | 903dcb0 | 2015-03-16 13:00:09 -0400 | [diff] [blame] | 8 | #define __STDC_LIMIT_MACROS |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 10 | #include "SkArenaAlloc.h" |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 11 | #include "SkAutoBlitterChoose.h" |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 12 | #include "SkBlendModePriv.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkBlitter.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkCanvas.h" |
Cary Clark | a4083c9 | 2017-09-15 11:59:23 -0400 | [diff] [blame] | 15 | #include "SkColorData.h" |
Mike Reed | 986480a | 2017-01-13 22:43:16 +0000 | [diff] [blame] | 16 | #include "SkDevice.h" |
reed@google.com | 1c028bd | 2013-08-28 15:23:19 +0000 | [diff] [blame] | 17 | #include "SkDeviceLooper.h" |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 18 | #include "SkDraw.h" |
| 19 | #include "SkDrawProcs.h" |
herb | e5911c9 | 2015-11-09 13:15:21 -0800 | [diff] [blame] | 20 | #include "SkFindAndPlaceGlyph.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | #include "SkMaskFilter.h" |
herb | f553e4e | 2015-11-09 08:51:56 -0800 | [diff] [blame] | 22 | #include "SkMatrix.h" |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 23 | #include "SkMatrixUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | #include "SkPaint.h" |
| 25 | #include "SkPathEffect.h" |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 26 | #include "SkRasterClip.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | #include "SkRasterizer.h" |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 28 | #include "SkRRect.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | #include "SkScan.h" |
| 30 | #include "SkShader.h" |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 31 | #include "SkString.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | #include "SkStroke.h" |
halcanary | 435657f | 2015-09-15 12:53:07 -0700 | [diff] [blame] | 33 | #include "SkStrokeRec.h" |
herb | f553e4e | 2015-11-09 08:51:56 -0800 | [diff] [blame] | 34 | #include "SkTemplates.h" |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 35 | #include "SkTextMapStateProc.h" |
reed@google.com | 32e5d97 | 2011-07-27 18:25:57 +0000 | [diff] [blame] | 36 | #include "SkTLazy.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | #include "SkUtils.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | |
Herb Derby | 4bf560a | 2017-02-14 11:13:26 -0500 | [diff] [blame] | 39 | static SkPaint make_paint_with_image( |
| 40 | const SkPaint& origPaint, const SkBitmap& bitmap, SkMatrix* matrix = nullptr) { |
| 41 | SkPaint paint(origPaint); |
| 42 | paint.setShader(SkMakeBitmapShader(bitmap, SkShader::kClamp_TileMode, |
| 43 | SkShader::kClamp_TileMode, matrix, |
| 44 | kNever_SkCopyPixelsMode)); |
| 45 | return paint; |
| 46 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | /////////////////////////////////////////////////////////////////////////////// |
| 49 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 50 | SkDraw::SkDraw() { |
| 51 | sk_bzero(this, sizeof(*this)); |
| 52 | } |
| 53 | |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 54 | bool SkDraw::computeConservativeLocalClipBounds(SkRect* localBounds) const { |
| 55 | if (fRC->isEmpty()) { |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | SkMatrix inverse; |
| 60 | if (!fMatrix->invert(&inverse)) { |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | SkIRect devBounds = fRC->getBounds(); |
| 65 | // outset to have slop for antialasing and hairlines |
| 66 | devBounds.outset(1, 1); |
| 67 | inverse.mapRect(localBounds, SkRect::Make(devBounds)); |
| 68 | return true; |
| 69 | } |
| 70 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | /////////////////////////////////////////////////////////////////////////////// |
| 72 | |
| 73 | typedef void (*BitmapXferProc)(void* pixels, size_t bytes, uint32_t data); |
| 74 | |
| 75 | static void D_Clear_BitmapXferProc(void* pixels, size_t bytes, uint32_t) { |
reed@android.com | 4516f47 | 2009-06-29 16:25:36 +0000 | [diff] [blame] | 76 | sk_bzero(pixels, bytes); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static void D_Dst_BitmapXferProc(void*, size_t, uint32_t data) {} |
| 80 | |
| 81 | static void D32_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) { |
commit-bot@chromium.org | a8c7f77 | 2014-01-24 21:46:29 +0000 | [diff] [blame] | 82 | sk_memset32((uint32_t*)pixels, data, SkToInt(bytes >> 2)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static void D16_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) { |
commit-bot@chromium.org | a8c7f77 | 2014-01-24 21:46:29 +0000 | [diff] [blame] | 86 | sk_memset16((uint16_t*)pixels, data, SkToInt(bytes >> 1)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static void DA8_Src_BitmapXferProc(void* pixels, size_t bytes, uint32_t data) { |
| 90 | memset(pixels, data, bytes); |
| 91 | } |
| 92 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 93 | static BitmapXferProc ChooseBitmapXferProc(const SkPixmap& dst, const SkPaint& paint, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | uint32_t* data) { |
| 95 | // todo: we can apply colorfilter up front if no shader, so we wouldn't |
| 96 | // need to abort this fastpath |
Mike Klein | 555ea8e | 2017-05-25 08:58:43 -0400 | [diff] [blame] | 97 | if (paint.getShader() || paint.getColorFilter() || dst.colorSpace()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 98 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 99 | } |
| 100 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 101 | SkBlendMode mode = paint.getBlendMode(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | SkColor color = paint.getColor(); |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 103 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | // collaps modes based on color... |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 105 | if (SkBlendMode::kSrcOver == mode) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | unsigned alpha = SkColorGetA(color); |
| 107 | if (0 == alpha) { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 108 | mode = SkBlendMode::kDst; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | } else if (0xFF == alpha) { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 110 | mode = SkBlendMode::kSrc; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 113 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | switch (mode) { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 115 | case SkBlendMode::kClear: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 116 | // SkDebugf("--- D_Clear_BitmapXferProc\n"); |
| 117 | return D_Clear_BitmapXferProc; // ignore data |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 118 | case SkBlendMode::kDst: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 119 | // SkDebugf("--- D_Dst_BitmapXferProc\n"); |
| 120 | return D_Dst_BitmapXferProc; // ignore data |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 121 | case SkBlendMode::kSrc: { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | /* |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 123 | should I worry about dithering for the lower depths? |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | */ |
| 125 | SkPMColor pmc = SkPreMultiplyColor(color); |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 126 | switch (dst.colorType()) { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 127 | case kN32_SkColorType: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | if (data) { |
| 129 | *data = pmc; |
| 130 | } |
| 131 | // SkDebugf("--- D32_Src_BitmapXferProc\n"); |
| 132 | return D32_Src_BitmapXferProc; |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 133 | case kRGB_565_SkColorType: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 134 | if (data) { |
| 135 | *data = SkPixel32ToPixel16(pmc); |
| 136 | } |
| 137 | // SkDebugf("--- D16_Src_BitmapXferProc\n"); |
| 138 | return D16_Src_BitmapXferProc; |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 139 | case kAlpha_8_SkColorType: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | if (data) { |
| 141 | *data = SkGetPackedA32(pmc); |
| 142 | } |
| 143 | // SkDebugf("--- DA8_Src_BitmapXferProc\n"); |
| 144 | return DA8_Src_BitmapXferProc; |
| 145 | default: |
| 146 | break; |
| 147 | } |
| 148 | break; |
| 149 | } |
| 150 | default: |
| 151 | break; |
| 152 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 153 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 154 | } |
| 155 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 156 | static void CallBitmapXferProc(const SkPixmap& dst, const SkIRect& rect, BitmapXferProc proc, |
| 157 | uint32_t procData) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | int shiftPerPixel; |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 159 | switch (dst.colorType()) { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 160 | case kN32_SkColorType: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 161 | shiftPerPixel = 2; |
| 162 | break; |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 163 | case kRGB_565_SkColorType: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 164 | shiftPerPixel = 1; |
| 165 | break; |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 166 | case kAlpha_8_SkColorType: |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | shiftPerPixel = 0; |
| 168 | break; |
| 169 | default: |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 170 | SkDEBUGFAIL("Can't use xferproc on this config"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 171 | return; |
| 172 | } |
| 173 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 174 | uint8_t* pixels = (uint8_t*)dst.writable_addr(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 175 | SkASSERT(pixels); |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 176 | const size_t rowBytes = dst.rowBytes(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 177 | const int widthBytes = rect.width() << shiftPerPixel; |
| 178 | |
| 179 | // skip down to the first scanline and X position |
| 180 | pixels += rect.fTop * rowBytes + (rect.fLeft << shiftPerPixel); |
| 181 | for (int scans = rect.height() - 1; scans >= 0; --scans) { |
| 182 | proc(pixels, widthBytes, procData); |
| 183 | pixels += rowBytes; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void SkDraw::drawPaint(const SkPaint& paint) const { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 188 | SkDEBUGCODE(this->validate();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 189 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 190 | if (fRC->isEmpty()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 191 | return; |
| 192 | } |
| 193 | |
| 194 | SkIRect devRect; |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 195 | devRect.set(0, 0, fDst.width(), fDst.height()); |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 196 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 197 | if (fRC->isBW()) { |
| 198 | /* If we don't have a shader (i.e. we're just a solid color) we may |
| 199 | be faster to operate directly on the device bitmap, rather than invoking |
| 200 | a blitter. Esp. true for xfermodes, which require a colorshader to be |
| 201 | present, which is just redundant work. Since we're drawing everywhere |
| 202 | in the clip, we don't have to worry about antialiasing. |
| 203 | */ |
| 204 | uint32_t procData = 0; // to avoid the warning |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 205 | BitmapXferProc proc = ChooseBitmapXferProc(fDst, paint, &procData); |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 206 | if (proc) { |
| 207 | if (D_Dst_BitmapXferProc == proc) { // nothing to do |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | SkRegion::Iterator iter(fRC->bwRgn()); |
| 212 | while (!iter.done()) { |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 213 | CallBitmapXferProc(fDst, iter.rect(), proc, procData); |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 214 | iter.next(); |
| 215 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 216 | return; |
| 217 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 218 | } |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 219 | |
| 220 | // normal case: use a blitter |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 221 | SkAutoBlitterChoose blitter(fDst, *fMatrix, paint); |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 222 | SkScan::FillIRect(devRect, *fRC, blitter.get()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | /////////////////////////////////////////////////////////////////////////////// |
| 226 | |
| 227 | struct PtProcRec { |
| 228 | SkCanvas::PointMode fMode; |
| 229 | const SkPaint* fPaint; |
| 230 | const SkRegion* fClip; |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 231 | const SkRasterClip* fRC; |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 232 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 233 | // computed values |
| 234 | SkFixed fRadius; |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 235 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 236 | typedef void (*Proc)(const PtProcRec&, const SkPoint devPts[], int count, |
| 237 | SkBlitter*); |
| 238 | |
| 239 | bool init(SkCanvas::PointMode, const SkPaint&, const SkMatrix* matrix, |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 240 | const SkRasterClip*); |
| 241 | Proc chooseProc(SkBlitter** blitter); |
| 242 | |
| 243 | private: |
| 244 | SkAAClipBlitterWrapper fWrapper; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | static void bw_pt_rect_hair_proc(const PtProcRec& rec, const SkPoint devPts[], |
| 248 | int count, SkBlitter* blitter) { |
| 249 | SkASSERT(rec.fClip->isRect()); |
| 250 | const SkIRect& r = rec.fClip->getBounds(); |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 251 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 252 | for (int i = 0; i < count; i++) { |
reed@google.com | 2d47a21 | 2012-11-29 21:01:00 +0000 | [diff] [blame] | 253 | int x = SkScalarFloorToInt(devPts[i].fX); |
| 254 | int y = SkScalarFloorToInt(devPts[i].fY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 255 | if (r.contains(x, y)) { |
| 256 | blitter->blitH(x, y, 1); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | static void bw_pt_rect_16_hair_proc(const PtProcRec& rec, |
| 262 | const SkPoint devPts[], int count, |
| 263 | SkBlitter* blitter) { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 264 | SkASSERT(rec.fRC->isRect()); |
| 265 | const SkIRect& r = rec.fRC->getBounds(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 266 | uint32_t value; |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 267 | const SkPixmap* dst = blitter->justAnOpaqueColor(&value); |
| 268 | SkASSERT(dst); |
skia.committer@gmail.com | c3d7d90 | 2012-11-30 02:01:24 +0000 | [diff] [blame] | 269 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 270 | uint16_t* addr = dst->writable_addr16(0, 0); |
| 271 | size_t rb = dst->rowBytes(); |
skia.committer@gmail.com | c3d7d90 | 2012-11-30 02:01:24 +0000 | [diff] [blame] | 272 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 273 | for (int i = 0; i < count; i++) { |
reed@google.com | 2d47a21 | 2012-11-29 21:01:00 +0000 | [diff] [blame] | 274 | int x = SkScalarFloorToInt(devPts[i].fX); |
| 275 | int y = SkScalarFloorToInt(devPts[i].fY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 276 | if (r.contains(x, y)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 277 | ((uint16_t*)((char*)addr + y * rb))[x] = SkToU16(value); |
| 278 | } |
| 279 | } |
| 280 | } |
| 281 | |
reed@google.com | 2d47a21 | 2012-11-29 21:01:00 +0000 | [diff] [blame] | 282 | static void bw_pt_rect_32_hair_proc(const PtProcRec& rec, |
| 283 | const SkPoint devPts[], int count, |
| 284 | SkBlitter* blitter) { |
| 285 | SkASSERT(rec.fRC->isRect()); |
| 286 | const SkIRect& r = rec.fRC->getBounds(); |
| 287 | uint32_t value; |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 288 | const SkPixmap* dst = blitter->justAnOpaqueColor(&value); |
| 289 | SkASSERT(dst); |
skia.committer@gmail.com | c3d7d90 | 2012-11-30 02:01:24 +0000 | [diff] [blame] | 290 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 291 | SkPMColor* addr = dst->writable_addr32(0, 0); |
| 292 | size_t rb = dst->rowBytes(); |
skia.committer@gmail.com | c3d7d90 | 2012-11-30 02:01:24 +0000 | [diff] [blame] | 293 | |
reed@google.com | 2d47a21 | 2012-11-29 21:01:00 +0000 | [diff] [blame] | 294 | for (int i = 0; i < count; i++) { |
| 295 | int x = SkScalarFloorToInt(devPts[i].fX); |
| 296 | int y = SkScalarFloorToInt(devPts[i].fY); |
| 297 | if (r.contains(x, y)) { |
| 298 | ((SkPMColor*)((char*)addr + y * rb))[x] = value; |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 303 | static void bw_pt_hair_proc(const PtProcRec& rec, const SkPoint devPts[], |
| 304 | int count, SkBlitter* blitter) { |
| 305 | for (int i = 0; i < count; i++) { |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 306 | int x = SkScalarFloorToInt(devPts[i].fX); |
| 307 | int y = SkScalarFloorToInt(devPts[i].fY); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 308 | if (rec.fClip->contains(x, y)) { |
| 309 | blitter->blitH(x, y, 1); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | static void bw_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[], |
| 315 | int count, SkBlitter* blitter) { |
| 316 | for (int i = 0; i < count; i += 2) { |
reed | 5dc6b7d | 2015-04-14 10:40:44 -0700 | [diff] [blame] | 317 | SkScan::HairLine(&devPts[i], 2, *rec.fRC, blitter); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
| 321 | static void bw_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[], |
| 322 | int count, SkBlitter* blitter) { |
reed | 5dc6b7d | 2015-04-14 10:40:44 -0700 | [diff] [blame] | 323 | SkScan::HairLine(devPts, count, *rec.fRC, blitter); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | // aa versions |
| 327 | |
| 328 | static void aa_line_hair_proc(const PtProcRec& rec, const SkPoint devPts[], |
| 329 | int count, SkBlitter* blitter) { |
| 330 | for (int i = 0; i < count; i += 2) { |
reed | 5dc6b7d | 2015-04-14 10:40:44 -0700 | [diff] [blame] | 331 | SkScan::AntiHairLine(&devPts[i], 2, *rec.fRC, blitter); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
| 335 | static void aa_poly_hair_proc(const PtProcRec& rec, const SkPoint devPts[], |
| 336 | int count, SkBlitter* blitter) { |
reed | 5dc6b7d | 2015-04-14 10:40:44 -0700 | [diff] [blame] | 337 | SkScan::AntiHairLine(devPts, count, *rec.fRC, blitter); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | // square procs (strokeWidth > 0 but matrix is square-scale (sx == sy) |
| 341 | |
| 342 | static void bw_square_proc(const PtProcRec& rec, const SkPoint devPts[], |
| 343 | int count, SkBlitter* blitter) { |
| 344 | const SkFixed radius = rec.fRadius; |
| 345 | for (int i = 0; i < count; i++) { |
| 346 | SkFixed x = SkScalarToFixed(devPts[i].fX); |
| 347 | SkFixed y = SkScalarToFixed(devPts[i].fY); |
skia.committer@gmail.com | c3d7d90 | 2012-11-30 02:01:24 +0000 | [diff] [blame] | 348 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 349 | SkXRect r; |
| 350 | r.fLeft = x - radius; |
| 351 | r.fTop = y - radius; |
| 352 | r.fRight = x + radius; |
| 353 | r.fBottom = y + radius; |
skia.committer@gmail.com | c3d7d90 | 2012-11-30 02:01:24 +0000 | [diff] [blame] | 354 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 355 | SkScan::FillXRect(r, *rec.fRC, blitter); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
| 359 | static void aa_square_proc(const PtProcRec& rec, const SkPoint devPts[], |
| 360 | int count, SkBlitter* blitter) { |
| 361 | const SkFixed radius = rec.fRadius; |
| 362 | for (int i = 0; i < count; i++) { |
| 363 | SkFixed x = SkScalarToFixed(devPts[i].fX); |
| 364 | SkFixed y = SkScalarToFixed(devPts[i].fY); |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 365 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 366 | SkXRect r; |
| 367 | r.fLeft = x - radius; |
| 368 | r.fTop = y - radius; |
| 369 | r.fRight = x + radius; |
| 370 | r.fBottom = y + radius; |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 371 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 372 | SkScan::AntiFillXRect(r, *rec.fRC, blitter); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
reed@android.com | b4f404a | 2009-07-10 17:02:17 +0000 | [diff] [blame] | 376 | // If this guy returns true, then chooseProc() must return a valid proc |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 377 | bool PtProcRec::init(SkCanvas::PointMode mode, const SkPaint& paint, |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 378 | const SkMatrix* matrix, const SkRasterClip* rc) { |
ochang | 3ece53e | 2015-05-21 15:44:53 -0700 | [diff] [blame] | 379 | if ((unsigned)mode > (unsigned)SkCanvas::kPolygon_PointMode) { |
| 380 | return false; |
| 381 | } |
| 382 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 383 | if (paint.getPathEffect()) { |
| 384 | return false; |
| 385 | } |
| 386 | SkScalar width = paint.getStrokeWidth(); |
| 387 | if (0 == width) { |
| 388 | fMode = mode; |
| 389 | fPaint = &paint; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 390 | fClip = nullptr; |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 391 | fRC = rc; |
reed@google.com | 2d47a21 | 2012-11-29 21:01:00 +0000 | [diff] [blame] | 392 | fRadius = SK_FixedHalf; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 393 | return true; |
| 394 | } |
reed@android.com | b4f404a | 2009-07-10 17:02:17 +0000 | [diff] [blame] | 395 | if (paint.getStrokeCap() != SkPaint::kRound_Cap && |
robertphillips | 9f2251c | 2014-11-04 13:33:50 -0800 | [diff] [blame] | 396 | matrix->isScaleTranslate() && SkCanvas::kPoints_PointMode == mode) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 397 | SkScalar sx = matrix->get(SkMatrix::kMScaleX); |
| 398 | SkScalar sy = matrix->get(SkMatrix::kMScaleY); |
| 399 | if (SkScalarNearlyZero(sx - sy)) { |
| 400 | if (sx < 0) { |
| 401 | sx = -sx; |
| 402 | } |
| 403 | |
| 404 | fMode = mode; |
| 405 | fPaint = &paint; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 406 | fClip = nullptr; |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 407 | fRC = rc; |
Mike Reed | a99b6ce | 2017-02-04 11:04:26 -0500 | [diff] [blame] | 408 | fRadius = SkScalarToFixed(width * sx) >> 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 409 | return true; |
| 410 | } |
| 411 | } |
| 412 | return false; |
| 413 | } |
| 414 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 415 | PtProcRec::Proc PtProcRec::chooseProc(SkBlitter** blitterPtr) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 416 | Proc proc = nullptr; |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 417 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 418 | SkBlitter* blitter = *blitterPtr; |
| 419 | if (fRC->isBW()) { |
| 420 | fClip = &fRC->bwRgn(); |
| 421 | } else { |
| 422 | fWrapper.init(*fRC, blitter); |
| 423 | fClip = &fWrapper.getRgn(); |
| 424 | blitter = fWrapper.getBlitter(); |
| 425 | *blitterPtr = blitter; |
| 426 | } |
| 427 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 428 | // for our arrays |
| 429 | SkASSERT(0 == SkCanvas::kPoints_PointMode); |
| 430 | SkASSERT(1 == SkCanvas::kLines_PointMode); |
| 431 | SkASSERT(2 == SkCanvas::kPolygon_PointMode); |
| 432 | SkASSERT((unsigned)fMode <= (unsigned)SkCanvas::kPolygon_PointMode); |
| 433 | |
reed@google.com | 2d47a21 | 2012-11-29 21:01:00 +0000 | [diff] [blame] | 434 | if (fPaint->isAntiAlias()) { |
| 435 | if (0 == fPaint->getStrokeWidth()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 436 | static const Proc gAAProcs[] = { |
| 437 | aa_square_proc, aa_line_hair_proc, aa_poly_hair_proc |
| 438 | }; |
| 439 | proc = gAAProcs[fMode]; |
reed@google.com | 2d47a21 | 2012-11-29 21:01:00 +0000 | [diff] [blame] | 440 | } else if (fPaint->getStrokeCap() != SkPaint::kRound_Cap) { |
| 441 | SkASSERT(SkCanvas::kPoints_PointMode == fMode); |
| 442 | proc = aa_square_proc; |
| 443 | } |
| 444 | } else { // BW |
| 445 | if (fRadius <= SK_FixedHalf) { // small radii and hairline |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 446 | if (SkCanvas::kPoints_PointMode == fMode && fClip->isRect()) { |
| 447 | uint32_t value; |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 448 | const SkPixmap* bm = blitter->justAnOpaqueColor(&value); |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 449 | if (bm && kRGB_565_SkColorType == bm->colorType()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 450 | proc = bw_pt_rect_16_hair_proc; |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 451 | } else if (bm && kN32_SkColorType == bm->colorType()) { |
reed@google.com | 2d47a21 | 2012-11-29 21:01:00 +0000 | [diff] [blame] | 452 | proc = bw_pt_rect_32_hair_proc; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 453 | } else { |
| 454 | proc = bw_pt_rect_hair_proc; |
| 455 | } |
| 456 | } else { |
| 457 | static Proc gBWProcs[] = { |
| 458 | bw_pt_hair_proc, bw_line_hair_proc, bw_poly_hair_proc |
| 459 | }; |
| 460 | proc = gBWProcs[fMode]; |
| 461 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 462 | } else { |
| 463 | proc = bw_square_proc; |
| 464 | } |
| 465 | } |
| 466 | return proc; |
| 467 | } |
| 468 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 469 | // each of these costs 8-bytes of stack space, so don't make it too large |
| 470 | // must be even for lines/polygon to work |
| 471 | #define MAX_DEV_PTS 32 |
| 472 | |
| 473 | void SkDraw::drawPoints(SkCanvas::PointMode mode, size_t count, |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 474 | const SkPoint pts[], const SkPaint& paint, |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 475 | SkBaseDevice* device) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 476 | // if we're in lines mode, force count to be even |
| 477 | if (SkCanvas::kLines_PointMode == mode) { |
| 478 | count &= ~(size_t)1; |
| 479 | } |
| 480 | |
| 481 | if ((long)count <= 0) { |
| 482 | return; |
| 483 | } |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 484 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 485 | SkASSERT(pts != nullptr); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 486 | SkDEBUGCODE(this->validate();) |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 487 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 488 | // nothing to draw |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 489 | if (fRC->isEmpty()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 490 | return; |
| 491 | } |
| 492 | |
| 493 | PtProcRec rec; |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 494 | if (!device && rec.init(mode, paint, fMatrix, fRC)) { |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 495 | SkAutoBlitterChoose blitter(fDst, *fMatrix, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 496 | |
| 497 | SkPoint devPts[MAX_DEV_PTS]; |
| 498 | const SkMatrix* matrix = fMatrix; |
| 499 | SkBlitter* bltr = blitter.get(); |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 500 | PtProcRec::Proc proc = rec.chooseProc(&bltr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 501 | // we have to back up subsequent passes if we're in polygon mode |
| 502 | const size_t backup = (SkCanvas::kPolygon_PointMode == mode); |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 503 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 504 | do { |
commit-bot@chromium.org | a8c7f77 | 2014-01-24 21:46:29 +0000 | [diff] [blame] | 505 | int n = SkToInt(count); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 506 | if (n > MAX_DEV_PTS) { |
| 507 | n = MAX_DEV_PTS; |
| 508 | } |
| 509 | matrix->mapPoints(devPts, pts, n); |
| 510 | proc(rec, devPts, n, bltr); |
| 511 | pts += n - backup; |
commit-bot@chromium.org | a8c7f77 | 2014-01-24 21:46:29 +0000 | [diff] [blame] | 512 | SkASSERT(SkToInt(count) >= n); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 513 | count -= n; |
| 514 | if (count > 0) { |
| 515 | count += backup; |
| 516 | } |
| 517 | } while (count != 0); |
| 518 | } else { |
| 519 | switch (mode) { |
| 520 | case SkCanvas::kPoints_PointMode: { |
| 521 | // temporarily mark the paint as filling. |
reed@google.com | 40c2ba2 | 2011-07-25 19:41:22 +0000 | [diff] [blame] | 522 | SkPaint newPaint(paint); |
| 523 | newPaint.setStyle(SkPaint::kFill_Style); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 524 | |
reed@google.com | 40c2ba2 | 2011-07-25 19:41:22 +0000 | [diff] [blame] | 525 | SkScalar width = newPaint.getStrokeWidth(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 526 | SkScalar radius = SkScalarHalf(width); |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 527 | |
reed@google.com | 40c2ba2 | 2011-07-25 19:41:22 +0000 | [diff] [blame] | 528 | if (newPaint.getStrokeCap() == SkPaint::kRound_Cap) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 529 | SkPath path; |
| 530 | SkMatrix preMatrix; |
mtklein | 7ef849d | 2014-11-24 09:11:45 -0800 | [diff] [blame] | 531 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 532 | path.addCircle(0, 0, radius); |
| 533 | for (size_t i = 0; i < count; i++) { |
| 534 | preMatrix.setTranslate(pts[i].fX, pts[i].fY); |
| 535 | // pass true for the last point, since we can modify |
| 536 | // then path then |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 537 | path.setIsVolatile((count-1) == i); |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 538 | if (device) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 539 | device->drawPath(path, newPaint, &preMatrix, (count-1) == i); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 540 | } else { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 541 | this->drawPath(path, newPaint, &preMatrix, (count-1) == i); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 542 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 543 | } |
| 544 | } else { |
| 545 | SkRect r; |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 546 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 547 | for (size_t i = 0; i < count; i++) { |
| 548 | r.fLeft = pts[i].fX - radius; |
| 549 | r.fTop = pts[i].fY - radius; |
| 550 | r.fRight = r.fLeft + width; |
| 551 | r.fBottom = r.fTop + width; |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 552 | if (device) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 553 | device->drawRect(r, newPaint); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 554 | } else { |
reed@google.com | 40c2ba2 | 2011-07-25 19:41:22 +0000 | [diff] [blame] | 555 | this->drawRect(r, newPaint); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 556 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | break; |
| 560 | } |
| 561 | case SkCanvas::kLines_PointMode: |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 562 | if (2 == count && paint.getPathEffect()) { |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 563 | // most likely a dashed line - see if it is one of the ones |
| 564 | // we can accelerate |
| 565 | SkStrokeRec rec(paint); |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 566 | SkPathEffect::PointData pointData; |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 567 | |
| 568 | SkPath path; |
| 569 | path.moveTo(pts[0]); |
| 570 | path.lineTo(pts[1]); |
| 571 | |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 572 | SkRect cullRect = SkRect::Make(fRC->getBounds()); |
skia.committer@gmail.com | 4024f32 | 2013-01-25 07:06:46 +0000 | [diff] [blame] | 573 | |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 574 | if (paint.getPathEffect()->asPoints(&pointData, path, rec, |
| 575 | *fMatrix, &cullRect)) { |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 576 | // 'asPoints' managed to find some fast path |
| 577 | |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 578 | SkPaint newP(paint); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 579 | newP.setPathEffect(nullptr); |
robertphillips@google.com | 935ad02 | 2012-12-05 19:07:21 +0000 | [diff] [blame] | 580 | newP.setStyle(SkPaint::kFill_Style); |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 581 | |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 582 | if (!pointData.fFirst.isEmpty()) { |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 583 | if (device) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 584 | device->drawPath(pointData.fFirst, newP); |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 585 | } else { |
| 586 | this->drawPath(pointData.fFirst, newP); |
| 587 | } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 588 | } |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 589 | |
| 590 | if (!pointData.fLast.isEmpty()) { |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 591 | if (device) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 592 | device->drawPath(pointData.fLast, newP); |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 593 | } else { |
| 594 | this->drawPath(pointData.fLast, newP); |
| 595 | } |
robertphillips@google.com | 935ad02 | 2012-12-05 19:07:21 +0000 | [diff] [blame] | 596 | } |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 597 | |
| 598 | if (pointData.fSize.fX == pointData.fSize.fY) { |
| 599 | // The rest of the dashed line can just be drawn as points |
| 600 | SkASSERT(pointData.fSize.fX == SkScalarHalf(newP.getStrokeWidth())); |
| 601 | |
| 602 | if (SkPathEffect::PointData::kCircles_PointFlag & pointData.fFlags) { |
| 603 | newP.setStrokeCap(SkPaint::kRound_Cap); |
| 604 | } else { |
| 605 | newP.setStrokeCap(SkPaint::kButt_Cap); |
| 606 | } |
| 607 | |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 608 | if (device) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 609 | device->drawPoints(SkCanvas::kPoints_PointMode, |
| 610 | pointData.fNumPoints, |
| 611 | pointData.fPoints, |
| 612 | newP); |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 613 | } else { |
| 614 | this->drawPoints(SkCanvas::kPoints_PointMode, |
| 615 | pointData.fNumPoints, |
| 616 | pointData.fPoints, |
| 617 | newP, |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 618 | device); |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 619 | } |
| 620 | break; |
| 621 | } else { |
| 622 | // The rest of the dashed line must be drawn as rects |
skia.committer@gmail.com | 7a03d86 | 2012-12-18 02:03:03 +0000 | [diff] [blame] | 623 | SkASSERT(!(SkPathEffect::PointData::kCircles_PointFlag & |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 624 | pointData.fFlags)); |
| 625 | |
| 626 | SkRect r; |
| 627 | |
| 628 | for (int i = 0; i < pointData.fNumPoints; ++i) { |
| 629 | r.set(pointData.fPoints[i].fX - pointData.fSize.fX, |
| 630 | pointData.fPoints[i].fY - pointData.fSize.fY, |
| 631 | pointData.fPoints[i].fX + pointData.fSize.fX, |
| 632 | pointData.fPoints[i].fY + pointData.fSize.fY); |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 633 | if (device) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 634 | device->drawRect(r, newP); |
robertphillips@google.com | 6d87557 | 2012-12-17 18:56:29 +0000 | [diff] [blame] | 635 | } else { |
| 636 | this->drawRect(r, newP); |
| 637 | } |
| 638 | } |
| 639 | } |
| 640 | |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 641 | break; |
| 642 | } |
| 643 | } |
robertphillips@google.com | 629ab54 | 2012-11-28 17:18:11 +0000 | [diff] [blame] | 644 | // couldn't take fast path so fall through! |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 645 | case SkCanvas::kPolygon_PointMode: { |
| 646 | count -= 1; |
| 647 | SkPath path; |
| 648 | SkPaint p(paint); |
| 649 | p.setStyle(SkPaint::kStroke_Style); |
| 650 | size_t inc = (SkCanvas::kLines_PointMode == mode) ? 2 : 1; |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 651 | path.setIsVolatile(true); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 652 | for (size_t i = 0; i < count; i += inc) { |
| 653 | path.moveTo(pts[i]); |
| 654 | path.lineTo(pts[i+1]); |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 655 | if (device) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 656 | device->drawPath(path, p, nullptr, true); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 657 | } else { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 658 | this->drawPath(path, p, nullptr, true); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 659 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 660 | path.rewind(); |
| 661 | } |
| 662 | break; |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | } |
| 667 | |
fmalita | 1a178ca | 2015-01-15 06:01:23 -0800 | [diff] [blame] | 668 | static inline SkPoint compute_stroke_size(const SkPaint& paint, const SkMatrix& matrix) { |
| 669 | SkASSERT(matrix.rectStaysRect()); |
| 670 | SkASSERT(SkPaint::kFill_Style != paint.getStyle()); |
| 671 | |
| 672 | SkVector size; |
| 673 | SkPoint pt = { paint.getStrokeWidth(), paint.getStrokeWidth() }; |
| 674 | matrix.mapVectors(&size, &pt, 1); |
| 675 | return SkPoint::Make(SkScalarAbs(size.fX), SkScalarAbs(size.fY)); |
| 676 | } |
| 677 | |
reed@google.com | 761fb62 | 2011-04-04 18:58:05 +0000 | [diff] [blame] | 678 | static bool easy_rect_join(const SkPaint& paint, const SkMatrix& matrix, |
| 679 | SkPoint* strokeSize) { |
| 680 | if (SkPaint::kMiter_Join != paint.getStrokeJoin() || |
| 681 | paint.getStrokeMiter() < SK_ScalarSqrt2) { |
| 682 | return false; |
| 683 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 684 | |
fmalita | 1a178ca | 2015-01-15 06:01:23 -0800 | [diff] [blame] | 685 | *strokeSize = compute_stroke_size(paint, matrix); |
reed@google.com | 761fb62 | 2011-04-04 18:58:05 +0000 | [diff] [blame] | 686 | return true; |
mike@reedtribe.org | 7ff678b | 2011-04-04 14:38:12 +0000 | [diff] [blame] | 687 | } |
| 688 | |
reed@google.com | 62ab7ad | 2011-04-05 14:08:25 +0000 | [diff] [blame] | 689 | SkDraw::RectType SkDraw::ComputeRectType(const SkPaint& paint, |
| 690 | const SkMatrix& matrix, |
| 691 | SkPoint* strokeSize) { |
| 692 | RectType rtype; |
| 693 | const SkScalar width = paint.getStrokeWidth(); |
| 694 | const bool zeroWidth = (0 == width); |
| 695 | SkPaint::Style style = paint.getStyle(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 696 | |
reed@google.com | 62ab7ad | 2011-04-05 14:08:25 +0000 | [diff] [blame] | 697 | if ((SkPaint::kStrokeAndFill_Style == style) && zeroWidth) { |
| 698 | style = SkPaint::kFill_Style; |
| 699 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 700 | |
reed@google.com | 62ab7ad | 2011-04-05 14:08:25 +0000 | [diff] [blame] | 701 | if (paint.getPathEffect() || paint.getMaskFilter() || |
| 702 | paint.getRasterizer() || !matrix.rectStaysRect() || |
| 703 | SkPaint::kStrokeAndFill_Style == style) { |
| 704 | rtype = kPath_RectType; |
| 705 | } else if (SkPaint::kFill_Style == style) { |
| 706 | rtype = kFill_RectType; |
| 707 | } else if (zeroWidth) { |
| 708 | rtype = kHair_RectType; |
| 709 | } else if (easy_rect_join(paint, matrix, strokeSize)) { |
| 710 | rtype = kStroke_RectType; |
| 711 | } else { |
| 712 | rtype = kPath_RectType; |
| 713 | } |
| 714 | return rtype; |
| 715 | } |
mike@reedtribe.org | 7ff678b | 2011-04-04 14:38:12 +0000 | [diff] [blame] | 716 | |
reed@google.com | 7324415 | 2012-05-11 14:35:23 +0000 | [diff] [blame] | 717 | static const SkPoint* rect_points(const SkRect& r) { |
reed@google.com | fc2f0d0 | 2013-03-29 14:23:56 +0000 | [diff] [blame] | 718 | return SkTCast<const SkPoint*>(&r); |
reed@google.com | 7324415 | 2012-05-11 14:35:23 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | static SkPoint* rect_points(SkRect& r) { |
reed@google.com | fc2f0d0 | 2013-03-29 14:23:56 +0000 | [diff] [blame] | 722 | return SkTCast<SkPoint*>(&r); |
reed@google.com | 40c2ba2 | 2011-07-25 19:41:22 +0000 | [diff] [blame] | 723 | } |
| 724 | |
reed | 0393912 | 2014-12-15 13:42:51 -0800 | [diff] [blame] | 725 | void SkDraw::drawRect(const SkRect& prePaintRect, const SkPaint& paint, |
| 726 | const SkMatrix* paintMatrix, const SkRect* postPaintRect) const { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 727 | SkDEBUGCODE(this->validate();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 728 | |
| 729 | // nothing to draw |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 730 | if (fRC->isEmpty()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 731 | return; |
| 732 | } |
| 733 | |
reed | 0393912 | 2014-12-15 13:42:51 -0800 | [diff] [blame] | 734 | const SkMatrix* matrix; |
| 735 | SkMatrix combinedMatrixStorage; |
| 736 | if (paintMatrix) { |
| 737 | SkASSERT(postPaintRect); |
| 738 | combinedMatrixStorage.setConcat(*fMatrix, *paintMatrix); |
| 739 | matrix = &combinedMatrixStorage; |
| 740 | } else { |
| 741 | SkASSERT(!postPaintRect); |
| 742 | matrix = fMatrix; |
| 743 | } |
| 744 | |
reed@google.com | 761fb62 | 2011-04-04 18:58:05 +0000 | [diff] [blame] | 745 | SkPoint strokeSize; |
reed@google.com | 62ab7ad | 2011-04-05 14:08:25 +0000 | [diff] [blame] | 746 | RectType rtype = ComputeRectType(paint, *fMatrix, &strokeSize); |
mike@reedtribe.org | 7ff678b | 2011-04-04 14:38:12 +0000 | [diff] [blame] | 747 | |
mike@reedtribe.org | 7ff678b | 2011-04-04 14:38:12 +0000 | [diff] [blame] | 748 | if (kPath_RectType == rtype) { |
reed | 0393912 | 2014-12-15 13:42:51 -0800 | [diff] [blame] | 749 | SkDraw draw(*this); |
| 750 | if (paintMatrix) { |
| 751 | draw.fMatrix = matrix; |
| 752 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 753 | SkPath tmp; |
reed | 0393912 | 2014-12-15 13:42:51 -0800 | [diff] [blame] | 754 | tmp.addRect(prePaintRect); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 755 | tmp.setFillType(SkPath::kWinding_FillType); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 756 | draw.drawPath(tmp, paint, nullptr, true); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 757 | return; |
| 758 | } |
| 759 | |
reed | 0393912 | 2014-12-15 13:42:51 -0800 | [diff] [blame] | 760 | SkRect devRect; |
fmalita | 1a178ca | 2015-01-15 06:01:23 -0800 | [diff] [blame] | 761 | const SkRect& paintRect = paintMatrix ? *postPaintRect : prePaintRect; |
| 762 | // skip the paintMatrix when transforming the rect by the CTM |
| 763 | fMatrix->mapPoints(rect_points(devRect), rect_points(paintRect), 2); |
reed@google.com | 7324415 | 2012-05-11 14:35:23 +0000 | [diff] [blame] | 764 | devRect.sort(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 765 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 766 | // look for the quick exit, before we build a blitter |
fmalita | 1a178ca | 2015-01-15 06:01:23 -0800 | [diff] [blame] | 767 | SkRect bbox = devRect; |
reed@google.com | 1c028bd | 2013-08-28 15:23:19 +0000 | [diff] [blame] | 768 | if (paint.getStyle() != SkPaint::kFill_Style) { |
| 769 | // extra space for hairlines |
george | b3eba47 | 2014-09-09 11:33:57 -0700 | [diff] [blame] | 770 | if (paint.getStrokeWidth() == 0) { |
fmalita | 1a178ca | 2015-01-15 06:01:23 -0800 | [diff] [blame] | 771 | bbox.outset(1, 1); |
george | b3eba47 | 2014-09-09 11:33:57 -0700 | [diff] [blame] | 772 | } else { |
fmalita | 1a178ca | 2015-01-15 06:01:23 -0800 | [diff] [blame] | 773 | // For kStroke_RectType, strokeSize is already computed. |
| 774 | const SkPoint& ssize = (kStroke_RectType == rtype) |
| 775 | ? strokeSize |
| 776 | : compute_stroke_size(paint, *fMatrix); |
| 777 | bbox.outset(SkScalarHalf(ssize.x()), SkScalarHalf(ssize.y())); |
george | b3eba47 | 2014-09-09 11:33:57 -0700 | [diff] [blame] | 778 | } |
reed@google.com | 1c028bd | 2013-08-28 15:23:19 +0000 | [diff] [blame] | 779 | } |
fmalita | 1a178ca | 2015-01-15 06:01:23 -0800 | [diff] [blame] | 780 | |
| 781 | SkIRect ir = bbox.roundOut(); |
reed@google.com | 1c028bd | 2013-08-28 15:23:19 +0000 | [diff] [blame] | 782 | if (fRC->quickReject(ir)) { |
| 783 | return; |
rmistry@google.com | e09d6f4 | 2013-08-27 18:53:41 +0000 | [diff] [blame] | 784 | } |
| 785 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 786 | SkDeviceLooper looper(fDst, *fRC, ir, paint.isAntiAlias()); |
reed@google.com | 1c028bd | 2013-08-28 15:23:19 +0000 | [diff] [blame] | 787 | while (looper.next()) { |
| 788 | SkRect localDevRect; |
| 789 | looper.mapRect(&localDevRect, devRect); |
| 790 | SkMatrix localMatrix; |
reed | 0393912 | 2014-12-15 13:42:51 -0800 | [diff] [blame] | 791 | looper.mapMatrix(&localMatrix, *matrix); |
rmistry@google.com | e09d6f4 | 2013-08-27 18:53:41 +0000 | [diff] [blame] | 792 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 793 | SkAutoBlitterChoose blitterStorage(looper.getPixmap(), localMatrix, paint); |
reed@google.com | 1c028bd | 2013-08-28 15:23:19 +0000 | [diff] [blame] | 794 | const SkRasterClip& clip = looper.getRC(); |
| 795 | SkBlitter* blitter = blitterStorage.get(); |
| 796 | |
| 797 | // we want to "fill" if we are kFill or kStrokeAndFill, since in the latter |
| 798 | // case we are also hairline (if we've gotten to here), which devolves to |
| 799 | // effectively just kFill |
| 800 | switch (rtype) { |
| 801 | case kFill_RectType: |
| 802 | if (paint.isAntiAlias()) { |
| 803 | SkScan::AntiFillRect(localDevRect, clip, blitter); |
| 804 | } else { |
| 805 | SkScan::FillRect(localDevRect, clip, blitter); |
| 806 | } |
| 807 | break; |
| 808 | case kStroke_RectType: |
| 809 | if (paint.isAntiAlias()) { |
| 810 | SkScan::AntiFrameRect(localDevRect, strokeSize, clip, blitter); |
| 811 | } else { |
| 812 | SkScan::FrameRect(localDevRect, strokeSize, clip, blitter); |
| 813 | } |
| 814 | break; |
| 815 | case kHair_RectType: |
| 816 | if (paint.isAntiAlias()) { |
| 817 | SkScan::AntiHairRect(localDevRect, clip, blitter); |
| 818 | } else { |
| 819 | SkScan::HairRect(localDevRect, clip, blitter); |
| 820 | } |
| 821 | break; |
| 822 | default: |
| 823 | SkDEBUGFAIL("bad rtype"); |
| 824 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 825 | } |
| 826 | } |
| 827 | |
| 828 | void SkDraw::drawDevMask(const SkMask& srcM, const SkPaint& paint) const { |
| 829 | if (srcM.fBounds.isEmpty()) { |
| 830 | return; |
| 831 | } |
| 832 | |
bungeman@google.com | 0a60b3d | 2011-09-26 19:09:08 +0000 | [diff] [blame] | 833 | const SkMask* mask = &srcM; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 834 | |
bungeman@google.com | 0a60b3d | 2011-09-26 19:09:08 +0000 | [diff] [blame] | 835 | SkMask dstM; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 836 | if (paint.getMaskFilter() && |
robertphillips | e80eb92 | 2015-12-17 11:33:12 -0800 | [diff] [blame] | 837 | paint.getMaskFilter()->filterMask(&dstM, srcM, *fMatrix, nullptr)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 838 | mask = &dstM; |
| 839 | } |
bungeman@google.com | 02f5584 | 2011-10-04 21:25:00 +0000 | [diff] [blame] | 840 | SkAutoMaskFreeImage ami(dstM.fImage); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 841 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 842 | SkAutoBlitterChoose blitterChooser(fDst, *fMatrix, paint); |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 843 | SkBlitter* blitter = blitterChooser.get(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 844 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 845 | SkAAClipBlitterWrapper wrapper; |
| 846 | const SkRegion* clipRgn; |
| 847 | |
| 848 | if (fRC->isBW()) { |
| 849 | clipRgn = &fRC->bwRgn(); |
| 850 | } else { |
| 851 | wrapper.init(*fRC, blitter); |
| 852 | clipRgn = &wrapper.getRgn(); |
| 853 | blitter = wrapper.getBlitter(); |
| 854 | } |
| 855 | blitter->blitMaskRegion(*mask, *clipRgn); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 856 | } |
| 857 | |
reed@android.com | ebdeeb8 | 2009-09-03 21:45:49 +0000 | [diff] [blame] | 858 | static SkScalar fast_len(const SkVector& vec) { |
| 859 | SkScalar x = SkScalarAbs(vec.fX); |
| 860 | SkScalar y = SkScalarAbs(vec.fY); |
| 861 | if (x < y) { |
| 862 | SkTSwap(x, y); |
| 863 | } |
| 864 | return x + SkScalarHalf(y); |
| 865 | } |
| 866 | |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 867 | bool SkDrawTreatAAStrokeAsHairline(SkScalar strokeWidth, const SkMatrix& matrix, |
| 868 | SkScalar* coverage) { |
| 869 | SkASSERT(strokeWidth > 0); |
| 870 | // We need to try to fake a thick-stroke with a modulated hairline. |
reed@google.com | ecadf99 | 2011-09-19 19:18:18 +0000 | [diff] [blame] | 871 | |
tomhudson@google.com | 8d43018 | 2011-06-06 19:11:19 +0000 | [diff] [blame] | 872 | if (matrix.hasPerspective()) { |
reed@android.com | ebdeeb8 | 2009-09-03 21:45:49 +0000 | [diff] [blame] | 873 | return false; |
| 874 | } |
reed@google.com | ecadf99 | 2011-09-19 19:18:18 +0000 | [diff] [blame] | 875 | |
reed@android.com | ebdeeb8 | 2009-09-03 21:45:49 +0000 | [diff] [blame] | 876 | SkVector src[2], dst[2]; |
reed@google.com | ecadf99 | 2011-09-19 19:18:18 +0000 | [diff] [blame] | 877 | src[0].set(strokeWidth, 0); |
| 878 | src[1].set(0, strokeWidth); |
reed@android.com | ebdeeb8 | 2009-09-03 21:45:49 +0000 | [diff] [blame] | 879 | matrix.mapVectors(dst, src, 2); |
| 880 | SkScalar len0 = fast_len(dst[0]); |
| 881 | SkScalar len1 = fast_len(dst[1]); |
agl@chromium.org | 652807b | 2010-04-27 15:47:34 +0000 | [diff] [blame] | 882 | if (len0 <= SK_Scalar1 && len1 <= SK_Scalar1) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 883 | if (coverage) { |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 884 | *coverage = SkScalarAve(len0, len1); |
| 885 | } |
reed@android.com | ebdeeb8 | 2009-09-03 21:45:49 +0000 | [diff] [blame] | 886 | return true; |
| 887 | } |
| 888 | return false; |
| 889 | } |
| 890 | |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 891 | void SkDraw::drawRRect(const SkRRect& rrect, const SkPaint& paint) const { |
| 892 | SkDEBUGCODE(this->validate()); |
| 893 | |
| 894 | if (fRC->isEmpty()) { |
| 895 | return; |
| 896 | } |
| 897 | |
| 898 | { |
| 899 | // TODO: Investigate optimizing these options. They are in the same |
| 900 | // order as SkDraw::drawPath, which handles each case. It may be |
| 901 | // that there is no way to optimize for these using the SkRRect path. |
| 902 | SkScalar coverage; |
| 903 | if (SkDrawTreatAsHairline(paint, *fMatrix, &coverage)) { |
| 904 | goto DRAW_PATH; |
| 905 | } |
| 906 | |
| 907 | if (paint.getPathEffect() || paint.getStyle() != SkPaint::kFill_Style) { |
| 908 | goto DRAW_PATH; |
| 909 | } |
| 910 | |
| 911 | if (paint.getRasterizer()) { |
| 912 | goto DRAW_PATH; |
| 913 | } |
| 914 | } |
| 915 | |
| 916 | if (paint.getMaskFilter()) { |
| 917 | // Transform the rrect into device space. |
| 918 | SkRRect devRRect; |
| 919 | if (rrect.transform(*fMatrix, &devRRect)) { |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 920 | SkAutoBlitterChoose blitter(fDst, *fMatrix, paint); |
bsalomon | 055e192 | 2016-05-06 07:22:58 -0700 | [diff] [blame] | 921 | if (paint.getMaskFilter()->filterRRect(devRRect, *fMatrix, *fRC, blitter.get())) { |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 922 | return; // filterRRect() called the blitter, so we're done |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | DRAW_PATH: |
| 928 | // Now fall back to the default case of using a path. |
| 929 | SkPath path; |
| 930 | path.addRRect(rrect); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 931 | this->drawPath(path, paint, nullptr, true); |
scroggo@google.com | a8e33a9 | 2013-11-08 18:02:53 +0000 | [diff] [blame] | 932 | } |
| 933 | |
caryclark | 1a7eb26 | 2016-01-21 07:07:02 -0800 | [diff] [blame] | 934 | SkScalar SkDraw::ComputeResScaleForStroking(const SkMatrix& matrix) { |
reed | 05d9044 | 2015-02-12 13:35:52 -0800 | [diff] [blame] | 935 | if (!matrix.hasPerspective()) { |
| 936 | SkScalar sx = SkPoint::Length(matrix[SkMatrix::kMScaleX], matrix[SkMatrix::kMSkewY]); |
| 937 | SkScalar sy = SkPoint::Length(matrix[SkMatrix::kMSkewX], matrix[SkMatrix::kMScaleY]); |
| 938 | if (SkScalarsAreFinite(sx, sy)) { |
lsalzman | a4b8704 | 2016-07-06 12:19:56 -0700 | [diff] [blame] | 939 | SkScalar scale = SkTMax(sx, sy); |
| 940 | if (scale > 0) { |
| 941 | return scale; |
| 942 | } |
reed | 05d9044 | 2015-02-12 13:35:52 -0800 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | return 1; |
| 946 | } |
| 947 | |
reed | 82595b6 | 2016-05-09 17:48:46 -0700 | [diff] [blame] | 948 | void SkDraw::drawDevPath(const SkPath& devPath, const SkPaint& paint, bool drawCoverage, |
| 949 | SkBlitter* customBlitter, bool doFill) const { |
reed | 01a2ff8 | 2016-07-18 13:22:55 -0700 | [diff] [blame] | 950 | // Do a conservative quick-reject test, since a looper or other modifier may have moved us |
| 951 | // out of range. |
| 952 | if (!devPath.isInverseFillType()) { |
| 953 | // If we're a H or V line, our bounds will be empty. So we bloat here just so we don't |
| 954 | // appear empty to the intersects call. This also gives us slop in case we're antialiasing |
| 955 | SkRect pathBounds = devPath.getBounds().makeOutset(1, 1); |
| 956 | |
| 957 | if (paint.getMaskFilter()) { |
| 958 | paint.getMaskFilter()->computeFastBounds(pathBounds, &pathBounds); |
| 959 | |
| 960 | // Need to outset the path to work-around a bug in blurmaskfilter. When that is fixed |
| 961 | // we can remove this hack. See skbug.com/5542 |
| 962 | pathBounds.outset(7, 7); |
| 963 | } |
| 964 | |
| 965 | // Now compare against the clip's bounds |
| 966 | if (!SkRect::Make(fRC->getBounds()).intersects(pathBounds)) { |
| 967 | return; |
| 968 | } |
| 969 | } |
| 970 | |
reed | 82595b6 | 2016-05-09 17:48:46 -0700 | [diff] [blame] | 971 | SkBlitter* blitter = nullptr; |
| 972 | SkAutoBlitterChoose blitterStorage; |
| 973 | if (nullptr == customBlitter) { |
| 974 | blitterStorage.choose(fDst, *fMatrix, paint, drawCoverage); |
| 975 | blitter = blitterStorage.get(); |
| 976 | } else { |
| 977 | blitter = customBlitter; |
| 978 | } |
| 979 | |
| 980 | if (paint.getMaskFilter()) { |
| 981 | SkStrokeRec::InitStyle style = doFill ? SkStrokeRec::kFill_InitStyle |
| 982 | : SkStrokeRec::kHairline_InitStyle; |
| 983 | if (paint.getMaskFilter()->filterPath(devPath, *fMatrix, *fRC, blitter, style)) { |
| 984 | return; // filterPath() called the blitter, so we're done |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | void (*proc)(const SkPath&, const SkRasterClip&, SkBlitter*); |
| 989 | if (doFill) { |
| 990 | if (paint.isAntiAlias()) { |
| 991 | proc = SkScan::AntiFillPath; |
| 992 | } else { |
| 993 | proc = SkScan::FillPath; |
| 994 | } |
| 995 | } else { // hairline |
| 996 | if (paint.isAntiAlias()) { |
| 997 | switch (paint.getStrokeCap()) { |
| 998 | case SkPaint::kButt_Cap: |
| 999 | proc = SkScan::AntiHairPath; |
| 1000 | break; |
| 1001 | case SkPaint::kSquare_Cap: |
| 1002 | proc = SkScan::AntiHairSquarePath; |
| 1003 | break; |
| 1004 | case SkPaint::kRound_Cap: |
| 1005 | proc = SkScan::AntiHairRoundPath; |
| 1006 | break; |
| 1007 | default: |
| 1008 | proc SK_INIT_TO_AVOID_WARNING; |
| 1009 | SkDEBUGFAIL("unknown paint cap type"); |
| 1010 | } |
| 1011 | } else { |
| 1012 | switch (paint.getStrokeCap()) { |
| 1013 | case SkPaint::kButt_Cap: |
| 1014 | proc = SkScan::HairPath; |
| 1015 | break; |
| 1016 | case SkPaint::kSquare_Cap: |
| 1017 | proc = SkScan::HairSquarePath; |
| 1018 | break; |
| 1019 | case SkPaint::kRound_Cap: |
| 1020 | proc = SkScan::HairRoundPath; |
| 1021 | break; |
| 1022 | default: |
| 1023 | proc SK_INIT_TO_AVOID_WARNING; |
| 1024 | SkDEBUGFAIL("unknown paint cap type"); |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | proc(devPath, *fRC, blitter); |
| 1029 | } |
| 1030 | |
reed@google.com | 32e5d97 | 2011-07-27 18:25:57 +0000 | [diff] [blame] | 1031 | void SkDraw::drawPath(const SkPath& origSrcPath, const SkPaint& origPaint, |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 1032 | const SkMatrix* prePathMatrix, bool pathIsMutable, |
krajcevski | 53f0959 | 2014-08-06 11:12:14 -0700 | [diff] [blame] | 1033 | bool drawCoverage, SkBlitter* customBlitter) const { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1034 | SkDEBUGCODE(this->validate();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1035 | |
| 1036 | // nothing to draw |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 1037 | if (fRC->isEmpty()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1038 | return; |
| 1039 | } |
| 1040 | |
| 1041 | SkPath* pathPtr = (SkPath*)&origSrcPath; |
| 1042 | bool doFill = true; |
| 1043 | SkPath tmpPath; |
| 1044 | SkMatrix tmpMatrix; |
| 1045 | const SkMatrix* matrix = fMatrix; |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 1046 | tmpPath.setIsVolatile(true); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1047 | |
| 1048 | if (prePathMatrix) { |
reed@google.com | 32e5d97 | 2011-07-27 18:25:57 +0000 | [diff] [blame] | 1049 | if (origPaint.getPathEffect() || origPaint.getStyle() != SkPaint::kFill_Style || |
| 1050 | origPaint.getRasterizer()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1051 | SkPath* result = pathPtr; |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 1052 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1053 | if (!pathIsMutable) { |
| 1054 | result = &tmpPath; |
| 1055 | pathIsMutable = true; |
| 1056 | } |
| 1057 | pathPtr->transform(*prePathMatrix, result); |
| 1058 | pathPtr = result; |
| 1059 | } else { |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1060 | tmpMatrix.setConcat(*matrix, *prePathMatrix); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1061 | matrix = &tmpMatrix; |
| 1062 | } |
| 1063 | } |
| 1064 | // at this point we're done with prePathMatrix |
| 1065 | SkDEBUGCODE(prePathMatrix = (const SkMatrix*)0x50FF8001;) |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 1066 | |
bsalomon@google.com | 5dc26b9 | 2012-10-11 19:32:32 +0000 | [diff] [blame] | 1067 | SkTCopyOnFirstWrite<SkPaint> paint(origPaint); |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 1068 | |
reed@google.com | ecadf99 | 2011-09-19 19:18:18 +0000 | [diff] [blame] | 1069 | { |
bsalomon@google.com | dd1be60 | 2012-01-18 20:34:00 +0000 | [diff] [blame] | 1070 | SkScalar coverage; |
| 1071 | if (SkDrawTreatAsHairline(origPaint, *matrix, &coverage)) { |
| 1072 | if (SK_Scalar1 == coverage) { |
bsalomon@google.com | 5dc26b9 | 2012-10-11 19:32:32 +0000 | [diff] [blame] | 1073 | paint.writable()->setStrokeWidth(0); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 1074 | } else if (SkBlendMode_SupportsCoverageAsAlpha(origPaint.getBlendMode())) { |
bsalomon@google.com | dd1be60 | 2012-01-18 20:34:00 +0000 | [diff] [blame] | 1075 | U8CPU newAlpha; |
| 1076 | #if 0 |
| 1077 | newAlpha = SkToU8(SkScalarRoundToInt(coverage * |
| 1078 | origPaint.getAlpha())); |
| 1079 | #else |
| 1080 | // this is the old technique, which we preserve for now so |
| 1081 | // we don't change previous results (testing) |
| 1082 | // the new way seems fine, its just (a tiny bit) different |
Mike Reed | a99b6ce | 2017-02-04 11:04:26 -0500 | [diff] [blame] | 1083 | int scale = (int)(coverage * 256); |
bsalomon@google.com | dd1be60 | 2012-01-18 20:34:00 +0000 | [diff] [blame] | 1084 | newAlpha = origPaint.getAlpha() * scale >> 8; |
| 1085 | #endif |
bsalomon@google.com | 5dc26b9 | 2012-10-11 19:32:32 +0000 | [diff] [blame] | 1086 | SkPaint* writablePaint = paint.writable(); |
| 1087 | writablePaint->setStrokeWidth(0); |
| 1088 | writablePaint->setAlpha(newAlpha); |
bsalomon@google.com | dd1be60 | 2012-01-18 20:34:00 +0000 | [diff] [blame] | 1089 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1090 | } |
| 1091 | } |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 1092 | |
reed@google.com | 32e5d97 | 2011-07-27 18:25:57 +0000 | [diff] [blame] | 1093 | if (paint->getPathEffect() || paint->getStyle() != SkPaint::kFill_Style) { |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 1094 | SkRect cullRect; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1095 | const SkRect* cullRectPtr = nullptr; |
reed@google.com | 4bbdeac | 2013-01-24 21:03:11 +0000 | [diff] [blame] | 1096 | if (this->computeConservativeLocalClipBounds(&cullRect)) { |
| 1097 | cullRectPtr = &cullRect; |
| 1098 | } |
reed | 05d9044 | 2015-02-12 13:35:52 -0800 | [diff] [blame] | 1099 | doFill = paint->getFillPath(*pathPtr, &tmpPath, cullRectPtr, |
caryclark | 1a7eb26 | 2016-01-21 07:07:02 -0800 | [diff] [blame] | 1100 | ComputeResScaleForStroking(*fMatrix)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1101 | pathPtr = &tmpPath; |
| 1102 | } |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 1103 | |
reed@google.com | 32e5d97 | 2011-07-27 18:25:57 +0000 | [diff] [blame] | 1104 | if (paint->getRasterizer()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1105 | SkMask mask; |
reed@google.com | 32e5d97 | 2011-07-27 18:25:57 +0000 | [diff] [blame] | 1106 | if (paint->getRasterizer()->rasterize(*pathPtr, *matrix, |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 1107 | &fRC->getBounds(), paint->getMaskFilter(), &mask, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1108 | SkMask::kComputeBoundsAndRenderImage_CreateMode)) { |
reed@google.com | 32e5d97 | 2011-07-27 18:25:57 +0000 | [diff] [blame] | 1109 | this->drawDevMask(mask, *paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1110 | SkMask::FreeImage(mask.fImage); |
| 1111 | } |
| 1112 | return; |
| 1113 | } |
| 1114 | |
| 1115 | // avoid possibly allocating a new path in transform if we can |
| 1116 | SkPath* devPathPtr = pathIsMutable ? pathPtr : &tmpPath; |
| 1117 | |
| 1118 | // transform the path into device space |
| 1119 | pathPtr->transform(*matrix, devPathPtr); |
| 1120 | |
reed | 82595b6 | 2016-05-09 17:48:46 -0700 | [diff] [blame] | 1121 | this->drawDevPath(*devPathPtr, *paint, drawCoverage, customBlitter, doFill); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
reed | 82595b6 | 2016-05-09 17:48:46 -0700 | [diff] [blame] | 1124 | void SkDraw::drawBitmapAsMask(const SkBitmap& bitmap, const SkPaint& paint) const { |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 1125 | SkASSERT(bitmap.colorType() == kAlpha_8_SkColorType); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1126 | |
fmalita | c7e211a | 2016-01-07 10:34:46 -0800 | [diff] [blame] | 1127 | if (SkTreatAsSprite(*fMatrix, bitmap.dimensions(), paint)) { |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 1128 | int ix = SkScalarRoundToInt(fMatrix->getTranslateX()); |
| 1129 | int iy = SkScalarRoundToInt(fMatrix->getTranslateY()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1130 | |
Mike Reed | 4edb5d2 | 2017-04-17 11:02:51 -0400 | [diff] [blame] | 1131 | SkPixmap pmap; |
| 1132 | if (!bitmap.peekPixels(&pmap)) { |
reed@google.com | a641f3f | 2012-12-13 22:16:30 +0000 | [diff] [blame] | 1133 | return; |
| 1134 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1135 | SkMask mask; |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 1136 | mask.fBounds.set(ix, iy, ix + pmap.width(), iy + pmap.height()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1137 | mask.fFormat = SkMask::kA8_Format; |
reed | 92fc2ae | 2015-05-22 08:06:21 -0700 | [diff] [blame] | 1138 | mask.fRowBytes = SkToU32(pmap.rowBytes()); |
| 1139 | // fImage is typed as writable, but in this case it is used read-only |
| 1140 | mask.fImage = (uint8_t*)pmap.addr8(0, 0); |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 1141 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1142 | this->drawDevMask(mask, paint); |
| 1143 | } else { // need to xform the bitmap first |
| 1144 | SkRect r; |
| 1145 | SkMask mask; |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 1146 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1147 | r.set(0, 0, |
| 1148 | SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height())); |
| 1149 | fMatrix->mapRect(&r); |
| 1150 | r.round(&mask.fBounds); |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 1151 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1152 | // set the mask's bounds to the transformed bitmap-bounds, |
| 1153 | // clipped to the actual device |
| 1154 | { |
| 1155 | SkIRect devBounds; |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 1156 | devBounds.set(0, 0, fDst.width(), fDst.height()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1157 | // need intersect(l, t, r, b) on irect |
| 1158 | if (!mask.fBounds.intersect(devBounds)) { |
| 1159 | return; |
| 1160 | } |
| 1161 | } |
reed@android.com | 543ed93 | 2009-04-24 12:43:40 +0000 | [diff] [blame] | 1162 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1163 | mask.fFormat = SkMask::kA8_Format; |
| 1164 | mask.fRowBytes = SkAlign4(mask.fBounds.width()); |
reed@android.com | 543ed93 | 2009-04-24 12:43:40 +0000 | [diff] [blame] | 1165 | size_t size = mask.computeImageSize(); |
| 1166 | if (0 == size) { |
| 1167 | // the mask is too big to allocated, draw nothing |
| 1168 | return; |
| 1169 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1170 | |
| 1171 | // allocate (and clear) our temp buffer to hold the transformed bitmap |
scroggo | 565901d | 2015-12-10 10:44:13 -0800 | [diff] [blame] | 1172 | SkAutoTMalloc<uint8_t> storage(size); |
| 1173 | mask.fImage = storage.get(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1174 | memset(mask.fImage, 0, size); |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 1175 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1176 | // now draw our bitmap(src) into mask(dst), transformed by the matrix |
| 1177 | { |
| 1178 | SkBitmap device; |
commit-bot@chromium.org | a3264e5 | 2014-05-30 13:26:10 +0000 | [diff] [blame] | 1179 | device.installPixels(SkImageInfo::MakeA8(mask.fBounds.width(), mask.fBounds.height()), |
| 1180 | mask.fImage, mask.fRowBytes); |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 1181 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1182 | SkCanvas c(device); |
| 1183 | // need the unclipped top/left for the translate |
| 1184 | c.translate(-SkIntToScalar(mask.fBounds.fLeft), |
| 1185 | -SkIntToScalar(mask.fBounds.fTop)); |
| 1186 | c.concat(*fMatrix); |
reed@android.com | 3469c76 | 2009-02-24 19:03:20 +0000 | [diff] [blame] | 1187 | |
| 1188 | // We can't call drawBitmap, or we'll infinitely recurse. Instead |
reed@android.com | fb12c3e | 2009-03-05 20:43:42 +0000 | [diff] [blame] | 1189 | // we manually build a shader and draw that into our new mask |
reed@android.com | 3469c76 | 2009-02-24 19:03:20 +0000 | [diff] [blame] | 1190 | SkPaint tmpPaint; |
| 1191 | tmpPaint.setFlags(paint.getFlags()); |
brianosman | 66a96d0 | 2016-05-19 08:43:55 -0700 | [diff] [blame] | 1192 | tmpPaint.setFilterQuality(paint.getFilterQuality()); |
Herb Derby | 4bf560a | 2017-02-14 11:13:26 -0500 | [diff] [blame] | 1193 | SkPaint paintWithShader = make_paint_with_image(tmpPaint, bitmap); |
reed@android.com | 3469c76 | 2009-02-24 19:03:20 +0000 | [diff] [blame] | 1194 | SkRect rr; |
| 1195 | rr.set(0, 0, SkIntToScalar(bitmap.width()), |
| 1196 | SkIntToScalar(bitmap.height())); |
Herb Derby | 4bf560a | 2017-02-14 11:13:26 -0500 | [diff] [blame] | 1197 | c.drawRect(rr, paintWithShader); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1198 | } |
| 1199 | this->drawDevMask(mask, paint); |
| 1200 | } |
| 1201 | } |
| 1202 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 1203 | static bool clipped_out(const SkMatrix& m, const SkRasterClip& c, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1204 | const SkRect& srcR) { |
| 1205 | SkRect dstR; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1206 | m.mapRect(&dstR, srcR); |
reed | b07a94f | 2014-11-19 05:03:18 -0800 | [diff] [blame] | 1207 | return c.quickReject(dstR.roundOut()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1208 | } |
| 1209 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 1210 | static bool clipped_out(const SkMatrix& matrix, const SkRasterClip& clip, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1211 | int width, int height) { |
| 1212 | SkRect r; |
| 1213 | r.set(0, 0, SkIntToScalar(width), SkIntToScalar(height)); |
| 1214 | return clipped_out(matrix, clip, r); |
| 1215 | } |
| 1216 | |
reed | c240e71 | 2015-05-23 12:26:41 -0700 | [diff] [blame] | 1217 | static bool clipHandlesSprite(const SkRasterClip& clip, int x, int y, const SkPixmap& pmap) { |
| 1218 | return clip.isBW() || clip.quickContains(x, y, x + pmap.width(), y + pmap.height()); |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 1219 | } |
| 1220 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1221 | void SkDraw::drawBitmap(const SkBitmap& bitmap, const SkMatrix& prematrix, |
reed | 0393912 | 2014-12-15 13:42:51 -0800 | [diff] [blame] | 1222 | const SkRect* dstBounds, const SkPaint& origPaint) const { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1223 | SkDEBUGCODE(this->validate();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1224 | |
| 1225 | // nothing to draw |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 1226 | if (fRC->isEmpty() || |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1227 | bitmap.width() == 0 || bitmap.height() == 0 || |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 1228 | bitmap.colorType() == kUnknown_SkColorType) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1229 | return; |
| 1230 | } |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 1231 | |
fmalita | f85d2a4 | 2016-10-03 13:46:58 -0700 | [diff] [blame] | 1232 | SkTCopyOnFirstWrite<SkPaint> paint(origPaint); |
| 1233 | if (origPaint.getStyle() != SkPaint::kFill_Style) { |
| 1234 | paint.writable()->setStyle(SkPaint::kFill_Style); |
| 1235 | } |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 1236 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1237 | SkMatrix matrix; |
commit-bot@chromium.org | 9236238 | 2014-03-18 12:51:48 +0000 | [diff] [blame] | 1238 | matrix.setConcat(*fMatrix, prematrix); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1239 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 1240 | if (clipped_out(matrix, *fRC, bitmap.width(), bitmap.height())) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1241 | return; |
| 1242 | } |
| 1243 | |
fmalita | c7e211a | 2016-01-07 10:34:46 -0800 | [diff] [blame] | 1244 | if (bitmap.colorType() != kAlpha_8_SkColorType |
fmalita | f85d2a4 | 2016-10-03 13:46:58 -0700 | [diff] [blame] | 1245 | && SkTreatAsSprite(matrix, bitmap.dimensions(), *paint)) { |
reed@google.com | f7ef56d | 2012-12-14 13:46:53 +0000 | [diff] [blame] | 1246 | // |
| 1247 | // It is safe to call lock pixels now, since we know the matrix is |
| 1248 | // (more or less) identity. |
| 1249 | // |
Mike Reed | 4edb5d2 | 2017-04-17 11:02:51 -0400 | [diff] [blame] | 1250 | SkPixmap pmap; |
| 1251 | if (!bitmap.peekPixels(&pmap)) { |
reed@google.com | f7ef56d | 2012-12-14 13:46:53 +0000 | [diff] [blame] | 1252 | return; |
| 1253 | } |
reed@google.com | e1ca705 | 2013-12-17 19:22:07 +0000 | [diff] [blame] | 1254 | int ix = SkScalarRoundToInt(matrix.getTranslateX()); |
| 1255 | int iy = SkScalarRoundToInt(matrix.getTranslateY()); |
reed | c240e71 | 2015-05-23 12:26:41 -0700 | [diff] [blame] | 1256 | if (clipHandlesSprite(*fRC, ix, iy, pmap)) { |
Florin Malita | 14a6430 | 2017-05-24 14:53:44 -0400 | [diff] [blame] | 1257 | SkSTArenaAlloc<kSkBlitterContextSize> allocator; |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 1258 | // blitter will be owned by the allocator. |
fmalita | f85d2a4 | 2016-10-03 13:46:58 -0700 | [diff] [blame] | 1259 | SkBlitter* blitter = SkBlitter::ChooseSprite(fDst, *paint, pmap, ix, iy, &allocator); |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 1260 | if (blitter) { |
reed | c240e71 | 2015-05-23 12:26:41 -0700 | [diff] [blame] | 1261 | SkScan::FillIRect(SkIRect::MakeXYWH(ix, iy, pmap.width(), pmap.height()), |
| 1262 | *fRC, blitter); |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 1263 | return; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1264 | } |
reed | c240e71 | 2015-05-23 12:26:41 -0700 | [diff] [blame] | 1265 | // if !blitter, then we fall-through to the slower case |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1266 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1267 | } |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 1268 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1269 | // now make a temp draw on the stack, and use it |
| 1270 | // |
| 1271 | SkDraw draw(*this); |
| 1272 | draw.fMatrix = &matrix; |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 1273 | |
Matt Sarett | 70ac8a9 | 2016-11-03 10:55:08 -0400 | [diff] [blame] | 1274 | if (bitmap.colorType() == kAlpha_8_SkColorType && !paint->getColorFilter()) { |
fmalita | f85d2a4 | 2016-10-03 13:46:58 -0700 | [diff] [blame] | 1275 | draw.drawBitmapAsMask(bitmap, *paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1276 | } else { |
Herb Derby | 4bf560a | 2017-02-14 11:13:26 -0500 | [diff] [blame] | 1277 | SkPaint paintWithShader = make_paint_with_image(*paint, bitmap); |
reed | 0393912 | 2014-12-15 13:42:51 -0800 | [diff] [blame] | 1278 | const SkRect srcBounds = SkRect::MakeIWH(bitmap.width(), bitmap.height()); |
| 1279 | if (dstBounds) { |
| 1280 | this->drawRect(srcBounds, paintWithShader, &prematrix, dstBounds); |
| 1281 | } else { |
| 1282 | draw.drawRect(srcBounds, paintWithShader); |
| 1283 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1284 | } |
| 1285 | } |
| 1286 | |
reed | c240e71 | 2015-05-23 12:26:41 -0700 | [diff] [blame] | 1287 | void SkDraw::drawSprite(const SkBitmap& bitmap, int x, int y, const SkPaint& origPaint) const { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1288 | SkDEBUGCODE(this->validate();) |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 1289 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1290 | // nothing to draw |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 1291 | if (fRC->isEmpty() || |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1292 | bitmap.width() == 0 || bitmap.height() == 0 || |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 1293 | bitmap.colorType() == kUnknown_SkColorType) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1294 | return; |
| 1295 | } |
| 1296 | |
reed | c240e71 | 2015-05-23 12:26:41 -0700 | [diff] [blame] | 1297 | const SkIRect bounds = SkIRect::MakeXYWH(x, y, bitmap.width(), bitmap.height()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1298 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 1299 | if (fRC->quickReject(bounds)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1300 | return; // nothing to draw |
| 1301 | } |
| 1302 | |
reed@google.com | 40c2ba2 | 2011-07-25 19:41:22 +0000 | [diff] [blame] | 1303 | SkPaint paint(origPaint); |
| 1304 | paint.setStyle(SkPaint::kFill_Style); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1305 | |
Mike Reed | 4edb5d2 | 2017-04-17 11:02:51 -0400 | [diff] [blame] | 1306 | SkPixmap pmap; |
| 1307 | if (!bitmap.peekPixels(&pmap)) { |
reed | c240e71 | 2015-05-23 12:26:41 -0700 | [diff] [blame] | 1308 | return; |
| 1309 | } |
reed | c240e71 | 2015-05-23 12:26:41 -0700 | [diff] [blame] | 1310 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1311 | if (nullptr == paint.getColorFilter() && clipHandlesSprite(*fRC, x, y, pmap)) { |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 1312 | // blitter will be owned by the allocator. |
Florin Malita | 14a6430 | 2017-05-24 14:53:44 -0400 | [diff] [blame] | 1313 | SkSTArenaAlloc<kSkBlitterContextSize> allocator; |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 1314 | SkBlitter* blitter = SkBlitter::ChooseSprite(fDst, paint, pmap, x, y, &allocator); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1315 | if (blitter) { |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 1316 | SkScan::FillIRect(bounds, *fRC, blitter); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1317 | return; |
| 1318 | } |
| 1319 | } |
| 1320 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1321 | SkMatrix matrix; |
| 1322 | SkRect r; |
| 1323 | |
| 1324 | // get a scalar version of our rect |
| 1325 | r.set(bounds); |
| 1326 | |
commit-bot@chromium.org | 9c9005a | 2014-04-28 14:55:39 +0000 | [diff] [blame] | 1327 | // create shader with offset |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1328 | matrix.setTranslate(r.fLeft, r.fTop); |
Herb Derby | 4bf560a | 2017-02-14 11:13:26 -0500 | [diff] [blame] | 1329 | SkPaint paintWithShader = make_paint_with_image(paint, bitmap, &matrix); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1330 | SkDraw draw(*this); |
| 1331 | matrix.reset(); |
| 1332 | draw.fMatrix = &matrix; |
| 1333 | // call ourself with a rect |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 1334 | // is this OK if paint has a rasterizer? |
Herb Derby | 4bf560a | 2017-02-14 11:13:26 -0500 | [diff] [blame] | 1335 | draw.drawRect(r, paintWithShader); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
| 1338 | /////////////////////////////////////////////////////////////////////////////// |
| 1339 | |
Mike Reed | 7120b2d | 2017-05-09 12:16:04 -0400 | [diff] [blame] | 1340 | #include "SkPaintPriv.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1341 | #include "SkScalerContext.h" |
| 1342 | #include "SkGlyphCache.h" |
reed@google.com | e691376 | 2012-08-07 15:19:47 +0000 | [diff] [blame] | 1343 | #include "SkTextToPathIter.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1344 | #include "SkUtils.h" |
| 1345 | |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 1346 | bool SkDraw::ShouldDrawTextAsPaths(const SkPaint& paint, const SkMatrix& ctm) { |
| 1347 | // hairline glyphs are fast enough so we don't need to cache them |
| 1348 | if (SkPaint::kStroke_Style == paint.getStyle() && 0 == paint.getStrokeWidth()) { |
| 1349 | return true; |
| 1350 | } |
| 1351 | |
| 1352 | // we don't cache perspective |
| 1353 | if (ctm.hasPerspective()) { |
| 1354 | return true; |
| 1355 | } |
| 1356 | |
| 1357 | SkMatrix textM; |
Mike Reed | 7120b2d | 2017-05-09 12:16:04 -0400 | [diff] [blame] | 1358 | SkPaintPriv::MakeTextMatrix(&textM, paint); |
| 1359 | return SkPaint::TooBigToUseCache(ctm, textM); |
commit-bot@chromium.org | 8128d8c | 2013-12-19 16:12:25 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 1362 | void SkDraw::drawText_asPaths(const char text[], size_t byteLength, SkScalar x, SkScalar y, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1363 | const SkPaint& paint) const { |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1364 | SkDEBUGCODE(this->validate();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1365 | |
djsollen@google.com | 166e653 | 2012-03-20 14:24:38 +0000 | [diff] [blame] | 1366 | SkTextToPathIter iter(text, byteLength, paint, true); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1367 | |
| 1368 | SkMatrix matrix; |
| 1369 | matrix.setScale(iter.getPathScale(), iter.getPathScale()); |
| 1370 | matrix.postTranslate(x, y); |
| 1371 | |
| 1372 | const SkPath* iterPath; |
| 1373 | SkScalar xpos, prevXPos = 0; |
| 1374 | |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 1375 | while (iter.next(&iterPath, &xpos)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1376 | matrix.postTranslate(xpos - prevXPos, 0); |
reed@google.com | 7b4531f | 2012-08-07 15:53:00 +0000 | [diff] [blame] | 1377 | if (iterPath) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 1378 | this->drawPath(*iterPath, iter.getPaint(), &matrix, false); |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1379 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1380 | prevXPos = xpos; |
| 1381 | } |
| 1382 | } |
| 1383 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1384 | // disable warning : local variable used without having been initialized |
bungeman | d7dc76f | 2016-03-10 11:14:40 -0800 | [diff] [blame] | 1385 | #if defined _WIN32 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1386 | #pragma warning ( push ) |
| 1387 | #pragma warning ( disable : 4701 ) |
| 1388 | #endif |
| 1389 | |
herb | f553e4e | 2015-11-09 08:51:56 -0800 | [diff] [blame] | 1390 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1391 | |
herb | d4c24f6 | 2015-12-07 12:12:29 -0800 | [diff] [blame] | 1392 | class DrawOneGlyph { |
| 1393 | public: |
| 1394 | DrawOneGlyph(const SkDraw& draw, const SkPaint& paint, SkGlyphCache* cache, SkBlitter* blitter) |
| 1395 | : fUseRegionToDraw(UsingRegionToDraw(draw.fRC)) |
| 1396 | , fGlyphCache(cache) |
| 1397 | , fBlitter(blitter) |
| 1398 | , fClip(fUseRegionToDraw ? &draw.fRC->bwRgn() : nullptr) |
| 1399 | , fDraw(draw) |
| 1400 | , fPaint(paint) |
| 1401 | , fClipBounds(PickClipBounds(draw)) { } |
herb | 11a7f7f | 2015-11-24 12:41:00 -0800 | [diff] [blame] | 1402 | |
herb | d4c24f6 | 2015-12-07 12:12:29 -0800 | [diff] [blame] | 1403 | void operator()(const SkGlyph& glyph, SkPoint position, SkPoint rounding) { |
| 1404 | position += rounding; |
herb | d4c24f6 | 2015-12-07 12:12:29 -0800 | [diff] [blame] | 1405 | // Prevent glyphs from being drawn outside of or straddling the edge of device space. |
mtklein | 875e13c | 2016-06-19 05:28:33 -0700 | [diff] [blame] | 1406 | // Comparisons written a little weirdly so that NaN coordinates are treated safely. |
| 1407 | auto gt = [](float a, int b) { return !(a <= (float)b); }; |
| 1408 | auto lt = [](float a, int b) { return !(a >= (float)b); }; |
| 1409 | if (gt(position.fX, INT_MAX - (INT16_MAX + UINT16_MAX)) || |
| 1410 | lt(position.fX, INT_MIN - (INT16_MIN + 0 /*UINT16_MIN*/)) || |
| 1411 | gt(position.fY, INT_MAX - (INT16_MAX + UINT16_MAX)) || |
| 1412 | lt(position.fY, INT_MIN - (INT16_MIN + 0 /*UINT16_MIN*/))) { |
herb | d4c24f6 | 2015-12-07 12:12:29 -0800 | [diff] [blame] | 1413 | return; |
| 1414 | } |
herb | 11a7f7f | 2015-11-24 12:41:00 -0800 | [diff] [blame] | 1415 | |
benjaminwagner | 77a9cc1 | 2016-03-25 11:24:30 -0700 | [diff] [blame] | 1416 | int left = SkScalarFloorToInt(position.fX); |
| 1417 | int top = SkScalarFloorToInt(position.fY); |
herb | d4c24f6 | 2015-12-07 12:12:29 -0800 | [diff] [blame] | 1418 | SkASSERT(glyph.fWidth > 0 && glyph.fHeight > 0); |
| 1419 | |
| 1420 | left += glyph.fLeft; |
| 1421 | top += glyph.fTop; |
| 1422 | |
| 1423 | int right = left + glyph.fWidth; |
| 1424 | int bottom = top + glyph.fHeight; |
| 1425 | |
| 1426 | SkMask mask; |
| 1427 | mask.fBounds.set(left, top, right, bottom); |
mtklein | 875e13c | 2016-06-19 05:28:33 -0700 | [diff] [blame] | 1428 | SkASSERT(!mask.fBounds.isEmpty()); |
herb | d4c24f6 | 2015-12-07 12:12:29 -0800 | [diff] [blame] | 1429 | |
| 1430 | if (fUseRegionToDraw) { |
| 1431 | SkRegion::Cliperator clipper(*fClip, mask.fBounds); |
| 1432 | |
| 1433 | if (!clipper.done() && this->getImageData(glyph, &mask)) { |
| 1434 | const SkIRect& cr = clipper.rect(); |
| 1435 | do { |
| 1436 | this->blitMask(mask, cr); |
| 1437 | clipper.next(); |
| 1438 | } while (!clipper.done()); |
| 1439 | } |
| 1440 | } else { |
| 1441 | SkIRect storage; |
| 1442 | SkIRect* bounds = &mask.fBounds; |
| 1443 | |
| 1444 | // this extra test is worth it, assuming that most of the time it succeeds |
| 1445 | // since we can avoid writing to storage |
| 1446 | if (!fClipBounds.containsNoEmptyCheck(mask.fBounds)) { |
| 1447 | if (!storage.intersectNoEmptyCheck(mask.fBounds, fClipBounds)) |
| 1448 | return; |
| 1449 | bounds = &storage; |
| 1450 | } |
| 1451 | |
| 1452 | if (this->getImageData(glyph, &mask)) { |
| 1453 | this->blitMask(mask, *bounds); |
| 1454 | } |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | private: |
| 1459 | static bool UsingRegionToDraw(const SkRasterClip* rClip) { |
| 1460 | return rClip->isBW() && !rClip->isRect(); |
| 1461 | } |
| 1462 | |
| 1463 | static SkIRect PickClipBounds(const SkDraw& draw) { |
| 1464 | const SkRasterClip& rasterClip = *draw.fRC; |
| 1465 | |
| 1466 | if (rasterClip.isBW()) { |
| 1467 | return rasterClip.bwRgn().getBounds(); |
| 1468 | } else { |
| 1469 | return rasterClip.aaRgn().getBounds(); |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | bool getImageData(const SkGlyph& glyph, SkMask* mask) { |
| 1474 | uint8_t* bits = (uint8_t*)(fGlyphCache->findImage(glyph)); |
| 1475 | if (nullptr == bits) { |
| 1476 | return false; // can't rasterize glyph |
| 1477 | } |
| 1478 | mask->fImage = bits; |
| 1479 | mask->fRowBytes = glyph.rowBytes(); |
| 1480 | mask->fFormat = static_cast<SkMask::Format>(glyph.fMaskFormat); |
| 1481 | return true; |
| 1482 | } |
| 1483 | |
herb | 11a7f7f | 2015-11-24 12:41:00 -0800 | [diff] [blame] | 1484 | void blitMask(const SkMask& mask, const SkIRect& clip) const { |
| 1485 | if (SkMask::kARGB32_Format == mask.fFormat) { |
herb | d4c24f6 | 2015-12-07 12:12:29 -0800 | [diff] [blame] | 1486 | SkBitmap bm; |
| 1487 | bm.installPixels( |
| 1488 | SkImageInfo::MakeN32Premul(mask.fBounds.width(), mask.fBounds.height()), |
| 1489 | (SkPMColor*)mask.fImage, mask.fRowBytes); |
| 1490 | |
| 1491 | fDraw.drawSprite(bm, mask.fBounds.x(), mask.fBounds.y(), fPaint); |
herb | 11a7f7f | 2015-11-24 12:41:00 -0800 | [diff] [blame] | 1492 | } else { |
| 1493 | fBlitter->blitMask(mask, clip); |
| 1494 | } |
| 1495 | } |
| 1496 | |
herb | d4c24f6 | 2015-12-07 12:12:29 -0800 | [diff] [blame] | 1497 | const bool fUseRegionToDraw; |
| 1498 | SkGlyphCache * const fGlyphCache; |
| 1499 | SkBlitter * const fBlitter; |
| 1500 | const SkRegion* const fClip; |
| 1501 | const SkDraw& fDraw; |
| 1502 | const SkPaint& fPaint; |
| 1503 | const SkIRect fClipBounds; |
herb | 11a7f7f | 2015-11-24 12:41:00 -0800 | [diff] [blame] | 1504 | }; |
| 1505 | |
herb | d4c24f6 | 2015-12-07 12:12:29 -0800 | [diff] [blame] | 1506 | //////////////////////////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1507 | |
brianosman | a1e8f8d | 2016-04-08 06:47:54 -0700 | [diff] [blame] | 1508 | uint32_t SkDraw::scalerContextFlags() const { |
| 1509 | uint32_t flags = SkPaint::kBoostContrast_ScalerContextFlag; |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 1510 | if (!fDst.colorSpace()) { |
brianosman | a1e8f8d | 2016-04-08 06:47:54 -0700 | [diff] [blame] | 1511 | flags |= SkPaint::kFakeGamma_ScalerContextFlag; |
| 1512 | } |
| 1513 | return flags; |
bungeman | f6d1e60 | 2016-02-22 13:20:28 -0800 | [diff] [blame] | 1514 | } |
| 1515 | |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 1516 | void SkDraw::drawText(const char text[], size_t byteLength, SkScalar x, SkScalar y, |
| 1517 | const SkPaint& paint, const SkSurfaceProps* props) const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1518 | SkASSERT(byteLength == 0 || text != nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1519 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1520 | SkDEBUGCODE(this->validate();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1521 | |
| 1522 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1523 | if (text == nullptr || byteLength == 0 || fRC->isEmpty()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1524 | return; |
| 1525 | } |
| 1526 | |
bsalomon@google.com | 36d6eda | 2012-10-10 16:12:14 +0000 | [diff] [blame] | 1527 | // SkScalarRec doesn't currently have a way of representing hairline stroke and |
| 1528 | // will fill if its frame-width is 0. |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 1529 | if (ShouldDrawTextAsPaths(paint, *fMatrix)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1530 | this->drawText_asPaths(text, byteLength, x, y, paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1531 | return; |
| 1532 | } |
| 1533 | |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 1534 | SkAutoGlyphCache cache(paint, props, this->scalerContextFlags(), fMatrix); |
herb | 11a7f7f | 2015-11-24 12:41:00 -0800 | [diff] [blame] | 1535 | |
| 1536 | // The Blitter Choose needs to be live while using the blitter below. |
| 1537 | SkAutoBlitterChoose blitterChooser(fDst, *fMatrix, paint); |
| 1538 | SkAAClipBlitterWrapper wrapper(*fRC, blitterChooser.get()); |
bungeman | f6d1e60 | 2016-02-22 13:20:28 -0800 | [diff] [blame] | 1539 | DrawOneGlyph drawOneGlyph(*this, paint, cache.get(), wrapper.getBlitter()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1540 | |
herb | e59124e | 2015-11-18 10:54:39 -0800 | [diff] [blame] | 1541 | SkFindAndPlaceGlyph::ProcessText( |
herb | 4c11b3f | 2015-11-20 13:53:12 -0800 | [diff] [blame] | 1542 | paint.getTextEncoding(), text, byteLength, |
bungeman | f6d1e60 | 2016-02-22 13:20:28 -0800 | [diff] [blame] | 1543 | {x, y}, *fMatrix, paint.getTextAlign(), cache.get(), drawOneGlyph); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1546 | ////////////////////////////////////////////////////////////////////////////// |
| 1547 | |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 1548 | void SkDraw::drawPosText_asPaths(const char text[], size_t byteLength, const SkScalar pos[], |
| 1549 | int scalarsPerPosition, const SkPoint& offset, |
| 1550 | const SkPaint& origPaint, const SkSurfaceProps* props) const { |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 1551 | // setup our std paint, in hopes of getting hits in the cache |
| 1552 | SkPaint paint(origPaint); |
| 1553 | SkScalar matrixScale = paint.setupForAsPaths(); |
| 1554 | |
reed@google.com | 5a64902 | 2013-06-05 18:00:30 +0000 | [diff] [blame] | 1555 | SkMatrix matrix; |
| 1556 | matrix.setScale(matrixScale, matrixScale); |
skia.committer@gmail.com | 8f6ef40 | 2013-06-05 07:01:06 +0000 | [diff] [blame] | 1557 | |
commit-bot@chromium.org | 4e82cdb | 2014-05-23 12:32:23 +0000 | [diff] [blame] | 1558 | // Temporarily jam in kFill, so we only ever ask for the raw outline from the cache. |
| 1559 | paint.setStyle(SkPaint::kFill_Style); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1560 | paint.setPathEffect(nullptr); |
commit-bot@chromium.org | 4e82cdb | 2014-05-23 12:32:23 +0000 | [diff] [blame] | 1561 | |
robertphillips | e34f17d | 2016-07-19 07:59:22 -0700 | [diff] [blame] | 1562 | SkPaint::GlyphCacheProc glyphCacheProc = SkPaint::GetGlyphCacheProc(paint.getTextEncoding(), |
| 1563 | paint.isDevKernText(), |
| 1564 | true); |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 1565 | SkAutoGlyphCache cache(paint, props, this->scalerContextFlags(), nullptr); |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 1566 | |
| 1567 | const char* stop = text + byteLength; |
bungeman | 79738cc | 2015-03-11 14:05:29 -0700 | [diff] [blame] | 1568 | SkTextAlignProc alignProc(paint.getTextAlign()); |
fmalita | 05c4a43 | 2014-09-29 06:29:53 -0700 | [diff] [blame] | 1569 | SkTextMapStateProc tmsProc(SkMatrix::I(), offset, scalarsPerPosition); |
skia.committer@gmail.com | 8f6ef40 | 2013-06-05 07:01:06 +0000 | [diff] [blame] | 1570 | |
commit-bot@chromium.org | 4e82cdb | 2014-05-23 12:32:23 +0000 | [diff] [blame] | 1571 | // Now restore the original settings, so we "draw" with whatever style/stroking. |
| 1572 | paint.setStyle(origPaint.getStyle()); |
Mike Reed | 693fdbd | 2017-01-12 10:13:40 -0500 | [diff] [blame] | 1573 | paint.setPathEffect(origPaint.refPathEffect()); |
commit-bot@chromium.org | 4e82cdb | 2014-05-23 12:32:23 +0000 | [diff] [blame] | 1574 | |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 1575 | while (text < stop) { |
benjaminwagner | d936f63 | 2016-02-23 10:44:31 -0800 | [diff] [blame] | 1576 | const SkGlyph& glyph = glyphCacheProc(cache.get(), &text); |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 1577 | if (glyph.fWidth) { |
| 1578 | const SkPath* path = cache->findPath(glyph); |
| 1579 | if (path) { |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 1580 | SkPoint tmsLoc; |
| 1581 | tmsProc(pos, &tmsLoc); |
bungeman@google.com | cfd90d6 | 2013-12-13 21:26:02 +0000 | [diff] [blame] | 1582 | SkPoint loc; |
kkinnunen | cb9a2c8 | 2014-06-12 23:06:28 -0700 | [diff] [blame] | 1583 | alignProc(tmsLoc, glyph, &loc); |
skia.committer@gmail.com | 8f6ef40 | 2013-06-05 07:01:06 +0000 | [diff] [blame] | 1584 | |
bungeman@google.com | cfd90d6 | 2013-12-13 21:26:02 +0000 | [diff] [blame] | 1585 | matrix[SkMatrix::kMTransX] = loc.fX; |
| 1586 | matrix[SkMatrix::kMTransY] = loc.fY; |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 1587 | this->drawPath(*path, paint, &matrix, false); |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 1588 | } |
| 1589 | } |
| 1590 | pos += scalarsPerPosition; |
| 1591 | } |
| 1592 | } |
| 1593 | |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 1594 | void SkDraw::drawPosText(const char text[], size_t byteLength, const SkScalar pos[], |
| 1595 | int scalarsPerPosition, const SkPoint& offset, const SkPaint& paint, |
| 1596 | const SkSurfaceProps* props) const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1597 | SkASSERT(byteLength == 0 || text != nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1598 | SkASSERT(1 == scalarsPerPosition || 2 == scalarsPerPosition); |
| 1599 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1600 | SkDEBUGCODE(this->validate();) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1601 | |
| 1602 | // nothing to draw |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1603 | if (text == nullptr || byteLength == 0 || fRC->isEmpty()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1604 | return; |
| 1605 | } |
| 1606 | |
reed@google.com | ed43dff | 2013-06-04 16:56:27 +0000 | [diff] [blame] | 1607 | if (ShouldDrawTextAsPaths(paint, *fMatrix)) { |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 1608 | this->drawPosText_asPaths(text, byteLength, pos, scalarsPerPosition, offset, paint, props); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1609 | return; |
| 1610 | } |
| 1611 | |
Mike Reed | 99330ba | 2017-02-22 11:01:08 -0500 | [diff] [blame] | 1612 | SkAutoGlyphCache cache(paint, props, this->scalerContextFlags(), fMatrix); |
herb | d4c24f6 | 2015-12-07 12:12:29 -0800 | [diff] [blame] | 1613 | |
herb | 9be5ff6 | 2015-11-11 11:30:11 -0800 | [diff] [blame] | 1614 | // The Blitter Choose needs to be live while using the blitter below. |
herb | 11a7f7f | 2015-11-24 12:41:00 -0800 | [diff] [blame] | 1615 | SkAutoBlitterChoose blitterChooser(fDst, *fMatrix, paint); |
| 1616 | SkAAClipBlitterWrapper wrapper(*fRC, blitterChooser.get()); |
bungeman | f6d1e60 | 2016-02-22 13:20:28 -0800 | [diff] [blame] | 1617 | DrawOneGlyph drawOneGlyph(*this, paint, cache.get(), wrapper.getBlitter()); |
herb | d4c24f6 | 2015-12-07 12:12:29 -0800 | [diff] [blame] | 1618 | SkPaint::Align textAlignment = paint.getTextAlign(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1619 | |
herb | 9be5ff6 | 2015-11-11 11:30:11 -0800 | [diff] [blame] | 1620 | SkFindAndPlaceGlyph::ProcessPosText( |
herb | 4c11b3f | 2015-11-20 13:53:12 -0800 | [diff] [blame] | 1621 | paint.getTextEncoding(), text, byteLength, |
bungeman | f6d1e60 | 2016-02-22 13:20:28 -0800 | [diff] [blame] | 1622 | offset, *fMatrix, pos, scalarsPerPosition, textAlignment, cache.get(), drawOneGlyph); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
bungeman | d7dc76f | 2016-03-10 11:14:40 -0800 | [diff] [blame] | 1625 | #if defined _WIN32 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1626 | #pragma warning ( pop ) |
| 1627 | #endif |
| 1628 | |
Mike Reed | 787a16d | 2017-05-15 09:29:18 -0400 | [diff] [blame] | 1629 | //////////////////////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1630 | |
| 1631 | #ifdef SK_DEBUG |
| 1632 | |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 1633 | void SkDraw::validate() const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1634 | SkASSERT(fMatrix != nullptr); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1635 | SkASSERT(fRC != nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1636 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 1637 | const SkIRect& cr = fRC->getBounds(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1638 | SkIRect br; |
| 1639 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 1640 | br.set(0, 0, fDst.width(), fDst.height()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1641 | SkASSERT(cr.isEmpty() || br.contains(cr)); |
| 1642 | } |
| 1643 | |
| 1644 | #endif |
| 1645 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1646 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 1647 | |
| 1648 | #include "SkPath.h" |
| 1649 | #include "SkDraw.h" |
| 1650 | #include "SkRegion.h" |
| 1651 | #include "SkBlitter.h" |
| 1652 | |
| 1653 | static bool compute_bounds(const SkPath& devPath, const SkIRect* clipBounds, |
reed | b07a94f | 2014-11-19 05:03:18 -0800 | [diff] [blame] | 1654 | const SkMaskFilter* filter, const SkMatrix* filterMatrix, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1655 | SkIRect* bounds) { |
| 1656 | if (devPath.isEmpty()) { |
| 1657 | return false; |
| 1658 | } |
| 1659 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1660 | // init our bounds from the path |
reed | 11fa224 | 2015-03-13 06:08:28 -0700 | [diff] [blame] | 1661 | *bounds = devPath.getBounds().makeOutset(SK_ScalarHalf, SK_ScalarHalf).roundOut(); |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 1662 | |
tomhudson@google.com | 6db75fc | 2012-03-23 14:46:48 +0000 | [diff] [blame] | 1663 | SkIPoint margin = SkIPoint::Make(0, 0); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1664 | if (filter) { |
| 1665 | SkASSERT(filterMatrix); |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 1666 | |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 1667 | SkMask srcM, dstM; |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 1668 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1669 | srcM.fBounds = *bounds; |
| 1670 | srcM.fFormat = SkMask::kA8_Format; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1671 | if (!filter->filterMask(&dstM, srcM, *filterMatrix, &margin)) { |
| 1672 | return false; |
| 1673 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 1676 | // (possibly) trim the bounds to reflect the clip |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1677 | // (plus whatever slop the filter needs) |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 1678 | if (clipBounds) { |
reed@android.com | 3555591 | 2009-03-16 18:46:55 +0000 | [diff] [blame] | 1679 | // Ugh. Guard against gigantic margins from wacky filters. Without this |
| 1680 | // check we can request arbitrary amounts of slop beyond our visible |
| 1681 | // clip, and bring down the renderer (at least on finite RAM machines |
| 1682 | // like handsets, etc.). Need to balance this invented value between |
| 1683 | // quality of large filters like blurs, and the corresponding memory |
| 1684 | // requests. |
| 1685 | static const int MAX_MARGIN = 128; |
reed | 11fa224 | 2015-03-13 06:08:28 -0700 | [diff] [blame] | 1686 | if (!bounds->intersect(clipBounds->makeOutset(SkMin32(margin.fX, MAX_MARGIN), |
| 1687 | SkMin32(margin.fY, MAX_MARGIN)))) { |
bungeman@google.com | 5af16f8 | 2011-09-02 15:06:44 +0000 | [diff] [blame] | 1688 | return false; |
| 1689 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
| 1692 | return true; |
| 1693 | } |
| 1694 | |
bsalomon | 055e192 | 2016-05-06 07:22:58 -0700 | [diff] [blame] | 1695 | static void draw_into_mask(const SkMask& mask, const SkPath& devPath, |
| 1696 | SkStrokeRec::InitStyle style) { |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 1697 | SkDraw draw; |
| 1698 | if (!draw.fDst.reset(mask)) { |
| 1699 | return; |
| 1700 | } |
| 1701 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 1702 | SkRasterClip clip; |
| 1703 | SkMatrix matrix; |
| 1704 | SkPaint paint; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1705 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 1706 | clip.setRect(SkIRect::MakeWH(mask.fBounds.width(), mask.fBounds.height())); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1707 | matrix.setTranslate(-SkIntToScalar(mask.fBounds.fLeft), |
| 1708 | -SkIntToScalar(mask.fBounds.fTop)); |
| 1709 | |
reed@google.com | 045e62d | 2011-10-24 12:19:46 +0000 | [diff] [blame] | 1710 | draw.fRC = &clip; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1711 | draw.fMatrix = &matrix; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1712 | paint.setAntiAlias(true); |
bsalomon | 055e192 | 2016-05-06 07:22:58 -0700 | [diff] [blame] | 1713 | switch (style) { |
| 1714 | case SkStrokeRec::kHairline_InitStyle: |
| 1715 | SkASSERT(!paint.getStrokeWidth()); |
| 1716 | paint.setStyle(SkPaint::kStroke_Style); |
| 1717 | break; |
| 1718 | case SkStrokeRec::kFill_InitStyle: |
| 1719 | SkASSERT(paint.getStyle() == SkPaint::kFill_Style); |
| 1720 | break; |
| 1721 | |
| 1722 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1723 | draw.drawPath(devPath, paint); |
| 1724 | } |
| 1725 | |
| 1726 | bool SkDraw::DrawToMask(const SkPath& devPath, const SkIRect* clipBounds, |
reed@google.com | 30711b7 | 2012-12-18 19:18:39 +0000 | [diff] [blame] | 1727 | const SkMaskFilter* filter, const SkMatrix* filterMatrix, |
junov@chromium.org | 2ac4ef5 | 2012-04-04 15:16:51 +0000 | [diff] [blame] | 1728 | SkMask* mask, SkMask::CreateMode mode, |
bsalomon | 055e192 | 2016-05-06 07:22:58 -0700 | [diff] [blame] | 1729 | SkStrokeRec::InitStyle style) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1730 | if (SkMask::kJustRenderImage_CreateMode != mode) { |
| 1731 | if (!compute_bounds(devPath, clipBounds, filter, filterMatrix, &mask->fBounds)) |
| 1732 | return false; |
| 1733 | } |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 1734 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1735 | if (SkMask::kComputeBoundsAndRenderImage_CreateMode == mode) { |
| 1736 | mask->fFormat = SkMask::kA8_Format; |
| 1737 | mask->fRowBytes = mask->fBounds.width(); |
reed@android.com | 543ed93 | 2009-04-24 12:43:40 +0000 | [diff] [blame] | 1738 | size_t size = mask->computeImageSize(); |
| 1739 | if (0 == size) { |
| 1740 | // we're too big to allocate the mask, abort |
| 1741 | return false; |
| 1742 | } |
| 1743 | mask->fImage = SkMask::AllocImage(size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1744 | memset(mask->fImage, 0, mask->computeImageSize()); |
| 1745 | } |
| 1746 | |
| 1747 | if (SkMask::kJustComputeBounds_CreateMode != mode) { |
junov@chromium.org | 2ac4ef5 | 2012-04-04 15:16:51 +0000 | [diff] [blame] | 1748 | draw_into_mask(*mask, devPath, style); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1749 | } |
reed@google.com | a76de3d | 2011-01-13 18:30:42 +0000 | [diff] [blame] | 1750 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1751 | return true; |
| 1752 | } |