blob: 08173e88407906bf49d531aeccc547f904b392b7 [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
30#include <cutils/log.h>
31#include <sync/sync.h>
Naseer Ahmeddc918132017-03-07 15:25:14 -050032#include <algorithm>
Naseer Ahmede69031e2016-11-22 20:05:16 -050033#include <sstream>
34#include <string>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053035
36#include "gr_device_impl.h"
37#include "gr_buf_descriptor.h"
38#include "gralloc_priv.h"
39#include "qd_utils.h"
40#include "qdMetaData.h"
41#include "gr_utils.h"
42
43int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device);
44
45int gralloc_device_close(struct hw_device_t *device);
46
47static struct hw_module_methods_t gralloc_module_methods = {.open = gralloc_device_open};
48
Naseer Ahmede69031e2016-11-22 20:05:16 -050049struct gralloc_module_t HAL_MODULE_INFO_SYM = {
50 .common = {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053051 .tag = HARDWARE_MODULE_TAG,
Naseer Ahmede69031e2016-11-22 20:05:16 -050052 .module_api_version = GRALLOC_MODULE_API_VERSION_1_0,
53 .hal_api_version = HARDWARE_HAL_API_VERSION,
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053054 .id = GRALLOC_HARDWARE_MODULE_ID,
55 .name = "Graphics Memory Module",
56 .author = "Code Aurora Forum",
57 .methods = &gralloc_module_methods,
58 .dso = 0,
59 .reserved = {0},
60 },
61};
62
63int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device) {
64 int status = -EINVAL;
65 if (!strcmp(name, GRALLOC_HARDWARE_MODULE_ID)) {
Naseer Ahmede69031e2016-11-22 20:05:16 -050066 gralloc1::GrallocImpl * /*gralloc1_device_t*/ dev = gralloc1::GrallocImpl::GetInstance(module);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053067 *device = reinterpret_cast<hw_device_t *>(dev);
Naseer Ahmed7df1e402017-03-16 15:13:34 -040068 if (dev) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053069 status = 0;
70 } else {
Naseer Ahmed7df1e402017-03-16 15:13:34 -040071 ALOGE("Fatal error opening gralloc1 device");
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053072 }
73 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053074 return status;
75}
76
77namespace gralloc1 {
78
Naseer Ahmede69031e2016-11-22 20:05:16 -050079GrallocImpl::GrallocImpl(const hw_module_t *module) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053080 common.tag = HARDWARE_DEVICE_TAG;
Naseer Ahmede69031e2016-11-22 20:05:16 -050081 common.version = GRALLOC_MODULE_API_VERSION_1_0;
82 common.module = const_cast<hw_module_t *>(module);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053083 common.close = CloseDevice;
84 getFunction = GetFunction;
85 getCapabilities = GetCapabilities;
Naseer Ahmed7df1e402017-03-16 15:13:34 -040086
87 initalized_ = Init();
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053088}
89
90bool GrallocImpl::Init() {
Naseer Ahmede69031e2016-11-22 20:05:16 -050091 buf_mgr_ = BufferManager::GetInstance();
Naseer Ahmed7df1e402017-03-16 15:13:34 -040092 return buf_mgr_ != nullptr;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053093}
94
95GrallocImpl::~GrallocImpl() {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053096}
97
Naseer Ahmed7df1e402017-03-16 15:13:34 -040098int GrallocImpl::CloseDevice(hw_device_t *device __unused) {
99 // No-op since the gralloc device is a singleton
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530100 return 0;
101}
102
103void GrallocImpl::GetCapabilities(struct gralloc1_device *device, uint32_t *out_count,
Naseer Ahmed8f9c7c32017-02-21 16:45:14 -0500104 int32_t /*gralloc1_capability_t*/ *out_capabilities) {
105 if (device != nullptr) {
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400106 if (out_capabilities != nullptr && *out_count >= 2) {
Naseer Ahmed8f9c7c32017-02-21 16:45:14 -0500107 out_capabilities[0] = GRALLOC1_CAPABILITY_TEST_ALLOCATE;
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400108 out_capabilities[1] = GRALLOC1_CAPABILITY_LAYERED_BUFFERS;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530109 }
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400110 *out_count = 2;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530111 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530112 return;
113}
114
115gralloc1_function_pointer_t GrallocImpl::GetFunction(gralloc1_device_t *device, int32_t function) {
116 if (!device) {
117 return NULL;
118 }
119
120 switch (function) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500121 case GRALLOC1_FUNCTION_DUMP:
122 return reinterpret_cast<gralloc1_function_pointer_t>(Dump);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530123 case GRALLOC1_FUNCTION_CREATE_DESCRIPTOR:
124 return reinterpret_cast<gralloc1_function_pointer_t>(CreateBufferDescriptor);
125 case GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR:
126 return reinterpret_cast<gralloc1_function_pointer_t>(DestroyBufferDescriptor);
127 case GRALLOC1_FUNCTION_SET_CONSUMER_USAGE:
128 return reinterpret_cast<gralloc1_function_pointer_t>(SetConsumerUsage);
129 case GRALLOC1_FUNCTION_SET_DIMENSIONS:
130 return reinterpret_cast<gralloc1_function_pointer_t>(SetBufferDimensions);
131 case GRALLOC1_FUNCTION_SET_FORMAT:
132 return reinterpret_cast<gralloc1_function_pointer_t>(SetColorFormat);
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400133 case GRALLOC1_FUNCTION_SET_LAYER_COUNT:
134 return reinterpret_cast<gralloc1_function_pointer_t>(SetLayerCount);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530135 case GRALLOC1_FUNCTION_SET_PRODUCER_USAGE:
136 return reinterpret_cast<gralloc1_function_pointer_t>(SetProducerUsage);
137 case GRALLOC1_FUNCTION_GET_BACKING_STORE:
138 return reinterpret_cast<gralloc1_function_pointer_t>(GetBackingStore);
139 case GRALLOC1_FUNCTION_GET_CONSUMER_USAGE:
140 return reinterpret_cast<gralloc1_function_pointer_t>(GetConsumerUsage);
141 case GRALLOC1_FUNCTION_GET_DIMENSIONS:
142 return reinterpret_cast<gralloc1_function_pointer_t>(GetBufferDimensions);
143 case GRALLOC1_FUNCTION_GET_FORMAT:
144 return reinterpret_cast<gralloc1_function_pointer_t>(GetColorFormat);
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400145 case GRALLOC1_FUNCTION_GET_LAYER_COUNT:
146 return reinterpret_cast<gralloc1_function_pointer_t>(GetLayerCount);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530147 case GRALLOC1_FUNCTION_GET_PRODUCER_USAGE:
148 return reinterpret_cast<gralloc1_function_pointer_t>(GetProducerUsage);
149 case GRALLOC1_FUNCTION_GET_STRIDE:
150 return reinterpret_cast<gralloc1_function_pointer_t>(GetBufferStride);
151 case GRALLOC1_FUNCTION_ALLOCATE:
152 return reinterpret_cast<gralloc1_function_pointer_t>(AllocateBuffers);
153 case GRALLOC1_FUNCTION_RETAIN:
154 return reinterpret_cast<gralloc1_function_pointer_t>(RetainBuffer);
155 case GRALLOC1_FUNCTION_RELEASE:
156 return reinterpret_cast<gralloc1_function_pointer_t>(ReleaseBuffer);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500157 case GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES:
158 return reinterpret_cast<gralloc1_function_pointer_t>(GetNumFlexPlanes);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530159 case GRALLOC1_FUNCTION_LOCK:
160 return reinterpret_cast<gralloc1_function_pointer_t>(LockBuffer);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500161 case GRALLOC1_FUNCTION_LOCK_FLEX:
162 return reinterpret_cast<gralloc1_function_pointer_t>(LockFlex);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530163 case GRALLOC1_FUNCTION_UNLOCK:
164 return reinterpret_cast<gralloc1_function_pointer_t>(UnlockBuffer);
165 case GRALLOC1_FUNCTION_PERFORM:
166 return reinterpret_cast<gralloc1_function_pointer_t>(Gralloc1Perform);
167 default:
168 ALOGE("%s:Gralloc Error. Client Requested for unsupported function", __FUNCTION__);
169 return NULL;
170 }
171
172 return NULL;
173}
174
Naseer Ahmede69031e2016-11-22 20:05:16 -0500175gralloc1_error_t GrallocImpl::Dump(gralloc1_device_t *device, uint32_t *out_size,
176 char *out_buffer) {
177 if (!device) {
178 ALOGE("Gralloc Error : device=%p", (void *)device);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530179 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
180 }
Naseer Ahmeddc918132017-03-07 15:25:14 -0500181 const size_t max_dump_size = 8192;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500182 if (out_buffer == nullptr) {
Naseer Ahmeddc918132017-03-07 15:25:14 -0500183 *out_size = max_dump_size;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500184 } else {
185 std::ostringstream os;
Naseer Ahmede69031e2016-11-22 20:05:16 -0500186 os << "-------------------------------" << std::endl;
187 os << "QTI gralloc dump:" << std::endl;
188 os << "-------------------------------" << std::endl;
Naseer Ahmeddc918132017-03-07 15:25:14 -0500189 GrallocImpl const *dev = GRALLOC_IMPL(device);
190 dev->buf_mgr_->Dump(&os);
191 os << "-------------------------------" << std::endl;
192 auto copied = os.str().copy(out_buffer, std::min(os.str().size(), max_dump_size), 0);
193 *out_size = UINT(copied);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500194 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530195
196 return GRALLOC1_ERROR_NONE;
197}
198
199gralloc1_error_t GrallocImpl::CheckDeviceAndHandle(gralloc1_device_t *device,
200 buffer_handle_t buffer) {
201 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
202 if (!device || (private_handle_t::validate(hnd) != 0)) {
203 ALOGE("Gralloc Error : device= %p, buffer-handle=%p", (void *)device, (void *)buffer);
204 return GRALLOC1_ERROR_BAD_HANDLE;
205 }
206
207 return GRALLOC1_ERROR_NONE;
208}
209
210gralloc1_error_t GrallocImpl::CreateBufferDescriptor(gralloc1_device_t *device,
211 gralloc1_buffer_descriptor_t *out_descriptor) {
212 if (!device) {
213 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
214 }
Naseer Ahmede69031e2016-11-22 20:05:16 -0500215 GrallocImpl const *dev = GRALLOC_IMPL(device);
216 return dev->buf_mgr_->CreateBufferDescriptor(out_descriptor);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530217}
218
219gralloc1_error_t GrallocImpl::DestroyBufferDescriptor(gralloc1_device_t *device,
220 gralloc1_buffer_descriptor_t descriptor) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500221 if (!device) {
222 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530223 }
Naseer Ahmede69031e2016-11-22 20:05:16 -0500224 GrallocImpl const *dev = GRALLOC_IMPL(device);
225 return dev->buf_mgr_->DestroyBufferDescriptor(descriptor);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530226}
227
228gralloc1_error_t GrallocImpl::SetConsumerUsage(gralloc1_device_t *device,
229 gralloc1_buffer_descriptor_t descriptor,
230 gralloc1_consumer_usage_t usage) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500231 if (!device) {
232 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
233 } else {
234 GrallocImpl const *dev = GRALLOC_IMPL(device);
235 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
236 &BufferDescriptor::SetConsumerUsage, usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530237 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530238}
239
240gralloc1_error_t GrallocImpl::SetBufferDimensions(gralloc1_device_t *device,
241 gralloc1_buffer_descriptor_t descriptor,
242 uint32_t width, uint32_t height) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500243 if (!device) {
244 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
245 } else {
246 GrallocImpl const *dev = GRALLOC_IMPL(device);
247 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
248 &BufferDescriptor::SetDimensions,
249 INT(width), INT(height));
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530250 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530251}
252
253gralloc1_error_t GrallocImpl::SetColorFormat(gralloc1_device_t *device,
254 gralloc1_buffer_descriptor_t descriptor,
255 int32_t format) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500256 if (!device) {
257 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
258 } else {
259 GrallocImpl const *dev = GRALLOC_IMPL(device);
260 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
261 &BufferDescriptor::SetColorFormat, format);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530262 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530263}
264
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400265gralloc1_error_t GrallocImpl::SetLayerCount(gralloc1_device_t *device,
266 gralloc1_buffer_descriptor_t descriptor,
267 uint32_t layer_count) {
268 if (!device) {
269 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
270 } else {
271 GrallocImpl const *dev = GRALLOC_IMPL(device);
272 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
273 &BufferDescriptor::SetLayerCount,
274 layer_count);
275 }
276}
277
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530278gralloc1_error_t GrallocImpl::SetProducerUsage(gralloc1_device_t *device,
279 gralloc1_buffer_descriptor_t descriptor,
280 gralloc1_producer_usage_t usage) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500281 if (!device) {
282 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
283 } else {
284 GrallocImpl const *dev = GRALLOC_IMPL(device);
285 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
286 &BufferDescriptor::SetProducerUsage, usage);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530287 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530288}
289
290gralloc1_error_t GrallocImpl::GetBackingStore(gralloc1_device_t *device, buffer_handle_t buffer,
291 gralloc1_backing_store_t *out_backstore) {
292 if (!device || !buffer) {
293 return GRALLOC1_ERROR_BAD_HANDLE;
294 }
295
296 *out_backstore =
297 static_cast<gralloc1_backing_store_t>(PRIV_HANDLE_CONST(buffer)->GetBackingstore());
298
299 return GRALLOC1_ERROR_NONE;
300}
301
302gralloc1_error_t GrallocImpl::GetConsumerUsage(gralloc1_device_t *device, buffer_handle_t buffer,
303 gralloc1_consumer_usage_t *outUsage) {
304 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
305 if (status == GRALLOC1_ERROR_NONE) {
306 *outUsage = PRIV_HANDLE_CONST(buffer)->GetConsumerUsage();
307 }
308
309 return status;
310}
311
312gralloc1_error_t GrallocImpl::GetBufferDimensions(gralloc1_device_t *device, buffer_handle_t buffer,
313 uint32_t *outWidth, uint32_t *outHeight) {
314 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
315 if (status == GRALLOC1_ERROR_NONE) {
316 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700317 *outWidth = UINT(hnd->GetUnalignedWidth());
318 *outHeight = UINT(hnd->GetUnalignedHeight());
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530319 }
320
321 return status;
322}
323
324gralloc1_error_t GrallocImpl::GetColorFormat(gralloc1_device_t *device, buffer_handle_t buffer,
325 int32_t *outFormat) {
326 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
327 if (status == GRALLOC1_ERROR_NONE) {
328 *outFormat = PRIV_HANDLE_CONST(buffer)->GetColorFormat();
329 }
330
331 return status;
332}
333
Naseer Ahmedbaa39c52017-03-27 14:00:07 -0400334gralloc1_error_t GrallocImpl::GetLayerCount(gralloc1_device_t *device, buffer_handle_t buffer,
335 uint32_t *outLayerCount) {
336 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
337 if (status == GRALLOC1_ERROR_NONE) {
338 *outLayerCount = PRIV_HANDLE_CONST(buffer)->GetLayerCount();
339 }
340
341 return status;
342}
343
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530344gralloc1_error_t GrallocImpl::GetProducerUsage(gralloc1_device_t *device, buffer_handle_t buffer,
345 gralloc1_producer_usage_t *outUsage) {
346 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
347 if (status == GRALLOC1_ERROR_NONE) {
348 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
349 *outUsage = hnd->GetProducerUsage();
350 }
351
352 return status;
353}
354
355gralloc1_error_t GrallocImpl::GetBufferStride(gralloc1_device_t *device, buffer_handle_t buffer,
356 uint32_t *outStride) {
357 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
358 if (status == GRALLOC1_ERROR_NONE) {
359 *outStride = UINT(PRIV_HANDLE_CONST(buffer)->GetStride());
360 }
361
362 return status;
363}
364
Naseer Ahmede69031e2016-11-22 20:05:16 -0500365gralloc1_error_t GrallocImpl::AllocateBuffers(gralloc1_device_t *device, uint32_t num_descriptors,
366 const gralloc1_buffer_descriptor_t *descriptors,
367 buffer_handle_t *out_buffers) {
368 if (!num_descriptors || !descriptors) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530369 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
370 }
371
372 GrallocImpl const *dev = GRALLOC_IMPL(device);
Naseer Ahmede69031e2016-11-22 20:05:16 -0500373 gralloc1_error_t status = dev->buf_mgr_->AllocateBuffers(num_descriptors, descriptors,
374 out_buffers);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530375
376 return status;
377}
378
379gralloc1_error_t GrallocImpl::RetainBuffer(gralloc1_device_t *device, buffer_handle_t buffer) {
380 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
381 if (status == GRALLOC1_ERROR_NONE) {
382 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
383 GrallocImpl const *dev = GRALLOC_IMPL(device);
384 status = dev->buf_mgr_->RetainBuffer(hnd);
385 }
386
387 return status;
388}
389
390gralloc1_error_t GrallocImpl::ReleaseBuffer(gralloc1_device_t *device, buffer_handle_t buffer) {
391 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
392 if (status == GRALLOC1_ERROR_NONE) {
393 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
394 GrallocImpl const *dev = GRALLOC_IMPL(device);
395 status = dev->buf_mgr_->ReleaseBuffer(hnd);
396 }
397
398 return status;
399}
400
Naseer Ahmede69031e2016-11-22 20:05:16 -0500401gralloc1_error_t GrallocImpl::GetNumFlexPlanes(gralloc1_device_t *device, buffer_handle_t buffer,
402 uint32_t *out_num_planes) {
403 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
404 if (status == GRALLOC1_ERROR_NONE) {
405 GrallocImpl const *dev = GRALLOC_IMPL(device);
406 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
407 status = dev->buf_mgr_->GetNumFlexPlanes(hnd, out_num_planes);
408 }
409 return status;
410}
411
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530412gralloc1_error_t GrallocImpl::LockBuffer(gralloc1_device_t *device, buffer_handle_t buffer,
413 gralloc1_producer_usage_t prod_usage,
414 gralloc1_consumer_usage_t cons_usage,
415 const gralloc1_rect_t *region, void **out_data,
416 int32_t acquire_fence) {
417 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
418 if (status == GRALLOC1_ERROR_NONE && (acquire_fence > 0)) {
419 int error = sync_wait(acquire_fence, 1000);
420 if (error < 0) {
421 ALOGE("%s: sync_wait timedout! error = %s", __FUNCTION__, strerror(errno));
422 return GRALLOC1_ERROR_UNDEFINED;
423 }
424 }
425
426 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
427 GrallocImpl const *dev = GRALLOC_IMPL(device);
428
429 // Either producer usage or consumer usage must be *_USAGE_NONE
430 if ((prod_usage != GRALLOC1_PRODUCER_USAGE_NONE) &&
431 (cons_usage != GRALLOC1_CONSUMER_USAGE_NONE)) {
Naseer Ahmede69031e2016-11-22 20:05:16 -0500432 // Current gralloc1 clients do not satisfy this restriction.
433 // See b/33588773 for details
434 // return GRALLOC1_ERROR_BAD_VALUE;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530435 }
436
437 // currently we ignore the region/rect client wants to lock
438 if (region == NULL) {
439 return GRALLOC1_ERROR_BAD_VALUE;
440 }
Naseer Ahmede69031e2016-11-22 20:05:16 -0500441 // TODO(user): Need to check if buffer was allocated with the same flags
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530442 status = dev->buf_mgr_->LockBuffer(hnd, prod_usage, cons_usage);
443
444 *out_data = reinterpret_cast<void *>(hnd->base);
445
446 return status;
447}
448
Naseer Ahmede69031e2016-11-22 20:05:16 -0500449gralloc1_error_t GrallocImpl::LockFlex(gralloc1_device_t *device, buffer_handle_t buffer,
450 gralloc1_producer_usage_t prod_usage,
451 gralloc1_consumer_usage_t cons_usage,
452 const gralloc1_rect_t *region,
453 struct android_flex_layout *out_flex_layout,
454 int32_t acquire_fence) {
455 void *out_data;
456 gralloc1_error_t status = GrallocImpl::LockBuffer(device, buffer, prod_usage, cons_usage, region,
457 &out_data, acquire_fence);
458 if (status != GRALLOC1_ERROR_NONE) {
459 return status;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530460 }
461
Naseer Ahmede69031e2016-11-22 20:05:16 -0500462 GrallocImpl const *dev = GRALLOC_IMPL(device);
463 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
464 dev->buf_mgr_->GetFlexLayout(hnd, out_flex_layout);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530465 return status;
466}
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530467
468gralloc1_error_t GrallocImpl::UnlockBuffer(gralloc1_device_t *device, buffer_handle_t buffer,
469 int32_t *release_fence) {
470 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
471
472 if (status != GRALLOC1_ERROR_NONE) {
473 return status;
474 }
475
476 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
477 GrallocImpl const *dev = GRALLOC_IMPL(device);
478
479 *release_fence = -1;
480
481 return dev->buf_mgr_->UnlockBuffer(hnd);
482}
483
484gralloc1_error_t GrallocImpl::Gralloc1Perform(gralloc1_device_t *device, int operation, ...) {
485 va_list args;
486 va_start(args, operation);
487 GrallocImpl const *dev = GRALLOC_IMPL(device);
488 gralloc1_error_t err = dev->buf_mgr_->Perform(operation, args);
489 va_end(args);
490
491 return err;
492}
493
494} // namespace gralloc1