1# Copyright 2014 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import its.device
16import its.caps
17import its.objects
18import its.target
19
20AE_FRAMES_PER_ITERATION = 8
21AE_CONVERGE_ITERATIONS = 5
22# AE must converge within this number of auto requests under scene1
23THRESH_AE_CONVERGE = AE_FRAMES_PER_ITERATION * AE_CONVERGE_ITERATIONS
24
25def main():
26    """Test the AE state machine when using the precapture trigger.
27    """
28
29    INACTIVE = 0
30    SEARCHING = 1
31    CONVERGED = 2
32    LOCKED = 3
33    FLASHREQUIRED = 4
34    PRECAPTURE = 5
35
36    with its.device.ItsSession() as cam:
37        props = cam.get_camera_properties()
38        its.caps.skip_unless(its.caps.compute_target_exposure(props) and
39                             its.caps.per_frame_control(props))
40
41        _,fmt = its.objects.get_fastest_manual_capture_settings(props)
42
43        # Capture 5 manual requests, with AE disabled, and the last request
44        # has an AE precapture trigger (which should be ignored since AE is
45        # disabled).
46        manual_reqs = []
47        e, s = its.target.get_target_exposure_combos(cam)["midExposureTime"]
48        manual_req = its.objects.manual_capture_request(s,e)
49        manual_req['android.control.aeMode'] = 0 # Off
50        manual_reqs += [manual_req]*4
51        precap_req = its.objects.manual_capture_request(s,e)
52        precap_req['android.control.aeMode'] = 0 # Off
53        precap_req['android.control.aePrecaptureTrigger'] = 1 # Start
54        manual_reqs.append(precap_req)
55        caps = cam.do_capture(manual_reqs, fmt)
56        for cap in caps:
57            assert(cap['metadata']['android.control.aeState'] == INACTIVE)
58
59        # Capture an auto request and verify the AE state; no trigger.
60        auto_req = its.objects.auto_capture_request()
61        auto_req['android.control.aeMode'] = 1  # On
62        cap = cam.do_capture(auto_req, fmt)
63        state = cap['metadata']['android.control.aeState']
64        print "AE state after auto request:", state
65        assert(state in [SEARCHING, CONVERGED])
66
67        # Capture with auto request with a precapture trigger.
68        auto_req['android.control.aePrecaptureTrigger'] = 1  # Start
69        cap = cam.do_capture(auto_req, fmt)
70        state = cap['metadata']['android.control.aeState']
71        print "AE state after auto request with precapture trigger:", state
72        assert(state in [SEARCHING, CONVERGED, PRECAPTURE])
73
74        # Capture some more auto requests, and AE should converge.
75        auto_req['android.control.aePrecaptureTrigger'] = 0
76        for i in range(AE_CONVERGE_ITERATIONS):
77            caps = cam.do_capture([auto_req] * AE_FRAMES_PER_ITERATION, fmt)
78            state = caps[-1]['metadata']['android.control.aeState']
79            print "AE state after auto request:", state
80            if state == CONVERGED:
81                return
82        assert(state == CONVERGED)
83
84if __name__ == '__main__':
85    main()
86