• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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_wrap_allocator.h"
17 #include "media_log.h"
18 
19 #define gst_shmem_wrap_allocator_parent_class parent_class
20 G_DEFINE_TYPE(GstShMemWrapAllocator, gst_shmem_wrap_allocator, GST_TYPE_ALLOCATOR);
21 
gst_shmem_wrap_allocator_new(void)22 GstShMemWrapAllocator *gst_shmem_wrap_allocator_new(void)
23 {
24     GstShMemWrapAllocator *alloc = GST_SHMEM_WRAP_ALLOCATOR_CAST(g_object_new(
25         GST_TYPE_SHMEM_WRAP_ALLOCATOR, "name", "ShMemWrapAllocator", nullptr));
26     (void)gst_object_ref_sink(alloc);
27 
28     return alloc;
29 }
30 
gst_shmem_wrap(GstAllocator * allocator,std::shared_ptr<OHOS::Media::AVSharedMemory> shmem)31 GstMemory *gst_shmem_wrap(GstAllocator *allocator, std::shared_ptr<OHOS::Media::AVSharedMemory> shmem)
32 {
33     g_return_val_if_fail(allocator != nullptr, nullptr);
34     g_return_val_if_fail(shmem != nullptr, nullptr);
35 
36     GstShMemMemory *memory = reinterpret_cast<GstShMemMemory *>(g_slice_alloc0(sizeof(GstShMemMemory)));
37     g_return_val_if_fail(memory != nullptr, nullptr);
38 
39     gst_memory_init(GST_MEMORY_CAST(memory), (GstMemoryFlags)0, allocator,
40         nullptr, shmem->GetSize(), 0, 0, shmem->GetSize());
41 
42     memory->mem = shmem;
43     GST_DEBUG("alloc memory for size: %" PRIu64 ", addr: 0x%06" PRIXPTR "",
44         static_cast<uint64_t>(shmem->GetSize()), FAKE_POINTER(shmem->GetBase()));
45 
46     return GST_MEMORY_CAST(memory);
47 }
48 
gst_shmem_wrap_allocator_alloc(GstAllocator * allocator,gsize size,GstAllocationParams * params)49 static GstMemory *gst_shmem_wrap_allocator_alloc(GstAllocator *allocator, gsize size, GstAllocationParams *params)
50 {
51     (void)allocator;
52     (void)size;
53     (void)params;
54     return nullptr;
55 }
56 
gst_shmem_wrap_allocator_free(GstAllocator * allocator,GstMemory * memory)57 static void gst_shmem_wrap_allocator_free(GstAllocator *allocator, GstMemory *memory)
58 {
59     g_return_if_fail(memory != nullptr && allocator != nullptr);
60     g_return_if_fail(gst_is_shmem_memory(memory));
61 
62     GstShMemMemory *avSharedMem = reinterpret_cast<GstShMemMemory *>(memory);
63     GST_DEBUG("free memory for size: %" G_GSIZE_FORMAT ", addr: 0x%06" PRIXPTR "",
64         memory->maxsize, FAKE_POINTER(avSharedMem->mem->GetBase()));
65 
66     avSharedMem->mem = nullptr;
67     g_slice_free(GstShMemMemory, avSharedMem);
68 }
69 
gst_shmem_wrap_allocator_mem_map(GstMemory * mem,gsize maxsize,GstMapFlags flags)70 static gpointer gst_shmem_wrap_allocator_mem_map(GstMemory *mem, gsize maxsize, GstMapFlags flags)
71 {
72     (void)maxsize;
73     (void)flags;
74     g_return_val_if_fail(mem != nullptr, nullptr);
75     g_return_val_if_fail(gst_is_shmem_memory(mem), nullptr);
76 
77     GstShMemMemory *avSharedMem = reinterpret_cast<GstShMemMemory *>(mem);
78     g_return_val_if_fail(avSharedMem->mem != nullptr, nullptr);
79 
80     GST_INFO("mem_map, maxsize: %" G_GSIZE_FORMAT ", size: %" G_GSIZE_FORMAT, mem->maxsize, mem->size);
81 
82     // Only need return GetBase, not GetBase + offset
83     return avSharedMem->mem->GetBase();
84 }
85 
gst_shmem_wrap_allocator_mem_unmap(GstMemory * mem)86 static void gst_shmem_wrap_allocator_mem_unmap(GstMemory *mem)
87 {
88     (void)mem;
89 }
90 
gst_shmem_wrap_allocator_mem_share(GstMemory * mem,gssize offset,gssize size)91 static GstMemory *gst_shmem_wrap_allocator_mem_share(GstMemory *mem, gssize offset, gssize size)
92 {
93     g_return_val_if_fail(mem != nullptr, nullptr);
94     g_return_val_if_fail(offset >= 0 && static_cast<gsize>(offset) < mem->size, nullptr);
95     GstShMemMemory *sub = nullptr;
96     GstMemory *parent = nullptr;
97 
98     /* find the real parent */
99     if ((parent = mem->parent) == NULL) {
100         parent = (GstMemory *)mem;
101     }
102     if (size == -1) {
103         size = mem->size - offset;
104     }
105 
106     sub = g_slice_new0(GstShMemMemory);
107     g_return_val_if_fail(sub != nullptr, nullptr);
108     /* the shared memory is always readonly */
109     gst_memory_init(
110         GST_MEMORY_CAST(sub),
111         (GstMemoryFlags)(GST_MINI_OBJECT_FLAGS(parent) | GST_MINI_OBJECT_FLAG_LOCK_READONLY),
112         mem->allocator,
113         GST_MEMORY_CAST(parent),
114         mem->maxsize,
115         mem->align,
116         mem->offset + offset,
117         size);
118 
119     sub->mem = reinterpret_cast<GstShMemMemory *>(mem)->mem;
120     return GST_MEMORY_CAST(sub);
121 }
122 
gst_shmem_wrap_allocator_init(GstShMemWrapAllocator * allocator)123 static void gst_shmem_wrap_allocator_init(GstShMemWrapAllocator *allocator)
124 {
125     GstAllocator *bAllocator = GST_ALLOCATOR_CAST(allocator);
126     g_return_if_fail(bAllocator != nullptr);
127 
128     GST_DEBUG_OBJECT(allocator, "init allocator 0x%06" PRIXPTR "", FAKE_POINTER(allocator));
129 
130     bAllocator->mem_type = GST_SHMEM_MEMORY_TYPE;
131     bAllocator->mem_map = (GstMemoryMapFunction)gst_shmem_wrap_allocator_mem_map;
132     bAllocator->mem_unmap = (GstMemoryUnmapFunction)gst_shmem_wrap_allocator_mem_unmap;
133     bAllocator->mem_share = (GstMemoryShareFunction)gst_shmem_wrap_allocator_mem_share;
134 }
135 
gst_shmem_wrap_allocator_finalize(GObject * obj)136 static void gst_shmem_wrap_allocator_finalize(GObject *obj)
137 {
138     GstShMemWrapAllocator *allocator = GST_SHMEM_WRAP_ALLOCATOR_CAST(obj);
139     g_return_if_fail(allocator != nullptr);
140 
141     GST_DEBUG_OBJECT(allocator, "finalize allocator 0x%06" PRIXPTR "", FAKE_POINTER(allocator));
142     G_OBJECT_CLASS(parent_class)->finalize(obj);
143 }
144 
gst_shmem_wrap_allocator_class_init(GstShMemWrapAllocatorClass * klass)145 static void gst_shmem_wrap_allocator_class_init(GstShMemWrapAllocatorClass *klass)
146 {
147     GObjectClass *gobjectClass = G_OBJECT_CLASS(klass);
148     g_return_if_fail(gobjectClass != nullptr);
149 
150     gobjectClass->finalize = gst_shmem_wrap_allocator_finalize;
151 
152     GstAllocatorClass *allocatorClass = GST_ALLOCATOR_CLASS(klass);
153     g_return_if_fail(allocatorClass != nullptr);
154 
155     allocatorClass->alloc = gst_shmem_wrap_allocator_alloc;
156     allocatorClass->free = gst_shmem_wrap_allocator_free;
157 }