Implement M9.2 native client lifecycle and immutable render boundary
This commit is contained in:
@@ -0,0 +1,160 @@
|
||||
#include "evo/client/application.hpp"
|
||||
#include "render/sdl_renderer.hpp"
|
||||
#include <SDL3/SDL.h>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <limits>
|
||||
#include <source_location>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
using namespace evo::client;
|
||||
void check(bool ok, std::source_location at = std::source_location::current()) {
|
||||
if (!ok)
|
||||
throw std::runtime_error("client lifecycle line " + std::to_string(at.line()));
|
||||
}
|
||||
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
|
||||
template <typename T, typename E> bool refused(const std::expected<T, E>& result, E error) {
|
||||
return !result && result.error() == error;
|
||||
}
|
||||
evo::render::Frame fixture() {
|
||||
const std::array<evo::render::Rectangle, 1> rectangles{{{0, 0, 32, 32, {255, 0, 0, 255}}}};
|
||||
auto result = evo::render::Frame::create(rectangles);
|
||||
CHECK(result);
|
||||
return std::move(result.value());
|
||||
}
|
||||
SDL_Window* window() {
|
||||
int count{};
|
||||
SDL_Window** windows = SDL_GetWindows(&count);
|
||||
SDL_Window* result = count == 1 ? windows[0] : nullptr;
|
||||
SDL_free(static_cast<void*>(windows));
|
||||
CHECK(result);
|
||||
return result;
|
||||
}
|
||||
void send(SDL_EventType type, SDL_WindowID id) {
|
||||
SDL_Event event{};
|
||||
event.type = type;
|
||||
if (type >= SDL_EVENT_RENDER_TARGETS_RESET && type <= SDL_EVENT_RENDER_DEVICE_LOST)
|
||||
event.render.windowID = id;
|
||||
else
|
||||
event.window.windowID = id;
|
||||
CHECK(SDL_PushEvent(&event));
|
||||
}
|
||||
void frame_contract() {
|
||||
using namespace evo::render;
|
||||
std::array<Rectangle, 1> r{{{1, 2, 3, 4, {5, 6, 7, 255}}}};
|
||||
const auto frame = Frame::create(r);
|
||||
CHECK(frame);
|
||||
r[0].x = 100;
|
||||
CHECK(frame->rectangles()[0].x == 1); // Owns copy; caller mutation cannot alter it.
|
||||
r[0].x = std::numeric_limits<float>::quiet_NaN();
|
||||
CHECK(refused(Frame::create(r), FrameError::invalid_geometry));
|
||||
r[0].x = 0;
|
||||
r[0].width = -1;
|
||||
CHECK(refused(Frame::create(r), FrameError::invalid_geometry));
|
||||
r[0].width = std::numeric_limits<float>::infinity();
|
||||
CHECK(refused(Frame::create(r), FrameError::invalid_geometry));
|
||||
std::vector<Rectangle> full(max_rectangles);
|
||||
CHECK(Frame::create(full));
|
||||
full.emplace_back();
|
||||
CHECK(refused(Frame::create(full), FrameError::too_many_primitives));
|
||||
}
|
||||
void pixels(SDL_Window* native, const evo::render::Frame& frame, WindowState state) {
|
||||
auto* renderer = SDL_GetRenderer(native);
|
||||
CHECK(renderer);
|
||||
// Exercise non-unit, anisotropic drawable scaling without requiring a HiDPI monitor.
|
||||
state.logical_width = state.pixel_width / 2;
|
||||
state.logical_height = state.pixel_height;
|
||||
CHECK(detail::draw(renderer, frame, state));
|
||||
const std::unique_ptr<SDL_Surface, decltype(&SDL_DestroySurface)> surface{
|
||||
SDL_RenderReadPixels(renderer, nullptr), SDL_DestroySurface};
|
||||
CHECK(surface);
|
||||
Uint8 r{}, g{}, b{}, a{};
|
||||
CHECK(SDL_ReadSurfacePixel(surface.get(), 48, 8, &r, &g, &b, &a));
|
||||
CHECK(r == 255 && g == 0 && b == 0 && a == 255);
|
||||
CHECK(SDL_ReadSurfacePixel(surface.get(), 80, 8, &r, &g, &b, &a));
|
||||
CHECK(r == 0 && g == 0 && b == 0 && a == 255);
|
||||
CHECK(SDL_ReadSurfacePixel(surface.get(), 8, 48, &r, &g, &b, &a));
|
||||
CHECK(r == 0 && g == 0 && b == 0 && a == 255);
|
||||
}
|
||||
void lifecycle() {
|
||||
const auto frame = fixture();
|
||||
CHECK(refused(Application::open({0, 64, false, "software"}), Error::invalid_config));
|
||||
CHECK(refused(Application::open({64, 64, false, "evo-invalid"}), Error::renderer));
|
||||
CHECK(SDL_WasInit(0) == 0); // Failed partial initialization released SDL.
|
||||
for (int round = 0; round < 32; ++round) {
|
||||
auto result = Application::open({160, 120, false, "software"});
|
||||
CHECK(result);
|
||||
auto app = std::move(result.value());
|
||||
CHECK(refused(Application::open({}), Error::already_open));
|
||||
CHECK(app->step(frame) == Step::rendered);
|
||||
auto* native = window();
|
||||
pixels(native, frame, app->state());
|
||||
const auto id = SDL_GetWindowID(native);
|
||||
// Foreign close/reset must not affect the one owned window.
|
||||
send(SDL_EVENT_WINDOW_CLOSE_REQUESTED, id + 1);
|
||||
send(SDL_EVENT_RENDER_DEVICE_LOST, id + 1);
|
||||
send(SDL_EVENT_WINDOW_FOCUS_GAINED, id);
|
||||
CHECK(app->step(frame) == Step::rendered);
|
||||
CHECK(app->state().accepts_input());
|
||||
send(SDL_EVENT_WINDOW_FOCUS_LOST, id);
|
||||
CHECK(app->step(frame) == Step::rendered);
|
||||
CHECK(!app->state().accepts_input());
|
||||
send(SDL_EVENT_WINDOW_MINIMIZED, id);
|
||||
CHECK(app->step(frame) == Step::paused);
|
||||
send(SDL_EVENT_WINDOW_RESTORED, id);
|
||||
CHECK(app->step(frame) == Step::rendered);
|
||||
CHECK(SDL_SetWindowSize(native, 240, 180));
|
||||
send(SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED, id);
|
||||
send(SDL_EVENT_WINDOW_DISPLAY_SCALE_CHANGED, id);
|
||||
CHECK(app->step(frame) == Step::rendered);
|
||||
int lw{}, lh{}, pw{}, ph{};
|
||||
CHECK(SDL_GetWindowSize(native, &lw, &lh));
|
||||
CHECK(SDL_GetWindowSizeInPixels(native, &pw, &ph));
|
||||
const auto state = app->state();
|
||||
CHECK(state.logical_width == lw && state.logical_height == lh);
|
||||
CHECK(state.pixel_width == pw && state.pixel_height == ph);
|
||||
CHECK(std::isfinite(state.content_scale) && state.content_scale > 0);
|
||||
send(SDL_EVENT_RENDER_TARGETS_RESET, id);
|
||||
CHECK(app->step(frame) == Step::rendered);
|
||||
send(SDL_EVENT_RENDER_DEVICE_RESET, id);
|
||||
CHECK(app->step(frame) == Step::rendered);
|
||||
CHECK(SDL_HideWindow(native));
|
||||
send(SDL_EVENT_WINDOW_HIDDEN, id);
|
||||
CHECK(app->step(frame) == Step::paused);
|
||||
CHECK(SDL_ShowWindow(native));
|
||||
send(SDL_EVENT_WINDOW_SHOWN, id);
|
||||
CHECK(app->step(frame) == Step::rendered);
|
||||
if (round % 2 == 0) {
|
||||
send(SDL_EVENT_RENDER_DEVICE_LOST, id);
|
||||
CHECK(refused(app->step(frame), Error::device_lost));
|
||||
CHECK(refused(app->step(frame), Error::device_lost));
|
||||
CHECK(app->state().failed && !app->state().accepts_input());
|
||||
} else {
|
||||
send(SDL_EVENT_WINDOW_CLOSE_REQUESTED, id);
|
||||
CHECK(app->step(frame) == Step::closed);
|
||||
CHECK(app->step(frame) == Step::closed);
|
||||
}
|
||||
app.reset();
|
||||
CHECK(SDL_WasInit(0) == 0);
|
||||
}
|
||||
auto hidden = Application::open({64, 64, true, "software"});
|
||||
CHECK(hidden);
|
||||
CHECK((*hidden)->step(frame) == Step::paused);
|
||||
send(SDL_EVENT_QUIT, 0);
|
||||
CHECK((*hidden)->step(frame) == Step::closed);
|
||||
}
|
||||
} // namespace
|
||||
int main() try {
|
||||
frame_contract();
|
||||
lifecycle();
|
||||
return 0;
|
||||
} catch (const std::exception& e) {
|
||||
std::fprintf(stderr, "%s: %s\n", e.what(), SDL_GetError());
|
||||
return 1;
|
||||
} catch (...) {
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user