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 | */ |
| 7 | |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 8 | #include "SkArenaAlloc.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | #include "SkBlitter.h" |
| 10 | #include "SkAntiRun.h" |
| 11 | #include "SkColor.h" |
| 12 | #include "SkColorFilter.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 13 | #include "SkReadBuffer.h" |
| 14 | #include "SkWriteBuffer.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include "SkMask.h" |
| 16 | #include "SkMaskFilter.h" |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 17 | #include "SkString.h" |
reed@google.com | 4588490 | 2012-05-11 14:38:51 +0000 | [diff] [blame] | 18 | #include "SkTLazy.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | #include "SkUtils.h" |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 20 | #include "SkXfermodeInterpretation.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | |
reed | 395eabe | 2016-01-30 18:52:31 -0800 | [diff] [blame] | 22 | // define this for testing srgb blits |
reed | a34be68 | 2016-02-15 07:48:35 -0800 | [diff] [blame] | 23 | //#define SK_FORCE_PM4f_FOR_L32_BLITS |
reed | 395eabe | 2016-01-30 18:52:31 -0800 | [diff] [blame] | 24 | |
reed@android.com | 845fdac | 2009-06-23 03:01:32 +0000 | [diff] [blame] | 25 | SkBlitter::~SkBlitter() {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | |
reed@google.com | ea03360 | 2012-12-14 13:13:55 +0000 | [diff] [blame] | 27 | bool SkBlitter::isNullBlitter() const { return false; } |
| 28 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 29 | const SkPixmap* SkBlitter::justAnOpaqueColor(uint32_t* value) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 30 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | } |
| 32 | |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 33 | /* |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 34 | void SkBlitter::blitH(int x, int y, int width) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 35 | SkDEBUGFAIL("unimplemented"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 36 | } |
| 37 | |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 38 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 39 | void SkBlitter::blitAntiH(int x, int y, const SkAlpha antialias[], |
| 40 | const int16_t runs[]) { |
tomhudson@google.com | 0c00f21 | 2011-12-28 14:59:50 +0000 | [diff] [blame] | 41 | SkDEBUGFAIL("unimplemented"); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | } |
herb | 7df9e4a | 2016-06-10 13:01:27 -0700 | [diff] [blame] | 43 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 44 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 45 | void SkBlitter::blitV(int x, int y, int height, SkAlpha alpha) { |
| 46 | if (alpha == 255) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | this->blitRect(x, y, 1, height); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 48 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | int16_t runs[2]; |
| 50 | runs[0] = 1; |
| 51 | runs[1] = 0; |
| 52 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 53 | while (--height >= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | this->blitAntiH(x, y++, &alpha, runs); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 55 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 59 | void SkBlitter::blitRect(int x, int y, int width, int height) { |
tomhudson@google.com | a31ac73 | 2011-12-29 16:09:31 +0000 | [diff] [blame] | 60 | SkASSERT(width > 0); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 61 | while (--height >= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | this->blitH(x, y++, width); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 63 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 64 | } |
| 65 | |
liyuqian | 38911a7 | 2016-10-04 11:23:22 -0700 | [diff] [blame] | 66 | /// Default implementation doesn't check for easy optimizations |
| 67 | /// such as alpha == 255; also uses blitV(), which some subclasses |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 68 | /// may not support. |
| 69 | void SkBlitter::blitAntiRect(int x, int y, int width, int height, |
| 70 | SkAlpha leftAlpha, SkAlpha rightAlpha) { |
liyuqian | 38911a7 | 2016-10-04 11:23:22 -0700 | [diff] [blame] | 71 | if (leftAlpha > 0) { // we may send in x = -1 with leftAlpha = 0 |
| 72 | this->blitV(x, y, height, leftAlpha); |
| 73 | } |
| 74 | x++; |
tomhudson@google.com | a31ac73 | 2011-12-29 16:09:31 +0000 | [diff] [blame] | 75 | if (width > 0) { |
tomhudson@google.com | 4714359 | 2011-12-28 17:58:07 +0000 | [diff] [blame] | 76 | this->blitRect(x, y, width, height); |
| 77 | x += width; |
| 78 | } |
liyuqian | 38911a7 | 2016-10-04 11:23:22 -0700 | [diff] [blame] | 79 | if (rightAlpha > 0) { |
| 80 | this->blitV(x, y, height, rightAlpha); |
| 81 | } |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 82 | } |
| 83 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | ////////////////////////////////////////////////////////////////////////////// |
| 85 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 86 | static inline void bits_to_runs(SkBlitter* blitter, int x, int y, |
| 87 | const uint8_t bits[], |
herb | 5520ded | 2015-11-18 10:50:33 -0800 | [diff] [blame] | 88 | uint8_t left_mask, ptrdiff_t rowBytes, |
| 89 | uint8_t right_mask) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | int inFill = 0; |
| 91 | int pos = 0; |
| 92 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 93 | while (--rowBytes >= 0) { |
herb | 5520ded | 2015-11-18 10:50:33 -0800 | [diff] [blame] | 94 | uint8_t b = *bits++ & left_mask; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 95 | if (rowBytes == 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 96 | b &= right_mask; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 97 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | |
herb | 5520ded | 2015-11-18 10:50:33 -0800 | [diff] [blame] | 99 | for (uint8_t test = 0x80U; test != 0; test >>= 1) { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 100 | if (b & test) { |
| 101 | if (!inFill) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | pos = x; |
| 103 | inFill = true; |
| 104 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 105 | } else { |
| 106 | if (inFill) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | blitter->blitH(pos, y, x - pos); |
| 108 | inFill = false; |
| 109 | } |
| 110 | } |
| 111 | x += 1; |
| 112 | } |
herb | 5520ded | 2015-11-18 10:50:33 -0800 | [diff] [blame] | 113 | left_mask = 0xFFU; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | // final cleanup |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 117 | if (inFill) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 118 | blitter->blitH(pos, y, x - pos); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 119 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 120 | } |
| 121 | |
herb | 5520ded | 2015-11-18 10:50:33 -0800 | [diff] [blame] | 122 | // maskBitCount is the number of 1's to place in the mask. It must be in the range between 1 and 8. |
| 123 | static uint8_t generate_right_mask(int maskBitCount) { |
| 124 | return static_cast<uint8_t>(0xFF00U >> maskBitCount); |
| 125 | } |
| 126 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 127 | void SkBlitter::blitMask(const SkMask& mask, const SkIRect& clip) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 128 | SkASSERT(mask.fBounds.contains(clip)); |
| 129 | |
reed | d96e7e5 | 2016-02-17 07:15:29 -0800 | [diff] [blame] | 130 | if (mask.fFormat == SkMask::kLCD16_Format) { |
| 131 | return; // needs to be handled by subclass |
| 132 | } |
| 133 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 134 | if (mask.fFormat == SkMask::kBW_Format) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | int cx = clip.fLeft; |
| 136 | int cy = clip.fTop; |
| 137 | int maskLeft = mask.fBounds.fLeft; |
herb | 5520ded | 2015-11-18 10:50:33 -0800 | [diff] [blame] | 138 | int maskRowBytes = mask.fRowBytes; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | int height = clip.height(); |
| 140 | |
| 141 | const uint8_t* bits = mask.getAddr1(cx, cy); |
| 142 | |
herb | 5520ded | 2015-11-18 10:50:33 -0800 | [diff] [blame] | 143 | SkDEBUGCODE(const uint8_t* endOfImage = |
| 144 | mask.fImage + (mask.fBounds.height() - 1) * maskRowBytes |
| 145 | + ((mask.fBounds.width() + 7) >> 3)); |
| 146 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 147 | if (cx == maskLeft && clip.fRight == mask.fBounds.fRight) { |
| 148 | while (--height >= 0) { |
herb | 5520ded | 2015-11-18 10:50:33 -0800 | [diff] [blame] | 149 | int affectedRightBit = mask.fBounds.width() - 1; |
| 150 | ptrdiff_t rowBytes = (affectedRightBit >> 3) + 1; |
| 151 | SkASSERT(bits + rowBytes <= endOfImage); |
| 152 | U8CPU rightMask = generate_right_mask((affectedRightBit & 7) + 1); |
| 153 | bits_to_runs(this, cx, cy, bits, 0xFF, rowBytes, rightMask); |
| 154 | bits += maskRowBytes; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 155 | cy += 1; |
| 156 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 157 | } else { |
herb | 3be72b0 | 2016-06-24 13:02:31 -0700 | [diff] [blame] | 158 | // Bits is calculated as the offset into the mask at the point {cx, cy} therefore, all |
herb | 5520ded | 2015-11-18 10:50:33 -0800 | [diff] [blame] | 159 | // addressing into the bit mask is relative to that point. Since this is an address |
| 160 | // calculated from a arbitrary bit in that byte, calculate the left most bit. |
| 161 | int bitsLeft = cx - ((cx - maskLeft) & 7); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | |
herb | 5520ded | 2015-11-18 10:50:33 -0800 | [diff] [blame] | 163 | // Everything is relative to the bitsLeft. |
| 164 | int leftEdge = cx - bitsLeft; |
| 165 | SkASSERT(leftEdge >= 0); |
| 166 | int rightEdge = clip.fRight - bitsLeft; |
| 167 | SkASSERT(rightEdge > leftEdge); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 168 | |
herb | 5520ded | 2015-11-18 10:50:33 -0800 | [diff] [blame] | 169 | // Calculate left byte and mask |
| 170 | const uint8_t* leftByte = bits; |
| 171 | U8CPU leftMask = 0xFFU >> (leftEdge & 7); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 172 | |
herb | 5520ded | 2015-11-18 10:50:33 -0800 | [diff] [blame] | 173 | // Calculate right byte and mask |
| 174 | int affectedRightBit = rightEdge - 1; |
| 175 | const uint8_t* rightByte = bits + (affectedRightBit >> 3); |
| 176 | U8CPU rightMask = generate_right_mask((affectedRightBit & 7) + 1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 177 | |
herb | 5520ded | 2015-11-18 10:50:33 -0800 | [diff] [blame] | 178 | // leftByte and rightByte are byte locations therefore, to get a count of bytes the |
| 179 | // code must add one. |
| 180 | ptrdiff_t rowBytes = rightByte - leftByte + 1; |
| 181 | |
| 182 | while (--height >= 0) { |
| 183 | SkASSERT(bits + rowBytes <= endOfImage); |
| 184 | bits_to_runs(this, bitsLeft, cy, bits, leftMask, rowBytes, rightMask); |
| 185 | bits += maskRowBytes; |
| 186 | cy += 1; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 189 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 190 | int width = clip.width(); |
| 191 | SkAutoSTMalloc<64, int16_t> runStorage(width + 1); |
| 192 | int16_t* runs = runStorage.get(); |
reed@google.com | 7989186 | 2011-10-18 15:44:57 +0000 | [diff] [blame] | 193 | const uint8_t* aa = mask.getAddr8(clip.fLeft, clip.fTop); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 194 | |
| 195 | sk_memset16((uint16_t*)runs, 1, width); |
| 196 | runs[width] = 0; |
| 197 | |
| 198 | int height = clip.height(); |
| 199 | int y = clip.fTop; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 200 | while (--height >= 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 201 | this->blitAntiH(clip.fLeft, y, aa, runs); |
| 202 | aa += mask.fRowBytes; |
| 203 | y += 1; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | /////////////////////// these guys are not virtual, just a helpers |
| 209 | |
| 210 | void SkBlitter::blitMaskRegion(const SkMask& mask, const SkRegion& clip) { |
| 211 | if (clip.quickReject(mask.fBounds)) { |
| 212 | return; |
| 213 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 214 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 215 | SkRegion::Cliperator clipper(clip, mask.fBounds); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 216 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 217 | while (!clipper.done()) { |
| 218 | const SkIRect& cr = clipper.rect(); |
| 219 | this->blitMask(mask, cr); |
| 220 | clipper.next(); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | void SkBlitter::blitRectRegion(const SkIRect& rect, const SkRegion& clip) { |
| 225 | SkRegion::Cliperator clipper(clip, rect); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 226 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 227 | while (!clipper.done()) { |
| 228 | const SkIRect& cr = clipper.rect(); |
| 229 | this->blitRect(cr.fLeft, cr.fTop, cr.width(), cr.height()); |
| 230 | clipper.next(); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void SkBlitter::blitRegion(const SkRegion& clip) { |
| 235 | SkRegion::Iterator iter(clip); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 236 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 237 | while (!iter.done()) { |
| 238 | const SkIRect& cr = iter.rect(); |
| 239 | this->blitRect(cr.fLeft, cr.fTop, cr.width(), cr.height()); |
| 240 | iter.next(); |
| 241 | } |
| 242 | } |
| 243 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 244 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 245 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 246 | void SkNullBlitter::blitH(int x, int y, int width) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 247 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 248 | void SkNullBlitter::blitAntiH(int x, int y, const SkAlpha antialias[], |
| 249 | const int16_t runs[]) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 250 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 251 | void SkNullBlitter::blitV(int x, int y, int height, SkAlpha alpha) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 252 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 253 | void SkNullBlitter::blitRect(int x, int y, int width, int height) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 254 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 255 | void SkNullBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 256 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 257 | const SkPixmap* SkNullBlitter::justAnOpaqueColor(uint32_t* value) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 258 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 259 | } |
| 260 | |
reed@google.com | ea03360 | 2012-12-14 13:13:55 +0000 | [diff] [blame] | 261 | bool SkNullBlitter::isNullBlitter() const { return true; } |
| 262 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 263 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 264 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 265 | static int compute_anti_width(const int16_t runs[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 266 | int width = 0; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 267 | |
| 268 | for (;;) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 269 | int count = runs[0]; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 270 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 271 | SkASSERT(count >= 0); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 272 | if (count == 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 273 | break; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 274 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 275 | width += count; |
| 276 | runs += count; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 277 | } |
| 278 | return width; |
| 279 | } |
| 280 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 281 | static inline bool y_in_rect(int y, const SkIRect& rect) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 282 | return (unsigned)(y - rect.fTop) < (unsigned)rect.height(); |
| 283 | } |
| 284 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 285 | static inline bool x_in_rect(int x, const SkIRect& rect) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 286 | return (unsigned)(x - rect.fLeft) < (unsigned)rect.width(); |
| 287 | } |
| 288 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 289 | void SkRectClipBlitter::blitH(int left, int y, int width) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 290 | SkASSERT(width > 0); |
| 291 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 292 | if (!y_in_rect(y, fClipRect)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 293 | return; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 294 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 295 | |
| 296 | int right = left + width; |
| 297 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 298 | if (left < fClipRect.fLeft) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 299 | left = fClipRect.fLeft; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 300 | } |
| 301 | if (right > fClipRect.fRight) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 302 | right = fClipRect.fRight; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 303 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 304 | |
| 305 | width = right - left; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 306 | if (width > 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 307 | fBlitter->blitH(left, y, width); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 308 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 309 | } |
| 310 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 311 | void SkRectClipBlitter::blitAntiH(int left, int y, const SkAlpha aa[], |
| 312 | const int16_t runs[]) { |
| 313 | if (!y_in_rect(y, fClipRect) || left >= fClipRect.fRight) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 314 | return; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 315 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 316 | |
| 317 | int x0 = left; |
| 318 | int x1 = left + compute_anti_width(runs); |
| 319 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 320 | if (x1 <= fClipRect.fLeft) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 321 | return; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 322 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 323 | |
| 324 | SkASSERT(x0 < x1); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 325 | if (x0 < fClipRect.fLeft) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 326 | int dx = fClipRect.fLeft - x0; |
| 327 | SkAlphaRuns::BreakAt((int16_t*)runs, (uint8_t*)aa, dx); |
| 328 | runs += dx; |
| 329 | aa += dx; |
| 330 | x0 = fClipRect.fLeft; |
| 331 | } |
| 332 | |
| 333 | SkASSERT(x0 < x1 && runs[x1 - x0] == 0); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 334 | if (x1 > fClipRect.fRight) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 335 | x1 = fClipRect.fRight; |
| 336 | SkAlphaRuns::BreakAt((int16_t*)runs, (uint8_t*)aa, x1 - x0); |
| 337 | ((int16_t*)runs)[x1 - x0] = 0; |
| 338 | } |
| 339 | |
| 340 | SkASSERT(x0 < x1 && runs[x1 - x0] == 0); |
| 341 | SkASSERT(compute_anti_width(runs) == x1 - x0); |
| 342 | |
| 343 | fBlitter->blitAntiH(x0, y, aa, runs); |
| 344 | } |
| 345 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 346 | void SkRectClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 347 | SkASSERT(height > 0); |
| 348 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 349 | if (!x_in_rect(x, fClipRect)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 350 | return; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 351 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 352 | |
| 353 | int y0 = y; |
| 354 | int y1 = y + height; |
| 355 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 356 | if (y0 < fClipRect.fTop) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 357 | y0 = fClipRect.fTop; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 358 | } |
| 359 | if (y1 > fClipRect.fBottom) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 360 | y1 = fClipRect.fBottom; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 361 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 362 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 363 | if (y0 < y1) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 364 | fBlitter->blitV(x, y0, y1 - y0, alpha); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 365 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 366 | } |
| 367 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 368 | void SkRectClipBlitter::blitRect(int left, int y, int width, int height) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 369 | SkIRect r; |
| 370 | |
| 371 | r.set(left, y, left + width, y + height); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 372 | if (r.intersect(fClipRect)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 373 | fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height()); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 374 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 375 | } |
| 376 | |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 377 | void SkRectClipBlitter::blitAntiRect(int left, int y, int width, int height, |
| 378 | SkAlpha leftAlpha, SkAlpha rightAlpha) { |
| 379 | SkIRect r; |
| 380 | |
| 381 | // The *true* width of the rectangle blitted is width+2: |
| 382 | r.set(left, y, left + width + 2, y + height); |
| 383 | if (r.intersect(fClipRect)) { |
| 384 | if (r.fLeft != left) { |
| 385 | SkASSERT(r.fLeft > left); |
| 386 | leftAlpha = 255; |
| 387 | } |
| 388 | if (r.fRight != left + width + 2) { |
| 389 | SkASSERT(r.fRight < left + width + 2); |
| 390 | rightAlpha = 255; |
| 391 | } |
| 392 | if (255 == leftAlpha && 255 == rightAlpha) { |
| 393 | fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height()); |
| 394 | } else if (1 == r.width()) { |
| 395 | if (r.fLeft == left) { |
| 396 | fBlitter->blitV(r.fLeft, r.fTop, r.height(), leftAlpha); |
| 397 | } else { |
| 398 | SkASSERT(r.fLeft == left + width + 1); |
| 399 | fBlitter->blitV(r.fLeft, r.fTop, r.height(), rightAlpha); |
| 400 | } |
| 401 | } else { |
| 402 | fBlitter->blitAntiRect(r.fLeft, r.fTop, r.width() - 2, r.height(), |
| 403 | leftAlpha, rightAlpha); |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 408 | void SkRectClipBlitter::blitMask(const SkMask& mask, const SkIRect& clip) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 409 | SkASSERT(mask.fBounds.contains(clip)); |
| 410 | |
| 411 | SkIRect r = clip; |
| 412 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 413 | if (r.intersect(fClipRect)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 414 | fBlitter->blitMask(mask, r); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 415 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 416 | } |
| 417 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 418 | const SkPixmap* SkRectClipBlitter::justAnOpaqueColor(uint32_t* value) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 419 | return fBlitter->justAnOpaqueColor(value); |
| 420 | } |
| 421 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 422 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 423 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 424 | void SkRgnClipBlitter::blitH(int x, int y, int width) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 425 | SkRegion::Spanerator span(*fRgn, y, x, x + width); |
| 426 | int left, right; |
| 427 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 428 | while (span.next(&left, &right)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 429 | SkASSERT(left < right); |
| 430 | fBlitter->blitH(left, y, right - left); |
| 431 | } |
| 432 | } |
| 433 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 434 | void SkRgnClipBlitter::blitAntiH(int x, int y, const SkAlpha aa[], |
| 435 | const int16_t runs[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 436 | int width = compute_anti_width(runs); |
| 437 | SkRegion::Spanerator span(*fRgn, y, x, x + width); |
| 438 | int left, right; |
| 439 | SkDEBUGCODE(const SkIRect& bounds = fRgn->getBounds();) |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 440 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 441 | int prevRite = x; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 442 | while (span.next(&left, &right)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 443 | SkASSERT(x <= left); |
| 444 | SkASSERT(left < right); |
| 445 | SkASSERT(left >= bounds.fLeft && right <= bounds.fRight); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 446 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 447 | SkAlphaRuns::Break((int16_t*)runs, (uint8_t*)aa, left - x, right - left); |
| 448 | |
| 449 | // now zero before left |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 450 | if (left > prevRite) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 451 | int index = prevRite - x; |
| 452 | ((uint8_t*)aa)[index] = 0; // skip runs after right |
| 453 | ((int16_t*)runs)[index] = SkToS16(left - prevRite); |
| 454 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 455 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 456 | prevRite = right; |
| 457 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 458 | |
| 459 | if (prevRite > x) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 460 | ((int16_t*)runs)[prevRite - x] = 0; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 461 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 462 | if (x < 0) { |
| 463 | int skip = runs[0]; |
| 464 | SkASSERT(skip >= -x); |
| 465 | aa += skip; |
| 466 | runs += skip; |
| 467 | x += skip; |
| 468 | } |
| 469 | fBlitter->blitAntiH(x, y, aa, runs); |
| 470 | } |
| 471 | } |
| 472 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 473 | void SkRgnClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 474 | SkIRect bounds; |
| 475 | bounds.set(x, y, x + 1, y + height); |
| 476 | |
| 477 | SkRegion::Cliperator iter(*fRgn, bounds); |
| 478 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 479 | while (!iter.done()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 480 | const SkIRect& r = iter.rect(); |
| 481 | SkASSERT(bounds.contains(r)); |
| 482 | |
| 483 | fBlitter->blitV(x, r.fTop, r.height(), alpha); |
| 484 | iter.next(); |
| 485 | } |
| 486 | } |
| 487 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 488 | void SkRgnClipBlitter::blitRect(int x, int y, int width, int height) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 489 | SkIRect bounds; |
| 490 | bounds.set(x, y, x + width, y + height); |
| 491 | |
| 492 | SkRegion::Cliperator iter(*fRgn, bounds); |
| 493 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 494 | while (!iter.done()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 495 | const SkIRect& r = iter.rect(); |
| 496 | SkASSERT(bounds.contains(r)); |
| 497 | |
| 498 | fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height()); |
| 499 | iter.next(); |
| 500 | } |
| 501 | } |
| 502 | |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 503 | void SkRgnClipBlitter::blitAntiRect(int x, int y, int width, int height, |
| 504 | SkAlpha leftAlpha, SkAlpha rightAlpha) { |
| 505 | // The *true* width of the rectangle to blit is width + 2 |
| 506 | SkIRect bounds; |
| 507 | bounds.set(x, y, x + width + 2, y + height); |
| 508 | |
| 509 | SkRegion::Cliperator iter(*fRgn, bounds); |
| 510 | |
| 511 | while (!iter.done()) { |
| 512 | const SkIRect& r = iter.rect(); |
| 513 | SkASSERT(bounds.contains(r)); |
| 514 | SkASSERT(r.fLeft >= x); |
tomhudson@google.com | 31bab39 | 2012-01-03 20:12:42 +0000 | [diff] [blame] | 515 | SkASSERT(r.fRight <= x + width + 2); |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 516 | |
| 517 | SkAlpha effectiveLeftAlpha = (r.fLeft == x) ? leftAlpha : 255; |
| 518 | SkAlpha effectiveRightAlpha = (r.fRight == x + width + 2) ? |
| 519 | rightAlpha : 255; |
| 520 | |
| 521 | if (255 == effectiveLeftAlpha && 255 == effectiveRightAlpha) { |
| 522 | fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height()); |
| 523 | } else if (1 == r.width()) { |
| 524 | if (r.fLeft == x) { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 525 | fBlitter->blitV(r.fLeft, r.fTop, r.height(), |
tomhudson@google.com | 49eac19 | 2011-12-27 13:59:20 +0000 | [diff] [blame] | 526 | effectiveLeftAlpha); |
| 527 | } else { |
| 528 | SkASSERT(r.fLeft == x + width + 1); |
| 529 | fBlitter->blitV(r.fLeft, r.fTop, r.height(), |
| 530 | effectiveRightAlpha); |
| 531 | } |
| 532 | } else { |
| 533 | fBlitter->blitAntiRect(r.fLeft, r.fTop, r.width() - 2, r.height(), |
| 534 | effectiveLeftAlpha, effectiveRightAlpha); |
| 535 | } |
| 536 | iter.next(); |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 541 | void SkRgnClipBlitter::blitMask(const SkMask& mask, const SkIRect& clip) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 542 | SkASSERT(mask.fBounds.contains(clip)); |
| 543 | |
| 544 | SkRegion::Cliperator iter(*fRgn, clip); |
| 545 | const SkIRect& r = iter.rect(); |
| 546 | SkBlitter* blitter = fBlitter; |
| 547 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 548 | while (!iter.done()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 549 | blitter->blitMask(mask, r); |
| 550 | iter.next(); |
| 551 | } |
| 552 | } |
| 553 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 554 | const SkPixmap* SkRgnClipBlitter::justAnOpaqueColor(uint32_t* value) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 555 | return fBlitter->justAnOpaqueColor(value); |
| 556 | } |
| 557 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 558 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 559 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 560 | SkBlitter* SkBlitterClipper::apply(SkBlitter* blitter, const SkRegion* clip, |
| 561 | const SkIRect* ir) { |
| 562 | if (clip) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 563 | const SkIRect& clipR = clip->getBounds(); |
| 564 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 565 | if (clip->isEmpty() || (ir && !SkIRect::Intersects(clipR, *ir))) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 566 | blitter = &fNullBlitter; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 567 | } else if (clip->isRect()) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 568 | if (ir == nullptr || !clipR.contains(*ir)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 569 | fRectBlitter.init(blitter, clipR); |
| 570 | blitter = &fRectBlitter; |
| 571 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 572 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 573 | fRgnBlitter.init(blitter, clip); |
| 574 | blitter = &fRgnBlitter; |
| 575 | } |
| 576 | } |
| 577 | return blitter; |
| 578 | } |
| 579 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 580 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 581 | |
| 582 | #include "SkColorShader.h" |
| 583 | #include "SkColorPriv.h" |
| 584 | |
| 585 | class Sk3DShader : public SkShader { |
| 586 | public: |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 587 | Sk3DShader(sk_sp<SkShader> proxy) : fProxy(std::move(proxy)) {} |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 588 | |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 589 | Context* onMakeContext(const ContextRec& rec, SkArenaAlloc* alloc) const override { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 590 | SkShader::Context* proxyContext = nullptr; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 591 | if (fProxy) { |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 592 | proxyContext = fProxy->makeContext(rec, alloc); |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 593 | if (!proxyContext) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 594 | return nullptr; |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 595 | } |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 596 | } |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 597 | return alloc->make<Sk3DShaderContext>(*this, rec, proxyContext); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 598 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 599 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 600 | class Sk3DShaderContext : public SkShader::Context { |
| 601 | public: |
| 602 | // Calls proxyContext's destructor but will NOT free its memory. |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 603 | Sk3DShaderContext(const Sk3DShader& shader, const ContextRec& rec, |
| 604 | SkShader::Context* proxyContext) |
| 605 | : INHERITED(shader, rec) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 606 | , fMask(nullptr) |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 607 | , fProxyContext(proxyContext) |
| 608 | { |
| 609 | if (!fProxyContext) { |
commit-bot@chromium.org | e901b6d | 2014-05-01 19:31:31 +0000 | [diff] [blame] | 610 | fPMColor = SkPreMultiplyColor(rec.fPaint->getColor()); |
commit-bot@chromium.org | bc2f1dc | 2014-04-23 09:11:58 +0000 | [diff] [blame] | 611 | } |
| 612 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 613 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 614 | virtual ~Sk3DShaderContext() { |
| 615 | if (fProxyContext) { |
| 616 | fProxyContext->~Context(); |
| 617 | } |
| 618 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 619 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 620 | void set3DMask(const SkMask* mask) override { fMask = mask; } |
commit-bot@chromium.org | 001f4ed | 2014-04-16 10:16:39 +0000 | [diff] [blame] | 621 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 622 | void shadeSpan(int x, int y, SkPMColor span[], int count) override { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 623 | if (fProxyContext) { |
| 624 | fProxyContext->shadeSpan(x, y, span, count); |
| 625 | } |
commit-bot@chromium.org | bc2f1dc | 2014-04-23 09:11:58 +0000 | [diff] [blame] | 626 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 627 | if (fMask == nullptr) { |
| 628 | if (fProxyContext == nullptr) { |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 629 | sk_memset32(span, fPMColor, count); |
| 630 | } |
| 631 | return; |
| 632 | } |
| 633 | |
| 634 | SkASSERT(fMask->fBounds.contains(x, y)); |
| 635 | SkASSERT(fMask->fBounds.contains(x + count - 1, y)); |
| 636 | |
| 637 | size_t size = fMask->computeImageSize(); |
| 638 | const uint8_t* alpha = fMask->getAddr8(x, y); |
| 639 | const uint8_t* mulp = alpha + size; |
| 640 | const uint8_t* addp = mulp + size; |
| 641 | |
| 642 | if (fProxyContext) { |
| 643 | for (int i = 0; i < count; i++) { |
| 644 | if (alpha[i]) { |
| 645 | SkPMColor c = span[i]; |
| 646 | if (c) { |
| 647 | unsigned a = SkGetPackedA32(c); |
| 648 | unsigned r = SkGetPackedR32(c); |
| 649 | unsigned g = SkGetPackedG32(c); |
| 650 | unsigned b = SkGetPackedB32(c); |
| 651 | |
| 652 | unsigned mul = SkAlpha255To256(mulp[i]); |
| 653 | unsigned add = addp[i]; |
| 654 | |
| 655 | r = SkFastMin32(SkAlphaMul(r, mul) + add, a); |
| 656 | g = SkFastMin32(SkAlphaMul(g, mul) + add, a); |
| 657 | b = SkFastMin32(SkAlphaMul(b, mul) + add, a); |
| 658 | |
| 659 | span[i] = SkPackARGB32(a, r, g, b); |
| 660 | } |
| 661 | } else { |
| 662 | span[i] = 0; |
| 663 | } |
| 664 | } |
| 665 | } else { // color |
| 666 | unsigned a = SkGetPackedA32(fPMColor); |
| 667 | unsigned r = SkGetPackedR32(fPMColor); |
| 668 | unsigned g = SkGetPackedG32(fPMColor); |
| 669 | unsigned b = SkGetPackedB32(fPMColor); |
| 670 | for (int i = 0; i < count; i++) { |
| 671 | if (alpha[i]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 672 | unsigned mul = SkAlpha255To256(mulp[i]); |
| 673 | unsigned add = addp[i]; |
| 674 | |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 675 | span[i] = SkPackARGB32( a, |
| 676 | SkFastMin32(SkAlphaMul(r, mul) + add, a), |
| 677 | SkFastMin32(SkAlphaMul(g, mul) + add, a), |
| 678 | SkFastMin32(SkAlphaMul(b, mul) + add, a)); |
| 679 | } else { |
| 680 | span[i] = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 681 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 682 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 683 | } |
| 684 | } |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 685 | |
| 686 | private: |
| 687 | // Unowned. |
| 688 | const SkMask* fMask; |
| 689 | // Memory is unowned, but we need to call the destructor. |
| 690 | SkShader::Context* fProxyContext; |
| 691 | SkPMColor fPMColor; |
| 692 | |
| 693 | typedef SkShader::Context INHERITED; |
| 694 | }; |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 695 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 696 | #ifndef SK_IGNORE_TO_STRING |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 697 | void toString(SkString* str) const override { |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 698 | str->append("Sk3DShader: ("); |
| 699 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 700 | if (fProxy) { |
robertphillips@google.com | 76f9e93 | 2013-01-15 20:17:47 +0000 | [diff] [blame] | 701 | str->append("Proxy: "); |
| 702 | fProxy->toString(str); |
| 703 | } |
| 704 | |
| 705 | this->INHERITED::toString(str); |
| 706 | |
| 707 | str->append(")"); |
| 708 | } |
| 709 | #endif |
| 710 | |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 711 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Sk3DShader) |
| 712 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 713 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 714 | void flatten(SkWriteBuffer& buffer) const override { |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 715 | buffer.writeFlattenable(fProxy.get()); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 716 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 717 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 718 | private: |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 719 | sk_sp<SkShader> fProxy; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 720 | |
| 721 | typedef SkShader INHERITED; |
| 722 | }; |
| 723 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 724 | sk_sp<SkFlattenable> Sk3DShader::CreateProc(SkReadBuffer& buffer) { |
| 725 | return sk_make_sp<Sk3DShader>(buffer.readShader()); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 726 | } |
| 727 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 728 | class Sk3DBlitter : public SkBlitter { |
| 729 | public: |
reed | cc0e311 | 2014-09-10 10:20:24 -0700 | [diff] [blame] | 730 | Sk3DBlitter(SkBlitter* proxy, SkShader::Context* shaderContext) |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 731 | : fProxy(proxy) |
reed | cc0e311 | 2014-09-10 10:20:24 -0700 | [diff] [blame] | 732 | , fShaderContext(shaderContext) |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 733 | {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 734 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 735 | void blitH(int x, int y, int width) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 736 | fProxy->blitH(x, y, width); |
| 737 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 738 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 739 | void blitAntiH(int x, int y, const SkAlpha antialias[], const int16_t runs[]) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 740 | fProxy->blitAntiH(x, y, antialias, runs); |
| 741 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 742 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 743 | void blitV(int x, int y, int height, SkAlpha alpha) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 744 | fProxy->blitV(x, y, height, alpha); |
| 745 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 746 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 747 | void blitRect(int x, int y, int width, int height) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 748 | fProxy->blitRect(x, y, width, height); |
| 749 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 750 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 751 | void blitMask(const SkMask& mask, const SkIRect& clip) override { |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 752 | if (mask.fFormat == SkMask::k3D_Format) { |
reed | cc0e311 | 2014-09-10 10:20:24 -0700 | [diff] [blame] | 753 | fShaderContext->set3DMask(&mask); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 754 | |
| 755 | ((SkMask*)&mask)->fFormat = SkMask::kA8_Format; |
| 756 | fProxy->blitMask(mask, clip); |
| 757 | ((SkMask*)&mask)->fFormat = SkMask::k3D_Format; |
| 758 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 759 | fShaderContext->set3DMask(nullptr); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 760 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 761 | fProxy->blitMask(mask, clip); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 762 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 763 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 764 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 765 | private: |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 766 | // Both pointers are unowned. They will be deleted by SkSmallAllocator. |
reed | cc0e311 | 2014-09-10 10:20:24 -0700 | [diff] [blame] | 767 | SkBlitter* fProxy; |
| 768 | SkShader::Context* fShaderContext; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 769 | }; |
| 770 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 771 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 772 | |
| 773 | #include "SkCoreBlitters.h" |
| 774 | |
fmalita | d0c4e09 | 2016-02-22 17:19:04 -0800 | [diff] [blame] | 775 | SkShader::ContextRec::DstType SkBlitter::PreferredShaderDest(const SkImageInfo& dstInfo) { |
| 776 | #ifdef SK_FORCE_PM4f_FOR_L32_BLITS |
| 777 | return SkShader::ContextRec::kPM4f_DstType; |
| 778 | #else |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 779 | return (dstInfo.gammaCloseToSRGB() || dstInfo.colorType() == kRGBA_F16_SkColorType) |
fmalita | d0c4e09 | 2016-02-22 17:19:04 -0800 | [diff] [blame] | 780 | ? SkShader::ContextRec::kPM4f_DstType |
| 781 | : SkShader::ContextRec::kPMColor_DstType; |
| 782 | #endif |
| 783 | } |
| 784 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 785 | SkBlitter* SkBlitter::Choose(const SkPixmap& device, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 786 | const SkMatrix& matrix, |
reed@google.com | 6b7aee3 | 2011-04-19 18:36:09 +0000 | [diff] [blame] | 787 | const SkPaint& origPaint, |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 788 | SkArenaAlloc* alloc, |
reed@google.com | 126f7f5 | 2013-11-07 16:06:53 +0000 | [diff] [blame] | 789 | bool drawCoverage) { |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 790 | SkASSERT(alloc != nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 791 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 792 | // which check, in case we're being called by a client with a dummy device |
| 793 | // (e.g. they have a bounder that always aborts the draw) |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 794 | if (kUnknown_SkColorType == device.colorType() || |
| 795 | (drawCoverage && (kAlpha_8_SkColorType != device.colorType()))) { |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 796 | return alloc->make<SkNullBlitter>(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 797 | } |
| 798 | |
reed@google.com | 4588490 | 2012-05-11 14:38:51 +0000 | [diff] [blame] | 799 | SkShader* shader = origPaint.getShader(); |
| 800 | SkColorFilter* cf = origPaint.getColorFilter(); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 801 | SkBlendMode mode = origPaint.getBlendMode(); |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 802 | sk_sp<Sk3DShader> shader3D; |
reed@google.com | 4588490 | 2012-05-11 14:38:51 +0000 | [diff] [blame] | 803 | |
bsalomon@google.com | 5dc26b9 | 2012-10-11 19:32:32 +0000 | [diff] [blame] | 804 | SkTCopyOnFirstWrite<SkPaint> paint(origPaint); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 805 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 806 | if (origPaint.getMaskFilter() != nullptr && |
reed@google.com | 4588490 | 2012-05-11 14:38:51 +0000 | [diff] [blame] | 807 | origPaint.getMaskFilter()->getFormat() == SkMask::k3D_Format) { |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 808 | shader3D = sk_make_sp<Sk3DShader>(sk_ref_sp(shader)); |
reed@google.com | 4588490 | 2012-05-11 14:38:51 +0000 | [diff] [blame] | 809 | // we know we haven't initialized lazyPaint yet, so just do it |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 810 | paint.writable()->setShader(shader3D); |
| 811 | shader = shader3D.get(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 812 | } |
| 813 | |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 814 | if (mode != SkBlendMode::kSrcOver) { |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 815 | bool deviceIsOpaque = kRGB_565_SkColorType == device.colorType(); |
| 816 | switch (SkInterpretXfermode(*paint, deviceIsOpaque)) { |
| 817 | case kSrcOver_SkXfermodeInterpretation: |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 818 | mode = SkBlendMode::kSrcOver; |
| 819 | paint.writable()->setBlendMode(mode); |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 820 | break; |
halcanary | a681433 | 2015-05-27 08:53:36 -0700 | [diff] [blame] | 821 | case kSkipDrawing_SkXfermodeInterpretation:{ |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 822 | return alloc->make<SkNullBlitter>(); |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 823 | } |
reed@android.com | d252db0 | 2009-04-01 18:31:44 +0000 | [diff] [blame] | 824 | default: |
| 825 | break; |
| 826 | } |
| 827 | } |
| 828 | |
reed@google.com | 13201e7 | 2012-11-16 13:20:41 +0000 | [diff] [blame] | 829 | /* |
| 830 | * If the xfermode is CLEAR, then we can completely ignore the installed |
| 831 | * color/shader/colorfilter, and just pretend we're SRC + color==0. This |
| 832 | * will fall into our optimizations for SRC mode. |
| 833 | */ |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 834 | if (mode == SkBlendMode::kClear) { |
reed@google.com | 13201e7 | 2012-11-16 13:20:41 +0000 | [diff] [blame] | 835 | SkPaint* p = paint.writable(); |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 836 | p->setShader(nullptr); |
| 837 | shader = nullptr; |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 838 | p->setColorFilter(nullptr); |
| 839 | cf = nullptr; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 840 | p->setBlendMode(mode = SkBlendMode::kSrc); |
reed@google.com | 13201e7 | 2012-11-16 13:20:41 +0000 | [diff] [blame] | 841 | p->setColor(0); |
| 842 | } |
reed@google.com | 13201e7 | 2012-11-16 13:20:41 +0000 | [diff] [blame] | 843 | |
Mike Klein | e71b167 | 2017-01-13 07:59:23 -0500 | [diff] [blame] | 844 | if (kAlpha_8_SkColorType == device.colorType() && drawCoverage) { |
| 845 | SkASSERT(nullptr == shader); |
| 846 | SkASSERT(paint->isSrcOver()); |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 847 | return alloc->make<SkA8_Coverage_Blitter>(device, *paint); |
Mike Klein | e71b167 | 2017-01-13 07:59:23 -0500 | [diff] [blame] | 848 | } |
| 849 | |
Matt Sarett | 25834ff | 2017-02-15 15:54:35 -0500 | [diff] [blame] | 850 | if (SkBlitter* blitter = SkCreateRasterPipelineBlitter(device, *paint, matrix, alloc)) { |
mtklein | 9a5c47f | 2016-07-22 11:05:04 -0700 | [diff] [blame] | 851 | return blitter; |
| 852 | } |
| 853 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 854 | if (nullptr == shader) { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 855 | if (mode != SkBlendMode::kSrcOver) { |
reed@google.com | 6b7aee3 | 2011-04-19 18:36:09 +0000 | [diff] [blame] | 856 | // xfermodes (and filters) require shaders for our current blitters |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 857 | paint.writable()->setShader(SkShader::MakeColorShader(paint->getColor())); |
commit-bot@chromium.org | 76a3b2a | 2014-04-24 16:54:46 +0000 | [diff] [blame] | 858 | paint.writable()->setAlpha(0xFF); |
reed | fe63045 | 2016-03-25 09:08:00 -0700 | [diff] [blame] | 859 | shader = paint->getShader(); |
reed@google.com | 6b7aee3 | 2011-04-19 18:36:09 +0000 | [diff] [blame] | 860 | } else if (cf) { |
| 861 | // if no shader && no xfermode, we just apply the colorfilter to |
| 862 | // our color and move on. |
bsalomon@google.com | 5dc26b9 | 2012-10-11 19:32:32 +0000 | [diff] [blame] | 863 | SkPaint* writablePaint = paint.writable(); |
| 864 | writablePaint->setColor(cf->filterColor(paint->getColor())); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 865 | writablePaint->setColorFilter(nullptr); |
| 866 | cf = nullptr; |
reed@google.com | 6b7aee3 | 2011-04-19 18:36:09 +0000 | [diff] [blame] | 867 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 868 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 869 | |
reed@google.com | 6b7aee3 | 2011-04-19 18:36:09 +0000 | [diff] [blame] | 870 | if (cf) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 871 | SkASSERT(shader); |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 872 | paint.writable()->setShader(shader->makeWithColorFilter(sk_ref_sp(cf))); |
reed | 150835e | 2016-03-10 06:36:49 -0800 | [diff] [blame] | 873 | shader = paint->getShader(); |
reed@android.com | 1fc4c60 | 2009-10-02 16:34:57 +0000 | [diff] [blame] | 874 | // blitters should ignore the presence/absence of a filter, since |
| 875 | // if there is one, the shader will take care of it. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 876 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 877 | |
reed@google.com | a641f3f | 2012-12-13 22:16:30 +0000 | [diff] [blame] | 878 | /* |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 879 | * We create a SkShader::Context object, and store it on the blitter. |
reed@google.com | a641f3f | 2012-12-13 22:16:30 +0000 | [diff] [blame] | 880 | */ |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 881 | SkShader::Context* shaderContext = nullptr; |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 882 | if (shader) { |
fmalita | d0c4e09 | 2016-02-22 17:19:04 -0800 | [diff] [blame] | 883 | const SkShader::ContextRec rec(*paint, matrix, nullptr, |
Brian Osman | 11970e5 | 2016-12-05 15:26:50 -0500 | [diff] [blame] | 884 | PreferredShaderDest(device.info()), |
| 885 | device.colorSpace()); |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 886 | // Try to create the ShaderContext |
| 887 | shaderContext = shader->makeContext(rec, alloc); |
| 888 | if (!shaderContext) { |
| 889 | return alloc->make<SkNullBlitter>(); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 890 | } |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 891 | SkASSERT(shaderContext); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 892 | } |
| 893 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 894 | SkBlitter* blitter = nullptr; |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 895 | switch (device.colorType()) { |
| 896 | case kAlpha_8_SkColorType: |
Mike Klein | e71b167 | 2017-01-13 07:59:23 -0500 | [diff] [blame] | 897 | SkASSERT(!drawCoverage); // Handled above. |
| 898 | if (shader) { |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 899 | blitter = alloc->make<SkA8_Shader_Blitter>(device, *paint, shaderContext); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 900 | } else { |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 901 | blitter = alloc->make<SkA8_Blitter>(device, *paint); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 902 | } |
| 903 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 904 | |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 905 | case kRGB_565_SkColorType: |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 906 | blitter = SkBlitter_ChooseD565(device, *paint, shaderContext, alloc); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 907 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 908 | |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 909 | case kN32_SkColorType: |
reed | a34be68 | 2016-02-15 07:48:35 -0800 | [diff] [blame] | 910 | #ifdef SK_FORCE_PM4f_FOR_L32_BLITS |
| 911 | if (true) |
| 912 | #else |
reed | 960b2d6 | 2016-06-17 09:26:41 -0700 | [diff] [blame] | 913 | if (device.info().gammaCloseToSRGB()) |
reed | a34be68 | 2016-02-15 07:48:35 -0800 | [diff] [blame] | 914 | #endif |
| 915 | { |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 916 | blitter = SkBlitter_ARGB32_Create(device, *paint, shaderContext, alloc); |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 917 | } else { |
reed | a34be68 | 2016-02-15 07:48:35 -0800 | [diff] [blame] | 918 | if (shader) { |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 919 | blitter = alloc->make<SkARGB32_Shader_Blitter>( |
reed | a34be68 | 2016-02-15 07:48:35 -0800 | [diff] [blame] | 920 | device, *paint, shaderContext); |
| 921 | } else if (paint->getColor() == SK_ColorBLACK) { |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 922 | blitter = alloc->make<SkARGB32_Black_Blitter>(device, *paint); |
reed | a34be68 | 2016-02-15 07:48:35 -0800 | [diff] [blame] | 923 | } else if (paint->getAlpha() == 0xFF) { |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 924 | blitter = alloc->make<SkARGB32_Opaque_Blitter>(device, *paint); |
reed | a34be68 | 2016-02-15 07:48:35 -0800 | [diff] [blame] | 925 | } else { |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 926 | blitter = alloc->make<SkARGB32_Blitter>(device, *paint); |
reed | a34be68 | 2016-02-15 07:48:35 -0800 | [diff] [blame] | 927 | } |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 928 | } |
| 929 | break; |
| 930 | |
reed | a34be68 | 2016-02-15 07:48:35 -0800 | [diff] [blame] | 931 | case kRGBA_F16_SkColorType: |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 932 | blitter = SkBlitter_F16_Create(device, *paint, shaderContext, alloc); |
reed | a34be68 | 2016-02-15 07:48:35 -0800 | [diff] [blame] | 933 | break; |
| 934 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 935 | default: |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 936 | break; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 937 | } |
| 938 | |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 939 | if (!blitter) { |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 940 | blitter = alloc->make<SkNullBlitter>(); |
reed | 129ed1c | 2016-02-22 06:42:31 -0800 | [diff] [blame] | 941 | } |
| 942 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 943 | if (shader3D) { |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 944 | SkBlitter* innerBlitter = blitter; |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 945 | // FIXME - comment about allocator |
commit-bot@chromium.org | a5572e5 | 2014-03-07 03:24:41 +0000 | [diff] [blame] | 946 | // innerBlitter was allocated by allocator, which will delete it. |
reed | cc0e311 | 2014-09-10 10:20:24 -0700 | [diff] [blame] | 947 | // We know shaderContext or its proxies is of type Sk3DShaderContext, so we need to |
| 948 | // wrapper the blitter to notify it when we see an emboss mask. |
Herb Derby | 83e939b | 2017-02-07 14:25:11 -0500 | [diff] [blame] | 949 | blitter = alloc->make<Sk3DBlitter>(innerBlitter, shaderContext); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 950 | } |
| 951 | return blitter; |
| 952 | } |
| 953 | |
reed@google.com | 82065d6 | 2011-02-07 15:30:46 +0000 | [diff] [blame] | 954 | /////////////////////////////////////////////////////////////////////////////// |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 955 | |
reed | 56263c7 | 2015-06-05 11:31:26 -0700 | [diff] [blame] | 956 | class SkZeroShaderContext : public SkShader::Context { |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 957 | public: |
reed | 56263c7 | 2015-06-05 11:31:26 -0700 | [diff] [blame] | 958 | SkZeroShaderContext(const SkShader& shader, const SkShader::ContextRec& rec) |
scroggo | ca76c55 | 2014-06-04 08:21:01 -0700 | [diff] [blame] | 959 | // Override rec with the identity matrix, so it is guaranteed to be invertible. |
fmalita | d0c4e09 | 2016-02-22 17:19:04 -0800 | [diff] [blame] | 960 | : INHERITED(shader, SkShader::ContextRec(*rec.fPaint, SkMatrix::I(), nullptr, |
Brian Osman | 11970e5 | 2016-12-05 15:26:50 -0500 | [diff] [blame] | 961 | rec.fPreferredDstType, rec.fDstColorSpace)) {} |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 962 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 963 | void shadeSpan(int x, int y, SkPMColor colors[], int count) override { |
commit-bot@chromium.org | ce56d96 | 2014-05-05 18:39:18 +0000 | [diff] [blame] | 964 | sk_bzero(colors, count * sizeof(SkPMColor)); |
| 965 | } |
| 966 | |
| 967 | private: |
| 968 | typedef SkShader::Context INHERITED; |
| 969 | }; |
| 970 | |
reed | 41e010c | 2015-06-09 12:16:53 -0700 | [diff] [blame] | 971 | SkShaderBlitter::SkShaderBlitter(const SkPixmap& device, const SkPaint& paint, |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 972 | SkShader::Context* shaderContext) |
| 973 | : INHERITED(device) |
| 974 | , fShader(paint.getShader()) |
| 975 | , fShaderContext(shaderContext) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 976 | SkASSERT(fShader); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 977 | SkASSERT(fShaderContext); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 978 | |
| 979 | fShader->ref(); |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 980 | fShaderFlags = fShaderContext->getFlags(); |
reed | a34be68 | 2016-02-15 07:48:35 -0800 | [diff] [blame] | 981 | fConstInY = SkToBool(fShaderFlags & SkShader::kConstInY32_Flag); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 982 | } |
| 983 | |
reed@android.com | 5119bdb | 2009-06-12 21:27:03 +0000 | [diff] [blame] | 984 | SkShaderBlitter::~SkShaderBlitter() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 985 | fShader->unref(); |
| 986 | } |
commit-bot@chromium.org | 87fcd95 | 2014-04-23 19:10:51 +0000 | [diff] [blame] | 987 | |
Mike Reed | 28930b4 | 2016-11-09 15:23:26 -0500 | [diff] [blame] | 988 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 989 | |
| 990 | #ifdef SK_DEBUG |
| 991 | |
| 992 | void SkRectClipCheckBlitter::blitH(int x, int y, int width) { |
| 993 | SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, width, 1))); |
| 994 | fBlitter->blitH(x, y, width); |
| 995 | } |
| 996 | |
| 997 | void SkRectClipCheckBlitter::blitAntiH(int x, int y, const SkAlpha aa[], const int16_t runs[]) { |
| 998 | const int16_t* iter = runs; |
| 999 | for (; *iter; iter += *iter) |
| 1000 | ; |
| 1001 | int width = iter - runs; |
| 1002 | SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, width, 1))); |
| 1003 | fBlitter->blitAntiH(x, y, aa, runs); |
| 1004 | } |
| 1005 | |
| 1006 | void SkRectClipCheckBlitter::blitV(int x, int y, int height, SkAlpha alpha) { |
| 1007 | SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, 1, height))); |
| 1008 | fBlitter->blitV(x, y, height, alpha); |
| 1009 | } |
| 1010 | |
| 1011 | void SkRectClipCheckBlitter::blitRect(int x, int y, int width, int height) { |
| 1012 | SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, width, height))); |
| 1013 | fBlitter->blitRect(x, y, width, height); |
| 1014 | } |
| 1015 | |
| 1016 | void SkRectClipCheckBlitter::blitAntiRect(int x, int y, int width, int height, |
| 1017 | SkAlpha leftAlpha, SkAlpha rightAlpha) { |
Yuqian Li | 8b9a725 | 2016-11-11 09:40:34 -0500 | [diff] [blame] | 1018 | bool skipLeft = !leftAlpha; |
| 1019 | bool skipRight = !rightAlpha; |
| 1020 | SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x + skipLeft, y, |
| 1021 | width + 2 - skipRight - skipLeft, height))); |
Mike Reed | 28930b4 | 2016-11-09 15:23:26 -0500 | [diff] [blame] | 1022 | fBlitter->blitAntiRect(x, y, width, height, leftAlpha, rightAlpha); |
| 1023 | } |
| 1024 | |
| 1025 | void SkRectClipCheckBlitter::blitMask(const SkMask& mask, const SkIRect& clip) { |
| 1026 | SkASSERT(mask.fBounds.contains(clip)); |
| 1027 | SkASSERT(fClipRect.contains(clip)); |
| 1028 | fBlitter->blitMask(mask, clip); |
| 1029 | } |
| 1030 | |
| 1031 | const SkPixmap* SkRectClipCheckBlitter::justAnOpaqueColor(uint32_t* value) { |
| 1032 | return fBlitter->justAnOpaqueColor(value); |
| 1033 | } |
| 1034 | |
Yuqian Li | 99bba9e | 2016-11-21 09:44:59 -0500 | [diff] [blame] | 1035 | void SkRectClipCheckBlitter::blitAntiH2(int x, int y, U8CPU a0, U8CPU a1) { |
| 1036 | SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, 2, 1))); |
| 1037 | fBlitter->blitAntiH2(x, y, a0, a1); |
| 1038 | } |
| 1039 | |
| 1040 | void SkRectClipCheckBlitter::blitAntiV2(int x, int y, U8CPU a0, U8CPU a1) { |
| 1041 | SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x, y, 1, 2))); |
| 1042 | fBlitter->blitAntiV2(x, y, a0, a1); |
| 1043 | } |
| 1044 | |
Mike Reed | 28930b4 | 2016-11-09 15:23:26 -0500 | [diff] [blame] | 1045 | #endif |