forked from flutter/flutter
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a wrapper for IOSurface to handle creation and (flutter#22663)
binding IOSurfaces to textures / framebuffers.
- Loading branch information
1 parent
2eea247
commit 07af6a9
Showing
5 changed files
with
113 additions
and
44 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
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
29 changes: 29 additions & 0 deletions
29
shell/platform/darwin/macos/framework/Source/FlutterIOSurfaceHolder.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,29 @@ | ||
// 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 <Cocoa/Cocoa.h> | ||
|
||
/** | ||
* FlutterIOSurfaceHolder maintains an IOSurface | ||
* and provides an interface to bind the IOSurface to a texture. | ||
*/ | ||
@interface FlutterIOSurfaceHolder : NSObject | ||
|
||
/** | ||
* Bind the IOSurface to the provided texture and fbo. | ||
*/ | ||
- (void)bindSurfaceToTexture:(GLuint)texture fbo:(GLuint)fbo size:(CGSize)size; | ||
|
||
/** | ||
* Releases the current IOSurface if one exists | ||
* and creates a new IOSurface with the specified size. | ||
*/ | ||
- (void)recreateIOSurfaceWithSize:(CGSize)size; | ||
|
||
/** | ||
* Returns a reference to the underlying IOSurface. | ||
*/ | ||
- (const IOSurfaceRef&)ioSurface; | ||
|
||
@end |
66 changes: 66 additions & 0 deletions
66
shell/platform/darwin/macos/framework/Source/FlutterIOSurfaceHolder.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,66 @@ | ||
// 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/FlutterIOSurfaceHolder.h" | ||
|
||
#include <OpenGL/gl.h> | ||
|
||
@interface FlutterIOSurfaceHolder () { | ||
IOSurfaceRef _ioSurface; | ||
} | ||
@end | ||
|
||
@implementation FlutterIOSurfaceHolder | ||
|
||
- (void)bindSurfaceToTexture:(GLuint)texture fbo:(GLuint)fbo size:(CGSize)size { | ||
[self recreateIOSurfaceWithSize:size]; | ||
|
||
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture); | ||
|
||
CGLTexImageIOSurface2D(CGLGetCurrentContext(), GL_TEXTURE_RECTANGLE_ARB, GL_RGBA, int(size.width), | ||
int(size.height), GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, _ioSurface, | ||
0 /* plane */); | ||
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); | ||
|
||
glBindFramebuffer(GL_FRAMEBUFFER, fbo); | ||
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB, texture, | ||
0); | ||
|
||
NSAssert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE, | ||
@"Framebuffer status check failed"); | ||
} | ||
|
||
- (void)recreateIOSurfaceWithSize:(CGSize)size { | ||
if (_ioSurface) { | ||
CFRelease(_ioSurface); | ||
} | ||
|
||
unsigned pixelFormat = 'BGRA'; | ||
unsigned bytesPerElement = 4; | ||
|
||
size_t bytesPerRow = IOSurfaceAlignProperty(kIOSurfaceBytesPerRow, size.width * bytesPerElement); | ||
size_t totalBytes = IOSurfaceAlignProperty(kIOSurfaceAllocSize, size.height * bytesPerRow); | ||
NSDictionary* options = @{ | ||
(id)kIOSurfaceWidth : @(size.width), | ||
(id)kIOSurfaceHeight : @(size.height), | ||
(id)kIOSurfacePixelFormat : @(pixelFormat), | ||
(id)kIOSurfaceBytesPerElement : @(bytesPerElement), | ||
(id)kIOSurfaceBytesPerRow : @(bytesPerRow), | ||
(id)kIOSurfaceAllocSize : @(totalBytes), | ||
}; | ||
|
||
_ioSurface = IOSurfaceCreate((CFDictionaryRef)options); | ||
} | ||
|
||
- (const IOSurfaceRef&)ioSurface { | ||
return _ioSurface; | ||
} | ||
|
||
- (void)dealloc { | ||
if (_ioSurface) { | ||
CFRelease(_ioSurface); | ||
} | ||
} | ||
|
||
@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