SDL_bframebuffer.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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. #if SDL_VIDEO_DRIVER_HAIKU
  20. #include "SDL_bframebuffer.h"
  21. #include <AppKit.h>
  22. #include <InterfaceKit.h>
  23. #include "SDL_bmodes.h"
  24. #include "SDL_BWin.h"
  25. #include "../../main/haiku/SDL_BApp.h"
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. int32 BE_UpdateOnce(SDL_Window *window);
  30. static SDL_INLINE SDL_BWin *_ToBeWin(SDL_Window *window) {
  31. return ((SDL_BWin*)(window->driverdata));
  32. }
  33. static SDL_INLINE SDL_BApp *_GetBeApp() {
  34. return ((SDL_BApp*)be_app);
  35. }
  36. int BE_CreateWindowFramebuffer(_THIS, SDL_Window * window,
  37. Uint32 * format,
  38. void ** pixels, int *pitch) {
  39. SDL_BWin *bwin = _ToBeWin(window);
  40. BScreen bscreen;
  41. if(!bscreen.IsValid()) {
  42. return -1;
  43. }
  44. while(!bwin->Connected()) { snooze(100); }
  45. /* Make sure we have exclusive access to frame buffer data */
  46. bwin->LockBuffer();
  47. /* format */
  48. display_mode bmode;
  49. bscreen.GetMode(&bmode);
  50. int32 bpp = BE_ColorSpaceToBitsPerPixel(bmode.space);
  51. *format = BE_BPPToSDLPxFormat(bpp);
  52. /* Create the new bitmap object */
  53. BBitmap *bitmap = bwin->GetBitmap();
  54. if(bitmap) {
  55. delete bitmap;
  56. }
  57. bitmap = new BBitmap(bwin->Bounds(), (color_space)bmode.space,
  58. false, /* Views not accepted */
  59. true); /* Contiguous memory required */
  60. if(bitmap->InitCheck() != B_OK) {
  61. return SDL_SetError("Could not initialize back buffer!\n");
  62. }
  63. bwin->SetBitmap(bitmap);
  64. /* Set the pixel pointer */
  65. *pixels = bitmap->Bits();
  66. /* pitch = width of window, in bytes */
  67. *pitch = bitmap->BytesPerRow();
  68. bwin->SetBufferExists(true);
  69. bwin->SetTrashBuffer(false);
  70. bwin->UnlockBuffer();
  71. return 0;
  72. }
  73. int BE_UpdateWindowFramebuffer(_THIS, SDL_Window * window,
  74. const SDL_Rect * rects, int numrects) {
  75. if(!window)
  76. return 0;
  77. SDL_BWin *bwin = _ToBeWin(window);
  78. #ifdef DRAWTHREAD
  79. bwin->LockBuffer();
  80. bwin->SetBufferDirty(true);
  81. bwin->UnlockBuffer();
  82. #else
  83. bwin->SetBufferDirty(true);
  84. BE_UpdateOnce(window);
  85. #endif
  86. return 0;
  87. }
  88. int32 BE_DrawThread(void *data) {
  89. SDL_BWin *bwin = (SDL_BWin*)data;
  90. BScreen bscreen;
  91. if(!bscreen.IsValid()) {
  92. return -1;
  93. }
  94. while(bwin->ConnectionEnabled()) {
  95. if( bwin->Connected() && bwin->BufferExists() && bwin->BufferIsDirty() ) {
  96. bwin->LockBuffer();
  97. BBitmap *bitmap = NULL;
  98. bitmap = bwin->GetBitmap();
  99. int32 windowPitch = bitmap->BytesPerRow();
  100. int32 bufferPitch = bwin->GetRowBytes();
  101. uint8 *windowpx;
  102. uint8 *bufferpx;
  103. int32 BPP = bwin->GetBytesPerPx();
  104. int32 windowSub = bwin->GetFbX() * BPP +
  105. bwin->GetFbY() * windowPitch;
  106. clipping_rect *clips = bwin->GetClips();
  107. int32 numClips = bwin->GetNumClips();
  108. int i, y;
  109. /* Blit each clipping rectangle */
  110. bscreen.WaitForRetrace();
  111. for(i = 0; i < numClips; ++i) {
  112. clipping_rect rc = clips[i];
  113. /* Get addresses of the start of each clipping rectangle */
  114. int32 width = clips[i].right - clips[i].left + 1;
  115. int32 height = clips[i].bottom - clips[i].top + 1;
  116. bufferpx = bwin->GetBufferPx() +
  117. clips[i].top * bufferPitch + clips[i].left * BPP;
  118. windowpx = (uint8*)bitmap->Bits() +
  119. clips[i].top * windowPitch + clips[i].left * BPP -
  120. windowSub;
  121. /* Copy each row of pixels from the window buffer into the frame
  122. buffer */
  123. for(y = 0; y < height; ++y)
  124. {
  125. if(bwin->CanTrashWindowBuffer()) {
  126. goto escape; /* Break out before the buffer is killed */
  127. }
  128. memcpy(bufferpx, windowpx, width * BPP);
  129. bufferpx += bufferPitch;
  130. windowpx += windowPitch;
  131. }
  132. }
  133. bwin->SetBufferDirty(false);
  134. escape:
  135. bwin->UnlockBuffer();
  136. } else {
  137. snooze(16000);
  138. }
  139. }
  140. return B_OK;
  141. }
  142. void BE_DestroyWindowFramebuffer(_THIS, SDL_Window * window) {
  143. SDL_BWin *bwin = _ToBeWin(window);
  144. bwin->LockBuffer();
  145. /* Free and clear the window buffer */
  146. BBitmap *bitmap = bwin->GetBitmap();
  147. delete bitmap;
  148. bwin->SetBitmap(NULL);
  149. bwin->SetBufferExists(false);
  150. bwin->UnlockBuffer();
  151. }
  152. /*
  153. * TODO:
  154. * This was written to test if certain errors were caused by threading issues.
  155. * The specific issues have since become rare enough that they may have been
  156. * solved, but I doubt it- they were pretty sporadic before now.
  157. */
  158. int32 BE_UpdateOnce(SDL_Window *window) {
  159. SDL_BWin *bwin = _ToBeWin(window);
  160. BScreen bscreen;
  161. if(!bscreen.IsValid()) {
  162. return -1;
  163. }
  164. if(bwin->ConnectionEnabled() && bwin->Connected()) {
  165. bwin->LockBuffer();
  166. int32 windowPitch = window->surface->pitch;
  167. int32 bufferPitch = bwin->GetRowBytes();
  168. uint8 *windowpx;
  169. uint8 *bufferpx;
  170. int32 BPP = bwin->GetBytesPerPx();
  171. uint8 *windowBaseAddress = (uint8*)window->surface->pixels;
  172. int32 windowSub = bwin->GetFbX() * BPP +
  173. bwin->GetFbY() * windowPitch;
  174. clipping_rect *clips = bwin->GetClips();
  175. int32 numClips = bwin->GetNumClips();
  176. int i, y;
  177. /* Blit each clipping rectangle */
  178. bscreen.WaitForRetrace();
  179. for(i = 0; i < numClips; ++i) {
  180. clipping_rect rc = clips[i];
  181. /* Get addresses of the start of each clipping rectangle */
  182. int32 width = clips[i].right - clips[i].left + 1;
  183. int32 height = clips[i].bottom - clips[i].top + 1;
  184. bufferpx = bwin->GetBufferPx() +
  185. clips[i].top * bufferPitch + clips[i].left * BPP;
  186. windowpx = windowBaseAddress +
  187. clips[i].top * windowPitch + clips[i].left * BPP - windowSub;
  188. /* Copy each row of pixels from the window buffer into the frame
  189. buffer */
  190. for(y = 0; y < height; ++y)
  191. {
  192. memcpy(bufferpx, windowpx, width * BPP);
  193. bufferpx += bufferPitch;
  194. windowpx += windowPitch;
  195. }
  196. }
  197. bwin->UnlockBuffer();
  198. }
  199. return 0;
  200. }
  201. #ifdef __cplusplus
  202. }
  203. #endif
  204. #endif /* SDL_VIDEO_DRIVER_HAIKU */