1
0
Fork 0

drm_hwcomposer: Set return type to std::optional for BufferInfoGetters

This is a bit of code modernization. Further changes will require indication
that buffer_info is valid, and using std::optional is the most correct
approach to do that.

Signed-off-by: Roman Stratiienko <roman.o.stratiienko@globallogic.com>
This commit is contained in:
Roman Stratiienko 2022-02-21 13:03:29 +02:00
parent 1cbaaf9eeb
commit e9fbd8d626
20 changed files with 153 additions and 129 deletions

View file

@ -49,15 +49,9 @@ BufferInfoGetter *BufferInfoGetter::GetInstance() {
}
bool BufferInfoGetter::IsHandleUsable(buffer_handle_t handle) {
BufferInfo bo{};
auto bo = GetBoInfo(handle);
if (ConvertBoInfo(handle, &bo) != 0) {
return false;
}
if (bo.prime_fds[0] == 0) {
return false;
}
return true;
return bo && bo->prime_fds[0] != 0;
}
int LegacyBufferInfoGetter::Init() {

View file

@ -20,6 +20,8 @@
#include <drm/drm_fourcc.h>
#include <hardware/gralloc.h>
#include <optional>
#include "BufferInfo.h"
#include "drm/DrmDevice.h"
@ -33,7 +35,8 @@ class BufferInfoGetter {
public:
virtual ~BufferInfoGetter() = default;
virtual int ConvertBoInfo(buffer_handle_t handle, BufferInfo *bo) = 0;
virtual auto GetBoInfo(buffer_handle_t handle)
-> std::optional<BufferInfo> = 0;
bool IsHandleUsable(buffer_handle_t handle);

View file

@ -86,55 +86,63 @@ BufferInfoMapperMetadata::GetFds(buffer_handle_t handle, BufferInfo *bo) {
return 0;
}
int BufferInfoMapperMetadata::ConvertBoInfo(buffer_handle_t handle,
BufferInfo *bo) {
auto BufferInfoMapperMetadata::GetBoInfo(buffer_handle_t handle)
-> std::optional<BufferInfo> {
GraphicBufferMapper &mapper = GraphicBufferMapper::getInstance();
if (handle == nullptr)
return -EINVAL;
return {};
int err = mapper.getPixelFormatFourCC(handle, &bo->format);
BufferInfo bi{};
int err = mapper.getPixelFormatFourCC(handle, &bi.format);
if (err != 0) {
ALOGE("Failed to get FourCC format err=%d", err);
return err;
return {};
}
err = mapper.getPixelFormatModifier(handle, &bo->modifiers[0]);
err = mapper.getPixelFormatModifier(handle, &bi.modifiers[0]);
if (err != 0) {
ALOGE("Failed to get DRM Modifier err=%d", err);
return err;
return {};
}
uint64_t width = 0;
err = mapper.getWidth(handle, &width);
if (err != 0) {
ALOGE("Failed to get Width err=%d", err);
return err;
return {};
}
bo->width = static_cast<uint32_t>(width);
bi.width = static_cast<uint32_t>(width);
uint64_t height = 0;
err = mapper.getHeight(handle, &height);
if (err != 0) {
ALOGE("Failed to get Height err=%d", err);
return err;
return {};
}
bo->height = static_cast<uint32_t>(height);
bi.height = static_cast<uint32_t>(height);
std::vector<ui::PlaneLayout> layouts;
err = mapper.getPlaneLayouts(handle, &layouts);
if (err != 0) {
ALOGE("Failed to get Plane Layouts err=%d", err);
return err;
return {};
}
for (uint32_t i = 0; i < layouts.size(); i++) {
bo->modifiers[i] = bo->modifiers[0];
bo->pitches[i] = layouts[i].strideInBytes;
bo->offsets[i] = layouts[i].offsetInBytes;
bo->sizes[i] = layouts[i].totalSizeInBytes;
bi.modifiers[i] = bi.modifiers[0];
bi.pitches[i] = layouts[i].strideInBytes;
bi.offsets[i] = layouts[i].offsetInBytes;
bi.sizes[i] = layouts[i].totalSizeInBytes;
}
return GetFds(handle, bo);
err = GetFds(handle, &bi);
if (err != 0) {
ALOGE("Failed to get fds (err=%d)", err);
return {};
}
return bi;
}
} // namespace android

View file

@ -25,7 +25,7 @@ class BufferInfoMapperMetadata : public BufferInfoGetter {
public:
using BufferInfoGetter::BufferInfoGetter;
int ConvertBoInfo(buffer_handle_t handle, BufferInfo *bo) override;
auto GetBoInfo(buffer_handle_t handle) -> std::optional<BufferInfo> override;
int GetFds(buffer_handle_t handle, BufferInfo *bo);

View file

@ -29,22 +29,24 @@ namespace android {
LEGACY_BUFFER_INFO_GETTER(BufferInfoImagination);
int BufferInfoImagination::ConvertBoInfo(buffer_handle_t handle,
BufferInfo *bo) {
auto BufferInfoImagination::GetBoInfo(buffer_handle_t handle)
-> std::optional<BufferInfo> {
auto *hnd = (IMG_native_handle_t *)handle;
if (!hnd)
return -EINVAL;
return {};
/* Extra bits are responsible for buffer compression and memory layout */
if (hnd->iFormat & ~0x10f) {
ALOGV("Special buffer formats are not supported");
return -EINVAL;
return {};
}
bo->width = hnd->iWidth;
bo->height = hnd->iHeight;
bo->prime_fds[0] = hnd->fd[0];
bo->pitches[0] = ALIGN(hnd->iWidth, HW_ALIGN) * hnd->uiBpp >> 3;
BufferInfo bi{};
bi.width = hnd->iWidth;
bi.height = hnd->iHeight;
bi.prime_fds[0] = hnd->fd[0];
bi.pitches[0] = ALIGN(hnd->iWidth, HW_ALIGN) * hnd->uiBpp >> 3;
switch (hnd->iFormat) {
#ifdef HAL_PIXEL_FORMAT_BGRX_8888
@ -53,14 +55,14 @@ int BufferInfoImagination::ConvertBoInfo(buffer_handle_t handle,
break;
#endif
default:
bo->format = ConvertHalFormatToDrm(hnd->iFormat & 0xf);
if (bo->format == DRM_FORMAT_INVALID) {
bi.format = ConvertHalFormatToDrm(hnd->iFormat & 0xf);
if (bi.format == DRM_FORMAT_INVALID) {
ALOGV("Cannot convert hal format to drm format %u", hnd->iFormat);
return -EINVAL;
return {};
}
}
return 0;
return bi;
}
} // namespace android

View file

@ -27,7 +27,7 @@ class BufferInfoImagination : public LegacyBufferInfoGetter {
public:
using LegacyBufferInfoGetter::LegacyBufferInfoGetter;
int ConvertBoInfo(buffer_handle_t handle, BufferInfo *bo) override;
auto GetBoInfo(buffer_handle_t handle) -> std::optional<BufferInfo> override;
};
} // namespace android

View file

@ -162,13 +162,16 @@ bool BufferInfoLibdrm::GetYuvPlaneInfo(uint32_t hal_format, int num_fds,
return true;
}
int BufferInfoLibdrm::ConvertBoInfo(buffer_handle_t handle, BufferInfo *bo) {
auto BufferInfoLibdrm::GetBoInfo(buffer_handle_t handle)
-> std::optional<BufferInfo> {
gralloc_handle_t *gr_handle = gralloc_handle(handle);
if (!gr_handle)
return -EINVAL;
return {};
bo->width = gr_handle->width;
bo->height = gr_handle->height;
BufferInfo bi{};
bi.width = gr_handle->width;
bi.height = gr_handle->height;
#if GRALLOC_HANDLE_VERSION < 4
static std::once_flag once;
@ -178,34 +181,34 @@ int BufferInfoLibdrm::ConvertBoInfo(buffer_handle_t handle, BufferInfo *bo) {
});
#endif
#if GRALLOC_HANDLE_VERSION == 4
bo->modifiers[0] = gr_handle->modifier;
bi.modifiers[0] = gr_handle->modifier;
#endif
bo->prime_fds[0] = gr_handle->prime_fd;
bi.prime_fds[0] = gr_handle->prime_fd;
if (is_yuv(gr_handle->format)) {
if (!GetYuvPlaneInfo(gr_handle->format, handle->numFds, handle, bo))
return -EINVAL;
if (!GetYuvPlaneInfo(gr_handle->format, handle->numFds, handle, &bi))
return {};
} else {
bo->pitches[0] = gr_handle->stride;
bo->offsets[0] = 0;
bi.pitches[0] = gr_handle->stride;
bi.offsets[0] = 0;
/* FOSS graphic components (gbm_gralloc, mesa3d) are translating
* HAL_PIXEL_FORMAT_RGB_565 to DRM_FORMAT_RGB565 without swapping
* the R and B components. Same must be done here. */
switch (gr_handle->format) {
case HAL_PIXEL_FORMAT_RGB_565:
bo->format = DRM_FORMAT_RGB565;
bi.format = DRM_FORMAT_RGB565;
break;
default:
bo->format = ConvertHalFormatToDrm(gr_handle->format);
bi.format = ConvertHalFormatToDrm(gr_handle->format);
}
if (bo->format == DRM_FORMAT_INVALID)
return -EINVAL;
if (bi.format == DRM_FORMAT_INVALID)
return {};
}
return 0;
return bi;
}
constexpr char gbm_gralloc_module_name[] = "GBM Memory Allocator";

View file

@ -26,7 +26,7 @@ namespace android {
class BufferInfoLibdrm : public LegacyBufferInfoGetter {
public:
using LegacyBufferInfoGetter::LegacyBufferInfoGetter;
int ConvertBoInfo(buffer_handle_t handle, BufferInfo *bo) override;
auto GetBoInfo(buffer_handle_t handle) -> std::optional<BufferInfo> override;
int ValidateGralloc() override;
private:

View file

@ -66,30 +66,33 @@ uint64_t BufferInfoMaliHisi::ConvertGrallocFormatToDrmModifiers(
}
#endif
int BufferInfoMaliHisi::ConvertBoInfo(buffer_handle_t handle, BufferInfo *bo) {
auto BufferInfoMaliHisi::GetBoInfo(buffer_handle_t handle)
-> std::optional<BufferInfo> {
bool is_rgb = false;
const auto *hnd = (private_handle_t const *)handle;
if (!hnd)
return -EINVAL;
return {};
if (!(hnd->usage & GRALLOC_USAGE_HW_FB))
return -EINVAL;
return {};
uint32_t fmt = ConvertHalFormatToDrm(hnd->req_format);
if (fmt == DRM_FORMAT_INVALID)
return -EINVAL;
return {};
BufferInfo bi{};
is_rgb = IsDrmFormatRgb(fmt);
bo->modifiers[0] = ConvertGrallocFormatToDrmModifiers(hnd->internal_format,
is_rgb);
bi.modifiers[0] = ConvertGrallocFormatToDrmModifiers(hnd->internal_format,
is_rgb);
bo->width = hnd->width;
bo->height = hnd->height;
bo->format = fmt;
bo->pitches[0] = hnd->byte_stride;
bo->prime_fds[0] = hnd->share_fd;
bo->offsets[0] = 0;
bi.width = hnd->width;
bi.height = hnd->height;
bi.format = fmt;
bi.pitches[0] = hnd->byte_stride;
bi.prime_fds[0] = hnd->share_fd;
bi.offsets[0] = 0;
switch (fmt) {
case DRM_FORMAT_YVU420: {
@ -103,20 +106,20 @@ int BufferInfoMaliHisi::ConvertBoInfo(buffer_handle_t handle, BufferInfo *bo) {
int v_size = vu_stride * (adjusted_height / 2);
/* V plane*/
bo->prime_fds[1] = hnd->share_fd;
bo->pitches[1] = vu_stride;
bo->offsets[1] = y_size;
bi.prime_fds[1] = hnd->share_fd;
bi.pitches[1] = vu_stride;
bi.offsets[1] = y_size;
/* U plane */
bo->prime_fds[2] = hnd->share_fd;
bo->pitches[2] = vu_stride;
bo->offsets[2] = y_size + v_size;
bi.prime_fds[2] = hnd->share_fd;
bi.pitches[2] = vu_stride;
bi.offsets[2] = y_size + v_size;
break;
}
default:
break;
}
return 0;
return bi;
}
} // namespace android

View file

@ -27,7 +27,7 @@ class BufferInfoMaliHisi : public LegacyBufferInfoGetter {
public:
using LegacyBufferInfoGetter::LegacyBufferInfoGetter;
int ConvertBoInfo(buffer_handle_t handle, BufferInfo *bo) override;
auto GetBoInfo(buffer_handle_t handle) -> std::optional<BufferInfo> override;
private:
uint64_t ConvertGrallocFormatToDrmModifiers(uint64_t flags, bool is_rgb);

View file

@ -32,24 +32,26 @@ namespace android {
LEGACY_BUFFER_INFO_GETTER(BufferInfoMaliMediatek);
int BufferInfoMaliMediatek::ConvertBoInfo(buffer_handle_t handle,
BufferInfo *bo) {
auto BufferInfoMaliMediatek::GetBoInfo(buffer_handle_t handle)
-> std::optional<BufferInfo> {
const auto *hnd = (private_handle_t const *)handle;
if (!hnd)
return -EINVAL;
return {};
uint32_t fmt = ConvertHalFormatToDrm(hnd->req_format);
if (fmt == DRM_FORMAT_INVALID)
return -EINVAL;
return {};
bo->width = hnd->width;
bo->height = hnd->height;
bo->format = fmt;
bo->prime_fds[0] = hnd->share_fd;
bo->pitches[0] = hnd->byte_stride;
bo->offsets[0] = 0;
BufferInfo bi{};
return 0;
bi.width = hnd->width;
bi.height = hnd->height;
bi.format = fmt;
bi.prime_fds[0] = hnd->share_fd;
bi.pitches[0] = hnd->byte_stride;
bi.offsets[0] = 0;
return bi;
}
} // namespace android

View file

@ -27,7 +27,7 @@ class BufferInfoMaliMediatek : public LegacyBufferInfoGetter {
public:
using LegacyBufferInfoGetter::LegacyBufferInfoGetter;
int ConvertBoInfo(buffer_handle_t handle, BufferInfo *bo) override;
auto GetBoInfo(buffer_handle_t handle) -> std::optional<BufferInfo> override;
};
} // namespace android

View file

@ -61,29 +61,32 @@ uint64_t BufferInfoMaliMeson::ConvertGrallocFormatToDrmModifiers(
}
#endif
int BufferInfoMaliMeson::ConvertBoInfo(buffer_handle_t handle, BufferInfo *bo) {
auto BufferInfoMaliMeson::GetBoInfo(buffer_handle_t handle)
-> std::optional<BufferInfo> {
const auto *hnd = (private_handle_t const *)handle;
if (!hnd)
return -EINVAL;
return {};
if (!(hnd->usage & GRALLOC_USAGE_HW_FB))
return -EINVAL;
return {};
uint32_t fmt = ConvertHalFormatToDrm(hnd->req_format);
if (fmt == DRM_FORMAT_INVALID)
return -EINVAL;
return {};
bo->modifiers[0] = BufferInfoMaliMeson::ConvertGrallocFormatToDrmModifiers(
BufferInfo bi{};
bi.modifiers[0] = BufferInfoMaliMeson::ConvertGrallocFormatToDrmModifiers(
hnd->internal_format);
bo->width = hnd->width;
bo->height = hnd->height;
bo->format = fmt;
bo->prime_fds[0] = hnd->share_fd;
bo->pitches[0] = hnd->byte_stride;
bo->offsets[0] = 0;
bi.width = hnd->width;
bi.height = hnd->height;
bi.format = fmt;
bi.prime_fds[0] = hnd->share_fd;
bi.pitches[0] = hnd->byte_stride;
bi.offsets[0] = 0;
return 0;
return {};
}
} // namespace android

View file

@ -26,7 +26,7 @@ namespace android {
class BufferInfoMaliMeson : public LegacyBufferInfoGetter {
public:
using LegacyBufferInfoGetter::LegacyBufferInfoGetter;
int ConvertBoInfo(buffer_handle_t handle, BufferInfo *bo) override;
auto GetBoInfo(buffer_handle_t handle) -> std::optional<BufferInfo> override;
private:
uint64_t ConvertGrallocFormatToDrmModifiers(uint64_t flags);

View file

@ -43,11 +43,14 @@ struct cros_gralloc0_buffer_info {
int stride[4];
};
int BufferInfoMinigbm::ConvertBoInfo(buffer_handle_t handle, BufferInfo *bo) {
auto BufferInfoMinigbm::GetBoInfo(buffer_handle_t handle)
-> std::optional<BufferInfo> {
if (handle == nullptr) {
return -EINVAL;
return {};
}
BufferInfo bi{};
uint32_t width{};
uint32_t height{};
if (gralloc_->perform(gralloc_, CROS_GRALLOC_DRM_GET_DIMENSIONS, handle,
@ -55,7 +58,7 @@ int BufferInfoMinigbm::ConvertBoInfo(buffer_handle_t handle, BufferInfo *bo) {
ALOGE(
"CROS_GRALLOC_DRM_GET_DIMENSIONS operation has failed. "
"Please ensure you are using the latest minigbm.");
return -EINVAL;
return {};
}
int32_t droid_format{};
@ -64,7 +67,7 @@ int BufferInfoMinigbm::ConvertBoInfo(buffer_handle_t handle, BufferInfo *bo) {
ALOGE(
"CROS_GRALLOC_DRM_GET_FORMAT operation has failed. "
"Please ensure you are using the latest minigbm.");
return -EINVAL;
return {};
}
uint32_t usage{};
@ -73,7 +76,7 @@ int BufferInfoMinigbm::ConvertBoInfo(buffer_handle_t handle, BufferInfo *bo) {
ALOGE(
"CROS_GRALLOC_DRM_GET_USAGE operation has failed. "
"Please ensure you are using the latest minigbm.");
return -EINVAL;
return {};
}
struct cros_gralloc0_buffer_info info {};
@ -82,22 +85,22 @@ int BufferInfoMinigbm::ConvertBoInfo(buffer_handle_t handle, BufferInfo *bo) {
ALOGE(
"CROS_GRALLOC_DRM_GET_BUFFER_INFO operation has failed. "
"Please ensure you are using the latest minigbm.");
return -EINVAL;
return {};
}
bo->width = width;
bo->height = height;
bi.width = width;
bi.height = height;
bo->format = info.drm_fourcc;
bi.format = info.drm_fourcc;
for (int i = 0; i < info.num_fds; i++) {
bo->modifiers[i] = info.modifier;
bo->prime_fds[i] = info.fds[i];
bo->pitches[i] = info.stride[i];
bo->offsets[i] = info.offset[i];
bi.modifiers[i] = info.modifier;
bi.prime_fds[i] = info.fds[i];
bi.pitches[i] = info.stride[i];
bi.offsets[i] = info.offset[i];
}
return 0;
return bi;
}
constexpr char cros_gralloc_module_name[] = "CrOS Gralloc";

View file

@ -26,7 +26,7 @@ namespace android {
class BufferInfoMinigbm : public LegacyBufferInfoGetter {
public:
using LegacyBufferInfoGetter::LegacyBufferInfoGetter;
int ConvertBoInfo(buffer_handle_t handle, BufferInfo *bo) override;
auto GetBoInfo(buffer_handle_t handle) -> std::optional<BufferInfo> override;
int ValidateGralloc() override;
};

View file

@ -176,7 +176,7 @@ bool DrmPlane::IsValidForLayer(DrmHwcLayer *layer) {
return false;
}
uint32_t format = layer->buffer_info.format;
uint32_t format = layer->buffer_info->format;
if (!IsFormatSupported(format)) {
ALOGV("Plane %d does not supports %c%c%c%c format", GetId(), format,
format >> 8, format >> 16, format >> 24);

View file

@ -598,13 +598,16 @@ HWC2::Error HwcDisplay::SetClientTarget(buffer_handle_t target,
/* TODO: Do not update source_crop every call.
* It makes sense to do it once after every hotplug event. */
BufferInfo bo{};
BufferInfoGetter::GetInstance()->ConvertBoInfo(target, &bo);
auto bi = BufferInfoGetter::GetInstance()->GetBoInfo(target);
if (!bi) {
return HWC2::Error::BadParameter;
}
hwc_frect_t source_crop = {.left = 0.0F,
.top = 0.0F,
.right = static_cast<float>(bo.width),
.bottom = static_cast<float>(bo.height)};
.right = static_cast<float>(bi->width),
.bottom = static_cast<float>(bi->height)};
client_layer_.SetLayerSourceCrop(source_crop);
return HWC2::Error::None;

View file

@ -22,6 +22,7 @@
#include <cstdbool>
#include <cstdint>
#include <optional>
#include <vector>
#include "bufferinfo/BufferInfo.h"
@ -62,7 +63,7 @@ enum class DrmHwcBlending : int32_t {
struct DrmHwcLayer {
buffer_handle_t sf_handle = nullptr;
BufferInfo buffer_info{};
std::optional<BufferInfo> buffer_info;
std::shared_ptr<DrmFbIdHandle> fb_id_handle;
DrmHwcTransform transform{};

View file

@ -28,16 +28,15 @@
namespace android {
int DrmHwcLayer::ImportBuffer(DrmDevice *drm_device) {
buffer_info = BufferInfo{};
buffer_info = BufferInfoGetter::GetInstance()->GetBoInfo(sf_handle);
int ret = BufferInfoGetter::GetInstance()->ConvertBoInfo(sf_handle,
&buffer_info);
if (ret != 0) {
ALOGE("Failed to convert buffer info %d", ret);
return ret;
if (!buffer_info) {
ALOGE("Failed to convert buffer info");
return -EINVAL;
}
fb_id_handle = drm_device->GetDrmFbImporter().GetOrCreateFbId(&buffer_info);
fb_id_handle = drm_device->GetDrmFbImporter().GetOrCreateFbId(
&(*buffer_info));
if (!fb_id_handle) {
ALOGE("Failed to import buffer");
return -EINVAL;