Mercurial > sdl-ios-xcode
annotate src/video/SDL_stretch.c @ 1411:efb34e597d21
Fixed assembly alignment error on Intel Macs.
Do we really need 16-bit alignment here?
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 21 Feb 2006 19:28:45 +0000 |
parents | d910939febfa |
children | e3242177fe4a |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1234
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1234
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1234
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 9 |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1234
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1234
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1234
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1234
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
22 #include "SDL_config.h" |
0 | 23 |
24 /* This a stretch blit implementation based on ideas given to me by | |
25 Tomasz Cejner - thanks! :) | |
26 | |
27 April 27, 2000 - Sam Lantinga | |
28 */ | |
29 | |
30 #include "SDL_video.h" | |
31 #include "SDL_blit.h" | |
32 | |
33 /* This isn't ready for general consumption yet - it should be folded | |
34 into the general blitting mechanism. | |
35 */ | |
36 | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
37 #if ((defined(_MFC_VER) && defined(_M_IX86)/* && !defined(_WIN32_WCE) still needed? */) || \ |
1402
d910939febfa
Use consistent identifiers for the various platforms we support.
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
38 (defined(__GNUC__) && defined(__i386__))) && SDL_ASSEMBLY_ROUTINES |
0 | 39 #define USE_ASM_STRETCH |
40 #endif | |
41 | |
42 #ifdef USE_ASM_STRETCH | |
43 | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
44 #if defined(_M_IX86) || defined(i386) |
0 | 45 #define PREFIX16 0x66 |
46 #define STORE_BYTE 0xAA | |
47 #define STORE_WORD 0xAB | |
48 #define LOAD_BYTE 0xAC | |
49 #define LOAD_WORD 0xAD | |
50 #define RETURN 0xC3 | |
51 #else | |
52 #error Need assembly opcodes for this architecture | |
53 #endif | |
54 | |
55 static unsigned char copy_row[4096]; | |
56 | |
57 static int generate_rowbytes(int src_w, int dst_w, int bpp) | |
58 { | |
59 static struct { | |
60 int bpp; | |
61 int src_w; | |
62 int dst_w; | |
63 } last; | |
64 | |
65 int i; | |
66 int pos, inc; | |
67 unsigned char *eip; | |
68 unsigned char load, store; | |
69 | |
70 /* See if we need to regenerate the copy buffer */ | |
71 if ( (src_w == last.src_w) && | |
1164
10b3fb28c86b
Date: Mon, 31 Oct 2005 14:23:34 +0100
Ryan C. Gordon <icculus@icculus.org>
parents:
894
diff
changeset
|
72 (dst_w == last.dst_w) && (bpp == last.bpp) ) { |
0 | 73 return(0); |
74 } | |
75 last.bpp = bpp; | |
76 last.src_w = src_w; | |
77 last.dst_w = dst_w; | |
78 | |
79 switch (bpp) { | |
80 case 1: | |
81 load = LOAD_BYTE; | |
82 store = STORE_BYTE; | |
83 break; | |
84 case 2: | |
85 case 4: | |
86 load = LOAD_WORD; | |
87 store = STORE_WORD; | |
88 break; | |
89 default: | |
90 SDL_SetError("ASM stretch of %d bytes isn't supported\n", bpp); | |
91 return(-1); | |
92 } | |
93 pos = 0x10000; | |
94 inc = (src_w << 16) / dst_w; | |
95 eip = copy_row; | |
96 for ( i=0; i<dst_w; ++i ) { | |
97 while ( pos >= 0x10000L ) { | |
98 if ( bpp == 2 ) { | |
99 *eip++ = PREFIX16; | |
100 } | |
101 *eip++ = load; | |
102 pos -= 0x10000L; | |
103 } | |
104 if ( bpp == 2 ) { | |
105 *eip++ = PREFIX16; | |
106 } | |
107 *eip++ = store; | |
108 pos += inc; | |
109 } | |
110 *eip++ = RETURN; | |
111 | |
112 /* Verify that we didn't overflow (too late) */ | |
113 if ( eip > (copy_row+sizeof(copy_row)) ) { | |
114 SDL_SetError("Copy buffer overflow"); | |
115 return(-1); | |
116 } | |
117 return(0); | |
118 } | |
119 | |
120 #else | |
121 | |
122 #define DEFINE_COPY_ROW(name, type) \ | |
123 void name(type *src, int src_w, type *dst, int dst_w) \ | |
124 { \ | |
125 int i; \ | |
126 int pos, inc; \ | |
127 type pixel = 0; \ | |
128 \ | |
129 pos = 0x10000; \ | |
130 inc = (src_w << 16) / dst_w; \ | |
131 for ( i=dst_w; i>0; --i ) { \ | |
132 while ( pos >= 0x10000L ) { \ | |
133 pixel = *src++; \ | |
134 pos -= 0x10000L; \ | |
135 } \ | |
136 *dst++ = pixel; \ | |
137 pos += inc; \ | |
138 } \ | |
139 } | |
140 DEFINE_COPY_ROW(copy_row1, Uint8) | |
141 DEFINE_COPY_ROW(copy_row2, Uint16) | |
142 DEFINE_COPY_ROW(copy_row4, Uint32) | |
143 | |
144 #endif /* USE_ASM_STRETCH */ | |
145 | |
146 /* The ASM code doesn't handle 24-bpp stretch blits */ | |
147 void copy_row3(Uint8 *src, int src_w, Uint8 *dst, int dst_w) | |
148 { | |
149 int i; | |
150 int pos, inc; | |
151 Uint8 pixel[3]; | |
152 | |
153 pos = 0x10000; | |
154 inc = (src_w << 16) / dst_w; | |
155 for ( i=dst_w; i>0; --i ) { | |
156 while ( pos >= 0x10000L ) { | |
157 pixel[0] = *src++; | |
158 pixel[1] = *src++; | |
159 pixel[2] = *src++; | |
160 pos -= 0x10000L; | |
161 } | |
162 *dst++ = pixel[0]; | |
163 *dst++ = pixel[1]; | |
164 *dst++ = pixel[2]; | |
165 pos += inc; | |
166 } | |
167 } | |
168 | |
169 /* Perform a stretch blit between two surfaces of the same format. | |
170 NOTE: This function is not safe to call from multiple threads! | |
171 */ | |
172 int SDL_SoftStretch(SDL_Surface *src, SDL_Rect *srcrect, | |
173 SDL_Surface *dst, SDL_Rect *dstrect) | |
174 { | |
894
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
175 int src_locked; |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
176 int dst_locked; |
0 | 177 int pos, inc; |
178 int dst_width; | |
179 int dst_maxrow; | |
180 int src_row, dst_row; | |
181 Uint8 *srcp = NULL; | |
182 Uint8 *dstp; | |
183 SDL_Rect full_src; | |
184 SDL_Rect full_dst; | |
185 #if defined(USE_ASM_STRETCH) && defined(__GNUC__) | |
186 int u1, u2; | |
187 #endif | |
188 const int bpp = dst->format->BytesPerPixel; | |
189 | |
190 if ( src->format->BitsPerPixel != dst->format->BitsPerPixel ) { | |
191 SDL_SetError("Only works with same format surfaces"); | |
192 return(-1); | |
193 } | |
194 | |
195 /* Verify the blit rectangles */ | |
196 if ( srcrect ) { | |
197 if ( (srcrect->x < 0) || (srcrect->y < 0) || | |
198 ((srcrect->x+srcrect->w) > src->w) || | |
199 ((srcrect->y+srcrect->h) > src->h) ) { | |
200 SDL_SetError("Invalid source blit rectangle"); | |
201 return(-1); | |
202 } | |
203 } else { | |
204 full_src.x = 0; | |
205 full_src.y = 0; | |
206 full_src.w = src->w; | |
207 full_src.h = src->h; | |
208 srcrect = &full_src; | |
209 } | |
210 if ( dstrect ) { | |
211 if ( (dstrect->x < 0) || (dstrect->y < 0) || | |
212 ((dstrect->x+dstrect->w) > dst->w) || | |
213 ((dstrect->y+dstrect->h) > dst->h) ) { | |
214 SDL_SetError("Invalid destination blit rectangle"); | |
215 return(-1); | |
216 } | |
217 } else { | |
218 full_dst.x = 0; | |
219 full_dst.y = 0; | |
220 full_dst.w = dst->w; | |
221 full_dst.h = dst->h; | |
222 dstrect = &full_dst; | |
223 } | |
224 | |
894
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
225 /* Lock the destination if it's in hardware */ |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
226 dst_locked = 0; |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
227 if ( SDL_MUSTLOCK(dst) ) { |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
228 if ( SDL_LockSurface(dst) < 0 ) { |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
229 SDL_SetError("Unable to lock destination surface"); |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
230 return(-1); |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
231 } |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
232 dst_locked = 1; |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
233 } |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
234 /* Lock the source if it's in hardware */ |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
235 src_locked = 0; |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
236 if ( SDL_MUSTLOCK(src) ) { |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
237 if ( SDL_LockSurface(src) < 0 ) { |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
238 if ( dst_locked ) { |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
239 SDL_UnlockSurface(dst); |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
240 } |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
241 SDL_SetError("Unable to lock source surface"); |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
242 return(-1); |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
243 } |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
244 src_locked = 1; |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
245 } |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
246 |
0 | 247 /* Set up the data... */ |
248 pos = 0x10000; | |
249 inc = (srcrect->h << 16) / dstrect->h; | |
250 src_row = srcrect->y; | |
251 dst_row = dstrect->y; | |
252 dst_width = dstrect->w*bpp; | |
253 | |
254 #ifdef USE_ASM_STRETCH | |
255 /* Write the opcodes for this stretch */ | |
256 if ( (bpp != 3) && | |
257 (generate_rowbytes(srcrect->w, dstrect->w, bpp) < 0) ) { | |
258 return(-1); | |
259 } | |
260 #endif | |
261 | |
262 /* Perform the stretch blit */ | |
263 for ( dst_maxrow = dst_row+dstrect->h; dst_row<dst_maxrow; ++dst_row ) { | |
264 dstp = (Uint8 *)dst->pixels + (dst_row*dst->pitch) | |
265 + (dstrect->x*bpp); | |
266 while ( pos >= 0x10000L ) { | |
267 srcp = (Uint8 *)src->pixels + (src_row*src->pitch) | |
268 + (srcrect->x*bpp); | |
269 ++src_row; | |
270 pos -= 0x10000L; | |
271 } | |
272 #ifdef USE_ASM_STRETCH | |
273 switch (bpp) { | |
274 case 3: | |
275 copy_row3(srcp, srcrect->w, dstp, dstrect->w); | |
276 break; | |
277 default: | |
278 #ifdef __GNUC__ | |
627
8b9ac38381d0
Fixed compile problem in SDL_stretch.c with gcc 3.3
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
279 __asm__ __volatile__ ( |
1228
f4a3a4129d04
From Mike Frysinger and/or Gentoo:
Ryan C. Gordon <icculus@icculus.org>
parents:
1164
diff
changeset
|
280 "call *%4" |
0 | 281 : "=&D" (u1), "=&S" (u2) |
1234
73676c1f56ee
For sanity's sake, removed the '&' when passing copy_row array to asm.
Ryan C. Gordon <icculus@icculus.org>
parents:
1233
diff
changeset
|
282 : "0" (dstp), "1" (srcp), "r" (copy_row) |
0 | 283 : "memory" ); |
284 #else | |
1361
19418e4422cb
New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents:
1358
diff
changeset
|
285 #ifdef _MSC_VER |
1234
73676c1f56ee
For sanity's sake, removed the '&' when passing copy_row array to asm.
Ryan C. Gordon <icculus@icculus.org>
parents:
1233
diff
changeset
|
286 { void *code = copy_row; |
0 | 287 __asm { |
288 push edi | |
289 push esi | |
290 | |
291 mov edi, dstp | |
292 mov esi, srcp | |
293 call dword ptr code | |
294 | |
295 pop esi | |
296 pop edi | |
297 } | |
298 } | |
299 #else | |
300 #error Need inline assembly for this compiler | |
301 #endif | |
302 #endif /* __GNUC__ */ | |
303 break; | |
304 } | |
305 #else | |
306 switch (bpp) { | |
307 case 1: | |
308 copy_row1(srcp, srcrect->w, dstp, dstrect->w); | |
309 break; | |
310 case 2: | |
311 copy_row2((Uint16 *)srcp, srcrect->w, | |
312 (Uint16 *)dstp, dstrect->w); | |
313 break; | |
314 case 3: | |
315 copy_row3(srcp, srcrect->w, dstp, dstrect->w); | |
316 break; | |
317 case 4: | |
318 copy_row4((Uint32 *)srcp, srcrect->w, | |
319 (Uint32 *)dstp, dstrect->w); | |
320 break; | |
321 } | |
322 #endif | |
323 pos += inc; | |
324 } | |
894
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
325 |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
326 /* We need to unlock the surfaces if they're locked */ |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
327 if ( dst_locked ) { |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
328 SDL_UnlockSurface(dst); |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
329 } |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
330 if ( src_locked ) { |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
331 SDL_UnlockSurface(src); |
1d1a823904d8
Don't crash if the stretch routines are used on hardware surfaces
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
332 } |
0 | 333 return(0); |
334 } | |
335 |