ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / video / cfbimgblt.c
1 /*
2  *  Generic BitBLT function for frame buffer with packed pixels of any depth.
3  *
4  *      Copyright (C)  June 1999 James Simmons
5  *
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.
9  *
10  * NOTES:
11  *
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. 
21  *
22  *  Tony: 
23  *  Incorporate mask tables similar to fbcon-cfb*.c in 2.4 API.  This speeds 
24  *  up the code significantly.
25  *  
26  *  Code for depths not multiples of BITS_PER_LONG is still kludgy, which is
27  *  still processed a bit at a time.   
28  *
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.
31  */
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>
37
38 #define DEBUG
39
40 #ifdef DEBUG
41 #define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt,__FUNCTION__,## args)
42 #else
43 #define DPRINTK(fmt, args...)
44 #endif
45
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
60 };
61
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
70 };
71
72 static u32 cfb_tab32[] = {
73         0x00000000, 0xffffffff
74 };
75
76 #define FB_WRITEL fb_writel
77 #define FB_READL  fb_readl
78
79 #if defined (__BIG_ENDIAN)
80 #define LEFT_POS(bpp)          (32 - bpp)
81 #define SHIFT_HIGH(val, bits)  ((val) >> (bits))
82 #define SHIFT_LOW(val, bits)   ((val) << (bits))
83 #else
84 #define LEFT_POS(bpp)          (0)
85 #define SHIFT_HIGH(val, bits)  ((val) << (bits))
86 #define SHIFT_LOW(val, bits)   ((val) >> (bits))
87 #endif
88
89 static inline void color_imageblit(const struct fb_image *image, 
90                                    struct fb_info *p, u8 *dst1, 
91                                    u32 start_index,
92                                    u32 pitch_index)
93 {
94         /* Draw the penguin */
95         u32 *dst, *dst2, color = 0, val, shift;
96         int i, n, bpp = p->var.bits_per_pixel;
97         u32 null_bits = 32 - bpp;
98         u32 *palette = (u32 *) p->pseudo_palette;
99         const u8 *src = image->data;
100
101         dst2 = (u32 *) dst1;
102         for (i = image->height; i--; ) {
103                 n = image->width;
104                 dst = (u32 *) dst1;
105                 shift = 0;
106                 val = 0;
107                 
108                 if (start_index) {
109                         u32 start_mask = ~(SHIFT_HIGH(~(u32)0, start_index));
110                         val = FB_READL(dst) & start_mask;
111                         shift = start_index;
112                 }
113                 while (n--) {
114                         if (p->fix.visual == FB_VISUAL_TRUECOLOR ||
115                             p->fix.visual == FB_VISUAL_DIRECTCOLOR )
116                                 color = palette[*src];
117                         else
118                                 color = *src;
119                         color <<= LEFT_POS(bpp);
120                         val |= SHIFT_HIGH(color, shift);
121                         if (shift >= null_bits) {
122                                 FB_WRITEL(val, dst++);
123         
124                                 val = (shift == null_bits) ? 0 : 
125                                         SHIFT_LOW(color, 32 - shift);
126                         }
127                         shift += bpp;
128                         shift &= (32 - 1);
129                         src++;
130                 }
131                 if (shift) {
132                         u32 end_mask = SHIFT_HIGH(~(u32)0, shift);
133
134                         FB_WRITEL((FB_READL(dst) & end_mask) | val, dst);
135                 }
136                 dst1 += p->fix.line_length;
137                 if (pitch_index) {
138                         dst2 += p->fix.line_length;
139                         dst1 = (u8 *)((long)dst2 & ~(sizeof(u32) - 1));
140
141                         start_index += pitch_index;
142                         start_index &= 32 - 1;
143                 }
144         }
145 }
146
147 static inline void slow_imageblit(const struct fb_image *image, struct fb_info *p, 
148                                   u8 *dst1, u32 fgcolor,
149                                   u32 bgcolor, 
150                                   u32 start_index,
151                                   u32 pitch_index)
152 {
153         u32 shift, color = 0, bpp = p->var.bits_per_pixel;
154         u32 *dst, *dst2, val, pitch = p->fix.line_length;
155         u32 null_bits = 32 - bpp;
156         u32 spitch = (image->width+7)/8;
157         const u8 *src = image->data, *s;
158         u32 i, j, l;
159         
160         dst2 = (u32 *) dst1;
161
162         for (i = image->height; i--; ) {
163                 shift = val = 0;
164                 l = 8;
165                 j = image->width;
166                 dst = (u32 *) dst1;
167                 s = src;
168
169                 /* write leading bits */
170                 if (start_index) {
171                         u32 start_mask = ~(SHIFT_HIGH(~(u32)0, start_index));
172                         val = FB_READL(dst) & start_mask;
173                         shift = start_index;
174                 }
175
176                 while (j--) {
177                         l--;
178                         color = (*s & (1 << l)) ? fgcolor : bgcolor;
179                         color <<= LEFT_POS(bpp);
180                         val |= SHIFT_HIGH(color, shift);
181                         
182                         /* Did the bitshift spill bits to the next long? */
183                         if (shift >= null_bits) {
184                                 FB_WRITEL(val, dst++);
185                                 val = (shift == null_bits) ? 0 :
186                                          SHIFT_LOW(color,32 - shift);
187                         }
188                         shift += bpp;
189                         shift &= (32 - 1);
190                         if (!l) { l = 8; s++; };
191                 }
192
193                 /* write trailing bits */
194                 if (shift) {
195                         u32 end_mask = SHIFT_HIGH(~(u32)0, shift);
196
197                         FB_WRITEL((FB_READL(dst) & end_mask) | val, dst);
198                 }
199                 
200                 dst1 += pitch;
201                 src += spitch;  
202                 if (pitch_index) {
203                         dst2 += pitch;
204                         dst1 = (u8 *)((long)dst2 & ~(sizeof(u32) - 1));
205                         start_index += pitch_index;
206                         start_index &= 32 - 1;
207                 }
208                 
209         }
210 }
211
212 /*
213  * fast_imageblit - optimized monochrome color expansion
214  *
215  * Only if:  bits_per_pixel == 8, 16, or 32
216  *           image->width is divisible by pixel/dword (ppw);
217  *           fix->line_legth is divisible by 4;
218  *           beginning and end of a scanline is dword aligned
219  */
220 static inline void fast_imageblit(const struct fb_image *image, struct fb_info *p, 
221                                   u8 *dst1, u32 fgcolor, 
222                                   u32 bgcolor) 
223 {
224         u32 fgx = fgcolor, bgx = bgcolor, bpp = p->var.bits_per_pixel;
225         u32 ppw = 32/bpp, spitch = (image->width + 7)/8;
226         u32 bit_mask, end_mask, eorx, shift;
227         const char *s = image->data, *src;
228         u32 *dst;
229         u32 *tab = NULL;
230         int i, j, k;
231                 
232         switch (bpp) {
233         case 8:
234                 tab = cfb_tab8;
235                 break;
236         case 16:
237                 tab = cfb_tab16;
238                 break;
239         case 32:
240                 tab = cfb_tab32;
241                 break;
242         }
243
244         for (i = ppw-1; i--; ) {
245                 fgx <<= bpp;
246                 bgx <<= bpp;
247                 fgx |= fgcolor;
248                 bgx |= bgcolor;
249         }
250         
251         bit_mask = (1 << ppw) - 1;
252         eorx = fgx ^ bgx;
253         k = image->width/ppw;
254
255         for (i = image->height; i--; ) {
256                 dst = (u32 *) dst1, shift = 8; src = s;
257                 
258                 for (j = k; j--; ) {
259                         shift -= ppw;
260                         end_mask = tab[(*src >> shift) & bit_mask];
261                         FB_WRITEL((end_mask & eorx)^bgx, dst++);
262                         if (!shift) { shift = 8; src++; }               
263                 }
264                 dst1 += p->fix.line_length;
265                 s += spitch;
266         }
267 }       
268         
269 void cfb_imageblit(struct fb_info *p, const struct fb_image *image)
270 {
271         u32 fgcolor, bgcolor, start_index, bitstart, pitch_index = 0;
272         u32 bpl = sizeof(u32), bpp = p->var.bits_per_pixel;
273         u32 width = image->width, height = image->height; 
274         u32 dx = image->dx, dy = image->dy;
275         int x2, y2, vxres, vyres;
276         u8 *dst1;
277
278         if (p->state != FBINFO_STATE_RUNNING)
279                 return;
280
281         vxres = p->var.xres_virtual;
282         vyres = p->var.yres_virtual;
283         /* 
284          * We could use hardware clipping but on many cards you get around
285          * hardware clipping by writing to framebuffer directly like we are
286          * doing here. 
287          */
288         if (image->dx > vxres || image->dy > vyres)
289                 return;
290
291         x2 = image->dx + image->width;
292         y2 = image->dy + image->height;
293         dx = image->dx > 0 ? image->dx : 0;
294         dy = image->dy > 0 ? image->dy : 0;
295         x2 = x2 < vxres ? x2 : vxres;
296         y2 = y2 < vyres ? y2 : vyres;
297         width  = x2 - dx;
298         height = y2 - dy;
299
300         bitstart = (dy * p->fix.line_length * 8) + (dx * bpp);
301         start_index = bitstart & (32 - 1);
302         pitch_index = (p->fix.line_length & (bpl - 1)) * 8;
303
304         bitstart /= 8;
305         bitstart &= ~(bpl - 1);
306         dst1 = p->screen_base + bitstart;
307
308         if (p->fbops->fb_sync)
309                 p->fbops->fb_sync(p);
310
311         if (image->depth == 1) {
312                 if (p->fix.visual == FB_VISUAL_TRUECOLOR ||
313                     p->fix.visual == FB_VISUAL_DIRECTCOLOR) {
314                         fgcolor = ((u32*)(p->pseudo_palette))[image->fg_color];
315                         bgcolor = ((u32*)(p->pseudo_palette))[image->bg_color];
316                 } else {
317                         fgcolor = image->fg_color;
318                         bgcolor = image->bg_color;
319                 }       
320                 
321                 if (32 % bpp == 0 && !start_index && !pitch_index && 
322                     ((width & (32/bpp-1)) == 0) &&
323                     bpp >= 8 && bpp <= 32)                      
324                         fast_imageblit(image, p, dst1, fgcolor, bgcolor);
325                 else 
326                         slow_imageblit(image, p, dst1, fgcolor, bgcolor,
327                                         start_index, pitch_index);
328         } else if (image->depth <= bpp) 
329                 color_imageblit(image, p, dst1, start_index, pitch_index);
330 }
331
332 EXPORT_SYMBOL(cfb_imageblit);
333
334 MODULE_AUTHOR("James Simmons <jsimmons@users.sf.net>");
335 MODULE_DESCRIPTION("Generic software accelerated imaging drawing");
336 MODULE_LICENSE("GPL");
337