blob: 77e660f3a4c1f68cc97d3b5855d561afc13f53af [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"
Shuzhen Wange8675782019-12-05 09:12:14 -080036#include "device3/StatusTracker.h"
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080037#include "binder/Status.h"
Emilian Peevfaa4bde2020-01-23 12:19:37 -080038#include "FrameProducer.h"
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080039
Yin-Chia Yehb978c382019-10-30 00:22:37 -070040#include "CameraOfflineSessionBase.h"
41
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042namespace android {
43
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080044class CameraProviderManager;
45
Shuzhen Wang0129d522016-10-30 22:43:41 -070046// Mapping of output stream index to surface ids
47typedef std::unordered_map<int, std::vector<size_t> > SurfaceMap;
48
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080049/**
50 * Base interface for version >= 2 camera device classes, which interface to
51 * camera HAL device versions >= 2.
52 */
Emilian Peevfaa4bde2020-01-23 12:19:37 -080053class CameraDeviceBase : public virtual FrameProducer {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080054 public:
55 virtual ~CameraDeviceBase();
56
Igor Murashkin71381052013-03-04 14:53:08 -080057 /**
Emilian Peev00420d22018-02-05 21:33:13 +000058 * The device vendor tag ID
59 */
60 virtual metadata_vendor_id_t getVendorTagId() const = 0;
61
Emilian Peevbd8c5032018-02-14 23:05:40 +000062 virtual status_t initialize(sp<CameraProviderManager> manager, const String8& monitorTags) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080063 virtual status_t disconnect() = 0;
64
Jianing Weicb0652e2014-03-12 18:29:36 -070065 virtual status_t dump(int fd, const Vector<String16> &args) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080066
67 /**
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -070068 * The physical camera device's static characteristics metadata buffer
69 */
Emilian Peevfaa4bde2020-01-23 12:19:37 -080070 virtual const CameraMetadata& infoPhysical(const String8& physicalId) const = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080071
Emilian Peevaebbe412018-01-15 13:53:24 +000072 struct PhysicalCameraSettings {
73 std::string cameraId;
74 CameraMetadata metadata;
75 };
76 typedef List<PhysicalCameraSettings> PhysicalCameraSettingsList;
77
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080078 /**
79 * Submit request for capture. The CameraDevice takes ownership of the
80 * passed-in buffer.
Jianing Weicb0652e2014-03-12 18:29:36 -070081 * Output lastFrameNumber is the expected frame number of this request.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080082 */
Jianing Weicb0652e2014-03-12 18:29:36 -070083 virtual status_t capture(CameraMetadata &request, int64_t *lastFrameNumber = NULL) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080084
85 /**
Jianing Wei90e59c92014-03-12 18:29:36 -070086 * Submit a list of requests.
Jianing Weicb0652e2014-03-12 18:29:36 -070087 * Output lastFrameNumber is the expected last frame number of the list of requests.
Jianing Wei90e59c92014-03-12 18:29:36 -070088 */
Emilian Peevaebbe412018-01-15 13:53:24 +000089 virtual status_t captureList(const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -070090 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -070091 int64_t *lastFrameNumber = NULL) = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -070092
93 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080094 * Submit request for streaming. The CameraDevice makes a copy of the
95 * passed-in buffer and the caller retains ownership.
Jianing Weicb0652e2014-03-12 18:29:36 -070096 * Output lastFrameNumber is the last frame number of the previous streaming request.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080097 */
Jianing Weicb0652e2014-03-12 18:29:36 -070098 virtual status_t setStreamingRequest(const CameraMetadata &request,
99 int64_t *lastFrameNumber = NULL) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800100
101 /**
Jianing Wei90e59c92014-03-12 18:29:36 -0700102 * Submit a list of requests for streaming.
Jianing Weicb0652e2014-03-12 18:29:36 -0700103 * Output lastFrameNumber is the last frame number of the previous streaming request.
Jianing Wei90e59c92014-03-12 18:29:36 -0700104 */
Emilian Peevaebbe412018-01-15 13:53:24 +0000105 virtual status_t setStreamingRequestList(const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700106 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -0700107 int64_t *lastFrameNumber = NULL) = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -0700108
109 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800110 * Clear the streaming request slot.
Jianing Weicb0652e2014-03-12 18:29:36 -0700111 * Output lastFrameNumber is the last frame number of the previous streaming request.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800112 */
Jianing Weicb0652e2014-03-12 18:29:36 -0700113 virtual status_t clearStreamingRequest(int64_t *lastFrameNumber = NULL) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800114
115 /**
116 * Wait until a request with the given ID has been dequeued by the
117 * HAL. Returns TIMED_OUT if the timeout duration is reached. Returns
118 * immediately if the latest request received by the HAL has this id.
119 */
120 virtual status_t waitUntilRequestReceived(int32_t requestId,
121 nsecs_t timeout) = 0;
122
123 /**
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700124 * Create an output stream of the requested size, format, rotation and dataspace
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800125 *
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800126 * For HAL_PIXEL_FORMAT_BLOB formats, the width and height should be the
127 * logical dimensions of the buffer, not the number of bytes.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800128 */
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700129 virtual status_t createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800130 uint32_t width, uint32_t height, int format,
Zhijun He125684a2015-12-26 15:07:30 -0800131 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800132 const String8& physicalCameraId,
Emilian Peev40ead602017-09-26 15:46:36 +0100133 std::vector<int> *surfaceIds = nullptr,
Zhijun He5d677d12016-05-29 16:52:39 -0700134 int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID,
Emilian Peev050f5dc2017-05-18 14:43:56 +0100135 bool isShared = false, uint64_t consumerUsage = 0) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800136
137 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700138 * Create an output stream of the requested size, format, rotation and
139 * dataspace with a number of consumers.
140 *
141 * For HAL_PIXEL_FORMAT_BLOB formats, the width and height should be the
142 * logical dimensions of the buffer, not the number of bytes.
143 */
144 virtual status_t createStream(const std::vector<sp<Surface>>& consumers,
145 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
146 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800147 const String8& physicalCameraId,
Emilian Peev40ead602017-09-26 15:46:36 +0100148 std::vector<int> *surfaceIds = nullptr,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700149 int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID,
Emilian Peev050f5dc2017-05-18 14:43:56 +0100150 bool isShared = false, uint64_t consumerUsage = 0) = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700151
152 /**
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700153 * Create an input stream of width, height, and format.
154 *
155 * Return value is the stream ID if non-negative and an error if negative.
156 */
157 virtual status_t createInputStream(uint32_t width, uint32_t height,
158 int32_t format, /*out*/ int32_t *id) = 0;
159
Emilian Peev710c1422017-08-30 11:19:38 +0100160 struct StreamInfo {
161 uint32_t width;
162 uint32_t height;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700163
Emilian Peev710c1422017-08-30 11:19:38 +0100164 uint32_t format;
165 bool formatOverridden;
166 uint32_t originalFormat;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700167
Emilian Peev710c1422017-08-30 11:19:38 +0100168 android_dataspace dataSpace;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700169 bool dataSpaceOverridden;
170 android_dataspace originalDataSpace;
171
Emilian Peev710c1422017-08-30 11:19:38 +0100172 StreamInfo() : width(0), height(0), format(0), formatOverridden(false), originalFormat(0),
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700173 dataSpace(HAL_DATASPACE_UNKNOWN), dataSpaceOverridden(false),
174 originalDataSpace(HAL_DATASPACE_UNKNOWN) {}
Emilian Peev710c1422017-08-30 11:19:38 +0100175 /**
176 * Check whether the format matches the current or the original one in case
177 * it got overridden.
178 */
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700179 bool matchFormat(uint32_t clientFormat) const {
Emilian Peev710c1422017-08-30 11:19:38 +0100180 if ((formatOverridden && (originalFormat == clientFormat)) ||
181 (format == clientFormat)) {
182 return true;
183 }
184 return false;
185 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700186
187 /**
188 * Check whether the dataspace matches the current or the original one in case
189 * it got overridden.
190 */
191 bool matchDataSpace(android_dataspace clientDataSpace) const {
192 if ((dataSpaceOverridden && (originalDataSpace == clientDataSpace)) ||
193 (dataSpace == clientDataSpace)) {
194 return true;
195 }
196 return false;
197 }
198
Emilian Peev710c1422017-08-30 11:19:38 +0100199 };
200
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700201 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800202 * Get information about a given stream.
203 */
Emilian Peev710c1422017-08-30 11:19:38 +0100204 virtual status_t getStreamInfo(int id, StreamInfo *streamInfo) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800205
206 /**
207 * Set stream gralloc buffer transform
208 */
209 virtual status_t setStreamTransform(int id, int transform) = 0;
210
211 /**
212 * Delete stream. Must not be called if there are requests in flight which
213 * reference that stream.
214 */
215 virtual status_t deleteStream(int id) = 0;
216
217 /**
Igor Murashkine2d167e2014-08-19 16:19:59 -0700218 * Take the currently-defined set of streams and configure the HAL to use
219 * them. This is a long-running operation (may be several hundered ms).
220 *
221 * The device must be idle (see waitUntilDrained) before calling this.
222 *
223 * Returns OK on success; otherwise on error:
224 * - BAD_VALUE if the set of streams was invalid (e.g. fmts or sizes)
225 * - INVALID_OPERATION if the device was in the wrong state
226 */
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100227 virtual status_t configureStreams(const CameraMetadata& sessionParams,
228 int operatingMode = 0) = 0;
Igor Murashkine2d167e2014-08-19 16:19:59 -0700229
Emilian Peevcc0b7952020-01-07 13:54:47 -0800230 /**
231 * Retrieve a list of all stream ids that were advertised as capable of
232 * supporting offline processing mode by Hal after the last stream configuration.
233 */
234 virtual void getOfflineStreamIds(std::vector<int> *offlineStreamIds) = 0;
235
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700236 // get the buffer producer of the input stream
237 virtual status_t getInputBufferProducer(
238 sp<IGraphicBufferProducer> *producer) = 0;
239
Igor Murashkine2d167e2014-08-19 16:19:59 -0700240 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800241 * Create a metadata buffer with fields that the HAL device believes are
242 * best for the given use case
243 */
244 virtual status_t createDefaultRequest(int templateId,
245 CameraMetadata *request) = 0;
246
247 /**
248 * Wait until all requests have been processed. Returns INVALID_OPERATION if
249 * the streaming slot is not empty, or TIMED_OUT if the requests haven't
250 * finished processing in 10 seconds.
251 */
252 virtual status_t waitUntilDrained() = 0;
253
254 /**
Zhijun He28c9b6f2014-08-08 12:00:47 -0700255 * Get Jpeg buffer size for a given jpeg resolution.
256 * Negative values are error codes.
257 */
258 virtual ssize_t getJpegBufferSize(uint32_t width, uint32_t height) const = 0;
259
260 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800261 * Connect HAL notifications to a listener. Overwrites previous
262 * listener. Set to NULL to stop receiving notifications.
263 */
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700264 virtual status_t setNotifyCallback(wp<NotificationListener> listener) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800265
266 /**
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -0700267 * Whether the device supports calling notifyAutofocus, notifyAutoExposure,
268 * and notifyAutoWhitebalance; if this returns false, the client must
269 * synthesize these notifications from received frame metadata.
270 */
271 virtual bool willNotify3A() = 0;
272
273 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800274 * Trigger auto-focus. The latest ID used in a trigger autofocus or cancel
275 * autofocus call will be returned by the HAL in all subsequent AF
276 * notifications.
277 */
278 virtual status_t triggerAutofocus(uint32_t id) = 0;
279
280 /**
281 * Cancel auto-focus. The latest ID used in a trigger autofocus/cancel
282 * autofocus call will be returned by the HAL in all subsequent AF
283 * notifications.
284 */
285 virtual status_t triggerCancelAutofocus(uint32_t id) = 0;
286
287 /**
288 * Trigger pre-capture metering. The latest ID used in a trigger pre-capture
289 * call will be returned by the HAL in all subsequent AE and AWB
290 * notifications.
291 */
292 virtual status_t triggerPrecaptureMetering(uint32_t id) = 0;
293
294 /**
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700295 * Flush all pending and in-flight requests. Blocks until flush is
296 * complete.
Jianing Weicb0652e2014-03-12 18:29:36 -0700297 * Output lastFrameNumber is the last frame number of the previous streaming request.
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700298 */
Jianing Weicb0652e2014-03-12 18:29:36 -0700299 virtual status_t flush(int64_t *lastFrameNumber = NULL) = 0;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700300
Zhijun He204e3292014-07-14 17:09:23 -0700301 /**
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700302 * Prepare stream by preallocating buffers for it asynchronously.
303 * Calls notifyPrepared() once allocation is complete.
304 */
305 virtual status_t prepare(int streamId) = 0;
306
307 /**
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700308 * Free stream resources by dumping its unused gralloc buffers.
309 */
310 virtual status_t tearDown(int streamId) = 0;
311
312 /**
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700313 * Add buffer listener for a particular stream in the device.
314 */
315 virtual status_t addBufferListenerForStream(int streamId,
316 wp<camera3::Camera3StreamBufferListener> listener) = 0;
317
318 /**
Ruben Brunkc78ac262015-08-13 17:58:46 -0700319 * Prepare stream by preallocating up to maxCount buffers for it asynchronously.
320 * Calls notifyPrepared() once allocation is complete.
321 */
322 virtual status_t prepare(int maxCount, int streamId) = 0;
323
324 /**
Zhijun He5d677d12016-05-29 16:52:39 -0700325 * Set the deferred consumer surface and finish the rest of the stream configuration.
326 */
Shuzhen Wang758c2152017-01-10 18:26:18 -0800327 virtual status_t setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +0100328 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds /*out*/) = 0;
Zhijun He5d677d12016-05-29 16:52:39 -0700329
Emilian Peev40ead602017-09-26 15:46:36 +0100330 /**
331 * Update a given stream.
332 */
333 virtual status_t updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
334 const std::vector<android::camera3::OutputStreamInfo> &outputInfo,
335 const std::vector<size_t> &removedSurfaceIds,
336 KeyedVector<sp<Surface>, size_t> *outputMap/*out*/) = 0;
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700337
338 /**
339 * Drop buffers for stream of streamId if dropping is true. If dropping is false, do not
340 * drop buffers for stream of streamId.
341 */
342 virtual status_t dropStreamBuffers(bool /*dropping*/, int /*streamId*/) = 0;
Emilian Peev2a8e2832019-08-23 13:00:31 -0700343
344 /**
345 * Returns the maximum expected time it'll take for all currently in-flight
346 * requests to complete, based on their settings
347 */
348 virtual nsecs_t getExpectedInFlightDuration() = 0;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700349
350 /**
351 * switch to offline session
352 */
353 virtual status_t switchToOffline(
354 const std::vector<int32_t>& streamsToKeep,
355 /*out*/ sp<CameraOfflineSessionBase>* session) = 0;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800356
357 /**
358 * Set the current behavior for the ROTATE_AND_CROP control when in AUTO.
359 *
360 * The value must be one of the ROTATE_AND_CROP_* values besides AUTO,
361 * and defaults to NONE.
362 */
363 virtual status_t setRotateAndCropAutoBehavior(
364 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) = 0;
365
Shuzhen Wange8675782019-12-05 09:12:14 -0800366 /**
367 * Get the status tracker of the camera device
368 */
369 virtual wp<camera3::StatusTracker> getStatusTracker() = 0;
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -0800370
371 /**
372 * Set bitmask for image dump flag
373 */
374 void setImageDumpMask(int mask) { mImageDumpMask = mask; }
375
376protected:
377 bool mImageDumpMask = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800378};
379
380}; // namespace android
381
382#endif