summaryrefslogtreecommitdiff
path: root/tests/kms/libkms-test-framebuffer.c
blob: bc96fb93969ad2f8c23e1a8e9a5448f7f2fb1719 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * Copyright © 2014 NVIDIA 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.
 */

#include <errno.h>
#include <string.h>

#include <sys/mman.h>

#include <drm_fourcc.h>

#include "xf86drm.h"

#include "libkms-test.h"

struct kms_framebuffer *kms_framebuffer_create(struct kms_device *device,
					       unsigned int width,
					       unsigned int height,
					       uint32_t format)
{
	uint32_t handles[4], pitches[4], offsets[4];
	struct drm_mode_create_dumb args;
	struct kms_framebuffer *fb;
	int err;

	fb = calloc(1, sizeof(*fb));
	if (!fb)
		return NULL;

	fb->device = device;
	fb->width = width;
	fb->height = height;
	fb->format = format;

	memset(&args, 0, sizeof(args));
	args.width = width;
	args.height = height;

	switch (format) {
	case DRM_FORMAT_XRGB8888:
	case DRM_FORMAT_XBGR8888:
	case DRM_FORMAT_ARGB8888:
	case DRM_FORMAT_ABGR8888:
	case DRM_FORMAT_RGBA8888:
		args.bpp = 32;
		break;

	case DRM_FORMAT_ARGB1555:
	case DRM_FORMAT_BGRA5551:
	case DRM_FORMAT_RGB565:
		args.bpp = 16;
		break;

	default:
		free(fb);
		return NULL;
	}

	err = drmIoctl(device->fd, DRM_IOCTL_MODE_CREATE_DUMB, &args);
	if (err < 0) {
		free(fb);
		return NULL;
	}

	fb->handle = args.handle;
	fb->pitch = args.pitch;
	fb->size = args.size;

	handles[0] = fb->handle;
	pitches[0] = fb->pitch;
	offsets[0] = 0;

	err = drmModeAddFB2(device->fd, width, height, format, handles,
			    pitches, offsets, &fb->id, 0);
	if (err < 0) {
		kms_framebuffer_free(fb);
		return NULL;
	}

	return fb;
}

void kms_framebuffer_free(struct kms_framebuffer *fb)
{
	struct kms_device *device = fb->device;
	struct drm_mode_destroy_dumb args;
	int err;

	if (fb->id) {
		err = drmModeRmFB(device->fd, fb->id);
		if (err < 0) {
			/* not much we can do now */
		}
	}

	memset(&args, 0, sizeof(args));
	args.handle = fb->handle;

	err = drmIoctl(device->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &args);
	if (err < 0) {
		/* not much we can do now */
	}

	free(fb);
}

int kms_framebuffer_map(struct kms_framebuffer *fb, void **ptrp)
{
	struct kms_device *device = fb->device;
	struct drm_mode_map_dumb args;
	void *ptr;
	int err;

	if (fb->ptr) {
		*ptrp = fb->ptr;
		return 0;
	}

	memset(&args, 0, sizeof(args));
	args.handle = fb->handle;

	err = drmIoctl(device->fd, DRM_IOCTL_MODE_MAP_DUMB, &args);
	if (err < 0)
		return -errno;

	ptr = mmap(0, fb->size, PROT_READ | PROT_WRITE, MAP_SHARED,
		   device->fd, args.offset);
	if (ptr == MAP_FAILED)
		return -errno;

	*ptrp = fb->ptr = ptr;

	return 0;
}

void kms_framebuffer_unmap(struct kms_framebuffer *fb)
{
	if (fb->ptr) {
		munmap(fb->ptr, fb->size);
		fb->ptr = NULL;
	}
}