| 5 |
| 6 | #include <algorithm> |
| 7 | #include <cstdint> |
| 8 | #include <cstdio> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 |
| 12 | namespace { |
| 13 |
| 14 | constexpr float kDefaultCurveTolerance = 0.125f; |
| 15 |
| 16 | struct LayerColorReplacements { |
| 17 | // The C ABI borrows these UTF-8 buffers during tlottie_new_with_options. |
| 18 | std::vector<std::string> prefixes; |
| 19 | std::vector<TLottieLayerColorReplacement> values; |
| 20 | }; |
| 21 |
| 22 | static uint32_t toTlottieFitz(jint modifier) { |
| 23 | switch (modifier) { |
| 24 | case 12: return TLOTTIE_FITZ_TYPE_12; |
| 25 | case 3: return TLOTTIE_FITZ_TYPE_3; |
| 26 | case 4: return TLOTTIE_FITZ_TYPE_4; |
| 27 | case 5: return TLOTTIE_FITZ_TYPE_5; |
| 28 | case 6: return TLOTTIE_FITZ_TYPE_6; |
| 29 | default: return TLOTTIE_FITZ_NONE; |
| 30 | } |
| 31 | } |
| 32 |
| 33 | static std::string normalizePrefix(std::string prefix) { |
| 34 | if (prefix.size() >= 3 && prefix.compare(prefix.size() - 3, 3, ".**") == 0) { |
| 35 | prefix.resize(prefix.size() - 3); |
| 36 | } else if (prefix.size() >= 2 && prefix.compare(prefix.size() - 2, 2, "**") == 0) { |
| 37 | prefix.resize(prefix.size() - 2); |
| 38 | } |
| 39 | return prefix; |
| 40 | } |
| 41 |
| 42 | static LayerColorReplacements readLayerColors(JNIEnv *env, jobjectArray names, jintArray colors) { |
| 43 | LayerColorReplacements result; |
| 44 | if (names == nullptr || colors == nullptr) { |
| 45 | return result; |
| 46 | } |
| 47 | const jsize count = std::min(env->GetArrayLength(names), env->GetArrayLength(colors)); |
| 48 | jint *values = env->GetIntArrayElements(colors, nullptr); |
| 49 | if (values == nullptr) { |
| 50 | return result; |
| 51 | } |
| 52 | // Reserving is important: moving an SSO string later could invalidate the |
| 53 | // pointer already stored in the corresponding C replacement descriptor. |
| 54 | result.prefixes.reserve(static_cast<size_t>(count)); |
| 55 | result.values.reserve(static_cast<size_t>(count)); |
| 56 | for (jsize i = 0; i < count; ++i) { |
| 57 | auto name = static_cast<jstring>(env->GetObjectArrayElement(names, i)); |
| 58 | if (name != nullptr) { |
| 59 | const char *chars = env->GetStringUTFChars(name, nullptr); |
| 60 | if (chars != nullptr) { |
| 61 | result.prefixes.push_back(normalizePrefix(chars)); |
| 62 | const std::string &prefix = result.prefixes.back(); |
| 63 | result.values.push_back({reinterpret_cast<const uint8_t *>(prefix.data()), |
| 64 | prefix.size(), static_cast<uint32_t>(values[i])}); |
| 65 | env->ReleaseStringUTFChars(name, chars); |
| 66 | } |
| 67 | env->DeleteLocalRef(name); |
| 68 | } |
| 69 | } |
| 70 | env->ReleaseIntArrayElements(colors, values, JNI_ABORT); |
| 71 | return result; |
| 72 | } |
| 73 |
| 74 | static std::vector<TLottieColorReplacement> readColorReplacements(JNIEnv *env, jintArray replacements) { |
| 75 | std::vector<TLottieColorReplacement> result; |
| 76 | if (replacements == nullptr) { |
| 77 | return result; |
| 78 | } |
| 79 | jint *values = env->GetIntArrayElements(replacements, nullptr); |
| 80 | if (values == nullptr) { |
| 81 | return result; |
| 82 | } |
| 83 | const jsize count = env->GetArrayLength(replacements); |
| 84 | result.reserve(static_cast<size_t>(count / 2)); |
| 85 | for (jsize i = 0; i + 1 < count; i += 2) { |
| 86 | // Android colors are 0xAARRGGBB. tlottie replacement matching is RGB-only; |
| 87 | // keep the standard RRGGBB byte order and discard the Java alpha byte. |
| 88 | result.push_back({static_cast<uint32_t>(values[i]) & 0x00ffffffu, |
| 89 | static_cast<uint32_t>(values[i + 1]) & 0x00ffffffu}); |
| 90 | } |
| 91 | env->ReleaseIntArrayElements(replacements, values, JNI_ABORT); |
| 92 | return result; |
| 93 | } |
| 94 |
| 95 | static bool readFile(const char *path, std::string &out) { |
| 96 | gzFile file = gzopen(path, "rb"); |
| 97 | if (file == nullptr) { |
| 98 | return false; |
| 99 | } |
| 100 | char buffer[16 * 1024]; |
| 101 | int read; |
| 102 | while ((read = gzread(file, buffer, sizeof(buffer))) > 0) { |
| 103 | out.append(buffer, static_cast<size_t>(read)); |
| 104 | } |
| 105 | const bool ok = read == 0; |
| 106 | gzclose(file); |
| 107 | return ok && !out.empty(); |
| 108 | } |
| 109 |
| 110 | static TLottieInstance *createInstance(JNIEnv *env, const char *json, size_t jsonLength, |
| 111 | jobjectArray layerNames, jintArray layerValues, |
| 112 | jintArray colorReplacement, jint fitzModifier) { |
| 113 | const LayerColorReplacements layerReplacements = |
| 114 | readLayerColors(env, layerNames, layerValues); |
| 115 | const std::vector<TLottieColorReplacement> colorReplacements = |
| 116 | readColorReplacements(env, colorReplacement); |
| 117 | return tlottie_new_with_options( |
| 118 | reinterpret_cast<const uint8_t *>(json), jsonLength, toTlottieFitz(fitzModifier), |
| 119 | layerReplacements.values.data(), layerReplacements.values.size(), |
| 120 | colorReplacements.data(), colorReplacements.size(), TlottieChannelOrder::TLOTTIE_CHANNEL_RGBA); |
| 121 | } |
| 122 |
| 123 | static bool writeMetadata(JNIEnv *env, jintArray data, TLottieInstance *instance) { |
| 124 | const uint32_t frameCount = tlottie_frame_count(instance); |
| 125 | const int32_t fps = static_cast<int32_t>(tlottie_frame_rate(instance)); |
| 126 | if (fps <= 0 || fps > 60 || frameCount > 600) { |
| 127 | return false; |
| 128 | } |
| 129 | if (data == nullptr || env->GetArrayLength(data) < 3) { |
| 130 | return true; |
| 131 | } |
| 132 | const jint values[3] = {static_cast<jint>(frameCount), fps, 0}; |
| 133 | env->SetIntArrayRegion(data, 0, 3, values); |
| 134 | return true; |
| 135 | } |
| 136 |
| 137 | } // namespace |
| 138 |
| 139 | extern "C" { |
| 140 |
| 141 | JNIEXPORT jlong JNICALL Java_org_telegram_ui_Components_RLottieNative_nCreate( |
| 142 | JNIEnv *env, jclass, jstring src, jstring json, jint, jint, jintArray data, |
| 143 | jboolean, jintArray colorReplacement, jboolean, jint fitzModifier, |
| 144 | jobjectArray layerNames, jintArray layerColors) { |
| 145 | std::string input; |
| 146 | if (json != nullptr) { |
| 147 | const char *chars = env->GetStringUTFChars(json, nullptr); |
| 148 | if (chars != nullptr) { |
| 149 | input.assign(chars, static_cast<size_t>(env->GetStringUTFLength(json))); |
| 150 | env->ReleaseStringUTFChars(json, chars); |
| 151 | } |
| 152 | } else if (src != nullptr) { |
| 153 | const char *path = env->GetStringUTFChars(src, nullptr); |
| 154 | if (path != nullptr) { |
| 155 | readFile(path, input); |
| 156 | env->ReleaseStringUTFChars(src, path); |
| 157 | } |
| 158 | } |
| 159 | TLottieInstance *instance = input.empty() ? nullptr : createInstance(env, input.data(), input.size(), |
| 160 | layerNames, layerColors, colorReplacement, fitzModifier); |
| 161 | if (instance == nullptr) return 0; |
| 162 | if (!writeMetadata(env, data, instance)) { |
| 163 | tlottie_drop(instance); |
| 164 | return 0; |
| 165 | } |
| 166 | return reinterpret_cast<jlong>(instance); |
| 167 | } |
| 168 |
| 169 | JNIEXPORT jlong JNICALL Java_org_telegram_ui_Components_RLottieNative_nCreateWithJson( |
| 170 | JNIEnv *env, jclass, jstring json, jstring, jintArray data, jintArray colorReplacement, |
| 171 | jobjectArray layerNames, jintArray layerColors) { |
| 172 | if (json == nullptr) return 0; |
| 173 | const char *chars = env->GetStringUTFChars(json, nullptr); |
| 174 | if (chars == nullptr) return 0; |
| 175 | TLottieInstance *instance = createInstance(env, chars, static_cast<size_t>(env->GetStringUTFLength(json)), |
| 176 | layerNames, layerColors, colorReplacement, 0); |
| 177 | env->ReleaseStringUTFChars(json, chars); |
| 178 | if (instance == nullptr) return 0; |
| 179 | if (!writeMetadata(env, data, instance)) { |
| 180 | tlottie_drop(instance); |
| 181 | return 0; |
| 182 | } |
| 183 | return reinterpret_cast<jlong>(instance); |
| 184 | } |
| 185 |
| 186 | JNIEXPORT void JNICALL Java_org_telegram_ui_Components_RLottieNative_nDestroy(JNIEnv *, jclass, jlong ptr) { |
| 187 | tlottie_drop(reinterpret_cast<TLottieInstance *>(ptr)); |
| 188 | } |
| 189 |
| 190 | JNIEXPORT jint JNICALL Java_org_telegram_ui_Components_RLottieNative_nGetFrame( |
| 191 | JNIEnv *env, jclass, jlong ptr, jint frame, jobject bitmap, jboolean clear) { |
| 192 | if (ptr == 0 || bitmap == nullptr) return 0; |
| 193 | auto *instance = reinterpret_cast<TLottieInstance *>(ptr); |
| 194 | AndroidBitmapInfo bitmapInfo; |
| 195 | if (AndroidBitmap_getInfo(env, bitmap, &bitmapInfo) != ANDROID_BITMAP_RESULT_SUCCESS || |
| 196 | (bitmapInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888 && |
| 197 | bitmapInfo.format != ANDROID_BITMAP_FORMAT_A_8)) return 0; |
| 198 | void *rawPixels = nullptr; |
| 199 | if (AndroidBitmap_lockPixels(env, bitmap, &rawPixels) < 0) return 0; |
| 200 | const size_t count = static_cast<size_t>(bitmapInfo.width) * bitmapInfo.height; |
| 201 | int32_t result; |
| 202 | if (bitmapInfo.format == ANDROID_BITMAP_FORMAT_A_8) { |
| 203 | std::vector<uint8_t> temporary; |
| 204 | auto *alpha = static_cast<uint8_t *>(rawPixels); |
| 205 | if (bitmapInfo.stride != bitmapInfo.width) { |
| 206 | temporary.resize(count); |
| 207 | alpha = temporary.data(); |
| 208 | if (!clear) { |
| 209 | const auto *source = static_cast<const uint8_t *>(rawPixels); |
| 210 | for (uint32_t y = 0; y < bitmapInfo.height; ++y) { |
| 211 | std::copy_n(source + static_cast<size_t>(y) * bitmapInfo.stride, |
| 212 | bitmapInfo.width, |
| 213 | alpha + static_cast<size_t>(y) * bitmapInfo.width); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | result = tlottie_render_alpha8_with_options(instance, static_cast<float>(frame), |
| 218 | bitmapInfo.width, bitmapInfo.height, alpha, count, 1, |
| 219 | kDefaultCurveTolerance, clear ? 1 : 0); |
| 220 | if (result == TLOTTIE_OK && !temporary.empty()) { |
| 221 | auto *destination = static_cast<uint8_t *>(rawPixels); |
| 222 | for (uint32_t y = 0; y < bitmapInfo.height; ++y) { |
| 223 | std::copy_n(alpha + static_cast<size_t>(y) * bitmapInfo.width, bitmapInfo.width, |
| 224 | destination + static_cast<size_t>(y) * bitmapInfo.stride); |
| 225 | } |
| 226 | } |
| 227 | } else { |
| 228 | std::vector<uint32_t> temporary; |
| 229 | auto *pixels = static_cast<uint32_t *>(rawPixels); |
| 230 | if (bitmapInfo.stride != bitmapInfo.width * sizeof(uint32_t)) { |
| 231 | temporary.resize(count); |
| 232 | pixels = temporary.data(); |
| 233 | if (!clear) { |
| 234 | const auto *source = static_cast<const uint8_t *>(rawPixels); |
| 235 | for (uint32_t y = 0; y < bitmapInfo.height; ++y) { |
| 236 | std::copy_n( |
| 237 | reinterpret_cast<const uint32_t *>( |
| 238 | source + static_cast<size_t>(y) * bitmapInfo.stride), |
| 239 | bitmapInfo.width, |
| 240 | pixels + static_cast<size_t>(y) * bitmapInfo.width); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | result = tlottie_render_with_options(instance, static_cast<float>(frame), |
| 245 | bitmapInfo.width, bitmapInfo.height, pixels, count, 1, |
| 246 | kDefaultCurveTolerance, clear ? 1 : 0); |
| 247 | if (result == TLOTTIE_OK) { |
| 248 | if (!temporary.empty()) { |
| 249 | auto *destination = static_cast<uint8_t *>(rawPixels); |
| 250 | for (uint32_t y = 0; y < bitmapInfo.height; ++y) { |
| 251 | std::copy_n(pixels + static_cast<size_t>(y) * bitmapInfo.width, bitmapInfo.width, |
| 252 | reinterpret_cast<uint32_t *>(destination + static_cast<size_t>(y) * bitmapInfo.stride)); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | AndroidBitmap_unlockPixels(env, bitmap); |
| 258 | return result == TLOTTIE_OK ? frame : -5; |
| 259 | } |
| 260 | } |
| 261 |