blob: 600d4404d0d1b5c7e16f14372a6f4178e6455b17 [file] [log] [blame]
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -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
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070017#define LOG_TAG "Camera2_test"
Igor Murashkineab33fc2012-11-06 17:02:54 -080018//#define LOG_NDEBUG 0
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070019
20#include <utils/Log.h>
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -070021#include <gtest/gtest.h>
22#include <iostream>
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070023#include <fstream>
24
25#include <utils/Vector.h>
26#include <gui/CpuConsumer.h>
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -070027#include <ui/PixelFormat.h>
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070028#include <system/camera_metadata.h>
29
30#include "camera2_utils.h"
Igor Murashkineab33fc2012-11-06 17:02:54 -080031#include "TestExtensions.h"
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070032
33namespace android {
Igor Murashkine302ee32012-11-05 11:14:49 -080034namespace camera2 {
35namespace tests {
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -070036
37class Camera2Test: public testing::Test {
38 public:
Igor Murashkineab33fc2012-11-06 17:02:54 -080039 void SetUpModule() {
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -070040 int res;
41
42 hw_module_t *module = NULL;
43 res = hw_get_module(CAMERA_HARDWARE_MODULE_ID,
44 (const hw_module_t **)&module);
45
46 ASSERT_EQ(0, res)
47 << "Failure opening camera hardware module: " << res;
48 ASSERT_TRUE(NULL != module)
49 << "No camera module was set by hw_get_module";
50
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070051 IF_ALOGV() {
52 std::cout << " Camera module name: "
53 << module->name << std::endl;
54 std::cout << " Camera module author: "
55 << module->author << std::endl;
56 std::cout << " Camera module API version: 0x" << std::hex
57 << module->module_api_version << std::endl;
58 std::cout << " Camera module HAL API version: 0x" << std::hex
59 << module->hal_api_version << std::endl;
60 }
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -070061
62 int16_t version2_0 = CAMERA_MODULE_API_VERSION_2_0;
Igor Murashkinfb40d5d2013-03-26 18:07:31 -070063 ASSERT_LE(version2_0, module->module_api_version)
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -070064 << "Camera module version is 0x"
65 << std::hex << module->module_api_version
Igor Murashkinfb40d5d2013-03-26 18:07:31 -070066 << ", should be at least 2.0. (0x"
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -070067 << std::hex << CAMERA_MODULE_API_VERSION_2_0 << ")";
68
69 sCameraModule = reinterpret_cast<camera_module_t*>(module);
70
71 sNumCameras = sCameraModule->get_number_of_cameras();
72 ASSERT_LT(0, sNumCameras) << "No camera devices available!";
73
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070074 IF_ALOGV() {
75 std::cout << " Camera device count: " << sNumCameras << std::endl;
76 }
77
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -070078 sCameraSupportsHal2 = new bool[sNumCameras];
79
80 for (int i = 0; i < sNumCameras; i++) {
81 camera_info info;
82 res = sCameraModule->get_camera_info(i, &info);
83 ASSERT_EQ(0, res)
84 << "Failure getting camera info for camera " << i;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070085 IF_ALOGV() {
86 std::cout << " Camera device: " << std::dec
87 << i << std::endl;;
88 std::cout << " Facing: " << std::dec
89 << info.facing << std::endl;
90 std::cout << " Orientation: " << std::dec
91 << info.orientation << std::endl;
92 std::cout << " Version: 0x" << std::hex <<
93 info.device_version << std::endl;
94 }
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070095 if (info.device_version >= CAMERA_DEVICE_API_VERSION_2_0 &&
96 info.device_version < CAMERA_DEVICE_API_VERSION_3_0) {
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -070097 sCameraSupportsHal2[i] = true;
98 ASSERT_TRUE(NULL != info.static_camera_characteristics);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -070099 IF_ALOGV() {
100 std::cout << " Static camera metadata:" << std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700101 dump_indented_camera_metadata(info.static_camera_characteristics,
102 0, 1, 6);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700103 }
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700104 } else {
105 sCameraSupportsHal2[i] = false;
106 }
107 }
108 }
109
Igor Murashkineab33fc2012-11-06 17:02:54 -0800110 void TearDownModule() {
Igor Murashkine302ee32012-11-05 11:14:49 -0800111 hw_module_t *module = reinterpret_cast<hw_module_t*>(sCameraModule);
112 ASSERT_EQ(0, HWModuleHelpers::closeModule(module));
113 }
114
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700115 static const camera_module_t *getCameraModule() {
116 return sCameraModule;
117 }
118
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700119 static int getNumCameras() {
120 return sNumCameras;
121 }
122
123 static bool isHal2Supported(int id) {
124 return sCameraSupportsHal2[id];
125 }
126
127 static camera2_device_t *openCameraDevice(int id) {
128 ALOGV("Opening camera %d", id);
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700129 if (NULL == sCameraSupportsHal2) return NULL;
130 if (id >= sNumCameras) return NULL;
131 if (!sCameraSupportsHal2[id]) return NULL;
132
133 hw_device_t *device = NULL;
134 const camera_module_t *cam_module = getCameraModule();
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700135 if (cam_module == NULL) {
136 return NULL;
137 }
138
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700139 char camId[10];
140 int res;
141
142 snprintf(camId, 10, "%d", id);
143 res = cam_module->common.methods->open(
144 (const hw_module_t*)cam_module,
145 camId,
146 &device);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700147 if (res != NO_ERROR || device == NULL) {
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700148 return NULL;
149 }
150 camera2_device_t *cam_device =
151 reinterpret_cast<camera2_device_t*>(device);
152 return cam_device;
153 }
154
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700155 static status_t configureCameraDevice(camera2_device_t *dev,
156 MetadataQueue &requestQueue,
157 MetadataQueue &frameQueue,
158 NotifierListener &listener) {
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700159
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700160 status_t err;
161
162 err = dev->ops->set_request_queue_src_ops(dev,
163 requestQueue.getToConsumerInterface());
164 if (err != OK) return err;
165
166 requestQueue.setFromConsumerInterface(dev);
167
168 err = dev->ops->set_frame_queue_dst_ops(dev,
169 frameQueue.getToProducerInterface());
170 if (err != OK) return err;
171
172 err = listener.getNotificationsFrom(dev);
173 if (err != OK) return err;
174
175 vendor_tag_query_ops_t *vendor_metadata_tag_ops;
176 err = dev->ops->get_metadata_vendor_tag_ops(dev, &vendor_metadata_tag_ops);
177 if (err != OK) return err;
178
179 err = set_camera_metadata_vendor_tag_ops(vendor_metadata_tag_ops);
180 if (err != OK) return err;
181
182 return OK;
183 }
184
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700185 static status_t closeCameraDevice(camera2_device_t **cam_dev) {
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700186 int res;
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700187 if (*cam_dev == NULL ) return OK;
188
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700189 ALOGV("Closing camera %p", cam_dev);
190
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700191 hw_device_t *dev = reinterpret_cast<hw_device_t *>(*cam_dev);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700192 res = dev->close(dev);
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700193 *cam_dev = NULL;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700194 return res;
195 }
196
197 void setUpCamera(int id) {
198 ASSERT_GT(sNumCameras, id);
199 status_t res;
200
201 if (mDevice != NULL) {
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700202 closeCameraDevice(&mDevice);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700203 }
Zhijun He8ef01442013-08-13 17:36:17 -0700204 mId = id;
205 mDevice = openCameraDevice(mId);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700206 ASSERT_TRUE(NULL != mDevice) << "Failed to open camera device";
207
208 camera_info info;
209 res = sCameraModule->get_camera_info(id, &info);
210 ASSERT_EQ(OK, res);
211
212 mStaticInfo = info.static_camera_characteristics;
213
214 res = configureCameraDevice(mDevice,
215 mRequests,
216 mFrames,
217 mNotifications);
218 ASSERT_EQ(OK, res) << "Failure to configure camera device";
219
220 }
221
Andy McFaddeneda79df2012-12-18 09:50:24 -0800222 void setUpStream(sp<IGraphicBufferProducer> consumer,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700223 int width, int height, int format, int *id) {
224 status_t res;
225
226 StreamAdapter* stream = new StreamAdapter(consumer);
227
228 ALOGV("Creating stream, format 0x%x, %d x %d", format, width, height);
229 res = stream->connectToDevice(mDevice, width, height, format);
230 ASSERT_EQ(NO_ERROR, res) << "Failed to connect to stream: "
231 << strerror(-res);
232 mStreams.push_back(stream);
233
234 *id = stream->getId();
235 }
236
237 void disconnectStream(int id) {
238 status_t res;
239 unsigned int i=0;
240 for (; i < mStreams.size(); i++) {
241 if (mStreams[i]->getId() == id) {
242 res = mStreams[i]->disconnect();
243 ASSERT_EQ(NO_ERROR, res) <<
244 "Failed to disconnect stream " << id;
245 break;
246 }
247 }
248 ASSERT_GT(mStreams.size(), i) << "Stream id not found:" << id;
249 }
250
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700251 void getResolutionList(int32_t format,
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700252 const int32_t **list,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700253 size_t *count) {
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700254 ALOGV("Getting resolutions for format %x", format);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700255 status_t res;
Eino-Ville Talvala2388a2d2012-08-28 14:01:26 -0700256 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700257 camera_metadata_ro_entry_t availableFormats;
258 res = find_camera_metadata_ro_entry(mStaticInfo,
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700259 ANDROID_SCALER_AVAILABLE_FORMATS,
260 &availableFormats);
261 ASSERT_EQ(OK, res);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700262
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700263 uint32_t formatIdx;
264 for (formatIdx=0; formatIdx < availableFormats.count; formatIdx++) {
265 if (availableFormats.data.i32[formatIdx] == format) break;
266 }
267 ASSERT_NE(availableFormats.count, formatIdx)
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700268 << "No support found for format 0x" << std::hex << format;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700269 }
270
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700271 camera_metadata_ro_entry_t availableSizes;
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700272 if (format == HAL_PIXEL_FORMAT_RAW_SENSOR) {
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700273 res = find_camera_metadata_ro_entry(mStaticInfo,
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700274 ANDROID_SCALER_AVAILABLE_RAW_SIZES,
275 &availableSizes);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700276 } else if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700277 res = find_camera_metadata_ro_entry(mStaticInfo,
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700278 ANDROID_SCALER_AVAILABLE_JPEG_SIZES,
279 &availableSizes);
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700280 } else {
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700281 res = find_camera_metadata_ro_entry(mStaticInfo,
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700282 ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES,
283 &availableSizes);
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700284 }
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700285 ASSERT_EQ(OK, res);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700286
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700287 *list = availableSizes.data.i32;
288 *count = availableSizes.count;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700289 }
290
Igor Murashkin599b76f2012-12-18 16:06:07 -0800291 status_t waitUntilDrained() {
292 static const uint32_t kSleepTime = 50000; // 50 ms
293 static const uint32_t kMaxSleepTime = 10000000; // 10 s
294 ALOGV("%s: Camera %d: Starting wait", __FUNCTION__, mId);
295
296 // TODO: Set up notifications from HAL, instead of sleeping here
297 uint32_t totalTime = 0;
298 while (mDevice->ops->get_in_progress_count(mDevice) > 0) {
299 usleep(kSleepTime);
300 totalTime += kSleepTime;
301 if (totalTime > kMaxSleepTime) {
302 ALOGE("%s: Waited %d us, %d requests still in flight", __FUNCTION__,
303 mDevice->ops->get_in_progress_count(mDevice), totalTime);
304 return TIMED_OUT;
305 }
306 }
307 ALOGV("%s: Camera %d: HAL is idle", __FUNCTION__, mId);
308 return OK;
309 }
310
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700311 virtual void SetUp() {
Igor Murashkineab33fc2012-11-06 17:02:54 -0800312 TEST_EXTENSION_FORKING_SET_UP;
313
314 SetUpModule();
315
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700316 const ::testing::TestInfo* const testInfo =
317 ::testing::UnitTest::GetInstance()->current_test_info();
Igor Murashkineab33fc2012-11-06 17:02:54 -0800318 (void)testInfo;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700319
Igor Murashkineab33fc2012-11-06 17:02:54 -0800320 ALOGV("*** Starting test %s in test case %s", testInfo->name(),
321 testInfo->test_case_name());
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700322 mDevice = NULL;
323 }
324
325 virtual void TearDown() {
Igor Murashkineab33fc2012-11-06 17:02:54 -0800326 TEST_EXTENSION_FORKING_TEAR_DOWN;
327
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700328 for (unsigned int i = 0; i < mStreams.size(); i++) {
329 delete mStreams[i];
330 }
331 if (mDevice != NULL) {
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700332 closeCameraDevice(&mDevice);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700333 }
Igor Murashkineab33fc2012-11-06 17:02:54 -0800334
335 TearDownModule();
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700336 }
337
Zhijun He8ef01442013-08-13 17:36:17 -0700338 int mId;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700339 camera2_device *mDevice;
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700340 const camera_metadata_t *mStaticInfo;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700341
342 MetadataQueue mRequests;
343 MetadataQueue mFrames;
344 NotifierListener mNotifications;
345
346 Vector<StreamAdapter*> mStreams;
347
348 private:
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700349 static camera_module_t *sCameraModule;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700350 static int sNumCameras;
351 static bool *sCameraSupportsHal2;
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700352};
353
354camera_module_t *Camera2Test::sCameraModule = NULL;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700355bool *Camera2Test::sCameraSupportsHal2 = NULL;
356int Camera2Test::sNumCameras = 0;
357
358static const nsecs_t USEC = 1000;
359static const nsecs_t MSEC = 1000*USEC;
360static const nsecs_t SEC = 1000*MSEC;
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700361
362
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700363TEST_F(Camera2Test, OpenClose) {
Igor Murashkineab33fc2012-11-06 17:02:54 -0800364
365 TEST_EXTENSION_FORKING_INIT;
366
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700367 status_t res;
368
369 for (int id = 0; id < getNumCameras(); id++) {
370 if (!isHal2Supported(id)) continue;
371
372 camera2_device_t *d = openCameraDevice(id);
373 ASSERT_TRUE(NULL != d) << "Failed to open camera device";
374
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700375 res = closeCameraDevice(&d);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700376 ASSERT_EQ(NO_ERROR, res) << "Failed to close camera device";
377 }
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700378}
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700379
380TEST_F(Camera2Test, Capture1Raw) {
Igor Murashkineab33fc2012-11-06 17:02:54 -0800381
382 TEST_EXTENSION_FORKING_INIT;
383
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700384 status_t res;
385
386 for (int id = 0; id < getNumCameras(); id++) {
387 if (!isHal2Supported(id)) continue;
388
389 ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
390
Ying Wang1973f4a2013-07-15 22:18:04 -0700391 sp<BufferQueue> bq = new BufferQueue();
392 sp<CpuConsumer> rawConsumer = new CpuConsumer(bq, 1);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700393 sp<FrameWaiter> rawWaiter = new FrameWaiter();
394 rawConsumer->setFrameAvailableListener(rawWaiter);
395
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700396 const int32_t *rawResolutions;
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700397 size_t rawResolutionsCount;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700398
399 int format = HAL_PIXEL_FORMAT_RAW_SENSOR;
400
401 getResolutionList(format,
402 &rawResolutions, &rawResolutionsCount);
Igor Murashkin3d991c82013-04-18 10:09:16 -0700403
404 if (rawResolutionsCount <= 0) {
405 const ::testing::TestInfo* const test_info =
406 ::testing::UnitTest::GetInstance()->current_test_info();
407 std::cerr << "Skipping test "
408 << test_info->test_case_name() << "."
409 << test_info->name()
410 << " because the optional format was not available: "
411 << "RAW_SENSOR" << std::endl;
412 return;
413 }
414
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700415 ASSERT_LT((size_t)0, rawResolutionsCount);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700416
417 // Pick first available raw resolution
418 int width = rawResolutions[0];
419 int height = rawResolutions[1];
420
421 int streamId;
422 ASSERT_NO_FATAL_FAILURE(
Mathias Agopian3321f1b2013-08-06 14:22:12 -0700423 setUpStream(bq, width, height, format, &streamId) );
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700424
425 camera_metadata_t *request;
426 request = allocate_camera_metadata(20, 2000);
427
Igor Murashkin09ad0a32012-12-03 13:33:08 -0800428 uint8_t metadataMode = ANDROID_REQUEST_METADATA_MODE_FULL;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700429 add_camera_metadata_entry(request,
430 ANDROID_REQUEST_METADATA_MODE,
431 (void**)&metadataMode, 1);
432 uint32_t outputStreams = streamId;
433 add_camera_metadata_entry(request,
434 ANDROID_REQUEST_OUTPUT_STREAMS,
435 (void**)&outputStreams, 1);
436
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700437 uint64_t exposureTime = 10*MSEC;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700438 add_camera_metadata_entry(request,
439 ANDROID_SENSOR_EXPOSURE_TIME,
440 (void**)&exposureTime, 1);
441 uint64_t frameDuration = 30*MSEC;
442 add_camera_metadata_entry(request,
443 ANDROID_SENSOR_FRAME_DURATION,
444 (void**)&frameDuration, 1);
445 uint32_t sensitivity = 100;
446 add_camera_metadata_entry(request,
447 ANDROID_SENSOR_SENSITIVITY,
448 (void**)&sensitivity, 1);
Igor Murashkin599b76f2012-12-18 16:06:07 -0800449 uint8_t requestType = ANDROID_REQUEST_TYPE_CAPTURE;
450 add_camera_metadata_entry(request,
451 ANDROID_REQUEST_TYPE,
452 (void**)&requestType, 1);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700453
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700454 uint32_t hourOfDay = 12;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700455 add_camera_metadata_entry(request,
456 0x80000000, // EMULATOR_HOUROFDAY
457 &hourOfDay, 1);
458
459 IF_ALOGV() {
460 std::cout << "Input request: " << std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700461 dump_indented_camera_metadata(request, 0, 1, 2);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700462 }
463
464 res = mRequests.enqueue(request);
465 ASSERT_EQ(NO_ERROR, res) << "Can't enqueue request: " << strerror(-res);
466
467 res = mFrames.waitForBuffer(exposureTime + SEC);
468 ASSERT_EQ(NO_ERROR, res) << "No frame to get: " << strerror(-res);
469
470 camera_metadata_t *frame;
471 res = mFrames.dequeue(&frame);
472 ASSERT_EQ(NO_ERROR, res);
473 ASSERT_TRUE(frame != NULL);
474
475 IF_ALOGV() {
476 std::cout << "Output frame:" << std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700477 dump_indented_camera_metadata(frame, 0, 1, 2);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700478 }
479
480 res = rawWaiter->waitForFrame(exposureTime + SEC);
481 ASSERT_EQ(NO_ERROR, res);
482
483 CpuConsumer::LockedBuffer buffer;
484 res = rawConsumer->lockNextBuffer(&buffer);
485 ASSERT_EQ(NO_ERROR, res);
486
487 IF_ALOGV() {
488 const char *dumpname =
489 "/data/local/tmp/camera2_test-capture1raw-dump.raw";
490 ALOGV("Dumping raw buffer to %s", dumpname);
491 // Write to file
492 std::ofstream rawFile(dumpname);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700493 size_t bpp = 2;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700494 for (unsigned int y = 0; y < buffer.height; y++) {
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700495 rawFile.write(
496 (const char *)(buffer.data + y * buffer.stride * bpp),
497 buffer.width * bpp);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700498 }
499 rawFile.close();
500 }
501
502 res = rawConsumer->unlockBuffer(buffer);
503 ASSERT_EQ(NO_ERROR, res);
504
Igor Murashkin599b76f2012-12-18 16:06:07 -0800505 ASSERT_EQ(OK, waitUntilDrained());
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700506 ASSERT_NO_FATAL_FAILURE(disconnectStream(streamId));
507
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700508 res = closeCameraDevice(&mDevice);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700509 ASSERT_EQ(NO_ERROR, res) << "Failed to close camera device";
510
511 }
512}
513
514TEST_F(Camera2Test, CaptureBurstRaw) {
Igor Murashkineab33fc2012-11-06 17:02:54 -0800515
516 TEST_EXTENSION_FORKING_INIT;
517
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700518 status_t res;
519
520 for (int id = 0; id < getNumCameras(); id++) {
521 if (!isHal2Supported(id)) continue;
522
523 ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
524
Ying Wang1973f4a2013-07-15 22:18:04 -0700525 sp<BufferQueue> bq = new BufferQueue();
526 sp<CpuConsumer> rawConsumer = new CpuConsumer(bq, 1);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700527 sp<FrameWaiter> rawWaiter = new FrameWaiter();
528 rawConsumer->setFrameAvailableListener(rawWaiter);
529
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700530 const int32_t *rawResolutions;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700531 size_t rawResolutionsCount;
532
533 int format = HAL_PIXEL_FORMAT_RAW_SENSOR;
534
535 getResolutionList(format,
536 &rawResolutions, &rawResolutionsCount);
Igor Murashkin3d991c82013-04-18 10:09:16 -0700537
538 if (rawResolutionsCount <= 0) {
539 const ::testing::TestInfo* const test_info =
540 ::testing::UnitTest::GetInstance()->current_test_info();
541 std::cerr << "Skipping test "
542 << test_info->test_case_name() << "."
543 << test_info->name()
544 << " because the optional format was not available: "
545 << "RAW_SENSOR" << std::endl;
546 return;
547 }
548
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700549 ASSERT_LT((uint32_t)0, rawResolutionsCount);
550
551 // Pick first available raw resolution
552 int width = rawResolutions[0];
553 int height = rawResolutions[1];
554
555 int streamId;
556 ASSERT_NO_FATAL_FAILURE(
Mathias Agopian3321f1b2013-08-06 14:22:12 -0700557 setUpStream(bq, width, height, format, &streamId) );
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700558
559 camera_metadata_t *request;
560 request = allocate_camera_metadata(20, 2000);
561
Igor Murashkin09ad0a32012-12-03 13:33:08 -0800562 uint8_t metadataMode = ANDROID_REQUEST_METADATA_MODE_FULL;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700563 add_camera_metadata_entry(request,
564 ANDROID_REQUEST_METADATA_MODE,
565 (void**)&metadataMode, 1);
566 uint32_t outputStreams = streamId;
567 add_camera_metadata_entry(request,
568 ANDROID_REQUEST_OUTPUT_STREAMS,
569 (void**)&outputStreams, 1);
570
571 uint64_t frameDuration = 30*MSEC;
572 add_camera_metadata_entry(request,
573 ANDROID_SENSOR_FRAME_DURATION,
574 (void**)&frameDuration, 1);
575 uint32_t sensitivity = 100;
576 add_camera_metadata_entry(request,
577 ANDROID_SENSOR_SENSITIVITY,
578 (void**)&sensitivity, 1);
Igor Murashkin599b76f2012-12-18 16:06:07 -0800579 uint8_t requestType = ANDROID_REQUEST_TYPE_CAPTURE;
580 add_camera_metadata_entry(request,
581 ANDROID_REQUEST_TYPE,
582 (void**)&requestType, 1);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700583
584 uint32_t hourOfDay = 12;
585 add_camera_metadata_entry(request,
586 0x80000000, // EMULATOR_HOUROFDAY
587 &hourOfDay, 1);
588
589 IF_ALOGV() {
590 std::cout << "Input request template: " << std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700591 dump_indented_camera_metadata(request, 0, 1, 2);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700592 }
593
594 int numCaptures = 10;
595
596 // Enqueue numCaptures requests with increasing exposure time
597
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700598 uint64_t exposureTime = 100 * USEC;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700599 for (int reqCount = 0; reqCount < numCaptures; reqCount++ ) {
600 camera_metadata_t *req;
601 req = allocate_camera_metadata(20, 2000);
602 append_camera_metadata(req, request);
603
604 add_camera_metadata_entry(req,
605 ANDROID_SENSOR_EXPOSURE_TIME,
606 (void**)&exposureTime, 1);
607 exposureTime *= 2;
608
609 res = mRequests.enqueue(req);
610 ASSERT_EQ(NO_ERROR, res) << "Can't enqueue request: "
611 << strerror(-res);
612 }
613
614 // Get frames and image buffers one by one
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700615 uint64_t expectedExposureTime = 100 * USEC;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700616 for (int frameCount = 0; frameCount < 10; frameCount++) {
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700617 res = mFrames.waitForBuffer(SEC + expectedExposureTime);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700618 ASSERT_EQ(NO_ERROR, res) << "No frame to get: " << strerror(-res);
619
620 camera_metadata_t *frame;
621 res = mFrames.dequeue(&frame);
622 ASSERT_EQ(NO_ERROR, res);
623 ASSERT_TRUE(frame != NULL);
624
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700625 camera_metadata_entry_t frameNumber;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700626 res = find_camera_metadata_entry(frame,
627 ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700628 &frameNumber);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700629 ASSERT_EQ(NO_ERROR, res);
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700630 ASSERT_EQ(frameCount, *frameNumber.data.i32);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700631
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700632 res = rawWaiter->waitForFrame(SEC + expectedExposureTime);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700633 ASSERT_EQ(NO_ERROR, res) <<
634 "Never got raw data for capture " << frameCount;
635
636 CpuConsumer::LockedBuffer buffer;
637 res = rawConsumer->lockNextBuffer(&buffer);
638 ASSERT_EQ(NO_ERROR, res);
639
640 IF_ALOGV() {
641 char dumpname[60];
642 snprintf(dumpname, 60,
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700643 "/data/local/tmp/camera2_test-"
644 "captureBurstRaw-dump_%d.raw",
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700645 frameCount);
646 ALOGV("Dumping raw buffer to %s", dumpname);
647 // Write to file
648 std::ofstream rawFile(dumpname);
649 for (unsigned int y = 0; y < buffer.height; y++) {
650 rawFile.write(
651 (const char *)(buffer.data + y * buffer.stride * 2),
652 buffer.width * 2);
653 }
654 rawFile.close();
655 }
656
657 res = rawConsumer->unlockBuffer(buffer);
658 ASSERT_EQ(NO_ERROR, res);
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700659
660 expectedExposureTime *= 2;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700661 }
662 }
663}
664
Eino-Ville Talvala6adfd6b2012-05-14 15:25:27 -0700665TEST_F(Camera2Test, ConstructDefaultRequests) {
Igor Murashkineab33fc2012-11-06 17:02:54 -0800666
667 TEST_EXTENSION_FORKING_INIT;
668
Eino-Ville Talvala6adfd6b2012-05-14 15:25:27 -0700669 status_t res;
670
671 for (int id = 0; id < getNumCameras(); id++) {
672 if (!isHal2Supported(id)) continue;
673
674 ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
675
676 for (int i = CAMERA2_TEMPLATE_PREVIEW; i < CAMERA2_TEMPLATE_COUNT;
677 i++) {
678 camera_metadata_t *request = NULL;
679 res = mDevice->ops->construct_default_request(mDevice,
680 i,
681 &request);
682 EXPECT_EQ(NO_ERROR, res) <<
Igor Murashkin3d991c82013-04-18 10:09:16 -0700683 "Unable to construct request from template type " << i;
Eino-Ville Talvala6adfd6b2012-05-14 15:25:27 -0700684 EXPECT_TRUE(request != NULL);
685 EXPECT_LT((size_t)0, get_camera_metadata_entry_count(request));
686 EXPECT_LT((size_t)0, get_camera_metadata_data_count(request));
687
688 IF_ALOGV() {
689 std::cout << " ** Template type " << i << ":"<<std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700690 dump_indented_camera_metadata(request, 0, 2, 4);
Eino-Ville Talvala6adfd6b2012-05-14 15:25:27 -0700691 }
692
693 free_camera_metadata(request);
694 }
695 }
696}
697
Igor Murashkineab33fc2012-11-06 17:02:54 -0800698TEST_F(Camera2Test, Capture1Jpeg) {
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700699 status_t res;
700
701 for (int id = 0; id < getNumCameras(); id++) {
702 if (!isHal2Supported(id)) continue;
703
704 ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
705
Ying Wang1973f4a2013-07-15 22:18:04 -0700706 sp<BufferQueue> bq = new BufferQueue();
707 sp<CpuConsumer> jpegConsumer = new CpuConsumer(bq, 1);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700708 sp<FrameWaiter> jpegWaiter = new FrameWaiter();
709 jpegConsumer->setFrameAvailableListener(jpegWaiter);
710
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700711 const int32_t *jpegResolutions;
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700712 size_t jpegResolutionsCount;
713
714 int format = HAL_PIXEL_FORMAT_BLOB;
715
716 getResolutionList(format,
717 &jpegResolutions, &jpegResolutionsCount);
718 ASSERT_LT((size_t)0, jpegResolutionsCount);
719
720 // Pick first available JPEG resolution
721 int width = jpegResolutions[0];
722 int height = jpegResolutions[1];
723
724 int streamId;
725 ASSERT_NO_FATAL_FAILURE(
Mathias Agopian3321f1b2013-08-06 14:22:12 -0700726 setUpStream(bq, width, height, format, &streamId) );
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700727
728 camera_metadata_t *request;
729 request = allocate_camera_metadata(20, 2000);
730
Igor Murashkin09ad0a32012-12-03 13:33:08 -0800731 uint8_t metadataMode = ANDROID_REQUEST_METADATA_MODE_FULL;
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700732 add_camera_metadata_entry(request,
733 ANDROID_REQUEST_METADATA_MODE,
734 (void**)&metadataMode, 1);
735 uint32_t outputStreams = streamId;
736 add_camera_metadata_entry(request,
737 ANDROID_REQUEST_OUTPUT_STREAMS,
738 (void**)&outputStreams, 1);
739
740 uint64_t exposureTime = 10*MSEC;
741 add_camera_metadata_entry(request,
742 ANDROID_SENSOR_EXPOSURE_TIME,
743 (void**)&exposureTime, 1);
744 uint64_t frameDuration = 30*MSEC;
745 add_camera_metadata_entry(request,
746 ANDROID_SENSOR_FRAME_DURATION,
747 (void**)&frameDuration, 1);
748 uint32_t sensitivity = 100;
749 add_camera_metadata_entry(request,
750 ANDROID_SENSOR_SENSITIVITY,
751 (void**)&sensitivity, 1);
Igor Murashkin599b76f2012-12-18 16:06:07 -0800752 uint8_t requestType = ANDROID_REQUEST_TYPE_CAPTURE;
753 add_camera_metadata_entry(request,
754 ANDROID_REQUEST_TYPE,
755 (void**)&requestType, 1);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700756
757 uint32_t hourOfDay = 12;
758 add_camera_metadata_entry(request,
759 0x80000000, // EMULATOR_HOUROFDAY
760 &hourOfDay, 1);
761
762 IF_ALOGV() {
763 std::cout << "Input request: " << std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700764 dump_indented_camera_metadata(request, 0, 1, 4);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700765 }
766
767 res = mRequests.enqueue(request);
768 ASSERT_EQ(NO_ERROR, res) << "Can't enqueue request: " << strerror(-res);
769
770 res = mFrames.waitForBuffer(exposureTime + SEC);
771 ASSERT_EQ(NO_ERROR, res) << "No frame to get: " << strerror(-res);
772
773 camera_metadata_t *frame;
774 res = mFrames.dequeue(&frame);
775 ASSERT_EQ(NO_ERROR, res);
776 ASSERT_TRUE(frame != NULL);
777
778 IF_ALOGV() {
779 std::cout << "Output frame:" << std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700780 dump_indented_camera_metadata(frame, 0, 1, 4);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700781 }
782
783 res = jpegWaiter->waitForFrame(exposureTime + SEC);
784 ASSERT_EQ(NO_ERROR, res);
785
786 CpuConsumer::LockedBuffer buffer;
787 res = jpegConsumer->lockNextBuffer(&buffer);
788 ASSERT_EQ(NO_ERROR, res);
789
790 IF_ALOGV() {
791 const char *dumpname =
792 "/data/local/tmp/camera2_test-capture1jpeg-dump.jpeg";
793 ALOGV("Dumping raw buffer to %s", dumpname);
794 // Write to file
795 std::ofstream jpegFile(dumpname);
796 size_t bpp = 1;
797 for (unsigned int y = 0; y < buffer.height; y++) {
798 jpegFile.write(
799 (const char *)(buffer.data + y * buffer.stride * bpp),
800 buffer.width * bpp);
801 }
802 jpegFile.close();
803 }
804
805 res = jpegConsumer->unlockBuffer(buffer);
806 ASSERT_EQ(NO_ERROR, res);
807
Igor Murashkin599b76f2012-12-18 16:06:07 -0800808 ASSERT_EQ(OK, waitUntilDrained());
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700809 ASSERT_NO_FATAL_FAILURE(disconnectStream(streamId));
810
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700811 res = closeCameraDevice(&mDevice);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700812 ASSERT_EQ(NO_ERROR, res) << "Failed to close camera device";
813
814 }
815}
816
Igor Murashkine302ee32012-11-05 11:14:49 -0800817} // namespace tests
818} // namespace camera2
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700819} // namespace android