diff options
Diffstat (limited to 'apps/plugins/lib/gray_drawgraymap.c')
-rw-r--r-- | apps/plugins/lib/gray_drawgraymap.c | 274 |
1 files changed, 274 insertions, 0 deletions
diff --git a/apps/plugins/lib/gray_drawgraymap.c b/apps/plugins/lib/gray_drawgraymap.c new file mode 100644 index 0000000000..5febeb27ad --- /dev/null +++ b/apps/plugins/lib/gray_drawgraymap.c | |||
@@ -0,0 +1,274 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Grayscale framework | ||
11 | * gray_drawgraymap() function | ||
12 | * | ||
13 | * This is a generic framework to use grayscale display within Rockbox | ||
14 | * plugins. It obviously does not work for the player. | ||
15 | * | ||
16 | * Copyright (C) 2004 Jens Arnold | ||
17 | * | ||
18 | * All files in this archive are subject to the GNU General Public License. | ||
19 | * See the file COPYING in the source tree root for full license agreement. | ||
20 | * | ||
21 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
22 | * KIND, either express or implied. | ||
23 | * | ||
24 | ****************************************************************************/ | ||
25 | |||
26 | #ifndef SIMULATOR /* not for simulator by now */ | ||
27 | #include "plugin.h" | ||
28 | |||
29 | #ifdef HAVE_LCD_BITMAP /* and also not for the Player */ | ||
30 | #include "gray.h" | ||
31 | |||
32 | /* Prototypes */ | ||
33 | static void _writearray(unsigned char *address, unsigned char *src, int stride, | ||
34 | unsigned mask); | ||
35 | |||
36 | /* Write an 8-pixel block, defined by their brightnesses in a graymap. | ||
37 | * Address is the byte in the first bitplane, src is the graymap start address, | ||
38 | * stride is the increment for the graymap to get to the next pixel, mask | ||
39 | * determines which pixels of the destination block are changed. For "0" bits, | ||
40 | * the src address is not incremented! */ | ||
41 | static void _writearray(unsigned char *address, unsigned char *src, int stride, | ||
42 | unsigned mask) | ||
43 | { | ||
44 | unsigned long pat_stack[8]; | ||
45 | register unsigned char *end_addr; | ||
46 | register unsigned long *pat_ptr = &pat_stack[8]; | ||
47 | |||
48 | /* precalculate the bit patterns with random shifts (same RNG as | ||
49 | * _writepixel, see there for an explanation) for all 8 pixels and put them | ||
50 | * on an extra "stack" */ | ||
51 | asm ( | ||
52 | "mov #8,r3 \n" /* loop count in r3: 8 pixels */ | ||
53 | "mov %7,r2 \n" /* copy mask */ | ||
54 | |||
55 | ".wa_loop: \n" /** load pattern for pixel **/ | ||
56 | "mov #0,r0 \n" /* pattern for skipped pixel must be 0 */ | ||
57 | "shlr r2 \n" /* shift out lsb of mask */ | ||
58 | "bf .wa_skip \n" /* skip this pixel */ | ||
59 | |||
60 | "mov.b @%2,r0 \n" /* load src byte */ | ||
61 | "extu.b r0,r0 \n" /* extend unsigned */ | ||
62 | "mulu %4,r0 \n" /* macl = byte * depth; */ | ||
63 | "add %3,%2 \n" /* src += stride; */ | ||
64 | "sts macl,r4 \n" /* r4 = macl; */ | ||
65 | "add r4,r0 \n" /* byte += r4; */ | ||
66 | "shlr8 r0 \n" /* byte >>= 8; */ | ||
67 | "shll2 r0 \n" | ||
68 | "mov.l @(r0,%5),r4 \n" /* r4 = bitpattern[byte]; */ | ||
69 | |||
70 | "mov #75,r0 \n" | ||
71 | "mulu r0,%0 \n" /* multiply by 75 */ | ||
72 | "sts macl,%0 \n" | ||
73 | "add #74,%0 \n" /* add another 74 */ | ||
74 | /* Since the lower bits are not very random: */ | ||
75 | "swap.b %0,r1 \n" /* get bits 8..15 (need max. 5) */ | ||
76 | "and %6,r1 \n" /* mask out unneeded bits */ | ||
77 | |||
78 | "cmp/hs %4,r1 \n" /* random >= depth ? */ | ||
79 | "bf .wa_ntrim \n" | ||
80 | "sub %4,r1 \n" /* yes: random -= depth; */ | ||
81 | ".wa_ntrim: \n" | ||
82 | |||
83 | "mov.l .ashlsi3,r0 \n" /** rotate pattern **/ | ||
84 | "jsr @r0 \n" /* r4 -> r0, shift left by r5 */ | ||
85 | "mov r1,r5 \n" | ||
86 | |||
87 | "mov %4,r5 \n" | ||
88 | "sub r1,r5 \n" /* r5 = depth - r1 */ | ||
89 | "mov.l .lshrsi3,r1 \n" | ||
90 | "jsr @r1 \n" /* r4 -> r0, shift right by r5 */ | ||
91 | "mov r0,r1 \n" /* store previous result in r1 */ | ||
92 | |||
93 | "or r1,r0 \n" /* rotated_pattern = r0 | r1 */ | ||
94 | |||
95 | ".wa_skip: \n" | ||
96 | "mov.l r0,@-%1 \n" /* push on pattern stack */ | ||
97 | |||
98 | "add #-1,r3 \n" /* decrease loop count */ | ||
99 | "cmp/pl r3 \n" /* loop count > 0? */ | ||
100 | "bt .wa_loop \n" /* yes: loop */ | ||
101 | : /* outputs */ | ||
102 | /* %0, in & out */ "+r"(_gray_random_buffer), | ||
103 | /* %1, in & out */ "+r"(pat_ptr) | ||
104 | : /* inputs */ | ||
105 | /* %2 */ "r"(src), | ||
106 | /* %3 */ "r"(stride), | ||
107 | /* %4 */ "r"(_graybuf->depth), | ||
108 | /* %5 */ "r"(_graybuf->bitpattern), | ||
109 | /* %6 */ "r"(_graybuf->randmask), | ||
110 | /* %7 */ "r"(mask) | ||
111 | : /* clobbers */ | ||
112 | "r0", "r1", "r2", "r3", "r4", "r5", "macl" | ||
113 | ); | ||
114 | |||
115 | end_addr = address + MULU16(_graybuf->depth, _graybuf->plane_size); | ||
116 | |||
117 | /* set the bits for all 8 pixels in all bytes according to the | ||
118 | * precalculated patterns on the pattern stack */ | ||
119 | asm volatile ( | ||
120 | "mov.l @%3+,r1 \n" /* pop all 8 patterns */ | ||
121 | "mov.l @%3+,r2 \n" | ||
122 | "mov.l @%3+,r3 \n" | ||
123 | "mov.l @%3+,r8 \n" | ||
124 | "mov.l @%3+,r9 \n" | ||
125 | "mov.l @%3+,r10 \n" | ||
126 | "mov.l @%3+,r11 \n" | ||
127 | "mov.l @%3+,r12 \n" | ||
128 | |||
129 | "not %4,%4 \n" /* "set" mask -> "keep" mask */ | ||
130 | "extu.b %4,%4 \n" /* mask out high bits */ | ||
131 | "tst %4,%4 \n" /* nothing to keep? */ | ||
132 | "bt .wa_sloop \n" /* yes: jump to short loop */ | ||
133 | |||
134 | ".wa_floop: \n" /** full loop (there are bits to keep)**/ | ||
135 | "shlr r1 \n" /* rotate lsb of pattern 1 to t bit */ | ||
136 | "rotcl r0 \n" /* rotate t bit into r0 */ | ||
137 | "shlr r2 \n" | ||
138 | "rotcl r0 \n" | ||
139 | "shlr r3 \n" | ||
140 | "rotcl r0 \n" | ||
141 | "shlr r8 \n" | ||
142 | "rotcl r0 \n" | ||
143 | "shlr r9 \n" | ||
144 | "rotcl r0 \n" | ||
145 | "shlr r10 \n" | ||
146 | "rotcl r0 \n" | ||
147 | "shlr r11 \n" | ||
148 | "rotcl r0 \n" | ||
149 | "shlr r12 \n" | ||
150 | "mov.b @%0,%3 \n" /* read old value */ | ||
151 | "rotcl r0 \n" | ||
152 | "and %4,%3 \n" /* mask out unneeded bits */ | ||
153 | "or r0,%3 \n" /* set new bits */ | ||
154 | "mov.b %3,@%0 \n" /* store value to bitplane */ | ||
155 | "add %2,%0 \n" /* advance to next bitplane */ | ||
156 | "cmp/hi %0,%1 \n" /* last bitplane done? */ | ||
157 | "bt .wa_floop \n" /* no: loop */ | ||
158 | |||
159 | "bra .wa_end \n" | ||
160 | "nop \n" | ||
161 | |||
162 | ".wa_sloop: \n" /** short loop (nothing to keep) **/ | ||
163 | "shlr r1 \n" /* rotate lsb of pattern 1 to t bit */ | ||
164 | "rotcl r0 \n" /* rotate t bit into r0 */ | ||
165 | "shlr r2 \n" | ||
166 | "rotcl r0 \n" | ||
167 | "shlr r3 \n" | ||
168 | "rotcl r0 \n" | ||
169 | "shlr r8 \n" | ||
170 | "rotcl r0 \n" | ||
171 | "shlr r9 \n" | ||
172 | "rotcl r0 \n" | ||
173 | "shlr r10 \n" | ||
174 | "rotcl r0 \n" | ||
175 | "shlr r11 \n" | ||
176 | "rotcl r0 \n" | ||
177 | "shlr r12 \n" | ||
178 | "rotcl r0 \n" | ||
179 | "mov.b r0,@%0 \n" /* store byte to bitplane */ | ||
180 | "add %2,%0 \n" /* advance to next bitplane */ | ||
181 | "cmp/hi %0,%1 \n" /* last bitplane done? */ | ||
182 | "bt .wa_sloop \n" /* no: loop */ | ||
183 | |||
184 | ".wa_end: \n" | ||
185 | : /* outputs */ | ||
186 | : /* inputs */ | ||
187 | /* %0 */ "r"(address), | ||
188 | /* %1 */ "r"(end_addr), | ||
189 | /* %2 */ "r"(_graybuf->plane_size), | ||
190 | /* %3 */ "r"(pat_ptr), | ||
191 | /* %4 */ "r"(mask) | ||
192 | : /* clobbers */ | ||
193 | "r0", "r1", "r2", "r3", "r8", "r9", "r10", "r11", "r12" | ||
194 | ); | ||
195 | } | ||
196 | |||
197 | /* References to C library routines used in _writearray */ | ||
198 | asm ( | ||
199 | ".align 2 \n" | ||
200 | ".ashlsi3: \n" /* C library routine: */ | ||
201 | ".long ___ashlsi3 \n" /* shift r4 left by r5, return in r0 */ | ||
202 | ".lshrsi3: \n" /* C library routine: */ | ||
203 | ".long ___lshrsi3 \n" /* shift r4 right by r5, return in r0 */ | ||
204 | /* both routines preserve r4, destroy r5 and take ~16 cycles */ | ||
205 | ); | ||
206 | |||
207 | /*--------------------------------------------------------------------------- | ||
208 | Copy a grayscale bitmap into the display | ||
209 | ---------------------------------------------------------------------------- | ||
210 | A grayscale bitmap contains one byte for every pixel that defines the | ||
211 | brightness of the pixel (0..255). Bytes are read in row-major order. | ||
212 | The <stride> parameter is useful if you want to show only a part of a | ||
213 | bitmap. It should always be set to the "row length" of the bitmap, so | ||
214 | for displaying the whole bitmap, nx == stride. | ||
215 | |||
216 | This is the only drawing function NOT using the drawinfo. | ||
217 | */ | ||
218 | void gray_drawgraymap(unsigned char *src, int x, int y, int nx, int ny, | ||
219 | int stride) | ||
220 | { | ||
221 | int shift; | ||
222 | unsigned mask_top, mask_bottom; | ||
223 | unsigned char *src_row, *dst, *dst_row; | ||
224 | |||
225 | if ((unsigned) x >= (unsigned) _graybuf->width | ||
226 | || (unsigned) y >= (unsigned) _graybuf->height) | ||
227 | return; | ||
228 | |||
229 | if ((unsigned) (y + ny) >= (unsigned) _graybuf->height) /* clip bottom */ | ||
230 | ny = _graybuf->height - y; | ||
231 | |||
232 | if ((unsigned) (x + nx) >= (unsigned) _graybuf->width) /* clip right */ | ||
233 | nx = _graybuf->width - x; | ||
234 | |||
235 | dst = _graybuf->data + x + MULU16(_graybuf->width, y >> 3); | ||
236 | shift = y & 7; | ||
237 | ny += shift; | ||
238 | |||
239 | mask_top = 0xFFu << (y & 7); | ||
240 | mask_bottom = ~(0xFEu << ((ny - 1) & 7)); | ||
241 | if (ny <= 8) | ||
242 | mask_bottom &= mask_top; | ||
243 | |||
244 | if (ny > 8) | ||
245 | { | ||
246 | src_row = src; | ||
247 | dst_row = dst; | ||
248 | |||
249 | for (x = 0; x < nx; x++) | ||
250 | _writearray(dst_row++, src_row++, stride, mask_top); | ||
251 | |||
252 | src += MULU16(stride, 8 - shift); | ||
253 | dst += _graybuf->width; | ||
254 | |||
255 | for (y = 8; y < ny - 8; y += 8) | ||
256 | { | ||
257 | src_row = src; | ||
258 | dst_row = dst; | ||
259 | |||
260 | for (x = 0; x < nx; x++) | ||
261 | _writearray(dst_row++, src_row++, stride, 0xFFu); | ||
262 | |||
263 | src += stride << 3; | ||
264 | dst += _graybuf->width; | ||
265 | } | ||
266 | } | ||
267 | |||
268 | for (x = 0; x < nx; x++) | ||
269 | _writearray(dst++, src++, stride, mask_bottom); | ||
270 | } | ||
271 | |||
272 | #endif // #ifdef HAVE_LCD_BITMAP | ||
273 | #endif // #ifndef SIMULATOR | ||
274 | |||