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
|
#include "buffer.h"
#define assert(int )
int animation_buffer_init(animation_buffer * buf, int size, int count, ISzAlloc *alloc)
{
buf->buffer = buf->w_pos = buf->r_pos = alloc->Alloc(alloc, size * count);
if(!buf->buffer)
return -1;
buf->frame_count = count;
buf->frame_size = size;
buf->eof = 0;
sema_init(&buf->sem_writable, count);
sema_init(&buf->sem_readable, 0);
return 0;
}
int animation_buffer_release(animation_buffer * buf, ISzAlloc *alloc)
{
if (buf->buffer)
alloc->Free(alloc, buf->buffer);
buf->buffer = buf->w_pos = buf->r_pos = NULL;
return 0;
}
unsigned char * animation_buffer_get_writable(animation_buffer * buf, unsigned int * pSize)
{
down_interruptible(&buf->sem_writable);
*pSize = buf->frame_size;
return buf->w_pos;
}
int animation_buffer_stop(animation_buffer * buf)
{
up(&buf->sem_readable);
/* up twice for safey*/
up(&buf->sem_writable);
up(&buf->sem_writable);
return 0;
}
void animation_buffer_write_finish(animation_buffer * buf, unsigned char * addr)
{
assert(addr == buf->w_pos);
// printk(KERN_INFO "add one buffer 0x%p\n", addr);
buf->w_pos += buf->frame_size;
if (buf->w_pos - buf->buffer == buf->frame_size * buf->frame_count)
buf->w_pos = buf->buffer;
up(&buf->sem_readable);
// printk(KERN_INFO "write <<<<<<<======\n");
return;
}
unsigned char * animation_buffer_get_readable(animation_buffer * buf)
{
int ret = down_trylock(&buf->sem_readable);
if (ret == 0) {
return buf->r_pos;
}
else {
return NULL;
}
}
void animation_buffer_read_finish(animation_buffer * buf, unsigned char * addr)
{
// Integrity check
assert(addr == buf->r_pos);
// printk(KERN_INFO "read one buffer 0x%p\n", addr);
buf->r_pos += buf->frame_size;
if (buf->r_pos - buf->buffer == buf->frame_size * buf->frame_count)
buf->r_pos = buf->buffer;
up(&buf->sem_writable);
}
|