44
55#import " flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h"
66#import " flutter/shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProvider.h"
7+ #import " flutter/shell/platform/darwin/macos/framework/Source/FlutterIOSurfaceHolder.h"
78
89#include < OpenGL/gl.h>
910
@@ -22,7 +23,7 @@ @interface FlutterSurfaceManager () {
2223
2324 NSOpenGLContext * _openGLContext;
2425
25- IOSurfaceRef _ioSurface [kFlutterSurfaceManagerBufferCount ];
26+ FlutterIOSurfaceHolder* _ioSurfaces [kFlutterSurfaceManagerBufferCount ];
2627 FlutterFrameBufferProvider* _frameBuffers[kFlutterSurfaceManagerBufferCount ];
2728}
2829@end
@@ -42,6 +43,9 @@ - (instancetype)initWithLayer:(CALayer*)containingLayer
4243
4344 _frameBuffers[0 ] = [[FlutterFrameBufferProvider alloc ] initWithOpenGLContext: _openGLContext];
4445 _frameBuffers[1 ] = [[FlutterFrameBufferProvider alloc ] initWithOpenGLContext: _openGLContext];
46+
47+ _ioSurfaces[0 ] = [FlutterIOSurfaceHolder alloc ];
48+ _ioSurfaces[1 ] = [FlutterIOSurfaceHolder alloc ];
4549 }
4650 return self;
4751}
@@ -55,38 +59,11 @@ - (void)ensureSurfaceSize:(CGSize)size {
5559 MacOSGLContextSwitch context_switch (_openGLContext);
5660
5761 for (int i = 0 ; i < kFlutterSurfaceManagerBufferCount ; ++i) {
58- if (_ioSurface[i]) {
59- CFRelease (_ioSurface[i]);
60- }
61- unsigned pixelFormat = ' BGRA' ;
62- unsigned bytesPerElement = 4 ;
63-
64- size_t bytesPerRow =
65- IOSurfaceAlignProperty (kIOSurfaceBytesPerRow , size.width * bytesPerElement);
66- size_t totalBytes = IOSurfaceAlignProperty (kIOSurfaceAllocSize , size.height * bytesPerRow);
67- NSDictionary * options = @{
68- (id )kIOSurfaceWidth : @(size.width ),
69- (id )kIOSurfaceHeight : @(size.height ),
70- (id )kIOSurfacePixelFormat : @(pixelFormat),
71- (id )kIOSurfaceBytesPerElement : @(bytesPerElement),
72- (id )kIOSurfaceBytesPerRow : @(bytesPerRow),
73- (id )kIOSurfaceAllocSize : @(totalBytes),
74- };
75- _ioSurface[i] = IOSurfaceCreate ((CFDictionaryRef)options);
76-
77- glBindTexture (GL_TEXTURE_RECTANGLE_ARB, [_frameBuffers[i] glTextureId ]);
78-
79- CGLTexImageIOSurface2D (CGLGetCurrentContext (), GL_TEXTURE_RECTANGLE_ARB, GL_RGBA,
80- int (size.width ), int (size.height ), GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
81- _ioSurface[i], 0 /* plane */ );
82- glBindTexture (GL_TEXTURE_RECTANGLE_ARB, 0 );
83-
84- glBindFramebuffer (GL_FRAMEBUFFER, [_frameBuffers[i] glFrameBufferId ]);
85- glFramebufferTexture2D (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB,
86- [_frameBuffers[i] glTextureId ], 0 );
87-
88- NSAssert (glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE,
89- @"Framebuffer status check failed");
62+ [_ioSurfaces[i] recreateIOSurfaceWithSize: size];
63+
64+ GLuint fbo = [_frameBuffers[i] glFrameBufferId ];
65+ GLuint texture = [_frameBuffers[i] glTextureId ];
66+ [_ioSurfaces[i] bindSurfaceToTexture: texture fbo: fbo size: size];
9067 }
9168}
9269
@@ -96,10 +73,11 @@ - (void)swapBuffers {
9673 // The surface is an OpenGL texture, which means it has origin in bottom left corner
9774 // and needs to be flipped vertically
9875 _contentLayer.transform = CATransform3DMakeScale (1 , -1 , 1 );
99- [_contentLayer setContents: (__bridge id )_ioSurface[kFlutterSurfaceManagerBackBuffer ]];
76+ IOSurfaceRef contentIOSurface = [_ioSurfaces[kFlutterSurfaceManagerBackBuffer ] ioSurface ];
77+ [_contentLayer setContents: (__bridge id )contentIOSurface];
10078
101- std::swap (_ioSurface [kFlutterSurfaceManagerBackBuffer ],
102- _ioSurface [kFlutterSurfaceManagerFrontBuffer ]);
79+ std::swap (_ioSurfaces [kFlutterSurfaceManagerBackBuffer ],
80+ _ioSurfaces [kFlutterSurfaceManagerFrontBuffer ]);
10381 std::swap (_frameBuffers[kFlutterSurfaceManagerBackBuffer ],
10482 _frameBuffers[kFlutterSurfaceManagerFrontBuffer ]);
10583}
@@ -108,12 +86,4 @@ - (uint32_t)glFrameBufferId {
10886 return [_frameBuffers[kFlutterSurfaceManagerBackBuffer ] glFrameBufferId ];
10987}
11088
111- - (void )dealloc {
112- for (int i = 0 ; i < kFlutterSurfaceManagerBufferCount ; ++i) {
113- if (_ioSurface[i]) {
114- CFRelease (_ioSurface[i]);
115- }
116- }
117- }
118-
11989@end
0 commit comments