blob: a254324c601a201892c85545051318f9f5df50da [file] [log] [blame]
Chris Wilson176011e2013-08-15 10:55:22 +01001/*
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#define _GNU_SOURCE
30#include <stdint.h>
31#include <sys/types.h>
32#include <sys/mman.h>
33#include <errno.h>
34#include <xf86drmMode.h>
35#include <i915_drm.h>
36#include <cairo.h>
37
38#include "intel_gpu_tools.h"
39#include "drmtest.h"
40
41int main(int argc, char **argv)
42{
43 drmModeResPtr res;
44 int fd, n;
45
46 fd = drmOpen("i915", NULL);
47 if (fd < 0)
48 return ENOENT;
49
50 res = drmModeGetResources(fd);
51 if (res == NULL)
52 return ENOMEM;
53
54 for (n = 0; n < res->count_crtcs; n++) {
55 struct drm_gem_open open_arg;
56 struct drm_gem_flink flink;
57 drmModeCrtcPtr crtc;
58 drmModeFBPtr fb;
59
60 crtc = drmModeGetCrtc(fd, res->crtcs[n]);
61 if (crtc == NULL)
62 continue;
63
64 fb = drmModeGetFB(fd, crtc->buffer_id);
65 drmModeFreeCrtc(crtc);
66 if (fb == NULL)
67 continue;
68
69 flink.handle = fb->handle;
70 if (drmIoctl(fd, DRM_IOCTL_GEM_FLINK, &flink)) {
71 drmModeFreeFB(fb);
72 continue;
73 }
74
75 open_arg.name = flink.name;
76 if (drmIoctl(fd, DRM_IOCTL_GEM_OPEN, &open_arg) == 0) {
77 struct drm_i915_gem_mmap_gtt mmap_arg;
78 void *ptr;
79
80 mmap_arg.handle = open_arg.handle;
81 if (drmIoctl(fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &mmap_arg) == 0 &&
82 (ptr = mmap(0, open_arg.size, PROT_READ, MAP_SHARED, fd, mmap_arg.offset)) != (void *)-1) {
83 cairo_surface_t *surface;
84 cairo_format_t format;
85 char name[80];
86
87 snprintf(name, sizeof(name), "fb-%d.png", fb->fb_id);
88
89 switch (fb->depth) {
90 case 16: format = CAIRO_FORMAT_RGB16_565; break;
91 case 24: format = CAIRO_FORMAT_RGB24; break;
92 case 30: format = CAIRO_FORMAT_RGB30; break;
93 case 32: format = CAIRO_FORMAT_ARGB32; break;
94 default: format = CAIRO_FORMAT_INVALID; break;
95 }
96
97 surface = cairo_image_surface_create_for_data(ptr, format,
98 fb->width, fb->height, fb->pitch);
99 cairo_surface_write_to_png(surface, name);
100 cairo_surface_destroy(surface);
101
102 munmap(ptr, open_arg.size);
103 }
104 drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &open_arg.handle);
105 }
106
107 drmModeFreeFB(fb);
108 }
109
110 return 0;
111}