1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef STAGEFRIGHT_CODEC2_ALLOCATOR_ION_H_ 18 #define STAGEFRIGHT_CODEC2_ALLOCATOR_ION_H_ 19 20 #include <functional> 21 #include <list> 22 #include <mutex> 23 #include <tuple> 24 #include <unordered_map> 25 26 #include <C2Buffer.h> 27 28 namespace android { 29 30 class C2AllocatorIon : public C2Allocator { 31 public: 32 // Usage mapper function used by the allocator 33 // (usage, capacity) => (align, heapMask, flags) 34 // 35 // capacity is aligned to the default block-size (defaults to page size) to reduce caching 36 // overhead 37 typedef std::function<c2_status_t(C2MemoryUsage, size_t, 38 /* => */ size_t*, unsigned*, unsigned*)> UsageMapperFn; 39 40 virtual id_t getId() const override; 41 42 virtual C2String getName() const override; 43 44 virtual std::shared_ptr<const Traits> getTraits() const override; 45 46 virtual c2_status_t newLinearAllocation( 47 uint32_t capacity, C2MemoryUsage usage, 48 std::shared_ptr<C2LinearAllocation> *allocation) override; 49 50 virtual c2_status_t priorLinearAllocation( 51 const C2Handle *handle, 52 std::shared_ptr<C2LinearAllocation> *allocation) override; 53 54 C2AllocatorIon(id_t id); 55 status()56 virtual c2_status_t status() const { return mInit; } 57 58 virtual ~C2AllocatorIon() override; 59 60 static bool isValid(const C2Handle* const o); 61 62 /** 63 * Updates the usage mapper for subsequent new allocations, as well as the supported 64 * minimum and maximum usage masks and default block-size to use for the mapper. 65 * 66 * \param mapper this method is called to map Codec 2.0 buffer usage to ion flags 67 * required by the ion device 68 * \param minUsage minimum buffer usage required for supported allocations (defaults to 0) 69 * \param maxUsage maximum buffer usage supported by the ion allocator (defaults to SW_READ 70 * | SW_WRITE) 71 * \param blockSize alignment used prior to calling |mapper| for the buffer capacity. 72 * This also helps reduce the size of cache required for caching mapper results. 73 * (defaults to the page size) 74 */ 75 void setUsageMapper( 76 const UsageMapperFn &mapper, uint64_t minUsage, uint64_t maxUsage, uint64_t blockSize); 77 78 private: 79 c2_status_t mapUsage(C2MemoryUsage usage, size_t size, 80 /* => */ size_t *align, unsigned *heapMask, unsigned *flags); 81 82 c2_status_t mInit; 83 int mIonFd; 84 85 // this locks mTraits, mBlockSize, mUsageMapper, mUsageMapperLru and mUsageMapperCache 86 mutable std::mutex mUsageMapperLock; 87 std::shared_ptr<const Traits> mTraits; 88 size_t mBlockSize; 89 UsageMapperFn mUsageMapper; 90 typedef std::pair<uint64_t, size_t> MapperKey; 91 struct MapperKeyHash { 92 std::size_t operator()(const MapperKey &) const; 93 }; 94 typedef std::tuple<size_t, unsigned, unsigned, c2_status_t> MapperValue; 95 typedef std::pair<MapperKey, MapperValue> MapperKeyValue; 96 typedef std::list<MapperKeyValue>::iterator MapperKeyValuePointer; 97 std::list<MapperKeyValue> mUsageMapperLru; 98 std::unordered_map<MapperKey, MapperKeyValuePointer, MapperKeyHash> mUsageMapperCache; 99 }; 100 101 } // namespace android 102 103 #endif // STAGEFRIGHT_CODEC2_ALLOCATOR_ION_H_ 104