1 /*
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 #define LOG_TAG "Camera2_test_utils"
20 #define LOG_NDEBUG 0
21
22 #include "utils/Log.h"
23 #include "camera2_utils.h"
24 #include <dlfcn.h>
25
26 namespace android {
27 namespace camera2 {
28 namespace tests {
29
30 /**
31 * MetadataQueue
32 */
33
MetadataQueue()34 MetadataQueue::MetadataQueue():
35 mDevice(NULL),
36 mFrameCount(0),
37 mCount(0),
38 mStreamSlotCount(0),
39 mSignalConsumer(true)
40 {
41 camera2_request_queue_src_ops::dequeue_request = consumer_dequeue;
42 camera2_request_queue_src_ops::request_count = consumer_buffer_count;
43 camera2_request_queue_src_ops::free_request = consumer_free;
44
45 camera2_frame_queue_dst_ops::dequeue_frame = producer_dequeue;
46 camera2_frame_queue_dst_ops::cancel_frame = producer_cancel;
47 camera2_frame_queue_dst_ops::enqueue_frame = producer_enqueue;
48 }
49
~MetadataQueue()50 MetadataQueue::~MetadataQueue() {
51 freeBuffers(mEntries.begin(), mEntries.end());
52 freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
53 }
54
55 // Interface to camera2 HAL as consumer (input requests/reprocessing)
getToConsumerInterface()56 const camera2_request_queue_src_ops_t* MetadataQueue::getToConsumerInterface() {
57 return static_cast<camera2_request_queue_src_ops_t*>(this);
58 }
59
setFromConsumerInterface(camera2_device_t * d)60 void MetadataQueue::setFromConsumerInterface(camera2_device_t *d) {
61 mDevice = d;
62 }
63
getToProducerInterface()64 const camera2_frame_queue_dst_ops_t* MetadataQueue::getToProducerInterface() {
65 return static_cast<camera2_frame_queue_dst_ops_t*>(this);
66 }
67
68 // Real interfaces
enqueue(camera_metadata_t * buf)69 status_t MetadataQueue::enqueue(camera_metadata_t *buf) {
70 Mutex::Autolock l(mMutex);
71
72 mCount++;
73 mEntries.push_back(buf);
74 notEmpty.signal();
75
76 if (mSignalConsumer && mDevice != NULL) {
77 mSignalConsumer = false;
78
79 mMutex.unlock();
80 ALOGV("%s: Signaling consumer", __FUNCTION__);
81 mDevice->ops->notify_request_queue_not_empty(mDevice);
82 mMutex.lock();
83 }
84 return OK;
85 }
86
getBufferCount()87 int MetadataQueue::getBufferCount() {
88 Mutex::Autolock l(mMutex);
89 if (mStreamSlotCount > 0) {
90 return CAMERA2_REQUEST_QUEUE_IS_BOTTOMLESS;
91 }
92 return mCount;
93 }
94
dequeue(camera_metadata_t ** buf,bool incrementCount)95 status_t MetadataQueue::dequeue(camera_metadata_t **buf, bool incrementCount) {
96 Mutex::Autolock l(mMutex);
97
98 if (mCount == 0) {
99 if (mStreamSlotCount == 0) {
100 ALOGV("%s: Empty", __FUNCTION__);
101 *buf = NULL;
102 mSignalConsumer = true;
103 return OK;
104 }
105 ALOGV("%s: Streaming %d frames to queue", __FUNCTION__,
106 mStreamSlotCount);
107
108 for (List<camera_metadata_t*>::iterator slotEntry = mStreamSlot.begin();
109 slotEntry != mStreamSlot.end();
110 slotEntry++ ) {
111 size_t entries = get_camera_metadata_entry_count(*slotEntry);
112 size_t dataBytes = get_camera_metadata_data_count(*slotEntry);
113
114 camera_metadata_t *copy = allocate_camera_metadata(entries, dataBytes);
115 append_camera_metadata(copy, *slotEntry);
116 mEntries.push_back(copy);
117 }
118 mCount = mStreamSlotCount;
119 }
120 ALOGV("MetadataQueue: deque (%d buffers)", mCount);
121 camera_metadata_t *b = *(mEntries.begin());
122 mEntries.erase(mEntries.begin());
123
124 if (incrementCount) {
125 add_camera_metadata_entry(b,
126 ANDROID_REQUEST_FRAME_COUNT,
127 (void**)&mFrameCount, 1);
128 mFrameCount++;
129 }
130
131 *buf = b;
132 mCount--;
133
134 return OK;
135 }
136
waitForBuffer(nsecs_t timeout)137 status_t MetadataQueue::waitForBuffer(nsecs_t timeout) {
138 Mutex::Autolock l(mMutex);
139 status_t res;
140 while (mCount == 0) {
141 res = notEmpty.waitRelative(mMutex,timeout);
142 if (res != OK) return res;
143 }
144 return OK;
145 }
146
setStreamSlot(camera_metadata_t * buf)147 status_t MetadataQueue::setStreamSlot(camera_metadata_t *buf) {
148 if (buf == NULL) {
149 freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
150 mStreamSlotCount = 0;
151 return OK;
152 }
153 if (mStreamSlotCount > 1) {
154 List<camera_metadata_t*>::iterator deleter = ++mStreamSlot.begin();
155 freeBuffers(++mStreamSlot.begin(), mStreamSlot.end());
156 mStreamSlotCount = 1;
157 }
158 if (mStreamSlotCount == 1) {
159 free_camera_metadata( *(mStreamSlot.begin()) );
160 *(mStreamSlot.begin()) = buf;
161 } else {
162 mStreamSlot.push_front(buf);
163 mStreamSlotCount = 1;
164 }
165 return OK;
166 }
167
setStreamSlot(const List<camera_metadata_t * > & bufs)168 status_t MetadataQueue::setStreamSlot(const List<camera_metadata_t*> &bufs) {
169 if (mStreamSlotCount > 0) {
170 freeBuffers(mStreamSlot.begin(), mStreamSlot.end());
171 }
172 mStreamSlot = bufs;
173 mStreamSlotCount = mStreamSlot.size();
174
175 return OK;
176 }
177
freeBuffers(List<camera_metadata_t * >::iterator start,List<camera_metadata_t * >::iterator end)178 status_t MetadataQueue::freeBuffers(List<camera_metadata_t*>::iterator start,
179 List<camera_metadata_t*>::iterator end) {
180 while (start != end) {
181 free_camera_metadata(*start);
182 start = mStreamSlot.erase(start);
183 }
184 return OK;
185 }
186
getInstance(const camera2_request_queue_src_ops_t * q)187 MetadataQueue* MetadataQueue::getInstance(
188 const camera2_request_queue_src_ops_t *q) {
189 const MetadataQueue* cmq = static_cast<const MetadataQueue*>(q);
190 return const_cast<MetadataQueue*>(cmq);
191 }
192
getInstance(const camera2_frame_queue_dst_ops_t * q)193 MetadataQueue* MetadataQueue::getInstance(
194 const camera2_frame_queue_dst_ops_t *q) {
195 const MetadataQueue* cmq = static_cast<const MetadataQueue*>(q);
196 return const_cast<MetadataQueue*>(cmq);
197 }
198
consumer_buffer_count(const camera2_request_queue_src_ops_t * q)199 int MetadataQueue::consumer_buffer_count(
200 const camera2_request_queue_src_ops_t *q) {
201 MetadataQueue *queue = getInstance(q);
202 return queue->getBufferCount();
203 }
204
consumer_dequeue(const camera2_request_queue_src_ops_t * q,camera_metadata_t ** buffer)205 int MetadataQueue::consumer_dequeue(const camera2_request_queue_src_ops_t *q,
206 camera_metadata_t **buffer) {
207 MetadataQueue *queue = getInstance(q);
208 return queue->dequeue(buffer, true);
209 }
210
consumer_free(const camera2_request_queue_src_ops_t *,camera_metadata_t * old_buffer)211 int MetadataQueue::consumer_free(const camera2_request_queue_src_ops_t * /* q */,
212 camera_metadata_t *old_buffer) {
213 free_camera_metadata(old_buffer);
214 return OK;
215 }
216
producer_dequeue(const camera2_frame_queue_dst_ops_t *,size_t entries,size_t bytes,camera_metadata_t ** buffer)217 int MetadataQueue::producer_dequeue(const camera2_frame_queue_dst_ops_t * /* q */,
218 size_t entries, size_t bytes,
219 camera_metadata_t **buffer) {
220 camera_metadata_t *new_buffer =
221 allocate_camera_metadata(entries, bytes);
222 if (new_buffer == NULL) return NO_MEMORY;
223 *buffer = new_buffer;
224 return OK;
225 }
226
producer_cancel(const camera2_frame_queue_dst_ops_t *,camera_metadata_t * old_buffer)227 int MetadataQueue::producer_cancel(const camera2_frame_queue_dst_ops_t * /* q */,
228 camera_metadata_t *old_buffer) {
229 free_camera_metadata(old_buffer);
230 return OK;
231 }
232
producer_enqueue(const camera2_frame_queue_dst_ops_t * q,camera_metadata_t * filled_buffer)233 int MetadataQueue::producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
234 camera_metadata_t *filled_buffer) {
235 MetadataQueue *queue = getInstance(q);
236 return queue->enqueue(filled_buffer);
237 }
238
239 /**
240 * NotifierListener
241 */
242
NotifierListener()243 NotifierListener::NotifierListener() {
244 }
245
getNotificationsFrom(camera2_device * dev)246 status_t NotifierListener::getNotificationsFrom(camera2_device *dev) {
247 if (!dev) return BAD_VALUE;
248 status_t err;
249 err = dev->ops->set_notify_callback(dev,
250 notify_callback_dispatch,
251 (void*)this);
252 return err;
253 }
254
getNextNotification(int32_t * msg_type,int32_t * ext1,int32_t * ext2,int32_t * ext3)255 status_t NotifierListener::getNextNotification(int32_t *msg_type,
256 int32_t *ext1,
257 int32_t *ext2,
258 int32_t *ext3) {
259 Mutex::Autolock l(mMutex);
260 if (mNotifications.size() == 0) return BAD_VALUE;
261 return getNextNotificationLocked(msg_type, ext1, ext2, ext3);
262 }
263
waitForNotification(int32_t * msg_type,int32_t * ext1,int32_t * ext2,int32_t * ext3)264 status_t NotifierListener::waitForNotification(int32_t *msg_type,
265 int32_t *ext1,
266 int32_t *ext2,
267 int32_t *ext3) {
268 Mutex::Autolock l(mMutex);
269 while (mNotifications.size() == 0) {
270 mNewNotification.wait(mMutex);
271 }
272 return getNextNotificationLocked(msg_type, ext1, ext2, ext3);
273 }
274
numNotifications()275 int NotifierListener::numNotifications() {
276 Mutex::Autolock l(mMutex);
277 return mNotifications.size();
278 }
279
getNextNotificationLocked(int32_t * msg_type,int32_t * ext1,int32_t * ext2,int32_t * ext3)280 status_t NotifierListener::getNextNotificationLocked(int32_t *msg_type,
281 int32_t *ext1,
282 int32_t *ext2,
283 int32_t *ext3) {
284 *msg_type = mNotifications.begin()->msg_type;
285 *ext1 = mNotifications.begin()->ext1;
286 *ext2 = mNotifications.begin()->ext2;
287 *ext3 = mNotifications.begin()->ext3;
288 mNotifications.erase(mNotifications.begin());
289 return OK;
290 }
291
onNotify(int32_t msg_type,int32_t ext1,int32_t ext2,int32_t ext3)292 void NotifierListener::onNotify(int32_t msg_type,
293 int32_t ext1,
294 int32_t ext2,
295 int32_t ext3) {
296 Mutex::Autolock l(mMutex);
297 mNotifications.push_back(Notification(msg_type, ext1, ext2, ext3));
298 mNewNotification.signal();
299 }
300
notify_callback_dispatch(int32_t msg_type,int32_t ext1,int32_t ext2,int32_t ext3,void * user)301 void NotifierListener::notify_callback_dispatch(int32_t msg_type,
302 int32_t ext1,
303 int32_t ext2,
304 int32_t ext3,
305 void *user) {
306 NotifierListener *me = reinterpret_cast<NotifierListener*>(user);
307 me->onNotify(msg_type, ext1, ext2, ext3);
308 }
309
310 /**
311 * StreamAdapter
312 */
313
314 #ifndef container_of
315 #define container_of(ptr, type, member) \
316 (type *)((char*)(ptr) - offsetof(type, member))
317 #endif
318
StreamAdapter(sp<IGraphicBufferProducer> consumer)319 StreamAdapter::StreamAdapter(sp<IGraphicBufferProducer> consumer):
320 mState(UNINITIALIZED), mDevice(NULL),
321 mId(-1),
322 mWidth(0), mHeight(0), mFormat(0)
323 {
324 mConsumerInterface = new Surface(consumer);
325 camera2_stream_ops::dequeue_buffer = dequeue_buffer;
326 camera2_stream_ops::enqueue_buffer = enqueue_buffer;
327 camera2_stream_ops::cancel_buffer = cancel_buffer;
328 camera2_stream_ops::set_crop = set_crop;
329 }
330
~StreamAdapter()331 StreamAdapter::~StreamAdapter() {
332 disconnect();
333 }
334
connectToDevice(camera2_device_t * d,uint32_t width,uint32_t height,int format)335 status_t StreamAdapter::connectToDevice(camera2_device_t *d,
336 uint32_t width, uint32_t height, int format) {
337 if (mState != UNINITIALIZED) return INVALID_OPERATION;
338 if (d == NULL) {
339 ALOGE("%s: Null device passed to stream adapter", __FUNCTION__);
340 return BAD_VALUE;
341 }
342
343 status_t res;
344
345 mWidth = width;
346 mHeight = height;
347 mFormat = format;
348
349 // Allocate device-side stream interface
350
351 uint32_t id;
352 uint32_t formatActual; // ignored
353 uint32_t usage;
354 uint32_t maxBuffers = 2;
355 res = d->ops->allocate_stream(d,
356 mWidth, mHeight, mFormat, getStreamOps(),
357 &id, &formatActual, &usage, &maxBuffers);
358 if (res != OK) {
359 ALOGE("%s: Device stream allocation failed: %s (%d)",
360 __FUNCTION__, strerror(-res), res);
361 mState = UNINITIALIZED;
362 return res;
363 }
364 mDevice = d;
365
366 mId = id;
367 mUsage = usage;
368 mMaxProducerBuffers = maxBuffers;
369
370 // Configure consumer-side ANativeWindow interface
371
372 res = native_window_api_connect(mConsumerInterface.get(),
373 NATIVE_WINDOW_API_CAMERA);
374 if (res != OK) {
375 ALOGE("%s: Unable to connect to native window for stream %d",
376 __FUNCTION__, mId);
377 mState = ALLOCATED;
378 return res;
379 }
380
381 res = native_window_set_usage(mConsumerInterface.get(), mUsage);
382 if (res != OK) {
383 ALOGE("%s: Unable to configure usage %08x for stream %d",
384 __FUNCTION__, mUsage, mId);
385 mState = CONNECTED;
386 return res;
387 }
388
389 res = native_window_set_buffers_dimensions(mConsumerInterface.get(),
390 mWidth, mHeight);
391 if (res != OK) {
392 ALOGE("%s: Unable to configure buffer dimensions"
393 " %d x %d for stream %d",
394 __FUNCTION__, mWidth, mHeight, mId);
395 mState = CONNECTED;
396 return res;
397 }
398 res = native_window_set_buffers_format(mConsumerInterface.get(),
399 mFormat);
400 if (res != OK) {
401 ALOGE("%s: Unable to configure buffer format"
402 " 0x%x for stream %d",
403 __FUNCTION__, mFormat, mId);
404 mState = CONNECTED;
405 return res;
406 }
407
408 int maxConsumerBuffers;
409 res = mConsumerInterface->query(mConsumerInterface.get(),
410 NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS, &maxConsumerBuffers);
411 if (res != OK) {
412 ALOGE("%s: Unable to query consumer undequeued"
413 " buffer count for stream %d", __FUNCTION__, mId);
414 mState = CONNECTED;
415 return res;
416 }
417 mMaxConsumerBuffers = maxConsumerBuffers;
418
419 ALOGV("%s: Producer wants %d buffers, consumer wants %d", __FUNCTION__,
420 mMaxProducerBuffers, mMaxConsumerBuffers);
421
422 int totalBuffers = mMaxConsumerBuffers + mMaxProducerBuffers;
423
424 res = native_window_set_buffer_count(mConsumerInterface.get(),
425 totalBuffers);
426 if (res != OK) {
427 ALOGE("%s: Unable to set buffer count for stream %d",
428 __FUNCTION__, mId);
429 mState = CONNECTED;
430 return res;
431 }
432
433 // Register allocated buffers with HAL device
434 buffer_handle_t *buffers = new buffer_handle_t[totalBuffers];
435 ANativeWindowBuffer **anwBuffers = new ANativeWindowBuffer*[totalBuffers];
436 int bufferIdx = 0;
437 for (; bufferIdx < totalBuffers; bufferIdx++) {
438 res = native_window_dequeue_buffer_and_wait(mConsumerInterface.get(),
439 &anwBuffers[bufferIdx]);
440 if (res != OK) {
441 ALOGE("%s: Unable to dequeue buffer %d for initial registration for"
442 "stream %d", __FUNCTION__, bufferIdx, mId);
443 mState = CONNECTED;
444 goto cleanUpBuffers;
445 }
446 buffers[bufferIdx] = anwBuffers[bufferIdx]->handle;
447 }
448
449 res = mDevice->ops->register_stream_buffers(mDevice,
450 mId,
451 totalBuffers,
452 buffers);
453 if (res != OK) {
454 ALOGE("%s: Unable to register buffers with HAL device for stream %d",
455 __FUNCTION__, mId);
456 mState = CONNECTED;
457 } else {
458 mState = ACTIVE;
459 }
460
461 cleanUpBuffers:
462 for (int i = 0; i < bufferIdx; i++) {
463 res = mConsumerInterface->cancelBuffer(mConsumerInterface.get(),
464 anwBuffers[i], -1);
465 }
466 delete[] anwBuffers;
467 delete[] buffers;
468
469 return res;
470 }
471
disconnect()472 status_t StreamAdapter::disconnect() {
473 status_t res;
474 if (mState >= ALLOCATED) {
475 res = mDevice->ops->release_stream(mDevice, mId);
476 if (res != OK) {
477 ALOGE("%s: Unable to release stream %d",
478 __FUNCTION__, mId);
479 return res;
480 }
481 }
482 if (mState >= CONNECTED) {
483 res = native_window_api_disconnect(mConsumerInterface.get(),
484 NATIVE_WINDOW_API_CAMERA);
485 if (res != OK) {
486 ALOGE("%s: Unable to disconnect stream %d from native window",
487 __FUNCTION__, mId);
488 return res;
489 }
490 }
491 mId = -1;
492 mState = DISCONNECTED;
493 return OK;
494 }
495
getId()496 int StreamAdapter::getId() {
497 return mId;
498 }
499
getStreamOps()500 const camera2_stream_ops *StreamAdapter::getStreamOps() {
501 return static_cast<camera2_stream_ops *>(this);
502 }
503
toANW(const camera2_stream_ops_t * w)504 ANativeWindow* StreamAdapter::toANW(const camera2_stream_ops_t *w) {
505 return static_cast<const StreamAdapter*>(w)->mConsumerInterface.get();
506 }
507
dequeue_buffer(const camera2_stream_ops_t * w,buffer_handle_t ** buffer)508 int StreamAdapter::dequeue_buffer(const camera2_stream_ops_t *w,
509 buffer_handle_t** buffer) {
510 int res;
511 int state = static_cast<const StreamAdapter*>(w)->mState;
512 if (state != ACTIVE) {
513 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
514 return INVALID_OPERATION;
515 }
516
517 ANativeWindow *a = toANW(w);
518 ANativeWindowBuffer* anb;
519 res = native_window_dequeue_buffer_and_wait(a, &anb);
520 if (res != OK) return res;
521
522 *buffer = &(anb->handle);
523
524 return res;
525 }
526
enqueue_buffer(const camera2_stream_ops_t * w,int64_t timestamp,buffer_handle_t * buffer)527 int StreamAdapter::enqueue_buffer(const camera2_stream_ops_t* w,
528 int64_t timestamp,
529 buffer_handle_t* buffer) {
530 int state = static_cast<const StreamAdapter*>(w)->mState;
531 if (state != ACTIVE) {
532 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
533 return INVALID_OPERATION;
534 }
535 ANativeWindow *a = toANW(w);
536 status_t err;
537 err = native_window_set_buffers_timestamp(a, timestamp);
538 if (err != OK) return err;
539 return a->queueBuffer(a,
540 container_of(buffer, ANativeWindowBuffer, handle), -1);
541 }
542
cancel_buffer(const camera2_stream_ops_t * w,buffer_handle_t * buffer)543 int StreamAdapter::cancel_buffer(const camera2_stream_ops_t* w,
544 buffer_handle_t* buffer) {
545 int state = static_cast<const StreamAdapter*>(w)->mState;
546 if (state != ACTIVE) {
547 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
548 return INVALID_OPERATION;
549 }
550 ANativeWindow *a = toANW(w);
551 return a->cancelBuffer(a,
552 container_of(buffer, ANativeWindowBuffer, handle), -1);
553 }
554
set_crop(const camera2_stream_ops_t * w,int left,int top,int right,int bottom)555 int StreamAdapter::set_crop(const camera2_stream_ops_t* w,
556 int left, int top, int right, int bottom) {
557 int state = static_cast<const StreamAdapter*>(w)->mState;
558 if (state != ACTIVE) {
559 ALOGE("%s: Called when in bad state: %d", __FUNCTION__, state);
560 return INVALID_OPERATION;
561 }
562 ANativeWindow *a = toANW(w);
563 android_native_rect_t crop = { left, top, right, bottom };
564 return native_window_set_crop(a, &crop);
565 }
566
567 /**
568 * FrameWaiter
569 */
570
FrameWaiter()571 FrameWaiter::FrameWaiter():
572 mPendingFrames(0) {
573 }
574
waitForFrame(nsecs_t timeout)575 status_t FrameWaiter::waitForFrame(nsecs_t timeout) {
576 status_t res;
577 Mutex::Autolock lock(mMutex);
578 while (mPendingFrames == 0) {
579 res = mCondition.waitRelative(mMutex, timeout);
580 if (res != OK) return res;
581 }
582 mPendingFrames--;
583 return OK;
584 }
585
onFrameAvailable(const BufferItem &)586 void FrameWaiter::onFrameAvailable(const BufferItem& /* item */) {
587 Mutex::Autolock lock(mMutex);
588 mPendingFrames++;
589 mCondition.signal();
590 }
591
closeModule(void * dso)592 int HWModuleHelpers::closeModule(void *dso) {
593 int status;
594 if (!dso) {
595 return -EINVAL;
596 }
597
598 status = dlclose(dso);
599 if (status != 0) {
600 char const *err_str = dlerror();
601 ALOGE("%s dlclose failed, error: %s", __func__, err_str ?: "unknown");
602 }
603
604 return status;
605 }
606
607 } // namespace tests
608 } // namespace camera2
609 } // namespace android
610