Skip to content

Commit

Permalink
Add test context for loading swiftshader so
Browse files Browse the repository at this point in the history
  • Loading branch information
bdero committed Oct 19, 2021
1 parent 1dee294 commit 47ac97f
Show file tree
Hide file tree
Showing 20 changed files with 228 additions and 38 deletions.
5 changes: 2 additions & 3 deletions shell/config.gni
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ declare_args() {
shell_enable_gl = !is_fuchsia
shell_enable_metal = false

# TODO(dworsham): Enable once Fuchsia supports Vulkan through the embedder.
shell_enable_vulkan = false
shell_enable_vulkan = true
shell_enable_software = true
}

declare_args() {
test_enable_gl = shell_enable_gl
test_enable_metal = shell_enable_metal
test_enable_vulkan = is_fuchsia
test_enable_vulkan = shell_enable_vulkan
test_enable_software = shell_enable_software
}
16 changes: 5 additions & 11 deletions shell/gpu/gpu_surface_vulkan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,17 @@ namespace flutter {

GPUSurfaceVulkan::GPUSurfaceVulkan(
GPUSurfaceVulkanDelegate* delegate,
std::unique_ptr<vulkan::VulkanNativeSurface> native_surface,
bool render_to_surface)
std::unique_ptr<vulkan::VulkanNativeSurface> native_surface)
: GPUSurfaceVulkan(/*context=*/nullptr,
delegate,
std::move(native_surface),
render_to_surface) {}
std::move(native_surface)) {}

GPUSurfaceVulkan::GPUSurfaceVulkan(
const sk_sp<GrDirectContext>& context,
GPUSurfaceVulkanDelegate* delegate,
std::unique_ptr<vulkan::VulkanNativeSurface> native_surface,
bool render_to_surface)
: window_(context,
delegate->vk(),
std::move(native_surface),
render_to_surface),
render_to_surface_(render_to_surface),
std::unique_ptr<vulkan::VulkanNativeSurface> native_surface)
: window_(context, delegate->vk(), std::move(native_surface)),
render_to_surface_(native_surface),
weak_factory_(this) {}

GPUSurfaceVulkan::~GPUSurfaceVulkan() = default;
Expand Down
6 changes: 2 additions & 4 deletions shell/gpu/gpu_surface_vulkan.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,15 @@ class GPUSurfaceVulkan : public Surface {
/// GrDirectContext for Skia.
///
GPUSurfaceVulkan(GPUSurfaceVulkanDelegate* delegate,
std::unique_ptr<vulkan::VulkanNativeSurface> native_surface,
bool render_to_surface);
std::unique_ptr<vulkan::VulkanNativeSurface> native_surface);

//------------------------------------------------------------------------------
/// @brief Create a GPUSurfaceVulkan while letting it reuse an existing
/// GrDirectContext.
///
GPUSurfaceVulkan(const sk_sp<GrDirectContext>& context,
GPUSurfaceVulkanDelegate* delegate,
std::unique_ptr<vulkan::VulkanNativeSurface> native_surface,
bool render_to_surface);
std::unique_ptr<vulkan::VulkanNativeSurface> native_surface);

~GPUSurfaceVulkan() override;

Expand Down
1 change: 1 addition & 0 deletions shell/platform/android/context/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ source_set("context") {

deps = [
"//flutter/fml",
"//flutter/vulkan", # TODO(bdero)
"//third_party/skia",
]
}
1 change: 1 addition & 0 deletions shell/platform/android/surface/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ source_set("surface_mock") {
deps = [
":surface",
"//flutter/shell/gpu:gpu_surface_gl",
"//flutter/vulkan", # TODO(bdero)
"//third_party/googletest:gmock",
"//third_party/googletest:gtest",
"//third_party/skia",
Expand Down
16 changes: 15 additions & 1 deletion shell/platform/embedder/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ if (enable_unittests) {
"tests/embedder_unittests_gl.cc",
]

deps += [ "//flutter/testing:opengl" ]
deps += [
"//flutter/testing:opengl",
]
}

if (test_enable_metal) {
Expand All @@ -242,6 +244,18 @@ if (enable_unittests) {

deps += [ "//flutter/testing:metal" ]
}

if (test_enable_vulkan) {
sources += [
"tests/embedder_test_context_vulkan.cc",
"tests/embedder_test_context_vulkan.h",
]

deps += [
"//flutter/testing:vulkan",
"//flutter/vulkan",
]
}
}

# Tests the build in FLUTTER_ENGINE_NO_PROTOTYPES mode.
Expand Down
13 changes: 13 additions & 0 deletions shell/platform/embedder/embedder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,14 @@ static sk_sp<SkSurface> MakeSkSurfaceFromBackingStore(
#endif
}

static sk_sp<SkSurface> MakeSkSurfaceFromBackingStore(
GrDirectContext* context,
const FlutterBackingStoreConfig& config,
const FlutterVulkanBackingStore* vulkan) {
assert(false); // TODO(bdero)
return nullptr;
}

static std::unique_ptr<flutter::EmbedderRenderTarget>
CreateEmbedderRenderTarget(const FlutterCompositor* compositor,
const FlutterBackingStoreConfig& config,
Expand Down Expand Up @@ -665,6 +673,11 @@ CreateEmbedderRenderTarget(const FlutterCompositor* compositor,
render_surface =
MakeSkSurfaceFromBackingStore(context, config, &backing_store.metal);
break;

case kFlutterBackingStoreTypeVulkan:
render_surface =
MakeSkSurfaceFromBackingStore(context, config, &backing_store.vulkan);
break;
};

if (!render_surface) {
Expand Down
44 changes: 44 additions & 0 deletions shell/platform/embedder/embedder.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ typedef enum {
/// iOS version >= 10.0 (device), 13.0 (simulator)
/// macOS version >= 10.14
kMetal,
kVulkan,
} FlutterRendererType;

/// Additional accessibility features that may be enabled by the platform.
Expand Down Expand Up @@ -541,6 +542,33 @@ typedef struct {
FlutterMetalTextureFrameCallback external_texture_frame_callback;
} FlutterMetalRendererConfig;

typedef uintptr_t* (*FlutterVulkanGetNextImageCallback)(void* /* user data */);

typedef void* (*ProcAddressCallback)(void* user /* in */,
const char* pName /* in */);
typedef void* (*InstanceProcAddressCallback)(void* user /* in */,
uintptr_t* instance /* in */,
const char* pName /* in */);
typedef uintptr_t* (*UintPtrCallback)(void* user /* in */);

typedef struct {
/// The size of this struct. Must be sizeof(FlutterVulkanRendererConfig).
size_t struct_size;

void* user_data;

/// Platform Callbacks
ProcAddressCallback get_proc_address_callback;
InstanceProcAddressCallback get_instance_proc_address_callback;
UintPtrCallback get_instance_handle_callback;
UintPtrCallback get_physical_device_handle_callback;
UintPtrCallback get_device_handle_callback;

FlutterVulkanGetNextImageCallback acquire_next_image_callback;
VoidCallback terminate_callback;

} FlutterVulkanRendererConfig;

typedef struct {
/// The size of this struct. Must be sizeof(FlutterSoftwareRendererConfig).
size_t struct_size;
Expand All @@ -557,6 +585,7 @@ typedef struct {
FlutterOpenGLRendererConfig open_gl;
FlutterSoftwareRendererConfig software;
FlutterMetalRendererConfig metal;
FlutterVulkanRendererConfig vulkan;
};
} FlutterRendererConfig;

Expand Down Expand Up @@ -989,6 +1018,17 @@ typedef struct {
};
} FlutterMetalBackingStore;

typedef struct {
/// The size of this struct. Must be sizeof(FlutterVulkanBackingStore).
size_t struct_size;

/// VkImage handle.
uint64_t handle;

/// VkSemaphore signaled when engine is done writing image.
uint64_t image_ready;
} FlutterVulkanBackingStore;

typedef enum {
/// Indicates that the Flutter application requested that an opacity be
/// applied to the platform view.
Expand Down Expand Up @@ -1048,6 +1088,8 @@ typedef enum {
kFlutterBackingStoreTypeSoftware,
/// Specifies a Metal backing store. This is backed by a Metal texture.
kFlutterBackingStoreTypeMetal,
/// Specifies a Vulkan backing store. This is backed by a Vulkan VkImage.
kFlutterBackingStoreTypeVulkan,
} FlutterBackingStoreType;

typedef struct {
Expand All @@ -1069,6 +1111,8 @@ typedef struct {
FlutterSoftwareBackingStore software;
// The description of the Metal backing store.
FlutterMetalBackingStore metal;
// The description of the Vulkan backing store.
FlutterVulkanBackingStore vulkan;
};
} FlutterBackingStore;

Expand Down
19 changes: 19 additions & 0 deletions shell/platform/embedder/tests/embedder_assertions.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ inline bool operator==(const FlutterMetalTexture& a,
return a.texture_id == b.texture_id && a.texture == b.texture;
}

inline bool operator==(const FlutterVulkanBackingStore& a,
const FlutterVulkanBackingStore& b) {
return a.handle == b.handle && a.image_ready == b.image_ready;
}

inline bool operator==(const FlutterMetalBackingStore& a,
const FlutterMetalBackingStore& b) {
return a.texture == b.texture;
Expand Down Expand Up @@ -112,6 +117,8 @@ inline bool operator==(const FlutterBackingStore& a,
return a.software == b.software;
case kFlutterBackingStoreTypeMetal:
return a.metal == b.metal;
case kFlutterBackingStoreTypeVulkan:
return a.vulkan == b.vulkan;
}

return false;
Expand Down Expand Up @@ -230,6 +237,8 @@ inline std::string FlutterBackingStoreTypeToString(
return "kFlutterBackingStoreTypeSoftware";
case kFlutterBackingStoreTypeMetal:
return "kFlutterBackingStoreTypeMetal";
case kFlutterBackingStoreTypeVulkan:
return "kFlutterBackingStoreTypeVulkan";
}
return "Unknown";
}
Expand Down Expand Up @@ -347,6 +356,12 @@ inline std::ostream& operator<<(std::ostream& out,
return out << "(FlutterMetalBackingStore) Texture: " << item.texture;
}

inline std::ostream& operator<<(std::ostream& out,
const FlutterVulkanBackingStore& item) {
return out << "(FlutterVulkanBackingStore) Handle: " << item.handle
<< "Image Ready Semaphore: " << item.image_ready;
}

inline std::ostream& operator<<(std::ostream& out,
const FlutterBackingStore& backing_store) {
out << "(FlutterBackingStore) Struct size: " << backing_store.struct_size
Expand All @@ -366,6 +381,10 @@ inline std::ostream& operator<<(std::ostream& out,
case kFlutterBackingStoreTypeMetal:
out << backing_store.metal;
break;

case kFlutterBackingStoreTypeVulkan:
out << backing_store.vulkan;
break;
}

return out;
Expand Down
6 changes: 6 additions & 0 deletions shell/platform/embedder/tests/embedder_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "flutter/shell/platform/embedder/tests/embedder_test.h"
#include "flutter/shell/platform/embedder/tests/embedder_test_context_software.h"
#include "flutter/shell/platform/embedder/tests/embedder_test_context_vulkan.h"

#ifdef SHELL_ENABLE_GL
#include "flutter/shell/platform/embedder/tests/embedder_test_context_gl.h"
Expand Down Expand Up @@ -33,6 +34,11 @@ EmbedderTestContext& EmbedderTest::GetEmbedderContext(
std::make_unique<EmbedderTestContextSoftware>(
GetFixturesDirectory());
break;
case EmbedderTestContextType::kVulkanContext:
embedder_contexts_[type] =
std::make_unique<EmbedderTestContextVulkan>(
GetFixturesDirectory());
break;
#ifdef SHELL_ENABLE_GL
case EmbedderTestContextType::kOpenGLContext:
embedder_contexts_[type] =
Expand Down
1 change: 1 addition & 0 deletions shell/platform/embedder/tests/embedder_test_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ enum class EmbedderTestContextType {
kSoftwareContext,
kOpenGLContext,
kMetalContext,
kVulkanContext,
};

class EmbedderTestContext {
Expand Down
31 changes: 31 additions & 0 deletions shell/platform/embedder/tests/embedder_unittests_gl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <string>
#include <vector>

#include "vulkan/vulkan.h"

#include "embedder.h"
#include "embedder_engine.h"
#include "flutter/flow/raster_cache.h"
Expand All @@ -15,6 +17,7 @@
#include "flutter/fml/mapping.h"
#include "flutter/fml/message_loop.h"
#include "flutter/fml/message_loop_task_queues.h"
#include "flutter/fml/native_library.h"
#include "flutter/fml/paths.h"
#include "flutter/fml/synchronization/count_down_latch.h"
#include "flutter/fml/synchronization/waitable_event.h"
Expand All @@ -29,7 +32,9 @@
#include "flutter/shell/platform/embedder/tests/embedder_unittests_util.h"
#include "flutter/testing/assertions_skia.h"
#include "flutter/testing/test_gl_surface.h"
#include "flutter/testing/test_vulkan_context.h"
#include "flutter/testing/testing.h"
#include "flutter/vulkan/vulkan_proc_table.h"
#include "third_party/skia/include/core/SkSurface.h"
#include "third_party/skia/src/gpu/gl/GrGLDefines.h"
#include "third_party/tonic/converter/dart_converter.h"
Expand All @@ -39,6 +44,32 @@ namespace testing {

using EmbedderTest = testing::EmbedderTest;

TEST_F(EmbedderTest, CanLoadSwiftshaderVulkanICD) {
auto lib = fml::NativeLibrary::Create("libvk_swiftshader.dylib");
PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr =
(PFN_vkGetInstanceProcAddr)lib->ResolveSymbol("vkGetInstanceProcAddr");

PFN_vkCreateInstance vkCreateInstance =
(PFN_vkCreateInstance)vkGetInstanceProcAddr(nullptr, "vkCreateInstance");

VkInstanceCreateInfo info = {
.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
};
VkInstance inst;
VkResult result = vkCreateInstance(&info, nullptr, &inst);
ASSERT_EQ(result, VK_SUCCESS);
}

TEST_F(EmbedderTest, CanResolveVulkanProcTable) {
auto proc_table =
fml::MakeRefCounted<vulkan::VulkanProcTable>("libvk_swiftshader.dylib");
ASSERT_TRUE(proc_table->HasAcquiredMandatoryProcAddresses());
}

TEST_F(EmbedderTest, CanInitializeTestVulkanContext) {
TestVulkanContext ctx;
}

TEST_F(EmbedderTest, CanCreateOpenGLRenderingEngine) {
EmbedderConfigBuilder builder(
GetEmbedderContext(EmbedderTestContextType::kOpenGLContext));
Expand Down
8 changes: 8 additions & 0 deletions testing/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,15 @@ if (enable_unittests) {
source_set("vulkan") {
testonly = true

sources = [
"test_vulkan_context.h",
"test_vulkan_context.cc",
]

deps = [
":skia",
"//flutter/fml",
"//flutter/vulkan",
]

# SwiftShader only supports x86/x64_64
Expand Down Expand Up @@ -161,6 +167,7 @@ if (enable_unittests) {
deps = [
":skia",
"//flutter/fml",
"//flutter/vulkan", # TODO(bdero)
]
}

Expand All @@ -183,6 +190,7 @@ if (enable_unittests) {
":testing",
":testing_fixtures",
"//flutter/runtime:libdart",
"//flutter/vulkan",
]

if (shell_enable_metal) {
Expand Down
Loading

0 comments on commit 47ac97f

Please sign in to comment.