Skip to content

Commit 5224003

Browse files
RichardJCaichaselatta
authored andcommitted
Create FlutterFrameBufferProvider class. (flutter#22656)
1 parent 824ded4 commit 5224003

File tree

5 files changed

+92
-27
lines changed

5 files changed

+92
-27
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,8 @@ FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterEngin
10541054
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h
10551055
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.h
10561056
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterExternalTextureGL.mm
1057+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProvider.h
1058+
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProvider.mm
10571059
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMouseCursorPlugin.h
10581060
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterMouseCursorPlugin.mm
10591061
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Source/FlutterResizeSynchronizer.h

shell/platform/darwin/macos/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ source_set("flutter_framework_source") {
5252
"framework/Source/FlutterEngine_Internal.h",
5353
"framework/Source/FlutterExternalTextureGL.h",
5454
"framework/Source/FlutterExternalTextureGL.mm",
55+
"framework/Source/FlutterFrameBufferProvider.h",
56+
"framework/Source/FlutterFrameBufferProvider.mm",
5557
"framework/Source/FlutterMouseCursorPlugin.h",
5658
"framework/Source/FlutterMouseCursorPlugin.mm",
5759
"framework/Source/FlutterResizeSynchronizer.h",
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#import <Cocoa/Cocoa.h>
6+
7+
/**
8+
* Creates framebuffers and their backing textures.
9+
*/
10+
@interface FlutterFrameBufferProvider : NSObject
11+
12+
- (nullable instancetype)initWithOpenGLContext:(nonnull NSOpenGLContext*)opengLContext;
13+
14+
/**
15+
* Returns the id of the framebuffer.
16+
*/
17+
- (uint32_t)glFrameBufferId;
18+
19+
/**
20+
* Returns the id of the backing texture..
21+
*/
22+
- (uint32_t)glTextureId;
23+
24+
@end
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProvider.h"
6+
7+
#include <OpenGL/gl.h>
8+
9+
#import "flutter/shell/platform/darwin/macos/framework/Source/MacOSGLContextSwitch.h"
10+
11+
@interface FlutterFrameBufferProvider () {
12+
uint32_t _frameBufferId;
13+
uint32_t _backingTexture;
14+
}
15+
@end
16+
17+
@implementation FlutterFrameBufferProvider
18+
- (instancetype)initWithOpenGLContext:(NSOpenGLContext*)openGLContext {
19+
if (self = [super init]) {
20+
MacOSGLContextSwitch context_switch(openGLContext);
21+
22+
glGenFramebuffers(1, &_frameBufferId);
23+
glGenTextures(1, &_backingTexture);
24+
25+
[self createFramebuffer];
26+
}
27+
return self;
28+
}
29+
30+
- (void)createFramebuffer {
31+
glBindFramebuffer(GL_FRAMEBUFFER, _frameBufferId);
32+
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, _backingTexture);
33+
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
34+
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
35+
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
36+
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
37+
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
38+
}
39+
40+
- (uint32_t)glFrameBufferId {
41+
return _frameBufferId;
42+
}
43+
44+
- (uint32_t)glTextureId {
45+
return _backingTexture;
46+
}
47+
48+
- (void)dealloc {
49+
glDeleteFramebuffers(1, &_frameBufferId);
50+
glDeleteTextures(1, &_backingTexture);
51+
}
52+
53+
@end

shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.mm

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterSurfaceManager.h"
6+
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterFrameBufferProvider.h"
67

78
#include <OpenGL/gl.h>
89

@@ -20,9 +21,9 @@ @interface FlutterSurfaceManager () {
2021
CALayer* _contentLayer;
2122

2223
NSOpenGLContext* _openGLContext;
23-
uint32_t _frameBufferId[kFlutterSurfaceManagerBufferCount];
24-
uint32_t _backingTexture[kFlutterSurfaceManagerBufferCount];
24+
2525
IOSurfaceRef _ioSurface[kFlutterSurfaceManagerBufferCount];
26+
FlutterFrameBufferProvider* _frameBuffers[kFlutterSurfaceManagerBufferCount];
2627
}
2728
@end
2829

@@ -39,27 +40,12 @@ - (instancetype)initWithLayer:(CALayer*)containingLayer
3940
_contentLayer = [[CALayer alloc] init];
4041
[_containingLayer addSublayer:_contentLayer];
4142

42-
MacOSGLContextSwitch context_switch(openGLContext);
43-
44-
glGenFramebuffers(2, _frameBufferId);
45-
glGenTextures(2, _backingTexture);
46-
47-
[self createFramebuffer:_frameBufferId[0] withBackingTexture:_backingTexture[0]];
48-
[self createFramebuffer:_frameBufferId[1] withBackingTexture:_backingTexture[1]];
43+
_frameBuffers[0] = [[FlutterFrameBufferProvider alloc] initWithOpenGLContext:_openGLContext];
44+
_frameBuffers[1] = [[FlutterFrameBufferProvider alloc] initWithOpenGLContext:_openGLContext];
4945
}
5046
return self;
5147
}
5248

53-
- (void)createFramebuffer:(uint32_t)fbo withBackingTexture:(uint32_t)texture {
54-
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
55-
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture);
56-
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
57-
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
58-
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
59-
glTexParameteri(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
60-
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
61-
}
62-
6349
- (void)ensureSurfaceSize:(CGSize)size {
6450
if (CGSizeEqualToSize(size, _surfaceSize)) {
6551
return;
@@ -88,16 +74,16 @@ - (void)ensureSurfaceSize:(CGSize)size {
8874
};
8975
_ioSurface[i] = IOSurfaceCreate((CFDictionaryRef)options);
9076

91-
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, _backingTexture[i]);
77+
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, [_frameBuffers[i] glTextureId]);
9278

9379
CGLTexImageIOSurface2D(CGLGetCurrentContext(), GL_TEXTURE_RECTANGLE_ARB, GL_RGBA,
9480
int(size.width), int(size.height), GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV,
9581
_ioSurface[i], 0 /* plane */);
9682
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
9783

98-
glBindFramebuffer(GL_FRAMEBUFFER, _frameBufferId[i]);
84+
glBindFramebuffer(GL_FRAMEBUFFER, [_frameBuffers[i] glFrameBufferId]);
9985
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_RECTANGLE_ARB,
100-
_backingTexture[i], 0);
86+
[_frameBuffers[i] glTextureId], 0);
10187

10288
NSAssert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE,
10389
@"Framebuffer status check failed");
@@ -114,14 +100,12 @@ - (void)swapBuffers {
114100

115101
std::swap(_ioSurface[kFlutterSurfaceManagerBackBuffer],
116102
_ioSurface[kFlutterSurfaceManagerFrontBuffer]);
117-
std::swap(_frameBufferId[kFlutterSurfaceManagerBackBuffer],
118-
_frameBufferId[kFlutterSurfaceManagerFrontBuffer]);
119-
std::swap(_backingTexture[kFlutterSurfaceManagerBackBuffer],
120-
_backingTexture[kFlutterSurfaceManagerFrontBuffer]);
103+
std::swap(_frameBuffers[kFlutterSurfaceManagerBackBuffer],
104+
_frameBuffers[kFlutterSurfaceManagerFrontBuffer]);
121105
}
122106

123107
- (uint32_t)glFrameBufferId {
124-
return _frameBufferId[kFlutterSurfaceManagerBackBuffer];
108+
return [_frameBuffers[kFlutterSurfaceManagerBackBuffer] glFrameBufferId];
125109
}
126110

127111
- (void)dealloc {

0 commit comments

Comments
 (0)