Flutter Engine
The Flutter Engine
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
5#include "flutter/shell/platform/linux/public/flutter_linux/fl_texture_registrar.h"
6#include "flutter/shell/platform/linux/fl_texture_registrar_private.h"
7#include "flutter/shell/platform/linux/public/flutter_linux/fl_pixel_buffer_texture.h"
8#include "flutter/shell/platform/linux/public/flutter_linux/fl_texture_gl.h"
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
18static constexpr uint32_t kBufferWidth = 4u;
19static constexpr uint32_t kBufferHeight = 4u;
20static constexpr uint32_t kRealBufferWidth = 2u;
21static constexpr uint32_t kRealBufferHeight = 2u;
22static constexpr uint64_t kThreadCount = 16u;
23
24G_DECLARE_FINAL_TYPE(FlTestRegistrarTexture,
25 fl_test_registrar_texture,
26 FL,
27 TEST_REGISTRAR_TEXTURE,
28 FlTextureGL)
29
30/// A simple texture.
31struct _FlTestRegistrarTexture {
32 FlTextureGL parent_instance;
33};
34
35G_DEFINE_TYPE(FlTestRegistrarTexture,
36 fl_test_registrar_texture,
37 fl_texture_gl_get_type())
38
39static 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
62static void fl_test_registrar_texture_init(FlTestRegistrarTexture* self) {}
63
64static 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
69static 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());
74 int64_t* id = static_cast<int64_t*>(malloc(sizeof(int64_t)));
76 pthread_exit(id);
77}
78
79// Checks can make a mock registrar.
80TEST(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));
89 FL_TEXTURE_REGISTRAR(registrar), texture));
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.
97TEST(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));
105}
106
107// Test that marking a texture frame available works.
108TEST(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(
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
124TEST(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}
FlutterEngine engine
Definition: main.cc:68
static FlEngine * make_mock_engine()
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)
void * malloc(size_t size)
Definition: allocation.cc:19
static void MarkTextureFrameAvailable(JNIEnv *env, jobject jcaller, jlong shell_holder, jlong texture_id)
static void RegisterTexture(JNIEnv *env, jobject jcaller, jlong shell_holder, jlong texture_id, jobject surface_texture)
const uintptr_t id
#define EXPECT_TRUE(handle)
Definition: unit_test.h:678