blob: 1adb7a9b2001a9581916d8aa4bbed9ef6b43ea0b [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 Talvala7fa43f32013-02-06 17:20:07 -080025#include "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 */
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080041 virtual status_t initialize(camera_module_t *module);
42 virtual status_t disconnect();
43 virtual status_t dump(int fd, const Vector<String16>& args);
44 virtual const CameraMetadata& info() const;
45 virtual status_t capture(CameraMetadata &request);
46 virtual status_t setStreamingRequest(const CameraMetadata &request);
47 virtual status_t clearStreamingRequest();
48 virtual status_t waitUntilRequestReceived(int32_t requestId, nsecs_t timeout);
49 virtual status_t createStream(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070050 uint32_t width, uint32_t height, int format, size_t size,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070051 int *id);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080052 virtual status_t createReprocessStreamFromStream(int outputId, int *id);
53 virtual status_t getStreamInfo(int id,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070054 uint32_t *width, uint32_t *height, uint32_t *format);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080055 virtual status_t setStreamTransform(int id, int transform);
56 virtual status_t deleteStream(int id);
57 virtual status_t deleteReprocessStream(int id);
58 virtual status_t createDefaultRequest(int templateId, CameraMetadata *request);
59 virtual status_t waitUntilDrained();
60 virtual status_t setNotifyCallback(NotificationListener *listener);
61 virtual status_t waitForNextFrame(nsecs_t timeout);
62 virtual status_t getNextFrame(CameraMetadata *frame);
63 virtual status_t triggerAutofocus(uint32_t id);
64 virtual status_t triggerCancelAutofocus(uint32_t id);
65 virtual status_t triggerPrecaptureMetering(uint32_t id);
66 virtual status_t pushReprocessBuffer(int reprocessStreamId,
Eino-Ville Talvala69230df2012-08-29 17:37:16 -070067 buffer_handle_t *buffer, wp<BufferReleasedListener> listener);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070068 private:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070069 const int mId;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080070 camera2_device_t *mHal2Device;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070071
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070072 CameraMetadata mDeviceInfo;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070073 vendor_tag_query_ops_t *mVendorTagOps;
74
75 /**
76 * Queue class for both sending requests to a camera2 device, and for
77 * receiving frames from a camera2 device.
78 */
79 class MetadataQueue: public camera2_request_queue_src_ops_t,
80 public camera2_frame_queue_dst_ops_t {
81 public:
82 MetadataQueue();
83 ~MetadataQueue();
84
85 // Interface to camera2 HAL device, either for requests (device is
86 // consumer) or for frames (device is producer)
87 const camera2_request_queue_src_ops_t* getToConsumerInterface();
88 void setFromConsumerInterface(camera2_device_t *d);
89
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070090 // Connect queue consumer endpoint to a camera2 device
91 status_t setConsumerDevice(camera2_device_t *d);
92 // Connect queue producer endpoint to a camera2 device
93 status_t setProducerDevice(camera2_device_t *d);
94
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070095 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
96
97 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
98 // On dequeue, user takes ownership of buffer pointer.
99 status_t enqueue(camera_metadata_t *buf);
Eino-Ville Talvalafbd60662012-10-16 10:28:07 -0700100 status_t dequeue(camera_metadata_t **buf, bool incrementCount = false);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700101 int getBufferCount();
102 status_t waitForBuffer(nsecs_t timeout);
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700103 // Wait until a buffer with the given ID is dequeued. Will return
104 // immediately if the latest buffer dequeued has that ID.
105 status_t waitForDequeue(int32_t id, nsecs_t timeout);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700106
107 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
108 // queue copies the contents of the stream slot into the queue, and then
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700109 // dequeues the first new entry. The metadata buffers passed in are
110 // copied.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700111 status_t setStreamSlot(camera_metadata_t *buf);
112 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
113
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700114 status_t dump(int fd, const Vector<String16>& args);
115
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700116 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700117 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700118 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
119 List<camera_metadata_t*>::iterator end);
120
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800121 camera2_device_t *mHal2Device;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700122
123 Mutex mMutex;
124 Condition notEmpty;
125
126 int mFrameCount;
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700127 int32_t mLatestRequestId;
128 Condition mNewRequestId;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700129
130 int mCount;
131 List<camera_metadata_t*> mEntries;
132 int mStreamSlotCount;
133 List<camera_metadata_t*> mStreamSlot;
134
135 bool mSignalConsumer;
136
137 static MetadataQueue* getInstance(
138 const camera2_frame_queue_dst_ops_t *q);
139 static MetadataQueue* getInstance(
140 const camera2_request_queue_src_ops_t *q);
141
142 static int consumer_buffer_count(
143 const camera2_request_queue_src_ops_t *q);
144
145 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
146 camera_metadata_t **buffer);
147
148 static int consumer_free(const camera2_request_queue_src_ops_t *q,
149 camera_metadata_t *old_buffer);
150
151 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
152 size_t entries, size_t bytes,
153 camera_metadata_t **buffer);
154
155 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
156 camera_metadata_t *old_buffer);
157
158 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
159 camera_metadata_t *filled_buffer);
160
161 }; // class MetadataQueue
162
163 MetadataQueue mRequestQueue;
164 MetadataQueue mFrameQueue;
165
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700166 /**
167 * Adapter from an ANativeWindow interface to camera2 device stream ops.
168 * Also takes care of allocating/deallocating stream in device interface
169 */
170 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
171 public:
172 StreamAdapter(camera2_device_t *d);
173
174 ~StreamAdapter();
175
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700176 /**
177 * Create a HAL device stream of the requested size and format.
178 *
179 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
180 * selects an appropriate format; it can be queried with getFormat.
181 *
182 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
183 * be equal to the size in bytes of the buffers to allocate for the
184 * stream. For other formats, the size parameter is ignored.
185 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700186 status_t connectToDevice(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700187 uint32_t width, uint32_t height, int format, size_t size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700188
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700189 status_t release();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700190
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700191 status_t setTransform(int transform);
192
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700193 // Get stream parameters.
194 // Only valid after a successful connectToDevice call.
195 int getId() const { return mId; }
196 uint32_t getWidth() const { return mWidth; }
197 uint32_t getHeight() const { return mHeight; }
198 uint32_t getFormat() const { return mFormat; }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700199
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700200 // Dump stream information
201 status_t dump(int fd, const Vector<String16>& args);
202
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700203 private:
204 enum {
205 ERROR = -1,
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700206 RELEASED = 0,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700207 ALLOCATED,
208 CONNECTED,
209 ACTIVE
210 } mState;
211
212 sp<ANativeWindow> mConsumerInterface;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800213 camera2_device_t *mHal2Device;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700214
215 uint32_t mId;
216 uint32_t mWidth;
217 uint32_t mHeight;
218 uint32_t mFormat;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700219 size_t mSize;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700220 uint32_t mUsage;
221 uint32_t mMaxProducerBuffers;
222 uint32_t mMaxConsumerBuffers;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700223 uint32_t mTotalBuffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700224 int mFormatRequested;
225
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700226 /** Debugging information */
227 uint32_t mActiveBuffers;
228 uint32_t mFrameCount;
229 int64_t mLastTimestamp;
230
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700231 const camera2_stream_ops *getStreamOps();
232
233 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
234
235 static int dequeue_buffer(const camera2_stream_ops_t *w,
236 buffer_handle_t** buffer);
237
238 static int enqueue_buffer(const camera2_stream_ops_t* w,
239 int64_t timestamp,
240 buffer_handle_t* buffer);
241
242 static int cancel_buffer(const camera2_stream_ops_t* w,
243 buffer_handle_t* buffer);
244
245 static int set_crop(const camera2_stream_ops_t* w,
246 int left, int top, int right, int bottom);
247 }; // class StreamAdapter
248
249 typedef List<sp<StreamAdapter> > StreamList;
250 StreamList mStreams;
251
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700252 /**
253 * Adapter from an ANativeWindow interface to camera2 device stream ops.
254 * Also takes care of allocating/deallocating stream in device interface
255 */
256 class ReprocessStreamAdapter: public camera2_stream_in_ops, public virtual RefBase {
257 public:
258 ReprocessStreamAdapter(camera2_device_t *d);
259
260 ~ReprocessStreamAdapter();
261
262 /**
263 * Create a HAL device reprocess stream based on an existing output stream.
264 */
265 status_t connectToDevice(const sp<StreamAdapter> &outputStream);
266
267 status_t release();
268
269 /**
270 * Push buffer into stream for reprocessing. Takes ownership until it notifies
271 * that the buffer has been released
272 */
273 status_t pushIntoStream(buffer_handle_t *handle,
274 const wp<BufferReleasedListener> &releaseListener);
275
276 /**
277 * Get stream parameters.
278 * Only valid after a successful connectToDevice call.
279 */
280 int getId() const { return mId; }
281 uint32_t getWidth() const { return mWidth; }
282 uint32_t getHeight() const { return mHeight; }
283 uint32_t getFormat() const { return mFormat; }
284
285 // Dump stream information
286 status_t dump(int fd, const Vector<String16>& args);
287
288 private:
289 enum {
290 ERROR = -1,
291 RELEASED = 0,
292 ACTIVE
293 } mState;
294
295 sp<ANativeWindow> mConsumerInterface;
296 wp<StreamAdapter> mBaseStream;
297
298 struct QueueEntry {
299 buffer_handle_t *handle;
300 wp<BufferReleasedListener> releaseListener;
301 };
302
303 List<QueueEntry> mQueue;
304
305 List<QueueEntry> mInFlightQueue;
306
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800307 camera2_device_t *mHal2Device;
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700308
309 uint32_t mId;
310 uint32_t mWidth;
311 uint32_t mHeight;
312 uint32_t mFormat;
313
314 /** Debugging information */
315 uint32_t mActiveBuffers;
316 uint32_t mFrameCount;
317 int64_t mLastTimestamp;
318
319 const camera2_stream_in_ops *getStreamOps();
320
321 static int acquire_buffer(const camera2_stream_in_ops_t *w,
322 buffer_handle_t** buffer);
323
324 static int release_buffer(const camera2_stream_in_ops_t* w,
325 buffer_handle_t* buffer);
326
327 }; // class ReprocessStreamAdapter
328
329 typedef List<sp<ReprocessStreamAdapter> > ReprocessStreamList;
330 ReprocessStreamList mReprocessStreams;
331
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700332 // Receives HAL notifications and routes them to the NotificationListener
333 static void notificationCallback(int32_t msg_type,
334 int32_t ext1,
335 int32_t ext2,
336 int32_t ext3,
337 void *user);
338
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700339}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700340
341}; // namespace android
342
343#endif