Use plane for rendering

This commit is contained in:
Scott Anderson 2017-08-05 17:27:59 +12:00
parent e16c5504cd
commit c46605d605
3 changed files with 97 additions and 93 deletions

View File

@ -196,13 +196,14 @@ static void free_fb(struct gbm_bo *bo, void *data) {
uint32_t *id = data; uint32_t *id = data;
if (id && *id) { if (id && *id) {
drmModeRmFB(gbm_bo_get_fd(bo), *id); struct gbm_device *gbm = gbm_bo_get_device(bo);
drmModeRmFB(gbm_device_get_fd(gbm), *id);
} }
free(id); free(id);
} }
static uint32_t get_fb_for_bo(int fd, struct gbm_bo *bo) { static uint32_t get_fb_for_bo(struct gbm_bo *bo) {
uint32_t *id = gbm_bo_get_user_data(bo); uint32_t *id = gbm_bo_get_user_data(bo);
if (id) { if (id) {
@ -215,6 +216,9 @@ static uint32_t get_fb_for_bo(int fd, struct gbm_bo *bo) {
return 0; return 0;
} }
struct gbm_device *gbm = gbm_bo_get_device(bo);
int fd = gbm_device_get_fd(gbm);
drmModeAddFB(fd, gbm_bo_get_width(bo), gbm_bo_get_height(bo), 24, 32, drmModeAddFB(fd, gbm_bo_get_width(bo), gbm_bo_get_height(bo), 24, 32,
gbm_bo_get_stride(bo), gbm_bo_get_handle(bo).u32, id); gbm_bo_get_stride(bo), gbm_bo_get_handle(bo).u32, id);
@ -225,26 +229,32 @@ static uint32_t get_fb_for_bo(int fd, struct gbm_bo *bo) {
static void wlr_drm_output_make_current(struct wlr_output_state *output) { static void wlr_drm_output_make_current(struct wlr_output_state *output) {
struct wlr_drm_renderer *renderer = output->renderer; struct wlr_drm_renderer *renderer = output->renderer;
eglMakeCurrent(renderer->egl.display, output->egl, struct wlr_drm_plane *plane = output->crtc->primary;
output->egl, renderer->egl.context);
eglMakeCurrent(renderer->egl.display, plane->egl,
plane->egl, renderer->egl.context);
} }
static void wlr_drm_output_swap_buffers(struct wlr_output_state *output) { static void wlr_drm_output_swap_buffers(struct wlr_output_state *output) {
struct wlr_drm_renderer *renderer = output->renderer; struct wlr_drm_renderer *renderer = output->renderer;
struct wlr_drm_crtc *crtc = output->crtc;
struct wlr_drm_plane *plane = crtc->primary;
if (!eglSwapBuffers(renderer->egl.display, output->egl)) { if (!eglSwapBuffers(renderer->egl.display, plane->egl)) {
return; return;
} }
struct gbm_bo *bo = gbm_surface_lock_front_buffer(output->gbm);
struct gbm_bo *bo = gbm_surface_lock_front_buffer(plane->gbm);
if (!bo) { if (!bo) {
return; return;
} }
uint32_t fb_id = get_fb_for_bo(renderer->fd, bo);
drmModePageFlip(renderer->fd, output->crtc, fb_id, DRM_MODE_PAGE_FLIP_EVENT, output); uint32_t fb_id = get_fb_for_bo(bo);
drmModePageFlip(renderer->fd, crtc->id, fb_id, DRM_MODE_PAGE_FLIP_EVENT, output);
output->pageflip_pending = true; output->pageflip_pending = true;
output->bo[1] = output->bo[0]; plane->front = plane->back;
output->bo[0] = bo; plane->back = bo;
} }
void wlr_drm_output_pause_renderer(struct wlr_output_state *output) { void wlr_drm_output_pause_renderer(struct wlr_output_state *output) {
@ -252,14 +262,17 @@ void wlr_drm_output_pause_renderer(struct wlr_output_state *output) {
return; return;
} }
if (output->bo[1]) { struct wlr_drm_plane *plane = output->crtc->primary;
gbm_surface_release_buffer(output->gbm, output->bo[1]);
output->bo[1] = NULL; if (plane->front) {
gbm_surface_release_buffer(plane->gbm, plane->front);
plane->front = NULL;
} }
if (output->bo[0]) { if (plane->back) {
gbm_surface_release_buffer(output->gbm, output->bo[0]); gbm_surface_release_buffer(plane->gbm, plane->back);
output->bo[0] = NULL; plane->back = NULL;
} }
} }
void wlr_drm_output_start_renderer(struct wlr_output_state *output) { void wlr_drm_output_start_renderer(struct wlr_output_state *output) {
@ -269,47 +282,52 @@ void wlr_drm_output_start_renderer(struct wlr_output_state *output) {
struct wlr_drm_renderer *renderer = output->renderer; struct wlr_drm_renderer *renderer = output->renderer;
struct wlr_output_mode *mode = output->base->current_mode; struct wlr_output_mode *mode = output->base->current_mode;
struct wlr_drm_crtc *crtc = output->crtc;
struct wlr_drm_plane *plane = crtc->primary;
// Render black frame eglMakeCurrent(renderer->egl.display, plane->egl, plane->egl, renderer->egl.context);
eglMakeCurrent(renderer->egl.display, output->egl, output->egl, renderer->egl.context);
glViewport(0, 0, output->width, output->height); glViewport(0, 0, output->width, output->height);
glClearColor(0.0, 0.0, 0.0, 1.0); glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
eglSwapBuffers(renderer->egl.display, output->egl); if (!eglSwapBuffers(renderer->egl.display, plane->egl)) {
return;
}
struct gbm_bo *bo = gbm_surface_lock_front_buffer(output->gbm); struct gbm_bo *bo = gbm_surface_lock_front_buffer(plane->gbm);
uint32_t fb_id = get_fb_for_bo(renderer->fd, bo); if (!bo) {
return;
}
drmModeSetCrtc(renderer->fd, output->crtc, fb_id, 0, 0, uint32_t fb_id = get_fb_for_bo(bo);
drmModeSetCrtc(renderer->fd, crtc->id, fb_id, 0, 0,
&output->connector, 1, &mode->state->mode); &output->connector, 1, &mode->state->mode);
drmModePageFlip(renderer->fd, output->crtc, fb_id, drmModePageFlip(renderer->fd, crtc->id, fb_id, DRM_MODE_PAGE_FLIP_EVENT, output);
DRM_MODE_PAGE_FLIP_EVENT, output); output->pageflip_pending = true;
output->bo[1] = NULL; plane->back = plane->front;
output->bo[0] = bo; plane->front = bo;
} }
static bool display_init_renderer(struct wlr_drm_renderer *renderer, static bool plane_init_renderer(struct wlr_drm_renderer *renderer,
struct wlr_output_state *output) { struct wlr_drm_plane *plane, struct wlr_output_mode *mode) {
struct wlr_output_mode *mode = output->base->current_mode; plane->width = mode->width;
output->renderer = renderer; plane->height = mode->height;
output->gbm = gbm_surface_create(renderer->gbm, mode->width,
plane->gbm = gbm_surface_create(renderer->gbm, mode->width,
mode->height, GBM_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING); mode->height, GBM_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING);
if (!output->gbm) { if (!plane->gbm) {
wlr_log(L_ERROR, "Failed to create GBM surface for %s: %s", output->base->name, wlr_log_errno(L_ERROR, "Failed to create GBM surface for plane");
strerror(errno));
return false; return false;
} }
output->egl = wlr_egl_create_surface(&renderer->egl, output->gbm); plane->egl = wlr_egl_create_surface(&renderer->egl, plane->gbm);
if (output->egl == EGL_NO_SURFACE) { if (plane->egl == EGL_NO_SURFACE) {
wlr_log(L_ERROR, "Failed to create EGL surface for %s", output->base->name); wlr_log(L_ERROR, "Failed to create EGL surface for plane");
return false; return false;
} }
wlr_drm_output_start_renderer(output);
return true; return true;
} }
@ -500,37 +518,37 @@ static size_t match_obj(size_t num_objs, const uint32_t objs[static restrict num
} }
static void realloc_planes(struct wlr_backend_state *drm, const uint32_t *crtc_in) { static void realloc_planes(struct wlr_backend_state *drm, const uint32_t *crtc_in) {
for (int i = 0; i < 3; ++i) { // overlay, primary, cursor
if (drm->num_type_planes[i] == 0) for (int type = 0; type < 3; ++type) {
if (drm->num_type_planes[type] == 0)
continue; continue;
uint32_t possible[drm->num_type_planes[i]]; uint32_t possible[drm->num_type_planes[type]];
uint32_t crtc[drm->num_crtcs]; uint32_t crtc[drm->num_crtcs];
uint32_t crtc_res[drm->num_crtcs]; uint32_t crtc_res[drm->num_crtcs];
for (size_t j = 0; j < drm->num_type_planes[i]; ++j) for (size_t i = 0; i < drm->num_type_planes[type]; ++i)
possible[j] = drm->type_planes[i][j].possible_crtcs; possible[i] = drm->type_planes[type][i].possible_crtcs;
for (size_t j = 0; j < drm->num_crtcs; ++j) { for (size_t i = 0; i < drm->num_crtcs; ++i) {
if (crtc_in[j] == UNMATCHED) { if (crtc_in[i] == UNMATCHED)
crtc[j] = SKIP; crtc[i] = SKIP;
else if (drm->crtcs[i].planes[type])
crtc[i] = drm->crtcs[i].planes[type] - drm->type_planes[type];
else
crtc[i] = UNMATCHED;
}
match_obj(drm->num_type_planes[type], possible, drm->num_crtcs, crtc, crtc_res);
for (size_t i = 0; i < drm->num_crtcs; ++i) {
if (crtc_res[i] == UNMATCHED || crtc_res[i] == SKIP)
continue; continue;
}
if (drm->crtcs[j].planes[j]) { struct wlr_drm_crtc *c = &drm->crtcs[i];
crtc[j] = drm->crtcs[j].planes[i] - drm->type_planes[i]; c->planes[type] = &drm->type_planes[type][crtc_res[i]];
} else {
crtc[j] = UNMATCHED;
} }
} }
match_obj(drm->num_type_planes[i], possible, drm->num_crtcs, crtc, crtc_res);
for (size_t j = 0; j < drm->num_crtcs; ++j) {
struct wlr_drm_crtc *c = &drm->crtcs[j];
c->planes[i] = &drm->type_planes[i][crtc_res[j]];
}
};
} }
static void realloc_crtcs(struct wlr_backend_state *drm, struct wlr_output_state *output) { static void realloc_crtcs(struct wlr_backend_state *drm, struct wlr_output_state *output) {
@ -558,7 +576,7 @@ static void realloc_crtcs(struct wlr_backend_state *drm, struct wlr_output_state
} }
possible_crtc[i] = o->possible_crtc; possible_crtc[i] = o->possible_crtc;
crtc[o->crtc_ - drm->crtcs] = i; crtc[o->crtc - drm->crtcs] = i;
} }
possible_crtc[index] = output->possible_crtc; possible_crtc[index] = output->possible_crtc;
@ -575,8 +593,7 @@ static void realloc_crtcs(struct wlr_backend_state *drm, struct wlr_output_state
if (crtc_res[i] != crtc[i]) { if (crtc_res[i] != crtc[i]) {
struct wlr_output_state *o = drm->outputs->items[crtc_res[i]]; struct wlr_output_state *o = drm->outputs->items[crtc_res[i]];
o->crtc_ = &drm->crtcs[i]; o->crtc = &drm->crtcs[i];
o->crtc = o->crtc_->id;
} }
} }
@ -618,12 +635,12 @@ static bool wlr_drm_output_set_mode(struct wlr_output_state *output,
output->possible_crtc = enc->possible_crtcs; output->possible_crtc = enc->possible_crtcs;
realloc_crtcs(drm, output); realloc_crtcs(drm, output);
if (!output->crtc_) { if (!output->crtc) {
wlr_log(L_ERROR, "Unable to match %s with a CRTC", output->base->name); wlr_log(L_ERROR, "Unable to match %s with a CRTC", output->base->name);
goto error_enc; goto error_enc;
} }
struct wlr_drm_crtc *crtc = output->crtc_; struct wlr_drm_crtc *crtc = output->crtc;
wlr_log(L_DEBUG, "%s: crtc=%ju ovr=%jd pri=%jd cur=%jd", output->base->name, wlr_log(L_DEBUG, "%s: crtc=%ju ovr=%jd pri=%jd cur=%jd", output->base->name,
crtc - drm->crtcs, crtc - drm->crtcs,
crtc->overlay ? crtc->overlay - drm->overlay_planes : -1, crtc->overlay ? crtc->overlay - drm->overlay_planes : -1,
@ -636,11 +653,13 @@ static bool wlr_drm_output_set_mode(struct wlr_output_state *output,
output->base->current_mode = mode; output->base->current_mode = mode;
wl_signal_emit(&output->base->events.resolution, output->base); wl_signal_emit(&output->base->events.resolution, output->base);
if (!display_init_renderer(&drm->renderer, output)) { if (!plane_init_renderer(&drm->renderer, output->crtc->primary, mode)) {
wlr_log(L_ERROR, "Failed to initalise renderer for %s", output->base->name); wlr_log(L_ERROR, "Failed to initalise renderer for plane");
goto error_enc; goto error_enc;
} }
wlr_drm_output_start_renderer(output);
drmModeFreeEncoder(enc); drmModeFreeEncoder(enc);
drmModeFreeConnector(conn); drmModeFreeConnector(conn);
return true; return true;
@ -663,7 +682,7 @@ static bool wlr_drm_output_set_cursor(struct wlr_output_state *output,
const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height) { const uint8_t *buf, int32_t stride, uint32_t width, uint32_t height) {
struct wlr_backend_state *state = wl_container_of(output->renderer, state, renderer); struct wlr_backend_state *state = wl_container_of(output->renderer, state, renderer);
if (!buf) { if (!buf) {
drmModeSetCursor(state->fd, output->crtc, 0, 0, 0); drmModeSetCursor(state->fd, output->crtc->id, 0, 0, 0);
return true; return true;
} }
@ -719,7 +738,7 @@ static bool wlr_drm_output_set_cursor(struct wlr_output_state *output,
} }
uint32_t bo_handle = gbm_bo_get_handle(bo).u32; uint32_t bo_handle = gbm_bo_get_handle(bo).u32;
if (drmModeSetCursor(state->fd, output->crtc, bo_handle, bo_width, bo_height)) { if (drmModeSetCursor(state->fd, output->crtc->id, bo_handle, bo_width, bo_height)) {
wlr_log_errno(L_INFO, "Failed to set hardware cursor"); wlr_log_errno(L_INFO, "Failed to set hardware cursor");
return false; return false;
} }
@ -731,7 +750,7 @@ static bool wlr_drm_output_move_cursor(struct wlr_output_state *output,
int x, int y) { int x, int y) {
struct wlr_backend_state *state = struct wlr_backend_state *state =
wl_container_of(output->renderer, state, renderer); wl_container_of(output->renderer, state, renderer);
return !drmModeMoveCursor(state->fd, output->crtc, x, y); return !drmModeMoveCursor(state->fd, output->crtc->id, x, y);
} }
static void wlr_drm_output_destroy(struct wlr_output_state *output) { static void wlr_drm_output_destroy(struct wlr_output_state *output) {
@ -978,9 +997,10 @@ static void page_flip_handler(int fd, unsigned seq,
struct wlr_backend_state *state = struct wlr_backend_state *state =
wl_container_of(output->renderer, state, renderer); wl_container_of(output->renderer, state, renderer);
if (output->gbm && output->bo[1]) { struct wlr_drm_plane *plane = output->crtc->primary;
gbm_surface_release_buffer(output->gbm, output->bo[1]); if (plane->front) {
output->bo[1] = NULL; gbm_surface_release_buffer(plane->gbm, plane->front);
plane->front = NULL;
} }
output->pageflip_pending = false; output->pageflip_pending = false;
@ -1031,20 +1051,7 @@ void wlr_drm_output_cleanup(struct wlr_output_state *output, bool restore) {
restore = false; restore = false;
} }
if (output->bo[0]) { output->crtc = NULL;
gbm_surface_release_buffer(output->gbm, output->bo[0]);
output->bo[0] = NULL;
}
if (output->bo[1]) {
gbm_surface_release_buffer(output->gbm, output->bo[1]);
output->bo[1] = NULL;
}
eglDestroySurface(renderer->egl.display, output->egl);
gbm_surface_destroy(output->gbm);
output->egl = EGL_NO_SURFACE;
output->gbm = NULL;
output->crtc_ = NULL;
output->possible_crtc = 0; output->possible_crtc = 0;
/* Fallthrough */ /* Fallthrough */
case WLR_DRM_OUTPUT_NEEDS_MODESET: case WLR_DRM_OUTPUT_NEEDS_MODESET:

View File

@ -131,7 +131,7 @@ struct wlr_output_state {
enum wlr_drm_output_state state; enum wlr_drm_output_state state;
uint32_t connector; uint32_t connector;
struct wlr_drm_crtc *crtc_; struct wlr_drm_crtc *crtc;
uint32_t possible_crtc; uint32_t possible_crtc;
union wlr_drm_connector_props props; union wlr_drm_connector_props props;
@ -139,12 +139,9 @@ struct wlr_output_state {
uint32_t width; uint32_t width;
uint32_t height; uint32_t height;
uint32_t crtc;
drmModeCrtc *old_crtc; drmModeCrtc *old_crtc;
struct wlr_drm_renderer *renderer; struct wlr_drm_renderer *renderer;
EGLSurface *egl;
struct gbm_surface *gbm;
struct gbm_bo *bo[2]; struct gbm_bo *bo[2];
struct gbm_bo *cursor_bo[2]; struct gbm_bo *cursor_bo[2];
int current_cursor; int current_cursor;