Big cleanup part 1.
[SDL.s60v3.git] / src / video / symbian / dsa.cpp
blobeb328abb0b73190199e8a5a7b7727f0e3964ac56
1 #include "dsa.h"
2 #include "sdlepocapi.h"
3 #include <cdsb.h>
4 #include <basched.h>
6 LOCAL_C TInt BytesPerPixel(TDisplayMode aMode)
8 return ((TDisplayModeUtils::NumDisplayModeBitsPerPixel(aMode) - 1) >> 3) + 1;
11 template<class T>
12 NONSHARABLE_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();
23 void Wipe(TInt aLength);
24 void Free();
25 void Update(CFbsBitmap& aBmp);
26 TInt ExternalUpdate();
27 CFbsBitmap* iBmp;
28 CFbsBitmap* iCopyBmp;
31 template<class T>
32 void CBitmapSurface<T>::ConstructL(RWindow& aWindow, CWsScreenDevice& aDevice)
34 delete iCopyBmp;
35 iCopyBmp = NULL;
36 iCopyBmp = new (ELeave) CFbsBitmap();
37 T::ConstructL(aWindow, aDevice);
40 template<class T>
41 CBitmapSurface<T>::CBitmapSurface(RWsSession& aSession) : T(aSession)
45 template<class T>
46 void CBitmapSurface<T>::Free()
48 delete iBmp;
49 iBmp = NULL;
50 T::Free();
53 template<class T>
54 CBitmapSurface<T>::~CBitmapSurface()
56 __ASSERT_DEBUG(iBmp == NULL, PANIC(KErrNotReady));
57 delete iCopyBmp;
60 template<class T>
61 TUint8* CBitmapSurface<T>::LockSurface()
63 iBmp->LockHeap();
64 return reinterpret_cast<TUint8*>(iBmp->DataAddress());
68 template<class T>
69 void CBitmapSurface<T>::UnlockHwSurface()
71 iBmp->UnlockHeap();
72 T::SetUpdating(EFalse);
73 Update(*iBmp);
77 template<class T>
78 void CBitmapSurface<T>::Update(CFbsBitmap& aBmp)
80 T::DoBlt(aBmp);
81 T::DrawOverlays();
82 T::CompleteUpdate();
85 template<class T>
86 void CBitmapSurface<T>::CreateSurfaceL()
88 Free();
89 iBmp = new (ELeave) CFbsBitmap();
90 User::LeaveIfError(iBmp->Create(T::SwSize(), T::DisplayMode()));
91 T::CreateSurfaceL(*iBmp);
94 template<class T>
95 void CBitmapSurface<T>::Wipe(TInt aLength) //dont call in drawing
97 iBmp->LockHeap();
98 Mem::FillZ(iBmp->DataAddress(), aLength);
99 iBmp->UnlockHeap();
102 template<class T>
103 TInt CBitmapSurface<T>::ExternalUpdate()
105 if(iCopyBmp->Handle() == 0)
107 const TInt err = iCopyBmp->Duplicate(iBmp->Handle());
108 if(err != KErrNone)
109 return err;
111 Update(*iCopyBmp);
112 return KErrNone;
116 //////////////////////////////////////////////////////////////////////
118 NONSHARABLE_CLASS(CDsaGles) : public CDsa
120 public:
121 CDsaGles(RWsSession& aSession);
122 private:
123 TUint8* LockSurface();
124 void UnlockHWSurfaceRequestComplete();
125 void UnlockHwSurface();
126 void Resume();
127 void CreateSurfaceL();
128 void Wipe(TInt aLength);
129 TInt ExternalUpdate();
130 CBitmapContext* Gc();
133 CDsaGles::CDsaGles(RWsSession& aSession) : CDsa(aSession)
137 TUint8* CDsaGles::LockSurface()
139 return NULL;
142 void CDsaGles::UnlockHWSurfaceRequestComplete()
146 void CDsaGles::UnlockHwSurface()
150 void CDsaGles::Resume()
154 void CDsaGles::CreateSurfaceL()
158 void CDsaGles::Wipe(TInt aLength)
162 TInt CDsaGles::ExternalUpdate()
164 return KErrNotSupported;
167 CBitmapContext* CDsaGles::Gc()
169 return NULL;
173 //////////////////////////////////////////////////////////////////////
176 NONSHARABLE_CLASS(CDsaBitgdi) : public CDsa
178 public:
179 CDsaBitgdi(RWsSession& aSession);
180 protected:
181 void ConstructL(RWindow& aWindow, CWsScreenDevice& aDevice);
182 CBitmapContext* Gc();
183 void CompleteUpdate();
184 ~CDsaBitgdi();
185 void CreateSurfaceL(CFbsBitmap& aBmp);
186 void Free();
187 void UnlockHWSurfaceRequestComplete();
188 private:
189 void Resume();
191 CFbsBitGc* iGc;
192 CFbsDevice* iDevice;
193 CFbsBitmap* iBitGdiBmp;
194 CWindowGc* iWinGc;
195 RWindow* iWindow;
196 TInt iHandle;
200 CDsaBitgdi::CDsaBitgdi(RWsSession& aSession) : CDsa(aSession)
204 CDsaBitgdi::~CDsaBitgdi()
206 delete iWinGc;
207 delete iBitGdiBmp;
210 void CDsaBitgdi::CompleteUpdate()
212 EpocSdlEnv::Request(CDsa::ERequestUpdate);
216 void CDsaBitgdi::UnlockHWSurfaceRequestComplete()
218 if(iHandle == 0)
219 return;
221 if(iBitGdiBmp == NULL)
223 iBitGdiBmp = new CFbsBitmap();
224 if(iBitGdiBmp == NULL)
225 return;
226 iBitGdiBmp->Duplicate(iHandle);
229 iWindow->Invalidate();
231 iWindow->BeginRedraw();
232 iWinGc->Activate(*iWindow);
234 if(!Blitter(*iBitGdiBmp))
236 if(SwSize() == HwRect().Size())
237 iWinGc->BitBlt(HwRect().iTl, iBitGdiBmp);
238 else
239 iWinGc->DrawBitmap(HwRect(), iBitGdiBmp);
242 iWinGc->Deactivate();
243 iWindow->EndRedraw();
246 void CDsaBitgdi::Resume()
248 Start();
251 CBitmapContext* CDsaBitgdi::Gc()
253 return iWinGc;
256 void CDsaBitgdi::ConstructL(RWindow& aWindow, CWsScreenDevice& aDevice)
259 delete iBitGdiBmp;
260 iBitGdiBmp = NULL;
261 delete iWinGc;
262 iWinGc = NULL;
263 iHandle = 0;
265 iWindow = &aWindow;
266 User::LeaveIfError(aDevice.CreateContext(iWinGc));
267 CDsa::ConstructL(aWindow, aDevice);
268 Start();
271 void CDsaBitgdi::CreateSurfaceL(CFbsBitmap& aBmp)
273 iDevice = CFbsBitmapDevice::NewL(&aBmp);
274 User::LeaveIfError(iDevice->CreateContext(iGc));
275 iHandle = aBmp.Handle();
278 void CDsaBitgdi::Free()
280 delete iGc;
281 iGc = NULL;
282 delete iDevice;
283 iDevice = NULL;
286 ///////////////////////////////////////////////////////////////////////
288 NONSHARABLE_CLASS(CDsaBase) : public CDsa, public MDirectScreenAccess
290 protected:
291 inline CDirectScreenAccess& Dsa() const;
292 CDsaBase(RWsSession& aSession);
293 ~CDsaBase();
294 void ConstructL(RWindow& aWindow, CWsScreenDevice& aDevice);
295 void Stop();
296 void Resume();
297 CBitmapContext* Gc();
298 protected:
299 CDirectScreenAccess* iDsa;
300 private:
301 void AbortNow(RDirectScreenAccess::TTerminationReasons aReason);
302 void Restart(RDirectScreenAccess::TTerminationReasons aReason);
303 private:
304 void RestartL();
308 inline CDirectScreenAccess& CDsaBase::Dsa() const
310 return *iDsa;
314 CDsaBase::CDsaBase(RWsSession& aSession) : CDsa(aSession)
318 CBitmapContext* CDsaBase::Gc()
320 return Dsa().Gc();
323 void CDsaBase::ConstructL(RWindow& aWindow, CWsScreenDevice& aDevice)
325 CDsa::ConstructL(aWindow, aDevice);
326 if(iDsa != NULL)
328 iDsa->Cancel();
329 delete iDsa;
330 iDsa = NULL;
333 iDsa = CDirectScreenAccess::NewL(
334 Session(),
335 aDevice,
336 aWindow,
337 *this);
338 RestartL();
341 void CDsaBase::Resume()
343 if(Stopped())
344 Restart(RDirectScreenAccess::ETerminateRegion);
347 CDsaBase::~CDsaBase()
349 if(iDsa != NULL)
351 iDsa->Cancel();
353 delete iDsa;
357 void CDsaBase::RestartL()
361 iDsa->StartL();
363 const RRegion* r = iDsa->DrawingRegion();
364 const TRect rect = r->BoundingRect();
365 iDsa->Gc()->SetClippingRegion(r);
367 if(rect != ScreenRect())
369 return ;
373 SetTargetRect();
374 RecreateL();
376 Start();
381 void CDsaBase::AbortNow(RDirectScreenAccess::TTerminationReasons /*aReason*/)
383 Stop();
386 void CDsaBase::Restart(RDirectScreenAccess::TTerminationReasons aReason)
388 if(aReason == RDirectScreenAccess::ETerminateRegion) //auto restart
390 TRAPD(err, RestartL());
391 if(err == KLeaveExit)
393 Stop();
395 else
397 PANIC_IF_ERROR(err);
403 void CDsaBase::Stop()
405 CDsa::Stop();
406 iDsa->Cancel();
409 ///////////////////////////////////////////////////////////////////////
410 NONSHARABLE_CLASS(TDsa)
412 public:
413 inline TDsa(const CDsa& aDsa);
414 inline TBool IsFlip() const;
415 inline TBool IsTurn() const;
416 inline const TSize& SwSize() const;
417 inline void Copy(TUint32* aTarget, const TUint8* aSrc, TInt aBytes, TInt aHeight) const;
418 private:
419 const CDsa& iDsa;
422 inline TDsa::TDsa(const CDsa& aDsa) : iDsa(aDsa)
426 inline TBool TDsa::IsTurn() const
428 return iDsa.iStateFlags & CDsa::EOrientation90;
431 inline TBool TDsa::IsFlip() const
433 return iDsa.iStateFlags & CDsa::EOrientation180;
436 inline const TSize& TDsa::SwSize() const
438 return iDsa.SwSize();
441 inline void TDsa::Copy(TUint32* aTarget, const TUint8* aSrc, TInt aBytes, TInt aHeight) const
443 iDsa.iCopyFunction(iDsa, aTarget, aSrc, aBytes, aHeight);
446 template<class T, class S>
447 void ClipCopy(const TDsa& iDsa, TUint8* aTarget,
448 const TUint8* aSource,
449 const TRect& aUpdateRect,
450 const TRect& aSourceRect)
452 const S* source = reinterpret_cast<const S*>(aSource);
453 const TInt lineWidth = aSourceRect.Width();
455 source += (aUpdateRect.iTl.iY * lineWidth);
456 const TInt sourceStartOffset = aUpdateRect.iTl.iX;
457 source += sourceStartOffset;
459 T* targetPtr = reinterpret_cast<T*>(aTarget);
461 const TInt scanLineWidth = iDsa.SwSize().iWidth;
463 targetPtr += (aSourceRect.iTl.iY + aUpdateRect.iTl.iY ) * scanLineWidth;
464 const TInt targetStartOffset = (aUpdateRect.iTl.iX + aSourceRect.iTl.iX);
466 targetPtr += targetStartOffset;
469 const TInt height = aUpdateRect.Height();
471 const TInt lineMove = iDsa.IsTurn() ? 1 : lineWidth;
472 const TInt copyLen = aUpdateRect.Width();
475 if(iDsa.IsFlip())
478 targetPtr += scanLineWidth * (height - 1);
480 for(TInt i = 0; i < height; i++) //source is always smaller
482 iDsa.Copy(reinterpret_cast<TUint32*>(targetPtr), reinterpret_cast<const TUint8*>(source), copyLen, height);
483 source += lineMove;
484 targetPtr -= scanLineWidth;
487 else
491 for(TInt i = 0; i < height; i++) //source is always smaller
493 iDsa.Copy(reinterpret_cast<TUint32*>(targetPtr), reinterpret_cast<const TUint8*>(source), copyLen, height);
494 source += lineMove;
495 targetPtr += scanLineWidth; // >> 2;
503 NONSHARABLE_CLASS(CDsaA) : public CDsaBase
505 public:
506 CDsaA(RWsSession& aSession);
507 protected:
508 void ConstructL(RWindow& aWindow, CWsScreenDevice& aDevice);
509 void CompleteUpdate();
510 void CreateSurfaceL(CFbsBitmap& aBmp);
511 void Free();
512 void UnlockHWSurfaceRequestComplete();
513 void DoBlt(CFbsBitmap& aBmp);
517 CDsaA::CDsaA(RWsSession& aSession) : CDsaBase(aSession)
522 void CDsaA::ConstructL(RWindow& aWindow, CWsScreenDevice& aDevice)
524 CDsaBase::ConstructL(aWindow, aDevice);
527 void CDsaA::CompleteUpdate()
529 iDsa->ScreenDevice()->Update();
532 void CDsaA::CreateSurfaceL(CFbsBitmap& /*aBmp*/)
536 void CDsaA::Free()
540 void CDsaA::DoBlt(CFbsBitmap& aBmp)
542 if(!Blitter(aBmp))
544 if(SwSize() == HwRect().Size())
545 Gc()->BitBlt(HwRect().iTl, &aBmp);
546 else
547 Gc()->DrawBitmap(HwRect(), &aBmp);
551 void CDsaA::UnlockHWSurfaceRequestComplete()
553 PANIC(KErrNotSupported);
558 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
560 NONSHARABLE_CLASS(MDsbObs)
562 public:
563 virtual void SurfaceReady() = 0;
564 virtual CDirectScreenBitmap& Dsb() = 0;
567 NONSHARABLE_CLASS(CDsbSurface) : public CActive
569 public:
570 CDsbSurface(MDsbObs& aDsb);
571 virtual TUint8* Address() = 0;
572 virtual void Complete() = 0;
573 virtual void Unlock() = 0;
574 ~CDsbSurface();
575 protected:
576 void RunL();
577 void DoCancel();
578 protected:
579 MDsbObs& iDsb;
580 TUint8* iAddress;
583 CDsbSurface::CDsbSurface(MDsbObs& aDsb) : CActive(CActive::EPriorityHigh) , iDsb(aDsb)
585 CActiveScheduler::Add(this);
588 CDsbSurface::~CDsbSurface()
590 Cancel();
593 void CDsbSurface::RunL()
595 iDsb.SurfaceReady();
598 void CDsbSurface::DoCancel()
600 //empty
603 NONSHARABLE_CLASS(CDsbSyncSurface) : public CDsbSurface
605 public:
606 TUint8* Address();
607 CDsbSyncSurface(MDsbObs& aDsb);
608 void Unlock();
609 void Complete();
610 ~CDsbSyncSurface();
611 RSemaphore iSema;
612 private:
613 void RunL();
616 CDsbSyncSurface::CDsbSyncSurface(MDsbObs& aDsb) : CDsbSurface(aDsb)
618 iSema.CreateLocal(1);
621 TUint8* CDsbSyncSurface::Address()
623 if(IsActive())
624 iSema.Wait();
625 TAcceleratedBitmapInfo info;
626 if(KErrNone == iDsb.Dsb().BeginUpdate(info))
627 iAddress = info.iAddress;
628 return iAddress;
631 void CDsbSyncSurface::Unlock()
633 EpocSdlEnv::Request(CDsa::ERequestUpdate);
636 void CDsbSyncSurface::RunL()
638 CDsbSurface::RunL();
639 iSema.Signal();
642 CDsbSyncSurface::~CDsbSyncSurface()
644 iSema.Signal();
645 iSema.Close();
648 void CDsbSyncSurface::Complete()
650 if(iAddress != NULL && !IsActive())
652 iAddress = NULL;
653 SetActive();
654 iDsb.Dsb().EndUpdate(iStatus);
656 else
657 iSema.Signal();
660 NONSHARABLE_CLASS(CDsbAsyncSurface) : public CDsbSurface
662 public:
663 TUint8* Address();
664 CDsbAsyncSurface(MDsbObs& aDsb);
665 void Complete();
666 void Unlock();
667 ~CDsbAsyncSurface();
670 TUint8* CDsbAsyncSurface::Address()
672 if(iAddress == NULL && !IsActive())
674 TAcceleratedBitmapInfo info;
675 if(KErrNone == iDsb.Dsb().BeginUpdate(info))
676 iAddress = info.iAddress;
678 return iAddress;
681 CDsbAsyncSurface::CDsbAsyncSurface(MDsbObs& aDsb) : CDsbSurface(aDsb)
685 CDsbAsyncSurface::~CDsbAsyncSurface()
689 void CDsbAsyncSurface::Unlock()
691 EpocSdlEnv::Request(CDsa::ERequestUpdate);
694 void CDsbAsyncSurface::Complete()
696 if(iAddress != NULL && !IsActive())
698 iAddress = NULL;
699 SetActive();
700 iDsb.Dsb().EndUpdate(iStatus);
704 NONSHARABLE_CLASS(CDsaB) : public CDsaBase,
705 public MDsbObs
707 public:
708 enum {EDrawModeDSBAsync = 0x1000};
709 CDsaB(RWsSession& aSession, TInt aFlags);
710 private:
711 ~CDsaB();
712 TUint8* LockSurface();
713 void UnlockHWSurfaceRequestComplete();
714 void UnlockHwSurface();
715 void CreateSurfaceL();
716 void Wipe(TInt aLength);
717 void RecreateL();
718 void ConstructL(RWindow& aWindow, CWsScreenDevice& aDevice);
719 CDirectScreenBitmap& Dsb();
720 void SurfaceReady();
721 TInt ExternalUpdate();
722 private:
723 CDsbSurface* iSurface1;
724 CDirectScreenBitmap* iDsb;
725 TInt iType;
728 CDsaB::CDsaB(RWsSession& aSession, TInt aFlags) : CDsaBase(aSession), iType(aFlags)
734 void CDsaB::UnlockHWSurfaceRequestComplete()
736 iSurface1->Complete();
739 void CDsaB::CreateSurfaceL()
741 __ASSERT_ALWAYS(SwSize() == HwRect().Size(), PANIC(KErrNotSupported));
744 void CDsaB::Wipe(TInt aLength) //dont call in drawing
746 TUint8* addr = LockSurface();
747 if(addr != NULL)
749 Mem::FillZ(addr, aLength);
750 UnlockHwSurface();
755 void CDsaB::UnlockHwSurface()
757 iSurface1->Unlock();
760 TUint8* CDsaB::LockSurface()
762 TUint8* addr = iSurface1->Address();
763 SetUpdating(addr == NULL);
764 return addr;
767 void CDsaB::SurfaceReady()
769 SetUpdating(EFalse);
772 CDirectScreenBitmap& CDsaB::Dsb()
774 return *iDsb;
777 void CDsaB::ConstructL(RWindow& aWindow, CWsScreenDevice& aDevice)
779 if(iDsb == NULL)
780 iDsb = CDirectScreenBitmap::NewL();
781 CDsaBase::ConstructL(aWindow, aDevice);
782 if(iSurface1 == NULL)
784 if( (iType & CDsaB::EDrawModeDSBAsync))
785 iSurface1 = new (ELeave) CDsbAsyncSurface(*this);
786 else
787 iSurface1 = new (ELeave) CDsbSyncSurface(*this);
791 CDsaB::~CDsaB()
793 delete iSurface1;
794 delete iDsb;
797 void CDsaB::RecreateL()
799 iDsb->Close();
800 CDirectScreenBitmap::TSettingsFlags flags = CDirectScreenBitmap::TSettingsFlags(iType & 0xFF);
801 iDsb->Create(HwRect(), flags);
804 TInt CDsaB::ExternalUpdate()
806 if(LockSurface())
808 UnlockHWSurfaceRequestComplete();
809 return KErrNone;
811 return KErrNotReady;
814 /////////////////////////////////////////////////////////////////////////////////////////////////////
816 CDsa* CDsa::CreateL(RWsSession& aSession)
818 if(EpocSdlEnv::Flags(CSDL::EDrawModeDSB))
820 TInt flags = CDirectScreenBitmap::ENone;
821 if(EpocSdlEnv::Flags(CSDL::EDrawModeDSBDoubleBuffer))
822 flags |= CDirectScreenBitmap::EDoubleBuffer;
823 if(EpocSdlEnv::Flags(CSDL::EDrawModeDSBIncrementalUpdate))
824 flags |= CDirectScreenBitmap::EIncrementalUpdate;
825 if(EpocSdlEnv::Flags(CSDL::EDrawModeDSBAsync))
826 flags |= CDsaB::EDrawModeDSBAsync;
827 return new (ELeave) CDsaB(aSession, flags);
829 else if(EpocSdlEnv::Flags(CSDL::EDrawModeGdi))
831 return new (ELeave) CBitmapSurface<CDsaBitgdi>(aSession);
833 else
835 return new (ELeave) CBitmapSurface<CDsaA>(aSession);
840 void CDsa::RecreateL()
844 void CDsa::Free()
848 TSize CDsa::WindowSize() const
850 TSize size = iSwSize;
851 if(iStateFlags & EOrientation90)
853 const TInt tmp = size.iWidth;
854 size.iWidth = size.iHeight;
855 size.iHeight = tmp;
857 return size;
860 void CDsa::SetSuspend()
862 iStateFlags |= ESdlThreadSuspend;
866 void CDsa::SetUpdating(TBool aUpdate)
868 if(aUpdate)
869 iStateFlags |= EUpdating;
870 else
871 iStateFlags &= ~EUpdating;
875 TBool CDsa::Stopped() const
877 return (iStateFlags & ESdlThreadExplicitStop);
880 void CDsa::SetOrientation(CDsa::TOrientationMode aOrientation)
882 TInt flags = 0;
883 switch(aOrientation)
885 case CDsa::EViewOrientation90:
886 flags = EOrientation90;
887 break;
888 case CDsa::EViewOrientation180:
889 flags = EOrientation180;
890 break;
891 case CDsa::EViewOrientation270:
892 flags = EOrientation90 | EOrientation180;
893 break;
894 case CDsa::EViewOrientation0:
895 flags = 0;
896 break;
898 if(flags != (iStateFlags & EOrientationFlags))
900 iStateFlags |= EOrientationChanged;
901 iNewFlags = flags; //cannot be set during drawing...
905 CDsa::~CDsa()
907 iOverlays.Close();
908 User::Free(iLut256);
911 void CDsa::ConstructL(RWindow& aWindow, CWsScreenDevice& /*aDevice*/)
913 if(iLut256 == NULL)
914 iLut256 = (TUint32*) User::AllocL(256 * sizeof(TUint32));
915 iTargetMode = aWindow.DisplayMode();
916 iTargetBpp = BytesPerPixel(DisplayMode());
917 iScreenRect = TRect(aWindow.Position(), aWindow.Size());
918 SetTargetRect();
919 iWindow = &aWindow;
923 void CDsa::DrawOverlays()
925 const TInt last = iOverlays.Count() - 1;
926 for(TInt i = last; i >= 0 ; i--)
927 iOverlays[i].iOverlay->Draw(*Gc(), HwRect(), SwSize());
930 void CDsa::DoBlt(CFbsBitmap& /*aBmp*/)
934 TInt CDsa::AppendOverlay(MOverlay& aOverlay, TInt aPriority)
936 TInt i;
937 for(i = 0; i < iOverlays.Count() && iOverlays[i].iPriority < aPriority; i++)
939 const TOverlay overlay = {&aOverlay, aPriority};
940 return iOverlays.Insert(overlay, i);
943 TInt CDsa::RemoveOverlay(MOverlay& aOverlay)
945 for(TInt i = 0; i < iOverlays.Count(); i++)
947 if(iOverlays[i].iOverlay == &aOverlay)
949 iOverlays.Remove(i);
950 return KErrNone;
953 return KErrNotFound;
956 void CDsa::LockPalette(TBool aLock)
958 if(aLock)
959 iStateFlags |= EPaletteLocked;
960 else
961 iStateFlags &= ~EPaletteLocked;
963 TInt CDsa::SetPalette(TInt aFirst, TInt aCount, TUint32* aPalette)
965 if(iLut256 == NULL)
966 return KErrNotFound;
967 const TInt count = aCount - aFirst;
968 if(count > 256)
969 return KErrArgument;
970 if(iStateFlags & EPaletteLocked)
971 return KErrNone;
972 for(TInt i = aFirst; i < count; i++) //not so busy here:-)
974 iLut256[i] = aPalette[i];
976 return KErrNone;
979 CDsa::CDsa(RWsSession& aSession) :
980 iStateFlags(0),
981 iSession(aSession)
984 iCFTable[0] = CopyMem;
985 iCFTable[1] = CopyMemFlipReversed;
986 iCFTable[2] = CopyMemReversed;
987 iCFTable[3] = CopyMemFlip;
989 iCFTable[4] = Copy256;
990 iCFTable[5] = Copy256FlipReversed;
991 iCFTable[6] = Copy256Reversed;
992 iCFTable[7] = Copy256Flip;
995 iCFTable[8] = CopySlow;
996 iCFTable[9] = CopySlowFlipReversed;
997 iCFTable[10] = CopySlowReversed;
998 iCFTable[11] = CopySlowFlip;
1001 RWsSession& CDsa::Session()
1003 return iSession;
1006 TInt CDsa::RedrawRequest()
1008 if(!(iStateFlags & (EUpdating) && (iStateFlags & ERunning)))
1010 return ExternalUpdate();
1012 return KErrNotReady;
1015 TUint8* CDsa::LockHwSurface()
1017 if((iStateFlags & EUpdating) == 0) //else frame is skipped
1019 return LockSurface();
1021 return NULL;
1024 TInt CDsa::AllocSurface(TBool aHwSurface, const TSize& aSize, TDisplayMode aMode)
1026 if(aHwSurface && aMode != DisplayMode())
1027 return KErrArgument;
1029 iSourceMode = aMode;
1031 iSourceBpp = BytesPerPixel(aMode);
1033 const TSize size = WindowSize();
1034 if(aSize.iWidth > size.iWidth)
1035 return KErrTooBig;
1036 if(aSize.iHeight > size.iHeight)
1037 return KErrTooBig;
1039 TRAPD(err, CreateSurfaceL());
1040 if(err != KErrNone)
1041 return err;
1043 SetCopyFunction();
1045 return KErrNone;
1049 void CDsa::CreateZoomerL(const TSize& aSize)
1051 iSwSize = aSize;
1052 iStateFlags |= EResizeRequest;
1053 CreateSurfaceL();
1054 SetTargetRect();
1057 void CDsa::ClipCopy(TUint8* aTarget,
1058 const TUint8* aSource,
1059 const TRect& aUpdateRect,
1060 const TRect& aSourceRect) const
1062 const TDsa dsa(*this);
1063 switch(iSourceBpp)
1065 case 1:
1066 ::ClipCopy<TUint32, TUint8>(dsa, aTarget, aSource, aUpdateRect, aSourceRect);
1067 break;
1068 case 2:
1069 ::ClipCopy<TUint32, TUint16>(dsa, aTarget, aSource, aUpdateRect, aSourceRect);
1070 break;
1071 case 4:
1072 ::ClipCopy<TUint32, TUint32>(dsa, aTarget, aSource, aUpdateRect, aSourceRect);
1073 break;
1078 void CDsa::Wipe() //dont call in drawing
1080 if(IsDsaAvailable())
1081 Wipe(iTargetBpp * SwSize().iWidth * SwSize().iHeight);
1084 void CDsa::SetCopyFunction()
1086 //calculate offset to correct function in iCFTable according to given parameters
1087 TInt function = 0;
1088 const TInt KCopyFunctions = 4;
1089 const TInt KOffsetToNative = 0;
1090 const TInt KOffsetTo256 = KOffsetToNative + KCopyFunctions;
1091 const TInt KOffsetToOtherModes = KOffsetTo256 + KCopyFunctions;
1092 const TInt KOffsetTo90Functions = 1;
1093 const TInt KOffsetTo180Functions = 2;
1095 if(iSourceMode == DisplayMode())
1096 function = KOffsetToNative; //0
1097 else if(iSourceMode == EColor256)
1098 function = KOffsetTo256; //4
1099 else
1100 function = KOffsetToOtherModes; //8
1102 if(iStateFlags & EOrientation90)
1103 function += KOffsetTo90Functions; // + 1
1104 if(iStateFlags & EOrientation180)
1105 function += KOffsetTo180Functions; //+ 2
1107 iCopyFunction = iCFTable[function];
1109 Wipe();
1112 inline void Rotate(TRect& aRect)
1114 const TInt dx = aRect.iBr.iX - aRect.iTl.iX;
1115 const TInt dy = aRect.iBr.iY - aRect.iTl.iY;
1117 aRect.iBr.iX = aRect.iTl.iX + dy;
1118 aRect.iBr.iY = aRect.iTl.iY + dx;
1120 const TInt tmp = aRect.iTl.iX;
1121 aRect.iTl.iX = aRect.iTl.iY;
1122 aRect.iTl.iY = tmp;
1125 TBool CDsa::AddUpdateRect(const TUint8* aBits, const TRect& aUpdateRect, const TRect& aRect)
1128 if(iStateFlags & EOrientationChanged)
1130 iStateFlags &= ~EOrientationFlags;
1131 iStateFlags |= iNewFlags;
1132 SetCopyFunction();
1133 iStateFlags &= ~EOrientationChanged;
1134 EpocSdlEnv::WaitDeviceChange();
1135 return EFalse; //skip this frame as data is may be changed
1138 if(iTargetAddr == NULL)
1140 iTargetAddr = LockHwSurface();
1143 TUint8* target = iTargetAddr;
1144 if(target == NULL)
1145 return EFalse;
1148 TRect targetRect = TRect(TPoint(0, 0), SwSize());
1150 TRect sourceRect = aRect;
1151 TRect updateRect = aUpdateRect;
1153 if(iStateFlags & EOrientation90)
1155 Rotate(sourceRect);
1156 Rotate(updateRect);
1159 if(iSourceMode != DisplayMode() || targetRect != sourceRect || targetRect != updateRect || ((iStateFlags & EOrientationFlags) != 0))
1161 sourceRect.Intersection(targetRect); //so source always smaller or equal than target
1162 ClipCopy(target, aBits, updateRect, sourceRect);
1164 else
1166 const TInt byteCount = aRect.Width() * aRect.Height() * iSourceBpp; //this could be stored
1167 Mem::Copy(target, aBits, byteCount);
1170 return ETrue;
1174 void CDsa::UpdateSwSurface()
1176 iTargetAddr = NULL;
1177 UnlockHwSurface(); //could be faster if does not use AO, but only check status before redraw, then no context switch needed
1180 void CDsa::DoStop()
1182 if(IsDsaAvailable())
1183 iStateFlags |= ESdlThreadExplicitStop;
1184 Stop();
1188 void CDsa::Stop()
1190 iStateFlags &= ~ERunning;
1193 void CDsa::Start()
1195 iStateFlags |= ERunning;
1197 iStateFlags &= ~ESdlThreadExplicitStop;
1199 if(iStateFlags & ESdlThreadSuspend)
1201 EpocSdlEnv::Resume();
1202 iStateFlags &= ~ ESdlThreadSuspend;
1204 EpocSdlEnv::ObserverEvent(MSDLObserver::EEventWindowReserved);
1208 TBool CDsa::Blitter(CFbsBitmap& aBmp)
1210 return iBlitter && iBlitter->BitBlt(*Gc(), aBmp, HwRect(), SwSize());
1213 void CDsa::SetBlitter(MBlitter* aBlitter)
1215 iBlitter = aBlitter;
1219 TPoint CDsa::WindowCoordinates(const TPoint& aPoint) const
1221 TPoint pos = aPoint - iScreenRect.iTl;
1222 const TSize asz = iScreenRect.Size();
1223 if(iStateFlags & EOrientation180)
1225 pos.iX = asz.iWidth - pos.iX;
1226 pos.iY = asz.iHeight - pos.iY;
1228 if(iStateFlags & EOrientation90)
1230 pos.iX = aPoint.iY;
1231 pos.iY = aPoint.iX;
1233 pos.iX <<= 16;
1234 pos.iY <<= 16;
1235 pos.iX /= asz.iWidth;
1236 pos.iY /= asz.iHeight;
1237 pos.iX *= iSwSize.iWidth;
1238 pos.iY *= iSwSize.iHeight;
1239 pos.iX >>= 16;
1240 pos.iY >>= 16;
1241 return pos;
1244 void CDsa::SetTargetRect()
1246 iTargetRect = iScreenRect;
1247 if(iStateFlags & EResizeRequest && EpocSdlEnv::Flags(CSDL::EAllowImageResizeKeepRatio))
1249 const TSize asz = iScreenRect.Size();
1250 const TSize sz = iSwSize;
1252 TRect rect;
1254 const TInt dh = (sz.iHeight << 16) / sz.iWidth;
1256 if((asz.iWidth * dh ) >> 16 <= asz.iHeight)
1258 rect.SetRect(TPoint(0, 0), TSize(asz.iWidth, (asz.iWidth * dh) >> 16));
1260 else
1262 const TInt dw = (sz.iWidth << 16) / sz.iHeight;
1263 rect.SetRect(TPoint(0, 0), TSize((asz.iHeight * dw) >> 16, asz.iHeight));
1265 rect.Move((asz.iWidth - rect.Size().iWidth) >> 1, (asz.iHeight - rect.Size().iHeight) >> 1);
1267 iTargetRect = rect;
1268 iTargetRect.Move(iScreenRect.iTl);
1271 if(!(iStateFlags & EResizeRequest))
1272 iSwSize = iScreenRect.Size();
1276 RWindow* CDsa::Window()
1278 return iWindow;
1281 CDsa* CDsa::CreateGlesDsaL()
1283 CDsa* dsa = new (ELeave) CDsaGles(Session());
1284 CWsScreenDevice* dummy = NULL;
1285 dsa->ConstructL(*Window(), *dummy);
1286 Free();
1287 delete this;
1288 return dsa;
1292 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
1294 void CDsa::Copy256(const CDsa& aDsa, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt)
1296 TUint32* target = aTarget;
1297 const TUint32* endt = target + aBytes;
1298 const TUint8* source = aSource;
1299 while(target < endt)
1301 *target++ = aDsa.iLut256[*source++];
1305 void CDsa::Copy256Reversed(const CDsa& aDsa, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt)
1307 const TUint32* target = aTarget;
1308 TUint32* endt = aTarget + aBytes;
1309 const TUint8* source = aSource;
1310 while(target < endt)
1312 *(--endt) = aDsa.iLut256[*source++];
1316 void CDsa::Copy256Flip(const CDsa& aDsa, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt aLineLen)
1318 TUint32* target = aTarget;
1319 const TUint32* endt = target + aBytes;
1320 const TUint8* column = aSource;
1322 while(target < endt)
1324 *target++ = aDsa.iLut256[*column];
1325 column += aLineLen;
1329 void CDsa::Copy256FlipReversed(const CDsa& aDsa, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt aLineLen)
1331 const TUint32* target = aTarget;
1332 TUint32* endt = aTarget + aBytes;
1333 const TUint8* column = aSource;
1335 while(target < endt)
1337 *(--endt) = aDsa.iLut256[*column];
1338 column += aLineLen;
1342 void CDsa::CopyMem(const CDsa& /*aDsa*/, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt)
1344 const TUint32* src = reinterpret_cast<const TUint32*>(aSource);
1345 Mem::Copy(aTarget, src, aBytes << 2);
1348 void CDsa::CopyMemFlip(const CDsa& /*aDsa*/, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt aLineLen)
1350 TUint32* target = aTarget;
1351 const TUint32* endt = target + aBytes;
1352 const TUint32* column = reinterpret_cast<const TUint32*>(aSource);
1354 while(target < endt)
1356 *target++ = *column;
1357 column += aLineLen;
1361 void CDsa::CopyMemReversed(const CDsa& /*aDsa*/, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt)
1363 const TUint32* target = aTarget;
1364 TUint32* endt = aTarget + aBytes;
1365 const TUint32* source = reinterpret_cast<const TUint32*>(aSource);
1366 while(target < endt)
1368 *(--endt) = *source++;
1373 void CDsa::CopyMemFlipReversed(const CDsa& /*aDsa*/, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt aLineLen)
1375 const TUint32* target = aTarget;
1376 TUint32* endt = aTarget + aBytes;
1377 const TUint32* column = reinterpret_cast<const TUint32*>(aSource);
1379 while(target < endt)
1381 *(--endt) = *column;
1382 column += aLineLen;
1386 NONSHARABLE_CLASS(MRgbCopy)
1388 public:
1389 virtual void Copy(TUint32* aTarget, const TUint8* aSource, TInt aBytes, TBool aReversed) = 0;
1390 virtual void FlipCopy(TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt aLineLen, TBool aReversed) = 0;
1393 template <class T>
1394 NONSHARABLE_CLASS(TRgbCopy) : public MRgbCopy
1396 public:
1397 TRgbCopy(TDisplayMode aMode);
1398 void* operator new(TUint aBytes, TAny* aMem);
1399 void Copy(TUint32* aTarget, const TUint8* aSource, TInt aBytes, TBool aReversed);
1400 void FlipCopy(TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt aLineLen, TBool aReversed);
1401 static TUint32 Gray256(const TUint8& aPixel);
1402 static TUint32 Color256(const TUint8& aPixel);
1403 static TUint32 Color4K(const TUint16& aPixel);
1404 static TUint32 Color64K(const TUint16& aPixel);
1405 static TUint32 Color16M(const TUint32& aPixel);
1406 static TUint32 Color16MU(const TUint32& aPixel);
1407 static TUint32 Color16MA(const TUint32& aPixel);
1408 private:
1409 typedef TUint32 (*TRgbFunc) (const T& aValue);
1410 TRgbFunc iFunc;
1414 template <class T>
1415 void* TRgbCopy<T>::operator new(TUint /*aBytes*/, TAny* aMem)
1417 return aMem;
1420 template <class T>
1421 TRgbCopy<T>::TRgbCopy(TDisplayMode aMode)
1423 switch(aMode)
1425 case EGray256 : iFunc = (TRgbFunc) Gray256; break;
1426 case EColor256 : iFunc = (TRgbFunc) Color256; break;
1427 case EColor4K : iFunc = (TRgbFunc) Color4K; break;
1428 case EColor64K : iFunc = (TRgbFunc) Color64K; break;
1429 case EColor16M : iFunc = (TRgbFunc) Color16M; break;
1430 case EColor16MU : iFunc = (TRgbFunc) Color16MU; break;
1431 case EColor16MA : iFunc = (TRgbFunc) Color16MA; break;
1432 default:
1433 PANIC(KErrNotSupported);
1437 template <class T>
1438 void TRgbCopy<T>::Copy(TUint32* aTarget, const TUint8* aSource, TInt aBytes, TBool aReversed)
1440 const T* source = reinterpret_cast<const T*>(aSource);
1441 TUint32* target = aTarget;
1442 TUint32* endt = target + aBytes;
1444 if(aReversed)
1446 while(target < endt)
1448 const T value = *source++;
1449 *(--endt) = iFunc(value);
1452 else
1454 while(target < endt)
1456 const T value = *source++;
1457 *target++ = iFunc(value);
1462 template <class T>
1463 void TRgbCopy<T>::FlipCopy(TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt aLineLen, TBool aReversed)
1465 const T* column = reinterpret_cast<const T*>(aSource);
1466 TUint32* target = aTarget;
1467 TUint32* endt = target + aBytes;
1469 if(aReversed)
1471 while(target < endt)
1473 *(--endt) = iFunc(*column);
1474 column += aLineLen;
1477 else
1479 while(target < endt)
1481 *target++ = iFunc(*column);
1482 column += aLineLen;
1487 template <class T> TUint32 TRgbCopy<T>::Gray256(const TUint8& aPixel)
1489 const TUint32 px = aPixel << 16 | aPixel << 8 | aPixel;
1490 return px;
1493 template <class T> TUint32 TRgbCopy<T>::Color256(const TUint8& aPixel)
1495 return TRgb::Color256(aPixel).Value();
1498 template <class T> TUint32 TRgbCopy<T>::Color4K(const TUint16& aPixel)
1500 TUint32 col = (aPixel & 0xF00) << 12;
1501 col |= (aPixel & 0xF00) << 8;
1503 col |= (aPixel & 0x0F0) << 8;
1504 col |= (aPixel & 0x0F0);
1506 col |= (aPixel & 0x00F) << 4;
1507 col |= (aPixel & 0x00F);
1509 return col;
1512 template <class T> TUint32 TRgbCopy<T>::Color64K(const TUint16& aPixel)
1514 TUint32 col = (aPixel & 0xF800)<< 8;
1515 col |= (aPixel & 0xE000) << 3;
1517 col |= (aPixel & 0x07E0) << 5;
1518 col |= (aPixel & 0xC0) >> 1;
1520 col |= (aPixel & 0x07E0) << 3;
1521 col |= (aPixel & 0x1C) >> 2;
1523 return col;
1526 template <class T> TUint32 TRgbCopy<T>::Color16M(const TUint32& aPixel)
1528 return TRgb::Color16M(aPixel).Value();
1531 template <class T> TUint32 TRgbCopy<T>::Color16MU(const TUint32& aPixel)
1533 return TRgb::Color16MU(aPixel).Value();
1536 template <class T> TUint32 TRgbCopy<T>::Color16MA(const TUint32& aPixel)
1538 return TRgb::Color16MA(aPixel).Value();
1541 typedef TUint64 TStackMem;
1543 LOCAL_C MRgbCopy* GetCopy(TAny* mem, TDisplayMode aMode)
1545 if(aMode == EColor256 || aMode == EGray256)
1547 return new (mem) TRgbCopy<TUint8>(aMode);
1549 if(aMode == EColor4K || aMode == EColor64K)
1551 return new (mem) TRgbCopy<TUint16>(aMode);
1553 if(aMode == EColor16M || aMode == EColor16MU || aMode == EColor16MA)
1555 return new (mem) TRgbCopy<TUint32>(aMode);
1557 PANIC(KErrNotSupported);
1558 return NULL;
1562 void CDsa::CopySlowFlipReversed(const CDsa& aDsa, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt aLineLen)
1564 TStackMem mem = 0;
1565 GetCopy(&mem, aDsa.iSourceMode)->FlipCopy(aTarget, aSource, aBytes, aLineLen, ETrue);
1568 void CDsa::CopySlowFlip(const CDsa& aDsa, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt aLineLen)
1570 TStackMem mem = 0;
1571 GetCopy(&mem, aDsa.iSourceMode)->FlipCopy(aTarget, aSource, aBytes, aLineLen, EFalse);
1574 void CDsa::CopySlow(const CDsa& aDsa, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt)
1576 TStackMem mem = 0;
1577 GetCopy(&mem, aDsa.iSourceMode)->Copy(aTarget, aSource, aBytes, EFalse);
1580 void CDsa::CopySlowReversed(const CDsa& aDsa, TUint32* aTarget, const TUint8* aSource, TInt aBytes, TInt)
1582 TStackMem mem = 0;
1583 GetCopy(&mem, aDsa.iSourceMode)->Copy(aTarget, aSource, aBytes, ETrue);