blob: cab52b6c722c002ec5dd221f277ca61cc6ce0a96 [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,
53 camera3_stream_rotation_t rotation, int *id, const String8& physicalCameraId,
54 std::vector<int> *surfaceIds, int streamSetId, bool isShared) override;
55 status_t deleteInternalStreams() override;
56 status_t configureStream() override;
57 status_t insertGbp(SurfaceMap* /*out*/outSurfaceMap, Vector<int32_t>* /*out*/outputStreamIds,
58 int32_t* /*out*/currentStreamId) override;
Emilian Peev4697b642019-11-19 17:11:14 -080059 status_t insertCompositeStreamIds(std::vector<int32_t>* compositeStreamIds /*out*/) override;
Emilian Peev538c90e2018-12-17 18:03:19 +000060 int getStreamId() override { return mBlobStreamId; }
61
62 // CpuConsumer listener implementation
63 void onFrameAvailable(const BufferItem& item) override;
64
65 // Return stream information about the internal camera streams
66 static status_t getCompositeStreamInfo(const OutputStreamInfo &streamInfo,
67 const CameraMetadata& ch, std::vector<OutputStreamInfo>* compositeOutput /*out*/);
68
69protected:
70
71 bool threadLoop() override;
72 bool onStreamBufferError(const CaptureResultExtras& resultExtras) override;
73 void onResultError(const CaptureResultExtras& resultExtras) override;
74
75private:
76 struct InputFrame {
77 CpuConsumer::LockedBuffer depthBuffer;
78 CpuConsumer::LockedBuffer jpegBuffer;
79 CameraMetadata result;
80 bool error;
81 bool errorNotified;
82 int64_t frameNumber;
Shuzhen Wange8675782019-12-05 09:12:14 -080083 int32_t requestId;
Emilian Peev538c90e2018-12-17 18:03:19 +000084
Shuzhen Wange8675782019-12-05 09:12:14 -080085 InputFrame() : error(false), errorNotified(false), frameNumber(-1), requestId(-1) { }
Emilian Peev538c90e2018-12-17 18:03:19 +000086 };
87
88 // Helper methods
89 static void getSupportedDepthSizes(const CameraMetadata& ch,
90 std::vector<std::tuple<size_t, size_t>>* depthSizes /*out*/);
91 static status_t getMatchingDepthSize(size_t width, size_t height,
92 const std::vector<std::tuple<size_t, size_t>>& supporedDepthSizes,
93 size_t *depthWidth /*out*/, size_t *depthHeight /*out*/);
94
95 // Dynamic depth processing
96 status_t encodeGrayscaleJpeg(size_t width, size_t height, uint8_t *in, void *out,
97 const size_t maxOutSize, uint8_t jpegQuality, size_t &actualSize);
98 std::unique_ptr<DepthMap> processDepthMapFrame(const CpuConsumer::LockedBuffer &depthMapBuffer,
99 size_t maxJpegSize, uint8_t jpegQuality,
100 std::vector<std::unique_ptr<Item>>* items /*out*/);
101 std::unique_ptr<ImagingModel> getImagingModel();
Emilian Peev90a839f2019-10-02 15:12:50 -0700102 status_t processInputFrame(nsecs_t ts, const InputFrame &inputFrame);
Emilian Peev538c90e2018-12-17 18:03:19 +0000103
104 // Buffer/Results handling
105 void compilePendingInputLocked();
106 void releaseInputFrameLocked(InputFrame *inputFrame /*out*/);
107 void releaseInputFramesLocked(int64_t currentTs);
108
109 // Find first complete and valid frame with smallest timestamp
110 bool getNextReadyInputLocked(int64_t *currentTs /*inout*/);
111
112 // Find next failing frame number with smallest timestamp and return respective frame number
113 int64_t getNextFailingInputLocked(int64_t *currentTs /*inout*/);
114
115 static const nsecs_t kWaitDuration = 10000000; // 10 ms
116 static const auto kDepthMapPixelFormat = HAL_PIXEL_FORMAT_Y16;
117 static const auto kDepthMapDataSpace = HAL_DATASPACE_DEPTH;
118 static const auto kJpegDataSpace = HAL_DATASPACE_V0_JFIF;
119
Emilian Peev538c90e2018-12-17 18:03:19 +0000120 int mBlobStreamId, mBlobSurfaceId, mDepthStreamId, mDepthSurfaceId;
121 size_t mBlobWidth, mBlobHeight;
122 sp<CpuConsumer> mBlobConsumer, mDepthConsumer;
123 bool mDepthBufferAcquired, mBlobBufferAcquired;
124 sp<Surface> mDepthSurface, mBlobSurface, mOutputSurface;
125 sp<ProducerListener> mProducerListener;
126
127 ssize_t mMaxJpegSize;
128 std::vector<std::tuple<size_t, size_t>> mSupportedDepthSizes;
Emilian Peev94c98022019-06-19 09:11:51 -0700129 std::vector<float> mIntrinsicCalibration, mLensDistortion;
Emilian Peev538c90e2018-12-17 18:03:19 +0000130 bool mIsLogicalCamera;
Emilian Peev538c90e2018-12-17 18:03:19 +0000131
132 // Keep all incoming Depth buffer timestamps pending further processing.
133 std::vector<int64_t> mInputDepthBuffers;
134
135 // Keep all incoming Jpeg/Blob buffer timestamps pending further processing.
136 std::vector<int64_t> mInputJpegBuffers;
137
138 // Map of all input frames pending further processing.
139 std::unordered_map<int64_t, InputFrame> mPendingInputFrames;
140};
141
142}; //namespace camera3
143}; //namespace android
144
145#endif