1 /*
2  * Copyright 2014 Google Inc.
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 
8 #include "SkBlitMask.h"
9 #include "SkColor.h"
10 #include "SkColorData.h"
11 #include "SkOpts.h"
12 
BlitLCD16RowFactory(bool isOpaque)13 SkBlitMask::BlitLCD16RowProc SkBlitMask::BlitLCD16RowFactory(bool isOpaque) {
14     BlitLCD16RowProc proc = PlatformBlitRowProcs16(isOpaque);
15     if (proc) {
16         return proc;
17     }
18 
19     if (isOpaque) {
20         return  SkBlitLCD16OpaqueRow;
21     } else {
22         return  SkBlitLCD16Row;
23     }
24 }
25 
D32_LCD16_Proc(void * SK_RESTRICT dst,size_t dstRB,const void * SK_RESTRICT mask,size_t maskRB,SkColor color,int width,int height)26 static void D32_LCD16_Proc(void* SK_RESTRICT dst, size_t dstRB,
27                            const void* SK_RESTRICT mask, size_t maskRB,
28                            SkColor color, int width, int height) {
29 
30     SkPMColor*        dstRow = (SkPMColor*)dst;
31     const uint16_t* srcRow = (const uint16_t*)mask;
32     SkPMColor       opaqueDst;
33 
34     SkBlitMask::BlitLCD16RowProc proc = nullptr;
35     bool isOpaque = (0xFF == SkColorGetA(color));
36     proc = SkBlitMask::BlitLCD16RowFactory(isOpaque);
37     SkASSERT(proc != nullptr);
38 
39     if (isOpaque) {
40         opaqueDst = SkPreMultiplyColor(color);
41     } else {
42         opaqueDst = 0;  // ignored
43     }
44 
45     do {
46         proc(dstRow, srcRow, color, width, opaqueDst);
47         dstRow = (SkPMColor*)((char*)dstRow + dstRB);
48         srcRow = (const uint16_t*)((const char*)srcRow + maskRB);
49     } while (--height != 0);
50 }
51 
52 ///////////////////////////////////////////////////////////////////////////////
53 
BlitColor(const SkPixmap & device,const SkMask & mask,const SkIRect & clip,SkColor color)54 bool SkBlitMask::BlitColor(const SkPixmap& device, const SkMask& mask,
55                            const SkIRect& clip, SkColor color) {
56     int x = clip.fLeft, y = clip.fTop;
57 
58     if (device.colorType() == kN32_SkColorType && mask.fFormat == SkMask::kA8_Format) {
59         SkOpts::blit_mask_d32_a8(device.writable_addr32(x,y), device.rowBytes(),
60                                  (const SkAlpha*)mask.getAddr(x,y), mask.fRowBytes,
61                                  color, clip.width(), clip.height());
62         return true;
63     }
64 
65     if (device.colorType() == kN32_SkColorType && mask.fFormat == SkMask::kLCD16_Format) {
66         // TODO: Is this reachable code?  Seems like no.
67         D32_LCD16_Proc(device.writable_addr32(x,y), device.rowBytes(),
68                        mask.getAddr(x,y), mask.fRowBytes,
69                        color, clip.width(), clip.height());
70         return true;
71     }
72 
73     return false;
74 }
75 
76 ///////////////////////////////////////////////////////////////////////////////
77 ///////////////////////////////////////////////////////////////////////////////
78 
BW_RowProc_Blend(SkPMColor * SK_RESTRICT dst,const void * maskIn,const SkPMColor * SK_RESTRICT src,int count)79 static void BW_RowProc_Blend(
80         SkPMColor* SK_RESTRICT dst, const void* maskIn, const SkPMColor* SK_RESTRICT src, int count) {
81     const uint8_t* SK_RESTRICT mask = static_cast<const uint8_t*>(maskIn);
82     int i, octuple = (count + 7) >> 3;
83     for (i = 0; i < octuple; ++i) {
84         int m = *mask++;
85         if (m & 0x80) { dst[0] = SkPMSrcOver(src[0], dst[0]); }
86         if (m & 0x40) { dst[1] = SkPMSrcOver(src[1], dst[1]); }
87         if (m & 0x20) { dst[2] = SkPMSrcOver(src[2], dst[2]); }
88         if (m & 0x10) { dst[3] = SkPMSrcOver(src[3], dst[3]); }
89         if (m & 0x08) { dst[4] = SkPMSrcOver(src[4], dst[4]); }
90         if (m & 0x04) { dst[5] = SkPMSrcOver(src[5], dst[5]); }
91         if (m & 0x02) { dst[6] = SkPMSrcOver(src[6], dst[6]); }
92         if (m & 0x01) { dst[7] = SkPMSrcOver(src[7], dst[7]); }
93         src += 8;
94         dst += 8;
95     }
96     count &= 7;
97     if (count > 0) {
98         int m = *mask;
99         do {
100             if (m & 0x80) { dst[0] = SkPMSrcOver(src[0], dst[0]); }
101             m <<= 1;
102             src += 1;
103             dst += 1;
104         } while (--count > 0);
105     }
106 }
107 
BW_RowProc_Opaque(SkPMColor * SK_RESTRICT dst,const void * maskIn,const SkPMColor * SK_RESTRICT src,int count)108 static void BW_RowProc_Opaque(
109         SkPMColor* SK_RESTRICT dst, const void* maskIn, const SkPMColor* SK_RESTRICT src, int count) {
110     const uint8_t* SK_RESTRICT mask = static_cast<const uint8_t*>(maskIn);
111     int i, octuple = (count + 7) >> 3;
112     for (i = 0; i < octuple; ++i) {
113         int m = *mask++;
114         if (m & 0x80) { dst[0] = src[0]; }
115         if (m & 0x40) { dst[1] = src[1]; }
116         if (m & 0x20) { dst[2] = src[2]; }
117         if (m & 0x10) { dst[3] = src[3]; }
118         if (m & 0x08) { dst[4] = src[4]; }
119         if (m & 0x04) { dst[5] = src[5]; }
120         if (m & 0x02) { dst[6] = src[6]; }
121         if (m & 0x01) { dst[7] = src[7]; }
122         src += 8;
123         dst += 8;
124     }
125     count &= 7;
126     if (count > 0) {
127         int m = *mask;
128         do {
129             if (m & 0x80) { dst[0] = SkPMSrcOver(src[0], dst[0]); }
130             m <<= 1;
131             src += 1;
132             dst += 1;
133         } while (--count > 0);
134     }
135 }
136 
A8_RowProc_Blend(SkPMColor * SK_RESTRICT dst,const void * maskIn,const SkPMColor * SK_RESTRICT src,int count)137 static void A8_RowProc_Blend(
138         SkPMColor* SK_RESTRICT dst, const void* maskIn, const SkPMColor* SK_RESTRICT src, int count) {
139     const uint8_t* SK_RESTRICT mask = static_cast<const uint8_t*>(maskIn);
140     for (int i = 0; i < count; ++i) {
141         if (mask[i]) {
142             dst[i] = SkBlendARGB32(src[i], dst[i], mask[i]);
143         }
144     }
145 }
146 
A8_RowProc_Opaque(SkPMColor * SK_RESTRICT dst,const void * maskIn,const SkPMColor * SK_RESTRICT src,int count)147 static void A8_RowProc_Opaque(
148         SkPMColor* SK_RESTRICT dst, const void* maskIn, const SkPMColor* SK_RESTRICT src, int count) {
149     const uint8_t* SK_RESTRICT mask = static_cast<const uint8_t*>(maskIn);
150     for (int i = 0; i < count; ++i) {
151         int m = mask[i];
152         if (m) {
153             m += (m >> 7);
154             dst[i] = SkPMLerp(src[i], dst[i], m);
155         }
156     }
157 }
158 
upscale31To255(int value)159 static int upscale31To255(int value) {
160     value = (value << 3) | (value >> 2);
161     return value;
162 }
163 
src_alpha_blend(int src,int dst,int srcA,int mask)164 static int src_alpha_blend(int src, int dst, int srcA, int mask) {
165 
166     return dst + SkAlphaMul(src - SkAlphaMul(srcA, dst), mask);
167 }
168 
LCD16_RowProc_Blend(SkPMColor * SK_RESTRICT dst,const void * maskIn,const SkPMColor * SK_RESTRICT src,int count)169 static void LCD16_RowProc_Blend(
170         SkPMColor* SK_RESTRICT dst, const void* maskIn, const SkPMColor* SK_RESTRICT src, int count) {
171     const uint16_t* SK_RESTRICT mask = static_cast<const uint16_t*>(maskIn);
172     for (int i = 0; i < count; ++i) {
173         uint16_t m = mask[i];
174         if (0 == m) {
175             continue;
176         }
177 
178         SkPMColor s = src[i];
179         SkPMColor d = dst[i];
180 
181         int srcA = SkGetPackedA32(s);
182         int srcR = SkGetPackedR32(s);
183         int srcG = SkGetPackedG32(s);
184         int srcB = SkGetPackedB32(s);
185 
186         srcA += srcA >> 7;
187 
188         /*  We want all of these in 5bits, hence the shifts in case one of them
189          *  (green) is 6bits.
190          */
191         int maskR = SkGetPackedR16(m) >> (SK_R16_BITS - 5);
192         int maskG = SkGetPackedG16(m) >> (SK_G16_BITS - 5);
193         int maskB = SkGetPackedB16(m) >> (SK_B16_BITS - 5);
194 
195         maskR = upscale31To255(maskR);
196         maskG = upscale31To255(maskG);
197         maskB = upscale31To255(maskB);
198 
199         int dstR = SkGetPackedR32(d);
200         int dstG = SkGetPackedG32(d);
201         int dstB = SkGetPackedB32(d);
202 
203         // LCD blitting is only supported if the dst is known/required
204         // to be opaque
205         dst[i] = SkPackARGB32(0xFF,
206                               src_alpha_blend(srcR, dstR, srcA, maskR),
207                               src_alpha_blend(srcG, dstG, srcA, maskG),
208                               src_alpha_blend(srcB, dstB, srcA, maskB));
209     }
210 }
211 
LCD16_RowProc_Opaque(SkPMColor * SK_RESTRICT dst,const void * maskIn,const SkPMColor * SK_RESTRICT src,int count)212 static void LCD16_RowProc_Opaque(
213         SkPMColor* SK_RESTRICT dst, const void* maskIn, const SkPMColor* SK_RESTRICT src, int count) {
214     const uint16_t* SK_RESTRICT mask = static_cast<const uint16_t*>(maskIn);
215     for (int i = 0; i < count; ++i) {
216         uint16_t m = mask[i];
217         if (0 == m) {
218             continue;
219         }
220 
221         SkPMColor s = src[i];
222         SkPMColor d = dst[i];
223 
224         int srcR = SkGetPackedR32(s);
225         int srcG = SkGetPackedG32(s);
226         int srcB = SkGetPackedB32(s);
227 
228         /*  We want all of these in 5bits, hence the shifts in case one of them
229          *  (green) is 6bits.
230          */
231         int maskR = SkGetPackedR16(m) >> (SK_R16_BITS - 5);
232         int maskG = SkGetPackedG16(m) >> (SK_G16_BITS - 5);
233         int maskB = SkGetPackedB16(m) >> (SK_B16_BITS - 5);
234 
235         // Now upscale them to 0..32, so we can use blend32
236         maskR = SkUpscale31To32(maskR);
237         maskG = SkUpscale31To32(maskG);
238         maskB = SkUpscale31To32(maskB);
239 
240         int dstR = SkGetPackedR32(d);
241         int dstG = SkGetPackedG32(d);
242         int dstB = SkGetPackedB32(d);
243 
244         // LCD blitting is only supported if the dst is known/required
245         // to be opaque
246         dst[i] = SkPackARGB32(0xFF,
247                               SkBlend32(srcR, dstR, maskR),
248                               SkBlend32(srcG, dstG, maskG),
249                               SkBlend32(srcB, dstB, maskB));
250     }
251 }
252 
RowFactory(SkColorType ct,SkMask::Format format,RowFlags flags)253 SkBlitMask::RowProc SkBlitMask::RowFactory(SkColorType ct,
254                                            SkMask::Format format,
255                                            RowFlags flags) {
256 // make this opt-in until chrome can rebaseline
257     RowProc proc = PlatformRowProcs(ct, format, flags);
258     if (proc) {
259         return proc;
260     }
261 
262     static const RowProc gProcs[] = {
263         // need X coordinate to handle BW
264         false ? (RowProc)BW_RowProc_Blend : nullptr, // suppress unused warning
265         false ? (RowProc)BW_RowProc_Opaque : nullptr, // suppress unused warning
266         (RowProc)A8_RowProc_Blend,      (RowProc)A8_RowProc_Opaque,
267         (RowProc)LCD16_RowProc_Blend,   (RowProc)LCD16_RowProc_Opaque,
268     };
269 
270     int index;
271     switch (ct) {
272         case kN32_SkColorType:
273             switch (format) {
274                 case SkMask::kBW_Format:    index = 0; break;
275                 case SkMask::kA8_Format:    index = 2; break;
276                 case SkMask::kLCD16_Format: index = 4; break;
277                 default:
278                     return nullptr;
279             }
280             if (flags & kSrcIsOpaque_RowFlag) {
281                 index |= 1;
282             }
283             SkASSERT((size_t)index < SK_ARRAY_COUNT(gProcs));
284             return gProcs[index];
285         default:
286             break;
287     }
288     return nullptr;
289 }
290