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 <stdlib.h>
21 #include <string.h>
22 
23 #if GRALLOC_USE_GRALLOC1_API == 1
24 #include <hardware/gralloc1.h>
25 #else
26 #include <hardware/gralloc.h>
27 #endif
28 
29 #include "mali_gralloc_module.h"
30 #include "mali_gralloc_bufferdescriptor.h"
31 #include "mali_gralloc_private_interface_types.h"
32 #include "mali_gralloc_buffer.h"
33 
34 #if GRALLOC_USE_GRALLOC1_API == 1
mali_gralloc_create_descriptor_internal(gralloc1_buffer_descriptor_t * outDescriptor)35 int mali_gralloc_create_descriptor_internal(gralloc1_buffer_descriptor_t *outDescriptor)
36 {
37 	buffer_descriptor_t *buffer_descriptor;
38 
39 	if (NULL == outDescriptor)
40 	{
41 		return GRALLOC1_ERROR_BAD_DESCRIPTOR;
42 	}
43 
44 	buffer_descriptor = reinterpret_cast<buffer_descriptor_t *>(malloc(sizeof(buffer_descriptor_t)));
45 
46 	if (NULL == buffer_descriptor)
47 	{
48 		AERR("failed to create buffer descriptor");
49 		return GRALLOC1_ERROR_BAD_DESCRIPTOR;
50 	}
51 
52 	/*
53 	 * Initialise the buffer descriptor.
54 	 *
55 	 * Layer count is initialised to a single layer in
56 	 * case clients don't support multi-layer or use
57 	 * function GRALLOC1_PFN_SET_LAYER_COUNT.
58 	 */
59 	memset((void *)buffer_descriptor, 0, sizeof(*buffer_descriptor));
60 	buffer_descriptor->layer_count = 1;
61 
62 	*outDescriptor = (gralloc1_buffer_descriptor_t)buffer_descriptor;
63 	return GRALLOC1_ERROR_NONE;
64 }
65 
mali_gralloc_destroy_descriptor_internal(gralloc1_buffer_descriptor_t descriptor)66 int mali_gralloc_destroy_descriptor_internal(gralloc1_buffer_descriptor_t descriptor)
67 {
68 	if (!descriptor)
69 	{
70 		return GRALLOC1_ERROR_BAD_DESCRIPTOR;
71 	}
72 
73 	buffer_descriptor_t *buffer_descriptor = (buffer_descriptor_t *)descriptor;
74 	free(buffer_descriptor);
75 	return GRALLOC1_ERROR_NONE;
76 }
77 
mali_gralloc_set_dimensions_internal(gralloc1_buffer_descriptor_t descriptor,uint32_t width,uint32_t height)78 int mali_gralloc_set_dimensions_internal(gralloc1_buffer_descriptor_t descriptor, uint32_t width, uint32_t height)
79 {
80 	if (!descriptor)
81 	{
82 		return GRALLOC1_ERROR_BAD_DESCRIPTOR;
83 	}
84 
85 	buffer_descriptor_t *buffer_descriptor = (buffer_descriptor_t *)descriptor;
86 	buffer_descriptor->width = width;
87 	buffer_descriptor->height = height;
88 	return GRALLOC1_ERROR_NONE;
89 }
90 
mali_gralloc_set_format_internal(gralloc1_buffer_descriptor_t descriptor,int32_t format)91 int mali_gralloc_set_format_internal(gralloc1_buffer_descriptor_t descriptor, int32_t format)
92 {
93 	if (!descriptor)
94 	{
95 		return GRALLOC1_ERROR_BAD_DESCRIPTOR;
96 	}
97 
98 	buffer_descriptor_t *buffer_descriptor = (buffer_descriptor_t *)descriptor;
99 	buffer_descriptor->hal_format = format;
100 	buffer_descriptor->format_type = MALI_GRALLOC_FORMAT_TYPE_USAGE;
101 	return GRALLOC1_ERROR_NONE;
102 }
103 
mali_gralloc_set_producerusage_internal(gralloc1_buffer_descriptor_t descriptor,uint64_t usage)104 int mali_gralloc_set_producerusage_internal(gralloc1_buffer_descriptor_t descriptor, uint64_t usage)
105 {
106 	if (!descriptor)
107 	{
108 		return GRALLOC1_ERROR_BAD_DESCRIPTOR;
109 	}
110 
111 	buffer_descriptor_t *buffer_descriptor = (buffer_descriptor_t *)descriptor;
112 	buffer_descriptor->producer_usage = usage;
113 	return GRALLOC1_ERROR_NONE;
114 }
115 
mali_gralloc_set_consumerusage_internal(gralloc1_buffer_descriptor_t descriptor,uint64_t usage)116 int mali_gralloc_set_consumerusage_internal(gralloc1_buffer_descriptor_t descriptor, uint64_t usage)
117 {
118 	if (!descriptor)
119 	{
120 		return GRALLOC1_ERROR_BAD_DESCRIPTOR;
121 	}
122 
123 	buffer_descriptor_t *buffer_descriptor = (buffer_descriptor_t *)descriptor;
124 	buffer_descriptor->consumer_usage = usage;
125 	return GRALLOC1_ERROR_NONE;
126 }
127 
mali_gralloc_get_backing_store_internal(buffer_handle_t buffer,gralloc1_backing_store_t * outStore)128 int mali_gralloc_get_backing_store_internal(buffer_handle_t buffer, gralloc1_backing_store_t *outStore)
129 {
130 	if (private_handle_t::validate(buffer) < 0)
131 	{
132 		AERR("Invalid buffer %p, returning error", buffer);
133 		return GRALLOC1_ERROR_BAD_HANDLE;
134 	}
135 
136 	private_handle_t *hnd = (private_handle_t *)buffer;
137 
138 	*outStore = (gralloc1_backing_store_t)hnd->backing_store_id;
139 	return GRALLOC1_ERROR_NONE;
140 }
141 
mali_gralloc_get_consumer_usage_internal(buffer_handle_t buffer,uint64_t * outUsage)142 int mali_gralloc_get_consumer_usage_internal(buffer_handle_t buffer, uint64_t *outUsage)
143 {
144 	if (private_handle_t::validate(buffer) < 0)
145 	{
146 		AERR("Invalid buffer %p, returning error", buffer);
147 		return GRALLOC1_ERROR_BAD_HANDLE;
148 	}
149 
150 	private_handle_t *hnd = (private_handle_t *)buffer;
151 	*outUsage = hnd->consumer_usage;
152 	return GRALLOC1_ERROR_NONE;
153 }
154 
mali_gralloc_get_dimensions_internal(buffer_handle_t buffer,uint32_t * outWidth,uint32_t * outHeight)155 int mali_gralloc_get_dimensions_internal(buffer_handle_t buffer, uint32_t *outWidth, uint32_t *outHeight)
156 {
157 	if (private_handle_t::validate(buffer) < 0)
158 	{
159 		AERR("Invalid buffer %p, returning error", buffer);
160 		return GRALLOC1_ERROR_BAD_HANDLE;
161 	}
162 
163 	private_handle_t *hnd = (private_handle_t *)buffer;
164 	*outWidth = hnd->width;
165 	*outHeight = hnd->height;
166 	return GRALLOC1_ERROR_NONE;
167 }
168 
mali_gralloc_get_format_internal(buffer_handle_t buffer,int32_t * outFormat)169 int mali_gralloc_get_format_internal(buffer_handle_t buffer, int32_t *outFormat)
170 {
171 	if (private_handle_t::validate(buffer) < 0)
172 	{
173 		AERR("Invalid buffer %p, returning error", buffer);
174 		return GRALLOC1_ERROR_BAD_HANDLE;
175 	}
176 
177 	private_handle_t *hnd = (private_handle_t *)buffer;
178 	*outFormat = hnd->req_format;
179 	return GRALLOC1_ERROR_NONE;
180 }
181 
mali_gralloc_get_producer_usage_internal(buffer_handle_t buffer,uint64_t * outUsage)182 int mali_gralloc_get_producer_usage_internal(buffer_handle_t buffer, uint64_t *outUsage)
183 {
184 	if (private_handle_t::validate(buffer) < 0)
185 	{
186 		AERR("Invalid buffer %p, returning error", buffer);
187 		return GRALLOC1_ERROR_BAD_HANDLE;
188 	}
189 
190 	private_handle_t *hnd = (private_handle_t *)buffer;
191 	*outUsage = hnd->producer_usage;
192 	return GRALLOC1_ERROR_NONE;
193 }
194 
195 #endif
mali_gralloc_query_getstride(buffer_handle_t buffer,int * pixelStride)196 int mali_gralloc_query_getstride(buffer_handle_t buffer, int *pixelStride)
197 {
198 	int rval = -1;
199 
200 	if (buffer != NULL && pixelStride != NULL)
201 	{
202 		private_handle_t const *hnd = reinterpret_cast<private_handle_t const *>(buffer);
203 
204 		if (hnd)
205 		{
206 			*pixelStride = hnd->stride;
207 			rval = 0;
208 		}
209 	}
210 
211 	return rval;
212 }
213