blob: 4a7c674b063c437e66bf720b8c2946760b2f4e82 [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"
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080037
38namespace android {
39
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080040class CameraProviderManager;
41
Shuzhen Wang0129d522016-10-30 22:43:41 -070042// Mapping of output stream index to surface ids
43typedef std::unordered_map<int, std::vector<size_t> > SurfaceMap;
44
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080045/**
46 * Base interface for version >= 2 camera device classes, which interface to
47 * camera HAL device versions >= 2.
48 */
49class CameraDeviceBase : public virtual RefBase {
50 public:
51 virtual ~CameraDeviceBase();
52
Igor Murashkin71381052013-03-04 14:53:08 -080053 /**
54 * The device's camera ID
55 */
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080056 virtual const String8& getId() const = 0;
Igor Murashkin71381052013-03-04 14:53:08 -080057
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080058 virtual status_t initialize(sp<CameraProviderManager> manager) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080059 virtual status_t disconnect() = 0;
60
Jianing Weicb0652e2014-03-12 18:29:36 -070061 virtual status_t dump(int fd, const Vector<String16> &args) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080062
63 /**
64 * The device's static characteristics metadata buffer
65 */
66 virtual const CameraMetadata& info() const = 0;
67
68 /**
69 * Submit request for capture. The CameraDevice takes ownership of the
70 * passed-in buffer.
Jianing Weicb0652e2014-03-12 18:29:36 -070071 * Output lastFrameNumber is the expected frame number of this request.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080072 */
Jianing Weicb0652e2014-03-12 18:29:36 -070073 virtual status_t capture(CameraMetadata &request, int64_t *lastFrameNumber = NULL) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080074
75 /**
Jianing Wei90e59c92014-03-12 18:29:36 -070076 * Submit a list of requests.
Jianing Weicb0652e2014-03-12 18:29:36 -070077 * Output lastFrameNumber is the expected last frame number of the list of requests.
Jianing Wei90e59c92014-03-12 18:29:36 -070078 */
Jianing Weicb0652e2014-03-12 18:29:36 -070079 virtual status_t captureList(const List<const CameraMetadata> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -070080 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -070081 int64_t *lastFrameNumber = NULL) = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -070082
83 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080084 * Submit request for streaming. The CameraDevice makes a copy of the
85 * passed-in buffer and the caller retains ownership.
Jianing Weicb0652e2014-03-12 18:29:36 -070086 * Output lastFrameNumber is the last frame number of the previous streaming request.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080087 */
Jianing Weicb0652e2014-03-12 18:29:36 -070088 virtual status_t setStreamingRequest(const CameraMetadata &request,
89 int64_t *lastFrameNumber = NULL) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080090
91 /**
Jianing Wei90e59c92014-03-12 18:29:36 -070092 * Submit a list of requests for streaming.
Jianing Weicb0652e2014-03-12 18:29:36 -070093 * Output lastFrameNumber is the last frame number of the previous streaming request.
Jianing Wei90e59c92014-03-12 18:29:36 -070094 */
Jianing Weicb0652e2014-03-12 18:29:36 -070095 virtual status_t setStreamingRequestList(const List<const CameraMetadata> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -070096 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -070097 int64_t *lastFrameNumber = NULL) = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -070098
99 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800100 * Clear the streaming request slot.
Jianing Weicb0652e2014-03-12 18:29:36 -0700101 * Output lastFrameNumber is the last frame number of the previous streaming request.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800102 */
Jianing Weicb0652e2014-03-12 18:29:36 -0700103 virtual status_t clearStreamingRequest(int64_t *lastFrameNumber = NULL) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800104
105 /**
106 * Wait until a request with the given ID has been dequeued by the
107 * HAL. Returns TIMED_OUT if the timeout duration is reached. Returns
108 * immediately if the latest request received by the HAL has this id.
109 */
110 virtual status_t waitUntilRequestReceived(int32_t requestId,
111 nsecs_t timeout) = 0;
112
113 /**
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700114 * Create an output stream of the requested size, format, rotation and dataspace
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800115 *
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800116 * For HAL_PIXEL_FORMAT_BLOB formats, the width and height should be the
117 * logical dimensions of the buffer, not the number of bytes.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800118 */
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700119 virtual status_t createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800120 uint32_t width, uint32_t height, int format,
Zhijun He125684a2015-12-26 15:07:30 -0800121 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800122 const String8& physicalCameraId,
Emilian Peev40ead602017-09-26 15:46:36 +0100123 std::vector<int> *surfaceIds = nullptr,
Zhijun He5d677d12016-05-29 16:52:39 -0700124 int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID,
Emilian Peev050f5dc2017-05-18 14:43:56 +0100125 bool isShared = false, uint64_t consumerUsage = 0) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800126
127 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700128 * Create an output stream of the requested size, format, rotation and
129 * dataspace with a number of consumers.
130 *
131 * For HAL_PIXEL_FORMAT_BLOB formats, the width and height should be the
132 * logical dimensions of the buffer, not the number of bytes.
133 */
134 virtual status_t createStream(const std::vector<sp<Surface>>& consumers,
135 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
136 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -0800137 const String8& physicalCameraId,
Emilian Peev40ead602017-09-26 15:46:36 +0100138 std::vector<int> *surfaceIds = nullptr,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700139 int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID,
Emilian Peev050f5dc2017-05-18 14:43:56 +0100140 bool isShared = false, uint64_t consumerUsage = 0) = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700141
142 /**
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700143 * Create an input stream of width, height, and format.
144 *
145 * Return value is the stream ID if non-negative and an error if negative.
146 */
147 virtual status_t createInputStream(uint32_t width, uint32_t height,
148 int32_t format, /*out*/ int32_t *id) = 0;
149
Emilian Peev710c1422017-08-30 11:19:38 +0100150 struct StreamInfo {
151 uint32_t width;
152 uint32_t height;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700153
Emilian Peev710c1422017-08-30 11:19:38 +0100154 uint32_t format;
155 bool formatOverridden;
156 uint32_t originalFormat;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700157
Emilian Peev710c1422017-08-30 11:19:38 +0100158 android_dataspace dataSpace;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700159 bool dataSpaceOverridden;
160 android_dataspace originalDataSpace;
161
Emilian Peev710c1422017-08-30 11:19:38 +0100162 StreamInfo() : width(0), height(0), format(0), formatOverridden(false), originalFormat(0),
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700163 dataSpace(HAL_DATASPACE_UNKNOWN), dataSpaceOverridden(false),
164 originalDataSpace(HAL_DATASPACE_UNKNOWN) {}
Emilian Peev710c1422017-08-30 11:19:38 +0100165 /**
166 * Check whether the format matches the current or the original one in case
167 * it got overridden.
168 */
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700169 bool matchFormat(uint32_t clientFormat) const {
Emilian Peev710c1422017-08-30 11:19:38 +0100170 if ((formatOverridden && (originalFormat == clientFormat)) ||
171 (format == clientFormat)) {
172 return true;
173 }
174 return false;
175 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700176
177 /**
178 * Check whether the dataspace matches the current or the original one in case
179 * it got overridden.
180 */
181 bool matchDataSpace(android_dataspace clientDataSpace) const {
182 if ((dataSpaceOverridden && (originalDataSpace == clientDataSpace)) ||
183 (dataSpace == clientDataSpace)) {
184 return true;
185 }
186 return false;
187 }
188
Emilian Peev710c1422017-08-30 11:19:38 +0100189 };
190
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700191 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800192 * Get information about a given stream.
193 */
Emilian Peev710c1422017-08-30 11:19:38 +0100194 virtual status_t getStreamInfo(int id, StreamInfo *streamInfo) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800195
196 /**
197 * Set stream gralloc buffer transform
198 */
199 virtual status_t setStreamTransform(int id, int transform) = 0;
200
201 /**
202 * Delete stream. Must not be called if there are requests in flight which
203 * reference that stream.
204 */
205 virtual status_t deleteStream(int id) = 0;
206
207 /**
Igor Murashkine2d167e2014-08-19 16:19:59 -0700208 * Take the currently-defined set of streams and configure the HAL to use
209 * them. This is a long-running operation (may be several hundered ms).
210 *
211 * The device must be idle (see waitUntilDrained) before calling this.
212 *
213 * Returns OK on success; otherwise on error:
214 * - BAD_VALUE if the set of streams was invalid (e.g. fmts or sizes)
215 * - INVALID_OPERATION if the device was in the wrong state
216 */
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100217 virtual status_t configureStreams(const CameraMetadata& sessionParams,
218 int operatingMode = 0) = 0;
Igor Murashkine2d167e2014-08-19 16:19:59 -0700219
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700220 // get the buffer producer of the input stream
221 virtual status_t getInputBufferProducer(
222 sp<IGraphicBufferProducer> *producer) = 0;
223
Igor Murashkine2d167e2014-08-19 16:19:59 -0700224 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800225 * Create a metadata buffer with fields that the HAL device believes are
226 * best for the given use case
227 */
228 virtual status_t createDefaultRequest(int templateId,
229 CameraMetadata *request) = 0;
230
231 /**
232 * Wait until all requests have been processed. Returns INVALID_OPERATION if
233 * the streaming slot is not empty, or TIMED_OUT if the requests haven't
234 * finished processing in 10 seconds.
235 */
236 virtual status_t waitUntilDrained() = 0;
237
238 /**
Zhijun He28c9b6f2014-08-08 12:00:47 -0700239 * Get Jpeg buffer size for a given jpeg resolution.
240 * Negative values are error codes.
241 */
242 virtual ssize_t getJpegBufferSize(uint32_t width, uint32_t height) const = 0;
243
244 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800245 * Abstract class for HAL notification listeners
246 */
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700247 class NotificationListener : public virtual RefBase {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800248 public:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700249 // The set of notifications is a merge of the notifications required for
250 // API1 and API2.
251
252 // Required for API 1 and 2
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800253 virtual void notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -0700254 const CaptureResultExtras &resultExtras) = 0;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700255
256 // Required only for API2
257 virtual void notifyIdle() = 0;
Jianing Weicb0652e2014-03-12 18:29:36 -0700258 virtual void notifyShutter(const CaptureResultExtras &resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700259 nsecs_t timestamp) = 0;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700260 virtual void notifyPrepared(int streamId) = 0;
Shuzhen Wang9d066012016-09-30 11:30:20 -0700261 virtual void notifyRequestQueueEmpty() = 0;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700262
263 // Required only for API1
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800264 virtual void notifyAutoFocus(uint8_t newState, int triggerId) = 0;
265 virtual void notifyAutoExposure(uint8_t newState, int triggerId) = 0;
266 virtual void notifyAutoWhitebalance(uint8_t newState,
267 int triggerId) = 0;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700268 virtual void notifyRepeatingRequestError(long lastFrameNumber) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800269 protected:
270 virtual ~NotificationListener();
271 };
272
273 /**
274 * Connect HAL notifications to a listener. Overwrites previous
275 * listener. Set to NULL to stop receiving notifications.
276 */
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700277 virtual status_t setNotifyCallback(wp<NotificationListener> listener) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800278
279 /**
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -0700280 * Whether the device supports calling notifyAutofocus, notifyAutoExposure,
281 * and notifyAutoWhitebalance; if this returns false, the client must
282 * synthesize these notifications from received frame metadata.
283 */
284 virtual bool willNotify3A() = 0;
285
286 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800287 * Wait for a new frame to be produced, with timeout in nanoseconds.
288 * Returns TIMED_OUT when no frame produced within the specified duration
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700289 * May be called concurrently to most methods, except for getNextFrame
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800290 */
291 virtual status_t waitForNextFrame(nsecs_t timeout) = 0;
292
293 /**
Jianing Weicb0652e2014-03-12 18:29:36 -0700294 * Get next capture result frame from the result queue. Returns NOT_ENOUGH_DATA
295 * if the queue is empty; caller takes ownership of the metadata buffer inside
296 * the capture result object's metadata field.
297 * May be called concurrently to most methods, except for waitForNextFrame.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800298 */
Jianing Weicb0652e2014-03-12 18:29:36 -0700299 virtual status_t getNextResult(CaptureResult *frame) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800300
301 /**
302 * Trigger auto-focus. The latest ID used in a trigger autofocus or cancel
303 * autofocus call will be returned by the HAL in all subsequent AF
304 * notifications.
305 */
306 virtual status_t triggerAutofocus(uint32_t id) = 0;
307
308 /**
309 * Cancel auto-focus. The latest ID used in a trigger autofocus/cancel
310 * autofocus call will be returned by the HAL in all subsequent AF
311 * notifications.
312 */
313 virtual status_t triggerCancelAutofocus(uint32_t id) = 0;
314
315 /**
316 * Trigger pre-capture metering. The latest ID used in a trigger pre-capture
317 * call will be returned by the HAL in all subsequent AE and AWB
318 * notifications.
319 */
320 virtual status_t triggerPrecaptureMetering(uint32_t id) = 0;
321
322 /**
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700323 * Flush all pending and in-flight requests. Blocks until flush is
324 * complete.
Jianing Weicb0652e2014-03-12 18:29:36 -0700325 * Output lastFrameNumber is the last frame number of the previous streaming request.
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700326 */
Jianing Weicb0652e2014-03-12 18:29:36 -0700327 virtual status_t flush(int64_t *lastFrameNumber = NULL) = 0;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700328
Zhijun He204e3292014-07-14 17:09:23 -0700329 /**
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700330 * Prepare stream by preallocating buffers for it asynchronously.
331 * Calls notifyPrepared() once allocation is complete.
332 */
333 virtual status_t prepare(int streamId) = 0;
334
335 /**
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700336 * Free stream resources by dumping its unused gralloc buffers.
337 */
338 virtual status_t tearDown(int streamId) = 0;
339
340 /**
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700341 * Add buffer listener for a particular stream in the device.
342 */
343 virtual status_t addBufferListenerForStream(int streamId,
344 wp<camera3::Camera3StreamBufferListener> listener) = 0;
345
346 /**
Ruben Brunkc78ac262015-08-13 17:58:46 -0700347 * Prepare stream by preallocating up to maxCount buffers for it asynchronously.
348 * Calls notifyPrepared() once allocation is complete.
349 */
350 virtual status_t prepare(int maxCount, int streamId) = 0;
351
352 /**
Zhijun He5d677d12016-05-29 16:52:39 -0700353 * Set the deferred consumer surface and finish the rest of the stream configuration.
354 */
Shuzhen Wang758c2152017-01-10 18:26:18 -0800355 virtual status_t setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +0100356 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds /*out*/) = 0;
Zhijun He5d677d12016-05-29 16:52:39 -0700357
Emilian Peev40ead602017-09-26 15:46:36 +0100358 /**
359 * Update a given stream.
360 */
361 virtual status_t updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
362 const std::vector<android::camera3::OutputStreamInfo> &outputInfo,
363 const std::vector<size_t> &removedSurfaceIds,
364 KeyedVector<sp<Surface>, size_t> *outputMap/*out*/) = 0;
Chien-Yu Chena936ac22017-10-23 15:59:49 -0700365
366 /**
367 * Drop buffers for stream of streamId if dropping is true. If dropping is false, do not
368 * drop buffers for stream of streamId.
369 */
370 virtual status_t dropStreamBuffers(bool /*dropping*/, int /*streamId*/) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800371};
372
373}; // namespace android
374
375#endif