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