Skip to content

Commit

Permalink
Migrate to typed-D3DShot (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam authored Oct 9, 2024
1 parent aa63fff commit 65b24d6
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 14 deletions.
3 changes: 2 additions & 1 deletion docs/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion scripts/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
4 changes: 1 addition & 3 deletions scripts/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion scripts/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion scripts/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
11 changes: 4 additions & 7 deletions src/capture_method/DesktopDuplicationCaptureMethod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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)

0 comments on commit 65b24d6

Please sign in to comment.