• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "gst_shmem_allocator.h"
17 #include "media_log.h"
18 #include "media_errors.h"
19 #include "securec.h"
20 
21 #define gst_shmem_allocator_parent_class parent_class
22 G_DEFINE_TYPE(GstShMemAllocator, gst_shmem_allocator, GST_TYPE_ALLOCATOR);
23 
24 GST_DEBUG_CATEGORY_STATIC(gst_shmem_allocator_debug_category);
25 #define GST_CAT_DEFAULT gst_shmem_allocator_debug_category
26 
27 static constexpr uint32_t ALIGN_BYTES = 4;
28 
gst_shmem_allocator_new()29 GstShMemAllocator *gst_shmem_allocator_new()
30 {
31     GstShMemAllocator *alloc = GST_SHMEM_ALLOCATOR_CAST(g_object_new(
32         GST_TYPE_SHMEM_ALLOCATOR, "name", "ShMemAllocator", nullptr));
33     (void)gst_object_ref_sink(alloc);
34 
35     return alloc;
36 }
37 
gst_shmem_allocator_set_pool(GstShMemAllocator * allocator,std::shared_ptr<OHOS::Media::AVSharedMemoryPool> pool)38 void gst_shmem_allocator_set_pool(GstShMemAllocator *allocator,
39                                   std::shared_ptr<OHOS::Media::AVSharedMemoryPool> pool)
40 {
41     g_return_if_fail(allocator != nullptr && pool != nullptr);
42     allocator->avShmemPool = pool;
43 }
44 
gst_shmem_allocator_alloc(GstAllocator * allocator,gsize size,GstAllocationParams * params)45 GstMemory *gst_shmem_allocator_alloc(GstAllocator *allocator, gsize size, GstAllocationParams *params)
46 {
47     g_return_val_if_fail(allocator != nullptr && params != nullptr, nullptr);
48     GstShMemAllocator *sAlloctor = GST_SHMEM_ALLOCATOR_CAST(allocator);
49     g_return_val_if_fail(sAlloctor != nullptr && sAlloctor->avShmemPool != nullptr, nullptr);
50     g_return_val_if_fail((params->prefix & (ALIGN_BYTES - 1)) == 0, nullptr);
51     g_return_val_if_fail((UINT64_MAX - params->prefix) >= size, nullptr);
52 
53     uint64_t allocSize = size + params->prefix;
54     g_return_val_if_fail(allocSize < INT32_MAX, nullptr);
55 
56     std::shared_ptr<OHOS::Media::AVSharedMemory> shmem = sAlloctor->avShmemPool->AcquireMemory(allocSize);
57     if (shmem == nullptr) {
58         GST_LOG("no memory");
59         return nullptr;
60     }
61 
62     GstShMemMemory *memory = reinterpret_cast<GstShMemMemory *>(g_slice_alloc0(sizeof(GstShMemMemory)));
63     g_return_val_if_fail(memory != nullptr, nullptr);
64 
65     gst_memory_init(GST_MEMORY_CAST(memory), (GstMemoryFlags)0, allocator,
66         nullptr, allocSize, 0, 0, size);
67 
68     memory->mem = shmem;
69     GST_LOG("alloc memory from %s for size: %" PRIu64 ", gstmemory: 0x%06" PRIXPTR "",
70         sAlloctor->avShmemPool->GetName().c_str(), allocSize, FAKE_POINTER(memory));
71 
72     return GST_MEMORY_CAST(memory);
73 }
74 
gst_shmem_allocator_free(GstAllocator * allocator,GstMemory * memory)75 void gst_shmem_allocator_free(GstAllocator *allocator, GstMemory *memory)
76 {
77     g_return_if_fail(memory != nullptr && allocator != nullptr);
78     g_return_if_fail(gst_is_shmem_memory(memory));
79 
80     GstShMemAllocator *sAlloctor = GST_SHMEM_ALLOCATOR_CAST(allocator);
81     g_return_if_fail(sAlloctor != nullptr && sAlloctor->avShmemPool != nullptr);
82 
83     GstShMemMemory *avSharedMem = reinterpret_cast<GstShMemMemory *>(memory);
84     GST_LOG("free memory to %s for size: %" G_GSIZE_FORMAT ", gstmemory: 0x%06" PRIXPTR ", recount: %ld",
85         sAlloctor->avShmemPool->GetName().c_str(), memory->size, FAKE_POINTER(avSharedMem),
86         avSharedMem->mem.use_count());
87 
88     // assign the nullptr will decrease the refcount, if the refcount is zero,
89     // the memory will be released back to pool.
90     avSharedMem->mem = nullptr;
91     g_slice_free(GstShMemMemory, avSharedMem);
92 }
93 
gst_shmem_allocator_mem_map(GstMemory * mem,gsize maxsize,GstMapFlags flags)94 static gpointer gst_shmem_allocator_mem_map(GstMemory *mem, gsize maxsize, GstMapFlags flags)
95 {
96     g_return_val_if_fail(mem != nullptr, nullptr);
97     g_return_val_if_fail(gst_is_shmem_memory(mem), nullptr);
98 
99     GstShMemMemory *avSharedMem = reinterpret_cast<GstShMemMemory *>(mem);
100     g_return_val_if_fail(avSharedMem->mem != nullptr, nullptr);
101 
102     return avSharedMem->mem->GetBase();
103 }
104 
gst_shmem_allocator_mem_unmap(GstMemory * mem)105 static void gst_shmem_allocator_mem_unmap(GstMemory *mem)
106 {
107     (void)mem;
108 }
109 
gst_shmem_allocator_mem_copy(GstShMemMemory * mem,gssize offset,gssize size)110 static GstMemory *gst_shmem_allocator_mem_copy(GstShMemMemory *mem, gssize offset, gssize size)
111 {
112     g_return_val_if_fail(mem != nullptr && mem->mem != nullptr, nullptr);
113     g_return_val_if_fail(mem->mem->GetBase() != nullptr, nullptr);
114 
115     gssize realOffset = 0;
116     if (((gint64)mem->parent.offset + offset) > INT32_MAX) {
117         GST_ERROR("invalid offset");
118         return nullptr;
119     } else {
120         realOffset = static_cast<gssize>(mem->parent.offset) + offset;
121     }
122     g_return_val_if_fail(realOffset >= 0, nullptr);
123 
124     if (size == -1) {
125         if (((gint64)mem->parent.size - offset) > INT32_MAX) {
126             GST_ERROR("invalid size");
127             return nullptr;
128         } else {
129             size = static_cast<gssize>(mem->parent.size) - offset;
130         }
131     }
132     g_return_val_if_fail(size > 0, nullptr);
133 
134     if ((UINT32_MAX - size) <= realOffset) {
135         GST_ERROR("invalid limit");
136         return nullptr;
137     }
138     gsize realLimit = static_cast<gsize>(size) + static_cast<gsize>(realOffset);
139     g_return_val_if_fail(realLimit <= static_cast<gsize>(mem->mem->GetSize()), nullptr);
140 
141     GstMemory *copy = gst_allocator_alloc(nullptr, static_cast<gsize>(size), nullptr);
142     g_return_val_if_fail(copy != nullptr, nullptr);
143 
144     GstMapInfo info = GST_MAP_INFO_INIT;
145     if (!gst_memory_map(copy, &info, GST_MAP_READ)) {
146         gst_memory_unref(copy);
147         GST_ERROR("map failed");
148         return nullptr;
149     }
150 
151     uint8_t *src = mem->mem->GetBase() + realOffset;
152     errno_t rc = memcpy_s(info.data, info.size, src, static_cast<size_t>(size));
153     if (rc != EOK) {
154         GST_ERROR("memcpy failed");
155         gst_memory_unmap(copy, &info);
156         gst_memory_unref(copy);
157         return nullptr;
158     }
159 
160     gst_memory_unmap(copy, &info);
161     GST_LOG("copy memory: 0x%06" PRIXPTR " for size: %" G_GSSIZE_FORMAT ", offset: %" G_GSSIZE_FORMAT
162         ", gstmemory: 0x%06" PRIXPTR, FAKE_POINTER(mem), size, offset, FAKE_POINTER(copy));
163 
164     return copy;
165 }
166 
gst_shmem_allocator_mem_share(GstMemory * mem,gssize offset,gssize size)167 static GstShMemMemory *gst_shmem_allocator_mem_share(GstMemory *mem, gssize offset, gssize size)
168 {
169     g_return_val_if_fail(mem != nullptr, nullptr);
170     g_return_val_if_fail(offset >= 0 && static_cast<gsize>(offset) < mem->size, nullptr);
171     GstShMemMemory *shmem = reinterpret_cast<GstShMemMemory *>(mem);
172     g_return_val_if_fail(shmem->mem != nullptr, nullptr);
173 
174     GstMemory *parent = mem->parent;
175     if (parent == nullptr) {
176         parent = GST_MEMORY_CAST(mem);
177     }
178     if (size == -1) {
179         size = static_cast<gssize>(mem->size) - offset;
180     }
181 
182     GstShMemMemory *sub = reinterpret_cast<GstShMemMemory *>(g_slice_alloc0(sizeof(GstShMemMemory)));
183     g_return_val_if_fail(sub != nullptr, nullptr);
184 
185     GstMemoryFlags flags = static_cast<GstMemoryFlags>(GST_MEMORY_FLAGS(parent) | GST_MEMORY_FLAG_READONLY);
186     gst_memory_init(GST_MEMORY_CAST(sub), flags, mem->allocator, parent, mem->maxsize,
187         mem->align, mem->offset + offset, size);
188     sub->mem = shmem->mem;
189 
190     GST_LOG("share memory 0x%06" PRIXPTR " for size: %" G_GSSIZE_FORMAT ", offset: %" G_GSSIZE_FORMAT
191             ", addr: 0x%06" PRIXPTR ", refcount: %ld",
192             FAKE_POINTER(mem), size, offset, FAKE_POINTER(sub), sub->mem.use_count());
193 
194     return sub;
195 }
196 
gst_shmem_allocator_is_span(GstShMemMemory * mem1,GstShMemMemory * mem2,gsize * offset)197 static gboolean gst_shmem_allocator_is_span(GstShMemMemory *mem1, GstShMemMemory *mem2, gsize *offset)
198 {
199     g_return_val_if_fail(mem1 != nullptr && mem1->mem != nullptr, FALSE);
200     g_return_val_if_fail(mem2 != nullptr && mem2->mem != nullptr, FALSE);
201 
202     if (mem1->mem != mem2->mem) {
203         return FALSE;
204     }
205 
206     if (offset != nullptr) {
207         GstShMemMemory *parent = reinterpret_cast<GstShMemMemory *>(mem1->parent.parent);
208         if (parent != nullptr) {
209             *offset = mem1->parent.offset - parent->parent.offset;
210         }
211     }
212 
213     return mem1->mem->GetBase() + mem1->parent.offset + mem1->parent.size ==
214         mem2->mem->GetBase() + mem2->parent.offset;
215 }
216 
gst_shmem_allocator_init(GstShMemAllocator * allocator)217 static void gst_shmem_allocator_init(GstShMemAllocator *allocator)
218 {
219     GstAllocator *bAllocator = GST_ALLOCATOR_CAST(allocator);
220     g_return_if_fail(bAllocator != nullptr);
221 
222     GST_DEBUG_OBJECT(allocator, "init allocator 0x%06" PRIXPTR "", FAKE_POINTER(allocator));
223 
224     bAllocator->mem_type = GST_SHMEM_MEMORY_TYPE;
225     bAllocator->mem_map = (GstMemoryMapFunction)gst_shmem_allocator_mem_map;
226     bAllocator->mem_unmap = (GstMemoryUnmapFunction)gst_shmem_allocator_mem_unmap;
227     bAllocator->mem_copy = (GstMemoryCopyFunction)gst_shmem_allocator_mem_copy;
228     bAllocator->mem_share = (GstMemoryShareFunction)gst_shmem_allocator_mem_share;
229     bAllocator->mem_is_span = (GstMemoryIsSpanFunction)gst_shmem_allocator_is_span;
230 }
231 
gst_shmem_allocator_finalize(GObject * obj)232 static void gst_shmem_allocator_finalize(GObject *obj)
233 {
234     GstShMemAllocator *allocator = GST_SHMEM_ALLOCATOR_CAST(obj);
235     g_return_if_fail(allocator != nullptr);
236 
237     GST_DEBUG_OBJECT(allocator, "finalize allocator 0x%06" PRIXPTR "", FAKE_POINTER(allocator));
238     allocator->avShmemPool = nullptr;
239     G_OBJECT_CLASS(parent_class)->finalize(obj);
240 }
241 
gst_shmem_allocator_class_init(GstShMemAllocatorClass * klass)242 static void gst_shmem_allocator_class_init(GstShMemAllocatorClass *klass)
243 {
244     GObjectClass *gobjectClass = G_OBJECT_CLASS(klass);
245     g_return_if_fail(gobjectClass != nullptr);
246 
247     gobjectClass->finalize = gst_shmem_allocator_finalize;
248 
249     GstAllocatorClass *allocatorClass = GST_ALLOCATOR_CLASS(klass);
250     g_return_if_fail(allocatorClass != nullptr);
251 
252     allocatorClass->alloc = gst_shmem_allocator_alloc;
253     allocatorClass->free = gst_shmem_allocator_free;
254 
255     GST_DEBUG_CATEGORY_INIT(gst_shmem_allocator_debug_category, "shmemalloc", 0, "shmemalloc class");
256 }
257