summaryrefslogtreecommitdiff
path: root/src/draw/ao_blt.c
blob: e3f45221c9091612d2de59daf5d8e0ccc716aa20 (plain) (blame)
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
/*
 * Copyright © 2016 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

#include "ao.h"
#include "ao_draw.h"
#include "ao_draw_int.h"

#define O 0
#define I AO_ALLONES

struct ao_merge_rop {
	uint32_t	ca1, cx1, ca2, cx2;
};

const struct ao_merge_rop ao_merge_rop[16] = {
    {O, O, O, O},               /* clear         0x0         0 */
    {I, O, O, O},               /* and           0x1         src AND dst */
    {I, O, I, O},               /* andReverse    0x2         src AND NOT dst */
    {O, O, I, O},               /* copy          0x3         src */
    {I, I, O, O},               /* andInverted   0x4         NOT src AND dst */
    {O, I, O, O},               /* noop          0x5         dst */
    {O, I, I, O},               /* xor           0x6         src XOR dst */
    {I, I, I, O},               /* or            0x7         src OR dst */
    {I, I, I, I},               /* nor           0x8         NOT src AND NOT dst */
    {O, I, I, I},               /* equiv         0x9         NOT src XOR dst */
    {O, I, O, I},               /* invert        0xa         NOT dst */
    {I, I, O, I},               /* orReverse     0xb         src OR NOT dst */
    {O, O, I, I},               /* copyInverted  0xc         NOT src */
    {I, O, I, I},               /* orInverted    0xd         NOT src OR dst */
    {I, O, O, I},               /* nand          0xe         NOT src OR NOT dst */
    {O, O, O, I},               /* set           0xf         1 */
};

#define ao_do_merge_rop(src, dst) \
    (((dst) & (((src) & _ca1) ^ _cx1)) ^ (((src) & _ca2) ^ _cx2))

#define ao_do_dst_invarient_merge_rop(src)	(((src) & _ca2) ^ _cx2)

#define ao_do_mask_merge_rop(src, dst, mask) \
    (((dst) & ((((src) & _ca1) ^ _cx1) | ~(mask))) ^ ((((src) & _ca2) ^ _cx2) & (mask)))

#define ao_dst_invarient_merge_rop()   (_ca1 == 0 && _cx1 == 0)

void
ao_blt(uint32_t		*src_line,
       int16_t		src_stride,
       int16_t		src_x,
       uint32_t		*dst_line,
       int16_t		dst_stride,
       int16_t		dst_x,
       int16_t		width,
       int16_t		height,
       uint8_t		rop,
       uint8_t		reverse,
       uint8_t		upsidedown)
{
	uint32_t	*src, *dst;
	uint32_t	_ca1, _cx1, _ca2, _cx2;
	uint8_t		dst_invarient;
	uint32_t	startmask, endmask;
	int16_t		nmiddle, n;
	uint32_t	bits1, bits;
	int16_t		left_shift, right_shift;

	_ca1 = ao_merge_rop[rop].ca1;
	_cx1 = ao_merge_rop[rop].cx1;
	_ca2 = ao_merge_rop[rop].ca2;
	_cx2 = ao_merge_rop[rop].cx2;
	dst_invarient = ao_dst_invarient_merge_rop();

	if (upsidedown) {
		src_line += (height - 1) * src_stride;
		dst_line += (height - 1) * dst_stride;
		src_stride = -src_stride;
		dst_stride = -dst_stride;
	}

	ao_mask_bits(dst_x, width, startmask, nmiddle, endmask);
	if (reverse) {
		src_line += ((src_x + width - 1) >> AO_SHIFT) + 1;
		dst_line += ((dst_x + width - 1) >> AO_SHIFT) + 1;
		src_x = (src_x + width - 1) & AO_MASK;
		dst_x = (dst_x + width - 1) & AO_MASK;
	} else {
		src_line += src_x >> AO_SHIFT;
		dst_line += dst_x >> AO_SHIFT;
		src_x &= AO_MASK;
		dst_x &= AO_MASK;
	}
	if (src_x == dst_x) {
		while (height--) {
			src = src_line;
			src_line += src_stride;
			dst = dst_line;
			dst_line += dst_stride;
			if (reverse) {
				if (endmask) {
					bits = *--src;
					--dst;
					*dst = ao_do_mask_merge_rop(bits, *dst, endmask);
				}
				n = nmiddle;
				if (dst_invarient) {
					while (n--)
						*--dst = ao_do_dst_invarient_merge_rop(*--src);
				}
				else {
					while (n--) {
						bits = *--src;
						--dst;
						*dst = ao_do_merge_rop(bits, *dst);
					}
				}
				if (startmask) {
					bits = *--src;
					--dst;
					*dst = ao_do_mask_merge_rop(bits, *dst, startmask);
				}
			}
			else {
				if (startmask) {
					bits = *src++;
					*dst = ao_do_mask_merge_rop(bits, *dst, startmask);
					dst++;
				}
				n = nmiddle;
				if (dst_invarient) {
					while (n--)
						*dst++ = ao_do_dst_invarient_merge_rop(*src++);
				}
				else {
					while (n--) {
						bits = *src++;
						*dst = ao_do_merge_rop(bits, *dst);
						dst++;
					}
				}
				if (endmask) {
					bits = *src;
					*dst = ao_do_mask_merge_rop(bits, *dst, endmask);
				}
			}
		}
	} else {
		if (src_x > dst_x) {
			left_shift = src_x - dst_x;
			right_shift = AO_UNIT - left_shift;
		} else {
			right_shift = dst_x - src_x;
			left_shift = AO_UNIT - right_shift;
		}
		while (height--) {
			src = src_line;
			src_line += src_stride;
			dst = dst_line;
			dst_line += dst_stride;

			bits1 = 0;
			if (reverse) {
				if (src_x < dst_x)
					bits1 = *--src;
				if (endmask) {
					bits = ao_right(bits1, right_shift);
					if (ao_right(endmask, left_shift)) {
						bits1 = *--src;
						bits |= ao_left(bits1, left_shift);
					}
					--dst;
					*dst = ao_do_mask_merge_rop(bits, *dst, endmask);
				}
				n = nmiddle;
				if (dst_invarient) {
					while (n--) {
						bits = ao_right(bits1, right_shift);
						bits1 = *--src;
						bits |= ao_left(bits1, left_shift);
						--dst;
						*dst = ao_do_dst_invarient_merge_rop(bits);
					}
				} else {
					while (n--) {
						bits = ao_right(bits1, right_shift);
						bits1 = *--src;
						bits |= ao_left(bits1, left_shift);
						--dst;
						*dst = ao_do_merge_rop(bits, *dst);
					}
				}
				if (startmask) {
					bits = ao_right(bits1, right_shift);
					if (ao_right(startmask, left_shift)) {
						bits1 = *--src;
						bits |= ao_left(bits1, left_shift);
					}
					--dst;
					*dst = ao_do_mask_merge_rop(bits, *dst, startmask);
				}
			}
			else {
				if (src_x > dst_x)
					bits1 = *src++;
				if (startmask) {
					bits = ao_left(bits1, left_shift);
					if (ao_left(startmask, right_shift)) {
						bits1 = *src++;
						bits |= ao_right(bits1, right_shift);
					}
					*dst = ao_do_mask_merge_rop(bits, *dst, startmask);
					dst++;
				}
				n = nmiddle;
				if (dst_invarient) {
					while (n--) {
						bits = ao_left(bits1, left_shift);
						bits1 = *src++;
						bits |= ao_right(bits1, right_shift);
						*dst = ao_do_dst_invarient_merge_rop(bits);
						dst++;
					}
				}
				else {
					while (n--) {
						bits = ao_left(bits1, left_shift);
						bits1 = *src++;
						bits |= ao_right(bits1, right_shift);
						*dst = ao_do_merge_rop(bits, *dst);
						dst++;
					}
				}
				if (endmask) {
					bits = ao_left(bits1, left_shift);
					if (ao_left(endmask, right_shift)) {
						bits1 = *src;
						bits |= ao_right(bits1, right_shift);
					}
					*dst = ao_do_mask_merge_rop(bits, *dst, endmask);
				}
			}
		}
	}
}

void
ao_solid(uint32_t	and,
	 uint32_t	xor,
	 uint32_t	*dst,
	 int16_t	dst_stride,
	 int16_t	dst_x,
	 int16_t	width,
	 int16_t	height)
{
	uint32_t	startmask, endmask;
	int16_t		nmiddle;
	int16_t		n;

	dst += dst_x >> AO_SHIFT;
	dst_x &= AO_MASK;

	ao_mask_bits(dst_x, width, startmask, nmiddle, endmask);

	if (startmask)
		dst_stride--;

	dst_stride -= nmiddle;
	while (height--) {
		if (startmask) {
			*dst = ao_do_mask_rrop(*dst, and, xor, startmask);
			dst++;
		}
		n = nmiddle;
		if (!and)
			while (n--)
				*dst++ = xor;
		else
			while (n--) {
				*dst = ao_do_rrop(*dst, and, xor);
				dst++;
			}
		if (endmask)
			*dst = ao_do_mask_rrop(*dst, and, xor, endmask);
		dst += dst_stride;
	}
}