-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmapped_texture_test.c
More file actions
427 lines (364 loc) · 11.5 KB
/
mapped_texture_test.c
File metadata and controls
427 lines (364 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
* Copyright 2017 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/*
The mapped_texture_test test consists of:
* Importing external buffers as EGL Images
* Drawing an ellipse using the CPU
* Binding CPU drawn buffer as a texture and sampling from it
* Using KMS to scanout the resultant framebuffer
*/
#include <getopt.h>
#include "bs_drm.h"
// double buffering
#define NUM_BUFFERS 2
struct offscreen_buffer {
struct gbm_bo *bo;
GLuint tex;
EGLImageKHR image;
const struct bs_draw_format *draw_format;
};
struct framebuffer {
struct gbm_bo *bo;
uint32_t fb_id;
EGLImageKHR image;
struct bs_egl_fb *egl_fb;
};
struct gl_resources {
GLuint program;
GLuint vbo;
};
// clang-format off
static const GLfloat vertices[] = {
// x y u v
-0.25f, -0.25f, 0.0f, 0.0f, // Bottom left
-0.25f, 0.25f, 0.0f, 1.0f, // Top left
0.25f, 0.25f, 1.0f, 1.0f, // Top right
0.25f, -0.25f, 1.0f, 0.0f, // Bottom Right
};
static const int binding_xy = 0;
static const int binding_uv = 1;
static const GLubyte indices[] = {
0, 1, 2,
0, 2, 3
};
// clang-format on
static const GLchar *vert =
"attribute vec2 xy;\n"
"attribute vec2 uv;\n"
"varying vec2 tex_coordinate;\n"
"void main() {\n"
" gl_Position = vec4(xy, 0, 1);\n"
" tex_coordinate = uv;\n"
"}\n";
static const GLchar *frag =
"precision mediump float;\n"
"uniform sampler2D ellipse;\n"
"varying vec2 tex_coordinate;\n"
"void main() {\n"
" gl_FragColor = texture2D(ellipse, tex_coordinate);\n"
"}\n";
static bool create_framebuffer(int display_fd, struct gbm_device *gbm, struct bs_egl *egl,
uint32_t width, uint32_t height, struct framebuffer *fb)
{
fb->bo = gbm_bo_create(gbm, width, height, GBM_FORMAT_XRGB8888,
GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING);
if (!fb->bo) {
bs_debug_error("failed to create a gbm buffer.");
goto delete_gl_fb;
}
fb->fb_id = bs_drm_fb_create_gbm(fb->bo);
if (!fb->fb_id) {
bs_debug_error("failed to create framebuffer from buffer object");
goto delete_gl_image;
}
fb->image = bs_egl_image_create_gbm(egl, fb->bo);
if (fb->image == EGL_NO_IMAGE_KHR) {
bs_debug_error("failed to make image from buffer object");
goto delete_fb;
}
fb->egl_fb = bs_egl_fb_new(egl, fb->image);
if (!fb->egl_fb) {
bs_debug_error("failed to make rednering framebuffer for buffer object");
goto delete_gbm_bo;
}
return true;
delete_gl_fb:
bs_egl_fb_destroy(&fb->egl_fb);
delete_gl_image:
bs_egl_image_destroy(egl, &fb->image);
delete_fb:
drmModeRmFB(display_fd, fb->fb_id);
delete_gbm_bo:
gbm_bo_destroy(fb->bo);
return false;
}
static bool add_offscreen_texture(struct gbm_device *gbm, struct bs_egl *egl,
struct offscreen_buffer *buffer, uint32_t width, uint32_t height,
uint32_t flags)
{
buffer->bo =
gbm_bo_create(gbm, width, height, bs_get_pixel_format(buffer->draw_format), flags);
if (!buffer->bo) {
bs_debug_error("failed to allocate offscreen buffer object: format=%s \n",
bs_get_format_name(buffer->draw_format));
goto destroy_offscreen_buffer;
}
buffer->image = bs_egl_image_create_gbm(egl, buffer->bo);
if (buffer->image == EGL_NO_IMAGE_KHR) {
bs_debug_error("failed to create offscreen egl image");
goto destroy_offscreen_bo;
}
glActiveTexture(GL_TEXTURE1);
glGenTextures(1, &buffer->tex);
glBindTexture(GL_TEXTURE_2D, buffer->tex);
if (!bs_egl_target_texture2D(egl, buffer->image)) {
bs_debug_error("failed to import egl image as texture");
goto destroy_offscreen_image;
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
return true;
destroy_offscreen_image:
glDeleteTextures(1, &buffer->tex);
bs_egl_image_destroy(egl, &buffer->image);
destroy_offscreen_bo:
gbm_bo_destroy(buffer->bo);
destroy_offscreen_buffer:
return false;
}
static bool init_gl(struct bs_egl_fb *fb, uint32_t width, uint32_t height,
struct gl_resources *resources)
{
struct bs_gl_program_create_binding bindings[] = {
{ binding_xy, "xy" }, { binding_uv, "uv" }, { 2, NULL },
};
resources->program = bs_gl_program_create_vert_frag_bind(vert, frag, bindings);
if (!resources->program) {
bs_debug_error("failed to compile shader program");
return false;
}
glGenBuffers(1, &resources->vbo);
glBindBuffer(GL_ARRAY_BUFFER, resources->vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindFramebuffer(GL_FRAMEBUFFER, bs_egl_fb_name(fb));
glViewport(0, 0, (GLint)width, (GLint)height);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(resources->program);
glUniform1i(glGetUniformLocation(resources->program, "ellipse"), 1);
glEnableVertexAttribArray(binding_xy);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0);
glEnableVertexAttribArray(binding_uv);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat),
(void *)(2 * sizeof(GLfloat)));
return true;
}
static void draw_textured_quad(GLuint tex)
{
glClear(GL_COLOR_BUFFER_BIT);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, tex);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);
}
static const struct option longopts[] = {
{ "help", no_argument, NULL, 'h' },
{ "format", required_argument, NULL, 'f' },
{ "dma-buf", no_argument, NULL, 'b' },
{ "gem", no_argument, NULL, 'g' },
{ "dumb", no_argument, NULL, 'd' },
{ "tiled", no_argument, NULL, 't' },
{ 0, 0, 0, 0 },
};
static void print_help(const char *argv0)
{
printf("Usage: %s [OPTIONS]\n", argv0);
printf(" -h, --help Print help.\n");
printf(" -f, --format FOURCC format of texture (defaults to ARGB8888)\n");
printf(" -b, --dma-buf Use dma-buf mmap.\n");
printf(" -g, --gem Use GEM map(by default).\n");
printf(" -d, --dumb Use dump map.\n");
}
static void page_flip_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec,
void *data)
{
int *waiting_for_flip = data;
*waiting_for_flip = 0;
}
void flush_egl(struct bs_egl *egl, EGLImageKHR image)
{
bs_egl_image_flush_external(egl, image);
EGLSyncKHR sync = bs_egl_create_sync(egl, EGL_SYNC_FENCE_KHR, NULL);
bs_egl_wait_sync(egl, sync, 0, EGL_FOREVER_KHR);
bs_egl_destroy_sync(egl, sync);
}
int main(int argc, char **argv)
{
int ret = 1;
int display_fd = bs_drm_open_main_display();
if (display_fd < 0) {
bs_debug_error("failed to open card for display");
goto out;
}
struct offscreen_buffer buffer;
buffer.draw_format = bs_get_draw_format_from_name("ARGB8888");
struct bs_mapper *mapper = NULL;
uint32_t flags = GBM_BO_USE_TEXTURING;
int c;
while ((c = getopt_long(argc, argv, "f:bgdh", longopts, NULL)) != -1) {
switch (c) {
case 'f':
if (!bs_parse_draw_format(optarg, &buffer.draw_format)) {
printf("choose the default format ARGB8888\n");
}
printf("format=%s\n", bs_get_format_name(buffer.draw_format));
break;
case 'b':
mapper = bs_mapper_dma_buf_new();
flags |= GBM_BO_USE_LINEAR;
printf("using dma-buf mmap\n");
break;
case 'g':
mapper = bs_mapper_gem_new();
flags |= GBM_BO_USE_SW_WRITE_OFTEN;
printf("using GEM map\n");
break;
case 'd':
mapper = bs_mapper_dumb_new(display_fd);
flags |= GBM_BO_USE_LINEAR;
printf("using dumb map\n");
break;
case 'h':
default:
print_help(argv[0]);
goto destroy_display_fd;
}
}
// Use gem map mapper by default, in case any arguments aren't selected.
if (!mapper) {
mapper = bs_mapper_gem_new();
flags |= GBM_BO_USE_SW_WRITE_OFTEN;
printf("using GEM map\n");
}
if (!mapper) {
bs_debug_error("failed to create mapper object");
goto destroy_display_fd;
}
uint32_t width;
uint32_t height;
struct gbm_device *gbm = gbm_create_device(display_fd);
if (!gbm) {
bs_debug_error("failed to create gbm device");
goto destroy_mapper;
}
struct bs_drm_pipe pipe = { 0 };
if (!bs_drm_pipe_make(display_fd, &pipe)) {
bs_debug_error("failed to make pipe");
goto destroy_gbm_device;
}
drmModeConnector *connector = drmModeGetConnector(display_fd, pipe.connector_id);
drmModeModeInfo *mode = &connector->modes[0];
width = mode->hdisplay;
height = mode->vdisplay;
struct bs_egl *egl = bs_egl_new();
if (!bs_egl_setup(egl)) {
bs_debug_error("failed to setup egl context");
goto destroy_gbm_device;
}
struct framebuffer fbs[NUM_BUFFERS] = {};
uint32_t front_buffer = 0;
for (size_t i = 0; i < NUM_BUFFERS; i++) {
if (!create_framebuffer(display_fd, gbm, egl, width, height, &fbs[i])) {
bs_debug_error("failed to create framebuffer");
goto delete_framebuffers;
}
}
if (!add_offscreen_texture(gbm, egl, &buffer, width / 4, height / 4, flags)) {
bs_debug_error("failed to create offscreen texture");
goto destroy_offscreen_buffer;
}
const struct framebuffer *back_fb = &fbs[front_buffer ^ 1];
struct gl_resources resources;
if (!init_gl(back_fb->egl_fb, width, height, &resources)) {
bs_debug_error("failed to initialize GL resources.\n");
goto destroy_gl_resources;
}
flush_egl(egl, back_fb->image);
ret = drmModeSetCrtc(display_fd, pipe.crtc_id, fbs[front_buffer].fb_id, 0 /* x */,
0 /* y */, &pipe.connector_id, 1 /* connector count */, mode);
if (ret) {
bs_debug_error("failed to set crtc: %d", ret);
goto destroy_gl_resources;
}
drmEventContext evctx = {
.version = DRM_EVENT_CONTEXT_VERSION, .page_flip_handler = page_flip_handler,
};
fd_set fds;
// The test takes about 2 seconds to complete.
const size_t test_frames = 120;
for (size_t i = 0; i < test_frames; i++) {
int waiting_for_flip = 1;
const struct framebuffer *back_fb = &fbs[front_buffer ^ 1];
glBindFramebuffer(GL_FRAMEBUFFER, bs_egl_fb_name(back_fb->egl_fb));
if (!bs_draw_ellipse(mapper, buffer.bo, buffer.draw_format,
(float)i / test_frames)) {
bs_debug_error("failed to draw to buffer");
goto destroy_gl_resources;
}
draw_textured_quad(buffer.tex);
flush_egl(egl, back_fb->image);
ret = drmModePageFlip(display_fd, pipe.crtc_id, back_fb->fb_id,
DRM_MODE_PAGE_FLIP_EVENT, &waiting_for_flip);
if (ret) {
bs_debug_error("failed to queue page flip");
goto destroy_gl_resources;
}
while (waiting_for_flip) {
FD_ZERO(&fds);
FD_SET(0, &fds);
FD_SET(display_fd, &fds);
int ret = select(display_fd + 1, &fds, NULL, NULL, NULL);
if (ret < 0) {
bs_debug_error("select err: %s", strerror(errno));
goto destroy_gl_resources;
} else if (FD_ISSET(0, &fds)) {
bs_debug_error("exit due to user-input");
goto destroy_gl_resources;
} else if (FD_ISSET(display_fd, &fds)) {
drmHandleEvent(display_fd, &evctx);
}
}
front_buffer ^= 1;
}
destroy_gl_resources:
glBindBuffer(GL_ARRAY_BUFFER, 0);
glUseProgram(0);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glDeleteProgram(resources.program);
glDeleteBuffers(1, &resources.vbo);
destroy_offscreen_buffer:
glDeleteTextures(1, &buffer.tex);
bs_egl_image_destroy(egl, &buffer.image);
gbm_bo_destroy(buffer.bo);
delete_framebuffers:
for (size_t i = 0; i < NUM_BUFFERS; i++) {
bs_egl_fb_destroy(&fbs[i].egl_fb);
bs_egl_image_destroy(egl, &fbs[i].image);
drmModeRmFB(display_fd, fbs[i].fb_id);
gbm_bo_destroy(fbs[i].bo);
}
bs_egl_destroy(&egl);
destroy_gbm_device:
gbm_device_destroy(gbm);
destroy_mapper:
bs_mapper_destroy(mapper);
destroy_display_fd:
close(display_fd);
out:
return ret;
}