Skip to content

Commit 636f8e5

Browse files
committed
Add flutter_tizen_texture_registrar unittest
Signed-off-by: MuHong Byun <[email protected]>
1 parent eafde6a commit 636f8e5

9 files changed

+299
-8
lines changed

shell/platform/tizen/BUILD.gn

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ template("embedder_executable") {
245245
"channels/localization_channel_stub.cc",
246246
"channels/platform_channel_stub.cc",
247247
"channels/settings_channel_stub.cc",
248+
"external_texture_pixel_gl_stub.cc",
249+
"external_texture_surface_gl_stub.cc",
248250
"tizen_log_stub.cc",
249251
"tizen_renderer_evas_gl.cc",
250252
]
@@ -303,6 +305,7 @@ embedder_executable("flutter_tizen_unittests") {
303305

304306
sources = [
305307
"flutter_tizen_engine_unittest.cc",
308+
"flutter_tizen_texture_registrar_unittests.cc",
306309
"testing/mock_engine.cc",
307310
]
308311

shell/platform/tizen/external_texture_pixel_gl.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ ExternalTexturePixelGL::ExternalTexturePixelGL(
4545
user_data_(user_data) {}
4646

4747
bool ExternalTexturePixelGL::CopyPixelBuffer(size_t& width, size_t& height) {
48+
if (!texture_callback_) {
49+
return false;
50+
}
51+
4852
const FlutterDesktopPixelBuffer* pixel_buffer =
4953
texture_callback_(width, height, user_data_);
5054

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2021 Samsung Electronics Co., Ltd. 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/tizen/external_texture_pixel_gl.h"
6+
7+
#ifdef TIZEN_RENDERER_EVAS_GL
8+
#undef EFL_BETA_API_SUPPORT
9+
#include "tizen_evas_gl_helper.h"
10+
extern Evas_GL* g_evas_gl;
11+
EVAS_GL_GLOBAL_GLES3_DECLARE();
12+
#else
13+
#include <EGL/egl.h>
14+
#include <EGL/eglext.h>
15+
#include <GLES2/gl2ext.h>
16+
#include <GLES3/gl32.h>
17+
#endif
18+
19+
namespace flutter {
20+
21+
ExternalTexturePixelGL::ExternalTexturePixelGL(
22+
FlutterDesktopPixelBufferTextureCallback texture_callback,
23+
void* user_data)
24+
: ExternalTexture(),
25+
texture_callback_(texture_callback),
26+
user_data_(user_data) {}
27+
28+
bool ExternalTexturePixelGL::PopulateTexture(
29+
size_t width,
30+
size_t height,
31+
FlutterOpenGLTexture* opengl_texture) {
32+
if (!CopyPixelBuffer(width, height)) {
33+
return false;
34+
}
35+
36+
// Populate the texture object used by the engine.
37+
opengl_texture->target = GL_TEXTURE_2D;
38+
opengl_texture->name = state_->gl_texture;
39+
opengl_texture->format = GL_RGBA8;
40+
opengl_texture->destruction_callback = nullptr;
41+
opengl_texture->user_data = nullptr;
42+
opengl_texture->width = width;
43+
opengl_texture->height = height;
44+
return true;
45+
}
46+
47+
bool ExternalTexturePixelGL::CopyPixelBuffer(size_t& width, size_t& height) {
48+
if (!texture_callback_) {
49+
return false;
50+
}
51+
52+
const FlutterDesktopPixelBuffer* pixel_buffer =
53+
texture_callback_(width, height, user_data_);
54+
55+
if (!pixel_buffer || !pixel_buffer->buffer) {
56+
return false;
57+
}
58+
59+
width = pixel_buffer->width;
60+
height = pixel_buffer->height;
61+
62+
if (state_->gl_texture == 0) {
63+
glGenTextures(1, &state_->gl_texture);
64+
glBindTexture(GL_TEXTURE_2D, state_->gl_texture);
65+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
66+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
67+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
68+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
69+
} else {
70+
glBindTexture(GL_TEXTURE_2D, state_->gl_texture);
71+
}
72+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pixel_buffer->width,
73+
pixel_buffer->height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
74+
pixel_buffer->buffer);
75+
return true;
76+
}
77+
} // namespace flutter

shell/platform/tizen/external_texture_surface_gl.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ bool ExternalTextureSurfaceGL::PopulateTexture(
5454
size_t width,
5555
size_t height,
5656
FlutterOpenGLTexture* opengl_texture) {
57+
if (!texture_callback_) {
58+
return false;
59+
}
5760
const FlutterDesktopGpuBuffer* gpu_buffer =
5861
texture_callback_(width, height, user_data_);
5962
if (!gpu_buffer) {
@@ -153,7 +156,9 @@ bool ExternalTextureSurfaceGL::PopulateTexture(
153156
}
154157

155158
void ExternalTextureSurfaceGL::OnDestruction() {
156-
destruction_callback_(user_data_);
159+
if (destruction_callback_) {
160+
destruction_callback_(user_data_);
161+
}
157162
}
158163

159164
} // namespace flutter
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2021 Samsung Electronics Co., Ltd. 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 "external_texture_surface_gl.h"
6+
7+
#include "flutter/shell/platform/common/public/flutter_texture_registrar.h"
8+
#include "flutter/shell/platform/tizen/tizen_log.h"
9+
10+
namespace flutter {
11+
12+
ExternalTextureSurfaceGL::ExternalTextureSurfaceGL(
13+
FlutterDesktopGpuBufferTextureCallback texture_callback,
14+
FlutterDesktopGpuBufferDestructionCallback destruction_callback,
15+
void* user_data)
16+
: ExternalTexture(),
17+
texture_callback_(texture_callback),
18+
destruction_callback_(destruction_callback),
19+
user_data_(user_data) {}
20+
21+
ExternalTextureSurfaceGL::~ExternalTextureSurfaceGL() {
22+
state_.release();
23+
}
24+
25+
bool ExternalTextureSurfaceGL::PopulateTexture(
26+
size_t width,
27+
size_t height,
28+
FlutterOpenGLTexture* opengl_texture) {
29+
if (!texture_callback_) {
30+
return false;
31+
}
32+
33+
const FlutterDesktopGpuBuffer* gpu_buffer =
34+
texture_callback_(width, height, user_data_);
35+
if (!gpu_buffer) {
36+
FT_LOGI("[texture id:%ld] gpu_buffer is null", texture_id_);
37+
return false;
38+
}
39+
if (!gpu_buffer->buffer) {
40+
FT_LOGI("[texture id:%ld] tbm_surface_ is null", texture_id_);
41+
return false;
42+
}
43+
FT_UNIMPLEMENTED();
44+
return false;
45+
}
46+
47+
void ExternalTextureSurfaceGL::OnDestruction() {
48+
if (destruction_callback_) {
49+
destruction_callback_(user_data_);
50+
}
51+
}
52+
53+
} // namespace flutter

shell/platform/tizen/flutter_tizen_engine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ class FlutterTizenEngine : public TizenRenderer::Delegate {
137137
std::unique_ptr<PlatformViewChannel> platform_view_channel;
138138

139139
private:
140+
friend class EngineModifier;
141+
140142
// Whether the engine is running in headed or headless mode.
141143
bool IsHeaded() { return renderer != nullptr; }
142144

shell/platform/tizen/flutter_tizen_texture_registrar.cc

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
#include <iostream>
88
#include <mutex>
99

10-
#ifndef __X64_SHELL__
1110
#include "flutter/shell/platform/tizen/external_texture_pixel_gl.h"
1211
#include "flutter/shell/platform/tizen/external_texture_surface_gl.h"
13-
#endif
1412
#include "flutter/shell/platform/tizen/flutter_tizen_engine.h"
1513
#include "flutter/shell/platform/tizen/tizen_log.h"
1614

@@ -90,10 +88,6 @@ bool FlutterTizenTextureRegistrar::PopulateTexture(
9088
std::unique_ptr<ExternalTexture>
9189
FlutterTizenTextureRegistrar::CreateExternalTexture(
9290
const FlutterDesktopTextureInfo* texture_info) {
93-
#ifdef __X64_SHELL__
94-
FT_UNIMPLEMENTED();
95-
return nullptr;
96-
#else
9791
switch (texture_info->type) {
9892
case kFlutterDesktopPixelBufferTexture:
9993
return std::make_unique<ExternalTexturePixelGL>(
@@ -110,7 +104,6 @@ FlutterTizenTextureRegistrar::CreateExternalTexture(
110104
FT_LOGE("Invalid texture type.");
111105
return nullptr;
112106
}
113-
#endif
114107
}
115108

116109
} // namespace flutter
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
// Copyright 2021 Samsung Electronics Co., Ltd. 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 <iostream>
6+
7+
#include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h"
8+
#include "flutter/shell/platform/tizen/flutter_tizen_engine.h"
9+
#include "flutter/shell/platform/tizen/flutter_tizen_texture_registrar.h"
10+
#include "flutter/shell/platform/tizen/testing/engine_modifier.h"
11+
#include "gtest/gtest.h"
12+
13+
namespace flutter {
14+
namespace testing {
15+
16+
class FlutterTizenTextureRegistrarTest : public ::testing::Test {
17+
public:
18+
FlutterTizenTextureRegistrarTest() {
19+
ecore_init();
20+
elm_init(0, nullptr);
21+
}
22+
23+
protected:
24+
void SetUp() {
25+
FlutterDesktopEngineProperties engine_prop = {};
26+
engine_prop.assets_path = "foo/flutter_assets";
27+
engine_prop.icu_data_path = "foo/icudtl.dat";
28+
engine_prop.aot_library_path = "foo/libapp.so";
29+
30+
FlutterProjectBundle project(engine_prop);
31+
auto engine = std::make_unique<FlutterTizenEngine>(project);
32+
engine_ = engine.release();
33+
}
34+
35+
void TearDown() {
36+
if (engine_) {
37+
delete engine_;
38+
}
39+
engine_ = nullptr;
40+
}
41+
42+
FlutterTizenEngine* engine_;
43+
};
44+
45+
TEST_F(FlutterTizenTextureRegistrarTest, CreateDestroy) {
46+
FlutterTizenTextureRegistrar registrar(engine_);
47+
48+
EXPECT_TRUE(true);
49+
}
50+
51+
TEST_F(FlutterTizenTextureRegistrarTest, RegisterUnregisterTexture) {
52+
EngineModifier modifier(engine_);
53+
54+
FlutterTizenTextureRegistrar registrar(engine_);
55+
56+
FlutterDesktopTextureInfo texture_info = {};
57+
texture_info.type = kFlutterDesktopPixelBufferTexture;
58+
texture_info.pixel_buffer_config.callback =
59+
[](size_t width, size_t height,
60+
void* user_data) -> const FlutterDesktopPixelBuffer* {
61+
return nullptr;
62+
};
63+
64+
int64_t registered_texture_id = 0;
65+
bool register_called = false;
66+
modifier.embedder_api().RegisterExternalTexture = MOCK_ENGINE_PROC(
67+
RegisterExternalTexture, ([&register_called, &registered_texture_id](
68+
auto engine, auto texture_id) {
69+
register_called = true;
70+
registered_texture_id = texture_id;
71+
return kSuccess;
72+
}));
73+
74+
bool unregister_called = false;
75+
modifier.embedder_api().UnregisterExternalTexture = MOCK_ENGINE_PROC(
76+
UnregisterExternalTexture, ([&unregister_called, &registered_texture_id](
77+
auto engine, auto texture_id) {
78+
unregister_called = true;
79+
EXPECT_EQ(registered_texture_id, texture_id);
80+
return kSuccess;
81+
}));
82+
83+
bool mark_frame_available_called = false;
84+
modifier.embedder_api().MarkExternalTextureFrameAvailable =
85+
MOCK_ENGINE_PROC(MarkExternalTextureFrameAvailable,
86+
([&mark_frame_available_called, &registered_texture_id](
87+
auto engine, auto texture_id) {
88+
mark_frame_available_called = true;
89+
EXPECT_EQ(registered_texture_id, texture_id);
90+
return kSuccess;
91+
}));
92+
93+
auto texture_id = registrar.RegisterTexture(&texture_info);
94+
EXPECT_TRUE(register_called);
95+
EXPECT_NE(texture_id, -1);
96+
EXPECT_EQ(texture_id, registered_texture_id);
97+
98+
EXPECT_TRUE(registrar.MarkTextureFrameAvailable(texture_id));
99+
EXPECT_TRUE(mark_frame_available_called);
100+
101+
EXPECT_TRUE(registrar.UnregisterTexture(texture_id));
102+
EXPECT_TRUE(unregister_called);
103+
}
104+
105+
TEST_F(FlutterTizenTextureRegistrarTest, RegisterUnknownTextureType) {
106+
EngineModifier modifier(engine_);
107+
108+
FlutterTizenTextureRegistrar registrar(engine_);
109+
110+
FlutterDesktopTextureInfo texture_info = {};
111+
texture_info.type = static_cast<FlutterDesktopTextureType>(1234);
112+
113+
auto texture_id = registrar.RegisterTexture(&texture_info);
114+
115+
EXPECT_EQ(texture_id, -1);
116+
}
117+
118+
TEST_F(FlutterTizenTextureRegistrarTest, PopulateInvalidTexture) {
119+
FlutterTizenTextureRegistrar registrar(engine_);
120+
121+
auto result = registrar.PopulateTexture(1, 640, 480, nullptr);
122+
EXPECT_FALSE(result);
123+
}
124+
125+
} // namespace testing
126+
} // namespace flutter
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved.
2+
// Copyright 2013 The Flutter Authors. All rights reserved.
3+
4+
#include "flutter/shell/platform/tizen/flutter_tizen_engine.h"
5+
6+
namespace flutter {
7+
8+
// A test utility class providing the ability to access and alter various
9+
// private fields in an Engine instance.
10+
//
11+
// This simply provides a way to access the normally-private embedder proc
12+
// table, so the lifetime of any changes made to the proc table is that of the
13+
// engine object, not this helper.
14+
class EngineModifier {
15+
public:
16+
explicit EngineModifier(FlutterTizenEngine* engine) : engine_(engine) {}
17+
18+
// Returns the engine's embedder API proc table, allowing for modification.
19+
//
20+
// Modifications are to the engine, and will last for the lifetime of the
21+
// engine unless overwritten again.
22+
FlutterEngineProcTable& embedder_api() { return engine_->embedder_api_; }
23+
24+
private:
25+
FlutterTizenEngine* engine_;
26+
};
27+
28+
} // namespace flutter

0 commit comments

Comments
 (0)