blob: 261004473c8e76b8e446317a7dfec8bbb9342517 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/module.h>
33#include <linux/string.h>
34#include <linux/fb.h>
35#include <asm/types.h>
36
37#define DEBUG
38
39#ifdef DEBUG
40#define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt,__FUNCTION__,## args)
41#else
42#define DPRINTK(fmt, args...)
43#endif
44
Helge Dellerd95159c2006-12-08 02:40:28 -080045static const u32 cfb_tab8[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#if defined(__BIG_ENDIAN)
47 0x00000000,0x000000ff,0x0000ff00,0x0000ffff,
48 0x00ff0000,0x00ff00ff,0x00ffff00,0x00ffffff,
49 0xff000000,0xff0000ff,0xff00ff00,0xff00ffff,
50 0xffff0000,0xffff00ff,0xffffff00,0xffffffff
51#elif defined(__LITTLE_ENDIAN)
52 0x00000000,0xff000000,0x00ff0000,0xffff0000,
53 0x0000ff00,0xff00ff00,0x00ffff00,0xffffff00,
54 0x000000ff,0xff0000ff,0x00ff00ff,0xffff00ff,
55 0x0000ffff,0xff00ffff,0x00ffffff,0xffffffff
56#else
57#error FIXME: No endianness??
58#endif
59};
60
Helge Dellerd95159c2006-12-08 02:40:28 -080061static const u32 cfb_tab16[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#if defined(__BIG_ENDIAN)
63 0x00000000, 0x0000ffff, 0xffff0000, 0xffffffff
64#elif defined(__LITTLE_ENDIAN)
65 0x00000000, 0xffff0000, 0x0000ffff, 0xffffffff
66#else
67#error FIXME: No endianness??
68#endif
69};
70
Helge Dellerd95159c2006-12-08 02:40:28 -080071static const u32 cfb_tab32[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 0x00000000, 0xffffffff
73};
74
75#define FB_WRITEL fb_writel
76#define FB_READL fb_readl
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static inline void color_imageblit(const struct fb_image *image,
79 struct fb_info *p, u8 __iomem *dst1,
80 u32 start_index,
81 u32 pitch_index)
82{
83 /* Draw the penguin */
84 u32 __iomem *dst, *dst2;
85 u32 color = 0, val, shift;
86 int i, n, bpp = p->var.bits_per_pixel;
87 u32 null_bits = 32 - bpp;
88 u32 *palette = (u32 *) p->pseudo_palette;
89 const u8 *src = image->data;
90
91 dst2 = (u32 __iomem *) dst1;
92 for (i = image->height; i--; ) {
93 n = image->width;
94 dst = (u32 __iomem *) dst1;
95 shift = 0;
96 val = 0;
97
98 if (start_index) {
Antonino A. Daplasbe0d9b62005-12-12 22:17:21 -080099 u32 start_mask = ~(FB_SHIFT_HIGH(~(u32)0, start_index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 val = FB_READL(dst) & start_mask;
101 shift = start_index;
102 }
103 while (n--) {
104 if (p->fix.visual == FB_VISUAL_TRUECOLOR ||
105 p->fix.visual == FB_VISUAL_DIRECTCOLOR )
106 color = palette[*src];
107 else
108 color = *src;
Antonino A. Daplasbe0d9b62005-12-12 22:17:21 -0800109 color <<= FB_LEFT_POS(bpp);
110 val |= FB_SHIFT_HIGH(color, shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (shift >= null_bits) {
112 FB_WRITEL(val, dst++);
113
114 val = (shift == null_bits) ? 0 :
Antonino A. Daplasbe0d9b62005-12-12 22:17:21 -0800115 FB_SHIFT_LOW(color, 32 - shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117 shift += bpp;
118 shift &= (32 - 1);
119 src++;
120 }
121 if (shift) {
Antonino A. Daplasbe0d9b62005-12-12 22:17:21 -0800122 u32 end_mask = FB_SHIFT_HIGH(~(u32)0, shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 FB_WRITEL((FB_READL(dst) & end_mask) | val, dst);
125 }
126 dst1 += p->fix.line_length;
127 if (pitch_index) {
128 dst2 += p->fix.line_length;
129 dst1 = (u8 __iomem *)((long __force)dst2 & ~(sizeof(u32) - 1));
130
131 start_index += pitch_index;
132 start_index &= 32 - 1;
133 }
134 }
135}
136
137static inline void slow_imageblit(const struct fb_image *image, struct fb_info *p,
138 u8 __iomem *dst1, u32 fgcolor,
139 u32 bgcolor,
140 u32 start_index,
141 u32 pitch_index)
142{
143 u32 shift, color = 0, bpp = p->var.bits_per_pixel;
144 u32 __iomem *dst, *dst2;
145 u32 val, pitch = p->fix.line_length;
146 u32 null_bits = 32 - bpp;
147 u32 spitch = (image->width+7)/8;
148 const u8 *src = image->data, *s;
149 u32 i, j, l;
150
151 dst2 = (u32 __iomem *) dst1;
Antonino A. Daplasbe0d9b62005-12-12 22:17:21 -0800152 fgcolor <<= FB_LEFT_POS(bpp);
153 bgcolor <<= FB_LEFT_POS(bpp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 for (i = image->height; i--; ) {
156 shift = val = 0;
157 l = 8;
158 j = image->width;
159 dst = (u32 __iomem *) dst1;
160 s = src;
161
162 /* write leading bits */
163 if (start_index) {
Antonino A. Daplasbe0d9b62005-12-12 22:17:21 -0800164 u32 start_mask = ~(FB_SHIFT_HIGH(~(u32)0,start_index));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 val = FB_READL(dst) & start_mask;
166 shift = start_index;
167 }
168
169 while (j--) {
170 l--;
Antonino A. Daplasa5360932006-03-31 02:31:54 -0800171 color = (*s & (1 << l)) ? fgcolor : bgcolor;
Antonino A. Daplasbe0d9b62005-12-12 22:17:21 -0800172 val |= FB_SHIFT_HIGH(color, shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 /* Did the bitshift spill bits to the next long? */
175 if (shift >= null_bits) {
176 FB_WRITEL(val, dst++);
177 val = (shift == null_bits) ? 0 :
Antonino A. Daplasbe0d9b62005-12-12 22:17:21 -0800178 FB_SHIFT_LOW(color,32 - shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
180 shift += bpp;
181 shift &= (32 - 1);
182 if (!l) { l = 8; s++; };
183 }
184
185 /* write trailing bits */
186 if (shift) {
Antonino A. Daplasbe0d9b62005-12-12 22:17:21 -0800187 u32 end_mask = FB_SHIFT_HIGH(~(u32)0, shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 FB_WRITEL((FB_READL(dst) & end_mask) | val, dst);
190 }
191
192 dst1 += pitch;
193 src += spitch;
194 if (pitch_index) {
195 dst2 += pitch;
196 dst1 = (u8 __iomem *)((long __force)dst2 & ~(sizeof(u32) - 1));
197 start_index += pitch_index;
198 start_index &= 32 - 1;
199 }
200
201 }
202}
203
204/*
205 * fast_imageblit - optimized monochrome color expansion
206 *
207 * Only if: bits_per_pixel == 8, 16, or 32
208 * image->width is divisible by pixel/dword (ppw);
209 * fix->line_legth is divisible by 4;
210 * beginning and end of a scanline is dword aligned
211 */
212static inline void fast_imageblit(const struct fb_image *image, struct fb_info *p,
213 u8 __iomem *dst1, u32 fgcolor,
214 u32 bgcolor)
215{
216 u32 fgx = fgcolor, bgx = bgcolor, bpp = p->var.bits_per_pixel;
217 u32 ppw = 32/bpp, spitch = (image->width + 7)/8;
218 u32 bit_mask, end_mask, eorx, shift;
219 const char *s = image->data, *src;
220 u32 __iomem *dst;
Helge Dellerd95159c2006-12-08 02:40:28 -0800221 const u32 *tab = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 int i, j, k;
223
224 switch (bpp) {
225 case 8:
226 tab = cfb_tab8;
227 break;
228 case 16:
229 tab = cfb_tab16;
230 break;
231 case 32:
Antonino A. Daplas8126a042006-06-26 00:26:43 -0700232 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 tab = cfb_tab32;
234 break;
235 }
236
237 for (i = ppw-1; i--; ) {
238 fgx <<= bpp;
239 bgx <<= bpp;
240 fgx |= fgcolor;
241 bgx |= bgcolor;
242 }
243
244 bit_mask = (1 << ppw) - 1;
245 eorx = fgx ^ bgx;
246 k = image->width/ppw;
247
248 for (i = image->height; i--; ) {
249 dst = (u32 __iomem *) dst1, shift = 8; src = s;
250
251 for (j = k; j--; ) {
252 shift -= ppw;
253 end_mask = tab[(*src >> shift) & bit_mask];
254 FB_WRITEL((end_mask & eorx)^bgx, dst++);
255 if (!shift) { shift = 8; src++; }
256 }
257 dst1 += p->fix.line_length;
258 s += spitch;
259 }
260}
261
262void cfb_imageblit(struct fb_info *p, const struct fb_image *image)
263{
264 u32 fgcolor, bgcolor, start_index, bitstart, pitch_index = 0;
265 u32 bpl = sizeof(u32), bpp = p->var.bits_per_pixel;
Antonino A. Daplasb4d8aea2005-11-07 01:00:39 -0800266 u32 width = image->width;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 u32 dx = image->dx, dy = image->dy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 u8 __iomem *dst1;
269
270 if (p->state != FBINFO_STATE_RUNNING)
271 return;
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 bitstart = (dy * p->fix.line_length * 8) + (dx * bpp);
274 start_index = bitstart & (32 - 1);
275 pitch_index = (p->fix.line_length & (bpl - 1)) * 8;
276
277 bitstart /= 8;
278 bitstart &= ~(bpl - 1);
279 dst1 = p->screen_base + bitstart;
280
281 if (p->fbops->fb_sync)
282 p->fbops->fb_sync(p);
283
284 if (image->depth == 1) {
285 if (p->fix.visual == FB_VISUAL_TRUECOLOR ||
286 p->fix.visual == FB_VISUAL_DIRECTCOLOR) {
287 fgcolor = ((u32*)(p->pseudo_palette))[image->fg_color];
288 bgcolor = ((u32*)(p->pseudo_palette))[image->bg_color];
289 } else {
290 fgcolor = image->fg_color;
291 bgcolor = image->bg_color;
292 }
293
294 if (32 % bpp == 0 && !start_index && !pitch_index &&
295 ((width & (32/bpp-1)) == 0) &&
296 bpp >= 8 && bpp <= 32)
297 fast_imageblit(image, p, dst1, fgcolor, bgcolor);
298 else
299 slow_imageblit(image, p, dst1, fgcolor, bgcolor,
300 start_index, pitch_index);
301 } else
302 color_imageblit(image, p, dst1, start_index, pitch_index);
303}
304
305EXPORT_SYMBOL(cfb_imageblit);
306
307MODULE_AUTHOR("James Simmons <jsimmons@users.sf.net>");
308MODULE_DESCRIPTION("Generic software accelerated imaging drawing");
309MODULE_LICENSE("GPL");
310