SDL_fillrect.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2016 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_video.h"
  20. #include "SDL_blit.h"
  21. #ifdef __SSE__
  22. /* *INDENT-OFF* */
  23. #ifdef _MSC_VER
  24. #define SSE_BEGIN \
  25. __m128 c128; \
  26. c128.m128_u32[0] = color; \
  27. c128.m128_u32[1] = color; \
  28. c128.m128_u32[2] = color; \
  29. c128.m128_u32[3] = color;
  30. #else
  31. #define SSE_BEGIN \
  32. __m128 c128; \
  33. DECLARE_ALIGNED(Uint32, cccc[4], 16); \
  34. cccc[0] = color; \
  35. cccc[1] = color; \
  36. cccc[2] = color; \
  37. cccc[3] = color; \
  38. c128 = *(__m128 *)cccc;
  39. #endif
  40. #define SSE_WORK \
  41. for (i = n / 64; i--;) { \
  42. _mm_stream_ps((float *)(p+0), c128); \
  43. _mm_stream_ps((float *)(p+16), c128); \
  44. _mm_stream_ps((float *)(p+32), c128); \
  45. _mm_stream_ps((float *)(p+48), c128); \
  46. p += 64; \
  47. }
  48. #define SSE_END
  49. #define DEFINE_SSE_FILLRECT(bpp, type) \
  50. static void \
  51. SDL_FillRect##bpp##SSE(Uint8 *pixels, int pitch, Uint32 color, int w, int h) \
  52. { \
  53. int i, n; \
  54. Uint8 *p = NULL; \
  55. \
  56. SSE_BEGIN; \
  57. \
  58. while (h--) { \
  59. n = w * bpp; \
  60. p = pixels; \
  61. \
  62. if (n > 63) { \
  63. int adjust = 16 - ((uintptr_t)p & 15); \
  64. if (adjust < 16) { \
  65. n -= adjust; \
  66. adjust /= bpp; \
  67. while (adjust--) { \
  68. *((type *)p) = (type)color; \
  69. p += bpp; \
  70. } \
  71. } \
  72. SSE_WORK; \
  73. } \
  74. if (n & 63) { \
  75. int remainder = (n & 63); \
  76. remainder /= bpp; \
  77. while (remainder--) { \
  78. *((type *)p) = (type)color; \
  79. p += bpp; \
  80. } \
  81. } \
  82. pixels += pitch; \
  83. } \
  84. \
  85. SSE_END; \
  86. }
  87. static void
  88. SDL_FillRect1SSE(Uint8 *pixels, int pitch, Uint32 color, int w, int h)
  89. {
  90. int i, n;
  91. SSE_BEGIN;
  92. while (h--) {
  93. Uint8 *p = pixels;
  94. n = w;
  95. if (n > 63) {
  96. int adjust = 16 - ((uintptr_t)p & 15);
  97. if (adjust) {
  98. n -= adjust;
  99. SDL_memset(p, color, adjust);
  100. p += adjust;
  101. }
  102. SSE_WORK;
  103. }
  104. if (n & 63) {
  105. int remainder = (n & 63);
  106. SDL_memset(p, color, remainder);
  107. }
  108. pixels += pitch;
  109. }
  110. SSE_END;
  111. }
  112. /* DEFINE_SSE_FILLRECT(1, Uint8) */
  113. DEFINE_SSE_FILLRECT(2, Uint16)
  114. DEFINE_SSE_FILLRECT(4, Uint32)
  115. /* *INDENT-ON* */
  116. #endif /* __SSE__ */
  117. static void
  118. SDL_FillRect1(Uint8 * pixels, int pitch, Uint32 color, int w, int h)
  119. {
  120. int n;
  121. Uint8 *p = NULL;
  122. while (h--) {
  123. n = w;
  124. p = pixels;
  125. if (n > 3) {
  126. switch ((uintptr_t) p & 3) {
  127. case 1:
  128. *p++ = (Uint8) color;
  129. --n;
  130. case 2:
  131. *p++ = (Uint8) color;
  132. --n;
  133. case 3:
  134. *p++ = (Uint8) color;
  135. --n;
  136. }
  137. SDL_memset4(p, color, (n >> 2));
  138. }
  139. if (n & 3) {
  140. p += (n & ~3);
  141. switch (n & 3) {
  142. case 3:
  143. *p++ = (Uint8) color;
  144. case 2:
  145. *p++ = (Uint8) color;
  146. case 1:
  147. *p++ = (Uint8) color;
  148. }
  149. }
  150. pixels += pitch;
  151. }
  152. }
  153. static void
  154. SDL_FillRect2(Uint8 * pixels, int pitch, Uint32 color, int w, int h)
  155. {
  156. int n;
  157. Uint16 *p = NULL;
  158. while (h--) {
  159. n = w;
  160. p = (Uint16 *) pixels;
  161. if (n > 1) {
  162. if ((uintptr_t) p & 2) {
  163. *p++ = (Uint16) color;
  164. --n;
  165. }
  166. SDL_memset4(p, color, (n >> 1));
  167. }
  168. if (n & 1) {
  169. p[n - 1] = (Uint16) color;
  170. }
  171. pixels += pitch;
  172. }
  173. }
  174. static void
  175. SDL_FillRect3(Uint8 * pixels, int pitch, Uint32 color, int w, int h)
  176. {
  177. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  178. Uint8 b1 = (Uint8) (color & 0xFF);
  179. Uint8 b2 = (Uint8) ((color >> 8) & 0xFF);
  180. Uint8 b3 = (Uint8) ((color >> 16) & 0xFF);
  181. #elif SDL_BYTEORDER == SDL_BIG_ENDIAN
  182. Uint8 b1 = (Uint8) ((color >> 16) & 0xFF);
  183. Uint8 b2 = (Uint8) ((color >> 8) & 0xFF);
  184. Uint8 b3 = (Uint8) (color & 0xFF);
  185. #endif
  186. int n;
  187. Uint8 *p = NULL;
  188. while (h--) {
  189. n = w;
  190. p = pixels;
  191. while (n--) {
  192. *p++ = b1;
  193. *p++ = b2;
  194. *p++ = b3;
  195. }
  196. pixels += pitch;
  197. }
  198. }
  199. static void
  200. SDL_FillRect4(Uint8 * pixels, int pitch, Uint32 color, int w, int h)
  201. {
  202. while (h--) {
  203. SDL_memset4(pixels, color, w);
  204. pixels += pitch;
  205. }
  206. }
  207. /*
  208. * This function performs a fast fill of the given rectangle with 'color'
  209. */
  210. int
  211. SDL_FillRect(SDL_Surface * dst, const SDL_Rect * rect, Uint32 color)
  212. {
  213. SDL_Rect clipped;
  214. Uint8 *pixels;
  215. if (!dst) {
  216. return SDL_SetError("Passed NULL destination surface");
  217. }
  218. /* This function doesn't work on surfaces < 8 bpp */
  219. if (dst->format->BitsPerPixel < 8) {
  220. return SDL_SetError("SDL_FillRect(): Unsupported surface format");
  221. }
  222. /* If 'rect' == NULL, then fill the whole surface */
  223. if (rect) {
  224. /* Perform clipping */
  225. if (!SDL_IntersectRect(rect, &dst->clip_rect, &clipped)) {
  226. return 0;
  227. }
  228. rect = &clipped;
  229. } else {
  230. rect = &dst->clip_rect;
  231. /* Don't attempt to fill if the surface's clip_rect is empty */
  232. if (SDL_RectEmpty(rect)) {
  233. return 0;
  234. }
  235. }
  236. /* Perform software fill */
  237. if (!dst->pixels) {
  238. return SDL_SetError("SDL_FillRect(): You must lock the surface");
  239. }
  240. pixels = (Uint8 *) dst->pixels + rect->y * dst->pitch +
  241. rect->x * dst->format->BytesPerPixel;
  242. switch (dst->format->BytesPerPixel) {
  243. case 1:
  244. {
  245. color |= (color << 8);
  246. color |= (color << 16);
  247. #ifdef __SSE__
  248. if (SDL_HasSSE()) {
  249. SDL_FillRect1SSE(pixels, dst->pitch, color, rect->w, rect->h);
  250. break;
  251. }
  252. #endif
  253. SDL_FillRect1(pixels, dst->pitch, color, rect->w, rect->h);
  254. break;
  255. }
  256. case 2:
  257. {
  258. color |= (color << 16);
  259. #ifdef __SSE__
  260. if (SDL_HasSSE()) {
  261. SDL_FillRect2SSE(pixels, dst->pitch, color, rect->w, rect->h);
  262. break;
  263. }
  264. #endif
  265. SDL_FillRect2(pixels, dst->pitch, color, rect->w, rect->h);
  266. break;
  267. }
  268. case 3:
  269. /* 24-bit RGB is a slow path, at least for now. */
  270. {
  271. SDL_FillRect3(pixels, dst->pitch, color, rect->w, rect->h);
  272. break;
  273. }
  274. case 4:
  275. {
  276. #ifdef __SSE__
  277. if (SDL_HasSSE()) {
  278. SDL_FillRect4SSE(pixels, dst->pitch, color, rect->w, rect->h);
  279. break;
  280. }
  281. #endif
  282. SDL_FillRect4(pixels, dst->pitch, color, rect->w, rect->h);
  283. break;
  284. }
  285. }
  286. /* We're done! */
  287. return 0;
  288. }
  289. int
  290. SDL_FillRects(SDL_Surface * dst, const SDL_Rect * rects, int count,
  291. Uint32 color)
  292. {
  293. int i;
  294. int status = 0;
  295. if (!rects) {
  296. return SDL_SetError("SDL_FillRects() passed NULL rects");
  297. }
  298. for (i = 0; i < count; ++i) {
  299. status += SDL_FillRect(dst, &rects[i], color);
  300. }
  301. return status;
  302. }
  303. /* vi: set ts=4 sw=4 expandtab: */