blob: f9b062a0423ee4e4cb34eb055ac0e268dfa14e94 [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"
Yin-Chia Yehe074a932015-01-30 10:29:02 -080033#include "common/CameraModule.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
Yin-Chia Yehe074a932015-01-30 10:29:02 -080058 virtual status_t initialize(CameraModule *module) = 0;
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080059 virtual status_t initialize(sp<CameraProviderManager> manager) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080060 virtual status_t disconnect() = 0;
61
Jianing Weicb0652e2014-03-12 18:29:36 -070062 virtual status_t dump(int fd, const Vector<String16> &args) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080063
64 /**
65 * The device's static characteristics metadata buffer
66 */
67 virtual const CameraMetadata& info() const = 0;
68
69 /**
70 * Submit request for capture. The CameraDevice takes ownership of the
71 * passed-in buffer.
Jianing Weicb0652e2014-03-12 18:29:36 -070072 * Output lastFrameNumber is the expected frame number of this request.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080073 */
Jianing Weicb0652e2014-03-12 18:29:36 -070074 virtual status_t capture(CameraMetadata &request, int64_t *lastFrameNumber = NULL) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080075
76 /**
Jianing Wei90e59c92014-03-12 18:29:36 -070077 * Submit a list of requests.
Jianing Weicb0652e2014-03-12 18:29:36 -070078 * Output lastFrameNumber is the expected last frame number of the list of requests.
Jianing Wei90e59c92014-03-12 18:29:36 -070079 */
Jianing Weicb0652e2014-03-12 18:29:36 -070080 virtual status_t captureList(const List<const CameraMetadata> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -070081 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -070082 int64_t *lastFrameNumber = NULL) = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -070083
84 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080085 * Submit request for streaming. The CameraDevice makes a copy of the
86 * passed-in buffer and the caller retains ownership.
Jianing Weicb0652e2014-03-12 18:29:36 -070087 * Output lastFrameNumber is the last frame number of the previous streaming request.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080088 */
Jianing Weicb0652e2014-03-12 18:29:36 -070089 virtual status_t setStreamingRequest(const CameraMetadata &request,
90 int64_t *lastFrameNumber = NULL) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080091
92 /**
Jianing Wei90e59c92014-03-12 18:29:36 -070093 * Submit a list of requests for streaming.
Jianing Weicb0652e2014-03-12 18:29:36 -070094 * Output lastFrameNumber is the last frame number of the previous streaming request.
Jianing Wei90e59c92014-03-12 18:29:36 -070095 */
Jianing Weicb0652e2014-03-12 18:29:36 -070096 virtual status_t setStreamingRequestList(const List<const CameraMetadata> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -070097 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -070098 int64_t *lastFrameNumber = NULL) = 0;
Jianing Wei90e59c92014-03-12 18:29:36 -070099
100 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800101 * Clear the streaming request slot.
Jianing Weicb0652e2014-03-12 18:29:36 -0700102 * Output lastFrameNumber is the last frame number of the previous streaming request.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800103 */
Jianing Weicb0652e2014-03-12 18:29:36 -0700104 virtual status_t clearStreamingRequest(int64_t *lastFrameNumber = NULL) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800105
106 /**
107 * Wait until a request with the given ID has been dequeued by the
108 * HAL. Returns TIMED_OUT if the timeout duration is reached. Returns
109 * immediately if the latest request received by the HAL has this id.
110 */
111 virtual status_t waitUntilRequestReceived(int32_t requestId,
112 nsecs_t timeout) = 0;
113
114 /**
Yin-Chia Yehb97babb2015-03-12 13:42:44 -0700115 * Create an output stream of the requested size, format, rotation and dataspace
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800116 *
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800117 * For HAL_PIXEL_FORMAT_BLOB formats, the width and height should be the
118 * logical dimensions of the buffer, not the number of bytes.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800119 */
Eino-Ville Talvala727d1722015-06-09 13:44:19 -0700120 virtual status_t createStream(sp<Surface> consumer,
Eino-Ville Talvala3d82c0d2015-02-23 15:19:19 -0800121 uint32_t width, uint32_t height, int format,
Zhijun He125684a2015-12-26 15:07:30 -0800122 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
Zhijun He5d677d12016-05-29 16:52:39 -0700123 int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID,
Shuzhen Wang758c2152017-01-10 18:26:18 -0800124 bool isShared = false, uint32_t consumerUsage = 0) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800125
126 /**
Shuzhen Wang0129d522016-10-30 22:43:41 -0700127 * Create an output stream of the requested size, format, rotation and
128 * dataspace with a number of consumers.
129 *
130 * For HAL_PIXEL_FORMAT_BLOB formats, the width and height should be the
131 * logical dimensions of the buffer, not the number of bytes.
132 */
133 virtual status_t createStream(const std::vector<sp<Surface>>& consumers,
134 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
135 android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
136 int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID,
Shuzhen Wang758c2152017-01-10 18:26:18 -0800137 bool isShared = false, uint32_t consumerUsage = 0) = 0;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700138
139 /**
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700140 * Create an input stream of width, height, and format.
141 *
142 * Return value is the stream ID if non-negative and an error if negative.
143 */
144 virtual status_t createInputStream(uint32_t width, uint32_t height,
145 int32_t format, /*out*/ int32_t *id) = 0;
146
147 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800148 * Create an input reprocess stream that uses buffers from an existing
149 * output stream.
150 */
151 virtual status_t createReprocessStreamFromStream(int outputId, int *id) = 0;
152
153 /**
154 * Get information about a given stream.
155 */
156 virtual status_t getStreamInfo(int id,
Eino-Ville Talvalad46a6b92015-05-14 17:26:24 -0700157 uint32_t *width, uint32_t *height,
158 uint32_t *format, android_dataspace *dataSpace) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800159
160 /**
161 * Set stream gralloc buffer transform
162 */
163 virtual status_t setStreamTransform(int id, int transform) = 0;
164
165 /**
166 * Delete stream. Must not be called if there are requests in flight which
167 * reference that stream.
168 */
169 virtual status_t deleteStream(int id) = 0;
170
171 /**
172 * Delete reprocess stream. Must not be called if there are requests in
173 * flight which reference that stream.
174 */
175 virtual status_t deleteReprocessStream(int id) = 0;
176
177 /**
Igor Murashkine2d167e2014-08-19 16:19:59 -0700178 * Take the currently-defined set of streams and configure the HAL to use
179 * them. This is a long-running operation (may be several hundered ms).
180 *
181 * The device must be idle (see waitUntilDrained) before calling this.
182 *
183 * Returns OK on success; otherwise on error:
184 * - BAD_VALUE if the set of streams was invalid (e.g. fmts or sizes)
185 * - INVALID_OPERATION if the device was in the wrong state
186 */
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800187 virtual status_t configureStreams(int operatingMode = 0) = 0;
Igor Murashkine2d167e2014-08-19 16:19:59 -0700188
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -0700189 // get the buffer producer of the input stream
190 virtual status_t getInputBufferProducer(
191 sp<IGraphicBufferProducer> *producer) = 0;
192
Igor Murashkine2d167e2014-08-19 16:19:59 -0700193 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800194 * Create a metadata buffer with fields that the HAL device believes are
195 * best for the given use case
196 */
197 virtual status_t createDefaultRequest(int templateId,
198 CameraMetadata *request) = 0;
199
200 /**
201 * Wait until all requests have been processed. Returns INVALID_OPERATION if
202 * the streaming slot is not empty, or TIMED_OUT if the requests haven't
203 * finished processing in 10 seconds.
204 */
205 virtual status_t waitUntilDrained() = 0;
206
207 /**
Zhijun He28c9b6f2014-08-08 12:00:47 -0700208 * Get Jpeg buffer size for a given jpeg resolution.
209 * Negative values are error codes.
210 */
211 virtual ssize_t getJpegBufferSize(uint32_t width, uint32_t height) const = 0;
212
213 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800214 * Abstract class for HAL notification listeners
215 */
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700216 class NotificationListener : public virtual RefBase {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800217 public:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700218 // The set of notifications is a merge of the notifications required for
219 // API1 and API2.
220
221 // Required for API 1 and 2
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800222 virtual void notifyError(int32_t errorCode,
Jianing Weicb0652e2014-03-12 18:29:36 -0700223 const CaptureResultExtras &resultExtras) = 0;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700224
225 // Required only for API2
226 virtual void notifyIdle() = 0;
Jianing Weicb0652e2014-03-12 18:29:36 -0700227 virtual void notifyShutter(const CaptureResultExtras &resultExtras,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700228 nsecs_t timestamp) = 0;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700229 virtual void notifyPrepared(int streamId) = 0;
Shuzhen Wang9d066012016-09-30 11:30:20 -0700230 virtual void notifyRequestQueueEmpty() = 0;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700231
232 // Required only for API1
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800233 virtual void notifyAutoFocus(uint8_t newState, int triggerId) = 0;
234 virtual void notifyAutoExposure(uint8_t newState, int triggerId) = 0;
235 virtual void notifyAutoWhitebalance(uint8_t newState,
236 int triggerId) = 0;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -0700237 virtual void notifyRepeatingRequestError(long lastFrameNumber) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800238 protected:
239 virtual ~NotificationListener();
240 };
241
242 /**
243 * Connect HAL notifications to a listener. Overwrites previous
244 * listener. Set to NULL to stop receiving notifications.
245 */
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700246 virtual status_t setNotifyCallback(wp<NotificationListener> listener) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800247
248 /**
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -0700249 * Whether the device supports calling notifyAutofocus, notifyAutoExposure,
250 * and notifyAutoWhitebalance; if this returns false, the client must
251 * synthesize these notifications from received frame metadata.
252 */
253 virtual bool willNotify3A() = 0;
254
255 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800256 * Wait for a new frame to be produced, with timeout in nanoseconds.
257 * Returns TIMED_OUT when no frame produced within the specified duration
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700258 * May be called concurrently to most methods, except for getNextFrame
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800259 */
260 virtual status_t waitForNextFrame(nsecs_t timeout) = 0;
261
262 /**
Jianing Weicb0652e2014-03-12 18:29:36 -0700263 * Get next capture result frame from the result queue. Returns NOT_ENOUGH_DATA
264 * if the queue is empty; caller takes ownership of the metadata buffer inside
265 * the capture result object's metadata field.
266 * May be called concurrently to most methods, except for waitForNextFrame.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800267 */
Jianing Weicb0652e2014-03-12 18:29:36 -0700268 virtual status_t getNextResult(CaptureResult *frame) = 0;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800269
270 /**
271 * Trigger auto-focus. The latest ID used in a trigger autofocus or cancel
272 * autofocus call will be returned by the HAL in all subsequent AF
273 * notifications.
274 */
275 virtual status_t triggerAutofocus(uint32_t id) = 0;
276
277 /**
278 * Cancel auto-focus. The latest ID used in a trigger autofocus/cancel
279 * autofocus call will be returned by the HAL in all subsequent AF
280 * notifications.
281 */
282 virtual status_t triggerCancelAutofocus(uint32_t id) = 0;
283
284 /**
285 * Trigger pre-capture metering. The latest ID used in a trigger pre-capture
286 * call will be returned by the HAL in all subsequent AE and AWB
287 * notifications.
288 */
289 virtual status_t triggerPrecaptureMetering(uint32_t id) = 0;
290
291 /**
292 * Abstract interface for clients that want to listen to reprocess buffer
293 * release events
294 */
295 struct BufferReleasedListener : public virtual RefBase {
296 virtual void onBufferReleased(buffer_handle_t *handle) = 0;
297 };
298
299 /**
300 * Push a buffer to be reprocessed into a reprocessing stream, and
301 * provide a listener to call once the buffer is returned by the HAL
302 */
303 virtual status_t pushReprocessBuffer(int reprocessStreamId,
304 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) = 0;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700305
306 /**
307 * Flush all pending and in-flight requests. Blocks until flush is
308 * complete.
Jianing Weicb0652e2014-03-12 18:29:36 -0700309 * Output lastFrameNumber is the last frame number of the previous streaming request.
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700310 */
Jianing Weicb0652e2014-03-12 18:29:36 -0700311 virtual status_t flush(int64_t *lastFrameNumber = NULL) = 0;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700312
Zhijun He204e3292014-07-14 17:09:23 -0700313 /**
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700314 * Prepare stream by preallocating buffers for it asynchronously.
315 * Calls notifyPrepared() once allocation is complete.
316 */
317 virtual status_t prepare(int streamId) = 0;
318
319 /**
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -0700320 * Free stream resources by dumping its unused gralloc buffers.
321 */
322 virtual status_t tearDown(int streamId) = 0;
323
324 /**
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -0700325 * Add buffer listener for a particular stream in the device.
326 */
327 virtual status_t addBufferListenerForStream(int streamId,
328 wp<camera3::Camera3StreamBufferListener> listener) = 0;
329
330 /**
Ruben Brunkc78ac262015-08-13 17:58:46 -0700331 * Prepare stream by preallocating up to maxCount buffers for it asynchronously.
332 * Calls notifyPrepared() once allocation is complete.
333 */
334 virtual status_t prepare(int maxCount, int streamId) = 0;
335
336 /**
Zhijun He204e3292014-07-14 17:09:23 -0700337 * Get the HAL device version.
338 */
339 virtual uint32_t getDeviceVersion() = 0;
Zhijun He5d677d12016-05-29 16:52:39 -0700340
341 /**
342 * Set the deferred consumer surface and finish the rest of the stream configuration.
343 */
Shuzhen Wang758c2152017-01-10 18:26:18 -0800344 virtual status_t setConsumerSurfaces(int streamId,
345 const std::vector<sp<Surface>>& consumers) = 0;
Zhijun He5d677d12016-05-29 16:52:39 -0700346
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800347};
348
349}; // namespace android
350
351#endif