src/video/bwindow/SDL_bframebuffer.cc
author Nathan Heisey <nathanheisey@gmail.com>
Wed, 03 Aug 2011 05:09:36 +0000
changeset 5932 cd08863bcb49
parent 5931 edf9a8e68b34
child 5933 5f8a7ebc88e5
permissions -rw-r--r--
Fixed minor rendering issues.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     4 
     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.
     8 
     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:
    12 
    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.
    20 */
    21 
    22 #include "SDL_bframebuffer.h"
    23 
    24 #include <AppKit.h>
    25 #include <InterfaceKit.h>
    26 #include "SDL_bmodes.h"
    27 #include "SDL_BWin.h"
    28 
    29 #include "../../main/beos/SDL_BApp.h"
    30 
    31 #ifdef __cplusplus
    32 extern "C" {
    33 #endif
    34 
    35 static inline SDL_BWin *_ToBeWin(SDL_Window *window) {
    36 	return ((SDL_BWin*)(window->driverdata));
    37 }
    38 
    39 static inline SDL_BApp *_GetBeApp() {
    40 	return ((SDL_BApp*)be_app);
    41 }
    42 
    43 int BE_CreateWindowFramebuffer(_THIS, SDL_Window * window,
    44                                        Uint32 * format,
    45                                        void ** pixels, int *pitch) {
    46 	SDL_BWin *bwin = _ToBeWin(window);
    47 	BScreen bscreen;
    48 	if(!bscreen.IsValid()) {
    49 		return -1;
    50 	}
    51 
    52 	while(!bwin->Connected()) { snooze(100); }
    53 	
    54 	/* Make sure we have exclusive access to frame buffer data */
    55 	bwin->LockBuffer();
    56 
    57 	/* format */
    58 	display_mode bmode;
    59 	bscreen.GetMode(&bmode);
    60 	int32 bpp = ColorSpaceToBitsPerPixel(bmode.space);
    61 	*format = BPPToSDLPxFormat(bpp);
    62 
    63 	/* pitch = width of screen, in bytes */
    64 	*pitch = bwin->GetFbWidth() * bwin->GetBytesPerPx();
    65 
    66 	/* Create a copy of the pixel buffer if it doesn't recycle */
    67 	*pixels = bwin->GetWindowFramebuffer();
    68 	if( (*pixels) != NULL ) {
    69 		SDL_free(*pixels);
    70 	}
    71 	*pixels = SDL_calloc((*pitch) * bwin->GetFbHeight() * 
    72 		bwin->GetBytesPerPx(), sizeof(uint8));
    73 	bwin->SetWindowFramebuffer((uint8*)(*pixels));
    74 
    75 	bwin->SetBufferExists(true);
    76 	bwin->SetTrashBuffer(false);
    77 	bwin->UnlockBuffer();
    78 	return 0;
    79 }
    80 
    81 
    82 
    83 int BE_UpdateWindowFramebuffer(_THIS, SDL_Window * window,
    84                                       SDL_Rect * rects, int numrects) {
    85 	if(!window)
    86 		return 0;
    87 
    88 	SDL_BWin *bwin = _ToBeWin(window);
    89 	
    90 	bwin->LockBuffer();
    91 	bwin->SetBufferDirty(true);
    92 	bwin->UnlockBuffer();
    93 
    94 	return 0;
    95 }
    96 
    97 int32 BE_DrawThread(void *data) {
    98 	SDL_BWin *bwin = (SDL_BWin*)data;
    99 	SDL_Window *window = _GetBeApp()->GetSDLWindow(bwin->GetID());
   100 	
   101 	BScreen bscreen;
   102 	if(!bscreen.IsValid()) {
   103 		return -1;
   104 	}
   105 
   106 	while(bwin->ConnectionEnabled()) {
   107 		if( bwin->Connected() && bwin->BufferExists() && bwin->BufferIsDirty() ) {
   108 			bwin->LockBuffer();
   109 			int32 windowPitch = window->surface->pitch;
   110 			int32 bufferPitch = bwin->GetRowBytes();
   111 			uint8 *windowpx;
   112 			uint8 *bufferpx;
   113 
   114 			int32 BPP = bwin->GetBytesPerPx();
   115 			uint8 *windowBaseAddress = (uint8*)window->surface->pixels;
   116 			int32 windowSub = bwin->GetFbX() * BPP +
   117 						  bwin->GetFbY() * windowPitch;
   118 			clipping_rect *clips = bwin->GetClips();
   119 			int32 numClips = bwin->GetNumClips();
   120 			int i, y;
   121 
   122 			/* Blit each clipping rectangle */
   123 			bscreen.WaitForRetrace();
   124 			for(i = 0; i < numClips; ++i) {
   125 				clipping_rect rc = clips[i];
   126 				/* Get addresses of the start of each clipping rectangle */
   127 				int32 width = clips[i].right - clips[i].left + 1;
   128 				int32 height = clips[i].bottom - clips[i].top + 1;
   129 				bufferpx = bwin->GetBufferPx() + 
   130 					clips[i].top * bufferPitch + clips[i].left * BPP;
   131 				windowpx = windowBaseAddress + 
   132 					clips[i].top * windowPitch + clips[i].left * BPP - windowSub;
   133 
   134 				/* Copy each row of pixels from the window buffer into the frame
   135 				   buffer */
   136 				for(y = 0; y < height; ++y)
   137 				{
   138 					if(bwin->CanTrashWindowBuffer()) {
   139 						goto escape;	/* Break out before the buffer is killed */
   140 					}
   141 					memcpy(bufferpx, windowpx, width * BPP);
   142 					bufferpx += bufferPitch;
   143 					windowpx += windowPitch;
   144 				}
   145 			}
   146 			bwin->SetBufferDirty(false);
   147 escape:
   148 			bwin->UnlockBuffer();
   149 		} else {
   150 			snooze(16000);
   151 		}
   152 	}
   153 	
   154 	return B_OK;
   155 }
   156 
   157 void BE_DestroyWindowFramebuffer(_THIS, SDL_Window * window) {
   158 	SDL_BWin *bwin = _ToBeWin(window);
   159 	
   160 	bwin->LockBuffer();
   161 	
   162 	/* Free and clear the window buffer */
   163 	uint8* winBuffer = bwin->GetWindowFramebuffer();
   164 	SDL_free(winBuffer);
   165 	bwin->SetWindowFramebuffer(NULL);
   166 	bwin->SetBufferExists(false);
   167 	bwin->UnlockBuffer();
   168 }
   169 
   170 #ifdef __cplusplus
   171 }
   172 #endif