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