SDL_blit.c 8.4 KB

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