summaryrefslogtreecommitdiff
path: root/drivers/video/fbdev/core/fb_copyarea.h
blob: 53f1d5385c6e41ded02cb850a383fd3f7098672b (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* SPDX-License-Identifier: GPL-2.0-only
 *
 *  Generic bit area copy and twister engine for packed pixel framebuffers
 *
 *      Rewritten by:
 *	Copyright (C)  2025 Zsolt Kajtar (soci@c64.rulez.org)
 *
 *	Based on previous work of:
 *	Copyright (C)  1999-2005 James Simmons <jsimmons@www.infradead.org>
 *	Anton Vorontsov <avorontsov@ru.mvista.com>
 *	Pavel Pisa <pisa@cmp.felk.cvut.cz>
 *	Antonino Daplas <adaplas@hotpop.com>
 *	Geert Uytterhoeven
 *	and others
 *
 * NOTES:
 *
 * Handles native and foreign byte order on both endians, standard and
 * reverse pixel order in a byte (<8 BPP), word length of 32/64 bits,
 * bits per pixel from 1 to the word length. Handles line lengths at byte
 * granularity while maintaining aligned accesses.
 *
 * Optimized routines for word aligned copying and byte aligned copying
 * on reverse pixel framebuffers.
 */
#include "fb_draw.h"

/* used when no reversing is necessary */
static inline unsigned long fb_no_reverse(unsigned long val, struct fb_reverse reverse)
{
	return val;
}

/* modifies the masked area in a word */
static inline void fb_copy_offset_masked(unsigned long mask, int offset,
					 const struct fb_address *dst,
					 const struct fb_address *src)
{
	fb_modify_offset(fb_read_offset(offset, src), mask, offset, dst);
}

/* copies the whole word */
static inline void fb_copy_offset(int offset, const struct fb_address *dst,
				  const struct fb_address *src)
{
	fb_write_offset(fb_read_offset(offset, src), offset, dst);
}

/* forward aligned copy */
static inline void fb_copy_aligned_fwd(const struct fb_address *dst,
				       const struct fb_address *src,
				       int end, struct fb_reverse reverse)
{
	unsigned long first, last;

	first = fb_pixel_mask(dst->bits, reverse);
	last = ~fb_pixel_mask(end & (BITS_PER_LONG-1), reverse);

	/* Same alignment for source and dest */
	if (end <= BITS_PER_LONG) {
		/* Single word */
		last = last ? (last & first) : first;

		/* Trailing bits */
		if (last == ~0UL)
			fb_copy_offset(0, dst, src);
		else
			fb_copy_offset_masked(last, 0, dst, src);
	} else {
		/* Multiple destination words */
		int offset = first != ~0UL;

		/* Leading bits */
		if (offset)
			fb_copy_offset_masked(first, 0, dst, src);

		/* Main chunk */
		end /= BITS_PER_LONG;
		while (offset + 4 <= end) {
			fb_copy_offset(offset + 0, dst, src);
			fb_copy_offset(offset + 1, dst, src);
			fb_copy_offset(offset + 2, dst, src);
			fb_copy_offset(offset + 3, dst, src);
			offset += 4;
		}
		while (offset < end)
			fb_copy_offset(offset++, dst, src);

		/* Trailing bits */
		if (last)
			fb_copy_offset_masked(last, offset, dst, src);
	}
}

/* reverse aligned copy */
static inline void fb_copy_aligned_rev(const struct fb_address *dst,
				       const struct fb_address *src,
				       int end, struct fb_reverse reverse)
{
	unsigned long first, last;

	first = fb_pixel_mask(dst->bits, reverse);
	last = ~fb_pixel_mask(end & (BITS_PER_LONG-1), reverse);

	if (end <= BITS_PER_LONG) {
		/* Single word */
		if (last)
			first &= last;
		if (first == ~0UL)
			fb_copy_offset(0, dst, src);
		else
			fb_copy_offset_masked(first, 0, dst, src);
	} else {
		/* Multiple destination words */
		int offset = first != ~0UL;

		/* Trailing bits */
		end /= BITS_PER_LONG;

		if (last)
			fb_copy_offset_masked(last, end, dst, src);

		/* Main chunk */
		while (end >= offset + 4) {
			fb_copy_offset(end - 1, dst, src);
			fb_copy_offset(end - 2, dst, src);
			fb_copy_offset(end - 3, dst, src);
			fb_copy_offset(end - 4, dst, src);
			end -= 4;
		}
		while (end > offset)
			fb_copy_offset(--end, dst, src);

		/* Leading bits */
		if (offset)
			fb_copy_offset_masked(first, 0, dst, src);
	}
}

static inline void fb_copy_aligned(struct fb_address *dst, struct fb_address *src,
				   int width, u32 height, unsigned int bits_per_line,
				   struct fb_reverse reverse, bool rev_copy)
{
	if (rev_copy)
		while (height--) {
			fb_copy_aligned_rev(dst, src, width + dst->bits, reverse);
			fb_address_backward(dst, bits_per_line);
			fb_address_backward(src, bits_per_line);
		}
	else
		while (height--) {
			fb_copy_aligned_fwd(dst, src, width + dst->bits, reverse);
			fb_address_forward(dst, bits_per_line);
			fb_address_forward(src, bits_per_line);
		}
}

static __always_inline void fb_copy_fwd(const struct fb_address *dst,
					const struct fb_address *src, int width,
					unsigned long (*reorder)(unsigned long val,
								 struct fb_reverse reverse),
					struct fb_reverse reverse)
{
	unsigned long first, last;
	unsigned long d0, d1;
	int end = dst->bits + width;
	int shift, left, right;

	first = fb_pixel_mask(dst->bits, reverse);
	last = ~fb_pixel_mask(end & (BITS_PER_LONG-1), reverse);

	shift = dst->bits - src->bits;
	right = shift & (BITS_PER_LONG - 1);
	left = -shift & (BITS_PER_LONG - 1);

	if (end <= BITS_PER_LONG) {
		/* Single destination word */
		last = last ? (last & first) : first;
		if (shift < 0) {
			d0 = fb_left(reorder(fb_read_offset(-1, src), reverse), left);
			if (src->bits + width > BITS_PER_LONG)
				d0 |= fb_right(reorder(fb_read_offset(0, src), reverse), right);

			if (last == ~0UL)
				fb_write_offset(reorder(d0, reverse), 0, dst);
			else
				fb_modify_offset(reorder(d0, reverse), last, 0, dst);
		} else {
			d0 = fb_right(reorder(fb_read_offset(0, src), reverse), right);
			fb_modify_offset(reorder(d0, reverse), last, 0, dst);
		}
	} else {
		/* Multiple destination words */
		int offset = first != ~0UL;

		/* Leading bits */
		if (shift < 0)
			d0 = reorder(fb_read_offset(-1, src), reverse);
		else
			d0 = 0;

		/* 2 source words */
		if (offset) {
			d1 = reorder(fb_read_offset(0, src), reverse);
			d0 = fb_left(d0, left) | fb_right(d1, right);
			fb_modify_offset(reorder(d0, reverse), first, 0, dst);
			d0 = d1;
		}

		/* Main chunk */
		end /= BITS_PER_LONG;
		if (reorder == fb_no_reverse)
			while (offset + 4 <= end) {
				d1 = fb_read_offset(offset + 0, src);
				d0 = fb_left(d0, left) | fb_right(d1, right);
				fb_write_offset(d0, offset + 0, dst);
				d0 = d1;
				d1 = fb_read_offset(offset + 1, src);
				d0 = fb_left(d0, left) | fb_right(d1, right);
				fb_write_offset(d0, offset + 1, dst);
				d0 = d1;
				d1 = fb_read_offset(offset + 2, src);
				d0 = fb_left(d0, left) | fb_right(d1, right);
				fb_write_offset(d0, offset + 2, dst);
				d0 = d1;
				d1 = fb_read_offset(offset + 3, src);
				d0 = fb_left(d0, left) | fb_right(d1, right);
				fb_write_offset(d0, offset + 3, dst);
				d0 = d1;
				offset += 4;
			}

		while (offset < end) {
			d1 = reorder(fb_read_offset(offset, src), reverse);
			d0 = fb_left(d0, left) | fb_right(d1, right);
			fb_write_offset(reorder(d0, reverse), offset, dst);
			d0 = d1;
			offset++;
		}

		/* Trailing bits */
		if (last) {
			d0 = fb_left(d0, left);
			if (src->bits + width
			    > offset * BITS_PER_LONG + ((shift < 0) ? BITS_PER_LONG : 0))
				d0 |= fb_right(reorder(fb_read_offset(offset, src), reverse),
					       right);
			fb_modify_offset(reorder(d0, reverse), last, offset, dst);
		}
	}
}

static __always_inline void fb_copy_rev(const struct fb_address *dst,
					const struct fb_address *src, int end,
					unsigned long (*reorder)(unsigned long val,
								 struct fb_reverse reverse),
					struct fb_reverse reverse)
{
	unsigned long first, last;
	unsigned long d0, d1;
	int shift, left, right;

	first = fb_pixel_mask(dst->bits, reverse);
	last = ~fb_pixel_mask(end & (BITS_PER_LONG-1), reverse);

	shift = dst->bits - src->bits;
	right = shift & (BITS_PER_LONG-1);
	left = -shift & (BITS_PER_LONG-1);

	if (end <= BITS_PER_LONG) {
		/* Single destination word */
		if (last)
			first &= last;

		if (shift > 0) {
			d0 = fb_right(reorder(fb_read_offset(1, src), reverse), right);
			if (src->bits > left)
				d0 |= fb_left(reorder(fb_read_offset(0, src), reverse), left);
			fb_modify_offset(reorder(d0, reverse), first, 0, dst);
		} else {
			d0 = fb_left(reorder(fb_read_offset(0, src), reverse), left);
			if (src->bits + end - dst->bits > BITS_PER_LONG)
				d0 |= fb_right(reorder(fb_read_offset(1, src), reverse), right);
			if (first == ~0UL)
				fb_write_offset(reorder(d0, reverse), 0, dst);
			else
				fb_modify_offset(reorder(d0, reverse), first, 0, dst);
		}
	} else {
		/* Multiple destination words */
		int offset = first != ~0UL;

		end /= BITS_PER_LONG;

		/* 2 source words */
		if (fb_right(~0UL, right) & last)
			d0 = fb_right(reorder(fb_read_offset(end + 1, src), reverse), right);
		else
			d0 = 0;

		/* Trailing bits */
		d1 = reorder(fb_read_offset(end, src), reverse);
		if (last)
			fb_modify_offset(reorder(fb_left(d1, left) | d0, reverse),
					 last, end, dst);
		d0 = d1;

		/* Main chunk */
		if (reorder == fb_no_reverse)
			while (end >= offset + 4) {
				d1 = fb_read_offset(end - 1, src);
				d0 = fb_left(d1, left) | fb_right(d0, right);
				fb_write_offset(d0, end - 1, dst);
				d0 = d1;
				d1 = fb_read_offset(end - 2, src);
				d0 = fb_left(d1, left) | fb_right(d0, right);
				fb_write_offset(d0, end - 2, dst);
				d0 = d1;
				d1 = fb_read_offset(end - 3, src);
				d0 = fb_left(d1, left) | fb_right(d0, right);
				fb_write_offset(d0, end - 3, dst);
				d0 = d1;
				d1 = fb_read_offset(end - 4, src);
				d0 = fb_left(d1, left) | fb_right(d0, right);
				fb_write_offset(d0, end - 4, dst);
				d0 = d1;
				end -= 4;
			}

		while (end > offset) {
			end--;
			d1 = reorder(fb_read_offset(end, src), reverse);
			d0 = fb_left(d1, left) | fb_right(d0, right);
			fb_write_offset(reorder(d0, reverse), end, dst);
			d0 = d1;
		}

		/* Leading bits */
		if (offset) {
			d0 = fb_right(d0, right);
			if (src->bits > left)
				d0 |= fb_left(reorder(fb_read_offset(0, src), reverse), left);
			fb_modify_offset(reorder(d0, reverse), first, 0, dst);
		}
	}
}

static __always_inline void fb_copy(struct fb_address *dst, struct fb_address *src,
				    int width, u32 height, unsigned int bits_per_line,
				    unsigned long (*reorder)(unsigned long val,
							     struct fb_reverse reverse),
				    struct fb_reverse reverse, bool rev_copy)
{
	if (rev_copy)
		while (height--) {
			int move = src->bits < dst->bits ? -1 : 0;

			fb_address_move_long(src, move);
			fb_copy_rev(dst, src, width + dst->bits, reorder, reverse);
			fb_address_backward(dst, bits_per_line);
			fb_address_backward(src, bits_per_line);
			fb_address_move_long(src, -move);
		}
	else
		while (height--) {
			int move = src->bits > dst->bits ? 1 : 0;

			fb_address_move_long(src, move);
			fb_copy_fwd(dst, src, width, reorder, reverse);
			fb_address_forward(dst, bits_per_line);
			fb_address_forward(src, bits_per_line);
			fb_address_move_long(src, -move);
		}
}

static inline void fb_copyarea(struct fb_info *p, const struct fb_copyarea *area)
{
	int bpp = p->var.bits_per_pixel;
	u32 dy = area->dy;
	u32 sy = area->sy;
	u32 height = area->height;
	int width = area->width * bpp;
	unsigned int bits_per_line = BYTES_TO_BITS(p->fix.line_length);
	struct fb_reverse reverse = fb_reverse_init(p);
	struct fb_address dst = fb_address_init(p);
	struct fb_address src = dst;
	bool rev_copy = (dy > sy) || (dy == sy && area->dx > area->sx);

	if (rev_copy) {
		dy += height - 1;
		sy += height - 1;
	}
	fb_address_forward(&dst, dy*bits_per_line + area->dx*bpp);
	fb_address_forward(&src, sy*bits_per_line + area->sx*bpp);

	if (src.bits == dst.bits)
		fb_copy_aligned(&dst, &src, width, height, bits_per_line, reverse, rev_copy);
	else if (!reverse.byte && (!reverse.pixel ||
				     !((src.bits ^ dst.bits) & (BITS_PER_BYTE-1)))) {
		fb_copy(&dst, &src, width, height, bits_per_line,
			fb_no_reverse, reverse, rev_copy);
	} else
		fb_copy(&dst, &src, width, height, bits_per_line,
			fb_reverse_long, reverse, rev_copy);
}