From 4b422b3f619b2af46935b88301db9a738c484798 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B9=96=E5=8C=97=E6=8D=B7=E6=99=BA=E4=BA=91=E6=8A=80?= =?UTF-8?q?=E6=9C=AF=E6=9C=89=E9=99=90=E5=85=AC=E5=8F=B8?= Date: Tue, 9 Apr 2019 11:12:34 +0800 Subject: [PATCH 01/30] Add texture support for macOS shell. --- shell/platform/darwin/macos/BUILD.gn | 3 + .../framework/Headers/FLEPluginRegistrar.h | 5 ++ .../framework/Headers/FLEViewController.h | 3 +- .../macos/framework/Headers/FlutterMacOS.h | 1 + .../framework/Source/FLEExternalTextureGL.h | 34 ++++++++ .../framework/Source/FLEExternalTextureGL.mm | 87 +++++++++++++++++++ .../framework/Source/FLEViewController.mm | 54 +++++++++++- 7 files changed, 184 insertions(+), 3 deletions(-) create mode 100644 shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h create mode 100644 shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm diff --git a/shell/platform/darwin/macos/BUILD.gn b/shell/platform/darwin/macos/BUILD.gn index 87c055bbdae7e..02d5a72c2b6b9 100644 --- a/shell/platform/darwin/macos/BUILD.gn +++ b/shell/platform/darwin/macos/BUILD.gn @@ -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", @@ -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", diff --git a/shell/platform/darwin/macos/framework/Headers/FLEPluginRegistrar.h b/shell/platform/darwin/macos/framework/Headers/FLEPluginRegistrar.h index b9f7ac99b2a2a..4a22056cad3a3 100644 --- a/shell/platform/darwin/macos/framework/Headers/FLEPluginRegistrar.h +++ b/shell/platform/darwin/macos/framework/Headers/FLEPluginRegistrar.h @@ -31,6 +31,11 @@ FLUTTER_EXPORT */ @property(nonnull, readonly) id messenger; +/** + * Returns a `FLETextureRegistry` for registering textures provided by the plugin. + */ +@property(nonnull, readonly) id textures; + /** * The view displaying Flutter content. * diff --git a/shell/platform/darwin/macos/framework/Headers/FLEViewController.h b/shell/platform/darwin/macos/framework/Headers/FLEViewController.h index e831934cd1911..47cf0406a9981 100644 --- a/shell/platform/darwin/macos/framework/Headers/FLEViewController.h +++ b/shell/platform/darwin/macos/framework/Headers/FLEViewController.h @@ -4,6 +4,7 @@ #import +#import "FLETexture.h" #import "FLEOpenGLContextHandling.h" #import "FLEPluginRegistrar.h" #import "FLEReshapeListener.h" @@ -36,7 +37,7 @@ typedef NS_ENUM(NSInteger, FlutterMouseTrackingMode) { */ FLUTTER_EXPORT @interface FLEViewController - : NSViewController + : NSViewController /** * The view this controller manages when launched in interactive mode (headless set to false). Must diff --git a/shell/platform/darwin/macos/framework/Headers/FlutterMacOS.h b/shell/platform/darwin/macos/framework/Headers/FlutterMacOS.h index 55f949119e0b8..d6677a0d0afad 100644 --- a/shell/platform/darwin/macos/framework/Headers/FlutterMacOS.h +++ b/shell/platform/darwin/macos/framework/Headers/FlutterMacOS.h @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#import "FLETexture.h" #import "FLEOpenGLContextHandling.h" #import "FLEPlugin.h" #import "FLEPluginRegistrar.h" diff --git a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h new file mode 100644 index 0000000000000..2b261f20141c8 --- /dev/null +++ b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h @@ -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 + +#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; + +/** + * 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 diff --git a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm new file mode 100644 index 0000000000000..a8c9b95979e53 --- /dev/null +++ b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm @@ -0,0 +1,87 @@ +// 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" + +#import +#import +#import +#import + +@implementation FLEExternalTextureGL { + CVOpenGLTextureCacheRef _textureCache; + CVPixelBufferRef _pixelBuffer; + id _fleTexture; +} + +- (instancetype)initWithFLETexture:(id)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 diff --git a/shell/platform/darwin/macos/framework/Source/FLEViewController.mm b/shell/platform/darwin/macos/framework/Source/FLEViewController.mm index b0f732e31015b..0a5cbbee8d7a3 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEViewController.mm +++ b/shell/platform/darwin/macos/framework/Source/FLEViewController.mm @@ -4,7 +4,7 @@ #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/macos/framework/Source/FLEExternalTextureGL.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" @@ -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 @@ -142,6 +146,16 @@ 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) { @@ -181,6 +195,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 *_textures; } @dynamic view; @@ -191,6 +208,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 { @@ -374,7 +392,8 @@ + (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; } } @@ -389,6 +408,33 @@ - (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)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 @@ -522,6 +568,10 @@ - (void)setMessageHandlerOnChannel:(nonnull NSString*)channel return self; } +- (id)textures { + return self; +} + - (void)addMethodCallDelegate:(nonnull id)delegate channel:(nonnull FlutterMethodChannel*)channel { [channel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) { From fa3f8b0231ef5a58ded4dd234553a525130c39e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B9=96=E5=8C=97=E6=8D=B7=E6=99=BA=E4=BA=91=E6=8A=80?= =?UTF-8?q?=E6=9C=AF=E6=9C=89=E9=99=90=E5=85=AC=E5=8F=B8?= Date: Tue, 9 Apr 2019 11:33:51 +0800 Subject: [PATCH 02/30] code format. --- .../framework/Headers/FLEViewController.h | 8 ++++--- .../macos/framework/Headers/FlutterMacOS.h | 2 +- .../framework/Source/FLEExternalTextureGL.h | 2 +- .../framework/Source/FLEExternalTextureGL.mm | 11 +++++----- .../framework/Source/FLEViewController.mm | 21 +++++++++---------- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Headers/FLEViewController.h b/shell/platform/darwin/macos/framework/Headers/FLEViewController.h index 47cf0406a9981..bbe8ac55a3613 100644 --- a/shell/platform/darwin/macos/framework/Headers/FLEViewController.h +++ b/shell/platform/darwin/macos/framework/Headers/FLEViewController.h @@ -4,10 +4,10 @@ #import -#import "FLETexture.h" #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" @@ -36,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 +@interface FLEViewController : NSViewController /** * The view this controller manages when launched in interactive mode (headless set to false). Must diff --git a/shell/platform/darwin/macos/framework/Headers/FlutterMacOS.h b/shell/platform/darwin/macos/framework/Headers/FlutterMacOS.h index d6677a0d0afad..14ec7c3b9c809 100644 --- a/shell/platform/darwin/macos/framework/Headers/FlutterMacOS.h +++ b/shell/platform/darwin/macos/framework/Headers/FlutterMacOS.h @@ -2,11 +2,11 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#import "FLETexture.h" #import "FLEOpenGLContextHandling.h" #import "FLEPlugin.h" #import "FLEPluginRegistrar.h" #import "FLEReshapeListener.h" +#import "FLETexture.h" #import "FLEView.h" #import "FLEViewController.h" #import "FlutterBinaryMessenger.h" diff --git a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h index 2b261f20141c8..0f51d29665896 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h +++ b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h @@ -24,7 +24,7 @@ */ - (BOOL)populateTextureWidth:(size_t)width height:(size_t)height - texture:(nonnull FlutterOpenGLTexture *)texture; + texture:(nonnull FlutterOpenGLTexture*)texture; /** * The texture id. diff --git a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm index a8c9b95979e53..3ca1686f9d236 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm +++ b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm @@ -35,7 +35,7 @@ - (int64_t)textureId { - (BOOL)populateTextureWidth:(size_t)width height:(size_t)height - texture:(FlutterOpenGLTexture *)texture { + texture:(FlutterOpenGLTexture*)texture { if (_fleTexture == NULL) { return NO; } @@ -51,8 +51,8 @@ - (BOOL)populateTextureWidth:(size_t)width if (_textureCache == NULL) { CGLContextObj context = [NSOpenGLContext currentContext].CGLContextObj; CGLPixelFormatObj format = CGLGetPixelFormat(context); - if (CVOpenGLTextureCacheCreate(kCFAllocatorDefault, NULL, context, format, - NULL, &_textureCache) != kCVReturnSuccess) { + if (CVOpenGLTextureCacheCreate(kCFAllocatorDefault, NULL, context, format, NULL, + &_textureCache) != kCVReturnSuccess) { NSLog(@"Could not create texture cache."); CVPixelBufferRelease(_pixelBuffer); return NO; @@ -63,9 +63,8 @@ - (BOOL)populateTextureWidth:(size_t)width CVOpenGLTextureCacheFlush(_textureCache, 0); CVOpenGLTextureRef openGLTexture = NULL; - if (CVOpenGLTextureCacheCreateTextureFromImage( - kCFAllocatorDefault, _textureCache, _pixelBuffer, NULL, - &openGLTexture) != kCVReturnSuccess) { + if (CVOpenGLTextureCacheCreateTextureFromImage(kCFAllocatorDefault, _textureCache, _pixelBuffer, + NULL, &openGLTexture) != kCVReturnSuccess) { CVPixelBufferRelease(_pixelBuffer); return NO; } diff --git a/shell/platform/darwin/macos/framework/Source/FLEViewController.mm b/shell/platform/darwin/macos/framework/Source/FLEViewController.mm index 0a5cbbee8d7a3..b299943ad6d80 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEViewController.mm +++ b/shell/platform/darwin/macos/framework/Source/FLEViewController.mm @@ -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/macos/framework/Source/FLEExternalTextureGL.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"; @@ -147,7 +147,8 @@ static bool OnMakeResourceCurrent(FLEViewController* controller) { } static bool OnAcquireExternalTexture(FLEViewController* controller, - int64_t texture_identifier, size_t width, + int64_t texture_identifier, + size_t width, size_t height, FlutterOpenGLTexture* texture) { return [controller populateTextureWithIdentifier:texture_identifier @@ -197,7 +198,7 @@ @implementation FLEViewController { FlutterBasicMessageChannel* _keyEventChannel; // A mapping of external textures. - NSMutableDictionary *_textures; + NSMutableDictionary* _textures; } @dynamic view; @@ -393,7 +394,8 @@ + (FlutterRendererConfig)createRenderConfigHeadless:(BOOL)headless { .open_gl.present = (BoolCallback)OnPresent, .open_gl.fbo_callback = (UIntCallback)OnFBO, .open_gl.make_resource_current = (BoolCallback)OnMakeResourceCurrent, - .open_gl.gl_external_texture_frame_callback = (TextureFrameCallback)OnAcquireExternalTexture}; + .open_gl.gl_external_texture_frame_callback = + (TextureFrameCallback)OnAcquireExternalTexture}; return config; } } @@ -412,14 +414,11 @@ - (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]; + return [_textures[@(textureId)] populateTextureWidth:width height:height texture:texture]; } - (int64_t)registerTexture:(id)texture { - FLEExternalTextureGL* fleTexture = - [[FLEExternalTextureGL alloc] initWithFLETexture:texture]; + FLEExternalTextureGL* fleTexture = [[FLEExternalTextureGL alloc] initWithFLETexture:texture]; int64_t textureId = [fleTexture textureId]; FlutterEngineRegisterExternalTexture(_engine, textureId); _textures[@(textureId)] = fleTexture; @@ -569,7 +568,7 @@ - (void)setMessageHandlerOnChannel:(nonnull NSString*)channel } - (id)textures { - return self; + return self; } - (void)addMethodCallDelegate:(nonnull id)delegate From a222a4a0a7f8f616785f7696b4cfb1f5701475a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B9=96=E5=8C=97=E6=8D=B7=E6=99=BA=E4=BA=91=E6=8A=80?= =?UTF-8?q?=E6=9C=AF=E6=9C=89=E9=99=90=E5=85=AC=E5=8F=B8?= Date: Wed, 10 Apr 2019 06:36:26 +0800 Subject: [PATCH 03/30] Add missing file. --- .../macos/framework/Headers/FLETexture.h | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 shell/platform/darwin/macos/framework/Headers/FLETexture.h diff --git a/shell/platform/darwin/macos/framework/Headers/FLETexture.h b/shell/platform/darwin/macos/framework/Headers/FLETexture.h new file mode 100644 index 0000000000000..dc77b7d038b9d --- /dev/null +++ b/shell/platform/darwin/macos/framework/Headers/FLETexture.h @@ -0,0 +1,52 @@ +// Copyright 2018 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import +#import + +/** + * Implement a texture object for the FLE plugin side. + */ +@protocol FLETexture + +/** + * When the texture on the platform side is ready, + * the flutter engine will copy the texture buffer + * using copyPixelBuffer. + */ +- (nullable CVPixelBufferRef)copyPixelBuffer:(size_t) width height:(size_t)height; + +@end + +/** + * The protocol for an object managing registration for texture. + */ +@protocol FLETextureRegistrar + +/** + * Register a |texture| object and return a textureId. + */ +- (int64_t)registerTexture:(nonnull id)texture; + +/** + * Mark a texture buffer is ready. + */ +- (void)textureFrameAvailable:(int64_t)textureId; + +/** + * Unregister an existing Texture object. + */ +- (void)unregisterTexture:(int64_t)textureId; + +@end From 668e656a8c6c638422e4c57a00180de128c25378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B9=96=E5=8C=97=E6=8D=B7=E6=99=BA=E4=BA=91=E6=8A=80?= =?UTF-8?q?=E6=9C=AF=E6=9C=89=E9=99=90=E5=85=AC=E5=8F=B8?= Date: Wed, 10 Apr 2019 06:37:25 +0800 Subject: [PATCH 04/30] Update FLETexture.h --- .../darwin/macos/framework/Headers/FLETexture.h | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Headers/FLETexture.h b/shell/platform/darwin/macos/framework/Headers/FLETexture.h index dc77b7d038b9d..76bdf4b6f54eb 100644 --- a/shell/platform/darwin/macos/framework/Headers/FLETexture.h +++ b/shell/platform/darwin/macos/framework/Headers/FLETexture.h @@ -1,16 +1,7 @@ -// Copyright 2018 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. + +// 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 #import From 97f1e91a9a2aae85abcfab9d91571ca97dbae651 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B9=96=E5=8C=97=E6=8D=B7=E6=99=BA=E4=BA=91=E6=8A=80?= =?UTF-8?q?=E6=9C=AF=E6=9C=89=E9=99=90=E5=85=AC=E5=8F=B8?= Date: Wed, 10 Apr 2019 10:17:44 +0800 Subject: [PATCH 05/30] Update FLEPluginRegistrar.h --- .../darwin/macos/framework/Headers/FLEPluginRegistrar.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shell/platform/darwin/macos/framework/Headers/FLEPluginRegistrar.h b/shell/platform/darwin/macos/framework/Headers/FLEPluginRegistrar.h index 4a22056cad3a3..30d9d9b0ff4c8 100644 --- a/shell/platform/darwin/macos/framework/Headers/FLEPluginRegistrar.h +++ b/shell/platform/darwin/macos/framework/Headers/FLEPluginRegistrar.h @@ -5,6 +5,7 @@ #import #import "FLEPlugin.h" +#import "FLETexture.h" #if defined(FLUTTER_FRAMEWORK) #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterBinaryMessenger.h" @@ -32,7 +33,7 @@ FLUTTER_EXPORT @property(nonnull, readonly) id messenger; /** - * Returns a `FLETextureRegistry` for registering textures provided by the plugin. + * Returns a `FLETextureRegistrar` for registering textures provided by the plugin. */ @property(nonnull, readonly) id textures; From cd636b43974900c4f3667c232ae2e37ae04d9bc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B9=96=E5=8C=97=E6=8D=B7=E6=99=BA=E4=BA=91=E6=8A=80?= =?UTF-8?q?=E6=9C=AF=E6=9C=89=E9=99=90=E5=85=AC=E5=8F=B8?= Date: Wed, 10 Apr 2019 11:50:11 +0800 Subject: [PATCH 06/30] Restore header file order. --- .../darwin/macos/framework/Source/FLEViewController.mm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shell/platform/darwin/macos/framework/Source/FLEViewController.mm b/shell/platform/darwin/macos/framework/Source/FLEViewController.mm index b299943ad6d80..0c89ce616f232 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEViewController.mm +++ b/shell/platform/darwin/macos/framework/Source/FLEViewController.mm @@ -3,13 +3,14 @@ // 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"; From 799e9c1a954310011897c16cd6067ea5d5281b7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B9=96=E5=8C=97=E6=8D=B7=E6=99=BA=E4=BA=91=E6=8A=80?= =?UTF-8?q?=E6=9C=AF=E6=9C=89=E9=99=90=E5=85=AC=E5=8F=B8?= Date: Wed, 10 Apr 2019 13:03:35 +0800 Subject: [PATCH 07/30] Update code styles and comments. --- .../macos/framework/Headers/FLETexture.h | 8 ++--- .../framework/Source/FLEExternalTextureGL.h | 10 +++--- .../framework/Source/FLEExternalTextureGL.mm | 31 +++++++++---------- .../framework/Source/FLEViewController.mm | 11 +++++-- 4 files changed, 33 insertions(+), 27 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Headers/FLETexture.h b/shell/platform/darwin/macos/framework/Headers/FLETexture.h index 76bdf4b6f54eb..bc771d5bef7cb 100644 --- a/shell/platform/darwin/macos/framework/Headers/FLETexture.h +++ b/shell/platform/darwin/macos/framework/Headers/FLETexture.h @@ -3,27 +3,27 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#import #import -#import /** * Implement a texture object for the FLE plugin side. */ -@protocol FLETexture +@protocol FLETexture /** * When the texture on the platform side is ready, * the flutter engine will copy the texture buffer * using copyPixelBuffer. */ -- (nullable CVPixelBufferRef)copyPixelBuffer:(size_t) width height:(size_t)height; +- (nullable CVPixelBufferRef)copyPixelBuffer:(size_t)width height:(size_t)height; @end /** * The protocol for an object managing registration for texture. */ -@protocol FLETextureRegistrar +@protocol FLETextureRegistrar /** * Register a |texture| object and return a textureId. diff --git a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h index 0f51d29665896..f8bea75dcad9e 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h +++ b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h @@ -15,16 +15,16 @@ @interface FLEExternalTextureGL : NSObject /** - * Initializes a texture adapter with |fleTexture|. + * Initializes a texture adapter with |texture|. */ -- (nonnull instancetype)initWithFLETexture:(nonnull id)fleTexture; +- (nonnull instancetype)initWithFLETexture:(nonnull id)texture; /** * Accept texture rendering notifications from the flutter engine. */ -- (BOOL)populateTextureWidth:(size_t)width - height:(size_t)height - texture:(nonnull FlutterOpenGLTexture*)texture; +- (BOOL)populateTextureWithWidth:(size_t)width + height:(size_t)height + texture:(nonnull FlutterOpenGLTexture*)texture; /** * The texture id. diff --git a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm index 3ca1686f9d236..3480db4eb2344 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm +++ b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm @@ -4,24 +4,23 @@ #import "flutter/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h" -#import -#import -#import -#import +#import +#import +#import @implementation FLEExternalTextureGL { CVOpenGLTextureCacheRef _textureCache; CVPixelBufferRef _pixelBuffer; - id _fleTexture; + id _texture; } -- (instancetype)initWithFLETexture:(id)fleTexture { +- (instancetype)initWithFLETexture:(id)texture { self = [super init]; - if (self) { - _fleTexture = fleTexture; + _texture = texture; + _pixelBuffer = nil; + _textureCache = nil; } - return self; } @@ -33,22 +32,22 @@ - (int64_t)textureId { return (NSInteger)(self); } -- (BOOL)populateTextureWidth:(size_t)width - height:(size_t)height - texture:(FlutterOpenGLTexture*)texture { - if (_fleTexture == NULL) { +- (BOOL)populateTextureWithWidth:(size_t)width + height:(size_t)height + texture:(FlutterOpenGLTexture*)texture { + if (!_texture) { return NO; } // Copy image buffer from external texture. - _pixelBuffer = [_fleTexture copyPixelBuffer:width height:height]; + _pixelBuffer = [_texture copyPixelBuffer:width height:height]; - if (_pixelBuffer == NULL) { + if (!_pixelBuffer) { return NO; } // Create the texture cache if necessary. - if (_textureCache == NULL) { + if (!_textureCache) { CGLContextObj context = [NSOpenGLContext currentContext].CGLContextObj; CGLPixelFormatObj format = CGLGetPixelFormat(context); if (CVOpenGLTextureCacheCreate(kCFAllocatorDefault, NULL, context, format, NULL, diff --git a/shell/platform/darwin/macos/framework/Source/FLEViewController.mm b/shell/platform/darwin/macos/framework/Source/FLEViewController.mm index 0c89ce616f232..2fd47b218be2b 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEViewController.mm +++ b/shell/platform/darwin/macos/framework/Source/FLEViewController.mm @@ -92,6 +92,10 @@ - (void)dispatchMouseEvent:(nonnull NSEvent*)event phase:(FlutterPointerPhase)ph */ - (void)dispatchKeyEvent:(NSEvent*)event ofType:(NSString*)type; +/** + * Forwarding texture copy request to the corresponding texture via |textureId|, + * |width| and |height| may be used to get a specific sized texture in the future. + */ - (BOOL)populateTextureWithIdentifier:(int64_t)textureId width:(size_t)width height:(size_t)height @@ -147,6 +151,9 @@ static bool OnMakeResourceCurrent(FLEViewController* controller) { return true; } +/** + * Dispatching the texture copy request forward to the controller. + */ static bool OnAcquireExternalTexture(FLEViewController* controller, int64_t texture_identifier, size_t width, @@ -198,7 +205,7 @@ @implementation FLEViewController { // an embedding API; see Issue #47. FlutterBasicMessageChannel* _keyEventChannel; - // A mapping of external textures. + // A mapping of textureID to internal FLEExternalTextureGL adapter. NSMutableDictionary* _textures; } @@ -415,7 +422,7 @@ - (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]; + return [_textures[@(textureId)] populateTextureWithWidth:width height:height texture:texture]; } - (int64_t)registerTexture:(id)texture { From 52b141746dc280929f1237c41fb25db3fcfdc4e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B9=96=E5=8C=97=E6=8D=B7=E6=99=BA=E4=BA=91=E6=8A=80?= =?UTF-8?q?=E6=9C=AF=E6=9C=89=E9=99=90=E5=85=AC=E5=8F=B8?= Date: Wed, 10 Apr 2019 13:31:49 +0800 Subject: [PATCH 08/30] Rename textureId to textureID. --- .../macos/framework/Headers/FLETexture.h | 6 ++--- .../framework/Source/FLEExternalTextureGL.h | 2 +- .../framework/Source/FLEExternalTextureGL.mm | 14 +++++----- .../framework/Source/FLEViewController.mm | 26 +++++++++---------- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Headers/FLETexture.h b/shell/platform/darwin/macos/framework/Headers/FLETexture.h index bc771d5bef7cb..871b7b16179ad 100644 --- a/shell/platform/darwin/macos/framework/Headers/FLETexture.h +++ b/shell/platform/darwin/macos/framework/Headers/FLETexture.h @@ -26,18 +26,18 @@ @protocol FLETextureRegistrar /** - * Register a |texture| object and return a textureId. + * Register a |texture| object and return a textureID. */ - (int64_t)registerTexture:(nonnull id)texture; /** * Mark a texture buffer is ready. */ -- (void)textureFrameAvailable:(int64_t)textureId; +- (void)textureFrameAvailable:(int64_t)textureID; /** * Unregister an existing Texture object. */ -- (void)unregisterTexture:(int64_t)textureId; +- (void)unregisterTexture:(int64_t)textureID; @end diff --git a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h index f8bea75dcad9e..0853b06569527 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h +++ b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h @@ -29,6 +29,6 @@ /** * The texture id. */ -- (int64_t)textureId; +- (int64_t)textureID; @end diff --git a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm index 3480db4eb2344..278a593d259ad 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm +++ b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm @@ -6,7 +6,7 @@ #import #import -#import +#import @implementation FLEExternalTextureGL { CVOpenGLTextureCacheRef _textureCache; @@ -28,8 +28,8 @@ static void OnGLTextureRelease(CVPixelBufferRef pixelBuffer) { CVPixelBufferRelease(pixelBuffer); } -- (int64_t)textureId { - return (NSInteger)(self); +- (int64_t)textureID { + return reinterpret_cast(self); } - (BOOL)populateTextureWithWidth:(size_t)width @@ -68,10 +68,10 @@ - (BOOL)populateTextureWithWidth:(size_t)width return NO; } - texture->target = CVOpenGLTextureGetTarget(openGLTexture); - texture->name = CVOpenGLTextureGetName(openGLTexture); - texture->format = GL_RGBA8; - texture->destruction_callback = (VoidCallback)&OnGLTextureRelease; + texture->target = static_cast(CVOpenGLTextureGetTarget(openGLTexture)); + texture->name = static_cast(CVOpenGLTextureGetName(openGLTexture)); + texture->format = static_cast(GL_RGBA8); + texture->destruction_callback = reinterpret_cast(&OnGLTextureRelease); texture->user_data = openGLTexture; CVPixelBufferRelease(_pixelBuffer); diff --git a/shell/platform/darwin/macos/framework/Source/FLEViewController.mm b/shell/platform/darwin/macos/framework/Source/FLEViewController.mm index 2fd47b218be2b..9bc6504254125 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEViewController.mm +++ b/shell/platform/darwin/macos/framework/Source/FLEViewController.mm @@ -93,10 +93,10 @@ - (void)dispatchMouseEvent:(nonnull NSEvent*)event phase:(FlutterPointerPhase)ph - (void)dispatchKeyEvent:(NSEvent*)event ofType:(NSString*)type; /** - * Forwarding texture copy request to the corresponding texture via |textureId|, + * Forwarding texture copy request to the corresponding texture via |textureID|, * |width| and |height| may be used to get a specific sized texture in the future. */ -- (BOOL)populateTextureWithIdentifier:(int64_t)textureId +- (BOOL)populateTextureWithIdentifier:(int64_t)textureID width:(size_t)width height:(size_t)height texture:(FlutterOpenGLTexture*)texture; @@ -418,28 +418,28 @@ - (void)makeResourceContextCurrent { [_resourceContext makeCurrentContext]; } -- (BOOL)populateTextureWithIdentifier:(int64_t)textureId +- (BOOL)populateTextureWithIdentifier:(int64_t)textureID width:(size_t)width height:(size_t)height texture:(FlutterOpenGLTexture*)texture { - return [_textures[@(textureId)] populateTextureWithWidth:width height:height texture:texture]; + return [_textures[@(textureID)] populateTextureWithWidth:width height:height texture:texture]; } - (int64_t)registerTexture:(id)texture { FLEExternalTextureGL* fleTexture = [[FLEExternalTextureGL alloc] initWithFLETexture:texture]; - int64_t textureId = [fleTexture textureId]; - FlutterEngineRegisterExternalTexture(_engine, textureId); - _textures[@(textureId)] = fleTexture; - return textureId; + int64_t textureID = [fleTexture textureID]; + FlutterEngineRegisterExternalTexture(_engine, textureID); + _textures[@(textureID)] = fleTexture; + return textureID; } -- (void)textureFrameAvailable:(int64_t)textureId { - FlutterEngineMarkExternalTextureFrameAvailable(_engine, textureId); +- (void)textureFrameAvailable:(int64_t)textureID { + FlutterEngineMarkExternalTextureFrameAvailable(_engine, textureID); } -- (void)unregisterTexture:(int64_t)textureId { - FlutterEngineUnregisterExternalTexture(_engine, textureId); - [_textures removeObjectForKey:@(textureId)]; +- (void)unregisterTexture:(int64_t)textureID { + FlutterEngineUnregisterExternalTexture(_engine, textureID); + [_textures removeObjectForKey:@(textureID)]; } - (void)handlePlatformMessage:(const FlutterPlatformMessage*)message { From e49c54956f7bad75b7da2d507024ace2845ab47b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B9=96=E5=8C=97=E6=8D=B7=E6=99=BA=E4=BA=91=E6=8A=80?= =?UTF-8?q?=E6=9C=AF=E6=9C=89=E9=99=90=E5=85=AC=E5=8F=B8?= Date: Wed, 10 Apr 2019 14:34:09 +0800 Subject: [PATCH 09/30] Update comment for FLEExternalTextureGL.h. --- .../macos/framework/Source/FLEExternalTextureGL.h | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h index 0853b06569527..e12284501feb5 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h +++ b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h @@ -8,26 +8,24 @@ #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. + * Used to bridge FLETexture object and handle the texture copy request. */ @interface FLEExternalTextureGL : NSObject /** - * Initializes a texture adapter with |texture|. + * Initializes a texture adapter with |texture| return a FLEExternalTextureGL. */ - (nonnull instancetype)initWithFLETexture:(nonnull id)texture; /** - * Accept texture rendering notifications from the flutter engine. + * Accept texture buffer copy request from the flutter engine. */ - (BOOL)populateTextureWithWidth:(size_t)width height:(size_t)height texture:(nonnull FlutterOpenGLTexture*)texture; /** - * The texture id. + * The texture ID. */ - (int64_t)textureID; From 7589f463c247c21571b625b5bb3b585de3cef82d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B9=96=E5=8C=97=E6=8D=B7=E6=99=BA=E4=BA=91=E6=8A=80?= =?UTF-8?q?=E6=9C=AF=E6=9C=89=E9=99=90=E5=85=AC=E5=8F=B8?= Date: Wed, 17 Apr 2019 22:44:07 +0800 Subject: [PATCH 10/30] Update comments and partial code. --- .../framework/Source/FLEExternalTextureGL.h | 14 +++-- .../framework/Source/FLEExternalTextureGL.mm | 60 ++++++++++--------- .../framework/Source/FLEViewController.mm | 55 ++++++++--------- 3 files changed, 69 insertions(+), 60 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h index e12284501feb5..d3452155631ac 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h +++ b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h @@ -8,24 +8,28 @@ #import "flutter/shell/platform/embedder/embedder.h" /** - * Used to bridge FLETexture object and handle the texture copy request. + * Used to bridge FLETexture object and handle the texture copy request the + * flutter engine. */ @interface FLEExternalTextureGL : NSObject /** - * Initializes a texture adapter with |texture| return a FLEExternalTextureGL. + * Initializes a texture adapter with |texture| return a instance. */ - (nonnull instancetype)initWithFLETexture:(nonnull id)texture; /** - * Accept texture buffer copy request from the flutter engine. + * Accepts texture buffer copy request from the flutter engine. + * When the user side marks the textureId as available, the flutter engine will + * callback to this method and ask for populate the |openGLTexture| object, + * such as the texture type and the format of the pixel buffer and the texture object. */ - (BOOL)populateTextureWithWidth:(size_t)width height:(size_t)height - texture:(nonnull FlutterOpenGLTexture*)texture; + texture:(nonnull FlutterOpenGLTexture*)openGLTexture; /** - * The texture ID. + * Returns the ID for the FLETexture instance. */ - (int64_t)textureID; diff --git a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm index 278a593d259ad..220471ffccb84 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm +++ b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm @@ -8,9 +8,23 @@ #import #import +static void OnGLTextureRelease(CVPixelBufferRef pixelBuffer) { + CVPixelBufferRelease(pixelBuffer); +} + @implementation FLEExternalTextureGL { - CVOpenGLTextureCacheRef _textureCache; + /** + * OpenGL texture cache. + */ + CVOpenGLTextureCacheRef _openGLTextureCache; + /** + * The pixel buffer copied from the user side will be released + * when the flutter engine renders it. + */ CVPixelBufferRef _pixelBuffer; + /** + * User side texture object, used to copy pixel buffer. + */ id _texture; } @@ -18,40 +32,30 @@ - (instancetype)initWithFLETexture:(id)texture { self = [super init]; if (self) { _texture = texture; - _pixelBuffer = nil; - _textureCache = nil; } return self; } -static void OnGLTextureRelease(CVPixelBufferRef pixelBuffer) { - CVPixelBufferRelease(pixelBuffer); -} - - (int64_t)textureID { return reinterpret_cast(self); } - (BOOL)populateTextureWithWidth:(size_t)width height:(size_t)height - texture:(FlutterOpenGLTexture*)texture { - if (!_texture) { - return NO; - } - - // Copy image buffer from external texture. + texture:(FlutterOpenGLTexture*)openGLTexture { + // Copy the pixel buffer from the FLETexture instance implemented on the user side. _pixelBuffer = [_texture copyPixelBuffer:width height:height]; if (!_pixelBuffer) { return NO; } - // Create the texture cache if necessary. - if (!_textureCache) { + // Create the opengl texture cache if necessary. + if (!_openGLTextureCache) { CGLContextObj context = [NSOpenGLContext currentContext].CGLContextObj; CGLPixelFormatObj format = CGLGetPixelFormat(context); if (CVOpenGLTextureCacheCreate(kCFAllocatorDefault, NULL, context, format, NULL, - &_textureCache) != kCVReturnSuccess) { + &_openGLTextureCache) != kCVReturnSuccess) { NSLog(@"Could not create texture cache."); CVPixelBufferRelease(_pixelBuffer); return NO; @@ -59,27 +63,27 @@ - (BOOL)populateTextureWithWidth:(size_t)width } // Try to clear the cache of OpenGL textures to save memory. - CVOpenGLTextureCacheFlush(_textureCache, 0); + CVOpenGLTextureCacheFlush(_openGLTextureCache, 0); - CVOpenGLTextureRef openGLTexture = NULL; - if (CVOpenGLTextureCacheCreateTextureFromImage(kCFAllocatorDefault, _textureCache, _pixelBuffer, - NULL, &openGLTexture) != kCVReturnSuccess) { + CVOpenGLTextureRef cvOpenGLTexture = NULL; + if (CVOpenGLTextureCacheCreateTextureFromImage(kCFAllocatorDefault, _openGLTextureCache, + _pixelBuffer, NULL, + &cvOpenGLTexture) != kCVReturnSuccess) { CVPixelBufferRelease(_pixelBuffer); return NO; } - - texture->target = static_cast(CVOpenGLTextureGetTarget(openGLTexture)); - texture->name = static_cast(CVOpenGLTextureGetName(openGLTexture)); - texture->format = static_cast(GL_RGBA8); - texture->destruction_callback = reinterpret_cast(&OnGLTextureRelease); - texture->user_data = openGLTexture; - CVPixelBufferRelease(_pixelBuffer); + + openGLTexture->target = static_cast(CVOpenGLTextureGetTarget(cvOpenGLTexture)); + openGLTexture->name = static_cast(CVOpenGLTextureGetName(cvOpenGLTexture)); + openGLTexture->format = static_cast(GL_RGBA8); + openGLTexture->destruction_callback = (VoidCallback)OnGLTextureRelease; + openGLTexture->user_data = cvOpenGLTexture; return YES; } - (void)dealloc { - CVOpenGLTextureCacheRelease(_textureCache); + CVOpenGLTextureCacheRelease(_openGLTextureCache); } @end diff --git a/shell/platform/darwin/macos/framework/Source/FLEViewController.mm b/shell/platform/darwin/macos/framework/Source/FLEViewController.mm index 9bc6504254125..797a755ed800f 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEViewController.mm +++ b/shell/platform/darwin/macos/framework/Source/FLEViewController.mm @@ -93,13 +93,12 @@ - (void)dispatchMouseEvent:(nonnull NSEvent*)event phase:(FlutterPointerPhase)ph - (void)dispatchKeyEvent:(NSEvent*)event ofType:(NSString*)type; /** - * Forwarding texture copy request to the corresponding texture via |textureID|, - * |width| and |height| may be used to get a specific sized texture in the future. + * Forwards texture copy request to the corresponding texture via |textureID|. */ - (BOOL)populateTextureWithIdentifier:(int64_t)textureID width:(size_t)width height:(size_t)height - texture:(FlutterOpenGLTexture*)texture; + texture:(FlutterOpenGLTexture*)openGLTexture; @end #pragma mark - Static methods provided to engine configuration @@ -418,30 +417,6 @@ - (void)makeResourceContextCurrent { [_resourceContext makeCurrentContext]; } -- (BOOL)populateTextureWithIdentifier:(int64_t)textureID - width:(size_t)width - height:(size_t)height - texture:(FlutterOpenGLTexture*)texture { - return [_textures[@(textureID)] populateTextureWithWidth:width height:height texture:texture]; -} - -- (int64_t)registerTexture:(id)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 @@ -532,6 +507,32 @@ - (void)dispatchKeyEvent:(NSEvent*)event ofType:(NSString*)type { }]; } +#pragma mark - FlutterTextureRegistrar + +- (BOOL)populateTextureWithIdentifier:(int64_t)textureID + width:(size_t)width + height:(size_t)height + texture:(FlutterOpenGLTexture*)texture { + return [_textures[@(textureID)] populateTextureWithWidth:width height:height texture:texture]; +} + +- (int64_t)registerTexture:(id)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)]; +} + #pragma mark - FLEReshapeListener /** From 2c606319f7b8b4d49f81541555e91b4f57188628 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B9=96=E5=8C=97=E6=8D=B7=E6=99=BA=E4=BA=91=E6=8A=80?= =?UTF-8?q?=E6=9C=AF=E6=9C=89=E9=99=90=E5=85=AC=E5=8F=B8?= Date: Wed, 17 Apr 2019 22:58:18 +0800 Subject: [PATCH 11/30] Rename the texture in some methods to disambiguate. --- .../macos/framework/Source/FLEExternalTextureGL.h | 2 +- .../macos/framework/Source/FLEExternalTextureGL.mm | 2 +- .../darwin/macos/framework/Source/FLEViewController.mm | 10 +++++----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h index d3452155631ac..c026ebfd1f22e 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h +++ b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h @@ -26,7 +26,7 @@ */ - (BOOL)populateTextureWithWidth:(size_t)width height:(size_t)height - texture:(nonnull FlutterOpenGLTexture*)openGLTexture; + openGLTexture:(nonnull FlutterOpenGLTexture*)openGLTexture; /** * Returns the ID for the FLETexture instance. diff --git a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm index 220471ffccb84..044ee5b06aa4e 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm +++ b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm @@ -42,7 +42,7 @@ - (int64_t)textureID { - (BOOL)populateTextureWithWidth:(size_t)width height:(size_t)height - texture:(FlutterOpenGLTexture*)openGLTexture { + openGLTexture:(FlutterOpenGLTexture*)openGLTexture { // Copy the pixel buffer from the FLETexture instance implemented on the user side. _pixelBuffer = [_texture copyPixelBuffer:width height:height]; diff --git a/shell/platform/darwin/macos/framework/Source/FLEViewController.mm b/shell/platform/darwin/macos/framework/Source/FLEViewController.mm index 797a755ed800f..3cb619df5d446 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEViewController.mm +++ b/shell/platform/darwin/macos/framework/Source/FLEViewController.mm @@ -98,7 +98,7 @@ - (void)dispatchKeyEvent:(NSEvent*)event ofType:(NSString*)type; - (BOOL)populateTextureWithIdentifier:(int64_t)textureID width:(size_t)width height:(size_t)height - texture:(FlutterOpenGLTexture*)openGLTexture; + openGLTexture:(FlutterOpenGLTexture*)openGLTexture; @end #pragma mark - Static methods provided to engine configuration @@ -157,11 +157,11 @@ static bool OnAcquireExternalTexture(FLEViewController* controller, int64_t texture_identifier, size_t width, size_t height, - FlutterOpenGLTexture* texture) { + FlutterOpenGLTexture* openGLTexture) { return [controller populateTextureWithIdentifier:texture_identifier width:width height:height - texture:texture]; + openGLTexture:openGLTexture]; } #pragma mark Static methods provided for headless engine configuration @@ -512,8 +512,8 @@ - (void)dispatchKeyEvent:(NSEvent*)event ofType:(NSString*)type { - (BOOL)populateTextureWithIdentifier:(int64_t)textureID width:(size_t)width height:(size_t)height - texture:(FlutterOpenGLTexture*)texture { - return [_textures[@(textureID)] populateTextureWithWidth:width height:height texture:texture]; + openGLTexture:(FlutterOpenGLTexture*)openGLTexture { + return [_textures[@(textureID)] populateTextureWithWidth:width height:height openGLTexture:openGLTexture]; } - (int64_t)registerTexture:(id)texture { From 9b1542708cc0b5b26429827c5f7e99a173cd1d52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B9=96=E5=8C=97=E6=8D=B7=E6=99=BA=E4=BA=91=E6=8A=80?= =?UTF-8?q?=E6=9C=AF=E6=9C=89=E9=99=90=E5=85=AC=E5=8F=B8?= Date: Thu, 18 Apr 2019 20:30:39 +0800 Subject: [PATCH 12/30] Fix errors caused by merge master conflicts. --- .../darwin/macos/framework/Headers/FLEViewController.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shell/platform/darwin/macos/framework/Headers/FLEViewController.h b/shell/platform/darwin/macos/framework/Headers/FLEViewController.h index 38187e2cd652c..da4dbcc53156a 100644 --- a/shell/platform/darwin/macos/framework/Headers/FLEViewController.h +++ b/shell/platform/darwin/macos/framework/Headers/FLEViewController.h @@ -40,7 +40,8 @@ FLUTTER_EXPORT FLEPluginRegistrar, FLEReshapeListener, FLEPluginRegistry, - FLEReshapeListener> + FLEReshapeListener, + FLETextureRegistrar> /** * The view this controller manages when launched in interactive mode (headless set to false). Must From 9c19bac429e26ff7adb95bf9b64bc2b749605d74 Mon Sep 17 00:00:00 2001 From: CloudWebRTC Date: Wed, 17 Jul 2019 02:07:56 +0800 Subject: [PATCH 13/30] clang format. --- .../darwin/macos/framework/Source/FLEEngine.mm | 17 +++++++++-------- .../framework/Source/FLEExternalTextureGL.h | 2 +- .../framework/Source/FLEExternalTextureGL.mm | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Source/FLEEngine.mm b/shell/platform/darwin/macos/framework/Source/FLEEngine.mm index 1ca0bb7d04da5..2341def97dc31 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEEngine.mm +++ b/shell/platform/darwin/macos/framework/Source/FLEEngine.mm @@ -53,7 +53,7 @@ - (void)shutDownEngine; - (BOOL)populateTextureWithIdentifier:(int64_t)textureID width:(size_t)width height:(size_t)height - openGLTexture:(FlutterOpenGLTexture*)openGLTexture; + openGLTexture:(FlutterOpenGLTexture*)openGLTexture; @end @@ -138,9 +138,9 @@ static bool OnAcquireExternalTexture(FLEEngine* engine, size_t height, FlutterOpenGLTexture* openGLTexture) { return [engine populateTextureWithIdentifier:texture_identifier - width:width - height:height - openGLTexture:openGLTexture]; + width:width + height:height + openGLTexture:openGLTexture]; } #pragma mark - FLEEngine implementation @@ -204,8 +204,7 @@ - (BOOL)runWithEntrypoint:(NSString*)entrypoint { .open_gl.present = (BoolCallback)OnPresent, .open_gl.fbo_callback = (UIntCallback)OnFBO, .open_gl.make_resource_current = (BoolCallback)OnMakeResourceCurrent, - .open_gl.gl_external_texture_frame_callback = - (TextureFrameCallback)OnAcquireExternalTexture, + .open_gl.gl_external_texture_frame_callback = (TextureFrameCallback)OnAcquireExternalTexture, }; // TODO(stuartmorgan): Move internal channel registration from FLEViewController to here. @@ -390,8 +389,10 @@ - (void)setMessageHandlerOnChannel:(nonnull NSString*)channel - (BOOL)populateTextureWithIdentifier:(int64_t)textureID width:(size_t)width height:(size_t)height - openGLTexture:(FlutterOpenGLTexture*)openGLTexture { - return [_textures[@(textureID)] populateTextureWithWidth:width height:height openGLTexture:openGLTexture]; + openGLTexture:(FlutterOpenGLTexture*)openGLTexture { + return [_textures[@(textureID)] populateTextureWithWidth:width + height:height + openGLTexture:openGLTexture]; } - (int64_t)registerTexture:(id)texture { diff --git a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h index c026ebfd1f22e..a6b94fc398d33 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h +++ b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h @@ -26,7 +26,7 @@ */ - (BOOL)populateTextureWithWidth:(size_t)width height:(size_t)height - openGLTexture:(nonnull FlutterOpenGLTexture*)openGLTexture; + openGLTexture:(nonnull FlutterOpenGLTexture*)openGLTexture; /** * Returns the ID for the FLETexture instance. diff --git a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm index 044ee5b06aa4e..135291cef16b9 100644 --- a/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm +++ b/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm @@ -42,7 +42,7 @@ - (int64_t)textureID { - (BOOL)populateTextureWithWidth:(size_t)width height:(size_t)height - openGLTexture:(FlutterOpenGLTexture*)openGLTexture { + openGLTexture:(FlutterOpenGLTexture*)openGLTexture { // Copy the pixel buffer from the FLETexture instance implemented on the user side. _pixelBuffer = [_texture copyPixelBuffer:width height:height]; From 6ff0a767024568a64a8910a4800bd8818a04ef88 Mon Sep 17 00:00:00 2001 From: CloudWebRTC Date: Wed, 17 Jul 2019 02:21:04 +0800 Subject: [PATCH 14/30] gn format. --- shell/platform/darwin/macos/BUILD.gn | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/shell/platform/darwin/macos/BUILD.gn b/shell/platform/darwin/macos/BUILD.gn index b1c769961e3af..25ccabd276bfc 100644 --- a/shell/platform/darwin/macos/BUILD.gn +++ b/shell/platform/darwin/macos/BUILD.gn @@ -77,7 +77,10 @@ shared_library("create_flutter_framework_dylib") { cflags_objcc = [ "-fobjc-arc" ] - libs = [ "Cocoa.framework", "CoreVideo.framework" ] + libs = [ + "Cocoa.framework", + "CoreVideo.framework", + ] } copy("copy_framework_dylib") { From ec8083a7b8e5e7bd8e698cda00fb04e1cb3919af Mon Sep 17 00:00:00 2001 From: CloudWebRTC Date: Wed, 17 Jul 2019 02:35:42 +0800 Subject: [PATCH 15/30] Add files to licenses_flutter. --- ci/licenses_golden/licenses_flutter | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index 06b0d17ac1d79..86faa45b7b2c4 100644 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -750,6 +750,7 @@ FILE: ../../../flutter/shell/platform/darwin/ios/platform_view_ios.mm FILE: ../../../flutter/shell/platform/darwin/macos/framework/FlutterMacOS.podspec FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FLEDartProject.h FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FLEEngine.h +FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FLETexture.h FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FLEViewController.h FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterMacOS.h FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterPluginMacOS.h @@ -759,6 +760,8 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FLEDartProje FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FLEDartProject_Internal.h FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FLEEngine.mm FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FLEEngine_Internal.h +FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.h +FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FLEExternalTextureGL.mm FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FLETextInputModel.h FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FLETextInputModel.mm FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FLETextInputPlugin.h From ee09e6948f87e6e2b97b27f27dd15214dc5b41e9 Mon Sep 17 00:00:00 2001 From: CloudWebRTC Date: Tue, 24 Sep 2019 17:50:21 +0800 Subject: [PATCH 16/30] clang-format. --- shell/platform/darwin/macos/framework/Source/FlutterEngine.mm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm b/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm index 8cdc983480e77..1e098fc6c6e31 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm @@ -439,7 +439,8 @@ - (BOOL)populateTextureWithIdentifier:(int64_t)textureID } - (int64_t)registerTexture:(id)texture { - FlutterExternalTextureGL* FlutterTexture = [[FlutterExternalTextureGL alloc] initWithFlutterTexture:texture]; + FlutterExternalTextureGL* FlutterTexture = + [[FlutterExternalTextureGL alloc] initWithFlutterTexture:texture]; int64_t textureID = [FlutterTexture textureID]; FlutterEngineRegisterExternalTexture(_engine, textureID); _textures[@(textureID)] = FlutterTexture; From 0f0600469561be84e93cf7ac329d3c7c11e4f03f Mon Sep 17 00:00:00 2001 From: CloudWebRTC Date: Tue, 24 Sep 2019 17:56:38 +0800 Subject: [PATCH 17/30] Update. --- ci/licenses_golden/licenses_flutter | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index 7b8059076f49f..a161f6f53fc6b 100644 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -810,12 +810,12 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterDartP FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h +FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.h +FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.mm FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputModel.h FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputModel.mm FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.h FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputPlugin.mm -FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.h -FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.mm FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterView.h FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterView.mm FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm From 09156bf51373445dbbb490584654673c715b554a Mon Sep 17 00:00:00 2001 From: CloudWebRTC Date: Wed, 25 Sep 2019 11:38:45 +0800 Subject: [PATCH 18/30] Use shared FlutterTexture.h. --- ci/licenses_golden/licenses_flutter | 3 +- .../framework/Headers/FlutterTexture.h | 0 .../darwin/common/framework_shared.gni | 1 + shell/platform/darwin/macos/BUILD.gn | 1 - .../macos/framework/Headers/FlutterEngine.h | 2 +- .../Headers/FlutterPluginRegistrarMacOS.h | 5 ++- .../macos/framework/Headers/FlutterTexture.h | 43 ------------------- .../macos/framework/Source/FlutterEngine.mm | 2 +- .../Source/FlutterExternalTextureGL.h | 2 +- .../Source/FlutterExternalTextureGL.mm | 4 +- shell/platform/embedder/embedder.cc | 34 ++++++++++++++- shell/platform/embedder/embedder.h | 6 ++- 12 files changed, 49 insertions(+), 54 deletions(-) rename shell/platform/darwin/{ios => common}/framework/Headers/FlutterTexture.h (100%) delete mode 100644 shell/platform/darwin/macos/framework/Headers/FlutterTexture.h diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index a161f6f53fc6b..36b14d5562829 100644 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -714,6 +714,7 @@ FILE: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterBin FILE: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h FILE: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterCodecs.h FILE: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h +FILE: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterTexture.h FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterChannels.mm FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterChannelsTest.m FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterCodecs.mm @@ -731,7 +732,6 @@ FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterHeadle FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlatformViews.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlugin.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h -FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterTexture.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Info.plist FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm @@ -802,7 +802,6 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterEngi FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterMacOS.h FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterPluginMacOS.h FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterPluginRegistrarMacOS.h -FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterTexture.h FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterViewController.h FILE: ../../../flutter/shell/platform/darwin/macos/framework/Info.plist FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterAppDelegate.mm diff --git a/shell/platform/darwin/ios/framework/Headers/FlutterTexture.h b/shell/platform/darwin/common/framework/Headers/FlutterTexture.h similarity index 100% rename from shell/platform/darwin/ios/framework/Headers/FlutterTexture.h rename to shell/platform/darwin/common/framework/Headers/FlutterTexture.h diff --git a/shell/platform/darwin/common/framework_shared.gni b/shell/platform/darwin/common/framework_shared.gni index 7b2b3cb2a16b5..8a679c4d6fa25 100644 --- a/shell/platform/darwin/common/framework_shared.gni +++ b/shell/platform/darwin/common/framework_shared.gni @@ -8,5 +8,6 @@ framework_shared_headers = "framework/Headers/FlutterBinaryMessenger.h", "framework/Headers/FlutterChannels.h", "framework/Headers/FlutterCodecs.h", + "framework/Headers/FlutterTexture.h", ], "abspath") diff --git a/shell/platform/darwin/macos/BUILD.gn b/shell/platform/darwin/macos/BUILD.gn index a1a05d25a0e29..4cb1fa32b559f 100644 --- a/shell/platform/darwin/macos/BUILD.gn +++ b/shell/platform/darwin/macos/BUILD.gn @@ -35,7 +35,6 @@ _flutter_framework_headers = [ "framework/Headers/FlutterMacOS.h", "framework/Headers/FlutterPluginMacOS.h", "framework/Headers/FlutterPluginRegistrarMacOS.h", - "framework/Headers/FlutterTexture.h", "framework/Headers/FlutterViewController.h", ] diff --git a/shell/platform/darwin/macos/framework/Headers/FlutterEngine.h b/shell/platform/darwin/macos/framework/Headers/FlutterEngine.h index 33209b7b07ea0..4f4583878ff15 100644 --- a/shell/platform/darwin/macos/framework/Headers/FlutterEngine.h +++ b/shell/platform/darwin/macos/framework/Headers/FlutterEngine.h @@ -21,7 +21,7 @@ * Coordinates a single instance of execution of a Flutter engine. */ FLUTTER_EXPORT -@interface FlutterEngine : NSObject +@interface FlutterEngine : NSObject /** * Initializes an engine with the given viewController. diff --git a/shell/platform/darwin/macos/framework/Headers/FlutterPluginRegistrarMacOS.h b/shell/platform/darwin/macos/framework/Headers/FlutterPluginRegistrarMacOS.h index 3cb953728d5cf..bd760823a3863 100644 --- a/shell/platform/darwin/macos/framework/Headers/FlutterPluginRegistrarMacOS.h +++ b/shell/platform/darwin/macos/framework/Headers/FlutterPluginRegistrarMacOS.h @@ -30,9 +30,10 @@ FLUTTER_EXPORT @property(nonnull, readonly) id messenger; /** - * ReturThe texture registrar. + * Returns a `FlutterTextureRegistry` for registering textures + * provided by the plugin. */ -@property(nonnull, readonly) id textures; +@property(nonnull, readonly) id textures; /** * The view displaying Flutter content. May return |nil|, for instance in a headless environment. diff --git a/shell/platform/darwin/macos/framework/Headers/FlutterTexture.h b/shell/platform/darwin/macos/framework/Headers/FlutterTexture.h deleted file mode 100644 index 1e1e4ad8748d1..0000000000000 --- a/shell/platform/darwin/macos/framework/Headers/FlutterTexture.h +++ /dev/null @@ -1,43 +0,0 @@ - -// 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 -#import - -/** - * Implement a texture object for the Flutter plugin side. - */ -@protocol FlutterTexture - -/** - * When the texture on the platform side is ready, - * the flutter engine will copy the texture buffer - * using copyPixelBuffer. - */ -- (nullable CVPixelBufferRef)copyPixelBuffer:(size_t)width height:(size_t)height; - -@end - -/** - * The protocol for an object managing registration for texture. - */ -@protocol FlutterTextureRegistrar - -/** - * Register a |texture| object and return a textureID. - */ -- (int64_t)registerTexture:(nonnull id)texture; - -/** - * Mark a texture buffer is ready. - */ -- (void)textureFrameAvailable:(int64_t)textureID; - -/** - * Unregister an existing Texture object. - */ -- (void)unregisterTexture:(int64_t)textureID; - -@end diff --git a/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm b/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm index 1e098fc6c6e31..4855528833d2e 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm @@ -87,7 +87,7 @@ - (instancetype)initWithPlugin:(NSString*)pluginKey flutterEngine:(FlutterEngine return _flutterEngine.binaryMessenger; } -- (id)textures { +- (id)textures { return _flutterEngine; } diff --git a/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.h b/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.h index 6edadfa219ca7..52e3e489484f8 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.h +++ b/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.h @@ -4,7 +4,7 @@ #import -#import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterTexture.h" +#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterTexture.h" #import "flutter/shell/platform/embedder/embedder.h" /** diff --git a/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.mm b/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.mm index 9a95c09fa6b0c..29f945a1f8902 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.mm @@ -44,7 +44,7 @@ - (BOOL)populateTextureWithWidth:(size_t)width height:(size_t)height openGLTexture:(FlutterOpenGLTexture*)openGLTexture { // Copy the pixel buffer from the FlutterTexture instance implemented on the user side. - _pixelBuffer = [_texture copyPixelBuffer:width height:height]; + _pixelBuffer = [_texture copyPixelBuffer]; if (!_pixelBuffer) { return NO; @@ -79,6 +79,8 @@ - (BOOL)populateTextureWithWidth:(size_t)width openGLTexture->format = static_cast(GL_RGBA8); openGLTexture->destruction_callback = (VoidCallback)OnGLTextureRelease; openGLTexture->user_data = cvOpenGLTexture; + openGLTexture->width = CVPixelBufferGetWidth(_pixelBuffer); + openGLTexture->height = CVPixelBufferGetHeight(_pixelBuffer); return YES; } diff --git a/shell/platform/embedder/embedder.cc b/shell/platform/embedder/embedder.cc index 72da210079e73..495f46917257c 100644 --- a/shell/platform/embedder/embedder.cc +++ b/shell/platform/embedder/embedder.cc @@ -45,6 +45,8 @@ extern const intptr_t kPlatformStrongDillSize; const int32_t kFlutterSemanticsNodeIdBatchEnd = -1; const int32_t kFlutterSemanticsCustomActionIdBatchEnd = -1; +#define GL_TEXTURE_RECTANGLE 0x84F5 + static FlutterEngineResult LogEmbedderError(FlutterEngineResult code, const char* name, const char* function, @@ -807,7 +809,18 @@ FlutterEngineResult FlutterEngineRun(size_t version, GrGLTextureInfo gr_texture_info = {texture.target, texture.name, texture.format}; - GrBackendTexture gr_backend_texture(size.width(), size.height(), + size_t width = size.width(); + size_t height = size.height(); + + /// When the texture type is GL_TEXTURE_RECTANGLE, the image size must + /// first be set to the actual size, and then resize to the bounds size. + /// see: https://stackoverflow.com/questions/13933503/core-video-pixel-buffers-as-gl-texture-2d + if(texture.target == GL_TEXTURE_RECTANGLE) { + width = texture.width; + height = texture.height; + } + + GrBackendTexture gr_backend_texture(width, height, GrMipMapped::kNo, gr_texture_info); SkImage::TextureReleaseProc release_proc = texture.destruction_callback; auto image = SkImage::MakeFromTexture( @@ -821,6 +834,25 @@ FlutterEngineResult FlutterEngineRun(size_t version, texture.user_data // texture release context ); + /// Scale to the bounds size to adapt the user to change the Texture widget size. + if(image && texture.target == GL_TEXTURE_RECTANGLE) { + const auto resized_dimensions = SkISize::Make(size.width(), size.height()); + + if (resized_dimensions != image->dimensions()) { + const auto scaled_image_info = image->imageInfo().makeWH( + resized_dimensions.width(), resized_dimensions.height()); + + SkBitmap scaled_bitmap; + + if (scaled_bitmap.tryAllocPixels(scaled_image_info) + && image->scalePixels(scaled_bitmap.pixmap(), kLow_SkFilterQuality, + SkImage::kDisallow_CachingHint)) { + scaled_bitmap.setImmutable(); + image = SkImage::MakeFromBitmap(scaled_bitmap); + } + } + } + if (!image) { // In case Skia rejects the image, call the release proc so that // embedders can perform collection of intermediates. diff --git a/shell/platform/embedder/embedder.h b/shell/platform/embedder/embedder.h index 4953066473107..429b4dcd09e1c 100644 --- a/shell/platform/embedder/embedder.h +++ b/shell/platform/embedder/embedder.h @@ -216,7 +216,11 @@ typedef enum { } FlutterOpenGLTargetType; typedef struct { - /// Target texture of the active texture unit (example GL_TEXTURE_2D). + /// Width of the texture. + size_t width; + /// Height of the texture. + size_t height; + /// Target texture of the active texture unit (example GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE). uint32_t target; /// The name of the texture. uint32_t name; From 235314ae6164bdb98450f2a7851deb103d6637fc Mon Sep 17 00:00:00 2001 From: CloudWebRTC Date: Wed, 25 Sep 2019 11:40:16 +0800 Subject: [PATCH 19/30] clang-format. --- shell/platform/embedder/embedder.cc | 39 ++++++++++++++++------------- shell/platform/embedder/embedder.h | 3 ++- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/shell/platform/embedder/embedder.cc b/shell/platform/embedder/embedder.cc index 495f46917257c..385a40393c78f 100644 --- a/shell/platform/embedder/embedder.cc +++ b/shell/platform/embedder/embedder.cc @@ -45,7 +45,7 @@ extern const intptr_t kPlatformStrongDillSize; const int32_t kFlutterSemanticsNodeIdBatchEnd = -1; const int32_t kFlutterSemanticsCustomActionIdBatchEnd = -1; -#define GL_TEXTURE_RECTANGLE 0x84F5 +#define GL_TEXTURE_RECTANGLE 0x84F5 static FlutterEngineResult LogEmbedderError(FlutterEngineResult code, const char* name, @@ -814,14 +814,15 @@ FlutterEngineResult FlutterEngineRun(size_t version, /// When the texture type is GL_TEXTURE_RECTANGLE, the image size must /// first be set to the actual size, and then resize to the bounds size. - /// see: https://stackoverflow.com/questions/13933503/core-video-pixel-buffers-as-gl-texture-2d - if(texture.target == GL_TEXTURE_RECTANGLE) { + /// see: + /// https://stackoverflow.com/questions/13933503/core-video-pixel-buffers-as-gl-texture-2d + if (texture.target == GL_TEXTURE_RECTANGLE) { width = texture.width; height = texture.height; } - GrBackendTexture gr_backend_texture(width, height, - GrMipMapped::kNo, gr_texture_info); + GrBackendTexture gr_backend_texture(width, height, GrMipMapped::kNo, + gr_texture_info); SkImage::TextureReleaseProc release_proc = texture.destruction_callback; auto image = SkImage::MakeFromTexture( context, // context @@ -834,23 +835,25 @@ FlutterEngineResult FlutterEngineRun(size_t version, texture.user_data // texture release context ); - /// Scale to the bounds size to adapt the user to change the Texture widget size. - if(image && texture.target == GL_TEXTURE_RECTANGLE) { - const auto resized_dimensions = SkISize::Make(size.width(), size.height()); + /// Scale to the bounds size to adapt the user to change the Texture + /// widget size. + if (image && texture.target == GL_TEXTURE_RECTANGLE) { + const auto resized_dimensions = + SkISize::Make(size.width(), size.height()); - if (resized_dimensions != image->dimensions()) { - const auto scaled_image_info = image->imageInfo().makeWH( - resized_dimensions.width(), resized_dimensions.height()); + if (resized_dimensions != image->dimensions()) { + const auto scaled_image_info = image->imageInfo().makeWH( + resized_dimensions.width(), resized_dimensions.height()); - SkBitmap scaled_bitmap; + SkBitmap scaled_bitmap; - if (scaled_bitmap.tryAllocPixels(scaled_image_info) - && image->scalePixels(scaled_bitmap.pixmap(), kLow_SkFilterQuality, - SkImage::kDisallow_CachingHint)) { - scaled_bitmap.setImmutable(); - image = SkImage::MakeFromBitmap(scaled_bitmap); - } + if (scaled_bitmap.tryAllocPixels(scaled_image_info) && + image->scalePixels(scaled_bitmap.pixmap(), kLow_SkFilterQuality, + SkImage::kDisallow_CachingHint)) { + scaled_bitmap.setImmutable(); + image = SkImage::MakeFromBitmap(scaled_bitmap); } + } } if (!image) { diff --git a/shell/platform/embedder/embedder.h b/shell/platform/embedder/embedder.h index 429b4dcd09e1c..bf05ef48b35de 100644 --- a/shell/platform/embedder/embedder.h +++ b/shell/platform/embedder/embedder.h @@ -220,7 +220,8 @@ typedef struct { size_t width; /// Height of the texture. size_t height; - /// Target texture of the active texture unit (example GL_TEXTURE_2D or GL_TEXTURE_RECTANGLE). + /// Target texture of the active texture unit (example GL_TEXTURE_2D or + /// GL_TEXTURE_RECTANGLE). uint32_t target; /// The name of the texture. uint32_t name; From 650c00aa250394d00961fe8b39ce5691144493b7 Mon Sep 17 00:00:00 2001 From: CloudWebRTC Date: Wed, 25 Sep 2019 12:26:04 +0800 Subject: [PATCH 20/30] Fixed FlutterTexture.h import path for ios. --- shell/platform/darwin/ios/BUILD.gn | 1 - shell/platform/darwin/ios/ios_external_texture_gl.h | 2 +- shell/platform/darwin/ios/platform_view_ios.h | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/shell/platform/darwin/ios/BUILD.gn b/shell/platform/darwin/ios/BUILD.gn index 6e00a098ee4ed..13ee3acd6c040 100644 --- a/shell/platform/darwin/ios/BUILD.gn +++ b/shell/platform/darwin/ios/BUILD.gn @@ -30,7 +30,6 @@ _flutter_framework_headers = [ "framework/Headers/FlutterPlatformViews.h", "framework/Headers/FlutterPlugin.h", "framework/Headers/FlutterPluginAppLifeCycleDelegate.h", - "framework/Headers/FlutterTexture.h", "framework/Headers/FlutterViewController.h", ] diff --git a/shell/platform/darwin/ios/ios_external_texture_gl.h b/shell/platform/darwin/ios/ios_external_texture_gl.h index 88be5cc7c466a..586cf53d28149 100644 --- a/shell/platform/darwin/ios/ios_external_texture_gl.h +++ b/shell/platform/darwin/ios/ios_external_texture_gl.h @@ -7,7 +7,7 @@ #include "flutter/flow/texture.h" #include "flutter/fml/platform/darwin/cf_utils.h" -#include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterTexture.h" +#include "flutter/shell/platform/darwin/common/framework/Headers/FlutterTexture.h" namespace flutter { diff --git a/shell/platform/darwin/ios/platform_view_ios.h b/shell/platform/darwin/ios/platform_view_ios.h index eeaca3dd9df50..2dfa3cc817bef 100644 --- a/shell/platform/darwin/ios/platform_view_ios.h +++ b/shell/platform/darwin/ios/platform_view_ios.h @@ -12,7 +12,7 @@ #include "flutter/fml/memory/weak_ptr.h" #include "flutter/fml/platform/darwin/scoped_nsobject.h" #include "flutter/shell/common/platform_view.h" -#include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterTexture.h" +#include "flutter/shell/platform/darwin/common/framework/Headers/FlutterTexture.h" #include "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h" #include "flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h" #include "flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.h" From d4c0be8b21201bcd3fa5f81d9b0613d9cb1a11b6 Mon Sep 17 00:00:00 2001 From: CloudWebRTC Date: Thu, 26 Sep 2019 14:18:47 +0800 Subject: [PATCH 21/30] Update. --- .../Source/FlutterExternalTextureGL.h | 6 +++--- .../Source/FlutterExternalTextureGL.mm | 9 +++++---- shell/platform/embedder/embedder.cc | 20 ++++++++++--------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.h b/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.h index 52e3e489484f8..3fe3b361e38ea 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.h +++ b/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.h @@ -9,7 +9,7 @@ /** * Used to bridge FlutterTexture object and handle the texture copy request the - * flutter engine. + * Flutter engine. */ @interface FlutterExternalTextureGL : NSObject @@ -19,8 +19,8 @@ - (nonnull instancetype)initWithFlutterTexture:(nonnull id)texture; /** - * Accepts texture buffer copy request from the flutter engine. - * When the user side marks the textureId as available, the flutter engine will + * Accepts texture buffer copy request from the Flutter engine. + * When the user side marks the textureID as available, the Flutter engine will * callback to this method and ask for populate the |openGLTexture| object, * such as the texture type and the format of the pixel buffer and the texture object. */ diff --git a/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.mm b/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.mm index 29f945a1f8902..f4e98e1a91934 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.mm @@ -8,8 +8,8 @@ #import #import -static void OnGLTextureRelease(CVPixelBufferRef pixelBuffer) { - CVPixelBufferRelease(pixelBuffer); +static void OnCVOpenGLTextureRelease(CVOpenGLTextureRef cvOpenGLTexture) { + CVOpenGLTextureRelease(cvOpenGLTexture); } @implementation FlutterExternalTextureGL { @@ -72,15 +72,16 @@ - (BOOL)populateTextureWithWidth:(size_t)width CVPixelBufferRelease(_pixelBuffer); return NO; } - CVPixelBufferRelease(_pixelBuffer); openGLTexture->target = static_cast(CVOpenGLTextureGetTarget(cvOpenGLTexture)); openGLTexture->name = static_cast(CVOpenGLTextureGetName(cvOpenGLTexture)); openGLTexture->format = static_cast(GL_RGBA8); - openGLTexture->destruction_callback = (VoidCallback)OnGLTextureRelease; + openGLTexture->destruction_callback = (VoidCallback)OnCVOpenGLTextureRelease; openGLTexture->user_data = cvOpenGLTexture; openGLTexture->width = CVPixelBufferGetWidth(_pixelBuffer); openGLTexture->height = CVPixelBufferGetHeight(_pixelBuffer); + + CVPixelBufferRelease(_pixelBuffer); return YES; } diff --git a/shell/platform/embedder/embedder.cc b/shell/platform/embedder/embedder.cc index 385a40393c78f..c5c7db127d2b3 100644 --- a/shell/platform/embedder/embedder.cc +++ b/shell/platform/embedder/embedder.cc @@ -45,7 +45,8 @@ extern const intptr_t kPlatformStrongDillSize; const int32_t kFlutterSemanticsNodeIdBatchEnd = -1; const int32_t kFlutterSemanticsCustomActionIdBatchEnd = -1; -#define GL_TEXTURE_RECTANGLE 0x84F5 +// Fixed value of GL_TEXTURE_RECTANGLE in OpenGL. +const int32_t kGlTextureRectangle = 0x84F5; static FlutterEngineResult LogEmbedderError(FlutterEngineResult code, const char* name, @@ -812,11 +813,11 @@ FlutterEngineResult FlutterEngineRun(size_t version, size_t width = size.width(); size_t height = size.height(); - /// When the texture type is GL_TEXTURE_RECTANGLE, the image size must - /// first be set to the actual size, and then resize to the bounds size. - /// see: - /// https://stackoverflow.com/questions/13933503/core-video-pixel-buffers-as-gl-texture-2d - if (texture.target == GL_TEXTURE_RECTANGLE) { + // When the texture type is GL_TEXTURE_RECTANGLE, the image size must + // first be set to the actual size, and then resized to the bounds size. + // see: + // https://stackoverflow.com/questions/13933503/core-video-pixel-buffers-as-gl-texture-2d + if (texture.target == kGlTextureRectangle) { width = texture.width; height = texture.height; } @@ -835,9 +836,10 @@ FlutterEngineResult FlutterEngineRun(size_t version, texture.user_data // texture release context ); - /// Scale to the bounds size to adapt the user to change the Texture - /// widget size. - if (image && texture.target == GL_TEXTURE_RECTANGLE) { + // SkImage::MakeFromTexture can automatically scale a GL_TEXTURE_2D texture + // to the bounds size, but GL_TEXTURE_RECTANGLE does not stretch properly, + // so we need to resize the image. + if (image && texture.target == kGlTextureRectangle) { const auto resized_dimensions = SkISize::Make(size.width(), size.height()); From cda947cee28b7dfcbb34d61dd4c39e1776543dbb Mon Sep 17 00:00:00 2001 From: CloudWebRTC Date: Thu, 26 Sep 2019 14:19:30 +0800 Subject: [PATCH 22/30] clang-format. --- shell/platform/embedder/embedder.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/shell/platform/embedder/embedder.cc b/shell/platform/embedder/embedder.cc index c5c7db127d2b3..236c9fcc08684 100644 --- a/shell/platform/embedder/embedder.cc +++ b/shell/platform/embedder/embedder.cc @@ -836,9 +836,9 @@ FlutterEngineResult FlutterEngineRun(size_t version, texture.user_data // texture release context ); - // SkImage::MakeFromTexture can automatically scale a GL_TEXTURE_2D texture - // to the bounds size, but GL_TEXTURE_RECTANGLE does not stretch properly, - // so we need to resize the image. + // SkImage::MakeFromTexture can automatically scale a GL_TEXTURE_2D + // texture to the bounds size, but GL_TEXTURE_RECTANGLE does not stretch + // properly, so we need to resize the image. if (image && texture.target == kGlTextureRectangle) { const auto resized_dimensions = SkISize::Make(size.width(), size.height()); From e78d2e163065b47fec45f100ebeb4d2ae4c67d7f Mon Sep 17 00:00:00 2001 From: CloudWebRTC Date: Fri, 27 Sep 2019 08:38:14 +0800 Subject: [PATCH 23/30] Change _pixelBuffer to a local variable. --- .../Source/FlutterExternalTextureGL.mm | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.mm b/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.mm index f4e98e1a91934..8f503a3cb7c81 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.mm @@ -17,11 +17,6 @@ @implementation FlutterExternalTextureGL { * OpenGL texture cache. */ CVOpenGLTextureCacheRef _openGLTextureCache; - /** - * The pixel buffer copied from the user side will be released - * when the flutter engine renders it. - */ - CVPixelBufferRef _pixelBuffer; /** * User side texture object, used to copy pixel buffer. */ @@ -44,9 +39,9 @@ - (BOOL)populateTextureWithWidth:(size_t)width height:(size_t)height openGLTexture:(FlutterOpenGLTexture*)openGLTexture { // Copy the pixel buffer from the FlutterTexture instance implemented on the user side. - _pixelBuffer = [_texture copyPixelBuffer]; + CVPixelBufferRef pixelBuffer = [_texture copyPixelBuffer]; - if (!_pixelBuffer) { + if (!pixelBuffer) { return NO; } @@ -57,7 +52,7 @@ - (BOOL)populateTextureWithWidth:(size_t)width if (CVOpenGLTextureCacheCreate(kCFAllocatorDefault, NULL, context, format, NULL, &_openGLTextureCache) != kCVReturnSuccess) { NSLog(@"Could not create texture cache."); - CVPixelBufferRelease(_pixelBuffer); + CVPixelBufferRelease(pixelBuffer); return NO; } } @@ -67,9 +62,9 @@ - (BOOL)populateTextureWithWidth:(size_t)width CVOpenGLTextureRef cvOpenGLTexture = NULL; if (CVOpenGLTextureCacheCreateTextureFromImage(kCFAllocatorDefault, _openGLTextureCache, - _pixelBuffer, NULL, + pixelBuffer, NULL, &cvOpenGLTexture) != kCVReturnSuccess) { - CVPixelBufferRelease(_pixelBuffer); + CVPixelBufferRelease(pixelBuffer); return NO; } @@ -78,10 +73,10 @@ - (BOOL)populateTextureWithWidth:(size_t)width openGLTexture->format = static_cast(GL_RGBA8); openGLTexture->destruction_callback = (VoidCallback)OnCVOpenGLTextureRelease; openGLTexture->user_data = cvOpenGLTexture; - openGLTexture->width = CVPixelBufferGetWidth(_pixelBuffer); - openGLTexture->height = CVPixelBufferGetHeight(_pixelBuffer); + openGLTexture->width = CVPixelBufferGetWidth(pixelBuffer); + openGLTexture->height = CVPixelBufferGetHeight(pixelBuffer); - CVPixelBufferRelease(_pixelBuffer); + CVPixelBufferRelease(pixelBuffer); return YES; } From eb3bf39b9c56ba29df6e88a898a49e6017b09a33 Mon Sep 17 00:00:00 2001 From: stuartmorgan Date: Thu, 26 Sep 2019 20:26:39 -0700 Subject: [PATCH 24/30] Add missing static on a local constant --- shell/platform/embedder/embedder.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/platform/embedder/embedder.cc b/shell/platform/embedder/embedder.cc index 236c9fcc08684..c2de7e21fb694 100644 --- a/shell/platform/embedder/embedder.cc +++ b/shell/platform/embedder/embedder.cc @@ -46,7 +46,7 @@ const int32_t kFlutterSemanticsNodeIdBatchEnd = -1; const int32_t kFlutterSemanticsCustomActionIdBatchEnd = -1; // Fixed value of GL_TEXTURE_RECTANGLE in OpenGL. -const int32_t kGlTextureRectangle = 0x84F5; +static const int32_t kGlTextureRectangle = 0x84F5; static FlutterEngineResult LogEmbedderError(FlutterEngineResult code, const char* name, From d622f01818d4f497b854ac039f0c19a9b0296c8e Mon Sep 17 00:00:00 2001 From: stuartmorgan Date: Thu, 26 Sep 2019 20:27:59 -0700 Subject: [PATCH 25/30] Fix variable naming inconsistency --- shell/platform/darwin/macos/framework/Source/FlutterEngine.mm | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm b/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm index 4855528833d2e..3340967464fbd 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm @@ -136,11 +136,11 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine, int64_t texture_identifier, size_t width, size_t height, - FlutterOpenGLTexture* openGLTexture) { + FlutterOpenGLTexture* open_gl_texture) { return [engine populateTextureWithIdentifier:texture_identifier width:width height:height - openGLTexture:openGLTexture]; + openGLTexture:open_gl_texture]; } #pragma mark - From 28719569e2a9e24c04810eefe2957c09cf559e62 Mon Sep 17 00:00:00 2001 From: CloudWebRTC Date: Sat, 5 Oct 2019 13:15:54 +0800 Subject: [PATCH 26/30] Use drawImageRect to draw textures of undesired size. --- shell/platform/embedder/embedder.cc | 36 +++---------------- shell/platform/embedder/embedder.h | 12 ++++--- .../embedder/embedder_external_texture_gl.cc | 6 +++- 3 files changed, 18 insertions(+), 36 deletions(-) diff --git a/shell/platform/embedder/embedder.cc b/shell/platform/embedder/embedder.cc index 236c9fcc08684..61f2bbf9e2494 100644 --- a/shell/platform/embedder/embedder.cc +++ b/shell/platform/embedder/embedder.cc @@ -45,9 +45,6 @@ extern const intptr_t kPlatformStrongDillSize; const int32_t kFlutterSemanticsNodeIdBatchEnd = -1; const int32_t kFlutterSemanticsCustomActionIdBatchEnd = -1; -// Fixed value of GL_TEXTURE_RECTANGLE in OpenGL. -const int32_t kGlTextureRectangle = 0x84F5; - static FlutterEngineResult LogEmbedderError(FlutterEngineResult code, const char* name, const char* function, @@ -800,7 +797,10 @@ FlutterEngineResult FlutterEngineRun(size_t version, [ptr = open_gl_config->gl_external_texture_frame_callback, user_data]( int64_t texture_identifier, GrContext* context, const SkISize& size) -> sk_sp { - FlutterOpenGLTexture texture = {}; + FlutterOpenGLTexture texture = { + .width = 0, + .height = 0, + }; if (!ptr(user_data, texture_identifier, size.width(), size.height(), &texture)) { @@ -813,11 +813,7 @@ FlutterEngineResult FlutterEngineRun(size_t version, size_t width = size.width(); size_t height = size.height(); - // When the texture type is GL_TEXTURE_RECTANGLE, the image size must - // first be set to the actual size, and then resized to the bounds size. - // see: - // https://stackoverflow.com/questions/13933503/core-video-pixel-buffers-as-gl-texture-2d - if (texture.target == kGlTextureRectangle) { + if (texture.width != 0 && texture.height != 0) { width = texture.width; height = texture.height; } @@ -836,28 +832,6 @@ FlutterEngineResult FlutterEngineRun(size_t version, texture.user_data // texture release context ); - // SkImage::MakeFromTexture can automatically scale a GL_TEXTURE_2D - // texture to the bounds size, but GL_TEXTURE_RECTANGLE does not stretch - // properly, so we need to resize the image. - if (image && texture.target == kGlTextureRectangle) { - const auto resized_dimensions = - SkISize::Make(size.width(), size.height()); - - if (resized_dimensions != image->dimensions()) { - const auto scaled_image_info = image->imageInfo().makeWH( - resized_dimensions.width(), resized_dimensions.height()); - - SkBitmap scaled_bitmap; - - if (scaled_bitmap.tryAllocPixels(scaled_image_info) && - image->scalePixels(scaled_bitmap.pixmap(), kLow_SkFilterQuality, - SkImage::kDisallow_CachingHint)) { - scaled_bitmap.setImmutable(); - image = SkImage::MakeFromBitmap(scaled_bitmap); - } - } - } - if (!image) { // In case Skia rejects the image, call the release proc so that // embedders can perform collection of intermediates. diff --git a/shell/platform/embedder/embedder.h b/shell/platform/embedder/embedder.h index bf05ef48b35de..235e1af89d9b6 100644 --- a/shell/platform/embedder/embedder.h +++ b/shell/platform/embedder/embedder.h @@ -216,10 +216,6 @@ typedef enum { } FlutterOpenGLTargetType; typedef struct { - /// Width of the texture. - size_t width; - /// Height of the texture. - size_t height; /// Target texture of the active texture unit (example GL_TEXTURE_2D or /// GL_TEXTURE_RECTANGLE). uint32_t target; @@ -232,6 +228,14 @@ typedef struct { /// Callback invoked (on an engine managed thread) that asks the embedder to /// collect the texture. VoidCallback destruction_callback; + /// Optional parameters for texture height/width, default is 0, non-zero means + /// the texture has the specified width/height. Usually, when the texture type + /// is GL_TEXTURE_RECTANGLE, we need to specify the texture width/height to + /// tell the embedder to scale when rendering. + /// Width of the texture. + size_t width; + /// Height of the texture. + size_t height; } FlutterOpenGLTexture; typedef struct { diff --git a/shell/platform/embedder/embedder_external_texture_gl.cc b/shell/platform/embedder/embedder_external_texture_gl.cc index b9857ae4ee363..cc71c1f687434 100644 --- a/shell/platform/embedder/embedder_external_texture_gl.cc +++ b/shell/platform/embedder/embedder_external_texture_gl.cc @@ -31,7 +31,11 @@ void EmbedderExternalTextureGL::Paint(SkCanvas& canvas, } if (last_image_) { - canvas.drawImage(last_image_, bounds.x(), bounds.y()); + if(bounds != SkRect::Make(last_image_->bounds())) { + canvas.drawImageRect(last_image_, bounds, nullptr); + }else { + canvas.drawImage(last_image_, bounds.x(), bounds.y()); + } } } From 4962f91f223bf15401c2ca0cbb586c3e4b9428ec Mon Sep 17 00:00:00 2001 From: CloudWebRTC Date: Sat, 5 Oct 2019 13:20:24 +0800 Subject: [PATCH 27/30] clang-format. --- shell/platform/embedder/embedder_external_texture_gl.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shell/platform/embedder/embedder_external_texture_gl.cc b/shell/platform/embedder/embedder_external_texture_gl.cc index cc71c1f687434..46c494eb9a12f 100644 --- a/shell/platform/embedder/embedder_external_texture_gl.cc +++ b/shell/platform/embedder/embedder_external_texture_gl.cc @@ -31,9 +31,9 @@ void EmbedderExternalTextureGL::Paint(SkCanvas& canvas, } if (last_image_) { - if(bounds != SkRect::Make(last_image_->bounds())) { + if (bounds != SkRect::Make(last_image_->bounds())) { canvas.drawImageRect(last_image_, bounds, nullptr); - }else { + } else { canvas.drawImage(last_image_, bounds.x(), bounds.y()); } } From b20be2c0990e7986299521ad4bc31adcf2fb5abb Mon Sep 17 00:00:00 2001 From: CloudWebRTC Date: Sat, 5 Oct 2019 13:42:24 +0800 Subject: [PATCH 28/30] Fixed CI errors. --- shell/platform/embedder/embedder.cc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/shell/platform/embedder/embedder.cc b/shell/platform/embedder/embedder.cc index 61f2bbf9e2494..9fab591f6e693 100644 --- a/shell/platform/embedder/embedder.cc +++ b/shell/platform/embedder/embedder.cc @@ -797,10 +797,9 @@ FlutterEngineResult FlutterEngineRun(size_t version, [ptr = open_gl_config->gl_external_texture_frame_callback, user_data]( int64_t texture_identifier, GrContext* context, const SkISize& size) -> sk_sp { - FlutterOpenGLTexture texture = { - .width = 0, - .height = 0, - }; + FlutterOpenGLTexture texture = {}; + texture.width = 0; + texture.height = 0; if (!ptr(user_data, texture_identifier, size.width(), size.height(), &texture)) { From 067e8a135fcec74b4d6f075e75f08cdd8e57f75f Mon Sep 17 00:00:00 2001 From: CloudWebRTC Date: Mon, 7 Oct 2019 12:53:11 +0800 Subject: [PATCH 29/30] update. --- .../darwin/macos/framework/Source/FlutterEngine.mm | 10 +--------- .../macos/framework/Source/FlutterExternalTextureGL.h | 4 +--- .../macos/framework/Source/FlutterExternalTextureGL.mm | 4 +--- shell/platform/embedder/embedder.cc | 2 -- 4 files changed, 3 insertions(+), 17 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm b/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm index 3340967464fbd..c0cf048135091 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm @@ -51,8 +51,6 @@ - (void)shutDownEngine; * Forwards texture copy request to the corresponding texture via |textureID|. */ - (BOOL)populateTextureWithIdentifier:(int64_t)textureID - width:(size_t)width - height:(size_t)height openGLTexture:(FlutterOpenGLTexture*)openGLTexture; @end @@ -138,8 +136,6 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine, size_t height, FlutterOpenGLTexture* open_gl_texture) { return [engine populateTextureWithIdentifier:texture_identifier - width:width - height:height openGLTexture:open_gl_texture]; } @@ -430,12 +426,8 @@ - (void)setMessageHandlerOnChannel:(nonnull NSString*)channel #pragma mark - FlutterTextureRegistrar - (BOOL)populateTextureWithIdentifier:(int64_t)textureID - width:(size_t)width - height:(size_t)height openGLTexture:(FlutterOpenGLTexture*)openGLTexture { - return [_textures[@(textureID)] populateTextureWithWidth:width - height:height - openGLTexture:openGLTexture]; + return [_textures[@(textureID)] populateTexture:openGLTexture]; } - (int64_t)registerTexture:(id)texture { diff --git a/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.h b/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.h index 3fe3b361e38ea..5fad0ab1924ca 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.h +++ b/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.h @@ -24,9 +24,7 @@ * callback to this method and ask for populate the |openGLTexture| object, * such as the texture type and the format of the pixel buffer and the texture object. */ -- (BOOL)populateTextureWithWidth:(size_t)width - height:(size_t)height - openGLTexture:(nonnull FlutterOpenGLTexture*)openGLTexture; +- (BOOL)populateTexture:(nonnull FlutterOpenGLTexture*)openGLTexture; /** * Returns the ID for the FlutterTexture instance. diff --git a/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.mm b/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.mm index 8f503a3cb7c81..0314280e9f12d 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.mm @@ -35,9 +35,7 @@ - (int64_t)textureID { return reinterpret_cast(self); } -- (BOOL)populateTextureWithWidth:(size_t)width - height:(size_t)height - openGLTexture:(FlutterOpenGLTexture*)openGLTexture { +- (BOOL)populateTexture:(FlutterOpenGLTexture*)openGLTexture { // Copy the pixel buffer from the FlutterTexture instance implemented on the user side. CVPixelBufferRef pixelBuffer = [_texture copyPixelBuffer]; diff --git a/shell/platform/embedder/embedder.cc b/shell/platform/embedder/embedder.cc index 9fab591f6e693..0a62ea71453b2 100644 --- a/shell/platform/embedder/embedder.cc +++ b/shell/platform/embedder/embedder.cc @@ -798,8 +798,6 @@ FlutterEngineResult FlutterEngineRun(size_t version, int64_t texture_identifier, GrContext* context, const SkISize& size) -> sk_sp { FlutterOpenGLTexture texture = {}; - texture.width = 0; - texture.height = 0; if (!ptr(user_data, texture_identifier, size.width(), size.height(), &texture)) { From 27c6f949db5c6fad1f80e9b17ab9bcc66d994019 Mon Sep 17 00:00:00 2001 From: CloudWebRTC Date: Mon, 7 Oct 2019 13:00:00 +0800 Subject: [PATCH 30/30] clang-format. --- shell/platform/darwin/macos/framework/Source/FlutterEngine.mm | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm b/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm index c0cf048135091..42044b9c13542 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm @@ -135,8 +135,7 @@ static bool OnAcquireExternalTexture(FlutterEngine* engine, size_t width, size_t height, FlutterOpenGLTexture* open_gl_texture) { - return [engine populateTextureWithIdentifier:texture_identifier - openGLTexture:open_gl_texture]; + return [engine populateTextureWithIdentifier:texture_identifier openGLTexture:open_gl_texture]; } #pragma mark -