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.quirk
18 
19 import android.annotation.SuppressLint
20 import android.os.Build
21 import androidx.camera.core.impl.Quirk
22 
23 /**
24  * Quirk needed on devices where not closing the camera device can lead to undesirable behaviors,
25  * such as switching to a new session without closing the camera device may cause native camera HAL
26  * crashes, or the app getting "frozen" while CameraPipe awaits on a 1s cooldown to finally close
27  * the camera device.
28  *
29  * QuirkSummary
30  * - Bug Id: 282871038, 369300443
31  * - Description: Instructs CameraPipe to close the camera device before creating a new capture
32  *   session to avoid undesirable behaviors
33  *
34  * TODO(b/270421716): enable CameraXQuirksClassDetector lint check when kotlin is supported.
35  */
36 @SuppressLint("CameraXQuirksClassDetector")
37 public class CloseCameraDeviceOnCameraGraphCloseQuirk : Quirk {
38     public companion object {
39         @JvmStatic
isEnablednull40         public fun isEnabled(): Boolean {
41             if (Build.HARDWARE == "samsungexynos7870") {
42                 // On Exynos7870 platforms, when their 3A pipeline times out, recreating a capture
43                 // session has a high chance of triggering use-after-free crashes. Closing the
44                 // camera device helps reduce the likelihood of this happening.
45                 return true
46             } else if (
47                 Build.VERSION.SDK_INT in Build.VERSION_CODES.R..Build.VERSION_CODES.TIRAMISU &&
48                     (Device.isOppoDevice() || Device.isOnePlusDevice() || Device.isRealmeDevice())
49             ) {
50                 // On Oppo-family devices from Android 11 to Android 13, a process called
51                 // OplusHansManager actively "freezes" app processes, which means we cannot delay
52                 // closing the camera device for any amount of time.
53                 return true
54             }
55             return false
56         }
57     }
58 }
59