2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "SDL_error.h"
33 #include "SDL_video.h"
34 #include "SDL_sysvideo.h"
36 #include "SDL_RLEaccel_c.h"
37 #include "SDL_pixels_c.h"
38 #include "SDL_memops.h"
40 #if defined(i386) && defined(__GNUC__) && defined(USE_ASMBLIT)
42 /* Function to check the CPU flags */
43 #define MMX_CPU 0x800000
44 #define SSE_CPU 0x2000000
45 #define CPU_Flags() Hermes_X86_CPU()
47 #define HermesConverterInterface void
48 #define HermesClearInterface void
53 /* The general purpose software blit routine */
54 static int SDL_SoftBlit(SDL_Surface *src, SDL_Rect *srcrect,
55 SDL_Surface *dst, SDL_Rect *dstrect)
61 /* Everything is okay at the beginning... */
64 /* Lock the destination if it's in hardware */
66 if ( SDL_MUSTLOCK(dst) ) {
67 if ( SDL_LockSurface(dst) < 0 ) {
73 /* Lock the source if it's in hardware */
75 if ( SDL_MUSTLOCK(src) ) {
76 if ( SDL_LockSurface(src) < 0 ) {
83 /* Set up source and destination buffer pointers, and BLIT! */
84 if ( okay && srcrect->w && srcrect->h ) {
88 /* Set up the blit information */
89 info.s_pixels = (Uint8 *)src->pixels +
90 (Uint16)srcrect->y*src->pitch +
91 (Uint16)srcrect->x*src->format->BytesPerPixel;
92 info.s_width = srcrect->w;
93 info.s_height = srcrect->h;
94 info.s_skip=src->pitch-info.s_width*src->format->BytesPerPixel;
95 info.d_pixels = (Uint8 *)dst->pixels +
96 (Uint16)dstrect->y*dst->pitch +
97 (Uint16)dstrect->x*dst->format->BytesPerPixel;
98 info.d_width = dstrect->w;
99 info.d_height = dstrect->h;
100 info.d_skip=dst->pitch-info.d_width*dst->format->BytesPerPixel;
101 info.aux_data = src->map->sw_data->aux_data;
102 info.src = src->format;
103 info.table = src->map->table;
104 info.dst = dst->format;
105 RunBlit = src->map->sw_data->blit;
107 /* Run the actual software blit */
111 /* We need to unlock the surfaces if they're locked */
113 SDL_UnlockSurface(dst);
116 SDL_UnlockSurface(src);
119 return(okay ? 0 : -1);
122 #if defined(i386) && defined(__GNUC__) && defined(USE_ASMBLIT)
123 void SDL_memcpyMMX(char* to,char* from,int len)
127 for(i=0; i<len/8; i++) {
128 __asm__ __volatile__ (
129 " movq (%0), %%mm0\n"
130 " movq %%mm0, (%1)\n"
131 : : "r" (from), "r" (to) : "memory");
136 SDL_memcpy(to, from, len&7);
139 void SDL_memcpySSE(char* to,char* from,int len)
143 __asm__ __volatile__ (
144 " prefetchnta (%0)\n"
145 " prefetchnta 64(%0)\n"
146 " prefetchnta 128(%0)\n"
147 " prefetchnta 192(%0)\n"
150 for(i=0; i<len/8; i++) {
151 __asm__ __volatile__ (
152 " prefetchnta 256(%0)\n"
153 " movq (%0), %%mm0\n"
154 " movntq %%mm0, (%1)\n"
155 : : "r" (from), "r" (to) : "memory");
160 SDL_memcpy(to, from, len&7);
164 static void SDL_BlitCopy(SDL_BlitInfo *info)
168 int srcskip, dstskip;
169 #if defined(i386) && defined(__GNUC__) && defined(USE_ASMBLIT)
173 w = info->d_width*info->dst->BytesPerPixel;
175 src = info->s_pixels;
176 dst = info->d_pixels;
177 srcskip = w+info->s_skip;
178 dstskip = w+info->d_skip;
179 #if defined(i386) && defined(__GNUC__) && defined(USE_ASMBLIT)
181 if((f&(MMX_CPU|SSE_CPU))==(MMX_CPU|SSE_CPU))
184 SDL_memcpySSE(dst, src, w);
188 __asm__ __volatile__ (
196 SDL_memcpyMMX(dst, src, w);
200 __asm__ __volatile__ (
207 SDL_memcpy(dst, src, w);
213 static void SDL_BlitCopyOverlap(SDL_BlitInfo *info)
217 int srcskip, dstskip;
219 w = info->d_width*info->dst->BytesPerPixel;
221 src = info->s_pixels;
222 dst = info->d_pixels;
223 srcskip = w+info->s_skip;
224 dstskip = w+info->d_skip;
227 SDL_memcpy(dst, src, w);
232 src += ((h-1) * srcskip);
233 dst += ((h-1) * dstskip);
235 SDL_revcpy(dst, src, w);
242 /* Figure out which of many blit routines to set up on a surface */
243 int SDL_CalculateBlit(SDL_Surface *surface)
247 /* Clean everything out to start */
248 if ( (surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
249 SDL_UnRLESurface(surface, 1);
251 surface->map->sw_blit = NULL;
253 /* Figure out if an accelerated hardware blit is possible */
254 surface->flags &= ~SDL_HWACCEL;
255 if ( surface->map->identity ) {
258 if ( (surface->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
259 /* We only support accelerated blitting to hardware */
260 if ( surface->map->dst->flags & SDL_HWSURFACE ) {
261 hw_blit_ok = current_video->info.blit_hw;
265 if (hw_blit_ok && (surface->flags & SDL_SRCCOLORKEY)) {
266 hw_blit_ok = current_video->info.blit_hw_CC;
268 if ( hw_blit_ok && (surface->flags & SDL_SRCALPHA) ) {
269 hw_blit_ok = current_video->info.blit_hw_A;
272 /* We only support accelerated blitting to hardware */
273 if ( surface->map->dst->flags & SDL_HWSURFACE ) {
274 hw_blit_ok = current_video->info.blit_sw;
278 if (hw_blit_ok && (surface->flags & SDL_SRCCOLORKEY)) {
279 hw_blit_ok = current_video->info.blit_sw_CC;
281 if ( hw_blit_ok && (surface->flags & SDL_SRCALPHA) ) {
282 hw_blit_ok = current_video->info.blit_sw_A;
286 SDL_VideoDevice *video = current_video;
287 SDL_VideoDevice *this = current_video;
288 video->CheckHWBlit(this, surface, surface->map->dst);
292 /* Get the blit function index, based on surface mode */
293 /* { 0 = nothing, 1 = colorkey, 2 = alpha, 3 = colorkey+alpha } */
295 blit_index |= (!!(surface->flags & SDL_SRCCOLORKEY)) << 0;
296 if ( surface->flags & SDL_SRCALPHA
297 && (surface->format->alpha != SDL_ALPHA_OPAQUE
298 || surface->format->Amask) ) {
302 /* Check for special "identity" case -- copy blit */
303 if ( surface->map->identity && blit_index == 0 ) {
304 surface->map->sw_data->blit = SDL_BlitCopy;
306 /* Handle overlapping blits on the same surface */
307 if ( surface == surface->map->dst ) {
308 surface->map->sw_data->blit = SDL_BlitCopyOverlap;
311 if ( surface->format->BitsPerPixel < 8 ) {
312 surface->map->sw_data->blit =
313 SDL_CalculateBlit0(surface, blit_index);
315 switch ( surface->format->BytesPerPixel ) {
317 surface->map->sw_data->blit =
318 SDL_CalculateBlit1(surface, blit_index);
323 surface->map->sw_data->blit =
324 SDL_CalculateBlitN(surface, blit_index);
327 surface->map->sw_data->blit = NULL;
332 /* Make sure we have a blit function */
333 if ( surface->map->sw_data->blit == NULL ) {
334 SDL_InvalidateMap(surface->map);
335 SDL_SetError("Blit combination not supported");
339 /* Choose software blitting function */
340 if(surface->flags & SDL_RLEACCELOK
341 && (surface->flags & SDL_HWACCEL) != SDL_HWACCEL) {
343 if(surface->map->identity
345 || (blit_index == 3 && !surface->format->Amask))) {
346 if ( SDL_RLESurface(surface) == 0 )
347 surface->map->sw_blit = SDL_RLEBlit;
348 } else if(blit_index == 2 && surface->format->Amask) {
349 if ( SDL_RLESurface(surface) == 0 )
350 surface->map->sw_blit = SDL_RLEAlphaBlit;
354 if ( surface->map->sw_blit == NULL ) {
355 surface->map->sw_blit = SDL_SoftBlit;