1/*
2 * Copyright 2011 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// Define NAME_WRAP(x) before including this header to perform name-wrapping
9// E.g. for ARM NEON, defined it as 'x ## _neon' to ensure all important
10// identifiers have a _neon suffix.
11#ifndef NAME_WRAP
12#error "Please define NAME_WRAP() before including this file"
13#endif
14
15// returns expanded * 5bits
16static inline uint32_t Filter_565_Expanded(unsigned x, unsigned y,
17                                           uint32_t a00, uint32_t a01,
18                                           uint32_t a10, uint32_t a11) {
19    SkASSERT((unsigned)x <= 0xF);
20    SkASSERT((unsigned)y <= 0xF);
21
22    a00 = SkExpand_rgb_16(a00);
23    a01 = SkExpand_rgb_16(a01);
24    a10 = SkExpand_rgb_16(a10);
25    a11 = SkExpand_rgb_16(a11);
26
27    int xy = x * y >> 3;
28    return  a00 * (32 - 2*y - 2*x + xy) +
29            a01 * (2*x - xy) +
30            a10 * (2*y - xy) +
31            a11 * xy;
32}
33
34// turn an expanded 565 * 5bits into SkPMColor
35// g:11 | r:10 | x:1 | b:10
36static inline SkPMColor SkExpanded_565_To_PMColor(uint32_t c) {
37    unsigned r = (c >> 13) & 0xFF;
38    unsigned g = (c >> 24);
39    unsigned b = (c >> 2) & 0xFF;
40    return SkPackARGB32(0xFF, r, g, b);
41}
42
43// returns answer in SkPMColor format
44static inline SkPMColor Filter_4444_D32(unsigned x, unsigned y,
45                                        uint32_t a00, uint32_t a01,
46                                        uint32_t a10, uint32_t a11) {
47    SkASSERT((unsigned)x <= 0xF);
48    SkASSERT((unsigned)y <= 0xF);
49
50    a00 = SkExpand_4444(a00);
51    a01 = SkExpand_4444(a01);
52    a10 = SkExpand_4444(a10);
53    a11 = SkExpand_4444(a11);
54
55    int xy = x * y >> 4;
56    uint32_t result =   a00 * (16 - y - x + xy) +
57                        a01 * (x - xy) +
58                        a10 * (y - xy) +
59                        a11 * xy;
60
61    return SkCompact_8888(result);
62}
63
64static inline U8CPU Filter_8(unsigned x, unsigned y,
65                             U8CPU a00, U8CPU a01,
66                             U8CPU a10, U8CPU a11) {
67    SkASSERT((unsigned)x <= 0xF);
68    SkASSERT((unsigned)y <= 0xF);
69
70    int xy = x * y;
71    unsigned result =   a00 * (256 - 16*y - 16*x + xy) +
72                        a01 * (16*x - xy) +
73                        a10 * (16*y - xy) +
74                        a11 * xy;
75
76    return result >> 8;
77}
78
79/*****************************************************************************
80 *
81 *  D32 functions
82 *
83 */
84
85// SRC == 8888
86
87#define FILTER_PROC(x, y, a, b, c, d, dst)   NAME_WRAP(Filter_32_opaque)(x, y, a, b, c, d, dst)
88
89#define MAKENAME(suffix)        NAME_WRAP(S32_opaque_D32 ## suffix)
90#define SRCTYPE                 SkPMColor
91#define CHECKSTATE(state)       SkASSERT(4 == state.fPixmap.info().bytesPerPixel()); \
92                                SkASSERT(state.fAlphaScale == 256)
93#define RETURNDST(src)          src
94#define SRC_TO_FILTER(src)      src
95#include "SkBitmapProcState_sample.h"
96
97#undef FILTER_PROC
98#define FILTER_PROC(x, y, a, b, c, d, dst)   NAME_WRAP(Filter_32_alpha)(x, y, a, b, c, d, dst, alphaScale)
99
100#define MAKENAME(suffix)        NAME_WRAP(S32_alpha_D32 ## suffix)
101#define SRCTYPE                 SkPMColor
102#define CHECKSTATE(state)       SkASSERT(4 == state.fPixmap.info().bytesPerPixel()); \
103                                SkASSERT(state.fAlphaScale < 256)
104#define PREAMBLE(state)         unsigned alphaScale = state.fAlphaScale
105#define RETURNDST(src)          SkAlphaMulQ(src, alphaScale)
106#define SRC_TO_FILTER(src)      src
107#include "SkBitmapProcState_sample.h"
108
109// SRC == 565
110
111#undef FILTER_PROC
112#define FILTER_PROC(x, y, a, b, c, d, dst) \
113    do {                                                        \
114        uint32_t tmp = Filter_565_Expanded(x, y, a, b, c, d);   \
115        *(dst) = SkExpanded_565_To_PMColor(tmp);                \
116    } while (0)
117
118#define MAKENAME(suffix)        NAME_WRAP(S16_opaque_D32 ## suffix)
119#define SRCTYPE                 uint16_t
120#define CHECKSTATE(state)       SkASSERT(kRGB_565_SkColorType == state.fPixmap.colorType()); \
121                                SkASSERT(state.fAlphaScale == 256)
122#define RETURNDST(src)          SkPixel16ToPixel32(src)
123#define SRC_TO_FILTER(src)      src
124#include "SkBitmapProcState_sample.h"
125
126#undef FILTER_PROC
127#define FILTER_PROC(x, y, a, b, c, d, dst) \
128    do {                                                                    \
129        uint32_t tmp = Filter_565_Expanded(x, y, a, b, c, d);               \
130        *(dst) = SkAlphaMulQ(SkExpanded_565_To_PMColor(tmp), alphaScale);   \
131    } while (0)
132
133#define MAKENAME(suffix)        NAME_WRAP(S16_alpha_D32 ## suffix)
134#define SRCTYPE                 uint16_t
135#define CHECKSTATE(state)       SkASSERT(kRGB_565_SkColorType == state.fPixmap.colorType()); \
136                                SkASSERT(state.fAlphaScale < 256)
137#define PREAMBLE(state)         unsigned alphaScale = state.fAlphaScale
138#define RETURNDST(src)          SkAlphaMulQ(SkPixel16ToPixel32(src), alphaScale)
139#define SRC_TO_FILTER(src)      src
140#include "SkBitmapProcState_sample.h"
141
142// SRC == Index8
143
144#undef FILTER_PROC
145#define FILTER_PROC(x, y, a, b, c, d, dst)   NAME_WRAP(Filter_32_opaque)(x, y, a, b, c, d, dst)
146
147#define MAKENAME(suffix)        NAME_WRAP(SI8_opaque_D32 ## suffix)
148#define SRCTYPE                 uint8_t
149#define CHECKSTATE(state)       SkASSERT(kIndex_8_SkColorType == state.fPixmap.colorType()); \
150                                SkASSERT(state.fAlphaScale == 256)
151#define PREAMBLE(state)         const SkPMColor* SK_RESTRICT table = state.fPixmap.ctable()->readColors()
152#define RETURNDST(src)          table[src]
153#define SRC_TO_FILTER(src)      table[src]
154#define POSTAMBLE(state)
155#include "SkBitmapProcState_sample.h"
156
157#undef FILTER_PROC
158#define FILTER_PROC(x, y, a, b, c, d, dst)   NAME_WRAP(Filter_32_alpha)(x, y, a, b, c, d, dst, alphaScale)
159
160#define MAKENAME(suffix)        NAME_WRAP(SI8_alpha_D32 ## suffix)
161#define SRCTYPE                 uint8_t
162#define CHECKSTATE(state)       SkASSERT(kIndex_8_SkColorType == state.fPixmap.colorType()); \
163                                SkASSERT(state.fAlphaScale < 256)
164#define PREAMBLE(state)         unsigned alphaScale = state.fAlphaScale; \
165                                const SkPMColor* SK_RESTRICT table = state.fPixmap.ctable()->readColors()
166#define RETURNDST(src)          SkAlphaMulQ(table[src], alphaScale)
167#define SRC_TO_FILTER(src)      table[src]
168#define POSTAMBLE(state)
169#include "SkBitmapProcState_sample.h"
170
171// SRC == 4444
172
173#undef FILTER_PROC
174#define FILTER_PROC(x, y, a, b, c, d, dst)  *(dst) = Filter_4444_D32(x, y, a, b, c, d)
175
176#define MAKENAME(suffix)        NAME_WRAP(S4444_opaque_D32 ## suffix)
177#define SRCTYPE                 SkPMColor16
178#define CHECKSTATE(state)       SkASSERT(kARGB_4444_SkColorType == state.fPixmap.colorType()); \
179                                SkASSERT(state.fAlphaScale == 256)
180#define RETURNDST(src)          SkPixel4444ToPixel32(src)
181#define SRC_TO_FILTER(src)      src
182#include "SkBitmapProcState_sample.h"
183
184#undef FILTER_PROC
185#define FILTER_PROC(x, y, a, b, c, d, dst)  \
186    do {                                                    \
187        uint32_t tmp = Filter_4444_D32(x, y, a, b, c, d);   \
188        *(dst) = SkAlphaMulQ(tmp, alphaScale);              \
189    } while (0)
190
191#define MAKENAME(suffix)        NAME_WRAP(S4444_alpha_D32 ## suffix)
192#define SRCTYPE                 SkPMColor16
193#define CHECKSTATE(state)       SkASSERT(kARGB_4444_SkColorType == state.fPixmap.colorType()); \
194                                SkASSERT(state.fAlphaScale < 256)
195#define PREAMBLE(state)         unsigned alphaScale = state.fAlphaScale
196#define RETURNDST(src)          SkAlphaMulQ(SkPixel4444ToPixel32(src), alphaScale)
197#define SRC_TO_FILTER(src)      src
198#include "SkBitmapProcState_sample.h"
199
200// SRC == A8
201
202#undef FILTER_PROC
203#define FILTER_PROC(x, y, a, b, c, d, dst) \
204    do {                                                        \
205        unsigned tmp = Filter_8(x, y, a, b, c, d);              \
206        *(dst) = SkAlphaMulQ(pmColor, SkAlpha255To256(tmp));    \
207    } while (0)
208
209#define MAKENAME(suffix)        NAME_WRAP(SA8_alpha_D32 ## suffix)
210#define SRCTYPE                 uint8_t
211#define CHECKSTATE(state)       SkASSERT(kAlpha_8_SkColorType == state.fPixmap.colorType());
212#define PREAMBLE(state)         const SkPMColor pmColor = state.fPaintPMColor;
213#define RETURNDST(src)          SkAlphaMulQ(pmColor, SkAlpha255To256(src))
214#define SRC_TO_FILTER(src)      src
215#include "SkBitmapProcState_sample.h"
216
217// SRC == Gray8
218
219#undef FILTER_PROC
220#define FILTER_PROC(x, y, a, b, c, d, dst) \
221    do {                                                        \
222        unsigned tmp = Filter_8(x, y, a, b, c, d);              \
223        SkPMColor color = SkPackARGB32(0xFF, tmp, tmp, tmp);    \
224        *(dst) = SkAlphaMulQ(color, alphaScale);                \
225    } while (0)
226
227#define MAKENAME(suffix)        NAME_WRAP(SG8_alpha_D32 ## suffix)
228#define SRCTYPE                 uint8_t
229#define CHECKSTATE(state)       SkASSERT(kGray_8_SkColorType == state.fPixmap.colorType());
230#define PREAMBLE(state)         unsigned alphaScale = state.fAlphaScale
231#define RETURNDST(src)          SkAlphaMulQ(SkPackARGB32(0xFF, src, src, src), alphaScale)
232#define SRC_TO_FILTER(src)      src
233#include "SkBitmapProcState_sample.h"
234
235
236#define TILEX_PROCF(fx, max)     SkClampMax((fx) >> 16, max)
237#define TILEY_PROCF(fy, max)     SkClampMax((fy) >> 16, max)
238#define EXTRACT_LOW_BITS(v, max) (((v) >> 12) & 0xF)
239
240#undef FILTER_PROC
241#define FILTER_PROC(x, y, a, b, c, d, dst)   NAME_WRAP(Filter_32_opaque)(x, y, a, b, c, d, dst)
242#define MAKENAME(suffix)        NAME_WRAP(Clamp_SI8_opaque_D32 ## suffix)
243#define SRCTYPE                 uint8_t
244#define CHECKSTATE(state)       SkASSERT(kIndex_8_SkColorType == state.fPixmap.colorType())
245#define PREAMBLE(state)         const SkPMColor* SK_RESTRICT table = state.fPixmap.ctable()->readColors()
246#define SRC_TO_FILTER(src)      table[src]
247#define POSTAMBLE(state)
248#include "SkBitmapProcState_shaderproc.h"
249
250#undef NAME_WRAP
251