SDL_blit.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 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_sysvideo.h"
  20. #include "SDL_surface_c.h"
  21. #include "SDL_blit_auto.h"
  22. #include "SDL_blit_copy.h"
  23. #include "SDL_blit_slow.h"
  24. #include "SDL_RLEaccel_c.h"
  25. #include "SDL_pixels_c.h"
  26. // The general purpose software blit routine
  27. static bool SDLCALL SDL_SoftBlit(SDL_Surface *src, const SDL_Rect *srcrect,
  28. SDL_Surface *dst, const SDL_Rect *dstrect)
  29. {
  30. bool okay;
  31. int src_locked;
  32. int dst_locked;
  33. // Everything is okay at the beginning...
  34. okay = true;
  35. // Lock the destination if it's in hardware
  36. dst_locked = 0;
  37. if (SDL_MUSTLOCK(dst)) {
  38. if (!SDL_LockSurface(dst)) {
  39. okay = false;
  40. } else {
  41. dst_locked = 1;
  42. }
  43. }
  44. // Lock the source if it's in hardware
  45. src_locked = 0;
  46. if (SDL_MUSTLOCK(src)) {
  47. if (!SDL_LockSurface(src)) {
  48. okay = false;
  49. } else {
  50. src_locked = 1;
  51. }
  52. }
  53. // Set up source and destination buffer pointers, and BLIT!
  54. if (okay) {
  55. SDL_BlitFunc RunBlit;
  56. SDL_BlitInfo *info = &src->map.info;
  57. // Set up the blit information
  58. if (info->src_fmt->bits_per_pixel >= 8) {
  59. info->src = (Uint8 *)src->pixels +
  60. srcrect->y * src->pitch +
  61. srcrect->x * info->src_fmt->bytes_per_pixel;
  62. } else {
  63. info->src = (Uint8 *)src->pixels +
  64. srcrect->y * src->pitch +
  65. (srcrect->x * info->src_fmt->bits_per_pixel) / 8;
  66. info->leading_skip =
  67. ((srcrect->x * info->src_fmt->bits_per_pixel) % 8) /
  68. info->src_fmt->bits_per_pixel;
  69. }
  70. info->src_w = srcrect->w;
  71. info->src_h = srcrect->h;
  72. info->src_pitch = src->pitch;
  73. info->src_skip =
  74. info->src_pitch - info->src_w * info->src_fmt->bytes_per_pixel;
  75. info->dst = (Uint8 *)dst->pixels +
  76. dstrect->y * dst->pitch +
  77. dstrect->x * info->dst_fmt->bytes_per_pixel;
  78. info->dst_w = dstrect->w;
  79. info->dst_h = dstrect->h;
  80. info->dst_pitch = dst->pitch;
  81. info->dst_skip =
  82. info->dst_pitch - info->dst_w * info->dst_fmt->bytes_per_pixel;
  83. RunBlit = (SDL_BlitFunc)src->map.data;
  84. // Run the actual software blit
  85. RunBlit(info);
  86. }
  87. // We need to unlock the surfaces if they're locked
  88. if (dst_locked) {
  89. SDL_UnlockSurface(dst);
  90. }
  91. if (src_locked) {
  92. SDL_UnlockSurface(src);
  93. }
  94. // Blit is done!
  95. return okay;
  96. }
  97. #ifdef SDL_HAVE_BLIT_AUTO
  98. #ifdef SDL_PLATFORM_MACOS
  99. #include <sys/sysctl.h>
  100. static bool SDL_UseAltivecPrefetch(void)
  101. {
  102. const char key[] = "hw.l3cachesize";
  103. u_int64_t result = 0;
  104. size_t typeSize = sizeof(result);
  105. if (sysctlbyname(key, &result, &typeSize, NULL, 0) == 0 && result > 0) {
  106. return true;
  107. } else {
  108. return false;
  109. }
  110. }
  111. #else
  112. static bool SDL_UseAltivecPrefetch(void)
  113. {
  114. // Just guess G4
  115. return true;
  116. }
  117. #endif // SDL_PLATFORM_MACOS
  118. static SDL_BlitFunc SDL_ChooseBlitFunc(SDL_PixelFormat src_format, SDL_PixelFormat dst_format, int flags,
  119. SDL_BlitFuncEntry *entries)
  120. {
  121. int i, flagcheck = (flags & (SDL_COPY_MODULATE_MASK | SDL_COPY_BLEND_MASK | SDL_COPY_COLORKEY | SDL_COPY_NEAREST));
  122. static unsigned int features = 0x7fffffff;
  123. // Get the available CPU features
  124. if (features == 0x7fffffff) {
  125. features = SDL_CPU_ANY;
  126. if (SDL_HasMMX()) {
  127. features |= SDL_CPU_MMX;
  128. }
  129. if (SDL_HasSSE()) {
  130. features |= SDL_CPU_SSE;
  131. }
  132. if (SDL_HasSSE2()) {
  133. features |= SDL_CPU_SSE2;
  134. }
  135. if (SDL_HasAltiVec()) {
  136. if (SDL_UseAltivecPrefetch()) {
  137. features |= SDL_CPU_ALTIVEC_PREFETCH;
  138. } else {
  139. features |= SDL_CPU_ALTIVEC_NOPREFETCH;
  140. }
  141. }
  142. }
  143. for (i = 0; entries[i].func; ++i) {
  144. // Check for matching pixel formats
  145. if (src_format != entries[i].src_format) {
  146. continue;
  147. }
  148. if (dst_format != entries[i].dst_format) {
  149. continue;
  150. }
  151. // Check flags
  152. if ((flagcheck & entries[i].flags) != flagcheck) {
  153. continue;
  154. }
  155. // Check CPU features
  156. if ((entries[i].cpu & features) != entries[i].cpu) {
  157. continue;
  158. }
  159. // We found the best one!
  160. return entries[i].func;
  161. }
  162. return NULL;
  163. }
  164. #endif // SDL_HAVE_BLIT_AUTO
  165. // Figure out which of many blit routines to set up on a surface
  166. bool SDL_CalculateBlit(SDL_Surface *surface, SDL_Surface *dst)
  167. {
  168. SDL_BlitFunc blit = NULL;
  169. SDL_BlitMap *map = &surface->map;
  170. SDL_Colorspace src_colorspace = surface->colorspace;
  171. SDL_Colorspace dst_colorspace = dst->colorspace;
  172. // We don't currently support blitting to < 8 bpp surfaces
  173. if (SDL_BITSPERPIXEL(dst->format) < 8) {
  174. SDL_InvalidateMap(map);
  175. return SDL_SetError("Blit combination not supported");
  176. }
  177. // We should have cleared out RLE at this point
  178. SDL_assert(!(surface->internal_flags & SDL_INTERNAL_SURFACE_RLEACCEL));
  179. map->blit = SDL_SoftBlit;
  180. map->info.src_surface = surface;
  181. map->info.src_fmt = surface->fmt;
  182. map->info.src_pal = surface->palette;
  183. map->info.dst_surface = dst;
  184. map->info.dst_fmt = dst->fmt;
  185. map->info.dst_pal = dst->palette;
  186. #ifdef SDL_HAVE_RLE
  187. // See if we can do RLE acceleration
  188. if (map->info.flags & SDL_COPY_RLE_DESIRED) {
  189. if (SDL_RLESurface(surface)) {
  190. return true;
  191. }
  192. }
  193. #endif
  194. // Choose a standard blit function
  195. if (!blit) {
  196. if (src_colorspace != dst_colorspace ||
  197. SDL_BYTESPERPIXEL(surface->format) > 4 ||
  198. SDL_BYTESPERPIXEL(dst->format) > 4) {
  199. blit = SDL_Blit_Slow_Float;
  200. }
  201. }
  202. if (!blit) {
  203. if (map->identity && !(map->info.flags & ~SDL_COPY_RLE_DESIRED)) {
  204. blit = SDL_BlitCopy;
  205. } else if (SDL_ISPIXELFORMAT_10BIT(surface->format) ||
  206. SDL_ISPIXELFORMAT_10BIT(dst->format)) {
  207. blit = SDL_Blit_Slow;
  208. }
  209. #ifdef SDL_HAVE_BLIT_0
  210. else if (SDL_BITSPERPIXEL(surface->format) < 8 &&
  211. SDL_ISPIXELFORMAT_INDEXED(surface->format)) {
  212. blit = SDL_CalculateBlit0(surface);
  213. }
  214. #endif
  215. #ifdef SDL_HAVE_BLIT_1
  216. else if (SDL_BYTESPERPIXEL(surface->format) == 1 &&
  217. SDL_ISPIXELFORMAT_INDEXED(surface->format)) {
  218. blit = SDL_CalculateBlit1(surface);
  219. }
  220. #endif
  221. #ifdef SDL_HAVE_BLIT_A
  222. else if (map->info.flags & SDL_COPY_BLEND) {
  223. blit = SDL_CalculateBlitA(surface);
  224. }
  225. #endif
  226. #ifdef SDL_HAVE_BLIT_N
  227. else {
  228. blit = SDL_CalculateBlitN(surface);
  229. }
  230. #endif
  231. }
  232. #ifdef SDL_HAVE_BLIT_AUTO
  233. if (!blit) {
  234. SDL_PixelFormat src_format = surface->format;
  235. SDL_PixelFormat dst_format = dst->format;
  236. blit =
  237. SDL_ChooseBlitFunc(src_format, dst_format, map->info.flags,
  238. SDL_GeneratedBlitFuncTable);
  239. }
  240. #endif
  241. #ifndef TEST_SLOW_BLIT
  242. if (!blit)
  243. #endif
  244. {
  245. SDL_PixelFormat src_format = surface->format;
  246. SDL_PixelFormat dst_format = dst->format;
  247. if ((!SDL_ISPIXELFORMAT_INDEXED(src_format) ||
  248. (src_format == SDL_PIXELFORMAT_INDEX8 && surface->palette)) &&
  249. !SDL_ISPIXELFORMAT_FOURCC(src_format) &&
  250. (!SDL_ISPIXELFORMAT_INDEXED(dst_format) ||
  251. (dst_format == SDL_PIXELFORMAT_INDEX8 && dst->palette)) &&
  252. !SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
  253. blit = SDL_Blit_Slow;
  254. }
  255. }
  256. map->data = (void *)blit;
  257. // Make sure we have a blit function
  258. if (!blit) {
  259. SDL_InvalidateMap(map);
  260. return SDL_SetError("Blit combination not supported");
  261. }
  262. return true;
  263. }