xref: /linux/drivers/video/fbdev/core/fb_draw.h (revision bb9707077b4ee5f77bc9939b057ff8a0d410296f)
1 #ifndef _FB_DRAW_H
2 #define _FB_DRAW_H
3 
4 #include <asm/types.h>
5 #include <linux/fb.h>
6 #include <linux/bug.h>
7 
8     /*
9      *  Compose two values, using a bitmask as decision value
10      *  This is equivalent to (a & mask) | (b & ~mask)
11      */
12 
13 static inline unsigned long
14 comp(unsigned long a, unsigned long b, unsigned long mask)
15 {
16     return ((a ^ b) & mask) ^ b;
17 }
18 
19     /*
20      *  Create a pattern with the given pixel's color
21      */
22 
23 #if BITS_PER_LONG == 64
24 static inline unsigned long
25 pixel_to_pat( u32 bpp, u32 pixel)
26 {
27 	switch (bpp) {
28 	case 1:
29 		return 0xfffffffffffffffful*pixel;
30 	case 2:
31 		return 0x5555555555555555ul*pixel;
32 	case 4:
33 		return 0x1111111111111111ul*pixel;
34 	case 8:
35 		return 0x0101010101010101ul*pixel;
36 	case 12:
37 		return 0x1001001001001001ul*pixel;
38 	case 16:
39 		return 0x0001000100010001ul*pixel;
40 	case 24:
41 		return 0x0001000001000001ul*pixel;
42 	case 32:
43 		return 0x0000000100000001ul*pixel;
44 	default:
45 		WARN(1, "pixel_to_pat(): unsupported pixelformat %d\n", bpp);
46 		return 0;
47     }
48 }
49 #else
50 static inline unsigned long
51 pixel_to_pat( u32 bpp, u32 pixel)
52 {
53 	switch (bpp) {
54 	case 1:
55 		return 0xfffffffful*pixel;
56 	case 2:
57 		return 0x55555555ul*pixel;
58 	case 4:
59 		return 0x11111111ul*pixel;
60 	case 8:
61 		return 0x01010101ul*pixel;
62 	case 12:
63 		return 0x01001001ul*pixel;
64 	case 16:
65 		return 0x00010001ul*pixel;
66 	case 24:
67 		return 0x01000001ul*pixel;
68 	case 32:
69 		return 0x00000001ul*pixel;
70 	default:
71 		WARN(1, "pixel_to_pat(): unsupported pixelformat %d\n", bpp);
72 		return 0;
73     }
74 }
75 #endif
76 
77 #ifdef CONFIG_FB_CFB_REV_PIXELS_IN_BYTE
78 #if BITS_PER_LONG == 64
79 #define REV_PIXELS_MASK1 0x5555555555555555ul
80 #define REV_PIXELS_MASK2 0x3333333333333333ul
81 #define REV_PIXELS_MASK4 0x0f0f0f0f0f0f0f0ful
82 #else
83 #define REV_PIXELS_MASK1 0x55555555ul
84 #define REV_PIXELS_MASK2 0x33333333ul
85 #define REV_PIXELS_MASK4 0x0f0f0f0ful
86 #endif
87 
88 static inline unsigned long fb_rev_pixels_in_long(unsigned long val,
89 						  u32 bswapmask)
90 {
91 	if (bswapmask & 1)
92 		val = comp(val >> 1, val << 1, REV_PIXELS_MASK1);
93 	if (bswapmask & 2)
94 		val = comp(val >> 2, val << 2, REV_PIXELS_MASK2);
95 	if (bswapmask & 3)
96 		val = comp(val >> 4, val << 4, REV_PIXELS_MASK4);
97 	return val;
98 }
99 
100 static inline u32 fb_shifted_pixels_mask_u32(struct fb_info *p, u32 index,
101 					     u32 bswapmask)
102 {
103 	u32 mask;
104 
105 	if (!bswapmask) {
106 		mask = FB_SHIFT_HIGH(p, ~(u32)0, index);
107 	} else {
108 		mask = 0xff << FB_LEFT_POS(p, 8);
109 		mask = FB_SHIFT_LOW(p, mask, index & (bswapmask)) & mask;
110 		mask = FB_SHIFT_HIGH(p, mask, index & ~(bswapmask));
111 #if defined(__i386__) || defined(__x86_64__)
112 		/* Shift argument is limited to 0 - 31 on x86 based CPU's */
113 		if(index + bswapmask < 32)
114 #endif
115 			mask |= FB_SHIFT_HIGH(p, ~(u32)0,
116 					(index + bswapmask) & ~(bswapmask));
117 	}
118 	return mask;
119 }
120 
121 static inline unsigned long fb_shifted_pixels_mask_long(struct fb_info *p,
122 							u32 index,
123 							u32 bswapmask)
124 {
125 	unsigned long mask;
126 
127 	if (!bswapmask) {
128 		mask = FB_SHIFT_HIGH(p, ~0UL, index);
129 	} else {
130 		mask = 0xff << FB_LEFT_POS(p, 8);
131 		mask = FB_SHIFT_LOW(p, mask, index & (bswapmask)) & mask;
132 		mask = FB_SHIFT_HIGH(p, mask, index & ~(bswapmask));
133 #if defined(__i386__) || defined(__x86_64__)
134 		/* Shift argument is limited to 0 - 31 on x86 based CPU's */
135 		if(index + bswapmask < BITS_PER_LONG)
136 #endif
137 			mask |= FB_SHIFT_HIGH(p, ~0UL,
138 					(index + bswapmask) & ~(bswapmask));
139 	}
140 	return mask;
141 }
142 
143 
144 static inline u32 fb_compute_bswapmask(struct fb_info *info)
145 {
146 	u32 bswapmask = 0;
147 	unsigned bpp = info->var.bits_per_pixel;
148 
149 	if ((bpp < 8) && (info->var.nonstd & FB_NONSTD_REV_PIX_IN_B)) {
150 		/*
151 		 * Reversed order of pixel layout in bytes
152 		 * works only for 1, 2 and 4 bpp
153 		 */
154 		bswapmask = 7 - bpp + 1;
155 	}
156 	return bswapmask;
157 }
158 
159 #else /* CONFIG_FB_CFB_REV_PIXELS_IN_BYTE */
160 
161 static inline unsigned long fb_rev_pixels_in_long(unsigned long val,
162 						  u32 bswapmask)
163 {
164 	return val;
165 }
166 
167 #define fb_shifted_pixels_mask_u32(p, i, b) FB_SHIFT_HIGH((p), ~(u32)0, (i))
168 #define fb_shifted_pixels_mask_long(p, i, b) FB_SHIFT_HIGH((p), ~0UL, (i))
169 #define fb_compute_bswapmask(...) 0
170 
171 #endif  /* CONFIG_FB_CFB_REV_PIXELS_IN_BYTE */
172 
173 #define cpu_to_le_long _cpu_to_le_long(BITS_PER_LONG)
174 #define _cpu_to_le_long(x) __cpu_to_le_long(x)
175 #define __cpu_to_le_long(x) cpu_to_le##x
176 
177 #define le_long_to_cpu _le_long_to_cpu(BITS_PER_LONG)
178 #define _le_long_to_cpu(x) __le_long_to_cpu(x)
179 #define __le_long_to_cpu(x) le##x##_to_cpu
180 
181 static inline unsigned long rolx(unsigned long word, unsigned int shift, unsigned int x)
182 {
183 	return (word << shift) | (word >> (x - shift));
184 }
185 
186 #endif /* FB_DRAW_H */
187