blob: 8945ec22b480fe9729f38597172841be19e013ed [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
31 */
32class Camera2Device: public CameraDeviceBase {
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070033 public:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070034 Camera2Device(int id);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070035
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080036 virtual ~Camera2Device();
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -070037
38 /**
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080039 * CameraDevice interface
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -070040 */
Igor Murashkin71381052013-03-04 14:53:08 -080041 virtual int getId() const;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080042 virtual status_t initialize(camera_module_t *module);
43 virtual status_t disconnect();
44 virtual status_t dump(int fd, const Vector<String16>& args);
45 virtual const CameraMetadata& info() const;
46 virtual status_t capture(CameraMetadata &request);
47 virtual status_t setStreamingRequest(const CameraMetadata &request);
48 virtual status_t clearStreamingRequest();
49 virtual status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout);
50 virtual status_t createStream(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070051 uint32_t width, uint32_t height, int format, size_t size,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070052 int *id);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080053 virtual status_t createReprocessStreamFromStream(int outputId, int *id);
54 virtual status_t getStreamInfo(int id,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070055 uint32_t *width, uint32_t *height, uint32_t *format);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080056 virtual status_t setStreamTransform(int id, int transform);
57 virtual status_t deleteStream(int id);
58 virtual status_t deleteReprocessStream(int id);
59 virtual status_t createDefaultRequest(int templateId, CameraMetadata *request);
60 virtual status_t waitUntilDrained();
61 virtual status_t setNotifyCallback(NotificationListener *listener);
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -070062 virtual bool willNotify3A();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080063 virtual status_t waitForNextFrame(nsecs_t timeout);
64 virtual status_t getNextFrame(CameraMetadata *frame);
65 virtual status_t triggerAutofocus(uint32_t id);
66 virtual status_t triggerCancelAutofocus(uint32_t id);
67 virtual status_t triggerPrecaptureMetering(uint32_t id);
68 virtual status_t pushReprocessBuffer(int reprocessStreamId,
Eino-Ville Talvala69230df2012-08-29 17:37:16 -070069 buffer_handle_t *buffer, wp<BufferReleasedListener> listener);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070070 private:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070071 const int mId;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080072 camera2_device_t *mHal2Device;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070073
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070074 CameraMetadata mDeviceInfo;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070075 vendor_tag_query_ops_t *mVendorTagOps;
76
77 /**
78 * Queue class for both sending requests to a camera2 device, and for
79 * receiving frames from a camera2 device.
80 */
81 class MetadataQueue: public camera2_request_queue_src_ops_t,
82 public camera2_frame_queue_dst_ops_t {
83 public:
84 MetadataQueue();
85 ~MetadataQueue();
86
87 // Interface to camera2 HAL device, either for requests (device is
88 // consumer) or for frames (device is producer)
89 const camera2_request_queue_src_ops_t* getToConsumerInterface();
90 void setFromConsumerInterface(camera2_device_t *d);
91
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070092 // Connect queue consumer endpoint to a camera2 device
93 status_t setConsumerDevice(camera2_device_t *d);
94 // Connect queue producer endpoint to a camera2 device
95 status_t setProducerDevice(camera2_device_t *d);
96
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070097 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
98
99 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
100 // On dequeue, user takes ownership of buffer pointer.
101 status_t enqueue(camera_metadata_t *buf);
Eino-Ville Talvalafbd60662012-10-16 10:28:07 -0700102 status_t dequeue(camera_metadata_t **buf, bool incrementCount = false);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700103 int getBufferCount();
104 status_t waitForBuffer(nsecs_t timeout);
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700105 // Wait until a buffer with the given ID is dequeued. Will return
106 // immediately if the latest buffer dequeued has that ID.
107 status_t waitForDequeue(int32_t id, nsecs_t timeout);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700108
109 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
110 // queue copies the contents of the stream slot into the queue, and then
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700111 // dequeues the first new entry. The metadata buffers passed in are
112 // copied.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700113 status_t setStreamSlot(camera_metadata_t *buf);
114 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
115
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700116 status_t dump(int fd, const Vector<String16>& args);
117
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700118 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700119 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700120 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
121 List<camera_metadata_t*>::iterator end);
122
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800123 camera2_device_t *mHal2Device;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700124
125 Mutex mMutex;
126 Condition notEmpty;
127
128 int mFrameCount;
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700129 int32_t mLatestRequestId;
130 Condition mNewRequestId;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700131
132 int mCount;
133 List<camera_metadata_t*> mEntries;
134 int mStreamSlotCount;
135 List<camera_metadata_t*> mStreamSlot;
136
137 bool mSignalConsumer;
138
139 static MetadataQueue* getInstance(
140 const camera2_frame_queue_dst_ops_t *q);
141 static MetadataQueue* getInstance(
142 const camera2_request_queue_src_ops_t *q);
143
144 static int consumer_buffer_count(
145 const camera2_request_queue_src_ops_t *q);
146
147 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
148 camera_metadata_t **buffer);
149
150 static int consumer_free(const camera2_request_queue_src_ops_t *q,
151 camera_metadata_t *old_buffer);
152
153 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
154 size_t entries, size_t bytes,
155 camera_metadata_t **buffer);
156
157 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
158 camera_metadata_t *old_buffer);
159
160 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
161 camera_metadata_t *filled_buffer);
162
163 }; // class MetadataQueue
164
165 MetadataQueue mRequestQueue;
166 MetadataQueue mFrameQueue;
167
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700168 /**
169 * Adapter from an ANativeWindow interface to camera2 device stream ops.
170 * Also takes care of allocating/deallocating stream in device interface
171 */
172 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
173 public:
174 StreamAdapter(camera2_device_t *d);
175
176 ~StreamAdapter();
177
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700178 /**
179 * Create a HAL device stream of the requested size and format.
180 *
181 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
182 * selects an appropriate format; it can be queried with getFormat.
183 *
184 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
185 * be equal to the size in bytes of the buffers to allocate for the
186 * stream. For other formats, the size parameter is ignored.
187 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700188 status_t connectToDevice(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700189 uint32_t width, uint32_t height, int format, size_t size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700190
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700191 status_t release();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700192
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700193 status_t setTransform(int transform);
194
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700195 // Get stream parameters.
196 // Only valid after a successful connectToDevice call.
197 int getId() const { return mId; }
198 uint32_t getWidth() const { return mWidth; }
199 uint32_t getHeight() const { return mHeight; }
200 uint32_t getFormat() const { return mFormat; }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700201
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700202 // Dump stream information
203 status_t dump(int fd, const Vector<String16>& args);
204
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700205 private:
206 enum {
207 ERROR = -1,
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700208 RELEASED = 0,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700209 ALLOCATED,
210 CONNECTED,
211 ACTIVE
212 } mState;
213
214 sp<ANativeWindow> mConsumerInterface;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800215 camera2_device_t *mHal2Device;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700216
217 uint32_t mId;
218 uint32_t mWidth;
219 uint32_t mHeight;
220 uint32_t mFormat;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700221 size_t mSize;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700222 uint32_t mUsage;
223 uint32_t mMaxProducerBuffers;
224 uint32_t mMaxConsumerBuffers;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700225 uint32_t mTotalBuffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700226 int mFormatRequested;
227
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700228 /** Debugging information */
229 uint32_t mActiveBuffers;
230 uint32_t mFrameCount;
231 int64_t mLastTimestamp;
232
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700233 const camera2_stream_ops *getStreamOps();
234
235 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
236
237 static int dequeue_buffer(const camera2_stream_ops_t *w,
238 buffer_handle_t** buffer);
239
240 static int enqueue_buffer(const camera2_stream_ops_t* w,
241 int64_t timestamp,
242 buffer_handle_t* buffer);
243
244 static int cancel_buffer(const camera2_stream_ops_t* w,
245 buffer_handle_t* buffer);
246
247 static int set_crop(const camera2_stream_ops_t* w,
248 int left, int top, int right, int bottom);
249 }; // class StreamAdapter
250
251 typedef List<sp<StreamAdapter> > StreamList;
252 StreamList mStreams;
253
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700254 /**
255 * Adapter from an ANativeWindow interface to camera2 device stream ops.
256 * Also takes care of allocating/deallocating stream in device interface
257 */
258 class ReprocessStreamAdapter: public camera2_stream_in_ops, public virtual RefBase {
259 public:
260 ReprocessStreamAdapter(camera2_device_t *d);
261
262 ~ReprocessStreamAdapter();
263
264 /**
265 * Create a HAL device reprocess stream based on an existing output stream.
266 */
267 status_t connectToDevice(const sp<StreamAdapter> &outputStream);
268
269 status_t release();
270
271 /**
272 * Push buffer into stream for reprocessing. Takes ownership until it notifies
273 * that the buffer has been released
274 */
275 status_t pushIntoStream(buffer_handle_t *handle,
276 const wp<BufferReleasedListener> &releaseListener);
277
278 /**
279 * Get stream parameters.
280 * Only valid after a successful connectToDevice call.
281 */
282 int getId() const { return mId; }
283 uint32_t getWidth() const { return mWidth; }
284 uint32_t getHeight() const { return mHeight; }
285 uint32_t getFormat() const { return mFormat; }
286
287 // Dump stream information
288 status_t dump(int fd, const Vector<String16>& args);
289
290 private:
291 enum {
292 ERROR = -1,
293 RELEASED = 0,
294 ACTIVE
295 } mState;
296
297 sp<ANativeWindow> mConsumerInterface;
298 wp<StreamAdapter> mBaseStream;
299
300 struct QueueEntry {
301 buffer_handle_t *handle;
302 wp<BufferReleasedListener> releaseListener;
303 };
304
305 List<QueueEntry> mQueue;
306
307 List<QueueEntry> mInFlightQueue;
308
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800309 camera2_device_t *mHal2Device;
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700310
311 uint32_t mId;
312 uint32_t mWidth;
313 uint32_t mHeight;
314 uint32_t mFormat;
315
316 /** Debugging information */
317 uint32_t mActiveBuffers;
318 uint32_t mFrameCount;
319 int64_t mLastTimestamp;
320
321 const camera2_stream_in_ops *getStreamOps();
322
323 static int acquire_buffer(const camera2_stream_in_ops_t *w,
324 buffer_handle_t** buffer);
325
326 static int release_buffer(const camera2_stream_in_ops_t* w,
327 buffer_handle_t* buffer);
328
329 }; // class ReprocessStreamAdapter
330
331 typedef List<sp<ReprocessStreamAdapter> > ReprocessStreamList;
332 ReprocessStreamList mReprocessStreams;
333
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700334 // Receives HAL notifications and routes them to the NotificationListener
335 static void notificationCallback(int32_t msg_type,
336 int32_t ext1,
337 int32_t ext2,
338 int32_t ext3,
339 void *user);
340
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700341}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700342
343}; // namespace android
344
345#endif