aboutsummaryrefslogtreecommitdiff
path: root/src/egl.zig
blob: ad5be7c892d619b344964622a445bcfa7d8efd42 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const c = @cImport({
    @cInclude("EGL/egl.h");
    @cInclude("GL/gl.h");
});

pub const Error = error{
    Unknown,
    NoDisplay,
    NotInitialized,
    BadAccess,
    BadAlloc,
    BadAttribute,
    BadContext,
    BadConfig,
    BadCurrentSurface,
    BadDisplay,
    BadMatch,
    BadNativePixmap,
    BadNativeWindow,
    BadParameter,
    BadSurface,
    ContextLost,
};

extern fn eglGetError() i32;
fn getError() Error!void {
    return switch (eglGetError()) {
        c.EGL_NOT_INITIALIZED => Error.NotInitialized,
        c.EGL_BAD_ACCESS => Error.BadDisplay,
        c.EGL_BAD_ATTRIBUTE => Error.BadAttribute,
        c.EGL_BAD_CONFIG => Error.BadConfig,
        c.EGL_BAD_CONTEXT => Error.BadContext,
        c.EGL_BAD_CURRENT_SURFACE => Error.BadCurrentSurface,
        c.EGL_BAD_DISPLAY => Error.BadDisplay,
        c.EGL_BAD_MATCH => Error.BadMatch,
        c.EGL_BAD_NATIVE_PIXMAP => Error.BadNativePixmap,
        c.EGL_BAD_NATIVE_WINDOW => Error.BadNativeWindow,
        c.EGL_BAD_PARAMETER => Error.BadParameter,
        c.EGL_BAD_SURFACE => Error.BadSurface,
        c.EGL_CONTEXT_LOST => Error.ContextLost,
        else => Error.Unknown,
    };
}

pub const Api = enum(u32) {
    opengl = c.EGL_OPENGL_API,
    opengl_es = c.EGL_OPENGL_ES_API,
};

extern fn eglBindAPI(api: Api) bool;
pub fn bindApi(api: Api) Error!void {
    if (!eglBindAPI(api)) try getError();
}

pub const AttributeType = enum(i32) {
    none = c.EGL_NONE,
    alpha_size = c.EGL_ALPHA_SIZE,
    red_size = c.EGL_RED_SIZE,
    green_size = c.EGL_GREEN_SIZE,
    blue_size = c.EGL_BLUE_SIZE,
};

pub const Attribute = packed struct {
    type: AttributeType,
    value: i32,
};

pub const Config = opaque {};

pub const Surface = opaque {};

pub const Context = opaque {};

pub const Display = opaque {
    extern fn eglGetDisplay(native_display: *anyopaque) ?*Display;
    pub fn get(native_display: *anyopaque) Error!*Display {
        return eglGetDisplay(
            native_display,
        ) orelse Error.NoDisplay;
    }

    extern fn eglInitialize(display: *Display, major: ?*i32, minor: ?*i32) bool;
    pub fn initialize(display: *Display, major: ?*i32, minor: ?*i32) Error!void {
        if (!eglInitialize(display, major, minor)) try getError();
    }

    extern fn eglChooseConfig(
        display: *Display,
        attributes: [*c]Attribute,
        configs: [*c]*Config,
        configs_len: i32,
        config_cnt: *i32,
    ) bool;
    /// Returns the number of configs that were put in @configs.
    pub fn chooseConfig(
        display: *Display,
        attributes: []Attribute,
        configs: []*Config,
    ) Error!i32 {
        var config_cnt: i32 = 0;
        if (!eglChooseConfig(
            display,
            attributes.ptr,
            configs.ptr,
            @intCast(configs.len),
            &config_cnt,
        )) {
            try getError();
        }
        return config_cnt;
    }

    extern fn eglCreateContext(
        display: *Display,
        config: *Config,
        share_ctx: ?*Context,
        attributes: [*c]Attribute,
    ) ?*Context;
    pub fn createContext(
        display: *Display,
        config: *Config,
        share_ctx: ?*Context,
        attributes: ?[]Attribute,
    ) Error!*Context {
        var attrs: [*c]Attribute = null;
        if (attributes) |_attrs| attrs = _attrs.ptr;
        return eglCreateContext(display, config, share_ctx, attrs) orelse {
            try getError();
            unreachable;
        };
    }

    extern fn eglCreateWindowSurface(
        display: *Display,
        config: *Config,
        native_window: *anyopaque,
        attributes: [*c]Attribute,
    ) ?*Surface;
    pub fn createWindowSurface(
        display: *Display,
        config: *Config,
        native_window: *anyopaque,
        attributes: ?[]Attribute,
    ) Error!*Surface {
        var attrs: [*c]Attribute = null;
        if (attributes) |_attrs| attrs = _attrs.ptr;
        return eglCreateWindowSurface(display, config, native_window, attrs) orelse {
            try getError();
            unreachable;
        };
    }

    extern fn eglMakeCurrent(
        display: *Display,
        draw: *Surface,
        read: *Surface,
        ctx: *Context,
    ) bool;
    pub fn makeCurrent(
        display: *Display,
        draw: *Surface,
        read: *Surface,
        ctx: *Context,
    ) Error!void {
        if (!eglMakeCurrent(display, draw, read, ctx)) try getError();
    }

    extern fn eglQueryString(display: *Display, name: i32) [*:0]const u8;
    pub const queryString = eglQueryString;

    extern fn eglSwapBuffers(display: *Display, surface: *Surface) bool;
    pub fn swapBuffers(display: *Display, surface: *Surface) Error!void {
        if (!eglSwapBuffers(display, surface)) try getError();
    }

    extern fn eglSwapInterval(display: *Display, interval: i32) bool;
    pub fn swapInterval(display: *Display, interval: i32) Error!void {
        if (!eglSwapInterval(display, interval)) try getError();
    }

    extern fn eglTerminate(display: *Display) bool;
    pub fn terminate(display: *Display) void {
        // Do I really care if destruction fails?
        _ = eglTerminate(display);
    }
};