1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#![allow(unused)]
use std::mem;
use std::ops::{Deref, DerefMut};
use std::ptr::{null, null_mut};
use winapi::shared::dxgi::IDXGIDevice;
use winapi::shared::dxgi1_2::DXGI_ALPHA_MODE_IGNORE;
use winapi::shared::dxgiformat::DXGI_FORMAT_B8G8R8A8_UNORM;
use winapi::shared::minwindef::{FALSE, TRUE};
use winapi::shared::windef::{HWND, POINT, RECT};
use winapi::shared::winerror::SUCCEEDED;
use winapi::um::d2d1::*;
use winapi::um::d2d1_1::*;
use winapi::um::d3d11::*;
use winapi::um::d3dcommon::D3D_DRIVER_TYPE_HARDWARE;
use winapi::um::dcomp::*;
use winapi::um::dcompanimation::*;
use winapi::um::unknwnbase::IUnknown;
use winapi::um::winnt::HRESULT;
use winapi::Interface;
use wio::com::ComPtr;
use direct2d::math::Matrix3x2F;
use direct2d::{self, RenderTarget};
use log::error;
use crate::util::OPTIONAL_FUNCTIONS;
unsafe fn wrap<T, U, F>(hr: HRESULT, ptr: *mut T, f: F) -> Result<U, HRESULT>
where
F: Fn(ComPtr<T>) -> U,
T: Interface,
{
if SUCCEEDED(hr) {
Ok(f(ComPtr::from_raw(ptr)))
} else {
Err(hr)
}
}
fn unit_err(hr: HRESULT) -> Result<(), HRESULT> {
if SUCCEEDED(hr) {
Ok(())
} else {
Err(hr)
}
}
pub struct D3D11Device(ComPtr<ID3D11Device>);
pub struct D2D1Device(ComPtr<ID2D1Device>);
pub struct DCompositionDevice(ComPtr<IDCompositionDevice>);
pub struct DCompositionTarget(ComPtr<IDCompositionTarget>);
pub struct DCompositionVisual(ComPtr<IDCompositionVisual>);
pub struct DCompositionVirtualSurface(ComPtr<IDCompositionVirtualSurface>);
pub trait Content {
unsafe fn unknown_ptr(&mut self) -> *mut IUnknown;
}
impl D3D11Device {
pub fn new_simple() -> Result<D3D11Device, HRESULT> {
unsafe {
let mut d3d11_device: *mut ID3D11Device = null_mut();
let flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
let hr = D3D11CreateDevice(
null_mut(),
D3D_DRIVER_TYPE_HARDWARE,
null_mut(),
flags,
null(),
0,
D3D11_SDK_VERSION,
&mut d3d11_device,
null_mut(),
null_mut(),
);
if !SUCCEEDED(hr) {
error!("D3D11CreateDevice: 0x{:x}", hr);
}
wrap(hr, d3d11_device, D3D11Device)
}
}
pub fn create_d2d1_device(&mut self) -> Result<D2D1Device, HRESULT> {
unsafe {
let mut dxgi_device: ComPtr<IDXGIDevice> = self.0.cast()?;
let mut d2d1_device: *mut ID2D1Device = null_mut();
let hr = D2D1CreateDevice(dxgi_device.as_raw(), null(), &mut d2d1_device);
wrap(hr, d2d1_device, D2D1Device)
}
}
pub fn raw_ptr(&mut self) -> *mut ID3D11Device {
self.0.as_raw()
}
}
impl D2D1Device {
pub fn create_composition_device(&mut self) -> Result<DCompositionDevice, HRESULT> {
unsafe {
let create = OPTIONAL_FUNCTIONS.DCompositionCreateDevice2.ok_or(0)?;
let mut dcomp_device: *mut IDCompositionDevice = null_mut();
let hr = create(
self.0.as_raw() as *mut IUnknown,
&IDCompositionDevice::uuidof(),
&mut dcomp_device as *mut _ as *mut _,
);
wrap(hr, dcomp_device, DCompositionDevice)
}
}
}
impl DCompositionDevice {
pub unsafe fn create_target_for_hwnd(
&mut self,
hwnd: HWND,
topmost: bool,
) -> Result<DCompositionTarget, HRESULT> {
let mut dcomp_target: *mut IDCompositionTarget = null_mut();
let hr =
self.0
.CreateTargetForHwnd(hwnd, if topmost { TRUE } else { FALSE }, &mut dcomp_target);
wrap(hr, dcomp_target, DCompositionTarget)
}
pub fn create_visual(&mut self) -> Result<DCompositionVisual, HRESULT> {
unsafe {
let mut visual: *mut IDCompositionVisual = null_mut();
let hr = self.0.CreateVisual(&mut visual);
wrap(hr, visual, DCompositionVisual)
}
}
pub fn create_virtual_surface(
&mut self,
height: u32,
width: u32,
) -> Result<DCompositionVirtualSurface, HRESULT> {
unsafe {
let mut surface: *mut IDCompositionVirtualSurface = null_mut();
let hr = self.0.CreateVirtualSurface(
width,
height,
DXGI_FORMAT_B8G8R8A8_UNORM,
DXGI_ALPHA_MODE_IGNORE,
&mut surface,
);
wrap(hr, surface, DCompositionVirtualSurface)
}
}
pub fn commit(&mut self) -> Result<(), HRESULT> {
unsafe { unit_err(self.0.Commit()) }
}
}
impl DCompositionTarget {
pub fn clear_root(&mut self) -> Result<(), HRESULT> {
unsafe { unit_err(self.0.SetRoot(null_mut())) }
}
pub fn set_root(&mut self, visual: &mut DCompositionVisual) -> Result<(), HRESULT> {
unsafe { unit_err(self.0.SetRoot(visual.0.as_raw())) }
}
}
impl DCompositionVisual {
pub fn set_content<T: Content>(&mut self, content: &mut T) -> Result<(), HRESULT> {
unsafe { self.set_content_raw(content.unknown_ptr()) }
}
pub unsafe fn set_content_raw(&mut self, content: *mut IUnknown) -> Result<(), HRESULT> {
unit_err(self.0.SetContent(content))
}
pub fn set_pos(&mut self, x: f32, y: f32) {
unsafe {
self.0.SetOffsetX_1(x);
self.0.SetOffsetY_1(y);
}
}
}