/
DXGI.go
90 lines (79 loc) · 1.78 KB
/
DXGI.go
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
package screenshot
import (
"fmt"
"github.com/ghp3000/screenshot/d3d"
"image"
"sync/atomic"
)
type DXGIScreenshot struct {
rect image.Rectangle
device *d3d.ID3D11Device
deviceCtx *d3d.ID3D11DeviceContext
ddup *d3d.OutputDuplicator
display int //截取哪个屏幕
cursor int32
}
func NewDXGIScreenshot() *DXGIScreenshot {
return &DXGIScreenshot{}
}
func (s *DXGIScreenshot) Init(display int) error {
if display < 0 {
return fmt.Errorf("display %d is invalid", display)
}
s.Release()
s.display = display
s.rect = GetDisplayBounds(display)
var err error
s.device, s.deviceCtx, err = d3d.NewD3D11Device()
if err != nil {
return err
}
s.ddup, err = d3d.NewIDXGIOutputDuplication(s.device, s.deviceCtx, uint(s.display))
if err != nil {
return err
}
return nil
}
func (s *DXGIScreenshot) Capture() (*image.RGBA, error) {
if s.ddup == nil {
return nil, fmt.Errorf("no init,please run Init first")
}
rect := GetDisplayBounds(s.display)
// 如果发现屏幕范围发生了变化就重新初始化
if rect != s.rect {
s.Release()
if err := s.Init(s.display); err != nil {
return nil, err
}
}
imgBuf := image.NewRGBA(s.rect)
err := s.ddup.GetImage(imgBuf, 0, atomic.LoadInt32(&s.cursor) == 1)
if err != nil {
return nil, err
}
return imgBuf, err
}
func (s *DXGIScreenshot) CaptureBGRA() (*image.RGBA, error) {
// todo
return s.Capture()
}
func (s *DXGIScreenshot) Release() {
if s.device != nil {
s.device.Release()
}
if s.deviceCtx != nil {
s.deviceCtx.Release()
}
if s.ddup != nil {
s.ddup.Release()
}
}
func (s *DXGIScreenshot) GetDisplayId() int {
return s.display
}
func (s *DXGIScreenshot) GetCaptureName() string {
return "DXGI"
}
func (s *DXGIScreenshot) DrawCursor(cursor int32) {
atomic.StoreInt32(&s.cursor, cursor)
}