|  | 
|  | 1 | +# SPDX-License-Identifier: MIT | 
|  | 2 | +# Copyright (c) 2024 David Lechner <[email protected]> | 
|  | 3 | +import sys | 
|  | 4 | + | 
|  | 5 | +if sys.platform != "win32": | 
|  | 6 | +    raise OSError | 
|  | 7 | + | 
|  | 8 | +import ctypes | 
|  | 9 | +import enum | 
|  | 10 | +import uuid | 
|  | 11 | +from ctypes import wintypes | 
|  | 12 | +from typing import TYPE_CHECKING | 
|  | 13 | + | 
|  | 14 | +if TYPE_CHECKING: | 
|  | 15 | +    from ctypes import _FuncPointer  # pyright: ignore[reportPrivateUsage] | 
|  | 16 | + | 
|  | 17 | + | 
|  | 18 | +### | 
|  | 19 | +# https://github.com/pywinrt/pywinrt/blob/main/samples/screen_capture/iunknown.py | 
|  | 20 | +### | 
|  | 21 | + | 
|  | 22 | + | 
|  | 23 | +class GUID(ctypes.Structure): | 
|  | 24 | +    _fields_ = ( | 
|  | 25 | +        ("Data1", ctypes.c_ulong), | 
|  | 26 | +        ("Data2", ctypes.c_ushort), | 
|  | 27 | +        ("Data3", ctypes.c_ushort), | 
|  | 28 | +        ("Data4", ctypes.c_ubyte * 8), | 
|  | 29 | +    ) | 
|  | 30 | + | 
|  | 31 | + | 
|  | 32 | +class IUnknown(ctypes.c_void_p): | 
|  | 33 | +    QueryInterface = ctypes.WINFUNCTYPE( | 
|  | 34 | +        # _CData is incompatible with int | 
|  | 35 | +        int,  # type: ignore[arg-type] # pyright: ignore[reportArgumentType] | 
|  | 36 | +        ctypes.POINTER(GUID), | 
|  | 37 | +        ctypes.POINTER(wintypes.LPVOID), | 
|  | 38 | +    )(0, "QueryInterface") | 
|  | 39 | +    AddRef = ctypes.WINFUNCTYPE(wintypes.ULONG)(1, "AddRef") | 
|  | 40 | +    Release = ctypes.WINFUNCTYPE(wintypes.ULONG)(2, "Release") | 
|  | 41 | + | 
|  | 42 | +    def query_interface(self, iid: uuid.UUID | str) -> "IUnknown": | 
|  | 43 | +        if isinstance(iid, str): | 
|  | 44 | +            iid = uuid.UUID(iid) | 
|  | 45 | + | 
|  | 46 | +        ppv = wintypes.LPVOID() | 
|  | 47 | +        _iid = GUID.from_buffer_copy(iid.bytes_le) | 
|  | 48 | +        ret = self.QueryInterface(self, ctypes.byref(_iid), ctypes.byref(ppv)) | 
|  | 49 | + | 
|  | 50 | +        if ret: | 
|  | 51 | +            raise ctypes.WinError(ret) | 
|  | 52 | + | 
|  | 53 | +        return IUnknown(ppv.value) | 
|  | 54 | + | 
|  | 55 | +    def __del__(self): | 
|  | 56 | +        IUnknown.Release(self) | 
|  | 57 | + | 
|  | 58 | + | 
|  | 59 | +### | 
|  | 60 | +# https://github.com/pywinrt/pywinrt/blob/main/samples/screen_capture/d3d11.py | 
|  | 61 | +### | 
|  | 62 | + | 
|  | 63 | + | 
|  | 64 | +__all__ = [ | 
|  | 65 | +    "D3D11_CREATE_DEVICE_FLAG", | 
|  | 66 | +    "D3D_DRIVER_TYPE", | 
|  | 67 | +    "D3D_FEATURE_LEVEL", | 
|  | 68 | +    "D3D11CreateDevice", | 
|  | 69 | +] | 
|  | 70 | + | 
|  | 71 | +IN = 1 | 
|  | 72 | +OUT = 2 | 
|  | 73 | + | 
|  | 74 | +# https://learn.microsoft.com/en-us/windows/win32/api/d3dcommon/ne-d3dcommon-d3d_driver_type | 
|  | 75 | +# | 
|  | 76 | +# typedef enum D3D_DRIVER_TYPE { | 
|  | 77 | +#   D3D_DRIVER_TYPE_UNKNOWN = 0, | 
|  | 78 | +#   D3D_DRIVER_TYPE_HARDWARE, | 
|  | 79 | +#   D3D_DRIVER_TYPE_REFERENCE, | 
|  | 80 | +#   D3D_DRIVER_TYPE_NULL, | 
|  | 81 | +#   D3D_DRIVER_TYPE_SOFTWARE, | 
|  | 82 | +#   D3D_DRIVER_TYPE_WARP | 
|  | 83 | +# } ; | 
|  | 84 | + | 
|  | 85 | + | 
|  | 86 | +class D3D_DRIVER_TYPE(enum.IntEnum): | 
|  | 87 | +    UNKNOWN = 0 | 
|  | 88 | +    HARDWARE = 1 | 
|  | 89 | +    REFERENCE = 2 | 
|  | 90 | +    NULL = 3 | 
|  | 91 | +    SOFTWARE = 4 | 
|  | 92 | +    WARP = 5 | 
|  | 93 | + | 
|  | 94 | + | 
|  | 95 | +# https://learn.microsoft.com/en-us/windows/win32/api/d3d11/ne-d3d11-d3d11_create_device_flag | 
|  | 96 | +# | 
|  | 97 | +# typedef enum D3D11_CREATE_DEVICE_FLAG { | 
|  | 98 | +#   D3D11_CREATE_DEVICE_SINGLETHREADED = 0x1, | 
|  | 99 | +#   D3D11_CREATE_DEVICE_DEBUG = 0x2, | 
|  | 100 | +#   D3D11_CREATE_DEVICE_SWITCH_TO_REF = 0x4, | 
|  | 101 | +#   D3D11_CREATE_DEVICE_PREVENT_INTERNAL_THREADING_OPTIMIZATIONS = 0x8, | 
|  | 102 | +#   D3D11_CREATE_DEVICE_BGRA_SUPPORT = 0x20, | 
|  | 103 | +#   D3D11_CREATE_DEVICE_DEBUGGABLE = 0x40, | 
|  | 104 | +#   D3D11_CREATE_DEVICE_PREVENT_ALTERING_LAYER_SETTINGS_FROM_REGISTRY = 0x80, | 
|  | 105 | +#   D3D11_CREATE_DEVICE_DISABLE_GPU_TIMEOUT = 0x100, | 
|  | 106 | +#   D3D11_CREATE_DEVICE_VIDEO_SUPPORT = 0x800 | 
|  | 107 | +# } ; | 
|  | 108 | + | 
|  | 109 | + | 
|  | 110 | +class D3D11_CREATE_DEVICE_FLAG(enum.IntFlag): | 
|  | 111 | +    SINGLETHREADED = 0x1 | 
|  | 112 | +    DEBUG = 0x2 | 
|  | 113 | +    SWITCH_TO_REF = 0x4 | 
|  | 114 | +    PREVENT_INTERNAL_THREADING_OPTIMIZATIONS = 0x8 | 
|  | 115 | +    BGRA_SUPPORT = 0x20 | 
|  | 116 | +    DEBUGGABLE = 0x40 | 
|  | 117 | +    PREVENT_ALTERING_LAYER_SETTINGS_FROM_REGISTRY = 0x80 | 
|  | 118 | +    DISABLE_GPU_TIMEOUT = 0x100 | 
|  | 119 | +    VIDEO_SUPPORT = 0x800 | 
|  | 120 | + | 
|  | 121 | + | 
|  | 122 | +# https://learn.microsoft.com/en-us/windows/win32/api/d3dcommon/ne-d3dcommon-d3d_feature_level | 
|  | 123 | +# | 
|  | 124 | +# typedef enum D3D_FEATURE_LEVEL { | 
|  | 125 | +#   D3D_FEATURE_LEVEL_1_0_GENERIC, | 
|  | 126 | +#   D3D_FEATURE_LEVEL_1_0_CORE, | 
|  | 127 | +#   D3D_FEATURE_LEVEL_9_1, | 
|  | 128 | +#   D3D_FEATURE_LEVEL_9_2, | 
|  | 129 | +#   D3D_FEATURE_LEVEL_9_3, | 
|  | 130 | +#   D3D_FEATURE_LEVEL_10_0, | 
|  | 131 | +#   D3D_FEATURE_LEVEL_10_1, | 
|  | 132 | +#   D3D_FEATURE_LEVEL_11_0, | 
|  | 133 | +#   D3D_FEATURE_LEVEL_11_1, | 
|  | 134 | +#   D3D_FEATURE_LEVEL_12_0, | 
|  | 135 | +#   D3D_FEATURE_LEVEL_12_1, | 
|  | 136 | +#   D3D_FEATURE_LEVEL_12_2 | 
|  | 137 | +# } ; | 
|  | 138 | + | 
|  | 139 | + | 
|  | 140 | +class D3D_FEATURE_LEVEL(enum.IntEnum): | 
|  | 141 | +    LEVEL_1_0_GENERIC = 0x1000 | 
|  | 142 | +    LEVEL_1_0_CORE = 0x1001 | 
|  | 143 | +    LEVEL_9_1 = 0x9100 | 
|  | 144 | +    LEVEL_9_2 = 0x9200 | 
|  | 145 | +    LEVEL_9_3 = 0x9300 | 
|  | 146 | +    LEVEL_10_0 = 0xA000 | 
|  | 147 | +    LEVEL_10_1 = 0xA100 | 
|  | 148 | +    LEVEL_11_0 = 0xB000 | 
|  | 149 | +    LEVEL_11_1 = 0xB100 | 
|  | 150 | +    LEVEL_12_0 = 0xC000 | 
|  | 151 | +    LEVEL_12_1 = 0xC100 | 
|  | 152 | +    LEVEL_12_2 = 0xC200 | 
|  | 153 | + | 
|  | 154 | + | 
|  | 155 | +# not sure where this is officially defined or if the value would ever change | 
|  | 156 | + | 
|  | 157 | +D3D11_SDK_VERSION = 7 | 
|  | 158 | + | 
|  | 159 | +# https://learn.microsoft.com/en-us/windows/win32/api/d3d11/nf-d3d11-d3d11createdevice | 
|  | 160 | +# | 
|  | 161 | +# HRESULT D3D11CreateDevice( | 
|  | 162 | +#   [in, optional]  IDXGIAdapter            *pAdapter, | 
|  | 163 | +#                   D3D_DRIVER_TYPE         DriverType, | 
|  | 164 | +#                   HMODULE                 Software, | 
|  | 165 | +#                   UINT                    Flags, | 
|  | 166 | +#   [in, optional]  const D3D_FEATURE_LEVEL *pFeatureLevels, | 
|  | 167 | +#                   UINT                    FeatureLevels, | 
|  | 168 | +#                   UINT                    SDKVersion, | 
|  | 169 | +#   [out, optional] ID3D11Device            **ppDevice, | 
|  | 170 | +#   [out, optional] D3D_FEATURE_LEVEL       *pFeatureLevel, | 
|  | 171 | +#   [out, optional] ID3D11DeviceContext     **ppImmediateContext | 
|  | 172 | +# ); | 
|  | 173 | + | 
|  | 174 | + | 
|  | 175 | +def errcheck( | 
|  | 176 | +    result: int, | 
|  | 177 | +    _func: "_FuncPointer",  # Actually WinFunctionType but that's an internal class | 
|  | 178 | +    args: tuple[ | 
|  | 179 | +        IUnknown | None,  # IDXGIAdapter | 
|  | 180 | +        D3D_DRIVER_TYPE, | 
|  | 181 | +        wintypes.HMODULE | None, | 
|  | 182 | +        D3D11_CREATE_DEVICE_FLAG, | 
|  | 183 | +        D3D_FEATURE_LEVEL | None, | 
|  | 184 | +        int, | 
|  | 185 | +        int, | 
|  | 186 | +        IUnknown,  # ID3D11Device | 
|  | 187 | +        wintypes.UINT, | 
|  | 188 | +        IUnknown,  # ID3D11DeviceContext | 
|  | 189 | +    ], | 
|  | 190 | +): | 
|  | 191 | +    if result: | 
|  | 192 | +        raise ctypes.WinError(result) | 
|  | 193 | + | 
|  | 194 | +    return (args[7], D3D_FEATURE_LEVEL(args[8].value), args[9]) | 
|  | 195 | + | 
|  | 196 | + | 
|  | 197 | +D3D11CreateDevice = ctypes.WINFUNCTYPE( | 
|  | 198 | +    # _CData is incompatible with int | 
|  | 199 | +    int,  # type: ignore[arg-type] # pyright: ignore[reportArgumentType] | 
|  | 200 | +    wintypes.LPVOID, | 
|  | 201 | +    wintypes.UINT, | 
|  | 202 | +    wintypes.LPVOID, | 
|  | 203 | +    wintypes.UINT, | 
|  | 204 | +    ctypes.POINTER(wintypes.UINT), | 
|  | 205 | +    wintypes.UINT, | 
|  | 206 | +    wintypes.UINT, | 
|  | 207 | +    ctypes.POINTER(IUnknown), | 
|  | 208 | +    ctypes.POINTER(wintypes.UINT), | 
|  | 209 | +    ctypes.POINTER(IUnknown), | 
|  | 210 | +)( | 
|  | 211 | +    ("D3D11CreateDevice", ctypes.windll.d3d11), | 
|  | 212 | +    ( | 
|  | 213 | +        (IN, "pAdapter", None), | 
|  | 214 | +        (IN, "DriverType", D3D_DRIVER_TYPE.UNKNOWN), | 
|  | 215 | +        (IN, "Software", None), | 
|  | 216 | +        (IN, "Flags", 0), | 
|  | 217 | +        (IN, "pFeatureLevels", None), | 
|  | 218 | +        (IN, "FeatureLevels", 0), | 
|  | 219 | +        (IN, "SDKVersion", D3D11_SDK_VERSION), | 
|  | 220 | +        (OUT, "ppDevice"), | 
|  | 221 | +        (OUT, "pFeatureLevel"), | 
|  | 222 | +        (OUT, "ppImmediateContext"), | 
|  | 223 | +    ), | 
|  | 224 | +) | 
|  | 225 | +# _CData is incompatible with int | 
|  | 226 | +D3D11CreateDevice.errcheck = errcheck  # type: ignore[assignment] # pyright: ignore[reportAttributeAccessIssue] | 
0 commit comments