调试平板的 recovery,执行下面函数时报“mmap() failer: Invalid argument” 错误,不知道错误原因是什么。具体相关变量值标注在注释里,希望高手给予指点。
static drm_surface *drm_create_surface(int width, int height) {
struct drm_surface *surface;
struct drm_mode_create_dumb create_dumb;
uint32_t format;
int ret;
surface = (struct drm_surface*)calloc(1, sizeof(*surface));
if (!surface) {
printf("Can't allocate memory\n");
return NULL;
}
#if defined(RECOVERY_ABGR)
format = DRM_FORMAT_RGBA8888;
#elif defined(RECOVERY_BGRA)
format = DRM_FORMAT_ARGB8888; //这是我的平板中的参数
#elif defined(RECOVERY_RGBX)
format = DRM_FORMAT_XBGR8888;
#else
format = DRM_FORMAT_RGB565;
#endif
memset(&create_dumb, 0, sizeof(create_dumb));
create_dumb.height = height;
create_dumb.width = width;
create_dumb.bpp = drm_format_to_bpp(format);
create_dumb.flags = 0;
ret = drmIoctl(drm_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb);
if (ret) {
printf("DRM_IOCTL_MODE_CREATE_DUMB failed ret=%d\n",ret);
drm_destroy_surface(surface);
return NULL;
}
surface->handle = create_dumb.handle;
uint32_t handles[4], pitches[4], offsets[4];
handles[0] = surface->handle;
pitches[0] = create_dumb.pitch;
offsets[0] = 0;
ret = drmModeAddFB2(drm_fd, width, height,
format, handles, pitches, offsets,
&(surface->fb_id), 0);
if (ret) {
printf("DRM_IOCTL_MODE_MAP_DUMB failed ret=%d\n",ret);
drm_destroy_surface(surface);
return NULL;;
}
surface->base.height = height;
surface->base.width = width;
surface->base.row_bytes = create_dumb.pitch;
surface->base.pixel_bytes = create_dumb.bpp / 8;
printf("Butterfly check mmap():baseheight= %d\n", surface->base.height);
printf("Butterfly check mmap():baserow_bytes= %d\n", surface->base.row_bytes);
printf("Butterfly check mmap():drm_fd= %d\n", drm_fd);
printf("Butterfly check mmap():map_dumb.offset= %d\n", map_dumb.offset);
surface->base.data = (unsigned char*)
mmap(NULL,
surface->base.height * surface->base.row_bytes,
PROT_READ | PROT_WRITE, MAP_SHARED,
drm_fd, map_dumb.offset); //执行到这里,surface->base.height = 2048,surface->base.row_bytes = 3072,drm_fd=0, map_dumb.offset=0,并且检查了本函数所在 cpp 中与 drm_fd 相关的语句,执行均正常。
if (surface->base.data == MAP_FAILED) {
perror("mmap() failed"); //此时报错 “mmap() failer: Invalid argument”,本人水平有限,不知道原因
drm_destroy_surface(surface);
return NULL;
}
return surface;
}