Skip to content

Commit 8d31197

Browse files
authored
Merge pull request #258 from go-flutter-desktop/select_opengl
Select opengl
2 parents de58aa4 + f3ccad5 commit 8d31197

File tree

6 files changed

+147
-28
lines changed

6 files changed

+147
-28
lines changed

application.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/go-flutter-desktop/go-flutter/embedder"
1414
"github.com/go-flutter-desktop/go-flutter/internal/execpath"
15+
"github.com/go-flutter-desktop/go-flutter/internal/opengl"
1516
)
1617

1718
// Run executes a flutter application with the provided options.
@@ -99,10 +100,7 @@ func (a *Application) Run() error {
99100
return errors.Errorf("invalid window mode %T", a.config.windowMode)
100101
}
101102

102-
glfw.WindowHint(glfw.ContextVersionMajor, 4)
103-
glfw.WindowHint(glfw.ContextVersionMinor, 1)
104-
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
105-
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
103+
opengl.GLFWWindowHint()
106104

107105
if a.config.windowInitialLocation.xpos != 0 {
108106
// To create the window at a specific position, make it initially invisible

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
23
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
34
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 h1:SCYMcCJ89LjRGwEa0tRluNRiMjZHalQZrVrvTbPh+qw=
45
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk=
56
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1 h1:QbL/5oDUmRBzO9/Z7Seo6zf912W/a6Sr4Eu0G/3Jho0=
67
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
78
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
89
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
10+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
911
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1012
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
13+
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
1114
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
15+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1216
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
17+
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
1318
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

internal/opengl/doc.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Package opengl wraps the go-gl/gl OpenGL bindings with a compile-time OpenGL
2+
// version selector.
3+
//
4+
// This package allows clients to target multiple version of OpenGL when
5+
// building go-flutter.
6+
// default is v3.3
7+
//
8+
// Golang build constraints are used for version selection.
9+
package opengl

internal/opengl/opengl.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// +build !openglnone
2+
3+
package opengl
4+
5+
// The default version (3.3) of OpenGL used by go-flutter.
6+
// If you want to support other version, copy/pase this file, change the import
7+
// statement, add builds constraints and open a PR.
8+
9+
import (
10+
"unsafe"
11+
12+
"github.com/go-gl/gl/v3.3-core/gl"
13+
"github.com/go-gl/glfw/v3.2/glfw"
14+
)
15+
16+
// const exposed to go-flutter
17+
const (
18+
TEXTURE2D = gl.TEXTURE_2D
19+
RGBA8 = gl.RGBA8
20+
)
21+
22+
// Init opengl
23+
func Init() error {
24+
return gl.Init()
25+
}
26+
27+
// DeleteTextures deletes named textures
28+
func DeleteTextures(n int32, textures *uint32) {
29+
gl.DeleteTextures(n, textures)
30+
}
31+
32+
// CreateTexture creates a texture for go-flutter uses
33+
func CreateTexture(texture *uint32) {
34+
gl.GenTextures(1, texture)
35+
gl.BindTexture(gl.TEXTURE_2D, *texture)
36+
// set the texture wrapping parameters
37+
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_BORDER)
38+
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_BORDER)
39+
// set texture filtering parameters
40+
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
41+
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
42+
}
43+
44+
// BindTexture binds a named texture to a texturing target
45+
func BindTexture(texture uint32) {
46+
gl.BindTexture(gl.TEXTURE_2D, texture)
47+
}
48+
49+
// Ptr takes a slice or pointer (to a singular scalar value or the first
50+
// element of an array or slice) and returns its GL-compatible address.
51+
func Ptr(data interface{}) unsafe.Pointer {
52+
return gl.Ptr(data)
53+
}
54+
55+
// TexImage2D specifies a two-dimensional texture image
56+
func TexImage2D(width, height int32, pixels unsafe.Pointer) {
57+
// It the current flutter/engine can only support RGBA texture.
58+
gl.TexImage2D(
59+
gl.TEXTURE_2D,
60+
0,
61+
gl.RGBA,
62+
width,
63+
height,
64+
0,
65+
gl.RGBA,
66+
gl.UNSIGNED_BYTE,
67+
pixels,
68+
)
69+
}
70+
71+
// GLFWWindowHint sets hints for the next call to CreateWindow.
72+
func GLFWWindowHint() {
73+
glfw.WindowHint(glfw.ContextVersionMajor, 3)
74+
glfw.WindowHint(glfw.ContextVersionMinor, 3)
75+
glfw.WindowHint(glfw.OpenGLProfile, glfw.OpenGLCoreProfile)
76+
glfw.WindowHint(glfw.OpenGLForwardCompatible, glfw.True)
77+
}

internal/opengl/opengl_none.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// +build openglnone
2+
3+
package opengl
4+
5+
// Don't link go-flutter against OpenGL, less-dependency at the cost of not
6+
// supporting external texture (eg: video_plugin)
7+
//
8+
// compile go-flutter with: hover build|run --opengl=none
9+
10+
import (
11+
"unsafe"
12+
)
13+
14+
// const exposed to go-flutter
15+
const (
16+
TEXTURE2D = 0
17+
RGBA8 = 0
18+
)
19+
20+
// Init opengl
21+
func Init() error { return nil }
22+
23+
// DeleteTextures deletes named textures
24+
func DeleteTextures(n int32, textures *uint32) {}
25+
26+
// CreateTexture creates a texture for go-flutter uses
27+
func CreateTexture(texture *uint32) {}
28+
29+
// BindTexture binds a named texture to a texturing target
30+
func BindTexture(texture uint32) {}
31+
32+
// Ptr takes a slice or pointer (to a singular scalar value or the first
33+
// element of an array or slice) and returns its GL-compatible address.
34+
func Ptr(data interface{}) unsafe.Pointer { return nil }
35+
36+
// TexImage2D specifies a two-dimensional texture image
37+
func TexImage2D(width, height int32, pixels unsafe.Pointer) {
38+
panic("go-flutter: go-flutter wasn't compiled with support for external texture plugin.")
39+
}
40+
41+
// GLFWWindowHint sets hints for the next call to CreateWindow.
42+
func GLFWWindowHint() {}

texture-registry.go

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"sync"
66

77
"github.com/go-flutter-desktop/go-flutter/embedder"
8-
"github.com/go-gl/gl/v4.6-core/gl"
8+
"github.com/go-flutter-desktop/go-flutter/internal/opengl"
99
"github.com/go-gl/glfw/v3.2/glfw"
1010
"github.com/pkg/errors"
1111
)
@@ -42,9 +42,9 @@ func newTextureRegistry(engine *embedder.FlutterEngine, window *glfw.Window) *Te
4242

4343
func (t *TextureRegistry) init() error {
4444
t.window.MakeContextCurrent()
45-
// Important! Call gl.Init only under the presence of an active OpenGL context,
45+
// Important! Call open.Init only under the presence of an active OpenGL context,
4646
// i.e., after MakeContextCurrent.
47-
if err := gl.Init(); err != nil {
47+
if err := opengl.Init(); err != nil {
4848
return errors.Wrap(err, "TextureRegistry gl init failed")
4949
}
5050
return nil
@@ -80,7 +80,7 @@ func (t *TextureRegistry) setTextureHandler(textureID int64, handler ExternalTex
8080
if handler == nil {
8181
texture := t.channels[textureID]
8282
if texture != nil {
83-
gl.DeleteTextures(1, &texture.texture)
83+
opengl.DeleteTextures(1, &texture.texture)
8484
}
8585
delete(t.channels, textureID)
8686
} else {
@@ -118,33 +118,21 @@ func (t *TextureRegistry) handleExternalTexture(textureID int64,
118118
t.window.MakeContextCurrent()
119119

120120
if registration.texture == 0 {
121-
gl.GenTextures(1, &registration.texture)
122-
gl.BindTexture(gl.TEXTURE_2D, registration.texture)
123-
// set the texture wrapping parameters
124-
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_BORDER)
125-
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_BORDER)
126-
// set texture filtering parameters
127-
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
128-
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
121+
opengl.CreateTexture(&registration.texture)
129122
}
130123

131-
gl.BindTexture(gl.TEXTURE_2D, registration.texture)
132-
// It seems that current flutter/engine can only support RGBA texture.
133-
gl.TexImage2D(
134-
gl.TEXTURE_2D,
135-
0,
136-
gl.RGBA,
124+
opengl.BindTexture(registration.texture)
125+
126+
opengl.TexImage2D(
137127
int32(pixelBuffer.Width),
138128
int32(pixelBuffer.Height),
139-
0,
140-
gl.RGBA,
141-
gl.UNSIGNED_BYTE,
142-
gl.Ptr(pixelBuffer.Pix))
129+
opengl.Ptr(pixelBuffer.Pix),
130+
)
143131

144132
return &embedder.FlutterOpenGLTexture{
145-
Target: gl.TEXTURE_2D,
133+
Target: opengl.TEXTURE2D,
146134
Name: registration.texture,
147-
Format: gl.RGBA8,
135+
Format: opengl.RGBA8,
148136
}
149137

150138
}

0 commit comments

Comments
 (0)