1 /*
2 * Copyright (c) 2021, Jeffy Chen <jeffy.chen@rock-chips.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15 #include <errno.h>
16 #include <fcntl.h>
17 #include <malloc.h>
18 #include <unistd.h>
19
20 #include <drm.h>
21 #include <xf86drm.h>
22 #include <xf86drmMode.h>
23
24 #include <gbm.h>
25 #include <EGL/egl.h>
26 #include <EGL/eglext.h>
27 #include <GLES2/gl2.h>
28 #include <GLES2/gl2ext.h>
29
30 #include "drm_common.h"
31 #include "drm_egl.h"
32
33 #define EGL_LOAD_PROC(val, type, func) \
34 do { val = (type) eglGetProcAddress(func); } while (0)
35
36 static const GLfloat texcoords[] = {
37 0.0f, 1.0f,
38 1.0f, 1.0f,
39 0.0f, 0.0f,
40 1.0f, 0.0f,
41 };
42
43 static const char vertex_shader_source[] =
44 "attribute vec4 position;\n"
45 "attribute vec2 texcoord;\n"
46 "varying vec2 v_texcoord;\n"
47 "void main()\n"
48 "{\n"
49 " gl_Position = position;\n"
50 " v_texcoord = texcoord;\n"
51 "}\n";
52
53 static const char fragment_shader_source[] =
54 "#extension GL_OES_EGL_image_external : require\n"
55 "precision mediump float;\n"
56 "varying vec2 v_texcoord;\n"
57 "uniform samplerExternalOES tex;\n"
58 "void main()\n"
59 "{\n"
60 " gl_FragColor = texture2D(tex, v_texcoord);\n"
61 "}\n";
62
63 /* HACK: use multiple surfaces to avoid AFBC corruption */
64 #define MAX_NUM_SURFACES 64
65
66 typedef struct {
67 struct gbm_device *gbm_dev;
68 struct gbm_surface *gbm_surfaces[MAX_NUM_SURFACES];
69
70 EGLDisplay egl_display;
71 EGLContext egl_context;
72 EGLConfig egl_config;
73 EGLSurface egl_surfaces[MAX_NUM_SURFACES];
74 GLuint vertex_shader, fragment_shader, program;
75
76 int width;
77 int height;
78
79 int format;
80 uint64_t modifier;
81
82 int current_surface;
83 int num_surfaces;
84 } egl_ctx;
85
egl_free_ctx(void * data)86 drm_private void egl_free_ctx(void *data)
87 {
88 egl_ctx *ctx = data;
89 int i;
90
91 if (ctx->egl_display != EGL_NO_DISPLAY) {
92 eglMakeCurrent(ctx->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
93 EGL_NO_CONTEXT);
94
95 if (ctx->program)
96 glDeleteProgram(ctx->program);
97
98 if (ctx->fragment_shader)
99 glDeleteShader(ctx->fragment_shader);
100
101 if (ctx->vertex_shader)
102 glDeleteShader(ctx->vertex_shader);
103
104 for (i = 0; i < ctx->num_surfaces; i++) {
105 if (ctx->egl_surfaces[i] != EGL_NO_SURFACE)
106 eglDestroySurface(ctx->egl_display, ctx->egl_surfaces[i]);
107 }
108
109 if (ctx->egl_context != EGL_NO_CONTEXT)
110 eglDestroyContext(ctx->egl_display, ctx->egl_context);
111
112 eglTerminate(ctx->egl_display);
113 eglReleaseThread();
114 }
115
116 for (i = 0; i < ctx->num_surfaces; i++) {
117 if (ctx->gbm_surfaces[i])
118 gbm_surface_destroy(ctx->gbm_surfaces[i]);
119 }
120
121 if (ctx->gbm_dev)
122 gbm_device_destroy(ctx->gbm_dev);
123
124 free(ctx);
125 }
126
egl_flush_surfaces(egl_ctx * ctx)127 static int egl_flush_surfaces(egl_ctx *ctx)
128 {
129 int i;
130
131 /* Re-create surfaces */
132
133 eglMakeCurrent(ctx->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
134 EGL_NO_CONTEXT);
135
136 for (i = 0; i < ctx->num_surfaces; i++) {
137 if (ctx->egl_surfaces[i] != EGL_NO_SURFACE) {
138 eglDestroySurface(ctx->egl_display, ctx->egl_surfaces[i]);
139 ctx->egl_surfaces[i] = EGL_NO_SURFACE;
140 }
141 }
142
143 for (i = 0; i < ctx->num_surfaces; i++) {
144 if (ctx->gbm_surfaces[i]) {
145 gbm_surface_destroy(ctx->gbm_surfaces[i]);
146 ctx->gbm_surfaces[i] = NULL;
147 }
148 }
149
150 for (i = 0; i < ctx->num_surfaces; i++) {
151 if (!ctx->modifier)
152 ctx->gbm_surfaces[i] =
153 gbm_surface_create(ctx->gbm_dev, ctx->width, ctx->height,
154 ctx->format, 0);
155 else
156 ctx->gbm_surfaces[i] =
157 gbm_surface_create_with_modifiers(ctx->gbm_dev,
158 ctx->width, ctx->height,
159 ctx->format, &ctx->modifier, 1);
160 if (!ctx->gbm_surfaces[i]) {
161 DRM_ERROR("failed to create GBM surface\n");
162 return -1;
163 }
164
165 ctx->egl_surfaces[i] =
166 eglCreateWindowSurface(ctx->egl_display, ctx->egl_config,
167 (EGLNativeWindowType)ctx->gbm_surfaces[i], NULL);
168 if (ctx->egl_surfaces[i] == EGL_NO_SURFACE) {
169 DRM_ERROR("failed to create EGL surface\n");
170 return -1;
171 }
172 }
173
174 ctx->current_surface = 0;
175 return 0;
176 }
177
egl_init_ctx(int fd,int num_surfaces,int format,uint64_t modifier)178 drm_private void *egl_init_ctx(int fd, int num_surfaces, int format,
179 uint64_t modifier)
180 {
181 PFNEGLGETPLATFORMDISPLAYEXTPROC get_platform_display;
182
183 EGLConfig *configs;
184 EGLint num_configs;
185 egl_ctx *ctx;
186
187 GLint texcoord;
188 GLint status;
189 const char *source;
190 char msg[512];
191 int i;
192
193 static const EGLint context_attribs[] = {
194 EGL_CONTEXT_CLIENT_VERSION, 2,
195 EGL_NONE
196 };
197
198 if (num_surfaces > MAX_NUM_SURFACES) {
199 DRM_ERROR("too much surfaces: %d > %d\n", num_surfaces, MAX_NUM_SURFACES);
200 return NULL;
201 }
202
203 EGL_LOAD_PROC(get_platform_display, PFNEGLGETPLATFORMDISPLAYEXTPROC,
204 "eglGetPlatformDisplayEXT");
205 if (!get_platform_display) {
206 DRM_ERROR("failed to get proc address\n");
207 return NULL;
208 }
209
210 ctx = calloc(1, sizeof(*ctx));
211 if (!ctx) {
212 DRM_ERROR("failed to alloc ctx\n");
213 return NULL;
214 }
215
216 ctx->format = format;
217 ctx->modifier = modifier;
218 ctx->num_surfaces = num_surfaces;
219 ctx->width = ctx->height = 0;
220
221 for (i = 0; i < ctx->num_surfaces; i++)
222 ctx->egl_surfaces[i] = EGL_NO_SURFACE;
223
224 ctx->gbm_dev = gbm_create_device(fd);
225 if (!ctx->gbm_dev) {
226 DRM_ERROR("failed to create gbm device\n");
227 goto err;
228 }
229
230 ctx->egl_display = get_platform_display(EGL_PLATFORM_GBM_KHR,
231 (void*)ctx->gbm_dev, NULL);
232 if (ctx->egl_display == EGL_NO_DISPLAY) {
233 DRM_ERROR("failed to get platform display\n");
234 goto err;
235 }
236
237 if (!eglInitialize(ctx->egl_display, NULL, NULL)) {
238 DRM_ERROR("failed to init egl\n");
239 goto err;
240 }
241
242 if (!eglBindAPI(EGL_OPENGL_ES_API)) {
243 DRM_ERROR("failed to bind api\n");
244 goto err;
245 }
246
247 if (!eglGetConfigs(ctx->egl_display, NULL, 0, &num_configs) ||
248 num_configs < 1) {
249 DRM_ERROR("failed to get configs\n");
250 goto err;
251 }
252
253 configs = calloc(num_configs, sizeof(*configs));
254 if (!configs) {
255 DRM_ERROR("failed to alloc configs\n");
256 goto err;
257 }
258
259 if (!eglGetConfigs(ctx->egl_display, configs, num_configs, &num_configs)) {
260 DRM_ERROR("failed to get configs\n");
261 goto err;
262 }
263
264 for (i = 0; i < num_configs; i++) {
265 EGLint value;
266
267 if (!eglGetConfigAttrib(ctx->egl_display, configs[i],
268 EGL_NATIVE_VISUAL_ID, &value))
269 continue;
270
271 if (value == format)
272 break;
273 }
274
275 if (i == num_configs) {
276 DRM_ERROR("failed to find EGL config for %.4s, force using the first\n",
277 (char *)&format);
278 ctx->egl_config = configs[0];
279 } else {
280 ctx->egl_config = configs[i];
281 }
282
283 ctx->egl_context = eglCreateContext(ctx->egl_display, ctx->egl_config,
284 EGL_NO_CONTEXT, context_attribs);
285 if (ctx->egl_context == EGL_NO_CONTEXT) {
286 DRM_ERROR("failed to create EGL context\n");
287 goto err;
288 }
289
290 eglMakeCurrent(ctx->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE,
291 ctx->egl_context);
292
293 source = vertex_shader_source;
294 ctx->vertex_shader = glCreateShader(GL_VERTEX_SHADER);
295 glShaderSource(ctx->vertex_shader, 1, &source, NULL);
296 glCompileShader(ctx->vertex_shader);
297 glGetShaderiv(ctx->vertex_shader, GL_COMPILE_STATUS, &status);
298 if (!status) {
299 glGetShaderInfoLog(ctx->vertex_shader, sizeof(msg), NULL, msg);
300 DRM_ERROR("failed to compile shader: %s\n", msg);
301 goto err;
302 }
303
304 source = fragment_shader_source;
305 ctx->fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
306 glShaderSource(ctx->fragment_shader, 1, &source, NULL);
307 glCompileShader(ctx->fragment_shader);
308 glGetShaderiv(ctx->fragment_shader, GL_COMPILE_STATUS, &status);
309 if (!status) {
310 glGetShaderInfoLog(ctx->fragment_shader, sizeof(msg), NULL, msg);
311 DRM_ERROR("failed to compile shader: %s\n", msg);
312 goto err;
313 }
314
315 ctx->program = glCreateProgram();
316 glAttachShader(ctx->program, ctx->vertex_shader);
317 glAttachShader(ctx->program, ctx->fragment_shader);
318 glLinkProgram(ctx->program);
319
320 glGetProgramiv(ctx->program, GL_LINK_STATUS, &status);
321 if (!status) {
322 glGetProgramInfoLog(ctx->program, sizeof(msg), NULL, msg);
323 DRM_ERROR("failed to link: %s\n", msg);
324 goto err;
325 }
326
327 glUseProgram(ctx->program);
328
329 texcoord = glGetAttribLocation(ctx->program, "texcoord");
330 glVertexAttribPointer(texcoord, 2, GL_FLOAT, GL_FALSE, 0, texcoords);
331 glEnableVertexAttribArray(texcoord);
332
333 glUniform1i(glGetUniformLocation(ctx->program, "tex"), 0);
334
335 return ctx;
336 err:
337 egl_free_ctx(ctx);
338 return NULL;
339 }
340
egl_bo_to_fb(int fd,struct gbm_bo * bo,int format,uint64_t modifier)341 static uint32_t egl_bo_to_fb(int fd, struct gbm_bo* bo, int format,
342 uint64_t modifier)
343 {
344 uint32_t width = gbm_bo_get_width(bo);
345 uint32_t height = gbm_bo_get_height(bo);
346 uint32_t bpp = gbm_bo_get_bpp(bo) ? gbm_bo_get_bpp(bo) : 32;
347 uint32_t handles[4] = { 0 };
348 uint32_t strides[4] = { 0 };
349 uint32_t offsets[4] = { 0 };
350 uint64_t modifiers[4] = { 0 };
351 uint32_t fb = 0;
352 int ret;
353
354 handles[0] = gbm_bo_get_handle(bo).u32;
355 strides[0] = gbm_bo_get_stride(bo);
356 modifiers[0] = modifier;
357
358 if (!modifier)
359 ret = drmModeAddFB(fd, width, height, bpp, bpp,
360 strides[0], handles[0], &fb);
361 else
362 ret = drmModeAddFB2WithModifiers(fd, width, height, format,
363 handles, strides,
364 offsets, modifiers, &fb,
365 DRM_MODE_FB_MODIFIERS);
366
367 if (ret < 0) {
368 DRM_ERROR("failed to add fb (%d)\n", errno);
369 return 0;
370 }
371
372 return fb;
373 }
374
egl_attach_dmabuf(egl_ctx * ctx,int dma_fd,int width,int height)375 static int egl_attach_dmabuf(egl_ctx *ctx, int dma_fd, int width, int height)
376 {
377 static PFNGLEGLIMAGETARGETTEXTURE2DOESPROC image_target_texture_2d = NULL;
378 static PFNEGLCREATEIMAGEKHRPROC create_image = NULL;
379 static PFNEGLDESTROYIMAGEKHRPROC destroy_image = NULL;
380 EGLImageKHR image;
381
382 /* Cursor format should be ARGB8888 */
383 const EGLint attrs[] = {
384 EGL_WIDTH, width,
385 EGL_HEIGHT, height,
386 EGL_LINUX_DRM_FOURCC_EXT, DRM_FORMAT_ARGB8888,
387 EGL_DMA_BUF_PLANE0_FD_EXT, dma_fd,
388 EGL_DMA_BUF_PLANE0_OFFSET_EXT, 0,
389 EGL_DMA_BUF_PLANE0_PITCH_EXT, width * 4,
390
391 /* Old mali needs extra attributes */
392 EGL_YUV_COLOR_SPACE_HINT_EXT, EGL_ITU_REC601_EXT,
393 EGL_SAMPLE_RANGE_HINT_EXT, EGL_YUV_NARROW_RANGE_EXT,
394
395 EGL_NONE,
396 };
397
398 if (!create_image)
399 EGL_LOAD_PROC(create_image, PFNEGLCREATEIMAGEKHRPROC,
400 "eglCreateImageKHR");
401
402 if (!destroy_image)
403 EGL_LOAD_PROC(destroy_image, PFNEGLDESTROYIMAGEKHRPROC,
404 "eglDestroyImageKHR");
405
406 if (!image_target_texture_2d)
407 EGL_LOAD_PROC(image_target_texture_2d, PFNGLEGLIMAGETARGETTEXTURE2DOESPROC,
408 "glEGLImageTargetTexture2DOES");
409
410 if (!create_image || !destroy_image || !image_target_texture_2d) {
411 DRM_ERROR("failed to get proc address\n");
412 return -1;
413 }
414
415 image = create_image(ctx->egl_display, ctx->egl_context,
416 EGL_LINUX_DMA_BUF_EXT, NULL, attrs);
417 if (image == EGL_NO_IMAGE) {
418 DRM_ERROR("failed to create egl image: 0x%x\n", eglGetError());
419 return -1;
420 }
421
422 image_target_texture_2d(GL_TEXTURE_EXTERNAL_OES, (GLeglImageOES)image);
423 destroy_image(ctx->egl_display, image);
424 return 0;
425 }
426
egl_convert_fb(int fd,void * data,uint32_t handle,int w,int h,int scaled_w,int scaled_h,int x,int y)427 drm_private uint32_t egl_convert_fb(int fd, void *data, uint32_t handle,
428 int w, int h, int scaled_w, int scaled_h,
429 int x, int y)
430 {
431 egl_ctx *ctx = data;
432 GLint position;
433 GLuint texture;
434 struct gbm_bo* bo;
435 uint32_t fb = 0;
436 int dma_fd;
437
438 GLfloat verts[] = {
439 -1.0f, -1.0f,
440 1.0f, -1.0f,
441 -1.0f, 1.0f,
442 1.0f, 1.0f,
443 };
444
445 if (drmPrimeHandleToFD(fd, handle, DRM_CLOEXEC, &dma_fd) < 0) {
446 DRM_ERROR("failed to get dma fd (-%d)\n", errno);
447 return 0;
448 }
449
450 if (ctx->width != scaled_w || ctx->height != scaled_h) {
451 ctx->width = scaled_w;
452 ctx->height = scaled_h;
453 glViewport(0, 0, ctx->width, ctx->height);
454
455 egl_flush_surfaces(ctx);
456 }
457
458 ctx->current_surface = (ctx->current_surface + 1) % ctx->num_surfaces;
459 eglMakeCurrent(ctx->egl_display, ctx->egl_surfaces[ctx->current_surface],
460 ctx->egl_surfaces[ctx->current_surface],
461 ctx->egl_context);
462
463 /* Apply offsets */
464 for (int i = 0; i < 4; i++) {
465 verts[2 * i] += x * 2.0 / ctx->width;
466 verts[2 * i + 1] -= y * 2.0 / ctx->height;
467 }
468
469 position = glGetAttribLocation(ctx->program, "position");
470 glVertexAttribPointer(position, 2, GL_FLOAT, GL_FALSE, 0, verts);
471 glEnableVertexAttribArray(position);
472
473 glGenTextures(1, &texture);
474 glActiveTexture(GL_TEXTURE0);
475 glBindTexture(GL_TEXTURE_EXTERNAL_OES, texture);
476
477 if (egl_attach_dmabuf(ctx, dma_fd, w, h) < 0) {
478 DRM_ERROR("failed to attach dmabuf\n");
479 goto err_del_texture;
480 }
481
482 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
483 eglSwapBuffers(ctx->egl_display, ctx->egl_surfaces[ctx->current_surface]);
484
485 bo = gbm_surface_lock_front_buffer(ctx->gbm_surfaces[ctx->current_surface]);
486 if (!bo) {
487 DRM_ERROR("failed to get front bo\n");
488 goto err_del_texture;
489 }
490
491 fb = egl_bo_to_fb(fd, bo, ctx->format, ctx->modifier);
492 gbm_surface_release_buffer(ctx->gbm_surfaces[ctx->current_surface], bo);
493
494 err_del_texture:
495 glDeleteTextures(1, &texture);
496 close(dma_fd);
497 return fb;
498 }
499