forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
texture.go
184 lines (152 loc) · 5.06 KB
/
texture.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package gl
import (
"fmt"
"github.com/gorustyt/fyne/v2/canvas3d/context"
"image"
"image/draw"
"math"
"github.com/gorustyt/fyne/v2"
"github.com/gorustyt/fyne/v2/canvas"
"github.com/gorustyt/fyne/v2/internal/cache"
paint "github.com/gorustyt/fyne/v2/internal/painter"
"github.com/gorustyt/fyne/v2/theme"
)
var noTexture = context.Texture(cache.NoTexture)
func (p *painter) freeTexture(obj fyne.CanvasObject) {
texture, ok := cache.GetTexture(obj)
if !ok {
return
}
p.ctx.DeleteTexture(context.Texture(texture))
p.logError()
cache.DeleteTexture(obj)
}
func (p *painter) getTexture(object fyne.CanvasObject, creator func(canvasObject fyne.CanvasObject) context.Texture) (context.Texture, error) {
texture, ok := cache.GetTexture(object)
if !ok {
texture = cache.TextureType(creator(object))
cache.SetTexture(object, texture, p.canvas)
}
if !cache.IsValid(texture) {
return noTexture, fmt.Errorf("no texture available")
}
return context.Texture(texture), nil
}
func (p *painter) imgToTexture(img image.Image, textureFilter canvas.ImageScale) context.Texture {
switch i := img.(type) {
case *image.Uniform:
texture := p.newTexture(textureFilter)
r, g, b, a := i.RGBA()
r8, g8, b8, a8 := uint8(r>>8), uint8(g>>8), uint8(b>>8), uint8(a>>8)
data := []uint8{r8, g8, b8, a8}
p.ctx.TexImage2D(
Texture2D,
0,
1,
1,
colorFormatRGBA,
unsignedByte,
data,
)
p.logError()
return texture
case *image.RGBA:
if len(i.Pix) == 0 { // image is empty
return noTexture
}
texture := p.newTexture(textureFilter)
p.ctx.TexImage2D(
Texture2D,
0,
i.Rect.Size().X,
i.Rect.Size().Y,
colorFormatRGBA,
unsignedByte,
i.Pix,
)
p.logError()
return texture
default:
rgba := image.NewRGBA(image.Rect(0, 0, img.Bounds().Dx(), img.Bounds().Dy()))
draw.Draw(rgba, rgba.Rect, img, image.Point{}, draw.Over)
return p.imgToTexture(rgba, textureFilter)
}
}
func (p *painter) newGlCircleTexture(obj fyne.CanvasObject) context.Texture {
circle := obj.(*canvas.Circle)
raw := paint.DrawCircle(circle, paint.VectorPad(circle), p.textureScale)
return p.imgToTexture(raw, canvas.ImageScaleSmooth)
}
func (p *painter) newGlImageTexture(obj fyne.CanvasObject) context.Texture {
img := obj.(*canvas.Image)
width := p.textureScale(img.Size().Width)
height := p.textureScale(img.Size().Height)
tex := paint.PaintImage(img, p.canvas, int(width), int(height))
if tex == nil {
return noTexture
}
return p.imgToTexture(tex, img.ScaleMode)
}
func (p *painter) newGlLinearGradientTexture(obj fyne.CanvasObject) context.Texture {
gradient := obj.(*canvas.LinearGradient)
w := gradient.Size().Width
h := gradient.Size().Height
switch gradient.Angle {
case 90, 270:
h = 1
case 0, 180:
w = 1
}
width := p.textureScale(w)
height := p.textureScale(h)
return p.imgToTexture(gradient.Generate(int(width), int(height)), canvas.ImageScaleSmooth)
}
func (p *painter) newGlRadialGradientTexture(obj fyne.CanvasObject) context.Texture {
gradient := obj.(*canvas.RadialGradient)
width := p.textureScale(gradient.Size().Width)
height := p.textureScale(gradient.Size().Height)
return p.imgToTexture(gradient.Generate(int(width), int(height)), canvas.ImageScaleSmooth)
}
func (p *painter) newGlRasterTexture(obj fyne.CanvasObject) context.Texture {
rast := obj.(*canvas.Raster)
width := p.textureScale(rast.Size().Width)
height := p.textureScale(rast.Size().Height)
return p.imgToTexture(rast.Generator(int(width), int(height)), rast.ScaleMode)
}
func (p *painter) newGlTextTexture(obj fyne.CanvasObject) context.Texture {
text := obj.(*canvas.Text)
color := text.Color
if color == nil {
color = theme.ForegroundColor()
}
bounds := text.MinSize()
width := int(math.Ceil(float64(p.textureScale(bounds.Width) + paint.VectorPad(text)))) // potentially italic overspill
height := int(math.Ceil(float64(p.textureScale(bounds.Height))))
img := image.NewNRGBA(image.Rect(0, 0, width, height))
face := paint.CachedFontFace(text.TextStyle, text.TextSize*p.canvas.Scale(), p.texScale)
paint.DrawString(img, text.Text, color, face.Fonts, text.TextSize, p.pixScale, text.TextStyle.TabWidth)
return p.imgToTexture(img, canvas.ImageScaleSmooth)
}
func (p *painter) newTexture(textureFilter canvas.ImageScale) context.Texture {
if int(textureFilter) >= len(textureFilterToGL) {
fyne.LogError(fmt.Sprintf("Invalid canvas.ImageScale value (%d), using canvas.ImageScaleSmooth as default value", textureFilter), nil)
textureFilter = canvas.ImageScaleSmooth
}
texture := p.ctx.CreateTexture()
p.logError()
p.ctx.ActiveTexture(texture0)
p.ctx.BindTexture(Texture2D, texture)
p.logError()
p.ctx.TexParameteri(Texture2D, TextureMinFilter, textureFilterToGL[textureFilter])
p.ctx.TexParameteri(Texture2D, TextureMagFilter, textureFilterToGL[textureFilter])
p.ctx.TexParameteri(Texture2D, textureWrapS, clampToEdge)
p.ctx.TexParameteri(Texture2D, textureWrapT, clampToEdge)
p.logError()
return texture
}
func (p *painter) textureScale(v float32) float32 {
if p.pixScale == 1.0 {
return float32(math.Round(float64(v)))
}
return float32(math.Round(float64(v * p.pixScale)))
}