blob: c938ee76992a981c63c5cd612928d83d9b1eb82f [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 {
Igor Murashkine302ee32012-11-05 11:14:49 -080030namespace camera2 {
31namespace tests {
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070032
33/**
34 * Queue class for both sending requests to a camera2 device, and for receiving
35 * frames from a camera2 device.
36 */
37class MetadataQueue: public camera2_request_queue_src_ops_t,
38 public camera2_frame_queue_dst_ops_t {
39 public:
40 MetadataQueue();
41 ~MetadataQueue();
42
43 // Interface to camera2 HAL device, either for requests (device is consumer)
44 // or for frames (device is producer)
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070045 const camera2_request_queue_src_ops_t* getToConsumerInterface();
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070046 void setFromConsumerInterface(camera2_device_t *d);
47
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070048 const camera2_frame_queue_dst_ops_t* getToProducerInterface();
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070049
50 // Real interfaces. On enqueue, queue takes ownership of buffer pointer
51 // On dequeue, user takes ownership of buffer pointer.
52 status_t enqueue(camera_metadata_t *buf);
53 status_t dequeue(camera_metadata_t **buf, bool incrementCount = true);
54 int getBufferCount();
55 status_t waitForBuffer(nsecs_t timeout);
56
57 // Set repeating buffer(s); if the queue is empty on a dequeue call, the
58 // queue copies the contents of the stream slot into the queue, and then
59 // dequeues the first new entry.
60 status_t setStreamSlot(camera_metadata_t *buf);
61 status_t setStreamSlot(const List<camera_metadata_t*> &bufs);
62
63 private:
64 status_t freeBuffers(List<camera_metadata_t*>::iterator start,
65 List<camera_metadata_t*>::iterator end);
66
67 camera2_device_t *mDevice;
68
69 Mutex mMutex;
70 Condition notEmpty;
71
72 int mFrameCount;
73
74 int mCount;
75 List<camera_metadata_t*> mEntries;
76 int mStreamSlotCount;
77 List<camera_metadata_t*> mStreamSlot;
78
79 bool mSignalConsumer;
80
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070081 static MetadataQueue* getInstance(const camera2_frame_queue_dst_ops_t *q);
82 static MetadataQueue* getInstance(const camera2_request_queue_src_ops_t *q);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070083
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070084 static int consumer_buffer_count(const camera2_request_queue_src_ops_t *q);
85
86 static int consumer_dequeue(const camera2_request_queue_src_ops_t *q,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070087 camera_metadata_t **buffer);
88
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070089 static int consumer_free(const camera2_request_queue_src_ops_t *q,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070090 camera_metadata_t *old_buffer);
91
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070092 static int producer_dequeue(const camera2_frame_queue_dst_ops_t *q,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070093 size_t entries, size_t bytes,
94 camera_metadata_t **buffer);
95
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070096 static int producer_cancel(const camera2_frame_queue_dst_ops_t *q,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070097 camera_metadata_t *old_buffer);
98
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070099 static int producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700100 camera_metadata_t *filled_buffer);
101
102};
103
104/**
105 * Basic class to receive and queue up notifications from the camera device
106 */
107
108class NotifierListener {
109 public:
110
111 NotifierListener();
112
113 status_t getNotificationsFrom(camera2_device *dev);
114
115 status_t getNextNotification(int32_t *msg_type, int32_t *ext1,
116 int32_t *ext2, int32_t *ext3);
117
118 status_t waitForNotification(int32_t *msg_type, int32_t *ext1,
119 int32_t *ext2, int32_t *ext3);
120
121 int numNotifications();
122
123 private:
124
125 status_t getNextNotificationLocked(int32_t *msg_type,
126 int32_t *ext1, int32_t *ext2, int32_t *ext3);
127
128 struct Notification {
129 Notification(int32_t type, int32_t e1, int32_t e2, int32_t e3):
130 msg_type(type),
131 ext1(e1),
132 ext2(e2),
133 ext3(e3)
134 {}
135
136 int32_t msg_type;
137 int32_t ext1;
138 int32_t ext2;
139 int32_t ext3;
140 };
141
142 List<Notification> mNotifications;
143
144 Mutex mMutex;
145 Condition mNewNotification;
146
147 void onNotify(int32_t msg_type,
148 int32_t ext1,
149 int32_t ext2,
150 int32_t ext3);
151
152 static void notify_callback_dispatch(int32_t msg_type,
153 int32_t ext1,
154 int32_t ext2,
155 int32_t ext3,
156 void *user);
157
158};
159
160/**
161 * Adapter from an ISurfaceTexture interface to camera2 device stream ops.
162 * Also takes care of allocating/deallocating stream in device interface
163 */
164class StreamAdapter: public camera2_stream_ops {
165 public:
166 StreamAdapter(sp<ISurfaceTexture> consumer);
167
168 ~StreamAdapter();
169
170 status_t connectToDevice(camera2_device_t *d,
171 uint32_t width, uint32_t height, int format);
172
173 status_t disconnect();
174
175 // Get stream ID. Only valid after a successful connectToDevice call.
176 int getId();
177
178 private:
179 enum {
180 ERROR = -1,
181 DISCONNECTED = 0,
182 UNINITIALIZED,
183 ALLOCATED,
184 CONNECTED,
185 ACTIVE
186 } mState;
187
188 sp<ANativeWindow> mConsumerInterface;
189 camera2_device_t *mDevice;
190
191 uint32_t mId;
192 uint32_t mWidth;
193 uint32_t mHeight;
194 uint32_t mFormat;
195 uint32_t mUsage;
196 uint32_t mMaxProducerBuffers;
197 uint32_t mMaxConsumerBuffers;
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
Igor Murashkine302ee32012-11-05 11:14:49 -0800238struct HWModuleHelpers {
239 /* attempt to unload the library with dlclose */
240 static int closeModule(hw_module_t* module);
241};
242
243}
244}
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700245}