blob: 3662a658ada289eef4c2d9f9c6a908d2bc2516ee [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003 *
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_CAMERADEVICEBASE_H
18#define ANDROID_SERVERS_CAMERA_CAMERADEVICEBASE_H
19
Shuzhen Wang0129d522016-10-30 22:43:41 -070020#include <list>
21
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080022#include <utils/RefBase.h>
23#include <utils/String8.h>
24#include <utils/String16.h>
25#include <utils/Vector.h>
Emilian Peev40ead602017-09-26 15:46:36 +010026#include <utils/KeyedVector.h>
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080027#include <utils/Timers.h>
Jianing Wei90e59c92014-03-12 18:29:36 -070028#include <utils/List.h>
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080029
30#include "hardware/camera2.h"
Zhijun He0ea8fa42014-07-07 17:05:38 -070031#include "hardware/camera3.h"
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080032#include "camera/CameraMetadata.h"
Jianing Weicb0652e2014-03-12 18:29:36 -070033#include "camera/CaptureResult.h"
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070034#include "gui/IGraphicBufferProducer.h"
Zhijun He125684a2015-12-26 15:07:30 -080035#include "device3/Camera3StreamInterface.h"
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080036#include "binder/Status.h"
Emilian Peevfaa4bde2020-01-23 12:19:37 -080037#include "FrameProducer.h"
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080038
Yin-Chia Yehb978c382019-10-30 00:22:37 -070039#include "CameraOfflineSessionBase.h"
40
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080041namespace android {
42
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080043class CameraProviderManager;
44
Shuzhen Wang0129d522016-10-30 22:43:41 -070045// Mapping of output stream index to surface ids
46typedef std::unordered_map<int, std::vector<size_t> > SurfaceMap;
47
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080048/**
49 * Base interface for version >= 2 camera device classes, which interface to
50 * camera HAL device versions >= 2.
51 */
Emilian Peevfaa4bde2020-01-23 12:19:37 -080052class CameraDeviceBase : public virtual FrameProducer {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080053 public:
54 virtual ~CameraDeviceBase();
55
Igor Murashkin71381052013-03-04 14:53:08 -080056 /**
Emilian Peev00420d22018-02-05 21:33:13 +000057 * The device vendor tag ID
58 */
59 virtual metadata_vendor_id_t getVendorTagId() const = 0;
60
Emilian Peevbd8c5032018-02-14 23:05:40 +000061 virtual status_t initialize(sp<CameraProviderManager> manager, const String8& monitorTags) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080062 virtual status_t disconnect() = 0;
63
Jianing Weicb0652e2014-03-12 18:29:36 -070064 virtual status_t dump(int fd, const Vector<String16> &args) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080065
66 /**
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -070067 * The physical camera device's static characteristics metadata buffer
68 */
Emilian Peevfaa4bde2020-01-23 12:19:37 -080069 virtual const CameraMetadata& infoPhysical(const String8& physicalId) const = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080070
Emilian Peevaebbe412018-01-15 13:53:24 +000071 struct PhysicalCameraSettings {
72 std::string cameraId;
73 CameraMetadata metadata;
74 };
75 typedef List<PhysicalCameraSettings> PhysicalCameraSettingsList;
76
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080077 /**
78 * Submit request for capture. The CameraDevice takes ownership of the
79 * passed-in buffer.
Jianing Weicb0652e2014-03-12 18:29:36 -070080 * Output lastFrameNumber is the expected frame number of this request.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080081 */
Jianing Weicb0652e2014-03-12 18:29:36 -070082 virtual status_t capture(CameraMetadata &request, int64_t *lastFrameNumber = NULL) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080083
84 /**
Jianing Wei90e59c92014-03-12 18:29:36 -070085 * Submit a list of requests.
Jianing Weicb0652e2014-03-12 18:29:36 -070086 * Output lastFrameNumber is the expected last frame number of the list of requests.
Jianing Wei90e59c92014-03-12 18:29:36 -070087 */
Emilian Peevaebbe412018-01-15 13:53:24 +000088 virtual status_t captureList(const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -070089 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -070090 int64_t *lastFrameNumber = NULL) = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -070091
92 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080093 * Submit request for streaming. The CameraDevice makes a copy of the
94 * passed-in buffer and the caller retains ownership.
Jianing Weicb0652e2014-03-12 18:29:36 -070095 * Output lastFrameNumber is the last frame number of the previous streaming request.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080096 */
Jianing Weicb0652e2014-03-12 18:29:36 -070097 virtual status_t setStreamingRequest(const CameraMetadata &request,
98 int64_t *lastFrameNumber = NULL) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080099
100 /**
Jianing Wei90e59c92014-03-12 18:29:36 -0700101 * Submit a list of requests for streaming.
Jianing Weicb0652e2014-03-12 18:29:36 -0700102 * Output lastFrameNumber is the last frame number of the previous streaming request.
Jianing Wei90e59c92014-03-12 18:29:36 -0700103 */
Emilian Peevaebbe412018-01-15 13:53:24 +0000104 virtual status_t setStreamingRequestList(const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700105 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -0700106 int64_t *lastFrameNumber = NULL) = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700107
108 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800109 * Clear the streaming request slot.
Jianing Weicb0652e2014-03-12 18:29:36 -0700110 * Output lastFrameNumber is the last frame number of the previous streaming request.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800111 */
Jianing Weicb0652e2014-03-12 18:29:36 -0700112 virtual status_t clearStreamingRequest(int64_t *lastFrameNumber = NULL) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800113
114 /**
115 * Wait until a request with the given ID has been dequeued by the
116 * HAL. Returns TIMED_OUT if the timeout duration is reached. Returns
117 * immediately if the latest request received by the HAL has this id.
118 */
119 virtual status_t waitUntilRequestReceived(int32_t requestId,
120 nsecs_t timeout) = 0;
121
122 /**
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700123 * Create an output stream of the requested size, format, rotation and dataspace
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800124 *
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800125 * For HAL_PIXEL_FORMAT_BLOB formats, the width and height should be the
126 * logical dimensions of the buffer, not the number of bytes.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800127 */
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700128 virtual status_t createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800129 uint32_t width, uint32_t height, int format,
Zhijun He125684a2015-12-26 15:07:30 -0800130 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800131 const String8& physicalCameraId,
Emilian Peev40ead602017-09-26 15:46:36 +0100132 std::vector<int> *surfaceIds = nullptr,
Zhijun He5d677d12016-05-29 16:52:39 -0700133 int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID,
Emilian Peev050f5dc2017-05-18 14:43:56 +0100134 bool isShared = false, uint64_t consumerUsage = 0) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800135
136 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700137 * Create an output stream of the requested size, format, rotation and
138 * dataspace with a number of consumers.
139 *
140 * For HAL_PIXEL_FORMAT_BLOB formats, the width and height should be the
141 * logical dimensions of the buffer, not the number of bytes.
142 */
143 virtual status_t createStream(const std::vector<sp<Surface>>& consumers,
144 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
145 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800146 const String8& physicalCameraId,
Emilian Peev40ead602017-09-26 15:46:36 +0100147 std::vector<int> *surfaceIds = nullptr,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700148 int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID,
Emilian Peev050f5dc2017-05-18 14:43:56 +0100149 bool isShared = false, uint64_t consumerUsage = 0) = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700150
151 /**
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700152 * Create an input stream of width, height, and format.
153 *
154 * Return value is the stream ID if non-negative and an error if negative.
155 */
156 virtual status_t createInputStream(uint32_t width, uint32_t height,
157 int32_t format, /*out*/ int32_t *id) = 0;
158
Emilian Peev710c1422017-08-30 11:19:38 +0100159 struct StreamInfo {
160 uint32_t width;
161 uint32_t height;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700162
Emilian Peev710c1422017-08-30 11:19:38 +0100163 uint32_t format;
164 bool formatOverridden;
165 uint32_t originalFormat;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700166
Emilian Peev710c1422017-08-30 11:19:38 +0100167 android_dataspace dataSpace;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700168 bool dataSpaceOverridden;
169 android_dataspace originalDataSpace;
170
Emilian Peev710c1422017-08-30 11:19:38 +0100171 StreamInfo() : width(0), height(0), format(0), formatOverridden(false), originalFormat(0),
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700172 dataSpace(HAL_DATASPACE_UNKNOWN), dataSpaceOverridden(false),
173 originalDataSpace(HAL_DATASPACE_UNKNOWN) {}
Emilian Peev710c1422017-08-30 11:19:38 +0100174 /**
175 * Check whether the format matches the current or the original one in case
176 * it got overridden.
177 */
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700178 bool matchFormat(uint32_t clientFormat) const {
Emilian Peev710c1422017-08-30 11:19:38 +0100179 if ((formatOverridden && (originalFormat == clientFormat)) ||
180 (format == clientFormat)) {
181 return true;
182 }
183 return false;
184 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700185
186 /**
187 * Check whether the dataspace matches the current or the original one in case
188 * it got overridden.
189 */
190 bool matchDataSpace(android_dataspace clientDataSpace) const {
191 if ((dataSpaceOverridden && (originalDataSpace == clientDataSpace)) ||
192 (dataSpace == clientDataSpace)) {
193 return true;
194 }
195 return false;
196 }
197
Emilian Peev710c1422017-08-30 11:19:38 +0100198 };
199
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700200 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800201 * Get information about a given stream.
202 */
Emilian Peev710c1422017-08-30 11:19:38 +0100203 virtual status_t getStreamInfo(int id, StreamInfo *streamInfo) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800204
205 /**
206 * Set stream gralloc buffer transform
207 */
208 virtual status_t setStreamTransform(int id, int transform) = 0;
209
210 /**
211 * Delete stream. Must not be called if there are requests in flight which
212 * reference that stream.
213 */
214 virtual status_t deleteStream(int id) = 0;
215
216 /**
Igor Murashkine2d167e2014-08-19 16:19:59 -0700217 * Take the currently-defined set of streams and configure the HAL to use
218 * them. This is a long-running operation (may be several hundered ms).
219 *
220 * The device must be idle (see waitUntilDrained) before calling this.
221 *
222 * Returns OK on success; otherwise on error:
223 * - BAD_VALUE if the set of streams was invalid (e.g. fmts or sizes)
224 * - INVALID_OPERATION if the device was in the wrong state
225 */
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100226 virtual status_t configureStreams(const CameraMetadata& sessionParams,
227 int operatingMode = 0) = 0;
Igor Murashkine2d167e2014-08-19 16:19:59 -0700228
Emilian Peevcc0b7952020-01-07 13:54:47 -0800229 /**
230 * Retrieve a list of all stream ids that were advertised as capable of
231 * supporting offline processing mode by Hal after the last stream configuration.
232 */
233 virtual void getOfflineStreamIds(std::vector<int> *offlineStreamIds) = 0;
234
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700235 // get the buffer producer of the input stream
236 virtual status_t getInputBufferProducer(
237 sp<IGraphicBufferProducer> *producer) = 0;
238
Igor Murashkine2d167e2014-08-19 16:19:59 -0700239 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800240 * Create a metadata buffer with fields that the HAL device believes are
241 * best for the given use case
242 */
243 virtual status_t createDefaultRequest(int templateId,
244 CameraMetadata *request) = 0;
245
246 /**
247 * Wait until all requests have been processed. Returns INVALID_OPERATION if
248 * the streaming slot is not empty, or TIMED_OUT if the requests haven't
249 * finished processing in 10 seconds.
250 */
251 virtual status_t waitUntilDrained() = 0;
252
253 /**
Zhijun He28c9b6f2014-08-08 12:00:47 -0700254 * Get Jpeg buffer size for a given jpeg resolution.
255 * Negative values are error codes.
256 */
257 virtual ssize_t getJpegBufferSize(uint32_t width, uint32_t height) const = 0;
258
259 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800260 * Connect HAL notifications to a listener. Overwrites previous
261 * listener. Set to NULL to stop receiving notifications.
262 */
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700263 virtual status_t setNotifyCallback(wp<NotificationListener> listener) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800264
265 /**
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -0700266 * Whether the device supports calling notifyAutofocus, notifyAutoExposure,
267 * and notifyAutoWhitebalance; if this returns false, the client must
268 * synthesize these notifications from received frame metadata.
269 */
270 virtual bool willNotify3A() = 0;
271
272 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800273 * Trigger auto-focus. The latest ID used in a trigger autofocus or cancel
274 * autofocus call will be returned by the HAL in all subsequent AF
275 * notifications.
276 */
277 virtual status_t triggerAutofocus(uint32_t id) = 0;
278
279 /**
280 * Cancel auto-focus. The latest ID used in a trigger autofocus/cancel
281 * autofocus call will be returned by the HAL in all subsequent AF
282 * notifications.
283 */
284 virtual status_t triggerCancelAutofocus(uint32_t id) = 0;
285
286 /**
287 * Trigger pre-capture metering. The latest ID used in a trigger pre-capture
288 * call will be returned by the HAL in all subsequent AE and AWB
289 * notifications.
290 */
291 virtual status_t triggerPrecaptureMetering(uint32_t id) = 0;
292
293 /**
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700294 * Flush all pending and in-flight requests. Blocks until flush is
295 * complete.
Jianing Weicb0652e2014-03-12 18:29:36 -0700296 * Output lastFrameNumber is the last frame number of the previous streaming request.
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700297 */
Jianing Weicb0652e2014-03-12 18:29:36 -0700298 virtual status_t flush(int64_t *lastFrameNumber = NULL) = 0;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700299
Zhijun He204e3292014-07-14 17:09:23 -0700300 /**
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700301 * Prepare stream by preallocating buffers for it asynchronously.
302 * Calls notifyPrepared() once allocation is complete.
303 */
304 virtual status_t prepare(int streamId) = 0;
305
306 /**
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700307 * Free stream resources by dumping its unused gralloc buffers.
308 */
309 virtual status_t tearDown(int streamId) = 0;
310
311 /**
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700312 * Add buffer listener for a particular stream in the device.
313 */
314 virtual status_t addBufferListenerForStream(int streamId,
315 wp<camera3::Camera3StreamBufferListener> listener) = 0;
316
317 /**
Ruben Brunkc78ac262015-08-13 17:58:46 -0700318 * Prepare stream by preallocating up to maxCount buffers for it asynchronously.
319 * Calls notifyPrepared() once allocation is complete.
320 */
321 virtual status_t prepare(int maxCount, int streamId) = 0;
322
323 /**
Zhijun He5d677d12016-05-29 16:52:39 -0700324 * Set the deferred consumer surface and finish the rest of the stream configuration.
325 */
Shuzhen Wang758c2152017-01-10 18:26:18 -0800326 virtual status_t setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +0100327 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds /*out*/) = 0;
Zhijun He5d677d12016-05-29 16:52:39 -0700328
Emilian Peev40ead602017-09-26 15:46:36 +0100329 /**
330 * Update a given stream.
331 */
332 virtual status_t updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
333 const std::vector<android::camera3::OutputStreamInfo> &outputInfo,
334 const std::vector<size_t> &removedSurfaceIds,
335 KeyedVector<sp<Surface>, size_t> *outputMap/*out*/) = 0;
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700336
337 /**
338 * Drop buffers for stream of streamId if dropping is true. If dropping is false, do not
339 * drop buffers for stream of streamId.
340 */
341 virtual status_t dropStreamBuffers(bool /*dropping*/, int /*streamId*/) = 0;
Emilian Peev2a8e2832019-08-23 13:00:31 -0700342
343 /**
344 * Returns the maximum expected time it'll take for all currently in-flight
345 * requests to complete, based on their settings
346 */
347 virtual nsecs_t getExpectedInFlightDuration() = 0;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700348
349 /**
350 * switch to offline session
351 */
352 virtual status_t switchToOffline(
353 const std::vector<int32_t>& streamsToKeep,
354 /*out*/ sp<CameraOfflineSessionBase>* session) = 0;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800355
356 /**
357 * Set the current behavior for the ROTATE_AND_CROP control when in AUTO.
358 *
359 * The value must be one of the ROTATE_AND_CROP_* values besides AUTO,
360 * and defaults to NONE.
361 */
362 virtual status_t setRotateAndCropAutoBehavior(
363 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) = 0;
364
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800365};
366
367}; // namespace android
368
369#endif