vmo_buffer.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2021 The Chromium 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. #ifndef MEDIA_FUCHSIA_COMMON_VMO_BUFFER_H_
  5. #define MEDIA_FUCHSIA_COMMON_VMO_BUFFER_H_
  6. #include <fuchsia/media/cpp/fidl.h>
  7. #include <fuchsia/sysmem/cpp/fidl.h>
  8. #include <memory>
  9. #include "base/containers/span.h"
  10. #include "base/memory/shared_memory_mapping.h"
  11. #include "media/base/media_export.h"
  12. #include "third_party/abseil-cpp/absl/types/optional.h"
  13. namespace media {
  14. class MEDIA_EXPORT VmoBuffer {
  15. public:
  16. // Returns sysmem buffer constraints to use to ensure that sysmem buffer
  17. // collection is compatible with this class.
  18. static fuchsia::sysmem::BufferCollectionConstraints GetRecommendedConstraints(
  19. size_t min_buffer_count,
  20. absl::optional<size_t> min_buffer_size,
  21. bool writable);
  22. // Creates a set of buffers from a sysmem collection. An empty vector is
  23. // returned in case of a failure.
  24. static std::vector<VmoBuffer> CreateBuffersFromSysmemCollection(
  25. fuchsia::sysmem::BufferCollectionInfo_2* info,
  26. bool writable);
  27. VmoBuffer();
  28. ~VmoBuffer();
  29. VmoBuffer(VmoBuffer&&);
  30. VmoBuffer& operator=(VmoBuffer&&);
  31. // Initializes the buffer from the specified |vmo|. |writable| should be true
  32. // for writable buffers. |offset| and |size| are used to specify the portion
  33. // of the |vmo| that should be used for this buffer. Returns false if it fails
  34. // to map the buffer.
  35. [[nodiscard]] bool Initialize(
  36. zx::vmo vmo,
  37. bool writable,
  38. size_t offset,
  39. size_t size,
  40. fuchsia::sysmem::CoherencyDomain coherency_domain);
  41. size_t size() const { return size_; }
  42. // Read the buffer content into |data|, starting from |offset|. For buffers
  43. // with RAM coherency the cache is invalidated prior to read to ensure the
  44. // data is read from RAM instead of the CPU cache. Returns number of bytes
  45. // that have been copied.
  46. size_t Read(size_t offset, base::span<uint8_t> data);
  47. // Writes |data| to this input buffer. If the |data| is larger than the buffer
  48. // then writes only size() bytes from the head of the |data|. Returns number
  49. // of bytes that have been copied.
  50. size_t Write(base::span<const uint8_t> data);
  51. // Returns read-only memory corresponding to the buffer. Also invalidates CPU
  52. // cache for buffers with RAM coherency.
  53. base::span<const uint8_t> GetMemory();
  54. // Returns writable memory span. The caller should call Flush() after writing
  55. // to the buffer in order to ensure that the buffer is flushed in case it uses
  56. // RAM coherency.
  57. base::span<uint8_t> GetWritableMemory();
  58. // Flushes the CPU cache if the buffers uses RAM coherency. No-op for buffers
  59. // with CPU coherency. If |invalidate| flag is set then the cache is also
  60. // invalidated.
  61. void FlushCache(size_t flush_offset, size_t flush_size, bool invalidate);
  62. // Duplicates VMO.
  63. zx::vmo Duplicate(bool writable);
  64. private:
  65. // Returns size of the mapped region.
  66. size_t mapped_size();
  67. zx::vmo vmo_;
  68. uint8_t* base_address_ = nullptr;
  69. bool writable_ = false;
  70. size_t offset_ = 0;
  71. size_t size_ = 0;
  72. fuchsia::sysmem::CoherencyDomain coherency_domain_;
  73. };
  74. } // namespace media
  75. #endif // MEDIA_FUCHSIA_COMMON_VMO_BUFFER_H_