Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions shell/platform/common/client_wrapper/core_implementations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<GpuBufferTexture>(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<GpuBufferTexture*>(user_data);
auto buffer = texture->ObtainGpuBuffer(width, height);
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 @@ -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<const FlutterDesktopGpuBuffer*(size_t width,
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) {}

// 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;
}

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<PixelBufferTexture> TextureVariant;
// Only PixelBufferTexture and GpuBufferTexture are currently implemented.
typedef std::variant<PixelBufferTexture, GpuBufferTexture> TextureVariant;

// An object keeping track of external textures.
//
Expand Down
32 changes: 31 additions & 1 deletion shell/platform/common/public/flutter_texture_registrar.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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;

Expand Down