blob: d32b71cf98d307047d62c186072d6b61277cec0b [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_COMPOSITE_STREAM_H
18#define ANDROID_SERVERS_CAMERA_CAMERA3_COMPOSITE_STREAM_H
19
20#include <set>
21#include <unordered_map>
22
23#include <android/hardware/camera2/ICameraDeviceCallbacks.h>
24#include <camera/CameraMetadata.h>
25#include <camera/camera2/OutputConfiguration.h>
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080026#include <gui/IProducerListener.h>
Emilian Peev538c90e2018-12-17 18:03:19 +000027#include "common/CameraDeviceBase.h"
28#include "device3/Camera3StreamInterface.h"
29
30namespace android {
31
32class CameraDeviceClient;
33class CameraMetadata;
34class Surface;
35
36namespace camera3 {
37
38class CompositeStream : public camera3::Camera3StreamBufferListener {
39
40public:
Shuzhen Wange8675782019-12-05 09:12:14 -080041 CompositeStream(sp<CameraDeviceBase> device, wp<hardware::camera2::ICameraDeviceCallbacks> cb);
Emilian Peev538c90e2018-12-17 18:03:19 +000042 virtual ~CompositeStream() {}
43
44 status_t createStream(const std::vector<sp<Surface>>& consumers,
45 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -070046 camera_stream_rotation_t rotation, int *id, const String8& physicalCameraId,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080047 const std::unordered_set<int32_t> &sensorPixelModesUsed,
48 std::vector<int> *surfaceIds,
49 int streamSetId, bool isShared, bool isMultiResolution);
Emilian Peev538c90e2018-12-17 18:03:19 +000050
51 status_t deleteStream();
52
Emilian Peevc0fe54c2020-03-11 14:05:07 -070053 // Switch to offline mode and release any online resources.
54 void switchToOffline();
55
Emilian Peev538c90e2018-12-17 18:03:19 +000056 // Create and register all internal camera streams.
57 virtual status_t createInternalStreams(const std::vector<sp<Surface>>& consumers,
58 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -070059 camera_stream_rotation_t rotation, int *id, const String8& physicalCameraId,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080060 const std::unordered_set<int32_t> &sensorPixelModesUsed,
61 std::vector<int> *surfaceIds,
62 int streamSetId, bool isShared) = 0;
Emilian Peev538c90e2018-12-17 18:03:19 +000063
64 // Release all internal streams and corresponding resources.
65 virtual status_t deleteInternalStreams() = 0;
66
67 // Stream configuration completed.
68 virtual status_t configureStream() = 0;
69
70 // Insert the internal composite stream id in the user capture request.
71 virtual status_t insertGbp(SurfaceMap* /*out*/outSurfaceMap,
72 Vector<int32_t>* /*out*/outputStreamIds, int32_t* /*out*/currentStreamId) = 0;
73
Emilian Peev4697b642019-11-19 17:11:14 -080074 // Attach the internal composite stream ids.
75 virtual status_t insertCompositeStreamIds(
76 std::vector<int32_t>* compositeStreamIds /*out*/) = 0;
77
Emilian Peev538c90e2018-12-17 18:03:19 +000078 // Return composite stream id.
79 virtual int getStreamId() = 0;
80
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080081 // Notify when shutter notify is triggered
82 virtual void onShutter(const CaptureResultExtras& /*resultExtras*/, nsecs_t /*timestamp*/) {}
83
Emilian Peev538c90e2018-12-17 18:03:19 +000084 void onResultAvailable(const CaptureResult& result);
85 bool onError(int32_t errorCode, const CaptureResultExtras& resultExtras);
86
87 // Camera3StreamBufferListener implementation
88 void onBufferAcquired(const BufferInfo& /*bufferInfo*/) override { /*Empty for now */ }
89 void onBufferReleased(const BufferInfo& bufferInfo) override;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080090 void onBufferRequestForFrameNumber(uint64_t frameNumber, int streamId,
91 const CameraMetadata& settings) override;
Emilian Peev538c90e2018-12-17 18:03:19 +000092
93protected:
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -080094 struct ProducerListener : public BnProducerListener {
95 // ProducerListener impementation
96 void onBufferReleased() override { /*No impl. for now*/ };
97 };
98
Emilian Peev538c90e2018-12-17 18:03:19 +000099 status_t registerCompositeStreamListener(int32_t streamId);
100 void eraseResult(int64_t frameNumber);
101 void flagAnErrorFrameNumber(int64_t frameNumber);
Shuzhen Wange8675782019-12-05 09:12:14 -0800102 void notifyError(int64_t frameNumber, int32_t requestId);
Emilian Peev538c90e2018-12-17 18:03:19 +0000103
104 // Subclasses should check for buffer errors from internal streams and return 'true' in
105 // case the error notification should remain within camera service.
106 virtual bool onStreamBufferError(const CaptureResultExtras& resultExtras) = 0;
107
108 // Subclasses can decide how to handle result errors depending on whether or not the
109 // internal processing needs result data.
110 virtual void onResultError(const CaptureResultExtras& resultExtras) = 0;
111
Shuzhen Wange8675782019-12-05 09:12:14 -0800112 // Subclasses can decide how to handle request errors depending on whether
113 // or not the internal processing needs clean up.
114 virtual void onRequestError(const CaptureResultExtras& /*resultExtras*/) {}
115
Emilian Peev538c90e2018-12-17 18:03:19 +0000116 // Device and/or service is in unrecoverable error state.
117 // Composite streams should behave accordingly.
118 void enableErrorState();
119
Shuzhen Wangc87315d2022-03-17 00:11:20 +0000120 // Utility class to lock and unlock a GraphicBuffer
121 class GraphicBufferLocker {
122 public:
123 GraphicBufferLocker(sp<GraphicBuffer> buffer) : _buffer(buffer) {}
124
125 status_t lockAsync(void** dstBuffer, int fenceFd) {
126 if (_buffer == nullptr) return BAD_VALUE;
127
128 status_t res = OK;
129 if (!_locked) {
130 status_t res = _buffer->lockAsync(GRALLOC_USAGE_SW_WRITE_OFTEN,
131 dstBuffer, fenceFd);
132 if (res == OK) {
133 _locked = true;
134 }
135 }
136 return res;
137 }
138
139 ~GraphicBufferLocker() {
140 if (_locked && _buffer != nullptr) {
141 auto res = _buffer->unlock();
142 if (res != OK) {
143 ALOGE("%s: Error trying to unlock buffer: %s (%d)", __FUNCTION__,
144 strerror(-res), res);
145 }
146 }
147 }
148
149 private:
150 sp<GraphicBuffer> _buffer;
151 bool _locked = false;
152 };
153
154
Emilian Peev538c90e2018-12-17 18:03:19 +0000155 wp<CameraDeviceBase> mDevice;
Shuzhen Wange8675782019-12-05 09:12:14 -0800156 wp<camera3::StatusTracker> mStatusTracker;
Emilian Peev538c90e2018-12-17 18:03:19 +0000157 wp<hardware::camera2::ICameraDeviceCallbacks> mRemoteCallback;
158
159 mutable Mutex mMutex;
160 Condition mInputReadyCondition;
161 int32_t mNumPartialResults;
162 bool mErrorState;
163
164 // Frame number to capture result map of partial pending request results.
165 std::unordered_map<uint64_t, CameraMetadata> mPendingCaptureResults;
166
167 // Timestamp to capture (frame number, result) map of completed pending request results.
168 std::unordered_map<int64_t, std::tuple<int64_t, CameraMetadata>> mCaptureResults;
169
170 // Frame number to timestamp map
171 std::unordered_map<int64_t, int64_t> mFrameNumberMap;
172
173 // Keeps a set buffer/result frame numbers for any errors detected during processing.
174 std::set<int64_t> mErrorFrameNumbers;
175
176};
177
178}; //namespace camera3
179}; //namespace android
180
181#endif