aboutsummaryrefslogtreecommitdiff
path: root/src/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c435
1 files changed, 84 insertions, 351 deletions
diff --git a/src/utils.c b/src/utils.c
index be5e58b..4aab138 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -1,4 +1,5 @@
#include "../include/utils.h"
+#include "../include/window/window.h"
#include <time.h>
#include <string.h>
@@ -13,10 +14,8 @@
#include <xf86drmMode.h>
#include <xf86drm.h>
-#include <libdrm/drm_fourcc.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xrandr.h>
-#include <va/va_drmcommon.h>
#include <libavcodec/avcodec.h>
#include <libavutil/hwcontext_vaapi.h>
@@ -47,16 +46,6 @@ bool generate_random_characters_standard_alphabet(char *buffer, int buffer_size)
return generate_random_characters(buffer, buffer_size, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", 62);
}
-static gsr_monitor_rotation wayland_transform_to_gsr_rotation(int32_t rot) {
- switch(rot) {
- case 0: return GSR_MONITOR_ROT_0;
- case 1: return GSR_MONITOR_ROT_90;
- case 2: return GSR_MONITOR_ROT_180;
- case 3: return GSR_MONITOR_ROT_270;
- }
- return GSR_MONITOR_ROT_0;
-}
-
static const XRRModeInfo* get_mode_info(const XRRScreenResources *sr, RRMode id) {
for(int i = 0; i < sr->nmode; ++i) {
if(sr->modes[i].id == id)
@@ -146,31 +135,22 @@ void for_each_active_monitor_output_x11_not_cached(Display *display, active_moni
XRRFreeScreenResources(screen_res);
}
-void for_each_active_monitor_output_x11(const gsr_egl *egl, active_monitor_callback callback, void *userdata) {
- for(int i = 0; i < egl->x11.num_outputs; ++i) {
- const gsr_x11_output *output = &egl->x11.outputs[i];
- const gsr_monitor monitor = {
- .name = output->name,
- .name_len = strlen(output->name),
- .pos = output->pos,
- .size = output->size,
- .connector_id = output->connector_id,
- .rotation = output->rotation,
- .monitor_identifier = output->monitor_identifier
- };
- callback(&monitor, userdata);
- }
+/* TODO: Support more connector types */
+int get_connector_type_by_name(const char *name) {
+ int len = strlen(name);
+ if(len >= 5 && strncmp(name, "HDMI-", 5) == 0)
+ return 1;
+ else if(len >= 3 && strncmp(name, "DP-", 3) == 0)
+ return 2;
+ else if(len >= 12 && strncmp(name, "DisplayPort-", 12) == 0)
+ return 3;
+ else if(len >= 4 && strncmp(name, "eDP-", 4) == 0)
+ return 4;
+ else
+ return -1;
}
-typedef struct {
- int type;
- int count;
- int count_active;
-} drm_connector_type_count;
-
-#define CONNECTOR_TYPE_COUNTS 32
-
-static drm_connector_type_count* drm_connector_types_get_index(drm_connector_type_count *type_counts, int *num_type_counts, int connector_type) {
+drm_connector_type_count* drm_connector_types_get_index(drm_connector_type_count *type_counts, int *num_type_counts, int connector_type) {
for(int i = 0; i < *num_type_counts; ++i) {
if(type_counts[i].type == connector_type)
return &type_counts[i];
@@ -187,6 +167,10 @@ static drm_connector_type_count* drm_connector_types_get_index(drm_connector_typ
return &type_counts[index];
}
+uint32_t monitor_identifier_from_type_and_count(int monitor_type_index, int monitor_type_count) {
+ return ((uint32_t)monitor_type_index << 16) | ((uint32_t)monitor_type_count);
+}
+
static bool connector_get_property_by_name(int drmfd, drmModeConnectorPtr props, const char *name, uint64_t *result) {
for(int i = 0; i < props->count_props; ++i) {
drmModePropertyPtr prop = drmModeGetProperty(drmfd, props->props[i]);
@@ -202,61 +186,12 @@ static bool connector_get_property_by_name(int drmfd, drmModeConnectorPtr props,
return false;
}
-/* TODO: Support more connector types */
-static int get_connector_type_by_name(const char *name) {
- int len = strlen(name);
- if(len >= 5 && strncmp(name, "HDMI-", 5) == 0)
- return 1;
- else if(len >= 3 && strncmp(name, "DP-", 3) == 0)
- return 2;
- else if(len >= 12 && strncmp(name, "DisplayPort-", 12) == 0)
- return 3;
- else if(len >= 4 && strncmp(name, "eDP-", 4) == 0)
- return 4;
- else
- return -1;
-}
-
-static uint32_t monitor_identifier_from_type_and_count(int monitor_type_index, int monitor_type_count) {
- return ((uint32_t)monitor_type_index << 16) | ((uint32_t)monitor_type_count);
-}
-
-static void for_each_active_monitor_output_wayland(const gsr_egl *egl, active_monitor_callback callback, void *userdata) {
- drm_connector_type_count type_counts[CONNECTOR_TYPE_COUNTS];
- int num_type_counts = 0;
-
- for(int i = 0; i < egl->wayland.num_outputs; ++i) {
- const gsr_wayland_output *output = &egl->wayland.outputs[i];
- if(!output->name)
- continue;
-
- const int connector_type_index = get_connector_type_by_name(output->name);
- drm_connector_type_count *connector_type = NULL;
- if(connector_type_index != -1)
- connector_type = drm_connector_types_get_index(type_counts, &num_type_counts, connector_type_index);
-
- if(connector_type) {
- ++connector_type->count;
- ++connector_type->count_active;
- }
-
- const gsr_monitor monitor = {
- .name = output->name,
- .name_len = strlen(output->name),
- .pos = { .x = output->pos.x, .y = output->pos.y },
- .size = { .x = output->size.x, .y = output->size.y },
- .connector_id = 0,
- .rotation = wayland_transform_to_gsr_rotation(output->transform),
- .monitor_identifier = connector_type ? monitor_identifier_from_type_and_count(connector_type_index, connector_type->count_active) : 0
- };
- callback(&monitor, userdata);
- }
-}
-
-static void for_each_active_monitor_output_drm(const gsr_egl *egl, active_monitor_callback callback, void *userdata) {
- int fd = open(egl->card_path, O_RDONLY);
- if(fd == -1)
+static void for_each_active_monitor_output_drm(const char *card_path, active_monitor_callback callback, void *userdata) {
+ int fd = open(card_path, O_RDONLY);
+ if(fd == -1) {
+ fprintf(stderr, "gsr error: for_each_active_monitor_output_drm failed, failed to open \"%s\", error: %s\n", card_path, strerror(errno));
return;
+ }
drmSetClientCap(fd, DRM_CLIENT_CAP_ATOMIC, 1);
@@ -315,16 +250,14 @@ static void for_each_active_monitor_output_drm(const gsr_egl *egl, active_monito
close(fd);
}
-void for_each_active_monitor_output(const gsr_egl *egl, gsr_connection_type connection_type, active_monitor_callback callback, void *userdata) {
+void for_each_active_monitor_output(const gsr_window *window, const char *card_path, gsr_connection_type connection_type, active_monitor_callback callback, void *userdata) {
switch(connection_type) {
case GSR_CONNECTION_X11:
- for_each_active_monitor_output_x11(egl, callback, userdata);
- break;
case GSR_CONNECTION_WAYLAND:
- for_each_active_monitor_output_wayland(egl, callback, userdata);
+ gsr_window_for_each_active_monitor_output_cached(window, callback, userdata);
break;
case GSR_CONNECTION_DRM:
- for_each_active_monitor_output_drm(egl, callback, userdata);
+ for_each_active_monitor_output_drm(card_path, callback, userdata);
break;
}
}
@@ -347,13 +280,14 @@ bool get_monitor_by_name(const gsr_egl *egl, gsr_connection_type connection_type
userdata.name_len = strlen(name);
userdata.monitor = monitor;
userdata.found_monitor = false;
- for_each_active_monitor_output(egl, connection_type, get_monitor_by_name_callback, &userdata);
+ for_each_active_monitor_output(egl->window, egl->card_path, connection_type, get_monitor_by_name_callback, &userdata);
return userdata.found_monitor;
}
typedef struct {
const gsr_monitor *monitor;
gsr_monitor_rotation rotation;
+ vec2i position;
bool match_found;
} get_monitor_by_connector_id_userdata;
@@ -365,6 +299,7 @@ static void get_monitor_by_name_and_size_callback(const gsr_monitor *monitor, vo
get_monitor_by_connector_id_userdata *data = (get_monitor_by_connector_id_userdata*)userdata;
if(monitor->name && data->monitor->name && strcmp(monitor->name, data->monitor->name) == 0 && vec2i_eql(monitor->size, data->monitor->size)) {
data->rotation = monitor->rotation;
+ data->position = monitor->pos;
data->match_found = true;
}
}
@@ -375,39 +310,51 @@ static void get_monitor_by_connector_id_callback(const gsr_monitor *monitor, voi
(!monitor->connector_id && monitor->monitor_identifier == data->monitor->monitor_identifier))
{
data->rotation = monitor->rotation;
+ data->position = monitor->pos;
data->match_found = true;
}
}
-gsr_monitor_rotation drm_monitor_get_display_server_rotation(const gsr_egl *egl, const gsr_monitor *monitor) {
- if(gsr_egl_get_display_server(egl) == GSR_DISPLAY_SERVER_WAYLAND) {
+bool drm_monitor_get_display_server_data(const gsr_window *window, const gsr_monitor *monitor, gsr_monitor_rotation *monitor_rotation, vec2i *monitor_position) {
+ *monitor_rotation = GSR_MONITOR_ROT_0;
+ *monitor_position = (vec2i){0, 0};
+
+ if(gsr_window_get_display_server(window) == GSR_DISPLAY_SERVER_WAYLAND) {
{
get_monitor_by_connector_id_userdata userdata;
userdata.monitor = monitor;
userdata.rotation = GSR_MONITOR_ROT_0;
+ userdata.position = (vec2i){0, 0};
userdata.match_found = false;
- for_each_active_monitor_output_wayland(egl, get_monitor_by_name_and_size_callback, &userdata);
- if(userdata.match_found)
- return userdata.rotation;
+ gsr_window_for_each_active_monitor_output_cached(window, get_monitor_by_name_and_size_callback, &userdata);
+ if(userdata.match_found) {
+ *monitor_rotation = userdata.rotation;
+ *monitor_position = userdata.position;
+ return true;
+ }
}
{
get_monitor_by_connector_id_userdata userdata;
userdata.monitor = monitor;
userdata.rotation = GSR_MONITOR_ROT_0;
+ userdata.position = (vec2i){0, 0};
userdata.match_found = false;
- for_each_active_monitor_output_wayland(egl, get_monitor_by_connector_id_callback, &userdata);
- return userdata.rotation;
+ gsr_window_for_each_active_monitor_output_cached(window, get_monitor_by_connector_id_callback, &userdata);
+ *monitor_rotation = userdata.rotation;
+ *monitor_position = userdata.position;
+ return userdata.match_found;
}
} else {
get_monitor_by_connector_id_userdata userdata;
userdata.monitor = monitor;
userdata.rotation = GSR_MONITOR_ROT_0;
+ userdata.position = (vec2i){0, 0};
userdata.match_found = false;
- for_each_active_monitor_output_x11(egl, get_monitor_by_connector_id_callback, &userdata);
- return userdata.rotation;
+ gsr_window_for_each_active_monitor_output_cached(window, get_monitor_by_connector_id_callback, &userdata);
+ *monitor_rotation = userdata.rotation;
+ *monitor_position = userdata.position;
+ return userdata.match_found;
}
-
- return GSR_MONITOR_ROT_0;
}
bool gl_get_gpu_info(gsr_egl *egl, gsr_gpu_info *info) {
@@ -447,6 +394,8 @@ bool gl_get_gpu_info(gsr_egl *egl, gsr_gpu_info *info) {
info->vendor = GSR_GPU_VENDOR_INTEL;
else if(strstr((const char*)gl_vendor, "NVIDIA"))
info->vendor = GSR_GPU_VENDOR_NVIDIA;
+ else if(strstr((const char*)gl_vendor, "Broadcom"))
+ info->vendor = GSR_GPU_VENDOR_BROADCOM;
else {
fprintf(stderr, "gsr error: unknown gpu vendor: %s\n", gl_vendor);
supported = false;
@@ -478,15 +427,7 @@ bool gl_get_gpu_info(gsr_egl *egl, gsr_gpu_info *info) {
return supported;
}
-static bool version_greater_than(int major, int minor, int patch, int other_major, int other_minor, int other_patch) {
- return (major > other_major) || (major == other_major && minor > other_minor) || (major == other_major && minor == other_minor && patch > other_patch);
-}
-
-bool gl_driver_version_greater_than(const gsr_egl *egl, int major, int minor, int patch) {
- return version_greater_than(egl->gpu_info.driver_major, egl->gpu_info.driver_minor, egl->gpu_info.driver_patch, major, minor, patch);
-}
-
-static bool try_card_has_valid_plane(const char *card_path) {
+bool try_card_has_valid_plane(const char *card_path) {
drmVersion *ver = NULL;
drmModePlaneResPtr planes = NULL;
bool found_screen_card = false;
@@ -700,250 +641,18 @@ static VADisplay video_codec_context_get_vaapi_display(AVCodecContext *video_cod
}
bool video_codec_context_is_vaapi(AVCodecContext *video_codec_context) {
+ if(!video_codec_context)
+ return false;
+
AVBufferRef *hw_frames_ctx = video_codec_context->hw_frames_ctx;
if(!hw_frames_ctx)
- return NULL;
+ return false;
AVHWFramesContext *hw_frame_context = (AVHWFramesContext*)hw_frames_ctx->data;
AVHWDeviceContext *device_context = (AVHWDeviceContext*)hw_frame_context->device_ctx;
return device_context->type == AV_HWDEVICE_TYPE_VAAPI;
}
-static uint32_t drm_fourcc_to_va_fourcc(uint32_t drm_fourcc) {
- switch(drm_fourcc) {
- case DRM_FORMAT_XRGB8888: return VA_FOURCC_BGRX;
- case DRM_FORMAT_XBGR8888: return VA_FOURCC_RGBX;
- case DRM_FORMAT_RGBX8888: return VA_FOURCC_XBGR;
- case DRM_FORMAT_BGRX8888: return VA_FOURCC_XRGB;
- case DRM_FORMAT_ARGB8888: return VA_FOURCC_BGRA;
- case DRM_FORMAT_ABGR8888: return VA_FOURCC_RGBA;
- case DRM_FORMAT_RGBA8888: return VA_FOURCC_ABGR;
- case DRM_FORMAT_BGRA8888: return VA_FOURCC_ARGB;
- default: return drm_fourcc;
- }
-}
-
-bool vaapi_copy_drm_planes_to_video_surface(AVCodecContext *video_codec_context, AVFrame *video_frame, vec2i source_pos, vec2i source_size, vec2i dest_pos, vec2i dest_size, uint32_t format, vec2i size, const int *fds, const uint32_t *offsets, const uint32_t *pitches, const uint64_t *modifiers, int num_planes) {
- VAConfigID config_id = 0;
- VAContextID context_id = 0;
- VASurfaceID input_surface_id = 0;
- VABufferID buffer_id = 0;
- bool success = true;
-
- VADisplay va_dpy = video_codec_context_get_vaapi_display(video_codec_context);
- if(!va_dpy) {
- success = false;
- goto done;
- }
-
- VAStatus va_status = vaCreateConfig(va_dpy, VAProfileNone, VAEntrypointVideoProc, NULL, 0, &config_id);
- if(va_status != VA_STATUS_SUCCESS) {
- fprintf(stderr, "gsr error: vaapi_copy_drm_planes_to_video_surface: vaCreateConfig failed, error: %s\n", vaErrorStr(va_status));
- success = false;
- goto done;
- }
-
- VASurfaceID output_surface_id = (uintptr_t)video_frame->data[3];
- va_status = vaCreateContext(va_dpy, config_id, size.x, size.y, VA_PROGRESSIVE, &output_surface_id, 1, &context_id);
- if(va_status != VA_STATUS_SUCCESS) {
- fprintf(stderr, "gsr error: vaapi_copy_drm_planes_to_video_surface: vaCreateContext failed, error: %s\n", vaErrorStr(va_status));
- success = false;
- goto done;
- }
-
- VADRMPRIMESurfaceDescriptor buf = {0};
- buf.fourcc = drm_fourcc_to_va_fourcc(format);//VA_FOURCC_BGRX; // TODO: VA_FOURCC_BGRA, VA_FOURCC_X2R10G10B10
- buf.width = size.x;
- buf.height = size.y;
- buf.num_objects = num_planes;
- buf.num_layers = 1;
- buf.layers[0].drm_format = format;
- buf.layers[0].num_planes = buf.num_objects;
- for(int i = 0; i < num_planes; ++i) {
- buf.objects[i].fd = fds[i];
- buf.objects[i].size = size.y * pitches[i]; // TODO:
- buf.objects[i].drm_format_modifier = modifiers[i];
-
- buf.layers[0].object_index[i] = i;
- buf.layers[0].offset[i] = offsets[i];
- buf.layers[0].pitch[i] = pitches[i];
- }
-
- VASurfaceAttrib attribs[2] = {0};
- attribs[0].type = VASurfaceAttribMemoryType;
- attribs[0].flags = VA_SURFACE_ATTRIB_SETTABLE;
- attribs[0].value.type = VAGenericValueTypeInteger;
- attribs[0].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2;
- attribs[1].type = VASurfaceAttribExternalBufferDescriptor;
- attribs[1].flags = VA_SURFACE_ATTRIB_SETTABLE;
- attribs[1].value.type = VAGenericValueTypePointer;
- attribs[1].value.value.p = &buf;
-
- // TODO: RT_FORMAT with 10 bit/hdr, VA_RT_FORMAT_RGB32_10
- // TODO: Max size same as source_size
- va_status = vaCreateSurfaces(va_dpy, VA_RT_FORMAT_RGB32, size.x, size.y, &input_surface_id, 1, attribs, 2);
- if(va_status != VA_STATUS_SUCCESS) {
- fprintf(stderr, "gsr error: vaapi_copy_drm_planes_to_video_surface: vaCreateSurfaces failed, error: %s\n", vaErrorStr(va_status));
- success = false;
- goto done;
- }
-
- const VARectangle source_region = {
- .x = source_pos.x,
- .y = source_pos.y,
- .width = source_size.x,
- .height = source_size.y
- };
-
- const VARectangle output_region = {
- .x = dest_pos.x,
- .y = dest_pos.y,
- .width = dest_size.x,
- .height = dest_size.y
- };
-
- const bool scaled = dest_size.x != source_size.x || dest_size.y != source_size.y;
-
- // Copying a surface to another surface will automatically perform the color conversion. Thanks vaapi!
- VAProcPipelineParameterBuffer params = {0};
- params.surface = input_surface_id;
- params.surface_region = NULL;
- params.surface_region = &source_region;
- params.output_region = &output_region;
- params.output_background_color = 0;
- params.filter_flags = scaled ? (VA_FILTER_SCALING_HQ | VA_FILTER_INTERPOLATION_BILINEAR) : 0;
- params.pipeline_flags = VA_PROC_PIPELINE_FAST;
-
- params.input_color_properties.colour_primaries = 1;
- params.input_color_properties.transfer_characteristics = 1;
- params.input_color_properties.matrix_coefficients = 1;
- params.surface_color_standard = VAProcColorStandardBT709; // TODO:
- params.input_color_properties.color_range = video_frame->color_range == AVCOL_RANGE_JPEG ? VA_SOURCE_RANGE_FULL : VA_SOURCE_RANGE_REDUCED;
-
- params.output_color_properties.colour_primaries = 1;
- params.output_color_properties.transfer_characteristics = 1;
- params.output_color_properties.matrix_coefficients = 1;
- params.output_color_standard = VAProcColorStandardBT709; // TODO:
- params.output_color_properties.color_range = video_frame->color_range == AVCOL_RANGE_JPEG ? VA_SOURCE_RANGE_FULL : VA_SOURCE_RANGE_REDUCED;
-
- params.processing_mode = VAProcPerformanceMode;
-
- // VAProcPipelineCaps pipeline_caps = {0};
- // va_status = vaQueryVideoProcPipelineCaps(self->va_dpy,
- // self->context_id,
- // NULL, 0,
- // &pipeline_caps);
- // if(va_status == VA_STATUS_SUCCESS) {
- // fprintf(stderr, "pipeline_caps: %u, %u\n", (unsigned int)pipeline_caps.rotation_flags, pipeline_caps.blend_flags);
- // }
-
- // TODO: params.output_hdr_metadata
-
- // TODO:
- // if (first surface to render)
- // pipeline_param->output_background_color = 0xff000000; // black
-
- va_status = vaCreateBuffer(va_dpy, context_id, VAProcPipelineParameterBufferType, sizeof(params), 1, &params, &buffer_id);
- if(va_status != VA_STATUS_SUCCESS) {
- fprintf(stderr, "gsr error: vaapi_copy_drm_planes_to_video_surface: vaCreateBuffer failed, error: %d\n", va_status);
- success = false;
- goto done;
- }
-
- va_status = vaBeginPicture(va_dpy, context_id, output_surface_id);
- if(va_status != VA_STATUS_SUCCESS) {
- fprintf(stderr, "gsr error: vaapi_copy_drm_planes_to_video_surface: vaBeginPicture failed, error: %d\n", va_status);
- success = false;
- goto done;
- }
-
- va_status = vaRenderPicture(va_dpy, context_id, &buffer_id, 1);
- if(va_status != VA_STATUS_SUCCESS) {
- vaEndPicture(va_dpy, context_id);
- fprintf(stderr, "gsr error: vaapi_copy_drm_planes_to_video_surface: vaRenderPicture failed, error: %d\n", va_status);
- success = false;
- goto done;
- }
-
- va_status = vaEndPicture(va_dpy, context_id);
- if(va_status != VA_STATUS_SUCCESS) {
- fprintf(stderr, "gsr error: vaapi_copy_drm_planes_to_video_surface: vaEndPicture failed, error: %d\n", va_status);
- success = false;
- goto done;
- }
-
- // vaSyncBuffer(va_dpy, buffer_id, 1000 * 1000 * 1000);
- // vaSyncSurface(va_dpy, input_surface_id);
- // vaSyncSurface(va_dpy, output_surface_id);
-
- done:
- if(buffer_id)
- vaDestroyBuffer(va_dpy, buffer_id);
-
- if(input_surface_id)
- vaDestroySurfaces(va_dpy, &input_surface_id, 1);
-
- if(context_id)
- vaDestroyContext(va_dpy, context_id);
-
- if(config_id)
- vaDestroyConfig(va_dpy, config_id);
-
- return success;
-}
-
-bool vaapi_copy_egl_image_to_video_surface(gsr_egl *egl, EGLImage image, vec2i source_pos, vec2i source_size, vec2i dest_pos, vec2i dest_size, AVCodecContext *video_codec_context, AVFrame *video_frame) {
- if(!image)
- return false;
-
- int texture_fourcc = 0;
- int texture_num_planes = 0;
- uint64_t texture_modifiers = 0;
- if(!egl->eglExportDMABUFImageQueryMESA(egl->egl_display, image, &texture_fourcc, &texture_num_planes, &texture_modifiers)) {
- fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_tick: eglExportDMABUFImageQueryMESA failed\n");
- return false;
- }
-
- if(texture_num_planes <= 0 || texture_num_planes > 8) {
- fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_tick: expected planes size to be 0<planes<=8 for drm buf, got %d planes\n", texture_num_planes);
- return false;
- }
-
- int texture_fds[8];
- int32_t texture_strides[8];
- int32_t texture_offsets[8];
-
- while(egl->eglGetError() != EGL_SUCCESS){}
- if(!egl->eglExportDMABUFImageMESA(egl->egl_display, image, texture_fds, texture_strides, texture_offsets)) {
- fprintf(stderr, "gsr error: gsr_capture_xcomposite_vaapi_tick: eglExportDMABUFImageMESA failed, error: %d\n", egl->eglGetError());
- return false;
- }
-
- int fds[8];
- uint32_t offsets[8];
- uint32_t pitches[8];
- uint64_t modifiers[8];
- for(int i = 0; i < texture_num_planes; ++i) {
- fds[i] = texture_fds[i];
- offsets[i] = texture_offsets[i];
- pitches[i] = texture_strides[i];
- modifiers[i] = texture_modifiers;
-
- if(fds[i] == -1)
- texture_num_planes = i;
- }
- const bool success = texture_num_planes > 0 && vaapi_copy_drm_planes_to_video_surface(video_codec_context, video_frame, source_pos, source_size, dest_pos, dest_size, texture_fourcc, source_size, fds, offsets, pitches, modifiers, texture_num_planes);
-
- for(int i = 0; i < texture_num_planes; ++i) {
- if(texture_fds[i] > 0) {
- close(texture_fds[i]);
- texture_fds[i] = -1;
- }
- }
-
- return success;
-}
-
vec2i scale_keep_aspect_ratio(vec2i from, vec2i to) {
if(from.x == 0 || from.y == 0)
return (vec2i){0, 0};
@@ -960,3 +669,27 @@ vec2i scale_keep_aspect_ratio(vec2i from, vec2i to) {
return from;
}
+
+unsigned int gl_create_texture(gsr_egl *egl, int width, int height, int internal_format, unsigned int format, int filter) {
+ float border_color[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
+ if(format == GL_RG) { // UV
+ border_color[0] = 0.5f;
+ border_color[1] = 0.5f;
+ border_color[2] = 0.0f;
+ border_color[3] = 1.0f;
+ }
+
+ unsigned int texture_id = 0;
+ egl->glGenTextures(1, &texture_id);
+ egl->glBindTexture(GL_TEXTURE_2D, texture_id);
+ egl->glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
+
+ egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
+ egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
+ egl->glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, border_color);
+ egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
+ egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
+
+ egl->glBindTexture(GL_TEXTURE_2D, 0);
+ return texture_id;
+}