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 #ifndef VENDOR_GOOGLE_CAMERA_SENSOR_LISTENER_GOOG_GRALLOC_WRAPPER_H_
18 #define VENDOR_GOOGLE_CAMERA_SENSOR_LISTENER_GOOG_GRALLOC_WRAPPER_H_
19 
20 #include <unordered_set>
21 
22 #include <android/frameworks/sensorservice/1.0/ISensorManager.h>
23 #include <android/hardware/graphics/allocator/3.0/IAllocator.h>
24 #include <android/hardware/graphics/mapper/3.0/IMapper.h>
25 #include <sys/mman.h>
26 #include <utils/StrongPointer.h>
27 
28 namespace android {
29 namespace camera_sensor_listener {
30 
31 // GoogGrallocWrapper is a wrapper class to
32 // ::android::hardware::graphics::allocator::V3_0::IAllocator and
33 // ::android::hardware::graphics::mapper::V3_0::IMapper.
34 // It can used by direct channel based sensor listener class.
35 // Modified from //hardware/interfaces/graphics/mapper/2.0/utils/vts
36 // /include/mapper-vts/2.0/MapperVts.h.
37 class GoogGrallocWrapper {
38  public:
39   // Constructor.
40   GoogGrallocWrapper();
41 
42   // Destructor.
43   ~GoogGrallocWrapper();
44 
45   // Get StrongPointer to IAllocator.
46   sp<::android::hardware::graphics::allocator::V3_0::IAllocator> GetAllocator()
47       const;
48 
49   // Wrapper of IAllocator dumpDebugInfo method.
50   std::string DumpDebugInfo() const;
51 
52   // Wrapper of IAllocator allocate method.
53   std::vector<const native_handle_t*> Allocate(
54       const ::android::hardware::graphics::mapper::V3_0::BufferDescriptor&
55           descriptor,
56       uint32_t count, bool import = true, uint32_t* out_stride = nullptr);
57 
58   // Special case of Allocate, where allocated buffer count is 1.
59   const native_handle_t* AllocateOneBuffer(
60       const ::android::hardware::graphics::mapper::V3_0::IMapper::
61           BufferDescriptorInfo& descriptor_info,
62       bool import = true, uint32_t* out_stride = nullptr);
63 
64   // Get StrongPointer to IMapper.
65   sp<::android::hardware::graphics::mapper::V3_0::IMapper> GetMapper() const;
66 
67   // Wrapper of IMapper createDescriptor method.
68   ::android::hardware::graphics::mapper::V3_0::BufferDescriptor CreateDescriptor(
69       const ::android::hardware::graphics::mapper::V3_0::IMapper::
70           BufferDescriptorInfo& descriptor_info);
71 
72   // Wrapper of IMapper importBuffer method.
73   const native_handle_t* ImportBuffer(
74       const ::android::hardware::hidl_handle& raw_handle);
75 
76   // Wrapper of IMapper freeBuffer method.
77   void FreeBuffer(const native_handle_t* buffer_handle);
78 
79   // Wrapper of Imapper lock method.
80   // We use fd instead of hardware::hidl_handle in these functions to pass
81   // fences in and out of the mapper.  The ownership of the fd is always
82   // transferred with each of these functions.
83   void* Lock(const native_handle_t* buffer_handle, uint64_t cpu_usage,
84              const ::android::hardware::graphics::mapper::V3_0::IMapper::Rect&
85                  access_region,
86              int acquire_fence);
87 
88   // Wrapper of Imapper unlock method.
89   int Unlock(const native_handle_t* buffer_handle);
90 
91  private:
92   const native_handle_t* CloneBuffer(const hardware::hidl_handle& raw_handle);
93 
94   // StrongPointer to IAllocator.
95   sp<::android::hardware::graphics::allocator::V3_0::IAllocator> allocator_;
96 
97   // StrongPointer to IMapper.
98   sp<::android::hardware::graphics::mapper::V3_0::IMapper> mapper_;
99 
100   // Set of cloned buffer handles.
101   // Keep track of all cloned and imported handles.  When a test fails with
102   // ASSERT_*, the destructor will free the handles for the test.
103   std::unordered_set<const native_handle_t*> cloned_buffers_;
104 
105   // Set of imported buffer handles.
106   std::unordered_set<const native_handle_t*> imported_buffers_;
107 };
108 
109 }  // namespace camera_sensor_listener
110 }  // namespace android
111 
112 #endif  // VENDOR_GOOGLE_CAMERA_SENSOR_LISTENER_GOOG_GRALLOC_WRAPPER_H_
113