From 0c7f7436b535e063726a21e043ea2b6482a99b95 Mon Sep 17 00:00:00 2001 From: pelya Date: Wed, 25 Nov 2009 18:02:29 +0200 Subject: [PATCH] Yay, no screen flickering! --- .../sdl/src/video/android/SDL_androidvideo.c | 260 +++++++++++------- .../sdl/src/video/android/SDL_androidvideo.h | 2 - src/sdl/CVideoDriver.cpp | 2 +- 3 files changed, 168 insertions(+), 96 deletions(-) diff --git a/build/Android/project/jni/sdl/src/video/android/SDL_androidvideo.c b/build/Android/project/jni/sdl/src/video/android/SDL_androidvideo.c index cc136fd98..bb3e11b75 100644 --- a/build/Android/project/jni/sdl/src/video/android/SDL_androidvideo.c +++ b/build/Android/project/jni/sdl/src/video/android/SDL_androidvideo.c @@ -71,6 +71,7 @@ static int ANDROID_AllocHWSurface(_THIS, SDL_Surface *surface); static int ANDROID_LockHWSurface(_THIS, SDL_Surface *surface); static void ANDROID_UnlockHWSurface(_THIS, SDL_Surface *surface); static void ANDROID_FreeHWSurface(_THIS, SDL_Surface *surface); +static int ANDROID_FlipHWSurface(_THIS, SDL_Surface *surface); /* etc. */ static void ANDROID_UpdateRects(_THIS, int numrects, SDL_Rect *rects); @@ -78,11 +79,15 @@ static void ANDROID_UpdateRects(_THIS, int numrects, SDL_Rect *rects); static int sWindowWidth = 320; static int sWindowHeight = 480; -static SDL_sem * WaitForNativeRender = NULL; -static SDL_sem * WaitForNativeRender1 = NULL; +static SDL_mutex * WaitForNativeRender = NULL; +static SDL_cond * WaitForNativeRender1 = NULL; +static enum { Render_State_Started, Render_State_Processing, Render_State_Finished } + WaitForNativeRenderState = Render_State_Finished; // Pointer to in-memory video surface static int memX = 0; static int memY = 0; +static void * memBuffer1 = NULL; +static void * memBuffer2 = NULL; static void * memBuffer = NULL; static SDL_Thread * mainThread = NULL; static enum { GL_State_Init, GL_State_Ready, GL_State_Uninit, GL_State_Uninit2 } openglInitialized = GL_State_Uninit2; @@ -146,7 +151,7 @@ static SDL_VideoDevice *ANDROID_CreateDevice(int devindex) device->SetHWAlpha = NULL; device->LockHWSurface = ANDROID_LockHWSurface; device->UnlockHWSurface = ANDROID_UnlockHWSurface; - device->FlipHWSurface = NULL; + device->FlipHWSurface = ANDROID_FlipHWSurface; device->FreeHWSurface = ANDROID_FreeHWSurface; device->SetCaption = NULL; device->SetIcon = NULL; @@ -185,8 +190,8 @@ int ANDROID_VideoInit(_THIS, SDL_PixelFormat *vformat) SDL_modelist[2]->w = 320; SDL_modelist[2]->h = 200; // Always available on any screen and any orientation SDL_modelist[3] = NULL; - WaitForNativeRender = SDL_CreateSemaphore(0); - WaitForNativeRender1 = SDL_CreateSemaphore(0); + WaitForNativeRender = SDL_CreateMutex(); + WaitForNativeRender1 = SDL_CreateCond(); /* We're done! */ return(0); } @@ -201,61 +206,95 @@ SDL_Rect **ANDROID_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags) SDL_Surface *ANDROID_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags) { - if ( this->hidden->buffer ) { - SDL_free( this->hidden->buffer ); - } + if ( memBuffer1 ) + SDL_free( memBuffer1 ); + if ( memBuffer2 ) + SDL_free( memBuffer2 ); memX = width; memY = height; - - /* - // Texture sizes should be 2^n - if( memX <= 256 ) - memX = 256; - else if( memX <= 512 ) - memX = 512; - else - memX = 1024; - - if( memY <= 256 ) - memY = 256; - else if( memY <= 512 ) - memY = 512; - else - memY = 1024; - */ - this->hidden->buffer = SDL_malloc(memX * memY * (bpp / 8)); - if ( ! this->hidden->buffer ) { + memBuffer1 = SDL_malloc(memX * memY * (bpp / 8)); + if ( ! memBuffer1 ) { SDL_SetError("Couldn't allocate buffer for requested mode"); return(NULL); } - memBuffer = this->hidden->buffer; + SDL_memset(memBuffer1, 0, memX * memY * (bpp / 8)); + + if( flags & SDL_DOUBLEBUF ) + { + memBuffer2 = SDL_malloc(memX * memY * (bpp / 8)); + if ( ! memBuffer2 ) { + SDL_SetError("Couldn't allocate buffer for requested mode"); + return(NULL); + } + SDL_memset(memBuffer2, 0, memX * memY * (bpp / 8)); + } + + memBuffer = memBuffer1; openglInitialized = GL_State_Init; -/* printf("Setting mode %dx%d\n", width, height); */ - - SDL_memset(this->hidden->buffer, 0, memX * memY * (bpp / 8)); - /* Allocate the new pixel format for the screen */ if ( ! SDL_ReallocFormat(current, bpp, 0, 0, 0, 0) ) { - SDL_free(this->hidden->buffer); - this->hidden->buffer = NULL; + SDL_free(memBuffer); + memBuffer = NULL; SDL_SetError("Couldn't allocate new pixel format for requested mode"); return(NULL); } /* Set up the new mode framebuffer */ - current->flags = flags & SDL_FULLSCREEN; - this->hidden->w = current->w = width; - this->hidden->h = current->h = height; + current->flags = (flags & SDL_FULLSCREEN) | (flags & SDL_DOUBLEBUF); + current->w = width; + current->h = height; current->pitch = memX * (bpp / 8); - current->pixels = this->hidden->buffer; + current->pixels = memBuffer; /* We're done */ return(current); } +/* Note: If we are terminated, this could be called in the middle of + another SDL video routine -- notably UpdateRects. +*/ +void ANDROID_VideoQuit(_THIS) +{ + openglInitialized = GL_State_Uninit; + while( openglInitialized != GL_State_Uninit2 ) + SDL_Delay(50); + + memX = 0; + memY = 0; + memBuffer = NULL; + SDL_free( memBuffer1 ); + memBuffer1 = NULL; + if( memBuffer2 ) + SDL_free( memBuffer2 ); + memBuffer2 = NULL; + SDL_DestroyMutex( WaitForNativeRender ); + WaitForNativeRender = NULL; + SDL_DestroyCond( WaitForNativeRender1 ); + WaitForNativeRender1 = NULL; + + int i; + + if (this->screen->pixels != NULL) + { + SDL_free(this->screen->pixels); + this->screen->pixels = NULL; + } + /* Free video mode lists */ + for ( i=0; ipixels = memBuffer; + + SDL_mutexV(WaitForNativeRender); + + return(0); +}; int ANDROID_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors) { return(1); } -/* Note: If we are terminated, this could be called in the middle of - another SDL video routine -- notably UpdateRects. -*/ -void ANDROID_VideoQuit(_THIS) -{ - openglInitialized = GL_State_Uninit; - while( openglInitialized != GL_State_Uninit2 ) - SDL_Delay(50); - - memX = 0; - memY = 0; - memBuffer = NULL; - SDL_DestroySemaphore( WaitForNativeRender ); - WaitForNativeRender = NULL; - SDL_DestroySemaphore( WaitForNativeRender1 ); - WaitForNativeRender1 = NULL; - - int i; - - if (this->screen->pixels != NULL) - { - SDL_free(this->screen->pixels); - this->screen->pixels = NULL; - } - /* Free video mode lists */ - for ( i=0; i