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
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
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>
36 #include <asm/types.h>
41 #define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt,__FUNCTION__,## args)
43 #define DPRINTK(fmt, args...)
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
58 #error FIXME: No endianness??
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
68 #error FIXME: No endianness??
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
80 #define FB_WRITEL fb_writeq
81 #define FB_READL fb_readq
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))
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))
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
--; ) {
110 dst
= (unsigned long *) dst1
;
115 unsigned long start_mask
= ~(SHIFT_HIGH(~0UL,
117 val
= FB_READL(dst
) & start_mask
;
121 if (p
->fix
.visual
== FB_VISUAL_TRUECOLOR
||
122 p
->fix
.visual
== FB_VISUAL_DIRECTCOLOR
)
123 color
= palette
[*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
);
135 shift
&= (BITS_PER_LONG
- 1);
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
;
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
--; ) {
174 dst
= (unsigned long *) dst1
;
177 /* write leading bits */
179 unsigned long start_mask
= ~(SHIFT_HIGH(~0UL,
181 val
= FB_READL(dst
) & start_mask
;
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
);
198 shift
&= (BITS_PER_LONG
- 1);
199 if (!l
) { l
= 8; s
++; };
202 /* write trailing bits */
204 unsigned long end_mask
= SHIFT_HIGH(~0UL, shift
);
206 FB_WRITEL((FB_READL(dst
) & end_mask
) | val
, dst
);
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
;
255 for (i
= ppw
-1; i
--; ) {
262 bit_mask
= (1 << ppw
) - 1;
264 k
= image
->width
/ppw
;
266 for (i
= image
->height
; i
--; ) {
267 dst
= (unsigned long *) dst1
, shift
= 8; src
= s
;
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
;
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
;
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
294 if (image
->dx
> vxres
|| image
->dy
> vyres
)
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;
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
];
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
);
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");