From a7e0dbd83381377bd05a3fa988511d3713996370 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 16 Oct 2022 02:08:40 +0200 Subject: Refactor xcomposite into abstract capture api Refactor c++ files into c files, more usable --- include/CudaLibrary.hpp | 143 --------------------------------------- include/GlLibrary.hpp | 156 ------------------------------------------- include/LibraryLoader.hpp | 38 ----------- include/capture/capture.h | 19 ++++-- include/capture/nvfbc.h | 5 +- include/capture/xcomposite.h | 16 +++++ include/cuda.h | 101 ++++++++++++++++++++++++++++ include/gl.h | 102 ++++++++++++++++++++++++++++ include/library_loader.h | 41 ++++++++++++ include/time.h | 6 ++ include/window_texture.h | 28 ++++++++ 11 files changed, 310 insertions(+), 345 deletions(-) delete mode 100644 include/CudaLibrary.hpp delete mode 100644 include/GlLibrary.hpp delete mode 100644 include/LibraryLoader.hpp create mode 100644 include/capture/xcomposite.h create mode 100644 include/cuda.h create mode 100644 include/gl.h create mode 100644 include/library_loader.h create mode 100644 include/time.h create mode 100644 include/window_texture.h (limited to 'include') diff --git a/include/CudaLibrary.hpp b/include/CudaLibrary.hpp deleted file mode 100644 index fe99975..0000000 --- a/include/CudaLibrary.hpp +++ /dev/null @@ -1,143 +0,0 @@ -#pragma once - -#include "LibraryLoader.hpp" - -#include -#include - -// To prevent hwcontext_cuda.h from including cuda.h -#define CUDA_VERSION 11070 - -#if defined(_WIN64) || defined(__LP64__) -typedef unsigned long long CUdeviceptr_v2; -#else -typedef unsigned int CUdeviceptr_v2; -#endif -typedef CUdeviceptr_v2 CUdeviceptr; - -typedef int CUresult; -typedef int CUdevice_v1; -typedef CUdevice_v1 CUdevice; -typedef struct CUctx_st *CUcontext; -typedef struct CUstream_st *CUstream; -typedef struct CUarray_st *CUarray; - -static const int CUDA_SUCCESS = 0; - -typedef enum CUgraphicsMapResourceFlags_enum { - CU_GRAPHICS_MAP_RESOURCE_FLAGS_NONE = 0x00, - CU_GRAPHICS_MAP_RESOURCE_FLAGS_READ_ONLY = 0x01, - CU_GRAPHICS_MAP_RESOURCE_FLAGS_WRITE_DISCARD = 0x02 -} CUgraphicsMapResourceFlags; - -typedef enum CUgraphicsRegisterFlags_enum { - CU_GRAPHICS_REGISTER_FLAGS_NONE = 0x00, - CU_GRAPHICS_REGISTER_FLAGS_READ_ONLY = 0x01, - CU_GRAPHICS_REGISTER_FLAGS_WRITE_DISCARD = 0x02, - CU_GRAPHICS_REGISTER_FLAGS_SURFACE_LDST = 0x04, - CU_GRAPHICS_REGISTER_FLAGS_TEXTURE_GATHER = 0x08 -} CUgraphicsRegisterFlags; - -typedef enum CUmemorytype_enum { - CU_MEMORYTYPE_HOST = 0x01, /**< Host memory */ - CU_MEMORYTYPE_DEVICE = 0x02, /**< Device memory */ - CU_MEMORYTYPE_ARRAY = 0x03, /**< Array memory */ - CU_MEMORYTYPE_UNIFIED = 0x04 /**< Unified device or host memory */ -} CUmemorytype; - -typedef struct CUDA_MEMCPY2D_st { - size_t srcXInBytes; /**< Source X in bytes */ - size_t srcY; /**< Source Y */ - - CUmemorytype srcMemoryType; /**< Source memory type (host, device, array) */ - const void *srcHost; /**< Source host pointer */ - CUdeviceptr srcDevice; /**< Source device pointer */ - CUarray srcArray; /**< Source array reference */ - size_t srcPitch; /**< Source pitch (ignored when src is array) */ - - size_t dstXInBytes; /**< Destination X in bytes */ - size_t dstY; /**< Destination Y */ - - CUmemorytype dstMemoryType; /**< Destination memory type (host, device, array) */ - void *dstHost; /**< Destination host pointer */ - CUdeviceptr dstDevice; /**< Destination device pointer */ - CUarray dstArray; /**< Destination array reference */ - size_t dstPitch; /**< Destination pitch (ignored when dst is array) */ - - size_t WidthInBytes; /**< Width of 2D memory copy in bytes */ - size_t Height; /**< Height of 2D memory copy */ -} CUDA_MEMCPY2D_v2; -typedef CUDA_MEMCPY2D_v2 CUDA_MEMCPY2D; - -static const int CU_CTX_SCHED_AUTO = 0; - -typedef struct CUgraphicsResource_st *CUgraphicsResource; - -struct Cuda { - 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 (*cuCtxPushCurrent_v2)(CUcontext ctx); - CUresult (*cuCtxPopCurrent_v2)(CUcontext *pctx); - CUresult (*cuGetErrorString)(CUresult error, const char **pStr); - CUresult (*cuMemsetD8_v2)(CUdeviceptr dstDevice, unsigned char uc, size_t N); - CUresult (*cuMemcpy2D_v2)(const CUDA_MEMCPY2D *pCopy); - - 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 (*cuGraphicsUnregisterResource)(CUgraphicsResource resource); - CUresult (*cuGraphicsSubResourceGetMappedArray)(CUarray *pArray, CUgraphicsResource resource, unsigned int arrayIndex, unsigned int mipLevel); - - ~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; -}; diff --git a/include/GlLibrary.hpp b/include/GlLibrary.hpp deleted file mode 100644 index 1337ef3..0000000 --- a/include/GlLibrary.hpp +++ /dev/null @@ -1,156 +0,0 @@ -#pragma once - -#include "LibraryLoader.hpp" - -#include -#include -#include -#include - -typedef XID GLXPixmap; -typedef XID GLXDrawable; -typedef XID GLXWindow; - -typedef struct __GLXcontextRec *GLXContext; -typedef struct __GLXFBConfigRec *GLXFBConfig; - -#define GL_TEXTURE_2D 0x0DE1 -#define GL_RGB 0x1907 -#define GL_UNSIGNED_BYTE 0x1401 -#define GL_COLOR_BUFFER_BIT 0x00004000 -#define GL_TEXTURE_WRAP_S 0x2802 -#define GL_TEXTURE_WRAP_T 0x2803 -#define GL_TEXTURE_MAG_FILTER 0x2800 -#define GL_TEXTURE_MIN_FILTER 0x2801 -#define GL_TEXTURE_WIDTH 0x1000 -#define GL_TEXTURE_HEIGHT 0x1001 -#define GL_NEAREST 0x2600 - -#define GL_RENDERER 0x1F01 - -#define GLX_BUFFER_SIZE 2 -#define GLX_DOUBLEBUFFER 5 -#define GLX_RED_SIZE 8 -#define GLX_GREEN_SIZE 9 -#define GLX_BLUE_SIZE 10 -#define GLX_ALPHA_SIZE 11 -#define GLX_DEPTH_SIZE 12 - -#define GLX_RGBA_BIT 0x00000001 -#define GLX_RENDER_TYPE 0x8011 -#define GLX_FRONT_EXT 0x20DE -#define GLX_BIND_TO_TEXTURE_RGB_EXT 0x20D0 -#define GLX_DRAWABLE_TYPE 0x8010 -#define GLX_WINDOW_BIT 0x00000001 -#define GLX_PIXMAP_BIT 0x00000002 -#define GLX_BIND_TO_TEXTURE_TARGETS_EXT 0x20D3 -#define GLX_TEXTURE_2D_BIT_EXT 0x00000002 -#define GLX_TEXTURE_TARGET_EXT 0x20D6 -#define GLX_TEXTURE_2D_EXT 0x20DC -#define GLX_TEXTURE_FORMAT_EXT 0x20D5 -#define GLX_TEXTURE_FORMAT_RGB_EXT 0x20D9 -#define GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x00000002 -#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091 -#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092 -#define GLX_CONTEXT_FLAGS_ARB 0x2094 - -struct GlLibrary { - 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); - void (*glXReleaseTexImageEXT)(Display *dpy, GLXDrawable drawable, int buffer); - GLXFBConfig* (*glXChooseFBConfig)(Display *dpy, int screen, const int *attribList, int *nitems); - XVisualInfo* (*glXGetVisualFromFBConfig)(Display *dpy, GLXFBConfig config); - GLXContext (*glXCreateContextAttribsARB)(Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list); - Bool (*glXMakeContextCurrent)(Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx); - void (*glXDestroyContext)(Display *dpy, GLXContext ctx); - void (*glXSwapBuffers)(Display *dpy, GLXDrawable drawable); - - void (*glXSwapIntervalEXT)(Display *dpy, GLXDrawable drawable, int interval); - int (*glXSwapIntervalMESA)(unsigned int interval); - int (*glXSwapIntervalSGI)(int interval); - - void (*glClearTexImage)(unsigned int texture, unsigned int level, unsigned int format, unsigned int type, const void *data); - - unsigned int (*glGetError)(void); - const unsigned char* (*glGetString)(unsigned int name); - void (*glClear)(unsigned int mask); - void (*glGenTextures)(int n, unsigned int *textures); - void (*glDeleteTextures)(int n, const unsigned int *texture); - void (*glBindTexture)(unsigned int target, unsigned int texture); - void (*glTexParameteri)(unsigned int target, unsigned int pname, int param); - 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); - - ~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; - } - } - - void unload() { - if(library) { - dlclose(library); - library = nullptr; - } - } -private: - void *library = nullptr; -}; diff --git a/include/LibraryLoader.hpp b/include/LibraryLoader.hpp deleted file mode 100644 index 16dc580..0000000 --- a/include/LibraryLoader.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once - -#include -#include - -typedef struct { - void **func; - const char *name; -} dlsym_assign; - -static void* dlsym_print_fail(void *handle, const char *name, bool required) { - dlerror(); - void *sym = dlsym(handle, name); - char *err_str = dlerror(); - - if(!sym) - fprintf(stderr, "%s: dlsym(handle, \"%s\") failed, error: %s\n", required ? "error" : "warning", name, err_str ? err_str : "(null)"); - - return sym; -} - -/* |dlsyms| should be null terminated */ -static bool dlsym_load_list(void *handle, const dlsym_assign *dlsyms) { - bool success = true; - for(int i = 0; dlsyms[i].func; ++i) { - *dlsyms[i].func = dlsym_print_fail(handle, dlsyms[i].name, true); - if(!*dlsyms[i].func) - success = false; - } - return success; -} - -/* |dlsyms| should be null terminated */ -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 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 +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 + +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/cuda.h b/include/cuda.h new file mode 100644 index 0000000..cefdcad --- /dev/null +++ b/include/cuda.h @@ -0,0 +1,101 @@ +#ifndef GSR_CUDA_H +#define GSR_CUDA_H + +#include +#include + +// To prevent hwcontext_cuda.h from including cuda.h +#define CUDA_VERSION 11070 + +#if defined(_WIN64) || defined(__LP64__) +typedef unsigned long long CUdeviceptr_v2; +#else +typedef unsigned int CUdeviceptr_v2; +#endif +typedef CUdeviceptr_v2 CUdeviceptr; + +typedef int CUresult; +typedef int CUdevice_v1; +typedef CUdevice_v1 CUdevice; +typedef struct CUctx_st *CUcontext; +typedef struct CUstream_st *CUstream; +typedef struct CUarray_st *CUarray; + +#define CUDA_SUCCESS 0 + +typedef enum CUgraphicsMapResourceFlags_enum { + CU_GRAPHICS_MAP_RESOURCE_FLAGS_NONE = 0x00, + CU_GRAPHICS_MAP_RESOURCE_FLAGS_READ_ONLY = 0x01, + CU_GRAPHICS_MAP_RESOURCE_FLAGS_WRITE_DISCARD = 0x02 +} CUgraphicsMapResourceFlags; + +typedef enum CUgraphicsRegisterFlags_enum { + CU_GRAPHICS_REGISTER_FLAGS_NONE = 0x00, + CU_GRAPHICS_REGISTER_FLAGS_READ_ONLY = 0x01, + CU_GRAPHICS_REGISTER_FLAGS_WRITE_DISCARD = 0x02, + CU_GRAPHICS_REGISTER_FLAGS_SURFACE_LDST = 0x04, + CU_GRAPHICS_REGISTER_FLAGS_TEXTURE_GATHER = 0x08 +} CUgraphicsRegisterFlags; + +typedef enum CUmemorytype_enum { + CU_MEMORYTYPE_HOST = 0x01, /**< Host memory */ + CU_MEMORYTYPE_DEVICE = 0x02, /**< Device memory */ + CU_MEMORYTYPE_ARRAY = 0x03, /**< Array memory */ + CU_MEMORYTYPE_UNIFIED = 0x04 /**< Unified device or host memory */ +} CUmemorytype; + +typedef struct CUDA_MEMCPY2D_st { + size_t srcXInBytes; /**< Source X in bytes */ + size_t srcY; /**< Source Y */ + + CUmemorytype srcMemoryType; /**< Source memory type (host, device, array) */ + const void *srcHost; /**< Source host pointer */ + CUdeviceptr srcDevice; /**< Source device pointer */ + CUarray srcArray; /**< Source array reference */ + size_t srcPitch; /**< Source pitch (ignored when src is array) */ + + size_t dstXInBytes; /**< Destination X in bytes */ + size_t dstY; /**< Destination Y */ + + CUmemorytype dstMemoryType; /**< Destination memory type (host, device, array) */ + void *dstHost; /**< Destination host pointer */ + CUdeviceptr dstDevice; /**< Destination device pointer */ + CUarray dstArray; /**< Destination array reference */ + size_t dstPitch; /**< Destination pitch (ignored when dst is array) */ + + size_t WidthInBytes; /**< Width of 2D memory copy in bytes */ + size_t Height; /**< Height of 2D memory copy */ +} CUDA_MEMCPY2D_v2; +typedef CUDA_MEMCPY2D_v2 CUDA_MEMCPY2D; + +#define CU_CTX_SCHED_AUTO 0 + +typedef struct CUgraphicsResource_st *CUgraphicsResource; + +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); + CUresult (*cuMemsetD8_v2)(CUdeviceptr dstDevice, unsigned char uc, size_t N); + CUresult (*cuMemcpy2D_v2)(const CUDA_MEMCPY2D *pCopy); + + 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); + +#endif /* GSR_CUDA_H */ diff --git a/include/gl.h b/include/gl.h new file mode 100644 index 0000000..e27da6a --- /dev/null +++ b/include/gl.h @@ -0,0 +1,102 @@ +#ifndef GSR_GL_H +#define GSR_GL_H + +/* OpenGL library with a hidden window context (to allow using the opengl functions) */ + +#include +#include +#include + +typedef XID GLXPixmap; +typedef XID GLXDrawable; +typedef XID GLXWindow; + +typedef struct __GLXcontextRec *GLXContext; +typedef struct __GLXFBConfigRec *GLXFBConfig; + +#define GL_TEXTURE_2D 0x0DE1 +#define GL_RGB 0x1907 +#define GL_UNSIGNED_BYTE 0x1401 +#define GL_COLOR_BUFFER_BIT 0x00004000 +#define GL_TEXTURE_WRAP_S 0x2802 +#define GL_TEXTURE_WRAP_T 0x2803 +#define GL_TEXTURE_MAG_FILTER 0x2800 +#define GL_TEXTURE_MIN_FILTER 0x2801 +#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 + +#define GLX_BUFFER_SIZE 2 +#define GLX_DOUBLEBUFFER 5 +#define GLX_RED_SIZE 8 +#define GLX_GREEN_SIZE 9 +#define GLX_BLUE_SIZE 10 +#define GLX_ALPHA_SIZE 11 +#define GLX_DEPTH_SIZE 12 + +#define GLX_RGBA_BIT 0x00000001 +#define GLX_RENDER_TYPE 0x8011 +#define GLX_FRONT_EXT 0x20DE +#define GLX_BIND_TO_TEXTURE_RGB_EXT 0x20D0 +#define GLX_DRAWABLE_TYPE 0x8010 +#define GLX_WINDOW_BIT 0x00000001 +#define GLX_PIXMAP_BIT 0x00000002 +#define GLX_BIND_TO_TEXTURE_TARGETS_EXT 0x20D3 +#define GLX_TEXTURE_2D_BIT_EXT 0x00000002 +#define GLX_TEXTURE_TARGET_EXT 0x20D6 +#define GLX_TEXTURE_2D_EXT 0x20DC +#define GLX_TEXTURE_FORMAT_EXT 0x20D5 +#define GLX_TEXTURE_FORMAT_RGB_EXT 0x20D9 +#define GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB 0x00000002 +#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091 +#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092 +#define GLX_CONTEXT_FLAGS_ARB 0x2094 + +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); + void (*glXReleaseTexImageEXT)(Display *dpy, GLXDrawable drawable, int buffer); + GLXFBConfig* (*glXChooseFBConfig)(Display *dpy, int screen, const int *attribList, int *nitems); + XVisualInfo* (*glXGetVisualFromFBConfig)(Display *dpy, GLXFBConfig config); + GLXContext (*glXCreateContextAttribsARB)(Display *dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list); + Bool (*glXMakeContextCurrent)(Display *dpy, GLXDrawable draw, GLXDrawable read, GLXContext ctx); + void (*glXDestroyContext)(Display *dpy, GLXContext ctx); + void (*glXSwapBuffers)(Display *dpy, GLXDrawable drawable); + + void (*glXSwapIntervalEXT)(Display *dpy, GLXDrawable drawable, int interval); + int (*glXSwapIntervalMESA)(unsigned int interval); + int (*glXSwapIntervalSGI)(int interval); + + void (*glClearTexImage)(unsigned int texture, unsigned int level, unsigned int format, unsigned int type, const void *data); + + unsigned int (*glGetError)(void); + const unsigned char* (*glGetString)(unsigned int name); + void (*glClear)(unsigned int mask); + void (*glGenTextures)(int n, unsigned int *textures); + void (*glDeleteTextures)(int n, const unsigned int *texture); + void (*glBindTexture)(unsigned int target, unsigned int texture); + void (*glTexParameteri)(unsigned int target, unsigned int pname, int param); + 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; + +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); + +#endif /* GSR_GL_H */ diff --git a/include/library_loader.h b/include/library_loader.h new file mode 100644 index 0000000..d359c5b --- /dev/null +++ b/include/library_loader.h @@ -0,0 +1,41 @@ +#ifndef GSR_LIBRARY_LOADER_H +#define GSR_LIBRARY_LOADER_H + +#include +#include + +typedef struct { + void **func; + const char *name; +} dlsym_assign; + +static void* dlsym_print_fail(void *handle, const char *name, bool required) { + dlerror(); + void *sym = dlsym(handle, name); + char *err_str = dlerror(); + + if(!sym) + fprintf(stderr, "%s: dlsym(handle, \"%s\") failed, error: %s\n", required ? "error" : "warning", name, err_str ? err_str : "(null)"); + + return sym; +} + +/* |dlsyms| should be null terminated */ +static bool dlsym_load_list(void *handle, const dlsym_assign *dlsyms) { + bool success = true; + for(int i = 0; dlsyms[i].func; ++i) { + *dlsyms[i].func = dlsym_print_fail(handle, dlsyms[i].name, true); + if(!*dlsyms[i].func) + success = false; + } + return success; +} + +/* |dlsyms| should be null terminated */ +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); + } +} + +#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 */ -- cgit v1.2.3