2 #include "sdlepocapi.h"
6 static int BytesPerPixel(TDisplayMode aMode
)
8 return ((TDisplayModeUtils::NumDisplayModeBitsPerPixel(aMode
) - 1) >> 3) + 1;
12 class CBitmapSurface
: public T
15 CBitmapSurface(RWsSession
& aSession
);
18 void ConstructL(RWindow
& aWindow
, CWsScreenDevice
& aDevice
);
20 TUint8
* LockSurface();
21 void UnlockHwSurface();
22 void CreateSurfaceL(const TSize
& aSize
);
24 void Update(CFbsBitmap
& aBmp
);
29 void CBitmapSurface
<T
>::ConstructL(RWindow
& aWindow
, CWsScreenDevice
& aDevice
)
31 T::ConstructL(aWindow
, aDevice
);
35 CBitmapSurface
<T
>::CBitmapSurface(RWsSession
& aSession
) : T(aSession
)
40 void CBitmapSurface
<T
>::Free()
48 CBitmapSurface
<T
>::~CBitmapSurface()
50 __ASSERT_DEBUG(iBmp
== NULL
, PANIC(KErrNotReady
));
54 TUint8
* CBitmapSurface
<T
>::LockSurface()
57 return reinterpret_cast<TUint8
*>(iBmp
->DataAddress());
61 void CBitmapSurface
<T
>::UnlockHwSurface()
64 T::SetUpdating(EFalse
);
69 void CBitmapSurface
<T
>::Update(CFbsBitmap
& aBmp
)
76 void CBitmapSurface
<T
>::CreateSurfaceL(const TSize
& aSize
)
79 iBmp
= new CFbsBitmap();
80 User::LeaveIfError(iBmp
->Create(aSize
, T::DisplayMode()));
83 //////////////////////////////////////////////////////////////////////
85 class CDsaGles
: public CDsa
88 CDsaGles(RWsSession
& aSession
) : CDsa(aSession
) {}
91 TUint8
* LockSurface() { return NULL
; }
92 void UnlockHwSurface() {}
94 void CreateSurfaceL(const TSize
& aSize
) {}
97 //////////////////////////////////////////////////////////////////////
99 class CDsaBase
: public CDsa
, public MDirectScreenAccess
102 CDsaBase(RWsSession
& aSession
) : CDsa(aSession
) { m_updateWholeScreen
= false; }
104 void ConstructL(RWindow
& aWindow
, CWsScreenDevice
& aDevice
);
107 void CompleteUpdate();
108 void DoBlt(CFbsBitmap
& aBmp
);
110 CDirectScreenAccess
* iDsa
;
113 void AbortNow(RDirectScreenAccess::TTerminationReasons aReason
);
114 void Restart(RDirectScreenAccess::TTerminationReasons aReason
);
118 void CDsaBase::ConstructL(RWindow
& aWindow
, CWsScreenDevice
& aDevice
)
120 CDsa::ConstructL(aWindow
, aDevice
);
128 iDsa
= CDirectScreenAccess::NewL(Session(), aDevice
, aWindow
, *this);
132 void CDsaBase::Resume()
135 Restart(RDirectScreenAccess::ETerminateRegion
);
138 void CDsaBase::CompleteUpdate()
140 iDsa
->ScreenDevice()->Update();
143 void CDsaBase::DoBlt(CFbsBitmap
& aBmp
)
145 iDsa
->Gc()->BitBlt(TPoint(0, 0), &aBmp
);
148 CDsaBase::~CDsaBase()
157 void CDsaBase::RestartL()
160 iDsa
->Gc()->SetClippingRegion(iDsa
->DrawingRegion());
165 void CDsaBase::AbortNow(RDirectScreenAccess::TTerminationReasons
/*aReason*/)
170 void CDsaBase::Restart(RDirectScreenAccess::TTerminationReasons
/*aReason*/)
172 TRAPD(err
, RestartL());
173 if(err
== KLeaveExit
)
183 void CDsaBase::Stop()
189 ///////////////////////////////////////////////////////////////////////
194 inline TDsa(const CDsa
& aDsa
) : iDsa(aDsa
) {}
195 inline void Copy(TUint32
* aTarget
, const TUint8
* aSrc
, int aBytes
) const;
200 inline void TDsa::Copy(TUint32
* aTarget
, const TUint8
* aSrc
, int aBytes
) const
202 iDsa
.iCopyFunction(iDsa
, aTarget
, aSrc
, aBytes
);
205 template<class T
, class S
>
206 void ClipCopy(const TDsa
& iDsa
, TUint8
* aTarget
,
207 const TUint8
* aSource
,
208 const TRect
& aUpdateRect
,
209 const TRect
& aSourceRect
)
211 const S
* source
= reinterpret_cast<const S
*>(aSource
);
212 const int lineWidth
= aSourceRect
.Width();
214 source
+= (aUpdateRect
.iTl
.iY
* lineWidth
);
215 const int sourceStartOffset
= aUpdateRect
.iTl
.iX
;
216 source
+= sourceStartOffset
;
218 T
* targetPtr
= reinterpret_cast<T
*>(aTarget
);
220 const int scanLineWidth
= aSourceRect
.iBr
.iX
;
222 targetPtr
+= aUpdateRect
.iTl
.iY
* scanLineWidth
;
223 const int targetStartOffset
= aUpdateRect
.iTl
.iX
;
225 targetPtr
+= targetStartOffset
;
227 const int height
= aUpdateRect
.Height();
229 const int lineMove
= lineWidth
;
230 const int copyLen
= aUpdateRect
.Width();
233 for(int i
= 0; i
< height
; i
++) //source is always smaller
235 iDsa
.Copy(reinterpret_cast<TUint32
*>(targetPtr
), reinterpret_cast<const TUint8
*>(source
), copyLen
);
237 targetPtr
+= scanLineWidth
;
241 /////////////////////////////////////////////////////////////////////////////////////////////////////
243 CDsa
* CDsa::CreateL(RWsSession
& aSession
)
245 return new CBitmapSurface
<CDsaBase
>(aSession
);
252 void CDsa::SetSuspend()
254 iStateFlags
|= ESdlThreadSuspend
;
258 void CDsa::SetUpdating(TBool aUpdate
)
261 iStateFlags
|= EUpdating
;
263 iStateFlags
&= ~EUpdating
;
266 TBool
CDsa::Stopped() const
268 return (iStateFlags
& ESdlThreadExplicitStop
);
276 void CDsa::ConstructL(RWindow
& aWindow
, CWsScreenDevice
& /*aDevice*/)
279 iLut256
= new TUint32
[256];
280 iTargetMode
= aWindow
.DisplayMode();
281 iTargetBpp
= BytesPerPixel(DisplayMode());
285 void CDsa::DoBlt(CFbsBitmap
& /*aBmp*/)
289 int CDsa::SetPalette(int aFirst
, int aCount
, TUint32
* aPalette
)
293 for(int i
= aFirst
; i
< aFirst
+ aCount
; i
++)
295 iLut256
[i
] = aPalette
[i
];
300 CDsa::CDsa(RWsSession
& aSession
) :
305 iCFTable
[0] = CopyMem
;
306 iCFTable
[1] = Copy256
;
307 iCFTable
[2] = CopySlow
;
310 RWsSession
& CDsa::Session()
315 TUint8
* CDsa::LockHwSurface()
317 if((iStateFlags
& EUpdating
) == 0) //else frame is skipped
319 return LockSurface();
324 int CDsa::AllocSurface(const TSize
& aSize
, TDisplayMode aMode
)
327 iSourceBpp
= BytesPerPixel(aMode
);
329 TRAPD(err
, CreateSurfaceL(aSize
));
338 void CDsa::ClipCopy(TUint8
* aTarget
,
339 const TUint8
* aSource
,
340 const TRect
& aUpdateRect
,
341 const TRect
& aSourceRect
) const
343 const TDsa
dsa(*this);
347 ::ClipCopy
<TUint32
, TUint8
>(dsa
, aTarget
, aSource
, aUpdateRect
, aSourceRect
);
350 ::ClipCopy
<TUint32
, TUint16
>(dsa
, aTarget
, aSource
, aUpdateRect
, aSourceRect
);
353 ::ClipCopy
<TUint32
, TUint32
>(dsa
, aTarget
, aSource
, aUpdateRect
, aSourceRect
);
358 void CDsa::SetCopyFunction()
360 //calculate offset to correct function in iCFTable according to given parameters
362 const int KCopyFunctions
= 1;
363 const int KOffsetToNative
= 0;
364 const int KOffsetTo256
= KOffsetToNative
+ KCopyFunctions
;
365 const int KOffsetToOtherModes
= KOffsetTo256
+ KCopyFunctions
;
367 if(iSourceMode
== DisplayMode())
368 function
= KOffsetToNative
;
369 else if(iSourceMode
== EColor256
)
370 function
= KOffsetTo256
;
372 function
= KOffsetToOtherModes
;
374 iCopyFunction
= iCFTable
[function
];
377 inline void Rotate(TRect
& aRect
)
379 const int dx
= aRect
.iBr
.iX
- aRect
.iTl
.iX
;
380 const int dy
= aRect
.iBr
.iY
- aRect
.iTl
.iY
;
382 aRect
.iBr
.iX
= aRect
.iTl
.iX
+ dy
;
383 aRect
.iBr
.iY
= aRect
.iTl
.iY
+ dx
;
385 const int tmp
= aRect
.iTl
.iX
;
386 aRect
.iTl
.iX
= aRect
.iTl
.iY
;
390 TBool
CDsa::AddUpdateRect(const TUint8
* aBits
, const TRect
& aUpdateRect
, const TRect
& aRect
)
392 if(iTargetAddr
== NULL
)
394 iTargetAddr
= LockHwSurface();
397 TUint8
* target
= iTargetAddr
;
401 TRect sourceRect
= aRect
;
402 TRect updateRect
= aUpdateRect
;
404 if(iSourceMode
!= DisplayMode() || aRect
!= aUpdateRect
)
406 ClipCopy(target
, aBits
, aUpdateRect
, aRect
);
410 const int byteCount
= aRect
.Width() * aRect
.Height() * iSourceBpp
; //this could be stored
411 memcpy(target
, aBits
, byteCount
);
417 void CDsa::UpdateSwSurface()
420 UnlockHwSurface(); //could be faster if does not use AO, but only check status before redraw, then no context switch needed
426 iStateFlags
|= ESdlThreadExplicitStop
;
432 iStateFlags
&= ~ERunning
;
437 iStateFlags
|= ERunning
;
439 iStateFlags
&= ~ESdlThreadExplicitStop
;
441 if(iStateFlags
& ESdlThreadSuspend
)
443 EpocSdlEnv::Resume();
444 iStateFlags
&= ~ ESdlThreadSuspend
;
448 RWindow
* CDsa::Window()
453 CDsa
* CDsa::CreateGlesDsaL()
455 CDsa
* dsa
= new CDsaGles(Session());
456 CWsScreenDevice
* dummy
= NULL
;
457 dsa
->ConstructL(*Window(), *dummy
);
463 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
465 void CDsa::Copy256(const CDsa
& aDsa
, TUint32
* aTarget
, const TUint8
* aSource
, int aBytes
)
467 TUint32
* target
= aTarget
;
468 const TUint32
* endt
= target
+ aBytes
;
469 const TUint8
* source
= aSource
;
472 *target
++ = aDsa
.iLut256
[*source
++];
476 void CDsa::CopyMem(const CDsa
& /*aDsa*/, TUint32
* aTarget
, const TUint8
* aSource
, int aBytes
)
478 memcpy(aTarget
, aSource
, aBytes
<< 2);
484 virtual void Copy(TUint32
* aTarget
, const TUint8
* aSource
, int aBytes
) = 0;
488 class TRgbCopy
: public MRgbCopy
491 TRgbCopy(TDisplayMode aMode
);
492 void* operator new(TUint aBytes
, TAny
* aMem
);
493 void Copy(TUint32
* aTarget
, const TUint8
* aSource
, int aBytes
);
494 static TUint32
Gray256(const TUint8
& aPixel
);
495 static TUint32
Color256(const TUint8
& aPixel
);
496 static TUint32
Color4K(const TUint16
& aPixel
);
497 static TUint32
Color64K(const TUint16
& aPixel
);
498 static TUint32
Color16M(const TUint32
& aPixel
);
499 static TUint32
Color16MU(const TUint32
& aPixel
);
500 static TUint32
Color16MA(const TUint32
& aPixel
);
502 typedef TUint32 (*TRgbFunc
) (const T
& aValue
);
507 void* TRgbCopy
<T
>::operator new(TUint
/*aBytes*/, TAny
* aMem
)
513 TRgbCopy
<T
>::TRgbCopy(TDisplayMode aMode
)
517 case EGray256
: iFunc
= (TRgbFunc
) Gray256
; break;
518 case EColor256
: iFunc
= (TRgbFunc
) Color256
; break;
519 case EColor4K
: iFunc
= (TRgbFunc
) Color4K
; break;
520 case EColor64K
: iFunc
= (TRgbFunc
) Color64K
; break;
521 case EColor16M
: iFunc
= (TRgbFunc
) Color16M
; break;
522 case EColor16MU
: iFunc
= (TRgbFunc
) Color16MU
; break;
523 case EColor16MA
: iFunc
= (TRgbFunc
) Color16MA
; break;
525 PANIC(KErrNotSupported
);
530 void TRgbCopy
<T
>::Copy(TUint32
* aTarget
, const TUint8
* aSource
, int aBytes
)
532 const T
* source
= reinterpret_cast<const T
*>(aSource
);
533 TUint32
* target
= aTarget
;
534 TUint32
* endt
= target
+ aBytes
;
538 const T value
= *source
++;
539 *target
++ = iFunc(value
);
544 TUint32 TRgbCopy
<T
>::Gray256(const TUint8
& aPixel
)
546 const TUint32 px
= aPixel
<< 16 | aPixel
<< 8 | aPixel
;
551 TUint32 TRgbCopy
<T
>::Color256(const TUint8
& aPixel
)
553 return TRgb::Color256(aPixel
).Value();
557 TUint32 TRgbCopy
<T
>::Color4K(const TUint16
& aPixel
)
559 TUint32 col
= (aPixel
& 0xF00) << 12;
560 col
|= (aPixel
& 0xF00) << 8;
562 col
|= (aPixel
& 0x0F0) << 8;
563 col
|= (aPixel
& 0x0F0);
565 col
|= (aPixel
& 0x00F) << 4;
566 col
|= (aPixel
& 0x00F);
572 TUint32 TRgbCopy
<T
>::Color64K(const TUint16
& aPixel
)
574 TUint32 col
= (aPixel
& 0xF800)<< 8;
575 col
|= (aPixel
& 0xE000) << 3;
577 col
|= (aPixel
& 0x07E0) << 5;
578 col
|= (aPixel
& 0xC0) >> 1;
580 col
|= (aPixel
& 0x07E0) << 3;
581 col
|= (aPixel
& 0x1C) >> 2;
587 TUint32 TRgbCopy
<T
>::Color16M(const TUint32
& aPixel
)
589 return TRgb::Color16M(aPixel
).Value();
593 TUint32 TRgbCopy
<T
>::Color16MU(const TUint32
& aPixel
)
595 return TRgb::Color16MU(aPixel
).Value();
599 TUint32 TRgbCopy
<T
>::Color16MA(const TUint32
& aPixel
)
601 return TRgb::Color16MA(aPixel
).Value();
604 typedef TUint64 TStackMem
;
606 static MRgbCopy
* GetCopy(TAny
* mem
, TDisplayMode aMode
)
608 if(aMode
== EColor256
|| aMode
== EGray256
)
610 return new (mem
) TRgbCopy
<TUint8
>(aMode
);
612 if(aMode
== EColor4K
|| aMode
== EColor64K
)
614 return new (mem
) TRgbCopy
<TUint16
>(aMode
);
616 if(aMode
== EColor16M
|| aMode
== EColor16MU
|| aMode
== EColor16MA
)
618 return new (mem
) TRgbCopy
<TUint32
>(aMode
);
620 PANIC(KErrNotSupported
);
624 void CDsa::CopySlow(const CDsa
& aDsa
, TUint32
* aTarget
, const TUint8
* aSource
, int aBytes
)
627 GetCopy(&mem
, aDsa
.iSourceMode
)->Copy(aTarget
, aSource
, aBytes
);