summaryrefslogtreecommitdiff
path: root/v4l2-mfc-example/drm.c
blob: c04595dbce0734c357d4f355d6ba484fd49b3af7 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 * V4L2 Codec decoding example application
 * Kamil Debski <k.debski@samsung.com>
 * Mateusz Krawczuk <m.krawczuk@samsung.com>
 *
 * DRM operations
 *
 * Copyright 2014 - 2015 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "common.h"
#include "drm.h"
#include <string.h>
#include <errno.h>
#include <drm/drm_fourcc.h>

#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))

struct type_name {
	int type;
	char *name;
};

#define type_name_fn(res) \
char * res##_str(int type) {			\
	int i;						\
	for (i = 0; i < ARRAY_SIZE(res##_names); i++) { \
		if (res##_names[i].type == type)	\
			return res##_names[i].name;	\
	}						\
	return "(invalid)";				\
}

#ifdef DRM

#include <xf86drm.h>
#include <xf86drmMode.h>
#include <drm/exynos_drm.h>
#include "gem.h"
#include "ipp.h"

struct type_name encoder_type_names[] = {
	{ DRM_MODE_ENCODER_NONE, "none" },
	{ DRM_MODE_ENCODER_DAC, "DAC" },
	{ DRM_MODE_ENCODER_TMDS, "TMDS" },
	{ DRM_MODE_ENCODER_LVDS, "LVDS" },
	{ DRM_MODE_ENCODER_TVDAC, "TVDAC" },
};

type_name_fn(encoder_type)

struct type_name connector_status_names[] = {
	{ DRM_MODE_CONNECTED, "connected" },
	{ DRM_MODE_DISCONNECTED, "disconnected" },
	{ DRM_MODE_UNKNOWNCONNECTION, "unknown" },
};

type_name_fn(connector_status)

struct type_name connector_type_names[] = {
	{ DRM_MODE_CONNECTOR_Unknown, "unknown" },
	{ DRM_MODE_CONNECTOR_VGA, "VGA" },
	{ DRM_MODE_CONNECTOR_DVII, "DVI-I" },
	{ DRM_MODE_CONNECTOR_DVID, "DVI-D" },
	{ DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
	{ DRM_MODE_CONNECTOR_Composite, "composite" },
	{ DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
	{ DRM_MODE_CONNECTOR_LVDS, "LVDS" },
	{ DRM_MODE_CONNECTOR_Component, "component" },
	{ DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN" },
	{ DRM_MODE_CONNECTOR_DisplayPort, "displayport" },
	{ DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
	{ DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
	{ DRM_MODE_CONNECTOR_TV, "TV" },
	{ DRM_MODE_CONNECTOR_eDP, "embedded displayport" },
};

type_name_fn(connector_type)

int dump_encoders(struct instance *i);
int dump_connectors(struct instance *i);
int dump_crtcs(struct instance *i);
int dump_framebuffers(struct instance *i);


/*
 * drm_open calls functions to detect DRM parameters and setup gem and display.
 */
int drm_open(struct instance *i)
{
	char mode[32];
	char *name = "exynos";
	drmModeCrtc *crtc;
	struct drm_gem_close args;
	drmModeConnector *connector;
	drmModeModeInfo *mode_drm;
	unsigned int fb_id;
	int ret;
	int n;
	void *map = NULL;

/*
 *Open drm file descriptor
 */
	i->drm.fd =drmOpen(name, NULL);
	if (i->drm.fd < 0) {
		err("Failed to open drm module \"%s\"", name);
		return -1;
	}	i->drm.resources = drmModeGetResources(i->drm.fd);
/*
 *Auto detect Crtc and Connectors
 */

	if(i->drm.autodetect) {
		i->drm.crtc_id = dump_crtcs(i);
		i->drm.conn_id = dump_connectors(i);
	}
	printf("Conn_id:%i \n",i->drm.conn_id);
	printf("Crtc_id:%i \n",i->drm.crtc_id);

/*
 *Detect screen width and height
 */

	crtc = drmModeGetCrtc(i->drm.fd, i->drm.crtc_id);
	if (!crtc) {
		err("Failed to get crtc: %d", i->drm.crtc_id);
		return -1;
	}

	i->drm.width = crtc->width;
	i->drm.height = crtc->height;

	printf("DRM crtc (%d) resolution: %dx%d @", i->drm.crtc_id, i->drm.width,
							i->drm.height);
	drmModeFreeCrtc(crtc);

	for (n = 0; n < 3; n++) {
		i->drm.gem[n].size = i->drm.width * i->drm.height * 4;
		ret = ioctl(i->drm.fd, DRM_IOCTL_EXYNOS_GEM_CREATE,
								&i->drm.gem[n]);
		if (ret < 0) {
			   fprintf(stderr,
                    "Failed to Create Gem %s\n", strerror(errno));
			return -1;
		}
		args.handle = i->drm.gem[n].handle;

		 if (i->fimc.dmabuf)
			ret = drmPrimeHandleToFD(i->drm.fd, i->drm.gem[n].handle, DRM_CLOEXEC, &i->fimc.dbuf[n]);
			if (ret < 0) {
				ioctl(i->drm.fd, DRM_IOCTL_GEM_CLOSE, &args);
				err("Failed to expbuf a gem object");
				return -1;
			}
		ret = drmModeAddFB(i->drm.fd, i->drm.width, i->drm.height, 32,
			32, 4 *  i->drm.width, i->drm.gem[n].handle, &fb_id);
		if (ret) {
			munmap((void *)(unsigned long)map,i->drm.gem[n].size);
			ioctl(i->drm.fd, DRM_IOCTL_GEM_CLOSE, &args);
			err("Failed to add fb");
			return -1;
		}
		i->drm.fb[n] = fb_id;
	}

	snprintf(mode, 32, "%dx%d", crtc->width, crtc->height);
/*
 * Use detected Connector
 */
	connector = drmModeGetConnector(i->drm.fd, i->drm.conn_id);
	if (!connector || connector->connector_id != i->drm.conn_id) {
		err("Couldn't find the connector");
		drmModeFreeConnector(connector);
		return -1;
	}

	mode_drm = 0;

	for (n = 0; n < connector->count_modes; n++) {
		if (!strncmp(connector->modes[n].name, mode, 32)) {
			mode_drm = &connector->modes[n];
			break;
		}
	}

	if (!mode_drm) {
		err("Failed to find mode\n");
		drmModeFreeConnector(connector);
		return -1;
	}
/*
 * Use detected Crtc
 */
	ret = drmModeSetCrtc(i->drm.fd, i->drm.crtc_id, i->drm.fb[0], 0, 0,
		&i->drm.conn_id, 1, mode_drm);
	if (ret) {
		err("Failed to set crtc");
		drmModeFreeConnector(connector);
		return -1;
	}
/*
 * Setup fimc if DRM IPP is not enabled
 */
	if(i->fimc.enabled) {
	i->fimc.width = i->drm.width;
	i->fimc.height = i->drm.height;
	i->fimc.stride = i->drm.width * 4;
	i->fimc.bpp = 32;
	i->fimc.buffers = (i->fimc.double_buf?2:1);
	i->fimc.size = i->drm.width * i->drm.height * 4;
	}
	i->drm.crtc[0] = i->drm.crtc_id;
	i->drm.crtc[1] = i->drm.crtc_id;

	if(i->ipp.enabled) {
	exynos_drm_ipp_setup(i);

	}

	return 0;
}

/*
 * Closing DRM and IPP
 */
void drm_close(struct instance *i)
{
	drmClose(i->drm.fd);
	exynos_drm_ipp_close(i);
}
/*
 * Requise instance to get drm file descriptor and put data to struct
 */
int dump_encoders(struct instance *i)
{
	drmModeEncoder *encoder;
	int j;

	printf("Encoders:\n");
	printf("id\tcrtc\ttype\tpossible crtcs\tpossible clones\t\n");
	for (j = 0; j < i->drm.resources->count_encoders; j++) {
		encoder = drmModeGetEncoder(i->drm.fd, i->drm.resources->encoders[j]);

		if (!encoder) {
			fprintf(stderr, "could not get encoder %i: %s\n",
				i->drm.resources->encoders[j], strerror(errno));
			continue;
		}
		printf("%d\t%d\t%s\t0x%08x\t0x%08x\n",
		       encoder->encoder_id,
		       encoder->crtc_id,
		       encoder_type_str(encoder->encoder_type),
		       encoder->possible_crtcs,
		       encoder->possible_clones);
		drmModeFreeEncoder(encoder);
	}
	printf("\n");

	return 0;
}

int dump_connectors(struct instance *i)
{
	drmModeConnector *connector;
	int g , j;

	printf("Connectors:\n");
	printf("id\tencoder\tstatus\t\ttype\tsize (mm)\tmodes\tencoders\n");
	for (g = 0; g < i->drm.resources->count_connectors; g++) {
		connector = drmModeGetConnector(i->drm.fd, i->drm.resources->connectors[g]);

		if (!connector) {
			fprintf(stderr, "could not get connector %i: %s\n",
				i->drm.resources->connectors[g], strerror(errno));
			continue;
		}

		printf("%d\t%d\t%s\t%s\t%dx%d\t\t%d\t",
		       connector->connector_id,
		       connector->encoder_id,
		       connector_status_str(connector->connection),
		       connector_type_str(connector->connector_type),
		       connector->mmWidth, connector->mmHeight,
		       connector->count_modes);

		for (j = 0; j < connector->count_encoders; j++)
			printf("%s%d", j > 0 ? ", " : "",
							connector->encoders[j]);
		printf("\n");

		if (!connector->count_modes)
			continue;

		printf("  modes:\n");
		printf("  name refresh (Hz) hdisp hss hse htot vdisp "
		       "vss vse vtot)\n");
		printf("  props:\n");
	return connector->connector_id;

		drmModeFreeConnector(connector);
	}
	printf("\n");

	return 0;
}

int dump_crtcs(struct instance *i)
{
	drmModeCrtc *crtc;
	int j,ret;

	printf("CRTCs:\n");
	printf("id\tfb\tpos\tsize\n");
	for (j = 0; j < i->drm.resources->count_crtcs; j++) {
		crtc = drmModeGetCrtc(i->drm.fd, i->drm.resources->crtcs[j]);

		if (!crtc) {
			fprintf(stderr, "could not get crtc %i: %s\n",
				i->drm.resources->crtcs[j], strerror(errno));
			continue;
		}
		ret = crtc->crtc_id;
		printf("%d\t%d\t(%d,%d)\t(%dx%d)\n",
			crtc->crtc_id,
			crtc->buffer_id,
			crtc->x, crtc->y,
			crtc->width, crtc->height);
		drmModeFreeCrtc(crtc);
	}
	printf("%i %i \n",crtc->crtc_id, ret);
	return ret;
}

int dump_framebuffers(struct instance *i)
{
	drmModeFB *fb;
	int j;

	printf("Frame buffers:\n");
	printf("id\tsize\tpitch\n");
	for (j = 0; j < i->drm.resources->count_fbs; j++) {
		fb = drmModeGetFB(i->drm.fd, i->drm.resources->fbs[j]);

		if (!fb) {
			fprintf(stderr, "could not get fb %i: %s\n",
				i->drm.resources->fbs[j], strerror(errno));
			continue;
		}
		printf("%u\t(%ux%u)\t%u\n",
			fb->fb_id,
			fb->width, fb->height,
			fb->pitch);

		drmModeFreeFB(fb);
	}
	printf("\n");

	return 0;
}

#else /*if DRM is Disabled*/

int drm_open(struct instance *i)
{
	return 0;
};

void drm_close(struct instance *i) {
	return;
}

#endif /* DRM */