1 /*
2  * Copyright (C) 2021 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 #ifndef ANDROID_RENDERSCRIPT_TOOLKIT_UTILS_H
18 #define ANDROID_RENDERSCRIPT_TOOLKIT_UTILS_H
19 
20 #include <android/log.h>
21 #include <stddef.h>
22 
23 namespace renderscript {
24 
25 /* The Toolkit does not support floating point buffers but the original RenderScript Intrinsics
26  * did for some operations. That code was preserved and protected by
27  * ANDROID_RENDERSCRIPT_TOOLKIT_SUPPORTS_FLOAT.
28  */
29 // TODO: On final packaging, decide whether this should be define in the build file, and for which
30 // config.
31 // #define ANDROID_RENDERSCRIPT_TOOLKIT_SUPPORTS_FLOAT
32 
33 /* If we release the Toolkit as a C++ API, we'll want to enable validation at the C++ level
34  * by uncommenting this define.
35  *
36  * If we only have a Java/Kotlin API, the Kotlin layer does validation. We don't need to duplicate
37  * this effort.
38  */
39 #define ANDROID_RENDERSCRIPT_TOOLKIT_VALIDATE
40 
41 #define ALOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
42 #define ALOGW(...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
43 #define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
44 
45 using uchar = unsigned char;
46 using uint = unsigned int;
47 using ushort = unsigned short;
48 
49 using uint8_t = uchar;
50 using uint16_t = ushort;
51 using uint32_t = uint;
52 
53 typedef float float2 __attribute__((ext_vector_type(2)));
54 typedef float float3 __attribute__((ext_vector_type(3)));
55 typedef float float4 __attribute__((ext_vector_type(4)));
56 typedef uchar uchar2 __attribute__((ext_vector_type(2)));
57 typedef uchar uchar3 __attribute__((ext_vector_type(3)));
58 typedef uchar uchar4 __attribute__((ext_vector_type(4)));
59 typedef ushort ushort2 __attribute__((ext_vector_type(2)));
60 typedef ushort ushort3 __attribute__((ext_vector_type(3)));
61 typedef ushort ushort4 __attribute__((ext_vector_type(4)));
62 typedef uint uint2 __attribute__((ext_vector_type(2)));
63 typedef uint uint3 __attribute__((ext_vector_type(3)));
64 typedef uint uint4 __attribute__((ext_vector_type(4)));
65 typedef short short2 __attribute__((ext_vector_type(2)));
66 typedef short short3 __attribute__((ext_vector_type(3)));
67 typedef short short4 __attribute__((ext_vector_type(4)));
68 typedef int int2 __attribute__((ext_vector_type(2)));
69 typedef int int3 __attribute__((ext_vector_type(3)));
70 typedef int int4 __attribute__((ext_vector_type(4)));
71 
72 template <typename TO, typename TI>
convert(TI i)73 inline TO convert(TI i) {
74     // assert(i.x >= 0 && i.y >= 0 && i.z >= 0 && i.w >= 0);
75     // assert(i.x <= 255 && i.y <= 255 && i.z <= 255 && i.w <= 255);
76     return __builtin_convertvector(i, TO);
77 }
78 
79 template <>
convert(float i)80 inline uchar convert(float i) {
81     // assert(i.x >= 0 && i.y >= 0 && i.z >= 0 && i.w >= 0);
82     // assert(i.x <= 255 && i.y <= 255 && i.z <= 255 && i.w <= 255);
83     return (uchar)i;
84 }
85 
86 template <>
convert(uchar i)87 inline float convert(uchar i) {
88     // assert(i.x >= 0 && i.y >= 0 && i.z >= 0 && i.w >= 0);
89     // assert(i.x <= 255 && i.y <= 255 && i.z <= 255 && i.w <= 255);
90     return (float)i;
91 }
92 
clamp(int4 amount,int low,int high)93 inline int4 clamp(int4 amount, int low, int high) {
94     int4 r;
95     r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);
96     r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);
97     r.z = amount.z < low ? low : (amount.z > high ? high : amount.z);
98     r.w = amount.w < low ? low : (amount.w > high ? high : amount.w);
99     return r;
100 }
101 
clamp(float4 amount,float low,float high)102 inline float4 clamp(float4 amount, float low, float high) {
103     float4 r;
104     r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);
105     r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);
106     r.z = amount.z < low ? low : (amount.z > high ? high : amount.z);
107     r.w = amount.w < low ? low : (amount.w > high ? high : amount.w);
108     return r;
109 }
110 
clamp(int2 amount,int low,int high)111 inline int2 clamp(int2 amount, int low, int high) {
112     int2 r;
113     r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);
114     r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);
115     return r;
116 }
117 
clamp(float2 amount,float low,float high)118 inline float2 clamp(float2 amount, float low, float high) {
119     float2 r;
120     r.x = amount.x < low ? low : (amount.x > high ? high : amount.x);
121     r.y = amount.y < low ? low : (amount.y > high ? high : amount.y);
122     return r;
123 }
124 
clamp(int amount,int low,int high)125 inline int clamp(int amount, int low, int high) {
126     return amount < low ? low : (amount > high ? high : amount);
127 }
128 
clamp(float amount,float low,float high)129 inline float clamp(float amount, float low, float high) {
130     return amount < low ? low : (amount > high ? high : amount);
131 }
132 
133 #ifdef ANDROID_RENDERSCRIPT_TOOLKIT_VALIDATE
134 struct Restriction;
135 
136 bool validRestriction(const char* tag, size_t sizeX, size_t sizeY, const Restriction* restriction);
137 #endif
138 
139 /**
140  * Returns true if the processor we're running on supports the SIMD instructions that are
141  * used in our assembly code.
142  */
143 bool cpuSupportsSimd();
144 
divideRoundingUp(size_t a,size_t b)145 inline size_t divideRoundingUp(size_t a, size_t b) {
146     return a / b + (a % b == 0 ? 0 : 1);
147 }
148 
paddedSize(size_t size)149 inline size_t paddedSize(size_t size) {
150     return size == 3 ? 4 : size;
151 }
152 
153 }  // namespace renderscript
154 
155 #endif  // ANDROID_RENDERSCRIPT_TOOLKIT_UTILS_H
156