diff --git a/shell/platform/common/client_wrapper/core_implementations.cc b/shell/platform/common/client_wrapper/core_implementations.cc index 1412db7c939f2..78efc6b3ad92b 100644 --- a/shell/platform/common/client_wrapper/core_implementations.cc +++ b/shell/platform/common/client_wrapper/core_implementations.cc @@ -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(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(user_data); + auto buffer = texture->ObtainGpuBuffer(width, height); + return buffer; + }; + + info.gpu_buffer_config.destruction_callback = [](void* user_data) -> void { + auto texture = static_cast(user_data); + texture->Destruct(); + }; + int64_t texture_id = FlutterDesktopTextureRegistrarRegisterExternalTexture( texture_registrar_ref_, &info); return texture_id; diff --git a/shell/platform/common/client_wrapper/include/flutter/texture_registrar.h b/shell/platform/common/client_wrapper/include/flutter/texture_registrar.h index 5b59829f24283..c187ace4ab1c4 100644 --- a/shell/platform/common/client_wrapper/include/flutter/texture_registrar.h +++ b/shell/platform/common/client_wrapper/include/flutter/texture_registrar.h @@ -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 + ObtainGpuBufferCallback; + + typedef std::function 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(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 TextureVariant; +// Only PixelBufferTexture and GpuBufferTexture are currently implemented. +typedef std::variant TextureVariant; // An object keeping track of external textures. // diff --git a/shell/platform/common/public/flutter_texture_registrar.h b/shell/platform/common/public/flutter_texture_registrar.h index b115234e273de..e2bfcfb6bfdf8 100644 --- a/shell/platform/common/public/flutter_texture_registrar.h +++ b/shell/platform/common/public/flutter_texture_registrar.h @@ -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. @@ -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 @@ -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. @@ -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;