1 /*
2  * Copyright (C) 2016-2017 ARM Limited. All rights reserved.
3  *
4  * Copyright (C) 2008 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 
19 #include <hardware/hardware.h>
20 #include <inttypes.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #if GRALLOC_USE_GRALLOC1_API == 1
25 #include <hardware/gralloc1.h>
26 #else
27 #include <hardware/gralloc.h>
28 #endif
29 
30 #include "mali_gralloc_module.h"
31 #include "mali_gralloc_bufferdescriptor.h"
32 #include "mali_gralloc_private_interface_types.h"
33 #include "mali_gralloc_buffer.h"
34 
35 /*
36  * Validate descriptor to ensure that it originated from this version
37  * of gralloc. Rudimentary signature is simply calculated from size of
38  * buffer descriptor structure and initialised immediately after
39  * structure is allocated.
40  *
41  * @param buffer_descriptor    [in]    Buffer descriptor.
42  *
43  * @return true, for valid buffer descriptor;
44  *         false, otherwise
45  */
descriptor_is_valid(buffer_descriptor_t * buffer_descriptor)46 static bool descriptor_is_valid(buffer_descriptor_t *buffer_descriptor)
47 {
48 	if (buffer_descriptor /*&& buffer_descriptor->signature == sizeof(*buffer_descriptor)*/)
49 	{
50 		return true;
51 	}
52 
53 	return false;
54 }
55 
56 #if GRALLOC_USE_GRALLOC1_API == 1
mali_gralloc_create_descriptor_internal(gralloc1_buffer_descriptor_t * outDescriptor)57 int mali_gralloc_create_descriptor_internal(gralloc1_buffer_descriptor_t *outDescriptor)
58 {
59 	buffer_descriptor_t *buffer_descriptor;
60 
61 	if (NULL == outDescriptor)
62 	{
63 		return GRALLOC1_ERROR_BAD_DESCRIPTOR;
64 	}
65 
66 	buffer_descriptor = reinterpret_cast<buffer_descriptor_t *>(malloc(sizeof(buffer_descriptor_t)));
67 
68 	if (NULL == buffer_descriptor)
69 	{
70 		AERR("failed to create buffer descriptor");
71 		return GRALLOC1_ERROR_BAD_DESCRIPTOR;
72 	}
73 
74 	/*
75 	 * Initialise the buffer descriptor.
76 	 *
77 	 * Layer count is initialised to a single layer in
78 	 * case clients don't support multi-layer or use
79 	 * function GRALLOC1_PFN_SET_LAYER_COUNT.
80 	 */
81 	memset((void *)buffer_descriptor, 0, sizeof(*buffer_descriptor));
82 	buffer_descriptor->layer_count = 1;
83 
84 	*outDescriptor = (gralloc1_buffer_descriptor_t)buffer_descriptor;
85 	return GRALLOC1_ERROR_NONE;
86 }
87 
mali_gralloc_destroy_descriptor_internal(gralloc1_buffer_descriptor_t descriptor)88 int mali_gralloc_destroy_descriptor_internal(gralloc1_buffer_descriptor_t descriptor)
89 {
90 	if (!descriptor)
91 	{
92 		return GRALLOC1_ERROR_BAD_DESCRIPTOR;
93 	}
94 
95 	buffer_descriptor_t *buffer_descriptor = (buffer_descriptor_t *)descriptor;
96 	free(buffer_descriptor);
97 	return GRALLOC1_ERROR_NONE;
98 }
99 
mali_gralloc_set_dimensions_internal(gralloc1_buffer_descriptor_t descriptor,uint32_t width,uint32_t height)100 int mali_gralloc_set_dimensions_internal(gralloc1_buffer_descriptor_t descriptor, uint32_t width, uint32_t height)
101 {
102 	if (!descriptor)
103 	{
104 		return GRALLOC1_ERROR_BAD_DESCRIPTOR;
105 	}
106 
107 	buffer_descriptor_t *buffer_descriptor = (buffer_descriptor_t *)descriptor;
108 	buffer_descriptor->width = width;
109 	buffer_descriptor->height = height;
110 	return GRALLOC1_ERROR_NONE;
111 }
112 
mali_gralloc_set_format_internal(gralloc1_buffer_descriptor_t descriptor,int32_t format)113 int mali_gralloc_set_format_internal(gralloc1_buffer_descriptor_t descriptor, int32_t format)
114 {
115 	if (!descriptor)
116 	{
117 		return GRALLOC1_ERROR_BAD_DESCRIPTOR;
118 	}
119 
120 	buffer_descriptor_t *buffer_descriptor = (buffer_descriptor_t *)descriptor;
121 	buffer_descriptor->hal_format = format;
122 	buffer_descriptor->format_type = MALI_GRALLOC_FORMAT_TYPE_USAGE;
123 	return GRALLOC1_ERROR_NONE;
124 }
125 
mali_gralloc_set_producerusage_internal(gralloc1_buffer_descriptor_t descriptor,uint64_t usage)126 int mali_gralloc_set_producerusage_internal(gralloc1_buffer_descriptor_t descriptor, uint64_t usage)
127 {
128 	if (!descriptor)
129 	{
130 		return GRALLOC1_ERROR_BAD_DESCRIPTOR;
131 	}
132 
133 	buffer_descriptor_t *buffer_descriptor = (buffer_descriptor_t *)descriptor;
134 	buffer_descriptor->producer_usage = usage;
135 	return GRALLOC1_ERROR_NONE;
136 }
137 
mali_gralloc_set_consumerusage_internal(gralloc1_buffer_descriptor_t descriptor,uint64_t usage)138 int mali_gralloc_set_consumerusage_internal(gralloc1_buffer_descriptor_t descriptor, uint64_t usage)
139 {
140 	if (!descriptor)
141 	{
142 		return GRALLOC1_ERROR_BAD_DESCRIPTOR;
143 	}
144 
145 	buffer_descriptor_t *buffer_descriptor = (buffer_descriptor_t *)descriptor;
146 	buffer_descriptor->consumer_usage = usage;
147 	return GRALLOC1_ERROR_NONE;
148 }
149 
mali_gralloc_get_backing_store_internal(buffer_handle_t buffer,gralloc1_backing_store_t * outStore)150 int mali_gralloc_get_backing_store_internal(buffer_handle_t buffer, gralloc1_backing_store_t *outStore)
151 {
152 	if (private_handle_t::validate(buffer) < 0)
153 	{
154 		AERR("Invalid buffer %p, returning error", buffer);
155 		return GRALLOC1_ERROR_BAD_HANDLE;
156 	}
157 
158 	private_handle_t *hnd = (private_handle_t *)buffer;
159 
160 	*outStore = (gralloc1_backing_store_t)hnd->backing_store_id;
161 	return GRALLOC1_ERROR_NONE;
162 }
163 
mali_gralloc_get_consumer_usage_internal(buffer_handle_t buffer,uint64_t * outUsage)164 int mali_gralloc_get_consumer_usage_internal(buffer_handle_t buffer, uint64_t *outUsage)
165 {
166 	if (private_handle_t::validate(buffer) < 0)
167 	{
168 		AERR("Invalid buffer %p, returning error", buffer);
169 		return GRALLOC1_ERROR_BAD_HANDLE;
170 	}
171 
172 	private_handle_t *hnd = (private_handle_t *)buffer;
173 	*outUsage = hnd->consumer_usage;
174 	return GRALLOC1_ERROR_NONE;
175 }
176 
mali_gralloc_get_dimensions_internal(buffer_handle_t buffer,uint32_t * outWidth,uint32_t * outHeight)177 int mali_gralloc_get_dimensions_internal(buffer_handle_t buffer, uint32_t *outWidth, uint32_t *outHeight)
178 {
179 	if (private_handle_t::validate(buffer) < 0)
180 	{
181 		AERR("Invalid buffer %p, returning error", buffer);
182 		return GRALLOC1_ERROR_BAD_HANDLE;
183 	}
184 
185 	private_handle_t *hnd = (private_handle_t *)buffer;
186 	*outWidth = hnd->width;
187 	*outHeight = hnd->height;
188 	return GRALLOC1_ERROR_NONE;
189 }
190 
mali_gralloc_get_format_internal(buffer_handle_t buffer,int32_t * outFormat)191 int mali_gralloc_get_format_internal(buffer_handle_t buffer, int32_t *outFormat)
192 {
193 	if (private_handle_t::validate(buffer) < 0)
194 	{
195 		AERR("Invalid buffer %p, returning error", buffer);
196 		return GRALLOC1_ERROR_BAD_HANDLE;
197 	}
198 
199 	private_handle_t *hnd = (private_handle_t *)buffer;
200 	*outFormat = hnd->req_format;
201 	return GRALLOC1_ERROR_NONE;
202 }
203 
mali_gralloc_get_producer_usage_internal(buffer_handle_t buffer,uint64_t * outUsage)204 int mali_gralloc_get_producer_usage_internal(buffer_handle_t buffer, uint64_t *outUsage)
205 {
206 	if (private_handle_t::validate(buffer) < 0)
207 	{
208 		AERR("Invalid buffer %p, returning error", buffer);
209 		return GRALLOC1_ERROR_BAD_HANDLE;
210 	}
211 
212 	private_handle_t *hnd = (private_handle_t *)buffer;
213 	*outUsage = hnd->producer_usage;
214 	return GRALLOC1_ERROR_NONE;
215 }
216 
217 #if PLATFORM_SDK_VERSION >= 26
mali_gralloc_set_layer_count_internal(gralloc1_buffer_descriptor_t descriptor,uint32_t layerCount)218 int mali_gralloc_set_layer_count_internal(gralloc1_buffer_descriptor_t descriptor, uint32_t layerCount)
219 {
220         buffer_descriptor_t *buffer_descriptor = (buffer_descriptor_t *)descriptor;
221         if (!descriptor_is_valid(buffer_descriptor))
222         {
223                 AERR("Invalid buffer descriptor %p", buffer_descriptor);
224                 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
225         }
226 
227         if (layerCount == 0)
228         {
229                 AERR("Invalid layer count: %" PRIu32, layerCount);
230                 return GRALLOC1_ERROR_BAD_VALUE;
231         }
232 
233         buffer_descriptor->layer_count = layerCount;
234         return GRALLOC1_ERROR_NONE;
235 }
236 
mali_gralloc_get_layer_count_internal(buffer_handle_t buffer,uint32_t * outLayerCount)237 int mali_gralloc_get_layer_count_internal(buffer_handle_t buffer, uint32_t *outLayerCount)
238 {
239         if (private_handle_t::validate(buffer) < 0)
240         {
241                 AERR("Invalid buffer %p, returning error", buffer);
242                 return GRALLOC1_ERROR_BAD_HANDLE;
243         }
244 
245         if (outLayerCount == NULL)
246         {
247                 return GRALLOC1_ERROR_BAD_VALUE;
248         }
249 
250         private_handle_t *hnd = (private_handle_t *)buffer;
251         *outLayerCount = hnd->layer_count;
252         return GRALLOC1_ERROR_NONE;
253 }
254 #endif /* PLATFORM_SDK_VERSION >= 26 */
255 #endif
mali_gralloc_query_getstride(buffer_handle_t buffer,int * pixelStride)256 int mali_gralloc_query_getstride(buffer_handle_t buffer, int *pixelStride)
257 {
258 	int rval = -1;
259 
260 	if (buffer != NULL && pixelStride != NULL)
261 	{
262 		private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>(buffer);
263 
264 		if (hnd)
265 		{
266 			*pixelStride = hnd->stride;
267 			rval = 0;
268 		}
269 	}
270 
271 	return rval;
272 }
273