6
6
7
7
#include " log.h"
8
8
9
- BufferUnit::BufferUnit (int index, int width, int height)
10
- : isUsed_(false ),
11
- index_(index),
12
- width_(0 ),
13
- height_(0 ),
14
- tbm_surface_(nullptr ),
15
- gpu_buffer_(nullptr ) {
16
- Reset (width, height);
17
- }
9
+ BufferUnit::BufferUnit (int32_t width, int32_t height) { Reset (width, height); }
18
10
19
11
BufferUnit::~BufferUnit () {
20
12
if (tbm_surface_) {
@@ -28,18 +20,14 @@ BufferUnit::~BufferUnit() {
28
20
}
29
21
30
22
bool BufferUnit::MarkInUse () {
31
- if (!isUsed_ ) {
32
- isUsed_ = true ;
23
+ if (!is_used_ ) {
24
+ is_used_ = true ;
33
25
return true ;
34
26
}
35
27
return false ;
36
28
}
37
29
38
- FlutterDesktopGpuBuffer* BufferUnit::GpuBuffer () { return gpu_buffer_; }
39
-
40
- int BufferUnit::Index () { return index_; }
41
-
42
- bool BufferUnit::IsUsed () { return isUsed_ && tbm_surface_; }
30
+ void BufferUnit::UnmarkInUse () { is_used_ = false ; }
43
31
44
32
tbm_surface_h BufferUnit::Surface () {
45
33
if (IsUsed ()) {
@@ -48,14 +36,13 @@ tbm_surface_h BufferUnit::Surface() {
48
36
return nullptr ;
49
37
}
50
38
51
- void BufferUnit::UnmarkInUse () { isUsed_ = false ; }
52
-
53
- void BufferUnit::Reset (int width, int height) {
39
+ void BufferUnit::Reset (int32_t width, int32_t height) {
54
40
if (width_ == width && height_ == height) {
55
41
return ;
56
42
}
57
43
width_ = width;
58
44
height_ = height;
45
+
59
46
if (tbm_surface_) {
60
47
tbm_surface_destroy (tbm_surface_);
61
48
tbm_surface_ = nullptr ;
@@ -64,21 +51,22 @@ void BufferUnit::Reset(int width, int height) {
64
51
delete gpu_buffer_;
65
52
gpu_buffer_ = nullptr ;
66
53
}
54
+
67
55
tbm_surface_ = tbm_surface_create (width_, height_, TBM_FORMAT_ARGB8888);
68
56
gpu_buffer_ = new FlutterDesktopGpuBuffer ();
69
57
gpu_buffer_->width = width_;
70
58
gpu_buffer_->height = height_;
71
59
gpu_buffer_->buffer = tbm_surface_;
72
60
gpu_buffer_->release_callback = [](void * release_context) {
73
- BufferUnit* bu = ( BufferUnit*) release_context;
74
- bu ->UnmarkInUse ();
61
+ BufferUnit* buffer = reinterpret_cast < BufferUnit*>( release_context) ;
62
+ buffer ->UnmarkInUse ();
75
63
};
76
64
gpu_buffer_->release_context = this ;
77
65
}
78
66
79
- BufferPool::BufferPool (int width, int height, int pool_size) : last_index_( 0 ) {
80
- for (int idx = 0 ; idx < pool_size; idx ++) {
81
- pool_.emplace_back (new BufferUnit (idx, width, height));
67
+ BufferPool::BufferPool (int32_t width, int32_t height, size_t pool_size) {
68
+ for (size_t index = 0 ; index < pool_size; index ++) {
69
+ pool_.emplace_back (std::make_unique< BufferUnit>( width, height));
82
70
}
83
71
Prepare (width, height);
84
72
}
@@ -87,8 +75,8 @@ BufferPool::~BufferPool() {}
87
75
88
76
BufferUnit* BufferPool::GetAvailableBuffer () {
89
77
std::lock_guard<std::mutex> lock (mutex_);
90
- for (int idx = 0 ; idx < pool_.size (); idx ++) {
91
- int current = (idx + last_index_) % pool_.size ();
78
+ for (size_t index = 0 ; index < pool_.size (); index ++) {
79
+ size_t current = (index + last_index_) % pool_.size ();
92
80
BufferUnit* buffer = pool_[current].get ();
93
81
if (buffer->MarkInUse ()) {
94
82
last_index_ = current;
@@ -98,46 +86,47 @@ BufferUnit* BufferPool::GetAvailableBuffer() {
98
86
return nullptr ;
99
87
}
100
88
101
- void BufferPool::Release (BufferUnit* unit ) {
89
+ void BufferPool::Release (BufferUnit* buffer ) {
102
90
std::lock_guard<std::mutex> lock (mutex_);
103
- unit ->UnmarkInUse ();
91
+ buffer ->UnmarkInUse ();
104
92
}
105
93
106
- void BufferPool::Prepare (int width, int height) {
94
+ void BufferPool::Prepare (int32_t width, int32_t height) {
107
95
std::lock_guard<std::mutex> lock (mutex_);
108
- for (int idx = 0 ; idx < pool_.size (); idx ++) {
109
- BufferUnit* buffer = pool_[idx ].get ();
96
+ for (size_t index = 0 ; index < pool_.size (); index ++) {
97
+ BufferUnit* buffer = pool_[index ].get ();
110
98
buffer->Reset (width, height);
111
99
}
112
100
}
113
101
114
- SingleBufferPool::SingleBufferPool (int width, int height)
102
+ SingleBufferPool::SingleBufferPool (int32_t width, int32_t height)
115
103
: BufferPool(width, height, 1 ) {}
116
104
117
105
SingleBufferPool::~SingleBufferPool () {}
118
106
119
107
BufferUnit* SingleBufferPool::GetAvailableBuffer () {
120
- pool_[0 ].get ()->MarkInUse ();
121
- return pool_[0 ].get ();
108
+ BufferUnit* buffer = pool_[0 ].get ();
109
+ buffer->MarkInUse ();
110
+ return buffer;
122
111
}
123
112
124
- void SingleBufferPool::Release (BufferUnit* unit ) {}
113
+ void SingleBufferPool::Release (BufferUnit* buffer ) {}
125
114
126
115
#ifndef NDEBUG
127
116
#include < cairo.h>
128
117
void BufferUnit::DumpToPng (int file_name) {
129
- char filePath [256 ];
130
- sprintf (filePath , " /tmp/dump%d.png" , file_name);
131
- tbm_surface_info_s surfaceInfo;
132
- tbm_surface_map (tbm_surface_, TBM_SURF_OPTION_WRITE, &surfaceInfo) ;
133
- void * buffer = surfaceInfo. planes [ 0 ]. ptr ;
134
-
135
- cairo_surface_t * png_buffer ;
136
- png_buffer = cairo_image_surface_create_for_data (
137
- ( unsigned char *) buffer, CAIRO_FORMAT_ARGB32, width_, height_,
138
- surfaceInfo .planes [0 ].stride );
139
-
140
- cairo_surface_write_to_png (png_buffer, filePath );
118
+ char file_path [256 ];
119
+ sprintf (file_path , " /tmp/dump%d.png" , file_name);
120
+
121
+ tbm_surface_info_s surface_info ;
122
+ tbm_surface_map (tbm_surface_, TBM_SURF_OPTION_WRITE, &surface_info) ;
123
+
124
+ unsigned char * buffer = surface_info. planes [ 0 ]. ptr ;
125
+ cairo_surface_t * png_buffer = cairo_image_surface_create_for_data (
126
+ buffer, CAIRO_FORMAT_ARGB32, width_, height_,
127
+ surface_info .planes [0 ].stride );
128
+
129
+ cairo_surface_write_to_png (png_buffer, file_path );
141
130
142
131
tbm_surface_unmap (tbm_surface_);
143
132
cairo_surface_destroy (png_buffer);
0 commit comments