From 2d0f7bf2b8ffbc548448eeea058e8e3673f8f6e5 Mon Sep 17 00:00:00 2001 From: ReenigneArcher <42013603+ReenigneArcher@users.noreply.github.com> Date: Sat, 28 Nov 2026 00:05:34 -0500 Subject: [PATCH] refactor(streaming): compile Xbox scanout only for nxdk --- src/streaming/ffmpeg_video_presenter.cpp | 26 ++++++++---- src/streaming/ffmpeg_video_presenter.h | 54 ++++++++++++++---------- src/streaming/pvideo_presenter.cpp | 19 +-------- src/streaming/pvideo_presenter.h | 16 +++---- src/streaming/software_scanout.cpp | 22 ++-------- src/streaming/software_scanout.h | 18 ++++---- 6 files changed, 71 insertions(+), 84 deletions(-) diff --git a/src/streaming/ffmpeg_video_presenter.cpp b/src/streaming/ffmpeg_video_presenter.cpp index 75b2dad1..c1e08fa9 100644 --- a/src/streaming/ffmpeg_video_presenter.cpp +++ b/src/streaming/ffmpeg_video_presenter.cpp @@ -12,11 +12,11 @@ #include // lib includes +#ifdef NXDK extern "C" { -#include + #include } -#ifdef NXDK #include #endif @@ -127,7 +127,9 @@ namespace streaming { } void FfmpegVideoPresenter::reset() { +#ifdef NXDK stop_video_presentation(); +#endif if (statisticsTexture_ != nullptr) { SDL_DestroyTexture(statisticsTexture_); statisticsTexture_ = nullptr; @@ -142,22 +144,30 @@ namespace streaming { } textureWidth_ = 0; textureHeight_ = 0; +#ifdef NXDK sws_freeContext(presentScaleContext_); presentScaleContext_ = nullptr; - renderFrame_.clear(); overlayFailed_ = false; softwareScanoutFailed_ = false; +#endif + renderFrame_.clear(); } void FfmpegVideoPresenter::configure_gpu_presentation(bool enabled, GpuBufferCount bufferCount) { gpuPresentationEnabled_ = enabled; +#ifdef NXDK presenter_.set_buffer_count(bufferCount); +#else + (void) bufferCount; +#endif } std::string FfmpegVideoPresenter::presentation_path() const { +#ifdef NXDK if (presenter_.active()) { return "GPU"; } +#endif return acceleration_.softwareFrames > 0 ? "Software" : "Waiting"; } @@ -232,6 +242,7 @@ namespace streaming { } } +#ifdef NXDK void FfmpegVideoPresenter::stop_video_presentation() { if (!presenter_.reset()) { acceleration_.record_fallback("GPU overlay teardown timed out; acceleration disabled"); @@ -241,6 +252,7 @@ namespace streaming { logging::warn("stream", "Software scanout retirement timed out; extra framebuffer retained"); } } +#endif bool FfmpegVideoPresenter::render_video_frame(SDL_Renderer *renderer, int screenWidth, int screenHeight, bool newFrame) { const auto &frame = renderFrame_.view(); @@ -403,8 +415,8 @@ namespace streaming { } } - bool FfmpegVideoPresenter::render_latest_video_frame_to_framebuffer(int screenWidth, int screenHeight) { #ifdef NXDK + bool FfmpegVideoPresenter::render_latest_video_frame_to_framebuffer(int screenWidth, int screenHeight) { const VIDEO_MODE videoMode = XVideoGetMode(); const int bytesPerPixel = xbox_framebuffer_bytes_per_pixel(videoMode.bpp); const AVPixelFormat destinationFormat = xbox_framebuffer_pixel_format(videoMode.bpp); @@ -479,11 +491,7 @@ namespace streaming { statisticsOverlay_.draw_rgb(framebuffer, framebufferSize, framebufferPitch, videoMode.width, videoMode.height, videoMode.bpp, videoArea); saveIndicatorOverlay_.draw_rgb(framebuffer, framebufferSize, framebufferPitch, videoMode.width, videoMode.height, videoMode.bpp, videoArea); return softwareScanout_.present(); -#else - (void) screenWidth; - (void) screenHeight; - return false; -#endif } +#endif } // namespace streaming diff --git a/src/streaming/ffmpeg_video_presenter.h b/src/streaming/ffmpeg_video_presenter.h index b03eb7c1..08c99866 100644 --- a/src/streaming/ffmpeg_video_presenter.h +++ b/src/streaming/ffmpeg_video_presenter.h @@ -15,11 +15,15 @@ // local includes #include "src/streaming/ffmpeg_video_frame.h" -#include "src/streaming/pvideo_presenter.h" -#include "src/streaming/software_scanout.h" +#include "src/streaming/video_overlay.h" #include "src/ui/save_indicator.h" +#ifdef NXDK + #include "src/streaming/pvideo_presenter.h" + #include "src/streaming/software_scanout.h" + struct SwsContext; +#endif namespace streaming { /** @@ -43,7 +47,7 @@ namespace streaming { FfmpegVideoPresenter &operator=(const FfmpegVideoPresenter &) = delete; /** - * @brief Select the preferred presentation path and GPU buffer count for the new session. + * @brief Record the session's GPU preference and configure the NV2A buffer count on Xbox. * @param enabled True to try NV2A with automatic software fallback. * @param bufferCount Requested number of YUY2 buffers if NV2A is used. */ @@ -145,15 +149,10 @@ namespace streaming { */ bool present(SDL_Renderer *renderer, int screenWidth, int screenHeight, bool newFrame); - /** @brief Restore CPU framebuffer output and clear session-owned presentation resources. */ + /** @brief Retire Xbox scanout when present and clear session-owned presentation resources. */ void reset(); private: - /** - * @brief Restore CPU framebuffer output before drawing shutdown or summary UI on the render thread. - */ - void stop_video_presentation(); - /** * @brief Present a frame on the first available output path. * @param renderer SDL video renderer. @@ -164,6 +163,12 @@ namespace streaming { */ bool render_video_frame(SDL_Renderer *renderer, int screenWidth, int screenHeight, bool newFrame); +#ifdef NXDK + /** + * @brief Restore Xbox CPU framebuffer output before drawing shutdown or summary UI on the render thread. + */ + void stop_video_presentation(); + /** * @brief Present the current video and UI through the GPU overlay when available. * @@ -175,6 +180,16 @@ namespace streaming { */ bool render_gpu_video_frame(const SDL_Rect &destination, bool newFrame); + /** + * @brief Present the latest decoded frame through Xbox direct framebuffer scanout. + * + * @param screenWidth Current framebuffer width. + * @param screenHeight Current framebuffer height. + * @return True when the frame was presented without SDL renderer involvement. + */ + bool render_latest_video_frame_to_framebuffer(int screenWidth, int screenHeight); +#endif + /** * @brief Upload and present the current video and UI through SDL. * @param renderer SDL video renderer. @@ -196,23 +211,18 @@ namespace streaming { */ void invalidate_overlay_texture(); - /** - * @brief Present the latest decoded frame through a platform-specific direct framebuffer path. - * - * @param screenWidth Current framebuffer width. - * @param screenHeight Current framebuffer height. - * @return True when the frame was presented without SDL renderer involvement. - */ - bool render_latest_video_frame_to_framebuffer(int screenWidth, int screenHeight); - VideoAccelerationReport &acceleration_; ///< Session diagnostics, owned by the backend. FfmpegVideoFrame renderFrame_; ///< Frame retained for redraws and captures. - SwsContext *presentScaleContext_ = nullptr; ///< Software framebuffer conversion context. SDL_Texture *texture_ = nullptr; ///< SDL fallback video texture. int textureWidth_ = 0; ///< Width of the uploaded SDL video texture. int textureHeight_ = 0; ///< Height of the uploaded SDL video texture. - PvideoPresenter presenter_; ///< Native GPU scanout. - SoftwareScanout softwareScanout_; ///< Native direct framebuffer scanout. +#ifdef NXDK + SwsContext *presentScaleContext_ = nullptr; ///< Xbox software framebuffer conversion context. + PvideoPresenter presenter_; ///< Xbox GPU scanout. + SoftwareScanout softwareScanout_; ///< Xbox direct framebuffer scanout. + bool overlayFailed_ = false; ///< Avoid retrying a failed GPU path in the same session. + bool softwareScanoutFailed_ = false; ///< Retain the SDL fallback after native software scanout fails. +#endif VideoOverlay statisticsOverlay_; ///< Shared cached panel for all presentation paths. VideoOverlay saveIndicatorOverlay_; ///< Independent corner overlay drawn above stream controls. ui::SaveIndicator saveIndicator_; ///< Cached logo artwork for the save overlay. @@ -222,7 +232,5 @@ namespace streaming { bool statisticsTextureChanged_ = false; ///< SDL texture requires the current bitmap. VideoRectangle statisticsTextureDamage_ {}; ///< Pending SDL texture upload. bool gpuPresentationEnabled_ = false; ///< GPU preference for the session. - bool overlayFailed_ = false; ///< Avoid retrying a failed GPU path in the same session. - bool softwareScanoutFailed_ = false; ///< Retain the SDL fallback after native software scanout fails. }; } // namespace streaming diff --git a/src/streaming/pvideo_presenter.cpp b/src/streaming/pvideo_presenter.cpp index d8b36a7e..24901a82 100644 --- a/src/streaming/pvideo_presenter.cpp +++ b/src/streaming/pvideo_presenter.cpp @@ -131,7 +131,6 @@ namespace { return static_cast((static_cast(source - 1) << 20U) / static_cast(destination - 1)); } } // namespace -#endif namespace streaming { PvideoPresenter::~PvideoPresenter() { @@ -154,7 +153,6 @@ namespace streaming { } bool PvideoPresenter::initialize_scanout_surface(std::string *error) { -#ifdef NXDK const VIDEO_MODE mode = XVideoGetMode(); const auto *framebuffer = XVideoGetFB(); const int pitch = mode.width * (mode.bpp / 8); @@ -191,15 +189,10 @@ namespace streaming { return false; } return true; -#else - *error = "GPU scanout is unavailable on this platform"; - return false; -#endif } bool PvideoPresenter::reset() { bool synchronized = true; -#ifdef NXDK if (active_) { VIDEOREG(VIDEO_STOP) = 1; VIDEOREG(VIDEO_BUFFER) = 0; @@ -242,7 +235,6 @@ namespace streaming { XVideoSetVideoEnable(true); scanoutStateSaved_ = false; } -#endif active_ = false; bufferSize_ = 0; bufferReuseVblank_ = 0; @@ -250,7 +242,6 @@ namespace streaming { return synchronized; } -#ifdef NXDK void PvideoPresenter::configure_overlay_buffers(std::size_t required, int pitch, std::string &failure) { if (!reset()) { failure = "GPU overlay teardown timed out"; @@ -366,7 +357,6 @@ namespace streaming { } return true; } -#endif bool PvideoPresenter::present(const Yuv420FrameView &frame, const VideoRectangle &destination, std::string *error, const OverlaySamplingRegion &sampling, VideoOverlay *overlay, VideoOverlay *saveIndicator) { std::string failure = overlay_frame_error(frame); @@ -379,7 +369,6 @@ namespace streaming { if (failure.empty() && (source.width < 2 || source.height < 2 || sampling.xHalfPel < 0 || sampling.yHalfPel < 0 || static_cast(sampling.xHalfPel) + 2LL * (source.width - 1) + (sampling.xHalfPel & 1) > 2LL * (frame.width - 1) || static_cast(sampling.yHalfPel) + 2LL * (source.height - 1) + (sampling.yHalfPel & 1) > 2LL * (frame.height - 1))) { failure = "Source sampling region exceeds the GPU input buffer"; } -#ifdef NXDK if (failure.empty() && overlay_quarantine().disabled) { failure = "GPU overlay disabled after a scanout timeout"; } @@ -401,13 +390,6 @@ namespace streaming { } failure = "Timed out waiting for the first GPU frame; using software presentation"; } -#else - (void) overlay; - (void) saveIndicator; - if (failure.empty()) { - failure = "GPU overlay is unavailable on this platform"; - } -#endif reset(); if (error != nullptr) { *error = failure; @@ -415,3 +397,4 @@ namespace streaming { return false; } } // namespace streaming +#endif diff --git a/src/streaming/pvideo_presenter.h b/src/streaming/pvideo_presenter.h index 337c952e..15d906c8 100644 --- a/src/streaming/pvideo_presenter.h +++ b/src/streaming/pvideo_presenter.h @@ -4,9 +4,10 @@ */ #pragma once -// local includes -#include "src/streaming/video_acceleration.h" -#include "src/streaming/video_overlay.h" +#ifdef NXDK + // local includes + #include "src/streaming/video_acceleration.h" + #include "src/streaming/video_overlay.h" namespace streaming { /** @@ -20,7 +21,7 @@ namespace streaming { }; /** - * @brief Main-thread-only NV2A YUV overlay presenter with a configurable spare buffer. + * @brief Main-thread-only Xbox NV2A YUV overlay presenter with a configurable spare buffer. * * Supports progressive limited-range BT.601/BT.709 YUV420 with a compatible * 16-bit or 32-bit output mode. Callers select software presentation when present() @@ -52,8 +53,8 @@ namespace streaming { * @param destination Letterboxed output rectangle. * @param error Receives the reason acceleration could not be used. * @param sampling Optional crop used by the standalone prediction experiment. - * @param overlay Optional cached UI or statistics, composited using the frame's color matrix before GPU scanout. - * @param saveIndicator Optional save icon composited on top of the cached UI. + * @param overlay Optional UI or statistics whose YUY2 cache is refreshed as needed before compositing. + * @param saveIndicator Optional save icon whose YUY2 cache is refreshed as needed before compositing over the UI. * * @return True after the first overlay frame has latched or a subsequent frame has been submitted. */ @@ -93,7 +94,6 @@ namespace streaming { */ bool initialize_scanout_surface(std::string *error); -#ifdef NXDK /** * @brief Replace the scanout surface and allocate the required buffers plus a spare when requested. * @@ -129,7 +129,6 @@ namespace streaming { * @return True if the submission succeeded or false if the first latch timed out. */ bool submit_overlay(); -#endif std::array buffers_ {}; ///< Two contiguous write-combined YUY2 buffers and an optional spare. GpuBufferCount requestedBufferCount_ = GpuBufferCount::automatic; ///< Preference used when configuring the DMA buffers. @@ -145,3 +144,4 @@ namespace streaming { bool surfaceActive_ = false; ///< pbkit owns the GPU context and vblank counter for the scanout surface. }; } // namespace streaming +#endif diff --git a/src/streaming/software_scanout.cpp b/src/streaming/software_scanout.cpp index eb65cdcf..14d1b935 100644 --- a/src/streaming/software_scanout.cpp +++ b/src/streaming/software_scanout.cpp @@ -5,9 +5,6 @@ // class header include #include "src/streaming/software_scanout.h" -// local includes -#include "src/_nxdk_compat/ffmpeg_memory.h" - #ifdef NXDK // standard includes #include @@ -18,6 +15,9 @@ #include #include + // local includes + #include "src/_nxdk_compat/ffmpeg_memory.h" + namespace { constexpr std::uint32_t CRTC_START = 0x600800; ///< Physical address of the primary scanout page. constexpr std::uint32_t CRTC_INTERRUPT = 0x600100; ///< Display interrupt status with write-one-to-clear semantics. @@ -47,7 +47,6 @@ namespace { VIDEOREG(CRTC_START) = static_cast(MmGetPhysicalAddress(page)); } } // namespace -#endif namespace streaming { void SoftwareScanout::StagingBufferDeleter::operator()(std::byte *buffer) const noexcept { @@ -59,7 +58,6 @@ namespace streaming { } std::uint8_t *SoftwareScanout::back_buffer(std::size_t size, const VideoRectangle &destination) { -#ifdef NXDK if (!synchronized_) { return nullptr; } @@ -91,15 +89,9 @@ namespace streaming { previous = destination; } return pages_[back]; -#else - (void) size; - (void) destination; - return nullptr; -#endif } bool SoftwareScanout::present() { -#ifdef NXDK if (pages_[1] == nullptr || !synchronized_) { return false; } @@ -118,13 +110,9 @@ namespace streaming { // keep reading the previous page until the first vblank after this write. synchronized_ = wait_for_vblank(); return synchronized_; -#else - return false; -#endif } bool SoftwareScanout::reset() { -#ifdef NXDK if (pages_[1] == nullptr) { return true; } @@ -147,8 +135,6 @@ namespace streaming { front_ = 0; synchronized_ = true; return retired; -#else - return true; -#endif } } // namespace streaming +#endif diff --git a/src/streaming/software_scanout.h b/src/streaming/software_scanout.h index 689299f6..6df449dc 100644 --- a/src/streaming/software_scanout.h +++ b/src/streaming/software_scanout.h @@ -4,18 +4,19 @@ */ #pragma once -// standard includes -#include -#include -#include -#include +#ifdef NXDK + // standard includes + #include + #include + #include + #include -// local includes -#include "src/streaming/video_acceleration.h" + // local includes + #include "src/streaming/video_acceleration.h" namespace streaming { /** - * @brief Display CPU-composited frames using page flips in SD and a fixed scanout in HD. + * @brief Display Xbox CPU-composited frames using page flips in SD and a fixed scanout in HD. * * The active video mode must remain fixed until reset. All operations belong * to the stream render thread. @@ -71,3 +72,4 @@ namespace streaming { bool synchronized_ = true; ///< A completed vblank has retired the previous page. }; } // namespace streaming +#endif