1 /*
2  * Copyright (C) 2020 Arm Limited. All rights reserved.
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 <hwbinder/IPCThreadState.h>
18 
19 #include "GrallocAllocator.h"
20 #include "hidl_common/BufferDescriptor.h"
21 #include "hidl_common/Allocator.h"
22 #include "allocator/mali_gralloc_ion.h"
23 
24 namespace arm
25 {
26 namespace allocator
27 {
28 using android::hardware::graphics::allocator::V4_0::IAllocator;
29 using android::hardware::graphics::mapper::V4_0::Error;
30 using android::hardware::Return;
31 using android::hardware::hidl_handle;
32 using android::hardware::hidl_vec;
33 using android::hardware::Void;
34 using android::hardware::hidl_string;
35 
callingPid()36 unsigned long callingPid() {
37 	android::hardware::IPCThreadState* ipc = android::hardware::IPCThreadState::self();
38 	return static_cast<unsigned long>(ipc->getCallingPid());
39 }
40 
GrallocAllocator()41 GrallocAllocator::GrallocAllocator()
42 {
43 	MALI_GRALLOC_LOGV("Arm Module IAllocator %d.%d, pid = %d ppid = %d", GRALLOC_VERSION_MAJOR,
44 	                  (HIDL_ALLOCATOR_VERSION_SCALED - (GRALLOC_VERSION_MAJOR * 100)) / 10, getpid(), getppid());
45 }
46 
~GrallocAllocator()47 GrallocAllocator::~GrallocAllocator()
48 {
49 	mali_gralloc_ion_close();
50 }
51 
allocate(const BufferDescriptor & descriptor,uint32_t count,allocate_cb hidl_cb)52 Return<void> GrallocAllocator::allocate(const BufferDescriptor &descriptor, uint32_t count, allocate_cb hidl_cb)
53 {
54 	MALI_GRALLOC_LOGV("Allocation request from process: %lu", callingPid());
55 
56 	buffer_descriptor_t bufferDescriptor;
57 	if (!mapper::common::grallocDecodeBufferDescriptor(descriptor, bufferDescriptor))
58 	{
59 		hidl_cb(Error::BAD_DESCRIPTOR, 0, hidl_vec<hidl_handle>());
60 		return Void();
61 	}
62 	common::allocate(bufferDescriptor, count, hidl_cb);
63 	return Void();
64 }
65 
66 } // namespace allocator
67 } // namespace arm
68