forked from stschake/flutter_drm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
311 lines (270 loc) · 7.02 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
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
#include "embedder.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/select.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <linux/input.h>
#include <sys/poll.h>
#include "common.h"
#include "drm-common.h"
static const struct egl *egl;
static const struct gbm *gbm;
static const struct drm *drm;
static FlutterEngine engine = NULL;
struct timeval ps, pe;
static void perf_enter()
{
gettimeofday(&ps, NULL);
}
static void perf_exit(const char* name)
{
gettimeofday(&pe, NULL);
int elapsed = ((pe.tv_sec - ps.tv_sec)*1000000) + (pe.tv_usec - ps.tv_usec);
printf("%s took %dus\n", name, elapsed);
}
static void 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 platform_message_callback(const FlutterPlatformMessage* message, void* user)
{
printf("Platform message for channel %s, %d bytes\n", message->channel, message->message_size);
}
bool opengl_make_current(void* user)
{
eglMakeCurrent(egl->display, egl->surface, egl->surface, egl->context);
return true;
}
bool opengl_clear_current(void* user)
{
eglMakeCurrent(egl->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
return true;
}
struct gbm_bo *bo;
struct gbm_bo *next_bo;
static bool waiting_for_flip = false;
static struct pollfd drm_fds;
static drmEventContext drm_evctx;
static bool drm_wait_flip(int timeout)
{
drm_fds.revents = 0;
if (poll(&drm_fds, 1, timeout) < 0)
return false;
if (drm_fds.revents & (POLLHUP | POLLERR))
return false;
if (drm_fds.revents & POLLIN)
{
drmHandleEvent(drm->fd, &drm_evctx);
return true;
}
return false;
}
static bool wait_for_flip(bool block)
{
int timeout = 0;
if (!waiting_for_flip)
return false;
if (block)
timeout = -1;
while (waiting_for_flip)
{
if (!drm_wait_flip(timeout))
break;
}
if (waiting_for_flip)
return true;
gbm_surface_release_buffer(gbm->surface, bo);
bo = next_bo;
return false;
}
static bool queue_flip()
{
next_bo = gbm_surface_lock_front_buffer(gbm->surface);
struct drm_fb *fb = drm_fb_get_from_bo(next_bo);
if (drmModePageFlip(drm->fd, drm->crtc_id, fb->fb_id, DRM_MODE_PAGE_FLIP_EVENT, &waiting_for_flip) == 0)
return true;
return false;
}
static void swap_buffers()
{
eglSwapBuffers(egl->display, egl->surface);
if (wait_for_flip(0))
return;
waiting_for_flip = queue_flip();
if (gbm_surface_has_free_buffers(gbm->surface))
return;
wait_for_flip(true);
}
bool display_init()
{
drm_fds.fd = drm->fd;
drm_fds.events = POLLIN;
drm_evctx.version = DRM_EVENT_CONTEXT_VERSION;
drm_evctx.page_flip_handler = flip_handler;
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
eglSwapBuffers(egl->display, egl->surface);
bo = gbm_surface_lock_front_buffer(gbm->surface);
struct drm_fb *fb = drm_fb_get_from_bo(bo);
if (!fb)
{
printf("Failed to get a new framebuffer bo\n");
return false;
}
int ret = drmModeSetCrtc(drm->fd, drm->crtc_id, fb->fb_id, 0, 0, &drm->connector_id, 1, drm->mode);
if (ret)
{
printf("Failed to set mode: %s\n", strerror(errno));
return false;
}
return true;
}
bool opengl_present(void* user)
{
static bool first = true;
if (first)
{
perf_exit("time to first image");
first = false;
}
swap_buffers();
return true;
}
uint32_t opengl_fbo_callback(void* user) {
return 0;
}
void update_window_size(int width, int height)
{
FlutterWindowMetricsEvent event = {};
event.struct_size = sizeof(event);
event.width = width;
event.height = height;
event.pixel_ratio = 1.0;
FlutterEngineSendWindowMetricsEvent(engine, &event);
}
void input_loop()
{
int fd = open("/dev/input/event0", O_RDONLY);
if (fd < 0)
{
printf("Failed to open input event0\n");
return;
}
int curX = -1, curY = -1;
bool down = false;
while (1)
{
struct input_event event;
if (read(fd, &event, sizeof(event)) <= 0)
{
printf("Error reading from event0\n");
close(fd);
return;
}
if (event.type == EV_ABS)
{
if (event.code == ABS_MT_POSITION_X)
curX = event.value;
else if (event.code == ABS_MT_POSITION_Y)
curY = event.value;
// pick one dimension since we will get two events for every move
if (down && event.code == ABS_MT_POSITION_Y)
{
FlutterPointerEvent flutter_event;
flutter_event.struct_size = sizeof(flutter_event);
flutter_event.timestamp = (event.time.tv_sec * 1000000ull) + event.time.tv_usec;
flutter_event.x = curX;
flutter_event.y = curY;
flutter_event.phase = kMove;
FlutterEngineSendPointerEvent(engine, &flutter_event, 1);
}
}
else if (event.type == EV_KEY && event.code == BTN_TOUCH)
{
// printf("%s on %d,%d\n", event.value == 0 ? "Up" : "Down", curX, curY);
FlutterPointerEvent flutter_event;
flutter_event.struct_size = sizeof(flutter_event);
flutter_event.timestamp = (event.time.tv_sec * 1000000ull) + event.time.tv_usec;
flutter_event.x = curX;
flutter_event.y = curY;
flutter_event.phase = event.value == 0 ? kUp : kDown;
down = flutter_event.phase == kDown;
FlutterEngineSendPointerEvent(engine, &flutter_event, 1);
}
}
}
int main()
{
perf_enter();
const char* device = "/dev/dri/card0";
printf("Initializing DRM on %s\n", device);
drm = init_drm_legacy(device);
if (!drm)
{
printf("Failed to initialize DRM\n");
return 1;
}
printf("Resolution: %dx%d\n", drm->mode->hdisplay, drm->mode->vdisplay);
printf("Initializing GBM\n");
uint64_t modifier = DRM_FORMAT_MOD_INVALID;
gbm = init_gbm(drm->fd, drm->mode->hdisplay, drm->mode->vdisplay, modifier);
if (!gbm)
{
printf("Failed to initialize GBM\n");
return 1;
}
printf("Initializing EGL\n");
egl = egl_init(gbm);
if (!egl)
{
printf("Failed to initialize EGL\n");
return 1;
}
printf("Initializing display\n");
if (!display_init())
{
printf("Failed to initialize display\n");
return 1;
}
perf_exit("Platform initialization");
printf("Starting flutter\n");
FlutterRendererConfig renderer;
renderer.type = kOpenGL;
renderer.open_gl.struct_size = sizeof(FlutterOpenGLRendererConfig);
renderer.open_gl.make_current = opengl_make_current;
renderer.open_gl.clear_current = opengl_clear_current;
renderer.open_gl.present = opengl_present;
renderer.open_gl.fbo_callback = opengl_fbo_callback;
FlutterProjectArgs project;
project.struct_size = sizeof(FlutterProjectArgs);
project.assets_path = "project/";
project.main_path = ""; //"project/main.dart";
project.packages_path = ""; //"project/.packages";
project.icu_data_path = ".";
project.platform_message_callback = platform_message_callback;
project.command_line_argc = 2;
const char* argv[] = {"flutter_tester", "--aot-snapshot-path=."};
project.command_line_argv = argv;
perf_enter();
FlutterResult res = FlutterEngineRun(FLUTTER_ENGINE_VERSION,
&renderer, &project, NULL, &engine);
if (res == kSuccess)
{
update_window_size(drm->mode->hdisplay, drm->mode->vdisplay);
printf("Successfully started flutter engine: %p\n", engine);
input_loop();
FlutterEngineShutdown(engine);
return 0;
}
else
{
printf("Flutter engine error: %d\n", res);
return res;
}
}