blob: 3034a1d04a31b82a7fc45ae2072bbd5952ae54e1 [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 */
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);
62 virtual status_t waitForNextFrame(nsecs_t timeout);
63 virtual status_t getNextFrame(CameraMetadata *frame);
64 virtual status_t triggerAutofocus(uint32_t id);
65 virtual status_t triggerCancelAutofocus(uint32_t id);
66 virtual status_t triggerPrecaptureMetering(uint32_t id);
67 virtual status_t pushReprocessBuffer(int reprocessStreamId,
Eino-Ville Talvala69230df2012-08-29 17:37:16 -070068 buffer_handle_t *buffer, wp<BufferReleasedListener> listener);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070069 private:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070070 const int mId;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080071 camera2_device_t *mHal2Device;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070072
Eino-Ville Talvalacab96a42012-08-24 11:29:22 -070073 CameraMetadata mDeviceInfo;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070074 vendor_tag_query_ops_t *mVendorTagOps;
75
76 /**
77 * Queue class for both sending requests to a camera2 device, and for
78 * receiving frames from a camera2 device.
79 */
80 class MetadataQueue: public camera2_request_queue_src_ops_t,
81 public camera2_frame_queue_dst_ops_t {
82 public:
83 MetadataQueue();
84 ~MetadataQueue();
85
86 // Interface to camera2 HAL device, either for requests (device is
87 // consumer) or for frames (device is producer)
88 const camera2_request_queue_src_ops_t* getToConsumerInterface();
89 void setFromConsumerInterface(camera2_device_t *d);
90
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070091 // Connect queue consumer endpoint to a camera2 device
92 status_t setConsumerDevice(camera2_device_t *d);
93 // Connect queue producer endpoint to a camera2 device
94 status_t setProducerDevice(camera2_device_t *d);
95
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070096 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
97
98 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
99 // On dequeue, user takes ownership of buffer pointer.
100 status_t enqueue(camera_metadata_t *buf);
Eino-Ville Talvalafbd60662012-10-16 10:28:07 -0700101 status_t dequeue(camera_metadata_t **buf, bool incrementCount = false);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700102 int getBufferCount();
103 status_t waitForBuffer(nsecs_t timeout);
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700104 // Wait until a buffer with the given ID is dequeued. Will return
105 // immediately if the latest buffer dequeued has that ID.
106 status_t waitForDequeue(int32_t id, nsecs_t timeout);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700107
108 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
109 // queue copies the contents of the stream slot into the queue, and then
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700110 // dequeues the first new entry. The metadata buffers passed in are
111 // copied.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700112 status_t setStreamSlot(camera_metadata_t *buf);
113 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
114
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700115 status_t dump(int fd, const Vector<String16>& args);
116
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700117 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700118 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700119 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
120 List<camera_metadata_t*>::iterator end);
121
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800122 camera2_device_t *mHal2Device;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700123
124 Mutex mMutex;
125 Condition notEmpty;
126
127 int mFrameCount;
Eino-Ville Talvala4c9eb712012-10-02 13:30:28 -0700128 int32_t mLatestRequestId;
129 Condition mNewRequestId;
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700130
131 int mCount;
132 List<camera_metadata_t*> mEntries;
133 int mStreamSlotCount;
134 List<camera_metadata_t*> mStreamSlot;
135
136 bool mSignalConsumer;
137
138 static MetadataQueue* getInstance(
139 const camera2_frame_queue_dst_ops_t *q);
140 static MetadataQueue* getInstance(
141 const camera2_request_queue_src_ops_t *q);
142
143 static int consumer_buffer_count(
144 const camera2_request_queue_src_ops_t *q);
145
146 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
147 camera_metadata_t **buffer);
148
149 static int consumer_free(const camera2_request_queue_src_ops_t *q,
150 camera_metadata_t *old_buffer);
151
152 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
153 size_t entries, size_t bytes,
154 camera_metadata_t **buffer);
155
156 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
157 camera_metadata_t *old_buffer);
158
159 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
160 camera_metadata_t *filled_buffer);
161
162 }; // class MetadataQueue
163
164 MetadataQueue mRequestQueue;
165 MetadataQueue mFrameQueue;
166
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700167 /**
168 * Adapter from an ANativeWindow interface to camera2 device stream ops.
169 * Also takes care of allocating/deallocating stream in device interface
170 */
171 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
172 public:
173 StreamAdapter(camera2_device_t *d);
174
175 ~StreamAdapter();
176
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700177 /**
178 * Create a HAL device stream of the requested size and format.
179 *
180 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
181 * selects an appropriate format; it can be queried with getFormat.
182 *
183 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
184 * be equal to the size in bytes of the buffers to allocate for the
185 * stream. For other formats, the size parameter is ignored.
186 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700187 status_t connectToDevice(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700188 uint32_t width, uint32_t height, int format, size_t size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700189
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700190 status_t release();
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700191
Eino-Ville Talvalac94cd192012-06-15 12:47:42 -0700192 status_t setTransform(int transform);
193
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700194 // Get stream parameters.
195 // Only valid after a successful connectToDevice call.
196 int getId() const { return mId; }
197 uint32_t getWidth() const { return mWidth; }
198 uint32_t getHeight() const { return mHeight; }
199 uint32_t getFormat() const { return mFormat; }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700200
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700201 // Dump stream information
202 status_t dump(int fd, const Vector<String16>& args);
203
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700204 private:
205 enum {
206 ERROR = -1,
Eino-Ville Talvala9cca4c62012-06-15 15:41:44 -0700207 RELEASED = 0,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700208 ALLOCATED,
209 CONNECTED,
210 ACTIVE
211 } mState;
212
213 sp<ANativeWindow> mConsumerInterface;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800214 camera2_device_t *mHal2Device;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700215
216 uint32_t mId;
217 uint32_t mWidth;
218 uint32_t mHeight;
219 uint32_t mFormat;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700220 size_t mSize;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700221 uint32_t mUsage;
222 uint32_t mMaxProducerBuffers;
223 uint32_t mMaxConsumerBuffers;
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700224 uint32_t mTotalBuffers;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700225 int mFormatRequested;
226
Eino-Ville Talvala3297daa2012-06-14 10:49:45 -0700227 /** Debugging information */
228 uint32_t mActiveBuffers;
229 uint32_t mFrameCount;
230 int64_t mLastTimestamp;
231
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700232 const camera2_stream_ops *getStreamOps();
233
234 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
235
236 static int dequeue_buffer(const camera2_stream_ops_t *w,
237 buffer_handle_t** buffer);
238
239 static int enqueue_buffer(const camera2_stream_ops_t* w,
240 int64_t timestamp,
241 buffer_handle_t* buffer);
242
243 static int cancel_buffer(const camera2_stream_ops_t* w,
244 buffer_handle_t* buffer);
245
246 static int set_crop(const camera2_stream_ops_t* w,
247 int left, int top, int right, int bottom);
248 }; // class StreamAdapter
249
250 typedef List<sp<StreamAdapter> > StreamList;
251 StreamList mStreams;
252
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700253 /**
254 * Adapter from an ANativeWindow interface to camera2 device stream ops.
255 * Also takes care of allocating/deallocating stream in device interface
256 */
257 class ReprocessStreamAdapter: public camera2_stream_in_ops, public virtual RefBase {
258 public:
259 ReprocessStreamAdapter(camera2_device_t *d);
260
261 ~ReprocessStreamAdapter();
262
263 /**
264 * Create a HAL device reprocess stream based on an existing output stream.
265 */
266 status_t connectToDevice(const sp<StreamAdapter> &outputStream);
267
268 status_t release();
269
270 /**
271 * Push buffer into stream for reprocessing. Takes ownership until it notifies
272 * that the buffer has been released
273 */
274 status_t pushIntoStream(buffer_handle_t *handle,
275 const wp<BufferReleasedListener> &releaseListener);
276
277 /**
278 * Get stream parameters.
279 * Only valid after a successful connectToDevice call.
280 */
281 int getId() const { return mId; }
282 uint32_t getWidth() const { return mWidth; }
283 uint32_t getHeight() const { return mHeight; }
284 uint32_t getFormat() const { return mFormat; }
285
286 // Dump stream information
287 status_t dump(int fd, const Vector<String16>& args);
288
289 private:
290 enum {
291 ERROR = -1,
292 RELEASED = 0,
293 ACTIVE
294 } mState;
295
296 sp<ANativeWindow> mConsumerInterface;
297 wp<StreamAdapter> mBaseStream;
298
299 struct QueueEntry {
300 buffer_handle_t *handle;
301 wp<BufferReleasedListener> releaseListener;
302 };
303
304 List<QueueEntry> mQueue;
305
306 List<QueueEntry> mInFlightQueue;
307
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800308 camera2_device_t *mHal2Device;
Eino-Ville Talvala69230df2012-08-29 17:37:16 -0700309
310 uint32_t mId;
311 uint32_t mWidth;
312 uint32_t mHeight;
313 uint32_t mFormat;
314
315 /** Debugging information */
316 uint32_t mActiveBuffers;
317 uint32_t mFrameCount;
318 int64_t mLastTimestamp;
319
320 const camera2_stream_in_ops *getStreamOps();
321
322 static int acquire_buffer(const camera2_stream_in_ops_t *w,
323 buffer_handle_t** buffer);
324
325 static int release_buffer(const camera2_stream_in_ops_t* w,
326 buffer_handle_t* buffer);
327
328 }; // class ReprocessStreamAdapter
329
330 typedef List<sp<ReprocessStreamAdapter> > ReprocessStreamList;
331 ReprocessStreamList mReprocessStreams;
332
Eino-Ville Talvala160d4af2012-08-03 09:40:16 -0700333 // Receives HAL notifications and routes them to the NotificationListener
334 static void notificationCallback(int32_t msg_type,
335 int32_t ext1,
336 int32_t ext2,
337 int32_t ext3,
338 void *user);
339
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700340}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700341
342}; // namespace android
343
344#endif