1 /*
2 * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
3
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 #define DEBUG 0
31 #define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)
32 #include <string.h>
33 #include <sync/sync.h>
34
35 #include <algorithm>
36 #include <sstream>
37 #include <string>
38
39 #include <cutils/trace.h>
40 #include <log/log.h>
41 #include <utils/Trace.h>
42
43 #include "gr_device_impl.h"
44 #include "gr_buf_descriptor.h"
45 #include "gralloc_priv.h"
46 #include "qd_utils.h"
47 #include "qdMetaData.h"
48 #include "gr_utils.h"
49
50 int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device);
51
52 int gralloc_device_close(struct hw_device_t *device);
53
54 static struct hw_module_methods_t gralloc_module_methods = {.open = gralloc_device_open};
55
56 struct gralloc_module_t HAL_MODULE_INFO_SYM = {
57 .common = {
58 .tag = HARDWARE_MODULE_TAG,
59 .module_api_version = GRALLOC_MODULE_API_VERSION_1_0,
60 .hal_api_version = HARDWARE_HAL_API_VERSION,
61 .id = GRALLOC_HARDWARE_MODULE_ID,
62 .name = "Graphics Memory Module",
63 .author = "Code Aurora Forum",
64 .methods = &gralloc_module_methods,
65 .dso = 0,
66 .reserved = {0},
67 },
68 };
69
gralloc_device_open(const struct hw_module_t * module,const char * name,hw_device_t ** device)70 int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device) {
71 int status = -EINVAL;
72 if (!strcmp(name, GRALLOC_HARDWARE_MODULE_ID)) {
73 gralloc1::GrallocImpl * /*gralloc1_device_t*/ dev = gralloc1::GrallocImpl::GetInstance(module);
74 *device = reinterpret_cast<hw_device_t *>(dev);
75 if (dev) {
76 status = 0;
77 } else {
78 ALOGE("Fatal error opening gralloc1 device");
79 }
80 }
81 return status;
82 }
83
84 namespace gralloc1 {
85
GrallocImpl(const hw_module_t * module)86 GrallocImpl::GrallocImpl(const hw_module_t *module) {
87 common.tag = HARDWARE_DEVICE_TAG;
88 common.version = GRALLOC_MODULE_API_VERSION_1_0;
89 common.module = const_cast<hw_module_t *>(module);
90 common.close = CloseDevice;
91 getFunction = GetFunction;
92 getCapabilities = GetCapabilities;
93
94 initalized_ = Init();
95 }
96
Init()97 bool GrallocImpl::Init() {
98 buf_mgr_ = BufferManager::GetInstance();
99 return buf_mgr_ != nullptr;
100 }
101
~GrallocImpl()102 GrallocImpl::~GrallocImpl() {
103 }
104
CloseDevice(hw_device_t * device __unused)105 int GrallocImpl::CloseDevice(hw_device_t *device __unused) {
106 // No-op since the gralloc device is a singleton
107 return 0;
108 }
109
GetCapabilities(struct gralloc1_device * device,uint32_t * out_count,int32_t * out_capabilities)110 void GrallocImpl::GetCapabilities(struct gralloc1_device *device, uint32_t *out_count,
111 int32_t /*gralloc1_capability_t*/ *out_capabilities) {
112 if (device != nullptr) {
113 if (out_capabilities != nullptr && *out_count >= 3) {
114 out_capabilities[0] = GRALLOC1_CAPABILITY_TEST_ALLOCATE;
115 out_capabilities[1] = GRALLOC1_CAPABILITY_LAYERED_BUFFERS;
116 out_capabilities[2] = GRALLOC1_CAPABILITY_RELEASE_IMPLY_DELETE;
117 }
118 *out_count = 3;
119 }
120 return;
121 }
122
GetFunction(gralloc1_device_t * device,int32_t function)123 gralloc1_function_pointer_t GrallocImpl::GetFunction(gralloc1_device_t *device, int32_t function) {
124 if (!device) {
125 return NULL;
126 }
127
128 switch (function) {
129 case GRALLOC1_FUNCTION_DUMP:
130 return reinterpret_cast<gralloc1_function_pointer_t>(Dump);
131 case GRALLOC1_FUNCTION_CREATE_DESCRIPTOR:
132 return reinterpret_cast<gralloc1_function_pointer_t>(CreateBufferDescriptor);
133 case GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR:
134 return reinterpret_cast<gralloc1_function_pointer_t>(DestroyBufferDescriptor);
135 case GRALLOC1_FUNCTION_SET_CONSUMER_USAGE:
136 return reinterpret_cast<gralloc1_function_pointer_t>(SetConsumerUsage);
137 case GRALLOC1_FUNCTION_SET_DIMENSIONS:
138 return reinterpret_cast<gralloc1_function_pointer_t>(SetBufferDimensions);
139 case GRALLOC1_FUNCTION_SET_FORMAT:
140 return reinterpret_cast<gralloc1_function_pointer_t>(SetColorFormat);
141 case GRALLOC1_FUNCTION_SET_LAYER_COUNT:
142 return reinterpret_cast<gralloc1_function_pointer_t>(SetLayerCount);
143 case GRALLOC1_FUNCTION_SET_PRODUCER_USAGE:
144 return reinterpret_cast<gralloc1_function_pointer_t>(SetProducerUsage);
145 case GRALLOC1_FUNCTION_GET_BACKING_STORE:
146 return reinterpret_cast<gralloc1_function_pointer_t>(GetBackingStore);
147 case GRALLOC1_FUNCTION_GET_CONSUMER_USAGE:
148 return reinterpret_cast<gralloc1_function_pointer_t>(GetConsumerUsage);
149 case GRALLOC1_FUNCTION_GET_DIMENSIONS:
150 return reinterpret_cast<gralloc1_function_pointer_t>(GetBufferDimensions);
151 case GRALLOC1_FUNCTION_GET_FORMAT:
152 return reinterpret_cast<gralloc1_function_pointer_t>(GetColorFormat);
153 case GRALLOC1_FUNCTION_GET_LAYER_COUNT:
154 return reinterpret_cast<gralloc1_function_pointer_t>(GetLayerCount);
155 case GRALLOC1_FUNCTION_GET_PRODUCER_USAGE:
156 return reinterpret_cast<gralloc1_function_pointer_t>(GetProducerUsage);
157 case GRALLOC1_FUNCTION_GET_STRIDE:
158 return reinterpret_cast<gralloc1_function_pointer_t>(GetBufferStride);
159 case GRALLOC1_FUNCTION_ALLOCATE:
160 return reinterpret_cast<gralloc1_function_pointer_t>(AllocateBuffers);
161 case GRALLOC1_FUNCTION_RETAIN:
162 return reinterpret_cast<gralloc1_function_pointer_t>(RetainBuffer);
163 case GRALLOC1_FUNCTION_RELEASE:
164 return reinterpret_cast<gralloc1_function_pointer_t>(ReleaseBuffer);
165 case GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES:
166 return reinterpret_cast<gralloc1_function_pointer_t>(GetNumFlexPlanes);
167 case GRALLOC1_FUNCTION_LOCK:
168 return reinterpret_cast<gralloc1_function_pointer_t>(LockBuffer);
169 case GRALLOC1_FUNCTION_LOCK_FLEX:
170 return reinterpret_cast<gralloc1_function_pointer_t>(LockFlex);
171 case GRALLOC1_FUNCTION_UNLOCK:
172 return reinterpret_cast<gralloc1_function_pointer_t>(UnlockBuffer);
173 case GRALLOC1_FUNCTION_PERFORM:
174 return reinterpret_cast<gralloc1_function_pointer_t>(Gralloc1Perform);
175 case GRALLOC1_FUNCTION_VALIDATE_BUFFER_SIZE:
176 return reinterpret_cast<gralloc1_function_pointer_t>(validateBufferSize);
177 case GRALLOC1_FUNCTION_GET_TRANSPORT_SIZE:
178 return reinterpret_cast<gralloc1_function_pointer_t>(getTransportSize);
179 case GRALLOC1_FUNCTION_IMPORT_BUFFER:
180 return reinterpret_cast<gralloc1_function_pointer_t>(importBuffer);
181 default:
182 ALOGE("%s:Gralloc Error. Client Requested for unsupported function", __FUNCTION__);
183 return NULL;
184 }
185
186 return NULL;
187 }
188
Dump(gralloc1_device_t * device,uint32_t * out_size,char * out_buffer)189 gralloc1_error_t GrallocImpl::Dump(gralloc1_device_t *device, uint32_t *out_size,
190 char *out_buffer) {
191 if (!device) {
192 ALOGE("Gralloc Error : device=%p", (void *)device);
193 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
194 }
195 const size_t max_dump_size = 8192;
196 if (out_buffer == nullptr) {
197 *out_size = max_dump_size;
198 } else {
199 std::ostringstream os;
200 os << "-------------------------------" << std::endl;
201 os << "QTI gralloc dump:" << std::endl;
202 os << "-------------------------------" << std::endl;
203 GrallocImpl const *dev = GRALLOC_IMPL(device);
204 dev->buf_mgr_->Dump(&os);
205 os << "-------------------------------" << std::endl;
206 auto copied = os.str().copy(out_buffer, std::min(os.str().size(), max_dump_size), 0);
207 *out_size = UINT(copied);
208 }
209
210 return GRALLOC1_ERROR_NONE;
211 }
212
CheckDeviceAndHandle(gralloc1_device_t * device,buffer_handle_t buffer)213 gralloc1_error_t GrallocImpl::CheckDeviceAndHandle(gralloc1_device_t *device,
214 buffer_handle_t buffer) {
215 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
216 if (!device || (private_handle_t::validate(hnd) != 0)) {
217 ALOGE("Gralloc Error : device= %p, buffer-handle=%p", (void *)device, (void *)buffer);
218 return GRALLOC1_ERROR_BAD_HANDLE;
219 }
220
221 return GRALLOC1_ERROR_NONE;
222 }
223
CreateBufferDescriptor(gralloc1_device_t * device,gralloc1_buffer_descriptor_t * out_descriptor)224 gralloc1_error_t GrallocImpl::CreateBufferDescriptor(gralloc1_device_t *device,
225 gralloc1_buffer_descriptor_t *out_descriptor) {
226 if (!device) {
227 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
228 }
229 GrallocImpl const *dev = GRALLOC_IMPL(device);
230 return dev->buf_mgr_->CreateBufferDescriptor(out_descriptor);
231 }
232
DestroyBufferDescriptor(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor)233 gralloc1_error_t GrallocImpl::DestroyBufferDescriptor(gralloc1_device_t *device,
234 gralloc1_buffer_descriptor_t descriptor) {
235 if (!device) {
236 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
237 }
238 GrallocImpl const *dev = GRALLOC_IMPL(device);
239 return dev->buf_mgr_->DestroyBufferDescriptor(descriptor);
240 }
241
SetConsumerUsage(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,gralloc1_consumer_usage_t usage)242 gralloc1_error_t GrallocImpl::SetConsumerUsage(gralloc1_device_t *device,
243 gralloc1_buffer_descriptor_t descriptor,
244 gralloc1_consumer_usage_t usage) {
245 if (!device) {
246 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
247 } else {
248 GrallocImpl const *dev = GRALLOC_IMPL(device);
249 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
250 &BufferDescriptor::SetConsumerUsage, usage);
251 }
252 }
253
SetBufferDimensions(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,uint32_t width,uint32_t height)254 gralloc1_error_t GrallocImpl::SetBufferDimensions(gralloc1_device_t *device,
255 gralloc1_buffer_descriptor_t descriptor,
256 uint32_t width, uint32_t height) {
257 if (!device) {
258 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
259 } else {
260 GrallocImpl const *dev = GRALLOC_IMPL(device);
261 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
262 &BufferDescriptor::SetDimensions,
263 INT(width), INT(height));
264 }
265 }
266
SetColorFormat(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,int32_t format)267 gralloc1_error_t GrallocImpl::SetColorFormat(gralloc1_device_t *device,
268 gralloc1_buffer_descriptor_t descriptor,
269 int32_t format) {
270 if (!device) {
271 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
272 } else {
273 GrallocImpl const *dev = GRALLOC_IMPL(device);
274 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
275 &BufferDescriptor::SetColorFormat, format);
276 }
277 }
278
SetLayerCount(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,uint32_t layer_count)279 gralloc1_error_t GrallocImpl::SetLayerCount(gralloc1_device_t *device,
280 gralloc1_buffer_descriptor_t descriptor,
281 uint32_t layer_count) {
282 if (!device) {
283 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
284 } else {
285 GrallocImpl const *dev = GRALLOC_IMPL(device);
286 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
287 &BufferDescriptor::SetLayerCount,
288 layer_count);
289 }
290 }
291
SetProducerUsage(gralloc1_device_t * device,gralloc1_buffer_descriptor_t descriptor,gralloc1_producer_usage_t usage)292 gralloc1_error_t GrallocImpl::SetProducerUsage(gralloc1_device_t *device,
293 gralloc1_buffer_descriptor_t descriptor,
294 gralloc1_producer_usage_t usage) {
295 if (!device) {
296 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
297 } else {
298 GrallocImpl const *dev = GRALLOC_IMPL(device);
299 return dev->buf_mgr_->CallBufferDescriptorFunction(descriptor,
300 &BufferDescriptor::SetProducerUsage, usage);
301 }
302 }
303
GetBackingStore(gralloc1_device_t * device,buffer_handle_t buffer,gralloc1_backing_store_t * out_backstore)304 gralloc1_error_t GrallocImpl::GetBackingStore(gralloc1_device_t *device, buffer_handle_t buffer,
305 gralloc1_backing_store_t *out_backstore) {
306 if (!device || !buffer) {
307 return GRALLOC1_ERROR_BAD_HANDLE;
308 }
309
310 *out_backstore =
311 static_cast<gralloc1_backing_store_t>(PRIV_HANDLE_CONST(buffer)->GetBackingstore());
312
313 return GRALLOC1_ERROR_NONE;
314 }
315
GetConsumerUsage(gralloc1_device_t * device,buffer_handle_t buffer,gralloc1_consumer_usage_t * outUsage)316 gralloc1_error_t GrallocImpl::GetConsumerUsage(gralloc1_device_t *device, buffer_handle_t buffer,
317 gralloc1_consumer_usage_t *outUsage) {
318 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
319 if (status == GRALLOC1_ERROR_NONE) {
320 *outUsage = PRIV_HANDLE_CONST(buffer)->GetConsumerUsage();
321 }
322
323 return status;
324 }
325
GetBufferDimensions(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * outWidth,uint32_t * outHeight)326 gralloc1_error_t GrallocImpl::GetBufferDimensions(gralloc1_device_t *device, buffer_handle_t buffer,
327 uint32_t *outWidth, uint32_t *outHeight) {
328 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
329 if (status == GRALLOC1_ERROR_NONE) {
330 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
331 *outWidth = UINT(hnd->GetUnalignedWidth());
332 *outHeight = UINT(hnd->GetUnalignedHeight());
333 }
334
335 return status;
336 }
337
GetColorFormat(gralloc1_device_t * device,buffer_handle_t buffer,int32_t * outFormat)338 gralloc1_error_t GrallocImpl::GetColorFormat(gralloc1_device_t *device, buffer_handle_t buffer,
339 int32_t *outFormat) {
340 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
341 if (status == GRALLOC1_ERROR_NONE) {
342 *outFormat = PRIV_HANDLE_CONST(buffer)->GetColorFormat();
343 }
344
345 return status;
346 }
347
GetLayerCount(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * outLayerCount)348 gralloc1_error_t GrallocImpl::GetLayerCount(gralloc1_device_t *device, buffer_handle_t buffer,
349 uint32_t *outLayerCount) {
350 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
351 if (status == GRALLOC1_ERROR_NONE) {
352 *outLayerCount = PRIV_HANDLE_CONST(buffer)->GetLayerCount();
353 }
354
355 return status;
356 }
357
GetProducerUsage(gralloc1_device_t * device,buffer_handle_t buffer,gralloc1_producer_usage_t * outUsage)358 gralloc1_error_t GrallocImpl::GetProducerUsage(gralloc1_device_t *device, buffer_handle_t buffer,
359 gralloc1_producer_usage_t *outUsage) {
360 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
361 if (status == GRALLOC1_ERROR_NONE) {
362 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
363 *outUsage = hnd->GetProducerUsage();
364 }
365
366 return status;
367 }
368
GetBufferStride(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * outStride)369 gralloc1_error_t GrallocImpl::GetBufferStride(gralloc1_device_t *device, buffer_handle_t buffer,
370 uint32_t *outStride) {
371 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
372 if (status == GRALLOC1_ERROR_NONE) {
373 *outStride = UINT(PRIV_HANDLE_CONST(buffer)->GetStride());
374 }
375
376 return status;
377 }
378
AllocateBuffers(gralloc1_device_t * device,uint32_t num_descriptors,const gralloc1_buffer_descriptor_t * descriptors,buffer_handle_t * out_buffers)379 gralloc1_error_t GrallocImpl::AllocateBuffers(gralloc1_device_t *device, uint32_t num_descriptors,
380 const gralloc1_buffer_descriptor_t *descriptors,
381 buffer_handle_t *out_buffers) {
382 if (!num_descriptors || !descriptors) {
383 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
384 }
385
386 GrallocImpl const *dev = GRALLOC_IMPL(device);
387 gralloc1_error_t status = dev->buf_mgr_->AllocateBuffers(num_descriptors, descriptors,
388 out_buffers);
389
390 return status;
391 }
392
RetainBuffer(gralloc1_device_t * device,buffer_handle_t buffer)393 gralloc1_error_t GrallocImpl::RetainBuffer(gralloc1_device_t *device, buffer_handle_t buffer) {
394 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
395 if (status == GRALLOC1_ERROR_NONE) {
396 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
397 GrallocImpl const *dev = GRALLOC_IMPL(device);
398 status = dev->buf_mgr_->RetainBuffer(hnd);
399 }
400
401 return status;
402 }
403
ReleaseBuffer(gralloc1_device_t * device,buffer_handle_t buffer)404 gralloc1_error_t GrallocImpl::ReleaseBuffer(gralloc1_device_t *device, buffer_handle_t buffer) {
405 if (!device || !buffer) {
406 return GRALLOC1_ERROR_BAD_DESCRIPTOR;
407 }
408
409 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
410 GrallocImpl const *dev = GRALLOC_IMPL(device);
411 return dev->buf_mgr_->ReleaseBuffer(hnd);
412 }
413
GetNumFlexPlanes(gralloc1_device_t * device,buffer_handle_t buffer,uint32_t * out_num_planes)414 gralloc1_error_t GrallocImpl::GetNumFlexPlanes(gralloc1_device_t *device, buffer_handle_t buffer,
415 uint32_t *out_num_planes) {
416 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
417 if (status == GRALLOC1_ERROR_NONE) {
418 GrallocImpl const *dev = GRALLOC_IMPL(device);
419 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
420 status = dev->buf_mgr_->GetNumFlexPlanes(hnd, out_num_planes);
421 }
422 return status;
423 }
424
CloseFdIfValid(int fd)425 static inline void CloseFdIfValid(int fd) {
426 if (fd > 0) {
427 close(fd);
428 }
429 }
430
LockBuffer(gralloc1_device_t * device,buffer_handle_t buffer,gralloc1_producer_usage_t prod_usage,gralloc1_consumer_usage_t cons_usage,const gralloc1_rect_t * region,void ** out_data,int32_t acquire_fence)431 gralloc1_error_t GrallocImpl::LockBuffer(gralloc1_device_t *device, buffer_handle_t buffer,
432 gralloc1_producer_usage_t prod_usage,
433 gralloc1_consumer_usage_t cons_usage,
434 const gralloc1_rect_t *region, void **out_data,
435 int32_t acquire_fence) {
436 ATRACE_CALL();
437 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
438 if (status != GRALLOC1_ERROR_NONE) {
439 CloseFdIfValid(acquire_fence);
440 return status;
441 }
442
443 if (acquire_fence > 0) {
444 ATRACE_BEGIN("fence wait");
445 int error = sync_wait(acquire_fence, 1000);
446 ATRACE_END();
447 CloseFdIfValid(acquire_fence);
448 if (error < 0) {
449 ALOGE("%s: sync_wait timedout! error = %s", __FUNCTION__, strerror(errno));
450 return GRALLOC1_ERROR_UNDEFINED;
451 }
452 }
453
454 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
455 GrallocImpl const *dev = GRALLOC_IMPL(device);
456
457 // Either producer usage or consumer usage must be *_USAGE_NONE
458 if ((prod_usage != GRALLOC1_PRODUCER_USAGE_NONE) &&
459 (cons_usage != GRALLOC1_CONSUMER_USAGE_NONE)) {
460 // Current gralloc1 clients do not satisfy this restriction.
461 // See b/33588773 for details
462 // return GRALLOC1_ERROR_BAD_VALUE;
463 }
464
465 // currently we ignore the region/rect client wants to lock
466 if (region == NULL) {
467 return GRALLOC1_ERROR_BAD_VALUE;
468 }
469 // TODO(user): Need to check if buffer was allocated with the same flags
470 status = dev->buf_mgr_->LockBuffer(hnd, prod_usage, cons_usage);
471
472 *out_data = reinterpret_cast<void *>(hnd->base);
473
474 return status;
475 }
476
LockFlex(gralloc1_device_t * device,buffer_handle_t buffer,gralloc1_producer_usage_t prod_usage,gralloc1_consumer_usage_t cons_usage,const gralloc1_rect_t * region,struct android_flex_layout * out_flex_layout,int32_t acquire_fence)477 gralloc1_error_t GrallocImpl::LockFlex(gralloc1_device_t *device, buffer_handle_t buffer,
478 gralloc1_producer_usage_t prod_usage,
479 gralloc1_consumer_usage_t cons_usage,
480 const gralloc1_rect_t *region,
481 struct android_flex_layout *out_flex_layout,
482 int32_t acquire_fence) {
483 void *out_data;
484 gralloc1_error_t status = GrallocImpl::LockBuffer(device, buffer, prod_usage, cons_usage, region,
485 &out_data, acquire_fence);
486 if (status != GRALLOC1_ERROR_NONE) {
487 return status;
488 }
489
490 GrallocImpl const *dev = GRALLOC_IMPL(device);
491 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
492 dev->buf_mgr_->GetFlexLayout(hnd, out_flex_layout);
493 return status;
494 }
495
UnlockBuffer(gralloc1_device_t * device,buffer_handle_t buffer,int32_t * release_fence)496 gralloc1_error_t GrallocImpl::UnlockBuffer(gralloc1_device_t *device, buffer_handle_t buffer,
497 int32_t *release_fence) {
498 gralloc1_error_t status = CheckDeviceAndHandle(device, buffer);
499
500 if (status != GRALLOC1_ERROR_NONE) {
501 return status;
502 }
503
504 const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer);
505 GrallocImpl const *dev = GRALLOC_IMPL(device);
506
507 *release_fence = -1;
508
509 return dev->buf_mgr_->UnlockBuffer(hnd);
510 }
511
Gralloc1Perform(gralloc1_device_t * device,int operation,...)512 gralloc1_error_t GrallocImpl::Gralloc1Perform(gralloc1_device_t *device, int operation, ...) {
513 va_list args;
514 va_start(args, operation);
515 GrallocImpl const *dev = GRALLOC_IMPL(device);
516 gralloc1_error_t err = dev->buf_mgr_->Perform(operation, args);
517 va_end(args);
518
519 return err;
520 }
521
validateBufferSize(gralloc1_device_t * device,buffer_handle_t buffer,const gralloc1_buffer_descriptor_info_t * info,uint32_t)522 gralloc1_error_t GrallocImpl::validateBufferSize(gralloc1_device_t *device,
523 buffer_handle_t buffer,
524 const gralloc1_buffer_descriptor_info_t *info,
525 uint32_t /* stride */) {
526 GrallocImpl const *dev = GRALLOC_IMPL(device);
527 auto hnd = static_cast<private_handle_t *>(const_cast<native_handle_t *>(buffer));
528 if (dev != NULL && buffer != NULL && private_handle_t::validate(hnd) == 0) {
529 if (dev->buf_mgr_->IsBufferImported(hnd) != GRALLOC1_ERROR_NONE) {
530 return GRALLOC1_ERROR_BAD_HANDLE;
531 }
532 BufferDescriptor descriptor(info->width, info->height, info->format);
533 gralloc1_producer_usage_t prod_usage = static_cast<gralloc1_producer_usage_t >
534 (info->producerUsage);
535
536 gralloc1_consumer_usage_t con_usage = static_cast<gralloc1_consumer_usage_t >
537 (info->consumerUsage);
538 descriptor.SetProducerUsage(prod_usage);
539 descriptor.SetConsumerUsage(con_usage);
540 descriptor.SetLayerCount(info->layerCount);
541
542 return dev->buf_mgr_->ValidateBufferSize(hnd, descriptor);
543 }
544 return GRALLOC1_ERROR_BAD_HANDLE;
545 }
546
getTransportSize(gralloc1_device_t *,buffer_handle_t buffer,uint32_t * out_num_fds,uint32_t * out_num_ints)547 gralloc1_error_t GrallocImpl::getTransportSize(gralloc1_device_t* /* device */,
548 buffer_handle_t buffer,
549 uint32_t *out_num_fds, uint32_t *out_num_ints) {
550 auto err = GRALLOC1_ERROR_BAD_HANDLE;
551 auto hnd = static_cast<private_handle_t *>(const_cast<native_handle_t *>(buffer));
552 if (buffer != NULL && private_handle_t::validate(hnd) == 0) {
553 *out_num_fds = 2; // Verify
554 *out_num_ints = static_cast<uint32_t >(hnd->numInts);
555 err = GRALLOC1_ERROR_NONE;
556 }
557 ALOGD_IF(DEBUG, "GetTransportSize: num fds: %d num ints: %d err:%d",
558 *out_num_fds, *out_num_ints, err);
559 return err;
560 }
561
importBuffer(gralloc1_device_t * device,const buffer_handle_t raw_handle,buffer_handle_t * out_handle)562 gralloc1_error_t GrallocImpl::importBuffer(gralloc1_device_t *device, const buffer_handle_t raw_handle,
563 buffer_handle_t *out_handle) {
564 if (!raw_handle) {
565 ALOGE("%s: handle is NULL", __FUNCTION__);
566 *out_handle = NULL;
567 return GRALLOC1_ERROR_BAD_HANDLE;
568 }
569
570 native_handle_t *buffer_handle = native_handle_clone(raw_handle);
571 if (!buffer_handle) {
572 ALOGE("%s: Unable to clone handle", __FUNCTION__);
573 *out_handle = NULL;
574 return GRALLOC1_ERROR_NO_RESOURCES;
575 }
576
577 GrallocImpl const *dev = GRALLOC_IMPL(device);
578 auto error = dev->buf_mgr_->RetainBuffer(PRIV_HANDLE_CONST(buffer_handle));
579 if (error != GRALLOC1_ERROR_NONE) {
580 ALOGE("%s: Unable to retain handle: %p", __FUNCTION__, buffer_handle);
581 native_handle_close(buffer_handle);
582 native_handle_delete(buffer_handle);
583 *out_handle = NULL;
584 return error;
585 }
586 ALOGD_IF(DEBUG, "Imported handle: %p id: %" PRIu64, buffer_handle,
587 PRIV_HANDLE_CONST(buffer_handle)->id);
588
589 *out_handle = buffer_handle;
590 return GRALLOC1_ERROR_NONE;
591 }
592
593 } // namespace gralloc1
594