Skip to content

Commit

Permalink
Dump layer buffer
Browse files Browse the repository at this point in the history
Support NV12&ARGB

Signed-off-by: lihaihong <[email protected]>
  • Loading branch information
HaihongxLi committed Aug 19, 2024
1 parent d526817 commit ae1e831
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 6 deletions.
6 changes: 5 additions & 1 deletion Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ cc_defaults {
"libutils",
],

include_dirs: ["vendor/intel/external/drm-hwcomposer"],
include_dirs: [
"vendor/intel/external/drm-hwcomposer",
"hardware/intel/external/minigbm-intel/cros_gralloc",
],

header_libs: [
"android.hardware.graphics.composer3-command-buffer",
Expand Down Expand Up @@ -140,6 +143,7 @@ cc_library_shared {
],
cppflags: [
"-DHEADLESS_RESOLUTION_2560_1600",
"-DHWC_DUMP_BUFFER",
],
}

Expand Down
14 changes: 13 additions & 1 deletion backend/Backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
#include <aidl/android/hardware/graphics/composer3/Composition.h>
#include "BackendManager.h"
#include "bufferinfo/BufferInfoGetter.h"

#ifdef HWC_DUMP_BUFFER
#include "bufferinfo/legacy/BufferInfoMinigbm.h"
#include "utils/properties.h"
#endif
namespace android {

HWC2::Error Backend::ValidateDisplay(HwcDisplay *display, uint32_t *num_types,
Expand All @@ -35,6 +38,15 @@ HWC2::Error Backend::ValidateDisplay(HwcDisplay *display, uint32_t *num_types,
if ((uint32_t)l->GetSfType() == (uint32_t)aidl::android::hardware::graphics::composer3::Composition::DISPLAY_DECORATION)
return HWC2::Error::Unsupported;
}

#ifdef HWC_DUMP_BUFFER
char status[PROPERTY_VALUE_MAX] = {0};
property_get("drm.dumpbuffer.on", status, "1");
for (size_t z_order = 0; z_order < layers.size(); ++z_order) {
if (status[0] != '0')
BufferInfoMinigbm::DumpBuffer(display->GetPipe().device, layers[z_order]->GetBufferHandle(), z_order);
}
#endif
int client_start = -1;
size_t client_size = 0;

Expand Down
89 changes: 89 additions & 0 deletions bufferinfo/legacy/BufferInfoMinigbm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <cstring>

#include "utils/log.h"
#include <cros_gralloc_helpers.h>
#include <i915_private_android_types.h>
namespace android {

LEGACY_BUFFER_INFO_GETTER(BufferInfoMinigbm);
Expand Down Expand Up @@ -122,4 +124,91 @@ int BufferInfoMinigbm::ValidateGralloc() {
return 0;
}

void BufferInfoMinigbm::InitializeGralloc1(DrmDevice *drmDevice) {
hw_device_t *device;

struct dri2_drm_display *dri_drm = (struct dri2_drm_display *)calloc(1, sizeof(*dri_drm));
if (!dri_drm)
return;

dri_drm->fd = -1;
int ret = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
(const hw_module_t **)&dri_drm->gralloc);
if (ret) {
return;
}

dri_drm->gralloc_version = dri_drm->gralloc->common.module_api_version;
if (dri_drm->gralloc_version == HARDWARE_MODULE_API_VERSION(1, 0)) {
ret = dri_drm->gralloc->common.methods->open(&dri_drm->gralloc->common, GRALLOC_HARDWARE_MODULE_ID, &device);
if (ret) {
ALOGE("Failed to open device");
return;
} else {
ALOGE("success to open device, Initialize");
dri_drm->gralloc1_dvc = (gralloc1_device_t *)device;
dri_drm->pfn_lock = (GRALLOC1_PFN_LOCK)dri_drm->gralloc1_dvc->getFunction(dri_drm->gralloc1_dvc, GRALLOC1_FUNCTION_LOCK);
dri_drm->pfn_importBuffer = (GRALLOC1_PFN_IMPORT_BUFFER)dri_drm->gralloc1_dvc->getFunction(dri_drm->gralloc1_dvc, GRALLOC1_FUNCTION_IMPORT_BUFFER);
dri_drm->pfn_release = (GRALLOC1_PFN_RELEASE)dri_drm->gralloc1_dvc->getFunction(dri_drm->gralloc1_dvc, GRALLOC1_FUNCTION_RELEASE);
dri_drm->pfn_unlock = (GRALLOC1_PFN_UNLOCK)dri_drm->gralloc1_dvc->getFunction(dri_drm->gralloc1_dvc, GRALLOC1_FUNCTION_UNLOCK);
dri_drm->pfn_get_stride = (GRALLOC1_PFN_GET_STRIDE)dri_drm->gralloc1_dvc->getFunction(dri_drm->gralloc1_dvc, GRALLOC1_FUNCTION_GET_STRIDE);
drmDevice->dri_drm_ = (void *)dri_drm;
}
}
return;
}

void BufferInfoMinigbm::DumpBuffer(DrmDevice *drmDevice, buffer_handle_t handle, int z_order) {
if (NULL == handle)
return;
char dump_file[256] = {0};
buffer_handle_t handle_copy;
uint8_t* pixels = nullptr;
cros_gralloc_handle *gr_handle = (cros_gralloc_handle *)handle;
gralloc1_rect_t accessRegion = {0, 0, (int32_t)gr_handle->width, (int32_t)gr_handle->height};;

struct dri2_drm_display *dri_drm = (struct dri2_drm_display *)drmDevice->dri_drm_;

assert (dri_drm == nullptr ||
dri_drm->pfn_importBuffer == nullptr ||
dri_drm->pfn_lock == nullptr ||
dri_drm->pfn_unlock == nullptr ||
dri_drm->pfn_release == nullptr ||
dri_drm->pfn_get_stride);

int ret = dri_drm->pfn_importBuffer(dri_drm->gralloc1_dvc, handle, &handle_copy);
if (ret) {
ALOGE("Gralloc importBuffer failed");
return;
}
ret = dri_drm->pfn_lock(dri_drm->gralloc1_dvc, handle_copy,
GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN, GRALLOC1_PRODUCER_USAGE_CPU_WRITE_NEVER,
&accessRegion, reinterpret_cast<void**>(&pixels), 0);
if (ret) {
ALOGE("gralloc->lock failed: %d", ret);
return;
} else {
char ctime[32];
time_t t = time(0);
static int i = 0;
strftime(ctime, sizeof(ctime), "%Y-%m-%d", localtime(&t));
sprintf(dump_file, "/data/local/traces/dump_%d_%dx%d_%s_%d", z_order, gr_handle->width, gr_handle->height, ctime,i++);
int file_fd = 0;
file_fd = open(dump_file, O_RDWR|O_CREAT, 0666);
if (file_fd == -1) {
ALOGE("Failed to open %s while dumping", dump_file);
} else {
size_t size = 0;
if (gr_handle->usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)
size = gr_handle->width * gr_handle->height * 1.5;
else
size = gr_handle->width * gr_handle->height * 4;
write(file_fd, pixels, size);
close(file_fd);
}
int outReleaseFence = 0;
dri_drm->pfn_unlock(dri_drm->gralloc1_dvc, handle_copy, &outReleaseFence);
dri_drm->pfn_release(dri_drm->gralloc1_dvc, handle_copy);
}
}
} // namespace android
28 changes: 27 additions & 1 deletion bufferinfo/legacy/BufferInfoMinigbm.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,42 @@
#define BUFFERINFOMINIGBM_H_

#include <hardware/gralloc.h>

#include <hardware/gralloc1.h>
#include "bufferinfo/BufferInfoGetter.h"

#define DRV_MAX_PLANES 4
#define DRV_MAX_FDS (DRV_MAX_PLANES + 1)

enum INITIALIZE_ERROR{
INITIALIZE_CALLOC_ERROR = 1,
INITIALIZE_GET_MODULE_ERROR,
INITIALIZE_OPEN_DEVICE_ERROR,
INITIALIZE_NONE = 0,
};

struct dri2_drm_display
{
int fd;
const gralloc_module_t *gralloc;
uint16_t gralloc_version;
gralloc1_device_t *gralloc1_dvc;
GRALLOC1_PFN_LOCK pfn_lock;
GRALLOC1_PFN_GET_FORMAT pfn_getFormat;
GRALLOC1_PFN_UNLOCK pfn_unlock;
GRALLOC1_PFN_IMPORT_BUFFER pfn_importBuffer;
GRALLOC1_PFN_RELEASE pfn_release;
GRALLOC1_PFN_GET_STRIDE pfn_get_stride;
};

namespace android {

class BufferInfoMinigbm : public LegacyBufferInfoGetter {
public:
using LegacyBufferInfoGetter::LegacyBufferInfoGetter;
auto GetBoInfo(buffer_handle_t handle) -> std::optional<BufferInfo> override;
int ValidateGralloc() override;
static void InitializeGralloc1(DrmDevice *drmDevice);
static void DumpBuffer(DrmDevice *drmDevice, buffer_handle_t handle, int z_order);
};

} // namespace android
Expand Down
1 change: 1 addition & 0 deletions drm/DrmDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ class DrmDevice {
bool planes_enabling_;
uint32_t planes_num_;
bool color_adjustment_enabling_;
void *dri_drm_;
};
} // namespace android

Expand Down
8 changes: 6 additions & 2 deletions hwc2_device/HwcDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
#include "utils/properties.h"
#include <sync/sync.h>
#include <cinttypes>

#ifdef HWC_DUMP_BUFFER
#include "bufferinfo/legacy/BufferInfoMinigbm.h"
#endif
namespace android {

std::string HwcDisplay::DumpDelta(HwcDisplay::Stats delta) {
Expand Down Expand Up @@ -168,7 +170,9 @@ HWC2::Error HwcDisplay::Init() {
}

client_layer_.SetLayerBlendMode(HWC2_BLEND_MODE_PREMULTIPLIED);

#ifdef HWC_DUMP_BUFFER
BufferInfoMinigbm::InitializeGralloc1(pipeline_->device);
#endif
return HWC2::Error::None;
}

Expand Down
8 changes: 7 additions & 1 deletion hwc2_device/HwcLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
#include "bufferinfo/BufferInfoGetter.h"
#include "utils/log.h"

#ifdef HWC_DUMP_BUFFER
#include "bufferinfo/legacy/BufferInfoMinigbm.h"
#endif

namespace android {

// NOLINTNEXTLINE(readability-convert-member-functions-to-static)
Expand Down Expand Up @@ -270,7 +274,9 @@ void HwcLayer::ImportFb() {

void HwcLayer::PopulateLayerData(bool test) {
ImportFb();

#ifdef HWC_DUMP_BUFFER
BufferInfoMinigbm::DumpBuffer(parent_->GetPipe().device, buffer_handle_, 0);
#endif
if (blend_mode_ != BufferBlendMode::kUndefined) {
layer_data_.bi->blend_mode = blend_mode_;
}
Expand Down
1 change: 1 addition & 0 deletions hwc2_device/HwcLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class HwcLayer {
return !bi_get_failed_ && !fb_import_failed_ && buffer_handle_ != nullptr;
}

buffer_handle_t GetBufferHandle() {return buffer_handle_;}
private:
void ImportFb();
bool bi_get_failed_{};
Expand Down

0 comments on commit ae1e831

Please sign in to comment.