Graphics Slides
Graphics Slides
Corrections, suggestions, contributions and translations are welcome! embedded Linux and kernel engineering
Send them to [email protected]
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 1/212
Understanding the Linux Graphics Stack training
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 2/212
About Bootlin
About Bootlin
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 3/212
Bootlin introduction
▶ Engineering company
• In business since 2004
• Before 2018: Free Electrons
▶ Team based in France and Italy
▶ Serving customers worldwide
▶ Highly focused and recognized expertise
• Embedded Linux
• Linux kernel
• Embedded Linux build systems
▶ Strong open-source contributor
▶ Activities
• Engineering services
• Training courses
▶ https://bootlin.com
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 4/212
Bootlin engineering services
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 5/212
Bootlin training courses
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 6/212
Bootlin, an open-source contributor
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 7/212
Bootlin on-line resources
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 8/212
Training quiz and certificate
▶ You have been given a quiz to test your knowledge on the topics covered by the
course. That’s not too late to take it if you haven’t done it yet!
▶ At the end of the course, we will submit this quiz to you again. That time, you
will see the correct answers.
▶ It allows Bootlin to assess your progress thanks to the course. That’s also a kind
of challenge, to look for clues throughout the lectures and labs / demos, as all the
answers are in the course!
▶ Another reason is that we only give training certificates to people who achieve at
least a 50% score in the final quiz and who attended all the sessions.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 9/212
Participate!
During the lectures...
▶ Don’t hesitate to ask questions. Other people in the audience may have similar
questions too.
▶ Don’t hesitate to share your experience too, for example to compare Linux with
other operating systems you know.
▶ Your point of view is most valuable, because it can be similar to your colleagues’
and different from the trainer’s.
▶ In on-line sessions
• Please always keep your camera on!
• Also make sure your name is properly filled.
• You can also use the ”Raise your hand” button when you wish to ask a question but
don’t want to interrupt.
▶ All this helps the trainer to engage with participants, see when something needs
clarifying and make the session more interactive, enjoyable and useful for everyone.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 10/212
Collaborate!
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 11/212
Base Theory and Concepts About Graphics
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 12/212
Base Theory and Concepts About Graphics
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 13/212
Light, pixels and pictures
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 14/212
Light, pixels and pictures
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 15/212
Light, pixels and pictures (illustrated)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 16/212
Sampling and frequency domain
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 17/212
Sampling and frequency domain (illustrated)
A wall of bricks represented in the spatial The wall of bricks represented in the
domain frequency domain
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 18/212
Sampling and frequency domain (illustrated)
A wall of bricks rotated 45◦ represented in The wall of bricks rotated 45◦ represented
the spatial domain in the frequency domain
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 19/212
Sampling and aliasing
▶ The spatial domain is quantized with a bi-dimensional sampling resolution
▶ Matching sampling frequencies exist, for each axis: (us , vs )
▶ They limit the frequencies that can be sampled from the initial domain
▶ The Shannon-Nyquist theorem provides a sufficient condition for (us , vs ):
▶ Frequencies such that u ≥ u2s and v ≥ v2s are not correctly sampled
▶ Can result in incorrect frequencies being introduced: Moiré pattern in 2D
0 1 2 3 4 5 6 7 8 9 10
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 20/212
Sampling and aliasing (illustrated)
Another wall of bricks Moiré on the bricks Moiré on the garage door
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 21/212
Light and color representation
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 22/212
Color quantization approaches
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 23/212
Light representation, color quantization (illustrated)
16 million colors (24 bits per pixel) 16 colors (4 bits per pixel)
▶ high color resolution ▶ medium color resolution
▶ high color range ▶ low color range
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 24/212
Light representation, color quantization (illustrated)
16 million colors (24 bits per pixel) 16 colors (4 bits per pixel)
▶ low color resolution ▶ low color resolution
▶ high color range ▶ high color range
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 25/212
Colorspaces and channels
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 26/212
Colorspaces and channels (illustrated with YUV)
R = Y + 1.140 × V
Y = +0.299 × R + 0.587 × G + 0.114 × B
G = Y − 0.395 × U − 0.581 × V U = −0.147 × R − 0.289 × G + 0.436 × B
B = Y + 2.032 × U V = +0.615 × R − 0.515 × G − 0.100 × B
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 27/212
Frame size and chroma sub-sampling
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 28/212
Frame size and chroma sub-sampling
▶ Chrominance samples are used for multiple luminance samples
▶ With specific vertical and horizontal ratios (usually integer)
▶ Usually summarized using a three-part ratio: J : a : b
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 29/212
Pixel data distribution in memory
▶ Pixel data can be distributed in different ways in memory
▶ Different ways to aggregate color components in data planes (memory chunks):
• Packed: Components are stored in the same data plane in memory
• Semi-planar (YUV): Luma and chroma are stored in distinct data planes
• Planar: Each component has its own data plane in memory
▶ When multiple color components are grouped, bit order must be specified:
• Which component comes first in memory?
• Affected by endianness when read by hardware!
▶ Scan order must also be specified:
• How to calculate the address for position (x, y) and back?
• Raster order (most common) specifies: row-major, left-to-right, top-to-bottom
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 30/212
Pixel formats, FourCC codes
▶ Many meta-data elements are needed to fully describe how a picture is coded
• Some describe picture-level attributes (e.g. dimensions)
• Some describe pixel-level attributes (e.g. colorspace, bpp)
▶ Pixel-level attributes are grouped as a pixel format that defines:
• Color model in use
• Number of bits per channel and per pixel (bpp)
• Bit attribution and byte order
• Per-channel sub-sampling ratios
• Pixel data planes distribution in memory
▶ Often represented as a 4-character code called FourCC
▶ Not really standardized and implementation-specific:
DRM in Linux uses XR24 for DRM_FORMAT_XRGB8888.
Not really standardized but widely used in various forms
▶ Scan order is specified separately with a modifier
Assumed to be raster order if unspecified
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 31/212
Level of detail of quantized pictures
Generally speaking:
▶ Many factors are involved
▶ The major bottleneck is not always obvious
▶ Implementation choices do matter
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 32/212
Base Theory and Concepts About Graphics
Pixel Drawing
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 33/212
Accessing pixel data
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 34/212
Iterating over pixel data
▶ Selected format (slides and demos): XRGB8888
• bpp = 32 = 8 × 4, one byte per channel, one memory plane
▶ Pixel data can be access by iterating nested variables:
for (y = 0; y < height; y++)
for (x = 0; x < width; x++)
data = base + y * stride + x * 4;
▶ Iterating over all pixels takes numerous CPU-cycles, tips:
• Incrementing the address instead of re-calculating it:
data = base;
for (y = 0; y < height; y++)
for (x = 0; x < width; x++)
data += 4;
data += stride - width * 4;
• Iterating in y-major is also better for cache hits
• Beware: C pointer arithmetic uses type size as unit
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 35/212
Concepts about rasterization
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 36/212
Rectangle drawing
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 37/212
Linear gradient drawing
x − xstart
r = rstart + (rstop − rstart )
xsize
x − xstart
g = gstart + (gstop − gstart )
xsize
x − xstart
b = bstart + (bstop − bstart )
xsize
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 38/212
Disk drawing
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 39/212
Circular gradient drawing
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 40/212
Line drawing
▶ A line is defined as an affine function:
y(x) = a × x + b
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 42/212
Line and shape aliasing, sub-pixel drawing (illustrated)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 43/212
Circles and polar coordinates
(x − xc )2 + (y − yc )2 = radius2
▶ Which is always verified with the expression:
y
x = xc + radius × cos(ϕ)
y = yc + radius × sin(ϕ)
r
▶ Corresponds to a translation in polar coordinates
• From a (x, y) base to (r, ϕ) x
▶ Iteration on ϕ with a specific range: ϕ ∈ [0; 2π]
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 44/212
Parametric curves
▶ Parametric curves generalize the idea of using independent parameters
▶ Each curve has defining equations and ranges for parameters
• Equations allow calculating (x, y) (or (r, ϕ))
▶ Drawing is achieved by iterating over parameter values
• Sampling is done on the range to get a finite number of points
• X/Y coordinates are calculated for each point
• Line interpolation is used between consecutive points
▶ Ellipse: ϕ ∈ [0; 2π]
x = xc + a × cos(ϕ)
y = yc + b × sin(ϕ)
▶ Many more parametric curves exist:
• Cycloid, Epicycloid, Epitrochoid, Hypocycloid, Hypotrochoid (spirograph)
• Lissajous Curve, Rose curve, Butterfly curve
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 45/212
Base Theory and Concepts About Graphics
Pixel Operations
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 46/212
Region copy
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 47/212
Alpha blending
Opaque
A and B
Partially-
transparent
A and B
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 48/212
Color-keying
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 49/212
Scaling and interpolation
▶ Scaling is a resizing operation on a pixel picture
• Involves a scaling factor (integer or real)
• Values are resampled with a new resolution
• Requires reconstructing the original signal
▶ Implemented with some form of interpolation:
• nearest-neighbor: uses the nearest pixel value from the source
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 50/212
Linear filtering and convolution
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 51/212
Linear filtering and convolution (illustrated)
0 1 2 3 4
3 2 0 2 0 3 2 0 2 0
-1 0 -1 -1 0 -1
4 1 2 1 0 4 1 2 1 0
0 5 0 0 5 0
3 2 4 0 3 3 2 4 0 3 2
-1 0 -1 -1 0 -1
1 3 1 4 0 1 3 1 4 0
4 0 2 3 1 4 0 2 3 1
∑
n ∑
m
g(x, y) = ω ∗ f(x, y) = ω(s, t)f(x − s, y − t)
s=−n t=−m
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 52/212
Blur filters
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 53/212
Dithering
▶ Reducing the color depth can lead to visually-unpleasant results
• Corresponds to color-space down-sampling
• Increases color quantization error
▶ Floyd–Steinberg dithering is a method for improving quality with low depth
▶ Quantization error is evaluated and distributed to neighboring pixels
▶ Used in hardware display engines and the GIF file format
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 56/212
Graphics theory illustrations attributions
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 57/212
Hardware Aspects
Hardware Aspects
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 58/212
Hardware Aspects
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 59/212
Technological types of graphics hardware implementations
▶ Dedicated graphics hardware is often used along a general-purpose CPU
▶ Two commonly-used technologies, with typical pros/cons:
Fixed-function Programmable
Technology Circuit Software
Source form HDL Source code
Product form Silicon, bitstream Firmware binaries
Implementation FPGA, ASIC, SoC block DSP, custom ISA
Arithmetic Fixed-point Fixed-point, floating point
Clock rate / power Low High
Pixel data access Queue (FIFO) Memory
CPU control Direct registers Mailbox
Die surface High Low
Reusability Low High
Example Allwinner SoCs Display Engine TI TMS340 DSP
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 60/212
Graphics memory and buffers
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 61/212
Display hardware overview
▶ Stream pixel data to a display device, via a display interface
▶ Internal pipeline with multiple components
▶ Generally fixed-function hardware, pipeline sink only
▶ Either discrete (video card) or integrated
▶ Connected to the CPU (and RAM) via a high-speed bus:
e.g. AXI with ARM, ISA, PCI, AGP, PCI-e with x86
A 1986 Hercules discrete video card An Intel processor with integrated graphics
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 62/212
Common components of a display pipeline overview
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 65/212
I/O with graphics hardware, pipelines
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 66/212
Building complex pipelines
▶ Display, rendering and video elements are chained from source(s) to sink(s)
▶ On source-sink boundaries:
• Mutually-supported pixel format (or conversion)
• Mutually-accessible memory (or copy) or internal FIFO
▶ Target frame rate (fps) gives a time budget for pipeline traversal:
t0 + t2 + t4 + t5 < fps−1 , t0 + t3 < fps−1 , t1 + t4 + t5 < fps−1
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 67/212
Debugging pipelines
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 68/212
Hardware Aspects
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 69/212
Visual display technologies generalities
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 70/212
CRT display technology
▶ Color cathode-ray tubes (CRTs), since the 1950s:
• Using electron beams to excite a phosphorescent screen
• Beams are guided by magnetic deflection
• One beam for each color with increased intensity for increased luminosity
• High energy consumption
• High contrast, low response time (1 − 10 µs)
• Other issues: monitor size, burn-in (screensavers), remnant magnetic field
(degaussing), high voltages and magnetic fields
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 71/212
Plasma display panels technology
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 72/212
LCD display technology
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 73/212
OLED display technology
▶ Organic light-emitting diodes (OLEDs), since 2010:
• Using organic compounds (carbon-based) to emit light as R-G-B LEDs
• Allows flat and flexible surfaces, with a large viewing angle
• Low energy consumption
• Very high contrast, low response time (1 − 10 µs)
• Issues: burn-in, independent cells aging, affected by UV light
• Rapidly becoming very popular and used
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 74/212
EPD display technology
▶ Electrophoretic displays (EPDs), since the 2000s:
• Using black and white electrically-charged ink-like particles in oil
e.g. positive charge for black and negative for white
• Electric fields attract one or the other color with current flow
the particles stay in place after they were moved
• Using incident light, does not emit light itself
• Very low consumption (only for changes)
• Very high response time (100 ms) and ghosting
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 76/212
Display panels integration and monitors (illustrated)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 77/212
Display panels refreshing
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 78/212
Display timings and modes
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 79/212
Display timings and modes (illustrated)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 80/212
Display timings and modes (panel example)
▶ hsync = thpw = 20 ∈ J1; 40K
hbp = thb − thpw = 46 − 20 = 26 (from diagram)
hactive = thd = 800
hfp = thfp = 210 ∈ J16; 354K
htotal = hsync + hbp + hactive + hfp = 1056
▶ vsync = tvpw = 10 ∈ J1; 20K
vbp = tvb − tvpw = 23 − 10 = 13 (from diagram)
vactive = tvd = 480
vfp = tvfp = 22 ∈ J7; 147K
vtotal = vsync + vbp + vactive + vfp = 525
▶ 1 frame takes: vtotal × htotal = 554400 tclk
AT070TN94 panel datasheet 60 frames take: vtotal × htotal × 60 = 33264000 tclk
60 fps requires: fclk ≥ 33.264 MHz
▶ Monitor display connectors often come with a Display Data Channel (DDC)
• Side-bus to allow communication between host and display
• Usually based on I2C, quite slow (≈ 100 kHz)
▶ DDC provides access to the Extended Display Identification Data (EDID)
• Contains the list of supported modes in a standard format
• Usually stored in an EEPROM at I2C address 0x50
▶ Another common monitor signal is Hotplug Detect (HPD)
• Connected to a pin of the connector, asserted with a cable plugged
• Can be wired to an interrupt pin to detect connection changes
▶ Direct panel interfaces (not monitors) usually lack DDC, EDID and HPD
• Panel is always considered connected
• Modes need to be known in advance
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 82/212
Extra display interface features and EDID extensions
▶ The EDID standard keeps evolving and exposes new features through extensions
▶ Configuration data for each feature is embedded in the EDID
▶ More or fewer features are supported depending on the display interface
▶ Common extra display interface features:
• Interlaced: Every other pixel line is sent at a time, alternating between top-fields
and bottom-fields; Allows faster refreshing for CRTs, needs deinterlacing for
progressive panels;
• Audio: Send audio in addition to pixels, during blanking periods;
• Stereoscopy: Pixel data is split between two screens that show a different
geometrical perspective, providing 3D perception;
• Variable Refresh Rate (VRR): Pixel data can be sent at any point and does not
need to conform to a given refresh rate;
• Consumer Electronic Control (CEC): Remote control features on a dedicated bus;
• High-Bandwidth Digital Content Protection (HDCP): Anti-copy protection
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 83/212
Types of display interfaces
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 84/212
Types of display interfaces (illustrated)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 85/212
VGA display interface
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 86/212
VGA display interface (illustrated)
• TMDS: Data+ (2, 5, 10, 13, 18, 21), Data- (1, 4, 9, 12, 17, 20), Clock (23, 24)
• Analog pixel: Red, Green, Blue (C1, C2, C3), Ground (C5)
• Analog sync: Hsync, Vsync (C4, 8)
• Side: DDC SDA, DDC SCL (7, 6), HPD (16)
• Power: +5 V (14), Ground (15)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 88/212
HDMI display interface
• TMDS: Data+ (1, 4, 7), Data- (3, 6, 9), Clock (10, 12)
• Side: SDA, SCL (16, 15), HPD (19), CEC (13)
• Power: +5 V (18), Ground (17)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 89/212
DP/eDP display interface
▶ DisplayPort (DP), since 2008 (VESA)
• Digital serial link with 4 data lanes using Low-Voltage Differential Signaling
(LVDS) or TMDS for DP Dual-Mode (DP++), compatible with DVI-D and HDMI
• Using packets for video/audio data and meta-data
• Auxiliary channel encapsulating I2C DDC, CEC and more (e.g. USB)
• High bandwidth (≤ 77.37 Gbit/s) (2.0)
• Extra features: Audio, CEC, HDR (1.4), 4K (1.3), Stereoscopy (1.2),
8K (1.3-1.4), 10K-16K (2.0)
• Multi-Stream Transport (MST) to chain displays
• Using a dedicated (and proprietary) DisplayPort connector for signals:
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 91/212
DPI display interface
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 92/212
Bridges/transcoders
A DP to DVI bridge
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 93/212
Hardware Aspects
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 94/212
Digital Signal Processors
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 95/212
Dedicated hardware accelerators
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 96/212
Dedicated hardware accelerators (illustrated)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 97/212
Graphics Processing Unit
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 98/212
Graphics Processing Unit architectures
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 99/212
Graphics Processing Unit architectures (illustrated)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 100/212
Graphics Processing Unit techniques
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 101/212
Graphics Processing Unit techniques (illustrated)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 102/212
Graphics Processing Unit internals
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 103/212
Hardware Aspects
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 104/212
Graphics integration and memory
▶ Graphics devices integrated in larger systems need two main interfaces:
• Control interface (low speed): to program the device from the main CPU
• Memory interface (high speed): to read the source data and write their framebuffer
▶ Other usual required elements: clocks, interrupts, reset
▶ Both the graphics device and the CPU need to access the memory
▶ Different types of memory used by graphics hardware:
• graphics memory: dedicated memory attached to the graphics device
the memory is made available to the CPU through the memory interface
• dedicated system memory: a reserved contiguous area of system memory
required when the device has no mapping unit
• system memory pages: any system memory page can be mapped for access
for devices with a dedicated IOMMU and graphics address remapping table (GART)
▶ Since the two parties access the same memory, cache can become incoherent
▶ Cache must be synchronized before reading and after writing, or disabled
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 105/212
Shared graphics memory access
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 106/212
Graphics shared memory access (illustrated tearing)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 107/212
Graphics memory constraints and performance
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 108/212
Tiled framebuffer format example
A tiled framebuffer read in raster order The same framebuffer read properly
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 109/212
Offloading graphics to hardware
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 110/212
Graphics performance tips
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 111/212
Graphics hardware online references
▶ Wikipedia (https://en.wikipedia.org/):
• Graphics pipeline
• Comparison of display technology
• List of video connectors
• Digital signal processor
• Graphics processing unit
• Tiled rendering
• Multiple buffering
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 112/212
Graphics hardware illustrations attributions
▶ ATI Hercules Card 1986: Jörgen Nixdorf, CC BY-SA 3.0
▶ Intel Skylake 14nm: Fritzchens Fritz, CC0 1.0
▶ TN display closeup 300X: Akpch, CC BY-SA 3.0
▶ LCD monitor screen image: Koperczak, CC BY-SA 3.0
▶ CRT color enhanced unlabeled: Grm_wnr, CC BY-SA 3.0
▶ Dell TFT LCD: Szasz-Revai Endre, CC BY 2.5
▶ CeBIT 2011 3D Passport system: Bin im Garten, CC BY-SA 3.0
▶ Bouquin électronique iLiad en plein soleil: Mathieu Despont, public domain
▶ Kindle 3 E Ink screen: HorsePunchKid, CC BY-SA 3.0
▶ DP to DVI converter unmounted: Antifumo, CC BY-SA 3.0
▶ Anisotropic filtering: Thomas, Lampak, CC BY-SA 3.0
▶ MipMap Example STS101: Mike Hicks, NASA, CC BY-SA 3.0
▶ Big Bug Bunny: Blender Foundation, CC BY 3.0
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 113/212
Software Aspects
Software Aspects
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 114/212
Software Aspects
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 115/212
System-agnostic overview: kernel
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 116/212
System-agnostic overview: display userspace
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 117/212
System-agnostic overview: display userspace (illustrated)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 119/212
System-agnostic overview (illustrated)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 120/212
Linux kernel overview
▶ Input subsystem
• Supports devices such as mice, keyboards, joysticks, touchscreens
• Legacy (unused) interfaces for keyboard, mice
• Unified evdev event interface to simplify userspace
▶ Framebuffer device (fbdev) subsystem
• Legacy interface for displaying pixel buffers on-screen
• Very limited pipeline configuration, no hotplug support
• Extended features added through driver-specific interfaces
▶ Direct Rendering Manager (DRM) subsystem
• Unified display configuration interface: Kernel Mode Setting (KMS or DRM mode)
• Allows synchronizing changes together (DRM atomic)
• Exposes render devices through driver-specific interfaces (DRM render)
Mostly for 3D rendering with GPUs, but a few 2D devices too
• Provides memory management mechanisms (DRM GEM)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 121/212
Linux-compatible low-level userspace overview
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 122/212
X Window overview
▶ X Window overview
• X Window/X11 is the historical (and legacy) display protocol
• Complemented by numerous protocol extensions for extra features
• X.org is the reference X11 server implementation
• Needs an external window manager to handle multiple applications
• Composition by the server or the window manager (Composite extension)
▶ Window manager implementations examples
• Mutter: GNOME accelerated compositing window manager
• i3: Popular tiling window manager
• Compiz: Popular 3D-enabled compositing window manager
▶ Display client libraries
• Xlib (C): The legacy X11 client-side protocol library helper
• XCB (C): The updated X11 client-side protocol library helper
• Integrated in most higher-level graphics-oriented libraries
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 123/212
Wayland overview
▶ Wayland overview
• Wayland is a display protocol (with a core and extensions), not an implementation
• Replaces X11 with a less intrusive, more modern and minimal paradigm
• Compositors (server-side) handle input, windows, composition and display
▶ Wayland compositor implementations examples
• Using the libwayland-server base protocol library
• Weston/libweston: Reference implementation
• Sway/wlroots: Tiling window manager and base library
• Mutter: GNOME compositor
▶ Display client libraries
• Using the libwayland-client base protocol library
• Integrated in many higher-level graphics-oriented libraries
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 124/212
High-level graphics libraries and desktop environments overview
▶ Applications rarely to never use Wayland or X11 directly
▶ Drawing and managing a user interface is complex
▶ Widely-used high-level graphics libraries (aka toolkits) TM
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 125/212
Software Aspects
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 126/212
Linux TTY subsystem introduction
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 127/212
Linux TTY (illustrated)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 128/212
Virtual terminals and graphics
▶ With VTs, the kernel is already using the display and keyboard!
▶ Display servers need to switch to graphics mode to release the display:
ret = ioctl(tty_fd, KDSETMODE, KD_GRAPHICS);
▶ And disable keyboard support on the standard input:
ret = ioctl(tty_fd, KDSKBMODE, K_OFF);
▶ The display device can then be used exclusively
▶ Input is no longer interpreted (e.g. Ctrl-C is ignored)
▶ Graphics and keyboard mode must be restored when leaving to keep the VT usable
▶ Current modes can be queried with:
short mode, kbmode;
ret = ioctl(tty_fd, KDGETMODE, &mode);
ret = ioctl(tty_fd, KDGKBMODE, &kbmode);
▶ More details in the console_ioctl man page
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 129/212
Virtual terminals switching and graphics
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 130/212
Software Aspects
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 131/212
Fbdev overview
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 132/212
Fbdev basic operations
▶ Fixed information about the display is retrieved with:
struct fb_fix_screeninfo fix_screeninfo = { 0 };
ret = ioctl(fb_fd, FBIOGET_FSCREENINFO, &fix_screeninfo);
▶ Variable information (including mode) is retrieved and configured with:
struct fb_var_screeninfo var_screeninfo = { 0 };
ret = ioctl(fb_fd, FBIOGET_VSCREENINFO, &var_screeninfo);
...
ret = ioctl(fb_fd, FBIOPUT_VSCREENINFO, &var_screeninfo);
▶ Power management is operated with:
ret = ioctl(fb_fd, FBIOBLANK, FB_BLANK_POWERDOWN);
▶ Double-buffering is sometimes supported (within the same buffer):
var_screeninfo.yoffset = height;
ret = ioctl(fb_fd, FBIOPAN_DISPLAY, &var_screeninfo);
▶ Blocking until the next vblank is possible with:
int vsync = 0;
ret = ioctl(fb_fd, FBIO_WAITFORVSYNC, &vsync);
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 133/212
Fbdev limitations
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 134/212
Software Aspects
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 135/212
DRM devices
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 136/212
DRM driver identification and capabilities
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 137/212
DRM master, magic and authentication
▶ Multiple userspace clients can open the same primary device node
▶ Only the master client is allowed to configure display (KMS)
▶ Master is exclusive and can be acquired and dropped (VT switching):
ret = ioctl(drm_fd, DRM_IOCTL_SET_MASTER, NULL);
ret = ioctl(drm_fd, DRM_IOCTL_DROP_MASTER, NULL);
▶ Requires CAP_SYS_ADMIN Linux capability, see capabilities man page
usually reserved to the root super user
▶ Some operations can be allowed on trusted clients with magic authentication:
• Mostly used before render nodes or for allocating buffers on another process
1. Client foo gets its client-specific magic:
struct drm_auth auth = { 0 };
ret = ioctl(drm_fd, DRM_IOCTL_GET_MAGIC, &auth);
2. Client foo sends auth.magic to master client bar (via IPC)
3. Master client bar authenticates client foo:
ret = ioctl(drm_fd, DRM_IOCTL_AUTH_MAGIC, &auth);
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 138/212
DRM memory management
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 139/212
DRM KMS dumb buffer API
▶ Allocating from width, height and bpp, returning handle, pitch and size:
struct drm_mode_create_dumb create_dumb = { ... };
ret = ioctl(drm_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
▶ Destroying an allocated buffer:
struct drm_mode_destroy_dumb destroy_dumb = { .handle = ..., };
ret = ioctl(drm_fd, DRM_IOCTL_MODE_DESTROY_DUMB, &destroy_dumb);
▶ Preparing a mapping in user memory for a buffer, returning an offset:
struct drm_mode_map_dumb map_dumb = { .handle = ..., };
ret = ioctl(drm_fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb);
▶ Mapping memory to userspace using the offset:
map = mmap(NULL, create_dumb.size, PROT_READ | PROT_WRITE, MAP_SHARED,
drm_fd, map_dumb.offset);
▶ Unmapping memory after use:
munmap(map, create_dumb.size);
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 140/212
DRM FourCCs and modifiers
▶ DRM has its own representation of pixel formats, with FourCC codes (on 32 bits)
▶ Defined in the include/uapi/drm/drm_fourcc.h header
▶ They can specify up to 4 distinct data planes for color components
▶ Pixel formats are named ”MSB-to-LSB” and specified in little-endian order
LSB comes first in memory in little-endian
▶ For instance, DRM_FORMAT_XRGB8888 has the B byte first in memory
Memory order is independent from the CPU or hardware endianness
▶ A format modifier (on 64 bits) indicates the pixel order in memory
▶ DRM_FORMAT_MOD_LINEAR indicates raster order
line-major left-to-right, top-to-bottom
▶ Other modifiers are usually hardware-specific, often tiled
(e.g. DRM_FORMAT_MOD_VIVANTE_TILED)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 141/212
DRM KMS resources probing
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 142/212
DRM KMS connector probing
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 143/212
DRM KMS modes
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 144/212
DRM KMS encoder probing
▶ The next step is to find which CRTC id can be used with the connector
▶ The encoder is the link between the connector and CRTC
▶ Current encoder state can be probed with:
struct drm_mode_get_encoder get_encoder = { .encoder_id = ... };
ret = ioctl(drm_fd, DRM_IOCTL_MODE_GETENCODER, &get_encoder);
▶ struct drm_mode_get_connector exposes some information:
• Encoder type
• Possible CRTCs, currently-attached CRTC
▶ This allows selecting the CRTC to use for the connector!
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 145/212
DRM KMS framebuffer management
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 146/212
DRM KMS CRTC configuration (legacy)
▶ The pipeline can then be configured with the connector and the CRTC
▶ The current CRTC configuration can be retrieved with:
struct drm_mode_crtc crtc = { .crtc_id = ... };
ret = ioctl(drm_fd, DRM_IOCTL_MODE_GETCRTC, &crtc);
▶ The CRTC is configured with the connector id
struct drm_mode_crtc crtc = { .crtc_id = ... };
ret = ioctl(drm_fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
▶ A mode and a framebuffer can be set (previous setup used otherwise)
mandatory if the CRTC was unused before
▶ The kernel will automatically select the best encoder for the connector and CRTC
▶ Legacy and deprecated way to do modesetting: only concerns the primary plane
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 147/212
DRM KMS page flipping (legacy)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 148/212
DRM KMS overlay plane configuration (legacy)
▶ Overlay planes are configured separately from the CRTC main plane
▶ The current state of a plane can be retrieved with:
struct drm_mode_get_plane get_plane = { .plane_id = ... };
ret = ioctl(drm_fd, DRM_IOCTL_MODE_GETPLANE, &get_plane);
▶ Provides possible CRTCs, current framebuffer and supported formats
▶ Planes are configured with source and destination parameters:
• crtc_[xywh]: On-CRTC position and dimensions
• src_[xywh]: In-framebuffer position and dimensions (source clipping area)
▶ Configuration takes place with:
struct drm_mode_set_plane set_plane = { .plane_id = ... };
ret = ioctl(drm_fd, DRM_IOCTL_MODE_SETPLANE, &set_plane);
▶ Legacy and deprecated: not synchronized to vblank or page flip
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 149/212
DRM KMS cursor configuration and position (legacy)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 150/212
DRM event notification and wait
▶ DRM provides an event notification mechanism for vblank and page flip done
▶ Available through the primary (KMS) file descriptor
▶ Can be used with poll and select (integrated in main loop)
▶ Events with a struct drm_event base are read using read
▶ Expand to struct drm_event_vblank for vblank and page flip done events
only complete events are returned, so the buffer must be large enough
▶ Events can be requested at page flip time or explicitly:
union drm_wait_vblank wait_vblank = { .request = ... };
ret = ioctl(drm_fd, DRM_IOCTL_WAIT_VBLANK, &wait_vblank);
▶ A blocking wait for an absolute or relative vblank sequence can also be requested
using the same ioctl and dedicated request.type values
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 151/212
DRM KMS object properties
▶ KMS objects expose generic (or driver-specific) properties with names and values
concerns connectors, CRTCs and planes
• Range properties: limits for the value (signed or unsigned)
• Enum properties: fixed values with associated names for the values
• Blob properties: raw data with a given length
▶ Properties have a unique identifier across objects, details can be queried:
struct drm_mode_obj_get_property get_property = { .prop_id = ... }
ret = ioctl(drm_fd, DRM_IOCTL_MODE_GETPROPERTY, &get_property);
▶ Registered properties of an object can be retrieved using:
struct drm_mode_obj_get_properties get_properties = { .obj_id = ... }
ret = ioctl(drm_fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &get_properties);
▶ The value of a property can be assigned with:
struct drm_mode_obj_set_property set_property = { .obj_id = ..., .prop_id = ... }
ret = ioctl(drm_fd, DRM_IOCTL_MODE_OBJ_SETPROPERTY, &set_property);
▶ Blob properties need to be created and destroyed (with their own identifier)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 152/212
DRM KMS atomic
▶ The legacy API comes with major design issues:
• Overlay and cursor plane updates are applied instantly (tearing)
• Plane updates cannot be synchronized together (intermediate states)
• No way to check that setup is valid before applying it
▶ The atomic API lifts these restrictions with a new paradigm:
• Objects are configured based on their KMS properties
values are affected to each changed property
• Property changes of different objects are grouped in an atomic commit
• Planes are handled regardless of their type (primary, overlay, cursor)
• Commits can be marked for test only: checked but not applied
• Changes are applied at next vblank, unless marked asynchronous
struct drm_mode_atomic atomic = { ... }
ret = ioctl(drm_fd, DRM_IOCTL_MODE_ATOMIC, &atomic);
▶ Unless marked non-blocking, the ioctl returns when changes are applied
▶ A page flip event can also be requested
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 153/212
DRM KMS atomic common properties
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 154/212
DRM KMS atomic driver walkthrough
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 155/212
DRM render generalities
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 156/212
DRM render driver walkthrough
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 157/212
DRM Prime zero-copy memory sharing (dma-buf)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 158/212
DRM sync object fencing
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 159/212
DRM sync object fencing
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 160/212
DRM debug and documentation
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 161/212
Software Aspects
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 162/212
libdrm wrapper
▶ Userspace access to DRM devices is wrapped with libdrm
▶ Exposes convenience wrappers, helpers and some data structures around ioctls
• For KMS support in the libdrm.so library
• For hardware-specific render drivers in dedicated libraries (e.g. libdrm_nouveau.so)
▶ Used by almost every userspace project dealing with DRM:
weston, mutter, Xorg, mesa, etc
libdrm-driver
libdrm
Process
User space
specific driver API generic DRM API (including GEM & KMS API)
register
DRM
core
DRM driver GEM KMS
Video Card
VRAM
Buffer #N framebuffer GPU
© 2015 Javier Cantero - this work is under the Creative Commons Attribution ShareAlike 4.0 license Version 1
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 163/212
Software Aspects
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 164/212
X11 protocol and architecture
▶ X11 core protocol implemented by Xorg:
• Asynchronous packet-based system with different types:
Request, Reply, Event and Error packets
• Can be used locally (UNIX socket) or over network (TCP/IP)
▶ Exposes drawables for clients to transfer or draw pixel data to the server:
• Windows: area of the display buffer owned by the application
without backing storage, must be redrawn when occluded
• Pixmaps: off-display backing storage that can be copied to windows
▶ Windows are represented as a tree:
• Starting with the root window created by X
• Top-level windows and sub-windows created by clients
▶ A graphics context (GC) allows requesting basic drawing and font rendering
▶ The server provides input events to concerned clients:
• Mouse movements relative to window coordinates
• Translated key symbols a from raw keycodes
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 165/212
X11 protocol extensions
▶ X11 has evolved over time through extensions to its main protocol
• Additional interfaces for X clients, matching new hardware features
▶ XKB: complex keyboard layouts
▶ Xinput2: touchpad, touchscreen and multi-touch support
▶ XSHM: shared client/server memory, avoiding extra transfers/copies
not possible to operate via the network
▶ XRandR: monitor configuration and hotplugging without server restart
▶ Composite: delegates window composition to compositing window managers
▶ XRender: 2D rendering API with with alpha composition, rasterization,
transformations, filtering
▶ Xv: video output format conversion and scaling offload in-DDX
involves buffer copies and lacks synchronization with window position
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 166/212
Xorg architecture and acceleration
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 167/212
Xorg drivers overview
▶ Generic Xorg input drivers:
• xf86-input-libinput: using libinput to get input events
• xf86-input-evdev: using the evdev kernel interface directly (deprecated)
▶ Specific Xorg input drivers:
• xf86-input-synaptics: for laptop touchpads
• xf86-input-wacom: for Wacom drawing tablets
• Specific drivers are deprecated in favor of xf86-input-libinput
▶ Generic Xorg display drivers:
• xf86-video-modesetting: for DRM KMS, can be accelerated using glamor
• xf86-video-fbdev: for the fbdev interface, without acceleration (legacy)
• xf86-video-vesa: for the Video BIOS Extension (VBE) framebuffer (x86)
▶ Specific Xorg display drivers:
• xf86-video-[intel,nouveau,amdgpu]: profiting from 2D acceleration blocks
• Specific drivers are deprecated in favor of xf86-video-modesetting and glamor
the trend is to accelerate everything via 3D rendering instead of 2D accelerators
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 168/212
X11 and OpenGL acceleration: GLX and DRI2
▶ Before DRM render nodes, there was a single device for KMS and render
correlates with the idea of a graphics card mixing both aspects
▶ The X server owns the graphics device exclusively
▶ Clients using OpenGL need to access the device for rendering
▶ The GLX API was introduced to perform indirect rendering:
1. Integrating OpenGL with the X Window API
2. Forwarding GL calls to the GL implementation via the X server (AIGLX)
introducing latency and performance issues
▶ The Direct Rendering Infrastructure (DRI/DRI2) was introduced next
• The X server allowed access through DRM magic/auth
• Buffers were shared via GEM flinks
• Now using the standalone render node and dma-buf instead
• Still in place for coordination between render and the display server
▶ GLX remained as a GL windowing API for X11 (deprecated by EGL)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 169/212
X11 and OpenGL acceleration: GLX and DRI2 (illustrated)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 170/212
Xorg usage, integration and configuration
▶ Xorg can be started with the startx command (wrapping xinit)
• Executes server script from /etc/X11/xinit/xserverrc or $HOME/.xserverrc
• Executes client script from /etc/X11/xinit/xinitrc or $HOME/.xinitrc
▶ An X display manager offers a login interface (e.g. KDM, LightDM)
• Runs under a Xorg server, with its own dedicated user
• Starts Xorg for authenticated users from session files in /usr/share/xsessions/
▶ Used to require running the server as root to access graphics devices
in particular, necessary to become DRM master
• The systemd-logind login manager lifts the restriction
• Opens the DRM KMS fd privileged and passes it to Xorg via IPC
• Xorg can then drop privileges: details in the Xorg.wrap man page
▶ Xorg is configured (both DIX and DDX) from /etc/X11/xorg.conf
▶ The DISPLAY environment variable indicates which server connection to use
• Already set for X client applications and inherited
• export DISPLAY=:0 useful to launch programs from other TTYs
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 171/212
Xorg architecture: input to display roundtrip
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 172/212
Major issues with X11
▶ The X11 core protocol and paradigm soon caused various issues:
• Based on buffer copies, transfers and frequent redraws
solved with XSHM and DRI2 extensions
• Immediate-mode drawing, with intermediate states scanned out
solved by drawing everything client-side instead
• Lack of synchronization/feedback interface
specified with the DRI3 and Present extensions
• Everything’s a window with X... but not in practice (screensavers, popups)
specified with the DRI3 and Present extensions
• Heavy packet-based protocol causing latency issues
• Security concerns regarding client input/output isolation
▶ Because the core protocol did not evolve, extensions proliferated:
• Complicated server aspects got delegated through extensions
• Working around major design issues, not solving them in depth
• In the end, the server mostly coordinates between other components
▶ Client-side rendering became more common (raster, operations, fonts, etc)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 173/212
Xorg code structure and walkthrough
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 174/212
Xorg debug and documentation
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 175/212
Software Aspects
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 176/212
Wayland overview and paradigm
▶ Wayland was started in 2008 as a modern replacement for the X Window system
solving issues in-depth with a clean implementation from scratch
▶ Drastic simplification of the stack and paradigm shift:
• The server and compositor are unified as the same component
• Clients are expected to do all the rendering
• Buffers are shared between client and server, no transfers
• Window decorations can be added by the client or the server
▶ Improves security aspects:
• Isolates the input and output of each client
• Only the compositor can access display buffers (and provide screenshots)
• Avoids running the compositor as root (using systemd-logind)
▶ No network support (can be implemented by compositors)
▶ Weston is the reference Wayland compositor
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 177/212
Wayland architecture: input to display roundtrip
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 178/212
Wayland protocol and architecture
▶ Wayland provides a client-server low-level API:
• Wayland display connections happen through local IPC
• Display is identified with the WAYLAND_DISPLAY environment variable
• Asynchronous and object-oriented protocol
• Objects represent resources from the compositor
• Objects implement specific interfaces, with requests and events
Requests are messages sent by the client,
Events are messages received from the server
errors are only specific types of events
▶ Some implementation details:
• IPC is a regular UNIX socket (allows passing file descriptors for zero-copy)
• A proxy provides client-side object representations and message translation
• Messages are serialized (marshalling) to the wire format
• Messages are buffered and flushed/dispatched when asked
▶ Client-server protocol is implemented in libwayland-client and libwayland-server
▶ These libraries do not provide any interface implementation
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 179/212
Wayland core protocol: global object interfaces
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 180/212
Wayland core protocol: specific object interfaces
▶ Input-related (wl_seat) specific core object interfaces:
• wl_pointer: exposes mice events and cursor
• wl_keyboard: exposes keyboard events and information
• wl_touch: exposes touchscreen events
▶ Compositor-related (wl_compositor) specific core object interfaces:
• wl_region: specifies any area
• wl_surface: rectangular on-screen pixel area
contents set to a wl_buffer (can be transformed)
configured with a wl_region for input
configured with a wl_region for area-based opacity
updated with buffer damage regions
• wl_subsurface: converts wl_surface objects to positioned sub-surfaces
▶ Shared memory-related (wl_shm) specific core object interfaces:
• wl_shm_pool: allows creating shared-memory buffers
▶ Memory-related specific core object interfaces:
• wl_buffer: generic object for a wl_surface
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 181/212
Wayland extra protocols
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 182/212
Wayland OpenGL integration
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 183/212
Wayland OpenGL integration (illustrated)
by Shmuel Csaba Otto Traian; GNU FDL 1.2+ and CC-BY-SA 3.0+; created 2013-08-28; updated 2014-02-27
GNOME Video
Clutter 1.17.4 W2EGL EGL
glue
libwayland-client code
Inkscape
GTK+ 3.12 W2EGL EGL
glue
libwayland-client code
② ③ KDE Plasma
② ③
EGL
libwayland-server
Mesa 3D
Enlightenment, ...
OpenGL|ES libGLES-mesa
① ④ ioctl
libDRM
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 184/212
Wayland status and adoption
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 185/212
Wayland stack overview (illustrated)
by Shmuel Csaba Otto Traian; GNU FDL 1.3+ and CC-BY-SA 4.0 International; created 2014-03-23; modified by Matthew Raymond and others; last modified 2017-12-18
Wayland-client X11-client
protocol
libwayland-client and libX or libXCB 3D Game Engine
rendering
direct
libwayland-
rendering XWayland
DIX
client
direct
DDX (hw/xwayland) Vulkan / rendering
OpenGL 4.5 /
② ③ ③ Glamor
OpenGL-based 2D rendering acceleration library,
translates "X rendering" into OpenGL and EGL
OpenGL ES 3.2 Vulkan /
② EGL direct
rendering
OpenGL 4.6 /
OpenGL ES 3.2
OpenGL
EGL
or
direct
libwayland-server rendering EGL
Wayland Compositor
Weston, Mutter, KWin, Enlightenment, ...
Mesa 3D
libinput API config parsing
libGL-nvidia-glx
libinput libGL-amdgpu-pro-glx
pointer acceleration device discovery
pointer touchpad tablet
user mode
① ④ ioctl udev
libDRM
AMDGPU-PRO Only
kernel mode
event0
Linux kernel
event1
evdev event2 KMS DRM proprietary BLOB
event3
event4
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 186/212
Wayland debug and documentation
▶ Debugging tips:
• Supported global object interfaces can be listed with weston-info
• The WAYLAND_DEBUG environment variable enables protocol tracing
▶ Weston debugging:
• Debug arguments: --debug, --log=file.log
• Grabbing a different TTY argument: --tty 1
• Wireframe keystroke: mod + shift + space + F
• Timeline recording (to a JSON file) keystroke: mod + shift + space + d
can produce a graph with https://github.com/ppaalanen/wesgr
▶ Community contact:
• Mailing list: [email protected]
• IRC channel: #wayland on the OFTC network
▶ Documentation resources:
• Online wiki of the project: https://wayland.freedesktop.org/
• Online documentation: https://wayland.freedesktop.org/docs/html/
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 187/212
Software Aspects
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 188/212
Standardized 3D rendering APIs: OpenGL
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 189/212
Standardized 3D rendering APIs: OpenGL
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 190/212
Standardized 3D rendering APIs: OpenGL ES and EGL
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 191/212
Standardized 3D rendering APIs: Vulkan
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 193/212
Mesa 3D implementation highlights
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 194/212
Mesa 3D internals: Gallium 3D
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 195/212
Mesa 3D internals: Gallium 3D (illustrated)
by Shmuel Csaba Otto Traian; GNU FDL 1.2+ and CC-BY-SA 3.0+; created 2013-08-26, updated 2013-10-30
Application
API: OpenGL
libGL
Gallium3D-style
API State Tracker
DRI-1.0-style GPU-specific
Device Driver Device Driver
OS WinSys
Kernel DRM
CPU & registers & L1 & L2 & L3 & L4 & main memory
GPU & registers & L1 & L2 (& graphic memory)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 196/212
Mesa 3D internals: intermediate representations
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 197/212
Mesa 3D internals: intermediate representations (illustrated)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 198/212
Mesa 3D Generic Buffer Management
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 199/212
Mesa 3D hardware support status: desktop
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 200/212
Mesa 3D hardware support status: desktop
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 201/212
Mesa 3D hardware support status: embedded
▶ Qualcomm Adreno
• Platforms: Qualcomm Snapdragon
• Mesa driver: freedreno (Gallium)
• DRM driver: freedreno
• Status: reverse engineered, advanced
▶ Vivante GCx000
• Platforms: i.MX6, i.MX8, i.MX8M
• Driver: etnaviv (Gallium)
• DRM driver: etnaviv
• Status: vastly usable
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 202/212
Mesa 3D hardware support status: embedded
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 203/212
Mesa 3D versus proprietary implementations
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 204/212
Mesa 3D code structure and walkthrough
▶ Mesa source code available at: https://gitlab.freedesktop.org/mesa/mesa
▶ Gallium 3D components:
• Drivers under: src/gallium/drivers/
• Winsys under: src/gallium/winsys/
• API state trackers under: src/gallium/state_trackers/
• Pipe loaders under: src/gallium/auxiliary/pipe-loader/
▶ Compilation and IR components:
• IR compiler support under: src/compiler/{glsl,nir,spirv}
• TGSI support under: src/gallium/auxiliary/tgsi/
• State tracking between IRs under: src/mesa/state_tracker
▶ Windowing and DRI2 components:
• EGL support under: src/egl/drivers/dri2/
• Vulkan WSI support under: src/vulkan/wsi/
▶ Classic drivers (DRI-1-style) under: src/mesa/drivers/dri/
▶ GBM support under: src/gbm/
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 205/212
Mesa 3D hardware support: debug and documentation
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 206/212
Graphics software online references
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 207/212
Graphics software illustrations attributions
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 208/212
Graphics software illustrations attributions
▶ Devuan GNU-Linux - tty login - server rack: Francesco Magno, CC BY-SA 4.0
▶ DRM architecture: Javier Cantero CC BY-SA 4.0
▶ Wayland architecture: Wayland Developers, GNU GPL 2+
▶ X architecture: Wayland Developers, GNU GPL 2+
▶ Wayland display server protocol: Shmuel Csaba Otto Traian, CC BY-SA 3.0
▶ The Linux Graphics Stack and glamor: Shmuel Csaba Otto Traian, CC BY-SA 3.0
▶ Khronos logo pack
▶ Gallium3D vs DRI graphics driver model, CC BY-SA 3.0
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 209/212
Last slides
Last slides
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 210/212
Last slide
Thank you!
And may the Source be with you
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 211/212
Rights to copy
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 212/212
Benchmarking, testing and validation tools
▶ DRM kernel aspects (display and render):
• IGT GPU Tools (IGT): main DRM test suite, used for CI
https://gitlab.freedesktop.org/drm/igt-gpu-tools
▶ OpenGL aspects:
• drawElements Quality Program (dEQP): OpenGL/OpenGL ES/Vulkan
conformance tests
https://android.googlesource.com/platform/external/deqp
• glmark2: OpenGL 2.0 and ES 2.0 benchmark tool
https://github.com/glmark2/glmark2
▶ Patch series continuous integration:
• EzBench: a collection of tools to benchmark graphics-related patch-series
https://github.com/freedesktop/ezbench
▶ General benchmarking (including graphics):
• Phoronix Test Suite: automated benchmarking tool
https://github.com/phoronix-test-suite/phoronix-test-suite
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 1/1