Skip to content
This repository has been archived by the owner on Nov 1, 2021. It is now read-only.

More accurate refresh rates (mHz) #4

Merged
merged 4 commits into from
May 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions backend/drm/backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,11 @@ struct wlr_backend *wlr_drm_backend_create(struct wl_display *display,
free(backend);
return NULL;
}

void wlr_drm_backend_dpms(struct wlr_backend *backend, bool screen_on) {
struct wlr_backend_state *state = backend->state;
for (size_t i = 0; i < state->outputs->length; ++i) {
struct wlr_output_state *output = state->outputs->items[i];
wlr_drm_output_dpms(state->fd, output, screen_on);
}
}
68 changes: 63 additions & 5 deletions backend/drm/drm.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,44 @@ static struct wlr_output_impl output_impl = {
.destroy = wlr_drm_output_destroy,
};

static int32_t calculate_refresh_rate(drmModeModeInfo *mode) {
int32_t refresh = (mode->clock * 1000000LL / mode->htotal +
mode->vtotal / 2) / mode->vtotal;

if (mode->flags & DRM_MODE_FLAG_INTERLACE)
refresh *= 2;
if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
refresh /= 2;
if (mode->vscan > 1)
refresh /= mode->vscan;

return refresh;
}

static void scan_property_ids(int fd, drmModeConnector *conn,
struct wlr_output_state *output) {
for (int i = 0; i < conn->count_props; ++i) {
drmModePropertyRes *prop = drmModeGetProperty(fd, conn->props[i]);
if (!prop) {
continue;
}

// I think this is guranteed to exist
if (strcmp(prop->name, "DPMS") == 0) {
output->props.dpms = prop->prop_id;

/* There may be more properties we want to get,
* but since it's currently only this, we exit early
*/

drmModeFreeProperty(prop);
break;
}

drmModeFreeProperty(prop);
}
}

void wlr_drm_scan_connectors(struct wlr_backend_state *state) {
wlr_log(L_INFO, "Scanning DRM connectors");

Expand Down Expand Up @@ -300,6 +338,8 @@ void wlr_drm_scan_connectors(struct wlr_backend_state *state) {
free(curr_enc);
}

scan_property_ids(state->fd, conn, output);

list_add(state->outputs, output);
wlr_log(L_INFO, "Found display '%s'", output->name);
} else {
Expand All @@ -321,14 +361,12 @@ void wlr_drm_scan_connectors(struct wlr_backend_state *state) {
struct wlr_output_mode *mode = calloc(1,
sizeof(struct wlr_output_mode));
mode->width = _state->mode.hdisplay;
// TODO: Calculate more accurate refresh rate
// TODO: Check that this refresh rate is mHz
mode->height = _state->mode.vdisplay;
mode->refresh = calculate_refresh_rate(&_state->mode);
mode->state = _state;

wlr_log(L_INFO, " %"PRIu16"@%"PRIu16"@%"PRIu32,
_state->mode.hdisplay, _state->mode.vdisplay,
_state->mode.vrefresh);
wlr_log(L_INFO, " %"PRId32"@%"PRId32"@%"PRId32,
mode->width, mode->height, mode->refresh);

list_add(wlr_output->modes, mode);
}
Expand Down Expand Up @@ -416,3 +454,23 @@ void wlr_drm_output_cleanup(struct wlr_output_state *output, bool restore) {
}
// TODO: free wlr_output
}

void wlr_drm_output_dpms(int fd, struct wlr_output_state *output, bool screen_on) {
if (output->state != DRM_OUTPUT_CONNECTED) {
return;
}

if (screen_on) {
drmModeConnectorSetProperty(fd, output->connector, output->props.dpms,
DRM_MODE_DPMS_ON);

// Start rendering loop again by drawing a black frame
wlr_drm_output_begin(output->wlr_output);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
wlr_drm_output_end(output->wlr_output);
} else {
drmModeConnectorSetProperty(fd, output->connector, output->props.dpms,
DRM_MODE_DPMS_STANDBY);
}
}
22 changes: 20 additions & 2 deletions example/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ int timer_done(void *data) {
return 1;
}

int dpms_on(void *data) {
struct wlr_backend *backend = data;
wlr_drm_backend_dpms(backend, false);
return 1;
}

int dpms_off(void *data) {
struct wlr_backend *backend = data;
wlr_drm_backend_dpms(backend, true);
return 1;
}

int main() {
if (getenv("DISPLAY")) {
fprintf(stderr, "Detected that X is running. Run this in its own virtual terminal.\n");
Expand Down Expand Up @@ -120,8 +132,14 @@ int main() {
bool done = false;
struct wl_event_source *timer = wl_event_loop_add_timer(event_loop,
timer_done, &done);

wl_event_source_timer_update(timer, 5000);
struct wl_event_source *timer_dpms_on = wl_event_loop_add_timer(event_loop,
dpms_on, wlr);
struct wl_event_source *timer_dpms_off = wl_event_loop_add_timer(event_loop,
dpms_off, wlr);

wl_event_source_timer_update(timer, 20000);
wl_event_source_timer_update(timer_dpms_on, 5000);
wl_event_source_timer_update(timer_dpms_off, 10000);

while (!done) {
wl_event_loop_dispatch(event_loop, 0);
Expand Down
5 changes: 5 additions & 0 deletions include/backend/drm/drm.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ struct wlr_output_state {
uint32_t connector;
char name[16];

struct {
uint32_t dpms;
} props;

uint32_t width;
uint32_t height;

Expand All @@ -51,6 +55,7 @@ struct wlr_output_state {
};

void wlr_drm_output_cleanup(struct wlr_output_state *output, bool restore);
void wlr_drm_output_dpms(int fd, struct wlr_output_state *output, bool screen_on);

void wlr_drm_scan_connectors(struct wlr_backend_state *state);
int wlr_drm_event(int fd, uint32_t mask, void *data);
Expand Down
2 changes: 2 additions & 0 deletions include/wlr/backend/drm.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
struct wlr_backend *wlr_drm_backend_create(struct wl_display *display,
struct wlr_session *session);

void wlr_drm_backend_dpms(struct wlr_backend *backend, bool screen_on);

void wlr_drm_output_begin(struct wlr_output *out);
void wlr_drm_output_end(struct wlr_output *out);

Expand Down