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