Skip to content

Commit 3285a67

Browse files
committed
[webview_flutter] Update minor things #2
* Update lightweightwebengine binary (aarch64/armel, tizen 5.5) * Introduce buffer pool Signed-off-by: MuHong Byun <[email protected]>
1 parent cd11009 commit 3285a67

File tree

8 files changed

+177
-21
lines changed

8 files changed

+177
-21
lines changed

packages/webview_flutter/CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@
1414

1515
## 0.2.2
1616
* Update lightweight web engine binary & header file
17-
* Activate resizing function
17+
* Activate resizing function
18+
* Apply embedder's texture APIs change
Binary file not shown.
Binary file not shown.

packages/webview_flutter/tizen/project_def.prop

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type = sharedLib
66
profile = common-5.5
77

88
# Source files
9-
USER_SRCS += src/webview_flutter_tizen_plugin.cc src/webview.cc src/webview_factory.cc
9+
USER_SRCS += src/webview_flutter_tizen_plugin.cc src/webview.cc src/webview_factory.cc src/buffer_pool.cc
1010

1111
# User defines
1212
USER_DEFS =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "buffer_pool.h"
6+
7+
#include "log.h"
8+
9+
#define BUFFER_POOL_SIZE 5
10+
11+
BufferUnit::BufferUnit(int index, int width, int height)
12+
: isAllocated_(false),
13+
index_(index),
14+
width_(0),
15+
height_(0),
16+
tbm_surface_(nullptr) {
17+
Reset(width, height);
18+
}
19+
20+
BufferUnit::~BufferUnit() {
21+
if (tbm_surface_) {
22+
tbm_surface_destroy(tbm_surface_);
23+
tbm_surface_ = nullptr;
24+
}
25+
}
26+
27+
bool BufferUnit::Allocate() {
28+
if (!isAllocated_) {
29+
isAllocated_ = true;
30+
return true;
31+
}
32+
return false;
33+
}
34+
35+
int BufferUnit::Index() { return index_; }
36+
bool BufferUnit::isAllocate() { return isAllocated_ && tbm_surface_; }
37+
tbm_surface_h BufferUnit::Surface() {
38+
if (isAllocate()) {
39+
return tbm_surface_;
40+
}
41+
return nullptr;
42+
}
43+
44+
void BufferUnit::Release() { isAllocated_ = false; }
45+
46+
void BufferUnit::Reset(int width, int height) {
47+
if (width_ == width && height_ == height) {
48+
return;
49+
}
50+
width_ = width;
51+
height_ = height;
52+
if (tbm_surface_) {
53+
tbm_surface_destroy(tbm_surface_);
54+
tbm_surface_ = nullptr;
55+
}
56+
tbm_surface_ = tbm_surface_create(width_, height_, TBM_FORMAT_ARGB8888);
57+
}
58+
59+
BufferPool::BufferPool(int width, int height) {
60+
for (int idx = 0; idx < BUFFER_POOL_SIZE; idx++) {
61+
pool_.emplace_back(new BufferUnit(idx, width, height));
62+
}
63+
Prepare(width, height);
64+
}
65+
66+
BufferPool::~BufferPool() {}
67+
68+
BufferUnit* BufferPool::Get(tbm_surface_h surface) {
69+
for (int idx = 0; idx < BUFFER_POOL_SIZE; idx++) {
70+
BufferUnit* buffer = pool_[idx].get();
71+
if (buffer->Surface() == surface) {
72+
return buffer;
73+
}
74+
}
75+
return nullptr;
76+
}
77+
78+
BufferUnit* BufferPool::AllocateBuffer() {
79+
std::lock_guard<std::mutex> lock(mutex_);
80+
for (int idx = 0; idx < BUFFER_POOL_SIZE; idx++) {
81+
BufferUnit* buffer = pool_[idx].get();
82+
if (buffer->Allocate()) {
83+
return buffer;
84+
}
85+
}
86+
return nullptr;
87+
}
88+
89+
void BufferPool::Release(BufferUnit* unit) {
90+
std::lock_guard<std::mutex> lock(mutex_);
91+
unit->Release();
92+
}
93+
94+
void BufferPool::Prepare(int width, int height) {
95+
std::lock_guard<std::mutex> lock(mutex_);
96+
for (int idx = 0; idx < BUFFER_POOL_SIZE; idx++) {
97+
BufferUnit* buffer = pool_[idx].get();
98+
buffer->Reset(width, height);
99+
}
100+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef FLUTTER_PLUGIN_WEBVIEW_FLUTTER_TIZEN_WEBVIEW_BUFFER_POOL_H_
6+
#define FLUTTER_PLUGIN_WEBVIEW_FLUTTER_TIZEN_WEBVIEW_BUFFER_POOL_H_
7+
8+
#include <tbm_surface.h>
9+
10+
#include <memory>
11+
#include <mutex>
12+
#include <string>
13+
#include <vector>
14+
15+
class BufferUnit {
16+
public:
17+
explicit BufferUnit(int index, int width, int height);
18+
~BufferUnit();
19+
void Reset(int width, int height);
20+
bool Allocate();
21+
void Release();
22+
int Index();
23+
bool isAllocate();
24+
tbm_surface_h Surface();
25+
26+
private:
27+
bool isAllocated_;
28+
int index_;
29+
int width_;
30+
int height_;
31+
tbm_surface_h tbm_surface_;
32+
};
33+
34+
class BufferPool {
35+
public:
36+
explicit BufferPool(int width, int height);
37+
~BufferPool();
38+
39+
BufferUnit* AllocateBuffer();
40+
BufferUnit* Get(tbm_surface_h surface);
41+
void Release(BufferUnit* unit);
42+
void Prepare(int with, int height);
43+
44+
private:
45+
std::mutex mutex_;
46+
std::vector<std::unique_ptr<BufferUnit>> pool_;
47+
};
48+
49+
#endif

packages/webview_flutter/tizen/src/webview.cc

+18-17
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <sstream>
1616
#include <string>
1717

18+
#include "buffer_pool.h"
1819
#include "log.h"
1920
#include "lwe/LWEWebView.h"
2021
#include "lwe/PlatformIntegrationData.h"
@@ -170,6 +171,7 @@ WebView::WebView(flutter::PluginRegistrar* registrar, int viewId,
170171
context_(nullptr),
171172
texture_variant_(nullptr),
172173
gpu_buffer_(nullptr) {
174+
tbm_pool_ = std::make_unique<BufferPool>(width, height);
173175
texture_variant_ = new flutter::TextureVariant(flutter::GpuBufferTexture(
174176
[this](size_t width, size_t height) -> const FlutterDesktopGpuBuffer* {
175177
return this->ObtainGpuBuffer(width, height);
@@ -408,9 +410,9 @@ void WebView::Resize(double width, double height) {
408410
height_ = height;
409411

410412
if (candidate_surface_) {
411-
tbm_surface_destroy(candidate_surface_);
412413
candidate_surface_ = nullptr;
413414
}
415+
tbm_pool_->Prepare(width_, height_);
414416
webview_instance_->ResizeTo(width_, height_);
415417
}
416418

@@ -786,10 +788,13 @@ void WebView::InitWebView() {
786788
std::lock_guard<std::mutex> lock(mutex_);
787789
LWE::WebContainer::ExternalImageInfo result;
788790
if (!candidate_surface_) {
789-
candidate_surface_ =
790-
tbm_surface_create(width_, height_, TBM_FORMAT_ARGB8888);
791+
candidate_surface_ = tbm_pool_->AllocateBuffer();
792+
}
793+
if (candidate_surface_) {
794+
result.imageAddress = (void*)candidate_surface_->Surface();
795+
} else {
796+
result.imageAddress = nullptr;
791797
}
792-
result.imageAddress = (void*)candidate_surface_;
793798
return result;
794799
},
795800
[this](LWE::WebContainer* c, bool isRendered) {
@@ -931,20 +936,17 @@ void WebView::HandleCookieMethodCall(
931936

932937
FlutterDesktopGpuBuffer* WebView::ObtainGpuBuffer(size_t width, size_t height) {
933938
if (!candidate_surface_) {
934-
return nullptr;
939+
if (!rendered_surface_) {
940+
return nullptr;
941+
} else {
942+
return gpu_buffer_;
943+
}
935944
}
936-
937945
std::lock_guard<std::mutex> lock(mutex_);
938-
if (rendered_surface_) {
939-
tbm_surface_destroy(rendered_surface_);
940-
rendered_surface_ = nullptr;
941-
}
942-
943946
rendered_surface_ = candidate_surface_;
944947
candidate_surface_ = nullptr;
945-
946948
if (gpu_buffer_) {
947-
gpu_buffer_->buffer = rendered_surface_;
949+
gpu_buffer_->buffer = (void*)rendered_surface_->Surface();
948950
gpu_buffer_->width = width;
949951
gpu_buffer_->height = height;
950952
}
@@ -953,10 +955,9 @@ FlutterDesktopGpuBuffer* WebView::ObtainGpuBuffer(size_t width, size_t height) {
953955

954956
void WebView::DestructBuffer(void* buffer) {
955957
if (buffer) {
956-
std::lock_guard<std::mutex> lock(mutex_);
957-
tbm_surface_destroy((tbm_surface_h)buffer);
958-
if (rendered_surface_ == buffer) {
959-
rendered_surface_ = nullptr;
958+
BufferUnit* unit = tbm_pool_->Get((tbm_surface_h)buffer);
959+
if (unit) {
960+
tbm_pool_->Release(unit);
960961
}
961962
}
962963
}

packages/webview_flutter/tizen/src/webview.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class WebContainer;
2020
}
2121

2222
class TextInputChannel;
23+
class BufferPool;
24+
class BufferUnit;
2325

2426
class WebView : public PlatformView {
2527
public:
@@ -68,8 +70,10 @@ class WebView : public PlatformView {
6870
LWE::WebContainer* webview_instance_;
6971
double width_;
7072
double height_;
71-
tbm_surface_h candidate_surface_;
72-
tbm_surface_h rendered_surface_;
73+
// tbm_surface_h candidate_surface_;
74+
// tbm_surface_h rendered_surface_;
75+
BufferUnit* candidate_surface_;
76+
BufferUnit* rendered_surface_;
7377
bool is_mouse_lbutton_down_;
7478
bool has_navigation_delegate_;
7579
bool has_progress_tracking_;
@@ -78,6 +82,7 @@ class WebView : public PlatformView {
7882
flutter::TextureVariant* texture_variant_;
7983
FlutterDesktopGpuBuffer* gpu_buffer_;
8084
std::mutex mutex_;
85+
std::unique_ptr<BufferPool> tbm_pool_;
8186
};
8287

8388
#endif // FLUTTER_PLUGIN_WEBVIEW_FLUTTER_TIZEN_WEVIEW_H_

0 commit comments

Comments
 (0)