blob: c63c32adb4354f00bcb1fcb1813718213ee9ca3d [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#define LOG_TAG "Camera2_test_utils"
20#define LOG_NDEBUG 0
21
22#include "utils/Log.h"
23#include "camera2_utils.h"
Igor Murashkine302ee32012-11-05 11:14:49 -080024#include <dlfcn.h>
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070025
26namespace android {
Igor Murashkine302ee32012-11-05 11:14:49 -080027namespace camera2 {
28namespace tests {
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070029
30/**
31 * MetadataQueue
32 */
33
34MetadataQueue::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
50MetadataQueue::~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)
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070056const camera2_request_queue_src_ops_t* MetadataQueue::getToConsumerInterface() {
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070057 return static_cast<camera2_request_queue_src_ops_t*>(this);
58}
59
60void MetadataQueue::setFromConsumerInterface(camera2_device_t *d) {
61 mDevice = d;
62}
63
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -070064const camera2_frame_queue_dst_ops_t* MetadataQueue::getToProducerInterface() {
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070065 return static_cast<camera2_frame_queue_dst_ops_t*>(this);
66}
67
68// Real interfaces
69status_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
87int MetadataQueue::getBufferCount() {
88 Mutex::Autolock l(mMutex);
89 if (mStreamSlotCount > 0) {
90 return CAMERA2_REQUEST_QUEUE_IS_BOTTOMLESS;
91 }
92 return mCount;
93}
94
95status_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
137status_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
147status_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
168status_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
178status_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
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700187MetadataQueue* 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
193MetadataQueue* 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
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700199int MetadataQueue::consumer_buffer_count(
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700200 const camera2_request_queue_src_ops_t *q) {
201 MetadataQueue *queue = getInstance(q);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700202 return queue->getBufferCount();
203}
204
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700205int MetadataQueue::consumer_dequeue(const camera2_request_queue_src_ops_t *q,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700206 camera_metadata_t **buffer) {
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700207 MetadataQueue *queue = getInstance(q);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700208 return queue->dequeue(buffer, true);
209}
210
Andreas Gampe1aa58f92015-12-16 14:17:44 -0800211int MetadataQueue::consumer_free(const camera2_request_queue_src_ops_t * /* q */,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700212 camera_metadata_t *old_buffer) {
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700213 free_camera_metadata(old_buffer);
214 return OK;
215}
216
Andreas Gampe1aa58f92015-12-16 14:17:44 -0800217int MetadataQueue::producer_dequeue(const camera2_frame_queue_dst_ops_t * /* q */,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700218 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
Andreas Gampe1aa58f92015-12-16 14:17:44 -0800227int MetadataQueue::producer_cancel(const camera2_frame_queue_dst_ops_t * /* q */,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700228 camera_metadata_t *old_buffer) {
229 free_camera_metadata(old_buffer);
230 return OK;
231}
232
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700233int MetadataQueue::producer_enqueue(const camera2_frame_queue_dst_ops_t *q,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700234 camera_metadata_t *filled_buffer) {
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700235 MetadataQueue *queue = getInstance(q);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700236 return queue->enqueue(filled_buffer);
237}
238
239/**
240 * NotifierListener
241 */
242
243NotifierListener::NotifierListener() {
244}
245
246status_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
255status_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
264status_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
275int NotifierListener::numNotifications() {
276 Mutex::Autolock l(mMutex);
277 return mNotifications.size();
278}
279
280status_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
292void 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
301void 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
Andy McFaddeneda79df2012-12-18 09:50:24 -0800319StreamAdapter::StreamAdapter(sp<IGraphicBufferProducer> consumer):
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700320 mState(UNINITIALIZED), mDevice(NULL),
321 mId(-1),
Eino-Ville Talvala2388a2d2012-08-28 14:01:26 -0700322 mWidth(0), mHeight(0), mFormat(0)
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700323{
Mathias Agopiancc501112013-02-14 17:34:35 -0800324 mConsumerInterface = new Surface(consumer);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700325 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
331StreamAdapter::~StreamAdapter() {
332 disconnect();
333}
334
335status_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;
Eino-Ville Talvala2388a2d2012-08-28 14:01:26 -0700347 mFormat = format;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700348
349 // Allocate device-side stream interface
350
351 uint32_t id;
Eino-Ville Talvala2388a2d2012-08-28 14:01:26 -0700352 uint32_t formatActual; // ignored
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700353 uint32_t usage;
354 uint32_t maxBuffers = 2;
355 res = d->ops->allocate_stream(d,
Eino-Ville Talvala2388a2d2012-08-28 14:01:26 -0700356 mWidth, mHeight, mFormat, getStreamOps(),
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700357 &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;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700367 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
Andreas Gampe1aa58f92015-12-16 14:17:44 -0800389 res = native_window_set_buffers_dimensions(mConsumerInterface.get(),
390 mWidth, mHeight);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700391 if (res != OK) {
Andreas Gampe1aa58f92015-12-16 14:17:44 -0800392 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);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700404 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++) {
Jamie Gennisbd85f472012-06-13 16:36:01 -0700438 res = native_window_dequeue_buffer_and_wait(mConsumerInterface.get(),
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700439 &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 }
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700446 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
461cleanUpBuffers:
462 for (int i = 0; i < bufferIdx; i++) {
463 res = mConsumerInterface->cancelBuffer(mConsumerInterface.get(),
Jamie Gennisbd85f472012-06-13 16:36:01 -0700464 anwBuffers[i], -1);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700465 }
Andreas Gampe1aa58f92015-12-16 14:17:44 -0800466 delete[] anwBuffers;
467 delete[] buffers;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700468
469 return res;
470}
471
472status_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
496int StreamAdapter::getId() {
497 return mId;
498}
499
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700500const camera2_stream_ops *StreamAdapter::getStreamOps() {
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700501 return static_cast<camera2_stream_ops *>(this);
502}
503
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700504ANativeWindow* StreamAdapter::toANW(const camera2_stream_ops_t *w) {
505 return static_cast<const StreamAdapter*>(w)->mConsumerInterface.get();
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700506}
507
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700508int StreamAdapter::dequeue_buffer(const camera2_stream_ops_t *w,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700509 buffer_handle_t** buffer) {
510 int res;
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700511 int state = static_cast<const StreamAdapter*>(w)->mState;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700512 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;
Jamie Gennisbd85f472012-06-13 16:36:01 -0700519 res = native_window_dequeue_buffer_and_wait(a, &anb);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700520 if (res != OK) return res;
521
522 *buffer = &(anb->handle);
523
524 return res;
525}
526
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700527int StreamAdapter::enqueue_buffer(const camera2_stream_ops_t* w,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700528 int64_t timestamp,
529 buffer_handle_t* buffer) {
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700530 int state = static_cast<const StreamAdapter*>(w)->mState;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700531 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,
Jamie Gennisbd85f472012-06-13 16:36:01 -0700540 container_of(buffer, ANativeWindowBuffer, handle), -1);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700541}
542
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700543int StreamAdapter::cancel_buffer(const camera2_stream_ops_t* w,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700544 buffer_handle_t* buffer) {
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700545 int state = static_cast<const StreamAdapter*>(w)->mState;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700546 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,
Jamie Gennisbd85f472012-06-13 16:36:01 -0700552 container_of(buffer, ANativeWindowBuffer, handle), -1);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700553}
554
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700555int StreamAdapter::set_crop(const camera2_stream_ops_t* w,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700556 int left, int top, int right, int bottom) {
Eino-Ville Talvala08a6e5e2012-05-17 17:54:56 -0700557 int state = static_cast<const StreamAdapter*>(w)->mState;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700558 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
571FrameWaiter::FrameWaiter():
572 mPendingFrames(0) {
573}
574
575status_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
Dan Stoza32ef0ca2014-11-04 11:39:35 -0800586void FrameWaiter::onFrameAvailable(const BufferItem& /* item */) {
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700587 Mutex::Autolock lock(mMutex);
588 mPendingFrames++;
589 mCondition.signal();
590}
591
Yin-Chia Yehc2537652015-03-24 10:48:08 -0700592int HWModuleHelpers::closeModule(void *dso) {
Igor Murashkine302ee32012-11-05 11:14:49 -0800593 int status;
Yin-Chia Yehc2537652015-03-24 10:48:08 -0700594 if (!dso) {
Igor Murashkine302ee32012-11-05 11:14:49 -0800595 return -EINVAL;
596 }
597
Yin-Chia Yehc2537652015-03-24 10:48:08 -0700598 status = dlclose(dso);
Igor Murashkine302ee32012-11-05 11:14:49 -0800599 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
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700609} // namespace android