2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997, 1998, 1999, 2000, 2001 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 slouken@devolution.com
29 #include "SDL_error.h"
30 #include "SDL_timer.h"
31 #include "SDL_video.h"
33 #include "SDL_pixels_c.h"
35 #include "SDL_mouse.h"
38 #include "SDL_epocvideo.h"
39 #include "SDL_epocevents_c.h"
45 #include "sdlepocapi.h"
46 #include <SDL_gliop.h>
48 #include "gles_armv5_def.h"
50 _LIT(KLibName
, "SDL");
54 /* Initialization/Query functions */
56 static int S60_VideoInit(_THIS
, SDL_PixelFormat
*vformat
);
57 static SDL_Rect
**S60_ListModes(_THIS
, SDL_PixelFormat
*format
, Uint32 flags
);
58 static SDL_Surface
*S60_SetVideoMode(_THIS
, SDL_Surface
*current
, int width
, int height
, int bpp
, Uint32 flags
);
59 static int S60_SetColors(_THIS
, int firstcolor
, int ncolors
,
61 static void S60_VideoQuit(_THIS
);
63 /* Hardware surface functions */
65 static int S60_AllocHWSurface(_THIS
, SDL_Surface
*surface
);
66 static int S60_LockHWSurface(_THIS
, SDL_Surface
*surface
);
67 static int S60_FlipHWSurface(_THIS
, SDL_Surface
*surface
);
68 static void S60_UnlockHWSurface(_THIS
, SDL_Surface
*surface
);
69 static void S60_FreeHWSurface(_THIS
, SDL_Surface
*surface
);
70 static void S60_DirectUpdate(_THIS
, int numrects
, SDL_Rect
*rects
);
72 static int S60_Available(void);
73 static SDL_VideoDevice
*S60_CreateDevice(int devindex
);
75 void DrawBackground(_THIS
);
76 void DirectDraw(_THIS
, int numrects
, SDL_Rect
*rects
, TUint16
* screenBuffer
);
77 void DirectDrawRotated(_THIS
, int numrects
, SDL_Rect
*rects
, TUint16
* screenBuffer
);
81 static WMcursor
*S60_CreateWMCursor(_THIS
, Uint8
*data
, Uint8
*mask
, int w
, int h
, int hot_x
, int hot_y
);
82 static void S60_FreeWMCursor(_THIS
, WMcursor
*cursor
);
83 static int S60_ShowWMCursor(_THIS
, WMcursor
*cursor
);
86 static int S60_GlesLoadLibrary(_THIS
, const char* path
);
87 static void* S60_GlesGetProcAddress(_THIS
, const char *proc
);
88 static int S60_GlesGetAttribute(_THIS
, SDL_GLattr attrib
, int* value
);
89 static int S60_GlesMakeCurrent(_THIS
);
90 static void S60_GlesSwapBuffers(_THIS
);
100 static TSize
GetScreenSize()
102 CWsScreenDevice
*sd
= new CWsScreenDevice(CEikonEnv::Static()->WsSession());
104 const TSize
sz(sd
->SizeInPixels());
110 /* Epoc video driver bootstrap functions */
111 static int S60_Available(void)
113 return 1; /* Always available */
116 static void S60_DeleteDevice(SDL_VideoDevice
*device
)
118 delete device
->gl_data
;
119 delete device
->hidden
;
123 static SDL_VideoDevice
*S60_CreateDevice(int /*devindex*/)
125 SDL_VideoDevice
*device
;
127 /* Allocate all variables that we free on delete */
128 device
= new SDL_VideoDevice
;
131 Mem::FillZ(device
, (sizeof *device
));
132 device
->hidden
= new SDL_PrivateVideoData
;
133 device
->gl_data
= new SDL_PrivateGLData
;
140 if((device
->hidden
== NULL
) || (device
->gl_data
== NULL
))
143 delete device
->hidden
;
144 delete device
->gl_data
;
149 Mem::FillZ(device
->hidden
, (sizeof *device
->hidden
));
151 /* Set the function pointers */
152 device
->VideoInit
= S60_VideoInit
;
153 device
->ListModes
= S60_ListModes
;
154 device
->SetVideoMode
= S60_SetVideoMode
;
155 device
->SetColors
= S60_SetColors
;
156 device
->UpdateRects
= NULL
;
157 device
->VideoQuit
= S60_VideoQuit
;
158 device
->AllocHWSurface
= S60_AllocHWSurface
;
159 device
->CheckHWBlit
= NULL
;
160 device
->FillHWRect
= NULL
;
161 device
->SetHWColorKey
= NULL
;
162 device
->SetHWAlpha
= NULL
;
163 device
->LockHWSurface
= S60_LockHWSurface
;
164 device
->UnlockHWSurface
= S60_UnlockHWSurface
;
165 device
->FlipHWSurface
= S60_FlipHWSurface
;
166 device
->FreeHWSurface
= S60_FreeHWSurface
;
167 device
->SetIcon
= NULL
;
168 device
->SetCaption
= NULL
;
169 device
->GetWMInfo
= NULL
;
170 device
->FreeWMCursor
= S60_FreeWMCursor
;
171 device
->CreateWMCursor
= S60_CreateWMCursor
;
172 device
->ShowWMCursor
= S60_ShowWMCursor
;
173 device
->WarpWMCursor
= NULL
;
174 device
->InitOSKeymap
= EPOC_InitOSKeymap
;
175 device
->PumpEvents
= EPOC_PumpEvents
;
176 device
->free
= S60_DeleteDevice
;
179 device
->GL_LoadLibrary
= S60_GlesLoadLibrary
;
180 device
->GL_GetProcAddress
= S60_GlesGetProcAddress
;
181 device
->GL_GetAttribute
= S60_GlesGetAttribute
;
182 device
->GL_MakeCurrent
= S60_GlesMakeCurrent
;
183 device
->GL_SwapBuffers
= S60_GlesSwapBuffers
;
185 device
->gl_data
->iLibrary
.SetHandle(0);
190 VideoBootStrap EPOC_bootstrap
= {
191 "epoc\0\0\0", "EPOC system",
192 S60_Available
, S60_CreateDevice
195 int S60_VideoInit(_THIS
, SDL_PixelFormat
*vformat
)
197 /* Initialise Epoc frame buffer */
199 const TDisplayMode displayMode
= EpocSdlEnv::DisplayMode();
201 /* The "best" video format should be returned to caller. */
203 vformat
->BitsPerPixel
= TDisplayModeUtils::NumDisplayModeBitsPerPixel(displayMode
);
204 vformat
->BytesPerPixel
= TDisplayModeUtils::NumDisplayModeBitsPerPixel(displayMode
) / 8;
206 Private
->iRect
= new SDL_Rect
*[3];
207 Private
->iRect
[0] = new SDL_Rect
;
208 Private
->iRect
[1] = new SDL_Rect
;
209 Private
->iRect
[2] = NULL
;
211 Private
->iScreenPos
= TPoint(0, 0);
213 Private
->iRect
[0]->x
= Private
->iScreenPos
.iX
;
214 Private
->iRect
[0]->y
= Private
->iScreenPos
.iY
;
216 Private
->iRect
[1]->x
= Private
->iScreenPos
.iX
;
217 Private
->iRect
[1]->y
= Private
->iScreenPos
.iY
;
219 const TSize sz
= GetScreenSize();
221 Private
->iRect
[0]->w
= sz
.iWidth
;
222 Private
->iRect
[0]->h
= sz
.iHeight
;
224 Private
->iRect
[1]->w
= sz
.iHeight
;
225 Private
->iRect
[1]->h
= sz
.iWidth
;
227 Private
->iOrientation
= new CAknAppUi::TAppUiOrientation
[2];
229 if(sz
.iWidth
< sz
.iHeight
)
231 Private
->iOrientation
[0] = CAknAppUi::EAppUiOrientationPortrait
;
232 Private
->iOrientation
[1] = CAknAppUi::EAppUiOrientationLandscape
;
236 Private
->iOrientation
[0] = CAknAppUi::EAppUiOrientationLandscape
;
237 Private
->iOrientation
[1] = CAknAppUi::EAppUiOrientationPortrait
;
243 SDL_Rect
**S60_ListModes(_THIS
, SDL_PixelFormat
*format
, Uint32 flags
)
245 if(flags
& SDL_HWSURFACE
)
247 if(format
->BytesPerPixel
!= 4) //in HW only full color is supported
250 if(flags
& SDL_FULLSCREEN
)
252 return Private
->iRect
;
255 return (SDL_Rect
**)(-1); //everythingisok, but too small shoes
258 int S60_SetColors(_THIS
, int firstcolor
, int ncolors
, SDL_Color
*colors
)
260 if ((firstcolor
+ncolors
) > 256)
262 TUint32 palette
[256];
263 const TDisplayMode mode
= EpocSdlEnv::DisplayMode();
266 if(TDisplayModeUtils::NumDisplayModeColors(mode
) == 4096)
268 // Set 12 bit palette
269 for(int i
= firstcolor
; i
< firstcolor
+ncolors
; i
++)
271 // 4k value: 0000 rrrr gggg bbbb
272 TUint32 color4K
= (colors
[c
].r
& 0x0000f0) << 4;
273 color4K
|= (colors
[c
].g
& 0x0000f0);
274 color4K
|= (colors
[c
].b
& 0x0000f0) >> 4;
275 palette
[i
] = color4K
;
279 else if(TDisplayModeUtils::NumDisplayModeColors(mode
) == 65536)
281 for(int i
= firstcolor
; i
< firstcolor
+ncolors
; i
++)
283 // 64k-colour displays effectively support RGB values
284 // with 5 bits allocated to red, 6 to green and 5 to blue
285 // 64k value: rrrr rggg gggb bbbb
286 TUint32 color64K
= (colors
[c
].r
& 0x0000f8) << 8;
287 color64K
|= (colors
[c
].g
& 0x0000fc) << 3;
288 color64K
|= (colors
[c
].b
& 0x0000f8) >> 3;
289 palette
[i
] = color64K
;
293 else if(TDisplayModeUtils::NumDisplayModeColors(mode
) == 16777216)
295 for(int i
= firstcolor
; i
< firstcolor
+ncolors
; i
++)
298 //0000 0000 rrrr rrrr gggg gggg bbbb bbbb
299 TUint32 color16M
= colors
[c
].r
<< 16;
300 color16M
|= colors
[c
].g
<< 8;
301 color16M
|= colors
[c
].b
;
302 palette
[i
] = color16M
;
310 if(EpocSdlEnv::SetPalette(firstcolor
, ncolors
, palette
) == KErrNone
)
312 // palette has been updated, redraw whole screen to include changes
313 EpocSdlEnv::UpdateWholeScreen(true);
320 int S60_GlesLoadLibrary(SDL_VideoDevice
* _this
, const char* path
)
322 if(_this
->gl_data
->iLibrary
.Handle() != 0)
323 return KErrAlreadyExists
; //already loaded
324 const char* const gles_lib
[] = {"libgles_cm.dll", "libSWGLES.dll"};
325 int err
= KErrNotFound
;
326 for(int i
= 0; i
< 2 && err
!= KErrNone
; i
++)
328 const char* name8
= path
== NULL
? gles_lib
[i
] : path
;
330 lib
.Copy(TPtrC8((unsigned char*)name8
));
331 err
= _this
->gl_data
->iLibrary
.Load(lib
);
334 _this
->gl_config
.driver_loaded
= 1;
338 typedef int (*Ftp
)(...);
339 #define DC(x) ((Ftp) S60_GlesGetProcAddress(_this, #x))
341 const char* const OpenGL
[] = //these funtions are in gl, but not in gles, at least in all in all versions...
349 "glPushClientAttrib",
357 User::Panic(_L("SDL, Gles"), KErrNotSupported
);
361 void* S60_GlesGetProcAddress(_THIS
, const char *proc
)
363 if(_this
->gl_data
->iLibrary
.Handle() == 0)
364 return NULL
; //not loaded
365 TLibraryFunction f
= NULL
;
366 for(int i
= 0; i
< G_ordinals_count
; i
++)
368 if(strcmp(G_ordinals
[i
].name
, proc
) == 0)
370 f
= _this
->gl_data
->iLibrary
.Lookup(G_ordinals
[i
].ord
);
375 if(f
!= NULL
) /*Lookup may fail*/
378 for(int i
= 0; i
< sizeof(OpenGL
) / sizeof(char*); i
++)
380 if(strcmp(OpenGL
[i
], proc
) == 0)
381 return (void*) NotSupported
;
387 int S60_GlesGetAttribute(_THIS
, SDL_GLattr aAttrib
, int* aValue
)
393 case SDL_GL_RED_SIZE
: attrib
= EGL_RED_SIZE
; break;
394 case SDL_GL_GREEN_SIZE
: attrib
= EGL_GREEN_SIZE
; break;
395 case SDL_GL_BLUE_SIZE
:attrib
= EGL_BLUE_SIZE
; break;
396 case SDL_GL_ALPHA_SIZE
: attrib
= EGL_ALPHA_SIZE
; break;
397 case SDL_GL_BUFFER_SIZE
: attrib
= EGL_BUFFER_SIZE
; break;
398 case SDL_GL_DOUBLEBUFFER
: *aValue
= 1; return 0; //always
399 case SDL_GL_DEPTH_SIZE
: attrib
= EGL_DEPTH_SIZE
; break;
400 case SDL_GL_STENCIL_SIZE
: attrib
= EGL_STENCIL_SIZE
; break;
401 case SDL_GL_ACCUM_RED_SIZE
:
402 case SDL_GL_ACCUM_GREEN_SIZE
:
403 case SDL_GL_ACCUM_BLUE_SIZE
:
404 case SDL_GL_ACCUM_ALPHA_SIZE
:
406 case SDL_GL_MULTISAMPLEBUFFERS
:
407 case SDL_GL_MULTISAMPLESAMPLES
:
408 case SDL_GL_ACCELERATED_VISUAL
:
409 case SDL_GL_SWAP_CONTROL
:
413 const int success
= DC(eglGetConfigAttrib
)
415 _this
->gl_data
->iDisplay
,
416 _this
->gl_data
->iConfig
,
419 return success
== EGL_FALSE
? -1 : 0;
423 int S60_GlesMakeCurrent(_THIS
)
426 (_this
->gl_data
->iDisplay
,
427 _this
->gl_data
->iSurface
,
428 _this
->gl_data
->iSurface
,
429 _this
->gl_data
->iContext
);
430 return DC(eglGetError
)();
433 void S60_GlesSwapBuffers(_THIS
)
436 _this
->gl_data
->iDisplay
,
437 _this
->gl_data
->iSurface
);
440 TDisplayMode
GetDisplayMode(int aBitsPerPixel
)
442 const TDisplayMode displayMode
= EpocSdlEnv::DisplayMode();
443 int dmode
= EColorLast
;
444 if(TDisplayModeUtils::NumDisplayModeBitsPerPixel(displayMode
) == aBitsPerPixel
)
454 while(TDisplayModeUtils::IsDisplayModeColor(TDisplayMode(dmode
)) &&
455 TDisplayModeUtils::NumDisplayModeBitsPerPixel(TDisplayMode(dmode
)) != aBitsPerPixel
);
457 return TDisplayMode(dmode
);
461 LOCAL_C
void glAssert(_THIS
)
463 const EGLint err
= DC(eglGetError
)();
464 if(err
!= EGL_SUCCESS
)
470 LOCAL_C
void CreateGles(_THIS
, RWindow
& aWindow
, int bpp
, SDL_PrivateGLData
& aData
)
472 SDL_GL_LoadLibrary(NULL
); //just if its not already loaded
473 aData
.iDisplay
= DC(eglGetDisplay
)(EGL_DEFAULT_DISPLAY
);
474 DC(eglInitialize
)(aData
.iDisplay
, NULL
, NULL
);
479 EGLConfig
* configList
= NULL
;
481 DC(eglGetConfigs
)(aData
.iDisplay
, configList
, configSz
, &configs
);
486 configList
= new EGLConfig
[configSz
];
488 int red
, green
, blue
;
502 const EGLint attribList
[] =
504 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
506 EGL_GREEN_SIZE
, green
,
508 EGL_BUFFER_SIZE
, EGL_DONT_CARE
,
513 DC(eglChooseConfig
)(aData
.iDisplay
,
522 __ASSERT_ALWAYS(configs
> 0, User::Invariant());
524 aData
.iConfig
= configList
[0];
528 aData
.iContext
= DC(eglCreateContext
)(aData
.iDisplay
,
535 aData
.iSurface
= DC(eglCreateWindowSurface
)(aData
.iDisplay
,
544 LOCAL_C
void DestroyGles(_THIS
)
546 if( _this
->gl_config
.driver_loaded
)
548 DC(eglMakeCurrent
)(_this
->gl_data
->iDisplay
,
552 DC(eglDestroySurface
)(_this
->gl_data
->iDisplay
, _this
->gl_data
->iSurface
);
553 DC(eglDestroyContext
)(_this
->gl_data
->iDisplay
, _this
->gl_data
->iContext
);
554 DC(eglTerminate
)(_this
->gl_data
->iDisplay
);
555 _this
->gl_data
->iLibrary
.Close();
556 _this
->gl_config
.driver_loaded
= 0;
560 SDL_Surface
*S60_SetVideoMode(_THIS
, SDL_Surface
*current
, int width
, int height
, int bpp
, Uint32 flags
)
562 if(flags
& SDL_OPENGL
)
564 current
->flags
|= SDL_OPENGL
;
568 RWindow
* win
= EpocSdlEnv::Window();
569 EpocSdlEnv::ApplyGlesDsa();
571 CreateGles(_this
, *win
, bpp
, *_this
->gl_data
);
576 if(flags
& SDL_HWSURFACE
)
579 // check orientation and resolution validity
580 bool resValid
= false;
581 Orientation orientation
= CAknAppUi::EAppUiOrientationUnspecified
;
582 for(int i
=0; i
<2; i
++)
584 if(width
<= Private
->iRect
[i
]->w
&& height
<= Private
->iRect
[i
]->h
)
586 orientation
= Private
->iOrientation
[i
];
594 if(!SDL_ReallocFormat(current
, bpp
, 0, 0, 0, 0))
597 const int numBytesPerPixel
= ((bpp
-1)>>3) + 1;
598 current
->pitch
= numBytesPerPixel
* width
;
600 // set proper surface flags
601 current
->flags
= SDL_SWSURFACE
| SDL_PREALLOC
;
602 if(flags
& SDL_FULLSCREEN
)
603 current
->flags
|= SDL_FULLSCREEN
;
604 if(flags
& SDL_RESIZABLE
)
605 current
->flags
|= SDL_RESIZABLE
;
607 current
->flags
|= SDL_HWPALETTE
;
613 const int surfacesize
= width
* height
* numBytesPerPixel
;
614 Private
->iSwSurfaceSize
= TSize(width
, height
);
616 delete[] (TUint8
*)current
->pixels
;
617 current
->pixels
= new TUint8
[surfacesize
];
618 Private
->iSwSurface
= (TUint8
*) current
->pixels
;
620 if(current
->pixels
== NULL
)
623 const TSize sz
= GetScreenSize();
625 if(sz
.iWidth
< sz
.iHeight
&& orientation
!= CAknAppUi::EAppUiOrientationPortrait
||
626 sz
.iWidth
> sz
.iHeight
&& orientation
!= CAknAppUi::EAppUiOrientationLandscape
)
628 EpocSdlEnv::SetOrientation(orientation
, TSize(width
, height
), GetDisplayMode(current
->format
->BitsPerPixel
));
632 if((flags
& SDL_RESIZABLE
) == 0)
634 EpocSdlEnv::SetOrientation(orientation
, TSize(width
, height
), GetDisplayMode(current
->format
->BitsPerPixel
));
637 const int err
= EpocSdlEnv::AllocSurface(TSize(width
, height
), GetDisplayMode(current
->format
->BitsPerPixel
));
642 // Set the blit function
643 _this
->UpdateRects
= S60_DirectUpdate
;
645 // Centralize game window on device screen
646 Private
->iScreenPos
.iX
= 0;
647 Private
->iScreenPos
.iY
= 0;
652 static int S60_AllocHWSurface(_THIS
, SDL_Surface
* surface
)
654 return KErrNone
== EpocSdlEnv::AllocSurface(TSize(surface
->w
, surface
->h
), GetDisplayMode(surface
->format
->BitsPerPixel
));
657 static void S60_FreeHWSurface(_THIS
, SDL_Surface
* /*surface*/)
661 static int S60_LockHWSurface(_THIS
, SDL_Surface
* surface
)
663 if(EpocSdlEnv::IsDsaAvailable())
665 TUint8
* address
= EpocSdlEnv::LockHwSurface();
668 surface
->pixels
= address
;
674 static void S60_UnlockHWSurface(_THIS
, SDL_Surface
* /*surface*/)
676 EpocSdlEnv::UnlockHwSurface();
679 static int S60_FlipHWSurface(_THIS
, SDL_Surface
* /*surface*/)
684 static void S60_DirectUpdate(_THIS
, int numrects
, SDL_Rect
*rects
)
686 if(EpocSdlEnv::IsDsaAvailable())
688 if(Private
->iSwSurface
)
690 const TRect
target(TPoint(0, 0), Private
->iSwSurfaceSize
);
692 if(EpocSdlEnv::GetUpdateWholeScreen())
694 if(!EpocSdlEnv::AddUpdateRect(Private
->iSwSurface
, target
, target
))
699 for(int i
= 0; i
< numrects
;i
++)
701 const TRect
rect(TPoint(rects
[i
].x
, rects
[i
].y
),
702 TSize(rects
[i
].w
, rects
[i
].h
));
703 if(!EpocSdlEnv::AddUpdateRect(Private
->iSwSurface
, rect
, target
))
704 return; //not succesful
705 EpocSdlEnv::UpdateWholeScreen(false);
708 EpocSdlEnv::UpdateSwSurface();
715 EpocSdlEnv::WaitDsaAvailable();
719 /* Note: If we are terminated, this could be called in the middle of
720 another SDL video routine -- notably UpdateRects.
722 void S60_VideoQuit(_THIS
)
724 delete[] Private
->iOrientation
;
726 delete Private
->iRect
[0];
727 delete Private
->iRect
[1];
728 delete[] Private
->iRect
;
733 delete[] Private
->iSwSurface
;
734 Private
->iSwSurface
= NULL
;
735 EpocSdlEnv::FreeSurface();
739 WMcursor
*S60_CreateWMCursor(_THIS
, Uint8
* /*data*/, Uint8
* /*mask*/, int /*w*/, int /*h*/, int /*hot_x*/, int /*hot_y*/)
741 // prevents SDL from displaying standard cursor
742 return (WMcursor
*) 1;
745 void S60_FreeWMCursor(_THIS
, WMcursor
* /*cursor*/)
749 int S60_ShowWMCursor(_THIS
, WMcursor
*cursor
)
756 void glBegin(GLenum a
) {}
758 void glOrtho(GLdouble l
, GLdouble r
, GLdouble b
, GLdouble t
, GLdouble n
, GLdouble f
) {}
759 void glPopAttrib(void) {}
760 void glPopClientAttrib(void){}
761 void glPushAttrib(GLbitfield mask
) {}
762 void glPushClientAttrib(GLbitfield mask
) {}
763 void glTexCoord2f(GLfloat s
, GLfloat t
) {}
764 void glVertex2i(GLint x
, GLint y
) {}