Skip to content

Commit 1240ac2

Browse files
committed
Allocate image memory for test surface; parameterize a GL raster test
1 parent 349676d commit 1240ac2

12 files changed

+184
-20
lines changed

shell/platform/embedder/tests/embedder_config_builder.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "flutter/runtime/dart_vm.h"
88
#include "flutter/shell/platform/embedder/embedder.h"
9+
#include "tests/embedder_test_context.h"
910
#include "third_party/skia/include/core/SkBitmap.h"
1011

1112
#ifdef SHELL_ENABLE_GL
@@ -163,6 +164,24 @@ void EmbedderConfigBuilder::SetOpenGLPresentCallBack() {
163164
#endif
164165
}
165166

167+
void EmbedderConfigBuilder::SetRenderConfig(EmbedderTestContextType type,
168+
SkISize surface_size) {
169+
switch (type) {
170+
case EmbedderTestContextType::kOpenGLContext:
171+
SetOpenGLRendererConfig(surface_size);
172+
break;
173+
case EmbedderTestContextType::kMetalContext:
174+
SetMetalRendererConfig(surface_size);
175+
break;
176+
case EmbedderTestContextType::kVulkanContext:
177+
SetVulkanRendererConfig(surface_size);
178+
break;
179+
case EmbedderTestContextType::kSoftwareContext:
180+
SetSoftwareRendererConfig(surface_size);
181+
break;
182+
}
183+
}
184+
166185
void EmbedderConfigBuilder::SetOpenGLRendererConfig(SkISize surface_size) {
167186
#ifdef SHELL_ENABLE_GL
168187
renderer_config_.type = FlutterRendererType::kOpenGL;

shell/platform/embedder/tests/embedder_config_builder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class EmbedderConfigBuilder {
4545

4646
FlutterProjectArgs& GetProjectArgs();
4747

48+
void SetRenderConfig(EmbedderTestContextType type, SkISize surface_size);
49+
4850
void SetSoftwareRendererConfig(SkISize surface_size = SkISize::Make(1, 1));
4951

5052
void SetOpenGLRendererConfig(SkISize surface_size);

shell/platform/embedder/tests/embedder_test.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "flutter/shell/platform/embedder/tests/embedder_test_context.h"
1313
#include "flutter/testing/testing.h"
1414
#include "flutter/testing/thread_test.h"
15+
#include "gtest/gtest.h"
1516

1617
namespace flutter {
1718
namespace testing {
@@ -31,6 +32,10 @@ class EmbedderTest : public ThreadTest {
3132
FML_DISALLOW_COPY_AND_ASSIGN(EmbedderTest);
3233
};
3334

35+
class EmbedderTestMultiBackend
36+
: public EmbedderTest,
37+
public ::testing::WithParamInterface<EmbedderTestContextType> {};
38+
3439
} // namespace testing
3540
} // namespace flutter
3641

shell/platform/embedder/tests/embedder_test_context_vulkan.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ size_t EmbedderTestContextVulkan::GetSurfacePresentCount() const {
3737
}
3838

3939
VkImage EmbedderTestContextVulkan::GetNextImage(const SkISize& size) {
40-
assert(false); // TODO(bdero)
41-
return nullptr;
40+
return surface_->GetImage();
4241
}
4342

4443
bool EmbedderTestContextVulkan::PresentImage(VkImage image) {
45-
assert(false); // TODO(bdero)
46-
return false;
44+
FireRootSurfacePresentCallbackIfPresent(
45+
[&]() { return surface_->GetSurfaceSnapshot(); });
46+
present_count_++;
47+
return true;
4748
}
4849

4950
EmbedderTestContextType EmbedderTestContextVulkan::GetContextType() const {

shell/platform/embedder/tests/embedder_unittests_gl.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
#include "tests/embedder_test_context.h"
56
#define FML_USED_ON_EMBEDDER
67

78
#include <string>
@@ -1235,13 +1236,13 @@ TEST_F(EmbedderTest, CanRenderSceneWithoutCustomCompositorWithTransformation) {
12351236
"scene_without_custom_compositor_with_xform.png", rendered_scene));
12361237
}
12371238

1238-
TEST_F(EmbedderTest, CanRenderGradientWithoutCompositor) {
1239-
auto& context = GetEmbedderContext(EmbedderTestContextType::kOpenGLContext);
1239+
TEST_P(EmbedderTestMultiBackend, CanRenderGradientWithoutCompositor) {
1240+
auto& context = GetEmbedderContext(GetParam());
12401241

12411242
EmbedderConfigBuilder builder(context);
12421243

12431244
builder.SetDartEntrypoint("render_gradient");
1244-
builder.SetOpenGLRendererConfig(SkISize::Make(800, 600));
1245+
builder.SetRenderConfig(GetParam(), SkISize::Make(800, 600));
12451246

12461247
auto rendered_scene = context.GetNextSceneImage();
12471248

@@ -3688,5 +3689,11 @@ TEST_F(EmbedderTest, ExternalTextureGLRefreshedTooOften) {
36883689
EXPECT_TRUE(resolve_called);
36893690
}
36903691

3692+
INSTANTIATE_TEST_SUITE_P(
3693+
EmbedderTestGlVk,
3694+
EmbedderTestMultiBackend,
3695+
::testing::Values(EmbedderTestContextType::kOpenGLContext,
3696+
EmbedderTestContextType::kVulkanContext));
3697+
36913698
} // namespace testing
36923699
} // namespace flutter

testing/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ if (enable_unittests) {
128128
sources = [
129129
"test_vulkan_context.cc",
130130
"test_vulkan_context.h",
131+
"test_vulkan_image.cc",
132+
"test_vulkan_image.h",
131133
"test_vulkan_surface.cc",
132134
"test_vulkan_surface.h",
133135
]

testing/test_vulkan_context.cc

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
// found in the LICENSE file.
44

55
#include <cassert>
6+
#include <optional>
67

78
#include "flutter/fml/logging.h"
89
#include "flutter/shell/common/context_options.h"
910
#include "flutter/testing/test_vulkan_context.h"
1011

11-
#include "include/gpu/GrDirectContext.h"
12-
#include "include/gpu/vk/GrVkExtensions.h"
1312
#include "third_party/skia/include/core/SkSurface.h"
13+
#include "third_party/skia/include/gpu/GrDirectContext.h"
14+
#include "third_party/skia/include/gpu/vk/GrVkExtensions.h"
15+
#include "vulkan/vulkan_core.h"
1416

1517
#ifdef OS_MACOSX
1618
#define VULKAN_SO_PATH "libvk_swiftshader.dylib"
@@ -90,9 +92,67 @@ TestVulkanContext::TestVulkanContext() {
9092
context_ = GrDirectContext::MakeVulkan(backend_context, options);
9193
}
9294

93-
VkImage TestVulkanContext::CreateImage(const SkISize& size) const {
94-
assert(false); // TODO(bdero)
95-
return nullptr;
95+
std::optional<TestVulkanImage> TestVulkanContext::CreateImage(
96+
const SkISize& size) const {
97+
TestVulkanImage result;
98+
99+
VkImageCreateInfo info = {
100+
.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
101+
.pNext = nullptr,
102+
.flags = 0,
103+
.imageType = VK_IMAGE_TYPE_2D,
104+
.format = VK_FORMAT_R8G8B8A8_UNORM,
105+
.extent = VkExtent3D{static_cast<uint32_t>(size.width()),
106+
static_cast<uint32_t>(size.height()), 1},
107+
.mipLevels = 1,
108+
.arrayLayers = 1,
109+
.samples = VK_SAMPLE_COUNT_1_BIT,
110+
.tiling = VK_IMAGE_TILING_OPTIMAL,
111+
.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
112+
VK_IMAGE_USAGE_TRANSFER_DST_BIT |
113+
VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
114+
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
115+
.queueFamilyIndexCount = 0,
116+
.pQueueFamilyIndices = nullptr,
117+
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
118+
};
119+
120+
VkImage image;
121+
if (VK_CALL_LOG_ERROR(VK_CALL_LOG_ERROR(
122+
vk_->CreateImage(device_->GetHandle(), &info, nullptr, &image)))) {
123+
return std::nullopt;
124+
}
125+
126+
result.image_ = vulkan::VulkanHandle<VkImage>(
127+
image, [&vk = vk_, &device = device_](VkImage image) {
128+
vk->DestroyImage(device->GetHandle(), image, nullptr);
129+
});
130+
131+
VkMemoryRequirements mem_req;
132+
vk_->GetImageMemoryRequirements(device_->GetHandle(), image, &mem_req);
133+
VkMemoryAllocateInfo alloc_info{};
134+
alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
135+
alloc_info.allocationSize = mem_req.size;
136+
alloc_info.memoryTypeIndex = static_cast<uint32_t>(__builtin_ctz(
137+
mem_req.memoryTypeBits & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT));
138+
139+
VkDeviceMemory memory;
140+
if (VK_CALL_LOG_ERROR(vk_->AllocateMemory(device_->GetHandle(), &alloc_info,
141+
nullptr, &memory)) != VK_SUCCESS) {
142+
return std::nullopt;
143+
}
144+
145+
result.memory_ = vulkan::VulkanHandle<VkDeviceMemory>{
146+
memory, [&vk = vk_, &device = device_](VkDeviceMemory memory) {
147+
vk->FreeMemory(device->GetHandle(), memory, nullptr);
148+
}};
149+
150+
if (VK_CALL_LOG_ERROR(VK_CALL_LOG_ERROR(vk_->BindImageMemory(
151+
device_->GetHandle(), result.image_, result.memory_, 0)))) {
152+
return std::nullopt;
153+
}
154+
155+
return result;
96156
}
97157

98158
sk_sp<GrDirectContext> TestVulkanContext::GetGrDirectContext() const {

testing/test_vulkan_context.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include "flutter/fml/macros.h"
99
#include "flutter/fml/memory/ref_ptr.h"
10+
#include "flutter/testing/test_vulkan_image.h"
1011
#include "flutter/vulkan/vulkan_application.h"
1112
#include "flutter/vulkan/vulkan_device.h"
1213
#include "flutter/vulkan/vulkan_proc_table.h"
@@ -21,7 +22,7 @@ class TestVulkanContext {
2122
public:
2223
TestVulkanContext();
2324

24-
VkImage CreateImage(const SkISize& size) const;
25+
std::optional<TestVulkanImage> CreateImage(const SkISize& size) const;
2526

2627
sk_sp<GrDirectContext> GetGrDirectContext() const;
2728

testing/test_vulkan_image.cc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 "flutter/testing/test_vulkan_image.h"
6+
7+
namespace flutter {
8+
namespace testing {
9+
10+
TestVulkanImage::TestVulkanImage() = default;
11+
12+
TestVulkanImage::TestVulkanImage(TestVulkanImage&& other) = default;
13+
TestVulkanImage& TestVulkanImage::operator=(TestVulkanImage&& other) = default;
14+
15+
TestVulkanImage::~TestVulkanImage() = default;
16+
17+
VkImage TestVulkanImage::GetImage() {
18+
return image_;
19+
}
20+
21+
} // namespace testing
22+
} // namespace flutter

testing/test_vulkan_image.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 "flutter/fml/macros.h"
6+
#include "flutter/vulkan/vulkan_handle.h"
7+
8+
#include "third_party/skia/include/core/SkSize.h"
9+
10+
namespace flutter {
11+
namespace testing {
12+
13+
class TestVulkanContext;
14+
15+
/// Captures the lifetime of a test VkImage along with its bound memory.
16+
class TestVulkanImage {
17+
public:
18+
TestVulkanImage(TestVulkanImage&& other);
19+
TestVulkanImage& operator=(TestVulkanImage&& other);
20+
21+
~TestVulkanImage();
22+
23+
VkImage GetImage();
24+
25+
private:
26+
TestVulkanImage();
27+
28+
vulkan::VulkanHandle<VkImage> image_;
29+
vulkan::VulkanHandle<VkDeviceMemory> memory_;
30+
31+
FML_DISALLOW_COPY_AND_ASSIGN(TestVulkanImage);
32+
33+
friend TestVulkanContext;
34+
};
35+
36+
} // namespace testing
37+
} // namespace flutter

0 commit comments

Comments
 (0)