summaryrefslogtreecommitdiff
path: root/sys/dev/rasops/rasops_putchar_width.h
blob: 8e890bc990c576b2bc101354f09f533b43ca8914 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/* $NetBSD: rasops_putchar_width.h,v 1.9.2.1 2019/08/15 12:21:27 martin Exp $ */

/* NetBSD: rasops8.c,v 1.41 2019/07/25 03:02:44 rin Exp  */
/*-
 * Copyright (c) 1999 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Andrew Doran.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#if RASOPS_DEPTH !=  2 && RASOPS_DEPTH !=  4 && RASOPS_DEPTH !=  8 &&	\
    RASOPS_DEPTH != 15 && RASOPS_DEPTH != 24 && RASOPS_DEPTH != 32
#error "Depth not supported"
#endif

#if RASOPS_WIDTH != 8 && RASOPS_WIDTH != 12 && RASOPS_WIDTH != 16
#error "Width not supported"
#endif

#if   RASOPS_DEPTH == 2
#define	STAMP_TYPE	uint8_t
#elif RASOPS_DEPTH == 4
#define	STAMP_TYPE	uint16_t
#else
#define	STAMP_TYPE	uint32_t
#endif

#if   RASOPS_DEPTH <= 8
#define	SUBST_UNIT	1
#elif RASOPS_DEPTH == 15
#define	SUBST_UNIT	2
#elif RASOPS_DEPTH == 24
#define	SUBST_UNIT	3
#elif RASOPS_DEPTH == 32
#define	SUBST_UNIT	4
#endif

#define	SUBST_BYTES	(SUBST_UNIT * (RASOPS_WIDTH / 4) * sizeof(STAMP_TYPE))

#if   RASOPS_DEPTH <= 8
#define	FILLED_STAMP	15
#elif RASOPS_DEPTH == 15
#define	FILLED_STAMP	30
#else
#define	FILLED_STAMP	60
#endif

/* ################################################################### */

#if RASOPS_DEPTH <= 8

#define	SUBST_STAMP1(off, base)						\
	rp[(off) * 1 + 0] = stamp[base]

#define	SUBST_GLYPH1(index, nibble, off)				\
	do {								\
		int so = STAMP_SHIFT(fr[index], nibble) & STAMP_MASK;	\
		rp[(off) * 1 + 0] = STAMP_READ(so);			\
	} while (0 /* CONSTCOND */)

#endif /* RASOPS_DEPTH <= 8 */

/* ################################################################### */

#if RASOPS_DEPTH == 15

#define	SUBST_STAMP1(off, base)						\
	rp[(off) * 2 + 0] = rp[(off) * 2 + 1] = stamp[base]

#define	SUBST_GLYPH1(index, nibble, off)				\
	do {								\
		int so = STAMP_SHIFT(fr[index], nibble) & STAMP_MASK;	\
		rp[(off) * 2 + 0] = STAMP_READ(so);			\
		rp[(off) * 2 + 1] = STAMP_READ(so +  4);		\
	} while (0 /* CONSTCOND */)

#endif /* RASOPS_DEPTH == 15 */

/* ################################################################### */

#if RASOPS_DEPTH == 24

#define	SUBST_STAMP1(off, base)						\
	do {								\
		rp[(off) * 3 + 0] = stamp[(base) + 0];			\
		rp[(off) * 3 + 1] = stamp[(base) + 1];			\
		rp[(off) * 3 + 2] = stamp[(base) + 2];			\
	} while (0 /* CONSTCOND */)

#define	SUBST_GLYPH1(index, nibble, off)				\
	do {								\
		int so = STAMP_SHIFT(fr[index], nibble) & STAMP_MASK;	\
		rp[(off) * 3 + 0] = STAMP_READ(so);			\
		rp[(off) * 3 + 1] = STAMP_READ(so +  4);		\
		rp[(off) * 3 + 2] = STAMP_READ(so +  8);		\
	} while (0 /* CONSTCOND */)

#endif /* RASOPS_DEPTH == 24 */

/* ################################################################### */

#if RASOPS_DEPTH == 32

#define	SUBST_STAMP1(off, base)						\
	rp[(off) * 4 + 0] = rp[(off) * 4 + 1] =				\
	rp[(off) * 4 + 2] = rp[(off) * 4 + 3] = stamp[base]

#define	SUBST_GLYPH1(index, nibble, off)				\
	do {								\
		int so = STAMP_SHIFT(fr[index], nibble) & STAMP_MASK;	\
		rp[(off) * 4 + 0] = STAMP_READ(so);			\
		rp[(off) * 4 + 1] = STAMP_READ(so +  4);		\
		rp[(off) * 4 + 2] = STAMP_READ(so +  8);		\
		rp[(off) * 4 + 3] = STAMP_READ(so + 12);		\
	} while (0 /* CONSTCOND */)

#endif /* RASOPS_DEPTH == 32 */

/* ################################################################### */

#if   RASOPS_WIDTH == 8
#define	SUBST_STAMP(base) 			\
	do {					\
		SUBST_STAMP1(0, base);		\
		SUBST_STAMP1(1, base);		\
	} while (0 /* CONSTCOND */)
#elif RASOPS_WIDTH == 12
#define	SUBST_STAMP(base)			\
	do {					\
		SUBST_STAMP1(0, base);		\
		SUBST_STAMP1(1, base);		\
		SUBST_STAMP1(2, base);		\
	} while (0 /* CONSTCOND */)
#elif RASOPS_WIDTH == 16
#define	SUBST_STAMP(base)			\
	do {					\
		SUBST_STAMP1(0, base);		\
		SUBST_STAMP1(1, base);		\
		SUBST_STAMP1(2, base);		\
		SUBST_STAMP1(3, base);		\
	} while (0 /* CONSTCOND */)
#endif

/* ################################################################### */

#if   RASOPS_WIDTH == 8
#define	SUBST_GLYPH				\
	do {					\
		SUBST_GLYPH1(0, 1, 0);		\
		SUBST_GLYPH1(0, 0, 1);		\
	} while (0 /* CONSTCOND */)
#elif RASOPS_WIDTH == 12
#define	SUBST_GLYPH				\
	do {					\
		SUBST_GLYPH1(0, 1, 0);		\
		SUBST_GLYPH1(0, 0, 1);		\
		SUBST_GLYPH1(1, 1, 2);		\
	} while (0 /* CONSTCOND */)
#elif RASOPS_WIDTH == 16
#define	SUBST_GLYPH				\
	do {					\
		SUBST_GLYPH1(0, 1, 0);		\
		SUBST_GLYPH1(0, 0, 1);		\
		SUBST_GLYPH1(1, 1, 2);		\
		SUBST_GLYPH1(1, 0, 3);		\
	} while (0 /* CONSTCOND */)
#endif

/* ################################################################### */

#define	NAME(depth, width)	NAME1(depth, width)
#define	NAME1(depth, width)	rasops ## depth ## _putchar ## width

#define	PUTCHAR(depth)		PUTCHAR1(depth)
#define	PUTCHAR1(depth)		rasops ## depth ## _putchar

#define	MAKESTAMP(depth)	MAKESTAMP1(depth)
#define	MAKESTAMP1(depth)	rasops ## depth ## _makestamp

/*
 * Width-optimized putchar function.
 */
static void
NAME(RASOPS_DEPTH, RASOPS_WIDTH)(void *cookie, int row, int col, u_int uc,
    long attr)
{
	struct rasops_info *ri = (struct rasops_info *)cookie;
	struct wsdisplay_font *font = PICK_FONT(ri, uc);
	int height;
	uint8_t *fr;
	bool do_ul;
	STAMP_TYPE *rp, *hp;

	hp = NULL; /* XXX GCC */

	/* check if character fits into font limits */
	if (__predict_false(!CHAR_IN_FONT(uc, font)))
		return;

#ifdef RASOPS_CLIPPING
	/* Catches 'row < 0' case too */
	if ((unsigned)row >= (unsigned)ri->ri_rows)
		return;

	if ((unsigned)col >= (unsigned)ri->ri_cols)
		return;
#endif

	/*
	 * We don't care attributions other than back/foreground
	 * colors when using stamp.
	 */
	do_ul = (attr & WSATTR_UNDERLINE) != 0;
	attr &= (ATTR_MASK_BG | ATTR_MASK_FG);

	/* Recompute stamp? */
	if (attr != stamp_attr || __predict_false(ri != stamp_ri))
		MAKESTAMP(RASOPS_DEPTH)(ri, attr);

	height = font->fontheight;

	rp = (STAMP_TYPE *)(ri->ri_bits + FBOFFSET(ri, row, col));
	if (ri->ri_hwbits)
		hp = (STAMP_TYPE *)(ri->ri_hwbits + FBOFFSET(ri, row, col));

	if (uc == ' ') {
		while (height--) {
			SUBST_STAMP(0);
			if (ri->ri_hwbits) {
				memcpy(hp, rp, SUBST_BYTES);
				DELTA(hp, ri->ri_stride, STAMP_TYPE *);
			}
			DELTA(rp, ri->ri_stride, STAMP_TYPE *);
		}
	} else {
		fr = FONT_GLYPH(uc, font, ri);
		while (height--) {
			SUBST_GLYPH;
			fr += font->stride;
			if (ri->ri_hwbits) {
				memcpy(hp, rp, SUBST_BYTES);
				DELTA(hp, ri->ri_stride, STAMP_TYPE *);
			}
			DELTA(rp, ri->ri_stride, STAMP_TYPE *);
		}
	}

	/* Do underline */
	if (do_ul) {
		DELTA(rp, - ri->ri_stride * ri->ri_ul.off, STAMP_TYPE *);
		if (ri->ri_hwbits)
			DELTA(hp, - ri->ri_stride * ri->ri_ul.off,
			    STAMP_TYPE *);

		for (height = ri->ri_ul.height; height; height--) {
			DELTA(rp, - ri->ri_stride, STAMP_TYPE *);
			SUBST_STAMP(FILLED_STAMP);
			if (ri->ri_hwbits) {
				DELTA(hp, - ri->ri_stride, STAMP_TYPE *);
				memcpy(hp, rp, SUBST_BYTES);
			}
		}
	}
}

#undef	STAMP_TYPE

#undef	SUBST_UNIT
#undef	SUBST_BYTES

#undef	FILLED_STAMP

#undef	SUBST_STAMP1
#undef	SUBST_STAMP

#undef	SUBST_GLYPH1
#undef	SUBST_GLYPH

#undef	NAME
#undef	NAME1

#undef	PUTCHAR
#undef	PUTCHAR1

#undef	MAKESTAMP
#undef	MAKESTAMP1