blob: 65ff460ba7e4af8d1d7578730d3d4c607f1cf9c1 [file] [log] [blame]
Igor Murashkinf1b9ae72012-12-07 15:08:35 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
Sasha Levitskiy0ab4c962014-04-21 14:49:12 -070018#include <inttypes.h>
Igor Murashkinf1b9ae72012-12-07 15:08:35 -080019
20#define LOG_TAG "CameraBurstTest"
21//#define LOG_NDEBUG 0
22#include <utils/Log.h>
Eino-Ville Talvala7d831712013-07-01 18:47:09 -070023#include <utils/Timers.h>
Igor Murashkinf1b9ae72012-12-07 15:08:35 -080024
25#include <cmath>
26
27#include "CameraStreamFixture.h"
28#include "TestExtensions.h"
29
Eino-Ville Talvala7d831712013-07-01 18:47:09 -070030#define CAMERA_FRAME_TIMEOUT 1000000000LL //nsecs (1 secs)
Igor Murashkinf1b9ae72012-12-07 15:08:35 -080031#define CAMERA_HEAP_COUNT 2 //HALBUG: 1 means registerBuffers fails
32#define CAMERA_BURST_DEBUGGING 0
33#define CAMERA_FRAME_BURST_COUNT 10
34
35/* constants for the exposure test */
36#define CAMERA_EXPOSURE_DOUBLE 2
37#define CAMERA_EXPOSURE_DOUBLING_THRESHOLD 1.0f
38#define CAMERA_EXPOSURE_DOUBLING_COUNT 4
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -070039#define CAMERA_EXPOSURE_FORMAT CAMERA_STREAM_AUTO_CPU_FORMAT
Igor Murashkinf1b9ae72012-12-07 15:08:35 -080040#define CAMERA_EXPOSURE_STARTING 100000 // 1/10ms, up to 51.2ms with 10 steps
41
Eino-Ville Talvala7d831712013-07-01 18:47:09 -070042#define USEC 1000LL // in ns
43#define MSEC 1000000LL // in ns
44#define SEC 1000000000LL // in ns
45
Igor Murashkinf1b9ae72012-12-07 15:08:35 -080046#if CAMERA_BURST_DEBUGGING
47#define dout std::cout
48#else
49#define dout if (0) std::cout
50#endif
51
Zhijun He60cbb522013-09-18 09:44:19 -070052#define WARN_UNLESS(condition) (!(condition) ? (std::cerr) : (std::ostream(NULL)) << "Warning: ")
53#define WARN_LE(exp, act) WARN_UNLESS((exp) <= (act))
54#define WARN_LT(exp, act) WARN_UNLESS((exp) < (act))
55#define WARN_GT(exp, act) WARN_UNLESS((exp) > (act))
56
Igor Murashkinf1b9ae72012-12-07 15:08:35 -080057using namespace android;
58using namespace android::camera2;
59
60namespace android {
61namespace camera2 {
62namespace tests {
63
64static CameraStreamParams STREAM_PARAMETERS = {
Igor Murashkinf1b9ae72012-12-07 15:08:35 -080065 /*mFormat*/ CAMERA_EXPOSURE_FORMAT,
66 /*mHeapCount*/ CAMERA_HEAP_COUNT
67};
68
69class CameraBurstTest
70 : public ::testing::Test,
71 public CameraStreamFixture {
72
73public:
74 CameraBurstTest() : CameraStreamFixture(STREAM_PARAMETERS) {
75 TEST_EXTENSION_FORKING_CONSTRUCTOR;
76
77 if (HasFatalFailure()) {
78 return;
79 }
80
81 CreateStream();
82 }
83
84 ~CameraBurstTest() {
85 TEST_EXTENSION_FORKING_DESTRUCTOR;
86
87 if (mDevice.get()) {
88 mDevice->waitUntilDrained();
89 }
90 DeleteStream();
91 }
92
93 virtual void SetUp() {
94 TEST_EXTENSION_FORKING_SET_UP;
95 }
96 virtual void TearDown() {
97 TEST_EXTENSION_FORKING_TEAR_DOWN;
98 }
99
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700100 /* this assumes the format is YUV420sp or flexible YUV */
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800101 long long TotalBrightness(const CpuConsumer::LockedBuffer& imgBuffer,
102 int *underexposed,
103 int *overexposed) const {
104
105 const uint8_t* buf = imgBuffer.data;
106 size_t stride = imgBuffer.stride;
107
108 /* iterate over the Y plane only */
109 long long acc = 0;
110
111 *underexposed = 0;
112 *overexposed = 0;
113
114 for (size_t y = 0; y < imgBuffer.height; ++y) {
115 for (size_t x = 0; x < imgBuffer.width; ++x) {
116 const uint8_t p = buf[y * stride + x];
117
118 if (p == 0) {
119 if (underexposed) {
120 ++*underexposed;
121 }
122 continue;
123 } else if (p == 255) {
124 if (overexposed) {
125 ++*overexposed;
126 }
127 continue;
128 }
129
130 acc += p;
131 }
132 }
133
134 return acc;
135 }
Eino-Ville Talvala7d831712013-07-01 18:47:09 -0700136
137 // Parses a comma-separated string list into a Vector
138 template<typename T>
139 void ParseList(const char *src, Vector<T> &list) {
140 std::istringstream s(src);
141 while (!s.eof()) {
142 char c = s.peek();
143 if (c == ',' || c == ' ') {
144 s.ignore(1, EOF);
145 continue;
146 }
147 T val;
148 s >> val;
149 list.push_back(val);
150 }
151 }
152
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800153};
154
155TEST_F(CameraBurstTest, ManualExposureControl) {
156
157 TEST_EXTENSION_FORKING_INIT;
158
159 // Range of valid exposure times, in nanoseconds
160 int64_t minExp, maxExp;
161 {
162 camera_metadata_ro_entry exposureTimeRange =
163 GetStaticEntry(ANDROID_SENSOR_INFO_EXPOSURE_TIME_RANGE);
164
165 ASSERT_EQ(2u, exposureTimeRange.count);
166 minExp = exposureTimeRange.data.i64[0];
167 maxExp = exposureTimeRange.data.i64[1];
168 }
169
170 dout << "Min exposure is " << minExp;
171 dout << " max exposure is " << maxExp << std::endl;
172
173 // Calculate some set of valid exposure times for each request
174 int64_t exposures[CAMERA_FRAME_BURST_COUNT];
175 exposures[0] = CAMERA_EXPOSURE_STARTING;
176 for (int i = 1; i < CAMERA_FRAME_BURST_COUNT; ++i) {
177 exposures[i] = exposures[i-1] * CAMERA_EXPOSURE_DOUBLE;
178 }
179 // Our calculated exposure times should be in [minExp, maxExp]
180 EXPECT_LE(minExp, exposures[0])
181 << "Minimum exposure range is too high, wanted at most "
182 << exposures[0] << "ns";
183 EXPECT_GE(maxExp, exposures[CAMERA_FRAME_BURST_COUNT-1])
184 << "Maximum exposure range is too low, wanted at least "
185 << exposures[CAMERA_FRAME_BURST_COUNT-1] << "ns";
186
187 // Create a preview request, turning off all 3A
188 CameraMetadata previewRequest;
189 ASSERT_EQ(OK, mDevice->createDefaultRequest(CAMERA2_TEMPLATE_PREVIEW,
190 &previewRequest));
191 {
Zhijun Hea1594172013-09-06 15:35:09 -0700192 Vector<int32_t> outputStreamIds;
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800193 outputStreamIds.push(mStreamId);
194 ASSERT_EQ(OK, previewRequest.update(ANDROID_REQUEST_OUTPUT_STREAMS,
195 outputStreamIds));
196
197 // Disable all 3A routines
198 uint8_t cmOff = static_cast<uint8_t>(ANDROID_CONTROL_MODE_OFF);
199 ASSERT_EQ(OK, previewRequest.update(ANDROID_CONTROL_MODE,
200 &cmOff, 1));
Eino-Ville Talvala4c543a12013-06-25 18:12:19 -0700201
202 int requestId = 1;
203 ASSERT_EQ(OK, previewRequest.update(ANDROID_REQUEST_ID,
204 &requestId, 1));
205
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800206 if (CAMERA_BURST_DEBUGGING) {
207 int frameCount = 0;
208 ASSERT_EQ(OK, previewRequest.update(ANDROID_REQUEST_FRAME_COUNT,
209 &frameCount, 1));
210 }
211 }
212
213 if (CAMERA_BURST_DEBUGGING) {
214 previewRequest.dump(STDOUT_FILENO);
215 }
216
217 // Submit capture requests
218 for (int i = 0; i < CAMERA_FRAME_BURST_COUNT; ++i) {
219 CameraMetadata tmpRequest = previewRequest;
220 ASSERT_EQ(OK, tmpRequest.update(ANDROID_SENSOR_EXPOSURE_TIME,
221 &exposures[i], 1));
Sasha Levitskiy0ab4c962014-04-21 14:49:12 -0700222 ALOGV("Submitting capture request %d with exposure %"PRId64, i,
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800223 exposures[i]);
224 dout << "Capture request " << i << " exposure is "
225 << (exposures[i]/1e6f) << std::endl;
226 ASSERT_EQ(OK, mDevice->capture(tmpRequest));
227 }
228
229 dout << "Buffer dimensions " << mWidth << "x" << mHeight << std::endl;
230
231 float brightnesses[CAMERA_FRAME_BURST_COUNT];
232 // Get each frame (metadata) and then the buffer. Calculate brightness.
233 for (int i = 0; i < CAMERA_FRAME_BURST_COUNT; ++i) {
Sasha Levitskiy0ab4c962014-04-21 14:49:12 -0700234 ALOGV("Reading capture request %d with exposure %"PRId64, i, exposures[i]);
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800235 ASSERT_EQ(OK, mDevice->waitForNextFrame(CAMERA_FRAME_TIMEOUT));
236 ALOGV("Reading capture request-1 %d", i);
Jianing Weif816eea2014-04-10 14:17:57 -0700237 CaptureResult result;
238 ASSERT_EQ(OK, mDevice->getNextResult(&result));
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800239 ALOGV("Reading capture request-2 %d", i);
240
241 ASSERT_EQ(OK, mFrameListener->waitForFrame(CAMERA_FRAME_TIMEOUT));
242 ALOGV("We got the frame now");
243
244 CpuConsumer::LockedBuffer imgBuffer;
245 ASSERT_EQ(OK, mCpuConsumer->lockNextBuffer(&imgBuffer));
246
247 int underexposed, overexposed;
248 long long brightness = TotalBrightness(imgBuffer, &underexposed,
249 &overexposed);
250 float avgBrightness = brightness * 1.0f /
251 (mWidth * mHeight - (underexposed + overexposed));
252 ALOGV("Total brightness for frame %d was %lld (underexposed %d, "
253 "overexposed %d), avg %f", i, brightness, underexposed,
254 overexposed, avgBrightness);
255 dout << "Average brightness (frame " << i << ") was " << avgBrightness
256 << " (underexposed " << underexposed << ", overexposed "
257 << overexposed << ")" << std::endl;
258
259 ASSERT_EQ(OK, mCpuConsumer->unlockBuffer(imgBuffer));
260
261 brightnesses[i] = avgBrightness;
262 }
263
264 // Calculate max consecutive frame exposure doubling
265 float prev = brightnesses[0];
266 int doubling_count = 1;
267 int max_doubling_count = 0;
268 for (int i = 1; i < CAMERA_FRAME_BURST_COUNT; ++i) {
269 if (fabs(brightnesses[i] - prev*CAMERA_EXPOSURE_DOUBLE)
270 <= CAMERA_EXPOSURE_DOUBLING_THRESHOLD) {
271 doubling_count++;
272 }
273 else {
274 max_doubling_count = std::max(max_doubling_count, doubling_count);
275 doubling_count = 1;
276 }
277 prev = brightnesses[i];
278 }
279
280 dout << "max doubling count: " << max_doubling_count << std::endl;
281
Zhijun He60cbb522013-09-18 09:44:19 -0700282 /**
283 * Make this check warning only, since the brightness calculation is not reliable
284 * and we have separate test to cover this case. Plus it is pretty subtle to make
285 * it right without complicating the test too much.
286 */
287 WARN_LE(CAMERA_EXPOSURE_DOUBLING_COUNT, max_doubling_count)
288 << "average brightness should double at least "
289 << CAMERA_EXPOSURE_DOUBLING_COUNT
290 << " times over each consecutive frame as the exposure is doubled"
291 << std::endl;
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800292}
293
Eino-Ville Talvala7d831712013-07-01 18:47:09 -0700294/**
295 * This test varies exposure time, frame duration, and sensitivity for a
296 * burst of captures. It picks values by default, but the selection can be
297 * overridden with the environment variables
298 * CAMERA2_TEST_VARIABLE_BURST_EXPOSURE_TIMES
299 * CAMERA2_TEST_VARIABLE_BURST_FRAME_DURATIONS
300 * CAMERA2_TEST_VARIABLE_BURST_SENSITIVITIES
301 * which must all be a list of comma-separated values, and each list must be
302 * the same length. In addition, if the environment variable
303 * CAMERA2_TEST_VARIABLE_BURST_DUMP_FRAMES
304 * is set to 1, then the YUV buffers are dumped into files named
305 * "camera2_test_variable_burst_frame_NNN.yuv"
306 *
307 * For example:
308 * $ setenv CAMERA2_TEST_VARIABLE_BURST_EXPOSURE_TIMES 10000000,20000000
309 * $ setenv CAMERA2_TEST_VARIABLE_BURST_FRAME_DURATIONS 40000000,40000000
310 * $ setenv CAMERA2_TEST_VARIABLE_BURST_SENSITIVITIES 200,100
311 * $ setenv CAMERA2_TEST_VARIABLE_BURST_DUMP_FRAMES 1
312 * $ /data/nativetest/camera2_test/camera2_test --gtest_filter="*VariableBurst"
313 */
314TEST_F(CameraBurstTest, VariableBurst) {
315
316 TEST_EXTENSION_FORKING_INIT;
317
318 // Bounds for checking frame duration is within range
319 const nsecs_t DURATION_UPPER_BOUND = 10 * MSEC;
320 const nsecs_t DURATION_LOWER_BOUND = 20 * MSEC;
321
322 // Threshold for considering two captures to have equivalent exposure value,
323 // as a ratio of the smaller EV to the larger EV.
324 const float EV_MATCH_BOUND = 0.95;
325 // Bound for two captures with equivalent exp values to have the same
326 // measured brightness, in 0-255 luminance.
327 const float BRIGHTNESS_MATCH_BOUND = 5;
328
329 // Environment variables to look for to override test settings
330 const char *expEnv = "CAMERA2_TEST_VARIABLE_BURST_EXPOSURE_TIMES";
331 const char *durationEnv = "CAMERA2_TEST_VARIABLE_BURST_FRAME_DURATIONS";
332 const char *sensitivityEnv = "CAMERA2_TEST_VARIABLE_BURST_SENSITIVITIES";
333 const char *dumpFrameEnv = "CAMERA2_TEST_VARIABLE_BURST_DUMP_FRAMES";
334
335 // Range of valid exposure times, in nanoseconds
336 int64_t minExp = 0, maxExp = 0;
337 // List of valid sensor sensitivities
338 Vector<int32_t> sensitivities;
339 // Range of valid frame durations, in nanoseconds
340 int64_t minDuration = 0, maxDuration = 0;
341
342 {
343 camera_metadata_ro_entry exposureTimeRange =
344 GetStaticEntry(ANDROID_SENSOR_INFO_EXPOSURE_TIME_RANGE);
345
346 EXPECT_EQ(2u, exposureTimeRange.count) << "Bad exposure time range tag."
347 "Using default values";
348 if (exposureTimeRange.count == 2) {
349 minExp = exposureTimeRange.data.i64[0];
350 maxExp = exposureTimeRange.data.i64[1];
351 }
352
353 EXPECT_LT(0, minExp) << "Minimum exposure time is 0";
354 EXPECT_LT(0, maxExp) << "Maximum exposure time is 0";
355 EXPECT_LE(minExp, maxExp) << "Minimum exposure is greater than maximum";
356
357 if (minExp == 0) {
358 minExp = 1 * MSEC; // Fallback minimum exposure time
359 }
360
361 if (maxExp == 0) {
362 maxExp = 10 * SEC; // Fallback maximum exposure time
363 }
364 }
365
Zhijun He3bf3b452013-09-18 23:42:12 -0700366 camera_metadata_ro_entry hardwareLevel =
367 GetStaticEntry(ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL);
368 ASSERT_EQ(1u, hardwareLevel.count);
369 uint8_t level = hardwareLevel.data.u8[0];
370 ASSERT_GE(level, ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED);
371 ASSERT_LE(level, ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_FULL);
372 if (level == ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED) {
373 const ::testing::TestInfo* const test_info =
374 ::testing::UnitTest::GetInstance()->current_test_info();
375 std::cerr << "Skipping test "
376 << test_info->test_case_name() << "."
377 << test_info->name()
378 << " because HAL hardware supported level is limited "
379 << std::endl;
380 return;
381 }
382
Eino-Ville Talvala7d831712013-07-01 18:47:09 -0700383 dout << "Stream size is " << mWidth << " x " << mHeight << std::endl;
384 dout << "Valid exposure range is: " <<
385 minExp << " - " << maxExp << " ns " << std::endl;
386
387 {
Zhijun He7f3ce002013-07-18 17:01:57 -0700388 camera_metadata_ro_entry sensivityRange =
389 GetStaticEntry(ANDROID_SENSOR_INFO_SENSITIVITY_RANGE);
390 EXPECT_EQ(2u, sensivityRange.count) << "No sensitivity range listed."
Eino-Ville Talvala7d831712013-07-01 18:47:09 -0700391 "Falling back to default set.";
Zhijun He7f3ce002013-07-18 17:01:57 -0700392 int32_t minSensitivity = 100;
393 int32_t maxSensitivity = 800;
Zhijun He3bf3b452013-09-18 23:42:12 -0700394 if (sensivityRange.count == 2) {
395 ASSERT_GT(sensivityRange.data.i32[0], 0);
396 ASSERT_GT(sensivityRange.data.i32[1], 0);
Zhijun He7f3ce002013-07-18 17:01:57 -0700397 minSensitivity = sensivityRange.data.i32[0];
398 maxSensitivity = sensivityRange.data.i32[1];
Eino-Ville Talvala7d831712013-07-01 18:47:09 -0700399 }
Zhijun He7f3ce002013-07-18 17:01:57 -0700400 int32_t count = (maxSensitivity - minSensitivity + 99) / 100;
401 sensitivities.push_back(minSensitivity);
402 for (int i = 1; i < count; i++) {
403 sensitivities.push_back(minSensitivity + i * 100);
404 }
405 sensitivities.push_back(maxSensitivity);
Eino-Ville Talvala7d831712013-07-01 18:47:09 -0700406 }
407
408 dout << "Available sensitivities: ";
409 for (size_t i = 0; i < sensitivities.size(); i++) {
410 dout << sensitivities[i] << " ";
411 }
412 dout << std::endl;
413
414 {
415 camera_metadata_ro_entry availableProcessedSizes =
416 GetStaticEntry(ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES);
417
418 camera_metadata_ro_entry availableProcessedMinFrameDurations =
419 GetStaticEntry(ANDROID_SCALER_AVAILABLE_PROCESSED_MIN_DURATIONS);
420
421 EXPECT_EQ(availableProcessedSizes.count,
422 availableProcessedMinFrameDurations.count * 2) <<
423 "The number of minimum frame durations doesn't match the number of "
424 "available sizes. Using fallback values";
425
426 if (availableProcessedSizes.count ==
427 availableProcessedMinFrameDurations.count * 2) {
428 bool gotSize = false;
429 for (size_t i = 0; i < availableProcessedSizes.count; i += 2) {
430 if (availableProcessedSizes.data.i32[i] == mWidth &&
431 availableProcessedSizes.data.i32[i+1] == mHeight) {
432 gotSize = true;
433 minDuration = availableProcessedMinFrameDurations.data.i64[i/2];
434 }
435 }
436 EXPECT_TRUE(gotSize) << "Can't find stream size in list of "
437 "available sizes: " << mWidth << ", " << mHeight;
438 }
439 if (minDuration == 0) {
440 minDuration = 1 * SEC / 30; // Fall back to 30 fps as minimum duration
441 }
442
443 ASSERT_LT(0, minDuration);
444
445 camera_metadata_ro_entry maxFrameDuration =
446 GetStaticEntry(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION);
447
448 EXPECT_EQ(1u, maxFrameDuration.count) << "No valid maximum frame duration";
449
450 if (maxFrameDuration.count == 1) {
451 maxDuration = maxFrameDuration.data.i64[0];
452 }
453
Zhijun He6e548cf2013-08-08 19:43:24 -0700454 EXPECT_GT(maxDuration, 0) << "Max duration is 0 or not given, using fallback";
Eino-Ville Talvala7d831712013-07-01 18:47:09 -0700455
456 if (maxDuration == 0) {
457 maxDuration = 10 * SEC; // Fall back to 10 seconds as max duration
458 }
459
460 }
461 dout << "Available frame duration range for configured stream size: "
462 << minDuration << " - " << maxDuration << " ns" << std::endl;
463
464 // Get environment variables if set
465 const char *expVal = getenv(expEnv);
466 const char *durationVal = getenv(durationEnv);
467 const char *sensitivityVal = getenv(sensitivityEnv);
468
469 bool gotExp = (expVal != NULL);
470 bool gotDuration = (durationVal != NULL);
471 bool gotSensitivity = (sensitivityVal != NULL);
472
473 // All or none must be provided if using override envs
474 ASSERT_TRUE( (gotDuration && gotExp && gotSensitivity) ||
475 (!gotDuration && !gotExp && !gotSensitivity) ) <<
476 "Incomplete set of environment variable overrides provided";
477
478 Vector<int64_t> expList, durationList;
479 Vector<int32_t> sensitivityList;
480 if (gotExp) {
481 ParseList(expVal, expList);
482 ParseList(durationVal, durationList);
483 ParseList(sensitivityVal, sensitivityList);
484
485 ASSERT_TRUE(
486 (expList.size() == durationList.size()) &&
487 (durationList.size() == sensitivityList.size())) <<
488 "Mismatched sizes in env lists, or parse error";
489
490 dout << "Using burst list from environment with " << expList.size() <<
491 " captures" << std::endl;
492 } else {
493 // Create a default set of controls based on the available ranges
494
495 int64_t e;
496 int64_t d;
497 int32_t s;
498
499 // Exposure ramp
500
501 e = minExp;
502 d = minDuration;
503 s = sensitivities[0];
504 while (e < maxExp) {
505 expList.push_back(e);
506 durationList.push_back(d);
507 sensitivityList.push_back(s);
508 e = e * 2;
509 }
510 e = maxExp;
511 expList.push_back(e);
512 durationList.push_back(d);
513 sensitivityList.push_back(s);
514
515 // Duration ramp
516
517 e = 30 * MSEC;
518 d = minDuration;
519 s = sensitivities[0];
520 while (d < maxDuration) {
521 // make sure exposure <= frame duration
522 expList.push_back(e > d ? d : e);
523 durationList.push_back(d);
524 sensitivityList.push_back(s);
525 d = d * 2;
526 }
527
528 // Sensitivity ramp
529
530 e = 30 * MSEC;
531 d = 30 * MSEC;
532 d = d > minDuration ? d : minDuration;
533 for (size_t i = 0; i < sensitivities.size(); i++) {
534 expList.push_back(e);
535 durationList.push_back(d);
536 sensitivityList.push_back(sensitivities[i]);
537 }
538
539 // Constant-EV ramp, duration == exposure
540
541 e = 30 * MSEC; // at ISO 100
542 for (size_t i = 0; i < sensitivities.size(); i++) {
543 int64_t e_adj = e * 100 / sensitivities[i];
544 expList.push_back(e_adj);
545 durationList.push_back(e_adj > minDuration ? e_adj : minDuration);
546 sensitivityList.push_back(sensitivities[i]);
547 }
548
549 dout << "Default burst sequence created with " << expList.size() <<
550 " entries" << std::endl;
551 }
552
553 // Validate the list, but warn only
554 for (size_t i = 0; i < expList.size(); i++) {
555 EXPECT_GE(maxExp, expList[i])
556 << "Capture " << i << " exposure too long: " << expList[i];
557 EXPECT_LE(minExp, expList[i])
558 << "Capture " << i << " exposure too short: " << expList[i];
559 EXPECT_GE(maxDuration, durationList[i])
560 << "Capture " << i << " duration too long: " << durationList[i];
561 EXPECT_LE(minDuration, durationList[i])
562 << "Capture " << i << " duration too short: " << durationList[i];
563 bool validSensitivity = false;
564 for (size_t j = 0; j < sensitivities.size(); j++) {
565 if (sensitivityList[i] == sensitivities[j]) {
566 validSensitivity = true;
567 break;
568 }
569 }
570 EXPECT_TRUE(validSensitivity)
571 << "Capture " << i << " sensitivity not in list: " << sensitivityList[i];
572 }
573
574 // Check if debug yuv dumps are requested
575
576 bool dumpFrames = false;
577 {
578 const char *frameDumpVal = getenv(dumpFrameEnv);
579 if (frameDumpVal != NULL) {
580 if (frameDumpVal[0] == '1') dumpFrames = true;
581 }
582 }
583
584 dout << "Dumping YUV frames " <<
585 (dumpFrames ? "enabled, not checking timing" : "disabled") << std::endl;
586
587 // Create a base preview request, turning off all 3A
588 CameraMetadata previewRequest;
589 ASSERT_EQ(OK, mDevice->createDefaultRequest(CAMERA2_TEMPLATE_PREVIEW,
590 &previewRequest));
591 {
Zhijun Hea1594172013-09-06 15:35:09 -0700592 Vector<int32_t> outputStreamIds;
Eino-Ville Talvala7d831712013-07-01 18:47:09 -0700593 outputStreamIds.push(mStreamId);
594 ASSERT_EQ(OK, previewRequest.update(ANDROID_REQUEST_OUTPUT_STREAMS,
595 outputStreamIds));
596
597 // Disable all 3A routines
598 uint8_t cmOff = static_cast<uint8_t>(ANDROID_CONTROL_MODE_OFF);
599 ASSERT_EQ(OK, previewRequest.update(ANDROID_CONTROL_MODE,
600 &cmOff, 1));
601
602 int requestId = 1;
603 ASSERT_EQ(OK, previewRequest.update(ANDROID_REQUEST_ID,
604 &requestId, 1));
605 }
606
607 // Submit capture requests
608
609 for (size_t i = 0; i < expList.size(); ++i) {
610 CameraMetadata tmpRequest = previewRequest;
611 ASSERT_EQ(OK, tmpRequest.update(ANDROID_SENSOR_EXPOSURE_TIME,
612 &expList[i], 1));
613 ASSERT_EQ(OK, tmpRequest.update(ANDROID_SENSOR_FRAME_DURATION,
614 &durationList[i], 1));
615 ASSERT_EQ(OK, tmpRequest.update(ANDROID_SENSOR_SENSITIVITY,
616 &sensitivityList[i], 1));
Sasha Levitskiy0ab4c962014-04-21 14:49:12 -0700617 ALOGV("Submitting capture %zu with exposure %"PRId64", frame duration %"PRId64", sensitivity %d",
Eino-Ville Talvala7d831712013-07-01 18:47:09 -0700618 i, expList[i], durationList[i], sensitivityList[i]);
619 dout << "Capture request " << i <<
620 ": exposure is " << (expList[i]/1e6f) << " ms" <<
621 ", frame duration is " << (durationList[i]/1e6f) << " ms" <<
622 ", sensitivity is " << sensitivityList[i] <<
623 std::endl;
624 ASSERT_EQ(OK, mDevice->capture(tmpRequest));
625 }
626
627 Vector<float> brightnesses;
628 Vector<nsecs_t> captureTimes;
629 brightnesses.setCapacity(expList.size());
630 captureTimes.setCapacity(expList.size());
631
632 // Get each frame (metadata) and then the buffer. Calculate brightness.
633 for (size_t i = 0; i < expList.size(); ++i) {
634
Sasha Levitskiy0ab4c962014-04-21 14:49:12 -0700635 ALOGV("Reading request %zu", i);
Eino-Ville Talvala7d831712013-07-01 18:47:09 -0700636 dout << "Waiting for capture " << i << ": " <<
637 " exposure " << (expList[i]/1e6f) << " ms," <<
638 " frame duration " << (durationList[i]/1e6f) << " ms," <<
639 " sensitivity " << sensitivityList[i] <<
640 std::endl;
641
642 // Set wait limit based on expected frame duration, or minimum timeout
643 int64_t waitLimit = CAMERA_FRAME_TIMEOUT;
644 if (expList[i] * 2 > waitLimit) waitLimit = expList[i] * 2;
645 if (durationList[i] * 2 > waitLimit) waitLimit = durationList[i] * 2;
646
647 ASSERT_EQ(OK, mDevice->waitForNextFrame(waitLimit));
Sasha Levitskiy0ab4c962014-04-21 14:49:12 -0700648 ALOGV("Reading capture request-1 %zu", i);
Jianing Weif816eea2014-04-10 14:17:57 -0700649 CaptureResult result;
650 ASSERT_EQ(OK, mDevice->getNextResult(&result));
Sasha Levitskiy0ab4c962014-04-21 14:49:12 -0700651 ALOGV("Reading capture request-2 %zu", i);
Eino-Ville Talvala7d831712013-07-01 18:47:09 -0700652
653 ASSERT_EQ(OK, mFrameListener->waitForFrame(CAMERA_FRAME_TIMEOUT));
654 ALOGV("We got the frame now");
655
656 captureTimes.push_back(systemTime());
657
658 CpuConsumer::LockedBuffer imgBuffer;
659 ASSERT_EQ(OK, mCpuConsumer->lockNextBuffer(&imgBuffer));
660
661 int underexposed, overexposed;
662 float avgBrightness = 0;
663 long long brightness = TotalBrightness(imgBuffer, &underexposed,
664 &overexposed);
665 int numValidPixels = mWidth * mHeight - (underexposed + overexposed);
666 if (numValidPixels != 0) {
667 avgBrightness = brightness * 1.0f / numValidPixels;
668 } else if (underexposed < overexposed) {
669 avgBrightness = 255;
670 }
671
Sasha Levitskiy0ab4c962014-04-21 14:49:12 -0700672 ALOGV("Total brightness for frame %zu was %lld (underexposed %d, "
Eino-Ville Talvala7d831712013-07-01 18:47:09 -0700673 "overexposed %d), avg %f", i, brightness, underexposed,
674 overexposed, avgBrightness);
675 dout << "Average brightness (frame " << i << ") was " << avgBrightness
676 << " (underexposed " << underexposed << ", overexposed "
677 << overexposed << ")" << std::endl;
678 brightnesses.push_back(avgBrightness);
679
680 if (i != 0) {
681 float prevEv = static_cast<float>(expList[i - 1]) * sensitivityList[i - 1];
682 float currentEv = static_cast<float>(expList[i]) * sensitivityList[i];
683 float evRatio = (prevEv > currentEv) ? (currentEv / prevEv) :
684 (prevEv / currentEv);
685 if ( evRatio > EV_MATCH_BOUND ) {
Zhijun He60cbb522013-09-18 09:44:19 -0700686 WARN_LT(fabs(brightnesses[i] - brightnesses[i - 1]),
Eino-Ville Talvala7d831712013-07-01 18:47:09 -0700687 BRIGHTNESS_MATCH_BOUND) <<
688 "Capture brightness different from previous, even though "
689 "they have the same EV value. Ev now: " << currentEv <<
690 ", previous: " << prevEv << ". Brightness now: " <<
Zhijun He60cbb522013-09-18 09:44:19 -0700691 brightnesses[i] << ", previous: " << brightnesses[i-1] <<
692 std::endl;
Eino-Ville Talvala7d831712013-07-01 18:47:09 -0700693 }
694 // Only check timing if not saving to disk, since that slows things
695 // down substantially
696 if (!dumpFrames) {
697 nsecs_t timeDelta = captureTimes[i] - captureTimes[i-1];
698 nsecs_t expectedDelta = expList[i] > durationList[i] ?
699 expList[i] : durationList[i];
Zhijun He60cbb522013-09-18 09:44:19 -0700700 WARN_LT(timeDelta, expectedDelta + DURATION_UPPER_BOUND) <<
Eino-Ville Talvala7d831712013-07-01 18:47:09 -0700701 "Capture took " << timeDelta << " ns to receive, but expected"
Zhijun He60cbb522013-09-18 09:44:19 -0700702 " frame duration was " << expectedDelta << " ns." <<
703 std::endl;
704 WARN_GT(timeDelta, expectedDelta - DURATION_LOWER_BOUND) <<
Eino-Ville Talvala7d831712013-07-01 18:47:09 -0700705 "Capture took " << timeDelta << " ns to receive, but expected"
Zhijun He60cbb522013-09-18 09:44:19 -0700706 " frame duration was " << expectedDelta << " ns." <<
707 std::endl;
Eino-Ville Talvala7d831712013-07-01 18:47:09 -0700708 dout << "Time delta from previous frame: " << timeDelta / 1e6 <<
709 " ms. Expected " << expectedDelta / 1e6 << " ms" << std::endl;
710 }
711 }
712
713 if (dumpFrames) {
714 String8 dumpName =
Sasha Levitskiy0ab4c962014-04-21 14:49:12 -0700715 String8::format("/data/local/tmp/camera2_test_variable_burst_frame_%03zu.yuv", i);
Eino-Ville Talvala7d831712013-07-01 18:47:09 -0700716 dout << " Writing YUV dump to " << dumpName << std::endl;
717 DumpYuvToFile(dumpName, imgBuffer);
718 }
719
720 ASSERT_EQ(OK, mCpuConsumer->unlockBuffer(imgBuffer));
721 }
722
723}
724
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800725}
726}
727}