Skip to content

Commit 3f8aa3a

Browse files
committed
checkpoint: Manual collect of pixelBuffer.Pix
1 parent 4cec5ef commit 3f8aa3a

File tree

4 files changed

+44
-20
lines changed

4 files changed

+44
-20
lines changed

embedder/embedder.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,14 @@ const (
5555

5656
// FlutterOpenGLTexture corresponds to the C.FlutterOpenGLTexture struct.
5757
type FlutterOpenGLTexture struct {
58-
// Target texture of the active texture unit (example GL_TEXTURE_2D).
58+
// Target texture of the active texture unit (example GL_TEXTURE_2D)
5959
Target uint32
60-
// The name of the texture.
60+
// The name of the texture
6161
Name uint32
62-
// The texture format (example GL_RGBA8).
62+
// The texture format (example GL_RGBA8)
6363
Format uint32
64+
// Callback invoked to collect the texture
65+
Collect func()
6466
}
6567

6668
// FlutterEngine corresponds to the C.FlutterEngine with his associated callback's method.
@@ -81,7 +83,7 @@ type FlutterEngine struct {
8183
GLFboCallback func() int32
8284
GLMakeResourceCurrent func() bool
8385
GLProcResolver func(procName string) unsafe.Pointer
84-
GLExternalTextureFrameCallback func(textureID int64, width int, height int) (bool, *FlutterOpenGLTexture)
86+
GLExternalTextureFrameCallback func(textureID int64, width int, height int) *FlutterOpenGLTexture
8587

8688
// platform message callback function
8789
PlatfromMessage func(message *PlatformMessage)

embedder/embedder_proxy.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package embedder
22

33
// #include "embedder.h"
4+
// void proxy_texture_destruction_callback(void* user_data);
45
import "C"
56
import (
7+
"runtime"
68
"unsafe"
79
)
810

@@ -68,12 +70,28 @@ func proxy_gl_external_texture_frame_callback(userData unsafe.Pointer,
6870
texture *C.FlutterOpenGLTexture) C.bool {
6971
flutterEnginePointer := *(*uintptr)(userData)
7072
flutterEngine := (*FlutterEngine)(unsafe.Pointer(flutterEnginePointer))
71-
res, embedderGLTexture := flutterEngine.GLExternalTextureFrameCallback(textureID, int(width), int(height))
73+
embedderGLTexture := flutterEngine.GLExternalTextureFrameCallback(textureID, int(width), int(height))
7274
if embedderGLTexture == nil {
73-
return C.bool(res)
75+
return C.bool(false)
7476
}
7577
texture.target = C.uint32_t(embedderGLTexture.Target)
7678
texture.name = C.uint32_t(embedderGLTexture.Name)
7779
texture.format = C.uint32_t(embedderGLTexture.Format)
78-
return C.bool(res)
80+
collectPointer := (unsafe.Pointer(&embedderGLTexture.Collect))
81+
texture.user_data = collectPointer
82+
texture.destruction_callback = (C.VoidCallback)(C.proxy_texture_destruction_callback)
83+
defer func() {
84+
runtime.KeepAlive(collectPointer)
85+
}()
86+
return C.bool(true)
87+
}
88+
89+
//export proxy_texture_destruction_callback
90+
func proxy_texture_destruction_callback(userData unsafe.Pointer) {
91+
defer recover()
92+
destroyCB := (*func())(userData)
93+
if destroyCB != nil {
94+
destroyCB := *destroyCB
95+
destroyCB()
96+
}
7997
}

texture-registry.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ type TextureRegistry struct {
2121
texturesLock sync.Mutex
2222
}
2323

24+
type externalTextureHanlder struct {
25+
// handle is called when flutter needs the PixelBuffer
26+
handle ExternalTextureHanlderFunc
27+
// gl texture to refer to for this handler
28+
texture uint32
29+
}
30+
2431
func newRegistry(engine *embedder.FlutterEngine, window *glfw.Window) *TextureRegistry {
2532
return &TextureRegistry{
2633
window: window,
@@ -59,13 +66,6 @@ type PixelBuffer struct {
5966
Width, Height int
6067
}
6168

62-
type externalTextureHanlder struct {
63-
// handle is called when flutter needs the PixelBuffer
64-
handle ExternalTextureHanlderFunc
65-
// gl texture to refer to for this handler
66-
texture uint32
67-
}
68-
6969
// setTextureHandler registers a handler to be invoked when the Flutter
7070
// application want to get a PixelBuffer to draw into the scene.
7171
//
@@ -84,23 +84,23 @@ func (t *TextureRegistry) setTextureHandler(textureID int64, handler ExternalTex
8484
}
8585

8686
func (t *TextureRegistry) handleExternalTexture(textureID int64,
87-
width int, height int) (bool, *embedder.FlutterOpenGLTexture) {
87+
width int, height int) *embedder.FlutterOpenGLTexture {
8888

8989
t.channelsLock.RLock()
9090
registration, registrationExists := t.channels[textureID]
9191
t.channelsLock.RUnlock()
9292

9393
if !registrationExists {
9494
fmt.Printf("go-flutter: no texture handler found for Texture ID: %v\n", textureID)
95-
return false, nil
95+
return nil
9696
}
9797
res, pixelBuffer := registration.handle(width, height)
9898
if !res || pixelBuffer == nil {
99-
return false, nil
99+
return nil
100100
}
101101

102102
if len(pixelBuffer.Pix) == 0 {
103-
return false, nil
103+
return nil
104104
}
105105

106106
t.window.MakeContextCurrent()
@@ -131,10 +131,14 @@ func (t *TextureRegistry) handleExternalTexture(textureID int64,
131131
gl.UNSIGNED_BYTE,
132132
gl.Ptr(pixelBuffer.Pix))
133133

134-
return true, &embedder.FlutterOpenGLTexture{
134+
return &embedder.FlutterOpenGLTexture{
135135
Target: gl.TEXTURE_2D,
136136
Name: registration.texture,
137137
Format: gl.RGBA8,
138+
Collect: func() {
139+
// runtime delete the pixelBuffer.Pix, (should be done by the GC)
140+
// gl.DeleteTextures(1, &registration.texture) // not this, this is for destroy not collect!!
141+
},
138142
}
139143

140144
}

texture.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type Texture struct {
1313
registry *TextureRegistry
1414
}
1515

16-
// Register registers a textureID
16+
// Register registers a textureID with his associated handler
1717
func (t *Texture) Register(handler ExternalTextureHanlderFunc) error {
1818
t.registry.setTextureHandler(t.ID, handler)
1919
result := t.registry.engine.RegisterExternalTexture(t.ID)

0 commit comments

Comments
 (0)