blob: a90050e1bf519e4f4b1c5b9e0fc957ea9f45aa1b [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
2 * Copyright (C) 2013 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_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>
26#include <utils/Timers.h>
Jianing Wei90e59c92014-03-12 18:29:36 -070027#include <utils/List.h>
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080028
29#include "hardware/camera2.h"
Zhijun He0ea8fa42014-07-07 17:05:38 -070030#include "hardware/camera3.h"
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080031#include "camera/CameraMetadata.h"
Jianing Weicb0652e2014-03-12 18:29:36 -070032#include "camera/CaptureResult.h"
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070033#include "gui/IGraphicBufferProducer.h"
Zhijun He125684a2015-12-26 15:07:30 -080034#include "device3/Camera3StreamInterface.h"
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080035#include "binder/Status.h"
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080036
37namespace android {
38
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080039class CameraProviderManager;
40
Shuzhen Wang0129d522016-10-30 22:43:41 -070041// Mapping of output stream index to surface ids
42typedef std::unordered_map<int, std::vector<size_t> > SurfaceMap;
43
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080044/**
45 * Base interface for version >= 2 camera device classes, which interface to
46 * camera HAL device versions >= 2.
47 */
48class CameraDeviceBase : public virtual RefBase {
49 public:
50 virtual ~CameraDeviceBase();
51
Igor Murashkin71381052013-03-04 14:53:08 -080052 /**
53 * The device's camera ID
54 */
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080055 virtual const String8& getId() const = 0;
Igor Murashkin71381052013-03-04 14:53:08 -080056
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080057 virtual status_t initialize(sp<CameraProviderManager> manager) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058 virtual status_t disconnect() = 0;
59
Jianing Weicb0652e2014-03-12 18:29:36 -070060 virtual status_t dump(int fd, const Vector<String16> &args) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080061
62 /**
63 * The device's static characteristics metadata buffer
64 */
65 virtual const CameraMetadata& info() const = 0;
66
67 /**
68 * Submit request for capture. The CameraDevice takes ownership of the
69 * passed-in buffer.
Jianing Weicb0652e2014-03-12 18:29:36 -070070 * Output lastFrameNumber is the expected frame number of this request.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080071 */
Jianing Weicb0652e2014-03-12 18:29:36 -070072 virtual status_t capture(CameraMetadata &request, int64_t *lastFrameNumber = NULL) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080073
74 /**
Jianing Wei90e59c92014-03-12 18:29:36 -070075 * Submit a list of requests.
Jianing Weicb0652e2014-03-12 18:29:36 -070076 * Output lastFrameNumber is the expected last frame number of the list of requests.
Jianing Wei90e59c92014-03-12 18:29:36 -070077 */
Jianing Weicb0652e2014-03-12 18:29:36 -070078 virtual status_t captureList(const List<const CameraMetadata> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -070079 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -070080 int64_t *lastFrameNumber = NULL) = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -070081
82 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080083 * Submit request for streaming. The CameraDevice makes a copy of the
84 * passed-in buffer and the caller retains ownership.
Jianing Weicb0652e2014-03-12 18:29:36 -070085 * Output lastFrameNumber is the last frame number of the previous streaming request.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080086 */
Jianing Weicb0652e2014-03-12 18:29:36 -070087 virtual status_t setStreamingRequest(const CameraMetadata &request,
88 int64_t *lastFrameNumber = NULL) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080089
90 /**
Jianing Wei90e59c92014-03-12 18:29:36 -070091 * Submit a list of requests for streaming.
Jianing Weicb0652e2014-03-12 18:29:36 -070092 * Output lastFrameNumber is the last frame number of the previous streaming request.
Jianing Wei90e59c92014-03-12 18:29:36 -070093 */
Jianing Weicb0652e2014-03-12 18:29:36 -070094 virtual status_t setStreamingRequestList(const List<const CameraMetadata> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -070095 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -070096 int64_t *lastFrameNumber = NULL) = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -070097
98 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080099 * Clear the streaming request slot.
Jianing Weicb0652e2014-03-12 18:29:36 -0700100 * Output lastFrameNumber is the last frame number of the previous streaming request.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800101 */
Jianing Weicb0652e2014-03-12 18:29:36 -0700102 virtual status_t clearStreamingRequest(int64_t *lastFrameNumber = NULL) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800103
104 /**
105 * Wait until a request with the given ID has been dequeued by the
106 * HAL. Returns TIMED_OUT if the timeout duration is reached. Returns
107 * immediately if the latest request received by the HAL has this id.
108 */
109 virtual status_t waitUntilRequestReceived(int32_t requestId,
110 nsecs_t timeout) = 0;
111
112 /**
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700113 * Create an output stream of the requested size, format, rotation and dataspace
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800114 *
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800115 * For HAL_PIXEL_FORMAT_BLOB formats, the width and height should be the
116 * logical dimensions of the buffer, not the number of bytes.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800117 */
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700118 virtual status_t createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800119 uint32_t width, uint32_t height, int format,
Zhijun He125684a2015-12-26 15:07:30 -0800120 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Zhijun He5d677d12016-05-29 16:52:39 -0700121 int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID,
Emilian Peev050f5dc2017-05-18 14:43:56 +0100122 bool isShared = false, uint64_t consumerUsage = 0) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800123
124 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700125 * Create an output stream of the requested size, format, rotation and
126 * dataspace with a number of consumers.
127 *
128 * For HAL_PIXEL_FORMAT_BLOB formats, the width and height should be the
129 * logical dimensions of the buffer, not the number of bytes.
130 */
131 virtual status_t createStream(const std::vector<sp<Surface>>& consumers,
132 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
133 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
134 int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID,
Emilian Peev050f5dc2017-05-18 14:43:56 +0100135 bool isShared = false, uint64_t consumerUsage = 0) = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700136
137 /**
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700138 * Create an input stream of width, height, and format.
139 *
140 * Return value is the stream ID if non-negative and an error if negative.
141 */
142 virtual status_t createInputStream(uint32_t width, uint32_t height,
143 int32_t format, /*out*/ int32_t *id) = 0;
144
Emilian Peev710c1422017-08-30 11:19:38 +0100145 struct StreamInfo {
146 uint32_t width;
147 uint32_t height;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700148
Emilian Peev710c1422017-08-30 11:19:38 +0100149 uint32_t format;
150 bool formatOverridden;
151 uint32_t originalFormat;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700152
Emilian Peev710c1422017-08-30 11:19:38 +0100153 android_dataspace dataSpace;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700154 bool dataSpaceOverridden;
155 android_dataspace originalDataSpace;
156
Emilian Peev710c1422017-08-30 11:19:38 +0100157 StreamInfo() : width(0), height(0), format(0), formatOverridden(false), originalFormat(0),
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700158 dataSpace(HAL_DATASPACE_UNKNOWN), dataSpaceOverridden(false),
159 originalDataSpace(HAL_DATASPACE_UNKNOWN) {}
Emilian Peev710c1422017-08-30 11:19:38 +0100160 /**
161 * Check whether the format matches the current or the original one in case
162 * it got overridden.
163 */
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700164 bool matchFormat(uint32_t clientFormat) const {
Emilian Peev710c1422017-08-30 11:19:38 +0100165 if ((formatOverridden && (originalFormat == clientFormat)) ||
166 (format == clientFormat)) {
167 return true;
168 }
169 return false;
170 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700171
172 /**
173 * Check whether the dataspace matches the current or the original one in case
174 * it got overridden.
175 */
176 bool matchDataSpace(android_dataspace clientDataSpace) const {
177 if ((dataSpaceOverridden && (originalDataSpace == clientDataSpace)) ||
178 (dataSpace == clientDataSpace)) {
179 return true;
180 }
181 return false;
182 }
183
Emilian Peev710c1422017-08-30 11:19:38 +0100184 };
185
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700186 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800187 * Get information about a given stream.
188 */
Emilian Peev710c1422017-08-30 11:19:38 +0100189 virtual status_t getStreamInfo(int id, StreamInfo *streamInfo) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800190
191 /**
192 * Set stream gralloc buffer transform
193 */
194 virtual status_t setStreamTransform(int id, int transform) = 0;
195
196 /**
197 * Delete stream. Must not be called if there are requests in flight which
198 * reference that stream.
199 */
200 virtual status_t deleteStream(int id) = 0;
201
202 /**
Igor Murashkine2d167e2014-08-19 16:19:59 -0700203 * Take the currently-defined set of streams and configure the HAL to use
204 * them. This is a long-running operation (may be several hundered ms).
205 *
206 * The device must be idle (see waitUntilDrained) before calling this.
207 *
208 * Returns OK on success; otherwise on error:
209 * - BAD_VALUE if the set of streams was invalid (e.g. fmts or sizes)
210 * - INVALID_OPERATION if the device was in the wrong state
211 */
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800212 virtual status_t configureStreams(int operatingMode = 0) = 0;
Igor Murashkine2d167e2014-08-19 16:19:59 -0700213
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700214 // get the buffer producer of the input stream
215 virtual status_t getInputBufferProducer(
216 sp<IGraphicBufferProducer> *producer) = 0;
217
Igor Murashkine2d167e2014-08-19 16:19:59 -0700218 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800219 * Create a metadata buffer with fields that the HAL device believes are
220 * best for the given use case
221 */
222 virtual status_t createDefaultRequest(int templateId,
223 CameraMetadata *request) = 0;
224
225 /**
226 * Wait until all requests have been processed. Returns INVALID_OPERATION if
227 * the streaming slot is not empty, or TIMED_OUT if the requests haven't
228 * finished processing in 10 seconds.
229 */
230 virtual status_t waitUntilDrained() = 0;
231
232 /**
Zhijun He28c9b6f2014-08-08 12:00:47 -0700233 * Get Jpeg buffer size for a given jpeg resolution.
234 * Negative values are error codes.
235 */
236 virtual ssize_t getJpegBufferSize(uint32_t width, uint32_t height) const = 0;
237
238 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800239 * Abstract class for HAL notification listeners
240 */
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700241 class NotificationListener : public virtual RefBase {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800242 public:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700243 // The set of notifications is a merge of the notifications required for
244 // API1 and API2.
245
246 // Required for API 1 and 2
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800247 virtual void notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -0700248 const CaptureResultExtras &resultExtras) = 0;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700249
250 // Required only for API2
251 virtual void notifyIdle() = 0;
Jianing Weicb0652e2014-03-12 18:29:36 -0700252 virtual void notifyShutter(const CaptureResultExtras &resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700253 nsecs_t timestamp) = 0;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700254 virtual void notifyPrepared(int streamId) = 0;
Shuzhen Wang9d066012016-09-30 11:30:20 -0700255 virtual void notifyRequestQueueEmpty() = 0;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700256
257 // Required only for API1
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800258 virtual void notifyAutoFocus(uint8_t newState, int triggerId) = 0;
259 virtual void notifyAutoExposure(uint8_t newState, int triggerId) = 0;
260 virtual void notifyAutoWhitebalance(uint8_t newState,
261 int triggerId) = 0;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700262 virtual void notifyRepeatingRequestError(long lastFrameNumber) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800263 protected:
264 virtual ~NotificationListener();
265 };
266
267 /**
268 * Connect HAL notifications to a listener. Overwrites previous
269 * listener. Set to NULL to stop receiving notifications.
270 */
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700271 virtual status_t setNotifyCallback(wp<NotificationListener> listener) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800272
273 /**
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -0700274 * Whether the device supports calling notifyAutofocus, notifyAutoExposure,
275 * and notifyAutoWhitebalance; if this returns false, the client must
276 * synthesize these notifications from received frame metadata.
277 */
278 virtual bool willNotify3A() = 0;
279
280 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800281 * Wait for a new frame to be produced, with timeout in nanoseconds.
282 * Returns TIMED_OUT when no frame produced within the specified duration
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700283 * May be called concurrently to most methods, except for getNextFrame
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800284 */
285 virtual status_t waitForNextFrame(nsecs_t timeout) = 0;
286
287 /**
Jianing Weicb0652e2014-03-12 18:29:36 -0700288 * Get next capture result frame from the result queue. Returns NOT_ENOUGH_DATA
289 * if the queue is empty; caller takes ownership of the metadata buffer inside
290 * the capture result object's metadata field.
291 * May be called concurrently to most methods, except for waitForNextFrame.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800292 */
Jianing Weicb0652e2014-03-12 18:29:36 -0700293 virtual status_t getNextResult(CaptureResult *frame) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800294
295 /**
296 * Trigger auto-focus. The latest ID used in a trigger autofocus or cancel
297 * autofocus call will be returned by the HAL in all subsequent AF
298 * notifications.
299 */
300 virtual status_t triggerAutofocus(uint32_t id) = 0;
301
302 /**
303 * Cancel auto-focus. The latest ID used in a trigger autofocus/cancel
304 * autofocus call will be returned by the HAL in all subsequent AF
305 * notifications.
306 */
307 virtual status_t triggerCancelAutofocus(uint32_t id) = 0;
308
309 /**
310 * Trigger pre-capture metering. The latest ID used in a trigger pre-capture
311 * call will be returned by the HAL in all subsequent AE and AWB
312 * notifications.
313 */
314 virtual status_t triggerPrecaptureMetering(uint32_t id) = 0;
315
316 /**
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700317 * Flush all pending and in-flight requests. Blocks until flush is
318 * complete.
Jianing Weicb0652e2014-03-12 18:29:36 -0700319 * Output lastFrameNumber is the last frame number of the previous streaming request.
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700320 */
Jianing Weicb0652e2014-03-12 18:29:36 -0700321 virtual status_t flush(int64_t *lastFrameNumber = NULL) = 0;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700322
Zhijun He204e3292014-07-14 17:09:23 -0700323 /**
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700324 * Prepare stream by preallocating buffers for it asynchronously.
325 * Calls notifyPrepared() once allocation is complete.
326 */
327 virtual status_t prepare(int streamId) = 0;
328
329 /**
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700330 * Free stream resources by dumping its unused gralloc buffers.
331 */
332 virtual status_t tearDown(int streamId) = 0;
333
334 /**
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700335 * Add buffer listener for a particular stream in the device.
336 */
337 virtual status_t addBufferListenerForStream(int streamId,
338 wp<camera3::Camera3StreamBufferListener> listener) = 0;
339
340 /**
Ruben Brunkc78ac262015-08-13 17:58:46 -0700341 * Prepare stream by preallocating up to maxCount buffers for it asynchronously.
342 * Calls notifyPrepared() once allocation is complete.
343 */
344 virtual status_t prepare(int maxCount, int streamId) = 0;
345
346 /**
Zhijun He5d677d12016-05-29 16:52:39 -0700347 * Set the deferred consumer surface and finish the rest of the stream configuration.
348 */
Shuzhen Wang758c2152017-01-10 18:26:18 -0800349 virtual status_t setConsumerSurfaces(int streamId,
350 const std::vector<sp<Surface>>& consumers) = 0;
Zhijun He5d677d12016-05-29 16:52:39 -0700351
Chien-Yu Chen63068522017-10-23 15:59:49 -0700352 /**
353 * Drop buffers for stream of streamId if dropping is true. If dropping is false, do not
354 * drop buffers for stream of streamId.
355 */
356 virtual status_t dropStreamBuffers(bool /*dropping*/, int /*streamId*/) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800357};
358
359}; // namespace android
360
361#endif