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
+95 -2
View File
@@ -33,6 +33,7 @@ ScopeBase::ScopeBase(QWidget *parent)
: super(parent)
, texture_(nullptr)
, managed_tex_up_to_date_(false)
, software_image_up_to_date_(false)
{
EnableDefaultContextMenu();
}
@@ -41,6 +42,7 @@ void ScopeBase::SetBuffer(TexturePtr frame)
{
texture_ = frame;
managed_tex_up_to_date_ = false;
software_image_up_to_date_ = false;
update();
}
@@ -60,17 +62,103 @@ void ScopeBase::DrawScope(TexturePtr managed_tex, QVariant pipeline)
renderer()->Blit(pipeline, job, GetViewportParams());
}
void ScopeBase::UpdateSoftwareImage()
{
if (!texture_ || texture_->IsDummy() || !renderer()) {
software_image_ = QImage();
software_image_up_to_date_ = true;
return;
}
// Backend-neutral widgets each have their own renderer instance (e.g. a
// separate Vulkan device). The reference texture emitted by the viewer lives
// in the viewer's renderer, so we must download it to the CPU and re-upload
// it into this scope's renderer before we can sample it.
TexturePtr source_tex = texture_;
if (texture_->renderer() && texture_->renderer() != renderer()) {
FramePtr temp_frame = Frame::Create();
temp_frame->set_video_params(texture_->params());
temp_frame->allocate();
texture_->Download(temp_frame->data(), temp_frame->linesize_pixels());
local_texture_ = renderer()->CreateTexture(
temp_frame->video_params(), temp_frame->data(),
temp_frame->linesize_pixels());
source_tex = local_texture_;
}
if (!source_tex || source_tex->IsDummy()) {
software_image_ = QImage();
software_image_up_to_date_ = true;
return;
}
const int texture_width = static_cast<int>(width() * devicePixelRatioF());
const int texture_height = static_cast<int>(height() * devicePixelRatioF());
const VideoParams offscreen_params(
texture_width, texture_height, PixelFormat::U8,
VideoParams::kRGBAChannelCount);
if (!software_tex_ || software_tex_->params() != offscreen_params) {
software_tex_ = renderer()->CreateTexture(offscreen_params);
software_buffer_.resize(
texture_width * texture_height *
VideoParams::GetBytesPerPixel(PixelFormat::U8,
VideoParams::kRGBAChannelCount));
}
if (!software_tex_ || software_tex_->IsDummy()) {
software_image_ = QImage();
software_image_up_to_date_ = true;
return;
}
ColorTransformJob job;
job.SetColorProcessor(color_service());
job.SetInputTexture(source_tex);
job.SetInputAlphaAssociation(kAlphaNone);
job.SetClearDestinationEnabled(true);
job.SetForceOpaque(true);
renderer()->BlitColorManaged(job, software_tex_.get());
renderer()->DownloadFromTexture(software_tex_->id(), software_tex_->params(),
software_buffer_.data(), 0);
software_image_ = QImage(
reinterpret_cast<const uchar *>(software_buffer_.constData()),
texture_width, texture_height,
texture_width *
VideoParams::GetBytesPerPixel(PixelFormat::U8,
VideoParams::kRGBAChannelCount),
QImage::Format_RGBA8888_Premultiplied);
software_image_.setDevicePixelRatio(devicePixelRatioF());
software_image_up_to_date_ = true;
}
void ScopeBase::OnInit()
{
super::OnInit();
pipeline_ = renderer()->CreateNativeShader(GenerateShaderCode());
if (!IsBackendNeutral()) {
pipeline_ = renderer()->CreateNativeShader(GenerateShaderCode());
}
}
void ScopeBase::OnPaint()
{
if (IsBackendNeutral()) {
// TODO: implement backend-neutral scope display
if (!software_image_up_to_date_) {
UpdateSoftwareImage();
}
QPainter p(paint_device());
p.fillRect(rect(), Qt::black);
if (!software_image_.isNull()) {
DrawScopeSoftware(p, software_image_);
}
return;
}
@@ -89,6 +177,7 @@ void ScopeBase::OnPaint()
job.SetInputAlphaAssociation(kAlphaNone);
renderer()->BlitColorManaged(job, managed_tex_.get());
managed_tex_up_to_date_ = true;
}
DrawScope(managed_tex_, pipeline_);
@@ -97,6 +186,10 @@ void ScopeBase::OnPaint()
void ScopeBase::OnDestroy()
{
local_texture_ = nullptr;
software_tex_ = nullptr;
software_buffer_.clear();
software_image_ = QImage();
managed_tex_ = nullptr;
texture_ = nullptr;
pipeline_.clear();