Merge branch '7.8'
[mesa.git] / progs / egl / eglut / eglut_screen.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/time.h>
4
5 #define EGL_EGLEXT_PROTOTYPES
6 #include "EGL/egl.h"
7 #include "EGL/eglext.h"
8
9 #include "eglutint.h"
10
11 #define MAX_MODES 100
12
13 static EGLScreenMESA kms_screen;
14 static EGLModeMESA kms_mode;
15 static EGLint kms_width, kms_height;
16
17 void
18 _eglutNativeInitDisplay(void)
19 {
20 _eglut->native_dpy = EGL_DEFAULT_DISPLAY;
21 _eglut->surface_type = EGL_SCREEN_BIT_MESA;
22 }
23
24 void
25 _eglutNativeFiniDisplay(void)
26 {
27 kms_screen = 0;
28 kms_mode = 0;
29 kms_width = 0;
30 kms_height = 0;
31 }
32
33 static void
34 init_kms(void)
35 {
36 EGLModeMESA modes[MAX_MODES];
37 EGLint num_screens, num_modes;
38 EGLint width, height, best_mode;
39 EGLint i;
40
41 if (!eglGetScreensMESA(_eglut->dpy, &kms_screen, 1, &num_screens) ||
42 !num_screens)
43 _eglutFatal("eglGetScreensMESA failed\n");
44
45 if (!eglGetModesMESA(_eglut->dpy, kms_screen,
46 modes, MAX_MODES, &num_modes) || !num_modes)
47 _eglutFatal("eglGetModesMESA failed!\n");
48
49 printf("Found %d modes:\n", num_modes);
50
51 best_mode = 0;
52 width = 0;
53 height = 0;
54 for (i = 0; i < num_modes; i++) {
55 EGLint w, h;
56 eglGetModeAttribMESA(_eglut->dpy, modes[i], EGL_WIDTH, &w);
57 eglGetModeAttribMESA(_eglut->dpy, modes[i], EGL_HEIGHT, &h);
58 printf("%3d: %d x %d\n", i, w, h);
59 if (w > width && h > height) {
60 width = w;
61 height = h;
62 best_mode = i;
63 }
64 }
65
66 printf("Will use screen size: %d x %d\n", width, height);
67
68 kms_mode = modes[best_mode];
69 kms_width = width;
70 kms_height = height;
71 }
72
73 void
74 _eglutNativeInitWindow(struct eglut_window *win, const char *title,
75 int x, int y, int w, int h)
76 {
77 EGLint surf_attribs[16];
78 EGLint i;
79 const char *exts;
80
81 exts = eglQueryString(_eglut->dpy, EGL_EXTENSIONS);
82 if (!exts || !strstr(exts, "EGL_MESA_screen_surface"))
83 _eglutFatal("EGL_MESA_screen_surface is not supported\n");
84
85 init_kms();
86
87 i = 0;
88 surf_attribs[i++] = EGL_WIDTH;
89 surf_attribs[i++] = kms_width;
90 surf_attribs[i++] = EGL_HEIGHT;
91 surf_attribs[i++] = kms_height;
92 surf_attribs[i++] = EGL_NONE;
93
94 /* create surface */
95 win->native.u.surface = eglCreateScreenSurfaceMESA(_eglut->dpy,
96 win->config, surf_attribs);
97 if (win->native.u.surface == EGL_NO_SURFACE)
98 _eglutFatal("eglCreateScreenSurfaceMESA failed\n");
99
100 if (!eglShowScreenSurfaceMESA(_eglut->dpy, kms_screen,
101 win->native.u.surface, kms_mode))
102 _eglutFatal("eglShowScreenSurfaceMESA failed\n");
103
104 win->native.width = kms_width;
105 win->native.height = kms_height;
106 }
107
108 void
109 _eglutNativeFiniWindow(struct eglut_window *win)
110 {
111 eglShowScreenSurfaceMESA(_eglut->dpy,
112 kms_screen, EGL_NO_SURFACE, 0);
113 eglDestroySurface(_eglut->dpy, win->native.u.surface);
114 }
115
116 void
117 _eglutNativeEventLoop(void)
118 {
119 int start = _eglutNow();
120 int frames = 0;
121
122 _eglut->redisplay = 1;
123
124 while (1) {
125 struct eglut_window *win = _eglut->current;
126 int now = _eglutNow();
127
128 if (now - start > 5000) {
129 double elapsed = (double) (now - start) / 1000.0;
130
131 printf("%d frames in %3.1f seconds = %6.3f FPS\n",
132 frames, elapsed, frames / elapsed);
133
134 start = now;
135 frames = 0;
136
137 /* send escape */
138 if (win->keyboard_cb)
139 win->keyboard_cb(27);
140 }
141
142 if (_eglut->idle_cb)
143 _eglut->idle_cb();
144
145 if (_eglut->redisplay) {
146 _eglut->redisplay = 0;
147
148 if (win->display_cb)
149 win->display_cb();
150 eglSwapBuffers(_eglut->dpy, win->surface);
151 frames++;
152 }
153 }
154 }