-
Notifications
You must be signed in to change notification settings - Fork 1
/
framebuffer.hpp
132 lines (95 loc) · 2.74 KB
/
framebuffer.hpp
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
#ifndef FRAMEBUFFER_HPP
#define FRAMEBUFFER_HPP
#include "mpegheader.hpp"
#include "exceptions.hpp"
#include "slicerow.hpp"
#include "opq.hpp"
#include <stdint.h>
#include <pthread.h>
class Frame;
class BufferPool;
class FrameHandle
{
friend class Frame;
private:
BufferPool *pool;
Picture *pic;
Frame *frame;
int locks;
pthread_cond_t activity;
void set_frame( Frame *s_frame );
public:
void increment_lockcount( void );
void decrement_lockcount( void );
bool increment_lockcount_if_renderable( void );
Frame *get_frame( void ) { ahabassert( frame ); return frame; }
Picture *get_picture( void ) { ahabassert( pic ); return pic; }
FrameHandle( BufferPool *s_pool, Picture *s_pic );
~FrameHandle();
void wait_rendered( void );
};
class BufferPool
{
private:
uint num_frames, width, height;
Frame **frames;
Queue<Frame> free;
Queue<Frame> freeable;
pthread_mutex_t mutex;
pthread_cond_t activity;
public:
BufferPool( uint s_num_frames, uint mb_width, uint mb_height );
~BufferPool();
FrameHandle *make_handle( Picture *pic ) { return new FrameHandle( this, pic ); }
Frame *get_free_frame( void );
void make_freeable( Frame *frame );
void make_free( Frame *frame );
void remove_from_freeable( Frame *frame );
pthread_mutex_t *get_mutex( void ) { return &mutex; }
void print_status( void )
{
fprintf( stderr, "free: %d, freeable: %d\n",
free.get_count(), freeable.get_count() );
}
void signal( void ) {
unixassert( pthread_cond_broadcast( &activity ) );
}
void wait( void ) {
unixassert( pthread_cond_wait( &activity, &mutex ) );
}
};
enum FrameState { FREE, LOCKED, RENDERED, FREEABLE };
class Frame
{
private:
BufferPool *pool;
uint width, height;
uint8_t *buf;
FrameState state;
FrameHandle *handle;
Frame *prev, *next;
pthread_cond_t activity;
SliceRow **slicerow;
QueueElement<Frame> *queue_element;
public:
Frame( BufferPool *s_pool, uint mb_width, uint mb_height );
~Frame();
uint8_t *get_buf( void ) { return buf; }
uint8_t *get_y( void ) { return buf; }
uint8_t *get_cb( void ) { return buf + width * height; }
uint8_t *get_cr( void ) { return buf + width * height + width * height / 4; }
void lock( FrameHandle *s_handle,
int f_code_fv, int f_code_bv,
Picture *forward, Picture *backward );
void set_rendered( void );
void set_freeable( void );
void relock( void );
void free( void );
void free_locked( void );
FrameState get_state( void ) { return state; }
void wait_rendered( void );
SliceRow *get_slicerow( uint row ) { return slicerow[ row ]; }
void set_element( QueueElement<Frame> *s_element ) { queue_element = s_element; }
QueueElement<Frame> *get_element( void ) { return queue_element; }
};
#endif