summaryrefslogtreecommitdiff
path: root/camera/common/1.0/default/HandleImporter.cpp
blob: d2fdf024574b879146c7fdac349a3f0de4c3794e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "HandleImporter"
#include "HandleImporter.h"

#include <gralloctypes/Gralloc4.h>
#include "aidl/android/hardware/graphics/common/Smpte2086.h"
#include <log/log.h>

namespace android {
namespace hardware {
namespace camera {
namespace common {
namespace V1_0 {
namespace helper {

using aidl::android::hardware::graphics::common::PlaneLayout;
using aidl::android::hardware::graphics::common::PlaneLayoutComponent;
using aidl::android::hardware::graphics::common::PlaneLayoutComponentType;
using aidl::android::hardware::graphics::common::Smpte2086;
using MetadataType = android::hardware::graphics::mapper::V4_0::IMapper::MetadataType;
using MapperErrorV2 = android::hardware::graphics::mapper::V2_0::Error;
using MapperErrorV3 = android::hardware::graphics::mapper::V3_0::Error;
using MapperErrorV4 = android::hardware::graphics::mapper::V4_0::Error;
using IMapperV3 = android::hardware::graphics::mapper::V3_0::IMapper;
using IMapperV4 = android::hardware::graphics::mapper::V4_0::IMapper;

HandleImporter::HandleImporter() : mInitialized(false) {}

void HandleImporter::initializeLocked() {
    if (mInitialized) {
        return;
    }

    mMapperV4 = IMapperV4::getService();
    if (mMapperV4 != nullptr) {
        mInitialized = true;
        return;
    }

    mMapperV3 = IMapperV3::getService();
    if (mMapperV3 != nullptr) {
        mInitialized = true;
        return;
    }

    mMapperV2 = IMapper::getService();
    if (mMapperV2 == nullptr) {
        ALOGE("%s: cannnot acccess graphics mapper HAL!", __FUNCTION__);
        return;
    }

    mInitialized = true;
    return;
}

void HandleImporter::cleanup() {
    mMapperV4.clear();
    mMapperV3.clear();
    mMapperV2.clear();
    mInitialized = false;
}

template<class M, class E>
bool HandleImporter::importBufferInternal(const sp<M> mapper, buffer_handle_t& handle) {
    E error;
    buffer_handle_t importedHandle;
    auto ret = mapper->importBuffer(
        hidl_handle(handle),
        [&](const auto& tmpError, const auto& tmpBufferHandle) {
            error = tmpError;
            importedHandle = static_cast<buffer_handle_t>(tmpBufferHandle);
        });

    if (!ret.isOk()) {
        ALOGE("%s: mapper importBuffer failed: %s",
                __FUNCTION__, ret.description().c_str());
        return false;
    }

    if (error != E::NONE) {
        return false;
    }

    handle = importedHandle;
    return true;
}

template<class M, class E>
YCbCrLayout HandleImporter::lockYCbCrInternal(const sp<M> mapper, buffer_handle_t& buf,
        uint64_t cpuUsage, const IMapper::Rect& accessRegion) {
    hidl_handle acquireFenceHandle;
    auto buffer = const_cast<native_handle_t*>(buf);
    YCbCrLayout layout = {};

    typename M::Rect accessRegionCopy = {accessRegion.left, accessRegion.top,
            accessRegion.width, accessRegion.height};
    mapper->lockYCbCr(buffer, cpuUsage, accessRegionCopy, acquireFenceHandle,
            [&](const auto& tmpError, const auto& tmpLayout) {
                if (tmpError == E::NONE) {
                    // Member by member copy from different versions of YCbCrLayout.
                    layout.y = tmpLayout.y;
                    layout.cb = tmpLayout.cb;
                    layout.cr = tmpLayout.cr;
                    layout.yStride = tmpLayout.yStride;
                    layout.cStride = tmpLayout.cStride;
                    layout.chromaStep = tmpLayout.chromaStep;
                } else {
                    ALOGE("%s: failed to lockYCbCr error %d!", __FUNCTION__, tmpError);
                }
           });
    return layout;
}

bool isMetadataPesent(const sp<IMapperV4> mapper, const buffer_handle_t& buf,
        MetadataType metadataType) {
    auto buffer = const_cast<native_handle_t*>(buf);
    bool ret = false;
    hidl_vec<uint8_t> vec;
    mapper->get(buffer, metadataType, [&] (const auto& tmpError,
                const auto& tmpMetadata) {
                    if (tmpError == MapperErrorV4::NONE) {
                        vec = tmpMetadata;
                    } else {
                        ALOGE("%s: failed to get metadata %d!", __FUNCTION__, tmpError);
                    }});

    if (vec.size() > 0) {
            if (metadataType == gralloc4::MetadataType_Smpte2086){
                std::optional<Smpte2086> realSmpte2086;
                gralloc4::decodeSmpte2086(vec, &realSmpte2086);
                ret = realSmpte2086.has_value();
            } else if (metadataType == gralloc4::MetadataType_Smpte2094_10) {
                std::optional<std::vector<uint8_t>> realSmpte2094_10;
                gralloc4::decodeSmpte2094_10(vec, &realSmpte2094_10);
                ret = realSmpte2094_10.has_value();
            } else if (metadataType == gralloc4::MetadataType_Smpte2094_40) {
                std::optional<std::vector<uint8_t>> realSmpte2094_40;
                gralloc4::decodeSmpte2094_40(vec, &realSmpte2094_40);
                ret = realSmpte2094_40.has_value();
            } else {
                ALOGE("%s: Unknown metadata type!", __FUNCTION__);
            }
    }

    return ret;
}

std::vector<PlaneLayout> getPlaneLayouts(const sp<IMapperV4> mapper, buffer_handle_t& buf) {
    auto buffer = const_cast<native_handle_t*>(buf);
    std::vector<PlaneLayout> planeLayouts;
    hidl_vec<uint8_t> encodedPlaneLayouts;
    mapper->get(buffer, gralloc4::MetadataType_PlaneLayouts,
                [&](const auto& tmpError, const auto& tmpEncodedPlaneLayouts) {
                    if (tmpError == MapperErrorV4::NONE) {
                        encodedPlaneLayouts = tmpEncodedPlaneLayouts;
                    } else {
                        ALOGE("%s: failed to get plane layouts %d!", __FUNCTION__, tmpError);
                    }
                });

    gralloc4::decodePlaneLayouts(encodedPlaneLayouts, &planeLayouts);

    return planeLayouts;
}

template <>
YCbCrLayout HandleImporter::lockYCbCrInternal<IMapperV4, MapperErrorV4>(
        const sp<IMapperV4> mapper, buffer_handle_t& buf, uint64_t cpuUsage,
        const IMapper::Rect& accessRegion) {
    hidl_handle acquireFenceHandle;
    auto buffer = const_cast<native_handle_t*>(buf);
    YCbCrLayout layout = {};
    void* mapped = nullptr;

    typename IMapperV4::Rect accessRegionV4 = {accessRegion.left, accessRegion.top,
                                               accessRegion.width, accessRegion.height};
    mapper->lock(buffer, cpuUsage, accessRegionV4, acquireFenceHandle,
                 [&](const auto& tmpError, const auto& tmpPtr) {
                     if (tmpError == MapperErrorV4::NONE) {
                         mapped = tmpPtr;
                     } else {
                         ALOGE("%s: failed to lock error %d!", __FUNCTION__, tmpError);
                     }
                 });

    if (mapped == nullptr) {
        return layout;
    }

    std::vector<PlaneLayout> planeLayouts = getPlaneLayouts(mapper, buf);
    for (const auto& planeLayout : planeLayouts) {
        for (const auto& planeLayoutComponent : planeLayout.components) {
            const auto& type = planeLayoutComponent.type;

            if (!gralloc4::isStandardPlaneLayoutComponentType(type)) {
                continue;
            }

            uint8_t* data = reinterpret_cast<uint8_t*>(mapped);
            data += planeLayout.offsetInBytes;
            data += planeLayoutComponent.offsetInBits / 8;

            switch (static_cast<PlaneLayoutComponentType>(type.value)) {
                case PlaneLayoutComponentType::Y:
                    layout.y = data;
                    layout.yStride = planeLayout.strideInBytes;
                    break;
                case PlaneLayoutComponentType::CB:
                    layout.cb = data;
                    layout.cStride = planeLayout.strideInBytes;
                    layout.chromaStep = planeLayout.sampleIncrementInBits / 8;
                    break;
                case PlaneLayoutComponentType::CR:
                    layout.cr = data;
                    layout.cStride = planeLayout.strideInBytes;
                    layout.chromaStep = planeLayout.sampleIncrementInBits / 8;
                    break;
                default:
                    break;
            }
        }
    }

    return layout;
}

template<class M, class E>
int HandleImporter::unlockInternal(const sp<M> mapper, buffer_handle_t& buf) {
    int releaseFence = -1;
    auto buffer = const_cast<native_handle_t*>(buf);

    mapper->unlock(
        buffer, [&](const auto& tmpError, const auto& tmpReleaseFence) {
            if (tmpError == E::NONE) {
                auto fenceHandle = tmpReleaseFence.getNativeHandle();
                if (fenceHandle) {
                    if (fenceHandle->numInts != 0 || fenceHandle->numFds != 1) {
                        ALOGE("%s: bad release fence numInts %d numFds %d",
                                __FUNCTION__, fenceHandle->numInts, fenceHandle->numFds);
                        return;
                    }
                    releaseFence = dup(fenceHandle->data[0]);
                    if (releaseFence < 0) {
                        ALOGE("%s: bad release fence FD %d",
                                __FUNCTION__, releaseFence);
                    }
                }
            } else {
                ALOGE("%s: failed to unlock error %d!", __FUNCTION__, tmpError);
            }
        });
    return releaseFence;
}

// In IComposer, any buffer_handle_t is owned by the caller and we need to
// make a clone for hwcomposer2.  We also need to translate empty handle
// to nullptr.  This function does that, in-place.
bool HandleImporter::importBuffer(buffer_handle_t& handle) {
    if (!handle->numFds && !handle->numInts) {
        handle = nullptr;
        return true;
    }

    Mutex::Autolock lock(mLock);
    if (!mInitialized) {
        initializeLocked();
    }

    if (mMapperV4 != nullptr) {
        return importBufferInternal<IMapperV4, MapperErrorV4>(mMapperV4, handle);
    }

    if (mMapperV3 != nullptr) {
        return importBufferInternal<IMapperV3, MapperErrorV3>(mMapperV3, handle);
    }

    if (mMapperV2 != nullptr) {
        return importBufferInternal<IMapper, MapperErrorV2>(mMapperV2, handle);
    }

    ALOGE("%s: mMapperV4, mMapperV3 and mMapperV2 are all null!", __FUNCTION__);
    return false;
}

void HandleImporter::freeBuffer(buffer_handle_t handle) {
    if (!handle) {
        return;
    }

    Mutex::Autolock lock(mLock);
    if (!mInitialized) {
        initializeLocked();
    }

    if (mMapperV4 != nullptr) {
        auto ret = mMapperV4->freeBuffer(const_cast<native_handle_t*>(handle));
        if (!ret.isOk()) {
            ALOGE("%s: mapper freeBuffer failed: %s", __FUNCTION__, ret.description().c_str());
        }
    } else if (mMapperV3 != nullptr) {
        auto ret = mMapperV3->freeBuffer(const_cast<native_handle_t*>(handle));
        if (!ret.isOk()) {
            ALOGE("%s: mapper freeBuffer failed: %s",
                    __FUNCTION__, ret.description().c_str());
        }
    } else {
        auto ret = mMapperV2->freeBuffer(const_cast<native_handle_t*>(handle));
        if (!ret.isOk()) {
            ALOGE("%s: mapper freeBuffer failed: %s",
                    __FUNCTION__, ret.description().c_str());
        }
    }
}

bool HandleImporter::importFence(const native_handle_t* handle, int& fd) const {
    if (handle == nullptr || handle->numFds == 0) {
        fd = -1;
    } else if (handle->numFds == 1) {
        fd = dup(handle->data[0]);
        if (fd < 0) {
            ALOGE("failed to dup fence fd %d", handle->data[0]);
            return false;
        }
    } else {
        ALOGE("invalid fence handle with %d file descriptors",
                handle->numFds);
        return false;
    }

    return true;
}

void HandleImporter::closeFence(int fd) const {
    if (fd >= 0) {
        close(fd);
    }
}

void* HandleImporter::lock(
        buffer_handle_t& buf, uint64_t cpuUsage, size_t size) {
    IMapper::Rect accessRegion{0, 0, static_cast<int>(size), 1};
    return lock(buf, cpuUsage, accessRegion);
}

void* HandleImporter::lock(buffer_handle_t& buf, uint64_t cpuUsage,
                           const IMapper::Rect& accessRegion) {
    Mutex::Autolock lock(mLock);

    if (!mInitialized) {
        initializeLocked();
    }

    void* ret = nullptr;

    if (mMapperV4 == nullptr && mMapperV3 == nullptr && mMapperV2 == nullptr) {
        ALOGE("%s: mMapperV4, mMapperV3 and mMapperV2 are all null!", __FUNCTION__);
        return ret;
    }

    hidl_handle acquireFenceHandle;
    auto buffer = const_cast<native_handle_t*>(buf);
    if (mMapperV4 != nullptr) {
        IMapperV4::Rect accessRegionV4{accessRegion.left, accessRegion.top, accessRegion.width,
                                       accessRegion.height};

        mMapperV4->lock(buffer, cpuUsage, accessRegionV4, acquireFenceHandle,
                        [&](const auto& tmpError, const auto& tmpPtr) {
                            if (tmpError == MapperErrorV4::NONE) {
                                ret = tmpPtr;
                            } else {
                                ALOGE("%s: failed to lock error %d!", __FUNCTION__, tmpError);
                            }
                        });
    } else if (mMapperV3 != nullptr) {
        IMapperV3::Rect accessRegionV3{accessRegion.left, accessRegion.top, accessRegion.width,
                                       accessRegion.height};

        mMapperV3->lock(buffer, cpuUsage, accessRegionV3, acquireFenceHandle,
                        [&](const auto& tmpError, const auto& tmpPtr, const auto& /*bytesPerPixel*/,
                            const auto& /*bytesPerStride*/) {
                            if (tmpError == MapperErrorV3::NONE) {
                                ret = tmpPtr;
                            } else {
                                ALOGE("%s: failed to lock error %d!", __FUNCTION__, tmpError);
                            }
                        });
    } else {
        mMapperV2->lock(buffer, cpuUsage, accessRegion, acquireFenceHandle,
                [&](const auto& tmpError, const auto& tmpPtr) {
                    if (tmpError == MapperErrorV2::NONE) {
                        ret = tmpPtr;
                    } else {
                        ALOGE("%s: failed to lock error %d!", __FUNCTION__, tmpError);
                    }
               });
    }

    ALOGV("%s: ptr %p accessRegion.top: %d accessRegion.left: %d accessRegion.width: %d "
          "accessRegion.height: %d",
          __FUNCTION__, ret, accessRegion.top, accessRegion.left, accessRegion.width,
          accessRegion.height);
    return ret;
}

YCbCrLayout HandleImporter::lockYCbCr(
        buffer_handle_t& buf, uint64_t cpuUsage,
        const IMapper::Rect& accessRegion) {
    Mutex::Autolock lock(mLock);

    if (!mInitialized) {
        initializeLocked();
    }

    if (mMapperV4 != nullptr) {
        return lockYCbCrInternal<IMapperV4, MapperErrorV4>(mMapperV4, buf, cpuUsage, accessRegion);
    }

    if (mMapperV3 != nullptr) {
        return lockYCbCrInternal<IMapperV3, MapperErrorV3>(
                mMapperV3, buf, cpuUsage, accessRegion);
    }

    if (mMapperV2 != nullptr) {
        return lockYCbCrInternal<IMapper, MapperErrorV2>(
                mMapperV2, buf, cpuUsage, accessRegion);
    }

    ALOGE("%s: mMapperV4, mMapperV3 and mMapperV2 are all null!", __FUNCTION__);
    return {};
}

status_t HandleImporter::getMonoPlanarStrideBytes(buffer_handle_t &buf, uint32_t *stride /*out*/) {
    if (stride == nullptr) {
        return BAD_VALUE;
    }

    Mutex::Autolock lock(mLock);

    if (!mInitialized) {
        initializeLocked();
    }

    if (mMapperV4 != nullptr) {
        std::vector<PlaneLayout> planeLayouts = getPlaneLayouts(mMapperV4, buf);
        if (planeLayouts.size() != 1) {
            ALOGE("%s: Unexpected number of planes %zu!",  __FUNCTION__, planeLayouts.size());
            return BAD_VALUE;
        }

        *stride = planeLayouts[0].strideInBytes;
    } else {
        ALOGE("%s: mMapperV4 is null! Query not supported!", __FUNCTION__);
        return NO_INIT;
    }

    return OK;
}

int HandleImporter::unlock(buffer_handle_t& buf) {
    if (mMapperV4 != nullptr) {
        return unlockInternal<IMapperV4, MapperErrorV4>(mMapperV4, buf);
    }
    if (mMapperV3 != nullptr) {
        return unlockInternal<IMapperV3, MapperErrorV3>(mMapperV3, buf);
    }
    if (mMapperV2 != nullptr) {
        return unlockInternal<IMapper, MapperErrorV2>(mMapperV2, buf);
    }

    ALOGE("%s: mMapperV4, mMapperV3 and mMapperV2 are all null!", __FUNCTION__);
    return -1;
}

bool HandleImporter::isSmpte2086Present(const buffer_handle_t& buf) {
    Mutex::Autolock lock(mLock);

    if (!mInitialized) {
        initializeLocked();
    }

    if (mMapperV4 != nullptr) {
        return isMetadataPesent(mMapperV4, buf, gralloc4::MetadataType_Smpte2086);
    } else {
        ALOGE("%s: mMapperV4 is null! Query not supported!", __FUNCTION__);
    }

    return false;
}

bool HandleImporter::isSmpte2094_10Present(const buffer_handle_t& buf) {
    Mutex::Autolock lock(mLock);

    if (!mInitialized) {
        initializeLocked();
    }

    if (mMapperV4 != nullptr) {
        return isMetadataPesent(mMapperV4, buf, gralloc4::MetadataType_Smpte2094_10);
    } else {
        ALOGE("%s: mMapperV4 is null! Query not supported!", __FUNCTION__);
    }

    return false;
}

bool HandleImporter::isSmpte2094_40Present(const buffer_handle_t& buf) {
    Mutex::Autolock lock(mLock);

    if (!mInitialized) {
        initializeLocked();
    }

    if (mMapperV4 != nullptr) {
        return isMetadataPesent(mMapperV4, buf, gralloc4::MetadataType_Smpte2094_40);
    } else {
        ALOGE("%s: mMapperV4 is null! Query not supported!", __FUNCTION__);
    }

    return false;
}


} // namespace helper
} // namespace V1_0
} // namespace common
} // namespace camera
} // namespace hardware
} // namespace android