Skip to content

Commit f1e1b01

Browse files
authored
Release buffer after creating egl image (#259)
* Release buffer after creating egl image * No need wait for flutter engine destruct callback to destory TBM buffer. * Apply common release callback
1 parent c03dc26 commit f1e1b01

File tree

8 files changed

+31
-57
lines changed

8 files changed

+31
-57
lines changed

shell/platform/common/client_wrapper/core_implementations.cc

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,6 @@ int64_t TextureRegistrarImpl::RegisterTexture(TextureVariant* texture) {
184184
return buffer;
185185
};
186186

187-
info.gpu_buffer_config.destruction_callback = [](void* user_data) -> void {
188-
auto texture = static_cast<GpuBufferTexture*>(user_data);
189-
texture->Destruct();
190-
};
191-
192187
int64_t texture_id = FlutterDesktopTextureRegistrarRegisterExternalTexture(
193188
texture_registrar_ref_, &info);
194189
return texture_id;

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

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,37 +50,23 @@ class GpuBufferTexture {
5050
size_t height)>
5151
ObtainGpuBufferCallback;
5252

53-
typedef std::function<void(void* buffer)> DestructGpuBufferCallback;
54-
5553
// Creates a gpu buffer texture that uses the provided |obtain_buffer_cb| to
5654
// retrieve the buffer.
5755
// As the callback is usually invoked from the render thread, the callee must
5856
// take care of proper synchronization. It also needs to be ensured that the
5957
// 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) {}
58+
GpuBufferTexture(ObtainGpuBufferCallback obtain_buffer_callback)
59+
: obtain_gpu_buffer_callback_(obtain_buffer_callback) {}
6560

6661
// Returns the callback-provided FlutterDesktopGpuBuffer that contains the
6762
// actual gpu buffer pointer. The intended surface size is specified by
6863
// |width| and |height|.
6964
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;
65+
return obtain_gpu_buffer_callback_(width, height);
7666
}
7767

78-
void Destruct() { destruct_gpu_buffer_callback_(buffer_); }
79-
8068
private:
8169
const ObtainGpuBufferCallback obtain_gpu_buffer_callback_;
82-
const DestructGpuBufferCallback destruct_gpu_buffer_callback_;
83-
void* buffer_;
8470
};
8571

8672
// The available texture variants.

shell/platform/common/public/flutter_texture_registrar.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ typedef struct {
5050
size_t width;
5151
// Height of the gpu buffer.
5252
size_t height;
53+
// An optional callback that gets invoked when the |buffer| can be released.
54+
void (*release_callback)(void* release_context);
55+
// Opaque data passed to |release_callback|.
56+
void* release_context;
5357
} FlutterDesktopGpuBuffer;
5458

5559
// The pixel buffer copy callback definition provided to
@@ -71,8 +75,6 @@ typedef const FlutterDesktopGpuBuffer* (
7175
size_t height,
7276
void* user_data);
7377

74-
typedef void (*FlutterDesktopGpuBufferDestructionCallback)(void* user_data);
75-
7678
// An object used to configure pixel buffer textures.
7779
typedef struct {
7880
// The callback used by the engine to copy the pixel buffer object.
@@ -85,17 +87,15 @@ typedef struct {
8587
typedef struct {
8688
// The callback used by the engine to obtain the GPU buffer object.
8789
FlutterDesktopGpuBufferTextureCallback callback;
88-
// The callback used by the engine to destruct the GPU buffer object.
89-
FlutterDesktopGpuBufferDestructionCallback destruction_callback;
9090
// Opaque data that will get passed to the provided |callback|.
9191
void* user_data;
92-
} FlutterDesktopGPUBufferTextureConfig;
92+
} FlutterDesktopGpuBufferTextureConfig;
9393

9494
typedef struct {
9595
FlutterDesktopTextureType type;
9696
union {
9797
FlutterDesktopPixelBufferTextureConfig pixel_buffer_config;
98-
FlutterDesktopGPUBufferTextureConfig gpu_buffer_config;
98+
FlutterDesktopGpuBufferTextureConfig gpu_buffer_config;
9999
};
100100
} FlutterDesktopTextureInfo;
101101

shell/platform/tizen/external_texture.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct ExternalTextureGLState {
3030
static std::atomic_long next_texture_id = {1};
3131

3232
// An adaptation class of flutter engine and external texture interface.
33-
class ExternalTexture : public std::enable_shared_from_this<ExternalTexture> {
33+
class ExternalTexture {
3434
public:
3535
ExternalTexture(ExternalTextureExtensionType gl_extension =
3636
ExternalTextureExtensionType::kNone)
@@ -46,7 +46,6 @@ class ExternalTexture : public std::enable_shared_from_this<ExternalTexture> {
4646
virtual bool PopulateTexture(size_t width,
4747
size_t height,
4848
FlutterOpenGLTexture* opengl_texture) = 0;
49-
virtual void OnDestruction(){};
5049

5150
protected:
5251
std::unique_ptr<ExternalTextureGLState> state_;

shell/platform/tizen/external_texture_surface_gl.cc

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,12 @@ EVAS_GL_GLOBAL_GLES3_DECLARE();
3434

3535
namespace flutter {
3636

37-
static void OnCollectTexture(void* textureGL) {
38-
auto* weak_texture =
39-
reinterpret_cast<std::weak_ptr<ExternalTexture>*>(textureGL);
40-
auto strong_texture = weak_texture->lock();
41-
delete weak_texture;
42-
if (strong_texture) {
43-
strong_texture->OnDestruction();
44-
}
45-
}
46-
4737
ExternalTextureSurfaceGL::ExternalTextureSurfaceGL(
4838
ExternalTextureExtensionType gl_extension,
4939
FlutterDesktopGpuBufferTextureCallback texture_callback,
50-
FlutterDesktopGpuBufferDestructionCallback destruction_callback,
5140
void* user_data)
5241
: ExternalTexture(gl_extension),
5342
texture_callback_(texture_callback),
54-
destruction_callback_(destruction_callback),
5543
user_data_(user_data) {}
5644

5745
ExternalTextureSurfaceGL::~ExternalTextureSurfaceGL() {
@@ -77,6 +65,9 @@ bool ExternalTextureSurfaceGL::PopulateTexture(
7765

7866
if (!gpu_buffer->buffer) {
7967
FT_LOG(Info) << "tbm_surface is null for texture ID: " << texture_id_;
68+
if (gpu_buffer->release_callback) {
69+
gpu_buffer->release_callback(gpu_buffer->release_context);
70+
}
8071
return false;
8172
}
8273
const tbm_surface_h tbm_surface =
@@ -85,6 +76,9 @@ bool ExternalTextureSurfaceGL::PopulateTexture(
8576
tbm_surface_info_s info;
8677
if (tbm_surface_get_info(tbm_surface, &info) != TBM_SURFACE_ERROR_NONE) {
8778
FT_LOG(Info) << "tbm_surface is invalid for texture ID: " << texture_id_;
79+
if (gpu_buffer->release_callback) {
80+
gpu_buffer->release_callback(gpu_buffer->release_context);
81+
}
8882
return false;
8983
}
9084

@@ -98,9 +92,15 @@ bool ExternalTextureSurfaceGL::PopulateTexture(
9892
} else if (state_->gl_extension == ExternalTextureExtensionType::kDmaBuffer) {
9993
FT_LOG(Error)
10094
<< "EGL_EXT_image_dma_buf_import is not supported this renderer.";
95+
if (gpu_buffer->release_callback) {
96+
gpu_buffer->release_callback(gpu_buffer->release_context);
97+
}
10198
return false;
10299
}
103100
if (!egl_src_image) {
101+
if (gpu_buffer->release_callback) {
102+
gpu_buffer->release_callback(gpu_buffer->release_context);
103+
}
104104
return false;
105105
}
106106
if (state_->gl_texture == 0) {
@@ -179,6 +179,9 @@ bool ExternalTextureSurfaceGL::PopulateTexture(
179179
FT_LOG(Error) << "Either EGL_TIZEN_image_native_surface or "
180180
"EGL_EXT_image_dma_buf_import shoule be supported.";
181181
}
182+
if (gpu_buffer->release_callback) {
183+
gpu_buffer->release_callback(gpu_buffer->release_context);
184+
}
182185
return false;
183186
}
184187
if (state_->gl_texture == 0) {
@@ -206,22 +209,17 @@ bool ExternalTextureSurfaceGL::PopulateTexture(
206209
n_eglDestroyImageKHR(eglGetCurrentDisplay(), egl_src_image);
207210
}
208211
#endif
209-
210212
opengl_texture->target = GL_TEXTURE_EXTERNAL_OES;
211213
opengl_texture->name = state_->gl_texture;
212214
opengl_texture->format = GL_RGBA8;
213-
opengl_texture->destruction_callback = OnCollectTexture;
214-
auto* weak_texture = new std::weak_ptr<ExternalTexture>(shared_from_this());
215-
opengl_texture->user_data = weak_texture;
215+
opengl_texture->destruction_callback = nullptr;
216+
opengl_texture->user_data = nullptr;
216217
opengl_texture->width = width;
217218
opengl_texture->height = height;
218-
return true;
219-
}
220-
221-
void ExternalTextureSurfaceGL::OnDestruction() {
222-
if (destruction_callback_) {
223-
destruction_callback_(user_data_);
219+
if (gpu_buffer->release_callback) {
220+
gpu_buffer->release_callback(gpu_buffer->release_context);
224221
}
222+
return true;
225223
}
226224

227225
} // namespace flutter

shell/platform/tizen/external_texture_surface_gl.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ class ExternalTextureSurfaceGL : public ExternalTexture {
1717
ExternalTextureSurfaceGL(
1818
ExternalTextureExtensionType gl_extension,
1919
FlutterDesktopGpuBufferTextureCallback texture_callback,
20-
FlutterDesktopGpuBufferDestructionCallback destruction_callback,
2120
void* user_data);
2221

2322
virtual ~ExternalTextureSurfaceGL();
@@ -32,11 +31,9 @@ class ExternalTextureSurfaceGL : public ExternalTexture {
3231
bool PopulateTexture(size_t width,
3332
size_t height,
3433
FlutterOpenGLTexture* opengl_texture) override;
35-
void OnDestruction() override;
3634

3735
private:
3836
FlutterDesktopGpuBufferTextureCallback texture_callback_ = nullptr;
39-
FlutterDesktopGpuBufferDestructionCallback destruction_callback_ = nullptr;
4037
void* user_data_ = nullptr;
4138
};
4239

shell/platform/tizen/flutter_tizen_texture_registrar.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ FlutterTizenTextureRegistrar::CreateExternalTexture(
106106
}
107107
return std::make_unique<ExternalTextureSurfaceGL>(
108108
gl_extension, texture_info->gpu_buffer_config.callback,
109-
texture_info->gpu_buffer_config.destruction_callback,
110109
texture_info->gpu_buffer_config.user_data);
111110
break;
112111
}

shell/platform/tizen/flutter_tizen_texture_registrar.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class FlutterTizenTextureRegistrar {
5252
FlutterTizenEngine* engine_ = nullptr;
5353

5454
// All registered textures, keyed by their IDs.
55-
std::unordered_map<int64_t, std::shared_ptr<ExternalTexture>> textures_;
55+
std::unordered_map<int64_t, std::unique_ptr<ExternalTexture>> textures_;
5656
std::mutex map_mutex_;
5757
};
5858

0 commit comments

Comments
 (0)