ext-image-capture-source-v1: add wlr_ext_image_capture_source_v1_set_constraints_from_swapchain()

This commit is contained in:
Simon Ser 2024-04-18 18:39:28 +02:00
parent c24efad6df
commit 855b3fd607
2 changed files with 75 additions and 0 deletions

View file

@ -1,7 +1,14 @@
#include <assert.h>
#include <drm_fourcc.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <wlr/interfaces/wlr_ext_image_capture_source_v1.h>
#include <wlr/render/allocator.h>
#include <wlr/render/swapchain.h>
#include <wlr/render/wlr_renderer.h>
#include <wlr/types/wlr_buffer.h>
#include <wlr/types/wlr_ext_image_capture_source_v1.h>
#include <wlr/util/log.h>
#include "ext-image-capture-source-v1-protocol.h"
static void source_handle_destroy(struct wl_client *client,
@ -70,6 +77,69 @@ bool wlr_ext_image_capture_source_v1_create_resource(struct wlr_ext_image_captur
return true;
}
static uint32_t get_swapchain_shm_format(struct wlr_swapchain *swapchain,
struct wlr_renderer *renderer) {
struct wlr_buffer *buffer = wlr_swapchain_acquire(swapchain);
if (buffer == NULL) {
return DRM_FORMAT_INVALID;
}
struct wlr_texture *texture = wlr_texture_from_buffer(renderer, buffer);
wlr_buffer_unlock(buffer);
if (texture == NULL) {
return DRM_FORMAT_INVALID;
}
uint32_t format = wlr_texture_preferred_read_format(texture);
wlr_texture_destroy(texture);
return format;
}
bool wlr_ext_image_capture_source_v1_set_constraints_from_swapchain(struct wlr_ext_image_capture_source_v1 *source,
struct wlr_swapchain *swapchain, struct wlr_renderer *renderer) {
source->width = swapchain->width;
source->height = swapchain->height;
uint32_t shm_format = get_swapchain_shm_format(swapchain, renderer);
if (shm_format != DRM_FORMAT_INVALID) {
uint32_t *shm_formats = calloc(1, sizeof(shm_formats[0]));
if (shm_formats == NULL) {
wlr_log(WLR_ERROR, "Allocation failed");
return false;
}
shm_formats[0] = shm_format;
source->shm_formats_len = 1;
free(source->shm_formats);
source->shm_formats = shm_formats;
}
int drm_fd = wlr_renderer_get_drm_fd(renderer);
if (swapchain->allocator != NULL &&
(swapchain->allocator->buffer_caps & WLR_BUFFER_CAP_DMABUF) &&
drm_fd >= 0) {
struct stat dev_stat;
if (fstat(drm_fd, &dev_stat) != 0) {
wlr_log_errno(WLR_ERROR, "fstat() failed");
return false;
}
source->dmabuf_device = dev_stat.st_rdev;
wlr_drm_format_set_finish(&source->dmabuf_formats);
source->dmabuf_formats = (struct wlr_drm_format_set){0};
for (size_t i = 0; i < swapchain->format.len; i++) {
wlr_drm_format_set_add(&source->dmabuf_formats,
swapchain->format.format, swapchain->format.modifiers[i]);
}
}
wl_signal_emit_mutable(&source->events.constraints_update, NULL);
return true;
}
void wlr_ext_image_capture_source_v1_cursor_init(struct wlr_ext_image_capture_source_v1_cursor *source_cursor,
const struct wlr_ext_image_capture_source_v1_interface *impl) {
*source_cursor = (struct wlr_ext_image_capture_source_v1_cursor){0};