Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 07e866e

Browse files
committed
add tests for external textures
1 parent 235567e commit 07e866e

File tree

5 files changed

+80
-10
lines changed

5 files changed

+80
-10
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ - (instancetype)initWithPlugin:(nonnull NSString*)pluginKey
7070
@implementation FlutterEngineRegistrar {
7171
NSString* _pluginKey;
7272
FlutterEngine* _flutterEngine;
73-
FlutterEngineProcTable _embedderAPI;
7473
}
7574

7675
- (instancetype)initWithPlugin:(NSString*)pluginKey flutterEngine:(FlutterEngine*)flutterEngine {
@@ -146,7 +145,8 @@ - (instancetype)initWithName:(NSString*)labelPrefix
146145
_allowHeadlessExecution = allowHeadlessExecution;
147146
_embedderAPI.struct_size = sizeof(FlutterEngineProcTable);
148147
FlutterEngineGetProcAddresses(&_embedderAPI);
149-
_openGLRenderer = [[FlutterOpenGLRenderer alloc] initWithFlutterEngine:_engine];
148+
_openGLRenderer = [[FlutterOpenGLRenderer alloc] initWithEmbedderEngine:_engine
149+
flutterEngine:self];
150150

151151
NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
152152
[notificationCenter addObserver:self

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

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
#import <OCMock/OCMock.h>
6+
57
#import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterEngine.h"
68
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h"
79
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h"
@@ -50,4 +52,66 @@
5052
[engine shutDownEngine];
5153
}
5254

53-
} // flutter::testing
55+
TEST(FlutterEngine, RegisterExternalTexture) {
56+
FlutterEngine* engine = CreateTestEngine();
57+
EXPECT_TRUE([engine runWithEntrypoint:@"main"]);
58+
59+
id<FlutterTexture> flutterTexture = OCMProtocolMock(@protocol(FlutterTexture));
60+
bool called = false;
61+
62+
engine.embedderAPI.RegisterExternalTexture =
63+
MOCK_ENGINE_PROC(RegisterExternalTexture, [&](auto engine, int64_t textureIdentifier) {
64+
called = true;
65+
EXPECT_EQ(textureIdentifier, reinterpret_cast<int64_t>(flutterTexture));
66+
return kSuccess;
67+
});
68+
69+
[engine.openGLRenderer registerTexture:flutterTexture];
70+
EXPECT_TRUE(called);
71+
72+
[engine shutDownEngine];
73+
}
74+
75+
TEST(FlutterEngine, UnregisterExternalTexture) {
76+
FlutterEngine* engine = CreateTestEngine();
77+
EXPECT_TRUE([engine runWithEntrypoint:@"main"]);
78+
79+
id<FlutterTexture> flutterTexture = OCMProtocolMock(@protocol(FlutterTexture));
80+
bool called = false;
81+
82+
int64_t registeredTextureId = [engine.openGLRenderer registerTexture:flutterTexture];
83+
engine.embedderAPI.UnregisterExternalTexture =
84+
MOCK_ENGINE_PROC(UnregisterExternalTexture, [&](auto engine, int64_t textureIdentifier) {
85+
called = true;
86+
EXPECT_EQ(textureIdentifier, registeredTextureId);
87+
return kSuccess;
88+
});
89+
90+
[engine.openGLRenderer unregisterTexture:registeredTextureId];
91+
EXPECT_TRUE(called);
92+
93+
[engine shutDownEngine];
94+
}
95+
96+
TEST(FlutterEngine, MarkExternalTextureFrameAvailable) {
97+
FlutterEngine* engine = CreateTestEngine();
98+
EXPECT_TRUE([engine runWithEntrypoint:@"main"]);
99+
100+
id<FlutterTexture> flutterTexture = OCMProtocolMock(@protocol(FlutterTexture));
101+
bool called = false;
102+
103+
int64_t registeredTextureId = [engine.openGLRenderer registerTexture:flutterTexture];
104+
engine.embedderAPI.MarkExternalTextureFrameAvailable = MOCK_ENGINE_PROC(
105+
MarkExternalTextureFrameAvailable, [&](auto engine, int64_t textureIdentifier) {
106+
called = true;
107+
EXPECT_EQ(textureIdentifier, registeredTextureId);
108+
return kSuccess;
109+
});
110+
111+
[engine.openGLRenderer textureFrameAvailable:registeredTextureId];
112+
EXPECT_TRUE(called);
113+
114+
[engine shutDownEngine];
115+
}
116+
117+
} // namespace flutter::testing

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ - (instancetype)initWithFlutterTexture:(id<FlutterTexture>)texture {
3232
}
3333

3434
- (int64_t)textureID {
35-
return reinterpret_cast<int64_t>(self);
35+
return reinterpret_cast<int64_t>(_texture);
3636
}
3737

3838
- (BOOL)populateTexture:(FlutterOpenGLTexture*)openGLTexture {

shell/platform/darwin/macos/framework/Source/FlutterOpenGLRenderer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ NS_ASSUME_NONNULL_BEGIN
3131
/**
3232
* Intializes the renderer with the given FlutterEngine.
3333
*/
34-
- (instancetype)initWithFlutterEngine:(FLUTTER_API_SYMBOL(FlutterEngine))engine;
34+
- (instancetype)initWithEmbedderEngine:(FLUTTER_API_SYMBOL(FlutterEngine))engine
35+
flutterEngine:(FlutterEngine*)flutterEngine;
3536

3637
/**
3738
* Attaches to the FlutterView and sets up the renderers main OpenGL context.

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ @implementation FlutterOpenGLRenderer {
5959
// A mapping of textureID to internal FlutterExternalTextureGL adapter.
6060
NSMutableDictionary<NSNumber*, FlutterExternalTextureGL*>* _textures;
6161

62-
FlutterEngineProcTable _embedderAPI;
62+
FlutterEngine* _flutterEngine;
6363
}
6464

65-
- (instancetype)initWithFlutterEngine:(FLUTTER_API_SYMBOL(FlutterEngine))engine {
65+
- (instancetype)initWithEmbedderEngine:(FLUTTER_API_SYMBOL(FlutterEngine))engine
66+
flutterEngine:(FlutterEngine*)flutterEngine {
6667
self = [super init];
6768
if (self) {
6869
_engine = engine;
70+
_flutterEngine = flutterEngine;
6971
_textures = [[NSMutableDictionary alloc] init];
7072
}
7173
return self;
@@ -141,7 +143,8 @@ - (int64_t)registerTexture:(id<FlutterTexture>)texture {
141143
FlutterExternalTextureGL* FlutterTexture =
142144
[[FlutterExternalTextureGL alloc] initWithFlutterTexture:texture];
143145
int64_t textureID = [FlutterTexture textureID];
144-
FlutterEngineResult success = _embedderAPI.RegisterExternalTexture(_engine, textureID);
146+
FlutterEngineResult success =
147+
_flutterEngine.embedderAPI.RegisterExternalTexture(_engine, textureID);
145148
if (success == FlutterEngineResult::kSuccess) {
146149
_textures[@(textureID)] = FlutterTexture;
147150
return textureID;
@@ -152,14 +155,16 @@ - (int64_t)registerTexture:(id<FlutterTexture>)texture {
152155
}
153156

154157
- (void)textureFrameAvailable:(int64_t)textureID {
155-
FlutterEngineResult success = _embedderAPI.MarkExternalTextureFrameAvailable(_engine, textureID);
158+
FlutterEngineResult success =
159+
_flutterEngine.embedderAPI.MarkExternalTextureFrameAvailable(_engine, textureID);
156160
if (success != FlutterEngineResult::kSuccess) {
157161
NSLog(@"Unable to mark texture with id %lld as available.", textureID);
158162
}
159163
}
160164

161165
- (void)unregisterTexture:(int64_t)textureID {
162-
FlutterEngineResult success = _embedderAPI.UnregisterExternalTexture(_engine, textureID);
166+
FlutterEngineResult success =
167+
_flutterEngine.embedderAPI.UnregisterExternalTexture(_engine, textureID);
163168
if (success == FlutterEngineResult::kSuccess) {
164169
[_textures removeObjectForKey:@(textureID)];
165170
} else {

0 commit comments

Comments
 (0)