1 /*
2  * Copyright 2022 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 package androidx.camera.camera2.internal.compat.params;
18 
19 import android.hardware.camera2.params.DynamicRangeProfiles;
20 
21 import androidx.annotation.RequiresApi;
22 import androidx.camera.core.DynamicRange;
23 import androidx.core.util.Preconditions;
24 
25 import org.jspecify.annotations.NonNull;
26 import org.jspecify.annotations.Nullable;
27 
28 import java.util.Collections;
29 import java.util.HashSet;
30 import java.util.Set;
31 
32 @RequiresApi(33)
33 class DynamicRangesCompatApi33Impl implements DynamicRangesCompat.DynamicRangeProfilesCompatImpl {
34     private final DynamicRangeProfiles mDynamicRangeProfiles;
35 
DynamicRangesCompatApi33Impl(@onNull Object dynamicRangeProfiles)36     DynamicRangesCompatApi33Impl(@NonNull Object dynamicRangeProfiles) {
37         mDynamicRangeProfiles = (DynamicRangeProfiles) dynamicRangeProfiles;
38     }
39 
40     @Override
getDynamicRangeCaptureRequestConstraints( @onNull DynamicRange dynamicRange)41     public @NonNull Set<DynamicRange> getDynamicRangeCaptureRequestConstraints(
42             @NonNull DynamicRange dynamicRange) {
43         Long dynamicRangeProfile = dynamicRangeToFirstSupportedProfile(dynamicRange);
44         Preconditions.checkArgument(dynamicRangeProfile != null,
45                 "DynamicRange is not supported: " + dynamicRange);
46         return profileSetToDynamicRangeSet(
47                 mDynamicRangeProfiles.getProfileCaptureRequestConstraints(dynamicRangeProfile));
48     }
49 
50     @Override
getSupportedDynamicRanges()51     public @NonNull Set<DynamicRange> getSupportedDynamicRanges() {
52         return profileSetToDynamicRangeSet(mDynamicRangeProfiles.getSupportedProfiles());
53     }
54 
55     @Override
isExtraLatencyPresent(@onNull DynamicRange dynamicRange)56     public boolean isExtraLatencyPresent(@NonNull DynamicRange dynamicRange) {
57         Long dynamicRangeProfile = dynamicRangeToFirstSupportedProfile(dynamicRange);
58         Preconditions.checkArgument(dynamicRangeProfile != null,
59                 "DynamicRange is not supported: " + dynamicRange);
60         return mDynamicRangeProfiles.isExtraLatencyPresent(dynamicRangeProfile);
61     }
62 
63     @Override
unwrap()64     public @Nullable DynamicRangeProfiles unwrap() {
65         return mDynamicRangeProfiles;
66     }
67 
profileToDynamicRange(long profile)68     private static @NonNull DynamicRange profileToDynamicRange(long profile) {
69         return Preconditions.checkNotNull(DynamicRangeConversions.profileToDynamicRange(profile),
70                 "Dynamic range profile cannot be converted to a DynamicRange object: " + profile);
71     }
72 
dynamicRangeToFirstSupportedProfile(@onNull DynamicRange dynamicRange)73     private @Nullable Long dynamicRangeToFirstSupportedProfile(@NonNull DynamicRange dynamicRange) {
74         return DynamicRangeConversions.dynamicRangeToFirstSupportedProfile(dynamicRange,
75                 mDynamicRangeProfiles);
76     }
77 
profileSetToDynamicRangeSet( @onNull Set<Long> profileSet)78     private static @NonNull Set<DynamicRange> profileSetToDynamicRangeSet(
79             @NonNull Set<Long> profileSet) {
80         if (profileSet.isEmpty()) {
81             return Collections.emptySet();
82         }
83         Set<DynamicRange> dynamicRangeSet = new HashSet<>(profileSet.size());
84         for (long profile : profileSet) {
85             dynamicRangeSet.add(profileToDynamicRange(profile));
86         }
87         return Collections.unmodifiableSet(dynamicRangeSet);
88     }
89 }
90