rasops_bitops.h revision 1.24 1 /* $NetBSD: rasops_bitops.h,v 1.24 2019/08/07 12:36:36 rin Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef _RASOPS_BITOPS_H_
33 #define _RASOPS_BITOPS_H_ 1
34
35 #if RASOPS_DEPTH == 1
36 #define PIXEL_SHIFT 0
37 #elif RASOPS_DEPTH == 2
38 #define PIXEL_SHIFT 1
39 #elif RASOPS_DEPTH == 4
40 #define PIXEL_SHIFT 2
41 #else
42 #error "Depth not supported"
43 #endif
44
45 #define NAME(name) NAME1(RASOPS_DEPTH, name)
46 #define NAME1(depth, name) NAME2(depth, name)
47 #define NAME2(depth, name) rasops ## depth ## _ ## name
48
49 /*
50 * Erase columns.
51 */
52 static void
53 NAME(erasecols)(void *cookie, int row, int col, int num, long attr)
54 {
55 struct rasops_info *ri = (struct rasops_info *)cookie;
56 uint32_t lclr, rclr, clr;
57 uint32_t *dp, *rp, *hp, tmp, lmask, rmask;
58 int height, cnt;
59
60 hp = NULL; /* XXX GCC */
61
62 #ifdef RASOPS_CLIPPING
63 if ((unsigned)row >= (unsigned)ri->ri_rows)
64 return;
65
66 if (col < 0) {
67 num += col;
68 col = 0;
69 }
70
71 if (col + num > ri->ri_cols)
72 num = ri->ri_cols - col;
73
74 if (num <= 0)
75 return;
76 #endif
77
78 col *= ri->ri_font->fontwidth << PIXEL_SHIFT;
79 num *= ri->ri_font->fontwidth << PIXEL_SHIFT;
80 height = ri->ri_font->fontheight;
81 clr = ri->ri_devcmap[((uint32_t)attr >> 16) & 0xf];
82 rp = (uint32_t *)(ri->ri_bits + row*ri->ri_yscale + ((col >> 3) & ~3));
83 if (ri->ri_hwbits)
84 hp = (uint32_t *)(ri->ri_hwbits + row*ri->ri_yscale +
85 ((col >> 3) & ~3));
86 col &= 31;
87
88 if (col + num <= 32) {
89 lmask = ~rasops_pmask[col][num & 31];
90 lclr = clr & ~lmask;
91
92 while (height--) {
93 dp = rp;
94 DELTA(rp, ri->ri_stride, uint32_t *);
95
96 tmp = (*dp & lmask) | lclr;
97 *dp = tmp;
98 if (ri->ri_hwbits) {
99 *hp = tmp;
100 DELTA(hp, ri->ri_stride, uint32_t *);
101 }
102 }
103 } else {
104 lmask = rasops_rmask[col];
105 rmask = rasops_lmask[(col + num) & 31];
106
107 if (lmask)
108 num = (num - (32 - col)) >> 5;
109 else
110 num = num >> 5;
111
112 lclr = clr & ~lmask;
113 rclr = clr & ~rmask;
114
115 while (height--) {
116 dp = rp;
117
118 if (lmask) {
119 *dp = (*dp & lmask) | lclr;
120 dp++;
121 }
122
123 for (cnt = num; cnt > 0; cnt--)
124 *dp++ = clr;
125
126 if (rmask)
127 *dp = (*dp & rmask) | rclr;
128
129 if (ri->ri_hwbits) {
130 memcpy(hp, rp, ((lmask != 0) + num +
131 (rmask != 0)) << 2);
132 DELTA(hp, ri->ri_stride, uint32_t *);
133 }
134 DELTA(rp, ri->ri_stride, uint32_t *);
135 }
136 }
137 }
138
139 /*
140 * Actually paint the cursor.
141 */
142 static void
143 NAME(do_cursor)(struct rasops_info *ri)
144 {
145 int height, row, col, num, cnt;
146 uint32_t *dp, *rp, *hp, tmp, lmask, rmask;
147
148 hp = NULL; /* XXX GCC */
149
150 row = ri->ri_crow;
151 col = ri->ri_ccol * ri->ri_font->fontwidth << PIXEL_SHIFT;
152 height = ri->ri_font->fontheight;
153 num = ri->ri_font->fontwidth << PIXEL_SHIFT;
154 rp = (uint32_t *)(ri->ri_bits + row * ri->ri_yscale +
155 ((col >> 3) & ~3));
156 if (ri->ri_hwbits)
157 hp = (uint32_t *)(ri->ri_hwbits + row * ri->ri_yscale +
158 ((col >> 3) & ~3));
159 col &= 31;
160
161 if (col + num <= 32) {
162 lmask = rasops_pmask[col][num & 31];
163
164 while (height--) {
165 tmp = *rp ^ lmask;
166 *rp = tmp;
167 if (ri->ri_hwbits) {
168 *hp = tmp;
169 DELTA(hp, ri->ri_stride, uint32_t *);
170 }
171 DELTA(rp, ri->ri_stride, uint32_t *);
172 }
173 } else {
174 lmask = ~rasops_rmask[col];
175 rmask = ~rasops_lmask[(col + num) & 31];
176
177 if (lmask != -1)
178 num = (num - (32 - col)) >> 5;
179 else
180 num = num >> 5;
181
182 while (height--) {
183 dp = rp;
184
185 if (lmask != -1) {
186 *dp = *dp ^ lmask;
187 dp++;
188 }
189
190 for (cnt = num; cnt; cnt--) {
191 *dp = ~*dp;
192 dp++;
193 }
194
195 if (rmask != -1)
196 *dp = *dp ^ rmask;
197
198 if (ri->ri_hwbits) {
199 memcpy(hp, rp, ((lmask != -1) + num +
200 (rmask != -1)) << 2);
201 DELTA(hp, ri->ri_stride, uint32_t *);
202 }
203
204 DELTA(rp, ri->ri_stride, uint32_t *);
205 }
206 }
207 }
208
209 /*
210 * Copy columns. Ick!
211 */
212 static void
213 NAME(copycols)(void *cookie, int row, int src, int dst, int num)
214 {
215 struct rasops_info *ri = (struct rasops_info *)cookie;
216 int height, lnum, rnum, sb, db, cnt, full;
217 uint32_t tmp, lmask, rmask;
218 uint32_t *sp, *dp, *srp, *drp, *dhp, *hp;
219
220 dhp = hp = NULL; /* XXX GCC */
221
222 #ifdef RASOPS_CLIPPING
223 if (dst == src)
224 return;
225
226 /* Catches < 0 case too */
227 if ((unsigned)row >= (unsigned)ri->ri_rows)
228 return;
229
230 if (src < 0) {
231 num += src;
232 src = 0;
233 }
234
235 if (src + num > ri->ri_cols)
236 num = ri->ri_cols - src;
237
238 if (dst < 0) {
239 num += dst;
240 dst = 0;
241 }
242
243 if (dst + num > ri->ri_cols)
244 num = ri->ri_cols - dst;
245
246 if (num <= 0)
247 return;
248 #endif
249
250 cnt = ri->ri_font->fontwidth << PIXEL_SHIFT;
251 src *= cnt;
252 dst *= cnt;
253 num *= cnt;
254 row *= ri->ri_yscale;
255 height = ri->ri_font->fontheight;
256 db = dst & 31;
257
258 if (db + num <= 32) {
259 /* Destination is contained within a single word */
260 srp = (uint32_t *)(ri->ri_bits + row + ((src >> 3) & ~3));
261 drp = (uint32_t *)(ri->ri_bits + row + ((dst >> 3) & ~3));
262 if (ri->ri_hwbits)
263 dhp = (uint32_t *)(ri->ri_hwbits + row +
264 ((dst >> 3) & ~3));
265 sb = src & 31;
266
267 while (height--) {
268 GETBITS(srp, sb, num, tmp);
269 PUTBITS(tmp, db, num, drp);
270 if (ri->ri_hwbits) {
271 PUTBITS(tmp, db, num, dhp);
272 DELTA(dhp, ri->ri_stride, uint32_t *);
273 }
274 DELTA(srp, ri->ri_stride, uint32_t *);
275 DELTA(drp, ri->ri_stride, uint32_t *);
276 }
277
278 return;
279 }
280
281 lmask = rasops_rmask[db];
282 rmask = rasops_lmask[(dst + num) & 31];
283 lnum = (32 - db) & 31;
284 rnum = (dst + num) & 31;
285
286 if (lmask)
287 full = (num - lnum) >> 5;
288 else
289 full = num >> 5;
290
291 if (src < dst && src + num > dst) {
292 /* Copy right-to-left */
293 bool sbover;
294 int sboff;
295
296 srp = (uint32_t *)(ri->ri_bits + row +
297 (((src + num) >> 3) & ~3));
298 drp = (uint32_t *)(ri->ri_bits + row +
299 (((dst + num) >> 3) & ~3));
300 if (ri->ri_hwbits)
301 dhp = (uint32_t *)(ri->ri_hwbits + row +
302 (((dst + num) >> 3) & ~3));
303
304 sb = src & 31;
305 sbover = (sb + lnum) >= 32;
306 sboff = (src + num) & 31;
307 if ((sboff -= rnum) < 0) {
308 srp--;
309 sboff += 32;
310 }
311
312 while (height--) {
313 sp = srp;
314 dp = drp;
315
316 if (rnum) {
317 GETBITS(sp, sboff, rnum, tmp);
318 PUTBITS(tmp, 0, rnum, dp);
319 }
320
321 /* Now aligned to 32-bits wrt dp */
322 for (cnt = full; cnt; cnt--) {
323 --dp;
324 --sp;
325 GETBITS(sp, sboff, 32, tmp);
326 *dp = tmp;
327 }
328
329 if (lmask) {
330 if (sbover)
331 --sp;
332 --dp;
333 GETBITS(sp, sb, lnum, tmp);
334 PUTBITS(tmp, db, lnum, dp);
335 }
336
337 if (ri->ri_hwbits) {
338 hp = dhp;
339 hp -= (lmask != 0) + full;
340 memcpy(hp, dp, ((lmask != 0) + full +
341 (rnum != 0)) << 2);
342 DELTA(dhp, ri->ri_stride, uint32_t *);
343 }
344
345 DELTA(srp, ri->ri_stride, uint32_t *);
346 DELTA(drp, ri->ri_stride, uint32_t *);
347 }
348 } else {
349 /* Copy left-to-right */
350 srp = (uint32_t *)(ri->ri_bits + row + ((src >> 3) & ~3));
351 drp = (uint32_t *)(ri->ri_bits + row + ((dst >> 3) & ~3));
352 if (ri->ri_hwbits)
353 dhp = (uint32_t *)(ri->ri_hwbits + row +
354 ((dst >> 3) & ~3));
355
356 while (height--) {
357 sb = src & 31;
358 sp = srp;
359 dp = drp;
360
361 if (lmask) {
362 GETBITS(sp, sb, lnum, tmp);
363 PUTBITS(tmp, db, lnum, dp);
364 dp++;
365
366 sb += lnum;
367 if (sb > 31) {
368 sp++;
369 sb -= 32;
370 }
371 }
372
373 /* Now aligned to 32-bits wrt dp */
374 for (cnt = full; cnt; cnt--, sp++) {
375 GETBITS(sp, sb, 32, tmp);
376 *dp++ = tmp;
377 }
378
379 if (rmask) {
380 GETBITS(sp, sb, rnum, tmp);
381 PUTBITS(tmp, 0, rnum, dp);
382 }
383
384 if (ri->ri_hwbits) {
385 memcpy(dhp, drp, ((lmask != 0) + full +
386 (rmask != 0)) << 2);
387 DELTA(dhp, ri->ri_stride, uint32_t *);
388 }
389
390 DELTA(srp, ri->ri_stride, uint32_t *);
391 DELTA(drp, ri->ri_stride, uint32_t *);
392 }
393 }
394 }
395
396 #undef PIXEL_SHIFT
397
398 #undef NAME
399 #undef NAME1
400 #undef NAME2
401
402 #endif /* _RASOPS_BITOPS_H_ */
403