Wayland compositor (wlroots)
git clone https://git.lucas.co/cce-compositor.git
scenefx/render/fx_renderer/fx_texture.c (12.4K)
1 #include <assert.h>
2 #include <stdlib.h>
3 #include <wlr/render/interface.h>
4 #include <wlr/types/wlr_buffer.h>
5 #include <wlr/util/log.h>
6 #include <drm_fourcc.h>
7
8 #include "scenefx/render/fx_renderer/fx_renderer.h"
9 #include "render/fx_renderer/fx_renderer.h"
10 #include "render/pixel_format.h"
11 #include "render/egl.h"
12
13 static const struct wlr_texture_impl texture_impl;
14
15 bool wlr_texture_is_fx(struct wlr_texture *wlr_texture) {
16 return wlr_texture->impl == &texture_impl;
17 }
18
19 struct fx_texture *fx_get_texture(struct wlr_texture *wlr_texture) {
20 assert(wlr_texture_is_fx(wlr_texture));
21 struct fx_texture *texture = wl_container_of(wlr_texture, texture, wlr_texture);
22 return texture;
23 }
24
25 static bool fx_texture_update_from_buffer(struct wlr_texture *wlr_texture,
26 struct wlr_buffer *buffer, const pixman_region32_t *damage) {
27 struct fx_texture *texture = fx_get_texture(wlr_texture);
28
29 if (texture->drm_format == DRM_FORMAT_INVALID) {
30 return false;
31 }
32
33 void *data;
34 uint32_t format;
35 size_t stride;
36 if (!wlr_buffer_begin_data_ptr_access(buffer,
37 WLR_BUFFER_DATA_PTR_ACCESS_READ, &data, &format, &stride)) {
38 return false;
39 }
40
41 if (format != texture->drm_format) {
42 wlr_buffer_end_data_ptr_access(buffer);
43 return false;
44 }
45
46 const struct fx_pixel_format *fmt =
47 get_fx_format_from_drm(texture->drm_format);
48 assert(fmt);
49
50 const struct wlr_pixel_format_info *drm_fmt =
51 drm_get_pixel_format_info(texture->drm_format);
52 assert(drm_fmt);
53 if (pixel_format_info_pixels_per_block(drm_fmt) != 1) {
54 wlr_buffer_end_data_ptr_access(buffer);
55 wlr_log(WLR_ERROR, "Cannot update texture: block formats are not supported");
56 return false;
57 }
58
59 if (!pixel_format_info_check_stride(drm_fmt, stride, buffer->width)) {
60 wlr_buffer_end_data_ptr_access(buffer);
61 return false;
62 }
63
64 struct wlr_egl_context prev_ctx;
65 wlr_egl_make_current(texture->fx_renderer->egl, &prev_ctx);
66
67 push_fx_debug(texture->fx_renderer);
68
69 glBindTexture(GL_TEXTURE_2D, texture->tex);
70
71 int rects_len = 0;
72 pixman_box32_t *rects = pixman_region32_rectangles(damage, &rects_len);
73
74 for (int i = 0; i < rects_len; i++) {
75 pixman_box32_t rect = rects[i];
76
77 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, stride / drm_fmt->bytes_per_block);
78 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, rect.x1);
79 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, rect.y1);
80
81 int width = rect.x2 - rect.x1;
82 int height = rect.y2 - rect.y1;
83 glTexSubImage2D(GL_TEXTURE_2D, 0, rect.x1, rect.y1, width, height,
84 fmt->gl_format, fmt->gl_type, data);
85 }
86
87 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
88 glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0);
89 glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0);
90
91 glBindTexture(GL_TEXTURE_2D, 0);
92
93 pop_fx_debug(texture->fx_renderer);
94
95 wlr_egl_restore_context(&prev_ctx);
96
97 wlr_buffer_end_data_ptr_access(buffer);
98
99 return true;
100 }
101
102 void fx_texture_destroy(struct fx_texture *texture) {
103 wl_list_remove(&texture->link);
104 if (texture->buffer != NULL) {
105 wlr_buffer_unlock(texture->buffer->buffer);
106 } else {
107 struct wlr_egl_context prev_ctx;
108 wlr_egl_make_current(texture->fx_renderer->egl, &prev_ctx);
109
110 push_fx_debug(texture->fx_renderer);
111
112 glDeleteTextures(1, &texture->tex);
113 glDeleteFramebuffers(1, &texture->fbo);
114
115 pop_fx_debug(texture->fx_renderer);
116
117 wlr_egl_restore_context(&prev_ctx);
118 }
119
120 free(texture);
121 }
122
123 static bool fx_texture_bind(struct fx_texture *texture) {
124 if (texture->fbo) {
125 glBindFramebuffer(GL_FRAMEBUFFER, texture->fbo);
126 } else if (texture->buffer) {
127 if (texture->buffer->external_only) {
128 return false;
129 }
130
131 GLuint fbo = fx_framebuffer_get_fbo(texture->buffer);
132 if (!fbo) {
133 return false;
134 }
135
136 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
137 } else {
138 glGenFramebuffers(1, &texture->fbo);
139 glBindFramebuffer(GL_FRAMEBUFFER, texture->fbo);
140 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
141 texture->target, texture->tex, 0);
142
143 GLenum fb_status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
144 if (fb_status != GL_FRAMEBUFFER_COMPLETE) {
145 wlr_log(WLR_ERROR, "Failed to create FBO");
146 glDeleteFramebuffers(1, &texture->fbo);
147 texture->fbo = 0;
148
149 glBindFramebuffer(GL_FRAMEBUFFER, 0);
150 return false;
151 }
152 }
153
154 return true;
155 }
156
157 static bool fx_texture_read_pixels(struct wlr_texture *wlr_texture,
158 const struct wlr_texture_read_pixels_options *options) {
159 struct fx_texture *texture = fx_get_texture(wlr_texture);
160
161 struct wlr_box src;
162 wlr_texture_read_pixels_options_get_src_box(options, wlr_texture, &src);
163
164 const struct fx_pixel_format *fmt =
165 get_fx_format_from_drm(options->format);
166 if (fmt == NULL || !is_fx_pixel_format_supported(texture->fx_renderer, fmt)) {
167 wlr_log(WLR_ERROR, "Cannot read pixels: unsupported pixel format 0x%"PRIX32, options->format);
168 return false;
169 }
170
171 if (fmt->gl_format == GL_BGRA_EXT && !texture->fx_renderer->exts.EXT_read_format_bgra) {
172 wlr_log(WLR_ERROR,
173 "Cannot read pixels: missing GL_EXT_read_format_bgra extension");
174 return false;
175 }
176
177 const struct wlr_pixel_format_info *drm_fmt =
178 drm_get_pixel_format_info(fmt->drm_format);
179 assert(drm_fmt);
180 if (pixel_format_info_pixels_per_block(drm_fmt) != 1) {
181 wlr_log(WLR_ERROR, "Cannot read pixels: block formats are not supported");
182 return false;
183 }
184
185 push_fx_debug(texture->fx_renderer);
186 struct wlr_egl_context prev_ctx;
187 if (!wlr_egl_make_current(texture->fx_renderer->egl, &prev_ctx)) {
188 return false;
189 }
190
191 if (!fx_texture_bind(texture)) {
192 return false;
193 }
194
195 // Make sure any pending drawing is finished before we try to read it
196 glFinish();
197
198 glGetError(); // Clear the error flag
199
200 unsigned char *p = wlr_texture_read_pixel_options_get_data(options);
201
202 glPixelStorei(GL_PACK_ALIGNMENT, 1);
203 uint32_t pack_stride = pixel_format_info_min_stride(drm_fmt, src.width);
204 if (pack_stride == options->stride && options->dst_x == 0) {
205 // Under these particular conditions, we can read the pixels with only
206 // one glReadPixels call
207
208 glReadPixels(src.x, src.y, src.width, src.height, fmt->gl_format, fmt->gl_type, p);
209 } else {
210 // Unfortunately GLES2 doesn't support GL_PACK_ROW_LENGTH, so we have to read
211 // the lines out row by row
212 for (int32_t i = 0; i < src.height; ++i) {
213 uint32_t y = src.y + i;
214 glReadPixels(src.x, y, src.width, 1, fmt->gl_format,
215 fmt->gl_type, p + i * options->stride);
216 }
217 }
218
219 wlr_egl_restore_context(&prev_ctx);
220 pop_fx_debug(texture->fx_renderer);
221
222 return glGetError() == GL_NO_ERROR;
223 }
224
225 static uint32_t fx_texture_preferred_read_format(struct wlr_texture *wlr_texture) {
226 struct fx_texture *texture = fx_get_texture(wlr_texture);
227
228 push_fx_debug(texture->fx_renderer);
229
230 uint32_t fmt = DRM_FORMAT_INVALID;
231
232 struct wlr_egl_context prev_ctx;
233 if (!wlr_egl_make_current(texture->fx_renderer->egl, &prev_ctx)) {
234 return fmt;
235 }
236
237 if (!fx_texture_bind(texture)) {
238 goto out;
239 }
240
241 GLint gl_format = -1, gl_type = -1, alpha_size = -1;
242 glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &gl_format);
243 glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &gl_type);
244 glGetIntegerv(GL_ALPHA_BITS, &alpha_size);
245
246 glBindFramebuffer(GL_FRAMEBUFFER, 0);
247 pop_fx_debug(texture->fx_renderer);
248
249 const struct fx_pixel_format *pix_fmt =
250 get_fx_format_from_gl(gl_format, gl_type, alpha_size > 0);
251 if (pix_fmt != NULL) {
252 fmt = pix_fmt->drm_format;
253 goto out;
254 }
255
256 if (texture->fx_renderer->exts.EXT_read_format_bgra) {
257 fmt = DRM_FORMAT_XRGB8888;
258 goto out;
259 }
260
261 out:
262 wlr_egl_restore_context(&prev_ctx);
263 return fmt;
264 }
265
266 static void texture_handle_buffer_destroy(struct wlr_texture *wlr_texture) {
267 fx_texture_destroy(fx_get_texture(wlr_texture));
268 }
269
270 static const struct wlr_texture_impl texture_impl = {
271 .update_from_buffer = fx_texture_update_from_buffer,
272 .read_pixels = fx_texture_read_pixels,
273 .preferred_read_format = fx_texture_preferred_read_format,
274 .destroy = texture_handle_buffer_destroy,
275 };
276
277 static struct fx_texture *fx_texture_create(
278 struct fx_renderer *renderer, uint32_t width, uint32_t height) {
279 struct fx_texture *texture = calloc(1, sizeof(struct fx_texture));
280 if (texture == NULL) {
281 wlr_log_errno(WLR_ERROR, "Allocation failed");
282 return NULL;
283 }
284 wlr_texture_init(&texture->wlr_texture, &renderer->wlr_renderer,
285 &texture_impl, width, height);
286 texture->fx_renderer = renderer;
287 wl_list_insert(&renderer->textures, &texture->link);
288 return texture;
289 }
290
291 static struct wlr_texture *fx_texture_from_pixels(
292 struct wlr_renderer *wlr_renderer,
293 uint32_t drm_format, uint32_t stride, uint32_t width,
294 uint32_t height, const void *data) {
295 struct fx_renderer *renderer = fx_get_renderer(wlr_renderer);
296
297 const struct fx_pixel_format *fmt = get_fx_format_from_drm(drm_format);
298 if (fmt == NULL) {
299 wlr_log(WLR_ERROR, "Unsupported pixel format 0x%"PRIX32, drm_format);
300 return NULL;
301 }
302
303 const struct wlr_pixel_format_info *drm_fmt =
304 drm_get_pixel_format_info(drm_format);
305 assert(drm_fmt);
306 if (pixel_format_info_pixels_per_block(drm_fmt) != 1) {
307 wlr_log(WLR_ERROR, "Cannot upload texture: block formats are not supported");
308 return NULL;
309 }
310
311 if (!pixel_format_info_check_stride(drm_fmt, stride, width)) {
312 return NULL;
313 }
314
315 struct fx_texture *texture =
316 fx_texture_create(renderer, width, height);
317 if (texture == NULL) {
318 return NULL;
319 }
320 texture->target = GL_TEXTURE_2D;
321 texture->has_alpha = pixel_format_has_alpha(fmt->drm_format);
322 texture->drm_format = fmt->drm_format;
323
324 GLint internal_format = fmt->gl_internalformat;
325 if (!internal_format) {
326 internal_format = fmt->gl_format;
327 }
328
329 struct wlr_egl_context prev_ctx;
330 wlr_egl_make_current(renderer->egl, &prev_ctx);
331
332 push_fx_debug(renderer);
333
334 glGenTextures(1, &texture->tex);
335 glBindTexture(GL_TEXTURE_2D, texture->tex);
336
337 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
338 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
339 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, stride / drm_fmt->bytes_per_block);
340 glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0,
341 fmt->gl_format, fmt->gl_type, data);
342 glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
343
344 glBindTexture(GL_TEXTURE_2D, 0);
345
346 pop_fx_debug(renderer);
347
348 wlr_egl_restore_context(&prev_ctx);
349
350 return &texture->wlr_texture;
351 }
352
353 static struct wlr_texture *fx_texture_from_dmabuf(
354 struct wlr_renderer *wlr_renderer, struct wlr_buffer *wlr_buffer,
355 struct wlr_dmabuf_attributes *attribs) {
356 struct fx_renderer *renderer = fx_get_renderer(wlr_renderer);
357
358 if (!renderer->procs.glEGLImageTargetTexture2DOES) {
359 return NULL;
360 }
361
362 struct fx_framebuffer *buffer = fx_framebuffer_get_or_create(renderer, wlr_buffer);
363 if (!buffer) {
364 return NULL;
365 }
366
367 struct fx_texture *texture =
368 fx_texture_create(renderer, attribs->width, attribs->height);
369 if (texture == NULL) {
370 return NULL;
371 }
372 texture->target = buffer->external_only ? GL_TEXTURE_EXTERNAL_OES : GL_TEXTURE_2D;
373 texture->buffer = buffer;
374 texture->drm_format = DRM_FORMAT_INVALID; // texture can't be written anyways
375 texture->has_alpha = pixel_format_has_alpha(attribs->format);
376
377 struct wlr_egl_context prev_ctx;
378 wlr_egl_make_current(renderer->egl, &prev_ctx);
379 push_fx_debug(texture->fx_renderer);
380
381 bool invalid;
382 if (!buffer->tex) {
383 glGenTextures(1, &buffer->tex);
384 invalid = true;
385 } else {
386 // External changes are immediately made visible by the GL implementation
387 invalid = !buffer->external_only;
388 }
389
390 if (invalid) {
391 glBindTexture(texture->target, buffer->tex);
392 glTexParameteri(texture->target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
393 glTexParameteri(texture->target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
394 renderer->procs.glEGLImageTargetTexture2DOES(texture->target, buffer->image);
395 glBindTexture(texture->target, 0);
396 }
397
398 pop_fx_debug(texture->fx_renderer);
399 wlr_egl_restore_context(&prev_ctx);
400
401 texture->tex = buffer->tex;
402 wlr_buffer_lock(texture->buffer->buffer);
403 return &texture->wlr_texture;
404 }
405
406 struct wlr_texture *fx_texture_from_buffer(struct wlr_renderer *wlr_renderer,
407 struct wlr_buffer *buffer) {
408 struct fx_renderer *renderer = fx_get_renderer(wlr_renderer);
409
410 void *data;
411 uint32_t format;
412 size_t stride;
413 struct wlr_dmabuf_attributes dmabuf;
414 if (wlr_buffer_get_dmabuf(buffer, &dmabuf)) {
415 return fx_texture_from_dmabuf(&renderer->wlr_renderer, buffer, &dmabuf);
416 } else if (wlr_buffer_begin_data_ptr_access(buffer,
417 WLR_BUFFER_DATA_PTR_ACCESS_READ, &data, &format, &stride)) {
418 struct wlr_texture *tex = fx_texture_from_pixels(wlr_renderer,
419 format, stride, buffer->width, buffer->height, data);
420 wlr_buffer_end_data_ptr_access(buffer);
421 return tex;
422 } else {
423 return NULL;
424 }
425 }
426
427 void fx_texture_get_attribs(struct wlr_texture *wlr_texture,
428 struct fx_texture_attribs *attribs) {
429 struct fx_texture *texture = fx_get_texture(wlr_texture);
430 *attribs = (struct fx_texture_attribs){
431 .target = texture->target,
432 .tex = texture->tex,
433 .has_alpha = texture->has_alpha,
434 };
435 }