os/graphics/graphicsdeviceinterface/directgdiadaptation/swsrc/swdirectgdidrawresource.cpp
Update contrib.
1 // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
2 // All rights reserved.
3 // This component and the accompanying materials are made available
4 // under the terms of "Eclipse Public License v1.0"
5 // which accompanies this distribution, and is available
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
16 #include "directgdiadapter.h"
17 #include "swdirectgdiengine.h"
18 #include "swdirectgdiimagesourceimpl.h"
19 #include "swdirectgdidriverimpl.h"
20 #include <bitdrawinterfaceid.h>
23 #include <bmalphablend.h>
24 #include <graphics/directgdidrawablesource.h>
25 #include <pixelformats.h>
26 #include "pixelutil.h"
29 Helper class to deal with the case of blending 32-bit MAP source into 16-bit target which is not
30 supported by screen driver CDrawSixteenBppBitmap implementation.
36 class TDrawDeviceWrapper
39 TDrawDeviceWrapper(CFbsDrawDevice* aDrawDevice, CGraphicsContext::TDrawMode aDrawMode);
40 inline void WriteLine(TInt aX, TInt aY, TInt aLength, TUint32* aBuffer, CGraphicsContext::TDrawMode aDrawMode);
42 inline TUint16 ConvertTo64K(TUint32 aColor);
43 inline TUint16 Blend16MapTo64K(TUint16 aDest, TUint32 aSrc);
44 void BlendLine16MapTo64K(TInt aX, TInt aY, TInt aLength, TUint32* aBuffer, CGraphicsContext::TDrawMode aDrawMode);
45 void OriginalWriteLine(TInt aX, TInt aY, TInt aLength, TUint32* aBuffer, CGraphicsContext::TDrawMode aDrawMode);
48 CFbsDrawDevice* iDrawDevice;
51 typedef void (TDrawDeviceWrapper::*TWriteLineFunc)(TInt,TInt,TInt,TUint32*,CGraphicsContext::TDrawMode);
52 TWriteLineFunc iWriteLineFunc;
55 TDrawDeviceWrapper::TDrawDeviceWrapper(CFbsDrawDevice* aDrawDevice, CGraphicsContext::TDrawMode aDrawMode):
56 iDrawDevice(aDrawDevice)
58 TAny* interface = NULL;
59 TInt err = iDrawDevice->GetInterface(KFastBlit2InterfaceID, interface);
60 // interface is guaranted to exist for 16-bit and 32-bit draw device
61 GRAPHICS_ASSERT_DEBUG(err == KErrNone, EDirectGdiPanicUnexpectedError);
63 iBits = (TUint32*) reinterpret_cast<MFastBlit2*>(interface)->Bits();
65 // setup which funtion to call here rather tha making decision inside WriteLine which is usually called within
66 // a tight scanline loop
67 iWriteLineFunc = iDrawDevice->DisplayMode() == EColor64K && aDrawMode == CGraphicsContext::EDrawModePEN ?
68 &TDrawDeviceWrapper::BlendLine16MapTo64K : &TDrawDeviceWrapper::OriginalWriteLine;
71 inline void TDrawDeviceWrapper::WriteLine(TInt aX, TInt aY, TInt aLength, TUint32* aBuffer, CGraphicsContext::TDrawMode aDrawMode)
73 // calling member functions via pointer to member functions i.e.
74 // (object.*member_fn)(arg)
76 (this->*iWriteLineFunc)(aX, aY, aLength, aBuffer, aDrawMode);
79 void TDrawDeviceWrapper::OriginalWriteLine(TInt aX, TInt aY, TInt aLength, TUint32* aBuffer, CGraphicsContext::TDrawMode aDrawMode)
81 iDrawDevice->WriteLine(aX, aY, aLength, aBuffer, aDrawMode);
84 void TDrawDeviceWrapper::BlendLine16MapTo64K(TInt aX, TInt aY, TInt aLength, TUint32* aBuffer, CGraphicsContext::TDrawMode)
86 TUint16* pixelPtr = reinterpret_cast<TUint16*>(iBits);
87 pixelPtr += (aY * iDrawDevice->LongWidth()) + aX;
88 const TUint32* bufferPtr = aBuffer;
89 const TUint32* bufferPtrLimit = bufferPtr + aLength;
91 while (bufferPtr < bufferPtrLimit)
93 *pixelPtr = Blend16MapTo64K(*pixelPtr, *bufferPtr);
99 inline TUint16 TDrawDeviceWrapper::ConvertTo64K(TUint32 aSrc)
101 TInt col = (aSrc & 0x0000f8) >> 3;
102 col |= (aSrc & 0x00fc00) >> 5;
103 col |= (aSrc & 0xf80000) >> 8;
108 inline TUint16 TDrawDeviceWrapper::Blend16MapTo64K(TUint16 aDst, TUint32 aSrc)
110 const TInt alpha = aSrc >> 24;
119 return ConvertTo64K(aSrc);
122 // extract source components from 16MAP
123 const TInt src_rb = aSrc & 0x00ff00ff;
124 const TInt src_g = aSrc & 0x0000ff00;
125 const TInt oneMinusAlpha = 0x0100 - alpha;
127 // extract destination components from 64K format
128 TInt dr = (aDst & 0xf800) >> 8;
130 TInt dg = (aDst & 0x07e0) >> 3;
132 TInt db = (aDst & 0x001f) << 3;
135 // combine red and blue components into a word to combine mult in one go
136 TInt dst_rb = (dr << 16) | db;
137 TInt dst_g = dg << 8;
139 // dst and src are in pre-multiplied format (64K can be treated both as pre or non-pre)
140 // dst = src + (1-alpha) * dst
142 dst_rb = (src_rb + ((oneMinusAlpha * dst_rb) >> 8)) & 0x00ff00ff;
143 dst_g = (src_g + ((oneMinusAlpha * dst_g) >> 8)) & 0x0000ff00;
145 const TUint32 argb = 0xff000000 | dst_rb | dst_g;
146 return ConvertTo64K(argb);
150 // implements MDirectGdiEngine interfaces
154 @see MDirectGdiEngine::DrawResource(const TRect&,const RDirectGdiDrawableSource&,const TDesC8&)
156 void CSwDirectGdiEngine::DrawResource(
157 const TRect& aDestRect,
158 const RDirectGdiDrawableSource& aSource,
159 const TDesC8& /*aParam*/)
161 // DirectGDI reference implementation only support pixel based resource
162 // see CSwDirectGdiDriverImpl::CreateDrawableSource()
164 CSwDirectGdiImageSourceImpl* imgSrc = NULL;
166 if (CheckImageSource(aSource.Handle(), imgSrc, &imgSize) != KErrNone)
171 // drawing resource unscaled with no rotation
173 DrawResourceCommon(aDestRect, imgSrc, TRect(TPoint(0,0), imgSize), DirectGdi::EGraphicsRotationNone);
177 @see MDirectGdiEngine::DrawResource(const TPoint&,const RDirectGdiDrawableSource&,DirectGdi::TGraphicsRotation)
179 void CSwDirectGdiEngine::DrawResource(const TPoint& aPos,
180 const RDirectGdiDrawableSource& aSource,
181 DirectGdi::TGraphicsRotation aRotation)
183 CSwDirectGdiImageSourceImpl* imgSrc = NULL;
185 if (CheckImageSource(aSource.Handle(), imgSrc, &imgSize) != KErrNone)
190 // drawing resource unscaled
192 // rotation will be applied before scaling, we must create destination rectangle
193 // that match the size of rotated image
195 TRect destRect(aPos, imgSize);
196 if (aRotation==DirectGdi::EGraphicsRotation90 || aRotation==DirectGdi::EGraphicsRotation270)
198 // keep the top left corner in the same position but swap width and height
199 destRect.SetWidth(imgSize.iHeight);
200 destRect.SetHeight(imgSize.iWidth);
203 DrawResourceCommon(destRect, imgSrc, TRect(TPoint(0,0), imgSize), aRotation);
207 @see MDirectGdiEngine::DrawResource(const TRect&,const RDirectGdiDrawableSource&,DirectGdi::TGraphicsRotation)
209 void CSwDirectGdiEngine::DrawResource(const TRect& aDestRect,
210 const RDirectGdiDrawableSource& aSource,
211 DirectGdi::TGraphicsRotation aRotation)
213 // aDestRect is not empty when we reach here
215 CSwDirectGdiImageSourceImpl* imgSrc = NULL;
217 if (CheckImageSource(aSource.Handle(), imgSrc, &imgSize) != KErrNone)
222 DrawResourceCommon(aDestRect, imgSrc, TRect(TPoint(0,0), imgSize), aRotation);
226 @see MDirectGdiEngine::DrawResource(const TRect&,const RDirectGdiDrawableSource&,const TRect&,DirectGdi::TGraphicsRotation)
228 void CSwDirectGdiEngine::DrawResource(
229 const TRect& aDestRect,
230 const RDirectGdiDrawableSource& aSource,
231 const TRect& aSrcRect,
232 DirectGdi::TGraphicsRotation aRotation)
235 // aDestRect and aSrcRect are not empty
237 CSwDirectGdiImageSourceImpl* imgSrc = NULL;
239 if (CheckImageSource(aSource.Handle(), imgSrc, &imgSize) != KErrNone)
244 // check source rectangle is fully contained within the image resource extent
245 if (aSrcRect.iTl.iX < 0 ||
246 aSrcRect.iTl.iY < 0 ||
247 aSrcRect.iBr.iX > imgSize.iWidth ||
248 aSrcRect.iBr.iY > imgSize.iHeight)
250 iDriver->SetError(KErrArgument);
254 DrawResourceCommon(aDestRect, imgSrc, aSrcRect, aRotation);
258 // internal functions
261 Checks image resource is fully constructed and registered with the driver.
263 @param aHandle A valid handle to an image source.
264 @param aImg On return, contains the image source.
265 @param aSize If not NULL, will contain the dimensions of the image source.
266 @return KErrNone if successful, KErrBadHandle if aHandle is not a valid handle to an image source.
268 TInt CSwDirectGdiEngine::CheckImageSource(TInt aHandle, CSwDirectGdiImageSourceImpl*& aImg, TSize* aSize)
271 if (!iDriver->ValidImageSource(aHandle))
273 // replace KErrNotFound
274 const TInt err = KErrBadHandle;
275 iDriver->SetError(err);
279 aImg = reinterpret_cast<CSwDirectGdiImageSourceImpl*>(aHandle);
281 // RSgImage cannot be created with zero size, so there is no point in validating its size and
282 // simply return image size if requested
285 *aSize = aImg->Size();
292 Implements common DrawResource() functionality used by all DrawResource() variants.
294 void CSwDirectGdiEngine::DrawResourceCommon(
295 const TRect& aDestRect,
296 const CSwDirectGdiImageSourceImpl* aImg,
297 const TRect& aSrcRect,
298 DirectGdi::TGraphicsRotation aRotation)
301 // aDestRect and aSrcRect are not empty
302 // aSrcRect is within the image extent
304 // translate relative coord to target absolute coord system
305 TRect destRectAbs(aDestRect);
306 destRectAbs.Move(iOrigin);
308 // check whether we need to blend or write
309 const TBool opaqueSource = (!PixelFormatUtil::HasAlpha(aImg->PixelFormat())) && (iDrawMode == DirectGdi::EDrawModePEN);
312 iDrawMode = DirectGdi::EDrawModeWriteAlpha;
315 // repeat drawing op for each rectangle in the clipping region
316 const TInt numOfRects = iDefaultRegionPtr->Count();
317 for (TInt idx = 0; idx < numOfRects; ++idx)
319 TRect clipRectAbs = (*iDefaultRegionPtr)[idx];
320 if (!clipRectAbs.Intersects(destRectAbs))
325 // intersect current clip rect with dest rect to get actual draw area
326 clipRectAbs.Intersection(destRectAbs);
327 DoDrawResource(destRectAbs, aImg, aSrcRect, aRotation, clipRectAbs);
329 iDrawDevice->UpdateRegion(clipRectAbs);
334 iDrawMode = DirectGdi::EDrawModePEN;
339 Rotate a given rectangle 90 degree clockwise around the specified origin.
341 void CSwDirectGdiEngine::Rotate90(TRect& aRect, const TPoint& aOrigin)
343 // rotated bottom-left become top-left of rotated rect
345 TPoint bl(aRect.iTl.iX, aRect.iBr.iY);
346 const TInt w = aRect.Width();
347 const TInt h = aRect.Height();
349 const TPoint dbl = bl - aOrigin;
350 bl.iX = aOrigin.iX - dbl.iY;
351 bl.iY = aOrigin.iY + dbl.iX;
353 aRect = TRect(bl, TSize(h,w));
357 Rotate a given rectangle 180 degree clockwise around the specified origin.
359 void CSwDirectGdiEngine::Rotate180(TRect& aRect, const TPoint& aOrigin)
361 // rotated bottom-right become top-left of rotated rect
363 TPoint br(aRect.iBr);
364 const TSize sz = aRect.Size();
366 const TPoint dbr = br - aOrigin;
367 br.iX = aOrigin.iX - dbr.iX;
368 br.iY = aOrigin.iY - dbr.iY;
370 aRect = TRect(br, sz);
374 Rotate a given rectangle 270 degree clockwise around the specified origin.
376 void CSwDirectGdiEngine::Rotate270(TRect& aRect, const TPoint& aOrigin)
378 // rotated top-right become top-left of rotated rect
380 TPoint tr(aRect.iBr.iX, aRect.iTl.iY);
381 const TInt w = aRect.Width();
382 const TInt h = aRect.Height();
384 const TPoint dtr = tr - aOrigin;
385 tr.iX = aOrigin.iX + dtr.iY;
386 tr.iY = aOrigin.iY - dtr.iX;
388 aRect = TRect(tr, TSize(h,w));
392 Tests that the size of a given rotated rectangle match the image source extent.
394 TBool CSwDirectGdiEngine::RotatedSizeMatch(const TRect& aDst, const TRect& aSrc, DirectGdi::TGraphicsRotation aRot)
396 if (aRot==DirectGdi::EGraphicsRotationNone || aRot==DirectGdi::EGraphicsRotation180)
398 return aDst.Size()==aSrc.Size();
402 return aDst.Width()==aSrc.Height() && aDst.Height()==aSrc.Width();
407 Draws a rectangular area of resource and apply rotation and/or scaling if requested.
409 void CSwDirectGdiEngine::DoDrawResource(
410 const TRect& aDestRectAbs,
411 const CSwDirectGdiImageSourceImpl* aImg,
412 const TRect& aSrcRect,
413 DirectGdi::TGraphicsRotation aRotation,
414 const TRect& aClipRectAbs)
417 // SetClippingRegion() already check that clipping region is always contained
418 // within device full extent.
420 // check src rect match the size of rotated dest rect
421 if (RotatedSizeMatch(aDestRectAbs, aSrcRect, aRotation))
423 // aClipRect is the effective drawing clipped area, it has been intersected with dest rect by the
424 // calling function/DoDrawResourceCommon() and it is not empty when we reach here
426 // image size has been checked at the top level DrawResource() no need to check it again
428 // use aClipRect to determine how much area should be read from the image and transform
429 // the effective read area into source rect depending on rotation parameter
431 // start with aClipRect
432 TRect xformSrcRect(aClipRectAbs);
435 case DirectGdi::EGraphicsRotationNone:
436 // align top-left corner of dest rect with top-left corner of src rect
437 xformSrcRect.Move(aSrcRect.iTl - aDestRectAbs.iTl);
439 case DirectGdi::EGraphicsRotation90:
441 // align top-right corner of dest rect with top-left corner of src rect
442 xformSrcRect.Move(aSrcRect.iTl - TPoint(aDestRectAbs.iBr.iX, aDestRectAbs.iTl.iY));
443 // rotate 270 (-90) degree using top-left corner of src rect as pivot point
444 Rotate270(xformSrcRect, aSrcRect.iTl);
447 case DirectGdi::EGraphicsRotation180:
449 // align bottom-right corner of dest rect with top-left corner of src rect
450 xformSrcRect.Move(aSrcRect.iTl - aDestRectAbs.iBr);
451 // rotate 180 (-180) degree using top-left corner of src rect as pivot point
452 Rotate180(xformSrcRect, aSrcRect.iTl);
455 case DirectGdi::EGraphicsRotation270:
457 // align bottom-left corner of dest rect with top-left corner of src rect
458 xformSrcRect.Move(aSrcRect.iTl - TPoint(aDestRectAbs.iTl.iX, aDestRectAbs.iBr.iY));
459 // rotate 90 (-270) degree using top-left corner of src rect as pivot point
460 Rotate90(xformSrcRect, aSrcRect.iTl);
464 // no need for extra check, aRotation has been checked at generic layer
467 DoBlitResource(aClipRectAbs.iTl, aImg, xformSrcRect, aRotation);
471 DoScaledBlitResource(aDestRectAbs, aImg, aSrcRect, aRotation, aClipRectAbs);
475 Draws a rectangular area of resource rotated with no scaling.
476 @panic DGDIAdapter 1009, if the pixel format of the draw device is unknown (debug only).
478 void CSwDirectGdiEngine::DoBlitResource(
480 const CSwDirectGdiImageSourceImpl* aImg,
481 const TRect& aSrcRect,
482 DirectGdi::TGraphicsRotation aRotation)
485 // aDest is the top-left of clipped destination rectangle i.e. intersection of user destination
486 // rectangle with current clipping rect
487 // aSrcRect is rotated clipped read area i.e. effective read area with respect to original
488 // image orientation i.e.it no longer represents user specified source rect.
490 // no need to do extra check on parameters here because:
491 // aDest is guaranteed to be within device drawing area
492 // aSrcRect is guaranteed to be within image source area
494 const TUidPixelFormat devFormat = PixelFormatUtil::ConvertToPixelFormat(iDrawDevice->DisplayMode());
495 GRAPHICS_ASSERT_DEBUG(devFormat != EUidPixelFormatUnknown, EDirectGdiPanicInvalidDisplayMode);
497 const TUidPixelFormat imgFormat = aImg->PixelFormat();
498 const TUint32* imgAddr = reinterpret_cast<TUint32*>(aImg->DataBuffer());
499 const TInt imgStride = aImg->Stride();
500 const TSize imgSize = aImg->Size();
501 const CGraphicsContext::TDrawMode drawMode = GcDrawMode(iDrawMode);
503 const TInt width = aSrcRect.Width();
504 const TInt height = aSrcRect.Height();
505 // write scanline starting from top dest row
506 TInt destY = aDest.iY;
507 // setup pixel reader toolkit
508 TPixelBufferReader reader(imgAddr, imgSize, imgStride, imgFormat);
509 TDrawDeviceWrapper writer(iDrawDevice, GcDrawMode(iDrawMode));
512 // special case when rotation is none
514 if (aRotation == DirectGdi::EGraphicsRotationNone)
516 TAny* interface = NULL;
518 // source and destination format match and using write alpha mode i.e. reduce blit to memcpy
519 if (iDrawMode == DirectGdi::EDrawModeWriteAlpha && devFormat == imgFormat)
521 TInt err = iDrawDevice->GetInterface(KFastBlit2InterfaceID, interface);
524 GRAPHICS_ASSERT_DEBUG(interface, EDirectGdiPanicInvalidPointer);
525 MFastBlit2* fblit = static_cast<MFastBlit2*>(interface);
526 err = fblit->WriteBitmapBlock(aDest, imgAddr, imgStride, imgSize, aSrcRect);
534 // fallback from MFastBlit2 when source and destination format match.
535 // Note that there was previously an optimization added here that used MFlastBlend if
536 // available, however it did not work correctly for some cases using DrawResource so has been
537 // removed from this code. (A source drawn with OpenGLES on to a target in XRGB_8888 format
538 // actually had alpha in the unused channel which was being drawn by MFastBlend)
539 if (devFormat == imgFormat)
541 for (TInt row=aSrcRect.iTl.iY; row<aSrcRect.iBr.iY; ++row,++destY)
543 const TPoint pos(aSrcRect.iTl.iX, row);
544 const TUint32* slptr = reader.GetPixelAddr(pos);
545 writer.WriteLine(aDest.iX, destY, width, const_cast<TUint32*>(slptr), drawMode);
551 // there is one additional case that can be optimised by eleminating copy when:
555 // src : has alpha and premultiplied
562 // copying is necessary either to convert pixel format or to read back buffer in reverse order
563 // to achieve rotation
565 const TInt scanLineBytes = iDrawDevice->ScanLineBytes();
566 TUint32* scanLineBuffer = iDrawDevice->ScanLineBuffer();
567 TPtr8 scanLineDes(reinterpret_cast<TUint8*>(scanLineBuffer),scanLineBytes,scanLineBytes);
569 // if destination is opaque e.g. RGB_565, XRGB_8888 and the source has alpha, it shall blend
570 // (because iDrawMode is set to WritePEN)
571 // we treat opaque destination as in pre-multiplied format (with alpha value 1), and read the
572 // source in pre-multiplied format too, to enable optimum blending computation i.e.
573 // "src + (1-alpha) * dst"
574 const TUidPixelFormat readFormat = !PixelFormatUtil::HasAlpha(devFormat) &&
575 iDrawMode == DirectGdi::EDrawModePEN ?
576 EUidPixelFormatARGB_8888_PRE : devFormat;
578 if (aRotation == DirectGdi::EGraphicsRotationNone)
580 // general fallback from FastBlendBitmap
582 const TInt readLen = width;
583 // normal read scanline, left to right
585 for (TInt row=aSrcRect.iTl.iY; row<aSrcRect.iBr.iY; ++row,++destY)
587 const TPoint pos(aSrcRect.iTl.iX, row);
588 reader.GetScanLine(scanLineDes,pos, readLen, readFormat, TPixelBufferReader::EReadHorizontal);
589 writer.WriteLine(aDest.iX, destY, readLen, scanLineBuffer, drawMode);
592 else if (aRotation == DirectGdi::EGraphicsRotation90)
594 const TInt readLen = height;
595 // read scanline vertically from bottom to up, and from the lef-most column
597 for (TInt col=aSrcRect.iTl.iX; col<aSrcRect.iBr.iX; ++col,++destY)
599 const TPoint pos(col, aSrcRect.iBr.iY-1);
600 reader.GetScanLine(scanLineDes, pos, readLen, readFormat, TPixelBufferReader::EReadVerticalReverse);
601 writer.WriteLine(aDest.iX, destY, readLen, scanLineBuffer, drawMode);
604 else if (aRotation == DirectGdi::EGraphicsRotation180)
606 const TInt readLen = width;
607 // read scanline from right to left, starting from the most bottom scanline
609 for (TInt row=aSrcRect.iBr.iY-1; row>=aSrcRect.iTl.iY; --row,++destY)
611 const TPoint pos(aSrcRect.iBr.iX-1, row);
612 reader.GetScanLine(scanLineDes, pos, readLen, readFormat, TPixelBufferReader::EReadHorizontalReverse);
613 writer.WriteLine(aDest.iX, destY, readLen, scanLineBuffer, drawMode);
616 else if (aRotation == DirectGdi::EGraphicsRotation270)
618 const TInt readLen = height;
619 // read scanline vertically top to bottom, and from the right-most column
621 for (TInt col=aSrcRect.iBr.iX-1; col>=aSrcRect.iTl.iX; --col,++destY)
623 const TPoint pos(col, aSrcRect.iTl.iY);
624 reader.GetScanLine(scanLineDes, pos, readLen, readFormat, TPixelBufferReader::EReadVertical);
625 writer.WriteLine(aDest.iX, destY, readLen, scanLineBuffer, drawMode);
631 Draws a rectangular area of resource rotated and/or scaled up/down.
632 @panic DGDIAdapter 1009, if the pixel format of the draw device is unknown (debug only).
634 void CSwDirectGdiEngine::DoScaledBlitResource(
635 const TRect& aDestRectAbs,
636 const CSwDirectGdiImageSourceImpl* aImg,
637 const TRect& aSrcRect,
638 DirectGdi::TGraphicsRotation aRotation,
639 const TRect& aClipRectAbs)
642 // aDestRectAbs is the user specified dest rect that has been translated to target coord system. It may
643 // be larger/outside the target drawing area. We must not modify this rect as it will be used to
644 // determine the scaling factor.
645 // aSrcRect is the user specified src rect, it is within the image extent.
646 // aClipRectAbs is the intersection of current clipping rect and aDestRectAbs and is within the
647 // target drawing area.
649 const TUidPixelFormat devFormat = PixelFormatUtil::ConvertToPixelFormat(iDrawDevice->DisplayMode());
650 GRAPHICS_ASSERT_DEBUG(devFormat != EUidPixelFormatUnknown, EDirectGdiPanicInvalidDisplayMode);
652 const TUint32* imgAddr = reinterpret_cast<TUint32*>(aImg->DataBuffer());
653 const TInt imgStride = aImg->Stride();
654 const TSize imgSize = aImg->Size();
655 const TUidPixelFormat imgFormat = aImg->PixelFormat();
656 const CGraphicsContext::TDrawMode drawMode = GcDrawMode(iDrawMode);
658 // if destination is opaque e.g. RGB_565, XRGB_8888 and the source has alpha, it shall blend
659 // (because iDrawMode is set to WritePEN)
660 // we treat opaque destination as in pre-multiplied format (with alpha value 1), and read the
661 // source in pre-multiplied format too, to enable optimum blending computation i.e.
662 // "src + (1-alpha) * dst"
663 const TUidPixelFormat readFormat = !PixelFormatUtil::HasAlpha(devFormat) &&
664 iDrawMode == DirectGdi::EDrawModePEN ?
665 EUidPixelFormatARGB_8888_PRE : devFormat;
667 TUint32* scanLineBuffer = iDrawDevice->ScanLineBuffer();
668 const TInt scanLineBytes = iDrawDevice->ScanLineBytes();
669 TPtr8 scanLineDes(reinterpret_cast<TUint8*>(scanLineBuffer), scanLineBytes, scanLineBytes);
671 // setup pixel reader toolkit
673 TPixelBufferReader reader(imgAddr, imgSize, imgStride, imgFormat);
674 TDrawDeviceWrapper writer(iDrawDevice, GcDrawMode(iDrawMode));
676 if (aRotation == DirectGdi::EGraphicsRotationNone)
678 // Note that there was previously an optimization added here that used MFlastBlend if
679 // available, however it did not work correctly for some cases using DrawResource so has been
680 // removed from this code. (A source drawn with OpenGLES on to a target in XRGB_8888 format
681 // actually had alpha in the unused channel which was being drawn by MFastBlend)
683 const TInt srcLen = aSrcRect.Width();
684 const TInt destLen = aDestRectAbs.Width();
685 const TInt clipLen = aClipRectAbs.Width();
686 const TInt clipPos = aClipRectAbs.iTl.iX - aDestRectAbs.iTl.iX;
688 // setup DDA for scaling in Y direction based on src rect and dst rect size
689 // scaling in X direction will be done by GetScaledScanLine()
691 // setup Y scaler and start from top-left of src/dest to bottom-right of src/dest
695 TPoint(aSrcRect.iTl.iY, aDestRectAbs.iTl.iY),
696 TPoint(aSrcRect.iBr.iY, aDestRectAbs.iBr.iY),
699 // move to position of current clip rect top row as the start of dest Y
701 yScaler.JumpToYCoord2(srcY, aClipRectAbs.iTl.iY);
703 // yPos contains mapping between src Y (TPoint.iX) and dest Y (TPoint.iY)
704 TPoint yPos(srcY, aClipRectAbs.iTl.iY);
706 // write to target from top to bottom
708 while (yPos.iY < aClipRectAbs.iBr.iY)
710 reader.GetScaledScanLine(scanLineDes,
711 TPoint(aSrcRect.iTl.iX, yPos.iX), // src X and Y
712 clipPos, // clipped dest X
717 TPixelBufferReader::EReadHorizontal);
720 writer.WriteLine(aClipRectAbs.iTl.iX, yPos.iY, clipLen, scanLineBuffer, drawMode);
722 // move scaler one position
723 yScaler.NextStep(yPos);
726 else if (aRotation == DirectGdi::EGraphicsRotation90)
728 // we're going to read source vertically from bottom to up, swap relevant bits and pieces
729 // dst-width corresponds to src-height
730 // dst-height corresponds to src-width
732 const TInt srcLen = aSrcRect.Height();
733 const TInt destLen = aDestRectAbs.Width();
735 // the following doesn't change, the amount of pixel read (vertically) will be based on
736 // the drawing area witdh i.e. clip width
738 const TInt clipLen = aClipRectAbs.Width();
740 // offset into read area doesn't change either, it will be translated into some Y position
741 // from bottom row of source image
742 const TInt clipPos = aClipRectAbs.iTl.iX - aDestRectAbs.iTl.iX;
744 // setup DDA for scaling in src X direction based on src rect and dst rect size
745 // scaling in src Y direction will be done by GetScaledScanLine(EReadVerticalReverse)
747 // scaler map dest Y to src X because of 90 degre rotation
748 // start from top row dest, left-most column src to bottom row dest, right-most column src
752 TPoint(aSrcRect.iTl.iX, aDestRectAbs.iTl.iY),
753 TPoint(aSrcRect.iBr.iX, aDestRectAbs.iBr.iY),
756 // move to position of current clip rect top row as the start of src X
758 xScaler.JumpToYCoord2(srcX, aClipRectAbs.iTl.iY);
760 // xPos contains mapping between src X (TPoint.iX) and dest Y (TPoint.iY)
761 TPoint xPos(srcX, aClipRectAbs.iTl.iY);
763 // write to target from top to bottom
765 while (xPos.iY < aClipRectAbs.iBr.iY)
767 // read pixel vertically from left column to right
768 reader.GetScaledScanLine(scanLineDes,
769 TPoint(xPos.iX, aSrcRect.iBr.iY - 1), // src X, src Y
770 clipPos, // distance from bottom source
775 TPixelBufferReader::EReadVerticalReverse);
778 writer.WriteLine(aClipRectAbs.iTl.iX, xPos.iY, clipLen, scanLineBuffer, drawMode);
780 // move scaler one position
781 xScaler.NextStep(xPos);
784 else if (aRotation == DirectGdi::EGraphicsRotation180)
786 const TInt srcLen = aSrcRect.Width();
787 const TInt destLen = aDestRectAbs.Width();
788 const TInt clipLen = aClipRectAbs.Width();
790 // clipPos doesn't need to be inverted (using iBr.iX) because the yScaler below
791 // will do that for us (by stepping backward)
793 const TInt clipPos = aClipRectAbs.iTl.iX - aDestRectAbs.iTl.iX;
795 // setup DDA for scaling in Y direction based on src rect and dst rect size
796 // scaling in X direction will be done by GetScaledScanLine()
798 // setup Y scaler and start from bottom-right of src/dest to top-left of src/dest
799 // to do backward stepping (src Y inversion)
803 // we starting from 1st pixel from bottom of source image, we need to
804 // step back 1 position as bottom-row coord is beyond the last pixel in the source rect
805 TPoint(aSrcRect.iBr.iY - 1, aDestRectAbs.iTl.iY),
806 TPoint(aSrcRect.iTl.iY - 1, aDestRectAbs.iBr.iY),
809 // move to position of current clip rect top row as the start of dest Y
810 // which will calculate inverted src Y distance from bottom of source rectangle
812 yScaler.JumpToYCoord2(invSrcY, aClipRectAbs.iTl.iY);
814 // yPos contains mapping between inverted src Y (TPoint.iX) and dest Y (TPoint.iY)
815 TPoint yPos(invSrcY, aClipRectAbs.iTl.iY);
817 // write to target from top to bottom
819 while (yPos.iY < aClipRectAbs.iBr.iY)
821 // read scanline from righ to left i.e. use aSrcRect.iBr.iX-1 (last column)
822 // as starting src X value
823 reader.GetScaledScanLine(scanLineDes,
824 TPoint(aSrcRect.iBr.iX - 1, yPos.iX), // src X and inverted src Y
825 clipPos, // clipped dest X
830 TPixelBufferReader::EReadHorizontalReverse);
833 writer.WriteLine(aClipRectAbs.iTl.iX, yPos.iY, clipLen, scanLineBuffer, drawMode);
835 // move scaler one position
836 yScaler.NextStep(yPos);
839 else if(aRotation == DirectGdi::EGraphicsRotation270)
841 // we're going to read source vertically from top to bottom, swap relevant bits and pieces
842 // dst-width corresponds to src-height
843 // dst-height corresponds to src-width
844 const TInt srcLen = aSrcRect.Height();
845 const TInt destLen = aDestRectAbs.Width();
847 // the following doesn't change, the amount of pixel read (vertically) will be based on
848 // the drawing area witdh i.e. clip width
849 const TInt clipLen = aClipRectAbs.Width();
851 // offset into read area doesn't change either, it will be translated into some Y position
852 // from top row of source image
853 const TInt clipPos = aClipRectAbs.iTl.iX - aDestRectAbs.iTl.iX;
855 // setup DDA for scaling in src X direction based on src rect and dst rect size
856 // scaling in src Y direction will be done by GetScaledScanLine(EReadVertical)
858 // scaler map dest Y to src X because of 270 degre rotation
859 // start from top row dest, right-most column src to bottom row dest, left-most column src
863 // decrement 1 pixel to get into last pixel within source
864 TPoint(aSrcRect.iBr.iX - 1, aDestRectAbs.iTl.iY),
865 TPoint(aSrcRect.iTl.iX - 1, aDestRectAbs.iBr.iY),
868 // move to position of current clip rect top row as the start of src X
870 xScaler.JumpToYCoord2(srcX, aClipRectAbs.iTl.iY);
871 // xPos contains mapping between src X (TPoint.iX) and dest Y (TPoint.iY)
872 TPoint xPos(srcX, aClipRectAbs.iTl.iY);
874 // write to target from top to bottom
876 while (xPos.iY < aClipRectAbs.iBr.iY)
878 // read pixel vertically from left column to right
879 reader.GetScaledScanLine(scanLineDes,
880 TPoint(xPos.iX, aSrcRect.iTl.iY), // src X, src Y
881 clipPos, // distance from bottom source
886 TPixelBufferReader::EReadVertical);
889 writer.WriteLine(aClipRectAbs.iTl.iX, xPos.iY, clipLen, scanLineBuffer, drawMode);
891 // move scaler one position
892 xScaler.NextStep(xPos);