Skip to content

Commit 2d8b8a7

Browse files
committed
Add flutter_tizen_texture_registrar unittest
Signed-off-by: MuHong Byun <mh.byun@samsung.com>
1 parent eafde6a commit 2d8b8a7

File tree

7 files changed

+267
-7
lines changed

7 files changed

+267
-7
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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2020 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+
bool ExternalTexturePixelGL::PopulateTexture(
22+
size_t width,
23+
size_t height,
24+
FlutterOpenGLTexture* opengl_texture) {
25+
if (!CopyPixelBuffer(width, height)) {
26+
return false;
27+
}
28+
29+
// Populate the texture object used by the engine.
30+
opengl_texture->target = GL_TEXTURE_2D;
31+
opengl_texture->name = state_->gl_texture;
32+
opengl_texture->format = GL_RGBA8;
33+
opengl_texture->destruction_callback = nullptr;
34+
opengl_texture->user_data = nullptr;
35+
opengl_texture->width = width;
36+
opengl_texture->height = height;
37+
return true;
38+
}
39+
40+
ExternalTexturePixelGL::ExternalTexturePixelGL(
41+
FlutterDesktopPixelBufferTextureCallback texture_callback,
42+
void* user_data)
43+
: ExternalTexture(),
44+
texture_callback_(texture_callback),
45+
user_data_(user_data) {}
46+
47+
bool ExternalTexturePixelGL::CopyPixelBuffer(size_t& width, size_t& height) {
48+
const FlutterDesktopPixelBuffer* pixel_buffer =
49+
texture_callback_(width, height, user_data_);
50+
51+
if (!pixel_buffer || !pixel_buffer->buffer) {
52+
return false;
53+
}
54+
55+
width = pixel_buffer->width;
56+
height = pixel_buffer->height;
57+
58+
if (state_->gl_texture == 0) {
59+
glGenTextures(1, &state_->gl_texture);
60+
glBindTexture(GL_TEXTURE_2D, state_->gl_texture);
61+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
62+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
63+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
64+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
65+
} else {
66+
glBindTexture(GL_TEXTURE_2D, state_->gl_texture);
67+
}
68+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pixel_buffer->width,
69+
pixel_buffer->height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
70+
pixel_buffer->buffer);
71+
return true;
72+
}
73+
} // namespace flutter
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2020 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+
const FlutterDesktopGpuBuffer* gpu_buffer =
30+
texture_callback_(width, height, user_data_);
31+
if (!gpu_buffer) {
32+
FT_LOGI("[texture id:%ld] gpu_buffer is null", texture_id_);
33+
return false;
34+
}
35+
if (!gpu_buffer->buffer) {
36+
FT_LOGI("[texture id:%ld] tbm_surface_ is null", texture_id_);
37+
return false;
38+
}
39+
FT_UNIMPLEMENTED();
40+
return false;
41+
}
42+
43+
void ExternalTextureSurfaceGL::OnDestruction() {
44+
destruction_callback_(user_data_);
45+
}
46+
47+
} // 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: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
namespace {
17+
std::unique_ptr<FlutterTizenEngine> GetTestEngine() {
18+
FlutterDesktopEngineProperties engine_prop = {};
19+
engine_prop.assets_path = "foo/flutter_assets";
20+
engine_prop.icu_data_path = "foo/icudtl.dat";
21+
engine_prop.aot_library_path = "foo/libapp.so";
22+
FlutterProjectBundle project(engine_prop);
23+
return std::make_unique<FlutterTizenEngine>(project);
24+
}
25+
} // namespace
26+
27+
TEST(FlutterTizenTextureRegistrarTest, CreateDestroy) {
28+
std::unique_ptr<FlutterTizenEngine> engine = GetTestEngine();
29+
30+
FlutterTizenTextureRegistrar registrar(engine.get());
31+
32+
EXPECT_TRUE(true);
33+
}
34+
35+
TEST(FlutterTizenTextureRegistrarTest, RegisterUnregisterTexture) {
36+
std::unique_ptr<FlutterTizenEngine> engine = GetTestEngine();
37+
EngineModifier modifier(engine.get());
38+
39+
FlutterTizenTextureRegistrar registrar(engine.get());
40+
41+
FlutterDesktopTextureInfo texture_info = {};
42+
texture_info.type = kFlutterDesktopPixelBufferTexture;
43+
texture_info.pixel_buffer_config.callback =
44+
[](size_t width, size_t height,
45+
void* user_data) -> const FlutterDesktopPixelBuffer* {
46+
return nullptr;
47+
};
48+
49+
int64_t registered_texture_id = 0;
50+
bool register_called = false;
51+
modifier.embedder_api().RegisterExternalTexture = MOCK_ENGINE_PROC(
52+
RegisterExternalTexture, ([&register_called, &registered_texture_id](
53+
auto engine, auto texture_id) {
54+
register_called = true;
55+
registered_texture_id = texture_id;
56+
return kSuccess;
57+
}));
58+
59+
bool unregister_called = false;
60+
modifier.embedder_api().UnregisterExternalTexture = MOCK_ENGINE_PROC(
61+
UnregisterExternalTexture, ([&unregister_called, &registered_texture_id](
62+
auto engine, auto texture_id) {
63+
unregister_called = true;
64+
EXPECT_EQ(registered_texture_id, texture_id);
65+
return kSuccess;
66+
}));
67+
68+
bool mark_frame_available_called = false;
69+
modifier.embedder_api().MarkExternalTextureFrameAvailable =
70+
MOCK_ENGINE_PROC(MarkExternalTextureFrameAvailable,
71+
([&mark_frame_available_called, &registered_texture_id](
72+
auto engine, auto texture_id) {
73+
mark_frame_available_called = true;
74+
EXPECT_EQ(registered_texture_id, texture_id);
75+
return kSuccess;
76+
}));
77+
78+
auto texture_id = registrar.RegisterTexture(&texture_info);
79+
EXPECT_TRUE(register_called);
80+
EXPECT_NE(texture_id, -1);
81+
EXPECT_EQ(texture_id, registered_texture_id);
82+
83+
EXPECT_TRUE(registrar.MarkTextureFrameAvailable(texture_id));
84+
EXPECT_TRUE(mark_frame_available_called);
85+
86+
EXPECT_TRUE(registrar.UnregisterTexture(texture_id));
87+
EXPECT_TRUE(unregister_called);
88+
}
89+
90+
TEST(FlutterTizenTextureRegistrarTest, RegisterUnknownTextureType) {
91+
std::unique_ptr<FlutterTizenEngine> engine = GetTestEngine();
92+
EngineModifier modifier(engine.get());
93+
94+
FlutterTizenTextureRegistrar registrar(engine.get());
95+
96+
FlutterDesktopTextureInfo texture_info = {};
97+
texture_info.type = static_cast<FlutterDesktopTextureType>(1234);
98+
99+
auto texture_id = registrar.RegisterTexture(&texture_info);
100+
101+
EXPECT_EQ(texture_id, -1);
102+
}
103+
104+
TEST(FlutterTizenTextureRegistrarTest, PopulateInvalidTexture) {
105+
std::unique_ptr<FlutterTizenEngine> engine = GetTestEngine();
106+
107+
FlutterTizenTextureRegistrar registrar(engine.get());
108+
109+
auto result = registrar.PopulateTexture(1, 640, 480, nullptr);
110+
EXPECT_FALSE(result);
111+
}
112+
113+
} // namespace testing
114+
} // 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)