blob: f797f9ce3e63e41d47a741de0a2fb686be331241 [file] [log] [blame]
Emilian Peev538c90e2018-12-17 18:03:19 +00001/*
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
Emilian Peevcbf174b2019-01-25 14:38:59 -080020#include "common/DepthPhotoProcessor.h"
Emilian Peev538c90e2018-12-17 18:03:19 +000021#include <dynamic_depth/imaging_model.h>
22#include <dynamic_depth/depth_map.h>
23
Emilian Peev538c90e2018-12-17 18:03:19 +000024#include <gui/CpuConsumer.h>
25
26#include "CompositeStream.h"
27
28using dynamic_depth::DepthMap;
29using dynamic_depth::Item;
30using dynamic_depth::ImagingModel;
31
32namespace android {
33
34class CameraDeviceClient;
35class CameraMetadata;
36class Surface;
37
38namespace camera3 {
39
40class DepthCompositeStream : public CompositeStream, public Thread,
41 public CpuConsumer::FrameAvailableListener {
42
43public:
Shuzhen Wange8675782019-12-05 09:12:14 -080044 DepthCompositeStream(sp<CameraDeviceBase> device,
Emilian Peev538c90e2018-12-17 18:03:19 +000045 wp<hardware::camera2::ICameraDeviceCallbacks> cb);
46 ~DepthCompositeStream() override;
47
48 static bool isDepthCompositeStream(const sp<Surface> &surface);
49
50 // CompositeStream overrides
51 status_t createInternalStreams(const std::vector<sp<Surface>>& consumers,
52 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
Austin Borger1c1bee02023-06-01 16:51:35 -070053 camera_stream_rotation_t rotation, int *id, const std::string& physicalCameraId,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080054 const std::unordered_set<int32_t> &sensorPixelModesUsed,
55 std::vector<int> *surfaceIds,
Emilian Peev434248e2022-10-06 14:58:54 -070056 int streamSetId, bool isShared, int32_t colorSpace,
Shuzhen Wangbce53db2022-12-03 00:38:20 +000057 int64_t dynamicProfile, int64_t streamUseCase, bool useReadoutTimestamp) override;
Emilian Peev538c90e2018-12-17 18:03:19 +000058 status_t deleteInternalStreams() override;
59 status_t configureStream() override;
60 status_t insertGbp(SurfaceMap* /*out*/outSurfaceMap, Vector<int32_t>* /*out*/outputStreamIds,
61 int32_t* /*out*/currentStreamId) override;
Emilian Peev4697b642019-11-19 17:11:14 -080062 status_t insertCompositeStreamIds(std::vector<int32_t>* compositeStreamIds /*out*/) override;
Emilian Peev538c90e2018-12-17 18:03:19 +000063 int getStreamId() override { return mBlobStreamId; }
64
65 // CpuConsumer listener implementation
66 void onFrameAvailable(const BufferItem& item) override;
67
68 // Return stream information about the internal camera streams
69 static status_t getCompositeStreamInfo(const OutputStreamInfo &streamInfo,
70 const CameraMetadata& ch, std::vector<OutputStreamInfo>* compositeOutput /*out*/);
71
Emilian Peev567c31c2023-03-06 15:02:37 -080072 // Get composite stream stats
73 void getStreamStats(hardware::CameraStreamStats*) override {};
74
Emilian Peev538c90e2018-12-17 18:03:19 +000075protected:
76
77 bool threadLoop() override;
78 bool onStreamBufferError(const CaptureResultExtras& resultExtras) override;
79 void onResultError(const CaptureResultExtras& resultExtras) override;
80
81private:
82 struct InputFrame {
83 CpuConsumer::LockedBuffer depthBuffer;
84 CpuConsumer::LockedBuffer jpegBuffer;
85 CameraMetadata result;
86 bool error;
87 bool errorNotified;
88 int64_t frameNumber;
Shuzhen Wange8675782019-12-05 09:12:14 -080089 int32_t requestId;
Emilian Peev538c90e2018-12-17 18:03:19 +000090
Shuzhen Wange8675782019-12-05 09:12:14 -080091 InputFrame() : error(false), errorNotified(false), frameNumber(-1), requestId(-1) { }
Emilian Peev538c90e2018-12-17 18:03:19 +000092 };
93
94 // Helper methods
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080095 static void getSupportedDepthSizes(const CameraMetadata& ch, bool maxResolution,
Emilian Peev538c90e2018-12-17 18:03:19 +000096 std::vector<std::tuple<size_t, size_t>>* depthSizes /*out*/);
97 static status_t getMatchingDepthSize(size_t width, size_t height,
98 const std::vector<std::tuple<size_t, size_t>>& supporedDepthSizes,
99 size_t *depthWidth /*out*/, size_t *depthHeight /*out*/);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800100 static status_t checkAndGetMatchingDepthSize(size_t width, size_t height,
101 const std::vector<std::tuple<size_t, size_t>> &depthSizes,
102 const std::vector<std::tuple<size_t, size_t>> &depthSizesMaximumResolution,
103 const std::unordered_set<int32_t> &sensorPixelModesUsed,
104 size_t *depthWidth /*out*/, size_t *depthHeight /*out*/);
105
Emilian Peev538c90e2018-12-17 18:03:19 +0000106
107 // Dynamic depth processing
108 status_t encodeGrayscaleJpeg(size_t width, size_t height, uint8_t *in, void *out,
109 const size_t maxOutSize, uint8_t jpegQuality, size_t &actualSize);
110 std::unique_ptr<DepthMap> processDepthMapFrame(const CpuConsumer::LockedBuffer &depthMapBuffer,
111 size_t maxJpegSize, uint8_t jpegQuality,
112 std::vector<std::unique_ptr<Item>>* items /*out*/);
113 std::unique_ptr<ImagingModel> getImagingModel();
Emilian Peev90a839f2019-10-02 15:12:50 -0700114 status_t processInputFrame(nsecs_t ts, const InputFrame &inputFrame);
Emilian Peev538c90e2018-12-17 18:03:19 +0000115
116 // Buffer/Results handling
117 void compilePendingInputLocked();
118 void releaseInputFrameLocked(InputFrame *inputFrame /*out*/);
119 void releaseInputFramesLocked(int64_t currentTs);
120
121 // Find first complete and valid frame with smallest timestamp
122 bool getNextReadyInputLocked(int64_t *currentTs /*inout*/);
123
124 // Find next failing frame number with smallest timestamp and return respective frame number
125 int64_t getNextFailingInputLocked(int64_t *currentTs /*inout*/);
126
127 static const nsecs_t kWaitDuration = 10000000; // 10 ms
128 static const auto kDepthMapPixelFormat = HAL_PIXEL_FORMAT_Y16;
129 static const auto kDepthMapDataSpace = HAL_DATASPACE_DEPTH;
130 static const auto kJpegDataSpace = HAL_DATASPACE_V0_JFIF;
131
Emilian Peev538c90e2018-12-17 18:03:19 +0000132 int mBlobStreamId, mBlobSurfaceId, mDepthStreamId, mDepthSurfaceId;
133 size_t mBlobWidth, mBlobHeight;
134 sp<CpuConsumer> mBlobConsumer, mDepthConsumer;
135 bool mDepthBufferAcquired, mBlobBufferAcquired;
136 sp<Surface> mDepthSurface, mBlobSurface, mOutputSurface;
137 sp<ProducerListener> mProducerListener;
138
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700139 ssize_t mMaxJpegBufferSize;
140 ssize_t mUHRMaxJpegBufferSize;
141
142 camera3::Size mDefaultMaxJpegSize;
143 camera3::Size mUHRMaxJpegSize;
144
Emilian Peev538c90e2018-12-17 18:03:19 +0000145 std::vector<std::tuple<size_t, size_t>> mSupportedDepthSizes;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800146 std::vector<std::tuple<size_t, size_t>> mSupportedDepthSizesMaximumResolution;
Emilian Peev94c98022019-06-19 09:11:51 -0700147 std::vector<float> mIntrinsicCalibration, mLensDistortion;
Emilian Peev538c90e2018-12-17 18:03:19 +0000148 bool mIsLogicalCamera;
Emilian Peev538c90e2018-12-17 18:03:19 +0000149
150 // Keep all incoming Depth buffer timestamps pending further processing.
151 std::vector<int64_t> mInputDepthBuffers;
152
153 // Keep all incoming Jpeg/Blob buffer timestamps pending further processing.
154 std::vector<int64_t> mInputJpegBuffers;
155
156 // Map of all input frames pending further processing.
157 std::unordered_map<int64_t, InputFrame> mPendingInputFrames;
158};
159
160}; //namespace camera3
161}; //namespace android
162
163#endif