mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-12-05 12:35:42 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1a39d71724 |
No files matched your search
@@ -8,6 +8,23 @@ static inline int vkp_linear_dmabuf_through_buffer(int drm_modifiers, uint64_t m
|
||||
return !drm_modifiers && modifier == 0 && !blit_dst;
|
||||
}
|
||||
|
||||
static inline int vkp_linear_dmabuf_mapped(int dmabuf_import, int blit_dst) {
|
||||
return !dmabuf_import && !blit_dst;
|
||||
}
|
||||
|
||||
static inline VkBufferMemoryBarrier vkp_linear_dmabuf_host_copy(VkBuffer buffer) {
|
||||
return (VkBufferMemoryBarrier){
|
||||
.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER,
|
||||
.srcAccessMask = 0,
|
||||
.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT,
|
||||
.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED,
|
||||
.buffer = buffer,
|
||||
.offset = 0,
|
||||
.size = VK_WHOLE_SIZE,
|
||||
};
|
||||
}
|
||||
|
||||
static inline int vkp_linear_dmabuf_foreign_image(int dmabuf, int staged) {
|
||||
return dmabuf && !staged;
|
||||
}
|
||||
|
||||
@@ -25,6 +25,9 @@
|
||||
#include <stdarg.h>
|
||||
#include <android/log.h>
|
||||
#include <android/hardware_buffer.h>
|
||||
#include <linux/dma-buf.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#define TAG "DroidDeckWayland"
|
||||
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)
|
||||
@@ -47,10 +50,15 @@ struct vkp_image {
|
||||
VkBuffer linear_buf;
|
||||
VkDeviceMemory linear_mem;
|
||||
VkBufferImageCopy linear_copy;
|
||||
const void *cpu_src;
|
||||
void *cpu_dst;
|
||||
size_t cpu_size, cpu_map_size;
|
||||
int cpu_fd;
|
||||
};
|
||||
|
||||
static ANativeWindow *g_window;
|
||||
static int g_ahb_import;
|
||||
static int g_dmabuf_import = 1;
|
||||
static int g_drm_modifiers;
|
||||
static char *g_driver_path, *g_library_name, *g_native_lib_dir;
|
||||
static int g_allow_driver_fallback;
|
||||
@@ -635,6 +643,7 @@ static int dev_init(void) {
|
||||
else LOGE("present: driver MISSING %s (dmabuf import will fail)", import_exts[i]);
|
||||
}
|
||||
g_drm_modifiers = has_ext(exts, ne, "VK_EXT_image_drm_format_modifier");
|
||||
g_dmabuf_import = has_ext(exts, ne, "VK_KHR_external_memory_fd") && has_ext(exts, ne, "VK_EXT_external_memory_dma_buf");
|
||||
int want_hdr_md = 0;
|
||||
if (droiddeck_color_requested()) {
|
||||
want_hdr_md = has_ext(exts, ne, VK_EXT_HDR_METADATA_EXTENSION_NAME);
|
||||
@@ -1120,10 +1129,21 @@ static void release_external_client_draws(VkCommandBuffer cmd, const struct vkp_
|
||||
}
|
||||
}
|
||||
|
||||
static void copy_mapped_dmabuf(const struct vkp_image *img) {
|
||||
struct dma_buf_sync sync = {.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_READ};
|
||||
ioctl(img->cpu_fd, DMA_BUF_IOCTL_SYNC, &sync);
|
||||
memcpy(img->cpu_dst, img->cpu_src, img->cpu_size);
|
||||
sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ;
|
||||
ioctl(img->cpu_fd, DMA_BUF_IOCTL_SYNC, &sync);
|
||||
}
|
||||
|
||||
static void stage_linear_dmabuf(VkCommandBuffer cmd, const struct vkp_image *img) {
|
||||
if (!img || !img->linear_buf) return;
|
||||
const int mapped = img->cpu_src != NULL;
|
||||
if (mapped) copy_mapped_dmabuf(img);
|
||||
const VkImageSubresourceRange range = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
|
||||
VkBufferMemoryBarrier acquire = vkp_linear_dmabuf_ownership(img->linear_buf, g_qfam, VK_ACCESS_TRANSFER_READ_BIT, 1);
|
||||
VkBufferMemoryBarrier acquire = mapped ? vkp_linear_dmabuf_host_copy(img->linear_buf)
|
||||
: vkp_linear_dmabuf_ownership(img->linear_buf, g_qfam, VK_ACCESS_TRANSFER_READ_BIT, 1);
|
||||
VkImageMemoryBarrier write = {
|
||||
.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER, .oldLayout = VK_IMAGE_LAYOUT_UNDEFINED,
|
||||
.newLayout = VK_IMAGE_LAYOUT_GENERAL,
|
||||
@@ -1140,7 +1160,7 @@ static void stage_linear_dmabuf(VkCommandBuffer cmd, const struct vkp_image *img
|
||||
.image = img->image, .subresourceRange = range, .srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT,
|
||||
.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT | VK_ACCESS_SHADER_READ_BIT};
|
||||
g_vk.CmdPipelineBarrier(cmd, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 0,
|
||||
0, NULL, 1, &release, 1, &read);
|
||||
0, NULL, mapped ? 0 : 1, &release, 1, &read);
|
||||
}
|
||||
|
||||
static void stage_linear_dmabufs(VkCommandBuffer cmd, const struct vkp_draw *draws, int count) {
|
||||
@@ -1237,11 +1257,83 @@ static void destroy_image_objects(struct vkp_image *img) {
|
||||
if (img->mem) g_vk.FreeMemory(g_dev, img->mem, NULL);
|
||||
if (img->linear_buf) g_vk.DestroyBuffer(g_dev, img->linear_buf, NULL);
|
||||
if (img->linear_mem) g_vk.FreeMemory(g_dev, img->linear_mem, NULL);
|
||||
if (img->cpu_src) {
|
||||
munmap((void *)img->cpu_src, img->cpu_map_size);
|
||||
close(img->cpu_fd);
|
||||
}
|
||||
img->image = VK_NULL_HANDLE; img->mem = VK_NULL_HANDLE;
|
||||
img->linear_buf = VK_NULL_HANDLE; img->linear_mem = VK_NULL_HANDLE;
|
||||
img->cpu_src = NULL; img->cpu_dst = NULL;
|
||||
img->staged = 0; img->in_general = 0;
|
||||
}
|
||||
|
||||
static VkResult bind_dmabuf_buffer(struct vkp_image *img, int *fd, VkDeviceSize size) {
|
||||
VkExternalMemoryBufferCreateInfo ext = {
|
||||
.sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO,
|
||||
.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT};
|
||||
VkBufferCreateInfo bci = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .pNext = &ext, .size = size,
|
||||
.usage = img->blit_dst ? VK_BUFFER_USAGE_TRANSFER_DST_BIT : VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
||||
.sharingMode = VK_SHARING_MODE_EXCLUSIVE};
|
||||
VkResult r = g_vk.CreateBuffer(g_dev, &bci, NULL, &img->linear_buf);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
uint32_t allowed = 0xffffffff;
|
||||
if (g_vk.GetMemoryFdPropertiesKHR) {
|
||||
VkMemoryFdPropertiesKHR fp = {.sType = VK_STRUCTURE_TYPE_MEMORY_FD_PROPERTIES_KHR};
|
||||
if (g_vk.GetMemoryFdPropertiesKHR(g_dev, VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
|
||||
*fd, &fp) == VK_SUCCESS)
|
||||
allowed = fp.memoryTypeBits;
|
||||
}
|
||||
VkMemoryRequirements breq;
|
||||
g_vk.GetBufferMemoryRequirements(g_dev, img->linear_buf, &breq);
|
||||
int bidx = memory_type(breq.memoryTypeBits & allowed, 0);
|
||||
if (bidx < 0) return VK_ERROR_FORMAT_NOT_SUPPORTED;
|
||||
VkImportMemoryFdInfoKHR imp = {.sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR,
|
||||
.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, .fd = *fd};
|
||||
VkMemoryDedicatedAllocateInfo ded = {.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
|
||||
.pNext = &imp, .buffer = img->linear_buf};
|
||||
VkMemoryAllocateInfo mai = {.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .pNext = &ded,
|
||||
.allocationSize = breq.size, .memoryTypeIndex = (uint32_t)bidx};
|
||||
r = g_vk.AllocateMemory(g_dev, &mai, NULL, &img->linear_mem);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
*fd = -1;
|
||||
return g_vk.BindBufferMemory(g_dev, img->linear_buf, img->linear_mem, 0);
|
||||
}
|
||||
|
||||
static VkResult map_dmabuf_buffer(struct vkp_image *img, int *fd, VkDeviceSize size, VkDeviceSize need) {
|
||||
void *src = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED, *fd, 0);
|
||||
if (src == MAP_FAILED) {
|
||||
LOGE("dmabuf: mapping the linear %dx%d buffer failed (%s)", img->w, img->h, strerror(errno));
|
||||
return VK_ERROR_MEMORY_MAP_FAILED;
|
||||
}
|
||||
img->cpu_src = src;
|
||||
img->cpu_map_size = (size_t)size;
|
||||
img->cpu_size = (size_t)need;
|
||||
img->cpu_fd = *fd;
|
||||
*fd = -1;
|
||||
VkBufferCreateInfo bci = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .size = need,
|
||||
.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT, .sharingMode = VK_SHARING_MODE_EXCLUSIVE};
|
||||
VkResult r = g_vk.CreateBuffer(g_dev, &bci, NULL, &img->linear_buf);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
VkMemoryRequirements breq;
|
||||
g_vk.GetBufferMemoryRequirements(g_dev, img->linear_buf, &breq);
|
||||
int bidx = memory_type(breq.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
||||
if (bidx < 0) return VK_ERROR_FORMAT_NOT_SUPPORTED;
|
||||
VkMemoryAllocateInfo mai = {.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .allocationSize = breq.size,
|
||||
.memoryTypeIndex = (uint32_t)bidx};
|
||||
r = g_vk.AllocateMemory(g_dev, &mai, NULL, &img->linear_mem);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
r = g_vk.BindBufferMemory(g_dev, img->linear_buf, img->linear_mem, 0);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
r = g_vk.MapMemory(g_dev, img->linear_mem, 0, VK_WHOLE_SIZE, 0, &img->cpu_dst);
|
||||
if (r != VK_SUCCESS) return r;
|
||||
static int told;
|
||||
if (!told++)
|
||||
droiddeck_log("gpu", "dmabuf: the driver imports no dma-bufs; linear %dx%d frames are copied from their "
|
||||
"mapping (%llu bytes a frame) and uploaded at their row pitch",
|
||||
img->w, img->h, (unsigned long long)need);
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
|
||||
static int import_linear_dmabuf(struct vkp_image *img, int fd, const VkBufferImageCopy *copy, VkDeviceSize need) {
|
||||
int dupfd = dup(fd);
|
||||
if (dupfd < 0) return -1;
|
||||
@@ -1255,36 +1347,8 @@ static int import_linear_dmabuf(struct vkp_image *img, int fd, const VkBufferIma
|
||||
close(dupfd);
|
||||
return -1;
|
||||
}
|
||||
VkExternalMemoryBufferCreateInfo ext = {
|
||||
.sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_BUFFER_CREATE_INFO,
|
||||
.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT};
|
||||
VkBufferCreateInfo bci = {.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, .pNext = &ext, .size = size,
|
||||
.usage = img->blit_dst ? VK_BUFFER_USAGE_TRANSFER_DST_BIT : VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
|
||||
.sharingMode = VK_SHARING_MODE_EXCLUSIVE};
|
||||
VkResult r = g_vk.CreateBuffer(g_dev, &bci, NULL, &img->linear_buf);
|
||||
if (r != VK_SUCCESS) goto fail;
|
||||
uint32_t allowed = 0xffffffff;
|
||||
if (g_vk.GetMemoryFdPropertiesKHR) {
|
||||
VkMemoryFdPropertiesKHR fp = {.sType = VK_STRUCTURE_TYPE_MEMORY_FD_PROPERTIES_KHR};
|
||||
if (g_vk.GetMemoryFdPropertiesKHR(g_dev, VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT,
|
||||
dupfd, &fp) == VK_SUCCESS)
|
||||
allowed = fp.memoryTypeBits;
|
||||
}
|
||||
VkMemoryRequirements breq;
|
||||
g_vk.GetBufferMemoryRequirements(g_dev, img->linear_buf, &breq);
|
||||
int bidx = memory_type(breq.memoryTypeBits & allowed, 0);
|
||||
r = VK_ERROR_FORMAT_NOT_SUPPORTED;
|
||||
if (bidx < 0) goto fail;
|
||||
VkImportMemoryFdInfoKHR imp = {.sType = VK_STRUCTURE_TYPE_IMPORT_MEMORY_FD_INFO_KHR,
|
||||
.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT, .fd = dupfd};
|
||||
VkMemoryDedicatedAllocateInfo ded = {.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
|
||||
.pNext = &imp, .buffer = img->linear_buf};
|
||||
VkMemoryAllocateInfo mai = {.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .pNext = &ded,
|
||||
.allocationSize = breq.size, .memoryTypeIndex = (uint32_t)bidx};
|
||||
r = g_vk.AllocateMemory(g_dev, &mai, NULL, &img->linear_mem);
|
||||
if (r != VK_SUCCESS) goto fail;
|
||||
dupfd = -1;
|
||||
r = g_vk.BindBufferMemory(g_dev, img->linear_buf, img->linear_mem, 0);
|
||||
VkResult r = vkp_linear_dmabuf_mapped(g_dmabuf_import, img->blit_dst) ? map_dmabuf_buffer(img, &dupfd, size, need)
|
||||
: bind_dmabuf_buffer(img, &dupfd, size);
|
||||
if (r != VK_SUCCESS) goto fail;
|
||||
|
||||
VkImageCreateInfo ici = {
|
||||
@@ -1334,7 +1398,8 @@ struct vkp_image *vkp_image_import_dmabuf(int fd, uint32_t drm_format, uint64_t
|
||||
if (!img) return NULL;
|
||||
img->w = w; img->h = h; img->dmabuf = 1; img->blit_dst = as_blit_dst ? 1 : 0;
|
||||
img->fmt = format;
|
||||
const int through_buffer = padded_target || vkp_linear_dmabuf_through_buffer(g_drm_modifiers, modifier, as_blit_dst);
|
||||
const int through_buffer = padded_target ||
|
||||
vkp_linear_dmabuf_through_buffer(g_drm_modifiers && g_dmabuf_import, modifier, as_blit_dst);
|
||||
const VkImageUsageFlags sampled_usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
|
||||
const int modifier_sampled = !as_blit_dst && g_drm_modifiers && g_vk.GetPhysicalDeviceImageFormatProperties2 &&
|
||||
modifier_importable(img->fmt, modifier, sampled_usage);
|
||||
|
||||
@@ -67,6 +67,165 @@ int main(void) {
|
||||
}
|
||||
'''
|
||||
|
||||
MAPPED_HARNESS = r'''
|
||||
#define _GNU_SOURCE
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
#include <linux/dma-buf.h>
|
||||
#include <vulkan/vulkan.h>
|
||||
#include "vk_linear_dmabuf.h"
|
||||
|
||||
typedef struct AHardwareBuffer AHardwareBuffer;
|
||||
%(image)s
|
||||
|
||||
static void droiddeck_log(const char *tag, const char *fmt, ...) { (void)tag; (void)fmt; }
|
||||
#define LOGE(...) droiddeck_log("error", __VA_ARGS__)
|
||||
|
||||
static const VkDevice g_dev = (VkDevice)(uintptr_t)0x10;
|
||||
static const uint32_t g_qfam = 2;
|
||||
static VkPhysicalDeviceMemoryProperties g_memprops;
|
||||
static uint8_t staging[4096];
|
||||
static uint32_t allocated_type = 99;
|
||||
static int barriers, copies, freed_buffers, freed_memory;
|
||||
static VkBufferMemoryBarrier first_buffer_barrier;
|
||||
static uint32_t buffer_barrier_counts[4];
|
||||
|
||||
static void stub_barrier(VkCommandBuffer cmd, VkPipelineStageFlags src, VkPipelineStageFlags dst, VkDependencyFlags dep,
|
||||
uint32_t nm, const VkMemoryBarrier *m, uint32_t nb, const VkBufferMemoryBarrier *b,
|
||||
uint32_t ni, const VkImageMemoryBarrier *i) {
|
||||
(void)cmd; (void)src; (void)dst; (void)dep; (void)nm; (void)m; (void)ni; (void)i;
|
||||
if (barriers == 0 && nb) first_buffer_barrier = b[0];
|
||||
if (barriers < 4) buffer_barrier_counts[barriers] = nb;
|
||||
barriers++;
|
||||
}
|
||||
static void stub_copy(VkCommandBuffer cmd, VkBuffer b, VkImage i, VkImageLayout l, uint32_t n, const VkBufferImageCopy *r) {
|
||||
(void)cmd; (void)b; (void)i; (void)l; (void)n; (void)r;
|
||||
copies++;
|
||||
}
|
||||
static VkResult stub_create_buffer(VkDevice d, const VkBufferCreateInfo *ci, const VkAllocationCallbacks *a, VkBuffer *out) {
|
||||
(void)d; (void)a;
|
||||
assert(ci->pNext == NULL);
|
||||
assert(ci->usage == VK_BUFFER_USAGE_TRANSFER_SRC_BIT);
|
||||
*out = (VkBuffer)(uintptr_t)0x20;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
static void stub_buffer_reqs(VkDevice d, VkBuffer b, VkMemoryRequirements *r) {
|
||||
(void)d; (void)b;
|
||||
r->size = sizeof(staging); r->alignment = 64; r->memoryTypeBits = 0x3;
|
||||
}
|
||||
static VkResult stub_allocate(VkDevice d, const VkMemoryAllocateInfo *info, const VkAllocationCallbacks *a, VkDeviceMemory *out) {
|
||||
(void)d; (void)a;
|
||||
assert(info->pNext == NULL);
|
||||
allocated_type = info->memoryTypeIndex;
|
||||
*out = (VkDeviceMemory)(uintptr_t)0x30;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
static VkResult stub_bind(VkDevice d, VkBuffer b, VkDeviceMemory m, VkDeviceSize o) { (void)d; (void)b; (void)m; (void)o; return VK_SUCCESS; }
|
||||
static VkResult stub_map(VkDevice d, VkDeviceMemory m, VkDeviceSize o, VkDeviceSize s, VkMemoryMapFlags f, void **out) {
|
||||
(void)d; (void)m; (void)o; (void)s; (void)f;
|
||||
*out = staging;
|
||||
return VK_SUCCESS;
|
||||
}
|
||||
static void stub_destroy_image(VkDevice d, VkImage i, const VkAllocationCallbacks *a) { (void)d; (void)i; (void)a; }
|
||||
static void stub_destroy_buffer(VkDevice d, VkBuffer b, const VkAllocationCallbacks *a) { (void)d; (void)b; (void)a; freed_buffers++; }
|
||||
static void stub_free(VkDevice d, VkDeviceMemory m, const VkAllocationCallbacks *a) { (void)d; (void)m; (void)a; freed_memory++; }
|
||||
|
||||
static struct {
|
||||
PFN_vkCmdPipelineBarrier CmdPipelineBarrier;
|
||||
PFN_vkCmdCopyBufferToImage CmdCopyBufferToImage;
|
||||
PFN_vkCreateBuffer CreateBuffer;
|
||||
PFN_vkGetBufferMemoryRequirements GetBufferMemoryRequirements;
|
||||
PFN_vkAllocateMemory AllocateMemory;
|
||||
PFN_vkBindBufferMemory BindBufferMemory;
|
||||
PFN_vkMapMemory MapMemory;
|
||||
PFN_vkDestroyImage DestroyImage;
|
||||
PFN_vkDestroyBuffer DestroyBuffer;
|
||||
PFN_vkFreeMemory FreeMemory;
|
||||
} g_vk = {stub_barrier, stub_copy, stub_create_buffer, stub_buffer_reqs, stub_allocate, stub_bind, stub_map,
|
||||
stub_destroy_image, stub_destroy_buffer, stub_free};
|
||||
|
||||
%(memory_type)s
|
||||
%(copy)s
|
||||
%(stage)s
|
||||
%(destroy)s
|
||||
%(map)s
|
||||
|
||||
static void fill(int fd, uint8_t seed, size_t n) {
|
||||
uint8_t buf[4096];
|
||||
for (size_t i = 0; i < n; i++) buf[i] = (uint8_t)(seed + i * 7);
|
||||
assert(pwrite(fd, buf, n, 0) == (ssize_t)n);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
g_memprops.memoryTypeCount = 2;
|
||||
g_memprops.memoryTypes[0].propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
|
||||
g_memprops.memoryTypes[1].propertyFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
|
||||
|
||||
VkBufferImageCopy copy;
|
||||
const VkDeviceSize need = vkp_linear_dmabuf_copy(VK_FORMAT_B8G8R8A8_UNORM, 12, 8, 64, 0, ©);
|
||||
assert(need == 64 * 7 + 12 * 4);
|
||||
const size_t size = 1024;
|
||||
int fd = memfd_create("linear-frame", 0);
|
||||
assert(fd >= 0);
|
||||
assert(ftruncate(fd, (off_t)size) == 0);
|
||||
fill(fd, 1, size);
|
||||
|
||||
struct vkp_image img = {.w = 12, .h = 8, .dmabuf = 1, .image = (VkImage)(uintptr_t)0x40};
|
||||
int take = fd;
|
||||
assert(map_dmabuf_buffer(&img, &take, size, need) == VK_SUCCESS);
|
||||
assert(take == -1);
|
||||
assert(img.cpu_fd == fd);
|
||||
assert(img.cpu_size == need);
|
||||
assert(img.cpu_dst == staging);
|
||||
assert(allocated_type == 1);
|
||||
img.linear_copy = copy;
|
||||
img.staged = 1;
|
||||
|
||||
fill(fd, 9, size);
|
||||
uint8_t want[4096];
|
||||
assert(pread(fd, want, (size_t)need, 0) == (ssize_t)need);
|
||||
stage_linear_dmabuf((VkCommandBuffer)(uintptr_t)0x50, &img);
|
||||
assert(memcmp(staging, want, (size_t)need) == 0);
|
||||
assert(copies == 1 && barriers == 2);
|
||||
assert(first_buffer_barrier.srcQueueFamilyIndex == VK_QUEUE_FAMILY_IGNORED);
|
||||
assert(first_buffer_barrier.dstQueueFamilyIndex == VK_QUEUE_FAMILY_IGNORED);
|
||||
assert(buffer_barrier_counts[0] == 1 && buffer_barrier_counts[1] == 0);
|
||||
|
||||
destroy_image_objects(&img);
|
||||
assert(img.cpu_src == NULL && img.cpu_dst == NULL);
|
||||
assert(freed_buffers == 1 && freed_memory == 1);
|
||||
assert(fcntl(fd, F_GETFD) == -1 && errno == EBADF);
|
||||
|
||||
struct vkp_image imported = {.w = 12, .h = 8, .dmabuf = 1, .staged = 1, .image = (VkImage)(uintptr_t)0x41,
|
||||
.linear_buf = (VkBuffer)(uintptr_t)0x21, .linear_copy = copy};
|
||||
barriers = copies = 0;
|
||||
memset(staging, 0, sizeof(staging));
|
||||
stage_linear_dmabuf((VkCommandBuffer)(uintptr_t)0x50, &imported);
|
||||
assert(copies == 1 && barriers == 2);
|
||||
assert(first_buffer_barrier.srcQueueFamilyIndex == VK_QUEUE_FAMILY_FOREIGN_EXT);
|
||||
assert(first_buffer_barrier.dstQueueFamilyIndex == g_qfam);
|
||||
assert(buffer_barrier_counts[0] == 1 && buffer_barrier_counts[1] == 1);
|
||||
for (size_t i = 0; i < sizeof(staging); i++) assert(staging[i] == 0);
|
||||
|
||||
struct vkp_image unmappable = {.w = 12, .h = 8, .dmabuf = 1};
|
||||
int bad = open("/dev/null", O_WRONLY);
|
||||
assert(bad >= 0);
|
||||
int keep = bad;
|
||||
assert(map_dmabuf_buffer(&unmappable, &keep, size, need) == VK_ERROR_MEMORY_MAP_FAILED);
|
||||
assert(keep == bad);
|
||||
assert(unmappable.cpu_src == NULL && unmappable.linear_buf == VK_NULL_HANDLE);
|
||||
close(bad);
|
||||
return 0;
|
||||
}
|
||||
'''
|
||||
|
||||
|
||||
def compile_and_run(test, source, tmp, name):
|
||||
compiler = shutil.which(os.environ.get("CC", "cc"))
|
||||
@@ -151,6 +310,38 @@ class VkLinearDmabufTest(unittest.TestCase):
|
||||
for name in ("vkp_blit_image", "pass_finish"):
|
||||
self.assertIn("blit_to_layer(", function(source, name))
|
||||
|
||||
def test_driver_without_dmabuf_import_copies_from_the_mapping(self):
|
||||
vk = (SRC / "vk_present.c").read_text()
|
||||
image = re.search(r"^struct vkp_image \{.*?^\};", vk, re.M | re.S)
|
||||
self.assertIsNotNone(image, "struct vkp_image not found in vk_present.c")
|
||||
harness = MAPPED_HARNESS % {
|
||||
"image": image.group(0),
|
||||
"memory_type": function(vk, "memory_type"),
|
||||
"copy": function(vk, "copy_mapped_dmabuf"),
|
||||
"stage": function(vk, "stage_linear_dmabuf"),
|
||||
"destroy": function(vk, "destroy_image_objects"),
|
||||
"map": function(vk, "map_dmabuf_buffer"),
|
||||
}
|
||||
with tempfile.TemporaryDirectory(prefix="droiddeck-vk-linear-mapped-") as tmp:
|
||||
source = Path(tmp) / "mapped_dmabuf_test.c"
|
||||
source.write_text(harness)
|
||||
compile_and_run(self, source, tmp, "vk-linear-mapped")
|
||||
|
||||
def test_mapping_only_where_the_driver_imports_no_dmabufs(self):
|
||||
vk = (SRC / "vk_present.c").read_text()
|
||||
init = re.search(r"^\s+g_dmabuf_import = (.*?);", vk, re.M)
|
||||
self.assertIsNotNone(init, "g_dmabuf_import is never derived from the device extensions")
|
||||
terms = [t.strip() for t in init.group(1).split("&&")]
|
||||
self.assertEqual(terms, ['has_ext(exts, ne, "VK_KHR_external_memory_fd")',
|
||||
'has_ext(exts, ne, "VK_EXT_external_memory_dma_buf")'])
|
||||
self.assertIn("static int g_dmabuf_import = 1;", vk)
|
||||
body = function(vk, "import_linear_dmabuf")
|
||||
self.assertRegex(body, r"vkp_linear_dmabuf_mapped\(g_dmabuf_import, img->blit_dst\)\s*\?\s*map_dmabuf_buffer\(")
|
||||
self.assertIn("bind_dmabuf_buffer(img, &dupfd, size)", body)
|
||||
imp = function(vk, "vkp_image_import_dmabuf")
|
||||
self.assertIn("vkp_linear_dmabuf_through_buffer(g_drm_modifiers && g_dmabuf_import, modifier, as_blit_dst)", imp)
|
||||
self.assertIn("vkp_padded_linear_target_needs_layout(g_drm_modifiers, modifier, format, w, stride, offset)", imp)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -29,10 +29,13 @@ def function(text, name):
|
||||
|
||||
|
||||
HARNESS = r'''
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
typedef int VkResult;
|
||||
typedef uintptr_t VkFence;
|
||||
#define VK_TRUE 1
|
||||
@@ -69,7 +72,7 @@ static struct {
|
||||
typedef struct AHardwareBuffer AHardwareBuffer;
|
||||
static int released_buffers;
|
||||
static void AHardwareBuffer_release(AHardwareBuffer *b) { (void)b; ++released_buffers; }
|
||||
struct vkp_image { uintptr_t image, mem; void *map; AHardwareBuffer *android_buffer; uintptr_t linear_buf, linear_mem; int staged, in_general; };
|
||||
struct vkp_image { uintptr_t image, mem; void *map; AHardwareBuffer *android_buffer; uintptr_t linear_buf, linear_mem; int staged, in_general; const void *cpu_src; void *cpu_dst; size_t cpu_size, cpu_map_size; int cpu_fd; };
|
||||
static void blendp_forget_image(uintptr_t image) { (void)image; ++forgotten; }
|
||||
/* PRODUCTION_FUNCTIONS */
|
||||
static struct vkp_image *image(void) {
|
||||
|
||||
@@ -56,6 +56,27 @@ static void check_path_choice(void) {
|
||||
assert(!vkp_linear_dmabuf_through_buffer(1, linear, 1));
|
||||
}
|
||||
|
||||
static void check_mapped_choice(void) {
|
||||
assert(vkp_linear_dmabuf_mapped(0, 0));
|
||||
assert(!vkp_linear_dmabuf_mapped(1, 0));
|
||||
assert(!vkp_linear_dmabuf_mapped(0, 1));
|
||||
assert(!vkp_linear_dmabuf_mapped(1, 1));
|
||||
}
|
||||
|
||||
static void check_host_copy(void) {
|
||||
const VkBuffer buffer = (VkBuffer)(uintptr_t)0x1234;
|
||||
const VkBufferMemoryBarrier b = vkp_linear_dmabuf_host_copy(buffer);
|
||||
assert(b.sType == VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER);
|
||||
assert(b.pNext == NULL);
|
||||
assert(b.srcQueueFamilyIndex == VK_QUEUE_FAMILY_IGNORED);
|
||||
assert(b.dstQueueFamilyIndex == VK_QUEUE_FAMILY_IGNORED);
|
||||
assert(b.srcAccessMask == 0);
|
||||
assert(b.dstAccessMask == VK_ACCESS_TRANSFER_READ_BIT);
|
||||
assert(b.buffer == buffer);
|
||||
assert(b.offset == 0);
|
||||
assert(b.size == VK_WHOLE_SIZE);
|
||||
}
|
||||
|
||||
static void check_foreign_image(void) {
|
||||
assert(vkp_linear_dmabuf_foreign_image(1, 0));
|
||||
assert(!vkp_linear_dmabuf_foreign_image(1, 1));
|
||||
@@ -118,6 +139,8 @@ int main(void) {
|
||||
check_path_choice();
|
||||
check_padded_layer_targets();
|
||||
check_foreign_image();
|
||||
check_mapped_choice();
|
||||
check_host_copy();
|
||||
check_readback_order();
|
||||
check_ownership();
|
||||
return 0;
|
||||
|
||||
Reference in new issue
Block a user