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>
|
2018-03-20 18:14:33 +00:00
|
|
|
#include <stdio.h>
|
2018-02-12 20:29:23 +00:00
|
|
|
#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-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>
|
2017-06-21 16:10:07 +00:00
|
|
|
#include <wlr/util/log.h>
|
2017-11-29 21:32:55 +00:00
|
|
|
#include "glapi.h"
|
2018-03-24 22:30:28 +00:00
|
|
|
#include "render/gles2.h"
|
2017-06-08 19:52:42 +00:00
|
|
|
|
2018-03-20 18:14:33 +00:00
|
|
|
static const struct wlr_renderer_impl renderer_impl;
|
2017-06-08 19:52:42 +00:00
|
|
|
|
2018-04-01 20:07:50 +00:00
|
|
|
static struct wlr_gles2_renderer *gles2_get_renderer(
|
2018-03-20 18:14:33 +00:00
|
|
|
struct wlr_renderer *wlr_renderer) {
|
|
|
|
assert(wlr_renderer->impl == &renderer_impl);
|
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));
|
2018-03-20 18:14:33 +00:00
|
|
|
return renderer;
|
2017-06-08 19:52:42 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-04-25 22:24:58 +00:00
|
|
|
PUSH_GLES2_DEBUG;
|
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
|
|
|
|
|
|
|
// enable transparency
|
2018-03-20 18:14:33 +00:00
|
|
|
glEnable(GL_BLEND);
|
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
|
|
|
|
2018-04-25 22:24:58 +00:00
|
|
|
POP_GLES2_DEBUG;
|
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]) {
|
2018-03-24 22:30:28 +00:00
|
|
|
gles2_get_renderer_in_context(wlr_renderer);
|
2018-03-20 18:14:33 +00:00
|
|
|
|
2018-04-25 22:24:58 +00:00
|
|
|
PUSH_GLES2_DEBUG;
|
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);
|
2018-04-25 22:24:58 +00:00
|
|
|
POP_GLES2_DEBUG;
|
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
|
|
|
|
2018-04-25 22:24:58 +00:00
|
|
|
PUSH_GLES2_DEBUG;
|
2018-01-22 15:42:22 +00:00
|
|
|
if (box != NULL) {
|
2018-04-12 23:38:33 +00:00
|
|
|
struct wlr_box gl_box;
|
|
|
|
wlr_box_transform(box, WL_OUTPUT_TRANSFORM_FLIPPED_180,
|
|
|
|
renderer->viewport_width, renderer->viewport_height, &gl_box);
|
|
|
|
|
|
|
|
glScissor(gl_box.x, gl_box.y, gl_box.width, gl_box.height);
|
2018-01-22 15:42:22 +00:00
|
|
|
glEnable(GL_SCISSOR_TEST);
|
|
|
|
} else {
|
|
|
|
glDisable(GL_SCISSOR_TEST);
|
|
|
|
}
|
2018-04-25 22:24:58 +00:00
|
|
|
POP_GLES2_DEBUG;
|
2018-01-22 15:42:22 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 00:11:57 +00:00
|
|
|
static void draw_quad(void) {
|
2017-06-23 15:38:45 +00:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
2018-03-20 18:14:33 +00:00
|
|
|
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts);
|
|
|
|
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoord);
|
2017-06-23 15:38:45 +00:00
|
|
|
|
2018-03-20 18:14:33 +00:00
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
glEnableVertexAttribArray(1);
|
2017-06-23 15:38:45 +00:00
|
|
|
|
2018-03-20 18:14:33 +00:00
|
|
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
2017-06-23 15:38:45 +00:00
|
|
|
|
2018-03-20 18:14:33 +00:00
|
|
|
glDisableVertexAttribArray(0);
|
|
|
|
glDisableVertexAttribArray(1);
|
2017-06-08 19:52:42 +00:00
|
|
|
}
|
|
|
|
|
2018-03-24 22:30:28 +00:00
|
|
|
static bool gles2_render_texture_with_matrix(struct wlr_renderer *wlr_renderer,
|
|
|
|
struct wlr_texture *wlr_texture, const float matrix[static 9],
|
|
|
|
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);
|
2018-03-24 22:30:28 +00:00
|
|
|
|
2018-06-13 10:43:01 +00:00
|
|
|
struct wlr_gles2_tex_shader *shader = NULL;
|
2018-03-30 20:43:16 +00:00
|
|
|
GLenum target = 0;
|
2018-06-02 22:20:09 +00:00
|
|
|
|
2018-03-24 22:30:28 +00:00
|
|
|
switch (texture->type) {
|
|
|
|
case WLR_GLES2_TEXTURE_GLTEX:
|
|
|
|
case WLR_GLES2_TEXTURE_WL_DRM_GL:
|
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
|
|
|
target = GL_TEXTURE_2D;
|
|
|
|
break;
|
|
|
|
case WLR_GLES2_TEXTURE_WL_DRM_EXT:
|
|
|
|
case WLR_GLES2_TEXTURE_DMABUF:
|
2018-06-13 10:43:01 +00:00
|
|
|
shader = &renderer->shaders.tex_ext;
|
2018-03-24 22:30:28 +00:00
|
|
|
target = GL_TEXTURE_EXTERNAL_OES;
|
|
|
|
break;
|
2018-03-20 18:14:33 +00:00
|
|
|
}
|
|
|
|
|
2018-03-19 19:21:02 +00:00
|
|
|
// OpenGL ES 2 requires the glUniformMatrix3fv transpose parameter to be set
|
|
|
|
// to GL_FALSE
|
|
|
|
float transposition[9];
|
|
|
|
wlr_matrix_transpose(transposition, matrix);
|
|
|
|
|
2018-04-25 22:24:58 +00:00
|
|
|
PUSH_GLES2_DEBUG;
|
2018-03-24 22:30:28 +00:00
|
|
|
|
|
|
|
GLuint tex_id = texture->type == WLR_GLES2_TEXTURE_GLTEX ?
|
|
|
|
texture->gl_tex : texture->image_tex;
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
|
|
glBindTexture(target, tex_id);
|
|
|
|
|
|
|
|
glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
|
2018-06-13 10:43:01 +00:00
|
|
|
glUseProgram(shader->program);
|
2018-03-20 18:14:33 +00:00
|
|
|
|
2018-06-13 10:43:01 +00:00
|
|
|
glUniformMatrix3fv(shader->proj, 1, GL_FALSE, transposition);
|
|
|
|
glUniform1i(shader->invert_y, texture->inverted_y);
|
|
|
|
glUniform1i(shader->tex, 0);
|
|
|
|
glUniform1f(shader->alpha, alpha);
|
2018-03-24 22:30:28 +00:00
|
|
|
|
2017-06-23 15:38:45 +00:00
|
|
|
draw_quad();
|
2018-03-24 22:30:28 +00:00
|
|
|
|
2018-04-25 22:24:58 +00:00
|
|
|
POP_GLES2_DEBUG;
|
2017-06-08 19:52:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-21 22:20:09 +00:00
|
|
|
|
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
|
|
|
|
2018-03-19 19:21:02 +00:00
|
|
|
// OpenGL ES 2 requires the glUniformMatrix3fv transpose parameter to be set
|
|
|
|
// to GL_FALSE
|
|
|
|
float transposition[9];
|
|
|
|
wlr_matrix_transpose(transposition, matrix);
|
|
|
|
|
2018-04-25 22:24:58 +00:00
|
|
|
PUSH_GLES2_DEBUG;
|
2018-06-02 22:20:09 +00:00
|
|
|
glUseProgram(renderer->shaders.quad.program);
|
|
|
|
|
|
|
|
glUniformMatrix3fv(renderer->shaders.quad.proj, 1, GL_FALSE, transposition);
|
|
|
|
glUniform4f(renderer->shaders.quad.color, color[0], color[1], color[2], color[3]);
|
2017-06-23 15:38:45 +00:00
|
|
|
draw_quad();
|
2018-04-25 22:24:58 +00:00
|
|
|
POP_GLES2_DEBUG;
|
2017-06-15 19:31:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-26 16:41:51 +00:00
|
|
|
static void gles2_render_ellipse_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
|
|
|
|
2018-03-19 19:21:02 +00:00
|
|
|
// OpenGL ES 2 requires the glUniformMatrix3fv transpose parameter to be set
|
|
|
|
// to GL_FALSE
|
|
|
|
float transposition[9];
|
|
|
|
wlr_matrix_transpose(transposition, matrix);
|
|
|
|
|
2018-04-25 22:24:58 +00:00
|
|
|
PUSH_GLES2_DEBUG;
|
2018-06-02 22:20:09 +00:00
|
|
|
glUseProgram(renderer->shaders.ellipse.program);
|
|
|
|
|
|
|
|
glUniformMatrix3fv(renderer->shaders.ellipse.proj, 1, GL_FALSE, transposition);
|
|
|
|
glUniform4f(renderer->shaders.ellipse.color, color[0], color[1], color[2], color[3]);
|
2017-06-23 15:38:45 +00:00
|
|
|
draw_quad();
|
2018-04-25 22:24:58 +00:00
|
|
|
POP_GLES2_DEBUG;
|
2017-06-15 19:31:13 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 07:50:59 +00:00
|
|
|
static const enum wl_shm_format *gles2_renderer_formats(
|
|
|
|
struct wlr_renderer *wlr_renderer, size_t *len) {
|
2018-04-25 22:24:58 +00:00
|
|
|
return get_gles2_formats(len);
|
2017-06-22 20:32:47 +00:00
|
|
|
}
|
|
|
|
|
2018-03-24 22:30:28 +00:00
|
|
|
static bool gles2_resource_is_wl_drm_buffer(struct wlr_renderer *wlr_renderer,
|
|
|
|
struct wl_resource *resource) {
|
|
|
|
struct wlr_gles2_renderer *renderer =
|
2018-08-02 11:38:00 +00:00
|
|
|
gles2_get_renderer(wlr_renderer);
|
2018-03-24 22:30:28 +00:00
|
|
|
|
|
|
|
if (!eglQueryWaylandBufferWL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
EGLint fmt;
|
|
|
|
return eglQueryWaylandBufferWL(renderer->egl->display, resource,
|
|
|
|
EGL_TEXTURE_FORMAT, &fmt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gles2_wl_drm_buffer_get_size(struct wlr_renderer *wlr_renderer,
|
|
|
|
struct wl_resource *buffer, int *width, int *height) {
|
|
|
|
struct wlr_gles2_renderer *renderer =
|
2018-08-02 11:38:00 +00:00
|
|
|
gles2_get_renderer(wlr_renderer);
|
2018-03-24 22:30:28 +00:00
|
|
|
|
|
|
|
if (!eglQueryWaylandBufferWL) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-20 18:14:33 +00:00
|
|
|
|
2018-03-24 22:30:28 +00:00
|
|
|
eglQueryWaylandBufferWL(renderer->egl->display, buffer, EGL_WIDTH, width);
|
|
|
|
eglQueryWaylandBufferWL(renderer->egl->display, buffer, EGL_HEIGHT, height);
|
2017-10-08 00:11:56 +00:00
|
|
|
}
|
|
|
|
|
2018-04-08 15:00:56 +00:00
|
|
|
static int gles2_get_dmabuf_formats(struct wlr_renderer *wlr_renderer,
|
|
|
|
int **formats) {
|
|
|
|
struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer);
|
|
|
|
return wlr_egl_get_dmabuf_formats(renderer->egl, formats);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int gles2_get_dmabuf_modifiers(struct wlr_renderer *wlr_renderer,
|
|
|
|
int format, uint64_t **modifiers) {
|
|
|
|
struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer);
|
|
|
|
return wlr_egl_get_dmabuf_modifiers(renderer->egl, format, modifiers);
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:14:33 +00:00
|
|
|
static bool gles2_read_pixels(struct wlr_renderer *wlr_renderer,
|
2018-06-23 13:02:43 +00:00
|
|
|
enum wl_shm_format wl_fmt, uint32_t *flags, 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) {
|
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
|
|
|
|
2018-04-25 22:24:58 +00:00
|
|
|
const struct wlr_gles2_pixel_format *fmt = get_gles2_format_from_wl(wl_fmt);
|
2018-01-23 21:06:54 +00:00
|
|
|
if (fmt == NULL) {
|
2018-07-09 21:49:54 +00:00
|
|
|
wlr_log(WLR_ERROR, "Cannot read pixels: unsupported pixel format");
|
2018-01-23 21:06:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-25 22:24:58 +00:00
|
|
|
PUSH_GLES2_DEBUG;
|
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
|
|
|
|
|
2018-01-23 21:06:54 +00:00
|
|
|
unsigned char *p = data + dst_y * stride;
|
2018-06-23 13:02:43 +00:00
|
|
|
uint32_t pack_stride = width * fmt->bpp / 8;
|
|
|
|
if (pack_stride == stride && dst_x == 0 && flags != NULL) {
|
|
|
|
// Under these particular conditions, we can read the pixels with only
|
|
|
|
// one glReadPixels call
|
2018-06-23 14:12:17 +00:00
|
|
|
glReadPixels(src_x, renderer->viewport_height - height - src_y,
|
|
|
|
width, height, fmt->gl_format, fmt->gl_type, p);
|
2018-06-23 13:02:43 +00:00
|
|
|
*flags = WLR_RENDERER_READ_PIXELS_Y_INVERT;
|
|
|
|
} else {
|
|
|
|
// Unfortunately GLES2 doesn't support GL_PACK_*, so we have to read
|
|
|
|
// the lines out row by row
|
|
|
|
for (size_t i = src_y; i < src_y + height; ++i) {
|
|
|
|
glReadPixels(src_x, src_y + height - i - 1, width, 1, fmt->gl_format,
|
|
|
|
fmt->gl_type, p + i * stride + dst_x * fmt->bpp / 8);
|
|
|
|
}
|
|
|
|
if (flags != NULL) {
|
|
|
|
*flags = 0;
|
|
|
|
}
|
2017-10-08 00:11:56 +00:00
|
|
|
}
|
2018-01-23 21:06:54 +00:00
|
|
|
|
2018-04-25 22:24:58 +00:00
|
|
|
POP_GLES2_DEBUG;
|
2018-03-20 18:14:33 +00:00
|
|
|
|
2018-06-30 22:19:02 +00:00
|
|
|
return glGetError() == GL_NO_ERROR;
|
2017-10-08 00:11:56 +00:00
|
|
|
}
|
|
|
|
|
2018-04-01 20:07:50 +00:00
|
|
|
static bool gles2_format_supported(struct wlr_renderer *wlr_renderer,
|
2018-01-23 21:06:54 +00:00
|
|
|
enum wl_shm_format wl_fmt) {
|
2018-04-25 22:24:58 +00:00
|
|
|
return get_gles2_format_from_wl(wl_fmt) != NULL;
|
2018-03-20 18:14:33 +00:00
|
|
|
}
|
|
|
|
|
2018-04-01 20:07:50 +00:00
|
|
|
static struct wlr_texture *gles2_texture_from_pixels(
|
|
|
|
struct wlr_renderer *wlr_renderer, enum wl_shm_format wl_fmt,
|
|
|
|
uint32_t stride, uint32_t width, uint32_t height, const void *data) {
|
|
|
|
struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer);
|
|
|
|
return wlr_gles2_texture_from_pixels(renderer->egl, wl_fmt, stride, width,
|
|
|
|
height, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct wlr_texture *gles2_texture_from_wl_drm(
|
|
|
|
struct wlr_renderer *wlr_renderer, struct wl_resource *data) {
|
|
|
|
struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer);
|
|
|
|
return wlr_gles2_texture_from_wl_drm(renderer->egl, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct wlr_texture *gles2_texture_from_dmabuf(
|
|
|
|
struct wlr_renderer *wlr_renderer,
|
2018-05-29 21:38:00 +00:00
|
|
|
struct wlr_dmabuf_attributes *attribs) {
|
2018-04-01 20:07:50 +00:00
|
|
|
struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer);
|
|
|
|
return wlr_gles2_texture_from_dmabuf(renderer->egl, attribs);
|
|
|
|
}
|
|
|
|
|
2018-05-21 18:07:08 +00:00
|
|
|
static void gles2_init_wl_display(struct wlr_renderer *wlr_renderer,
|
|
|
|
struct wl_display *wl_display) {
|
|
|
|
struct wlr_gles2_renderer *renderer =
|
2018-08-02 11:38:00 +00:00
|
|
|
gles2_get_renderer(wlr_renderer);
|
2018-05-21 18:07:08 +00:00
|
|
|
if (!wlr_egl_bind_display(renderer->egl, wl_display)) {
|
2018-07-09 21:49:54 +00:00
|
|
|
wlr_log(WLR_INFO, "failed to bind wl_display to EGL");
|
2018-05-21 18:07:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
wlr_egl_make_current(renderer->egl, EGL_NO_SURFACE, NULL);
|
|
|
|
|
2018-04-25 22:24:58 +00:00
|
|
|
PUSH_GLES2_DEBUG;
|
2018-06-02 22:20:09 +00:00
|
|
|
glDeleteProgram(renderer->shaders.quad.program);
|
|
|
|
glDeleteProgram(renderer->shaders.ellipse.program);
|
|
|
|
glDeleteProgram(renderer->shaders.tex_rgba.program);
|
|
|
|
glDeleteProgram(renderer->shaders.tex_rgbx.program);
|
|
|
|
glDeleteProgram(renderer->shaders.tex_ext.program);
|
2018-04-25 22:24:58 +00:00
|
|
|
POP_GLES2_DEBUG;
|
2018-03-20 18:14:33 +00:00
|
|
|
|
|
|
|
if (glDebugMessageCallbackKHR) {
|
|
|
|
glDisable(GL_DEBUG_OUTPUT_KHR);
|
|
|
|
glDebugMessageCallbackKHR(NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
.begin = gles2_begin,
|
|
|
|
.end = gles2_end,
|
|
|
|
.clear = gles2_clear,
|
|
|
|
.scissor = gles2_scissor,
|
|
|
|
.render_texture_with_matrix = gles2_render_texture_with_matrix,
|
2018-03-26 16:41:51 +00:00
|
|
|
.render_quad_with_matrix = gles2_render_quad_with_matrix,
|
|
|
|
.render_ellipse_with_matrix = gles2_render_ellipse_with_matrix,
|
2018-03-21 07:50:59 +00:00
|
|
|
.formats = gles2_renderer_formats,
|
2018-03-24 22:30:28 +00:00
|
|
|
.resource_is_wl_drm_buffer = gles2_resource_is_wl_drm_buffer,
|
|
|
|
.wl_drm_buffer_get_size = gles2_wl_drm_buffer_get_size,
|
2018-04-08 15:00:56 +00:00
|
|
|
.get_dmabuf_formats = gles2_get_dmabuf_formats,
|
|
|
|
.get_dmabuf_modifiers = gles2_get_dmabuf_modifiers,
|
2018-03-20 18:14:33 +00:00
|
|
|
.read_pixels = gles2_read_pixels,
|
|
|
|
.format_supported = gles2_format_supported,
|
2018-03-24 22:30:28 +00:00
|
|
|
.texture_from_pixels = gles2_texture_from_pixels,
|
|
|
|
.texture_from_wl_drm = gles2_texture_from_wl_drm,
|
|
|
|
.texture_from_dmabuf = gles2_texture_from_dmabuf,
|
2018-05-21 18:07:08 +00:00
|
|
|
.init_wl_display = gles2_init_wl_display,
|
2017-06-08 19:52:42 +00:00
|
|
|
};
|
|
|
|
|
2018-04-25 22:24:58 +00:00
|
|
|
void push_gles2_marker(const char *file, const char *func) {
|
2018-03-20 18:14:33 +00:00
|
|
|
if (!glPushDebugGroupKHR) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int len = snprintf(NULL, 0, "%s:%s", file, func) + 1;
|
|
|
|
char str[len];
|
|
|
|
snprintf(str, len, "%s:%s", file, func);
|
|
|
|
glPushDebugGroupKHR(GL_DEBUG_SOURCE_APPLICATION_KHR, 1, -1, str);
|
|
|
|
}
|
|
|
|
|
2018-04-25 22:24:58 +00:00
|
|
|
void pop_gles2_marker(void) {
|
2018-03-20 18:14:33 +00:00
|
|
|
if (glPopDebugGroupKHR) {
|
|
|
|
glPopDebugGroupKHR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static GLuint compile_shader(GLuint type, const GLchar *src) {
|
2018-04-25 22:24:58 +00:00
|
|
|
PUSH_GLES2_DEBUG;
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-25 22:24:58 +00:00
|
|
|
POP_GLES2_DEBUG;
|
2018-03-20 18:14:33 +00:00
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GLuint link_program(const GLchar *vert_src, const GLchar *frag_src) {
|
2018-04-25 22:24:58 +00:00
|
|
|
PUSH_GLES2_DEBUG;
|
2018-03-20 18:14:33 +00:00
|
|
|
|
|
|
|
GLuint vert = compile_shader(GL_VERTEX_SHADER, vert_src);
|
|
|
|
if (!vert) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint frag = compile_shader(GL_FRAGMENT_SHADER, frag_src);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-25 22:24:58 +00:00
|
|
|
POP_GLES2_DEBUG;
|
2018-03-20 18:14:33 +00:00
|
|
|
return prog;
|
|
|
|
|
|
|
|
error:
|
2018-04-25 22:24:58 +00:00
|
|
|
POP_GLES2_DEBUG;
|
2018-03-20 18:14:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern const GLchar quad_vertex_src[];
|
|
|
|
extern const GLchar quad_fragment_src[];
|
|
|
|
extern const GLchar ellipse_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[];
|
|
|
|
|
2018-03-31 22:49:43 +00:00
|
|
|
struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_egl *egl) {
|
2018-04-01 20:07:50 +00:00
|
|
|
if (!load_glapi()) {
|
|
|
|
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
|
|
|
|
2018-03-31 22:49:43 +00:00
|
|
|
renderer->egl = egl;
|
2018-03-20 18:14:33 +00:00
|
|
|
wlr_egl_make_current(renderer->egl, EGL_NO_SURFACE, NULL);
|
|
|
|
|
2018-03-27 21:02:48 +00:00
|
|
|
renderer->exts_str = (const char*) glGetString(GL_EXTENSIONS);
|
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));
|
|
|
|
wlr_log(WLR_INFO, "Supported GLES2 extensions: %s", renderer->exts_str);
|
2018-03-27 21:02:48 +00:00
|
|
|
|
2018-03-20 18:14:33 +00:00
|
|
|
if (glDebugMessageCallbackKHR && glDebugMessageControlKHR) {
|
|
|
|
glEnable(GL_DEBUG_OUTPUT_KHR);
|
|
|
|
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_KHR);
|
|
|
|
glDebugMessageCallbackKHR(gles2_log, NULL);
|
|
|
|
|
|
|
|
// Silence unwanted message types
|
|
|
|
glDebugMessageControlKHR(GL_DONT_CARE, GL_DEBUG_TYPE_POP_GROUP_KHR,
|
|
|
|
GL_DONT_CARE, 0, NULL, GL_FALSE);
|
|
|
|
glDebugMessageControlKHR(GL_DONT_CARE, GL_DEBUG_TYPE_PUSH_GROUP_KHR,
|
|
|
|
GL_DONT_CARE, 0, NULL, GL_FALSE);
|
|
|
|
}
|
|
|
|
|
2018-04-25 22:24:58 +00:00
|
|
|
PUSH_GLES2_DEBUG;
|
2018-03-20 18:14:33 +00:00
|
|
|
|
2018-06-02 22:20:09 +00:00
|
|
|
GLuint prog;
|
|
|
|
renderer->shaders.quad.program = prog =
|
|
|
|
link_program(quad_vertex_src, quad_fragment_src);
|
|
|
|
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");
|
|
|
|
|
|
|
|
renderer->shaders.ellipse.program = prog =
|
2018-03-20 18:14:33 +00:00
|
|
|
link_program(quad_vertex_src, ellipse_fragment_src);
|
2018-06-02 22:20:09 +00:00
|
|
|
if (!renderer->shaders.ellipse.program) {
|
2018-03-20 18:14:33 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2018-06-02 22:20:09 +00:00
|
|
|
renderer->shaders.ellipse.proj = glGetUniformLocation(prog, "proj");
|
|
|
|
renderer->shaders.ellipse.color = glGetUniformLocation(prog, "color");
|
|
|
|
|
|
|
|
renderer->shaders.tex_rgba.program = prog =
|
2018-03-20 18:14:33 +00:00
|
|
|
link_program(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.invert_y = glGetUniformLocation(prog, "invert_y");
|
|
|
|
renderer->shaders.tex_rgba.tex = glGetUniformLocation(prog, "tex");
|
|
|
|
renderer->shaders.tex_rgba.alpha = glGetUniformLocation(prog, "alpha");
|
|
|
|
|
|
|
|
renderer->shaders.tex_rgbx.program = prog =
|
2018-03-20 18:14:33 +00:00
|
|
|
link_program(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.invert_y = glGetUniformLocation(prog, "invert_y");
|
|
|
|
renderer->shaders.tex_rgbx.tex = glGetUniformLocation(prog, "tex");
|
|
|
|
renderer->shaders.tex_rgbx.alpha = glGetUniformLocation(prog, "alpha");
|
|
|
|
|
2018-03-20 18:14:33 +00:00
|
|
|
if (glEGLImageTargetTexture2DOES) {
|
2018-06-02 22:20:09 +00:00
|
|
|
renderer->shaders.tex_ext.program = prog =
|
2018-03-20 18:14:33 +00:00
|
|
|
link_program(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.invert_y = glGetUniformLocation(prog, "invert_y");
|
|
|
|
renderer->shaders.tex_ext.tex = glGetUniformLocation(prog, "tex");
|
|
|
|
renderer->shaders.tex_ext.alpha = glGetUniformLocation(prog, "alpha");
|
2018-03-20 18:14:33 +00:00
|
|
|
}
|
|
|
|
|
2018-04-25 22:24:58 +00:00
|
|
|
POP_GLES2_DEBUG;
|
2017-10-21 21:20:26 +00:00
|
|
|
|
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.ellipse.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
|
|
|
|
2018-04-25 22:24:58 +00:00
|
|
|
POP_GLES2_DEBUG;
|
2018-03-20 18:14:33 +00:00
|
|
|
|
|
|
|
if (glDebugMessageCallbackKHR) {
|
|
|
|
glDisable(GL_DEBUG_OUTPUT_KHR);
|
|
|
|
glDebugMessageCallbackKHR(NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(renderer);
|
|
|
|
return NULL;
|
2017-06-08 19:52:42 +00:00
|
|
|
}
|