diff options
Diffstat (limited to 'backend/drm')
-rw-r--r-- | backend/drm/atomic.c | 16 | ||||
-rw-r--r-- | backend/drm/backend.c | 7 | ||||
-rw-r--r-- | backend/drm/drm.c | 505 | ||||
-rw-r--r-- | backend/drm/legacy.c | 5 | ||||
-rw-r--r-- | backend/drm/renderer.c | 39 | ||||
-rw-r--r-- | backend/drm/util.c | 32 |
6 files changed, 362 insertions, 242 deletions
diff --git a/backend/drm/atomic.c b/backend/drm/atomic.c index 61d2f6e1..335c9de1 100644 --- a/backend/drm/atomic.c +++ b/backend/drm/atomic.c @@ -199,14 +199,20 @@ static bool atomic_crtc_move_cursor(struct wlr_drm_backend *drm, static bool atomic_crtc_set_gamma(struct wlr_drm_backend *drm, struct wlr_drm_crtc *crtc, uint16_t *r, uint16_t *g, uint16_t *b, uint32_t size) { - struct drm_color_lut gamma[size]; - // Fallback to legacy gamma interface when gamma properties are not available // (can happen on older intel gpu's that support gamma but not degamma) - if (crtc->props.gamma_lut == 0) { + // TEMP: This is broken on AMDGPU. Always fallback to legacy until they get + // it fixed. Ref https://bugs.freedesktop.org/show_bug.cgi?id=107459 + if (crtc->props.gamma_lut == 0 || true) { return legacy_iface.crtc_set_gamma(drm, crtc, r, g, b, size); } + struct drm_color_lut *gamma = malloc(size * sizeof(struct drm_color_lut)); + if (gamma == NULL) { + wlr_log(WLR_ERROR, "Failed to allocate gamma table"); + return false; + } + for (uint32_t i = 0; i < size; i++) { gamma[i].red = r[i]; gamma[i].green = g[i]; @@ -218,10 +224,12 @@ static bool atomic_crtc_set_gamma(struct wlr_drm_backend *drm, } if (drmModeCreatePropertyBlob(drm->fd, gamma, - sizeof(struct drm_color_lut) * size, &crtc->gamma_lut)) { + size * sizeof(struct drm_color_lut), &crtc->gamma_lut)) { + free(gamma); wlr_log_errno(WLR_ERROR, "Unable to create property blob"); return false; } + free(gamma); struct atomic atom; atomic_begin(crtc, &atom); diff --git a/backend/drm/backend.c b/backend/drm/backend.c index 5833d46e..1df5cdd1 100644 --- a/backend/drm/backend.c +++ b/backend/drm/backend.c @@ -51,7 +51,12 @@ static void backend_destroy(struct wlr_backend *backend) { static struct wlr_renderer *backend_get_renderer( struct wlr_backend *backend) { struct wlr_drm_backend *drm = (struct wlr_drm_backend *)backend; - return drm->renderer.wlr_rend; + + if (drm->parent) { + return drm->parent->renderer.wlr_rend; + } else { + return drm->renderer.wlr_rend; + } } static struct wlr_backend_impl backend_impl = { diff --git a/backend/drm/drm.c b/backend/drm/drm.c index c4674235..e37eba74 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -27,6 +27,23 @@ #include "util/signal.h" bool check_drm_features(struct wlr_drm_backend *drm) { + if (drm->parent) { + uint64_t cap; + if (drmGetCap(drm->fd, DRM_CAP_PRIME, &cap) || + !(cap & DRM_PRIME_CAP_IMPORT)) { + wlr_log(WLR_ERROR, + "PRIME import not supported on secondary GPU"); + return false; + } + + if (drmGetCap(drm->parent->fd, DRM_CAP_PRIME, &cap) || + !(cap & DRM_PRIME_CAP_EXPORT)) { + wlr_log(WLR_ERROR, + "PRIME export not supported on primary GPU"); + return false; + } + } + if (drmSetClientCap(drm->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1)) { wlr_log(WLR_ERROR, "DRM universal planes unsupported"); return false; @@ -228,30 +245,54 @@ static bool drm_connector_swap_buffers(struct wlr_output *output, return true; } -static void drm_connector_set_gamma(struct wlr_output *output, - uint32_t size, uint16_t *r, uint16_t *g, uint16_t *b) { +static void fill_empty_gamma_table(uint32_t size, + uint16_t *r, uint16_t *g, uint16_t *b) { + for (uint32_t i = 0; i < size; ++i) { + uint16_t val = (uint32_t)0xffff * i / (size - 1); + r[i] = g[i] = b[i] = val; + } +} + +static uint32_t drm_connector_get_gamma_size(struct wlr_output *output) { struct wlr_drm_connector *conn = (struct wlr_drm_connector *)output; struct wlr_drm_backend *drm = (struct wlr_drm_backend *)output->backend; - bool ok; if (conn->crtc) { - ok = drm->iface->crtc_set_gamma(drm, conn->crtc, r, g, b, size); - if (ok) { - wlr_output_update_needs_swap(output); - } + return drm->iface->crtc_get_gamma_size(drm, conn->crtc); } + return 0; } -static uint32_t drm_connector_get_gamma_size(struct wlr_output *output) { +static bool drm_connector_set_gamma(struct wlr_output *output, + uint32_t size, uint16_t *r, uint16_t *g, uint16_t *b) { struct wlr_drm_connector *conn = (struct wlr_drm_connector *)output; struct wlr_drm_backend *drm = (struct wlr_drm_backend *)output->backend; - if (conn->crtc) { - return drm->iface->crtc_get_gamma_size(drm, conn->crtc); + if (!conn->crtc) { + return false; } - return 0; + uint16_t *reset_table = NULL; + if (size == 0) { + size = drm_connector_get_gamma_size(output); + reset_table = malloc(3 * size * sizeof(uint16_t)); + if (reset_table == NULL) { + wlr_log(WLR_ERROR, "Failed to allocate gamma table"); + return false; + } + r = reset_table; + g = reset_table + size; + b = reset_table + 2 * size; + fill_empty_gamma_table(size, r, g, b); + } + + bool ok = drm->iface->crtc_set_gamma(drm, conn->crtc, r, g, b, size); + if (ok) { + wlr_output_update_needs_swap(output); + } + free(reset_table); + return ok; } static bool drm_connector_export_dmabuf(struct wlr_output *output, @@ -303,7 +344,8 @@ static void drm_connector_start_renderer(struct wlr_drm_connector *conn) { void enable_drm_connector(struct wlr_output *output, bool enable) { struct wlr_drm_connector *conn = (struct wlr_drm_connector *)output; - if (conn->state != WLR_DRM_CONN_CONNECTED) { + if (conn->state != WLR_DRM_CONN_CONNECTED + && conn->state != WLR_DRM_CONN_NEEDS_MODESET) { return; } @@ -362,9 +404,11 @@ static void realloc_planes(struct wlr_drm_backend *drm, const uint32_t *crtc_in, struct wlr_drm_plane *new = &drm->type_planes[type][crtc_res[i]]; if (*old != new) { - wlr_log(WLR_DEBUG, "Assigning plane %d -> %d to CRTC %d", + wlr_log(WLR_DEBUG, + "Assigning plane %d -> %d (type %zu) to CRTC %d", *old ? (int)(*old)->id : -1, new ? (int)new->id : -1, + type, c->id); changed_outputs[crtc_res[i]] = true; @@ -378,192 +422,41 @@ static void realloc_planes(struct wlr_drm_backend *drm, const uint32_t *crtc_in, } } -static void realloc_crtcs(struct wlr_drm_backend *drm, - struct wlr_drm_connector *conn, bool *changed_outputs) { - uint32_t crtc[drm->num_crtcs]; - uint32_t crtc_res[drm->num_crtcs]; - ssize_t num_outputs = wl_list_length(&drm->outputs); - uint32_t possible_crtc[num_outputs]; - - for (size_t i = 0; i < drm->num_crtcs; ++i) { - crtc[i] = UNMATCHED; - } - - memset(possible_crtc, 0, sizeof(possible_crtc)); - - wlr_log(WLR_DEBUG, "Reallocating CRTCs for output '%s'", conn->output.name); - - ssize_t index = -1, i = -1; - struct wlr_drm_connector *c; - wl_list_for_each(c, &drm->outputs, link) { - i++; - if (c == conn) { - index = i; - } - - wlr_log(WLR_DEBUG, "output '%s' crtc=%p state=%d", - c->output.name, c->crtc, c->state); - - if (c->crtc) { - crtc[c->crtc - drm->crtcs] = i; - } - - if (c->state == WLR_DRM_CONN_CONNECTED) { - possible_crtc[i] = c->possible_crtc; - } - } - assert(index != -1); - - possible_crtc[index] = conn->possible_crtc; - match_obj(wl_list_length(&drm->outputs), possible_crtc, - drm->num_crtcs, crtc, crtc_res); - - bool matched[num_outputs]; - memset(matched, false, sizeof(matched)); - for (size_t i = 0; i < drm->num_crtcs; ++i) { - if (crtc_res[i] != UNMATCHED) { - matched[crtc_res[i]] = true; - } - } - - // There is no point doing anything if this monitor doesn't get activated - if (!matched[index]) { - wlr_log(WLR_DEBUG, "Could not match a CRTC for this output"); - return; - } - - for (size_t i = 0; i < drm->num_crtcs; ++i) { - // We don't want any of the current monitors to be deactivated. - if (crtc[i] != UNMATCHED && !matched[crtc[i]]) { - wlr_log(WLR_DEBUG, "Could not match a CRTC for other output %d", - crtc[i]); - return; - } - } - - changed_outputs[index] = true; - - for (size_t i = 0; i < drm->num_crtcs; ++i) { - if (crtc_res[i] == UNMATCHED) { - continue; - } - - if (crtc_res[i] != crtc[i]) { - changed_outputs[crtc_res[i]] = true; - struct wlr_drm_connector *c; - size_t pos = 0; - wl_list_for_each(c, &drm->outputs, link) { - if (pos == crtc_res[i]) { - break; - } - pos++; - } - c->crtc = &drm->crtcs[i]; - - wlr_log(WLR_DEBUG, "Assigning CRTC %d to output '%s'", - drm->crtcs[i].id, c->output.name); - } - } - - realloc_planes(drm, crtc_res, changed_outputs); -} - -static uint32_t get_possible_crtcs(int fd, uint32_t conn_id) { - drmModeConnector *conn = drmModeGetConnector(fd, conn_id); - if (!conn) { - wlr_log_errno(WLR_ERROR, "Failed to get DRM connector"); - return 0; - } - - if (conn->connection != DRM_MODE_CONNECTED || conn->count_modes == 0) { - wlr_log(WLR_ERROR, "Output is not connected"); - goto error_conn; - } - - drmModeEncoder *enc = NULL; - for (int i = 0; !enc && i < conn->count_encoders; ++i) { - enc = drmModeGetEncoder(fd, conn->encoders[i]); - } - - if (!enc) { - wlr_log(WLR_ERROR, "Failed to get DRM encoder"); - goto error_conn; - } - - uint32_t ret = enc->possible_crtcs; - drmModeFreeEncoder(enc); - drmModeFreeConnector(conn); - return ret; - -error_conn: - drmModeFreeConnector(conn); - return 0; -} - static void drm_connector_cleanup(struct wlr_drm_connector *conn); static bool drm_connector_set_mode(struct wlr_output *output, struct wlr_output_mode *mode) { struct wlr_drm_connector *conn = (struct wlr_drm_connector *)output; struct wlr_drm_backend *drm = (struct wlr_drm_backend *)output->backend; - bool changed_outputs[wl_list_length(&drm->outputs)]; - - wlr_log(WLR_INFO, "Modesetting '%s' with '%ux%u@%u mHz'", conn->output.name, - mode->width, mode->height, mode->refresh); - - conn->possible_crtc = get_possible_crtcs(drm->fd, conn->id); - if (conn->possible_crtc == 0) { - goto error_conn; + if (conn->crtc == NULL) { + wlr_log(WLR_ERROR, "Cannot modeset '%s': no CRTC for this connector", + conn->output.name); + // Save the desired mode for later, when we'll get a proper CRTC + conn->desired_mode = mode; + return false; } - memset(changed_outputs, false, sizeof(changed_outputs)); - realloc_crtcs(drm, conn, changed_outputs); + wlr_log(WLR_INFO, "Modesetting '%s' with '%ux%u@%u mHz'", + conn->output.name, mode->width, mode->height, mode->refresh); - struct wlr_drm_crtc *crtc = conn->crtc; - if (!crtc) { - wlr_log(WLR_ERROR, "Unable to match %s with a CRTC", conn->output.name); - goto error_conn; + if (!init_drm_plane_surfaces(conn->crtc->primary, drm, + mode->width, mode->height, GBM_FORMAT_XRGB8888)) { + wlr_log(WLR_ERROR, "Failed to initialize renderer for plane"); + return false; } - wlr_log(WLR_DEBUG, "%s: crtc=%td ovr=%td pri=%td cur=%td", conn->output.name, - crtc - drm->crtcs, - crtc->overlay ? crtc->overlay - drm->overlay_planes : -1, - crtc->primary ? crtc->primary - drm->primary_planes : -1, - crtc->cursor ? crtc->cursor - drm->cursor_planes : -1); conn->state = WLR_DRM_CONN_CONNECTED; + conn->desired_mode = NULL; wlr_output_update_mode(&conn->output, mode); + wlr_output_update_enabled(&conn->output, true); + + drm_connector_start_renderer(conn); // When switching VTs, the mode is not updated but the buffers become // invalid, so we need to manually damage the output here wlr_output_damage_whole(&conn->output); - // Since realloc_crtcs can deallocate planes on OTHER outputs, - // we actually need to reinitialize any that has changed - ssize_t output_index = -1; - wl_list_for_each(conn, &drm->outputs, link) { - output_index++; - struct wlr_output_mode *mode = conn->output.current_mode; - struct wlr_drm_crtc *crtc = conn->crtc; - - if (conn->state != WLR_DRM_CONN_CONNECTED || - !changed_outputs[output_index]) { - continue; - } - - if (!init_drm_plane_surfaces(crtc->primary, drm, - mode->width, mode->height, GBM_FORMAT_XRGB8888)) { - wlr_log(WLR_ERROR, "Failed to initialize renderer for plane"); - goto error_conn; - } - - drm_connector_start_renderer(conn); - } - return true; - -error_conn: - drm_connector_cleanup(conn); - return false; } bool wlr_drm_connector_add_mode(struct wlr_output *output, @@ -604,7 +497,6 @@ static bool drm_connector_set_cursor(struct wlr_output *output, bool update_texture) { struct wlr_drm_connector *conn = (struct wlr_drm_connector *)output; struct wlr_drm_backend *drm = (struct wlr_drm_backend *)output->backend; - struct wlr_drm_renderer *renderer = &drm->renderer; struct wlr_drm_crtc *crtc = conn->crtc; if (!crtc) { @@ -630,13 +522,16 @@ static bool drm_connector_set_cursor(struct wlr_output *output, ret = drmGetCap(drm->fd, DRM_CAP_CURSOR_HEIGHT, &h); h = ret ? 64 : h; + struct wlr_drm_renderer *renderer = + drm->parent ? &drm->parent->renderer : &drm->renderer; + if (!init_drm_surface(&plane->surf, renderer, w, h, GBM_FORMAT_ARGB8888, 0)) { wlr_log(WLR_ERROR, "Cannot allocate cursor resources"); return false; } - plane->cursor_bo = gbm_bo_create(renderer->gbm, w, h, + plane->cursor_bo = gbm_bo_create(drm->renderer.gbm, w, h, GBM_FORMAT_ARGB8888, GBM_BO_USE_CURSOR | GBM_BO_USE_WRITE); if (!plane->cursor_bo) { wlr_log_errno(WLR_ERROR, "Failed to create cursor bo"); @@ -811,6 +706,176 @@ static const int32_t subpixel_map[] = { [DRM_MODE_SUBPIXEL_NONE] = WL_OUTPUT_SUBPIXEL_NONE, }; +static void dealloc_crtc(struct wlr_drm_connector *conn) { + struct wlr_drm_backend *drm = (struct wlr_drm_backend *)conn->output.backend; + if (conn->crtc == NULL) { + return; + } + + wlr_log(WLR_DEBUG, "De-allocating CRTC %zu for output '%s'", + conn->crtc - drm->crtcs, conn->output.name); + + for (size_t type = 0; type < 3; ++type) { + struct wlr_drm_plane *plane = conn->crtc->planes[type]; + if (plane == NULL) { + continue; + } + + finish_drm_surface(&plane->surf); + conn->crtc->planes[type] = NULL; + } + + drm->iface->conn_enable(drm, conn, false); + + conn->crtc = NULL; +} + +void realloc_crtcs(struct wlr_drm_backend *drm, bool *changed_outputs) { + size_t num_outputs = wl_list_length(&drm->outputs); + + wlr_log(WLR_DEBUG, "Reallocating CRTCs"); + + uint32_t crtc[drm->num_crtcs]; + for (size_t i = 0; i < drm->num_crtcs; ++i) { + crtc[i] = UNMATCHED; + } + + uint32_t possible_crtc[num_outputs]; + memset(possible_crtc, 0, sizeof(possible_crtc)); + + wlr_log(WLR_DEBUG, "State before reallocation:"); + ssize_t i = -1; + struct wlr_drm_connector *conn; + wl_list_for_each(conn, &drm->outputs, link) { + i++; + + wlr_log(WLR_DEBUG, " '%s' crtc=%d state=%d", conn->output.name, + conn->crtc ? (int)(conn->crtc - drm->crtcs) : -1, conn->state); + + if (conn->crtc) { + crtc[conn->crtc - drm->crtcs] = i; + } + + if (conn->state == WLR_DRM_CONN_CONNECTED || + conn->state == WLR_DRM_CONN_NEEDS_MODESET) { + possible_crtc[i] = conn->possible_crtc; + } + } + + uint32_t crtc_res[drm->num_crtcs]; + match_obj(wl_list_length(&drm->outputs), possible_crtc, + drm->num_crtcs, crtc, crtc_res); + + bool matched[num_outputs]; + memset(matched, false, sizeof(matched)); + for (size_t i = 0; i < drm->num_crtcs; ++i) { + if (crtc_res[i] != UNMATCHED) { + matched[crtc_res[i]] = true; + } + } + + for (size_t i = 0; i < drm->num_crtcs; ++i) { + // We don't want any of the current monitors to be deactivated + if (crtc[i] != UNMATCHED && !matched[crtc[i]]) { + wlr_log(WLR_DEBUG, "Could not match a CRTC for connected output %d", + crtc[i]); + return; + } + } + + struct wlr_drm_connector *connectors[num_outputs]; + i = 0; + wl_list_for_each(conn, &drm->outputs, link) { + connectors[i] = conn; + i++; + } + + for (size_t i = 0; i < drm->num_crtcs; ++i) { + if (crtc_res[i] == UNMATCHED) { + // De-allocate CRTCs we don't use anymore + if (crtc[i] != UNMATCHED) { + dealloc_crtc(connectors[crtc[i]]); + } + continue; + } + + if (crtc_res[i] != crtc[i]) { + changed_outputs[crtc_res[i]] = true; + + struct wlr_drm_connector *conn = connectors[crtc_res[i]]; + + dealloc_crtc(conn); + conn->crtc = &drm->crtcs[i]; + + wlr_log(WLR_DEBUG, "Assigning CRTC %zu to output %d -> %d '%s'", + i, crtc[i], crtc_res[i], conn->output.name); + } + } + + wlr_log(WLR_DEBUG, "State after reallocation:"); + wl_list_for_each(conn, &drm->outputs, link) { + wlr_log(WLR_DEBUG, " '%s' crtc=%d state=%d", conn->output.name, + conn->crtc ? (int)(conn->crtc - drm->crtcs) : -1, conn->state); + } + + realloc_planes(drm, crtc_res, changed_outputs); + + // We need to reinitialize any plane that has changed + i = -1; + wl_list_for_each(conn, &drm->outputs, link) { + i++; + struct wlr_output_mode *mode = conn->output.current_mode; + + if (conn->state != WLR_DRM_CONN_CONNECTED || !changed_outputs[i]) { + continue; + } + assert(conn->crtc); + + if (!init_drm_plane_surfaces(conn->crtc->primary, drm, + mode->width, mode->height, GBM_FORMAT_XRGB8888)) { + wlr_log(WLR_ERROR, "Failed to initialize renderer for plane"); + drm_connector_cleanup(conn); + break; + } + + drm_connector_start_renderer(conn); + + wlr_output_damage_whole(&conn->output); + } +} + +static uint32_t get_possible_crtcs(int fd, uint32_t conn_id) { + drmModeConnector *conn = drmModeGetConnector(fd, conn_id); + if (!conn) { + wlr_log_errno(WLR_ERROR, "Failed to get DRM connector"); + return 0; + } + + if (conn->connection != DRM_MODE_CONNECTED || conn->count_modes == 0) { + wlr_log(WLR_ERROR, "Output is not connected"); + goto error_conn; + } + + drmModeEncoder *enc = NULL; + for (int i = 0; !enc && i < conn->count_encoders; ++i) { + enc = drmModeGetEncoder(fd, conn->encoders[i]); + } + + if (!enc) { + wlr_log(WLR_ERROR, "Failed to get DRM encoder"); + goto error_conn; + } + + uint32_t ret = enc->possible_crtcs; + drmModeFreeEncoder(enc); + drmModeFreeConnector(conn); + return ret; + +error_conn: + drmModeFreeConnector(conn); + return 0; +} + void scan_drm_connectors(struct wlr_drm_backend *drm) { wlr_log(WLR_INFO, "Scanning DRM connectors"); @@ -824,7 +889,9 @@ void scan_drm_connectors(struct wlr_drm_backend *drm) { // +1 so length can never be 0, which is undefined behaviour. // Last element isn't used. bool seen[seen_len + 1]; - memset(seen, 0, sizeof(seen)); + memset(seen, false, sizeof(seen)); + size_t new_outputs_len = 0; + struct wlr_drm_connector *new_outputs[res->count_connectors + 1]; for (int i = 0; i < res->count_connectors; ++i) { drmModeConnector *drm_conn = drmModeGetConnector(drm->fd, @@ -864,17 +931,16 @@ void scan_drm_connectors(struct wlr_drm_backend *drm) { wlr_conn->state = WLR_DRM_CONN_DISCONNECTED; wlr_conn->id = drm_conn->connector_id; + snprintf(wlr_conn->output.name, sizeof(wlr_conn->output.name), + "%s-%"PRIu32, conn_get_name(drm_conn->connector_type), + drm_conn->connector_type_id); + if (curr_enc) { wlr_conn->old_crtc = drmModeGetCrtc(drm->fd, curr_enc->crtc_id); } - snprintf(wlr_conn->output.name, sizeof(wlr_conn->output.name), - "%s-%"PRIu32, - conn_get_name(drm_conn->connector_type), - drm_conn->connector_type_id); - wl_list_insert(&drm->outputs, &wlr_conn->link); - wlr_log(WLR_INFO, "Found display '%s'", wlr_conn->output.name); + wlr_log(WLR_INFO, "Found connector '%s'", wlr_conn->output.name); } else { seen[index] = true; } @@ -930,18 +996,20 @@ void scan_drm_connectors(struct wlr_drm_backend *drm) { wl_list_insert(&wlr_conn->output.modes, &mode->wlr_mode.link); } - wlr_output_update_enabled(&wlr_conn->output, true); + wlr_conn->possible_crtc = get_possible_crtcs(drm->fd, wlr_conn->id); + if (wlr_conn->possible_crtc == 0) { + wlr_log(WLR_ERROR, "No CRTC possible for connector '%s'", + wlr_conn->output.name); + } + + wlr_output_update_enabled(&wlr_conn->output, wlr_conn->crtc != NULL); wlr_conn->state = WLR_DRM_CONN_NEEDS_MODESET; - wlr_log(WLR_INFO, "Sending modesetting signal for '%s'", - wlr_conn->output.name); - wlr_signal_emit_safe(&drm->backend.events.new_output, - &wlr_conn->output); + new_outputs[new_outputs_len++] = wlr_conn; } else if (wlr_conn->state == WLR_DRM_CONN_CONNECTED && drm_conn->connection != DRM_MODE_CONNECTED) { wlr_log(WLR_INFO, "'%s' disconnected", wlr_conn->output.name); - wlr_output_update_enabled(&wlr_conn->output, false); drm_connector_cleanup(wlr_conn); } @@ -967,6 +1035,44 @@ void scan_drm_connectors(struct wlr_drm_backend *drm) { wl_list_remove(&conn->link); free(conn); } + + bool changed_outputs[wl_list_length(&drm->outputs)]; + memset(changed_outputs, false, sizeof(changed_outputs)); + for (size_t i = 0; i < new_outputs_len; ++i) { + struct wlr_drm_connector *conn = new_outputs[i]; + + ssize_t pos = -1; + struct wlr_drm_connector *c; + wl_list_for_each(c, &drm->outputs, link) { + ++pos; + if (c == conn) { + break; + } + } + assert(pos >= 0); + + changed_outputs[pos] = true; + } + + realloc_crtcs(drm, changed_outputs); + + for (size_t i = 0; i < new_outputs_len; ++i) { + struct wlr_drm_connector *conn = new_outputs[i]; + + wlr_log(WLR_INFO, "Requesting modeset for '%s'", + conn->output.name); + wlr_signal_emit_safe(&drm->backend.events.new_output, + &conn->output); + } + + // Try to modeset any output that has a desired mode and a CRTC (ie. was + // lacking a CRTC on last modeset) + wl_list_for_each(conn, &drm->outputs, link) { + if (conn->state == WLR_DRM_CONN_NEEDS_MODESET && conn->crtc != NULL && + conn->desired_mode != NULL) { + drm_connector_set_mode(&conn->output, conn->desired_mode); + } + } } static void page_flip_handler(int fd, unsigned seq, @@ -1062,23 +1168,28 @@ static void drm_connector_cleanup(struct wlr_drm_connector *conn) { } conn->output.current_mode = NULL; + conn->desired_mode = NULL; struct wlr_drm_mode *mode, *tmp; wl_list_for_each_safe(mode, tmp, &conn->output.modes, wlr_mode.link) { wl_list_remove(&mode->wlr_mode.link); free(mode); } + conn->output.enabled = false; + conn->output.width = conn->output.height = conn->output.refresh = 0; + memset(&conn->output.make, 0, sizeof(conn->output.make)); memset(&conn->output.model, 0, sizeof(conn->output.model)); memset(&conn->output.serial, 0, sizeof(conn->output.serial)); - conn->crtc = NULL; - conn->possible_crtc = 0; conn->pageflip_pending = false; /* Fallthrough */ case WLR_DRM_CONN_NEEDS_MODESET: wlr_log(WLR_INFO, "Emitting destruction signal for '%s'", conn->output.name); + dealloc_crtc(conn); + conn->possible_crtc = 0; + conn->desired_mode = NULL; wlr_signal_emit_safe(&conn->output.events.destroy, &conn->output); break; case WLR_DRM_CONN_DISCONNECTED: diff --git a/backend/drm/legacy.c b/backend/drm/legacy.c index d27cf137..c205e167 100644 --- a/backend/drm/legacy.c +++ b/backend/drm/legacy.c @@ -39,7 +39,10 @@ bool legacy_crtc_set_cursor(struct wlr_drm_backend *drm, } if (!bo) { - drmModeSetCursor(drm->fd, crtc->id, 0, 0, 0); + if (drmModeSetCursor(drm->fd, crtc->id, 0, 0, 0)) { + wlr_log_errno(WLR_DEBUG, "Failed to clear hardware cursor"); + return false; + } return true; } diff --git a/backend/drm/renderer.c b/backend/drm/renderer.c index 38e6315d..fa7d090e 100644 --- a/backend/drm/renderer.c +++ b/backend/drm/renderer.c @@ -188,46 +188,29 @@ bool export_drm_bo(struct gbm_bo *bo, struct wlr_dmabuf_attributes *attribs) { return true; } -struct tex { - struct wlr_egl *egl; - EGLImageKHR img; - struct wlr_texture *tex; -}; - -static void free_eglimage(struct gbm_bo *bo, void *data) { - struct tex *tex = data; - - wlr_egl_destroy_image(tex->egl, tex->img); - wlr_texture_destroy(tex->tex); - free(tex); +static void free_tex(struct gbm_bo *bo, void *data) { + struct wlr_texture *tex = data; + wlr_texture_destroy(tex); } static struct wlr_texture *get_tex_for_bo(struct wlr_drm_renderer *renderer, struct gbm_bo *bo) { - struct tex *tex = gbm_bo_get_user_data(bo); - if (tex != NULL) { - return tex->tex; - } - - tex = calloc(1, sizeof(struct tex)); - if (tex == NULL) { - return NULL; + struct wlr_texture *tex = gbm_bo_get_user_data(bo); + if (tex) { + return tex; } struct wlr_dmabuf_attributes attribs; if (!export_drm_bo(bo, &attribs)) { - free(tex); return NULL; } - tex->tex = wlr_texture_from_dmabuf(renderer->wlr_rend, &attribs); - if (tex->tex == NULL) { - free(tex); - return NULL; + tex = wlr_texture_from_dmabuf(renderer->wlr_rend, &attribs); + if (tex) { + gbm_bo_set_user_data(bo, tex, free_tex); } - gbm_bo_set_user_data(bo, tex, free_eglimage); - return tex->tex; + return tex; } struct gbm_bo *copy_drm_surface_mgpu(struct wlr_drm_surface *dest, @@ -238,7 +221,7 @@ struct gbm_bo *copy_drm_surface_mgpu(struct wlr_drm_surface *dest, assert(tex); float mat[9]; - wlr_matrix_projection(mat, 1, 1, WL_OUTPUT_TRANSFORM_FLIPPED_180); + wlr_matrix_projection(mat, 1, 1, WL_OUTPUT_TRANSFORM_NORMAL); struct wlr_renderer *renderer = dest->renderer->wlr_rend; wlr_renderer_begin(renderer, dest->width, dest->height); diff --git a/backend/drm/util.c b/backend/drm/util.c index baacbfa8..050da2a6 100644 --- a/backend/drm/util.c +++ b/backend/drm/util.c @@ -144,6 +144,9 @@ const char *conn_get_name(uint32_t type_id) { case DRM_MODE_CONNECTOR_eDP: return "eDP"; case DRM_MODE_CONNECTOR_VIRTUAL: return "Virtual"; case DRM_MODE_CONNECTOR_DSI: return "DSI"; +#ifdef DRM_MODE_CONNECTOR_DPI + case DRM_MODE_CONNECTOR_DPI: return "DPI"; +#endif default: return "Unknown"; } } @@ -225,9 +228,6 @@ static bool match_obj_(struct match_state *st, size_t skips, size_t score, size_ st->replaced = replaced; memcpy(st->best, st->res, sizeof(st->best[0]) * st->num_res); - if (st->score == st->num_objs && st->replaced == 0) { - st->exit_early = true; - } st->exit_early = (st->score == st->num_res - skips || st->score == st->num_objs) && st->replaced == 0; @@ -247,37 +247,44 @@ static bool match_obj_(struct match_state *st, size_t skips, size_t score, size_ * Attempt to use the current solution first, to try and avoid * recalculating everything */ - if (st->orig[i] != UNMATCHED && !is_taken(i, st->res, st->orig[i])) { st->res[i] = st->orig[i]; if (match_obj_(st, skips, score + 1, replaced, i + 1)) { return true; } } + if (st->orig[i] == UNMATCHED) { + st->res[i] = UNMATCHED; + match_obj_(st, skips, score, replaced, i + 1); + if (st->exit_early) { + return true; + } + } if (st->orig[i] != UNMATCHED) { ++replaced; } - bool is_best = false; - for (st->res[i] = 0; st->res[i] < st->num_objs; ++st->res[i]) { + bool has_best = false; + for (size_t candidate = 0; candidate < st->num_objs; ++candidate) { // We tried this earlier - if (st->res[i] == st->orig[i]) { + if (candidate == st->orig[i]) { continue; } // Not compatible - if (!(st->objs[st->res[i]] & (1 << i))) { + if (!(st->objs[candidate] & (1 << i))) { continue; } // Already taken - if (is_taken(i, st->res, st->res[i])) { + if (is_taken(i, st->res, candidate)) { continue; } + st->res[i] = candidate; if (match_obj_(st, skips, score + 1, replaced, i + 1)) { - is_best = true; + has_best = true; } if (st->exit_early) { @@ -285,7 +292,7 @@ static bool match_obj_(struct match_state *st, size_t skips, size_t score, size_ } } - if (is_best) { + if (has_best) { return true; } @@ -298,6 +305,9 @@ size_t match_obj(size_t num_objs, const uint32_t objs[static restrict num_objs], size_t num_res, const uint32_t res[static restrict num_res], uint32_t out[static restrict num_res]) { uint32_t solution[num_res]; + for (size_t i = 0; i < num_res; ++i) { + solution[i] = UNMATCHED; + } struct match_state st = { .num_objs = num_objs, |