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/fml/backtrace.h"
12 #include "flutter/fml/command_line.h"
13 #include "flutter/lib/gpu/context.h"
14 #include "flutter/lib/ui/ui_dart_state.h"
15 #include "flutter/runtime/dart_isolate.h"
16 #include "flutter/runtime/dart_vm_lifecycle.h"
17 #include "flutter/runtime/isolate_configuration.h"
18 #include "flutter/testing/dart_fixture.h"
19 #include "flutter/testing/dart_isolate_runner.h"
20 #include "flutter/testing/fixture_test.h"
21 #include "flutter/testing/testing.h"
22 #include "impeller/fixtures/box_fade.frag.h"
23 #include "impeller/fixtures/box_fade.vert.h"
28 
29 #include "gtest/gtest.h"
30 #include "third_party/imgui/imgui.h"
31 
32 namespace impeller {
33 namespace testing {
34 
36  public flutter::testing::DartFixture {
37  public:
39  : settings_(CreateSettingsForFixture()),
40  vm_ref_(flutter::DartVMRef::Create(settings_)) {
41  fml::MessageLoop::EnsureInitializedForCurrentThread();
42 
43  current_task_runner_ = fml::MessageLoop::GetCurrent().GetTaskRunner();
44 
45  isolate_ = CreateDartIsolate();
46  assert(isolate_);
47  assert(isolate_->get()->GetPhase() == flutter::DartIsolate::Phase::Running);
48  }
49 
50  flutter::testing::AutoIsolateShutdown* GetIsolate() {
51  // Sneak the context into the Flutter GPU API.
52  assert(GetContext() != nullptr);
53  flutter::Context::SetOverrideContext(GetContext());
54 
55  return isolate_.get();
56  }
57 
58  private:
59  std::unique_ptr<flutter::testing::AutoIsolateShutdown> CreateDartIsolate() {
60  const auto settings = CreateSettingsForFixture();
61  flutter::TaskRunners task_runners(flutter::testing::GetCurrentTestName(),
62  current_task_runner_, //
63  current_task_runner_, //
64  current_task_runner_, //
65  current_task_runner_ //
66  );
67  return flutter::testing::RunDartCodeInIsolate(
68  vm_ref_, settings, task_runners, "main", {},
69  flutter::testing::GetDefaultKernelFilePath());
70  }
71 
72  const flutter::Settings settings_;
73  flutter::DartVMRef vm_ref_;
74  fml::RefPtr<fml::TaskRunner> current_task_runner_;
75  std::unique_ptr<flutter::testing::AutoIsolateShutdown> isolate_;
76 };
77 
78 INSTANTIATE_PLAYGROUND_SUITE(RendererDartTest);
79 
80 TEST_P(RendererDartTest, CanRunDartInPlaygroundFrame) {
81  auto isolate = GetIsolate();
82 
83  SinglePassCallback callback = [&](RenderPass& pass) {
84  ImGui::Begin("Dart test", nullptr);
85  ImGui::Text(
86  "This test executes Dart code during the playground frame callback.");
87  ImGui::End();
88 
89  return isolate->RunInIsolateScope([]() -> bool {
90  if (tonic::CheckAndHandleError(::Dart_Invoke(
91  Dart_RootLibrary(), tonic::ToDart("sayHi"), 0, nullptr))) {
92  return false;
93  }
94  return true;
95  });
96  };
97  OpenPlaygroundHere(callback);
98 }
99 
100 TEST_P(RendererDartTest, CanInstantiateFlutterGPUContext) {
101  auto isolate = GetIsolate();
102  bool result = isolate->RunInIsolateScope([]() -> bool {
103  if (tonic::CheckAndHandleError(::Dart_Invoke(
104  Dart_RootLibrary(), tonic::ToDart("instantiateDefaultContext"), 0,
105  nullptr))) {
106  return false;
107  }
108  return true;
109  });
110 
111  ASSERT_TRUE(result);
112 }
113 
114 TEST_P(RendererDartTest, CanEmplaceHostBuffer) {
115  auto isolate = GetIsolate();
116  bool result = isolate->RunInIsolateScope([]() -> bool {
117  if (tonic::CheckAndHandleError(
118  ::Dart_Invoke(Dart_RootLibrary(),
119  tonic::ToDart("canEmplaceHostBuffer"), 0, nullptr))) {
120  return false;
121  }
122  return true;
123  });
124 
125  ASSERT_TRUE(result);
126 }
127 
128 } // namespace testing
129 } // namespace impeller
sampler_library.h
impeller::testing::RendererDartTest
Definition: renderer_dart_unittests.cc:35
impeller::testing::INSTANTIATE_PLAYGROUND_SUITE
INSTANTIATE_PLAYGROUND_SUITE(AiksTest)
impeller::testing::TEST_P
TEST_P(AiksTest, RotateColorFilteredPath)
Definition: aiks_unittests.cc:56
render_pass.h
pipeline_library.h
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:27
impeller::testing::RendererDartTest::RendererDartTest
RendererDartTest()
Definition: renderer_dart_unittests.cc:38
impeller::testing::RendererDartTest::GetIsolate
flutter::testing::AutoIsolateShutdown * GetIsolate()
Definition: renderer_dart_unittests.cc:50
impeller::PlaygroundTest
Definition: playground_test.h:22
impeller::Playground::GetContext
std::shared_ptr< Context > GetContext() const
Definition: playground.cc:86
impeller
Definition: aiks_context.cc:10
playground_test.h