blob: 4def8ae599c8d6ce7512e42d6831ffc67bfb7401 [file] [log] [blame]
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -07001/*
2 * Copyright (C) 2012 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_CAMERA2DEVICE_H
18#define ANDROID_SERVERS_CAMERA_CAMERA2DEVICE_H
19
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070020#include <utils/Condition.h>
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070021#include <utils/Errors.h>
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -070022#include <utils/List.h>
23#include <utils/Mutex.h>
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -070024
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070025#include "common/CameraDeviceBase.h"
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070026
27namespace android {
28
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080029/**
30 * CameraDevice for HAL devices with version CAMERA_DEVICE_API_VERSION_2_0
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -070031 *
32 * TODO for camera2 API implementation:
33 * Does not produce notifyShutter / notifyIdle callbacks to NotificationListener
34 * Use waitUntilDrained for idle.
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080035 */
36class Camera2Device: public CameraDeviceBase {
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070037 public:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070038 Camera2Device(int id);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070039
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080040 virtual ~Camera2Device();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -070041
42 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080043 * CameraDevice interface
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -070044 */
Igor Murashkin71381052013-03-04 14:53:08 -080045 virtual int getId() const;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080046 virtual status_t initialize(camera_module_t *module);
47 virtual status_t disconnect();
48 virtual status_t dump(int fd, const Vector<String16>& args);
49 virtual const CameraMetadata& info() const;
Jianing Weicb0652e2014-03-12 18:29:36 -070050 virtual status_t capture(CameraMetadata &request, int64_t *lastFrameNumber = NULL);
51 virtual status_t captureList(const List<const CameraMetadata> &requests,
52 int64_t *lastFrameNumber = NULL);
53 virtual status_t setStreamingRequest(const CameraMetadata &request,
54 int64_t *lastFrameNumber = NULL);
55 virtual status_t setStreamingRequestList(const List<const CameraMetadata> &requests,
56 int64_t *lastFrameNumber = NULL);
57 virtual status_t clearStreamingRequest(int64_t *lastFrameNumber = NULL);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080058 virtual status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout);
59 virtual status_t createStream(sp<ANativeWindow> consumer,
Zhijun He28c9b6f2014-08-08 12:00:47 -070060 uint32_t width, uint32_t height, int format, int *id);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080061 virtual status_t createReprocessStreamFromStream(int outputId, int *id);
62 virtual status_t getStreamInfo(int id,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070063 uint32_t *width, uint32_t *height, uint32_t *format);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080064 virtual status_t setStreamTransform(int id, int transform);
65 virtual status_t deleteStream(int id);
66 virtual status_t deleteReprocessStream(int id);
Igor Murashkine2d167e2014-08-19 16:19:59 -070067 // No-op on HAL2 devices
68 virtual status_t configureStreams();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080069 virtual status_t createDefaultRequest(int templateId, CameraMetadata *request);
70 virtual status_t waitUntilDrained();
71 virtual status_t setNotifyCallback(NotificationListener *listener);
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -070072 virtual bool willNotify3A();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080073 virtual status_t waitForNextFrame(nsecs_t timeout);
Jianing Weicb0652e2014-03-12 18:29:36 -070074 virtual status_t getNextResult(CaptureResult *frame);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080075 virtual status_t triggerAutofocus(uint32_t id);
76 virtual status_t triggerCancelAutofocus(uint32_t id);
77 virtual status_t triggerPrecaptureMetering(uint32_t id);
78 virtual status_t pushReprocessBuffer(int reprocessStreamId,
Eino-Ville Talvala69230df2012-08-29 17:37:16 -070079 buffer_handle_t *buffer, wp<BufferReleasedListener> listener);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -070080 // Flush implemented as just a wait
Jianing Weicb0652e2014-03-12 18:29:36 -070081 virtual status_t flush(int64_t *lastFrameNumber = NULL);
Zhijun He204e3292014-07-14 17:09:23 -070082 virtual uint32_t getDeviceVersion();
Zhijun He28c9b6f2014-08-08 12:00:47 -070083 virtual ssize_t getJpegBufferSize(uint32_t width, uint32_t height) const;
Zhijun He204e3292014-07-14 17:09:23 -070084
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070085 private:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070086 const int mId;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080087 camera2_device_t *mHal2Device;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070088
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070089 CameraMetadata mDeviceInfo;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070090
Zhijun He204e3292014-07-14 17:09:23 -070091 uint32_t mDeviceVersion;
92
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070093 /**
94 * Queue class for both sending requests to a camera2 device, and for
95 * receiving frames from a camera2 device.
96 */
97 class MetadataQueue: public camera2_request_queue_src_ops_t,
98 public camera2_frame_queue_dst_ops_t {
99 public:
100 MetadataQueue();
101 ~MetadataQueue();
102
103 // Interface to camera2 HAL device, either for requests (device is
104 // consumer) or for frames (device is producer)
105 const camera2_request_queue_src_ops_t* getToConsumerInterface();
106 void setFromConsumerInterface(camera2_device_t *d);
107
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700108 // Connect queue consumer endpoint to a camera2 device
109 status_t setConsumerDevice(camera2_device_t *d);
110 // Connect queue producer endpoint to a camera2 device
111 status_t setProducerDevice(camera2_device_t *d);
112
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700113 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
114
115 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
116 // On dequeue, user takes ownership of buffer pointer.
117 status_t enqueue(camera_metadata_t *buf);
Eino-Ville Talvalafbd60662012-10-16 10:28:07 -0700118 status_t dequeue(camera_metadata_t **buf, bool incrementCount = false);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700119 int getBufferCount();
120 status_t waitForBuffer(nsecs_t timeout);
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700121 // Wait until a buffer with the given ID is dequeued. Will return
122 // immediately if the latest buffer dequeued has that ID.
123 status_t waitForDequeue(int32_t id, nsecs_t timeout);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700124
125 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
126 // queue copies the contents of the stream slot into the queue, and then
Chien-Yu Chen80de5dc2014-11-07 17:45:00 -0800127 // dequeues the first new entry. The methods take the ownership of the
128 // metadata buffers passed in.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700129 status_t setStreamSlot(camera_metadata_t *buf);
130 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
131
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700132 // Clear the request queue and the streaming slot
133 status_t clear();
134
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700135 status_t dump(int fd, const Vector<String16>& args);
136
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700137 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700138 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700139 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
140 List<camera_metadata_t*>::iterator end);
141
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800142 camera2_device_t *mHal2Device;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700143
144 Mutex mMutex;
145 Condition notEmpty;
146
147 int mFrameCount;
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700148 int32_t mLatestRequestId;
149 Condition mNewRequestId;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700150
151 int mCount;
152 List<camera_metadata_t*> mEntries;
153 int mStreamSlotCount;
154 List<camera_metadata_t*> mStreamSlot;
155
156 bool mSignalConsumer;
157
158 static MetadataQueue* getInstance(
159 const camera2_frame_queue_dst_ops_t *q);
160 static MetadataQueue* getInstance(
161 const camera2_request_queue_src_ops_t *q);
162
163 static int consumer_buffer_count(
164 const camera2_request_queue_src_ops_t *q);
165
166 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
167 camera_metadata_t **buffer);
168
169 static int consumer_free(const camera2_request_queue_src_ops_t *q,
170 camera_metadata_t *old_buffer);
171
172 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
173 size_t entries, size_t bytes,
174 camera_metadata_t **buffer);
175
176 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
177 camera_metadata_t *old_buffer);
178
179 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
180 camera_metadata_t *filled_buffer);
181
182 }; // class MetadataQueue
183
184 MetadataQueue mRequestQueue;
185 MetadataQueue mFrameQueue;
186
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700187 /**
188 * Adapter from an ANativeWindow interface to camera2 device stream ops.
189 * Also takes care of allocating/deallocating stream in device interface
190 */
191 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
192 public:
193 StreamAdapter(camera2_device_t *d);
194
195 ~StreamAdapter();
196
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700197 /**
198 * Create a HAL device stream of the requested size and format.
199 *
200 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
201 * selects an appropriate format; it can be queried with getFormat.
202 *
203 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
204 * be equal to the size in bytes of the buffers to allocate for the
205 * stream. For other formats, the size parameter is ignored.
206 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700207 status_t connectToDevice(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700208 uint32_t width, uint32_t height, int format, size_t size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700209
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700210 status_t release();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700211
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700212 status_t setTransform(int transform);
213
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700214 // Get stream parameters.
215 // Only valid after a successful connectToDevice call.
216 int getId() const { return mId; }
217 uint32_t getWidth() const { return mWidth; }
218 uint32_t getHeight() const { return mHeight; }
219 uint32_t getFormat() const { return mFormat; }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700220
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700221 // Dump stream information
222 status_t dump(int fd, const Vector<String16>& args);
223
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700224 private:
225 enum {
226 ERROR = -1,
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700227 RELEASED = 0,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700228 ALLOCATED,
229 CONNECTED,
230 ACTIVE
231 } mState;
232
233 sp<ANativeWindow> mConsumerInterface;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800234 camera2_device_t *mHal2Device;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700235
236 uint32_t mId;
237 uint32_t mWidth;
238 uint32_t mHeight;
239 uint32_t mFormat;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700240 size_t mSize;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700241 uint32_t mUsage;
242 uint32_t mMaxProducerBuffers;
243 uint32_t mMaxConsumerBuffers;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700244 uint32_t mTotalBuffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700245 int mFormatRequested;
246
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700247 /** Debugging information */
248 uint32_t mActiveBuffers;
249 uint32_t mFrameCount;
250 int64_t mLastTimestamp;
251
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700252 const camera2_stream_ops *getStreamOps();
253
254 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
255
256 static int dequeue_buffer(const camera2_stream_ops_t *w,
257 buffer_handle_t** buffer);
258
259 static int enqueue_buffer(const camera2_stream_ops_t* w,
260 int64_t timestamp,
261 buffer_handle_t* buffer);
262
263 static int cancel_buffer(const camera2_stream_ops_t* w,
264 buffer_handle_t* buffer);
265
266 static int set_crop(const camera2_stream_ops_t* w,
267 int left, int top, int right, int bottom);
268 }; // class StreamAdapter
269
270 typedef List<sp<StreamAdapter> > StreamList;
271 StreamList mStreams;
272
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700273 /**
274 * Adapter from an ANativeWindow interface to camera2 device stream ops.
275 * Also takes care of allocating/deallocating stream in device interface
276 */
277 class ReprocessStreamAdapter: public camera2_stream_in_ops, public virtual RefBase {
278 public:
279 ReprocessStreamAdapter(camera2_device_t *d);
280
281 ~ReprocessStreamAdapter();
282
283 /**
284 * Create a HAL device reprocess stream based on an existing output stream.
285 */
286 status_t connectToDevice(const sp<StreamAdapter> &outputStream);
287
288 status_t release();
289
290 /**
291 * Push buffer into stream for reprocessing. Takes ownership until it notifies
292 * that the buffer has been released
293 */
294 status_t pushIntoStream(buffer_handle_t *handle,
295 const wp<BufferReleasedListener> &releaseListener);
296
297 /**
298 * Get stream parameters.
299 * Only valid after a successful connectToDevice call.
300 */
301 int getId() const { return mId; }
302 uint32_t getWidth() const { return mWidth; }
303 uint32_t getHeight() const { return mHeight; }
304 uint32_t getFormat() const { return mFormat; }
305
306 // Dump stream information
307 status_t dump(int fd, const Vector<String16>& args);
308
309 private:
310 enum {
311 ERROR = -1,
312 RELEASED = 0,
313 ACTIVE
314 } mState;
315
316 sp<ANativeWindow> mConsumerInterface;
317 wp<StreamAdapter> mBaseStream;
318
319 struct QueueEntry {
320 buffer_handle_t *handle;
321 wp<BufferReleasedListener> releaseListener;
322 };
323
324 List<QueueEntry> mQueue;
325
326 List<QueueEntry> mInFlightQueue;
327
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800328 camera2_device_t *mHal2Device;
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700329
330 uint32_t mId;
331 uint32_t mWidth;
332 uint32_t mHeight;
333 uint32_t mFormat;
334
335 /** Debugging information */
336 uint32_t mActiveBuffers;
337 uint32_t mFrameCount;
338 int64_t mLastTimestamp;
339
340 const camera2_stream_in_ops *getStreamOps();
341
342 static int acquire_buffer(const camera2_stream_in_ops_t *w,
343 buffer_handle_t** buffer);
344
345 static int release_buffer(const camera2_stream_in_ops_t* w,
346 buffer_handle_t* buffer);
347
348 }; // class ReprocessStreamAdapter
349
350 typedef List<sp<ReprocessStreamAdapter> > ReprocessStreamList;
351 ReprocessStreamList mReprocessStreams;
352
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700353 // Receives HAL notifications and routes them to the NotificationListener
354 static void notificationCallback(int32_t msg_type,
355 int32_t ext1,
356 int32_t ext2,
357 int32_t ext3,
358 void *user);
359
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700360}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700361
362}; // namespace android
363
364#endif