2017-06-08 19:52:42 +00:00
|
|
|
#include <assert.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>
|
|
|
|
#include <stdlib.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-08-11 02:15:37 +00:00
|
|
|
#include <wlr/backend.h>
|
2017-06-08 19:52:42 +00:00
|
|
|
#include <wlr/render.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-15 08:11:03 +00:00
|
|
|
#include <wlr/types/wlr_matrix.h>
|
2017-06-21 16:10:07 +00:00
|
|
|
#include <wlr/util/log.h>
|
2017-06-23 15:38:45 +00:00
|
|
|
#include "render/gles2.h"
|
2017-11-29 21:32:55 +00:00
|
|
|
#include "glapi.h"
|
2017-06-08 19:52:42 +00:00
|
|
|
|
2017-06-23 18:25:55 +00:00
|
|
|
struct shaders shaders;
|
2017-06-08 19:52:42 +00:00
|
|
|
|
|
|
|
static bool compile_shader(GLuint type, const GLchar *src, GLuint *shader) {
|
2017-06-09 14:28:50 +00:00
|
|
|
*shader = GL_CALL(glCreateShader(type));
|
2017-06-08 19:52:42 +00:00
|
|
|
int len = strlen(src);
|
2017-06-09 14:28:50 +00:00
|
|
|
GL_CALL(glShaderSource(*shader, 1, &src, &len));
|
|
|
|
GL_CALL(glCompileShader(*shader));
|
2017-06-08 19:52:42 +00:00
|
|
|
GLint success;
|
2017-06-09 14:28:50 +00:00
|
|
|
GL_CALL(glGetShaderiv(*shader, GL_COMPILE_STATUS, &success));
|
2017-06-08 19:52:42 +00:00
|
|
|
if (success == GL_FALSE) {
|
|
|
|
GLint loglen;
|
2017-06-09 14:28:50 +00:00
|
|
|
GL_CALL(glGetShaderiv(*shader, GL_INFO_LOG_LENGTH, &loglen));
|
2017-06-08 19:52:42 +00:00
|
|
|
GLchar msg[loglen];
|
2017-06-09 14:28:50 +00:00
|
|
|
GL_CALL(glGetShaderInfoLog(*shader, loglen, &loglen, msg));
|
2017-06-15 19:31:13 +00:00
|
|
|
wlr_log(L_ERROR, "Shader compilation failed");
|
|
|
|
wlr_log(L_ERROR, "%s", msg);
|
2017-08-11 22:02:33 +00:00
|
|
|
glDeleteShader(*shader);
|
2017-06-08 19:52:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool compile_program(const GLchar *vert_src,
|
|
|
|
const GLchar *frag_src, GLuint *program) {
|
|
|
|
GLuint vertex, fragment;
|
|
|
|
if (!compile_shader(GL_VERTEX_SHADER, vert_src, &vertex)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!compile_shader(GL_FRAGMENT_SHADER, frag_src, &fragment)) {
|
2017-08-11 22:02:33 +00:00
|
|
|
glDeleteShader(vertex);
|
2017-06-08 19:52:42 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-06-09 14:28:50 +00:00
|
|
|
*program = GL_CALL(glCreateProgram());
|
|
|
|
GL_CALL(glAttachShader(*program, vertex));
|
|
|
|
GL_CALL(glAttachShader(*program, fragment));
|
|
|
|
GL_CALL(glLinkProgram(*program));
|
2017-08-11 22:02:33 +00:00
|
|
|
GLint success;
|
|
|
|
GL_CALL(glGetProgramiv(*program, GL_LINK_STATUS, &success));
|
|
|
|
if (success == GL_FALSE) {
|
|
|
|
GLint loglen;
|
|
|
|
GL_CALL(glGetProgramiv(*program, GL_INFO_LOG_LENGTH, &loglen));
|
|
|
|
GLchar msg[loglen];
|
|
|
|
GL_CALL(glGetProgramInfoLog(*program, loglen, &loglen, msg));
|
|
|
|
wlr_log(L_ERROR, "Program link failed");
|
|
|
|
wlr_log(L_ERROR, "%s", msg);
|
|
|
|
glDeleteProgram(*program);
|
|
|
|
glDeleteShader(vertex);
|
|
|
|
glDeleteShader(fragment);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
glDetachShader(*program, vertex);
|
|
|
|
glDetachShader(*program, fragment);
|
|
|
|
glDeleteShader(vertex);
|
|
|
|
glDeleteShader(fragment);
|
|
|
|
|
2017-06-08 19:52:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_default_shaders() {
|
|
|
|
if (shaders.initialized) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-23 18:25:55 +00:00
|
|
|
if (!compile_program(vertex_src, fragment_src_rgba, &shaders.rgba)) {
|
2017-06-08 19:52:42 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2017-06-23 18:25:55 +00:00
|
|
|
if (!compile_program(vertex_src, fragment_src_rgbx, &shaders.rgbx)) {
|
2017-06-08 19:52:42 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2017-06-15 19:31:13 +00:00
|
|
|
if (!compile_program(quad_vertex_src, quad_fragment_src, &shaders.quad)) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
if (!compile_program(quad_vertex_src, ellipse_fragment_src, &shaders.ellipse)) {
|
|
|
|
goto error;
|
|
|
|
}
|
2017-08-09 19:25:34 +00:00
|
|
|
if (glEGLImageTargetTexture2DOES) {
|
|
|
|
if (!compile_program(quad_vertex_src, fragment_src_external, &shaders.external)) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 19:29:27 +00:00
|
|
|
wlr_log(L_DEBUG, "Compiled default shaders");
|
2017-10-05 20:01:56 +00:00
|
|
|
shaders.initialized = true;
|
2017-06-08 19:52:42 +00:00
|
|
|
return;
|
|
|
|
error:
|
|
|
|
wlr_log(L_ERROR, "Failed to set up default shaders!");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_globals() {
|
|
|
|
init_default_shaders();
|
|
|
|
}
|
|
|
|
|
2018-01-22 15:42:22 +00:00
|
|
|
static void wlr_gles2_begin(struct wlr_renderer *wlr_renderer,
|
2017-06-08 19:52:42 +00:00
|
|
|
struct wlr_output *output) {
|
2018-01-18 13:36:42 +00:00
|
|
|
GL_CALL(glViewport(0, 0, output->width, output->height));
|
2017-08-10 12:36:33 +00:00
|
|
|
|
|
|
|
// enable transparency
|
|
|
|
GL_CALL(glEnable(GL_BLEND));
|
|
|
|
GL_CALL(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
|
|
|
|
|
2017-06-08 19:52:42 +00:00
|
|
|
// Note: maybe we should save output projection and remove some of the need
|
|
|
|
// for users to sling matricies themselves
|
|
|
|
}
|
|
|
|
|
2018-01-22 15:42:22 +00:00
|
|
|
static void wlr_gles2_end(struct wlr_renderer *wlr_renderer) {
|
2017-06-08 19:52:42 +00:00
|
|
|
// no-op
|
|
|
|
}
|
|
|
|
|
2018-02-03 08:32:02 +00:00
|
|
|
static void wlr_gles2_clear(struct wlr_renderer *wlr_renderer,
|
2018-03-15 10:10:56 +00:00
|
|
|
const float color[static 4]) {
|
|
|
|
glClearColor(color[0], color[1], color[2], color[3]);
|
2018-01-22 15:42:22 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wlr_gles2_scissor(struct wlr_renderer *wlr_renderer,
|
|
|
|
struct wlr_box *box) {
|
|
|
|
if (box != NULL) {
|
|
|
|
glScissor(box->x, box->y, box->width, box->height);
|
|
|
|
glEnable(GL_SCISSOR_TEST);
|
|
|
|
} else {
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-19 06:10:39 +00:00
|
|
|
static struct wlr_texture *wlr_gles2_texture_create(
|
2018-01-22 15:42:22 +00:00
|
|
|
struct wlr_renderer *wlr_renderer) {
|
2017-08-14 12:37:50 +00:00
|
|
|
struct wlr_gles2_renderer *renderer =
|
2018-01-22 15:42:22 +00:00
|
|
|
(struct wlr_gles2_renderer *)wlr_renderer;
|
2017-08-19 06:10:39 +00:00
|
|
|
return gles2_texture_create(renderer->egl);
|
2017-06-23 15:38:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_quad() {
|
|
|
|
GLfloat verts[] = {
|
|
|
|
1, 0, // top right
|
|
|
|
0, 0, // top left
|
|
|
|
1, 1, // bottom right
|
|
|
|
0, 1, // bottom left
|
|
|
|
};
|
|
|
|
GLfloat texcoord[] = {
|
|
|
|
1, 0, // top right
|
|
|
|
0, 0, // top left
|
|
|
|
1, 1, // bottom right
|
|
|
|
0, 1, // bottom left
|
|
|
|
};
|
|
|
|
|
|
|
|
GL_CALL(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts));
|
|
|
|
GL_CALL(glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoord));
|
|
|
|
|
|
|
|
GL_CALL(glEnableVertexAttribArray(0));
|
|
|
|
GL_CALL(glEnableVertexAttribArray(1));
|
|
|
|
|
|
|
|
GL_CALL(glDrawArrays(GL_TRIANGLE_STRIP, 0, 4));
|
|
|
|
|
|
|
|
GL_CALL(glDisableVertexAttribArray(0));
|
|
|
|
GL_CALL(glDisableVertexAttribArray(1));
|
2017-06-08 19:52:42 +00:00
|
|
|
}
|
|
|
|
|
2018-01-22 15:42:22 +00:00
|
|
|
static bool wlr_gles2_render_texture(struct wlr_renderer *wlr_renderer,
|
2018-03-15 10:10:56 +00:00
|
|
|
struct wlr_texture *texture, const float matrix[static 16],
|
|
|
|
float alpha) {
|
2017-08-14 15:09:56 +00:00
|
|
|
if (!texture || !texture->valid) {
|
2017-08-08 16:02:14 +00:00
|
|
|
wlr_log(L_ERROR, "attempt to render invalid texture");
|
2017-07-17 18:38:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-08-08 16:02:14 +00:00
|
|
|
wlr_texture_bind(texture);
|
2018-03-15 10:10:56 +00:00
|
|
|
GL_CALL(glUniformMatrix4fv(0, 1, GL_FALSE, matrix));
|
2018-02-21 22:20:09 +00:00
|
|
|
GL_CALL(glUniform1f(2, alpha));
|
2017-06-23 15:38:45 +00:00
|
|
|
draw_quad();
|
2017-06-08 19:52:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-21 22:20:09 +00:00
|
|
|
|
2018-01-22 15:42:22 +00:00
|
|
|
static void wlr_gles2_render_quad(struct wlr_renderer *wlr_renderer,
|
2018-03-15 10:10:56 +00:00
|
|
|
const float color[static 4], const float matrix[static 16]) {
|
2017-06-15 19:31:13 +00:00
|
|
|
GL_CALL(glUseProgram(shaders.quad));
|
2018-03-15 10:10:56 +00:00
|
|
|
GL_CALL(glUniformMatrix4fv(0, 1, GL_FALSE, matrix));
|
|
|
|
GL_CALL(glUniform4f(1, color[0], color[1], color[2], color[3]));
|
2017-06-23 15:38:45 +00:00
|
|
|
draw_quad();
|
2017-06-15 19:31:13 +00:00
|
|
|
}
|
|
|
|
|
2018-01-22 15:42:22 +00:00
|
|
|
static void wlr_gles2_render_ellipse(struct wlr_renderer *wlr_renderer,
|
2018-03-15 10:10:56 +00:00
|
|
|
const float color[static 4], const float matrix[static 16]) {
|
2017-06-15 19:31:13 +00:00
|
|
|
GL_CALL(glUseProgram(shaders.ellipse));
|
2018-03-15 10:10:56 +00:00
|
|
|
GL_CALL(glUniformMatrix4fv(0, 1, GL_TRUE, matrix));
|
|
|
|
GL_CALL(glUniform4f(1, color[0], color[1], color[2], color[3]));
|
2017-06-23 15:38:45 +00:00
|
|
|
draw_quad();
|
2017-06-15 19:31:13 +00:00
|
|
|
}
|
|
|
|
|
2017-06-22 20:32:47 +00:00
|
|
|
static const enum wl_shm_format *wlr_gles2_formats(
|
2017-08-14 12:37:50 +00:00
|
|
|
struct wlr_renderer *renderer, size_t *len) {
|
2017-06-22 20:32:47 +00:00
|
|
|
static enum wl_shm_format formats[] = {
|
|
|
|
WL_SHM_FORMAT_ARGB8888,
|
|
|
|
WL_SHM_FORMAT_XRGB8888,
|
2017-06-23 18:25:55 +00:00
|
|
|
WL_SHM_FORMAT_ABGR8888,
|
|
|
|
WL_SHM_FORMAT_XBGR8888,
|
2017-06-22 20:32:47 +00:00
|
|
|
};
|
|
|
|
*len = sizeof(formats) / sizeof(formats[0]);
|
|
|
|
return formats;
|
|
|
|
}
|
|
|
|
|
2018-01-22 15:42:22 +00:00
|
|
|
static bool wlr_gles2_buffer_is_drm(struct wlr_renderer *wlr_renderer,
|
2017-08-11 02:15:37 +00:00
|
|
|
struct wl_resource *buffer) {
|
2017-08-14 12:37:50 +00:00
|
|
|
struct wlr_gles2_renderer *renderer =
|
2018-01-22 15:42:22 +00:00
|
|
|
(struct wlr_gles2_renderer *)wlr_renderer;
|
2017-08-11 02:15:37 +00:00
|
|
|
EGLint format;
|
2017-08-14 12:37:50 +00:00
|
|
|
return wlr_egl_query_buffer(renderer->egl, buffer,
|
2017-10-08 00:11:56 +00:00
|
|
|
EGL_TEXTURE_FORMAT, &format);
|
|
|
|
}
|
|
|
|
|
2018-01-23 21:06:54 +00:00
|
|
|
static bool wlr_gles2_read_pixels(struct wlr_renderer *renderer,
|
|
|
|
enum wl_shm_format wl_fmt, uint32_t stride, uint32_t width,
|
|
|
|
uint32_t height, uint32_t src_x, uint32_t src_y, uint32_t dst_x,
|
|
|
|
uint32_t dst_y, void *data) {
|
|
|
|
const struct pixel_format *fmt = gl_format_for_wl_format(wl_fmt);
|
|
|
|
if (fmt == NULL) {
|
|
|
|
wlr_log(L_ERROR, "Cannot read pixels: unsupported pixel format");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure any pending drawing is finished before we try to read it
|
|
|
|
glFinish();
|
|
|
|
|
|
|
|
// Unfortunately GLES2 doesn't support GL_PACK_*, so we have to read
|
|
|
|
// the lines out row by row
|
|
|
|
unsigned char *p = data + dst_y * stride;
|
|
|
|
for (size_t i = src_y; i < src_y + height; ++i) {
|
|
|
|
glReadPixels(src_x, src_y + height - i - 1, width, 1, fmt->gl_format,
|
2018-01-26 22:13:41 +00:00
|
|
|
fmt->gl_type, p + i * stride + dst_x * fmt->bpp / 8);
|
2017-10-08 00:11:56 +00:00
|
|
|
}
|
2018-01-23 21:06:54 +00:00
|
|
|
|
|
|
|
return true;
|
2017-10-08 00:11:56 +00:00
|
|
|
}
|
|
|
|
|
2018-01-23 21:06:54 +00:00
|
|
|
static bool wlr_gles2_format_supported(struct wlr_renderer *r,
|
|
|
|
enum wl_shm_format wl_fmt) {
|
|
|
|
return gl_format_for_wl_format(wl_fmt);
|
2017-08-11 02:15:37 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 19:52:42 +00:00
|
|
|
static struct wlr_renderer_impl wlr_renderer_impl = {
|
2017-06-23 15:38:45 +00:00
|
|
|
.begin = wlr_gles2_begin,
|
|
|
|
.end = wlr_gles2_end,
|
2018-01-22 15:42:22 +00:00
|
|
|
.clear = wlr_gles2_clear,
|
|
|
|
.scissor = wlr_gles2_scissor,
|
2017-08-19 06:10:39 +00:00
|
|
|
.texture_create = wlr_gles2_texture_create,
|
2017-08-08 16:02:14 +00:00
|
|
|
.render_with_matrix = wlr_gles2_render_texture,
|
2017-06-23 15:38:45 +00:00
|
|
|
.render_quad = wlr_gles2_render_quad,
|
|
|
|
.render_ellipse = wlr_gles2_render_ellipse,
|
2017-06-22 20:32:47 +00:00
|
|
|
.formats = wlr_gles2_formats,
|
2017-08-11 02:15:37 +00:00
|
|
|
.buffer_is_drm = wlr_gles2_buffer_is_drm,
|
2017-10-08 00:11:56 +00:00
|
|
|
.read_pixels = wlr_gles2_read_pixels,
|
2018-01-23 21:06:54 +00:00
|
|
|
.format_supported = wlr_gles2_format_supported,
|
2017-06-08 19:52:42 +00:00
|
|
|
};
|
|
|
|
|
2017-08-19 06:10:39 +00:00
|
|
|
struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_backend *backend) {
|
2017-06-08 19:52:42 +00:00
|
|
|
init_globals();
|
2017-08-15 05:56:47 +00:00
|
|
|
struct wlr_gles2_renderer *renderer;
|
|
|
|
if (!(renderer = calloc(1, sizeof(struct wlr_gles2_renderer)))) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2017-08-14 12:37:50 +00:00
|
|
|
wlr_renderer_init(&renderer->wlr_renderer, &wlr_renderer_impl);
|
2017-10-21 21:20:26 +00:00
|
|
|
|
|
|
|
renderer->egl = wlr_backend_get_egl(backend);
|
|
|
|
|
2017-08-14 12:37:50 +00:00
|
|
|
return &renderer->wlr_renderer;
|
2017-06-08 19:52:42 +00:00
|
|
|
}
|