├── dxcam ├── _libs │ ├── __init__.py │ ├── user32.py │ ├── dxgi.py │ └── d3d11.py ├── util │ ├── __init__.py │ ├── timer.py │ └── io.py ├── processor │ ├── __init__.py │ ├── base.py │ └── numpy_processor.py ├── core │ ├── __init__.py │ ├── output.py │ ├── device.py │ ├── stagesurf.py │ └── duplicator.py ├── __init__.py └── dxcam.py ├── .github └── FUNDING.yml ├── pyproject.toml ├── CHANGELOG.md ├── benchmarks ├── dxcam_capture.py ├── mss_max_fps.py ├── dxcam_max_fps.py └── d3dshot_max_fps.py ├── examples ├── capture_to_video.py └── instant_replay.py ├── LICENSE ├── setup.cfg ├── .gitignore └── README.md /dxcam/_libs/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /dxcam/util/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /dxcam/processor/__init__.py: -------------------------------------------------------------------------------- 1 | from .base import Processor 2 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: [ra1nty] 4 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [build-system] 2 | requires = ["setuptools>=42"] 3 | build-backend = "setuptools.build_meta" -------------------------------------------------------------------------------- /dxcam/core/__init__.py: -------------------------------------------------------------------------------- 1 | __all__ = ["Device", "Output", "StageSurface", "Duplicator"] 2 | 3 | 4 | from dxcam.core.device import Device 5 | from dxcam.core.output import Output 6 | from dxcam.core.stagesurf import StageSurface 7 | from dxcam.core.duplicator import Duplicator 8 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ### 0.0.5 2 | - Fixed black screen for rotated display 3 | - Added delay on start to prevent black screenshot 4 | - Fixed capture mode for color = "GRAY" 5 | ### 0.0.2 6 | - Refactoring 7 | - Screen capturing w/ target FPS use CREATE_WAITABLE_TIMER_HIGH_RESOLUTION 8 | ### 0.0.1 9 | - Initial commit 10 | - Basic features: screenshot 11 | -------------------------------------------------------------------------------- /benchmarks/dxcam_capture.py: -------------------------------------------------------------------------------- 1 | import time 2 | import dxcam 3 | 4 | 5 | TOP = 0 6 | LEFT = 0 7 | RIGHT = 1920 8 | BOTTOM = 1080 9 | region = (LEFT, TOP, RIGHT, BOTTOM) 10 | title = "[DXcam] Capture benchmark" 11 | 12 | fps = 0 13 | camera = dxcam.create(output_idx=0) 14 | camera.start(target_fps=60) 15 | for i in range(1000): 16 | image = camera.get_latest_frame() 17 | camera.stop() 18 | del camera 19 | -------------------------------------------------------------------------------- /benchmarks/mss_max_fps.py: -------------------------------------------------------------------------------- 1 | import time 2 | import mss 3 | 4 | 5 | TOP = 0 6 | LEFT = 0 7 | RIGHT = 1920 8 | BOTTOM = 1080 9 | region = (LEFT, TOP, RIGHT, BOTTOM) 10 | title = "[MSS] FPS benchmark" 11 | start_time = time.perf_counter() 12 | 13 | 14 | fps = 0 15 | sct = mss.mss() 16 | start = time.perf_counter() 17 | while fps < 1000: 18 | frame = sct.grab(region) 19 | if frame is not None: 20 | fps += 1 21 | 22 | 23 | end_time = time.perf_counter() - start_time 24 | 25 | print(f"{title}: {fps/end_time}") 26 | -------------------------------------------------------------------------------- /benchmarks/dxcam_max_fps.py: -------------------------------------------------------------------------------- 1 | import time 2 | import dxcam 3 | 4 | 5 | TOP = 0 6 | LEFT = 0 7 | RIGHT = 1920 8 | BOTTOM = 1080 9 | region = (LEFT, TOP, RIGHT, BOTTOM) 10 | title = "[DXcam] FPS benchmark" 11 | start_time = time.perf_counter() 12 | 13 | 14 | fps = 0 15 | cam = dxcam.create() 16 | start = time.perf_counter() 17 | while fps < 1000: 18 | frame = cam.grab(region=region) 19 | if frame is not None: 20 | fps += 1 21 | 22 | end_time = time.perf_counter() - start_time 23 | 24 | print(f"{title}: {fps/end_time}") 25 | del cam 26 | -------------------------------------------------------------------------------- /benchmarks/d3dshot_max_fps.py: -------------------------------------------------------------------------------- 1 | import time 2 | import d3dshot 3 | 4 | 5 | TOP = 0 6 | LEFT = 0 7 | RIGHT = 1920 8 | BOTTOM = 1080 9 | region = (LEFT, TOP, RIGHT, BOTTOM) 10 | title = "[D3DShot] FPS benchmark" 11 | start_time = time.perf_counter() 12 | 13 | fps = 0 14 | 15 | 16 | sct = d3dshot.create(capture_output="numpy") 17 | 18 | 19 | start = time.perf_counter() 20 | while fps < 1000: 21 | frame = sct.screenshot(region) 22 | if frame is not None: 23 | fps += 1 24 | 25 | 26 | end_time = time.perf_counter() - start_time 27 | 28 | print(f"{title}: {fps/end_time}") 29 | sct.stop() 30 | -------------------------------------------------------------------------------- /examples/capture_to_video.py: -------------------------------------------------------------------------------- 1 | import dxcam 2 | 3 | # install OpenCV using `pip install dxcam[cv2]` command. 4 | import cv2 5 | 6 | TOP = 0 7 | LEFT = 0 8 | RIGHT = 1920 9 | BOTTOM = 1080 10 | region = (LEFT, TOP, RIGHT, BOTTOM) 11 | title = "[DXcam] Capture benchmark" 12 | 13 | target_fps = 30 14 | camera = dxcam.create(output_idx=0, output_color="BGR") 15 | camera.start(target_fps=target_fps, video_mode=True) 16 | writer = cv2.VideoWriter( 17 | "video.mp4", cv2.VideoWriter_fourcc(*"mp4v"), target_fps, (1920, 1080) 18 | ) 19 | for i in range(600): 20 | writer.write(camera.get_latest_frame()) 21 | camera.stop() 22 | writer.release() 23 | -------------------------------------------------------------------------------- /dxcam/_libs/user32.py: -------------------------------------------------------------------------------- 1 | import ctypes 2 | import ctypes.wintypes as wintypes 3 | 4 | 5 | MONITORINFOF_PRIMARY = 0x00000001 6 | DISPLAY_DEVICE_ACTIVE = 1 7 | DISPLAY_DEVICE_PRIMARY_DEVICE = 4 8 | 9 | 10 | class DISPLAY_DEVICE(ctypes.Structure): 11 | _fields_ = [ 12 | ("cb", wintypes.DWORD), 13 | ("DeviceName", wintypes.WCHAR * 32), 14 | ("DeviceString", wintypes.WCHAR * 128), 15 | ("StateFlags", wintypes.DWORD), 16 | ("DeviceID", wintypes.WCHAR * 128), 17 | ("DeviceKey", wintypes.WCHAR * 128), 18 | ] 19 | 20 | 21 | class MONITORINFOEXW(ctypes.Structure): 22 | _fields_ = [ 23 | ("cbSize", wintypes.DWORD), 24 | ("rcMonitor", wintypes.RECT), 25 | ("rcWork", wintypes.RECT), 26 | ("dwFlags", wintypes.DWORD), 27 | ("szDevice", wintypes.WCHAR * 32), 28 | ] 29 | -------------------------------------------------------------------------------- /dxcam/processor/base.py: -------------------------------------------------------------------------------- 1 | import enum 2 | 3 | 4 | class ProcessorBackends(enum.Enum): 5 | PIL = 0 6 | NUMPY = 1 7 | 8 | 9 | class Processor: 10 | def __init__(self, backend=ProcessorBackends.NUMPY, output_color: str = "RGB"): 11 | self.color_mode = output_color 12 | self.backend = self._initialize_backend(backend) 13 | 14 | def process(self, rect, width, height, region, rotation_angle): 15 | return self.backend.process(rect, width, height, region, rotation_angle) 16 | 17 | def process2(self, image_ptr, rect, width, height): 18 | self.backend.shot(image_ptr, rect, width, height) 19 | 20 | def _initialize_backend(self, backend): 21 | if backend == ProcessorBackends.NUMPY: 22 | from dxcam.processor.numpy_processor import NumpyProcessor 23 | 24 | return NumpyProcessor(self.color_mode) 25 | -------------------------------------------------------------------------------- /dxcam/util/timer.py: -------------------------------------------------------------------------------- 1 | import ctypes 2 | from ctypes.wintypes import LARGE_INTEGER 3 | 4 | 5 | INFINITE = 0xFFFFFFFF 6 | WAIT_FAILED = 0xFFFFFFFF 7 | CREATE_WAITABLE_TIMER_HIGH_RESOLUTION = 0x00000002 8 | TIMER_MODIFY_STATE = 0x0002 9 | TIMER_ALL_ACCESS = 0x1F0003 10 | 11 | 12 | __kernel32 = ctypes.windll.kernel32 13 | 14 | 15 | def create_high_resolution_timer(): 16 | handle = __kernel32.CreateWaitableTimerExW( 17 | None, None, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS 18 | ) 19 | if handle == 0: 20 | raise ctypes.WinError() 21 | return handle 22 | 23 | 24 | def set_periodic_timer(handle, period: int): 25 | res = __kernel32.SetWaitableTimer( 26 | handle, 27 | ctypes.byref(LARGE_INTEGER(0)), 28 | period, 29 | None, 30 | None, 31 | 0, 32 | ) 33 | if res == 0: 34 | raise ctypes.WinError() 35 | return True 36 | 37 | 38 | wait_for_timer = __kernel32.WaitForSingleObject 39 | cancel_timer = __kernel32.CancelWaitableTimer 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Rain 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | name = dxcam 3 | version = 0.0.5 4 | 5 | author = ra1nty 6 | description = A Python high-performance screenshot library for Windows use Desktop Duplication API 7 | long_description = file: README.md 8 | long_description_content_type = text/markdown 9 | url = https://github.com/ra1nty/DXcam 10 | project_urls = 11 | Source = https://github.com/ra1nty/DXcam 12 | Tracker = https://github.com/ra1nty/DXcam/issues 13 | keywords = screen, screenshot, screencapture, screengrab, windows 14 | license = MIT 15 | license_files = 16 | LICENSE 17 | classifiers = 18 | Development Status :: 2 - Pre-Alpha 19 | Intended Audience :: Developers 20 | Programming Language :: Python :: 3 21 | Programming Language :: Python :: 3 :: Only 22 | License :: OSI Approved :: MIT License 23 | Operating System :: Microsoft :: Windows 24 | Operating System :: Microsoft :: Windows :: Windows 8.1 25 | Operating System :: Microsoft :: Windows :: Windows 10 26 | Topic :: Multimedia :: Graphics 27 | Topic :: Multimedia :: Graphics :: Capture 28 | Topic :: Multimedia :: Graphics :: Capture :: Screen Capture 29 | 30 | [options] 31 | packages = find: 32 | python_requires = >=3.7 33 | install_requires = 34 | numpy 35 | comtypes 36 | [options.extras_require] 37 | cv2 = opencv-python 38 | [options.packages.find] 39 | include = dxcam* -------------------------------------------------------------------------------- /dxcam/core/output.py: -------------------------------------------------------------------------------- 1 | import ctypes 2 | from typing import Tuple 3 | from dataclasses import dataclass 4 | from dxcam._libs.d3d11 import * 5 | from dxcam._libs.dxgi import * 6 | 7 | 8 | @dataclass 9 | class Output: 10 | output: ctypes.POINTER(IDXGIOutput1) 11 | rotation_mapping: tuple = (0, 0, 90, 180, 270) 12 | desc: DXGI_OUTPUT_DESC = None 13 | 14 | def __post_init__(self): 15 | ctypes.windll.shcore.SetProcessDpiAwareness(2) 16 | self.desc = DXGI_OUTPUT_DESC() 17 | self.update_desc() 18 | 19 | def update_desc(self): 20 | if self.desc is None: 21 | self.desc = DXGI_OUTPUT_DESC() 22 | self.output.GetDesc(ctypes.byref(self.desc)) 23 | 24 | @property 25 | def hmonitor(self) -> wintypes.HMONITOR: 26 | return self.desc.Monitor 27 | 28 | @property 29 | def devicename(self) -> str: 30 | return self.desc.DeviceName 31 | 32 | @property 33 | def resolution(self) -> Tuple[int, int]: 34 | return ( 35 | (self.desc.DesktopCoordinates.right - self.desc.DesktopCoordinates.left), 36 | (self.desc.DesktopCoordinates.bottom - self.desc.DesktopCoordinates.top), 37 | ) 38 | 39 | @property 40 | def surface_size(self) -> Tuple[int, int]: 41 | if self.rotation_angle in (90, 270): 42 | return self.resolution[1], self.resolution[0] 43 | else: 44 | return self.resolution 45 | 46 | @property 47 | def attached_to_desktop(self) -> bool: 48 | return bool(self.desc.AttachedToDesktop) 49 | 50 | @property 51 | def rotation_angle(self) -> int: 52 | return self.rotation_mapping[self.desc.Rotation] 53 | 54 | def __repr__(self) -> str: 55 | return "<{} Name:{} Resolution:{} Rotation:{}>".format( 56 | self.__class__.__name__, 57 | self.devicename, 58 | self.resolution, 59 | self.rotation_angle, 60 | ) 61 | -------------------------------------------------------------------------------- /examples/instant_replay.py: -------------------------------------------------------------------------------- 1 | """A Minimal example of creating a ghetto instant replay with hotkeys. 2 | Took less than 5 minutes to write using pyav + dxcam + pynput. 3 | The code is shit but you got the idea. 4 | """ 5 | from collections import deque 6 | from threading import Event, Lock 7 | import dxcam 8 | import av 9 | from pynput import keyboard 10 | 11 | 12 | stop_event = Event() 13 | buffer_lock = Lock() 14 | replay_count = 0 15 | target_fps = 120 16 | buffer = deque(maxlen=target_fps * 10) 17 | 18 | container = av.open(f"replay{replay_count}.mp4", mode="w") 19 | stream = container.add_stream("mpeg4", rate=target_fps) 20 | stream.pix_fmt, stream.height, stream.width = "yuv420p", 1080, 1920 21 | stream.bit_rate = 8_000_000 22 | 23 | camera = dxcam.create(output_color="RGB") 24 | camera.start(target_fps=target_fps, video_mode=True) 25 | 26 | 27 | def save_replay(): 28 | global container, buffer_lock, stream, buffer, replay_count 29 | print("Saving Instant Replay for the last 10 seconds...") 30 | with buffer_lock: 31 | for idx, packet in enumerate(buffer): 32 | packet.pts = packet.dts = idx 33 | container.mux(packet) 34 | for packet in stream.encode(): 35 | container.mux(packet) 36 | container.close() 37 | replay_count += 1 38 | container = av.open(f"replay{replay_count}.mp4", mode="w") 39 | stream = container.add_stream("mpeg4", rate=target_fps) 40 | stream.pix_fmt, stream.height, stream.width = "yuv420p", 1080, 1920 41 | stream.bit_rate = 8_000_000 42 | 43 | 44 | def stop_record(): 45 | global stop_event 46 | print("Closing") 47 | stop_event.set() 48 | 49 | 50 | listener = keyboard.GlobalHotKeys( 51 | {"++h": save_replay, "++i": stop_record} 52 | ) 53 | listener.start() 54 | try: 55 | listener.wait() 56 | while not stop_event.is_set(): 57 | frame = av.VideoFrame.from_ndarray(camera.get_latest_frame(), format="rgb24") 58 | try: 59 | with buffer_lock: 60 | for packet in stream.encode(frame): 61 | buffer.append(packet) 62 | del frame 63 | except Exception as e: 64 | continue 65 | finally: 66 | listener.stop() 67 | listener.join() 68 | camera.stop() 69 | container.close() 70 | -------------------------------------------------------------------------------- /dxcam/processor/numpy_processor.py: -------------------------------------------------------------------------------- 1 | import ctypes 2 | from numpy import rot90, ndarray, newaxis, uint8, zeros 3 | from numpy.ctypeslib import as_array 4 | from .base import Processor 5 | 6 | 7 | class NumpyProcessor(Processor): 8 | def __init__(self, color_mode): 9 | self.cvtcolor = None 10 | self.color_mode = color_mode 11 | self.PBYTE = ctypes.POINTER(ctypes.c_ubyte) 12 | 13 | def process_cvtcolor(self, image): 14 | import cv2 15 | 16 | # only one time process 17 | if self.cvtcolor is None: 18 | color_mapping = { 19 | "RGB": cv2.COLOR_BGRA2RGB, 20 | "RGBA": cv2.COLOR_BGRA2RGBA, 21 | "BGR": cv2.COLOR_BGRA2BGR, 22 | "GRAY": cv2.COLOR_BGRA2GRAY, 23 | "BGRA": None, 24 | } 25 | cv2_code = color_mapping[self.color_mode] 26 | if cv2_code is not None: 27 | if cv2_code != cv2.COLOR_BGRA2GRAY: 28 | self.cvtcolor = lambda image: cv2.cvtColor(image, cv2_code) 29 | else: 30 | self.cvtcolor = lambda image: cv2.cvtColor(image, cv2_code)[ 31 | ..., newaxis 32 | ] 33 | else: 34 | return image 35 | 36 | return self.cvtcolor(image) 37 | 38 | def shot(self, image_ptr, rect, width, height): 39 | ctypes.memmove(image_ptr, rect.pBits, height*width*4) 40 | 41 | def process(self, rect, width, height, region, rotation_angle): 42 | width = region[2] - region[0] 43 | height = region[3] - region[1] 44 | if rotation_angle in (90, 270): 45 | width, height = height, width 46 | 47 | buffer = ctypes.cast(rect.pBits, self.PBYTE) 48 | image = as_array(buffer, (height, width, 4)) 49 | 50 | # Another approach from https://github.com/Agade09/DXcam 51 | # buffer = (ctypes.c_char*height*width*4).from_address(ctypes.addressof(rect.pBits.contents)) 52 | # image = ndarray((height, width, 4), dtype=uint8, buffer=buffer) 53 | 54 | if rotation_angle != 0: 55 | image = rot90(image, k=rotation_angle//90, axes=(1, 0)) 56 | 57 | if self.color_mode is not None: 58 | return self.process_cvtcolor(image) 59 | 60 | return image 61 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | 131 | # Local tests 132 | .test/ -------------------------------------------------------------------------------- /dxcam/core/device.py: -------------------------------------------------------------------------------- 1 | import ctypes 2 | from dataclasses import dataclass 3 | from typing import List 4 | import comtypes 5 | from dxcam._libs.d3d11 import * 6 | from dxcam._libs.dxgi import * 7 | 8 | 9 | @dataclass 10 | class Device: 11 | adapter: ctypes.POINTER(IDXGIAdapter1) 12 | device: ctypes.POINTER(ID3D11Device) = None 13 | context: ctypes.POINTER(ID3D11DeviceContext) = None 14 | im_context: ctypes.POINTER(ID3D11DeviceContext) = None 15 | desc: DXGI_ADAPTER_DESC1 = None 16 | 17 | def __post_init__(self) -> None: 18 | self.desc = DXGI_ADAPTER_DESC1() 19 | self.adapter.GetDesc1(ctypes.byref(self.desc)) 20 | 21 | D3D11CreateDevice = ctypes.windll.d3d11.D3D11CreateDevice 22 | 23 | feature_levels = [ 24 | D3D_FEATURE_LEVEL_11_0, 25 | D3D_FEATURE_LEVEL_10_1, 26 | D3D_FEATURE_LEVEL_10_0, 27 | ] 28 | 29 | self.device = ctypes.POINTER(ID3D11Device)() 30 | self.context = ctypes.POINTER(ID3D11DeviceContext)() 31 | self.im_context = ctypes.POINTER(ID3D11DeviceContext)() 32 | 33 | D3D11CreateDevice( 34 | self.adapter, 35 | 0, 36 | None, 37 | 0, 38 | ctypes.byref((ctypes.c_uint * len(feature_levels))(*feature_levels)), 39 | len(feature_levels), 40 | 7, 41 | ctypes.byref(self.device), 42 | None, 43 | ctypes.byref(self.context), 44 | ) 45 | self.device.GetImmediateContext(ctypes.byref(self.im_context)) 46 | 47 | def enum_outputs(self) -> List[ctypes.POINTER(IDXGIOutput1)]: 48 | i = 0 49 | p_outputs = [] 50 | while True: 51 | try: 52 | p_output = ctypes.POINTER(IDXGIOutput1)() 53 | self.adapter.EnumOutputs(i, ctypes.byref(p_output)) 54 | p_outputs.append(p_output) 55 | i += 1 56 | except comtypes.COMError as ce: 57 | if ctypes.c_int32(DXGI_ERROR_NOT_FOUND).value == ce.args[0]: 58 | break 59 | else: 60 | raise ce 61 | return p_outputs 62 | 63 | @property 64 | def description(self) -> str: 65 | return self.desc.Description 66 | 67 | @property 68 | def vram_size(self) -> int: 69 | return self.desc.DedicatedVideoMemory 70 | 71 | @property 72 | def vendor_id(self) -> int: 73 | return self.desc.VendorId 74 | 75 | def __repr__(self) -> str: 76 | return "<{} Name:{} Dedicated VRAM:{}Mb VendorId:{}>".format( 77 | self.__class__.__name__, 78 | self.desc.Description, 79 | self.desc.DedicatedVideoMemory // 1048576, 80 | self.desc.VendorId, 81 | ) 82 | -------------------------------------------------------------------------------- /dxcam/core/stagesurf.py: -------------------------------------------------------------------------------- 1 | import ctypes 2 | from dataclasses import dataclass, InitVar 3 | from dxcam._libs.d3d11 import * 4 | from dxcam._libs.dxgi import * 5 | from dxcam.core.device import Device 6 | from dxcam.core.output import Output 7 | from typing import Tuple 8 | 9 | 10 | @dataclass 11 | class StageSurface: 12 | width: ctypes.c_uint32 = 0 13 | height: ctypes.c_uint32 = 0 14 | dxgi_format: ctypes.c_uint32 = DXGI_FORMAT_B8G8R8A8_UNORM 15 | desc: D3D11_TEXTURE2D_DESC = D3D11_TEXTURE2D_DESC() 16 | texture: ctypes.POINTER(ID3D11Texture2D) = None 17 | output: InitVar[Output] = None 18 | device: InitVar[Device] = None 19 | 20 | def __post_init__(self, output, device) -> None: 21 | self.rebuild(output, device) 22 | 23 | def release(self): 24 | if self.texture is not None: 25 | self.width = 0 26 | self.height = 0 27 | self.texture.Release() 28 | self.texture = None 29 | 30 | def rebuild(self, output: Output, device: Device, dim:Tuple[int]=None): 31 | if dim is not None: 32 | self.width, self.height = dim 33 | else: 34 | self.width, self.height = output.surface_size 35 | 36 | if self.texture is None: 37 | self.desc.Width = self.width 38 | self.desc.Height = self.height 39 | self.desc.Format = self.dxgi_format 40 | self.desc.MipLevels = 1 41 | self.desc.ArraySize = 1 42 | self.desc.SampleDesc.Count = 1 43 | self.desc.SampleDesc.Quality = 0 44 | self.desc.Usage = D3D11_USAGE_STAGING 45 | self.desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ 46 | self.desc.MiscFlags = 0 47 | self.desc.BindFlags = 0 48 | self.texture = ctypes.POINTER(ID3D11Texture2D)() 49 | device.device.CreateTexture2D( 50 | ctypes.byref(self.desc), 51 | None, 52 | ctypes.byref(self.texture), 53 | ) 54 | 55 | self.interface = self.texture.QueryInterface(IDXGISurface) # Caching to improve performance from https://github.com/Agade09/DXcam 56 | 57 | def map(self): 58 | rect: DXGI_MAPPED_RECT = DXGI_MAPPED_RECT() 59 | self.interface.Map(ctypes.byref(rect), 1) 60 | return rect 61 | 62 | def unmap(self): 63 | self.interface.Unmap() 64 | 65 | def __repr__(self) -> str: 66 | repr = f"{self.width}, {self.height}, {self.dxgi_format}" 67 | return repr 68 | 69 | def __repr__(self) -> str: 70 | return "<{} Initialized:{} Size:{} Format:{}>".format( 71 | self.__class__.__name__, 72 | self.texture is not None, 73 | (self.width, self.height), 74 | "DXGI_FORMAT_B8G8R8A8_UNORM", 75 | ) 76 | -------------------------------------------------------------------------------- /dxcam/util/io.py: -------------------------------------------------------------------------------- 1 | import ctypes 2 | from typing import List 3 | from collections import defaultdict 4 | import comtypes 5 | from dxcam._libs.dxgi import ( 6 | IDXGIFactory1, 7 | IDXGIAdapter1, 8 | IDXGIOutput1, 9 | DXGI_ERROR_NOT_FOUND, 10 | ) 11 | from dxcam._libs.user32 import ( 12 | DISPLAY_DEVICE, 13 | MONITORINFOEXW, 14 | DISPLAY_DEVICE_ACTIVE, 15 | DISPLAY_DEVICE_PRIMARY_DEVICE, 16 | ) 17 | 18 | 19 | def enum_dxgi_adapters() -> List[ctypes.POINTER(IDXGIAdapter1)]: 20 | create_dxgi_factory = ctypes.windll.dxgi.CreateDXGIFactory1 21 | create_dxgi_factory.argtypes = (comtypes.GUID, ctypes.POINTER(ctypes.c_void_p)) 22 | create_dxgi_factory.restype = ctypes.c_int32 23 | pfactory = ctypes.c_void_p(0) 24 | create_dxgi_factory(IDXGIFactory1._iid_, ctypes.byref(pfactory)) 25 | dxgi_factory = ctypes.POINTER(IDXGIFactory1)(pfactory.value) 26 | i = 0 27 | p_adapters = list() 28 | while True: 29 | try: 30 | p_adapter = ctypes.POINTER(IDXGIAdapter1)() 31 | dxgi_factory.EnumAdapters1(i, ctypes.byref(p_adapter)) 32 | p_adapters.append(p_adapter) 33 | i += 1 34 | except comtypes.COMError as ce: 35 | if ctypes.c_int32(DXGI_ERROR_NOT_FOUND).value == ce.args[0]: 36 | break 37 | else: 38 | raise ce 39 | return p_adapters 40 | 41 | 42 | def enum_dxgi_outputs( 43 | dxgi_adapter: ctypes.POINTER(IDXGIAdapter1), 44 | ) -> List[ctypes.POINTER(IDXGIOutput1)]: 45 | i = 0 46 | p_outputs = list() 47 | while True: 48 | try: 49 | p_output = ctypes.POINTER(IDXGIOutput1)() 50 | dxgi_adapter.EnumOutputs(i, ctypes.byref(p_output)) 51 | p_outputs.append(p_output) 52 | i += 1 53 | except comtypes.COMError as ce: 54 | if ctypes.c_int32(DXGI_ERROR_NOT_FOUND).value == ce.args[0]: 55 | break 56 | else: 57 | raise ce 58 | return p_outputs 59 | 60 | 61 | def get_output_metadata(): 62 | mapping_adapter = defaultdict(list) 63 | adapter = DISPLAY_DEVICE() 64 | adapter.cb = ctypes.sizeof(adapter) 65 | i = 0 66 | # Enumerate all adapters 67 | while ctypes.windll.user32.EnumDisplayDevicesW(0, i, ctypes.byref(adapter), 1): 68 | if adapter.StateFlags & DISPLAY_DEVICE_ACTIVE != 0: 69 | is_primary = bool(adapter.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) 70 | mapping_adapter[adapter.DeviceName] = [adapter.DeviceString, is_primary, []] 71 | display = DISPLAY_DEVICE() 72 | display.cb = ctypes.sizeof(adapter) 73 | j = 0 74 | # Enumerate Monitors 75 | while ctypes.windll.user32.EnumDisplayDevicesW( 76 | adapter.DeviceName, j, ctypes.byref(display), 0 77 | ): 78 | mapping_adapter[adapter.DeviceName][2].append( 79 | ( 80 | display.DeviceName, 81 | display.DeviceString, 82 | ) 83 | ) 84 | j += 1 85 | i += 1 86 | return mapping_adapter 87 | 88 | 89 | def get_monitor_name_by_handle(hmonitor): 90 | info = MONITORINFOEXW() 91 | info.cbSize = ctypes.sizeof(MONITORINFOEXW) 92 | if ctypes.windll.user32.GetMonitorInfoW(hmonitor, ctypes.byref(info)): 93 | return info 94 | return None 95 | -------------------------------------------------------------------------------- /dxcam/core/duplicator.py: -------------------------------------------------------------------------------- 1 | import ctypes 2 | from time import sleep 3 | from dataclasses import dataclass, InitVar 4 | from dxcam._libs.d3d11 import * 5 | from dxcam._libs.dxgi import * 6 | from dxcam.core.device import Device 7 | from dxcam.core.output import Output 8 | 9 | 10 | @dataclass 11 | class Duplicator: 12 | texture: ctypes.POINTER(ID3D11Texture2D) = ctypes.POINTER(ID3D11Texture2D)() 13 | duplicator: ctypes.POINTER(IDXGIOutputDuplication) = None 14 | updated: bool = False 15 | output: InitVar[Output] = None 16 | device: InitVar[Device] = None 17 | cursor: ac_Cursor = ac_Cursor() 18 | 19 | def __post_init__(self, output: Output, device: Device) -> None: 20 | self.duplicator = ctypes.POINTER(IDXGIOutputDuplication)() 21 | output.output.DuplicateOutput(device.device, ctypes.byref(self.duplicator)) 22 | 23 | def update_frame(self): 24 | info = DXGI_OUTDUPL_FRAME_INFO() 25 | res = ctypes.POINTER(IDXGIResource)() 26 | try: 27 | self.duplicator.AcquireNextFrame( 28 | 10, 29 | ctypes.byref(info), 30 | ctypes.byref(res), 31 | ) 32 | if info.LastMouseUpdateTime > 0: 33 | new_PointerInfo, new_PointerShape = self.get_frame_pointer_shape(info) 34 | if new_PointerShape != False: 35 | self.cursor.Shape = new_PointerShape 36 | self.cursor.PointerShapeInfo = new_PointerInfo 37 | self.cursor.PointerPositionInfo = info.PointerPosition 38 | except comtypes.COMError as ce: 39 | if ctypes.c_int32(DXGI_ERROR_ACCESS_LOST).value == ce.args[0] or ctypes.c_int32(ABANDONED_MUTEX_EXCEPTION).value == ce.args[0]: 40 | self.release() # Release resources before reinitializing 41 | sleep(0.1) 42 | self.__post_init__(self.output, self.device) 43 | return False 44 | if ctypes.c_int32(DXGI_ERROR_WAIT_TIMEOUT).value == ce.args[0]: 45 | self.updated = False 46 | return True 47 | else: 48 | raise ce 49 | 50 | if info.LastPresentTime == 0: 51 | self.duplicator.ReleaseFrame() 52 | self.updated = False 53 | return True 54 | 55 | try: 56 | self.texture = res.QueryInterface(ID3D11Texture2D) 57 | except comtypes.COMError as ce: 58 | self.duplicator.ReleaseFrame() 59 | 60 | self.updated = True 61 | return True 62 | 63 | def release_frame(self): 64 | self.duplicator.ReleaseFrame() 65 | 66 | def release(self): 67 | if self.duplicator is not None: 68 | self.duplicator.Release() 69 | self.duplicator = None 70 | 71 | def get_frame_pointer_shape(self, FrameInfo): 72 | PointerShapeInfo = DXGI_OUTDUPL_POINTER_SHAPE_INFO() 73 | buffer_size_required = ctypes.c_uint() 74 | pPointerShapeBuffer = (ctypes.c_byte*FrameInfo.PointerShapeBufferSize)() 75 | hr = self.duplicator.GetFramePointerShape(FrameInfo.PointerShapeBufferSize, ctypes.byref(pPointerShapeBuffer), ctypes.byref(buffer_size_required), ctypes.byref(PointerShapeInfo)) 76 | if FrameInfo.PointerShapeBufferSize > 0: 77 | #print("T",PointerShapeInfo.Type,PointerShapeInfo.Width,"x",PointerShapeInfo.Height,"Pitch:",PointerShapeInfo.Pitch,"HS:",PointerShapeInfo.HotSpot.x,PointerShapeInfo.HotSpot.y) 78 | return PointerShapeInfo, pPointerShapeBuffer 79 | return False, False 80 | 81 | def __repr__(self) -> str: 82 | return "<{} Initalized:{}>".format( 83 | self.__class__.__name__, 84 | self.duplicator is not None, 85 | ) 86 | -------------------------------------------------------------------------------- /dxcam/__init__.py: -------------------------------------------------------------------------------- 1 | import weakref 2 | import time 3 | from dxcam.dxcam import DXCamera, Output, Device 4 | from dxcam.util.io import ( 5 | enum_dxgi_adapters, 6 | get_output_metadata, 7 | ) 8 | 9 | 10 | class Singleton(type): 11 | _instances = {} 12 | 13 | def __call__(cls, *args, **kwargs): 14 | if cls not in cls._instances: 15 | cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) 16 | else: 17 | print(f"Only 1 instance of {cls.__name__} is allowed.") 18 | 19 | return cls._instances[cls] 20 | 21 | 22 | class DXFactory(metaclass=Singleton): 23 | 24 | _camera_instances = weakref.WeakValueDictionary() 25 | 26 | def __init__(self) -> None: 27 | p_adapters = enum_dxgi_adapters() 28 | self.devices, self.outputs = [], [] 29 | for p_adapter in p_adapters: 30 | device = Device(p_adapter) 31 | p_outputs = device.enum_outputs() 32 | if len(p_outputs) != 0: 33 | self.devices.append(device) 34 | self.outputs.append([Output(p_output) for p_output in p_outputs]) 35 | self.output_metadata = get_output_metadata() 36 | 37 | def create( 38 | self, 39 | device_idx: int = 0, 40 | output_idx: int = None, 41 | region: tuple = None, 42 | output_color: str = "RGB", 43 | max_buffer_len: int = 64, 44 | ): 45 | device = self.devices[device_idx] 46 | if output_idx is None: 47 | # Select Primary Output 48 | output_idx = [ 49 | idx 50 | for idx, metadata in enumerate( 51 | self.output_metadata.get(output.devicename) 52 | for output in self.outputs[device_idx] 53 | ) 54 | if metadata[1] 55 | ][0] 56 | instance_key = (device_idx, output_idx) 57 | if instance_key in self._camera_instances: 58 | print( 59 | "".join( 60 | ( 61 | f"You already created a DXCamera Instance for Device {device_idx}--Output {output_idx}!\n", 62 | "Returning the existed instance...\n", 63 | "To change capture parameters you can manually delete the old object using `del obj`.", 64 | ) 65 | ) 66 | ) 67 | return self._camera_instances[instance_key] 68 | 69 | output = self.outputs[device_idx][output_idx] 70 | output.update_desc() 71 | camera = DXCamera( 72 | output=output, 73 | device=device, 74 | region=region, 75 | output_color=output_color, 76 | max_buffer_len=max_buffer_len, 77 | ) 78 | self._camera_instances[instance_key] = camera 79 | time.sleep(0.1) # Fix for https://github.com/ra1nty/DXcam/issues/31 80 | return camera 81 | 82 | def device_info(self) -> str: 83 | ret = "" 84 | for idx, device in enumerate(self.devices): 85 | ret += f"Device[{idx}]:{device}\n" 86 | return ret 87 | 88 | def output_info(self) -> str: 89 | ret = "" 90 | for didx, outputs in enumerate(self.outputs): 91 | for idx, output in enumerate(outputs): 92 | ret += f"Device[{didx}] Output[{idx}]: " 93 | ret += f"szDevice[{output.devicename}]: " 94 | ret += f"Res:{output.resolution} Rot:{output.rotation_angle}" 95 | ret += f" Primary:{self.output_metadata.get(output.devicename)[1]}\n" 96 | return ret 97 | 98 | def clean_up(self): 99 | for _, camera in self._camera_instances.items(): 100 | camera.release() 101 | 102 | 103 | __factory = DXFactory() 104 | 105 | 106 | def create( 107 | device_idx: int = 0, 108 | output_idx: int = None, 109 | region: tuple = None, 110 | output_color: str = "RGB", 111 | max_buffer_len: int = 64, 112 | ): 113 | return __factory.create( 114 | device_idx=device_idx, 115 | output_idx=output_idx, 116 | region=region, 117 | output_color=output_color, 118 | max_buffer_len=max_buffer_len, 119 | ) 120 | 121 | 122 | def device_info(): 123 | return __factory.device_info() 124 | 125 | 126 | def output_info(): 127 | return __factory.output_info() 128 | -------------------------------------------------------------------------------- /dxcam/_libs/dxgi.py: -------------------------------------------------------------------------------- 1 | import ctypes 2 | import ctypes.wintypes as wintypes 3 | import comtypes 4 | from .d3d11 import ID3D11Device 5 | 6 | 7 | DXGI_ERROR_ACCESS_LOST = 0x887A0026 8 | DXGI_ERROR_NOT_FOUND = 0x887A0002 9 | DXGI_ERROR_WAIT_TIMEOUT = 0x887A0027 10 | ABANDONED_MUTEX_EXCEPTION = -0x7785ffda # -2005270490 11 | 12 | 13 | class LUID(ctypes.Structure): 14 | _fields_ = [("LowPart", wintypes.DWORD), ("HighPart", wintypes.LONG)] 15 | 16 | 17 | class DXGI_ADAPTER_DESC1(ctypes.Structure): 18 | _fields_ = [ 19 | ("Description", wintypes.WCHAR * 128), 20 | ("VendorId", wintypes.UINT), 21 | ("DeviceId", wintypes.UINT), 22 | ("SubSysId", wintypes.UINT), 23 | ("Revision", wintypes.UINT), 24 | ("DedicatedVideoMemory", wintypes.ULARGE_INTEGER), 25 | ("DedicatedSystemMemory", wintypes.ULARGE_INTEGER), 26 | ("SharedSystemMemory", wintypes.ULARGE_INTEGER), 27 | ("AdapterLuid", LUID), 28 | ("Flags", wintypes.UINT), 29 | ] 30 | 31 | 32 | class DXGI_OUTPUT_DESC(ctypes.Structure): 33 | _fields_ = [ 34 | ("DeviceName", wintypes.WCHAR * 32), 35 | ("DesktopCoordinates", wintypes.RECT), 36 | ("AttachedToDesktop", wintypes.BOOL), 37 | ("Rotation", wintypes.UINT), 38 | ("Monitor", wintypes.HMONITOR), 39 | ] 40 | 41 | 42 | class DXGI_OUTDUPL_POINTER_POSITION(ctypes.Structure): 43 | _fields_ = [("Position", wintypes.POINT), ("Visible", wintypes.BOOL)] 44 | 45 | class DXGI_OUTDUPL_POINTER_SHAPE_INFO(ctypes.Structure): 46 | _fields_ = [('Type', wintypes.UINT), 47 | ('Width', wintypes.UINT), 48 | ('Height', wintypes.UINT), 49 | ('Pitch', wintypes.UINT), 50 | ('HotSpot', wintypes.POINT), 51 | ] 52 | 53 | class ac_Cursor(): 54 | def __init__(self): 55 | self.PointerPositionInfo: DXGI_OUTDUPL_POINTER_POSITION = DXGI_OUTDUPL_POINTER_POSITION() 56 | self.PointerShapeInfo: DXGI_OUTDUPL_POINTER_SHAPE_INFO = DXGI_OUTDUPL_POINTER_SHAPE_INFO() 57 | self.Shape: bytes = None 58 | 59 | 60 | class DXGI_OUTDUPL_FRAME_INFO(ctypes.Structure): 61 | _fields_ = [ 62 | ("LastPresentTime", wintypes.LARGE_INTEGER), 63 | ("LastMouseUpdateTime", wintypes.LARGE_INTEGER), 64 | ("AccumulatedFrames", wintypes.UINT), 65 | ("RectsCoalesced", wintypes.BOOL), 66 | ("ProtectedContentMaskedOut", wintypes.BOOL), 67 | ("PointerPosition", DXGI_OUTDUPL_POINTER_POSITION), 68 | ("TotalMetadataBufferSize", wintypes.UINT), 69 | ("PointerShapeBufferSize", wintypes.UINT), 70 | ] 71 | 72 | 73 | class DXGI_MAPPED_RECT(ctypes.Structure): 74 | _fields_ = [("Pitch", wintypes.INT), ("pBits", ctypes.POINTER(wintypes.FLOAT))] 75 | 76 | 77 | class IDXGIObject(comtypes.IUnknown): 78 | _iid_ = comtypes.GUID("{aec22fb8-76f3-4639-9be0-28eb43a67a2e}") 79 | _methods_ = [ 80 | comtypes.STDMETHOD(comtypes.HRESULT, "SetPrivateData"), 81 | comtypes.STDMETHOD(comtypes.HRESULT, "SetPrivateDataInterface"), 82 | comtypes.STDMETHOD(comtypes.HRESULT, "GetPrivateData"), 83 | comtypes.STDMETHOD(comtypes.HRESULT, "GetParent"), 84 | ] 85 | 86 | 87 | class IDXGIDeviceSubObject(IDXGIObject): 88 | _iid_ = comtypes.GUID("{3d3e0379-f9de-4d58-bb6c-18d62992f1a6}") 89 | _methods_ = [ 90 | comtypes.STDMETHOD(comtypes.HRESULT, "GetDevice"), 91 | ] 92 | 93 | 94 | class IDXGIResource(IDXGIDeviceSubObject): 95 | _iid_ = comtypes.GUID("{035f3ab4-482e-4e50-b41f-8a7f8bd8960b}") 96 | _methods_ = [ 97 | comtypes.STDMETHOD(comtypes.HRESULT, "GetSharedHandle"), 98 | comtypes.STDMETHOD(comtypes.HRESULT, "GetUsage"), 99 | comtypes.STDMETHOD(comtypes.HRESULT, "SetEvictionPriority"), 100 | comtypes.STDMETHOD(comtypes.HRESULT, "GetEvictionPriority"), 101 | ] 102 | 103 | 104 | class IDXGISurface(IDXGIDeviceSubObject): 105 | _iid_ = comtypes.GUID("{cafcb56c-6ac3-4889-bf47-9e23bbd260ec}") 106 | _methods_ = [ 107 | comtypes.STDMETHOD(comtypes.HRESULT, "GetDesc"), 108 | comtypes.STDMETHOD( 109 | comtypes.HRESULT, "Map", [ctypes.POINTER(DXGI_MAPPED_RECT), wintypes.UINT] 110 | ), 111 | comtypes.STDMETHOD(comtypes.HRESULT, "Unmap"), 112 | ] 113 | 114 | 115 | class IDXGIOutputDuplication(IDXGIObject): 116 | _iid_ = comtypes.GUID("{191cfac3-a341-470d-b26e-a864f428319c}") 117 | _methods_ = [ 118 | comtypes.STDMETHOD(None, "GetDesc"), 119 | comtypes.STDMETHOD( 120 | comtypes.HRESULT, 121 | "AcquireNextFrame", 122 | [ 123 | wintypes.UINT, 124 | ctypes.POINTER(DXGI_OUTDUPL_FRAME_INFO), 125 | ctypes.POINTER(ctypes.POINTER(IDXGIResource)), 126 | ], 127 | ), 128 | comtypes.STDMETHOD(comtypes.HRESULT, "GetFrameDirtyRects"), 129 | comtypes.STDMETHOD(comtypes.HRESULT, "GetFrameMoveRects"), 130 | comtypes.STDMETHOD(comtypes.HRESULT, "GetFramePointerShape", [ 131 | wintypes.UINT, 132 | ctypes.c_void_p, 133 | ctypes.POINTER(wintypes.UINT), 134 | ctypes.POINTER(DXGI_OUTDUPL_POINTER_SHAPE_INFO), 135 | ]), 136 | comtypes.STDMETHOD(comtypes.HRESULT, "MapDesktopSurface"), 137 | comtypes.STDMETHOD(comtypes.HRESULT, "UnMapDesktopSurface"), 138 | comtypes.STDMETHOD(comtypes.HRESULT, "ReleaseFrame"), 139 | ] 140 | 141 | 142 | class IDXGIOutput(IDXGIObject): 143 | _iid_ = comtypes.GUID("{ae02eedb-c735-4690-8d52-5a8dc20213aa}") 144 | _methods_ = [ 145 | comtypes.STDMETHOD( 146 | comtypes.HRESULT, "GetDesc", [ctypes.POINTER(DXGI_OUTPUT_DESC)] 147 | ), 148 | comtypes.STDMETHOD(comtypes.HRESULT, "GetDisplayModeList"), 149 | comtypes.STDMETHOD(comtypes.HRESULT, "FindClosestMatchingMode"), 150 | comtypes.STDMETHOD(comtypes.HRESULT, "WaitForVBlank"), 151 | comtypes.STDMETHOD(comtypes.HRESULT, "TakeOwnership"), 152 | comtypes.STDMETHOD(None, "ReleaseOwnership"), 153 | comtypes.STDMETHOD(comtypes.HRESULT, "GetGammaControlCapabilities"), 154 | comtypes.STDMETHOD(comtypes.HRESULT, "SetGammaControl"), 155 | comtypes.STDMETHOD(comtypes.HRESULT, "GetGammaControl"), 156 | comtypes.STDMETHOD(comtypes.HRESULT, "SetDisplaySurface"), 157 | comtypes.STDMETHOD(comtypes.HRESULT, "GetDisplaySurfaceData"), 158 | comtypes.STDMETHOD(comtypes.HRESULT, "GetFrameStatistics"), 159 | ] 160 | 161 | 162 | class IDXGIOutput1(IDXGIOutput): 163 | _iid_ = comtypes.GUID("{00cddea8-939b-4b83-a340-a685226666cc}") 164 | _methods_ = [ 165 | comtypes.STDMETHOD(comtypes.HRESULT, "GetDisplayModeList1"), 166 | comtypes.STDMETHOD(comtypes.HRESULT, "FindClosestMatchingMode1"), 167 | comtypes.STDMETHOD(comtypes.HRESULT, "GetDisplaySurfaceData1"), 168 | comtypes.STDMETHOD( 169 | comtypes.HRESULT, 170 | "DuplicateOutput", 171 | [ 172 | ctypes.POINTER(ID3D11Device), 173 | ctypes.POINTER(ctypes.POINTER(IDXGIOutputDuplication)), 174 | ], 175 | ), 176 | ] 177 | 178 | 179 | class IDXGIAdapter(IDXGIObject): 180 | _iid_ = comtypes.GUID("{2411e7e1-12ac-4ccf-bd14-9798e8534dc0}") 181 | _methods_ = [ 182 | comtypes.STDMETHOD( 183 | comtypes.HRESULT, 184 | "EnumOutputs", 185 | [wintypes.UINT, ctypes.POINTER(ctypes.POINTER(IDXGIOutput))], 186 | ), 187 | comtypes.STDMETHOD(comtypes.HRESULT, "GetDesc"), 188 | comtypes.STDMETHOD(comtypes.HRESULT, "CheckInterfaceSupport"), 189 | ] 190 | 191 | 192 | class IDXGIAdapter1(IDXGIAdapter): 193 | _iid_ = comtypes.GUID("{29038f61-3839-4626-91fd-086879011a05}") 194 | _methods_ = [ 195 | comtypes.STDMETHOD( 196 | comtypes.HRESULT, "GetDesc1", [ctypes.POINTER(DXGI_ADAPTER_DESC1)] 197 | ), 198 | ] 199 | 200 | 201 | class IDXGIFactory(IDXGIObject): 202 | _iid_ = comtypes.GUID("{7b7166ec-21c7-44ae-b21a-c9ae321ae369}") 203 | _methods_ = [ 204 | comtypes.STDMETHOD(comtypes.HRESULT, "EnumAdapters"), 205 | comtypes.STDMETHOD(comtypes.HRESULT, "MakeWindowAssociation"), 206 | comtypes.STDMETHOD(comtypes.HRESULT, "GetWindowAssociation"), 207 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateSwapChain"), 208 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateSoftwareAdapter"), 209 | ] 210 | 211 | 212 | class IDXGIFactory1(IDXGIFactory): 213 | _iid_ = comtypes.GUID("{770aae78-f26f-4dba-a829-253c83d1b387}") 214 | _methods_ = [ 215 | comtypes.STDMETHOD( 216 | comtypes.HRESULT, 217 | "EnumAdapters1", 218 | [ctypes.c_uint, ctypes.POINTER(ctypes.POINTER(IDXGIAdapter1))], 219 | ), 220 | comtypes.STDMETHOD(wintypes.BOOL, "IsCurrent"), 221 | ] 222 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This fork adds basic cursor support (GetFramePointerShape) via grab_cursor function 2 | Its used for https://github.com/scamiv/ActiveClone 3 | 4 | # **DXcam** 5 | > ***Fastest Python Screenshot for Windows*** 6 | ```python 7 | import dxcam 8 | camera = dxcam.create() 9 | camera.grab() 10 | ``` 11 | 12 | ## Introduction 13 | DXcam is a Python high-performance screenshot library for Windows using Desktop Duplication API. Capable of 240Hz+ capturing. It was originally built as a part of deep learning pipeline for FPS games to perform better than existed python solutions ([python-mss](https://github.com/BoboTiG/python-mss), [D3DShot](https://github.com/SerpentAI/D3DShot/)). 14 | 15 | Compared to these existed solutions, DXcam provides: 16 | - Way faster screen capturing speed (> 240Hz) 17 | - Capturing of Direct3D exclusive full-screen application without interrupting, even when alt+tab. 18 | - Automatic handling of scaled / stretched resolution. 19 | - Accurate FPS targeting when in capturing mode, makes it suitable for Video output. 20 | - Seamless integration with NumPy, OpenCV, PyTorch, etc. 21 | 22 | > ***Contributions are welcome!*** 23 | 24 | ## Installation 25 | ### From PyPI: 26 | ```bash 27 | pip install dxcam 28 | ``` 29 | 30 | **Note:** OpenCV is required by DXcam for colorspace conversion. If you don't already have OpenCV, install it easily with command `pip install dxcam[cv2]`. 31 | 32 | ### From source: 33 | ```bash 34 | pip install --editable . 35 | 36 | # for installing OpenCV also 37 | pip install --editable .[cv2] 38 | ``` 39 | 40 | ## Usage 41 | In DXCam, each output (monitor) is asscociated to a ```DXCamera``` instance. 42 | To create a DXCamera instance: 43 | ```python 44 | import dxcam 45 | camera = dxcam.create() # returns a DXCamera instance on primary monitor 46 | ``` 47 | ### Screenshot 48 | For screenshot, simply use ```.grab```: 49 | ```python 50 | frame = camera.grab() 51 | ``` 52 | The returned ```frame``` will be a ```numpy.ndarray``` in the shape of ```(Height, Width, 3[RGB])```. This is the default and the only supported format (**for now**). It is worth noting that ```.grab``` will return ```None``` if there is no new frame since the last time you called ```.grab```. Usually it means there's nothing new to render since last time (E.g. You are idling). 53 | 54 | To view the captured screenshot: 55 | ```python 56 | from PIL import Image 57 | Image.fromarray(frame).show() 58 | ``` 59 | To screenshot a specific region, use the ```region``` parameter: it takes ```tuple[int, int, int, int]``` as the left, top, right, bottom coordinates of the bounding box. Similar to [PIL.ImageGrab.grab](https://pillow.readthedocs.io/en/stable/reference/ImageGrab.html). 60 | ```python 61 | left, top = (1920 - 640) // 2, (1080 - 640) // 2 62 | right, bottom = left + 640, top + 640 63 | region = (left, top, right, bottom) 64 | frame = camera.grab(region=region) # numpy.ndarray of size (640x640x3) -> (HXWXC) 65 | ``` 66 | The above code will take a screenshot of the center ```640x640``` portion of a ```1920x1080``` monitor. 67 | ### Screen Capture 68 | To start a screen capture, simply use ```.start```: the capture will be started in a separated thread, default at 60Hz. Use ```.stop``` to stop the capture. 69 | ```python 70 | camera.start(region=(left, top, right, bottom)) # Optional argument to capture a region 71 | camera.is_capturing # True 72 | # ... Do Something 73 | camera.stop() 74 | camera.is_capturing # False 75 | ``` 76 | ### Consume the Screen Capture Data 77 | While the ```DXCamera``` instance is in capture mode, you can use ```.get_latest_frame``` to get the latest frame in the frame buffer: 78 | ```python 79 | camera.start() 80 | for i in range(1000): 81 | image = camera.get_latest_frame() # Will block until new frame available 82 | camera.stop() 83 | ``` 84 | Notice that ```.get_latest_frame``` by default will block until there is a new frame available since the last call to ```.get_latest_frame```. To change this behavior, use ```video_mode=True```. 85 | 86 | ## Advanced Usage and Remarks 87 | ### Multiple monitors / GPUs 88 | ```python 89 | cam1 = dxcam.create(device_idx=0, output_idx=0) 90 | cam2 = dxcam.create(device_idx=0, output_idx=1) 91 | cam3 = dxcam.create(device_idx=1, output_idx=1) 92 | img1 = cam1.grab() 93 | img2 = cam2.grab() 94 | img2 = cam3.grab() 95 | ``` 96 | The above code creates three ```DXCamera``` instances for: ```[monitor0, GPU0], [monitor1, GPU0], [monitor1, GPU1]```, and subsequently takes three full-screen screenshots. (cross GPU untested, but I hope it works.) To get a complete list of devices and outputs: 97 | ```pycon 98 | >>> import dxcam 99 | >>> dxcam.device_info() 100 | 'Device[0]:\n' 101 | >>> dxcam.output_info() 102 | 'Device[0] Output[0]: Res:(1920, 1080) Rot:0 Primary:True\nDevice[0] Output[1]: Res:(1920, 1080) Rot:0 Primary:False\n' 103 | ``` 104 | 105 | ### Output Format 106 | You can specify the output color mode upon creation of the DXCamera instance: 107 | ```python 108 | dxcam.create(output_idx=0, output_color="BGRA") 109 | ``` 110 | We currently support "RGB", "RGBA", "BGR", "BGRA", "GRAY", with "GRAY being the gray scale. As for the data format, ```DXCamera``` only supports ```numpy.ndarray``` in shape of ```(Height, Width, Channels)``` right now. ***We will soon add support for other output formats.*** 111 | 112 | ### Video Buffer 113 | The captured frames will be insert into a fixed-size ring buffer, and when the buffer is full the newest frame will replace the oldest frame. You can specify the max buffer length (defualt to 64) using the argument ```max_buffer_len``` upon creation of the ```DXCamera``` instance. 114 | ```python 115 | camera = dxcam.create(max_buffer_len=512) 116 | ``` 117 | ***Note: Right now to consume frames during capturing there is only `get_latest_frame` available which assume the user to process frames in a LIFO pattern. This is a read-only action and won't pop the processed frame from the buffer. we will make changes to support various of consuming pattern soon.*** 118 | 119 | ### Target FPS 120 | To make ```DXCamera``` capture close to the user specified ```target_fps```, we used the undocumented ```CREATE_WAITABLE_TIMER_HIGH_RESOLUTION ``` flag to create a Windows [Waitable Timer Object](https://docs.microsoft.com/en-us/windows/win32/sync/waitable-timer-objects). This is far more accurate (+/- 1ms) than Python (<3.11) ```time.sleep``` (min resolution 16ms). The implementation is done through ```ctypes``` creating a perodic timer. Python 3.11 used a similar approach[^2]. 121 | ```python 122 | camera.start(target_fps=120) # Should not be made greater than 160. 123 | ``` 124 | However, due to Windows itself is a preemptive OS[^1] and the overhead of Python calls, the target FPS can not be guarenteed accurate when greater than 160. (See Benchmarks) 125 | 126 | 127 | ### Video Mode 128 | The default behavior of ```.get_latest_frame``` only put newly rendered frame in the buffer, which suits the usage scenario of a object detection/machine learning pipeline. However, when recording a video that is not ideal since we aim to get the frames at a constant framerate: When the ```video_mode=True``` is specified when calling ```.start``` method of a ```DXCamera``` instance, the frame buffer will be feeded at the target fps, using the last frame if there is no new frame available. For example, the following code output a 5-second, 120Hz screen capture: 129 | ```python 130 | target_fps = 120 131 | camera = dxcam.create(output_idx=0, output_color="BGR") 132 | camera.start(target_fps=target_fps, video_mode=True) 133 | writer = cv2.VideoWriter( 134 | "video.mp4", cv2.VideoWriter_fourcc(*"mp4v"), target_fps, (1920, 1080) 135 | ) 136 | for i in range(600): 137 | writer.write(camera.get_latest_frame()) 138 | camera.stop() 139 | writer.release() 140 | ``` 141 | > You can do interesting stuff with libraries like ```pyav``` and ```pynput```: see examples/instant_replay.py for a ghetto implementation of instant replay using hot-keys 142 | 143 | 144 | ### Safely Releasing of Resource 145 | Upon calling ```.release``` on a DXCamera instance, it will stop any active capturing, free the buffer and release the duplicator and staging resource. Upon calling ```.stop()```, DXCamera will stop the active capture and free the frame buffer. If you want to manually recreate a ```DXCamera``` instance on the same output with different parameters, you can also manully delete it: 146 | ```python 147 | camera1 = dxcam.create(output_idx=0, output_color="BGR") 148 | camera2 = dxcam.create(output_idx=0) # Not allowed, camera1 will be returned 149 | camera1 is camera2 # True 150 | del camera1 151 | del camera2 152 | camera2 = dxcam.create(output_idx=0) # Allowed 153 | ``` 154 | 155 | ## Benchmarks 156 | ### For Max FPS Capability: 157 | ```python 158 | start_time, fps = time.perf_counter(), 0 159 | cam = dxcam.create() 160 | start = time.perf_counter() 161 | while fps < 1000: 162 | frame = cam.grab() 163 | if frame is not None: # New frame 164 | fps += 1 165 | end_time = time.perf_counter() - start_time 166 | print(f"{title}: {fps/end_time}") 167 | ``` 168 | When using a similar logistic (only captured new frame counts), ```DXCam, python-mss, D3DShot``` benchmarked as follow: 169 | 170 | | | DXcam | python-mss | D3DShot | 171 | |-------------|--------|------------|---------| 172 | | Average FPS | 238.79 :checkered_flag: | 75.87 | 118.36 | 173 | | Std Dev | 1.25 | 0.5447 | 0.3224 | 174 | 175 | The benchmark is across 5 runs, with a light-moderate usage on my PC (5900X + 3090; Chrome ~30tabs, VS Code opened, etc.), I used the [Blur Buster UFO test](https://www.testufo.com/framerates#count=5&background=stars&pps=960) to constantly render 240 fps on my monitor (Zowie 2546K). DXcam captured almost every frame rendered. 176 | 177 | ### For Targeting FPS: 178 | ```python 179 | camera = dxcam.create(output_idx=0) 180 | camera.start(target_fps=60) 181 | for i in range(1000): 182 | image = camera.get_latest_frame() 183 | camera.stop() 184 | ``` 185 | | (Target)\\(mean,std) | DXcam | python-mss | D3DShot | 186 | |------------- |-------- |------------|---------| 187 | | 60fps | 61.71, 0.26 :checkered_flag: | N/A | 47.11, 1.33 | 188 | | 30fps | 30.08, 0.02 :checkered_flag: | N/A | 21.24, 0.17 | 189 | 190 | ## Work Referenced 191 | [D3DShot](https://github.com/SerpentAI/D3DShot/) : DXcam borrows the ctypes header directly from the no-longer maintained D3DShot. 192 | 193 | [OBS Studio](https://github.com/obsproject/obs-studio) : Learned a lot from it. 194 | 195 | 196 | [^1]: Preemption (computing) 197 | 198 | [^2]: bpo-21302: time.sleep() uses waitable timer on Windows 199 | -------------------------------------------------------------------------------- /dxcam/_libs/d3d11.py: -------------------------------------------------------------------------------- 1 | import ctypes 2 | import ctypes.wintypes as wintypes 3 | import comtypes 4 | 5 | 6 | D3D11_CPU_ACCESS_WRITE = 0x10000 7 | D3D11_CPU_ACCESS_READ = 0x20000 8 | 9 | D3D_FEATURE_LEVEL_9_1 = 0x9100 10 | D3D_FEATURE_LEVEL_9_2 = 0x9200 11 | D3D_FEATURE_LEVEL_9_3 = 0x9300 12 | D3D_FEATURE_LEVEL_10_0 = 0xA000 13 | D3D_FEATURE_LEVEL_10_1 = 0xA100 14 | D3D_FEATURE_LEVEL_11_0 = 0xB000 15 | D3D_FEATURE_LEVEL_11_1 = 0xB100 16 | 17 | D3D11_USAGE_DEFAULT = 0 18 | D3D11_USAGE_IMMUTABLE = 1 19 | D3D11_USAGE_DYNAMIC = 2 20 | D3D11_USAGE_STAGING = 3 21 | 22 | DXGI_FORMAT_B8G8R8A8_UNORM = 87 23 | 24 | 25 | class DXGI_SAMPLE_DESC(ctypes.Structure): 26 | _fields_ = [ 27 | ("Count", wintypes.UINT), 28 | ("Quality", wintypes.UINT), 29 | ] 30 | 31 | 32 | class D3D11_BOX(ctypes.Structure): 33 | _fields_ = [ 34 | ("left", wintypes.UINT), 35 | ("top", wintypes.UINT), 36 | ("front", wintypes.UINT), 37 | ("right", wintypes.UINT), 38 | ("bottom", wintypes.UINT), 39 | ("back", wintypes.UINT), 40 | ] 41 | 42 | 43 | class D3D11_TEXTURE2D_DESC(ctypes.Structure): 44 | _fields_ = [ 45 | ("Width", wintypes.UINT), 46 | ("Height", wintypes.UINT), 47 | ("MipLevels", wintypes.UINT), 48 | ("ArraySize", wintypes.UINT), 49 | ("Format", wintypes.UINT), 50 | ("SampleDesc", DXGI_SAMPLE_DESC), 51 | ("Usage", wintypes.UINT), 52 | ("BindFlags", wintypes.UINT), 53 | ("CPUAccessFlags", wintypes.UINT), 54 | ("MiscFlags", wintypes.UINT), 55 | ] 56 | 57 | 58 | class ID3D11DeviceChild(comtypes.IUnknown): 59 | _iid_ = comtypes.GUID("{1841e5c8-16b0-489b-bcc8-44cfb0d5deae}") 60 | _methods_ = [ 61 | comtypes.STDMETHOD(None, "GetDevice"), 62 | comtypes.STDMETHOD(comtypes.HRESULT, "GetPrivateData"), 63 | comtypes.STDMETHOD(comtypes.HRESULT, "SetPrivateData"), 64 | comtypes.STDMETHOD(comtypes.HRESULT, "SetPrivateDataInterface"), 65 | ] 66 | 67 | 68 | class ID3D11Resource(ID3D11DeviceChild): 69 | _iid_ = comtypes.GUID("{dc8e63f3-d12b-4952-b47b-5e45026a862d}") 70 | _methods_ = [ 71 | comtypes.STDMETHOD(None, "GetType"), 72 | comtypes.STDMETHOD(None, "SetEvictionPriority"), 73 | comtypes.STDMETHOD(wintypes.UINT, "GetEvictionPriority"), 74 | ] 75 | 76 | 77 | class ID3D11Texture2D(ID3D11Resource): 78 | _iid_ = comtypes.GUID("{6f15aaf2-d208-4e89-9ab4-489535d34f9c}") 79 | _methods_ = [ 80 | comtypes.STDMETHOD(None, "GetDesc", [ctypes.POINTER(D3D11_TEXTURE2D_DESC)]), 81 | ] 82 | 83 | 84 | class ID3D11DeviceContext(ID3D11DeviceChild): 85 | _iid_ = comtypes.GUID("{c0bfa96c-e089-44fb-8eaf-26f8796190da}") 86 | _methods_ = [ 87 | comtypes.STDMETHOD(None, "VSSetConstantBuffers"), 88 | comtypes.STDMETHOD(None, "PSSetShaderResources"), 89 | comtypes.STDMETHOD(None, "PSSetShader"), 90 | comtypes.STDMETHOD(None, "PSSetSamplers"), 91 | comtypes.STDMETHOD(None, "VSSetShader"), 92 | comtypes.STDMETHOD(None, "DrawIndexed"), 93 | comtypes.STDMETHOD(None, "Draw"), 94 | comtypes.STDMETHOD(comtypes.HRESULT, "Map"), 95 | comtypes.STDMETHOD(None, "Unmap"), 96 | comtypes.STDMETHOD(None, "PSSetConstantBuffers"), 97 | comtypes.STDMETHOD(None, "IASetInputLayout"), 98 | comtypes.STDMETHOD(None, "IASetVertexBuffers"), 99 | comtypes.STDMETHOD(None, "IASetIndexBuffer"), 100 | comtypes.STDMETHOD(None, "DrawIndexedInstanced"), 101 | comtypes.STDMETHOD(None, "DrawInstanced"), 102 | comtypes.STDMETHOD(None, "GSSetConstantBuffers"), 103 | comtypes.STDMETHOD(None, "GSSetShader"), 104 | comtypes.STDMETHOD(None, "IASetPrimitiveTopology"), 105 | comtypes.STDMETHOD(None, "VSSetShaderResources"), 106 | comtypes.STDMETHOD(None, "VSSetSamplers"), 107 | comtypes.STDMETHOD(None, "Begin"), 108 | comtypes.STDMETHOD(None, "End"), 109 | comtypes.STDMETHOD(comtypes.HRESULT, "GetData"), 110 | comtypes.STDMETHOD(None, "SetPredication"), 111 | comtypes.STDMETHOD(None, "GSSetShaderResources"), 112 | comtypes.STDMETHOD(None, "GSSetSamplers"), 113 | comtypes.STDMETHOD(None, "OMSetRenderTargets"), 114 | comtypes.STDMETHOD(None, "OMSetRenderTargetsAndUnorderedAccessViews"), 115 | comtypes.STDMETHOD(None, "OMSetBlendState"), 116 | comtypes.STDMETHOD(None, "OMSetDepthStencilState"), 117 | comtypes.STDMETHOD(None, "SOSetTargets"), 118 | comtypes.STDMETHOD(None, "DrawAuto"), 119 | comtypes.STDMETHOD(None, "DrawIndexedInstancedIndirect"), 120 | comtypes.STDMETHOD(None, "DrawInstancedIndirect"), 121 | comtypes.STDMETHOD(None, "Dispatch"), 122 | comtypes.STDMETHOD(None, "DispatchIndirect"), 123 | comtypes.STDMETHOD(None, "RSSetState"), 124 | comtypes.STDMETHOD(None, "RSSetViewports"), 125 | comtypes.STDMETHOD(None, "RSSetScissorRects"), 126 | comtypes.STDMETHOD( 127 | None, 128 | "CopySubresourceRegion", 129 | [ 130 | ctypes.POINTER(ID3D11Resource), 131 | wintypes.UINT, 132 | wintypes.UINT, 133 | wintypes.UINT, 134 | wintypes.UINT, 135 | ctypes.POINTER(ID3D11Resource), 136 | wintypes.UINT, 137 | ctypes.POINTER(D3D11_BOX), 138 | ], 139 | ), 140 | comtypes.STDMETHOD( 141 | None, 142 | "CopyResource", 143 | [ctypes.POINTER(ID3D11Resource), ctypes.POINTER(ID3D11Resource)], 144 | ), 145 | comtypes.STDMETHOD(None, "UpdateSubresource"), 146 | comtypes.STDMETHOD(None, "CopyStructureCount"), 147 | comtypes.STDMETHOD(None, "ClearRenderTargetView"), 148 | comtypes.STDMETHOD(None, "ClearUnorderedAccessViewUint"), 149 | comtypes.STDMETHOD(None, "ClearUnorderedAccessViewFloat"), 150 | comtypes.STDMETHOD(None, "ClearDepthStencilView"), 151 | comtypes.STDMETHOD(None, "GenerateMips"), 152 | comtypes.STDMETHOD(None, "SetResourceMinLOD"), 153 | comtypes.STDMETHOD(wintypes.FLOAT, "GetResourceMinLOD"), 154 | comtypes.STDMETHOD(None, "ResolveSubresource"), 155 | comtypes.STDMETHOD(None, "ExecuteCommandList"), 156 | comtypes.STDMETHOD(None, "HSSetShaderResources"), 157 | comtypes.STDMETHOD(None, "HSSetShader"), 158 | comtypes.STDMETHOD(None, "HSSetSamplers"), 159 | comtypes.STDMETHOD(None, "HSSetConstantBuffers"), 160 | comtypes.STDMETHOD(None, "DSSetShaderResources"), 161 | comtypes.STDMETHOD(None, "DSSetShader"), 162 | comtypes.STDMETHOD(None, "DSSetSamplers"), 163 | comtypes.STDMETHOD(None, "DSSetConstantBuffers"), 164 | comtypes.STDMETHOD(None, "CSSetShaderResources"), 165 | comtypes.STDMETHOD(None, "CSSetUnorderedAccessViews"), 166 | comtypes.STDMETHOD(None, "CSSetShader"), 167 | comtypes.STDMETHOD(None, "CSSetSamplers"), 168 | comtypes.STDMETHOD(None, "CSSetConstantBuffers"), 169 | comtypes.STDMETHOD(None, "VSGetConstantBuffers"), 170 | comtypes.STDMETHOD(None, "PSGetShaderResources"), 171 | comtypes.STDMETHOD(None, "PSGetShader"), 172 | comtypes.STDMETHOD(None, "PSGetSamplers"), 173 | comtypes.STDMETHOD(None, "VSGetShader"), 174 | comtypes.STDMETHOD(None, "PSGetConstantBuffers"), 175 | comtypes.STDMETHOD(None, "IAGetInputLayout"), 176 | comtypes.STDMETHOD(None, "IAGetVertexBuffers"), 177 | comtypes.STDMETHOD(None, "IAGetIndexBuffer"), 178 | comtypes.STDMETHOD(None, "GSGetConstantBuffers"), 179 | comtypes.STDMETHOD(None, "GSGetShader"), 180 | comtypes.STDMETHOD(None, "IAGetPrimitiveTopology"), 181 | comtypes.STDMETHOD(None, "VSGetShaderResources"), 182 | comtypes.STDMETHOD(None, "VSGetSamplers"), 183 | comtypes.STDMETHOD(None, "GetPredication"), 184 | comtypes.STDMETHOD(None, "GSGetShaderResources"), 185 | comtypes.STDMETHOD(None, "GSGetSamplers"), 186 | comtypes.STDMETHOD(None, "OMGetRenderTargets"), 187 | comtypes.STDMETHOD(None, "OMGetRenderTargetsAndUnorderedAccessViews"), 188 | comtypes.STDMETHOD(None, "OMGetBlendState"), 189 | comtypes.STDMETHOD(None, "OMGetDepthStencilState"), 190 | comtypes.STDMETHOD(None, "SOGetTargets"), 191 | comtypes.STDMETHOD(None, "RSGetState"), 192 | comtypes.STDMETHOD(None, "RSGetViewports"), 193 | comtypes.STDMETHOD(None, "RSGetScissorRects"), 194 | comtypes.STDMETHOD(None, "HSGetShaderResources"), 195 | comtypes.STDMETHOD(None, "HSGetShader"), 196 | comtypes.STDMETHOD(None, "HSGetSamplers"), 197 | comtypes.STDMETHOD(None, "HSGetConstantBuffers"), 198 | comtypes.STDMETHOD(None, "DSGetShaderResources"), 199 | comtypes.STDMETHOD(None, "DSGetShader"), 200 | comtypes.STDMETHOD(None, "DSGetSamplers"), 201 | comtypes.STDMETHOD(None, "DSGetConstantBuffers"), 202 | comtypes.STDMETHOD(None, "CSGetShaderResources"), 203 | comtypes.STDMETHOD(None, "CSGetUnorderedAccessViews"), 204 | comtypes.STDMETHOD(None, "CSGetShader"), 205 | comtypes.STDMETHOD(None, "CSGetSamplers"), 206 | comtypes.STDMETHOD(None, "CSGetConstantBuffers"), 207 | comtypes.STDMETHOD(None, "ClearState"), 208 | comtypes.STDMETHOD(None, "Flush"), 209 | comtypes.STDMETHOD(None, "GetType"), 210 | comtypes.STDMETHOD(wintypes.UINT, "GetContextFlags"), 211 | comtypes.STDMETHOD(comtypes.HRESULT, "FinishCommandList"), 212 | ] 213 | 214 | 215 | class ID3D11Device(comtypes.IUnknown): 216 | _iid_ = comtypes.GUID("{db6f6ddb-ac77-4e88-8253-819df9bbf140}") 217 | _methods_ = [ 218 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateBuffer"), 219 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateTexture1D"), 220 | comtypes.STDMETHOD( 221 | comtypes.HRESULT, 222 | "CreateTexture2D", 223 | [ 224 | ctypes.POINTER(D3D11_TEXTURE2D_DESC), 225 | ctypes.POINTER(None), 226 | ctypes.POINTER(ctypes.POINTER(ID3D11Texture2D)), 227 | ], 228 | ), 229 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateTexture3D"), 230 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateShaderResourceView"), 231 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateUnorderedAccessView"), 232 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateRenderTargetView"), 233 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateDepthStencilView"), 234 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateInputLayout"), 235 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateVertexShader"), 236 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateGeometryShader"), 237 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateGeometryShaderWithStreamOutput"), 238 | comtypes.STDMETHOD(comtypes.HRESULT, "CreatePixelShader"), 239 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateHullShader"), 240 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateDomainShader"), 241 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateComputeShader"), 242 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateClassLinkage"), 243 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateBlendState"), 244 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateDepthStencilState"), 245 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateRasterizerState"), 246 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateSamplerState"), 247 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateQuery"), 248 | comtypes.STDMETHOD(comtypes.HRESULT, "CreatePredicate"), 249 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateCounter"), 250 | comtypes.STDMETHOD(comtypes.HRESULT, "CreateDeferredContext"), 251 | comtypes.STDMETHOD(comtypes.HRESULT, "OpenSharedResource"), 252 | comtypes.STDMETHOD(comtypes.HRESULT, "CheckFormatSupport"), 253 | comtypes.STDMETHOD(comtypes.HRESULT, "CheckMultisampleQualityLevels"), 254 | comtypes.STDMETHOD(comtypes.HRESULT, "CheckCounterInfo"), 255 | comtypes.STDMETHOD(comtypes.HRESULT, "CheckCounter"), 256 | comtypes.STDMETHOD(comtypes.HRESULT, "CheckFeatureSupport"), 257 | comtypes.STDMETHOD(comtypes.HRESULT, "GetPrivateData"), 258 | comtypes.STDMETHOD(comtypes.HRESULT, "SetPrivateData"), 259 | comtypes.STDMETHOD(comtypes.HRESULT, "SetPrivateDataInterface"), 260 | comtypes.STDMETHOD(ctypes.c_int32, "GetFeatureLevel"), 261 | comtypes.STDMETHOD(ctypes.c_uint, "GetCreationFlags"), 262 | comtypes.STDMETHOD(comtypes.HRESULT, "GetDeviceRemovedReason"), 263 | comtypes.STDMETHOD( 264 | None, 265 | "GetImmediateContext", 266 | [ctypes.POINTER(ctypes.POINTER(ID3D11DeviceContext))], 267 | ), 268 | comtypes.STDMETHOD(comtypes.HRESULT, "SetExceptionMode"), 269 | comtypes.STDMETHOD(ctypes.c_uint, "GetExceptionMode"), 270 | ] 271 | -------------------------------------------------------------------------------- /dxcam/dxcam.py: -------------------------------------------------------------------------------- 1 | import time 2 | import ctypes 3 | from typing import Tuple 4 | from threading import Thread, Event, Lock 5 | import comtypes 6 | import numpy as np 7 | from dxcam.core import Device, Output, StageSurface, Duplicator 8 | from dxcam._libs.d3d11 import D3D11_BOX 9 | from dxcam.processor import Processor 10 | from dxcam.util.timer import ( 11 | create_high_resolution_timer, 12 | set_periodic_timer, 13 | wait_for_timer, 14 | cancel_timer, 15 | INFINITE, 16 | WAIT_FAILED, 17 | ) 18 | 19 | 20 | class DXCamera: 21 | def __init__( 22 | self, 23 | output: Output, 24 | device: Device, 25 | region: Tuple[int, int, int, int], 26 | output_color: str = "RGB", 27 | max_buffer_len=64, 28 | ) -> None: 29 | self._output: Output = output 30 | self._device: Device = device 31 | self._stagesurf: StageSurface = StageSurface( 32 | output=self._output, device=self._device 33 | ) 34 | self._duplicator: Duplicator = Duplicator( 35 | output=self._output, device=self._device 36 | ) 37 | self._processor: Processor = Processor(output_color=output_color) 38 | self._sourceRegion: D3D11_BOX = D3D11_BOX() 39 | self._sourceRegion.front = 0 40 | self._sourceRegion.back = 1 41 | 42 | self.width, self.height = self._output.resolution 43 | self.shot_w, self.shot_h = self.width, self.height 44 | self.channel_size = len(output_color) if output_color != "GRAY" else 1 45 | self.rotation_angle: int = self._output.rotation_angle 46 | 47 | self._region_set_by_user = region is not None 48 | self.region: Tuple[int, int, int, int] = region 49 | if self.region is None: 50 | self.region = (0, 0, self.width, self.height) 51 | self._validate_region(self.region) 52 | 53 | self.max_buffer_len = max_buffer_len 54 | self.is_capturing = False 55 | 56 | self.__thread = None 57 | self.__lock = Lock() 58 | self.__stop_capture = Event() 59 | 60 | self.__frame_available = Event() 61 | self.__frame_buffer: np.ndarray = None 62 | self.__head = 0 63 | self.__tail = 0 64 | self.__full = False 65 | 66 | self.__timer_handle = None 67 | 68 | self.__frame_count = 0 69 | self.__capture_start_time = 0 70 | 71 | # from https://github.com/Agade09/DXcam 72 | def region_to_memory_region(self, region: Tuple[int, int, int, int], rotation_angle: int, output: Output): 73 | if rotation_angle==0: 74 | return region 75 | elif rotation_angle==90: #Axes (X,Y) -> (-Y,X) 76 | return (region[1], output.surface_size[1]-region[2], region[3], output.surface_size[1]-region[0]) 77 | elif rotation_angle==180: #Axes (X,Y) -> (-X,-Y) 78 | return (output.surface_size[0]-region[2], output.surface_size[1]-region[3], output.surface_size[0]-region[0], output.surface_size[1]-region[1]) 79 | else: #rotation_angle==270 Axes (X,Y) -> (Y,-X) 80 | return (output.surface_size[0]-region[3], region[0], output.surface_size[0]-region[1], region[2]) 81 | 82 | def grab(self, region: Tuple[int, int, int, int] = None): 83 | if region is None: 84 | region = self.region 85 | 86 | if not self.region==region: 87 | self._validate_region(region) 88 | 89 | return self._grab(region) 90 | 91 | def grab_cursor(self): 92 | return self._duplicator.cursor 93 | 94 | 95 | def shot(self, image_ptr, region: Tuple[int, int, int, int] = None): 96 | if region is None: 97 | region = self.region 98 | 99 | if not self.region==region: 100 | self._validate_region(region) 101 | 102 | return self._shot(image_ptr, region) 103 | 104 | def _shot(self, image_ptr, region: Tuple[int, int, int, int]): 105 | if self._duplicator.update_frame(): 106 | if not self._duplicator.updated: 107 | return None 108 | 109 | _region = self.region_to_memory_region(region, self.rotation_angle, self._output) 110 | _width = _region[2] - _region[0] 111 | _height = _region[3] - _region[1] 112 | 113 | if self._stagesurf.width != _width or self._stagesurf.height != _height: 114 | self._stagesurf.release() 115 | self._stagesurf.rebuild(output=self._output, device=self._device, dim=(_width, _height)) 116 | 117 | self._device.im_context.CopySubresourceRegion( 118 | self._stagesurf.texture, 0, 0, 0, 0, self._duplicator.texture, 0, ctypes.byref(self._sourceRegion) 119 | ) 120 | self._duplicator.release_frame() 121 | rect = self._stagesurf.map() 122 | self._processor.process2(image_ptr, rect, self.shot_w, self.shot_h) 123 | self._stagesurf.unmap() 124 | return True 125 | else: 126 | self._on_output_change() 127 | return False 128 | 129 | def _grab(self, region: Tuple[int, int, int, int]): 130 | if self._duplicator.update_frame(): 131 | if not self._duplicator.updated: 132 | return None 133 | 134 | _region = self.region_to_memory_region(region, self.rotation_angle, self._output) 135 | _width = _region[2] - _region[0] 136 | _height = _region[3] - _region[1] 137 | 138 | if self._stagesurf.width != _width or self._stagesurf.height != _height: 139 | self._stagesurf.release() 140 | self._stagesurf.rebuild(output=self._output, device=self._device, dim=(_width, _height)) 141 | 142 | self._device.im_context.CopySubresourceRegion( 143 | self._stagesurf.texture, 0, 0, 0, 0, self._duplicator.texture, 0, ctypes.byref(self._sourceRegion) 144 | ) 145 | self._duplicator.release_frame() 146 | rect = self._stagesurf.map() 147 | frame = self._processor.process( 148 | rect, self.shot_w, self.shot_h, region, self.rotation_angle 149 | ) 150 | self._stagesurf.unmap() 151 | return frame 152 | else: 153 | self._on_output_change() 154 | return None 155 | 156 | def _on_output_change(self): 157 | time.sleep(0.1) # Wait for Display mode change (Access Lost) 158 | self._duplicator.release() 159 | self._stagesurf.release() 160 | self._output.update_desc() 161 | self.width, self.height = self._output.resolution 162 | if self.region is None or not self._region_set_by_user: 163 | self.region = (0, 0, self.width, self.height) 164 | self._validate_region(self.region) 165 | if self.is_capturing: 166 | self._rebuild_frame_buffer(self.region) 167 | self.rotation_angle = self._output.rotation_angle 168 | while True: 169 | try: 170 | self._stagesurf.rebuild(output=self._output, device=self._device) 171 | self._duplicator = Duplicator(output=self._output, device=self._device) 172 | except comtypes.COMError as ce: 173 | continue 174 | break 175 | 176 | def start( 177 | self, 178 | region: Tuple[int, int, int, int] = None, 179 | target_fps: int = 60, 180 | video_mode=False, 181 | delay: int = 0, 182 | ): 183 | if delay != 0: 184 | time.sleep(delay) 185 | self._on_output_change() 186 | if region is None: 187 | region = self.region 188 | self._validate_region(region) 189 | self.is_capturing = True 190 | frame_shape = (region[3] - region[1], region[2] - region[0], self.channel_size) 191 | self.__frame_buffer = np.ndarray( 192 | (self.max_buffer_len, *frame_shape), dtype=np.uint8 193 | ) 194 | self.__thread = Thread( 195 | target=self.__capture, 196 | name="DXCamera", 197 | args=(region, target_fps, video_mode), 198 | ) 199 | self.__thread.daemon = True 200 | self.__thread.start() 201 | 202 | def stop(self): 203 | if self.is_capturing: 204 | self.__frame_available.set() 205 | self.__stop_capture.set() 206 | if self.__thread is not None: 207 | self.__thread.join(timeout=10) 208 | self.is_capturing = False 209 | self.__frame_buffer = None 210 | self.__frame_count = 0 211 | self.__frame_available.clear() 212 | self.__stop_capture.clear() 213 | 214 | def get_latest_frame(self): 215 | self.__frame_available.wait() 216 | with self.__lock: 217 | ret = self.__frame_buffer[(self.__head - 1) % self.max_buffer_len] 218 | self.__frame_available.clear() 219 | return np.array(ret) 220 | 221 | def __capture( 222 | self, region: Tuple[int, int, int, int], target_fps: int = 60, video_mode=False 223 | ): 224 | if target_fps != 0: 225 | period_ms = 1000 // target_fps # millisenonds for periodic timer 226 | self.__timer_handle = create_high_resolution_timer() 227 | set_periodic_timer(self.__timer_handle, period_ms) 228 | 229 | self.__capture_start_time = time.perf_counter() 230 | 231 | capture_error = None 232 | 233 | while not self.__stop_capture.is_set(): 234 | if self.__timer_handle: 235 | res = wait_for_timer(self.__timer_handle, INFINITE) 236 | if res == WAIT_FAILED: 237 | self.__stop_capture.set() 238 | capture_error = ctypes.WinError() 239 | continue 240 | try: 241 | frame = self._grab(region) 242 | if frame is not None: 243 | with self.__lock: 244 | # Reconstruct frame buffer when resolution change, Author is elmoiv (https://github.com/elmoiv) 245 | if frame.shape[0] != self.height or frame.shape[1] != self.width: 246 | self.width, self.height = frame.shape[1], frame.shape[0] 247 | region = (0, 0, frame.shape[1], frame.shape[0]) 248 | frame_shape = (region[3] - region[1], region[2] - region[0], self.channel_size) 249 | self.__frame_buffer = np.ndarray( 250 | (self.max_buffer_len, *frame_shape), dtype=np.uint8 251 | ) 252 | self.__frame_buffer[self.__head] = frame 253 | if self.__full: 254 | self.__tail = (self.__tail + 1) % self.max_buffer_len 255 | self.__head = (self.__head + 1) % self.max_buffer_len 256 | self.__frame_available.set() 257 | self.__frame_count += 1 258 | self.__full = self.__head == self.__tail 259 | elif video_mode: 260 | with self.__lock: 261 | self.__frame_buffer[self.__head] = np.array( 262 | self.__frame_buffer[(self.__head - 1) % self.max_buffer_len] 263 | ) 264 | if self.__full: 265 | self.__tail = (self.__tail + 1) % self.max_buffer_len 266 | self.__head = (self.__head + 1) % self.max_buffer_len 267 | self.__frame_available.set() 268 | self.__frame_count += 1 269 | self.__full = self.__head == self.__tail 270 | except Exception as e: 271 | import traceback 272 | 273 | print(traceback.format_exc()) 274 | self.__stop_capture.set() 275 | capture_error = e 276 | continue 277 | if self.__timer_handle: 278 | cancel_timer(self.__timer_handle) 279 | self.__timer_handle = None 280 | if capture_error is not None: 281 | self.stop() 282 | raise capture_error 283 | print( 284 | f"Screen Capture FPS: {int(self.__frame_count/(time.perf_counter() - self.__capture_start_time))}" 285 | ) 286 | 287 | def _rebuild_frame_buffer(self, region: Tuple[int, int, int, int]): 288 | if region is None: 289 | region = self.region 290 | frame_shape = ( 291 | region[3] - region[1], 292 | region[2] - region[0], 293 | self.channel_size, 294 | ) 295 | with self.__lock: 296 | self.__frame_buffer = np.ndarray( 297 | (self.max_buffer_len, *frame_shape), dtype=np.uint8 298 | ) 299 | self.__head = 0 300 | self.__tail = 0 301 | self.__full = False 302 | 303 | def _validate_region(self, region: Tuple[int, int, int, int]): 304 | l, t, r, b = region 305 | if not (self.width >= r > l >= 0 and self.height >= b > t >= 0): 306 | raise ValueError( 307 | f"Invalid Region: Region should be in {self.width}x{self.height}" 308 | ) 309 | self.region = region 310 | self._sourceRegion.left = region[0] 311 | self._sourceRegion.top = region[1] 312 | self._sourceRegion.right = region[2] 313 | self._sourceRegion.bottom = region[3] 314 | self.shot_w, self.shot_h = region[2]-region[0], region[3]-region[1] 315 | 316 | def release(self): 317 | self.stop() 318 | self._duplicator.release() 319 | self._stagesurf.release() 320 | 321 | def __del__(self): 322 | self.release() 323 | 324 | def __repr__(self) -> str: 325 | return "<{}:\n\t{},\n\t{},\n\t{},\n\t{}\n>".format( 326 | self.__class__.__name__, 327 | self._device, 328 | self._output, 329 | self._stagesurf, 330 | self._duplicator, 331 | ) 332 | --------------------------------------------------------------------------------