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