final fixes on resolution and fix build issue on mac and linux
This commit is contained in:
@@ -35,6 +35,8 @@ func Loop(ctx context.Context, env *rt.Env) {
|
||||
done := make(chan struct{})
|
||||
goSafe("capture initial frame", env.Cancel, func() {
|
||||
defer close(done)
|
||||
restore := BypassResolutionCap()
|
||||
defer restore()
|
||||
var err error
|
||||
if goruntime.GOOS == "windows" && activeDisplays() > 1 {
|
||||
err = captureAllDisplaysAndSend(ctx, env)
|
||||
@@ -197,12 +199,53 @@ func captureAllDisplaysAndSend(ctx context.Context, env *rt.Env) error {
|
||||
}
|
||||
}
|
||||
|
||||
canvas := image.NewRGBA(image.Rect(0, 0, maxX-minX, maxY-minY))
|
||||
for _, part := range parts {
|
||||
offX := part.bounds.Min.X - minX
|
||||
offY := part.bounds.Min.Y - minY
|
||||
dst := image.Rect(offX, offY, offX+part.img.Rect.Dx(), offY+part.img.Rect.Dy())
|
||||
draw.Draw(canvas, dst, part.img, part.img.Rect.Min, draw.Src)
|
||||
// Compute per-monitor scale factors to handle resolution-cap or DPI
|
||||
// mismatches between displayBounds (virtual coords) and captured pixels.
|
||||
type scaledPart struct {
|
||||
img *image.RGBA
|
||||
scaleX float64
|
||||
scaleY float64
|
||||
offX int
|
||||
offY int
|
||||
}
|
||||
scaled := make([]scaledPart, len(parts))
|
||||
for i, part := range parts {
|
||||
sx, sy := 1.0, 1.0
|
||||
bw := part.bounds.Dx()
|
||||
bh := part.bounds.Dy()
|
||||
iw := part.img.Rect.Dx()
|
||||
ih := part.img.Rect.Dy()
|
||||
if bw > 0 && iw > 0 {
|
||||
sx = float64(iw) / float64(bw)
|
||||
}
|
||||
if bh > 0 && ih > 0 {
|
||||
sy = float64(ih) / float64(bh)
|
||||
}
|
||||
scaled[i] = scaledPart{
|
||||
img: part.img,
|
||||
scaleX: sx,
|
||||
scaleY: sy,
|
||||
offX: int(float64(part.bounds.Min.X-minX) * sx),
|
||||
offY: int(float64(part.bounds.Min.Y-minY) * sy),
|
||||
}
|
||||
}
|
||||
|
||||
// Canvas sized from scaled image placements.
|
||||
cW, cH := 0, 0
|
||||
for _, sp := range scaled {
|
||||
rx := sp.offX + sp.img.Rect.Dx()
|
||||
ry := sp.offY + sp.img.Rect.Dy()
|
||||
if rx > cW {
|
||||
cW = rx
|
||||
}
|
||||
if ry > cH {
|
||||
cH = ry
|
||||
}
|
||||
}
|
||||
canvas := image.NewRGBA(image.Rect(0, 0, cW, cH))
|
||||
for _, sp := range scaled {
|
||||
dst := image.Rect(sp.offX, sp.offY, sp.offX+sp.img.Rect.Dx(), sp.offY+sp.img.Rect.Dy())
|
||||
draw.Draw(canvas, dst, sp.img, sp.img.Rect.Min, draw.Src)
|
||||
}
|
||||
|
||||
quality := jpegQuality()
|
||||
|
||||
@@ -196,6 +196,10 @@ func drawCursor(img *image.RGBA, cursorX, cursorY int32, bounds image.Rectangle)
|
||||
}
|
||||
|
||||
func DrawCursorOnDC(hdc uintptr, captureBounds image.Rectangle) bool {
|
||||
return DrawCursorOnDCScaled(hdc, captureBounds, 1.0, 1.0)
|
||||
}
|
||||
|
||||
func DrawCursorOnDCScaled(hdc uintptr, captureBounds image.Rectangle, scaleX, scaleY float64) bool {
|
||||
if !cursorCaptureEnabled.Load() || hdc == 0 {
|
||||
return false
|
||||
}
|
||||
@@ -217,8 +221,9 @@ func DrawCursorOnDC(hdc uintptr, captureBounds image.Rectangle) bool {
|
||||
defer deleteObject(icon.hbmColor)
|
||||
}
|
||||
|
||||
x := int32(ci.ptScreenPos.x) - int32(captureBounds.Min.X) - int32(icon.xHotspot)
|
||||
y := int32(ci.ptScreenPos.y) - int32(captureBounds.Min.Y) - int32(icon.yHotspot)
|
||||
// Map from screen space into the (possibly scaled) DC space.
|
||||
x := int32(float64(ci.ptScreenPos.x-int32(captureBounds.Min.X))*scaleX) - int32(icon.xHotspot)
|
||||
y := int32(float64(ci.ptScreenPos.y-int32(captureBounds.Min.Y))*scaleY) - int32(icon.yHotspot)
|
||||
ret, _, _ = procDrawIconEx.Call(
|
||||
hdc,
|
||||
uintptr(x),
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
//go:build !windows
|
||||
|
||||
package capture
|
||||
|
||||
func SetMaxResolution(maxH int) {}
|
||||
|
||||
func BypassResolutionCap() (restore func()) {
|
||||
return func() {}
|
||||
}
|
||||
|
||||
func EffectiveScaleForInput() float64 {
|
||||
return 1.0
|
||||
}
|
||||
@@ -227,7 +227,7 @@ func captureDisplayBitBlt(display int) (*image.RGBA, error) {
|
||||
dstH = srcH
|
||||
}
|
||||
|
||||
useStretch := dstW > 0 && dstH > 0 && (dstW < srcW || dstH < srcH) && !cursorCaptureEnabled.Load()
|
||||
useStretch := dstW > 0 && dstH > 0 && (dstW < srcW || dstH < srcH)
|
||||
capW := srcW
|
||||
capH := srcH
|
||||
if useStretch {
|
||||
@@ -290,7 +290,11 @@ func captureDisplayBitBlt(display int) (*image.RGBA, error) {
|
||||
}
|
||||
bitDur := time.Since(bitStart)
|
||||
cursorDrawn := false
|
||||
if !useStretch {
|
||||
if useStretch {
|
||||
sx := float64(capW) / float64(srcW)
|
||||
sy := float64(capH) / float64(srcH)
|
||||
cursorDrawn = DrawCursorOnDCScaled(hdcMem, bounds, sx, sy)
|
||||
} else {
|
||||
cursorDrawn = DrawCursorOnDC(hdcMem, bounds)
|
||||
}
|
||||
|
||||
@@ -544,6 +548,11 @@ func SetMaxResolution(maxH int) {
|
||||
log.Printf("capture: max resolution set to %d", maxH)
|
||||
}
|
||||
|
||||
func BypassResolutionCap() (restore func()) {
|
||||
old := maxResHeight.Swap(-1)
|
||||
return func() { maxResHeight.Store(old) }
|
||||
}
|
||||
|
||||
func EffectiveScaleForInput() float64 {
|
||||
bits := lastCapScale.Load()
|
||||
if bits == 0 {
|
||||
|
||||
Reference in New Issue
Block a user