Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 952a89b

Browse files
authored
[Impeller] Wire up a playground that can run Dart (#39805)
1 parent 3479463 commit 952a89b

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

ci/licenses_golden/excluded_files

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@
142142
../../../flutter/impeller/renderer/device_buffer_unittests.cc
143143
../../../flutter/impeller/renderer/host_buffer_unittests.cc
144144
../../../flutter/impeller/renderer/pipeline_descriptor_unittests.cc
145+
../../../flutter/impeller/renderer/renderer_dart_unittests.cc
145146
../../../flutter/impeller/renderer/renderer_unittests.cc
146147
../../../flutter/impeller/runtime_stage/runtime_stage_unittests.cc
147148
../../../flutter/impeller/scene/README.md

impeller/BUILD.gn

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,13 @@ impeller_component("impeller_unittests") {
9292
]
9393
}
9494
}
95+
96+
if (impeller_supports_rendering) {
97+
impeller_component("impeller_dart_unittests") {
98+
target_type = "executable"
99+
100+
testonly = true
101+
102+
deps = [ "renderer:renderer_dart_unittests" ]
103+
}
104+
}

impeller/fixtures/dart_tests.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
import 'dart:ui';
6+
7+
void main() {}
8+
9+
@pragma('vm:entry-point')
10+
void sayHi() {
11+
print('Hi');
12+
}

impeller/renderer/BUILD.gn

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,28 @@ impeller_component("renderer_unittests") {
123123
"//flutter/testing:testing_lib",
124124
]
125125
}
126+
127+
test_fixtures("renderer_dart_fixtures") {
128+
dart_main = "../fixtures/dart_tests.dart"
129+
130+
fixtures = [
131+
"../fixtures/bay_bridge.jpg",
132+
"../fixtures/boston.jpg",
133+
]
134+
}
135+
136+
impeller_component("renderer_dart_unittests") {
137+
testonly = true
138+
139+
sources = [ "renderer_dart_unittests.cc" ]
140+
141+
deps = [
142+
":renderer",
143+
":renderer_dart_fixtures",
144+
"../fixtures:shader_fixtures",
145+
"../playground:playground_test",
146+
"//flutter/runtime:runtime",
147+
"//flutter/testing:fixture_test",
148+
"//flutter/testing:testing_lib",
149+
]
150+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
#include <memory>
6+
7+
#include "flutter/common/settings.h"
8+
#include "flutter/common/task_runners.h"
9+
#include "flutter/lib/ui/ui_dart_state.h"
10+
#include "flutter/runtime/dart_isolate.h"
11+
#include "flutter/runtime/dart_vm_lifecycle.h"
12+
#include "flutter/runtime/isolate_configuration.h"
13+
#include "flutter/testing/dart_fixture.h"
14+
#include "flutter/testing/dart_isolate_runner.h"
15+
#include "flutter/testing/fixture_test.h"
16+
#include "flutter/testing/testing.h"
17+
#include "impeller/fixtures/box_fade.frag.h"
18+
#include "impeller/fixtures/box_fade.vert.h"
19+
#include "impeller/playground/playground_test.h"
20+
#include "impeller/renderer/pipeline_library.h"
21+
#include "impeller/renderer/render_pass.h"
22+
#include "impeller/renderer/sampler_library.h"
23+
24+
#include "third_party/imgui/imgui.h"
25+
26+
namespace impeller {
27+
namespace testing {
28+
29+
class RendererDartTest : public PlaygroundTest,
30+
public flutter::testing::DartFixture {
31+
public:
32+
RendererDartTest()
33+
: settings_(CreateSettingsForFixture()),
34+
vm_ref_(flutter::DartVMRef::Create(settings_)) {
35+
fml::MessageLoop::EnsureInitializedForCurrentThread();
36+
current_task_runner_ = fml::MessageLoop::GetCurrent().GetTaskRunner();
37+
isolate_ = CreateDartIsolate();
38+
assert(isolate_);
39+
assert(isolate_->get()->GetPhase() == flutter::DartIsolate::Phase::Running);
40+
}
41+
42+
flutter::testing::AutoIsolateShutdown* GetIsolate() { return isolate_.get(); }
43+
44+
private:
45+
std::unique_ptr<flutter::testing::AutoIsolateShutdown> CreateDartIsolate() {
46+
const auto settings = CreateSettingsForFixture();
47+
flutter::TaskRunners task_runners(flutter::testing::GetCurrentTestName(),
48+
current_task_runner_, //
49+
current_task_runner_, //
50+
current_task_runner_, //
51+
current_task_runner_ //
52+
);
53+
return flutter::testing::RunDartCodeInIsolate(
54+
vm_ref_, settings, task_runners, "main", {},
55+
flutter::testing::GetDefaultKernelFilePath());
56+
}
57+
58+
const flutter::Settings settings_;
59+
flutter::DartVMRef vm_ref_;
60+
fml::RefPtr<fml::TaskRunner> current_task_runner_;
61+
std::unique_ptr<flutter::testing::AutoIsolateShutdown> isolate_;
62+
};
63+
64+
INSTANTIATE_PLAYGROUND_SUITE(RendererDartTest);
65+
66+
TEST_P(RendererDartTest, CanRunDartInPlaygroundFrame) {
67+
auto isolate = GetIsolate();
68+
69+
SinglePassCallback callback = [&](RenderPass& pass) {
70+
ImGui::Begin("Dart test", nullptr);
71+
ImGui::Text(
72+
"This test executes Dart code during the playground frame callback.");
73+
ImGui::End();
74+
75+
return isolate->RunInIsolateScope([]() -> bool {
76+
if (tonic::CheckAndHandleError(::Dart_Invoke(
77+
Dart_RootLibrary(), tonic::ToDart("sayHi"), 0, nullptr))) {
78+
return false;
79+
}
80+
return true;
81+
});
82+
};
83+
OpenPlaygroundHere(callback);
84+
}
85+
86+
} // namespace testing
87+
} // namespace impeller

0 commit comments

Comments
 (0)