Linux Graphics Demystified
Linux Graphics Demystified
L
r
x
i
D
a
i
M
r
G
e
l
n
V
n
M
t
B M pe f
g
f
X
E
b
u
T
O
y
r
k
X
T v
B
h
o
n
D
i
b
A
D
i
m
O
fl
e
l
.
a
M
d
l
X
D
KMS
UXA
DRM
OMG
WTF
BBQ?
b
M
G
v
M
f S
E
X
d
R
G
n
d
D
Linux Graphics Demystified
a
M
u
n
d
l
K yla sa CB ea ay nh l
v
X
o
e
a
g
W
u
e
X
W M 3D no
X ad
c
E
r
A
A
a
m
1
A
f
X
M
1
r
I
u
L
X
U
i
X
$ ll
G PR Su
Linux-Grafikdschungel
im
Durchblick
Martin Fiedler
Dream Chip Technologies GmbH
Agenda
Console and Frame Buffer
X Window System
OpenGL, Mesa and Gallium3D
DRI Direct Rendering Infrastructure
KMS Kernel Mode Setting
Compositing
Driver Overview
Other Graphics Systems Android, Wayland and Mir
Video Acceleration
Hybrid Graphics
2/49
4/49
Framebuffer Devices
First in-kernel graphics framework: Framebuffer Devices (fbdev)
required for porting:
Console
many platforms dont have a text mode
Application
hardware-specific kernel drivers
with common API
/dev/tty1
z.B. intelfb, atifb
Linux Console
Subsystem
vesafb: hardware independent,
Graphical
uses the VESA BIOS of the graphics card
Application
fbcon
efifb: same, but for UEFI
accessible from userspace: /dev/fbX
/dev/fb0
very simple API
Framebuffer
Driver
fbcon: text console emulation
with bitmapped fonts (and penguins )
Hardware
done in the kernel, not userspace
Martin Fiedler Durchblick im Linux-Grafikdschungel
5/49
X Window System
X Window System
X11 Protocol
X11 Protocol
Most commonly used graphics system on Linux: The X Window System (X11, X)
popular on all Unix-like systems
client/server architecture
Application
Application
client = application
(X Client)
(X Client)
server manages input and output
network transparent: client and server
not required to run on the same machine
communication via TCP/IP
or locally via Unix Domain Sockets
X Server
X11 is the name of the protocol
Driver
Driver
X Server manages a window hierarchy
Input
Display
root window = desktop wallpaper
Devices
Devices
top-level windows = application windows
subwindows = controls (buttons etc.)
7/49
8/49
X Extensions
The X Protocol can be extended with new functionality via Extensions. Examples:
XSHM (X Shared Memory) faster local display of bitmap graphics
Xv (X Video) hardware-accelerated video display
GLX OpenGL on X
Xinerama multi-monitor support
XRandR (X Resize and Rotate) graphics mode setting without restarting the X
Server
XRender modern anti-aliased, alpha-blended 2D graphics
today used for (almost) every 2D graphics application
9/49
2D Acceleration in X
Multiple approaches to hardware-accelerated 2D graphics in XFree86 / X.Org:
XAA (XFree86 Acceleration Architecture, 1996)
simple acceleration of line drawing and fill operations
EXA (2005) derived from KAA (Kdrive Acceleration Architecture, 2004)
dedicated to XRender acceleration
UXA (Unified Memory Acceleration Architecture, 2008)
developed by Intel, designated successor to EXA
not adopted by non-Intel drivers
SNA (Sandy Bridge New Acceleration, 2011)
very Intel specific, but also quite fast
Glamor (2011)
implements all 2D acceleration via OpenGL
result: hardware independent
Martin Fiedler Durchblick im Linux-Grafikdschungel
10/49
OpenGL
OpenGL
OpenGL (Open Graphics Language) is the standard API for 3D graphics.
industry standard, governed by the Khronos Group consortium
functionality: hardware-accelerated drawing of textured triangles
OpenGL ES = OpenGL for Embedded Systems
(mostly) a subset of OpenGL, ~90% compatible
OpenGL (ES) 2.0 and newer feature programmable shaders
C-like language GLSL (OpenGL Shading Language)
extension mechanism (similar to X11)
additional API required as glue to the windowing system:
GLX for the X Window System
WGL (Windows), AGL (Mac OS X)
EGL for OpenGL ES (Embedded Linux, Android, iOS, ...)
available on all systems, will eventually supersede GLX etc.
Martin Fiedler Durchblick im Linux-Grafikdschungel
12/49
Application
X11 + GLX
X Server
Application
X11 + GLX
X Server
OpenGL
direct
function
calls
OpenGL
GLX
13/49
Mesa
There are two kinds of OpenGL implementations on Linux:
the proprietary drivers by nVidia and AMD
or Mesa
Mesa is an open source OpenGL implementation
... including GLX, EGL and OpenGL ES
initially only software-rendered
today its the bases for all open source 3D drivers
14/49
Gallium3D
Gallium3D is a framework for implementing GPU drivers in an operating system
independent manner.
partially dependent on Mesa
not just 3D graphics also does GPU compute and hardware video decoding
three basic parts:
State Tracker: implementation of a client API
e.g. OpenGL (via Mesa), OpenCL for compute, VDPAU and OpenMAX for video
WinSys Driver: implementation of the GLX or EGL layer
Pipe Driver: backend for a specific GPU
e.g. llvmpipe (a comparatively fast software renderer)
nv30, nv50, nvc0, nve0 (nVidia GPUs); r300, r600, radeonsi (AMD GPUs)
uses shader representation TGSI (Tungsten Graphics Shader Infrastructure)
some backends also use LLVM internally
Martin Fiedler Durchblick im Linux-Grafikdschungel
15/49
Application
Application
Application
Application
OpenGL
OpenGL
OpenGL
OpenGL
Mesa
Mesa
Mesa
Gallium3D
State Tracker
Gallium3D
State Tracker
Gallium3D
(TGSI)
Gallium3D
(TGSI)
proprietary
OpenGL
driver
hardwarespecific
driver
backend
gallivm
Gallium3D
Pipe Driver
LLVM IR
LLVM
Backend
GPU
GPU
GPU
Pipe Driver
GPU
16/49
OpenCL
Current GPUs are not just good for graphics
contain dozens to thousands of fast floating point compute units
GPGPU (General Purpose GPU) or Compute applications
Standard API for compute: OpenCL (Open Compute Language)
also governed by Khronos Group
Linux support works in a similar way to OpenGL:
closed source drivers bring their own implementation
Gallium3D: state tracker Clover
Beignet for Intel GPUs
other popular compute API: CUDA
proprietary, nVidia only, only available in closed source drivers
17/49
19/49
DRI Versions
There are three major generations of the DRI:
DRI 1 (1998)
first, limited implementation
rather inefficient if more than one application wanted to use the 3D hardware
DRI 2 (2007)
solves the most serious problems of DRI 1
the current, most widely deployed version
DRI 3 (2014?)
many detail improvements
currently in development
If not mentioned otherwise, the following slides refer to DRI 2.
Martin Fiedler Durchblick im Linux-Grafikdschungel
20/49
21/49
22/49
24/49
Frame Buffer
Plane(s)
CRTC
Encoder
Encoder
LVDS
HDMI
Connector
Connector
internal display
HDMI port
(Example)
25/49
KMS: Outlook
xf86-video-modesetting: hardware-independent DDX driver for X.Org, based on
KMS and Glamor
KMSCON: replacement of the Linux kernels framebuffer console layer with a
proper, fully featured terminal emulation in userspace
hardware acceleration, multiple monitors, full Unicode support, anti-aliasing, ...
Further development of KMS: ADF (Atomic Display Framework)
useful for hardware with multiple overlay planes
standard feature on embedded and mobile devices
settings of all overlays can be modified synchronously (atomically)
prevents flickering and tearing
26/49
Compositing
Compositing
normal X11 windows are lossy
have to be redrawn if areas that have been
occluded by other windows are exposed
alternative: redirection
window isnt drawn directly to the screen,
but off-screen into a so-called pixmap
input handling continues to work as usual
(i.e. as if the window was drawn on-screen)
compositor finally draws the off-screen pixmaps
at the correct locations
only one real window without redirection:
the Compositor Root Window
compositor commonly integrated into the window manager
unredirection = suspension of redirection for full-screen windows
Martin Fiedler Durchblick im Linux-Grafikdschungel
Offscreen
Pixmaps
Compositor
28/49
29/49
Xgl
Early solution attempt for the OpenGL compositing problem: Xgl
Xgl = special virtual X Server
draws everything with OpenGL
for classic X applications: using the
Application
glitz library (a predecessor of Glamor)
for OpenGL applications: by enforcing
Offindirect rendering
OpenGL screen
Xgl
all OpenGL commands go
through the Xgl server
X Server
... who redirects the output into
OpenGL Frame Buffer Objects
this way, the server can give the compositor
access to all windows contents
Xgl itself runs on another, real X Server
Martin Fiedler Durchblick im Linux-Grafikdschungel
Compositor
OpenGL
Display
30/49
AIGLX
Other early approach to the OpenGL compositing problem: AIGLX
(Accelerated Indirect GLX)
enables hardware accelerated
indirect rendering for OpenGL
Application
actually, it enforces indirect rendering:
all real OpenGL rendering
Offhappens in the X Server
OpenGL screen
output is redirected into
OpenGL Frame Buffer Objects
this way, the server can give the compositor
access to all windows contents
Compositor
OpenGL
Display
31/49
Driver Overview
33/49
34/49
Fallback
Intel
AMD
vesafb /
Framebuffer
KMS vesafb
KMS
efifb
DRM/Kernel
i915 fglrx
radeon
X.Org DDX fbdev / vesa intel fglrx
radeon
UXA / propriEXA /
2D Accel.
SNA
etary
Glamor
OpenGL
Mesa
Mesa
fglrx
Mesa
i915 /
Mesa
Gallium3D
Gallium3D
i965
r300 / r600 /
Gallium3D
llvmpipe
radeonsi
OpenCL
Gallium3D Beignet fglrx
Gallium3D
Martin Fiedler Durchblick im Linux-Grafikdschungel
nVidia
vesafb
KMS
nvidia
nvidia
proprietary
nVidia
nouveau
nouveau
Gallium3D
EXA
Mesa
nv30 / nv50 /
nvc0 / nve0
nVidia
Gallium3D
35/49
36/49
38/49
Android
Android uses the Linux kernel, but not much more
no GNU userland, no X
Application
Binder IPC
custom C library: Bionic
SurfaceFlinger
custom IPC mechanism: Binder
Android UI
Framework
graphics based on OpenGL ES and EGL
no DRI (mostly proprietary drivers)
OpenGL ES
EGL
HWComposer
gralloc
hardware-specific HWComposer
library as rough equivalent of KMS
Hardware (GPU + Display Controller)
gralloc for graphics memory management
part of HWComposer in newer versions
compositor and display server: SurfaceFlinger
SurfaceFlinger also allocates graphics buffers for applications
39/49
Wayland
So far the most promising candidate for replacing the X Window System: Wayland
goal: radical simplification of Xs concepts
technically, its a protocol
using Unix Domain Sockets
not network transparent
Wayland Compositor /
Application
server part is not a program of its own,
Protocol Display Server
but a library
used by the compositor
DRI: OpenGL + EGL
evdev
the compositor is the display server
reference implementation: Weston
Graphics HW
Input Devices
based on EGL and DRI
buffer allocation and drawing completely
done in the clients
input devices are used via the kernels event device framework
Martin Fiedler Durchblick im Linux-Grafikdschungel
40/49
41/49
Mir
Competition for Wayland: Mir by Canonical
graphics system for upcoming Ubuntu versions
not yet in 14.04, but maybe in 14.10
conceptually very closely related to Wayland,
but a totally different and incompatible implementation
uses more parts of Android, e.g. the input subsystem
more focus on data exchange between applications
graphics buffers are allocated in the server, but drawn in the client
XMir = XWayland for Mir
also employs libhybris for Android graphics driver support
much resistance in the community
its doubtfull whether another system is really necessary
42/49
Video Acceleration
Video Acceleration
There are multiple approaches for hardware-accelerated video on X:
Xv (X extension, 1991)
only for video output, not decoding
functionality: scaling, color space conversion
two typical kinds of implementation (can be mixed):
Overlay: directly overlays the video into the display output
Textured Video: draws the video into the framebuffer using the 3D hardware
XvMC (X extension, 2000)
accelerates two specific aspects of MPEG-2 decoding:
Motion Compensation (MC) and IDCT (88 block transform)
obsolete
specific to MPEG-2, never adapted to newer standards
supported by very few drivers only
Martin Fiedler Durchblick im Linux-Grafikdschungel
44/49
Hardware Decoding
Current GPUs contain hardware decoders for the common standards (e.g. H.264).
multiple incompatible APIs:
nVidia proprietary: VDPAU (Video Decode and Presentation API for Unix)
full-featured: decoding, display, deinterlacing, ...
AMD proprietary: XvBA (Xv Bitstream Acceleration)
decoding only, display via OpenGL
AMD
AMD
XvBA
Intel: VA-API (Video Acceleration API)
Closed Source
Open Source
decoding into DRI buffers
Intel
VA-API
embedded playforms: OpenMAX
Open Source
industry standard for de- and encoding
nVidia
nVidia
VDPAU
Closed Source
Open Source
Situation improves slowly:
VA-API backends for VDPAU and XvBA
OpenMAX
Gallium3D
Gallium3D State Tracker for VDPAU and OpenMAX
Gallium3D backends for nVidias und AMDs hardware decoders
Martin Fiedler Durchblick im Linux-Grafikdschungel
45/49
Hybrid Graphics
Hybrid Graphics
Many current notebooks have two GPUs:
processor-integrated graphics slow, but saves power
additional (dedicated) nVidia or AMD GPU fast, but inefficient
vga_switcheroo: deactivates one of the GPUs
switching GPUs requires restarting the X Server
only works on systems with Video Mux
where both GPUs can drive all displays
Problem: newer models are usually muxless
by now, proprietary drivers by AMD and nVidia have their own switchers
based on XRandR 1.4 (xrandr --setprovideroutputsource)
work on muxless systems too
but: the dedicated GPUs output is copied over to the integrated GPU
not saving power (quite the contrary both GPUs are active!)
Martin Fiedler Durchblick im Linux-Grafikdschungel
47/49
48/49
Thank You!