/* The host's rasteriser on the GPU (OpenGL ES 3): the same primitives as raster_stub.c, drawn the * same way, into a texture that stays on the GPU. * * EGL and GLES are loaded with dlopen, so the host starts (and draws on the CPU) on machines * without them, and building needs no GL headers. No window system is involved: the context is * surfaceless on a DRM render node, and finished frames go to the display's own scanout buffers * (gpu_scanout) or are read back (nested window, X11, screenshots). * * Every primitive is one instanced quad; what it looks like is decided per pixel from its integer * coordinates, with the same tests the CPU rasteriser makes, so both draw the same shapes. Quads * are cut to the clip here, so a clip costs no draw call; a batch ends only when the next image * uses another texture. */ #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include "raster_gpu.h" int gpu_on; /* ------------------------------------------------------------------ EGL and GL, as used here */ typedef void *EGLDisplay, *EGLContext, *EGLConfig, *EGLSurface, *EGLImage, *EGLDeviceEXT; typedef int32_t EGLint; typedef uint32_t GLenum, GLuint; typedef int32_t GLint, GLsizei; #define EGL_EXTENSIONS 0x3055 #define EGL_NONE 0x3038 #define EGL_WIDTH 0x3057 #define EGL_HEIGHT 0x3056 #define EGL_OPENGL_ES_API 0x30A0 #define EGL_CONTEXT_MAJOR_VERSION 0x3098 #define EGL_CONTEXT_PRIORITY_LEVEL_IMG 0x3100 #define EGL_CONTEXT_PRIORITY_HIGH_IMG 0x3101 #define EGL_PLATFORM_DEVICE_EXT 0x313F #define EGL_DRM_DEVICE_FILE_EXT 0x3233 #define EGL_DRM_RENDER_NODE_FILE_EXT 0x3377 #define EGL_LINUX_DMA_BUF_EXT 0x3270 #define EGL_LINUX_DRM_FOURCC_EXT 0x3271 #define GL_TEXTURE_2D 0x0DE1 #define GL_TEXTURE0 0x84C0 #define GL_RGBA 0x1908 #define GL_RED 0x1903 #define GL_GREEN 0x1904 #define GL_BLUE 0x1905 #define GL_ALPHA 0x1906 #define GL_ONE 1 #define GL_RGBA8 0x8058 #define GL_R8 0x8229 #define GL_UNSIGNED_BYTE 0x1401 #define GL_FLOAT 0x1406 #define GL_FRAMEBUFFER 0x8D40 #define GL_RENDERBUFFER 0x8D41 #define GL_COLOR_ATTACHMENT0 0x8CE0 #define GL_FRAMEBUFFER_COMPLETE 0x8CD5 #define GL_COLOR_BUFFER_BIT 0x4000 #define GL_BLEND 0x0BE2 #define GL_DITHER 0x0BD0 #define GL_SCISSOR_TEST 0x0C11 #define GL_ONE_MINUS_SRC_ALPHA 0x0303 #define GL_TRIANGLE_STRIP 5 #define GL_ARRAY_BUFFER 0x8892 #define GL_STREAM_DRAW 0x88E0 #define GL_PIXEL_UNPACK_BUFFER 0x88EC #define GL_MAP_WRITE_BIT 0x0002 #define GL_MAP_INVALIDATE_BUFFER_BIT 0x0008 #define GL_VERTEX_SHADER 0x8B31 #define GL_FRAGMENT_SHADER 0x8B30 #define GL_COMPILE_STATUS 0x8B81 #define GL_LINK_STATUS 0x8B82 #define GL_TEXTURE_MIN_FILTER 0x2801 #define GL_TEXTURE_MAG_FILTER 0x2800 #define GL_TEXTURE_WRAP_S 0x2802 #define GL_TEXTURE_WRAP_T 0x2803 #define GL_NEAREST 0x2600 #define GL_LINEAR 0x2601 #define GL_CLAMP_TO_EDGE 0x812F #define GL_TEXTURE_SWIZZLE_R 0x8E42 #define GL_TEXTURE_SWIZZLE_B 0x8E44 #define GL_TEXTURE_SWIZZLE_A 0x8E45 #define GL_UNPACK_ROW_LENGTH 0x0CF2 #define GL_UNPACK_ALIGNMENT 0x0CF5 #define GL_PACK_ROW_LENGTH 0x0D02 #define GL_MAX_TEXTURE_SIZE 0x0D33 #define GL_VENDOR 0x1F00 #define GL_RENDERER 0x1F01 #define GL_EXTENSIONS 0x1F03 #define EGL_FNS(F) \ F(const char *, eglQueryString, (EGLDisplay, EGLint)) \ F(uint32_t, eglInitialize, (EGLDisplay, EGLint *, EGLint *)) \ F(uint32_t, eglTerminate, (EGLDisplay)) \ F(uint32_t, eglBindAPI, (GLenum)) \ F(EGLContext, eglCreateContext, (EGLDisplay, EGLConfig, EGLContext, const EGLint *)) \ F(uint32_t, eglMakeCurrent, (EGLDisplay, EGLSurface, EGLSurface, EGLContext)) \ F(EGLint, eglGetError, (void)) \ F(EGLDisplay, eglGetPlatformDisplayEXT, (GLenum, void *, const EGLint *)) \ F(uint32_t, eglQueryDevicesEXT, (EGLint, EGLDeviceEXT *, EGLint *)) \ F(const char *, eglQueryDeviceStringEXT, (EGLDeviceEXT, EGLint)) /* Only needed for dmabufs (client buffers and scanout). */ #define EGL_OPT_FNS(F) \ F(EGLImage, eglCreateImageKHR, (EGLDisplay, EGLContext, GLenum, void *, const EGLint *)) \ F(uint32_t, eglDestroyImageKHR, (EGLDisplay, EGLImage)) \ F(uint32_t, eglQueryDmaBufFormatsEXT, (EGLDisplay, EGLint, EGLint *, EGLint *)) \ F(uint32_t, eglQueryDmaBufModifiersEXT, (EGLDisplay, EGLint, EGLint, uint64_t *, uint32_t *, EGLint *)) \ F(void, glEGLImageTargetTexture2DOES, (GLenum, EGLImage)) \ F(void, glEGLImageTargetRenderbufferStorageOES, (GLenum, EGLImage)) #define GL_FNS(F) \ F(const uint8_t *, glGetString, (GLenum)) \ F(void, glGetIntegerv, (GLenum, GLint *)) \ F(GLenum, glGetError, (void)) \ F(void, glEnable, (GLenum)) \ F(void, glDisable, (GLenum)) \ F(void, glBlendFunc, (GLenum, GLenum)) \ F(void, glViewport, (GLint, GLint, GLsizei, GLsizei)) \ F(void, glScissor, (GLint, GLint, GLsizei, GLsizei)) \ F(void, glClearColor, (float, float, float, float)) \ F(void, glClear, (GLenum)) \ F(void, glFinish, (void)) \ F(void, glFlush, (void)) \ F(void, glPixelStorei, (GLenum, GLint)) \ F(void, glReadPixels, (GLint, GLint, GLsizei, GLsizei, GLenum, GLenum, void *)) \ F(void, glGenTextures, (GLsizei, GLuint *)) \ F(void, glDeleteTextures, (GLsizei, const GLuint *)) \ F(void, glActiveTexture, (GLenum)) \ F(void, glBindTexture, (GLenum, GLuint)) \ F(void, glTexParameteri, (GLenum, GLenum, GLint)) \ F(void, glTexStorage2D, (GLenum, GLsizei, GLenum, GLsizei, GLsizei)) \ F(void, glTexSubImage2D, (GLenum, GLint, GLint, GLint, GLsizei, GLsizei, GLenum, GLenum, const void *)) \ F(void, glCopyTexSubImage2D, (GLenum, GLint, GLint, GLint, GLint, GLint, GLsizei, GLsizei)) \ F(void, glGenFramebuffers, (GLsizei, GLuint *)) \ F(void, glDeleteFramebuffers, (GLsizei, const GLuint *)) \ F(void, glBindFramebuffer, (GLenum, GLuint)) \ F(void, glFramebufferTexture2D, (GLenum, GLenum, GLenum, GLuint, GLint)) \ F(GLenum, glCheckFramebufferStatus, (GLenum)) \ F(void, glGenRenderbuffers, (GLsizei, GLuint *)) \ F(void, glDeleteRenderbuffers, (GLsizei, const GLuint *)) \ F(void, glBindRenderbuffer, (GLenum, GLuint)) \ F(void, glFramebufferRenderbuffer, (GLenum, GLenum, GLenum, GLuint)) \ F(GLuint, glCreateShader, (GLenum)) \ F(void, glShaderSource, (GLuint, GLsizei, const char *const *, const GLint *)) \ F(void, glCompileShader, (GLuint)) \ F(void, glGetShaderiv, (GLuint, GLenum, GLint *)) \ F(void, glGetShaderInfoLog, (GLuint, GLsizei, GLsizei *, char *)) \ F(GLuint, glCreateProgram, (void)) \ F(void, glAttachShader, (GLuint, GLuint)) \ F(void, glLinkProgram, (GLuint)) \ F(void, glGetProgramiv, (GLuint, GLenum, GLint *)) \ F(void, glUseProgram, (GLuint)) \ F(GLint, glGetUniformLocation, (GLuint, const char *)) \ F(void, glUniform1i, (GLint, GLint)) \ F(void, glUniform2f, (GLint, float, float)) \ F(void, glUniform4i, (GLint, GLint, GLint, GLint, GLint)) \ F(void, glGenVertexArrays, (GLsizei, GLuint *)) \ F(void, glBindVertexArray, (GLuint)) \ F(void, glGenBuffers, (GLsizei, GLuint *)) \ F(void, glBindBuffer, (GLenum, GLuint)) \ F(void, glBufferData, (GLenum, intptr_t, const void *, GLenum)) \ F(void *, glMapBufferRange, (GLenum, intptr_t, intptr_t, GLenum)) \ F(uint8_t, glUnmapBuffer, (GLenum)) \ F(void, glEnableVertexAttribArray, (GLuint)) \ F(void, glVertexAttribPointer, (GLuint, GLint, GLenum, uint8_t, GLsizei, const void *)) \ F(void, glVertexAttribDivisor, (GLuint, GLuint)) \ F(void, glDrawArrays, (GLenum, GLint, GLsizei)) \ F(void, glDrawArraysInstanced, (GLenum, GLint, GLsizei, GLsizei)) #define DECL(ret, name, args) static ret(*p_##name) args; EGL_FNS(DECL) EGL_OPT_FNS(DECL) GL_FNS(DECL) #undef DECL static EGLDisplay dpy; static EGLContext ctx; static uint64_t device; /* the render node's dev_t */ static int has_modifiers, can_import; static GLint max_tex; static int has_ext(const char *list, const char *name) { size_t n = strlen(name); for (const char *p = list; p && (p = strstr(p, name)); p += n) if ((p == list || p[-1] == ' ') && (p[n] == ' ' || p[n] == 0)) return 1; return 0; } /* ------------------------------------------------------------------ shaders */ static const char *VS = "#version 300 es\n" "precision highp float; precision highp int;\n" "uniform vec2 size;\n" "layout(location=0) in vec4 quad;\n" /* x0 y0 x1 y1, already clipped */ "layout(location=1) in vec4 a_p;\n" "layout(location=2) in vec4 a_q;\n" "layout(location=3) in vec4 a_color;\n" "flat out vec4 p; flat out vec4 q; flat out vec4 color;\n" "void main() {\n" " vec2 c = vec2((gl_VertexID & 1) == 0 ? quad.x : quad.z, (gl_VertexID & 2) == 0 ? quad.y : quad.w);\n" " p = a_p; q = a_q; color = a_color;\n" " gl_Position = vec4(c / size * 2.0 - 1.0, 0.0, 1.0);\n" "}\n"; enum { K_SOLID, K_ROUND, K_STROKE, K_DISC, K_RING, K_GLYPH, K_IMAGE, K_IMAGE_PM, K_SURFACE, K_TEXEL }; /* The frame texture's row 0 is the top row, so gl_FragCoord is the pixel as the CPU counts it. */ static const char *FS = "#version 300 es\n" "precision highp float; precision highp int; precision highp sampler2D;\n" "uniform sampler2D atlas; uniform sampler2D img;\n" "flat in vec4 p; flat in vec4 q; flat in vec4 color;\n" "out vec4 o;\n" "bool inside(ivec2 c, ivec4 b, int rad) {\n" " if (c.x < b.x || c.y < b.y || c.x >= b.x + b.z || c.y >= b.y + b.w) return false;\n" " int r = min(rad, min(b.z, b.w) / 2);\n" " if (r <= 0) return true;\n" " int dx = max(max(b.x + r - c.x, c.x - (b.x + b.z - r - 1)), 0);\n" " int dy = max(max(b.y + r - c.y, c.y - (b.y + b.w - r - 1)), 0);\n" " return dx == 0 || dy == 0 || dx * dx + dy * dy <= r * r;\n" "}\n" "void main() {\n" " ivec2 c = ivec2(gl_FragCoord.xy);\n" " int kind = int(q.w);\n" " vec4 s = color;\n" " if (kind == 0) {\n" " } else if (kind == 1) {\n" " if (!inside(c, ivec4(p), int(q.x))) discard;\n" " } else if (kind == 2) {\n" " ivec4 b = ivec4(p); int w = int(q.x);\n" " if (!inside(c, b, int(q.y))) discard;\n" " ivec4 n = ivec4(b.x + w, b.y + w, b.z - 2 * w, b.w - 2 * w);\n" " if (n.z > 0 && n.w > 0 && inside(c, n, max(int(q.y) - w, 0))) discard;\n" " } else if (kind == 3 || kind == 4) {\n" " float d = length(vec2(c - ivec2(p.xy)));\n" " float cov = min(p.z + 0.5 - d, 1.0);\n" " if (kind == 4) cov = min(cov, d - (p.z - 1.5));\n" " if (cov <= 0.02) discard;\n" " s.a *= cov;\n" " } else if (kind == 5) {\n" " s.a *= texelFetch(atlas, c - ivec2(p.xy) + ivec2(p.zw), 0).r;\n" " } else if (kind == 6) {\n" " s = texture(img, (gl_FragCoord.xy - p.xy) / p.zw);\n" " } else if (kind == 7) {\n" " o = texture(img, (gl_FragCoord.xy - p.xy) / p.zw); return;\n" " } else if (kind == 8) {\n" " o = texelFetch(img, c - ivec2(p.xy), 0); return;\n" " } else {\n" " o = texelFetch(img, ivec2(p.xy), 0); return;\n" " }\n" " o = vec4(s.rgb * s.a, s.a);\n" "}\n"; /* Copies (gl_VertexID makes the one triangle). */ static const char *VS_COPY = "#version 300 es\n" "void main() { gl_Position = vec4(gl_VertexID == 1 ? 3.0 : -1.0, gl_VertexID == 2 ? 3.0 : -1.0, 0.0, 1.0); }\n"; /* A scaled copy, the average of a 4 x 4 grid of bilinear samples per pixel; `flip` upside down. */ static const char *FS_SCALE = "#version 300 es\n" "precision highp float; precision highp sampler2D;\n" "uniform sampler2D img; uniform vec2 size; uniform int flip; uniform int one;\n" "out vec4 o;\n" "void main() {\n" " if (one != 0) {\n" " ivec2 c = ivec2(gl_FragCoord.xy);\n" " if (flip != 0) c.y = int(size.y) - 1 - c.y;\n" " o = texelFetch(img, c, 0); return;\n" " }\n" " vec4 t = vec4(0.0);\n" " for (int y = 0; y < 4; y++) for (int x = 0; x < 4; x++)\n" " t += texture(img, (floor(gl_FragCoord.xy) + (vec2(x, y) + 0.5) / 4.0) / size);\n" " o = t / 16.0;\n" "}\n"; /* The frame onto a scanout buffer: whole-number scale and quarter turns, one texel per pixel (what * kms_stub.c's copy_rows does). turn = rotation, scale, the unrotated frame's size. */ static const char *FS_SCAN = "#version 300 es\n" "precision highp float; precision highp int; precision highp sampler2D;\n" "uniform sampler2D img; uniform ivec4 turn;\n" "out vec4 o;\n" "void main() {\n" " ivec2 b = turn.zw - 1;\n" " ivec2 u = min(ivec2(gl_FragCoord.xy) / turn.y, b);\n" " ivec2 c = u;\n" " if (turn.x == 1) c = ivec2(u.y, b.x - u.x);\n" " else if (turn.x == 2) c = b - u;\n" " else if (turn.x == 3) c = ivec2(b.y - u.y, u.x);\n" " o = vec4(texelFetch(img, c, 0).rgb, 1.0);\n" "}\n"; static GLuint compile(GLenum type, const char *src) { GLuint s = p_glCreateShader(type); p_glShaderSource(s, 1, &src, NULL); p_glCompileShader(s); GLint ok = 0; p_glGetShaderiv(s, GL_COMPILE_STATUS, &ok); if (!ok) { char log[1024] = ""; p_glGetShaderInfoLog(s, sizeof log, NULL, log); fprintf(stderr, "gpu: shader: %s\n", log); } return ok ? s : 0; } static GLuint program(const char *vs, const char *fs) { GLuint v = compile(GL_VERTEX_SHADER, vs), f = compile(GL_FRAGMENT_SHADER, fs); if (!v || !f) return 0; GLuint pr = p_glCreateProgram(); p_glAttachShader(pr, v); p_glAttachShader(pr, f); p_glLinkProgram(pr); GLint ok = 0; p_glGetProgramiv(pr, GL_LINK_STATUS, &ok); if (!ok) fprintf(stderr, "gpu: shaders don't link\n"); return ok ? pr : 0; } /* ------------------------------------------------------------------ batching */ typedef struct { float quad[4], p[4], q[4]; /* q.w: the kind */ uint8_t color[4]; } Inst; #define BATCH 4096 static Inst batch[BATCH]; static int batch_n; static GLuint batch_tex; /* the image texture the batch uses, 0 for none yet */ static GLuint prog, prog_scale, prog_scan, vao, vao_empty, vbo; static GLint u_size, u_scale_size, u_scale_flip, u_scale_one, u_turn; static int fb_w, fb_h; static GLuint fb_tex, fb_fbo, back_tex, tmp_fbo; #define ATLAS 2048 static GLuint atlas_tex; static uint32_t atlas_gen = 1; static int atlas_x, atlas_y, atlas_row; static int clip_on, clip_x0, clip_y0, clip_x1, clip_y1; static GLuint tex_new(GLenum format, int w, int h, int filter) { GLuint t; p_glGenTextures(1, &t); p_glActiveTexture(GL_TEXTURE0 + 1); p_glBindTexture(GL_TEXTURE_2D, t); p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter); p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter); p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); if (format) p_glTexStorage2D(GL_TEXTURE_2D, 1, format, w, h); return t; } static int attach(GLuint fbo, GLuint tex) { p_glBindFramebuffer(GL_FRAMEBUFFER, fbo); p_glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0); return p_glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE; } void gpu_flush(void) { if (!batch_n) return; p_glBindFramebuffer(GL_FRAMEBUFFER, fb_fbo); p_glViewport(0, 0, fb_w, fb_h); p_glUseProgram(prog); p_glUniform2f(u_size, (float)fb_w, (float)fb_h); p_glActiveTexture(GL_TEXTURE0); p_glBindTexture(GL_TEXTURE_2D, atlas_tex); p_glActiveTexture(GL_TEXTURE0 + 1); p_glBindTexture(GL_TEXTURE_2D, batch_tex ? batch_tex : atlas_tex); p_glBindVertexArray(vao); p_glBindBuffer(GL_ARRAY_BUFFER, vbo); p_glBufferData(GL_ARRAY_BUFFER, (intptr_t)sizeof(Inst) * batch_n, batch, GL_STREAM_DRAW); p_glEnable(GL_BLEND); p_glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, batch_n); batch_n = 0; batch_tex = 0; } void gpu_submit(void) { gpu_flush(); p_glFlush(); } void gpu_clip(int on, int x, int y, int w, int h) { clip_on = on; clip_x0 = x; clip_y0 = y; clip_x1 = x + w; clip_y1 = y + h; } /* One quad over (x, y, w, h) cut to the frame and the clip; NULL when nothing is left of it. */ static Inst *quad(int kind, GLuint tex, int x, int y, int w, int h, int r, int g, int b, int a) { int x0 = x > 0 ? x : 0, y0 = y > 0 ? y : 0; int x1 = x + w < fb_w ? x + w : fb_w, y1 = y + h < fb_h ? y + h : fb_h; if (clip_on) { if (clip_x0 > x0) x0 = clip_x0; if (clip_y0 > y0) y0 = clip_y0; if (clip_x1 < x1) x1 = clip_x1; if (clip_y1 < y1) y1 = clip_y1; } if (x0 >= x1 || y0 >= y1) return NULL; if (batch_n == BATCH || (tex && batch_tex && tex != batch_tex)) gpu_flush(); if (tex) batch_tex = tex; Inst *i = &batch[batch_n++]; *i = (Inst){ { (float)x0, (float)y0, (float)x1, (float)y1 }, { (float)x, (float)y, (float)w, (float)h }, { 0, 0, 0, (float)kind }, { (uint8_t)r, (uint8_t)g, (uint8_t)b, (uint8_t)(a < 0 ? 0 : a > 255 ? 255 : a) } }; return i; } void gpu_fill(int x, int y, int w, int h, int r, int g, int b, int a, int radius) { if (w <= 0 || h <= 0 || a <= 0) return; Inst *i = quad(radius > 0 ? K_ROUND : K_SOLID, 0, x, y, w, h, r, g, b, a); if (i) i->q[0] = (float)radius; } void gpu_stroke(int x, int y, int w, int h, int r, int g, int b, int a, int width, int radius) { if (w <= 0 || h <= 0 || width <= 0 || a <= 0) return; Inst *i = quad(K_STROKE, 0, x, y, w, h, r, g, b, a); if (i) { i->q[0] = (float)width; i->q[1] = (float)radius; } } void gpu_circle(int cx, int cy, int radius, int r, int g, int b, int a, int filled) { if (radius <= 0 || a <= 0) return; Inst *i = quad(filled ? K_DISC : K_RING, 0, cx - radius - 1, cy - radius - 1, 2 * radius + 3, 2 * radius + 3, r, g, b, a); if (i) { i->p[0] = (float)cx; i->p[1] = (float)cy; i->p[2] = (float)radius; } } void gpu_glyph(const uint8_t *cov, int w, int h, int *ax, int *ay, uint32_t *gen, int x, int y, int r, int g, int b, int a) { if (w <= 0 || h <= 0 || w >= ATLAS || h >= ATLAS || a <= 0) return; if (*gen != atlas_gen) { if (atlas_x + w > ATLAS) { atlas_x = 0; atlas_y += atlas_row; atlas_row = 0; } if (atlas_y + h > ATLAS) { /* full: start over, every glyph is put in again when next drawn */ gpu_flush(); atlas_gen++; atlas_x = atlas_y = atlas_row = 0; } p_glActiveTexture(GL_TEXTURE0); p_glBindTexture(GL_TEXTURE_2D, atlas_tex); p_glPixelStorei(GL_UNPACK_ALIGNMENT, 1); p_glTexSubImage2D(GL_TEXTURE_2D, 0, atlas_x, atlas_y, w, h, GL_RED, GL_UNSIGNED_BYTE, cov); p_glPixelStorei(GL_UNPACK_ALIGNMENT, 4); *ax = atlas_x; *ay = atlas_y; *gen = atlas_gen; atlas_x += w; if (h > atlas_row) atlas_row = h; } Inst *i = quad(K_GLYPH, 0, x, y, w, h, r, g, b, a); if (i) { i->p[2] = (float)*ax; i->p[3] = (float)*ay; } } void gpu_image(uint32_t tex, int x, int y, int w, int h, int premultiplied) { if (tex && w > 0 && h > 0) quad(premultiplied ? K_IMAGE_PM : K_IMAGE, tex, x, y, w, h, 0, 0, 0, 0); } void gpu_surface(uint32_t tex, int tw, int th, int x, int y, int w, int h) { if (!tex || w <= 0 || h <= 0) return; int cw = tw < w ? tw : w, ch = th < h ? th : h; if (cw < w || ch < h) { Inst *i = quad(K_TEXEL, tex, x, y, w, h, 0, 0, 0, 0); if (i) { i->p[0] = (float)(tw - 1); i->p[1] = (float)(th - 1); } } quad(K_SURFACE, tex, x, y, cw, ch, 0, 0, 0, 0); } /* ------------------------------------------------------------------ the frame */ int gpu_resize(int w, int h) { gpu_flush(); if (w > max_tex || h > max_tex) return 0; if (fb_tex) p_glDeleteTextures(1, &fb_tex); if (back_tex) p_glDeleteTextures(1, &back_tex); back_tex = 0; if (!fb_fbo) p_glGenFramebuffers(1, &fb_fbo); fb_tex = tex_new(GL_RGBA8, w, h, GL_NEAREST); if (!attach(fb_fbo, fb_tex)) return 0; fb_w = w; fb_h = h; gpu_clear(); return 1; } void gpu_clear(void) { batch_n = 0; batch_tex = 0; p_glBindFramebuffer(GL_FRAMEBUFFER, fb_fbo); p_glClearColor(0, 0, 0, 0); p_glClear(GL_COLOR_BUFFER_BIT); } void gpu_read(uint8_t *fb, int stride_px, int x0, int y0, int x1, int y1) { gpu_flush(); p_glBindFramebuffer(GL_FRAMEBUFFER, fb_fbo); p_glPixelStorei(GL_PACK_ROW_LENGTH, stride_px); p_glReadPixels(x0, y0, x1 - x0, y1 - y0, GL_RGBA, GL_UNSIGNED_BYTE, fb + ((size_t)y0 * stride_px + x0) * 4); p_glPixelStorei(GL_PACK_ROW_LENGTH, 0); } void gpu_save_backdrop(void) { gpu_flush(); if (!back_tex) back_tex = tex_new(GL_RGBA8, fb_w, fb_h, GL_NEAREST); p_glBindFramebuffer(GL_FRAMEBUFFER, fb_fbo); p_glActiveTexture(GL_TEXTURE0 + 1); p_glBindTexture(GL_TEXTURE_2D, back_tex); p_glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, fb_w, fb_h); } void gpu_copy_backdrop(void) { if (back_tex) quad(K_SURFACE, back_tex, 0, 0, fb_w, fb_h, 0, 0, 0, 0); } /* ------------------------------------------------------------------ textures */ /* Textures of released surfaces, for the next capture of the same size (a client that redraws * every frame goes round two or three of them). */ #define SPARE 8 static struct { GLuint tex; int w, h; } spare[SPARE]; static GLuint tex_take(int w, int h) { for (int i = 0; i < SPARE; i++) if (spare[i].tex && spare[i].w == w && spare[i].h == h) { GLuint t = spare[i].tex; memmove(spare + i, spare + i + 1, (size_t)(SPARE - 1 - i) * sizeof spare[0]); spare[SPARE - 1].tex = 0; return t; } if (w > max_tex || h > max_tex) return 0; return tex_new(GL_RGBA8, w, h, GL_LINEAR); } void gpu_tex_free(uint32_t tex, int w, int h) { if (!tex) return; if (batch_tex == tex) gpu_flush(); int i = 0; while (i < SPARE && spare[i].tex) i++; if (i == SPARE) { p_glDeleteTextures(1, &spare[0].tex); memmove(spare, spare + 1, (SPARE - 1) * sizeof spare[0]); i = SPARE - 1; } spare[i].tex = tex; spare[i].w = w; spare[i].h = h; } static void tex_swizzle(int bgr, int opaque) { p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, bgr ? GL_BLUE : GL_RED); p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, bgr ? GL_RED : GL_BLUE); p_glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, opaque ? GL_ONE : GL_ALPHA); } /* Large uploads go through a buffer the GPU then copies from in its own time: writing into a * texture directly waits for the GPU, or converts its layout on the CPU (8 ms against 3 for a * 1080p window on Haswell). */ static int upload_buffered(const uint8_t *px, int stride_px, int w, int h) { static GLuint pbo; intptr_t n = (intptr_t)w * h * 4; if (!pbo) p_glGenBuffers(1, &pbo); p_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pbo); p_glBufferData(GL_PIXEL_UNPACK_BUFFER, n, NULL, GL_STREAM_DRAW); uint8_t *m = p_glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, n, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT); int ok = m != NULL; if (ok) { if (stride_px == w) memcpy(m, px, (size_t)n); else for (int y = 0; y < h; y++) memcpy(m + (size_t)y * w * 4, px + (size_t)y * stride_px * 4, (size_t)w * 4); ok = p_glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); if (ok) p_glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, NULL); } p_glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); return ok; } static GLuint tex_upload(const uint8_t *px, int stride_px, int w, int h, int bgr, int opaque) { GLuint t = tex_take(w, h); if (!t) return 0; p_glActiveTexture(GL_TEXTURE0 + 1); p_glBindTexture(GL_TEXTURE_2D, t); tex_swizzle(bgr, opaque); if (w * h >= 256 * 256 && upload_buffered(px, stride_px, w, h)) return t; p_glPixelStorei(GL_UNPACK_ROW_LENGTH, stride_px); p_glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, px); p_glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); return t; } uint32_t gpu_tex_rgba(const uint8_t *px, int w, int h) { return tex_upload(px, w, w, h, 0, 0); } uint32_t gpu_tex_shm(const uint8_t *px, int stride, int w, int h, int opaque) { if (stride % 4 == 0) return tex_upload(px, stride / 4, w, h, 1, opaque); /* Rows at odd strides: packed first. */ uint8_t *tight = malloc((size_t)w * h * 4); if (!tight) return 0; for (int y = 0; y < h; y++) memcpy(tight + (size_t)y * w * 4, px + (size_t)y * stride, (size_t)w * 4); GLuint t = tex_upload(tight, w, w, h, 1, opaque); free(tight); return t; } /* Draw `src` into a texture of w x h: one to one (`one`, upside down when `flip`) or scaled. */ static GLuint tex_copy(GLuint src, int w, int h, int one, int flip) { GLuint t = tex_take(w, h); if (!t) return 0; p_glActiveTexture(GL_TEXTURE0 + 1); p_glBindTexture(GL_TEXTURE_2D, t); tex_swizzle(0, 0); if (!attach(tmp_fbo, t)) { p_glDeleteTextures(1, &t); return 0; } p_glViewport(0, 0, w, h); p_glDisable(GL_BLEND); p_glUseProgram(prog_scale); p_glUniform2f(u_scale_size, (float)w, (float)h); p_glUniform1i(u_scale_flip, flip); p_glUniform1i(u_scale_one, one); p_glBindTexture(GL_TEXTURE_2D, src); p_glBindVertexArray(vao_empty); p_glDrawArrays(GL_TRIANGLE_STRIP, 0, 3); return t; } uint32_t gpu_tex_scaled(uint32_t src, int sw, int sh, int w, int h) { gpu_flush(); return tex_copy(src, w, h, sw == w && sh == h, 0); } /* ------------------------------------------------------------------ dmabufs */ #define FOURCC(a, b, c, d) ((uint32_t)(a) | (uint32_t)(b) << 8 | (uint32_t)(c) << 16 | (uint32_t)(d) << 24) #define DRM_XRGB FOURCC('X', 'R', '2', '4') #define DRM_ARGB FOURCC('A', 'R', '2', '4') #define DRM_XBGR FOURCC('X', 'B', '2', '4') #define DRM_ABGR FOURCC('A', 'B', '2', '4') #define MOD_INVALID 0x00ffffffffffffffull static int fourcc_known(uint32_t f) { return f == DRM_XRGB || f == DRM_ARGB || f == DRM_XBGR || f == DRM_ABGR; } uint64_t gpu_device(void) { return gpu_on && can_import ? device : 0; } int gpu_dmabuf_formats(uint32_t *fourcc, uint64_t *modifier, int cap) { if (!gpu_on || !can_import) return 0; EGLint formats[256], nf = 0; if (!p_eglQueryDmaBufFormatsEXT(dpy, 256, formats, &nf)) return 0; int n = 0; for (int i = 0; i < nf; i++) { uint32_t f = (uint32_t)formats[i]; if (!fourcc_known(f)) continue; uint64_t mods[128]; uint32_t external[128]; EGLint nm = 0; if (has_modifiers && p_eglQueryDmaBufModifiersEXT(dpy, formats[i], 128, mods, external, &nm)) for (int m = 0; m < nm && n < cap; m++) if (!external[m]) { fourcc[n] = f; modifier[n++] = mods[m]; } /* And a buffer whose layout the driver keeps to itself (clients that know no modifiers). */ if (n < cap) { fourcc[n] = f; modifier[n++] = MOD_INVALID; } } return n; } static EGLImage image_import(int n, const int *fd, const uint32_t *offset, const uint32_t *stride, uint64_t modifier, int w, int h, uint32_t fourcc) { static const EGLint plane_fd[4] = { 0x3272, 0x3275, 0x3278, 0x3440 }; static const EGLint plane_mod[4] = { 0x3443, 0x3445, 0x3447, 0x3449 }; EGLint a[64]; int k = 0; a[k++] = EGL_WIDTH; a[k++] = w; a[k++] = EGL_HEIGHT; a[k++] = h; a[k++] = EGL_LINUX_DRM_FOURCC_EXT; a[k++] = (EGLint)fourcc; for (int i = 0; i < n; i++) { a[k++] = plane_fd[i]; a[k++] = fd[i]; a[k++] = plane_fd[i] + 1; a[k++] = (EGLint)offset[i]; a[k++] = plane_fd[i] + 2; a[k++] = (EGLint)stride[i]; if (has_modifiers && modifier != MOD_INVALID) { a[k++] = plane_mod[i]; a[k++] = (EGLint)(uint32_t)modifier; a[k++] = plane_mod[i] + 1; a[k++] = (EGLint)(uint32_t)(modifier >> 32); } } a[k] = EGL_NONE; return p_eglCreateImageKHR(dpy, NULL, EGL_LINUX_DMA_BUF_EXT, NULL, a); } typedef struct { EGLImage image; GLuint tex; int w, h, opaque, flip; } Dmabuf; static Dmabuf *dmabufs; static int n_dmabufs; uint32_t gpu_dmabuf_import(int n, const int *fd, const uint32_t *offset, const uint32_t *stride, uint64_t modifier, int w, int h, uint32_t fourcc, int y_invert) { if (!gpu_on || !can_import || n < 1 || n > 4 || w <= 0 || h <= 0 || w > max_tex || h > max_tex) return 0; if (!fourcc_known(fourcc)) return 0; EGLImage im = image_import(n, fd, offset, stride, modifier, w, h, fourcc); if (!im) return 0; while (p_glGetError()) {} GLuint t = tex_new(0, 0, 0, GL_NEAREST); p_glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, im); if (p_glGetError()) { p_glDeleteTextures(1, &t); p_eglDestroyImageKHR(dpy, im); return 0; } int i = 0; while (i < n_dmabufs && dmabufs[i].image) i++; if (i == n_dmabufs) { Dmabuf *grown = realloc(dmabufs, sizeof *dmabufs * (size_t)(n_dmabufs + 16)); if (!grown) { p_glDeleteTextures(1, &t); p_eglDestroyImageKHR(dpy, im); return 0; } dmabufs = grown; memset(dmabufs + n_dmabufs, 0, sizeof *dmabufs * 16); n_dmabufs += 16; } dmabufs[i] = (Dmabuf){ im, t, w, h, fourcc == DRM_XRGB || fourcc == DRM_XBGR, y_invert }; return (uint32_t)i + 1; } static Dmabuf *dmabuf(uint32_t id) { return id >= 1 && id <= (uint32_t)n_dmabufs && dmabufs[id - 1].image ? &dmabufs[id - 1] : NULL; } void gpu_dmabuf_free(uint32_t id) { Dmabuf *d = dmabuf(id); if (!d) return; p_glDeleteTextures(1, &d->tex); p_eglDestroyImageKHR(dpy, d->image); memset(d, 0, sizeof *d); } /* The client may draw into its buffer again as soon as this returns: the copy is handed to the GPU * here, and the kernel orders the client's next drawing after it (implicit sync). */ uint32_t gpu_dmabuf_copy(uint32_t id, int *w, int *h, int *opaque) { Dmabuf *d = dmabuf(id); if (!d) return 0; gpu_flush(); p_glActiveTexture(GL_TEXTURE0 + 1); p_glBindTexture(GL_TEXTURE_2D, d->tex); p_glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, d->image); /* what the client drew since */ GLuint t = tex_copy(d->tex, d->w, d->h, 1, d->flip); if (!t) return 0; p_glFlush(); *w = d->w; *h = d->h; *opaque = d->opaque; return t; } /* ------------------------------------------------------------------ the crash guard */ /* A login session that dies while it starts drawing on the display with the GPU must not do so at * every login: it leaves this file, and the next session keeps to the CPU once (and removes it). */ static char guard_path[512]; static int64_t guard_since; /* when the file was made; 0: there is none */ static int64_t now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return (int64_t)t.tv_sec * 1000 + t.tv_nsec / 1000000; } /* 0 when the last session died at it. */ static int guard_check(void) { const char *state = getenv("XDG_STATE_HOME"), *home = getenv("HOME"); if (state && *state) snprintf(guard_path, sizeof guard_path, "%s/chandra/gpu-starting", state); else if (home && *home) snprintf(guard_path, sizeof guard_path, "%s/.local/state/chandra/gpu-starting", home); return !(guard_path[0] && unlink(guard_path) == 0); } /* An orderly end within those seconds is no crash. */ static void guard_exit(void) { if (guard_since) unlink(guard_path); } static void guard_start(void) { if (!guard_path[0] || guard_since) return; int fd = open(guard_path, O_WRONLY | O_CREAT | O_CLOEXEC, 0600); if (fd < 0) { guard_path[0] = 0; return; } close(fd); guard_since = now_ms(); atexit(guard_exit); } /* Frames have been shown for a while: all is well. */ static void guard_done(void) { if (!guard_since || now_ms() - guard_since < 5000) return; unlink(guard_path); guard_path[0] = 0; guard_since = 0; } /* ------------------------------------------------------------------ scanout */ static struct { EGLImage image; GLuint rbo, fbo; int state, w, h; } scan[2]; /* state: 1 works, -1 doesn't */ static int scan_setup(int slot, int fd, uint32_t pitch, int pw, int ph) { if (scan[slot].state && scan[slot].w == pw && scan[slot].h == ph) return scan[slot].state > 0; if (scan[slot].fbo) p_glDeleteFramebuffers(1, &scan[slot].fbo); if (scan[slot].rbo) p_glDeleteRenderbuffers(1, &scan[slot].rbo); if (scan[slot].image) p_eglDestroyImageKHR(dpy, scan[slot].image); memset(&scan[slot], 0, sizeof scan[slot]); scan[slot].state = -1; scan[slot].w = pw; scan[slot].h = ph; if (!can_import || fd < 0) return 0; uint32_t zero = 0; EGLImage im = image_import(1, &fd, &zero, &pitch, 0 /* linear */, pw, ph, DRM_XRGB); if (!im) { fprintf(stderr, "gpu: can't draw into the display's buffers (EGL error 0x%x)\n", p_eglGetError()); return 0; } p_glGenRenderbuffers(1, &scan[slot].rbo); p_glBindRenderbuffer(GL_RENDERBUFFER, scan[slot].rbo); while (p_glGetError()) {} p_glEGLImageTargetRenderbufferStorageOES(GL_RENDERBUFFER, im); p_glGenFramebuffers(1, &scan[slot].fbo); p_glBindFramebuffer(GL_FRAMEBUFFER, scan[slot].fbo); p_glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, scan[slot].rbo); scan[slot].image = im; if (p_glGetError() || p_glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { fprintf(stderr, "gpu: the display's buffers can't be drawn into\n"); return 0; } scan[slot].state = 1; return 1; } int gpu_scanout(int slot, int fd, uint32_t pitch, int pw, int ph, int scale, int rot, int x0, int y0, int x1, int y1) { guard_start(); guard_done(); if (!scan_setup(slot, fd, pitch, pw, ph)) return 0; gpu_flush(); if (x0 < x1 && y0 < y1) { p_glBindFramebuffer(GL_FRAMEBUFFER, scan[slot].fbo); p_glViewport(0, 0, pw, ph); p_glDisable(GL_BLEND); if (rot == 0) { /* turned screens are copied whole */ p_glEnable(GL_SCISSOR_TEST); p_glScissor(x0 * scale, y0 * scale, (x1 - x0) * scale, (y1 - y0) * scale); } p_glUseProgram(prog_scan); p_glUniform4i(u_turn, rot, scale, rot & 1 ? fb_h : fb_w, rot & 1 ? fb_w : fb_h); p_glActiveTexture(GL_TEXTURE0 + 1); p_glBindTexture(GL_TEXTURE_2D, fb_tex); p_glBindVertexArray(vao_empty); p_glDrawArrays(GL_TRIANGLE_STRIP, 0, 3); p_glDisable(GL_SCISSOR_TEST); } p_glFlush(); /* the page flip waits for the drawing (implicit sync) */ return 1; } /* ------------------------------------------------------------------ setup */ /* Display-only and virtual DRM drivers (compositor/shell/render_mode.mbt has the same list). */ static int driver_is_gpu(const char *node) { static const char *const not_gpus[] = { "simpledrm", "simple-framebuffer", "efifb", "vesafb", "vkms", "vgem", "virtio_gpu", "virtio-gpu", "qxl", "bochs", "bochs-drm", "cirrus", "cirrus-qemu", "vboxvideo", "vmwgfx", "hyperv_drm", "udl", "gud", "ast", "mgag200", "xen-front", }; char path[128], line[256], drv[128] = ""; snprintf(path, sizeof path, "/sys/class/drm/%s/device/uevent", node); FILE *f = fopen(path, "r"); if (!f) return 0; while (fgets(line, sizeof line, f)) if (sscanf(line, "DRIVER=%127s", drv) == 1) break; fclose(f); for (size_t i = 0; i < sizeof not_gpus / sizeof not_gpus[0]; i++) if (!strcmp(drv, not_gpus[i])) return 0; return drv[0] != 0; } static int env_is(const char *name, const char *const *values) { const char *e = getenv(name); for (; e && *values; values++) if (!strcasecmp(e, *values)) return 1; return 0; } static int load_functions(void) { void *egl = dlopen("libEGL.so.1", RTLD_NOW | RTLD_LOCAL); void *gl = egl ? dlopen("libGLESv2.so.2", RTLD_NOW | RTLD_LOCAL) : NULL; if (!egl || !gl) { fprintf(stderr, "gpu: no %s on this machine\n", egl ? "libGLESv2.so.2" : "libEGL.so.1"); return 0; } void *(*get)(const char *) = (void *(*)(const char *))dlsym(egl, "eglGetProcAddress"); if (!get) return 0; #define LOAD(lib, name) \ *(void **)&p_##name = dlsym(lib, #name); \ if (!p_##name) *(void **)&p_##name = get(#name); #define NEED_EGL(ret, name, args) LOAD(egl, name) if (!p_##name) { fprintf(stderr, "gpu: EGL has no " #name "\n"); return 0; } #define NEED_GL(ret, name, args) LOAD(gl, name) if (!p_##name) { fprintf(stderr, "gpu: GLES has no " #name "\n"); return 0; } #define WANT(ret, name, args) *(void **)&p_##name = get(#name); EGL_FNS(NEED_EGL) GL_FNS(NEED_GL) EGL_OPT_FNS(WANT) return 1; } /* The EGL device of the first real GPU (or of CHANDRA_RENDER_NODE), and its node's dev_t. */ static EGLDeviceEXT pick_device(int forced) { EGLDeviceEXT devs[16]; EGLint n = 0; if (!p_eglQueryDevicesEXT(16, devs, &n)) return NULL; const char *want = getenv("CHANDRA_RENDER_NODE"); EGLDeviceEXT best = NULL; char best_path[64] = ""; for (int i = 0; i < n; i++) { const char *path = p_eglQueryDeviceStringEXT(devs[i], EGL_DRM_RENDER_NODE_FILE_EXT); if (!path) path = p_eglQueryDeviceStringEXT(devs[i], EGL_DRM_DEVICE_FILE_EXT); if (!path) continue; /* Mesa's software device */ const char *base = strrchr(path, '/'); if (want && *want) { if (strcmp(path, want)) continue; } else if (!driver_is_gpu(base ? base + 1 : path) && !forced) continue; /* The lowest node, the order the kernel found them in. */ if (best && strcmp(path, best_path) >= 0) continue; struct stat st; if (stat(path, &st) < 0) continue; best = devs[i]; device = (uint64_t)st.st_rdev; snprintf(best_path, sizeof best_path, "%s", path); } return best; } static int setup(int forced) { if (!load_functions()) return 0; EGLDeviceEXT dev = pick_device(forced); if (!dev) { fprintf(stderr, "gpu: no GPU found\n"); return 0; } dpy = p_eglGetPlatformDisplayEXT(EGL_PLATFORM_DEVICE_EXT, dev, NULL); if (!dpy || !p_eglInitialize(dpy, NULL, NULL)) { fprintf(stderr, "gpu: can't open the GPU (EGL error 0x%x)\n", p_eglGetError()); return 0; } const char *ext = p_eglQueryString(dpy, EGL_EXTENSIONS); if (!has_ext(ext, "EGL_KHR_surfaceless_context") || !has_ext(ext, "EGL_KHR_no_config_context")) { fprintf(stderr, "gpu: EGL can't draw without a window\n"); return 0; } p_eglBindAPI(EGL_OPENGL_ES_API); /* Ahead of the apps' drawing, where the driver allows it. */ EGLint attrs[] = { EGL_CONTEXT_MAJOR_VERSION, 3, EGL_NONE, EGL_NONE, EGL_NONE }; if (has_ext(ext, "EGL_IMG_context_priority")) { attrs[2] = EGL_CONTEXT_PRIORITY_LEVEL_IMG; attrs[3] = EGL_CONTEXT_PRIORITY_HIGH_IMG; } ctx = p_eglCreateContext(dpy, NULL, NULL, attrs); if (!ctx && attrs[2] != EGL_NONE) { attrs[2] = EGL_NONE; ctx = p_eglCreateContext(dpy, NULL, NULL, attrs); } if (!ctx || !p_eglMakeCurrent(dpy, NULL, NULL, ctx)) { fprintf(stderr, "gpu: no OpenGL ES 3\n"); return 0; } const char *vendor = (const char *)p_glGetString(GL_VENDOR), *renderer = (const char *)p_glGetString(GL_RENDERER); if (!vendor) vendor = ""; if (!renderer) renderer = ""; if (!forced && (strstr(renderer, "llvmpipe") || strstr(renderer, "softpipe") || strstr(renderer, "SWR"))) { fprintf(stderr, "gpu: %s is not a GPU\n", renderer); return 0; } if (!forced && !strncmp(vendor, "NVIDIA", 6)) { /* No implicit sync: frames could be shown, and buffers reused, before they are drawn. */ fprintf(stderr, "gpu: NVIDIA's own driver is not used (CHANDRA_GPU=1 to try)\n"); return 0; } p_glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max_tex); has_modifiers = has_ext(ext, "EGL_EXT_image_dma_buf_import_modifiers") && p_eglQueryDmaBufModifiersEXT; can_import = has_ext(ext, "EGL_EXT_image_dma_buf_import") && has_modifiers && p_eglCreateImageKHR && p_eglDestroyImageKHR && p_eglQueryDmaBufFormatsEXT && p_glEGLImageTargetTexture2DOES && p_glEGLImageTargetRenderbufferStorageOES && has_ext((const char *)p_glGetString(GL_EXTENSIONS), "GL_OES_EGL_image"); prog = program(VS, FS); prog_scale = program(VS_COPY, FS_SCALE); prog_scan = program(VS_COPY, FS_SCAN); if (!prog || !prog_scale || !prog_scan) return 0; p_glUseProgram(prog); p_glUniform1i(p_glGetUniformLocation(prog, "atlas"), 0); p_glUniform1i(p_glGetUniformLocation(prog, "img"), 1); u_size = p_glGetUniformLocation(prog, "size"); p_glUseProgram(prog_scale); p_glUniform1i(p_glGetUniformLocation(prog_scale, "img"), 1); u_scale_size = p_glGetUniformLocation(prog_scale, "size"); u_scale_flip = p_glGetUniformLocation(prog_scale, "flip"); u_scale_one = p_glGetUniformLocation(prog_scale, "one"); p_glUseProgram(prog_scan); p_glUniform1i(p_glGetUniformLocation(prog_scan, "img"), 1); u_turn = p_glGetUniformLocation(prog_scan, "turn"); p_glGenVertexArrays(1, &vao_empty); p_glGenVertexArrays(1, &vao); p_glBindVertexArray(vao); p_glGenBuffers(1, &vbo); p_glBindBuffer(GL_ARRAY_BUFFER, vbo); static const size_t at[4] = { offsetof(Inst, quad), offsetof(Inst, p), offsetof(Inst, q), offsetof(Inst, color) }; for (GLuint i = 0; i < 4; i++) { p_glEnableVertexAttribArray(i); p_glVertexAttribPointer(i, 4, i == 3 ? GL_UNSIGNED_BYTE : GL_FLOAT, i == 3, sizeof(Inst), (const void *)at[i]); p_glVertexAttribDivisor(i, 1); } p_glGenFramebuffers(1, &tmp_fbo); atlas_tex = tex_new(GL_R8, ATLAS, ATLAS, GL_NEAREST); p_glDisable(GL_DITHER); p_glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); if (p_glGetError()) { fprintf(stderr, "gpu: OpenGL setup failed\n"); return 0; } fprintf(stderr, "gpu: drawing on %s%s\n", renderer, can_import ? "" : " (no dmabufs)"); return 1; } /* Find the GPU and get ready to draw on it. CHANDRA_GPU=0 (or CHANDRA_RENDER=software, * LIBGL_ALWAYS_SOFTWARE) keeps to the CPU; CHANDRA_GPU=1 takes whatever OpenGL there is, a virtual * machine's or llvmpipe too. */ int gpu_init(int session) { static const char *const yes[] = { "1", "yes", "on", "true", NULL }, *const no[] = { "0", "no", "off", "false", NULL }, *const soft[] = { "software", "sw", "cpu", NULL }; int forced = env_is("CHANDRA_GPU", yes); if (env_is("CHANDRA_GPU", no)) return 0; const char *ls = getenv("LIBGL_ALWAYS_SOFTWARE"); if (!forced && (env_is("CHANDRA_RENDER", soft) || (ls && *ls && strcmp(ls, "0") && strcmp(ls, "false")))) return 0; if (session && !guard_check() && !forced) { fprintf(stderr, "gpu: the last session died starting to draw on the GPU: this one uses the CPU\n"); return 0; } gpu_on = setup(forced); return gpu_on; }