Skip to content

Commit edb60f7

Browse files
authored
[webview_flutter] Refactor the C++ code (#383)
* [webview_flutter] Refactor the C++ code * Update based on reviews * Change types * Remove unused index_ from BufferUnit * Minor type fix
1 parent ee9abc2 commit edb60f7

File tree

8 files changed

+481
-638
lines changed

8 files changed

+481
-638
lines changed

packages/webview_flutter/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## NEXT
2+
3+
* Code refactoring.
4+
15
## 0.4.4
26

37
* Update LWE binary (645719ed084d899ec7d53de1758db71a8974e446).

packages/webview_flutter/tizen/src/buffer_pool.cc

Lines changed: 36 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,7 @@
66

77
#include "log.h"
88

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); }
1810

1911
BufferUnit::~BufferUnit() {
2012
if (tbm_surface_) {
@@ -28,18 +20,14 @@ BufferUnit::~BufferUnit() {
2820
}
2921

3022
bool BufferUnit::MarkInUse() {
31-
if (!isUsed_) {
32-
isUsed_ = true;
23+
if (!is_used_) {
24+
is_used_ = true;
3325
return true;
3426
}
3527
return false;
3628
}
3729

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; }
4331

4432
tbm_surface_h BufferUnit::Surface() {
4533
if (IsUsed()) {
@@ -48,14 +36,13 @@ tbm_surface_h BufferUnit::Surface() {
4836
return nullptr;
4937
}
5038

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) {
5440
if (width_ == width && height_ == height) {
5541
return;
5642
}
5743
width_ = width;
5844
height_ = height;
45+
5946
if (tbm_surface_) {
6047
tbm_surface_destroy(tbm_surface_);
6148
tbm_surface_ = nullptr;
@@ -64,21 +51,22 @@ void BufferUnit::Reset(int width, int height) {
6451
delete gpu_buffer_;
6552
gpu_buffer_ = nullptr;
6653
}
54+
6755
tbm_surface_ = tbm_surface_create(width_, height_, TBM_FORMAT_ARGB8888);
6856
gpu_buffer_ = new FlutterDesktopGpuBuffer();
6957
gpu_buffer_->width = width_;
7058
gpu_buffer_->height = height_;
7159
gpu_buffer_->buffer = tbm_surface_;
7260
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();
7563
};
7664
gpu_buffer_->release_context = this;
7765
}
7866

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));
8270
}
8371
Prepare(width, height);
8472
}
@@ -87,8 +75,8 @@ BufferPool::~BufferPool() {}
8775

8876
BufferUnit* BufferPool::GetAvailableBuffer() {
8977
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();
9280
BufferUnit* buffer = pool_[current].get();
9381
if (buffer->MarkInUse()) {
9482
last_index_ = current;
@@ -98,46 +86,47 @@ BufferUnit* BufferPool::GetAvailableBuffer() {
9886
return nullptr;
9987
}
10088

101-
void BufferPool::Release(BufferUnit* unit) {
89+
void BufferPool::Release(BufferUnit* buffer) {
10290
std::lock_guard<std::mutex> lock(mutex_);
103-
unit->UnmarkInUse();
91+
buffer->UnmarkInUse();
10492
}
10593

106-
void BufferPool::Prepare(int width, int height) {
94+
void BufferPool::Prepare(int32_t width, int32_t height) {
10795
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();
11098
buffer->Reset(width, height);
11199
}
112100
}
113101

114-
SingleBufferPool::SingleBufferPool(int width, int height)
102+
SingleBufferPool::SingleBufferPool(int32_t width, int32_t height)
115103
: BufferPool(width, height, 1) {}
116104

117105
SingleBufferPool::~SingleBufferPool() {}
118106

119107
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;
122111
}
123112

124-
void SingleBufferPool::Release(BufferUnit* unit) {}
113+
void SingleBufferPool::Release(BufferUnit* buffer) {}
125114

126115
#ifndef NDEBUG
127116
#include <cairo.h>
128117
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);
141130

142131
tbm_surface_unmap(tbm_surface_);
143132
cairo_surface_destroy(png_buffer);

packages/webview_flutter/tizen/src/buffer_pool.h

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
#ifndef FLUTTER_PLUGIN_WEBVIEW_FLUTTER_TIZEN_WEBVIEW_BUFFER_POOL_H_
6-
#define FLUTTER_PLUGIN_WEBVIEW_FLUTTER_TIZEN_WEBVIEW_BUFFER_POOL_H_
5+
#ifndef FLUTTER_PLUGIN_BUFFER_POOL_H_
6+
#define FLUTTER_PLUGIN_BUFFER_POOL_H_
77

88
#include <flutter_texture_registrar.h>
99
#include <tbm_surface.h>
@@ -14,52 +14,58 @@
1414

1515
class BufferUnit {
1616
public:
17-
explicit BufferUnit(int index, int width, int height);
17+
explicit BufferUnit(int32_t width, int32_t height);
1818
~BufferUnit();
19-
void Reset(int width, int height);
19+
20+
void Reset(int32_t width, int32_t height);
21+
2022
bool MarkInUse();
2123
void UnmarkInUse();
22-
int Index();
23-
bool IsUsed();
24+
25+
bool IsUsed() { return is_used_ && tbm_surface_; }
26+
2427
tbm_surface_h Surface();
25-
FlutterDesktopGpuBuffer* GpuBuffer();
28+
29+
FlutterDesktopGpuBuffer* GpuBuffer() { return gpu_buffer_; }
30+
2631
#ifndef NDEBUG
32+
// TODO: Unused code.
2733
void DumpToPng(int file_name);
2834
#endif
2935

3036
private:
31-
bool isUsed_;
32-
int index_;
33-
int width_;
34-
int height_;
35-
tbm_surface_h tbm_surface_;
36-
FlutterDesktopGpuBuffer* gpu_buffer_;
37+
bool is_used_ = false;
38+
int32_t width_ = 0;
39+
int32_t height_ = 0;
40+
tbm_surface_h tbm_surface_ = nullptr;
41+
FlutterDesktopGpuBuffer* gpu_buffer_ = nullptr;
3742
};
3843

3944
class BufferPool {
4045
public:
41-
explicit BufferPool(int width, int height, int pool_size);
46+
explicit BufferPool(int32_t width, int32_t height, size_t pool_size);
4247
virtual ~BufferPool();
4348

4449
virtual BufferUnit* GetAvailableBuffer();
45-
virtual void Release(BufferUnit* unit);
46-
void Prepare(int with, int height);
50+
virtual void Release(BufferUnit* buffer);
51+
52+
void Prepare(int32_t with, int32_t height);
4753

4854
protected:
4955
std::vector<std::unique_ptr<BufferUnit>> pool_;
5056

5157
private:
52-
int last_index_;
58+
size_t last_index_ = 0;
5359
std::mutex mutex_;
5460
};
5561

5662
class SingleBufferPool : public BufferPool {
5763
public:
58-
explicit SingleBufferPool(int width, int height);
64+
explicit SingleBufferPool(int32_t width, int32_t height);
5965
~SingleBufferPool();
6066

61-
virtual BufferUnit* GetAvailableBuffer();
62-
virtual void Release(BufferUnit* unit);
67+
virtual BufferUnit* GetAvailableBuffer() override;
68+
virtual void Release(BufferUnit* buffer) override;
6369
};
6470

65-
#endif
71+
#endif // FLUTTER_PLUGIN_BUFFER_POOL_H_

0 commit comments

Comments
 (0)