refactoring

This commit is contained in:
Maxim Devaev
2024-03-29 15:36:43 +02:00
parent caf9ed7bfe
commit 8e6c374acf
7 changed files with 94 additions and 92 deletions

View File

@@ -72,6 +72,7 @@ us_drm_s *us_drm_init(void) {
run->fd = -1;
run->status_fd = -1;
run->dpms_state = -1;
run->opened = -1;
run->has_vsync = true;
run->exposing_dma_fd = -1;
run->ft = us_frametext_init();
@@ -82,6 +83,7 @@ us_drm_s *us_drm_init(void) {
drm->path = "/dev/dri/by-path/platform-gpu-card";
drm->port = "HDMI-A-2"; // OUT2 on PiKVM V4 Plus
drm->timeout = 5;
drm->blank_after = 5;
drm->run = run;
return drm;
}
@@ -97,6 +99,8 @@ int us_drm_open(us_drm_s *drm, const us_capture_s *cap) {
assert(run->fd < 0);
_LOG_INFO("Using passthrough: %s[%s]", drm->path, drm->port);
switch (_drm_check_status(drm)) {
case 0: break;
case US_ERROR_NO_DEVICE: goto unplugged;
@@ -164,22 +168,24 @@ int us_drm_open(us_drm_s *drm, const us_capture_s *cap) {
goto error;
}
run->opened_for_stub = (stub > 0);
_LOG_INFO("Opened for %s ...", (stub > 0 ? "STUB" : "DMA"));
run->exposing_dma_fd = -1;
run->unplugged_once = 0;
_LOG_INFO("Opened for %s ...", (run->opened_for_stub ? "STUB" : "DMA"));
return stub;
run->blank_at_ts = 0;
run->opened = stub;
run->once = 0;
return run->opened;
error:
us_drm_close(drm);
return -1;
return run->opened; // -1 after us_drm_close()
unplugged:
US_ONCE_FOR(run->unplugged_once, __LINE__, {
US_ONCE_FOR(run->once, __LINE__, {
_LOG_ERROR("Display is not plugged");
});
us_drm_close(drm);
return US_ERROR_NO_DEVICE;
run->opened = US_ERROR_NO_DEVICE;
return run->opened;
}
void us_drm_close(us_drm_s *drm) {
@@ -233,6 +239,7 @@ void us_drm_close(us_drm_s *drm) {
run->crtc_id = 0;
run->dpms_state = -1;
run->opened = -1;
run->has_vsync = true;
run->stub_n_buf = 0;
@@ -241,6 +248,34 @@ void us_drm_close(us_drm_s *drm) {
}
}
int us_drm_ensure_no_signal(us_drm_s *drm) {
us_drm_runtime_s *const run = drm->run;
assert(run->fd >= 0);
assert(run->opened > 0);
const ldf now_ts = us_get_now_monotonic();
if (run->blank_at_ts == 0) {
run->blank_at_ts = now_ts + drm->blank_after;
}
const ldf saved_ts = run->blank_at_ts; // us_drm*() rewrites it to 0
int retval;
if (now_ts <= run->blank_at_ts) {
retval = us_drm_wait_for_vsync(drm);
if (retval == 0) {
retval = us_drm_expose_stub(drm, US_DRM_STUB_NO_SIGNAL, NULL);
}
} else {
US_ONCE_FOR(run->once, __LINE__, {
_LOG_INFO("Turning off the display by timeout ...");
});
retval = us_drm_dpms_power_off(drm);
}
run->blank_at_ts = saved_ts;
return retval;
}
int us_drm_dpms_power_off(us_drm_s *drm) {
assert(drm->run->fd >= 0);
switch (_drm_check_status(drm)) {
@@ -259,6 +294,7 @@ int us_drm_wait_for_vsync(us_drm_s *drm) {
us_drm_runtime_s *const run = drm->run;
assert(run->fd >= 0);
run->blank_at_ts = 0;
switch (_drm_check_status(drm)) {
case 0: break;
@@ -313,7 +349,8 @@ int us_drm_expose_stub(us_drm_s *drm, us_drm_stub_e stub, const us_capture_s *ca
us_drm_runtime_s *const run = drm->run;
assert(run->fd >= 0);
assert(run->opened_for_stub);
assert(run->opened > 0);
run->blank_at_ts = 0;
switch (_drm_check_status(drm)) {
case 0: break;
@@ -377,7 +414,8 @@ int us_drm_expose_dma(us_drm_s *drm, const us_capture_hwbuf_s *hw) {
us_drm_buffer_s *const buf = &run->bufs[hw->buf.index];
assert(run->fd >= 0);
assert(!run->opened_for_stub);
assert(run->opened == 0);
run->blank_at_ts = 0;
switch (_drm_check_status(drm)) {
case 0: break;

View File

@@ -63,11 +63,14 @@ typedef struct {
uint n_bufs;
drmModeCrtc *saved_crtc;
int dpms_state;
bool opened_for_stub;
int opened;
bool has_vsync;
int exposing_dma_fd;
uint stub_n_buf;
int unplugged_once;
ldf blank_at_ts;
int once;
us_frametext_s *ft;
} us_drm_runtime_s;
@@ -75,6 +78,7 @@ typedef struct {
char *path;
char *port;
uint timeout;
uint blank_after;
us_drm_runtime_s *run;
} us_drm_s;
@@ -90,3 +94,4 @@ 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_capture_s *cap);
int us_drm_expose_dma(us_drm_s *drm, const us_capture_hwbuf_s *hw);
int us_drm_ensure_no_signal(us_drm_s *drm);