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