flutter_flutter/engine/src/flutter/testing/test_vulkan_context.cc
Michael Ludwig ee0eb22cdb
Update test_vulkan_context.cc to use Vulkan 1.1 (#170351)
Skia is moving to require 1.1 as its minimum supported version instead
of 1.0.

This is part of an effort to clean up and consolidate Vulkan
initialization logic, which gets much easier with some of the extensions
that were promoted to 1.1. This is for skbug.com/422204178 and required
to roll https://skia-review.googlesource.com/c/skia/+/1002391 into
Flutter.

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [ ] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
2025-06-10 16:25:12 +00:00

188 lines
6.7 KiB
C++

// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <cassert>
#include <memory>
#include <optional>
#include "flutter/flutter_vma/flutter_skia_vma.h"
#include "flutter/fml/logging.h"
#include "flutter/shell/common/context_options.h"
#include "flutter/testing/test_vulkan_context.h"
#include "flutter/vulkan/vulkan_skia_proc_table.h"
#include "flutter/fml/memory/ref_ptr.h"
#include "flutter/fml/native_library.h"
#include "flutter/vulkan/swiftshader_path.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"
#include "third_party/skia/include/gpu/ganesh/vk/GrVkDirectContext.h"
#include "third_party/skia/include/gpu/vk/VulkanBackendContext.h"
#include "third_party/skia/include/gpu/vk/VulkanExtensions.h"
#include "vulkan/vulkan_core.h"
namespace flutter::testing {
TestVulkanContext::TestVulkanContext() {
// ---------------------------------------------------------------------------
// Initialize basic Vulkan state using the Swiftshader ICD.
// ---------------------------------------------------------------------------
const char* vulkan_icd = VULKAN_SO_PATH;
// TODO(96949): Clean this up and pass a native library directly to
// VulkanProcTable.
if (!fml::NativeLibrary::Create(VULKAN_SO_PATH)) {
FML_LOG(ERROR) << "Couldn't find Vulkan ICD \"" << vulkan_icd
<< "\", trying \"libvulkan.so\" instead.";
vulkan_icd = "libvulkan.so";
}
FML_LOG(INFO) << "Using Vulkan ICD: " << vulkan_icd;
vk_ = fml::MakeRefCounted<vulkan::VulkanProcTable>(vulkan_icd);
if (!vk_ || !vk_->HasAcquiredMandatoryProcAddresses()) {
FML_LOG(ERROR) << "Proc table has not acquired mandatory proc addresses.";
return;
}
application_ = std::make_unique<vulkan::VulkanApplication>(
*vk_, "Flutter Unittests", std::vector<std::string>{},
VK_MAKE_VERSION(1, 0, 0), VK_MAKE_VERSION(1, 1, 0), true);
if (!application_->IsValid()) {
FML_LOG(ERROR) << "Failed to initialize basic Vulkan state.";
return;
}
if (!vk_->AreInstanceProcsSetup()) {
FML_LOG(ERROR) << "Failed to acquire full proc table.";
return;
}
device_ = application_->AcquireFirstCompatibleLogicalDevice();
if (!device_ || !device_->IsValid()) {
FML_LOG(ERROR) << "Failed to create compatible logical device.";
return;
}
// ---------------------------------------------------------------------------
// Create a Skia context.
// For creating SkSurfaces from VkImages and snapshotting them, etc.
// ---------------------------------------------------------------------------
VkPhysicalDeviceFeatures features;
if (!device_->GetPhysicalDeviceFeatures(&features)) {
FML_LOG(ERROR) << "Failed to get physical device features.";
return;
}
auto get_proc = vulkan::CreateSkiaGetProc(vk_);
if (get_proc == nullptr) {
FML_LOG(ERROR) << "Failed to create Vulkan getProc for Skia.";
return;
}
sk_sp<skgpu::VulkanMemoryAllocator> allocator =
flutter::FlutterSkiaVulkanMemoryAllocator::Make(
VK_MAKE_VERSION(1, 1, 0), application_->GetInstance(),
device_->GetPhysicalDeviceHandle(), device_->GetHandle(), vk_, true);
skgpu::VulkanExtensions extensions;
skgpu::VulkanBackendContext backend_context = {};
backend_context.fInstance = application_->GetInstance();
backend_context.fPhysicalDevice = device_->GetPhysicalDeviceHandle();
backend_context.fDevice = device_->GetHandle();
backend_context.fQueue = device_->GetQueueHandle();
backend_context.fGraphicsQueueIndex = device_->GetGraphicsQueueIndex();
backend_context.fMaxAPIVersion = VK_MAKE_VERSION(1, 1, 0);
backend_context.fDeviceFeatures = &features;
backend_context.fVkExtensions = &extensions;
backend_context.fGetProc = get_proc;
backend_context.fMemoryAllocator = allocator;
GrContextOptions options =
MakeDefaultContextOptions(ContextType::kRender, GrBackendApi::kVulkan);
options.fReduceOpsTaskSplitting = GrContextOptions::Enable::kNo;
context_ = GrDirectContexts::MakeVulkan(backend_context, options);
}
TestVulkanContext::~TestVulkanContext() {
if (context_) {
context_->releaseResourcesAndAbandonContext();
}
}
std::optional<TestVulkanImage> TestVulkanContext::CreateImage(
const SkISize& size) const {
TestVulkanImage result;
VkImageCreateInfo info = {
.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.imageType = VK_IMAGE_TYPE_2D,
.format = VK_FORMAT_R8G8B8A8_UNORM,
.extent = VkExtent3D{static_cast<uint32_t>(size.width()),
static_cast<uint32_t>(size.height()), 1},
.mipLevels = 1,
.arrayLayers = 1,
.samples = VK_SAMPLE_COUNT_1_BIT,
.tiling = VK_IMAGE_TILING_OPTIMAL,
.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
VK_IMAGE_USAGE_TRANSFER_DST_BIT |
VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT,
.sharingMode = VK_SHARING_MODE_EXCLUSIVE,
.queueFamilyIndexCount = 0,
.pQueueFamilyIndices = nullptr,
.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED,
};
VkImage image;
if (VK_CALL_LOG_ERROR(VK_CALL_LOG_ERROR(
vk_->CreateImage(device_->GetHandle(), &info, nullptr, &image)))) {
return std::nullopt;
}
result.image_ = vulkan::VulkanHandle<VkImage>(
image, [&vk = vk_, &device = device_](VkImage image) {
vk->DestroyImage(device->GetHandle(), image, nullptr);
});
VkMemoryRequirements mem_req;
vk_->GetImageMemoryRequirements(device_->GetHandle(), image, &mem_req);
VkMemoryAllocateInfo alloc_info{};
alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
alloc_info.allocationSize = mem_req.size;
alloc_info.memoryTypeIndex = static_cast<uint32_t>(__builtin_ctz(
mem_req.memoryTypeBits & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT));
VkDeviceMemory memory;
if (VK_CALL_LOG_ERROR(vk_->AllocateMemory(device_->GetHandle(), &alloc_info,
nullptr, &memory)) != VK_SUCCESS) {
return std::nullopt;
}
result.memory_ = vulkan::VulkanHandle<VkDeviceMemory>{
memory, [&vk = vk_, &device = device_](VkDeviceMemory memory) {
vk->FreeMemory(device->GetHandle(), memory, nullptr);
}};
if (VK_CALL_LOG_ERROR(VK_CALL_LOG_ERROR(vk_->BindImageMemory(
device_->GetHandle(), result.image_, result.memory_, 0)))) {
return std::nullopt;
}
result.context_ =
fml::RefPtr<TestVulkanContext>(const_cast<TestVulkanContext*>(this));
return result;
}
sk_sp<GrDirectContext> TestVulkanContext::GetGrDirectContext() const {
return context_;
}
} // namespace flutter::testing