blob: 7071561f972b2850cb5ae0ef549b3afeb8799871 [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
20#include <utils/RefBase.h>
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070021#include <utils/List.h>
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070022#include <utils/Vector.h>
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070023#include <utils/Mutex.h>
24#include <utils/Condition.h>
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070025#include <utils/Errors.h>
26#include "hardware/camera2.h"
27
28namespace android {
29
30class Camera2Device : public virtual RefBase {
31 public:
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070032 Camera2Device(int id);
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070033
34 ~Camera2Device();
35
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070036 status_t initialize(camera_module_t *module);
37
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070038 camera_metadata_t* info();
39
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070040 /**
41 * Submit request for capture. The Camera2Device takes ownership of the
42 * passed-in buffer.
43 */
44 status_t capture(camera_metadata_t *request);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070045
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070046 /**
47 * Submit request for streaming. The Camera2Device makes a copy of the
48 * passed-in buffer and the caller retains ownership.
49 */
50 status_t setStreamingRequest(camera_metadata_t *request);
51
52 /**
53 * Create an output stream of the requested size and format.
54 *
55 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device selects
56 * an appropriate format; it can be queried with getStreamInfo.
57 *
58 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must be
59 * equal to the size in bytes of the buffers to allocate for the stream. For
60 * other formats, the size parameter is ignored.
61 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070062 status_t createStream(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070063 uint32_t width, uint32_t height, int format, size_t size,
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070064 int *id);
65
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070066 /**
67 * Get information about a given stream.
68 */
69 status_t getStreamInfo(int id,
70 uint32_t *width, uint32_t *height, uint32_t *format);
71
72 /**
73 * Delete stream. Must not be called if there are requests in flight which
74 * reference that stream.
75 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070076 status_t deleteStream(int id);
77
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070078 /**
79 * Create a metadata buffer with fields that the HAL device believes are
80 * best for the given use case
81 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -070082 status_t createDefaultRequest(int templateId,
83 camera_metadata_t **request);
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070084
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -070085 /**
86 * Wait until all requests have been processed. Returns INVALID_OPERATION if
87 * the streaming slot is not empty, or TIMED_OUT if the requests haven't
88 * finished processing in 10 seconds.
89 */
90 status_t waitUntilDrained();
91
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070092 private:
93
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070094 const int mId;
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -070095 camera2_device_t *mDevice;
96
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -070097 camera_metadata_t *mDeviceInfo;
98 vendor_tag_query_ops_t *mVendorTagOps;
99
100 /**
101 * Queue class for both sending requests to a camera2 device, and for
102 * receiving frames from a camera2 device.
103 */
104 class MetadataQueue: public camera2_request_queue_src_ops_t,
105 public camera2_frame_queue_dst_ops_t {
106 public:
107 MetadataQueue();
108 ~MetadataQueue();
109
110 // Interface to camera2 HAL device, either for requests (device is
111 // consumer) or for frames (device is producer)
112 const camera2_request_queue_src_ops_t* getToConsumerInterface();
113 void setFromConsumerInterface(camera2_device_t *d);
114
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700115 // Connect queue consumer endpoint to a camera2 device
116 status_t setConsumerDevice(camera2_device_t *d);
117 // Connect queue producer endpoint to a camera2 device
118 status_t setProducerDevice(camera2_device_t *d);
119
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700120 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
121
122 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
123 // On dequeue, user takes ownership of buffer pointer.
124 status_t enqueue(camera_metadata_t *buf);
125 status_t dequeue(camera_metadata_t **buf, bool incrementCount = true);
126 int getBufferCount();
127 status_t waitForBuffer(nsecs_t timeout);
128
129 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
130 // queue copies the contents of the stream slot into the queue, and then
Eino-Ville Talvala6ed1ed12012-06-07 10:46:38 -0700131 // dequeues the first new entry. The metadata buffers passed in are
132 // copied.
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700133 status_t setStreamSlot(camera_metadata_t *buf);
134 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
135
136 private:
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700137 status_t signalConsumerLocked();
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700138 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
139 List<camera_metadata_t*>::iterator end);
140
141 camera2_device_t *mDevice;
142
143 Mutex mMutex;
144 Condition notEmpty;
145
146 int mFrameCount;
147
148 int mCount;
149 List<camera_metadata_t*> mEntries;
150 int mStreamSlotCount;
151 List<camera_metadata_t*> mStreamSlot;
152
153 bool mSignalConsumer;
154
155 static MetadataQueue* getInstance(
156 const camera2_frame_queue_dst_ops_t *q);
157 static MetadataQueue* getInstance(
158 const camera2_request_queue_src_ops_t *q);
159
160 static int consumer_buffer_count(
161 const camera2_request_queue_src_ops_t *q);
162
163 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
164 camera_metadata_t **buffer);
165
166 static int consumer_free(const camera2_request_queue_src_ops_t *q,
167 camera_metadata_t *old_buffer);
168
169 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
170 size_t entries, size_t bytes,
171 camera_metadata_t **buffer);
172
173 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
174 camera_metadata_t *old_buffer);
175
176 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
177 camera_metadata_t *filled_buffer);
178
179 }; // class MetadataQueue
180
181 MetadataQueue mRequestQueue;
182 MetadataQueue mFrameQueue;
183
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700184 /**
185 * Adapter from an ANativeWindow interface to camera2 device stream ops.
186 * Also takes care of allocating/deallocating stream in device interface
187 */
188 class StreamAdapter: public camera2_stream_ops, public virtual RefBase {
189 public:
190 StreamAdapter(camera2_device_t *d);
191
192 ~StreamAdapter();
193
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700194 /**
195 * Create a HAL device stream of the requested size and format.
196 *
197 * If format is CAMERA2_HAL_PIXEL_FORMAT_OPAQUE, then the HAL device
198 * selects an appropriate format; it can be queried with getFormat.
199 *
200 * If format is HAL_PIXEL_FORMAT_COMPRESSED, the size parameter must
201 * be equal to the size in bytes of the buffers to allocate for the
202 * stream. For other formats, the size parameter is ignored.
203 */
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700204 status_t connectToDevice(sp<ANativeWindow> consumer,
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700205 uint32_t width, uint32_t height, int format, size_t size);
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700206
207 status_t disconnect();
208
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700209 // Get stream parameters.
210 // Only valid after a successful connectToDevice call.
211 int getId() const { return mId; }
212 uint32_t getWidth() const { return mWidth; }
213 uint32_t getHeight() const { return mHeight; }
214 uint32_t getFormat() const { return mFormat; }
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700215
216 private:
217 enum {
218 ERROR = -1,
219 DISCONNECTED = 0,
220 ALLOCATED,
221 CONNECTED,
222 ACTIVE
223 } mState;
224
225 sp<ANativeWindow> mConsumerInterface;
226 camera2_device_t *mDevice;
227
228 uint32_t mId;
229 uint32_t mWidth;
230 uint32_t mHeight;
231 uint32_t mFormat;
Eino-Ville Talvalad4bcfde2012-06-07 17:12:38 -0700232 size_t mSize;
Eino-Ville Talvala6db981c2012-05-21 18:54:30 -0700233 uint32_t mUsage;
234 uint32_t mMaxProducerBuffers;
235 uint32_t mMaxConsumerBuffers;
236
237 int mFormatRequested;
238
239 const camera2_stream_ops *getStreamOps();
240
241 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
242
243 static int dequeue_buffer(const camera2_stream_ops_t *w,
244 buffer_handle_t** buffer);
245
246 static int enqueue_buffer(const camera2_stream_ops_t* w,
247 int64_t timestamp,
248 buffer_handle_t* buffer);
249
250 static int cancel_buffer(const camera2_stream_ops_t* w,
251 buffer_handle_t* buffer);
252
253 static int set_crop(const camera2_stream_ops_t* w,
254 int left, int top, int right, int bottom);
255 }; // class StreamAdapter
256
257 typedef List<sp<StreamAdapter> > StreamList;
258 StreamList mStreams;
259
Eino-Ville Talvalaf69c70d2012-05-20 15:59:14 -0700260}; // class Camera2Device
Eino-Ville Talvala61ab9f92012-05-17 10:30:54 -0700261
262}; // namespace android
263
264#endif