Skip to content

Commit

Permalink
Add the drv_kms_ back
Browse files Browse the repository at this point in the history
Signed-off-by: He, Yue <[email protected]>
  • Loading branch information
yhe39 committed Aug 27, 2024
1 parent 6876211 commit d3a527c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 13 deletions.
88 changes: 76 additions & 12 deletions cros_gralloc/cros_gralloc_driver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ 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 @@ -179,14 +189,25 @@ cros_gralloc_driver::cros_gralloc_driver()
switch (availabe_node) {
// only have one render node, is GVT-d/BM/VirtIO
case 1:
if (drv_render_) {
drv_kms_ = drv_render_;
}
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) {
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]);
drv_kms_ = drv_render_;
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;
Expand All @@ -196,7 +217,15 @@ cros_gralloc_driver::cros_gralloc_driver()
close(node_fd[1]);
}
if (virtio_node_idx != -1) {
close(node_fd[virtio_node_idx]);
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]);
drv_kms_ = drv_render_;
break;
}
} else{
drv_kms_ = drv_render_;
}
gpu_grp_type = THREE_GPU_IGPU_VIRTIO_DGPU;
// TO-DO: the 3rd node is i915 or others.
Expand All @@ -212,6 +241,15 @@ cros_gralloc_driver::cros_gralloc_driver()
drv_render_ = nullptr;
}
}
if (drv_kms_ && (drv_kms_ != drv_render_)) {
if (drv_init(drv_kms_, gpu_grp_type)) {
drv_loge("Failed to init driver\n");
fd = drv_get_fd(drv_kms_);
drv_destroy(drv_kms_);
close(fd);
drv_kms_ = nullptr;
}
}
}

for (int i = 0; i < availabe_node; i++) {
Expand All @@ -224,6 +262,16 @@ 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 @@ -235,7 +283,7 @@ cros_gralloc_driver::~cros_gralloc_driver()

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

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

struct driver *drv = drv_render_;
struct driver *drv = (descriptor->use_flags & BO_USE_SCANOUT) ? drv_kms_ : 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 @@ -260,8 +308,21 @@ 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)) {
resolved_use_flags &= ~BO_USE_SCANOUT;
combo = drv_get_combination(drv, resolved_format, descriptor->use_flags);
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);
}
}
if (!combo && (descriptor->droid_usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) &&
descriptor->droid_format != HAL_PIXEL_FORMAT_YCbCr_420_888) {
Expand Down Expand Up @@ -289,7 +350,7 @@ bool cros_gralloc_driver::is_supported(const struct cros_gralloc_buffer_descript
{
uint32_t resolved_format;
uint64_t resolved_use_flags;
struct driver *drv = drv_render_;
struct driver *drv = (descriptor->use_flags & BO_USE_SCANOUT) ? drv_kms_ : 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;
Expand Down Expand Up @@ -337,8 +398,12 @@ int32_t cros_gralloc_driver::allocate(const struct cros_gralloc_buffer_descripto
std::unique_ptr<cros_gralloc_buffer> buffer;

struct driver *drv;

drv = drv_render_;
if ((descriptor->use_flags & BO_USE_SCANOUT) && (!IsSupportedYUVFormat(descriptor->droid_format))) {
from_kms = true;
drv = drv_kms_;
} else {
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 @@ -402,7 +467,6 @@ int32_t cros_gralloc_driver::allocate(const struct cros_gralloc_buffer_descripto
hnd->fds[i] = -1;

hnd->num_planes = num_planes;
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 @@ -488,7 +552,7 @@ int32_t cros_gralloc_driver::retain(buffer_handle_t handle)
return -EINVAL;
}

drv = drv_render_;
drv = (from_kms) ? drv_kms_ : drv_render_;

auto hnd_it = handles_.find(hnd);
if (hnd_it != handles_.end()) {
Expand Down Expand Up @@ -752,7 +816,7 @@ 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 = drv_render_;
struct driver *drv = ((use_flags & BO_USE_SCANOUT) && (!IsSupportedYUVFormat(drm_format))) ? drv_kms_ : drv_render_;

drv_resolve_format_and_use_flags(drv, drm_format, use_flags, &resolved_format,
&resolved_use_flags);
Expand Down
5 changes: 5 additions & 0 deletions cros_gralloc/cros_gralloc_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ 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 @@ -84,6 +87,8 @@ class cros_gralloc_driver
};

struct driver *drv_render_ = nullptr;
struct driver *drv_kms_ = nullptr;
bool from_kms = false;
std::mutex mutex_;
std::unordered_map<uint32_t, std::unique_ptr<cros_gralloc_buffer>> buffers_;
std::unordered_map<cros_gralloc_handle_t, cros_gralloc_imported_handle_info> handles_;
Expand Down
1 change: 0 additions & 1 deletion cros_gralloc/cros_gralloc_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ struct cros_gralloc_handle : public native_handle_t {
uint32_t strides[DRV_MAX_PLANES];
uint32_t offsets[DRV_MAX_PLANES];
uint32_t sizes[DRV_MAX_PLANES];
bool from_kms;
uint32_t id;
uint32_t width;
uint32_t height;
Expand Down

0 comments on commit d3a527c

Please sign in to comment.