PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Frame Buffer, DMA Question

Hi everyone,

I am planning on building a game for pocket pc, and have seen GAPI, PocketFrog, and a few other libraries. However, I am trying to understand how these libraries work. I have built a test app that can access the frame buffer (I think) at address 0x8451a000 on my iPaq. I got that address from the Microsoft GAPI dll.

What I want to understand though is where did that address come from - it seems that it can be anything depending on the device.

I have done heaps of searching around newsgroups and web pages and I have found a spot in the registry on my iPaq where I can get bpp and screen size but I'm not sure how portable that is. It also referenced ddi.dll which almost looked like the solution except I looked through all of the help on the MS site and none of the functions seem to return the address.

Using that address I do a VirtualAlloc, VirtualCopy to get a pointer to it and can write directly to what I think is the frame buffer. I have noticed discussions on this website about writing through DMA and there seems to be "direct access", and "backbuffer access" and I don't know how these fit together.

My little test app can draw a tile based map (tiles 32x32 pixels) and scroll around it at between 160fps and 200fps. This is by using memcpy, and having an offscreen bitmap the same size and bpp as the screen and drawing each tile to it, and then copying that whole bitmap to the frame buffer. I tried doing it pixel by pixel and it was heaps slower - about 50fps at best. I had a quick read of the ARM assembler commands and looked at the code produced by EC++4 and it seemed very inefficient from my limited knowledge, but I really don't want to go down to that level.

If anyone could shed some light on these topics it would be much appreciated.
j.edwards · 29 Oct 2003, 22:16 UTC — Example source code to return some fo the values
I have been experimenting and found a couple of almost useful functions...

Example 1:

Code:

HDC desktop = GetDC(0);
int ScreenWidth = GetDeviceCaps(desktop, HORZRES);
int ScreenHeight = GetDeviceCaps(desktop, VERTRES);
int BPP = GetDeviceCaps(desktop, BITSPIXEL);


Example 2:

Code:

BOOL bSuccess;
DISPLAY_DEVICE dd;
ZeroMemory(&dd, sizeof(dd));
//assume 1 display
dd.cb = sizeof(dd);
bSuccess = EnumDisplayDevices(NULL, 0, &dd, 0);
if (bSuccess)
{
//now try get settings for device
DEVMODE dm;
ZeroMemory(&dm, sizeof(dm));
dm.dmSize = sizeof(dm);
bSuccess = EnumDisplaySettings(dd.DeviceName, 0, &dm);

int ScreenWidth = dm.dmPelsWidth;
int ScreenHeight = dm.dmPelsHeight;
int BPP = dm.dmBitsPerPel;
}


The first method is quick and easy, but not sure how portable it will be especially considering it depends on the desktop screen resolution which could possible differ from the actual hardware configuration. I would imagine the second method is the better way to go. It can also return the vertical refresh frequency (although the MS documentation says it's unsupported, it seems my iPaq driver has implemented it).

One interesting thing which I picked up is the dmDeviceName is "GPE". I have run into GPE during my searches and think its a couple of classes MS have released to help with driver development. The other interesting thing is the DeviceName field for the device 0 (display) is "ddi.dll" - it may be possible to use functions from that dll, but from my research so far it seems none of the available functions can get the info I'm after.

...Still can't find the frame buffer address etc, as well as the configuration of the pixels eg. 565, 555.
j.edwards · 29 Oct 2003, 23:46 UTC — 565 555 bit format
I just found this:

http://msdn.microsoft.com/library/en-us/wceddk40/html/cxrefgdiinfo.asp?frame=true

It seems to be able to get the number of bits per pixel for each color (ulDACRed etc). However, it's not supported on CE according to the MS documentation.

Here is the same structure for "normal" windows:

http://msdn.microsoft.com/library/en-us/graphics/hh/graphics/grstrcts_2k9z.asp?frame=true

Any ideas anyone?
j.edwards · 30 Oct 2003, 01:57 UTC — I think I'm getting somewhere!
http://msdn.microsoft.com/library/en-us/wceddk40/html/_wceddk_display_driver_escape_codes.asp?frame=true

Not sure if this is accessible from user mode (as opposed to kernel mode) yet, but will do some tests.

According to the following this may not be the way to go though:

http://forums.projectmayo.com/viewtopic.php?topic=6187&forum=23

There is no API under HPC2000 that provides you all the information about the frame buffer (i.e. frame address buffer, size, pixel format, rgb masks, h and v pitch).

So basically you need to detect the device model and hard-code the frame buffer information for each model.

On some devices, the calls to ExtEspace with GETVFRAMEPHYSICAL and GETVFRAMELEN are implemented, but they are not sufficient.


Is there a list somewhere of all of the frame buffer addresses for each device. I don't really want to purchase every device to get it's address :( Maybe I could just go into a retail store with an sd card that has a little "detect" app and run through all of the models. It doesn't seem very sensible though as new models could have any address and you will need to re-release the app each time a new model needs to be supported.

Help!
I just discovered this interesting bit of info:


Port-Index: 0ch Port: 03d4h, 03b4h
usage: Start address high
------------------------------------------------------------
Port-Index: 0dh Port: 03d4h, 03b4h
usage: Start address low
Description: Determine the offset in display memory to be
displayed on the upper-left corner on the screen


Now does anyone know how to output to a port?
Just wanted to comment about the port's.. That is for PC's when you are doing normal VGA graphics. So no use on PocketPC.

-Sam
j.edwards: GapiDraw uses the frame buffer address as returned by GAPI (gx.dll). As proven by Thierry (PocketFrog author) performance can be improved by bypassing GAPI and accessing the display buffer directly, using ARM-specific optimizations.

I do not know how this is done, and neither do most people here on the forum. Thierry has previously stated that he cannot pass on any details on his implementation, so asking him will probably not help as well.

If you find out any details however, please do feel free to post them here...

Thanks...
Thanks Johan. When you say "bypassing GAPI and accessing the display buffer directly" - I thought that's what I was doing by VirtualCopy, VirtualAlloc on the frame buffer address. Is that so? Here is some code for what I'm doing at the moment:

Code:

//setting up pointer to frame buffer
FrameBuffer = 0x0451a000;

SYSTEM_INFO stInfo;
GetSystemInfo(&stInfo);

PageSize = stInfo.dwPageSize;

TotalBytes = 240 * 320 * 2;

DWORD dwSize = TotalBytes;
dwSize += PageSize - 1;
dwSize &= 0xFFFFFFFF - (PageSize - 1);

ptr = (PPixel16bit) VirtualAlloc(0, dwSize, MEM_RESERVE, PAGE_NOACCESS);
if (ptr)
{
//ok
DWORD p2;
p2 = 0xB0000000 | FrameBuffer;
BOOL b = VirtualCopy(
(LPVOID) ptr,
(LPVOID) p2,
dwSize,
PAGE_READWRITE | PAGE_NOCACHE
);
if (!b)
{
//err
}
}

//example of rendering 16bit bitmap to screen
//having same dimensions as display
void RenderFullScreen(TSurface* s)
{
//assume s is same dimensions as screen
PPixel16bit pSrc;
register PPixel16bit pDest;
pSrc = s->_bits;
pDest = (PPixel16bit) ptr;

memcpy(pDest, pSrc, TotalBytes);
}



Using the above code I can copy a bitmap the same size as the display to the display in 3 to 4 ms (about 300 fps). Is there a way to acheive better speed? When I had a loop that copied a pixel at a time it took way longer so I don't really know of a good solution for rotating a bitmap at an arbitrary angle because that will need to step through pixel at a time.

Also, "gx.dll" - is that the 8kb little dll that MS supply?

Are you a developer for the GAPI library developed by Viktoria Institute?
Thanks Sam. From what I've seen so far there doesn't appear to be very much difference at all between the hardware on the pocket pcs and normal pcs. I really wouldn't be surprised if those port numbers worked, however it may be at the discretion of the OEM to support them in which case it probably won't be much help as from what I've read not many of them pay close attention to MS documentation in other areas.

Only trouble is how do you do an "out dx, al" sort of port access from CE? I think mapping ports is the same as mapping memory but really need some help here.
Hey Johan ... I was just doing some research on Thierry and I think he's Italian - do you know what sort of wine he likes? :wink:

I also found this which is most likely from him (unless there are a couple of Thierry Tremblay's around)


I am trying to find some efficient algorithm for bullet/moving OBB collision
detection.
Dividing the time step is not really efficient. Any pointers? Couln't find
anything with Google.
The OBB movement includes both a rotation and a translation.

Thanks,
Thierry


See - everyone needs help at some stage, and I think we should all get together to encourage Thierry to be more forthcoming.

PS. I just read up on your input into GapiDraw - sorry about stupid question before :)
I always thought GAPI provided direct framebuffer access; that that was the point of using GAPI. This isn't true?
j.edwards: Interesting information, really!

The GapiDraw implementation for the final blit to display is actually quite simplistic. Get the framebuffer access from the gx.dll library and then simply call memcpy from back buffer stored in system memory to that address. If the yPitch of the display does not match source surface yPitch (e.g. Casio E115) each row is copied separately using memcpy. Performance with this approach is around 4.5ms for a full display on the ipaq.

Apparantly Thierry Tremblay found out a quicker way to copy the back buffer in system memory to the display. See the following link for info:
http://pockethal.droneship.com/features.html

2.1ms for the final blit is quite good. I haven't tried the VirtualAlloc / VirtualCopy approach you suggested, but it looks as if it should be faster than a simple memcpy (basically since the display is non-cached, memcpy will try to cache anyways which will only take time I guess).
j.edwards:

Umm..

My little test app can draw a tile based map (tiles 32x32 pixels) and scroll around it at between 160fps and 200fps. This is by using memcpy, and having an offscreen bitmap the same size and bpp as the screen and drawing each tile to it, and then copying that whole bitmap to the frame buffer.


Using the above code I can copy a bitmap the same size as the display to the display in 3 to 4 ms (about 300 fps).


So performance increased from 200 to 300 fps simply by using VirtualCopy disabling the write cache instead of memcpy? Wow! If that's the case then I guess I have to send you a beer or something!
Johan, to clarify:

If I have an offscreen bitmap the same dimensions as the display (and same bpp) then it takes 3ms to copy to the screen buffer using VirtualCopy, VirtualAlloc. However, I also tested with a tile based map. The map was 24x24 tiles, with each tile being 32x32 pixels. This map was then scrolled around randomly - ie. scroll to an edge then reverse direction etc. So to draw approx 75 tiles (with clipping as scrolling was by single pixel) into the offscreen bitmap, and then copy the bitmap to the screen it was taking between 4 and 5 ms. This seems to indicate that copy data to the frame buffer is considerable slower than working in "normal" ram. So the lesson I got from this is that everything should be built in a memory bitmap the same dimensions as the screen and then one simple memcpy to blit the whole lot. Also, my code for copying the tiles used memcpy per line as you mentioned.

I don't think there would be any difference from using the address gapi returns compared to VirtualAlloc/Copy. Wouldn't be surprised if gapi does the same thing.

From my understanding I think any address at 0x80000000 is always cached (or buffered, but not sure of the difference), whereas the range 0xa0000000 is not supposed to be, but not sure if that applies to pocket pcs, as I haven't got it working on that range. The more I am finding out the more I realise how little I know about these things. Are there any electonic engineers out there who can help? :cry:

Datasheet on the MQ1178:
http://www.mediaq.com/dynamic/downloads/download_file_54.pdf

- 2 window buffers
- 256kb sram
- usb (not sure why it would have it)
- absolute minimum time to transfer 240x320x2 bytes of data through it's bus is 0.3ms - there's a goal to work towards :D
- the interesting thing is it supports hardware accelarated rectangular source-copy block transfers, raster operations, line draw, pattern and area fill, clipping, and transparency

I haven't looked up any others yet, but I bet that is how Thierry is getting better than "normal" speeds - probably taking advantage of the hardware acceleration.

Can anyone explain the whole frame buffer, display memory, virtual mapping, etc etc architecture? as I'm really starting to get confused now. If I could understand what all of the terms referred to exactly it would probably really accelerate my learning of all of this stuff.
Hi!

I'm really not an expert, but here is a few things that I understand and that may help someone.

A frame buffer is an area of memory, preferably held in video memory, that holds the content of the screen. This is usually what is displayed on the device screen when you look at it.

Video memory is independant memory used to hold one (or more) frame buffer. If more than one frame buffer can exists in video memory (or system memory) they are called back buffers. Using a back buffer in addition to the main (primary) frame buffer allows for double buffering.

Virtual mapping is a way to assign a range of memory address to a specific resource. For example, in Win32 you can open a file using CreateFile() and then have the content of the file mapped to a memory range that you can access by pointers instead of reading from the file directly. I assume the same principle apply to other things.

BTW, if the "absolute minimun time to transfer 240x320x2" is 0.3 ms, since the screen is 16 bits you would have to multiply that time by 8, so the absolute minimal time to copy a screen would be 2.4 ms. Isn't this correct?

BTW #2, Thierry Tremblay is more of a french Canadian name than italian ;) . Since we've been known as "frogs" to some other countries, this may well be the origin of Thierry's name for PocketFrog (just a wild guess).

--Eric
j.edwards: Actually, 2.1 ms is achieved on standard StrongARM IPAQ devices such as the 36xx and the 38xx series.

The MediaQ chips aren't worth looking into.. Do a search on the forum and you will find lots of people actually using the HW acceleration features of the MQ with close to no performance improvement...
efortier: Thanks for the info.

The 0.3ms had already factored in 2 bytes per pixel.
Johan: That page about PocketHAL compared "flip times" between GAPI and PocketHAL...

http://pockethal.droneship.com/features.html

1) Does GAPI mean GapiDraw?
2) Is "flip time" to flip between back buffer and front buffer?
3) Why does both GAPI and PocketHAL take so long on the iPaq, ie 17ms? I have an iPaq 1940 and can do it in 3ms etc and thought the 2210 was a few models up from mine.
4) You say an iPaq should be able to do it in 2.1ms, why doesn't that relate to any of the figures on that webpage?

The memory on the MediaQ is only 256kb which is enough to hold only 1 full screen display worth. How is it possible to have a backbuffer anywhere else but in normal ram?

PocketHAL says "Buffered access to the display. Blitting from the backbuffer to the frontbuffer is blazing fast."

However, to get data onto the backbuffer it would be just as slow as writing to the frontbuffer wouldn't it? In which case how is this an advantage if you have the additional overhead of having to copy from the backbuffer to the frontbuffer? Wouldn't it be better to build everything in ram then do one copy to the display memory?

PocketHAL says "Synchronization with the VSYNC signal to eliminate tearing (available for most devices)." - there must be a way to talk directly to the display ports as I mentioned in a previous post as this is the only way to get hands on VSYNC info. If PocketHAL can do that then it can also read the frame buffer address from the display io ports as well which explains why it doesn't need to rely on gx.dll.

Support for GDI surfaces is no worries...probably just using CreateDIBSection etc, but wonder how he can transpose to any other pixel format without speed loss.

Another thing...you have used speeds like "2.1ms" - how is it you measure in speeds less than 1ms? I am using GetTickCount().
Actually I guess I shouldn't answer questins related to PocketHAL so let's see if Thierry can answer these questions when he gets back. I can answer some though...

1) Does GAPI mean GapiDraw?


I believe that "GAPI" in the benchmark means using the address returned by gx.dll to perform a full 240x320x2 memcpy from buffer in system memory to that address.

2) Is "flip time" to flip between back buffer and front buffer?


I believe that the "flip time" is the time to move data from the buffer in system memory to the framebuffer.

3) Why does both GAPI and PocketHAL take so long on the iPaq, ie 17ms? I have an iPaq 1940 and can do it in 3ms etc and thought the 2210 was a few models up from mine.


The 2210 uses a MediaQ chip with extremely low bandwidth. Unfortunately this can not be improved in any way.

4) You say an iPaq should be able to do it in 2.1ms, why doesn't that relate to any of the figures on that webpage?


Thierry released a small sample a while ago that I tested on my 38xx. The Audiovox Maestro is simply an Ipaq 36xx in an OEM form.

The memory on the MediaQ is only 256kb which is enough to hold only 1 full screen display worth. How is it possible to have a backbuffer anywhere else but in normal ram?


You can have the backbuffer in system memory (similar to DirectDraw).

PocketHAL says "Buffered access to the display. Blitting from the backbuffer to the frontbuffer is blazing fast."
However, to get data onto the backbuffer it would be just as slow as writing to the frontbuffer wouldn't it? In which case how is this an advantage if you have the additional overhead of having to copy from the backbuffer to the frontbuffer? Wouldn't it be better to build everything in ram then do one copy to the display memory?


Yes, that's basically what is done as I understand it.

PocketHAL says "Synchronization with the VSYNC signal to eliminate tearing (available for most devices)." - there must be a way to talk directly to the display ports as I mentioned in a previous post as this is the only way to get hands on VSYNC info. If PocketHAL can do that then it can also read the frame buffer address from the display io ports as well which explains why it doesn't need to rely on gx.dll.


Probably.. I have no clue on how to do this though...

Support for GDI surfaces is no worries...probably just using CreateDIBSection etc, but wonder how he can transpose to any other pixel format without speed loss.


PocketHAL doesn't. There are also issues with rotated displays when using GDI in PocketHAL. GDI in PocketHAL as I understand it is mostly for test, debug, and fallback purposes.

Another thing...you have used speeds like "2.1ms" - how is it you measure in speeds less than 1ms? I am using GetTickCount().


Simply call 100 blits from backbuffer to the framebuffer and divide that time by 100. :)
j.edwards wrote:
Hey Johan ... I was just doing some research on Thierry and I think he's Italian - do you know what sort of wine he likes? :wink:


Nope, I'm French-Canadian. Dunno where you got this Italian idea =) Thierry is really a common name in France, and less so in Canada.

I love red wine. Anything that isn't sweet.
Maybe you're referring to Gersen (Hekkus Sound System). He's Italian, I guess.


Welcome back Thierry ;-)
denthorg: You are right. I got a bit confused in my quest for answers. Apologies Thierry. Anyhow, not sure if wine is required anymore as I am starting to understand things and realise there is no point trying to redo everything Thierry has done with PocketHAL.

I have found it quite a learning curve though. A few weeks ago I began learning C++, and was therefore focused on speed to see just how well you could get things performing. I have found both the emulator and debugging on the iPaq really slow and tedious so am now building solely on the PC using MS VC++ using the same code in most cases, but with compatible classes for iPaq where there are differences - such as graphics - and occasionaly testing on iPaq. This has been going really well.

The game I am building is a RTS variant and I will publish it in the forums once a beta is ready. I also plan to publish code for some of the algorithms such as pathfinding and AI (for 2 not entirely unselfish reasons: because I've benefited hugely from other's sample code, and because there will no doubt be people who could help optimise them).

Must say, this is a really good developer community. Who actually runs the website?
I do. BTW your first payment is due, I sent an invoice to your account. Also, no more flaming or I'll ban you.
mlepage: Are you just being smart? No idea what you're talking about. Care to explain?
This forum's administrator is Johan Sanneblad, the originator of GapiDraw.
Just a joke, I'm a bit ill today and likely exercising bad judgement. I should have added a smiley.

My understanding is that the site itself is run by a couple of individuals, and that others run individual forums. I believe it says at the top of each forum who moderates it.
mlepage: I was going to say! I mistakenly identified Thierry as Italian instead of French-Canadian - didn't realise the emotion such a "flame" could stir. :)
I thought the fact that I was making random ludicrous statements was fairly clear. :-)

The aspirin is kicking in now so I am going to bed.

BTW all these frame buffer gritty details are currently beyond me but it's comforting to know that you guys are sorting it all out. I had thought that GAPI provided direct framebuffer access and that was it, but it seems there's much more to the story than that.