blob: e3e7d9a1cd4aad8380b056dc2be0ff50582e9e3a [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
Dan Stoza5dce9e42014-04-07 13:39:37 -0700384 sp<IGraphicBufferProducer> bqProducer;
385 sp<IGraphicBufferConsumer> bqConsumer;
386 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer);
387 sp<CpuConsumer> rawConsumer = new CpuConsumer(bqConsumer, 1);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700388 sp<FrameWaiter> rawWaiter = new FrameWaiter();
389 rawConsumer->setFrameAvailableListener(rawWaiter);
390
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700391 const int32_t *rawResolutions;
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700392 size_t rawResolutionsCount;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700393
394 int format = HAL_PIXEL_FORMAT_RAW_SENSOR;
395
396 getResolutionList(format,
397 &rawResolutions, &rawResolutionsCount);
Igor Murashkin3d991c82013-04-18 10:09:16 -0700398
399 if (rawResolutionsCount <= 0) {
400 const ::testing::TestInfo* const test_info =
401 ::testing::UnitTest::GetInstance()->current_test_info();
402 std::cerr << "Skipping test "
403 << test_info->test_case_name() << "."
404 << test_info->name()
405 << " because the optional format was not available: "
406 << "RAW_SENSOR" << std::endl;
407 return;
408 }
409
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700410 ASSERT_LT((size_t)0, rawResolutionsCount);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700411
412 // Pick first available raw resolution
413 int width = rawResolutions[0];
414 int height = rawResolutions[1];
415
416 int streamId;
417 ASSERT_NO_FATAL_FAILURE(
Dan Stoza5dce9e42014-04-07 13:39:37 -0700418 setUpStream(bqProducer, width, height, format, &streamId) );
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700419
420 camera_metadata_t *request;
421 request = allocate_camera_metadata(20, 2000);
422
Igor Murashkin09ad0a32012-12-03 13:33:08 -0800423 uint8_t metadataMode = ANDROID_REQUEST_METADATA_MODE_FULL;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700424 add_camera_metadata_entry(request,
425 ANDROID_REQUEST_METADATA_MODE,
426 (void**)&metadataMode, 1);
427 uint32_t outputStreams = streamId;
428 add_camera_metadata_entry(request,
429 ANDROID_REQUEST_OUTPUT_STREAMS,
430 (void**)&outputStreams, 1);
431
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700432 uint64_t exposureTime = 10*MSEC;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700433 add_camera_metadata_entry(request,
434 ANDROID_SENSOR_EXPOSURE_TIME,
435 (void**)&exposureTime, 1);
436 uint64_t frameDuration = 30*MSEC;
437 add_camera_metadata_entry(request,
438 ANDROID_SENSOR_FRAME_DURATION,
439 (void**)&frameDuration, 1);
440 uint32_t sensitivity = 100;
441 add_camera_metadata_entry(request,
442 ANDROID_SENSOR_SENSITIVITY,
443 (void**)&sensitivity, 1);
Igor Murashkin599b76f2012-12-18 16:06:07 -0800444 uint8_t requestType = ANDROID_REQUEST_TYPE_CAPTURE;
445 add_camera_metadata_entry(request,
446 ANDROID_REQUEST_TYPE,
447 (void**)&requestType, 1);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700448
Eino-Ville Talvala895ed342012-05-20 17:25:53 -0700449 uint32_t hourOfDay = 12;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700450 add_camera_metadata_entry(request,
451 0x80000000, // EMULATOR_HOUROFDAY
452 &hourOfDay, 1);
453
454 IF_ALOGV() {
455 std::cout << "Input request: " << std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700456 dump_indented_camera_metadata(request, 0, 1, 2);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700457 }
458
459 res = mRequests.enqueue(request);
460 ASSERT_EQ(NO_ERROR, res) << "Can't enqueue request: " << strerror(-res);
461
462 res = mFrames.waitForBuffer(exposureTime + SEC);
463 ASSERT_EQ(NO_ERROR, res) << "No frame to get: " << strerror(-res);
464
465 camera_metadata_t *frame;
466 res = mFrames.dequeue(&frame);
467 ASSERT_EQ(NO_ERROR, res);
468 ASSERT_TRUE(frame != NULL);
469
470 IF_ALOGV() {
471 std::cout << "Output frame:" << std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700472 dump_indented_camera_metadata(frame, 0, 1, 2);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700473 }
474
475 res = rawWaiter->waitForFrame(exposureTime + SEC);
476 ASSERT_EQ(NO_ERROR, res);
477
478 CpuConsumer::LockedBuffer buffer;
479 res = rawConsumer->lockNextBuffer(&buffer);
480 ASSERT_EQ(NO_ERROR, res);
481
482 IF_ALOGV() {
483 const char *dumpname =
484 "/data/local/tmp/camera2_test-capture1raw-dump.raw";
485 ALOGV("Dumping raw buffer to %s", dumpname);
486 // Write to file
487 std::ofstream rawFile(dumpname);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700488 size_t bpp = 2;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700489 for (unsigned int y = 0; y < buffer.height; y++) {
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700490 rawFile.write(
491 (const char *)(buffer.data + y * buffer.stride * bpp),
492 buffer.width * bpp);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700493 }
494 rawFile.close();
495 }
496
497 res = rawConsumer->unlockBuffer(buffer);
498 ASSERT_EQ(NO_ERROR, res);
499
Igor Murashkin599b76f2012-12-18 16:06:07 -0800500 ASSERT_EQ(OK, waitUntilDrained());
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700501 ASSERT_NO_FATAL_FAILURE(disconnectStream(streamId));
502
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700503 res = closeCameraDevice(&mDevice);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700504 ASSERT_EQ(NO_ERROR, res) << "Failed to close camera device";
505
506 }
507}
508
509TEST_F(Camera2Test, CaptureBurstRaw) {
Igor Murashkineab33fc2012-11-06 17:02:54 -0800510
511 TEST_EXTENSION_FORKING_INIT;
512
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700513 status_t res;
514
515 for (int id = 0; id < getNumCameras(); id++) {
516 if (!isHal2Supported(id)) continue;
517
518 ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
519
Dan Stoza5dce9e42014-04-07 13:39:37 -0700520 sp<IGraphicBufferProducer> bqProducer;
521 sp<IGraphicBufferConsumer> bqConsumer;
522 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer);
523 sp<CpuConsumer> rawConsumer = new CpuConsumer(bqConsumer, 1);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700524 sp<FrameWaiter> rawWaiter = new FrameWaiter();
525 rawConsumer->setFrameAvailableListener(rawWaiter);
526
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700527 const int32_t *rawResolutions;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700528 size_t rawResolutionsCount;
529
530 int format = HAL_PIXEL_FORMAT_RAW_SENSOR;
531
532 getResolutionList(format,
533 &rawResolutions, &rawResolutionsCount);
Igor Murashkin3d991c82013-04-18 10:09:16 -0700534
535 if (rawResolutionsCount <= 0) {
536 const ::testing::TestInfo* const test_info =
537 ::testing::UnitTest::GetInstance()->current_test_info();
538 std::cerr << "Skipping test "
539 << test_info->test_case_name() << "."
540 << test_info->name()
541 << " because the optional format was not available: "
542 << "RAW_SENSOR" << std::endl;
543 return;
544 }
545
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700546 ASSERT_LT((uint32_t)0, rawResolutionsCount);
547
548 // Pick first available raw resolution
549 int width = rawResolutions[0];
550 int height = rawResolutions[1];
551
552 int streamId;
553 ASSERT_NO_FATAL_FAILURE(
Dan Stoza5dce9e42014-04-07 13:39:37 -0700554 setUpStream(bqProducer, width, height, format, &streamId) );
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700555
556 camera_metadata_t *request;
557 request = allocate_camera_metadata(20, 2000);
558
Igor Murashkin09ad0a32012-12-03 13:33:08 -0800559 uint8_t metadataMode = ANDROID_REQUEST_METADATA_MODE_FULL;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700560 add_camera_metadata_entry(request,
561 ANDROID_REQUEST_METADATA_MODE,
562 (void**)&metadataMode, 1);
563 uint32_t outputStreams = streamId;
564 add_camera_metadata_entry(request,
565 ANDROID_REQUEST_OUTPUT_STREAMS,
566 (void**)&outputStreams, 1);
567
568 uint64_t frameDuration = 30*MSEC;
569 add_camera_metadata_entry(request,
570 ANDROID_SENSOR_FRAME_DURATION,
571 (void**)&frameDuration, 1);
572 uint32_t sensitivity = 100;
573 add_camera_metadata_entry(request,
574 ANDROID_SENSOR_SENSITIVITY,
575 (void**)&sensitivity, 1);
Igor Murashkin599b76f2012-12-18 16:06:07 -0800576 uint8_t requestType = ANDROID_REQUEST_TYPE_CAPTURE;
577 add_camera_metadata_entry(request,
578 ANDROID_REQUEST_TYPE,
579 (void**)&requestType, 1);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700580
581 uint32_t hourOfDay = 12;
582 add_camera_metadata_entry(request,
583 0x80000000, // EMULATOR_HOUROFDAY
584 &hourOfDay, 1);
585
586 IF_ALOGV() {
587 std::cout << "Input request template: " << std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700588 dump_indented_camera_metadata(request, 0, 1, 2);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700589 }
590
591 int numCaptures = 10;
592
593 // Enqueue numCaptures requests with increasing exposure time
594
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700595 uint64_t exposureTime = 100 * USEC;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700596 for (int reqCount = 0; reqCount < numCaptures; reqCount++ ) {
597 camera_metadata_t *req;
598 req = allocate_camera_metadata(20, 2000);
599 append_camera_metadata(req, request);
600
601 add_camera_metadata_entry(req,
602 ANDROID_SENSOR_EXPOSURE_TIME,
603 (void**)&exposureTime, 1);
604 exposureTime *= 2;
605
606 res = mRequests.enqueue(req);
607 ASSERT_EQ(NO_ERROR, res) << "Can't enqueue request: "
608 << strerror(-res);
609 }
610
611 // Get frames and image buffers one by one
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700612 uint64_t expectedExposureTime = 100 * USEC;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700613 for (int frameCount = 0; frameCount < 10; frameCount++) {
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700614 res = mFrames.waitForBuffer(SEC + expectedExposureTime);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700615 ASSERT_EQ(NO_ERROR, res) << "No frame to get: " << strerror(-res);
616
617 camera_metadata_t *frame;
618 res = mFrames.dequeue(&frame);
619 ASSERT_EQ(NO_ERROR, res);
620 ASSERT_TRUE(frame != NULL);
621
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700622 camera_metadata_entry_t frameNumber;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700623 res = find_camera_metadata_entry(frame,
624 ANDROID_REQUEST_FRAME_COUNT,
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700625 &frameNumber);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700626 ASSERT_EQ(NO_ERROR, res);
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700627 ASSERT_EQ(frameCount, *frameNumber.data.i32);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700628
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700629 res = rawWaiter->waitForFrame(SEC + expectedExposureTime);
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700630 ASSERT_EQ(NO_ERROR, res) <<
631 "Never got raw data for capture " << frameCount;
632
633 CpuConsumer::LockedBuffer buffer;
634 res = rawConsumer->lockNextBuffer(&buffer);
635 ASSERT_EQ(NO_ERROR, res);
636
637 IF_ALOGV() {
638 char dumpname[60];
639 snprintf(dumpname, 60,
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700640 "/data/local/tmp/camera2_test-"
641 "captureBurstRaw-dump_%d.raw",
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700642 frameCount);
643 ALOGV("Dumping raw buffer to %s", dumpname);
644 // Write to file
645 std::ofstream rawFile(dumpname);
646 for (unsigned int y = 0; y < buffer.height; y++) {
647 rawFile.write(
648 (const char *)(buffer.data + y * buffer.stride * 2),
649 buffer.width * 2);
650 }
651 rawFile.close();
652 }
653
654 res = rawConsumer->unlockBuffer(buffer);
655 ASSERT_EQ(NO_ERROR, res);
Eino-Ville Talvalae6a3c3c2012-05-11 16:18:42 -0700656
657 expectedExposureTime *= 2;
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700658 }
659 }
660}
661
Eino-Ville Talvala6adfd6b2012-05-14 15:25:27 -0700662TEST_F(Camera2Test, ConstructDefaultRequests) {
Igor Murashkineab33fc2012-11-06 17:02:54 -0800663
664 TEST_EXTENSION_FORKING_INIT;
665
Eino-Ville Talvala6adfd6b2012-05-14 15:25:27 -0700666 status_t res;
667
668 for (int id = 0; id < getNumCameras(); id++) {
669 if (!isHal2Supported(id)) continue;
670
671 ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
672
673 for (int i = CAMERA2_TEMPLATE_PREVIEW; i < CAMERA2_TEMPLATE_COUNT;
674 i++) {
675 camera_metadata_t *request = NULL;
676 res = mDevice->ops->construct_default_request(mDevice,
677 i,
678 &request);
679 EXPECT_EQ(NO_ERROR, res) <<
Igor Murashkin3d991c82013-04-18 10:09:16 -0700680 "Unable to construct request from template type " << i;
Eino-Ville Talvala6adfd6b2012-05-14 15:25:27 -0700681 EXPECT_TRUE(request != NULL);
682 EXPECT_LT((size_t)0, get_camera_metadata_entry_count(request));
683 EXPECT_LT((size_t)0, get_camera_metadata_data_count(request));
684
685 IF_ALOGV() {
686 std::cout << " ** Template type " << i << ":"<<std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700687 dump_indented_camera_metadata(request, 0, 2, 4);
Eino-Ville Talvala6adfd6b2012-05-14 15:25:27 -0700688 }
689
690 free_camera_metadata(request);
691 }
692 }
693}
694
Igor Murashkineab33fc2012-11-06 17:02:54 -0800695TEST_F(Camera2Test, Capture1Jpeg) {
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700696 status_t res;
697
698 for (int id = 0; id < getNumCameras(); id++) {
699 if (!isHal2Supported(id)) continue;
700
701 ASSERT_NO_FATAL_FAILURE(setUpCamera(id));
702
Dan Stoza5dce9e42014-04-07 13:39:37 -0700703 sp<IGraphicBufferProducer> bqProducer;
704 sp<IGraphicBufferConsumer> bqConsumer;
705 BufferQueue::createBufferQueue(&bqProducer, &bqConsumer);
706 sp<CpuConsumer> jpegConsumer = new CpuConsumer(bqConsumer, 1);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700707 sp<FrameWaiter> jpegWaiter = new FrameWaiter();
708 jpegConsumer->setFrameAvailableListener(jpegWaiter);
709
Eino-Ville Talvalab8b64392012-08-24 12:32:17 -0700710 const int32_t *jpegResolutions;
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700711 size_t jpegResolutionsCount;
712
713 int format = HAL_PIXEL_FORMAT_BLOB;
714
715 getResolutionList(format,
716 &jpegResolutions, &jpegResolutionsCount);
717 ASSERT_LT((size_t)0, jpegResolutionsCount);
718
719 // Pick first available JPEG resolution
720 int width = jpegResolutions[0];
721 int height = jpegResolutions[1];
722
723 int streamId;
724 ASSERT_NO_FATAL_FAILURE(
Dan Stoza5dce9e42014-04-07 13:39:37 -0700725 setUpStream(bqProducer, width, height, format, &streamId) );
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700726
727 camera_metadata_t *request;
728 request = allocate_camera_metadata(20, 2000);
729
Igor Murashkin09ad0a32012-12-03 13:33:08 -0800730 uint8_t metadataMode = ANDROID_REQUEST_METADATA_MODE_FULL;
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700731 add_camera_metadata_entry(request,
732 ANDROID_REQUEST_METADATA_MODE,
733 (void**)&metadataMode, 1);
734 uint32_t outputStreams = streamId;
735 add_camera_metadata_entry(request,
736 ANDROID_REQUEST_OUTPUT_STREAMS,
737 (void**)&outputStreams, 1);
738
739 uint64_t exposureTime = 10*MSEC;
740 add_camera_metadata_entry(request,
741 ANDROID_SENSOR_EXPOSURE_TIME,
742 (void**)&exposureTime, 1);
743 uint64_t frameDuration = 30*MSEC;
744 add_camera_metadata_entry(request,
745 ANDROID_SENSOR_FRAME_DURATION,
746 (void**)&frameDuration, 1);
747 uint32_t sensitivity = 100;
748 add_camera_metadata_entry(request,
749 ANDROID_SENSOR_SENSITIVITY,
750 (void**)&sensitivity, 1);
Igor Murashkin599b76f2012-12-18 16:06:07 -0800751 uint8_t requestType = ANDROID_REQUEST_TYPE_CAPTURE;
752 add_camera_metadata_entry(request,
753 ANDROID_REQUEST_TYPE,
754 (void**)&requestType, 1);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700755
756 uint32_t hourOfDay = 12;
757 add_camera_metadata_entry(request,
758 0x80000000, // EMULATOR_HOUROFDAY
759 &hourOfDay, 1);
760
761 IF_ALOGV() {
762 std::cout << "Input request: " << std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700763 dump_indented_camera_metadata(request, 0, 1, 4);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700764 }
765
766 res = mRequests.enqueue(request);
767 ASSERT_EQ(NO_ERROR, res) << "Can't enqueue request: " << strerror(-res);
768
769 res = mFrames.waitForBuffer(exposureTime + SEC);
770 ASSERT_EQ(NO_ERROR, res) << "No frame to get: " << strerror(-res);
771
772 camera_metadata_t *frame;
773 res = mFrames.dequeue(&frame);
774 ASSERT_EQ(NO_ERROR, res);
775 ASSERT_TRUE(frame != NULL);
776
777 IF_ALOGV() {
778 std::cout << "Output frame:" << std::endl;
Eino-Ville Talvala5d64b232012-07-30 10:12:40 -0700779 dump_indented_camera_metadata(frame, 0, 1, 4);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700780 }
781
782 res = jpegWaiter->waitForFrame(exposureTime + SEC);
783 ASSERT_EQ(NO_ERROR, res);
784
785 CpuConsumer::LockedBuffer buffer;
786 res = jpegConsumer->lockNextBuffer(&buffer);
787 ASSERT_EQ(NO_ERROR, res);
788
789 IF_ALOGV() {
790 const char *dumpname =
791 "/data/local/tmp/camera2_test-capture1jpeg-dump.jpeg";
792 ALOGV("Dumping raw buffer to %s", dumpname);
793 // Write to file
794 std::ofstream jpegFile(dumpname);
795 size_t bpp = 1;
796 for (unsigned int y = 0; y < buffer.height; y++) {
797 jpegFile.write(
798 (const char *)(buffer.data + y * buffer.stride * bpp),
799 buffer.width * bpp);
800 }
801 jpegFile.close();
802 }
803
804 res = jpegConsumer->unlockBuffer(buffer);
805 ASSERT_EQ(NO_ERROR, res);
806
Igor Murashkin599b76f2012-12-18 16:06:07 -0800807 ASSERT_EQ(OK, waitUntilDrained());
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700808 ASSERT_NO_FATAL_FAILURE(disconnectStream(streamId));
809
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700810 res = closeCameraDevice(&mDevice);
Eino-Ville Talvalafa7a91d2012-05-22 10:41:20 -0700811 ASSERT_EQ(NO_ERROR, res) << "Failed to close camera device";
812
813 }
814}
815
Igor Murashkine302ee32012-11-05 11:14:49 -0800816} // namespace tests
817} // namespace camera2
Eino-Ville Talvala567b4a22012-04-23 09:29:38 -0700818} // namespace android