2017-06-08 19:52:42 +00:00
|
|
|
#include <assert.h>
|
2021-02-16 18:30:26 +00:00
|
|
|
#include <drm_fourcc.h>
|
2021-04-20 18:48:22 +00:00
|
|
|
#include <gbm.h>
|
2017-06-23 15:38:45 +00:00
|
|
|
#include <GLES2/gl2.h>
|
2017-08-09 19:25:34 +00:00
|
|
|
#include <GLES2/gl2ext.h>
|
2018-02-12 20:29:23 +00:00
|
|
|
#include <stdint.h>
|
2018-03-20 18:14:33 +00:00
|
|
|
#include <stdio.h>
|
2018-02-12 20:29:23 +00:00
|
|
|
#include <stdlib.h>
|
2020-06-10 12:47:12 +00:00
|
|
|
#include <unistd.h>
|
2017-06-08 19:52:42 +00:00
|
|
|
#include <wayland-server-protocol.h>
|
2018-02-12 20:29:23 +00:00
|
|
|
#include <wayland-util.h>
|
2017-10-21 01:48:58 +00:00
|
|
|
#include <wlr/render/egl.h>
|
2017-06-08 19:52:42 +00:00
|
|
|
#include <wlr/render/interface.h>
|
2018-03-20 18:14:33 +00:00
|
|
|
#include <wlr/render/wlr_renderer.h>
|
2018-03-15 08:11:03 +00:00
|
|
|
#include <wlr/types/wlr_matrix.h>
|
2021-07-01 20:36:01 +00:00
|
|
|
#include <wlr/util/box.h>
|
2017-06-21 16:10:07 +00:00
|
|
|
#include <wlr/util/log.h>
|
2021-06-24 10:10:12 +00:00
|
|
|
#include "render/egl.h"
|
2018-03-24 22:30:28 +00:00
|
|
|
#include "render/gles2.h"
|
2021-03-24 14:10:52 +00:00
|
|
|
#include "render/pixel_format.h"
|
2017-06-08 19:52:42 +00:00
|
|
|
|
2020-06-08 16:24:19 +00:00
|
|
|
static const GLfloat verts[] = {
|
|
|
|
1, 0, // top right
|
|
|
|
0, 0, // top left
|
|
|
|
1, 1, // bottom right
|
|
|
|
0, 1, // bottom left
|
|
|
|
};
|
|
|
|
|
2018-03-20 18:14:33 +00:00
|
|
|
static const struct wlr_renderer_impl renderer_impl;
|
2017-06-08 19:52:42 +00:00
|
|
|
|
2021-05-28 06:06:49 +00:00
|
|
|
bool wlr_renderer_is_gles2(struct wlr_renderer *wlr_renderer) {
|
|
|
|
return wlr_renderer->impl == &renderer_impl;
|
|
|
|
}
|
|
|
|
|
2020-07-28 11:45:24 +00:00
|
|
|
struct wlr_gles2_renderer *gles2_get_renderer(
|
2018-03-20 18:14:33 +00:00
|
|
|
struct wlr_renderer *wlr_renderer) {
|
2021-05-28 06:06:49 +00:00
|
|
|
assert(wlr_renderer_is_gles2(wlr_renderer));
|
2018-03-24 22:30:28 +00:00
|
|
|
return (struct wlr_gles2_renderer *)wlr_renderer;
|
|
|
|
}
|
|
|
|
|
2018-04-01 20:07:50 +00:00
|
|
|
static struct wlr_gles2_renderer *gles2_get_renderer_in_context(
|
2018-03-24 22:30:28 +00:00
|
|
|
struct wlr_renderer *wlr_renderer) {
|
|
|
|
struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer);
|
2018-07-13 19:46:54 +00:00
|
|
|
assert(wlr_egl_is_current(renderer->egl));
|
2021-02-16 15:19:19 +00:00
|
|
|
assert(renderer->current_buffer != NULL);
|
2018-03-20 18:14:33 +00:00
|
|
|
return renderer;
|
2017-06-08 19:52:42 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 14:56:23 +00:00
|
|
|
static void destroy_buffer(struct wlr_gles2_buffer *buffer) {
|
|
|
|
wl_list_remove(&buffer->link);
|
2021-08-11 08:00:50 +00:00
|
|
|
wlr_addon_finish(&buffer->addon);
|
2020-07-28 14:56:23 +00:00
|
|
|
|
2021-01-14 11:00:06 +00:00
|
|
|
struct wlr_egl_context prev_ctx;
|
|
|
|
wlr_egl_save_context(&prev_ctx);
|
2021-01-09 11:13:35 +00:00
|
|
|
wlr_egl_make_current(buffer->renderer->egl);
|
2020-07-28 14:56:23 +00:00
|
|
|
|
|
|
|
push_gles2_debug(buffer->renderer);
|
|
|
|
|
|
|
|
glDeleteFramebuffers(1, &buffer->fbo);
|
|
|
|
glDeleteRenderbuffers(1, &buffer->rbo);
|
|
|
|
|
|
|
|
pop_gles2_debug(buffer->renderer);
|
|
|
|
|
|
|
|
wlr_egl_destroy_image(buffer->renderer->egl, buffer->image);
|
2021-01-14 11:00:06 +00:00
|
|
|
|
|
|
|
wlr_egl_restore_context(&prev_ctx);
|
|
|
|
|
2020-07-28 14:56:23 +00:00
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
|
2021-08-11 08:00:50 +00:00
|
|
|
static void handle_buffer_destroy(struct wlr_addon *addon) {
|
|
|
|
struct wlr_gles2_buffer *buffer =
|
|
|
|
wl_container_of(addon, buffer, addon);
|
|
|
|
destroy_buffer(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wlr_addon_interface buffer_addon_impl = {
|
|
|
|
.name = "wlr_gles2_buffer",
|
|
|
|
.destroy = handle_buffer_destroy,
|
|
|
|
};
|
|
|
|
|
2020-07-28 14:56:23 +00:00
|
|
|
static struct wlr_gles2_buffer *get_buffer(struct wlr_gles2_renderer *renderer,
|
|
|
|
struct wlr_buffer *wlr_buffer) {
|
2021-08-11 08:00:50 +00:00
|
|
|
struct wlr_addon *addon =
|
|
|
|
wlr_addon_find(&wlr_buffer->addons, renderer, &buffer_addon_impl);
|
|
|
|
if (addon == NULL) {
|
|
|
|
return NULL;
|
2020-07-28 14:56:23 +00:00
|
|
|
}
|
2021-08-11 08:00:50 +00:00
|
|
|
struct wlr_gles2_buffer *buffer = wl_container_of(addon, buffer, addon);
|
|
|
|
return buffer;
|
2020-07-28 14:56:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct wlr_gles2_buffer *create_buffer(struct wlr_gles2_renderer *renderer,
|
|
|
|
struct wlr_buffer *wlr_buffer) {
|
|
|
|
struct wlr_gles2_buffer *buffer = calloc(1, sizeof(*buffer));
|
|
|
|
if (buffer == NULL) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
buffer->buffer = wlr_buffer;
|
|
|
|
buffer->renderer = renderer;
|
|
|
|
|
|
|
|
struct wlr_dmabuf_attributes dmabuf = {0};
|
|
|
|
if (!wlr_buffer_get_dmabuf(wlr_buffer, &dmabuf)) {
|
|
|
|
goto error_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool external_only;
|
|
|
|
buffer->image = wlr_egl_create_image_from_dmabuf(renderer->egl,
|
|
|
|
&dmabuf, &external_only);
|
|
|
|
if (buffer->image == EGL_NO_IMAGE_KHR) {
|
|
|
|
goto error_buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
push_gles2_debug(renderer);
|
|
|
|
|
|
|
|
glGenRenderbuffers(1, &buffer->rbo);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, buffer->rbo);
|
|
|
|
renderer->procs.glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER,
|
|
|
|
buffer->image);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
|
|
|
|
|
|
|
glGenFramebuffers(1, &buffer->fbo);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, buffer->fbo);
|
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
|
|
|
|
GL_RENDERBUFFER, buffer->rbo);
|
|
|
|
GLenum fb_status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
|
|
|
|
pop_gles2_debug(renderer);
|
|
|
|
|
|
|
|
if (fb_status != GL_FRAMEBUFFER_COMPLETE) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to create FBO");
|
|
|
|
goto error_image;
|
|
|
|
}
|
|
|
|
|
2021-08-11 08:00:50 +00:00
|
|
|
wlr_addon_init(&buffer->addon, &wlr_buffer->addons, renderer,
|
|
|
|
&buffer_addon_impl);
|
2020-07-28 14:56:23 +00:00
|
|
|
|
|
|
|
wl_list_insert(&renderer->buffers, &buffer->link);
|
|
|
|
|
|
|
|
wlr_log(WLR_DEBUG, "Created GL FBO for buffer %dx%d",
|
|
|
|
wlr_buffer->width, wlr_buffer->height);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
|
|
|
|
error_image:
|
|
|
|
wlr_egl_destroy_image(renderer->egl, buffer->image);
|
|
|
|
error_buffer:
|
|
|
|
free(buffer);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool gles2_bind_buffer(struct wlr_renderer *wlr_renderer,
|
|
|
|
struct wlr_buffer *wlr_buffer) {
|
2021-01-14 11:45:07 +00:00
|
|
|
struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer);
|
2020-07-28 14:56:23 +00:00
|
|
|
|
|
|
|
if (renderer->current_buffer != NULL) {
|
2021-01-14 11:45:07 +00:00
|
|
|
assert(wlr_egl_is_current(renderer->egl));
|
|
|
|
|
2020-07-28 14:56:23 +00:00
|
|
|
push_gles2_debug(renderer);
|
|
|
|
glFlush();
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
pop_gles2_debug(renderer);
|
|
|
|
|
|
|
|
wlr_buffer_unlock(renderer->current_buffer->buffer);
|
|
|
|
renderer->current_buffer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wlr_buffer == NULL) {
|
2021-01-14 11:45:07 +00:00
|
|
|
wlr_egl_unset_current(renderer->egl);
|
2020-07-28 14:56:23 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-14 11:45:07 +00:00
|
|
|
wlr_egl_make_current(renderer->egl);
|
|
|
|
|
2020-07-28 14:56:23 +00:00
|
|
|
struct wlr_gles2_buffer *buffer = get_buffer(renderer, wlr_buffer);
|
|
|
|
if (buffer == NULL) {
|
|
|
|
buffer = create_buffer(renderer, wlr_buffer);
|
|
|
|
}
|
|
|
|
if (buffer == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
wlr_buffer_lock(wlr_buffer);
|
|
|
|
renderer->current_buffer = buffer;
|
|
|
|
|
|
|
|
push_gles2_debug(renderer);
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, renderer->current_buffer->fbo);
|
|
|
|
pop_gles2_debug(renderer);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-20 22:10:42 +00:00
|
|
|
static void gles2_begin(struct wlr_renderer *wlr_renderer, uint32_t width,
|
|
|
|
uint32_t height) {
|
2018-04-12 23:38:33 +00:00
|
|
|
struct wlr_gles2_renderer *renderer =
|
|
|
|
gles2_get_renderer_in_context(wlr_renderer);
|
2017-06-08 19:52:42 +00:00
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
push_gles2_debug(renderer);
|
2017-06-08 19:52:42 +00:00
|
|
|
|
2018-03-20 22:10:42 +00:00
|
|
|
glViewport(0, 0, width, height);
|
2018-04-12 23:38:33 +00:00
|
|
|
renderer->viewport_width = width;
|
|
|
|
renderer->viewport_height = height;
|
2017-08-10 12:36:33 +00:00
|
|
|
|
2021-03-04 18:22:28 +00:00
|
|
|
// refresh projection matrix
|
|
|
|
wlr_matrix_projection(renderer->projection, width, height,
|
|
|
|
WL_OUTPUT_TRANSFORM_NORMAL);
|
|
|
|
|
2018-05-18 17:05:49 +00:00
|
|
|
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
|
2017-08-10 12:36:33 +00:00
|
|
|
|
2018-03-20 18:14:33 +00:00
|
|
|
// XXX: maybe we should save output projection and remove some of the need
|
2017-06-08 19:52:42 +00:00
|
|
|
// for users to sling matricies themselves
|
2018-03-20 18:14:33 +00:00
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
pop_gles2_debug(renderer);
|
2017-06-08 19:52:42 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 18:14:33 +00:00
|
|
|
static void gles2_end(struct wlr_renderer *wlr_renderer) {
|
2018-03-24 22:30:28 +00:00
|
|
|
gles2_get_renderer_in_context(wlr_renderer);
|
2017-06-08 19:52:42 +00:00
|
|
|
// no-op
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:14:33 +00:00
|
|
|
static void gles2_clear(struct wlr_renderer *wlr_renderer,
|
2018-03-15 10:10:56 +00:00
|
|
|
const float color[static 4]) {
|
2020-07-28 11:58:07 +00:00
|
|
|
struct wlr_gles2_renderer *renderer =
|
|
|
|
gles2_get_renderer_in_context(wlr_renderer);
|
2018-03-20 18:14:33 +00:00
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
push_gles2_debug(renderer);
|
2018-03-15 10:10:56 +00:00
|
|
|
glClearColor(color[0], color[1], color[2], color[3]);
|
2018-01-22 15:42:22 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2020-07-28 11:58:07 +00:00
|
|
|
pop_gles2_debug(renderer);
|
2018-01-22 15:42:22 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 18:14:33 +00:00
|
|
|
static void gles2_scissor(struct wlr_renderer *wlr_renderer,
|
2018-01-22 15:42:22 +00:00
|
|
|
struct wlr_box *box) {
|
2018-04-12 23:38:33 +00:00
|
|
|
struct wlr_gles2_renderer *renderer =
|
|
|
|
gles2_get_renderer_in_context(wlr_renderer);
|
2018-03-20 18:14:33 +00:00
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
push_gles2_debug(renderer);
|
2018-01-22 15:42:22 +00:00
|
|
|
if (box != NULL) {
|
2021-02-16 15:19:19 +00:00
|
|
|
glScissor(box->x, box->y, box->width, box->height);
|
2018-01-22 15:42:22 +00:00
|
|
|
glEnable(GL_SCISSOR_TEST);
|
|
|
|
} else {
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
}
|
2020-07-28 11:58:07 +00:00
|
|
|
pop_gles2_debug(renderer);
|
2018-01-22 15:42:22 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 16:40:29 +00:00
|
|
|
static const float flip_180[9] = {
|
|
|
|
1.0f, 0.0f, 0.0f,
|
|
|
|
0.0f, -1.0f, 0.0f,
|
|
|
|
0.0f, 0.0f, 1.0f,
|
|
|
|
};
|
|
|
|
|
2020-04-27 10:27:01 +00:00
|
|
|
static bool gles2_render_subtexture_with_matrix(
|
|
|
|
struct wlr_renderer *wlr_renderer, struct wlr_texture *wlr_texture,
|
|
|
|
const struct wlr_fbox *box, const float matrix[static 9],
|
2018-03-24 22:30:28 +00:00
|
|
|
float alpha) {
|
|
|
|
struct wlr_gles2_renderer *renderer =
|
|
|
|
gles2_get_renderer_in_context(wlr_renderer);
|
|
|
|
struct wlr_gles2_texture *texture =
|
2018-08-02 11:38:00 +00:00
|
|
|
gles2_get_texture(wlr_texture);
|
2021-04-29 08:55:51 +00:00
|
|
|
assert(texture->renderer == renderer);
|
2018-03-24 22:30:28 +00:00
|
|
|
|
2018-06-13 10:43:01 +00:00
|
|
|
struct wlr_gles2_tex_shader *shader = NULL;
|
2018-06-02 22:20:09 +00:00
|
|
|
|
2019-11-06 08:28:43 +00:00
|
|
|
switch (texture->target) {
|
|
|
|
case GL_TEXTURE_2D:
|
2018-06-02 22:20:09 +00:00
|
|
|
if (texture->has_alpha) {
|
2018-06-13 10:43:01 +00:00
|
|
|
shader = &renderer->shaders.tex_rgba;
|
2018-06-02 22:20:09 +00:00
|
|
|
} else {
|
2018-06-13 10:43:01 +00:00
|
|
|
shader = &renderer->shaders.tex_rgbx;
|
2018-06-02 22:20:09 +00:00
|
|
|
}
|
2018-03-24 22:30:28 +00:00
|
|
|
break;
|
2019-11-06 08:28:43 +00:00
|
|
|
case GL_TEXTURE_EXTERNAL_OES:
|
2018-06-13 10:43:01 +00:00
|
|
|
shader = &renderer->shaders.tex_ext;
|
2018-11-01 17:03:32 +00:00
|
|
|
|
2021-07-11 18:00:02 +00:00
|
|
|
if (!renderer->exts.OES_egl_image_external) {
|
2018-11-01 17:03:32 +00:00
|
|
|
wlr_log(WLR_ERROR, "Failed to render texture: "
|
|
|
|
"GL_TEXTURE_EXTERNAL_OES not supported");
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-24 22:30:28 +00:00
|
|
|
break;
|
2019-11-06 08:28:43 +00:00
|
|
|
default:
|
|
|
|
abort();
|
2018-03-20 18:14:33 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 16:40:29 +00:00
|
|
|
float gl_matrix[9];
|
2021-03-04 18:22:28 +00:00
|
|
|
wlr_matrix_multiply(gl_matrix, renderer->projection, matrix);
|
|
|
|
wlr_matrix_multiply(gl_matrix, flip_180, gl_matrix);
|
2021-02-16 15:19:19 +00:00
|
|
|
|
2018-03-19 19:21:02 +00:00
|
|
|
// OpenGL ES 2 requires the glUniformMatrix3fv transpose parameter to be set
|
|
|
|
// to GL_FALSE
|
2020-06-11 16:40:29 +00:00
|
|
|
wlr_matrix_transpose(gl_matrix, gl_matrix);
|
2018-03-19 19:21:02 +00:00
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
push_gles2_debug(renderer);
|
2018-03-24 22:30:28 +00:00
|
|
|
|
2021-07-11 17:44:57 +00:00
|
|
|
if (!texture->has_alpha && alpha == 1.0) {
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
} else {
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
}
|
|
|
|
|
2018-03-24 22:30:28 +00:00
|
|
|
glActiveTexture(GL_TEXTURE0);
|
2019-11-06 08:28:43 +00:00
|
|
|
glBindTexture(texture->target, texture->tex);
|
2018-03-24 22:30:28 +00:00
|
|
|
|
2019-11-06 08:28:43 +00:00
|
|
|
glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
2018-03-24 22:30:28 +00:00
|
|
|
|
2018-06-13 10:43:01 +00:00
|
|
|
glUseProgram(shader->program);
|
2018-03-20 18:14:33 +00:00
|
|
|
|
2020-06-11 16:40:29 +00:00
|
|
|
glUniformMatrix3fv(shader->proj, 1, GL_FALSE, gl_matrix);
|
2018-06-13 10:43:01 +00:00
|
|
|
glUniform1i(shader->tex, 0);
|
|
|
|
glUniform1f(shader->alpha, alpha);
|
2018-03-24 22:30:28 +00:00
|
|
|
|
2020-06-08 16:24:19 +00:00
|
|
|
const GLfloat x1 = box->x / wlr_texture->width;
|
|
|
|
const GLfloat y1 = box->y / wlr_texture->height;
|
|
|
|
const GLfloat x2 = (box->x + box->width) / wlr_texture->width;
|
|
|
|
const GLfloat y2 = (box->y + box->height) / wlr_texture->height;
|
|
|
|
const GLfloat texcoord[] = {
|
|
|
|
x2, y1, // top right
|
|
|
|
x1, y1, // top left
|
|
|
|
x2, y2, // bottom right
|
|
|
|
x1, y2, // bottom left
|
|
|
|
};
|
|
|
|
|
|
|
|
glVertexAttribPointer(shader->pos_attrib, 2, GL_FLOAT, GL_FALSE, 0, verts);
|
|
|
|
glVertexAttribPointer(shader->tex_attrib, 2, GL_FLOAT, GL_FALSE, 0, texcoord);
|
|
|
|
|
|
|
|
glEnableVertexAttribArray(shader->pos_attrib);
|
|
|
|
glEnableVertexAttribArray(shader->tex_attrib);
|
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
|
|
|
|
glDisableVertexAttribArray(shader->pos_attrib);
|
|
|
|
glDisableVertexAttribArray(shader->tex_attrib);
|
2018-03-24 22:30:28 +00:00
|
|
|
|
2020-01-13 14:26:48 +00:00
|
|
|
glBindTexture(texture->target, 0);
|
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
pop_gles2_debug(renderer);
|
2017-06-08 19:52:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-26 16:41:51 +00:00
|
|
|
static void gles2_render_quad_with_matrix(struct wlr_renderer *wlr_renderer,
|
2018-03-15 14:33:58 +00:00
|
|
|
const float color[static 4], const float matrix[static 9]) {
|
2018-03-24 22:30:28 +00:00
|
|
|
struct wlr_gles2_renderer *renderer =
|
|
|
|
gles2_get_renderer_in_context(wlr_renderer);
|
2018-03-20 18:14:33 +00:00
|
|
|
|
2020-06-11 16:40:29 +00:00
|
|
|
float gl_matrix[9];
|
2021-03-04 18:22:28 +00:00
|
|
|
wlr_matrix_multiply(gl_matrix, renderer->projection, matrix);
|
|
|
|
wlr_matrix_multiply(gl_matrix, flip_180, gl_matrix);
|
2021-02-16 15:19:19 +00:00
|
|
|
|
2018-03-19 19:21:02 +00:00
|
|
|
// OpenGL ES 2 requires the glUniformMatrix3fv transpose parameter to be set
|
|
|
|
// to GL_FALSE
|
2020-06-11 16:40:29 +00:00
|
|
|
wlr_matrix_transpose(gl_matrix, gl_matrix);
|
2018-03-19 19:21:02 +00:00
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
push_gles2_debug(renderer);
|
2021-07-11 17:44:57 +00:00
|
|
|
|
|
|
|
if (color[3] == 1.0) {
|
|
|
|
glDisable(GL_BLEND);
|
|
|
|
} else {
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
}
|
|
|
|
|
2018-06-02 22:20:09 +00:00
|
|
|
glUseProgram(renderer->shaders.quad.program);
|
|
|
|
|
2020-06-11 16:40:29 +00:00
|
|
|
glUniformMatrix3fv(renderer->shaders.quad.proj, 1, GL_FALSE, gl_matrix);
|
2018-06-02 22:20:09 +00:00
|
|
|
glUniform4f(renderer->shaders.quad.color, color[0], color[1], color[2], color[3]);
|
2020-06-08 16:24:19 +00:00
|
|
|
|
|
|
|
glVertexAttribPointer(renderer->shaders.quad.pos_attrib, 2, GL_FLOAT, GL_FALSE,
|
|
|
|
0, verts);
|
|
|
|
|
|
|
|
glEnableVertexAttribArray(renderer->shaders.quad.pos_attrib);
|
|
|
|
|
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
|
|
|
|
|
|
|
glDisableVertexAttribArray(renderer->shaders.quad.pos_attrib);
|
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
pop_gles2_debug(renderer);
|
2017-06-15 19:31:13 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 18:20:00 +00:00
|
|
|
static const uint32_t *gles2_get_shm_texture_formats(
|
2018-03-21 07:50:59 +00:00
|
|
|
struct wlr_renderer *wlr_renderer, size_t *len) {
|
2021-07-30 03:53:22 +00:00
|
|
|
struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer);
|
|
|
|
return get_gles2_shm_formats(renderer, len);
|
2018-10-31 16:20:27 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 13:53:13 +00:00
|
|
|
static const struct wlr_drm_format_set *gles2_get_dmabuf_texture_formats(
|
2019-04-01 16:17:23 +00:00
|
|
|
struct wlr_renderer *wlr_renderer) {
|
2018-04-08 15:00:56 +00:00
|
|
|
struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer);
|
2020-11-18 13:53:13 +00:00
|
|
|
return wlr_egl_get_dmabuf_texture_formats(renderer->egl);
|
2018-04-08 15:00:56 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 14:22:06 +00:00
|
|
|
static const struct wlr_drm_format_set *gles2_get_render_formats(
|
2020-11-18 13:16:22 +00:00
|
|
|
struct wlr_renderer *wlr_renderer) {
|
|
|
|
struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer);
|
|
|
|
return wlr_egl_get_dmabuf_render_formats(renderer->egl);
|
|
|
|
}
|
|
|
|
|
2021-02-16 18:30:26 +00:00
|
|
|
static uint32_t gles2_preferred_read_format(
|
2018-10-31 16:20:27 +00:00
|
|
|
struct wlr_renderer *wlr_renderer) {
|
|
|
|
struct wlr_gles2_renderer *renderer =
|
|
|
|
gles2_get_renderer_in_context(wlr_renderer);
|
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
push_gles2_debug(renderer);
|
2021-02-16 15:19:19 +00:00
|
|
|
|
|
|
|
GLint gl_format = -1, gl_type = -1;
|
2018-10-31 16:20:27 +00:00
|
|
|
glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, &gl_format);
|
|
|
|
glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, &gl_type);
|
|
|
|
|
|
|
|
EGLint alpha_size = -1;
|
2021-02-16 15:19:19 +00:00
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, renderer->current_buffer->rbo);
|
|
|
|
glGetRenderbufferParameteriv(GL_RENDERBUFFER,
|
|
|
|
GL_RENDERBUFFER_ALPHA_SIZE, &alpha_size);
|
|
|
|
glBindRenderbuffer(GL_RENDERBUFFER, 0);
|
|
|
|
|
|
|
|
pop_gles2_debug(renderer);
|
2018-10-31 16:20:27 +00:00
|
|
|
|
|
|
|
const struct wlr_gles2_pixel_format *fmt =
|
|
|
|
get_gles2_format_from_gl(gl_format, gl_type, alpha_size > 0);
|
|
|
|
if (fmt != NULL) {
|
2021-02-16 18:30:26 +00:00
|
|
|
return fmt->drm_format;
|
2018-10-31 16:20:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-11 18:00:02 +00:00
|
|
|
if (renderer->exts.EXT_read_format_bgra) {
|
2021-02-16 18:30:26 +00:00
|
|
|
return DRM_FORMAT_XRGB8888;
|
2018-10-31 16:20:27 +00:00
|
|
|
}
|
2021-02-16 18:30:26 +00:00
|
|
|
return DRM_FORMAT_XBGR8888;
|
2018-10-31 16:20:27 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 18:14:33 +00:00
|
|
|
static bool gles2_read_pixels(struct wlr_renderer *wlr_renderer,
|
2021-02-16 18:34:02 +00:00
|
|
|
uint32_t drm_format, uint32_t *flags, uint32_t stride,
|
2018-06-23 13:02:43 +00:00
|
|
|
uint32_t width, uint32_t height, uint32_t src_x, uint32_t src_y,
|
|
|
|
uint32_t dst_x, uint32_t dst_y, void *data) {
|
2018-06-23 14:12:17 +00:00
|
|
|
struct wlr_gles2_renderer *renderer =
|
|
|
|
gles2_get_renderer_in_context(wlr_renderer);
|
2018-03-20 18:14:33 +00:00
|
|
|
|
2021-02-16 18:17:18 +00:00
|
|
|
const struct wlr_gles2_pixel_format *fmt =
|
2021-02-16 18:34:02 +00:00
|
|
|
get_gles2_format_from_drm(drm_format);
|
2021-07-30 03:53:22 +00:00
|
|
|
if (fmt == NULL || !is_gles2_pixel_format_supported(renderer, fmt)) {
|
|
|
|
wlr_log(WLR_ERROR, "Cannot read pixels: unsupported pixel format 0x%"PRIX32, drm_format);
|
2018-10-31 16:20:27 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-05 08:16:59 +00:00
|
|
|
if (fmt->gl_format == GL_BGRA_EXT && !renderer->exts.EXT_read_format_bgra) {
|
|
|
|
wlr_log(WLR_ERROR,
|
|
|
|
"Cannot read pixels: missing GL_EXT_read_format_bgra extension");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-03-24 14:10:52 +00:00
|
|
|
const struct wlr_pixel_format_info *drm_fmt =
|
|
|
|
drm_get_pixel_format_info(fmt->drm_format);
|
|
|
|
assert(drm_fmt);
|
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
push_gles2_debug(renderer);
|
2018-03-20 18:14:33 +00:00
|
|
|
|
2018-01-23 21:06:54 +00:00
|
|
|
// Make sure any pending drawing is finished before we try to read it
|
|
|
|
glFinish();
|
|
|
|
|
2018-06-23 14:12:17 +00:00
|
|
|
glGetError(); // Clear the error flag
|
|
|
|
|
2019-02-28 22:15:34 +00:00
|
|
|
unsigned char *p = (unsigned char *)data + dst_y * stride;
|
2021-03-24 14:10:52 +00:00
|
|
|
uint32_t pack_stride = width * drm_fmt->bpp / 8;
|
2021-02-16 15:19:19 +00:00
|
|
|
if (pack_stride == stride && dst_x == 0) {
|
2018-06-23 13:02:43 +00:00
|
|
|
// Under these particular conditions, we can read the pixels with only
|
|
|
|
// one glReadPixels call
|
2020-06-11 16:40:29 +00:00
|
|
|
|
2021-02-16 15:19:19 +00:00
|
|
|
glReadPixels(src_x, src_y, width, height, fmt->gl_format, fmt->gl_type, p);
|
2018-06-23 13:02:43 +00:00
|
|
|
} else {
|
|
|
|
// Unfortunately GLES2 doesn't support GL_PACK_*, so we have to read
|
|
|
|
// the lines out row by row
|
2019-08-31 20:00:52 +00:00
|
|
|
for (size_t i = 0; i < height; ++i) {
|
2020-06-11 16:40:29 +00:00
|
|
|
uint32_t y = src_y + i;
|
|
|
|
glReadPixels(src_x, y, width, 1, fmt->gl_format,
|
2021-03-24 14:10:52 +00:00
|
|
|
fmt->gl_type, p + i * stride + dst_x * drm_fmt->bpp / 8);
|
2018-06-23 13:02:43 +00:00
|
|
|
}
|
2017-10-08 00:11:56 +00:00
|
|
|
}
|
2018-01-23 21:06:54 +00:00
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
pop_gles2_debug(renderer);
|
2018-03-20 18:14:33 +00:00
|
|
|
|
2021-02-16 15:19:19 +00:00
|
|
|
if (flags != NULL) {
|
|
|
|
*flags = 0;
|
|
|
|
}
|
|
|
|
|
2018-06-30 22:19:02 +00:00
|
|
|
return glGetError() == GL_NO_ERROR;
|
2017-10-08 00:11:56 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 12:47:12 +00:00
|
|
|
static int gles2_get_drm_fd(struct wlr_renderer *wlr_renderer) {
|
|
|
|
struct wlr_gles2_renderer *renderer =
|
|
|
|
gles2_get_renderer(wlr_renderer);
|
|
|
|
|
|
|
|
if (renderer->drm_fd < 0) {
|
|
|
|
renderer->drm_fd = wlr_egl_dup_drm_fd(renderer->egl);
|
|
|
|
}
|
|
|
|
|
|
|
|
return renderer->drm_fd;
|
|
|
|
}
|
|
|
|
|
2021-06-08 16:57:15 +00:00
|
|
|
static uint32_t gles2_get_render_buffer_caps(struct wlr_renderer *wlr_renderer) {
|
2021-04-23 17:51:05 +00:00
|
|
|
return WLR_BUFFER_CAP_DMABUF;
|
|
|
|
}
|
|
|
|
|
2019-11-07 18:38:45 +00:00
|
|
|
struct wlr_egl *wlr_gles2_renderer_get_egl(struct wlr_renderer *wlr_renderer) {
|
|
|
|
struct wlr_gles2_renderer *renderer =
|
|
|
|
gles2_get_renderer(wlr_renderer);
|
|
|
|
return renderer->egl;
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:14:33 +00:00
|
|
|
static void gles2_destroy(struct wlr_renderer *wlr_renderer) {
|
|
|
|
struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer);
|
|
|
|
|
2021-01-09 11:13:35 +00:00
|
|
|
wlr_egl_make_current(renderer->egl);
|
2018-03-20 18:14:33 +00:00
|
|
|
|
2020-07-28 14:56:23 +00:00
|
|
|
struct wlr_gles2_buffer *buffer, *buffer_tmp;
|
|
|
|
wl_list_for_each_safe(buffer, buffer_tmp, &renderer->buffers, link) {
|
|
|
|
destroy_buffer(buffer);
|
|
|
|
}
|
|
|
|
|
2021-04-21 10:02:16 +00:00
|
|
|
struct wlr_gles2_texture *tex, *tex_tmp;
|
|
|
|
wl_list_for_each_safe(tex, tex_tmp, &renderer->textures, link) {
|
2021-05-29 08:25:25 +00:00
|
|
|
gles2_texture_destroy(tex);
|
2021-04-21 10:02:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
push_gles2_debug(renderer);
|
2018-06-02 22:20:09 +00:00
|
|
|
glDeleteProgram(renderer->shaders.quad.program);
|
|
|
|
glDeleteProgram(renderer->shaders.tex_rgba.program);
|
|
|
|
glDeleteProgram(renderer->shaders.tex_rgbx.program);
|
|
|
|
glDeleteProgram(renderer->shaders.tex_ext.program);
|
2020-07-28 11:58:07 +00:00
|
|
|
pop_gles2_debug(renderer);
|
2018-03-20 18:14:33 +00:00
|
|
|
|
2021-07-11 18:00:02 +00:00
|
|
|
if (renderer->exts.KHR_debug) {
|
2018-03-20 18:14:33 +00:00
|
|
|
glDisable(GL_DEBUG_OUTPUT_KHR);
|
2020-07-28 12:02:08 +00:00
|
|
|
renderer->procs.glDebugMessageCallbackKHR(NULL, NULL);
|
2018-03-20 18:14:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 09:54:59 +00:00
|
|
|
wlr_egl_unset_current(renderer->egl);
|
2021-01-09 11:00:31 +00:00
|
|
|
wlr_egl_destroy(renderer->egl);
|
2021-01-12 09:45:14 +00:00
|
|
|
|
2020-06-10 12:47:12 +00:00
|
|
|
if (renderer->drm_fd >= 0) {
|
|
|
|
close(renderer->drm_fd);
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:14:33 +00:00
|
|
|
free(renderer);
|
2017-08-11 02:15:37 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 18:14:33 +00:00
|
|
|
static const struct wlr_renderer_impl renderer_impl = {
|
|
|
|
.destroy = gles2_destroy,
|
2020-07-28 14:56:23 +00:00
|
|
|
.bind_buffer = gles2_bind_buffer,
|
2018-03-20 18:14:33 +00:00
|
|
|
.begin = gles2_begin,
|
|
|
|
.end = gles2_end,
|
|
|
|
.clear = gles2_clear,
|
|
|
|
.scissor = gles2_scissor,
|
2020-04-27 10:27:01 +00:00
|
|
|
.render_subtexture_with_matrix = gles2_render_subtexture_with_matrix,
|
2018-03-26 16:41:51 +00:00
|
|
|
.render_quad_with_matrix = gles2_render_quad_with_matrix,
|
2020-11-18 13:57:53 +00:00
|
|
|
.get_shm_texture_formats = gles2_get_shm_texture_formats,
|
2020-11-18 13:53:13 +00:00
|
|
|
.get_dmabuf_texture_formats = gles2_get_dmabuf_texture_formats,
|
2021-04-15 14:22:06 +00:00
|
|
|
.get_render_formats = gles2_get_render_formats,
|
2018-10-31 16:20:27 +00:00
|
|
|
.preferred_read_format = gles2_preferred_read_format,
|
2018-03-20 18:14:33 +00:00
|
|
|
.read_pixels = gles2_read_pixels,
|
2020-06-10 12:47:12 +00:00
|
|
|
.get_drm_fd = gles2_get_drm_fd,
|
2021-04-23 17:51:05 +00:00
|
|
|
.get_render_buffer_caps = gles2_get_render_buffer_caps,
|
2021-04-12 09:58:31 +00:00
|
|
|
.texture_from_buffer = gles2_texture_from_buffer,
|
2017-06-08 19:52:42 +00:00
|
|
|
};
|
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
void push_gles2_debug_(struct wlr_gles2_renderer *renderer,
|
|
|
|
const char *file, const char *func) {
|
2020-07-28 12:02:08 +00:00
|
|
|
if (!renderer->procs.glPushDebugGroupKHR) {
|
2018-03-20 18:14:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int len = snprintf(NULL, 0, "%s:%s", file, func) + 1;
|
|
|
|
char str[len];
|
|
|
|
snprintf(str, len, "%s:%s", file, func);
|
2020-07-28 12:02:08 +00:00
|
|
|
renderer->procs.glPushDebugGroupKHR(GL_DEBUG_SOURCE_APPLICATION_KHR, 1, -1, str);
|
2018-03-20 18:14:33 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
void pop_gles2_debug(struct wlr_gles2_renderer *renderer) {
|
2020-07-28 12:02:08 +00:00
|
|
|
if (renderer->procs.glPopDebugGroupKHR) {
|
|
|
|
renderer->procs.glPopDebugGroupKHR();
|
2018-03-20 18:14:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-09 21:49:54 +00:00
|
|
|
static enum wlr_log_importance gles2_log_importance_to_wlr(GLenum type) {
|
2018-03-20 18:14:33 +00:00
|
|
|
switch (type) {
|
2018-07-09 21:49:54 +00:00
|
|
|
case GL_DEBUG_TYPE_ERROR_KHR: return WLR_ERROR;
|
|
|
|
case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_KHR: return WLR_DEBUG;
|
|
|
|
case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_KHR: return WLR_ERROR;
|
|
|
|
case GL_DEBUG_TYPE_PORTABILITY_KHR: return WLR_DEBUG;
|
|
|
|
case GL_DEBUG_TYPE_PERFORMANCE_KHR: return WLR_DEBUG;
|
|
|
|
case GL_DEBUG_TYPE_OTHER_KHR: return WLR_DEBUG;
|
|
|
|
case GL_DEBUG_TYPE_MARKER_KHR: return WLR_DEBUG;
|
|
|
|
case GL_DEBUG_TYPE_PUSH_GROUP_KHR: return WLR_DEBUG;
|
|
|
|
case GL_DEBUG_TYPE_POP_GROUP_KHR: return WLR_DEBUG;
|
|
|
|
default: return WLR_DEBUG;
|
2018-03-20 18:14:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gles2_log(GLenum src, GLenum type, GLuint id, GLenum severity,
|
|
|
|
GLsizei len, const GLchar *msg, const void *user) {
|
|
|
|
_wlr_log(gles2_log_importance_to_wlr(type), "[GLES2] %s", msg);
|
|
|
|
}
|
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
static GLuint compile_shader(struct wlr_gles2_renderer *renderer,
|
|
|
|
GLuint type, const GLchar *src) {
|
|
|
|
push_gles2_debug(renderer);
|
2018-03-20 18:14:33 +00:00
|
|
|
|
|
|
|
GLuint shader = glCreateShader(type);
|
|
|
|
glShaderSource(shader, 1, &src, NULL);
|
|
|
|
glCompileShader(shader);
|
|
|
|
|
|
|
|
GLint ok;
|
|
|
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &ok);
|
|
|
|
if (ok == GL_FALSE) {
|
|
|
|
glDeleteShader(shader);
|
|
|
|
shader = 0;
|
|
|
|
}
|
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
pop_gles2_debug(renderer);
|
2018-03-20 18:14:33 +00:00
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
static GLuint link_program(struct wlr_gles2_renderer *renderer,
|
|
|
|
const GLchar *vert_src, const GLchar *frag_src) {
|
|
|
|
push_gles2_debug(renderer);
|
2018-03-20 18:14:33 +00:00
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
GLuint vert = compile_shader(renderer, GL_VERTEX_SHADER, vert_src);
|
2018-03-20 18:14:33 +00:00
|
|
|
if (!vert) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
GLuint frag = compile_shader(renderer, GL_FRAGMENT_SHADER, frag_src);
|
2018-03-20 18:14:33 +00:00
|
|
|
if (!frag) {
|
|
|
|
glDeleteShader(vert);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint prog = glCreateProgram();
|
|
|
|
glAttachShader(prog, vert);
|
|
|
|
glAttachShader(prog, frag);
|
|
|
|
glLinkProgram(prog);
|
|
|
|
|
|
|
|
glDetachShader(prog, vert);
|
|
|
|
glDetachShader(prog, frag);
|
|
|
|
glDeleteShader(vert);
|
|
|
|
glDeleteShader(frag);
|
|
|
|
|
|
|
|
GLint ok;
|
|
|
|
glGetProgramiv(prog, GL_LINK_STATUS, &ok);
|
|
|
|
if (ok == GL_FALSE) {
|
|
|
|
glDeleteProgram(prog);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
pop_gles2_debug(renderer);
|
2018-03-20 18:14:33 +00:00
|
|
|
return prog;
|
|
|
|
|
|
|
|
error:
|
2020-07-28 11:58:07 +00:00
|
|
|
pop_gles2_debug(renderer);
|
2018-03-20 18:14:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-31 16:20:27 +00:00
|
|
|
static bool check_gl_ext(const char *exts, const char *ext) {
|
|
|
|
size_t extlen = strlen(ext);
|
|
|
|
const char *end = exts + strlen(exts);
|
|
|
|
|
|
|
|
while (exts < end) {
|
|
|
|
if (exts[0] == ' ') {
|
|
|
|
exts++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
size_t n = strcspn(exts, " ");
|
|
|
|
if (n == extlen && strncmp(ext, exts, n) == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
exts += n;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-11-10 13:31:33 +00:00
|
|
|
static void load_gl_proc(void *proc_ptr, const char *name) {
|
|
|
|
void *proc = (void *)eglGetProcAddress(name);
|
|
|
|
if (proc == NULL) {
|
|
|
|
wlr_log(WLR_ERROR, "eglGetProcAddress(%s) failed", name);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
*(void **)proc_ptr = proc;
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:14:33 +00:00
|
|
|
extern const GLchar quad_vertex_src[];
|
|
|
|
extern const GLchar quad_fragment_src[];
|
|
|
|
extern const GLchar tex_vertex_src[];
|
|
|
|
extern const GLchar tex_fragment_src_rgba[];
|
|
|
|
extern const GLchar tex_fragment_src_rgbx[];
|
|
|
|
extern const GLchar tex_fragment_src_external[];
|
|
|
|
|
2021-04-20 18:48:22 +00:00
|
|
|
struct wlr_renderer *wlr_gles2_renderer_create_with_drm_fd(int drm_fd) {
|
2021-06-24 11:11:21 +00:00
|
|
|
struct wlr_egl *egl = wlr_egl_create_with_drm_fd(drm_fd);
|
2021-04-20 18:48:22 +00:00
|
|
|
if (egl == NULL) {
|
|
|
|
wlr_log(WLR_ERROR, "Could not initialize EGL");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wlr_renderer *renderer = wlr_gles2_renderer_create(egl);
|
|
|
|
if (!renderer) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to create GLES2 renderer");
|
|
|
|
wlr_egl_destroy(egl);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return renderer;
|
|
|
|
}
|
|
|
|
|
2018-03-31 22:49:43 +00:00
|
|
|
struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_egl *egl) {
|
2021-01-09 11:13:35 +00:00
|
|
|
if (!wlr_egl_make_current(egl)) {
|
2020-06-02 20:13:16 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-11-10 13:31:33 +00:00
|
|
|
const char *exts_str = (const char *)glGetString(GL_EXTENSIONS);
|
|
|
|
if (exts_str == NULL) {
|
|
|
|
wlr_log(WLR_ERROR, "Failed to get GL_EXTENSIONS");
|
2018-04-01 20:07:50 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:14:33 +00:00
|
|
|
struct wlr_gles2_renderer *renderer =
|
|
|
|
calloc(1, sizeof(struct wlr_gles2_renderer));
|
|
|
|
if (renderer == NULL) {
|
2017-08-15 05:56:47 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2018-03-20 18:14:33 +00:00
|
|
|
wlr_renderer_init(&renderer->wlr_renderer, &renderer_impl);
|
2017-10-21 21:20:26 +00:00
|
|
|
|
2020-07-28 14:56:23 +00:00
|
|
|
wl_list_init(&renderer->buffers);
|
2021-04-21 10:02:16 +00:00
|
|
|
wl_list_init(&renderer->textures);
|
2020-07-28 14:56:23 +00:00
|
|
|
|
2018-03-31 22:49:43 +00:00
|
|
|
renderer->egl = egl;
|
2020-04-21 12:52:47 +00:00
|
|
|
renderer->exts_str = exts_str;
|
2020-06-10 12:47:12 +00:00
|
|
|
renderer->drm_fd = -1;
|
2020-04-21 12:52:47 +00:00
|
|
|
|
2021-04-21 06:28:29 +00:00
|
|
|
wlr_log(WLR_INFO, "Creating GLES2 renderer");
|
2018-07-09 21:49:54 +00:00
|
|
|
wlr_log(WLR_INFO, "Using %s", glGetString(GL_VERSION));
|
|
|
|
wlr_log(WLR_INFO, "GL vendor: %s", glGetString(GL_VENDOR));
|
2019-05-07 16:44:05 +00:00
|
|
|
wlr_log(WLR_INFO, "GL renderer: %s", glGetString(GL_RENDERER));
|
2019-11-10 13:31:33 +00:00
|
|
|
wlr_log(WLR_INFO, "Supported GLES2 extensions: %s", exts_str);
|
2018-03-27 21:02:48 +00:00
|
|
|
|
2021-07-11 17:55:32 +00:00
|
|
|
if (!renderer->egl->exts.EXT_image_dma_buf_import) {
|
2021-04-08 20:21:50 +00:00
|
|
|
wlr_log(WLR_ERROR, "EGL_EXT_image_dma_buf_import not supported");
|
|
|
|
free(renderer);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-11-10 13:31:33 +00:00
|
|
|
if (!check_gl_ext(exts_str, "GL_EXT_texture_format_BGRA8888")) {
|
2018-10-31 16:20:27 +00:00
|
|
|
wlr_log(WLR_ERROR, "BGRA8888 format not supported by GLES2");
|
|
|
|
free(renderer);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-12-15 13:55:18 +00:00
|
|
|
if (!check_gl_ext(exts_str, "GL_EXT_unpack_subimage")) {
|
|
|
|
wlr_log(WLR_ERROR, "GL_EXT_unpack_subimage not supported");
|
|
|
|
free(renderer);
|
|
|
|
return NULL;
|
|
|
|
}
|
2018-10-31 16:20:27 +00:00
|
|
|
|
2021-07-11 18:00:02 +00:00
|
|
|
renderer->exts.EXT_read_format_bgra =
|
2019-11-10 13:31:33 +00:00
|
|
|
check_gl_ext(exts_str, "GL_EXT_read_format_bgra");
|
|
|
|
|
2021-07-30 03:55:57 +00:00
|
|
|
renderer->exts.EXT_texture_type_2_10_10_10_REV =
|
|
|
|
check_gl_ext(exts_str, "GL_EXT_texture_type_2_10_10_10_REV");
|
|
|
|
|
|
|
|
renderer->exts.OES_texture_half_float_linear =
|
|
|
|
check_gl_ext(exts_str, "GL_OES_texture_half_float_linear");
|
|
|
|
|
2019-11-10 13:31:33 +00:00
|
|
|
if (check_gl_ext(exts_str, "GL_KHR_debug")) {
|
2021-07-11 18:00:02 +00:00
|
|
|
renderer->exts.KHR_debug = true;
|
2020-07-28 12:02:08 +00:00
|
|
|
load_gl_proc(&renderer->procs.glDebugMessageCallbackKHR,
|
2019-11-10 13:31:33 +00:00
|
|
|
"glDebugMessageCallbackKHR");
|
2020-07-28 12:02:08 +00:00
|
|
|
load_gl_proc(&renderer->procs.glDebugMessageControlKHR,
|
2019-11-10 13:31:33 +00:00
|
|
|
"glDebugMessageControlKHR");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (check_gl_ext(exts_str, "GL_OES_EGL_image_external")) {
|
2021-07-11 18:00:02 +00:00
|
|
|
renderer->exts.OES_egl_image_external = true;
|
2020-07-28 12:02:08 +00:00
|
|
|
load_gl_proc(&renderer->procs.glEGLImageTargetTexture2DOES,
|
2019-11-10 13:31:33 +00:00
|
|
|
"glEGLImageTargetTexture2DOES");
|
|
|
|
}
|
2018-10-31 16:20:27 +00:00
|
|
|
|
2020-04-22 23:23:56 +00:00
|
|
|
if (check_gl_ext(exts_str, "GL_OES_EGL_image")) {
|
2021-07-11 18:00:02 +00:00
|
|
|
renderer->exts.OES_egl_image = true;
|
2020-07-28 12:02:08 +00:00
|
|
|
load_gl_proc(&renderer->procs.glEGLImageTargetRenderbufferStorageOES,
|
2020-04-22 23:23:56 +00:00
|
|
|
"glEGLImageTargetRenderbufferStorageOES");
|
|
|
|
}
|
|
|
|
|
2021-07-11 18:00:02 +00:00
|
|
|
if (renderer->exts.KHR_debug) {
|
2018-03-20 18:14:33 +00:00
|
|
|
glEnable(GL_DEBUG_OUTPUT_KHR);
|
|
|
|
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_KHR);
|
2020-07-28 12:02:08 +00:00
|
|
|
renderer->procs.glDebugMessageCallbackKHR(gles2_log, NULL);
|
2018-03-20 18:14:33 +00:00
|
|
|
|
|
|
|
// Silence unwanted message types
|
2020-07-28 12:02:08 +00:00
|
|
|
renderer->procs.glDebugMessageControlKHR(GL_DONT_CARE,
|
2019-11-10 13:31:33 +00:00
|
|
|
GL_DEBUG_TYPE_POP_GROUP_KHR, GL_DONT_CARE, 0, NULL, GL_FALSE);
|
2020-07-28 12:02:08 +00:00
|
|
|
renderer->procs.glDebugMessageControlKHR(GL_DONT_CARE,
|
2019-11-10 13:31:33 +00:00
|
|
|
GL_DEBUG_TYPE_PUSH_GROUP_KHR, GL_DONT_CARE, 0, NULL, GL_FALSE);
|
2018-03-20 18:14:33 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
push_gles2_debug(renderer);
|
2018-03-20 18:14:33 +00:00
|
|
|
|
2018-06-02 22:20:09 +00:00
|
|
|
GLuint prog;
|
|
|
|
renderer->shaders.quad.program = prog =
|
2020-07-28 11:58:07 +00:00
|
|
|
link_program(renderer, quad_vertex_src, quad_fragment_src);
|
2018-06-02 22:20:09 +00:00
|
|
|
if (!renderer->shaders.quad.program) {
|
2018-03-20 18:14:33 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2018-06-02 22:20:09 +00:00
|
|
|
renderer->shaders.quad.proj = glGetUniformLocation(prog, "proj");
|
|
|
|
renderer->shaders.quad.color = glGetUniformLocation(prog, "color");
|
2020-06-08 16:24:19 +00:00
|
|
|
renderer->shaders.quad.pos_attrib = glGetAttribLocation(prog, "pos");
|
2018-06-02 22:20:09 +00:00
|
|
|
|
|
|
|
renderer->shaders.tex_rgba.program = prog =
|
2020-07-28 11:58:07 +00:00
|
|
|
link_program(renderer, tex_vertex_src, tex_fragment_src_rgba);
|
2018-06-02 22:20:09 +00:00
|
|
|
if (!renderer->shaders.tex_rgba.program) {
|
2018-03-20 18:14:33 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2018-06-02 22:20:09 +00:00
|
|
|
renderer->shaders.tex_rgba.proj = glGetUniformLocation(prog, "proj");
|
|
|
|
renderer->shaders.tex_rgba.tex = glGetUniformLocation(prog, "tex");
|
|
|
|
renderer->shaders.tex_rgba.alpha = glGetUniformLocation(prog, "alpha");
|
2020-06-08 16:24:19 +00:00
|
|
|
renderer->shaders.tex_rgba.pos_attrib = glGetAttribLocation(prog, "pos");
|
|
|
|
renderer->shaders.tex_rgba.tex_attrib = glGetAttribLocation(prog, "texcoord");
|
2018-06-02 22:20:09 +00:00
|
|
|
|
|
|
|
renderer->shaders.tex_rgbx.program = prog =
|
2020-07-28 11:58:07 +00:00
|
|
|
link_program(renderer, tex_vertex_src, tex_fragment_src_rgbx);
|
2018-06-02 22:20:09 +00:00
|
|
|
if (!renderer->shaders.tex_rgbx.program) {
|
2018-03-20 18:14:33 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2018-06-02 22:20:09 +00:00
|
|
|
renderer->shaders.tex_rgbx.proj = glGetUniformLocation(prog, "proj");
|
|
|
|
renderer->shaders.tex_rgbx.tex = glGetUniformLocation(prog, "tex");
|
|
|
|
renderer->shaders.tex_rgbx.alpha = glGetUniformLocation(prog, "alpha");
|
2020-06-08 16:24:19 +00:00
|
|
|
renderer->shaders.tex_rgbx.pos_attrib = glGetAttribLocation(prog, "pos");
|
|
|
|
renderer->shaders.tex_rgbx.tex_attrib = glGetAttribLocation(prog, "texcoord");
|
2018-06-02 22:20:09 +00:00
|
|
|
|
2021-07-11 18:00:02 +00:00
|
|
|
if (renderer->exts.OES_egl_image_external) {
|
2018-06-02 22:20:09 +00:00
|
|
|
renderer->shaders.tex_ext.program = prog =
|
2020-07-28 11:58:07 +00:00
|
|
|
link_program(renderer, tex_vertex_src, tex_fragment_src_external);
|
2018-06-02 22:20:09 +00:00
|
|
|
if (!renderer->shaders.tex_ext.program) {
|
2018-03-20 18:14:33 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2018-06-02 22:20:09 +00:00
|
|
|
renderer->shaders.tex_ext.proj = glGetUniformLocation(prog, "proj");
|
|
|
|
renderer->shaders.tex_ext.tex = glGetUniformLocation(prog, "tex");
|
|
|
|
renderer->shaders.tex_ext.alpha = glGetUniformLocation(prog, "alpha");
|
2020-06-08 16:24:19 +00:00
|
|
|
renderer->shaders.tex_ext.pos_attrib = glGetAttribLocation(prog, "pos");
|
|
|
|
renderer->shaders.tex_ext.tex_attrib = glGetAttribLocation(prog, "texcoord");
|
2018-03-20 18:14:33 +00:00
|
|
|
}
|
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
pop_gles2_debug(renderer);
|
2017-10-21 21:20:26 +00:00
|
|
|
|
2020-05-19 09:54:59 +00:00
|
|
|
wlr_egl_unset_current(renderer->egl);
|
|
|
|
|
2017-08-14 12:37:50 +00:00
|
|
|
return &renderer->wlr_renderer;
|
2018-03-20 18:14:33 +00:00
|
|
|
|
|
|
|
error:
|
2018-06-02 22:20:09 +00:00
|
|
|
glDeleteProgram(renderer->shaders.quad.program);
|
|
|
|
glDeleteProgram(renderer->shaders.tex_rgba.program);
|
|
|
|
glDeleteProgram(renderer->shaders.tex_rgbx.program);
|
|
|
|
glDeleteProgram(renderer->shaders.tex_ext.program);
|
2018-03-20 18:14:33 +00:00
|
|
|
|
2020-07-28 11:58:07 +00:00
|
|
|
pop_gles2_debug(renderer);
|
2018-03-20 18:14:33 +00:00
|
|
|
|
2021-07-11 18:00:02 +00:00
|
|
|
if (renderer->exts.KHR_debug) {
|
2018-03-20 18:14:33 +00:00
|
|
|
glDisable(GL_DEBUG_OUTPUT_KHR);
|
2020-07-28 12:02:08 +00:00
|
|
|
renderer->procs.glDebugMessageCallbackKHR(NULL, NULL);
|
2018-03-20 18:14:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 09:54:59 +00:00
|
|
|
wlr_egl_unset_current(renderer->egl);
|
|
|
|
|
2018-03-20 18:14:33 +00:00
|
|
|
free(renderer);
|
|
|
|
return NULL;
|
2017-06-08 19:52:42 +00:00
|
|
|
}
|
2020-04-21 12:52:47 +00:00
|
|
|
|
|
|
|
bool wlr_gles2_renderer_check_ext(struct wlr_renderer *wlr_renderer,
|
|
|
|
const char *ext) {
|
|
|
|
struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer);
|
|
|
|
return check_gl_ext(renderer->exts_str, ext);
|
|
|
|
}
|
2021-05-28 05:53:24 +00:00
|
|
|
|
|
|
|
GLuint wlr_gles2_renderer_get_current_fbo(struct wlr_renderer *wlr_renderer) {
|
|
|
|
struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer);
|
|
|
|
assert(renderer->current_buffer);
|
|
|
|
return renderer->current_buffer->fbo;
|
|
|
|
}
|