xref: /aosp_15_r20/external/libdrm/man/drm-kms.7.rst (revision 7688df22e49036ff52a766b7101da3a49edadb8c)
1*7688df22SAndroid Build Coastguard Worker=======
2*7688df22SAndroid Build Coastguard Workerdrm-kms
3*7688df22SAndroid Build Coastguard Worker=======
4*7688df22SAndroid Build Coastguard Worker
5*7688df22SAndroid Build Coastguard Worker-------------------
6*7688df22SAndroid Build Coastguard WorkerKernel Mode-Setting
7*7688df22SAndroid Build Coastguard Worker-------------------
8*7688df22SAndroid Build Coastguard Worker
9*7688df22SAndroid Build Coastguard Worker:Date: September 2012
10*7688df22SAndroid Build Coastguard Worker:Manual section: 7
11*7688df22SAndroid Build Coastguard Worker:Manual group: Direct Rendering Manager
12*7688df22SAndroid Build Coastguard Worker
13*7688df22SAndroid Build Coastguard WorkerSynopsis
14*7688df22SAndroid Build Coastguard Worker========
15*7688df22SAndroid Build Coastguard Worker
16*7688df22SAndroid Build Coastguard Worker``#include <xf86drm.h>``
17*7688df22SAndroid Build Coastguard Worker
18*7688df22SAndroid Build Coastguard Worker``#include <xf86drmMode.h>``
19*7688df22SAndroid Build Coastguard Worker
20*7688df22SAndroid Build Coastguard WorkerDescription
21*7688df22SAndroid Build Coastguard Worker===========
22*7688df22SAndroid Build Coastguard Worker
23*7688df22SAndroid Build Coastguard WorkerEach DRM device provides access to manage which monitors and displays are
24*7688df22SAndroid Build Coastguard Workercurrently used and what frames to be displayed. This task is called *Kernel
25*7688df22SAndroid Build Coastguard WorkerMode-Setting* (KMS). Historically, this was done in user-space and called
26*7688df22SAndroid Build Coastguard Worker*User-space Mode-Setting* (UMS). Almost all open-source drivers now provide the
27*7688df22SAndroid Build Coastguard WorkerKMS kernel API to do this in the kernel, however, many non-open-source binary
28*7688df22SAndroid Build Coastguard Workerdrivers from different vendors still do not support this. You can use
29*7688df22SAndroid Build Coastguard Worker**drmModeSettingSupported**\ (3) to check whether your driver supports this. To
30*7688df22SAndroid Build Coastguard Workerunderstand how KMS works, we need to introduce 5 objects: *CRTCs*, *Planes*,
31*7688df22SAndroid Build Coastguard Worker*Encoders*, *Connectors* and *Framebuffers*.
32*7688df22SAndroid Build Coastguard Worker
33*7688df22SAndroid Build Coastguard WorkerCRTCs
34*7688df22SAndroid Build Coastguard Worker   A *CRTC* short for *CRT Controller* is an abstraction representing a part of
35*7688df22SAndroid Build Coastguard Worker   the chip that contains a pointer to a scanout buffer.  Therefore, the number
36*7688df22SAndroid Build Coastguard Worker   of CRTCs available determines how many independent scanout buffers can be
37*7688df22SAndroid Build Coastguard Worker   active at any given time. The CRTC structure contains several fields to
38*7688df22SAndroid Build Coastguard Worker   support this: a pointer to some video memory (abstracted as a frame-buffer
39*7688df22SAndroid Build Coastguard Worker   object), a list of driven connectors, a display mode and an (x, y) offset
40*7688df22SAndroid Build Coastguard Worker   into the video memory to support panning or configurations where one piece
41*7688df22SAndroid Build Coastguard Worker   of video memory spans multiple CRTCs. A CRTC is the central point where
42*7688df22SAndroid Build Coastguard Worker   configuration of displays happens. You select which objects to use, which
43*7688df22SAndroid Build Coastguard Worker   modes and which parameters and then configure each CRTC via
44*7688df22SAndroid Build Coastguard Worker   **drmModeCrtcSet**\ (3) to drive the display devices.
45*7688df22SAndroid Build Coastguard Worker
46*7688df22SAndroid Build Coastguard WorkerPlanes
47*7688df22SAndroid Build Coastguard Worker   A *plane* respresents an image source that can be blended with or overlayed
48*7688df22SAndroid Build Coastguard Worker   on top of a CRTC during the scanout process. Planes are associated with a
49*7688df22SAndroid Build Coastguard Worker   frame-buffer to crop a portion of the image memory (source) and optionally
50*7688df22SAndroid Build Coastguard Worker   scale it to a destination size. The result is then blended with or overlayed
51*7688df22SAndroid Build Coastguard Worker   on top of a CRTC. Planes are not provided by all hardware and the number of
52*7688df22SAndroid Build Coastguard Worker   available planes is limited. If planes are not available or if not enough
53*7688df22SAndroid Build Coastguard Worker   planes are available, the user should fall back to normal software blending
54*7688df22SAndroid Build Coastguard Worker   (via GPU or CPU).
55*7688df22SAndroid Build Coastguard Worker
56*7688df22SAndroid Build Coastguard WorkerEncoders
57*7688df22SAndroid Build Coastguard Worker   An *encoder* takes pixel data from a CRTC and converts it to a format
58*7688df22SAndroid Build Coastguard Worker   suitable for any attached connectors. On some devices, it may be possible to
59*7688df22SAndroid Build Coastguard Worker   have a CRTC send data to more than one encoder. In that case, both encoders
60*7688df22SAndroid Build Coastguard Worker   would receive data from the same scanout buffer, resulting in a *cloned*
61*7688df22SAndroid Build Coastguard Worker   display configuration across the connectors attached to each encoder.
62*7688df22SAndroid Build Coastguard Worker
63*7688df22SAndroid Build Coastguard WorkerConnectors
64*7688df22SAndroid Build Coastguard Worker   A *connector* is the final destination of pixel-data on a device, and
65*7688df22SAndroid Build Coastguard Worker   usually connects directly to an external display device like a monitor or
66*7688df22SAndroid Build Coastguard Worker   laptop panel. A connector can only be attached to one encoder at a time. The
67*7688df22SAndroid Build Coastguard Worker   connector is also the structure where information about the attached display
68*7688df22SAndroid Build Coastguard Worker   is kept, so it contains fields for display data, *EDID* data, *DPMS* and
69*7688df22SAndroid Build Coastguard Worker   *connection status*, and information about modes supported on the attached
70*7688df22SAndroid Build Coastguard Worker   displays.
71*7688df22SAndroid Build Coastguard Worker
72*7688df22SAndroid Build Coastguard WorkerFramebuffers
73*7688df22SAndroid Build Coastguard Worker   *Framebuffers* are abstract memory objects that provide a source of pixel
74*7688df22SAndroid Build Coastguard Worker   data to scanout to a CRTC. Applications explicitly request the creation of
75*7688df22SAndroid Build Coastguard Worker   framebuffers and can control their behavior. Framebuffers rely on the
76*7688df22SAndroid Build Coastguard Worker   underneath memory manager for low-level memory operations. When creating a
77*7688df22SAndroid Build Coastguard Worker   framebuffer, applications pass a memory handle through the API which is used
78*7688df22SAndroid Build Coastguard Worker   as backing storage. The framebuffer itself is only an abstract object with
79*7688df22SAndroid Build Coastguard Worker   no data. It just refers to memory buffers that must be created with the
80*7688df22SAndroid Build Coastguard Worker   **drm-memory**\ (7) API.
81*7688df22SAndroid Build Coastguard Worker
82*7688df22SAndroid Build Coastguard WorkerMode-Setting
83*7688df22SAndroid Build Coastguard Worker------------
84*7688df22SAndroid Build Coastguard Worker
85*7688df22SAndroid Build Coastguard WorkerBefore mode-setting can be performed, an application needs to call
86*7688df22SAndroid Build Coastguard Worker**drmSetMaster**\ (3) to become *DRM-Master*. It then has exclusive access to
87*7688df22SAndroid Build Coastguard Workerthe KMS API. A call to **drmModeGetResources**\ (3) returns a list of *CRTCs*,
88*7688df22SAndroid Build Coastguard Worker*Connectors*, *Encoders* and *Planes*.
89*7688df22SAndroid Build Coastguard Worker
90*7688df22SAndroid Build Coastguard WorkerNormal procedure now includes: First, you select which connectors you want to
91*7688df22SAndroid Build Coastguard Workeruse. Users are mostly interested in which monitor or display-panel is active so
92*7688df22SAndroid Build Coastguard Workeryou need to make sure to arrange them in the correct logical order and select
93*7688df22SAndroid Build Coastguard Workerthe correct ones to use. For each connector, you need to find a CRTC to drive
94*7688df22SAndroid Build Coastguard Workerthis connector. If you want to clone output to two or more connectors, you may
95*7688df22SAndroid Build Coastguard Workeruse a single CRTC for all cloned connectors (if the hardware supports this). To
96*7688df22SAndroid Build Coastguard Workerfind a suitable CRTC, you need to iterate over the list of encoders that are
97*7688df22SAndroid Build Coastguard Workeravailable for each connector. Each encoder contains a list of CRTCs that it can
98*7688df22SAndroid Build Coastguard Workerwork with and you simply select one of these CRTCs. If you later program the
99*7688df22SAndroid Build Coastguard WorkerCRTC to control a connector, it automatically selects the best encoder.
100*7688df22SAndroid Build Coastguard WorkerHowever, this procedure is needed so your CRTC has at least one working encoder
101*7688df22SAndroid Build Coastguard Workerfor the selected connector. See the *Examples* section below for more
102*7688df22SAndroid Build Coastguard Workerinformation.
103*7688df22SAndroid Build Coastguard Worker
104*7688df22SAndroid Build Coastguard WorkerAll valid modes for a connector can be retrieved with a call to
105*7688df22SAndroid Build Coastguard Worker**drmModeGetConnector**\ (3) You need to select the mode you want to use and save it.
106*7688df22SAndroid Build Coastguard WorkerThe first mode in the list is the default mode with the highest resolution
107*7688df22SAndroid Build Coastguard Workerpossible and often a suitable choice.
108*7688df22SAndroid Build Coastguard Worker
109*7688df22SAndroid Build Coastguard WorkerAfter you have a working connector+CRTC+mode combination, you need to create a
110*7688df22SAndroid Build Coastguard Workerframebuffer that is used for scanout. Memory buffer allocation is
111*7688df22SAndroid Build Coastguard Workerdriver-dependent and described in **drm-memory**\ (7). You need to create a
112*7688df22SAndroid Build Coastguard Workerbuffer big enough for your selected mode. Now you can create a framebuffer
113*7688df22SAndroid Build Coastguard Workerobject that uses your memory-buffer as scanout buffer. You can do this with
114*7688df22SAndroid Build Coastguard Worker**drmModeAddFB**\ (3) and **drmModeAddFB2**\ (3).
115*7688df22SAndroid Build Coastguard Worker
116*7688df22SAndroid Build Coastguard WorkerAs a last step, you want to program your CRTC to drive your selected connector.
117*7688df22SAndroid Build Coastguard WorkerYou can do this with a call to **drmModeSetCrtc**\ (3).
118*7688df22SAndroid Build Coastguard Worker
119*7688df22SAndroid Build Coastguard WorkerPage-Flipping
120*7688df22SAndroid Build Coastguard Worker-------------
121*7688df22SAndroid Build Coastguard Worker
122*7688df22SAndroid Build Coastguard WorkerA call to **drmModeSetCrtc**\ (3) is executed immediately and forces the CRTC
123*7688df22SAndroid Build Coastguard Workerto use the new scanout buffer. If you want smooth-transitions without tearing,
124*7688df22SAndroid Build Coastguard Workeryou probably use double-buffering. You need to create one framebuffer object
125*7688df22SAndroid Build Coastguard Workerfor each buffer you use. You can then call **drmModeSetCrtc**\ (3) on the next
126*7688df22SAndroid Build Coastguard Workerbuffer to flip. If you want to synchronize your flips with *vertical-blanks*,
127*7688df22SAndroid Build Coastguard Workeryou can use **drmModePageFlip**\ (3) which schedules your page-flip for the
128*7688df22SAndroid Build Coastguard Workernext *vblank*.
129*7688df22SAndroid Build Coastguard Worker
130*7688df22SAndroid Build Coastguard WorkerPlanes
131*7688df22SAndroid Build Coastguard Worker------
132*7688df22SAndroid Build Coastguard Worker
133*7688df22SAndroid Build Coastguard WorkerPlanes are controlled independently from CRTCs. That is, a call to
134*7688df22SAndroid Build Coastguard Worker**drmModeSetCrtc**\ (3) does not affect planes. Instead, you need to call
135*7688df22SAndroid Build Coastguard Worker**drmModeSetPlane**\ (3) to configure a plane. This requires the plane ID, a
136*7688df22SAndroid Build Coastguard WorkerCRTC, a framebuffer and offsets into the plane-framebuffer and the
137*7688df22SAndroid Build Coastguard WorkerCRTC-framebuffer. The CRTC then blends the content from the plane over the CRTC
138*7688df22SAndroid Build Coastguard Workerframebuffer buffer during scanout. As this does not involve any
139*7688df22SAndroid Build Coastguard Workersoftware-blending, it is way faster than traditional blending. However, plane
140*7688df22SAndroid Build Coastguard Workerresources are limited. See **drmModeGetPlaneResources**\ (3) for more
141*7688df22SAndroid Build Coastguard Workerinformation.
142*7688df22SAndroid Build Coastguard Worker
143*7688df22SAndroid Build Coastguard WorkerCursors
144*7688df22SAndroid Build Coastguard Worker-------
145*7688df22SAndroid Build Coastguard Worker
146*7688df22SAndroid Build Coastguard WorkerSimilar to planes, many hardware also supports cursors. A cursor is a very
147*7688df22SAndroid Build Coastguard Workersmall buffer with an image that is blended over the CRTC framebuffer. You can
148*7688df22SAndroid Build Coastguard Workerset a different cursor for each CRTC with **drmModeSetCursor**\ (3) and move it
149*7688df22SAndroid Build Coastguard Workeron the screen with **drmModeMoveCursor**\ (3).  This allows to move the cursor
150*7688df22SAndroid Build Coastguard Workeron the screen without rerendering. If no hardware cursors are supported, you
151*7688df22SAndroid Build Coastguard Workerneed to rerender for each frame the cursor is moved.
152*7688df22SAndroid Build Coastguard Worker
153*7688df22SAndroid Build Coastguard WorkerExamples
154*7688df22SAndroid Build Coastguard Worker========
155*7688df22SAndroid Build Coastguard Worker
156*7688df22SAndroid Build Coastguard WorkerSome examples of how basic mode-setting can be done. See the man-page of each
157*7688df22SAndroid Build Coastguard WorkerDRM function for more information.
158*7688df22SAndroid Build Coastguard Worker
159*7688df22SAndroid Build Coastguard WorkerCRTC/Encoder Selection
160*7688df22SAndroid Build Coastguard Worker----------------------
161*7688df22SAndroid Build Coastguard Worker
162*7688df22SAndroid Build Coastguard WorkerIf you retrieved all display configuration information via
163*7688df22SAndroid Build Coastguard Worker**drmModeGetResources**\ (3) as ``drmModeRes *res``, selected a connector from
164*7688df22SAndroid Build Coastguard Workerthe list in ``res->connectors`` and retrieved the connector-information as
165*7688df22SAndroid Build Coastguard Worker``drmModeConnector *conn`` via **drmModeGetConnector**\ (3) then this example
166*7688df22SAndroid Build Coastguard Workershows, how you can find a suitable CRTC id to drive this connector. This
167*7688df22SAndroid Build Coastguard Workerfunction takes a file-descriptor to the DRM device (see **drmOpen**\ (3)) as
168*7688df22SAndroid Build Coastguard Worker``fd``, a pointer to the retrieved resources as ``res`` and a pointer to the
169*7688df22SAndroid Build Coastguard Workerselected connector as ``conn``. It returns an integer smaller than 0 on
170*7688df22SAndroid Build Coastguard Workerfailure, otherwise, a valid CRTC id is returned.
171*7688df22SAndroid Build Coastguard Worker
172*7688df22SAndroid Build Coastguard Worker::
173*7688df22SAndroid Build Coastguard Worker
174*7688df22SAndroid Build Coastguard Worker   static int modeset_find_crtc(int fd, drmModeRes *res, drmModeConnector *conn)
175*7688df22SAndroid Build Coastguard Worker   {
176*7688df22SAndroid Build Coastguard Worker       drmModeEncoder *enc;
177*7688df22SAndroid Build Coastguard Worker       unsigned int i, j;
178*7688df22SAndroid Build Coastguard Worker
179*7688df22SAndroid Build Coastguard Worker       /* iterate all encoders of this connector */
180*7688df22SAndroid Build Coastguard Worker       for (i = 0; i < conn->count_encoders; ++i) {
181*7688df22SAndroid Build Coastguard Worker           enc = drmModeGetEncoder(fd, conn->encoders[i]);
182*7688df22SAndroid Build Coastguard Worker           if (!enc) {
183*7688df22SAndroid Build Coastguard Worker               /* cannot retrieve encoder, ignoring... */
184*7688df22SAndroid Build Coastguard Worker               continue;
185*7688df22SAndroid Build Coastguard Worker           }
186*7688df22SAndroid Build Coastguard Worker
187*7688df22SAndroid Build Coastguard Worker           /* iterate all global CRTCs */
188*7688df22SAndroid Build Coastguard Worker           for (j = 0; j < res->count_crtcs; ++j) {
189*7688df22SAndroid Build Coastguard Worker               /* check whether this CRTC works with the encoder */
190*7688df22SAndroid Build Coastguard Worker               if (!(enc->possible_crtcs & (1 << j)))
191*7688df22SAndroid Build Coastguard Worker                   continue;
192*7688df22SAndroid Build Coastguard Worker
193*7688df22SAndroid Build Coastguard Worker
194*7688df22SAndroid Build Coastguard Worker               /* Here you need to check that no other connector
195*7688df22SAndroid Build Coastguard Worker                * currently uses the CRTC with id "crtc". If you intend
196*7688df22SAndroid Build Coastguard Worker                * to drive one connector only, then you can skip this
197*7688df22SAndroid Build Coastguard Worker                * step. Otherwise, simply scan your list of configured
198*7688df22SAndroid Build Coastguard Worker                * connectors and CRTCs whether this CRTC is already
199*7688df22SAndroid Build Coastguard Worker                * used. If it is, then simply continue the search here. */
200*7688df22SAndroid Build Coastguard Worker               if (res->crtcs[j] "is unused") {
201*7688df22SAndroid Build Coastguard Worker                   drmModeFreeEncoder(enc);
202*7688df22SAndroid Build Coastguard Worker                   return res->crtcs[j];
203*7688df22SAndroid Build Coastguard Worker               }
204*7688df22SAndroid Build Coastguard Worker           }
205*7688df22SAndroid Build Coastguard Worker
206*7688df22SAndroid Build Coastguard Worker           drmModeFreeEncoder(enc);
207*7688df22SAndroid Build Coastguard Worker       }
208*7688df22SAndroid Build Coastguard Worker
209*7688df22SAndroid Build Coastguard Worker       /* cannot find a suitable CRTC */
210*7688df22SAndroid Build Coastguard Worker       return -ENOENT;
211*7688df22SAndroid Build Coastguard Worker   }
212*7688df22SAndroid Build Coastguard Worker
213*7688df22SAndroid Build Coastguard WorkerReporting Bugs
214*7688df22SAndroid Build Coastguard Worker==============
215*7688df22SAndroid Build Coastguard Worker
216*7688df22SAndroid Build Coastguard WorkerBugs in this manual should be reported to
217*7688df22SAndroid Build Coastguard Workerhttps://gitlab.freedesktop.org/mesa/drm/-/issues
218*7688df22SAndroid Build Coastguard Worker
219*7688df22SAndroid Build Coastguard WorkerSee Also
220*7688df22SAndroid Build Coastguard Worker========
221*7688df22SAndroid Build Coastguard Worker
222*7688df22SAndroid Build Coastguard Worker**drm**\ (7), **drm-memory**\ (7), **drmModeGetResources**\ (3),
223*7688df22SAndroid Build Coastguard Worker**drmModeGetConnector**\ (3), **drmModeGetEncoder**\ (3),
224*7688df22SAndroid Build Coastguard Worker**drmModeGetCrtc**\ (3), **drmModeSetCrtc**\ (3), **drmModeGetFB**\ (3),
225*7688df22SAndroid Build Coastguard Worker**drmModeAddFB**\ (3), **drmModeAddFB2**\ (3), **drmModeRmFB**\ (3),
226*7688df22SAndroid Build Coastguard Worker**drmModePageFlip**\ (3), **drmModeGetPlaneResources**\ (3),
227*7688df22SAndroid Build Coastguard Worker**drmModeGetPlane**\ (3), **drmModeSetPlane**\ (3), **drmModeSetCursor**\ (3),
228*7688df22SAndroid Build Coastguard Worker**drmModeMoveCursor**\ (3), **drmSetMaster**\ (3), **drmAvailable**\ (3),
229*7688df22SAndroid Build Coastguard Worker**drmCheckModesettingSupported**\ (3), **drmOpen**\ (3)
230