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 #include <BufferAllocator/BufferAllocator.h>
18 #include <BufferAllocator/BufferAllocatorWrapper.h>
19 #include <errno.h>
20 #include <sys/types.h>
21 
22 extern "C" {
23 
CreateDmabufHeapBufferAllocator()24 BufferAllocator* CreateDmabufHeapBufferAllocator() {
25     return new BufferAllocator();
26 }
27 
FreeDmabufHeapBufferAllocator(BufferAllocator * buffer_allocator)28 void FreeDmabufHeapBufferAllocator(BufferAllocator* buffer_allocator) {
29     delete buffer_allocator;
30 };
31 
DmabufHeapAlloc(BufferAllocator * buffer_allocator,const char * heap_name,size_t len,unsigned int heap_flags,size_t legacy_align)32 int DmabufHeapAlloc(BufferAllocator* buffer_allocator, const char* heap_name, size_t len,
33                     unsigned int heap_flags, size_t legacy_align) {
34     if (!buffer_allocator)
35         return -EINVAL;
36     return buffer_allocator->Alloc(heap_name, len, heap_flags, legacy_align);
37 }
38 
DmabufHeapAllocSystem(BufferAllocator * buffer_allocator,bool cpu_access,size_t len,unsigned int heap_flags,size_t legacy_align)39 int DmabufHeapAllocSystem(BufferAllocator* buffer_allocator, bool cpu_access, size_t len,
40                           unsigned int heap_flags, size_t legacy_align) {
41     if (!buffer_allocator) return -EINVAL;
42     return buffer_allocator->AllocSystem(cpu_access, len, heap_flags, legacy_align);
43 }
44 
MapDmabufHeapNameToIonHeap(BufferAllocator * buffer_allocator,const char * heap_name,const char * ion_heap_name,unsigned int ion_heap_flags,unsigned int legacy_ion_heap_mask,unsigned int legacy_ion_heap_flags)45 int MapDmabufHeapNameToIonHeap(BufferAllocator* buffer_allocator, const char* heap_name,
46                                const char* ion_heap_name, unsigned int ion_heap_flags,
47                                unsigned int legacy_ion_heap_mask,
48                                unsigned int legacy_ion_heap_flags) {
49     if (!buffer_allocator)
50         return -EINVAL;
51     return buffer_allocator->MapNameToIonHeap(heap_name, ion_heap_name, ion_heap_flags,
52                                               legacy_ion_heap_mask, legacy_ion_heap_flags);
53 }
54 
DmabufHeapCpuSyncStart(BufferAllocator * buffer_allocator,unsigned int dmabuf_fd,SyncType sync_type,int (* legacy_ion_cpu_sync)(int,int,void *),void * custom_data)55 int DmabufHeapCpuSyncStart(BufferAllocator* buffer_allocator, unsigned int dmabuf_fd,
56                            SyncType sync_type, int (*legacy_ion_cpu_sync)(int, int, void *),
57                            void *custom_data) {
58     if (!buffer_allocator)
59         return -EINVAL;
60     return buffer_allocator->CpuSyncStart(dmabuf_fd, sync_type, legacy_ion_cpu_sync,
61                                           custom_data);
62 }
63 
DmabufHeapCpuSyncEnd(BufferAllocator * buffer_allocator,unsigned int dmabuf_fd,SyncType sync_type,int (* legacy_ion_cpu_sync)(int,int,void *),void * custom_data)64 int DmabufHeapCpuSyncEnd(BufferAllocator* buffer_allocator, unsigned int dmabuf_fd,
65                          SyncType sync_type, int (*legacy_ion_cpu_sync)(int, int, void*),
66                          void* custom_data) {
67     if (!buffer_allocator)
68         return -EINVAL;
69     return buffer_allocator->CpuSyncEnd(dmabuf_fd, sync_type, legacy_ion_cpu_sync, custom_data);
70 }
71 
CheckIonSupport()72 bool CheckIonSupport() {
73     return BufferAllocator::CheckIonSupport();
74 }
75 }
76