add benchmark mode that shows the real frame time rather than the vsync time

This commit is contained in:
Konstantin Käfer
2015-08-10 17:14:00 +03:00
parent ca8ffcc456
commit 21ec505648
7 changed files with 96 additions and 20 deletions

View File

@@ -164,6 +164,7 @@ public:
void setDebug(bool value);
void toggleDebug();
bool getDebug() const;
void setNeedsRepaint();
void setCollisionDebug(bool value);
void toggleCollisionDebug();
bool getCollisionDebug() const;

View File

@@ -12,7 +12,7 @@
class GLFWView : public mbgl::View {
public:
GLFWView(bool fullscreen = false);
GLFWView(bool fullscreen = false, bool benchmark = false);
~GLFWView();
float getPixelRatio() const override;
@@ -42,7 +42,7 @@ public:
void setWindowTitle(const std::string&);
void run();
void fps();
void report(float duration);
private:
mbgl::LatLng makeRandomPoint() const;
@@ -61,9 +61,15 @@ private:
private:
bool fullscreen = false;
const bool benchmark = false;
bool tracking = false;
bool rotating = false;
// Frame timer
int frames = 0;
float frameTime = 0;
double lastReported = 0;
int width = 1024;
int height = 768;
int fbWidth;

View File

@@ -32,17 +32,19 @@ void quit_handler(int) {
int main(int argc, char *argv[]) {
bool fullscreen = false;
bool benchmark = false;
std::string style;
const struct option long_options[] = {
{"fullscreen", no_argument, 0, 'f'},
{"benchmark", no_argument, 0, 'b'},
{"style", required_argument, 0, 's'},
{0, 0, 0, 0}
};
while (true) {
int option_index = 0;
int opt = getopt_long(argc, argv, "fs:", long_options, &option_index);
int opt = getopt_long(argc, argv, "fbs:", long_options, &option_index);
if (opt == -1) break;
switch (opt)
{
@@ -52,6 +54,9 @@ int main(int argc, char *argv[]) {
case 'f':
fullscreen = true;
break;
case 'b':
benchmark = true;
break;
case 's':
style = std::string("asset://") + std::string(optarg);
default:
@@ -67,7 +72,11 @@ int main(int argc, char *argv[]) {
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
view = std::make_unique<GLFWView>(fullscreen);
if (benchmark) {
mbgl::Log::Info(mbgl::Event::General, "BENCHMARK MODE: Some optimizations are disabled.");
}
view = std::make_unique<GLFWView>(fullscreen, benchmark);
mbgl::SQLiteCache cache("/tmp/mbgl-cache.db");
mbgl::DefaultFileSource fileSource(&cache);

View File

@@ -12,6 +12,19 @@
#import <Foundation/Foundation.h>
#pragma GCC diagnostic push
#ifndef __clang__
#pragma GCC diagnostic ignored "-Wunused-local-typedefs"
#pragma GCC diagnostic ignored "-Wshadow"
#endif
#include <boost/program_options.hpp>
#pragma GCC diagnostic pop
#include <iostream>
namespace po = boost::program_options;
@interface URLHandler : NSObject
@property (nonatomic) mbgl::Map *map;
@@ -101,8 +114,33 @@ const std::string &defaultCacheDatabase() {
return path;
}
int main() {
GLFWView view;
int main(int argc, char* argv[]) {
bool fullscreen = false;
bool benchmark = false;
std::string style;
po::options_description desc("Allowed options");
desc.add_options()
("fullscreen,f", po::bool_switch(&fullscreen)->default_value(fullscreen), "Fullscreen mode")
("style,s", po::value(&style)->value_name("json"), "Map stylesheet")
("benchmark,b", po::bool_switch(&benchmark)->default_value(benchmark), "Benchmark mode")
;
try {
auto parsed = po::command_line_parser(argc, argv).options(desc).allow_unregistered().run();
po::variables_map vm;
po::store(parsed, vm);
po::notify(vm);
} catch(std::exception& e) {
std::cout << "Error: " << e.what() << std::endl << desc;
exit(1);
}
if (benchmark) {
mbgl::Log::Info(mbgl::Event::General, "BENCHMARK MODE: Some optimizations are disabled.");
}
GLFWView view(fullscreen, benchmark);
mbgl::SQLiteCache cache(defaultCacheDatabase());
mbgl::DefaultFileSource fileSource(&cache);
@@ -146,10 +184,15 @@ int main() {
mbgl::Log::Info(mbgl::Event::Setup, std::string("Changed style to: ") + newStyle.first);
});
// Load style
const auto& newStyle = mbgl::util::defaultStyles.front();
map.setStyleURL(newStyle.first);
view.setWindowTitle(newStyle.second);
if (style.empty()) {
const auto& newStyle = mbgl::util::defaultStyles.front();
style = newStyle.first;
view.setWindowTitle(newStyle.second);
}
map.setStyleURL(style);
view.run();

View File

@@ -43,6 +43,7 @@
],
'libraries': [
'<@(glfw_static_libs)',
'<@(boost_libprogram_options_static_libs)'
],
},

View File

@@ -15,7 +15,8 @@ void glfwError(int error, const char *description) {
assert(false);
}
GLFWView::GLFWView(bool fullscreen_) : fullscreen(fullscreen_) {
GLFWView::GLFWView(bool fullscreen_, bool benchmark_)
: fullscreen(fullscreen_), benchmark(benchmark_) {
glfwSetErrorCallback(glfwError);
std::srand(std::time(0));
@@ -56,7 +57,13 @@ GLFWView::GLFWView(bool fullscreen_) : fullscreen(fullscreen_) {
glfwSetWindowUserPointer(window, this);
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
if (benchmark) {
// Disables vsync on platforms that support it.
glfwSwapInterval(0);
} else {
glfwSwapInterval(1);
}
glfwSetCursorPosCallback(window, onMouseMove);
glfwSetMouseButtonCallback(window, onMouseClick);
@@ -336,7 +343,12 @@ void GLFWView::run() {
glfwWaitEvents();
const bool dirty = !clean.test_and_set();
if (dirty) {
const double started = glfwGetTime();
map->renderSync();
report(1000 * (glfwGetTime() - started));
if (benchmark) {
map->setNeedsRepaint();
}
map->nudgeTransitions();
}
}
@@ -373,20 +385,20 @@ void GLFWView::invalidate() {
void GLFWView::swap() {
glfwSwapBuffers(window);
fps();
}
void GLFWView::fps() {
static int frames = 0;
static double timeElapsed = 0;
void GLFWView::report(float duration) {
frames++;
double currentTime = glfwGetTime();
frameTime += duration;
if (currentTime - timeElapsed >= 1) {
mbgl::Log::Info(mbgl::Event::OpenGL, "FPS: %4.2f", frames / (currentTime - timeElapsed));
timeElapsed = currentTime;
const double currentTime = glfwGetTime();
if (currentTime - lastReported >= 1) {
frameTime /= frames;
mbgl::Log::Info(mbgl::Event::OpenGL, "Frame time: %6.2fms (%6.2f fps)", frameTime,
1000 / frameTime);
frames = 0;
frameTime = 0;
lastReported = currentTime;
}
}

View File

@@ -375,6 +375,10 @@ bool Map::getDebug() const {
return data->getDebug();
}
void Map::setNeedsRepaint() {
data->setNeedsRepaint(true);
}
void Map::setCollisionDebug(bool value) {
data->setCollisionDebug(value);
update(Update::Repaint);