| 5 | #include <unistd.h> |
| 6 | #include <condition_variable> |
| 7 | #include <atomic> |
| 8 | #include <thread> |
| 9 | #include <map> |
| 10 | #include <sys/stat.h> |
| 11 | #include <utime.h> |
| 12 | #include "tgnet/FileLog.h" |
| 13 | #include "tgnet/ConnectionsManager.h" |
| 14 | #include "c_utils.h" |
| 15 |
| 16 | extern "C" { |
| 17 | using namespace rlottie; |
| 18 |
| 19 | typedef struct LottieInfo { |
| 20 |
| 21 | ~LottieInfo() { |
| 22 | if (decompressBuffer != nullptr) { |
| 23 | delete[]decompressBuffer; |
| 24 | decompressBuffer = nullptr; |
| 25 | } |
| 26 | } |
| 27 |
| 28 | std::unique_ptr<Animation> animation; |
| 29 | size_t frameCount = 0; |
| 30 | int32_t fps = 30; |
| 31 | bool precache = false; |
| 32 | bool createCache = false; |
| 33 | bool limitFps = false; |
| 34 | std::string path; |
| 35 | std::string cacheFile; |
| 36 | uint8_t *decompressBuffer = nullptr; |
| 37 | uint32_t decompressBufferSize = 0; |
| 38 | volatile uint32_t maxFrameSize = 0; |
| 39 | uint32_t imageSize = 0; |
| 40 | uint32_t fileOffset = 0; |
| 41 | uint32_t fileFrame = 0; |
| 42 | bool nextFrameIsCacheFrame = false; |
| 43 |
| 44 | char *compressBuffer = nullptr; |
| 45 | const char *buffer = nullptr; |
| 46 | bool firstFrame = false; |
| 47 | int bufferSize = 0; |
| 48 | int compressBound = 0; |
| 49 | int firstFrameSize = 0; |
| 50 | volatile uint32_t framesAvailableInCache = 0; |
| 51 | }; |
| 52 |
| 53 | JNIEXPORT jlong Java_org_telegram_ui_Components_RLottieNative_nCreate(JNIEnv *env, jclass clazz, jstring src, jstring json, jint w, jint h, jintArray data, jboolean precache, jintArray colorReplacement, jboolean limitFps, jint fitzModifier) { |
| 54 | auto info = new LottieInfo(); |
| 55 |
| 56 | std::map<int32_t, int32_t> *colors = nullptr; |
| 57 | int color = 0; |
| 58 | if (colorReplacement != nullptr) { |
| 59 | jint *arr = env->GetIntArrayElements(colorReplacement, nullptr); |
| 60 | if (arr != nullptr) { |
| 61 | jsize len = env->GetArrayLength(colorReplacement); |
| 62 | colors = new std::map<int32_t, int32_t>(); |
| 63 | for (int32_t a = 0; a < len / 2; a++) { |
| 64 | (*colors)[arr[a * 2]] = arr[a * 2 + 1]; |
| 65 | if (color == 0) { |
| 66 | color = arr[a * 2 + 1]; |
| 67 | } |
| 68 | } |
| 69 | env->ReleaseIntArrayElements(colorReplacement, arr, 0); |
| 70 | } |
| 71 | } |
| 72 |
| 73 |
| 74 | FitzModifier modifier = FitzModifier::None; |
| 75 | switch (fitzModifier) { |
| 76 | case 12: |
| 77 | modifier = FitzModifier::Type12; |
| 78 | break; |
| 79 | case 3: |
| 80 | modifier = FitzModifier::Type3; |
| 81 | break; |
| 82 | case 4: |
| 83 | modifier = FitzModifier::Type4; |
| 84 | break; |
| 85 | case 5: |
| 86 | modifier = FitzModifier::Type5; |
| 87 | break; |
| 88 | case 6: |
| 89 | modifier = FitzModifier::Type6; |
| 90 | break; |
| 91 | } |
| 92 | char const *srcString = env->GetStringUTFChars(src, nullptr); |
| 93 | info->path = srcString; |
| 94 | if (json != nullptr) { |
| 95 | char const *jsonString = env->GetStringUTFChars(json, nullptr); |
| 96 | if (jsonString) { |
| 97 | info->animation = rlottie::Animation::loadFromData(jsonString, info->path, colors, modifier); |
| 98 | env->ReleaseStringUTFChars(json, jsonString); |
| 99 | } |
| 100 | } else { |
| 101 | info->animation = rlottie::Animation::loadFromFile(info->path, colors, modifier); |
| 102 | } |
| 103 | if (srcString) { |
| 104 | env->ReleaseStringUTFChars(src, srcString); |
| 105 | } |
| 106 | if (info->animation == nullptr) { |
| 107 | delete info; |
| 108 | return 0; |
| 109 | } |
| 110 | info->frameCount = info->animation->totalFrame(); |
| 111 | info->fps = (int) info->animation->frameRate(); |
| 112 | info->limitFps = limitFps; |
| 113 | if (info->fps > 60 || info->frameCount > 600) { |
| 114 | delete info; |
| 115 | return 0; |
| 116 | } |
| 117 | info->precache = precache; |
| 118 | if (info->precache) { |
| 119 | info->cacheFile = info->path; |
| 120 | std::string::size_type index = info->cacheFile.find_last_of('/'); |
| 121 | if (index != std::string::npos) { |
| 122 | std::string dir = info->cacheFile.substr(0, index) + "/acache"; |
| 123 | mkdir(dir.c_str(), 0777); |
| 124 | info->cacheFile.insert(index, "/acache"); |
| 125 | } |
| 126 | info->cacheFile += std::to_string(w) + "_" + std::to_string(h); |
| 127 | if (color != 0) { |
| 128 | info->cacheFile += "_" + std::to_string(color); |
| 129 | } |
| 130 | if (limitFps) { |
| 131 | info->cacheFile += ".s.cache"; |
| 132 | } else { |
| 133 | info->cacheFile += ".cache"; |
| 134 | } |
| 135 | FILE *precacheFile = fopen(info->cacheFile.c_str(), "r+"); |
| 136 | if (precacheFile == nullptr) { |
| 137 | info->createCache = true; |
| 138 | } else { |
| 139 | uint8_t temp; |
| 140 | size_t read = fread(&temp, sizeof(uint8_t), 1, precacheFile); |
| 141 | info->createCache = read != 1 || temp == 0; |
| 142 | if (!info->createCache) { |
| 143 | uint32_t maxFrameSize; |
| 144 | fread(&maxFrameSize, sizeof(uint32_t), 1, precacheFile); |
| 145 | info->maxFrameSize = maxFrameSize; |
| 146 | fread(&(info->imageSize), sizeof(uint32_t), 1, precacheFile); |
| 147 | info->fileOffset = 9; |
| 148 | info->fileFrame = 0; |
| 149 | utimensat(0, info->cacheFile.c_str(), nullptr, 0); |
| 150 | } |
| 151 | fclose(precacheFile); |
| 152 | } |
| 153 | } |
| 154 |
| 155 | jint *dataArr = env->GetIntArrayElements(data, nullptr); |
| 156 | if (dataArr != nullptr) { |
| 157 | dataArr[0] = (jint) info->frameCount; |
| 158 | dataArr[1] = (jint) info->animation->frameRate(); |
| 159 | dataArr[2] = info->createCache ? 1 : 0; |
| 160 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 161 | } |
| 162 | return (jlong) (intptr_t) info; |
| 163 | } |
| 164 |
| 165 | JNIEXPORT jlong Java_org_telegram_ui_Components_RLottieNative_nCreateWithJson(JNIEnv *env, jclass clazz, jstring json, jstring name, jintArray data, jintArray colorReplacement) { |
| 166 | std::map<int32_t, int32_t> *colors = nullptr; |
| 167 | if (colorReplacement != nullptr) { |
| 168 | jint *arr = env->GetIntArrayElements(colorReplacement, nullptr); |
| 169 | if (arr != nullptr) { |
| 170 | jsize len = env->GetArrayLength(colorReplacement); |
| 171 | colors = new std::map<int32_t, int32_t>(); |
| 172 | for (int32_t a = 0; a < len / 2; a++) { |
| 173 | (*colors)[arr[a * 2]] = arr[a * 2 + 1]; |
| 174 | } |
| 175 | env->ReleaseIntArrayElements(colorReplacement, arr, 0); |
| 176 | } |
| 177 | } |
| 178 |
| 179 | auto info = new LottieInfo(); |
| 180 |
| 181 | char const *jsonString = env->GetStringUTFChars(json, nullptr); |
| 182 | char const *nameString = env->GetStringUTFChars(name, nullptr); |
| 183 | info->animation = rlottie::Animation::loadFromData(jsonString, nameString, colors); |
| 184 | if (jsonString) { |
| 185 | env->ReleaseStringUTFChars(json, jsonString); |
| 186 | } |
| 187 | if (nameString) { |
| 188 | env->ReleaseStringUTFChars(name, nameString); |
| 189 | } |
| 190 | if (info->animation == nullptr) { |
| 191 | delete info; |
| 192 | return 0; |
| 193 | } |
| 194 | info->frameCount = info->animation->totalFrame(); |
| 195 | info->fps = (int) info->animation->frameRate(); |
| 196 |
| 197 | jint *dataArr = env->GetIntArrayElements(data, nullptr); |
| 198 | if (dataArr != nullptr) { |
| 199 | dataArr[0] = (int) info->frameCount; |
| 200 | dataArr[1] = (int) info->animation->frameRate(); |
| 201 | dataArr[2] = 0; |
| 202 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 203 | } |
| 204 | return (jlong) (intptr_t) info; |
| 205 | } |
| 206 |
| 207 | JNIEXPORT void Java_org_telegram_ui_Components_RLottieNative_nDestroy(JNIEnv *env, jclass clazz, jlong ptr) { |
| 208 | if (!ptr) { |
| 209 | return; |
| 210 | } |
| 211 | auto info = (LottieInfo *) (intptr_t) ptr; |
| 212 | delete info; |
| 213 | } |
| 214 |
| 215 | JNIEXPORT void Java_org_telegram_ui_Components_RLottieNative_nSetLayerColor(JNIEnv *env, jclass clazz, jlong ptr, jstring layer, jint color) { |
| 216 | if (!ptr || layer == nullptr) { |
| 217 | return; |
| 218 | } |
| 219 | auto info = (LottieInfo *) (intptr_t) ptr; |
| 220 | char const *layerString = env->GetStringUTFChars(layer, nullptr); |
| 221 | info->animation->setValue<Property::Color>(layerString, Color(((color) & 0xff) / 255.0f, ((color >> 8) & 0xff) / 255.0f, ((color >> 16) & 0xff) / 255.0f)); |
| 222 | if (layerString) { |
| 223 | env->ReleaseStringUTFChars(layer, layerString); |
| 224 | } |
| 225 | } |
| 226 |
| 227 | JNIEXPORT void Java_org_telegram_ui_Components_RLottieNative_nReplaceColors(JNIEnv *env, jclass clazz, jlong ptr, jintArray colorReplacement) { |
| 228 | if (!ptr || colorReplacement == nullptr) { |
| 229 | return; |
| 230 | } |
| 231 | auto info = (LottieInfo *) (intptr_t) ptr; |
| 232 |
| 233 | jint *arr = env->GetIntArrayElements(colorReplacement, nullptr); |
| 234 | if (arr != nullptr) { |
| 235 | jsize len = env->GetArrayLength(colorReplacement); |
| 236 | for (int32_t a = 0; a < len / 2; a++) { |
| 237 | (*info->animation->colorMap)[arr[a * 2]] = arr[a * 2 + 1]; |
| 238 | } |
| 239 | info->animation->resetCurrentFrame(); |
| 240 | env->ReleaseIntArrayElements(colorReplacement, arr, 0); |
| 241 | } |
| 242 | } |
| 243 |
| 244 |
| 245 | JNIEXPORT jint Java_org_telegram_ui_Components_RLottieNative_nGetFrame(JNIEnv *env, jclass clazz, jlong ptr, jint frame, jobject bitmap, jboolean clear) { |
| 246 | if (!ptr || bitmap == nullptr) { |
| 247 | return 0; |
| 248 | } |
| 249 | auto info = (LottieInfo *) (intptr_t) ptr; |
| 250 |
| 251 | AndroidBitmapInfo bitmapInfo; |
| 252 | if (__builtin_expect(AndroidBitmap_getInfo(env, bitmap, &bitmapInfo) != ANDROID_BITMAP_RESULT_SUCCESS, 0)) { |
| 253 | return 0; |
| 254 | } |
| 255 |
| 256 | void *pixels; |
| 257 | bool result = false; |
| 258 | if (AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0) { |
| 259 | Surface surface((uint32_t *) pixels, |
| 260 | static_cast<size_t>(bitmapInfo.width), |
| 261 | static_cast<size_t>(bitmapInfo.height), |
| 262 | static_cast<size_t>(bitmapInfo.stride)); |
| 263 | info->animation->renderSync((size_t) frame, surface, clear, &result); |
| 264 | AndroidBitmap_unlockPixels(env, bitmap); |
| 265 | } |
| 266 | if (!result) { |
| 267 | return -5; |
| 268 | } |
| 269 | return frame; |
| 270 | } |
| 271 | } |
| 272 |