Flutter Linux Embedder
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 
9 #include "flutter/shell/platform/linux/testing/fl_test.h"
10 #include "flutter/shell/platform/linux/testing/mock_texture_registrar.h"
11 #include "gtest/gtest.h"
12 
13 #include <epoxy/gl.h>
14 
15 #include <gmodule.h>
16 #include <pthread.h>
17 
18 static constexpr uint32_t kBufferWidth = 4u;
19 static constexpr uint32_t kBufferHeight = 4u;
20 static constexpr uint32_t kRealBufferWidth = 2u;
21 static constexpr uint32_t kRealBufferHeight = 2u;
22 static constexpr uint64_t kThreadCount = 16u;
23 
24 G_DECLARE_FINAL_TYPE(FlTestRegistrarTexture,
25  fl_test_registrar_texture,
26  FL,
27  TEST_REGISTRAR_TEXTURE,
28  FlTextureGL)
29 
30 /// A simple texture.
31 struct _FlTestRegistrarTexture {
32  FlTextureGL parent_instance;
33 };
34 
35 G_DEFINE_TYPE(FlTestRegistrarTexture,
36  fl_test_registrar_texture,
37  fl_texture_gl_get_type())
38 
39 static gboolean fl_test_registrar_texture_populate(FlTextureGL* texture,
40  uint32_t* target,
41  uint32_t* format,
42  uint32_t* width,
43  uint32_t* height,
44  GError** error) {
45  EXPECT_TRUE(FL_IS_TEST_REGISTRAR_TEXTURE(texture));
46 
47  EXPECT_EQ(*width, kBufferWidth);
48  EXPECT_EQ(*height, kBufferHeight);
49  *target = GL_TEXTURE_2D;
50  *format = GL_R8;
53 
54  return TRUE;
55 }
56 
58  FlTestRegistrarTextureClass* klass) {
59  FL_TEXTURE_GL_CLASS(klass)->populate = fl_test_registrar_texture_populate;
60 }
61 
62 static void fl_test_registrar_texture_init(FlTestRegistrarTexture* self) {}
63 
64 static FlTestRegistrarTexture* fl_test_registrar_texture_new() {
65  return FL_TEST_REGISTRAR_TEXTURE(
66  g_object_new(fl_test_registrar_texture_get_type(), nullptr));
67 }
68 
69 static void* add_mock_texture_to_registrar(void* pointer) {
70  g_return_val_if_fail(FL_TEXTURE_REGISTRAR(pointer), ((void*)NULL));
71  FlTextureRegistrar* registrar = FL_TEXTURE_REGISTRAR(pointer);
72  g_autoptr(FlTexture) texture = FL_TEXTURE(fl_test_registrar_texture_new());
73  fl_texture_registrar_register_texture(registrar, texture);
74  int64_t* id = static_cast<int64_t*>(malloc(sizeof(int64_t)));
75  id[0] = fl_texture_get_id(texture);
76  pthread_exit(id);
77 }
78 
79 // Checks can make a mock registrar.
80 TEST(FlTextureRegistrarTest, MockRegistrar) {
81  g_autoptr(FlTexture) texture = FL_TEXTURE(fl_test_registrar_texture_new());
82  g_autoptr(FlMockTextureRegistrar) registrar = fl_mock_texture_registrar_new();
83  EXPECT_TRUE(FL_IS_MOCK_TEXTURE_REGISTRAR(registrar));
84 
86  FL_TEXTURE_REGISTRAR(registrar), texture));
87  EXPECT_EQ(fl_mock_texture_registrar_get_texture(registrar), texture);
89  FL_TEXTURE_REGISTRAR(registrar), texture));
90  EXPECT_TRUE(fl_mock_texture_registrar_get_frame_available(registrar));
92  FL_TEXTURE_REGISTRAR(registrar), texture));
93  EXPECT_EQ(fl_mock_texture_registrar_get_texture(registrar), nullptr);
94 }
95 
96 // Test that registering a texture works.
97 TEST(FlTextureRegistrarTest, RegisterTexture) {
98  g_autoptr(FlEngine) engine = make_mock_engine();
99  g_autoptr(FlTextureRegistrar) registrar = fl_texture_registrar_new(engine);
100  g_autoptr(FlTexture) texture = FL_TEXTURE(fl_test_registrar_texture_new());
101 
102  EXPECT_FALSE(fl_texture_registrar_unregister_texture(registrar, texture));
103  EXPECT_TRUE(fl_texture_registrar_register_texture(registrar, texture));
104  EXPECT_TRUE(fl_texture_registrar_unregister_texture(registrar, texture));
105 }
106 
107 // Test that marking a texture frame available works.
108 TEST(FlTextureRegistrarTest, MarkTextureFrameAvailable) {
109  g_autoptr(FlEngine) engine = make_mock_engine();
110  g_autoptr(FlTextureRegistrar) registrar = fl_texture_registrar_new(engine);
111  g_autoptr(FlTexture) texture = FL_TEXTURE(fl_test_registrar_texture_new());
112 
113  EXPECT_FALSE(
115  EXPECT_TRUE(fl_texture_registrar_register_texture(registrar, texture));
116  EXPECT_TRUE(
118 }
119 
120 // Test the textures can be accessed via multiple threads without
121 // synchronization issues.
122 // TODO(robert-ancell): Re-enable when no longer flaky
123 // https://github.com/flutter/flutter/issues/138197
124 TEST(FlTextureRegistrarTest,
125  DISABLED_RegistrarRegisterTextureInMultipleThreads) {
126  g_autoptr(FlEngine) engine = make_mock_engine();
127  g_autoptr(FlTextureRegistrar) registrar = fl_texture_registrar_new(engine);
128  pthread_t threads[kThreadCount];
129  int64_t ids[kThreadCount];
130 
131  for (uint64_t t = 0; t < kThreadCount; t++) {
132  EXPECT_EQ(pthread_create(&threads[t], NULL, add_mock_texture_to_registrar,
133  (void*)registrar),
134  0);
135  }
136  for (uint64_t t = 0; t < kThreadCount; t++) {
137  void* id;
138  pthread_join(threads[t], &id);
139  ids[t] = static_cast<int64_t*>(id)[0];
140  free(id);
141  };
142  // Check all the textures were created.
143  for (uint64_t t = 0; t < kThreadCount; t++) {
144  EXPECT_TRUE(fl_texture_registrar_lookup_texture(registrar, ids[t]) != NULL);
145  };
146 }
add_mock_texture_to_registrar
static void * add_mock_texture_to_registrar(void *pointer)
Definition: fl_texture_registrar_test.cc:69
kBufferHeight
static constexpr uint32_t kBufferHeight
Definition: fl_texture_registrar_test.cc:19
kRealBufferWidth
static constexpr uint32_t kRealBufferWidth
Definition: fl_texture_registrar_test.cc:20
fl_texture_registrar_lookup_texture
FlTexture * fl_texture_registrar_lookup_texture(FlTextureRegistrar *self, int64_t texture_id)
Definition: fl_texture_registrar.cc:191
fl_pixel_buffer_texture.h
fl_test_registrar_texture_new
static FlTestRegistrarTexture * fl_test_registrar_texture_new()
Definition: fl_texture_registrar_test.cc:64
id
int64_t id
Definition: fl_pixel_buffer_texture.cc:28
kRealBufferHeight
static constexpr uint32_t kRealBufferHeight
Definition: fl_texture_registrar_test.cc:21
fl_texture_registrar_new
FlTextureRegistrar * fl_texture_registrar_new(FlEngine *engine)
Definition: fl_texture_registrar.cc:216
fl_texture_registrar_unregister_texture
G_MODULE_EXPORT gboolean fl_texture_registrar_unregister_texture(FlTextureRegistrar *self, FlTexture *texture)
Definition: fl_texture_registrar.cc:207
error
uint32_t uint32_t uint32_t uint32_t GError ** error
Definition: fl_texture_registrar_test.cc:44
make_mock_engine
static FlEngine * make_mock_engine()
Definition: fl_event_channel_test.cc:24
TRUE
return TRUE
Definition: fl_texture_registrar_test.cc:54
fl_texture_registrar_register_texture
G_MODULE_EXPORT gboolean fl_texture_registrar_register_texture(FlTextureRegistrar *self, FlTexture *texture)
Definition: fl_texture_registrar.cc:182
fl_texture_registrar.h
fl_test_registrar_texture_class_init
static void fl_test_registrar_texture_class_init(FlTestRegistrarTextureClass *klass)
Definition: fl_texture_registrar_test.cc:57
fl_texture_gl.h
fl_texture_get_id
G_MODULE_EXPORT int64_t fl_texture_get_id(FlTexture *self)
Definition: fl_texture.cc:20
kBufferWidth
static constexpr uint32_t kBufferWidth
Definition: fl_texture_registrar_test.cc:18
FL
FL
Definition: fl_binary_messenger.cc:27
width
uint32_t uint32_t uint32_t * width
Definition: fl_texture_registrar_test.cc:42
height
uint32_t uint32_t uint32_t uint32_t * height
Definition: fl_texture_registrar_test.cc:43
G_DECLARE_FINAL_TYPE
G_DECLARE_FINAL_TYPE(FlTestRegistrarTexture, fl_test_registrar_texture, FL, TEST_REGISTRAR_TEXTURE, FlTextureGL) struct _FlTestRegistrarTexture
A simple texture.
Definition: fl_texture_registrar_test.cc:24
kThreadCount
static constexpr uint64_t kThreadCount
Definition: fl_texture_registrar_test.cc:22
target
uint32_t * target
Definition: fl_texture_registrar_test.cc:40
G_DEFINE_TYPE
G_DEFINE_TYPE(FlTestRegistrarTexture, fl_test_registrar_texture, fl_texture_gl_get_type()) static gboolean fl_test_registrar_texture_populate(FlTextureGL *texture
fl_texture_registrar_private.h
engine
FlEngine * engine
Definition: fl_view_accessible.cc:26
fl_test_registrar_texture_init
static void fl_test_registrar_texture_init(FlTestRegistrarTexture *self)
Definition: fl_texture_registrar_test.cc:62
format
uint32_t uint32_t * format
Definition: fl_texture_registrar_test.cc:41
TEST
TEST(FlTextureRegistrarTest, MockRegistrar)
Definition: fl_texture_registrar_test.cc:80
fl_texture_registrar_mark_texture_frame_available
G_MODULE_EXPORT gboolean fl_texture_registrar_mark_texture_frame_available(FlTextureRegistrar *self, FlTexture *texture)
Definition: fl_texture_registrar.cc:198