Skip to content

[webview_flutter] Update minor things #118

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 4 commits into from
Jun 7, 2021
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
7 changes: 6 additions & 1 deletion packages/webview_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@
* Update webivew_flutter to 2.0.4

## 0.2.1
* Add lightweight web engine binary for arm64
* Add lightweight web engine binary for arm64

## 0.2.2
* Update lightweight web engine binary & header file (6263be6c888d5cb9dcca5466dfc3941a70b424eb)
* Activate resizing function
* Apply embedder's texture APIs change
2 changes: 1 addition & 1 deletion packages/webview_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: webview_flutter_tizen
description: Tizen implementation of the webview plugin
homepage: https://github.com/flutter-tizen/plugins
version: 0.2.1
version: 0.2.2

environment:
sdk: ">=2.12.0 <3.0.0"
Expand Down
3 changes: 3 additions & 0 deletions packages/webview_flutter/tizen/inc/lwe/LWEWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class LWE_EXPORT Settings {
void GetBaseForegroundColor(unsigned char& r, unsigned char& g,
unsigned char& b, unsigned char& a) const;
bool NeedsDownloadWebFontsEarly() const;
bool UseHttp2() const;
uint32_t NeedsDownScaleImageResourceLargerThan() const;
void SetUserAgentString(const std::string& ua);
void SetCacheMode(int mode);
Expand All @@ -97,6 +98,7 @@ class LWE_EXPORT Settings {
void SetIdleModeJob(IdleModeJob j);
void SetIdleModeCheckIntervalInMS(uint32_t intervalInMS);
void SetNeedsDownloadWebFontsEarly(bool b);
void SetUseHttp2(bool b);
void SetNeedsDownScaleImageResourceLargerThan(
uint32_t demention); // Experimental

Expand All @@ -113,6 +115,7 @@ class LWE_EXPORT Settings {
IdleModeJob m_idleModeJob; // default value is IdleModeJob::IdleModeFull
uint32_t m_idleModeCheckIntervalInMS; // default value is 3000(ms)
bool m_needsDownloadWebFontsEarly;
bool m_useHttp2; // default value is false
uint32_t m_needsDownScaleImageResourceLargerThan;
};

Expand Down
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/webview_flutter/tizen/project_def.prop
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type = sharedLib
profile = common-5.5

# Source files
USER_SRCS += src/webview_flutter_tizen_plugin.cc src/webview.cc src/webview_factory.cc
USER_SRCS += src/*.cc

# User defines
USER_DEFS =
Expand Down
102 changes: 102 additions & 0 deletions packages/webview_flutter/tizen/src/buffer_pool.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "buffer_pool.h"

#include "log.h"

#define BUFFER_POOL_SIZE 5

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

BufferUnit::~BufferUnit() {
if (tbm_surface_) {
tbm_surface_destroy(tbm_surface_);
tbm_surface_ = nullptr;
}
}

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

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

bool BufferUnit::IsUsed() { return isUsed_ && tbm_surface_; }

tbm_surface_h BufferUnit::Surface() {
if (IsUsed()) {
return tbm_surface_;
}
return nullptr;
}

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

void BufferUnit::Reset(int width, int height) {
if (width_ == width && height_ == height) {
return;
}
width_ = width;
height_ = height;
if (tbm_surface_) {
tbm_surface_destroy(tbm_surface_);
tbm_surface_ = nullptr;
}
tbm_surface_ = tbm_surface_create(width_, height_, TBM_FORMAT_ARGB8888);
}

BufferPool::BufferPool(int width, int height) {
for (int idx = 0; idx < BUFFER_POOL_SIZE; idx++) {
pool_.emplace_back(new BufferUnit(idx, width, height));
}
Prepare(width, height);
}

BufferPool::~BufferPool() {}

BufferUnit* BufferPool::Find(tbm_surface_h surface) {
for (int idx = 0; idx < pool_.size(); idx++) {
BufferUnit* buffer = pool_[idx].get();
if (buffer->Surface() == surface) {
return buffer;
}
}
return nullptr;
}

BufferUnit* BufferPool::GetAvailableBuffer() {
std::lock_guard<std::mutex> lock(mutex_);
for (int idx = 0; idx < pool_.size(); idx++) {
BufferUnit* buffer = pool_[idx].get();
if (buffer->MarkInUse()) {
return buffer;
}
}
return nullptr;
}

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

void BufferPool::Prepare(int width, int height) {
std::lock_guard<std::mutex> lock(mutex_);
for (int idx = 0; idx < pool_.size(); idx++) {
BufferUnit* buffer = pool_[idx].get();
buffer->Reset(width, height);
}
}
49 changes: 49 additions & 0 deletions packages/webview_flutter/tizen/src/buffer_pool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved.
// 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_

#include <tbm_surface.h>

#include <memory>
#include <mutex>
#include <string>
#include <vector>

class BufferUnit {
public:
explicit BufferUnit(int index, int width, int height);
~BufferUnit();
void Reset(int width, int height);
bool MarkInUse();
void UnmarkInUse();
int Index();
bool IsUsed();
tbm_surface_h Surface();

private:
bool isUsed_;
int index_;
int width_;
int height_;
tbm_surface_h tbm_surface_;
};

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

BufferUnit* GetAvailableBuffer();
BufferUnit* Find(tbm_surface_h surface);
void Release(BufferUnit* unit);
void Prepare(int with, int height);

private:
std::mutex mutex_;
std::vector<std::unique_ptr<BufferUnit>> pool_;
};

#endif
43 changes: 26 additions & 17 deletions packages/webview_flutter/tizen/src/webview.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <sstream>
#include <string>

#include "buffer_pool.h"
#include "log.h"
#include "lwe/LWEWebView.h"
#include "lwe/PlatformIntegrationData.h"
Expand Down Expand Up @@ -170,6 +171,7 @@ WebView::WebView(flutter::PluginRegistrar* registrar, int viewId,
context_(nullptr),
texture_variant_(nullptr),
gpu_buffer_(nullptr) {
tbm_pool_ = std::make_unique<BufferPool>(width, height);
texture_variant_ = new flutter::TextureVariant(flutter::GpuBufferTexture(
[this](size_t width, size_t height) -> const FlutterDesktopGpuBuffer* {
return this->ObtainGpuBuffer(width, height);
Expand Down Expand Up @@ -404,7 +406,14 @@ void WebView::Dispose() {

void WebView::Resize(double width, double height) {
LOG_DEBUG("WebView::Resize width: %f height: %f \n", width, height);
// NOTE: Not supported by LWE on Tizen.
width_ = width;
height_ = height;

if (candidate_surface_) {
candidate_surface_ = nullptr;
}
tbm_pool_->Prepare(width_, height_);
webview_instance_->ResizeTo(width_, height_);
}

void WebView::Touch(int type, int button, double x, double y, double dx,
Expand Down Expand Up @@ -779,10 +788,14 @@ void WebView::InitWebView() {
std::lock_guard<std::mutex> lock(mutex_);
LWE::WebContainer::ExternalImageInfo result;
if (!candidate_surface_) {
candidate_surface_ =
tbm_surface_create(width_, height_, TBM_FORMAT_ARGB8888);
candidate_surface_ = tbm_pool_->GetAvailableBuffer();
}
if (candidate_surface_) {
result.imageAddress =
static_cast<void*>(candidate_surface_->Surface());
} else {
result.imageAddress = nullptr;
}
result.imageAddress = (void*)candidate_surface_;
return result;
},
[this](LWE::WebContainer* c, bool isRendered) {
Expand Down Expand Up @@ -924,20 +937,17 @@ void WebView::HandleCookieMethodCall(

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

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_->buffer = static_cast<void*>(rendered_surface_->Surface());
gpu_buffer_->width = width;
gpu_buffer_->height = height;
}
Expand All @@ -946,10 +956,9 @@ FlutterDesktopGpuBuffer* WebView::ObtainGpuBuffer(size_t width, size_t height) {

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;
BufferUnit* unit = tbm_pool_->Find((tbm_surface_h)buffer);
if (unit) {
tbm_pool_->Release(unit);
}
}
}
7 changes: 5 additions & 2 deletions packages/webview_flutter/tizen/src/webview.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class WebContainer;
}

class TextInputChannel;
class BufferPool;
class BufferUnit;

class WebView : public PlatformView {
public:
Expand Down Expand Up @@ -68,8 +70,8 @@ class WebView : public PlatformView {
LWE::WebContainer* webview_instance_;
double width_;
double height_;
tbm_surface_h candidate_surface_;
tbm_surface_h rendered_surface_;
BufferUnit* candidate_surface_;
BufferUnit* rendered_surface_;
bool is_mouse_lbutton_down_;
bool has_navigation_delegate_;
bool has_progress_tracking_;
Expand All @@ -78,6 +80,7 @@ class WebView : public PlatformView {
flutter::TextureVariant* texture_variant_;
FlutterDesktopGpuBuffer* gpu_buffer_;
std::mutex mutex_;
std::unique_ptr<BufferPool> tbm_pool_;
};

#endif // FLUTTER_PLUGIN_WEBVIEW_FLUTTER_TIZEN_WEVIEW_H_