From 89b1746df3df9cb619dd314988715fcbaa9f7d70 Mon Sep 17 00:00:00 2001 From: richardjcai Date: Fri, 20 Nov 2020 17:07:10 -0500 Subject: [PATCH] Create FlutterFrameBufferProvider class. Refactor FlutterSurfaceManager to use FlutterFrameBufferProvider. FlutterFrameBufferProvider will be used to uncouple FlutterGLCompositor for MacOS and FlutterSurfaceManager. --- ci/licenses_golden/licenses_flutter | 2 + shell/platform/darwin/macos/BUILD.gn | 2 + .../Source/FlutterFrameBufferProvider.h | 24 +++++++++ .../Source/FlutterFrameBufferProvider.mm | 53 +++++++++++++++++++ .../framework/Source/FlutterSurfaceManager.mm | 38 ++++--------- 5 files changed, 92 insertions(+), 27 deletions(-) create mode 100644 shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProvider.h create mode 100644 shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProvider.mm diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index d7eb1dac4dc2a..b70e0f86c285c 100755 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -1054,6 +1054,8 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterEngin 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/FlutterFrameBufferProvider.h +FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProvider.mm FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMouseCursorPlugin.h FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMouseCursorPlugin.mm FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h diff --git a/shell/platform/darwin/macos/BUILD.gn b/shell/platform/darwin/macos/BUILD.gn index 4c345a008c19c..6731939cf7975 100644 --- a/shell/platform/darwin/macos/BUILD.gn +++ b/shell/platform/darwin/macos/BUILD.gn @@ -52,6 +52,8 @@ source_set("flutter_framework_source") { "framework/Source/FlutterEngine_Internal.h", "framework/Source/FlutterExternalTextureGL.h", "framework/Source/FlutterExternalTextureGL.mm", + "framework/Source/FlutterFrameBufferProvider.h", + "framework/Source/FlutterFrameBufferProvider.mm", "framework/Source/FlutterMouseCursorPlugin.h", "framework/Source/FlutterMouseCursorPlugin.mm", "framework/Source/FlutterResizeSynchronizer.h", diff --git a/shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProvider.h b/shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProvider.h new file mode 100644 index 0000000000000..f07e9ee2cd5ef --- /dev/null +++ b/shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProvider.h @@ -0,0 +1,24 @@ +// 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 + +/** + * Creates framebuffers and their backing textures. + */ +@interface FlutterFrameBufferProvider : NSObject + +- (nullable instancetype)initWithOpenGLContext:(nonnull NSOpenGLContext*)opengLContext; + +/** + * Returns the id of the framebuffer. + */ +- (uint32_t)glFrameBufferId; + +/** + * Returns the id of the backing texture.. + */ +- (uint32_t)glTextureId; + +@end diff --git a/shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProvider.mm b/shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProvider.mm new file mode 100644 index 0000000000000..c7d4ff463c611 --- /dev/null +++ b/shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProvider.mm @@ -0,0 +1,53 @@ +// 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/FlutterFrameBufferProvider.h" + +#include + +#import "flutter/shell/platform/darwin/macos/framework/Source/MacOSGLContextSwitch.h" + +@interface FlutterFrameBufferProvider () { + uint32_t _frameBufferId; + uint32_t _backingTexture; +} +@end + +@implementation FlutterFrameBufferProvider +- (instancetype)initWithOpenGLContext:(NSOpenGLContext*)openGLContext { + if (self = [super init]) { + MacOSGLContextSwitch context_switch(openGLContext); + + glGenFramebuffers(1, &_frameBufferId); + glGenTextures(1, &_backingTexture); + + [self createFramebuffer]; + } + return self; +} + +- (void)createFramebuffer { + glBindFramebuffer(GL_FRAMEBUFFER, _frameBufferId); + glBindTexture(GL_TEXTURE_RECTANGLE_ARB, _backingTexture); + glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); +} + +- (uint32_t)glFrameBufferId { + return _frameBufferId; +} + +- (uint32_t)glTextureId { + return _backingTexture; +} + +- (void)dealloc { + glDeleteFramebuffers(1, &_frameBufferId); + glDeleteTextures(1, &_backingTexture); +} + +@end diff --git a/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.mm b/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.mm index c316d7359245f..a1076bb395eb4 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.mm +++ b/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.mm @@ -3,6 +3,7 @@ // found in the LICENSE file. #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h" +#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProvider.h" #include @@ -20,9 +21,9 @@ @interface FlutterSurfaceManager () { CALayer* _contentLayer; NSOpenGLContext* _openGLContext; - uint32_t _frameBufferId[kFlutterSurfaceManagerBufferCount]; - uint32_t _backingTexture[kFlutterSurfaceManagerBufferCount]; + IOSurfaceRef _ioSurface[kFlutterSurfaceManagerBufferCount]; + FlutterFrameBufferProvider* _frameBuffers[kFlutterSurfaceManagerBufferCount]; } @end @@ -39,27 +40,12 @@ - (instancetype)initWithLayer:(CALayer*)containingLayer _contentLayer = [[CALayer alloc] init]; [_containingLayer addSublayer:_contentLayer]; - MacOSGLContextSwitch context_switch(openGLContext); - - glGenFramebuffers(2, _frameBufferId); - glGenTextures(2, _backingTexture); - - [self createFramebuffer:_frameBufferId[0] withBackingTexture:_backingTexture[0]]; - [self createFramebuffer:_frameBufferId[1] withBackingTexture:_backingTexture[1]]; + _frameBuffers[0] = [[FlutterFrameBufferProvider alloc] initWithOpenGLContext:_openGLContext]; + _frameBuffers[1] = [[FlutterFrameBufferProvider alloc] initWithOpenGLContext:_openGLContext]; } return self; } -- (void)createFramebuffer:(uint32_t)fbo withBackingTexture:(uint32_t)texture { - glBindFramebuffer(GL_FRAMEBUFFER, fbo); - glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture); - glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); -} - - (void)ensureSurfaceSize:(CGSize)size { if (CGSizeEqualToSize(size, _surfaceSize)) { return; @@ -88,16 +74,16 @@ - (void)ensureSurfaceSize:(CGSize)size { }; _ioSurface[i] = IOSurfaceCreate((CFDictionaryRef)options); - glBindTexture(GL_TEXTURE_RECTANGLE_ARB, _backingTexture[i]); + glBindTexture(GL_TEXTURE_RECTANGLE_ARB, [_frameBuffers[i] glTextureId]); 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[i], 0 /* plane */); glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); - glBindFramebuffer(GL_FRAMEBUFFER, _frameBufferId[i]); + glBindFramebuffer(GL_FRAMEBUFFER, [_frameBuffers[i] glFrameBufferId]); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB, - _backingTexture[i], 0); + [_frameBuffers[i] glTextureId], 0); NSAssert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE, @"Framebuffer status check failed"); @@ -114,14 +100,12 @@ - (void)swapBuffers { std::swap(_ioSurface[kFlutterSurfaceManagerBackBuffer], _ioSurface[kFlutterSurfaceManagerFrontBuffer]); - std::swap(_frameBufferId[kFlutterSurfaceManagerBackBuffer], - _frameBufferId[kFlutterSurfaceManagerFrontBuffer]); - std::swap(_backingTexture[kFlutterSurfaceManagerBackBuffer], - _backingTexture[kFlutterSurfaceManagerFrontBuffer]); + std::swap(_frameBuffers[kFlutterSurfaceManagerBackBuffer], + _frameBuffers[kFlutterSurfaceManagerFrontBuffer]); } - (uint32_t)glFrameBufferId { - return _frameBufferId[kFlutterSurfaceManagerBackBuffer]; + return [_frameBuffers[kFlutterSurfaceManagerBackBuffer] glFrameBufferId]; } - (void)dealloc {