1 /*
2  * Copyright 2023 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.pipe.integration.compat
18 
19 import android.hardware.camera2.params.DynamicRangeProfiles
20 import androidx.annotation.RequiresApi
21 import androidx.camera.camera2.pipe.integration.internal.DynamicRangeConversions
22 import androidx.camera.core.DynamicRange
23 import java.util.Collections
24 
25 @RequiresApi(33)
26 internal class DynamicRangeProfilesCompatApi33Impl(
27     private val dynamicRangeProfiles: DynamicRangeProfiles
28 ) : DynamicRangeProfilesCompat.DynamicRangeProfilesCompatImpl {
29     override val supportedDynamicRanges: Set<DynamicRange>
30         get() = profileSetToDynamicRangeSet(dynamicRangeProfiles.supportedProfiles)
31 
getDynamicRangeCaptureRequestConstraintsnull32     override fun getDynamicRangeCaptureRequestConstraints(
33         dynamicRange: DynamicRange
34     ): Set<DynamicRange> {
35         val dynamicRangeProfile = dynamicRangeToFirstSupportedProfile(dynamicRange)
36         require(dynamicRangeProfile != null) { "DynamicRange is not supported: $dynamicRange" }
37         return profileSetToDynamicRangeSet(
38             dynamicRangeProfiles.getProfileCaptureRequestConstraints(dynamicRangeProfile)
39         )
40     }
41 
isExtraLatencyPresentnull42     override fun isExtraLatencyPresent(dynamicRange: DynamicRange): Boolean {
43         val dynamicRangeProfile = dynamicRangeToFirstSupportedProfile(dynamicRange)
44         require(dynamicRangeProfile != null) { "DynamicRange is not supported: $dynamicRange" }
45         return dynamicRangeProfiles.isExtraLatencyPresent(dynamicRangeProfile)
46     }
47 
unwrapnull48     override fun unwrap() = dynamicRangeProfiles
49 
50     private fun dynamicRangeToFirstSupportedProfile(dynamicRange: DynamicRange) =
51         DynamicRangeConversions.dynamicRangeToFirstSupportedProfile(
52             dynamicRange,
53             dynamicRangeProfiles
54         )
55 
56     private fun profileToDynamicRange(profile: Long): DynamicRange {
57         val result = DynamicRangeConversions.profileToDynamicRange(profile)
58         require(result != null) {
59             "Dynamic range profile cannot be converted to a DynamicRange object: $profile"
60         }
61         return result
62     }
63 
profileSetToDynamicRangeSetnull64     private fun profileSetToDynamicRangeSet(profileSet: Set<Long>): Set<DynamicRange> {
65         if (profileSet.isEmpty()) {
66             return emptySet()
67         }
68         val dynamicRangeSet: MutableSet<DynamicRange> = mutableSetOf()
69         for (profile in profileSet) {
70             dynamicRangeSet.add(profileToDynamicRange(profile))
71         }
72         return Collections.unmodifiableSet(dynamicRangeSet)
73     }
74 }
75