blob: a386904dcd80a5c0987e70ca710afc11cc3240ae [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 Derby83e939b2017-02-07 14:25:11 -05008#include "SkArenaAlloc.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +00009#include "SkBlitter.h"
10#include "SkAntiRun.h"
11#include "SkColor.h"
12#include "SkColorFilter.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000013#include "SkReadBuffer.h"
14#include "SkWriteBuffer.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkMask.h"
16#include "SkMaskFilter.h"
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +000017#include "SkString.h"
reed@google.com45884902012-05-11 14:38:51 +000018#include "SkTLazy.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000019#include "SkUtils.h"
halcanarya6814332015-05-27 08:53:36 -070020#include "SkXfermodeInterpretation.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000021
reed395eabe2016-01-30 18:52:31 -080022// define this for testing srgb blits
reeda34be682016-02-15 07:48:35 -080023//#define SK_FORCE_PM4f_FOR_L32_BLITS
reed395eabe2016-01-30 18:52:31 -080024
reed@android.com845fdac2009-06-23 03:01:32 +000025SkBlitter::~SkBlitter() {}
reed@android.com8a1c16f2008-12-17 15:59:43 +000026
reed@google.comea033602012-12-14 13:13:55 +000027bool SkBlitter::isNullBlitter() const { return false; }
28
reed41e010c2015-06-09 12:16:53 -070029const SkPixmap* SkBlitter::justAnOpaqueColor(uint32_t* value) {
halcanary96fcdcc2015-08-27 07:41:13 -070030 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +000031}
32
herb7df9e4a2016-06-10 13:01:27 -070033/*
reed@google.com82065d62011-02-07 15:30:46 +000034void SkBlitter::blitH(int x, int y, int width) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000035 SkDEBUGFAIL("unimplemented");
reed@android.com8a1c16f2008-12-17 15:59:43 +000036}
37
herb7df9e4a2016-06-10 13:01:27 -070038
reed@google.com82065d62011-02-07 15:30:46 +000039void SkBlitter::blitAntiH(int x, int y, const SkAlpha antialias[],
40 const int16_t runs[]) {
tomhudson@google.com0c00f212011-12-28 14:59:50 +000041 SkDEBUGFAIL("unimplemented");
reed@android.com8a1c16f2008-12-17 15:59:43 +000042}
herb7df9e4a2016-06-10 13:01:27 -070043 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000044
reed@google.com82065d62011-02-07 15:30:46 +000045void SkBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
46 if (alpha == 255) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 this->blitRect(x, y, 1, height);
reed@google.com82065d62011-02-07 15:30:46 +000048 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 int16_t runs[2];
50 runs[0] = 1;
51 runs[1] = 0;
52
reed@google.com82065d62011-02-07 15:30:46 +000053 while (--height >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 this->blitAntiH(x, y++, &alpha, runs);
reed@google.com82065d62011-02-07 15:30:46 +000055 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 }
57}
58
reed@google.com82065d62011-02-07 15:30:46 +000059void SkBlitter::blitRect(int x, int y, int width, int height) {
tomhudson@google.coma31ac732011-12-29 16:09:31 +000060 SkASSERT(width > 0);
reed@google.com82065d62011-02-07 15:30:46 +000061 while (--height >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 this->blitH(x, y++, width);
reed@google.com82065d62011-02-07 15:30:46 +000063 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000064}
65
liyuqian38911a72016-10-04 11:23:22 -070066/// Default implementation doesn't check for easy optimizations
67/// such as alpha == 255; also uses blitV(), which some subclasses
tomhudson@google.com49eac192011-12-27 13:59:20 +000068/// may not support.
69void SkBlitter::blitAntiRect(int x, int y, int width, int height,
70 SkAlpha leftAlpha, SkAlpha rightAlpha) {
liyuqian38911a72016-10-04 11:23:22 -070071 if (leftAlpha > 0) { // we may send in x = -1 with leftAlpha = 0
72 this->blitV(x, y, height, leftAlpha);
73 }
74 x++;
tomhudson@google.coma31ac732011-12-29 16:09:31 +000075 if (width > 0) {
tomhudson@google.com47143592011-12-28 17:58:07 +000076 this->blitRect(x, y, width, height);
77 x += width;
78 }
liyuqian38911a72016-10-04 11:23:22 -070079 if (rightAlpha > 0) {
80 this->blitV(x, y, height, rightAlpha);
81 }
tomhudson@google.com49eac192011-12-27 13:59:20 +000082}
83
reed@android.com8a1c16f2008-12-17 15:59:43 +000084//////////////////////////////////////////////////////////////////////////////
85
reed@google.com82065d62011-02-07 15:30:46 +000086static inline void bits_to_runs(SkBlitter* blitter, int x, int y,
87 const uint8_t bits[],
herb5520ded2015-11-18 10:50:33 -080088 uint8_t left_mask, ptrdiff_t rowBytes,
89 uint8_t right_mask) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 int inFill = 0;
91 int pos = 0;
92
reed@google.com82065d62011-02-07 15:30:46 +000093 while (--rowBytes >= 0) {
herb5520ded2015-11-18 10:50:33 -080094 uint8_t b = *bits++ & left_mask;
reed@google.com82065d62011-02-07 15:30:46 +000095 if (rowBytes == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000096 b &= right_mask;
reed@google.com82065d62011-02-07 15:30:46 +000097 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000098
herb5520ded2015-11-18 10:50:33 -080099 for (uint8_t test = 0x80U; test != 0; test >>= 1) {
reed@google.com82065d62011-02-07 15:30:46 +0000100 if (b & test) {
101 if (!inFill) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 pos = x;
103 inFill = true;
104 }
reed@google.com82065d62011-02-07 15:30:46 +0000105 } else {
106 if (inFill) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 blitter->blitH(pos, y, x - pos);
108 inFill = false;
109 }
110 }
111 x += 1;
112 }
herb5520ded2015-11-18 10:50:33 -0800113 left_mask = 0xFFU;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000114 }
115
116 // final cleanup
reed@google.com82065d62011-02-07 15:30:46 +0000117 if (inFill) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 blitter->blitH(pos, y, x - pos);
reed@google.com82065d62011-02-07 15:30:46 +0000119 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120}
121
herb5520ded2015-11-18 10:50:33 -0800122// maskBitCount is the number of 1's to place in the mask. It must be in the range between 1 and 8.
123static uint8_t generate_right_mask(int maskBitCount) {
124 return static_cast<uint8_t>(0xFF00U >> maskBitCount);
125}
126
reed@google.com82065d62011-02-07 15:30:46 +0000127void SkBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000128 SkASSERT(mask.fBounds.contains(clip));
129
reedd96e7e52016-02-17 07:15:29 -0800130 if (mask.fFormat == SkMask::kLCD16_Format) {
131 return; // needs to be handled by subclass
132 }
133
reed@google.com82065d62011-02-07 15:30:46 +0000134 if (mask.fFormat == SkMask::kBW_Format) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 int cx = clip.fLeft;
136 int cy = clip.fTop;
137 int maskLeft = mask.fBounds.fLeft;
herb5520ded2015-11-18 10:50:33 -0800138 int maskRowBytes = mask.fRowBytes;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000139 int height = clip.height();
140
141 const uint8_t* bits = mask.getAddr1(cx, cy);
142
herb5520ded2015-11-18 10:50:33 -0800143 SkDEBUGCODE(const uint8_t* endOfImage =
144 mask.fImage + (mask.fBounds.height() - 1) * maskRowBytes
145 + ((mask.fBounds.width() + 7) >> 3));
146
reed@google.com82065d62011-02-07 15:30:46 +0000147 if (cx == maskLeft && clip.fRight == mask.fBounds.fRight) {
148 while (--height >= 0) {
herb5520ded2015-11-18 10:50:33 -0800149 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.com8a1c16f2008-12-17 15:59:43 +0000155 cy += 1;
156 }
reed@google.com82065d62011-02-07 15:30:46 +0000157 } else {
herb3be72b02016-06-24 13:02:31 -0700158 // Bits is calculated as the offset into the mask at the point {cx, cy} therefore, all
herb5520ded2015-11-18 10:50:33 -0800159 // 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.com8a1c16f2008-12-17 15:59:43 +0000162
herb5520ded2015-11-18 10:50:33 -0800163 // 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.com8a1c16f2008-12-17 15:59:43 +0000168
herb5520ded2015-11-18 10:50:33 -0800169 // Calculate left byte and mask
170 const uint8_t* leftByte = bits;
171 U8CPU leftMask = 0xFFU >> (leftEdge & 7);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172
herb5520ded2015-11-18 10:50:33 -0800173 // 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.com8a1c16f2008-12-17 15:59:43 +0000177
herb5520ded2015-11-18 10:50:33 -0800178 // 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.com8a1c16f2008-12-17 15:59:43 +0000187 }
188 }
reed@google.com82065d62011-02-07 15:30:46 +0000189 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190 int width = clip.width();
191 SkAutoSTMalloc<64, int16_t> runStorage(width + 1);
192 int16_t* runs = runStorage.get();
reed@google.com79891862011-10-18 15:44:57 +0000193 const uint8_t* aa = mask.getAddr8(clip.fLeft, clip.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000194
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.com82065d62011-02-07 15:30:46 +0000200 while (--height >= 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201 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
210void SkBlitter::blitMaskRegion(const SkMask& mask, const SkRegion& clip) {
211 if (clip.quickReject(mask.fBounds)) {
212 return;
213 }
reed@google.com82065d62011-02-07 15:30:46 +0000214
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215 SkRegion::Cliperator clipper(clip, mask.fBounds);
reed@google.com82065d62011-02-07 15:30:46 +0000216
reed@android.com8a1c16f2008-12-17 15:59:43 +0000217 while (!clipper.done()) {
218 const SkIRect& cr = clipper.rect();
219 this->blitMask(mask, cr);
220 clipper.next();
221 }
222}
223
224void SkBlitter::blitRectRegion(const SkIRect& rect, const SkRegion& clip) {
225 SkRegion::Cliperator clipper(clip, rect);
reed@google.com82065d62011-02-07 15:30:46 +0000226
reed@android.com8a1c16f2008-12-17 15:59:43 +0000227 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
234void SkBlitter::blitRegion(const SkRegion& clip) {
235 SkRegion::Iterator iter(clip);
reed@google.com82065d62011-02-07 15:30:46 +0000236
reed@android.com8a1c16f2008-12-17 15:59:43 +0000237 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.com82065d62011-02-07 15:30:46 +0000244///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000245
reed@google.com82065d62011-02-07 15:30:46 +0000246void SkNullBlitter::blitH(int x, int y, int width) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000247
reed@google.com82065d62011-02-07 15:30:46 +0000248void SkNullBlitter::blitAntiH(int x, int y, const SkAlpha antialias[],
249 const int16_t runs[]) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000250
reed@google.com82065d62011-02-07 15:30:46 +0000251void SkNullBlitter::blitV(int x, int y, int height, SkAlpha alpha) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000252
reed@google.com82065d62011-02-07 15:30:46 +0000253void SkNullBlitter::blitRect(int x, int y, int width, int height) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000254
reed@google.com82065d62011-02-07 15:30:46 +0000255void SkNullBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256
reed41e010c2015-06-09 12:16:53 -0700257const SkPixmap* SkNullBlitter::justAnOpaqueColor(uint32_t* value) {
halcanary96fcdcc2015-08-27 07:41:13 -0700258 return nullptr;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259}
260
reed@google.comea033602012-12-14 13:13:55 +0000261bool SkNullBlitter::isNullBlitter() const { return true; }
262
reed@google.com82065d62011-02-07 15:30:46 +0000263///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000264
reed@google.com82065d62011-02-07 15:30:46 +0000265static int compute_anti_width(const int16_t runs[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000266 int width = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000267
268 for (;;) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000269 int count = runs[0];
reed@google.com82065d62011-02-07 15:30:46 +0000270
reed@android.com8a1c16f2008-12-17 15:59:43 +0000271 SkASSERT(count >= 0);
reed@google.com82065d62011-02-07 15:30:46 +0000272 if (count == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000273 break;
reed@google.com82065d62011-02-07 15:30:46 +0000274 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000275 width += count;
276 runs += count;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000277 }
278 return width;
279}
280
reed@google.com82065d62011-02-07 15:30:46 +0000281static inline bool y_in_rect(int y, const SkIRect& rect) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000282 return (unsigned)(y - rect.fTop) < (unsigned)rect.height();
283}
284
reed@google.com82065d62011-02-07 15:30:46 +0000285static inline bool x_in_rect(int x, const SkIRect& rect) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000286 return (unsigned)(x - rect.fLeft) < (unsigned)rect.width();
287}
288
reed@google.com82065d62011-02-07 15:30:46 +0000289void SkRectClipBlitter::blitH(int left, int y, int width) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000290 SkASSERT(width > 0);
291
reed@google.com82065d62011-02-07 15:30:46 +0000292 if (!y_in_rect(y, fClipRect)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000293 return;
reed@google.com82065d62011-02-07 15:30:46 +0000294 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000295
296 int right = left + width;
297
reed@google.com82065d62011-02-07 15:30:46 +0000298 if (left < fClipRect.fLeft) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000299 left = fClipRect.fLeft;
reed@google.com82065d62011-02-07 15:30:46 +0000300 }
301 if (right > fClipRect.fRight) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000302 right = fClipRect.fRight;
reed@google.com82065d62011-02-07 15:30:46 +0000303 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000304
305 width = right - left;
reed@google.com82065d62011-02-07 15:30:46 +0000306 if (width > 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000307 fBlitter->blitH(left, y, width);
reed@google.com82065d62011-02-07 15:30:46 +0000308 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000309}
310
reed@google.com82065d62011-02-07 15:30:46 +0000311void 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.com8a1c16f2008-12-17 15:59:43 +0000314 return;
reed@google.com82065d62011-02-07 15:30:46 +0000315 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000316
317 int x0 = left;
318 int x1 = left + compute_anti_width(runs);
319
reed@google.com82065d62011-02-07 15:30:46 +0000320 if (x1 <= fClipRect.fLeft) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000321 return;
reed@google.com82065d62011-02-07 15:30:46 +0000322 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000323
324 SkASSERT(x0 < x1);
reed@google.com82065d62011-02-07 15:30:46 +0000325 if (x0 < fClipRect.fLeft) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000326 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.com82065d62011-02-07 15:30:46 +0000334 if (x1 > fClipRect.fRight) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000335 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.com82065d62011-02-07 15:30:46 +0000346void SkRectClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000347 SkASSERT(height > 0);
348
reed@google.com82065d62011-02-07 15:30:46 +0000349 if (!x_in_rect(x, fClipRect)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000350 return;
reed@google.com82065d62011-02-07 15:30:46 +0000351 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000352
353 int y0 = y;
354 int y1 = y + height;
355
reed@google.com82065d62011-02-07 15:30:46 +0000356 if (y0 < fClipRect.fTop) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000357 y0 = fClipRect.fTop;
reed@google.com82065d62011-02-07 15:30:46 +0000358 }
359 if (y1 > fClipRect.fBottom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000360 y1 = fClipRect.fBottom;
reed@google.com82065d62011-02-07 15:30:46 +0000361 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000362
reed@google.com82065d62011-02-07 15:30:46 +0000363 if (y0 < y1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000364 fBlitter->blitV(x, y0, y1 - y0, alpha);
reed@google.com82065d62011-02-07 15:30:46 +0000365 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000366}
367
reed@google.com82065d62011-02-07 15:30:46 +0000368void SkRectClipBlitter::blitRect(int left, int y, int width, int height) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000369 SkIRect r;
370
371 r.set(left, y, left + width, y + height);
reed@google.com82065d62011-02-07 15:30:46 +0000372 if (r.intersect(fClipRect)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000373 fBlitter->blitRect(r.fLeft, r.fTop, r.width(), r.height());
reed@google.com82065d62011-02-07 15:30:46 +0000374 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000375}
376
tomhudson@google.com49eac192011-12-27 13:59:20 +0000377void 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.com82065d62011-02-07 15:30:46 +0000408void SkRectClipBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000409 SkASSERT(mask.fBounds.contains(clip));
410
411 SkIRect r = clip;
412
reed@google.com82065d62011-02-07 15:30:46 +0000413 if (r.intersect(fClipRect)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000414 fBlitter->blitMask(mask, r);
reed@google.com82065d62011-02-07 15:30:46 +0000415 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000416}
417
reed41e010c2015-06-09 12:16:53 -0700418const SkPixmap* SkRectClipBlitter::justAnOpaqueColor(uint32_t* value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000419 return fBlitter->justAnOpaqueColor(value);
420}
421
reed@google.com82065d62011-02-07 15:30:46 +0000422///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000423
reed@google.com82065d62011-02-07 15:30:46 +0000424void SkRgnClipBlitter::blitH(int x, int y, int width) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000425 SkRegion::Spanerator span(*fRgn, y, x, x + width);
426 int left, right;
427
reed@google.com82065d62011-02-07 15:30:46 +0000428 while (span.next(&left, &right)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000429 SkASSERT(left < right);
430 fBlitter->blitH(left, y, right - left);
431 }
432}
433
reed@google.com82065d62011-02-07 15:30:46 +0000434void SkRgnClipBlitter::blitAntiH(int x, int y, const SkAlpha aa[],
435 const int16_t runs[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000436 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.com82065d62011-02-07 15:30:46 +0000440
reed@android.com8a1c16f2008-12-17 15:59:43 +0000441 int prevRite = x;
reed@google.com82065d62011-02-07 15:30:46 +0000442 while (span.next(&left, &right)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000443 SkASSERT(x <= left);
444 SkASSERT(left < right);
445 SkASSERT(left >= bounds.fLeft && right <= bounds.fRight);
reed@google.com82065d62011-02-07 15:30:46 +0000446
reed@android.com8a1c16f2008-12-17 15:59:43 +0000447 SkAlphaRuns::Break((int16_t*)runs, (uint8_t*)aa, left - x, right - left);
448
449 // now zero before left
reed@google.com82065d62011-02-07 15:30:46 +0000450 if (left > prevRite) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000451 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.com82065d62011-02-07 15:30:46 +0000455
reed@android.com8a1c16f2008-12-17 15:59:43 +0000456 prevRite = right;
457 }
reed@google.com82065d62011-02-07 15:30:46 +0000458
459 if (prevRite > x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000460 ((int16_t*)runs)[prevRite - x] = 0;
reed@google.com82065d62011-02-07 15:30:46 +0000461
reed@android.com8a1c16f2008-12-17 15:59:43 +0000462 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.com82065d62011-02-07 15:30:46 +0000473void SkRgnClipBlitter::blitV(int x, int y, int height, SkAlpha alpha) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000474 SkIRect bounds;
475 bounds.set(x, y, x + 1, y + height);
476
477 SkRegion::Cliperator iter(*fRgn, bounds);
478
reed@google.com82065d62011-02-07 15:30:46 +0000479 while (!iter.done()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000480 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.com82065d62011-02-07 15:30:46 +0000488void SkRgnClipBlitter::blitRect(int x, int y, int width, int height) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000489 SkIRect bounds;
490 bounds.set(x, y, x + width, y + height);
491
492 SkRegion::Cliperator iter(*fRgn, bounds);
493
reed@google.com82065d62011-02-07 15:30:46 +0000494 while (!iter.done()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000495 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.com49eac192011-12-27 13:59:20 +0000503void 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.com31bab392012-01-03 20:12:42 +0000515 SkASSERT(r.fRight <= x + width + 2);
tomhudson@google.com49eac192011-12-27 13:59:20 +0000516
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.comfbfcd562012-08-23 18:09:54 +0000525 fBlitter->blitV(r.fLeft, r.fTop, r.height(),
tomhudson@google.com49eac192011-12-27 13:59:20 +0000526 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.com82065d62011-02-07 15:30:46 +0000541void SkRgnClipBlitter::blitMask(const SkMask& mask, const SkIRect& clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000542 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.com82065d62011-02-07 15:30:46 +0000548 while (!iter.done()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000549 blitter->blitMask(mask, r);
550 iter.next();
551 }
552}
553
reed41e010c2015-06-09 12:16:53 -0700554const SkPixmap* SkRgnClipBlitter::justAnOpaqueColor(uint32_t* value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000555 return fBlitter->justAnOpaqueColor(value);
556}
557
reed@google.com82065d62011-02-07 15:30:46 +0000558///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000559
reed@google.com82065d62011-02-07 15:30:46 +0000560SkBlitter* SkBlitterClipper::apply(SkBlitter* blitter, const SkRegion* clip,
561 const SkIRect* ir) {
562 if (clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000563 const SkIRect& clipR = clip->getBounds();
564
reed@google.com82065d62011-02-07 15:30:46 +0000565 if (clip->isEmpty() || (ir && !SkIRect::Intersects(clipR, *ir))) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000566 blitter = &fNullBlitter;
reed@google.com82065d62011-02-07 15:30:46 +0000567 } else if (clip->isRect()) {
halcanary96fcdcc2015-08-27 07:41:13 -0700568 if (ir == nullptr || !clipR.contains(*ir)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000569 fRectBlitter.init(blitter, clipR);
570 blitter = &fRectBlitter;
571 }
reed@google.com82065d62011-02-07 15:30:46 +0000572 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000573 fRgnBlitter.init(blitter, clip);
574 blitter = &fRgnBlitter;
575 }
576 }
577 return blitter;
578}
579
reed@google.com82065d62011-02-07 15:30:46 +0000580///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000581
582#include "SkColorShader.h"
583#include "SkColorPriv.h"
584
585class Sk3DShader : public SkShader {
586public:
reed8a21c9f2016-03-08 18:50:00 -0800587 Sk3DShader(sk_sp<SkShader> proxy) : fProxy(std::move(proxy)) {}
reed@google.com82065d62011-02-07 15:30:46 +0000588
Herb Derby83e939b2017-02-07 14:25:11 -0500589 Context* onMakeContext(const ContextRec& rec, SkArenaAlloc* alloc) const override {
halcanary96fcdcc2015-08-27 07:41:13 -0700590 SkShader::Context* proxyContext = nullptr;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000591 if (fProxy) {
Herb Derby83e939b2017-02-07 14:25:11 -0500592 proxyContext = fProxy->makeContext(rec, alloc);
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000593 if (!proxyContext) {
halcanary96fcdcc2015-08-27 07:41:13 -0700594 return nullptr;
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000595 }
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000596 }
Herb Derby83e939b2017-02-07 14:25:11 -0500597 return alloc->make<Sk3DShaderContext>(*this, rec, proxyContext);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000598 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000599
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000600 class Sk3DShaderContext : public SkShader::Context {
601 public:
602 // Calls proxyContext's destructor but will NOT free its memory.
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000603 Sk3DShaderContext(const Sk3DShader& shader, const ContextRec& rec,
604 SkShader::Context* proxyContext)
605 : INHERITED(shader, rec)
halcanary96fcdcc2015-08-27 07:41:13 -0700606 , fMask(nullptr)
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000607 , fProxyContext(proxyContext)
608 {
609 if (!fProxyContext) {
commit-bot@chromium.orge901b6d2014-05-01 19:31:31 +0000610 fPMColor = SkPreMultiplyColor(rec.fPaint->getColor());
commit-bot@chromium.orgbc2f1dc2014-04-23 09:11:58 +0000611 }
612 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000613
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000614 virtual ~Sk3DShaderContext() {
615 if (fProxyContext) {
616 fProxyContext->~Context();
617 }
618 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000619
mtklein36352bf2015-03-25 18:17:31 -0700620 void set3DMask(const SkMask* mask) override { fMask = mask; }
commit-bot@chromium.org001f4ed2014-04-16 10:16:39 +0000621
mtklein36352bf2015-03-25 18:17:31 -0700622 void shadeSpan(int x, int y, SkPMColor span[], int count) override {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000623 if (fProxyContext) {
624 fProxyContext->shadeSpan(x, y, span, count);
625 }
commit-bot@chromium.orgbc2f1dc2014-04-23 09:11:58 +0000626
halcanary96fcdcc2015-08-27 07:41:13 -0700627 if (fMask == nullptr) {
628 if (fProxyContext == nullptr) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000629 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.com8a1c16f2008-12-17 15:59:43 +0000672 unsigned mul = SkAlpha255To256(mulp[i]);
673 unsigned add = addp[i];
674
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000675 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.com8a1c16f2008-12-17 15:59:43 +0000681 }
reed@google.com82065d62011-02-07 15:30:46 +0000682 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000683 }
684 }
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000685
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.com82065d62011-02-07 15:30:46 +0000695
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000696#ifndef SK_IGNORE_TO_STRING
mtklein36352bf2015-03-25 18:17:31 -0700697 void toString(SkString* str) const override {
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000698 str->append("Sk3DShader: (");
699
bsalomon49f085d2014-09-05 13:34:00 -0700700 if (fProxy) {
robertphillips@google.com76f9e932013-01-15 20:17:47 +0000701 str->append("Proxy: ");
702 fProxy->toString(str);
703 }
704
705 this->INHERITED::toString(str);
706
707 str->append(")");
708 }
709#endif
710
djsollen@google.comba28d032012-03-26 17:57:35 +0000711 SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(Sk3DShader)
712
reed@android.com8a1c16f2008-12-17 15:59:43 +0000713protected:
mtklein36352bf2015-03-25 18:17:31 -0700714 void flatten(SkWriteBuffer& buffer) const override {
reed8a21c9f2016-03-08 18:50:00 -0800715 buffer.writeFlattenable(fProxy.get());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000716 }
reed@google.com82065d62011-02-07 15:30:46 +0000717
reed@android.com8a1c16f2008-12-17 15:59:43 +0000718private:
reed8a21c9f2016-03-08 18:50:00 -0800719 sk_sp<SkShader> fProxy;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000720
721 typedef SkShader INHERITED;
722};
723
reed60c9b582016-04-03 09:11:13 -0700724sk_sp<SkFlattenable> Sk3DShader::CreateProc(SkReadBuffer& buffer) {
725 return sk_make_sp<Sk3DShader>(buffer.readShader());
reed9fa60da2014-08-21 07:59:51 -0700726}
727
reed@android.com8a1c16f2008-12-17 15:59:43 +0000728class Sk3DBlitter : public SkBlitter {
729public:
reedcc0e3112014-09-10 10:20:24 -0700730 Sk3DBlitter(SkBlitter* proxy, SkShader::Context* shaderContext)
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +0000731 : fProxy(proxy)
reedcc0e3112014-09-10 10:20:24 -0700732 , fShaderContext(shaderContext)
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +0000733 {}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000734
mtklein36352bf2015-03-25 18:17:31 -0700735 void blitH(int x, int y, int width) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000736 fProxy->blitH(x, y, width);
737 }
reed@google.com82065d62011-02-07 15:30:46 +0000738
reed60c9b582016-04-03 09:11:13 -0700739 void blitAntiH(int x, int y, const SkAlpha antialias[], const int16_t runs[]) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000740 fProxy->blitAntiH(x, y, antialias, runs);
741 }
reed@google.com82065d62011-02-07 15:30:46 +0000742
mtklein36352bf2015-03-25 18:17:31 -0700743 void blitV(int x, int y, int height, SkAlpha alpha) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000744 fProxy->blitV(x, y, height, alpha);
745 }
reed@google.com82065d62011-02-07 15:30:46 +0000746
mtklein36352bf2015-03-25 18:17:31 -0700747 void blitRect(int x, int y, int width, int height) override {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000748 fProxy->blitRect(x, y, width, height);
749 }
reed@google.com82065d62011-02-07 15:30:46 +0000750
mtklein36352bf2015-03-25 18:17:31 -0700751 void blitMask(const SkMask& mask, const SkIRect& clip) override {
reed@google.com82065d62011-02-07 15:30:46 +0000752 if (mask.fFormat == SkMask::k3D_Format) {
reedcc0e3112014-09-10 10:20:24 -0700753 fShaderContext->set3DMask(&mask);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000754
755 ((SkMask*)&mask)->fFormat = SkMask::kA8_Format;
756 fProxy->blitMask(mask, clip);
757 ((SkMask*)&mask)->fFormat = SkMask::k3D_Format;
758
halcanary96fcdcc2015-08-27 07:41:13 -0700759 fShaderContext->set3DMask(nullptr);
reed@google.com82065d62011-02-07 15:30:46 +0000760 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000761 fProxy->blitMask(mask, clip);
reed@google.com82065d62011-02-07 15:30:46 +0000762 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000763 }
reed@google.com82065d62011-02-07 15:30:46 +0000764
reed@android.com8a1c16f2008-12-17 15:59:43 +0000765private:
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000766 // Both pointers are unowned. They will be deleted by SkSmallAllocator.
reedcc0e3112014-09-10 10:20:24 -0700767 SkBlitter* fProxy;
768 SkShader::Context* fShaderContext;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000769};
770
reed@google.com82065d62011-02-07 15:30:46 +0000771///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000772
773#include "SkCoreBlitters.h"
774
fmalitad0c4e092016-02-22 17:19:04 -0800775SkShader::ContextRec::DstType SkBlitter::PreferredShaderDest(const SkImageInfo& dstInfo) {
776#ifdef SK_FORCE_PM4f_FOR_L32_BLITS
777 return SkShader::ContextRec::kPM4f_DstType;
778#else
reed960b2d62016-06-17 09:26:41 -0700779 return (dstInfo.gammaCloseToSRGB() || dstInfo.colorType() == kRGBA_F16_SkColorType)
fmalitad0c4e092016-02-22 17:19:04 -0800780 ? SkShader::ContextRec::kPM4f_DstType
781 : SkShader::ContextRec::kPMColor_DstType;
782#endif
783}
784
reed41e010c2015-06-09 12:16:53 -0700785SkBlitter* SkBlitter::Choose(const SkPixmap& device,
reed@android.com8a1c16f2008-12-17 15:59:43 +0000786 const SkMatrix& matrix,
reed@google.com6b7aee32011-04-19 18:36:09 +0000787 const SkPaint& origPaint,
Herb Derby83e939b2017-02-07 14:25:11 -0500788 SkArenaAlloc* alloc,
reed@google.com126f7f52013-11-07 16:06:53 +0000789 bool drawCoverage) {
Herb Derby83e939b2017-02-07 14:25:11 -0500790 SkASSERT(alloc != nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000791
reed@android.com8a1c16f2008-12-17 15:59:43 +0000792 // 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.com900ecf22014-02-20 20:55:37 +0000794 if (kUnknown_SkColorType == device.colorType() ||
795 (drawCoverage && (kAlpha_8_SkColorType != device.colorType()))) {
Herb Derby83e939b2017-02-07 14:25:11 -0500796 return alloc->make<SkNullBlitter>();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000797 }
798
reed@google.com45884902012-05-11 14:38:51 +0000799 SkShader* shader = origPaint.getShader();
800 SkColorFilter* cf = origPaint.getColorFilter();
reed374772b2016-10-05 17:33:02 -0700801 SkBlendMode mode = origPaint.getBlendMode();
reedfe630452016-03-25 09:08:00 -0700802 sk_sp<Sk3DShader> shader3D;
reed@google.com45884902012-05-11 14:38:51 +0000803
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000804 SkTCopyOnFirstWrite<SkPaint> paint(origPaint);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000805
halcanary96fcdcc2015-08-27 07:41:13 -0700806 if (origPaint.getMaskFilter() != nullptr &&
reed@google.com45884902012-05-11 14:38:51 +0000807 origPaint.getMaskFilter()->getFormat() == SkMask::k3D_Format) {
reedfe630452016-03-25 09:08:00 -0700808 shader3D = sk_make_sp<Sk3DShader>(sk_ref_sp(shader));
reed@google.com45884902012-05-11 14:38:51 +0000809 // we know we haven't initialized lazyPaint yet, so just do it
reedfe630452016-03-25 09:08:00 -0700810 paint.writable()->setShader(shader3D);
811 shader = shader3D.get();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000812 }
813
reed374772b2016-10-05 17:33:02 -0700814 if (mode != SkBlendMode::kSrcOver) {
halcanarya6814332015-05-27 08:53:36 -0700815 bool deviceIsOpaque = kRGB_565_SkColorType == device.colorType();
816 switch (SkInterpretXfermode(*paint, deviceIsOpaque)) {
817 case kSrcOver_SkXfermodeInterpretation:
reed374772b2016-10-05 17:33:02 -0700818 mode = SkBlendMode::kSrcOver;
819 paint.writable()->setBlendMode(mode);
reed@android.comd252db02009-04-01 18:31:44 +0000820 break;
halcanarya6814332015-05-27 08:53:36 -0700821 case kSkipDrawing_SkXfermodeInterpretation:{
Herb Derby83e939b2017-02-07 14:25:11 -0500822 return alloc->make<SkNullBlitter>();
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +0000823 }
reed@android.comd252db02009-04-01 18:31:44 +0000824 default:
825 break;
826 }
827 }
828
reed@google.com13201e72012-11-16 13:20:41 +0000829 /*
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 */
reed374772b2016-10-05 17:33:02 -0700834 if (mode == SkBlendMode::kClear) {
reed@google.com13201e72012-11-16 13:20:41 +0000835 SkPaint* p = paint.writable();
reedfe630452016-03-25 09:08:00 -0700836 p->setShader(nullptr);
837 shader = nullptr;
reedd053ce92016-03-22 10:17:23 -0700838 p->setColorFilter(nullptr);
839 cf = nullptr;
reed374772b2016-10-05 17:33:02 -0700840 p->setBlendMode(mode = SkBlendMode::kSrc);
reed@google.com13201e72012-11-16 13:20:41 +0000841 p->setColor(0);
842 }
reed@google.com13201e72012-11-16 13:20:41 +0000843
Mike Kleine71b1672017-01-13 07:59:23 -0500844 if (kAlpha_8_SkColorType == device.colorType() && drawCoverage) {
845 SkASSERT(nullptr == shader);
846 SkASSERT(paint->isSrcOver());
Herb Derby83e939b2017-02-07 14:25:11 -0500847 return alloc->make<SkA8_Coverage_Blitter>(device, *paint);
Mike Kleine71b1672017-01-13 07:59:23 -0500848 }
849
Matt Sarett25834ff2017-02-15 15:54:35 -0500850 if (SkBlitter* blitter = SkCreateRasterPipelineBlitter(device, *paint, matrix, alloc)) {
mtklein9a5c47f2016-07-22 11:05:04 -0700851 return blitter;
852 }
853
halcanary96fcdcc2015-08-27 07:41:13 -0700854 if (nullptr == shader) {
reed374772b2016-10-05 17:33:02 -0700855 if (mode != SkBlendMode::kSrcOver) {
reed@google.com6b7aee32011-04-19 18:36:09 +0000856 // xfermodes (and filters) require shaders for our current blitters
reedfe630452016-03-25 09:08:00 -0700857 paint.writable()->setShader(SkShader::MakeColorShader(paint->getColor()));
commit-bot@chromium.org76a3b2a2014-04-24 16:54:46 +0000858 paint.writable()->setAlpha(0xFF);
reedfe630452016-03-25 09:08:00 -0700859 shader = paint->getShader();
reed@google.com6b7aee32011-04-19 18:36:09 +0000860 } else if (cf) {
861 // if no shader && no xfermode, we just apply the colorfilter to
862 // our color and move on.
bsalomon@google.com5dc26b92012-10-11 19:32:32 +0000863 SkPaint* writablePaint = paint.writable();
864 writablePaint->setColor(cf->filterColor(paint->getColor()));
halcanary96fcdcc2015-08-27 07:41:13 -0700865 writablePaint->setColorFilter(nullptr);
866 cf = nullptr;
reed@google.com6b7aee32011-04-19 18:36:09 +0000867 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000868 }
reed@google.com82065d62011-02-07 15:30:46 +0000869
reed@google.com6b7aee32011-04-19 18:36:09 +0000870 if (cf) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000871 SkASSERT(shader);
reedd053ce92016-03-22 10:17:23 -0700872 paint.writable()->setShader(shader->makeWithColorFilter(sk_ref_sp(cf)));
reed150835e2016-03-10 06:36:49 -0800873 shader = paint->getShader();
reed@android.com1fc4c602009-10-02 16:34:57 +0000874 // blitters should ignore the presence/absence of a filter, since
875 // if there is one, the shader will take care of it.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000876 }
reed@google.com82065d62011-02-07 15:30:46 +0000877
reed@google.coma641f3f2012-12-13 22:16:30 +0000878 /*
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000879 * We create a SkShader::Context object, and store it on the blitter.
reed@google.coma641f3f2012-12-13 22:16:30 +0000880 */
halcanary96fcdcc2015-08-27 07:41:13 -0700881 SkShader::Context* shaderContext = nullptr;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000882 if (shader) {
fmalitad0c4e092016-02-22 17:19:04 -0800883 const SkShader::ContextRec rec(*paint, matrix, nullptr,
Brian Osman11970e52016-12-05 15:26:50 -0500884 PreferredShaderDest(device.info()),
885 device.colorSpace());
Herb Derby83e939b2017-02-07 14:25:11 -0500886 // Try to create the ShaderContext
887 shaderContext = shader->makeContext(rec, alloc);
888 if (!shaderContext) {
889 return alloc->make<SkNullBlitter>();
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000890 }
Herb Derby83e939b2017-02-07 14:25:11 -0500891 SkASSERT(shaderContext);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000892 }
893
halcanary96fcdcc2015-08-27 07:41:13 -0700894 SkBlitter* blitter = nullptr;
reed@google.com900ecf22014-02-20 20:55:37 +0000895 switch (device.colorType()) {
896 case kAlpha_8_SkColorType:
Mike Kleine71b1672017-01-13 07:59:23 -0500897 SkASSERT(!drawCoverage); // Handled above.
898 if (shader) {
Herb Derby83e939b2017-02-07 14:25:11 -0500899 blitter = alloc->make<SkA8_Shader_Blitter>(device, *paint, shaderContext);
reed@google.com82065d62011-02-07 15:30:46 +0000900 } else {
Herb Derby83e939b2017-02-07 14:25:11 -0500901 blitter = alloc->make<SkA8_Blitter>(device, *paint);
reed@google.com82065d62011-02-07 15:30:46 +0000902 }
903 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000904
reed@google.com900ecf22014-02-20 20:55:37 +0000905 case kRGB_565_SkColorType:
Herb Derby83e939b2017-02-07 14:25:11 -0500906 blitter = SkBlitter_ChooseD565(device, *paint, shaderContext, alloc);
reed@google.com82065d62011-02-07 15:30:46 +0000907 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000908
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000909 case kN32_SkColorType:
reeda34be682016-02-15 07:48:35 -0800910#ifdef SK_FORCE_PM4f_FOR_L32_BLITS
911 if (true)
912#else
reed960b2d62016-06-17 09:26:41 -0700913 if (device.info().gammaCloseToSRGB())
reeda34be682016-02-15 07:48:35 -0800914#endif
915 {
Herb Derby83e939b2017-02-07 14:25:11 -0500916 blitter = SkBlitter_ARGB32_Create(device, *paint, shaderContext, alloc);
reed@google.com82065d62011-02-07 15:30:46 +0000917 } else {
reeda34be682016-02-15 07:48:35 -0800918 if (shader) {
Herb Derby83e939b2017-02-07 14:25:11 -0500919 blitter = alloc->make<SkARGB32_Shader_Blitter>(
reeda34be682016-02-15 07:48:35 -0800920 device, *paint, shaderContext);
921 } else if (paint->getColor() == SK_ColorBLACK) {
Herb Derby83e939b2017-02-07 14:25:11 -0500922 blitter = alloc->make<SkARGB32_Black_Blitter>(device, *paint);
reeda34be682016-02-15 07:48:35 -0800923 } else if (paint->getAlpha() == 0xFF) {
Herb Derby83e939b2017-02-07 14:25:11 -0500924 blitter = alloc->make<SkARGB32_Opaque_Blitter>(device, *paint);
reeda34be682016-02-15 07:48:35 -0800925 } else {
Herb Derby83e939b2017-02-07 14:25:11 -0500926 blitter = alloc->make<SkARGB32_Blitter>(device, *paint);
reeda34be682016-02-15 07:48:35 -0800927 }
reed@google.com82065d62011-02-07 15:30:46 +0000928 }
929 break;
930
reeda34be682016-02-15 07:48:35 -0800931 case kRGBA_F16_SkColorType:
Herb Derby83e939b2017-02-07 14:25:11 -0500932 blitter = SkBlitter_F16_Create(device, *paint, shaderContext, alloc);
reeda34be682016-02-15 07:48:35 -0800933 break;
934
reed@google.com82065d62011-02-07 15:30:46 +0000935 default:
reed@google.com82065d62011-02-07 15:30:46 +0000936 break;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000937 }
938
reed129ed1c2016-02-22 06:42:31 -0800939 if (!blitter) {
Herb Derby83e939b2017-02-07 14:25:11 -0500940 blitter = alloc->make<SkNullBlitter>();
reed129ed1c2016-02-22 06:42:31 -0800941 }
942
reed@google.com82065d62011-02-07 15:30:46 +0000943 if (shader3D) {
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +0000944 SkBlitter* innerBlitter = blitter;
Herb Derby83e939b2017-02-07 14:25:11 -0500945 // FIXME - comment about allocator
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +0000946 // innerBlitter was allocated by allocator, which will delete it.
reedcc0e3112014-09-10 10:20:24 -0700947 // 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 Derby83e939b2017-02-07 14:25:11 -0500949 blitter = alloc->make<Sk3DBlitter>(innerBlitter, shaderContext);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000950 }
951 return blitter;
952}
953
reed@google.com82065d62011-02-07 15:30:46 +0000954///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000955
reed56263c72015-06-05 11:31:26 -0700956class SkZeroShaderContext : public SkShader::Context {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000957public:
reed56263c72015-06-05 11:31:26 -0700958 SkZeroShaderContext(const SkShader& shader, const SkShader::ContextRec& rec)
scroggoca76c552014-06-04 08:21:01 -0700959 // Override rec with the identity matrix, so it is guaranteed to be invertible.
fmalitad0c4e092016-02-22 17:19:04 -0800960 : INHERITED(shader, SkShader::ContextRec(*rec.fPaint, SkMatrix::I(), nullptr,
Brian Osman11970e52016-12-05 15:26:50 -0500961 rec.fPreferredDstType, rec.fDstColorSpace)) {}
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000962
mtklein36352bf2015-03-25 18:17:31 -0700963 void shadeSpan(int x, int y, SkPMColor colors[], int count) override {
commit-bot@chromium.orgce56d962014-05-05 18:39:18 +0000964 sk_bzero(colors, count * sizeof(SkPMColor));
965 }
966
967private:
968 typedef SkShader::Context INHERITED;
969};
970
reed41e010c2015-06-09 12:16:53 -0700971SkShaderBlitter::SkShaderBlitter(const SkPixmap& device, const SkPaint& paint,
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000972 SkShader::Context* shaderContext)
973 : INHERITED(device)
974 , fShader(paint.getShader())
975 , fShaderContext(shaderContext) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000976 SkASSERT(fShader);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000977 SkASSERT(fShaderContext);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000978
979 fShader->ref();
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000980 fShaderFlags = fShaderContext->getFlags();
reeda34be682016-02-15 07:48:35 -0800981 fConstInY = SkToBool(fShaderFlags & SkShader::kConstInY32_Flag);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000982}
983
reed@android.com5119bdb2009-06-12 21:27:03 +0000984SkShaderBlitter::~SkShaderBlitter() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000985 fShader->unref();
986}
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000987
Mike Reed28930b42016-11-09 15:23:26 -0500988///////////////////////////////////////////////////////////////////////////////////////////////////
989
990#ifdef SK_DEBUG
991
992void 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
997void 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
1006void 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
1011void 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
1016void SkRectClipCheckBlitter::blitAntiRect(int x, int y, int width, int height,
1017 SkAlpha leftAlpha, SkAlpha rightAlpha) {
Yuqian Li8b9a7252016-11-11 09:40:34 -05001018 bool skipLeft = !leftAlpha;
1019 bool skipRight = !rightAlpha;
1020 SkASSERT(fClipRect.contains(SkIRect::MakeXYWH(x + skipLeft, y,
1021 width + 2 - skipRight - skipLeft, height)));
Mike Reed28930b42016-11-09 15:23:26 -05001022 fBlitter->blitAntiRect(x, y, width, height, leftAlpha, rightAlpha);
1023}
1024
1025void 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
1031const SkPixmap* SkRectClipCheckBlitter::justAnOpaqueColor(uint32_t* value) {
1032 return fBlitter->justAnOpaqueColor(value);
1033}
1034
Yuqian Li99bba9e2016-11-21 09:44:59 -05001035void 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
1040void 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 Reed28930b42016-11-09 15:23:26 -05001045#endif