summaryrefslogtreecommitdiff
path: root/tools/intel_framebuffer_dump.c
blob: a254324c601a201892c85545051318f9f5df50da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * 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_gpu_tools.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;
}