|
| 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_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_TEXTURE_REGISTRAR_H_ |
| 6 | +#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_TEXTURE_REGISTRAR_H_ |
| 7 | + |
| 8 | +#include <flutter_texture_registrar.h> |
| 9 | + |
| 10 | +#include <cstdint> |
| 11 | +#include <functional> |
| 12 | +#include <memory> |
| 13 | +#include <variant> |
| 14 | + |
| 15 | +namespace flutter { |
| 16 | + |
| 17 | +// A pixel buffer texture. |
| 18 | +class PixelBufferTexture { |
| 19 | + public: |
| 20 | + // A callback used for retrieving pixel buffers. |
| 21 | + typedef std::function<const FlutterDesktopPixelBuffer*(size_t width, |
| 22 | + size_t height)> |
| 23 | + CopyBufferCallback; |
| 24 | + |
| 25 | + // Creates a pixel buffer texture that uses the provided |copy_buffer_cb| to |
| 26 | + // retrieve the buffer. |
| 27 | + // As the callback is usually invoked from the render thread, the callee must |
| 28 | + // take care of proper synchronization. It also needs to be ensured that the |
| 29 | + // returned buffer isn't released prior to unregistering this texture. |
| 30 | + PixelBufferTexture(CopyBufferCallback copy_buffer_callback) |
| 31 | + : copy_buffer_callback_(copy_buffer_callback) {} |
| 32 | + |
| 33 | + // Returns the callback-provided FlutterDesktopPixelBuffer that contains the |
| 34 | + // actual pixel data. The intended surface size is specified by |width| and |
| 35 | + // |height|. |
| 36 | + const FlutterDesktopPixelBuffer* CopyPixelBuffer(size_t width, |
| 37 | + size_t height) const { |
| 38 | + return copy_buffer_callback_(width, height); |
| 39 | + } |
| 40 | + |
| 41 | + private: |
| 42 | + const CopyBufferCallback copy_buffer_callback_; |
| 43 | +}; |
| 44 | + |
| 45 | +// The available texture variants. |
| 46 | +// Only PixelBufferTexture is currently implemented. |
| 47 | +// Other variants are expected to be added in the future. |
| 48 | +typedef std::variant<PixelBufferTexture> TextureVariant; |
| 49 | + |
| 50 | +// An object keeping track of external textures. |
| 51 | +// |
| 52 | +// Thread safety: |
| 53 | +// It's safe to call the member methods from any thread. |
| 54 | +class TextureRegistrar { |
| 55 | + public: |
| 56 | + virtual ~TextureRegistrar() = default; |
| 57 | + |
| 58 | + // Registers a |texture| object and returns the ID for that texture. |
| 59 | + virtual int64_t RegisterTexture(TextureVariant* texture) = 0; |
| 60 | + |
| 61 | + // Notifies the flutter engine that the texture object corresponding |
| 62 | + // to |texure_id| needs to render a new frame. |
| 63 | + // |
| 64 | + // For PixelBufferTextures, this will effectively make the engine invoke |
| 65 | + // the callback that was provided upon creating the texture. |
| 66 | + virtual bool MarkTextureFrameAvailable(int64_t texture_id) = 0; |
| 67 | + |
| 68 | + // Unregisters an existing Texture object. |
| 69 | + // Textures must not be unregistered while they're in use. |
| 70 | + virtual bool UnregisterTexture(int64_t texture_id) = 0; |
| 71 | +}; |
| 72 | + |
| 73 | +} // namespace flutter |
| 74 | + |
| 75 | +#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_TEXTURE_REGISTRAR_H_ |
0 commit comments