Makes Skia's vkQueueSubmit threadsafe. (flutter/engine#45459)
fixes https://github.com/flutter/flutter/issues/133933 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
@@ -361,6 +361,10 @@ if (enable_unittests) {
|
||||
if (test_enable_metal) {
|
||||
sources += [ "tests/embedder_metal_unittests.mm" ]
|
||||
}
|
||||
|
||||
if (test_enable_vulkan) {
|
||||
sources += [ "tests/embedder_vk_unittests.cc" ]
|
||||
}
|
||||
}
|
||||
|
||||
executable("embedder_a11y_unittests") {
|
||||
|
||||
@@ -0,0 +1,173 @@
|
||||
// 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.
|
||||
|
||||
#define FML_USED_ON_EMBEDDER
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "embedder.h"
|
||||
#include "embedder_engine.h"
|
||||
#include "flutter/fml/synchronization/count_down_latch.h"
|
||||
#include "flutter/shell/platform/embedder/tests/embedder_config_builder.h"
|
||||
#include "flutter/shell/platform/embedder/tests/embedder_test.h"
|
||||
#include "flutter/shell/platform/embedder/tests/embedder_unittests_util.h"
|
||||
#include "flutter/testing/testing.h"
|
||||
|
||||
// CREATE_NATIVE_ENTRY is leaky by design
|
||||
// NOLINTBEGIN(clang-analyzer-core.StackAddressEscape)
|
||||
|
||||
namespace flutter {
|
||||
namespace testing {
|
||||
|
||||
using EmbedderTest = testing::EmbedderTest;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// Notice: Other Vulkan unit tests exist in embedder_gl_unittests.cc.
|
||||
// See https://github.com/flutter/flutter/issues/134322
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
TEST_F(EmbedderTest, CanLaunchAndShutdownWithValidProjectArgsVulkan) {
|
||||
auto& context = GetEmbedderContext(EmbedderTestContextType::kVulkanContext);
|
||||
fml::AutoResetWaitableEvent latch;
|
||||
context.AddIsolateCreateCallback([&latch]() { latch.Signal(); });
|
||||
EmbedderConfigBuilder builder(context);
|
||||
builder.SetVulkanRendererConfig(SkISize::Make(1024, 1024));
|
||||
auto engine = builder.LaunchEngine();
|
||||
ASSERT_TRUE(engine.is_valid());
|
||||
// Wait for the root isolate to launch.
|
||||
latch.Wait();
|
||||
engine.reset();
|
||||
}
|
||||
|
||||
TEST_F(EmbedderTest,
|
||||
NoLayerCreatedForTransparentOverlayOnTopOfPlatformLayerVulkan) {
|
||||
auto& context = GetEmbedderContext(EmbedderTestContextType::kVulkanContext);
|
||||
|
||||
EmbedderConfigBuilder builder(context);
|
||||
builder.SetVulkanRendererConfig(SkISize::Make(800, 600));
|
||||
builder.SetCompositor();
|
||||
builder.SetDartEntrypoint("can_composite_platform_views_transparent_overlay");
|
||||
|
||||
builder.SetRenderTargetType(
|
||||
EmbedderTestBackingStoreProducer::RenderTargetType::kVulkanImage);
|
||||
|
||||
fml::CountDownLatch latch(4);
|
||||
|
||||
auto scene_image = context.GetNextSceneImage();
|
||||
|
||||
context.GetCompositor().SetNextPresentCallback(
|
||||
[&](const FlutterLayer** layers, size_t layers_count) {
|
||||
ASSERT_EQ(layers_count, 2u);
|
||||
|
||||
// Layer Root
|
||||
{
|
||||
FlutterBackingStore backing_store = *layers[0]->backing_store;
|
||||
backing_store.type = kFlutterBackingStoreTypeVulkan;
|
||||
backing_store.did_update = true;
|
||||
backing_store.software.height = 600;
|
||||
|
||||
FlutterRect paint_region_rects[] = {
|
||||
FlutterRectMakeLTRB(0, 0, 800, 600),
|
||||
};
|
||||
FlutterRegion paint_region = {
|
||||
.struct_size = sizeof(FlutterRegion),
|
||||
.rects_count = 1,
|
||||
.rects = paint_region_rects,
|
||||
};
|
||||
FlutterBackingStorePresentInfo present_info = {
|
||||
.struct_size = sizeof(FlutterBackingStorePresentInfo),
|
||||
.paint_region = &paint_region,
|
||||
};
|
||||
|
||||
FlutterLayer layer = {};
|
||||
layer.struct_size = sizeof(layer);
|
||||
layer.type = kFlutterLayerContentTypeBackingStore;
|
||||
layer.backing_store = &backing_store;
|
||||
layer.size = FlutterSizeMake(800.0, 600.0);
|
||||
layer.offset = FlutterPointMake(0.0, 0.0);
|
||||
layer.backing_store_present_info = &present_info;
|
||||
|
||||
ASSERT_EQ(*layers[0], layer);
|
||||
}
|
||||
|
||||
// Layer 1
|
||||
{
|
||||
FlutterPlatformView platform_view = *layers[1]->platform_view;
|
||||
platform_view.struct_size = sizeof(platform_view);
|
||||
platform_view.identifier = 1;
|
||||
|
||||
FlutterLayer layer = {};
|
||||
layer.struct_size = sizeof(layer);
|
||||
layer.type = kFlutterLayerContentTypePlatformView;
|
||||
layer.platform_view = &platform_view;
|
||||
layer.size = FlutterSizeMake(50.0, 150.0);
|
||||
layer.offset = FlutterPointMake(20.0, 20.0);
|
||||
|
||||
ASSERT_EQ(*layers[1], layer);
|
||||
}
|
||||
|
||||
latch.CountDown();
|
||||
});
|
||||
|
||||
context.GetCompositor().SetPlatformViewRendererCallback(
|
||||
[&](const FlutterLayer& layer, GrDirectContext*
|
||||
/* don't use because software compositor */) -> sk_sp<SkImage> {
|
||||
auto surface = CreateRenderSurface(
|
||||
layer, nullptr /* null because software compositor */);
|
||||
auto canvas = surface->getCanvas();
|
||||
FML_CHECK(canvas != nullptr);
|
||||
|
||||
switch (layer.platform_view->identifier) {
|
||||
case 1: {
|
||||
SkPaint paint;
|
||||
// See dart test for total order.
|
||||
paint.setColor(SK_ColorGREEN);
|
||||
paint.setAlpha(127);
|
||||
const auto& rect =
|
||||
SkRect::MakeWH(layer.size.width, layer.size.height);
|
||||
canvas->drawRect(rect, paint);
|
||||
latch.CountDown();
|
||||
} break;
|
||||
default:
|
||||
// Asked to render an unknown platform view.
|
||||
FML_CHECK(false)
|
||||
<< "Test was asked to composite an unknown platform view.";
|
||||
}
|
||||
|
||||
return surface->makeImageSnapshot();
|
||||
});
|
||||
|
||||
context.AddNativeCallback(
|
||||
"SignalNativeTest",
|
||||
CREATE_NATIVE_ENTRY(
|
||||
[&latch](Dart_NativeArguments args) { latch.CountDown(); }));
|
||||
|
||||
auto engine = builder.LaunchEngine();
|
||||
|
||||
// Send a window metrics events so frames may be scheduled.
|
||||
FlutterWindowMetricsEvent event = {};
|
||||
event.struct_size = sizeof(event);
|
||||
event.width = 800;
|
||||
event.height = 600;
|
||||
event.pixel_ratio = 1.0;
|
||||
event.physical_view_inset_top = 0.0;
|
||||
event.physical_view_inset_right = 0.0;
|
||||
event.physical_view_inset_bottom = 0.0;
|
||||
event.physical_view_inset_left = 0.0;
|
||||
ASSERT_EQ(FlutterEngineSendWindowMetricsEvent(engine.get(), &event),
|
||||
kSuccess);
|
||||
ASSERT_TRUE(engine.is_valid());
|
||||
|
||||
latch.Wait();
|
||||
|
||||
// There should no present calls on the root surface.
|
||||
ASSERT_EQ(context.GetSurfacePresentCount(), 0u);
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace flutter
|
||||
|
||||
// NOLINTEND(clang-analyzer-core.StackAddressEscape)
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include "flutter/vulkan/procs/vulkan_proc_table.h"
|
||||
|
||||
#include <mutex>
|
||||
#include <utility>
|
||||
|
||||
#include "flutter/fml/logging.h"
|
||||
@@ -19,6 +20,16 @@
|
||||
return false; \
|
||||
}
|
||||
|
||||
#if defined(__ANDROID__) && defined(__ARM_ARCH) && __ARM_ARCH >= 7 && \
|
||||
defined(__ARM_32BIT_STATE)
|
||||
// 32bit Android appears to use different calling conventions.
|
||||
// See:
|
||||
// https://android.googlesource.com/platform/external/vulkan-headers/+/refs/heads/master/include/vulkan/vk_platform.h#46
|
||||
#define VKAPI_ATTR __attribute__((pcs("aapcs-vfp")))
|
||||
#else
|
||||
#define VKAPI_ATTR
|
||||
#endif
|
||||
|
||||
namespace vulkan {
|
||||
|
||||
VulkanProcTable::VulkanProcTable() : VulkanProcTable("libvulkan.so"){};
|
||||
@@ -229,6 +240,82 @@ PFN_vkVoidFunction VulkanProcTable::AcquireProc(
|
||||
GetInstanceProcAddr(instance, proc_name));
|
||||
}
|
||||
|
||||
namespace {
|
||||
// These are atomic since 2 threads could simultaneously call the
|
||||
// AcquireThreadsafe* functions.
|
||||
std::atomic<decltype(vkQueueSubmit)*> g_non_threadsafe_vkQueueSubmit;
|
||||
std::atomic<decltype(vkQueueWaitIdle)*> g_non_threadsafe_vkQueueWaitIdle;
|
||||
|
||||
std::mutex& GetThreadsafeVkQueueSubmitMutex() {
|
||||
// Initialization of function static variables are threadsafe in C++11.
|
||||
static std::mutex* mutex_ptr = new std::mutex();
|
||||
return *mutex_ptr;
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult vkQueueSubmitThreadsafe(VkQueue queue,
|
||||
uint32_t submitCount,
|
||||
const VkSubmitInfo* pSubmits,
|
||||
VkFence fence) {
|
||||
std::scoped_lock lock(GetThreadsafeVkQueueSubmitMutex());
|
||||
return g_non_threadsafe_vkQueueSubmit.load()(queue, submitCount, pSubmits,
|
||||
fence);
|
||||
}
|
||||
|
||||
VKAPI_ATTR VkResult vkQueueWaitIdleThreadsafe(VkQueue queue) {
|
||||
std::scoped_lock lock(GetThreadsafeVkQueueSubmitMutex());
|
||||
return g_non_threadsafe_vkQueueWaitIdle.load()(queue);
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
struct CheckSameSignature : std::false_type {};
|
||||
|
||||
template <typename Ret, typename... Args>
|
||||
struct CheckSameSignature<Ret(Args...), Ret(Args...)> : std::true_type {};
|
||||
|
||||
// These static asserts don't work in platforms where the functions have calling
|
||||
// convention attributes, like on Android. See |VKAPI_ATTR|.
|
||||
#if defined(FML_OS_MACOSX) || defined(FML_OS_LINUX)
|
||||
static_assert(CheckSameSignature<decltype(vkQueueSubmit),
|
||||
decltype(vkQueueSubmitThreadsafe)>::value);
|
||||
static_assert(CheckSameSignature<decltype(vkQueueWaitIdle),
|
||||
decltype(vkQueueWaitIdleThreadsafe)>::value);
|
||||
#endif
|
||||
} // namespace
|
||||
|
||||
PFN_vkVoidFunction VulkanProcTable::AcquireThreadsafeSubmitQueue(
|
||||
const VulkanHandle<VkDevice>& device) const {
|
||||
if (!device || !GetInstanceProcAddr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto non_threadsafe_vkQueueSubmit =
|
||||
reinterpret_cast<decltype(vkQueueSubmit)*>(
|
||||
GetDeviceProcAddr(device, "vkQueueSubmit"));
|
||||
FML_DCHECK(g_non_threadsafe_vkQueueSubmit.load() == nullptr ||
|
||||
g_non_threadsafe_vkQueueSubmit.load() ==
|
||||
non_threadsafe_vkQueueSubmit);
|
||||
g_non_threadsafe_vkQueueSubmit.store(non_threadsafe_vkQueueSubmit);
|
||||
|
||||
return reinterpret_cast<PFN_vkVoidFunction>(vkQueueSubmitThreadsafe);
|
||||
}
|
||||
|
||||
PFN_vkVoidFunction VulkanProcTable::AcquireThreadsafeQueueWaitIdle(
|
||||
const VulkanHandle<VkDevice>& device) const {
|
||||
if (!device || !GetInstanceProcAddr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto non_threadsafe_vkQueueWaitIdle =
|
||||
reinterpret_cast<decltype(vkQueueWaitIdle)*>(
|
||||
GetDeviceProcAddr(device, "vkQueueWaitIdle"));
|
||||
FML_DCHECK(g_non_threadsafe_vkQueueWaitIdle.load() == nullptr ||
|
||||
g_non_threadsafe_vkQueueWaitIdle.load() ==
|
||||
non_threadsafe_vkQueueWaitIdle);
|
||||
g_non_threadsafe_vkQueueWaitIdle.store(non_threadsafe_vkQueueWaitIdle);
|
||||
|
||||
return reinterpret_cast<PFN_vkVoidFunction>(vkQueueWaitIdleThreadsafe);
|
||||
}
|
||||
|
||||
PFN_vkVoidFunction VulkanProcTable::AcquireProc(
|
||||
const char* proc_name,
|
||||
const VulkanHandle<VkDevice>& device) const {
|
||||
|
||||
@@ -163,6 +163,12 @@ class VulkanProcTable : public fml::RefCountedThreadSafe<VulkanProcTable> {
|
||||
PFN_vkVoidFunction AcquireProc(const char* proc_name,
|
||||
const VulkanHandle<VkDevice>& device) const;
|
||||
|
||||
PFN_vkVoidFunction AcquireThreadsafeSubmitQueue(
|
||||
const VulkanHandle<VkDevice>& device) const;
|
||||
|
||||
PFN_vkVoidFunction AcquireThreadsafeQueueWaitIdle(
|
||||
const VulkanHandle<VkDevice>& device) const;
|
||||
|
||||
private:
|
||||
fml::RefPtr<fml::NativeLibrary> handle_;
|
||||
bool acquired_mandatory_proc_addresses_;
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
|
||||
#include "flutter/vulkan/vulkan_skia_proc_table.h"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace vulkan {
|
||||
|
||||
GrVkGetProc CreateSkiaGetProc(const fml::RefPtr<vulkan::VulkanProcTable>& vk) {
|
||||
@@ -13,8 +15,17 @@ GrVkGetProc CreateSkiaGetProc(const fml::RefPtr<vulkan::VulkanProcTable>& vk) {
|
||||
|
||||
return [vk](const char* proc_name, VkInstance instance, VkDevice device) {
|
||||
if (device != VK_NULL_HANDLE) {
|
||||
auto result =
|
||||
vk->AcquireProc(proc_name, VulkanHandle<VkDevice>{device, nullptr});
|
||||
PFN_vkVoidFunction result = nullptr;
|
||||
VulkanHandle<VkDevice> device_handle =
|
||||
VulkanHandle<VkDevice>{device, nullptr};
|
||||
if (strcmp(proc_name, "vkQueueSubmit") == 0) {
|
||||
result = vk->AcquireThreadsafeSubmitQueue(device_handle);
|
||||
} else if (strcmp(proc_name, "vkQueueWaitIdle") == 0) {
|
||||
result = vk->AcquireThreadsafeQueueWaitIdle(device_handle);
|
||||
} else {
|
||||
result = vk->AcquireProc(proc_name, device_handle);
|
||||
}
|
||||
|
||||
if (result != nullptr) {
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user