blob: e39970c2d07f4287176c40a43d9874c088ffcdff [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>
18
19#define LOG_TAG "CameraBurstTest"
20//#define LOG_NDEBUG 0
21#include <utils/Log.h>
22
23#include <cmath>
24
25#include "CameraStreamFixture.h"
26#include "TestExtensions.h"
27
28#define CAMERA_FRAME_TIMEOUT 1000000000 //nsecs (1 secs)
29#define CAMERA_HEAP_COUNT 2 //HALBUG: 1 means registerBuffers fails
30#define CAMERA_BURST_DEBUGGING 0
31#define CAMERA_FRAME_BURST_COUNT 10
32
33/* constants for the exposure test */
34#define CAMERA_EXPOSURE_DOUBLE 2
35#define CAMERA_EXPOSURE_DOUBLING_THRESHOLD 1.0f
36#define CAMERA_EXPOSURE_DOUBLING_COUNT 4
37#define CAMERA_EXPOSURE_FORMAT HAL_PIXEL_FORMAT_YCrCb_420_SP
38#define CAMERA_EXPOSURE_STARTING 100000 // 1/10ms, up to 51.2ms with 10 steps
39
40#if CAMERA_BURST_DEBUGGING
41#define dout std::cout
42#else
43#define dout if (0) std::cout
44#endif
45
46using namespace android;
47using namespace android::camera2;
48
49namespace android {
50namespace camera2 {
51namespace tests {
52
53static CameraStreamParams STREAM_PARAMETERS = {
Igor Murashkinf1b9ae72012-12-07 15:08:35 -080054 /*mFormat*/ CAMERA_EXPOSURE_FORMAT,
55 /*mHeapCount*/ CAMERA_HEAP_COUNT
56};
57
58class CameraBurstTest
59 : public ::testing::Test,
60 public CameraStreamFixture {
61
62public:
63 CameraBurstTest() : CameraStreamFixture(STREAM_PARAMETERS) {
64 TEST_EXTENSION_FORKING_CONSTRUCTOR;
65
66 if (HasFatalFailure()) {
67 return;
68 }
69
70 CreateStream();
71 }
72
73 ~CameraBurstTest() {
74 TEST_EXTENSION_FORKING_DESTRUCTOR;
75
76 if (mDevice.get()) {
77 mDevice->waitUntilDrained();
78 }
79 DeleteStream();
80 }
81
82 virtual void SetUp() {
83 TEST_EXTENSION_FORKING_SET_UP;
84 }
85 virtual void TearDown() {
86 TEST_EXTENSION_FORKING_TEAR_DOWN;
87 }
88
89 /* this assumes the format is YUV420sp */
90 long long TotalBrightness(const CpuConsumer::LockedBuffer& imgBuffer,
91 int *underexposed,
92 int *overexposed) const {
93
94 const uint8_t* buf = imgBuffer.data;
95 size_t stride = imgBuffer.stride;
96
97 /* iterate over the Y plane only */
98 long long acc = 0;
99
100 *underexposed = 0;
101 *overexposed = 0;
102
103 for (size_t y = 0; y < imgBuffer.height; ++y) {
104 for (size_t x = 0; x < imgBuffer.width; ++x) {
105 const uint8_t p = buf[y * stride + x];
106
107 if (p == 0) {
108 if (underexposed) {
109 ++*underexposed;
110 }
111 continue;
112 } else if (p == 255) {
113 if (overexposed) {
114 ++*overexposed;
115 }
116 continue;
117 }
118
119 acc += p;
120 }
121 }
122
123 return acc;
124 }
Igor Murashkinf1b9ae72012-12-07 15:08:35 -0800125};
126
127TEST_F(CameraBurstTest, ManualExposureControl) {
128
129 TEST_EXTENSION_FORKING_INIT;
130
131 // Range of valid exposure times, in nanoseconds
132 int64_t minExp, maxExp;
133 {
134 camera_metadata_ro_entry exposureTimeRange =
135 GetStaticEntry(ANDROID_SENSOR_INFO_EXPOSURE_TIME_RANGE);
136
137 ASSERT_EQ(2u, exposureTimeRange.count);
138 minExp = exposureTimeRange.data.i64[0];
139 maxExp = exposureTimeRange.data.i64[1];
140 }
141
142 dout << "Min exposure is " << minExp;
143 dout << " max exposure is " << maxExp << std::endl;
144
145 // Calculate some set of valid exposure times for each request
146 int64_t exposures[CAMERA_FRAME_BURST_COUNT];
147 exposures[0] = CAMERA_EXPOSURE_STARTING;
148 for (int i = 1; i < CAMERA_FRAME_BURST_COUNT; ++i) {
149 exposures[i] = exposures[i-1] * CAMERA_EXPOSURE_DOUBLE;
150 }
151 // Our calculated exposure times should be in [minExp, maxExp]
152 EXPECT_LE(minExp, exposures[0])
153 << "Minimum exposure range is too high, wanted at most "
154 << exposures[0] << "ns";
155 EXPECT_GE(maxExp, exposures[CAMERA_FRAME_BURST_COUNT-1])
156 << "Maximum exposure range is too low, wanted at least "
157 << exposures[CAMERA_FRAME_BURST_COUNT-1] << "ns";
158
159 // Create a preview request, turning off all 3A
160 CameraMetadata previewRequest;
161 ASSERT_EQ(OK, mDevice->createDefaultRequest(CAMERA2_TEMPLATE_PREVIEW,
162 &previewRequest));
163 {
164 Vector<uint8_t> outputStreamIds;
165 outputStreamIds.push(mStreamId);
166 ASSERT_EQ(OK, previewRequest.update(ANDROID_REQUEST_OUTPUT_STREAMS,
167 outputStreamIds));
168
169 // Disable all 3A routines
170 uint8_t cmOff = static_cast<uint8_t>(ANDROID_CONTROL_MODE_OFF);
171 ASSERT_EQ(OK, previewRequest.update(ANDROID_CONTROL_MODE,
172 &cmOff, 1));
173 if (CAMERA_BURST_DEBUGGING) {
174 int frameCount = 0;
175 ASSERT_EQ(OK, previewRequest.update(ANDROID_REQUEST_FRAME_COUNT,
176 &frameCount, 1));
177 }
178 }
179
180 if (CAMERA_BURST_DEBUGGING) {
181 previewRequest.dump(STDOUT_FILENO);
182 }
183
184 // Submit capture requests
185 for (int i = 0; i < CAMERA_FRAME_BURST_COUNT; ++i) {
186 CameraMetadata tmpRequest = previewRequest;
187 ASSERT_EQ(OK, tmpRequest.update(ANDROID_SENSOR_EXPOSURE_TIME,
188 &exposures[i], 1));
189 ALOGV("Submitting capture request %d with exposure %lld", i,
190 exposures[i]);
191 dout << "Capture request " << i << " exposure is "
192 << (exposures[i]/1e6f) << std::endl;
193 ASSERT_EQ(OK, mDevice->capture(tmpRequest));
194 }
195
196 dout << "Buffer dimensions " << mWidth << "x" << mHeight << std::endl;
197
198 float brightnesses[CAMERA_FRAME_BURST_COUNT];
199 // Get each frame (metadata) and then the buffer. Calculate brightness.
200 for (int i = 0; i < CAMERA_FRAME_BURST_COUNT; ++i) {
201 ALOGV("Reading capture request %d with exposure %lld", i, exposures[i]);
202 ASSERT_EQ(OK, mDevice->waitForNextFrame(CAMERA_FRAME_TIMEOUT));
203 ALOGV("Reading capture request-1 %d", i);
204 CameraMetadata frameMetadata;
205 ASSERT_EQ(OK, mDevice->getNextFrame(&frameMetadata));
206 ALOGV("Reading capture request-2 %d", i);
207
208 ASSERT_EQ(OK, mFrameListener->waitForFrame(CAMERA_FRAME_TIMEOUT));
209 ALOGV("We got the frame now");
210
211 CpuConsumer::LockedBuffer imgBuffer;
212 ASSERT_EQ(OK, mCpuConsumer->lockNextBuffer(&imgBuffer));
213
214 int underexposed, overexposed;
215 long long brightness = TotalBrightness(imgBuffer, &underexposed,
216 &overexposed);
217 float avgBrightness = brightness * 1.0f /
218 (mWidth * mHeight - (underexposed + overexposed));
219 ALOGV("Total brightness for frame %d was %lld (underexposed %d, "
220 "overexposed %d), avg %f", i, brightness, underexposed,
221 overexposed, avgBrightness);
222 dout << "Average brightness (frame " << i << ") was " << avgBrightness
223 << " (underexposed " << underexposed << ", overexposed "
224 << overexposed << ")" << std::endl;
225
226 ASSERT_EQ(OK, mCpuConsumer->unlockBuffer(imgBuffer));
227
228 brightnesses[i] = avgBrightness;
229 }
230
231 // Calculate max consecutive frame exposure doubling
232 float prev = brightnesses[0];
233 int doubling_count = 1;
234 int max_doubling_count = 0;
235 for (int i = 1; i < CAMERA_FRAME_BURST_COUNT; ++i) {
236 if (fabs(brightnesses[i] - prev*CAMERA_EXPOSURE_DOUBLE)
237 <= CAMERA_EXPOSURE_DOUBLING_THRESHOLD) {
238 doubling_count++;
239 }
240 else {
241 max_doubling_count = std::max(max_doubling_count, doubling_count);
242 doubling_count = 1;
243 }
244 prev = brightnesses[i];
245 }
246
247 dout << "max doubling count: " << max_doubling_count << std::endl;
248
249 EXPECT_LE(CAMERA_EXPOSURE_DOUBLING_COUNT, max_doubling_count)
250 << "average brightness should double at least "
251 << CAMERA_EXPOSURE_DOUBLING_COUNT
252 << " times over each consecutive frame as the exposure is doubled";
253}
254
255}
256}
257}
258