Skip to content

Commit

Permalink
Cache some sizes to local variables in internal/painter
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz authored and andydotxyz committed Dec 18, 2023
1 parent 14d7247 commit f83e221
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
23 changes: 13 additions & 10 deletions internal/painter/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,22 @@ func DrawCircle(circle *canvas.Circle, vectorPad float32, scale func(float32) fl
// The scale function is used to understand how many pixels are required per unit of size.
func DrawLine(line *canvas.Line, vectorPad float32, scale func(float32) float32) *image.RGBA {
col := line.StrokeColor
width := int(scale(line.Size().Width + vectorPad*2))
height := int(scale(line.Size().Height + vectorPad*2))
size := line.Size()
width := int(scale(size.Width + vectorPad*2))
height := int(scale(size.Height + vectorPad*2))
stroke := scale(line.StrokeWidth)
if stroke < 1 { // software painter doesn't fade lines to compensate
stroke = 1
}

raw := image.NewRGBA(image.Rect(0, 0, width, height))
scanner := rasterx.NewScannerGV(int(line.Size().Width), int(line.Size().Height), raw, raw.Bounds())
scanner := rasterx.NewScannerGV(int(size.Width), int(size.Height), raw, raw.Bounds())
dasher := rasterx.NewDasher(width, height, scanner)
dasher.SetColor(col)
dasher.SetStroke(fixed.Int26_6(float64(stroke)*64), 0, nil, nil, nil, 0, nil, 0)
p1x, p1y := scale(line.Position1.X-line.Position().X+vectorPad), scale(line.Position1.Y-line.Position().Y+vectorPad)
p2x, p2y := scale(line.Position2.X-line.Position().X+vectorPad), scale(line.Position2.Y-line.Position().Y+vectorPad)
positon := line.Position()
p1x, p1y := scale(line.Position1.X-positon.X+vectorPad), scale(line.Position1.Y-positon.Y+vectorPad)
p2x, p2y := scale(line.Position2.X-positon.X+vectorPad), scale(line.Position2.Y-positon.Y+vectorPad)

if stroke <= 1.5 { // adjust to support 1px
if p1x == p2x {
Expand All @@ -84,17 +86,18 @@ func DrawLine(line *canvas.Line, vectorPad float32, scale func(float32) float32)
// The bounds of the output image will be increased by vectorPad to allow for stroke overflow at the edges.
// The scale function is used to understand how many pixels are required per unit of size.
func DrawRectangle(rect *canvas.Rectangle, vectorPad float32, scale func(float32) float32) *image.RGBA {
width := int(scale(rect.Size().Width + vectorPad*2))
height := int(scale(rect.Size().Height + vectorPad*2))
size := rect.Size()
width := int(scale(size.Width + vectorPad*2))
height := int(scale(size.Height + vectorPad*2))
stroke := scale(rect.StrokeWidth)

raw := image.NewRGBA(image.Rect(0, 0, width, height))
scanner := rasterx.NewScannerGV(int(rect.Size().Width), int(rect.Size().Height), raw, raw.Bounds())
scanner := rasterx.NewScannerGV(int(size.Width), int(size.Height), raw, raw.Bounds())

scaledPad := scale(vectorPad)
p1x, p1y := scaledPad, scaledPad
p2x, p2y := scale(rect.Size().Width)+scaledPad, scaledPad
p3x, p3y := scale(rect.Size().Width)+scaledPad, scale(rect.Size().Height)+scaledPad
p2x, p2y := scale(size.Width)+scaledPad, scaledPad
p3x, p3y := scale(size.Width)+scaledPad, scale(size.Height)+scaledPad
p4x, p4y := scaledPad, scale(rect.Size().Height)+scaledPad

if rect.FillColor != nil {
Expand Down
9 changes: 5 additions & 4 deletions internal/painter/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,19 @@ func scaleImage(pixels image.Image, scaledW, scaledH int, scale canvas.ImageScal
return pixels
}

pixW := int(fyne.Min(float32(scaledW), float32(pixels.Bounds().Dx()))) // don't push more pixels than we have to
pixH := int(fyne.Min(float32(scaledH), float32(pixels.Bounds().Dy()))) // the GL calls will scale this up on GPU.
bounds := pixels.Bounds()
pixW := int(fyne.Min(float32(scaledW), float32(bounds.Dx()))) // don't push more pixels than we have to
pixH := int(fyne.Min(float32(scaledH), float32(bounds.Dy()))) // the GL calls will scale this up on GPU.
scaledBounds := image.Rect(0, 0, pixW, pixH)
tex := image.NewNRGBA(scaledBounds)
switch scale {
case canvas.ImageScalePixels:
draw.NearestNeighbor.Scale(tex, scaledBounds, pixels, pixels.Bounds(), draw.Over, nil)
draw.NearestNeighbor.Scale(tex, scaledBounds, pixels, bounds, draw.Over, nil)
default:
if scale != canvas.ImageScaleSmooth {
fyne.LogError("Invalid canvas.ImageScale value, using canvas.ImageScaleSmooth", nil)
}
draw.CatmullRom.Scale(tex, scaledBounds, pixels, pixels.Bounds(), draw.Over, nil)
draw.CatmullRom.Scale(tex, scaledBounds, pixels, bounds, draw.Over, nil)
}
return tex
}

0 comments on commit f83e221

Please sign in to comment.