kerneltest/e32test/multimedia/t_camera_display.cpp
changeset 247 d8d70de2bd36
parent 201 43365a9b78a3
child 250 ac18961ed598
child 259 57b9594f5772
equal deleted inserted replaced
201:43365a9b78a3 247:d8d70de2bd36
     1 // Copyright (c) 2006-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 the License "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 // e32test\multimedia\t_camera_display.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include <e32test.h>
       
    19 #include <e32svr.h>
       
    20 #include <u32hal.h>
       
    21 #include <videodriver.h>
       
    22 #include "t_camera_display.h"
       
    23 
       
    24 _LIT(KFrameSizeConfTitle,"Current frame size  :");
       
    25 _LIT(KFrameSize, " %d x %d");
       
    26 
       
    27 #define CLIP(a) if (a < 0) a = 0; else if (a > 255) a = 255;
       
    28 
       
    29 /**
       
    30 Constructor
       
    31 */
       
    32 TCamDisplayHandler::TCamDisplayHandler()
       
    33 	{}
       
    34 
       
    35 /**
       
    36 Initialise the display handler.
       
    37 @return KErrNone if write was successful, otherwise one of the other system wide error codes.
       
    38 */
       
    39 TInt TCamDisplayHandler::Init()
       
    40 	{
       
    41 	TScreenInfoV01 screenInfo;
       
    42 	TPckg<TScreenInfoV01> screenInfoBuf(screenInfo);
       
    43 	UserSvr::ScreenInfo(screenInfoBuf);
       
    44 	iVideoAddress = (TUint8*) screenInfo.iScreenAddress;
       
    45 	iScreenWidth = screenInfo.iScreenSize.iWidth;
       
    46 	iScreenHeight = screenInfo.iScreenSize.iHeight;
       
    47 
       
    48 	TPckgBuf<TVideoInfoV01> videoInfoBuf;
       
    49 	UserSvr::HalFunction(EHalGroupDisplay, EDisplayHalCurrentModeInfo, &videoInfoBuf, NULL);
       
    50 	iBitsPerPixel = videoInfoBuf().iBitsPerPixel;
       
    51 
       
    52 	return(KErrNone);
       
    53 	}
       
    54 
       
    55 TInt TCamDisplayHandler::Min(TInt aA, TInt aB)
       
    56 	{
       
    57 	return (aA < aB) ? aA : aB;
       
    58 	}
       
    59 
       
    60 TInt TCamDisplayHandler::SetConfig(const SDevCamFrameSize& aSize,const SDevCamPixelFormat& aPixelFormat)
       
    61 	{
       
    62 	if (aPixelFormat.iPixelFormat==EUidPixelFormatYUV_422Interleaved || aPixelFormat.iPixelFormat==EUidPixelFormatRGB_565)
       
    63 		iPixelFormat=aPixelFormat;
       
    64 	else
       
    65 		return(KErrArgument);
       
    66 
       
    67 	iWidth = aSize.iWidth;
       
    68 	iHeight = aSize.iHeight;
       
    69 
       
    70 	return(KErrNone);
       
    71 	}
       
    72 
       
    73 /**
       
    74 Post process a received image.
       
    75 @return KErrNone if write was successful, otherwise one of the other system wide error codes.
       
    76 */
       
    77 TInt TCamDisplayHandler::Process(TUint8* aImageBaseAddress)
       
    78 	{
       
    79 	switch (iPixelFormat.iPixelFormat)
       
    80 		{
       
    81 		case EUidPixelFormatYUV_422Interleaved:
       
    82 			return(ProcessYUV422(aImageBaseAddress));
       
    83 		case EUidPixelFormatRGB_565:
       
    84 			return(ProcessRGB565(aImageBaseAddress));
       
    85 		default:
       
    86 			return(KErrNotSupported);
       
    87 		}
       
    88 	}
       
    89 
       
    90 /**
       
    91 Post process a received RGB565 image.
       
    92 @return KErrNone if write was successful, otherwise one of the other system wide error codes.
       
    93 */
       
    94 TInt TCamDisplayHandler::ProcessRGB565(TUint8* aImageBaseAddress)
       
    95 	{
       
    96 	TUint16* source = (TUint16*) aImageBaseAddress;
       
    97 	TUint16 pixel;
       
    98 	TInt sourceModulo, destModulo, width, height;
       
    99 	TInt r = KErrNone;
       
   100 
       
   101 	// Determine whether the screen or the picture to display is the widest, and calculate modulos
       
   102 	// and clipping sizes appropriately
       
   103 	if (iWidth < iScreenWidth)
       
   104 		{
       
   105 		width = iWidth;
       
   106 		sourceModulo = 0;
       
   107 		destModulo = (iScreenWidth - iWidth);
       
   108 		}
       
   109 	else
       
   110 		{
       
   111 		width = iScreenWidth;
       
   112 		sourceModulo = (iWidth - iScreenWidth);
       
   113 		destModulo = 0;
       
   114 		}
       
   115 
       
   116 	// Determine whether the screen or the picture to display is the highest
       
   117 	height = (iHeight < iScreenHeight) ? iHeight : iScreenHeight;
       
   118 
       
   119 	if (iBitsPerPixel == 16)
       
   120 		{
       
   121 		TUint16* dest = (TUint16*) iVideoAddress;
       
   122 
       
   123 		// Loop around and copy the data directly onto the screen
       
   124 		for (TInt line = 0; line < height; ++line)
       
   125 			{
       
   126 			for (TInt x = 0; x < width; ++x)
       
   127 				{
       
   128 				*dest++ = *source++;
       
   129 				}
       
   130 
       
   131 			source += sourceModulo;
       
   132 			dest += destModulo;
       
   133 			}
       
   134 		}
       
   135 	else if (iBitsPerPixel == 32)
       
   136 		{
       
   137 		TUint8* dest = iVideoAddress;
       
   138 
       
   139 		destModulo *= 4;
       
   140 
       
   141 		// Loop around and convert whatever part of the picture will fit onto the screen into BGRA,
       
   142 		// writing it directly onto the screen
       
   143 		for (TInt line = 0; line < height; ++line)
       
   144 			{
       
   145 			for (TInt x = 0; x < width; ++x)
       
   146 				{
       
   147 				pixel = *source++;
       
   148 				*dest++= (TUint8) ((pixel & 0x001f) << 3);
       
   149 				*dest++= (TUint8) ((pixel & 0x07e0) >> 3);
       
   150 				*dest++= (TUint8) ((pixel & 0xf800) >> 8);
       
   151 				*dest++ = 0xff;
       
   152 				}
       
   153 
       
   154 			source += sourceModulo;
       
   155 			dest += destModulo;
       
   156 			}
       
   157 		}
       
   158 	else
       
   159 		{
       
   160 		r = KErrNotSupported;
       
   161 		}
       
   162 
       
   163 	return r;
       
   164 	}
       
   165 
       
   166 /**
       
   167 Post process a received YUV422 image.
       
   168 @return KErrNone if write was successful, otherwise one of the other system wide error codes.
       
   169 */
       
   170 TInt TCamDisplayHandler::ProcessYUV422(TUint8* aImageBaseAddress)
       
   171 	{
       
   172 	TUint16* dest16 = (TUint16*) iVideoAddress;
       
   173 	TUint32* dest32 = (TUint32*) iVideoAddress;
       
   174 	TUint8* source = aImageBaseAddress;
       
   175 	TInt y, u, v, r, g, b, sourceModulo, destModulo, width, height;
       
   176 	TInt retVal = KErrNone;
       
   177 
       
   178 	// Determine whether the screen or the picture to display is the widest, and calculate modulos
       
   179 	// and clipping sizes appropriately
       
   180 	if (iWidth < iScreenWidth)
       
   181 		{
       
   182 		width = (iWidth / 2);
       
   183 		sourceModulo = 0;
       
   184 		destModulo = (iScreenWidth - iWidth);
       
   185 		}
       
   186 	else
       
   187 		{
       
   188 		width = (iScreenWidth / 2);
       
   189 		sourceModulo = ((iWidth - iScreenWidth) * 2);
       
   190 		destModulo = 0;
       
   191 		}
       
   192 
       
   193 	// Determine whether the screen or the picture to display is the highest
       
   194 	height = (iHeight < iScreenHeight) ? iHeight : iScreenHeight;
       
   195 
       
   196 	// Only 16 and 32 bits per pixel are supported.  It is also assumed that 16 bit will be RGB565 and
       
   197 	// 32 bit will be BGRA.  You will need to add support for new formats if required
       
   198 	if ((iBitsPerPixel == 16) || (iBitsPerPixel == 32))
       
   199 		{
       
   200 		// Loop around and convert whatever part of the picture will fit onto the screen into RGB565 or BGRA,
       
   201 		// writing it directly onto the screen
       
   202 		for (TInt line = 0; line < height; ++line)
       
   203 			{
       
   204 			for (TInt x = 0; x < width; ++x)
       
   205 				{
       
   206 				u = (source[0] - 128);
       
   207 				v = (source[2] - 128);
       
   208 				y = (source[3] - 16);
       
   209 
       
   210 				r = ((298 * y + 409 * u) / 256);
       
   211 				g = ((298 * y - 100 * v - 208 * u) / 256);
       
   212 				b = ((298 * y + 516 * v) / 256);
       
   213 
       
   214 				CLIP(r);
       
   215 				CLIP(g);
       
   216 				CLIP(b);
       
   217 
       
   218 				if (iBitsPerPixel == 16)
       
   219 					{
       
   220 					*dest16++ = (TUint16) (((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3));
       
   221 					}
       
   222 				else
       
   223 					{
       
   224 					*dest32++ = (0xff000000 | (r << 16) | (g << 8) | b);
       
   225 					}
       
   226 
       
   227 				y = (source[1] - 16);
       
   228 
       
   229 				r = ((298 * y + 409 * u) / 256);
       
   230 				g = ((298 * y - 100 * v - 208 * u) / 256);
       
   231 				b = ((298 * y + 516 * v) / 256);
       
   232 
       
   233 				CLIP(r);
       
   234 				CLIP(g);
       
   235 				CLIP(b);
       
   236 
       
   237 				if (iBitsPerPixel == 16)
       
   238 					{
       
   239 					*dest16++ = (TUint16) (((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3));
       
   240 					}
       
   241 				else
       
   242 					{
       
   243 					*dest32++ = (0xff000000 | (r << 16) | (g << 8) | b);
       
   244 					}
       
   245 
       
   246 				source += 4;
       
   247 				}
       
   248 
       
   249 			source += sourceModulo;
       
   250 			dest16 += destModulo;
       
   251 			dest32 += destModulo;
       
   252 			}
       
   253 		}
       
   254 	else
       
   255 		{
       
   256 		retVal = KErrNotSupported;
       
   257 		}
       
   258 
       
   259 	return retVal;
       
   260 	}
       
   261 
       
   262 /**
       
   263 Appends a string representing a pixel format UID onto a descriptor.
       
   264 @param aBuffer		Reference to the descriptor into which to append the string.  It is up to the
       
   265 					caller to ensure that this is large enough.
       
   266 @param aPixelFormat	UID of the pixel format to be converted into a string.
       
   267 */
       
   268 void AppendPixelFormat(TDes& aBuffer, TUidPixelFormat aPixelFormat)
       
   269 	{
       
   270 	if (aPixelFormat == EUidPixelFormatRGB_565)
       
   271 		aBuffer.Append(KPixelFormatRGB_565);
       
   272 	else if (aPixelFormat == EUidPixelFormatYUV_422Interleaved)
       
   273 		aBuffer.Append(KPixelFormatYUV_422Interleaved);
       
   274 	else if (aPixelFormat == EUidPixelFormatSpeedTaggedJPEG)
       
   275 		aBuffer.Append(KPixelFormatSpeedTaggedJPEG);
       
   276 	else if (aPixelFormat == EUidPixelFormatJPEG)
       
   277 		aBuffer.Append(KPixelFormatJPEG);
       
   278 	else
       
   279 		aBuffer.Append(KPixelFormatUnknown);
       
   280 	}
       
   281 
       
   282 void PrintCamModes(TCameraCapsV02* aCaps,RTest& aTest)
       
   283 	{
       
   284 	TBuf<80> buf;
       
   285 
       
   286 	// Display the supported capture modes
       
   287 	buf.Zero();
       
   288 	buf.Append(KCaptureModeCapsTitle);
       
   289 	if (aCaps->iNumImagePixelFormats)
       
   290 		buf.Append(KCaptureModeImage);
       
   291 	if (aCaps->iNumVideoPixelFormats)
       
   292 		buf.Append(KCaptureModeVideo);
       
   293 	if (aCaps->iNumViewFinderPixelFormats)
       
   294 		buf.Append(KCaptureModeViewFinder);
       
   295 	buf.Append(_L("\r\n"));
       
   296 	aTest.Printf(buf);
       
   297 
       
   298 	// Display the supported video pixel formats
       
   299 	TUint i;
       
   300 	SDevCamPixelFormat* pixelFormat;
       
   301 	if (aCaps->iNumImagePixelFormats)
       
   302 		{
       
   303 		buf.Zero();
       
   304 		buf.Append(KPixelFormatCapsTitle);
       
   305 		buf.Append(KCaptureModeImage);
       
   306 		pixelFormat = (SDevCamPixelFormat*) (aCaps + 1);
       
   307 		for (i = 0; i < aCaps->iNumImagePixelFormats; i++)
       
   308 			{
       
   309 			AppendPixelFormat(buf, pixelFormat->iPixelFormat);
       
   310 			pixelFormat++;
       
   311 			}
       
   312 		buf.Append(_L("\r\n"));
       
   313 		aTest.Printf(buf);
       
   314 		}
       
   315 
       
   316 	if (aCaps->iNumVideoPixelFormats)
       
   317 		{
       
   318 		buf.Zero();
       
   319 		buf.Append(KPixelFormatCapsTitle);
       
   320 		buf.Append(KCaptureModeVideo);
       
   321 		pixelFormat = (SDevCamPixelFormat*) (aCaps + 1);
       
   322 		for (i = aCaps->iNumImagePixelFormats; i < (aCaps->iNumImagePixelFormats + aCaps->iNumVideoPixelFormats); i++)
       
   323 			{
       
   324 			AppendPixelFormat(buf, pixelFormat->iPixelFormat);
       
   325 			pixelFormat++;
       
   326 			}
       
   327 		buf.Append(_L("\r\n"));
       
   328 		aTest.Printf(buf);
       
   329 		}
       
   330 
       
   331 	if (aCaps->iNumViewFinderPixelFormats)
       
   332 		{
       
   333 		buf.Zero();
       
   334 		buf.Append(KPixelFormatCapsTitle);
       
   335 		buf.Append(KCaptureModeViewFinder);
       
   336 		pixelFormat = (SDevCamPixelFormat*) (aCaps + 1);
       
   337 		i = aCaps->iNumImagePixelFormats + aCaps->iNumImagePixelFormats + 1;
       
   338 		for (i = aCaps->iNumImagePixelFormats + aCaps->iNumVideoPixelFormats; i < (aCaps->iNumImagePixelFormats + aCaps->iNumVideoPixelFormats + aCaps->iNumViewFinderPixelFormats); i++)
       
   339 			{
       
   340 			AppendPixelFormat(buf, pixelFormat->iPixelFormat);
       
   341 			pixelFormat++;
       
   342 			}
       
   343 		buf.Append(_L("\r\n"));
       
   344 		aTest.Printf(buf);
       
   345 		}
       
   346 	}
       
   347 
       
   348 void PrintCamConf(TCameraConfigV02& aConf,RTest& aTest)
       
   349 	{
       
   350 	TBuf<80> buf;
       
   351 
       
   352 	// Display the current frame size
       
   353 	buf.Zero();
       
   354 	buf.Append(KFrameSizeConfTitle);
       
   355 	buf.AppendFormat(KFrameSize, aConf.iFrameSize.iWidth, aConf.iFrameSize.iHeight);
       
   356 	buf.Append(_L("\r\n"));
       
   357 	aTest.Printf(buf);
       
   358 
       
   359 	// Display the current pixel format
       
   360 	buf.Zero();
       
   361 	buf.Append(KPixelFormatConfTitle);
       
   362 	AppendPixelFormat(buf, aConf.iPixelFormat.iPixelFormat);
       
   363 	buf.Append(_L("\r\n"));
       
   364 	aTest.Printf(buf);
       
   365 
       
   366 	// Display the current frame rate
       
   367 	buf.Zero();
       
   368 	buf.Format(_L("Current frame rate  : %d fps\r\n"),aConf.iFrameRate);
       
   369 	aTest.Printf(buf);
       
   370 	}
       
   371 
       
   372 void PrintBufferConf(TMmSharedChunkBufConfig& aBufConf,RTest& aTest)
       
   373 	{
       
   374 	TBuf<80> buf(0);
       
   375 
       
   376 	SBufSpecList* tempSpec = aBufConf.iSpec;
       
   377 
       
   378 	// Display the buffer configuration
       
   379 	buf.Format(_L("Buffer Config       : NumBufs:%d Size:%xH\r\n"),aBufConf.iNumBuffers,aBufConf.iBufferSizeInBytes);
       
   380 	aTest.Printf(buf);
       
   381 	if (aBufConf.iFlags & KScFlagBufOffsetListInUse)
       
   382 		{
       
   383 		buf.Format(_L(" Offsets[%08xH,%08xH,%08xH,%08xH]\r\n"),tempSpec[0].iBufferOffset,tempSpec[1].iBufferOffset,tempSpec[2].iBufferOffset,tempSpec[3].iBufferOffset);
       
   384 		aTest.Printf(buf);
       
   385 		buf.Format(_L(" Offsets[%08xH,%08xH,%08xH,%08xH]\r\n"),tempSpec[4].iBufferOffset,tempSpec[5].iBufferOffset,tempSpec[6].iBufferOffset,tempSpec[7].iBufferOffset);
       
   386 		aTest.Printf(buf);
       
   387 		}
       
   388 	}