blob: 61bfd1af2c3e21e2f2186586a97e93318f249c77 [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;
50 virtual status_t capture(CameraMetadata &request);
Jianing Wei90e59c92014-03-12 18:29:36 -070051 virtual status_t captureList(const List<const CameraMetadata> &requests);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080052 virtual status_t setStreamingRequest(const CameraMetadata &request);
Jianing Wei90e59c92014-03-12 18:29:36 -070053 virtual status_t setStreamingRequestList(const List<const CameraMetadata> &requests);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080054 virtual status_t clearStreamingRequest();
55 virtual status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout);
56 virtual status_t createStream(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070057 uint32_t width, uint32_t height, int format, size_t size,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070058 int *id);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080059 virtual status_t createReprocessStreamFromStream(int outputId, int *id);
60 virtual status_t getStreamInfo(int id,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070061 uint32_t *width, uint32_t *height, uint32_t *format);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080062 virtual status_t setStreamTransform(int id, int transform);
63 virtual status_t deleteStream(int id);
64 virtual status_t deleteReprocessStream(int id);
65 virtual status_t createDefaultRequest(int templateId, CameraMetadata *request);
66 virtual status_t waitUntilDrained();
67 virtual status_t setNotifyCallback(NotificationListener *listener);
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -070068 virtual bool willNotify3A();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080069 virtual status_t waitForNextFrame(nsecs_t timeout);
70 virtual status_t getNextFrame(CameraMetadata *frame);
71 virtual status_t triggerAutofocus(uint32_t id);
72 virtual status_t triggerCancelAutofocus(uint32_t id);
73 virtual status_t triggerPrecaptureMetering(uint32_t id);
74 virtual status_t pushReprocessBuffer(int reprocessStreamId,
Eino-Ville Talvala69230df2012-08-29 17:37:16 -070075 buffer_handle_t *buffer, wp<BufferReleasedListener> listener);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -070076 // Flush implemented as just a wait
77 virtual status_t flush();
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070078 private:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070079 const int mId;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080080 camera2_device_t *mHal2Device;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070081
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070082 CameraMetadata mDeviceInfo;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070083
84 /**
85 * Queue class for both sending requests to a camera2 device, and for
86 * receiving frames from a camera2 device.
87 */
88 class MetadataQueue: public camera2_request_queue_src_ops_t,
89 public camera2_frame_queue_dst_ops_t {
90 public:
91 MetadataQueue();
92 ~MetadataQueue();
93
94 // Interface to camera2 HAL device, either for requests (device is
95 // consumer) or for frames (device is producer)
96 const camera2_request_queue_src_ops_t* getToConsumerInterface();
97 void setFromConsumerInterface(camera2_device_t *d);
98
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070099 // Connect queue consumer endpoint to a camera2 device
100 status_t setConsumerDevice(camera2_device_t *d);
101 // Connect queue producer endpoint to a camera2 device
102 status_t setProducerDevice(camera2_device_t *d);
103
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700104 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
105
106 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
107 // On dequeue, user takes ownership of buffer pointer.
108 status_t enqueue(camera_metadata_t *buf);
Eino-Ville Talvalafbd60662012-10-16 10:28:07 -0700109 status_t dequeue(camera_metadata_t **buf, bool incrementCount = false);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700110 int getBufferCount();
111 status_t waitForBuffer(nsecs_t timeout);
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700112 // Wait until a buffer with the given ID is dequeued. Will return
113 // immediately if the latest buffer dequeued has that ID.
114 status_t waitForDequeue(int32_t id, nsecs_t timeout);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700115
116 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
117 // queue copies the contents of the stream slot into the queue, and then
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700118 // dequeues the first new entry. The metadata buffers passed in are
119 // copied.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700120 status_t setStreamSlot(camera_metadata_t *buf);
121 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
122
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -0700123 // Clear the request queue and the streaming slot
124 status_t clear();
125
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700126 status_t dump(int fd, const Vector<String16>& args);
127
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700128 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700129 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700130 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
131 List<camera_metadata_t*>::iterator end);
132
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800133 camera2_device_t *mHal2Device;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700134
135 Mutex mMutex;
136 Condition notEmpty;
137
138 int mFrameCount;
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700139 int32_t mLatestRequestId;
140 Condition mNewRequestId;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700141
142 int mCount;
143 List<camera_metadata_t*> mEntries;
144 int mStreamSlotCount;
145 List<camera_metadata_t*> mStreamSlot;
146
147 bool mSignalConsumer;
148
149 static MetadataQueue* getInstance(
150 const camera2_frame_queue_dst_ops_t *q);
151 static MetadataQueue* getInstance(
152 const camera2_request_queue_src_ops_t *q);
153
154 static int consumer_buffer_count(
155 const camera2_request_queue_src_ops_t *q);
156
157 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
158 camera_metadata_t **buffer);
159
160 static int consumer_free(const camera2_request_queue_src_ops_t *q,
161 camera_metadata_t *old_buffer);
162
163 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
164 size_t entries, size_t bytes,
165 camera_metadata_t **buffer);
166
167 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
168 camera_metadata_t *old_buffer);
169
170 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
171 camera_metadata_t *filled_buffer);
172
173 }; // class MetadataQueue
174
175 MetadataQueue mRequestQueue;
176 MetadataQueue mFrameQueue;
177
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700178 /**
179 * Adapter from an ANativeWindow interface to camera2 device stream ops.
180 * Also takes care of allocating/deallocating stream in device interface
181 */
182 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
183 public:
184 StreamAdapter(camera2_device_t *d);
185
186 ~StreamAdapter();
187
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700188 /**
189 * Create a HAL device stream of the requested size and format.
190 *
191 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
192 * selects an appropriate format; it can be queried with getFormat.
193 *
194 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
195 * be equal to the size in bytes of the buffers to allocate for the
196 * stream. For other formats, the size parameter is ignored.
197 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700198 status_t connectToDevice(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700199 uint32_t width, uint32_t height, int format, size_t size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700200
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700201 status_t release();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700202
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700203 status_t setTransform(int transform);
204
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700205 // Get stream parameters.
206 // Only valid after a successful connectToDevice call.
207 int getId() const { return mId; }
208 uint32_t getWidth() const { return mWidth; }
209 uint32_t getHeight() const { return mHeight; }
210 uint32_t getFormat() const { return mFormat; }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700211
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700212 // Dump stream information
213 status_t dump(int fd, const Vector<String16>& args);
214
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700215 private:
216 enum {
217 ERROR = -1,
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700218 RELEASED = 0,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700219 ALLOCATED,
220 CONNECTED,
221 ACTIVE
222 } mState;
223
224 sp<ANativeWindow> mConsumerInterface;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800225 camera2_device_t *mHal2Device;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700226
227 uint32_t mId;
228 uint32_t mWidth;
229 uint32_t mHeight;
230 uint32_t mFormat;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700231 size_t mSize;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700232 uint32_t mUsage;
233 uint32_t mMaxProducerBuffers;
234 uint32_t mMaxConsumerBuffers;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700235 uint32_t mTotalBuffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700236 int mFormatRequested;
237
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700238 /** Debugging information */
239 uint32_t mActiveBuffers;
240 uint32_t mFrameCount;
241 int64_t mLastTimestamp;
242
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700243 const camera2_stream_ops *getStreamOps();
244
245 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
246
247 static int dequeue_buffer(const camera2_stream_ops_t *w,
248 buffer_handle_t** buffer);
249
250 static int enqueue_buffer(const camera2_stream_ops_t* w,
251 int64_t timestamp,
252 buffer_handle_t* buffer);
253
254 static int cancel_buffer(const camera2_stream_ops_t* w,
255 buffer_handle_t* buffer);
256
257 static int set_crop(const camera2_stream_ops_t* w,
258 int left, int top, int right, int bottom);
259 }; // class StreamAdapter
260
261 typedef List<sp<StreamAdapter> > StreamList;
262 StreamList mStreams;
263
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700264 /**
265 * Adapter from an ANativeWindow interface to camera2 device stream ops.
266 * Also takes care of allocating/deallocating stream in device interface
267 */
268 class ReprocessStreamAdapter: public camera2_stream_in_ops, public virtual RefBase {
269 public:
270 ReprocessStreamAdapter(camera2_device_t *d);
271
272 ~ReprocessStreamAdapter();
273
274 /**
275 * Create a HAL device reprocess stream based on an existing output stream.
276 */
277 status_t connectToDevice(const sp<StreamAdapter> &outputStream);
278
279 status_t release();
280
281 /**
282 * Push buffer into stream for reprocessing. Takes ownership until it notifies
283 * that the buffer has been released
284 */
285 status_t pushIntoStream(buffer_handle_t *handle,
286 const wp<BufferReleasedListener> &releaseListener);
287
288 /**
289 * Get stream parameters.
290 * Only valid after a successful connectToDevice call.
291 */
292 int getId() const { return mId; }
293 uint32_t getWidth() const { return mWidth; }
294 uint32_t getHeight() const { return mHeight; }
295 uint32_t getFormat() const { return mFormat; }
296
297 // Dump stream information
298 status_t dump(int fd, const Vector<String16>& args);
299
300 private:
301 enum {
302 ERROR = -1,
303 RELEASED = 0,
304 ACTIVE
305 } mState;
306
307 sp<ANativeWindow> mConsumerInterface;
308 wp<StreamAdapter> mBaseStream;
309
310 struct QueueEntry {
311 buffer_handle_t *handle;
312 wp<BufferReleasedListener> releaseListener;
313 };
314
315 List<QueueEntry> mQueue;
316
317 List<QueueEntry> mInFlightQueue;
318
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800319 camera2_device_t *mHal2Device;
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700320
321 uint32_t mId;
322 uint32_t mWidth;
323 uint32_t mHeight;
324 uint32_t mFormat;
325
326 /** Debugging information */
327 uint32_t mActiveBuffers;
328 uint32_t mFrameCount;
329 int64_t mLastTimestamp;
330
331 const camera2_stream_in_ops *getStreamOps();
332
333 static int acquire_buffer(const camera2_stream_in_ops_t *w,
334 buffer_handle_t** buffer);
335
336 static int release_buffer(const camera2_stream_in_ops_t* w,
337 buffer_handle_t* buffer);
338
339 }; // class ReprocessStreamAdapter
340
341 typedef List<sp<ReprocessStreamAdapter> > ReprocessStreamList;
342 ReprocessStreamList mReprocessStreams;
343
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700344 // Receives HAL notifications and routes them to the NotificationListener
345 static void notificationCallback(int32_t msg_type,
346 int32_t ext1,
347 int32_t ext2,
348 int32_t ext3,
349 void *user);
350
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700351}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700352
353}; // namespace android
354
355#endif