Flutter Engine
 
Loading...
Searching...
No Matches
fl_texture_registrar_test.cc
Go to the documentation of this file.
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
12#include "gtest/gtest.h"
13
14#include <epoxy/gl.h>
15
16#include <gmodule.h>
17#include <pthread.h>
18
19static constexpr uint32_t kBufferWidth = 4u;
20static constexpr uint32_t kBufferHeight = 4u;
21static constexpr uint32_t kRealBufferWidth = 2u;
22static constexpr uint32_t kRealBufferHeight = 2u;
23static constexpr uint64_t kThreadCount = 16u;
24
25G_DECLARE_FINAL_TYPE(FlTestRegistrarTexture,
26 fl_test_registrar_texture,
27 FL,
28 TEST_REGISTRAR_TEXTURE,
29 FlTextureGL)
30
31/// A simple texture.
32struct _FlTestRegistrarTexture {
33 FlTextureGL parent_instance;
34};
35
36G_DEFINE_TYPE(FlTestRegistrarTexture,
37 fl_test_registrar_texture,
38 fl_texture_gl_get_type())
39
40static gboolean fl_test_registrar_texture_populate(FlTextureGL* texture,
41 uint32_t* target,
42 uint32_t* format,
43 uint32_t* width,
44 uint32_t* height,
45 GError** error) {
46 EXPECT_TRUE(FL_IS_TEST_REGISTRAR_TEXTURE(texture));
47
48 EXPECT_EQ(*width, kBufferWidth);
49 EXPECT_EQ(*height, kBufferHeight);
50 *target = GL_TEXTURE_2D;
51 *format = GL_R8;
54
55 return TRUE;
56}
57
59 FlTestRegistrarTextureClass* klass) {
60 FL_TEXTURE_GL_CLASS(klass)->populate = fl_test_registrar_texture_populate;
61}
62
63static void fl_test_registrar_texture_init(FlTestRegistrarTexture* self) {}
64
65static FlTestRegistrarTexture* fl_test_registrar_texture_new() {
66 return FL_TEST_REGISTRAR_TEXTURE(
67 g_object_new(fl_test_registrar_texture_get_type(), nullptr));
68}
69
70static void* add_mock_texture_to_registrar(void* pointer) {
71 g_return_val_if_fail(FL_TEXTURE_REGISTRAR(pointer), ((void*)NULL));
72 FlTextureRegistrar* registrar = FL_TEXTURE_REGISTRAR(pointer);
73 g_autoptr(FlTexture) texture = FL_TEXTURE(fl_test_registrar_texture_new());
75 int64_t* id = static_cast<int64_t*>(malloc(sizeof(int64_t)));
77 pthread_exit(id);
78}
79
80// Checks can make a mock registrar.
81TEST(FlTextureRegistrarTest, MockRegistrar) {
82 g_autoptr(FlTexture) texture = FL_TEXTURE(fl_test_registrar_texture_new());
83 g_autoptr(FlMockTextureRegistrar) registrar = fl_mock_texture_registrar_new();
84 EXPECT_TRUE(FL_IS_MOCK_TEXTURE_REGISTRAR(registrar));
85
87 FL_TEXTURE_REGISTRAR(registrar), texture));
90 FL_TEXTURE_REGISTRAR(registrar), texture));
93 FL_TEXTURE_REGISTRAR(registrar), texture));
94 EXPECT_EQ(fl_mock_texture_registrar_get_texture(registrar), nullptr);
95}
96
97// Test that registering a texture works.
98TEST(FlTextureRegistrarTest, RegisterTexture) {
99 g_autoptr(FlDartProject) project = fl_dart_project_new();
100 g_autoptr(FlEngine) engine = fl_engine_new(project);
101 bool register_called = false;
103 MOCK_ENGINE_PROC(RegisterExternalTexture,
104 ([&register_called](auto engine, int64_t texture_id) {
105 register_called = true;
106 return kSuccess;
107 }));
108 bool unregister_called = false;
110 MOCK_ENGINE_PROC(UnregisterExternalTexture,
111 ([&unregister_called](auto engine, int64_t texture_id) {
112 unregister_called = true;
113 return kSuccess;
114 }));
115
116 g_autoptr(FlTextureRegistrar) registrar = fl_texture_registrar_new(engine);
117 g_autoptr(FlTexture) texture = FL_TEXTURE(fl_test_registrar_texture_new());
118
119 // EXPECT_FALSE(fl_texture_registrar_unregister_texture(registrar, texture));
120 EXPECT_FALSE(register_called);
121 EXPECT_TRUE(fl_texture_registrar_register_texture(registrar, texture));
122 EXPECT_TRUE(register_called);
123 EXPECT_FALSE(unregister_called);
124 EXPECT_TRUE(fl_texture_registrar_unregister_texture(registrar, texture));
125 EXPECT_TRUE(unregister_called);
126}
127
128// Test that marking a texture frame available works.
129TEST(FlTextureRegistrarTest, MarkTextureFrameAvailable) {
130 g_autoptr(FlDartProject) project = fl_dart_project_new();
131 g_autoptr(FlEngine) engine = fl_engine_new(project);
132 bool register_called = false;
134 MOCK_ENGINE_PROC(RegisterExternalTexture,
135 ([&register_called](auto engine, int64_t texture_id) {
136 register_called = true;
137 return kSuccess;
138 }));
141 UnregisterExternalTexture,
142 ([](auto engine, int64_t texture_id) { return kSuccess; }));
145 MarkExternalTextureFrameAvailable,
146 ([](auto engine, int64_t texture_id) { return kSuccess; }));
147
148 g_autoptr(FlTextureRegistrar) registrar = fl_texture_registrar_new(engine);
149 g_autoptr(FlTexture) texture = FL_TEXTURE(fl_test_registrar_texture_new());
150
151 EXPECT_TRUE(fl_texture_registrar_register_texture(registrar, texture));
152 EXPECT_TRUE(register_called);
153 EXPECT_TRUE(
155}
156
157// Test handles error marking a texture frame available.
158TEST(FlTextureRegistrarTest, MarkInvalidTextureFrameAvailable) {
159 g_autoptr(FlDartProject) project = fl_dart_project_new();
160 g_autoptr(FlEngine) engine = fl_engine_new(project);
163 RegisterExternalTexture,
164 ([](auto engine, int64_t texture_id) { return kSuccess; }));
167 UnregisterExternalTexture,
168 ([](auto engine, int64_t texture_id) { return kSuccess; }));
170 MOCK_ENGINE_PROC(MarkExternalTextureFrameAvailable,
171 ([](auto engine, int64_t texture_id) {
173 }));
174
175 g_autoptr(FlTextureRegistrar) registrar = fl_texture_registrar_new(engine);
176 g_autoptr(FlTexture) texture = FL_TEXTURE(fl_test_registrar_texture_new());
177
178 EXPECT_TRUE(fl_texture_registrar_register_texture(registrar, texture));
179 EXPECT_FALSE(
181}
182
183// Test the textures can be accessed via multiple threads without
184// synchronization issues.
185// TODO(robert-ancell): Re-enable when no longer flaky
186// https://github.com/flutter/flutter/issues/138197
187TEST(FlTextureRegistrarTest,
188 DISABLED_RegistrarRegisterTextureInMultipleThreads) {
189 g_autoptr(FlDartProject) project = fl_dart_project_new();
190 g_autoptr(FlEngine) engine = fl_engine_new(project);
191
194 RegisterExternalTexture,
195 ([](auto engine, int64_t texture_id) { return kSuccess; }));
198 UnregisterExternalTexture,
199 ([](auto engine, int64_t texture_id) { return kSuccess; }));
200
201 g_autoptr(FlTextureRegistrar) registrar = fl_texture_registrar_new(engine);
202 pthread_t threads[kThreadCount];
203 int64_t ids[kThreadCount];
204
205 for (uint64_t t = 0; t < kThreadCount; t++) {
206 EXPECT_EQ(pthread_create(&threads[t], NULL, add_mock_texture_to_registrar,
207 (void*)registrar),
208 0);
209 }
210 for (uint64_t t = 0; t < kThreadCount; t++) {
211 void* id;
212 pthread_join(threads[t], &id);
213 ids[t] = static_cast<int64_t*>(id)[0];
214 free(id);
215 };
216 // Check all the textures were created.
217 for (uint64_t t = 0; t < kThreadCount; t++) {
218 EXPECT_TRUE(fl_texture_registrar_lookup_texture(registrar, ids[t]) != NULL);
219 };
220}
@ kInternalInconsistency
Definition embedder.h:76
@ kSuccess
Definition embedder.h:73
FlutterEngine engine
Definition main.cc:84
g_autoptr(GMutexLocker) locker
G_MODULE_EXPORT FlDartProject * fl_dart_project_new()
G_MODULE_EXPORT FlEngine * fl_engine_new(FlDartProject *project)
Definition fl_engine.cc:697
FlutterEngineProcTable * fl_engine_get_embedder_api(FlEngine *self)
Definition fl_engine.cc:868
G_MODULE_EXPORT int64_t fl_texture_get_id(FlTexture *self)
Definition fl_texture.cc:20
G_MODULE_EXPORT gboolean fl_texture_registrar_register_texture(FlTextureRegistrar *self, FlTexture *texture)
G_MODULE_EXPORT gboolean fl_texture_registrar_unregister_texture(FlTextureRegistrar *self, FlTexture *texture)
G_MODULE_EXPORT gboolean fl_texture_registrar_mark_texture_frame_available(FlTextureRegistrar *self, FlTexture *texture)
FlTexture * fl_texture_registrar_lookup_texture(FlTextureRegistrar *self, int64_t texture_id)
FlTextureRegistrar * fl_texture_registrar_new(FlEngine *engine)
uint32_t uint32_t uint32_t uint32_t GError ** error
static void fl_test_registrar_texture_init(FlTestRegistrarTexture *self)
static constexpr uint32_t kBufferWidth
static void * add_mock_texture_to_registrar(void *pointer)
uint32_t uint32_t uint32_t uint32_t * height
uint32_t uint32_t * format
TEST(FlTextureRegistrarTest, MockRegistrar)
uint32_t uint32_t uint32_t * width
G_DEFINE_TYPE(FlTestRegistrarTexture, fl_test_registrar_texture, fl_texture_gl_get_type()) static gboolean fl_test_registrar_texture_populate(FlTextureGL *texture
static constexpr uint32_t kRealBufferHeight
G_DECLARE_FINAL_TYPE(FlTestRegistrarTexture, fl_test_registrar_texture, FL, TEST_REGISTRAR_TEXTURE, FlTextureGL) struct _FlTestRegistrarTexture
A simple texture.
static void fl_test_registrar_texture_class_init(FlTestRegistrarTextureClass *klass)
static constexpr uint32_t kBufferHeight
static constexpr uint64_t kThreadCount
static FlTestRegistrarTexture * fl_test_registrar_texture_new()
static constexpr uint32_t kRealBufferWidth
uint32_t * target
FlTexture * fl_mock_texture_registrar_get_texture(FlMockTextureRegistrar *self)
FlTexture * texture
FlMockTextureRegistrar * fl_mock_texture_registrar_new()
gboolean fl_mock_texture_registrar_get_frame_available(FlMockTextureRegistrar *self)
#define MOCK_ENGINE_PROC(proc, mock_impl)
FlutterEngineRegisterExternalTextureFnPtr RegisterExternalTexture
Definition embedder.h:3724
FlutterEngineUnregisterExternalTextureFnPtr UnregisterExternalTexture
Definition embedder.h:3725
FlutterEngineMarkExternalTextureFrameAvailableFnPtr MarkExternalTextureFrameAvailable
Definition embedder.h:3727
int64_t texture_id
const uintptr_t id