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