[P3400 ROM Development] krazy_about_technology:
Dear Friends,

I want to raise your attention towards an issue that all of us have faced with Windows mobile 6.5 ROMs. The issue is, When we use CameraLoader in a ROM, a time comes when on pressing the camera button or using the start menu icon, we get an error which says that there was an error loading the device drivers\builtin\camera. Its particularly a nasty error and it only goes after we restart our devices. I dunno about you guys but i do use my camera occassionally and when it gives me that error at the time when i need it the most, it really annoys me coz rebooting takes a long time in 6.5.

I digged into this problem today and heres what i have found :

The camera works using two driver files, S5K3BAFX.dll, which is registered as a builtin device driver in registry at HKEY_LOCAL_MACHINE\Drivers\Builtin\Camera with device name CAM0 and CameraDriver.dll, which is registered as a standard Windows Imaging Capture Device at HKEY_LOCAL_MACHINE\Drivers\Builtin\CameraDriver with device name CAP0. Now we have a 2 Megapixel camera which supports a maximum resolution of 1600x1200 which translates to 1920000 pixels. When a picture is taken, it is stored in ram, before saving it to permanent memory. This place in RAM is known as a Buffer. In RAM, Each pixel is represented as a triplet of 3 bytes, one each for Red, Green and Blue color component of the pixel.

For those who dont know, Computers and other digital systems use RGB system to represent colors where each components can have a value from 0-255 which means we have 16777216 colors in total or 16.7 million colors roughly or what is known as 24 bit color. The name 24bit is also because 0-255 is also the range of values that can be stored in 1 byte (8 bits) of memory so 1 byte each will become 8x3=24bits :)

Now we have total of 1920000 pixels in a photo each of which requires 3 bytes of memory so a total of 5760000 bytes of memory is needed to store a single photo of 2 MP. If you divide 1920000 twice by 1024 and multiply it by 3 you get 3 MB which is the amount of memory CameraDriver.dll reserves in memory all the time for storing photographs in between. Thats why on using CameraLoader we get 3 MB extra RAM free. The trick that Camera Loader uses is that it sets up the value of FLags setting in registry for both Camera driver keys to 1 which causes them to not load on System startup. When we press the camera key, shortcut to CameraLoader.exe gets it running and it loads the Camera by a call to standard Windows API function

HINSTANCE LoadDriver(LPCWSTR lpszFileName);

Which takes name of a system driver and loads it on demand, and returns a handle to the loaded instance. It then unloads the camera when we close it by calling another API function

BOOL FreeLibrary( HMODULE hLibModule);

by passing it the HINSTANCE that it got earlier as a return value from LoadDriver().

Now this is wonderful, and works well but unfortunately there is something bad that happens due to the design strategy of Windows Mobile. This is a bit more tehnical. If some of you have read the first some posts of the DA_G's thread where he posts latest Windows Mobile releases, you must be knowing the Slot Based Virtual Memory structure of Windows. The problem occurs when we load a program or two which load a heavy dll in memory. This dll gets cached in memory and eventually, SLOT 0 becomes so full that it doesn't have 3 MB memory free. And we do not yet have any method to force windows to unload these DLLs. Now when this happens and we try to load the camera, the call to LoadDriver() fails as the CameraDriver.dll on its initialization tries to capture or allocate 3 MB of memory for its use. Since its allocation fails, it flags a non-successful loading and unloads itself and hence we get that nasty error. Restarting the phone freshens up the RAM again and everything runs fine again.

Now all the tools that we have that free up ram like Hibernate and others dont empty SLOT0 really. For Example, what Hibernate.exe really does is it uses the SendMessage() API function to send the WM_HIBERNATE message to all programs running in system using the special window handle HWND_BROADCAST. For a well written app, this is a signal that system is going to hibernate and it should free all its unused resources and data structures. But it is totally up to the app to do so. Most apps behave right but you never know. And it really doesn't do anything for the cached dlls. So thats why even though hibernate frees up memory, SLOT0 is not having the required free memory yet and hence the problem remains.

Now what we have to do is to find some way to release this mess from SLOT0 without rebooting. I am working on this and will try to get some help from our senior members like Da_G. I appeal all of you who have knowledge in this area to try and do something yourself also in this direction. Also, if possible please try to find out who is the writer of the original Camera Loader application, we may need him.

Thank You.

Issues in Public Interest by Krazy_About_Technology :D