Skip to content

[webview_flutter] Apply texture api change #100

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
Show file tree
Hide file tree
Changes from all 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
79 changes: 66 additions & 13 deletions packages/webview_flutter/tizen/src/webview.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

#include <Ecore_IMF_Evas.h>
#include <Ecore_Input_Evas.h>
#include <flutter/texture_registrar.h>
#include <flutter_platform_view.h>
#include <flutter_tizen_texture_registrar.h>
#include <flutter_texture_registrar.h>

#include <map>
#include <memory>
Expand Down Expand Up @@ -154,19 +155,27 @@ double ExtractDoubleFromMap(const flutter::EncodableValue& arguments,
}

WebView::WebView(flutter::PluginRegistrar* registrar, int viewId,
FlutterTextureRegistrar* texture_registrar, double width,
flutter::TextureRegistrar* texture_registrar, double width,
double height, flutter::EncodableMap& params)
: PlatformView(registrar, viewId),
texture_registrar_(texture_registrar),
webview_instance_(nullptr),
width_(width),
height_(height),
tbm_surface_(nullptr),
candidate_surface_(nullptr),
rendered_surface_(nullptr),
is_mouse_lbutton_down_(false),
has_navigation_delegate_(false),
has_progress_tracking_(false),
context_(nullptr) {
SetTextureId(FlutterRegisterExternalTexture(texture_registrar_));
context_(nullptr),
texture_variant_(nullptr),
gpu_buffer_(nullptr) {
texture_variant_ = new flutter::TextureVariant(flutter::GpuBufferTexture(
[this](size_t width, size_t height) -> const FlutterDesktopGpuBuffer* {
return this->ObtainGpuBuffer(width, height);
},
[this](void* buffer) -> void { this->DestructBuffer(buffer); }));
SetTextureId(texture_registrar_->RegisterTexture(texture_variant_));
InitWebView();

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

void WebView::Dispose() {
FlutterUnregisterExternalTexture(texture_registrar_, GetTextureId());
texture_registrar_->UnregisterTexture(GetTextureId());

if (webview_instance_) {
webview_instance_->Destroy();
webview_instance_ = nullptr;
}

if (texture_variant_) {
delete texture_variant_;
texture_variant_ = nullptr;
}

if (gpu_buffer_) {
delete gpu_buffer_;
gpu_buffer_ = nullptr;
}
}

void WebView::Resize(double width, double height) {
Expand Down Expand Up @@ -747,26 +766,28 @@ void WebView::InitWebView() {
webview_instance_->Destroy();
webview_instance_ = nullptr;
}
if (!gpu_buffer_) {
gpu_buffer_ = new FlutterDesktopGpuBuffer();
}

float scale_factor = 1;

webview_instance_ = (LWE::WebContainer*)createWebViewInstance(
0, 0, width_, height_, scale_factor, "SamsungOneUI", "ko-KR",
"Asia/Seoul",
[this]() -> LWE::WebContainer::ExternalImageInfo {
std::lock_guard<std::mutex> lock(mutex_);
LWE::WebContainer::ExternalImageInfo result;
if (!tbm_surface_) {
tbm_surface_ =
if (!candidate_surface_) {
candidate_surface_ =
tbm_surface_create(width_, height_, TBM_FORMAT_ARGB8888);
}
result.imageAddress = (void*)tbm_surface_;
result.imageAddress = (void*)candidate_surface_;
return result;
},
[this](LWE::WebContainer* c, bool isRendered) {
if (isRendered) {
FlutterMarkExternalTextureFrameAvailable(
texture_registrar_, GetTextureId(), tbm_surface_);
tbm_surface_destroy(tbm_surface_);
tbm_surface_ = nullptr;
texture_registrar_->MarkTextureFrameAvailable(GetTextureId());
}
});
#ifndef TV_PROFILE
Expand Down Expand Up @@ -900,3 +921,35 @@ void WebView::HandleCookieMethodCall(
result->NotImplemented();
}
}

FlutterDesktopGpuBuffer* WebView::ObtainGpuBuffer(size_t width, size_t height) {
if (!candidate_surface_) {
return nullptr;
}

std::lock_guard<std::mutex> lock(mutex_);
if (rendered_surface_) {
tbm_surface_destroy(rendered_surface_);
rendered_surface_ = nullptr;
}

rendered_surface_ = candidate_surface_;
candidate_surface_ = nullptr;

if (gpu_buffer_) {
gpu_buffer_->buffer = rendered_surface_;
gpu_buffer_->width = width;
gpu_buffer_->height = height;
}
return gpu_buffer_;
}

void WebView::DestructBuffer(void* buffer) {
if (buffer) {
std::lock_guard<std::mutex> lock(mutex_);
tbm_surface_destroy((tbm_surface_h)buffer);
if (rendered_surface_ == buffer) {
rendered_surface_ = nullptr;
}
}
}
17 changes: 13 additions & 4 deletions packages/webview_flutter/tizen/src/webview.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
#include <flutter/standard_message_codec.h>
#include <flutter/standard_method_codec.h>
#include <flutter_platform_view.h>
#include <flutter_tizen_texture_registrar.h>
#include <tbm_surface.h>

#include <mutex>
#include <stack>

namespace LWE {
class WebContainer;
}
Expand All @@ -22,7 +24,7 @@ class TextInputChannel;
class WebView : public PlatformView {
public:
WebView(flutter::PluginRegistrar* registrar, int viewId,
FlutterTextureRegistrar* textureRegistrar, double width,
flutter::TextureRegistrar* textureRegistrar, double width,
double height, flutter::EncodableMap& params);
~WebView();
virtual void Dispose() override;
Expand All @@ -46,6 +48,9 @@ class WebView : public PlatformView {
void HidePanel();
void ShowPanel();

FlutterDesktopGpuBuffer* ObtainGpuBuffer(size_t width, size_t height);
void DestructBuffer(void* buffer);

private:
void HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue>& method_call,
Expand All @@ -59,16 +64,20 @@ class WebView : public PlatformView {
void RegisterJavaScriptChannelName(const std::string& name);
void ApplySettings(flutter::EncodableMap);

FlutterTextureRegistrar* texture_registrar_;
flutter::TextureRegistrar* texture_registrar_;
LWE::WebContainer* webview_instance_;
double width_;
double height_;
tbm_surface_h tbm_surface_;
tbm_surface_h candidate_surface_;
tbm_surface_h rendered_surface_;
bool is_mouse_lbutton_down_;
bool has_navigation_delegate_;
bool has_progress_tracking_;
std::unique_ptr<flutter::MethodChannel<flutter::EncodableValue>> channel_;
Ecore_IMF_Context* context_;
flutter::TextureVariant* texture_variant_;
FlutterDesktopGpuBuffer* gpu_buffer_;
std::mutex mutex_;
};

#endif // FLUTTER_PLUGIN_WEBVIEW_FLUTTER_TIZEN_WEVIEW_H_
7 changes: 3 additions & 4 deletions packages/webview_flutter/tizen/src/webview_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <flutter/standard_message_codec.h>
#include <flutter/standard_method_codec.h>
#include <flutter_platform_view.h>
#include <flutter_tizen_texture_registrar.h>

#include <map>
#include <memory>
Expand All @@ -22,8 +21,8 @@
#include "webview_flutter_tizen_plugin.h"

WebViewFactory::WebViewFactory(flutter::PluginRegistrar* registrar,
FlutterTextureRegistrar* textureRegistrar)
: PlatformViewFactory(registrar), textureRegistrar_(textureRegistrar) {
flutter::TextureRegistrar* textureRegistrar)
: PlatformViewFactory(registrar), texture_registrar_(textureRegistrar) {
char* path = app_get_data_path();
if (!path || strlen(path) == 0) {
path = "/tmp/";
Expand Down Expand Up @@ -51,7 +50,7 @@ PlatformView* WebViewFactory::Create(int viewId, double width, double height,
}

try {
return new WebView(GetPluginRegistrar(), viewId, textureRegistrar_, width,
return new WebView(GetPluginRegistrar(), viewId, texture_registrar_, width,
height, params);
} catch (const std::invalid_argument& ex) {
LOG_ERROR("[Exception] %s\n", ex.what());
Expand Down
4 changes: 2 additions & 2 deletions packages/webview_flutter/tizen/src/webview_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
class WebViewFactory : public PlatformViewFactory {
public:
WebViewFactory(flutter::PluginRegistrar* registrar,
FlutterTextureRegistrar* textureRegistrar);
flutter::TextureRegistrar* textureRegistrar);
virtual void Dispose() override;
virtual PlatformView* Create(
int viewId, double width, double height,
const std::vector<uint8_t>& createParams) override;

private:
FlutterTextureRegistrar* textureRegistrar_;
flutter::TextureRegistrar* texture_registrar_;
};

#endif // FLUTTER_PLUGIN_WEBVIEW_FLUTTER_TIZEN_WEVIEW_FACTORY_H_
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,8 @@

#include "webview_flutter_tizen_plugin.h"

#include <flutter/method_channel.h>
#include <flutter/plugin_registrar.h>
#include <flutter/standard_message_codec.h>
#include <flutter/standard_method_codec.h>
#include <flutter_platform_view.h>
#include <flutter_tizen_texture_registrar.h>

#include <map>
#include <memory>
#include <sstream>
#include <string>

#include "log.h"
#include "webview.h"
#include "webview_factory.h"

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