1 /*
2  * Copyright (C) 2020 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_BUF_H_
18 #define STAGEFRIGHT_CODEC2_ALLOCATOR_BUF_H_
19 
20 #include <BufferAllocator/BufferAllocator.h>
21 #include <C2Buffer.h>
22 #include <sys/stat.h>  // stat
23 
24 #include <functional>
25 #include <list>
26 #include <mutex>
27 #include <tuple>
28 #include <unordered_map>
29 
30 namespace android {
31 
32 class C2DmaBufAllocator : public C2Allocator {
33    public:
34     virtual c2_status_t newLinearAllocation(
35             uint32_t capacity, C2MemoryUsage usage,
36             std::shared_ptr<C2LinearAllocation>* allocation) override;
37 
38     virtual c2_status_t priorLinearAllocation(
39             const C2Handle* handle, std::shared_ptr<C2LinearAllocation>* allocation) override;
40 
41     C2DmaBufAllocator(id_t id);
42 
status()43     virtual c2_status_t status() const { return mInit; }
44 
checkHandle(const C2Handle * const o)45     virtual bool checkHandle(const C2Handle* const o) const override { return CheckHandle(o); }
46 
47     static bool CheckHandle(const C2Handle* const o);
48 
49     virtual id_t getId() const override;
50 
51     virtual C2String getName() const override;
52 
53     virtual std::shared_ptr<const Traits> getTraits() const override;
54 
55     // Usage mapper function used by the allocator
56     //   (usage, capacity) => (heapName, flags)
57     //
58     // capacity is aligned to the default block-size (defaults to page size) to
59     // reduce caching overhead
60     typedef std::function<c2_status_t(C2MemoryUsage, size_t,
61                                       /* => */ C2String*, unsigned*)>
62             UsageMapperFn;
63 
64     /**
65      * Updates the usage mapper for subsequent new allocations, as well as the
66      * supported minimum and maximum usage masks and default block-size to use
67      * for the mapper.
68      *
69      * \param mapper          This method is called to map Codec 2.0 buffer usage
70      *                        to dmabuf heap name and flags required by the dma
71      *                        buf heap device
72      *
73      * \param minUsage        Minimum buffer usage required for supported
74      *                        allocations (defaults to 0)
75      *
76      * \param maxUsage        Maximum buffer usage supported by the ion allocator
77      *                        (defaults to SW_READ | SW_WRITE)
78      *
79      * \param blockSize       Alignment used prior to calling |mapper| for the
80      *                        buffer capacity. This also helps reduce the size of
81      *                        cache required for caching mapper results.
82      *                        (defaults to the page size)
83      */
84     void setUsageMapper(const UsageMapperFn& mapper, uint64_t minUsage, uint64_t maxUsage,
85                         uint64_t blockSize);
86 
system_uncached_supported(void)87     static bool system_uncached_supported(void) {
88         static int cached_result = -1;
89 
90         if (cached_result == -1) {
91             struct stat buffer;
92             cached_result = (stat("/dev/dma_heap/system-uncached", &buffer) == 0);
93         }
94         return (cached_result == 1);
95     };
96 
97    private:
98     c2_status_t mInit;
99     BufferAllocator mBufferAllocator;
100 
101     c2_status_t mapUsage(C2MemoryUsage usage, size_t size,
102                          /* => */ C2String* heap_name, unsigned* flags);
103 
104     // this locks mTraits, mBlockSize, mUsageMapper, mUsageMapperLru and
105     // mUsageMapperCache
106     mutable std::mutex mUsageMapperLock;
107     std::shared_ptr<const Traits> mTraits;
108     size_t mBlockSize;
109     UsageMapperFn mUsageMapper;
110     typedef std::pair<uint64_t, size_t> MapperKey;
111     struct MapperKeyHash {
112         std::size_t operator()(const MapperKey&) const;
113     };
114     typedef std::tuple<C2String, unsigned, c2_status_t> MapperValue;
115     typedef std::pair<MapperKey, MapperValue> MapperKeyValue;
116     typedef std::list<MapperKeyValue>::iterator MapperKeyValuePointer;
117     std::list<MapperKeyValue> mUsageMapperLru;
118     std::unordered_map<MapperKey, MapperKeyValuePointer, MapperKeyHash> mUsageMapperCache;
119 };
120 }  // namespace android
121 
122 #endif  // STAGEFRIGHT_CODEC2_ALLOCATOR_BUF_H_
123