waylandcomp: import layer pool buffers as AHardwareBuffers off Adreno

A non-QTI gralloc handle (Arm/MediaTek gralloc on Mali) has no layout
the compositor can read, so the pool import guessed a linear dma-buf and
failed. On drivers other than Qualcomm's the compositor device enables
VK_ANDROID_external_memory_android_hardware_buffer and
VK_EXT_queue_family_foreign and imports such buffers as AHardwareBuffers;
the linear dma-buf stays as the last resort. Adreno/Turnip keeps the
same device and the dma-buf path.
This commit is contained in:
Ahmad Sayed Alsayed Ebrahim authored and maxjivi05 committed 2026-10-26 13:55:18 -04:00
1 parent c883ce4439
commit 3943794b0e
3 files changed
+178 -21

No files matched your search

+72 -19
View File
@@ -381,6 +381,26 @@ static int sniff_modifier(const struct droiddeck_native_handle *h, uint64_t *mod
return 1;
}
#define SC_MOD_GRALLOC (~0ULL - 1)
static const char *pool_kind(uint64_t mod) {
return mod == MOD_QCOM_COMPRESSED ? "UBWC" : mod == SC_MOD_GRALLOC ? "gralloc-laid-out" : "linear";
}
static void describe_handle(const struct droiddeck_native_handle *h, char *out, size_t n) {
size_t o = 0;
out[0] = 0;
if (!h) { snprintf(out, n, "no handle"); return; }
for (int i = 0; i < h->numFds && i < 4 && o < n; i++) {
off_t sz = lseek(h->data[i], 0, SEEK_END);
if (sz >= 0) lseek(h->data[i], 0, SEEK_SET);
o += (size_t)snprintf(out + o, n - o, "%sfd%d %lld bytes", i ? ", " : "", i, (long long)sz);
}
if (o < n) o += (size_t)snprintf(out + o, n - o, "; ints");
for (int i = 0; i < h->numInts && i < 8 && o < n; i++)
o += (size_t)snprintf(out + o, n - o, " %#x", (unsigned)h->data[h->numFds + i]);
}
static int g_alloc_failed;
enum { TIER_UBWC, TIER_PLAIN, TIER_LINEAR };
@@ -416,22 +436,54 @@ static int alloc_slot(struct layer *l, struct slot *s, int w, int h, uint32_t fm
AHardwareBuffer_Desc got; AHardwareBuffer_describe(ahb, &got);
const struct droiddeck_native_handle *nh = api.getNativeHandle(ahb);
uint64_t mod = MOD_LINEAR;
if (!sniff_modifier(nh, &mod)) {
if (tier < TIER_LINEAR) {
struct vkp_image *img = NULL;
const char *path;
char info[160] = "";
if (sniff_modifier(nh, &mod)) {
img = vkp_image_import_dmabuf(nh->data[0], fmt == AHB_RGB10A2 ? DRM_ABGR2101010 : DRM_ABGR8888, mod, w, h,
got.stride * 4, 0, 1);
path = "dma-buf (QTI handle)";
} else {
if (tier == TIER_UBWC) {
char hd[256];
describe_handle(nh, hd, sizeof(hd));
droiddeck_log("layer", "%s: gralloc handle is not a QTI one (%d fds, %d ints: %s): no UBWC request; "
"pool buffers are imported %s", l->name, nh ? nh->numFds : -1, nh ? nh->numInts : -1, hd,
vkp_can_import_ahb() ? "as AHardwareBuffers (the driver takes gralloc's own layout)"
: "as linear dma-bufs (this driver has no AHardwareBuffer import)");
AHardwareBuffer_release(ahb);
l->alloc_tier = TIER_LINEAR;
droiddeck_log("layer", "%s: gralloc handle layout unknown (%d fds, %d ints): using linear pool buffers",
l->name, nh ? nh->numFds : -1, nh ? nh->numInts : -1);
l->alloc_tier = vkp_can_import_ahb() ? TIER_PLAIN : TIER_LINEAR;
continue;
}
mod = MOD_LINEAR;
mod = SC_MOD_GRALLOC;
path = "AHardwareBuffer";
img = vkp_image_import_ahb(ahb, info, sizeof(info));
if (!img && tier == TIER_LINEAR) {
const off_t need = (off_t)got.stride * 4 * h;
int fd = -1, which = -1;
for (int i = 0; nh && i < nh->numFds; i++) {
off_t sz = lseek(nh->data[i], 0, SEEK_END);
if (sz >= 0) lseek(nh->data[i], 0, SEEK_SET);
if (sz >= need) { fd = nh->data[i]; which = i; break; }
}
if (fd < 0 && nh && nh->numFds > 0) { fd = nh->data[0]; which = 0; }
mod = MOD_LINEAR;
path = "linear dma-buf";
img = fd >= 0 ? vkp_image_import_dmabuf(fd, fmt == AHB_RGB10A2 ? DRM_ABGR2101010 : DRM_ABGR8888, mod,
w, h, got.stride * 4, 0, 1) : NULL;
snprintf(info, sizeof(info), "handle fd %d of %d", which, nh ? nh->numFds : 0);
if (vkp_can_import_ahb())
droiddeck_log("layer", "%s: AHardwareBuffer import failed: the linear dma-buf in the handle's fd %d "
"(of %d) -> %s", l->name, which, nh ? nh->numFds : 0, img ? "imported" : "failed too");
}
}
int fd = (nh && nh->numFds > 0) ? nh->data[0] : -1;
struct vkp_image *img = fd >= 0 ? vkp_image_import_dmabuf(fd, fmt == AHB_RGB10A2 ? DRM_ABGR2101010 : DRM_ABGR8888,
mod, w, h, got.stride * 4, 0, 1) : NULL;
if (!img) {
droiddeck_log("layer", "%s: import of a %s %dx%d pool buffer (stride %u px) into the compositor's Turnip failed",
l->name, mod == MOD_QCOM_COMPRESSED ? "UBWC" : "linear", w, h, got.stride);
char hd[256];
describe_handle(nh, hd, sizeof(hd));
droiddeck_log("layer", "%s: import of a %s %dx%d pool buffer (stride %u px, %s, usage %#llx) into the "
"compositor's Turnip failed via %s; gralloc handle %d fds / %d ints: %s", l->name,
pool_kind(mod), w, h, got.stride, tier_name(tier), (unsigned long long)got.usage, path,
nh ? nh->numFds : -1, nh ? nh->numInts : -1, hd);
AHardwareBuffer_release(ahb);
if (tier < TIER_LINEAR) { l->alloc_tier = tier + 1; continue; }
g_alloc_failed = 1;
@@ -442,10 +494,11 @@ static int alloc_slot(struct layer *l, struct slot *s, int w, int h, uint32_t fm
if (l->logged_w != w || l->logged_h != h || l->logged_fmt != fmt || l->logged_mod != mod ||
l->logged_tier != tier) {
l->logged_w = w; l->logged_h = h; l->logged_fmt = fmt; l->logged_mod = mod; l->logged_tier = tier;
droiddeck_log("layer", "%s: pool buffers %dx%d%s: %s (%s), stride %u px, up to %d buffers (gralloc handle %d fds / %d ints)",
l->name, w, h, fmt == AHB_RGB10A2 ? " 10-bit RGBA1010102" : "",
mod == MOD_QCOM_COMPRESSED ? "UBWC (QCOM_COMPRESSED)" : "linear", tier_name(tier),
got.stride, l->pool_n, nh ? nh->numFds : -1, nh ? nh->numInts : -1);
droiddeck_log("layer", "%s: pool buffers %dx%d%s: %s (%s), stride %u px, up to %d buffers (gralloc handle %d fds / "
"%d ints), imported as %s%s%s%s", l->name, w, h, fmt == AHB_RGB10A2 ? " 10-bit RGBA1010102" : "",
mod == MOD_QCOM_COMPRESSED ? "UBWC (QCOM_COMPRESSED)" : pool_kind(mod), tier_name(tier),
got.stride, l->pool_n, nh ? nh->numFds : -1, nh ? nh->numInts : -1, path,
info[0] ? " (" : "", info, info[0] ? ")" : "");
}
return 0;
}
@@ -814,7 +867,7 @@ int sc_layer_present(struct vkp_image *src, int scene_w, int scene_h, const stru
l->first_logged = 1;
droiddeck_log("layer", "presenting %dx%d game frames on their own SurfaceControl layer (%s pool, %d buffers); "
"HUD and pointer stay Android views above it", sw, sh,
l->pool_modifier == MOD_QCOM_COMPRESSED ? "UBWC" : "linear", l->pool_n);
pool_kind(l->pool_modifier), l->pool_n);
}
vkp_signal_first_frame();
return 0;
@@ -836,7 +889,7 @@ int sc_layer_present_pass(const struct vkp_draw *draws, int n, int scene_w, int
l->first_logged = 1;
droiddeck_log("layer", "presenting %dx%d frames on their own SurfaceControl layer (%s pool, %d buffers); "
"HUD and pointer stay Android views above it", rw, rh,
l->pool_modifier == MOD_QCOM_COMPRESSED ? "UBWC" : "linear", l->pool_n);
pool_kind(l->pool_modifier), l->pool_n);
}
vkp_signal_first_frame();
return 0;
@@ -874,12 +927,12 @@ int sc_layer_present_hdr_scene(const struct vkp_draw *draws, int n, const struct
if (tm)
droiddeck_log("color", "tone-mapped picture on the game's display layer: %dx%d, %s 8-bit buffers, untagged "
"(sRGB) - HDR output is switched off", rw, rh,
l->pool_modifier == MOD_QCOM_COMPRESSED ? "UBWC" : "linear");
pool_kind(l->pool_modifier));
else {
char cov[96];
coverage_text(l, cov, sizeof(cov));
droiddeck_log("color", "HDR picture on its own display layer: %dx%d, %s %s buffers, tagged %s%s%s", rw, rh,
l->pool_modifier == MOD_QCOM_COMPRESSED ? "UBWC" : "linear",
pool_kind(l->pool_modifier),
g_hdr_pool_fmt == AHB_RGB10A2 ? "10-bit" : "8-bit",
color && color->dataspace == DROIDDECK_ADATASPACE_BT2020_PQ ? "BT2020_PQ" : "HDR",
cov[0] ? ", " : "", cov);
+102 -2
View File
@@ -94,6 +94,9 @@ static int g_black_state;
static PFN_vkImportSemaphoreFdKHR g_import_sem_fd;
static VkSemaphore g_wait_sem;
#define VKP_VENDOR_QUALCOMM 0x5143
static uint32_t g_vendor_id;
static struct vkp_perf g_perf;
static int64_t perf_now(void) {
struct timespec ts;
@@ -454,6 +457,7 @@ static int dev_init(void) {
{
VkPhysicalDeviceProperties props;
g_vk.GetPhysicalDeviceProperties(g_pd, &props);
g_vendor_id = props.vendorID;
snprintf(g_gpu_name, sizeof(g_gpu_name), "%s", props.deviceName);
droiddeck_log("gpu", "compositor renders on %s with %s", props.deviceName,
g_library_name ? g_library_name : "the system Vulkan driver");
@@ -984,8 +988,9 @@ struct vkp_image *vkp_image_import_dmabuf(int fd, uint32_t drm_format, uint64_t
.allocationSize = req.size, .memoryTypeIndex = (uint32_t)idx};
VkResult mr = g_vk.AllocateMemory(g_dev, &mai, NULL, &img->mem);
if (mr != VK_SUCCESS) {
LOGE("%s: importing the %s dma-buf (%dx%d, %llu bytes) -> %s", as_blit_dst ? "layer" : "dmabuf",
vkp_modifier_name(modifier), w, h, (unsigned long long)req.size, vk_result_name(mr));
LOGE("%s: importing the %s dma-buf (%dx%d, pitch %u, offset %u, %llu bytes, memory type %d of image types "
"%#x & fd types %#x) -> %s", as_blit_dst ? "layer" : "dmabuf", vkp_modifier_name(modifier), w, h,
stride, offset, (unsigned long long)req.size, idx, req.memoryTypeBits, allowed, vk_result_name(mr));
g_vk.DestroyImage(g_dev, img->image, NULL); close(dupfd); free(img); return NULL;
}
VkResult br = g_vk.BindImageMemory(g_dev, img->image, img->mem, 0);
@@ -1052,6 +1057,101 @@ struct vkp_image *vkp_image_from_ahb(AHardwareBuffer *ahb) {
return img;
}
int vkp_can_import_ahb(void) {
return dev_init() == 0 && g_vendor_id != VKP_VENDOR_QUALCOMM && g_ahb_import &&
g_vk.GetAndroidHardwareBufferPropertiesANDROID;
}
struct vkp_image *vkp_image_import_ahb(struct AHardwareBuffer *ahb, char *info, size_t info_n) {
if (info && info_n) info[0] = 0;
if (!ahb || !vkp_can_import_ahb()) return NULL;
AHardwareBuffer_Desc d;
AHardwareBuffer_describe(ahb, &d);
VkAndroidHardwareBufferFormatPropertiesANDROID fp = {
.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_FORMAT_PROPERTIES_ANDROID};
VkAndroidHardwareBufferPropertiesANDROID hp = {
.sType = VK_STRUCTURE_TYPE_ANDROID_HARDWARE_BUFFER_PROPERTIES_ANDROID, .pNext = &fp};
VkResult pr = g_vk.GetAndroidHardwareBufferPropertiesANDROID(g_dev, ahb, &hp);
if (pr != VK_SUCCESS) {
LOGE("layer: vkGetAndroidHardwareBufferPropertiesANDROID(%ux%u, AHB format %#x, usage %#llx) -> %s", d.width,
d.height, d.format, (unsigned long long)d.usage, vk_result_name(pr));
return NULL;
}
if (fp.format == VK_FORMAT_UNDEFINED) {
LOGE("layer: the driver reports the %ux%u AHB (format %#x) as external format %#llx only: no blit target",
d.width, d.height, d.format, (unsigned long long)fp.externalFormat);
return NULL;
}
const VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT;
if (g_vk.GetPhysicalDeviceImageFormatProperties2) {
VkPhysicalDeviceExternalImageFormatInfo ext = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTERNAL_IMAGE_FORMAT_INFO,
.handleType = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID};
VkPhysicalDeviceImageFormatInfo2 qi = {
.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_IMAGE_FORMAT_INFO_2, .pNext = &ext, .format = fp.format,
.type = VK_IMAGE_TYPE_2D, .tiling = VK_IMAGE_TILING_OPTIMAL, .usage = usage};
VkExternalImageFormatProperties extp = {.sType = VK_STRUCTURE_TYPE_EXTERNAL_IMAGE_FORMAT_PROPERTIES};
VkImageFormatProperties2 ip = {.sType = VK_STRUCTURE_TYPE_IMAGE_FORMAT_PROPERTIES_2, .pNext = &extp};
VkResult qr = g_vk.GetPhysicalDeviceImageFormatProperties2(g_pd, &qi, &ip);
if (qr != VK_SUCCESS ||
!(extp.externalMemoryProperties.externalMemoryFeatures & VK_EXTERNAL_MEMORY_FEATURE_IMPORTABLE_BIT)) {
LOGE("layer: VkFormat %d as an imported AHB blit target is not supported (%s, external features %#x)",
(int)fp.format, vk_result_name(qr), extp.externalMemoryProperties.externalMemoryFeatures);
return NULL;
}
}
struct vkp_image *img = calloc(1, sizeof(*img));
if (!img) return NULL;
img->w = (int)d.width; img->h = (int)d.height;
img->dmabuf = 1; img->blit_dst = 1;
img->fmt = fp.format;
VkExternalMemoryImageCreateInfo extImg = {
.sType = VK_STRUCTURE_TYPE_EXTERNAL_MEMORY_IMAGE_CREATE_INFO,
.handleTypes = VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID};
VkImageCreateInfo ici = {
.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, .pNext = &extImg,
.imageType = VK_IMAGE_TYPE_2D, .format = fp.format, .extent = {d.width, d.height, 1},
.mipLevels = 1, .arrayLayers = 1, .samples = VK_SAMPLE_COUNT_1_BIT,
.tiling = VK_IMAGE_TILING_OPTIMAL,
.usage = usage, .sharingMode = VK_SHARING_MODE_EXCLUSIVE, .initialLayout = VK_IMAGE_LAYOUT_UNDEFINED};
VkResult cr = g_vk.CreateImage(g_dev, &ici, NULL, &img->image);
if (cr != VK_SUCCESS) {
LOGE("layer: vkCreateImage(AHB %ux%u, VkFormat %d) -> %s", d.width, d.height, (int)fp.format,
vk_result_name(cr));
free(img); return NULL;
}
int idx = -1;
for (int i = 0; i < 32; i++) if (hp.memoryTypeBits & (1u << i)) { idx = i; break; }
if (idx < 0) {
LOGE("layer: no memory type can import the %ux%u AHB (memoryTypeBits 0)", d.width, d.height);
g_vk.DestroyImage(g_dev, img->image, NULL); free(img); return NULL;
}
VkImportAndroidHardwareBufferInfoANDROID imp = {
.sType = VK_STRUCTURE_TYPE_IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID, .buffer = ahb};
VkMemoryDedicatedAllocateInfo ded = {.sType = VK_STRUCTURE_TYPE_MEMORY_DEDICATED_ALLOCATE_INFO,
.pNext = &imp, .image = img->image};
VkMemoryAllocateInfo mai = {.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO, .pNext = &ded,
.allocationSize = hp.allocationSize, .memoryTypeIndex = (uint32_t)idx};
VkResult mr = g_vk.AllocateMemory(g_dev, &mai, NULL, &img->mem);
if (mr != VK_SUCCESS) {
LOGE("layer: importing the %ux%u AHB (VkFormat %d, %llu bytes, memory type %d of memoryTypeBits %#x) -> %s",
d.width, d.height, (int)fp.format, (unsigned long long)hp.allocationSize, idx, hp.memoryTypeBits,
vk_result_name(mr));
g_vk.DestroyImage(g_dev, img->image, NULL); free(img); return NULL;
}
VkResult br = g_vk.BindImageMemory(g_dev, img->image, img->mem, 0);
if (br != VK_SUCCESS) {
LOGE("layer: vkBindImageMemory(AHB) -> %s", vk_result_name(br));
g_vk.FreeMemory(g_dev, img->mem, NULL); g_vk.DestroyImage(g_dev, img->image, NULL);
free(img); return NULL;
}
if (info && info_n)
snprintf(info, info_n, "VkFormat %d, %llu bytes, memory type %d of memoryTypeBits %#x", (int)fp.format,
(unsigned long long)hp.allocationSize, idx, hp.memoryTypeBits);
return img;
}
static int shm_sampled(void) {
static int known;
if (!known) {
@@ -1,6 +1,7 @@
#ifndef VK_PRESENT_H
#define VK_PRESENT_H
#include <stdint.h>
#include <stddef.h>
#include <android/native_window.h>
void vk_present_set_driver(const char *driver_path, const char *library_name,
const char *native_lib_dir);
@@ -32,6 +33,9 @@ struct vkp_image *vkp_image_import_dmabuf(int fd, uint32_t drm_format, uint64_t
int w, int h, uint32_t stride, uint32_t offset,
int as_blit_dst);
int vkp_image_is_dmabuf(const struct vkp_image *img);
struct AHardwareBuffer;
int vkp_can_import_ahb(void);
struct vkp_image *vkp_image_import_ahb(struct AHardwareBuffer *ahb, char *info, size_t info_n);
struct vkp_image *vkp_image_create_shm(int w, int h);
struct AHardwareBuffer;