Skip to content

Commit

Permalink
Probe new added render node
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 c21748e commit a0d9257
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 5 deletions.
167 changes: 163 additions & 4 deletions cros_gralloc/cros_gralloc_driver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
// DRM Card nodes start at 0
#define DRM_CARD_NODE_START 0

#define IVSH_WIDTH 1920
#define IVSH_HEIGHT 1080

class cros_gralloc_driver_preloader
{
public:
Expand Down Expand Up @@ -90,6 +93,99 @@ cros_gralloc_driver *cros_gralloc_driver::get_instance()
return &s_instance;
}

int32_t cros_gralloc_driver::reload()
{
ALOGE("--yue reload in minigbm\n");
int fd;
drmVersionPtr version;
char const *str = "%s/renderD%d";
const char *undesired[2] = { "vgem", nullptr };
uint32_t min_node = 128;
uint32_t max_node = (min_node + drv_num + 2);
uint32_t j;
char *node;

const int render_num = 10;
const int name_length = 50;
int availabe_node = 0;
uint32_t gpu_grp_type = 0;

availabe_node = 0;
for (uint32_t i = min_node; i < max_node; i++) {
if (asprintf(&node, str, DRM_DIR_NAME, i) < 0)
continue;

fd = open(node, O_RDWR, 0);
free(node);
if (fd < 0)
continue;

version = drmGetVersion(fd);
if (!version) {
close(fd);
continue;
}

for (j = 0; j < ARRAY_SIZE(undesired); j++) {
if (undesired[j] && !strcmp(version->name, undesired[j])) {
drmFreeVersion(version);
close(fd);
break;
}
}

// hit any of undesired render node
if (j < ARRAY_SIZE(undesired))
continue;

availabe_node++;
close(fd);
drmFreeVersion(version);
}

// have the ivshemem node
if ((availabe_node > drv_num) && !drv_ivshmem_) {
drv_logi("init the drv_ivshmem _\n");
drv_num = availabe_node;
if (asprintf(&node, str, DRM_DIR_NAME, (min_node + availabe_node - 1)) < 0)
drv_logi("Open ivshmem node fail\n");
fd = open(node, O_RDWR, 0);
free(node);

if (fd < 0) {
return -ENODEV;
}

version = drmGetVersion(fd);
if (!version) {
close(fd);
return -ENODEV;
}

if (strcmp(version->name, "virtio_gpu")) {
drv_logi("New added node is not ivshmem node\n");
close(fd);
drmFreeVersion(version);
return -ENODEV;
}

drv_ivshmem_ = drv_create(fd);
if (!drv_ivshmem_) {
drv_logi("Failed to create driver for ivshmem device\n");
close(fd);
return -ENODEV;
}
gpu_grp_type = THREE_GPU_IGPU_VIRTIO_IVSHMEM;

if (drv_ivshmem_) {
drv_loge("--yue-- init ivshmem success\n");
drv_init(drv_ivshmem_, gpu_grp_type);
}
}

return 0;
}

cros_gralloc_driver::cros_gralloc_driver()
{
/*
Expand Down Expand Up @@ -139,6 +235,13 @@ cros_gralloc_driver::cros_gralloc_driver()
close(fd);
}

if (drv_ivshmem_) {
int fd = drv_get_fd(drv_ivshmem_);
drv_destroy(drv_ivshmem_);
drv_ivshmem_ = nullptr;
close(fd);
}

for (uint32_t i = min_render_node; i < max_render_node; i++) {
if (asprintf(&node, render_nodes_fmt, DRM_DIR_NAME, i) < 0)
continue;
Expand Down Expand Up @@ -179,6 +282,8 @@ cros_gralloc_driver::cros_gralloc_driver()
drmFreeVersion(version);
}

drv_num = availabe_node;

// open the first render node
if (availabe_node > 0) {
drv_render_ = drv_create(node_fd[0]);
Expand Down Expand Up @@ -279,6 +384,13 @@ cros_gralloc_driver::~cros_gralloc_driver()
close(fd);
}

if (drv_ivshmem_) {
int fd = drv_get_fd(drv_ivshmem_);
drv_destroy(drv_ivshmem_);
drv_ivshmem_ = nullptr;
close(fd);
}

}

bool cros_gralloc_driver::is_initialized()
Expand All @@ -294,7 +406,23 @@ 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;

if (!drv_ivshmem_) {
if (reload()) {
}
}

if (drv_ivshmem_ && (descriptor->use_flags & BO_USE_SCANOUT) &&
(descriptor->width == IVSH_WIDTH) && (descriptor->height == IVSH_HEIGHT)) {
drv = drv_ivshmem_;
use_ivshmem = true;
} else if ((descriptor->use_flags & BO_USE_SCANOUT) && (!IsSupportedYUVFormat(descriptor->droid_format))) {
drv = drv_kms_;
} else {
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 Down Expand Up @@ -350,7 +478,23 @@ 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;

if (!drv_ivshmem_) {
if (reload()) {
}
}

if (drv_ivshmem_ && (descriptor->use_flags & BO_USE_SCANOUT) &&
(descriptor->width == IVSH_WIDTH) && (descriptor->height == IVSH_HEIGHT)) {
drv = drv_ivshmem_;
use_ivshmem = true;
} else if ((descriptor->use_flags & BO_USE_SCANOUT) && (!IsSupportedYUVFormat(descriptor->droid_format))) {
drv = drv_kms_;
} else {
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;
Expand Down Expand Up @@ -399,7 +543,16 @@ int32_t cros_gralloc_driver::allocate(const struct cros_gralloc_buffer_descripto
bool from_kms = false;

struct driver *drv;
if ((descriptor->use_flags & BO_USE_SCANOUT) && (!IsSupportedYUVFormat(descriptor->droid_format))) {
if (!drv_ivshmem_) {
if (reload()) {
}
}

if (drv_ivshmem_ && (descriptor->use_flags & BO_USE_SCANOUT) &&
(descriptor->width == IVSH_WIDTH) && (descriptor->height == IVSH_HEIGHT)) {
drv = drv_ivshmem_;
use_ivshmem = true;
} else if ((descriptor->use_flags & BO_USE_SCANOUT) && (!IsSupportedYUVFormat(descriptor->droid_format))) {
from_kms = true;
drv = drv_kms_;
} else {
Expand Down Expand Up @@ -554,7 +707,13 @@ int32_t cros_gralloc_driver::retain(buffer_handle_t handle)
return -EINVAL;
}

drv = (hnd->from_kms) ? drv_kms_ : drv_render_;
if (drv_ivshmem_ && use_ivshmem) {
drv = drv_ivshmem_;
} else if(hnd->from_kms) {
drv = drv_kms_;
} else {
drv = drv_render_;
}

auto hnd_it = handles_.find(hnd);
if (hnd_it != handles_.end()) {
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 @@ -58,6 +58,7 @@ class cros_gralloc_driver
bool is_kmsro_enabled() {
return drv_kms_ != drv_render_;
}
int32_t reload();

private:
cros_gralloc_driver();
Expand Down Expand Up @@ -88,6 +89,9 @@ class cros_gralloc_driver

struct driver *drv_render_ = nullptr;
struct driver *drv_kms_ = nullptr;
struct driver *drv_ivshmem_ = nullptr;
bool use_ivshmem = false;
int32_t drv_num = 0;
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
3 changes: 2 additions & 1 deletion drv_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ enum CIV_GPU_TYPE {
ONE_GPU_VIRTIO,
TWO_GPU_IGPU_VIRTIO,
TWO_GPU_IGPU_DGPU,
THREE_GPU_IGPU_VIRTIO_DGPU
THREE_GPU_IGPU_VIRTIO_DGPU,
THREE_GPU_IGPU_VIRTIO_IVSHMEM
};

struct driver {
Expand Down

0 comments on commit a0d9257

Please sign in to comment.