1 /*
2  * Copyright (C) 2017 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 #ifndef CAMERA_COMMON_1_0_HANDLEIMPORTED_H
18 #define CAMERA_COMMON_1_0_HANDLEIMPORTED_H
19 
20 #include <android/hardware/graphics/mapper/2.0/IMapper.h>
21 #include <android/hardware/graphics/mapper/3.0/IMapper.h>
22 #include <android/hardware/graphics/mapper/4.0/IMapper.h>
23 #include <cutils/native_handle.h>
24 #include <utils/Mutex.h>
25 
26 using android::hardware::graphics::mapper::V2_0::IMapper;
27 using android::hardware::graphics::mapper::V2_0::YCbCrLayout;
28 
29 namespace android {
30 namespace hardware {
31 namespace camera {
32 namespace common {
33 namespace V1_0 {
34 namespace helper {
35 
36 // Borrowed from graphics HAL. Use this until gralloc mapper HAL is working
37 class HandleImporter {
38 public:
39     HandleImporter();
40 
41     // In IComposer, any buffer_handle_t is owned by the caller and we need to
42     // make a clone for hwcomposer2.  We also need to translate empty handle
43     // to nullptr.  This function does that, in-place.
44     bool importBuffer(buffer_handle_t& handle);
45     void freeBuffer(buffer_handle_t handle);
46     bool importFence(const native_handle_t* handle, int& fd) const;
47     void closeFence(int fd) const;
48 
49     // Locks 1-D buffer. Assumes caller has waited for acquire fences.
50     void* lock(buffer_handle_t& buf, uint64_t cpuUsage, size_t size);
51 
52     // Locks 2-D buffer. Assumes caller has waited for acquire fences.
53     void* lock(buffer_handle_t& buf, uint64_t cpuUsage, const IMapper::Rect& accessRegion);
54 
55     // Assumes caller has waited for acquire fences.
56     YCbCrLayout lockYCbCr(buffer_handle_t& buf, uint64_t cpuUsage,
57                           const IMapper::Rect& accessRegion);
58 
59     int unlock(buffer_handle_t& buf); // returns release fence
60 
61 private:
62     void initializeLocked();
63     void cleanup();
64 
65     template<class M, class E>
66     bool importBufferInternal(const sp<M> mapper, buffer_handle_t& handle);
67     template<class M, class E>
68     YCbCrLayout lockYCbCrInternal(const sp<M> mapper, buffer_handle_t& buf, uint64_t cpuUsage,
69             const IMapper::Rect& accessRegion);
70     template<class M, class E>
71     int unlockInternal(const sp<M> mapper, buffer_handle_t& buf);
72 
73     Mutex mLock;
74     bool mInitialized;
75     sp<IMapper> mMapperV2;
76     sp<graphics::mapper::V3_0::IMapper> mMapperV3;
77     sp<graphics::mapper::V4_0::IMapper> mMapperV4;
78 };
79 
80 } // namespace helper
81 } // namespace V1_0
82 } // namespace common
83 } // namespace camera
84 } // namespace hardware
85 } // namespace android
86 
87 #endif // CAMERA_COMMON_1_0_HANDLEIMPORTED_H
88