Solution for screen tearing in X11

burned televison

Burned out by Tina Rataj-Berard

I often see online people complaining that we have to get rid of X11 and start using Wayland because Wayland fixes the screen tearing on Linux desktop. I scratch my head as there have been solutions for screen tearing on X11 for years. I want to show you a simple solution to tackle screen tearing, but it depends which GPU your machine has.

Intel and newer AMD cards

Intel and AMD has made it really simple!  Basically just enable the TearFree option with this line in your X11 configuration:

Option "TearFree" "true"

I recommend to create a new configuration file in /etc/X11/xorg.conf.d/ for example 20-intel.conf or 20-amd.conf. The file could look like this:

# If Intel:
# /etc/X11/xorg.conf.d/20-intel.conf
Section "Device"
    Identifier  "Intel Graphics"
    Driver      "intel"
    Option      "TearFree" "true"
EndSection

# Or if AMD:
# /etc/X11/xorg.conf.d/20-amd.conf
Section "Device"
    Identifier  "AMD"
    Driver      "amdgpu"
    Option      "TearFree" "true"
EndSection

Then reboot (or just restart your X server) to apply these settings. On AMD this works on newer cards which use the new AMDGPU driver.

Older AMD cards

This is for older AMD cards which use the Radeon driver. The line in the X11 configuration looks similar:

Option "TearFree" "on"

Include this to the configuration file in /etc/X11/xorg.conf.d/:

# etc/X11/xorg.conf.d/20-radeon.conf
Section "Device"
    Identifier  "Radeon"
    Driver      "radeon"
    Option      "TearFree" "on"
EndSection

Then reboot or restart your X server.

NVIDIA cards

On NVIDIA running with the proprietary drivers I would recommend using the nvidia-settings tool instead of editing the X11 configuration files yourself. The nvidia-settings application gives you nice graphical user interface to manage all the settings you need. Using the application, first click “X Server Display Configuration” from the menu on the left and then click the “Advanced…” button:

Then click the checkboxes for “Force Composition Pipeline” and “Force Full Composition Pipeline:

Then click the button “Save to X Configuration File” and restart your X server to apply the new settings.

If all else fails

Above solutions should work 98% of the time (that number was pulled out of my arse), but I’ve heard that for some people they don’t. Also if you’re running NVIDIA hardware with the free Nouveau drivers, the above won’t work. Then the last solution is to run an external compositor. If you’re desktop environment doesn’t offer you proper one (disable that one before running this one), I would recommend using compton. Compton should be available from your distributions repository and it has quite many options to choose from. From my experience these options work fine:

compton -b --vsync opengl-swc --backend glx

Compton won’t perform as well as solutions mentioned before should. It is worth trying is nothing else works. Compton doesn’t require you to restart your X server and the changes should apply immediately.

SSD tips and tweaks

Hard drive

Endangered technic. photo by Patrick Lindenberg

SSD work fine out of the box and most optimisations aren’t really necessary, but I think these are useful if you want to increase the life-time of your brand new SSD.

Swap on SSD

If you have enough RAM that you don’t need swap (and you don’t need to hibernate instead of suspend) it might be good idea to disable swap completely. To do this, we can just comment out the line for swap partition in /etc/fstab'(example on my machine):

UUID=a495548e-1d7e-464b-8072-bdc43d07d3a5 none swap sw 0 0

Just add “#” in the beginning of the line:

#UUID=a495548e-1d7e-464b-8072-bdc43d07d3a5 none swap sw 0 0

If you don’t have too much RAM or you need to hibernate your machine, better action is just to reduce the swappiness. This way the swap is used only when necessary. Add this line to your /etc/sysctl.conf:

vm.swappiness=1

Root directory on SSD

This is where you will notice the most benefit over spinning disk. This is where you can reduce writes as well as on root directory you probably won’t need access time logging (some applications might need it on home directory) which would just create a lot of unneeded writes. Let’s edit out /etc/fstab again and add ‘noatime’ flag to line for our root directory:

UUID=bbc3a677-386a-40cb-b89c-fdf2ceb97cab / ext4 noatime,errors=remount-ro 0 1

Deadline I/O scheduler

By default Linux uses the CFQ I/O scheduler which is desirable for spinning disks, but it isn’t optimal for SSDs. For SSDs I would recommend using the Deadline scheduler. First find out which disk on your machine is SSD:

$ grep . /sys/block/sd?/queue/rotational
/sys/block/sda/queue/rotational:1
/sys/block/sdb/queue/rotational:1
/sys/block/sdc/queue/rotational:1
/sys/block/sdd/queue/rotational:0

Look at the number at the end of the line. This shows the /dev/sdd is SSD on my machine as “rotational is false”. Find out the current scheduler on this disk:

$ cat /sys/block/sdd/queue/scheduler
noop deadline [cfq]

Oh okay! Let’s set it to deadline by adding this line to /etc/sysfs.conf

block/sdd/queue/scheduler = deadline

It should apply on next reboot. To set the scheduler without rebooting (this change will reset unless you set the scheduler in /etc/sysfs.conf):

# echo deadline > /sys/block/$YOURDRIVE/queue/scheduler

Now it should look like this:

$ cat /sys/block/sdd/queue/scheduler 
noop [deadline] cfq

TRIM

There are several ways to do TRIM on Linux, but some of them (discard option fstab) might damage your SSD if it has buggy firmware. Using fstrim should generally be safe on all SSDs. To set fstrim to TRIM your SSD regularly first save this script:

#!/bin/sh
fstrim -v /

to /etc/cron.daily/trim or /etc/cron.weekly/trim depending whether you want TRIM done daily or weekly. On my machine it is done weekly, but if it isn’t often enough for you – do it daily.