aboutsummaryrefslogtreecommitdiff
path: root/src/window_texture.c
blob: d899266fcda5a5e677f0dc45affa4922fc9d6c6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "../include/window_texture.h"
#include <X11/extensions/Xcomposite.h>

static int x11_supports_composite_named_window_pixmap(Display *display) {
    int extension_major;
    int extension_minor;
    if(!XCompositeQueryExtension(display, &extension_major, &extension_minor))
        return 0;

    int major_version;
    int minor_version;
    return XCompositeQueryVersion(display, &major_version, &minor_version) && (major_version > 0 || minor_version >= 2);
}

int window_texture_init(WindowTexture *window_texture, Display *display, Window window) {
    if(!x11_supports_composite_named_window_pixmap(display))
        return 1;

    window_texture->display = display;
    window_texture->window = window;
    window_texture->pixmap = None;
    window_texture->glx_pixmap = None;
    window_texture->texture_id = 0;

    XCompositeRedirectWindow(display, window, CompositeRedirectAutomatic);
    return window_texture_on_resize(window_texture);
}

static void window_texture_cleanup(WindowTexture *self) {
    if(self->texture_id) {
        glDeleteTextures(1, &self->texture_id);
        self->texture_id = 0;
    }

    if(self->glx_pixmap) {
        glXDestroyPixmap(self->display, self->glx_pixmap);
        glXReleaseTexImageEXT(self->display, self->glx_pixmap, GLX_FRONT_EXT);
        self->glx_pixmap = None;
    }

    if(self->pixmap) {
        XFreePixmap(self->display, self->pixmap);
        self->pixmap = None;
    }
}

void window_texture_deinit(WindowTexture *self) {
    window_texture_cleanup(self);
}

int window_texture_on_resize(WindowTexture *self) {
    window_texture_cleanup(self);

    int result = 0;

    const int pixmap_config[] = {
        GLX_BIND_TO_TEXTURE_RGBA_EXT, True,
        GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
        GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_BIT_EXT,
        /*GLX_BIND_TO_MIPMAP_TEXTURE_EXT, True,*/
        GLX_DOUBLEBUFFER, False,
        GLX_BUFFER_SIZE, 32,
        GLX_ALPHA_SIZE, 8,
        None
    };

    const int pixmap_attribs[] = {
        GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
        GLX_TEXTURE_FORMAT_EXT, GLX_TEXTURE_FORMAT_RGB_EXT,
        /*GLX_MIPMAP_TEXTURE_EXT, True,*/
        None
    };

    int c;
    GLXFBConfig *configs = glXChooseFBConfig(self->display, 0, pixmap_config, &c);
    if(!configs)
        return 1;

    self->pixmap = XCompositeNameWindowPixmap(self->display, self->window);
    if(!self->pixmap) {
        result = 2;
        goto cleanup;
    }

    self->glx_pixmap = glXCreatePixmap(self->display, configs[0], self->pixmap, pixmap_attribs);
    if(!self->glx_pixmap) {
        result = 3;
        goto cleanup;
    }

    glGenTextures(1, &self->texture_id);
    glBindTexture(GL_TEXTURE_2D, self->texture_id);
    if(self->texture_id == 0) {
        result = 4;
        goto cleanup;
    }

    glXBindTexImageEXT(self->display, self->glx_pixmap, GLX_FRONT_EXT, NULL);
    /*glGenerateMipmap(GL_TEXTURE_2D)*/

    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);//GL_LINEAR_MIPMAP_LINEAR );
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
 
    float fLargest = 0.0f;
    glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &fLargest);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, fLargest);

    glBindTexture(GL_TEXTURE_2D, 0);

    cleanup:
    XFree(configs);
    return result;
}

GLuint window_texture_get_opengl_texture_id(WindowTexture *self) {
    return self->texture_id;
}