2 Simple DirectMedia Layer
3 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
21 #include "../../SDL_internal.h"
23 #if SDL_VIDEO_DRIVER_HAIKU
25 #include "SDL_bframebuffer.h"
28 #include <InterfaceKit.h>
29 #include "SDL_bmodes.h"
32 #include "../../main/haiku/SDL_BApp.h"
38 int32 BE_UpdateOnce(SDL_Window *window);
40 static SDL_INLINE SDL_BWin *_ToBeWin(SDL_Window *window) {
41 return ((SDL_BWin*)(window->driverdata));
44 static SDL_INLINE SDL_BApp *_GetBeApp() {
45 return ((SDL_BApp*)be_app);
48 int BE_CreateWindowFramebuffer(_THIS, SDL_Window * window,
50 void ** pixels, int *pitch) {
51 SDL_BWin *bwin = _ToBeWin(window);
53 if(!bscreen.IsValid()) {
57 while(!bwin->Connected()) { snooze(100); }
59 /* Make sure we have exclusive access to frame buffer data */
64 bscreen.GetMode(&bmode);
65 int32 bpp = BE_ColorSpaceToBitsPerPixel(bmode.space);
66 *format = BE_BPPToSDLPxFormat(bpp);
68 /* Create the new bitmap object */
69 BBitmap *bitmap = bwin->GetBitmap();
74 bitmap = new BBitmap(bwin->Bounds(), (color_space)bmode.space,
75 false, /* Views not accepted */
76 true); /* Contiguous memory required */
78 if(bitmap->InitCheck() != B_OK) {
79 return SDL_SetError("Could not initialize back buffer!\n");
83 bwin->SetBitmap(bitmap);
85 /* Set the pixel pointer */
86 *pixels = bitmap->Bits();
88 /* pitch = width of window, in bytes */
89 *pitch = bitmap->BytesPerRow();
91 bwin->SetBufferExists(true);
92 bwin->SetTrashBuffer(false);
99 int BE_UpdateWindowFramebuffer(_THIS, SDL_Window * window,
100 const SDL_Rect * rects, int numrects) {
104 SDL_BWin *bwin = _ToBeWin(window);
108 bwin->SetBufferDirty(true);
109 bwin->UnlockBuffer();
111 bwin->SetBufferDirty(true);
112 BE_UpdateOnce(window);
118 int32 BE_DrawThread(void *data) {
119 SDL_BWin *bwin = (SDL_BWin*)data;
122 if(!bscreen.IsValid()) {
126 while(bwin->ConnectionEnabled()) {
127 if( bwin->Connected() && bwin->BufferExists() && bwin->BufferIsDirty() ) {
129 BBitmap *bitmap = NULL;
130 bitmap = bwin->GetBitmap();
131 int32 windowPitch = bitmap->BytesPerRow();
132 int32 bufferPitch = bwin->GetRowBytes();
136 int32 BPP = bwin->GetBytesPerPx();
137 int32 windowSub = bwin->GetFbX() * BPP +
138 bwin->GetFbY() * windowPitch;
139 clipping_rect *clips = bwin->GetClips();
140 int32 numClips = bwin->GetNumClips();
143 /* Blit each clipping rectangle */
144 bscreen.WaitForRetrace();
145 for(i = 0; i < numClips; ++i) {
146 clipping_rect rc = clips[i];
147 /* Get addresses of the start of each clipping rectangle */
148 int32 width = clips[i].right - clips[i].left + 1;
149 int32 height = clips[i].bottom - clips[i].top + 1;
150 bufferpx = bwin->GetBufferPx() +
151 clips[i].top * bufferPitch + clips[i].left * BPP;
152 windowpx = (uint8*)bitmap->Bits() +
153 clips[i].top * windowPitch + clips[i].left * BPP -
156 /* Copy each row of pixels from the window buffer into the frame
158 for(y = 0; y < height; ++y)
161 if(bwin->CanTrashWindowBuffer()) {
162 goto escape; /* Break out before the buffer is killed */
165 memcpy(bufferpx, windowpx, width * BPP);
166 bufferpx += bufferPitch;
167 windowpx += windowPitch;
171 bwin->SetBufferDirty(false);
173 bwin->UnlockBuffer();
182 void BE_DestroyWindowFramebuffer(_THIS, SDL_Window * window) {
183 SDL_BWin *bwin = _ToBeWin(window);
187 /* Free and clear the window buffer */
188 BBitmap *bitmap = bwin->GetBitmap();
190 bwin->SetBitmap(NULL);
191 bwin->SetBufferExists(false);
192 bwin->UnlockBuffer();
198 * This was written to test if certain errors were caused by threading issues.
199 * The specific issues have since become rare enough that they may have been
200 * solved, but I doubt it- they were pretty sporadic before now.
202 int32 BE_UpdateOnce(SDL_Window *window) {
203 SDL_BWin *bwin = _ToBeWin(window);
205 if(!bscreen.IsValid()) {
209 if(bwin->ConnectionEnabled() && bwin->Connected()) {
211 int32 windowPitch = window->surface->pitch;
212 int32 bufferPitch = bwin->GetRowBytes();
216 int32 BPP = bwin->GetBytesPerPx();
217 uint8 *windowBaseAddress = (uint8*)window->surface->pixels;
218 int32 windowSub = bwin->GetFbX() * BPP +
219 bwin->GetFbY() * windowPitch;
220 clipping_rect *clips = bwin->GetClips();
221 int32 numClips = bwin->GetNumClips();
224 /* Blit each clipping rectangle */
225 bscreen.WaitForRetrace();
226 for(i = 0; i < numClips; ++i) {
227 clipping_rect rc = clips[i];
228 /* Get addresses of the start of each clipping rectangle */
229 int32 width = clips[i].right - clips[i].left + 1;
230 int32 height = clips[i].bottom - clips[i].top + 1;
231 bufferpx = bwin->GetBufferPx() +
232 clips[i].top * bufferPitch + clips[i].left * BPP;
233 windowpx = windowBaseAddress +
234 clips[i].top * windowPitch + clips[i].left * BPP - windowSub;
236 /* Copy each row of pixels from the window buffer into the frame
238 for(y = 0; y < height; ++y)
240 memcpy(bufferpx, windowpx, width * BPP);
241 bufferpx += bufferPitch;
242 windowpx += windowPitch;
245 bwin->UnlockBuffer();
254 #endif /* SDL_VIDEO_DRIVER_HAIKU */