2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
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
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* This a stretch blit implementation based on ideas given to me by
25 Tomasz Cejner - thanks! :)
27 April 27, 2000 - Sam Lantinga
30 #include "SDL_video.h"
33 /* This isn't ready for general consumption yet - it should be folded
34 into the general blitting mechanism.
37 #if ((defined(_MFC_VER) && defined(_M_IX86)/* && !defined(_WIN32_WCE) still needed? */) || \
38 defined(__WATCOMC__) || \
39 (defined(__GNUC__) && defined(__i386__))) && SDL_ASSEMBLY_ROUTINES
40 /* There's a bug with gcc 4.4.1 and -O2 where srcp doesn't get the correct
41 * value after the first scanline. FIXME? */
42 /*#define USE_ASM_STRETCH*/
45 #ifdef USE_ASM_STRETCH
48 #include <sys/types.h>
52 #define PAGE_ALIGNED __attribute__((__aligned__(4096)))
57 #if defined(_M_IX86) || defined(i386)
59 #define STORE_BYTE 0xAA
60 #define STORE_WORD 0xAB
61 #define LOAD_BYTE 0xAC
62 #define LOAD_WORD 0xAD
65 #error Need assembly opcodes for this architecture
68 static unsigned char copy_row[4096] PAGE_ALIGNED;
70 static int generate_rowbytes(int src_w, int dst_w, int bpp)
82 unsigned char load, store;
84 /* See if we need to regenerate the copy buffer */
85 if ( (src_w == last.src_w) &&
86 (dst_w == last.dst_w) && (bpp == last.bpp) ) {
105 SDL_SetError("ASM stretch of %d bytes isn't supported\n", bpp);
109 /* Make the code writeable */
110 if ( mprotect(copy_row, sizeof(copy_row), PROT_READ|PROT_WRITE) < 0 ) {
111 SDL_SetError("Couldn't make copy buffer writeable");
116 inc = (src_w << 16) / dst_w;
118 for ( i=0; i<dst_w; ++i ) {
119 while ( pos >= 0x10000L ) {
134 /* Verify that we didn't overflow (too late!!!) */
135 if ( eip > (copy_row+sizeof(copy_row)) ) {
136 SDL_SetError("Copy buffer overflow");
140 /* Make the code executable but not writeable */
141 if ( mprotect(copy_row, sizeof(copy_row), PROT_READ|PROT_EXEC) < 0 ) {
142 SDL_SetError("Couldn't make copy buffer executable");
150 #endif /* USE_ASM_STRETCH */
152 #define DEFINE_COPY_ROW(name, type) \
153 void name(type *src, int src_w, type *dst, int dst_w) \
160 inc = (src_w << 16) / dst_w; \
161 for ( i=dst_w; i>0; --i ) { \
162 while ( pos >= 0x10000L ) { \
170 DEFINE_COPY_ROW(copy_row1, Uint8)
171 DEFINE_COPY_ROW(copy_row2, Uint16)
172 DEFINE_COPY_ROW(copy_row4, Uint32)
174 /* The ASM code doesn't handle 24-bpp stretch blits */
175 void copy_row3(Uint8 *src, int src_w, Uint8 *dst, int dst_w)
179 Uint8 pixel[3] = { 0, 0, 0 };
182 inc = (src_w << 16) / dst_w;
183 for ( i=dst_w; i>0; --i ) {
184 while ( pos >= 0x10000L ) {
197 /* Perform a stretch blit between two surfaces of the same format.
198 NOTE: This function is not safe to call from multiple threads!
200 int SDL_SoftStretch(SDL_Surface *src, SDL_Rect *srcrect,
201 SDL_Surface *dst, SDL_Rect *dstrect)
208 int src_row, dst_row;
213 #ifdef USE_ASM_STRETCH
214 SDL_bool use_asm = SDL_TRUE;
218 #endif /* USE_ASM_STRETCH */
219 const int bpp = dst->format->BytesPerPixel;
221 if ( src->format->BitsPerPixel != dst->format->BitsPerPixel ) {
222 SDL_SetError("Only works with same format surfaces");
226 /* Verify the blit rectangles */
228 if ( (srcrect->x < 0) || (srcrect->y < 0) ||
229 ((srcrect->x+srcrect->w) > src->w) ||
230 ((srcrect->y+srcrect->h) > src->h) ) {
231 SDL_SetError("Invalid source blit rectangle");
242 if ( (dstrect->x < 0) || (dstrect->y < 0) ||
243 ((dstrect->x+dstrect->w) > dst->w) ||
244 ((dstrect->y+dstrect->h) > dst->h) ) {
245 SDL_SetError("Invalid destination blit rectangle");
256 /* Lock the destination if it's in hardware */
258 if ( SDL_MUSTLOCK(dst) ) {
259 if ( SDL_LockSurface(dst) < 0 ) {
260 SDL_SetError("Unable to lock destination surface");
265 /* Lock the source if it's in hardware */
267 if ( SDL_MUSTLOCK(src) ) {
268 if ( SDL_LockSurface(src) < 0 ) {
270 SDL_UnlockSurface(dst);
272 SDL_SetError("Unable to lock source surface");
278 /* Set up the data... */
280 inc = (srcrect->h << 16) / dstrect->h;
281 src_row = srcrect->y;
282 dst_row = dstrect->y;
283 dst_width = dstrect->w*bpp;
285 #ifdef USE_ASM_STRETCH
286 /* Write the opcodes for this stretch */
288 (generate_rowbytes(srcrect->w, dstrect->w, bpp) < 0) ) {
293 /* Perform the stretch blit */
294 for ( dst_maxrow = dst_row+dstrect->h; dst_row<dst_maxrow; ++dst_row ) {
295 dstp = (Uint8 *)dst->pixels + (dst_row*dst->pitch)
297 while ( pos >= 0x10000L ) {
298 srcp = (Uint8 *)src->pixels + (src_row*src->pitch)
303 #ifdef USE_ASM_STRETCH
306 __asm__ __volatile__ (
308 : "=&D" (u1), "=&S" (u2)
309 : "0" (dstp), "1" (srcp), "r" (copy_row)
311 #elif defined(_MSC_VER) || defined(__WATCOMC__)
312 { void *code = copy_row;
326 #error Need inline assembly for this compiler
332 copy_row1(srcp, srcrect->w, dstp, dstrect->w);
335 copy_row2((Uint16 *)srcp, srcrect->w,
336 (Uint16 *)dstp, dstrect->w);
339 copy_row3(srcp, srcrect->w, dstp, dstrect->w);
342 copy_row4((Uint32 *)srcp, srcrect->w,
343 (Uint32 *)dstp, dstrect->w);
349 /* We need to unlock the surfaces if they're locked */
351 SDL_UnlockSurface(dst);
354 SDL_UnlockSurface(src);