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"""Verifies EV compensation is applied.""" 15 16 17import logging 18import math 19import os.path 20import matplotlib 21from matplotlib import pylab 22from mobly import test_runner 23 24import its_base_test 25import camera_properties_utils 26import capture_request_utils 27import image_processing_utils 28import its_session_utils 29 30_LINEAR_TONEMAP_CURVE = [0.0, 0.0, 1.0, 1.0] 31_LOCKED = 3 32_LUMA_DELTA_ATOL = 0.05 33_LUMA_DELTA_ATOL_SAT = 0.10 34_LUMA_SAT_THRESH = 0.75 # luma value at which ATOL changes from MID to SAT 35_NAME = os.path.splitext(os.path.basename(__file__))[0] 36_PATCH_H = 0.1 # center 10% 37_PATCH_W = 0.1 38_PATCH_X = 0.5 - _PATCH_W/2 39_PATCH_Y = 0.5 - _PATCH_H/2 40_THRESH_CONVERGE_FOR_EV = 8 # AE must converge within this num auto reqs for EV 41 42 43def create_request_with_ev(ev): 44 """Create request with the ev compensation step.""" 45 req = capture_request_utils.auto_capture_request() 46 req['android.control.aeExposureCompensation'] = ev 47 req['android.control.aeLock'] = True 48 req['android.control.awbLock'] = True 49 # Use linear tonemap to avoid brightness being impacted by tone curves. 50 req['android.tonemap.mode'] = 0 51 req['android.tonemap.curve'] = {'red': _LINEAR_TONEMAP_CURVE, 52 'green': _LINEAR_TONEMAP_CURVE, 53 'blue': _LINEAR_TONEMAP_CURVE} 54 return req 55 56 57def create_ev_comp_changes(props): 58 """Create the ev compensation steps and shifts from control params.""" 59 ev_compensation_range = props['android.control.aeCompensationRange'] 60 range_min = ev_compensation_range[0] 61 range_max = ev_compensation_range[1] 62 ev_per_step = capture_request_utils.rational_to_float( 63 props['android.control.aeCompensationStep']) 64 logging.debug('ev_step_size_in_stops: %.3f', ev_per_step) 65 steps_per_ev = int(round(1.0 / ev_per_step)) 66 ev_steps = range(range_min, range_max + 1, steps_per_ev) 67 ev_shifts = [pow(2, step * ev_per_step) for step in ev_steps] 68 return ev_steps, ev_shifts 69 70 71class EvCompensationAdvancedTest(its_base_test.ItsBaseTest): 72 """Tests that EV compensation is applied.""" 73 74 def test_ev_compensation_advanced(self): 75 logging.debug('Starting %s', _NAME) 76 with its_session_utils.ItsSession( 77 device_id=self.dut.serial, 78 camera_id=self.camera_id, 79 hidden_physical_id=self.hidden_physical_id) as cam: 80 props = cam.get_camera_properties() 81 props = cam.override_with_hidden_physical_camera_props(props) 82 log_path = self.log_path 83 84 # check SKIP conditions 85 camera_properties_utils.skip_unless( 86 camera_properties_utils.ev_compensation(props) and 87 camera_properties_utils.manual_sensor(props) and 88 camera_properties_utils.manual_post_proc(props) and 89 camera_properties_utils.per_frame_control(props) and 90 camera_properties_utils.ae_lock(props) and 91 camera_properties_utils.awb_lock(props)) 92 93 # Load chart for scene 94 its_session_utils.load_scene( 95 cam, props, self.scene, self.tablet, 96 its_session_utils.CHART_DISTANCE_NO_SCALING) 97 98 # Create ev compensation changes 99 ev_steps, ev_shifts = create_ev_comp_changes(props) 100 101 # Converge 3A, and lock AE once converged. skip AF trigger as 102 # dark/bright scene could make AF convergence fail and this test 103 # doesn't care the image sharpness. 104 mono_camera = camera_properties_utils.mono_camera(props) 105 cam.do_3a(ev_comp=0, lock_ae=True, lock_awb=True, do_af=False, 106 mono_camera=mono_camera) 107 108 # Create requests and capture 109 largest_yuv = capture_request_utils.get_largest_yuv_format(props) 110 match_ar = (largest_yuv['width'], largest_yuv['height']) 111 fmt = capture_request_utils.get_near_vga_yuv_format( 112 props, match_ar=match_ar) 113 lumas = [] 114 for ev in ev_steps: 115 # Capture a single shot with the same EV comp and locked AE. 116 req = create_request_with_ev(ev) 117 caps = cam.do_capture([req]*_THRESH_CONVERGE_FOR_EV, fmt) 118 for cap in caps: 119 if cap['metadata']['android.control.aeState'] == _LOCKED: 120 ev_meta = cap['metadata']['android.control.aeExposureCompensation'] 121 if ev_meta != ev: 122 raise AssertionError( 123 f'EV comp capture != request! cap: {ev_meta}, req: {ev}') 124 lumas.append(image_processing_utils.extract_luma_from_patch( 125 cap, _PATCH_X, _PATCH_Y, _PATCH_W, _PATCH_H)) 126 break 127 if caps[_THRESH_CONVERGE_FOR_EV-1]['metadata'][ 128 'android.control.aeState'] != _LOCKED: 129 raise AssertionError('AE does not reach locked state in ' 130 f'{_THRESH_CONVERGE_FOR_EV} frames.') 131 logging.debug('lumas in AE locked captures: %s', str(lumas)) 132 133 i_mid = len(ev_steps) // 2 134 luma_normal = lumas[i_mid] / ev_shifts[i_mid] 135 expected_lumas = [min(1.0, luma_normal*shift) for shift in ev_shifts] 136 luma_delta_atols = [_LUMA_DELTA_ATOL if l < _LUMA_SAT_THRESH 137 else _LUMA_DELTA_ATOL_SAT for l in expected_lumas] 138 139 # Create plot 140 pylab.figure(_NAME) 141 pylab.plot(ev_steps, lumas, '-ro', label='measured', alpha=0.7) 142 pylab.plot(ev_steps, expected_lumas, '-bo', label='expected', alpha=0.7) 143 pylab.title(_NAME) 144 pylab.xlabel('EV Compensation') 145 pylab.ylabel('Mean Luma (Normalized)') 146 pylab.legend(loc='lower right', numpoints=1, fancybox=True) 147 name_with_log_path = os.path.join(log_path, _NAME) 148 matplotlib.pyplot.savefig(f'{name_with_log_path}_plot_means.png') 149 150 for i, luma in enumerate(lumas): 151 luma_delta_atol = luma_delta_atols[i] 152 logging.debug('EV step: %3d, luma: %.3f, model: %.3f, ATOL: %.2f', 153 ev_steps[i], luma, expected_lumas[i], luma_delta_atol) 154 if not math.isclose(luma, expected_lumas[i], 155 abs_tol=luma_delta_atol): 156 raise AssertionError('Modeled/measured luma deltas too large! ' 157 f'meas: {lumas[i]}, model: {expected_lumas[i]}, ' 158 f'ATOL: {luma_delta_atol}.') 159 160 161if __name__ == '__main__': 162 test_runner.main() 163