blob: d421ff1993d1a7fff446685a2d79e8a65f886eaa [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
Naseer Ahmede69031e2016-11-22 20:05:16 -05002 * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05303
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above
10 * copyright notice, this list of conditions and the following
11 * disclaimer in the documentation and/or other materials provided
12 * with the distribution.
13 * * Neither the name of The Linux Foundation nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
Naseer Ahmed7a2b09c2017-05-11 13:03:17 -040030#define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053031#include <cutils/log.h>
Naseer Ahmed7a2b09c2017-05-11 13:03:17 -040032#include <utils/Trace.h>
33#include <cutils/trace.h>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053034#include <sync/sync.h>
Naseer Ahmeddc918132017-03-07 15:25:14 -050035#include <algorithm>
Naseer Ahmede69031e2016-11-22 20:05:16 -050036#include <sstream>
37#include <string>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053038
39#include "gr_device_impl.h"
40#include "gr_buf_descriptor.h"
41#include "gralloc_priv.h"
42#include "qd_utils.h"
43#include "qdMetaData.h"
44#include "gr_utils.h"
45
46int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device);
47
48int gralloc_device_close(struct hw_device_t *device);
49
50static struct hw_module_methods_t gralloc_module_methods = {.open = gralloc_device_open};
51
Naseer Ahmede69031e2016-11-22 20:05:16 -050052struct gralloc_module_t HAL_MODULE_INFO_SYM = {
53 .common = {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053054 .tag = HARDWARE_MODULE_TAG,
Naseer Ahmede69031e2016-11-22 20:05:16 -050055 .module_api_version = GRALLOC_MODULE_API_VERSION_1_0,
56 .hal_api_version = HARDWARE_HAL_API_VERSION,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053057 .id = GRALLOC_HARDWARE_MODULE_ID,
58 .name = "Graphics Memory Module",
59 .author = "Code Aurora Forum",
60 .methods = &gralloc_module_methods,
61 .dso = 0,
62 .reserved = {0},
63 },
64};
65
66int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device) {
67 int status = -EINVAL;
68 if (!strcmp(name, GRALLOC_HARDWARE_MODULE_ID)) {
Naseer Ahmede69031e2016-11-22 20:05:16 -050069 gralloc1::GrallocImpl * /*gralloc1_device_t*/ dev = gralloc1::GrallocImpl::GetInstance(module);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053070 *device = reinterpret_cast<hw_device_t *>(dev);
Naseer Ahmed7df1e402017-03-16 15:13:34 -040071 if (dev) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053072 status = 0;
73 } else {
Naseer Ahmed7df1e402017-03-16 15:13:34 -040074 ALOGE("Fatal error opening gralloc1 device");
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053075 }
76 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053077 return status;
78}
79
80namespace gralloc1 {
81
Naseer Ahmede69031e2016-11-22 20:05:16 -050082GrallocImpl::GrallocImpl(const hw_module_t *module) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053083 common.tag = HARDWARE_DEVICE_TAG;
Naseer Ahmede69031e2016-11-22 20:05:16 -050084 common.version = GRALLOC_MODULE_API_VERSION_1_0;
85 common.module = const_cast<hw_module_t *>(module);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053086 common.close = CloseDevice;
87 getFunction = GetFunction;
88 getCapabilities = GetCapabilities;
Naseer Ahmed7df1e402017-03-16 15:13:34 -040089
90 initalized_ = Init();
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053091}
92
93bool GrallocImpl::Init() {
Naseer Ahmede69031e2016-11-22 20:05:16 -050094 buf_mgr_ = BufferManager::GetInstance();
Naseer Ahmed7df1e402017-03-16 15:13:34 -040095 return buf_mgr_ != nullptr;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053096}
97
98GrallocImpl::~GrallocImpl() {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053099}
100
Naseer Ahmed7df1e402017-03-16 15:13:34 -0400101int GrallocImpl::CloseDevice(hw_device_t *device __unused) {
102 // No-op since the gralloc device is a singleton
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530103 return 0;
104}
105
106void GrallocImpl::GetCapabilities(struct gralloc1_device *device, uint32_t *out_count,
Naseer Ahmed8f9c7c32017-02-21 16:45:14 -0500107 int32_t /*gralloc1_capability_t*/ *out_capabilities) {
108 if (device != nullptr) {
Naseer Ahmed67330702017-05-02 15:00:26 -0400109 if (out_capabilities != nullptr && *out_count >= 3) {
Naseer Ahmed8f9c7c32017-02-21 16:45:14 -0500110 out_capabilities[0] = GRALLOC1_CAPABILITY_TEST_ALLOCATE;
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400111 out_capabilities[1] = GRALLOC1_CAPABILITY_LAYERED_BUFFERS;
Naseer Ahmed67330702017-05-02 15:00:26 -0400112 out_capabilities[2] = GRALLOC1_CAPABILITY_RELEASE_IMPLY_DELETE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530113 }
Naseer Ahmed67330702017-05-02 15:00:26 -0400114 *out_count = 3;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530115 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530116 return;
117}
118
119gralloc1_function_pointer_t GrallocImpl::GetFunction(gralloc1_device_t *device, int32_t function) {
120 if (!device) {
121 return NULL;
122 }
123
124 switch (function) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500125 case GRALLOC1_FUNCTION_DUMP:
126 return reinterpret_cast<gralloc1_function_pointer_t>(Dump);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530127 case GRALLOC1_FUNCTION_CREATE_DESCRIPTOR:
128 return reinterpret_cast<gralloc1_function_pointer_t>(CreateBufferDescriptor);
129 case GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR:
130 return reinterpret_cast<gralloc1_function_pointer_t>(DestroyBufferDescriptor);
131 case GRALLOC1_FUNCTION_SET_CONSUMER_USAGE:
132 return reinterpret_cast<gralloc1_function_pointer_t>(SetConsumerUsage);
133 case GRALLOC1_FUNCTION_SET_DIMENSIONS:
134 return reinterpret_cast<gralloc1_function_pointer_t>(SetBufferDimensions);
135 case GRALLOC1_FUNCTION_SET_FORMAT:
136 return reinterpret_cast<gralloc1_function_pointer_t>(SetColorFormat);
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400137 case GRALLOC1_FUNCTION_SET_LAYER_COUNT:
138 return reinterpret_cast<gralloc1_function_pointer_t>(SetLayerCount);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530139 case GRALLOC1_FUNCTION_SET_PRODUCER_USAGE:
140 return reinterpret_cast<gralloc1_function_pointer_t>(SetProducerUsage);
141 case GRALLOC1_FUNCTION_GET_BACKING_STORE:
142 return reinterpret_cast<gralloc1_function_pointer_t>(GetBackingStore);
143 case GRALLOC1_FUNCTION_GET_CONSUMER_USAGE:
144 return reinterpret_cast<gralloc1_function_pointer_t>(GetConsumerUsage);
145 case GRALLOC1_FUNCTION_GET_DIMENSIONS:
146 return reinterpret_cast<gralloc1_function_pointer_t>(GetBufferDimensions);
147 case GRALLOC1_FUNCTION_GET_FORMAT:
148 return reinterpret_cast<gralloc1_function_pointer_t>(GetColorFormat);
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400149 case GRALLOC1_FUNCTION_GET_LAYER_COUNT:
150 return reinterpret_cast<gralloc1_function_pointer_t>(GetLayerCount);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530151 case GRALLOC1_FUNCTION_GET_PRODUCER_USAGE:
152 return reinterpret_cast<gralloc1_function_pointer_t>(GetProducerUsage);
153 case GRALLOC1_FUNCTION_GET_STRIDE:
154 return reinterpret_cast<gralloc1_function_pointer_t>(GetBufferStride);
155 case GRALLOC1_FUNCTION_ALLOCATE:
156 return reinterpret_cast<gralloc1_function_pointer_t>(AllocateBuffers);
157 case GRALLOC1_FUNCTION_RETAIN:
158 return reinterpret_cast<gralloc1_function_pointer_t>(RetainBuffer);
159 case GRALLOC1_FUNCTION_RELEASE:
160 return reinterpret_cast<gralloc1_function_pointer_t>(ReleaseBuffer);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500161 case GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES:
162 return reinterpret_cast<gralloc1_function_pointer_t>(GetNumFlexPlanes);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530163 case GRALLOC1_FUNCTION_LOCK:
164 return reinterpret_cast<gralloc1_function_pointer_t>(LockBuffer);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500165 case GRALLOC1_FUNCTION_LOCK_FLEX:
166 return reinterpret_cast<gralloc1_function_pointer_t>(LockFlex);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530167 case GRALLOC1_FUNCTION_UNLOCK:
168 return reinterpret_cast<gralloc1_function_pointer_t>(UnlockBuffer);
169 case GRALLOC1_FUNCTION_PERFORM:
170 return reinterpret_cast<gralloc1_function_pointer_t>(Gralloc1Perform);
171 default:
172 ALOGE("%s:Gralloc Error. Client Requested for unsupported function", __FUNCTION__);
173 return NULL;
174 }
175
176 return NULL;
177}
178
Naseer Ahmede69031e2016-11-22 20:05:16 -0500179gralloc1_error_t GrallocImpl::Dump(gralloc1_device_t *device, uint32_t *out_size,
180 char *out_buffer) {
181 if (!device) {
182 ALOGE("Gralloc Error : device=%p", (void *)device);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530183 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
184 }
Naseer Ahmeddc918132017-03-07 15:25:14 -0500185 const size_t max_dump_size = 8192;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500186 if (out_buffer == nullptr) {
Naseer Ahmeddc918132017-03-07 15:25:14 -0500187 *out_size = max_dump_size;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500188 } else {
189 std::ostringstream os;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500190 os << "-------------------------------" << std::endl;
191 os << "QTI gralloc dump:" << std::endl;
192 os << "-------------------------------" << std::endl;
Naseer Ahmeddc918132017-03-07 15:25:14 -0500193 GrallocImpl const *dev = GRALLOC_IMPL(device);
194 dev->buf_mgr_->Dump(&os);
195 os << "-------------------------------" << std::endl;
196 auto copied = os.str().copy(out_buffer, std::min(os.str().size(), max_dump_size), 0);
197 *out_size = UINT(copied);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500198 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530199
200 return GRALLOC1_ERROR_NONE;
201}
202
203gralloc1_error_t GrallocImpl::CheckDeviceAndHandle(gralloc1_device_t *device,
204 buffer_handle_t buffer) {
205 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
206 if (!device || (private_handle_t::validate(hnd) != 0)) {
207 ALOGE("Gralloc Error : device= %p, buffer-handle=%p", (void *)device, (void *)buffer);
208 return GRALLOC1_ERROR_BAD_HANDLE;
209 }
210
211 return GRALLOC1_ERROR_NONE;
212}
213
214gralloc1_error_t GrallocImpl::CreateBufferDescriptor(gralloc1_device_t *device,
215 gralloc1_buffer_descriptor_t *out_descriptor) {
216 if (!device) {
217 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
218 }
Naseer Ahmede69031e2016-11-22 20:05:16 -0500219 GrallocImpl const *dev = GRALLOC_IMPL(device);
220 return dev->buf_mgr_->CreateBufferDescriptor(out_descriptor);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530221}
222
223gralloc1_error_t GrallocImpl::DestroyBufferDescriptor(gralloc1_device_t *device,
224 gralloc1_buffer_descriptor_t descriptor) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500225 if (!device) {
226 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530227 }
Naseer Ahmede69031e2016-11-22 20:05:16 -0500228 GrallocImpl const *dev = GRALLOC_IMPL(device);
229 return dev->buf_mgr_->DestroyBufferDescriptor(descriptor);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530230}
231
232gralloc1_error_t GrallocImpl::SetConsumerUsage(gralloc1_device_t *device,
233 gralloc1_buffer_descriptor_t descriptor,
234 gralloc1_consumer_usage_t usage) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500235 if (!device) {
236 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
237 } else {
238 GrallocImpl const *dev = GRALLOC_IMPL(device);
239 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
240 &BufferDescriptor::SetConsumerUsage, usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530241 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530242}
243
244gralloc1_error_t GrallocImpl::SetBufferDimensions(gralloc1_device_t *device,
245 gralloc1_buffer_descriptor_t descriptor,
246 uint32_t width, uint32_t height) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500247 if (!device) {
248 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
249 } else {
250 GrallocImpl const *dev = GRALLOC_IMPL(device);
251 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
252 &BufferDescriptor::SetDimensions,
253 INT(width), INT(height));
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530254 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530255}
256
257gralloc1_error_t GrallocImpl::SetColorFormat(gralloc1_device_t *device,
258 gralloc1_buffer_descriptor_t descriptor,
259 int32_t format) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500260 if (!device) {
261 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
262 } else {
263 GrallocImpl const *dev = GRALLOC_IMPL(device);
264 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
265 &BufferDescriptor::SetColorFormat, format);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530266 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530267}
268
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400269gralloc1_error_t GrallocImpl::SetLayerCount(gralloc1_device_t *device,
270 gralloc1_buffer_descriptor_t descriptor,
271 uint32_t layer_count) {
272 if (!device) {
273 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
274 } else {
275 GrallocImpl const *dev = GRALLOC_IMPL(device);
276 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
277 &BufferDescriptor::SetLayerCount,
278 layer_count);
279 }
280}
281
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530282gralloc1_error_t GrallocImpl::SetProducerUsage(gralloc1_device_t *device,
283 gralloc1_buffer_descriptor_t descriptor,
284 gralloc1_producer_usage_t usage) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500285 if (!device) {
286 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
287 } else {
288 GrallocImpl const *dev = GRALLOC_IMPL(device);
289 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
290 &BufferDescriptor::SetProducerUsage, usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530291 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530292}
293
294gralloc1_error_t GrallocImpl::GetBackingStore(gralloc1_device_t *device, buffer_handle_t buffer,
295 gralloc1_backing_store_t *out_backstore) {
296 if (!device || !buffer) {
297 return GRALLOC1_ERROR_BAD_HANDLE;
298 }
299
300 *out_backstore =
301 static_cast<gralloc1_backing_store_t>(PRIV_HANDLE_CONST(buffer)->GetBackingstore());
302
303 return GRALLOC1_ERROR_NONE;
304}
305
306gralloc1_error_t GrallocImpl::GetConsumerUsage(gralloc1_device_t *device, buffer_handle_t buffer,
307 gralloc1_consumer_usage_t *outUsage) {
308 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
309 if (status == GRALLOC1_ERROR_NONE) {
310 *outUsage = PRIV_HANDLE_CONST(buffer)->GetConsumerUsage();
311 }
312
313 return status;
314}
315
316gralloc1_error_t GrallocImpl::GetBufferDimensions(gralloc1_device_t *device, buffer_handle_t buffer,
317 uint32_t *outWidth, uint32_t *outHeight) {
318 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
319 if (status == GRALLOC1_ERROR_NONE) {
320 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700321 *outWidth = UINT(hnd->GetUnalignedWidth());
322 *outHeight = UINT(hnd->GetUnalignedHeight());
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530323 }
324
325 return status;
326}
327
328gralloc1_error_t GrallocImpl::GetColorFormat(gralloc1_device_t *device, buffer_handle_t buffer,
329 int32_t *outFormat) {
330 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
331 if (status == GRALLOC1_ERROR_NONE) {
332 *outFormat = PRIV_HANDLE_CONST(buffer)->GetColorFormat();
333 }
334
335 return status;
336}
337
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400338gralloc1_error_t GrallocImpl::GetLayerCount(gralloc1_device_t *device, buffer_handle_t buffer,
339 uint32_t *outLayerCount) {
340 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
341 if (status == GRALLOC1_ERROR_NONE) {
342 *outLayerCount = PRIV_HANDLE_CONST(buffer)->GetLayerCount();
343 }
344
345 return status;
346}
347
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530348gralloc1_error_t GrallocImpl::GetProducerUsage(gralloc1_device_t *device, buffer_handle_t buffer,
349 gralloc1_producer_usage_t *outUsage) {
Saurabh Shah6e4b3762017-09-21 15:30:15 -0700350 if (!outUsage) {
351 return GRALLOC1_ERROR_BAD_VALUE;
352 }
353
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530354 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
355 if (status == GRALLOC1_ERROR_NONE) {
356 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
357 *outUsage = hnd->GetProducerUsage();
358 }
359
360 return status;
361}
362
363gralloc1_error_t GrallocImpl::GetBufferStride(gralloc1_device_t *device, buffer_handle_t buffer,
364 uint32_t *outStride) {
Saurabh Shah6e4b3762017-09-21 15:30:15 -0700365 if (!outStride) {
366 return GRALLOC1_ERROR_BAD_VALUE;
367 }
368
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530369 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
370 if (status == GRALLOC1_ERROR_NONE) {
371 *outStride = UINT(PRIV_HANDLE_CONST(buffer)->GetStride());
372 }
373
374 return status;
375}
376
Naseer Ahmede69031e2016-11-22 20:05:16 -0500377gralloc1_error_t GrallocImpl::AllocateBuffers(gralloc1_device_t *device, uint32_t num_descriptors,
378 const gralloc1_buffer_descriptor_t *descriptors,
379 buffer_handle_t *out_buffers) {
380 if (!num_descriptors || !descriptors) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530381 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
382 }
383
Saurabh Shah6e4b3762017-09-21 15:30:15 -0700384 if (!device) {
385 return GRALLOC1_ERROR_BAD_VALUE;
386 }
387
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530388 GrallocImpl const *dev = GRALLOC_IMPL(device);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500389 gralloc1_error_t status = dev->buf_mgr_->AllocateBuffers(num_descriptors, descriptors,
390 out_buffers);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530391
392 return status;
393}
394
395gralloc1_error_t GrallocImpl::RetainBuffer(gralloc1_device_t *device, buffer_handle_t buffer) {
396 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
397 if (status == GRALLOC1_ERROR_NONE) {
398 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
399 GrallocImpl const *dev = GRALLOC_IMPL(device);
400 status = dev->buf_mgr_->RetainBuffer(hnd);
401 }
402
403 return status;
404}
405
406gralloc1_error_t GrallocImpl::ReleaseBuffer(gralloc1_device_t *device, buffer_handle_t buffer) {
Naseer Ahmeda186b472017-07-07 18:50:49 -0400407 if (!device || !buffer) {
408 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530409 }
410
Naseer Ahmeda186b472017-07-07 18:50:49 -0400411 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
412 GrallocImpl const *dev = GRALLOC_IMPL(device);
413 return dev->buf_mgr_->ReleaseBuffer(hnd);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530414}
415
Naseer Ahmede69031e2016-11-22 20:05:16 -0500416gralloc1_error_t GrallocImpl::GetNumFlexPlanes(gralloc1_device_t *device, buffer_handle_t buffer,
417 uint32_t *out_num_planes) {
Saurabh Shah6e4b3762017-09-21 15:30:15 -0700418 if (!out_num_planes) {
419 return GRALLOC1_ERROR_BAD_VALUE;
420 }
421
Naseer Ahmede69031e2016-11-22 20:05:16 -0500422 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
423 if (status == GRALLOC1_ERROR_NONE) {
424 GrallocImpl const *dev = GRALLOC_IMPL(device);
425 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
426 status = dev->buf_mgr_->GetNumFlexPlanes(hnd, out_num_planes);
427 }
428 return status;
429}
430
Naseer Ahmed86514172017-03-29 10:07:43 -0400431static inline void CloseFdIfValid(int fd) {
432 if (fd > 0) {
433 close(fd);
434 }
435}
436
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530437gralloc1_error_t GrallocImpl::LockBuffer(gralloc1_device_t *device, buffer_handle_t buffer,
438 gralloc1_producer_usage_t prod_usage,
439 gralloc1_consumer_usage_t cons_usage,
440 const gralloc1_rect_t *region, void **out_data,
441 int32_t acquire_fence) {
Naseer Ahmed7a2b09c2017-05-11 13:03:17 -0400442 ATRACE_CALL();
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530443 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
Saurabh Shah6e4b3762017-09-21 15:30:15 -0700444 if (status != GRALLOC1_ERROR_NONE || !out_data ||
445 !region) { // currently we ignore the region/rect client wants to lock
Naseer Ahmed86514172017-03-29 10:07:43 -0400446 CloseFdIfValid(acquire_fence);
447 return status;
448 }
449
450 if (acquire_fence > 0) {
Naseer Ahmed7a2b09c2017-05-11 13:03:17 -0400451 ATRACE_BEGIN("fence wait");
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530452 int error = sync_wait(acquire_fence, 1000);
Naseer Ahmed7a2b09c2017-05-11 13:03:17 -0400453 ATRACE_END();
Naseer Ahmed86514172017-03-29 10:07:43 -0400454 CloseFdIfValid(acquire_fence);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530455 if (error < 0) {
456 ALOGE("%s: sync_wait timedout! error = %s", __FUNCTION__, strerror(errno));
457 return GRALLOC1_ERROR_UNDEFINED;
458 }
459 }
460
461 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
462 GrallocImpl const *dev = GRALLOC_IMPL(device);
463
464 // Either producer usage or consumer usage must be *_USAGE_NONE
465 if ((prod_usage != GRALLOC1_PRODUCER_USAGE_NONE) &&
466 (cons_usage != GRALLOC1_CONSUMER_USAGE_NONE)) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500467 // Current gralloc1 clients do not satisfy this restriction.
468 // See b/33588773 for details
469 // return GRALLOC1_ERROR_BAD_VALUE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530470 }
471
Naseer Ahmede69031e2016-11-22 20:05:16 -0500472 // TODO(user): Need to check if buffer was allocated with the same flags
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530473 status = dev->buf_mgr_->LockBuffer(hnd, prod_usage, cons_usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530474 *out_data = reinterpret_cast<void *>(hnd->base);
475
476 return status;
477}
478
Naseer Ahmede69031e2016-11-22 20:05:16 -0500479gralloc1_error_t GrallocImpl::LockFlex(gralloc1_device_t *device, buffer_handle_t buffer,
480 gralloc1_producer_usage_t prod_usage,
481 gralloc1_consumer_usage_t cons_usage,
482 const gralloc1_rect_t *region,
483 struct android_flex_layout *out_flex_layout,
484 int32_t acquire_fence) {
Saurabh Shah6e4b3762017-09-21 15:30:15 -0700485 if (!out_flex_layout) {
486 CloseFdIfValid(acquire_fence);
487 return GRALLOC1_ERROR_BAD_VALUE;
488 }
489
490 void *out_data {};
Naseer Ahmede69031e2016-11-22 20:05:16 -0500491 gralloc1_error_t status = GrallocImpl::LockBuffer(device, buffer, prod_usage, cons_usage, region,
492 &out_data, acquire_fence);
493 if (status != GRALLOC1_ERROR_NONE) {
494 return status;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530495 }
496
Naseer Ahmede69031e2016-11-22 20:05:16 -0500497 GrallocImpl const *dev = GRALLOC_IMPL(device);
498 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
499 dev->buf_mgr_->GetFlexLayout(hnd, out_flex_layout);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530500 return status;
501}
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530502
503gralloc1_error_t GrallocImpl::UnlockBuffer(gralloc1_device_t *device, buffer_handle_t buffer,
504 int32_t *release_fence) {
505 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530506 if (status != GRALLOC1_ERROR_NONE) {
507 return status;
508 }
509
Saurabh Shah6e4b3762017-09-21 15:30:15 -0700510 if (!release_fence) {
511 return GRALLOC1_ERROR_BAD_VALUE;
512 }
513
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530514 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
515 GrallocImpl const *dev = GRALLOC_IMPL(device);
516
517 *release_fence = -1;
518
519 return dev->buf_mgr_->UnlockBuffer(hnd);
520}
521
522gralloc1_error_t GrallocImpl::Gralloc1Perform(gralloc1_device_t *device, int operation, ...) {
Saurabh Shah6e4b3762017-09-21 15:30:15 -0700523 if (!device) {
524 return GRALLOC1_ERROR_BAD_VALUE;
525 }
526
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530527 va_list args;
528 va_start(args, operation);
529 GrallocImpl const *dev = GRALLOC_IMPL(device);
530 gralloc1_error_t err = dev->buf_mgr_->Perform(operation, args);
531 va_end(args);
532
533 return err;
534}
535
536} // namespace gralloc1