xref: /aosp_15_r20/external/minigbm/cros_gralloc/gralloc4/CrosGralloc4Mapper.cc (revision d95af8df99a05bcb8679a54dc3ab8e5cd312b38e)
1 /*
2  * Copyright 2020 The Chromium OS Authors. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the LICENSE file.
5  */
6 
7 #include "cros_gralloc/gralloc4/CrosGralloc4Mapper.h"
8 
9 #include <aidl/android/hardware/graphics/common/BlendMode.h>
10 #include <aidl/android/hardware/graphics/common/Cta861_3.h>
11 #include <aidl/android/hardware/graphics/common/Dataspace.h>
12 #include <aidl/android/hardware/graphics/common/PlaneLayout.h>
13 #include <aidl/android/hardware/graphics/common/Rect.h>
14 #include <aidl/android/hardware/graphics/common/Smpte2086.h>
15 
16 #include <cutils/native_handle.h>
17 #include <gralloctypes/Gralloc4.h>
18 
19 #include "cros_gralloc/cros_gralloc_helpers.h"
20 #include "cros_gralloc/gralloc4/CrosGralloc4Utils.h"
21 
22 using aidl::android::hardware::graphics::common::BlendMode;
23 using aidl::android::hardware::graphics::common::Cta861_3;
24 using aidl::android::hardware::graphics::common::Dataspace;
25 using aidl::android::hardware::graphics::common::PlaneLayout;
26 using aidl::android::hardware::graphics::common::Rect;
27 using aidl::android::hardware::graphics::common::Smpte2086;
28 using android::hardware::hidl_handle;
29 using android::hardware::hidl_vec;
30 using android::hardware::Return;
31 using android::hardware::Void;
32 using android::hardware::graphics::common::V1_2::BufferUsage;
33 using android::hardware::graphics::common::V1_2::PixelFormat;
34 using android::hardware::graphics::mapper::V4_0::Error;
35 using android::hardware::graphics::mapper::V4_0::IMapper;
36 
createDescriptor(const BufferDescriptorInfo & description,createDescriptor_cb hidlCb)37 Return<void> CrosGralloc4Mapper::createDescriptor(const BufferDescriptorInfo& description,
38                                                   createDescriptor_cb hidlCb) {
39     hidl_vec<uint8_t> descriptor;
40 
41     if (description.width == 0) {
42         ALOGE("Failed to createDescriptor. Bad width: %d.", description.width);
43         hidlCb(Error::BAD_VALUE, descriptor);
44         return Void();
45     }
46 
47     if (description.height == 0) {
48         ALOGE("Failed to createDescriptor. Bad height: %d.", description.height);
49         hidlCb(Error::BAD_VALUE, descriptor);
50         return Void();
51     }
52 
53     if (description.layerCount == 0) {
54         ALOGE("Failed to createDescriptor. Bad layer count: %d.", description.layerCount);
55         hidlCb(Error::BAD_VALUE, descriptor);
56         return Void();
57     }
58 
59     int ret = android::gralloc4::encodeBufferDescriptorInfo(description, &descriptor);
60     if (ret) {
61         ALOGE("Failed to createDescriptor. Failed to encode: %d.", ret);
62         hidlCb(Error::BAD_VALUE, descriptor);
63         return Void();
64     }
65 
66     hidlCb(Error::NONE, descriptor);
67     return Void();
68 }
69 
importBuffer(const hidl_handle & handle,importBuffer_cb hidlCb)70 Return<void> CrosGralloc4Mapper::importBuffer(const hidl_handle& handle, importBuffer_cb hidlCb) {
71     if (!mDriver) {
72         ALOGE("Failed to import buffer. Driver is uninitialized.");
73         hidlCb(Error::NO_RESOURCES, nullptr);
74         return Void();
75     }
76 
77     const native_handle_t* bufferHandle = handle.getNativeHandle();
78     if (!bufferHandle || bufferHandle->numFds == 0) {
79         ALOGE("Failed to importBuffer. Bad handle.");
80         hidlCb(Error::BAD_BUFFER, nullptr);
81         return Void();
82     }
83 
84     native_handle_t* importedBufferHandle = native_handle_clone(bufferHandle);
85     if (!importedBufferHandle) {
86         ALOGE("Failed to importBuffer. Handle clone failed: %s.", strerror(errno));
87         hidlCb(Error::NO_RESOURCES, nullptr);
88         return Void();
89     }
90 
91     int ret = mDriver->retain(importedBufferHandle);
92     if (ret) {
93         native_handle_close(importedBufferHandle);
94         native_handle_delete(importedBufferHandle);
95         hidlCb(Error::NO_RESOURCES, nullptr);
96         return Void();
97     }
98 
99     hidlCb(Error::NONE, importedBufferHandle);
100     return Void();
101 }
102 
freeBuffer(void * rawHandle)103 Return<Error> CrosGralloc4Mapper::freeBuffer(void* rawHandle) {
104     if (!mDriver) {
105         ALOGE("Failed to freeBuffer. Driver is uninitialized.");
106         return Error::NO_RESOURCES;
107     }
108 
109     native_handle_t* bufferHandle = reinterpret_cast<native_handle_t*>(rawHandle);
110     if (!bufferHandle) {
111         ALOGE("Failed to freeBuffer. Empty handle.");
112         return Error::BAD_BUFFER;
113     }
114 
115     int ret = mDriver->release(bufferHandle);
116     if (ret) {
117         return Error::BAD_BUFFER;
118     }
119 
120     native_handle_close(bufferHandle);
121     native_handle_delete(bufferHandle);
122     return Error::NONE;
123 }
124 
validateBufferSize(void * rawHandle,const BufferDescriptorInfo & descriptor,uint32_t stride)125 Return<Error> CrosGralloc4Mapper::validateBufferSize(void* rawHandle,
126                                                      const BufferDescriptorInfo& descriptor,
127                                                      uint32_t stride) {
128     if (!mDriver) {
129         ALOGE("Failed to validateBufferSize. Driver is uninitialized.");
130         return Error::NO_RESOURCES;
131     }
132 
133     native_handle_t* bufferHandle = reinterpret_cast<native_handle_t*>(rawHandle);
134     if (!bufferHandle) {
135         ALOGE("Failed to validateBufferSize. Empty handle.");
136         return Error::BAD_BUFFER;
137     }
138 
139     cros_gralloc_handle_t crosHandle = cros_gralloc_convert_handle(bufferHandle);
140     if (!crosHandle) {
141         ALOGE("Failed to validateBufferSize. Invalid handle.");
142         return Error::BAD_BUFFER;
143     }
144 
145     PixelFormat crosHandleFormat = static_cast<PixelFormat>(crosHandle->droid_format);
146     if (descriptor.format != crosHandleFormat) {
147         ALOGE("Failed to validateBufferSize. Format mismatch.");
148         return Error::BAD_BUFFER;
149     }
150 
151     if (descriptor.width != crosHandle->width) {
152         ALOGE("Failed to validateBufferSize. Width mismatch (%d vs %d).", descriptor.width,
153               crosHandle->width);
154         return Error::BAD_VALUE;
155     }
156 
157     if (descriptor.height != crosHandle->height) {
158         ALOGE("Failed to validateBufferSize. Height mismatch (%d vs %d).", descriptor.height,
159               crosHandle->height);
160         return Error::BAD_VALUE;
161     }
162 
163     if (crosHandle->droid_format == HAL_PIXEL_FORMAT_BLOB) {
164         if (stride > crosHandle->pixel_stride) {
165             ALOGE("Failed to validateBufferSize. Oversized stride (%d vs %d).", stride,
166                   crosHandle->pixel_stride);
167             return Error::BAD_VALUE;
168         }
169     } else if (stride != crosHandle->pixel_stride) {
170         ALOGE("Failed to validateBufferSize. Stride mismatch (%d vs %d).", stride,
171               crosHandle->pixel_stride);
172         return Error::BAD_VALUE;
173     }
174 
175     return Error::NONE;
176 }
177 
getTransportSize(void * rawHandle,getTransportSize_cb hidlCb)178 Return<void> CrosGralloc4Mapper::getTransportSize(void* rawHandle, getTransportSize_cb hidlCb) {
179     if (!mDriver) {
180         ALOGE("Failed to getTransportSize. Driver is uninitialized.");
181         hidlCb(Error::BAD_BUFFER, 0, 0);
182         return Void();
183     }
184 
185     native_handle_t* bufferHandle = reinterpret_cast<native_handle_t*>(rawHandle);
186     if (!bufferHandle) {
187         ALOGE("Failed to getTransportSize. Bad handle.");
188         hidlCb(Error::BAD_BUFFER, 0, 0);
189         return Void();
190     }
191 
192     // No local process data is currently stored on the native handle.
193     hidlCb(Error::NONE, bufferHandle->numFds, bufferHandle->numInts);
194     return Void();
195 }
196 
lock(void * rawBuffer,uint64_t cpuUsage,const Rect & region,const hidl_handle & acquireFence,lock_cb hidlCb)197 Return<void> CrosGralloc4Mapper::lock(void* rawBuffer, uint64_t cpuUsage, const Rect& region,
198                                       const hidl_handle& acquireFence, lock_cb hidlCb) {
199     if (!mDriver) {
200         ALOGE("Failed to lock. Driver is uninitialized.");
201         hidlCb(Error::NO_RESOURCES, nullptr);
202         return Void();
203     }
204 
205     buffer_handle_t bufferHandle = reinterpret_cast<buffer_handle_t>(rawBuffer);
206     if (!bufferHandle) {
207         ALOGE("Failed to lock. Empty handle.");
208         hidlCb(Error::BAD_BUFFER, nullptr);
209         return Void();
210     }
211 
212     if (cpuUsage == 0) {
213         ALOGE("Failed to lock. Bad cpu usage: %" PRIu64 ".", cpuUsage);
214         hidlCb(Error::BAD_VALUE, nullptr);
215         return Void();
216     }
217 
218     uint32_t mapUsage = 0;
219     int ret = convertToMapUsage(cpuUsage, &mapUsage);
220     if (ret) {
221         ALOGE("Failed to lock. Convert usage failed.");
222         hidlCb(Error::BAD_VALUE, nullptr);
223         return Void();
224     }
225 
226     cros_gralloc_handle_t crosHandle = cros_gralloc_convert_handle(bufferHandle);
227     if (crosHandle == nullptr) {
228         ALOGE("Failed to lock. Invalid handle.");
229         hidlCb(Error::BAD_VALUE, nullptr);
230         return Void();
231     }
232 
233     if (region.left < 0) {
234         ALOGE("Failed to lock. Invalid region: negative left value %d.", region.left);
235         hidlCb(Error::BAD_VALUE, nullptr);
236         return Void();
237     }
238 
239     if (region.top < 0) {
240         ALOGE("Failed to lock. Invalid region: negative top value %d.", region.top);
241         hidlCb(Error::BAD_VALUE, nullptr);
242         return Void();
243     }
244 
245     if (region.width < 0) {
246         ALOGE("Failed to lock. Invalid region: negative width value %d.", region.width);
247         hidlCb(Error::BAD_VALUE, nullptr);
248         return Void();
249     }
250 
251     if (region.height < 0) {
252         ALOGE("Failed to lock. Invalid region: negative height value %d.", region.height);
253         hidlCb(Error::BAD_VALUE, nullptr);
254         return Void();
255     }
256 
257     if (region.width > crosHandle->width) {
258         ALOGE("Failed to lock. Invalid region: width greater than buffer width (%d vs %d).",
259               region.width, crosHandle->width);
260         hidlCb(Error::BAD_VALUE, nullptr);
261         return Void();
262     }
263 
264     if (region.height > crosHandle->height) {
265         ALOGE("Failed to lock. Invalid region: height greater than buffer height (%d vs %d).",
266               region.height, crosHandle->height);
267         hidlCb(Error::BAD_VALUE, nullptr);
268         return Void();
269     }
270 
271     struct rectangle rect = {static_cast<uint32_t>(region.left), static_cast<uint32_t>(region.top),
272                              static_cast<uint32_t>(region.width),
273                              static_cast<uint32_t>(region.height)};
274 
275     // An access region of all zeros means the entire buffer.
276     if (rect.x == 0 && rect.y == 0 && rect.width == 0 && rect.height == 0) {
277         rect.width = crosHandle->width;
278         rect.height = crosHandle->height;
279     }
280 
281     int acquireFenceFd = -1;
282     ret = convertToFenceFd(acquireFence, &acquireFenceFd);
283     if (ret) {
284         ALOGE("Failed to lock. Bad acquire fence.");
285         hidlCb(Error::BAD_VALUE, nullptr);
286         return Void();
287     }
288 
289     uint8_t* addr[DRV_MAX_PLANES];
290     ret = mDriver->lock(bufferHandle, acquireFenceFd, /*close_acquire_fence=*/false, &rect,
291                         mapUsage, addr);
292     if (ret) {
293         hidlCb(Error::BAD_VALUE, nullptr);
294         return Void();
295     }
296 
297     hidlCb(Error::NONE, addr[0]);
298     return Void();
299 }
300 
unlock(void * rawHandle,unlock_cb hidlCb)301 Return<void> CrosGralloc4Mapper::unlock(void* rawHandle, unlock_cb hidlCb) {
302     if (!mDriver) {
303         ALOGE("Failed to unlock. Driver is uninitialized.");
304         hidlCb(Error::BAD_BUFFER, nullptr);
305         return Void();
306     }
307 
308     buffer_handle_t bufferHandle = reinterpret_cast<buffer_handle_t>(rawHandle);
309     if (!bufferHandle) {
310         ALOGE("Failed to unlock. Empty handle.");
311         hidlCb(Error::BAD_BUFFER, nullptr);
312         return Void();
313     }
314 
315     int releaseFenceFd = -1;
316     int ret = mDriver->unlock(bufferHandle, &releaseFenceFd);
317     if (ret) {
318         ALOGE("Failed to unlock.");
319         hidlCb(Error::BAD_BUFFER, nullptr);
320         return Void();
321     }
322 
323     NATIVE_HANDLE_DECLARE_STORAGE(releaseFenceHandleStorage, 1, 0);
324     hidlCb(Error::NONE, convertToFenceHandle(releaseFenceFd, releaseFenceHandleStorage));
325     return Void();
326 }
327 
flushLockedBuffer(void * rawHandle,flushLockedBuffer_cb hidlCb)328 Return<void> CrosGralloc4Mapper::flushLockedBuffer(void* rawHandle, flushLockedBuffer_cb hidlCb) {
329     if (!mDriver) {
330         ALOGE("Failed to flushLockedBuffer. Driver is uninitialized.");
331         hidlCb(Error::NO_RESOURCES, nullptr);
332         return Void();
333     }
334 
335     buffer_handle_t bufferHandle = reinterpret_cast<buffer_handle_t>(rawHandle);
336     if (!bufferHandle) {
337         ALOGE("Failed to flushLockedBuffer. Empty handle.");
338         hidlCb(Error::BAD_BUFFER, nullptr);
339         return Void();
340     }
341 
342     /*
343      * From the ANativeWindow::dequeueBuffer documentation:
344      *
345      * "A value of -1 indicates that the caller may access the buffer immediately without
346      * waiting on a fence."
347      */
348     int releaseFenceFd = -1;
349     int ret = mDriver->flush(bufferHandle);
350     if (ret) {
351         ALOGE("Failed to flushLockedBuffer. Flush failed.");
352         hidlCb(Error::BAD_BUFFER, nullptr);
353         return Void();
354     }
355 
356     NATIVE_HANDLE_DECLARE_STORAGE(releaseFenceHandleStorage, 1, 0);
357     hidlCb(Error::NONE, convertToFenceHandle(releaseFenceFd, releaseFenceHandleStorage));
358     return Void();
359 }
360 
rereadLockedBuffer(void * rawHandle)361 Return<Error> CrosGralloc4Mapper::rereadLockedBuffer(void* rawHandle) {
362     if (!mDriver) {
363         ALOGE("Failed to rereadLockedBuffer. Driver is uninitialized.");
364         return Error::NO_RESOURCES;
365     }
366 
367     buffer_handle_t bufferHandle = reinterpret_cast<buffer_handle_t>(rawHandle);
368     if (!bufferHandle) {
369         ALOGE("Failed to rereadLockedBuffer. Empty handle.");
370         return Error::BAD_BUFFER;
371     }
372 
373     int ret = mDriver->invalidate(bufferHandle);
374     if (ret) {
375         ALOGE("Failed to rereadLockedBuffer. Failed to invalidate.");
376         return Error::BAD_BUFFER;
377     }
378 
379     return Error::NONE;
380 }
381 
isSupported(const BufferDescriptorInfo & descriptor,isSupported_cb hidlCb)382 Return<void> CrosGralloc4Mapper::isSupported(const BufferDescriptorInfo& descriptor,
383                                              isSupported_cb hidlCb) {
384     if (!mDriver) {
385         ALOGE("Failed to isSupported. Driver is uninitialized.");
386         hidlCb(Error::BAD_VALUE, false);
387         return Void();
388     }
389 
390     struct cros_gralloc_buffer_descriptor crosDescriptor;
391     if (convertToCrosDescriptor(descriptor, &crosDescriptor)) {
392         hidlCb(Error::NONE, false);
393         return Void();
394     }
395 
396     hidlCb(Error::NONE, mDriver->is_supported(&crosDescriptor));
397     return Void();
398 }
399 
get(void * rawHandle,const MetadataType & metadataType,get_cb hidlCb)400 Return<void> CrosGralloc4Mapper::get(void* rawHandle, const MetadataType& metadataType,
401                                      get_cb hidlCb) {
402     hidl_vec<uint8_t> encodedMetadata;
403 
404     if (!mDriver) {
405         ALOGE("Failed to get. Driver is uninitialized.");
406         hidlCb(Error::NO_RESOURCES, encodedMetadata);
407         return Void();
408     }
409 
410     buffer_handle_t bufferHandle = reinterpret_cast<buffer_handle_t>(rawHandle);
411     if (!bufferHandle) {
412         ALOGE("Failed to get. Empty handle.");
413         hidlCb(Error::BAD_BUFFER, encodedMetadata);
414         return Void();
415     }
416 
417     cros_gralloc_handle_t crosHandle = cros_gralloc_convert_handle(bufferHandle);
418     if (!crosHandle) {
419         ALOGE("Failed to get. Invalid handle.");
420         hidlCb(Error::BAD_BUFFER, encodedMetadata);
421         return Void();
422     }
423 
424     mDriver->with_buffer(crosHandle, [&, this](cros_gralloc_buffer* crosBuffer) {
425         get(crosBuffer, metadataType, hidlCb);
426     });
427     return Void();
428 }
429 
get(const cros_gralloc_buffer * crosBuffer,const MetadataType & metadataType,get_cb hidlCb)430 Return<void> CrosGralloc4Mapper::get(const cros_gralloc_buffer* crosBuffer,
431                                      const MetadataType& metadataType, get_cb hidlCb) {
432     hidl_vec<uint8_t> encodedMetadata;
433 
434     if (!mDriver) {
435         ALOGE("Failed to get. Driver is uninitialized.");
436         hidlCb(Error::NO_RESOURCES, encodedMetadata);
437         return Void();
438     }
439 
440     if (!crosBuffer) {
441         ALOGE("Failed to get. Invalid buffer.");
442         hidlCb(Error::BAD_BUFFER, encodedMetadata);
443         return Void();
444     }
445 
446     android::status_t status = android::NO_ERROR;
447     if (metadataType == android::gralloc4::MetadataType_BufferId) {
448         status = android::gralloc4::encodeBufferId(crosBuffer->get_id(), &encodedMetadata);
449     } else if (metadataType == android::gralloc4::MetadataType_Name) {
450         std::optional<std::string> name;
451         int ret = crosBuffer->get_name(&name);
452         if (ret) {
453             ALOGE("Failed to get. Failed to get name internal.");
454             status = android::UNKNOWN_ERROR;
455         } else {
456             status = android::gralloc4::encodeName(*name, &encodedMetadata);
457         }
458     } else if (metadataType == android::gralloc4::MetadataType_Width) {
459         status = android::gralloc4::encodeWidth(crosBuffer->get_width(), &encodedMetadata);
460     } else if (metadataType == android::gralloc4::MetadataType_Height) {
461         status = android::gralloc4::encodeHeight(crosBuffer->get_height(), &encodedMetadata);
462     } else if (metadataType == android::gralloc4::MetadataType_LayerCount) {
463         status = android::gralloc4::encodeLayerCount(1, &encodedMetadata);
464     } else if (metadataType == android::gralloc4::MetadataType_PixelFormatRequested) {
465         PixelFormat pixelFormat = static_cast<PixelFormat>(crosBuffer->get_android_format());
466         status = android::gralloc4::encodePixelFormatRequested(pixelFormat, &encodedMetadata);
467     } else if (metadataType == android::gralloc4::MetadataType_PixelFormatFourCC) {
468         status = android::gralloc4::encodePixelFormatFourCC(
469                 drv_get_standard_fourcc(crosBuffer->get_format()), &encodedMetadata);
470     } else if (metadataType == android::gralloc4::MetadataType_PixelFormatModifier) {
471         status = android::gralloc4::encodePixelFormatModifier(crosBuffer->get_format_modifier(),
472                                                               &encodedMetadata);
473     } else if (metadataType == android::gralloc4::MetadataType_Usage) {
474         status = android::gralloc4::encodeUsage(crosBuffer->get_android_usage(), &encodedMetadata);
475     } else if (metadataType == android::gralloc4::MetadataType_AllocationSize) {
476         status = android::gralloc4::encodeAllocationSize(crosBuffer->get_total_size(),
477                                                          &encodedMetadata);
478     } else if (metadataType == android::gralloc4::MetadataType_ProtectedContent) {
479         uint64_t hasProtectedContent =
480                 crosBuffer->get_android_usage() & BufferUsage::PROTECTED ? 1 : 0;
481         status = android::gralloc4::encodeProtectedContent(hasProtectedContent, &encodedMetadata);
482     } else if (metadataType == android::gralloc4::MetadataType_Compression) {
483         status = android::gralloc4::encodeCompression(android::gralloc4::Compression_None,
484                                                       &encodedMetadata);
485     } else if (metadataType == android::gralloc4::MetadataType_Interlaced) {
486         status = android::gralloc4::encodeInterlaced(android::gralloc4::Interlaced_None,
487                                                      &encodedMetadata);
488     } else if (metadataType == android::gralloc4::MetadataType_ChromaSiting) {
489         status = android::gralloc4::encodeChromaSiting(android::gralloc4::ChromaSiting_None,
490                                                        &encodedMetadata);
491     } else if (metadataType == android::gralloc4::MetadataType_PlaneLayouts) {
492         std::vector<PlaneLayout> planeLayouts;
493         getPlaneLayouts(crosBuffer->get_format(), &planeLayouts);
494 
495         for (size_t plane = 0; plane < planeLayouts.size(); plane++) {
496             PlaneLayout& planeLayout = planeLayouts[plane];
497             planeLayout.offsetInBytes = crosBuffer->get_plane_offset(plane);
498             planeLayout.strideInBytes = crosBuffer->get_plane_stride(plane);
499             planeLayout.totalSizeInBytes = crosBuffer->get_plane_size(plane);
500             planeLayout.widthInSamples =
501                     crosBuffer->get_width() / planeLayout.horizontalSubsampling;
502             planeLayout.heightInSamples =
503                     crosBuffer->get_height() / planeLayout.verticalSubsampling;
504         }
505 
506         status = android::gralloc4::encodePlaneLayouts(planeLayouts, &encodedMetadata);
507     } else if (metadataType == android::gralloc4::MetadataType_Crop) {
508         const uint32_t numPlanes = crosBuffer->get_num_planes();
509         const uint32_t w = crosBuffer->get_width();
510         const uint32_t h = crosBuffer->get_height();
511         std::vector<aidl::android::hardware::graphics::common::Rect> crops;
512         for (uint32_t plane = 0; plane < numPlanes; plane++) {
513             aidl::android::hardware::graphics::common::Rect crop;
514             crop.left = 0;
515             crop.top = 0;
516             crop.right = w;
517             crop.bottom = h;
518             crops.push_back(crop);
519         }
520 
521         status = android::gralloc4::encodeCrop(crops, &encodedMetadata);
522     } else if (metadataType == android::gralloc4::MetadataType_Dataspace) {
523         std::optional<Dataspace> dataspace;
524         int ret = crosBuffer->get_dataspace(&dataspace);
525         if (ret) {
526             ALOGE("Failed to get. Failed to get dataspace internal.");
527             status = android::UNKNOWN_ERROR;
528         } else {
529             status = android::gralloc4::encodeDataspace(*dataspace, &encodedMetadata);
530         }
531     } else if (metadataType == android::gralloc4::MetadataType_BlendMode) {
532         std::optional<BlendMode> blend;
533         int ret = crosBuffer->get_blend_mode(&blend);
534         if (ret) {
535             ALOGE("Failed to get. Failed to get blend mode internal.");
536             status = android::UNKNOWN_ERROR;
537         } else {
538             status = android::gralloc4::encodeBlendMode(*blend, &encodedMetadata);
539         }
540     } else if (metadataType == android::gralloc4::MetadataType_Smpte2086) {
541         std::optional<Smpte2086> smpte;
542         int ret = crosBuffer->get_smpte2086(&smpte);
543         if (ret) {
544             ALOGE("Failed to get. Failed to get smpte2086 internal.");
545             status = android::UNKNOWN_ERROR;
546         } else {
547             status = android::gralloc4::encodeSmpte2086(smpte, &encodedMetadata);
548         }
549     } else if (metadataType == android::gralloc4::MetadataType_Cta861_3) {
550         std::optional<Cta861_3> cta;
551         int ret = crosBuffer->get_cta861_3(&cta);
552         if (ret) {
553             ALOGE("Failed to get. Failed to get cta861_3 internal.");
554             status = android::UNKNOWN_ERROR;
555         } else {
556             status = android::gralloc4::encodeCta861_3(cta, &encodedMetadata);
557         }
558     } else if (metadataType == android::gralloc4::MetadataType_Smpte2094_40) {
559         status = android::gralloc4::encodeSmpte2094_40(std::nullopt, &encodedMetadata);
560     } else {
561         hidlCb(Error::UNSUPPORTED, encodedMetadata);
562         return Void();
563     }
564 
565     if (status != android::NO_ERROR) {
566         hidlCb(Error::NO_RESOURCES, encodedMetadata);
567         ALOGE("Failed to get. Failed to encode metadata.");
568         return Void();
569     }
570 
571     hidlCb(Error::NONE, encodedMetadata);
572     return Void();
573 }
574 
set(void * rawHandle,const MetadataType & metadataType,const hidl_vec<uint8_t> & encodedMetadata)575 Return<Error> CrosGralloc4Mapper::set(void* rawHandle, const MetadataType& metadataType,
576                                       const hidl_vec<uint8_t>& encodedMetadata) {
577     if (!mDriver) {
578         ALOGE("Failed to set. Driver is uninitialized.");
579         return Error::NO_RESOURCES;
580     }
581 
582     buffer_handle_t bufferHandle = reinterpret_cast<buffer_handle_t>(rawHandle);
583     if (!bufferHandle) {
584         ALOGE("Failed to set. Empty handle.");
585         return Error::BAD_BUFFER;
586     }
587 
588     cros_gralloc_handle_t crosHandle = cros_gralloc_convert_handle(bufferHandle);
589     if (!crosHandle) {
590         ALOGE("Failed to set. Invalid handle.");
591         return Error::BAD_BUFFER;
592     }
593 
594     if (metadataType == android::gralloc4::MetadataType_BufferId) {
595         return Error::BAD_VALUE;
596     } else if (metadataType == android::gralloc4::MetadataType_Name) {
597         return Error::BAD_VALUE;
598     } else if (metadataType == android::gralloc4::MetadataType_Width) {
599         return Error::BAD_VALUE;
600     } else if (metadataType == android::gralloc4::MetadataType_Height) {
601         return Error::BAD_VALUE;
602     } else if (metadataType == android::gralloc4::MetadataType_LayerCount) {
603         return Error::BAD_VALUE;
604     } else if (metadataType == android::gralloc4::MetadataType_PixelFormatRequested) {
605         return Error::BAD_VALUE;
606     } else if (metadataType == android::gralloc4::MetadataType_Usage) {
607         return Error::BAD_VALUE;
608     }
609 
610     if (metadataType != android::gralloc4::MetadataType_BlendMode &&
611         metadataType != android::gralloc4::MetadataType_Cta861_3 &&
612         metadataType != android::gralloc4::MetadataType_Dataspace &&
613         metadataType != android::gralloc4::MetadataType_Smpte2086) {
614         return Error::UNSUPPORTED;
615     }
616 
617     Error error = Error::NONE;
618     mDriver->with_buffer(crosHandle, [&, this](cros_gralloc_buffer* crosBuffer) {
619         error = set(crosBuffer, metadataType, encodedMetadata);
620     });
621 
622     return error;
623 }
624 
set(cros_gralloc_buffer * crosBuffer,const MetadataType & metadataType,const android::hardware::hidl_vec<uint8_t> & encodedMetadata)625 Error CrosGralloc4Mapper::set(cros_gralloc_buffer* crosBuffer, const MetadataType& metadataType,
626                               const android::hardware::hidl_vec<uint8_t>& encodedMetadata) {
627     if (!mDriver) {
628         ALOGE("Failed to set. Driver is uninitialized.");
629         return Error::NO_RESOURCES;
630     }
631 
632     if (!crosBuffer) {
633         ALOGE("Failed to set. Invalid buffer.");
634         return Error::BAD_BUFFER;
635     }
636 
637     if (metadataType == android::gralloc4::MetadataType_BlendMode) {
638         BlendMode blend;
639         auto status = android::gralloc4::decodeBlendMode(encodedMetadata, &blend);
640         if (status != android::NO_ERROR) {
641             ALOGE("Failed to set. Failed to decode blend mode.");
642             return Error::UNSUPPORTED;
643         }
644         int ret = crosBuffer->set_blend_mode(blend);
645         if (ret) {
646             ALOGE("Failed to set. Failed to set blend mode internal.");
647             return Error::NO_RESOURCES;
648         }
649     } else if (metadataType == android::gralloc4::MetadataType_Cta861_3) {
650         std::optional<Cta861_3> cta;
651         auto status = android::gralloc4::decodeCta861_3(encodedMetadata, &cta);
652         if (status != android::NO_ERROR) {
653             ALOGE("Failed to set. Failed to decode cta861_3.");
654             return Error::UNSUPPORTED;
655         }
656         int ret = crosBuffer->set_cta861_3(cta);
657         if (ret) {
658             ALOGE("Failed to set. Failed to set cta861_3 internal.");
659             return Error::NO_RESOURCES;
660         }
661     } else if (metadataType == android::gralloc4::MetadataType_Dataspace) {
662         Dataspace dataspace;
663         auto status = android::gralloc4::decodeDataspace(encodedMetadata, &dataspace);
664         if (status != android::NO_ERROR) {
665             ALOGE("Failed to set. Failed to decode dataspace.");
666             return Error::UNSUPPORTED;
667         }
668         int ret = crosBuffer->set_dataspace(dataspace);
669         if (ret) {
670             ALOGE("Failed to set. Failed to set dataspace internal.");
671             return Error::NO_RESOURCES;
672         }
673     } else if (metadataType == android::gralloc4::MetadataType_Smpte2086) {
674         std::optional<Smpte2086> smpte;
675         auto status = android::gralloc4::decodeSmpte2086(encodedMetadata, &smpte);
676         if (status != android::NO_ERROR) {
677             ALOGE("Failed to set. Failed to decode smpte2086.");
678             return Error::UNSUPPORTED;
679         }
680         int ret = crosBuffer->set_smpte2086(smpte);
681         if (ret) {
682             ALOGE("Failed to set. Failed to set dataspace internal.");
683             return Error::NO_RESOURCES;
684         }
685     }
686 
687     return Error::NONE;
688 }
689 
getResolvedDrmFormat(PixelFormat pixelFormat,uint64_t bufferUsage,uint32_t * outDrmFormat)690 int CrosGralloc4Mapper::getResolvedDrmFormat(PixelFormat pixelFormat, uint64_t bufferUsage,
691                                              uint32_t* outDrmFormat) {
692     uint32_t drmFormat;
693     if (convertToDrmFormat(pixelFormat, &drmFormat)) {
694         std::string pixelFormatString = getPixelFormatString(pixelFormat);
695         ALOGE("Failed to getResolvedDrmFormat. Failed to convert format %s",
696               pixelFormatString.c_str());
697         return -EINVAL;
698     }
699 
700     uint64_t usage;
701     if (convertToBufferUsage(bufferUsage, &usage)) {
702         std::string usageString = getUsageString(bufferUsage);
703         ALOGE("Failed to getResolvedDrmFormat. Failed to convert usage %s", usageString.c_str());
704         return -EINVAL;
705     }
706 
707     uint32_t resolvedDrmFormat = mDriver->get_resolved_drm_format(drmFormat, usage);
708     if (resolvedDrmFormat == DRM_FORMAT_INVALID) {
709         std::string drmFormatString = get_drm_format_string(drmFormat);
710         ALOGE("Failed to getResolvedDrmFormat. Failed to resolve drm format %s",
711               drmFormatString.c_str());
712         return -EINVAL;
713     }
714 
715     *outDrmFormat = resolvedDrmFormat;
716 
717     return 0;
718 }
719 
getFromBufferDescriptorInfo(const BufferDescriptorInfo & descriptor,const MetadataType & metadataType,getFromBufferDescriptorInfo_cb hidlCb)720 Return<void> CrosGralloc4Mapper::getFromBufferDescriptorInfo(
721         const BufferDescriptorInfo& descriptor, const MetadataType& metadataType,
722         getFromBufferDescriptorInfo_cb hidlCb) {
723     hidl_vec<uint8_t> encodedMetadata;
724 
725     if (!mDriver) {
726         ALOGE("Failed to getFromBufferDescriptorInfo. Driver is uninitialized.");
727         hidlCb(Error::NO_RESOURCES, encodedMetadata);
728         return Void();
729     }
730 
731     android::status_t status = android::NO_ERROR;
732     if (metadataType == android::gralloc4::MetadataType_Name) {
733         status = android::gralloc4::encodeName(descriptor.name, &encodedMetadata);
734     } else if (metadataType == android::gralloc4::MetadataType_Width) {
735         status = android::gralloc4::encodeWidth(descriptor.width, &encodedMetadata);
736     } else if (metadataType == android::gralloc4::MetadataType_Height) {
737         status = android::gralloc4::encodeHeight(descriptor.height, &encodedMetadata);
738     } else if (metadataType == android::gralloc4::MetadataType_LayerCount) {
739         status = android::gralloc4::encodeLayerCount(1, &encodedMetadata);
740     } else if (metadataType == android::gralloc4::MetadataType_PixelFormatRequested) {
741         status = android::gralloc4::encodePixelFormatRequested(descriptor.format, &encodedMetadata);
742     } else if (metadataType == android::gralloc4::MetadataType_PixelFormatFourCC) {
743         uint32_t drmFormat;
744         if (getResolvedDrmFormat(descriptor.format, descriptor.usage, &drmFormat)) {
745             hidlCb(Error::BAD_VALUE, encodedMetadata);
746             return Void();
747         }
748         status = android::gralloc4::encodePixelFormatFourCC(drv_get_standard_fourcc(drmFormat),
749                                                             &encodedMetadata);
750     } else if (metadataType == android::gralloc4::MetadataType_Usage) {
751         status = android::gralloc4::encodeUsage(descriptor.usage, &encodedMetadata);
752     } else if (metadataType == android::gralloc4::MetadataType_ProtectedContent) {
753         uint64_t hasProtectedContent = descriptor.usage & BufferUsage::PROTECTED ? 1 : 0;
754         status = android::gralloc4::encodeProtectedContent(hasProtectedContent, &encodedMetadata);
755     } else if (metadataType == android::gralloc4::MetadataType_Compression) {
756         status = android::gralloc4::encodeCompression(android::gralloc4::Compression_None,
757                                                       &encodedMetadata);
758     } else if (metadataType == android::gralloc4::MetadataType_Interlaced) {
759         status = android::gralloc4::encodeInterlaced(android::gralloc4::Interlaced_None,
760                                                      &encodedMetadata);
761     } else if (metadataType == android::gralloc4::MetadataType_ChromaSiting) {
762         status = android::gralloc4::encodeChromaSiting(android::gralloc4::ChromaSiting_None,
763                                                        &encodedMetadata);
764     } else if (metadataType == android::gralloc4::MetadataType_Crop) {
765         uint32_t drmFormat;
766         if (getResolvedDrmFormat(descriptor.format, descriptor.usage, &drmFormat)) {
767             hidlCb(Error::BAD_VALUE, encodedMetadata);
768             return Void();
769         }
770 
771         size_t numPlanes = drv_num_planes_from_format(drmFormat);
772 
773         std::vector<aidl::android::hardware::graphics::common::Rect> crops;
774         for (size_t plane = 0; plane < numPlanes; plane++) {
775             aidl::android::hardware::graphics::common::Rect crop;
776             crop.left = 0;
777             crop.top = 0;
778             crop.right = descriptor.width;
779             crop.bottom = descriptor.height;
780             crops.push_back(crop);
781         }
782         status = android::gralloc4::encodeCrop(crops, &encodedMetadata);
783     } else if (metadataType == android::gralloc4::MetadataType_Dataspace) {
784         status = android::gralloc4::encodeDataspace(Dataspace::UNKNOWN, &encodedMetadata);
785     } else if (metadataType == android::gralloc4::MetadataType_BlendMode) {
786         status = android::gralloc4::encodeBlendMode(BlendMode::INVALID, &encodedMetadata);
787     } else if (metadataType == android::gralloc4::MetadataType_Smpte2086) {
788         status = android::gralloc4::encodeSmpte2086(std::nullopt, &encodedMetadata);
789     } else if (metadataType == android::gralloc4::MetadataType_Cta861_3) {
790         status = android::gralloc4::encodeCta861_3(std::nullopt, &encodedMetadata);
791     } else if (metadataType == android::gralloc4::MetadataType_Smpte2094_40) {
792         status = android::gralloc4::encodeSmpte2094_40(std::nullopt, &encodedMetadata);
793     } else {
794         hidlCb(Error::UNSUPPORTED, encodedMetadata);
795         return Void();
796     }
797 
798     if (status != android::NO_ERROR) {
799         hidlCb(Error::NO_RESOURCES, encodedMetadata);
800         return Void();
801     }
802 
803     hidlCb(Error::NONE, encodedMetadata);
804     return Void();
805 }
806 
listSupportedMetadataTypes(listSupportedMetadataTypes_cb hidlCb)807 Return<void> CrosGralloc4Mapper::listSupportedMetadataTypes(listSupportedMetadataTypes_cb hidlCb) {
808     hidl_vec<MetadataTypeDescription> supported;
809 
810     if (!mDriver) {
811         ALOGE("Failed to listSupportedMetadataTypes. Driver is uninitialized.");
812         hidlCb(Error::NO_RESOURCES, supported);
813         return Void();
814     }
815 
816     supported = hidl_vec<IMapper::MetadataTypeDescription>({
817             {
818                     android::gralloc4::MetadataType_BufferId,
819                     "",
820                     /*isGettable=*/true,
821                     /*isSettable=*/false,
822             },
823             {
824                     android::gralloc4::MetadataType_Name,
825                     "",
826                     /*isGettable=*/true,
827                     /*isSettable=*/false,
828             },
829             {
830                     android::gralloc4::MetadataType_Width,
831                     "",
832                     /*isGettable=*/true,
833                     /*isSettable=*/false,
834             },
835             {
836                     android::gralloc4::MetadataType_Height,
837                     "",
838                     /*isGettable=*/true,
839                     /*isSettable=*/false,
840             },
841             {
842                     android::gralloc4::MetadataType_LayerCount,
843                     "",
844                     /*isGettable=*/true,
845                     /*isSettable=*/false,
846             },
847             {
848                     android::gralloc4::MetadataType_PixelFormatRequested,
849                     "",
850                     /*isGettable=*/true,
851                     /*isSettable=*/false,
852             },
853             {
854                     android::gralloc4::MetadataType_PixelFormatFourCC,
855                     "",
856                     /*isGettable=*/true,
857                     /*isSettable=*/false,
858             },
859             {
860                     android::gralloc4::MetadataType_PixelFormatModifier,
861                     "",
862                     /*isGettable=*/true,
863                     /*isSettable=*/false,
864             },
865             {
866                     android::gralloc4::MetadataType_Usage,
867                     "",
868                     /*isGettable=*/true,
869                     /*isSettable=*/false,
870             },
871             {
872                     android::gralloc4::MetadataType_AllocationSize,
873                     "",
874                     /*isGettable=*/true,
875                     /*isSettable=*/false,
876             },
877             {
878                     android::gralloc4::MetadataType_ProtectedContent,
879                     "",
880                     /*isGettable=*/true,
881                     /*isSettable=*/false,
882             },
883             {
884                     android::gralloc4::MetadataType_Compression,
885                     "",
886                     /*isGettable=*/true,
887                     /*isSettable=*/false,
888             },
889             {
890                     android::gralloc4::MetadataType_Interlaced,
891                     "",
892                     /*isGettable=*/true,
893                     /*isSettable=*/false,
894             },
895             {
896                     android::gralloc4::MetadataType_ChromaSiting,
897                     "",
898                     /*isGettable=*/true,
899                     /*isSettable=*/false,
900             },
901             {
902                     android::gralloc4::MetadataType_PlaneLayouts,
903                     "",
904                     /*isGettable=*/true,
905                     /*isSettable=*/false,
906             },
907             {
908                     android::gralloc4::MetadataType_Crop,
909                     "",
910                     /*isGettable=*/true,
911                     /*isSettable=*/false,
912             },
913             {
914                     android::gralloc4::MetadataType_Dataspace,
915                     "",
916                     /*isGettable=*/true,
917                     /*isSettable=*/true,
918             },
919             {
920                     android::gralloc4::MetadataType_BlendMode,
921                     "",
922                     /*isGettable=*/true,
923                     /*isSettable=*/true,
924             },
925             {
926                     android::gralloc4::MetadataType_Smpte2086,
927                     "",
928                     /*isGettable=*/true,
929                     /*isSettable=*/true,
930             },
931             {
932                     android::gralloc4::MetadataType_Cta861_3,
933                     "",
934                     /*isGettable=*/true,
935                     /*isSettable=*/true,
936             },
937             {
938                     android::gralloc4::MetadataType_Smpte2094_40,
939                     "",
940                     /*isGettable=*/true,
941                     /*isSettable=*/false,
942             },
943     });
944 
945     hidlCb(Error::NONE, supported);
946     return Void();
947 }
948 
dumpBuffer(void * rawHandle,dumpBuffer_cb hidlCb)949 Return<void> CrosGralloc4Mapper::dumpBuffer(void* rawHandle, dumpBuffer_cb hidlCb) {
950     BufferDump bufferDump;
951 
952     if (!mDriver) {
953         ALOGE("Failed to dumpBuffer. Driver is uninitialized.");
954         hidlCb(Error::NO_RESOURCES, bufferDump);
955         return Void();
956     }
957 
958     buffer_handle_t bufferHandle = reinterpret_cast<buffer_handle_t>(rawHandle);
959     if (!bufferHandle) {
960         ALOGE("Failed to dumpBuffer. Empty handle.");
961         hidlCb(Error::BAD_BUFFER, bufferDump);
962         return Void();
963     }
964 
965     cros_gralloc_handle_t crosHandle = cros_gralloc_convert_handle(bufferHandle);
966     if (!crosHandle) {
967         ALOGE("Failed to dumpBuffer. Invalid handle.");
968         hidlCb(Error::BAD_BUFFER, bufferDump);
969         return Void();
970     }
971 
972     mDriver->with_buffer(crosHandle, [&, this](cros_gralloc_buffer* crosBuffer) {
973         dumpBuffer(crosBuffer, hidlCb);
974     });
975     return Void();
976 }
977 
dumpBuffer(const cros_gralloc_buffer * crosBuffer,dumpBuffer_cb hidlCb)978 Return<void> CrosGralloc4Mapper::dumpBuffer(const cros_gralloc_buffer* crosBuffer,
979                                             dumpBuffer_cb hidlCb) {
980     BufferDump bufferDump;
981 
982     if (!mDriver) {
983         ALOGE("Failed to dumpBuffer. Driver is uninitialized.");
984         hidlCb(Error::NO_RESOURCES, bufferDump);
985         return Void();
986     }
987 
988     std::vector<MetadataDump> metadataDumps;
989 
990     MetadataType metadataType = android::gralloc4::MetadataType_BufferId;
991     auto metadata_get_callback = [&](Error, hidl_vec<uint8_t> metadata) {
992         MetadataDump metadataDump;
993         metadataDump.metadataType = metadataType;
994         metadataDump.metadata = metadata;
995         metadataDumps.push_back(metadataDump);
996     };
997 
998     metadataType = android::gralloc4::MetadataType_BufferId;
999     get(crosBuffer, metadataType, metadata_get_callback);
1000 
1001     metadataType = android::gralloc4::MetadataType_Name;
1002     get(crosBuffer, metadataType, metadata_get_callback);
1003 
1004     metadataType = android::gralloc4::MetadataType_Width;
1005     get(crosBuffer, metadataType, metadata_get_callback);
1006 
1007     metadataType = android::gralloc4::MetadataType_Height;
1008     get(crosBuffer, metadataType, metadata_get_callback);
1009 
1010     metadataType = android::gralloc4::MetadataType_LayerCount;
1011     get(crosBuffer, metadataType, metadata_get_callback);
1012 
1013     metadataType = android::gralloc4::MetadataType_PixelFormatRequested;
1014     get(crosBuffer, metadataType, metadata_get_callback);
1015 
1016     metadataType = android::gralloc4::MetadataType_PixelFormatFourCC;
1017     get(crosBuffer, metadataType, metadata_get_callback);
1018 
1019     metadataType = android::gralloc4::MetadataType_PixelFormatModifier;
1020     get(crosBuffer, metadataType, metadata_get_callback);
1021 
1022     metadataType = android::gralloc4::MetadataType_Usage;
1023     get(crosBuffer, metadataType, metadata_get_callback);
1024 
1025     metadataType = android::gralloc4::MetadataType_AllocationSize;
1026     get(crosBuffer, metadataType, metadata_get_callback);
1027 
1028     metadataType = android::gralloc4::MetadataType_ProtectedContent;
1029     get(crosBuffer, metadataType, metadata_get_callback);
1030 
1031     metadataType = android::gralloc4::MetadataType_Compression;
1032     get(crosBuffer, metadataType, metadata_get_callback);
1033 
1034     metadataType = android::gralloc4::MetadataType_Interlaced;
1035     get(crosBuffer, metadataType, metadata_get_callback);
1036 
1037     metadataType = android::gralloc4::MetadataType_ChromaSiting;
1038     get(crosBuffer, metadataType, metadata_get_callback);
1039 
1040     metadataType = android::gralloc4::MetadataType_PlaneLayouts;
1041     get(crosBuffer, metadataType, metadata_get_callback);
1042 
1043     metadataType = android::gralloc4::MetadataType_Dataspace;
1044     get(crosBuffer, metadataType, metadata_get_callback);
1045 
1046     metadataType = android::gralloc4::MetadataType_BlendMode;
1047     get(crosBuffer, metadataType, metadata_get_callback);
1048 
1049     bufferDump.metadataDump = metadataDumps;
1050     hidlCb(Error::NONE, bufferDump);
1051     return Void();
1052 }
1053 
dumpBuffers(dumpBuffers_cb hidlCb)1054 Return<void> CrosGralloc4Mapper::dumpBuffers(dumpBuffers_cb hidlCb) {
1055     std::vector<BufferDump> bufferDumps;
1056 
1057     if (!mDriver) {
1058         ALOGE("Failed to dumpBuffers. Driver is uninitialized.");
1059         hidlCb(Error::NO_RESOURCES, bufferDumps);
1060         return Void();
1061     }
1062 
1063     Error error = Error::NONE;
1064 
1065     const auto dumpBufferCallback = [&](Error err, BufferDump bufferDump) {
1066         error = err;
1067         if (error == Error::NONE) {
1068             bufferDumps.push_back(bufferDump);
1069         }
1070     };
1071 
1072     mDriver->with_each_buffer(
1073             [&](cros_gralloc_buffer* crosBuffer) { dumpBuffer(crosBuffer, dumpBufferCallback); });
1074 
1075     hidlCb(error, bufferDumps);
1076     return Void();
1077 }
1078 
getReservedRegion(void * rawHandle,getReservedRegion_cb hidlCb)1079 Return<void> CrosGralloc4Mapper::getReservedRegion(void* rawHandle, getReservedRegion_cb hidlCb) {
1080     if (!mDriver) {
1081         ALOGE("Failed to getReservedRegion. Driver is uninitialized.");
1082         hidlCb(Error::NO_RESOURCES, nullptr, 0);
1083         return Void();
1084     }
1085 
1086     buffer_handle_t bufferHandle = reinterpret_cast<buffer_handle_t>(rawHandle);
1087     if (!bufferHandle) {
1088         ALOGE("Failed to getReservedRegion. Empty handle.");
1089         hidlCb(Error::BAD_BUFFER, nullptr, 0);
1090         return Void();
1091     }
1092 
1093     cros_gralloc_handle_t crosHandle = cros_gralloc_convert_handle(bufferHandle);
1094     if (!crosHandle) {
1095         ALOGE("Failed to getReservedRegion. Invalid handle.");
1096         hidlCb(Error::BAD_BUFFER, nullptr, 0);
1097         return Void();
1098     }
1099 
1100     void* reservedRegionAddr = nullptr;
1101     uint64_t reservedRegionSize = 0;
1102 
1103     Error error = Error::NONE;
1104     mDriver->with_buffer(crosHandle, [&](cros_gralloc_buffer* crosBuffer) {
1105         int ret = crosBuffer->get_client_reserved_region(&reservedRegionAddr, &reservedRegionSize);
1106         if (ret) {
1107             reservedRegionAddr = nullptr;
1108             reservedRegionSize = 0;
1109             error = Error::NO_RESOURCES;
1110         }
1111     });
1112 
1113     if (error != Error::NONE) {
1114         ALOGE("Failed to getReservedRegion.");
1115         hidlCb(Error::BAD_BUFFER, nullptr, 0);
1116         return Void();
1117     }
1118 
1119     hidlCb(Error::NONE, reservedRegionAddr, reservedRegionSize);
1120     return Void();
1121 }
1122 
HIDL_FETCH_IMapper(const char *)1123 android::hardware::graphics::mapper::V4_0::IMapper* HIDL_FETCH_IMapper(const char* /*name*/) {
1124     return static_cast<android::hardware::graphics::mapper::V4_0::IMapper*>(new CrosGralloc4Mapper);
1125 }
1126