141687f09Smrg=======
241687f09Smrgdrm-kms
341687f09Smrg=======
441687f09Smrg
541687f09Smrg-------------------
641687f09SmrgKernel Mode-Setting
741687f09Smrg-------------------
841687f09Smrg
941687f09Smrg:Date: September 2012
1041687f09Smrg:Manual section: 7
1141687f09Smrg:Manual group: Direct Rendering Manager
1241687f09Smrg
1341687f09SmrgSynopsis
1441687f09Smrg========
1541687f09Smrg
1641687f09Smrg``#include <xf86drm.h>``
1741687f09Smrg
1841687f09Smrg``#include <xf86drmMode.h>``
1941687f09Smrg
2041687f09SmrgDescription
2141687f09Smrg===========
2241687f09Smrg
2341687f09SmrgEach DRM device provides access to manage which monitors and displays are
2441687f09Smrgcurrently used and what frames to be displayed. This task is called *Kernel
2541687f09SmrgMode-Setting* (KMS). Historically, this was done in user-space and called
2641687f09Smrg*User-space Mode-Setting* (UMS). Almost all open-source drivers now provide the
2741687f09SmrgKMS kernel API to do this in the kernel, however, many non-open-source binary
2841687f09Smrgdrivers from different vendors still do not support this. You can use
2941687f09Smrg**drmModeSettingSupported**\ (3) to check whether your driver supports this. To
3041687f09Smrgunderstand how KMS works, we need to introduce 5 objects: *CRTCs*, *Planes*,
3141687f09Smrg*Encoders*, *Connectors* and *Framebuffers*.
3241687f09Smrg
3341687f09SmrgCRTCs
3441687f09Smrg   A *CRTC* short for *CRT Controller* is an abstraction representing a part of
3541687f09Smrg   the chip that contains a pointer to a scanout buffer.  Therefore, the number
3641687f09Smrg   of CRTCs available determines how many independent scanout buffers can be
3741687f09Smrg   active at any given time. The CRTC structure contains several fields to
3841687f09Smrg   support this: a pointer to some video memory (abstracted as a frame-buffer
3941687f09Smrg   object), a list of driven connectors, a display mode and an (x, y) offset
4041687f09Smrg   into the video memory to support panning or configurations where one piece
4141687f09Smrg   of video memory spans multiple CRTCs. A CRTC is the central point where
4241687f09Smrg   configuration of displays happens. You select which objects to use, which
4341687f09Smrg   modes and which parameters and then configure each CRTC via
4441687f09Smrg   **drmModeCrtcSet**\ (3) to drive the display devices.
4541687f09Smrg
4641687f09SmrgPlanes
4741687f09Smrg   A *plane* respresents an image source that can be blended with or overlayed
4841687f09Smrg   on top of a CRTC during the scanout process. Planes are associated with a
4941687f09Smrg   frame-buffer to crop a portion of the image memory (source) and optionally
5041687f09Smrg   scale it to a destination size. The result is then blended with or overlayed
5141687f09Smrg   on top of a CRTC. Planes are not provided by all hardware and the number of
5241687f09Smrg   available planes is limited. If planes are not available or if not enough
5341687f09Smrg   planes are available, the user should fall back to normal software blending
5441687f09Smrg   (via GPU or CPU).
5541687f09Smrg
5641687f09SmrgEncoders
5741687f09Smrg   An *encoder* takes pixel data from a CRTC and converts it to a format
5841687f09Smrg   suitable for any attached connectors. On some devices, it may be possible to
5941687f09Smrg   have a CRTC send data to more than one encoder. In that case, both encoders
6041687f09Smrg   would receive data from the same scanout buffer, resulting in a *cloned*
6141687f09Smrg   display configuration across the connectors attached to each encoder.
6241687f09Smrg
6341687f09SmrgConnectors
6441687f09Smrg   A *connector* is the final destination of pixel-data on a device, and
6541687f09Smrg   usually connects directly to an external display device like a monitor or
6641687f09Smrg   laptop panel. A connector can only be attached to one encoder at a time. The
6741687f09Smrg   connector is also the structure where information about the attached display
6841687f09Smrg   is kept, so it contains fields for display data, *EDID* data, *DPMS* and
6941687f09Smrg   *connection status*, and information about modes supported on the attached
7041687f09Smrg   displays.
7141687f09Smrg
7241687f09SmrgFramebuffers
7341687f09Smrg   *Framebuffers* are abstract memory objects that provide a source of pixel
7441687f09Smrg   data to scanout to a CRTC. Applications explicitly request the creation of
7541687f09Smrg   framebuffers and can control their behavior. Framebuffers rely on the
7641687f09Smrg   underneath memory manager for low-level memory operations. When creating a
7741687f09Smrg   framebuffer, applications pass a memory handle through the API which is used
7841687f09Smrg   as backing storage. The framebuffer itself is only an abstract object with
7941687f09Smrg   no data. It just refers to memory buffers that must be created with the
8041687f09Smrg   **drm-memory**\ (7) API.
8141687f09Smrg
8241687f09SmrgMode-Setting
8341687f09Smrg------------
8441687f09Smrg
8541687f09SmrgBefore mode-setting can be performed, an application needs to call
8641687f09Smrg**drmSetMaster**\ (3) to become *DRM-Master*. It then has exclusive access to
8741687f09Smrgthe KMS API. A call to **drmModeGetResources**\ (3) returns a list of *CRTCs*,
8841687f09Smrg*Connectors*, *Encoders* and *Planes*.
8941687f09Smrg
9041687f09SmrgNormal procedure now includes: First, you select which connectors you want to
9141687f09Smrguse. Users are mostly interested in which monitor or display-panel is active so
9241687f09Smrgyou need to make sure to arrange them in the correct logical order and select
9341687f09Smrgthe correct ones to use. For each connector, you need to find a CRTC to drive
9441687f09Smrgthis connector. If you want to clone output to two or more connectors, you may
9541687f09Smrguse a single CRTC for all cloned connectors (if the hardware supports this). To
9641687f09Smrgfind a suitable CRTC, you need to iterate over the list of encoders that are
9741687f09Smrgavailable for each connector. Each encoder contains a list of CRTCs that it can
9841687f09Smrgwork with and you simply select one of these CRTCs. If you later program the
9941687f09SmrgCRTC to control a connector, it automatically selects the best encoder.
10041687f09SmrgHowever, this procedure is needed so your CRTC has at least one working encoder
10141687f09Smrgfor the selected connector. See the *Examples* section below for more
10241687f09Smrginformation.
10341687f09Smrg
10441687f09SmrgAll valid modes for a connector can be retrieved with a call to
1050ed5401bSmrg**drmModeGetConnector**\ (3) You need to select the mode you want to use and save it.
10641687f09SmrgThe first mode in the list is the default mode with the highest resolution
10741687f09Smrgpossible and often a suitable choice.
10841687f09Smrg
10941687f09SmrgAfter you have a working connector+CRTC+mode combination, you need to create a
11041687f09Smrgframebuffer that is used for scanout. Memory buffer allocation is
1110ed5401bSmrgdriver-dependent and described in **drm-memory**\ (7). You need to create a
11241687f09Smrgbuffer big enough for your selected mode. Now you can create a framebuffer
11341687f09Smrgobject that uses your memory-buffer as scanout buffer. You can do this with
11441687f09Smrg**drmModeAddFB**\ (3) and **drmModeAddFB2**\ (3).
11541687f09Smrg
11641687f09SmrgAs a last step, you want to program your CRTC to drive your selected connector.
11741687f09SmrgYou can do this with a call to **drmModeSetCrtc**\ (3).
11841687f09Smrg
11941687f09SmrgPage-Flipping
12041687f09Smrg-------------
12141687f09Smrg
12241687f09SmrgA call to **drmModeSetCrtc**\ (3) is executed immediately and forces the CRTC
12341687f09Smrgto use the new scanout buffer. If you want smooth-transitions without tearing,
12441687f09Smrgyou probably use double-buffering. You need to create one framebuffer object
12541687f09Smrgfor each buffer you use. You can then call **drmModeSetCrtc**\ (3) on the next
12641687f09Smrgbuffer to flip. If you want to synchronize your flips with *vertical-blanks*,
12741687f09Smrgyou can use **drmModePageFlip**\ (3) which schedules your page-flip for the
12841687f09Smrgnext *vblank*.
12941687f09Smrg
13041687f09SmrgPlanes
13141687f09Smrg------
13241687f09Smrg
13341687f09SmrgPlanes are controlled independently from CRTCs. That is, a call to
13441687f09Smrg**drmModeSetCrtc**\ (3) does not affect planes. Instead, you need to call
13541687f09Smrg**drmModeSetPlane**\ (3) to configure a plane. This requires the plane ID, a
13641687f09SmrgCRTC, a framebuffer and offsets into the plane-framebuffer and the
13741687f09SmrgCRTC-framebuffer. The CRTC then blends the content from the plane over the CRTC
13841687f09Smrgframebuffer buffer during scanout. As this does not involve any
13941687f09Smrgsoftware-blending, it is way faster than traditional blending. However, plane
14041687f09Smrgresources are limited. See **drmModeGetPlaneResources**\ (3) for more
14141687f09Smrginformation.
14241687f09Smrg
14341687f09SmrgCursors
14441687f09Smrg-------
14541687f09Smrg
14641687f09SmrgSimilar to planes, many hardware also supports cursors. A cursor is a very
14741687f09Smrgsmall buffer with an image that is blended over the CRTC framebuffer. You can
14841687f09Smrgset a different cursor for each CRTC with **drmModeSetCursor**\ (3) and move it
14941687f09Smrgon the screen with **drmModeMoveCursor**\ (3).  This allows to move the cursor
15041687f09Smrgon the screen without rerendering. If no hardware cursors are supported, you
15141687f09Smrgneed to rerender for each frame the cursor is moved.
15241687f09Smrg
15341687f09SmrgExamples
15441687f09Smrg========
15541687f09Smrg
15641687f09SmrgSome examples of how basic mode-setting can be done. See the man-page of each
15741687f09SmrgDRM function for more information.
15841687f09Smrg
15941687f09SmrgCRTC/Encoder Selection
16041687f09Smrg----------------------
16141687f09Smrg
16241687f09SmrgIf you retrieved all display configuration information via
16341687f09Smrg**drmModeGetResources**\ (3) as ``drmModeRes *res``, selected a connector from
16441687f09Smrgthe list in ``res->connectors`` and retrieved the connector-information as
16541687f09Smrg``drmModeConnector *conn`` via **drmModeGetConnector**\ (3) then this example
16641687f09Smrgshows, how you can find a suitable CRTC id to drive this connector. This
16741687f09Smrgfunction takes a file-descriptor to the DRM device (see **drmOpen**\ (3)) as
16841687f09Smrg``fd``, a pointer to the retrieved resources as ``res`` and a pointer to the
16941687f09Smrgselected connector as ``conn``. It returns an integer smaller than 0 on
17041687f09Smrgfailure, otherwise, a valid CRTC id is returned.
17141687f09Smrg
17241687f09Smrg::
17341687f09Smrg
17441687f09Smrg   static int modeset_find_crtc(int fd, drmModeRes *res, drmModeConnector *conn)
17541687f09Smrg   {
17641687f09Smrg       drmModeEncoder *enc;
17741687f09Smrg       unsigned int i, j;
17841687f09Smrg
17941687f09Smrg       /* iterate all encoders of this connector */
18041687f09Smrg       for (i = 0; i < conn->count_encoders; ++i) {
18141687f09Smrg           enc = drmModeGetEncoder(fd, conn->encoders[i]);
18241687f09Smrg           if (!enc) {
18341687f09Smrg               /* cannot retrieve encoder, ignoring... */
18441687f09Smrg               continue;
18541687f09Smrg           }
18641687f09Smrg
18741687f09Smrg           /* iterate all global CRTCs */
18841687f09Smrg           for (j = 0; j < res->count_crtcs; ++j) {
18941687f09Smrg               /* check whether this CRTC works with the encoder */
19041687f09Smrg               if (!(enc->possible_crtcs & (1 << j)))
19141687f09Smrg                   continue;
19241687f09Smrg
19341687f09Smrg
19441687f09Smrg               /* Here you need to check that no other connector
19541687f09Smrg                * currently uses the CRTC with id "crtc". If you intend
19641687f09Smrg                * to drive one connector only, then you can skip this
19741687f09Smrg                * step. Otherwise, simply scan your list of configured
19841687f09Smrg                * connectors and CRTCs whether this CRTC is already
19941687f09Smrg                * used. If it is, then simply continue the search here. */
20041687f09Smrg               if (res->crtcs[j] "is unused") {
20141687f09Smrg                   drmModeFreeEncoder(enc);
20241687f09Smrg                   return res->crtcs[j];
20341687f09Smrg               }
20441687f09Smrg           }
20541687f09Smrg
20641687f09Smrg           drmModeFreeEncoder(enc);
20741687f09Smrg       }
20841687f09Smrg
20941687f09Smrg       /* cannot find a suitable CRTC */
21041687f09Smrg       return -ENOENT;
21141687f09Smrg   }
21241687f09Smrg
21341687f09SmrgReporting Bugs
21441687f09Smrg==============
21541687f09Smrg
21641687f09SmrgBugs in this manual should be reported to
21741687f09Smrghttps://gitlab.freedesktop.org/mesa/drm/-/issues
21841687f09Smrg
21941687f09SmrgSee Also
22041687f09Smrg========
22141687f09Smrg
22241687f09Smrg**drm**\ (7), **drm-memory**\ (7), **drmModeGetResources**\ (3),
22341687f09Smrg**drmModeGetConnector**\ (3), **drmModeGetEncoder**\ (3),
22441687f09Smrg**drmModeGetCrtc**\ (3), **drmModeSetCrtc**\ (3), **drmModeGetFB**\ (3),
22541687f09Smrg**drmModeAddFB**\ (3), **drmModeAddFB2**\ (3), **drmModeRmFB**\ (3),
22641687f09Smrg**drmModePageFlip**\ (3), **drmModeGetPlaneResources**\ (3),
22741687f09Smrg**drmModeGetPlane**\ (3), **drmModeSetPlane**\ (3), **drmModeSetCursor**\ (3),
22841687f09Smrg**drmModeMoveCursor**\ (3), **drmSetMaster**\ (3), **drmAvailable**\ (3),
22941687f09Smrg**drmCheckModesettingSupported**\ (3), **drmOpen**\ (3)
230