2
* Copyright © 2012 Linaro Limited
4
* This file is part of the glmark2 OpenGL (ES) 2.0 benchmark.
6
* glmark2 is free software: you can redistribute it and/or modify it under the
7
* terms of the GNU General Public License as published by the Free Software
8
* Foundation, either version 3 of the License, or (at your option) any later
11
* glmark2 is distributed in the hope that it will be useful, but WITHOUT ANY
12
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16
* You should have received a copy of the GNU General Public License along with
17
* glmark2. If not, see <http://www.gnu.org/licenses/>.
23
#include "main-loop.h"
34
MainLoop::MainLoop(Canvas &canvas, const std::vector<Benchmark *> &benchmarks) :
35
canvas_(canvas), benchmarks_(benchmarks)
45
scene_setup_status_ = SceneSetupStatusUnknown;
48
bench_iter_ = benchmarks_.begin();
55
return score_ / benchmarks_run_;
63
/* Find the next normal scene */
65
/* Find a normal scene */
66
while (bench_iter_ != benchmarks_.end()) {
67
scene_ = &(*bench_iter_)->scene();
70
* Scenes with empty names are option-setting scenes.
71
* Just set them up and continue with the search.
73
if (scene_->name().empty())
74
(*bench_iter_)->setup_scene();
81
/* If we have found a valid scene, set it up */
82
if (bench_iter_ != benchmarks_.end()) {
83
if (!Options::reuse_context)
86
scene_ = &(*bench_iter_)->setup_scene();
87
if (!scene_->running()) {
88
if (!scene_->supported(false))
89
scene_setup_status_ = SceneSetupStatusUnsupported;
91
scene_setup_status_ = SceneSetupStatusFailure;
94
scene_setup_status_ = SceneSetupStatusSuccess;
100
/* ... otherwise we are done */
105
bool should_quit = canvas_.should_quit();
107
if (scene_ ->running() && !should_quit)
111
* Need to recheck whether the scene is still running, because code
112
* in draw() may have changed the state.
114
if (!scene_->running() || should_quit) {
115
if (scene_setup_status_ == SceneSetupStatusSuccess) {
116
score_ += scene_->average_fps();
120
(*bench_iter_)->teardown_scene();
140
MainLoop::log_scene_info()
142
Log::info("%s", scene_->info_string().c_str());
147
MainLoop::log_scene_result()
149
static const std::string format_fps(Log::continuation_prefix +
150
" FPS: %u FrameTime: %.3f ms\n");
151
static const std::string format_unsupported(Log::continuation_prefix +
153
static const std::string format_fail(Log::continuation_prefix +
156
if (scene_setup_status_ == SceneSetupStatusSuccess) {
157
Log::info(format_fps.c_str(), scene_->average_fps(),
158
1000.0 / scene_->average_fps());
160
else if (scene_setup_status_ == SceneSetupStatusUnsupported) {
161
Log::info(format_unsupported.c_str());
164
Log::info(format_fail.c_str());
169
MainLoop::next_benchmark()
172
if (bench_iter_ == benchmarks_.end() && Options::run_forever)
173
bench_iter_ = benchmarks_.begin();
176
/**********************
177
* MainLoopDecoration *
178
**********************/
180
MainLoopDecoration::MainLoopDecoration(Canvas &canvas, const std::vector<Benchmark *> &benchmarks) :
181
MainLoop(canvas, benchmarks), show_fps_(false), show_title_(false),
182
fps_renderer_(0), title_renderer_(0), last_fps_(0)
187
MainLoopDecoration::~MainLoopDecoration()
189
delete fps_renderer_;
191
delete title_renderer_;
196
MainLoopDecoration::draw()
198
static const unsigned int fps_interval = 500000;
206
uint64_t now = Util::get_timestamp_us();
207
if (now - fps_timestamp_ >= fps_interval) {
208
last_fps_ = scene_->average_fps();
209
fps_renderer_update_text(last_fps_);
210
fps_timestamp_ = now;
212
fps_renderer_->render();
216
title_renderer_->render();
222
MainLoopDecoration::before_scene_setup()
224
delete fps_renderer_;
226
delete title_renderer_;
231
MainLoopDecoration::after_scene_setup()
233
const Scene::Option &show_fps_option(scene_->options().find("show-fps")->second);
234
const Scene::Option &title_option(scene_->options().find("title")->second);
235
show_fps_ = show_fps_option.value == "true";
236
show_title_ = !title_option.value.empty();
239
const Scene::Option &fps_pos_option(scene_->options().find("fps-pos")->second);
240
const Scene::Option &fps_size_option(scene_->options().find("fps-size")->second);
241
fps_renderer_ = new TextRenderer(canvas_);
242
fps_renderer_->position(vec2_from_pos_string(fps_pos_option.value));
243
fps_renderer_->size(Util::fromString<float>(fps_size_option.value));
244
fps_renderer_update_text(last_fps_);
245
fps_timestamp_ = Util::get_timestamp_us();
249
const Scene::Option &title_pos_option(scene_->options().find("title-pos")->second);
250
const Scene::Option &title_size_option(scene_->options().find("title-size")->second);
251
title_renderer_ = new TextRenderer(canvas_);
252
title_renderer_->position(vec2_from_pos_string(title_pos_option.value));
253
title_renderer_->size(Util::fromString<float>(title_size_option.value));
255
if (title_option.value == "#info#")
256
title_renderer_->text(scene_->info_string());
257
else if (title_option.value == "#name#")
258
title_renderer_->text(scene_->name());
259
else if (title_option.value == "#r2d2#")
260
title_renderer_->text("Help me, Obi-Wan Kenobi. You're my only hope.");
262
title_renderer_->text(title_option.value);
267
MainLoopDecoration::fps_renderer_update_text(unsigned int fps)
269
std::stringstream ss;
270
ss << "FPS: " << fps;
271
fps_renderer_->text(ss.str());
275
MainLoopDecoration::vec2_from_pos_string(const std::string &s)
277
LibMatrix::vec2 v(0.0, 0.0);
278
std::vector<std::string> elems;
279
Util::split(s, ',', elems, Util::SplitModeNormal);
281
if (elems.size() > 0)
282
v.x(Util::fromString<float>(elems[0]));
284
if (elems.size() > 1)
285
v.y(Util::fromString<float>(elems[1]));
290
/**********************
291
* MainLoopValidation *
292
**********************/
294
MainLoopValidation::MainLoopValidation(Canvas &canvas, const std::vector<Benchmark *> &benchmarks) :
295
MainLoop(canvas, benchmarks)
300
MainLoopValidation::draw()
302
/* Draw only the first frame of the scene and stop */
309
scene_->running(false);
313
MainLoopValidation::log_scene_result()
315
static const std::string format(Log::continuation_prefix + " Validation: %s\n");
318
switch(scene_->validate()) {
319
case Scene::ValidationSuccess:
322
case Scene::ValidationFailure:
325
case Scene::ValidationUnknown:
332
Log::info(format.c_str(), result.c_str());