blob: fb104ace9cb19f55b5e0558f1f965d5520b42d18 [file] [log] [blame]
digit@google.com3ada0ef2012-08-13 14:06:34 +00001/*
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);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000021
digit@google.com3ada0ef2012-08-13 14:06:34 +000022 a00 = SkExpand_rgb_16(a00);
23 a01 = SkExpand_rgb_16(a01);
24 a10 = SkExpand_rgb_16(a10);
25 a11 = SkExpand_rgb_16(a11);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000026
digit@google.com3ada0ef2012-08-13 14:06:34 +000027 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);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000049
digit@google.com3ada0ef2012-08-13 14:06:34 +000050 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);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000069
digit@google.com3ada0ef2012-08-13 14:06:34 +000070 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;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000075
digit@google.com3ada0ef2012-08-13 14:06:34 +000076 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)
digit@google.com3ada0ef2012-08-13 14:06:34 +000090#define SRCTYPE SkPMColor
reedad7ae6c2015-06-04 14:12:25 -070091#define CHECKSTATE(state) SkASSERT(4 == state.fPixmap.info().bytesPerPixel()); \
digit@google.com3ada0ef2012-08-13 14:06:34 +000092 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)
digit@google.com3ada0ef2012-08-13 14:06:34 +0000101#define SRCTYPE SkPMColor
reedad7ae6c2015-06-04 14:12:25 -0700102#define CHECKSTATE(state) SkASSERT(4 == state.fPixmap.info().bytesPerPixel()); \
digit@google.com3ada0ef2012-08-13 14:06:34 +0000103 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)
digit@google.com3ada0ef2012-08-13 14:06:34 +0000119#define SRCTYPE uint16_t
reedad7ae6c2015-06-04 14:12:25 -0700120#define CHECKSTATE(state) SkASSERT(kRGB_565_SkColorType == state.fPixmap.colorType()); \
digit@google.com3ada0ef2012-08-13 14:06:34 +0000121 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)
digit@google.com3ada0ef2012-08-13 14:06:34 +0000134#define SRCTYPE uint16_t
reedad7ae6c2015-06-04 14:12:25 -0700135#define CHECKSTATE(state) SkASSERT(kRGB_565_SkColorType == state.fPixmap.colorType()); \
digit@google.com3ada0ef2012-08-13 14:06:34 +0000136 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)
digit@google.com3ada0ef2012-08-13 14:06:34 +0000148#define SRCTYPE uint8_t
reedad7ae6c2015-06-04 14:12:25 -0700149#define CHECKSTATE(state) SkASSERT(kIndex_8_SkColorType == state.fPixmap.colorType()); \
digit@google.com3ada0ef2012-08-13 14:06:34 +0000150 SkASSERT(state.fAlphaScale == 256)
reedad7ae6c2015-06-04 14:12:25 -0700151#define PREAMBLE(state) const SkPMColor* SK_RESTRICT table = state.fPixmap.ctable()->readColors()
digit@google.com3ada0ef2012-08-13 14:06:34 +0000152#define RETURNDST(src) table[src]
153#define SRC_TO_FILTER(src) table[src]
mtklein775b8192014-12-02 09:11:25 -0800154#define POSTAMBLE(state)
digit@google.com3ada0ef2012-08-13 14:06:34 +0000155#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)
digit@google.com3ada0ef2012-08-13 14:06:34 +0000161#define SRCTYPE uint8_t
reedad7ae6c2015-06-04 14:12:25 -0700162#define CHECKSTATE(state) SkASSERT(kIndex_8_SkColorType == state.fPixmap.colorType()); \
digit@google.com3ada0ef2012-08-13 14:06:34 +0000163 SkASSERT(state.fAlphaScale < 256)
164#define PREAMBLE(state) unsigned alphaScale = state.fAlphaScale; \
reedad7ae6c2015-06-04 14:12:25 -0700165 const SkPMColor* SK_RESTRICT table = state.fPixmap.ctable()->readColors()
digit@google.com3ada0ef2012-08-13 14:06:34 +0000166#define RETURNDST(src) SkAlphaMulQ(table[src], alphaScale)
167#define SRC_TO_FILTER(src) table[src]
mtklein775b8192014-12-02 09:11:25 -0800168#define POSTAMBLE(state)
digit@google.com3ada0ef2012-08-13 14:06:34 +0000169#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)
digit@google.com3ada0ef2012-08-13 14:06:34 +0000177#define SRCTYPE SkPMColor16
reedad7ae6c2015-06-04 14:12:25 -0700178#define CHECKSTATE(state) SkASSERT(kARGB_4444_SkColorType == state.fPixmap.colorType()); \
digit@google.com3ada0ef2012-08-13 14:06:34 +0000179 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)
digit@google.com3ada0ef2012-08-13 14:06:34 +0000192#define SRCTYPE SkPMColor16
reedad7ae6c2015-06-04 14:12:25 -0700193#define CHECKSTATE(state) SkASSERT(kARGB_4444_SkColorType == state.fPixmap.colorType()); \
digit@google.com3ada0ef2012-08-13 14:06:34 +0000194 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)
digit@google.com3ada0ef2012-08-13 14:06:34 +0000210#define SRCTYPE uint8_t
reedad7ae6c2015-06-04 14:12:25 -0700211#define CHECKSTATE(state) SkASSERT(kAlpha_8_SkColorType == state.fPixmap.colorType());
digit@google.com3ada0ef2012-08-13 14:06:34 +0000212#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
reed0c9b1a82015-03-17 17:44:06 -0700217// 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)
reed0c9b1a82015-03-17 17:44:06 -0700228#define SRCTYPE uint8_t
reedad7ae6c2015-06-04 14:12:25 -0700229#define CHECKSTATE(state) SkASSERT(kGray_8_SkColorType == state.fPixmap.colorType());
reed0c9b1a82015-03-17 17:44:06 -0700230#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
digit@google.com3ada0ef2012-08-13 14:06:34 +0000235
Florin Malitad1c550e2016-12-19 10:55:41 -0500236#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)
digit@google.com3ada0ef2012-08-13 14:06:34 +0000239
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
reedad7ae6c2015-06-04 14:12:25 -0700244#define CHECKSTATE(state) SkASSERT(kIndex_8_SkColorType == state.fPixmap.colorType())
245#define PREAMBLE(state) const SkPMColor* SK_RESTRICT table = state.fPixmap.ctable()->readColors()
digit@google.com3ada0ef2012-08-13 14:06:34 +0000246#define SRC_TO_FILTER(src) table[src]
mtklein775b8192014-12-02 09:11:25 -0800247#define POSTAMBLE(state)
digit@google.com3ada0ef2012-08-13 14:06:34 +0000248#include "SkBitmapProcState_shaderproc.h"
249
reed@google.come4ee35d2012-08-14 13:22:16 +0000250#undef NAME_WRAP