-
Notifications
You must be signed in to change notification settings - Fork 37
/
framebuffer.h
88 lines (87 loc) · 2.43 KB
/
framebuffer.h
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
#ifndef _FRAMEBUFFER_H_
#define _FRAMEBUFFER_H_
#include "block.h"
class FrameBuffer {
Block *image_;
uint32_t width_;
uint32_t height_;
uint32_t nblocks_;
BlockMeta *meta_;
uint8_t *storage_;
uint8_t *meta_storage_;
uint8_t *mb_types_;
uint16_t *cbp_;
int frame_num_;
FrameBuffer(const FrameBuffer &other) = delete;
FrameBuffer& operator=(const FrameBuffer&other) = delete;
void destroy() {
if (width_ && height_) {
free(storage_);
free(meta_storage_);
}
memset(this, 0, sizeof(*this));
}
public:
FrameBuffer() {
image_ = nullptr;
storage_ = nullptr;
width_ = 0;
height_ = 0;
nblocks_ = 0;
}
void bzero() {
memset(meta_, 0, sizeof(BlockMeta) * nblocks_);
memset(image_, 0, sizeof(Block) * nblocks_);
}
void set_frame_num(int frame_num) {
frame_num_ = frame_num;
}
bool is_same_frame(int frame_num) const {
return frame_num_ == frame_num && width_ != 0 && height_ != 0;
}
uint32_t width()const {
return width_;
}
uint32_t height()const {
return height_;
}
void init(uint32_t width, uint32_t height, uint32_t nblocks) {
height_ = height;
width_ = width;
nblocks_ = width * height;
storage_ = (uint8_t*)malloc(nblocks_ * sizeof(Block) + 31);
meta_storage_ = (uint8_t*)malloc(nblocks_ * sizeof(BlockMeta) + 31);
size_t offset = storage_ - (uint8_t *)nullptr;
if (offset & 32) {
image_ = (Block*)(storage_ + 32 - (offset &31));
} else { // already aligned
image_ = (Block*)storage_;
}
offset = meta_storage_ - (uint8_t *)nullptr;
if (offset & 32) {
meta_ = (BlockMeta*)(meta_storage_ + 32 - (offset &31));
} else { // already aligned
meta_ = (BlockMeta*)meta_storage_;
}
bzero();
}
~FrameBuffer() {
destroy();
}
size_t block_allocated() const {
return nblocks_;
}
Block& at(uint32_t x, uint32_t y) {
return image_[x + y * width_];
}
const Block& at(uint32_t x, uint32_t y) const{
return image_[x + y * width_];
}
BlockMeta& meta_at(uint32_t x, uint32_t y) {
return meta_[x + y * width_];
}
const BlockMeta& meta_at(uint32_t x, uint32_t y) const{
return meta_[x + y * width_];
}
};
#endif