Flutter Impeller
impeller::testing::RendererDartTest Class Reference
Inheritance diagram for impeller::testing::RendererDartTest:
impeller::PlaygroundTest impeller::Playground

Public Member Functions

 RendererDartTest ()
 
flutter::testing::AutoIsolateShutdown * GetIsolate ()
 
std::shared_ptr< Texture > GetRenderedTextureFromDart (const char *dart_function_name)
 Run a Dart function that's expected to create a texture and pass it back for rendering via drawToPlayground. More...
 
bool RunDartFunction (const char *dart_function_name)
 Invokes a Dart function. More...
 
bool RunDartFunctionWithWindowSize (const char *dart_function_name)
 Invokes a Dart function with the window's width and height as arguments. More...
 
bool RenderDartToPlayground (const char *dart_function_name)
 Call a dart function that produces a texture and render the result in the playground. More...
 
- Public Member Functions inherited from impeller::PlaygroundTest
 PlaygroundTest ()
 
virtual ~PlaygroundTest ()
 
void SetUp () override
 
void TearDown () override
 
PlaygroundBackend GetBackend () const
 
std::unique_ptr< fml::Mapping > OpenAssetAsMapping (std::string asset_name) const override
 
absl::StatusOr< RuntimeStage::Map > OpenAssetAsRuntimeStage (const char *asset_name) const
 
std::string GetWindowTitle () const override
 
- Public Member Functions inherited from impeller::Playground
 Playground (PlaygroundBackend backend, const PlaygroundSwitches &switches)
 
virtual ~Playground ()
 
bool IsPlaygroundEnabled () const
 
Point GetCursorPosition () const
 
ISize GetWindowSize () const
 
IRect GetWindowBounds () const
 
Point GetContentScale () const
 
Scalar GetSecondsElapsed () const
 Get the amount of time elapsed from the start of the playground's execution. More...
 
std::shared_ptr< Context > GetContext () const
 
std::shared_ptr< Context > MakeContext () const
 
ContentContext & GetContentContext () const
 
std::shared_ptr< TypographerContext > GetTypographerContext () const
 
bool OpenPlaygroundHere (const RenderCallback &render_callback)
 
bool OpenPlaygroundHere (SinglePassCallback pass_callback)
 
std::shared_ptr< Texture > CreateTextureForFixture (const char *fixture_name, bool enable_mipmapping=false) const
 
std::shared_ptr< Texture > CreateTextureCubeForFixture (std::array< const char *, 6 > fixture_names) const
 
fml::Status SetCapabilities (const std::shared_ptr< Capabilities > &capabilities)
 
bool WillRenderSomething () const
 Returns true if OpenPlaygroundHere will actually render anything. More...
 
GLProcAddressResolver CreateGLProcAddressResolver () const
 
VKProcAddressResolver CreateVKProcAddressResolver () const
 
void SetGPUDisabled (bool disabled) const
 Mark the GPU as unavilable. More...
 
RuntimeStageBackend GetRuntimeStageBackend () const
 

Additional Inherited Members

- Public Types inherited from impeller::Playground
using SinglePassCallback = std::function< bool(RenderPass &pass)>
 
using RenderCallback = std::function< bool(RenderTarget &render_target)>
 
using GLProcAddressResolver = std::function< void *(const char *proc_name)>
 
using VKProcAddressResolver = std::function< void *(void *instance, const char *proc_name)>
 
- Static Public Member Functions inherited from impeller::Playground
static bool ShouldOpenNewPlaygrounds ()
 
static std::shared_ptr< CompressedImage > LoadFixtureImageCompressed (std::shared_ptr< fml::Mapping > mapping)
 
static std::optional< DecompressedImage > DecodeImageRGBA (const std::shared_ptr< CompressedImage > &compressed)
 
static std::shared_ptr< Texture > CreateTextureForMapping (const std::shared_ptr< Context > &context, std::shared_ptr< fml::Mapping > mapping, bool enable_mipmapping=false)
 
static bool SupportsBackend (PlaygroundBackend backend)
 
- Protected Member Functions inherited from impeller::Playground
void TearDownContextData ()
 
virtual void EnsureContextIsUnique ()
 Make sure that when the context is later created that it will not be shared with any other playgrounds. More...
 
bool PlatformSupportsWideGamutTests () const
 Returns true if the platform can support wide gamuts. More...
 
virtual bool EnsureContextSupportsWideGamut ()
 Make sure that when the context is later created that it will support wide gamuts if the platform supports it. Returns whether the platform supports wide gamut. More...
 
virtual void EnsureContextSupportsAntialiasLines ()
 Make sure that when the context is later created that it will support the experimental AA lines flag. More...
 
const PlaygroundSwitches & GetSwitches () const
 Return an unmodifiable reference to the current switches. The switches might change at the start of a test as it has a brief opportunity to call any of the Ensure* methods that define the environment it expects, but should be stable by the time any subsequent methods that might perform work are called. More...
 
void SetTypographerContext (std::shared_ptr< TypographerContext > typographer_context)
 
void SetWindowSize (ISize size)
 

Detailed Description

Definition at line 57 of file renderer_dart_unittests.cc.

Constructor & Destructor Documentation

◆ RendererDartTest()

impeller::testing::RendererDartTest::RendererDartTest ( )
inline

Definition at line 60 of file renderer_dart_unittests.cc.

61  : settings_(CreateSettingsForFixture()),
62  vm_ref_(flutter::DartVMRef::Create(settings_)) {
63  fml::MessageLoop::EnsureInitializedForCurrentThread();
64 
65  current_task_runner_ = fml::MessageLoop::GetCurrent().GetTaskRunner();
66 
67  isolate_ = CreateDartIsolate();
68  assert(isolate_);
69  assert(isolate_->get()->GetPhase() == flutter::DartIsolate::Phase::Running);
70 
71  // Set up native callbacks.
72  //
73  // Note: The Dart isolate is configured (by
74  // `RendererDartTest::CreateDartIsolate`) to use the main thread, so
75  // there's no need for additional synchronization.
76  {
77  auto set_display_texture = [this](Dart_NativeArguments args) {
78  flutter::gpu::Texture* texture =
79  tonic::DartConverter<flutter::gpu::Texture*>::FromDart(
80  Dart_GetNativeArgument(args, 0));
81  assert(texture != nullptr); // Should always be a valid pointer.
82  received_texture_ = texture->GetTexture();
83  };
84  AddNativeCallback("SetDisplayTexture",
85  CREATE_NATIVE_ENTRY(set_display_texture));
86  }
87  }
ScopedObject< Object > Create(CtorArgs &&... args)
Definition: object.h:161

Member Function Documentation

◆ GetIsolate()

flutter::testing::AutoIsolateShutdown* impeller::testing::RendererDartTest::GetIsolate ( )
inline

Definition at line 89 of file renderer_dart_unittests.cc.

89  {
90  // Sneak the context into the Flutter GPU API.
91  assert(GetContext() != nullptr);
92  flutter::gpu::Context::SetOverrideContext(GetContext());
93 
94  InstantiateTestShaderLibrary(GetContext()->GetBackendType());
95 
96  return isolate_.get();
97  }
std::shared_ptr< Context > GetContext() const
Definition: playground.cc:126
static void InstantiateTestShaderLibrary(Context::BackendType backend_type)

References impeller::Playground::GetContext(), and impeller::testing::InstantiateTestShaderLibrary().

Referenced by GetRenderedTextureFromDart(), RunDartFunction(), and RunDartFunctionWithWindowSize().

◆ GetRenderedTextureFromDart()

std::shared_ptr<Texture> impeller::testing::RendererDartTest::GetRenderedTextureFromDart ( const char *  dart_function_name)
inline

Run a Dart function that's expected to create a texture and pass it back for rendering via drawToPlayground.

Definition at line 101 of file renderer_dart_unittests.cc.

102  {
103  bool success =
104  GetIsolate()->RunInIsolateScope([this, &dart_function_name]() -> bool {
105  Dart_Handle args[] = {tonic::ToDart(GetWindowSize().width),
106  tonic::ToDart(GetWindowSize().height)};
107  if (tonic::CheckAndHandleError(
108  ::Dart_Invoke(Dart_RootLibrary(),
109  tonic::ToDart(dart_function_name), 2, args))) {
110  return false;
111  }
112  return DrainMicrotasks();
113  });
114  if (!success) {
115  FML_LOG(ERROR) << "Failed to invoke dart test function:"
116  << dart_function_name;
117  return nullptr;
118  }
119  if (!received_texture_) {
120  FML_LOG(ERROR) << "Dart test function `" << dart_function_name
121  << "` did not invoke `drawToPlaygroundSurface`.";
122  return nullptr;
123  }
124  return received_texture_;
125  }
ISize GetWindowSize() const
Definition: playground.cc:267
flutter::testing::AutoIsolateShutdown * GetIsolate()

References impeller::testing::DrainMicrotasks(), GetIsolate(), and impeller::Playground::GetWindowSize().

Referenced by RenderDartToPlayground().

◆ RenderDartToPlayground()

bool impeller::testing::RendererDartTest::RenderDartToPlayground ( const char *  dart_function_name)
inline

Call a dart function that produces a texture and render the result in the playground.

If the playground isn't enabled, the function is simply run once in order to verify that it doesn't throw any unhandled exceptions.

Prepare pipeline.

Prepare vertex data.

Prepare sampler.

Render to playground.

Definition at line 166 of file renderer_dart_unittests.cc.

166  {
167  if (!IsPlaygroundEnabled()) {
168  // If the playground is not enabled, run the function instead to at least
169  // verify that it doesn't crash.
170  return RunDartFunctionWithWindowSize(dart_function_name);
171  }
172 
173  auto context = GetContext();
174  assert(context != nullptr);
175 
176  //------------------------------------------------------------------------------
177  /// Prepare pipeline.
178  ///
179 
180  using TextureVS = TextureVertexShader;
181  using TextureFS = TextureFragmentShader;
182  using TexturePipelineBuilder = PipelineBuilder<TextureVS, TextureFS>;
183 
184  auto pipeline_desc =
185  TexturePipelineBuilder::MakeDefaultPipelineDescriptor(*context);
186  if (!pipeline_desc.has_value()) {
187  FML_LOG(ERROR) << "Failed to create default pipeline descriptor.";
188  return false;
189  }
190  pipeline_desc->SetSampleCount(SampleCount::kCount4);
191  pipeline_desc->SetStencilAttachmentDescriptors(std::nullopt);
192  pipeline_desc->SetDepthStencilAttachmentDescriptor(std::nullopt);
193  pipeline_desc->SetStencilPixelFormat(PixelFormat::kUnknown);
194  pipeline_desc->SetDepthPixelFormat(PixelFormat::kUnknown);
195 
196  auto pipeline =
197  context->GetPipelineLibrary()->GetPipeline(pipeline_desc).Get();
198  if (!pipeline || !pipeline->IsValid()) {
199  FML_LOG(ERROR) << "Failed to create default pipeline.";
200  return false;
201  }
202 
203  //------------------------------------------------------------------------------
204  /// Prepare vertex data.
205  ///
206 
207  VertexBufferBuilder<TextureVS::PerVertexData> texture_vtx_builder;
208 
209  // Always stretch out the texture to fill the screen.
210 
211  // clang-format off
212  texture_vtx_builder.AddVertices({
213  {{-0.5, -0.5, 0.0}, {0.0, 0.0}}, // 1
214  {{ 0.5, -0.5, 0.0}, {1.0, 0.0}}, // 2
215  {{ 0.5, 0.5, 0.0}, {1.0, 1.0}}, // 3
216  {{-0.5, -0.5, 0.0}, {0.0, 0.0}}, // 1
217  {{ 0.5, 0.5, 0.0}, {1.0, 1.0}}, // 3
218  {{-0.5, 0.5, 0.0}, {0.0, 1.0}}, // 4
219  });
220  // clang-format on
221 
222  //------------------------------------------------------------------------------
223  /// Prepare sampler.
224  ///
225 
226  const auto& sampler = context->GetSamplerLibrary()->GetSampler({});
227  if (!sampler) {
228  FML_LOG(ERROR) << "Failed to create default sampler.";
229  return false;
230  }
231 
232  //------------------------------------------------------------------------------
233  /// Render to playground.
234  ///
235 
236  SinglePassCallback callback = [&](RenderPass& pass) {
237  auto texture = GetRenderedTextureFromDart(dart_function_name);
238  if (!texture) {
239  return false;
240  }
241 
242  auto buffer = HostBuffer::Create(
243  context->GetResourceAllocator(), context->GetIdleWaiter(),
244  context->GetCapabilities()->GetMinimumUniformAlignment());
245 
246  pass.SetVertexBuffer(texture_vtx_builder.CreateVertexBuffer(
247  *context->GetResourceAllocator()));
248 
249  TextureVS::UniformBuffer uniforms;
250  uniforms.mvp = Matrix();
251  TextureVS::BindUniformBuffer(pass, buffer->EmplaceUniform(uniforms));
252  TextureFS::BindTextureContents(pass, texture, sampler);
253 
254  pass.SetPipeline(pipeline);
255 
256  if (!pass.Draw().ok()) {
257  return false;
258  }
259  return true;
260  };
261  return OpenPlaygroundHere(callback);
262  }
static std::shared_ptr< HostBuffer > Create(const std::shared_ptr< Allocator > &allocator, const std::shared_ptr< const IdleWaiter > &idle_waiter, size_t minimum_uniform_alignment)
Definition: host_buffer.cc:21
bool OpenPlaygroundHere(const RenderCallback &render_callback)
Definition: playground.cc:287
bool IsPlaygroundEnabled() const
Definition: playground.cc:224
std::function< bool(RenderPass &pass)> SinglePassCallback
Definition: playground.h:39
bool RunDartFunctionWithWindowSize(const char *dart_function_name)
Invokes a Dart function with the window's width and height as arguments.
std::shared_ptr< Texture > GetRenderedTextureFromDart(const char *dart_function_name)
Run a Dart function that's expected to create a texture and pass it back for rendering via drawToPlay...
std::shared_ptr< ContextGLES > context
std::shared_ptr< PipelineGLES > pipeline

References impeller::VertexBufferBuilder< VertexType_, IndexType_ >::AddVertices(), context, impeller::HostBuffer::Create(), impeller::VertexBufferBuilder< VertexType_, IndexType_ >::CreateVertexBuffer(), impeller::Playground::GetContext(), GetRenderedTextureFromDart(), impeller::Playground::IsPlaygroundEnabled(), impeller::kCount4, impeller::kUnknown, impeller::Playground::OpenPlaygroundHere(), pipeline, and RunDartFunctionWithWindowSize().

◆ RunDartFunction()

bool impeller::testing::RendererDartTest::RunDartFunction ( const char *  dart_function_name)
inline

Invokes a Dart function.

    Returns false if invoking the function failed or if any unhandled
    exceptions were thrown. 

Definition at line 131 of file renderer_dart_unittests.cc.

131  {
132  return GetIsolate()->RunInIsolateScope([&dart_function_name]() -> bool {
133  if (tonic::CheckAndHandleError(
134  ::Dart_Invoke(Dart_RootLibrary(),
135  tonic::ToDart(dart_function_name), 0, nullptr))) {
136  return false;
137  }
138  return DrainMicrotasks();
139  });
140  }

References impeller::testing::DrainMicrotasks(), and GetIsolate().

◆ RunDartFunctionWithWindowSize()

bool impeller::testing::RendererDartTest::RunDartFunctionWithWindowSize ( const char *  dart_function_name)
inline

Invokes a Dart function with the window's width and height as arguments.

Returns false if invoking the function failed or if any unhandled exceptions were thrown.

Definition at line 147 of file renderer_dart_unittests.cc.

147  {
148  return GetIsolate()->RunInIsolateScope(
149  [this, &dart_function_name]() -> bool {
150  Dart_Handle args[] = {tonic::ToDart(GetWindowSize().width),
151  tonic::ToDart(GetWindowSize().height)};
152  if (tonic::CheckAndHandleError(
153  ::Dart_Invoke(Dart_RootLibrary(),
154  tonic::ToDart(dart_function_name), 2, args))) {
155  return false;
156  }
157  return DrainMicrotasks();
158  });
159  }

References impeller::testing::DrainMicrotasks(), GetIsolate(), and impeller::Playground::GetWindowSize().

Referenced by RenderDartToPlayground().


The documentation for this class was generated from the following file: