1 /*
2 * Copyright © 2013 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 */
24
25 /*
26 * Read back all the KMS framebuffers attached to the CRTC and record as PNG.
27 */
28
29 #include <stdint.h>
30 #include <sys/types.h>
31 #include <sys/mman.h>
32 #include <errno.h>
33 #include <xf86drmMode.h>
34 #include <i915_drm.h>
35 #include <cairo.h>
36
37 #include "intel_io.h"
38 #include "drmtest.h"
39
main(int argc,char ** argv)40 int main(int argc, char **argv)
41 {
42 drmModeResPtr res;
43 int fd, n;
44
45 fd = drmOpen("i915", NULL);
46 if (fd < 0)
47 return ENOENT;
48
49 res = drmModeGetResources(fd);
50 if (res == NULL)
51 return ENOMEM;
52
53 for (n = 0; n < res->count_crtcs; n++) {
54 struct drm_gem_open open_arg;
55 struct drm_gem_flink flink;
56 drmModeCrtcPtr crtc;
57 drmModeFBPtr fb;
58
59 crtc = drmModeGetCrtc(fd, res->crtcs[n]);
60 if (crtc == NULL)
61 continue;
62
63 fb = drmModeGetFB(fd, crtc->buffer_id);
64 drmModeFreeCrtc(crtc);
65 if (fb == NULL)
66 continue;
67
68 flink.handle = fb->handle;
69 if (drmIoctl(fd, DRM_IOCTL_GEM_FLINK, &flink)) {
70 drmModeFreeFB(fb);
71 continue;
72 }
73
74 open_arg.name = flink.name;
75 if (drmIoctl(fd, DRM_IOCTL_GEM_OPEN, &open_arg) == 0) {
76 struct drm_i915_gem_mmap_gtt mmap_arg;
77 void *ptr;
78
79 mmap_arg.handle = open_arg.handle;
80 if (drmIoctl(fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &mmap_arg) == 0 &&
81 (ptr = mmap(0, open_arg.size, PROT_READ, MAP_SHARED, fd, mmap_arg.offset)) != (void *)-1) {
82 cairo_surface_t *surface;
83 cairo_format_t format;
84 char name[80];
85
86 snprintf(name, sizeof(name), "fb-%d.png", fb->fb_id);
87
88 switch (fb->depth) {
89 case 16: format = CAIRO_FORMAT_RGB16_565; break;
90 case 24: format = CAIRO_FORMAT_RGB24; break;
91 case 30: format = CAIRO_FORMAT_RGB30; break;
92 case 32: format = CAIRO_FORMAT_ARGB32; break;
93 default: format = CAIRO_FORMAT_INVALID; break;
94 }
95
96 surface = cairo_image_surface_create_for_data(ptr, format,
97 fb->width, fb->height, fb->pitch);
98 cairo_surface_write_to_png(surface, name);
99 cairo_surface_destroy(surface);
100
101 munmap(ptr, open_arg.size);
102 }
103 drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &open_arg.handle);
104 }
105
106 drmModeFreeFB(fb);
107 }
108
109 return 0;
110 }
111