SDL  2.0
SDL_fillrect.c File Reference
#include "../SDL_internal.h"
#include "SDL_video.h"
#include "SDL_blit.h"
#include "SDL_cpuinfo.h"
+ Include dependency graph for SDL_fillrect.c:

Go to the source code of this file.

Functions

static void SDL_FillRect1 (Uint8 *pixels, int pitch, Uint32 color, int w, int h)
 
static void SDL_FillRect2 (Uint8 *pixels, int pitch, Uint32 color, int w, int h)
 
static void SDL_FillRect3 (Uint8 *pixels, int pitch, Uint32 color, int w, int h)
 
static void SDL_FillRect4 (Uint8 *pixels, int pitch, Uint32 color, int w, int h)
 
int SDL_FillRect (SDL_Surface *dst, const SDL_Rect *rect, Uint32 color)
 
int SDL_FillRects (SDL_Surface *dst, const SDL_Rect *rects, int count, Uint32 color)
 

Function Documentation

◆ SDL_FillRect()

int SDL_FillRect ( SDL_Surface dst,
const SDL_Rect rect,
Uint32  color 
)

Performs a fast fill of the given rectangle with color.

If rect is NULL, the whole surface will be filled with color.

The color should be a pixel of the format used by the surface, and can be generated by the SDL_MapRGB() function.

Returns
0 on success, or -1 on error.

Definition at line 238 of file SDL_fillrect.c.

239 {
240  if (!dst) {
241  return SDL_SetError("Passed NULL destination surface");
242  }
243 
244  /* If 'rect' == NULL, then fill the whole surface */
245  if (!rect) {
246  rect = &dst->clip_rect;
247  /* Don't attempt to fill if the surface's clip_rect is empty */
248  if (SDL_RectEmpty(rect)) {
249  return 0;
250  }
251  }
252 
253  return SDL_FillRects(dst, rect, 1, color);
254 }

References rect, SDL_FillRects(), SDL_RectEmpty(), and SDL_SetError.

◆ SDL_FillRect1()

static void SDL_FillRect1 ( Uint8 pixels,
int  pitch,
Uint32  color,
int  w,
int  h 
)
static

Definition at line 135 of file SDL_fillrect.c.

136 {
137  int n;
138  Uint8 *p = NULL;
139 
140  while (h--) {
141  n = w;
142  p = pixels;
143 
144  if (n > 3) {
145  switch ((uintptr_t) p & 3) {
146  case 1:
147  *p++ = (Uint8) color;
148  --n; /* fallthrough */
149  case 2:
150  *p++ = (Uint8) color;
151  --n; /* fallthrough */
152  case 3:
153  *p++ = (Uint8) color;
154  --n; /* fallthrough */
155  }
156  SDL_memset4(p, color, (n >> 2));
157  }
158  if (n & 3) {
159  p += (n & ~3);
160  switch (n & 3) {
161  case 3:
162  *p++ = (Uint8) color; /* fallthrough */
163  case 2:
164  *p++ = (Uint8) color; /* fallthrough */
165  case 1:
166  *p++ = (Uint8) color; /* fallthrough */
167  }
168  }
169  pixels += pitch;
170  }
171 }

References NULL, and SDL_memset4().

Referenced by SDL_FillRects().

◆ SDL_FillRect2()

static void SDL_FillRect2 ( Uint8 pixels,
int  pitch,
Uint32  color,
int  w,
int  h 
)
static

Definition at line 174 of file SDL_fillrect.c.

175 {
176  int n;
177  Uint16 *p = NULL;
178 
179  while (h--) {
180  n = w;
181  p = (Uint16 *) pixels;
182 
183  if (n > 1) {
184  if ((uintptr_t) p & 2) {
185  *p++ = (Uint16) color;
186  --n;
187  }
188  SDL_memset4(p, color, (n >> 1));
189  }
190  if (n & 1) {
191  p[n - 1] = (Uint16) color;
192  }
193  pixels += pitch;
194  }
195 }

References NULL, and SDL_memset4().

Referenced by SDL_FillRects().

◆ SDL_FillRect3()

static void SDL_FillRect3 ( Uint8 pixels,
int  pitch,
Uint32  color,
int  w,
int  h 
)
static

Definition at line 198 of file SDL_fillrect.c.

199 {
200 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
201  Uint8 b1 = (Uint8) (color & 0xFF);
202  Uint8 b2 = (Uint8) ((color >> 8) & 0xFF);
203  Uint8 b3 = (Uint8) ((color >> 16) & 0xFF);
204 #elif SDL_BYTEORDER == SDL_BIG_ENDIAN
205  Uint8 b1 = (Uint8) ((color >> 16) & 0xFF);
206  Uint8 b2 = (Uint8) ((color >> 8) & 0xFF);
207  Uint8 b3 = (Uint8) (color & 0xFF);
208 #endif
209  int n;
210  Uint8 *p = NULL;
211 
212  while (h--) {
213  n = w;
214  p = pixels;
215 
216  while (n--) {
217  *p++ = b1;
218  *p++ = b2;
219  *p++ = b3;
220  }
221  pixels += pitch;
222  }
223 }

References NULL.

Referenced by SDL_FillRects().

◆ SDL_FillRect4()

static void SDL_FillRect4 ( Uint8 pixels,
int  pitch,
Uint32  color,
int  w,
int  h 
)
static

Definition at line 226 of file SDL_fillrect.c.

227 {
228  while (h--) {
230  pixels += pitch;
231  }
232 }

References SDL_memset4().

Referenced by SDL_FillRects().

◆ SDL_FillRects()

int SDL_FillRects ( SDL_Surface dst,
const SDL_Rect rects,
int  count,
Uint32  color 
)

Definition at line 299 of file SDL_fillrect.c.

301 {
302  SDL_Rect clipped;
303  Uint8 *pixels;
304  const SDL_Rect* rect;
305  void (*fill_function)(Uint8 * pixels, int pitch, Uint32 color, int w, int h) = NULL;
306  int i;
307 
308  if (!dst) {
309  return SDL_SetError("Passed NULL destination surface");
310  }
311 
312  /* This function doesn't work on surfaces < 8 bpp */
313  if (dst->format->BitsPerPixel < 8) {
314  return SDL_SetError("SDL_FillRect(): Unsupported surface format");
315  }
316 
317  /* Perform software fill */
318  if (!dst->pixels) {
319  return SDL_SetError("SDL_FillRect(): You must lock the surface");
320  }
321 
322  if (!rects) {
323  return SDL_SetError("SDL_FillRects() passed NULL rects");
324  }
325 
326 #if SDL_ARM_NEON_BLITTERS
327  if (SDL_HasNEON() && dst->format->BytesPerPixel != 3 && fill_function == NULL) {
328  switch (dst->format->BytesPerPixel) {
329  case 1:
330  fill_function = fill_8_neon;
331  break;
332  case 2:
333  fill_function = fill_16_neon;
334  break;
335  case 4:
336  fill_function = fill_32_neon;
337  break;
338  }
339  }
340 #endif
341 #if SDL_ARM_SIMD_BLITTERS
342  if (SDL_HasARMSIMD() && dst->format->BytesPerPixel != 3 && fill_function == NULL) {
343  switch (dst->format->BytesPerPixel) {
344  case 1:
345  fill_function = fill_8_simd;
346  break;
347  case 2:
348  fill_function = fill_16_simd;
349  break;
350  case 4:
351  fill_function = fill_32_simd;
352  break;
353  }
354  }
355 #endif
356 
357  if (fill_function == NULL) {
358  switch (dst->format->BytesPerPixel) {
359  case 1:
360  {
361  color |= (color << 8);
362  color |= (color << 16);
363 #ifdef __SSE__
364  if (SDL_HasSSE()) {
365  fill_function = SDL_FillRect1SSE;
366  break;
367  }
368 #endif
369  fill_function = SDL_FillRect1;
370  break;
371  }
372 
373  case 2:
374  {
375  color |= (color << 16);
376 #ifdef __SSE__
377  if (SDL_HasSSE()) {
378  fill_function = SDL_FillRect2SSE;
379  break;
380  }
381 #endif
382  fill_function = SDL_FillRect2;
383  break;
384  }
385 
386  case 3:
387  /* 24-bit RGB is a slow path, at least for now. */
388  {
389  fill_function = SDL_FillRect3;
390  break;
391  }
392 
393  case 4:
394  {
395 #ifdef __SSE__
396  if (SDL_HasSSE()) {
397  fill_function = SDL_FillRect4SSE;
398  break;
399  }
400 #endif
401  fill_function = SDL_FillRect4;
402  break;
403  }
404 
405  default:
406  return SDL_SetError("Unsupported pixel format");
407  }
408  }
409 
410  for (i = 0; i < count; ++i) {
411  rect = &rects[i];
412  /* Perform clipping */
413  if (!SDL_IntersectRect(rect, &dst->clip_rect, &clipped)) {
414  continue;
415  }
416  rect = &clipped;
417 
418  pixels = (Uint8 *) dst->pixels + rect->y * dst->pitch +
419  rect->x * dst->format->BytesPerPixel;
420 
421  fill_function(pixels, dst->pitch, color, rect->w, rect->h);
422  }
423 
424  /* We're done! */
425  return 0;
426 }

References SDL_Rect::h, i, NULL, rect, SDL_FillRect1(), SDL_FillRect2(), SDL_FillRect3(), SDL_FillRect4(), SDL_HasARMSIMD, SDL_HasNEON, SDL_HasSSE, SDL_IntersectRect, SDL_SetError, void, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by SDL_FillRect().

Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
NULL
#define NULL
Definition: begin_code.h:167
count
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1571
SDL_FillRect4
static void SDL_FillRect4(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:226
SDL_IntersectRect
#define SDL_IntersectRect
Definition: SDL_dynapi_overrides.h:294
SDL_RectEmpty
SDL_FORCE_INLINE SDL_bool SDL_RectEmpty(const SDL_Rect *r)
Returns true if the rectangle has no area.
Definition: SDL_rect.h:108
h
GLfloat GLfloat GLfloat GLfloat h
Definition: SDL_opengl_glext.h:1949
SDL_Rect::x
int x
Definition: SDL_rect.h:79
SDL_Rect::w
int w
Definition: SDL_rect.h:80
n
GLdouble n
Definition: SDL_opengl_glext.h:1955
dst
GLenum GLenum dst
Definition: SDL_opengl_glext.h:1740
p
GLfloat GLfloat p
Definition: SDL_opengl_glext.h:11093
SDL_FillRect1
static void SDL_FillRect1(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:135
SDL_FillRect2
static void SDL_FillRect2(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:174
color
GLuint color
Definition: SDL_opengl_glext.h:1151
SDL_Rect::y
int y
Definition: SDL_rect.h:79
SDL_Rect::h
int h
Definition: SDL_rect.h:80
rect
SDL_Rect rect
Definition: testrelative.c:27
Uint16
uint16_t Uint16
Definition: SDL_stdinc.h:191
SDL_HasNEON
#define SDL_HasNEON
Definition: SDL_dynapi_overrides.h:618
pixels
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1572
SDL_memset4
SDL_FORCE_INLINE void SDL_memset4(void *dst, Uint32 val, size_t dwords)
Definition: SDL_stdinc.h:423
SDL_FillRect3
static void SDL_FillRect3(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:198
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30
SDL_Rect
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:77
SDL_HasARMSIMD
#define SDL_HasARMSIMD
Definition: SDL_dynapi_overrides.h:730
SDL_HasSSE
#define SDL_HasSSE
Definition: SDL_dynapi_overrides.h:107
void
const SDL_PRINTF_FORMAT_STRING char int const SDL_PRINTF_FORMAT_STRING char int const SDL_PRINTF_FORMAT_STRING char int const SDL_PRINTF_FORMAT_STRING char const char const SDL_SCANF_FORMAT_STRING char return SDL_ThreadFunction const char void return Uint32 return Uint32 void
Definition: SDL_dynapi_procs.h:89
rects
EGLSurface EGLint * rects
Definition: eglext.h:282
SDL_FillRects
int SDL_FillRects(SDL_Surface *dst, const SDL_Rect *rects, int count, Uint32 color)
Definition: SDL_fillrect.c:299
i
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
w
GLubyte GLubyte GLubyte GLubyte w
Definition: SDL_opengl_glext.h:734
uintptr_t
unsigned int uintptr_t
Definition: SDL_config_windows.h:70
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:179