Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Add texture support for macOS shell. #8507

Merged
merged 40 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
5302cf8
Merge pull request #1 from flutter/master
cloudwebrtc Mar 28, 2019
2953e13
Merge remote-tracking branch 'upstream/master'
cloudwebrtc Apr 9, 2019
4b422b3
Add texture support for macOS shell.
cloudwebrtc Apr 9, 2019
fa3f8b0
code format.
cloudwebrtc Apr 9, 2019
a222a4a
Add missing file.
cloudwebrtc Apr 9, 2019
668e656
Update FLETexture.h
cloudwebrtc Apr 9, 2019
97f1e91
Update FLEPluginRegistrar.h
cloudwebrtc Apr 10, 2019
cd636b4
Restore header file order.
cloudwebrtc Apr 10, 2019
799e9c1
Update code styles and comments.
cloudwebrtc Apr 10, 2019
52b1417
Rename textureId to textureID.
cloudwebrtc Apr 10, 2019
e49c549
Update comment for FLEExternalTextureGL.h.
cloudwebrtc Apr 10, 2019
7589f46
Update comments and partial code.
cloudwebrtc Apr 17, 2019
2c60631
Rename the texture in some methods to disambiguate.
cloudwebrtc Apr 17, 2019
42b7502
Merge remote-tracking branch 'upstream/master'
cloudwebrtc Apr 18, 2019
dbcdf16
Merge branch 'master' into macos_texture
cloudwebrtc Apr 18, 2019
9b15427
Fix errors caused by merge master conflicts.
cloudwebrtc Apr 18, 2019
e4ff652
Merge remote-tracking branch 'upstream/master' into macos_texture
cloudwebrtc May 19, 2019
ba2efa3
Merge remote-tracking branch 'upstream/master' into macos_texture
cloudwebrtc Jul 11, 2019
9c19bac
clang format.
cloudwebrtc Jul 16, 2019
6ff0a76
gn format.
cloudwebrtc Jul 16, 2019
ec8083a
Add files to licenses_flutter.
cloudwebrtc Jul 16, 2019
ff49e14
Merge remote-tracking branch 'upstream/master' into macos_texture
cloudwebrtc Sep 24, 2019
ee09e69
clang-format.
cloudwebrtc Sep 24, 2019
0f06004
Update.
cloudwebrtc Sep 24, 2019
09156bf
Use shared FlutterTexture.h.
cloudwebrtc Sep 25, 2019
235314a
clang-format.
cloudwebrtc Sep 25, 2019
650c00a
Fixed FlutterTexture.h import path for ios.
cloudwebrtc Sep 25, 2019
d4c0be8
Update.
cloudwebrtc Sep 26, 2019
cda947c
clang-format.
cloudwebrtc Sep 26, 2019
e78d2e1
Change _pixelBuffer to a local variable.
cloudwebrtc Sep 27, 2019
eb3bf39
Add missing static on a local constant
stuartmorgan-g Sep 27, 2019
d622f01
Fix variable naming inconsistency
stuartmorgan-g Sep 27, 2019
ec70d4a
Merge branch 'master' into macos_texture
cloudwebrtc Oct 5, 2019
2871956
Use drawImageRect to draw textures of undesired size.
cloudwebrtc Oct 5, 2019
9b63c65
Merge branch 'macos_texture' of https://github.com/cloudwebrtc/flutte…
cloudwebrtc Oct 5, 2019
4962f91
clang-format.
cloudwebrtc Oct 5, 2019
b20be2c
Fixed CI errors.
cloudwebrtc Oct 5, 2019
067e8a1
update.
cloudwebrtc Oct 7, 2019
27c6f94
clang-format.
cloudwebrtc Oct 7, 2019
5f6833f
Merge remote-tracking branch 'upstream/master' into macos_texture
cloudwebrtc Oct 10, 2019
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
3 changes: 3 additions & 0 deletions shell/platform/darwin/macos/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ _flutter_framework_dir = "$root_out_dir/$_flutter_framework_filename"
# the Flutter engine source root.
_flutter_framework_headers = [
"framework/Headers/FlutterMacOS.h",
"framework/Headers/FLETexture.h",
"framework/Headers/FLEOpenGLContextHandling.h",
"framework/Headers/FLEPlugin.h",
"framework/Headers/FLEPluginRegistrar.h",
Expand All @@ -33,6 +34,8 @@ shared_library("create_flutter_framework_dylib") {
output_name = "$_flutter_framework_name"

sources = [
"framework/Source/FLEExternalTextureGL.h",
"framework/Source/FLEExternalTextureGL.mm",
"framework/Source/FLETextInputModel.h",
"framework/Source/FLETextInputModel.mm",
"framework/Source/FLETextInputPlugin.h",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ FLUTTER_EXPORT
*/
@property(nonnull, readonly) id<FlutterBinaryMessenger> messenger;

/**
* Returns a `FLETextureRegistry` for registering textures provided by the plugin.
*/
@property(nonnull, readonly) id<FLETextureRegistrar> textures;

/**
* The view displaying Flutter content.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#import "FLEOpenGLContextHandling.h"
#import "FLEPluginRegistrar.h"
#import "FLEReshapeListener.h"
#import "FLETexture.h"

#if defined(FLUTTER_FRAMEWORK)
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterBinaryMessenger.h"
Expand Down Expand Up @@ -35,8 +36,10 @@ typedef NS_ENUM(NSInteger, FlutterMouseTrackingMode) {
* Flutter engine in non-interactive mode, or with a drawable Flutter canvas.
*/
FLUTTER_EXPORT
@interface FLEViewController
: NSViewController <FlutterBinaryMessenger, FLEPluginRegistrar, FLEReshapeListener>
@interface FLEViewController : NSViewController <FlutterBinaryMessenger,
FLEPluginRegistrar,
FLEReshapeListener,
FLETextureRegistrar>

/**
* The view this controller manages when launched in interactive mode (headless set to false). Must
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#import "FLEPlugin.h"
#import "FLEPluginRegistrar.h"
#import "FLEReshapeListener.h"
#import "FLETexture.h"
#import "FLEView.h"
#import "FLEViewController.h"
#import "FlutterBinaryMessenger.h"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <Foundation/Foundation.h>

#import "flutter/shell/platform/darwin/macos/framework/Headers/FLETexture.h"
#import "flutter/shell/platform/embedder/embedder.h"

/**
* Used to bridge external FLETexture objects,
* so the implementer only needs to return the CVPixelBufferRef object,
* which will make the interface consistent with the FlutterTexture.
*/
@interface FLEExternalTextureGL : NSObject

/**
* Initializes a texture adapter with |fleTexture|.
*/
- (nonnull instancetype)initWithFLETexture:(nonnull id<FLETexture>)fleTexture;

/**
* Accept texture rendering notifications from the flutter engine.
*/
- (BOOL)populateTextureWidth:(size_t)width
height:(size_t)height
texture:(nonnull FlutterOpenGLTexture*)texture;

/**
* The texture id.
*/
- (int64_t)textureId;

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "flutter/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we're moving away from using FLE as a prefix, and this code isn't part of an unstable public API surface, you can use Flutter rather than FLE as the prefix for this class, so that it doesn't need to be renamed later.


#import <AppKit/NSOpenGL.h>
#import <CoreVideo/CVOpenGLBuffer.h>
#import <CoreVideo/CVOpenGLTextureCache.h>
#import <OpenGL/gl.h>

@implementation FLEExternalTextureGL {
CVOpenGLTextureCacheRef _textureCache;
CVPixelBufferRef _pixelBuffer;
id<FLETexture> _fleTexture;
}

- (instancetype)initWithFLETexture:(id<FLETexture>)fleTexture {
self = [super init];

if (self) {
_fleTexture = fleTexture;
}

return self;
}

static void OnGLTextureRelease(CVPixelBufferRef pixelBuffer) {
CVPixelBufferRelease(pixelBuffer);
}

- (int64_t)textureId {
return (NSInteger)(self);
}

- (BOOL)populateTextureWidth:(size_t)width
height:(size_t)height
texture:(FlutterOpenGLTexture*)texture {
if (_fleTexture == NULL) {
return NO;
}

// Copy image buffer from external texture.
_pixelBuffer = [_fleTexture copyPixelBuffer:width height:height];

if (_pixelBuffer == NULL) {
return NO;
}

// Create the texture cache if necessary.
if (_textureCache == NULL) {
CGLContextObj context = [NSOpenGLContext currentContext].CGLContextObj;
CGLPixelFormatObj format = CGLGetPixelFormat(context);
if (CVOpenGLTextureCacheCreate(kCFAllocatorDefault, NULL, context, format, NULL,
&_textureCache) != kCVReturnSuccess) {
NSLog(@"Could not create texture cache.");
CVPixelBufferRelease(_pixelBuffer);
return NO;
}
}

// Try to clear the cache of OpenGL textures to save memory.
CVOpenGLTextureCacheFlush(_textureCache, 0);

CVOpenGLTextureRef openGLTexture = NULL;
if (CVOpenGLTextureCacheCreateTextureFromImage(kCFAllocatorDefault, _textureCache, _pixelBuffer,
NULL, &openGLTexture) != kCVReturnSuccess) {
CVPixelBufferRelease(_pixelBuffer);
return NO;
}

texture->target = CVOpenGLTextureGetTarget(openGLTexture);
texture->name = CVOpenGLTextureGetName(openGLTexture);
texture->format = GL_RGBA8;
texture->destruction_callback = (VoidCallback)&OnGLTextureRelease;
texture->user_data = openGLTexture;

CVPixelBufferRelease(_pixelBuffer);
return YES;
}

- (void)dealloc {
CVOpenGLTextureCacheRelease(_textureCache);
}

@end
55 changes: 52 additions & 3 deletions shell/platform/darwin/macos/framework/Source/FLEViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
// found in the LICENSE file.

#import "flutter/shell/platform/darwin/macos/framework/Headers/FLEViewController.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FLEViewController_Internal.h"

#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterChannels.h"
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterCodecs.h"
#import "flutter/shell/platform/darwin/macos/framework/Headers/FLEReshapeListener.h"
#import "flutter/shell/platform/darwin/macos/framework/Headers/FLEView.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FLETextInputPlugin.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FLEViewController_Internal.h"
#import "flutter/shell/platform/embedder/embedder.h"

static NSString* const kICUBundlePath = @"icudtl.dat";
Expand Down Expand Up @@ -91,6 +91,10 @@ - (void)dispatchMouseEvent:(nonnull NSEvent*)event phase:(FlutterPointerPhase)ph
*/
- (void)dispatchKeyEvent:(NSEvent*)event ofType:(NSString*)type;

- (BOOL)populateTextureWithIdentifier:(int64_t)textureId
width:(size_t)width
height:(size_t)height
texture:(FlutterOpenGLTexture*)texture;
@end

#pragma mark - Static methods provided to engine configuration
Expand Down Expand Up @@ -142,6 +146,17 @@ static bool OnMakeResourceCurrent(FLEViewController* controller) {
return true;
}

static bool OnAcquireExternalTexture(FLEViewController* controller,
int64_t texture_identifier,
size_t width,
size_t height,
FlutterOpenGLTexture* texture) {
return [controller populateTextureWithIdentifier:texture_identifier
width:width
height:height
texture:texture];
}

#pragma mark Static methods provided for headless engine configuration

static bool HeadlessOnMakeCurrent(FLEViewController* controller) {
Expand Down Expand Up @@ -181,6 +196,9 @@ @implementation FLEViewController {
// A message channel for passing key events to the Flutter engine. This should be replaced with
// an embedding API; see Issue #47.
FlutterBasicMessageChannel* _keyEventChannel;

// A mapping of external textures.
NSMutableDictionary<NSNumber*, FLEExternalTextureGL*>* _textures;
}

@dynamic view;
Expand All @@ -191,6 +209,7 @@ @implementation FLEViewController {
static void CommonInit(FLEViewController* controller) {
controller->_messageHandlers = [[NSMutableDictionary alloc] init];
controller->_additionalKeyResponders = [[NSMutableOrderedSet alloc] init];
controller->_textures = [[NSMutableDictionary alloc] init];
}

- (instancetype)initWithCoder:(NSCoder*)coder {
Expand Down Expand Up @@ -374,7 +393,9 @@ + (FlutterRendererConfig)createRenderConfigHeadless:(BOOL)headless {
.open_gl.clear_current = (BoolCallback)OnClearCurrent,
.open_gl.present = (BoolCallback)OnPresent,
.open_gl.fbo_callback = (UIntCallback)OnFBO,
.open_gl.make_resource_current = (BoolCallback)OnMakeResourceCurrent};
.open_gl.make_resource_current = (BoolCallback)OnMakeResourceCurrent,
.open_gl.gl_external_texture_frame_callback =
(TextureFrameCallback)OnAcquireExternalTexture};
return config;
}
}
Expand All @@ -389,6 +410,30 @@ - (void)makeResourceContextCurrent {
[_resourceContext makeCurrentContext];
}

- (BOOL)populateTextureWithIdentifier:(int64_t)textureId
width:(size_t)width
height:(size_t)height
texture:(FlutterOpenGLTexture*)texture {
return [_textures[@(textureId)] populateTextureWidth:width height:height texture:texture];
}

- (int64_t)registerTexture:(id<FLETexture>)texture {
FLEExternalTextureGL* fleTexture = [[FLEExternalTextureGL alloc] initWithFLETexture:texture];
int64_t textureId = [fleTexture textureId];
FlutterEngineRegisterExternalTexture(_engine, textureId);
_textures[@(textureId)] = fleTexture;
return textureId;
}

- (void)textureFrameAvailable:(int64_t)textureId {
FlutterEngineMarkExternalTextureFrameAvailable(_engine, textureId);
}

- (void)unregisterTexture:(int64_t)textureId {
FlutterEngineUnregisterExternalTexture(_engine, textureId);
[_textures removeObjectForKey:@(textureId)];
}

- (void)handlePlatformMessage:(const FlutterPlatformMessage*)message {
NSData* messageData = [NSData dataWithBytesNoCopy:(void*)message->message
length:message->message_size
Expand Down Expand Up @@ -522,6 +567,10 @@ - (void)setMessageHandlerOnChannel:(nonnull NSString*)channel
return self;
}

- (id<FLETextureRegistrar>)textures {
return self;
}

- (void)addMethodCallDelegate:(nonnull id<FLEPlugin>)delegate
channel:(nonnull FlutterMethodChannel*)channel {
[channel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
Expand Down