功能:完善2K串流、显示恢复与输入优化

This commit is contained in:
songyiming committed 2026-11-03 13:43:50 +08:00
1 parent e47b01beec
commit 938ec97cce
95 files changed
+5788 -72

No files matched your search

@@ -0,0 +1,271 @@
--- a/app/streaming/input/mouse.cpp
+++ b/app/streaming/input/mouse.cpp
@@ -1,4 +1,5 @@
#include "input.h"
+#include "vdi_mouse_batch.h"
#include <Limelight.h>
#include "SDL_compat.h"
@@ -84,6 +85,11 @@
button = BUTTON_RIGHT;
}
+ // Button coordinates are captured with this event, not a later mouse state.
+ if (m_AbsoluteMouseMode) {
+ sendAbsoluteMousePosition(event->x, event->y);
+ }
+
LiSendMouseButtonEvent(event->state == SDL_PRESSED ?
BUTTON_ACTION_PRESS :
BUTTON_ACTION_RELEASE,
@@ -101,81 +107,78 @@
return;
}
- // Batch all pending mouse motion events to save CPU time
- Sint32 x = event->x, y = event->y, xrel = event->xrel, yrel = event->yrel;
- SDL_Event nextEvent;
- while (SDL_PeepEvents(&nextEvent, 1, SDL_GETEVENT, SDL_MOUSEMOTION, SDL_MOUSEMOTION) > 0) {
- event = &nextEvent.motion;
-
- // Ignore synthetic mouse events
- if (event->which != SDL_TOUCH_MOUSEID) {
- x = event->x;
- y = event->y;
- xrel += event->xrel;
- yrel += event->yrel;
- }
- }
+ // Coalesce only adjacent motions. Never consume motion past a click/key
+ // or render event, and bound work so a fast mouse cannot starve rendering.
+ SDL_MouseMotionEvent batched = *event;
+ vdiBatchMouseMotion(batched);
+ Sint32 x = batched.x, y = batched.y;
+ Sint32 xrel = batched.xrel, yrel = batched.yrel;
// We should not reference the original event anymore
event = nullptr;
if (m_AbsoluteMouseMode) {
- int windowWidth, windowHeight;
- SDL_GetWindowSize(m_Window, &windowWidth, &windowHeight);
-
- SDL_Rect src, dst;
- bool mouseInVideoRegion;
-
- src.x = src.y = 0;
- src.w = m_StreamWidth;
- src.h = m_StreamHeight;
-
- dst.x = dst.y = 0;
- dst.w = windowWidth;
- dst.h = windowHeight;
-
- // Use the stream and window sizes to determine the video region
- StreamUtils::scaleSourceToDestinationSurface(&src, &dst);
-
- mouseInVideoRegion = isMouseInVideoRegion(x, y, windowWidth, windowHeight);
-
- // Clamp motion to the video region
- x = qMin(qMax(x - dst.x, 0), dst.w);
- y = qMin(qMax(y - dst.y, 0), dst.h);
-
- // Send the mouse position update if one of the following is true:
- // a) it is in the video region now
- // b) it just left the video region (to ensure the mouse is clamped to the video boundary)
- // c) a mouse button is still down from before the cursor left the video region (to allow smooth dragging)
- Uint32 buttonState = SDL_GetMouseState(nullptr, nullptr);
- if (buttonState == 0) {
- if (m_PendingMouseButtonsAllUpOnVideoRegionLeave) {
- if (m_NeedsManualCaptureOnLeave) {
- // Stop capturing the mouse now
- SDL_CaptureMouse(SDL_FALSE);
- }
- m_PendingMouseButtonsAllUpOnVideoRegionLeave = false;
- }
- }
- if (mouseInVideoRegion || m_MouseWasInVideoRegion || m_PendingMouseButtonsAllUpOnVideoRegionLeave) {
- LiSendMousePositionEvent((short)x, (short)y, dst.w, dst.h);
- }
-
- // Adjust the cursor visibility if applicable
- if (mouseInVideoRegion ^ m_MouseWasInVideoRegion) {
- SDL_ShowCursor((mouseInVideoRegion && m_MouseCursorCapturedVisibilityState == SDL_DISABLE) ? SDL_DISABLE : SDL_ENABLE);
- if (!mouseInVideoRegion && buttonState != 0) {
- // If we still have a button pressed on leave, wait for that to come up
- // before we stop sending mouse position events.
- m_PendingMouseButtonsAllUpOnVideoRegionLeave = true;
- }
- }
-
- m_MouseWasInVideoRegion = mouseInVideoRegion;
+ sendAbsoluteMousePosition(x, y);
}
else {
LiSendMouseMoveEvent(xrel, yrel);
}
+}
+
+void SdlInputHandler::sendAbsoluteMousePosition(Sint32 x, Sint32 y)
+{
+ int windowWidth, windowHeight;
+ SDL_GetWindowSize(m_Window, &windowWidth, &windowHeight);
+
+ SDL_Rect src, dst;
+ bool mouseInVideoRegion;
+
+ src.x = src.y = 0;
+ src.w = m_StreamWidth;
+ src.h = m_StreamHeight;
+
+ dst.x = dst.y = 0;
+ dst.w = windowWidth;
+ dst.h = windowHeight;
+
+ // Use the stream and window sizes to determine the video region
+ StreamUtils::scaleSourceToDestinationSurface(&src, &dst);
+
+ mouseInVideoRegion = isMouseInVideoRegion(x, y, windowWidth, windowHeight);
+
+ // Clamp motion to the video region
+ x = qMin(qMax(x - dst.x, 0), dst.w);
+ y = qMin(qMax(y - dst.y, 0), dst.h);
+
+ // Send the mouse position update if one of the following is true:
+ // a) it is in the video region now
+ // b) it just left the video region (to ensure the mouse is clamped to the video boundary)
+ // c) a mouse button is still down from before the cursor left the video region (to allow smooth dragging)
+ Uint32 buttonState = SDL_GetMouseState(nullptr, nullptr);
+ if (buttonState == 0) {
+ if (m_PendingMouseButtonsAllUpOnVideoRegionLeave) {
+ if (m_NeedsManualCaptureOnLeave) {
+ // Stop capturing the mouse now
+ SDL_CaptureMouse(SDL_FALSE);
+ }
+ m_PendingMouseButtonsAllUpOnVideoRegionLeave = false;
+ }
+ }
+ if (mouseInVideoRegion || m_MouseWasInVideoRegion || m_PendingMouseButtonsAllUpOnVideoRegionLeave) {
+ LiSendMousePositionEvent((short)x, (short)y, dst.w, dst.h);
+ }
+
+ // Adjust the cursor visibility if applicable
+ if (mouseInVideoRegion ^ m_MouseWasInVideoRegion) {
+ SDL_ShowCursor((mouseInVideoRegion && m_MouseCursorCapturedVisibilityState == SDL_DISABLE) ? SDL_DISABLE : SDL_ENABLE);
+ if (!mouseInVideoRegion && buttonState != 0) {
+ // If we still have a button pressed on leave, wait for that to come up
+ // before we stop sending mouse position events.
+ m_PendingMouseButtonsAllUpOnVideoRegionLeave = true;
+ }
+ }
+
+ m_MouseWasInVideoRegion = mouseInVideoRegion;
}
void SdlInputHandler::handleMouseWheelEvent(SDL_MouseWheelEvent* event)
--- a/app/streaming/input/input.h
+++ b/app/streaming/input/input.h
@@ -95,6 +95,8 @@
void handleMouseButtonEvent(SDL_MouseButtonEvent* event);
void handleMouseMotionEvent(SDL_MouseMotionEvent* event);
+
+ void sendAbsoluteMousePosition(Sint32 x, Sint32 y);
void handleMouseWheelEvent(SDL_MouseWheelEvent* event);
--- /dev/null
+++ b/app/streaming/input/vdi_mouse_batch.h
@@ -0,0 +1,22 @@
+#pragma once
+#include <SDL.h>
+
+// Only the SDL main thread consumes events; other threads may append events.
+inline void vdiBatchMouseMotion(SDL_MouseMotionEvent& motion)
+{
+ for (int i = 0; i < 64; ++i) {
+ SDL_Event next;
+ if (SDL_PeepEvents(&next, 1, SDL_PEEKEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) != 1 ||
+ next.type != SDL_MOUSEMOTION || next.motion.which != motion.which ||
+ next.motion.windowID != motion.windowID || next.motion.state != motion.state) {
+ break;
+ }
+ if (SDL_PeepEvents(&next, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) != 1) {
+ break;
+ }
+ motion.x = next.motion.x;
+ motion.y = next.motion.y;
+ motion.xrel += next.motion.xrel;
+ motion.yrel += next.motion.yrel;
+ }
+}
--- a/app/streaming/video/ffmpeg-renderers/pacer/pacer.cpp
+++ b/app/streaming/video/ffmpeg-renderers/pacer/pacer.cpp
@@ -1,4 +1,5 @@
#include "pacer.h"
+#include "vdi_latest_frame.h"
#include "streaming/streamutils.h"
#ifdef Q_OS_WIN32
@@ -34,6 +35,7 @@
m_VsyncThread(nullptr),
m_DeferredFreeFrame(nullptr),
m_Stopping(false),
+ m_LatestFrameOnly(qEnvironmentVariableIntValue("VDI_LATEST_FRAME") == 1),
m_VsyncSource(nullptr),
m_VsyncRenderer(renderer),
m_MaxVideoFps(0),
@@ -178,7 +180,14 @@
void Pacer::enqueueFrameForRenderingAndUnlock(AVFrame *frame)
{
- dropFrameForEnqueue(m_RenderQueue);
+ if (m_LatestFrameOnly && m_VsyncSource == nullptr) {
+ // Drop only decoded, never presented frames. Keep the decoder reference
+ // chain and the deferred free of the last presented frame intact.
+ m_VideoStats->pacerDroppedFrames += vdiDiscardQueuedFrames(m_RenderQueue);
+ }
+ else {
+ dropFrameForEnqueue(m_RenderQueue);
+ }
m_RenderQueue.enqueue(frame);
m_FrameQueueLock.unlock();
@@ -261,6 +270,8 @@
bool Pacer::initialize(SDL_Window* window, int maxVideoFps, bool enablePacing)
{
+ SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "VDI latest-frame queue: %s",
+ (m_LatestFrameOnly && !enablePacing) ? "enabled" : "disabled");
m_MaxVideoFps = maxVideoFps;
m_DisplayFps = StreamUtils::getDisplayRefreshRate(window);
m_RendererAttributes = m_VsyncRenderer->getRendererAttributes();
--- a/app/streaming/video/ffmpeg-renderers/pacer/pacer.h
+++ b/app/streaming/video/ffmpeg-renderers/pacer/pacer.h
@@ -68,6 +68,7 @@
SDL_Thread* m_VsyncThread;
AVFrame* m_DeferredFreeFrame;
bool m_Stopping;
+ bool m_LatestFrameOnly;
IVsyncSource* m_VsyncSource;
IFFmpegRenderer* m_VsyncRenderer;
--- /dev/null
+++ b/app/streaming/video/ffmpeg-renderers/pacer/vdi_latest_frame.h
@@ -0,0 +1,16 @@
+#pragma once
+#include <QQueue>
+extern "C" {
+#include <libavutil/frame.h>
+}
+
+inline unsigned vdiDiscardQueuedFrames(QQueue<AVFrame*>& queue)
+{
+ unsigned dropped = 0;
+ while (!queue.isEmpty()) {
+ AVFrame* stale = queue.dequeue();
+ av_frame_free(&stale);
+ ++dropped;
+ }
+ return dropped;
+}
@@ -0,0 +1,143 @@
--- a/src/InputStream.c
+++ b/src/InputStream.c
@@ -86,11 +86,16 @@
int deltaX, deltaY;
bool dirty; // Update ready to send (queued packet holder in packetQueue)
} currentRelativeMouseState;
-static struct {
- int x, y;
- int width, height;
- bool dirty; // Update ready to send (queued packet holder in packetQueue)
-} currentAbsoluteMouseState;
+// Only this packet may absorb newer positions. A button event seals the batch.
+static PPACKET_HOLDER currentQueuedAbsoluteMousePacket;
+
+static void latchAbsoluteMousePacket(PPACKET_HOLDER holder) {
+ PltLockMutex(&batchedInputMutex);
+ if (currentQueuedAbsoluteMousePacket == holder) {
+ currentQueuedAbsoluteMousePacket = NULL;
+ }
+ PltUnlockMutex(&batchedInputMutex);
+}
// Initializes the input stream
int initializeInputStream(void) {
@@ -120,7 +125,7 @@
memset(currentGamepadSensorState, 0, sizeof(currentGamepadSensorState));
memset(&currentRelativeMouseState, 0, sizeof(currentRelativeMouseState));
- memset(&currentAbsoluteMouseState, 0, sizeof(currentAbsoluteMouseState));
+ currentQueuedAbsoluteMousePacket = NULL;
PltCreateMutex(&batchedInputMutex);
return 0;
@@ -444,24 +449,9 @@
now = PltGetMillis();
}
- PltLockMutex(&batchedInputMutex);
-
- // Populate the packet with the latest state
- holder->packet.mouseMoveAbs.x = BE16(currentAbsoluteMouseState.x);
- holder->packet.mouseMoveAbs.y = BE16(currentAbsoluteMouseState.y);
-
- // There appears to be a rounding error in GFE's scaling calculation which prevents
- // the cursor from reaching the far edge of the screen when streaming at smaller
- // resolutions with a higher desktop resolution (like streaming 720p with a desktop
- // resolution of 1080p, or streaming 720p/1080p with a desktop resolution of 4K).
- // Subtracting one from the reference dimensions seems to work around this issue.
- holder->packet.mouseMoveAbs.width = BE16(currentAbsoluteMouseState.width - 1);
- holder->packet.mouseMoveAbs.height = BE16(currentAbsoluteMouseState.height - 1);
-
- // The state change is no longer pending
- currentAbsoluteMouseState.dirty = false;
-
- PltUnlockMutex(&batchedInputMutex);
+ // Payload was filled by the producer under the same mutex. Latch
+ // only our packet; a later batch may already be queued after a click.
+ latchAbsoluteMousePacket(holder);
lastMousePacketTime = now;
}
@@ -780,52 +770,37 @@
}
PltLockMutex(&batchedInputMutex);
-
- // Overwrite the previous mouse location with the new one
- currentAbsoluteMouseState.x = x;
- currentAbsoluteMouseState.y = y;
- currentAbsoluteMouseState.width = referenceWidth;
- currentAbsoluteMouseState.height = referenceHeight;
-
- // Queue a packet holder if this is the only pending absolute mouse event
- if (!currentAbsoluteMouseState.dirty) {
- // Set the dirty flag to claim ownership of inserting the packet holder
- // and unlock to allow other threads to enqueue or process input.
- currentAbsoluteMouseState.dirty = true;
- PltUnlockMutex(&batchedInputMutex);
-
+ holder = currentQueuedAbsoluteMousePacket;
+ bool newPacket = holder == NULL;
+ if (newPacket) {
holder = allocatePacketHolder(0);
if (holder == NULL) {
- currentAbsoluteMouseState.dirty = false;
+ PltUnlockMutex(&batchedInputMutex);
return -1;
}
-
holder->channelId = CTRL_CHANNEL_MOUSE;
-
- // TODO: Send this as unreliable sequenced when we have a delayed reliable retransmission thread
holder->enetPacketFlags = ENET_PACKET_FLAG_RELIABLE;
-
holder->packet.mouseMoveAbs.header.size = BE32(sizeof(NV_ABS_MOUSE_MOVE_PACKET) - sizeof(uint32_t));
holder->packet.mouseMoveAbs.header.magic = LE32(MOUSE_MOVE_ABS_MAGIC);
holder->packet.mouseMoveAbs.unused = 0;
-
- // Remaining fields are set in the input thread based on the latest currentAbsoluteMouseState values
-
+ }
+ holder->packet.mouseMoveAbs.x = BE16(x);
+ holder->packet.mouseMoveAbs.y = BE16(y);
+ // Preserve upstream's far-edge rounding workaround.
+ holder->packet.mouseMoveAbs.width = BE16(referenceWidth - 1);
+ holder->packet.mouseMoveAbs.height = BE16(referenceHeight - 1);
+ err = 0;
+ if (newPacket) {
+ currentQueuedAbsoluteMousePacket = holder;
err = LbqOfferQueueItem(&packetQueue, holder, &holder->entry);
if (err != LBQ_SUCCESS) {
+ currentQueuedAbsoluteMousePacket = NULL;
LC_ASSERT(err == LBQ_BOUND_EXCEEDED);
Limelog("Input queue reached maximum size limit\n");
freePacketHolder(holder);
-
- // We weren't able to insert the entry, so let the next call try again
- currentAbsoluteMouseState.dirty = false;
- }
- }
- else {
- // There's already a packet holder queued to send this event
- PltUnlockMutex(&batchedInputMutex);
- err = 0;
- }
+ }
+ }
+ PltUnlockMutex(&batchedInputMutex);
// This is not thread safe, but it's not a big deal because callers that want to
// use LiSendRelativeMotionAsMousePositionEvent() must not mix these function
@@ -872,7 +847,11 @@
holder->packet.mouseButton.header.magic = LE32(holder->packet.mouseButton.header.magic);
holder->packet.mouseButton.button = (uint8_t)button;
+ PltLockMutex(&batchedInputMutex);
+ // Do not let a later position overwrite the move preceding this click.
+ currentQueuedAbsoluteMousePacket = NULL;
err = LbqOfferQueueItem(&packetQueue, holder, &holder->entry);
+ PltUnlockMutex(&batchedInputMutex);
if (err != LBQ_SUCCESS) {
LC_ASSERT(err == LBQ_BOUND_EXCEEDED);
Limelog("Input queue reached maximum size limit\n");
@@ -0,0 +1,179 @@
--- a/app/streaming/session.cpp
+++ b/app/streaming/session.cpp
@@ -39,6 +39,7 @@
#include <QImage>
#include <QGuiApplication>
#include <QCursor>
+#include "input/vdi_input_priority.h"
#include <QScreen>
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
@@ -1962,8 +1963,14 @@
// Hijack this thread to be the SDL main thread. We have to do this
// because we want to suspend all Qt processing until the stream is over.
+ VdiInputPriority vdiInputPriority(qEnvironmentVariableIntValue("VDI_INPUT_PRIORITY") == 1,
+ SDL_CODE_FRAME_READY);
+ VdiInputTiming vdiInputTiming(qEnvironmentVariableIntValue("VDI_INPUT_TIMING") == 1);
+ SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "VDI input priority: %s",
+ qEnvironmentVariableIntValue("VDI_INPUT_PRIORITY") == 1 ? "enabled" : "disabled");
SDL_Event event;
for (;;) {
+ if (!vdiInputPriority.takeDeferred(event, SDL_GetWindowID(m_Window))) {
#if SDL_VERSION_ATLEAST(2, 0, 18) && !defined(STEAM_LINK)
// SDL 2.0.18 has a proper wait event implementation that uses platform
// support to block on events rather than polling on Windows, macOS, X11,
@@ -1995,6 +2002,11 @@
continue;
}
#endif
+ if (vdiInputPriority.deferFrame(event)) {
+ continue;
+ }
+ }
+ vdiInputTiming.observe(event, SDL_GetWindowID(m_Window));
switch (event.type) {
case SDL_QUIT:
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
@@ -2005,7 +2017,9 @@
switch (event.user.code) {
case SDL_CODE_FRAME_READY:
if (m_VideoDecoder != nullptr) {
+ const auto renderStart = vdiInputTiming.startRender();
m_VideoDecoder->renderFrameOnMainThread();
+ vdiInputTiming.endRender(renderStart);
}
break;
case SDL_CODE_FLUSH_WINDOW_EVENT_BARRIER:
@@ -2308,6 +2322,7 @@
}
DispatchDeferredCleanup:
+ vdiInputTiming.report(vdiInputPriority.prioritized());
// Switch back to synchronous logging mode
StreamUtils::exitAsyncLoggingMode();
--- /dev/null
+++ b/app/streaming/input/vdi_input_priority.h
@@ -0,0 +1,121 @@
+#pragma once
+#include <SDL.h>
+#include <array>
+#include <algorithm>
+#include <cstdint>
+
+// SDL main thread only. Inputs may pass one frame-ready notification, never a
+// window/focus/quit/other notification or another frame. Input FIFO is preserved.
+class VdiInputPriority {
+public:
+ VdiInputPriority(bool enabled, Sint32 frameCode)
+ : m_Enabled(enabled), m_FrameCode(frameCode) {}
+
+ bool deferFrame(const SDL_Event& event) {
+ if (!m_Enabled || event.type != SDL_USEREVENT || event.user.code != m_FrameCode)
+ return false;
+ SDL_assert(!m_Pending);
+ m_Frame = event;
+ m_Pending = true;
+ m_Count = 0;
+ m_Start = SDL_GetPerformanceCounter();
+ return true;
+ }
+
+ bool takeDeferred(SDL_Event& event, Uint32 windowId) {
+ if (!m_Pending) return false;
+ SDL_Event next;
+ const auto elapsed = SDL_GetPerformanceCounter() - m_Start;
+ if (m_Count < 32 && elapsed < SDL_GetPerformanceFrequency() / 500 &&
+ SDL_PeepEvents(&next, 1, SDL_PEEKEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) == 1 &&
+ isInputForWindow(next, windowId) &&
+ SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) == 1) {
+ ++m_Count;
+ ++m_Prioritized;
+ return true;
+ }
+ event = m_Frame;
+ m_Pending = false;
+ return true;
+ }
+
+ uint64_t prioritized() const { return m_Prioritized; }
+
+ static bool isInputForWindow(const SDL_Event& event, Uint32 windowId) {
+ switch (event.type) {
+ case SDL_MOUSEMOTION:
+ return event.motion.windowID == windowId && event.motion.which != SDL_TOUCH_MOUSEID;
+ case SDL_MOUSEBUTTONDOWN:
+ case SDL_MOUSEBUTTONUP:
+ return event.button.windowID == windowId && event.button.which != SDL_TOUCH_MOUSEID;
+ case SDL_MOUSEWHEEL:
+ return event.wheel.windowID == windowId && event.wheel.which != SDL_TOUCH_MOUSEID;
+ case SDL_KEYDOWN:
+ case SDL_KEYUP:
+ return event.key.windowID == windowId;
+ default:
+ return false;
+ }
+ }
+
+private:
+ bool m_Enabled;
+ Sint32 m_FrameCode;
+ bool m_Pending = false;
+ SDL_Event m_Frame{};
+ unsigned m_Count = 0;
+ Uint64 m_Start = 0;
+ uint64_t m_Prioritized = 0;
+};
+
+// Aggregated only when explicitly requested. Ages are SDL queue/dispatch ages,
+// not network RTT or physical input-to-photon latency. 0.1 ms histogram bins.
+class VdiInputTiming {
+public:
+ explicit VdiInputTiming(bool enabled) : m_Enabled(enabled) {}
+ void observe(const SDL_Event& event, Uint32 windowId) {
+ if (!m_Enabled || !event.common.timestamp ||
+ !VdiInputPriority::isInputForWindow(event, windowId)) return;
+ const Uint32 age = SDL_GetTicks() - event.common.timestamp;
+ // Ignore invalid/future synthetic timestamps; subtraction handles wrap.
+ if (age < 60000) m_Input.add(Uint64(age) * 10);
+ }
+ Uint64 startRender() const { return m_Enabled ? SDL_GetPerformanceCounter() : 0; }
+ void endRender(Uint64 start) {
+ if (m_Enabled) m_Render.add((SDL_GetPerformanceCounter() - start) * 10000 / SDL_GetPerformanceFrequency());
+ }
+ void report(uint64_t prioritized) const {
+ if (!m_Enabled) return;
+ SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
+ "VDI input timing: SDL dispatch age only; prioritized=%llu",
+ (unsigned long long)prioritized);
+ m_Input.report("input dispatch age");
+ m_Render.report("main-thread render call");
+ }
+private:
+ struct Histogram {
+ std::array<uint64_t, 1024> bins{};
+ uint64_t count = 0, total = 0, max = 0;
+ void add(uint64_t tenths) {
+ ++bins[std::min<uint64_t>(tenths, bins.size()-1)];
+ ++count; total += tenths; max = std::max(max, tenths);
+ }
+ double quantile(unsigned percent) const {
+ const uint64_t rank = (count * percent + 99) / 100;
+ uint64_t accumulated = 0;
+ for (size_t i = 0; i < bins.size(); ++i) {
+ accumulated += bins[i];
+ if (accumulated >= rank) return i / 10.0;
+ }
+ return 0;
+ }
+ void report(const char* label) const {
+ SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
+ "VDI %s: samples=%llu avg=%.2f p50=%.1f p95=%.1f max=%.1f ms (percentiles capped at 102.3ms)",
+ label, (unsigned long long)count, count ? total / (10.0*count) : 0,
+ count ? quantile(50) : 0, count ? quantile(95) : 0, max / 10.0);
+ }
+ };
+ bool m_Enabled;
+ Histogram m_Input, m_Render;
+};