app.dhover×115 error×51all
// F01 — first pixel & init cost, Win32 (../../f01-first-pixel.md).
//
// Implements every requirement of ../../../features/f01-first-pixel.md on top
// of the scaffold (../scaffold/app.d):
//
//   * Presents exactly ONE software-drawn frame (the corner-anchored gradient
//     into a CreateDIBSection backbuffer, BitBlt inside WM_PAINT) and exits
//     cleanly after a short hold (WSI_AUTO_EXIT=1 -> ~200 ms SetTimer ->
//     DestroyWindow -> exit 0).
//   * Logs one `step name=<api-call>` per initialization call, init_start ->
//     first_pixel_presented. LoadCursorW is called TWICE (call=1 / call=2):
//     the scaffold found the first user32 call pays the one-time session
//     connection (~13 ms under Wine); the second call isolates the per-call
//     cost from that first-connection cost.
//   * WSI_WS_VISIBLE=1 creates the window with WS_VISIBLE instead of calling
//     ShowWindow, to prove for which styles the "WM_SIZE arrives during
//     CreateWindowExW" claim holds (the scaffold showed it does NOT for a
//     non-WS_VISIBLE window — the first WM_SIZE lands in ShowWindow instead).
//   * Every creation-cascade message (incl. WM_GETMINMAXINFO and
//     WM_WINDOWPOSCHANGING, which the scaffold left uninstrumented) is logged
//     so the two orderings can be diffed line by line.
//   * first_pixel_presented = the return of the first BitBlt inside WM_PAINT
//     (spec requirement 3); a `summary` event records the concept count.
//
// Only druntime's built-in core.sys.windows bindings — no third-party packages.
module 
(module) app
app
;
import
(package) core
core
.
(package) core.sys
sys
.
(package) core.sys.windows
windows
.
(module) core.sys.windows.windows

Windows API header module

Translated from MinGW API for MS-Windows 4.0

Source

core/sys/windows/windows.d

windows
;
import instrument;
unable to read module `instrument` Expected 'instrument.d' or 'instrument/package.d' in one of the following import paths:
unable to read module `instrument` Expected 'instrument.d' or 'instrument/package.d' in one of the following import paths:
struct
(struct) app.Demo
Demo
{ HDC
(field) _error_ app.Demo.memDc
memDc
; // memory DC the DIB section is selected into
undefined identifier `HDC`
HBITMAP
(field) _error_ app.Demo.dib
dib
; // top-down 32-bit DIB section (CPU-visible backbuffer)
undefined identifier `HBITMAP`
HBITMAP
(field) _error_ app.Demo.stockBmp
stockBmp
; // the 1x1 stock bitmap displaced by SelectObject
undefined identifier `HBITMAP`
uint*
(field) uint* app.Demo.pixels
pixels
; // DIB bits: 0x00RRGGBB, row-major, row 0 = top row
int
(field) int app.Demo.width
width
,
(field) int app.Demo.height
height
; // current client (= DIB) size, physical pixels
int
(field) int app.Demo.dpi
dpi
= 96; // monitor DPI; scale = dpi / 96
bool
(field) bool app.Demo.autoExit
autoExit
; // WSI_AUTO_EXIT=1: bounded run
bool
(field) bool app.Demo.wsVisible
wsVisible
; // WSI_WS_VISIBLE=1: create with WS_VISIBLE, skip ShowWindow
bool
(field) bool app.Demo.inCreate
inCreate
; // between the CreateWindowExW step and window_created
bool
(field) bool app.Demo.firstSizeSeen
firstSizeSeen
;
bool
(field) bool app.Demo.firstPaintDone
firstPaintDone
;
} __gshared
(struct) app.Demo
Demo
_error_ app.g
g
;
enum UINT_PTR
(constant) _error_ app.TIMER_ID = __error
TIMER_ID
= 1;
undefined identifier `UINT_PTR`
enum
(constant) int app.HOLD_MS = 200
HOLD_MS
= 200; // post-present hold before the bounded run exits
// Distinct platform object/handle types touched before first pixel (F01 // requirement 4): HINSTANCE, HCURSOR, WNDCLASSEXW, ATOM, HWND, HDC, // BITMAPINFO, HBITMAP, PAINTSTRUCT, MSG. enum
(constant) int app.CONCEPT_COUNT = 10
CONCEPT_COUNT
= 10;
// --------------------------------------------------------------------------- // Backbuffer + gradient (same shape as the scaffold; F01 draws it once). void
void app.createBackbuffer(int w, int h) nothrow
createBackbuffer
(int
(parameter) int w
w
, int
(parameter) int h
h
) nothrow
{ if (g.
(field) _error_ g.dib
dib
!is null)
{ SelectObject(g.
g.memDc
memDc
, g.
g.stockBmp
stockBmp
);
undefined identifier `SelectObject`
DeleteObject(g.
g.dib
dib
);
undefined identifier `DeleteObject`
g.
g.dib
dib
= null;
g.
g.pixels
pixels
= null;
g.
g.width
width
= g.
g.height
height
= 0;
} if (
(parameter) int w
w
<= 0 ||
(parameter) int h
h
<= 0)
return; BITMAPINFO
(local variable) _error_ bmi
bmi
;
undefined identifier `BITMAPINFO`
bmi.bmiHeader.biSize = BITMAPINFOHEADER.sizeof; bmi.bmiHeader.biWidth = w; bmi.bmiHeader.biHeight = -h; // negative height = top-down rows bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 32; bmi.bmiHeader.biCompression = BI_RGB; void*
(local variable) void* bits
bits
;
logEvent("step name=CreateDIBSection");
undefined identifier `logEvent`
g.
g.dib
dib
= CreateDIBSection(null, &bmi, DIB_RGB_COLORS, &bits, null, 0);
if (g.
(field) _error_ g.dib
dib
is null)
{ logEvent("error what=CreateDIBSection code=%lu", GetLastError());
undefined identifier `logEvent`
return; } g.
g.pixels
pixels
= cast(uint*) bits;
g.
g.width
width
= w;
g.
g.height
height
= h;
g.
g.stockBmp
stockBmp
= cast(HBITMAP) SelectObject(g.
g.memDc
memDc
, g.
g.dib
dib
);
logEvent("buffer_alloc size=%dx%d bytes=%d", w, h, w * h * 4);
undefined identifier `logEvent`
} void
void app.drawGradient() nothrow
drawGradient
() nothrow
{ if (g.
(field) _error_ g.pixels
pixels
is null)
return; const
(local variable) const(_error_) w
w
= g.
(field) _error_ g.width
width
,
(local variable) const(_error_) h
h
= g.
(field) _error_ g.height
height
;
foreach (
(parameter) y
y
; 0 .. h)
{ uint*
_error_ row
row
= g.
g.pixels
pixels
+ cast(size_t) y * w;
const
_error_ green
green
= h > 1 ? (y * 255) / (h - 1) : 0;
foreach (
(parameter) x
x
; 0 .. w)
{ const
_error_ red
red
= w > 1 ? (x * 255) / (w - 1) : 0;
row[x] = cast(uint)((red << 16) | (green << 8) | 0x40); } } } // --------------------------------------------------------------------------- // WndProc: every creation/show message is logged with an in_create= marker so // the WS_VISIBLE-vs-ShowWindow message orders can be compared line by line. const(char)*
const(char)* app.whereTag() nothrow @nogc
whereTag
() nothrow @nogc
{ return g.
(field) _error_ g.inCreate
inCreate
? "in_create=1" : "in_create=0";
} extern (Windows) LRESULT
app.wndProc
wndProc
(HWND
(parameter) HWND hwnd
hwnd
, UINT
(parameter) UINT msg
msg
, WPARAM wParam, LPARAM lParam) nothrow
undefined identifier `LRESULT`
undefined identifier `HWND`
undefined identifier `UINT`
undefined identifier `WPARAM`
undefined identifier `LPARAM`
{ switch (msg) { case WM_GETMINMAXINFO: // documented as part of the creation set logEvent("msg name=WM_GETMINMAXINFO %s", whereTag()); goto default; case WM_NCCREATE: logEvent("msg name=WM_NCCREATE %s", whereTag()); goto default; case WM_NCCALCSIZE: logEvent("msg name=WM_NCCALCSIZE %s", whereTag()); goto default; case WM_CREATE: logEvent("msg name=WM_CREATE %s", whereTag()); logEvent("step name=CreateCompatibleDC"); g.
g.memDc
memDc
= CreateCompatibleDC(null);
// druntime's core.sys.windows has no GetDpiForWindow (Win10 1607+); // the system DPI from the screen DC is enough for the scale= field. HDC
_error_ screen
screen
= GetDC(null);
g.
g.dpi
dpi
= GetDeviceCaps(screen, LOGPIXELSX);
ReleaseDC(null, screen); return 0; case WM_SHOWWINDOW: logEvent("msg name=WM_SHOWWINDOW shown=%d %s", cast(int) wParam, whereTag()); goto default; case WM_WINDOWPOSCHANGING: logEvent("msg name=WM_WINDOWPOSCHANGING %s", whereTag()); goto default; case WM_WINDOWPOSCHANGED: logEvent("msg name=WM_WINDOWPOSCHANGED %s", whereTag()); goto default; // DefWindowProcW synthesizes WM_SIZE/WM_MOVE from this case WM_MOVE: logEvent("msg name=WM_MOVE %s", whereTag()); return 0; case WM_ERASEBKGND: logEvent("msg name=WM_ERASEBKGND %s", whereTag()); return 1; // claim erased — WM_PAINT repaints the full client anyway case WM_SIZE: const
_error_ w
w
= cast(int)(lParam & 0xffff);
const
_error_ h
h
= cast(int)((lParam >> 16) & 0xffff);
if (!g.
g.firstSizeSeen
firstSizeSeen
)
{ g.
g.firstSizeSeen
firstSizeSeen
= true;
// F01's spec parenthetical claims this arrives during // CreateWindowExW; in_create= records for which mode that holds. logEvent("first_configure size=%dx%d %s", w, h, whereTag()); } logEvent("resize size=%dx%d scale=%d.%02d %s", w, h, g.
g.dpi
dpi
/ 96, (g.
g.dpi
dpi
% 96) * 100 / 96, whereTag());
if (wParam == SIZE_MINIMIZED) return 0; if (w != g.
g.width
width
|| h != g.
g.height
height
)
createBackbuffer(w, h); return 0; case WM_PAINT: logEvent("msg name=WM_PAINT %s", whereTag()); PAINTSTRUCT
_error_ ps
ps
;
logEvent("step name=BeginPaint"); HDC
_error_ hdc
hdc
= BeginPaint(hwnd, &ps);
if (!g.
g.firstPaintDone
firstPaintDone
)
{ drawGradient(); // F01: a single software frame logEvent("step name=BitBlt size=%dx%d", g.
g.width
width
, g.
g.height
height
);
if (g.
g.pixels
pixels
!is null)
BitBlt(hdc, 0, 0, g.
g.width
width
, g.
g.height
height
, g.
g.memDc
memDc
, 0, 0, SRCCOPY);
g.
g.firstPaintDone
firstPaintDone
= true;
// F01: "presented" on Win32 = the first BitBlt returned. logEvent("first_pixel_presented size=%dx%d", g.
g.width
width
, g.
g.height
height
);
logEvent("summary concepts=%d loc_file=app.d round_trips_observed=0", CONCEPT_COUNT); if (g.
g.autoExit
autoExit
)
{ logEvent("step name=SetTimer hold_ms=%d", HOLD_MS); SetTimer(hwnd, TIMER_ID, HOLD_MS, null); } } else if (g.
g.pixels
pixels
!is null)
BitBlt(hdc, 0, 0, g.
g.width
width
, g.
g.height
height
, g.
g.memDc
memDc
, 0, 0, SRCCOPY);
EndPaint(hwnd, &ps); return 0; case WM_TIMER: if (wParam != TIMER_ID) return 0; logEvent("hold_elapsed ms=%d", HOLD_MS); DestroyWindow(hwnd); return 0; case WM_CLOSE: logEvent("close_requested"); goto default; // DefWindowProcW responds with DestroyWindow case WM_DESTROY: logEvent("msg name=WM_DESTROY"); KillTimer(hwnd, TIMER_ID); createBackbuffer(0, 0); // frees the DIB section if (g.
g.memDc
memDc
!is null)
{ DeleteDC(g.
g.memDc
memDc
);
g.
g.memDc
memDc
= null;
} PostQuitMessage(0); return 0; case WM_NCDESTROY: logEvent("msg name=WM_NCDESTROY"); goto default; default: return DefWindowProcW(hwnd, msg, wParam, lParam); } } // --------------------------------------------------------------------------- bool
bool app.envFlag(const(wchar)* name) nothrow
envFlag
(const(wchar)*
(parameter) const(wchar)* name
name
) nothrow
{ WCHAR[8]
(local variable) _error_ buf
buf
;
undefined identifier `WCHAR`
const
(local variable) const(_error_) n
n
= GetEnvironmentVariableW(name, buf.ptr, buf.length);
undefined identifier `GetEnvironmentVariableW`
return n >= 1 && n < buf.length && buf[0] == '1'; } int
int D main()
main
()
{ instrumentInit("f01_first_pixel_win32");
undefined identifier `instrumentInit`
logEvent("init_start");
undefined identifier `logEvent`
g.
g.autoExit
autoExit
= envFlag("WSI_AUTO_EXIT"w.ptr);
g.
g.wsVisible
wsVisible
= envFlag("WSI_WS_VISIBLE"w.ptr);
logEvent("mode auto_exit=%d ws_visible=%d", g.
g.autoExit
autoExit
? 1 : 0, g.
g.wsVisible
wsVisible
? 1 : 0);
undefined identifier `logEvent`
logEvent("step name=GetModuleHandleW");
undefined identifier `logEvent`
HINSTANCE
(local variable) _error_ hInst
hInst
= GetModuleHandleW(null);
undefined identifier `HINSTANCE`
undefined identifier `GetModuleHandleW`
// First user32 call of the process: under Wine this is where user32 // connects to the wine server and loads the display driver. The second, // identical call right after isolates the steady-state per-call cost from // that one-time first-connection cost. logEvent("step name=LoadCursorW call=1");
undefined identifier `logEvent`
HCURSOR
(local variable) _error_ arrow
arrow
= LoadCursorW(null, IDC_ARROW);
undefined identifier `HCURSOR`
undefined identifier `LoadCursorW`
logEvent("step name=LoadCursorW call=2");
undefined identifier `logEvent`
arrow = LoadCursorW(null, IDC_ARROW); auto
(local variable) wstring clsName
clsName
= "wsi-f01-class"w;
WNDCLASSEXW
(local variable) _error_ wc
wc
;
undefined identifier `WNDCLASSEXW`
wc.cbSize = WNDCLASSEXW.sizeof; wc.lpfnWndProc = &wndProc; wc.hInstance = hInst; wc.lpszClassName = clsName.ptr; wc.hCursor = arrow; // No hbrBackground: WM_ERASEBKGND is handled, the DIB covers every pixel. logEvent("step name=RegisterClassExW");
undefined identifier `logEvent`
if (!RegisterClassExW(&wc))
undefined identifier `RegisterClassExW`
{ logEvent("error what=RegisterClassExW code=%lu", GetLastError());
undefined identifier `logEvent`
return 1; } const DWORD
(local variable) const(_error_) style
style
= WS_OVERLAPPEDWINDOW | (g.
(field) _error_ g.wsVisible
wsVisible
? WS_VISIBLE : 0);
undefined identifier `DWORD`
undefined identifier `WS_OVERLAPPEDWINDOW`
undefined identifier `WS_VISIBLE`
logEvent("step name=CreateWindowExW ws_visible=%d", g.
g.wsVisible
wsVisible
? 1 : 0);
undefined identifier `logEvent`
g.
g.inCreate
inCreate
= true;
HWND
(local variable) _error_ hwnd
hwnd
= CreateWindowExW(0, clsName.ptr, "wsi-f01-first-pixel"w.ptr,
undefined identifier `HWND`
undefined identifier `CreateWindowExW`
style, CW_USEDEFAULT, CW_USEDEFAULT, 480, 320, null, null, hInst, null); g.
g.inCreate
inCreate
= false;
if (hwnd is null) { logEvent("error what=CreateWindowExW code=%lu", GetLastError());
undefined identifier `logEvent`
return 1; } logEvent("window_created");
undefined identifier `logEvent`
if (g.
(field) _error_ g.wsVisible
wsVisible
)
logEvent("step name=ShowWindow skipped=1 reason=ws_visible");
undefined identifier `logEvent`
else { logEvent("step name=ShowWindow");
undefined identifier `logEvent`
ShowWindow(hwnd, SW_SHOW);
undefined identifier `ShowWindow`
} logEvent("step name=UpdateWindow");
undefined identifier `logEvent`
UpdateWindow(hwnd); // forces the first WM_PAINT synchronously, now
undefined identifier `UpdateWindow`
MSG
(local variable) _error_ msg
msg
;
undefined identifier `MSG`
while (GetMessageW(&msg, null, 0, 0) > 0)
undefined identifier `GetMessageW`
{ TranslateMessage(&msg);
undefined identifier `TranslateMessage`
DispatchMessageW(&msg);
undefined identifier `DispatchMessageW`
} logEvent("exit code=%d", cast(int) msg.wParam);
undefined identifier `logEvent`
return cast(int) msg.wParam; }