]> git.cworth.org Git - apitrace-tests/blob - apps/egl/eglut/eglut_screen.c
Add egl apps.
[apitrace-tests] / apps / egl / eglut / eglut_screen.c
1 /*
2  * Copyright (C) 2010 LunarG Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  *
22  * Authors:
23  *    Chia-I Wu <olv@lunarg.com>
24  */
25
26 #include <stdio.h>
27 #include <string.h>
28 #include <sys/time.h>
29
30 #define EGL_EGLEXT_PROTOTYPES
31 #include "EGL/egl.h"
32 #include "EGL/eglext.h"
33
34 #include "eglutint.h"
35
36 #define MAX_MODES 100
37
38 static EGLScreenMESA kms_screen;
39 static EGLModeMESA kms_mode;
40 static EGLint kms_width, kms_height;
41
42 void
43 _eglutNativeInitDisplay(void)
44 {
45    _eglut->native_dpy = EGL_DEFAULT_DISPLAY;
46    _eglut->surface_type = EGL_SCREEN_BIT_MESA;
47 }
48
49 void
50 _eglutNativeFiniDisplay(void)
51 {
52    kms_screen = 0;
53    kms_mode = 0;
54    kms_width = 0;
55    kms_height = 0;
56 }
57
58 static void
59 init_kms(void)
60 {
61    EGLModeMESA modes[MAX_MODES];
62    EGLint num_screens, num_modes;
63    EGLint width, height, best_mode;
64    EGLint i;
65
66    if (!eglGetScreensMESA(_eglut->dpy, &kms_screen, 1, &num_screens) ||
67          !num_screens)
68       _eglutFatal("eglGetScreensMESA failed\n");
69
70    if (!eglGetModesMESA(_eglut->dpy, kms_screen,
71             modes, MAX_MODES, &num_modes) || !num_modes)
72       _eglutFatal("eglGetModesMESA failed!\n");
73
74    printf("Found %d modes:\n", num_modes);
75
76    best_mode = 0;
77    width = 0;
78    height = 0;
79    for (i = 0; i < num_modes; i++) {
80       EGLint w, h;
81       eglGetModeAttribMESA(_eglut->dpy, modes[i], EGL_WIDTH, &w);
82       eglGetModeAttribMESA(_eglut->dpy, modes[i], EGL_HEIGHT, &h);
83       printf("%3d: %d x %d\n", i, w, h);
84       if (w > width && h > height) {
85          width = w;
86          height = h;
87          best_mode = i;
88       }
89    }
90
91    printf("Will use screen size: %d x %d\n", width, height);
92
93    kms_mode = modes[best_mode];
94    kms_width = width;
95    kms_height = height;
96 }
97
98 void
99 _eglutNativeInitWindow(struct eglut_window *win, const char *title,
100                        int x, int y, int w, int h)
101 {
102    EGLint surf_attribs[16];
103    EGLint i;
104    const char *exts;
105
106    exts = eglQueryString(_eglut->dpy, EGL_EXTENSIONS);
107    if (!exts || !strstr(exts, "EGL_MESA_screen_surface"))
108       _eglutFatal("EGL_MESA_screen_surface is not supported\n");
109
110    init_kms();
111
112    i = 0;
113    surf_attribs[i++] = EGL_WIDTH;
114    surf_attribs[i++] = kms_width;
115    surf_attribs[i++] = EGL_HEIGHT;
116    surf_attribs[i++] = kms_height;
117    surf_attribs[i++] = EGL_NONE;
118
119    /* create surface */
120    win->native.u.surface = eglCreateScreenSurfaceMESA(_eglut->dpy,
121          win->config, surf_attribs);
122    if (win->native.u.surface == EGL_NO_SURFACE)
123       _eglutFatal("eglCreateScreenSurfaceMESA failed\n");
124
125    if (!eglShowScreenSurfaceMESA(_eglut->dpy, kms_screen,
126             win->native.u.surface, kms_mode))
127       _eglutFatal("eglShowScreenSurfaceMESA failed\n");
128
129    win->native.width = kms_width;
130    win->native.height = kms_height;
131 }
132
133 void
134 _eglutNativeFiniWindow(struct eglut_window *win)
135 {
136    eglShowScreenSurfaceMESA(_eglut->dpy,
137          kms_screen, EGL_NO_SURFACE, 0);
138    eglDestroySurface(_eglut->dpy, win->native.u.surface);
139 }
140
141 void
142 _eglutNativeEventLoop(void)
143 {
144    int start = _eglutNow();
145    int frames = 0;
146
147    _eglut->redisplay = 1;
148
149    while (1) {
150       struct eglut_window *win = _eglut->current;
151       int now = _eglutNow();
152
153       if (now - start > 5000) {
154          double elapsed = (double) (now - start) / 1000.0;
155
156          printf("%d frames in %3.1f seconds = %6.3f FPS\n",
157                frames, elapsed, frames / elapsed);
158          fflush(stdout);
159
160          start = now;
161          frames = 0;
162
163          /* send escape */
164          if (win->keyboard_cb)
165             win->keyboard_cb(27);
166       }
167
168       if (_eglut->idle_cb)
169          _eglut->idle_cb();
170
171       if (_eglut->redisplay) {
172          _eglut->redisplay = 0;
173
174          if (win->display_cb)
175             win->display_cb();
176          eglSwapBuffers(_eglut->dpy, win->surface);
177          frames++;
178       }
179    }
180 }