Blob Blame Raw
/*
    ......... 2015 Ivan Mahonin

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <iostream>
#include <iomanip>

#include <time.h>

#include "measure.h"
#include "utils.h"
#include "glcontext.h"


using namespace std;


std::vector<Measure*> Measure::stack;


void Measure::init() {
	hide = !stack.empty() && stack.back()->hide_subs;
	hide_subs |= hide;
	tga = filename.size() > 4 && filename.substr(filename.size()-4, 4) == ".tga";
	if (!hide)
		cout << string(stack.size()*2, ' ')
		     << "begin             "
			 << filename
			 << endl << flush;
	stack.push_back(this);

	timespec spec;
	clock_gettime(CLOCK_MONOTONIC , &spec);
	t = spec.tv_sec*1000000000 + spec.tv_nsec;
}

Measure::~Measure() {
	if (!surface && tga) glFinish();

	timespec spec;
	clock_gettime(CLOCK_MONOTONIC , &spec);
	long long dt = subs ? subs : spec.tv_sec*1000000000 + spec.tv_nsec - t;
	Real ms = 1000.0*(Real)dt*(Real)(1e-9);

	if (!hide)
		cout << string((stack.size()-1)*2, ' ') << "end "
			 << setw(8) << fixed << setprecision(3)
			 << ms << " ms - "
			 << filename
			 << endl << flush;

	if (tga) {
		if (surface)
			Utils::save_surface(*surface, filename);
		else
			Utils::save_viewport(filename);
	}

	if (surface) {
		surface->clear();
	} else
	if (tga) {
		glClear(GL_COLOR_BUFFER_BIT);
		glFinish();
	}

	stack.pop_back();
	if (!stack.empty()) stack.back()->subs += dt;
}