Skip to content

Commit da8b248

Browse files
committed
[webview_flutter] Refactor the C++ code
1 parent 02bad9b commit da8b248

File tree

8 files changed

+465
-619
lines changed

8 files changed

+465
-619
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: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +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) {
9+
BufferUnit::BufferUnit(int index, int width, int height) : index_(index) {
1610
Reset(width, height);
1711
}
1812

@@ -28,18 +22,14 @@ BufferUnit::~BufferUnit() {
2822
}
2923

3024
bool BufferUnit::MarkInUse() {
31-
if (!isUsed_) {
32-
isUsed_ = true;
25+
if (!is_used_) {
26+
is_used_ = true;
3327
return true;
3428
}
3529
return false;
3630
}
3731

38-
FlutterDesktopGpuBuffer* BufferUnit::GpuBuffer() { return gpu_buffer_; }
39-
40-
int BufferUnit::Index() { return index_; }
41-
42-
bool BufferUnit::IsUsed() { return isUsed_ && tbm_surface_; }
32+
void BufferUnit::UnmarkInUse() { is_used_ = false; }
4333

4434
tbm_surface_h BufferUnit::Surface() {
4535
if (IsUsed()) {
@@ -48,14 +38,13 @@ tbm_surface_h BufferUnit::Surface() {
4838
return nullptr;
4939
}
5040

51-
void BufferUnit::UnmarkInUse() { isUsed_ = false; }
52-
5341
void BufferUnit::Reset(int width, int height) {
5442
if (width_ == width && height_ == height) {
5543
return;
5644
}
5745
width_ = width;
5846
height_ = height;
47+
5948
if (tbm_surface_) {
6049
tbm_surface_destroy(tbm_surface_);
6150
tbm_surface_ = nullptr;
@@ -64,14 +53,15 @@ void BufferUnit::Reset(int width, int height) {
6453
delete gpu_buffer_;
6554
gpu_buffer_ = nullptr;
6655
}
56+
6757
tbm_surface_ = tbm_surface_create(width_, height_, TBM_FORMAT_ARGB8888);
6858
gpu_buffer_ = new FlutterDesktopGpuBuffer();
6959
gpu_buffer_->width = width_;
7060
gpu_buffer_->height = height_;
7161
gpu_buffer_->buffer = tbm_surface_;
7262
gpu_buffer_->release_callback = [](void* release_context) {
73-
BufferUnit* bu = (BufferUnit*)release_context;
74-
bu->UnmarkInUse();
63+
BufferUnit* buffer = reinterpret_cast<BufferUnit*>(release_context);
64+
buffer->UnmarkInUse();
7565
};
7666
gpu_buffer_->release_context = this;
7767
}
@@ -98,9 +88,9 @@ BufferUnit* BufferPool::GetAvailableBuffer() {
9888
return nullptr;
9989
}
10090

101-
void BufferPool::Release(BufferUnit* unit) {
91+
void BufferPool::Release(BufferUnit* buffer) {
10292
std::lock_guard<std::mutex> lock(mutex_);
103-
unit->UnmarkInUse();
93+
buffer->UnmarkInUse();
10494
}
10595

10696
void BufferPool::Prepare(int width, int height) {
@@ -117,27 +107,28 @@ SingleBufferPool::SingleBufferPool(int width, int height)
117107
SingleBufferPool::~SingleBufferPool() {}
118108

119109
BufferUnit* SingleBufferPool::GetAvailableBuffer() {
120-
pool_[0].get()->MarkInUse();
121-
return pool_[0].get();
110+
BufferUnit* buffer = pool_[0].get();
111+
buffer->MarkInUse();
112+
return buffer;
122113
}
123114

124-
void SingleBufferPool::Release(BufferUnit* unit) {}
115+
void SingleBufferPool::Release(BufferUnit* buffer) {}
125116

126117
#ifndef NDEBUG
127118
#include <cairo.h>
128119
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);
120+
char file_path[256];
121+
sprintf(file_path, "/tmp/dump%d.png", file_name);
122+
123+
tbm_surface_info_s surface_info;
124+
tbm_surface_map(tbm_surface_, TBM_SURF_OPTION_WRITE, &surface_info);
125+
126+
unsigned char* buffer = surface_info.planes[0].ptr;
127+
cairo_surface_t* png_buffer = cairo_image_surface_create_for_data(
128+
buffer, CAIRO_FORMAT_ARGB32, width_, height_,
129+
surface_info.planes[0].stride);
130+
131+
cairo_surface_write_to_png(png_buffer, file_path);
141132

142133
tbm_surface_unmap(tbm_surface_);
143134
cairo_surface_destroy(png_buffer);

packages/webview_flutter/tizen/src/buffer_pool.h

Lines changed: 21 additions & 14 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>
@@ -16,24 +16,30 @@ class BufferUnit {
1616
public:
1717
explicit BufferUnit(int index, int width, int height);
1818
~BufferUnit();
19+
1920
void Reset(int width, int 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_;
37+
bool is_used_ = false;
3238
int index_;
33-
int width_;
34-
int height_;
35-
tbm_surface_h tbm_surface_;
36-
FlutterDesktopGpuBuffer* gpu_buffer_;
39+
int width_ = 0;
40+
int height_ = 0;
41+
tbm_surface_h tbm_surface_ = nullptr;
42+
FlutterDesktopGpuBuffer* gpu_buffer_ = nullptr;
3743
};
3844

3945
class BufferPool {
@@ -42,7 +48,8 @@ class BufferPool {
4248
virtual ~BufferPool();
4349

4450
virtual BufferUnit* GetAvailableBuffer();
45-
virtual void Release(BufferUnit* unit);
51+
virtual void Release(BufferUnit* buffer);
52+
4653
void Prepare(int with, int height);
4754

4855
protected:
@@ -58,8 +65,8 @@ class SingleBufferPool : public BufferPool {
5865
explicit SingleBufferPool(int width, int height);
5966
~SingleBufferPool();
6067

61-
virtual BufferUnit* GetAvailableBuffer();
62-
virtual void Release(BufferUnit* unit);
68+
virtual BufferUnit* GetAvailableBuffer() override;
69+
virtual void Release(BufferUnit* buffer) override;
6370
};
6471

65-
#endif
72+
#endif // FLUTTER_PLUGIN_BUFFER_POOL_H_

0 commit comments

Comments
 (0)