graphicsresourceservices/graphicsresourceadaptation/src/sgimagecollectionimpl.cpp
changeset 0 5d03bc08d59c
equal deleted inserted replaced
-1:000000000000 0:5d03bc08d59c
       
     1 // Copyright (c) 2007-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".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "sgimagecollectionimpl.h"
       
    17 #include "sgimageimpl.h"
       
    18 
       
    19 
       
    20 TInt XSgImageCollectionImpl::New(XSgImageCollectionImpl*& aPtr, XSgDriverImpl& aDriverImpl,
       
    21                                  const TSgImageInfo& aInfo, TInt aImageCount, TBool aIsCached,
       
    22                                  TInt aStride, TInt aOffsetToFirstBuffer, TInt aOffsetBetweenBuffers,
       
    23                                  TInt aMetaDataIndex, const XSgImageCollectionImpl* aCollection)
       
    24 	{
       
    25 	aPtr = static_cast<XSgImageCollectionImpl*>(aDriverImpl.Alloc(sizeof(XSgImageCollectionImpl)));
       
    26 	if (!aPtr)
       
    27 		{
       
    28 		return KErrNoMemory;
       
    29 		}
       
    30 	new(aPtr) XSgImageCollectionImpl(aDriverImpl);
       
    31 	TInt err = aPtr->Construct(aInfo, aImageCount, aIsCached, aStride, aOffsetToFirstBuffer, aOffsetBetweenBuffers, aMetaDataIndex, aCollection);
       
    32 	if (err != KErrNone)
       
    33 		{
       
    34 		aPtr->Delete();
       
    35 		aPtr = NULL;
       
    36 		}
       
    37 	return err;
       
    38 	}
       
    39 
       
    40 
       
    41 TInt XSgImageCollectionImpl::Construct(const TSgImageInfo& aInfo, TInt aImageCount, TBool aIsCached,
       
    42                                        TInt aStride, TInt aOffsetToFirstBuffer, TInt aOffsetBetweenBuffers,
       
    43                                        TInt aMetaDataIndex, const XSgImageCollectionImpl* aCollection)
       
    44 	{
       
    45 	TInt maxNumberOfHints;
       
    46 	TInt err;
       
    47 	err=iDriverImpl.GetSurfaceManagerAttrib(RSurfaceManager::EMaxNumberOfHints,maxNumberOfHints);
       
    48 	if (err != KErrNone)
       
    49 	    {
       
    50 	    return err;
       
    51 	    } 
       
    52 			
       
    53 	if (aInfo.iUserAttributeCount > maxNumberOfHints)
       
    54 		{
       
    55 		return KErrOverflow;
       
    56 		}
       
    57 	RSurfaceManager::THintPair* hints = new RSurfaceManager::THintPair[aInfo.iUserAttributeCount];
       
    58 	if(!hints)
       
    59 		{
       
    60 		return KErrNoMemory;
       
    61 		}
       
    62 
       
    63 	RSurfaceManager::TSurfaceCreationAttributesBuf reqs;
       
    64 	reqs().iSize = aInfo.iSizeInPixels;
       
    65 	reqs().iBuffers = aImageCount;
       
    66 	reqs().iPixelFormat = aInfo.iPixelFormat;
       
    67 	reqs().iStride = aStride;
       
    68 	reqs().iOffsetToFirstBuffer = aOffsetToFirstBuffer;
       
    69 	reqs().iAlignment = 4;
       
    70 	reqs().iContiguous = EFalse;
       
    71 	reqs().iCacheAttrib = aIsCached ? RSurfaceManager::ECached : RSurfaceManager::ENotCached;
       
    72 	reqs().iMappable = ETrue;
       
    73 	reqs().iOffsetBetweenBuffers = aOffsetBetweenBuffers;
       
    74 	reqs().iSurfaceHints = hints;
       
    75 	reqs().iHintCount = aInfo.iUserAttributeCount;
       
    76 	for (TInt i = 0; i < aInfo.iUserAttributeCount; ++i)
       
    77 		{
       
    78 		reqs().iSurfaceHints[i].iKey = aInfo.iUserAttributes[i].iUid;
       
    79 		reqs().iSurfaceHints[i].iValue = aInfo.iUserAttributes[i].iValue;
       
    80 		reqs().iSurfaceHints[i].iMutable = EFalse;
       
    81 		}	
       
    82 	TSurfaceId surfaceId;
       
    83 	if (!aCollection)
       
    84 		{
       
    85 		err = iDriverImpl.CreateSurface(reqs, surfaceId);
       
    86 		}
       
    87 	else
       
    88 		{
       
    89 		err = iDriverImpl.CreateSurface(reqs, surfaceId, aCollection->iDataChunk);
       
    90 		}
       
    91 	delete[] hints;
       
    92 	hints = NULL;
       
    93 	reqs().iSurfaceHints = NULL;
       
    94 	if (err != KErrNone)
       
    95 		{
       
    96 		return err;
       
    97 		}
       
    98 	iSurfaceId = surfaceId;
       
    99 	iCount = aImageCount;
       
   100 	RChunk chunk;
       
   101 	if (!aCollection)
       
   102 		{
       
   103 		err = iDriverImpl.MapSurface(surfaceId, chunk);
       
   104 		}
       
   105 	else
       
   106 		{
       
   107 		chunk = aCollection->iDataChunk;
       
   108 		err = chunk.Duplicate(RThread()); // Get a process-wide handle
       
   109 		}
       
   110 	if (err != KErrNone)
       
   111 		{
       
   112 		return err;
       
   113 		}
       
   114 	iDataChunk = chunk;
       
   115 	iMetaDataIndex = aMetaDataIndex;
       
   116 	new(chunk.Base() + aMetaDataIndex * sizeof(TSgImageMetaData)) TSgImageMetaData(aInfo, iDriverImpl.PixelFormatTable(), aIsCached);
       
   117 	return KErrNone;
       
   118 	}
       
   119 
       
   120 
       
   121 XSgImageCollectionImpl::~XSgImageCollectionImpl()
       
   122 	{
       
   123 	if (!iSurfaceId.IsNull())
       
   124 		{
       
   125 		iDriverImpl.CloseSurface(iSurfaceId);
       
   126 		iDataChunk.Close();
       
   127 		}
       
   128 	}
       
   129 
       
   130 
       
   131 void XSgImageCollectionImpl::Close()
       
   132 	{
       
   133 	XSgDriverImpl& driverImpl = iDriverImpl;
       
   134 	driverImpl.Wait();
       
   135 	if (DecRefCount() == 0)
       
   136 		{
       
   137 		driverImpl.DeleteImageCollection(this);
       
   138 		}
       
   139 	driverImpl.Signal();
       
   140 	}
       
   141 
       
   142 
       
   143 const TSurfaceId& XSgImageCollectionImpl::SurfaceId() const
       
   144 	{
       
   145 	return iSurfaceId;
       
   146 	}
       
   147 
       
   148 
       
   149 TInt XSgImageCollectionImpl::GetInfo(TSgImageInfo& aInfo) const
       
   150 	{
       
   151 	reinterpret_cast<TSgImageMetaData*>(iDataChunk.Base() + iMetaDataIndex * sizeof(TSgImageMetaData))->GetInfo(aInfo);
       
   152 	for (TInt i = 0; i < aInfo.iUserAttributeCount; ++i)
       
   153 		{
       
   154 		RSurfaceManager::THintPair hint;
       
   155 		hint.iKey = aInfo.iUserAttributes[i].iUid;
       
   156 		TInt err = iDriverImpl.GetSurfaceHint(iSurfaceId, hint);
       
   157 		if (err != KErrNone)
       
   158 			{
       
   159 			return err;
       
   160 			}
       
   161 		aInfo.iUserAttributes[i].iValue = hint.iValue;
       
   162 		}
       
   163 	return KErrNone;
       
   164 	}
       
   165 
       
   166 
       
   167 TInt XSgImageCollectionImpl::Count() const
       
   168 	{
       
   169 	return iCount;
       
   170 	}
       
   171 
       
   172 
       
   173 TInt XSgImageCollectionImpl::OpenImage(TInt aIndex, MSgDrawableAdapter*& aResult)
       
   174 	{
       
   175 	if (aIndex < 0 || aIndex >= iCount)
       
   176 		{
       
   177 		return KErrArgument;
       
   178 		}
       
   179 	union
       
   180 		{
       
   181 		TSgDrawableId id;
       
   182 		TSgImageId_SurfaceManager id_SurfaceManager;
       
   183 		};
       
   184 	id_SurfaceManager.iSurfaceId = iSurfaceId;
       
   185 	id_SurfaceManager.iBufferIndex = aIndex;
       
   186 	id_SurfaceManager.iMetaDataIndex = iMetaDataIndex;
       
   187 	id_SurfaceManager.iFlags = 0;
       
   188 	iDriverImpl.Wait();
       
   189 	TInt err = iDriverImpl.OpenImage(id, KSgDefaultOpenMode, aResult);
       
   190 	iDriverImpl.Signal();
       
   191 	return err;
       
   192 	}