19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* 29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL - Simple DirectMedia Layer 39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Copyright (C) 1997-2012 Sam Lantinga 49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall This library is free software; you can redistribute it and/or 69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall modify it under the terms of the GNU Lesser General Public 79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall License as published by the Free Software Foundation; either 89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall version 2.1 of the License, or (at your option) any later version. 99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall This library is distributed in the hope that it will be useful, 119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall but WITHOUT ANY WARRANTY; without even the implied warranty of 129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Lesser General Public License for more details. 149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall You should have received a copy of the GNU Lesser General Public 169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall License along with this library; if not, write to the Free Software 179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Sam Lantinga 209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall slouken@libsdl.org 219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/ 229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h" 239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This a stretch blit implementation based on ideas given to me by 259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Tomasz Cejner - thanks! :) 269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall April 27, 2000 - Sam Lantinga 289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/ 299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_video.h" 319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_blit.h" 329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This isn't ready for general consumption yet - it should be folded 349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall into the general blitting mechanism. 359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/ 369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if ((defined(_MFC_VER) && defined(_M_IX86)/* && !defined(_WIN32_WCE) still needed? */) || \ 389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall defined(__WATCOMC__) || \ 399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (defined(__GNUC__) && defined(__i386__))) && SDL_ASSEMBLY_ROUTINES 409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* There's a bug with gcc 4.4.1 and -O2 where srcp doesn't get the correct 419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * value after the first scanline. FIXME? */ 429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*#define USE_ASM_STRETCH*/ 439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_ASM_STRETCH 469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef HAVE_MPROTECT 489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/types.h> 499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/mman.h> 509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __GNUC__ 529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define PAGE_ALIGNED __attribute__((__aligned__(4096))) 539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else 549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define PAGE_ALIGNED 559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if defined(_M_IX86) || defined(i386) 589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define PREFIX16 0x66 599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define STORE_BYTE 0xAA 609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define STORE_WORD 0xAB 619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define LOAD_BYTE 0xAC 629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define LOAD_WORD 0xAD 639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define RETURN 0xC3 649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else 659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#error Need assembly opcodes for this architecture 669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic unsigned char copy_row[4096] PAGE_ALIGNED; 699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int generate_rowbytes(int src_w, int dst_w, int bpp) 719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall static struct { 739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int bpp; 749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int src_w; 759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int dst_w; 769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int status; 779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } last; 789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int i; 809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int pos, inc; 819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall unsigned char *eip, *fence; 829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall unsigned char load, store; 839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* See if we need to regenerate the copy buffer */ 859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( (src_w == last.src_w) && 869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (dst_w == last.dst_w) && (bpp == last.bpp) ) { 879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(last.status); 889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall last.bpp = bpp; 909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall last.src_w = src_w; 919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall last.dst_w = dst_w; 929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall last.status = -1; 939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall switch (bpp) { 959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case 1: 969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall load = LOAD_BYTE; 979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall store = STORE_BYTE; 989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case 2: 1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case 4: 1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall load = LOAD_WORD; 1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall store = STORE_WORD; 1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall default: 1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("ASM stretch of %d bytes isn't supported\n", bpp); 1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef HAVE_MPROTECT 1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Make the code writeable */ 1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( mprotect(copy_row, sizeof(copy_row), PROT_READ|PROT_WRITE) < 0 ) { 1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Couldn't make copy buffer writeable"); 1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall pos = 0x10000; 1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall inc = (src_w << 16) / dst_w; 1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall eip = copy_row; 1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall fence = copy_row+sizeof(copy_row)-2; 1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for ( i=0; i<dst_w && eip < end; ++i ) { 1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall while ( pos >= 0x10000L ) { 1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( eip == fence ) { 1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return -1; 1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( bpp == 2 ) { 1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *eip++ = PREFIX16; 1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *eip++ = load; 1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall pos -= 0x10000L; 1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( eip == fence ) { 1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return -1; 1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( bpp == 2 ) { 1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *eip++ = PREFIX16; 1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *eip++ = store; 1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall pos += inc; 1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *eip++ = RETURN; 1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef HAVE_MPROTECT 1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Make the code executable but not writeable */ 1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( mprotect(copy_row, sizeof(copy_row), PROT_READ|PROT_EXEC) < 0 ) { 1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Couldn't make copy buffer executable"); 1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall last.status = 0; 1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(0); 1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* USE_ASM_STRETCH */ 1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define DEFINE_COPY_ROW(name, type) \ 1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid name(type *src, int src_w, type *dst, int dst_w) \ 1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ \ 1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int i; \ 1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int pos, inc; \ 1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall type pixel = 0; \ 1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall \ 1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall pos = 0x10000; \ 1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall inc = (src_w << 16) / dst_w; \ 1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for ( i=dst_w; i>0; --i ) { \ 1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall while ( pos >= 0x10000L ) { \ 1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall pixel = *src++; \ 1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall pos -= 0x10000L; \ 1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } \ 1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *dst++ = pixel; \ 1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall pos += inc; \ 1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } \ 1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallDEFINE_COPY_ROW(copy_row1, Uint8) 1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallDEFINE_COPY_ROW(copy_row2, Uint16) 1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallDEFINE_COPY_ROW(copy_row4, Uint32) 1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The ASM code doesn't handle 24-bpp stretch blits */ 1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid copy_row3(Uint8 *src, int src_w, Uint8 *dst, int dst_w) 1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int i; 1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int pos, inc; 1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Uint8 pixel[3] = { 0, 0, 0 }; 1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall pos = 0x10000; 1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall inc = (src_w << 16) / dst_w; 1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for ( i=dst_w; i>0; --i ) { 1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall while ( pos >= 0x10000L ) { 1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall pixel[0] = *src++; 1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall pixel[1] = *src++; 1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall pixel[2] = *src++; 1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall pos -= 0x10000L; 1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *dst++ = pixel[0]; 1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *dst++ = pixel[1]; 1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall *dst++ = pixel[2]; 1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall pos += inc; 1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Perform a stretch blit between two surfaces of the same format. 2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall NOTE: This function is not safe to call from multiple threads! 2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/ 2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SoftStretch(SDL_Surface *src, SDL_Rect *srcrect, 2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_Surface *dst, SDL_Rect *dstrect) 2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{ 2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int src_locked; 2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int dst_locked; 2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int pos, inc; 2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int dst_maxrow; 2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int src_row, dst_row; 2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Uint8 *srcp = NULL; 2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall Uint8 *dstp; 2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_Rect full_src; 2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_Rect full_dst; 2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_ASM_STRETCH 2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_bool use_asm = SDL_TRUE; 2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __GNUC__ 2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall int u1, u2; 2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* USE_ASM_STRETCH */ 2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall const int bpp = dst->format->BytesPerPixel; 2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( src->format->BitsPerPixel != dst->format->BitsPerPixel ) { 2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Only works with same format surfaces"); 2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Verify the blit rectangles */ 2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( srcrect ) { 2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( (srcrect->x < 0) || (srcrect->y < 0) || 2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ((srcrect->x+srcrect->w) > src->w) || 2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ((srcrect->y+srcrect->h) > src->h) ) { 2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Invalid source blit rectangle"); 2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else { 2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall full_src.x = 0; 2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall full_src.y = 0; 2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall full_src.w = src->w; 2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall full_src.h = src->h; 2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall srcrect = &full_src; 2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( dstrect ) { 2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( (dstrect->x < 0) || (dstrect->y < 0) || 2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ((dstrect->x+dstrect->w) > dst->w) || 2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ((dstrect->y+dstrect->h) > dst->h) ) { 2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Invalid destination blit rectangle"); 2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else { 2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall full_dst.x = 0; 2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall full_dst.y = 0; 2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall full_dst.w = dst->w; 2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall full_dst.h = dst->h; 2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall dstrect = &full_dst; 2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Lock the destination if it's in hardware */ 2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall dst_locked = 0; 2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( SDL_MUSTLOCK(dst) ) { 2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( SDL_LockSurface(dst) < 0 ) { 2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Unable to lock destination surface"); 2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall dst_locked = 1; 2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Lock the source if it's in hardware */ 2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall src_locked = 0; 2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( SDL_MUSTLOCK(src) ) { 2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( SDL_LockSurface(src) < 0 ) { 2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( dst_locked ) { 2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_UnlockSurface(dst); 2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_SetError("Unable to lock source surface"); 2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(-1); 2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall src_locked = 1; 2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Set up the data... */ 2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall pos = 0x10000; 2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall inc = (srcrect->h << 16) / dstrect->h; 2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall src_row = srcrect->y; 2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall dst_row = dstrect->y; 2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_ASM_STRETCH 2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Write the opcodes for this stretch */ 2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( (bpp == 3) || 2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (generate_rowbytes(srcrect->w, dstrect->w, bpp) < 0) ) { 2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall use_asm = SDL_FALSE; 2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* Perform the stretch blit */ 2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall for ( dst_maxrow = dst_row+dstrect->h; dst_row<dst_maxrow; ++dst_row ) { 2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall dstp = (Uint8 *)dst->pixels + (dst_row*dst->pitch) 2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall + (dstrect->x*bpp); 2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall while ( pos >= 0x10000L ) { 2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall srcp = (Uint8 *)src->pixels + (src_row*src->pitch) 2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall + (srcrect->x*bpp); 3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall ++src_row; 3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall pos -= 0x10000L; 3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef USE_ASM_STRETCH 3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if (use_asm) { 3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef __GNUC__ 3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall __asm__ __volatile__ ( 3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall "call *%4" 3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall : "=&D" (u1), "=&S" (u2) 3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall : "0" (dstp), "1" (srcp), "r" (copy_row) 3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall : "memory" ); 3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#elif defined(_MSC_VER) || defined(__WATCOMC__) 3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall { void *code = copy_row; 3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall __asm { 3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall push edi 3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall push esi 3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall mov edi, dstp 3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall mov esi, srcp 3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall call dword ptr code 3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall pop esi 3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall pop edi 3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else 3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#error Need inline assembly for this compiler 3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } else 3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif 3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall switch (bpp) { 3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case 1: 3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall copy_row1(srcp, srcrect->w, dstp, dstrect->w); 3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case 2: 3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall copy_row2((Uint16 *)srcp, srcrect->w, 3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (Uint16 *)dstp, dstrect->w); 3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case 3: 3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall copy_row3(srcp, srcrect->w, dstp, dstrect->w); 3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall case 4: 3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall copy_row4((Uint32 *)srcp, srcrect->w, 3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall (Uint32 *)dstp, dstrect->w); 3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall break; 3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall pos += inc; 3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall /* We need to unlock the surfaces if they're locked */ 3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( dst_locked ) { 3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_UnlockSurface(dst); 3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall if ( src_locked ) { 3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall SDL_UnlockSurface(src); 3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall } 3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall return(0); 3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall} 3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall 359