Add YUV AHB import and sample end2end tests

... which triggers the device lost failures when aosp/3163776
is reverted.

Reviewed-by: Aaron Ruby <aruby@blackberry.com>
Acked-by: Yonggang Luo <luoyonggang@gmail.com>
Acked-by: Adam Jackson <ajax@redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/27246>
This commit is contained in:
Jason Macnak 2024-07-09 12:23:02 -07:00 committed by Marge Bot
parent 02b45e7e99
commit f240f5677a
9 changed files with 316 additions and 82 deletions

View file

@ -17,6 +17,7 @@
#include <cutils/log.h>
#include <optional>
#include <unordered_map>
#include "drm_fourcc.h"
@ -28,6 +29,18 @@ static constexpr int numInts = 1;
#define DRM_FORMAT_R8_BLOB fourcc_code('9', '9', '9', '9')
template <typename T, typename N>
T DivideRoundUp(T n, N divisor) {
const T div = static_cast<T>(divisor);
const T q = n / div;
return n % div == 0 ? q : q + 1;
}
template <typename T, typename N>
T Align(T number, N n) {
return DivideRoundUp(number, n) * n;
}
std::optional<uint32_t> GlFormatToDrmFormat(uint32_t glFormat) {
switch (glFormat) {
case kGlRGB:
@ -71,70 +84,204 @@ std::optional<uint32_t> AhbToDrmFormat(uint32_t ahbFormat) {
return DRM_FORMAT_ABGR16161616F;
case GFXSTREAM_AHB_FORMAT_R10G10B10A2_UNORM:
return DRM_FORMAT_ABGR2101010;
case GFXSTREAM_AHB_FORMAT_Y8Cb8Cr8_420:
return DRM_FORMAT_NV12;
}
return std::nullopt;
}
std::optional<uint32_t> DrmToAhbFormat(uint32_t drmFormat) {
switch (drmFormat) {
case DRM_FORMAT_ABGR8888:
return GFXSTREAM_AHB_FORMAT_R8G8B8A8_UNORM;
case DRM_FORMAT_XBGR8888:
return GFXSTREAM_AHB_FORMAT_R8G8B8X8_UNORM;
case DRM_FORMAT_ARGB8888:
return GFXSTREAM_AHB_FORMAT_B8G8R8A8_UNORM;
case DRM_FORMAT_BGR888:
return GFXSTREAM_AHB_FORMAT_R8G8B8_UNORM;
case DRM_FORMAT_RGB565:
return GFXSTREAM_AHB_FORMAT_R5G6B5_UNORM;
case DRM_FORMAT_R8_BLOB:
return GFXSTREAM_AHB_FORMAT_BLOB;
case DRM_FORMAT_R8:
return GFXSTREAM_AHB_FORMAT_R8_UNORM;
case DRM_FORMAT_YVU420:
return GFXSTREAM_AHB_FORMAT_YV12;
case DRM_FORMAT_ABGR16161616F:
return GFXSTREAM_AHB_FORMAT_R16G16B16A16_FLOAT;
case DRM_FORMAT_ABGR2101010:
return GFXSTREAM_AHB_FORMAT_R10G10B10A2_UNORM;
}
return std::nullopt;
}
std::optional<uint32_t> DrmToBpp(uint32_t drmFormat) {
switch (drmFormat) {
case DRM_FORMAT_ABGR8888:
case DRM_FORMAT_ARGB8888:
case DRM_FORMAT_XBGR8888:
return 4;
case DRM_FORMAT_BGR888:
return 3;
case DRM_FORMAT_RGB565:
return 2;
case DRM_FORMAT_R8_BLOB:
case DRM_FORMAT_R8:
return 1;
}
return std::nullopt;
}
std::optional<uint32_t> DrmToVirglFormat(uint32_t drmFormat) {
switch (drmFormat) {
case DRM_FORMAT_ABGR8888:
return VIRGL_FORMAT_R8G8B8A8_UNORM;
case DRM_FORMAT_ARGB8888:
return VIRGL_FORMAT_B8G8R8A8_UNORM;
case DRM_FORMAT_BGR888:
return VIRGL_FORMAT_R8G8B8_UNORM;
case DRM_FORMAT_BGR565:
return VIRGL_FORMAT_B5G6R5_UNORM;
case DRM_FORMAT_YVU420:
return VIRGL_FORMAT_YV12;
case DRM_FORMAT_R8:
case DRM_FORMAT_R8_BLOB:
return VIRGL_FORMAT_R8_UNORM;
}
return std::nullopt;
struct DrmFormatPlaneInfo {
uint32_t horizontalSubsampling;
uint32_t verticalSubsampling;
uint32_t bytesPerPixel;
};
struct DrmFormatInfo {
uint32_t androidFormat;
uint32_t virglFormat;
bool isYuv;
uint32_t horizontalAlignmentPixels;
uint32_t verticalAlignmentPixels;
std::vector<DrmFormatPlaneInfo> planes;
};
const std::unordered_map<uint32_t, DrmFormatInfo>& GetDrmFormatInfoMap() {
static const auto* kFormatInfoMap = new std::unordered_map<uint32_t, DrmFormatInfo>({
{DRM_FORMAT_ABGR8888,
{
.androidFormat = GFXSTREAM_AHB_FORMAT_R8G8B8A8_UNORM,
.virglFormat = VIRGL_FORMAT_R8G8B8A8_UNORM,
.isYuv = false,
.horizontalAlignmentPixels = 1,
.verticalAlignmentPixels = 1,
.planes =
{
{
.horizontalSubsampling = 1,
.verticalSubsampling = 1,
.bytesPerPixel = 4,
},
},
}},
{DRM_FORMAT_ARGB8888,
{
.androidFormat = GFXSTREAM_AHB_FORMAT_B8G8R8A8_UNORM,
.virglFormat = VIRGL_FORMAT_B8G8R8A8_UNORM,
.isYuv = false,
.horizontalAlignmentPixels = 1,
.verticalAlignmentPixels = 1,
.planes =
{
{
.horizontalSubsampling = 1,
.verticalSubsampling = 1,
.bytesPerPixel = 4,
},
},
}},
{DRM_FORMAT_BGR888,
{
.androidFormat = GFXSTREAM_AHB_FORMAT_R8G8B8_UNORM,
.virglFormat = VIRGL_FORMAT_R8G8B8_UNORM,
.isYuv = false,
.horizontalAlignmentPixels = 1,
.verticalAlignmentPixels = 1,
.planes =
{
{
.horizontalSubsampling = 1,
.verticalSubsampling = 1,
.bytesPerPixel = 3,
},
},
}},
{DRM_FORMAT_BGR565,
{
.androidFormat = GFXSTREAM_AHB_FORMAT_R5G6B5_UNORM,
.virglFormat = VIRGL_FORMAT_B5G6R5_UNORM,
.isYuv = false,
.horizontalAlignmentPixels = 1,
.verticalAlignmentPixels = 1,
.planes =
{
{
.horizontalSubsampling = 1,
.verticalSubsampling = 1,
.bytesPerPixel = 2,
},
},
}},
{DRM_FORMAT_R8,
{
.androidFormat = GFXSTREAM_AHB_FORMAT_R8_UNORM,
.virglFormat = VIRGL_FORMAT_R8_UNORM,
.isYuv = false,
.horizontalAlignmentPixels = 1,
.verticalAlignmentPixels = 1,
.planes =
{
{
.horizontalSubsampling = 1,
.verticalSubsampling = 1,
.bytesPerPixel = 1,
},
},
}},
{DRM_FORMAT_R8_BLOB,
{
.androidFormat = GFXSTREAM_AHB_FORMAT_BLOB,
.virglFormat = VIRGL_FORMAT_R8_UNORM,
.isYuv = false,
.horizontalAlignmentPixels = 1,
.verticalAlignmentPixels = 1,
.planes =
{
{
.horizontalSubsampling = 1,
.verticalSubsampling = 1,
.bytesPerPixel = 1,
},
},
}},
{DRM_FORMAT_ABGR16161616F,
{
.androidFormat = GFXSTREAM_AHB_FORMAT_R16G16B16A16_FLOAT,
.virglFormat = VIRGL_FORMAT_R16G16B16A16_FLOAT,
.isYuv = false,
.horizontalAlignmentPixels = 1,
.verticalAlignmentPixels = 1,
.planes =
{
{
.horizontalSubsampling = 1,
.verticalSubsampling = 1,
.bytesPerPixel = 8,
},
},
}},
{DRM_FORMAT_ABGR2101010,
{
.androidFormat = GFXSTREAM_AHB_FORMAT_R10G10B10A2_UNORM,
.virglFormat = VIRGL_FORMAT_R10G10B10A2_UNORM,
.isYuv = false,
.horizontalAlignmentPixels = 1,
.verticalAlignmentPixels = 1,
.planes =
{
{
.horizontalSubsampling = 1,
.verticalSubsampling = 1,
.bytesPerPixel = 4,
},
},
}},
{DRM_FORMAT_NV12,
{
.androidFormat = GFXSTREAM_AHB_FORMAT_Y8Cb8Cr8_420,
.virglFormat = VIRGL_FORMAT_NV12,
.isYuv = true,
.horizontalAlignmentPixels = 2,
.verticalAlignmentPixels = 1,
.planes =
{
{
.horizontalSubsampling = 1,
.verticalSubsampling = 1,
.bytesPerPixel = 1,
},
{
.horizontalSubsampling = 2,
.verticalSubsampling = 2,
.bytesPerPixel = 2,
},
},
}},
{DRM_FORMAT_YVU420,
{
.androidFormat = GFXSTREAM_AHB_FORMAT_YV12,
.virglFormat = VIRGL_FORMAT_YV12,
.isYuv = true,
.horizontalAlignmentPixels = 32,
.verticalAlignmentPixels = 1,
.planes =
{
{
.horizontalSubsampling = 1,
.verticalSubsampling = 1,
.bytesPerPixel = 1,
},
{
.horizontalSubsampling = 2,
.verticalSubsampling = 2,
.bytesPerPixel = 1,
},
{
.horizontalSubsampling = 2,
.verticalSubsampling = 2,
.bytesPerPixel = 1,
},
},
}},
});
return *kFormatInfoMap;
}
} // namespace
@ -152,12 +299,14 @@ uint32_t EmulatedAHardwareBuffer::getWidth() const { return mWidth; }
uint32_t EmulatedAHardwareBuffer::getHeight() const { return mHeight; }
int EmulatedAHardwareBuffer::getAndroidFormat() const {
auto ahbFormat = DrmToAhbFormat(mDrmFormat);
if (!ahbFormat) {
const auto& formatInfosMap = GetDrmFormatInfoMap();
auto formatInfoIt = formatInfosMap.find(mDrmFormat);
if (formatInfoIt == formatInfosMap.end()) {
ALOGE("Unhandled DRM format:%u", mDrmFormat);
return -1;
}
return *ahbFormat;
const auto& formatInfo = formatInfoIt->second;
return formatInfo.androidFormat;
}
uint32_t EmulatedAHardwareBuffer::getDrmFormat() const { return mDrmFormat; }
@ -199,6 +348,54 @@ int EmulatedAHardwareBuffer::lock(uint8_t** ptr) {
return 0;
}
int EmulatedAHardwareBuffer::lockPlanes(std::vector<Gralloc::LockedPlane>* ahbPlanes) {
uint8_t* data = 0;
int ret = lock(&data);
if (ret) {
return ret;
}
const auto& formatInfosMap = GetDrmFormatInfoMap();
auto formatInfoIt = formatInfosMap.find(mDrmFormat);
if (formatInfoIt == formatInfosMap.end()) {
ALOGE("Failed to lock: failed to find format info for drm format:%u", mDrmFormat);
return -1;
}
const auto& formatInfo = formatInfoIt->second;
const uint32_t alignedWidth = Align(mWidth, formatInfo.horizontalAlignmentPixels);
const uint32_t alignedHeight = Align(mHeight, formatInfo.verticalAlignmentPixels);
uint32_t cumulativeSize = 0;
for (const DrmFormatPlaneInfo& planeInfo : formatInfo.planes) {
const uint32_t planeWidth = DivideRoundUp(alignedWidth, planeInfo.horizontalSubsampling);
const uint32_t planeHeight = DivideRoundUp(alignedHeight, planeInfo.verticalSubsampling);
const uint32_t planeBpp = planeInfo.bytesPerPixel;
const uint32_t planeStrideBytes = planeWidth * planeBpp;
const uint32_t planeSizeBytes = planeHeight * planeStrideBytes;
ahbPlanes->emplace_back(Gralloc::LockedPlane{
.data = data + cumulativeSize,
.pixelStrideBytes = planeBpp,
.rowStrideBytes = planeStrideBytes,
});
cumulativeSize += planeSizeBytes;
}
if (mDrmFormat == DRM_FORMAT_NV12) {
const auto& uPlane = (*ahbPlanes)[1];
auto vPlane = uPlane;
vPlane.data += 1;
ahbPlanes->push_back(vPlane);
} else if (mDrmFormat == DRM_FORMAT_YVU420) {
// Note: lockPlanes() always returns Y, then U, then V but YV12 is Y, then V, then U.
auto& plane1 = (*ahbPlanes)[1];
auto& plane2 = (*ahbPlanes)[2];
std::swap(plane1, plane2);
}
return 0;
}
int EmulatedAHardwareBuffer::unlock() {
if (!mMapped) {
ALOGE("Failed to unlock EmulatedAHardwareBuffer: never locked?");
@ -216,9 +413,13 @@ uint32_t EmulatedGralloc::createColorBuffer(void*, int width, int height, uint32
auto drmFormat = GlFormatToDrmFormat(glFormat);
if (!drmFormat) {
ALOGE("Unhandled format");
return -1;
}
auto ahb = allocate(width, height, *drmFormat);
if (ahb == nullptr) {
return -1;
}
EmulatedAHardwareBuffer* rahb = reinterpret_cast<EmulatedAHardwareBuffer*>(ahb);
@ -254,24 +455,36 @@ AHardwareBuffer* EmulatedGralloc::allocate(uint32_t width, uint32_t height, uint
return nullptr;
}
auto virglFormat = DrmToVirglFormat(drmFormat);
if (!virglFormat) {
ALOGE("Failed to allocate: Unhandled DRM format:%u to Virgl format conversion.", drmFormat);
const auto& formatInfosMap = GetDrmFormatInfoMap();
auto formatInfoIt = formatInfosMap.find(drmFormat);
if (formatInfoIt == formatInfosMap.end()) {
ALOGE("Failed to allocate: failed to find format info for drm format:%u", drmFormat);
return nullptr;
}
const auto& formatInfo = formatInfoIt->second;
auto bpp = DrmToBpp(drmFormat);
if (!virglFormat) {
ALOGE("Failed to allocate: Unhandled DRM format:%u to bpp conversion.", drmFormat);
return nullptr;
const uint32_t alignedWidth = Align(width, formatInfo.horizontalAlignmentPixels);
const uint32_t alignedHeight = Align(height, formatInfo.verticalAlignmentPixels);
uint32_t stride = 0;
uint32_t size = 0;
for (uint32_t i = 0; i < formatInfo.planes.size(); i++) {
const DrmFormatPlaneInfo& planeInfo = formatInfo.planes[i];
const uint32_t planeWidth = DivideRoundUp(alignedWidth, planeInfo.horizontalSubsampling);
const uint32_t planeHeight = DivideRoundUp(alignedHeight, planeInfo.verticalSubsampling);
const uint32_t planeBpp = planeInfo.bytesPerPixel;
const uint32_t planeStrideBytes = planeWidth * planeBpp;
const uint32_t planeSizeBytes = planeHeight * planeStrideBytes;
size += planeSizeBytes;
if (i == 0) stride = planeStrideBytes;
}
const uint32_t bind =
(drmFormat == DRM_FORMAT_R8_BLOB) ? VIRGL_BIND_LINEAR : VIRGL_BIND_RENDER_TARGET;
const uint32_t stride = width * (*bpp);
const uint32_t bind = (drmFormat == DRM_FORMAT_R8_BLOB || drmFormat == DRM_FORMAT_NV12 ||
drmFormat == DRM_FORMAT_YVU420)
? VIRGL_BIND_LINEAR
: VIRGL_BIND_RENDER_TARGET;
auto resource =
device->createResource(width, height, stride, *virglFormat, PIPE_TEXTURE_2D, bind);
auto resource = device->createResource(width, height, stride, size, formatInfo.virglFormat,
PIPE_TEXTURE_2D, bind);
if (!resource) {
ALOGE("Failed to allocate: failed to create virtio resource.");
return nullptr;
@ -298,6 +511,11 @@ int EmulatedGralloc::lock(AHardwareBuffer* ahb, uint8_t** ptr) {
return rahb->lock(ptr);
}
int EmulatedGralloc::lockPlanes(AHardwareBuffer* ahb, std::vector<LockedPlane>* ahbPlanes) {
auto* rahb = reinterpret_cast<EmulatedAHardwareBuffer*>(ahb);
return rahb->lockPlanes(ahbPlanes);
}
int EmulatedGralloc::unlock(AHardwareBuffer* ahb) {
auto* rahb = reinterpret_cast<EmulatedAHardwareBuffer*>(ahb);
return rahb->unlock();

View file

@ -51,6 +51,7 @@ class EmulatedAHardwareBuffer {
void release();
int lock(uint8_t** ptr);
int lockPlanes(std::vector<Gralloc::LockedPlane>* ahbPlanes);
int unlock();
private:
@ -77,6 +78,7 @@ class EmulatedGralloc : public Gralloc {
void release(AHardwareBuffer* ahb) override;
int lock(AHardwareBuffer* ahb, uint8_t** ptr) override;
int lockPlanes(AHardwareBuffer* ahb, std::vector<LockedPlane>* ahbPlanes) override;
int unlock(AHardwareBuffer* ahb) override;
uint32_t getHostHandle(const native_handle_t* handle) override;

View file

@ -48,6 +48,10 @@ int GoldfishGralloc::lock(AHardwareBuffer* ahb, uint8_t** ptr) {
reinterpret_cast<void**>(ptr));
}
int GoldfishGralloc::lockPlanes(AHardwareBuffer* ahb, std::vector<LockedPlane>* ahbPlanes) {
return -1;
}
int GoldfishGralloc::unlock(AHardwareBuffer* ahb) { return AHardwareBuffer_unlock(ahb, nullptr); }
uint32_t GoldfishGralloc::getHostHandle(native_handle_t const* handle) {

View file

@ -29,6 +29,7 @@ class GoldfishGralloc : public Gralloc {
void release(AHardwareBuffer* ahb) override;
int lock(AHardwareBuffer* ahb, uint8_t** ptr) override;
int lockPlanes(AHardwareBuffer* ahb, std::vector<LockedPlane>* ahbPlanes) override;
int unlock(AHardwareBuffer* ahb) override;
uint32_t getHostHandle(native_handle_t const* handle) override;

View file

@ -181,12 +181,17 @@ int MinigbmGralloc::lock(AHardwareBuffer* ahb, uint8_t** ptr) {
reinterpret_cast<void**>(ptr));
}
int MinigbmGralloc::lockPlanes(AHardwareBuffer* ahb, std::vector<LockedPlane>* ahbPlanes) {
ALOGE("%s: unimplemented", __func__);
return -1;
}
int MinigbmGralloc::unlock(AHardwareBuffer* ahb) { return AHardwareBuffer_unlock(ahb, nullptr); }
uint32_t MinigbmGralloc::getHostHandle(const native_handle_t* handle) {
struct drm_virtgpu_resource_info info;
if (!getVirtioGpuResourceInfo(m_fd, handle, &info)) {
ALOGE("%s: failed to get resource info\n", __func__);
ALOGE("%s: failed to get resource info", __func__);
return 0;
}

View file

@ -29,6 +29,7 @@ class MinigbmGralloc : public Gralloc {
void release(AHardwareBuffer* ahb) override;
int lock(AHardwareBuffer* ahb, uint8_t** ptr) override;
int lockPlanes(AHardwareBuffer* ahb, std::vector<LockedPlane>* ahbPlanes) override;
int unlock(AHardwareBuffer* ahb) override;
uint32_t getHostHandle(native_handle_t const* handle) override;

View file

@ -22,11 +22,13 @@
#include "virtgpu_gfxstream_protocol.h"
// See virgl_hw.h and p_defines.h
#define VIRGL_FORMAT_R8_UNORM 64
#define VIRGL_FORMAT_B8G8R8A8_UNORM 1
#define VIRGL_FORMAT_B5G6R5_UNORM 7
#define VIRGL_FORMAT_R10G10B10A2_UNORM 8
#define VIRGL_FORMAT_R8_UNORM 64
#define VIRGL_FORMAT_R8G8B8_UNORM 66
#define VIRGL_FORMAT_R8G8B8A8_UNORM 67
#define VIRGL_FORMAT_R16G16B16A16_FLOAT 94
#define VIRGL_FORMAT_YV12 163
#define VIRGL_FORMAT_YV16 164
#define VIRGL_FORMAT_IYUV 165
@ -191,7 +193,7 @@ class VirtGpuDevice {
virtual VirtGpuResourcePtr createBlob(const struct VirtGpuCreateBlob& blobCreate) = 0;
virtual VirtGpuResourcePtr createResource(uint32_t width, uint32_t height, uint32_t stride,
uint32_t virglFormat, uint32_t target,
uint32_t size, uint32_t virglFormat, uint32_t target,
uint32_t bind) = 0;
virtual VirtGpuResourcePtr importBlob(const struct VirtGpuExternalHandle& handle) = 0;

View file

@ -69,7 +69,7 @@ class LinuxVirtGpuDevice : public VirtGpuDevice {
VirtGpuResourcePtr createBlob(const struct VirtGpuCreateBlob& blobCreate) override;
VirtGpuResourcePtr createResource(uint32_t width, uint32_t height, uint32_t stride,
uint32_t virglFormat, uint32_t target,
uint32_t size, uint32_t virglFormat, uint32_t target,
uint32_t bind) override;
virtual VirtGpuResourcePtr importBlob(const struct VirtGpuExternalHandle& handle);

View file

@ -154,8 +154,9 @@ struct VirtGpuCaps LinuxVirtGpuDevice::getCaps(void) { return mCaps; }
int64_t LinuxVirtGpuDevice::getDeviceHandle(void) { return mDeviceHandle; }
VirtGpuResourcePtr LinuxVirtGpuDevice::createResource(uint32_t width, uint32_t height,
uint32_t stride, uint32_t virglFormat,
uint32_t target, uint32_t bind) {
uint32_t stride, uint32_t size,
uint32_t virglFormat, uint32_t target,
uint32_t bind) {
drm_virtgpu_resource_create create = {
.target = target,
.format = virglFormat,
@ -166,7 +167,7 @@ VirtGpuResourcePtr LinuxVirtGpuDevice::createResource(uint32_t width, uint32_t h
.array_size = 1U,
.last_level = 0,
.nr_samples = 0,
.size = stride * height,
.size = size,
.stride = stride,
};