From 65b24d6157cf853bf11fba7220208a685d38d0d6 Mon Sep 17 00:00:00 2001 From: Avasam Date: Wed, 9 Oct 2024 17:39:41 -0400 Subject: [PATCH 1/2] Migrate to typed-D3DShot (#302) --- docs/tutorial.md | 3 ++- scripts/build.ps1 | 3 ++- scripts/install.ps1 | 4 +--- scripts/requirements-dev.txt | 1 - scripts/requirements.txt | 2 +- src/capture_method/DesktopDuplicationCaptureMethod.py | 11 ++++------- 6 files changed, 10 insertions(+), 14 deletions(-) diff --git a/docs/tutorial.md b/docs/tutorial.md index c1722a51..5c40d99f 100644 --- a/docs/tutorial.md +++ b/docs/tutorial.md @@ -53,7 +53,8 @@ - **Direct3D Desktop Duplication** (slower, bound to display) Duplicates the desktop using Direct3D. It can record OpenGL and Hardware Accelerated windows. - About 10-15x slower than BitBlt. Not affected by window size. + Up to 15x slower than BitBlt for tiny regions. Not affected by window size. + Limited by the target window and monitor's refresh rate. Overlapping windows will show up and can't record across displays. This option may not be available for hybrid GPU laptops, see [D3DDD-Note-Laptops.md](/docs/D3DDD-Note-Laptops.md) for a solution. - **Force Full Content Rendering** (very slow, can affect rendering) diff --git a/scripts/build.ps1 b/scripts/build.ps1 index f6ed7bec..3d416c9f 100644 --- a/scripts/build.ps1 +++ b/scripts/build.ps1 @@ -17,8 +17,9 @@ $arguments = @( '--exclude=pytweening', '--exclude=mouseinfo') if ($IsWindows) { + # These are used on Linux $arguments += @( - # Installed by PyAutoGUI, but used by linux + # Installed by PyAutoGUI '--exclude=pyscreeze' # Installed by D3DShot '--exclude=PIL') diff --git a/scripts/install.ps1 b/scripts/install.ps1 index 9144f38e..f7bd1bcd 100644 --- a/scripts/install.ps1 +++ b/scripts/install.ps1 @@ -48,9 +48,7 @@ If ($IsLinux) { # These libraries install extra requirements we don't want # Open suggestion for support in requirements files: https://github.com/pypa/pip/issues/9948 & https://github.com/pypa/pip/pull/10837 # PyAutoGUI: We only use it for hotkeys -# D3DShot: Will install Pillow, which we don't use on Windows. -# Even then, PyPI with Pillow>=7.2.0 will install 0.1.3 instead of 0.1.5 -&"$python" -m pip install PyAutoGUI "D3DShot>=0.1.5 ; sys_platform == 'win32'" --no-deps --upgrade +&"$python" -m pip install PyAutoGUI --no-deps --upgrade # Uninstall optional dependencies if PyAutoGUI or D3DShot was installed outside this script # PyScreeze -> pyscreenshot -> mss deps call SetProcessDpiAwareness, used to be installed on Windows diff --git a/scripts/requirements-dev.txt b/scripts/requirements-dev.txt index 526ebb85..913108e0 100644 --- a/scripts/requirements-dev.txt +++ b/scripts/requirements-dev.txt @@ -15,7 +15,6 @@ ruff>=0.6.8 # Pre-commit fix # Must match .pre-commit-config.yaml # # Types -types-D3DShot ; sys_platform == 'win32' types-keyboard types-psutil types-PyAutoGUI diff --git a/scripts/requirements.txt b/scripts/requirements.txt index 3405f2b4..1897d5a6 100644 --- a/scripts/requirements.txt +++ b/scripts/requirements.txt @@ -36,7 +36,7 @@ winrt-Windows.Graphics.DirectX.Direct3D11>=2.2.0 ; sys_platform == 'win32' # Py winrt-Windows.Graphics.Imaging>=2.2.0 ; sys_platform == 'win32' # Python 3.13 support winrt-Windows.Graphics>=2.2.0 ; sys_platform == 'win32' # Python 3.13 support winrt-Windows.Media.Capture>=2.2.0 ; sys_platform == 'win32' # Python 3.13 support -# D3DShot # See install.ps1 +typed-D3DShot[numpy]>=1.0.1 ; sys_platform == 'win32' # # Linux-only dependencies PyScreeze ; sys_platform == 'linux' diff --git a/src/capture_method/DesktopDuplicationCaptureMethod.py b/src/capture_method/DesktopDuplicationCaptureMethod.py index 827f443c..e13ff11c 100644 --- a/src/capture_method/DesktopDuplicationCaptureMethod.py +++ b/src/capture_method/DesktopDuplicationCaptureMethod.py @@ -2,14 +2,13 @@ if sys.platform != "win32": raise OSError -from typing import TYPE_CHECKING, cast +from typing import TYPE_CHECKING import cv2 import d3dshot import win32api import win32con import win32gui -from cv2.typing import MatLike from typing_extensions import override from capture_method.BitBltCaptureMethod import BitBltCaptureMethod @@ -25,7 +24,8 @@ class DesktopDuplicationCaptureMethod(BitBltCaptureMethod): description = f""" Duplicates the desktop using Direct3D. It can record OpenGL and Hardware Accelerated windows. -About 10-15x slower than BitBlt. Not affected by window size. +Up to 15x slower than BitBlt for tiny regions. Not affected by window size. +Limited by the target window and monitor's refresh rate. Overlapping windows will show up and can't record across displays. This option may not be available for hybrid GPU laptops, see D3DDD-Note-Laptops.md for a solution. @@ -57,10 +57,7 @@ def get_frame(self): top = selection["y"] + offset_y + top_bounds right = selection["width"] + left bottom = selection["height"] + top - screenshot = cast( - MatLike | None, - self.desktop_duplication.screenshot((left, top, right, bottom)), - ) + screenshot = self.desktop_duplication.screenshot((left, top, right, bottom)) if screenshot is None: return None return cv2.cvtColor(screenshot, cv2.COLOR_RGB2BGRA) From bd6cf54fdd3569c769e5c9d4ef9aab64344fca84 Mon Sep 17 00:00:00 2001 From: Avasam Date: Wed, 9 Oct 2024 17:42:07 -0400 Subject: [PATCH 2/2] Bump pre commit hooks (#301) --- .pre-commit-config.yaml | 4 ++-- scripts/requirements-dev.txt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 10e7bbe9..e901070b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.6.0 + rev: v5.0.0 hooks: - id: pretty-format-json exclude: ".vscode/.*" # Exclude jsonc @@ -19,7 +19,7 @@ repos: - id: pretty-format-ini args: [--autofix] - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.6.8 # Must match requirements-dev.txt + rev: v0.6.9 # Must match requirements-dev.txt hooks: - id: ruff args: [--fix] diff --git a/scripts/requirements-dev.txt b/scripts/requirements-dev.txt index 913108e0..3f11be6e 100644 --- a/scripts/requirements-dev.txt +++ b/scripts/requirements-dev.txt @@ -12,7 +12,7 @@ -r requirements.txt # # Linters & Formatters -ruff>=0.6.8 # Pre-commit fix # Must match .pre-commit-config.yaml +ruff>=0.6.9 # Pre-commit fix # Must match .pre-commit-config.yaml # # Types types-keyboard