~ubuntu-branches/ubuntu/raring/glmark2/raring

« back to all changes in this revision

Viewing changes to src/main-loop.cpp

  • Committer: Package Import Robot
  • Author(s): Ricardo Salveti de Araujo
  • Date: 2012-08-21 15:38:09 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20120821153809-bwux72bat8qp2n5v
Tags: 2012.08-0ubuntu1
* New upstream release 2012.08 (LP: #1039736)
  - Avoid crashing if gl used is not >= 2.0 (LP: #842279)
* Bumping dh compatibility level to v9
* debian/control:
  - Update Standards-Version to 3.9.3.
  - Add libjpeg-dev build dependency.
  - Use libegl1-x11-dev as an build-dep alternative instead of libegl1-dev.
  - Update description of glmark2-data binary package.
* debian/copyright:
  - Refresh copyright based on the current upstrem version
* debian/rules:
  - Clean compiled python code from unpacked waflib/ directory, as
    described in http://wiki.debian.org/UnpackWaf

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright © 2012 Linaro Limited
 
3
 *
 
4
 * This file is part of the glmark2 OpenGL (ES) 2.0 benchmark.
 
5
 *
 
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
 
9
 * version.
 
10
 *
 
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
 
14
 * details.
 
15
 *
 
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/>.
 
18
 *
 
19
 * Authors:
 
20
 *  Alexandros Frantzis
 
21
 */
 
22
#include "options.h"
 
23
#include "main-loop.h"
 
24
#include "util.h"
 
25
#include "log.h"
 
26
 
 
27
#include <string>
 
28
#include <sstream>
 
29
 
 
30
/************
 
31
 * MainLoop *
 
32
 ************/
 
33
 
 
34
MainLoop::MainLoop(Canvas &canvas, const std::vector<Benchmark *> &benchmarks) :
 
35
    canvas_(canvas), benchmarks_(benchmarks)
 
36
{
 
37
    reset();
 
38
}
 
39
 
 
40
 
 
41
void
 
42
MainLoop::reset()
 
43
{
 
44
    scene_ = 0;
 
45
    scene_setup_status_ = SceneSetupStatusUnknown;
 
46
    score_ = 0;
 
47
    benchmarks_run_ = 0;
 
48
    bench_iter_ = benchmarks_.begin();
 
49
}
 
50
 
 
51
unsigned int
 
52
MainLoop::score()
 
53
{
 
54
    if (benchmarks_run_)
 
55
        return score_ / benchmarks_run_;
 
56
    else
 
57
        return score_;
 
58
}
 
59
 
 
60
bool
 
61
MainLoop::step()
 
62
{
 
63
    /* Find the next normal scene */
 
64
    if (!scene_) {
 
65
        /* Find a normal scene */
 
66
        while (bench_iter_ != benchmarks_.end()) {
 
67
            scene_ = &(*bench_iter_)->scene();
 
68
 
 
69
            /* 
 
70
             * Scenes with empty names are option-setting scenes.
 
71
             * Just set them up and continue with the search.
 
72
             */
 
73
            if (scene_->name().empty())
 
74
                (*bench_iter_)->setup_scene();
 
75
            else
 
76
                break;
 
77
 
 
78
            next_benchmark();
 
79
        }
 
80
 
 
81
        /* If we have found a valid scene, set it up */
 
82
        if (bench_iter_ != benchmarks_.end()) {
 
83
            if (!Options::reuse_context)
 
84
                canvas_.reset();
 
85
            before_scene_setup();
 
86
            scene_ = &(*bench_iter_)->setup_scene();
 
87
            if (!scene_->running()) {
 
88
                if (!scene_->supported(false))
 
89
                    scene_setup_status_ = SceneSetupStatusUnsupported;
 
90
                else
 
91
                    scene_setup_status_ = SceneSetupStatusFailure;
 
92
            }
 
93
            else {
 
94
                scene_setup_status_ = SceneSetupStatusSuccess;
 
95
            }
 
96
            after_scene_setup();
 
97
            log_scene_info();
 
98
        }
 
99
        else {
 
100
            /* ... otherwise we are done */
 
101
            return false;
 
102
        }
 
103
    }
 
104
 
 
105
    bool should_quit = canvas_.should_quit();
 
106
 
 
107
    if (scene_ ->running() && !should_quit)
 
108
        draw();
 
109
 
 
110
    /*
 
111
     * Need to recheck whether the scene is still running, because code
 
112
     * in draw() may have changed the state.
 
113
     */
 
114
    if (!scene_->running() || should_quit) {
 
115
        if (scene_setup_status_ == SceneSetupStatusSuccess) {
 
116
            score_ += scene_->average_fps();
 
117
            benchmarks_run_++;
 
118
        }
 
119
        log_scene_result();
 
120
        (*bench_iter_)->teardown_scene();
 
121
        scene_ = 0;
 
122
        next_benchmark();
 
123
    }
 
124
 
 
125
    return !should_quit;
 
126
}
 
127
 
 
128
void
 
129
MainLoop::draw()
 
130
{
 
131
    canvas_.clear();
 
132
 
 
133
    scene_->draw();
 
134
    scene_->update();
 
135
 
 
136
    canvas_.update();
 
137
}
 
138
 
 
139
void
 
140
MainLoop::log_scene_info()
 
141
{
 
142
    Log::info("%s", scene_->info_string().c_str());
 
143
    Log::flush();
 
144
}
 
145
 
 
146
void
 
147
MainLoop::log_scene_result()
 
148
{
 
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 +
 
152
                                                " Unsupported\n");
 
153
    static const std::string format_fail(Log::continuation_prefix +
 
154
                                         " Set up failed\n");
 
155
 
 
156
    if (scene_setup_status_ == SceneSetupStatusSuccess) {
 
157
        Log::info(format_fps.c_str(), scene_->average_fps(),
 
158
                                      1000.0 / scene_->average_fps());
 
159
    }
 
160
    else if (scene_setup_status_ == SceneSetupStatusUnsupported) {
 
161
        Log::info(format_unsupported.c_str());
 
162
    }
 
163
    else {
 
164
        Log::info(format_fail.c_str());
 
165
    }
 
166
}
 
167
 
 
168
void
 
169
MainLoop::next_benchmark()
 
170
{
 
171
    bench_iter_++;
 
172
    if (bench_iter_ == benchmarks_.end() && Options::run_forever)
 
173
        bench_iter_ = benchmarks_.begin();
 
174
}
 
175
 
 
176
/**********************
 
177
 * MainLoopDecoration *
 
178
 **********************/
 
179
 
 
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)
 
183
{
 
184
 
 
185
}
 
186
 
 
187
MainLoopDecoration::~MainLoopDecoration()
 
188
{
 
189
    delete fps_renderer_;
 
190
    fps_renderer_ = 0;
 
191
    delete title_renderer_;
 
192
    title_renderer_ = 0;
 
193
}
 
194
 
 
195
void
 
196
MainLoopDecoration::draw()
 
197
{
 
198
    static const unsigned int fps_interval = 500000;
 
199
 
 
200
    canvas_.clear();
 
201
 
 
202
    scene_->draw();
 
203
    scene_->update();
 
204
 
 
205
    if (show_fps_) {
 
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;
 
211
        }
 
212
        fps_renderer_->render();
 
213
    }
 
214
 
 
215
    if (show_title_)
 
216
        title_renderer_->render();
 
217
 
 
218
    canvas_.update();
 
219
}
 
220
 
 
221
void
 
222
MainLoopDecoration::before_scene_setup()
 
223
{
 
224
    delete fps_renderer_;
 
225
    fps_renderer_ = 0;
 
226
    delete title_renderer_;
 
227
    title_renderer_ = 0;
 
228
}
 
229
 
 
230
void
 
231
MainLoopDecoration::after_scene_setup()
 
232
{
 
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();
 
237
 
 
238
    if (show_fps_) {
 
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();
 
246
    }
 
247
 
 
248
    if (show_title_) {
 
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));
 
254
 
 
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.");
 
261
        else
 
262
            title_renderer_->text(title_option.value);
 
263
    }
 
264
}
 
265
 
 
266
void
 
267
MainLoopDecoration::fps_renderer_update_text(unsigned int fps)
 
268
{
 
269
    std::stringstream ss;
 
270
    ss << "FPS: " << fps;
 
271
    fps_renderer_->text(ss.str());
 
272
}
 
273
 
 
274
LibMatrix::vec2
 
275
MainLoopDecoration::vec2_from_pos_string(const std::string &s)
 
276
{
 
277
    LibMatrix::vec2 v(0.0, 0.0);
 
278
    std::vector<std::string> elems;
 
279
    Util::split(s, ',', elems, Util::SplitModeNormal);
 
280
 
 
281
    if (elems.size() > 0)
 
282
        v.x(Util::fromString<float>(elems[0]));
 
283
 
 
284
    if (elems.size() > 1)
 
285
        v.y(Util::fromString<float>(elems[1]));
 
286
 
 
287
    return v;
 
288
}
 
289
 
 
290
/**********************
 
291
 * MainLoopValidation *
 
292
 **********************/
 
293
 
 
294
MainLoopValidation::MainLoopValidation(Canvas &canvas, const std::vector<Benchmark *> &benchmarks) :
 
295
        MainLoop(canvas, benchmarks)
 
296
{
 
297
}
 
298
 
 
299
void
 
300
MainLoopValidation::draw()
 
301
{
 
302
    /* Draw only the first frame of the scene and stop */
 
303
    canvas_.clear();
 
304
 
 
305
    scene_->draw();
 
306
 
 
307
    canvas_.update();
 
308
 
 
309
    scene_->running(false);
 
310
}
 
311
 
 
312
void
 
313
MainLoopValidation::log_scene_result()
 
314
{
 
315
    static const std::string format(Log::continuation_prefix + " Validation: %s\n");
 
316
    std::string result;
 
317
 
 
318
    switch(scene_->validate()) {
 
319
        case Scene::ValidationSuccess:
 
320
            result = "Success";
 
321
            break;
 
322
        case Scene::ValidationFailure:
 
323
            result = "Failure";
 
324
            break;
 
325
        case Scene::ValidationUnknown:
 
326
            result = "Unknown";
 
327
            break;
 
328
        default:
 
329
            break;
 
330
    }
 
331
 
 
332
    Log::info(format.c_str(), result.c_str());
 
333
}