Merge with Linux 2.5.59.
[linux-2.6/linux-mips.git] / drivers / video / cfbimgblt.c
blob3365ceddf4e4744279ac52123072935aeda368e8
1 /*
2 * Generic BitBLT function for frame buffer with packed pixels of any depth.
4 * Copyright (C) June 1999 James Simmons
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file COPYING in the main directory of this archive for
8 * more details.
10 * NOTES:
12 * This function copys a image from system memory to video memory. The
13 * image can be a bitmap where each 0 represents the background color and
14 * each 1 represents the foreground color. Great for font handling. It can
15 * also be a color image. This is determined by image_depth. The color image
16 * must be laid out exactly in the same format as the framebuffer. Yes I know
17 * their are cards with hardware that coverts images of various depths to the
18 * framebuffer depth. But not every card has this. All images must be rounded
19 * up to the nearest byte. For example a bitmap 12 bits wide must be two
20 * bytes width.
22 * Tony:
23 * Incorporate mask tables similar to fbcon-cfb*.c in 2.4 API. This speeds
24 * up the code significantly.
26 * Code for depths not multiples of BITS_PER_LONG is still kludgy, which is
27 * still processed a bit at a time.
29 * Also need to add code to deal with cards endians that are different than
30 * the native cpu endians. I also need to deal with MSB position in the word.
32 #include <linux/config.h>
33 #include <linux/module.h>
34 #include <linux/string.h>
35 #include <linux/fb.h>
36 #include <asm/types.h>
38 #define DEBUG
40 #ifdef DEBUG
41 #define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt,__FUNCTION__,## args)
42 #else
43 #define DPRINTK(fmt, args...)
44 #endif
46 static u32 cfb_tab8[] = {
47 #if defined(__BIG_ENDIAN)
48 0x00000000,0x000000ff,0x0000ff00,0x0000ffff,
49 0x00ff0000,0x00ff00ff,0x00ffff00,0x00ffffff,
50 0xff000000,0xff0000ff,0xff00ff00,0xff00ffff,
51 0xffff0000,0xffff00ff,0xffffff00,0xffffffff
52 #elif defined(__LITTLE_ENDIAN)
53 0x00000000,0xff000000,0x00ff0000,0xffff0000,
54 0x0000ff00,0xff00ff00,0x00ffff00,0xffffff00,
55 0x000000ff,0xff0000ff,0x00ff00ff,0xffff00ff,
56 0x0000ffff,0xff00ffff,0x00ffffff,0xffffffff
57 #else
58 #error FIXME: No endianness??
59 #endif
62 static u32 cfb_tab16[] = {
63 #if defined(__BIG_ENDIAN)
64 0x00000000, 0x0000ffff, 0xffff0000, 0xffffffff
65 #elif defined(__LITTLE_ENDIAN)
66 0x00000000, 0xffff0000, 0x0000ffff, 0xffffffff
67 #else
68 #error FIXME: No endianness??
69 #endif
72 static u32 cfb_tab32[] = {
73 0x00000000, 0xffffffff
76 #if BITS_PER_LONG == 32
77 #define FB_WRITEL fb_writel
78 #define FB_READL fb_readl
79 #else
80 #define FB_WRITEL fb_writeq
81 #define FB_READL fb_readq
82 #endif
84 #if defined (__BIG_ENDIAN)
85 #define LEFT_POS(bpp) (BITS_PER_LONG - bpp)
86 #define NEXT_POS(pos, bpp) ((pos) -= (bpp))
87 #define SHIFT_HIGH(val, bits) ((val) >> (bits))
88 #define SHIFT_LOW(val, bits) ((val) << (bits))
89 #else
90 #define LEFT_POS(bpp) (0)
91 #define NEXT_POS(pos, bpp) ((pos) += (bpp))
92 #define SHIFT_HIGH(val, bits) ((val) << (bits))
93 #define SHIFT_LOW(val, bits) ((val) >> (bits))
94 #endif
96 static inline void color_imageblit(struct fb_image *image, struct fb_info *p,
97 u8 *dst1, unsigned long start_index,
98 unsigned long pitch_index)
100 /* Draw the penguin */
101 unsigned long *dst, *dst2, color = 0, val, shift;
102 int i, n, bpp = p->var.bits_per_pixel;
103 unsigned long null_bits = BITS_PER_LONG - bpp;
104 u32 *palette = (u32 *) p->pseudo_palette;
105 u8 *src = image->data;
107 dst2 = (unsigned long *) dst1;
108 for (i = image->height; i--; ) {
109 n = image->width;
110 dst = (unsigned long *) dst1;
111 shift = 0;
112 val = 0;
114 if (start_index) {
115 unsigned long start_mask = ~(SHIFT_HIGH(~0UL,
116 start_index));
117 val = FB_READL(dst) & start_mask;
118 shift = start_index;
120 while (n--) {
121 if (p->fix.visual == FB_VISUAL_TRUECOLOR ||
122 p->fix.visual == FB_VISUAL_DIRECTCOLOR )
123 color = palette[*src];
124 else
125 color = *src;
126 color <<= LEFT_POS(bpp);
127 val |= SHIFT_HIGH(color, shift);
128 if (shift >= null_bits) {
129 FB_WRITEL(val, dst++);
131 val = (shift == null_bits) ? 0 :
132 SHIFT_LOW(color,BITS_PER_LONG - shift);
134 shift += bpp;
135 shift &= (BITS_PER_LONG - 1);
136 src++;
138 if (shift) {
139 unsigned long end_mask = SHIFT_HIGH(~0UL, shift);
141 FB_WRITEL((FB_READL(dst) & end_mask) | val, dst);
143 dst1 += p->fix.line_length;
144 if (pitch_index) {
145 dst2 += p->fix.line_length;
146 dst1 = (char *) dst2;
147 (unsigned long) dst1 &= ~(sizeof(unsigned long) - 1);
149 start_index += pitch_index;
150 start_index &= BITS_PER_LONG - 1;
155 static inline void slow_imageblit(struct fb_image *image, struct fb_info *p,
156 u8 *dst1, unsigned long fgcolor,
157 unsigned long bgcolor,
158 unsigned long start_index,
159 unsigned long pitch_index)
161 unsigned long shift, color = 0, bpp = p->var.bits_per_pixel;
162 unsigned long *dst, *dst2, val, pitch = p->fix.line_length;
163 unsigned long null_bits = BITS_PER_LONG - bpp;
164 unsigned long spitch = (image->width+7)/8;
165 u8 *src = image->data, *s;
166 unsigned long i, j, l;
168 dst2 = (unsigned long *) dst1;
170 for (i = image->height; i--; ) {
171 shift = val = 0;
172 l = 8;
173 j = image->width;
174 dst = (unsigned long *) dst1;
175 s = src;
177 /* write leading bits */
178 if (start_index) {
179 unsigned long start_mask = ~(SHIFT_HIGH(~0UL,
180 start_index));
181 val = FB_READL(dst) & start_mask;
182 shift = start_index;
185 while (j--) {
186 l--;
187 color = (*s & (1 << l)) ? fgcolor : bgcolor;
188 color <<= LEFT_POS(bpp);
189 val |= SHIFT_HIGH(color, shift);
191 /* Did the bitshift spill bits to the next long? */
192 if (shift >= null_bits) {
193 FB_WRITEL(val, dst++);
194 val = (shift == null_bits) ? 0 :
195 SHIFT_LOW(color,BITS_PER_LONG - shift);
197 shift += bpp;
198 shift &= (BITS_PER_LONG - 1);
199 if (!l) { l = 8; s++; };
202 /* write trailing bits */
203 if (shift) {
204 unsigned long end_mask = SHIFT_HIGH(~0UL, shift);
206 FB_WRITEL((FB_READL(dst) & end_mask) | val, dst);
209 dst1 += pitch;
210 src += spitch;
211 if (pitch_index) {
212 dst2 += pitch;
213 dst1 = (char *) dst2;
214 (unsigned long) dst1 &= ~(sizeof(unsigned long) - 1);
216 start_index += pitch_index;
217 start_index &= BITS_PER_LONG - 1;
224 * fast_imageblit - optimized monochrome color expansion
226 * Only if: bits_per_pixel == 8, 16, or 32
227 * image->width is divisible by pixel/dword (ppw);
228 * fix->line_legth is divisible by 4;
229 * beginning and end of a scanline is dword aligned
231 static inline void fast_imageblit(struct fb_image *image, struct fb_info *p,
232 u8 *dst1, unsigned long fgcolor,
233 unsigned long bgcolor)
235 unsigned long fgx = fgcolor, bgx = bgcolor, bpp = p->var.bits_per_pixel;
236 unsigned long ppw = BITS_PER_LONG/bpp, spitch = (image->width + 7)/8;
237 unsigned long bit_mask, end_mask, eorx, shift;
238 char *s = image->data, *src;
239 unsigned long *dst;
240 u32 *tab = NULL;
241 int i, j, k;
243 switch (bpp) {
244 case 8:
245 tab = cfb_tab8;
246 break;
247 case 16:
248 tab = cfb_tab16;
249 break;
250 case 32:
251 tab = cfb_tab32;
252 break;
255 for (i = ppw-1; i--; ) {
256 fgx <<= bpp;
257 bgx <<= bpp;
258 fgx |= fgcolor;
259 bgx |= bgcolor;
262 bit_mask = (1 << ppw) - 1;
263 eorx = fgx ^ bgx;
264 k = image->width/ppw;
266 for (i = image->height; i--; ) {
267 dst = (unsigned long *) dst1, shift = 8; src = s;
269 for (j = k; j--; ) {
270 shift -= ppw;
271 end_mask = tab[(*src >> shift) & bit_mask];
272 FB_WRITEL((end_mask & eorx)^bgx, dst++);
273 if (!shift) { shift = 8; src++; }
275 dst1 += p->fix.line_length;
276 s += spitch;
280 void cfb_imageblit(struct fb_info *p, struct fb_image *image)
282 unsigned long fgcolor, bgcolor, start_index, bitstart, pitch_index = 0;
283 unsigned long bpl = sizeof(unsigned long), bpp = p->var.bits_per_pixel;
284 int x2, y2, vxres, vyres;
285 u8 *dst1;
287 vxres = p->var.xres_virtual;
288 vyres = p->var.yres_virtual;
290 * We could use hardware clipping but on many cards you get around
291 * hardware clipping by writing to framebuffer directly like we are
292 * doing here.
294 if (image->dx > vxres || image->dy > vyres)
295 return;
297 x2 = image->dx + image->width;
298 y2 = image->dy + image->height;
299 image->dx = image->dx > 0 ? image->dx : 0;
300 image->dy = image->dy > 0 ? image->dy : 0;
301 x2 = x2 < vxres ? x2 : vxres;
302 y2 = y2 < vyres ? y2 : vyres;
303 image->width = x2 - image->dx;
304 image->height = y2 - image->dy;
306 bitstart = (image->dy * p->fix.line_length * 8) + (image->dx * bpp);
307 start_index = bitstart & (BITS_PER_LONG - 1);
308 pitch_index = (p->fix.line_length & (bpl - 1)) * 8;
310 bitstart /= 8;
311 bitstart &= ~(bpl - 1);
312 dst1 = p->screen_base + bitstart;
314 if (p->fbops->fb_sync)
315 p->fbops->fb_sync(p);
317 if (image->depth == 1) {
318 if (p->fix.visual == FB_VISUAL_TRUECOLOR ||
319 p->fix.visual == FB_VISUAL_DIRECTCOLOR) {
320 fgcolor = ((u32*)(p->pseudo_palette))[image->fg_color];
321 bgcolor = ((u32*)(p->pseudo_palette))[image->bg_color];
322 } else {
323 fgcolor = image->fg_color;
324 bgcolor = image->bg_color;
327 if (BITS_PER_LONG % bpp == 0 && !start_index && !pitch_index &&
328 ((image->width & (BITS_PER_LONG/bpp-1)) == 0) &&
329 bpp >= 8 && bpp <= 32)
330 fast_imageblit(image, p, dst1, fgcolor, bgcolor);
331 else
332 slow_imageblit(image, p, dst1, fgcolor, bgcolor, start_index, pitch_index);
334 else if (image->depth == bpp)
335 color_imageblit(image, p, dst1, start_index, pitch_index);
338 EXPORT_SYMBOL(cfb_imageblit);
340 MODULE_AUTHOR("James Simmons <jsimmons@users.sf.net>");
341 MODULE_DESCRIPTION("Generic software accelerated imaging drawing");
342 MODULE_LICENSE("GPL");