sl@0: // Copyright (c) 2007-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 "sgimagecollectionimpl.h" sl@0: #include "sgimageimpl.h" sl@0: sl@0: sl@0: TInt XSgImageCollectionImpl::New(XSgImageCollectionImpl*& aPtr, XSgDriverImpl& aDriverImpl, sl@0: const TSgImageInfo& aInfo, TInt aImageCount, TBool aIsCached, sl@0: TInt aStride, TInt aOffsetToFirstBuffer, TInt aOffsetBetweenBuffers, sl@0: TInt aMetaDataIndex, const XSgImageCollectionImpl* aCollection) sl@0: { sl@0: aPtr = static_cast(aDriverImpl.Alloc(sizeof(XSgImageCollectionImpl))); sl@0: if (!aPtr) sl@0: { sl@0: return KErrNoMemory; sl@0: } sl@0: new(aPtr) XSgImageCollectionImpl(aDriverImpl); sl@0: TInt err = aPtr->Construct(aInfo, aImageCount, aIsCached, aStride, aOffsetToFirstBuffer, aOffsetBetweenBuffers, aMetaDataIndex, aCollection); sl@0: if (err != KErrNone) sl@0: { sl@0: aPtr->Delete(); sl@0: aPtr = NULL; sl@0: } sl@0: return err; sl@0: } sl@0: sl@0: sl@0: TInt XSgImageCollectionImpl::Construct(const TSgImageInfo& aInfo, TInt aImageCount, TBool aIsCached, sl@0: TInt aStride, TInt aOffsetToFirstBuffer, TInt aOffsetBetweenBuffers, sl@0: TInt aMetaDataIndex, const XSgImageCollectionImpl* aCollection) sl@0: { sl@0: TInt maxNumberOfHints; sl@0: TInt err; sl@0: err=iDriverImpl.GetSurfaceManagerAttrib(RSurfaceManager::EMaxNumberOfHints,maxNumberOfHints); sl@0: if (err != KErrNone) sl@0: { sl@0: return err; sl@0: } sl@0: sl@0: if (aInfo.iUserAttributeCount > maxNumberOfHints) sl@0: { sl@0: return KErrOverflow; sl@0: } sl@0: RSurfaceManager::THintPair* hints = new RSurfaceManager::THintPair[aInfo.iUserAttributeCount]; sl@0: if(!hints) sl@0: { sl@0: return KErrNoMemory; sl@0: } sl@0: sl@0: RSurfaceManager::TSurfaceCreationAttributesBuf reqs; sl@0: reqs().iSize = aInfo.iSizeInPixels; sl@0: reqs().iBuffers = aImageCount; sl@0: reqs().iPixelFormat = aInfo.iPixelFormat; sl@0: reqs().iStride = aStride; sl@0: reqs().iOffsetToFirstBuffer = aOffsetToFirstBuffer; sl@0: reqs().iAlignment = 4; sl@0: reqs().iContiguous = EFalse; sl@0: reqs().iCacheAttrib = aIsCached ? RSurfaceManager::ECached : RSurfaceManager::ENotCached; sl@0: reqs().iMappable = ETrue; sl@0: reqs().iOffsetBetweenBuffers = aOffsetBetweenBuffers; sl@0: reqs().iSurfaceHints = hints; sl@0: reqs().iHintCount = aInfo.iUserAttributeCount; sl@0: for (TInt i = 0; i < aInfo.iUserAttributeCount; ++i) sl@0: { sl@0: reqs().iSurfaceHints[i].iKey = aInfo.iUserAttributes[i].iUid; sl@0: reqs().iSurfaceHints[i].iValue = aInfo.iUserAttributes[i].iValue; sl@0: reqs().iSurfaceHints[i].iMutable = EFalse; sl@0: } sl@0: TSurfaceId surfaceId; sl@0: if (!aCollection) sl@0: { sl@0: err = iDriverImpl.CreateSurface(reqs, surfaceId); sl@0: } sl@0: else sl@0: { sl@0: err = iDriverImpl.CreateSurface(reqs, surfaceId, aCollection->iDataChunk); sl@0: } sl@0: delete[] hints; sl@0: hints = NULL; sl@0: reqs().iSurfaceHints = NULL; sl@0: if (err != KErrNone) sl@0: { sl@0: return err; sl@0: } sl@0: iSurfaceId = surfaceId; sl@0: iCount = aImageCount; sl@0: RChunk chunk; sl@0: if (!aCollection) sl@0: { sl@0: err = iDriverImpl.MapSurface(surfaceId, chunk); sl@0: } sl@0: else sl@0: { sl@0: chunk = aCollection->iDataChunk; sl@0: err = chunk.Duplicate(RThread()); // Get a process-wide handle sl@0: } sl@0: if (err != KErrNone) sl@0: { sl@0: return err; sl@0: } sl@0: iDataChunk = chunk; sl@0: iMetaDataIndex = aMetaDataIndex; sl@0: new(chunk.Base() + aMetaDataIndex * sizeof(TSgImageMetaData)) TSgImageMetaData(aInfo, iDriverImpl.PixelFormatTable(), aIsCached); sl@0: return KErrNone; sl@0: } sl@0: sl@0: sl@0: XSgImageCollectionImpl::~XSgImageCollectionImpl() sl@0: { sl@0: if (!iSurfaceId.IsNull()) sl@0: { sl@0: iDriverImpl.CloseSurface(iSurfaceId); sl@0: iDataChunk.Close(); sl@0: } sl@0: } sl@0: sl@0: sl@0: void XSgImageCollectionImpl::Close() sl@0: { sl@0: XSgDriverImpl& driverImpl = iDriverImpl; sl@0: driverImpl.Wait(); sl@0: if (DecRefCount() == 0) sl@0: { sl@0: driverImpl.DeleteImageCollection(this); sl@0: } sl@0: driverImpl.Signal(); sl@0: } sl@0: sl@0: sl@0: const TSurfaceId& XSgImageCollectionImpl::SurfaceId() const sl@0: { sl@0: return iSurfaceId; sl@0: } sl@0: sl@0: sl@0: TInt XSgImageCollectionImpl::GetInfo(TSgImageInfo& aInfo) const sl@0: { sl@0: reinterpret_cast(iDataChunk.Base() + iMetaDataIndex * sizeof(TSgImageMetaData))->GetInfo(aInfo); sl@0: for (TInt i = 0; i < aInfo.iUserAttributeCount; ++i) sl@0: { sl@0: RSurfaceManager::THintPair hint; sl@0: hint.iKey = aInfo.iUserAttributes[i].iUid; sl@0: TInt err = iDriverImpl.GetSurfaceHint(iSurfaceId, hint); sl@0: if (err != KErrNone) sl@0: { sl@0: return err; sl@0: } sl@0: aInfo.iUserAttributes[i].iValue = hint.iValue; sl@0: } sl@0: return KErrNone; sl@0: } sl@0: sl@0: sl@0: TInt XSgImageCollectionImpl::Count() const sl@0: { sl@0: return iCount; sl@0: } sl@0: sl@0: sl@0: TInt XSgImageCollectionImpl::OpenImage(TInt aIndex, MSgDrawableAdapter*& aResult) sl@0: { sl@0: if (aIndex < 0 || aIndex >= iCount) sl@0: { sl@0: return KErrArgument; sl@0: } sl@0: union sl@0: { sl@0: TSgDrawableId id; sl@0: TSgImageId_SurfaceManager id_SurfaceManager; sl@0: }; sl@0: id_SurfaceManager.iSurfaceId = iSurfaceId; sl@0: id_SurfaceManager.iBufferIndex = aIndex; sl@0: id_SurfaceManager.iMetaDataIndex = iMetaDataIndex; sl@0: id_SurfaceManager.iFlags = 0; sl@0: iDriverImpl.Wait(); sl@0: TInt err = iDriverImpl.OpenImage(id, KSgDefaultOpenMode, aResult); sl@0: iDriverImpl.Signal(); sl@0: return err; sl@0: }