Skip to content

Commit 8c60c51

Browse files
authored
[webview_flutter] Apply texture api change (#100)
* [webview_flutter] Apply texture api change Signed-off-by: MuHong Byun <[email protected]> * [webview_flutter] fix memory leak Signed-off-by: MuHong Byun <[email protected]> * [webview_flutter] Apply review comment Signed-off-by: MuHong Byun <[email protected]> * [webview_flutter] Fix memory issue Signed-off-by: MuHong Byun <[email protected]> * [webview_flutter] Apply review comment Signed-off-by: MuHong Byun <[email protected]>
1 parent 8704927 commit 8c60c51

File tree

5 files changed

+85
-36
lines changed

5 files changed

+85
-36
lines changed

packages/webview_flutter/tizen/src/webview.cc

+66-13
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
#include <Ecore_IMF_Evas.h>
88
#include <Ecore_Input_Evas.h>
9+
#include <flutter/texture_registrar.h>
910
#include <flutter_platform_view.h>
10-
#include <flutter_tizen_texture_registrar.h>
11+
#include <flutter_texture_registrar.h>
1112

1213
#include <map>
1314
#include <memory>
@@ -154,19 +155,27 @@ double ExtractDoubleFromMap(const flutter::EncodableValue& arguments,
154155
}
155156

156157
WebView::WebView(flutter::PluginRegistrar* registrar, int viewId,
157-
FlutterTextureRegistrar* texture_registrar, double width,
158+
flutter::TextureRegistrar* texture_registrar, double width,
158159
double height, flutter::EncodableMap& params)
159160
: PlatformView(registrar, viewId),
160161
texture_registrar_(texture_registrar),
161162
webview_instance_(nullptr),
162163
width_(width),
163164
height_(height),
164-
tbm_surface_(nullptr),
165+
candidate_surface_(nullptr),
166+
rendered_surface_(nullptr),
165167
is_mouse_lbutton_down_(false),
166168
has_navigation_delegate_(false),
167169
has_progress_tracking_(false),
168-
context_(nullptr) {
169-
SetTextureId(FlutterRegisterExternalTexture(texture_registrar_));
170+
context_(nullptr),
171+
texture_variant_(nullptr),
172+
gpu_buffer_(nullptr) {
173+
texture_variant_ = new flutter::TextureVariant(flutter::GpuBufferTexture(
174+
[this](size_t width, size_t height) -> const FlutterDesktopGpuBuffer* {
175+
return this->ObtainGpuBuffer(width, height);
176+
},
177+
[this](void* buffer) -> void { this->DestructBuffer(buffer); }));
178+
SetTextureId(texture_registrar_->RegisterTexture(texture_variant_));
170179
InitWebView();
171180

172181
channel_ = std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
@@ -375,12 +384,22 @@ std::string WebView::GetChannelName() {
375384
}
376385

377386
void WebView::Dispose() {
378-
FlutterUnregisterExternalTexture(texture_registrar_, GetTextureId());
387+
texture_registrar_->UnregisterTexture(GetTextureId());
379388

380389
if (webview_instance_) {
381390
webview_instance_->Destroy();
382391
webview_instance_ = nullptr;
383392
}
393+
394+
if (texture_variant_) {
395+
delete texture_variant_;
396+
texture_variant_ = nullptr;
397+
}
398+
399+
if (gpu_buffer_) {
400+
delete gpu_buffer_;
401+
gpu_buffer_ = nullptr;
402+
}
384403
}
385404

386405
void WebView::Resize(double width, double height) {
@@ -747,26 +766,28 @@ void WebView::InitWebView() {
747766
webview_instance_->Destroy();
748767
webview_instance_ = nullptr;
749768
}
769+
if (!gpu_buffer_) {
770+
gpu_buffer_ = new FlutterDesktopGpuBuffer();
771+
}
772+
750773
float scale_factor = 1;
751774

752775
webview_instance_ = (LWE::WebContainer*)createWebViewInstance(
753776
0, 0, width_, height_, scale_factor, "SamsungOneUI", "ko-KR",
754777
"Asia/Seoul",
755778
[this]() -> LWE::WebContainer::ExternalImageInfo {
779+
std::lock_guard<std::mutex> lock(mutex_);
756780
LWE::WebContainer::ExternalImageInfo result;
757-
if (!tbm_surface_) {
758-
tbm_surface_ =
781+
if (!candidate_surface_) {
782+
candidate_surface_ =
759783
tbm_surface_create(width_, height_, TBM_FORMAT_ARGB8888);
760784
}
761-
result.imageAddress = (void*)tbm_surface_;
785+
result.imageAddress = (void*)candidate_surface_;
762786
return result;
763787
},
764788
[this](LWE::WebContainer* c, bool isRendered) {
765789
if (isRendered) {
766-
FlutterMarkExternalTextureFrameAvailable(
767-
texture_registrar_, GetTextureId(), tbm_surface_);
768-
tbm_surface_destroy(tbm_surface_);
769-
tbm_surface_ = nullptr;
790+
texture_registrar_->MarkTextureFrameAvailable(GetTextureId());
770791
}
771792
});
772793
#ifndef TV_PROFILE
@@ -900,3 +921,35 @@ void WebView::HandleCookieMethodCall(
900921
result->NotImplemented();
901922
}
902923
}
924+
925+
FlutterDesktopGpuBuffer* WebView::ObtainGpuBuffer(size_t width, size_t height) {
926+
if (!candidate_surface_) {
927+
return nullptr;
928+
}
929+
930+
std::lock_guard<std::mutex> lock(mutex_);
931+
if (rendered_surface_) {
932+
tbm_surface_destroy(rendered_surface_);
933+
rendered_surface_ = nullptr;
934+
}
935+
936+
rendered_surface_ = candidate_surface_;
937+
candidate_surface_ = nullptr;
938+
939+
if (gpu_buffer_) {
940+
gpu_buffer_->buffer = rendered_surface_;
941+
gpu_buffer_->width = width;
942+
gpu_buffer_->height = height;
943+
}
944+
return gpu_buffer_;
945+
}
946+
947+
void WebView::DestructBuffer(void* buffer) {
948+
if (buffer) {
949+
std::lock_guard<std::mutex> lock(mutex_);
950+
tbm_surface_destroy((tbm_surface_h)buffer);
951+
if (rendered_surface_ == buffer) {
952+
rendered_surface_ = nullptr;
953+
}
954+
}
955+
}

packages/webview_flutter/tizen/src/webview.h

+13-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
#include <flutter/standard_message_codec.h>
1111
#include <flutter/standard_method_codec.h>
1212
#include <flutter_platform_view.h>
13-
#include <flutter_tizen_texture_registrar.h>
1413
#include <tbm_surface.h>
1514

15+
#include <mutex>
16+
#include <stack>
17+
1618
namespace LWE {
1719
class WebContainer;
1820
}
@@ -22,7 +24,7 @@ class TextInputChannel;
2224
class WebView : public PlatformView {
2325
public:
2426
WebView(flutter::PluginRegistrar* registrar, int viewId,
25-
FlutterTextureRegistrar* textureRegistrar, double width,
27+
flutter::TextureRegistrar* textureRegistrar, double width,
2628
double height, flutter::EncodableMap& params);
2729
~WebView();
2830
virtual void Dispose() override;
@@ -46,6 +48,9 @@ class WebView : public PlatformView {
4648
void HidePanel();
4749
void ShowPanel();
4850

51+
FlutterDesktopGpuBuffer* ObtainGpuBuffer(size_t width, size_t height);
52+
void DestructBuffer(void* buffer);
53+
4954
private:
5055
void HandleMethodCall(
5156
const flutter::MethodCall<flutter::EncodableValue>& method_call,
@@ -59,16 +64,20 @@ class WebView : public PlatformView {
5964
void RegisterJavaScriptChannelName(const std::string& name);
6065
void ApplySettings(flutter::EncodableMap);
6166

62-
FlutterTextureRegistrar* texture_registrar_;
67+
flutter::TextureRegistrar* texture_registrar_;
6368
LWE::WebContainer* webview_instance_;
6469
double width_;
6570
double height_;
66-
tbm_surface_h tbm_surface_;
71+
tbm_surface_h candidate_surface_;
72+
tbm_surface_h rendered_surface_;
6773
bool is_mouse_lbutton_down_;
6874
bool has_navigation_delegate_;
6975
bool has_progress_tracking_;
7076
std::unique_ptr<flutter::MethodChannel<flutter::EncodableValue>> channel_;
7177
Ecore_IMF_Context* context_;
78+
flutter::TextureVariant* texture_variant_;
79+
FlutterDesktopGpuBuffer* gpu_buffer_;
80+
std::mutex mutex_;
7281
};
7382

7483
#endif // FLUTTER_PLUGIN_WEBVIEW_FLUTTER_TIZEN_WEVIEW_H_

packages/webview_flutter/tizen/src/webview_factory.cc

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <flutter/standard_message_codec.h>
1111
#include <flutter/standard_method_codec.h>
1212
#include <flutter_platform_view.h>
13-
#include <flutter_tizen_texture_registrar.h>
1413

1514
#include <map>
1615
#include <memory>
@@ -22,8 +21,8 @@
2221
#include "webview_flutter_tizen_plugin.h"
2322

2423
WebViewFactory::WebViewFactory(flutter::PluginRegistrar* registrar,
25-
FlutterTextureRegistrar* textureRegistrar)
26-
: PlatformViewFactory(registrar), textureRegistrar_(textureRegistrar) {
24+
flutter::TextureRegistrar* textureRegistrar)
25+
: PlatformViewFactory(registrar), texture_registrar_(textureRegistrar) {
2726
char* path = app_get_data_path();
2827
if (!path || strlen(path) == 0) {
2928
path = "/tmp/";
@@ -51,7 +50,7 @@ PlatformView* WebViewFactory::Create(int viewId, double width, double height,
5150
}
5251

5352
try {
54-
return new WebView(GetPluginRegistrar(), viewId, textureRegistrar_, width,
53+
return new WebView(GetPluginRegistrar(), viewId, texture_registrar_, width,
5554
height, params);
5655
} catch (const std::invalid_argument& ex) {
5756
LOG_ERROR("[Exception] %s\n", ex.what());

packages/webview_flutter/tizen/src/webview_factory.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
class WebViewFactory : public PlatformViewFactory {
1010
public:
1111
WebViewFactory(flutter::PluginRegistrar* registrar,
12-
FlutterTextureRegistrar* textureRegistrar);
12+
flutter::TextureRegistrar* textureRegistrar);
1313
virtual void Dispose() override;
1414
virtual PlatformView* Create(
1515
int viewId, double width, double height,
1616
const std::vector<uint8_t>& createParams) override;
1717

1818
private:
19-
FlutterTextureRegistrar* textureRegistrar_;
19+
flutter::TextureRegistrar* texture_registrar_;
2020
};
2121

2222
#endif // FLUTTER_PLUGIN_WEBVIEW_FLUTTER_TIZEN_WEVIEW_FACTORY_H_

packages/webview_flutter/tizen/src/webview_flutter_tizen_plugin.cc

+1-13
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,8 @@
44

55
#include "webview_flutter_tizen_plugin.h"
66

7-
#include <flutter/method_channel.h>
8-
#include <flutter/plugin_registrar.h>
9-
#include <flutter/standard_message_codec.h>
10-
#include <flutter/standard_method_codec.h>
11-
#include <flutter_platform_view.h>
12-
#include <flutter_tizen_texture_registrar.h>
13-
14-
#include <map>
157
#include <memory>
16-
#include <sstream>
17-
#include <string>
188

19-
#include "log.h"
20-
#include "webview.h"
219
#include "webview_factory.h"
2210

2311
static constexpr char kViewType[] = "plugins.flutter.io/webview";
@@ -38,7 +26,7 @@ void WebviewFlutterTizenPluginRegisterWithRegistrar(
3826
flutter::PluginRegistrarManager::GetInstance()
3927
->GetRegistrar<flutter::PluginRegistrar>(registrar);
4028
auto factory = std::make_unique<WebViewFactory>(
41-
core_registrar, FlutterPluginRegistrarGetTexture(registrar));
29+
core_registrar, core_registrar->texture_registrar());
4230
FlutterRegisterViewFactory(registrar, kViewType, std::move(factory));
4331
WebviewFlutterTizenPlugin::RegisterWithRegistrar(core_registrar);
4432
}

0 commit comments

Comments
 (0)