Skip to content

Commit

Permalink
v4p: dpms
Browse files Browse the repository at this point in the history
  • Loading branch information
mdevaev committed Mar 9, 2024
1 parent 7228502 commit c333e75
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
64 changes: 59 additions & 5 deletions src/v4p/drm.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@

static void _drm_vsync_callback(int fd, uint n_frame, uint sec, uint usec, void *v_buf);
static int _drm_check_status(us_drm_s *drm);
static void _drm_ensure_dpms_power(us_drm_s *drm, bool on);
static int _drm_init_buffers(us_drm_s *drm, const us_device_s *dev);
static int _drm_find_sink(us_drm_s *drm, uint width, uint height, float hz);

static drmModeModeInfo *_find_best_mode(drmModeConnector *conn, uint width, uint height, float hz);
static u32 _find_dpms(int fd, drmModeConnector *conn);
static u32 _find_crtc(int fd, drmModeRes *res, drmModeConnector *conn, u32 *taken_crtcs);
static const char *_connector_type_to_string(u32 type);
static float _get_refresh_rate(const drmModeModeInfo *mode);
Expand All @@ -68,6 +70,7 @@ us_drm_s *us_drm_init(void) {
run->fd = -1;
run->status_fd = -1;
run->has_vsync = true;
run->dpms_state = -1;
run->ft = us_frametext_init();

us_drm_s *drm;
Expand Down Expand Up @@ -219,13 +222,25 @@ void us_drm_close(us_drm_s *drm) {

run->crtc_id = 0;
run->has_vsync = true;
run->dpms_state = -1;
run->stub_n_buf = 0;

if (say) {
_D_LOG_INFO("Closed");
}
}

int us_drm_dpms_power_off(us_drm_s *drm) {
assert(drm->run->fd >= 0);
switch (_drm_check_status(drm)) {
case 0: break;
case -2: return -2;
default: return -1;
}
_drm_ensure_dpms_power(drm, false);
return 0;
}

int us_drm_wait_for_vsync(us_drm_s *drm) {
us_drm_runtime_s *const run = drm->run;

Expand All @@ -236,6 +251,7 @@ int us_drm_wait_for_vsync(us_drm_s *drm) {
case -2: return -2;
default: return -1;
}
_drm_ensure_dpms_power(drm, true);

if (run->has_vsync) {
return 0;
Expand Down Expand Up @@ -289,6 +305,7 @@ int us_drm_expose_stub(us_drm_s *drm, us_drm_stub_e stub, const us_device_s *dev
case -2: return -2;
default: return -1;
}
_drm_ensure_dpms_power(drm, true);

# define DRAW_MSG(x_msg) us_frametext_draw(run->ft, (x_msg), run->mode.hdisplay, run->mode.vdisplay)
switch (stub) {
Expand Down Expand Up @@ -357,6 +374,7 @@ int us_drm_expose_dma(us_drm_s *drm, const us_hw_buffer_s *hw) {
case -2: return -2;
default: return -1;
}
_drm_ensure_dpms_power(drm, true);

run->has_vsync = false;

Expand Down Expand Up @@ -412,6 +430,20 @@ static int _drm_check_status(us_drm_s *drm) {
return -1;
}

static void _drm_ensure_dpms_power(us_drm_s *drm, bool on) {
us_drm_runtime_s *const run = drm->run;
if (run->dpms_id > 0 && run->dpms_state != (int)on) {
_D_LOG_INFO("Changing DPMS power mode: %d -> %u ...", run->dpms_state, on);
if (drmModeConnectorSetProperty(
run->fd, run->conn_id, run->dpms_id,
(on ? DRM_MODE_DPMS_ON : DRM_MODE_DPMS_OFF)
) < 0) {
_D_LOG_PERROR("Can't set DPMS power=%u (ignored)", on);
}
}
run->dpms_state = (int)on;
}

static int _drm_init_buffers(us_drm_s *drm, const us_device_s *dev) {
us_drm_runtime_s *const run = drm->run;

Expand Down Expand Up @@ -517,7 +549,7 @@ static int _drm_find_sink(us_drm_s *drm, uint width, uint height, float hz) {
drmModeFreeConnector(conn);
continue;
}
_D_LOG_DEBUG("Found connector for port %s: conn_type=%d, conn_type_id=%d",
_D_LOG_INFO("Using connector %s: conn_type=%d, conn_type_id=%d",
drm->port, conn->connector_type, conn->connector_type_id);

if (conn->connection != DRM_MODE_CONNECTED) {
Expand All @@ -532,18 +564,23 @@ static int _drm_find_sink(us_drm_s *drm, uint width, uint height, float hz) {
drmModeFreeConnector(conn);
goto unplugged;
}
assert(best->hdisplay > 0);
assert(best->vdisplay > 0);

_D_LOG_INFO("The best display mode is: %ux%up%.02f",
_D_LOG_INFO("Using best mode: %ux%up%.02f",
best->hdisplay, best->vdisplay, _get_refresh_rate(best));

if ((run->dpms_id = _find_dpms(run->fd, conn)) > 0) {
_D_LOG_INFO("Using DPMS: id=%u", run->dpms_id);
} else {
_D_LOG_INFO("Using DPMS: None");
}

u32 taken_crtcs = 0; // Unused here
if ((run->crtc_id = _find_crtc(run->fd, res, conn, &taken_crtcs)) == 0) {
_D_LOG_ERROR("Can't find CRTC");
drmModeFreeConnector(conn);
goto done;
}
_D_LOG_INFO("Using CRTC: id=%u", run->crtc_id);

run->conn_id = conn->connector_id;
memcpy(&run->mode, best, sizeof(drmModeModeInfo));

Expand Down Expand Up @@ -596,9 +633,26 @@ static drmModeModeInfo *_find_best_mode(drmModeConnector *conn, uint width, uint
if (best == NULL) {
best = (conn->count_modes > 0 ? &conn->modes[0] : NULL);
}
assert(best == NULL || best->hdisplay > 0);
assert(best == NULL || best->vdisplay > 0);
return best;
}

static u32 _find_dpms(int fd, drmModeConnector *conn) {
for (int pi = 0; pi < conn->count_props; pi++) {
drmModePropertyPtr prop = drmModeGetProperty(fd, conn->props[pi]);
if (prop != NULL) {
if (!strcmp(prop->name, "DPMS")) {
const u32 id = prop->prop_id;
drmModeFreeProperty(prop);
return id;
}
drmModeFreeProperty(prop);
}
}
return 0;
}

static u32 _find_crtc(int fd, drmModeRes *res, drmModeConnector *conn, u32 *taken_crtcs) {
for (int ei = 0; ei < conn->count_encoders; ++ei) {
drmModeEncoder *enc = drmModeGetEncoder(fd, conn->encoders[ei]);
Expand Down
7 changes: 5 additions & 2 deletions src/v4p/drm.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ typedef struct {
int fd;
u32 crtc_id;
u32 conn_id;
u32 dpms_id;
drmModeModeInfo mode;
us_drm_buffer_s *bufs;
uint n_bufs;
drmModeCrtc *saved_crtc;
int dpms_state;
bool has_vsync;
bool stub;
uint stub_n_buf;
bool stub;
us_frametext_s *ft;
bool unplugged_reported;
} us_drm_runtime_s;
Expand All @@ -82,6 +84,7 @@ void us_drm_destroy(us_drm_s *drm);
int us_drm_open(us_drm_s *drm, const us_device_s *dev);
void us_drm_close(us_drm_s *drm);

int us_drm_dpms_power_off(us_drm_s *drm);
int us_drm_wait_for_vsync(us_drm_s *drm);
int us_drm_expose_stub(us_drm_s *drm, us_drm_stub_e stub, const us_device_s *dev);
int us_drm_expose_dma(us_drm_s *drm, const us_hw_buffer_s *hw);
int us_drm_wait_for_vsync(us_drm_s *drm);
7 changes: 5 additions & 2 deletions src/v4p/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ static void _main_loop(void) {
assert(drm_opened > 0);

if (atomic_load(&_g_ustreamer_online)) {
US_ONCE({ US_LOG_ERROR("DRM: Online stream is active"); });
US_ONCE({ US_LOG_ERROR("DRM: Online stream is active, stopping capture ..."); });
if (us_drm_wait_for_vsync(drm) < 0) {
goto close;
}
Expand All @@ -194,11 +194,14 @@ static void _main_loop(void) {
}

if (us_device_open(dev) < 0) {
if (us_drm_wait_for_vsync(drm) < 0) {
/*if (us_drm_wait_for_vsync(drm) < 0) {
goto close;
}
if (us_drm_expose_stub(drm, US_DRM_STUB_NO_SIGNAL, NULL) < 0) {
goto close;
}*/
if (us_drm_dpms_power_off(drm) < 0) {
goto close;
}
_slowdown();
continue;
Expand Down

0 comments on commit c333e75

Please sign in to comment.