SDL_blit.c 8.3 KB

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