Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
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
6
7#include "impeller/toolkit/interop/impeller.hpp"
8
9#if IMPELLER_ENABLE_METAL
12#endif // IMPELLER_ENABLE_METAL
13
14#if IMPELLER_ENABLE_OPENGLES
17#endif // IMPELLER_ENABLE_METAL
18
19#if IMPELLER_ENABLE_VULKAN
22#endif // IMPELLER_ENABLE_VULKAN
23
26} // namespace IMPELLER_HPP_NAMESPACE
27
29
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
46
48
49// |PlaygroundTest|
53
54// |PlaygroundTest|
58
60 switch (GetBackend()) {
63 return Adopt<Context>(
67 Playground::GLProcAddressResolver playground_gl_proc_address_callback =
69 ImpellerProcAddressCallback gl_proc_address_callback =
70 [](const char* proc_name, void* user_data) -> void* {
71 return (*reinterpret_cast<Playground::GLProcAddressResolver*>(
72 user_data))(proc_name);
73 };
74 return Adopt<Context>(ImpellerContextCreateOpenGLESNew(
75 ImpellerGetVersion(), gl_proc_address_callback,
76 &playground_gl_proc_address_callback));
77 }
80 struct UserData {
82 } user_data;
84 settings.user_data = &user_data;
86 settings.proc_address_callback = [](void* instance, //
87 const char* proc_name, //
88 void* user_data //
89 ) -> void* {
90 auto resolver = reinterpret_cast<UserData*>(user_data)->resolver;
91 if (resolver) {
92 return resolver(instance, proc_name);
93 } else {
94 return nullptr;
95 }
96 };
97 return Adopt<Context>(
99 }
101}
102
104 PlaygroundBackend backend,
106 std::shared_ptr<impeller::Surface> shared_surface) {
107 switch (backend) {
108#if IMPELLER_ENABLE_METAL
111 return Adopt<Surface>(new SurfaceMTL(context, std::move(shared_surface)));
112#endif
113
114#if IMPELLER_ENABLE_OPENGLES
117 return Adopt<Surface>(
118 new SurfaceGLES(context, std::move(shared_surface)));
119#endif
120
121#if IMPELLER_ENABLE_VULKAN
123 return Adopt<Surface>(new SurfaceVK(context, std::move(shared_surface)));
124#endif
125 default:
126 return nullptr;
127 }
129}
130
132 auto interop_context = GetInteropContext();
133 if (!interop_context) {
134 return false;
135 }
137 auto impeller_surface = std::make_shared<impeller::Surface>(target);
139 *interop_context.Get(), //
140 std::move(impeller_surface) //
141 );
142 if (!surface) {
143 VALIDATION_LOG << "Could not wrap test surface as an interop surface.";
144 return false;
145 }
146 return callback(interop_context, surface);
147 });
148}
149
151 PlaygroundBackend backend,
152 std::shared_ptr<impeller::Context> shared_context) {
153 switch (backend) {
154#if IMPELLER_ENABLE_METAL
157 return ContextMTL::Create(shared_context);
158#endif
159#if IMPELLER_ENABLE_OPENGLES
162 return ContextGLES::Create(std::move(shared_context));
163#endif
164#if IMPELLER_ENABLE_VULKAN
166 return ContextVK::Create(std::move(shared_context));
167#endif
168 default:
169 return nullptr;
170 }
172}
173
175 if (interop_context_) {
176 return interop_context_;
177 }
178
180 if (!context) {
181 return nullptr;
182 }
183 interop_context_ = std::move(context);
184 return interop_context_;
185}
186
188 auto c_context = GetInteropContext().GetC();
189 ImpellerContextRetain(c_context);
190 return hpp::Context{c_context, hpp::AdoptTag::kAdopt};
191}
192
193std::unique_ptr<hpp::Mapping> PlaygroundTest::OpenAssetAsHPPMapping(
194 std::string asset_name) const {
195 std::shared_ptr<fml::Mapping> data =
196 OpenAssetAsMapping(std::move(asset_name));
197 if (!data) {
198 return nullptr;
199 }
200 return std::make_unique<hpp::Mapping>(data->GetMapping(), //
201 data->GetSize(), //
202 [data]() {} //
203 );
204}
205
206hpp::Texture PlaygroundTest::OpenAssetAsHPPTexture(std::string asset_name) {
207 auto compressed_data = OpenAssetAsMapping(std::move(asset_name));
208 if (!compressed_data) {
209 return {nullptr, hpp::AdoptTag::kAdopt};
210 }
211 auto compressed_image =
212 LoadFixtureImageCompressed(std::move(compressed_data));
213 if (!compressed_image) {
214 return {nullptr, hpp::AdoptTag::kAdopt};
215 }
216 auto decompressed_image = DecodeImageRGBA(compressed_image);
217 if (!decompressed_image.has_value()) {
218 return {nullptr, hpp::AdoptTag::kAdopt};
219 }
220 auto rgba_decompressed_image =
221 std::make_shared<DecompressedImage>(decompressed_image->ConvertToRGBA());
222 if (!rgba_decompressed_image || !rgba_decompressed_image->IsValid()) {
223 return {nullptr, hpp::AdoptTag::kAdopt};
224 }
225 auto context = GetHPPContext();
226 if (!context) {
227 return {nullptr, hpp::AdoptTag::kAdopt};
228 }
229
230 auto rgba_mapping = std::make_unique<hpp::Mapping>(
231 rgba_decompressed_image->GetAllocation()->GetMapping(),
232 rgba_decompressed_image->GetAllocation()->GetSize(),
233 [rgba_decompressed_image]() {});
234
235 return hpp::Texture::WithContents(
236 context,
239 .size = {rgba_decompressed_image->GetSize().width,
240 rgba_decompressed_image->GetSize().height},
241 .mip_count = 1u,
242 },
243 std::move(rgba_mapping));
244}
245
246} // namespace impeller::interop::testing
bool OpenPlaygroundHere(const RenderCallback &render_callback)
static std::shared_ptr< CompressedImage > LoadFixtureImageCompressed(std::shared_ptr< fml::Mapping > mapping)
GLProcAddressResolver CreateGLProcAddressResolver() const
const PlaygroundSwitches switches_
Definition playground.h:123
std::shared_ptr< Context > GetContext() const
Definition playground.cc:96
std::function< void *(void *instance, const char *proc_name)> VKProcAddressResolver
Definition playground.h:112
std::function< void *(const char *proc_name)> GLProcAddressResolver
Definition playground.h:108
static std::optional< DecompressedImage > DecodeImageRGBA(const std::shared_ptr< CompressedImage > &compressed)
VKProcAddressResolver CreateVKProcAddressResolver() const
std::unique_ptr< fml::Mapping > OpenAssetAsMapping(std::string asset_name) const override
PlaygroundBackend GetBackend() const
static ScopedObject< Context > Create(std::function< void *(const char *gl_proc_name)> proc_address_callback)
static ScopedObject< Context > Create()
static ScopedObject< Context > Create(const Settings &settings)
Definition context_vk.cc:44
hpp::Texture OpenAssetAsHPPTexture(std::string asset_name)
ScopedObject< Context > CreateContext() const
std::unique_ptr< hpp::Mapping > OpenAssetAsHPPMapping(std::string asset_name) const
std::function< bool(const ScopedObject< Context > &context, const ScopedObject< Surface > &surface)> InteropPlaygroundCallback
bool OpenPlaygroundHere(InteropPlaygroundCallback callback)
VkInstance instance
Definition main.cc:64
VkSurfaceKHR surface
Definition main.cc:65
uint32_t * target
FlutterDesktopBinaryReply callback
#define FML_UNREACHABLE()
Definition logging.h:128
const char * name
Definition fuchsia.cc:50
void *IMPELLER_NULLABLE(* ImpellerProcAddressCallback)(const char *IMPELLER_NONNULL proc_name, void *IMPELLER_NULLABLE user_data)
Definition impeller.h:347
@ kImpellerPixelFormatRGBA8888
Definition impeller.h:425
static ScopedObject< Surface > CreateSharedSurface(PlaygroundBackend backend, Context &context, std::shared_ptr< impeller::Surface > shared_surface)
static ScopedObject< Context > CreateSharedContext(PlaygroundBackend backend, std::shared_ptr< impeller::Context > shared_context)
static void SetupImpellerHPPProcTableOnce()
IMPELLER_EXTERN_C uint32_t ImpellerGetVersion()
Definition impeller.cc:92
IMPELLER_EXTERN_C ImpellerContext ImpellerContextCreateVulkanNew(uint32_t version, const ImpellerContextVulkanSettings *settings)
Definition impeller.cc:152
IMPELLER_EXTERN_C ImpellerContext ImpellerContextCreateMetalNew(uint32_t version)
Definition impeller.cc:134
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_EXTERN_C void ImpellerContextRetain(ImpellerContext context)
Definition impeller.cc:171
PlaygroundBackend
Definition playground.h:26
std::shared_ptr< ContextGLES > context
int32_t width
ImpellerVulkanProcAddressCallback IMPELLER_NONNULL proc_address_callback
Definition impeller.h:634
void *IMPELLER_NULLABLE user_data
Definition impeller.h:633
ImpellerPixelFormat pixel_format
Definition impeller.h:621
#define IMPELLER_HPP_PROC(name)
#define VALIDATION_LOG
Definition validation.h:91