Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ivi diff v8 #109

Merged
merged 16 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 45 additions & 103 deletions cros_gralloc/cros_gralloc_driver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ cros_gralloc_driver::cros_gralloc_driver()
int fd;
drmVersionPtr version;
const int render_num = 10;
const int name_length = 50;
int node_fd[render_num];
char *node_name[render_num] = {};
int availabe_node = 0;
Expand All @@ -121,16 +122,6 @@ cros_gralloc_driver::cros_gralloc_driver()
mt8183_camera_quirk_ = !strncmp(buf, "kukui", strlen("kukui"));

// destroy drivers if exist before re-initializing them
if (drv_kms_) {
int fd = drv_get_fd(drv_kms_);
drv_destroy(drv_kms_);
if (!is_kmsro_enabled()) {
drv_render_ = nullptr;
}
drv_kms_ = nullptr;
close(fd);
}

if (drv_render_) {
int fd = drv_get_fd(drv_render_);
drv_destroy(drv_render_);
Expand Down Expand Up @@ -172,7 +163,7 @@ cros_gralloc_driver::cros_gralloc_driver()
node_fd[availabe_node] = fd;
int len = snprintf(NULL, 0, "%s", version->name);
node_name[availabe_node] = (char *)malloc(len + 1);
strcpy(node_name[availabe_node], version->name);
strncpy(node_name[availabe_node], version->name, len + 1);
availabe_node++;

drmFreeVersion(version);
Expand All @@ -184,61 +175,42 @@ cros_gralloc_driver::cros_gralloc_driver()
if (!drv_render_) {
drv_loge("Failed to create driver for the 1st device\n");
close(node_fd[0]);
} else {
switch (availabe_node) {
// only have one render node, is GVT-d/BM/VirtIO
case 1:
if (drv_render_) {
drv_kms_ = drv_render_;
} else
break;
gpu_grp_type = (virtio_node_idx != -1)? ONE_GPU_VIRTIO: ONE_GPU_INTEL;
break;
// is SR-IOV or iGPU + dGPU
case 2:
if (virtio_node_idx != -1) {
drv_kms_ = drv_create(node_fd[virtio_node_idx]);
if (!drv_kms_) {
drv_loge("Failed to create driver for virtio device\n");
close(node_fd[virtio_node_idx]);
break;
}
gpu_grp_type = TWO_GPU_IGPU_VIRTIO;
} else {
close(node_fd[1]);
drv_kms_ = drv_render_;
gpu_grp_type = TWO_GPU_IGPU_DGPU;
}
break;
// is SR-IOV + dGPU
case 3:
if (!strcmp(node_name[1], "i915")) {
close(node_fd[1]);
}
if (virtio_node_idx != -1) {
drv_kms_ = drv_create(node_fd[virtio_node_idx]);
if (!drv_kms_) {
drv_loge("Failed to create driver for virtio device\n");
close(node_fd[virtio_node_idx]);
break;
}
}
gpu_grp_type = THREE_GPU_IGPU_VIRTIO_DGPU;
// TO-DO: the 3rd node is i915 or others.
break;
}
switch (availabe_node) {
// only have one render node, is GVT-d/BM/VirtIO
case 1:
if (!drv_render_) {
fd = drv_get_fd(drv_render_);
drv_destroy(drv_render_);
close(fd);
drv_render_ = nullptr;
}

if (drv_render_) {
if (drv_init(drv_render_, gpu_grp_type)) {
drv_loge("Failed to init render driver\n");
}
gpu_grp_type = (virtio_node_idx != -1)? ONE_GPU_VIRTIO: ONE_GPU_INTEL;
break;
// is SR-IOV or iGPU + dGPU
case 2:
close(node_fd[1]);
if (virtio_node_idx != -1) {
gpu_grp_type = TWO_GPU_IGPU_VIRTIO;
} else {
gpu_grp_type = TWO_GPU_IGPU_DGPU;
}

if (drv_kms_ && (drv_kms_ != drv_render_)) {
if (drv_init(drv_kms_, gpu_grp_type)) {
drv_loge("Failed to init kms driver\n");
}
break;
// is SR-IOV + dGPU
case 3:
if (!strcmp(node_name[1], "i915")) {
close(node_fd[1]);
}
if (virtio_node_idx != -1) {
close(node_fd[virtio_node_idx]);
}
gpu_grp_type = THREE_GPU_IGPU_VIRTIO_DGPU;
// TO-DO: the 3rd node is i915 or others.
break;
}

if (drv_render_) {
drv_init(drv_render_, gpu_grp_type);
}
}

Expand All @@ -252,16 +224,6 @@ cros_gralloc_driver::~cros_gralloc_driver()
buffers_.clear();
handles_.clear();

if (drv_kms_) {
int fd = drv_get_fd(drv_kms_);
drv_destroy(drv_kms_);
if (!is_kmsro_enabled()) {
drv_render_ = nullptr;
}
drv_kms_ = nullptr;
close(fd);
}

if (drv_render_) {
int fd = drv_get_fd(drv_render_);
drv_destroy(drv_render_);
Expand All @@ -273,7 +235,7 @@ cros_gralloc_driver::~cros_gralloc_driver()

bool cros_gralloc_driver::is_initialized()
{
return (drv_render_ != nullptr && drv_kms_ != nullptr);
return (drv_render_ != nullptr);
}

bool cros_gralloc_driver::get_resolved_format_and_use_flags(
Expand All @@ -284,7 +246,7 @@ bool cros_gralloc_driver::get_resolved_format_and_use_flags(
uint64_t resolved_use_flags;
struct combination *combo;

struct driver *drv = (descriptor->use_flags & BO_USE_SCANOUT) ? drv_kms_ : drv_render_;
struct driver *drv = drv_render_;
if (mt8183_camera_quirk_ && (descriptor->use_flags & BO_USE_CAMERA_READ) &&
!(descriptor->use_flags & BO_USE_SCANOUT) &&
descriptor->drm_format == DRM_FORMAT_FLEX_IMPLEMENTATION_DEFINED) {
Expand All @@ -298,21 +260,8 @@ bool cros_gralloc_driver::get_resolved_format_and_use_flags(

combo = drv_get_combination(drv, resolved_format, resolved_use_flags);
if (!combo && (descriptor->use_flags & BO_USE_SCANOUT)) {
if (is_kmsro_enabled()) {
/* if kmsro is enabled, it is scanout buffer and not used for video,
* don't need remove scanout flag */
if (!IsSupportedYUVFormat(descriptor->droid_format)) {
combo = drv_get_combination(drv, resolved_format,
(descriptor->use_flags) & (~BO_USE_SCANOUT));
} else {
drv = drv_render_;
resolved_use_flags &= ~BO_USE_SCANOUT;
combo = drv_get_combination(drv, resolved_format, descriptor->use_flags);
}
} else {
resolved_use_flags &= ~BO_USE_SCANOUT;
combo = drv_get_combination(drv, resolved_format, descriptor->use_flags);
}
resolved_use_flags &= ~BO_USE_SCANOUT;
combo = drv_get_combination(drv, resolved_format, descriptor->use_flags);
}
if (!combo && (descriptor->droid_usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) &&
descriptor->droid_format != HAL_PIXEL_FORMAT_YCbCr_420_888) {
Expand Down Expand Up @@ -340,11 +289,10 @@ bool cros_gralloc_driver::is_supported(const struct cros_gralloc_buffer_descript
{
uint32_t resolved_format;
uint64_t resolved_use_flags;
struct driver *drv = (descriptor->use_flags & BO_USE_SCANOUT) ? drv_kms_ : drv_render_;
struct driver *drv = drv_render_;
uint32_t max_texture_size = drv_get_max_texture_2d_size(drv);
if (!get_resolved_format_and_use_flags(descriptor, &resolved_format, &resolved_use_flags))
return false;

// Allow blob buffers to go beyond the limit.
if (descriptor->droid_format == HAL_PIXEL_FORMAT_BLOB)
return true;
Expand Down Expand Up @@ -388,16 +336,10 @@ int32_t cros_gralloc_driver::allocate(const struct cros_gralloc_buffer_descripto
struct cros_gralloc_handle *hnd;
int32_t format;
std::unique_ptr<cros_gralloc_buffer> buffer;
bool from_kms = false;

struct driver *drv;

if ((descriptor->use_flags & BO_USE_SCANOUT)) {
from_kms = true;
drv = drv_kms_;
} else {
drv = drv_render_;
}
drv = drv_render_;

if (!get_resolved_format_and_use_flags(descriptor, &resolved_format, &resolved_use_flags)) {
ALOGE("Failed to resolve format and use_flags.");
Expand Down Expand Up @@ -461,7 +403,7 @@ int32_t cros_gralloc_driver::allocate(const struct cros_gralloc_buffer_descripto
hnd->fds[i] = -1;

hnd->num_planes = num_planes;
hnd->from_kms = from_kms;
hnd->from_kms = false; // not used, just set a default value. keep this member to be backward compatible.
for (size_t plane = 0; plane < num_planes; plane++) {
ret = drv_bo_get_plane_fd(bo, plane);
if (ret < 0)
Expand Down Expand Up @@ -551,7 +493,7 @@ int32_t cros_gralloc_driver::retain(buffer_handle_t handle)
return -EINVAL;
}

drv = (hnd->from_kms) ? drv_kms_ : drv_render_;
drv = drv_render_;

auto hnd_it = handles_.find(hnd);
if (hnd_it != handles_.end()) {
Expand Down Expand Up @@ -815,8 +757,8 @@ uint32_t cros_gralloc_driver::get_resolved_drm_format(uint32_t drm_format, uint6
{
uint32_t resolved_format;
uint64_t resolved_use_flags;
struct driver *drv = (use_flags & BO_USE_SCANOUT) ? drv_kms_ : drv_render_;

struct driver *drv = drv_render_;
drv_resolve_format_and_use_flags(drv, drm_format, use_flags, &resolved_format,
&resolved_use_flags);

Expand Down
5 changes: 0 additions & 5 deletions cros_gralloc/cros_gralloc_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ class cros_gralloc_driver
const std::function<void(cros_gralloc_buffer *)> &function);
void with_each_buffer(const std::function<void(cros_gralloc_buffer *)> &function);
uint32_t get_resolved_common_drm_format(uint32_t drm_format);
bool is_kmsro_enabled()
{
return drv_kms_ != drv_render_;
};

private:
cros_gralloc_driver();
Expand Down Expand Up @@ -87,7 +83,6 @@ class cros_gralloc_driver
int32_t refcount = 1;
};

struct driver *drv_kms_ = nullptr;
struct driver *drv_render_ = nullptr;
std::mutex mutex_;
std::unordered_map<uint32_t, std::unique_ptr<cros_gralloc_buffer>> buffers_;
Expand Down
18 changes: 18 additions & 0 deletions cros_gralloc/cros_gralloc_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#endif

#include <hardware/gralloc.h>
#include "i915_private_android_types.h"
#include <sync/sync.h>

/* Define to match AIDL BufferUsage::VIDEO_DECODER. */
Expand Down Expand Up @@ -46,6 +47,21 @@ bool is_flex_format(uint32_t format)
return false;
}

bool flex_format_match(uint32_t descriptor_format, uint32_t handle_format, uint64_t usage)
{
bool flag = usage & (GRALLOC_USAGE_HW_CAMERA_READ | GRALLOC_USAGE_HW_CAMERA_WRITE);

/* HACK: See b/28671744 */
if (HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED == descriptor_format &&
((HAL_PIXEL_FORMAT_NV12 == handle_format && flag) ||
(HAL_PIXEL_FORMAT_RGBX_8888 == handle_format && !flag)))
return true;
else if (HAL_PIXEL_FORMAT_YCBCR_420_888 == descriptor_format && HAL_PIXEL_FORMAT_NV12 == handle_format)
return true;
else
return false;
}

uint32_t cros_gralloc_convert_format(int format)
{
/*
Expand Down Expand Up @@ -306,6 +322,8 @@ int32_t cros_gralloc_invert_format(int format)
return HAL_PIXEL_FORMAT_YCbCr_422_SP;
case DRM_FORMAT_YUV422:
return HAL_PIXEL_FORMAT_YCbCr_422_888;
case DRM_FORMAT_ABGR16161616F:
return HAL_PIXEL_FORMAT_RGBA_FP16;
default:
ALOGE("Unhandled DRM format %4.4s", drmFormat2Str(format));
}
Expand Down
2 changes: 2 additions & 0 deletions cros_gralloc/cros_gralloc_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ cros_gralloc_handle_t cros_gralloc_convert_handle(buffer_handle_t handle);

int32_t cros_gralloc_sync_wait(int32_t fence, bool close_fence);

bool flex_format_match(uint32_t descriptor_format, uint32_t handle_format, uint64_t usage = 0);

#ifdef USE_GRALLOC1
int32_t cros_gralloc_sync_wait(int32_t acquire_fence);
#endif
Expand Down
13 changes: 10 additions & 3 deletions cros_gralloc/gralloc1/cros_gralloc1_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,8 @@ int32_t CrosGralloc1::validateBufferSize(buffer_handle_t buffer,
return CROS_GRALLOC_ERROR_BAD_HANDLE;
}

if (!is_flex_format(cros_gralloc_convert_format(descriptorInfo->format)) &&
if (!flex_format_match((uint32_t)descriptorInfo->format, (uint32_t)hnd->droid_format,
descriptorInfo->producerUsage | descriptorInfo->consumerUsage) &&
cros_gralloc_convert_format(descriptorInfo->format) != hnd->format) {
return CROS_GRALLOC_ERROR_BAD_VALUE;
}
Expand Down Expand Up @@ -466,12 +467,15 @@ int32_t CrosGralloc1::lock(buffer_handle_t bufferHandle, gralloc1_producer_usage
buffer_handle_t buffer_handle = native_handle_clone(bufferHandle);
auto error = retain(buffer_handle);
if (error != GRALLOC1_ERROR_NONE) {
ALOGE("Retain failed");
free(const_cast<native_handle_t*>(buffer_handle));
return error;
}
bufferHandle = buffer_handle;
if (driver->lock(bufferHandle, acquireFence, map_flags, addr))
if (driver->lock(bufferHandle, acquireFence, map_flags, addr)) {
ALOGE("lock failed");
return CROS_GRALLOC_ERROR_BAD_HANDLE;
}
free(const_cast<native_handle_t*>(buffer_handle));
}

Expand Down Expand Up @@ -567,12 +571,15 @@ int32_t CrosGralloc1::lockYCbCr(buffer_handle_t bufferHandle,
buffer_handle_t buffer_handle = native_handle_clone(bufferHandle);
auto error = retain(buffer_handle);
if (error != GRALLOC1_ERROR_NONE) {
ALOGE("Retain failed");
free(const_cast<native_handle_t*>(buffer_handle));
return error;
}
bufferHandle = buffer_handle;
if (driver->lock(bufferHandle, acquireFence, map_flags, addr))
if (driver->lock(bufferHandle, acquireFence, map_flags, addr)) {
ALOGE("lock failed");
return CROS_GRALLOC_ERROR_BAD_HANDLE;
}
driver->release(buffer_handle);
free(const_cast<native_handle_t*>(buffer_handle));
}
Expand Down
1 change: 1 addition & 0 deletions cros_gralloc/gralloc4/CrosGralloc4Allocator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ Error CrosGralloc4Allocator::allocate(const BufferDescriptorInfo& descriptor, ui
}

if (!outStride || !outHandle) {
ALOGE("Invalid params.\n");
return Error::NO_RESOURCES;
}

Expand Down
21 changes: 16 additions & 5 deletions cros_gralloc/gralloc4/CrosGralloc4Mapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,25 @@ Return<Error> CrosGralloc4Mapper::validateBufferSize(void* rawHandle,
}

PixelFormat crosHandleFormat = static_cast<PixelFormat>(crosHandle->droid_format);
int32_t yuvFormat = static_cast<int32_t>(descriptor.format);
if (descriptor.format != crosHandleFormat && yuvFormat != crosHandle->droid_format &&
!(descriptor.format == PixelFormat::YCBCR_420_888 &&
crosHandle->droid_format == HAL_PIXEL_FORMAT_NV12)) {


uint32_t descriptor_drmFormat = 0, handle_drmFormat = 0;

if (convertToDrmFormat(descriptor.format, &descriptor_drmFormat)) {
ALOGE("Failed to convertToDrmFormat, descriptor.format = %d", (uint32_t)descriptor.format);
return Error::BAD_VALUE;
}

if (convertToDrmFormat(crosHandleFormat, &handle_drmFormat)) {
ALOGE("Failed to convertToDrmFormat, crosHandleFormat = %d", (uint32_t)crosHandleFormat);
return Error::BAD_VALUE;
}

if (descriptor_drmFormat != handle_drmFormat &&
!flex_format_match((uint32_t)descriptor.format, (uint32_t)crosHandleFormat, descriptor.usage)) {
ALOGE("Failed to validateBufferSize. Format mismatch.");
return Error::BAD_BUFFER;
}

if (descriptor.width != crosHandle->width) {
ALOGE("Failed to validateBufferSize. Width mismatch (%d vs %d).", descriptor.width,
crosHandle->width);
Expand Down
Loading
Loading