1 /*
2     SDL - Simple DirectMedia Layer
3 
4     This library is free software; you can redistribute it and/or
5     modify it under the terms of the GNU Lesser General Public
6     License as published by the Free Software Foundation; either
7     version 2.1 of the License, or (at your option) any later version.
8 
9     This library is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12     Lesser General Public License for more details.
13 
14     You should have received a copy of the GNU Lesser General Public
15     License along with this library; if not, write to the Free Software
16     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17 
18 
19 */
20 
21 /*
22     SDL_epocvideo.cpp
23     Epoc based SDL video driver implementation
24 
25     Markus Mertama
26 */
27 
28 
29 
30 #include "epoc_sdl.h"
31 
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 
36 extern "C" {
37 #include "SDL_error.h"
38 #include "SDL_timer.h"
39 #include "SDL_video.h"
40 #undef NULL
41 #include "SDL_pixels_c.h"
42 #include "SDL.h"
43 #include "SDL_mouse.h"
44 }
45 
46 #include "SDL_epocvideo.h"
47 #include "SDL_epocevents_c.h"
48 
49 
50 
51 #include <coedef.h>
52 #include <flogger.h>
53 
54 #include <eikenv.h>
55 #include <eikappui.h>
56 #include <eikapp.h>
57 #include "sdlepocapi.h"
58 #include <SDL_gliop.h>
59 #include <egl.h>
60 
61 #include "appsrv.h"
62 #ifdef WINS
63 #include "gles_wincw_def.h"
64 #else
65 #include "gles_armv5_def.h"
66 #endif
67 
68 
69 //extern TBuf<64> gLastError;
70 
71 ////////////////////////////////////////////////////////////////
72 
73 #ifdef DEBUG_TRACE_ENABLED
74 
75 #ifdef TRACE_TO_FILE
76 _LIT(KLibName, "SDL");
77 #endif
78 
79 
80 
RDebug_Print_b(char * error_str,void * param)81 void RDebug_Print_b(char* error_str, void* param)
82     {
83     TBuf8<128> error8((TUint8*)error_str);
84     TBuf<128> error;
85     error.Copy(error8);
86 
87 #ifndef TRACE_TO_FILE
88     if (param) //!! Do not work if the parameter is really 0!!
89         RDebug::Print(error, param);
90     else
91         RDebug::Print(error);
92 #else
93     if (param) //!! Do not work if the parameter is really 0!!
94         RFileLogger::WriteFormat(KLibName, _L("SDL.txt"), EFileLoggingModeAppend, error, param);
95     else
96         RFileLogger::Write(KLibName, _L("SDL.txt"), EFileLoggingModeAppend, error);
97 #endif
98 
99     }
100 
RDebug_Print(char * error_str,void * param)101 extern "C" void RDebug_Print(char* error_str, void* param)
102     {
103     RDebug_Print_b(error_str, param);
104     }
105 
106 #endif //DEBUG_TRACE_ENABLED
107 
108 /*
109 int Debug_AvailMem2()
110     {
111     //User::CompressAllHeaps();
112     TMemoryInfoV1Buf membuf;
113     User::LeaveIfError(UserHal::MemoryInfo(membuf));
114     TMemoryInfoV1 minfo = membuf();
115 	return(minfo.iFreeRamInBytes);
116     }
117 
118 extern "C" int Debug_AvailMem()
119     {
120     return(Debug_AvailMem2());
121     }
122 
123 */
124 
125 extern "C" {
126 
127 /* Initialization/Query functions */
128 
129 static int VideoInit(_THIS, SDL_PixelFormat *vformat);
130 static SDL_Rect** ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
131 static SDL_Surface* SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
132 static int SetColors(_THIS, int firstcolor, int ncolors,
133 			  SDL_Color *colors);
134 static void VideoQuit(_THIS);
135 
136 /* Hardware surface functions */
137 
138 static int AllocHWSurface(_THIS, SDL_Surface *surface);
139 static int LockHWSurface(_THIS, SDL_Surface *surface);
140 static int FlipHWSurface(_THIS, SDL_Surface *surface);
141 static void UnlockHWSurface(_THIS, SDL_Surface *surface);
142 static void FreeHWSurface(_THIS, SDL_Surface *surface);
143 static void DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
144 
145 static int Available(void);
146 static SDL_VideoDevice *CreateDevice(int devindex);
147 
148 void DrawBackground(_THIS);
149 void DirectDraw(_THIS, int numrects, SDL_Rect *rects, TUint16* screenBuffer);
150 void DirectDrawRotated(_THIS, int numrects, SDL_Rect *rects, TUint16* screenBuffer);
151 
152 /* Mouse functions */
153 
154 static WMcursor* CreateWMCursor(_THIS, Uint8 *data, Uint8 *mask, int w, int h, int hot_x, int hot_y);
155 static void FreeWMCursor(_THIS, WMcursor *cursor);
156 static int ShowWMCursor(_THIS, WMcursor *cursor);
157 
158 /*GL Functions*/
159 static int GlesLoadLibrary(_THIS, const char* path);
160 static void* GlesGetProcAddress(_THIS, const char *proc);
161 static int GlesGetAttribute(_THIS, SDL_GLattr attrib, int* value);
162 static int GlesMakeCurrent(_THIS);
163 static void GlesSwapBuffers(_THIS);
164 }
165 
166 
167 
168 extern "C"
169 	{
170 	struct WMcursor
171 		{
172 		};
173 	}
174 
175 /* Epoc video driver bootstrap functions */
176 
177 
Available(void)178 static int Available(void)
179     {
180     return 1; /* Always available */
181     }
182 
DeleteDevice(SDL_VideoDevice * device)183 static void DeleteDevice(SDL_VideoDevice *device)
184     {
185     User::Free(device->gl_data);
186 		User::Free(device->hidden);
187 		User::Free(device);
188     }
189 
CreateDevice(int)190 static SDL_VideoDevice* CreateDevice(int /*devindex*/)
191     {
192 	SDL_VideoDevice *device;
193 
194 	SDL_TRACE("SDL:S60_CreateDevice");
195 
196 	/* Allocate all variables that we free on delete */
197 	device = static_cast<SDL_VideoDevice*>(User::Alloc(sizeof(SDL_VideoDevice)));
198 	if (device)
199 	    {
200 		Mem::FillZ(device, (sizeof *device));
201 		device->hidden = static_cast<struct SDL_PrivateVideoData*>
202 				(User::Alloc((sizeof *device->hidden)));
203 		device->gl_data = static_cast<struct SDL_PrivateGLData*>
204 				(User::Alloc((sizeof (SDL_PrivateGLData))));
205 	    }
206 	 else
207 	 	{
208 	 	SDL_OutOfMemory();
209 	 	return 0;
210 	 	}
211 	if((device->hidden == NULL)  ||  (device->gl_data == NULL))
212 	    {
213 		SDL_OutOfMemory();
214 		User::Free(device->hidden);
215 		User::Free(device->gl_data);
216 		User::Free(device);
217 		return(0);
218 		}
219 
220 	Mem::FillZ(device->hidden, (sizeof *device->hidden));
221 
222 	/* Set the function pointers */
223 	device->VideoInit = VideoInit;
224 	device->ListModes = ListModes;
225 	device->SetVideoMode = SetVideoMode;
226 	device->SetColors = SetColors;
227 	device->UpdateRects = DirectUpdate;
228 	device->VideoQuit = VideoQuit;
229 	device->AllocHWSurface = AllocHWSurface;
230 	device->CheckHWBlit = NULL;
231 	device->FillHWRect = NULL;
232 	device->SetHWColorKey = NULL;
233 	device->SetHWAlpha = NULL;
234 	device->LockHWSurface = LockHWSurface;
235 	device->UnlockHWSurface = UnlockHWSurface;
236 	device->FlipHWSurface = FlipHWSurface;
237 	device->FreeHWSurface = FreeHWSurface;
238 	device->SetIcon = NULL;
239 	device->SetCaption = NULL;
240 	device->GetWMInfo = NULL;
241 	device->FreeWMCursor = FreeWMCursor;
242 	device->CreateWMCursor = CreateWMCursor;
243 	device->ShowWMCursor = ShowWMCursor;
244 	device->WarpWMCursor = NULL;
245 	device->InitOSKeymap = InitOSKeymap;
246 	device->PumpEvents = PumpEvents;
247 	device->free = DeleteDevice;
248 
249 	/*gles funtions*/
250 
251 	device->GL_LoadLibrary 		= GlesLoadLibrary;
252    	device->GL_GetProcAddress	= GlesGetProcAddress;
253    	device->GL_GetAttribute 	= GlesGetAttribute;
254    	device->GL_MakeCurrent 		= GlesMakeCurrent;
255    	device->GL_SwapBuffers 		= GlesSwapBuffers;
256 
257 	device->gl_data->iLibrary.SetHandle(0);
258 
259 	return device;
260 }
261 
262 
263 VideoBootStrap EPOC_bootstrap = {
264 	"epoc\0\0\0", "EPOC system",
265     Available, CreateDevice
266 };
267 
268 
269 
DisableKeyBlocking(_THIS)270 void DisableKeyBlocking(_THIS)
271     {
272     EpocSdlEnv::Request(EpocSdlEnv::EDisableKeyBlocking);
273     }
274 
275 
VideoInit(_THIS,SDL_PixelFormat * vformat)276 int VideoInit(_THIS, SDL_PixelFormat *vformat)
277 	{
278     /* Construct Epoc window */
279 
280 
281     /* Initialise Epoc frame buffer */
282 
283 	DisableKeyBlocking(_this); //disable key blocking
284 
285     const TDisplayMode displayMode = EpocSdlEnv::DisplayMode();
286 
287     /* The "best" video format should be returned to caller. */
288 
289     vformat->BitsPerPixel 	= TDisplayModeUtils::NumDisplayModeBitsPerPixel(displayMode);
290     vformat->BytesPerPixel  = TDisplayModeUtils::NumDisplayModeBitsPerPixel(displayMode) / 8;
291 
292 
293 
294     Private->iScreenPos = TPoint(0, 0);
295     const TSize sz = EpocSdlEnv::ScreenSize();
296 
297     Private->iRect.x = Private->iScreenPos.iX;
298     Private->iRect.y = Private->iScreenPos.iY;
299 
300 
301     if (sz.iHeight > sz.iWidth)
302         {
303         Private->iRect.h = sz.iWidth;
304 	    Private->iRect.w = sz.iHeight;
305     	}
306     else
307         {
308         Private->iRect.w = sz.iWidth;
309         Private->iRect.h = sz.iHeight;
310         }
311 	Private->iRectPtr = &Private->iRect;
312 
313 	/*_this->info->hw_available    = 0;
314 	_this->info->wm_available      = 0;
315 	_this->info->blit_hw           = 0;
316 	_this->info->blit_hw_CC        = 0;
317 	_this->info->blit_hw_A         = 0;
318 	_this->info->blit_sw           = 0;
319 	_this->info->blit_sw_CC        = 0;
320 	_this->info-> blit_sw_A        = 0;
321 	_this->info->blit_fill         = 0;
322 	_this->info->video_mem         = 0;  */
323 
324 	_this->info.current_w = sz.iWidth;
325 	_this->info.current_h = sz.iHeight;
326 
327 	return(0);
328 	}
329 
330 
ListModes(_THIS,SDL_PixelFormat * format,Uint32 flags)331 SDL_Rect **ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
332 	{
333     /*
334 	if(flags & SDL_HWSURFACE)
335 		{
336 		if(format->BytesPerPixel != 4) //in HW only full color is supported
337 			return NULL;
338 		}
339 	*/
340 	if(flags & SDL_FULLSCREEN)
341 		{
342 		return &Private->iRectPtr;
343 		}
344 
345     return (SDL_Rect **)(-1); //everythingisok, but too small shoes
346 	}
347 
348 
SetColors(_THIS,int firstcolor,int ncolors,SDL_Color * colors)349 int SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
350 	{
351 	if ((firstcolor + ncolors) > 256)
352 		return -1;
353 	TUint32 palette[256];
354 	const TDisplayMode mode = EpocSdlEnv::DisplayMode();
355     if(TDisplayModeUtils::NumDisplayModeColors(mode) == 4096)
356         {
357 	// Set 12 bit palette
358         for(int i = 0 ; i < ncolors; i++)
359             {
360 	        // 4k value: 0000 rrrr gggg bbbb
361 	        TUint32 color4K	 = (colors[i].r & 0x0000f0) << 4;
362 	        color4K			|= (colors[i].g & 0x0000f0);
363 	        color4K			|= (colors[i].b & 0x0000f0) >> 4;
364             palette[i + firstcolor] = color4K;
365             }
366         }
367     else if(TDisplayModeUtils::NumDisplayModeColors(mode) == 65536)
368         {
369         for(int i = 0; i < ncolors; i++)
370             {
371 			// 64k-colour displays effectively support RGB values
372 			// with 5 bits allocated to red, 6 to green and 5 to blue
373 			// 64k value: rrrr rggg gggb bbbb
374 	        TUint32 color64K = (colors[i].r & 0x0000f8) << 8;
375 	        color64K		|= (colors[i].g & 0x0000fc) << 3;
376 	        color64K		|= (colors[i].b & 0x0000f8) >> 3;
377             palette[i + firstcolor] = color64K;
378             }
379         }
380     else if(TDisplayModeUtils::NumDisplayModeColors(mode) == 16777216)
381         {
382         for(int i = 0; i < ncolors; i++)
383             {
384 			// 16M-colour
385             //0000 0000 rrrr rrrr gggg gggg bbbb bbbb
386 	        TUint32 color16M = colors[i].r << 16;
387 	        color16M		|= colors[i].g << 8;
388 	        color16M		|= colors[i].b;
389             palette[i + firstcolor] = color16M;
390             }
391         }
392     else
393         {
394         return -2;
395         }
396     if(EpocSdlEnv::SetPalette(firstcolor, ncolors, palette) == KErrNone)
397     	return 0;
398 	return -1;
399 	}
400 
401 
402 
403 
404 
GlesLoadLibrary(SDL_VideoDevice * _this,const char * path)405 int GlesLoadLibrary(SDL_VideoDevice* _this, const char* path)
406 	{
407 	if(_this->gl_data->iLibrary.Handle() != 0)
408 		return KErrAlreadyExists; //already loaded
409 	const char* const gles_lib[] = {"libgles_cm.dll", "libSWGLES.dll"};
410 	TInt err = KErrNotFound;
411 	for(TInt i = 0; i < 2 && err != KErrNone; i++)
412 		{
413 		const char* name8 = path == NULL ? gles_lib[i] : path;
414 		TFileName lib;
415 		lib.Copy(TPtrC8((unsigned char*)name8));
416 		err = _this->gl_data->iLibrary.Load(lib);
417 		}
418 	if(err == KErrNone)
419 		_this->gl_config.driver_loaded = 1;
420 	return err;
421 	}
422 
423 typedef TInt (*Ftp)(...);
424 #define DC(x) ((Ftp) GlesGetProcAddress(_this, #x))
425 
426 const char* const OpenGL[] = //these funtions are in gl, but not in gles, at least in all in all versions...
427 	{
428 	"glBegin",
429 	"glEnd",
430 	"glOrtho",
431 	"glPopAttrib",
432 	"glPopClientAttrib",
433 	"glPushAttrib",
434 	"glPushClientAttrib",
435 	"glTexCoord2f",
436 	"glVertex2i",
437 	"glTexParameteri"
438 	};
439 
NotSupported()440 TInt NotSupported()
441 	{
442 	User::Panic(_L("SDL, Gles"), KErrNotSupported);
443 	return 0;
444 	}
445 
GlesGetProcAddress(_THIS,const char * proc)446 void* GlesGetProcAddress(_THIS, const char *proc)
447 	{
448 	if(_this->gl_data->iLibrary.Handle() == 0)
449 		return NULL; //not loaded
450 	TLibraryFunction f = NULL;
451 	for(TInt i = 0; i < G_ordinals_count; i++)
452 		{
453 		if(strcmp(G_ordinals[i].name, proc) == 0)
454 			{
455 			f = _this->gl_data->iLibrary.Lookup(G_ordinals[i].ord);
456 			break;
457 			}
458 		}
459 
460 	if(f != NULL) /*Lookup may fail*/
461 		return  (void*) f;
462 
463 	for(TInt i = 0; i < sizeof(OpenGL) / sizeof(char*); i++)
464 		{
465 		if(strcmp(OpenGL[i], proc) == 0)
466 			return (void*) NotSupported;
467 		}
468 
469 	return NULL;
470 	}
471 
472 
473 
GlesGetAttribute(_THIS,SDL_GLattr aAttrib,int * aValue)474 int GlesGetAttribute(_THIS, SDL_GLattr aAttrib, int* aValue)
475 	{
476 	EGLint attrib = 0;
477 	switch(aAttrib)
478 		{
479 		/*todo*/
480 		case SDL_GL_RED_SIZE: attrib = EGL_RED_SIZE; break;
481 	    case SDL_GL_GREEN_SIZE: attrib = EGL_GREEN_SIZE; break;
482 	    case SDL_GL_BLUE_SIZE:attrib = EGL_BLUE_SIZE; break;
483 	    case SDL_GL_ALPHA_SIZE: attrib = EGL_ALPHA_SIZE; break;
484 	    case SDL_GL_BUFFER_SIZE: attrib = EGL_BUFFER_SIZE; break;
485 	    case SDL_GL_DOUBLEBUFFER: *aValue = 1; return 0; //always
486 	    case SDL_GL_DEPTH_SIZE: attrib = EGL_DEPTH_SIZE; break;
487 	    case SDL_GL_STENCIL_SIZE: attrib = EGL_STENCIL_SIZE; break;
488 	    case SDL_GL_ACCUM_RED_SIZE:
489 	    case SDL_GL_ACCUM_GREEN_SIZE:
490 	    case SDL_GL_ACCUM_BLUE_SIZE:
491 	    case SDL_GL_ACCUM_ALPHA_SIZE:
492 	    case SDL_GL_STEREO:
493 	    case SDL_GL_MULTISAMPLEBUFFERS:
494 	    case SDL_GL_MULTISAMPLESAMPLES:
495 	    case SDL_GL_ACCELERATED_VISUAL:
496 	    case SDL_GL_SWAP_CONTROL:
497 	    	*aValue = 0;
498 	    	return -1;
499 		}
500 	const int success = DC(eglGetConfigAttrib)
501 		(
502 		_this->gl_data->iDisplay,
503 		_this->gl_data->iConfig,
504 		attrib,
505 		aValue);
506 	return success == EGL_FALSE ? -1 : 0;
507 	}
508 
509 
GlesMakeCurrent(_THIS)510 int GlesMakeCurrent(_THIS)
511 	{
512 	DC(eglMakeCurrent)
513 	(_this->gl_data->iDisplay,
514 	_this->gl_data->iSurface,
515 	_this->gl_data->iSurface,
516 	_this->gl_data->iContext);
517 	return DC(eglGetError)();
518 	}
519 
GlesSwapBuffers(_THIS)520 void GlesSwapBuffers(_THIS)
521 	{
522 	DC(eglSwapBuffers)(
523 		_this->gl_data->iDisplay,
524 		_this->gl_data->iSurface);
525 	}
526 
GetDisplayMode(TInt aBitsPerPixel)527 TDisplayMode GetDisplayMode(TInt aBitsPerPixel)
528 	{
529 	const TDisplayMode displayMode = EpocSdlEnv::DisplayMode();
530 	TInt dmode = EColorLast;
531 	if(
532 	TDisplayModeUtils::NumDisplayModeBitsPerPixel(displayMode)
533 	== aBitsPerPixel)
534 		{
535 		dmode = displayMode;
536 		}
537 	else
538 		{
539 		--dmode;
540 		while(TDisplayModeUtils::IsDisplayModeColor(TDisplayMode(dmode)) &&
541 			TDisplayModeUtils::NumDisplayModeBitsPerPixel(TDisplayMode(dmode)) !=
542 			aBitsPerPixel)
543 			--dmode;
544 		}
545 	return TDisplayMode(dmode);
546 	}
547 
548 
glAssert(_THIS)549 LOCAL_C void glAssert(_THIS)
550 	{
551 	const EGLint err = DC(eglGetError)();
552 	if(err != EGL_SUCCESS)
553 		{
554 		User::Leave(err);
555 		}
556 	}
557 
CreateGles(_THIS,RWindow & aWindow,int aBpp,SDL_PrivateGLData & aData)558 LOCAL_C void CreateGles(_THIS, RWindow& aWindow, int aBpp, SDL_PrivateGLData& aData)
559 	{
560 	SDL_GL_LoadLibrary(NULL); //just if its not already loaded
561 	aData.iDisplay = DC(eglGetDisplay)(EGL_DEFAULT_DISPLAY);
562 	DC(eglInitialize)(aData.iDisplay, NULL, NULL);
563 
564 	glAssert(_this);
565 
566 	int configs = 0;
567 	EGLConfig* configList = NULL;
568 	int configSz = 0;
569 	DC(eglGetConfigs)(aData.iDisplay, configList, configSz, &configs);
570 	configSz = configs;
571 
572 	glAssert(_this);
573 
574 	configList = (EGLConfig*) User::Alloc(sizeof(EGLConfig) * configSz);
575 
576 #ifdef __WIN32__
577 	const TDisplayMode mode = aWindow.DisplayMode();
578 	const TInt sz = TDisplayModeUtils::NumDisplayModeBitsPerPixel(mode);
579 	aBpp;
580 #else
581 	TInt red, green, blue;
582 	if(aBpp == 16)
583 		{
584 		red = 5;
585 		green = 6;
586 		blue = 5;
587 		}
588 	else
589 		{
590 		red = 8;
591 		green = 8;
592 		blue = 8;
593 		}
594 #endif
595 
596 	const EGLint attribList[] =
597 		{
598 		EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
599 #ifdef __WIN32__
600 		EGL_BUFFER_SIZE, sz,
601 #else
602 		EGL_RED_SIZE, red,
603 		EGL_GREEN_SIZE, green,
604 		EGL_BLUE_SIZE, blue,
605 		EGL_BUFFER_SIZE, EGL_DONT_CARE,
606 #endif
607 		EGL_DEPTH_SIZE, 8,
608 	//	EGL_ALPHA_SIZE, 0,
609 		EGL_NONE
610 		};
611 
612 	DC(eglChooseConfig)(aData.iDisplay,
613 		 attribList,
614 		 configList,
615 		 configSz,
616 		 &configs);
617 
618 
619 	glAssert(_this);
620 
621 	__ASSERT_ALWAYS(configs > 0, User::Invariant());
622 
623 	aData.iConfig = configList[0];
624 
625 	User::Free(configList);
626 
627 	aData.iContext = DC(eglCreateContext)(aData.iDisplay,
628 		 	aData.iConfig,
629 		  	EGL_NO_CONTEXT,
630 		  	NULL);
631 
632 	glAssert(_this);
633 
634 	aData.iSurface	= DC(eglCreateWindowSurface)(aData.iDisplay,
635 		 aData.iConfig,
636 		 &aWindow,
637 		 NULL);
638 
639 	glAssert(_this);
640 
641 	}
642 
643 
DestroyGles(_THIS)644 LOCAL_C void DestroyGles(_THIS)
645 	{
646 	if(	_this->gl_config.driver_loaded)
647 		{
648 		DC(eglMakeCurrent)(_this->gl_data->iDisplay,
649 		EGL_NO_SURFACE,
650 		EGL_NO_SURFACE,
651 		EGL_NO_CONTEXT);
652 		DC(eglDestroySurface)(_this->gl_data->iDisplay, _this->gl_data->iSurface);
653 		DC(eglDestroyContext)(_this->gl_data->iDisplay, _this->gl_data->iContext);
654 		DC(eglTerminate)(_this->gl_data->iDisplay);
655 		_this->gl_data->iLibrary.Close();
656 		_this->gl_config.driver_loaded = 0;
657 		}
658 	}
659 
660 
SetVideoMode(_THIS,SDL_Surface * current,int width,int height,int bpp,Uint32 flags)661 SDL_Surface *SetVideoMode(_THIS, SDL_Surface *current,
662 				int width, int height, int bpp, Uint32 flags)
663 	{
664 
665 	if(EpocSdlEnv::Flags(CSDL::EAutoOrientation))
666 	        {
667 	      //  current_video->hidden->iFlags |= EAutoRotationOn;
668 	        EpocSdlEnv::AutoOrientation(TSize(width, height));
669 	      //  current_video->hidden->iFlags &= ~EAutoRotationOn;
670 	        }
671 
672 
673    	if(flags & SDL_OPENGL)
674    		{
675    	//	current_video->hidden->iFlags &= ~EContainerChange;
676 
677    		current->flags |= SDL_OPENGL;
678    		current->w = width;
679    		current->h = height;
680 
681 		RWindow* win = EpocSdlEnv::Window();
682 		EpocSdlEnv::ApplyGlesDsa();
683 
684 		CreateGles(_this, *win, bpp, *_this->gl_data);
685 
686 		const TSize sz(width, height);
687 		const TInt param = reinterpret_cast<TInt>(&sz);
688 		EpocSdlEnv::ObserverEvent(MSDLObserver::EEventScreenSurfaceCreated, param);
689 
690    		return current;
691    		}
692 
693    	if(EpocSdlEnv::Flags(CSDL::EAllowImageResize))
694    	    EpocSdlEnv::CreateZoomer(TSize(width, height));
695 
696    	const TSize screenSize = EpocSdlEnv::WindowSize();
697 
698 	if(width > screenSize.iWidth || height > screenSize.iHeight || width <= 0 || height <= 0)
699 	    {
700 	    if(flags & SDL_FULLSCREEN)
701 	        {
702 	        width = screenSize.iWidth;
703 	        height = screenSize.iHeight;
704 	        }
705 	    else
706 		    return NULL;
707 	    }
708 
709     if(current && current->pixels)
710     	{
711         current->pixels = NULL;
712     	}
713 
714 	if(!SDL_ReallocFormat(current, bpp, 0, 0, 0, 0))
715 	 	{
716 		return(NULL);
717 	 	}
718 
719 	current->flags = 0;
720 	if(width == screenSize.iWidth && height == screenSize.iHeight)
721 		current->flags |= SDL_FULLSCREEN;
722 
723 	const int numBytesPerPixel = ((bpp-1)>>3) + 1;
724 	current->pitch = numBytesPerPixel * width; // Number of bytes in scanline
725 
726     /* Set up the new mode framebuffer */
727    	current->flags |= SDL_PREALLOC;
728 
729    	if(bpp <= 8)
730    		current->flags |= SDL_HWPALETTE;
731 
732    	User::Free(Private->iSwSurface);
733    	current->pixels = NULL;
734    	Private->iSwSurface = NULL;
735 
736    	if(flags & SDL_HWSURFACE)
737    	    {
738    	    const TInt err = EpocSdlEnv::AllocHwSurface(TSize(width, height), GetDisplayMode(current->format->BitsPerPixel));
739    	    current->flags |= SDL_HWSURFACE;
740    	    current->pixels = NULL;
741    	    }
742    	else
743    	    {
744    	    if(!(current->flags & SDL_SWSURFACE) ||
745    	            Private->iSwSurfaceSize != TSize(width, height) ||
746    	            Private->iSwSurfaceDepth != numBytesPerPixel)
747    	        {
748    	        current->flags |= SDL_SWSURFACE;
749    	        const TInt surfacesize = width * height * numBytesPerPixel;
750    	        Private->iSwSurfaceSize = TSize(width, height);
751    	        Private->iSwSurfaceDepth = numBytesPerPixel;
752    	        delete Private->iSwSurface;
753    	        Private->iSwSurface = NULL;
754    	        current->pixels = (TUint8*) User::Alloc(surfacesize);
755    	        Private->iSwSurface = (TUint8*) current->pixels;
756    	        const TInt err = EpocSdlEnv::AllocSwSurface
757                 (TSize(width, height), GetDisplayMode(current->format->BitsPerPixel));
758    	        if(err != KErrNone || current->pixels == NULL)
759    	            return NULL;
760    	        }
761    	    }
762 
763 	current->w = width;
764 	current->h = height;
765 
766 
767 
768     /*
769      *  Logic for getting suitable screen dimensions, offset, scaling and orientation
770      */
771 
772 
773     /* Centralize game window on device screen  */
774 
775 
776     Private->iScreenPos.iX = Max(0, (screenSize.iWidth  - width)  / 2);
777     Private->iScreenPos.iY = Max(0, (screenSize.iHeight - height) / 2);
778 
779 
780     SDL_TRACE1("View width %d", width);
781     SDL_TRACE1("View height %d", height);
782     SDL_TRACE1("View bmode %d", bpp);
783     SDL_TRACE1("View x %d", Private->iScreenPos.iX);
784     SDL_TRACE1("View y %d", Private->iScreenPos.iY);
785 
786     const TPoint pos = EpocSdlEnv::WindowCoordinates(Private->iScreenPos);
787 
788     Private->iRect.x = pos.iX;
789     Private->iRect.y = pos.iY;
790 
791     const TSize winSz = EpocSdlEnv::WindowSize();
792 
793     Private->iRect.w = winSz.iWidth;
794     Private->iRect.h = winSz.iHeight;
795 
796 
797     EpocSdlEnv::EnableDraw();
798     EpocSdlEnv::ResumeDsa();
799 
800 	EpocSdlEnv::LockPalette(EFalse);
801 
802 	Private->iFlags |= EFocusedWindow;
803 
804 	const TSize sz(width, height);
805 	const TInt param = reinterpret_cast<TInt>(&sz);
806 	EpocSdlEnv::ObserverEvent(MSDLObserver::EEventScreenSurfaceCreated, param);
807 
808 
809 //	current_video->hidden->iFlags &= ~EContainerChange;
810 	/* We're done */
811 	return(current);
812 }
813 
814 
815 
AllocHWSurface(_THIS,SDL_Surface * surface)816 static int AllocHWSurface(_THIS, SDL_Surface* surface)
817 	{
818 	if(KErrNone == EpocSdlEnv::AllocHwSurface(TSize(surface->w, surface->h), GetDisplayMode(surface->format->BitsPerPixel)))
819 	    {
820 	    Private->iFlags |= EFocusedWindow;
821 	    return true;
822 	    }
823 	return false;
824 	}
825 
FreeHWSurface(_THIS,SDL_Surface *)826 static void FreeHWSurface(_THIS, SDL_Surface* /*surface*/)
827 	{
828 	}
829 
LockHWSurface(_THIS,SDL_Surface * surface)830 static int LockHWSurface(_THIS, SDL_Surface* surface)
831 	{
832 	if(EpocSdlEnv::IsDsaAvailable())
833 		{
834 		TUint8* address = EpocSdlEnv::LockHwSurface();
835 		if(address != NULL)
836 			{
837 			surface->pixels = address;
838 			return 1;
839 			}
840 		}
841 	return 0;
842 	}
UnlockHWSurface(_THIS,SDL_Surface *)843 static void UnlockHWSurface(_THIS, SDL_Surface* /*surface*/)
844 	{
845 	EpocSdlEnv::UnlockHwSurface();
846 	}
847 
FlipHWSurface(_THIS,SDL_Surface *)848 static int FlipHWSurface(_THIS, SDL_Surface* /*surface*/)
849 	{
850 	if(EpocSdlEnv::IsDsaAvailable())
851 	    {
852 	    EpocSdlEnv::Request(CSdlAppServ::EVideoUpdate);
853 	    return true;
854 	    }
855 	return false;
856 	}
857 
RequestVideoUpdate(_THIS,int numrects,SDL_Rect * rects)858 static void RequestVideoUpdate(_THIS, int numrects, SDL_Rect* rects)
859     {
860     if(numrects > 0)
861         {
862         Private->iUpdateRect = rects[0];
863         for(int i = 1; i < numrects; i++)
864             {
865             const SDL_Rect& r = rects[0];
866             if(r.x < Private->iUpdateRect.x)
867                 Private->iUpdateRect.x = r.x;
868             if((r.x + r.w) >  Private->iUpdateRect.x + Private->iUpdateRect.w)
869                 Private->iUpdateRect.w = r.w;
870             if(r.y < Private->iUpdateRect.y)
871                 Private->iUpdateRect.y = r.y;
872             if((r.y + r.h) >  Private->iUpdateRect.y + Private->iUpdateRect.h)
873                 Private->iUpdateRect.h = r.h;
874             }
875         EpocSdlEnv::Request(CSdlAppServ::EVideoUpdate);
876         }
877     }
878 
DirectUpdate(_THIS,int numrects,SDL_Rect * rects)879 static void DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
880 	{
881 	if(!EpocSdlEnv::IsVideoThread())
882 	    {
883         RequestVideoUpdate(_this, numrects, rects);
884 	    return;
885 	    }
886 
887 	if(EpocSdlEnv::IsDsaAvailable())
888 		{
889 		const TSize screenSize = EpocSdlEnv::WindowSize();
890 		if(screenSize.iWidth < SDL_VideoSurface->w || screenSize.iHeight <  SDL_VideoSurface->h)
891 		    return; // video surface dimenstions do not match
892 
893 		if(Private->iSwSurface /*&& 0 == (current_video->hidden->iFlags & EContainerChange)*/)
894 		    {
895 		    const TRect target(Private->iScreenPos, Private->iSwSurfaceSize);
896 		    for(TInt i = 0; i < numrects ;i++)
897 		    	{
898 		    	const TRect rect(TPoint(rects[i].x, rects[i].y),
899 		    		TSize(rects[i].w, rects[i].h));
900 		    	if(!EpocSdlEnv::AddUpdateRect(Private->iSwSurface, rect, target))
901 		    	    {
902 		    		return; //not succesful
903 		    	    }
904 		    	}
905 	        EpocSdlEnv::UpdateSwSurface();
906 		    }
907 		else
908 		    {
909 		    EpocSdlEnv::UpdateHwSurface();
910 		    }
911 		EpocSdlEnv::PostUpdate();
912 		}
913 	//gLastError = _L("out du");
914 	}
915 
916 
917 /* Note:  If we are terminated, this could be called in the middle of
918    another SDL video routine -- notably UpdateRects.
919 */
VideoQuit(_THIS)920 void VideoQuit(_THIS)
921 	{
922 
923 	if(_this->gl_data)
924 		DestroyGles(_this);
925 
926 	User::Free(Private->iSwSurface);
927 	Private->iSwSurface = NULL;
928 	}
929 
930 
CreateWMCursor(_THIS,Uint8 *,Uint8 *,int,int,int,int)931 WMcursor *CreateWMCursor(_THIS, Uint8* /*data*/, Uint8* /*mask*/, int /*w*/, int /*h*/, int /*hot_x*/, int /*hot_y*/)
932     {
933     return (WMcursor*) 1; //hii! prevents SDL to view a std cursor
934     }
935 
FreeWMCursor(_THIS,WMcursor *)936 void FreeWMCursor(_THIS, WMcursor* /*cursor*/)
937     {
938     }
939 
ShowWMCursor(_THIS,WMcursor *)940 int ShowWMCursor(_THIS, WMcursor* /*cursor*/)
941     {
942     return true;
943     }
944 
PrepareContainerWindowChanged()945 void PrepareContainerWindowChanged()
946     {
947  /*   if(current_video)
948         {
949         current_video->hidden->iFlags |= EContainerChange;
950         } */
951     }
952 
ContainerWindowChanged()953 void ContainerWindowChanged()
954     {
955     if(current_video && SDL_VideoSurface)
956         {
957   /*      if((current_video->hidden->iFlags & EContainerChange) &&
958             !(current_video->hidden->iFlags & EAutoRotationOn) &&
959             (SDL_VideoSurface->flags & SDL_FULLSCREEN))
960             {
961             const TSize screenSize = EpocSdlEnv::WindowSize();
962             if(current_video->info.current_w != screenSize.iWidth || current_video->info.current_h != screenSize.iHeight)
963                 {
964                 current_video->info.current_w = screenSize.iWidth;
965                 current_video->info.current_h = screenSize.iHeight;
966                 SDL_SetVideoMode(screenSize.iWidth, screenSize.iHeight, 0, SDL_VideoSurface->flags);
967                 }
968             }
969         current_video->hidden->iFlags &= ~EContainerChange;*/
970         SDL_UpdateRect(SDL_VideoSurface, 0, 0, SDL_VideoSurface->w, SDL_VideoSurface->h);
971         }
972     }
973 
974 /*FOR GL comp*/
975 
glBegin(GLenum)976 void glBegin(GLenum /*a*/) {}
glEnd(void)977 void glEnd(void) {}
glOrtho(GLdouble,GLdouble,GLdouble,GLdouble,GLdouble,GLdouble)978 void glOrtho(GLdouble /*l*/, GLdouble /*r*/, GLdouble /*b*/, GLdouble /*t*/, GLdouble /*n*/, GLdouble /*f*/) {}
glPopAttrib(void)979 void glPopAttrib(void) {}
glPopClientAttrib(void)980 void glPopClientAttrib(void){}
glPushAttrib(GLbitfield)981 void glPushAttrib(GLbitfield /*mask*/) {}
glPushClientAttrib(GLbitfield)982 void glPushClientAttrib(GLbitfield /*mask*/) {}
glTexCoord2f(GLfloat,GLfloat)983 void glTexCoord2f(GLfloat /*s*/, GLfloat /*t*/) {}
glVertex2i(GLint,GLint)984 void glVertex2i(GLint /*x*/, GLint /*y*/) {}
985 
986 /*
987  * Video update goes around problem that video update cannot be called from other thread
988  * that it is created. As timer works in its own window SDL_Update rect called from that
989  * callback reroutes update request from appserv AO
990  * */
991 
992 
VideoUpdate()993 void VideoUpdate()
994     {
995     DirectUpdate(current_video, 1, &current_video->hidden->iUpdateRect);
996     }
997 
998 
999 
1000