| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#ifndef __TOOLS_FRAME_H |
| 20 |
#define __TOOLS_FRAME_H |
| 21 |
|
| 22 |
#include "SDL.h" |
| 23 |
#include "list.h" |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
struct frame_queue { |
| 34 |
struct list_head list; |
| 35 |
SDL_mutex *lock; |
| 36 |
size_t len; |
| 37 |
}; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
struct frame { |
| 59 |
struct list_head list; |
| 60 |
void *dev; |
| 61 |
unsigned long timestamp; |
| 62 |
uint8_t cb[64]; |
| 63 |
union { |
| 64 |
struct list_head list; |
| 65 |
uint8_t raw_data[32]; |
| 66 |
} dcb; |
| 67 |
|
| 68 |
unsigned int queue; |
| 69 |
unsigned int ref; |
| 70 |
unsigned int alloced; |
| 71 |
unsigned int total_len; |
| 72 |
unsigned int len; |
| 73 |
uint8_t *head; |
| 74 |
uint8_t *data; |
| 75 |
uint8_t *tail; |
| 76 |
|
| 77 |
|
| 78 |
uint8_t payload[0]; |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
static inline void *frame_put(struct frame *frame, unsigned int size) |
| 91 |
{ |
| 92 |
void *tmp; |
| 93 |
|
| 94 |
BUG_ON(frame->total_len + size > frame->alloced); |
| 95 |
|
| 96 |
frame->len += size; |
| 97 |
frame->total_len += size; |
| 98 |
|
| 99 |
tmp = (void *) frame->tail; |
| 100 |
frame->tail += size; |
| 101 |
|
| 102 |
BUG_ON(frame->tail > (frame->payload + frame->alloced)); |
| 103 |
|
| 104 |
return tmp; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
static inline void *frame_push(struct frame *frame, unsigned int size) |
| 116 |
{ |
| 117 |
frame->len += size; |
| 118 |
frame->data -= size; |
| 119 |
|
| 120 |
BUG_ON(frame->data < frame->payload); |
| 121 |
return frame->data; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
static inline struct frame *frame_get(struct frame *frame) |
| 133 |
{ |
| 134 |
frame->ref++; |
| 135 |
return frame; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
static inline void *frame_pull(struct frame *frame, unsigned int size) |
| 148 |
{ |
| 149 |
BUG_ON(frame->len < size); |
| 150 |
|
| 151 |
frame->len -= size; |
| 152 |
frame->total_len -= size; |
| 153 |
frame->data += size; |
| 154 |
|
| 155 |
return frame->data; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
static inline void frame_reserve(struct frame *frame, unsigned int size) |
| 168 |
{ |
| 169 |
BUG_ON(frame->total_len + size > frame->alloced); |
| 170 |
BUG_ON(frame->len != 0); |
| 171 |
|
| 172 |
frame->total_len += size; |
| 173 |
frame->data += size; |
| 174 |
frame->tail += size; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
static inline void frame_trim(struct frame *frame, unsigned int size) |
| 186 |
{ |
| 187 |
BUG_ON(size > frame->total_len); |
| 188 |
|
| 189 |
frame->len = size; |
| 190 |
frame->total_len = size; |
| 191 |
frame->data = frame->head; |
| 192 |
frame->tail = frame->head + size; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
static inline struct frame *frame_alloc(unsigned int size) |
| 203 |
{ |
| 204 |
struct frame *tmp; |
| 205 |
|
| 206 |
tmp = malloc(size + sizeof(*tmp)); |
| 207 |
if (tmp != NULL) { |
| 208 |
memset(tmp, 0, sizeof(*tmp)); |
| 209 |
init_list_head(&tmp->list); |
| 210 |
init_list_head(&tmp->dcb.list); |
| 211 |
tmp->len = 0; |
| 212 |
tmp->total_len = 0; |
| 213 |
tmp->alloced = size; |
| 214 |
|
| 215 |
tmp->head = tmp->payload; |
| 216 |
tmp->data = tmp->payload; |
| 217 |
tmp->tail = tmp->payload; |
| 218 |
tmp->ref = 1; |
| 219 |
} |
| 220 |
return tmp; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
static inline void frame_free(struct frame *frame) |
| 231 |
{ |
| 232 |
if (!--frame->ref) |
| 233 |
free(frame); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
#define FRAME_WALK(pos, head) \ |
| 248 |
list_for_each_entry((pos), &(head)->list, list) |
| 249 |
|
| 250 |
static inline void __frame_queue_init(struct frame_queue *queue) |
| 251 |
{ |
| 252 |
queue->len = 0; |
| 253 |
init_list_head(&queue->list); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
static inline void frame_queue_init(struct frame_queue *queue) |
| 264 |
{ |
| 265 |
queue->lock = SDL_CreateMutex(); |
| 266 |
__frame_queue_init(queue); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
static inline unsigned int frame_queue_len(struct frame_queue *queue) |
| 275 |
{ |
| 276 |
return queue->len; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
static inline bool frame_queue_empty(struct frame_queue *queue) |
| 285 |
{ |
| 286 |
return list_empty(&queue->list); |
| 287 |
} |
| 288 |
|
| 289 |
static inline void __frame_queue_head(struct frame_queue *queue, struct frame *frame) |
| 290 |
{ |
| 291 |
list_add_head(&frame->list, &queue->list); |
| 292 |
queue->len++; |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
static inline void frame_queue_head(struct frame_queue *queue, struct frame *frame) |
| 300 |
{ |
| 301 |
BUG_ON((SDL_mutexP(queue->lock) != 0)); |
| 302 |
__frame_queue_head(queue, frame); |
| 303 |
SDL_mutexV(queue->lock); |
| 304 |
} |
| 305 |
|
| 306 |
static inline void __frame_queue_tail(struct frame_queue *queue, struct frame *frame) |
| 307 |
{ |
| 308 |
list_add_tail(&frame->list, &queue->list); |
| 309 |
queue->len++; |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
static inline void frame_queue_tail(struct frame_queue *queue, struct frame *frame) |
| 317 |
{ |
| 318 |
BUG_ON((SDL_mutexP(queue->lock) != 0)); |
| 319 |
__frame_queue_tail(queue, frame); |
| 320 |
SDL_mutexV(queue->lock); |
| 321 |
} |
| 322 |
|
| 323 |
static inline void __frame_unlink(struct frame_queue *queue, struct frame *frame) |
| 324 |
{ |
| 325 |
list_del(&frame->list); |
| 326 |
queue->len--; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
static inline void frame_unlink(struct frame_queue *queue, struct frame *frame) |
| 335 |
{ |
| 336 |
BUG_ON((SDL_mutexP(queue->lock) != 0)); |
| 337 |
__frame_unlink(queue, frame); |
| 338 |
SDL_mutexV(queue->lock); |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
static inline struct frame *__frame_dequeue(struct frame_queue *queue) |
| 343 |
{ |
| 344 |
struct frame *tmp = NULL; |
| 345 |
|
| 346 |
if (!frame_queue_empty(queue)) { |
| 347 |
tmp = list_entry(queue->list.next, struct frame, list); |
| 348 |
__frame_unlink(queue, tmp); |
| 349 |
} |
| 350 |
|
| 351 |
return tmp; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
static inline struct frame *frame_dequeue(struct frame_queue *queue) |
| 360 |
{ |
| 361 |
struct frame *tmp; |
| 362 |
|
| 363 |
BUG_ON((SDL_mutexP(queue->lock) != 0)); |
| 364 |
|
| 365 |
tmp = __frame_dequeue(queue); |
| 366 |
SDL_mutexV(queue->lock); |
| 367 |
return tmp; |
| 368 |
} |
| 369 |
|
| 370 |
static inline void __frame_queue_purge(struct frame_queue *queue) |
| 371 |
{ |
| 372 |
while (list_empty(&queue->list) == false) |
| 373 |
frame_free(__frame_dequeue(queue)); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
static inline void frame_queue_purge(struct frame_queue *queue) |
| 382 |
{ |
| 383 |
BUG_ON((SDL_mutexP(queue->lock) != 0)); |
| 384 |
__frame_queue_purge(queue); |
| 385 |
SDL_mutexV(queue->lock); |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
static inline void frame_queue_kill(struct frame_queue *queue) |
| 396 |
{ |
| 397 |
SDL_DestroyMutex(queue->lock); |
| 398 |
__frame_queue_purge(queue); |
| 399 |
} |
| 400 |
|
| 401 |
#endif |