2 * Copyright © 2014 NVIDIA Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
36 #include <sys/ioctl.h>
39 #include "xf86drmMode.h"
40 #include "drm_fourcc.h"
44 static int drm_screen_probe_connector(struct drm_screen
*screen
,
45 drmModeConnectorPtr connector
)
47 drmModeEncoderPtr encoder
;
51 encoder
= drmModeGetEncoder(screen
->fd
, connector
->encoder_id
);
55 crtc
= drmModeGetCrtc(screen
->fd
, encoder
->crtc_id
);
57 drmModeFreeEncoder(encoder
);
61 screen
->old_fb
= crtc
->buffer_id
;
63 fb
= drmModeGetFB(screen
->fd
, crtc
->buffer_id
);
65 /* TODO: create new framebuffer */
66 drmModeFreeEncoder(encoder
);
67 drmModeFreeCrtc(crtc
);
71 screen
->connector
= connector
->connector_id
;
72 screen
->old_fb
= crtc
->buffer_id
;
73 screen
->crtc
= encoder
->crtc_id
;
74 /* TODO: check crtc->mode_valid */
75 screen
->mode
= crtc
->mode
;
77 screen
->width
= fb
->width
;
78 screen
->height
= fb
->height
;
79 screen
->pitch
= fb
->pitch
;
80 screen
->depth
= fb
->depth
;
81 screen
->bpp
= fb
->bpp
;
83 drmModeFreeEncoder(encoder
);
84 drmModeFreeCrtc(crtc
);
90 int drm_screen_open(struct drm_screen
**screenp
, int fd
)
92 drmModeConnectorPtr connector
;
93 struct drm_screen
*screen
;
99 if (!screenp
|| fd
< 0)
102 screen
= calloc(1, sizeof(*screen
));
106 screen
->format
= DRM_FORMAT_XRGB8888
;
109 res
= drmModeGetResources(fd
);
115 for (i
= 0; i
< (unsigned int)res
->count_connectors
; i
++) {
116 connector
= drmModeGetConnector(fd
, res
->connectors
[i
]);
120 if (connector
->connection
!= DRM_MODE_CONNECTED
) {
121 drmModeFreeConnector(connector
);
125 err
= drm_screen_probe_connector(screen
, connector
);
127 drmModeFreeConnector(connector
);
131 drmModeFreeConnector(connector
);
136 drmModeFreeResources(res
);
148 int drm_screen_close(struct drm_screen
*screen
)
152 err
= drmModeSetCrtc(screen
->fd
, screen
->crtc
, screen
->old_fb
, 0, 0,
153 &screen
->connector
, 1, &screen
->mode
);
155 fprintf(stderr
, "drmModeSetCrtc() failed: %m\n");
164 int drm_framebuffer_new(struct drm_framebuffer
**fbp
,
165 struct drm_screen
*screen
, uint32_t handle
,
166 unsigned int width
, unsigned int height
,
167 unsigned int pitch
, uint32_t format
,
170 struct drm_framebuffer
*fb
;
176 fb
= calloc(1, sizeof(*fb
));
191 err
= drmModeAddFB2(screen
->fd
, width
, height
, format
, handles
,
192 pitches
, offsets
, &fb
->handle
, 0);
201 int drm_framebuffer_free(struct drm_framebuffer
*fb
)
205 err
= drmModeRmFB(fb
->fd
, fb
->handle
);
214 int drm_screen_set_framebuffer(struct drm_screen
*screen
,
215 struct drm_framebuffer
*fb
)
219 err
= drmModeSetCrtc(screen
->fd
, screen
->crtc
, fb
->handle
, 0, 0,
220 &screen
->connector
, 1, &screen
->mode
);
227 int drm_open(const char *path
)
231 fd
= open(path
, O_RDWR
);
235 err
= drmSetMaster(fd
);
244 void drm_close(int fd
)