From 13d5c007b043f41bd2bf71c489370194e1eaa335 Mon Sep 17 00:00:00 2001 From: Warchamp7 Date: Mon, 17 Aug 2026 17:16:43 -0400 Subject: [PATCH] frontend: Refactor ScreenshotObj options --- frontend/utility/ScreenshotObj.cpp | 55 ++++++++++++------------------ frontend/utility/ScreenshotObj.hpp | 22 +++++++----- frontend/utility/ThumbnailItem.cpp | 8 +++-- 3 files changed, 39 insertions(+), 46 deletions(-) diff --git a/frontend/utility/ScreenshotObj.cpp b/frontend/utility/ScreenshotObj.cpp index ee5df6351..aeae0285a 100644 --- a/frontend/utility/ScreenshotObj.cpp +++ b/frontend/utility/ScreenshotObj.cpp @@ -32,25 +32,15 @@ #include "moc_ScreenshotObj.cpp" -namespace { -void renderTick(void *param, float) -{ - ScreenshotObj *self = static_cast(param); - if (self->stage() == ScreenshotObj::Stage::Finished) { - return; - } - - obs_enter_graphics(); - self->processStage(); - obs_leave_graphics(); -} -} // namespace - -ScreenshotObj::ScreenshotObj(obs_source_t *source) : weakSource(OBSGetWeakRef(source)) +ScreenshotObj::ScreenshotObj(obs_source_t *source, const Options &options) + : weakSource(OBSGetWeakRef(source)), + options(options) { obs_add_tick_callback(renderTick, this); } +ScreenshotObj::ScreenshotObj(obs_source_t *source) : ScreenshotObj(source, {}) {} + ScreenshotObj::~ScreenshotObj() { obs_enter_graphics(); @@ -61,6 +51,18 @@ ScreenshotObj::~ScreenshotObj() obs_remove_tick_callback(renderTick, this); } +void ScreenshotObj::renderTick(void *param, float) +{ + ScreenshotObj *self = static_cast(param); + if (self->stage() == ScreenshotObj::Stage::Finished) { + return; + } + + obs_enter_graphics(); + self->processStage(); + obs_leave_graphics(); +} + void ScreenshotObj::renderScreenshot() { OBSSourceAutoRelease source = OBSGetStrongRef(weakSource); @@ -94,8 +96,8 @@ void ScreenshotObj::renderScreenshot() #endif const enum gs_color_format format = gs_get_format_from_space(space); - outputWidth = customSize.isValid() ? customSize.width() : sourceWidth; - outputHeight = customSize.isValid() ? customSize.height() : sourceHeight; + outputWidth = options.size.isValid() ? options.size.width() : sourceWidth; + outputHeight = options.size.isValid() ? options.size.height() : sourceHeight; texrender = gs_texrender_create(format, GS_ZS_NONE); stagesurf = gs_stagesurface_create(outputWidth, outputHeight, format); @@ -198,7 +200,7 @@ void ScreenshotObj::copyData() void ScreenshotObj::saveToFile() { - if (!outputToFile) { + if (!options.outputToFile) { QMetaObject::invokeMethod(this, &ScreenshotObj::onFinished, Qt::QueuedConnection); return; } @@ -352,7 +354,7 @@ void ScreenshotObj::onFinished() } if (outputWidth > 0 && outputHeight > 0) { - if (outputToFile) { + if (options.outputToFile) { emit imageSaved(path); } @@ -362,21 +364,6 @@ void ScreenshotObj::onFinished() this->deleteLater(); } -void ScreenshotObj::setSize(QSize size) -{ - customSize = size; -} - -void ScreenshotObj::setSize(int width, int height) -{ - setSize(QSize(width, height)); -} - -void ScreenshotObj::setSaveToFile(bool save) -{ - outputToFile = save; -} - void ScreenshotObj::handleSave() { saveToFile(); diff --git a/frontend/utility/ScreenshotObj.hpp b/frontend/utility/ScreenshotObj.hpp index 99fe3ebb8..30753ae3d 100644 --- a/frontend/utility/ScreenshotObj.hpp +++ b/frontend/utility/ScreenshotObj.hpp @@ -29,6 +29,12 @@ class ScreenshotObj : public QObject { Q_OBJECT public: + struct Options { + QSize size{}; + bool outputToFile{true}; + }; + + ScreenshotObj(obs_source_t *source, const Options &options); ScreenshotObj(obs_source_t *source); ~ScreenshotObj() override; @@ -36,13 +42,10 @@ public: Stage stage() { return stage_; } - void setSize(QSize size); - void setSize(int width, int height); - void setSaveToFile(bool save); - +private: + static void renderTick(void *param, float seconds); void processStage(); -private: void renderScreenshot(); void downloadData(); void copyData(); @@ -50,23 +53,24 @@ private: void muxFile(); void onFinished(); + OBSWeakSource weakSource; + Stage stage_ = Stage::Render; + Options options; gs_texrender_t *texrender = nullptr; gs_stagesurf_t *stagesurf = nullptr; - OBSWeakSource weakSource; + std::string path; QImage image; std::vector half_bytes; - QSize customSize; + uint32_t sourceWidth = 0; uint32_t sourceHeight = 0; uint32_t outputWidth = 0; uint32_t outputHeight = 0; std::thread thread; - std::shared_ptr imagePtr; - bool outputToFile = true; signals: void imageSaved(std::string path); diff --git a/frontend/utility/ThumbnailItem.cpp b/frontend/utility/ThumbnailItem.cpp index 61a882251..a4f201c55 100644 --- a/frontend/utility/ThumbnailItem.cpp +++ b/frontend/utility/ThumbnailItem.cpp @@ -185,9 +185,11 @@ bool ThumbnailItem::update() return false; } - auto *obj = new ScreenshotObj(source); - obj->setSaveToFile(false); - obj->setSize(kDefaultWidth, kDefaultHeight); + ScreenshotObj::Options options; + options.outputToFile = false; + options.size = {kDefaultWidth, kDefaultHeight}; + + auto *obj = new ScreenshotObj(source, options); connect(obj, &ScreenshotObj::imageReady, this, &ThumbnailItem::updatePixmapFromImage); }