aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2022-10-16 02:08:40 +0200
committerdec05eba <dec05eba@protonmail.com>2022-10-16 04:15:09 +0200
commita7e0dbd83381377bd05a3fa988511d3713996370 (patch)
tree0e18f4c7b95aa4cf79d6646bca77ed5f03c65fb3 /include
parent93d46b976730bced7a70be1617a0171600167314 (diff)
Refactor xcomposite into abstract capture api
Refactor c++ files into c files, more usable
Diffstat (limited to 'include')
-rw-r--r--include/capture/capture.h19
-rw-r--r--include/capture/nvfbc.h5
-rw-r--r--include/capture/xcomposite.h16
-rw-r--r--include/cuda.h (renamed from include/CudaLibrary.hpp)76
-rw-r--r--include/gl.h (renamed from include/GlLibrary.hpp)96
-rw-r--r--include/library_loader.h (renamed from include/LibraryLoader.hpp)7
-rw-r--r--include/time.h6
-rw-r--r--include/window_texture.h28
8 files changed, 109 insertions, 144 deletions
diff --git a/include/capture/capture.h b/include/capture/capture.h
index 9e23a23..edcc14d 100644
--- a/include/capture/capture.h
+++ b/include/capture/capture.h
@@ -3,23 +3,28 @@
#include <stdbool.h>
+typedef struct AVCodecContext AVCodecContext;
typedef struct AVFrame AVFrame;
typedef struct gsr_capture gsr_capture;
struct gsr_capture {
- int (*start)(gsr_capture *cap);
- void (*stop)(gsr_capture *cap);
+ /* These methods should not be called manually. Call gsr_capture_* instead */
+ int (*start)(gsr_capture *cap, AVCodecContext *video_codec_context);
+ void (*tick)(gsr_capture *cap, AVCodecContext *video_codec_context, AVFrame **frame); /* can be NULL */
+ bool (*should_stop)(gsr_capture *cap, bool *err); /* can be NULL */
int (*capture)(gsr_capture *cap, AVFrame *frame);
- void (*destroy)(gsr_capture *cap);
+ void (*destroy)(gsr_capture *cap, AVCodecContext *video_codec_context);
- void *priv;
+ void *priv; /* can be NULL */
+ bool started;
};
-int gsr_capture_start(gsr_capture *cap);
-void gsr_capture_stop(gsr_capture *cap);
+int gsr_capture_start(gsr_capture *cap, AVCodecContext *video_codec_context);
+void gsr_capture_tick(gsr_capture *cap, AVCodecContext *video_codec_context, AVFrame **frame);
+bool gsr_capture_should_stop(gsr_capture *cap, bool *err);
int gsr_capture_capture(gsr_capture *cap, AVFrame *frame);
/* Calls |gsr_capture_stop| as well */
-void gsr_capture_destroy(gsr_capture *cap);
+void gsr_capture_destroy(gsr_capture *cap, AVCodecContext *video_codec_context);
#endif /* GSR_CAPTURE_CAPTURE_H */
diff --git a/include/capture/nvfbc.h b/include/capture/nvfbc.h
index b749b87..06587d8 100644
--- a/include/capture/nvfbc.h
+++ b/include/capture/nvfbc.h
@@ -4,8 +4,11 @@
#include "capture.h"
#include "../vec2.h"
+typedef struct _XDisplay Display;
+
typedef struct {
- const char *display_to_capture; /* if this is "screen", then the entire x11 screen is captured (all displays) */
+ Display *dpy;
+ const char *display_to_capture; /* if this is "screen", then the entire x11 screen is captured (all displays). A copy is made of this */
int fps;
vec2i pos;
vec2i size;
diff --git a/include/capture/xcomposite.h b/include/capture/xcomposite.h
new file mode 100644
index 0000000..edd335c
--- /dev/null
+++ b/include/capture/xcomposite.h
@@ -0,0 +1,16 @@
+#ifndef GSR_CAPTURE_XCOMPOSITE_H
+#define GSR_CAPTURE_XCOMPOSITE_H
+
+#include "capture.h"
+#include "../vec2.h"
+#include <X11/X.h>
+
+typedef struct _XDisplay Display;
+
+typedef struct {
+ Window window;
+} gsr_capture_xcomposite_params;
+
+gsr_capture* gsr_capture_xcomposite_create(const gsr_capture_xcomposite_params *params);
+
+#endif /* GSR_CAPTURE_XCOMPOSITE_H */
diff --git a/include/CudaLibrary.hpp b/include/cuda.h
index fe99975..cefdcad 100644
--- a/include/CudaLibrary.hpp
+++ b/include/cuda.h
@@ -1,9 +1,8 @@
-#pragma once
+#ifndef GSR_CUDA_H
+#define GSR_CUDA_H
-#include "LibraryLoader.hpp"
-
-#include <dlfcn.h>
-#include <stdio.h>
+#include <stddef.h>
+#include <stdbool.h>
// To prevent hwcontext_cuda.h from including cuda.h
#define CUDA_VERSION 11070
@@ -22,7 +21,7 @@ typedef struct CUctx_st *CUcontext;
typedef struct CUstream_st *CUstream;
typedef struct CUarray_st *CUarray;
-static const int CUDA_SUCCESS = 0;
+#define CUDA_SUCCESS 0
typedef enum CUgraphicsMapResourceFlags_enum {
CU_GRAPHICS_MAP_RESOURCE_FLAGS_NONE = 0x00,
@@ -69,15 +68,19 @@ typedef struct CUDA_MEMCPY2D_st {
} CUDA_MEMCPY2D_v2;
typedef CUDA_MEMCPY2D_v2 CUDA_MEMCPY2D;
-static const int CU_CTX_SCHED_AUTO = 0;
+#define CU_CTX_SCHED_AUTO 0
typedef struct CUgraphicsResource_st *CUgraphicsResource;
-struct Cuda {
+typedef struct {
+ void *library;
+ CUcontext cu_ctx;
+
CUresult (*cuInit)(unsigned int Flags);
CUresult (*cuDeviceGetCount)(int *count);
CUresult (*cuDeviceGet)(CUdevice *device, int ordinal);
CUresult (*cuCtxCreate_v2)(CUcontext *pctx, unsigned int flags, CUdevice dev);
+ CUresult (*cuCtxDestroy_v2)(CUcontext ctx);
CUresult (*cuCtxPushCurrent_v2)(CUcontext ctx);
CUresult (*cuCtxPopCurrent_v2)(CUcontext *pctx);
CUresult (*cuGetErrorString)(CUresult error, const char **pStr);
@@ -87,57 +90,12 @@ struct Cuda {
CUresult (*cuGraphicsGLRegisterImage)(CUgraphicsResource *pCudaResource, unsigned int image, unsigned int target, unsigned int Flags);
CUresult (*cuGraphicsResourceSetMapFlags)(CUgraphicsResource resource, unsigned int flags);
CUresult (*cuGraphicsMapResources)(unsigned int count, CUgraphicsResource *resources, CUstream hStream);
+ CUresult (*cuGraphicsUnmapResources)(unsigned int count, CUgraphicsResource *resources, CUstream hStream);
CUresult (*cuGraphicsUnregisterResource)(CUgraphicsResource resource);
CUresult (*cuGraphicsSubResourceGetMappedArray)(CUarray *pArray, CUgraphicsResource resource, unsigned int arrayIndex, unsigned int mipLevel);
+} gsr_cuda;
+
+bool gsr_cuda_load(gsr_cuda *self);
+void gsr_cuda_unload(gsr_cuda *self);
- ~Cuda() {
- if(library)
- dlclose(library);
- }
-
- bool load() {
- if(library)
- return true;
-
- dlerror(); // clear
- void *lib = dlopen("libcuda.so.1", RTLD_LAZY);
- if(!lib) {
- lib = dlopen("libcuda.so", RTLD_LAZY);
- if(!lib) {
- fprintf(stderr, "Error: failed to load libcuda.so/libcuda.so.1, error: %s\n", dlerror());
- return false;
- }
- }
-
- dlsym_assign required_dlsym[] = {
- { (void**)&cuInit, "cuInit" },
- { (void**)&cuDeviceGetCount, "cuDeviceGetCount" },
- { (void**)&cuDeviceGet, "cuDeviceGet" },
- { (void**)&cuCtxCreate_v2, "cuCtxCreate_v2" },
- { (void**)&cuCtxPushCurrent_v2, "cuCtxPushCurrent_v2" },
- { (void**)&cuCtxPopCurrent_v2, "cuCtxPopCurrent_v2" },
- { (void**)&cuGetErrorString, "cuGetErrorString" },
- { (void**)&cuMemsetD8_v2, "cuMemsetD8_v2" },
- { (void**)&cuMemcpy2D_v2, "cuMemcpy2D_v2" },
-
- { (void**)&cuGraphicsGLRegisterImage, "cuGraphicsGLRegisterImage" },
- { (void**)&cuGraphicsResourceSetMapFlags, "cuGraphicsResourceSetMapFlags" },
- { (void**)&cuGraphicsMapResources, "cuGraphicsMapResources" },
- { (void**)&cuGraphicsUnregisterResource, "cuGraphicsUnregisterResource" },
- { (void**)&cuGraphicsSubResourceGetMappedArray, "cuGraphicsSubResourceGetMappedArray" },
-
- { NULL, NULL }
- };
-
- if(dlsym_load_list(lib, required_dlsym)) {
- library = lib;
- return true;
- } else {
- fprintf(stderr, "Error: missing required symbols in libcuda.so\n");
- dlclose(lib);
- return false;
- }
- }
-private:
- void *library = nullptr;
-};
+#endif /* GSR_CUDA_H */
diff --git a/include/GlLibrary.hpp b/include/gl.h
index 1337ef3..e27da6a 100644
--- a/include/GlLibrary.hpp
+++ b/include/gl.h
@@ -1,11 +1,11 @@
-#pragma once
+#ifndef GSR_GL_H
+#define GSR_GL_H
-#include "LibraryLoader.hpp"
+/* OpenGL library with a hidden window context (to allow using the opengl functions) */
#include <X11/X.h>
#include <X11/Xutil.h>
-#include <dlfcn.h>
-#include <stdio.h>
+#include <stdbool.h>
typedef XID GLXPixmap;
typedef XID GLXDrawable;
@@ -25,6 +25,8 @@ typedef struct __GLXFBConfigRec *GLXFBConfig;
#define GL_TEXTURE_WIDTH 0x1000
#define GL_TEXTURE_HEIGHT 0x1001
#define GL_NEAREST 0x2600
+#define GL_CLAMP_TO_EDGE 0x812F
+#define GL_LINEAR 0x2601
#define GL_RENDERER 0x1F01
@@ -54,7 +56,16 @@ typedef struct __GLXFBConfigRec *GLXFBConfig;
#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
#define GLX_CONTEXT_FLAGS_ARB 0x2094
-struct GlLibrary {
+typedef struct {
+ void *library;
+ Display *dpy;
+ GLXFBConfig *fbconfigs;
+ XVisualInfo *visual_info;
+ GLXFBConfig fbconfig;
+ Colormap colormap;
+ GLXContext gl_context;
+ Window window;
+
GLXPixmap (*glXCreatePixmap)(Display *dpy, GLXFBConfig config, Pixmap pixmap, const int *attribList);
void (*glXDestroyPixmap)(Display *dpy, GLXPixmap pixmap);
void (*glXBindTexImageEXT)(Display *dpy, GLXDrawable drawable, int buffer, const int *attrib_list);
@@ -82,75 +93,10 @@ struct GlLibrary {
void (*glGetTexLevelParameteriv)(unsigned int target, int level, unsigned int pname, int *params);
void (*glTexImage2D)(unsigned int target, int level, int internalFormat, int width, int height, int border, unsigned int format, unsigned int type, const void *pixels);
void (*glCopyImageSubData)(unsigned int srcName, unsigned int srcTarget, int srcLevel, int srcX, int srcY, int srcZ, unsigned int dstName, unsigned int dstTarget, int dstLevel, int dstX, int dstY, int dstZ, int srcWidth, int srcHeight, int srcDepth);
+} gsr_gl;
- ~GlLibrary() {
- unload();
- }
-
- bool load() {
- if(library)
- return true;
-
- dlerror(); // clear
- void *lib = dlopen("libGL.so.1", RTLD_LAZY);
- if(!lib) {
- fprintf(stderr, "Error: failed to load libGL.so.1, error: %s\n", dlerror());
- return false;
- }
-
- dlsym_assign optional_dlsym[] = {
- { (void**)&glClearTexImage, "glClearTexImage" },
- { (void**)&glXSwapIntervalEXT, "glXSwapIntervalEXT" },
- { (void**)&glXSwapIntervalMESA, "glXSwapIntervalMESA" },
- { (void**)&glXSwapIntervalSGI, "glXSwapIntervalSGI" },
-
- { NULL, NULL }
- };
-
- dlsym_load_list_optional(lib, optional_dlsym);
-
- dlsym_assign required_dlsym[] = {
- { (void**)&glXCreatePixmap, "glXCreatePixmap" },
- { (void**)&glXDestroyPixmap, "glXDestroyPixmap" },
- { (void**)&glXBindTexImageEXT, "glXBindTexImageEXT" },
- { (void**)&glXReleaseTexImageEXT, "glXReleaseTexImageEXT" },
- { (void**)&glXChooseFBConfig, "glXChooseFBConfig" },
- { (void**)&glXGetVisualFromFBConfig, "glXGetVisualFromFBConfig" },
- { (void**)&glXCreateContextAttribsARB, "glXCreateContextAttribsARB" },
- { (void**)&glXMakeContextCurrent, "glXMakeContextCurrent" },
- { (void**)&glXDestroyContext, "glXDestroyContext" },
- { (void**)&glXSwapBuffers, "glXSwapBuffers" },
-
- { (void**)&glGetError, "glGetError" },
- { (void**)&glGetString, "glGetString" },
- { (void**)&glClear, "glClear" },
- { (void**)&glGenTextures, "glGenTextures" },
- { (void**)&glDeleteTextures, "glDeleteTextures" },
- { (void**)&glBindTexture, "glBindTexture" },
- { (void**)&glTexParameteri, "glTexParameteri" },
- { (void**)&glGetTexLevelParameteriv, "glGetTexLevelParameteriv" },
- { (void**)&glTexImage2D, "glTexImage2D" },
- { (void**)&glCopyImageSubData, "glCopyImageSubData" },
-
- { NULL, NULL }
- };
-
- if(dlsym_load_list(lib, required_dlsym)) {
- library = lib;
- return true;
- } else {
- fprintf(stderr, "Error: missing required symbols in libGL.so.1\n");
- dlclose(lib);
- return false;
- }
- }
+bool gsr_gl_load(gsr_gl *self, Display *dpy);
+bool gsr_gl_make_context_current(gsr_gl *self);
+void gsr_gl_unload(gsr_gl *self);
- void unload() {
- if(library) {
- dlclose(library);
- library = nullptr;
- }
- }
-private:
- void *library = nullptr;
-};
+#endif /* GSR_GL_H */
diff --git a/include/LibraryLoader.hpp b/include/library_loader.h
index 16dc580..d359c5b 100644
--- a/include/LibraryLoader.hpp
+++ b/include/library_loader.h
@@ -1,4 +1,5 @@
-#pragma once
+#ifndef GSR_LIBRARY_LOADER_H
+#define GSR_LIBRARY_LOADER_H
#include <dlfcn.h>
#include <stdio.h>
@@ -35,4 +36,6 @@ static void dlsym_load_list_optional(void *handle, const dlsym_assign *dlsyms) {
for(int i = 0; dlsyms[i].func; ++i) {
*dlsyms[i].func = dlsym_print_fail(handle, dlsyms[i].name, false);
}
-} \ No newline at end of file
+}
+
+#endif /* GSR_LIBRARY_LOADER_H */ \ No newline at end of file
diff --git a/include/time.h b/include/time.h
new file mode 100644
index 0000000..150c655
--- /dev/null
+++ b/include/time.h
@@ -0,0 +1,6 @@
+#ifndef GSR_TIME_H
+#define GSR_TIME_H
+
+double clock_get_monotonic_seconds();
+
+#endif /* GSR_TIME_H */
diff --git a/include/window_texture.h b/include/window_texture.h
new file mode 100644
index 0000000..db64191
--- /dev/null
+++ b/include/window_texture.h
@@ -0,0 +1,28 @@
+#ifndef WINDOW_TEXTURE_H
+#define WINDOW_TEXTURE_H
+
+#include "gl.h"
+
+typedef struct {
+ Display *display;
+ Window window;
+ Pixmap pixmap;
+ GLXPixmap glx_pixmap;
+ unsigned int texture_id;
+ int redirected;
+ gsr_gl *gl;
+} WindowTexture;
+
+/* Returns 0 on success */
+int window_texture_init(WindowTexture *window_texture, Display *display, Window window, gsr_gl *gl);
+void window_texture_deinit(WindowTexture *self);
+
+/*
+ This should ONLY be called when the target window is resized.
+ Returns 0 on success.
+*/
+int window_texture_on_resize(WindowTexture *self);
+
+unsigned int window_texture_get_opengl_texture_id(WindowTexture *self);
+
+#endif /* WINDOW_TEXTURE_H */