Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
embedder_unittests_util.h
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#ifndef FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_UNITTESTS_UTIL_H_
6#define FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_UNITTESTS_UTIL_H_
7
8#define FML_USED_ON_EMBEDDER
9
10#include <future>
11#include <utility>
12
13#include "flutter/fml/mapping.h"
15#include "flutter/fml/paths.h"
19#include "third_party/skia/include/core/SkSurface.h"
20
21namespace flutter {
22namespace testing {
23
24sk_sp<SkSurface> CreateRenderSurface(const FlutterLayer& layer,
25 GrDirectContext* context);
26
27bool RasterImagesAreSame(const sk_sp<SkImage>& a, const sk_sp<SkImage>& b);
28
29/// @brief Prepends a prefix to the name which is unique to the test
30/// context type. This is useful for tests that use
31/// EmbedderTestMultiBackend and require different fixtures per
32/// backend. For OpenGL, the name remains unchanged.
33/// @param[in] backend The test context type used to determine the prepended
34/// prefix (e.g. `vk_[name]` for Vulkan).
35/// @param[in] name The name of the fixture without any special prefixes.
37 const std::string& name);
38
39/// @brief Resolves a render target type for a given backend description.
40/// This is useful for tests that use EmbedderTestMultiBackend.
41/// @param[in] backend The test context type to resolve the render
42/// target for.
43/// @param[in] opengl_framebuffer Ignored for all non-OpenGL backends. Flutter
44/// supports rendering to both OpenGL textures
45/// and framebuffers. When false, the OpenGL
46/// texture render target type is returned.
49 bool opengl_framebuffer);
50
51/// @brief Configures per-backend properties for a given backing store.
52/// @param[in] backing_store The backing store to configure.
53/// @param[in] backend The test context type used to decide which
54/// backend the backing store will be used with.
55/// @param[in] opengl_framebuffer Ignored for all non-OpenGL backends. Flutter
56/// supports rendering to both OpenGL textures
57/// and framebuffers. When false, the backing
58/// store is configured to be an OpenGL texture.
61 bool opengl_framebuffer);
62
63bool WriteImageToDisk(const fml::UniqueFD& directory,
64 const std::string& name,
65 const sk_sp<SkImage>& image);
66
67bool ImageMatchesFixture(const std::string& fixture_file_name,
68 const sk_sp<SkImage>& scene_image,
69 int allowable_different_pixels = 0);
70
71bool ImageMatchesFixture(const std::string& fixture_file_name,
72 std::future<sk_sp<SkImage>>& scene_image,
73 int allowable_different_pixels = 0);
74
75bool SurfacePixelDataMatchesBytes(SkSurface* surface,
76 const std::vector<uint8_t>& bytes);
77
78bool SurfacePixelDataMatchesBytes(std::future<SkSurface*>& surface_future,
79 const std::vector<uint8_t>& bytes);
80
82 const FlutterPlatformViewMutation** mutations,
83 size_t count,
85 const std::function<void(const FlutterPlatformViewMutation& mutation)>&
86 handler);
87
89 const FlutterPlatformView* view,
91 const std::function<void(const FlutterPlatformViewMutation& mutation)>&
92 handler);
93
95 const FlutterPlatformViewMutation** mutations,
96 size_t count);
97
99
100//------------------------------------------------------------------------------
101/// @brief A task runner that we expect the embedder to provide but whose
102/// implementation is a real FML task runner.
103///
105 public:
106 using TaskExpiryCallback = std::function<void(FlutterTask)>;
107 using DestructionCallback = std::function<void()>;
108
110 TaskExpiryCallback on_task_expired)
111 : EmbedderTestTaskRunner(std::move(real_task_runner),
112 std::move(on_task_expired),
113 {}) {}
114
116 TaskExpiryCallback on_task_expired,
117 std::function<void()> destruction_callback)
118 : identifier_(++sEmbedderTaskRunnerIdentifiers),
119 real_task_runner_(std::move(real_task_runner)),
120 on_task_expired_(std::move(on_task_expired)),
121 destruction_callback_(std::move(destruction_callback)) {
122 FML_CHECK(real_task_runner_);
123 FML_CHECK(on_task_expired_);
124
125 task_runner_description_.struct_size = sizeof(FlutterTaskRunnerDescription);
126 task_runner_description_.user_data = this;
127 task_runner_description_.runs_task_on_current_thread_callback =
128 [](void* user_data) -> bool {
129 return reinterpret_cast<EmbedderTestTaskRunner*>(user_data)
130 ->real_task_runner_->RunsTasksOnCurrentThread();
131 };
132 task_runner_description_.post_task_callback = [](FlutterTask task,
133 uint64_t target_time_nanos,
134 void* user_data) -> void {
135 auto thiz = reinterpret_cast<EmbedderTestTaskRunner*>(user_data);
136
137 auto target_time = fml::TimePoint::FromEpochDelta(
138 fml::TimeDelta::FromNanoseconds(target_time_nanos));
139 auto on_task_expired = thiz->on_task_expired_;
140 auto invoke_task = [task, on_task_expired]() { on_task_expired(task); };
141 auto real_task_runner = thiz->real_task_runner_;
142
143 real_task_runner->PostTaskForTime(invoke_task, target_time);
144 };
145 if (destruction_callback_) {
146 task_runner_description_.destruction_callback = [](void* user_data) {
147 auto thiz = reinterpret_cast<EmbedderTestTaskRunner*>(user_data);
148 thiz->destruction_callback_();
149 };
150 } else {
151 task_runner_description_.destruction_callback = [](void* user_data) {};
152 }
153 task_runner_description_.identifier = identifier_;
154 }
155
157 return task_runner_description_;
158 }
159
160 private:
161 static std::atomic_size_t sEmbedderTaskRunnerIdentifiers;
162 const size_t identifier_;
163 const fml::RefPtr<fml::TaskRunner> real_task_runner_;
164 const TaskExpiryCallback on_task_expired_;
165 const DestructionCallback destruction_callback_;
166 FlutterTaskRunnerDescription task_runner_description_ = {};
167
168 FML_DISALLOW_COPY_AND_ASSIGN(EmbedderTestTaskRunner);
169};
170
172 public:
174 fml::RefPtr<fml::TaskRunner> task_runner) {
175 real_task_runner_ = std::move(task_runner);
176 return *this;
177 }
178
184
187 destruction_callback_ = std::move(callback);
188 return *this;
189 }
190
192 return EmbedderTestTaskRunner(real_task_runner_, on_task_expired_,
193 destruction_callback_);
194 }
195
196 private:
197 fml::RefPtr<fml::TaskRunner> real_task_runner_;
200};
201
202} // namespace testing
203} // namespace flutter
204
205#endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_TESTS_EMBEDDER_UNITTESTS_UTIL_H_
EmbedderTestTaskRunnerBuilder & SetRealTaskRunner(fml::RefPtr< fml::TaskRunner > task_runner)
EmbedderTestTaskRunnerBuilder & SetTaskExpiryCallback(EmbedderTestTaskRunner::TaskExpiryCallback callback)
EmbedderTestTaskRunnerBuilder & SetDestructionCallback(EmbedderTestTaskRunner::DestructionCallback callback)
A task runner that we expect the embedder to provide but whose implementation is a real FML task runn...
const FlutterTaskRunnerDescription & GetFlutterTaskRunnerDescription()
EmbedderTestTaskRunner(fml::RefPtr< fml::TaskRunner > real_task_runner, TaskExpiryCallback on_task_expired)
EmbedderTestTaskRunner(fml::RefPtr< fml::TaskRunner > real_task_runner, TaskExpiryCallback on_task_expired, std::function< void()> destruction_callback)
std::function< void(FlutterTask)> TaskExpiryCallback
virtual bool RunsTasksOnCurrentThread()
static constexpr TimeDelta FromNanoseconds(int64_t nanos)
Definition time_delta.h:40
static constexpr TimePoint FromEpochDelta(TimeDelta ticks)
Definition time_point.h:43
FlutterPlatformViewMutationType
Definition embedder.h:2035
FlutterVulkanImage * image
const gchar FlBinaryMessengerMessageHandler handler
FlutterDesktopBinaryReply callback
#define FML_CHECK(condition)
Definition logging.h:104
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
bool WriteImageToDisk(const fml::UniqueFD &directory, const std::string &name, const sk_sp< SkImage > &image)
sk_sp< SkSurface > CreateRenderSurface(const FlutterLayer &layer, GrDirectContext *context)
void FilterMutationsByType(const FlutterPlatformViewMutation **mutations, size_t count, FlutterPlatformViewMutationType type, const std::function< void(const FlutterPlatformViewMutation &mutation)> &handler)
bool SurfacePixelDataMatchesBytes(SkSurface *surface, const std::vector< uint8_t > &bytes)
bool ImageMatchesFixture(const std::string &fixture_file_name, const sk_sp< SkImage > &scene_image, int allowable_different_pixels)
void ConfigureBackingStore(FlutterBackingStore &backing_store, EmbedderTestContextType backend, bool opengl_framebuffer)
Configures per-backend properties for a given backing store.
std::string FixtureNameForBackend(EmbedderTestContextType backend, const std::string &name)
Prepends a prefix to the name which is unique to the test context type. This is useful for tests that...
EmbedderTestBackingStoreProducer::RenderTargetType GetRenderTargetFromBackend(EmbedderTestContextType backend, bool opengl_framebuffer)
Resolves a render target type for a given backend description. This is useful for tests that use Embe...
bool RasterImagesAreSame(const sk_sp< SkImage > &a, const sk_sp< SkImage > &b, int allowable_different_pixels)
DlMatrix GetTotalMutationTransformationMatrix(const FlutterPlatformViewMutation **mutations, size_t count)
DEF_SWITCHES_START aot vmservice shared library name
Definition switch_defs.h:27
Definition ref_ptr.h:261
impeller::ShaderType type
size_t struct_size
The size of this struct. Must be sizeof(FlutterTaskRunnerDescription).
Definition embedder.h:1902
BoolCallback runs_task_on_current_thread_callback
Definition embedder.h:1908
FlutterTaskRunnerPostTaskCallback post_task_callback
Definition embedder.h:1919
VoidCallback destruction_callback
The callback invoked when the task runner is destroyed.
Definition embedder.h:1924
A 4x4 matrix using column-major storage.
Definition matrix.h:37