Skip to content

Commit

Permalink
Implementing feedback.
Browse files Browse the repository at this point in the history
.
  • Loading branch information
phoenixxxx committed Dec 5, 2024
1 parent a3cf53c commit 901b1a2
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 219 deletions.
2 changes: 1 addition & 1 deletion android/samples/sample-stream-test/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ android {
compileSdkVersion versions.compileSdk
defaultConfig {
applicationId "com.google.android.filament.streamtest"
minSdkVersion 21
minSdkVersion 23
targetSdkVersion versions.targetSdk
}

Expand Down
10 changes: 6 additions & 4 deletions filament/backend/include/backend/platforms/VulkanPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,15 +339,17 @@ class VulkanPlatform : public Platform, utils::PrivateImplementation<VulkanPlatf
uint32_t memoryTypeBits;
};
virtual ExternalImageMetadata getExternalImageMetadata(void* externalImage);
virtual VkImage createExternalImage(void* externalImage, const ExternalImageMetadata& metadata, VkDeviceMemory& memory);

typedef std::pair<VkImage, VkDeviceMemory> imageData;
virtual imageData createExternalImage(void* externalImage,
const ExternalImageMetadata& metadata);

private:
static ExtensionSet getSwapchainInstanceExtensions();
static ExternalImageMetadata getExternalImageMetadataImpl(void* externalImage,
VkDevice device);
static VkImage createExternalImageImpl(void* externalImage, VkDevice device,
const VkAllocationCallbacks* allocator, const ExternalImageMetadata& metadata,
VkDeviceMemory& memory);
static imageData createExternalImageImpl(void* externalImage, VkDevice device,
const VkAllocationCallbacks* allocator, const ExternalImageMetadata& metadata);

// Platform dependent helper methods
using SurfaceBundle = std::tuple<VkSurfaceKHR, VkExtent2D>;
Expand Down
6 changes: 2 additions & 4 deletions filament/backend/src/vulkan/VulkanDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,6 @@ void VulkanDriver::createTextureExternalImageR(Handle<HwTexture> th, backend::Te
uint32_t width, uint32_t height, backend::TextureUsage usage, void* externalImage) {
FVK_SYSTRACE_SCOPE();

usage = backend::TextureUsage::SAMPLEABLE;
const auto& metadata = mPlatform->getExternalImageMetadata(externalImage);
if (metadata.isProtected) {
usage |= backend::TextureUsage::PROTECTED;
Expand All @@ -557,11 +556,10 @@ void VulkanDriver::createTextureExternalImageR(Handle<HwTexture> th, backend::Te
assert_invariant(height == metadata.height);
assert_invariant(getVkFormat(format) == metadata.format);

VkDeviceMemory memory;
VkImage image = mPlatform->createExternalImage(externalImage, metadata, memory);
const auto& data = mPlatform->createExternalImage(externalImage, metadata);

auto texture = resource_ptr<VulkanTexture>::make(&mResourceManager, th, mPlatform->getDevice(),
mAllocator, &mResourceManager, &mCommands, image, memory, metadata.format,
mAllocator, &mResourceManager, &mCommands, data.first, data.second, metadata.format,
1, metadata.width, metadata.height, usage, mStagePool);

texture.inc();
Expand Down
9 changes: 5 additions & 4 deletions filament/backend/src/vulkan/platform/VulkanPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ ExtensionSet getDeviceExtensions(VkPhysicalDevice device) {
#if FVK_ENABLED(FVK_DEBUG_DEBUG_UTILS)
VK_EXT_DEBUG_MARKER_EXTENSION_NAME,
#endif
// We only support external image for Android for now, but nothing bars us from
// supporting other platforms.
#if defined(__ANDROID__)
VK_KHR_EXTERNAL_MEMORY_EXTENSION_NAME,
VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME,
Expand Down Expand Up @@ -957,10 +959,9 @@ VulkanPlatform::ExternalImageMetadata VulkanPlatform::getExternalImageMetadata(
return getExternalImageMetadataImpl(externalImage, mImpl->mDevice);
}

VkImage VulkanPlatform::createExternalImage(void* externalImage,
const ExternalImageMetadata& metadata, VkDeviceMemory& memory) {
return createExternalImageImpl(externalImage, mImpl->mDevice, nullptr, metadata,
memory);
VulkanPlatform::imageData VulkanPlatform::createExternalImage(void* externalImage,
const ExternalImageMetadata& metadata) {
return createExternalImageImpl(externalImage, mImpl->mDevice, nullptr, metadata);
}

#undef SWAPCHAIN_RET_FUNC
Expand Down
113 changes: 48 additions & 65 deletions filament/backend/src/vulkan/platform/VulkanPlatformAndroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

using namespace bluevk;

namespace filament::backend {
void GetVKFormatAndUsage(const AHardwareBuffer_Desc& desc,
namespace {
void getVKFormatAndUsage(const AHardwareBuffer_Desc& desc,
VkFormat& format,
VkImageUsageFlags& usage,
bool& isProtected) {
Expand Down Expand Up @@ -104,49 +104,10 @@ void GetVKFormatAndUsage(const AHardwareBuffer_Desc& desc,
}
}

void describeExternalImage(void* externalBuffer, VkDevice device,
VulkanPlatform::ExternalImageMetadata& metadata) {
AHardwareBuffer* buffer = static_cast<AHardwareBuffer*>(externalBuffer);
if (__builtin_available(android 26, *)) {
AHardwareBuffer_Desc buffer_desc;
AHardwareBuffer_describe(buffer, &buffer_desc);
metadata.width = buffer_desc.width;
metadata.height = buffer_desc.height;
metadata.layers = buffer_desc.layers;

GetVKFormatAndUsage(buffer_desc, metadata.format, metadata.usage,
metadata.isProtected);
}
// In the unprotected case add R/W capabilities
if (metadata.isProtected == false) {
metadata.usage |= VK_IMAGE_USAGE_TRANSFER_DST_BIT |
VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
}

VkAndroidHardwareBufferFormatPropertiesANDROID format_info = {
.sType =
VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID,
.pNext = nullptr,
};
VkAndroidHardwareBufferPropertiesANDROID properties = {
.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_PROPERTIES_ANDROID,
.pNext = &format_info,
};
VkResult prop_res = vkGetAndroidHardwareBufferPropertiesANDROID(device, buffer,
&properties);
FILAMENT_CHECK_POSTCONDITION(prop_res == VK_SUCCESS);

FILAMENT_CHECK_POSTCONDITION(metadata.format == format_info.format);
metadata.externalFormat = format_info.externalFormat;
metadata.allocationSize = properties.allocationSize;
metadata.memoryTypeBits = properties.memoryTypeBits;
}

VkImage allocateExternalImage(void* externalBuffer, VkDevice device,
VulkanPlatform::imageData allocateExternalImage(void* externalBuffer, VkDevice device,
const VkAllocationCallbacks* allocator,
const VulkanPlatform::ExternalImageMetadata& metadata,
VkDeviceMemory& memory) {
VkImage image;
const VulkanPlatform::ExternalImageMetadata& metadata) {
VulkanPlatform::imageData data;
AHardwareBuffer* buffer = static_cast<AHardwareBuffer*>(externalBuffer);

//if external format we need to specifiy it in the allocation
Expand Down Expand Up @@ -183,7 +144,7 @@ VkImage allocateExternalImage(void* externalBuffer, VkDevice device,
VK_IMAGE_USAGE_TRANSFER_SRC_BIT;

VkResult const result =
vkCreateImage(device, &imageInfo, allocator, &image);
vkCreateImage(device, &imageInfo, allocator, &data.first);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS);

// Allocate the memory
Expand All @@ -195,7 +156,7 @@ VkImage allocateExternalImage(void* externalBuffer, VkDevice device,
VkMemoryDedicatedAllocateInfo memory_dedicated_allocate_info = {
.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
.pNext = &android_hardware_buffer_info,
.image = image,
.image = data.first,
.buffer = VK_NULL_HANDLE,
};
VkMemoryAllocateInfo alloc_info = {
Expand All @@ -204,37 +165,59 @@ VkImage allocateExternalImage(void* externalBuffer, VkDevice device,
.allocationSize = metadata.allocationSize,
.memoryTypeIndex = metadata.memoryTypeBits };
VkResult const result_alloc =
vkAllocateMemory(device, &alloc_info, allocator, &memory);
vkAllocateMemory(device, &alloc_info, allocator, &data.second);
FILAMENT_CHECK_POSTCONDITION(result_alloc == VK_SUCCESS);

return image;
return data;
}

VulkanPlatform::ExternalImageMetadata VulkanPlatform::getExternalImageMetadataImpl(
void* externalImage, VkDevice device) {
VulkanPlatform::ExternalImageMetadata metadata = {
.width = 0,
.height = 0,
.format = VK_FORMAT_UNDEFINED,
.isProtected = false,
.externalFormat = 0,
.usage = 0,
.allocationSize = 0,
.memoryTypeBits = 0,
VulkanPlatform::ExternalImageMetadata metadata;
AHardwareBuffer* buffer = static_cast<AHardwareBuffer*>(externalImage);
if (__builtin_available(android 26, *)) {
AHardwareBuffer_Desc buffer_desc;
AHardwareBuffer_describe(buffer, &buffer_desc);
metadata.width = buffer_desc.width;
metadata.height = buffer_desc.height;
metadata.layers = buffer_desc.layers;

getVKFormatAndUsage(buffer_desc, metadata.format, metadata.usage,
metadata.isProtected);
}
// In the unprotected case add R/W capabilities
if (metadata.isProtected == false) {
metadata.usage |= VK_IMAGE_USAGE_TRANSFER_DST_BIT |
VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
}

VkAndroidHardwareBufferFormatPropertiesANDROID format_info = {
.sType =
VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID,
.pNext = nullptr,
};
VkAndroidHardwareBufferPropertiesANDROID properties = {
.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_PROPERTIES_ANDROID,
.pNext = &format_info,
};
describeExternalImage(externalImage, device, metadata);
VkResult prop_res = vkGetAndroidHardwareBufferPropertiesANDROID(device, buffer,
&properties);
FILAMENT_CHECK_POSTCONDITION(prop_res == VK_SUCCESS);

FILAMENT_CHECK_POSTCONDITION(metadata.format == format_info.format);
metadata.externalFormat = format_info.externalFormat;
metadata.allocationSize = properties.allocationSize;
metadata.memoryTypeBits = properties.memoryTypeBits;
return metadata;
}

VkImage VulkanPlatform::createExternalImageImpl(void* externalImage, VkDevice device,
const VkAllocationCallbacks* allocator, const ExternalImageMetadata& metadata,
VkDeviceMemory& memory) {
VkImage image = allocateExternalImage(externalImage, device, allocator, metadata,
memory);
VkResult result = vkBindImageMemory(device, image, memory, 0);
VulkanPlatform::imageData VulkanPlatform::createExternalImageImpl(void* externalImage, VkDevice device,
const VkAllocationCallbacks* allocator, const ExternalImageMetadata& metadata) {
imageData data = allocateExternalImage(externalImage, device, allocator, metadata);
VkResult result = vkBindImageMemory(device, data.first, data.second, 0);
FILAMENT_CHECK_POSTCONDITION(result == VK_SUCCESS) << "vkBindImageMemory error="
<< result << ".";
return image;
return data;
}

VulkanPlatform::ExtensionSet VulkanPlatform::getSwapchainInstanceExtensions() {
Expand Down
7 changes: 3 additions & 4 deletions filament/backend/src/vulkan/platform/VulkanPlatformApple.mm
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,9 @@
void* externalImage, VkDevice device) {
return {};
}
VkImage VulkanPlatform::createExternalImageImpl(void* externalImage, VkDevice device,
const VkAllocationCallbacks* allocator, const ExternalImageMetadata& metadata,
VkDeviceMemory& memory) {
return VK_NULL_HANDLE;
VulkanPlatform::imageData VulkanPlatform::createExternalImageImpl(void* externalImage, VkDevice device,
const VkAllocationCallbacks* allocator, const ExternalImageMetadata& metadata) {
return {VK_NULL_HANDLE, VK_NULL_HANDLE};
}
VulkanPlatform::SurfaceBundle VulkanPlatform::createVkSurfaceKHR(void* nativeWindow,
VkInstance instance, uint64_t flags) noexcept {
Expand Down
Loading

0 comments on commit 901b1a2

Please sign in to comment.