blob: 2c9f801622e080124b64ee3f7e9d1b4412d4ecb6 [file] [log] [blame]
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -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// Utility classes for camera2 HAL testing
18
19#include <system/camera_metadata.h>
20#include <hardware/camera2.h>
21
22#include <gui/SurfaceTextureClient.h>
23#include <gui/CpuConsumer.h>
24
25#include <utils/List.h>
26#include <utils/Mutex.h>
27#include <utils/Condition.h>
28
29namespace android {
30
31/**
32 * Queue class for both sending requests to a camera2 device, and for receiving
33 * frames from a camera2 device.
34 */
35class MetadataQueue: public camera2_request_queue_src_ops_t,
36 public camera2_frame_queue_dst_ops_t {
37 public:
38 MetadataQueue();
39 ~MetadataQueue();
40
41 // Interface to camera2 HAL device, either for requests (device is consumer)
42 // or for frames (device is producer)
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070043 const camera2_request_queue_src_ops_t* getToConsumerInterface();
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070044 void setFromConsumerInterface(camera2_device_t *d);
45
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070046 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070047
48 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
49 // On dequeue, user takes ownership of buffer pointer.
50 status_t enqueue(camera_metadata_t *buf);
51 status_t dequeue(camera_metadata_t **buf, bool incrementCount = true);
52 int getBufferCount();
53 status_t waitForBuffer(nsecs_t timeout);
54
55 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
56 // queue copies the contents of the stream slot into the queue, and then
57 // dequeues the first new entry.
58 status_t setStreamSlot(camera_metadata_t *buf);
59 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
60
61 private:
62 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
63 List<camera_metadata_t*>::iterator end);
64
65 camera2_device_t *mDevice;
66
67 Mutex mMutex;
68 Condition notEmpty;
69
70 int mFrameCount;
71
72 int mCount;
73 List<camera_metadata_t*> mEntries;
74 int mStreamSlotCount;
75 List<camera_metadata_t*> mStreamSlot;
76
77 bool mSignalConsumer;
78
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070079 static MetadataQueue* getInstance(const camera2_frame_queue_dst_ops_t *q);
80 static MetadataQueue* getInstance(const camera2_request_queue_src_ops_t *q);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070081
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070082 static int consumer_buffer_count(const camera2_request_queue_src_ops_t *q);
83
84 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070085 camera_metadata_t **buffer);
86
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070087 static int consumer_free(const camera2_request_queue_src_ops_t *q,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070088 camera_metadata_t *old_buffer);
89
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070090 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070091 size_t entries, size_t bytes,
92 camera_metadata_t **buffer);
93
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070094 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070095 camera_metadata_t *old_buffer);
96
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070097 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070098 camera_metadata_t *filled_buffer);
99
100};
101
102/**
103 * Basic class to receive and queue up notifications from the camera device
104 */
105
106class NotifierListener {
107 public:
108
109 NotifierListener();
110
111 status_t getNotificationsFrom(camera2_device *dev);
112
113 status_t getNextNotification(int32_t *msg_type, int32_t *ext1,
114 int32_t *ext2, int32_t *ext3);
115
116 status_t waitForNotification(int32_t *msg_type, int32_t *ext1,
117 int32_t *ext2, int32_t *ext3);
118
119 int numNotifications();
120
121 private:
122
123 status_t getNextNotificationLocked(int32_t *msg_type,
124 int32_t *ext1, int32_t *ext2, int32_t *ext3);
125
126 struct Notification {
127 Notification(int32_t type, int32_t e1, int32_t e2, int32_t e3):
128 msg_type(type),
129 ext1(e1),
130 ext2(e2),
131 ext3(e3)
132 {}
133
134 int32_t msg_type;
135 int32_t ext1;
136 int32_t ext2;
137 int32_t ext3;
138 };
139
140 List<Notification> mNotifications;
141
142 Mutex mMutex;
143 Condition mNewNotification;
144
145 void onNotify(int32_t msg_type,
146 int32_t ext1,
147 int32_t ext2,
148 int32_t ext3);
149
150 static void notify_callback_dispatch(int32_t msg_type,
151 int32_t ext1,
152 int32_t ext2,
153 int32_t ext3,
154 void *user);
155
156};
157
158/**
159 * Adapter from an ISurfaceTexture interface to camera2 device stream ops.
160 * Also takes care of allocating/deallocating stream in device interface
161 */
162class StreamAdapter: public camera2_stream_ops {
163 public:
164 StreamAdapter(sp<ISurfaceTexture> consumer);
165
166 ~StreamAdapter();
167
168 status_t connectToDevice(camera2_device_t *d,
169 uint32_t width, uint32_t height, int format);
170
171 status_t disconnect();
172
173 // Get stream ID. Only valid after a successful connectToDevice call.
174 int getId();
175
176 private:
177 enum {
178 ERROR = -1,
179 DISCONNECTED = 0,
180 UNINITIALIZED,
181 ALLOCATED,
182 CONNECTED,
183 ACTIVE
184 } mState;
185
186 sp<ANativeWindow> mConsumerInterface;
187 camera2_device_t *mDevice;
188
189 uint32_t mId;
190 uint32_t mWidth;
191 uint32_t mHeight;
192 uint32_t mFormat;
193 uint32_t mUsage;
194 uint32_t mMaxProducerBuffers;
195 uint32_t mMaxConsumerBuffers;
196
197 int mFormatRequested;
198
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700199 const camera2_stream_ops *getStreamOps();
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700200
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700201 static ANativeWindow* toANW(const camera2_stream_ops_t *w);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700202
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700203 static int dequeue_buffer(const camera2_stream_ops_t *w,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700204 buffer_handle_t** buffer);
205
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700206 static int enqueue_buffer(const camera2_stream_ops_t* w,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700207 int64_t timestamp,
208 buffer_handle_t* buffer);
209
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700210 static int cancel_buffer(const camera2_stream_ops_t* w,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700211 buffer_handle_t* buffer);
212
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700213 static int set_crop(const camera2_stream_ops_t* w,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700214 int left, int top, int right, int bottom);
215
216};
217
218/**
219 * Simple class to wait on the CpuConsumer to have a frame available
220 */
221class FrameWaiter : public CpuConsumer::FrameAvailableListener {
222 public:
223 FrameWaiter();
224
225 /**
226 * Wait for max timeout nanoseconds for a new frame. Returns
227 * OK if a frame is available, TIMED_OUT if the timeout was reached.
228 */
229 status_t waitForFrame(nsecs_t timeout);
230
231 virtual void onFrameAvailable();
232
233 int mPendingFrames;
234 Mutex mMutex;
235 Condition mCondition;
236};
237
238}