Skip to content

Commit

Permalink
Add the drv_kms_ back
Browse files Browse the repository at this point in the history
Use virtio to allocate scanout buffer.

Signed-off-by: HeYue <[email protected]>
  • Loading branch information
yhe39 committed May 30, 2024
1 parent 0da1979 commit 25ab5b0
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 13 deletions.
81 changes: 68 additions & 13 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,7 +189,9 @@ cros_gralloc_driver::cros_gralloc_driver()
switch (availabe_node) {
// only have one render node, is GVT-d/BM/VirtIO
case 1:
if (!drv_render_) {
if (drv_render_) {
drv_kms_ = drv_render_;
} else if (!drv_render_) {
fd = drv_get_fd(drv_render_);
drv_destroy(drv_render_);
close(fd);
Expand All @@ -189,10 +201,17 @@ cros_gralloc_driver::cros_gralloc_driver()
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]);
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 @@ -202,7 +221,12 @@ 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]);
break;
}
}
gpu_grp_type = THREE_GPU_IGPU_VIRTIO_DGPU;
// TO-DO: the 3rd node is i915 or others.
Expand All @@ -212,6 +236,9 @@ cros_gralloc_driver::cros_gralloc_driver()
if (drv_render_) {
drv_init(drv_render_, gpu_grp_type);
}
if (drv_kms_ && (drv_kms_ != drv_render_)) {
drv_init(drv_kms_, gpu_grp_type);
}
}

for (int i = 0; i < availabe_node; i++) {
Expand All @@ -224,6 +251,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 +272,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 +283,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 +297,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 +339,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 @@ -335,10 +385,15 @@ int32_t cros_gralloc_driver::allocate(const struct cros_gralloc_buffer_descripto
struct bo *bo;
struct cros_gralloc_handle *hnd;
std::unique_ptr<cros_gralloc_buffer> buffer;
bool from_kms = false;

struct driver *drv;

drv = drv_render_;
if ((descriptor->use_flags & BO_USE_SCANOUT)) {
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 +457,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 = false; // not used, just set a default value. keep this member to be backward compatible.
hnd->from_kms = from_kms;
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 +543,7 @@ int32_t cros_gralloc_driver::retain(buffer_handle_t handle)
return -EINVAL;
}

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

auto hnd_it = handles_.find(hnd);
if (hnd_it != handles_.end()) {
Expand Down Expand Up @@ -752,7 +807,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) ? drv_kms_ : drv_render_;

drv_resolve_format_and_use_flags(drv, drm_format, use_flags, &resolved_format,
&resolved_use_flags);
Expand Down
4 changes: 4 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,7 @@ class cros_gralloc_driver
};

struct driver *drv_render_ = nullptr;
struct driver *drv_kms_ = nullptr;
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

0 comments on commit 25ab5b0

Please sign in to comment.