1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <cmath>
18 
19 #include <mediautils/AImageReaderUtils.h>
20 #include <hidl/HybridInterface.h>
21 
22 namespace android {
23 namespace aimg {
24 
25 using hardware::hidl_vec;
26 
convertNativeHandleToHGBP(const native_handle_t * handle)27 static sp<HGraphicBufferProducer> convertNativeHandleToHGBP (
28         const native_handle_t *handle) {
29     // Read the size of the halToken vec<uint8_t>
30     hidl_vec<uint8_t> halToken;
31     halToken.setToExternal(
32         reinterpret_cast<uint8_t *>(const_cast<int *>(&(handle->data[1]))),
33         handle->data[0]);
34     sp<HGraphicBufferProducer> hgbp =
35         HGraphicBufferProducer::castFrom(retrieveHalInterface(halToken));
36     return hgbp;
37 }
38 
AImageReader_getHGBPFromHandle(const native_handle_t * handle)39 sp<HGraphicBufferProducer> AImageReader_getHGBPFromHandle(
40     const native_handle_t *handle) {
41     if (handle == nullptr) {
42         return nullptr;
43     }
44     if (handle->numFds != 0  ||
45         handle->numInts < std::ceil(sizeof(size_t) / sizeof(int))) {
46         return nullptr;
47     }
48     return convertNativeHandleToHGBP(handle);
49 }
50 
51 } //namespace aimg
52 } //namespace android
53