Skip to content

[webview_flutter] Refactor the C++ code #383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/webview_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## NEXT

* Code refactoring.

## 0.4.4

* Update LWE binary (645719ed084d899ec7d53de1758db71a8974e446).
Expand Down
83 changes: 36 additions & 47 deletions packages/webview_flutter/tizen/src/buffer_pool.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,7 @@

#include "log.h"

BufferUnit::BufferUnit(int index, int width, int height)
: isUsed_(false),
index_(index),
width_(0),
height_(0),
tbm_surface_(nullptr),
gpu_buffer_(nullptr) {
Reset(width, height);
}
BufferUnit::BufferUnit(int32_t width, int32_t height) { Reset(width, height); }

BufferUnit::~BufferUnit() {
if (tbm_surface_) {
Expand All @@ -28,18 +20,14 @@ BufferUnit::~BufferUnit() {
}

bool BufferUnit::MarkInUse() {
if (!isUsed_) {
isUsed_ = true;
if (!is_used_) {
is_used_ = true;
return true;
}
return false;
}

FlutterDesktopGpuBuffer* BufferUnit::GpuBuffer() { return gpu_buffer_; }

int BufferUnit::Index() { return index_; }

bool BufferUnit::IsUsed() { return isUsed_ && tbm_surface_; }
void BufferUnit::UnmarkInUse() { is_used_ = false; }

tbm_surface_h BufferUnit::Surface() {
if (IsUsed()) {
Expand All @@ -48,14 +36,13 @@ tbm_surface_h BufferUnit::Surface() {
return nullptr;
}

void BufferUnit::UnmarkInUse() { isUsed_ = false; }

void BufferUnit::Reset(int width, int height) {
void BufferUnit::Reset(int32_t width, int32_t height) {
if (width_ == width && height_ == height) {
return;
}
width_ = width;
height_ = height;

if (tbm_surface_) {
tbm_surface_destroy(tbm_surface_);
tbm_surface_ = nullptr;
Expand All @@ -64,21 +51,22 @@ void BufferUnit::Reset(int width, int height) {
delete gpu_buffer_;
gpu_buffer_ = nullptr;
}

tbm_surface_ = tbm_surface_create(width_, height_, TBM_FORMAT_ARGB8888);
gpu_buffer_ = new FlutterDesktopGpuBuffer();
gpu_buffer_->width = width_;
gpu_buffer_->height = height_;
gpu_buffer_->buffer = tbm_surface_;
gpu_buffer_->release_callback = [](void* release_context) {
BufferUnit* bu = (BufferUnit*)release_context;
bu->UnmarkInUse();
BufferUnit* buffer = reinterpret_cast<BufferUnit*>(release_context);
buffer->UnmarkInUse();
};
gpu_buffer_->release_context = this;
}

BufferPool::BufferPool(int width, int height, int pool_size) : last_index_(0) {
for (int idx = 0; idx < pool_size; idx++) {
pool_.emplace_back(new BufferUnit(idx, width, height));
BufferPool::BufferPool(int32_t width, int32_t height, size_t pool_size) {
for (size_t index = 0; index < pool_size; index++) {
pool_.emplace_back(std::make_unique<BufferUnit>(width, height));
}
Prepare(width, height);
}
Expand All @@ -87,8 +75,8 @@ BufferPool::~BufferPool() {}

BufferUnit* BufferPool::GetAvailableBuffer() {
std::lock_guard<std::mutex> lock(mutex_);
for (int idx = 0; idx < pool_.size(); idx++) {
int current = (idx + last_index_) % pool_.size();
for (size_t index = 0; index < pool_.size(); index++) {
size_t current = (index + last_index_) % pool_.size();
BufferUnit* buffer = pool_[current].get();
if (buffer->MarkInUse()) {
last_index_ = current;
Expand All @@ -98,46 +86,47 @@ BufferUnit* BufferPool::GetAvailableBuffer() {
return nullptr;
}

void BufferPool::Release(BufferUnit* unit) {
void BufferPool::Release(BufferUnit* buffer) {
std::lock_guard<std::mutex> lock(mutex_);
unit->UnmarkInUse();
buffer->UnmarkInUse();
}

void BufferPool::Prepare(int width, int height) {
void BufferPool::Prepare(int32_t width, int32_t height) {
std::lock_guard<std::mutex> lock(mutex_);
for (int idx = 0; idx < pool_.size(); idx++) {
BufferUnit* buffer = pool_[idx].get();
for (size_t index = 0; index < pool_.size(); index++) {
BufferUnit* buffer = pool_[index].get();
buffer->Reset(width, height);
}
}

SingleBufferPool::SingleBufferPool(int width, int height)
SingleBufferPool::SingleBufferPool(int32_t width, int32_t height)
: BufferPool(width, height, 1) {}

SingleBufferPool::~SingleBufferPool() {}

BufferUnit* SingleBufferPool::GetAvailableBuffer() {
pool_[0].get()->MarkInUse();
return pool_[0].get();
BufferUnit* buffer = pool_[0].get();
buffer->MarkInUse();
return buffer;
}

void SingleBufferPool::Release(BufferUnit* unit) {}
void SingleBufferPool::Release(BufferUnit* buffer) {}

#ifndef NDEBUG
#include <cairo.h>
void BufferUnit::DumpToPng(int file_name) {
char filePath[256];
sprintf(filePath, "/tmp/dump%d.png", file_name);
tbm_surface_info_s surfaceInfo;
tbm_surface_map(tbm_surface_, TBM_SURF_OPTION_WRITE, &surfaceInfo);
void* buffer = surfaceInfo.planes[0].ptr;

cairo_surface_t* png_buffer;
png_buffer = cairo_image_surface_create_for_data(
(unsigned char*)buffer, CAIRO_FORMAT_ARGB32, width_, height_,
surfaceInfo.planes[0].stride);

cairo_surface_write_to_png(png_buffer, filePath);
char file_path[256];
sprintf(file_path, "/tmp/dump%d.png", file_name);

tbm_surface_info_s surface_info;
tbm_surface_map(tbm_surface_, TBM_SURF_OPTION_WRITE, &surface_info);

unsigned char* buffer = surface_info.planes[0].ptr;
cairo_surface_t* png_buffer = cairo_image_surface_create_for_data(
buffer, CAIRO_FORMAT_ARGB32, width_, height_,
surface_info.planes[0].stride);

cairo_surface_write_to_png(png_buffer, file_path);

tbm_surface_unmap(tbm_surface_);
cairo_surface_destroy(png_buffer);
Expand Down
48 changes: 27 additions & 21 deletions packages/webview_flutter/tizen/src/buffer_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef FLUTTER_PLUGIN_WEBVIEW_FLUTTER_TIZEN_WEBVIEW_BUFFER_POOL_H_
#define FLUTTER_PLUGIN_WEBVIEW_FLUTTER_TIZEN_WEBVIEW_BUFFER_POOL_H_
#ifndef FLUTTER_PLUGIN_BUFFER_POOL_H_
#define FLUTTER_PLUGIN_BUFFER_POOL_H_

#include <flutter_texture_registrar.h>
#include <tbm_surface.h>
Expand All @@ -14,52 +14,58 @@

class BufferUnit {
public:
explicit BufferUnit(int index, int width, int height);
explicit BufferUnit(int32_t width, int32_t height);
~BufferUnit();
void Reset(int width, int height);

void Reset(int32_t width, int32_t height);

bool MarkInUse();
void UnmarkInUse();
int Index();
bool IsUsed();

bool IsUsed() { return is_used_ && tbm_surface_; }

tbm_surface_h Surface();
FlutterDesktopGpuBuffer* GpuBuffer();

FlutterDesktopGpuBuffer* GpuBuffer() { return gpu_buffer_; }

#ifndef NDEBUG
// TODO: Unused code.
void DumpToPng(int file_name);
#endif

private:
bool isUsed_;
int index_;
int width_;
int height_;
tbm_surface_h tbm_surface_;
FlutterDesktopGpuBuffer* gpu_buffer_;
bool is_used_ = false;
int32_t width_ = 0;
int32_t height_ = 0;
tbm_surface_h tbm_surface_ = nullptr;
FlutterDesktopGpuBuffer* gpu_buffer_ = nullptr;
};

class BufferPool {
public:
explicit BufferPool(int width, int height, int pool_size);
explicit BufferPool(int32_t width, int32_t height, size_t pool_size);
virtual ~BufferPool();

virtual BufferUnit* GetAvailableBuffer();
virtual void Release(BufferUnit* unit);
void Prepare(int with, int height);
virtual void Release(BufferUnit* buffer);

void Prepare(int32_t with, int32_t height);

protected:
std::vector<std::unique_ptr<BufferUnit>> pool_;

private:
int last_index_;
size_t last_index_ = 0;
std::mutex mutex_;
};

class SingleBufferPool : public BufferPool {
public:
explicit SingleBufferPool(int width, int height);
explicit SingleBufferPool(int32_t width, int32_t height);
~SingleBufferPool();

virtual BufferUnit* GetAvailableBuffer();
virtual void Release(BufferUnit* unit);
virtual BufferUnit* GetAvailableBuffer() override;
virtual void Release(BufferUnit* buffer) override;
};

#endif
#endif // FLUTTER_PLUGIN_BUFFER_POOL_H_
Loading