2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 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
25 Epoc based SDL video driver implementation
39 #include "SDL_error.h"
40 #include "SDL_timer.h"
41 #include "SDL_video.h"
43 #include "SDL_pixels_c.h"
45 #include "SDL_mouse.h"
48 #include "SDL_epocvideo.h"
49 #include "SDL_epocevents_c.h"
59 #include "sdlepocapi.h"
62 ////////////////////////////////////////////////////////////////
67 _LIT(KLibName, "SDL");
69 void RDebug_Print_b(char* error_str, void* param)
71 TBuf8<128> error8((TUint8*)error_str);
76 if (param) //!! Do not work if the parameter is really 0!!
77 RDebug::Print(error, param);
81 if (param) //!! Do not work if the parameter is really 0!!
82 RFileLogger::WriteFormat(KLibName, _L("SDL.txt"), EFileLoggingModeAppend, error, param);
84 RFileLogger::Write(KLibName, _L("SDL.txt"), EFileLoggingModeAppend, error);
89 extern "C" void RDebug_Print(char* error_str, void* param)
91 RDebug_Print_b(error_str, param);
97 //User::CompressAllHeaps();
98 TMemoryInfoV1Buf membuf;
99 User::LeaveIfError(UserHal::MemoryInfo(membuf));
100 TMemoryInfoV1 minfo = membuf();
101 return(minfo.iFreeRamInBytes);
104 extern "C" int Debug_AvailMem()
106 return(Debug_AvailMem2());
113 /* Initialization/Query functions */
115 static int EPOC_VideoInit(_THIS, SDL_PixelFormat *vformat);
116 static SDL_Rect **EPOC_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
117 static SDL_Surface *EPOC_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
118 static int EPOC_SetColors(_THIS, int firstcolor, int ncolors,
120 static void EPOC_VideoQuit(_THIS);
122 /* Hardware surface functions */
124 static int EPOC_AllocHWSurface(_THIS, SDL_Surface *surface);
125 static int EPOC_LockHWSurface(_THIS, SDL_Surface *surface);
126 static int EPOC_FlipHWSurface(_THIS, SDL_Surface *surface);
127 static void EPOC_UnlockHWSurface(_THIS, SDL_Surface *surface);
128 static void EPOC_FreeHWSurface(_THIS, SDL_Surface *surface);
129 static void EPOC_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
131 static int EPOC_Available(void);
132 static SDL_VideoDevice *EPOC_CreateDevice(int devindex);
134 void DrawBackground(_THIS);
135 void DirectDraw(_THIS, int numrects, SDL_Rect *rects, TUint16* screenBuffer);
136 void DirectDrawRotated(_THIS, int numrects, SDL_Rect *rects, TUint16* screenBuffer);
138 /* Mouse functions */
140 static WMcursor *EPOC_CreateWMCursor(_THIS, Uint8 *data, Uint8 *mask, int w, int h, int hot_x, int hot_y);
141 static void EPOC_FreeWMCursor(_THIS, WMcursor *cursor);
142 static int EPOC_ShowWMCursor(_THIS, WMcursor *cursor);
153 /* Epoc video driver bootstrap functions */
156 static int EPOC_Available(void)
158 return 1; /* Always available */
161 static void EPOC_DeleteDevice(SDL_VideoDevice *device)
163 User::Free(device->hidden);
167 static SDL_VideoDevice *EPOC_CreateDevice(int /*devindex*/)
169 SDL_VideoDevice *device;
171 SDL_TRACE("SDL:EPOC_CreateDevice");
173 /* Allocate all variables that we free on delete */
174 device = static_cast<SDL_VideoDevice*>(User::Alloc(sizeof(SDL_VideoDevice)));
177 Mem::FillZ(device, (sizeof *device));
178 device->hidden = static_cast<struct SDL_PrivateVideoData*>
179 (User::Alloc((sizeof *device->hidden)));
181 if ( (device == NULL) || (device->hidden == NULL) )
189 Mem::FillZ(device->hidden, (sizeof *device->hidden));
191 /* Set the function pointers */
192 device->VideoInit = EPOC_VideoInit;
193 device->ListModes = EPOC_ListModes;
194 device->SetVideoMode = EPOC_SetVideoMode;
195 device->SetColors = EPOC_SetColors;
196 device->UpdateRects = NULL;
197 device->VideoQuit = EPOC_VideoQuit;
198 device->AllocHWSurface = EPOC_AllocHWSurface;
199 device->CheckHWBlit = NULL;
200 device->FillHWRect = NULL;
201 device->SetHWColorKey = NULL;
202 device->SetHWAlpha = NULL;
203 device->LockHWSurface = EPOC_LockHWSurface;
204 device->UnlockHWSurface = EPOC_UnlockHWSurface;
205 device->FlipHWSurface = EPOC_FlipHWSurface;
206 device->FreeHWSurface = EPOC_FreeHWSurface;
207 device->SetIcon = NULL;
208 device->SetCaption = NULL;
209 device->GetWMInfo = NULL;
210 device->FreeWMCursor = EPOC_FreeWMCursor;
211 device->CreateWMCursor = EPOC_CreateWMCursor;
212 device->ShowWMCursor = EPOC_ShowWMCursor;
213 device->WarpWMCursor = NULL;
214 device->InitOSKeymap = EPOC_InitOSKeymap;
215 device->PumpEvents = EPOC_PumpEvents;
216 device->free = EPOC_DeleteDevice;
222 VideoBootStrap EPOC_bootstrap = {
223 "epoc\0\0\0", "EPOC system",
224 EPOC_Available, EPOC_CreateDevice
229 void DisableKeyBlocking(_THIS)
231 EpocSdlEnv::Request(EpocSdlEnv::EDisableKeyBlocking);
234 void ConstructWindowL(_THIS)
236 SDL_TRACE("SDL:ConstructWindowL");
237 DisableKeyBlocking(_this); //disable key blocking
241 int EPOC_VideoInit(_THIS, SDL_PixelFormat *vformat)
243 /* Construct Epoc window */
245 ConstructWindowL(_this);
247 /* Initialise Epoc frame buffer */
250 const TDisplayMode displayMode = EpocSdlEnv::DisplayMode();
252 /* The "best" video format should be returned to caller. */
254 vformat->BitsPerPixel = TDisplayModeUtils::NumDisplayModeBitsPerPixel(displayMode);
255 vformat->BytesPerPixel = TDisplayModeUtils::NumDisplayModeBitsPerPixel(displayMode) / 8;
258 //?? Private->iWindow->PointerFilter(EPointerFilterDrag, 0);
260 Private->iScreenPos = TPoint(0, 0);
262 Private->iRect.x = Private->iScreenPos.iX;
263 Private->iRect.y = Private->iScreenPos.iY;
265 const TSize sz = EpocSdlEnv::WindowSize();
267 Private->iRect.w = sz.iWidth;
268 Private->iRect.h = sz.iHeight;
269 Private->iRectPtr = &Private->iRect;
275 SDL_Rect **EPOC_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
277 if(flags & SDL_HWSURFACE)
279 if(format->BytesPerPixel != 4) //in HW only full color is supported
282 if(flags & SDL_FULLSCREEN)
284 return &Private->iRectPtr;
286 return (SDL_Rect **)(-1); //everythingisok, unless too small shoes
290 int EPOC_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
292 if ((firstcolor+ncolors) > 256)
294 TUint32 palette[256];
295 const TDisplayMode mode = EpocSdlEnv::DisplayMode();
296 if(TDisplayModeUtils::NumDisplayModeColors(mode) == 4096)
298 // Set 12 bit palette
299 for(int i = firstcolor; i < ncolors; i++)
301 // 4k value: 0000 rrrr gggg bbbb
302 TUint32 color4K = (colors[i].r & 0x0000f0) << 4;
303 color4K |= (colors[i].g & 0x0000f0);
304 color4K |= (colors[i].b & 0x0000f0) >> 4;
305 palette[i] = color4K;
308 else if(TDisplayModeUtils::NumDisplayModeColors(mode) == 65536)
310 for(int i = firstcolor; i < ncolors; i++)
312 // 64k-colour displays effectively support RGB values
313 // with 5 bits allocated to red, 6 to green and 5 to blue
314 // 64k value: rrrr rggg gggb bbbb
315 TUint32 color64K = (colors[i].r & 0x0000f8) << 8;
316 color64K |= (colors[i].g & 0x0000fc) << 3;
317 color64K |= (colors[i].b & 0x0000f8) >> 3;
318 palette[i] = color64K;
321 else if(TDisplayModeUtils::NumDisplayModeColors(mode) == 16777216)
323 for(int i = firstcolor; i < ncolors; i++)
326 //0000 0000 rrrr rrrr gggg gggg bbbb bbbb
327 TUint32 color16M = colors[i].r << 16;
328 color16M |= colors[i].g << 8;
329 color16M |= colors[i].b;
330 palette[i] = color16M;
337 if(EpocSdlEnv::SetPalette(firstcolor, ncolors, palette) == KErrNone)
344 void AllocHWSurfaceL(CFbsBitmap*& aBitmap, const TDisplayMode& aMode, const TSize& aSize)
346 aBitmap = new (ELeave) CFbsBitmap();
347 if(KErrNone != aBitmap->CreateHardwareBitmap(aSize, aMode,
348 EpocSdlEnv::EikonEnv().EikAppUi()->Application()->AppDllUid()))
349 //...if it fails - should we use wsbitmaps???
350 {//the good reason to use hw bitmaps is that they wont need lock heap
351 PANIC_IF_ERROR(aBitmap->Create(aSize, aMode));
355 int CreateSurfaceL(_THIS, SDL_Surface* surface)
357 __ASSERT_ALWAYS(Private->iFrame == NULL, PANIC(KErrAlreadyExists));
359 TInt dmode = EColorLast;
361 TDisplayMode displayMode;
362 EpocSdlEnv::GetDiplayMode(displayMode);
365 TDisplayModeUtils::NumDisplayModeBitsPerPixel(displayMode)
366 == surface->format->BitsPerPixel)
373 while(TDisplayModeUtils::IsDisplayModeColor(TDisplayMode(dmode)) &&
374 TDisplayModeUtils::NumDisplayModeBitsPerPixel(TDisplayMode(dmode)) !=
375 surface->format->BitsPerPixel)
379 __ASSERT_ALWAYS(TDisplayModeUtils::IsDisplayModeColor(TDisplayMode(dmode)), PANIC(KErrNotSupported));
380 TRAPD(err, AllocHWSurfaceL(Private->iFrame, TDisplayMode(dmode), TSize(surface->w, surface->h)));
381 return err == KErrNone ? 0 : -1;
385 TDisplayMode GetDisplayMode(TInt aBitsPerPixel)
387 const TDisplayMode displayMode = EpocSdlEnv::DisplayMode();
388 TInt dmode = EColorLast;
390 TDisplayModeUtils::NumDisplayModeBitsPerPixel(displayMode)
398 while(TDisplayModeUtils::IsDisplayModeColor(TDisplayMode(dmode)) &&
399 TDisplayModeUtils::NumDisplayModeBitsPerPixel(TDisplayMode(dmode)) !=
403 return TDisplayMode(dmode);
406 SDL_Surface *EPOC_SetVideoMode(_THIS, SDL_Surface *current,
407 int width, int height, int bpp, Uint32 flags)
409 const TSize screenSize = EpocSdlEnv::WindowSize(TSize(width, height));
410 if(width > screenSize.iWidth || height > screenSize.iHeight)
412 if(flags & SDL_FULLSCREEN)
414 width = screenSize.iWidth;
415 height = screenSize.iHeight;
421 if(current && current->pixels)
423 // free(current->pixels);
424 current->pixels = NULL;
427 if(!SDL_ReallocFormat(current, bpp, 0, 0, 0, 0))
433 if(width == screenSize.iWidth && height == screenSize.iHeight)
434 current->flags |= SDL_FULLSCREEN;
436 const int numBytesPerPixel = ((bpp-1)>>3) + 1;
437 current->pitch = numBytesPerPixel * width; // Number of bytes in scanline
439 /* Set up the new mode framebuffer */
440 current->flags |= SDL_PREALLOC;
443 current->flags |= SDL_HWPALETTE;
445 User::Free(Private->iSwSurface);
446 current->pixels = NULL;
447 Private->iSwSurface = NULL;
449 if(flags & SDL_HWSURFACE)
451 current->flags |= SDL_HWSURFACE;
452 // current->pixels = NULL;
453 // Private->iSwSurface = NULL;
457 current->flags |= SDL_SWSURFACE;
458 const TInt surfacesize = width * height * numBytesPerPixel;
459 Private->iSwSurfaceSize = TSize(width, height);
460 delete Private->iSwSurface;
461 Private->iSwSurface = NULL;
462 current->pixels = (TUint8*) User::AllocL(surfacesize);
463 Private->iSwSurface = (TUint8*) current->pixels;
464 const TInt err = EpocSdlEnv::AllocSwSurface
465 (TSize(width, height), GetDisplayMode(current->format->BitsPerPixel));
475 /* Set the blit function */
476 _this->UpdateRects = EPOC_DirectUpdate;
479 * Logic for getting suitable screen dimensions, offset, scaling and orientation
483 /* Centralize game window on device screen */
486 Private->iScreenPos.iX = Max(0, (screenSize.iWidth - width) / 2);
487 Private->iScreenPos.iY = Max(0, (screenSize.iHeight - height) / 2);
489 // delete (Private->iFrame);
490 // Private->iFrame = NULL;
492 // TRAPD(err, CreateSurfaceL(_this, current));
493 // PANIC_IF_ERROR(err);
495 SDL_TRACE1("View width %d", width);
496 SDL_TRACE1("View height %d", height);
497 SDL_TRACE1("View bmode %d", bpp);
498 SDL_TRACE1("View x %d", Private->iScreenPos.iX);
499 SDL_TRACE1("View y %d", Private->iScreenPos.iY);
501 EpocSdlEnv::LockPalette(EFalse);
508 static int EPOC_AllocHWSurface(_THIS, SDL_Surface* surface)
510 return KErrNone == EpocSdlEnv::AllocHwSurface(TSize(surface->w, surface->h), GetDisplayMode(surface->format->BitsPerPixel));
513 static void EPOC_FreeHWSurface(_THIS, SDL_Surface* /*surface*/)
517 static int EPOC_LockHWSurface(_THIS, SDL_Surface* surface)
519 if(EpocSdlEnv::IsDsaAvailable())
521 TUint8* address = EpocSdlEnv::LockHwSurface();
524 surface->pixels = address;
530 static void EPOC_UnlockHWSurface(_THIS, SDL_Surface* /*surface*/)
532 EpocSdlEnv::UnlockHwSurface();
535 static int EPOC_FlipHWSurface(_THIS, SDL_Surface* /*surface*/)
540 static void EPOC_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
542 if(EpocSdlEnv::IsDsaAvailable())
544 if(Private->iSwSurface)
546 const TRect target(Private->iScreenPos, Private->iSwSurfaceSize);
547 for(TInt i = 0; i < numrects ;i++)
549 const TRect rect(TPoint(rects[i].x, rects[i].y),
550 TSize(rects[i].w, rects[i].h));
551 if(!EpocSdlEnv::AddUpdateRect(Private->iSwSurface, rect, target))
552 return; //not succesful
554 EpocSdlEnv::UpdateSwSurface();
561 EpocSdlEnv::WaitDsaAvailable();
566 /* Note: If we are terminated, this could be called in the middle of
567 another SDL video routine -- notably UpdateRects.
569 void EPOC_VideoQuit(_THIS)
571 // delete Private->iFrame;
572 // Private->iFrame = NULL;
573 User::Free(Private->iSwSurface);
574 Private->iSwSurface = NULL;
575 EpocSdlEnv::FreeSurface();
581 WMcursor *EPOC_CreateWMCursor(_THIS, Uint8* /*data*/, Uint8* /*mask*/, int /*w*/, int /*h*/, int /*hot_x*/, int /*hot_y*/)
583 return (WMcursor*) 1; //hii! prevents SDL to view a std cursor
586 void EPOC_FreeWMCursor(_THIS, WMcursor* /*cursor*/)
590 int EPOC_ShowWMCursor(_THIS, WMcursor *cursor)