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 ANDROID_HARDWARE_CAMERA_DEVICE_V3_4_EXTCAMUTIL_H
18 #define ANDROID_HARDWARE_CAMERA_DEVICE_V3_4_EXTCAMUTIL_H
19 
20 #include <android/hardware/graphics/mapper/2.0/IMapper.h>
21 #include <inttypes.h>
22 #include <mutex>
23 #include <unordered_set>
24 #include <vector>
25 #include "tinyxml2.h"  // XML parsing
26 #include "utils/LightRefBase.h"
27 
28 using android::hardware::graphics::mapper::V2_0::IMapper;
29 using android::hardware::graphics::mapper::V2_0::YCbCrLayout;
30 
31 namespace android {
32 namespace hardware {
33 namespace camera {
34 
35 namespace external {
36 namespace common {
37 
38 struct Size {
39     uint32_t width;
40     uint32_t height;
41 
42     bool operator==(const Size& other) const {
43         return (width == other.width && height == other.height);
44     }
45 };
46 
47 struct SizeHasher {
operatorSizeHasher48     size_t operator()(const Size& sz) const {
49         size_t result = 1;
50         result = 31 * result + sz.width;
51         result = 31 * result + sz.height;
52         return result;
53     }
54 };
55 
56 struct ExternalCameraConfig {
57     static const char* kDefaultCfgPath;
58     static ExternalCameraConfig loadFromCfg(const char* cfgPath = kDefaultCfgPath);
59 
60     // List of internal V4L2 video nodes external camera HAL must ignore.
61     std::unordered_set<std::string> mInternalDevices;
62 
63     // Maximal size of a JPEG buffer, in bytes
64     uint32_t maxJpegBufSize;
65 
66     // Maximum Size that can sustain 30fps streaming
67     Size maxVideoSize;
68 
69     // Size of v4l2 buffer queue when streaming <= kMaxVideoSize
70     uint32_t numVideoBuffers;
71 
72     // Size of v4l2 buffer queue when streaming > kMaxVideoSize
73     uint32_t numStillBuffers;
74 
75     // Indication that the device connected supports depth output
76     bool depthEnabled;
77 
78     struct FpsLimitation {
79         Size size;
80         double fpsUpperBound;
81     };
82     std::vector<FpsLimitation> fpsLimits;
83     std::vector<FpsLimitation> depthFpsLimits;
84 
85     // Minimum output stream size
86     Size minStreamSize;
87 
88     // The value of android.sensor.orientation
89     int32_t orientation;
90 
91 private:
92     ExternalCameraConfig();
93     static bool updateFpsList(tinyxml2::XMLElement* fpsList, std::vector<FpsLimitation>& fpsLimits);
94 };
95 
96 } // common
97 } // external
98 
99 namespace device {
100 namespace V3_4 {
101 namespace implementation {
102 
103 struct SupportedV4L2Format {
104     uint32_t width;
105     uint32_t height;
106     uint32_t fourcc;
107     // All supported frame rate for this w/h/fourcc combination
108     struct FrameRate {
109         uint32_t durationNumerator;   // frame duration numerator.   Ex: 1
110         uint32_t durationDenominator; // frame duration denominator. Ex: 30
111         double getDouble() const;     // FrameRate in double.        Ex: 30.0
112     };
113     std::vector<FrameRate> frameRates;
114 };
115 
116 // A class provide access to a dequeued V4L2 frame buffer (mostly in MJPG format)
117 // Also contains necessary information to enqueue the buffer back to V4L2 buffer queue
118 class V4L2Frame : public virtual VirtualLightRefBase {
119 public:
120     V4L2Frame(uint32_t w, uint32_t h, uint32_t fourcc, int bufIdx, int fd,
121               uint32_t dataSize, uint64_t offset);
122     ~V4L2Frame() override;
123     const uint32_t mWidth;
124     const uint32_t mHeight;
125     const uint32_t mFourcc;
126     const int mBufferIndex; // for later enqueue
127     int map(uint8_t** data, size_t* dataSize);
128     int unmap();
129 private:
130     std::mutex mLock;
131     const int mFd; // used for mmap but doesn't claim ownership
132     const size_t mDataSize;
133     const uint64_t mOffset; // used for mmap
134     uint8_t* mData = nullptr;
135     bool  mMapped = false;
136 };
137 
138 // A RAII class representing a CPU allocated YUV frame used as intermeidate buffers
139 // when generating output images.
140 class AllocatedFrame : public virtual VirtualLightRefBase {
141 public:
142     AllocatedFrame(uint32_t w, uint32_t h); // TODO: use Size?
143     ~AllocatedFrame() override;
144     const uint32_t mWidth;
145     const uint32_t mHeight;
146     const uint32_t mFourcc; // Only support YU12 format for now
147     int allocate(YCbCrLayout* out = nullptr);
148     int getLayout(YCbCrLayout* out);
149     int getCroppedLayout(const IMapper::Rect&, YCbCrLayout* out); // return non-zero for bad input
150 private:
151     std::mutex mLock;
152     std::vector<uint8_t> mData;
153 };
154 
155 enum CroppingType {
156     HORIZONTAL = 0,
157     VERTICAL = 1
158 };
159 
160 // Aspect ratio is defined as width/height here and ExternalCameraDevice
161 // will guarantee all supported sizes has width >= height (so aspect ratio >= 1.0)
162 #define ASPECT_RATIO(sz) (static_cast<float>((sz).width) / (sz).height)
163 const float kMaxAspectRatio = std::numeric_limits<float>::max();
164 const float kMinAspectRatio = 1.f;
165 
166 bool isAspectRatioClose(float ar1, float ar2);
167 
168 }  // namespace implementation
169 }  // namespace V3_4
170 }  // namespace device
171 }  // namespace camera
172 }  // namespace hardware
173 }  // namespace android
174 
175 #endif  // ANDROID_HARDWARE_CAMERA_DEVICE_V3_4_EXTCAMUTIL_H
176