Home | History | Annotate | Download | only in perf_tests
      1 //
      2 // Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
      3 // Use of this source code is governed by a BSD-style license that can be
      4 // found in the LICENSE file.
      5 //
      6 
      7 #include "SimpleBenchmark.h"
      8 #include <iostream>
      9 
     10 SimpleBenchmark::SimpleBenchmark(const std::string &name, size_t width, size_t height, EGLint glesMajorVersion, EGLint requestedRenderer)
     11     : mNumFrames(0),
     12       mName(name),
     13       mRunning(false),
     14       mDrawIterations(10),
     15       mRunTimeSeconds(5.0)
     16 {
     17     mOSWindow.reset(CreateOSWindow());
     18     mEGLWindow.reset(new EGLWindow(width, height, glesMajorVersion, requestedRenderer));
     19     mTimer.reset(CreateTimer());
     20 }
     21 
     22 bool SimpleBenchmark::initialize()
     23 {
     24     std::cout << "========= " << mName << " =========" << std::endl;
     25     return initializeBenchmark();
     26 }
     27 
     28 void SimpleBenchmark::destroy()
     29 {
     30     double totalTime = mTimer->getElapsedTime();
     31     std::cout << " - total time: " << totalTime << " sec" << std::endl;
     32     std::cout << " - frames: " << mNumFrames << std::endl;
     33     std::cout << " - average frame time: " << 1000.0 * totalTime / mNumFrames << " msec" << std::endl;
     34     std::cout << "=========" << std::endl << std::endl;
     35 
     36     destroyBenchmark();
     37 }
     38 
     39 void SimpleBenchmark::step(float dt, double totalTime)
     40 {
     41     stepBenchmark(dt, totalTime);
     42 }
     43 
     44 void SimpleBenchmark::draw()
     45 {
     46     if (mTimer->getElapsedTime() > mRunTimeSeconds) {
     47         mRunning = false;
     48         return;
     49     }
     50 
     51     ++mNumFrames;
     52 
     53     beginDrawBenchmark();
     54 
     55     for (unsigned int iteration = 0; iteration < mDrawIterations; ++iteration)
     56     {
     57         drawBenchmark();
     58     }
     59 
     60     endDrawBenchmark();
     61 }
     62 
     63 int SimpleBenchmark::run()
     64 {
     65     if (!mOSWindow->initialize(mName, mEGLWindow->getWidth(), mEGLWindow->getHeight()))
     66     {
     67         return -1;
     68     }
     69 
     70     if (!mEGLWindow->initializeGL(mOSWindow.get()))
     71     {
     72         return -1;
     73     }
     74 
     75     mRunning = true;
     76     int result = 0;
     77 
     78     if (!initialize())
     79     {
     80         mRunning = false;
     81         result = -1;
     82     }
     83 
     84     mTimer->start();
     85     double prevTime = 0.0;
     86 
     87     while (mRunning)
     88     {
     89         double elapsedTime = mTimer->getElapsedTime();
     90         double deltaTime = elapsedTime - prevTime;
     91 
     92         step(static_cast<float>(deltaTime), elapsedTime);
     93 
     94         // Clear events that the application did not process from this frame
     95         Event event;
     96         while (popEvent(&event))
     97         {
     98             // If the application did not catch a close event, close now
     99             if (event.Type == Event::EVENT_CLOSED)
    100             {
    101                 mRunning = false;
    102             }
    103         }
    104 
    105         if (!mRunning)
    106         {
    107             break;
    108         }
    109 
    110         draw();
    111         mEGLWindow->swap();
    112 
    113         mOSWindow->messageLoop();
    114 
    115         prevTime = elapsedTime;
    116     }
    117 
    118     destroy();
    119     mEGLWindow->destroyGL();
    120     mOSWindow->destroy();
    121 
    122     return result;
    123 }
    124 
    125 bool SimpleBenchmark::popEvent(Event *event)
    126 {
    127     return mOSWindow->popEvent(event);
    128 }
    129 
    130 OSWindow *SimpleBenchmark::getWindow()
    131 {
    132     return mOSWindow.get();
    133 }