Interoperability latency between GDI RECT and Direct2D D2D1_RECT_F under concurrent thread access
0 reputation · 15 Mar 2023, 10:06 UTC
0 reputation · 15 Mar 2023, 10:06 UTC
The goal is to understand whether the internal GDI batching subsystem introduces measurable latency when multiple threads simultaneously issue drawing calls that rely on the RECT structure’s exclusive‑bottom‑right convention, and how this latency interacts with Direct2D’s inclusive‑edge D2D1_RECT_F expectations in a mixed‑rendering scenario.
Constraints include the documented RECT semantics, the fact that GDI batching may be disabled on certain Windows Server editions, and the requirement that any conversion between RECT and D2D1_REFT_F must preserve pixel alignment. The uncertainty lies in the concurrency threshold at which the batching‑induced delay becomes observable and whether mitigating it requires API‑level changes, synchronization, or alternative rendering paths.
What concurrent thread count triggers noticeable GDI batching latency? Does disabling GDI batching alter the visual output when Direct2D draws the same rectangle? How can developers quantify this latency without affecting the correctness of the mixed GDI/Direct2D rendering pipeline?
32775 reputation · 15 Mar 2023, 12:23 UTC
There is no specific, documented thread count that triggers a "batching latency threshold" because GDI drawing calls are fundamentally serialized by the Device Context (HDC) lock. When multiple threads attempt to issue drawing calls to the same HDC, they are queued; the latency observed is a result of lock contention and kernel-mode transitions, not the internal batching of the RECT structure itself.
Confirmed Facts:
RECT uses an exclusive bottom-right convention (the pixel at right and bottom is not drawn). Direct2D D2D1_RECT_F uses floating-point coordinates that are inclusive.Likely Explanation: The perceived latency in mixed-mode rendering usually stems from the synchronization overhead of the GDI lock and the CPU-to-GPU transition when switching between GDI (CPU/Software) and Direct2D (GPU/Hardware) pipelines, rather than the batching subsystem of GDI.
To prevent one-pixel gaps or overlaps when converting a GDI RECT to a D2D1_RECT_F, you must account for the exclusive-to-inclusive shift. Assuming a 1:1 pixel ratio:
// Conversion to preserve pixel alignment
D2D1_RECT_F d2dRect;
d2dRect.left = (float)gdiRect.left;
d2dRect.top = (float)gdiRect.top;
d2dRect.right = (float)gdiRect.right - 1.0f;
d2dRect.bottom = (float)gdiRect.bottom - 1.0f;
Disabling GDI batching (as seen in some Windows Server configurations) does not alter the visual output of the rectangle. It increases the frequency of kernel-mode transitions, which may increase CPU overhead and overall latency, but the coordinate calculations remain identical.
Gdi32.dll versus D2D1.dll.Missing Diagnostic Detail: Are you using ID2D1DCRenderTarget for the mixed-mode rendering, or are you managing separate surfaces and compositing them manually?
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.