SDL  2.0
SDL_blit.c File Reference
#include "../SDL_internal.h"
#include "SDL_video.h"
#include "SDL_sysvideo.h"
#include "SDL_blit.h"
#include "SDL_blit_auto.h"
#include "SDL_blit_copy.h"
#include "SDL_blit_slow.h"
#include "SDL_RLEaccel_c.h"
#include "SDL_pixels_c.h"
+ Include dependency graph for SDL_blit.c:

Go to the source code of this file.

Functions

static int SDL_SoftBlit (SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
 
static SDL_bool SDL_UseAltivecPrefetch ()
 
static SDL_BlitFunc SDL_ChooseBlitFunc (Uint32 src_format, Uint32 dst_format, int flags, SDL_BlitFuncEntry *entries)
 
int SDL_CalculateBlit (SDL_Surface *surface)
 

Function Documentation

◆ SDL_CalculateBlit()

int SDL_CalculateBlit ( SDL_Surface surface)

Definition at line 196 of file SDL_blit.c.

197 {
198  SDL_BlitFunc blit = NULL;
199  SDL_BlitMap *map = surface->map;
200  SDL_Surface *dst = map->dst;
201 
202  /* We don't currently support blitting to < 8 bpp surfaces */
203  if (dst->format->BitsPerPixel < 8) {
205  return SDL_SetError("Blit combination not supported");
206  }
207 
208 #if SDL_HAVE_RLE
209  /* Clean everything out to start */
210  if ((surface->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
212  }
213 #endif
214 
215  map->blit = SDL_SoftBlit;
216  map->info.src_fmt = surface->format;
217  map->info.src_pitch = surface->pitch;
218  map->info.dst_fmt = dst->format;
219  map->info.dst_pitch = dst->pitch;
220 
221 #if SDL_HAVE_RLE
222  /* See if we can do RLE acceleration */
223  if (map->info.flags & SDL_COPY_RLE_DESIRED) {
224  if (SDL_RLESurface(surface) == 0) {
225  return 0;
226  }
227  }
228 #endif
229 
230  /* Choose a standard blit function */
231  if (map->identity && !(map->info.flags & ~SDL_COPY_RLE_DESIRED)) {
232  blit = SDL_BlitCopy;
233  } else if (surface->format->Rloss > 8 || dst->format->Rloss > 8) {
234  /* Greater than 8 bits per channel not supported yet */
236  return SDL_SetError("Blit combination not supported");
237  }
238 #if SDL_HAVE_BLIT_0
239  else if (surface->format->BitsPerPixel < 8 &&
240  SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
241  blit = SDL_CalculateBlit0(surface);
242  }
243 #endif
244 #if SDL_HAVE_BLIT_1
245  else if (surface->format->BytesPerPixel == 1 &&
246  SDL_ISPIXELFORMAT_INDEXED(surface->format->format)) {
247  blit = SDL_CalculateBlit1(surface);
248  }
249 #endif
250 #if SDL_HAVE_BLIT_A
251  else if (map->info.flags & SDL_COPY_BLEND) {
252  blit = SDL_CalculateBlitA(surface);
253  }
254 #endif
255 #if SDL_HAVE_BLIT_N
256  else {
257  blit = SDL_CalculateBlitN(surface);
258  }
259 #endif
260 #if SDL_HAVE_BLIT_AUTO
261  if (blit == NULL) {
262  Uint32 src_format = surface->format->format;
263  Uint32 dst_format = dst->format->format;
264 
265  blit =
266  SDL_ChooseBlitFunc(src_format, dst_format, map->info.flags,
268  }
269 #endif
270 
271 #ifndef TEST_SLOW_BLIT
272  if (blit == NULL)
273 #endif
274  {
275  Uint32 src_format = surface->format->format;
276  Uint32 dst_format = dst->format->format;
277 
278  if (!SDL_ISPIXELFORMAT_INDEXED(src_format) &&
279  !SDL_ISPIXELFORMAT_FOURCC(src_format) &&
280  !SDL_ISPIXELFORMAT_INDEXED(dst_format) &&
281  !SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
282  blit = SDL_Blit_Slow;
283  }
284  }
285  map->data = blit;
286 
287  /* Make sure we have a blit function */
288  if (blit == NULL) {
290  return SDL_SetError("Blit combination not supported");
291  }
292 
293  return 0;
294 }
void SDL_UnRLESurface(SDL_Surface *surface, int recode)
int SDL_RLESurface(SDL_Surface *surface)
static SDL_BlitFunc SDL_ChooseBlitFunc(Uint32 src_format, Uint32 dst_format, int flags, SDL_BlitFuncEntry *entries)
Definition: SDL_blit.c:130
static int SDL_SoftBlit(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect)
Definition: SDL_blit.c:34
SDL_BlitFunc SDL_CalculateBlitA(SDL_Surface *surface)
Definition: SDL_blit_A.c:1335
SDL_BlitFunc SDL_CalculateBlitN(SDL_Surface *surface)
Definition: SDL_blit_N.c:3359
SDL_BlitFunc SDL_CalculateBlit1(SDL_Surface *surface)
Definition: SDL_blit_1.c:524
#define SDL_COPY_RLE_DESIRED
Definition: SDL_blit.h:42
void(* SDL_BlitFunc)(SDL_BlitInfo *info)
Definition: SDL_blit.h:74
SDL_BlitFunc SDL_CalculateBlit0(SDL_Surface *surface)
Definition: SDL_blit_0.c:456
#define SDL_COPY_BLEND
Definition: SDL_blit.h:36
SDL_BlitFuncEntry SDL_GeneratedBlitFuncTable[]
void SDL_BlitCopy(SDL_BlitInfo *info)
Definition: SDL_blit_copy.c:91
void SDL_Blit_Slow(SDL_BlitInfo *info)
Definition: SDL_blit_slow.c:31
#define SDL_SetError
const GLubyte GLuint GLuint GLuint GLuint alpha GLboolean GLboolean GLboolean GLboolean alpha GLint GLint GLsizei GLsizei GLenum type GLenum GLint GLenum GLint GLint GLsizei GLsizei GLint border GLenum GLint GLint GLint GLint GLint GLsizei GLsizei height GLsizei GLsizei GLenum GLenum const GLvoid *pixels GLenum GLint GLint GLint GLint j2 GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble zFar GLenum GLenum GLint *params GLenum GLenum GLint *params GLenum GLenum GLint *params GLenum GLenum GLfloat *params GLenum GLint GLenum GLenum GLvoid *pixels GLenum GLint GLenum GLint *params GLenum GLenum GLint *params GLenum GLsizei const GLvoid *pointer GLenum GLenum const GLint *params GLenum GLfloat GLfloat GLint GLint const GLfloat *points GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat *points GLint GLfloat GLfloat GLint GLfloat GLfloat v2 GLenum GLenum const GLint *params GLdouble GLdouble GLdouble GLdouble GLdouble GLdouble zFar GLenum map
Definition: SDL_glfuncs.h:291
GLenum GLenum dst
void SDL_InvalidateMap(SDL_BlitMap *map)
Definition: SDL_pixels.c:1083
#define SDL_ISPIXELFORMAT_INDEXED(format)
Definition: SDL_pixels.h:134
#define SDL_ISPIXELFORMAT_FOURCC(format)
Definition: SDL_pixels.h:167
uint32_t Uint32
Definition: SDL_stdinc.h:209
#define SDL_RLEACCEL
Definition: SDL_surface.h:54
#define NULL
Definition: begin_code.h:163
EGLSurface surface
Definition: eglext.h:248
A collection of pixels used in software blitting.
Definition: SDL_surface.h:71

References map, NULL, SDL_Blit_Slow(), SDL_BlitCopy(), SDL_CalculateBlit0(), SDL_CalculateBlit1(), SDL_CalculateBlitA(), SDL_CalculateBlitN(), SDL_ChooseBlitFunc(), SDL_COPY_BLEND, SDL_COPY_RLE_DESIRED, SDL_GeneratedBlitFuncTable, SDL_InvalidateMap(), SDL_ISPIXELFORMAT_FOURCC, SDL_ISPIXELFORMAT_INDEXED, SDL_RLEACCEL, SDL_RLESurface(), SDL_SetError, SDL_SoftBlit(), and SDL_UnRLESurface().

Referenced by SDL_MapSurface().

◆ SDL_ChooseBlitFunc()

static SDL_BlitFunc SDL_ChooseBlitFunc ( Uint32  src_format,
Uint32  dst_format,
int  flags,
SDL_BlitFuncEntry entries 
)
static

Definition at line 130 of file SDL_blit.c.

132 {
134  static int features = 0x7fffffff;
135 
136  /* Get the available CPU features */
137  if (features == 0x7fffffff) {
138  const char *override = SDL_getenv("SDL_BLIT_CPU_FEATURES");
139 
140  features = SDL_CPU_ANY;
141 
142  /* Allow an override for testing .. */
143  if (override) {
144  SDL_sscanf(override, "%u", &features);
145  } else {
146  if (SDL_HasMMX()) {
147  features |= SDL_CPU_MMX;
148  }
149  if (SDL_Has3DNow()) {
150  features |= SDL_CPU_3DNOW;
151  }
152  if (SDL_HasSSE()) {
153  features |= SDL_CPU_SSE;
154  }
155  if (SDL_HasSSE2()) {
156  features |= SDL_CPU_SSE2;
157  }
158  if (SDL_HasAltiVec()) {
159  if (SDL_UseAltivecPrefetch()) {
160  features |= SDL_CPU_ALTIVEC_PREFETCH;
161  } else {
162  features |= SDL_CPU_ALTIVEC_NOPREFETCH;
163  }
164  }
165  }
166  }
167 
168  for (i = 0; entries[i].func; ++i) {
169  /* Check for matching pixel formats */
170  if (src_format != entries[i].src_format) {
171  continue;
172  }
173  if (dst_format != entries[i].dst_format) {
174  continue;
175  }
176 
177  /* Check flags */
178  if ((flagcheck & entries[i].flags) != flagcheck) {
179  continue;
180  }
181 
182  /* Check CPU features */
183  if ((entries[i].cpu & features) != entries[i].cpu) {
184  continue;
185  }
186 
187  /* We found the best one! */
188  return entries[i].func;
189  }
190  return NULL;
191 }
static SDL_bool SDL_UseAltivecPrefetch()
Definition: SDL_blit.c:122
#define SDL_CPU_ALTIVEC_PREFETCH
Definition: SDL_blit.h:53
#define SDL_CPU_3DNOW
Definition: SDL_blit.h:50
#define SDL_CPU_SSE2
Definition: SDL_blit.h:52
#define SDL_COPY_MUL
Definition: SDL_blit.h:39
#define SDL_COPY_NEAREST
Definition: SDL_blit.h:41
#define SDL_COPY_MODULATE_COLOR
Definition: SDL_blit.h:34
#define SDL_COPY_ADD
Definition: SDL_blit.h:37
#define SDL_COPY_COLORKEY
Definition: SDL_blit.h:40
#define SDL_COPY_MODULATE_ALPHA
Definition: SDL_blit.h:35
#define SDL_CPU_ANY
Definition: SDL_blit.h:48
#define SDL_CPU_SSE
Definition: SDL_blit.h:51
#define SDL_COPY_MOD
Definition: SDL_blit.h:38
#define SDL_CPU_ALTIVEC_NOPREFETCH
Definition: SDL_blit.h:54
#define SDL_CPU_MMX
Definition: SDL_blit.h:49
#define SDL_HasMMX
#define SDL_Has3DNow
#define SDL_HasSSE
#define SDL_HasAltiVec
#define SDL_getenv
#define SDL_sscanf
#define SDL_HasSSE2
GLbitfield flags
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
SDL_BlitFunc func
Definition: SDL_blit.h:83

References SDL_BlitFuncEntry::func, i, NULL, SDL_COPY_ADD, SDL_COPY_BLEND, SDL_COPY_COLORKEY, SDL_COPY_MOD, SDL_COPY_MODULATE_ALPHA, SDL_COPY_MODULATE_COLOR, SDL_COPY_MUL, SDL_COPY_NEAREST, SDL_CPU_3DNOW, SDL_CPU_ALTIVEC_NOPREFETCH, SDL_CPU_ALTIVEC_PREFETCH, SDL_CPU_ANY, SDL_CPU_MMX, SDL_CPU_SSE, SDL_CPU_SSE2, SDL_getenv, SDL_Has3DNow, SDL_HasAltiVec, SDL_HasMMX, SDL_HasSSE, SDL_HasSSE2, SDL_sscanf, and SDL_UseAltivecPrefetch().

Referenced by SDL_CalculateBlit().

◆ SDL_SoftBlit()

static int SDL_SoftBlit ( SDL_Surface src,
SDL_Rect srcrect,
SDL_Surface dst,
SDL_Rect dstrect 
)
static

Definition at line 34 of file SDL_blit.c.

36 {
37  int okay;
38  int src_locked;
39  int dst_locked;
40 
41  /* Everything is okay at the beginning... */
42  okay = 1;
43 
44  /* Lock the destination if it's in hardware */
45  dst_locked = 0;
46  if (SDL_MUSTLOCK(dst)) {
47  if (SDL_LockSurface(dst) < 0) {
48  okay = 0;
49  } else {
50  dst_locked = 1;
51  }
52  }
53  /* Lock the source if it's in hardware */
54  src_locked = 0;
55  if (SDL_MUSTLOCK(src)) {
56  if (SDL_LockSurface(src) < 0) {
57  okay = 0;
58  } else {
59  src_locked = 1;
60  }
61  }
62 
63  /* Set up source and destination buffer pointers, and BLIT! */
64  if (okay && !SDL_RectEmpty(srcrect)) {
65  SDL_BlitFunc RunBlit;
66  SDL_BlitInfo *info = &src->map->info;
67 
68  /* Set up the blit information */
69  info->src = (Uint8 *) src->pixels +
70  (Uint16) srcrect->y * src->pitch +
71  (Uint16) srcrect->x * info->src_fmt->BytesPerPixel;
72  info->src_w = srcrect->w;
73  info->src_h = srcrect->h;
74  info->src_pitch = src->pitch;
75  info->src_skip =
76  info->src_pitch - info->src_w * info->src_fmt->BytesPerPixel;
77  info->dst =
78  (Uint8 *) dst->pixels + (Uint16) dstrect->y * dst->pitch +
79  (Uint16) dstrect->x * info->dst_fmt->BytesPerPixel;
80  info->dst_w = dstrect->w;
81  info->dst_h = dstrect->h;
82  info->dst_pitch = dst->pitch;
83  info->dst_skip =
84  info->dst_pitch - info->dst_w * info->dst_fmt->BytesPerPixel;
85  RunBlit = (SDL_BlitFunc) src->map->data;
86 
87  /* Run the actual software blit */
88  RunBlit(info);
89  }
90 
91  /* We need to unlock the surfaces if they're locked */
92  if (dst_locked) {
94  }
95  if (src_locked) {
97  }
98  /* Blit is done! */
99  return (okay ? 0 : -1);
100 }
#define SDL_UnlockSurface
#define SDL_LockSurface
GLenum src
SDL_FORCE_INLINE SDL_bool SDL_RectEmpty(const SDL_Rect *r)
Returns true if the rectangle has no area.
Definition: SDL_rect.h:108
uint16_t Uint16
Definition: SDL_stdinc.h:197
uint8_t Uint8
Definition: SDL_stdinc.h:185
#define SDL_MUSTLOCK(S)
Definition: SDL_surface.h:62
SDL_PixelFormat * src_fmt
Definition: SDL_blit.h:66
int dst_skip
Definition: SDL_blit.h:65
SDL_PixelFormat * dst_fmt
Definition: SDL_blit.h:67
int src_pitch
Definition: SDL_blit.h:60
int dst_pitch
Definition: SDL_blit.h:64
int src_skip
Definition: SDL_blit.h:61
Uint8 * src
Definition: SDL_blit.h:58
Uint8 * dst
Definition: SDL_blit.h:62
Uint8 BytesPerPixel
Definition: SDL_pixels.h:329
int h
Definition: SDL_rect.h:80
int w
Definition: SDL_rect.h:80
int y
Definition: SDL_rect.h:79
int x
Definition: SDL_rect.h:79

References SDL_PixelFormat::BytesPerPixel, SDL_BlitInfo::dst, SDL_BlitInfo::dst_fmt, SDL_BlitInfo::dst_h, SDL_BlitInfo::dst_pitch, SDL_BlitInfo::dst_skip, SDL_BlitInfo::dst_w, SDL_Rect::h, SDL_LockSurface, SDL_MUSTLOCK, SDL_RectEmpty(), SDL_UnlockSurface, SDL_BlitInfo::src, SDL_BlitInfo::src_fmt, SDL_BlitInfo::src_h, SDL_BlitInfo::src_pitch, SDL_BlitInfo::src_skip, SDL_BlitInfo::src_w, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by SDL_CalculateBlit().

◆ SDL_UseAltivecPrefetch()

static SDL_bool SDL_UseAltivecPrefetch ( )
static

Definition at line 122 of file SDL_blit.c.

123 {
124  /* Just guess G4 */
125  return SDL_TRUE;
126 }
@ SDL_TRUE
Definition: SDL_stdinc.h:170

References SDL_TRUE.

Referenced by SDL_ChooseBlitFunc().