SDL_fillrect.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #include "SDL_surface_c.h"
  20. #ifdef SDL_SSE_INTRINSICS
  21. /* *INDENT-OFF* */ // clang-format off
  22. #if defined(_MSC_VER) && !defined(__clang__)
  23. #define SSE_BEGIN \
  24. __m128 c128; \
  25. c128.m128_u32[0] = color; \
  26. c128.m128_u32[1] = color; \
  27. c128.m128_u32[2] = color; \
  28. c128.m128_u32[3] = color;
  29. #else
  30. #define SSE_BEGIN \
  31. __m128 c128; \
  32. DECLARE_ALIGNED(Uint32, cccc[4], 16); \
  33. cccc[0] = color; \
  34. cccc[1] = color; \
  35. cccc[2] = color; \
  36. cccc[3] = color; \
  37. c128 = *(__m128 *)cccc;
  38. #endif
  39. #define SSE_WORK \
  40. for (i = n / 64; i--;) { \
  41. _mm_stream_ps((float *)(p+0), c128); \
  42. _mm_stream_ps((float *)(p+16), c128); \
  43. _mm_stream_ps((float *)(p+32), c128); \
  44. _mm_stream_ps((float *)(p+48), c128); \
  45. p += 64; \
  46. }
  47. #define SSE_END
  48. #define DEFINE_SSE_FILLRECT(bpp, type) \
  49. static void SDL_TARGETING("sse") SDL_FillSurfaceRect##bpp##SSE(Uint8 *pixels, int pitch, Uint32 color, int w, int h) \
  50. { \
  51. int i, n; \
  52. Uint8 *p = NULL; \
  53. \
  54. SSE_BEGIN; \
  55. \
  56. while (h--) { \
  57. n = (w) * (bpp); \
  58. p = pixels; \
  59. \
  60. if (n > 63) { \
  61. int adjust = 16 - ((uintptr_t)p & 15); \
  62. if (adjust < 16) { \
  63. n -= adjust; \
  64. adjust /= (bpp); \
  65. while (adjust--) { \
  66. *((type *)p) = (type)color; \
  67. p += (bpp); \
  68. } \
  69. } \
  70. SSE_WORK; \
  71. } \
  72. if (n & 63) { \
  73. int remainder = (n & 63); \
  74. remainder /= (bpp); \
  75. while (remainder--) { \
  76. *((type *)p) = (type)color; \
  77. p += (bpp); \
  78. } \
  79. } \
  80. pixels += pitch; \
  81. } \
  82. \
  83. SSE_END; \
  84. }
  85. static void SDL_TARGETING("sse") SDL_FillSurfaceRect1SSE(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
  86. {
  87. int i, n;
  88. SSE_BEGIN;
  89. while (h--) {
  90. Uint8 *p = pixels;
  91. n = w;
  92. if (n > 63) {
  93. int adjust = 16 - ((uintptr_t)p & 15);
  94. if (adjust) {
  95. n -= adjust;
  96. SDL_memset(p, color, adjust);
  97. p += adjust;
  98. }
  99. SSE_WORK;
  100. }
  101. if (n & 63) {
  102. int remainder = (n & 63);
  103. SDL_memset(p, color, remainder);
  104. }
  105. pixels += pitch;
  106. }
  107. SSE_END;
  108. }
  109. // DEFINE_SSE_FILLRECT(1, Uint8)
  110. DEFINE_SSE_FILLRECT(2, Uint16)
  111. DEFINE_SSE_FILLRECT(4, Uint32)
  112. /* *INDENT-ON* */ // clang-format on
  113. #endif // __SSE__
  114. static void SDL_FillSurfaceRect1(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
  115. {
  116. int n;
  117. Uint8 *p = NULL;
  118. while (h--) {
  119. n = w;
  120. p = pixels;
  121. if (n > 3) {
  122. switch ((uintptr_t)p & 3) {
  123. case 1:
  124. *p++ = (Uint8)color;
  125. --n;
  126. SDL_FALLTHROUGH;
  127. case 2:
  128. *p++ = (Uint8)color;
  129. --n;
  130. SDL_FALLTHROUGH;
  131. case 3:
  132. *p++ = (Uint8)color;
  133. --n;
  134. }
  135. SDL_memset4(p, color, (n >> 2));
  136. }
  137. if (n & 3) {
  138. p += (n & ~3);
  139. switch (n & 3) {
  140. case 3:
  141. *p++ = (Uint8)color;
  142. SDL_FALLTHROUGH;
  143. case 2:
  144. *p++ = (Uint8)color;
  145. SDL_FALLTHROUGH;
  146. case 1:
  147. *p++ = (Uint8)color;
  148. }
  149. }
  150. pixels += pitch;
  151. }
  152. }
  153. static void SDL_FillSurfaceRect2(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
  154. {
  155. int n;
  156. Uint16 *p = NULL;
  157. while (h--) {
  158. n = w;
  159. p = (Uint16 *)pixels;
  160. if (n > 1) {
  161. if ((uintptr_t)p & 2) {
  162. *p++ = (Uint16)color;
  163. --n;
  164. }
  165. SDL_memset4(p, color, (n >> 1));
  166. }
  167. if (n & 1) {
  168. p[n - 1] = (Uint16)color;
  169. }
  170. pixels += pitch;
  171. }
  172. }
  173. static void SDL_FillSurfaceRect3(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
  174. {
  175. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  176. Uint8 b1 = (Uint8)(color & 0xFF);
  177. Uint8 b2 = (Uint8)((color >> 8) & 0xFF);
  178. Uint8 b3 = (Uint8)((color >> 16) & 0xFF);
  179. #elif SDL_BYTEORDER == SDL_BIG_ENDIAN
  180. Uint8 b1 = (Uint8)((color >> 16) & 0xFF);
  181. Uint8 b2 = (Uint8)((color >> 8) & 0xFF);
  182. Uint8 b3 = (Uint8)(color & 0xFF);
  183. #endif
  184. int n;
  185. Uint8 *p = NULL;
  186. while (h--) {
  187. n = w;
  188. p = pixels;
  189. while (n--) {
  190. *p++ = b1;
  191. *p++ = b2;
  192. *p++ = b3;
  193. }
  194. pixels += pitch;
  195. }
  196. }
  197. static void SDL_FillSurfaceRect4(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
  198. {
  199. while (h--) {
  200. SDL_memset4(pixels, color, w);
  201. pixels += pitch;
  202. }
  203. }
  204. /*
  205. * This function performs a fast fill of the given rectangle with 'color'
  206. */
  207. bool SDL_FillSurfaceRect(SDL_Surface *dst, const SDL_Rect *rect, Uint32 color)
  208. {
  209. if (!SDL_SurfaceValid(dst)) {
  210. return SDL_InvalidParamError("SDL_FillSurfaceRect(): dst");
  211. }
  212. // If 'rect' == NULL, then fill the whole surface
  213. if (!rect) {
  214. rect = &dst->clip_rect;
  215. // Don't attempt to fill if the surface's clip_rect is empty
  216. if (SDL_RectEmpty(rect)) {
  217. return true;
  218. }
  219. }
  220. return SDL_FillSurfaceRects(dst, rect, 1, color);
  221. }
  222. bool SDL_FillSurfaceRects(SDL_Surface *dst, const SDL_Rect *rects, int count, Uint32 color)
  223. {
  224. SDL_Rect clipped;
  225. Uint8 *pixels;
  226. const SDL_Rect *rect;
  227. void (*fill_function)(Uint8 * pixels, int pitch, Uint32 color, int w, int h) = NULL;
  228. int i;
  229. if (!SDL_SurfaceValid(dst)) {
  230. return SDL_InvalidParamError("SDL_FillSurfaceRects(): dst");
  231. }
  232. // Nothing to do
  233. if (dst->w == 0 || dst->h == 0) {
  234. return true;
  235. }
  236. // Perform software fill
  237. if (!dst->pixels) {
  238. return SDL_SetError("SDL_FillSurfaceRects(): You must lock the surface");
  239. }
  240. if (!rects) {
  241. return SDL_InvalidParamError("SDL_FillSurfaceRects(): rects");
  242. }
  243. /* This function doesn't usually work on surfaces < 8 bpp
  244. * Except: support for 4bits, when filling full size.
  245. */
  246. if (SDL_BITSPERPIXEL(dst->format) < 8) {
  247. if (count == 1) {
  248. const SDL_Rect *r = &rects[0];
  249. if (r->x == 0 && r->y == 0 && r->w == dst->w && r->h == dst->h) {
  250. if (SDL_BITSPERPIXEL(dst->format) == 4) {
  251. Uint8 b = (((Uint8)color << 4) | (Uint8)color);
  252. SDL_memset(dst->pixels, b, (size_t)dst->h * dst->pitch);
  253. return true;
  254. }
  255. }
  256. }
  257. return SDL_SetError("SDL_FillSurfaceRects(): Unsupported surface format");
  258. }
  259. if (fill_function == NULL) {
  260. switch (SDL_BYTESPERPIXEL(dst->format)) {
  261. case 1:
  262. {
  263. color |= (color << 8);
  264. color |= (color << 16);
  265. #ifdef SDL_SSE_INTRINSICS
  266. if (SDL_HasSSE()) {
  267. fill_function = SDL_FillSurfaceRect1SSE;
  268. break;
  269. }
  270. #endif
  271. fill_function = SDL_FillSurfaceRect1;
  272. break;
  273. }
  274. case 2:
  275. {
  276. color |= (color << 16);
  277. #ifdef SDL_SSE_INTRINSICS
  278. if (SDL_HasSSE()) {
  279. fill_function = SDL_FillSurfaceRect2SSE;
  280. break;
  281. }
  282. #endif
  283. fill_function = SDL_FillSurfaceRect2;
  284. break;
  285. }
  286. case 3:
  287. // 24-bit RGB is a slow path, at least for now.
  288. {
  289. fill_function = SDL_FillSurfaceRect3;
  290. break;
  291. }
  292. case 4:
  293. {
  294. #ifdef SDL_SSE_INTRINSICS
  295. if (SDL_HasSSE()) {
  296. fill_function = SDL_FillSurfaceRect4SSE;
  297. break;
  298. }
  299. #endif
  300. fill_function = SDL_FillSurfaceRect4;
  301. break;
  302. }
  303. default:
  304. return SDL_SetError("Unsupported pixel format");
  305. }
  306. }
  307. for (i = 0; i < count; ++i) {
  308. rect = &rects[i];
  309. // Perform clipping
  310. if (!SDL_GetRectIntersection(rect, &dst->clip_rect, &clipped)) {
  311. continue;
  312. }
  313. rect = &clipped;
  314. pixels = (Uint8 *)dst->pixels + rect->y * dst->pitch +
  315. rect->x * SDL_BYTESPERPIXEL(dst->format);
  316. fill_function(pixels, dst->pitch, color, rect->w, rect->h);
  317. }
  318. // We're done!
  319. return true;
  320. }