363ad72c1ecb7051d1a465aad7fdcc6b6cfb0115
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 /* align x to next highest multiple of 2^n */
33 #define ALIGN2(x,n) (((x) + ((1 << (n)) - 1)) & ~((1 << (n)) - 1))
35 typedef enum {
36 false = 0,
37 true = 1
38 } bool;
40 #include "list.h"
42 /* Display Interface:
43 *
44 * Could be either KMS or X11 depending on build and
45 * environment. Some of details of opening/connecting, allocating buffers,
46 * etc, differ. The intention is just to provide as simple as possible
47 * abstraction to avoid lots of duplication in each test app to handle
48 * both cases.
49 */
51 struct buffer {
52 uint32_t fourcc, width, height;
53 int nbo;
54 struct omap_bo *bo[4];
55 uint32_t pitches[4];
56 struct list unlocked;
57 };
59 /* State variables, used to maintain the playback rate. */
60 struct rate_control {
61 int fps; /* When > zero, we maintain playback rate. */
62 long last_frame_mark; /* The time when the last frame was displayed,
63 * as returned by the mark() function. */
64 int usecs_to_sleep; /* Number of useconds we have slep last frame. */
65 };
67 struct display {
68 int fd;
69 uint32_t width, height;
70 struct omap_device *dev;
71 struct list unlocked;
72 struct rate_control rtctl;
74 struct buffer ** (*get_buffers)(struct display *disp, uint32_t n);
75 struct buffer ** (*get_vid_buffers)(struct display *disp,
76 uint32_t n, uint32_t fourcc, uint32_t w, uint32_t h);
77 int (*post_buffer)(struct display *disp, struct buffer *buf);
78 int (*post_vid_buffer)(struct display *disp, struct buffer *buf,
79 uint32_t x, uint32_t y, uint32_t w, uint32_t h);
80 };
82 /* Print display related help */
83 void disp_usage(void);
85 /* Open display.. X11 or KMS depending on cmdline args, environment,
86 * and build args
87 */
88 struct display * disp_open(int argc, char **argv);
90 /* Close display */
91 void disp_close(struct display *disp);
93 /* Get normal RGB/UI buffers (ie. not scaled, not YUV) */
94 static inline struct buffer **
95 disp_get_buffers(struct display *disp, uint32_t n)
96 {
97 return disp->get_buffers(disp, n);
98 }
100 /* Get video/overlay buffers (ie. can be YUV, scaled, etc) */
101 struct buffer ** disp_get_vid_buffers(struct display *disp, uint32_t n,
102 uint32_t fourcc, uint32_t w, uint32_t h);
104 /* flip to / post the specified buffer */
105 int
106 disp_post_buffer(struct display *disp, struct buffer *buf);
108 /* flip to / post the specified video buffer */
109 int
110 disp_post_vid_buffer(struct display *disp, struct buffer *buf,
111 uint32_t x, uint32_t y, uint32_t w, uint32_t h);
113 /* allocate a buffer from pool created by disp_get_vid_buffers() */
114 struct buffer * disp_get_vid_buffer(struct display *disp);
115 /* free to video buffer pool */
116 void disp_put_vid_buffer(struct display *disp, struct buffer *buf);
118 /* helper to setup the display for apps that just need video with
119 * no flipchain on the GUI layer
120 */
121 struct buffer * disp_get_fb(struct display *disp);
124 /* V4L2 utilities:
125 */
127 struct v4l2;
129 /* Print v4l2 related help */
130 void v4l2_usage(void);
132 /* Open v4l2 (and media0??) XXX */
133 struct v4l2 * v4l2_open(int argc, char **argv, uint32_t *fourcc,
134 uint32_t *width, uint32_t *height);
136 /* Share the buffers w/ v4l2 via dmabuf */
137 int v4l2_reqbufs(struct v4l2 *v4l2, struct buffer **bufs, uint32_t n);
139 int v4l2_streamon(struct v4l2 *v4l2);
140 int v4l2_streamoff(struct v4l2 *v4l2);
142 /* Queue a buffer to the camera */
143 int v4l2_qbuf(struct v4l2 *v4l2, struct buffer *buf);
145 /* Dequeue buffer from camera */
146 struct buffer * v4l2_dqbuf(struct v4l2 *v4l2);
148 /* Other utilities..
149 */
150 extern int debug;
152 int check_args(int argc, char **argv);
154 void fill(struct buffer *buf, int i);
156 #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 ))
157 #define FOURCC_STR(str) FOURCC(str[0], str[1], str[2], str[3])
159 /* Dynamic debug. */
160 #define DBG(fmt, ...) \
161 do { if (debug) fprintf(stderr, fmt "\n", ##__VA_ARGS__); } while (0)
163 #define MSG(fmt, ...) \
164 do { fprintf(stderr, fmt "\n", ##__VA_ARGS__); } while (0)
165 #define ERROR(fmt, ...) \
166 do { fprintf(stderr, "ERROR:%s:%d: " fmt "\n", __func__, __LINE__, ##__VA_ARGS__); } while (0)
168 #ifndef container_of
169 #define container_of(ptr, type, member) \
170 (type *)((char *)(ptr) - (char *) &((type *)0)->member)
171 #endif
173 #ifndef MIN
174 # define MIN(a,b) (((a) < (b)) ? (a) : (b))
175 #endif
177 #ifndef MAX
178 # define MAX(a,b) (((a) > (b)) ? (a) : (b))
179 #endif
181 #ifndef PAGE_SHIFT
182 # define PAGE_SHIFT 12
183 #endif
185 #ifndef PAGE_SIZE
186 # define PAGE_SIZE (1 << PAGE_SHIFT)
187 #endif
189 /* align x to next highest multiple of 2^n */
190 #define ALIGN2(x,n) (((x) + ((1 << (n)) - 1)) & ~((1 << (n)) - 1))
192 #include <sys/time.h>
193 static inline long
194 mark(long *last)
195 {
196 struct timeval t;
197 gettimeofday(&t, NULL);
198 if (last) {
199 long delta = t.tv_usec - *last;
201 /* Handle the case, where the seconds have changed.
202 * TODO: keep the whole timeval struct, to be able to cope with
203 * more than one second deltas? */
204 if (t.tv_usec < *last)
205 delta += 1000000;
207 *last = t.tv_usec;
208 return delta;
209 }
210 return t.tv_usec;
211 }
213 #endif /* UTIL_H_ */