vdi-cloud/terminal/moonlight/tests/test_latency.cpp

76 lines
2.7 KiB
C++

#include <SDL.h>
#include <cstdio>
#include "vdi_mouse_batch.h"
#include "vdi_latest_frame.h"
#undef NDEBUG
#include <cassert>
static SDL_Event motion(int x, Uint32 which = 1)
{
SDL_Event event{};
event.type = SDL_MOUSEMOTION;
event.motion.which = which;
event.motion.windowID = 1;
event.motion.x = x;
event.motion.y = 100;
event.motion.xrel = 1;
return event;
}
int main()
{
assert(SDL_Init(SDL_INIT_EVENTS) == 0);
for (Uint32 boundary : {Uint32(SDL_MOUSEBUTTONDOWN), Uint32(SDL_MOUSEBUTTONUP),
Uint32(SDL_KEYDOWN), Uint32(SDL_USEREVENT)}) {
SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
SDL_Event first = motion(10), adjacent = motion(20), later = motion(90), barrier{};
barrier.type = boundary;
assert(SDL_PushEvent(&adjacent) == 1);
assert(SDL_PushEvent(&barrier) == 1);
assert(SDL_PushEvent(&later) == 1);
vdiBatchMouseMotion(first.motion);
assert(first.motion.x == 20 && first.motion.xrel == 2);
SDL_Event next;
assert(SDL_PeepEvents(&next, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) == 1);
assert(next.type == boundary);
assert(SDL_PeepEvents(&next, 1, SDL_GETEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) == 1);
assert(next.motion.x == 90);
}
SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
SDL_Event first = motion(0);
for (int i = 1; i <= 200; ++i) {
SDL_Event next = motion(i);
assert(SDL_PushEvent(&next) == 1);
}
vdiBatchMouseMotion(first.motion);
assert(first.motion.x == 64); // Work per dispatch is bounded.
assert(SDL_PeepEvents(nullptr, 0, SDL_PEEKEVENT, SDL_FIRSTEVENT, SDL_LASTEVENT) == 136);
SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT);
first = motion(10);
SDL_Event synthetic = motion(99, SDL_TOUCH_MOUSEID);
assert(SDL_PushEvent(&synthetic) == 1);
vdiBatchMouseMotion(first.motion);
assert(first.motion.x == 10);
AVFrame* presented = av_frame_alloc();
assert(presented);
presented->buf[0] = av_buffer_alloc(16);
assert(presented->buf[0]);
QQueue<AVFrame*> queue;
assert(vdiDiscardQueuedFrames(queue) == 0);
for (int i = 0; i < 200; ++i) {
unsigned dropped = vdiDiscardQueuedFrames(queue);
assert(dropped == (i == 0 ? 0u : 1u));
AVFrame* frame = av_frame_alloc();
assert(frame);
frame->pts = i;
queue.enqueue(frame);
assert(queue.size() == 1 && queue.head()->pts == i);
assert(av_buffer_get_ref_count(presented->buf[0]) == 1);
}
assert(vdiDiscardQueuedFrames(queue) == 1);
av_frame_free(&presented);
SDL_Quit();
std::puts("PASS: SDL event boundaries, bounded motion work, latest decoded frame ownership");
}