Skip to content

Commit

Permalink
Merge pull request #5295 from andydotxyz/fix/1977
Browse files Browse the repository at this point in the history
Fix/1977
  • Loading branch information
andydotxyz authored Dec 4, 2024
2 parents b251cfe + a6e37c5 commit 2cccd08
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 16 deletions.
4 changes: 1 addition & 3 deletions internal/painter/gl/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,8 @@ func (p *painter) drawTextureWithDetails(o fyne.CanvasObject, creator func(canva
p.defineVertexArray(p.program, "vert", 3, 5, 0)
p.defineVertexArray(p.program, "vertTexCoord", 2, 5, 3)

// here we have to choose between blending the image alpha or fading it...
// TODO find a way to support both
if alpha != 1.0 {
p.ctx.BlendColor(0, 0, 0, alpha)
p.ctx.BlendColor(alpha, alpha, alpha, alpha)
p.ctx.BlendFunc(constantAlpha, oneMinusConstantAlpha)
} else {
p.ctx.BlendFunc(one, oneMinusSrcAlpha)
Expand Down
2 changes: 1 addition & 1 deletion internal/painter/gl/shaders.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion internal/painter/gl/shaders/simple.frag
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@ uniform sampler2D tex;
varying vec2 fragTexCoord;

void main() {
gl_FragColor = texture2D(tex, fragTexCoord);
vec4 texColor = texture2D(tex, fragTexCoord);
if(texColor.a < 0.01)
discard;
gl_FragColor = texColor;
}
24 changes: 15 additions & 9 deletions internal/painter/software/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package software
import (
"fmt"
"image"
"image/color"
"math"

"fyne.io/fyne/v2"
Expand Down Expand Up @@ -46,7 +47,7 @@ func drawGradient(c fyne.Canvas, g gradient, pos fyne.Position, base *image.NRGB
width := scale.ToScreenCoordinate(c, bounds.Width)
height := scale.ToScreenCoordinate(c, bounds.Height)
tex := g.Generate(width, height)
drawTex(scale.ToScreenCoordinate(c, pos.X), scale.ToScreenCoordinate(c, pos.Y), width, height, base, tex, clip)
drawTex(scale.ToScreenCoordinate(c, pos.X), scale.ToScreenCoordinate(c, pos.Y), width, height, base, tex, clip, 1.0)
}

func drawImage(c fyne.Canvas, img *canvas.Image, pos fyne.Position, base *image.NRGBA, clip image.Rectangle) {
Expand Down Expand Up @@ -75,13 +76,13 @@ func drawImage(c fyne.Canvas, img *canvas.Image, pos fyne.Position, base *image.
}
}

drawPixels(scaledX, scaledY, width, height, img.ScaleMode, base, origImg, clip)
drawPixels(scaledX, scaledY, width, height, img.ScaleMode, base, origImg, clip, img.Alpha())
}

func drawPixels(x, y, width, height int, mode canvas.ImageScale, base *image.NRGBA, origImg image.Image, clip image.Rectangle) {
func drawPixels(x, y, width, height int, mode canvas.ImageScale, base *image.NRGBA, origImg image.Image, clip image.Rectangle, alpha float64) {
if origImg.Bounds().Dx() == width && origImg.Bounds().Dy() == height {
// do not scale or duplicate image since not needed, draw directly
drawTex(x, y, width, height, base, origImg, clip)
drawTex(x, y, width, height, base, origImg, clip, alpha)
return
}

Expand All @@ -99,7 +100,7 @@ func drawPixels(x, y, width, height int, mode canvas.ImageScale, base *image.NRG
draw.CatmullRom.Scale(scaledImg, scaledBounds, origImg, origImg.Bounds(), draw.Over, nil)
}

drawTex(x, y, width, height, base, scaledImg, clip)
drawTex(x, y, width, height, base, scaledImg, clip, alpha)
}

func drawLine(c fyne.Canvas, line *canvas.Line, pos fyne.Position, base *image.NRGBA, clip image.Rectangle) {
Expand All @@ -124,11 +125,16 @@ func drawLine(c fyne.Canvas, line *canvas.Line, pos fyne.Position, base *image.N
draw.Draw(base, bounds, raw, image.Point{offX, offY}, draw.Over)
}

func drawTex(x, y, width, height int, base *image.NRGBA, tex image.Image, clip image.Rectangle) {
func drawTex(x, y, width, height int, base *image.NRGBA, tex image.Image, clip image.Rectangle, alpha float64) {
outBounds := image.Rect(x, y, x+width, y+height)
clippedBounds := clip.Intersect(outBounds)
srcPt := image.Point{X: clippedBounds.Min.X - outBounds.Min.X, Y: clippedBounds.Min.Y - outBounds.Min.Y}
draw.Draw(base, clippedBounds, tex, srcPt, draw.Over)
if alpha == 1.0 {
draw.Draw(base, clippedBounds, tex, srcPt, draw.Over)
} else {
mask := &image.Uniform{C: color.NRGBA{R: 0xff, G: 0xff, B: 0xff, A: uint8(float64(0xff) * alpha)}}
draw.DrawMask(base, clippedBounds, tex, srcPt, mask, srcPt, draw.Over)
}
}

func drawText(c fyne.Canvas, text *canvas.Text, pos fyne.Position, base *image.NRGBA, clip image.Rectangle) {
Expand Down Expand Up @@ -176,9 +182,9 @@ func drawRaster(c fyne.Canvas, rast *canvas.Raster, pos fyne.Position, base *ima

pix := rast.Generator(width, height)
if pix.Bounds().Bounds().Dx() != width || pix.Bounds().Dy() != height {
drawPixels(scaledX, scaledY, width, height, rast.ScaleMode, base, pix, clip)
drawPixels(scaledX, scaledY, width, height, rast.ScaleMode, base, pix, clip, 1.0)
} else {
drawTex(scaledX, scaledY, width, height, base, pix, clip)
drawTex(scaledX, scaledY, width, height, base, pix, clip, 1.0)
}
}

Expand Down
14 changes: 14 additions & 0 deletions internal/painter/software/painter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ func TestPainter_paintImage(t *testing.T) {
test.AssertImageMatches(t, "draw_image_default.png", target)
}

func TestPainter_paintImageAlpha(t *testing.T) {
img := canvas.NewImageFromImage(makeTestImage(3, 3))
img.Translucency = 0.5

c := test.NewCanvas()
c.SetPadded(false)
c.SetContent(img)
c.Resize(fyne.NewSize(50, 50))
p := software.NewPainter()

target := p.Paint(c)
test.AssertImageMatches(t, "draw_image_alpha.png", target)
}

func TestPainter_paintImage_clipped(t *testing.T) {
test.ApplyTheme(t, test.Theme())
img := canvas.NewImageFromImage(makeTestImage(5, 5))
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions test/markup_renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ func Test_snapshot(t *testing.T) {
"image with translucency": {
content: func() fyne.CanvasObject {
img := fynecanvas.NewImageFromResource(theme.ZoomOutIcon())
img.Translucency = 1.3
img.Translucency = 0.3
return img
}(),
want: "<canvas size=\"100x100\">\n" +
"\t<content>\n" +
"\t\t<image rsc=\"zoomOutIcon\" size=\"100x100\" themed=\"foreground\" translucency=\"1.3\"/>\n" +
"\t\t<image rsc=\"zoomOutIcon\" size=\"100x100\" themed=\"foreground\" translucency=\"0.3\"/>\n" +
"\t</content>\n" +
"</canvas>\n",
},
Expand Down

0 comments on commit 2cccd08

Please sign in to comment.