]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/video/cfbimgblt.c
[PATCH] fbdev: Shift pixel value before entering loop in cfbimageblit
[linux-2.6-omap-h63xx.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 #define BIT_NR(b)              (7 - (b))
84 #else
85 #define LEFT_POS(bpp)          (0)
86 #define SHIFT_HIGH(val, bits)  ((val) << (bits))
87 #define SHIFT_LOW(val, bits)   ((val) >> (bits))
88 #define BIT_NR(b)              (b)
89 #endif
90
91 static inline void color_imageblit(const struct fb_image *image, 
92                                    struct fb_info *p, u8 __iomem *dst1, 
93                                    u32 start_index,
94                                    u32 pitch_index)
95 {
96         /* Draw the penguin */
97         u32 __iomem *dst, *dst2;
98         u32 color = 0, val, shift;
99         int i, n, bpp = p->var.bits_per_pixel;
100         u32 null_bits = 32 - bpp;
101         u32 *palette = (u32 *) p->pseudo_palette;
102         const u8 *src = image->data;
103
104         dst2 = (u32 __iomem *) dst1;
105         for (i = image->height; i--; ) {
106                 n = image->width;
107                 dst = (u32 __iomem *) dst1;
108                 shift = 0;
109                 val = 0;
110                 
111                 if (start_index) {
112                         u32 start_mask = ~(SHIFT_HIGH(~(u32)0, start_index));
113                         val = FB_READL(dst) & start_mask;
114                         shift = start_index;
115                 }
116                 while (n--) {
117                         if (p->fix.visual == FB_VISUAL_TRUECOLOR ||
118                             p->fix.visual == FB_VISUAL_DIRECTCOLOR )
119                                 color = palette[*src];
120                         else
121                                 color = *src;
122                         color <<= LEFT_POS(bpp);
123                         val |= SHIFT_HIGH(color, shift);
124                         if (shift >= null_bits) {
125                                 FB_WRITEL(val, dst++);
126         
127                                 val = (shift == null_bits) ? 0 : 
128                                         SHIFT_LOW(color, 32 - shift);
129                         }
130                         shift += bpp;
131                         shift &= (32 - 1);
132                         src++;
133                 }
134                 if (shift) {
135                         u32 end_mask = SHIFT_HIGH(~(u32)0, shift);
136
137                         FB_WRITEL((FB_READL(dst) & end_mask) | val, dst);
138                 }
139                 dst1 += p->fix.line_length;
140                 if (pitch_index) {
141                         dst2 += p->fix.line_length;
142                         dst1 = (u8 __iomem *)((long __force)dst2 & ~(sizeof(u32) - 1));
143
144                         start_index += pitch_index;
145                         start_index &= 32 - 1;
146                 }
147         }
148 }
149
150 static inline void slow_imageblit(const struct fb_image *image, struct fb_info *p, 
151                                   u8 __iomem *dst1, u32 fgcolor,
152                                   u32 bgcolor, 
153                                   u32 start_index,
154                                   u32 pitch_index)
155 {
156         u32 shift, color = 0, bpp = p->var.bits_per_pixel;
157         u32 __iomem *dst, *dst2;
158         u32 val, pitch = p->fix.line_length;
159         u32 null_bits = 32 - bpp;
160         u32 spitch = (image->width+7)/8;
161         const u8 *src = image->data, *s;
162         u32 i, j, l;
163         
164         dst2 = (u32 __iomem *) dst1;
165         fgcolor <<= LEFT_POS(bpp);
166         bgcolor <<= LEFT_POS(bpp);
167
168         for (i = image->height; i--; ) {
169                 shift = val = 0;
170                 l = 8;
171                 j = image->width;
172                 dst = (u32 __iomem *) dst1;
173                 s = src;
174
175                 /* write leading bits */
176                 if (start_index) {
177                         u32 start_mask = ~(SHIFT_HIGH(~(u32)0, start_index));
178                         val = FB_READL(dst) & start_mask;
179                         shift = start_index;
180                 }
181
182                 while (j--) {
183                         l--;
184                         color = (*s & 1 << (BIT_NR(l))) ? fgcolor : bgcolor;
185                         val |= SHIFT_HIGH(color, shift);
186                         
187                         /* Did the bitshift spill bits to the next long? */
188                         if (shift >= null_bits) {
189                                 FB_WRITEL(val, dst++);
190                                 val = (shift == null_bits) ? 0 :
191                                          SHIFT_LOW(color,32 - shift);
192                         }
193                         shift += bpp;
194                         shift &= (32 - 1);
195                         if (!l) { l = 8; s++; };
196                 }
197
198                 /* write trailing bits */
199                 if (shift) {
200                         u32 end_mask = SHIFT_HIGH(~(u32)0, shift);
201
202                         FB_WRITEL((FB_READL(dst) & end_mask) | val, dst);
203                 }
204                 
205                 dst1 += pitch;
206                 src += spitch;  
207                 if (pitch_index) {
208                         dst2 += pitch;
209                         dst1 = (u8 __iomem *)((long __force)dst2 & ~(sizeof(u32) - 1));
210                         start_index += pitch_index;
211                         start_index &= 32 - 1;
212                 }
213                 
214         }
215 }
216
217 /*
218  * fast_imageblit - optimized monochrome color expansion
219  *
220  * Only if:  bits_per_pixel == 8, 16, or 32
221  *           image->width is divisible by pixel/dword (ppw);
222  *           fix->line_legth is divisible by 4;
223  *           beginning and end of a scanline is dword aligned
224  */
225 static inline void fast_imageblit(const struct fb_image *image, struct fb_info *p, 
226                                   u8 __iomem *dst1, u32 fgcolor, 
227                                   u32 bgcolor) 
228 {
229         u32 fgx = fgcolor, bgx = bgcolor, bpp = p->var.bits_per_pixel;
230         u32 ppw = 32/bpp, spitch = (image->width + 7)/8;
231         u32 bit_mask, end_mask, eorx, shift;
232         const char *s = image->data, *src;
233         u32 __iomem *dst;
234         u32 *tab = NULL;
235         int i, j, k;
236                 
237         switch (bpp) {
238         case 8:
239                 tab = cfb_tab8;
240                 break;
241         case 16:
242                 tab = cfb_tab16;
243                 break;
244         case 32:
245                 tab = cfb_tab32;
246                 break;
247         }
248
249         for (i = ppw-1; i--; ) {
250                 fgx <<= bpp;
251                 bgx <<= bpp;
252                 fgx |= fgcolor;
253                 bgx |= bgcolor;
254         }
255         
256         bit_mask = (1 << ppw) - 1;
257         eorx = fgx ^ bgx;
258         k = image->width/ppw;
259
260         for (i = image->height; i--; ) {
261                 dst = (u32 __iomem *) dst1, shift = 8; src = s;
262                 
263                 for (j = k; j--; ) {
264                         shift -= ppw;
265                         end_mask = tab[(*src >> shift) & bit_mask];
266                         FB_WRITEL((end_mask & eorx)^bgx, dst++);
267                         if (!shift) { shift = 8; src++; }               
268                 }
269                 dst1 += p->fix.line_length;
270                 s += spitch;
271         }
272 }       
273         
274 void cfb_imageblit(struct fb_info *p, const struct fb_image *image)
275 {
276         u32 fgcolor, bgcolor, start_index, bitstart, pitch_index = 0;
277         u32 bpl = sizeof(u32), bpp = p->var.bits_per_pixel;
278         u32 width = image->width;
279         u32 dx = image->dx, dy = image->dy;
280         u8 __iomem *dst1;
281
282         if (p->state != FBINFO_STATE_RUNNING)
283                 return;
284
285         bitstart = (dy * p->fix.line_length * 8) + (dx * bpp);
286         start_index = bitstart & (32 - 1);
287         pitch_index = (p->fix.line_length & (bpl - 1)) * 8;
288
289         bitstart /= 8;
290         bitstart &= ~(bpl - 1);
291         dst1 = p->screen_base + bitstart;
292
293         if (p->fbops->fb_sync)
294                 p->fbops->fb_sync(p);
295
296         if (image->depth == 1) {
297                 if (p->fix.visual == FB_VISUAL_TRUECOLOR ||
298                     p->fix.visual == FB_VISUAL_DIRECTCOLOR) {
299                         fgcolor = ((u32*)(p->pseudo_palette))[image->fg_color];
300                         bgcolor = ((u32*)(p->pseudo_palette))[image->bg_color];
301                 } else {
302                         fgcolor = image->fg_color;
303                         bgcolor = image->bg_color;
304                 }       
305                 
306                 if (32 % bpp == 0 && !start_index && !pitch_index && 
307                     ((width & (32/bpp-1)) == 0) &&
308                     bpp >= 8 && bpp <= 32)                      
309                         fast_imageblit(image, p, dst1, fgcolor, bgcolor);
310                 else 
311                         slow_imageblit(image, p, dst1, fgcolor, bgcolor,
312                                         start_index, pitch_index);
313         } else
314                 color_imageblit(image, p, dst1, start_index, pitch_index);
315 }
316
317 EXPORT_SYMBOL(cfb_imageblit);
318
319 MODULE_AUTHOR("James Simmons <jsimmons@users.sf.net>");
320 MODULE_DESCRIPTION("Generic software accelerated imaging drawing");
321 MODULE_LICENSE("GPL");
322