1 /*
2  * Copyright 2020 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.compose.ui.graphics
18 
19 import android.graphics.Shader
20 import android.os.Build
21 import androidx.annotation.RequiresApi
22 
23 /**
24  * Helper method to determine if the appropriate [TileMode] is supported on the given Android API
25  * level this provides an opportunity for consumers to fallback on an alternative user experience
26  * for devices that do not support the corresponding blend mode. Usages of [TileMode] types that are
27  * not supported will fallback onto the default of [TileMode.Clamp]
28  */
isSupportednull29 actual fun TileMode.isSupported(): Boolean =
30     Build.VERSION.SDK_INT >= Build.VERSION_CODES.S || this != TileMode.Decal
31 
32 fun TileMode.toAndroidTileMode(): Shader.TileMode =
33     when (this) {
34         TileMode.Clamp -> Shader.TileMode.CLAMP
35         TileMode.Repeated -> Shader.TileMode.REPEAT
36         TileMode.Mirror -> Shader.TileMode.MIRROR
37         TileMode.Decal ->
38             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
39                 TileModeVerificationHelper.getFrameworkTileModeDecal()
40             } else {
41                 Shader.TileMode.CLAMP
42             }
43 
44         // Always fallback to TileMode.Clamp
45         else -> Shader.TileMode.CLAMP
46     }
47 
toComposeTileModenull48 fun Shader.TileMode.toComposeTileMode(): TileMode =
49     when (this) {
50         Shader.TileMode.CLAMP -> TileMode.Clamp
51         Shader.TileMode.MIRROR -> TileMode.Mirror
52         Shader.TileMode.REPEAT -> TileMode.Repeated
53         else -> {
54             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && this == Shader.TileMode.DECAL) {
55                 TileModeVerificationHelper.getComposeTileModeDecal()
56             } else {
57                 TileMode.Clamp
58             }
59         }
60     }
61 
62 @RequiresApi(Build.VERSION_CODES.S)
63 private object TileModeVerificationHelper {
getFrameworkTileModeDecalnull64     fun getFrameworkTileModeDecal() = Shader.TileMode.DECAL
65 
66     fun getComposeTileModeDecal() = TileMode.Decal
67 }
68