Skip to content

Latest commit

 

History

History
362 lines (325 loc) · 8.84 KB

SDL_blit.c

File metadata and controls

362 lines (325 loc) · 8.84 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Jan 4, 2004
Jan 4, 2004
3
Copyright (C) 1997-2004 Sam Lantinga
Apr 26, 2001
Apr 26, 2001
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Apr 26, 2001
Apr 26, 2001
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
*/
#ifdef SAVE_RCSID
static char rcsid =
"@(#) $Id$";
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "SDL_error.h"
#include "SDL_video.h"
#include "SDL_sysvideo.h"
#include "SDL_blit.h"
#include "SDL_RLEaccel_c.h"
#include "SDL_pixels_c.h"
#include "SDL_memops.h"
Apr 11, 2004
Apr 11, 2004
40
41
42
43
44
#if (defined(i386) || defined(__x86_64__)) && defined(__GNUC__) && defined(USE_ASMBLIT)
#define MMX_ASMBLIT
#endif
#if defined(MMX_ASMBLIT)
Nov 18, 2003
Nov 18, 2003
45
#include "SDL_cpuinfo.h"
Aug 22, 2003
Aug 22, 2003
46
47
48
#include "mmx.h"
#endif
Apr 26, 2001
Apr 26, 2001
49
50
51
52
53
54
55
56
57
58
59
60
61
/* The general purpose software blit routine */
static int SDL_SoftBlit(SDL_Surface *src, SDL_Rect *srcrect,
SDL_Surface *dst, SDL_Rect *dstrect)
{
int okay;
int src_locked;
int dst_locked;
/* Everything is okay at the beginning... */
okay = 1;
/* Lock the destination if it's in hardware */
dst_locked = 0;
Oct 11, 2002
Oct 11, 2002
62
63
if ( SDL_MUSTLOCK(dst) ) {
if ( SDL_LockSurface(dst) < 0 ) {
Apr 26, 2001
Apr 26, 2001
64
65
66
67
68
69
70
okay = 0;
} else {
dst_locked = 1;
}
}
/* Lock the source if it's in hardware */
src_locked = 0;
Oct 11, 2002
Oct 11, 2002
71
72
if ( SDL_MUSTLOCK(src) ) {
if ( SDL_LockSurface(src) < 0 ) {
Apr 26, 2001
Apr 26, 2001
73
74
75
76
77
78
79
80
81
82
83
84
okay = 0;
} else {
src_locked = 1;
}
}
/* Set up source and destination buffer pointers, and BLIT! */
if ( okay && srcrect->w && srcrect->h ) {
SDL_BlitInfo info;
SDL_loblit RunBlit;
/* Set up the blit information */
Oct 11, 2002
Oct 11, 2002
85
info.s_pixels = (Uint8 *)src->pixels +
Apr 26, 2001
Apr 26, 2001
86
87
88
89
90
(Uint16)srcrect->y*src->pitch +
(Uint16)srcrect->x*src->format->BytesPerPixel;
info.s_width = srcrect->w;
info.s_height = srcrect->h;
info.s_skip=src->pitch-info.s_width*src->format->BytesPerPixel;
Oct 11, 2002
Oct 11, 2002
91
info.d_pixels = (Uint8 *)dst->pixels +
Apr 26, 2001
Apr 26, 2001
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
(Uint16)dstrect->y*dst->pitch +
(Uint16)dstrect->x*dst->format->BytesPerPixel;
info.d_width = dstrect->w;
info.d_height = dstrect->h;
info.d_skip=dst->pitch-info.d_width*dst->format->BytesPerPixel;
info.aux_data = src->map->sw_data->aux_data;
info.src = src->format;
info.table = src->map->table;
info.dst = dst->format;
RunBlit = src->map->sw_data->blit;
/* Run the actual software blit */
RunBlit(&info);
}
/* We need to unlock the surfaces if they're locked */
if ( dst_locked ) {
Oct 11, 2002
Oct 11, 2002
109
SDL_UnlockSurface(dst);
Mar 20, 2002
Mar 20, 2002
110
}
Apr 26, 2001
Apr 26, 2001
111
if ( src_locked ) {
Oct 11, 2002
Oct 11, 2002
112
SDL_UnlockSurface(src);
Apr 26, 2001
Apr 26, 2001
113
114
115
116
117
}
/* Blit is done! */
return(okay ? 0 : -1);
}
Apr 11, 2004
Apr 11, 2004
118
#ifdef MMX_ASMBLIT
Apr 11, 2004
Apr 11, 2004
119
static __inline__ void SDL_memcpyMMX(char* to,char* from,int len)
Aug 22, 2003
Aug 22, 2003
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
{
int i;
for(i=0; i<len/8; i++) {
__asm__ __volatile__ (
" movq (%0), %%mm0\n"
" movq %%mm0, (%1)\n"
: : "r" (from), "r" (to) : "memory");
from+=8;
to+=8;
}
if (len&7)
SDL_memcpy(to, from, len&7);
}
Apr 11, 2004
Apr 11, 2004
135
static __inline__ void SDL_memcpySSE(char* to,char* from,int len)
Aug 22, 2003
Aug 22, 2003
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
{
int i;
__asm__ __volatile__ (
" prefetchnta (%0)\n"
" prefetchnta 64(%0)\n"
" prefetchnta 128(%0)\n"
" prefetchnta 192(%0)\n"
: : "r" (from) );
for(i=0; i<len/8; i++) {
__asm__ __volatile__ (
" prefetchnta 256(%0)\n"
" movq (%0), %%mm0\n"
" movntq %%mm0, (%1)\n"
: : "r" (from), "r" (to) : "memory");
from+=8;
to+=8;
}
if (len&7)
SDL_memcpy(to, from, len&7);
}
#endif
Apr 26, 2001
Apr 26, 2001
160
161
162
163
164
165
166
167
168
169
170
171
static void SDL_BlitCopy(SDL_BlitInfo *info)
{
Uint8 *src, *dst;
int w, h;
int srcskip, dstskip;
w = info->d_width*info->dst->BytesPerPixel;
h = info->d_height;
src = info->s_pixels;
dst = info->d_pixels;
srcskip = w+info->s_skip;
dstskip = w+info->d_skip;
Apr 11, 2004
Apr 11, 2004
172
#ifdef MMX_ASMBLIT
Nov 18, 2003
Nov 18, 2003
173
if(SDL_HasSSE())
Aug 22, 2003
Aug 22, 2003
174
175
176
177
178
179
180
181
182
183
184
{
while ( h-- ) {
SDL_memcpySSE(dst, src, w);
src += srcskip;
dst += dstskip;
}
__asm__ __volatile__ (
" emms\n"
::);
}
else
Nov 18, 2003
Nov 18, 2003
185
if(SDL_HasMMX())
Aug 22, 2003
Aug 22, 2003
186
187
188
189
190
191
192
193
194
195
196
197
{
while ( h-- ) {
SDL_memcpyMMX(dst, src, w);
src += srcskip;
dst += dstskip;
}
__asm__ __volatile__ (
" emms\n"
::);
}
else
#endif
Apr 26, 2001
Apr 26, 2001
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
while ( h-- ) {
SDL_memcpy(dst, src, w);
src += srcskip;
dst += dstskip;
}
}
static void SDL_BlitCopyOverlap(SDL_BlitInfo *info)
{
Uint8 *src, *dst;
int w, h;
int srcskip, dstskip;
w = info->d_width*info->dst->BytesPerPixel;
h = info->d_height;
src = info->s_pixels;
dst = info->d_pixels;
srcskip = w+info->s_skip;
dstskip = w+info->d_skip;
if ( dst < src ) {
while ( h-- ) {
SDL_memcpy(dst, src, w);
src += srcskip;
dst += dstskip;
}
} else {
src += ((h-1) * srcskip);
dst += ((h-1) * dstskip);
while ( h-- ) {
SDL_revcpy(dst, src, w);
src -= srcskip;
dst -= dstskip;
}
}
}
/* Figure out which of many blit routines to set up on a surface */
int SDL_CalculateBlit(SDL_Surface *surface)
{
int blit_index;
/* Clean everything out to start */
if ( (surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
SDL_UnRLESurface(surface, 1);
}
surface->map->sw_blit = NULL;
/* Figure out if an accelerated hardware blit is possible */
surface->flags &= ~SDL_HWACCEL;
if ( surface->map->identity ) {
int hw_blit_ok;
if ( (surface->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
/* We only support accelerated blitting to hardware */
if ( surface->map->dst->flags & SDL_HWSURFACE ) {
hw_blit_ok = current_video->info.blit_hw;
} else {
hw_blit_ok = 0;
}
if (hw_blit_ok && (surface->flags & SDL_SRCCOLORKEY)) {
hw_blit_ok = current_video->info.blit_hw_CC;
}
if ( hw_blit_ok && (surface->flags & SDL_SRCALPHA) ) {
hw_blit_ok = current_video->info.blit_hw_A;
}
} else {
/* We only support accelerated blitting to hardware */
if ( surface->map->dst->flags & SDL_HWSURFACE ) {
hw_blit_ok = current_video->info.blit_sw;
} else {
hw_blit_ok = 0;
}
if (hw_blit_ok && (surface->flags & SDL_SRCCOLORKEY)) {
hw_blit_ok = current_video->info.blit_sw_CC;
}
if ( hw_blit_ok && (surface->flags & SDL_SRCALPHA) ) {
hw_blit_ok = current_video->info.blit_sw_A;
}
}
if ( hw_blit_ok ) {
SDL_VideoDevice *video = current_video;
SDL_VideoDevice *this = current_video;
video->CheckHWBlit(this, surface, surface->map->dst);
}
}
Apr 17, 2005
Apr 17, 2005
283
284
285
286
287
288
289
290
291
292
293
/* if an alpha pixel format is specified, we can accelerate alpha blits */
if (((surface->flags & SDL_HWSURFACE) == SDL_HWSURFACE )&&(current_video->displayformatalphapixel))
{
if ( (surface->flags & SDL_SRCALPHA) )
if ( current_video->info.blit_hw_A ) {
SDL_VideoDevice *video = current_video;
SDL_VideoDevice *this = current_video;
video->CheckHWBlit(this, surface, surface->map->dst);
}
}
Apr 26, 2001
Apr 26, 2001
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* Get the blit function index, based on surface mode */
/* { 0 = nothing, 1 = colorkey, 2 = alpha, 3 = colorkey+alpha } */
blit_index = 0;
blit_index |= (!!(surface->flags & SDL_SRCCOLORKEY)) << 0;
if ( surface->flags & SDL_SRCALPHA
&& (surface->format->alpha != SDL_ALPHA_OPAQUE
|| surface->format->Amask) ) {
blit_index |= 2;
}
/* Check for special "identity" case -- copy blit */
if ( surface->map->identity && blit_index == 0 ) {
surface->map->sw_data->blit = SDL_BlitCopy;
/* Handle overlapping blits on the same surface */
if ( surface == surface->map->dst ) {
surface->map->sw_data->blit = SDL_BlitCopyOverlap;
}
} else {
if ( surface->format->BitsPerPixel < 8 ) {
surface->map->sw_data->blit =
SDL_CalculateBlit0(surface, blit_index);
} else {
switch ( surface->format->BytesPerPixel ) {
case 1:
surface->map->sw_data->blit =
SDL_CalculateBlit1(surface, blit_index);
break;
case 2:
case 3:
case 4:
surface->map->sw_data->blit =
SDL_CalculateBlitN(surface, blit_index);
break;
default:
surface->map->sw_data->blit = NULL;
break;
}
}
}
/* Make sure we have a blit function */
if ( surface->map->sw_data->blit == NULL ) {
SDL_InvalidateMap(surface->map);
SDL_SetError("Blit combination not supported");
return(-1);
}
/* Choose software blitting function */
if(surface->flags & SDL_RLEACCELOK
&& (surface->flags & SDL_HWACCEL) != SDL_HWACCEL) {
if(surface->map->identity
&& (blit_index == 1
|| (blit_index == 3 && !surface->format->Amask))) {
if ( SDL_RLESurface(surface) == 0 )
surface->map->sw_blit = SDL_RLEBlit;
} else if(blit_index == 2 && surface->format->Amask) {
if ( SDL_RLESurface(surface) == 0 )
surface->map->sw_blit = SDL_RLEAlphaBlit;
}
}
if ( surface->map->sw_blit == NULL ) {
surface->map->sw_blit = SDL_SoftBlit;
}
return(0);
}