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
);
76 static WMcursor
*S60_CreateWMCursor(_THIS
, Uint8
*data
, Uint8
*mask
, int w
, int h
, int hot_x
, int hot_y
);
77 static void S60_FreeWMCursor(_THIS
, WMcursor
*cursor
);
78 static int S60_ShowWMCursor(_THIS
, WMcursor
*cursor
);
81 static int S60_GlesLoadLibrary(_THIS
, const char* path
);
82 static void* S60_GlesGetProcAddress(_THIS
, const char *proc
);
83 static int S60_GlesGetAttribute(_THIS
, SDL_GLattr attrib
, int* value
);
84 static int S60_GlesMakeCurrent(_THIS
);
85 static void S60_GlesSwapBuffers(_THIS
);
95 static TSize
GetScreenSize()
97 CWsScreenDevice
*sd
= new CWsScreenDevice(CEikonEnv::Static()->WsSession());
99 const TSize
sz(sd
->SizeInPixels());
105 /* Epoc video driver bootstrap functions */
106 static int S60_Available(void)
108 return 1; /* Always available */
111 static void S60_DeleteDevice(SDL_VideoDevice
*device
)
113 delete device
->gl_data
;
114 delete device
->hidden
;
118 static SDL_VideoDevice
*S60_CreateDevice(int /*devindex*/)
120 SDL_VideoDevice
*device
;
122 /* Allocate all variables that we free on delete */
123 device
= new SDL_VideoDevice
;
126 Mem::FillZ(device
, (sizeof *device
));
127 device
->hidden
= new SDL_PrivateVideoData
;
128 device
->gl_data
= new SDL_PrivateGLData
;
135 if((device
->hidden
== NULL
) || (device
->gl_data
== NULL
))
138 delete device
->hidden
;
139 delete device
->gl_data
;
144 Mem::FillZ(device
->hidden
, (sizeof *device
->hidden
));
146 /* Set the function pointers */
147 device
->VideoInit
= S60_VideoInit
;
148 device
->ListModes
= S60_ListModes
;
149 device
->SetVideoMode
= S60_SetVideoMode
;
150 device
->SetColors
= S60_SetColors
;
151 device
->UpdateRects
= NULL
;
152 device
->VideoQuit
= S60_VideoQuit
;
153 device
->AllocHWSurface
= S60_AllocHWSurface
;
154 device
->CheckHWBlit
= NULL
;
155 device
->FillHWRect
= NULL
;
156 device
->SetHWColorKey
= NULL
;
157 device
->SetHWAlpha
= NULL
;
158 device
->LockHWSurface
= S60_LockHWSurface
;
159 device
->UnlockHWSurface
= S60_UnlockHWSurface
;
160 device
->FlipHWSurface
= S60_FlipHWSurface
;
161 device
->FreeHWSurface
= S60_FreeHWSurface
;
162 device
->SetIcon
= NULL
;
163 device
->SetCaption
= NULL
;
164 device
->GetWMInfo
= NULL
;
165 device
->FreeWMCursor
= S60_FreeWMCursor
;
166 device
->CreateWMCursor
= S60_CreateWMCursor
;
167 device
->ShowWMCursor
= S60_ShowWMCursor
;
168 device
->WarpWMCursor
= NULL
;
169 device
->InitOSKeymap
= EPOC_InitOSKeymap
;
170 device
->PumpEvents
= EPOC_PumpEvents
;
171 device
->free
= S60_DeleteDevice
;
174 device
->GL_LoadLibrary
= S60_GlesLoadLibrary
;
175 device
->GL_GetProcAddress
= S60_GlesGetProcAddress
;
176 device
->GL_GetAttribute
= S60_GlesGetAttribute
;
177 device
->GL_MakeCurrent
= S60_GlesMakeCurrent
;
178 device
->GL_SwapBuffers
= S60_GlesSwapBuffers
;
180 device
->gl_data
->iLibrary
.SetHandle(0);
185 VideoBootStrap EPOC_bootstrap
= {
186 "epoc\0\0\0", "EPOC system",
187 S60_Available
, S60_CreateDevice
190 int S60_VideoInit(_THIS
, SDL_PixelFormat
*vformat
)
192 /* Initialise Epoc frame buffer */
194 const TDisplayMode displayMode
= EpocSdlEnv::DisplayMode();
196 /* The "best" video format should be returned to caller. */
198 vformat
->BitsPerPixel
= TDisplayModeUtils::NumDisplayModeBitsPerPixel(displayMode
);
199 vformat
->BytesPerPixel
= TDisplayModeUtils::NumDisplayModeBitsPerPixel(displayMode
) / 8;
201 Private
->iRect
= new SDL_Rect
*[3];
202 Private
->iRect
[0] = new SDL_Rect
;
203 Private
->iRect
[1] = new SDL_Rect
;
204 Private
->iRect
[2] = NULL
;
206 Private
->iScreenPos
= TPoint(0, 0);
208 Private
->iRect
[0]->x
= Private
->iScreenPos
.iX
;
209 Private
->iRect
[0]->y
= Private
->iScreenPos
.iY
;
211 Private
->iRect
[1]->x
= Private
->iScreenPos
.iX
;
212 Private
->iRect
[1]->y
= Private
->iScreenPos
.iY
;
214 const TSize sz
= GetScreenSize();
216 Private
->iRect
[0]->w
= sz
.iWidth
;
217 Private
->iRect
[0]->h
= sz
.iHeight
;
219 Private
->iRect
[1]->w
= sz
.iHeight
;
220 Private
->iRect
[1]->h
= sz
.iWidth
;
222 Private
->iOrientation
= new CAknAppUi::TAppUiOrientation
[2];
224 if(sz
.iWidth
< sz
.iHeight
)
226 Private
->iOrientation
[0] = CAknAppUi::EAppUiOrientationPortrait
;
227 Private
->iOrientation
[1] = CAknAppUi::EAppUiOrientationLandscape
;
231 Private
->iOrientation
[0] = CAknAppUi::EAppUiOrientationLandscape
;
232 Private
->iOrientation
[1] = CAknAppUi::EAppUiOrientationPortrait
;
238 SDL_Rect
**S60_ListModes(_THIS
, SDL_PixelFormat
*format
, Uint32 flags
)
240 if(flags
& SDL_HWSURFACE
)
242 if(format
->BytesPerPixel
!= 4) //in HW only full color is supported
245 if(flags
& SDL_FULLSCREEN
)
247 return Private
->iRect
;
250 return (SDL_Rect
**)(-1); //everythingisok, but too small shoes
253 int S60_SetColors(_THIS
, int firstcolor
, int ncolors
, SDL_Color
*colors
)
255 if ((firstcolor
+ncolors
) > 256)
257 TUint32 palette
[256];
258 const TDisplayMode mode
= EpocSdlEnv::DisplayMode();
261 if(TDisplayModeUtils::NumDisplayModeColors(mode
) == 4096)
263 // Set 12 bit palette
264 for(int i
= firstcolor
; i
< firstcolor
+ncolors
; i
++)
266 // 4k value: 0000 rrrr gggg bbbb
267 TUint32 color4K
= (colors
[c
].r
& 0x0000f0) << 4;
268 color4K
|= (colors
[c
].g
& 0x0000f0);
269 color4K
|= (colors
[c
].b
& 0x0000f0) >> 4;
270 palette
[i
] = color4K
;
274 else if(TDisplayModeUtils::NumDisplayModeColors(mode
) == 65536)
276 for(int i
= firstcolor
; i
< firstcolor
+ncolors
; i
++)
278 // 64k-colour displays effectively support RGB values
279 // with 5 bits allocated to red, 6 to green and 5 to blue
280 // 64k value: rrrr rggg gggb bbbb
281 TUint32 color64K
= (colors
[c
].r
& 0x0000f8) << 8;
282 color64K
|= (colors
[c
].g
& 0x0000fc) << 3;
283 color64K
|= (colors
[c
].b
& 0x0000f8) >> 3;
284 palette
[i
] = color64K
;
288 else if(TDisplayModeUtils::NumDisplayModeColors(mode
) == 16777216)
290 for(int i
= firstcolor
; i
< firstcolor
+ncolors
; i
++)
293 //0000 0000 rrrr rrrr gggg gggg bbbb bbbb
294 TUint32 color16M
= colors
[c
].r
<< 16;
295 color16M
|= colors
[c
].g
<< 8;
296 color16M
|= colors
[c
].b
;
297 palette
[i
] = color16M
;
305 if(EpocSdlEnv::SetPalette(firstcolor
, ncolors
, palette
) == KErrNone
)
307 // palette has been updated, redraw whole screen to include changes
308 EpocSdlEnv::UpdateWholeScreen(true);
315 int S60_GlesLoadLibrary(SDL_VideoDevice
* _this
, const char* path
)
317 if(_this
->gl_data
->iLibrary
.Handle() != 0)
318 return KErrAlreadyExists
; //already loaded
319 const char* const gles_lib
[] = {"libgles_cm.dll", "libSWGLES.dll"};
320 int err
= KErrNotFound
;
321 for(int i
= 0; i
< 2 && err
!= KErrNone
; i
++)
323 const char* name8
= path
== NULL
? gles_lib
[i
] : path
;
325 lib
.Copy(TPtrC8((unsigned char*)name8
));
326 err
= _this
->gl_data
->iLibrary
.Load(lib
);
329 _this
->gl_config
.driver_loaded
= 1;
333 typedef int (*Ftp
)(...);
334 #define DC(x) ((Ftp) S60_GlesGetProcAddress(_this, #x))
336 const char* const OpenGL
[] = //these funtions are in gl, but not in gles, at least in all in all versions...
344 "glPushClientAttrib",
352 User::Panic(_L("SDL, Gles"), KErrNotSupported
);
356 void* S60_GlesGetProcAddress(_THIS
, const char *proc
)
358 if(_this
->gl_data
->iLibrary
.Handle() == 0)
359 return NULL
; //not loaded
360 TLibraryFunction f
= NULL
;
361 for(int i
= 0; i
< G_ordinals_count
; i
++)
363 if(strcmp(G_ordinals
[i
].name
, proc
) == 0)
365 f
= _this
->gl_data
->iLibrary
.Lookup(G_ordinals
[i
].ord
);
370 if(f
!= NULL
) /*Lookup may fail*/
373 for(int i
= 0; i
< sizeof(OpenGL
) / sizeof(char*); i
++)
375 if(strcmp(OpenGL
[i
], proc
) == 0)
376 return (void*) NotSupported
;
382 int S60_GlesGetAttribute(_THIS
, SDL_GLattr aAttrib
, int* aValue
)
388 case SDL_GL_RED_SIZE
: attrib
= EGL_RED_SIZE
; break;
389 case SDL_GL_GREEN_SIZE
: attrib
= EGL_GREEN_SIZE
; break;
390 case SDL_GL_BLUE_SIZE
:attrib
= EGL_BLUE_SIZE
; break;
391 case SDL_GL_ALPHA_SIZE
: attrib
= EGL_ALPHA_SIZE
; break;
392 case SDL_GL_BUFFER_SIZE
: attrib
= EGL_BUFFER_SIZE
; break;
393 case SDL_GL_DOUBLEBUFFER
: *aValue
= 1; return 0; //always
394 case SDL_GL_DEPTH_SIZE
: attrib
= EGL_DEPTH_SIZE
; break;
395 case SDL_GL_STENCIL_SIZE
: attrib
= EGL_STENCIL_SIZE
; break;
396 case SDL_GL_ACCUM_RED_SIZE
:
397 case SDL_GL_ACCUM_GREEN_SIZE
:
398 case SDL_GL_ACCUM_BLUE_SIZE
:
399 case SDL_GL_ACCUM_ALPHA_SIZE
:
401 case SDL_GL_MULTISAMPLEBUFFERS
:
402 case SDL_GL_MULTISAMPLESAMPLES
:
403 case SDL_GL_ACCELERATED_VISUAL
:
404 case SDL_GL_SWAP_CONTROL
:
408 const int success
= DC(eglGetConfigAttrib
)
410 _this
->gl_data
->iDisplay
,
411 _this
->gl_data
->iConfig
,
414 return success
== EGL_FALSE
? -1 : 0;
418 int S60_GlesMakeCurrent(_THIS
)
421 (_this
->gl_data
->iDisplay
,
422 _this
->gl_data
->iSurface
,
423 _this
->gl_data
->iSurface
,
424 _this
->gl_data
->iContext
);
425 return DC(eglGetError
)();
428 void S60_GlesSwapBuffers(_THIS
)
431 _this
->gl_data
->iDisplay
,
432 _this
->gl_data
->iSurface
);
435 TDisplayMode
GetDisplayMode(int aBitsPerPixel
)
437 const TDisplayMode displayMode
= EpocSdlEnv::DisplayMode();
438 int dmode
= EColorLast
;
439 if(TDisplayModeUtils::NumDisplayModeBitsPerPixel(displayMode
) == aBitsPerPixel
)
449 while(TDisplayModeUtils::IsDisplayModeColor(TDisplayMode(dmode
)) &&
450 TDisplayModeUtils::NumDisplayModeBitsPerPixel(TDisplayMode(dmode
)) != aBitsPerPixel
);
452 return TDisplayMode(dmode
);
456 static void glAssert(_THIS
)
458 const EGLint err
= DC(eglGetError
)();
459 if(err
!= EGL_SUCCESS
)
465 static void CreateGles(_THIS
, RWindow
& aWindow
, int bpp
, SDL_PrivateGLData
& aData
)
467 SDL_GL_LoadLibrary(NULL
); //just if its not already loaded
468 aData
.iDisplay
= DC(eglGetDisplay
)(EGL_DEFAULT_DISPLAY
);
469 DC(eglInitialize
)(aData
.iDisplay
, NULL
, NULL
);
474 EGLConfig
* configList
= NULL
;
476 DC(eglGetConfigs
)(aData
.iDisplay
, configList
, configSz
, &configs
);
481 configList
= new EGLConfig
[configSz
];
483 int red
, green
, blue
;
497 const EGLint attribList
[] =
499 EGL_SURFACE_TYPE
, EGL_WINDOW_BIT
,
501 EGL_GREEN_SIZE
, green
,
503 EGL_BUFFER_SIZE
, EGL_DONT_CARE
,
508 DC(eglChooseConfig
)(aData
.iDisplay
,
517 __ASSERT_ALWAYS(configs
> 0, User::Invariant());
519 aData
.iConfig
= configList
[0];
523 aData
.iContext
= DC(eglCreateContext
)(aData
.iDisplay
,
530 aData
.iSurface
= DC(eglCreateWindowSurface
)(aData
.iDisplay
,
539 static void DestroyGles(_THIS
)
541 if( _this
->gl_config
.driver_loaded
)
543 DC(eglMakeCurrent
)(_this
->gl_data
->iDisplay
,
547 DC(eglDestroySurface
)(_this
->gl_data
->iDisplay
, _this
->gl_data
->iSurface
);
548 DC(eglDestroyContext
)(_this
->gl_data
->iDisplay
, _this
->gl_data
->iContext
);
549 DC(eglTerminate
)(_this
->gl_data
->iDisplay
);
550 _this
->gl_data
->iLibrary
.Close();
551 _this
->gl_config
.driver_loaded
= 0;
555 SDL_Surface
*S60_SetVideoMode(_THIS
, SDL_Surface
*current
, int width
, int height
, int bpp
, Uint32 flags
)
557 if(flags
& SDL_OPENGL
)
559 current
->flags
|= SDL_OPENGL
;
563 RWindow
* win
= EpocSdlEnv::Window();
564 EpocSdlEnv::ApplyGlesDsa();
566 CreateGles(_this
, *win
, bpp
, *_this
->gl_data
);
571 if(flags
& SDL_HWSURFACE
)
574 // check orientation and resolution validity
575 bool resValid
= false;
576 Orientation orientation
= CAknAppUi::EAppUiOrientationUnspecified
;
577 for(int i
=0; i
<2; i
++)
579 if(width
<= Private
->iRect
[i
]->w
&& height
<= Private
->iRect
[i
]->h
)
581 orientation
= Private
->iOrientation
[i
];
589 if(!SDL_ReallocFormat(current
, bpp
, 0, 0, 0, 0))
592 const int numBytesPerPixel
= ((bpp
-1)>>3) + 1;
593 current
->pitch
= numBytesPerPixel
* width
;
595 // set proper surface flags
596 current
->flags
= SDL_SWSURFACE
| SDL_PREALLOC
;
597 if(flags
& SDL_FULLSCREEN
)
598 current
->flags
|= SDL_FULLSCREEN
;
599 if(flags
& SDL_RESIZABLE
)
600 current
->flags
|= SDL_RESIZABLE
;
602 current
->flags
|= SDL_HWPALETTE
;
608 const int surfacesize
= width
* height
* numBytesPerPixel
;
609 Private
->iSwSurfaceSize
= TSize(width
, height
);
611 delete[] (TUint8
*)current
->pixels
;
612 current
->pixels
= new TUint8
[surfacesize
];
613 Private
->iSwSurface
= (TUint8
*) current
->pixels
;
615 if(current
->pixels
== NULL
)
618 const TSize sz
= GetScreenSize();
620 if(sz
.iWidth
< sz
.iHeight
&& orientation
!= CAknAppUi::EAppUiOrientationPortrait
||
621 sz
.iWidth
> sz
.iHeight
&& orientation
!= CAknAppUi::EAppUiOrientationLandscape
)
623 EpocSdlEnv::SetOrientation(orientation
, TSize(width
, height
), GetDisplayMode(current
->format
->BitsPerPixel
));
627 if((flags
& SDL_RESIZABLE
) == 0)
629 TRAPD(err
, static_cast<CAknAppUi
*>(CEikonEnv::Static()->EikAppUi())->SetOrientationL(orientation
));
633 TRAPD(err
, static_cast<CAknAppUi
*>(CEikonEnv::Static()->EikAppUi())->SetOrientationL(CAknAppUi::EAppUiOrientationUnspecified
));
636 const int err
= EpocSdlEnv::AllocSurface(TSize(width
, height
), GetDisplayMode(current
->format
->BitsPerPixel
));
641 // Set the blit function
642 _this
->UpdateRects
= S60_DirectUpdate
;
644 // Centralize game window on device screen
645 Private
->iScreenPos
.iX
= 0;
646 Private
->iScreenPos
.iY
= 0;
651 static int S60_AllocHWSurface(_THIS
, SDL_Surface
* surface
)
653 return KErrNone
== EpocSdlEnv::AllocSurface(TSize(surface
->w
, surface
->h
), GetDisplayMode(surface
->format
->BitsPerPixel
));
656 static void S60_FreeHWSurface(_THIS
, SDL_Surface
* /*surface*/)
660 static int S60_LockHWSurface(_THIS
, SDL_Surface
* surface
)
662 if(EpocSdlEnv::IsDsaAvailable())
664 TUint8
* address
= EpocSdlEnv::LockHwSurface();
667 surface
->pixels
= address
;
673 static void S60_UnlockHWSurface(_THIS
, SDL_Surface
* /*surface*/)
675 EpocSdlEnv::UnlockHwSurface();
678 static int S60_FlipHWSurface(_THIS
, SDL_Surface
* /*surface*/)
683 static void S60_DirectUpdate(_THIS
, int numrects
, SDL_Rect
*rects
)
685 if(EpocSdlEnv::IsDsaAvailable())
687 if(Private
->iSwSurface
)
689 const TRect
target(TPoint(0, 0), Private
->iSwSurfaceSize
);
691 if(EpocSdlEnv::GetUpdateWholeScreen())
693 if(!EpocSdlEnv::AddUpdateRect(Private
->iSwSurface
, target
, target
))
698 for(int i
= 0; i
< numrects
;i
++)
700 const TRect
rect(TPoint(rects
[i
].x
, rects
[i
].y
),
701 TSize(rects
[i
].w
, rects
[i
].h
));
702 if(!EpocSdlEnv::AddUpdateRect(Private
->iSwSurface
, rect
, target
))
703 return; //not succesful
704 EpocSdlEnv::UpdateWholeScreen(false);
707 EpocSdlEnv::UpdateSwSurface();
717 /* Note: If we are terminated, this could be called in the middle of
718 another SDL video routine -- notably UpdateRects.
720 void S60_VideoQuit(_THIS
)
722 delete[] Private
->iOrientation
;
724 delete Private
->iRect
[0];
725 delete Private
->iRect
[1];
726 delete[] Private
->iRect
;
731 delete[] Private
->iSwSurface
;
732 Private
->iSwSurface
= NULL
;
733 EpocSdlEnv::FreeSurface();
737 WMcursor
*S60_CreateWMCursor(_THIS
, Uint8
* /*data*/, Uint8
* /*mask*/, int /*w*/, int /*h*/, int /*hot_x*/, int /*hot_y*/)
739 // prevents SDL from displaying standard cursor
740 return (WMcursor
*) 1;
743 void S60_FreeWMCursor(_THIS
, WMcursor
* /*cursor*/)
747 int S60_ShowWMCursor(_THIS
, WMcursor
*cursor
)
754 void glBegin(GLenum a
) {}
756 void glOrtho(GLdouble l
, GLdouble r
, GLdouble b
, GLdouble t
, GLdouble n
, GLdouble f
) {}
757 void glPopAttrib(void) {}
758 void glPopClientAttrib(void){}
759 void glPushAttrib(GLbitfield mask
) {}
760 void glPushClientAttrib(GLbitfield mask
) {}
761 void glTexCoord2f(GLfloat s
, GLfloat t
) {}
762 void glVertex2i(GLint x
, GLint y
) {}