Remove unused functionality.
[SDL.s60v3.git] / src / video / symbian / dsa.cpp
blobf9eff5e4dbecf7ec67a6baf47b5debc9dde98c2d
1 #include "dsa.h"
2 #include "sdlepocapi.h"
3 #include <cdsb.h>
4 #include <basched.h>
6 static int BytesPerPixel(TDisplayMode aMode)
8 return ((TDisplayModeUtils::NumDisplayModeBitsPerPixel(aMode) - 1) >> 3) + 1;
11 template<class T>
12 class CBitmapSurface : public T
14 public:
15 CBitmapSurface(RWsSession& aSession);
17 private:
18 void ConstructL(RWindow& aWindow, CWsScreenDevice& aDevice);
19 ~CBitmapSurface();
20 TUint8* LockSurface();
21 void UnlockHwSurface();
22 void CreateSurfaceL(const TSize& aSize);
23 void Free();
24 void Update(CFbsBitmap& aBmp);
25 CFbsBitmap* iBmp;
28 template<class T>
29 void CBitmapSurface<T>::ConstructL(RWindow& aWindow, CWsScreenDevice& aDevice)
31 T::ConstructL(aWindow, aDevice);
34 template<class T>
35 CBitmapSurface<T>::CBitmapSurface(RWsSession& aSession) : T(aSession)
39 template<class T>
40 void CBitmapSurface<T>::Free()
42 delete iBmp;
43 iBmp = NULL;
44 T::Free();
47 template<class T>
48 CBitmapSurface<T>::~CBitmapSurface()
50 __ASSERT_DEBUG(iBmp == NULL, PANIC(KErrNotReady));
53 template<class T>
54 TUint8* CBitmapSurface<T>::LockSurface()
56 iBmp->LockHeap();
57 return reinterpret_cast<TUint8*>(iBmp->DataAddress());
60 template<class T>
61 void CBitmapSurface<T>::UnlockHwSurface()
63 iBmp->UnlockHeap();
64 Update(*iBmp);
67 template<class T>
68 void CBitmapSurface<T>::Update(CFbsBitmap& aBmp)
70 T::DoBlt(aBmp);
71 T::CompleteUpdate();
74 template<class T>
75 void CBitmapSurface<T>::CreateSurfaceL(const TSize& aSize)
77 Free();
78 iBmp = new CFbsBitmap();
79 User::LeaveIfError(iBmp->Create(aSize, T::DisplayMode()));
82 //////////////////////////////////////////////////////////////////////
84 class CDsaGles : public CDsa
86 public:
87 CDsaGles(RWsSession& aSession) : CDsa(aSession) {}
89 private:
90 TUint8* LockSurface() { return NULL; }
91 void UnlockHwSurface() {}
92 void CreateSurfaceL(const TSize& aSize) {}
95 //////////////////////////////////////////////////////////////////////
97 class CDsaBase : public CDsa, public MDirectScreenAccess
99 protected:
100 CDsaBase(RWsSession& aSession) : CDsa(aSession) { m_updateWholeScreen = false; }
101 ~CDsaBase();
102 void ConstructL(RWindow& aWindow, CWsScreenDevice& aDevice);
103 void Stop();
104 void CompleteUpdate();
105 void DoBlt(CFbsBitmap& aBmp);
107 CDirectScreenAccess* iDsa;
109 private:
110 void AbortNow(RDirectScreenAccess::TTerminationReasons aReason);
111 void Restart(RDirectScreenAccess::TTerminationReasons aReason);
112 void RestartL();
115 void CDsaBase::ConstructL(RWindow& aWindow, CWsScreenDevice& aDevice)
117 CDsa::ConstructL(aWindow, aDevice);
118 if(iDsa != NULL)
120 iDsa->Cancel();
121 delete iDsa;
122 iDsa = NULL;
125 iDsa = CDirectScreenAccess::NewL(Session(), aDevice, aWindow, *this);
126 RestartL();
129 void CDsaBase::CompleteUpdate()
131 iDsa->ScreenDevice()->Update();
134 void CDsaBase::DoBlt(CFbsBitmap& aBmp)
136 iDsa->Gc()->BitBlt(TPoint(0, 0), &aBmp);
139 CDsaBase::~CDsaBase()
141 if(iDsa != NULL)
143 iDsa->Cancel();
145 delete iDsa;
148 void CDsaBase::RestartL()
150 iDsa->StartL();
151 iDsa->Gc()->SetClippingRegion(iDsa->DrawingRegion());
153 Start();
156 void CDsaBase::AbortNow(RDirectScreenAccess::TTerminationReasons /*aReason*/)
158 Stop();
161 void CDsaBase::Restart(RDirectScreenAccess::TTerminationReasons /*aReason*/)
163 TRAPD(err, RestartL());
164 if(err == KLeaveExit)
166 Stop();
168 else
170 PANIC_IF_ERROR(err);
174 void CDsaBase::Stop()
176 CDsa::Stop();
177 iDsa->Cancel();
180 ///////////////////////////////////////////////////////////////////////
182 class TDsa
184 public:
185 inline TDsa(const CDsa& aDsa) : iDsa(aDsa) {}
186 inline void Copy(TUint32* aTarget, const TUint8* aSrc, int aBytes) const;
187 private:
188 const CDsa& iDsa;
191 inline void TDsa::Copy(TUint32* aTarget, const TUint8* aSrc, int aBytes) const
193 iDsa.iCopyFunction(iDsa, aTarget, aSrc, aBytes);
196 template<class T, class S>
197 void ClipCopy(const TDsa& iDsa, TUint8* aTarget,
198 const TUint8* aSource,
199 const TRect& aUpdateRect,
200 const TRect& aSourceRect)
202 const S* source = reinterpret_cast<const S*>(aSource);
203 const int lineWidth = aSourceRect.Width();
205 source += (aUpdateRect.iTl.iY * lineWidth);
206 const int sourceStartOffset = aUpdateRect.iTl.iX;
207 source += sourceStartOffset;
209 T* targetPtr = reinterpret_cast<T*>(aTarget);
211 const int scanLineWidth = aSourceRect.iBr.iX;
213 targetPtr += aUpdateRect.iTl.iY * scanLineWidth;
214 const int targetStartOffset = aUpdateRect.iTl.iX;
216 targetPtr += targetStartOffset;
218 const int height = aUpdateRect.Height();
220 const int lineMove = lineWidth;
221 const int copyLen = aUpdateRect.Width();
223 for(int i = 0; i < height; i++) //source is always smaller
225 iDsa.Copy(reinterpret_cast<TUint32*>(targetPtr), reinterpret_cast<const TUint8*>(source), copyLen);
226 source += lineMove;
227 targetPtr += scanLineWidth;
231 /////////////////////////////////////////////////////////////////////////////////////////////////////
233 CDsa* CDsa::CreateL(RWsSession& aSession)
235 return new CBitmapSurface<CDsaBase>(aSession);
238 void CDsa::Free()
242 CDsa::~CDsa()
244 delete[] iLut256;
247 void CDsa::ConstructL(RWindow& aWindow, CWsScreenDevice& /*aDevice*/)
249 if(iLut256 == NULL)
250 iLut256 = new TUint32[256];
251 iTargetMode = aWindow.DisplayMode();
252 iTargetBpp = BytesPerPixel(DisplayMode());
253 iWindow = &aWindow;
256 void CDsa::DoBlt(CFbsBitmap& /*aBmp*/)
260 int CDsa::SetPalette(int aFirst, int aCount, TUint32* aPalette)
262 if(iLut256 == NULL)
263 return KErrNotFound;
264 for(int i = aFirst; i < aFirst + aCount; i++)
266 iLut256[i] = aPalette[i];
268 return KErrNone;
271 CDsa::CDsa(RWsSession& aSession) :
272 iRunning(false),
273 iSession(aSession)
276 iCFTable[0] = CopyMem;
277 iCFTable[1] = Copy256;
278 iCFTable[2] = CopySlow;
281 RWsSession& CDsa::Session()
283 return iSession;
286 TUint8* CDsa::LockHwSurface()
288 return LockSurface();
291 int CDsa::AllocSurface(const TSize& aSize, TDisplayMode aMode)
293 iSourceMode = aMode;
294 iSourceBpp = BytesPerPixel(aMode);
296 TRAPD(err, CreateSurfaceL(aSize));
297 if(err != KErrNone)
298 return err;
300 SetCopyFunction();
302 return KErrNone;
305 void CDsa::ClipCopy(TUint8* aTarget,
306 const TUint8* aSource,
307 const TRect& aUpdateRect,
308 const TRect& aSourceRect) const
310 const TDsa dsa(*this);
311 switch(iSourceBpp)
313 case 1:
314 ::ClipCopy<TUint32, TUint8>(dsa, aTarget, aSource, aUpdateRect, aSourceRect);
315 break;
316 case 2:
317 ::ClipCopy<TUint32, TUint16>(dsa, aTarget, aSource, aUpdateRect, aSourceRect);
318 break;
319 case 4:
320 ::ClipCopy<TUint32, TUint32>(dsa, aTarget, aSource, aUpdateRect, aSourceRect);
321 break;
325 void CDsa::SetCopyFunction()
327 if(iSourceMode == DisplayMode())
328 iCopyFunction = iCFTable[0];
329 else if(iSourceMode == EColor256)
330 iCopyFunction = iCFTable[1];
331 else
332 iCopyFunction = iCFTable[2];
335 TBool CDsa::AddUpdateRect(const TUint8* aBits, const TRect& aUpdateRect, const TRect& aRect)
337 if(iTargetAddr == NULL)
339 iTargetAddr = LockHwSurface();
342 TUint8* target = iTargetAddr;
343 if(target == NULL)
344 return EFalse;
346 TRect sourceRect = aRect;
347 TRect updateRect = aUpdateRect;
349 if(iSourceMode != DisplayMode() || aRect != aUpdateRect)
351 ClipCopy(target, aBits, aUpdateRect, aRect);
353 else
355 const int byteCount = aRect.Width() * aRect.Height() * iSourceBpp; //this could be stored
356 memcpy(target, aBits, byteCount);
359 return ETrue;
362 void CDsa::UpdateSwSurface()
364 iTargetAddr = NULL;
365 UnlockHwSurface(); //could be faster if does not use AO, but only check status before redraw, then no context switch needed
368 void CDsa::Stop()
370 iRunning = false;
373 void CDsa::Start()
375 iRunning = true;
378 RWindow* CDsa::Window()
380 return iWindow;
383 CDsa* CDsa::CreateGlesDsaL()
385 CDsa* dsa = new CDsaGles(Session());
386 CWsScreenDevice* dummy = NULL;
387 dsa->ConstructL(*Window(), *dummy);
388 Free();
389 delete this;
390 return dsa;
393 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
395 void CDsa::Copy256(const CDsa& aDsa, TUint32* aTarget, const TUint8* aSource, int aBytes)
397 TUint32* target = aTarget;
398 const TUint32* endt = target + aBytes;
399 const TUint8* source = aSource;
400 while(target < endt)
402 *target++ = aDsa.iLut256[*source++];
406 void CDsa::CopyMem(const CDsa& /*aDsa*/, TUint32* aTarget, const TUint8* aSource, int aBytes)
408 memcpy(aTarget, aSource, aBytes << 2);
411 class MRgbCopy
413 public:
414 virtual void Copy(TUint32* aTarget, const TUint8* aSource, int aBytes) = 0;
417 template <class T>
418 class TRgbCopy : public MRgbCopy
420 public:
421 TRgbCopy(TDisplayMode aMode);
422 void* operator new(TUint aBytes, void* aMem);
423 void Copy(TUint32* aTarget, const TUint8* aSource, int aBytes);
424 static TUint32 Gray256(const TUint8& aPixel);
425 static TUint32 Color256(const TUint8& aPixel);
426 static TUint32 Color4K(const TUint16& aPixel);
427 static TUint32 Color64K(const TUint16& aPixel);
428 static TUint32 Color16M(const TUint32& aPixel);
429 static TUint32 Color16MU(const TUint32& aPixel);
430 static TUint32 Color16MA(const TUint32& aPixel);
431 private:
432 typedef TUint32 (*TRgbFunc) (const T& aValue);
433 TRgbFunc iFunc;
436 template <class T>
437 void* TRgbCopy<T>::operator new(TUint /*aBytes*/, void* aMem)
439 return aMem;
442 template <class T>
443 TRgbCopy<T>::TRgbCopy(TDisplayMode aMode)
445 switch(aMode)
447 case EGray256 : iFunc = (TRgbFunc) Gray256; break;
448 case EColor256 : iFunc = (TRgbFunc) Color256; break;
449 case EColor4K : iFunc = (TRgbFunc) Color4K; break;
450 case EColor64K : iFunc = (TRgbFunc) Color64K; break;
451 case EColor16M : iFunc = (TRgbFunc) Color16M; break;
452 case EColor16MU : iFunc = (TRgbFunc) Color16MU; break;
453 case EColor16MA : iFunc = (TRgbFunc) Color16MA; break;
454 default:
455 PANIC(KErrNotSupported);
459 template <class T>
460 void TRgbCopy<T>::Copy(TUint32* aTarget, const TUint8* aSource, int aBytes)
462 const T* source = reinterpret_cast<const T*>(aSource);
463 TUint32* target = aTarget;
464 TUint32* endt = target + aBytes;
466 while(target < endt)
468 const T value = *source++;
469 *target++ = iFunc(value);
473 template <class T>
474 TUint32 TRgbCopy<T>::Gray256(const TUint8& aPixel)
476 const TUint32 px = aPixel << 16 | aPixel << 8 | aPixel;
477 return px;
480 template <class T>
481 TUint32 TRgbCopy<T>::Color256(const TUint8& aPixel)
483 return TRgb::Color256(aPixel).Value();
486 template <class T>
487 TUint32 TRgbCopy<T>::Color4K(const TUint16& aPixel)
489 TUint32 col = (aPixel & 0xF00) << 12;
490 col |= (aPixel & 0xF00) << 8;
492 col |= (aPixel & 0x0F0) << 8;
493 col |= (aPixel & 0x0F0);
495 col |= (aPixel & 0x00F) << 4;
496 col |= (aPixel & 0x00F);
498 return col;
501 template <class T>
502 TUint32 TRgbCopy<T>::Color64K(const TUint16& aPixel)
504 TUint32 col = (aPixel & 0xF800)<< 8;
505 col |= (aPixel & 0xE000) << 3;
507 col |= (aPixel & 0x07E0) << 5;
508 col |= (aPixel & 0xC0) >> 1;
510 col |= (aPixel & 0x07E0) << 3;
511 col |= (aPixel & 0x1C) >> 2;
513 return col;
516 template <class T>
517 TUint32 TRgbCopy<T>::Color16M(const TUint32& aPixel)
519 return TRgb::Color16M(aPixel).Value();
522 template <class T>
523 TUint32 TRgbCopy<T>::Color16MU(const TUint32& aPixel)
525 return TRgb::Color16MU(aPixel).Value();
528 template <class T>
529 TUint32 TRgbCopy<T>::Color16MA(const TUint32& aPixel)
531 return TRgb::Color16MA(aPixel).Value();
534 typedef TUint64 TStackMem;
536 static MRgbCopy* GetCopy(void* mem, TDisplayMode aMode)
538 if(aMode == EColor256 || aMode == EGray256)
540 return new (mem) TRgbCopy<TUint8>(aMode);
542 if(aMode == EColor4K || aMode == EColor64K)
544 return new (mem) TRgbCopy<TUint16>(aMode);
546 if(aMode == EColor16M || aMode == EColor16MU || aMode == EColor16MA)
548 return new (mem) TRgbCopy<TUint32>(aMode);
550 PANIC(KErrNotSupported);
551 return NULL;
554 void CDsa::CopySlow(const CDsa& aDsa, TUint32* aTarget, const TUint8* aSource, int aBytes)
556 TStackMem mem = 0;
557 GetCopy(&mem, aDsa.iSourceMode)->Copy(aTarget, aSource, aBytes);