Added missing includes
[glmemperf] / glmemperf.cpp
1 /**
2  * OpenGL ES 2.0 memory performance estimator
3  * Copyright (C) 2009 Nokia
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * \author Sami Kyöstilä <sami.kyostila@nokia.com>
20  */
21 #include <GLES2/gl2.h>
22 #include <GLES2/gl2ext.h>
23 #include <X11/Xutil.h>
24 #include <EGL/egl.h>
25 #include <string>
26 #include <unistd.h>
27 #include <time.h>
28 #include <memory>
29 #include <list>
30 #include <iostream>
31 #include <algorithm>
32 #include <sys/stat.h>
33
34 #include "native.h"
35 #include "util.h"
36 #include "test.h"
37 #include "blittest.h"
38 #include "cleartest.h"
39 #include "pixmapblittest.h"
40 #include "fboblittest.h"
41 #include "shaderblittest.h"
42 #include "cpuinterleavingtest.h"
43 #include "config.h"
44
45 #if defined(HAVE_LIBOSSO)
46 #include <libosso.h>
47 #endif
48
49 /**
50  *  Command line options
51  */
52 static struct
53 {
54     int                    bitsPerPixel;
55     bool                   verbose;
56     int                    minTime;
57     bool                   listTests;
58     std::list<std::string> includedTests;
59     std::list<std::string> excludedTests;
60 } options;
61
62 /** Shared EGL objects */
63 struct Context ctx;
64
65 #if defined(HAVE_LIBOSSO)
66 osso_context_t* ossoContext;
67 #endif
68
69 bool initializeEgl(int width, int height, const EGLint* configAttrs, const EGLint* contextAttrs)
70 {
71     EGLint configCount = 0;
72     
73 #if defined(HAVE_LIBOSSO)
74     ossoContext = osso_initialize("com.nokia.memperf", "1.0", FALSE, NULL);
75     if (!ossoContext)
76     {
77         perror("osso_initialize");
78         goto out_error;
79     }
80 #endif
81
82     ctx.dpy = eglGetDisplay(ctx.nativeDisplay);
83     ASSERT_EGL();
84    
85     eglInitialize(ctx.dpy, NULL, NULL);
86     eglChooseConfig(ctx.dpy, configAttrs, &ctx.config, 1, &configCount);
87     ASSERT_EGL();
88
89     if (!configCount)
90     {
91         printf("Config not found\n");
92         goto out_error;
93     }
94
95     if (options.verbose)
96     {
97         printf("Config attributes:\n");
98         dumpConfig(ctx.dpy, ctx.config);
99     }
100
101     if (!nativeCreateWindow(ctx.nativeDisplay, ctx.dpy, ctx.config, __FILE__,
102                             width, height, &ctx.win))
103     {
104         printf("Unable to create a window\n");
105         goto out_error;
106     }
107
108     ctx.context = eglCreateContext(ctx.dpy, ctx.config, EGL_NO_CONTEXT, contextAttrs);
109     ASSERT_EGL();
110     if (!ctx.context)
111     {
112         printf("Unable to create a context\n");
113         goto out_error;
114     }
115     
116     ctx.surface = eglCreateWindowSurface(ctx.dpy, ctx.config, ctx.win, NULL);
117     ASSERT_EGL();
118     if (!ctx.surface)
119     {
120         printf("Unable to create a surface\n");
121         goto out_error;
122     }
123
124     eglMakeCurrent(ctx.dpy, ctx.surface, ctx.surface, ctx.context);
125     ASSERT_EGL();
126     
127     eglSwapInterval(ctx.dpy, 0);
128     return true;
129
130 out_error:
131     eglMakeCurrent(ctx.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
132     eglDestroySurface(ctx.dpy, ctx.surface);
133     eglDestroyContext(ctx.dpy, ctx.context);
134     eglTerminate(ctx.dpy);
135     nativeDestroyWindow(ctx.nativeDisplay, ctx.win);
136     nativeDestroyDisplay(ctx.nativeDisplay);
137     return false;
138 }
139
140 void terminateEgl()
141 {
142     eglMakeCurrent(ctx.dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
143     eglDestroySurface(ctx.dpy, ctx.surface);
144     eglDestroyContext(ctx.dpy, ctx.context);
145     eglTerminate(ctx.dpy);
146     nativeDestroyWindow(ctx.nativeDisplay, ctx.win);
147     nativeDestroyDisplay(ctx.nativeDisplay);
148
149 #if defined(HAVE_LIBOSSO)
150     osso_deinitialize(ossoContext);
151     ossoContext = 0;
152 #endif
153 }
154
155 int64_t timeDiff(const struct timespec& start, const struct timespec& end)
156 {
157     int64_t s = start.tv_sec * (1000 * 1000 * 1000LL) + start.tv_nsec;
158     int64_t e =   end.tv_sec * (1000 * 1000 * 1000LL) +   end.tv_nsec;
159     return e - s;
160 }
161
162 bool shouldRunTest(const std::string& testName)
163 {
164     std::list<std::string>::iterator i;
165     bool result = true;
166
167     if (options.includedTests.size())
168     {
169         result = false;
170         for (i = options.includedTests.begin(); i != options.includedTests.end(); ++i)
171         {
172             if (testName.find(*i) != std::string::npos)
173             {
174                 result = true;
175                 break;
176             }
177         }
178     }
179
180     for (i = options.excludedTests.begin(); i != options.excludedTests.end(); ++i)
181     {
182         if (testName.find(*i) != std::string::npos)
183         {
184             result = false;
185             break;
186         }
187     }
188
189     return result;
190 }
191
192 void runTest(Test& test)
193 {
194     int frames = 0;
195     int frameLimit = 100;
196     int warmup = 20;
197     int64_t minTime = options.minTime * 1000 * 1000 * 1000LL;
198     struct timespec res, start, end;
199
200     if (options.listTests)
201     {
202         printf("%s\n", test.name().c_str());
203         return;
204     }
205
206     if (!shouldRunTest(test.name()))
207     {
208         return;
209     }
210
211     clock_getres(CLOCK_REALTIME, &res);
212     //printf("Timer resolution: %d.%09d s\n", res.tv_sec, res.tv_nsec);
213     printf("%-40s", (test.name() + ":").c_str());
214     fflush(stdout);
215
216     try
217     {
218         test.prepare();
219         ASSERT_GL();
220         ASSERT_EGL();
221     } catch (const std::exception& e)
222     {
223         printf("%s\n", e.what());
224         return;
225     }
226
227     while (warmup--)
228     {
229         test(0);
230         swapBuffers();
231     }
232
233     ASSERT_GL();
234     ASSERT_EGL();
235
236 #if defined(HAVE_LIBOSSO)
237     osso_display_blanking_pause(ossoContext);
238 #endif
239
240     clock_gettime(CLOCK_REALTIME, &start);
241     while (frames < frameLimit)
242     {
243         test(frames);
244         swapBuffers();
245         clock_gettime(CLOCK_REALTIME, &end);
246         frames++;
247         if (frames >= frameLimit && timeDiff(start, end) < minTime)
248         {
249             frameLimit *= 2;
250         }
251     }
252
253     ASSERT_GL();
254     ASSERT_EGL();
255     
256     test.teardown();
257     ASSERT_GL();
258     ASSERT_EGL();
259
260     int64_t diff = timeDiff(start, end);
261     int fps = static_cast<int>((1000 * 1000 * 1000LL * frames) / diff);
262     //printf("%d frames in %6.2f ms (%3d fps) ", frames, diff / (1000.0f * 1000.0f), fps);
263     printf("%3d fps ", fps);
264
265     while (fps > 0)
266     {
267         fputc('#', stdout);
268         fps -= 3;
269     }
270     fputc('\n', stdout);
271 }
272
273 void getScreenSize(int& width, int& height)
274 {
275     Window rootWindow = DefaultRootWindow(ctx.nativeDisplay);
276     XWindowAttributes rootAttrs;
277
278     XGetWindowAttributes(ctx.nativeDisplay, rootWindow, &rootAttrs);
279
280     width = rootAttrs.width;
281     height = rootAttrs.height;
282 }
283
284 void showIntro()
285 {
286     std::cout << 
287         "GLMemPerf v" PACKAGE_VERSION " - OpenGL ES 2.0 memory performance benchmark\n"
288         "Copyright (C) 2009 Nokia Corporation. GLMemPerf comes with ABSOLUTELY\n"
289         "NO WARRANTY; This is free software, and you are welcome to redistribute\n"
290         "it under certain conditions.\n"
291         "\n";
292 }
293
294 void showUsage()
295 {
296     std::cout << 
297         "Usage:\n"
298         "       glmemperf [OPTIONS]\n"
299         "Options:\n"
300         "       -h             This text\n"
301         "       -v             Verbose mode\n"
302         "       -l             List all tests without running them\n"
303         "       -i TEST        Include a specific test (full name or substring)\n"
304         "       -e TEST        Exclude a specific test (full name or substring)\n"
305         "       -t SECS        Minimum time to run each test\n"
306         "       -b BPP         Bits per pixel\n";
307 }
308
309 void parseArguments(const std::list<std::string>& args)
310 {
311     std::list<std::string>::const_iterator i;
312
313     // Set up defaults
314     options.verbose = false;
315     options.minTime = 1;
316     options.bitsPerPixel = 16;
317     options.listTests = false;
318
319     for (i = args.begin(), i++; i != args.end(); ++i)
320     {
321         if (*i == "-h" || *i == "--help")
322         {
323             showUsage();
324             exit(0);
325         }
326         else if (*i == "-i" && ++i != args.end())
327         {
328             options.includedTests.push_back(*i);
329         }
330         else if (*i == "-e" && ++i != args.end())
331         {
332             options.excludedTests.push_back(*i);
333         }
334         else if (*i == "-t" && ++i != args.end())
335         {
336             options.minTime = atoi((*i).c_str());
337         }
338         else if (*i == "-b" && ++i != args.end())
339         {
340             options.bitsPerPixel = atoi((*i).c_str());
341         }
342         else if (*i == "-v")
343         {
344             options.verbose = true;
345         }
346         else if (*i == "-l")
347         {
348             options.listTests = true;
349         }
350         else
351         {
352             std::cerr << "Invalid option: " << *i << std::endl;
353             showUsage();
354             exit(1);
355         }
356     }
357 }
358
359 void findDataDirectory()
360 {
361     struct stat st;
362     if (stat("data", &st) == 0)
363     {
364         return;
365     }
366     chdir(PREFIX "/share/glmemperf/");
367     assert(stat("data", &st) == 0);
368 }
369
370 #define ADD_TEST(TEST) runTest(*std::auto_ptr<Test>(new TEST));
371
372 int main(int argc, char** argv)
373 {
374     std::list<std::string> args(argv, argv + argc);
375     
376     showIntro();
377     parseArguments(args);
378     findDataDirectory();
379
380     const EGLint configAttrs[] =
381     {
382         EGL_BUFFER_SIZE, options.bitsPerPixel,
383         EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
384         EGL_NONE
385     };
386
387     const EGLint configAttrs32[] =
388     {
389         EGL_BUFFER_SIZE, 32,
390         EGL_NONE
391     };
392
393     const EGLint contextAttrs[] =
394     {
395         EGL_CONTEXT_CLIENT_VERSION, 2,
396         EGL_NONE
397     };
398     int winWidth = 800;
399     int winHeight = 480;
400     const float w = winWidth, h = winHeight;
401     EGLConfig config32 = 0;
402     EGLint configCount = 0;
403
404     bool result = nativeCreateDisplay(&ctx.nativeDisplay);
405     assert(result);
406
407     getScreenSize(winWidth, winHeight);
408     result = initializeEgl(winWidth, winHeight, configAttrs, contextAttrs);
409     assert(result);
410
411     eglChooseConfig(ctx.dpy, configAttrs32, &config32, 1, &configCount);
412
413     if (!configCount)
414     {
415         printf("32bpp config not found\n");
416     }
417
418     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
419     ASSERT_GL();
420
421     // Clear test
422     ADD_TEST(ClearTest());
423
424     // Normal blits
425     ADD_TEST(BlitTest(GL_RGBA, GL_UNSIGNED_BYTE,              800, 480, "data/water2_800x480_rgba8888.raw"));
426     ADD_TEST(BlitTest(GL_RGB,  GL_UNSIGNED_BYTE,              800, 480, "data/water2_800x480_rgb888.raw"));
427     ADD_TEST(BlitTest(GL_RGBA, GL_UNSIGNED_BYTE,             1024, 512, "data/digital_nature2_1024x512_rgba8888.raw", false, 800.0 / 1024, 480.0 / 512));
428     ADD_TEST(BlitTest(GL_RGB,  GL_UNSIGNED_SHORT_5_6_5,       800, 480, "data/water2_800x480_rgb565.raw"));
429     ADD_TEST(BlitTest(GL_RGB,  GL_UNSIGNED_SHORT_5_6_5,      1024, 512, "data/digital_nature2_1024x512_rgb565.raw", false, 800.0 / 1024, 480.0 / 512));
430     ADD_TEST(BlitTest(GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, 0, 1024, 512, "data/abstract3_1024x512_pvrtc4.raw", false, 800.0 / 1024, 480.0 / 512));
431     ADD_TEST(BlitTest(GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, 0, 1024, 512, "data/abstract3_1024x512_pvrtc2.raw", false, 800.0 / 1024, 480.0 / 512));
432     ADD_TEST(BlitTest(GL_ETC1_RGB8_OES,                   0, 1024, 512, "data/abstract3_1024x512_etc1.raw", false, 800.0 / 1024, 480.0 / 512));
433     ADD_TEST(PixmapBlitTest(w, h, ctx.config));
434     ADD_TEST(PixmapBlitTest(w, h, config32));
435     ADD_TEST(FBOBlitTest(GL_RGBA, GL_UNSIGNED_BYTE,          w, h));
436     ADD_TEST(FBOBlitTest(GL_RGBA, GL_UNSIGNED_BYTE,          1024, 512, false, w / 1024, h / 512));
437     ADD_TEST(FBOBlitTest(GL_RGB,  GL_UNSIGNED_SHORT_5_6_5,   w, h));
438     ADD_TEST(FBOBlitTest(GL_RGB,  GL_UNSIGNED_SHORT_5_6_5,   1024, 512, false, w / 1042, h / 512));
439
440     // Rotated blits
441     ADD_TEST(BlitTest(GL_RGBA, GL_UNSIGNED_BYTE,              480,  800, "data/water2_480x800_rgba8888.raw", true));
442     ADD_TEST(BlitTest(GL_RGB,  GL_UNSIGNED_BYTE,              480,  800, "data/water2_480x800_rgb888.raw", true));
443     ADD_TEST(BlitTest(GL_RGBA, GL_UNSIGNED_BYTE,              512, 1024, "data/digital_nature2_512x1024_rgba8888.raw", true, 480.0 / 512, 800.0 / 1024));
444     ADD_TEST(BlitTest(GL_RGB,  GL_UNSIGNED_SHORT_5_6_5,       480,  800, "data/water2_480x800_rgb565.raw", true));
445     ADD_TEST(BlitTest(GL_RGB,  GL_UNSIGNED_SHORT_5_6_5,       512, 1024, "data/digital_nature2_512x1024_rgb565.raw", true, 480.0 / 512, 800.0 / 1024));
446     ADD_TEST(BlitTest(GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, 0,  512, 1024, "data/abstract3_512x1024_pvrtc4.raw", true, 480.0 / 512, 800.0 / 1024));
447     ADD_TEST(BlitTest(GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, 0,  512, 1024, "data/abstract3_512x1024_pvrtc2.raw", true, 480.0 / 512, 800.0 / 1024));
448     ADD_TEST(BlitTest(GL_ETC1_RGB8_OES,                   0,  512, 1024, "data/abstract3_512x1024_etc1.raw", true, 480.0 / 512, 800.0 / 1024));
449     ADD_TEST(PixmapBlitTest(h, w, ctx.config, true));
450     ADD_TEST(PixmapBlitTest(h, w, config32,   true));
451     ADD_TEST(FBOBlitTest(GL_RGBA, GL_UNSIGNED_BYTE,        w, h, true, h / w, w / h));
452     ADD_TEST(FBOBlitTest(GL_RGBA, GL_UNSIGNED_BYTE,        1024, 512, true, h / 512, w / 1024));
453     ADD_TEST(FBOBlitTest(GL_RGB,  GL_UNSIGNED_SHORT_5_6_5, w, h, true, h / w, w / h));
454     ADD_TEST(FBOBlitTest(GL_RGB,  GL_UNSIGNED_SHORT_5_6_5, 1024, 512, true, h / 512, w / 1024));
455
456     int gridW = 5;
457     int gridH = 3;
458     float w2 = winWidth  / gridW;
459     float h2 = winHeight / gridH;
460
461     glEnable(GL_BLEND);
462     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
463
464     // Small blended blits
465     ADD_TEST(BlitTest(GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4,     128, 128, "data/xorg_128x128_rgba4444.raw", false, gridW, gridH, 128.0 / w2, 128.0 / h2));
466     ADD_TEST(BlitTest(GL_RGBA, GL_UNSIGNED_BYTE,              127, 127, "data/xorg_127x127_rgba8888.raw", false, gridW, gridH, 127.0 / w2, 127.0 / h2));
467     ADD_TEST(BlitTest(GL_RGBA, GL_UNSIGNED_BYTE,              128, 128, "data/xorg_128x128_rgba8888.raw", false, gridW, gridH, 128.0 / w2, 128.0 / h2));
468     ADD_TEST(BlitTest(GL_RGB, GL_UNSIGNED_SHORT_5_6_5,        127, 127, "data/xorg_127x127_rgb565.raw",   false, gridW, gridH, 127.0 / w2, 127.0 / h2));
469     ADD_TEST(BlitTest(GL_RGB, GL_UNSIGNED_SHORT_5_6_5,        128, 128, "data/xorg_128x128_rgb565.raw",   false, gridW, gridH, 128.0 / w2, 128.0 / h2));
470     ADD_TEST(BlitTest(GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, 0, 128, 128, "data/xorg_128x128_pvrtc4.raw",   false, gridW, gridH, 128.0 / w2, 128.0 / h2));
471     ADD_TEST(BlitTest(GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, 0, 128, 128, "data/xorg_128x128_pvrtc2.raw",   false, gridW, gridH, 128.0 / w2, 128.0 / h2));
472     ADD_TEST(BlitTest(GL_ETC1_RGB8_OES,                    0, 128, 128, "data/xorg_128x128_etc1.raw",     false, gridW, gridH, 128.0 / w2, 128.0 / h2));
473     ADD_TEST(ShaderBlitTest("mask", 128, 128, gridW, gridH * 0.5f, 128.0 / w2, 128.0 / h2));
474
475     // Rotated small blended blits
476     ADD_TEST(BlitTest(GL_RGBA, GL_UNSIGNED_BYTE,              127, 127, "data/xorg_127x127_rgba8888.raw", true, gridH, gridW, 127.0 / w2, 127.0 / h2));
477     ADD_TEST(BlitTest(GL_RGBA, GL_UNSIGNED_BYTE,              128, 128, "data/xorg_128x128_rgba8888.raw", true, gridH, gridW, 128.0 / w2, 128.0 / h2));
478     ADD_TEST(BlitTest(GL_RGB, GL_UNSIGNED_SHORT_5_6_5,        127, 127, "data/xorg_127x127_rgb565.raw",   true, gridH, gridW, 127.0 / w2, 127.0 / h2));
479     ADD_TEST(BlitTest(GL_RGB, GL_UNSIGNED_SHORT_5_6_5,        128, 128, "data/xorg_128x128_rgb565.raw",   true, gridH, gridW, 128.0 / w2, 128.0 / h2));
480     ADD_TEST(BlitTest(GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, 0, 128, 128, "data/xorg_128x128_pvrtc4.raw",   true, gridH, gridW, 128.0 / w2, 128.0 / h2));
481     ADD_TEST(BlitTest(GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, 0, 128, 128, "data/xorg_128x128_pvrtc2.raw",   true, gridH, gridW, 128.0 / w2, 128.0 / h2));
482     ADD_TEST(BlitTest(GL_ETC1_RGB8_OES,                    0, 128, 128, "data/xorg_128x128_etc1.raw",     true, gridH, gridW, 128.0 / w2, 128.0 / h2));
483
484     glDisable(GL_BLEND);
485
486     // Shader tests
487     ADD_TEST(ShaderBlitTest("const", w, h));
488     ADD_TEST(ShaderBlitTest("lingrad", w, h));
489     ADD_TEST(ShaderBlitTest("radgrad", w, h));
490     ADD_TEST(ShaderBlitTest("palette", w, h));
491     
492     // CPU interleaving
493     ADD_TEST(CPUInterleavingTest(CPUI_XSHM_IMAGE, 2, 16, winWidth, winHeight));
494     ADD_TEST(CPUInterleavingTest(CPUI_XSHM_IMAGE, 2, 32, winWidth, winHeight));
495     ADD_TEST(CPUInterleavingTest(CPUI_TEXTURE_UPLOAD, 2, 16, winWidth, winHeight));
496     ADD_TEST(CPUInterleavingTest(CPUI_TEXTURE_UPLOAD, 2, 32, winWidth, winHeight));
497
498     terminateEgl();
499 }