Skip to content

Commit

Permalink
Texture api change (flutter-tizen#86)
Browse files Browse the repository at this point in the history
* Add platfrom surface buffer structor

* Texture api change(draft)

1.Delete tizen texture api, use common api, add GpuBufferTexture for gpu
buffer.

2.Delete ref/unref tbm_surface code in engine, add destructioncall for
delete tbm_surface.

3.Support pixel buffer texture.

* Revert code to original

* Support GPU buffer texture

1.Implement GPU buffer texture.
2.Remove ref/unref tbm surface code.

* Change file permission

* Add FlutterDesktopGpuBufferTextureConfig in FlutterDesktopTextureInfo.

* Add buffer parameter at Destruction callback

* Change class name ExternalTextureGL to ExternalTextureSurfaceGL

Class ExternamTextureSurfaceGL handle tbm surface texture render.

* Remove not used code

* Fix wild pointer issue

If plugin call UnregisterTexture, then will remove external texture,
But at this time, gpu render is not finish. when gpu render finished,
tiggle destruction callback, need check whether external texture pointer
is wild pointer

* Convert unique_ptr to shared_ptr when create external texture

* Code format

* Fix arm64 build error

* Remove not used file

* Remove unnecessary headers

* Refactor based on comments

1.Remove not used file.
2.Rename define of head file.
2.Rename of variable.

* Refactor based on comments

1.Return nullptr for default case when create external texture.
2.Rename methode name from texture_registrar to GetTextureRegistrar.
3.Change OnCollectTextur to static function.

* Fix code review issue

1.Use C++ style casts
2.Rename variable and function, make the code more friendly.
3.Add copyright info.

* Fix code review issue

1.Rename static veriable(nextTextureId -> kNextTextureId)
2.Modify comment, make it more clear.
3.Rename function name(FlutterDesktopDestructionCallback)

* Fix code review issue

* Remove warning log

This warning log shouldn't be displayed when launching a headless
app.
  • Loading branch information
xiaowei-guan authored and swift-kim committed Nov 14, 2021
1 parent 6a44376 commit 70887cf
Show file tree
Hide file tree
Showing 16 changed files with 528 additions and 270 deletions.
20 changes: 20 additions & 0 deletions shell/platform/common/client_wrapper/core_implementations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,26 @@ int64_t TextureRegistrarImpl::RegisterTexture(TextureVariant* texture) {
return buffer;
};

int64_t texture_id = FlutterDesktopTextureRegistrarRegisterExternalTexture(
texture_registrar_ref_, &info);
return texture_id;
} else if (auto gpu_buffer_texture = std::get_if<GpuBufferTexture>(texture)) {
FlutterDesktopTextureInfo info = {};
info.type = kFlutterDesktopGpuBufferTexture;
info.gpu_buffer_config.user_data = gpu_buffer_texture;
info.gpu_buffer_config.callback =
[](size_t width, size_t height,
void* user_data) -> const FlutterDesktopGpuBuffer* {
auto texture = static_cast<GpuBufferTexture*>(user_data);
auto buffer = texture->ObtainGpuBuffer(width, height);
return buffer;
};

info.gpu_buffer_config.destruction_callback = [](void* user_data) -> void {
auto texture = static_cast<GpuBufferTexture*>(user_data);
texture->Destruct();
};

int64_t texture_id = FlutterDesktopTextureRegistrarRegisterExternalTexture(
texture_registrar_ref_, &info);
return texture_id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,50 @@ class PixelBufferTexture {
const CopyBufferCallback copy_buffer_callback_;
};

// A gpu buffer texture.
class GpuBufferTexture {
public:
// A callback used for retrieving gpu buffers.
typedef std::function<const FlutterDesktopGpuBuffer*(size_t width,
size_t height)>
ObtainGpuBufferCallback;

typedef std::function<void(void* buffer)> DestructGpuBufferCallback;

// Creates a gpu buffer texture that uses the provided |obtain_buffer_cb| to
// retrieve the buffer.
// As the callback is usually invoked from the render thread, the callee must
// take care of proper synchronization. It also needs to be ensured that the
// returned buffer isn't released prior to unregistering this texture.
GpuBufferTexture(ObtainGpuBufferCallback obtain_buffer_callback,
DestructGpuBufferCallback destruction_callback)
: obtain_gpu_buffer_callback_(obtain_buffer_callback),
destruct_gpu_buffer_callback_(destruction_callback),
buffer_(nullptr) {}

// Returns the callback-provided FlutterDesktopGpuBuffer that contains the
// actual gpu buffer pointer. The intended surface size is specified by
// |width| and |height|.
const FlutterDesktopGpuBuffer* ObtainGpuBuffer(size_t width, size_t height) {
const FlutterDesktopGpuBuffer* flutter_buffer =
obtain_gpu_buffer_callback_(width, height);
if (flutter_buffer) {
buffer_ = const_cast<void*>(flutter_buffer->buffer);
}
return flutter_buffer;
}

void Destruct() { destruct_gpu_buffer_callback_(buffer_); }

private:
const ObtainGpuBufferCallback obtain_gpu_buffer_callback_;
const DestructGpuBufferCallback destruct_gpu_buffer_callback_;
void* buffer_;
};

// The available texture variants.
// Only PixelBufferTexture is currently implemented.
// Other variants are expected to be added in the future.
typedef std::variant<PixelBufferTexture> TextureVariant;
// Only PixelBufferTexture and GpuBufferTexture are currently implemented.
typedef std::variant<PixelBufferTexture, GpuBufferTexture> TextureVariant;

// An object keeping track of external textures.
//
Expand Down
32 changes: 31 additions & 1 deletion shell/platform/common/public/flutter_texture_registrar.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ typedef struct FlutterDesktopTextureRegistrar*
// Additional types may be added in the future.
typedef enum {
// A Pixel buffer-based texture.
kFlutterDesktopPixelBufferTexture
kFlutterDesktopPixelBufferTexture,
// A Gpu buffer-based texture.
kFlutterDesktopGpuBufferTexture
} FlutterDesktopTextureType;

// An image buffer object.
Expand All @@ -36,6 +38,16 @@ typedef struct {
size_t height;
} FlutterDesktopPixelBuffer;

// An image buffer object.
typedef struct {
// The gpu data buffer.
const void* buffer;
// Width of the gpu buffer.
size_t width;
// Height of the gpu buffer.
size_t height;
} FlutterDesktopGpuBuffer;

// The pixel buffer copy callback definition provided to
// the Flutter engine to copy the texture.
// It is invoked with the intended surface size specified by |width| and
Expand All @@ -50,6 +62,13 @@ typedef const FlutterDesktopPixelBuffer* (
size_t height,
void* user_data);

typedef const FlutterDesktopGpuBuffer* (
*FlutterDesktopGpuBufferTextureCallback)(size_t width,
size_t height,
void* user_data);

typedef void (*FlutterDesktopGpuBufferDestructionCallback)(void* user_data);

// An object used to configure pixel buffer textures.
typedef struct {
// The callback used by the engine to copy the pixel buffer object.
Expand All @@ -58,10 +77,21 @@ typedef struct {
void* user_data;
} FlutterDesktopPixelBufferTextureConfig;

// An object used to configure GPU buffer textures.
typedef struct {
// The callback used by the engine to obtain the GPU buffer object.
FlutterDesktopGpuBufferTextureCallback callback;
// The callback used by the engine to destruct the GPU buffer object.
FlutterDesktopGpuBufferDestructionCallback destruction_callback;
// Opaque data that will get passed to the provided |callback|.
void* user_data;
} FlutterDesktopGPUBufferTextureConfig;

typedef struct {
FlutterDesktopTextureType type;
union {
FlutterDesktopPixelBufferTextureConfig pixel_buffer_config;
FlutterDesktopGPUBufferTextureConfig gpu_buffer_config;
};
} FlutterDesktopTextureInfo;

Expand Down
5 changes: 3 additions & 2 deletions shell/platform/tizen/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ source_set("flutter_engine") {

_public_headers = [
"public/flutter_platform_view.h",
"public/flutter_tizen_texture_registrar.h",
"public/flutter_tizen.h",
]

Expand Down Expand Up @@ -95,9 +94,11 @@ template("embedder_for_profile") {
"channels/platform_view_channel.cc",
"channels/settings_channel.cc",
"channels/text_input_channel.cc",
"external_texture_gl.cc",
"external_texture_pixel_gl.cc",
"external_texture_surface_gl.cc",
"flutter_tizen.cc",
"flutter_tizen_engine.cc",
"flutter_tizen_texture_registrar.cc",
"key_event_handler.cc",
"tizen_event_loop.cc",
"tizen_log.cc",
Expand Down
49 changes: 49 additions & 0 deletions shell/platform/tizen/external_texture.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2020 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 EMBEDDER_EXTERNAL_TEXTURE_H_
#define EMBEDDER_EXTERNAL_TEXTURE_H_

#include <atomic>
#include <memory>
#include "flutter/shell/platform/common/cpp/public/flutter_texture_registrar.h"
#include "flutter/shell/platform/embedder/embedder.h"

#ifdef TIZEN_RENDERER_EVAS_GL
#undef EFL_BETA_API_SUPPORT
#include <Evas_GL.h>
#else
#include <GLES2/gl2.h>
#endif

struct ExternalTextureGLState {
GLuint gl_texture;
};

static std::atomic_long next_texture_id = {1};

// An adaptation class of flutter engine and external texture interface.
class ExternalTexture : public std::enable_shared_from_this<ExternalTexture> {
public:
ExternalTexture()
: state_(std::make_unique<ExternalTextureGLState>()),
texture_id_(next_texture_id++) {}
virtual ~ExternalTexture() = default;

/**
* Returns the unique id for the ExternalTextureGL instance.
*/
int64_t TextureId() { return (int64_t)texture_id_; }

virtual bool PopulateTexture(size_t width,
size_t height,
FlutterOpenGLTexture* opengl_texture) = 0;
virtual void OnDestruction(){};

protected:
std::unique_ptr<ExternalTextureGLState> state_;
const long texture_id_{0};
};

#endif // EMBEDDER_EXTERNAL_TEXTURE_H_
53 changes: 0 additions & 53 deletions shell/platform/tizen/external_texture_gl.h

This file was deleted.

70 changes: 70 additions & 0 deletions shell/platform/tizen/external_texture_pixel_gl.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2020 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 "flutter/shell/platform/tizen/external_texture_pixel_gl.h"

#ifdef TIZEN_RENDERER_EVAS_GL
#undef EFL_BETA_API_SUPPORT
#include <Evas_GL_GLES3_Helpers.h>
extern Evas_GL* g_evas_gl;
EVAS_GL_GLOBAL_GLES3_DECLARE();
#else
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <GLES2/gl2ext.h>
#include <GLES3/gl32.h>
#endif

bool ExternalTexturePixelGL::PopulateTexture(
size_t width,
size_t height,
FlutterOpenGLTexture* opengl_texture) {
if (!CopyPixelBuffer(width, height)) {
return false;
}

// Populate the texture object used by the engine.
opengl_texture->target = GL_TEXTURE_2D;
opengl_texture->name = state_->gl_texture;
opengl_texture->format = GL_RGBA8;
opengl_texture->destruction_callback = nullptr;
opengl_texture->user_data = nullptr;
opengl_texture->width = width;
opengl_texture->height = height;
return true;
}

ExternalTexturePixelGL::ExternalTexturePixelGL(
FlutterDesktopPixelBufferTextureCallback texture_callback,
void* user_data)
: ExternalTexture(),
texture_callback_(texture_callback),
user_data_(user_data) {}

bool ExternalTexturePixelGL::CopyPixelBuffer(size_t& width, size_t& height) {
const FlutterDesktopPixelBuffer* pixel_buffer =
texture_callback_(width, height, user_data_);

if (!pixel_buffer || !pixel_buffer->buffer) {
return false;
}

width = pixel_buffer->width;
height = pixel_buffer->height;

if (state_->gl_texture == 0) {
glGenTextures(1, &state_->gl_texture);
glBindTexture(GL_TEXTURE_2D, state_->gl_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
} else {
glBindTexture(GL_TEXTURE_2D, state_->gl_texture);
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pixel_buffer->width,
pixel_buffer->height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
pixel_buffer->buffer);
return true;
}
34 changes: 34 additions & 0 deletions shell/platform/tizen/external_texture_pixel_gl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2020 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 EMBEDDER_EXTERNAL_TEXTURE_PIXEL_GL_H
#define EMBEDDER_EXTERNAL_TEXTURE_PIXEL_GL_H

#include <memory>

#include "flutter/shell/platform/common/cpp/public/flutter_texture_registrar.h"
#include "flutter/shell/platform/embedder/embedder.h"
#include "flutter/shell/platform/tizen/external_texture.h"

// An adaptation class of flutter engine and external texture interface.
class ExternalTexturePixelGL : public ExternalTexture {
public:
ExternalTexturePixelGL(
FlutterDesktopPixelBufferTextureCallback texture_callback,
void* user_data);

~ExternalTexturePixelGL() = default;

bool PopulateTexture(size_t width,
size_t height,
FlutterOpenGLTexture* opengl_texture) override;

bool CopyPixelBuffer(size_t& width, size_t& height);

private:
FlutterDesktopPixelBufferTextureCallback texture_callback_ = nullptr;
void* user_data_ = nullptr;
};

#endif // EMBEDDER_EXTERNAL_TEXTURE_PIXEL_GL_H
Loading

0 comments on commit 70887cf

Please sign in to comment.