class CodecBuffer

Defined at line 88 of file ../../src/media/lib/codec_impl/include/lib/media/codec_impl/codec_buffer.h

This object is a buffer both known to CodecImpl and exposed to the

CodecAdapter with the buffer mapped (as appropriate) and pinned (as

appropriate).

The CodecAdapter can choose to use the mapping or pin that the CodecBuffer

provides, but this only lasts as long as the CodecBuffer instance.

The CodecBuffer instance remains allocated until CoreCodecRemoveBuffer

specifying the instance or CoreCodecEnsureBuffersNotConfigured (impliciatly

specifying all configured CodecBuffer(s)) is called, and the CodecAdapter has

closed all handles and mappings derived from GetChildVmo().

The CodecAdapter should not make any handles, mappings, or pins derived from

vmo() (including duplicates and child VMOs). These won't keep the CodecBuffer

allocated. Instead, the CodecAdapter can use GetChildVmo(); the returned

handle (and handles/mappings/pins derived from that) will keep the

CodecBuffer allocated. Ensuring that the CodecBuffer pointer remains valid

until the CodecAdapter has fully closed/unmapped/unpinned all its

buffer-derived stuff reduces the chance of a CodecAdapter accessing a

no-longer-allocated CodecBuffer.

The const-ness of a CodecBuffer refers to the fields of the CodecBuffer

instance, not to the data pointed at by buffer_base(). The context accessors

re. VideoFrame, CodecAdapterFrameBase, CodecAdapterBufferContextBase are

marked mutable, as those are for use by the CodecAdapter whenever is

convenient to the CodecAdapter (as long as CodecBuffer is known still

allocated).

Protected Members

CodecImpl * parent_
Info buffer_info_
function_impl pending_remove_completion_
CodecVmoRange vmo_range_
vmo vmo_
vmo parent_vmo_
shared_ptr until_remove_started_child_vmo_
optional zero_children_wait_
callback_impl do_delete_
weak_ptr deprecated_video_frame_
weak_ptr codec_adapter_frame_
weak_ptr codec_adapter_buffer_context_
uint8_t * buffer_base_
bool is_mapped_
pmt pinned_
bool is_known_contiguous_
zx_paddr_t contiguous_paddr_base_
uint32_t output_in_flight_count_
bool is_remove_pending_
bool was_ever_added_to_core_codec_
uint32_t magic_
static const uint32_t kMagic

Public Methods

std::unique_ptr<CodecBuffer> CreateFromVmo (uint32_t buffer_index, zx::vmo vmo, uint32_t vmo_usable_start, uint32_t vmo_usable_size, bool need_write, bool is_physically_contiguous)

Defined at line 61 of file ../../src/media/lib/test/codec_buffer.cc

bool GetDupVmo (bool is_for_write, zx::vmo * out_vmo)

Each successful call to this method dups the VMO handle, with basic rights

+ read + optional write depending on is_for_write.

Defined at line 26 of file ../../src/media/lib/test/codec_buffer.cc

uint32_t buffer_index ()

In buffer-per-packet mode this is equal to the corresponding packet index,

for purposes of mapping from packet_index to buffer_index.

Defined at line 14 of file ../../src/media/lib/test/codec_buffer.cc

uint8_t * base ()

The vaddr of the start of the mapped VMO for this buffer.

This will return nullptr if there's no VMO mapping because CPU access isn't

possible. In that case the vaddr data pointer passed around regarding a

packet will be an offset into the buffer / VMO, and is only meaningful

with respect to a CodecBuffer that's also passed alongside.

Defined at line 32 of file ../../src/media/lib/test/include/lib/media/test/codec_buffer.h

size_t size_bytes ()

Defined at line 33 of file ../../src/media/lib/test/include/lib/media/test/codec_buffer.h

bool is_physically_contiguous ()

For testing.

Defined at line 39 of file ../../src/media/lib/test/include/lib/media/test/codec_buffer.h

uint64_t lifetime_ordinal ()

This is the same value as buffer_lifetime_ordinal in StreamProcessor FIDL.

Defined at line 91 of file ../../src/media/lib/codec_impl/include/lib/media/codec_impl/codec_buffer.h

uint32_t index ()

This matches the buffer_index field of fuchsia::media::Packet when the packet refers to this

buffer.

Defined at line 95 of file ../../src/media/lib/codec_impl/include/lib/media/codec_impl/codec_buffer.h

CodecPort port ()

Defined at line 97 of file ../../src/media/lib/codec_impl/include/lib/media/codec_impl/codec_buffer.h

bool is_secure ()

Defined at line 99 of file ../../src/media/lib/codec_impl/include/lib/media/codec_impl/codec_buffer.h

bool is_known_contiguous ()

Defined at line 108 of file ../../src/media/lib/codec_impl/codec_buffer.cc

zx_paddr_t physical_base ()

This will ZX_PANIC() if the buffer hasn't been pinned yet, or if !is_known_contiguous().

Defined at line 110 of file ../../src/media/lib/codec_impl/codec_buffer.cc

size_t size ()

Defined at line 119 of file ../../src/media/lib/codec_impl/codec_buffer.cc

const zx::vmo & vmo ()

This VMO is owned by CodecBuffer, but can be used temporarily (in a

non-owned fashion) to get VMO info or similar.

If the CodecAdapter wants to hold a VMO handle, a mapping, or a pin of its

own, the CodecAdapter should start with GetChildVmo() instead of vmo().

Holding a handle, mapping, or pin based on vmo() won't keep the CodecBuffer

instance allocated (but it will prevent the underlying memory from being

reused). Instead, by using GetChildVmo(), the CodecBuffer instance stays

allocated until the CodecAdapter has closed all its GetChildVmo-derived

handles to the buffer and unmapped all its GetChildVmo-derived mappings to

the buffer (and all pins). This helps avoid potential use-after-free of the

CodecBuffer and can make debugging / diagnosing simpler.

Defined at line 35 of file ../../src/media/lib/test/include/lib/media/test/codec_buffer.h

zx::vmo GetChildVmo ()

Up until the first buffer-relevant call to CoreCodecRemoveBuffer or

CoreCodecEnsureBuffersNotConfigured, the CodecBuffer will remain allocated

even if the CodecAdapter is not holding a handle obtained from

GetChildVmo() or anything derived from such a handle. If the CodecAdapter

wants/needs the CodecBuffer to remain allocated beyond said call, the

CodecAdapter must hold at least one handle obtained from GetChildVmo(), or

something derived from such a handle that keeps the VMO alive.

If a CodecAdapter does not keep any vmo(s) from GetChildVmo(), including

duplicates, mappings, or pins, that CodecAdapter must ensure that no buffer

usage of any form will occur beyond the first relevant

CoreCodecRemoveBuffer or CoreCodecEnsureBuffersNotConfigured.

If a CodecAdapter makes a separate pin of its own using GetChildVmo() (or a

duplicate and/or descendent), the CodecAdapter can continue doing DMA

to/from the buffer until its own unpin.

The CodecAdapter should not rely on the returned VMO to be derived from

vmo(), though the returned vmo does refer to the same underlying buffer.

Defined at line 123 of file ../../src/media/lib/codec_impl/codec_buffer.cc

uint64_t vmo_offset ()

The offset within the main VMO where data of this CodecBuffer starts. The vmo_offset() is not

required to be divisible by page size.

Defined at line 36 of file ../../src/media/lib/test/include/lib/media/test/codec_buffer.h

void SetVideoFrame (std::weak_ptr<VideoFrame> video_frame)

Deprecated. See SetCodecAdapterFrame and/or

SetCodecAdapterBufferContext instead.

Defined at line 133 of file ../../src/media/lib/codec_impl/codec_buffer.cc

std::weak_ptr<VideoFrame> video_frame ()

Defined at line 137 of file ../../src/media/lib/codec_impl/codec_buffer.cc

void SetCodecAdapterFrame (std::weak_ptr<CodecAdapterFrameBase> video_frame)

The CodecAdapter can set a CodecAdapterFrameBase to avoid needing to

look up a CodecBuffer* later to find the relevant "frame" (from the

CodecAdapter's point of view).

std::weak_ptr<CodecAdapterFrameBase> codec_adapter_frame ()
void SetCodecAdapterBufferContext (std::weak_ptr<CodecAdapterBufferContextBase> buffer_context)

The CodecAdapter can set a CodecAdapterBufferContextBase to avoid needing

to look up a CodecBuffer* later to find the relevant "buffer context" (from

the CodecAdapter's point of view).

std::weak_ptr<CodecAdapterBufferContextBase> codec_adapter_buffer_context ()
zx_status_t Pin ()

Unpin is automatic during ~CodecBuffer.

Defined at line 139 of file ../../src/media/lib/codec_impl/codec_buffer.cc

bool is_pinned ()

Defined at line 181 of file ../../src/media/lib/codec_impl/codec_buffer.cc

void CacheFlush (uint32_t flush_offset, uint32_t length)

Defined at line 183 of file ../../src/media/lib/codec_impl/codec_buffer.cc

void CacheFlushAndInvalidate (uint32_t flush_offset, uint32_t length)

Defined at line 187 of file ../../src/media/lib/codec_impl/codec_buffer.cc

bool is_remove_pending ()

This returns true from just before CoreCodecRemoveBuffer or CoreCodecEnsureBuffersNotConfigured

is called, until destruction.

A CodecAdapter may choose to ignore this accessor (relying only on CoreCodecRemoveBuffer and/or

CoreCodecEnsureBuffersNotConfigured).

If a CodecAdapter does not support dynamic buffers, this bool isn't particularly interesting

other than possibly for some debug asserts.

If a CodecAdapter does support dynamic buffers, this bool being true means it's appropriate to

close all handles derived from GetChildVmo() (and any derived from vmo(), though creating those

isn't recommended in the first place). Upon parent_vmo_ seeing ZX_VMO_ZERO_CHILDREN, the

CodecBuffer will be deleted, so the CodecAdapter should take care to remove all dependence on

the CodecBuffer* before closing the last handle.

For output buffers, the CodecAdapter can look at is_remove_pending() just before it would

otherwise be putting an output buffer back on its free list, for buffers that weren't already

free when CoreCodecRemoveBuffer or mid-stream CoreCodecEnsureBuffersNotConfigured was called.

For input buffers, this will only become true if the buffer is not with the CodecAdapter and

won't be referenced by any subsequent input packet sent to the CodecAdapter. So for most

CodecAdapter(s), input buffer removal can happen during the call to CoreCodecRemoveBuffer or

CoreCodecEnsureBuffersNotConfigured, without needing to call is_remove_pending() (may still be

useful for asserts).

This is public to avoid the CodecAdapter needing to keep a redundant bool in VideoFrame,

CodecAdapterFrame, or CodecAdapterBufferContext.

Defined at line 203 of file ../../src/media/lib/codec_impl/include/lib/media/codec_impl/codec_buffer.h

bool was_ever_added_to_core_codec ()

Defined at line 205 of file ../../src/media/lib/codec_impl/include/lib/media/codec_impl/codec_buffer.h

void SetWasEverAddedToCoreCodec ()

Defined at line 206 of file ../../src/media/lib/codec_impl/include/lib/media/codec_impl/codec_buffer.h

void AssertMagic ()

Defined at line 211 of file ../../src/media/lib/codec_impl/include/lib/media/codec_impl/codec_buffer.h

Protected Methods

void CodecBuffer (CodecImpl * parent, Info buffer_info, CodecVmoRange vmo_range)

Defined at line 15 of file ../../src/media/lib/codec_impl/codec_buffer.cc

void ~CodecBuffer ()

Defined at line 36 of file ../../src/media/lib/codec_impl/codec_buffer.cc

void SetDoDelete (DoDelete do_delete)

Separate from constructor because some tests don't need this.

Defined at line 65 of file ../../src/media/lib/codec_impl/codec_buffer.cc

bool Map ()

Maps a page-aligned portion of the VMO including vmo_usable_start to vmo_usable_start +

vmo_usable_size.

Defined at line 71 of file ../../src/media/lib/codec_impl/codec_buffer.cc

void FakeMap (uint8_t * fake_map_addr)

FakeMap() exists because most CodecAdapter(s) expect to have a CodecBuffer::base() and "data"

vaddr(s) within the buffer, even when buffers are secure. IIUC, mapping to secure buffer +

cached policy on the VMO + speculative execution + aarch64 potentially would

randomly/spuriously fault even if the code never actually touched the mapping. So instead of

mapping, we use a VMAR to reserve some vaddr space, but without any VMOs backing the VMAR, so

any actual accesses to any part of the VMAR will fault, and any speculative accesses won't

spuriuously/randomly fault. We only need one VMAR across all buffers of a BufferCollection, so

CodecImpl passes in the vaddr of that VMAR here. The fake_map_addr is in keeping with trying

to minimize the differences between non-secure and secure cases; it's just that we can't have

an actual mapping to the secure physical pages at the moment. In addition, by not actually

mapping buffers we can't touch anyway, we presumably save some page table resources.

The fake_map_addr is used as the a page-aligned base address for a fake mapping. Client code

must not touch memory at buffer_base() when a fake mapping is in effect, but if client code

does anyway, that thread will cleanly fault (not get stuck reading, not seem to let a write

happen, not be reading/writing any arbitrary other data in the process's address space). The

fake_map_addr vaddr region is guaranteed to have enough vaddr pages to accommodate

vmo_usable_start % PAGE_SIZE + vmo_usable_size (so that an access within the bounds of the

buffer will reliably fault cleanly).

Defined at line 97 of file ../../src/media/lib/codec_impl/codec_buffer.cc

void CacheFlushInternal (uint32_t flush_offset, uint32_t length, bool also_invalidate)

Defined at line 191 of file ../../src/media/lib/codec_impl/codec_buffer.cc

void BeginWaitForZeroChildren (async_dispatcher_t * dispatcher)

Defined at line 67 of file ../../src/media/lib/codec_impl/codec_buffer.cc

void OnZeroChildren (async_dispatcher_t * dispatcher, async::WaitBase * wait, zx_status_t status, const zx_packet_signal_t * signal)

Defined at line 216 of file ../../src/media/lib/codec_impl/codec_buffer.cc

fit::function<void (ScopedLock &)> TakePendingRemoveCompletion ()

Defined at line 241 of file ../../src/media/lib/codec_impl/codec_buffer.cc

KeepAlive GetKeepAlive ()

Defined at line 236 of file ../../src/media/lib/codec_impl/codec_buffer.cc

const zx::vmo & original_vmo ()

Defined at line 277 of file ../../src/media/lib/codec_impl/include/lib/media/codec_impl/codec_buffer.h

void CodecBuffer (const CodecBuffer & to_copy)

Defined at line 387 of file ../../src/media/lib/codec_impl/include/lib/media/codec_impl/codec_buffer.h

CodecBuffer & operator= (const CodecBuffer & to_copy)

Defined at line 388 of file ../../src/media/lib/codec_impl/include/lib/media/codec_impl/codec_buffer.h

void CodecBuffer (CodecBuffer && to_move)

Defined at line 389 of file ../../src/media/lib/codec_impl/include/lib/media/codec_impl/codec_buffer.h

CodecBuffer & operator= (CodecBuffer && to_move)

Defined at line 390 of file ../../src/media/lib/codec_impl/include/lib/media/codec_impl/codec_buffer.h

Records

Friends

class CodecPacket
class CodecBufferForTest
class default_delete
class unique_ptr
class CodecImpl