SDL_fillrect.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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_blit.h"
  20. #if HAVE_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. int SDL_FillSurfaceRect(SDL_Surface *dst, const SDL_Rect *rect, Uint32 color)
  208. {
  209. if (dst == NULL) {
  210. return SDL_InvalidParamError("SDL_FillSurfaceRect(): dst");
  211. }
  212. /* If 'rect' == NULL, then fill the whole surface */
  213. if (rect == NULL) {
  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 0;
  218. }
  219. }
  220. return SDL_FillSurfaceRects(dst, rect, 1, color);
  221. }
  222. #if SDL_ARM_NEON_BLITTERS
  223. void FillSurfaceRect8ARMNEONAsm(int32_t w, int32_t h, uint8_t *dst, int32_t dst_stride, uint8_t src);
  224. void FillSurfaceRect16ARMNEONAsm(int32_t w, int32_t h, uint16_t *dst, int32_t dst_stride, uint16_t src);
  225. void FillSurfaceRect32ARMNEONAsm(int32_t w, int32_t h, uint32_t *dst, int32_t dst_stride, uint32_t src);
  226. static void fill_8_neon(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
  227. {
  228. FillSurfaceRect8ARMNEONAsm(w, h, (uint8_t *)pixels, pitch >> 0, color);
  229. return;
  230. }
  231. static void fill_16_neon(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
  232. {
  233. FillSurfaceRect16ARMNEONAsm(w, h, (uint16_t *)pixels, pitch >> 1, color);
  234. return;
  235. }
  236. static void fill_32_neon(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
  237. {
  238. FillSurfaceRect32ARMNEONAsm(w, h, (uint32_t *)pixels, pitch >> 2, color);
  239. return;
  240. }
  241. #endif
  242. #if SDL_ARM_SIMD_BLITTERS
  243. void FillSurfaceRect8ARMSIMDAsm(int32_t w, int32_t h, uint8_t *dst, int32_t dst_stride, uint8_t src);
  244. void FillSurfaceRect16ARMSIMDAsm(int32_t w, int32_t h, uint16_t *dst, int32_t dst_stride, uint16_t src);
  245. void FillSurfaceRect32ARMSIMDAsm(int32_t w, int32_t h, uint32_t *dst, int32_t dst_stride, uint32_t src);
  246. static void fill_8_simd(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
  247. {
  248. FillSurfaceRect8ARMSIMDAsm(w, h, (uint8_t *)pixels, pitch >> 0, color);
  249. return;
  250. }
  251. static void fill_16_simd(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
  252. {
  253. FillSurfaceRect16ARMSIMDAsm(w, h, (uint16_t *)pixels, pitch >> 1, color);
  254. return;
  255. }
  256. static void fill_32_simd(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
  257. {
  258. FillSurfaceRect32ARMSIMDAsm(w, h, (uint32_t *)pixels, pitch >> 2, color);
  259. return;
  260. }
  261. #endif
  262. int SDL_FillSurfaceRects(SDL_Surface *dst, const SDL_Rect *rects, int count,
  263. Uint32 color)
  264. {
  265. SDL_Rect clipped;
  266. Uint8 *pixels;
  267. const SDL_Rect *rect;
  268. void (*fill_function)(Uint8 * pixels, int pitch, Uint32 color, int w, int h) = NULL;
  269. int i;
  270. if (dst == NULL) {
  271. return SDL_InvalidParamError("SDL_FillSurfaceRects(): dst");
  272. }
  273. /* Nothing to do */
  274. if (dst->w == 0 || dst->h == 0) {
  275. return 0;
  276. }
  277. /* Perform software fill */
  278. if (!dst->pixels) {
  279. return SDL_SetError("SDL_FillSurfaceRects(): You must lock the surface");
  280. }
  281. if (rects == NULL) {
  282. return SDL_InvalidParamError("SDL_FillSurfaceRects(): rects");
  283. }
  284. /* This function doesn't usually work on surfaces < 8 bpp
  285. * Except: support for 4bits, when filling full size.
  286. */
  287. if (dst->format->BitsPerPixel < 8) {
  288. if (count == 1) {
  289. const SDL_Rect *r = &rects[0];
  290. if (r->x == 0 && r->y == 0 && r->w == dst->w && r->h == dst->h) {
  291. if (dst->format->BitsPerPixel == 4) {
  292. Uint8 b = (((Uint8)color << 4) | (Uint8)color);
  293. SDL_memset(dst->pixels, b, (size_t)dst->h * dst->pitch);
  294. return 1;
  295. }
  296. }
  297. }
  298. return SDL_SetError("SDL_FillSurfaceRects(): Unsupported surface format");
  299. }
  300. #if SDL_ARM_NEON_BLITTERS
  301. if (SDL_HasNEON() && dst->format->BytesPerPixel != 3 && fill_function == NULL) {
  302. switch (dst->format->BytesPerPixel) {
  303. case 1:
  304. fill_function = fill_8_neon;
  305. break;
  306. case 2:
  307. fill_function = fill_16_neon;
  308. break;
  309. case 4:
  310. fill_function = fill_32_neon;
  311. break;
  312. }
  313. }
  314. #endif
  315. #if SDL_ARM_SIMD_BLITTERS
  316. if (SDL_HasARMSIMD() && dst->format->BytesPerPixel != 3 && fill_function == NULL) {
  317. switch (dst->format->BytesPerPixel) {
  318. case 1:
  319. fill_function = fill_8_simd;
  320. break;
  321. case 2:
  322. fill_function = fill_16_simd;
  323. break;
  324. case 4:
  325. fill_function = fill_32_simd;
  326. break;
  327. }
  328. }
  329. #endif
  330. if (fill_function == NULL) {
  331. switch (dst->format->BytesPerPixel) {
  332. case 1:
  333. {
  334. color |= (color << 8);
  335. color |= (color << 16);
  336. #if HAVE_SSE_INTRINSICS
  337. if (SDL_HasSSE()) {
  338. fill_function = SDL_FillSurfaceRect1SSE;
  339. break;
  340. }
  341. #endif
  342. fill_function = SDL_FillSurfaceRect1;
  343. break;
  344. }
  345. case 2:
  346. {
  347. color |= (color << 16);
  348. #if HAVE_SSE_INTRINSICS
  349. if (SDL_HasSSE()) {
  350. fill_function = SDL_FillSurfaceRect2SSE;
  351. break;
  352. }
  353. #endif
  354. fill_function = SDL_FillSurfaceRect2;
  355. break;
  356. }
  357. case 3:
  358. /* 24-bit RGB is a slow path, at least for now. */
  359. {
  360. fill_function = SDL_FillSurfaceRect3;
  361. break;
  362. }
  363. case 4:
  364. {
  365. #if HAVE_SSE_INTRINSICS
  366. if (SDL_HasSSE()) {
  367. fill_function = SDL_FillSurfaceRect4SSE;
  368. break;
  369. }
  370. #endif
  371. fill_function = SDL_FillSurfaceRect4;
  372. break;
  373. }
  374. default:
  375. return SDL_SetError("Unsupported pixel format");
  376. }
  377. }
  378. for (i = 0; i < count; ++i) {
  379. rect = &rects[i];
  380. /* Perform clipping */
  381. if (!SDL_GetRectIntersection(rect, &dst->clip_rect, &clipped)) {
  382. continue;
  383. }
  384. rect = &clipped;
  385. pixels = (Uint8 *)dst->pixels + rect->y * dst->pitch +
  386. rect->x * dst->format->BytesPerPixel;
  387. fill_function(pixels, dst->pitch, color, rect->w, rect->h);
  388. }
  389. /* We're done! */
  390. return 0;
  391. }