sl@0: // Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: // All rights reserved. sl@0: // This component and the accompanying materials are made available sl@0: // under the terms of "Eclipse Public License v1.0" sl@0: // which accompanies this distribution, and is available sl@0: // at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: // sl@0: // Initial Contributors: sl@0: // Nokia Corporation - initial contribution. sl@0: // sl@0: // Contributors: sl@0: // sl@0: // Description: sl@0: // sl@0: sl@0: #include "directgdiadapter.h" sl@0: #include "swdirectgdiengine.h" sl@0: #include "swdirectgdiimagesourceimpl.h" sl@0: #include "swdirectgdidriverimpl.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "pixelutil.h" sl@0: sl@0: /** sl@0: Helper class to deal with the case of blending 32-bit MAP source into 16-bit target which is not sl@0: supported by screen driver CDrawSixteenBppBitmap implementation. sl@0: sl@0: @publishedPartner sl@0: @prototype sl@0: @deprecated sl@0: */ sl@0: class TDrawDeviceWrapper sl@0: { sl@0: public: sl@0: TDrawDeviceWrapper(CFbsDrawDevice* aDrawDevice, CGraphicsContext::TDrawMode aDrawMode); sl@0: inline void WriteLine(TInt aX, TInt aY, TInt aLength, TUint32* aBuffer, CGraphicsContext::TDrawMode aDrawMode); sl@0: private: sl@0: inline TUint16 ConvertTo64K(TUint32 aColor); sl@0: inline TUint16 Blend16MapTo64K(TUint16 aDest, TUint32 aSrc); sl@0: void BlendLine16MapTo64K(TInt aX, TInt aY, TInt aLength, TUint32* aBuffer, CGraphicsContext::TDrawMode aDrawMode); sl@0: void OriginalWriteLine(TInt aX, TInt aY, TInt aLength, TUint32* aBuffer, CGraphicsContext::TDrawMode aDrawMode); sl@0: sl@0: private: sl@0: CFbsDrawDevice* iDrawDevice; sl@0: TUint32* iBits; sl@0: sl@0: typedef void (TDrawDeviceWrapper::*TWriteLineFunc)(TInt,TInt,TInt,TUint32*,CGraphicsContext::TDrawMode); sl@0: TWriteLineFunc iWriteLineFunc; sl@0: }; sl@0: sl@0: TDrawDeviceWrapper::TDrawDeviceWrapper(CFbsDrawDevice* aDrawDevice, CGraphicsContext::TDrawMode aDrawMode): sl@0: iDrawDevice(aDrawDevice) sl@0: { sl@0: TAny* interface = NULL; sl@0: TInt err = iDrawDevice->GetInterface(KFastBlit2InterfaceID, interface); sl@0: // interface is guaranted to exist for 16-bit and 32-bit draw device sl@0: GRAPHICS_ASSERT_DEBUG(err == KErrNone, EDirectGdiPanicUnexpectedError); sl@0: sl@0: iBits = (TUint32*) reinterpret_cast(interface)->Bits(); sl@0: sl@0: // setup which funtion to call here rather tha making decision inside WriteLine which is usually called within sl@0: // a tight scanline loop sl@0: iWriteLineFunc = iDrawDevice->DisplayMode() == EColor64K && aDrawMode == CGraphicsContext::EDrawModePEN ? sl@0: &TDrawDeviceWrapper::BlendLine16MapTo64K : &TDrawDeviceWrapper::OriginalWriteLine; sl@0: } sl@0: sl@0: inline void TDrawDeviceWrapper::WriteLine(TInt aX, TInt aY, TInt aLength, TUint32* aBuffer, CGraphicsContext::TDrawMode aDrawMode) sl@0: { sl@0: // calling member functions via pointer to member functions i.e. sl@0: // (object.*member_fn)(arg) sl@0: // sl@0: (this->*iWriteLineFunc)(aX, aY, aLength, aBuffer, aDrawMode); sl@0: } sl@0: sl@0: void TDrawDeviceWrapper::OriginalWriteLine(TInt aX, TInt aY, TInt aLength, TUint32* aBuffer, CGraphicsContext::TDrawMode aDrawMode) sl@0: { sl@0: iDrawDevice->WriteLine(aX, aY, aLength, aBuffer, aDrawMode); sl@0: } sl@0: sl@0: void TDrawDeviceWrapper::BlendLine16MapTo64K(TInt aX, TInt aY, TInt aLength, TUint32* aBuffer, CGraphicsContext::TDrawMode) sl@0: { sl@0: TUint16* pixelPtr = reinterpret_cast(iBits); sl@0: pixelPtr += (aY * iDrawDevice->LongWidth()) + aX; sl@0: const TUint32* bufferPtr = aBuffer; sl@0: const TUint32* bufferPtrLimit = bufferPtr + aLength; sl@0: sl@0: while (bufferPtr < bufferPtrLimit) sl@0: { sl@0: *pixelPtr = Blend16MapTo64K(*pixelPtr, *bufferPtr); sl@0: ++bufferPtr; sl@0: ++pixelPtr; sl@0: } sl@0: } sl@0: sl@0: inline TUint16 TDrawDeviceWrapper::ConvertTo64K(TUint32 aSrc) sl@0: { sl@0: TInt col = (aSrc & 0x0000f8) >> 3; sl@0: col |= (aSrc & 0x00fc00) >> 5; sl@0: col |= (aSrc & 0xf80000) >> 8; sl@0: sl@0: return col; sl@0: } sl@0: sl@0: inline TUint16 TDrawDeviceWrapper::Blend16MapTo64K(TUint16 aDst, TUint32 aSrc) sl@0: { sl@0: const TInt alpha = aSrc >> 24; sl@0: sl@0: if(alpha == 0x00) sl@0: { sl@0: return aDst; sl@0: } sl@0: sl@0: if (alpha == 0xff) sl@0: { sl@0: return ConvertTo64K(aSrc); sl@0: } sl@0: sl@0: // extract source components from 16MAP sl@0: const TInt src_rb = aSrc & 0x00ff00ff; sl@0: const TInt src_g = aSrc & 0x0000ff00; sl@0: const TInt oneMinusAlpha = 0x0100 - alpha; sl@0: sl@0: // extract destination components from 64K format sl@0: TInt dr = (aDst & 0xf800) >> 8; sl@0: dr += dr >> 5; sl@0: TInt dg = (aDst & 0x07e0) >> 3; sl@0: dg += dg >> 6; sl@0: TInt db = (aDst & 0x001f) << 3; sl@0: db += db >> 5; sl@0: sl@0: // combine red and blue components into a word to combine mult in one go sl@0: TInt dst_rb = (dr << 16) | db; sl@0: TInt dst_g = dg << 8; sl@0: sl@0: // dst and src are in pre-multiplied format (64K can be treated both as pre or non-pre) sl@0: // dst = src + (1-alpha) * dst sl@0: // sl@0: dst_rb = (src_rb + ((oneMinusAlpha * dst_rb) >> 8)) & 0x00ff00ff; sl@0: dst_g = (src_g + ((oneMinusAlpha * dst_g) >> 8)) & 0x0000ff00; sl@0: sl@0: const TUint32 argb = 0xff000000 | dst_rb | dst_g; sl@0: return ConvertTo64K(argb); sl@0: } sl@0: sl@0: // sl@0: // implements MDirectGdiEngine interfaces sl@0: // sl@0: sl@0: /** sl@0: @see MDirectGdiEngine::DrawResource(const TRect&,const RDirectGdiDrawableSource&,const TDesC8&) sl@0: */ sl@0: void CSwDirectGdiEngine::DrawResource( sl@0: const TRect& aDestRect, sl@0: const RDirectGdiDrawableSource& aSource, sl@0: const TDesC8& /*aParam*/) sl@0: { sl@0: // DirectGDI reference implementation only support pixel based resource sl@0: // see CSwDirectGdiDriverImpl::CreateDrawableSource() sl@0: // sl@0: CSwDirectGdiImageSourceImpl* imgSrc = NULL; sl@0: TSize imgSize; sl@0: if (CheckImageSource(aSource.Handle(), imgSrc, &imgSize) != KErrNone) sl@0: { sl@0: return; sl@0: } sl@0: sl@0: // drawing resource unscaled with no rotation sl@0: // sl@0: DrawResourceCommon(aDestRect, imgSrc, TRect(TPoint(0,0), imgSize), DirectGdi::EGraphicsRotationNone); sl@0: } sl@0: sl@0: /** sl@0: @see MDirectGdiEngine::DrawResource(const TPoint&,const RDirectGdiDrawableSource&,DirectGdi::TGraphicsRotation) sl@0: */ sl@0: void CSwDirectGdiEngine::DrawResource(const TPoint& aPos, sl@0: const RDirectGdiDrawableSource& aSource, sl@0: DirectGdi::TGraphicsRotation aRotation) sl@0: { sl@0: CSwDirectGdiImageSourceImpl* imgSrc = NULL; sl@0: TSize imgSize; sl@0: if (CheckImageSource(aSource.Handle(), imgSrc, &imgSize) != KErrNone) sl@0: { sl@0: return; sl@0: } sl@0: sl@0: // drawing resource unscaled sl@0: // sl@0: // rotation will be applied before scaling, we must create destination rectangle sl@0: // that match the size of rotated image sl@0: sl@0: TRect destRect(aPos, imgSize); sl@0: if (aRotation==DirectGdi::EGraphicsRotation90 || aRotation==DirectGdi::EGraphicsRotation270) sl@0: { sl@0: // keep the top left corner in the same position but swap width and height sl@0: destRect.SetWidth(imgSize.iHeight); sl@0: destRect.SetHeight(imgSize.iWidth); sl@0: } sl@0: sl@0: DrawResourceCommon(destRect, imgSrc, TRect(TPoint(0,0), imgSize), aRotation); sl@0: } sl@0: sl@0: /** sl@0: @see MDirectGdiEngine::DrawResource(const TRect&,const RDirectGdiDrawableSource&,DirectGdi::TGraphicsRotation) sl@0: */ sl@0: void CSwDirectGdiEngine::DrawResource(const TRect& aDestRect, sl@0: const RDirectGdiDrawableSource& aSource, sl@0: DirectGdi::TGraphicsRotation aRotation) sl@0: { sl@0: // aDestRect is not empty when we reach here sl@0: // sl@0: CSwDirectGdiImageSourceImpl* imgSrc = NULL; sl@0: TSize imgSize; sl@0: if (CheckImageSource(aSource.Handle(), imgSrc, &imgSize) != KErrNone) sl@0: { sl@0: return; sl@0: } sl@0: sl@0: DrawResourceCommon(aDestRect, imgSrc, TRect(TPoint(0,0), imgSize), aRotation); sl@0: } sl@0: sl@0: /** sl@0: @see MDirectGdiEngine::DrawResource(const TRect&,const RDirectGdiDrawableSource&,const TRect&,DirectGdi::TGraphicsRotation) sl@0: */ sl@0: void CSwDirectGdiEngine::DrawResource( sl@0: const TRect& aDestRect, sl@0: const RDirectGdiDrawableSource& aSource, sl@0: const TRect& aSrcRect, sl@0: DirectGdi::TGraphicsRotation aRotation) sl@0: { sl@0: // pre: sl@0: // aDestRect and aSrcRect are not empty sl@0: // sl@0: CSwDirectGdiImageSourceImpl* imgSrc = NULL; sl@0: TSize imgSize; sl@0: if (CheckImageSource(aSource.Handle(), imgSrc, &imgSize) != KErrNone) sl@0: { sl@0: return; sl@0: } sl@0: sl@0: // check source rectangle is fully contained within the image resource extent sl@0: if (aSrcRect.iTl.iX < 0 || sl@0: aSrcRect.iTl.iY < 0 || sl@0: aSrcRect.iBr.iX > imgSize.iWidth || sl@0: aSrcRect.iBr.iY > imgSize.iHeight) sl@0: { sl@0: iDriver->SetError(KErrArgument); sl@0: return; sl@0: } sl@0: sl@0: DrawResourceCommon(aDestRect, imgSrc, aSrcRect, aRotation); sl@0: } sl@0: sl@0: // sl@0: // internal functions sl@0: // sl@0: /** sl@0: Checks image resource is fully constructed and registered with the driver. sl@0: sl@0: @param aHandle A valid handle to an image source. sl@0: @param aImg On return, contains the image source. sl@0: @param aSize If not NULL, will contain the dimensions of the image source. sl@0: @return KErrNone if successful, KErrBadHandle if aHandle is not a valid handle to an image source. sl@0: */ sl@0: TInt CSwDirectGdiEngine::CheckImageSource(TInt aHandle, CSwDirectGdiImageSourceImpl*& aImg, TSize* aSize) sl@0: { sl@0: // check image exist sl@0: if (!iDriver->ValidImageSource(aHandle)) sl@0: { sl@0: // replace KErrNotFound sl@0: const TInt err = KErrBadHandle; sl@0: iDriver->SetError(err); sl@0: return err; sl@0: } sl@0: sl@0: aImg = reinterpret_cast(aHandle); sl@0: sl@0: // RSgImage cannot be created with zero size, so there is no point in validating its size and sl@0: // simply return image size if requested sl@0: if (aSize) sl@0: { sl@0: *aSize = aImg->Size(); sl@0: } sl@0: sl@0: return KErrNone; sl@0: } sl@0: sl@0: /** sl@0: Implements common DrawResource() functionality used by all DrawResource() variants. sl@0: */ sl@0: void CSwDirectGdiEngine::DrawResourceCommon( sl@0: const TRect& aDestRect, sl@0: const CSwDirectGdiImageSourceImpl* aImg, sl@0: const TRect& aSrcRect, sl@0: DirectGdi::TGraphicsRotation aRotation) sl@0: { sl@0: // pre: sl@0: // aDestRect and aSrcRect are not empty sl@0: // aSrcRect is within the image extent sl@0: // sl@0: // translate relative coord to target absolute coord system sl@0: TRect destRectAbs(aDestRect); sl@0: destRectAbs.Move(iOrigin); sl@0: sl@0: // check whether we need to blend or write sl@0: const TBool opaqueSource = (!PixelFormatUtil::HasAlpha(aImg->PixelFormat())) && (iDrawMode == DirectGdi::EDrawModePEN); sl@0: if (opaqueSource) sl@0: { sl@0: iDrawMode = DirectGdi::EDrawModeWriteAlpha; sl@0: } sl@0: sl@0: // repeat drawing op for each rectangle in the clipping region sl@0: const TInt numOfRects = iDefaultRegionPtr->Count(); sl@0: for (TInt idx = 0; idx < numOfRects; ++idx) sl@0: { sl@0: TRect clipRectAbs = (*iDefaultRegionPtr)[idx]; sl@0: if (!clipRectAbs.Intersects(destRectAbs)) sl@0: { sl@0: continue; sl@0: } sl@0: sl@0: // intersect current clip rect with dest rect to get actual draw area sl@0: clipRectAbs.Intersection(destRectAbs); sl@0: DoDrawResource(destRectAbs, aImg, aSrcRect, aRotation, clipRectAbs); sl@0: sl@0: iDrawDevice->UpdateRegion(clipRectAbs); sl@0: } sl@0: sl@0: if (opaqueSource) sl@0: { sl@0: iDrawMode = DirectGdi::EDrawModePEN; sl@0: } sl@0: } sl@0: sl@0: /** sl@0: Rotate a given rectangle 90 degree clockwise around the specified origin. sl@0: */ sl@0: void CSwDirectGdiEngine::Rotate90(TRect& aRect, const TPoint& aOrigin) sl@0: { sl@0: // rotated bottom-left become top-left of rotated rect sl@0: // sl@0: TPoint bl(aRect.iTl.iX, aRect.iBr.iY); sl@0: const TInt w = aRect.Width(); sl@0: const TInt h = aRect.Height(); sl@0: sl@0: const TPoint dbl = bl - aOrigin; sl@0: bl.iX = aOrigin.iX - dbl.iY; sl@0: bl.iY = aOrigin.iY + dbl.iX; sl@0: sl@0: aRect = TRect(bl, TSize(h,w)); sl@0: } sl@0: sl@0: /** sl@0: Rotate a given rectangle 180 degree clockwise around the specified origin. sl@0: */ sl@0: void CSwDirectGdiEngine::Rotate180(TRect& aRect, const TPoint& aOrigin) sl@0: { sl@0: // rotated bottom-right become top-left of rotated rect sl@0: // sl@0: TPoint br(aRect.iBr); sl@0: const TSize sz = aRect.Size(); sl@0: sl@0: const TPoint dbr = br - aOrigin; sl@0: br.iX = aOrigin.iX - dbr.iX; sl@0: br.iY = aOrigin.iY - dbr.iY; sl@0: sl@0: aRect = TRect(br, sz); sl@0: } sl@0: sl@0: /** sl@0: Rotate a given rectangle 270 degree clockwise around the specified origin. sl@0: */ sl@0: void CSwDirectGdiEngine::Rotate270(TRect& aRect, const TPoint& aOrigin) sl@0: { sl@0: // rotated top-right become top-left of rotated rect sl@0: // sl@0: TPoint tr(aRect.iBr.iX, aRect.iTl.iY); sl@0: const TInt w = aRect.Width(); sl@0: const TInt h = aRect.Height(); sl@0: sl@0: const TPoint dtr = tr - aOrigin; sl@0: tr.iX = aOrigin.iX + dtr.iY; sl@0: tr.iY = aOrigin.iY - dtr.iX; sl@0: sl@0: aRect = TRect(tr, TSize(h,w)); sl@0: } sl@0: sl@0: /** sl@0: Tests that the size of a given rotated rectangle match the image source extent. sl@0: */ sl@0: TBool CSwDirectGdiEngine::RotatedSizeMatch(const TRect& aDst, const TRect& aSrc, DirectGdi::TGraphicsRotation aRot) sl@0: { sl@0: if (aRot==DirectGdi::EGraphicsRotationNone || aRot==DirectGdi::EGraphicsRotation180) sl@0: { sl@0: return aDst.Size()==aSrc.Size(); sl@0: } sl@0: else sl@0: { sl@0: return aDst.Width()==aSrc.Height() && aDst.Height()==aSrc.Width(); sl@0: } sl@0: } sl@0: sl@0: /** sl@0: Draws a rectangular area of resource and apply rotation and/or scaling if requested. sl@0: */ sl@0: void CSwDirectGdiEngine::DoDrawResource( sl@0: const TRect& aDestRectAbs, sl@0: const CSwDirectGdiImageSourceImpl* aImg, sl@0: const TRect& aSrcRect, sl@0: DirectGdi::TGraphicsRotation aRotation, sl@0: const TRect& aClipRectAbs) sl@0: { sl@0: // pre: sl@0: // SetClippingRegion() already check that clipping region is always contained sl@0: // within device full extent. sl@0: sl@0: // check src rect match the size of rotated dest rect sl@0: if (RotatedSizeMatch(aDestRectAbs, aSrcRect, aRotation)) sl@0: { sl@0: // aClipRect is the effective drawing clipped area, it has been intersected with dest rect by the sl@0: // calling function/DoDrawResourceCommon() and it is not empty when we reach here sl@0: sl@0: // image size has been checked at the top level DrawResource() no need to check it again sl@0: sl@0: // use aClipRect to determine how much area should be read from the image and transform sl@0: // the effective read area into source rect depending on rotation parameter sl@0: sl@0: // start with aClipRect sl@0: TRect xformSrcRect(aClipRectAbs); sl@0: switch (aRotation) sl@0: { sl@0: case DirectGdi::EGraphicsRotationNone: sl@0: // align top-left corner of dest rect with top-left corner of src rect sl@0: xformSrcRect.Move(aSrcRect.iTl - aDestRectAbs.iTl); sl@0: break; sl@0: case DirectGdi::EGraphicsRotation90: sl@0: { sl@0: // align top-right corner of dest rect with top-left corner of src rect sl@0: xformSrcRect.Move(aSrcRect.iTl - TPoint(aDestRectAbs.iBr.iX, aDestRectAbs.iTl.iY)); sl@0: // rotate 270 (-90) degree using top-left corner of src rect as pivot point sl@0: Rotate270(xformSrcRect, aSrcRect.iTl); sl@0: } sl@0: break; sl@0: case DirectGdi::EGraphicsRotation180: sl@0: { sl@0: // align bottom-right corner of dest rect with top-left corner of src rect sl@0: xformSrcRect.Move(aSrcRect.iTl - aDestRectAbs.iBr); sl@0: // rotate 180 (-180) degree using top-left corner of src rect as pivot point sl@0: Rotate180(xformSrcRect, aSrcRect.iTl); sl@0: } sl@0: break; sl@0: case DirectGdi::EGraphicsRotation270: sl@0: { sl@0: // align bottom-left corner of dest rect with top-left corner of src rect sl@0: xformSrcRect.Move(aSrcRect.iTl - TPoint(aDestRectAbs.iTl.iX, aDestRectAbs.iBr.iY)); sl@0: // rotate 90 (-270) degree using top-left corner of src rect as pivot point sl@0: Rotate90(xformSrcRect, aSrcRect.iTl); sl@0: } sl@0: break; sl@0: sl@0: // no need for extra check, aRotation has been checked at generic layer sl@0: } sl@0: sl@0: DoBlitResource(aClipRectAbs.iTl, aImg, xformSrcRect, aRotation); sl@0: return; sl@0: } sl@0: sl@0: DoScaledBlitResource(aDestRectAbs, aImg, aSrcRect, aRotation, aClipRectAbs); sl@0: } sl@0: sl@0: /** sl@0: Draws a rectangular area of resource rotated with no scaling. sl@0: @panic DGDIAdapter 1009, if the pixel format of the draw device is unknown (debug only). sl@0: */ sl@0: void CSwDirectGdiEngine::DoBlitResource( sl@0: const TPoint& aDest, sl@0: const CSwDirectGdiImageSourceImpl* aImg, sl@0: const TRect& aSrcRect, sl@0: DirectGdi::TGraphicsRotation aRotation) sl@0: { sl@0: // pre: sl@0: // aDest is the top-left of clipped destination rectangle i.e. intersection of user destination sl@0: // rectangle with current clipping rect sl@0: // aSrcRect is rotated clipped read area i.e. effective read area with respect to original sl@0: // image orientation i.e.it no longer represents user specified source rect. sl@0: sl@0: // no need to do extra check on parameters here because: sl@0: // aDest is guaranteed to be within device drawing area sl@0: // aSrcRect is guaranteed to be within image source area sl@0: sl@0: const TUidPixelFormat devFormat = PixelFormatUtil::ConvertToPixelFormat(iDrawDevice->DisplayMode()); sl@0: GRAPHICS_ASSERT_DEBUG(devFormat != EUidPixelFormatUnknown, EDirectGdiPanicInvalidDisplayMode); sl@0: sl@0: const TUidPixelFormat imgFormat = aImg->PixelFormat(); sl@0: const TUint32* imgAddr = reinterpret_cast(aImg->DataBuffer()); sl@0: const TInt imgStride = aImg->Stride(); sl@0: const TSize imgSize = aImg->Size(); sl@0: const CGraphicsContext::TDrawMode drawMode = GcDrawMode(iDrawMode); sl@0: sl@0: const TInt width = aSrcRect.Width(); sl@0: const TInt height = aSrcRect.Height(); sl@0: // write scanline starting from top dest row sl@0: TInt destY = aDest.iY; sl@0: // setup pixel reader toolkit sl@0: TPixelBufferReader reader(imgAddr, imgSize, imgStride, imgFormat); sl@0: TDrawDeviceWrapper writer(iDrawDevice, GcDrawMode(iDrawMode)); sl@0: sl@0: // sl@0: // special case when rotation is none sl@0: // sl@0: if (aRotation == DirectGdi::EGraphicsRotationNone) sl@0: { sl@0: TAny* interface = NULL; sl@0: sl@0: // source and destination format match and using write alpha mode i.e. reduce blit to memcpy sl@0: if (iDrawMode == DirectGdi::EDrawModeWriteAlpha && devFormat == imgFormat) sl@0: { sl@0: TInt err = iDrawDevice->GetInterface(KFastBlit2InterfaceID, interface); sl@0: if (err == KErrNone) sl@0: { sl@0: GRAPHICS_ASSERT_DEBUG(interface, EDirectGdiPanicInvalidPointer); sl@0: MFastBlit2* fblit = static_cast(interface); sl@0: err = fblit->WriteBitmapBlock(aDest, imgAddr, imgStride, imgSize, aSrcRect); sl@0: if (err == KErrNone) sl@0: { sl@0: return; sl@0: } sl@0: } sl@0: } sl@0: sl@0: // fallback from MFastBlit2 when source and destination format match. sl@0: // Note that there was previously an optimization added here that used MFlastBlend if sl@0: // available, however it did not work correctly for some cases using DrawResource so has been sl@0: // removed from this code. (A source drawn with OpenGLES on to a target in XRGB_8888 format sl@0: // actually had alpha in the unused channel which was being drawn by MFastBlend) sl@0: if (devFormat == imgFormat) sl@0: { sl@0: for (TInt row=aSrcRect.iTl.iY; row(slptr), drawMode); sl@0: } sl@0: sl@0: return; sl@0: } sl@0: sl@0: // there is one additional case that can be optimised by eleminating copy when: sl@0: // rotation : none sl@0: // scaling : none sl@0: // dst : opaque sl@0: // src : has alpha and premultiplied sl@0: // mode : PEN sl@0: } sl@0: sl@0: // sl@0: // generic cases sl@0: // sl@0: // copying is necessary either to convert pixel format or to read back buffer in reverse order sl@0: // to achieve rotation sl@0: // sl@0: const TInt scanLineBytes = iDrawDevice->ScanLineBytes(); sl@0: TUint32* scanLineBuffer = iDrawDevice->ScanLineBuffer(); sl@0: TPtr8 scanLineDes(reinterpret_cast(scanLineBuffer),scanLineBytes,scanLineBytes); sl@0: sl@0: // if destination is opaque e.g. RGB_565, XRGB_8888 and the source has alpha, it shall blend sl@0: // (because iDrawMode is set to WritePEN) sl@0: // we treat opaque destination as in pre-multiplied format (with alpha value 1), and read the sl@0: // source in pre-multiplied format too, to enable optimum blending computation i.e. sl@0: // "src + (1-alpha) * dst" sl@0: const TUidPixelFormat readFormat = !PixelFormatUtil::HasAlpha(devFormat) && sl@0: iDrawMode == DirectGdi::EDrawModePEN ? sl@0: EUidPixelFormatARGB_8888_PRE : devFormat; sl@0: sl@0: if (aRotation == DirectGdi::EGraphicsRotationNone) sl@0: { sl@0: // general fallback from FastBlendBitmap sl@0: // sl@0: const TInt readLen = width; sl@0: // normal read scanline, left to right sl@0: // sl@0: for (TInt row=aSrcRect.iTl.iY; row=aSrcRect.iTl.iY; --row,++destY) sl@0: { sl@0: const TPoint pos(aSrcRect.iBr.iX-1, row); sl@0: reader.GetScanLine(scanLineDes, pos, readLen, readFormat, TPixelBufferReader::EReadHorizontalReverse); sl@0: writer.WriteLine(aDest.iX, destY, readLen, scanLineBuffer, drawMode); sl@0: } sl@0: } sl@0: else if (aRotation == DirectGdi::EGraphicsRotation270) sl@0: { sl@0: const TInt readLen = height; sl@0: // read scanline vertically top to bottom, and from the right-most column sl@0: // sl@0: for (TInt col=aSrcRect.iBr.iX-1; col>=aSrcRect.iTl.iX; --col,++destY) sl@0: { sl@0: const TPoint pos(col, aSrcRect.iTl.iY); sl@0: reader.GetScanLine(scanLineDes, pos, readLen, readFormat, TPixelBufferReader::EReadVertical); sl@0: writer.WriteLine(aDest.iX, destY, readLen, scanLineBuffer, drawMode); sl@0: } sl@0: } sl@0: } sl@0: sl@0: /** sl@0: Draws a rectangular area of resource rotated and/or scaled up/down. sl@0: @panic DGDIAdapter 1009, if the pixel format of the draw device is unknown (debug only). sl@0: */ sl@0: void CSwDirectGdiEngine::DoScaledBlitResource( sl@0: const TRect& aDestRectAbs, sl@0: const CSwDirectGdiImageSourceImpl* aImg, sl@0: const TRect& aSrcRect, sl@0: DirectGdi::TGraphicsRotation aRotation, sl@0: const TRect& aClipRectAbs) sl@0: { sl@0: // pre: sl@0: // aDestRectAbs is the user specified dest rect that has been translated to target coord system. It may sl@0: // be larger/outside the target drawing area. We must not modify this rect as it will be used to sl@0: // determine the scaling factor. sl@0: // aSrcRect is the user specified src rect, it is within the image extent. sl@0: // aClipRectAbs is the intersection of current clipping rect and aDestRectAbs and is within the sl@0: // target drawing area. sl@0: sl@0: const TUidPixelFormat devFormat = PixelFormatUtil::ConvertToPixelFormat(iDrawDevice->DisplayMode()); sl@0: GRAPHICS_ASSERT_DEBUG(devFormat != EUidPixelFormatUnknown, EDirectGdiPanicInvalidDisplayMode); sl@0: sl@0: const TUint32* imgAddr = reinterpret_cast(aImg->DataBuffer()); sl@0: const TInt imgStride = aImg->Stride(); sl@0: const TSize imgSize = aImg->Size(); sl@0: const TUidPixelFormat imgFormat = aImg->PixelFormat(); sl@0: const CGraphicsContext::TDrawMode drawMode = GcDrawMode(iDrawMode); sl@0: sl@0: // if destination is opaque e.g. RGB_565, XRGB_8888 and the source has alpha, it shall blend sl@0: // (because iDrawMode is set to WritePEN) sl@0: // we treat opaque destination as in pre-multiplied format (with alpha value 1), and read the sl@0: // source in pre-multiplied format too, to enable optimum blending computation i.e. sl@0: // "src + (1-alpha) * dst" sl@0: const TUidPixelFormat readFormat = !PixelFormatUtil::HasAlpha(devFormat) && sl@0: iDrawMode == DirectGdi::EDrawModePEN ? sl@0: EUidPixelFormatARGB_8888_PRE : devFormat; sl@0: sl@0: TUint32* scanLineBuffer = iDrawDevice->ScanLineBuffer(); sl@0: const TInt scanLineBytes = iDrawDevice->ScanLineBytes(); sl@0: TPtr8 scanLineDes(reinterpret_cast(scanLineBuffer), scanLineBytes, scanLineBytes); sl@0: sl@0: // setup pixel reader toolkit sl@0: // sl@0: TPixelBufferReader reader(imgAddr, imgSize, imgStride, imgFormat); sl@0: TDrawDeviceWrapper writer(iDrawDevice, GcDrawMode(iDrawMode)); sl@0: sl@0: if (aRotation == DirectGdi::EGraphicsRotationNone) sl@0: { sl@0: // Note that there was previously an optimization added here that used MFlastBlend if sl@0: // available, however it did not work correctly for some cases using DrawResource so has been sl@0: // removed from this code. (A source drawn with OpenGLES on to a target in XRGB_8888 format sl@0: // actually had alpha in the unused channel which was being drawn by MFastBlend) sl@0: // sl@0: const TInt srcLen = aSrcRect.Width(); sl@0: const TInt destLen = aDestRectAbs.Width(); sl@0: const TInt clipLen = aClipRectAbs.Width(); sl@0: const TInt clipPos = aClipRectAbs.iTl.iX - aDestRectAbs.iTl.iX; sl@0: sl@0: // setup DDA for scaling in Y direction based on src rect and dst rect size sl@0: // scaling in X direction will be done by GetScaledScanLine() sl@0: // sl@0: // setup Y scaler and start from top-left of src/dest to bottom-right of src/dest sl@0: sl@0: TLinearDDA yScaler; sl@0: yScaler.Construct( sl@0: TPoint(aSrcRect.iTl.iY, aDestRectAbs.iTl.iY), sl@0: TPoint(aSrcRect.iBr.iY, aDestRectAbs.iBr.iY), sl@0: TLinearDDA::ELeft); sl@0: sl@0: // move to position of current clip rect top row as the start of dest Y sl@0: TInt srcY; sl@0: yScaler.JumpToYCoord2(srcY, aClipRectAbs.iTl.iY); sl@0: sl@0: // yPos contains mapping between src Y (TPoint.iX) and dest Y (TPoint.iY) sl@0: TPoint yPos(srcY, aClipRectAbs.iTl.iY); sl@0: sl@0: // write to target from top to bottom sl@0: // sl@0: while (yPos.iY < aClipRectAbs.iBr.iY) sl@0: { sl@0: reader.GetScaledScanLine(scanLineDes, sl@0: TPoint(aSrcRect.iTl.iX, yPos.iX), // src X and Y sl@0: clipPos, // clipped dest X sl@0: clipLen, sl@0: destLen, sl@0: srcLen, sl@0: readFormat, sl@0: TPixelBufferReader::EReadHorizontal); sl@0: sl@0: // use dest Y here sl@0: writer.WriteLine(aClipRectAbs.iTl.iX, yPos.iY, clipLen, scanLineBuffer, drawMode); sl@0: sl@0: // move scaler one position sl@0: yScaler.NextStep(yPos); sl@0: }; sl@0: } sl@0: else if (aRotation == DirectGdi::EGraphicsRotation90) sl@0: { sl@0: // we're going to read source vertically from bottom to up, swap relevant bits and pieces sl@0: // dst-width corresponds to src-height sl@0: // dst-height corresponds to src-width sl@0: // sl@0: const TInt srcLen = aSrcRect.Height(); sl@0: const TInt destLen = aDestRectAbs.Width(); sl@0: sl@0: // the following doesn't change, the amount of pixel read (vertically) will be based on sl@0: // the drawing area witdh i.e. clip width sl@0: // sl@0: const TInt clipLen = aClipRectAbs.Width(); sl@0: sl@0: // offset into read area doesn't change either, it will be translated into some Y position sl@0: // from bottom row of source image sl@0: const TInt clipPos = aClipRectAbs.iTl.iX - aDestRectAbs.iTl.iX; sl@0: sl@0: // setup DDA for scaling in src X direction based on src rect and dst rect size sl@0: // scaling in src Y direction will be done by GetScaledScanLine(EReadVerticalReverse) sl@0: // sl@0: // scaler map dest Y to src X because of 90 degre rotation sl@0: // start from top row dest, left-most column src to bottom row dest, right-most column src sl@0: // sl@0: TLinearDDA xScaler; sl@0: xScaler.Construct( sl@0: TPoint(aSrcRect.iTl.iX, aDestRectAbs.iTl.iY), sl@0: TPoint(aSrcRect.iBr.iX, aDestRectAbs.iBr.iY), sl@0: TLinearDDA::ELeft); sl@0: sl@0: // move to position of current clip rect top row as the start of src X sl@0: TInt srcX; sl@0: xScaler.JumpToYCoord2(srcX, aClipRectAbs.iTl.iY); sl@0: sl@0: // xPos contains mapping between src X (TPoint.iX) and dest Y (TPoint.iY) sl@0: TPoint xPos(srcX, aClipRectAbs.iTl.iY); sl@0: sl@0: // write to target from top to bottom sl@0: // sl@0: while (xPos.iY < aClipRectAbs.iBr.iY) sl@0: { sl@0: // read pixel vertically from left column to right sl@0: reader.GetScaledScanLine(scanLineDes, sl@0: TPoint(xPos.iX, aSrcRect.iBr.iY - 1), // src X, src Y sl@0: clipPos, // distance from bottom source sl@0: clipLen, sl@0: destLen, sl@0: srcLen, sl@0: readFormat, sl@0: TPixelBufferReader::EReadVerticalReverse); sl@0: sl@0: // use dest Y here sl@0: writer.WriteLine(aClipRectAbs.iTl.iX, xPos.iY, clipLen, scanLineBuffer, drawMode); sl@0: sl@0: // move scaler one position sl@0: xScaler.NextStep(xPos); sl@0: } sl@0: } sl@0: else if (aRotation == DirectGdi::EGraphicsRotation180) sl@0: { sl@0: const TInt srcLen = aSrcRect.Width(); sl@0: const TInt destLen = aDestRectAbs.Width(); sl@0: const TInt clipLen = aClipRectAbs.Width(); sl@0: sl@0: // clipPos doesn't need to be inverted (using iBr.iX) because the yScaler below sl@0: // will do that for us (by stepping backward) sl@0: // sl@0: const TInt clipPos = aClipRectAbs.iTl.iX - aDestRectAbs.iTl.iX; sl@0: sl@0: // setup DDA for scaling in Y direction based on src rect and dst rect size sl@0: // scaling in X direction will be done by GetScaledScanLine() sl@0: // sl@0: // setup Y scaler and start from bottom-right of src/dest to top-left of src/dest sl@0: // to do backward stepping (src Y inversion) sl@0: sl@0: TLinearDDA yScaler; sl@0: yScaler.Construct( sl@0: // we starting from 1st pixel from bottom of source image, we need to sl@0: // step back 1 position as bottom-row coord is beyond the last pixel in the source rect sl@0: TPoint(aSrcRect.iBr.iY - 1, aDestRectAbs.iTl.iY), sl@0: TPoint(aSrcRect.iTl.iY - 1, aDestRectAbs.iBr.iY), sl@0: TLinearDDA::ELeft); sl@0: sl@0: // move to position of current clip rect top row as the start of dest Y sl@0: // which will calculate inverted src Y distance from bottom of source rectangle sl@0: TInt invSrcY; sl@0: yScaler.JumpToYCoord2(invSrcY, aClipRectAbs.iTl.iY); sl@0: sl@0: // yPos contains mapping between inverted src Y (TPoint.iX) and dest Y (TPoint.iY) sl@0: TPoint yPos(invSrcY, aClipRectAbs.iTl.iY); sl@0: sl@0: // write to target from top to bottom sl@0: // sl@0: while (yPos.iY < aClipRectAbs.iBr.iY) sl@0: { sl@0: // read scanline from righ to left i.e. use aSrcRect.iBr.iX-1 (last column) sl@0: // as starting src X value sl@0: reader.GetScaledScanLine(scanLineDes, sl@0: TPoint(aSrcRect.iBr.iX - 1, yPos.iX), // src X and inverted src Y sl@0: clipPos, // clipped dest X sl@0: clipLen, sl@0: destLen, sl@0: srcLen, sl@0: readFormat, sl@0: TPixelBufferReader::EReadHorizontalReverse); sl@0: sl@0: // use dest Y here sl@0: writer.WriteLine(aClipRectAbs.iTl.iX, yPos.iY, clipLen, scanLineBuffer, drawMode); sl@0: sl@0: // move scaler one position sl@0: yScaler.NextStep(yPos); sl@0: } sl@0: } sl@0: else if(aRotation == DirectGdi::EGraphicsRotation270) sl@0: { sl@0: // we're going to read source vertically from top to bottom, swap relevant bits and pieces sl@0: // dst-width corresponds to src-height sl@0: // dst-height corresponds to src-width sl@0: const TInt srcLen = aSrcRect.Height(); sl@0: const TInt destLen = aDestRectAbs.Width(); sl@0: sl@0: // the following doesn't change, the amount of pixel read (vertically) will be based on sl@0: // the drawing area witdh i.e. clip width sl@0: const TInt clipLen = aClipRectAbs.Width(); sl@0: sl@0: // offset into read area doesn't change either, it will be translated into some Y position sl@0: // from top row of source image sl@0: const TInt clipPos = aClipRectAbs.iTl.iX - aDestRectAbs.iTl.iX; sl@0: sl@0: // setup DDA for scaling in src X direction based on src rect and dst rect size sl@0: // scaling in src Y direction will be done by GetScaledScanLine(EReadVertical) sl@0: // sl@0: // scaler map dest Y to src X because of 270 degre rotation sl@0: // start from top row dest, right-most column src to bottom row dest, left-most column src sl@0: sl@0: TLinearDDA xScaler; sl@0: xScaler.Construct( sl@0: // decrement 1 pixel to get into last pixel within source sl@0: TPoint(aSrcRect.iBr.iX - 1, aDestRectAbs.iTl.iY), sl@0: TPoint(aSrcRect.iTl.iX - 1, aDestRectAbs.iBr.iY), sl@0: TLinearDDA::ELeft); sl@0: sl@0: // move to position of current clip rect top row as the start of src X sl@0: TInt srcX; sl@0: xScaler.JumpToYCoord2(srcX, aClipRectAbs.iTl.iY); sl@0: // xPos contains mapping between src X (TPoint.iX) and dest Y (TPoint.iY) sl@0: TPoint xPos(srcX, aClipRectAbs.iTl.iY); sl@0: sl@0: // write to target from top to bottom sl@0: // sl@0: while (xPos.iY < aClipRectAbs.iBr.iY) sl@0: { sl@0: // read pixel vertically from left column to right sl@0: reader.GetScaledScanLine(scanLineDes, sl@0: TPoint(xPos.iX, aSrcRect.iTl.iY), // src X, src Y sl@0: clipPos, // distance from bottom source sl@0: clipLen, sl@0: destLen, sl@0: srcLen, sl@0: readFormat, sl@0: TPixelBufferReader::EReadVertical); sl@0: sl@0: // use dest Y here sl@0: writer.WriteLine(aClipRectAbs.iTl.iX, xPos.iY, clipLen, scanLineBuffer, drawMode); sl@0: sl@0: // move scaler one position sl@0: xScaler.NextStep(xPos); sl@0: } sl@0: } sl@0: }