blob: 356ed370c033b56c4a835027e3f71687b8d05330 [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 }
125
126protected:
127
128 camera_metadata_ro_entry GetStaticEntry(uint32_t tag) const {
129 const CameraMetadata& staticInfo = mDevice->info();
130 camera_metadata_ro_entry entry = staticInfo.find(tag);
131 return entry;
132 }
133
134
135};
136
137TEST_F(CameraBurstTest, ManualExposureControl) {
138
139 TEST_EXTENSION_FORKING_INIT;
140
141 // Range of valid exposure times, in nanoseconds
142 int64_t minExp, maxExp;
143 {
144 camera_metadata_ro_entry exposureTimeRange =
145 GetStaticEntry(ANDROID_SENSOR_INFO_EXPOSURE_TIME_RANGE);
146
147 ASSERT_EQ(2u, exposureTimeRange.count);
148 minExp = exposureTimeRange.data.i64[0];
149 maxExp = exposureTimeRange.data.i64[1];
150 }
151
152 dout << "Min exposure is " << minExp;
153 dout << " max exposure is " << maxExp << std::endl;
154
155 // Calculate some set of valid exposure times for each request
156 int64_t exposures[CAMERA_FRAME_BURST_COUNT];
157 exposures[0] = CAMERA_EXPOSURE_STARTING;
158 for (int i = 1; i < CAMERA_FRAME_BURST_COUNT; ++i) {
159 exposures[i] = exposures[i-1] * CAMERA_EXPOSURE_DOUBLE;
160 }
161 // Our calculated exposure times should be in [minExp, maxExp]
162 EXPECT_LE(minExp, exposures[0])
163 << "Minimum exposure range is too high, wanted at most "
164 << exposures[0] << "ns";
165 EXPECT_GE(maxExp, exposures[CAMERA_FRAME_BURST_COUNT-1])
166 << "Maximum exposure range is too low, wanted at least "
167 << exposures[CAMERA_FRAME_BURST_COUNT-1] << "ns";
168
169 // Create a preview request, turning off all 3A
170 CameraMetadata previewRequest;
171 ASSERT_EQ(OK, mDevice->createDefaultRequest(CAMERA2_TEMPLATE_PREVIEW,
172 &previewRequest));
173 {
174 Vector<uint8_t> outputStreamIds;
175 outputStreamIds.push(mStreamId);
176 ASSERT_EQ(OK, previewRequest.update(ANDROID_REQUEST_OUTPUT_STREAMS,
177 outputStreamIds));
178
179 // Disable all 3A routines
180 uint8_t cmOff = static_cast<uint8_t>(ANDROID_CONTROL_MODE_OFF);
181 ASSERT_EQ(OK, previewRequest.update(ANDROID_CONTROL_MODE,
182 &cmOff, 1));
183 if (CAMERA_BURST_DEBUGGING) {
184 int frameCount = 0;
185 ASSERT_EQ(OK, previewRequest.update(ANDROID_REQUEST_FRAME_COUNT,
186 &frameCount, 1));
187 }
188 }
189
190 if (CAMERA_BURST_DEBUGGING) {
191 previewRequest.dump(STDOUT_FILENO);
192 }
193
194 // Submit capture requests
195 for (int i = 0; i < CAMERA_FRAME_BURST_COUNT; ++i) {
196 CameraMetadata tmpRequest = previewRequest;
197 ASSERT_EQ(OK, tmpRequest.update(ANDROID_SENSOR_EXPOSURE_TIME,
198 &exposures[i], 1));
199 ALOGV("Submitting capture request %d with exposure %lld", i,
200 exposures[i]);
201 dout << "Capture request " << i << " exposure is "
202 << (exposures[i]/1e6f) << std::endl;
203 ASSERT_EQ(OK, mDevice->capture(tmpRequest));
204 }
205
206 dout << "Buffer dimensions " << mWidth << "x" << mHeight << std::endl;
207
208 float brightnesses[CAMERA_FRAME_BURST_COUNT];
209 // Get each frame (metadata) and then the buffer. Calculate brightness.
210 for (int i = 0; i < CAMERA_FRAME_BURST_COUNT; ++i) {
211 ALOGV("Reading capture request %d with exposure %lld", i, exposures[i]);
212 ASSERT_EQ(OK, mDevice->waitForNextFrame(CAMERA_FRAME_TIMEOUT));
213 ALOGV("Reading capture request-1 %d", i);
214 CameraMetadata frameMetadata;
215 ASSERT_EQ(OK, mDevice->getNextFrame(&frameMetadata));
216 ALOGV("Reading capture request-2 %d", i);
217
218 ASSERT_EQ(OK, mFrameListener->waitForFrame(CAMERA_FRAME_TIMEOUT));
219 ALOGV("We got the frame now");
220
221 CpuConsumer::LockedBuffer imgBuffer;
222 ASSERT_EQ(OK, mCpuConsumer->lockNextBuffer(&imgBuffer));
223
224 int underexposed, overexposed;
225 long long brightness = TotalBrightness(imgBuffer, &underexposed,
226 &overexposed);
227 float avgBrightness = brightness * 1.0f /
228 (mWidth * mHeight - (underexposed + overexposed));
229 ALOGV("Total brightness for frame %d was %lld (underexposed %d, "
230 "overexposed %d), avg %f", i, brightness, underexposed,
231 overexposed, avgBrightness);
232 dout << "Average brightness (frame " << i << ") was " << avgBrightness
233 << " (underexposed " << underexposed << ", overexposed "
234 << overexposed << ")" << std::endl;
235
236 ASSERT_EQ(OK, mCpuConsumer->unlockBuffer(imgBuffer));
237
238 brightnesses[i] = avgBrightness;
239 }
240
241 // Calculate max consecutive frame exposure doubling
242 float prev = brightnesses[0];
243 int doubling_count = 1;
244 int max_doubling_count = 0;
245 for (int i = 1; i < CAMERA_FRAME_BURST_COUNT; ++i) {
246 if (fabs(brightnesses[i] - prev*CAMERA_EXPOSURE_DOUBLE)
247 <= CAMERA_EXPOSURE_DOUBLING_THRESHOLD) {
248 doubling_count++;
249 }
250 else {
251 max_doubling_count = std::max(max_doubling_count, doubling_count);
252 doubling_count = 1;
253 }
254 prev = brightnesses[i];
255 }
256
257 dout << "max doubling count: " << max_doubling_count << std::endl;
258
259 EXPECT_LE(CAMERA_EXPOSURE_DOUBLING_COUNT, max_doubling_count)
260 << "average brightness should double at least "
261 << CAMERA_EXPOSURE_DOUBLING_COUNT
262 << " times over each consecutive frame as the exposure is doubled";
263}
264
265}
266}
267}
268