[PATCH] intelfb: extend partial support of i915G to include i915GM
[safe/jmp/linux-2.6] / drivers / video / c2p.c
1 /*
2  *  Fast C2P (Chunky-to-Planar) Conversion
3  *
4  *  Copyright (C) 2003 Geert Uytterhoeven
5  *
6  *  NOTES:
7  *    - This code was inspired by Scout's C2P tutorial
8  *    - It assumes to run on a big endian system
9  *
10  *  This file is subject to the terms and conditions of the GNU General Public
11  *  License. See the file COPYING in the main directory of this archive
12  *  for more details.
13  */
14
15 #include <linux/string.h>
16 #include "c2p.h"
17
18
19     /*
20      *  Basic transpose step
21      */
22
23 #define _transp(d, i1, i2, shift, mask)                 \
24     do {                                                \
25         u32 t = (d[i1] ^ (d[i2] >> shift)) & mask;      \
26         d[i1] ^= t;                                     \
27         d[i2] ^= t << shift;                            \
28     } while (0)
29
30 static inline u32 get_mask(int n)
31 {
32     switch (n) {
33         case 1:
34             return 0x55555555;
35             break;
36
37         case 2:
38             return 0x33333333;
39             break;
40
41         case 4:
42             return 0x0f0f0f0f;
43             break;
44
45         case 8:
46             return 0x00ff00ff;
47             break;
48
49         case 16:
50             return 0x0000ffff;
51             break;
52     }
53     return 0;
54 }
55
56 #define transp_nx1(d, n)                                \
57     do {                                                \
58         u32 mask = get_mask(n);                         \
59         /* First block */                               \
60         _transp(d, 0, 1, n, mask);                      \
61         /* Second block */                              \
62         _transp(d, 2, 3, n, mask);                      \
63         /* Third block */                               \
64         _transp(d, 4, 5, n, mask);                      \
65         /* Fourth block */                              \
66         _transp(d, 6, 7, n, mask);                      \
67     } while (0)
68
69 #define transp_nx2(d, n)                                \
70     do {                                                \
71         u32 mask = get_mask(n);                         \
72         /* First block */                               \
73         _transp(d, 0, 2, n, mask);                      \
74         _transp(d, 1, 3, n, mask);                      \
75         /* Second block */                              \
76         _transp(d, 4, 6, n, mask);                      \
77         _transp(d, 5, 7, n, mask);                      \
78     } while (0)
79
80 #define transp_nx4(d, n)                                \
81     do {                                                \
82         u32 mask = get_mask(n);                         \
83         _transp(d, 0, 4, n, mask);                      \
84         _transp(d, 1, 5, n, mask);                      \
85         _transp(d, 2, 6, n, mask);                      \
86         _transp(d, 3, 7, n, mask);                      \
87     } while (0)
88
89 #define transp(d, n, m) transp_nx ## m(d, n)
90
91
92     /*
93      *  Perform a full C2P step on 32 8-bit pixels, stored in 8 32-bit words
94      *  containing
95      *    - 32 8-bit chunky pixels on input
96      *    - permuted planar data on output
97      */
98
99 static void c2p_8bpp(u32 d[8])
100 {
101     transp(d, 16, 4);
102     transp(d, 8, 2);
103     transp(d, 4, 1);
104     transp(d, 2, 4);
105     transp(d, 1, 2);
106 }
107
108
109     /*
110      *  Array containing the permution indices of the planar data after c2p
111      */
112
113 static const int perm_c2p_8bpp[8] = { 7, 5, 3, 1, 6, 4, 2, 0 };
114
115
116     /*
117      *  Compose two values, using a bitmask as decision value
118      *  This is equivalent to (a & mask) | (b & ~mask)
119      */
120
121 static inline unsigned long comp(unsigned long a, unsigned long b,
122                                  unsigned long mask)
123 {
124         return ((a ^ b) & mask) ^ b;
125 }
126
127
128     /*
129      *  Store a full block of planar data after c2p conversion
130      */
131
132 static inline void store_planar(char *dst, u32 dst_inc, u32 bpp, u32 d[8])
133 {
134     int i;
135
136     for (i = 0; i < bpp; i++, dst += dst_inc)
137         *(u32 *)dst = d[perm_c2p_8bpp[i]];
138 }
139
140
141     /*
142      *  Store a partial block of planar data after c2p conversion
143      */
144
145 static inline void store_planar_masked(char *dst, u32 dst_inc, u32 bpp,
146                                        u32 d[8], u32 mask)
147 {
148     int i;
149
150     for (i = 0; i < bpp; i++, dst += dst_inc)
151         *(u32 *)dst = comp(d[perm_c2p_8bpp[i]], *(u32 *)dst, mask);
152 }
153
154
155     /*
156      *  c2p - Copy 8-bit chunky image data to a planar frame buffer
157      *  @dst: Starting address of the planar frame buffer
158      *  @dx: Horizontal destination offset (in pixels)
159      *  @dy: Vertical destination offset (in pixels)
160      *  @width: Image width (in pixels)
161      *  @height: Image height (in pixels)
162      *  @dst_nextline: Frame buffer offset to the next line (in bytes)
163      *  @dst_nextplane: Frame buffer offset to the next plane (in bytes)
164      *  @src_nextline: Image offset to the next line (in bytes)
165      *  @bpp: Bits per pixel of the planar frame buffer (1-8)
166      */
167
168 void c2p(u8 *dst, const u8 *src, u32 dx, u32 dy, u32 width, u32 height,
169          u32 dst_nextline, u32 dst_nextplane, u32 src_nextline, u32 bpp)
170 {
171     int dst_idx;
172     u32 d[8], first, last, w;
173     const u8 *c;
174     u8 *p;
175
176     dst += dy*dst_nextline+(dx & ~31);
177     dst_idx = dx % 32;
178     first = ~0UL >> dst_idx;
179     last = ~(~0UL >> ((dst_idx+width) % 32));
180     while (height--) {
181         c = src;
182         p = dst;
183         w = width;
184         if (dst_idx+width <= 32) {
185             /* Single destination word */
186             first &= last;
187             memset(d, 0, sizeof(d));
188             memcpy((u8 *)d+dst_idx, c, width);
189             c += width;
190             c2p_8bpp(d);
191             store_planar_masked(p, dst_nextplane, bpp, d, first);
192             p += 4;
193         } else {
194             /* Multiple destination words */
195             w = width;
196             /* Leading bits */
197             if (dst_idx) {
198                 w = 32 - dst_idx;
199                 memset(d, 0, dst_idx);
200                 memcpy((u8 *)d+dst_idx, c, w);
201                 c += w;
202                 c2p_8bpp(d);
203                 store_planar_masked(p, dst_nextplane, bpp, d, first);
204                 p += 4;
205                 w = width-w;
206             }
207             /* Main chunk */
208             while (w >= 32) {
209                 memcpy(d, c, 32);
210                 c += 32;
211                 c2p_8bpp(d);
212                 store_planar(p, dst_nextplane, bpp, d);
213                 p += 4;
214                 w -= 32;
215             }
216             /* Trailing bits */
217             w %= 32;
218             if (w > 0) {
219                 memcpy(d, c, w);
220                 memset((u8 *)d+w, 0, 32-w);
221                 c2p_8bpp(d);
222                 store_planar_masked(p, dst_nextplane, bpp, d, last);
223             }
224         }
225         src += src_nextline;
226         dst += dst_nextline;
227     }
228 }
229