Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix hot restart #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/flutter_web_gl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class FlutterWebGL {
static late Pointer<Void> _dummySurface;
static int? _activeFramebuffer;

static Map<String, FlutterGLTexture> _textureCache = {};

static LibOpenGLES get rawOpenGl {
if (_libOpenGLES == null) {
if (Platform.isMacOS || Platform.isIOS) {
Expand Down Expand Up @@ -244,8 +246,10 @@ class FlutterWebGL {
print('\n');
}

static Future<FlutterGLTexture> createTexture(int width, int height) async {
final result = await _channel.invokeMethod('createTexture', {"width": width, "height": height});
static Future<FlutterGLTexture> createTexture(int width, int height, [String id = "__DEFAULT_TEXTURE_ID"]) async {
final result = await _channel.invokeMethod('createTexture', {"width": width, "height": height, "id": id});

if (result['cached'] == 1 && _textureCache.containsKey(id)) return _textureCache[id]!;

Pointer<Uint32> fbo = calloc();
rawOpenGl.glGenFramebuffers(1, fbo);
Expand Down Expand Up @@ -283,6 +287,8 @@ class FlutterWebGL {
_activeFramebuffer = fbo.value;

calloc.free(fbo);

_textureCache[id] = newTexture;
return newTexture;
}

Expand Down
45 changes: 36 additions & 9 deletions macos/Classes/FlutterWebGlPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,19 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
if ([call.method isEqualToString:@"initOpenGL"]) {
static EGLContext context;
static EGLContext context;
static EGLSurface dummySurfaceForDartSide;

if (context != NULL)
{
// this means initOpenGL() was already called, which makes sense if you want to acess a Texture not only
// from the main thread but also from an isolate. On the plugin layer here that doesn't bother because all
// by `initOpenGL``in Dart created contexts will be linked to the one we got from the very first call to `initOpenGL`
// we return this information so that the Dart side can dispose of one context.

result([NSNumber numberWithLong: (long)context]);
result(@{@"context" : [NSNumber numberWithLong: (long)context],
@"dummySurface" : [NSNumber numberWithLong: (long)dummySurfaceForDartSide]
});
return;

}
Expand Down Expand Up @@ -270,7 +274,7 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
CALayer* dummyLayer2 = [[CALayer alloc] init];
dummyLayer2.frame = CGRectMake(0, 0, 1, 1);

EGLSurface dummySurfaceForDartSide = eglCreateWindowSurface(display, config,(__bridge EGLNativeWindowType)dummyLayer, NULL);
dummySurfaceForDartSide = eglCreateWindowSurface(display, config,(__bridge EGLNativeWindowType)dummyLayer, NULL);
EGLSurface dummySurface = eglCreateWindowSurface(display,
config,(__bridge EGLNativeWindowType)dummyLayer2, NULL);

Expand Down Expand Up @@ -313,12 +317,22 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
@"dummySurface" : [NSNumber numberWithLong: (long)dummySurfaceForDartSide]
});
return;

}
if ([call.method isEqualToString:@"createTexture"]) {
static NSMutableDictionary *textureCache = nil;

NSNumber* width;
NSNumber* height;
NSString* id;
if (call.arguments) {
id = call.arguments[@"id"];

if ([textureCache objectForKey:id]) {
NSDictionary* createTextureResult = textureCache[id];
return result(createTextureResult);
}

width = call.arguments[@"width"];
if (width == NULL)
{
Expand Down Expand Up @@ -352,11 +366,24 @@ - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
}

// flutterGLTextures.insert(TextureMap::value_type(flutterGLTexture->flutterTextureId, std::move(flutterGLTexture)));
result(@{
@"textureId" : [NSNumber numberWithLongLong: [_flutterGLTexture flutterTextureId]],
@"rbo": [NSNumber numberWithLongLong: [_flutterGLTexture rbo]],
@"metalAsGLTexture": [NSNumber numberWithLongLong: [_flutterGLTexture metalAsGLTexture]]
});

NSDictionary* createTextureResult = @{
@"textureId" : [NSNumber numberWithLongLong: [_flutterGLTexture flutterTextureId]],
@"rbo": [NSNumber numberWithLongLong: [_flutterGLTexture rbo]],
@"metalAsGLTexture": [NSNumber numberWithLongLong: [_flutterGLTexture metalAsGLTexture]],
@"cached": [NSNumber numberWithInt: 0],
};

result(createTextureResult);

NSMutableDictionary *cachedVal = [NSMutableDictionary dictionaryWithDictionary:createTextureResult];
[cachedVal setObject:[NSNumber numberWithInt:1] forKey:@"cached"];

if (textureCache == nil) {
textureCache = [NSMutableDictionary dictionaryWithDictionary:@{ id: cachedVal }];
} else {
[textureCache setValue:cachedVal forKey:id];
}

return;
}
Expand Down