blob: a91c0df0dee826f37c0ddde540f73acf23449303 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
reed@android.com8a1c16f2008-12-17 15:59:43 +00009
10#include "SkBlitRow.h"
11#include "SkCoreBlitters.h"
12#include "SkColorPriv.h"
13#include "SkDither.h"
14#include "SkShader.h"
15#include "SkUtils.h"
djsollen@google.come552dc82014-03-21 13:13:22 +000016#include "SkUtilsArm.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017#include "SkXfermode.h"
18
djordje.pesut6336f7c2014-07-14 07:48:11 -070019#if SK_MIPS_HAS_DSP
20extern void blitmask_d565_opaque_mips(int width, int height, uint16_t* device,
21 unsigned deviceRB, const uint8_t* alpha,
22 uint32_t expanded32, unsigned maskRB);
23#endif
24
commit-bot@chromium.org89351ec2014-03-20 18:37:06 +000025#if SK_ARM_NEON_IS_ALWAYS && defined(SK_CPU_LENDIAN)
reed@android.com867ee802009-10-20 13:55:41 +000026 #include <arm_neon.h>
27#else
28 // if we don't have neon, then our black blitter is worth the extra code
29 #define USE_BLACK_BLITTER
30#endif
31
reed@android.com8a1c16f2008-12-17 15:59:43 +000032void sk_dither_memset16(uint16_t dst[], uint16_t value, uint16_t other,
33 int count) {
34 if (count > 0) {
35 // see if we need to write one short before we can cast to an 4byte ptr
36 // (we do this subtract rather than (unsigned)dst so we don't get warnings
37 // on 64bit machines)
38 if (((char*)dst - (char*)0) & 2) {
39 *dst++ = value;
40 count -= 1;
41 SkTSwap(value, other);
42 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000043
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 // fast way to set [value,other] pairs
45#ifdef SK_CPU_BENDIAN
46 sk_memset32((uint32_t*)dst, (value << 16) | other, count >> 1);
47#else
48 sk_memset32((uint32_t*)dst, (other << 16) | value, count >> 1);
49#endif
rmistry@google.comfbfcd562012-08-23 18:09:54 +000050
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 if (count & 1) {
52 dst[count - 1] = value;
53 }
54 }
55}
56
57///////////////////////////////////////////////////////////////////////////////
58
reed@android.com1fc4c602009-10-02 16:34:57 +000059class SkRGB16_Blitter : public SkRasterBlitter {
60public:
61 SkRGB16_Blitter(const SkBitmap& device, const SkPaint& paint);
62 virtual void blitH(int x, int y, int width);
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000063 virtual void blitAntiH(int x, int y, const SkAlpha* antialias,
64 const int16_t* runs);
reed@android.com1fc4c602009-10-02 16:34:57 +000065 virtual void blitV(int x, int y, int height, SkAlpha alpha);
66 virtual void blitRect(int x, int y, int width, int height);
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000067 virtual void blitMask(const SkMask&,
68 const SkIRect&);
reed@android.com1fc4c602009-10-02 16:34:57 +000069 virtual const SkBitmap* justAnOpaqueColor(uint32_t*);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000070
reed@android.com1fc4c602009-10-02 16:34:57 +000071protected:
72 SkPMColor fSrcColor32;
reed@android.comea16cfa2009-10-02 19:11:02 +000073 uint32_t fExpandedRaw16;
reed@android.com1fc4c602009-10-02 16:34:57 +000074 unsigned fScale;
75 uint16_t fColor16; // already scaled by fScale
76 uint16_t fRawColor16; // unscaled
77 uint16_t fRawDither16; // unscaled
78 SkBool8 fDoDither;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000079
reed@android.com1fc4c602009-10-02 16:34:57 +000080 // illegal
81 SkRGB16_Blitter& operator=(const SkRGB16_Blitter&);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000082
reed@android.com1fc4c602009-10-02 16:34:57 +000083 typedef SkRasterBlitter INHERITED;
84};
85
86class SkRGB16_Opaque_Blitter : public SkRGB16_Blitter {
87public:
88 SkRGB16_Opaque_Blitter(const SkBitmap& device, const SkPaint& paint);
89 virtual void blitH(int x, int y, int width);
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000090 virtual void blitAntiH(int x, int y, const SkAlpha* antialias,
91 const int16_t* runs);
reed@android.comea16cfa2009-10-02 19:11:02 +000092 virtual void blitV(int x, int y, int height, SkAlpha alpha);
reed@android.com1fc4c602009-10-02 16:34:57 +000093 virtual void blitRect(int x, int y, int width, int height);
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +000094 virtual void blitMask(const SkMask&,
95 const SkIRect&);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000096
reed@android.com1fc4c602009-10-02 16:34:57 +000097private:
98 typedef SkRGB16_Blitter INHERITED;
99};
100
reed@android.com867ee802009-10-20 13:55:41 +0000101#ifdef USE_BLACK_BLITTER
reed@android.com1fc4c602009-10-02 16:34:57 +0000102class SkRGB16_Black_Blitter : public SkRGB16_Opaque_Blitter {
103public:
104 SkRGB16_Black_Blitter(const SkBitmap& device, const SkPaint& paint);
105 virtual void blitMask(const SkMask&, const SkIRect&);
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +0000106 virtual void blitAntiH(int x, int y, const SkAlpha* antialias,
107 const int16_t* runs);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000108
reed@android.com1fc4c602009-10-02 16:34:57 +0000109private:
110 typedef SkRGB16_Opaque_Blitter INHERITED;
111};
reed@android.com867ee802009-10-20 13:55:41 +0000112#endif
reed@android.com1fc4c602009-10-02 16:34:57 +0000113
114class SkRGB16_Shader_Blitter : public SkShaderBlitter {
115public:
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000116 SkRGB16_Shader_Blitter(const SkBitmap& device, const SkPaint& paint,
117 SkShader::Context* shaderContext);
reed@android.com1fc4c602009-10-02 16:34:57 +0000118 virtual ~SkRGB16_Shader_Blitter();
119 virtual void blitH(int x, int y, int width);
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +0000120 virtual void blitAntiH(int x, int y, const SkAlpha* antialias,
121 const int16_t* runs);
reed@android.com1fc4c602009-10-02 16:34:57 +0000122 virtual void blitRect(int x, int y, int width, int height);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000123
reed@android.com1fc4c602009-10-02 16:34:57 +0000124protected:
125 SkPMColor* fBuffer;
126 SkBlitRow::Proc fOpaqueProc;
127 SkBlitRow::Proc fAlphaProc;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000128
reed@android.com1fc4c602009-10-02 16:34:57 +0000129private:
130 // illegal
131 SkRGB16_Shader_Blitter& operator=(const SkRGB16_Shader_Blitter&);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000132
reed@android.com1fc4c602009-10-02 16:34:57 +0000133 typedef SkShaderBlitter INHERITED;
134};
135
136// used only if the shader can perform shadSpan16
137class SkRGB16_Shader16_Blitter : public SkRGB16_Shader_Blitter {
138public:
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000139 SkRGB16_Shader16_Blitter(const SkBitmap& device, const SkPaint& paint,
140 SkShader::Context* shaderContext);
reed@android.com1fc4c602009-10-02 16:34:57 +0000141 virtual void blitH(int x, int y, int width);
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +0000142 virtual void blitAntiH(int x, int y, const SkAlpha* antialias,
143 const int16_t* runs);
reed@android.com1fc4c602009-10-02 16:34:57 +0000144 virtual void blitRect(int x, int y, int width, int height);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000145
reed@android.com1fc4c602009-10-02 16:34:57 +0000146private:
147 typedef SkRGB16_Shader_Blitter INHERITED;
148};
149
150class SkRGB16_Shader_Xfermode_Blitter : public SkShaderBlitter {
151public:
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000152 SkRGB16_Shader_Xfermode_Blitter(const SkBitmap& device, const SkPaint& paint,
153 SkShader::Context* shaderContext);
reed@android.com1fc4c602009-10-02 16:34:57 +0000154 virtual ~SkRGB16_Shader_Xfermode_Blitter();
155 virtual void blitH(int x, int y, int width);
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +0000156 virtual void blitAntiH(int x, int y, const SkAlpha* antialias,
157 const int16_t* runs);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000158
reed@android.com1fc4c602009-10-02 16:34:57 +0000159private:
160 SkXfermode* fXfermode;
161 SkPMColor* fBuffer;
162 uint8_t* fAAExpand;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000163
reed@android.com1fc4c602009-10-02 16:34:57 +0000164 // illegal
165 SkRGB16_Shader_Xfermode_Blitter& operator=(const SkRGB16_Shader_Xfermode_Blitter&);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000166
reed@android.com1fc4c602009-10-02 16:34:57 +0000167 typedef SkShaderBlitter INHERITED;
168};
169
170///////////////////////////////////////////////////////////////////////////////
reed@android.com867ee802009-10-20 13:55:41 +0000171#ifdef USE_BLACK_BLITTER
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172SkRGB16_Black_Blitter::SkRGB16_Black_Blitter(const SkBitmap& device, const SkPaint& paint)
reed@android.comdafaf7a2009-07-10 03:05:59 +0000173 : INHERITED(device, paint) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000174 SkASSERT(paint.getShader() == NULL);
175 SkASSERT(paint.getColorFilter() == NULL);
176 SkASSERT(paint.getXfermode() == NULL);
177 SkASSERT(paint.getColor() == SK_ColorBLACK);
178}
179
180#if 1
181#define black_8_pixels(mask, dst) \
182 do { \
183 if (mask & 0x80) dst[0] = 0; \
184 if (mask & 0x40) dst[1] = 0; \
185 if (mask & 0x20) dst[2] = 0; \
186 if (mask & 0x10) dst[3] = 0; \
187 if (mask & 0x08) dst[4] = 0; \
188 if (mask & 0x04) dst[5] = 0; \
189 if (mask & 0x02) dst[6] = 0; \
190 if (mask & 0x01) dst[7] = 0; \
191 } while (0)
192#else
193static inline black_8_pixels(U8CPU mask, uint16_t dst[])
194{
195 if (mask & 0x80) dst[0] = 0;
196 if (mask & 0x40) dst[1] = 0;
197 if (mask & 0x20) dst[2] = 0;
198 if (mask & 0x10) dst[3] = 0;
199 if (mask & 0x08) dst[4] = 0;
200 if (mask & 0x04) dst[5] = 0;
201 if (mask & 0x02) dst[6] = 0;
202 if (mask & 0x01) dst[7] = 0;
203}
204#endif
205
206#define SK_BLITBWMASK_NAME SkRGB16_Black_BlitBW
207#define SK_BLITBWMASK_ARGS
208#define SK_BLITBWMASK_BLIT8(mask, dst) black_8_pixels(mask, dst)
209#define SK_BLITBWMASK_GETADDR getAddr16
210#define SK_BLITBWMASK_DEVTYPE uint16_t
211#include "SkBlitBWMaskTemplate.h"
212
tomhudson@google.com333d6cb2011-07-12 19:19:03 +0000213void SkRGB16_Black_Blitter::blitMask(const SkMask& mask,
214 const SkIRect& clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000215 if (mask.fFormat == SkMask::kBW_Format) {
216 SkRGB16_Black_BlitBW(fDevice, mask, clip);
217 } else {
218 uint16_t* SK_RESTRICT device = fDevice.getAddr16(clip.fLeft, clip.fTop);
reed@google.com79891862011-10-18 15:44:57 +0000219 const uint8_t* SK_RESTRICT alpha = mask.getAddr8(clip.fLeft, clip.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000220 unsigned width = clip.width();
221 unsigned height = clip.height();
scroggo@google.come5f48242013-02-25 21:47:41 +0000222 size_t deviceRB = fDevice.rowBytes() - (width << 1);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000223 unsigned maskRB = mask.fRowBytes - width;
224
225 SkASSERT((int)height > 0);
226 SkASSERT((int)width > 0);
227 SkASSERT((int)deviceRB >= 0);
228 SkASSERT((int)maskRB >= 0);
229
230 do {
231 unsigned w = width;
232 do {
233 unsigned aa = *alpha++;
234 *device = SkAlphaMulRGB16(*device, SkAlpha255To256(255 - aa));
235 device += 1;
236 } while (--w != 0);
237 device = (uint16_t*)((char*)device + deviceRB);
238 alpha += maskRB;
239 } while (--height != 0);
240 }
241}
242
243void SkRGB16_Black_Blitter::blitAntiH(int x, int y,
244 const SkAlpha* SK_RESTRICT antialias,
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +0000245 const int16_t* SK_RESTRICT runs) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000246 uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y);
247
248 for (;;) {
249 int count = runs[0];
250 SkASSERT(count >= 0);
251 if (count <= 0) {
252 return;
253 }
254 runs += count;
255
256 unsigned aa = antialias[0];
257 antialias += count;
258 if (aa) {
259 if (aa == 255) {
260 memset(device, 0, count << 1);
261 } else {
262 aa = SkAlpha255To256(255 - aa);
263 do {
264 *device = SkAlphaMulRGB16(*device, aa);
265 device += 1;
266 } while (--count != 0);
267 continue;
268 }
269 }
270 device += count;
271 }
272}
reed@android.com867ee802009-10-20 13:55:41 +0000273#endif
reed@android.com8a1c16f2008-12-17 15:59:43 +0000274
reed@android.com9a74d312009-06-15 15:56:35 +0000275///////////////////////////////////////////////////////////////////////////////
276///////////////////////////////////////////////////////////////////////////////
277
278SkRGB16_Opaque_Blitter::SkRGB16_Opaque_Blitter(const SkBitmap& device,
279 const SkPaint& paint)
280: INHERITED(device, paint) {}
281
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +0000282void SkRGB16_Opaque_Blitter::blitH(int x, int y, int width) {
reed@android.com9a74d312009-06-15 15:56:35 +0000283 SkASSERT(width > 0);
284 SkASSERT(x + width <= fDevice.width());
285 uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y);
286 uint16_t srcColor = fColor16;
reed@android.com31d1c642009-06-15 18:45:19 +0000287
reed@android.com9a74d312009-06-15 15:56:35 +0000288 SkASSERT(fRawColor16 == srcColor);
289 if (fDoDither) {
290 uint16_t ditherColor = fRawDither16;
291 if ((x ^ y) & 1) {
292 SkTSwap(ditherColor, srcColor);
293 }
294 sk_dither_memset16(device, srcColor, ditherColor, width);
295 } else {
296 sk_memset16(device, srcColor, width);
297 }
298}
299
300// return 1 or 0 from a bool
reed@android.com63debae2009-12-16 17:25:43 +0000301static inline int Bool2Int(int value) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000302 return !!value;
reed@android.com9a74d312009-06-15 15:56:35 +0000303}
304
305void SkRGB16_Opaque_Blitter::blitAntiH(int x, int y,
306 const SkAlpha* SK_RESTRICT antialias,
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +0000307 const int16_t* SK_RESTRICT runs) {
reed@android.com9a74d312009-06-15 15:56:35 +0000308 uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y);
309 uint16_t srcColor = fRawColor16;
reed@android.comea16cfa2009-10-02 19:11:02 +0000310 uint32_t srcExpanded = fExpandedRaw16;
reed@android.com9a74d312009-06-15 15:56:35 +0000311 int ditherInt = Bool2Int(fDoDither);
reed@android.com9a74d312009-06-15 15:56:35 +0000312 uint16_t ditherColor = fRawDither16;
313 // if we have no dithering, this will always fail
314 if ((x ^ y) & ditherInt) {
315 SkTSwap(ditherColor, srcColor);
316 }
317 for (;;) {
318 int count = runs[0];
319 SkASSERT(count >= 0);
320 if (count <= 0) {
321 return;
322 }
323 runs += count;
reed@android.com31d1c642009-06-15 18:45:19 +0000324
reed@android.com9a74d312009-06-15 15:56:35 +0000325 unsigned aa = antialias[0];
326 antialias += count;
327 if (aa) {
328 if (aa == 255) {
329 if (ditherInt) {
330 sk_dither_memset16(device, srcColor,
331 ditherColor, count);
332 } else {
333 sk_memset16(device, srcColor, count);
334 }
335 } else {
336 // TODO: respect fDoDither
337 unsigned scale5 = SkAlpha255To256(aa) >> 3;
reed@android.comea16cfa2009-10-02 19:11:02 +0000338 uint32_t src32 = srcExpanded * scale5;
reed@android.com9a74d312009-06-15 15:56:35 +0000339 scale5 = 32 - scale5; // now we can use it on the device
340 int n = count;
341 do {
342 uint32_t dst32 = SkExpand_rgb_16(*device) * scale5;
343 *device++ = SkCompact_rgb_16((src32 + dst32) >> 5);
344 } while (--n != 0);
345 goto DONE;
346 }
347 }
348 device += count;
reed@android.com31d1c642009-06-15 18:45:19 +0000349
reed@android.com9a74d312009-06-15 15:56:35 +0000350 DONE:
351 // if we have no dithering, this will always fail
352 if (count & ditherInt) {
353 SkTSwap(ditherColor, srcColor);
354 }
355 }
356}
357
358#define solid_8_pixels(mask, dst, color) \
359 do { \
360 if (mask & 0x80) dst[0] = color; \
361 if (mask & 0x40) dst[1] = color; \
362 if (mask & 0x20) dst[2] = color; \
363 if (mask & 0x10) dst[3] = color; \
364 if (mask & 0x08) dst[4] = color; \
365 if (mask & 0x04) dst[5] = color; \
366 if (mask & 0x02) dst[6] = color; \
367 if (mask & 0x01) dst[7] = color; \
368 } while (0)
369
370#define SK_BLITBWMASK_NAME SkRGB16_BlitBW
371#define SK_BLITBWMASK_ARGS , uint16_t color
372#define SK_BLITBWMASK_BLIT8(mask, dst) solid_8_pixels(mask, dst, color)
373#define SK_BLITBWMASK_GETADDR getAddr16
374#define SK_BLITBWMASK_DEVTYPE uint16_t
375#include "SkBlitBWMaskTemplate.h"
376
djordje.pesut6336f7c2014-07-14 07:48:11 -0700377#if !defined(SK_MIPS_HAS_DSP)
reed@android.com9a74d312009-06-15 15:56:35 +0000378static U16CPU blend_compact(uint32_t src32, uint32_t dst32, unsigned scale5) {
379 return SkCompact_rgb_16(dst32 + ((src32 - dst32) * scale5 >> 5));
380}
djordje.pesut6336f7c2014-07-14 07:48:11 -0700381#endif
reed@android.com9a74d312009-06-15 15:56:35 +0000382
tomhudson@google.com333d6cb2011-07-12 19:19:03 +0000383void SkRGB16_Opaque_Blitter::blitMask(const SkMask& mask,
384 const SkIRect& clip) {
reed@android.com9a74d312009-06-15 15:56:35 +0000385 if (mask.fFormat == SkMask::kBW_Format) {
386 SkRGB16_BlitBW(fDevice, mask, clip, fColor16);
387 return;
388 }
reed@android.com31d1c642009-06-15 18:45:19 +0000389
reed@android.com9a74d312009-06-15 15:56:35 +0000390 uint16_t* SK_RESTRICT device = fDevice.getAddr16(clip.fLeft, clip.fTop);
reed@google.com79891862011-10-18 15:44:57 +0000391 const uint8_t* SK_RESTRICT alpha = mask.getAddr8(clip.fLeft, clip.fTop);
reed@android.com9a74d312009-06-15 15:56:35 +0000392 int width = clip.width();
393 int height = clip.height();
scroggo@google.come5f48242013-02-25 21:47:41 +0000394 size_t deviceRB = fDevice.rowBytes() - (width << 1);
reed@android.com9a74d312009-06-15 15:56:35 +0000395 unsigned maskRB = mask.fRowBytes - width;
reed@android.comea16cfa2009-10-02 19:11:02 +0000396 uint32_t expanded32 = fExpandedRaw16;
reed@android.com31d1c642009-06-15 18:45:19 +0000397
commit-bot@chromium.org89351ec2014-03-20 18:37:06 +0000398#if SK_ARM_NEON_IS_ALWAYS && defined(SK_CPU_LENDIAN)
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000399#define UNROLL 8
reed@android.com867ee802009-10-20 13:55:41 +0000400 do {
401 int w = width;
402 if (w >= UNROLL) {
commit-bot@chromium.org641a2492013-08-08 10:51:45 +0000403 uint32x4_t color, dev_lo, dev_hi;
404 uint32x4_t wn1, wn2, tmp;
405 uint32x4_t vmask_g16, vmask_ng16;
406 uint16x8_t valpha, vdev;
407 uint16x4_t odev_lo, odev_hi, valpha_lo, valpha_hi;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000408
commit-bot@chromium.org641a2492013-08-08 10:51:45 +0000409 // prepare constants
410 vmask_g16 = vdupq_n_u32(SK_G16_MASK_IN_PLACE);
411 vmask_ng16 = vdupq_n_u32(~SK_G16_MASK_IN_PLACE);
reed@android.com867ee802009-10-20 13:55:41 +0000412 color = vdupq_n_u32(expanded32);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000413
reed@android.com867ee802009-10-20 13:55:41 +0000414 do {
commit-bot@chromium.org641a2492013-08-08 10:51:45 +0000415 // alpha is 8x8, widen and split to get a pair of 16x4
416 valpha = vaddw_u8(vdupq_n_u16(1), vld1_u8(alpha));
417 valpha = vshrq_n_u16(valpha, 3);
418 valpha_lo = vget_low_u16(valpha);
419 valpha_hi = vget_high_u16(valpha);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000420
commit-bot@chromium.org641a2492013-08-08 10:51:45 +0000421 // load pixels
422 vdev = vld1q_u16(device);
423 dev_lo = vmovl_u16(vget_low_u16(vdev));
424 dev_hi = vmovl_u16(vget_high_u16(vdev));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000425
commit-bot@chromium.org641a2492013-08-08 10:51:45 +0000426 // unpack them in 32 bits
427 dev_lo = (dev_lo & vmask_ng16) | vshlq_n_u32(dev_lo & vmask_g16, 16);
428 dev_hi = (dev_hi & vmask_ng16) | vshlq_n_u32(dev_hi & vmask_g16, 16);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000429
commit-bot@chromium.org641a2492013-08-08 10:51:45 +0000430 // blend with color
431 tmp = (color - dev_lo) * vmovl_u16(valpha_lo);
432 tmp = vshrq_n_u32(tmp, 5);
433 dev_lo += tmp;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000434
commit-bot@chromium.org641a2492013-08-08 10:51:45 +0000435 tmp = vmulq_u32(color - dev_hi, vmovl_u16(valpha_hi));
436 tmp = vshrq_n_u32(tmp, 5);
437 dev_hi += tmp;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000438
commit-bot@chromium.org641a2492013-08-08 10:51:45 +0000439 // re-compact
440 wn1 = dev_lo & vmask_ng16;
441 wn2 = vshrq_n_u32(dev_lo, 16) & vmask_g16;
442 odev_lo = vmovn_u32(wn1 | wn2);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000443
commit-bot@chromium.org641a2492013-08-08 10:51:45 +0000444 wn1 = dev_hi & vmask_ng16;
445 wn2 = vshrq_n_u32(dev_hi, 16) & vmask_g16;
446 odev_hi = vmovn_u32(wn1 | wn2);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000447
commit-bot@chromium.org641a2492013-08-08 10:51:45 +0000448 // store
449 vst1q_u16(device, vcombine_u16(odev_lo, odev_hi));
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000450
reed@android.com867ee802009-10-20 13:55:41 +0000451 device += UNROLL;
452 alpha += UNROLL;
453 w -= UNROLL;
454 } while (w >= UNROLL);
455 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000456
commit-bot@chromium.org641a2492013-08-08 10:51:45 +0000457 // residuals
reed@android.com867ee802009-10-20 13:55:41 +0000458 while (w > 0) {
459 *device = blend_compact(expanded32, SkExpand_rgb_16(*device),
460 SkAlpha255To256(*alpha++) >> 3);
461 device += 1;
462 --w;
463 }
464 device = (uint16_t*)((char*)device + deviceRB);
465 alpha += maskRB;
466 } while (--height != 0);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000467#undef UNROLL
djordje.pesut6336f7c2014-07-14 07:48:11 -0700468#elif SK_MIPS_HAS_DSP
469 blitmask_d565_opaque_mips(width, height, device, deviceRB, alpha, expanded32, maskRB);
reed@android.com867ee802009-10-20 13:55:41 +0000470#else // non-neon code
reed@android.com9a74d312009-06-15 15:56:35 +0000471 do {
472 int w = width;
473 do {
reed@android.comea16cfa2009-10-02 19:11:02 +0000474 *device = blend_compact(expanded32, SkExpand_rgb_16(*device),
reed@android.com9a74d312009-06-15 15:56:35 +0000475 SkAlpha255To256(*alpha++) >> 3);
476 device += 1;
477 } while (--w != 0);
478 device = (uint16_t*)((char*)device + deviceRB);
479 alpha += maskRB;
480 } while (--height != 0);
reed@android.com867ee802009-10-20 13:55:41 +0000481#endif
reed@android.com9a74d312009-06-15 15:56:35 +0000482}
483
reed@android.comea16cfa2009-10-02 19:11:02 +0000484void SkRGB16_Opaque_Blitter::blitV(int x, int y, int height, SkAlpha alpha) {
485 uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y);
scroggo@google.come5f48242013-02-25 21:47:41 +0000486 size_t deviceRB = fDevice.rowBytes();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000487
reed@android.comea16cfa2009-10-02 19:11:02 +0000488 // TODO: respect fDoDither
489 unsigned scale5 = SkAlpha255To256(alpha) >> 3;
490 uint32_t src32 = fExpandedRaw16 * scale5;
491 scale5 = 32 - scale5;
492 do {
493 uint32_t dst32 = SkExpand_rgb_16(*device) * scale5;
494 *device = SkCompact_rgb_16((src32 + dst32) >> 5);
495 device = (uint16_t*)((char*)device + deviceRB);
496 } while (--height != 0);
497}
498
reed@android.com9a74d312009-06-15 15:56:35 +0000499void SkRGB16_Opaque_Blitter::blitRect(int x, int y, int width, int height) {
500 SkASSERT(x + width <= fDevice.width() && y + height <= fDevice.height());
501 uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y);
scroggo@google.come5f48242013-02-25 21:47:41 +0000502 size_t deviceRB = fDevice.rowBytes();
reed@android.com9a74d312009-06-15 15:56:35 +0000503 uint16_t color16 = fColor16;
reed@android.com31d1c642009-06-15 18:45:19 +0000504
reed@android.com9a74d312009-06-15 15:56:35 +0000505 if (fDoDither) {
506 uint16_t ditherColor = fRawDither16;
507 if ((x ^ y) & 1) {
508 SkTSwap(ditherColor, color16);
509 }
510 while (--height >= 0) {
511 sk_dither_memset16(device, color16, ditherColor, width);
512 SkTSwap(ditherColor, color16);
513 device = (uint16_t*)((char*)device + deviceRB);
514 }
515 } else { // no dither
516 while (--height >= 0) {
517 sk_memset16(device, color16, width);
518 device = (uint16_t*)((char*)device + deviceRB);
519 }
520 }
521}
522
523///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000524
525SkRGB16_Blitter::SkRGB16_Blitter(const SkBitmap& device, const SkPaint& paint)
526 : INHERITED(device) {
527 SkColor color = paint.getColor();
528
529 fSrcColor32 = SkPreMultiplyColor(color);
530 fScale = SkAlpha255To256(SkColorGetA(color));
531
532 int r = SkColorGetR(color);
533 int g = SkColorGetG(color);
534 int b = SkColorGetB(color);
535
536 fRawColor16 = fRawDither16 = SkPack888ToRGB16(r, g, b);
537 // if we're dithered, use fRawDither16 to hold that.
538 if ((fDoDither = paint.isDither()) != false) {
539 fRawDither16 = SkDitherPack888ToRGB16(r, g, b);
540 }
reed@android.comea16cfa2009-10-02 19:11:02 +0000541
542 fExpandedRaw16 = SkExpand_rgb_16(fRawColor16);
543
reed@android.com8a1c16f2008-12-17 15:59:43 +0000544 fColor16 = SkPackRGB16( SkAlphaMul(r, fScale) >> (8 - SK_R16_BITS),
545 SkAlphaMul(g, fScale) >> (8 - SK_G16_BITS),
546 SkAlphaMul(b, fScale) >> (8 - SK_B16_BITS));
547}
548
549const SkBitmap* SkRGB16_Blitter::justAnOpaqueColor(uint32_t* value) {
550 if (!fDoDither && 256 == fScale) {
551 *value = fRawColor16;
552 return &fDevice;
553 }
554 return NULL;
555}
556
reed@android.comdafaf7a2009-07-10 03:05:59 +0000557static uint32_t pmcolor_to_expand16(SkPMColor c) {
558 unsigned r = SkGetPackedR32(c);
559 unsigned g = SkGetPackedG32(c);
560 unsigned b = SkGetPackedB32(c);
561 return (g << 24) | (r << 13) | (b << 2);
562}
563
564static inline void blend32_16_row(SkPMColor src, uint16_t dst[], int count) {
565 SkASSERT(count > 0);
reed@android.comdafaf7a2009-07-10 03:05:59 +0000566 uint32_t src_expand = pmcolor_to_expand16(src);
reed@android.comb602b8e2009-07-10 15:58:53 +0000567 unsigned scale = SkAlpha255To256(0xFF - SkGetPackedA32(src)) >> 3;
reed@android.comdafaf7a2009-07-10 03:05:59 +0000568 do {
569 uint32_t dst_expand = SkExpand_rgb_16(*dst) * scale;
570 *dst = SkCompact_rgb_16((src_expand + dst_expand) >> 5);
571 dst += 1;
572 } while (--count != 0);
reed@android.comdafaf7a2009-07-10 03:05:59 +0000573}
574
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +0000575void SkRGB16_Blitter::blitH(int x, int y, int width) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000576 SkASSERT(width > 0);
577 SkASSERT(x + width <= fDevice.width());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000578 uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000579
reed@android.com9a74d312009-06-15 15:56:35 +0000580 // TODO: respect fDoDither
reed@android.comdafaf7a2009-07-10 03:05:59 +0000581 blend32_16_row(fSrcColor32, device, width);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000582}
583
584void SkRGB16_Blitter::blitAntiH(int x, int y,
585 const SkAlpha* SK_RESTRICT antialias,
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +0000586 const int16_t* SK_RESTRICT runs) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000587 uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y);
reed@android.comea16cfa2009-10-02 19:11:02 +0000588 uint32_t srcExpanded = fExpandedRaw16;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000589 unsigned scale = fScale;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000590
reed@android.com9a74d312009-06-15 15:56:35 +0000591 // TODO: respect fDoDither
592 for (;;) {
593 int count = runs[0];
594 SkASSERT(count >= 0);
595 if (count <= 0) {
596 return;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000597 }
reed@android.com9a74d312009-06-15 15:56:35 +0000598 runs += count;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000599
reed@android.com9a74d312009-06-15 15:56:35 +0000600 unsigned aa = antialias[0];
601 antialias += count;
602 if (aa) {
603 unsigned scale5 = SkAlpha255To256(aa) * scale >> (8 + 3);
reed@android.comea16cfa2009-10-02 19:11:02 +0000604 uint32_t src32 = srcExpanded * scale5;
reed@android.com9a74d312009-06-15 15:56:35 +0000605 scale5 = 32 - scale5;
606 do {
607 uint32_t dst32 = SkExpand_rgb_16(*device) * scale5;
608 *device++ = SkCompact_rgb_16((src32 + dst32) >> 5);
609 } while (--count != 0);
610 continue;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000611 }
reed@android.com9a74d312009-06-15 15:56:35 +0000612 device += count;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000613 }
614}
615
reed@android.com8a1c16f2008-12-17 15:59:43 +0000616static inline void blend_8_pixels(U8CPU bw, uint16_t dst[], unsigned dst_scale,
617 U16CPU srcColor) {
618 if (bw & 0x80) dst[0] = srcColor + SkAlphaMulRGB16(dst[0], dst_scale);
619 if (bw & 0x40) dst[1] = srcColor + SkAlphaMulRGB16(dst[1], dst_scale);
620 if (bw & 0x20) dst[2] = srcColor + SkAlphaMulRGB16(dst[2], dst_scale);
621 if (bw & 0x10) dst[3] = srcColor + SkAlphaMulRGB16(dst[3], dst_scale);
622 if (bw & 0x08) dst[4] = srcColor + SkAlphaMulRGB16(dst[4], dst_scale);
623 if (bw & 0x04) dst[5] = srcColor + SkAlphaMulRGB16(dst[5], dst_scale);
624 if (bw & 0x02) dst[6] = srcColor + SkAlphaMulRGB16(dst[6], dst_scale);
625 if (bw & 0x01) dst[7] = srcColor + SkAlphaMulRGB16(dst[7], dst_scale);
626}
627
628#define SK_BLITBWMASK_NAME SkRGB16_BlendBW
629#define SK_BLITBWMASK_ARGS , unsigned dst_scale, U16CPU src_color
630#define SK_BLITBWMASK_BLIT8(mask, dst) blend_8_pixels(mask, dst, dst_scale, src_color)
631#define SK_BLITBWMASK_GETADDR getAddr16
632#define SK_BLITBWMASK_DEVTYPE uint16_t
633#include "SkBlitBWMaskTemplate.h"
634
tomhudson@google.com333d6cb2011-07-12 19:19:03 +0000635void SkRGB16_Blitter::blitMask(const SkMask& mask,
636 const SkIRect& clip) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000637 if (mask.fFormat == SkMask::kBW_Format) {
reed@android.com9a74d312009-06-15 15:56:35 +0000638 SkRGB16_BlendBW(fDevice, mask, clip, 256 - fScale, fColor16);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000639 return;
640 }
641
642 uint16_t* SK_RESTRICT device = fDevice.getAddr16(clip.fLeft, clip.fTop);
reed@google.com79891862011-10-18 15:44:57 +0000643 const uint8_t* SK_RESTRICT alpha = mask.getAddr8(clip.fLeft, clip.fTop);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000644 int width = clip.width();
645 int height = clip.height();
scroggo@google.come5f48242013-02-25 21:47:41 +0000646 size_t deviceRB = fDevice.rowBytes() - (width << 1);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000647 unsigned maskRB = mask.fRowBytes - width;
reed@android.comea16cfa2009-10-02 19:11:02 +0000648 uint32_t color32 = fExpandedRaw16;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000649
reed@android.com9a74d312009-06-15 15:56:35 +0000650 unsigned scale256 = fScale;
651 do {
652 int w = width;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000653 do {
reed@android.com9a74d312009-06-15 15:56:35 +0000654 unsigned aa = *alpha++;
655 unsigned scale = SkAlpha255To256(aa) * scale256 >> (8 + 3);
656 uint32_t src32 = color32 * scale;
657 uint32_t dst32 = SkExpand_rgb_16(*device) * (32 - scale);
658 *device++ = SkCompact_rgb_16((src32 + dst32) >> 5);
659 } while (--w != 0);
660 device = (uint16_t*)((char*)device + deviceRB);
661 alpha += maskRB;
662 } while (--height != 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000663}
664
665void SkRGB16_Blitter::blitV(int x, int y, int height, SkAlpha alpha) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000666 uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y);
scroggo@google.come5f48242013-02-25 21:47:41 +0000667 size_t deviceRB = fDevice.rowBytes();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000668
reed@android.comea16cfa2009-10-02 19:11:02 +0000669 // TODO: respect fDoDither
670 unsigned scale5 = SkAlpha255To256(alpha) * fScale >> (8 + 3);
671 uint32_t src32 = fExpandedRaw16 * scale5;
672 scale5 = 32 - scale5;
673 do {
674 uint32_t dst32 = SkExpand_rgb_16(*device) * scale5;
675 *device = SkCompact_rgb_16((src32 + dst32) >> 5);
676 device = (uint16_t*)((char*)device + deviceRB);
677 } while (--height != 0);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000678}
679
680void SkRGB16_Blitter::blitRect(int x, int y, int width, int height) {
681 SkASSERT(x + width <= fDevice.width() && y + height <= fDevice.height());
reed@android.com8a1c16f2008-12-17 15:59:43 +0000682 uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y);
scroggo@google.come5f48242013-02-25 21:47:41 +0000683 size_t deviceRB = fDevice.rowBytes();
reed@android.com9a74d312009-06-15 15:56:35 +0000684 SkPMColor src32 = fSrcColor32;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000685
reed@android.com9a74d312009-06-15 15:56:35 +0000686 while (--height >= 0) {
reed@android.comdafaf7a2009-07-10 03:05:59 +0000687 blend32_16_row(src32, device, width);
reed@android.com9a74d312009-06-15 15:56:35 +0000688 device = (uint16_t*)((char*)device + deviceRB);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000689 }
690}
691
692///////////////////////////////////////////////////////////////////////////////
693
694SkRGB16_Shader16_Blitter::SkRGB16_Shader16_Blitter(const SkBitmap& device,
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000695 const SkPaint& paint,
696 SkShader::Context* shaderContext)
697 : SkRGB16_Shader_Blitter(device, paint, shaderContext) {
reed@android.com5119bdb2009-06-12 21:27:03 +0000698 SkASSERT(SkShader::CanCallShadeSpan16(fShaderFlags));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000699}
700
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +0000701void SkRGB16_Shader16_Blitter::blitH(int x, int y, int width) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000702 SkASSERT(x + width <= fDevice.width());
703
704 uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y);
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000705 SkShader::Context* shaderContext = fShaderContext;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000706
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000707 int alpha = shaderContext->getSpan16Alpha();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000708 if (0xFF == alpha) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000709 shaderContext->shadeSpan16(x, y, device, width);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000710 } else {
711 uint16_t* span16 = (uint16_t*)fBuffer;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000712 shaderContext->shadeSpan16(x, y, span16, width);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000713 SkBlendRGB16(span16, device, SkAlpha255To256(alpha), width);
714 }
715}
716
reed@android.comdafaf7a2009-07-10 03:05:59 +0000717void SkRGB16_Shader16_Blitter::blitRect(int x, int y, int width, int height) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000718 SkShader::Context* shaderContext = fShaderContext;
719 uint16_t* dst = fDevice.getAddr16(x, y);
720 size_t dstRB = fDevice.rowBytes();
721 int alpha = shaderContext->getSpan16Alpha();
reed@android.comdafaf7a2009-07-10 03:05:59 +0000722
reed@android.comdafaf7a2009-07-10 03:05:59 +0000723 if (0xFF == alpha) {
reed@android.com3c9b2a42009-08-27 19:28:37 +0000724 if (fShaderFlags & SkShader::kConstInY16_Flag) {
reed@android.comdafaf7a2009-07-10 03:05:59 +0000725 // have the shader blit directly into the device the first time
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000726 shaderContext->shadeSpan16(x, y, dst, width);
reed@android.comdafaf7a2009-07-10 03:05:59 +0000727 // and now just memcpy that line on the subsequent lines
728 if (--height > 0) {
729 const uint16_t* orig = dst;
730 do {
731 dst = (uint16_t*)((char*)dst + dstRB);
732 memcpy(dst, orig, width << 1);
733 } while (--height);
734 }
735 } else { // need to call shadeSpan16 for every line
736 do {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000737 shaderContext->shadeSpan16(x, y, dst, width);
reed@android.comdafaf7a2009-07-10 03:05:59 +0000738 y += 1;
739 dst = (uint16_t*)((char*)dst + dstRB);
740 } while (--height);
741 }
742 } else {
743 int scale = SkAlpha255To256(alpha);
744 uint16_t* span16 = (uint16_t*)fBuffer;
reed@android.com3c9b2a42009-08-27 19:28:37 +0000745 if (fShaderFlags & SkShader::kConstInY16_Flag) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000746 shaderContext->shadeSpan16(x, y, span16, width);
reed@android.comdafaf7a2009-07-10 03:05:59 +0000747 do {
748 SkBlendRGB16(span16, dst, scale, width);
749 dst = (uint16_t*)((char*)dst + dstRB);
750 } while (--height);
751 } else {
752 do {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000753 shaderContext->shadeSpan16(x, y, span16, width);
reed@android.comdafaf7a2009-07-10 03:05:59 +0000754 SkBlendRGB16(span16, dst, scale, width);
755 y += 1;
756 dst = (uint16_t*)((char*)dst + dstRB);
757 } while (--height);
758 }
759 }
760}
761
reed@android.com8a1c16f2008-12-17 15:59:43 +0000762void SkRGB16_Shader16_Blitter::blitAntiH(int x, int y,
763 const SkAlpha* SK_RESTRICT antialias,
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +0000764 const int16_t* SK_RESTRICT runs) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000765 SkShader::Context* shaderContext = fShaderContext;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000766 SkPMColor* SK_RESTRICT span = fBuffer;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000767 uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000768
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000769 int alpha = shaderContext->getSpan16Alpha();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000770 uint16_t* span16 = (uint16_t*)span;
771
772 if (0xFF == alpha) {
773 for (;;) {
774 int count = *runs;
775 if (count <= 0) {
776 break;
777 }
778 SkASSERT(count <= fDevice.width()); // don't overrun fBuffer
779
780 int aa = *antialias;
781 if (aa == 255) {
782 // go direct to the device!
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000783 shaderContext->shadeSpan16(x, y, device, count);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000784 } else if (aa) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000785 shaderContext->shadeSpan16(x, y, span16, count);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000786 SkBlendRGB16(span16, device, SkAlpha255To256(aa), count);
787 }
788 device += count;
789 runs += count;
790 antialias += count;
791 x += count;
792 }
793 } else { // span alpha is < 255
794 alpha = SkAlpha255To256(alpha);
795 for (;;) {
796 int count = *runs;
797 if (count <= 0) {
798 break;
799 }
800 SkASSERT(count <= fDevice.width()); // don't overrun fBuffer
801
802 int aa = SkAlphaMul(*antialias, alpha);
803 if (aa) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000804 shaderContext->shadeSpan16(x, y, span16, count);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000805 SkBlendRGB16(span16, device, SkAlpha255To256(aa), count);
806 }
807
808 device += count;
809 runs += count;
810 antialias += count;
811 x += count;
812 }
813 }
814}
815
816///////////////////////////////////////////////////////////////////////////////
817
818SkRGB16_Shader_Blitter::SkRGB16_Shader_Blitter(const SkBitmap& device,
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000819 const SkPaint& paint,
820 SkShader::Context* shaderContext)
821: INHERITED(device, paint, shaderContext) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000822 SkASSERT(paint.getXfermode() == NULL);
823
824 fBuffer = (SkPMColor*)sk_malloc_throw(device.width() * sizeof(SkPMColor));
825
826 // compute SkBlitRow::Procs
827 unsigned flags = 0;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000828
reed@android.com5119bdb2009-06-12 21:27:03 +0000829 uint32_t shaderFlags = fShaderFlags;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000830 // shaders take care of global alpha, so we never set it in SkBlitRow
831 if (!(shaderFlags & SkShader::kOpaqueAlpha_Flag)) {
832 flags |= SkBlitRow::kSrcPixelAlpha_Flag;
commit-bot@chromium.orgc2050e32013-07-15 13:10:31 +0000833 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000834 // don't dither if the shader is really 16bit
835 if (paint.isDither() && !(shaderFlags & SkShader::kIntrinsicly16_Flag)) {
836 flags |= SkBlitRow::kDither_Flag;
837 }
838 // used when we know our global alpha is 0xFF
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000839 fOpaqueProc = SkBlitRow::Factory(flags, kRGB_565_SkColorType);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000840 // used when we know our global alpha is < 0xFF
841 fAlphaProc = SkBlitRow::Factory(flags | SkBlitRow::kGlobalAlpha_Flag,
commit-bot@chromium.orgcba73782014-05-29 15:57:47 +0000842 kRGB_565_SkColorType);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000843}
844
845SkRGB16_Shader_Blitter::~SkRGB16_Shader_Blitter() {
846 sk_free(fBuffer);
847}
848
849void SkRGB16_Shader_Blitter::blitH(int x, int y, int width) {
850 SkASSERT(x + width <= fDevice.width());
851
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000852 fShaderContext->shadeSpan(x, y, fBuffer, width);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000853 // shaders take care of global alpha, so we pass 0xFF (should be ignored)
854 fOpaqueProc(fDevice.getAddr16(x, y), fBuffer, width, 0xFF, x, y);
855}
856
reed@android.com5119bdb2009-06-12 21:27:03 +0000857void SkRGB16_Shader_Blitter::blitRect(int x, int y, int width, int height) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000858 SkShader::Context* shaderContext = fShaderContext;
859 SkBlitRow::Proc proc = fOpaqueProc;
860 SkPMColor* buffer = fBuffer;
861 uint16_t* dst = fDevice.getAddr16(x, y);
862 size_t dstRB = fDevice.rowBytes();
reed@android.com5119bdb2009-06-12 21:27:03 +0000863
reed@android.com3c9b2a42009-08-27 19:28:37 +0000864 if (fShaderFlags & SkShader::kConstInY32_Flag) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000865 shaderContext->shadeSpan(x, y, buffer, width);
reed@android.com5119bdb2009-06-12 21:27:03 +0000866 do {
867 proc(dst, buffer, width, 0xFF, x, y);
868 y += 1;
869 dst = (uint16_t*)((char*)dst + dstRB);
870 } while (--height);
871 } else {
872 do {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000873 shaderContext->shadeSpan(x, y, buffer, width);
reed@android.com5119bdb2009-06-12 21:27:03 +0000874 proc(dst, buffer, width, 0xFF, x, y);
875 y += 1;
876 dst = (uint16_t*)((char*)dst + dstRB);
877 } while (--height);
878 }
879}
880
reed@android.com8a1c16f2008-12-17 15:59:43 +0000881static inline int count_nonzero_span(const int16_t runs[], const SkAlpha aa[]) {
882 int count = 0;
883 for (;;) {
884 int n = *runs;
885 if (n == 0 || *aa == 0) {
886 break;
887 }
888 runs += n;
889 aa += n;
890 count += n;
891 }
892 return count;
893}
894
895void SkRGB16_Shader_Blitter::blitAntiH(int x, int y,
896 const SkAlpha* SK_RESTRICT antialias,
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +0000897 const int16_t* SK_RESTRICT runs) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000898 SkShader::Context* shaderContext = fShaderContext;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000899 SkPMColor* SK_RESTRICT span = fBuffer;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000900 uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000901
902 for (;;) {
903 int count = *runs;
904 if (count <= 0) {
905 break;
906 }
907 int aa = *antialias;
908 if (0 == aa) {
909 device += count;
910 runs += count;
911 antialias += count;
912 x += count;
913 continue;
914 }
915
916 int nonZeroCount = count + count_nonzero_span(runs + count, antialias + count);
917
918 SkASSERT(nonZeroCount <= fDevice.width()); // don't overrun fBuffer
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000919 shaderContext->shadeSpan(x, y, span, nonZeroCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000920
921 SkPMColor* localSpan = span;
922 for (;;) {
923 SkBlitRow::Proc proc = (aa == 0xFF) ? fOpaqueProc : fAlphaProc;
924 proc(device, localSpan, count, aa, x, y);
925
926 x += count;
927 device += count;
928 runs += count;
929 antialias += count;
930 nonZeroCount -= count;
931 if (nonZeroCount == 0) {
932 break;
933 }
934 localSpan += count;
935 SkASSERT(nonZeroCount > 0);
936 count = *runs;
937 SkASSERT(count > 0);
938 aa = *antialias;
939 }
940 }
941}
942
943///////////////////////////////////////////////////////////////////////
944
945SkRGB16_Shader_Xfermode_Blitter::SkRGB16_Shader_Xfermode_Blitter(
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000946 const SkBitmap& device, const SkPaint& paint,
947 SkShader::Context* shaderContext)
948: INHERITED(device, paint, shaderContext) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000949 fXfermode = paint.getXfermode();
950 SkASSERT(fXfermode);
951 fXfermode->ref();
952
953 int width = device.width();
954 fBuffer = (SkPMColor*)sk_malloc_throw((width + (SkAlign4(width) >> 2)) * sizeof(SkPMColor));
955 fAAExpand = (uint8_t*)(fBuffer + width);
956}
957
958SkRGB16_Shader_Xfermode_Blitter::~SkRGB16_Shader_Xfermode_Blitter() {
959 fXfermode->unref();
960 sk_free(fBuffer);
961}
962
963void SkRGB16_Shader_Xfermode_Blitter::blitH(int x, int y, int width) {
964 SkASSERT(x + width <= fDevice.width());
965
966 uint16_t* device = fDevice.getAddr16(x, y);
967 SkPMColor* span = fBuffer;
968
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000969 fShaderContext->shadeSpan(x, y, span, width);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000970 fXfermode->xfer16(device, span, width, NULL);
971}
972
973void SkRGB16_Shader_Xfermode_Blitter::blitAntiH(int x, int y,
974 const SkAlpha* SK_RESTRICT antialias,
tomhudson@google.coma87cd2a2011-06-15 16:50:27 +0000975 const int16_t* SK_RESTRICT runs) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000976 SkShader::Context* shaderContext = fShaderContext;
977 SkXfermode* mode = fXfermode;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000978 SkPMColor* SK_RESTRICT span = fBuffer;
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +0000979 uint8_t* SK_RESTRICT aaExpand = fAAExpand;
980 uint16_t* SK_RESTRICT device = fDevice.getAddr16(x, y);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000981
982 for (;;) {
983 int count = *runs;
984 if (count <= 0) {
985 break;
986 }
987 int aa = *antialias;
988 if (0 == aa) {
989 device += count;
990 runs += count;
991 antialias += count;
992 x += count;
993 continue;
994 }
995
996 int nonZeroCount = count + count_nonzero_span(runs + count,
997 antialias + count);
998
999 SkASSERT(nonZeroCount <= fDevice.width()); // don't overrun fBuffer
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001000 shaderContext->shadeSpan(x, y, span, nonZeroCount);
reed@android.com8a1c16f2008-12-17 15:59:43 +00001001
1002 x += nonZeroCount;
1003 SkPMColor* localSpan = span;
1004 for (;;) {
1005 if (aa == 0xFF) {
1006 mode->xfer16(device, localSpan, count, NULL);
1007 } else {
1008 SkASSERT(aa);
1009 memset(aaExpand, aa, count);
1010 mode->xfer16(device, localSpan, count, aaExpand);
1011 }
1012 device += count;
1013 runs += count;
1014 antialias += count;
1015 nonZeroCount -= count;
1016 if (nonZeroCount == 0) {
1017 break;
1018 }
1019 localSpan += count;
1020 SkASSERT(nonZeroCount > 0);
1021 count = *runs;
1022 SkASSERT(count > 0);
1023 aa = *antialias;
1024 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001025 }
reed@android.com8a1c16f2008-12-17 15:59:43 +00001026}
1027
reed@android.com1fc4c602009-10-02 16:34:57 +00001028///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +00001029
reed@android.com1fc4c602009-10-02 16:34:57 +00001030SkBlitter* SkBlitter_ChooseD565(const SkBitmap& device, const SkPaint& paint,
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001031 SkShader::Context* shaderContext,
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +00001032 SkTBlitterAllocator* allocator) {
1033 SkASSERT(allocator != NULL);
1034
reed@android.com1fc4c602009-10-02 16:34:57 +00001035 SkBlitter* blitter;
1036 SkShader* shader = paint.getShader();
1037 SkXfermode* mode = paint.getXfermode();
1038
1039 // we require a shader if there is an xfermode, handled by our caller
1040 SkASSERT(NULL == mode || NULL != shader);
1041
1042 if (shader) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001043 SkASSERT(shaderContext != NULL);
reed@android.com1fc4c602009-10-02 16:34:57 +00001044 if (mode) {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001045 blitter = allocator->createT<SkRGB16_Shader_Xfermode_Blitter>(device, paint,
1046 shaderContext);
1047 } else if (shaderContext->canCallShadeSpan16()) {
1048 blitter = allocator->createT<SkRGB16_Shader16_Blitter>(device, paint, shaderContext);
reed@android.com1fc4c602009-10-02 16:34:57 +00001049 } else {
commit-bot@chromium.org87fcd952014-04-23 19:10:51 +00001050 blitter = allocator->createT<SkRGB16_Shader_Blitter>(device, paint, shaderContext);
reed@android.com1fc4c602009-10-02 16:34:57 +00001051 }
1052 } else {
1053 // no shader, no xfermode, (and we always ignore colorfilter)
1054 SkColor color = paint.getColor();
1055 if (0 == SkColorGetA(color)) {
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +00001056 blitter = allocator->createT<SkNullBlitter>();
reed@android.com867ee802009-10-20 13:55:41 +00001057#ifdef USE_BLACK_BLITTER
reed@android.com1fc4c602009-10-02 16:34:57 +00001058 } else if (SK_ColorBLACK == color) {
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +00001059 blitter = allocator->createT<SkRGB16_Black_Blitter>(device, paint);
reed@android.com867ee802009-10-20 13:55:41 +00001060#endif
reed@android.com1fc4c602009-10-02 16:34:57 +00001061 } else if (0xFF == SkColorGetA(color)) {
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +00001062 blitter = allocator->createT<SkRGB16_Opaque_Blitter>(device, paint);
reed@android.com1fc4c602009-10-02 16:34:57 +00001063 } else {
commit-bot@chromium.orga5572e52014-03-07 03:24:41 +00001064 blitter = allocator->createT<SkRGB16_Blitter>(device, paint);
reed@android.com1fc4c602009-10-02 16:34:57 +00001065 }
1066 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001067
reed@android.com1fc4c602009-10-02 16:34:57 +00001068 return blitter;
reed@android.com8a1c16f2008-12-17 15:59:43 +00001069}