Merge "codec2: handle multiple maps in linear block" into pi-dev
diff --git a/media/libstagefright/codec2/tests/vndk/C2BufferTest.cpp b/media/libstagefright/codec2/tests/vndk/C2BufferTest.cpp
index 038be48..0da3398 100644
--- a/media/libstagefright/codec2/tests/vndk/C2BufferTest.cpp
+++ b/media/libstagefright/codec2/tests/vndk/C2BufferTest.cpp
@@ -745,4 +745,23 @@
EXPECT_FALSE(buffer->hasInfo(info2->type()));
}
+TEST_F(C2BufferTest, MultipleLinearMapTest) {
+ std::shared_ptr<C2BlockPool> pool(makeLinearBlockPool());
+ constexpr size_t kCapacity = 524288u;
+ for (int i = 0; i < 100; ++i) {
+ std::vector<C2WriteView> wViews;
+ std::vector<C2ReadView> rViews;
+ for (int j = 0; j < 16; ++j) {
+ std::shared_ptr<C2LinearBlock> block;
+ ASSERT_EQ(C2_OK, pool->fetchLinearBlock(
+ kCapacity,
+ { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE },
+ &block));
+ wViews.push_back(block->map().get());
+ C2ConstLinearBlock cBlock = block->share(0, kCapacity / 2, C2Fence());
+ rViews.push_back(cBlock.map().get());
+ }
+ }
+}
+
} // namespace android
diff --git a/media/libstagefright/codec2/vndk/C2AllocatorIon.cpp b/media/libstagefright/codec2/vndk/C2AllocatorIon.cpp
index cf7658a..fda8fe4 100644
--- a/media/libstagefright/codec2/vndk/C2AllocatorIon.cpp
+++ b/media/libstagefright/codec2/vndk/C2AllocatorIon.cpp
@@ -18,6 +18,8 @@
#define LOG_TAG "C2AllocatorIon"
#include <utils/Log.h>
+#include <list>
+
#include <ion/ion.h>
#include <sys/mman.h>
#include <unistd.h>
@@ -161,8 +163,7 @@
mBuffer(buffer),
mId(id),
mInit(c2_map_errno<ENOMEM, EACCES, EINVAL>(err)),
- mMapFd(-1),
- mMapSize(0) {
+ mMapFd(-1) {
if (mInit != C2_OK) {
// close ionFd now on error
if (mIonFd >= 0) {
@@ -221,7 +222,8 @@
c2_status_t map(size_t offset, size_t size, C2MemoryUsage usage, C2Fence *fence, void **addr) {
(void)fence; // TODO: wait for fence
*addr = nullptr;
- if (mMapSize > 0) {
+ if (!mMappings.empty()) {
+ ALOGV("multiple map");
// TODO: technically we should return DUPLICATE here, but our block views don't
// actually unmap, so we end up remapping an ion buffer multiple times.
//
@@ -244,54 +246,63 @@
size_t alignmentBytes = offset % PAGE_SIZE;
size_t mapOffset = offset - alignmentBytes;
size_t mapSize = size + alignmentBytes;
+ Mapping map = { nullptr, alignmentBytes, mapSize };
c2_status_t err = C2_OK;
if (mMapFd == -1) {
int ret = ion_map(mIonFd, mBuffer, mapSize, prot,
- flags, mapOffset, (unsigned char**)&mMapAddr, &mMapFd);
+ flags, mapOffset, (unsigned char**)&map.addr, &mMapFd);
if (ret) {
mMapFd = -1;
- *addr = nullptr;
+ map.addr = *addr = nullptr;
err = c2_map_errno<EINVAL>(-ret);
} else {
- *addr = (uint8_t *)mMapAddr + alignmentBytes;
- mMapAlignmentBytes = alignmentBytes;
- mMapSize = mapSize;
+ *addr = (uint8_t *)map.addr + alignmentBytes;
}
} else {
- mMapAddr = mmap(nullptr, mapSize, prot, flags, mMapFd, mapOffset);
- if (mMapAddr == MAP_FAILED) {
- mMapAddr = *addr = nullptr;
+ map.addr = mmap(nullptr, mapSize, prot, flags, mMapFd, mapOffset);
+ if (map.addr == MAP_FAILED) {
+ map.addr = *addr = nullptr;
err = c2_map_errno<EINVAL>(errno);
} else {
- *addr = (uint8_t *)mMapAddr + alignmentBytes;
- mMapAlignmentBytes = alignmentBytes;
- mMapSize = mapSize;
+ *addr = (uint8_t *)map.addr + alignmentBytes;
}
}
+ if (map.addr) {
+ mMappings.push_back(map);
+ }
return err;
}
c2_status_t unmap(void *addr, size_t size, C2Fence *fence) {
- if (mMapFd < 0 || mMapSize == 0) {
+ if (mMapFd < 0 || mMappings.empty()) {
return C2_NOT_FOUND;
}
- if (addr != (uint8_t *)mMapAddr + mMapAlignmentBytes ||
- size + mMapAlignmentBytes != mMapSize) {
- return C2_BAD_VALUE;
+ for (auto it = mMappings.begin(); it != mMappings.end(); ++it) {
+ if (addr != (uint8_t *)it->addr + it->alignmentBytes ||
+ size + it->alignmentBytes != it->size) {
+ continue;
+ }
+ int err = munmap(it->addr, it->size);
+ if (err != 0) {
+ return c2_map_errno<EINVAL>(errno);
+ }
+ if (fence) {
+ *fence = C2Fence(); // not using fences
+ }
+ (void)mMappings.erase(it);
+ return C2_OK;
}
- int err = munmap(mMapAddr, mMapSize);
- if (err != 0) {
- return c2_map_errno<EINVAL>(errno);
- }
- if (fence) {
- *fence = C2Fence(); // not using fences
- }
- mMapSize = 0;
- return C2_OK;
+ return C2_BAD_VALUE;
}
~Impl() {
+ if (!mMappings.empty()) {
+ ALOGD("Dangling mappings!");
+ for (const Mapping &map : mMappings) {
+ (void)munmap(map.addr, map.size);
+ }
+ }
if (mMapFd >= 0) {
close(mMapFd);
mMapFd = -1;
@@ -328,9 +339,12 @@
C2Allocator::id_t mId;
c2_status_t mInit;
int mMapFd; // only one for now
- void *mMapAddr;
- size_t mMapAlignmentBytes;
- size_t mMapSize;
+ struct Mapping {
+ void *addr;
+ size_t alignmentBytes;
+ size_t size;
+ };
+ std::list<Mapping> mMappings;
};
c2_status_t C2AllocationIon::map(