blob: e8fe517e13b175d34189a865548860aa39040c6e [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
24#include <gui/IProducerListener.h>
25#include <gui/CpuConsumer.h>
26
27#include "CompositeStream.h"
28
29using dynamic_depth::DepthMap;
30using dynamic_depth::Item;
31using dynamic_depth::ImagingModel;
32
33namespace android {
34
35class CameraDeviceClient;
36class CameraMetadata;
37class Surface;
38
39namespace camera3 {
40
41class DepthCompositeStream : public CompositeStream, public Thread,
42 public CpuConsumer::FrameAvailableListener {
43
44public:
45 DepthCompositeStream(wp<CameraDeviceBase> device,
46 wp<hardware::camera2::ICameraDeviceCallbacks> cb);
47 ~DepthCompositeStream() override;
48
49 static bool isDepthCompositeStream(const sp<Surface> &surface);
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 camera3_stream_rotation_t rotation, int *id, const String8& physicalCameraId,
55 std::vector<int> *surfaceIds, int streamSetId, bool isShared) override;
56 status_t deleteInternalStreams() override;
57 status_t configureStream() override;
58 status_t insertGbp(SurfaceMap* /*out*/outSurfaceMap, Vector<int32_t>* /*out*/outputStreamIds,
59 int32_t* /*out*/currentStreamId) override;
60 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;
83
84 InputFrame() : error(false), errorNotified(false), frameNumber(-1) { }
85 };
86
87 // Helper methods
88 static void getSupportedDepthSizes(const CameraMetadata& ch,
89 std::vector<std::tuple<size_t, size_t>>* depthSizes /*out*/);
90 static status_t getMatchingDepthSize(size_t width, size_t height,
91 const std::vector<std::tuple<size_t, size_t>>& supporedDepthSizes,
92 size_t *depthWidth /*out*/, size_t *depthHeight /*out*/);
93
94 // Dynamic depth processing
95 status_t encodeGrayscaleJpeg(size_t width, size_t height, uint8_t *in, void *out,
96 const size_t maxOutSize, uint8_t jpegQuality, size_t &actualSize);
97 std::unique_ptr<DepthMap> processDepthMapFrame(const CpuConsumer::LockedBuffer &depthMapBuffer,
98 size_t maxJpegSize, uint8_t jpegQuality,
99 std::vector<std::unique_ptr<Item>>* items /*out*/);
100 std::unique_ptr<ImagingModel> getImagingModel();
101 status_t processInputFrame(const InputFrame &inputFrame);
102
103 // Buffer/Results handling
104 void compilePendingInputLocked();
105 void releaseInputFrameLocked(InputFrame *inputFrame /*out*/);
106 void releaseInputFramesLocked(int64_t currentTs);
107
108 // Find first complete and valid frame with smallest timestamp
109 bool getNextReadyInputLocked(int64_t *currentTs /*inout*/);
110
111 // Find next failing frame number with smallest timestamp and return respective frame number
112 int64_t getNextFailingInputLocked(int64_t *currentTs /*inout*/);
113
114 static const nsecs_t kWaitDuration = 10000000; // 10 ms
115 static const auto kDepthMapPixelFormat = HAL_PIXEL_FORMAT_Y16;
116 static const auto kDepthMapDataSpace = HAL_DATASPACE_DEPTH;
117 static const auto kJpegDataSpace = HAL_DATASPACE_V0_JFIF;
118
119 struct ProducerListener : public BnProducerListener {
120 // ProducerListener implementation
121 void onBufferReleased() override { /*No impl. for now*/ };
122 };
123
124 int mBlobStreamId, mBlobSurfaceId, mDepthStreamId, mDepthSurfaceId;
125 size_t mBlobWidth, mBlobHeight;
126 sp<CpuConsumer> mBlobConsumer, mDepthConsumer;
127 bool mDepthBufferAcquired, mBlobBufferAcquired;
128 sp<Surface> mDepthSurface, mBlobSurface, mOutputSurface;
129 sp<ProducerListener> mProducerListener;
130
131 ssize_t mMaxJpegSize;
132 std::vector<std::tuple<size_t, size_t>> mSupportedDepthSizes;
133 std::vector<float> mInstrinsicCalibration, mLensDistortion;
134 bool mIsLogicalCamera;
Emilian Peevcbf174b2019-01-25 14:38:59 -0800135 void* mDepthPhotoLibHandle;
136 process_depth_photo_frame mDepthPhotoProcess;
Emilian Peev538c90e2018-12-17 18:03:19 +0000137
138 // Keep all incoming Depth buffer timestamps pending further processing.
139 std::vector<int64_t> mInputDepthBuffers;
140
141 // Keep all incoming Jpeg/Blob buffer timestamps pending further processing.
142 std::vector<int64_t> mInputJpegBuffers;
143
144 // Map of all input frames pending further processing.
145 std::unordered_map<int64_t, InputFrame> mPendingInputFrames;
146};
147
148}; //namespace camera3
149}; //namespace android
150
151#endif