Skip to content

Commit 9f08d18

Browse files
authored
Texture api change (#86)
* 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.
1 parent da7932c commit 9f08d18

16 files changed

+528
-270
lines changed

shell/platform/common/cpp/client_wrapper/core_implementations.cc

+20
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,26 @@ int64_t TextureRegistrarImpl::RegisterTexture(TextureVariant* texture) {
169169
return buffer;
170170
};
171171

172+
int64_t texture_id = FlutterDesktopTextureRegistrarRegisterExternalTexture(
173+
texture_registrar_ref_, &info);
174+
return texture_id;
175+
} else if (auto gpu_buffer_texture = std::get_if<GpuBufferTexture>(texture)) {
176+
FlutterDesktopTextureInfo info = {};
177+
info.type = kFlutterDesktopGpuBufferTexture;
178+
info.gpu_buffer_config.user_data = gpu_buffer_texture;
179+
info.gpu_buffer_config.callback =
180+
[](size_t width, size_t height,
181+
void* user_data) -> const FlutterDesktopGpuBuffer* {
182+
auto texture = static_cast<GpuBufferTexture*>(user_data);
183+
auto buffer = texture->ObtainGpuBuffer(width, height);
184+
return buffer;
185+
};
186+
187+
info.gpu_buffer_config.destruction_callback = [](void* user_data) -> void {
188+
auto texture = static_cast<GpuBufferTexture*>(user_data);
189+
texture->Destruct();
190+
};
191+
172192
int64_t texture_id = FlutterDesktopTextureRegistrarRegisterExternalTexture(
173193
texture_registrar_ref_, &info);
174194
return texture_id;

shell/platform/common/cpp/client_wrapper/include/flutter/texture_registrar.h

+43-3
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,50 @@ class PixelBufferTexture {
4242
const CopyBufferCallback copy_buffer_callback_;
4343
};
4444

45+
// A gpu buffer texture.
46+
class GpuBufferTexture {
47+
public:
48+
// A callback used for retrieving gpu buffers.
49+
typedef std::function<const FlutterDesktopGpuBuffer*(size_t width,
50+
size_t height)>
51+
ObtainGpuBufferCallback;
52+
53+
typedef std::function<void(void* buffer)> DestructGpuBufferCallback;
54+
55+
// Creates a gpu buffer texture that uses the provided |obtain_buffer_cb| to
56+
// retrieve the buffer.
57+
// As the callback is usually invoked from the render thread, the callee must
58+
// take care of proper synchronization. It also needs to be ensured that the
59+
// returned buffer isn't released prior to unregistering this texture.
60+
GpuBufferTexture(ObtainGpuBufferCallback obtain_buffer_callback,
61+
DestructGpuBufferCallback destruction_callback)
62+
: obtain_gpu_buffer_callback_(obtain_buffer_callback),
63+
destruct_gpu_buffer_callback_(destruction_callback),
64+
buffer_(nullptr) {}
65+
66+
// Returns the callback-provided FlutterDesktopGpuBuffer that contains the
67+
// actual gpu buffer pointer. The intended surface size is specified by
68+
// |width| and |height|.
69+
const FlutterDesktopGpuBuffer* ObtainGpuBuffer(size_t width, size_t height) {
70+
const FlutterDesktopGpuBuffer* flutter_buffer =
71+
obtain_gpu_buffer_callback_(width, height);
72+
if (flutter_buffer) {
73+
buffer_ = const_cast<void*>(flutter_buffer->buffer);
74+
}
75+
return flutter_buffer;
76+
}
77+
78+
void Destruct() { destruct_gpu_buffer_callback_(buffer_); }
79+
80+
private:
81+
const ObtainGpuBufferCallback obtain_gpu_buffer_callback_;
82+
const DestructGpuBufferCallback destruct_gpu_buffer_callback_;
83+
void* buffer_;
84+
};
85+
4586
// The available texture variants.
46-
// Only PixelBufferTexture is currently implemented.
47-
// Other variants are expected to be added in the future.
48-
typedef std::variant<PixelBufferTexture> TextureVariant;
87+
// Only PixelBufferTexture and GpuBufferTexture are currently implemented.
88+
typedef std::variant<PixelBufferTexture, GpuBufferTexture> TextureVariant;
4989

5090
// An object keeping track of external textures.
5191
//

shell/platform/common/cpp/public/flutter_texture_registrar.h

+31-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ typedef struct FlutterDesktopTextureRegistrar*
2323
// Additional types may be added in the future.
2424
typedef enum {
2525
// A Pixel buffer-based texture.
26-
kFlutterDesktopPixelBufferTexture
26+
kFlutterDesktopPixelBufferTexture,
27+
// A Gpu buffer-based texture.
28+
kFlutterDesktopGpuBufferTexture
2729
} FlutterDesktopTextureType;
2830

2931
// An image buffer object.
@@ -36,6 +38,16 @@ typedef struct {
3638
size_t height;
3739
} FlutterDesktopPixelBuffer;
3840

41+
// An image buffer object.
42+
typedef struct {
43+
// The gpu data buffer.
44+
const void* buffer;
45+
// Width of the gpu buffer.
46+
size_t width;
47+
// Height of the gpu buffer.
48+
size_t height;
49+
} FlutterDesktopGpuBuffer;
50+
3951
// The pixel buffer copy callback definition provided to
4052
// the Flutter engine to copy the texture.
4153
// It is invoked with the intended surface size specified by |width| and
@@ -50,6 +62,13 @@ typedef const FlutterDesktopPixelBuffer* (
5062
size_t height,
5163
void* user_data);
5264

65+
typedef const FlutterDesktopGpuBuffer* (
66+
*FlutterDesktopGpuBufferTextureCallback)(size_t width,
67+
size_t height,
68+
void* user_data);
69+
70+
typedef void (*FlutterDesktopGpuBufferDestructionCallback)(void* user_data);
71+
5372
// An object used to configure pixel buffer textures.
5473
typedef struct {
5574
// The callback used by the engine to copy the pixel buffer object.
@@ -58,10 +77,21 @@ typedef struct {
5877
void* user_data;
5978
} FlutterDesktopPixelBufferTextureConfig;
6079

80+
// An object used to configure GPU buffer textures.
81+
typedef struct {
82+
// The callback used by the engine to obtain the GPU buffer object.
83+
FlutterDesktopGpuBufferTextureCallback callback;
84+
// The callback used by the engine to destruct the GPU buffer object.
85+
FlutterDesktopGpuBufferDestructionCallback destruction_callback;
86+
// Opaque data that will get passed to the provided |callback|.
87+
void* user_data;
88+
} FlutterDesktopGPUBufferTextureConfig;
89+
6190
typedef struct {
6291
FlutterDesktopTextureType type;
6392
union {
6493
FlutterDesktopPixelBufferTextureConfig pixel_buffer_config;
94+
FlutterDesktopGPUBufferTextureConfig gpu_buffer_config;
6595
};
6696
} FlutterDesktopTextureInfo;
6797

shell/platform/tizen/BUILD.gn

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ source_set("flutter_engine") {
2626

2727
_public_headers = [
2828
"public/flutter_platform_view.h",
29-
"public/flutter_tizen_texture_registrar.h",
3029
"public/flutter_tizen.h",
3130
]
3231

@@ -95,9 +94,11 @@ template("embedder_for_profile") {
9594
"channels/platform_view_channel.cc",
9695
"channels/settings_channel.cc",
9796
"channels/text_input_channel.cc",
98-
"external_texture_gl.cc",
97+
"external_texture_pixel_gl.cc",
98+
"external_texture_surface_gl.cc",
9999
"flutter_tizen.cc",
100100
"flutter_tizen_engine.cc",
101+
"flutter_tizen_texture_registrar.cc",
101102
"key_event_handler.cc",
102103
"tizen_event_loop.cc",
103104
"tizen_log.cc",
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2020 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 EMBEDDER_EXTERNAL_TEXTURE_H_
6+
#define EMBEDDER_EXTERNAL_TEXTURE_H_
7+
8+
#include <atomic>
9+
#include <memory>
10+
#include "flutter/shell/platform/common/cpp/public/flutter_texture_registrar.h"
11+
#include "flutter/shell/platform/embedder/embedder.h"
12+
13+
#ifdef TIZEN_RENDERER_EVAS_GL
14+
#undef EFL_BETA_API_SUPPORT
15+
#include <Evas_GL.h>
16+
#else
17+
#include <GLES2/gl2.h>
18+
#endif
19+
20+
struct ExternalTextureGLState {
21+
GLuint gl_texture;
22+
};
23+
24+
static std::atomic_long next_texture_id = {1};
25+
26+
// An adaptation class of flutter engine and external texture interface.
27+
class ExternalTexture : public std::enable_shared_from_this<ExternalTexture> {
28+
public:
29+
ExternalTexture()
30+
: state_(std::make_unique<ExternalTextureGLState>()),
31+
texture_id_(next_texture_id++) {}
32+
virtual ~ExternalTexture() = default;
33+
34+
/**
35+
* Returns the unique id for the ExternalTextureGL instance.
36+
*/
37+
int64_t TextureId() { return (int64_t)texture_id_; }
38+
39+
virtual bool PopulateTexture(size_t width,
40+
size_t height,
41+
FlutterOpenGLTexture* opengl_texture) = 0;
42+
virtual void OnDestruction(){};
43+
44+
protected:
45+
std::unique_ptr<ExternalTextureGLState> state_;
46+
const long texture_id_{0};
47+
};
48+
49+
#endif // EMBEDDER_EXTERNAL_TEXTURE_H_

shell/platform/tizen/external_texture_gl.h

-53
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2020 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 "flutter/shell/platform/tizen/external_texture_pixel_gl.h"
6+
7+
#ifdef TIZEN_RENDERER_EVAS_GL
8+
#undef EFL_BETA_API_SUPPORT
9+
#include <Evas_GL_GLES3_Helpers.h>
10+
extern Evas_GL* g_evas_gl;
11+
EVAS_GL_GLOBAL_GLES3_DECLARE();
12+
#else
13+
#include <EGL/egl.h>
14+
#include <EGL/eglext.h>
15+
#include <GLES2/gl2ext.h>
16+
#include <GLES3/gl32.h>
17+
#endif
18+
19+
bool ExternalTexturePixelGL::PopulateTexture(
20+
size_t width,
21+
size_t height,
22+
FlutterOpenGLTexture* opengl_texture) {
23+
if (!CopyPixelBuffer(width, height)) {
24+
return false;
25+
}
26+
27+
// Populate the texture object used by the engine.
28+
opengl_texture->target = GL_TEXTURE_2D;
29+
opengl_texture->name = state_->gl_texture;
30+
opengl_texture->format = GL_RGBA8;
31+
opengl_texture->destruction_callback = nullptr;
32+
opengl_texture->user_data = nullptr;
33+
opengl_texture->width = width;
34+
opengl_texture->height = height;
35+
return true;
36+
}
37+
38+
ExternalTexturePixelGL::ExternalTexturePixelGL(
39+
FlutterDesktopPixelBufferTextureCallback texture_callback,
40+
void* user_data)
41+
: ExternalTexture(),
42+
texture_callback_(texture_callback),
43+
user_data_(user_data) {}
44+
45+
bool ExternalTexturePixelGL::CopyPixelBuffer(size_t& width, size_t& height) {
46+
const FlutterDesktopPixelBuffer* pixel_buffer =
47+
texture_callback_(width, height, user_data_);
48+
49+
if (!pixel_buffer || !pixel_buffer->buffer) {
50+
return false;
51+
}
52+
53+
width = pixel_buffer->width;
54+
height = pixel_buffer->height;
55+
56+
if (state_->gl_texture == 0) {
57+
glGenTextures(1, &state_->gl_texture);
58+
glBindTexture(GL_TEXTURE_2D, state_->gl_texture);
59+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
60+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
61+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
62+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
63+
} else {
64+
glBindTexture(GL_TEXTURE_2D, state_->gl_texture);
65+
}
66+
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pixel_buffer->width,
67+
pixel_buffer->height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
68+
pixel_buffer->buffer);
69+
return true;
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2020 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 EMBEDDER_EXTERNAL_TEXTURE_PIXEL_GL_H
6+
#define EMBEDDER_EXTERNAL_TEXTURE_PIXEL_GL_H
7+
8+
#include <memory>
9+
10+
#include "flutter/shell/platform/common/cpp/public/flutter_texture_registrar.h"
11+
#include "flutter/shell/platform/embedder/embedder.h"
12+
#include "flutter/shell/platform/tizen/external_texture.h"
13+
14+
// An adaptation class of flutter engine and external texture interface.
15+
class ExternalTexturePixelGL : public ExternalTexture {
16+
public:
17+
ExternalTexturePixelGL(
18+
FlutterDesktopPixelBufferTextureCallback texture_callback,
19+
void* user_data);
20+
21+
~ExternalTexturePixelGL() = default;
22+
23+
bool PopulateTexture(size_t width,
24+
size_t height,
25+
FlutterOpenGLTexture* opengl_texture) override;
26+
27+
bool CopyPixelBuffer(size_t& width, size_t& height);
28+
29+
private:
30+
FlutterDesktopPixelBufferTextureCallback texture_callback_ = nullptr;
31+
void* user_data_ = nullptr;
32+
};
33+
34+
#endif // EMBEDDER_EXTERNAL_TEXTURE_PIXEL_GL_H

0 commit comments

Comments
 (0)