接前一篇文章:DRM遇到的实际问题及领悟(1)
三、进一步定位
上回说到使用网上例程有同样会遇到mmap错误的问题,对于这个错误,perror只给出了“Invalid argument”的错误原因,具体错在哪里,并不知道。
1. 源码准备
没有办法,只能再找一个能够成功的例程。功夫不负有心人,在网上不断搜索,最终找到了以下链接中的例程:
https://github.com/tiagovignatti/intel-gpu-tools/blob/master/tools/intel_framebuffer_dump.c
代码如下:
/*
* Copyright © 2013 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*
* Read back all the KMS framebuffers attached to the CRTC and record as PNG.
*/
#define _GNU_SOURCE
#include <stdint.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <errno.h>
#include <xf86drmMode.h>
#include <i915_drm.h>
#include <cairo.h>
#include "intel_io.h"
#include "drmtest.h"
int main(int argc, char **argv)
{
drmModeResPtr res;
int fd, n;
fd = drmOpen("i915", NULL);
if (fd < 0)
return ENOENT;
res = drmModeGetResources(fd);
if (res == NULL)
return ENOMEM;
for (n = 0; n < res->count_crtcs; n++) {
struct drm_gem_open open_arg;
struct drm_gem_flink flink;
drmModeCrtcPtr crtc;
drmModeFBPtr fb;
crtc = drmModeGetCrtc(fd, res->crtcs[n]);
if (crtc == NULL)
continue;
fb = drmModeGetFB(fd, crtc->buffer_id);
drmModeFreeCrtc(crtc);
if (fb == NULL)
continue;
flink.handle = fb->handle;
if (drmIoctl(fd, DRM_IOCTL_GEM_FLINK, &flink)) {
drmModeFreeFB(fb);
continue;
}
open_arg.name = flink.name;
if (drmIoctl(fd, DRM_IOCTL_GEM_OPEN, &open_arg) == 0) {
struct drm_i915_gem_mmap_gtt mmap_arg;
void *ptr;
mmap_arg.handle = open_arg.handle;
if (drmIoctl(fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &mmap_arg) == 0 &&
(ptr = mmap(0, open_arg.size, PROT_READ, MAP_SHARED, fd, mmap_arg.offset)) != (void *)-1) {
cairo_surface_t *surface;
cairo_format_t format;
char name[80];
snprintf(name, sizeof(name), "fb-%d.png", fb->fb_id);
switch (fb->depth) {
case 16: format = CAIRO_FORMAT_RGB16_565; break;
case 24: format = CAIRO_FORMAT_RGB24; break;
case 30: format = CAIRO_FORMAT_RGB30; break;
case 32: format = CAIRO_FORMAT_ARGB32; break;
default: format = CAIRO_FORMAT_INVALID; break;
}
surface = cairo_image_surface_create_for_data(ptr, format,
fb->width, fb->height, fb->pitch);
cairo_surface_write_to_png(surface, name);
cairo_surface_destroy(surface);
munmap(ptr, open_arg.size);
}
drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &open_arg.handle);
}
drmModeFreeFB(fb);
}
return 0;
}
但是此代码直接编译执行在笔者电脑上会有问题,于是笔者边摸索边对其进行改进。最终经过笔者改造后的代码如下:
/*
* Read back all the KMS framebuffers attached to the CRTC and record as PNG.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <errno.h>
#include <xf86drmMode.h>
#include <xf86drm.h>
#include <i915_drm.h>
#include <cairo.h>
#include "intel_io.h"
//#include "drmtest.h"
int main(int argc, char **argv)
{
drmModeResPtr res;
int fd, n;
drmModeConnector *connector = NULL;
drmModeRes *resources = NULL;
drmModeEncoder *encoder = NULL;
drmModeCrtc *crtc = NULL;
struct drm_gem_open open_arg;
struct drm_gem_flink flink;
uint32_t conn_id;
uint32_t crtc_id;
fd = drmOpen("i915", NULL);
//fd = open("/dev/dri/card0", O_RDWR); //with same effect
if (fd < 0)
{
perror("drmOpen failed!");
return ENOENT;
}
res = drmModeGetResources(fd); //获取drmModeRes资源,包含fb、crtc、encoder、connector等
if (res == NULL)
{
perror("drmModeGetResources failed!");
return ENOMEM;
}
for (int i = 0; i < res->count_connectors; ++i)
{
connector = drmModeGetConnector(fd, res->connectors[i]); //根据connector_id获取connector资源
if (connector == NULL || connector->connection != DRM_MODE_CONNECTED)
continue;
for (int j = 0; j < res->count_encoders; ++j)
{
encoder = drmModeGetEncoder(fd, res->encoders[j]);
if (encoder == NULL || encoder->encoder_id != connector->encoder_id)
continue;
break;
}
break;
}
crtc = drmModeGetCrtc(fd, encoder->crtc_id);
uint32_t framebuffer_id = crtc->buffer_id;
drmModeFBPtr fb = drmModeGetFB(fd, crtc->buffer_id);
drmModeFreeCrtc(crtc);
if (fb == NULL)
{
printf("daozhelilema2?\n");
//continue;
}
flink.handle = fb->handle;
if (drmIoctl(fd, DRM_IOCTL_GEM_FLINK, &flink))
{
perror("DRM_IOCTL_GEM_OPEN failed!");
//drmModeFreeFB(fb);
printf("daozhelilema3?\n");
}
open_arg.name = flink.name;
if (drmIoctl(fd, DRM_IOCTL_GEM_OPEN, &open_arg))
{
perror("DRM_IOCTL_GEM_OPEN failed!");
//drmModeFreeFB(fb);
printf("daozhelilema4?\n");
}
struct drm_i915_gem_mmap_gtt mmap_arg;
void *ptr;
mmap_arg.handle = open_arg.handle;
if (drmIoctl(fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &mmap_arg))
{
perror("DRM_IOCTL_I915_GEM_MMAP_GTT failed!");
//drmModeFreeFB(fb);
printf("daozhelilema5?\n");
}
ptr = mmap(NULL, open_arg.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, mmap_arg.offset);
if (ptr == (void *)MAP_FAILED)
{
perror("mmap failed!");
//drmModeFreeFB(fb);
printf("daozhelilema6?\n");
}
unsigned char tmp;
void *dup_ptr = malloc(open_arg.size);
#if 0
for (int i=0; i<open_arg.size; i++)
{
tmp = *((unsigned char *) ptr +i);
//tmp = !tmp;
tmp += 64;
//*((unsigned char *)ptr + i) = tmp;
*((unsigned char *)dup_ptr + i) = tmp;
}
#endif
cairo_surface_t *surface;
cairo_format_t format;
char name[80] = {0};
snprintf(name, sizeof(name), "fb-%d.png", fb->fb_id);
printf("fb->depth is: %d\n", fb->depth);
switch (fb->depth)
{
case 16:
format = CAIRO_FORMAT_RGB16_565;
break;
case 24:
format = CAIRO_FORMAT_RGB24;
break;
case 30:
format = CAIRO_FORMAT_RGB30;
break;
case 32:
format = CAIRO_FORMAT_ARGB32;
break;
default:
format = CAIRO_FORMAT_INVALID;
break;
}
surface = cairo_image_surface_create_for_data(ptr, format, fb->width, fb->height, fb->pitch);
//surface = cairo_image_surface_create_for_data(dup_ptr, format, fb->width, fb->height, fb->pitch);
cairo_surface_write_to_png(surface, name);
cairo_surface_destroy(surface);
FILE *output_file = fopen("./framebuffer.bin", "wb");
if (output_file == NULL)
{
fprintf(stderr, "Unable to open output file\n");
return EXIT_FAILURE;
}
printf("open_arg.size is %d\n", open_arg.size);
//fwrite(framebuffer_data, sizeof(uint8_t), framebuffer_size, output_file);
fwrite(ptr, sizeof(uint8_t), open_arg.size, output_file);
fclose(output_file);
drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &open_arg.handle);
drmModeFreeFB(fb);
return 0;
}
2. 源码编译
通过以下命令进行源码编译:
gcc intel_framebuffer_dump.c -o intel_framebuffer_dump -I/usr/include/drm -I/usr/include/cairo -ldrm -lcairo
最终生成可执行文件intel_framebuffer_dump。
3. 源码执行及结果
执行此可执行文件,得到以下结果:
$ ./intel_framebuffer_dump DRM_IOCTL_GEM_OPEN failed!: Permission denied
daozhelilema3?
DRM_IOCTL_GEM_OPEN failed!: Permission denied
daozhelilema4?
DRM_IOCTL_I915_GEM_MMAP_GTT failed!: No such file or directory
daozhelilema5?
mmap failed!: Invalid argument
daozhelilema6?
fb->depth is: 30
段错误(核心已转储)
需要使用root权限或者sudo执行,运行结果如下:
$ sudo ./intel_framebuffer_dump
fb->depth is: 30
同时,会生成以下图片:
说明截屏功能正常实现。
下一步又该如何走,如何更加深入地定位问题?请看下回。