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 }
#define SDL_SetError
int SDL_FillRects(SDL_Surface *dst, const SDL_Rect *rects, int count, Uint32 color)
Definition: SDL_fillrect.c:299
GLuint color
GLenum GLenum dst
SDL_FORCE_INLINE SDL_bool SDL_RectEmpty(const SDL_Rect *r)
Returns true if the rectangle has no area.
Definition: SDL_rect.h:108
SDL_Rect rect
Definition: testrelative.c:27

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 }
unsigned int uintptr_t
GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid * pixels
Definition: SDL_opengl.h:1572
GLfloat GLfloat p
GLdouble n
GLfloat GLfloat GLfloat GLfloat h
GLubyte GLubyte GLubyte GLubyte w
SDL_FORCE_INLINE void SDL_memset4(void *dst, Uint32 val, size_t dwords)
Definition: SDL_stdinc.h:431
uint8_t Uint8
Definition: SDL_stdinc.h:185
#define NULL
Definition: begin_code.h:163

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 }
uint16_t Uint16
Definition: SDL_stdinc.h:197

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  /* Nothing to do */
318  if (dst->w == 0 || dst->h == 0) {
319  return 0;
320  }
321 
322  /* Perform software fill */
323  if (!dst->pixels) {
324  return SDL_SetError("SDL_FillRect(): You must lock the surface");
325  }
326 
327  if (!rects) {
328  return SDL_SetError("SDL_FillRects() passed NULL rects");
329  }
330 
331 #if SDL_ARM_NEON_BLITTERS
332  if (SDL_HasNEON() && dst->format->BytesPerPixel != 3 && fill_function == NULL) {
333  switch (dst->format->BytesPerPixel) {
334  case 1:
335  fill_function = fill_8_neon;
336  break;
337  case 2:
338  fill_function = fill_16_neon;
339  break;
340  case 4:
341  fill_function = fill_32_neon;
342  break;
343  }
344  }
345 #endif
346 #if SDL_ARM_SIMD_BLITTERS
347  if (SDL_HasARMSIMD() && dst->format->BytesPerPixel != 3 && fill_function == NULL) {
348  switch (dst->format->BytesPerPixel) {
349  case 1:
350  fill_function = fill_8_simd;
351  break;
352  case 2:
353  fill_function = fill_16_simd;
354  break;
355  case 4:
356  fill_function = fill_32_simd;
357  break;
358  }
359  }
360 #endif
361 
362  if (fill_function == NULL) {
363  switch (dst->format->BytesPerPixel) {
364  case 1:
365  {
366  color |= (color << 8);
367  color |= (color << 16);
368 #ifdef __SSE__
369  if (SDL_HasSSE()) {
370  fill_function = SDL_FillRect1SSE;
371  break;
372  }
373 #endif
374  fill_function = SDL_FillRect1;
375  break;
376  }
377 
378  case 2:
379  {
380  color |= (color << 16);
381 #ifdef __SSE__
382  if (SDL_HasSSE()) {
383  fill_function = SDL_FillRect2SSE;
384  break;
385  }
386 #endif
387  fill_function = SDL_FillRect2;
388  break;
389  }
390 
391  case 3:
392  /* 24-bit RGB is a slow path, at least for now. */
393  {
394  fill_function = SDL_FillRect3;
395  break;
396  }
397 
398  case 4:
399  {
400 #ifdef __SSE__
401  if (SDL_HasSSE()) {
402  fill_function = SDL_FillRect4SSE;
403  break;
404  }
405 #endif
406  fill_function = SDL_FillRect4;
407  break;
408  }
409 
410  default:
411  return SDL_SetError("Unsupported pixel format");
412  }
413  }
414 
415  for (i = 0; i < count; ++i) {
416  rect = &rects[i];
417  /* Perform clipping */
418  if (!SDL_IntersectRect(rect, &dst->clip_rect, &clipped)) {
419  continue;
420  }
421  rect = &clipped;
422 
423  pixels = (Uint8 *) dst->pixels + rect->y * dst->pitch +
424  rect->x * dst->format->BytesPerPixel;
425 
426  fill_function(pixels, dst->pitch, color, rect->w, rect->h);
427  }
428 
429  /* We're done! */
430  return 0;
431 }
#define SDL_HasSSE
#define SDL_HasNEON
#define SDL_IntersectRect
#define SDL_HasARMSIMD
SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 void
static void SDL_FillRect2(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:174
static void SDL_FillRect3(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:198
static void SDL_FillRect1(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:135
static void SDL_FillRect4(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
Definition: SDL_fillrect.c:226
GLuint GLuint GLsizei count
Definition: SDL_opengl.h:1571
uint32_t Uint32
Definition: SDL_stdinc.h:209
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
EGLSurface EGLint * rects
Definition: eglext.h:282
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:78
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_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().