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
push_descriptor_string(hidl_vec<uint8_t> * vec,size_t * pos,const std::string & str)107 static void push_descriptor_string(hidl_vec<uint8_t> *vec, size_t *pos, const std::string &str)
108 {
109 strcpy(reinterpret_cast<char *>(vec->data() + *pos), str.c_str());
110 *pos += strlen(str.c_str()) + 1;
111 }
112
pop_descriptor_string(const hidl_vec<uint8_t> & vec,size_t * pos)113 static std::string pop_descriptor_string(const hidl_vec<uint8_t> &vec, size_t *pos)
114 {
115 std::string str(reinterpret_cast<const char *>(vec.data() + *pos));
116 *pos += str.size() + 1;
117 return str;
118 }
119
120 template <typename vecT, typename BufferDescriptorInfoT>
grallocEncodeBufferDescriptor(const BufferDescriptorInfoT & descriptorInfo)121 static const hidl_vec<vecT> grallocEncodeBufferDescriptor(const BufferDescriptorInfoT &descriptorInfo)
122 {
123 hidl_vec<vecT> descriptor;
124
125 static_assert(sizeof(uint32_t) % sizeof(vecT) == 0, "Unsupported vector type");
126 size_t dynamic_size = 0;
127 constexpr size_t static_size = (DESCRIPTOR_32BIT_FIELDS * sizeof(uint32_t) / sizeof(vecT)) +
128 (DESCRIPTOR_64BIT_FIELDS * sizeof(uint64_t) / sizeof(vecT));
129
130 /* Include the name and '\0' in the descriptor. */
131 dynamic_size += strlen(descriptorInfo.name.c_str()) + 1;
132
133 size_t pos = 0;
134 descriptor.resize(dynamic_size + static_size);
135 push_descriptor_uint32(&descriptor, &pos, HIDL_MAPPER_VERSION_SCALED / 10);
136 push_descriptor_uint32(&descriptor, &pos, descriptorInfo.width);
137 push_descriptor_uint32(&descriptor, &pos, descriptorInfo.height);
138 push_descriptor_uint32(&descriptor, &pos, descriptorInfo.layerCount);
139 push_descriptor_uint32(&descriptor, &pos, static_cast<uint32_t>(descriptorInfo.format));
140 push_descriptor_uint64(&descriptor, &pos, static_cast<uint64_t>(descriptorInfo.usage));
141
142 push_descriptor_uint64(&descriptor, &pos, descriptorInfo.reservedSize);
143
144 assert(pos == static_size);
145
146 push_descriptor_string(&descriptor, &pos, descriptorInfo.name);
147
148 return descriptor;
149 }
150
151 template <typename vecT>
grallocDecodeBufferDescriptor(const hidl_vec<vecT> & androidDescriptor,buffer_descriptor_t & grallocDescriptor)152 static bool grallocDecodeBufferDescriptor(const hidl_vec<vecT> &androidDescriptor, buffer_descriptor_t &grallocDescriptor)
153 {
154 static_assert(sizeof(uint32_t) % sizeof(vecT) == 0, "Unsupported vector type");
155 size_t pos = 0;
156
157 if (((DESCRIPTOR_32BIT_FIELDS * sizeof(uint32_t) / sizeof(vecT)) +
158 (DESCRIPTOR_64BIT_FIELDS * sizeof(uint64_t) / sizeof(vecT))) > androidDescriptor.size())
159 {
160 MALI_GRALLOC_LOGE("Descriptor is too small");
161 return false;
162 }
163
164 if (pop_descriptor_uint32(androidDescriptor, &pos) != HIDL_MAPPER_VERSION_SCALED / 10)
165 {
166 MALI_GRALLOC_LOGE("Corrupted buffer version in descriptor = %p, pid = %d ", &androidDescriptor, getpid());
167 return false;
168 }
169
170 grallocDescriptor.width = pop_descriptor_uint32(androidDescriptor, &pos);
171 grallocDescriptor.height = pop_descriptor_uint32(androidDescriptor, &pos);
172 grallocDescriptor.layer_count = pop_descriptor_uint32(androidDescriptor, &pos);
173 grallocDescriptor.hal_format = static_cast<uint64_t>(pop_descriptor_uint32(androidDescriptor, &pos));
174 grallocDescriptor.producer_usage = pop_descriptor_uint64(androidDescriptor, &pos);
175 grallocDescriptor.consumer_usage = grallocDescriptor.producer_usage;
176 grallocDescriptor.format_type = MALI_GRALLOC_FORMAT_TYPE_USAGE;
177 grallocDescriptor.signature = sizeof(buffer_descriptor_t);
178 grallocDescriptor.reserved_size = pop_descriptor_uint64(androidDescriptor, &pos);
179 grallocDescriptor.name = pop_descriptor_string(androidDescriptor, &pos);
180
181 return true;
182 }
183
184 } // namespace common
185 } // namespace mapper
186 } // namespace arm
187 #endif
188