Flutter Impeller
renderer_dart_unittests.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 #define FML_USED_ON_EMBEDDER
6 
7 #include <memory>
8 
9 #include "flutter/common/settings.h"
10 #include "flutter/common/task_runners.h"
11 #include "flutter/lib/gpu/context.h"
12 #include "flutter/lib/gpu/shader_library.h"
13 #include "flutter/runtime/dart_isolate.h"
14 #include "flutter/runtime/dart_vm_lifecycle.h"
15 #include "flutter/testing/dart_fixture.h"
16 #include "flutter/testing/dart_isolate_runner.h"
17 #include "flutter/testing/testing.h"
18 #include "fml/memory/ref_ptr.h"
21 
22 #include "gtest/gtest.h"
23 #include "third_party/imgui/imgui.h"
24 
25 namespace impeller {
26 namespace testing {
27 
29  auto fixture =
30  flutter::testing::OpenFixtureAsMapping("playground.shaderbundle");
31  auto library = flutter::gpu::ShaderLibrary::MakeFromFlatbuffer(
32  backend_type, std::move(fixture));
33  flutter::gpu::ShaderLibrary::SetOverride(library);
34 }
35 
37  public flutter::testing::DartFixture {
38  public:
40  : settings_(CreateSettingsForFixture()),
41  vm_ref_(flutter::DartVMRef::Create(settings_)) {
42  fml::MessageLoop::EnsureInitializedForCurrentThread();
43 
44  current_task_runner_ = fml::MessageLoop::GetCurrent().GetTaskRunner();
45 
46  isolate_ = CreateDartIsolate();
47  assert(isolate_);
48  assert(isolate_->get()->GetPhase() == flutter::DartIsolate::Phase::Running);
49  }
50 
51  flutter::testing::AutoIsolateShutdown* GetIsolate() {
52  // Sneak the context into the Flutter GPU API.
53  assert(GetContext() != nullptr);
54  flutter::gpu::Context::SetOverrideContext(GetContext());
55 
56  InstantiateTestShaderLibrary(GetContext()->GetBackendType());
57 
58  return isolate_.get();
59  }
60 
61  private:
62  std::unique_ptr<flutter::testing::AutoIsolateShutdown> CreateDartIsolate() {
63  const auto settings = CreateSettingsForFixture();
64  flutter::TaskRunners task_runners(flutter::testing::GetCurrentTestName(),
65  current_task_runner_, //
66  current_task_runner_, //
67  current_task_runner_, //
68  current_task_runner_ //
69  );
70  return flutter::testing::RunDartCodeInIsolate(
71  vm_ref_, settings, task_runners, "main", {},
72  flutter::testing::GetDefaultKernelFilePath());
73  }
74 
75  const flutter::Settings settings_;
76  flutter::DartVMRef vm_ref_;
77  fml::RefPtr<fml::TaskRunner> current_task_runner_;
78  std::unique_ptr<flutter::testing::AutoIsolateShutdown> isolate_;
79 };
80 
81 INSTANTIATE_PLAYGROUND_SUITE(RendererDartTest);
82 
83 TEST_P(RendererDartTest, CanRunDartInPlaygroundFrame) {
84  auto isolate = GetIsolate();
85 
86  SinglePassCallback callback = [&](RenderPass& pass) {
87  ImGui::Begin("Dart test", nullptr);
88  ImGui::Text(
89  "This test executes Dart code during the playground frame callback.");
90  ImGui::End();
91 
92  return isolate->RunInIsolateScope([]() -> bool {
93  if (tonic::CheckAndHandleError(::Dart_Invoke(
94  Dart_RootLibrary(), tonic::ToDart("sayHi"), 0, nullptr))) {
95  return false;
96  }
97  return true;
98  });
99  };
100  OpenPlaygroundHere(callback);
101 }
102 
103 TEST_P(RendererDartTest, CanInstantiateFlutterGPUContext) {
104  auto isolate = GetIsolate();
105  bool result = isolate->RunInIsolateScope([]() -> bool {
106  if (tonic::CheckAndHandleError(::Dart_Invoke(
107  Dart_RootLibrary(), tonic::ToDart("instantiateDefaultContext"), 0,
108  nullptr))) {
109  return false;
110  }
111  return true;
112  });
113 
114  ASSERT_TRUE(result);
115 }
116 
117 #define DART_TEST_CASE(name) \
118  TEST_P(RendererDartTest, name) { \
119  auto isolate = GetIsolate(); \
120  bool result = isolate->RunInIsolateScope([]() -> bool { \
121  if (tonic::CheckAndHandleError(::Dart_Invoke( \
122  Dart_RootLibrary(), tonic::ToDart(#name), 0, nullptr))) { \
123  return false; \
124  } \
125  return true; \
126  }); \
127  ASSERT_TRUE(result); \
128  }
129 
130 /// These test entries correspond to Dart functions located in
131 /// `flutter/impeller/fixtures/dart_tests.dart`
132 
133 DART_TEST_CASE(canCreateShaderLibrary);
134 DART_TEST_CASE(canReflectUniformStructs);
135 DART_TEST_CASE(uniformBindFailsForInvalidHostBufferOffset);
136 
137 DART_TEST_CASE(canCreateRenderPassAndSubmit);
138 
139 } // namespace testing
140 } // namespace impeller
impeller::testing::DART_TEST_CASE
DART_TEST_CASE(canCreateShaderLibrary)
impeller::Context::BackendType
BackendType
Definition: context.h:48
impeller::testing::RendererDartTest
Definition: renderer_dart_unittests.cc:36
impeller::testing::INSTANTIATE_PLAYGROUND_SUITE
INSTANTIATE_PLAYGROUND_SUITE(AiksTest)
render_pass.h
impeller::testing::InstantiateTestShaderLibrary
static void InstantiateTestShaderLibrary(Context::BackendType backend_type)
Definition: renderer_dart_unittests.cc:28
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
impeller::testing::RendererDartTest::RendererDartTest
RendererDartTest()
Definition: renderer_dart_unittests.cc:39
impeller::testing::TEST_P
TEST_P(AiksTest, CanRenderMaskBlurHugeSigma)
Definition: aiks_blur_unittests.cc:23
impeller::testing::RendererDartTest::GetIsolate
flutter::testing::AutoIsolateShutdown * GetIsolate()
Definition: renderer_dart_unittests.cc:51
impeller::PlaygroundTest
Definition: playground_test.h:23
impeller::Playground::GetContext
std::shared_ptr< Context > GetContext() const
Definition: playground.cc:89
impeller
Definition: aiks_blur_unittests.cc:20
playground_test.h