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