| Flutter Impeller |

playground_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 "impeller/toolkit/interop/playground_test.h"
6
7 #include "impeller/toolkit/interop/impeller.hpp"
8
9 #if IMPELLER_ENABLE_METAL
10 #include "impeller/toolkit/interop/backend/metal/context_mtl.h"
11 #include "impeller/toolkit/interop/backend/metal/surface_mtl.h"
12 #endif // IMPELLER_ENABLE_METAL
13
14 #if IMPELLER_ENABLE_OPENGLES
15 #include "impeller/toolkit/interop/backend/gles/context_gles.h"
16 #include "impeller/toolkit/interop/backend/gles/surface_gles.h"
17 #endif // IMPELLER_ENABLE_METAL
18
19 #if IMPELLER_ENABLE_VULKAN
20 #include "impeller/toolkit/interop/backend/vulkan/context_vk.h"
21 #include "impeller/toolkit/interop/backend/vulkan/surface_vk.h"
22 #endif // IMPELLER_ENABLE_VULKAN
23
24 namespace IMPELLER_HPP_NAMESPACE {
25 ProcTable gGlobalProcTable;
26 } // namespace IMPELLER_HPP_NAMESPACE
27
28 namespace impeller::interop::testing {
29
30 static void SetupImpellerHPPProcTableOnce() {
31 static std::once_flag sOnceFlag;
32 std::call_once(sOnceFlag, []() {
33 std::map<std::string, void*> proc_map;
34 #define IMPELLER_HPP_PROC(name) \
35 proc_map[#name] = reinterpret_cast<void*>(&name);
36 IMPELLER_HPP_EACH_PROC(IMPELLER_HPP_PROC)
37 #undef IMPELLER_HPP_PROC
38 hpp::gGlobalProcTable.Initialize(
39 [&](auto name) { return proc_map.at(name); });
40 });
41 }
42
43 PlaygroundTest::PlaygroundTest() {
44 SetupImpellerHPPProcTableOnce();
45 }
46
47 PlaygroundTest::~PlaygroundTest() = default;
48
49 // |PlaygroundTest|
50 void PlaygroundTest::SetUp() {
51 ::impeller::PlaygroundTest::SetUp();
52 }
53
54 // |PlaygroundTest|
55 void PlaygroundTest::TearDown() {
56 ::impeller::PlaygroundTest::TearDown();
57 }
58
59 ScopedObject<Context> PlaygroundTest::CreateContext() const {
60 switch (GetBackend()) {
61 case PlaygroundBackend::kMetal:
62 return Adopt<Context>(
63 ImpellerContextCreateMetalNew(ImpellerGetVersion()));
64 case PlaygroundBackend::kOpenGLES: {
65 Playground::GLProcAddressResolver playground_gl_proc_address_callback =
66 CreateGLProcAddressResolver();
67 ImpellerProcAddressCallback gl_proc_address_callback =
68 [](const char* proc_name, void* user_data) -> void* {
69 return (*reinterpret_cast<Playground::GLProcAddressResolver*>(
70 user_data))(proc_name);
71 };
72 return Adopt<Context>(ImpellerContextCreateOpenGLESNew(
73 ImpellerGetVersion(), gl_proc_address_callback,
74 &playground_gl_proc_address_callback));
75 }
76 case PlaygroundBackend::kVulkan:
77 ImpellerContextVulkanSettings settings = {};
78 struct UserData {
79 Playground::VKProcAddressResolver resolver;
80 } user_data;
81 user_data.resolver = CreateVKProcAddressResolver();
82 settings.user_data = &user_data;
83 settings.enable_vulkan_validation = switches_.enable_vulkan_validation;
84 settings.proc_address_callback = [](void* instance, //
85 const char* proc_name, //
86 void* user_data //
87 ) -> void* {
88 auto resolver = reinterpret_cast<UserData*>(user_data)->resolver;
89 if (resolver) {
90 return resolver(instance, proc_name);
91 } else {
92 return nullptr;
93 }
94 };
95 return Adopt<Context>(
96 ImpellerContextCreateVulkanNew(ImpellerGetVersion(), &settings));
97 }
98 FML_UNREACHABLE();
99 }
100
101 static ScopedObject<Surface> CreateSharedSurface(
102 PlaygroundBackend backend,
103 Context& context,
104 std::shared_ptr<impeller::Surface> shared_surface) {
105 switch (backend) {
106 #if IMPELLER_ENABLE_METAL
107 case PlaygroundBackend::kMetal:
108 return Adopt<Surface>(new SurfaceMTL(context, std::move(shared_surface)));
109 #endif
110
111 #if IMPELLER_ENABLE_OPENGLES
112 case PlaygroundBackend::kOpenGLES:
113 return Adopt<Surface>(
114 new SurfaceGLES(context, std::move(shared_surface)));
115 #endif
116
117 #if IMPELLER_ENABLE_VULKAN
118 case PlaygroundBackend::kVulkan:
119 return Adopt<Surface>(new SurfaceVK(context, std::move(shared_surface)));
120 #endif
121 default:
122 return nullptr;
123 }
124 FML_UNREACHABLE();
125 }
126
127 bool PlaygroundTest::OpenPlaygroundHere(InteropPlaygroundCallback callback) {
128 auto interop_context = GetInteropContext();
129 if (!interop_context) {
130 return false;
131 }
132 return Playground::OpenPlaygroundHere([&](RenderTarget& target) -> bool {
133 auto impeller_surface = std::make_shared<impeller::Surface>(target);
134 auto surface = CreateSharedSurface(GetBackend(), //
135 *interop_context.Get(), //
136 std::move(impeller_surface) //
137 );
138 if (!surface) {
139 VALIDATION_LOG << "Could not wrap test surface as an interop surface.";
140 return false;
141 }
142 return callback(interop_context, surface);
143 });
144 }
145
146 static ScopedObject<Context> CreateSharedContext(
147 PlaygroundBackend backend,
148 std::shared_ptr<impeller::Context> shared_context) {
149 switch (backend) {
150 #if IMPELLER_ENABLE_METAL
151 case PlaygroundBackend::kMetal:
152 return ContextMTL::Create(shared_context);
153 #endif
154 #if IMPELLER_ENABLE_OPENGLES
155 case PlaygroundBackend::kOpenGLES:
156 return ContextGLES::Create(std::move(shared_context));
157 #endif
158 #if IMPELLER_ENABLE_VULKAN
159 case PlaygroundBackend::kVulkan:
160 return ContextVK::Create(std::move(shared_context));
161 #endif
162 default:
163 return nullptr;
164 }
165 FML_UNREACHABLE();
166 }
167
168 ScopedObject<Context> PlaygroundTest::GetInteropContext() {
169 if (interop_context_) {
170 return interop_context_;
171 }
172
173 auto context = CreateSharedContext(GetBackend(), GetContext());
174 if (!context) {
175 return nullptr;
176 }
177 interop_context_ = std::move(context);
178 return interop_context_;
179 }
180
181 hpp::Context PlaygroundTest::GetHPPContext() {
182 auto c_context = GetInteropContext().GetC();
183 ImpellerContextRetain(c_context);
184 return hpp::Context{c_context, hpp::AdoptTag::kAdopt};
185 }
186
187 std::unique_ptr<hpp::Mapping> PlaygroundTest::OpenAssetAsHPPMapping(
188 std::string asset_name) const {
189 std::shared_ptr<fml::Mapping> data =
190 OpenAssetAsMapping(std::move(asset_name));
191 if (!data) {
192 return nullptr;
193 }
194 return std::make_unique<hpp::Mapping>(data->GetMapping(), //
195 data->GetSize(), //
196 [data]() {} //
197 );
198 }
199
200 hpp::Texture PlaygroundTest::OpenAssetAsHPPTexture(std::string asset_name) {
201 auto compressed_data = OpenAssetAsMapping(std::move(asset_name));
202 if (!compressed_data) {
203 return {nullptr, hpp::AdoptTag::kAdopt};
204 }
205 auto compressed_image =
206 LoadFixtureImageCompressed(std::move(compressed_data));
207 if (!compressed_image) {
208 return {nullptr, hpp::AdoptTag::kAdopt};
209 }
210 auto decompressed_image = DecodeImageRGBA(compressed_image);
211 if (!decompressed_image.has_value()) {
212 return {nullptr, hpp::AdoptTag::kAdopt};
213 }
214 auto rgba_decompressed_image =
215 std::make_shared<DecompressedImage>(decompressed_image->ConvertToRGBA());
216 if (!rgba_decompressed_image || !rgba_decompressed_image->IsValid()) {
217 return {nullptr, hpp::AdoptTag::kAdopt};
218 }
219 auto context = GetHPPContext();
220 if (!context) {
221 return {nullptr, hpp::AdoptTag::kAdopt};
222 }
223
224 auto rgba_mapping = std::make_unique<hpp::Mapping>(
225 rgba_decompressed_image->GetAllocation()->GetMapping(),
226 rgba_decompressed_image->GetAllocation()->GetSize(),
227 [rgba_decompressed_image]() {});
228
229 return hpp::Texture::WithContents(
230 context,
231 ImpellerTextureDescriptor{
232 .pixel_format = kImpellerPixelFormatRGBA8888,
233 .size = {rgba_decompressed_image->GetSize().width,
234 rgba_decompressed_image->GetSize().height},
235 .mip_count = 1u,
236 },
237 std::move(rgba_mapping));
238 }
239
240 } // namespace impeller::interop::testing
impeller::Playground::OpenPlaygroundHere
bool OpenPlaygroundHere(const RenderCallback &render_callback)
Definition: playground.cc:201
impeller::Playground::LoadFixtureImageCompressed
static std::shared_ptr< CompressedImage > LoadFixtureImageCompressed(std::shared_ptr< fml::Mapping > mapping)
Definition: playground.cc:361
impeller::Playground::CreateGLProcAddressResolver
GLProcAddressResolver CreateGLProcAddressResolver() const
Definition: playground.cc:520
impeller::Playground::switches_
const PlaygroundSwitches switches_
Definition: playground.h:131
impeller::Playground::GetContext
std::shared_ptr< Context > GetContext() const
Definition: playground.cc:91
impeller::Playground::VKProcAddressResolver
std::function< void *(void *instance, const char *proc_name)> VKProcAddressResolver
Definition: playground.h:122
impeller::Playground::GLProcAddressResolver
std::function< void *(const char *proc_name)> GLProcAddressResolver
Definition: playground.h:118
impeller::Playground::DecodeImageRGBA
static std::optional< DecompressedImage > DecodeImageRGBA(const std::shared_ptr< CompressedImage > &compressed)
Definition: playground.cc:372
impeller::Playground::CreateVKProcAddressResolver
VKProcAddressResolver CreateVKProcAddressResolver() const
Definition: playground.cc:525
impeller::PlaygroundTest::OpenAssetAsMapping
std::unique_ptr< fml::Mapping > OpenAssetAsMapping(std::string asset_name) const override
Definition: playground_test.cc:84
impeller::PlaygroundTest::GetBackend
PlaygroundBackend GetBackend() const
Definition: playground_test.cc:75
impeller::PlaygroundTest::TearDown
void TearDown() override
Definition: playground_test.cc:79
impeller::PlaygroundTest::SetUp
void SetUp() override
Definition: playground_test.cc:45
impeller::RenderTarget
Definition: render_target.h:38
impeller::interop::ContextGLES::Create
static ScopedObject< Context > Create(std::function< void *(const char *gl_proc_name)> proc_address_callback)
Definition: context_gles.cc:14
impeller::interop::Context
Definition: context.h:16
impeller::interop::ContextMTL::Create
static ScopedObject< Context > Create()
Definition: context_mtl.mm:29
impeller::interop::ContextVK::Create
static ScopedObject< Context > Create(const Settings &settings)
Definition: context_vk.cc:44
impeller::interop::ScopedObject
Definition: object.h:67
impeller::interop::SurfaceGLES
Definition: surface_gles.h:12
impeller::interop::SurfaceMTL
Definition: surface_mtl.h:12
impeller::interop::SurfaceVK
Definition: surface_vk.h:12
impeller::interop::testing::PlaygroundTest::OpenAssetAsHPPTexture
hpp::Texture OpenAssetAsHPPTexture(std::string asset_name)
Definition: playground_test.cc:200
impeller::interop::testing::PlaygroundTest::SetUp
void SetUp() override
Definition: playground_test.cc:50
impeller::interop::testing::PlaygroundTest::CreateContext
ScopedObject< Context > CreateContext() const
Definition: playground_test.cc:59
impeller::interop::testing::PlaygroundTest::OpenAssetAsHPPMapping
std::unique_ptr< hpp::Mapping > OpenAssetAsHPPMapping(std::string asset_name) const
Definition: playground_test.cc:187
impeller::interop::testing::PlaygroundTest::GetInteropContext
ScopedObject< Context > GetInteropContext()
Definition: playground_test.cc:168
impeller::interop::testing::PlaygroundTest::~PlaygroundTest
~PlaygroundTest() override
impeller::interop::testing::PlaygroundTest::TearDown
void TearDown() override
Definition: playground_test.cc:55
impeller::interop::testing::PlaygroundTest::InteropPlaygroundCallback
std::function< bool(const ScopedObject< Context > &context, const ScopedObject< Surface > &surface)> InteropPlaygroundCallback
Definition: playground_test.h:44
impeller::interop::testing::PlaygroundTest::GetHPPContext
hpp::Context GetHPPContext()
Definition: playground_test.cc:181
impeller::interop::testing::PlaygroundTest::OpenPlaygroundHere
bool OpenPlaygroundHere(InteropPlaygroundCallback callback)
Definition: playground_test.cc:127
impeller::interop::testing::PlaygroundTest::PlaygroundTest
PlaygroundTest()
Definition: playground_test.cc:43
ImpellerProcAddressCallback
void *IMPELLER_NULLABLE(* ImpellerProcAddressCallback)(const char *IMPELLER_NONNULL proc_name, void *IMPELLER_NULLABLE user_data)
Definition: impeller.h:347
kImpellerPixelFormatRGBA8888
@ kImpellerPixelFormatRGBA8888
Definition: impeller.h:425
impeller.hpp
IMPELLER_HPP_EACH_PROC
#define IMPELLER_HPP_EACH_PROC(PROC)
Definition: impeller.hpp:48
IMPELLER_HPP_NAMESPACE
Definition: impeller.hpp:32
IMPELLER_HPP_NAMESPACE::gGlobalProcTable
ProcTable gGlobalProcTable
Definition: playground_test.cc:25
impeller::interop::testing
Definition: impeller_unittests.cc:25
impeller::interop::testing::CreateSharedContext
static ScopedObject< Context > CreateSharedContext(PlaygroundBackend backend, std::shared_ptr< impeller::Context > shared_context)
Definition: playground_test.cc:146
impeller::interop::testing::CreateSharedSurface
static ScopedObject< Surface > CreateSharedSurface(PlaygroundBackend backend, Context &context, std::shared_ptr< impeller::Surface > shared_surface)
Definition: playground_test.cc:101
impeller::interop::testing::SetupImpellerHPPProcTableOnce
static void SetupImpellerHPPProcTableOnce()
Definition: playground_test.cc:30
impeller::interop::ImpellerGetVersion
IMPELLER_EXTERN_C uint32_t ImpellerGetVersion()
Definition: impeller.cc:92
impeller::interop::ImpellerContextCreateVulkanNew
IMPELLER_EXTERN_C ImpellerContext ImpellerContextCreateVulkanNew(uint32_t version, const ImpellerContextVulkanSettings *settings)
Definition: impeller.cc:152
impeller::interop::ImpellerContextCreateMetalNew
IMPELLER_EXTERN_C ImpellerContext ImpellerContextCreateMetalNew(uint32_t version)
Definition: impeller.cc:134
impeller::interop::ImpellerContextCreateOpenGLESNew
IMPELLER_EXTERN_C ImpellerContext ImpellerContextCreateOpenGLESNew(uint32_t version, ImpellerProcAddressCallback gl_proc_address_callback, void *gl_proc_address_callback_user_data)
Definition: impeller.cc:108
impeller::interop::ImpellerContextRetain
IMPELLER_EXTERN_C void ImpellerContextRetain(ImpellerContext context)
Definition: impeller.cc:171
impeller::PlaygroundBackend
PlaygroundBackend
Definition: playground.h:27
impeller::PlaygroundBackend::kOpenGLES
@ kOpenGLES
impeller::PlaygroundBackend::kMetal
@ kMetal
impeller::PlaygroundBackend::kVulkan
@ kVulkan
IMPELLER_HPP_NAMESPACE::ProcTable
Definition: impeller.hpp:226
IMPELLER_HPP_NAMESPACE::ProcTable::Initialize
bool Initialize(const std::function< void *(const char *function_name)> &resolver)
Definition: impeller.hpp:227
ImpellerContextVulkanSettings
Definition: impeller.h:632
ImpellerContextVulkanSettings::proc_address_callback
ImpellerVulkanProcAddressCallback IMPELLER_NONNULL proc_address_callback
Definition: impeller.h:634
ImpellerContextVulkanSettings::enable_vulkan_validation
bool enable_vulkan_validation
Definition: impeller.h:635
ImpellerContextVulkanSettings::user_data
void *IMPELLER_NULLABLE user_data
Definition: impeller.h:633
ImpellerTextureDescriptor
Definition: impeller.h:620
ImpellerTextureDescriptor::pixel_format
ImpellerPixelFormat pixel_format
Definition: impeller.h:621
impeller::PlaygroundSwitches::enable_vulkan_validation
bool enable_vulkan_validation
Definition: switches.h:22
data
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:68
context_gles.h
surface_gles.h
context_mtl.h
surface_mtl.h
context_vk.h
surface_vk.h
IMPELLER_HPP_PROC
#define IMPELLER_HPP_PROC(name)
playground_test.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91