blob: 73f8c614166695f144178fe53858d193957a0ed0 [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
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700175 return OK;
176 }
177
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700178 static status_t closeCameraDevice(camera2_device_t **cam_dev) {
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700179 int res;
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700180 if (*cam_dev == NULL ) return OK;
181
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700182 ALOGV("Closing camera %p", cam_dev);
183
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700184 hw_device_t *dev = reinterpret_cast<hw_device_t *>(*cam_dev);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700185 res = dev->close(dev);
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700186 *cam_dev = NULL;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700187 return res;
188 }
189
190 void setUpCamera(int id) {
191 ASSERT_GT(sNumCameras, id);
192 status_t res;
193
194 if (mDevice != NULL) {
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700195 closeCameraDevice(&mDevice);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700196 }
Zhijun He8ef01442013-08-13 17:36:17 -0700197 mId = id;
198 mDevice = openCameraDevice(mId);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700199 ASSERT_TRUE(NULL != mDevice) << "Failed to open camera device";
200
201 camera_info info;
202 res = sCameraModule->get_camera_info(id, &info);
203 ASSERT_EQ(OK, res);
204
205 mStaticInfo = info.static_camera_characteristics;
206
207 res = configureCameraDevice(mDevice,
208 mRequests,
209 mFrames,
210 mNotifications);
211 ASSERT_EQ(OK, res) << "Failure to configure camera device";
212
213 }
214
Andy McFaddeneda79df2012-12-18 09:50:24 -0800215 void setUpStream(sp<IGraphicBufferProducer> consumer,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700216 int width, int height, int format, int *id) {
217 status_t res;
218
219 StreamAdapter* stream = new StreamAdapter(consumer);
220
221 ALOGV("Creating stream, format 0x%x, %d x %d", format, width, height);
222 res = stream->connectToDevice(mDevice, width, height, format);
223 ASSERT_EQ(NO_ERROR, res) << "Failed to connect to stream: "
224 << strerror(-res);
225 mStreams.push_back(stream);
226
227 *id = stream->getId();
228 }
229
230 void disconnectStream(int id) {
231 status_t res;
232 unsigned int i=0;
233 for (; i < mStreams.size(); i++) {
234 if (mStreams[i]->getId() == id) {
235 res = mStreams[i]->disconnect();
236 ASSERT_EQ(NO_ERROR, res) <<
237 "Failed to disconnect stream " << id;
238 break;
239 }
240 }
241 ASSERT_GT(mStreams.size(), i) << "Stream id not found:" << id;
242 }
243
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700244 void getResolutionList(int32_t format,
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700245 const int32_t **list,
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700246 size_t *count) {
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700247 ALOGV("Getting resolutions for format %x", format);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700248 status_t res;
Eino-Ville Talvala2388a2d2012-08-28 14:01:26 -0700249 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700250 camera_metadata_ro_entry_t availableFormats;
251 res = find_camera_metadata_ro_entry(mStaticInfo,
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700252 ANDROID_SCALER_AVAILABLE_FORMATS,
253 &availableFormats);
254 ASSERT_EQ(OK, res);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700255
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700256 uint32_t formatIdx;
257 for (formatIdx=0; formatIdx < availableFormats.count; formatIdx++) {
258 if (availableFormats.data.i32[formatIdx] == format) break;
259 }
260 ASSERT_NE(availableFormats.count, formatIdx)
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700261 << "No support found for format 0x" << std::hex << format;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700262 }
263
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700264 camera_metadata_ro_entry_t availableSizes;
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700265 if (format == HAL_PIXEL_FORMAT_RAW_SENSOR) {
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700266 res = find_camera_metadata_ro_entry(mStaticInfo,
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700267 ANDROID_SCALER_AVAILABLE_RAW_SIZES,
268 &availableSizes);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700269 } else if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700270 res = find_camera_metadata_ro_entry(mStaticInfo,
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700271 ANDROID_SCALER_AVAILABLE_JPEG_SIZES,
272 &availableSizes);
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700273 } else {
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700274 res = find_camera_metadata_ro_entry(mStaticInfo,
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700275 ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES,
276 &availableSizes);
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700277 }
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700278 ASSERT_EQ(OK, res);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700279
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700280 *list = availableSizes.data.i32;
281 *count = availableSizes.count;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700282 }
283
Igor Murashkin599b76f2012-12-18 16:06:07 -0800284 status_t waitUntilDrained() {
285 static const uint32_t kSleepTime = 50000; // 50 ms
286 static const uint32_t kMaxSleepTime = 10000000; // 10 s
287 ALOGV("%s: Camera %d: Starting wait", __FUNCTION__, mId);
288
289 // TODO: Set up notifications from HAL, instead of sleeping here
290 uint32_t totalTime = 0;
291 while (mDevice->ops->get_in_progress_count(mDevice) > 0) {
292 usleep(kSleepTime);
293 totalTime += kSleepTime;
294 if (totalTime > kMaxSleepTime) {
295 ALOGE("%s: Waited %d us, %d requests still in flight", __FUNCTION__,
296 mDevice->ops->get_in_progress_count(mDevice), totalTime);
297 return TIMED_OUT;
298 }
299 }
300 ALOGV("%s: Camera %d: HAL is idle", __FUNCTION__, mId);
301 return OK;
302 }
303
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700304 virtual void SetUp() {
Igor Murashkineab33fc2012-11-06 17:02:54 -0800305 TEST_EXTENSION_FORKING_SET_UP;
306
307 SetUpModule();
308
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700309 const ::testing::TestInfo* const testInfo =
310 ::testing::UnitTest::GetInstance()->current_test_info();
Igor Murashkineab33fc2012-11-06 17:02:54 -0800311 (void)testInfo;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700312
Igor Murashkineab33fc2012-11-06 17:02:54 -0800313 ALOGV("*** Starting test %s in test case %s", testInfo->name(),
314 testInfo->test_case_name());
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700315 mDevice = NULL;
316 }
317
318 virtual void TearDown() {
Igor Murashkineab33fc2012-11-06 17:02:54 -0800319 TEST_EXTENSION_FORKING_TEAR_DOWN;
320
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700321 for (unsigned int i = 0; i < mStreams.size(); i++) {
322 delete mStreams[i];
323 }
324 if (mDevice != NULL) {
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700325 closeCameraDevice(&mDevice);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700326 }
Igor Murashkineab33fc2012-11-06 17:02:54 -0800327
328 TearDownModule();
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700329 }
330
Zhijun He8ef01442013-08-13 17:36:17 -0700331 int mId;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700332 camera2_device *mDevice;
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700333 const camera_metadata_t *mStaticInfo;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700334
335 MetadataQueue mRequests;
336 MetadataQueue mFrames;
337 NotifierListener mNotifications;
338
339 Vector<StreamAdapter*> mStreams;
340
341 private:
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700342 static camera_module_t *sCameraModule;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700343 static int sNumCameras;
344 static bool *sCameraSupportsHal2;
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700345};
346
347camera_module_t *Camera2Test::sCameraModule = NULL;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700348bool *Camera2Test::sCameraSupportsHal2 = NULL;
349int Camera2Test::sNumCameras = 0;
350
351static const nsecs_t USEC = 1000;
352static const nsecs_t MSEC = 1000*USEC;
353static const nsecs_t SEC = 1000*MSEC;
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700354
355
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700356TEST_F(Camera2Test, OpenClose) {
Igor Murashkineab33fc2012-11-06 17:02:54 -0800357
358 TEST_EXTENSION_FORKING_INIT;
359
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700360 status_t res;
361
362 for (int id = 0; id < getNumCameras(); id++) {
363 if (!isHal2Supported(id)) continue;
364
365 camera2_device_t *d = openCameraDevice(id);
366 ASSERT_TRUE(NULL != d) << "Failed to open camera device";
367
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700368 res = closeCameraDevice(&d);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700369 ASSERT_EQ(NO_ERROR, res) << "Failed to close camera device";
370 }
Eino-Ville Talvalafed0c022012-03-22 13:11:05 -0700371}
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700372
373TEST_F(Camera2Test, Capture1Raw) {
Igor Murashkineab33fc2012-11-06 17:02:54 -0800374
375 TEST_EXTENSION_FORKING_INIT;
376
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700377 status_t res;
378
379 for (int id = 0; id < getNumCameras(); id++) {
380 if (!isHal2Supported(id)) continue;
381
382 ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
383
Ying Wang1973f4a2013-07-15 22:18:04 -0700384 sp<BufferQueue> bq = new BufferQueue();
385 sp<CpuConsumer> rawConsumer = new CpuConsumer(bq, 1);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700386 sp<FrameWaiter> rawWaiter = new FrameWaiter();
387 rawConsumer->setFrameAvailableListener(rawWaiter);
388
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700389 const int32_t *rawResolutions;
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700390 size_t rawResolutionsCount;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700391
392 int format = HAL_PIXEL_FORMAT_RAW_SENSOR;
393
394 getResolutionList(format,
395 &rawResolutions, &rawResolutionsCount);
Igor Murashkin3d991c82013-04-18 10:09:16 -0700396
397 if (rawResolutionsCount <= 0) {
398 const ::testing::TestInfo* const test_info =
399 ::testing::UnitTest::GetInstance()->current_test_info();
400 std::cerr << "Skipping test "
401 << test_info->test_case_name() << "."
402 << test_info->name()
403 << " because the optional format was not available: "
404 << "RAW_SENSOR" << std::endl;
405 return;
406 }
407
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700408 ASSERT_LT((size_t)0, rawResolutionsCount);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700409
410 // Pick first available raw resolution
411 int width = rawResolutions[0];
412 int height = rawResolutions[1];
413
414 int streamId;
415 ASSERT_NO_FATAL_FAILURE(
Mathias Agopian3321f1b2013-08-06 14:22:12 -0700416 setUpStream(bq, width, height, format, &streamId) );
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700417
418 camera_metadata_t *request;
419 request = allocate_camera_metadata(20, 2000);
420
Igor Murashkin09ad0a32012-12-03 13:33:08 -0800421 uint8_t metadataMode = ANDROID_REQUEST_METADATA_MODE_FULL;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700422 add_camera_metadata_entry(request,
423 ANDROID_REQUEST_METADATA_MODE,
424 (void**)&metadataMode, 1);
425 uint32_t outputStreams = streamId;
426 add_camera_metadata_entry(request,
427 ANDROID_REQUEST_OUTPUT_STREAMS,
428 (void**)&outputStreams, 1);
429
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700430 uint64_t exposureTime = 10*MSEC;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700431 add_camera_metadata_entry(request,
432 ANDROID_SENSOR_EXPOSURE_TIME,
433 (void**)&exposureTime, 1);
434 uint64_t frameDuration = 30*MSEC;
435 add_camera_metadata_entry(request,
436 ANDROID_SENSOR_FRAME_DURATION,
437 (void**)&frameDuration, 1);
438 uint32_t sensitivity = 100;
439 add_camera_metadata_entry(request,
440 ANDROID_SENSOR_SENSITIVITY,
441 (void**)&sensitivity, 1);
Igor Murashkin599b76f2012-12-18 16:06:07 -0800442 uint8_t requestType = ANDROID_REQUEST_TYPE_CAPTURE;
443 add_camera_metadata_entry(request,
444 ANDROID_REQUEST_TYPE,
445 (void**)&requestType, 1);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700446
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700447 uint32_t hourOfDay = 12;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700448 add_camera_metadata_entry(request,
449 0x80000000, // EMULATOR_HOUROFDAY
450 &hourOfDay, 1);
451
452 IF_ALOGV() {
453 std::cout << "Input request: " << std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700454 dump_indented_camera_metadata(request, 0, 1, 2);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700455 }
456
457 res = mRequests.enqueue(request);
458 ASSERT_EQ(NO_ERROR, res) << "Can't enqueue request: " << strerror(-res);
459
460 res = mFrames.waitForBuffer(exposureTime + SEC);
461 ASSERT_EQ(NO_ERROR, res) << "No frame to get: " << strerror(-res);
462
463 camera_metadata_t *frame;
464 res = mFrames.dequeue(&frame);
465 ASSERT_EQ(NO_ERROR, res);
466 ASSERT_TRUE(frame != NULL);
467
468 IF_ALOGV() {
469 std::cout << "Output frame:" << std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700470 dump_indented_camera_metadata(frame, 0, 1, 2);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700471 }
472
473 res = rawWaiter->waitForFrame(exposureTime + SEC);
474 ASSERT_EQ(NO_ERROR, res);
475
476 CpuConsumer::LockedBuffer buffer;
477 res = rawConsumer->lockNextBuffer(&buffer);
478 ASSERT_EQ(NO_ERROR, res);
479
480 IF_ALOGV() {
481 const char *dumpname =
482 "/data/local/tmp/camera2_test-capture1raw-dump.raw";
483 ALOGV("Dumping raw buffer to %s", dumpname);
484 // Write to file
485 std::ofstream rawFile(dumpname);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700486 size_t bpp = 2;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700487 for (unsigned int y = 0; y < buffer.height; y++) {
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700488 rawFile.write(
489 (const char *)(buffer.data + y * buffer.stride * bpp),
490 buffer.width * bpp);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700491 }
492 rawFile.close();
493 }
494
495 res = rawConsumer->unlockBuffer(buffer);
496 ASSERT_EQ(NO_ERROR, res);
497
Igor Murashkin599b76f2012-12-18 16:06:07 -0800498 ASSERT_EQ(OK, waitUntilDrained());
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700499 ASSERT_NO_FATAL_FAILURE(disconnectStream(streamId));
500
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700501 res = closeCameraDevice(&mDevice);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700502 ASSERT_EQ(NO_ERROR, res) << "Failed to close camera device";
503
504 }
505}
506
507TEST_F(Camera2Test, CaptureBurstRaw) {
Igor Murashkineab33fc2012-11-06 17:02:54 -0800508
509 TEST_EXTENSION_FORKING_INIT;
510
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700511 status_t res;
512
513 for (int id = 0; id < getNumCameras(); id++) {
514 if (!isHal2Supported(id)) continue;
515
516 ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
517
Ying Wang1973f4a2013-07-15 22:18:04 -0700518 sp<BufferQueue> bq = new BufferQueue();
519 sp<CpuConsumer> rawConsumer = new CpuConsumer(bq, 1);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700520 sp<FrameWaiter> rawWaiter = new FrameWaiter();
521 rawConsumer->setFrameAvailableListener(rawWaiter);
522
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700523 const int32_t *rawResolutions;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700524 size_t rawResolutionsCount;
525
526 int format = HAL_PIXEL_FORMAT_RAW_SENSOR;
527
528 getResolutionList(format,
529 &rawResolutions, &rawResolutionsCount);
Igor Murashkin3d991c82013-04-18 10:09:16 -0700530
531 if (rawResolutionsCount <= 0) {
532 const ::testing::TestInfo* const test_info =
533 ::testing::UnitTest::GetInstance()->current_test_info();
534 std::cerr << "Skipping test "
535 << test_info->test_case_name() << "."
536 << test_info->name()
537 << " because the optional format was not available: "
538 << "RAW_SENSOR" << std::endl;
539 return;
540 }
541
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700542 ASSERT_LT((uint32_t)0, rawResolutionsCount);
543
544 // Pick first available raw resolution
545 int width = rawResolutions[0];
546 int height = rawResolutions[1];
547
548 int streamId;
549 ASSERT_NO_FATAL_FAILURE(
Mathias Agopian3321f1b2013-08-06 14:22:12 -0700550 setUpStream(bq, width, height, format, &streamId) );
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700551
552 camera_metadata_t *request;
553 request = allocate_camera_metadata(20, 2000);
554
Igor Murashkin09ad0a32012-12-03 13:33:08 -0800555 uint8_t metadataMode = ANDROID_REQUEST_METADATA_MODE_FULL;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700556 add_camera_metadata_entry(request,
557 ANDROID_REQUEST_METADATA_MODE,
558 (void**)&metadataMode, 1);
559 uint32_t outputStreams = streamId;
560 add_camera_metadata_entry(request,
561 ANDROID_REQUEST_OUTPUT_STREAMS,
562 (void**)&outputStreams, 1);
563
564 uint64_t frameDuration = 30*MSEC;
565 add_camera_metadata_entry(request,
566 ANDROID_SENSOR_FRAME_DURATION,
567 (void**)&frameDuration, 1);
568 uint32_t sensitivity = 100;
569 add_camera_metadata_entry(request,
570 ANDROID_SENSOR_SENSITIVITY,
571 (void**)&sensitivity, 1);
Igor Murashkin599b76f2012-12-18 16:06:07 -0800572 uint8_t requestType = ANDROID_REQUEST_TYPE_CAPTURE;
573 add_camera_metadata_entry(request,
574 ANDROID_REQUEST_TYPE,
575 (void**)&requestType, 1);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700576
577 uint32_t hourOfDay = 12;
578 add_camera_metadata_entry(request,
579 0x80000000, // EMULATOR_HOUROFDAY
580 &hourOfDay, 1);
581
582 IF_ALOGV() {
583 std::cout << "Input request template: " << std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700584 dump_indented_camera_metadata(request, 0, 1, 2);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700585 }
586
587 int numCaptures = 10;
588
589 // Enqueue numCaptures requests with increasing exposure time
590
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700591 uint64_t exposureTime = 100 * USEC;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700592 for (int reqCount = 0; reqCount < numCaptures; reqCount++ ) {
593 camera_metadata_t *req;
594 req = allocate_camera_metadata(20, 2000);
595 append_camera_metadata(req, request);
596
597 add_camera_metadata_entry(req,
598 ANDROID_SENSOR_EXPOSURE_TIME,
599 (void**)&exposureTime, 1);
600 exposureTime *= 2;
601
602 res = mRequests.enqueue(req);
603 ASSERT_EQ(NO_ERROR, res) << "Can't enqueue request: "
604 << strerror(-res);
605 }
606
607 // Get frames and image buffers one by one
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700608 uint64_t expectedExposureTime = 100 * USEC;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700609 for (int frameCount = 0; frameCount < 10; frameCount++) {
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700610 res = mFrames.waitForBuffer(SEC + expectedExposureTime);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700611 ASSERT_EQ(NO_ERROR, res) << "No frame to get: " << strerror(-res);
612
613 camera_metadata_t *frame;
614 res = mFrames.dequeue(&frame);
615 ASSERT_EQ(NO_ERROR, res);
616 ASSERT_TRUE(frame != NULL);
617
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700618 camera_metadata_entry_t frameNumber;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700619 res = find_camera_metadata_entry(frame,
620 ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700621 &frameNumber);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700622 ASSERT_EQ(NO_ERROR, res);
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700623 ASSERT_EQ(frameCount, *frameNumber.data.i32);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700624
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700625 res = rawWaiter->waitForFrame(SEC + expectedExposureTime);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700626 ASSERT_EQ(NO_ERROR, res) <<
627 "Never got raw data for capture " << frameCount;
628
629 CpuConsumer::LockedBuffer buffer;
630 res = rawConsumer->lockNextBuffer(&buffer);
631 ASSERT_EQ(NO_ERROR, res);
632
633 IF_ALOGV() {
634 char dumpname[60];
635 snprintf(dumpname, 60,
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700636 "/data/local/tmp/camera2_test-"
637 "captureBurstRaw-dump_%d.raw",
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700638 frameCount);
639 ALOGV("Dumping raw buffer to %s", dumpname);
640 // Write to file
641 std::ofstream rawFile(dumpname);
642 for (unsigned int y = 0; y < buffer.height; y++) {
643 rawFile.write(
644 (const char *)(buffer.data + y * buffer.stride * 2),
645 buffer.width * 2);
646 }
647 rawFile.close();
648 }
649
650 res = rawConsumer->unlockBuffer(buffer);
651 ASSERT_EQ(NO_ERROR, res);
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700652
653 expectedExposureTime *= 2;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700654 }
655 }
656}
657
Eino-Ville Talvala6adfd6b2012-05-14 15:25:27 -0700658TEST_F(Camera2Test, ConstructDefaultRequests) {
Igor Murashkineab33fc2012-11-06 17:02:54 -0800659
660 TEST_EXTENSION_FORKING_INIT;
661
Eino-Ville Talvala6adfd6b2012-05-14 15:25:27 -0700662 status_t res;
663
664 for (int id = 0; id < getNumCameras(); id++) {
665 if (!isHal2Supported(id)) continue;
666
667 ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
668
669 for (int i = CAMERA2_TEMPLATE_PREVIEW; i < CAMERA2_TEMPLATE_COUNT;
670 i++) {
671 camera_metadata_t *request = NULL;
672 res = mDevice->ops->construct_default_request(mDevice,
673 i,
674 &request);
675 EXPECT_EQ(NO_ERROR, res) <<
Igor Murashkin3d991c82013-04-18 10:09:16 -0700676 "Unable to construct request from template type " << i;
Eino-Ville Talvala6adfd6b2012-05-14 15:25:27 -0700677 EXPECT_TRUE(request != NULL);
678 EXPECT_LT((size_t)0, get_camera_metadata_entry_count(request));
679 EXPECT_LT((size_t)0, get_camera_metadata_data_count(request));
680
681 IF_ALOGV() {
682 std::cout << " ** Template type " << i << ":"<<std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700683 dump_indented_camera_metadata(request, 0, 2, 4);
Eino-Ville Talvala6adfd6b2012-05-14 15:25:27 -0700684 }
685
686 free_camera_metadata(request);
687 }
688 }
689}
690
Igor Murashkineab33fc2012-11-06 17:02:54 -0800691TEST_F(Camera2Test, Capture1Jpeg) {
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700692 status_t res;
693
694 for (int id = 0; id < getNumCameras(); id++) {
695 if (!isHal2Supported(id)) continue;
696
697 ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
698
Ying Wang1973f4a2013-07-15 22:18:04 -0700699 sp<BufferQueue> bq = new BufferQueue();
700 sp<CpuConsumer> jpegConsumer = new CpuConsumer(bq, 1);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700701 sp<FrameWaiter> jpegWaiter = new FrameWaiter();
702 jpegConsumer->setFrameAvailableListener(jpegWaiter);
703
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700704 const int32_t *jpegResolutions;
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700705 size_t jpegResolutionsCount;
706
707 int format = HAL_PIXEL_FORMAT_BLOB;
708
709 getResolutionList(format,
710 &jpegResolutions, &jpegResolutionsCount);
711 ASSERT_LT((size_t)0, jpegResolutionsCount);
712
713 // Pick first available JPEG resolution
714 int width = jpegResolutions[0];
715 int height = jpegResolutions[1];
716
717 int streamId;
718 ASSERT_NO_FATAL_FAILURE(
Mathias Agopian3321f1b2013-08-06 14:22:12 -0700719 setUpStream(bq, width, height, format, &streamId) );
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700720
721 camera_metadata_t *request;
722 request = allocate_camera_metadata(20, 2000);
723
Igor Murashkin09ad0a32012-12-03 13:33:08 -0800724 uint8_t metadataMode = ANDROID_REQUEST_METADATA_MODE_FULL;
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700725 add_camera_metadata_entry(request,
726 ANDROID_REQUEST_METADATA_MODE,
727 (void**)&metadataMode, 1);
728 uint32_t outputStreams = streamId;
729 add_camera_metadata_entry(request,
730 ANDROID_REQUEST_OUTPUT_STREAMS,
731 (void**)&outputStreams, 1);
732
733 uint64_t exposureTime = 10*MSEC;
734 add_camera_metadata_entry(request,
735 ANDROID_SENSOR_EXPOSURE_TIME,
736 (void**)&exposureTime, 1);
737 uint64_t frameDuration = 30*MSEC;
738 add_camera_metadata_entry(request,
739 ANDROID_SENSOR_FRAME_DURATION,
740 (void**)&frameDuration, 1);
741 uint32_t sensitivity = 100;
742 add_camera_metadata_entry(request,
743 ANDROID_SENSOR_SENSITIVITY,
744 (void**)&sensitivity, 1);
Igor Murashkin599b76f2012-12-18 16:06:07 -0800745 uint8_t requestType = ANDROID_REQUEST_TYPE_CAPTURE;
746 add_camera_metadata_entry(request,
747 ANDROID_REQUEST_TYPE,
748 (void**)&requestType, 1);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700749
750 uint32_t hourOfDay = 12;
751 add_camera_metadata_entry(request,
752 0x80000000, // EMULATOR_HOUROFDAY
753 &hourOfDay, 1);
754
755 IF_ALOGV() {
756 std::cout << "Input request: " << std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700757 dump_indented_camera_metadata(request, 0, 1, 4);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700758 }
759
760 res = mRequests.enqueue(request);
761 ASSERT_EQ(NO_ERROR, res) << "Can't enqueue request: " << strerror(-res);
762
763 res = mFrames.waitForBuffer(exposureTime + SEC);
764 ASSERT_EQ(NO_ERROR, res) << "No frame to get: " << strerror(-res);
765
766 camera_metadata_t *frame;
767 res = mFrames.dequeue(&frame);
768 ASSERT_EQ(NO_ERROR, res);
769 ASSERT_TRUE(frame != NULL);
770
771 IF_ALOGV() {
772 std::cout << "Output frame:" << std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700773 dump_indented_camera_metadata(frame, 0, 1, 4);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700774 }
775
776 res = jpegWaiter->waitForFrame(exposureTime + SEC);
777 ASSERT_EQ(NO_ERROR, res);
778
779 CpuConsumer::LockedBuffer buffer;
780 res = jpegConsumer->lockNextBuffer(&buffer);
781 ASSERT_EQ(NO_ERROR, res);
782
783 IF_ALOGV() {
784 const char *dumpname =
785 "/data/local/tmp/camera2_test-capture1jpeg-dump.jpeg";
786 ALOGV("Dumping raw buffer to %s", dumpname);
787 // Write to file
788 std::ofstream jpegFile(dumpname);
789 size_t bpp = 1;
790 for (unsigned int y = 0; y < buffer.height; y++) {
791 jpegFile.write(
792 (const char *)(buffer.data + y * buffer.stride * bpp),
793 buffer.width * bpp);
794 }
795 jpegFile.close();
796 }
797
798 res = jpegConsumer->unlockBuffer(buffer);
799 ASSERT_EQ(NO_ERROR, res);
800
Igor Murashkin599b76f2012-12-18 16:06:07 -0800801 ASSERT_EQ(OK, waitUntilDrained());
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700802 ASSERT_NO_FATAL_FAILURE(disconnectStream(streamId));
803
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700804 res = closeCameraDevice(&mDevice);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700805 ASSERT_EQ(NO_ERROR, res) << "Failed to close camera device";
806
807 }
808}
809
Igor Murashkine302ee32012-11-05 11:14:49 -0800810} // namespace tests
811} // namespace camera2
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700812} // namespace android