Skip to content

Commit 11956ee

Browse files
committed
src/mirror-screencopy, src/mirror-extcopy: deduplicate init code for shm/dmabuf
1 parent bb91cb9 commit 11956ee

2 files changed

Lines changed: 46 additions & 106 deletions

File tree

src/mirror-extcopy.c

Lines changed: 20 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -412,35 +412,31 @@ static void on_options_updated(ctx_t * ctx) {
412412

413413
// --- wlm_mirror_extcopy_init ---
414414

415-
void wlm_mirror_extcopy_shm_init(ctx_t * ctx) {
415+
static void wlm_mirror_extcopy_init(ctx_t * ctx, bool use_dmabuf) {
416416
// check for required protocols
417-
if (ctx->wl.shm == NULL) {
417+
if (!use_dmabuf && ctx->wl.shm == NULL) {
418418
wlm_log_error("mirror-extcopy::shm_init(): missing wl_shm protocol\n");
419419
return;
420-
}
421-
422-
if (ctx->wl.copy_capture_manager == NULL) {
423-
wlm_log_error("mirror-extcopy::shm_init(): missing ext_image_copy_capture protocol\n");
420+
} else if (ctx->wl.copy_capture_manager == NULL) {
421+
wlm_log_error("mirror-extcopy::init(): missing ext_image_copy_capture protocol\n");
424422
return;
425-
}
426-
427-
if (ctx->wl.output_capture_source_manager == NULL) {
428-
wlm_log_error("mirror-extcopy::shm_init(): missing ext_output_image_capture_source_manager protocol\n");
423+
} else if (ctx->wl.output_capture_source_manager == NULL) {
424+
wlm_log_error("mirror-extcopy::init(): missing ext_output_image_capture_source_manager protocol\n");
429425
return;
430426
}
431427

432428
// allocate backend context structure
433429
extcopy_mirror_backend_t * backend = calloc(1, sizeof (extcopy_mirror_backend_t));
434430
if (backend == NULL) {
435-
wlm_log_error("mirror-extcopy::shm_init(): failed to allocate backend state\n");
431+
wlm_log_error("mirror-extcopy::init(): failed to allocate backend state\n");
436432
return;
437433
}
438434

439435
backend->header.do_capture = do_capture;
440436
backend->header.do_cleanup = do_cleanup;
441437
backend->header.on_options_updated = on_options_updated;
442438
backend->header.fail_count = 0;
443-
backend->use_dmabuf = false;
439+
backend->use_dmabuf = use_dmabuf;
444440

445441
backend->capture_source = NULL;
446442
backend->capture_session = NULL;
@@ -459,53 +455,20 @@ void wlm_mirror_extcopy_shm_init(ctx_t * ctx) {
459455
// set backend object as current backend
460456
ctx->mirror.backend = (mirror_backend_t *)backend;
461457

462-
// create shm pool
463-
if (!wlm_wayland_shm_create_pool(ctx)) {
464-
wlm_log_error("mirror-extcopy::shm_init(): failed to create shm pool\n");
465-
wlm_mirror_backend_fail(ctx);
466-
return;
458+
if (!use_dmabuf) {
459+
// create shm pool
460+
if (!wlm_wayland_shm_create_pool(ctx)) {
461+
wlm_log_error("mirror-extcopy::shm_init(): failed to create shm pool\n");
462+
wlm_mirror_backend_fail(ctx);
463+
return;
464+
}
467465
}
468466
}
469467

470-
void wlm_mirror_extcopy_dmabuf_init(ctx_t * ctx) {
471-
// check for required protocols
472-
if (ctx->wl.copy_capture_manager == NULL) {
473-
wlm_log_error("mirror-extcopy::dmabuf_init(): missing ext_image_copy_capture protocol\n");
474-
return;
475-
}
476-
477-
if (ctx->wl.output_capture_source_manager == NULL) {
478-
wlm_log_error("mirror-extcopy::dmabuf_init(): missing ext_output_image_capture_source_manager protocol\n");
479-
return;
480-
}
481-
482-
// allocate backend context structure
483-
extcopy_mirror_backend_t * backend = calloc(1, sizeof (extcopy_mirror_backend_t));
484-
if (backend == NULL) {
485-
wlm_log_error("mirror-extcopy::dmabuf_init(): failed to allocate backend state\n");
486-
return;
487-
}
488-
489-
backend->header.do_capture = do_capture;
490-
backend->header.do_cleanup = do_cleanup;
491-
backend->header.on_options_updated = on_options_updated;
492-
backend->header.fail_count = 0;
493-
backend->use_dmabuf = true;
494-
495-
backend->capture_source = NULL;
496-
backend->capture_session = NULL;
497-
backend->capture_frame = NULL;
498-
499-
backend->frame_width = 0;
500-
backend->frame_height = 0;
501-
backend->frame_shm_stride = 0;
502-
backend->frame_shm_format = 0;
503-
backend->frame_drm_format = 0;
504-
backend->frame_drm_modifiers = NULL;
505-
backend->frame_num_drm_modifiers = 0;
506-
507-
backend->state = STATE_INIT;
468+
void wlm_mirror_extcopy_shm_init(ctx_t * ctx) {
469+
wlm_mirror_extcopy_init(ctx, false);
470+
}
508471

509-
// set backend object as current backend
510-
ctx->mirror.backend = (mirror_backend_t *)backend;
472+
void wlm_mirror_extcopy_dmabuf_init(ctx_t * ctx) {
473+
wlm_mirror_extcopy_init(ctx, true);
511474
}

src/mirror-screencopy.c

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -338,20 +338,23 @@ static void on_dmabuf_device_opened(ctx_t * ctx, bool success) {
338338

339339
// --- init_mirror_screencopy ---
340340

341-
void wlm_mirror_screencopy_shm_init(ctx_t * ctx) {
341+
static void wlm_mirror_screencopy_init(ctx_t * ctx, bool use_dmabuf) {
342342
// check for required protocols
343-
if (ctx->wl.shm == NULL) {
343+
if (use_dmabuf && ctx->wl.linux_dmabuf == NULL) {
344+
wlm_log_error("mirror-screencopy::dmabuf_init(): missing linux_dmabuf protocol\n");
345+
return;
346+
} else if (!use_dmabuf && ctx->wl.shm == NULL) {
344347
wlm_log_error("mirror-screencopy::shm_init(): missing wl_shm protocol\n");
345348
return;
346349
} else if (ctx->wl.screencopy_manager == NULL) {
347-
wlm_log_error("mirror-screencopy::shm_init(): missing wlr_screencopy protocol\n");
350+
wlm_log_error("mirror-screencopy::init(): missing wlr_screencopy protocol\n");
348351
return;
349352
}
350353

351354
// allocate backend context structure
352355
screencopy_mirror_backend_t * backend = calloc(1, sizeof (screencopy_mirror_backend_t));
353356
if (backend == NULL) {
354-
wlm_log_error("mirror-screencopy::shm_init(): failed to allocate backend state\n");
357+
wlm_log_error("mirror-screencopy::init(): failed to allocate backend state\n");
355358
return;
356359
}
357360

@@ -360,7 +363,7 @@ void wlm_mirror_screencopy_shm_init(ctx_t * ctx) {
360363
backend->header.do_cleanup = do_cleanup;
361364
backend->header.on_options_updated = NULL;
362365
backend->header.fail_count = 0;
363-
backend->use_dmabuf = false;
366+
backend->use_dmabuf = use_dmabuf;
364367

365368
backend->screencopy_frame = NULL;
366369

@@ -370,56 +373,30 @@ void wlm_mirror_screencopy_shm_init(ctx_t * ctx) {
370373
backend->frame_format = 0;
371374
backend->frame_flags = 0;
372375

373-
backend->state = STATE_READY;
374-
375376
// set backend object as current backend
376377
ctx->mirror.backend = (mirror_backend_t *)backend;
377378

378-
// create shm pool
379-
if (!wlm_wayland_shm_create_pool(ctx)) {
380-
wlm_log_error("mirror-screencopy::shm_init(): failed to create shm pool\n");
381-
wlm_mirror_backend_fail(ctx);
382-
return;
383-
}
384-
}
379+
if (use_dmabuf) {
380+
backend->state = STATE_WAIT_DMABUF_DEVICE;
385381

386-
void wlm_mirror_screencopy_dmabuf_init(ctx_t * ctx) {
387-
// check for required protocols
388-
if (ctx->wl.linux_dmabuf == NULL) {
389-
wlm_log_error("mirror-screencopy::dmabuf_init(): missing linux_dmabuf protocol\n");
390-
return;
391-
} else if (ctx->wl.screencopy_manager == NULL) {
392-
wlm_log_error("mirror-screencopy::dmabuf_init(): missing wlr_screencopy protocol\n");
393-
return;
394-
}
382+
// open dmabuf device
383+
wlm_wayland_dmabuf_open_main_device(ctx, on_dmabuf_device_opened);
384+
} else {
385+
backend->state = STATE_READY;
395386

396-
// allocate backend context structure
397-
screencopy_mirror_backend_t * backend = calloc(1, sizeof (screencopy_mirror_backend_t));
398-
if (backend == NULL) {
399-
wlm_log_error("mirror-screencopy::dmabuf_init(): failed to allocate backend state\n");
400-
return;
387+
// create shm pool
388+
if (!wlm_wayland_shm_create_pool(ctx)) {
389+
wlm_log_error("mirror-screencopy::shm_init(): failed to create shm pool\n");
390+
wlm_mirror_backend_fail(ctx);
391+
return;
392+
}
401393
}
394+
}
402395

403-
// initialize context structure
404-
backend->header.do_capture = do_capture;
405-
backend->header.do_cleanup = do_cleanup;
406-
backend->header.on_options_updated = NULL;
407-
backend->header.fail_count = 0;
408-
backend->use_dmabuf = true;
409-
410-
backend->screencopy_frame = NULL;
411-
412-
backend->frame_width = 0;
413-
backend->frame_height = 0;
414-
backend->frame_stride = 0;
415-
backend->frame_format = 0;
416-
backend->frame_flags = 0;
417-
418-
backend->state = STATE_WAIT_DMABUF_DEVICE;
419-
420-
// set backend object as current backend
421-
ctx->mirror.backend = (mirror_backend_t *)backend;
396+
void wlm_mirror_screencopy_shm_init(ctx_t * ctx) {
397+
wlm_mirror_screencopy_init(ctx, false);
398+
}
422399

423-
// open dmabuf device
424-
wlm_wayland_dmabuf_open_main_device(ctx, on_dmabuf_device_opened);
400+
void wlm_mirror_screencopy_dmabuf_init(ctx_t * ctx) {
401+
wlm_mirror_screencopy_init(ctx, true);
425402
}

0 commit comments

Comments
 (0)