src/video/bwindow/SDL_bframebuffer.cc
author Nathan Heisey <nathanheisey@gmail.com>
Mon, 08 Aug 2011 17:00:38 +0000
changeset 5934 01531d8d1e01
parent 5933 5f8a7ebc88e5
child 5936 116b7457e73a
permissions -rw-r--r--
Some video fixes
     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 	/* Create the new bitmap object */
    64 	BBitmap *bitmap = bwin->GetBitmap();
    65 
    66 	if(bitmap) {
    67 		delete bitmap;
    68 	}
    69 	bitmap = new BBitmap(bwin->Bounds(), (color_space)bmode.space,
    70 			false,	/* Views not accepted */
    71 			true);	/* Contiguous memory required */
    72 			
    73 	if(bitmap->InitCheck() != B_OK) {
    74 		SDL_SetError("Could not initialize back buffer!\n");
    75 		return -1;
    76 	}
    77 
    78 
    79 	bwin->SetBitmap(bitmap);
    80 	
    81 	/* Set the pixel pointer */
    82 	*pixels = bitmap->Bits();
    83 
    84 	/* pitch = width of window, in bytes */
    85 	*pitch = bitmap->BytesPerRow();
    86 
    87 	bwin->SetBufferExists(true);
    88 	bwin->SetTrashBuffer(false);
    89 	bwin->UnlockBuffer();
    90 	return 0;
    91 }
    92 
    93 
    94 
    95 int BE_UpdateWindowFramebuffer(_THIS, SDL_Window * window,
    96                                       SDL_Rect * rects, int numrects) {
    97 	if(!window)
    98 		return 0;
    99 
   100 	SDL_BWin *bwin = _ToBeWin(window);
   101 	
   102 	bwin->LockBuffer();
   103 	bwin->SetBufferDirty(true);
   104 	bwin->UnlockBuffer();
   105 
   106 	return 0;
   107 }
   108 
   109 int32 BE_DrawThread(void *data) {
   110 	SDL_BWin *bwin = (SDL_BWin*)data;
   111 	SDL_Window *window = _GetBeApp()->GetSDLWindow(bwin->GetID());
   112 	
   113 	BScreen bscreen;
   114 	if(!bscreen.IsValid()) {
   115 		return -1;
   116 	}
   117 
   118 	while(bwin->ConnectionEnabled()) {
   119 		if( bwin->Connected() && bwin->BufferExists() && bwin->BufferIsDirty() ) {
   120 			bwin->LockBuffer();
   121 			BBitmap *bitmap = bwin->GetBitmap();
   122 			int32 windowPitch = bitmap->BytesPerRow();
   123 			int32 bufferPitch = bwin->GetRowBytes();
   124 			uint8 *windowpx;
   125 			uint8 *bufferpx;
   126 
   127 			int32 BPP = bwin->GetBytesPerPx();
   128 			int32 windowSub = bwin->GetFbX() * BPP +
   129 						  bwin->GetFbY() * windowPitch;
   130 			clipping_rect *clips = bwin->GetClips();
   131 			int32 numClips = bwin->GetNumClips();
   132 			int i, y;
   133 
   134 			/* Blit each clipping rectangle */
   135 			bscreen.WaitForRetrace();
   136 			for(i = 0; i < numClips; ++i) {
   137 				clipping_rect rc = clips[i];
   138 				/* Get addresses of the start of each clipping rectangle */
   139 				int32 width = clips[i].right - clips[i].left + 1;
   140 				int32 height = clips[i].bottom - clips[i].top + 1;
   141 				bufferpx = bwin->GetBufferPx() + 
   142 					clips[i].top * bufferPitch + clips[i].left * BPP;
   143 				windowpx = (uint8*)bitmap->Bits() + 
   144 					clips[i].top * windowPitch + clips[i].left * BPP -
   145 					windowSub;
   146 
   147 				/* Copy each row of pixels from the window buffer into the frame
   148 				   buffer */
   149 				for(y = 0; y < height; ++y)
   150 				{
   151 					if(bwin->CanTrashWindowBuffer()) {
   152 						goto escape;	/* Break out before the buffer is killed */
   153 					}
   154 //					printf("memcpy(0x%x, 0x%x, %i) ", bufferpx, windowpx, width * BPP);
   155 					memcpy(bufferpx, windowpx, width * BPP);
   156 					bufferpx += bufferPitch;
   157 					windowpx += windowPitch;
   158 				}
   159 //				printf("\t-\t");
   160 			}
   161 //			printf("\n");
   162 			bwin->SetBufferDirty(false);
   163 escape:
   164 			bwin->UnlockBuffer();
   165 		} else {
   166 			snooze(16000);
   167 		}
   168 	}
   169 	
   170 	return B_OK;
   171 }
   172 
   173 void BE_DestroyWindowFramebuffer(_THIS, SDL_Window * window) {
   174 	SDL_BWin *bwin = _ToBeWin(window);
   175 	
   176 	bwin->LockBuffer();
   177 	
   178 	/* Free and clear the window buffer */
   179 	BBitmap *bitmap = bwin->GetBitmap();
   180 	delete bitmap;
   181 	bwin->SetBitmap(NULL);
   182 	bwin->SetBufferExists(false);
   183 	bwin->UnlockBuffer();
   184 }
   185 
   186 #ifdef __cplusplus
   187 }
   188 #endif