forked from google/gxui
/
texture.go
103 lines (86 loc) · 1.94 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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gl
import (
"image"
"github.com/google/gxui/math"
"github.com/goxjs/gl"
)
type texture struct {
refCounted
image image.Image
pixelsPerDip float32
flipY bool
}
func newTexture(img image.Image, pixelsPerDip float32) *texture {
t := &texture{
image: img,
pixelsPerDip: pixelsPerDip,
}
t.init()
return t
}
// gxui.Texture compliance
func (t *texture) Image() image.Image {
return t.image
}
func (t *texture) Size() math.Size {
return t.SizePixels().ScaleS(1.0 / t.pixelsPerDip)
}
func (t *texture) SizePixels() math.Size {
s := t.image.Bounds().Size()
return math.Size{W: s.X, H: s.Y}
}
func (t *texture) FlipY() bool {
return t.flipY
}
func (t *texture) SetFlipY(flipY bool) {
t.flipY = flipY
}
func (t *texture) Release() {
t.release()
}
func (t *texture) newContext() *textureContext {
var fmt gl.Enum
var data []byte
var pma bool
switch ty := t.image.(type) {
case *image.RGBA:
fmt = gl.RGBA
data = ty.Pix
pma = true
case *image.NRGBA:
fmt = gl.RGBA
data = ty.Pix
case *image.Alpha:
fmt = gl.ALPHA
data = ty.Pix
default:
panic("Unsupported image type")
}
texture := gl.CreateTexture()
gl.BindTexture(gl.TEXTURE_2D, texture)
w, h := t.SizePixels().WH()
gl.TexImage2D(gl.TEXTURE_2D, 0, w, h, fmt, gl.UNSIGNED_BYTE, data)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.BindTexture(gl.TEXTURE_2D, gl.Texture{})
checkError()
return &textureContext{
texture: texture,
sizePixels: t.Size(),
flipY: t.flipY,
pma: pma,
}
}
type textureContext struct {
texture gl.Texture
sizePixels math.Size
flipY bool
pma bool
}
func (c *textureContext) destroy() {
gl.DeleteTexture(c.texture)
c.texture = gl.Texture{}
}