os/kernelhwsrv/kerneltest/e32test/examples/camera1/camera1_ldd.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2005-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
// its implementation.
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
/**
sl@0
    19
 @file An example camera device driver which uses Shared Chunks in
sl@0
    20
 @publishedPartner
sl@0
    21
 @prototype 9.1
sl@0
    22
*/
sl@0
    23
sl@0
    24
#include <kernel/kern_priv.h>
sl@0
    25
#include <kernel/cache.h>
sl@0
    26
#include "camera1.h"
sl@0
    27
#include "camera1_dev.h"
sl@0
    28
sl@0
    29
#if 0  // Set true for tracing
sl@0
    30
#define TRACE(x) x
sl@0
    31
#else
sl@0
    32
#define TRACE(x)
sl@0
    33
#endif
sl@0
    34
sl@0
    35
//
sl@0
    36
// DCamera1Factory
sl@0
    37
//
sl@0
    38
sl@0
    39
/**
sl@0
    40
  Standard export function for LDDs. This creates a DLogicalDevice derived object,
sl@0
    41
  in this case, our DCamera1Factory
sl@0
    42
*/
sl@0
    43
DECLARE_STANDARD_LDD()
sl@0
    44
	{
sl@0
    45
	return new DCamera1Factory;
sl@0
    46
	}
sl@0
    47
sl@0
    48
/**
sl@0
    49
  Constructor
sl@0
    50
*/
sl@0
    51
DCamera1Factory::DCamera1Factory()
sl@0
    52
	{
sl@0
    53
	// Set version number for this device
sl@0
    54
	iVersion=RCamera1::VersionRequired();
sl@0
    55
	// Indicate that do support units or a PDD
sl@0
    56
	iParseMask=0;
sl@0
    57
	}
sl@0
    58
sl@0
    59
/**
sl@0
    60
  Second stage constructor for DCamera1Factory.
sl@0
    61
  This must at least set a name for the driver object.
sl@0
    62
sl@0
    63
  @return KErrNone if successful, otherwise one of the other system wide error codes.
sl@0
    64
*/
sl@0
    65
TInt DCamera1Factory::Install()
sl@0
    66
	{
sl@0
    67
	return SetName(&RCamera1::Name());
sl@0
    68
	}
sl@0
    69
sl@0
    70
/**
sl@0
    71
  Destructor
sl@0
    72
*/
sl@0
    73
DCamera1Factory::~DCamera1Factory()
sl@0
    74
	{
sl@0
    75
	}
sl@0
    76
sl@0
    77
/**
sl@0
    78
  Return the drivers capabilities.
sl@0
    79
  Called in the response to an RDevice::GetCaps() request.
sl@0
    80
sl@0
    81
  @param aDes User-side descriptor to write capabilities information into
sl@0
    82
*/
sl@0
    83
void DCamera1Factory::GetCaps(TDes8& aDes) const
sl@0
    84
	{
sl@0
    85
	// Create a capabilities object
sl@0
    86
	RCamera1::TCaps caps;
sl@0
    87
	caps.iVersion = iVersion;
sl@0
    88
	// Write it back to user memory
sl@0
    89
	Kern::InfoCopy(aDes,(TUint8*)&caps,sizeof(caps));
sl@0
    90
	}
sl@0
    91
sl@0
    92
/**
sl@0
    93
  Called by the kernel's device driver framework to create a Logical Channel.
sl@0
    94
  This is called in the context of the user thread (client) which requested the creation of a Logical Channel
sl@0
    95
  (E.g. through a call to RBusLogicalChannel::DoCreate)
sl@0
    96
  The thread is in a critical section.
sl@0
    97
sl@0
    98
  @param aChannel Set to point to the created Logical Channel
sl@0
    99
sl@0
   100
  @return KErrNone if successful, otherwise one of the other system wide error codes.
sl@0
   101
*/
sl@0
   102
TInt DCamera1Factory::Create(DLogicalChannelBase*& aChannel)
sl@0
   103
	{
sl@0
   104
	aChannel=new DCamera1Channel;
sl@0
   105
	if(!aChannel)
sl@0
   106
		return KErrNoMemory;
sl@0
   107
sl@0
   108
	return KErrNone;
sl@0
   109
	}
sl@0
   110
sl@0
   111
//
sl@0
   112
// Logical Channel
sl@0
   113
//
sl@0
   114
sl@0
   115
/**
sl@0
   116
  Default configuration for driver (640x480 pixels of 32bits captured at 15 frames/sec)
sl@0
   117
*/
sl@0
   118
static const RCamera1::TConfig DefaultConfig = {{640,480},4,15};
sl@0
   119
sl@0
   120
/**
sl@0
   121
  Constructor
sl@0
   122
*/
sl@0
   123
DCamera1Channel::DCamera1Channel()
sl@0
   124
	:	iDfcQ(Kern::TimerDfcQ()),  // This test uses the timer DFC queue for DFCs
sl@0
   125
		iStateChangeDfc(StateChangeDfcTrampoline,this,1),  // DFC is priority '1'
sl@0
   126
		iConfig(DefaultConfig),
sl@0
   127
		iCaptureTimer(CaptureDfcTrampoline,this)
sl@0
   128
	{
sl@0
   129
	}
sl@0
   130
sl@0
   131
/**
sl@0
   132
  Second stage constructor called by the kernel's device driver framework.
sl@0
   133
  This is called in the context of the user thread (client) which requested the creation of a Logical Channel
sl@0
   134
  (E.g. through a call to RBusLogicalChannel::DoCreate)
sl@0
   135
  The thread is in a critical section.
sl@0
   136
sl@0
   137
  @param aUnit The unit argument supplied by the client to RBusLogicalChannel::DoCreate
sl@0
   138
  @param aInfo The info argument supplied by the client to RBusLogicalChannel::DoCreate
sl@0
   139
  @param aVer The version argument supplied by the client to RBusLogicalChannel::DoCreate
sl@0
   140
sl@0
   141
  @return KErrNone if successful, otherwise one of the other system wide error codes.
sl@0
   142
*/
sl@0
   143
TInt DCamera1Channel::DoCreate(TInt /*aUnit*/, const TDesC8* /*aInfo*/, const TVersion& aVer)
sl@0
   144
	{
sl@0
   145
	// Check client has EMultimediaDD capability
sl@0
   146
	if(!Kern::CurrentThreadHasCapability(ECapabilityMultimediaDD,__PLATSEC_DIAGNOSTIC_STRING("Checked by CAPTURE1")))
sl@0
   147
		return KErrPermissionDenied;
sl@0
   148
sl@0
   149
	// Check version
sl@0
   150
	if (!Kern::QueryVersionSupported(RCamera1::VersionRequired(),aVer))
sl@0
   151
		return KErrNotSupported;
sl@0
   152
sl@0
   153
	// Setup DFCs
sl@0
   154
	iStateChangeDfc.SetDfcQ(iDfcQ);
sl@0
   155
sl@0
   156
	// Done
sl@0
   157
	return Kern::MutexCreate(iStateChangeMutex,KNullDesC,KMutexOrdGeneral7);
sl@0
   158
	}
sl@0
   159
sl@0
   160
/**
sl@0
   161
  Destructor
sl@0
   162
*/
sl@0
   163
DCamera1Channel::~DCamera1Channel()
sl@0
   164
	{
sl@0
   165
	DoCancel(RCamera1::EAllRequests);
sl@0
   166
	EndCapture();
sl@0
   167
	iStateChangeDfc.Cancel();
sl@0
   168
	if(iStateChangeMutex)
sl@0
   169
		iStateChangeMutex->Close(0);
sl@0
   170
	if(iCaptureBuffers)
sl@0
   171
		iCaptureBuffers->Close();
sl@0
   172
	}
sl@0
   173
sl@0
   174
/**
sl@0
   175
  Process a request on this logical channel.
sl@0
   176
sl@0
   177
  @param aReqNo Request number:
sl@0
   178
  	            ==KMaxTInt, a 'DoCancel' message
sl@0
   179
	            >=0, a 'DoControl' message with function number equal to iValue
sl@0
   180
	            <0, a 'DoRequest' message with function number equal to ~iValue
sl@0
   181
  @param a1     First argument. For DoRequest requests this is a pointer to the TRequestStatus.
sl@0
   182
  @param a2     Second argument. For DoRequest this is a pointer to the 2 actual TAny* arguments.
sl@0
   183
sl@0
   184
  @return       Result. Ignored by device driver framework for DoRequest requests.
sl@0
   185
*/
sl@0
   186
TInt DCamera1Channel::Request(TInt aReqNo, TAny* a1, TAny* a2)
sl@0
   187
	{
sl@0
   188
	// Decode the message type and dispatch it to the relevent handler function...
sl@0
   189
	if ((TUint)aReqNo<(TUint)KMaxTInt)
sl@0
   190
		return DoControl(aReqNo,a1,a2);
sl@0
   191
	if(aReqNo==KMaxTInt)
sl@0
   192
		return DoCancel((TInt)a1);
sl@0
   193
	return DoRequest(aReqNo,a1,a2);
sl@0
   194
	}
sl@0
   195
sl@0
   196
/**
sl@0
   197
  Process synchronous 'control' requests
sl@0
   198
*/
sl@0
   199
TInt DCamera1Channel::DoControl(TInt aFunction, TAny* a1, TAny* a2)
sl@0
   200
	{
sl@0
   201
	TRACE(Kern::Printf(">DCamera1Channel::DoControl fn=%d\n",aFunction);)
sl@0
   202
sl@0
   203
	(void)a2;   // a2 not used in this example
sl@0
   204
sl@0
   205
	TInt r = KErrNotSupported;
sl@0
   206
	switch (aFunction)
sl@0
   207
		{
sl@0
   208
		case RCamera1::EGetConfig:
sl@0
   209
			r = GetConfig((TDes8*)a1);
sl@0
   210
			break;
sl@0
   211
sl@0
   212
		case RCamera1::ESetConfig:
sl@0
   213
			r = SetConfig((const TDesC8*)a1);
sl@0
   214
			break;
sl@0
   215
sl@0
   216
		case RCamera1::EStartCapture:
sl@0
   217
			r = StartCapture();
sl@0
   218
			break;
sl@0
   219
sl@0
   220
		case RCamera1::EEndCapture:
sl@0
   221
			r = EndCapture();
sl@0
   222
			break;
sl@0
   223
sl@0
   224
		case RCamera1::EReleaseImage:
sl@0
   225
			r = ImageRelease((TInt)a1);
sl@0
   226
			break;
sl@0
   227
sl@0
   228
		case RCamera1::ECaptureImage:
sl@0
   229
			CaptureImage((TRequestStatus*)a1,(TInt)a2);
sl@0
   230
			break;
sl@0
   231
		}
sl@0
   232
sl@0
   233
	TRACE(Kern::Printf("<DCamera1Channel::DoControl result=%d\n",r);)
sl@0
   234
sl@0
   235
	return r;
sl@0
   236
	}
sl@0
   237
sl@0
   238
/**
sl@0
   239
  Process asynchronous requests.
sl@0
   240
  This driver doesn't have any 'DoRequest' requests because we handle asyncronous
sl@0
   241
  requests using 'DoControl' for performance reasons. I.e. to avoid having to read
sl@0
   242
  the arguments with kumemget()
sl@0
   243
*/
sl@0
   244
TInt DCamera1Channel::DoRequest(TInt aNotReqNo, TAny* a1, TAny* a2)
sl@0
   245
	{
sl@0
   246
	TRACE(Kern::Printf(">DCamera1Channel::DoRequest req=%d\n",aNotReqNo);)
sl@0
   247
sl@0
   248
	// Get arguments
sl@0
   249
	TAny* a[2];
sl@0
   250
	kumemget32(a,a2,sizeof(a)); 
sl@0
   251
	TRequestStatus* status=(TRequestStatus*)a1;
sl@0
   252
	TInt reqNo = ~aNotReqNo;
sl@0
   253
sl@0
   254
	// Do the request
sl@0
   255
	TInt r;
sl@0
   256
	switch(reqNo)
sl@0
   257
		{
sl@0
   258
		case RCamera1::ECaptureImage:
sl@0
   259
			// Not used because we do 'ECaptureImage' as a DoControl rather than
sl@0
   260
			// a DoRequest for performance reasons
sl@0
   261
sl@0
   262
		default:
sl@0
   263
			r = KErrNotSupported;
sl@0
   264
			break;
sl@0
   265
		}
sl@0
   266
sl@0
   267
	// Complete request if there was an error
sl@0
   268
	if (r!=KErrNone)
sl@0
   269
		Kern::RequestComplete(&Kern::CurrentThread(),status,r);
sl@0
   270
sl@0
   271
	TRACE(Kern::Printf("<DCamera1Channel::DoRequest result=%d\n",r);)
sl@0
   272
sl@0
   273
	return KErrNone;  // Result is ignored by device driver framework for DoRequest requests
sl@0
   274
	}
sl@0
   275
sl@0
   276
/**
sl@0
   277
  Process cancelling of asynchronous requests.
sl@0
   278
*/
sl@0
   279
TInt DCamera1Channel::DoCancel(TUint aMask)
sl@0
   280
	{
sl@0
   281
	TRACE(Kern::Printf(">DCamera1Channel::DoCancel mask=%08x\n",aMask);)
sl@0
   282
sl@0
   283
	if(aMask&(1<<RCamera1::ECaptureImage))
sl@0
   284
		CaptureImageCancel();
sl@0
   285
sl@0
   286
	TRACE(Kern::Printf("<DCamera1Channel::DoCancel\n");)
sl@0
   287
sl@0
   288
	return KErrNone;
sl@0
   289
	}
sl@0
   290
sl@0
   291
//
sl@0
   292
// Methods for processing configuration control messages
sl@0
   293
//
sl@0
   294
sl@0
   295
/**
sl@0
   296
  Process a GetConfig control message. This writes the current driver configuration to a
sl@0
   297
  RCamera1::TConfigBuf supplied by the client.
sl@0
   298
*/
sl@0
   299
TInt DCamera1Channel::GetConfig(TDes8* aConfigBuf)
sl@0
   300
	{
sl@0
   301
	// Write the config to the client
sl@0
   302
	Kern::InfoCopy(*aConfigBuf,(const TUint8*)&iConfig,sizeof(iConfig));
sl@0
   303
	return KErrNone;
sl@0
   304
	}
sl@0
   305
sl@0
   306
/**
sl@0
   307
  Process a SetConfig control message. This sets the driver configuration using a
sl@0
   308
  RCamera1::TConfigBuf supplied by the client.
sl@0
   309
*/
sl@0
   310
TInt DCamera1Channel::SetConfig(const TDesC8* aConfigBuf)
sl@0
   311
	{
sl@0
   312
	// Create a config structure.
sl@0
   313
	RCamera1::TConfig config(DefaultConfig);
sl@0
   314
sl@0
   315
	// Note: We have constructed a config using DefaultConfig, this is to allow
sl@0
   316
	// backwards compatibility when a client gives us an old (and shorter) version
sl@0
   317
	// of the config structure.
sl@0
   318
sl@0
   319
	// Read the config structure from client
sl@0
   320
	TPtr8 ptr((TUint8*)&config,sizeof(config));
sl@0
   321
	Kern::KUDesGet(ptr,*aConfigBuf);
sl@0
   322
sl@0
   323
	// For some settings we allow zero to mean default...
sl@0
   324
	if(!config.iImageSize.iWidth)
sl@0
   325
		config.iImageSize.iWidth = DefaultConfig.iImageSize.iWidth;
sl@0
   326
	if(!config.iImageSize.iHeight)
sl@0
   327
		config.iImageSize.iHeight = DefaultConfig.iImageSize.iHeight;
sl@0
   328
	if(!config.iImageBytesPerPixel)
sl@0
   329
		config.iImageBytesPerPixel = DefaultConfig.iImageBytesPerPixel;
sl@0
   330
sl@0
   331
	// Validate configuration
sl@0
   332
	TInt scale = DefaultConfig.iImageSize.iWidth/config.iImageSize.iWidth;
sl@0
   333
	if(scale*config.iImageSize.iWidth != DefaultConfig.iImageSize.iWidth)
sl@0
   334
		return KErrArgument;
sl@0
   335
	if(scale*config.iImageSize.iHeight != DefaultConfig.iImageSize.iHeight)
sl@0
   336
		return KErrArgument;
sl@0
   337
	if(config.iImageBytesPerPixel<=0 || config.iImageBytesPerPixel>4)
sl@0
   338
		return KErrArgument;
sl@0
   339
sl@0
   340
	if(config.iFrameRate<0)
sl@0
   341
		return KErrArgument;
sl@0
   342
	if(config.iNumImageBuffers<1)
sl@0
   343
		return KErrArgument;
sl@0
   344
sl@0
   345
	TInt imageSize;
sl@0
   346
	DCaptureBuffers* buffers;
sl@0
   347
	TInt r;
sl@0
   348
sl@0
   349
	// Need to be in critical section whilst holding a DMutex
sl@0
   350
	NKern::ThreadEnterCS();
sl@0
   351
sl@0
   352
	// Claim state change mutex. Note, the return value is ignored because a Wait
sl@0
   353
	// can only fail if the mutex is destroyed whilst waiting for it, this can't 
sl@0
   354
	// happen in our driver.
sl@0
   355
	Kern::MutexWait(*iStateChangeMutex);
sl@0
   356
sl@0
   357
	// Check we aren't in the middle of capturing images
sl@0
   358
	if(iCapturing)
sl@0
   359
		{
sl@0
   360
		r = KErrInUse;
sl@0
   361
		goto done;
sl@0
   362
		}
sl@0
   363
sl@0
   364
	// Change the config
sl@0
   365
	iConfig = config; 
sl@0
   366
	iCaptureRateTicks = config.iFrameRate ? 1000000/config.iFrameRate/NKern::TickPeriod() : KMaxTInt;
sl@0
   367
	if(iCaptureRateTicks<1)
sl@0
   368
		iCaptureRateTicks = 1;
sl@0
   369
sl@0
   370
	// Claim ownership of old buffers
sl@0
   371
	NKern::FMWait(&iCaptureMutex);
sl@0
   372
	buffers = iCaptureBuffers;
sl@0
   373
	iCaptureBuffers = NULL;
sl@0
   374
	NKern::FMSignal(&iCaptureMutex);
sl@0
   375
sl@0
   376
	// Delete old buffers
sl@0
   377
	if(buffers)
sl@0
   378
		buffers->Close();
sl@0
   379
sl@0
   380
	// Contruct new buffer object
sl@0
   381
	imageSize = iConfig.iImageSize.iWidth*iConfig.iImageSize.iHeight*iConfig.iImageBytesPerPixel;
sl@0
   382
	buffers = DCaptureBuffers::New(2+iConfig.iNumImageBuffers,imageSize);
sl@0
   383
	if(!buffers)
sl@0
   384
		{
sl@0
   385
		r = KErrNoMemory;
sl@0
   386
		goto done;
sl@0
   387
		}
sl@0
   388
sl@0
   389
	// Use the new buffers if another thread didn't create them first
sl@0
   390
	NKern::FMWait(&iCaptureMutex);
sl@0
   391
	iCaptureBuffers = buffers;
sl@0
   392
	NKern::FMSignal(&iCaptureMutex);
sl@0
   393
sl@0
   394
	// Create handle for chunk
sl@0
   395
	r = Kern::MakeHandleAndOpen(NULL, iCaptureBuffers->iChunk);
sl@0
   396
sl@0
   397
done:
sl@0
   398
	// Release state change mutex
sl@0
   399
	Kern::MutexSignal(*iStateChangeMutex);
sl@0
   400
sl@0
   401
	NKern::ThreadLeaveCS();
sl@0
   402
sl@0
   403
	return r;
sl@0
   404
	}
sl@0
   405
sl@0
   406
//
sl@0
   407
// Methods for processing start/end capture
sl@0
   408
//
sl@0
   409
sl@0
   410
/**
sl@0
   411
   Start image capturing
sl@0
   412
*/
sl@0
   413
TInt DCamera1Channel::StartCapture()
sl@0
   414
	{
sl@0
   415
	// Need to be in critical section whilst holding a DMutex
sl@0
   416
	NKern::ThreadEnterCS();
sl@0
   417
sl@0
   418
	// Claim state change mutex. Note, the return value is ignored because a Wait
sl@0
   419
	// can only fail if the mutex is destroyed whilst waiting for it, this can't 
sl@0
   420
	// happen in our driver.
sl@0
   421
	Kern::MutexWait(*iStateChangeMutex);
sl@0
   422
sl@0
   423
	NKern::FMWait(&iCaptureMutex);
sl@0
   424
sl@0
   425
	TInt r;
sl@0
   426
	if(!iCaptureBuffers)
sl@0
   427
		r = KErrNotReady;  // SetConfig not yet been called
sl@0
   428
	else if(iCapturing)
sl@0
   429
		r = KErrInUse;     // StartCapture has already been called
sl@0
   430
	else
sl@0
   431
		{
sl@0
   432
		// Initialise image buffer state for capturing images
sl@0
   433
		iCaptureBuffers->Reset();
sl@0
   434
sl@0
   435
		// Flag capturing started
sl@0
   436
		iCapturing = ETrue;
sl@0
   437
		r = KErrNone;
sl@0
   438
		}
sl@0
   439
sl@0
   440
	NKern::FMSignal(&iCaptureMutex);
sl@0
   441
sl@0
   442
	// Get state change DFC to initialise camera hardware for capture
sl@0
   443
	if(r==KErrNone)
sl@0
   444
		StateChange(ETrue);
sl@0
   445
sl@0
   446
	// Release state change mutex
sl@0
   447
	Kern::MutexSignal(*iStateChangeMutex);
sl@0
   448
sl@0
   449
	NKern::ThreadLeaveCS();
sl@0
   450
sl@0
   451
	return r;
sl@0
   452
	}
sl@0
   453
sl@0
   454
/**
sl@0
   455
   End image capturing
sl@0
   456
*/
sl@0
   457
TInt DCamera1Channel::EndCapture()
sl@0
   458
	{
sl@0
   459
	// Need to be in critical section whilst holding a DMutex
sl@0
   460
	NKern::ThreadEnterCS();
sl@0
   461
sl@0
   462
	// Claim state change mutex. Note, the return value is ignored because a Wait
sl@0
   463
	// can only fail if the mutex is destroyed whilst waiting for it, this can't 
sl@0
   464
	// happen in our driver.
sl@0
   465
	Kern::MutexWait(*iStateChangeMutex);
sl@0
   466
sl@0
   467
	if(iCapturing)
sl@0
   468
		{
sl@0
   469
		// Get state change DFC to reset camera hardware
sl@0
   470
		StateChange(EFalse);
sl@0
   471
sl@0
   472
		// Flag capture ended
sl@0
   473
		NKern::FMWait(&iCaptureMutex);
sl@0
   474
		iCapturing = EFalse;
sl@0
   475
		NKern::FMSignal(&iCaptureMutex);
sl@0
   476
sl@0
   477
		// Cancel any pending caoture request
sl@0
   478
		CaptureImageCancel();
sl@0
   479
		}
sl@0
   480
sl@0
   481
	// Release state change mutex
sl@0
   482
	Kern::MutexSignal(*iStateChangeMutex);
sl@0
   483
sl@0
   484
	NKern::ThreadLeaveCS();
sl@0
   485
sl@0
   486
	return KErrNone;
sl@0
   487
	}
sl@0
   488
sl@0
   489
/**
sl@0
   490
  Performs state change on Start/EndCapture by calling state change DFC
sl@0
   491
  Call with iStateChangeMutex held.
sl@0
   492
sl@0
   493
  @param aNewState True to start image capture, false to stop image capture.
sl@0
   494
*/
sl@0
   495
void DCamera1Channel::StateChange(TBool aNewState)
sl@0
   496
	{
sl@0
   497
	iNewState = aNewState;
sl@0
   498
	NKern::FSSetOwner(&iStateChangeSemaphore,NULL);
sl@0
   499
	iStateChangeDfc.Enque();
sl@0
   500
	NKern::FSWait(&iStateChangeSemaphore);
sl@0
   501
	}
sl@0
   502
sl@0
   503
/**
sl@0
   504
  DFC callback called when Start/EndCapture requests are made.
sl@0
   505
*/
sl@0
   506
void DCamera1Channel::StateChangeDfcTrampoline(TAny* aSelf)
sl@0
   507
	{
sl@0
   508
	// Just call non-static method
sl@0
   509
	((DCamera1Channel*)aSelf)->StateChangeDfc();
sl@0
   510
	}
sl@0
   511
sl@0
   512
/**
sl@0
   513
  DFC callback called when Start/EndCapture requests are made.
sl@0
   514
*/
sl@0
   515
void DCamera1Channel::StateChangeDfc()
sl@0
   516
	{
sl@0
   517
	TRACE(Kern::Printf(">DCamera1Channel::StateChangeDfc\n");)
sl@0
   518
sl@0
   519
	// Call relevent state change function
sl@0
   520
	if(iNewState)
sl@0
   521
		DoStartCapture();
sl@0
   522
	else
sl@0
   523
		DoEndCapture();
sl@0
   524
sl@0
   525
	// Signal completion
sl@0
   526
	NKern::FSSignal(&iStateChangeSemaphore);
sl@0
   527
sl@0
   528
	TRACE(Kern::Printf("<DCamera1Channel::StateChangeDfc\n");)
sl@0
   529
	}
sl@0
   530
sl@0
   531
//
sl@0
   532
// Methods for processing CaptureImage
sl@0
   533
//
sl@0
   534
sl@0
   535
/**
sl@0
   536
  Process Capture Image request 
sl@0
   537
*/
sl@0
   538
void DCamera1Channel::CaptureImage(TRequestStatus* aRequestStatus,TInt aReleaseImage)
sl@0
   539
	{
sl@0
   540
	TInt r=KErrNone;
sl@0
   541
sl@0
   542
	// Get the thread making the request
sl@0
   543
	DThread* requestThread = &Kern::CurrentThread();
sl@0
   544
sl@0
   545
	// Release image (if one was specified)
sl@0
   546
	if(aReleaseImage!=-1)
sl@0
   547
		{
sl@0
   548
		r = ImageRelease(aReleaseImage);
sl@0
   549
		if(r!=KErrNone)
sl@0
   550
			goto done;
sl@0
   551
		}
sl@0
   552
sl@0
   553
	NKern::FMWait(&iCaptureMutex);
sl@0
   554
sl@0
   555
	if(!iCapturing)
sl@0
   556
		r = KErrNotReady;     // StartCapture hasn't yet been called
sl@0
   557
	else if(iCaptureRequestStatus)
sl@0
   558
		r = KErrInUse;        // There is already a pending CaptureImage request
sl@0
   559
	else
sl@0
   560
		{
sl@0
   561
		// See if an image is already available...
sl@0
   562
		DImageBuffer* buffer=iCaptureBuffers->ImageForClient();
sl@0
   563
		if(buffer)
sl@0
   564
			{
sl@0
   565
			// Return offset of buffer to client
sl@0
   566
			r = buffer->iChunkOffset;
sl@0
   567
			}
sl@0
   568
		else
sl@0
   569
			{
sl@0
   570
			// Image not found...
sl@0
   571
			if(!iCaptureBuffers->iFreeBuffers[0])
sl@0
   572
				r = KErrOverflow;  // Out of buffers
sl@0
   573
			else
sl@0
   574
				{
sl@0
   575
				// Wait for new image to become available
sl@0
   576
				iCaptureRequestStatus = aRequestStatus;
sl@0
   577
				requestThread->Open(); // can't fail because this is the current thread
sl@0
   578
				iCaptureRequestThread = requestThread;
sl@0
   579
				r = KErrNone;
sl@0
   580
				}
sl@0
   581
			}
sl@0
   582
		}
sl@0
   583
sl@0
   584
	NKern::FMSignal(&iCaptureMutex);
sl@0
   585
sl@0
   586
done:
sl@0
   587
	// Complete request if there was an error
sl@0
   588
	if (r!=KErrNone)
sl@0
   589
		Kern::RequestComplete(requestThread,aRequestStatus,r);
sl@0
   590
	}
sl@0
   591
sl@0
   592
/**
sl@0
   593
  Signal Capture Image request completed
sl@0
   594
*/
sl@0
   595
void DCamera1Channel::CaptureImageCancel()
sl@0
   596
	{
sl@0
   597
	// Need to be in critical section so we don't die whilst owning the capture image request
sl@0
   598
	NKern::ThreadEnterCS();
sl@0
   599
sl@0
   600
	// Claim the capture image request
sl@0
   601
	NKern::FMWait(&iCaptureMutex);
sl@0
   602
	DThread* thread = iCaptureRequestThread;;
sl@0
   603
	TRequestStatus* status = iCaptureRequestStatus;
sl@0
   604
	iCaptureRequestStatus = NULL;
sl@0
   605
	NKern::FMSignal(&iCaptureMutex);
sl@0
   606
sl@0
   607
	// Signal completion
sl@0
   608
	if(status)
sl@0
   609
		{
sl@0
   610
		Kern::RequestComplete(thread,status,KErrCancel);
sl@0
   611
		thread->Close(0);
sl@0
   612
		}
sl@0
   613
sl@0
   614
	NKern::ThreadLeaveCS();
sl@0
   615
	}
sl@0
   616
sl@0
   617
/**
sl@0
   618
  DFC callback called when after a new image has been captured
sl@0
   619
  In this example code this is called by
sl@0
   620
*/
sl@0
   621
void DCamera1Channel::CaptureDfcTrampoline(TAny* aSelf)
sl@0
   622
	{
sl@0
   623
	// Just call non-static method
sl@0
   624
	((DCamera1Channel*)aSelf)->CaptureDfc();
sl@0
   625
	}
sl@0
   626
sl@0
   627
/**
sl@0
   628
  DFC callback called when a new image has been captured
sl@0
   629
*/
sl@0
   630
void DCamera1Channel::CaptureDfc()
sl@0
   631
	{
sl@0
   632
	TRACE(Kern::Printf(">DCamera1Channel::CaptureDfc\n");)
sl@0
   633
sl@0
   634
	NKern::FMWait(&iCaptureMutex);
sl@0
   635
sl@0
   636
	// Update image buffers state
sl@0
   637
	iCaptureBuffers->ImageCaptured();
sl@0
   638
sl@0
   639
	// Did client request an image and is one available?
sl@0
   640
	DImageBuffer* clientBuffer;
sl@0
   641
	if(iCaptureRequestStatus && (clientBuffer=iCaptureBuffers->ImageForClient())!=NULL )
sl@0
   642
		{
sl@0
   643
		// Claim the client request
sl@0
   644
		DThread* thread = iCaptureRequestThread;
sl@0
   645
		TRequestStatus* status = iCaptureRequestStatus;
sl@0
   646
		iCaptureRequestStatus = NULL;
sl@0
   647
sl@0
   648
		NKern::FMSignal(&iCaptureMutex);
sl@0
   649
sl@0
   650
		// We now own the client request but we don't have to worry about
sl@0
   651
		// being in a critical section because we are running in a DFC thread
sl@0
   652
		// which can't be killed
sl@0
   653
sl@0
   654
		// Complete client request with the chunk offset for a captured image
sl@0
   655
		// (We use AsyncClose() here because we are running in a high priority DFC and
sl@0
   656
		// don't want to take the penalty for possibly deleting a thread in this context.)
sl@0
   657
		Kern::RequestComplete(thread,status,clientBuffer->iChunkOffset);
sl@0
   658
		thread->AsyncClose();
sl@0
   659
		}
sl@0
   660
	else
sl@0
   661
		NKern::FMSignal(&iCaptureMutex);
sl@0
   662
sl@0
   663
	// Get camera hardware to capture next image
sl@0
   664
	DoNextCapture();
sl@0
   665
sl@0
   666
	TRACE(Kern::Printf("<DCamera1Channel::CaptureDfc\n");)
sl@0
   667
	}
sl@0
   668
sl@0
   669
/**
sl@0
   670
  Release a buffer which was being used by client
sl@0
   671
sl@0
   672
  @param aChunkOffset The chunk offset corresponding to the buffer to be freed
sl@0
   673
sl@0
   674
  @return KErrNone if successful.
sl@0
   675
		  KErrNotFound if no 'in use' buffer had the specified chunk offset.
sl@0
   676
*/
sl@0
   677
TInt DCamera1Channel::ImageRelease(TInt aChunkOffset)
sl@0
   678
	{
sl@0
   679
	// Need to be in critical section so we don't die whilst holding reference on buffers
sl@0
   680
	NKern::ThreadEnterCS();
sl@0
   681
sl@0
   682
	// Get reference to buffers object and find the buffer we want
sl@0
   683
	NKern::FMWait(&iCaptureMutex);
sl@0
   684
	DCaptureBuffers* buffers = iCaptureBuffers;
sl@0
   685
	DImageBuffer* buffer = NULL;
sl@0
   686
	if(buffers)
sl@0
   687
		{
sl@0
   688
		buffers->Open();
sl@0
   689
		buffer = buffers->InUseImage(aChunkOffset);
sl@0
   690
		}
sl@0
   691
	NKern::FMSignal(&iCaptureMutex);
sl@0
   692
sl@0
   693
	TInt r;
sl@0
   694
	if(!buffer)
sl@0
   695
		r = KErrNotFound;	// Buffer not found
sl@0
   696
	else
sl@0
   697
		{
sl@0
   698
		// Purge the CPU cache for the buffer.
sl@0
   699
		// Note, we don't do this whilst holding iCaptureMutex because it can
sl@0
   700
		// take a long time.
sl@0
   701
		// Also, it doesn't mater that e aren't holding the mutex because:
sl@0
   702
		// 1. The buffer can't be delete because we have a reference count on iCaptureBuffers
sl@0
   703
		// 2. Reentrancy of the Purge method is safe 
sl@0
   704
		buffers->Purge(buffer);
sl@0
   705
sl@0
   706
		// Release buffer (move it to the free list)
sl@0
   707
		NKern::FMWait(&iCaptureMutex);
sl@0
   708
		r = buffers->ImageRelease(aChunkOffset) ? KErrNone : KErrArgument;
sl@0
   709
		NKern::FMSignal(&iCaptureMutex);
sl@0
   710
		}
sl@0
   711
sl@0
   712
	// Close reference on buffers
sl@0
   713
	if(buffers)
sl@0
   714
		buffers->Close();
sl@0
   715
sl@0
   716
	NKern::ThreadLeaveCS();
sl@0
   717
sl@0
   718
	return r;
sl@0
   719
	}
sl@0
   720
sl@0
   721
//
sl@0
   722
// DCaptureBuffers
sl@0
   723
//
sl@0
   724
sl@0
   725
/**
sl@0
   726
  Construct a new set of buffers
sl@0
   727
sl@0
   728
  @param aNumBuffers Number of buffers
sl@0
   729
  @param aBufferSize Size of each buffer in bytes
sl@0
   730
sl@0
   731
  @return Pointer to the created DCaptureBuffers or NULL if the system ran out of memory
sl@0
   732
*/
sl@0
   733
DCaptureBuffers* DCaptureBuffers::New(TInt aNumBuffers,TInt aBufferSize)
sl@0
   734
	{
sl@0
   735
	DCaptureBuffers* buffers = new DCaptureBuffers;
sl@0
   736
	if(buffers)
sl@0
   737
		{
sl@0
   738
		TInt r = buffers->Create(aNumBuffers,aBufferSize);
sl@0
   739
		if(r==KErrNone)
sl@0
   740
			return buffers;
sl@0
   741
		delete buffers;
sl@0
   742
		// An error other than 'no memory' must be a programming error in the driver
sl@0
   743
		__NK_ASSERT_ALWAYS(r==KErrNoMemory);
sl@0
   744
		}
sl@0
   745
	return NULL;
sl@0
   746
	}
sl@0
   747
sl@0
   748
/**
sl@0
   749
  Construct with access count of one
sl@0
   750
*/
sl@0
   751
DCaptureBuffers::DCaptureBuffers()
sl@0
   752
	: iAccessCount(1)
sl@0
   753
	{
sl@0
   754
	}
sl@0
   755
sl@0
   756
/**
sl@0
   757
  Create all buffers and lists
sl@0
   758
*/
sl@0
   759
TInt DCaptureBuffers::Create(TInt aNumBuffers,TInt aBufferSize)
sl@0
   760
	{
sl@0
   761
	// Allocate buffer lists
sl@0
   762
	DImageBuffer** lists = (DImageBuffer**)Kern::AllocZ(3*aNumBuffers*sizeof(DImageBuffer*));
sl@0
   763
	if(!lists)
sl@0
   764
		return KErrNoMemory;
sl@0
   765
	iBufferLists = lists;
sl@0
   766
	iFreeBuffers = lists;
sl@0
   767
	iCompletedBuffers = lists+aNumBuffers;
sl@0
   768
	iInUseBuffers = lists+2*aNumBuffers;
sl@0
   769
sl@0
   770
	// Calculate sizes
sl@0
   771
	aBufferSize = Kern::RoundToPageSize(aBufferSize);
sl@0
   772
	TInt pageSize = Kern::RoundToPageSize(1);
sl@0
   773
	TUint64 chunkSize = TUint64(aBufferSize+pageSize)*aNumBuffers+pageSize;
sl@0
   774
	if(chunkSize>(TUint64)KMaxTInt)
sl@0
   775
		return KErrNoMemory;  // Need more than 2GB of memory!
sl@0
   776
sl@0
   777
	// Create chunk
sl@0
   778
	TChunkCreateInfo info;
sl@0
   779
	info.iType = TChunkCreateInfo::ESharedKernelMultiple;
sl@0
   780
	info.iMaxSize = (TInt)chunkSize;
sl@0
   781
#ifndef __WINS__
sl@0
   782
	info.iMapAttr = EMapAttrCachedMax;
sl@0
   783
#else
sl@0
   784
	info.iMapAttr = 0;
sl@0
   785
#endif
sl@0
   786
	info.iOwnsMemory = ETrue;
sl@0
   787
	TInt r = Kern::ChunkCreate(info,iChunk,iChunkBase,iChunkMapAttr);
sl@0
   788
	if(r!=KErrNone)
sl@0
   789
		return r;
sl@0
   790
sl@0
   791
	// Construct array of buffers
sl@0
   792
	iNumBuffers = aNumBuffers;
sl@0
   793
	iImageBuffer = new DImageBuffer[aNumBuffers];
sl@0
   794
	if(!iImageBuffer)
sl@0
   795
		return KErrNoMemory;
sl@0
   796
sl@0
   797
	// Create each buffer
sl@0
   798
	TInt offset = pageSize;
sl@0
   799
	while(aNumBuffers)
sl@0
   800
		{
sl@0
   801
		r = iImageBuffer[--aNumBuffers].Create(iChunk,offset,aBufferSize);
sl@0
   802
		if(r!=KErrNone)
sl@0
   803
			return r;
sl@0
   804
		offset += aBufferSize+pageSize;
sl@0
   805
		}
sl@0
   806
sl@0
   807
	return KErrNone;
sl@0
   808
	}
sl@0
   809
sl@0
   810
/**
sl@0
   811
  Destructor
sl@0
   812
*/
sl@0
   813
DCaptureBuffers::~DCaptureBuffers()
sl@0
   814
	{
sl@0
   815
	if(iChunk)
sl@0
   816
		Kern::ChunkClose(iChunk);
sl@0
   817
	delete [] iImageBuffer;
sl@0
   818
	Kern::Free(iBufferLists);
sl@0
   819
	}
sl@0
   820
sl@0
   821
/**
sl@0
   822
  Increment access count of buffers
sl@0
   823
*/
sl@0
   824
void DCaptureBuffers::Open()
sl@0
   825
	{
sl@0
   826
	__e32_atomic_tas_ord32(&iAccessCount, 1, 1, 0);
sl@0
   827
	}
sl@0
   828
sl@0
   829
/**
sl@0
   830
  Decrement access count of buffers.
sl@0
   831
  Deleting them if the count is decremented to zero.
sl@0
   832
*/
sl@0
   833
void DCaptureBuffers::Close()
sl@0
   834
	{
sl@0
   835
	__ASSERT_NO_FAST_MUTEX;
sl@0
   836
	__ASSERT_CRITICAL;
sl@0
   837
	if(__e32_atomic_tas_ord32(&iAccessCount, 1, -1, 0) == 1)
sl@0
   838
		AsyncDelete();
sl@0
   839
	}
sl@0
   840
sl@0
   841
/**
sl@0
   842
  Reset all image buffer lists to reflect the state at the start of image capture process
sl@0
   843
*/
sl@0
   844
void DCaptureBuffers::Reset()
sl@0
   845
	{
sl@0
   846
	// Purge cache for all buffers in use by client.
sl@0
   847
	DImageBuffer** list = iInUseBuffers;
sl@0
   848
	DImageBuffer* buffer;
sl@0
   849
	while((buffer=*list++)!=NULL)
sl@0
   850
		Purge(buffer);
sl@0
   851
sl@0
   852
	// Get pointers to first buffer
sl@0
   853
	buffer = iImageBuffer; 
sl@0
   854
sl@0
   855
	// Set buffers for current and next images
sl@0
   856
	iCurrentBuffer = buffer++;
sl@0
   857
	iNextBuffer = buffer++;
sl@0
   858
sl@0
   859
	// Add all other buffers to the free list
sl@0
   860
	DImageBuffer** free = iFreeBuffers;
sl@0
   861
	DImageBuffer* bufferLimit = iImageBuffer+iNumBuffers; 
sl@0
   862
	while(buffer<bufferLimit)
sl@0
   863
		*free++ = buffer++;
sl@0
   864
	*free = 0;
sl@0
   865
sl@0
   866
	// Start with no completed or used buffers
sl@0
   867
	iCompletedBuffers[0] = 0;
sl@0
   868
	iInUseBuffers[0] = 0;
sl@0
   869
	}
sl@0
   870
sl@0
   871
/**
sl@0
   872
  Purge cache for an image buffer.
sl@0
   873
  @param aBuffer The buffer.
sl@0
   874
*/
sl@0
   875
void DCaptureBuffers::Purge(DImageBuffer* aBuffer)
sl@0
   876
	{
sl@0
   877
	Cache::SyncMemoryBeforeDmaRead(iChunkBase+aBuffer->iChunkOffset,aBuffer->iSize,iChunkMapAttr);
sl@0
   878
	}
sl@0
   879
sl@0
   880
/**
sl@0
   881
  Remove an image buffer to the start of the given image list.
sl@0
   882
  @return A pointer to the image buffer or NULL if the list was empty
sl@0
   883
*/
sl@0
   884
DImageBuffer* DCaptureBuffers::Remove(DImageBuffer** aList)
sl@0
   885
	{
sl@0
   886
	DImageBuffer* buffer=aList[0];
sl@0
   887
	if(buffer)
sl@0
   888
		{
sl@0
   889
		DImageBuffer* b;
sl@0
   890
		do
sl@0
   891
			{
sl@0
   892
			b=aList[1];
sl@0
   893
			*aList++ = b;
sl@0
   894
			}
sl@0
   895
		while(b);
sl@0
   896
		}
sl@0
   897
	return buffer;
sl@0
   898
	}
sl@0
   899
sl@0
   900
/**
sl@0
   901
  Add an image buffer to the end of the given image list.
sl@0
   902
*/
sl@0
   903
DImageBuffer* DCaptureBuffers::Add(DImageBuffer** aList, DImageBuffer* aBuffer)
sl@0
   904
	{
sl@0
   905
	while(*aList) aList++;
sl@0
   906
	*aList = aBuffer;
sl@0
   907
	return aBuffer;
sl@0
   908
	}
sl@0
   909
sl@0
   910
/**
sl@0
   911
  Update buffer lists after an image has been captured.
sl@0
   912
  @return A pointer to the catptured image buffer
sl@0
   913
*/
sl@0
   914
DImageBuffer* DCaptureBuffers::ImageCaptured()
sl@0
   915
	{
sl@0
   916
	// Add captured image to completed list
sl@0
   917
	DImageBuffer* buffer = iCurrentBuffer;
sl@0
   918
	DCaptureBuffers::Add(iCompletedBuffers,buffer);
sl@0
   919
sl@0
   920
	// Make queued buffer the current one
sl@0
   921
	iCurrentBuffer = iNextBuffer;
sl@0
   922
sl@0
   923
	// Queue a new buffer
sl@0
   924
	iNextBuffer = DCaptureBuffers::Remove(iFreeBuffers);
sl@0
   925
	if(!iNextBuffer)
sl@0
   926
		iNextBuffer = DCaptureBuffers::Remove(iCompletedBuffers);
sl@0
   927
sl@0
   928
	TRACE(Kern::Printf("DCaptureBuffers::ImageCaptured  buf=%08x\n",buffer->iChunkOffset);)
sl@0
   929
sl@0
   930
	return buffer;
sl@0
   931
	}
sl@0
   932
sl@0
   933
/**
sl@0
   934
  Get the next image from the completed capture list and make it 'in use' by the client
sl@0
   935
sl@0
   936
  @return A pointer to the next completed image buffer
sl@0
   937
*/
sl@0
   938
DImageBuffer* DCaptureBuffers::ImageForClient()
sl@0
   939
	{
sl@0
   940
	DImageBuffer* buffer=Remove(iCompletedBuffers);
sl@0
   941
	if(buffer)
sl@0
   942
		DCaptureBuffers::Add(iInUseBuffers,buffer);
sl@0
   943
sl@0
   944
	TRACE(Kern::Printf("DCaptureBuffers::ImageForClient buf=%08x\n",buffer ? buffer->iChunkOffset : -1);)
sl@0
   945
sl@0
   946
	return buffer;
sl@0
   947
	}
sl@0
   948
sl@0
   949
/**
sl@0
   950
  Release (move to free list) the 'in use' image specified by the given chunk offset.
sl@0
   951
sl@0
   952
  @param aChunkOffset The chunk offset corresponding to the buffer to be freed
sl@0
   953
sl@0
   954
  @return The freed image buffer, or NULL if no 'in use' buffer had the specified chunk offset.
sl@0
   955
*/
sl@0
   956
DImageBuffer* DCaptureBuffers::ImageRelease(TInt aChunkOffset)
sl@0
   957
	{
sl@0
   958
	// Scan 'in use' list for the image buffer
sl@0
   959
	DImageBuffer** list = iInUseBuffers;
sl@0
   960
	DImageBuffer* buffer;
sl@0
   961
	while((buffer=*list++)!=NULL && buffer->iChunkOffset!=aChunkOffset)
sl@0
   962
		{};
sl@0
   963
sl@0
   964
	// Move buffer to the free list (if found)
sl@0
   965
	if(buffer)
sl@0
   966
		buffer = Add(iFreeBuffers,Remove(list-1));
sl@0
   967
sl@0
   968
	TRACE(Kern::Printf("DCaptureBuffers::ImageRelease   buf=%08x\n",buffer ? buffer->iChunkOffset : -1);)
sl@0
   969
sl@0
   970
	return buffer;
sl@0
   971
	}
sl@0
   972
sl@0
   973
/**
sl@0
   974
  Find the 'in use' image specified by the given chunk offset
sl@0
   975
sl@0
   976
  @param aChunkOffset The chunk offset corresponding to the buffer to be freed
sl@0
   977
sl@0
   978
  @return The image buffer, or NULL if no 'in use' buffer had the specified chunk offset
sl@0
   979
*/
sl@0
   980
DImageBuffer* DCaptureBuffers::InUseImage(TInt aChunkOffset)
sl@0
   981
	{
sl@0
   982
	// Scan 'in use' list for the image buffer
sl@0
   983
	DImageBuffer** list = iInUseBuffers;
sl@0
   984
	DImageBuffer* buffer;
sl@0
   985
	while((buffer=*list++)!=NULL && buffer->iChunkOffset!=aChunkOffset)
sl@0
   986
		{};
sl@0
   987
sl@0
   988
	return buffer;
sl@0
   989
	}
sl@0
   990
sl@0
   991
//
sl@0
   992
// DImageBuffer
sl@0
   993
//
sl@0
   994
sl@0
   995
/**
sl@0
   996
  Constructor clears all member data
sl@0
   997
*/
sl@0
   998
DImageBuffer::DImageBuffer()
sl@0
   999
	{
sl@0
  1000
	memclr(this,sizeof(*this));
sl@0
  1001
	}
sl@0
  1002
sl@0
  1003
/**
sl@0
  1004
  Commit memory for this buffer.
sl@0
  1005
sl@0
  1006
  @param aChunk  The chunk into which the memory is to be commited
sl@0
  1007
  @param aOffset The offset within aChunk for the start of the comitted memory.
sl@0
  1008
                 Must be a multiple of the MMU page size.
sl@0
  1009
  @param aSize   The number of bytes of memory to commit.
sl@0
  1010
                 Must be a multiple of the MMU page size.
sl@0
  1011
sl@0
  1012
  @return KErrNone if successful, otherwise one of the other system wide error codes.
sl@0
  1013
*/
sl@0
  1014
TInt DImageBuffer::Create(DChunk* aChunk, TInt aOffset, TInt aSize)
sl@0
  1015
	{
sl@0
  1016
	TInt r;
sl@0
  1017
sl@0
  1018
	// Initialise data
sl@0
  1019
	iChunkOffset = aOffset;
sl@0
  1020
	iSize = aSize;
sl@0
  1021
sl@0
  1022
	// Try for physically contiguous memory first
sl@0
  1023
	r = Kern::ChunkCommitContiguous(aChunk,aOffset,aSize,iPhysicalAddress);
sl@0
  1024
	if(r==KErrNone)
sl@0
  1025
		return r;
sl@0
  1026
sl@0
  1027
	// failed to get contiguous memory...
sl@0
  1028
sl@0
  1029
	// Mark physical address invalid
sl@0
  1030
	iPhysicalAddress = KPhysAddrInvalid;
sl@0
  1031
sl@0
  1032
	// Commit discontiguous memory
sl@0
  1033
	r = Kern::ChunkCommit(aChunk,aOffset,aSize);
sl@0
  1034
	if(r!=KErrNone)
sl@0
  1035
		return r;
sl@0
  1036
sl@0
  1037
	// Allocate array for list of physical pages
sl@0
  1038
	iPhysicalPages = new TPhysAddr[aSize/Kern::RoundToPageSize(1)];
sl@0
  1039
	if(!iPhysicalPages)
sl@0
  1040
		return KErrNoMemory;
sl@0
  1041
sl@0
  1042
	// Get physical addresses of pages in buffer
sl@0
  1043
	TUint32 kernAddr;
sl@0
  1044
	TUint32 mapAttr;
sl@0
  1045
	TPhysAddr physAddr;
sl@0
  1046
	r = Kern::ChunkPhysicalAddress(aChunk,aOffset,aSize,kernAddr,mapAttr,physAddr,iPhysicalPages);
sl@0
  1047
	// r = 0 or 1 on success. (1 meaning the physical pages are not-contiguous)
sl@0
  1048
	if(r>=0)
sl@0
  1049
		r = KErrNone;
sl@0
  1050
	return r;
sl@0
  1051
	}
sl@0
  1052
sl@0
  1053
/**
sl@0
  1054
  Destructor
sl@0
  1055
*/
sl@0
  1056
DImageBuffer::~DImageBuffer()
sl@0
  1057
	{
sl@0
  1058
	delete [] iPhysicalPages;
sl@0
  1059
	}
sl@0
  1060
sl@0
  1061
//
sl@0
  1062
// Program camera hardware
sl@0
  1063
//
sl@0
  1064
sl@0
  1065
/**
sl@0
  1066
  Initialise camera hardware to start capturing images
sl@0
  1067
  First buffer to fill is iCaptureBuffers->iCurrentBuffer.
sl@0
  1068
  Next buffer to fill will be iCaptureBuffers->iNextBuffer.
sl@0
  1069
*/
sl@0
  1070
void DCamera1Channel::DoStartCapture()
sl@0
  1071
	{
sl@0
  1072
	// For this example test...
sl@0
  1073
	
sl@0
  1074
	TRACE(Kern::Printf("DCamera1Channel::DoStartCapture buf=%08x cnt=%04d\n",iCaptureBuffers->iCurrentBuffer->iChunkOffset,iCaptureCounter);)
sl@0
  1075
sl@0
  1076
	// Initialise frame counter
sl@0
  1077
	iCaptureCounter = 0;
sl@0
  1078
sl@0
  1079
	// Put frame counter into current image buffer. (This is the 'image' data we capture).
sl@0
  1080
	*(TInt*)(iCaptureBuffers->iChunkBase+iCaptureBuffers->iCurrentBuffer->iChunkOffset) = iCaptureCounter++;
sl@0
  1081
sl@0
  1082
	// Start the timer
sl@0
  1083
	TInt r=iCaptureTimer.OneShot(iCaptureRateTicks,ETrue);
sl@0
  1084
	__NK_ASSERT_ALWAYS(r==KErrNone);
sl@0
  1085
	}
sl@0
  1086
sl@0
  1087
/**
sl@0
  1088
  Reset camera hardware to stop capturing images
sl@0
  1089
*/
sl@0
  1090
void DCamera1Channel::DoEndCapture()
sl@0
  1091
	{
sl@0
  1092
	// For this example test...
sl@0
  1093
sl@0
  1094
	TRACE(Kern::Printf("DCamera1Channel::DoEndCapture\n");)
sl@0
  1095
sl@0
  1096
	// Cancel the timer
sl@0
  1097
	iCaptureTimer.Cancel();
sl@0
  1098
	}
sl@0
  1099
sl@0
  1100
/**
sl@0
  1101
  Setup camera hardware to capture next image
sl@0
  1102
  Next buffer to fill will be iCaptureBuffers->iNextBuffer;
sl@0
  1103
sl@0
  1104
  @param aLastImage The last image just captured. I.e. the completed capture which caused
sl@0
  1105
					this method to be called
sl@0
  1106
*/
sl@0
  1107
void DCamera1Channel::DoNextCapture()
sl@0
  1108
	{
sl@0
  1109
	// For this example test...
sl@0
  1110
	
sl@0
  1111
	TRACE(Kern::Printf("DCamera1Channel::DoNextCapture  cur=%08x cnt=%04d nxt=%08x\n",iCaptureBuffers->iCurrentBuffer->iChunkOffset,iCaptureCounter,iCaptureBuffers->iNextBuffer->iChunkOffset);)
sl@0
  1112
sl@0
  1113
	// Put frame counter into current image buffer. (This is the 'image' data we capture).
sl@0
  1114
	*(TInt*)(iCaptureBuffers->iChunkBase+iCaptureBuffers->iCurrentBuffer->iChunkOffset) = iCaptureCounter++;
sl@0
  1115
sl@0
  1116
	// Restart the timer
sl@0
  1117
	TInt r = iCaptureTimer.Again(iCaptureRateTicks);
sl@0
  1118
	if(r==KErrArgument)
sl@0
  1119
		{
sl@0
  1120
		// Timer would have already expired.
sl@0
  1121
		//
sl@0
  1122
		// In a real device driver this is analogous to iCurrentBuffer already being filled
sl@0
  1123
		// and the DMA queue being emptied. I.e. we have missed some frames.
sl@0
  1124
		//
sl@0
  1125
		// For this test...
sl@0
  1126
sl@0
  1127
		TRACE(Kern::Printf("DCamera1Channel::DoNextCapture frame dropped cnt=%04d\n",iCaptureCounter);)
sl@0
  1128
sl@0
  1129
		// Skip a frame count
sl@0
  1130
		++iCaptureCounter;
sl@0
  1131
sl@0
  1132
		// Restart timer
sl@0
  1133
		r = iCaptureTimer.OneShot(iCaptureRateTicks,ETrue);
sl@0
  1134
		}
sl@0
  1135
	__NK_ASSERT_ALWAYS(r==KErrNone);
sl@0
  1136
	}
sl@0
  1137