Tearing in SDL 1.2 apps unless composite extension is completely disabled.

The subject pretty says anything.
From simple emulators running atop of sdl to more complex games, the only way to stop them to “tear”, usually with a single line in the top quarter of the screen, is to completely disable the composite extension from xorg.conf.
NO MATTER if i use a compositing WM or not.
NO MATTER if i use a multihead or a single head setup.

Is this expected?

I tested the issue on two systems, different Motherboard, but same card: 9800GT, driven by 331.38 drivers.

I can’t be sure about it, but i don’t remember to have seen this with previous drivers, (nor i have the time needed to test for regressions, sorry).
By now i start the problematic apps on a separate X screen as a workaround.
nvidia-bug-report.log.gz (76.3 KB)

I just tracked down the same problem. It looks like the culprit is backing store. When the server says that backing store is available, libSDL turns it on for windows it creates:

from http://hg.libsdl.org/SDL/file/22a7f096bb9d/src/video/x11/SDL_x11video.c#l1096:

#if 0 /* This is an experiment - are the graphics faster now? - nope. */
        if ( SDL_getenv("SDL_VIDEO_X11_BACKINGSTORE") )
#endif
        /* Cache the window in the server, when possible */
        {
                Screen *xscreen;
                XSetWindowAttributes a;

                xscreen = ScreenOfDisplay(SDL_Display, SDL_Screen);
                a.backing_store = DoesBackingStore(xscreen);
                if ( a.backing_store != NotUseful ) {
                        XChangeWindowAttributes(SDL_Display, SDL_Window,
                                                CWBackingStore, &a);
                }
        }

Modern X servers handle backing store by implicitly redirecting windows using Composite, as if a composite manager were running.

You can disable backing store in the X server by passing the -bs argument to it. You’re also supposed to be able to disable it by setting

Option "BackingStore" "off"

in the “Screen” section of xorg.conf, but it looks like support for that is broken in the X server.

[Edit: I filed freedesktop.org bug #74158 for the BackingStore option not working.]

Thanks aaron for looking into this,
from what i can understand from the code you quoted, exporting SDL_VIDEO_X11_BACKINGSTORE=0 has no effect, right?

Hi again aaron, i tried to patch SDL_x11video.c with the following:

--- a/src/video/x11/SDL_x11video.c      2014-02-03 09:19:06.152761230 +0100
+++ b/src/video/x11/SDL_x11video.c      2014-02-03 09:14:40.062762569 +0100
@@ -1088,11 +1088,10 @@
                }
        }
 
-#if 0 /* This is an experiment - are the graphics faster now? - nope. */
        if ( SDL_getenv("SDL_VIDEO_X11_BACKINGSTORE") )
-#endif
-       /* Cache the window in the server, when possible */
+       /* Cache the window in the server, on request */
        {
+               printf("backingstore on:\n");
                Screen *xscreen;
                XSetWindowAttributes a;

…so that the final code is:

if ( SDL_getenv("SDL_VIDEO_X11_BACKINGSTORE") )
    /* Cache the window in the server, on request */
    {
        printf("backingstore on:\n");
        Screen *xscreen;
        XSetWindowAttributes a;

        xscreen = ScreenOfDisplay(SDL_Display, SDL_Screen);
        a.backing_store = DoesBackingStore(xscreen);
        if ( a.backing_store != NotUseful ) {
            XChangeWindowAttributes(SDL_Display, SDL_Window,
                                    CWBackingStore, &a);
        }
    }

Then i tried:
unset SDL_VIDEO_X11_BACKINGSTORE

EDIT:
It DOES do the trick.
I have still tearing in dual head mode, but that’s another story i’ll investigate soon.

That #if 0 has been there since the very start of being in mercurial (2001).

…That points me to the question:
Was i blind and didn’t noticed tearing in the past, or something changed in the drivers?

Read:
*will we need to disable backing store from xorg.conf in the future to avoid tearing or maybe the drivers can be fixed in that regard?
*Does disabling backing store leads to any performance loss?

thanks!

-EDIT
in this thread:
https://bbs.archlinux.org/viewtopic.php?id=175820&p=3

Some people says that disabling composite extension did fix the issue on amd cards too; so giving this information as true, maybe there is more than the bug you filed in the new Xorg release?

Backing store has a storied history in the X server. Traditionally, it was implemented as its own layer of code that plugged into the X server’s acceleration architecture. That code was riddled with bugs and was a constant source of crashes so eventually the developers got so fed up that they just removed it entirely. Backing store was always an optional feature so applications were supposed to be able to deal with it not being there, but of course, there are a few old apps floating around that rely on it. Fairly recently, backing store got re-implemented by piggybacking on the Composite extension.

You likely didn’t notice tearing in the past because either backing store wasn’t enabled in your X server, or the implementation didn’t use Composite so it didn’t introduce the tearing problems that Composite’s automatic redirection code causes.

I would not expect disabling backing store to lead to any performance loss on modern applications. It’s mostly designed for apps where re-rendering the window content when they’re exposed is much more expensive than saving a copy of the pixels. For an SDL application that draws to the whole window most of the time, enabling backing store doesn’t make much sense and probably never helped. I suspect the “This is an experiment - are the graphics faster now? - nope” experiment didn’t pan out, but got left enabled by mistake.

Undesrtood, thanks.
So it makes sense to file a bug against sdl too:
https://bugzilla.libsdl.org/show_bug.cgi?id=2383