diff --git a/CMakeLists.txt b/CMakeLists.txt index 01e3284..42b7af6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.28) project(EpochPlatformEngine - VERSION 0.2.1 + VERSION 0.2.2 DESCRIPTION "Cross-platform C++23 multi-context platform runtime" LANGUAGES CXX) diff --git a/README.md b/README.md index 368fdb6..d945553 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # EpochPlatformEngine -Current source version: **0.2.1**. Release artifacts and native acceptance are separate gates. +Current source candidate: **0.2.2**, prepared locally and unpublished. Release artifacts and native acceptance are separate gates. See [release notes](docs/release-notes.md). MPL-2.0 permits commercial use, including proprietary larger works subject to its covered-file obligations. See [Commercial use and giving back](#commercial-use-and-giving-back) for licensing, origin links and voluntary credit requests. diff --git a/RELEASE_VERSION b/RELEASE_VERSION index 0c62199..ee1372d 100644 --- a/RELEASE_VERSION +++ b/RELEASE_VERSION @@ -1 +1 @@ -0.2.1 +0.2.2 diff --git a/docs/release-notes.md b/docs/release-notes.md index 70c6161..49a96de 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -1,4 +1,20 @@ -# EpochPlatformEngine v0.1.1 +# EpochPlatformEngine release notes + +## 0.2.2 — local unpublished candidate + +- Default unselected frame pacing to 30 Hz; retain explicit finite rates, VSync + and uncapped selection. Each limiter turn offers at most one cooperative yield. +- Record precise attachment refusal stages and native errors. Skip already-correct + native attribute writes; reconcile error 87 only for verified changed style + postconditions, preserving identity/readback checks and other refusals. +- Apply sibling/descendant clipping to admitted child attachments and restore + their exact original styles on retirement. + +Standalone Debug/Release static/shared/module builds and CPU contracts pass. +Native attachment, desktop responsiveness and exact-package acceptance remain +separate; no release or runtime success is claimed from these checks. + +## 0.1.1 ## DPI contract diff --git a/modules/perf.tier.ixx b/modules/perf.tier.ixx index ee2fc90..00f9234 100644 --- a/modules/perf.tier.ixx +++ b/modules/perf.tier.ixx @@ -32,6 +32,7 @@ module; #include #include #include +#include export module perf.tier; @@ -74,7 +75,7 @@ export namespace epochengine::perf struct frame_pacing_policy final { frame_pacing_mode mode = frame_pacing_mode::target_hz; - double target_hz = 120.0; + double target_hz = 30.0; double background_hz = 30.0; double minimized_hz = 10.0; }; @@ -211,7 +212,7 @@ export namespace epochengine::perf double requested, double fallback) noexcept { - if (!(requested > 0.0) || requested > 1000.0) + if (!(requested > 0.0) || requested > (std::numeric_limits::max)()) return fallback; return requested; } @@ -231,7 +232,7 @@ export namespace epochengine::perf } return { frame_pacing_mode::target_hz, - sanitize_frame_hz(target_hz, 120.0), + sanitize_frame_hz(target_hz, 30.0), sanitize_frame_hz(background_hz, 30.0), sanitize_frame_hz(minimized_hz, 10.0)}; } @@ -243,7 +244,7 @@ export namespace epochengine::perf { policy.target_hz = policy.mode == frame_pacing_mode::uncapped ? 0.0 - : sanitize_frame_hz(policy.target_hz, 120.0); + : sanitize_frame_hz(policy.target_hz, 30.0); policy.background_hz = sanitize_frame_hz(policy.background_hz, 30.0); policy.minimized_hz = sanitize_frame_hz(policy.minimized_hz, 10.0); @@ -355,12 +356,12 @@ export namespace epochengine::perf frame_pacing_mode explicit_mode, double explicit_target_hz, bool explicit_selection, - bool standalone_project) noexcept + [[maybe_unused]] bool standalone_project) noexcept { if (!explicit_selection) - // Keep the Editor's advertised 120 Hz default and standalone - // projects' 60 Hz default consistent on every backend. - return target_hz_policy(standalone_project ? 60.0 : 120.0); + // Start lightly on every platform. Higher rates, VSync and true + // unlimited mode require an explicit saved/UI/launch selection. + return target_hz_policy(30.0); if (explicit_mode == frame_pacing_mode::uncapped) return target_hz_policy(0.0); if (explicit_mode == frame_pacing_mode::vsync) @@ -372,7 +373,7 @@ export namespace epochengine::perf 10.0}; } // An invalid numeric target is not permission to become uncapped. - return target_hz_policy(sanitize_frame_hz(explicit_target_hz, 120.0)); + return target_hz_policy(sanitize_frame_hz(explicit_target_hz, 30.0)); } struct frame_pacing_selection final { @@ -392,7 +393,7 @@ export namespace epochengine::perf const double value = mode == frame_pacing_mode::uncapped ? 0.0 : mode == frame_pacing_mode::vsync ? -sanitize_frame_hz(hz, 60.0) - : sanitize_frame_hz(hz, 120.0); + : sanitize_frame_hz(hz, 30.0); selected_hz_.store(value, std::memory_order_release); } @@ -642,24 +643,40 @@ namespace epochengine::perf void frame_limiter::wait_for_next_frame() noexcept { if (target_dt <= 0.0) + { + // Uncapped removes the deadline, not the OS scheduler's opportunity + // to run another ready thread. Never impose a fixed minimum sleep. + std::this_thread::yield(); return; + } const double now0 = epochengine::core::time::now_seconds(); next_time = next_frame_deadline( next_time, now0, target_dt, started); started = true; + bool yielded = false; for (;;) { const double now = epochengine::core::time::now_seconds(); const double remaining = next_time - now; if (remaining <= 0.0) + { + // A fine wait already offered this turn's cooperative pause. + if (!yielded) + std::this_thread::yield(); break; + } const double ms = remaining * 1000.0; if (ms > 2.0) epochengine::core::time::sleep_ms(static_cast(ms - 1.0)); + else if (!yielded) + { + std::this_thread::yield(); + yielded = true; + } } } } diff --git a/src/platform/platform.window_attachment.cpp b/src/platform/platform.window_attachment.cpp index 85c6531..20ee10a 100644 --- a/src/platform/platform.window_attachment.cpp +++ b/src/platform/platform.window_attachment.cpp @@ -2,6 +2,7 @@ module; #include "core.stl_types.hpp" #include +#include #if defined(_WIN32) # ifndef WIN32_LEAN_AND_MEAN # define WIN32_LEAN_AND_MEAN @@ -27,6 +28,44 @@ namespace | WS_EX_TOPMOST | WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE | WS_EX_DLGMODALFRAME; std::atomic g_externalAttachmentCookie{}; + [[nodiscard]] bool attachment_failure(std::string_view stage, DWORD nativeError, + HWND child, HWND host, DWORD processId, int x = 0, int y = 0, + int width = 0, int height = 0, RECT client = {}, RECT outer = {}, + std::string_view attribute = {}, bool reconciled = false) noexcept + { + static thread_local unsigned records{}; + if (records >= 32u) return false; + ++records; + try + { + const std::string message = (reconciled ? "Attachment attribute reconciled stage=" + : "Attachment failure stage=") + std::string{stage} + + " native_error=" + std::to_string(nativeError) + + " child=" + std::to_string(reinterpret_cast(child)) + + " host=" + std::to_string(reinterpret_cast(host)) + + " pid=" + std::to_string(processId) + + " slot=" + std::to_string(x) + "," + std::to_string(y) + + "," + std::to_string(width) + "," + std::to_string(height) + + " client=" + std::to_string(client.right - client.left) + + "," + std::to_string(client.bottom - client.top) + + " outer=" + std::to_string(outer.left) + "," + std::to_string(outer.top) + + "," + std::to_string(outer.right) + "," + std::to_string(outer.bottom) + + (attribute.empty() ? std::string{} : " " + std::string{attribute}); + core::log::warn("Context.Multiplexer.Win", + epochengine::string_view{message.data(), message.size()}); + } + catch (...) {} // Diagnostic allocation must not change a native refusal. + return false; + } + + [[nodiscard]] std::string attribute_hex(LONG_PTR value) + { + char bytes[2u + 2u * sizeof(std::uintptr_t)]{'0', 'x'}; + const auto converted = std::to_chars(bytes + 2u, bytes + sizeof(bytes), + static_cast(value), 16); + return std::string{bytes, converted.ptr}; + } + [[nodiscard]] std::uintptr_t next_external_attachment_cookie() noexcept { auto current = g_externalAttachmentCookie.load(std::memory_order_relaxed); @@ -105,13 +144,67 @@ namespace == reinterpret_cast(cookie)); } - [[nodiscard]] bool write_long(int index, LONG_PTR value) const noexcept + [[nodiscard]] bool write_long(int index, LONG_PTR value, + std::string_view stage = "restore_attribute") const noexcept { - if (!same_window()) return false; + if (!same_window()) return attachment_failure(stage, ERROR_INVALID_WINDOW_HANDLE, + hwnd, parent, processId, 0, 0, 0, 0, {}, {}, "failure=identity_before_write"); + ::SetLastError(ERROR_SUCCESS); + const LONG_PTR before = ::GetWindowLongPtrW(hwnd, index); + const DWORD beforeError = ::GetLastError(); + const bool beforeRead = before != 0 || beforeError == ERROR_SUCCESS; + if (!same_window()) return attachment_failure(stage, ERROR_INVALID_WINDOW_HANDLE, + hwnd, parent, processId, 0, 0, 0, 0, {}, {}, "failure=identity_after_pre_read"); + if (!beforeRead) return attachment_failure(stage, beforeError, + hwnd, parent, processId, 0, 0, 0, 0, {}, {}, "failure=pre_read"); + // Already-correct owner/style state needs no native mutation. In + // particular, restoring an absent top-level owner is a no-op. + if (beforeRead && before == value) return true; ::SetLastError(ERROR_SUCCESS); const LONG_PTR previous = ::SetWindowLongPtrW(hwnd, index, value); - return (previous != 0 || ::GetLastError() == ERROR_SUCCESS) - && same_window() && ::GetWindowLongPtrW(hwnd, index) == value; + // The previous value can legitimately be zero. Capture its error + // immediately: identity/readback APIs may change the thread error. + const DWORD setterError = ::GetLastError(); + const bool setterSucceeded = previous != 0 || setterError == ERROR_SUCCESS; + bool identity = same_window(); + LONG_PTR observed{}; + DWORD readError{}; + if (identity) + { + ::SetLastError(ERROR_SUCCESS); + observed = ::GetWindowLongPtrW(hwnd, index); + readError = ::GetLastError(); + } + identity = identity && same_window(); + const bool readSucceeded = identity && (observed != 0 || readError == ERROR_SUCCESS); + if (setterSucceeded && readSucceeded && observed == value) return true; + // A native style change can report error87 after applying its bits. + // Reconcile only that observed changed postcondition; access denial, + // other errors and unreadable/replaced windows remain refusals. + const bool reconciled = (index == GWL_STYLE || index == GWL_EXSTYLE) + && beforeRead && before != value && previous == 0 + && setterError == ERROR_INVALID_PARAMETER && readSucceeded && observed == value; + const char* failure = !setterSucceeded ? "setter" + : !identity ? "identity_after_write" : !readSucceeded ? "readback" : "readback_mismatch"; + const DWORD error = !setterSucceeded ? setterError + : !identity ? ERROR_INVALID_WINDOW_HANDLE : !readSucceeded ? readError : ERROR_INVALID_DATA; + try + { + const std::string detail = "failure=" + std::string{failure} + + " attribute_index=" + std::to_string(index) + + " before=" + attribute_hex(before) + + " before_error=" + std::to_string(beforeError) + + " previous=" + attribute_hex(previous) + + " requested=" + attribute_hex(value) + + " observed=" + attribute_hex(observed) + + " setter_error=" + std::to_string(setterError) + + " read_error=" + std::to_string(readError) + + " identity_after_write=" + std::to_string(identity); + (void)attachment_failure(stage, error, hwnd, parent, processId, + 0, 0, 0, 0, {}, {}, detail, reconciled); + return reconciled; + } + catch (...) { return false; } } [[nodiscard]] bool attached() const noexcept override @@ -128,13 +221,20 @@ namespace [[nodiscard]] bool place(int x, int y, int width, int height) noexcept override { slotVerified = false; - if (!attached() || ::IsHungAppWindow(hwnd) != FALSE - || x < 0 || y < 0 || width <= 0 || height <= 0) - return false; + RECT client{}, outer{}; + const auto refuse = [&](std::string_view stage, DWORD error = 0) + { return attachment_failure(stage, error, hwnd, parent, processId, + x, y, width, height, client, outer); }; + if (!attached()) return refuse("placement_identity"); + if (::IsHungAppWindow(hwnd) != FALSE) return refuse("placement_hung_window"); + if (x < 0 || y < 0 || width <= 0 || height <= 0) + return refuse("placement_invalid_slot"); RECT parentClient{}; - if (!::GetClientRect(parent, &parentClient) - || x > parentClient.right - width || y > parentClient.bottom - height) - return false; + ::SetLastError(ERROR_SUCCESS); + if (!::GetClientRect(parent, &parentClient)) + return refuse("placement_parent_client", ::GetLastError()); + if (x > parentClient.right - width || y > parentClient.bottom - height) + return refuse("placement_outside_parent"); RECT previous{}; POINT previousOrigin{}; const bool haveRectangle = ::GetWindowRect(hwnd, &previous) != FALSE; @@ -147,15 +247,22 @@ namespace && ::IsWindowVisible(hwnd) != FALSE; // Style changes are handled at attachment, not every slot refresh. // Repeating FRAMECHANGED needlessly invalidates compositor surfaces. - if ((!unchanged && !::SetWindowPos(hwnd, nullptr, x, y, width, height, - SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW)) - || !attached()) - return false; - RECT client{}, outer{}; + if (!unchanged) + { + ::SetLastError(ERROR_SUCCESS); + if (!::SetWindowPos(hwnd, nullptr, x, y, width, height, + SWP_NOZORDER | SWP_NOACTIVATE | SWP_SHOWWINDOW)) + return refuse("placement_set_window_pos", ::GetLastError()); + } + if (!attached()) return refuse("placement_identity_after_move"); POINT origin{}; - if (!::GetClientRect(hwnd, &client) || !::GetWindowRect(hwnd, &outer) - || !::ClientToScreen(parent, &origin)) - return false; + ::SetLastError(ERROR_SUCCESS); + if (!::GetClientRect(hwnd, &client)) + return refuse("placement_child_client", ::GetLastError()); + if (!::GetWindowRect(hwnd, &outer)) + return refuse("placement_outer_rectangle", ::GetLastError()); + if (!::ClientToScreen(parent, &origin)) + return refuse("placement_parent_origin", ::GetLastError()); // Child chrome is removed: both the client area and native bounds // must agree with the admitted slot, not merely be nonzero. slotVerified = client.right - client.left == width && client.bottom - client.top == height @@ -163,7 +270,7 @@ namespace && outer.right - outer.left == width && outer.bottom - outer.top == height && attached(); if (slotVerified) admittedSlot = { x, y, x + width, y + height }; - return slotVerified; + return slotVerified || refuse("placement_geometry_mismatch"); } [[nodiscard]] bool placement_current() const noexcept override @@ -183,54 +290,73 @@ namespace [[nodiscard]] bool attach(HWND candidate, HWND host, DWORD pid) noexcept { + const auto refuse = [&](std::string_view stage, DWORD error = 0) + { return attachment_failure(stage, error, candidate, host, pid); }; // Reject an already stalled child before attempting synchronous // cross-process parenting. This is a bounded health probe, not a // guarantee that a foreign window cannot hang later. - if (!candidate || ::IsHungAppWindow(candidate) != FALSE) - return false; + if (!candidate) return refuse("admission_missing_window"); + if (::IsHungAppWindow(candidate) != FALSE) + return refuse("admission_hung_window"); DWORD_PTR probe{}; + ::SetLastError(ERROR_SUCCESS); if (::SendMessageTimeoutW(candidate, WM_NULL, 0, 0, SMTO_ABORTIFHUNG | SMTO_BLOCK, 150u, &probe) == 0) - return false; + return refuse("admission_health_probe", ::GetLastError()); hwnd = candidate; parent = host; processId = pid; DWORD observedPid{}; threadId = ::GetWindowThreadProcessId(hwnd, &observedPid); if (threadId == 0 || observedPid != pid || pid == ::GetCurrentProcessId()) - return false; + return refuse("admission_thread_process_identity"); process = ::OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION | SYNCHRONIZE, FALSE, pid); - if (!same_window() || ::GetPropW(hwnd, kExternalAttachmentProp)) - return false; + if (!process) return refuse("admission_open_process", ::GetLastError()); + if (!same_window()) return refuse("admission_process_incarnation"); + if (::GetPropW(hwnd, kExternalAttachmentProp)) + return refuse("admission_existing_lease"); originalStyle = ::GetWindowLongPtrW(hwnd, GWL_STYLE); originalExStyle = ::GetWindowLongPtrW(hwnd, GWL_EXSTYLE); originalOwner = ::GetWindow(hwnd, GW_OWNER); // The child-process supervisor enumerates unowned top-level HWNDs. // Do not steal a child or dialog from another ownership hierarchy. - if ((originalStyle & WS_CHILD) != 0 || originalOwner != nullptr - || !::GetWindowRect(hwnd, &originalRect) - || !::GetWindowPlacement(hwnd, &originalPlacement)) - return false; + if ((originalStyle & WS_CHILD) != 0 || originalOwner != nullptr) + return refuse("admission_foreign_hierarchy"); + ::SetLastError(ERROR_SUCCESS); + if (!::GetWindowRect(hwnd, &originalRect)) + return refuse("admission_original_rectangle", ::GetLastError()); + if (!::GetWindowPlacement(hwnd, &originalPlacement)) + return refuse("admission_original_placement", ::GetLastError()); RECT hostClient{}; - if (!::GetClientRect(parent, &hostClient) - || hostClient.right <= 0 || hostClient.bottom <= 0 || !same_window()) - return false; + if (!::GetClientRect(parent, &hostClient)) + return refuse("admission_host_client", ::GetLastError()); + if (hostClient.right <= 0 || hostClient.bottom <= 0 || !same_window()) + return refuse("admission_host_geometry_or_identity"); cookie = next_external_attachment_cookie(); - if (cookie == 0 || !::SetPropW(hwnd, kExternalAttachmentProp, reinterpret_cast(cookie))) - return false; + if (cookie == 0) return refuse("admission_cookie_exhausted"); + ::SetLastError(ERROR_SUCCESS); + if (!::SetPropW(hwnd, kExternalAttachmentProp, reinterpret_cast(cookie))) + return refuse("admission_set_lease_property", ::GetLastError()); propertyOwned = true; - if (!same_window()) return false; + if (!same_window()) return refuse("admission_identity_after_property"); nativeChanged = true; - if (!write_long(GWL_STYLE, (originalStyle & ~kExternalFrameStyles) | WS_CHILD) - || !write_long(GWL_EXSTYLE, - (originalExStyle & ~kExternalFrameExStyles) | WS_EX_NOPARENTNOTIFY)) + // Match engine-created child contexts: neither sibling paints nor + // descendant paints may overwrite another hosted surface. Restore + // the exact saved top-level style when this attachment retires. + if (!write_long(GWL_STYLE, (originalStyle & ~kExternalFrameStyles) + | WS_CHILD | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, + "admission_child_style")) return false; - if (!same_window()) return false; + if (!write_long(GWL_EXSTYLE, + (originalExStyle & ~kExternalFrameExStyles) | WS_EX_NOPARENTNOTIFY, + "admission_child_extended_style")) + return false; + if (!same_window()) return refuse("admission_identity_before_parent"); ::SetLastError(ERROR_SUCCESS); const HWND previousParent = ::SetParent(hwnd, parent); - if ((!previousParent && ::GetLastError() != ERROR_SUCCESS) - || !attached()) - return false; + if (!previousParent && ::GetLastError() != ERROR_SUCCESS) + return refuse("admission_set_parent", ::GetLastError()); + if (!attached()) return refuse("admission_identity_after_parent"); return place(0, 0, hostClient.right, hostClient.bottom); } diff --git a/tests/perf.tier_contract.cpp b/tests/perf.tier_contract.cpp index b85a896..0e0e169 100644 --- a/tests/perf.tier_contract.cpp +++ b/tests/perf.tier_contract.cpp @@ -2,6 +2,7 @@ // A standalone dependency consumer. No Engine, renderer, GUI or model service. #include "core.stl_types.hpp" #include +#include import core.env; import core.log; import perf.tier; @@ -60,6 +61,14 @@ int main() VERIFY(core::env::set("EPOCH_TIER", "invalid")); VERIFY(tier_from_env() == tier::editor_120); + VERIFY(frame_pacing_policy{}.target_hz == 30.0); + for (const bool project : {false, true}) + { + const auto startup = select_frame_pacing_policy(frame_pacing_mode::uncapped, 0.0, false, project); + VERIFY(startup.mode == frame_pacing_mode::target_hz && startup.target_hz == 30.0); + const auto selected = select_frame_pacing_policy(frame_pacing_mode::target_hz, 120.0, true, project); + VERIFY(selected.target_hz == 120.0); + } const auto unlimited = target_hz_policy(0.0); const auto foreground = resolve_frame_pacing_with_capabilities( unlimited, frame_activity::foreground, {true, true}); @@ -87,6 +96,13 @@ int main() VERIFY(with_frame_pacing_override(target_hz_policy(120.0), selected).target_hz == 0.0); selected.clear(); VERIFY(!selected.snapshot().explicit_selection); + for(const double invalid : {-1.0, std::numeric_limits::quiet_NaN(), + std::numeric_limits::infinity()}) + { + selected.publish(frame_pacing_mode::target_hz, invalid); + VERIFY(selected.snapshot().target_hz == 30.0); + } + selected.clear(); frame_limiter limiter; limiter.set_plan(foreground); limiter.wait_for_next_frame(); // Actual uncapped path returns without a deadline wait. @@ -110,6 +126,15 @@ int main() VERIFY(parse_frame_limit_value(" 5000\t", manual) && manual == 5000.0); VERIFY(parse_frame_limit_value("0", manual) && manual == 0.0); VERIFY(parse_frame_limit_value("59.94", manual) && manual == 59.94); + for (const double high : {1500.0, 5000.0}) + { + selected.publish(frame_pacing_mode::target_hz, high); + const auto policy = with_frame_pacing_override(target_hz_policy(120.0), selected); + const auto plan = resolve_frame_pacing_with_capabilities(policy, + frame_activity::foreground, {true, false}); + VERIFY(policy.target_hz == high && plan.configured_hz == high + && plan.effective_hz == high && plan.cpu_deadline_wait); + } for (const auto invalid : {"", "-1", "nan", "inf", "1e999", "120garbage", "1 20"}) { const double before = manual;