Flutter macOS Embedder
texture_registrar.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. 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 FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_TEXTURE_REGISTRAR_H_
6 #define FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_TEXTURE_REGISTRAR_H_
7 
9 
10 #include <cstdint>
11 #include <functional>
12 #include <memory>
13 #include <utility>
14 #include <variant>
15 
16 namespace flutter {
17 
18 // A pixel buffer texture.
20  public:
21  // A callback used for retrieving pixel buffers.
22  typedef std::function<const FlutterDesktopPixelBuffer*(size_t width,
23  size_t height)>
25 
26  // Creates a pixel buffer texture that uses the provided |copy_buffer_cb| to
27  // retrieve the buffer.
28  // As the callback is usually invoked from the render thread, the callee must
29  // take care of proper synchronization. It also needs to be ensured that the
30  // returned buffer isn't released prior to unregistering this texture.
31  explicit PixelBufferTexture(CopyBufferCallback copy_buffer_callback)
32  : copy_buffer_callback_(std::move(copy_buffer_callback)) {}
33 
34  // Returns the callback-provided FlutterDesktopPixelBuffer that contains the
35  // actual pixel data. The intended surface size is specified by |width| and
36  // |height|.
38  size_t height) const {
39  return copy_buffer_callback_(width, height);
40  }
41 
42  private:
43  const CopyBufferCallback copy_buffer_callback_;
44 };
45 
46 // A GPU surface-based texture.
48  public:
49  // A callback used for retrieving surface descriptors.
50  typedef std::function<
51  const FlutterDesktopGpuSurfaceDescriptor*(size_t width, size_t height)>
53 
55  ObtainDescriptorCallback obtain_descriptor_callback)
56  : surface_type_(surface_type),
57  obtain_descriptor_callback_(std::move(obtain_descriptor_callback)) {}
58 
59  // Returns the callback-provided FlutterDesktopGpuSurfaceDescriptor that
60  // contains the surface handle. The intended surface size is specified by
61  // |width| and |height|.
63  size_t width,
64  size_t height) const {
65  return obtain_descriptor_callback_(width, height);
66  }
67 
68  // Gets the surface type.
69  FlutterDesktopGpuSurfaceType surface_type() const { return surface_type_; }
70 
71  private:
72  const FlutterDesktopGpuSurfaceType surface_type_;
73  const ObtainDescriptorCallback obtain_descriptor_callback_;
74 };
75 
76 // The available texture variants.
77 // Only PixelBufferTexture is currently implemented.
78 // Other variants are expected to be added in the future.
79 typedef std::variant<PixelBufferTexture, GpuSurfaceTexture> TextureVariant;
80 
81 // An object keeping track of external textures.
82 //
83 // Thread safety:
84 // It's safe to call the member methods from any thread.
86  public:
87  virtual ~TextureRegistrar() = default;
88 
89  // Registers a |texture| object and returns the ID for that texture.
90  virtual int64_t RegisterTexture(TextureVariant* texture) = 0;
91 
92  // Notifies the flutter engine that the texture object corresponding
93  // to |texure_id| needs to render a new frame.
94  //
95  // For PixelBufferTextures, this will effectively make the engine invoke
96  // the callback that was provided upon creating the texture.
97  virtual bool MarkTextureFrameAvailable(int64_t texture_id) = 0;
98 
99  // Asynchronously unregisters an existing texture object.
100  // Upon completion, the optional |callback| gets invoked.
101  virtual void UnregisterTexture(int64_t texture_id,
102  std::function<void()> callback) = 0;
103 
104  // Unregisters an existing texture object.
105  // DEPRECATED: Use UnregisterTexture(texture_id, optional_callback) instead.
106  virtual bool UnregisterTexture(int64_t texture_id) = 0;
107 };
108 
109 } // namespace flutter
110 
111 #endif // FLUTTER_SHELL_PLATFORM_COMMON_CLIENT_WRAPPER_INCLUDE_FLUTTER_TEXTURE_REGISTRAR_H_
flutter::TextureRegistrar::UnregisterTexture
virtual void UnregisterTexture(int64_t texture_id, std::function< void()> callback)=0
FlutterDesktopPixelBuffer
Definition: flutter_texture_registrar.h:56
flutter::GpuSurfaceTexture::surface_type
FlutterDesktopGpuSurfaceType surface_type() const
Definition: texture_registrar.h:69
flutter::GpuSurfaceTexture::ObtainDescriptorCallback
std::function< const FlutterDesktopGpuSurfaceDescriptor *(size_t width, size_t height)> ObtainDescriptorCallback
Definition: texture_registrar.h:52
flutter::TextureRegistrar::MarkTextureFrameAvailable
virtual bool MarkTextureFrameAvailable(int64_t texture_id)=0
FlutterDesktopGpuSurfaceType
FlutterDesktopGpuSurfaceType
Definition: flutter_texture_registrar.h:32
flutter::GpuSurfaceTexture
Definition: texture_registrar.h:47
flutter::TextureRegistrar
Definition: texture_registrar.h:85
flutter::PixelBufferTexture::CopyPixelBuffer
const FlutterDesktopPixelBuffer * CopyPixelBuffer(size_t width, size_t height) const
Definition: texture_registrar.h:37
flutter::TextureRegistrar::~TextureRegistrar
virtual ~TextureRegistrar()=default
flutter::TextureVariant
std::variant< PixelBufferTexture, GpuSurfaceTexture > TextureVariant
Definition: texture_registrar.h:79
flutter
Definition: AccessibilityBridgeMac.h:16
FlutterDesktopGpuSurfaceDescriptor
Definition: flutter_texture_registrar.h:70
flutter::PixelBufferTexture
Definition: texture_registrar.h:19
flutter_texture_registrar.h
flutter::PixelBufferTexture::CopyBufferCallback
std::function< const FlutterDesktopPixelBuffer *(size_t width, size_t height)> CopyBufferCallback
Definition: texture_registrar.h:24
flutter::GpuSurfaceTexture::ObtainDescriptor
const FlutterDesktopGpuSurfaceDescriptor * ObtainDescriptor(size_t width, size_t height) const
Definition: texture_registrar.h:62
flutter::PixelBufferTexture::PixelBufferTexture
PixelBufferTexture(CopyBufferCallback copy_buffer_callback)
Definition: texture_registrar.h:31
texture_id
int64_t texture_id
Definition: texture_registrar_unittests.cc:24
flutter::TextureRegistrar::RegisterTexture
virtual int64_t RegisterTexture(TextureVariant *texture)=0
flutter::GpuSurfaceTexture::GpuSurfaceTexture
GpuSurfaceTexture(FlutterDesktopGpuSurfaceType surface_type, ObtainDescriptorCallback obtain_descriptor_callback)
Definition: texture_registrar.h:54