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
|
#include "graphics.h"
#include <EGL/egl.h>
#include <EGL/eglplatform.h>
#include <cstring>
#include "utils.h"
#include "wlstate.h"
#define WAYC_OPENGL_MAJOR 4
#define WAYC_OPENGL_MINOR 6
#define WAYC_COLOR_BITS 8
/* clang-format off */
static EGLint WAYC_EGL_ATTRS[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_RED_SIZE, WAYC_COLOR_BITS,
EGL_GREEN_SIZE, WAYC_COLOR_BITS,
EGL_BLUE_SIZE, WAYC_COLOR_BITS,
EGL_ALPHA_SIZE, WAYC_COLOR_BITS,
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
EGL_NONE
};
static EGLint WAYC_OGL_ATTRS[] = {
EGL_CONTEXT_MAJOR_VERSION, WAYC_OPENGL_MAJOR,
EGL_CONTEXT_MINOR_VERSION, WAYC_OPENGL_MINOR,
EGL_NONE
};
/* clang-format on */
static bool wayc_graphics_config(EGLDisplay display, EGLConfig* config) {
wayc_notnull(display);
wayc_notnull(config);
EGLint nconfs = 0;
if (!eglChooseConfig(display, WAYC_EGL_ATTRS, config, 1, &nconfs))
return false;
return nconfs > 0;
}
enum graphics_error_e wayc_graphics_init(struct graphics_s* graphics,
struct wlstate_s* state) {
wayc_notnull(graphics);
wayc_notnull(state);
memset(graphics, 0, sizeof(*graphics));
EGLDisplay display = eglGetDisplay(state->display);
if (display == EGL_NO_DISPLAY) return GRAPHICS_ERROR_ACQUIRE_DISPLAY;
if (!eglInitialize(display, nullptr, nullptr)) {
eglTerminate(display);
return GRAPHICS_ERROR_ACQUIRE_DISPLAY;
}
if (!eglBindAPI(EGL_OPENGL_API)) {
eglTerminate(display);
return GRAPHICS_ERROR_USE_DESKTOP;
}
EGLConfig config;
if (!wayc_graphics_config(display, &config)) {
eglTerminate(display);
return GRAPHICS_ERROR_SELECT_CONFIG;
}
EGLContext context =
eglCreateContext(display, config, EGL_NO_CONTEXT, WAYC_OGL_ATTRS);
if (context == EGL_NO_CONTEXT) {
eglDestroyContext(display, context);
eglTerminate(display);
return GRAPHICS_ERROR_CREATE_CONTEXT;
}
graphics->display = display;
graphics->config = config;
graphics->context = context;
return GRAPHICS_ERROR_NONE;
}
void wayc_graphics_deinit(struct graphics_s* graphics) {
wayc_notnull(graphics);
if (graphics->display == nullptr || graphics->context == nullptr) return;
eglDestroyContext(graphics->display, graphics->context);
eglTerminate(graphics->display);
graphics->display = nullptr;
graphics->context = nullptr;
}
enum renderer_error_e wayc_renderer_init(struct renderer_s* renderer,
struct window_s* window,
struct graphics_s* graphics, i32 width,
i32 height) {
wayc_notnull(renderer);
wayc_notnull(window);
wayc_notnull(graphics);
memset(renderer, 0, sizeof(*renderer));
wl_egl_window_t ewindow =
wl_egl_window_create(window->surface, width, height);
if (ewindow == nullptr) return RENDERER_ERROR_WINDOW_CREATION;
EGLNativeWindowType nwindow = (EGLNativeWindowType)ewindow;
EGLSurface esurface = eglCreateWindowSurface(
graphics->display, graphics->config, nwindow, nullptr);
if (esurface == EGL_NO_SURFACE) {
wl_egl_window_destroy(ewindow);
return RENDERER_ERROR_SURFACE_CREATION;
}
renderer->graphics = graphics;
renderer->ewindow = ewindow;
renderer->esurface = esurface;
renderer->width = width;
renderer->height = height;
return RENDERER_ERROR_NONE;
}
void wayc_renderer_deinit(struct renderer_s* renderer) {
wayc_notnull(renderer);
if (renderer->graphics == nullptr || renderer->esurface == nullptr ||
renderer->ewindow == nullptr)
return;
eglDestroySurface(renderer->graphics->display, renderer->esurface);
wl_egl_window_destroy(renderer->ewindow);
renderer->graphics = nullptr;
renderer->esurface = nullptr;
renderer->ewindow = nullptr;
}
|