better cursor capture

This commit is contained in:
KingKDot
2026-03-06 19:11:39 -06:00
parent c0580c559f
commit 1577493853
@@ -593,25 +593,26 @@ var (
) )
type duplicationState struct { type duplicationState struct {
mu sync.Mutex mu sync.Mutex
display int display int
outputName string outputName string
bounds image.Rectangle bounds image.Rectangle
cursorBounds image.Rectangle cursorBounds image.Rectangle
factory *idxgiFactory1 factory *idxgiFactory1
adapter *idxgiAdapter1 adapter *idxgiAdapter1
output *idxgiOutput output *idxgiOutput
output1 *idxgiOutput1 output1 *idxgiOutput1
dup *idxgiOutputDuplication dup *idxgiOutputDuplication
device *d3d11Device device *d3d11Device
context *d3d11DeviceContext context *d3d11DeviceContext
staging *d3d11Texture2D staging *d3d11Texture2D
stagingDesc d3d11Texture2DDesc stagingDesc d3d11Texture2DDesc
desc dxgiOutDuplDesc desc dxgiOutDuplDesc
lastBase *image.RGBA lastBase *image.RGBA
lastFrame *image.RGBA lastFrame *image.RGBA
lastFrameAt time.Time lastFrameAt time.Time
lastFail time.Time lastFail time.Time
cursorScratch *image.RGBA
} }
func SetDesktopDuplication(enabled bool) { func SetDesktopDuplication(enabled bool) {
@@ -673,6 +674,7 @@ func (s *duplicationState) closeLocked() {
s.lastBase = nil s.lastBase = nil
s.lastFrame = nil s.lastFrame = nil
s.lastFrameAt = time.Time{} s.lastFrameAt = time.Time{}
s.cursorScratch = nil
} }
func (s *duplicationState) capture(display int) (*image.RGBA, error) { func (s *duplicationState) capture(display int) (*image.RGBA, error) {
@@ -767,7 +769,8 @@ func (s *duplicationState) composeFrame(base *image.RGBA) *image.RGBA {
img := base img := base
if withCursor { if withCursor {
img = cloneRGBA(base) img = s.ensureCursorScratch(base.Rect)
copy(img.Pix, base.Pix)
} }
bounds := s.bounds bounds := s.bounds
if bounds.Dx() != int(s.desc.ModeDesc.Width) || bounds.Dy() != int(s.desc.ModeDesc.Height) { if bounds.Dx() != int(s.desc.ModeDesc.Width) || bounds.Dy() != int(s.desc.ModeDesc.Height) {
@@ -787,6 +790,13 @@ func (s *duplicationState) composeFrame(base *image.RGBA) *image.RGBA {
return img return img
} }
func (s *duplicationState) ensureCursorScratch(rect image.Rectangle) *image.RGBA {
if s.cursorScratch == nil || s.cursorScratch.Rect != rect {
s.cursorScratch = image.NewRGBA(rect)
}
return s.cursorScratch
}
func cloneRGBA(src *image.RGBA) *image.RGBA { func cloneRGBA(src *image.RGBA) *image.RGBA {
if src == nil { if src == nil {
return nil return nil