1 /*
2 * Copyright (C) 2011 Texas Instruments
3 * Author: Rob Clark <rob.clark@linaro.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 */
18 #ifndef UTIL_H_
19 #define UTIL_H_
21 #include <stdio.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <assert.h>
29 #include <omap_drm.h>
30 #include <omap_drmif.h>
32 typedef enum {
33 false = 0,
34 true = 1
35 } bool;
37 #include "list.h"
39 /* Display Interface:
40 *
41 * Could be either KMS or X11 depending on build and
42 * environment. Some of details of opening/connecting, allocating buffers,
43 * etc, differ. The intention is just to provide as simple as possible
44 * abstraction to avoid lots of duplication in each test app to handle
45 * both cases.
46 */
48 struct buffer {
49 uint32_t fourcc, width, height;
50 int nbo;
51 struct omap_bo *bo[4];
52 uint32_t pitches[4];
53 struct list unlocked;
54 };
56 struct display {
57 int fd;
58 uint32_t width, height;
59 struct omap_device *dev;
60 struct list unlocked;
62 struct buffer ** (*get_buffers)(struct display *disp, uint32_t n);
63 struct buffer ** (*get_vid_buffers)(struct display *disp,
64 uint32_t n, uint32_t fourcc, uint32_t w, uint32_t h);
65 int (*post_buffer)(struct display *disp, struct buffer *buf);
66 int (*post_vid_buffer)(struct display *disp, struct buffer *buf,
67 uint32_t x, uint32_t y, uint32_t w, uint32_t h);
68 };
70 /* Print display related help */
71 void disp_usage(void);
73 /* Open display.. X11 or KMS depending on cmdline args, environment,
74 * and build args
75 */
76 struct display * disp_open(int argc, char **argv);
78 /* Get normal RGB/UI buffers (ie. not scaled, not YUV) */
79 static inline struct buffer **
80 disp_get_buffers(struct display *disp, uint32_t n)
81 {
82 return disp->get_buffers(disp, n);
83 }
85 /* Get video/overlay buffers (ie. can be YUV, scaled, etc) */
86 struct buffer ** disp_get_vid_buffers(struct display *disp, uint32_t n,
87 uint32_t fourcc, uint32_t w, uint32_t h);
89 /* flip to / post the specified buffer */
90 static inline int
91 disp_post_buffer(struct display *disp, struct buffer *buf)
92 {
93 return disp->post_buffer(disp, buf);
94 }
96 /* flip to / post the specified video buffer */
97 static inline int
98 disp_post_vid_buffer(struct display *disp, struct buffer *buf,
99 uint32_t x, uint32_t y, uint32_t w, uint32_t h)
100 {
101 return disp->post_vid_buffer(disp, buf, x, y, w, h);
102 }
104 /* allocate a buffer from pool created by disp_get_vid_buffers() */
105 struct buffer * disp_get_vid_buffer(struct display *disp);
106 /* free to video buffer pool */
107 void disp_put_vid_buffer(struct display *disp, struct buffer *buf);
109 /* helper to setup the display for apps that just need video with
110 * no flipchain on the GUI layer
111 */
112 struct buffer * disp_get_fb(struct display *disp);
115 /* V4L2 utilities:
116 */
118 struct v4l2;
120 /* Print v4l2 related help */
121 void v4l2_usage(void);
123 /* Open v4l2 (and media0??) XXX */
124 struct v4l2 * v4l2_open(int argc, char **argv, uint32_t *fourcc,
125 uint32_t *width, uint32_t *height);
127 /* Share the buffers w/ v4l2 via dmabuf */
128 int v4l2_reqbufs(struct v4l2 *v4l2, struct buffer **bufs, uint32_t n);
130 int v4l2_streamon(struct v4l2 *v4l2);
131 int v4l2_streamoff(struct v4l2 *v4l2);
133 /* Queue a buffer to the camera */
134 int v4l2_qbuf(struct v4l2 *v4l2, struct buffer *buf);
136 /* Dequeue buffer from camera */
137 struct buffer * v4l2_dqbuf(struct v4l2 *v4l2);
139 /* Other utilities..
140 */
142 int check_args(int argc, char **argv);
144 void fill(struct buffer *buf, int i);
146 #define FOURCC(a, b, c, d) ((uint32_t)(uint8_t)(a) | ((uint32_t)(uint8_t)(b) << 8) | ((uint32_t)(uint8_t)(c) << 16) | ((uint32_t)(uint8_t)(d) << 24 ))
147 #define FOURCC_STR(str) FOURCC(str[0], str[1], str[2], str[3])
149 #define MSG(fmt, ...) \
150 do { fprintf(stderr, fmt "\n", ##__VA_ARGS__); } while (0)
151 #define ERROR(fmt, ...) \
152 do { fprintf(stderr, "ERROR:%s:%d: " fmt "\n", __func__, __LINE__, ##__VA_ARGS__); } while (0)
154 #ifndef container_of
155 #define container_of(ptr, type, member) \
156 (type *)((char *)(ptr) - (char *) &((type *)0)->member)
157 #endif
159 #ifndef MIN
160 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
161 #endif
163 #ifndef MAX
164 # define MAX(a,b) (((a) > (b)) ? (a) : (b))
165 #endif
167 /* align x to next highest multiple of 2^n */
168 #define ALIGN2(x,n) (((x) + ((1 << (n)) - 1)) & ~((1 << (n)) - 1))
170 #include <sys/time.h>
171 static inline long
172 mark(long *last)
173 {
174 struct timeval t;
175 gettimeofday(&t, NULL);
176 if (last) {
177 return t.tv_usec - *last;
178 }
179 return t.tv_usec;
180 }
182 #endif /* UTIL_H_ */