1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- // Copyright 2021 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #ifndef MEDIA_FUCHSIA_COMMON_VMO_BUFFER_H_
- #define MEDIA_FUCHSIA_COMMON_VMO_BUFFER_H_
- #include <fuchsia/media/cpp/fidl.h>
- #include <fuchsia/sysmem/cpp/fidl.h>
- #include <memory>
- #include "base/containers/span.h"
- #include "base/memory/shared_memory_mapping.h"
- #include "media/base/media_export.h"
- #include "third_party/abseil-cpp/absl/types/optional.h"
- namespace media {
- class MEDIA_EXPORT VmoBuffer {
- public:
- // Returns sysmem buffer constraints to use to ensure that sysmem buffer
- // collection is compatible with this class.
- static fuchsia::sysmem::BufferCollectionConstraints GetRecommendedConstraints(
- size_t min_buffer_count,
- absl::optional<size_t> min_buffer_size,
- bool writable);
- // Creates a set of buffers from a sysmem collection. An empty vector is
- // returned in case of a failure.
- static std::vector<VmoBuffer> CreateBuffersFromSysmemCollection(
- fuchsia::sysmem::BufferCollectionInfo_2* info,
- bool writable);
- VmoBuffer();
- ~VmoBuffer();
- VmoBuffer(VmoBuffer&&);
- VmoBuffer& operator=(VmoBuffer&&);
- // Initializes the buffer from the specified |vmo|. |writable| should be true
- // for writable buffers. |offset| and |size| are used to specify the portion
- // of the |vmo| that should be used for this buffer. Returns false if it fails
- // to map the buffer.
- [[nodiscard]] bool Initialize(
- zx::vmo vmo,
- bool writable,
- size_t offset,
- size_t size,
- fuchsia::sysmem::CoherencyDomain coherency_domain);
- size_t size() const { return size_; }
- // Read the buffer content into |data|, starting from |offset|. For buffers
- // with RAM coherency the cache is invalidated prior to read to ensure the
- // data is read from RAM instead of the CPU cache. Returns number of bytes
- // that have been copied.
- size_t Read(size_t offset, base::span<uint8_t> data);
- // Writes |data| to this input buffer. If the |data| is larger than the buffer
- // then writes only size() bytes from the head of the |data|. Returns number
- // of bytes that have been copied.
- size_t Write(base::span<const uint8_t> data);
- // Returns read-only memory corresponding to the buffer. Also invalidates CPU
- // cache for buffers with RAM coherency.
- base::span<const uint8_t> GetMemory();
- // Returns writable memory span. The caller should call Flush() after writing
- // to the buffer in order to ensure that the buffer is flushed in case it uses
- // RAM coherency.
- base::span<uint8_t> GetWritableMemory();
- // Flushes the CPU cache if the buffers uses RAM coherency. No-op for buffers
- // with CPU coherency. If |invalidate| flag is set then the cache is also
- // invalidated.
- void FlushCache(size_t flush_offset, size_t flush_size, bool invalidate);
- // Duplicates VMO.
- zx::vmo Duplicate(bool writable);
- private:
- // Returns size of the mapped region.
- size_t mapped_size();
- zx::vmo vmo_;
- uint8_t* base_address_ = nullptr;
- bool writable_ = false;
- size_t offset_ = 0;
- size_t size_ = 0;
- fuchsia::sysmem::CoherencyDomain coherency_domain_;
- };
- } // namespace media
- #endif // MEDIA_FUCHSIA_COMMON_VMO_BUFFER_H_
|