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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
use crate::error::Error;
use crate::{model::Result, utils::FrameInfoExt};
use std::ptr;
use windows::Win32::Graphics::Dxgi::DXGI_OUTDUPL_DESC;
use windows::Win32::Graphics::Gdi::{GetMonitorInfoW, MONITORINFO};
use windows::{
  core::ComInterface,
  Win32::Graphics::{
    Direct3D11::{
      ID3D11Device, ID3D11DeviceContext, ID3D11Texture2D, D3D11_BIND_FLAG, D3D11_CPU_ACCESS_READ,
      D3D11_RESOURCE_MISC_FLAG, D3D11_TEXTURE2D_DESC, D3D11_USAGE_STAGING,
    },
    Dxgi::{
      Common::{DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_SAMPLE_DESC},
      IDXGIOutput1, IDXGIOutputDuplication, IDXGIResource, IDXGISurface1, DXGI_MAPPED_RECT,
      DXGI_MAP_READ, DXGI_OUTDUPL_FRAME_INFO, DXGI_OUTDUPL_POINTER_SHAPE_INFO, DXGI_OUTPUT_DESC,
      DXGI_RESOURCE_PRIORITY_MAXIMUM,
    },
  },
};

/// Stateless.
pub struct DuplicationContext {
  device: ID3D11Device,
  device_context: ID3D11DeviceContext,
  timeout_ms: u32,
  output: IDXGIOutput1,
  output_duplication: IDXGIOutputDuplication,
}

impl DuplicationContext {
  pub fn new(
    device: ID3D11Device,
    device_context: ID3D11DeviceContext,
    output: IDXGIOutput1,
    output_duplication: IDXGIOutputDuplication,
    timeout_ms: u32,
  ) -> Self {
    Self {
      device,
      device_context,
      timeout_ms,
      output,
      output_duplication,
    }
  }

  pub fn monitor_info(&self) -> Result<MONITORINFO> {
    let h_monitor = self.dxgi_output_desc()?.Monitor;
    let mut info = MONITORINFO::default();
    info.cbSize = std::mem::size_of::<MONITORINFO>() as u32;
    if unsafe { GetMonitorInfoW(h_monitor, &mut info).as_bool() } {
      Ok(info)
    } else {
      Err(Error::new("GetMonitorInfoW"))
    }
  }

  /// This is usually used to get the screen's position and size.
  pub fn dxgi_output_desc(&self) -> Result<DXGI_OUTPUT_DESC> {
    let mut desc = DXGI_OUTPUT_DESC::default();
    unsafe { self.output.GetDesc(&mut desc) }
      .map_err(|e| Error::windows("DXGI_OUTPUT_DESC.GetDesc", e))?;
    Ok(desc)
  }

  /// This is usually used to get the screen's pixel width/height and buffer size.
  pub fn dxgi_outdupl_desc(&self) -> DXGI_OUTDUPL_DESC {
    let mut desc = DXGI_OUTDUPL_DESC::default();
    unsafe { self.output_duplication.GetDesc(&mut desc) };
    desc
  }

  pub fn create_readable_texture(
    &self,
  ) -> Result<(ID3D11Texture2D, DXGI_OUTDUPL_DESC, D3D11_TEXTURE2D_DESC)> {
    let dupl_desc = self.dxgi_outdupl_desc();
    let output_desc = self.dxgi_output_desc()?;

    // create a readable texture description
    let texture_desc = D3D11_TEXTURE2D_DESC {
      BindFlags: D3D11_BIND_FLAG::default(),
      CPUAccessFlags: D3D11_CPU_ACCESS_READ,
      MiscFlags: D3D11_RESOURCE_MISC_FLAG::default(),
      Usage: D3D11_USAGE_STAGING, // A resource that supports data transfer (copy) from the GPU to the CPU.
      Width: if output_desc.Rotation.0 == 2 || output_desc.Rotation.0 == 4 {
        dupl_desc.ModeDesc.Height
      } else {
        dupl_desc.ModeDesc.Width
      },
      Height: if output_desc.Rotation.0 == 2 || output_desc.Rotation.0 == 4 {
        dupl_desc.ModeDesc.Width
      } else {
        dupl_desc.ModeDesc.Height
      },
      MipLevels: 1,
      ArraySize: 1,
      Format: DXGI_FORMAT_B8G8R8A8_UNORM,
      SampleDesc: DXGI_SAMPLE_DESC {
        Count: 1,
        Quality: 0,
      },
    };

    // create a readable texture in GPU memory
    let mut readable_texture: Option<ID3D11Texture2D> = None.clone();
    unsafe {
      self
        .device
        .CreateTexture2D(&texture_desc, None, Some(&mut readable_texture))
    }
    .map_err(|e| Error::windows("CreateTexture2D", e))?;
    let readable_texture = readable_texture.unwrap();
    // Lower priorities causes stuff to be needlessly copied from gpu to ram,
    // causing huge ram usage on some systems.
    // https://github.com/bryal/dxgcap-rs/blob/208d93368bc64aed783791242410459c878a10fb/src/lib.rs#L225
    unsafe { readable_texture.SetEvictionPriority(DXGI_RESOURCE_PRIORITY_MAXIMUM.0) };

    Ok((readable_texture, dupl_desc, texture_desc))
  }

  fn acquire_next_frame(
    &self,
    readable_texture: &ID3D11Texture2D,
  ) -> Result<(IDXGISurface1, DXGI_OUTDUPL_FRAME_INFO)> {
    // acquire GPU texture
    let mut frame_info = DXGI_OUTDUPL_FRAME_INFO::default();
    let mut resource: Option<IDXGIResource> = None.clone();
    unsafe {
      self
        .output_duplication
        .AcquireNextFrame(self.timeout_ms, &mut frame_info, &mut resource)
    }
    .map_err(|e| Error::windows("AcquireNextFrame", e))?;
    let texture: ID3D11Texture2D = resource.unwrap().cast().unwrap();

    // copy GPU texture to readable texture
    unsafe { self.device_context.CopyResource(readable_texture, &texture) };

    Ok((readable_texture.cast().unwrap(), frame_info))
  }

  fn release_frame(&self) -> Result<()> {
    unsafe { self.output_duplication.ReleaseFrame() }.map_err(|e| Error::windows("ReleaseFrame", e))
  }

  pub fn next_frame(
    &self,
    readable_texture: &ID3D11Texture2D,
  ) -> Result<(IDXGISurface1, DXGI_OUTDUPL_FRAME_INFO)> {
    let (surface, frame_info) = self.acquire_next_frame(readable_texture)?;
    self.release_frame()?;
    Ok((surface, frame_info))
  }

  /// If mouse is updated, the `Option<DXGI_OUTDUPL_POINTER_SHAPE_INFO>` is `Some`.
  /// and this will resize `pointer_shape_buffer` if needed and update it.
  pub fn next_frame_with_pointer_shape(
    &self,
    readable_texture: &ID3D11Texture2D,
    pointer_shape_buffer: &mut Vec<u8>,
  ) -> Result<(
    IDXGISurface1,
    DXGI_OUTDUPL_FRAME_INFO,
    Option<DXGI_OUTDUPL_POINTER_SHAPE_INFO>,
  )> {
    let (surface, frame_info) = self.acquire_next_frame(readable_texture)?;

    if !frame_info.mouse_updated() {
      self.release_frame()?;
      return Ok((surface, frame_info, None));
    }

    // resize buffer if needed
    let pointer_shape_buffer_size = frame_info.PointerShapeBufferSize as usize;
    if pointer_shape_buffer.len() < pointer_shape_buffer_size {
      pointer_shape_buffer.resize(pointer_shape_buffer_size, 0);
    }

    // get pointer shape
    let mut size: u32 = 0;
    let mut pointer_shape_info = DXGI_OUTDUPL_POINTER_SHAPE_INFO::default();
    match unsafe {
      self
        .output_duplication
        .GetFramePointerShape(
          pointer_shape_buffer.len() as u32,
          pointer_shape_buffer.as_mut_ptr() as *mut _,
          &mut size,
          &mut pointer_shape_info,
        )
        .map_err(|e| Error::windows("GetFramePointerShape", e))
    } {
      Ok(_) => {
        self.release_frame()?;
        Ok((surface, frame_info, Some(pointer_shape_info)))
      }
      Err(e) => {
        self.release_frame()?;
        return Err(e);
      }
    }
  }

  pub fn capture(
    &self,
    dest: *mut u8,
    len: usize,
    readable_texture: &ID3D11Texture2D,
    texture_desc: &D3D11_TEXTURE2D_DESC,
  ) -> Result<DXGI_OUTDUPL_FRAME_INFO> {
    let (frame, frame_info) = self.next_frame(readable_texture)?;
    let mut mapped_surface = DXGI_MAPPED_RECT::default();
    let line_bytes = texture_desc.Width as usize * 4;

    unsafe {
      frame
        .Map(&mut mapped_surface, DXGI_MAP_READ)
        .map_err(|e| Error::windows("Map", e))?;
      if mapped_surface.Pitch as usize == line_bytes {
        ptr::copy_nonoverlapping(mapped_surface.pBits, dest, len);
      } else {
        // https://github.com/DiscreteTom/rusty-duplication/issues/7
        for i in 0..texture_desc.Height {
          let src = mapped_surface
            .pBits
            .offset((i * mapped_surface.Pitch as u32) as isize);
          let dest = dest.offset((i * line_bytes as u32) as isize);
          ptr::copy_nonoverlapping(src, dest, mapped_surface.Pitch as usize);
        }
      }
      frame.Unmap().map_err(|e| Error::windows("Unmap", e))?;
    }

    Ok(frame_info)
  }

  /// If mouse is updated, the `Option<DXGI_OUTDUPL_POINTER_SHAPE_INFO>` is `Some`.
  /// and this will resize `pointer_shape_buffer` if needed and update it.
  pub fn capture_with_pointer_shape(
    &self,
    dest: *mut u8,
    len: usize,
    readable_texture: &ID3D11Texture2D,
    texture_desc: &D3D11_TEXTURE2D_DESC,
    pointer_shape_buffer: &mut Vec<u8>,
  ) -> Result<(
    DXGI_OUTDUPL_FRAME_INFO,
    Option<DXGI_OUTDUPL_POINTER_SHAPE_INFO>,
  )> {
    let (frame, frame_info, pointer_shape_info) =
      self.next_frame_with_pointer_shape(readable_texture, pointer_shape_buffer)?;
    let mut mapped_surface = DXGI_MAPPED_RECT::default();
    let line_bytes = texture_desc.Width as usize * 4;

    unsafe {
      frame
        .Map(&mut mapped_surface, DXGI_MAP_READ)
        .map_err(|e| Error::windows("Map", e))?;
      if mapped_surface.Pitch as usize == line_bytes {
        ptr::copy_nonoverlapping(mapped_surface.pBits, dest, len);
      } else {
        // https://github.com/DiscreteTom/rusty-duplication/issues/7
        for i in 0..texture_desc.Height {
          let src = mapped_surface
            .pBits
            .offset((i * mapped_surface.Pitch as u32) as isize);
          let dest = dest.offset((i * line_bytes as u32) as isize);
          ptr::copy_nonoverlapping(src, dest, mapped_surface.Pitch as usize);
        }
      }
      frame.Unmap().map_err(|e| Error::windows("Unmap", e))?;
    }

    Ok((frame_info, pointer_shape_info))
  }
}

#[cfg(test)]
mod tests {
  use std::{thread, time::Duration};

  use crate::{
    manager::Manager,
    utils::{FrameInfoExt, MonitorInfoExt, OutDuplDescExt},
  };

  #[test]
  fn duplication_context() {
    let manager = Manager::default().unwrap();
    assert_ne!(manager.contexts.len(), 0);

    // make sure only one primary monitor
    let mut primary_monitor_count = 0;
    for c in &manager.contexts {
      if c.monitor_info().unwrap().is_primary() {
        primary_monitor_count += 1;
      }
    }
    assert_eq!(primary_monitor_count, 1);

    let (texture, desc, texture_desc) = manager.contexts[0].create_readable_texture().unwrap();
    let mut buffer = vec![0u8; desc.calc_buffer_size()];

    // sleep for a while before capture to wait system to update the screen
    thread::sleep(Duration::from_millis(100));

    let info = manager.contexts[0]
      .capture(buffer.as_mut_ptr(), buffer.len(), &texture, &texture_desc)
      .unwrap();
    assert!(info.desktop_updated());

    // ensure buffer not all zero
    let mut all_zero = true;
    for i in 0..buffer.len() {
      if buffer[i] != 0 {
        all_zero = false;
        break;
      }
    }
    assert!(!all_zero);

    // sleep for a while before capture to wait system to update the mouse
    thread::sleep(Duration::from_millis(1000));

    // check pointer
    let mut pointer_shape_buffer = vec![0u8; info.PointerShapeBufferSize as usize];
    let (frame_info, pointer_shape_info) = manager.contexts[0]
      .capture_with_pointer_shape(
        buffer.as_mut_ptr(),
        buffer.len(),
        &texture,
        &texture_desc,
        &mut pointer_shape_buffer,
      )
      .unwrap();
    assert!(frame_info.mouse_updated());
    assert!(pointer_shape_info.is_some());

    // ensure pointer_shape_buffer not all zero
    let mut all_zero = true;
    for i in 0..pointer_shape_buffer.len() {
      if pointer_shape_buffer[i] != 0 {
        all_zero = false;
        break;
      }
    }
    assert!(!all_zero);
  }
}