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