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_SERVERS_CAMERA_CAMERA3_DEPTH_COMPOSITE_STREAM_H
18 #define ANDROID_SERVERS_CAMERA_CAMERA3_DEPTH_COMPOSITE_STREAM_H
19 
20 #include "common/DepthPhotoProcessor.h"
21 #include <dynamic_depth/imaging_model.h>
22 #include <dynamic_depth/depth_map.h>
23 
24 #include <gui/CpuConsumer.h>
25 
26 #include "CompositeStream.h"
27 
28 using dynamic_depth::DepthMap;
29 using dynamic_depth::Item;
30 using dynamic_depth::ImagingModel;
31 
32 namespace android {
33 
34 class CameraDeviceClient;
35 class CameraMetadata;
36 class Surface;
37 
38 namespace camera3 {
39 
40 class DepthCompositeStream : public CompositeStream, public Thread,
41         public CpuConsumer::FrameAvailableListener {
42 
43 public:
44     DepthCompositeStream(sp<CameraDeviceBase> device,
45             wp<hardware::camera2::ICameraDeviceCallbacks> cb);
46     ~DepthCompositeStream() override;
47 
48     static bool isDepthCompositeStream(const sp<Surface> &surface);
49     static bool isDepthCompositeStreamInfo(const OutputStreamInfo& streamInfo);
50 
51     // CompositeStream overrides
52     status_t createInternalStreams(const std::vector<sp<Surface>>& consumers,
53             bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
54             camera_stream_rotation_t rotation, int *id, const std::string& physicalCameraId,
55             const std::unordered_set<int32_t> &sensorPixelModesUsed,
56             std::vector<int> *surfaceIds,
57             int streamSetId, bool isShared, int32_t colorSpace,
58             int64_t dynamicProfile, int64_t streamUseCase, bool useReadoutTimestamp) override;
59     status_t deleteInternalStreams() override;
60     status_t configureStream() override;
61     status_t insertGbp(SurfaceMap* /*out*/outSurfaceMap, Vector<int32_t>* /*out*/outputStreamIds,
62             int32_t* /*out*/currentStreamId) override;
63     status_t insertCompositeStreamIds(std::vector<int32_t>* compositeStreamIds /*out*/) override;
getStreamId()64     int getStreamId() override { return mBlobStreamId; }
65 
66     // CpuConsumer listener implementation
67     void onFrameAvailable(const BufferItem& item) override;
68 
69     // Return stream information about the internal camera streams
70     static status_t getCompositeStreamInfo(const OutputStreamInfo &streamInfo,
71             const CameraMetadata& ch, std::vector<OutputStreamInfo>* compositeOutput /*out*/);
72 
73     // Get composite stream stats
getStreamStats(hardware::CameraStreamStats *)74     void getStreamStats(hardware::CameraStreamStats*) override {};
75 
76 protected:
77 
78     bool threadLoop() override;
79     bool onStreamBufferError(const CaptureResultExtras& resultExtras) override;
80     void onResultError(const CaptureResultExtras& resultExtras) override;
81 
82 private:
83     struct InputFrame {
84         CpuConsumer::LockedBuffer depthBuffer;
85         CpuConsumer::LockedBuffer jpegBuffer;
86         CameraMetadata            result;
87         bool                      error;
88         bool                      errorNotified;
89         int64_t                   frameNumber;
90         int32_t                   requestId;
91 
InputFrameInputFrame92         InputFrame() : error(false), errorNotified(false), frameNumber(-1), requestId(-1) { }
93     };
94 
95     // Helper methods
96     static void getSupportedDepthSizes(const CameraMetadata& ch, bool maxResolution,
97             std::vector<std::tuple<size_t, size_t>>* depthSizes /*out*/);
98     static status_t getMatchingDepthSize(size_t width, size_t height,
99             const std::vector<std::tuple<size_t, size_t>>& supporedDepthSizes,
100             size_t *depthWidth /*out*/, size_t *depthHeight /*out*/);
101     static status_t checkAndGetMatchingDepthSize(size_t width, size_t height,
102         const std::vector<std::tuple<size_t, size_t>> &depthSizes,
103         const std::vector<std::tuple<size_t, size_t>> &depthSizesMaximumResolution,
104         const std::unordered_set<int32_t> &sensorPixelModesUsed,
105         size_t *depthWidth /*out*/, size_t *depthHeight /*out*/);
106 
107 
108     // Dynamic depth processing
109     status_t encodeGrayscaleJpeg(size_t width, size_t height, uint8_t *in, void *out,
110             const size_t maxOutSize, uint8_t jpegQuality, size_t &actualSize);
111     std::unique_ptr<DepthMap> processDepthMapFrame(const CpuConsumer::LockedBuffer &depthMapBuffer,
112             size_t maxJpegSize, uint8_t jpegQuality,
113             std::vector<std::unique_ptr<Item>>* items /*out*/);
114     std::unique_ptr<ImagingModel> getImagingModel();
115     status_t processInputFrame(nsecs_t ts, const InputFrame &inputFrame);
116 
117     // Buffer/Results handling
118     void compilePendingInputLocked();
119     void releaseInputFrameLocked(InputFrame *inputFrame /*out*/);
120     void releaseInputFramesLocked(int64_t currentTs);
121 
122     // Find first complete and valid frame with smallest timestamp
123     bool getNextReadyInputLocked(int64_t *currentTs /*inout*/);
124 
125     // Find next failing frame number with smallest timestamp and return respective frame number
126     int64_t getNextFailingInputLocked(int64_t *currentTs /*inout*/);
127 
128     static const nsecs_t kWaitDuration = 10000000; // 10 ms
129     static const auto kDepthMapPixelFormat = HAL_PIXEL_FORMAT_Y16;
130     static const auto kDepthMapDataSpace = HAL_DATASPACE_DEPTH;
131     static const auto kJpegDataSpace = HAL_DATASPACE_V0_JFIF;
132 
133     int                  mBlobStreamId, mBlobSurfaceId, mDepthStreamId, mDepthSurfaceId;
134     size_t               mBlobWidth, mBlobHeight;
135     sp<CpuConsumer>      mBlobConsumer, mDepthConsumer;
136     bool                 mDepthBufferAcquired, mBlobBufferAcquired;
137     sp<Surface>          mDepthSurface, mBlobSurface, mOutputSurface;
138     sp<ProducerListener> mProducerListener;
139 
140     ssize_t              mMaxJpegBufferSize;
141     ssize_t              mUHRMaxJpegBufferSize;
142 
143     camera3::Size        mDefaultMaxJpegSize;
144     camera3::Size        mUHRMaxJpegSize;
145 
146     std::vector<std::tuple<size_t, size_t>> mSupportedDepthSizes;
147     std::vector<std::tuple<size_t, size_t>> mSupportedDepthSizesMaximumResolution;
148     std::vector<float>   mIntrinsicCalibration, mLensDistortion;
149     bool                 mIsLogicalCamera;
150 
151     // Keep all incoming Depth buffer timestamps pending further processing.
152     std::vector<int64_t> mInputDepthBuffers;
153 
154     // Keep all incoming Jpeg/Blob buffer timestamps pending further processing.
155     std::vector<int64_t> mInputJpegBuffers;
156 
157     // Map of all input frames pending further processing.
158     std::unordered_map<int64_t, InputFrame> mPendingInputFrames;
159 };
160 
161 }; //namespace camera3
162 }; //namespace android
163 
164 #endif
165