1# Copyright 2020 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"""Verifies 3A converges in CONTINUOUS_PICTURE mode."""
15
16
17import logging
18import os.path
19
20from mobly import test_runner
21
22import its_base_test
23import camera_properties_utils
24import capture_request_utils
25import image_processing_utils
26import its_session_utils
27
28_CONTINUOUS_PICTURE_MODE = 4
29_CONVERGED_3A = (2, 2, 2)  # (AE, AF, AWB)
30_M_TO_CM = 100
31_NAME = os.path.splitext(os.path.basename(__file__))[0]
32_NS_TO_MS = 1E-6
33_NUM_FRAMES = 50
34_PATCH_H = 0.1  # center 10%
35_PATCH_W = 0.1
36_PATCH_X = 0.5 - _PATCH_W/2
37_PATCH_Y = 0.5 - _PATCH_H/2
38_RGB_G_CH = 1
39_VGA_W, _VGA_H = 640, 480
40
41
42def _capture_frames(cam):
43  """Captures frames, logs info, and creates cap_3a_state_list.
44
45  Args:
46    cam: a camera capture object.
47
48  Returns:
49    cap_3a_state_list: list of 3a states [AE, AF, AWB] during captures.
50    imgs: list of images indexed by frame.
51  """
52  cap_3a_state_list = []
53  req = capture_request_utils.auto_capture_request()
54  req['android.control.afMode'] = _CONTINUOUS_PICTURE_MODE
55  fmt = {'format': 'yuv', 'width': _VGA_W, 'height': _VGA_H}
56  caps = cam.do_capture([req]*_NUM_FRAMES, fmt)
57
58  # Extract frame metadata and frame.
59  imgs = []
60  for i, cap in enumerate(caps):
61    md = cap['metadata']
62    exp = md['android.sensor.exposureTime']
63    iso = md['android.sensor.sensitivity']
64    fd = md['android.lens.focalLength']
65    ae_state = md['android.control.aeState']
66    af_state = md['android.control.afState']
67    awb_state = md['android.control.awbState']
68    fd_str = 'infinity'
69    if fd != 0.0:
70      fd_str = f'{_M_TO_CM/fd:.2f}cm'
71    img = image_processing_utils.convert_capture_to_rgb_image(cap)
72    patch = image_processing_utils.get_image_patch(
73        img, _PATCH_X, _PATCH_Y, _PATCH_W, _PATCH_H)
74    green_mean = image_processing_utils.compute_image_means(patch)[_RGB_G_CH]
75    logging.debug(
76        '%d, iso: %d, exp: %.2fms, fd: %s, avg: %.3f, [ae,af,awb]'
77        ': [%d,%d,%d]', i, iso, exp * _NS_TO_MS, fd_str, green_mean, ae_state,
78        af_state, awb_state)
79    cap_3a_state_list.append([ae_state, af_state, awb_state])
80    imgs.append(img)
81  return cap_3a_state_list, imgs
82
83
84class ContinuousPictureTest(its_base_test.ItsBaseTest):
85  """Test 3A converges in CONTINUOUS_PICTURE mode.
86
87  Sets camera into CONTINUOUS_PICTURE mode and does NUM_FRAMES capture.
88  By the end of NUM_FRAMES capture, 3A should be in converged state.
89  Converged state is [2, 2, 2] for [AE, AF, AWB]
90
91  State information:
92    AE_STATES: {0: INACTIVE, 1: SEARCHING, 2: CONVERGED, 3: LOCKED,
93                4: FLASH_REQ, 5: PRECAPTURE}
94    AF_STATES: {0: INACTIVE, 1: PASSIVE_SCAN, 2: PASSIVE_FOCUSED,
95                3: ACTIVE_SCAN, 4: FOCUS_LOCKED, 5: NOT_FOCUSED_LOCKED,
96                6: PASSIVE_UNFOCUSED}
97    AWB_STATES: {0: INACTIVE, 1: SEARCHING, 2: CONVERGED, 3: LOCKED}
98  """
99
100  def test_continuous_picture(self):
101    with its_session_utils.ItsSession(
102        device_id=self.dut.serial,
103        camera_id=self.camera_id,
104        hidden_physical_id=self.hidden_physical_id) as cam:
105      props = cam.get_camera_properties()
106      props = cam.override_with_hidden_physical_camera_props(props)
107
108      # Check SKIP conditions.
109      camera_properties_utils.skip_unless(
110          camera_properties_utils.continuous_picture(props) and
111          camera_properties_utils.read_3a(props))
112
113      # Load chart for scene.
114      its_session_utils.load_scene(
115          cam, props, self.scene, self.tablet, self.chart_distance)
116
117      # Do 3A up front.
118      cam.do_3a()
119
120      # Ensure 3A settles in CONTINUOUS_PICTURE mode with no scene change.
121      cap_3a_state_list, imgs = _capture_frames(cam)
122      final_3a = cap_3a_state_list[_NUM_FRAMES-1]
123      if final_3a != list(_CONVERGED_3A):
124        for i, img in enumerate(imgs):
125          image_processing_utils.write_image(
126              img, f'{os.path.join(self.log_path, _NAME)}_{i}.jpg')
127        raise AssertionError(
128            f'Last frame [AE,AF,AWB]: {final_3a}. CONVERGED: {_CONVERGED_3A}.')
129
130if __name__ == '__main__':
131  test_runner.main()
132
133