drm: remove duplicate drmModeGetResources

This commit is contained in:
Simon Ser 2021-07-20 20:56:16 +02:00
parent 70d3dda4d4
commit d5097e2a21

View file

@ -37,37 +37,33 @@ bool g_bDebugLayers = false;
static int s_drm_log = 0; static int s_drm_log = 0;
static uint32_t find_crtc_for_encoder(const drmModeRes *resources, static uint32_t find_crtc_for_encoder(const struct drm_t *drm, const drmModeEncoder *encoder) {
const drmModeEncoder *encoder) { for (size_t i = 0; i < drm->crtcs.size(); i++) {
for (int i = 0; i < resources->count_crtcs; i++) {
/* possible_crtcs is a bitmask as described here: /* possible_crtcs is a bitmask as described here:
* https://dvdhrm.wordpress.com/2012/09/13/linux-drm-mode-setting-api * https://dvdhrm.wordpress.com/2012/09/13/linux-drm-mode-setting-api
*/ */
const uint32_t crtc_mask = 1 << i; uint32_t crtc_mask = 1 << i;
const uint32_t crtc_id = resources->crtcs[i]; uint32_t crtc_id = drm->crtcs[i].id;
if (encoder->possible_crtcs & crtc_mask) { if (encoder->possible_crtcs & crtc_mask)
return crtc_id; return crtc_id;
}
} }
/* no match found */ /* no match found */
return 0; return 0;
} }
static uint32_t find_crtc_for_connector(const struct drm_t *drm, const drmModeRes *resources, static uint32_t find_crtc_for_connector(const struct drm_t *drm, const drmModeConnector *connector) {
const drmModeConnector *connector) {
for (int i = 0; i < connector->count_encoders; i++) { for (int i = 0; i < connector->count_encoders; i++) {
const uint32_t encoder_id = connector->encoders[i]; uint32_t encoder_id = connector->encoders[i];
drmModeEncoder *encoder = drmModeGetEncoder(drm->fd, encoder_id); drmModeEncoder *encoder = drmModeGetEncoder(drm->fd, encoder_id);
if (encoder == nullptr)
continue;
if (encoder) { uint32_t crtc_id = find_crtc_for_encoder(drm, encoder);
const uint32_t crtc_id = find_crtc_for_encoder(resources, encoder); drmModeFreeEncoder(encoder);
if (crtc_id != 0)
drmModeFreeEncoder(encoder); return crtc_id;
if (crtc_id != 0) {
return crtc_id;
}
}
} }
/* no match found */ /* no match found */
@ -439,14 +435,13 @@ static bool compare_modes( drmModeModeInfo mode1, drmModeModeInfo mode2 )
int init_drm(struct drm_t *drm, const char *device) int init_drm(struct drm_t *drm, const char *device)
{ {
drmModeRes *resources;
drmModeConnector *connector = NULL; drmModeConnector *connector = NULL;
int i, ret; int ret;
if (device) { if (device) {
drm->fd = open(device, O_RDWR | O_CLOEXEC); drm->fd = open(device, O_RDWR | O_CLOEXEC);
if (!drmIsKMS(drm->fd)) { if (!drmIsKMS(drm->fd)) {
fprintf(stderr, "%s does not look like a modeset device\n", device); fprintf(stderr, "%s is not a KMS device\n", device);
return -1; return -1;
} }
} else { } else {
@ -458,12 +453,6 @@ int init_drm(struct drm_t *drm, const char *device)
return -1; return -1;
} }
resources = drmModeGetResources(drm->fd);
if (!resources) {
perror("drmModeGetResources failed");
return -1;
}
if (drmSetClientCap(drm->fd, DRM_CLIENT_CAP_ATOMIC, 1) != 0) { if (drmSetClientCap(drm->fd, DRM_CLIENT_CAP_ATOMIC, 1) != 0) {
fprintf(stderr, "drmSetClientCap(ATOMIC) failed\n"); fprintf(stderr, "drmSetClientCap(ATOMIC) failed\n");
return -1; return -1;
@ -485,18 +474,16 @@ int init_drm(struct drm_t *drm, const char *device)
return -1; return -1;
} }
/* find a connected connector: */ /* find a connected connector */
for (i = 0; i < resources->count_connectors; i++) { for (size_t i = 0; i < drm->connectors.size(); i++) {
connector = drmModeGetConnector(drm->fd, resources->connectors[i]); if (drm->connectors[i].connector->connection == DRM_MODE_CONNECTED) {
if (connector->connection == DRM_MODE_CONNECTED) { drm->connector = &drm->connectors[i];
/* it's connected, let's use this! */ connector = drm->connector->connector;
break; break;
} }
drmModeFreeConnector(connector);
connector = NULL;
} }
if (!connector) { if (!drm->connector) {
/* we could be fancy and listen for hotplug events and wait for /* we could be fancy and listen for hotplug events and wait for
* a connector.. * a connector..
*/ */
@ -528,14 +515,14 @@ int init_drm(struct drm_t *drm, const char *device)
return -1; return -1;
} }
drm->crtc_id = find_crtc_for_connector(drm, resources, connector); drm->crtc_id = find_crtc_for_connector(drm, drm->connector->connector);
if (drm->crtc_id == 0) { if (drm->crtc_id == 0) {
fprintf(stderr, "no crtc found!\n"); fprintf(stderr, "no crtc found!\n");
return -1; return -1;
} }
for (i = 0; i < resources->count_crtcs; i++) { for (size_t i = 0; i < drm->crtcs.size(); i++) {
if (resources->crtcs[i] == drm->crtc_id) { if (drm->crtcs[i].id == drm->crtc_id) {
drm->crtc_index = i; drm->crtc_index = i;
break; break;
} }
@ -543,14 +530,12 @@ int init_drm(struct drm_t *drm, const char *device)
// Disable all CRTCs. This ensures the CRTC we've selected isn't being used // Disable all CRTCs. This ensures the CRTC we've selected isn't being used
// by another connector. // by another connector.
for (i = 0; i < resources->count_crtcs; i++) { for (size_t i = 0; i < drm->crtcs.size(); i++) {
ret = drmModeSetCrtc(drm->fd, resources->crtcs[i], 0, 0, 0, nullptr, 0, nullptr); ret = drmModeSetCrtc(drm->fd, drm->crtcs[i].id, 0, 0, 0, nullptr, 0, nullptr);
if (ret != 0) if (ret != 0)
fprintf(stderr, "failed to disable CRTC %" PRIu32 ": %s", resources->crtcs[i], strerror(-ret)); fprintf(stderr, "failed to disable CRTC %" PRIu32 ": %s", drm->crtcs[i].id, strerror(-ret));
} }
drmModeFreeResources(resources);
drm->connector_id = connector->connector_id; drm->connector_id = connector->connector_id;
drm->plane_id = get_plane_id( &g_DRM ); drm->plane_id = get_plane_id( &g_DRM );