Skip to content

Commit e494cb0

Browse files
committed
WIP external texture
1 parent a86a520 commit e494cb0

File tree

5 files changed

+127
-40
lines changed

5 files changed

+127
-40
lines changed

application.go

+17
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,13 @@ func (a *Application) Run() error {
199199
a.engine.GLProcResolver = func(procName string) unsafe.Pointer {
200200
return glfw.GetProcAddress(procName)
201201
}
202+
a.engine.GLExternalTextureFrameCallback = func(textureID int64,
203+
width int,
204+
height int,
205+
texture *embedder.FlutterOpenGLTexture) bool {
206+
fmt.Println("\nFlutterOpenGLTexture\n")
207+
return true
208+
}
202209

203210
a.engine.PlatfromMessage = messenger.handlePlatformMessage
204211

@@ -248,8 +255,18 @@ func (a *Application) Run() error {
248255
a.window.SetScrollCallback(m.glfwScrollCallback)
249256
defer a.engine.Shutdown()
250257

258+
frameI := int64(0)
251259
for !a.window.ShouldClose() {
260+
frameI++
252261
glfw.WaitEventsTimeout(0.016) // timeout to get 60fps-ish iterations
262+
if frameI == 400 {
263+
print("REGISTER: ")
264+
print(a.engine.RegisterExternalTexture(frameI))
265+
}
266+
if frameI == 600 {
267+
print("New Frame: ")
268+
print(a.engine.MarkExternalTextureFrameAvailable(frameI))
269+
}
253270
embedder.FlutterEngineFlushPendingTasksNow()
254271
defaultPlatformPlugin.glfwTasker.ExecuteTasks()
255272
messenger.engineTasker.ExecuteTasks()

embedder/embedder.go

+29-6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ const (
5252
ResultInvalidArguments Result = C.kInvalidArguments
5353
)
5454

55+
// FlutterOpenGLTexture corresponds to the C.FlutterOpenGLTexture struct.
56+
type FlutterOpenGLTexture C.FlutterOpenGLTexture
57+
5558
// FlutterEngine corresponds to the C.FlutterEngine with his associated callback's method.
5659
type FlutterEngine struct {
5760
// Flutter Engine.
@@ -61,12 +64,13 @@ type FlutterEngine struct {
6164
index int
6265

6366
// GL callback functions
64-
GLMakeCurrent func() bool
65-
GLClearCurrent func() bool
66-
GLPresent func() bool
67-
GLFboCallback func() int32
68-
GLMakeResourceCurrent func() bool
69-
GLProcResolver func(procName string) unsafe.Pointer
67+
GLMakeCurrent func() bool
68+
GLClearCurrent func() bool
69+
GLPresent func() bool
70+
GLFboCallback func() int32
71+
GLMakeResourceCurrent func() bool
72+
GLProcResolver func(procName string) unsafe.Pointer
73+
GLExternalTextureFrameCallback func(textureID int64, width int, height int, texture *FlutterOpenGLTexture) bool
7074

7175
// platform message callback function
7276
PlatfromMessage func(message *PlatformMessage)
@@ -286,6 +290,25 @@ func (flu *FlutterEngine) SendPlatformMessageResponse(
286290
return (Result)(res)
287291
}
288292

293+
// RegisterExternalTexture registers an external texture with a unique identifier.
294+
func (flu *FlutterEngine) RegisterExternalTexture(textureID int64) Result {
295+
res := C.FlutterEngineRegisterExternalTexture(flu.Engine, C.int64_t(textureID))
296+
return (Result)(res)
297+
}
298+
299+
// UnregisterExternalTexture unregisters a previous texture registration.
300+
func (flu *FlutterEngine) UnregisterExternalTexture(textureID int64) Result {
301+
res := C.FlutterEngineUnregisterExternalTexture(flu.Engine, C.int64_t(textureID))
302+
return (Result)(res)
303+
}
304+
305+
// MarkExternalTextureFrameAvailable marks that a new texture frame is
306+
// available for a given texture identifier.
307+
func (flu *FlutterEngine) MarkExternalTextureFrameAvailable(textureID int64) Result {
308+
res := C.FlutterEngineMarkExternalTextureFrameAvailable(flu.Engine, C.int64_t(textureID))
309+
return (Result)(res)
310+
}
311+
289312
// FlutterEngineFlushPendingTasksNow flush tasks on a message loop not
290313
// controlled by the Flutter engine.
291314
//

embedder/embedder.h

+43-6
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ typedef enum {
153153
// |PageView| widget does not have implicit scrolling, so that users don't
154154
// navigate to the next page when reaching the end of the current one.
155155
kFlutterSemanticsFlagHasImplicitScrolling = 1 << 18,
156+
// Whether the semantic node is read only.
157+
//
158+
// Only applicable when kFlutterSemanticsFlagIsTextField flag is on.
159+
kFlutterSemanticsFlagIsReadOnly = 1 << 20,
156160
} FlutterSemanticsFlag;
157161

158162
typedef enum {
@@ -369,12 +373,11 @@ typedef struct {
369373
size_t struct_size;
370374
const char* channel;
371375
const uint8_t* message;
372-
const size_t message_size;
376+
size_t message_size;
373377
// The response handle on which to invoke
374-
// |FlutterEngineSendPlatformMessageResponse| when the response is ready. This
375-
// field is ignored for messages being sent from the embedder to the
376-
// framework. |FlutterEngineSendPlatformMessageResponse| must be called for
377-
// all messages received by the embedder. Failure to call
378+
// |FlutterEngineSendPlatformMessageResponse| when the response is ready.
379+
// |FlutterEngineSendPlatformMessageResponse| must be called for all messages
380+
// received by the embedder. Failure to call
378381
// |FlutterEngineSendPlatformMessageResponse| will cause a memory leak. It is
379382
// not safe to send multiple responses on a single response object.
380383
const FlutterPlatformMessageResponseHandle* response_handle;
@@ -384,6 +387,10 @@ typedef void (*FlutterPlatformMessageCallback)(
384387
const FlutterPlatformMessage* /* message*/,
385388
void* /* user data */);
386389

390+
typedef void (*FlutterDataCallback)(const uint8_t* /* data */,
391+
size_t /* size */,
392+
void* /* user data */);
393+
387394
typedef struct {
388395
double left;
389396
double top;
@@ -646,6 +653,9 @@ typedef struct {
646653
// Path to a directory used to store data that is cached across runs of a
647654
// Flutter application (such as compiled shader programs used by Skia).
648655
// This is optional. The string must be NULL terminated.
656+
//
657+
// This is different from the cache-path-dir argument defined in switches.h,
658+
// which is used in |flutter::Settings| as |temp_directory_path|.
649659
const char* persistent_cache_path;
650660

651661
// If true, we'll only read the existing cache, but not write new ones.
@@ -703,6 +713,33 @@ FlutterEngineResult FlutterEngineSendPlatformMessage(
703713
FlutterEngine engine,
704714
const FlutterPlatformMessage* message);
705715

716+
// Creates a platform message response handle that allows the embedder to set a
717+
// native callback for a response to a message. This handle may be set on the
718+
// |response_handle| field of any |FlutterPlatformMessage| sent to the engine.
719+
//
720+
// The handle must be collected via a call to
721+
// |FlutterPlatformMessageReleaseResponseHandle|. This may be done immediately
722+
// after a call to |FlutterEngineSendPlatformMessage| with a platform message
723+
// whose response handle contains the handle created using this call. In case a
724+
// handle is created but never sent in a message, the release call must still be
725+
// made. Not calling release on the handle results in a small memory leak.
726+
//
727+
// The user data baton passed to the data callback is the one specified in this
728+
// call as the third argument.
729+
FLUTTER_EXPORT
730+
FlutterEngineResult FlutterPlatformMessageCreateResponseHandle(
731+
FlutterEngine engine,
732+
FlutterDataCallback data_callback,
733+
void* user_data,
734+
FlutterPlatformMessageResponseHandle** response_out);
735+
736+
// Collects the handle created using
737+
// |FlutterPlatformMessageCreateResponseHandle|.
738+
FLUTTER_EXPORT
739+
FlutterEngineResult FlutterPlatformMessageReleaseResponseHandle(
740+
FlutterEngine engine,
741+
FlutterPlatformMessageResponseHandle* response);
742+
706743
FLUTTER_EXPORT
707744
FlutterEngineResult FlutterEngineSendPlatformMessageResponse(
708745
FlutterEngine engine,
@@ -835,4 +872,4 @@ FlutterEngineResult FlutterEngineRunTask(FlutterEngine engine,
835872
} // extern "C"
836873
#endif
837874

838-
#endif // FLUTTER_EMBEDDER_H_
875+
#endif // FLUTTER_EMBEDDER_H_

embedder/embedder_helper.c

+31-28
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
#include <stdio.h>
23
#include <stdlib.h>
34

45
#include "embedder.h"
@@ -10,36 +11,38 @@ bool proxy_present(void *user_data);
1011
uint32_t proxy_fbo_callback(void *user_data);
1112
bool proxy_make_resource_current(void *user_data);
1213
void *proxy_gl_proc_resolver(void *user_data, const char *procname);
13-
void proxy_platform_message_callback(const FlutterPlatformMessage *message, void *user_data);
14+
void proxy_platform_message_callback(const FlutterPlatformMessage *message,
15+
void *user_data);
16+
bool proxy_gl_external_texture_frame_callback(void *user_data,
17+
int64_t texture_id, size_t width,
18+
size_t height,
19+
FlutterOpenGLTexture *texture);
1420

1521
// C helper
16-
FlutterEngineResult runFlutter(void *user_data, FlutterEngine *engine, FlutterProjectArgs *Args,
17-
const char *const *vmArgs, int nVmAgrs)
18-
{
19-
FlutterRendererConfig config = {};
20-
config.type = kOpenGL;
21-
22-
config.open_gl.struct_size = sizeof(FlutterOpenGLRendererConfig);
23-
config.open_gl.make_current = proxy_make_current;
24-
config.open_gl.clear_current = proxy_clear_current;
25-
config.open_gl.present = proxy_present;
26-
config.open_gl.fbo_callback = proxy_fbo_callback;
27-
config.open_gl.make_resource_current = proxy_make_resource_current;
28-
config.open_gl.gl_proc_resolver = proxy_gl_proc_resolver;
29-
30-
Args->command_line_argc = nVmAgrs;
31-
Args->command_line_argv = vmArgs;
32-
Args->platform_message_callback = proxy_platform_message_callback;
33-
34-
return FlutterEngineRun(FLUTTER_ENGINE_VERSION, &config, Args, user_data, engine);
22+
FlutterEngineResult runFlutter(void *user_data, FlutterEngine *engine,
23+
FlutterProjectArgs *Args,
24+
const char *const *vmArgs, int nVmAgrs) {
25+
FlutterRendererConfig config = {};
26+
config.type = kOpenGL;
27+
28+
config.open_gl.struct_size = sizeof(FlutterOpenGLRendererConfig);
29+
config.open_gl.make_current = proxy_make_current;
30+
config.open_gl.clear_current = proxy_clear_current;
31+
config.open_gl.present = proxy_present;
32+
config.open_gl.fbo_callback = proxy_fbo_callback;
33+
config.open_gl.make_resource_current = proxy_make_resource_current;
34+
config.open_gl.gl_proc_resolver = proxy_gl_proc_resolver;
35+
config.open_gl.gl_external_texture_frame_callback =
36+
proxy_gl_external_texture_frame_callback;
37+
38+
Args->command_line_argc = nVmAgrs;
39+
Args->command_line_argv = vmArgs;
40+
Args->platform_message_callback = proxy_platform_message_callback;
41+
42+
return FlutterEngineRun(FLUTTER_ENGINE_VERSION, &config, Args, user_data,
43+
engine);
3544
}
3645

37-
char **makeCharArray(int size)
38-
{
39-
return calloc(sizeof(char *), size);
40-
}
46+
char **makeCharArray(int size) { return calloc(sizeof(char *), size); }
4147

42-
void setArrayString(char **a, char *s, int n)
43-
{
44-
a[n] = s;
45-
}
48+
void setArrayString(char **a, char *s, int n) { a[n] = s; }

embedder/embedder_proxy.go

+7
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,10 @@ func proxy_gl_proc_resolver(userData unsafe.Pointer, procname *C.char) unsafe.Po
5959
flutterEngine := (*FlutterEngine)(unsafe.Pointer(flutterEnginePointer))
6060
return flutterEngine.GLProcResolver(C.GoString(procname))
6161
}
62+
63+
//export proxy_gl_external_texture_frame_callback
64+
func proxy_gl_external_texture_frame_callback(userData unsafe.Pointer, textureID int64, width int, height int, texture *FlutterOpenGLTexture) C.bool {
65+
flutterEnginePointer := *(*uintptr)(userData)
66+
flutterEngine := (*FlutterEngine)(unsafe.Pointer(flutterEnginePointer))
67+
return C.bool(flutterEngine.GLExternalTextureFrameCallback(textureID, width, height, texture))
68+
}

0 commit comments

Comments
 (0)