|
@@ -55,88 +55,88 @@ namespace {
|
|
|
/** Number of data points recorded, clamped to \c NUM_FRAMERATE_POINTS */
|
|
|
int num_valid;
|
|
|
|
|
|
/** Current accumulated duration */
|
|
|
TimingMeasurement acc_duration;
|
|
|
/** Start time for current accumulation cycle */
|
|
|
TimingMeasurement acc_timestamp;
|
|
|
|
|
|
/**
|
|
|
* Initialize a data element with an expected collection rate
|
|
|
* @param expected_rate
|
|
|
* Expected number of cycles per second of the performance element. Use 1 if unknown or not relevant.
|
|
|
* The rate is used for highlighting slow-running elements in the GUI.
|
|
|
*/
|
|
|
explicit PerformanceData(double expected_rate) : expected_rate(expected_rate), next_index(0), prev_index(0), num_valid(0) { }
|
|
|
|
|
|
/** Collect a complete measurement, given start and ending times for a processing block */
|
|
|
void Add(TimingMeasurement start_time, TimingMeasurement end_time)
|
|
|
{
|
|
|
this->durations[this->next_index] = end_time - start_time;
|
|
|
this->timestamps[this->next_index] = start_time;
|
|
|
this->prev_index = this->next_index;
|
|
|
this->next_index += 1;
|
|
|
if (this->next_index >= NUM_FRAMERATE_POINTS) this->next_index = 0;
|
|
|
this->num_valid = min(NUM_FRAMERATE_POINTS, this->num_valid + 1);
|
|
|
this->num_valid = std::min(NUM_FRAMERATE_POINTS, this->num_valid + 1);
|
|
|
}
|
|
|
|
|
|
/** Begin an accumulation of multiple measurements into a single value, from a given start time */
|
|
|
void BeginAccumulate(TimingMeasurement start_time)
|
|
|
{
|
|
|
this->timestamps[this->next_index] = this->acc_timestamp;
|
|
|
this->durations[this->next_index] = this->acc_duration;
|
|
|
this->prev_index = this->next_index;
|
|
|
this->next_index += 1;
|
|
|
if (this->next_index >= NUM_FRAMERATE_POINTS) this->next_index = 0;
|
|
|
this->num_valid = min(NUM_FRAMERATE_POINTS, this->num_valid + 1);
|
|
|
this->num_valid = std::min(NUM_FRAMERATE_POINTS, this->num_valid + 1);
|
|
|
|
|
|
this->acc_duration = 0;
|
|
|
this->acc_timestamp = start_time;
|
|
|
}
|
|
|
|
|
|
/** Accumulate a period onto the current measurement */
|
|
|
void AddAccumulate(TimingMeasurement duration)
|
|
|
{
|
|
|
this->acc_duration += duration;
|
|
|
}
|
|
|
|
|
|
/** Indicate a pause/expected discontinuity in processing the element */
|
|
|
void AddPause(TimingMeasurement start_time)
|
|
|
{
|
|
|
if (this->durations[this->prev_index] != INVALID_DURATION) {
|
|
|
this->timestamps[this->next_index] = start_time;
|
|
|
this->durations[this->next_index] = INVALID_DURATION;
|
|
|
this->prev_index = this->next_index;
|
|
|
this->next_index += 1;
|
|
|
if (this->next_index >= NUM_FRAMERATE_POINTS) this->next_index = 0;
|
|
|
this->num_valid += 1;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/** Get average cycle processing time over a number of data points */
|
|
|
double GetAverageDurationMilliseconds(int count)
|
|
|
{
|
|
|
count = min(count, this->num_valid);
|
|
|
count = std::min(count, this->num_valid);
|
|
|
|
|
|
int first_point = this->prev_index - count;
|
|
|
if (first_point < 0) first_point += NUM_FRAMERATE_POINTS;
|
|
|
|
|
|
/* Sum durations, skipping invalid points */
|
|
|
double sumtime = 0;
|
|
|
for (int i = first_point; i < first_point + count; i++) {
|
|
|
auto d = this->durations[i % NUM_FRAMERATE_POINTS];
|
|
|
if (d != INVALID_DURATION) {
|
|
|
sumtime += d;
|
|
|
} else {
|
|
|
/* Don't count the invalid durations */
|
|
|
count--;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (count == 0) return 0; // avoid div by zero
|
|
|
return sumtime * 1000 / count / TIMESTAMP_PRECISION;
|
|
|
}
|
|
|
|
|
|
/** Get current rate of a performance element, based on approximately the past one second of data */
|
|
|
double GetRate()
|
|
|
{
|
|
|
/* Start at last recorded point, end at latest when reaching the earliest recorded point */
|
|
@@ -374,89 +374,89 @@ static const NWidgetPart _framerate_wind
|
|
|
EndContainer(),
|
|
|
EndContainer(),
|
|
|
NWidget(NWID_VERTICAL),
|
|
|
NWidget(NWID_VSCROLLBAR, COLOUR_GREY, WID_FRW_SCROLLBAR),
|
|
|
NWidget(WWT_RESIZEBOX, COLOUR_GREY),
|
|
|
EndContainer(),
|
|
|
EndContainer(),
|
|
|
};
|
|
|
|
|
|
struct FramerateWindow : Window {
|
|
|
bool small;
|
|
|
bool showing_memory;
|
|
|
GUITimer next_update;
|
|
|
int num_active;
|
|
|
int num_displayed;
|
|
|
|
|
|
struct CachedDecimal {
|
|
|
StringID strid;
|
|
|
uint32 value;
|
|
|
|
|
|
inline void SetRate(double value, double target)
|
|
|
{
|
|
|
const double threshold_good = target * 0.95;
|
|
|
const double threshold_bad = target * 2 / 3;
|
|
|
value = min(9999.99, value);
|
|
|
value = std::min(9999.99, value);
|
|
|
this->value = (uint32)(value * 100);
|
|
|
this->strid = (value > threshold_good) ? STR_FRAMERATE_FPS_GOOD : (value < threshold_bad) ? STR_FRAMERATE_FPS_BAD : STR_FRAMERATE_FPS_WARN;
|
|
|
}
|
|
|
|
|
|
inline void SetTime(double value, double target)
|
|
|
{
|
|
|
const double threshold_good = target / 3;
|
|
|
const double threshold_bad = target;
|
|
|
value = min(9999.99, value);
|
|
|
value = std::min(9999.99, value);
|
|
|
this->value = (uint32)(value * 100);
|
|
|
this->strid = (value < threshold_good) ? STR_FRAMERATE_MS_GOOD : (value > threshold_bad) ? STR_FRAMERATE_MS_BAD : STR_FRAMERATE_MS_WARN;
|
|
|
}
|
|
|
|
|
|
inline void InsertDParams(uint n) const
|
|
|
{
|
|
|
SetDParam(n, this->value);
|
|
|
SetDParam(n + 1, 2);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
CachedDecimal rate_gameloop; ///< cached game loop tick rate
|
|
|
CachedDecimal rate_drawing; ///< cached drawing frame rate
|
|
|
CachedDecimal speed_gameloop; ///< cached game loop speed factor
|
|
|
CachedDecimal times_shortterm[PFE_MAX]; ///< cached short term average times
|
|
|
CachedDecimal times_longterm[PFE_MAX]; ///< cached long term average times
|
|
|
|
|
|
static const int VSPACING = 3; ///< space between column heading and values
|
|
|
static const int MIN_ELEMENTS = 5; ///< smallest number of elements to display
|
|
|
static constexpr int VSPACING = 3; ///< space between column heading and values
|
|
|
static constexpr int MIN_ELEMENTS = 5; ///< smallest number of elements to display
|
|
|
|
|
|
FramerateWindow(WindowDesc *desc, WindowNumber number) : Window(desc)
|
|
|
{
|
|
|
this->InitNested(number);
|
|
|
this->small = this->IsShaded();
|
|
|
this->showing_memory = true;
|
|
|
this->UpdateData();
|
|
|
this->num_displayed = this->num_active;
|
|
|
this->next_update.SetInterval(100);
|
|
|
|
|
|
/* Window is always initialised to MIN_ELEMENTS height, resize to contain num_displayed */
|
|
|
ResizeWindow(this, 0, (max(MIN_ELEMENTS, this->num_displayed) - MIN_ELEMENTS) * FONT_HEIGHT_NORMAL);
|
|
|
ResizeWindow(this, 0, (std::max(MIN_ELEMENTS, this->num_displayed) - MIN_ELEMENTS) * FONT_HEIGHT_NORMAL);
|
|
|
}
|
|
|
|
|
|
void OnRealtimeTick(uint delta_ms) override
|
|
|
{
|
|
|
bool elapsed = this->next_update.Elapsed(delta_ms);
|
|
|
|
|
|
/* Check if the shaded state has changed, switch caption text if it has */
|
|
|
if (this->small != this->IsShaded()) {
|
|
|
this->small = this->IsShaded();
|
|
|
this->GetWidget<NWidgetLeaf>(WID_FRW_CAPTION)->SetDataTip(this->small ? STR_FRAMERATE_CAPTION_SMALL : STR_FRAMERATE_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS);
|
|
|
elapsed = true;
|
|
|
}
|
|
|
|
|
|
if (elapsed) {
|
|
|
this->UpdateData();
|
|
|
this->SetDirty();
|
|
|
this->next_update.SetInterval(100);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void UpdateData()
|
|
|
{
|
|
|
double gl_rate = _pf_data[PFE_GAMELOOP].GetRate();
|
|
|
bool have_script = false;
|
|
@@ -465,49 +465,49 @@ struct FramerateWindow : Window {
|
|
|
if (this->small) return; // in small mode, this is everything needed
|
|
|
|
|
|
this->rate_drawing.SetRate(_pf_data[PFE_DRAWING].GetRate(), _pf_data[PFE_DRAWING].expected_rate);
|
|
|
|
|
|
int new_active = 0;
|
|
|
for (PerformanceElement e = PFE_FIRST; e < PFE_MAX; e++) {
|
|
|
this->times_shortterm[e].SetTime(_pf_data[e].GetAverageDurationMilliseconds(8), MILLISECONDS_PER_TICK);
|
|
|
this->times_longterm[e].SetTime(_pf_data[e].GetAverageDurationMilliseconds(NUM_FRAMERATE_POINTS), MILLISECONDS_PER_TICK);
|
|
|
if (_pf_data[e].num_valid > 0) {
|
|
|
new_active++;
|
|
|
if (e == PFE_GAMESCRIPT || e >= PFE_AI0) have_script = true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (this->showing_memory != have_script) {
|
|
|
NWidgetStacked *plane = this->GetWidget<NWidgetStacked>(WID_FRW_SEL_MEMORY);
|
|
|
plane->SetDisplayedPlane(have_script ? 0 : SZSP_VERTICAL);
|
|
|
this->showing_memory = have_script;
|
|
|
}
|
|
|
|
|
|
if (new_active != this->num_active) {
|
|
|
this->num_active = new_active;
|
|
|
Scrollbar *sb = this->GetScrollbar(WID_FRW_SCROLLBAR);
|
|
|
sb->SetCount(this->num_active);
|
|
|
sb->SetCapacity(min(this->num_displayed, this->num_active));
|
|
|
sb->SetCapacity(std::min(this->num_displayed, this->num_active));
|
|
|
this->ReInit();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void SetStringParameters(int widget) const override
|
|
|
{
|
|
|
switch (widget) {
|
|
|
case WID_FRW_CAPTION:
|
|
|
/* When the window is shaded, the caption shows game loop rate and speed factor */
|
|
|
if (!this->small) break;
|
|
|
SetDParam(0, this->rate_gameloop.strid);
|
|
|
this->rate_gameloop.InsertDParams(1);
|
|
|
this->speed_gameloop.InsertDParams(3);
|
|
|
break;
|
|
|
|
|
|
case WID_FRW_RATE_GAMELOOP:
|
|
|
SetDParam(0, this->rate_gameloop.strid);
|
|
|
this->rate_gameloop.InsertDParams(1);
|
|
|
break;
|
|
|
case WID_FRW_RATE_DRAWING:
|
|
|
SetDParam(0, this->rate_drawing.strid);
|
|
|
this->rate_drawing.InsertDParams(1);
|
|
|
break;
|
|
|
case WID_FRW_RATE_FACTOR:
|
|
@@ -534,61 +534,61 @@ struct FramerateWindow : Window {
|
|
|
SetDParam(2, 2);
|
|
|
*size = GetStringBoundingBox(STR_FRAMERATE_RATE_BLITTER);
|
|
|
break;
|
|
|
case WID_FRW_RATE_FACTOR:
|
|
|
SetDParam(0, 999999);
|
|
|
SetDParam(1, 2);
|
|
|
*size = GetStringBoundingBox(STR_FRAMERATE_SPEED_FACTOR);
|
|
|
break;
|
|
|
|
|
|
case WID_FRW_TIMES_NAMES: {
|
|
|
size->width = 0;
|
|
|
size->height = FONT_HEIGHT_NORMAL + VSPACING + MIN_ELEMENTS * FONT_HEIGHT_NORMAL;
|
|
|
resize->width = 0;
|
|
|
resize->height = FONT_HEIGHT_NORMAL;
|
|
|
for (PerformanceElement e : DISPLAY_ORDER_PFE) {
|
|
|
if (_pf_data[e].num_valid == 0) continue;
|
|
|
Dimension line_size;
|
|
|
if (e < PFE_AI0) {
|
|
|
line_size = GetStringBoundingBox(STR_FRAMERATE_GAMELOOP + e);
|
|
|
} else {
|
|
|
SetDParam(0, e - PFE_AI0 + 1);
|
|
|
SetDParamStr(1, GetAIName(e - PFE_AI0));
|
|
|
line_size = GetStringBoundingBox(STR_FRAMERATE_AI);
|
|
|
}
|
|
|
size->width = max(size->width, line_size.width);
|
|
|
size->width = std::max(size->width, line_size.width);
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
case WID_FRW_TIMES_CURRENT:
|
|
|
case WID_FRW_TIMES_AVERAGE:
|
|
|
case WID_FRW_ALLOCSIZE: {
|
|
|
*size = GetStringBoundingBox(STR_FRAMERATE_CURRENT + (widget - WID_FRW_TIMES_CURRENT));
|
|
|
SetDParam(0, 999999);
|
|
|
SetDParam(1, 2);
|
|
|
Dimension item_size = GetStringBoundingBox(STR_FRAMERATE_MS_GOOD);
|
|
|
size->width = max(size->width, item_size.width);
|
|
|
size->width = std::max(size->width, item_size.width);
|
|
|
size->height += FONT_HEIGHT_NORMAL * MIN_ELEMENTS + VSPACING;
|
|
|
resize->width = 0;
|
|
|
resize->height = FONT_HEIGHT_NORMAL;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/** Render a column of formatted average durations */
|
|
|
void DrawElementTimesColumn(const Rect &r, StringID heading_str, const CachedDecimal *values) const
|
|
|
{
|
|
|
const Scrollbar *sb = this->GetScrollbar(WID_FRW_SCROLLBAR);
|
|
|
uint16 skip = sb->GetPosition();
|
|
|
int drawable = this->num_displayed;
|
|
|
int y = r.top;
|
|
|
DrawString(r.left, r.right, y, heading_str, TC_FROMSTRING, SA_CENTER, true);
|
|
|
y += FONT_HEIGHT_NORMAL + VSPACING;
|
|
|
for (PerformanceElement e : DISPLAY_ORDER_PFE) {
|
|
|
if (_pf_data[e].num_valid == 0) continue;
|
|
|
if (skip > 0) {
|
|
|
skip--;
|
|
|
} else {
|
|
|
values[e].InsertDParams(0);
|
|
|
DrawString(r.left, r.right, y, values[e].strid, TC_FROMSTRING, SA_RIGHT);
|
|
@@ -748,49 +748,49 @@ struct FrametimeGraphWindow : Window {
|
|
|
void SetStringParameters(int widget) const override
|
|
|
{
|
|
|
switch (widget) {
|
|
|
case WID_FGW_CAPTION:
|
|
|
if (this->element < PFE_AI0) {
|
|
|
SetDParam(0, STR_FRAMETIME_CAPTION_GAMELOOP + this->element);
|
|
|
} else {
|
|
|
SetDParam(0, STR_FRAMETIME_CAPTION_AI);
|
|
|
SetDParam(1, this->element - PFE_AI0 + 1);
|
|
|
SetDParamStr(2, GetAIName(this->element - PFE_AI0));
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void UpdateWidgetSize(int widget, Dimension *size, const Dimension &padding, Dimension *fill, Dimension *resize) override
|
|
|
{
|
|
|
if (widget == WID_FGW_GRAPH) {
|
|
|
SetDParam(0, 100);
|
|
|
Dimension size_ms_label = GetStringBoundingBox(STR_FRAMERATE_GRAPH_MILLISECONDS);
|
|
|
SetDParam(0, 100);
|
|
|
Dimension size_s_label = GetStringBoundingBox(STR_FRAMERATE_GRAPH_SECONDS);
|
|
|
|
|
|
/* Size graph in height to fit at least 10 vertical labels with space between, or at least 100 pixels */
|
|
|
graph_size.height = max<uint>(100, 10 * (size_ms_label.height + 1));
|
|
|
graph_size.height = std::max(100u, 10 * (size_ms_label.height + 1));
|
|
|
/* Always 2:1 graph area */
|
|
|
graph_size.width = 2 * graph_size.height;
|
|
|
*size = graph_size;
|
|
|
|
|
|
size->width += size_ms_label.width + 2;
|
|
|
size->height += size_s_label.height + 2;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
void SelectHorizontalScale(TimingMeasurement range)
|
|
|
{
|
|
|
/* Determine horizontal scale based on period covered by 60 points
|
|
|
* (slightly less than 2 seconds at full game speed) */
|
|
|
struct ScaleDef { TimingMeasurement range; int scale; };
|
|
|
static const ScaleDef hscales[] = {
|
|
|
{ 120, 60 },
|
|
|
{ 10, 20 },
|
|
|
{ 5, 10 },
|
|
|
{ 3, 4 },
|
|
|
{ 1, 2 },
|
|
|
};
|
|
|
for (const ScaleDef *sc = hscales; sc < hscales + lengthof(hscales); sc++) {
|
|
|
if (range < sc->range) this->horizontal_scale = sc->scale;
|
|
|
}
|
|
@@ -959,49 +959,49 @@ struct FrametimeGraphWindow : Window {
|
|
|
|
|
|
/* Draw line from previous point to new point */
|
|
|
Point newpoint = {
|
|
|
(int)Scinterlate<int64>(x_zero, x_max, 0, (int64)draw_horz_scale, (int64)draw_horz_scale - (int64)time_sum),
|
|
|
(int)Scinterlate<int64>(y_zero, y_max, 0, (int64)draw_vert_scale, (int64)value)
|
|
|
};
|
|
|
assert(newpoint.x <= lastpoint.x);
|
|
|
GfxDrawLine(lastpoint.x, lastpoint.y, newpoint.x, newpoint.y, c_lines);
|
|
|
lastpoint = newpoint;
|
|
|
|
|
|
/* Record peak and average value across graphed data */
|
|
|
value_sum += value;
|
|
|
points_drawn++;
|
|
|
if (value > peak_value) {
|
|
|
peak_value = value;
|
|
|
peak_point = newpoint;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/* If the peak value is significantly larger than the average, mark and label it */
|
|
|
if (points_drawn > 0 && peak_value > TIMESTAMP_PRECISION / 100 && 2 * peak_value > 3 * value_sum / points_drawn) {
|
|
|
TextColour tc_peak = (TextColour)(TC_IS_PALETTE_COLOUR | c_peak);
|
|
|
GfxFillRect(peak_point.x - 1, peak_point.y - 1, peak_point.x + 1, peak_point.y + 1, c_peak);
|
|
|
SetDParam(0, peak_value * 1000 / TIMESTAMP_PRECISION);
|
|
|
int label_y = max(y_max, peak_point.y - FONT_HEIGHT_SMALL);
|
|
|
int label_y = std::max(y_max, peak_point.y - FONT_HEIGHT_SMALL);
|
|
|
if (peak_point.x - x_zero > (int)this->graph_size.width / 2) {
|
|
|
DrawString(x_zero, peak_point.x - 2, label_y, STR_FRAMERATE_GRAPH_MILLISECONDS, tc_peak, SA_RIGHT | SA_FORCE, false, FS_SMALL);
|
|
|
} else {
|
|
|
DrawString(peak_point.x + 2, x_max, label_y, STR_FRAMERATE_GRAPH_MILLISECONDS, tc_peak, SA_LEFT | SA_FORCE, false, FS_SMALL);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
static WindowDesc _frametime_graph_window_desc(
|
|
|
WDP_AUTO, "frametime_graph", 140, 90,
|
|
|
WC_FRAMETIME_GRAPH, WC_NONE,
|
|
|
0,
|
|
|
_frametime_graph_window_widgets, lengthof(_frametime_graph_window_widgets)
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
/** Open the general framerate window */
|
|
|
void ShowFramerateWindow()
|
|
|
{
|
|
|
AllocateWindowDescFront<FramerateWindow>(&_framerate_display_desc, 0);
|
|
|
}
|