summaryrefslogtreecommitdiff
path: root/tests/camera2/CameraBurstTests.cpp
blob: 5c4b6e7f0995b05a4c558071c3827d22602c7bef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <gtest/gtest.h>

#define LOG_TAG "CameraBurstTest"
//#define LOG_NDEBUG 0
#include <utils/Log.h>

#include <cmath>

#include "CameraStreamFixture.h"
#include "TestExtensions.h"

#define CAMERA_FRAME_TIMEOUT    1000000000 //nsecs (1 secs)
#define CAMERA_HEAP_COUNT       2 //HALBUG: 1 means registerBuffers fails
#define CAMERA_BURST_DEBUGGING  0
#define CAMERA_FRAME_BURST_COUNT 10

/* constants for the exposure test */
#define CAMERA_EXPOSURE_DOUBLE  2
#define CAMERA_EXPOSURE_DOUBLING_THRESHOLD 1.0f
#define CAMERA_EXPOSURE_DOUBLING_COUNT 4
#define CAMERA_EXPOSURE_FORMAT CAMERA_STREAM_AUTO_CPU_FORMAT
#define CAMERA_EXPOSURE_STARTING 100000 // 1/10ms, up to 51.2ms with 10 steps

#if CAMERA_BURST_DEBUGGING
#define dout std::cout
#else
#define dout if (0) std::cout
#endif

using namespace android;
using namespace android::camera2;

namespace android {
namespace camera2 {
namespace tests {

static CameraStreamParams STREAM_PARAMETERS = {
    /*mFormat*/     CAMERA_EXPOSURE_FORMAT,
    /*mHeapCount*/  CAMERA_HEAP_COUNT
};

class CameraBurstTest
    : public ::testing::Test,
      public CameraStreamFixture {

public:
    CameraBurstTest() : CameraStreamFixture(STREAM_PARAMETERS) {
        TEST_EXTENSION_FORKING_CONSTRUCTOR;

        if (HasFatalFailure()) {
            return;
        }

        CreateStream();
    }

    ~CameraBurstTest() {
        TEST_EXTENSION_FORKING_DESTRUCTOR;

        if (mDevice.get()) {
            mDevice->waitUntilDrained();
        }
        DeleteStream();
    }

    virtual void SetUp() {
        TEST_EXTENSION_FORKING_SET_UP;
    }
    virtual void TearDown() {
        TEST_EXTENSION_FORKING_TEAR_DOWN;
    }

    /* this assumes the format is YUV420sp or flexible YUV */
    long long TotalBrightness(const CpuConsumer::LockedBuffer& imgBuffer,
                              int *underexposed,
                              int *overexposed) const {

        const uint8_t* buf = imgBuffer.data;
        size_t stride = imgBuffer.stride;

        /* iterate over the Y plane only */
        long long acc = 0;

        *underexposed = 0;
        *overexposed = 0;

        for (size_t y = 0; y < imgBuffer.height; ++y) {
            for (size_t x = 0; x < imgBuffer.width; ++x) {
                const uint8_t p = buf[y * stride + x];

                if (p == 0) {
                    if (underexposed) {
                        ++*underexposed;
                    }
                    continue;
                } else if (p == 255) {
                    if (overexposed) {
                        ++*overexposed;
                    }
                    continue;
                }

                acc += p;
            }
        }

        return acc;
    }
};

TEST_F(CameraBurstTest, ManualExposureControl) {

    TEST_EXTENSION_FORKING_INIT;

    // Range of valid exposure times, in nanoseconds
    int64_t minExp, maxExp;
    {
        camera_metadata_ro_entry exposureTimeRange =
            GetStaticEntry(ANDROID_SENSOR_INFO_EXPOSURE_TIME_RANGE);

        ASSERT_EQ(2u, exposureTimeRange.count);
        minExp = exposureTimeRange.data.i64[0];
        maxExp = exposureTimeRange.data.i64[1];
    }

    dout << "Min exposure is " << minExp;
    dout << " max exposure is " << maxExp << std::endl;

    // Calculate some set of valid exposure times for each request
    int64_t exposures[CAMERA_FRAME_BURST_COUNT];
    exposures[0] = CAMERA_EXPOSURE_STARTING;
    for (int i = 1; i < CAMERA_FRAME_BURST_COUNT; ++i) {
        exposures[i] = exposures[i-1] * CAMERA_EXPOSURE_DOUBLE;
    }
    // Our calculated exposure times should be in [minExp, maxExp]
    EXPECT_LE(minExp, exposures[0])
        << "Minimum exposure range is too high, wanted at most "
        << exposures[0] << "ns";
    EXPECT_GE(maxExp, exposures[CAMERA_FRAME_BURST_COUNT-1])
        << "Maximum exposure range is too low, wanted at least "
        << exposures[CAMERA_FRAME_BURST_COUNT-1] << "ns";

    // Create a preview request, turning off all 3A
    CameraMetadata previewRequest;
    ASSERT_EQ(OK, mDevice->createDefaultRequest(CAMERA2_TEMPLATE_PREVIEW,
                                                &previewRequest));
    {
        Vector<uint8_t> outputStreamIds;
        outputStreamIds.push(mStreamId);
        ASSERT_EQ(OK, previewRequest.update(ANDROID_REQUEST_OUTPUT_STREAMS,
                                            outputStreamIds));

        // Disable all 3A routines
        uint8_t cmOff = static_cast<uint8_t>(ANDROID_CONTROL_MODE_OFF);
        ASSERT_EQ(OK, previewRequest.update(ANDROID_CONTROL_MODE,
                                            &cmOff, 1));

        int requestId = 1;
        ASSERT_EQ(OK, previewRequest.update(ANDROID_REQUEST_ID,
                                            &requestId, 1));

        if (CAMERA_BURST_DEBUGGING) {
            int frameCount = 0;
            ASSERT_EQ(OK, previewRequest.update(ANDROID_REQUEST_FRAME_COUNT,
                                                &frameCount, 1));
        }
    }

    if (CAMERA_BURST_DEBUGGING) {
        previewRequest.dump(STDOUT_FILENO);
    }

    // Submit capture requests
    for (int i = 0; i < CAMERA_FRAME_BURST_COUNT; ++i) {
        CameraMetadata tmpRequest = previewRequest;
        ASSERT_EQ(OK, tmpRequest.update(ANDROID_SENSOR_EXPOSURE_TIME,
                                        &exposures[i], 1));
        ALOGV("Submitting capture request %d with exposure %lld", i,
            exposures[i]);
        dout << "Capture request " << i << " exposure is "
             << (exposures[i]/1e6f) << std::endl;
        ASSERT_EQ(OK, mDevice->capture(tmpRequest));
    }

    dout << "Buffer dimensions " << mWidth << "x" << mHeight << std::endl;

    float brightnesses[CAMERA_FRAME_BURST_COUNT];
    // Get each frame (metadata) and then the buffer. Calculate brightness.
    for (int i = 0; i < CAMERA_FRAME_BURST_COUNT; ++i) {
        ALOGV("Reading capture request %d with exposure %lld", i, exposures[i]);
        ASSERT_EQ(OK, mDevice->waitForNextFrame(CAMERA_FRAME_TIMEOUT));
        ALOGV("Reading capture request-1 %d", i);
        CameraMetadata frameMetadata;
        ASSERT_EQ(OK, mDevice->getNextFrame(&frameMetadata));
        ALOGV("Reading capture request-2 %d", i);

        ASSERT_EQ(OK, mFrameListener->waitForFrame(CAMERA_FRAME_TIMEOUT));
        ALOGV("We got the frame now");

        CpuConsumer::LockedBuffer imgBuffer;
        ASSERT_EQ(OK, mCpuConsumer->lockNextBuffer(&imgBuffer));

        int underexposed, overexposed;
        long long brightness = TotalBrightness(imgBuffer, &underexposed,
                                               &overexposed);
        float avgBrightness = brightness * 1.0f /
                              (mWidth * mHeight - (underexposed + overexposed));
        ALOGV("Total brightness for frame %d was %lld (underexposed %d, "
              "overexposed %d), avg %f", i, brightness, underexposed,
              overexposed, avgBrightness);
        dout << "Average brightness (frame " << i << ") was " << avgBrightness
             << " (underexposed " << underexposed << ", overexposed "
             << overexposed << ")" << std::endl;

        ASSERT_EQ(OK, mCpuConsumer->unlockBuffer(imgBuffer));

        brightnesses[i] = avgBrightness;
    }

    // Calculate max consecutive frame exposure doubling
    float prev = brightnesses[0];
    int doubling_count = 1;
    int max_doubling_count = 0;
    for (int i = 1; i < CAMERA_FRAME_BURST_COUNT; ++i) {
        if (fabs(brightnesses[i] - prev*CAMERA_EXPOSURE_DOUBLE)
            <= CAMERA_EXPOSURE_DOUBLING_THRESHOLD) {
            doubling_count++;
        }
        else {
            max_doubling_count = std::max(max_doubling_count, doubling_count);
            doubling_count = 1;
        }
        prev = brightnesses[i];
    }

    dout << "max doubling count: " << max_doubling_count << std::endl;

    EXPECT_LE(CAMERA_EXPOSURE_DOUBLING_COUNT, max_doubling_count)
      << "average brightness should double at least "
      << CAMERA_EXPOSURE_DOUBLING_COUNT
      << " times over each consecutive frame as the exposure is doubled";
}

}
}
}