Commit 7feeab08 authored by Sergey Ulanov's avatar Sergey Ulanov Committed by Commit Bot

[Fuchsia] Add SysmemBufferWriter::ReserveAndMapBuffer()

The new function will allow SysmemBufferWriter users to get direct
mapping to the buffer if they need it.

Bug: b/158033083
Change-Id: Id6aeabf415a65f859d6d0add7015a8a7868946ca
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2206339
Commit-Queue: Sergey Ulanov <sergeyu@chromium.org>
Reviewed-by: default avatarDavid Dorwin <ddorwin@chromium.org>
Reviewed-by: default avatarKevin Marshall <kmarshall@chromium.org>
Auto-Submit: Sergey Ulanov <sergeyu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#774397}
parent a414d98b
......@@ -70,16 +70,30 @@ class SysmemBufferWriter::Buffer {
size_t bytes_to_fill = std::min(size_, data.size());
memcpy(base_address_ + offset_, data.data(), bytes_to_fill);
// Flush CPU cache if StreamProcessor reads from RAM.
if (coherency_domain_ == fuchsia::sysmem::CoherencyDomain::RAM) {
zx_status_t status = zx_cache_flush(base_address_ + offset_,
bytes_to_fill, ZX_CACHE_FLUSH_DATA);
ZX_DCHECK(status == ZX_OK, status) << "zx_cache_flush";
}
FlushBuffer(0, bytes_to_fill);
return bytes_to_fill;
}
base::span<uint8_t> ReserveAndMapBuffer() {
DCHECK(!is_used_);
is_used_ = true;
return base::make_span(base_address_ + offset_, size_);
}
void FlushBuffer(size_t flush_offset, size_t flush_size) {
DCHECK(is_used_);
DCHECK_LE(flush_size, size_ - flush_offset);
if (coherency_domain_ != fuchsia::sysmem::CoherencyDomain::RAM)
return;
uint8_t* address = base_address_ + offset_ + flush_offset;
zx_status_t status =
zx_cache_flush(address, flush_size, ZX_CACHE_FLUSH_DATA);
ZX_DCHECK(status == ZX_OK, status) << "zx_cache_flush";
}
void Release() { is_used_ = false; }
private:
......@@ -187,4 +201,16 @@ SysmemBufferWriter::GetRecommendedConstraints(
return buffer_constraints;
}
base::span<uint8_t> SysmemBufferWriter::ReserveAndMapBuffer(size_t index) {
DCHECK_LT(index, buffers_.size());
return buffers_[index].ReserveAndMapBuffer();
}
void SysmemBufferWriter::FlushBuffer(size_t index,
size_t flush_offset,
size_t flush_size) {
DCHECK_LT(index, buffers_.size());
return buffers_[index].FlushBuffer(flush_offset, flush_size);
}
} // namespace media
......@@ -11,6 +11,7 @@
#include <memory>
#include "base/containers/span.h"
#include "base/memory/shared_memory_mapping.h"
#include "base/optional.h"
namespace media {
......@@ -30,11 +31,24 @@ class SysmemBufferWriter {
explicit SysmemBufferWriter(std::vector<Buffer> buffers);
~SysmemBufferWriter();
// Write the content of |data| into buffer at |index|. Return num of bytes
// written into the buffer. Write a used buffer will fail. It will mark the
// buffer as "used".
SysmemBufferWriter(const SysmemBufferWriter&) = delete;
SysmemBufferWriter& operator=(const SysmemBufferWriter&) = delete;
// Write the content of |data| into the buffer at |index|. Return num of bytes
// written into the buffer. Can be called only for an unused buffer. Marks
// the buffer as used.
size_t Write(size_t index, base::span<const uint8_t> data);
// Returns a span for the memory-mapping of the buffer with the specified
// |index|. Can be called only for an unused buffer. Marks the buffer as used.
// Callers must call FlushCache() after they are finished updating the buffer.
base::span<uint8_t> ReserveAndMapBuffer(size_t index);
// Flushes CPU cache for specified range in the buffer with the specified
// |index| in case the buffer collection uses RAM coherency. No-op for
// collections with RAM coherency.
void FlushBuffer(size_t index, size_t flush_offset, size_t flush_size);
// Acquire unused buffer for write. If |min_size| is provided, the returned
// buffer will have available size larger than |min_size|. This will NOT
// mark the buffer as "used".
......@@ -50,8 +64,6 @@ class SysmemBufferWriter {
private:
std::vector<Buffer> buffers_;
DISALLOW_COPY_AND_ASSIGN(SysmemBufferWriter);
};
} // namespace media
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment