Again with doubts on Xlib

Hi,
Few days back I had mailed wrt some doubts regarding the SDL over X11. I
did some analysis and back again with few more queries.

My requirement is to port a library mainly used for gaming over a embedded
linux platform, which has X11, SDL, directfb support. After going through
many docs on X11, directfb, SDL I am again in confused state. Sorry for
being stupid.

My first doubt: Is Xserver running in user space? if so can it map the video
memory in it address space? If not, how does it copy the video data to video
memory? which driver is used by the Xserver to access the video memory? In
normal X application, the video data is copied twice:

  1. From application to Xserver memory.
  2. Xserver to video memory. Am I correct here?

DirectFB claims its a library which gives direct access to video memory with
hardware acceleration, using the linux framebuffer driver. There is also a X
server (XdirectFb) based on Directfb library, which avoids the X
application’s data to go through some IPCs like sockets or SHM. Instead it
gives the application to map the video memory in its address space, and thus
directly allow it to manipulate the screen. Thereby it claims, it avoids the
context switch between the application and Xserver processes.

Also there is a X extension called DGA ( another one DRI) which claims it
gives direct access to video memory for the X applications. In what way is
it different to Directfb? which driver is it using to give the direct video
memory access. Is the video memory mapped to user space in this setup just
like the directfb?

In the SHM enabled X server, the applications data doesnt go through the
network layer, instead the data is shared between the application and the
Xserver through shared memory. Is it possible to get direct access to video
memory here. Can the video memory be shared to application in this setup?

Sorry for writing this big mail. If I can get a link to some nice document
which hits all my questions , it would be of great help. I am getting many
docs, which are all non converging and are deviated with its own merits over
the others.

Thanks in advance,
Regards,
Pavan**

My first doubt: Is Xserver running in user space? if so can it map the video
memory in it address space? If not, how does it copy the video data to video
memory?

Xservers normally are user-mode programs (IE, not kernel processes), but run
as Root, meaning they have essentially as much hardware access as they like,
just like the kernel does. It may sound strange or wrong, but it’s relatively
practical it seems.

which driver is used by the Xserver to access the video memory?

AFAIR, the servers normally just access it directly like any other memory, but
have their own internal “drivers” to deal with different cards having different
control registers and features and video memory layouts and whatever else. I
don’t know much about this side of things though, so if someone else says
otherwise they probably know better than me! :smiley:

There are also framebuffer-based X-servers that go through the kernel’s /dev/fb
drivers, but these AFAIK do not give any acceleration, and most X-servers don’t
use the kernel framebuffer drivers at all(?).

In normal X application, the video data is copied twice:

  1. From application to Xserver memory.
  2. Xserver to video memory. Am I correct here?

That sounds correct from my memory of Xlib programming (I never did too much
of that though, and I much prefer SDL for most things now- although it’s not
able to give access to X’s line-drawing functions etc). There’s also the
MIT-XSHM or whichever extension, but I see you’ve already heard of that. That
IIRC cuts out step 1 above, but not step 2. And as you mention next, there
is also DGA which cuts out both steps AFAIK.

Also there is a X extension called DGA ( another one DRI) which claims it
gives direct access to video memory for the X applications. In what way is
it different to Directfb?

You would use DirectFB with programs running at the console. You must not
use it under X, because DirectFB’s access to the video card would clash with
the X-server’s. I’m not sure if it’s even possible to, maybe one would stop
the other from running, but otherwise you could expect crashes or at least
screen corruption. The DGA and DRI extensions OTOH should not cause problems
with the X-server (assuming you don’t write something so buggy that it causes
a crash; I have encountered SDL programs that have locked my machine solid
when they crashed, so I presume that’s possible). And of course you can’t
use DGA outside of X.

At least, that is my understanding of DirectFB; there could be some
cleverness it carries out that somehow makes things all work fine, but I
would presume otherwise that it would clash with X. Probably even the
DirectFB X-server would clash with other DirectFB programs.

In the SHM enabled X server, the applications data doesnt go through the
network layer, instead the data is shared between the application and the
Xserver through shared memory. Is it possible to get direct access to video
memory here. Can the video memory be shared to application in this setup?

AFAIK the SHM part only gives access to the X-server’s buffers, and you would
need DGA to access video memory directly. Or just use SDL. What would be wrong
with that? SDL can be set up to use X11 with or without DGA, and DirectFB-
I have run some of my programs from the console, although it needed an
environment variable to be set in order to choose which one. It may have
needed another to select the mouse, I don’t remember.

Actually I guess I may have misunderstood: are you porting this library
from embedded Linux, or to embedded Linux? What is the other platform
in the port?

BTW: To use shared memory buffers on X-servers with the feature, your program
must use the special X-SHM extension functions, it doesn’t just happen
automatically with normal X-lib code! Unless they’ve changed something in the
past few years. Yet another reason why SDL makes things so much easier… Sam
et al get to worry about all this awful stuff, and we can just concentrate on
the higher-level code for our own projects.

Hope this helped a bit, sorry I’m no expert. If I got anything wrong, please
feel free to correct me, anyone.
-Tom Barnes-LawrenceOn Wed, 23 May 2007 12:54:02 +0530 “Pavan Krishnamurthy” <2pavan at gmail.com> wrote:

I think I need a new signature

My requirement is to port a library mainly used for gaming over a embedded
linux platform, which has X11, SDL, directfb support. After going through
many docs on X11, directfb, SDL I am again in confused state. Sorry for
being stupid.

My first doubt: Is Xserver running in user space?

Yes.

if so can it map the video memory in it address space?

Yes, Linux drivers can support mmaping. However, much more important and
tricky are the registers that control the video adapter. Normally, access to
those would be exclusive to the kernel, but unfortunately it isn’t in
reality. I’d take a look at http://kerneltrap.org/node/8242 and
http://www.kgi-project.org/, maybe that clears up some confusion. The first
rather describes the current state while the second rather describes the way
it should be.

If not, how does it copy the video data to video memory?

There is still the framebuffer interface, which is an alternate way of using
X, but I think that interface also at least partially relies on memory
mapping.

which driver is used by the Xserver to access the video memory?

X.org has builtin drivers for quite some video adapters. Otherwise, it can
also use the Linux framebuffer driver.

In normal X application, the video data is copied twice:

  1. From application to Xserver memory.
  2. Xserver to video memory. Am I correct here?

I’m not sure, and it probably also depends on the way X is accessing the video
adapter. Possibly, you would have to copy it a third time if your X server
isn’t allowed to access the video memory directly.

DirectFB claims its a library which gives direct access to video memory
with hardware acceleration, using the linux framebuffer driver. There is
also a X server (XdirectFb) based on Directfb library, which avoids the X
application’s data to go through some IPCs like sockets or SHM. Instead it
gives the application to map the video memory in its address space, and
thus directly allow it to manipulate the screen. Thereby it claims, it
avoids the context switch between the application and Xserver processes.

Also there is a X extension called DGA ( another one DRI) which claims it
gives direct access to video memory for the X applications. In what way is
it different to Directfb? which driver is it using to give the direct video
memory access. Is the video memory mapped to user space in this setup just
like the directfb?

If and how all this works also depends on the system. If all you have is a
framebuffer, i.e. if you don’t have a GPU that can do blitting or even
rendering for you, there is pretty little to accelerate.

In the SHM enabled X server, the applications data doesnt go through the
network layer, instead the data is shared between the application and the
Xserver through shared memory. Is it possible to get direct access to video
memory here. Can the video memory be shared to application in this setup?

Again, it depends on whether X has access. Without it, there’s no way for it
to grant access. However, generally it doesn’t and shouldn’t give away video
memory to applications, because the whole idea behind that is to go through
the X protocol in order to be a) network transparent and b) portable and
hardware-independent. Further, I think it can be used asynchronously.

Two more points: SDL works as a layer on top of X11 or directfb. If you use
SDL, you can decide at runtime which driver to use. If X11 doesn’t work well
enough, you can switch to a different VT and use the framebuffer driver
there.

Then, asking questions about the internals of X (which implementation, btw,
X.org? XFree86? Exceed?) here is rather off-topic. I’d try on the Usenet or
maybe a dedicated X mailinglist instead.

UliOn Wednesday 23 May 2007 09:24:02 Pavan Krishnamurthy wrote:

My first doubt: Is Xserver running in user space? if so can it map the
video memory in it address space? If not, how does it copy the video

It runs in user space as root, and talks to hardware (using ioperm() or
iopl()). In some cases, it talks to a kernel driver that talks to hardware.

data to video memory? which driver is used by the Xserver to access the
video memory? In normal X application, the video data is copied twice:

  1. From application to Xserver memory.
  2. Xserver to video memory. Am I correct here?

Sometimes, yes. On most X servers, you’re writing to shared memory,
though, so step #1 is eliminated; you write to where the Xserver wants
it without an extra copy.

Frequently the copy from the X server to video memory is a DMA
operation, not like a real memcpy().

–ryan.