Implement software scope rendering for backend-neutral display paths

The Waveform, Vectorscope and Histogram panels were black when running on
the Vulkan / backend-neutral render path because ScopeBase::OnPaint()
returned early with a TODO. The viewer emits GPU textures that live in the
viewer's own renderer; on Vulkan each ManagedDisplayWidget has a separate
device, so those textures cannot be sampled directly.

Fix by:

- Adding a backend-neutral branch in ScopeBase::OnPaint() that downloads the
  reference texture (cross-renderer if needed), color-manages it into a U8
  offscreen texture, downloads it to a QImage, and draws via QPainter.
- Adding a pure virtual DrawScopeSoftware() to ScopeBase and implementing it
  for WaveformScope, VectorscopeScope and HistogramScope.
- Fixing the managed_tex_up_to_date_ flag that was never set to true in the
  OpenGL path, which caused the color-managed scope texture to be recreated
  on every paint.

All gtest suites pass.
This commit is contained in:
2026-07-13 10:19:30 +08:00
parent f0119a910c
commit 8d3bde5a50
8 changed files with 416 additions and 3 deletions
+102
View File
@@ -21,6 +21,7 @@
#include "histogram.h"
#include <array>
#include <QPainter>
#include <QtMath>
#include <QVector2D>
@@ -148,4 +149,105 @@ void HistogramScope::DrawScope(TexturePtr managed_tex, QVariant pipeline)
p.drawLines(histogram_lines);
}
void HistogramScope::DrawScopeSoftware(QPainter &p, const QImage &image)
{
const float histogram_scale = 0.80f;
const float histogram_base = 2.5f;
const int histogram_dim_x = qCeil((width() - 1.0) * histogram_scale);
const int histogram_dim_y = qCeil((height() - 1.0) * histogram_scale);
const int histogram_start_dim_x =
qFloor(((width() - 1.0) - histogram_dim_x) / 2.0f);
const int histogram_start_dim_y =
qFloor(((height() - 1.0) - histogram_dim_y) / 2.0f);
const int histogram_end_dim_x = width() - 1 - histogram_start_dim_x;
std::array<int, 256> r_counts{};
std::array<int, 256> g_counts{};
std::array<int, 256> b_counts{};
const int src_w = image.width();
const int src_h = image.height();
// Limit analysis resolution to keep CPU usage reasonable.
const int step_x = qMax(1, src_w / 512);
const int step_y = qMax(1, src_h / 512);
for (int sy = 0; sy < src_h; sy += step_y) {
const uchar *src_line = image.constScanLine(sy);
for (int sx = 0; sx < src_w; sx += step_x) {
const uchar *src = src_line + sx * 4;
r_counts[src[0]]++;
g_counts[src[1]]++;
b_counts[src[2]]++;
}
}
int max_count = 1;
for (int i = 0; i < 256; ++i) {
max_count = qMax(max_count, r_counts[i]);
max_count = qMax(max_count, g_counts[i]);
max_count = qMax(max_count, b_counts[i]);
}
auto draw_channel = [&](const std::array<int, 256> &counts, const QColor &color) {
QPen pen(color);
pen.setWidth(2);
p.setPen(pen);
QVector<QPointF> points;
points.reserve(256);
for (int i = 0; i < 256; ++i) {
float x = histogram_start_dim_x +
(float(i) / 255.0f) * histogram_dim_x;
float normalized = float(counts[i]) / float(max_count);
float y = histogram_start_dim_y +
histogram_dim_y *
(1.0f - pow(normalized, 1.0f / histogram_base));
points.append(QPointF(x, y));
}
p.drawPolyline(points.constData(), points.size());
};
p.setCompositionMode(QPainter::CompositionMode_Plus);
draw_channel(r_counts, QColor(255, 0, 0));
draw_channel(g_counts, QColor(0, 255, 0));
draw_channel(b_counts, QColor(0, 0, 255));
// Draw percentage line overlays
QFont font = p.font();
font.setPixelSize(10);
QFontMetrics font_metrics = QFontMetrics(font);
QString label;
std::vector<float> histogram_increments = { 0.00, 0.25, 0.50, 1.0 };
int histogram_steps = histogram_increments.size();
QVector<QLine> histogram_lines(histogram_steps + 1);
int font_x_offset = 0;
int font_y_offset = font_metrics.capHeight() / 2.0f;
p.setCompositionMode(QPainter::CompositionMode_SourceOver);
p.setPen(QColor(0.0, 0.6 * 255.0, 0.0));
p.setFont(font);
for (std::vector<float>::iterator it = histogram_increments.begin();
it != histogram_increments.end(); it++) {
histogram_lines[it - histogram_increments.begin()].setLine(
histogram_start_dim_x,
(histogram_dim_y * pow(1.0 - *it, histogram_base)) +
histogram_start_dim_y,
histogram_end_dim_x,
(histogram_dim_y * pow(1.0 - *it, histogram_base)) +
histogram_start_dim_y);
label = QString::number(*it * 100, 'f', 1) + "%";
font_x_offset = QtUtils::QFontMetricsWidth(font_metrics, label) + 4;
p.drawText(histogram_start_dim_x - font_x_offset,
(histogram_dim_y * pow(1.0 - *it, histogram_base)) +
histogram_start_dim_y + font_y_offset,
label);
}
p.drawLines(histogram_lines);
}
}