xref: /OK3568_Linux_fs/kernel/drivers/video/fbdev/c2p_planar.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  *  Fast C2P (Chunky-to-Planar) Conversion
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  *  Copyright (C) 2003-2008 Geert Uytterhoeven
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  *  This file is subject to the terms and conditions of the GNU General Public
7*4882a593Smuzhiyun  *  License. See the file COPYING in the main directory of this archive
8*4882a593Smuzhiyun  *  for more details.
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/module.h>
12*4882a593Smuzhiyun #include <linux/string.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <asm/unaligned.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include "c2p.h"
17*4882a593Smuzhiyun #include "c2p_core.h"
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun     /*
21*4882a593Smuzhiyun      *  Perform a full C2P step on 32 8-bit pixels, stored in 8 32-bit words
22*4882a593Smuzhiyun      *  containing
23*4882a593Smuzhiyun      *    - 32 8-bit chunky pixels on input
24*4882a593Smuzhiyun      *    - permutated planar data (1 plane per 32-bit word) on output
25*4882a593Smuzhiyun      */
26*4882a593Smuzhiyun 
c2p_32x8(u32 d[8])27*4882a593Smuzhiyun static void c2p_32x8(u32 d[8])
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun 	transp8(d, 16, 4);
30*4882a593Smuzhiyun 	transp8(d, 8, 2);
31*4882a593Smuzhiyun 	transp8(d, 4, 1);
32*4882a593Smuzhiyun 	transp8(d, 2, 4);
33*4882a593Smuzhiyun 	transp8(d, 1, 2);
34*4882a593Smuzhiyun }
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun     /*
38*4882a593Smuzhiyun      *  Array containing the permutation indices of the planar data after c2p
39*4882a593Smuzhiyun      */
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun static const int perm_c2p_32x8[8] = { 7, 5, 3, 1, 6, 4, 2, 0 };
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun     /*
45*4882a593Smuzhiyun      *  Store a full block of planar data after c2p conversion
46*4882a593Smuzhiyun      */
47*4882a593Smuzhiyun 
store_planar(void * dst,u32 dst_inc,u32 bpp,u32 d[8])48*4882a593Smuzhiyun static inline void store_planar(void *dst, u32 dst_inc, u32 bpp, u32 d[8])
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun 	int i;
51*4882a593Smuzhiyun 
52*4882a593Smuzhiyun 	for (i = 0; i < bpp; i++, dst += dst_inc)
53*4882a593Smuzhiyun 		put_unaligned_be32(d[perm_c2p_32x8[i]], dst);
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun     /*
58*4882a593Smuzhiyun      *  Store a partial block of planar data after c2p conversion
59*4882a593Smuzhiyun      */
60*4882a593Smuzhiyun 
store_planar_masked(void * dst,u32 dst_inc,u32 bpp,u32 d[8],u32 mask)61*4882a593Smuzhiyun static inline void store_planar_masked(void *dst, u32 dst_inc, u32 bpp,
62*4882a593Smuzhiyun 				       u32 d[8], u32 mask)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	int i;
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	for (i = 0; i < bpp; i++, dst += dst_inc)
67*4882a593Smuzhiyun 		put_unaligned_be32(comp(d[perm_c2p_32x8[i]],
68*4882a593Smuzhiyun 					get_unaligned_be32(dst), mask),
69*4882a593Smuzhiyun 				   dst);
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun     /*
74*4882a593Smuzhiyun      *  c2p_planar - Copy 8-bit chunky image data to a planar frame buffer
75*4882a593Smuzhiyun      *  @dst: Starting address of the planar frame buffer
76*4882a593Smuzhiyun      *  @dx: Horizontal destination offset (in pixels)
77*4882a593Smuzhiyun      *  @dy: Vertical destination offset (in pixels)
78*4882a593Smuzhiyun      *  @width: Image width (in pixels)
79*4882a593Smuzhiyun      *  @height: Image height (in pixels)
80*4882a593Smuzhiyun      *  @dst_nextline: Frame buffer offset to the next line (in bytes)
81*4882a593Smuzhiyun      *  @dst_nextplane: Frame buffer offset to the next plane (in bytes)
82*4882a593Smuzhiyun      *  @src_nextline: Image offset to the next line (in bytes)
83*4882a593Smuzhiyun      *  @bpp: Bits per pixel of the planar frame buffer (1-8)
84*4882a593Smuzhiyun      */
85*4882a593Smuzhiyun 
c2p_planar(void * dst,const void * src,u32 dx,u32 dy,u32 width,u32 height,u32 dst_nextline,u32 dst_nextplane,u32 src_nextline,u32 bpp)86*4882a593Smuzhiyun void c2p_planar(void *dst, const void *src, u32 dx, u32 dy, u32 width,
87*4882a593Smuzhiyun 		u32 height, u32 dst_nextline, u32 dst_nextplane,
88*4882a593Smuzhiyun 		u32 src_nextline, u32 bpp)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun 	union {
91*4882a593Smuzhiyun 		u8 pixels[32];
92*4882a593Smuzhiyun 		u32 words[8];
93*4882a593Smuzhiyun 	} d;
94*4882a593Smuzhiyun 	u32 dst_idx, first, last, w;
95*4882a593Smuzhiyun 	const u8 *c;
96*4882a593Smuzhiyun 	void *p;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	dst += dy*dst_nextline+(dx & ~31);
99*4882a593Smuzhiyun 	dst_idx = dx % 32;
100*4882a593Smuzhiyun 	first = 0xffffffffU >> dst_idx;
101*4882a593Smuzhiyun 	last = ~(0xffffffffU >> ((dst_idx+width) % 32));
102*4882a593Smuzhiyun 	while (height--) {
103*4882a593Smuzhiyun 		c = src;
104*4882a593Smuzhiyun 		p = dst;
105*4882a593Smuzhiyun 		w = width;
106*4882a593Smuzhiyun 		if (dst_idx+width <= 32) {
107*4882a593Smuzhiyun 			/* Single destination word */
108*4882a593Smuzhiyun 			first &= last;
109*4882a593Smuzhiyun 			memset(d.pixels, 0, sizeof(d));
110*4882a593Smuzhiyun 			memcpy(d.pixels+dst_idx, c, width);
111*4882a593Smuzhiyun 			c += width;
112*4882a593Smuzhiyun 			c2p_32x8(d.words);
113*4882a593Smuzhiyun 			store_planar_masked(p, dst_nextplane, bpp, d.words,
114*4882a593Smuzhiyun 					    first);
115*4882a593Smuzhiyun 			p += 4;
116*4882a593Smuzhiyun 		} else {
117*4882a593Smuzhiyun 			/* Multiple destination words */
118*4882a593Smuzhiyun 			w = width;
119*4882a593Smuzhiyun 			/* Leading bits */
120*4882a593Smuzhiyun 			if (dst_idx) {
121*4882a593Smuzhiyun 				w = 32 - dst_idx;
122*4882a593Smuzhiyun 				memset(d.pixels, 0, dst_idx);
123*4882a593Smuzhiyun 				memcpy(d.pixels+dst_idx, c, w);
124*4882a593Smuzhiyun 				c += w;
125*4882a593Smuzhiyun 				c2p_32x8(d.words);
126*4882a593Smuzhiyun 				store_planar_masked(p, dst_nextplane, bpp,
127*4882a593Smuzhiyun 						    d.words, first);
128*4882a593Smuzhiyun 				p += 4;
129*4882a593Smuzhiyun 				w = width-w;
130*4882a593Smuzhiyun 			}
131*4882a593Smuzhiyun 			/* Main chunk */
132*4882a593Smuzhiyun 			while (w >= 32) {
133*4882a593Smuzhiyun 				memcpy(d.pixels, c, 32);
134*4882a593Smuzhiyun 				c += 32;
135*4882a593Smuzhiyun 				c2p_32x8(d.words);
136*4882a593Smuzhiyun 				store_planar(p, dst_nextplane, bpp, d.words);
137*4882a593Smuzhiyun 				p += 4;
138*4882a593Smuzhiyun 				w -= 32;
139*4882a593Smuzhiyun 			}
140*4882a593Smuzhiyun 			/* Trailing bits */
141*4882a593Smuzhiyun 			w %= 32;
142*4882a593Smuzhiyun 			if (w > 0) {
143*4882a593Smuzhiyun 				memcpy(d.pixels, c, w);
144*4882a593Smuzhiyun 				memset(d.pixels+w, 0, 32-w);
145*4882a593Smuzhiyun 				c2p_32x8(d.words);
146*4882a593Smuzhiyun 				store_planar_masked(p, dst_nextplane, bpp,
147*4882a593Smuzhiyun 						    d.words, last);
148*4882a593Smuzhiyun 			}
149*4882a593Smuzhiyun 		}
150*4882a593Smuzhiyun 		src += src_nextline;
151*4882a593Smuzhiyun 		dst += dst_nextline;
152*4882a593Smuzhiyun 	}
153*4882a593Smuzhiyun }
154*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(c2p_planar);
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun MODULE_LICENSE("GPL");
157