-
Notifications
You must be signed in to change notification settings - Fork 1
/
render.c
233 lines (197 loc) · 7.05 KB
/
render.c
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include "render.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <GLES2/gl2.h>
#include <cairo/cairo-gl.h>
static void handle_xdg_output_name(void *data,
struct zxdg_output_v1 *xdg_output
__attribute__((unused)),
const char *name) {
struct wayab_renderer *ptr = data;
ptr->name = strdup(name);
}
static void handle_xdg_output_done(void *data __attribute__((unused)),
struct zxdg_output_v1 *xdg_output) {
// We have no further use for this object.
zxdg_output_v1_destroy(xdg_output);
}
static void noop() {}
struct zxdg_output_v1_listener xdg_output_listener = {
.name = handle_xdg_output_name,
.done = handle_xdg_output_done,
.logical_position = noop,
.logical_size = noop,
.description = noop,
};
static void layer_surface_configure(void *data,
struct zwlr_layer_surface_v1 *layer_surface,
uint32_t serial, uint32_t width,
uint32_t height) {
struct wayab_renderer *ptr = data;
ptr->width = width;
ptr->height = height;
LOG("name: %s width: %d, height: %d\n", ptr->name, width, height);
zwlr_layer_surface_v1_ack_configure(layer_surface, serial);
}
static void layer_surface_closed(void *data,
struct zwlr_layer_surface_v1 *layer_surface
__attribute__((unused))) {}
static struct zwlr_layer_surface_v1_listener layer_surface_listener = {
.configure = layer_surface_configure,
.closed = layer_surface_closed,
};
struct wayab_renderer *wayab_renderer_new(struct wl_output *wl_output,
struct wayab_wl *wl) {
struct wayab_renderer *ptr = calloc(1, sizeof(struct wayab_renderer));
ptr->name = NULL;
ptr->native_display = wl->display;
ptr->wl_output = wl_output;
// init surface
ptr->wl_surface = wl_compositor_create_surface(wl->compositor);
if (ptr->wl_surface == NULL) {
LOG("No Compositor surface\n");
goto error;
}
// init output name
assert(wl->output_manager != NULL);
struct zxdg_output_v1 *xdg_output =
zxdg_output_manager_v1_get_xdg_output(wl->output_manager, wl_output);
zxdg_output_v1_add_listener(xdg_output, &xdg_output_listener, ptr);
// init layer_surface and output resolution
ptr->layer_surface = zwlr_layer_shell_v1_get_layer_surface(
wl->layer_shell, ptr->wl_surface, wl_output,
ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND, "wayab");
zwlr_layer_surface_v1_set_size(ptr->layer_surface, 0, 0);
zwlr_layer_surface_v1_set_anchor(ptr->layer_surface,
ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP |
ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT |
ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM |
ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT);
zwlr_layer_surface_v1_set_exclusive_zone(ptr->layer_surface, -1);
zwlr_layer_surface_v1_add_listener(ptr->layer_surface,
&layer_surface_listener, ptr);
wl_surface_commit(ptr->wl_surface);
wl_display_roundtrip(wl->display);
// init window
ptr->native_window =
wl_egl_window_create(ptr->wl_surface, ptr->width, ptr->height);
if (ptr->native_window == EGL_NO_SURFACE) {
LOG("Failed to create egl window\n");
goto error;
}
ptr->display = eglGetDisplay(ptr->native_display);
if (ptr->display == EGL_NO_DISPLAY) {
goto error;
}
EGLint major_version;
EGLint minor_version;
if (!eglInitialize(ptr->display, &major_version, &minor_version)) {
LOG("eglInitialize\n");
goto error;
}
EGLint num_configs;
if ((eglGetConfigs(ptr->display, NULL, 0, &num_configs) != EGL_TRUE) ||
(num_configs == 0)) {
LOG("eglGetConfigs\n");
goto error;
}
EGLint attrib_list[] = {EGL_SURFACE_TYPE,
EGL_WINDOW_BIT,
EGL_RENDERABLE_TYPE,
EGL_OPENGL_ES2_BIT,
EGL_RED_SIZE,
8,
EGL_GREEN_SIZE,
8,
EGL_BLUE_SIZE,
8,
EGL_NONE};
EGLConfig config;
if ((eglChooseConfig(ptr->display, attrib_list, &config, 1, &num_configs) !=
EGL_TRUE) ||
(num_configs != 1)) {
LOG("eglChooseConfig\n");
goto error;
}
ptr->surface =
eglCreateWindowSurface(ptr->display, config, ptr->native_window, NULL);
if (ptr->surface == EGL_NO_SURFACE) {
LOG("eglCreateWindowSurface\n");
goto error;
}
EGLint context_attribs[] = {EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE,
EGL_NONE};
ptr->context =
eglCreateContext(ptr->display, config, EGL_NO_CONTEXT, context_attribs);
if (ptr->context == EGL_NO_CONTEXT) {
LOG("eglCreateContext\n");
goto error;
}
ptr->cairo_device = cairo_egl_device_create(ptr->display, ptr->context);
if (cairo_device_status(ptr->cairo_device) != CAIRO_STATUS_SUCCESS) {
LOG("cairo_egl_device_create");
goto error;
}
ptr->cairo_surface = cairo_gl_surface_create_for_egl(
ptr->cairo_device, ptr->surface, ptr->width, ptr->height);
if (ptr->cairo_surface == NULL) {
LOG("cairo_gl_surface_create_for_egl");
goto error;
}
ptr->cr = cairo_create(ptr->cairo_surface);
struct wayab_rule *rule, *tmp;
wl_list_for_each_safe(rule, tmp, &wl->config->rules, link) {
if (strcmp(rule->output_name, "*") == 0 ||
strcmp(rule->output_name, ptr->name) == 0) {
ptr->image = wayab_image_new(rule, ptr->cr, ptr->width, ptr->height);
break;
}
}
if (ptr->image == NULL) {
LOG("no matching output\n");
goto error;
}
return ptr;
error:
if (ptr)
wayab_renderer_destroy(ptr);
return NULL;
}
int wayab_renderer_destroy(struct wayab_renderer *ptr) {
eglDestroySurface(ptr->display, ptr->surface);
wl_egl_window_destroy(ptr->native_window);
zwlr_layer_surface_v1_destroy(ptr->layer_surface);
wl_surface_destroy(ptr->wl_surface);
eglDestroyContext(ptr->display, ptr->context);
if (ptr->name) {
free(ptr->name);
}
if (ptr->image)
wayab_image_destroy(ptr->image);
if (ptr->cr)
cairo_destroy(ptr->cr);
free(ptr);
return 0;
}
int wayab_renderer_draw(struct wayab_renderer *ptr, uint64_t counter) {
if (!eglMakeCurrent(ptr->display, ptr->surface, ptr->surface, ptr->context)) {
LOG("eglMakeCurrent\n");
return -1;
}
int frame = counter % ptr->image->count;
cairo_surface_t *cache_surface = ptr->image->surfaces[frame];
if (cache_surface == NULL) {
if (wayab_image_bootstrap(ptr->image, ptr->cr, ptr->width, ptr->height,
0)) {
LOG("Failed to load frames\n");
return -1;
}
return 0;
}
cairo_set_source_surface(ptr->cr, cache_surface, 0, 0);
cairo_paint(ptr->cr);
cairo_gl_surface_swapbuffers(ptr->cairo_surface);
glClear(GL_COLOR_BUFFER_BIT);
return 0;
}