1

So I found this intresting file (ref to it I found in here). It is sad

Also chech out glGrab which uses OpenGL to grab the screen and is very fast.

so I wonder can we grab desctop screen frames via openGl on Windows and Linux using some openGL wrapper like SDL?

Community
  • 1
  • 1
Rella
  • 65,003
  • 109
  • 363
  • 636

1 Answers1

2

OpenGL can (easily, fast, and in a straightforward way) grab the front/back buffers that it owns and that you have a valid context for.

In other words: no.

The desktop is not owned by OpenGL. Under Windows, it is managed by the driver under pre-Vista, and by the window manager under Vista/7. You'll need the BitBlt function here, which is neither portable, nor fast.
Under Linux, the desktop may at least sometimes indeed be owned by OpenGL (compositing window managers), but you don't have a context handle for that.

If you can lessen your requirements from "Desktop" to "my window's content", then it all becomes super easy. In the simplest case, it's one function call, and if you want to do it asynchronously with DMA, it's 3-4 more.

Damon
  • 67,688
  • 20
  • 135
  • 185
  • And what will happen if we'd create transparent fullscreen window? will trick work? – Rella Jul 08 '11 at 12:16
  • No. If you create a transparent fullscreen window, this window will have its own front/back buffers, which will appear over the desktop if you draw to them. But although it _looks like it_, that does not mean that what's behind the transparent window is actually in its front buffer. You can think of that situation as if putting a plastic film (as in a cartoon studio 20 years ago) over the backdrop. This does not make the backdrop appear on the plastic screen (although in the movie, it _looks like_ one scene). – Damon Jul 08 '11 at 12:19
  • 1
    @Damon: Technically you are right. However if no window compositing happens you can in fact abuse OpenGL to grab the screen's content. After creating a full screen singlebuffered window, the window's framebuffer (which actually is just a pointer, stride and length into the screenbuffer), will contain the image of what was below the window when it was created/mapped. You can actually read this content. Furthermore if the window is at the lowest level and pixel ownership test is not performed on glReadPixels you can do live captures. However this is not an OpenGL feature, but a OS glitch. – datenwolf Jul 08 '11 at 13:16
  • 1
    @datenwolf: You could be right, though that would be a kind of terrible hack which I would personally not like to rely on (how do you know that no compositing is taking place, not even thinking of things like hardware overlay). Plus, I think another necessary precondition would be that the pixel format is `PFD_SWAP_EXCHANGE` (or whatever the X equivalent is), otherwise there is no reason why either of the window's buffers should have any relation to the framebuffer. – Damon Jul 08 '11 at 13:40
  • @datenwolf: Please, show us some source samples in form of an answer!)I would love at least try it out. – Rella Jul 08 '11 at 14:03