-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
229 lines (200 loc) · 8.33 KB
/
main.c
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
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <time.h>
#include <X11/Xlib.h>
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#define EGL_EGLEXT_PROTOTYPES
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include "socket.h"
#include "window.h"
#include "render.h"
void parse_arguments(int argc, char **argv, int *is_server);
void rotate_data(int data[4]);
int main(int argc, char **argv)
{
// Parse arguments
int is_server;
parse_arguments(argc, argv, &is_server);
// Create X11 window
Display *x11_display;
Window x11_window;
create_x11_window(is_server, &x11_display, &x11_window);
// Initialize EGL
EGLDisplay egl_display;
EGLContext egl_context;
EGLSurface egl_surface;
initialize_egl(x11_display, x11_window, &egl_display, &egl_context, &egl_surface);
// Setup GL scene
gl_setup_scene();
// Server texture data { red, greee, blue, white }
int texture_data[4] = {0x000000FF, 0x0000FF00, 0X00FF0000, 0x00FFFFFF};
// -----------------------------
// --- Texture sharing start ---
// -----------------------------
// Socket paths for sending/receiving file descriptor and image storage data
const char *SERVER_FILE = "/tmp/test_server";
const char *CLIENT_FILE = "/tmp/test_client";
// Custom image storage data description to transfer over socket
struct texture_storage_metadata_t
{
int fourcc;
EGLint offset;
EGLint stride;
};
// GL texture that will be shared
GLuint texture;
// The next `if` block contains server code in the `true` branch and client code in the `false` branch. The `true` branch is always executed first and the `false` branch after it (in a different process). This is because the server loops at the end of the branch until it can send a message to the client and the client blocks at the start of the branch until it has a message to read. This way the whole `if` block from top to bottom represents the order of events as they happen.
if (is_server)
{
// GL: Create and populate the texture
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 2, 2, GL_RGBA, GL_UNSIGNED_BYTE, texture_data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// EGL: Create EGL image from the GL texture
EGLImage image = eglCreateImage(egl_display,
egl_context,
EGL_GL_TEXTURE_2D,
(EGLClientBuffer)(uint64_t)texture,
NULL);
assert(image != EGL_NO_IMAGE);
// The next line works around an issue in radeonsi driver (fixed in master at the time of writing). If you are
// not having problems with texture rendering until the first texture update you can omit this line
glFlush();
// EGL (extension: EGL_MESA_image_dma_buf_export): Get file descriptor (texture_dmabuf_fd) for the EGL image and get its
// storage data (texture_storage_metadata - fourcc, stride, offset)
int texture_dmabuf_fd;
struct texture_storage_metadata_t texture_storage_metadata;
PFNEGLEXPORTDMABUFIMAGEQUERYMESAPROC eglExportDMABUFImageQueryMESA =
(PFNEGLEXPORTDMABUFIMAGEQUERYMESAPROC)eglGetProcAddress("eglExportDMABUFImageQueryMESA");
EGLBoolean queried = eglExportDMABUFImageQueryMESA(egl_display,
image,
&texture_storage_metadata.fourcc,
NULL,
NULL);
assert(queried);
PFNEGLEXPORTDMABUFIMAGEMESAPROC eglExportDMABUFImageMESA =
(PFNEGLEXPORTDMABUFIMAGEMESAPROC)eglGetProcAddress("eglExportDMABUFImageMESA");
EGLBoolean exported = eglExportDMABUFImageMESA(egl_display,
image,
&texture_dmabuf_fd,
&texture_storage_metadata.stride,
&texture_storage_metadata.offset);
assert(exported);
// Unix Domain Socket: Send file descriptor (texture_dmabuf_fd) and texture storage data (texture_storage_metadata)
int sock = create_socket(SERVER_FILE);
while (connect_socket(sock, CLIENT_FILE) != 0)
;
write_fd(sock, texture_dmabuf_fd, &texture_storage_metadata, sizeof(texture_storage_metadata));
close(sock);
close(texture_dmabuf_fd);
}
else
{
// Unix Domain Socket: Receive file descriptor (texture_dmabuf_fd) and texture storage data (texture_storage_metadata)
int texture_dmabuf_fd;
struct texture_storage_metadata_t texture_storage_metadata;
int sock = create_socket(CLIENT_FILE);
read_fd(sock, &texture_dmabuf_fd, &texture_storage_metadata, sizeof(texture_storage_metadata));
close(sock);
// EGL (extension: EGL_EXT_image_dma_buf_import): Create EGL image from file descriptor (texture_dmabuf_fd) and storage
// data (texture_storage_metadata)
EGLAttrib const attribute_list[] = {
EGL_WIDTH, 2,
EGL_HEIGHT, 2,
EGL_LINUX_DRM_FOURCC_EXT, texture_storage_metadata.fourcc,
EGL_DMA_BUF_PLANE0_FD_EXT, texture_dmabuf_fd,
EGL_DMA_BUF_PLANE0_OFFSET_EXT, texture_storage_metadata.offset,
EGL_DMA_BUF_PLANE0_PITCH_EXT, texture_storage_metadata.stride,
EGL_NONE};
EGLImage image = eglCreateImage(egl_display,
NULL,
EGL_LINUX_DMA_BUF_EXT,
(EGLClientBuffer)NULL,
attribute_list);
assert(image != EGL_NO_IMAGE);
close(texture_dmabuf_fd);
// GLES (extension: GL_OES_EGL_image_external): Create GL texture from EGL image
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
// -----------------------------
// --- Texture sharing end ---
// -----------------------------
time_t last_time = time(NULL);
while (1)
{
// Draw scene (uses shared texture)
gl_draw_scene(texture);
eglSwapBuffers(egl_display, egl_surface);
// Update texture data each second to see that the client didn't just copy the texture and is indeed referencing
// the same texture data.
if (is_server)
{
time_t cur_time = time(NULL);
if (last_time < cur_time)
{
last_time = cur_time;
rotate_data(texture_data);
glBindTexture(GL_TEXTURE_2D, texture);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 2, 2, GL_RGBA, GL_UNSIGNED_BYTE, texture_data);
}
}
// Check for errors
assert(glGetError() == GL_NO_ERROR);
assert(eglGetError() == EGL_SUCCESS);
}
return 0;
}
void help()
{
printf("USAGE:\n"
" dmabufshare server\n"
" dmabufshare client\n");
}
void parse_arguments(int argc, char **argv, int *is_server)
{
if (2 == argc)
{
if (strcmp(argv[1], "server") == 0)
{
*is_server = 1;
}
else if (strcmp(argv[1], "client") == 0)
{
*is_server = 0;
}
else if (strcmp(argv[1], "--help") == 0)
{
help();
exit(0);
}
else
{
help();
exit(-1);
}
}
else
{
help();
exit(-1);
}
}
void rotate_data(int data[4])
{
int temp = data[0];
data[0] = data[1];
data[1] = data[3];
data[3] = data[2];
data[2] = temp;
}