Rev 468 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
468 | giacomo | 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 | |||
33 | #include <linuxcomp.h> |
||
34 | |||
35 | #include <linux/config.h> |
||
36 | #include <linux/module.h> |
||
37 | #include <linux/string.h> |
||
38 | #include <linux/fb.h> |
||
39 | #include <asm/types.h> |
||
40 | |||
41 | #define DEBUG |
||
42 | |||
43 | #ifdef DEBUG |
||
44 | #define DPRINTK(fmt, args...) printk(KERN_DEBUG "%s: " fmt,__FUNCTION__,## args) |
||
45 | #else |
||
46 | #define DPRINTK(fmt, args...) |
||
47 | #endif |
||
48 | |||
49 | static u32 cfb_tab8[] = { |
||
50 | #if defined(__BIG_ENDIAN) |
||
51 | 0x00000000,0x000000ff,0x0000ff00,0x0000ffff, |
||
52 | 0x00ff0000,0x00ff00ff,0x00ffff00,0x00ffffff, |
||
53 | 0xff000000,0xff0000ff,0xff00ff00,0xff00ffff, |
||
54 | 0xffff0000,0xffff00ff,0xffffff00,0xffffffff |
||
55 | #elif defined(__LITTLE_ENDIAN) |
||
56 | 0x00000000,0xff000000,0x00ff0000,0xffff0000, |
||
57 | 0x0000ff00,0xff00ff00,0x00ffff00,0xffffff00, |
||
58 | 0x000000ff,0xff0000ff,0x00ff00ff,0xffff00ff, |
||
59 | 0x0000ffff,0xff00ffff,0x00ffffff,0xffffffff |
||
60 | #else |
||
61 | #error FIXME: No endianness?? |
||
62 | #endif |
||
63 | }; |
||
64 | |||
65 | static u32 cfb_tab16[] = { |
||
66 | #if defined(__BIG_ENDIAN) |
||
67 | 0x00000000, 0x0000ffff, 0xffff0000, 0xffffffff |
||
68 | #elif defined(__LITTLE_ENDIAN) |
||
69 | 0x00000000, 0xffff0000, 0x0000ffff, 0xffffffff |
||
70 | #else |
||
71 | #error FIXME: No endianness?? |
||
72 | #endif |
||
73 | }; |
||
74 | |||
75 | static u32 cfb_tab32[] = { |
||
76 | 0x00000000, 0xffffffff |
||
77 | }; |
||
78 | |||
79 | #define FB_WRITEL fb_writel |
||
80 | #define FB_READL fb_readl |
||
81 | |||
82 | #if defined (__BIG_ENDIAN) |
||
83 | #define LEFT_POS(bpp) (32 - bpp) |
||
84 | #define SHIFT_HIGH(val, bits) ((val) >> (bits)) |
||
85 | #define SHIFT_LOW(val, bits) ((val) << (bits)) |
||
86 | #else |
||
87 | #define LEFT_POS(bpp) (0) |
||
88 | #define SHIFT_HIGH(val, bits) ((val) << (bits)) |
||
89 | #define SHIFT_LOW(val, bits) ((val) >> (bits)) |
||
90 | #endif |
||
91 | |||
92 | static inline void color_imageblit(const struct fb_image *image, |
||
93 | struct fb_info *p, u8 *dst1, |
||
94 | u32 start_index, |
||
95 | u32 pitch_index) |
||
96 | { |
||
97 | /* Draw the penguin */ |
||
98 | u32 *dst, *dst2, 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 *) dst1; |
||
105 | for (i = image->height; i--; ) { |
||
106 | n = image->width; |
||
107 | dst = (u32 *) 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 = (char *) dst2; |
||
143 | |||
1056 | tullio | 144 | // changed for gcc4 compatibility |
145 | dst1 = (unsigned long)(dst1) & ~(sizeof(u32) - 1); |
||
146 | |||
468 | giacomo | 147 | start_index += pitch_index; |
148 | start_index &= 32 - 1; |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | |||
153 | static inline void slow_imageblit(const struct fb_image *image, struct fb_info *p, |
||
154 | u8 *dst1, u32 fgcolor, |
||
155 | u32 bgcolor, |
||
156 | u32 start_index, |
||
157 | u32 pitch_index) |
||
158 | { |
||
159 | u32 shift, color = 0, bpp = p->var.bits_per_pixel; |
||
160 | u32 *dst, *dst2, val, pitch = p->fix.line_length; |
||
161 | u32 null_bits = 32 - bpp; |
||
162 | u32 spitch = (image->width+7)/8; |
||
163 | const u8 *src = image->data, *s; |
||
164 | u32 i, j, l; |
||
165 | |||
166 | dst2 = (u32 *) dst1; |
||
167 | |||
168 | for (i = image->height; i--; ) { |
||
169 | shift = val = 0; |
||
170 | l = 8; |
||
171 | j = image->width; |
||
172 | dst = (u32 *) 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 << l)) ? fgcolor : bgcolor; |
||
185 | color <<= LEFT_POS(bpp); |
||
186 | val |= SHIFT_HIGH(color, shift); |
||
187 | |||
188 | /* Did the bitshift spill bits to the next long? */ |
||
189 | if (shift >= null_bits) { |
||
190 | FB_WRITEL(val, dst++); |
||
191 | val = (shift == null_bits) ? 0 : |
||
192 | SHIFT_LOW(color,32 - shift); |
||
193 | } |
||
194 | shift += bpp; |
||
195 | shift &= (32 - 1); |
||
196 | if (!l) { l = 8; s++; }; |
||
197 | } |
||
198 | |||
199 | /* write trailing bits */ |
||
200 | if (shift) { |
||
201 | u32 end_mask = SHIFT_HIGH(~(u32)0, shift); |
||
202 | |||
203 | FB_WRITEL((FB_READL(dst) & end_mask) | val, dst); |
||
204 | } |
||
205 | |||
206 | dst1 += pitch; |
||
207 | src += spitch; |
||
208 | if (pitch_index) { |
||
209 | dst2 += pitch; |
||
210 | dst1 = (char *) dst2; |
||
211 | |||
1056 | tullio | 212 | // changed for gcc4 compatibility |
213 | dst1 = (unsigned long)(dst1) & ~(sizeof(u32) - 1); |
||
214 | |||
468 | giacomo | 215 | start_index += pitch_index; |
216 | start_index &= 32 - 1; |
||
217 | } |
||
218 | |||
219 | } |
||
220 | } |
||
221 | |||
222 | /* |
||
223 | * fast_imageblit - optimized monochrome color expansion |
||
224 | * |
||
225 | * Only if: bits_per_pixel == 8, 16, or 32 |
||
226 | * image->width is divisible by pixel/dword (ppw); |
||
227 | * fix->line_legth is divisible by 4; |
||
228 | * beginning and end of a scanline is dword aligned |
||
229 | */ |
||
230 | static inline void fast_imageblit(const struct fb_image *image, struct fb_info *p, |
||
231 | u8 *dst1, u32 fgcolor, |
||
232 | u32 bgcolor) |
||
233 | { |
||
234 | u32 fgx = fgcolor, bgx = bgcolor, bpp = p->var.bits_per_pixel; |
||
235 | u32 ppw = 32/bpp, spitch = (image->width + 7)/8; |
||
236 | u32 bit_mask, end_mask, eorx, shift; |
||
237 | const char *s = image->data, *src; |
||
238 | u32 *dst; |
||
239 | u32 *tab = NULL; |
||
240 | int i, j, k; |
||
241 | |||
242 | switch (bpp) { |
||
243 | case 8: |
||
244 | tab = cfb_tab8; |
||
245 | break; |
||
246 | case 16: |
||
247 | tab = cfb_tab16; |
||
248 | break; |
||
249 | case 32: |
||
250 | tab = cfb_tab32; |
||
251 | break; |
||
252 | } |
||
253 | |||
254 | for (i = ppw-1; i--; ) { |
||
255 | fgx <<= bpp; |
||
256 | bgx <<= bpp; |
||
257 | fgx |= fgcolor; |
||
258 | bgx |= bgcolor; |
||
259 | } |
||
260 | |||
261 | bit_mask = (1 << ppw) - 1; |
||
262 | eorx = fgx ^ bgx; |
||
263 | k = image->width/ppw; |
||
264 | |||
265 | for (i = image->height; i--; ) { |
||
266 | dst = (u32 *) dst1, shift = 8; src = s; |
||
267 | |||
268 | for (j = k; j--; ) { |
||
269 | shift -= ppw; |
||
270 | end_mask = tab[(*src >> shift) & bit_mask]; |
||
271 | FB_WRITEL((end_mask & eorx)^bgx, dst++); |
||
272 | if (!shift) { shift = 8; src++; } |
||
273 | } |
||
274 | dst1 += p->fix.line_length; |
||
275 | s += spitch; |
||
276 | } |
||
277 | } |
||
278 | |||
279 | void cfb_imageblit(struct fb_info *p, const struct fb_image *image) |
||
280 | { |
||
281 | u32 fgcolor, bgcolor, start_index, bitstart, pitch_index = 0; |
||
282 | u32 bpl = sizeof(u32), bpp = p->var.bits_per_pixel; |
||
283 | u32 width = image->width, height = image->height; |
||
284 | u32 dx = image->dx, dy = image->dy; |
||
285 | int x2, y2, vxres, vyres; |
||
286 | u8 *dst1; |
||
287 | |||
288 | vxres = p->var.xres_virtual; |
||
289 | vyres = p->var.yres_virtual; |
||
290 | /* |
||
291 | * We could use hardware clipping but on many cards you get around |
||
292 | * hardware clipping by writing to framebuffer directly like we are |
||
293 | * doing here. |
||
294 | */ |
||
295 | if (image->dx > vxres || image->dy > vyres) |
||
296 | return; |
||
297 | |||
298 | x2 = image->dx + image->width; |
||
299 | y2 = image->dy + image->height; |
||
300 | dx = image->dx > 0 ? image->dx : 0; |
||
301 | dy = image->dy > 0 ? image->dy : 0; |
||
302 | x2 = x2 < vxres ? x2 : vxres; |
||
303 | y2 = y2 < vyres ? y2 : vyres; |
||
304 | width = x2 - dx; |
||
305 | height = y2 - dy; |
||
306 | |||
307 | bitstart = (dy * p->fix.line_length * 8) + (dx * bpp); |
||
308 | start_index = bitstart & (32 - 1); |
||
309 | pitch_index = (p->fix.line_length & (bpl - 1)) * 8; |
||
310 | |||
311 | bitstart /= 8; |
||
312 | bitstart &= ~(bpl - 1); |
||
313 | dst1 = p->screen_base + bitstart; |
||
314 | |||
315 | if (p->fbops->fb_sync) |
||
316 | p->fbops->fb_sync(p); |
||
317 | |||
318 | if (image->depth == 1) { |
||
319 | if (p->fix.visual == FB_VISUAL_TRUECOLOR || |
||
320 | p->fix.visual == FB_VISUAL_DIRECTCOLOR) { |
||
321 | fgcolor = ((u32*)(p->pseudo_palette))[image->fg_color]; |
||
322 | bgcolor = ((u32*)(p->pseudo_palette))[image->bg_color]; |
||
323 | } else { |
||
324 | fgcolor = image->fg_color; |
||
325 | bgcolor = image->bg_color; |
||
326 | } |
||
327 | |||
328 | if (32 % bpp == 0 && !start_index && !pitch_index && |
||
329 | ((width & (32/bpp-1)) == 0) && |
||
330 | bpp >= 8 && bpp <= 32) |
||
331 | fast_imageblit(image, p, dst1, fgcolor, bgcolor); |
||
332 | else |
||
333 | slow_imageblit(image, p, dst1, fgcolor, bgcolor, |
||
334 | start_index, pitch_index); |
||
335 | } else if (image->depth <= bpp) |
||
336 | color_imageblit(image, p, dst1, start_index, pitch_index); |
||
337 | } |
||
338 | |||
339 | EXPORT_SYMBOL(cfb_imageblit); |
||
340 | |||
341 | MODULE_AUTHOR("James Simmons <jsimmons@users.sf.net>"); |
||
342 | MODULE_DESCRIPTION("Generic software accelerated imaging drawing"); |
||
343 | MODULE_LICENSE("GPL"); |
||
344 |