• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
20 #include <utils/RefBase.h>
21 #include <utils/String8.h>
22 #include <utils/String16.h>
23 #include <utils/Vector.h>
24 #include <utils/Timers.h>
25 #include <utils/List.h>
26 
27 #include "hardware/camera2.h"
28 #include "hardware/camera3.h"
29 #include "camera/CameraMetadata.h"
30 #include "camera/CaptureResult.h"
31 #include "common/CameraModule.h"
32 #include "gui/IGraphicBufferProducer.h"
33 #include "device3/Camera3StreamInterface.h"
34 #include "binder/Status.h"
35 
36 namespace android {
37 
38 /**
39  * Base interface for version >= 2 camera device classes, which interface to
40  * camera HAL device versions >= 2.
41  */
42 class CameraDeviceBase : public virtual RefBase {
43   public:
44     virtual ~CameraDeviceBase();
45 
46     /**
47      * The device's camera ID
48      */
49     virtual int      getId() const = 0;
50 
51     virtual status_t initialize(CameraModule *module) = 0;
52     virtual status_t disconnect() = 0;
53 
54     virtual status_t dump(int fd, const Vector<String16> &args) = 0;
55 
56     /**
57      * The device's static characteristics metadata buffer
58      */
59     virtual const CameraMetadata& info() const = 0;
60 
61     /**
62      * Submit request for capture. The CameraDevice takes ownership of the
63      * passed-in buffer.
64      * Output lastFrameNumber is the expected frame number of this request.
65      */
66     virtual status_t capture(CameraMetadata &request, int64_t *lastFrameNumber = NULL) = 0;
67 
68     /**
69      * Submit a list of requests.
70      * Output lastFrameNumber is the expected last frame number of the list of requests.
71      */
72     virtual status_t captureList(const List<const CameraMetadata> &requests,
73                                  int64_t *lastFrameNumber = NULL) = 0;
74 
75     /**
76      * Submit request for streaming. The CameraDevice makes a copy of the
77      * passed-in buffer and the caller retains ownership.
78      * Output lastFrameNumber is the last frame number of the previous streaming request.
79      */
80     virtual status_t setStreamingRequest(const CameraMetadata &request,
81                                          int64_t *lastFrameNumber = NULL) = 0;
82 
83     /**
84      * Submit a list of requests for streaming.
85      * Output lastFrameNumber is the last frame number of the previous streaming request.
86      */
87     virtual status_t setStreamingRequestList(const List<const CameraMetadata> &requests,
88                                              int64_t *lastFrameNumber = NULL) = 0;
89 
90     /**
91      * Clear the streaming request slot.
92      * Output lastFrameNumber is the last frame number of the previous streaming request.
93      */
94     virtual status_t clearStreamingRequest(int64_t *lastFrameNumber = NULL) = 0;
95 
96     /**
97      * Wait until a request with the given ID has been dequeued by the
98      * HAL. Returns TIMED_OUT if the timeout duration is reached. Returns
99      * immediately if the latest request received by the HAL has this id.
100      */
101     virtual status_t waitUntilRequestReceived(int32_t requestId,
102             nsecs_t timeout) = 0;
103 
104     /**
105      * Create an output stream of the requested size, format, rotation and dataspace
106      *
107      * For HAL_PIXEL_FORMAT_BLOB formats, the width and height should be the
108      * logical dimensions of the buffer, not the number of bytes.
109      */
110     virtual status_t createStream(sp<Surface> consumer,
111             uint32_t width, uint32_t height, int format,
112             android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
113             int streamSetId = camera3::CAMERA3_STREAM_SET_ID_INVALID,
114             uint32_t consumerUsage = 0) = 0;
115 
116     /**
117      * Create an input stream of width, height, and format.
118      *
119      * Return value is the stream ID if non-negative and an error if negative.
120      */
121     virtual status_t createInputStream(uint32_t width, uint32_t height,
122             int32_t format, /*out*/ int32_t *id) = 0;
123 
124     /**
125      * Create an input reprocess stream that uses buffers from an existing
126      * output stream.
127      */
128     virtual status_t createReprocessStreamFromStream(int outputId, int *id) = 0;
129 
130     /**
131      * Get information about a given stream.
132      */
133     virtual status_t getStreamInfo(int id,
134             uint32_t *width, uint32_t *height,
135             uint32_t *format, android_dataspace *dataSpace) = 0;
136 
137     /**
138      * Set stream gralloc buffer transform
139      */
140     virtual status_t setStreamTransform(int id, int transform) = 0;
141 
142     /**
143      * Delete stream. Must not be called if there are requests in flight which
144      * reference that stream.
145      */
146     virtual status_t deleteStream(int id) = 0;
147 
148     /**
149      * Delete reprocess stream. Must not be called if there are requests in
150      * flight which reference that stream.
151      */
152     virtual status_t deleteReprocessStream(int id) = 0;
153 
154     /**
155      * Take the currently-defined set of streams and configure the HAL to use
156      * them. This is a long-running operation (may be several hundered ms).
157      *
158      * The device must be idle (see waitUntilDrained) before calling this.
159      *
160      * Returns OK on success; otherwise on error:
161      * - BAD_VALUE if the set of streams was invalid (e.g. fmts or sizes)
162      * - INVALID_OPERATION if the device was in the wrong state
163      */
164     virtual status_t configureStreams(bool isConstrainedHighSpeed = false) = 0;
165 
166     // get the buffer producer of the input stream
167     virtual status_t getInputBufferProducer(
168             sp<IGraphicBufferProducer> *producer) = 0;
169 
170     /**
171      * Create a metadata buffer with fields that the HAL device believes are
172      * best for the given use case
173      */
174     virtual status_t createDefaultRequest(int templateId,
175             CameraMetadata *request) = 0;
176 
177     /**
178      * Wait until all requests have been processed. Returns INVALID_OPERATION if
179      * the streaming slot is not empty, or TIMED_OUT if the requests haven't
180      * finished processing in 10 seconds.
181      */
182     virtual status_t waitUntilDrained() = 0;
183 
184     /**
185      * Get Jpeg buffer size for a given jpeg resolution.
186      * Negative values are error codes.
187      */
188     virtual ssize_t getJpegBufferSize(uint32_t width, uint32_t height) const = 0;
189 
190     /**
191      * Abstract class for HAL notification listeners
192      */
193     class NotificationListener : public virtual RefBase {
194       public:
195         // The set of notifications is a merge of the notifications required for
196         // API1 and API2.
197 
198         // Required for API 1 and 2
199         virtual void notifyError(int32_t errorCode,
200                                  const CaptureResultExtras &resultExtras) = 0;
201 
202         // Required only for API2
203         virtual void notifyIdle() = 0;
204         virtual void notifyShutter(const CaptureResultExtras &resultExtras,
205                 nsecs_t timestamp) = 0;
206         virtual void notifyPrepared(int streamId) = 0;
207 
208         // Required only for API1
209         virtual void notifyAutoFocus(uint8_t newState, int triggerId) = 0;
210         virtual void notifyAutoExposure(uint8_t newState, int triggerId) = 0;
211         virtual void notifyAutoWhitebalance(uint8_t newState,
212                 int triggerId) = 0;
213         virtual void notifyRepeatingRequestError(long lastFrameNumber) = 0;
214       protected:
215         virtual ~NotificationListener();
216     };
217 
218     /**
219      * Connect HAL notifications to a listener. Overwrites previous
220      * listener. Set to NULL to stop receiving notifications.
221      */
222     virtual status_t setNotifyCallback(wp<NotificationListener> listener) = 0;
223 
224     /**
225      * Whether the device supports calling notifyAutofocus, notifyAutoExposure,
226      * and notifyAutoWhitebalance; if this returns false, the client must
227      * synthesize these notifications from received frame metadata.
228      */
229     virtual bool     willNotify3A() = 0;
230 
231     /**
232      * Wait for a new frame to be produced, with timeout in nanoseconds.
233      * Returns TIMED_OUT when no frame produced within the specified duration
234      * May be called concurrently to most methods, except for getNextFrame
235      */
236     virtual status_t waitForNextFrame(nsecs_t timeout) = 0;
237 
238     /**
239      * Get next capture result frame from the result queue. Returns NOT_ENOUGH_DATA
240      * if the queue is empty; caller takes ownership of the metadata buffer inside
241      * the capture result object's metadata field.
242      * May be called concurrently to most methods, except for waitForNextFrame.
243      */
244     virtual status_t getNextResult(CaptureResult *frame) = 0;
245 
246     /**
247      * Trigger auto-focus. The latest ID used in a trigger autofocus or cancel
248      * autofocus call will be returned by the HAL in all subsequent AF
249      * notifications.
250      */
251     virtual status_t triggerAutofocus(uint32_t id) = 0;
252 
253     /**
254      * Cancel auto-focus. The latest ID used in a trigger autofocus/cancel
255      * autofocus call will be returned by the HAL in all subsequent AF
256      * notifications.
257      */
258     virtual status_t triggerCancelAutofocus(uint32_t id) = 0;
259 
260     /**
261      * Trigger pre-capture metering. The latest ID used in a trigger pre-capture
262      * call will be returned by the HAL in all subsequent AE and AWB
263      * notifications.
264      */
265     virtual status_t triggerPrecaptureMetering(uint32_t id) = 0;
266 
267     /**
268      * Abstract interface for clients that want to listen to reprocess buffer
269      * release events
270      */
271     struct BufferReleasedListener : public virtual RefBase {
272         virtual void onBufferReleased(buffer_handle_t *handle) = 0;
273     };
274 
275     /**
276      * Push a buffer to be reprocessed into a reprocessing stream, and
277      * provide a listener to call once the buffer is returned by the HAL
278      */
279     virtual status_t pushReprocessBuffer(int reprocessStreamId,
280             buffer_handle_t *buffer, wp<BufferReleasedListener> listener) = 0;
281 
282     /**
283      * Flush all pending and in-flight requests. Blocks until flush is
284      * complete.
285      * Output lastFrameNumber is the last frame number of the previous streaming request.
286      */
287     virtual status_t flush(int64_t *lastFrameNumber = NULL) = 0;
288 
289     /**
290      * Prepare stream by preallocating buffers for it asynchronously.
291      * Calls notifyPrepared() once allocation is complete.
292      */
293     virtual status_t prepare(int streamId) = 0;
294 
295     /**
296      * Free stream resources by dumping its unused gralloc buffers.
297      */
298     virtual status_t tearDown(int streamId) = 0;
299 
300     /**
301      * Add buffer listener for a particular stream in the device.
302      */
303     virtual status_t addBufferListenerForStream(int streamId,
304             wp<camera3::Camera3StreamBufferListener> listener) = 0;
305 
306     /**
307      * Prepare stream by preallocating up to maxCount buffers for it asynchronously.
308      * Calls notifyPrepared() once allocation is complete.
309      */
310     virtual status_t prepare(int maxCount, int streamId) = 0;
311 
312     /**
313      * Get the HAL device version.
314      */
315     virtual uint32_t getDeviceVersion() = 0;
316 
317     /**
318      * Set the deferred consumer surface and finish the rest of the stream configuration.
319      */
320     virtual status_t setConsumerSurface(int streamId, sp<Surface> consumer) = 0;
321 
322 };
323 
324 }; // namespace android
325 
326 #endif
327