Skip to content

Commit

Permalink
Release buffer after creating egl image (#259)
Browse files Browse the repository at this point in the history
* Release buffer after creating egl image

* No need wait for flutter engine destruct callback to destory TBM
  buffer.

* Apply common release callback
  • Loading branch information
xiaowei-guan authored Mar 25, 2022
1 parent c03dc26 commit f1e1b01
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 57 deletions.
5 changes: 0 additions & 5 deletions shell/platform/common/client_wrapper/core_implementations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,6 @@ int64_t TextureRegistrarImpl::RegisterTexture(TextureVariant* texture) {
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 @@ -50,37 +50,23 @@ class GpuBufferTexture {
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) {}
GpuBufferTexture(ObtainGpuBufferCallback obtain_buffer_callback)
: obtain_gpu_buffer_callback_(obtain_buffer_callback) {}

// 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;
return obtain_gpu_buffer_callback_(width, height);
}

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.
Expand Down
12 changes: 6 additions & 6 deletions shell/platform/common/public/flutter_texture_registrar.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ typedef struct {
size_t width;
// Height of the gpu buffer.
size_t height;
// An optional callback that gets invoked when the |buffer| can be released.
void (*release_callback)(void* release_context);
// Opaque data passed to |release_callback|.
void* release_context;
} FlutterDesktopGpuBuffer;

// The pixel buffer copy callback definition provided to
Expand All @@ -71,8 +75,6 @@ typedef const FlutterDesktopGpuBuffer* (
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 @@ -85,17 +87,15 @@ typedef struct {
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;
} FlutterDesktopGpuBufferTextureConfig;

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

Expand Down
3 changes: 1 addition & 2 deletions shell/platform/tizen/external_texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct ExternalTextureGLState {
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> {
class ExternalTexture {
public:
ExternalTexture(ExternalTextureExtensionType gl_extension =
ExternalTextureExtensionType::kNone)
Expand All @@ -46,7 +46,6 @@ class ExternalTexture : public std::enable_shared_from_this<ExternalTexture> {
virtual bool PopulateTexture(size_t width,
size_t height,
FlutterOpenGLTexture* opengl_texture) = 0;
virtual void OnDestruction(){};

protected:
std::unique_ptr<ExternalTextureGLState> state_;
Expand Down
42 changes: 20 additions & 22 deletions shell/platform/tizen/external_texture_surface_gl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,12 @@ EVAS_GL_GLOBAL_GLES3_DECLARE();

namespace flutter {

static void OnCollectTexture(void* textureGL) {
auto* weak_texture =
reinterpret_cast<std::weak_ptr<ExternalTexture>*>(textureGL);
auto strong_texture = weak_texture->lock();
delete weak_texture;
if (strong_texture) {
strong_texture->OnDestruction();
}
}

ExternalTextureSurfaceGL::ExternalTextureSurfaceGL(
ExternalTextureExtensionType gl_extension,
FlutterDesktopGpuBufferTextureCallback texture_callback,
FlutterDesktopGpuBufferDestructionCallback destruction_callback,
void* user_data)
: ExternalTexture(gl_extension),
texture_callback_(texture_callback),
destruction_callback_(destruction_callback),
user_data_(user_data) {}

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

if (!gpu_buffer->buffer) {
FT_LOG(Info) << "tbm_surface is null for texture ID: " << texture_id_;
if (gpu_buffer->release_callback) {
gpu_buffer->release_callback(gpu_buffer->release_context);
}
return false;
}
const tbm_surface_h tbm_surface =
Expand All @@ -85,6 +76,9 @@ bool ExternalTextureSurfaceGL::PopulateTexture(
tbm_surface_info_s info;
if (tbm_surface_get_info(tbm_surface, &info) != TBM_SURFACE_ERROR_NONE) {
FT_LOG(Info) << "tbm_surface is invalid for texture ID: " << texture_id_;
if (gpu_buffer->release_callback) {
gpu_buffer->release_callback(gpu_buffer->release_context);
}
return false;
}

Expand All @@ -98,9 +92,15 @@ bool ExternalTextureSurfaceGL::PopulateTexture(
} else if (state_->gl_extension == ExternalTextureExtensionType::kDmaBuffer) {
FT_LOG(Error)
<< "EGL_EXT_image_dma_buf_import is not supported this renderer.";
if (gpu_buffer->release_callback) {
gpu_buffer->release_callback(gpu_buffer->release_context);
}
return false;
}
if (!egl_src_image) {
if (gpu_buffer->release_callback) {
gpu_buffer->release_callback(gpu_buffer->release_context);
}
return false;
}
if (state_->gl_texture == 0) {
Expand Down Expand Up @@ -179,6 +179,9 @@ bool ExternalTextureSurfaceGL::PopulateTexture(
FT_LOG(Error) << "Either EGL_TIZEN_image_native_surface or "
"EGL_EXT_image_dma_buf_import shoule be supported.";
}
if (gpu_buffer->release_callback) {
gpu_buffer->release_callback(gpu_buffer->release_context);
}
return false;
}
if (state_->gl_texture == 0) {
Expand Down Expand Up @@ -206,22 +209,17 @@ bool ExternalTextureSurfaceGL::PopulateTexture(
n_eglDestroyImageKHR(eglGetCurrentDisplay(), egl_src_image);
}
#endif

opengl_texture->target = GL_TEXTURE_EXTERNAL_OES;
opengl_texture->name = state_->gl_texture;
opengl_texture->format = GL_RGBA8;
opengl_texture->destruction_callback = OnCollectTexture;
auto* weak_texture = new std::weak_ptr<ExternalTexture>(shared_from_this());
opengl_texture->user_data = weak_texture;
opengl_texture->destruction_callback = nullptr;
opengl_texture->user_data = nullptr;
opengl_texture->width = width;
opengl_texture->height = height;
return true;
}

void ExternalTextureSurfaceGL::OnDestruction() {
if (destruction_callback_) {
destruction_callback_(user_data_);
if (gpu_buffer->release_callback) {
gpu_buffer->release_callback(gpu_buffer->release_context);
}
return true;
}

} // namespace flutter
3 changes: 0 additions & 3 deletions shell/platform/tizen/external_texture_surface_gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class ExternalTextureSurfaceGL : public ExternalTexture {
ExternalTextureSurfaceGL(
ExternalTextureExtensionType gl_extension,
FlutterDesktopGpuBufferTextureCallback texture_callback,
FlutterDesktopGpuBufferDestructionCallback destruction_callback,
void* user_data);

virtual ~ExternalTextureSurfaceGL();
Expand All @@ -32,11 +31,9 @@ class ExternalTextureSurfaceGL : public ExternalTexture {
bool PopulateTexture(size_t width,
size_t height,
FlutterOpenGLTexture* opengl_texture) override;
void OnDestruction() override;

private:
FlutterDesktopGpuBufferTextureCallback texture_callback_ = nullptr;
FlutterDesktopGpuBufferDestructionCallback destruction_callback_ = nullptr;
void* user_data_ = nullptr;
};

Expand Down
1 change: 0 additions & 1 deletion shell/platform/tizen/flutter_tizen_texture_registrar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ FlutterTizenTextureRegistrar::CreateExternalTexture(
}
return std::make_unique<ExternalTextureSurfaceGL>(
gl_extension, texture_info->gpu_buffer_config.callback,
texture_info->gpu_buffer_config.destruction_callback,
texture_info->gpu_buffer_config.user_data);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion shell/platform/tizen/flutter_tizen_texture_registrar.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class FlutterTizenTextureRegistrar {
FlutterTizenEngine* engine_ = nullptr;

// All registered textures, keyed by their IDs.
std::unordered_map<int64_t, std::shared_ptr<ExternalTexture>> textures_;
std::unordered_map<int64_t, std::unique_ptr<ExternalTexture>> textures_;
std::mutex map_mutex_;
};

Expand Down

0 comments on commit f1e1b01

Please sign in to comment.