blob: f27a7b07a01cd6ada014f945dfb1904d852fd3b8 [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
Mahesh Aiabb828122019-01-16 13:07:42 -08002 * Copyright (c) 2016-2019, 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)
Naseer Ahmedfcad05e2018-03-06 20:41:14 -050031#include <log/log.h>
Naseer Ahmed7a2b09c2017-05-11 13:03:17 -040032#include <cutils/trace.h>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053033#include <sync/sync.h>
Naseer Ahmede36f2242017-12-01 15:33:56 -050034#include <utils/Trace.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
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053039#include "gr_buf_descriptor.h"
Naseer Ahmede36f2242017-12-01 15:33:56 -050040#include "gr_device_impl.h"
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053041#include "gr_utils.h"
Naseer Ahmede36f2242017-12-01 15:33:56 -050042#include "gralloc_priv.h"
43#include "qdMetaData.h"
44#include "qd_utils.h"
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053045
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 = {
Naseer Ahmede36f2242017-12-01 15:33:56 -050053 // clang-format off
54 .common = {
55 .tag = HARDWARE_MODULE_TAG,
56 .module_api_version = GRALLOC_MODULE_API_VERSION_1_0,
57 .hal_api_version = HARDWARE_HAL_API_VERSION,
58 .id = GRALLOC_HARDWARE_MODULE_ID,
59 .name = "Graphics Memory Module",
60 .author = "Code Aurora Forum",
61 .methods = &gralloc_module_methods,
62 .dso = 0,
63 .reserved = {0},
64 },
65 // clang-format on
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053066};
67
68int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device) {
69 int status = -EINVAL;
Rohit Kulkarni511602d2017-09-26 16:56:41 -070070 if (module && device && !strcmp(name, GRALLOC_HARDWARE_MODULE_ID)) {
Naseer Ahmede36f2242017-12-01 15:33:56 -050071 gralloc::GrallocImpl * /*gralloc1_device_t*/ dev = gralloc::GrallocImpl::GetInstance(module);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053072 *device = reinterpret_cast<hw_device_t *>(dev);
Naseer Ahmed7df1e402017-03-16 15:13:34 -040073 if (dev) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053074 status = 0;
75 } else {
Naseer Ahmed7df1e402017-03-16 15:13:34 -040076 ALOGE("Fatal error opening gralloc1 device");
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053077 }
78 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053079 return status;
80}
81
Naseer Ahmede36f2242017-12-01 15:33:56 -050082namespace gralloc {
83
84std::atomic<uint64_t> GrallocImpl::next_descriptor_id_(1);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053085
Naseer Ahmede69031e2016-11-22 20:05:16 -050086GrallocImpl::GrallocImpl(const hw_module_t *module) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053087 common.tag = HARDWARE_DEVICE_TAG;
Naseer Ahmede69031e2016-11-22 20:05:16 -050088 common.version = GRALLOC_MODULE_API_VERSION_1_0;
89 common.module = const_cast<hw_module_t *>(module);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053090 common.close = CloseDevice;
91 getFunction = GetFunction;
92 getCapabilities = GetCapabilities;
Naseer Ahmed7df1e402017-03-16 15:13:34 -040093
Naseer Ahmede36f2242017-12-01 15:33:56 -050094 initialized_ = Init();
95}
96
97inline gralloc1_error_t ToError(Error error) {
98 switch (error) {
99 case Error::NONE:
100 return GRALLOC1_ERROR_NONE;
101 case Error::BAD_DESCRIPTOR:
102 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
103 case Error::BAD_BUFFER:
104 return GRALLOC1_ERROR_BAD_HANDLE;
105 case Error::BAD_VALUE:
106 return GRALLOC1_ERROR_BAD_VALUE;
107 case Error::NO_RESOURCES:
108 return GRALLOC1_ERROR_NO_RESOURCES;
109 case Error::UNSUPPORTED:
110 default:
111 return GRALLOC1_ERROR_UNSUPPORTED;
112 }
113}
114
Mahesh Aiab9325182019-07-09 10:27:55 -0700115static uint64_t ProducerUsageToBufferUsage(uint64_t /*gralloc1_producer_usage_t*/ producer_usage) {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500116 uint64_t usage = producer_usage & ~(GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN |
117 GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN);
118 if ((producer_usage & GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN) ==
119 GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN) {
120 usage |= BufferUsage::CPU_READ_OFTEN;
121 } else if ((producer_usage & GRALLOC1_PRODUCER_USAGE_CPU_READ) ==
122 GRALLOC1_PRODUCER_USAGE_CPU_READ) {
123 usage |= BufferUsage::CPU_READ_RARELY;
124 }
125
126 if ((producer_usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN) ==
127 GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN) {
128 usage |= BufferUsage::CPU_WRITE_OFTEN;
129 } else if ((producer_usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE) ==
130 GRALLOC1_PRODUCER_USAGE_CPU_WRITE) {
131 usage |= BufferUsage::CPU_WRITE_RARELY;
132 }
133 return usage;
134}
135
Mahesh Aiab9325182019-07-09 10:27:55 -0700136static uint64_t ConsumerUsageToBufferUsage(uint64_t /*gralloc1_consumer_usage_t*/ consumer_usage) {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500137 uint64_t usage = consumer_usage & ~(GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN);
138 if ((consumer_usage & GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) ==
139 GRALLOC1_CONSUMER_USAGE_CPU_READ_OFTEN) {
140 usage |= BufferUsage::CPU_READ_OFTEN;
141 } else if ((consumer_usage & GRALLOC1_CONSUMER_USAGE_CPU_READ) ==
142 GRALLOC1_CONSUMER_USAGE_CPU_READ) {
143 usage |= BufferUsage::CPU_READ_RARELY;
144 }
145 return usage;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530146}
147
148bool GrallocImpl::Init() {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500149 buf_mgr_ = BufferManager::GetInstance();
Naseer Ahmed7df1e402017-03-16 15:13:34 -0400150 return buf_mgr_ != nullptr;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530151}
152
Naseer Ahmede36f2242017-12-01 15:33:56 -0500153GrallocImpl::~GrallocImpl() {}
154
155gralloc1_error_t GrallocImpl::CreateBufferDescriptorLocked(
156 gralloc1_buffer_descriptor_t *descriptor_id) {
157 std::lock_guard<std::mutex> lock(descriptor_lock_);
158 auto descriptor = std::make_shared<BufferDescriptor>(next_descriptor_id_++);
159 *descriptor_id = static_cast<gralloc1_buffer_descriptor_t>(descriptor->GetId());
160 descriptors_map_.emplace(*descriptor_id, descriptor);
161 return GRALLOC1_ERROR_NONE;
162}
163
164gralloc1_error_t GrallocImpl::DestroyBufferDescriptorLocked(
165 gralloc1_buffer_descriptor_t descriptor_id) {
166 std::lock_guard<std::mutex> lock(descriptor_lock_);
167 const auto descriptor = descriptors_map_.find(descriptor_id);
168 if (descriptor == descriptors_map_.end()) {
169 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
170 }
171 descriptors_map_.erase(descriptor);
172 return GRALLOC1_ERROR_NONE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530173}
174
Naseer Ahmed7df1e402017-03-16 15:13:34 -0400175int GrallocImpl::CloseDevice(hw_device_t *device __unused) {
176 // No-op since the gralloc device is a singleton
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530177 return 0;
178}
179
180void GrallocImpl::GetCapabilities(struct gralloc1_device *device, uint32_t *out_count,
Naseer Ahmede36f2242017-12-01 15:33:56 -0500181 int32_t /*gralloc1_capability_t*/ *out_capabilities) {
Rohit Kulkarni511602d2017-09-26 16:56:41 -0700182 if (device != nullptr && out_count != nullptr) {
Naseer Ahmed67330702017-05-02 15:00:26 -0400183 if (out_capabilities != nullptr && *out_count >= 3) {
Naseer Ahmed8f9c7c32017-02-21 16:45:14 -0500184 out_capabilities[0] = GRALLOC1_CAPABILITY_TEST_ALLOCATE;
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400185 out_capabilities[1] = GRALLOC1_CAPABILITY_LAYERED_BUFFERS;
Naseer Ahmed67330702017-05-02 15:00:26 -0400186 out_capabilities[2] = GRALLOC1_CAPABILITY_RELEASE_IMPLY_DELETE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530187 }
Naseer Ahmed67330702017-05-02 15:00:26 -0400188 *out_count = 3;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530189 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530190 return;
191}
192
193gralloc1_function_pointer_t GrallocImpl::GetFunction(gralloc1_device_t *device, int32_t function) {
194 if (!device) {
195 return NULL;
196 }
197
198 switch (function) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500199 case GRALLOC1_FUNCTION_DUMP:
200 return reinterpret_cast<gralloc1_function_pointer_t>(Dump);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530201 case GRALLOC1_FUNCTION_CREATE_DESCRIPTOR:
202 return reinterpret_cast<gralloc1_function_pointer_t>(CreateBufferDescriptor);
203 case GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR:
204 return reinterpret_cast<gralloc1_function_pointer_t>(DestroyBufferDescriptor);
205 case GRALLOC1_FUNCTION_SET_CONSUMER_USAGE:
206 return reinterpret_cast<gralloc1_function_pointer_t>(SetConsumerUsage);
207 case GRALLOC1_FUNCTION_SET_DIMENSIONS:
208 return reinterpret_cast<gralloc1_function_pointer_t>(SetBufferDimensions);
209 case GRALLOC1_FUNCTION_SET_FORMAT:
210 return reinterpret_cast<gralloc1_function_pointer_t>(SetColorFormat);
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400211 case GRALLOC1_FUNCTION_SET_LAYER_COUNT:
212 return reinterpret_cast<gralloc1_function_pointer_t>(SetLayerCount);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530213 case GRALLOC1_FUNCTION_SET_PRODUCER_USAGE:
214 return reinterpret_cast<gralloc1_function_pointer_t>(SetProducerUsage);
215 case GRALLOC1_FUNCTION_GET_BACKING_STORE:
216 return reinterpret_cast<gralloc1_function_pointer_t>(GetBackingStore);
217 case GRALLOC1_FUNCTION_GET_CONSUMER_USAGE:
218 return reinterpret_cast<gralloc1_function_pointer_t>(GetConsumerUsage);
219 case GRALLOC1_FUNCTION_GET_DIMENSIONS:
220 return reinterpret_cast<gralloc1_function_pointer_t>(GetBufferDimensions);
221 case GRALLOC1_FUNCTION_GET_FORMAT:
222 return reinterpret_cast<gralloc1_function_pointer_t>(GetColorFormat);
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400223 case GRALLOC1_FUNCTION_GET_LAYER_COUNT:
224 return reinterpret_cast<gralloc1_function_pointer_t>(GetLayerCount);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530225 case GRALLOC1_FUNCTION_GET_PRODUCER_USAGE:
226 return reinterpret_cast<gralloc1_function_pointer_t>(GetProducerUsage);
227 case GRALLOC1_FUNCTION_GET_STRIDE:
228 return reinterpret_cast<gralloc1_function_pointer_t>(GetBufferStride);
229 case GRALLOC1_FUNCTION_ALLOCATE:
230 return reinterpret_cast<gralloc1_function_pointer_t>(AllocateBuffers);
231 case GRALLOC1_FUNCTION_RETAIN:
232 return reinterpret_cast<gralloc1_function_pointer_t>(RetainBuffer);
233 case GRALLOC1_FUNCTION_RELEASE:
234 return reinterpret_cast<gralloc1_function_pointer_t>(ReleaseBuffer);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500235 case GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES:
236 return reinterpret_cast<gralloc1_function_pointer_t>(GetNumFlexPlanes);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530237 case GRALLOC1_FUNCTION_LOCK:
238 return reinterpret_cast<gralloc1_function_pointer_t>(LockBuffer);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500239 case GRALLOC1_FUNCTION_LOCK_FLEX:
240 return reinterpret_cast<gralloc1_function_pointer_t>(LockFlex);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530241 case GRALLOC1_FUNCTION_UNLOCK:
242 return reinterpret_cast<gralloc1_function_pointer_t>(UnlockBuffer);
243 case GRALLOC1_FUNCTION_PERFORM:
244 return reinterpret_cast<gralloc1_function_pointer_t>(Gralloc1Perform);
245 default:
246 ALOGE("%s:Gralloc Error. Client Requested for unsupported function", __FUNCTION__);
247 return NULL;
248 }
249
250 return NULL;
251}
252
Mahesh Aiabb828122019-01-16 13:07:42 -0800253void GrallocImpl::Dump(gralloc1_device_t *device, uint32_t *out_size,
Naseer Ahmede69031e2016-11-22 20:05:16 -0500254 char *out_buffer) {
Rohit Kulkarni511602d2017-09-26 16:56:41 -0700255 if (!device || !out_size) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500256 ALOGE("Gralloc Error : device=%p", (void *)device);
Mahesh Aiabb828122019-01-16 13:07:42 -0800257 return;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530258 }
Naseer Ahmeddc918132017-03-07 15:25:14 -0500259 const size_t max_dump_size = 8192;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500260 if (out_buffer == nullptr) {
Naseer Ahmeddc918132017-03-07 15:25:14 -0500261 *out_size = max_dump_size;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500262 } else {
263 std::ostringstream os;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500264 os << "-------------------------------" << std::endl;
265 os << "QTI gralloc dump:" << std::endl;
266 os << "-------------------------------" << std::endl;
Naseer Ahmeddc918132017-03-07 15:25:14 -0500267 GrallocImpl const *dev = GRALLOC_IMPL(device);
268 dev->buf_mgr_->Dump(&os);
269 os << "-------------------------------" << std::endl;
270 auto copied = os.str().copy(out_buffer, std::min(os.str().size(), max_dump_size), 0);
271 *out_size = UINT(copied);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500272 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530273
Mahesh Aiabb828122019-01-16 13:07:42 -0800274 return;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530275}
276
277gralloc1_error_t GrallocImpl::CheckDeviceAndHandle(gralloc1_device_t *device,
278 buffer_handle_t buffer) {
279 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
280 if (!device || (private_handle_t::validate(hnd) != 0)) {
281 ALOGE("Gralloc Error : device= %p, buffer-handle=%p", (void *)device, (void *)buffer);
282 return GRALLOC1_ERROR_BAD_HANDLE;
283 }
284
285 return GRALLOC1_ERROR_NONE;
286}
287
Mahesh Aiabb828122019-01-16 13:07:42 -0800288int32_t GrallocImpl::CreateBufferDescriptor(gralloc1_device_t *device,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530289 gralloc1_buffer_descriptor_t *out_descriptor) {
Rohit Kulkarni511602d2017-09-26 16:56:41 -0700290 if (!device || !out_descriptor) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530291 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
292 }
Naseer Ahmede36f2242017-12-01 15:33:56 -0500293 auto *dev = reinterpret_cast<GrallocImpl *>(device);
Mahesh Aiabb828122019-01-16 13:07:42 -0800294 return static_cast<int32_t>(dev->CreateBufferDescriptorLocked(out_descriptor));
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530295}
296
Mahesh Aiabb828122019-01-16 13:07:42 -0800297int32_t GrallocImpl::DestroyBufferDescriptor(gralloc1_device_t *device,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530298 gralloc1_buffer_descriptor_t descriptor) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500299 if (!device) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800300 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_DESCRIPTOR);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530301 }
Naseer Ahmede36f2242017-12-01 15:33:56 -0500302 auto *dev = reinterpret_cast<GrallocImpl *>(device);
Mahesh Aiabb828122019-01-16 13:07:42 -0800303 return static_cast<int32_t>(dev->DestroyBufferDescriptorLocked(descriptor));
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530304}
305
Mahesh Aiabb828122019-01-16 13:07:42 -0800306int32_t GrallocImpl::SetConsumerUsage(gralloc1_device_t *device,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530307 gralloc1_buffer_descriptor_t descriptor,
Mahesh Aiab9325182019-07-09 10:27:55 -0700308 uint64_t /*gralloc1_consumer_usage_t*/ usage) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500309 if (!device) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800310 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_DESCRIPTOR);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500311 } else {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500312 auto *dev = reinterpret_cast<GrallocImpl *>(device);
Mahesh Aiabb828122019-01-16 13:07:42 -0800313 return static_cast<int32_t>(dev->CallBufferDescriptorFunction(descriptor,
314 &BufferDescriptor::SetUsage,
315 ConsumerUsageToBufferUsage(usage)));
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530316 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530317}
318
Mahesh Aiabb828122019-01-16 13:07:42 -0800319int32_t GrallocImpl::SetBufferDimensions(gralloc1_device_t *device,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530320 gralloc1_buffer_descriptor_t descriptor,
321 uint32_t width, uint32_t height) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500322 if (!device) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800323 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_DESCRIPTOR);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500324 } else {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500325 auto *dev = reinterpret_cast<GrallocImpl *>(device);
Mahesh Aiabb828122019-01-16 13:07:42 -0800326 return static_cast<int32_t>(dev->CallBufferDescriptorFunction(descriptor,
327 &BufferDescriptor::SetDimensions,
328 INT(width), INT(height)));
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530329 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530330}
331
Mahesh Aiabb828122019-01-16 13:07:42 -0800332int32_t GrallocImpl::SetColorFormat(gralloc1_device_t *device,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530333 gralloc1_buffer_descriptor_t descriptor,
334 int32_t format) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500335 if (!device) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800336 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_DESCRIPTOR);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500337 } else {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500338 auto *dev = reinterpret_cast<GrallocImpl *>(device);
Mahesh Aiabb828122019-01-16 13:07:42 -0800339 return static_cast<int32_t>(dev->CallBufferDescriptorFunction(descriptor,
340 &BufferDescriptor::SetColorFormat, format));
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530341 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530342}
343
Mahesh Aiabb828122019-01-16 13:07:42 -0800344int32_t GrallocImpl::SetLayerCount(gralloc1_device_t *device,
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400345 gralloc1_buffer_descriptor_t descriptor,
346 uint32_t layer_count) {
347 if (!device) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800348 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_DESCRIPTOR);
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400349 } else {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500350 auto *dev = reinterpret_cast<GrallocImpl *>(device);
Mahesh Aiabb828122019-01-16 13:07:42 -0800351 return static_cast<int32_t>(dev->CallBufferDescriptorFunction(descriptor,
352 &BufferDescriptor::SetLayerCount, layer_count));
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400353 }
354}
355
Mahesh Aiabb828122019-01-16 13:07:42 -0800356int32_t GrallocImpl::SetProducerUsage(gralloc1_device_t *device,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530357 gralloc1_buffer_descriptor_t descriptor,
Mahesh Aiab9325182019-07-09 10:27:55 -0700358 uint64_t /*gralloc1_producer_usage_t*/ usage) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500359 if (!device) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800360 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_DESCRIPTOR);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500361 } else {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500362 auto *dev = reinterpret_cast<GrallocImpl *>(device);
Mahesh Aiabb828122019-01-16 13:07:42 -0800363 return static_cast<int32_t>(dev->CallBufferDescriptorFunction(descriptor,
364 &BufferDescriptor::SetUsage, ProducerUsageToBufferUsage(usage)));
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530365 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530366}
367
Mahesh Aiabb828122019-01-16 13:07:42 -0800368int32_t GrallocImpl::GetBackingStore(gralloc1_device_t *device, buffer_handle_t buffer,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530369 gralloc1_backing_store_t *out_backstore) {
Varun Arora16911a92018-06-13 17:59:12 -0700370 if (!out_backstore) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800371 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530372 }
373
Varun Arora16911a92018-06-13 17:59:12 -0700374 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
375 if (status == GRALLOC1_ERROR_NONE) {
376 *out_backstore =
377 static_cast<gralloc1_backing_store_t>(PRIV_HANDLE_CONST(buffer)->GetBackingstore());
378 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530379
Varun Arora16911a92018-06-13 17:59:12 -0700380 return status;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530381}
382
Mahesh Aiabb828122019-01-16 13:07:42 -0800383int32_t GrallocImpl::GetConsumerUsage(gralloc1_device_t *device, buffer_handle_t buffer,
Mahesh Aiab9325182019-07-09 10:27:55 -0700384 uint64_t /*gralloc1_consumer_usage_t*/ *outUsage) {
Varun Arora16911a92018-06-13 17:59:12 -0700385 if (!outUsage) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800386 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
Varun Arora16911a92018-06-13 17:59:12 -0700387 }
388
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530389 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
390 if (status == GRALLOC1_ERROR_NONE) {
Mahesh Aiab9325182019-07-09 10:27:55 -0700391 *outUsage = static_cast<uint64_t>(PRIV_HANDLE_CONST(buffer)->GetUsage());
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530392 }
393
Mahesh Aiabb828122019-01-16 13:07:42 -0800394 return static_cast<int32_t>(status);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530395}
396
Mahesh Aiabb828122019-01-16 13:07:42 -0800397int32_t GrallocImpl::GetBufferDimensions(gralloc1_device_t *device, buffer_handle_t buffer,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530398 uint32_t *outWidth, uint32_t *outHeight) {
Varun Arora16911a92018-06-13 17:59:12 -0700399 if (!outWidth || !outHeight) {
400 return GRALLOC1_ERROR_BAD_VALUE;
401 }
402
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530403 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
404 if (status == GRALLOC1_ERROR_NONE) {
405 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700406 *outWidth = UINT(hnd->GetUnalignedWidth());
407 *outHeight = UINT(hnd->GetUnalignedHeight());
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530408 }
409
Mahesh Aiabb828122019-01-16 13:07:42 -0800410 return static_cast<int32_t>(status);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530411}
412
Mahesh Aiabb828122019-01-16 13:07:42 -0800413int32_t GrallocImpl::GetColorFormat(gralloc1_device_t *device, buffer_handle_t buffer,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530414 int32_t *outFormat) {
Varun Arora16911a92018-06-13 17:59:12 -0700415 if (!outFormat) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800416 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
Varun Arora16911a92018-06-13 17:59:12 -0700417 }
418
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530419 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
420 if (status == GRALLOC1_ERROR_NONE) {
421 *outFormat = PRIV_HANDLE_CONST(buffer)->GetColorFormat();
422 }
423
Mahesh Aiabb828122019-01-16 13:07:42 -0800424 return static_cast<int32_t>(status);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530425}
426
Mahesh Aiabb828122019-01-16 13:07:42 -0800427int32_t GrallocImpl::GetLayerCount(gralloc1_device_t *device, buffer_handle_t buffer,
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400428 uint32_t *outLayerCount) {
Varun Arora16911a92018-06-13 17:59:12 -0700429 if (!outLayerCount) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800430 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
Varun Arora16911a92018-06-13 17:59:12 -0700431 }
432
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400433 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
434 if (status == GRALLOC1_ERROR_NONE) {
435 *outLayerCount = PRIV_HANDLE_CONST(buffer)->GetLayerCount();
436 }
437
Mahesh Aiabb828122019-01-16 13:07:42 -0800438 return static_cast<int32_t>(status);
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400439}
440
Mahesh Aiabb828122019-01-16 13:07:42 -0800441int32_t GrallocImpl::GetProducerUsage(gralloc1_device_t *device, buffer_handle_t buffer,
Mahesh Aiab9325182019-07-09 10:27:55 -0700442 uint64_t /*gralloc1_producer_usage_t*/ *outUsage) {
Saurabh Shah6e4b3762017-09-21 15:30:15 -0700443 if (!outUsage) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800444 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
Saurabh Shah6e4b3762017-09-21 15:30:15 -0700445 }
446
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530447 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
448 if (status == GRALLOC1_ERROR_NONE) {
Mahesh Aiab9325182019-07-09 10:27:55 -0700449 *outUsage = static_cast<uint64_t>(PRIV_HANDLE_CONST(buffer)->GetUsage());
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530450 }
451
Mahesh Aiabb828122019-01-16 13:07:42 -0800452 return static_cast<int32_t>(status);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530453}
454
Mahesh Aiabb828122019-01-16 13:07:42 -0800455int32_t GrallocImpl::GetBufferStride(gralloc1_device_t *device, buffer_handle_t buffer,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530456 uint32_t *outStride) {
Saurabh Shah6e4b3762017-09-21 15:30:15 -0700457 if (!outStride) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800458 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
Saurabh Shah6e4b3762017-09-21 15:30:15 -0700459 }
460
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530461 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
462 if (status == GRALLOC1_ERROR_NONE) {
463 *outStride = UINT(PRIV_HANDLE_CONST(buffer)->GetStride());
464 }
465
Mahesh Aiabb828122019-01-16 13:07:42 -0800466 return static_cast<int32_t>(status);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530467}
468
Naseer Ahmede36f2242017-12-01 15:33:56 -0500469gralloc1_error_t GrallocImpl::AllocateBuffer(const gralloc1_buffer_descriptor_t *descriptor_ids,
470 buffer_handle_t *out_buffers) {
471 gralloc1_error_t status = GRALLOC1_ERROR_NONE;
472
473 // Validate descriptor
474 std::lock_guard<std::mutex> descriptor_lock(descriptor_lock_);
475 std::shared_ptr<gralloc::BufferDescriptor> descriptor;
476 const auto map_descriptor = descriptors_map_.find(descriptor_ids[0]);
477 if (map_descriptor == descriptors_map_.end()) {
478 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
479 } else {
480 descriptor = map_descriptor->second;
481 }
482
483 // Allocate separate buffer for each descriptor
484 if (buf_mgr_->AllocateBuffer(*descriptor, &out_buffers[0]) != Error::NONE) {
485 return GRALLOC1_ERROR_NO_RESOURCES;
486 }
487
488 return status;
489}
490
Mahesh Aiabb828122019-01-16 13:07:42 -0800491int32_t GrallocImpl::AllocateBuffers(gralloc1_device_t *device, uint32_t num_descriptors,
Naseer Ahmede69031e2016-11-22 20:05:16 -0500492 const gralloc1_buffer_descriptor_t *descriptors,
493 buffer_handle_t *out_buffers) {
494 if (!num_descriptors || !descriptors) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800495 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_DESCRIPTOR);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530496 }
497
Saurabh Shah6e4b3762017-09-21 15:30:15 -0700498 if (!device) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800499 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
Saurabh Shah6e4b3762017-09-21 15:30:15 -0700500 }
501
Naseer Ahmede36f2242017-12-01 15:33:56 -0500502 if (num_descriptors != 1) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800503 return static_cast<int32_t>(GRALLOC1_ERROR_UNSUPPORTED);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500504 }
505
506 auto *dev = reinterpret_cast<GrallocImpl *>(device);
507 gralloc1_error_t status = dev->AllocateBuffer(descriptors, out_buffers);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530508
Mahesh Aiabb828122019-01-16 13:07:42 -0800509 return static_cast<int32_t>(status);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530510}
511
Mahesh Aiabb828122019-01-16 13:07:42 -0800512int32_t GrallocImpl::RetainBuffer(gralloc1_device_t *device, buffer_handle_t buffer) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530513 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
514 if (status == GRALLOC1_ERROR_NONE) {
515 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
516 GrallocImpl const *dev = GRALLOC_IMPL(device);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500517 status = ToError(dev->buf_mgr_->RetainBuffer(hnd));
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530518 }
519
Mahesh Aiabb828122019-01-16 13:07:42 -0800520 return static_cast<int32_t>(status);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530521}
522
Mahesh Aiabb828122019-01-16 13:07:42 -0800523int32_t GrallocImpl::ReleaseBuffer(gralloc1_device_t *device, buffer_handle_t buffer) {
Naseer Ahmeda186b472017-07-07 18:50:49 -0400524 if (!device || !buffer) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800525 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_DESCRIPTOR);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530526 }
527
Naseer Ahmeda186b472017-07-07 18:50:49 -0400528 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
529 GrallocImpl const *dev = GRALLOC_IMPL(device);
Mahesh Aiabb828122019-01-16 13:07:42 -0800530 return static_cast<int32_t>((ToError(dev->buf_mgr_->ReleaseBuffer(hnd))));
Naseer Ahmede36f2242017-12-01 15:33:56 -0500531}
532
Mahesh Aiabb828122019-01-16 13:07:42 -0800533int32_t GrallocImpl::GetFlexLayout(const private_handle_t *hnd,
Naseer Ahmede36f2242017-12-01 15:33:56 -0500534 struct android_flex_layout *layout) {
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +0530535 if (!IsYuvFormat(hnd->format)) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800536 return static_cast<int32_t>(GRALLOC1_ERROR_UNSUPPORTED);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500537 }
538
539 android_ycbcr yuvPlaneInfo[2];
540 int err = GetYUVPlaneInfo(hnd, yuvPlaneInfo);
541
542 if (err != 0) {
543 return GRALLOC1_ERROR_BAD_HANDLE;
544 }
545
546 layout->format = FLEX_FORMAT_YCbCr;
547 layout->num_planes = 3;
548
549 for (uint32_t i = 0; i < layout->num_planes; i++) {
550 layout->planes[i].bits_per_component = 8;
551 layout->planes[i].bits_used = 8;
552 layout->planes[i].h_increment = 1;
553 layout->planes[i].v_increment = 1;
554 layout->planes[i].h_subsampling = 2;
555 layout->planes[i].v_subsampling = 2;
556 }
557
558 // We are only returning flex layout for progressive or single field formats.
559 struct android_ycbcr ycbcr = yuvPlaneInfo[0];
560 layout->planes[0].top_left = static_cast<uint8_t *>(ycbcr.y);
561 layout->planes[0].component = FLEX_COMPONENT_Y;
562 layout->planes[0].v_increment = static_cast<int32_t>(ycbcr.ystride);
563
564 layout->planes[1].top_left = static_cast<uint8_t *>(ycbcr.cb);
565 layout->planes[1].component = FLEX_COMPONENT_Cb;
566 layout->planes[1].h_increment = static_cast<int32_t>(ycbcr.chroma_step);
567 layout->planes[1].v_increment = static_cast<int32_t>(ycbcr.cstride);
568
569 layout->planes[2].top_left = static_cast<uint8_t *>(ycbcr.cr);
570 layout->planes[2].component = FLEX_COMPONENT_Cr;
571 layout->planes[2].h_increment = static_cast<int32_t>(ycbcr.chroma_step);
572 layout->planes[2].v_increment = static_cast<int32_t>(ycbcr.cstride);
Mahesh Aiabb828122019-01-16 13:07:42 -0800573 return static_cast<int32_t>(GRALLOC1_ERROR_NONE);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530574}
575
Mahesh Aiabb828122019-01-16 13:07:42 -0800576int32_t GrallocImpl::GetNumFlexPlanes(gralloc1_device_t *device, buffer_handle_t buffer,
Naseer Ahmede69031e2016-11-22 20:05:16 -0500577 uint32_t *out_num_planes) {
Saurabh Shah6e4b3762017-09-21 15:30:15 -0700578 if (!out_num_planes) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800579 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
Saurabh Shah6e4b3762017-09-21 15:30:15 -0700580 }
581
Naseer Ahmede69031e2016-11-22 20:05:16 -0500582 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
583 if (status == GRALLOC1_ERROR_NONE) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500584 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +0530585 if (!IsYuvFormat(hnd->format)) {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500586 status = GRALLOC1_ERROR_UNSUPPORTED;
587 } else {
588 *out_num_planes = 3;
589 }
Naseer Ahmede69031e2016-11-22 20:05:16 -0500590 }
Mahesh Aiabb828122019-01-16 13:07:42 -0800591 return static_cast<int32_t>(status);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500592}
593
Naseer Ahmed86514172017-03-29 10:07:43 -0400594static inline void CloseFdIfValid(int fd) {
595 if (fd > 0) {
596 close(fd);
597 }
598}
599
Mahesh Aiabb828122019-01-16 13:07:42 -0800600int32_t GrallocImpl::LockBuffer(gralloc1_device_t *device, buffer_handle_t buffer,
Mahesh Aiab9325182019-07-09 10:27:55 -0700601 uint64_t /*gralloc1_producer_usage_t*/ prod_usage,
602 uint64_t /*gralloc1_consumer_usage_t*/ cons_usage,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530603 const gralloc1_rect_t *region, void **out_data,
604 int32_t acquire_fence) {
Naseer Ahmed7a2b09c2017-05-11 13:03:17 -0400605 ATRACE_CALL();
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530606 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
Saurabh Shah6e4b3762017-09-21 15:30:15 -0700607 if (status != GRALLOC1_ERROR_NONE || !out_data ||
608 !region) { // currently we ignore the region/rect client wants to lock
Naseer Ahmed86514172017-03-29 10:07:43 -0400609 CloseFdIfValid(acquire_fence);
Mahesh Aiabb828122019-01-16 13:07:42 -0800610 return static_cast<int32_t>(status);
Naseer Ahmed86514172017-03-29 10:07:43 -0400611 }
612
613 if (acquire_fence > 0) {
Naseer Ahmed7a2b09c2017-05-11 13:03:17 -0400614 ATRACE_BEGIN("fence wait");
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530615 int error = sync_wait(acquire_fence, 1000);
Naseer Ahmed7a2b09c2017-05-11 13:03:17 -0400616 ATRACE_END();
Naseer Ahmed86514172017-03-29 10:07:43 -0400617 CloseFdIfValid(acquire_fence);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530618 if (error < 0) {
619 ALOGE("%s: sync_wait timedout! error = %s", __FUNCTION__, strerror(errno));
Mahesh Aiabb828122019-01-16 13:07:42 -0800620 return static_cast<int32_t>(GRALLOC1_ERROR_UNDEFINED);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530621 }
622 }
623
624 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
625 GrallocImpl const *dev = GRALLOC_IMPL(device);
626
627 // Either producer usage or consumer usage must be *_USAGE_NONE
628 if ((prod_usage != GRALLOC1_PRODUCER_USAGE_NONE) &&
629 (cons_usage != GRALLOC1_CONSUMER_USAGE_NONE)) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500630 // Current gralloc1 clients do not satisfy this restriction.
631 // See b/33588773 for details
632 // return GRALLOC1_ERROR_BAD_VALUE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530633 }
634
Naseer Ahmede36f2242017-12-01 15:33:56 -0500635 status = ToError(dev->buf_mgr_->LockBuffer(
636 hnd, ProducerUsageToBufferUsage(prod_usage) | ConsumerUsageToBufferUsage(cons_usage)));
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530637 *out_data = reinterpret_cast<void *>(hnd->base);
638
Mahesh Aiabb828122019-01-16 13:07:42 -0800639 return static_cast<int32_t>(status);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530640}
641
Mahesh Aiabb828122019-01-16 13:07:42 -0800642int32_t GrallocImpl::LockFlex(gralloc1_device_t *device, buffer_handle_t buffer,
Mahesh Aiab9325182019-07-09 10:27:55 -0700643 uint64_t /*gralloc1_producer_usage_t*/ prod_usage,
644 uint64_t /*gralloc1_consumer_usage_t*/ cons_usage,
Naseer Ahmede69031e2016-11-22 20:05:16 -0500645 const gralloc1_rect_t *region,
646 struct android_flex_layout *out_flex_layout,
647 int32_t acquire_fence) {
Saurabh Shah6e4b3762017-09-21 15:30:15 -0700648 if (!out_flex_layout) {
649 CloseFdIfValid(acquire_fence);
Mahesh Aiabb828122019-01-16 13:07:42 -0800650 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
Saurabh Shah6e4b3762017-09-21 15:30:15 -0700651 }
652
Naseer Ahmede36f2242017-12-01 15:33:56 -0500653 void *out_data{};
Mahesh Aiabb828122019-01-16 13:07:42 -0800654 int32_t status = GrallocImpl::LockBuffer(device, buffer, prod_usage, cons_usage, region,
Naseer Ahmede69031e2016-11-22 20:05:16 -0500655 &out_data, acquire_fence);
656 if (status != GRALLOC1_ERROR_NONE) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800657 return static_cast<int32_t>(status);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530658 }
659
Naseer Ahmede36f2242017-12-01 15:33:56 -0500660 auto *dev = reinterpret_cast<GrallocImpl *>(device);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500661 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500662 dev->GetFlexLayout(hnd, out_flex_layout);
Mahesh Aiabb828122019-01-16 13:07:42 -0800663 return static_cast<int32_t>(status);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530664}
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530665
Mahesh Aiabb828122019-01-16 13:07:42 -0800666int32_t GrallocImpl::UnlockBuffer(gralloc1_device_t *device, buffer_handle_t buffer,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530667 int32_t *release_fence) {
668 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530669 if (status != GRALLOC1_ERROR_NONE) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800670 return static_cast<int32_t>(status);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530671 }
672
Saurabh Shah6e4b3762017-09-21 15:30:15 -0700673 if (!release_fence) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800674 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
Saurabh Shah6e4b3762017-09-21 15:30:15 -0700675 }
676
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530677 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
678 GrallocImpl const *dev = GRALLOC_IMPL(device);
679
680 *release_fence = -1;
681
Mahesh Aiabb828122019-01-16 13:07:42 -0800682 return static_cast<int32_t>(ToError(dev->buf_mgr_->UnlockBuffer(hnd)));
Naseer Ahmede36f2242017-12-01 15:33:56 -0500683}
684
Mahesh Aiabb828122019-01-16 13:07:42 -0800685static int32_t Perform(int operation, va_list args) {
Naseer Ahmede36f2242017-12-01 15:33:56 -0500686 switch (operation) {
687 case GRALLOC_MODULE_PERFORM_GET_STRIDE: {
688 int width = va_arg(args, int);
689 int format = va_arg(args, int);
690 int *stride = va_arg(args, int *);
691 unsigned int alignedw = 0, alignedh = 0;
692
693 if (!stride) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800694 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500695 }
696
697 BufferInfo info(width, width, format);
698 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
699 *stride = INT(alignedw);
700 } break;
701
702 case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE: {
703 private_handle_t *hnd = va_arg(args, private_handle_t *);
704 int *stride = va_arg(args, int *);
705 if (private_handle_t::validate(hnd) != 0) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800706 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_HANDLE);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500707 }
708
709 if (!stride) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800710 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500711 }
712
713 BufferDim_t buffer_dim;
714 if (getMetaData(hnd, GET_BUFFER_GEOMETRY, &buffer_dim) == 0) {
715 *stride = buffer_dim.sliceWidth;
716 } else {
717 *stride = hnd->width;
718 }
719 } break;
720
721 case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE: {
722 private_handle_t *hnd = va_arg(args, private_handle_t *);
723 int *stride = va_arg(args, int *);
724 int *height = va_arg(args, int *);
725 if (private_handle_t::validate(hnd) != 0) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800726 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_HANDLE);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500727 }
728
729 if (!stride || !height) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800730 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500731 }
732
733 GetCustomDimensions(hnd, stride, height);
734 } break;
735
736 case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES: {
737 int width = va_arg(args, int);
738 int height = va_arg(args, int);
739 int format = va_arg(args, int);
740 uint64_t usage = va_arg(args, uint64_t);
741 usage |= va_arg(args, uint64_t);
742
743 int *aligned_width = va_arg(args, int *);
744 int *aligned_height = va_arg(args, int *);
745 int *tile_enabled = va_arg(args, int *);
746 if (!aligned_width || !aligned_height || !tile_enabled) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800747 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500748 }
749
750 unsigned int alignedw, alignedh;
751 BufferInfo info(width, height, format, usage);
752 *tile_enabled = IsUBwcEnabled(format, usage);
753 GetAlignedWidthAndHeight(info, &alignedw, &alignedh);
754 *aligned_width = INT(alignedw);
755 *aligned_height = INT(alignedh);
756 } break;
757
758 case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE: {
759 private_handle_t *hnd = va_arg(args, private_handle_t *);
760 int *color_space = va_arg(args, int *);
761
762 if (private_handle_t::validate(hnd) != 0) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800763 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_HANDLE);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500764 }
765
766 if (!color_space) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800767 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500768 }
769
770 *color_space = 0;
771 GetColorSpaceFromMetadata(hnd, color_space);
772 } break;
773 case GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO: {
774 private_handle_t *hnd = va_arg(args, private_handle_t *);
775 android_ycbcr *ycbcr = va_arg(args, struct android_ycbcr *);
776 if (private_handle_t::validate(hnd) != 0) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800777 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_HANDLE);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500778 }
779
780 if (!ycbcr) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800781 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500782 }
783
784 if (GetYUVPlaneInfo(hnd, ycbcr)) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800785 return static_cast<int32_t>(GRALLOC1_ERROR_UNDEFINED);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500786 }
787 } break;
788
789 case GRALLOC_MODULE_PERFORM_GET_MAP_SECURE_BUFFER_INFO: {
790 private_handle_t *hnd = va_arg(args, private_handle_t *);
791 int *map_secure_buffer = va_arg(args, int *);
792
793 if (private_handle_t::validate(hnd) != 0) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800794 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_HANDLE);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500795 }
796
797 if (!map_secure_buffer) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800798 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500799 }
800
801 if (getMetaData(hnd, GET_MAP_SECURE_BUFFER, map_secure_buffer) != 0) {
802 *map_secure_buffer = 0;
803 }
804 } break;
805
806 case GRALLOC_MODULE_PERFORM_GET_UBWC_FLAG: {
807 private_handle_t *hnd = va_arg(args, private_handle_t *);
808 int *flag = va_arg(args, int *);
809
810 if (private_handle_t::validate(hnd) != 0) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800811 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_HANDLE);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500812 }
813
814 if (!flag) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800815 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500816 }
817
818 *flag = hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
819 int linear_format = 0;
820 if (getMetaData(hnd, GET_LINEAR_FORMAT, &linear_format) == 0) {
821 if (linear_format) {
822 *flag = 0;
823 }
824 }
825 } break;
826
827 case GRALLOC_MODULE_PERFORM_GET_RGB_DATA_ADDRESS: {
828 private_handle_t *hnd = va_arg(args, private_handle_t *);
829 void **rgb_data = va_arg(args, void **);
830
831 if (private_handle_t::validate(hnd) != 0) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800832 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_HANDLE);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500833 }
834
835 if (!rgb_data) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800836 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500837 }
838
839 if (GetRgbDataAddress(hnd, rgb_data)) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800840 return static_cast<int32_t>(GRALLOC1_ERROR_UNDEFINED);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500841 }
842 } break;
843
844 case GRALLOC1_MODULE_PERFORM_GET_INTERLACE_FLAG: {
845 private_handle_t *hnd = va_arg(args, private_handle_t *);
846 int *flag = va_arg(args, int *);
847
848 if (private_handle_t::validate(hnd) != 0) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800849 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_HANDLE);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500850 }
851
852 if (!flag) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800853 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_VALUE);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500854 }
855
856 if (getMetaData(hnd, GET_PP_PARAM_INTERLACED, flag) != 0) {
857 *flag = 0;
858 }
859 } break;
860
861 case GRALLOC_MODULE_PERFORM_SET_SINGLE_BUFFER_MODE: {
862 private_handle_t *hnd = va_arg(args, private_handle_t *);
863 uint32_t *enable = va_arg(args, uint32_t *);
864 if (private_handle_t::validate(hnd) != 0) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800865 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_HANDLE);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500866 }
867 if (setMetaData(hnd, SET_SINGLE_BUFFER_MODE, enable) != 0) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800868 return static_cast<int32_t>(GRALLOC1_ERROR_UNSUPPORTED);
Naseer Ahmede36f2242017-12-01 15:33:56 -0500869 }
870 } break;
871
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +0530872 case GRALLOC_MODULE_PERFORM_GET_GRAPHICS_METADATA: {
873 private_handle_t* hnd = va_arg(args, private_handle_t *);
874
875 if (private_handle_t::validate(hnd) != 0) {
Mahesh Aiabb828122019-01-16 13:07:42 -0800876 return static_cast<int32_t>(GRALLOC1_ERROR_BAD_HANDLE);
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +0530877 }
878
879 void* graphic_metadata = va_arg(args, void*);
880
881 if (getMetaData(hnd, GET_GRAPHICS_METADATA, graphic_metadata) != 0) {
882 graphic_metadata = NULL;
Mahesh Aiabb828122019-01-16 13:07:42 -0800883 return static_cast<int32_t>(GRALLOC1_ERROR_UNSUPPORTED);
Saurabh Dubeyb28b82b2018-05-29 09:46:41 +0530884 }
885 } break;
886
Naseer Ahmede36f2242017-12-01 15:33:56 -0500887 default:
888 break;
889 }
Mahesh Aiabb828122019-01-16 13:07:42 -0800890 return static_cast<int32_t>(GRALLOC1_ERROR_NONE);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530891}
892
Mahesh Aiabb828122019-01-16 13:07:42 -0800893int32_t GrallocImpl::Gralloc1Perform(gralloc1_device_t *device, int operation, ...) {
Saurabh Shah6e4b3762017-09-21 15:30:15 -0700894 if (!device) {
895 return GRALLOC1_ERROR_BAD_VALUE;
896 }
897
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530898 va_list args;
899 va_start(args, operation);
Mahesh Aiabb828122019-01-16 13:07:42 -0800900 int32_t err = Perform(operation, args);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530901 va_end(args);
902
903 return err;
904}
905
Naseer Ahmede36f2242017-12-01 15:33:56 -0500906} // namespace gralloc