-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add texture support for macOS shell. (#8507)
- Loading branch information
1 parent
179fab9
commit 0f9d88c
Showing
16 changed files
with
215 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterTexture.h" | ||
#import "flutter/shell/platform/embedder/embedder.h" | ||
|
||
/** | ||
* Used to bridge FlutterTexture object and handle the texture copy request the | ||
* Flutter engine. | ||
*/ | ||
@interface FlutterExternalTextureGL : NSObject | ||
|
||
/** | ||
* Initializes a texture adapter with |texture| return a instance. | ||
*/ | ||
- (nonnull instancetype)initWithFlutterTexture:(nonnull id<FlutterTexture>)texture; | ||
|
||
/** | ||
* 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)populateTexture:(nonnull FlutterOpenGLTexture*)openGLTexture; | ||
|
||
/** | ||
* Returns the ID for the FlutterTexture instance. | ||
*/ | ||
- (int64_t)textureID; | ||
|
||
@end |
85 changes: 85 additions & 0 deletions
85
shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// 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/FlutterExternalTextureGL.h" | ||
|
||
#import <AppKit/AppKit.h> | ||
#import <CoreVideo/CoreVideo.h> | ||
#import <OpenGL/gl.h> | ||
|
||
static void OnCVOpenGLTextureRelease(CVOpenGLTextureRef cvOpenGLTexture) { | ||
CVOpenGLTextureRelease(cvOpenGLTexture); | ||
} | ||
|
||
@implementation FlutterExternalTextureGL { | ||
/** | ||
* OpenGL texture cache. | ||
*/ | ||
CVOpenGLTextureCacheRef _openGLTextureCache; | ||
/** | ||
* User side texture object, used to copy pixel buffer. | ||
*/ | ||
id<FlutterTexture> _texture; | ||
} | ||
|
||
- (instancetype)initWithFlutterTexture:(id<FlutterTexture>)texture { | ||
self = [super init]; | ||
if (self) { | ||
_texture = texture; | ||
} | ||
return self; | ||
} | ||
|
||
- (int64_t)textureID { | ||
return reinterpret_cast<int64_t>(self); | ||
} | ||
|
||
- (BOOL)populateTexture:(FlutterOpenGLTexture*)openGLTexture { | ||
// Copy the pixel buffer from the FlutterTexture instance implemented on the user side. | ||
CVPixelBufferRef pixelBuffer = [_texture copyPixelBuffer]; | ||
|
||
if (!pixelBuffer) { | ||
return NO; | ||
} | ||
|
||
// 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, | ||
&_openGLTextureCache) != kCVReturnSuccess) { | ||
NSLog(@"Could not create texture cache."); | ||
CVPixelBufferRelease(pixelBuffer); | ||
return NO; | ||
} | ||
} | ||
|
||
// Try to clear the cache of OpenGL textures to save memory. | ||
CVOpenGLTextureCacheFlush(_openGLTextureCache, 0); | ||
|
||
CVOpenGLTextureRef cvOpenGLTexture = NULL; | ||
if (CVOpenGLTextureCacheCreateTextureFromImage(kCFAllocatorDefault, _openGLTextureCache, | ||
pixelBuffer, NULL, | ||
&cvOpenGLTexture) != kCVReturnSuccess) { | ||
CVPixelBufferRelease(pixelBuffer); | ||
return NO; | ||
} | ||
|
||
openGLTexture->target = static_cast<uint32_t>(CVOpenGLTextureGetTarget(cvOpenGLTexture)); | ||
openGLTexture->name = static_cast<uint32_t>(CVOpenGLTextureGetName(cvOpenGLTexture)); | ||
openGLTexture->format = static_cast<uint32_t>(GL_RGBA8); | ||
openGLTexture->destruction_callback = (VoidCallback)OnCVOpenGLTextureRelease; | ||
openGLTexture->user_data = cvOpenGLTexture; | ||
openGLTexture->width = CVPixelBufferGetWidth(pixelBuffer); | ||
openGLTexture->height = CVPixelBufferGetHeight(pixelBuffer); | ||
|
||
CVPixelBufferRelease(pixelBuffer); | ||
return YES; | ||
} | ||
|
||
- (void)dealloc { | ||
CVOpenGLTextureCacheRelease(_openGLTextureCache); | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.