summaryrefslogtreecommitdiff
path: root/drivers/video/cfbfillrect.c
blob: 0fbc695a85d860f6e1423299379012d63254bfec (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
/*
 *  Generic fillrect for frame buffers with packed pixels of any depth. 
 *
 *      Copyright (C)  2000 James Simmons (jsimmons@linux-fbdev.org) 
 *
 *  This file is subject to the terms and conditions of the GNU General Public
 *  License.  See the file COPYING in the main directory of this archive for
 *  more details.
 *
 * NOTES:
 *
 *  The code for depths like 24 that don't have integer number of pixels per 
 *  long is broken and needs to be fixed. For now I turned these types of 
 *  mode off.
 *
 *  Also need to add code to deal with cards endians that are different than
 *  the native cpu endians. I also need to deal with MSB position in the word.
 *
 */
#include <linux/string.h>
#include <linux/fb.h>
#include <asm/types.h>
#include <video/fbcon.h>

void cfb_fillrect(struct fb_info *p, struct fb_fillrect *rect)
{
	unsigned long start_index, end_index, start_mask = 0, end_mask = 0;
	unsigned long height, ppw, fg, fgcolor;
	int i, n, x2, y2, linesize = p->fix.line_length;
	int bpl = sizeof(unsigned long);
	unsigned long *dst;
	char *dst1;

	if (!rect->width || !rect->height)
		return;

	/* We could use hardware clipping but on many cards you get around
	 * hardware clipping by writing to framebuffer directly. */
	x2 = rect->dx + rect->width;
	y2 = rect->dy + rect->height;
	x2 = x2 < p->var.xres_virtual ? x2 : p->var.xres_virtual;
	y2 = y2 < p->var.yres_virtual ? y2 : p->var.yres_virtual;
	rect->width = x2 - rect->dx;
	height = y2 - rect->dy;

	/* Size of the scanline in bytes */
	n = (rect->width * (p->var.bits_per_pixel >> 3));
	ppw = BITS_PER_LONG / p->var.bits_per_pixel;

	dst1 = p->screen_base + (rect->dy * linesize) +
	    	(rect->dx * (p->var.bits_per_pixel >> 3));
	start_index = ((unsigned long) dst1 & (bpl - 1));
	end_index = ((unsigned long) (dst1 + n) & (bpl - 1));

	if (p->fix.visual == FB_VISUAL_TRUECOLOR)
		fg = fgcolor = ((u32 *) (p->pseudo_palette))[rect->color];
	else
		fg = fgcolor = rect->color;

	for (i = 0; i < ppw - 1; i++) {
		fg <<= p->var.bits_per_pixel;
		fg |= fgcolor;
	}

	if (start_index) {
		start_mask = fg << (start_index << 3);
		n -= (bpl - start_index);
	}

	if (end_index) {
		end_mask = fg >> ((bpl - end_index) << 3);
		n -= end_index;
	}

	n = n / bpl;

	if (n <= 0) {
		if (start_mask) {
			if (end_mask)
				end_mask &= start_mask;
			else
				end_mask = start_mask;
			start_mask = 0;
		}
		n = 0;
	}

	if ((BITS_PER_LONG % p->var.bits_per_pixel) == 0) {
		switch (rect->rop) {
		case ROP_COPY:
			do {
				/* Word align to increases performace :-) */
				dst = (unsigned long *) (dst1 - start_index);

				if (start_mask) {
#if BITS_PER_LONG == 32
					fb_writel(fb_readl(dst) |
						  start_mask, dst);
#else
					fb_writeq(fb_readq(dst) |
						  start_mask, dst);
#endif
					dst++;
				}

				for (i = 0; i < n; i++) {
#if BITS_PER_LONG == 32
					fb_writel(fg, dst);
#else
					fb_writeq(fg, dst);
#endif
					dst++;
				}

				if (end_mask)
#if BITS_PER_LONG == 32
					fb_writel(fb_readl(dst) | end_mask,
						  dst);
#else
					fb_writeq(fb_readq(dst) | end_mask,
						  dst);
#endif
				dst1 += linesize;
			} while (--height);
			break;
		case ROP_XOR:
			do {
				dst = (unsigned long *) (dst1 - start_index);

				if (start_mask) {
#if BITS_PER_LONG == 32
					fb_writel(fb_readl(dst) ^
						  start_mask, dst);
#else
					fb_writeq(fb_readq(dst) ^
						  start_mask, dst);
#endif
					dst++;
				}

				for (i = 0; i < n; i++) {
#if BITS_PER_LONG == 32
					fb_writel(fb_readl(dst) ^ fg, dst);
#else
					fb_writeq(fb_readq(dst) ^ fg, dst);
#endif
					dst++;
				}

				if (end_mask) {
#if BITS_PER_LONG == 32
					fb_writel(fb_readl(dst) ^ end_mask,
						  dst);
#else
					fb_writeq(fb_readq(dst) ^ end_mask,
						  dst);
#endif
				}
				dst1 += linesize;
			} while (--height);
			break;
		}
	} else {
		/* Odd modes like 24 or 80 bits per pixel */
		start_mask = fg >> (start_index * p->var.bits_per_pixel);
		end_mask = fg << (end_index * p->var.bits_per_pixel);
		/* start_mask =& PFILL24(x1,fg);
		   end_mask_or = end_mask & PFILL24(x1+width-1,fg); */

		n = (rect->width - start_index - end_index) / ppw;

		switch (rect->rop) {
		case ROP_COPY:
			do {
				dst = (unsigned long *) dst1;
				if (start_mask)
					*dst |= start_mask;
				if ((start_index + rect->width) > ppw)
					dst++;

				/* XXX: slow */
				for (i = 0; i < n; i++) {
					*dst++ = fg;
				}
				if (end_mask)
					*dst |= end_mask;
				dst1 += linesize;
			} while (--height);
			break;
		case ROP_XOR:
			do {
				dst = (unsigned long *) dst1;
				if (start_mask)
					*dst ^= start_mask;
				if ((start_mask + rect->width) > ppw)
					dst++;

				for (i = 0; i < n; i++) {
					*dst++ ^= fg;	/* PFILL24(fg,x1+i); */
				}
				if (end_mask)
					*dst ^= end_mask;
				dst1 += linesize;
			} while (--height);
			break;
		}
	}
	return;
}