• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 ARM Limited. All rights reserved.
3  *
4  * Copyright 2016 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #ifndef _GRALLOC_BUFFER_DESCRIPTOR_H_
19 #define _GRALLOC_BUFFER_DESCRIPTOR_H_
20 
21 #include "core/mali_gralloc_bufferdescriptor.h"
22 
23 #include "4.x/gralloc_mapper_hidl_header.h"
24 
25 #include <assert.h>
26 #include <inttypes.h>
27 #include <string.h>
28 
29 namespace arm {
30 namespace mapper {
31 namespace common {
32 
33 using android::hardware::hidl_vec;
34 
35 const size_t DESCRIPTOR_32BIT_FIELDS = 5;
36 const size_t DESCRIPTOR_64BIT_FIELDS = 2;
37 
38 const uint64_t validUsageBits =
39 		BufferUsage::GPU_CUBE_MAP |
40 		BufferUsage::GPU_MIPMAP_COMPLETE |
41 		BufferUsage::CPU_READ_MASK | BufferUsage::CPU_WRITE_MASK |
42 		BufferUsage::GPU_TEXTURE | BufferUsage::GPU_RENDER_TARGET |
43 		BufferUsage::COMPOSER_OVERLAY | BufferUsage::COMPOSER_CLIENT_TARGET |
44 		BufferUsage::CAMERA_INPUT | BufferUsage::CAMERA_OUTPUT |
45 		BufferUsage::PROTECTED |
46 		BufferUsage::COMPOSER_CURSOR |
47 		BufferUsage::VIDEO_ENCODER |
48 		BufferUsage::RENDERSCRIPT |
49 		BufferUsage::VIDEO_DECODER |
50 		BufferUsage::SENSOR_DIRECT_DATA |
51 		BufferUsage::GPU_DATA_BUFFER |
52 		BufferUsage::VENDOR_MASK |
53 		BufferUsage::VENDOR_MASK_HI;
54 
55 template<typename BufferDescriptorInfoT>
validateDescriptorInfo(const BufferDescriptorInfoT & descriptorInfo)56 static bool validateDescriptorInfo(const BufferDescriptorInfoT &descriptorInfo)
57 {
58 	if (descriptorInfo.width == 0 || descriptorInfo.height == 0 || descriptorInfo.layerCount == 0)
59 	{
60 		return false;
61 	}
62 
63 	if (static_cast<int32_t>(descriptorInfo.format) == 0)
64 	{
65 		return false;
66 	}
67 
68 	return true;
69 }
70 
71 template <typename vecT>
push_descriptor_uint32(hidl_vec<vecT> * vec,size_t * pos,uint32_t val)72 static void push_descriptor_uint32(hidl_vec<vecT> *vec, size_t *pos, uint32_t val)
73 {
74 	static_assert(sizeof(val) % sizeof(vecT) == 0, "Unsupported vector type");
75 	memcpy(vec->data() + *pos, &val, sizeof(val));
76 	*pos += sizeof(val) / sizeof(vecT);
77 }
78 
79 template <typename vecT>
pop_descriptor_uint32(const hidl_vec<vecT> & vec,size_t * pos)80 static uint32_t pop_descriptor_uint32(const hidl_vec<vecT> &vec, size_t *pos)
81 {
82 	uint32_t val;
83 	static_assert(sizeof(val) % sizeof(vecT) == 0, "Unsupported vector type");
84 	memcpy(&val, vec.data() + *pos, sizeof(val));
85 	*pos += sizeof(val) / sizeof(vecT);
86 	return val;
87 }
88 
89 template <typename vecT>
push_descriptor_uint64(hidl_vec<vecT> * vec,size_t * pos,uint64_t val)90 static void push_descriptor_uint64(hidl_vec<vecT> *vec, size_t *pos, uint64_t val)
91 {
92 	static_assert(sizeof(val) % sizeof(vecT) == 0, "Unsupported vector type");
93 	memcpy(vec->data() + *pos, &val, sizeof(val));
94 	*pos += sizeof(val) / sizeof(vecT);
95 }
96 
97 template <typename vecT>
pop_descriptor_uint64(const hidl_vec<vecT> & vec,size_t * pos)98 static uint64_t pop_descriptor_uint64(const hidl_vec<vecT> &vec, size_t *pos)
99 {
100 	uint64_t val;
101 	static_assert(sizeof(val) % sizeof(vecT) == 0, "Unsupported vector type");
102 	memcpy(&val, vec.data() + *pos, sizeof(val));
103 	*pos += sizeof(val) / sizeof(vecT);
104 	return val;
105 }
106 
107 // There can only be one string at the end of the descriptor
push_descriptor_string(hidl_vec<uint8_t> * vec,size_t * pos,const std::string & str)108 static void push_descriptor_string(hidl_vec<uint8_t> *vec, size_t *pos, const std::string &str)
109 {
110 	strcpy(reinterpret_cast<char *>(vec->data() + *pos), str.c_str());
111 	*pos += strlen(str.c_str()) + 1;
112 }
113 
pop_descriptor_string(const hidl_vec<uint8_t> & vec,size_t * pos)114 static std::string pop_descriptor_string(const hidl_vec<uint8_t> &vec, size_t *pos)
115 {
116 	std::string str(reinterpret_cast<const char *>(vec.data() + *pos));
117 	*pos += str.size() + 1;
118 	return str;
119 }
120 
121 template <typename vecT, typename BufferDescriptorInfoT>
grallocEncodeBufferDescriptor(const BufferDescriptorInfoT & descriptorInfo)122 static const hidl_vec<vecT> grallocEncodeBufferDescriptor(const BufferDescriptorInfoT &descriptorInfo)
123 {
124 	hidl_vec<vecT> descriptor;
125 
126 	static_assert(sizeof(uint32_t) % sizeof(vecT) == 0, "Unsupported vector type");
127 	size_t dynamic_size = 0;
128 	constexpr size_t static_size = (DESCRIPTOR_32BIT_FIELDS * sizeof(uint32_t) / sizeof(vecT)) +
129 	                               (DESCRIPTOR_64BIT_FIELDS * sizeof(uint64_t) / sizeof(vecT));
130 
131 	/* Include the name and '\0' in the descriptor. */
132 	dynamic_size += strlen(descriptorInfo.name.c_str()) + 1;
133 
134 	size_t pos = 0;
135 	descriptor.resize(dynamic_size + static_size);
136 	push_descriptor_uint32(&descriptor, &pos, HIDL_MAPPER_VERSION_SCALED / 10);
137 	push_descriptor_uint32(&descriptor, &pos, descriptorInfo.width);
138 	push_descriptor_uint32(&descriptor, &pos, descriptorInfo.height);
139 	push_descriptor_uint32(&descriptor, &pos, descriptorInfo.layerCount);
140 	push_descriptor_uint32(&descriptor, &pos, static_cast<uint32_t>(descriptorInfo.format));
141 	push_descriptor_uint64(&descriptor, &pos, static_cast<uint64_t>(descriptorInfo.usage));
142 
143 	push_descriptor_uint64(&descriptor, &pos, descriptorInfo.reservedSize);
144 
145 	assert(pos == static_size);
146 
147 	push_descriptor_string(&descriptor, &pos, descriptorInfo.name);
148 
149 	return descriptor;
150 }
151 
152 template <typename vecT>
grallocDecodeBufferDescriptor(const hidl_vec<vecT> & androidDescriptor,buffer_descriptor_t & grallocDescriptor)153 static bool grallocDecodeBufferDescriptor(const hidl_vec<vecT> &androidDescriptor, buffer_descriptor_t &grallocDescriptor)
154 {
155 	static_assert(sizeof(uint32_t) % sizeof(vecT) == 0, "Unsupported vector type");
156 	size_t pos = 0;
157 
158 	if (((DESCRIPTOR_32BIT_FIELDS * sizeof(uint32_t) / sizeof(vecT)) +
159 	     (DESCRIPTOR_64BIT_FIELDS * sizeof(uint64_t) / sizeof(vecT))) +
160 	     sizeof('\0') > androidDescriptor.size())
161 	{
162 		MALI_GRALLOC_LOGE("Descriptor is too small");
163 		return false;
164 	}
165 
166 	if (static_cast<char>(androidDescriptor[androidDescriptor.size() - 1]) != '\0') {
167 		MALI_GRALLOC_LOGE("Descriptor does not contain an ending null character");
168 		return false;
169 	}
170 
171 	if (pop_descriptor_uint32(androidDescriptor, &pos) != HIDL_MAPPER_VERSION_SCALED / 10)
172 	{
173 		MALI_GRALLOC_LOGE("Corrupted buffer version in descriptor = %p, pid = %d ", &androidDescriptor, getpid());
174 		return false;
175 	}
176 
177 	grallocDescriptor.width = pop_descriptor_uint32(androidDescriptor, &pos);
178 	grallocDescriptor.height = pop_descriptor_uint32(androidDescriptor, &pos);
179 	grallocDescriptor.layer_count = pop_descriptor_uint32(androidDescriptor, &pos);
180 	grallocDescriptor.hal_format = static_cast<uint64_t>(pop_descriptor_uint32(androidDescriptor, &pos));
181 	grallocDescriptor.producer_usage = pop_descriptor_uint64(androidDescriptor, &pos);
182 	grallocDescriptor.consumer_usage = grallocDescriptor.producer_usage;
183 	grallocDescriptor.format_type = MALI_GRALLOC_FORMAT_TYPE_USAGE;
184 	grallocDescriptor.signature = sizeof(buffer_descriptor_t);
185 	grallocDescriptor.reserved_size = pop_descriptor_uint64(androidDescriptor, &pos);
186 	grallocDescriptor.name = pop_descriptor_string(androidDescriptor, &pos);
187 
188 	return true;
189 }
190 
191 } // namespace common
192 } // namespace mapper
193 } // namespace arm
194 #endif
195