Skip to content

Commit 160223f

Browse files
xiaowei-guanswift-kim
authored andcommitted
Texture API change (#86)
* Add platfrom surface buffer structure * Support GPU buffer texture * Change file permission * Add FlutterDesktopGpuBufferTextureConfig in FlutterDesktopTextureInfo. * Add buffer parameter at Destruction callback * Change class name ExternalTextureGL to ExternalTextureSurfaceGL * Fix wild pointer issue * Convert unique_ptr to shared_ptr when create external texture * Fix arm64 build error
1 parent 2330b31 commit 160223f

16 files changed

+528
-270
lines changed

shell/platform/common/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/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/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.
@@ -40,6 +42,16 @@ typedef struct {
4042
void* release_context;
4143
} FlutterDesktopPixelBuffer;
4244

45+
// An image buffer object.
46+
typedef struct {
47+
// The gpu data buffer.
48+
const void* buffer;
49+
// Width of the gpu buffer.
50+
size_t width;
51+
// Height of the gpu buffer.
52+
size_t height;
53+
} FlutterDesktopGpuBuffer;
54+
4355
// The pixel buffer copy callback definition provided to
4456
// the Flutter engine to copy the texture.
4557
// It is invoked with the intended surface size specified by |width| and
@@ -54,6 +66,13 @@ typedef const FlutterDesktopPixelBuffer* (
5466
size_t height,
5567
void* user_data);
5668

69+
typedef const FlutterDesktopGpuBuffer* (
70+
*FlutterDesktopGpuBufferTextureCallback)(size_t width,
71+
size_t height,
72+
void* user_data);
73+
74+
typedef void (*FlutterDesktopGpuBufferDestructionCallback)(void* user_data);
75+
5776
// An object used to configure pixel buffer textures.
5877
typedef struct {
5978
// The callback used by the engine to copy the pixel buffer object.
@@ -62,10 +81,21 @@ typedef struct {
6281
void* user_data;
6382
} FlutterDesktopPixelBufferTextureConfig;
6483

84+
// An object used to configure GPU buffer textures.
85+
typedef struct {
86+
// The callback used by the engine to obtain the GPU buffer object.
87+
FlutterDesktopGpuBufferTextureCallback callback;
88+
// The callback used by the engine to destruct the GPU buffer object.
89+
FlutterDesktopGpuBufferDestructionCallback destruction_callback;
90+
// Opaque data that will get passed to the provided |callback|.
91+
void* user_data;
92+
} FlutterDesktopGPUBufferTextureConfig;
93+
6594
typedef struct {
6695
FlutterDesktopTextureType type;
6796
union {
6897
FlutterDesktopPixelBufferTextureConfig pixel_buffer_config;
98+
FlutterDesktopGPUBufferTextureConfig gpu_buffer_config;
6999
};
70100
} FlutterDesktopTextureInfo;
71101

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)