Loading...
Searching...
No Matches
WindowCaptureBackend.hpp
1#pragma once
2#include <algorithm>
3#include <cstdint>
4#include <memory>
5#include <string>
6#include <string_view>
7#include <vector>
8
9namespace Gfx::WindowCapture
10{
11
12enum class CaptureMode : int
13{
14 Window = 0,
15 AllScreens = 1,
16 SingleScreen = 2,
17 Region = 3
18};
19
21{
22 enum Type
23 {
24 None,
25 CPU_RGBA,
26 CPU_BGRA,
27 D3D11_Texture,
28 IOSurface_Ref,
29 DMA_BUF_FD
30 };
31 Type type{None};
32
33 // CPU path
34 const uint8_t* data{};
35 int stride{};
36
37 // GPU path: D3D11 ID3D11Texture2D*, or IOSurfaceRef
38 void* nativeHandle{};
39
40 // PipeWire DMA-BUF path. When ownsDmabufFd is set, dmabufFd is a dup
41 // handed to the consumer, which must close it after import.
42 int dmabufFd{-1};
43 bool ownsDmabufFd{false};
44 uint32_t drmFormat{};
45 uint64_t drmModifier{};
46 int dmabufStride{};
47 int dmabufOffset{};
48
49 int width{};
50 int height{};
51};
52
54{
55 std::string title;
56 uint64_t id{};
57};
58
60{
61 std::string name;
62 uint64_t id{};
63 int x{}, y{}, width{}, height{};
64};
65
67{
68 CaptureMode mode{CaptureMode::Window};
69 uint64_t windowId{};
70 uint64_t screenId{};
71 int regionX{}, regionY{}, regionW{}, regionH{};
72};
73
83inline uint64_t resolveWindowId(
84 const std::vector<CapturableWindow>& windows, uint64_t id, std::string_view title)
85{
86 if(id != 0 && std::ranges::any_of(windows, [=](auto& w) { return w.id == id; }))
87 return id;
88 if(title.empty())
89 return id;
90 for(auto& w : windows)
91 if(w.title == title)
92 return w.id;
93 for(auto& w : windows)
94 if(w.title.find(title) != std::string::npos)
95 return w.id;
96 return id;
97}
98
100inline uint64_t resolveScreenId(
101 const std::vector<CapturableScreen>& screens, uint64_t id, std::string_view name)
102{
103 if(id != 0 && std::ranges::any_of(screens, [=](auto& s) { return s.id == id; }))
104 return id;
105 if(name.empty())
106 return id;
107 for(auto& s : screens)
108 if(s.name == name)
109 return s.id;
110 for(auto& s : screens)
111 if(s.name.find(name) != std::string::npos)
112 return s.id;
113 return id;
114}
115
117{
118 virtual ~WindowCaptureBackend() = default;
119 virtual bool available() const = 0;
120 virtual bool supportsMode(CaptureMode mode) const = 0;
121 virtual std::vector<CapturableWindow> enumerate() = 0;
122 virtual std::vector<CapturableScreen> enumerateScreens() = 0;
123 virtual bool start(const CaptureTarget& target) = 0;
124 virtual void stop() = 0;
125 virtual CapturedFrame grab() = 0;
126};
127
128// Returns the appropriate backend for the current platform and session type.
129std::unique_ptr<WindowCaptureBackend> createWindowCaptureBackend();
130
131}
STL namespace.
Definition WindowCaptureBackend.hpp:60
Definition WindowCaptureBackend.hpp:54
Definition WindowCaptureBackend.hpp:67
Definition WindowCaptureBackend.hpp:21
Definition WindowCaptureBackend.hpp:117