| 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 | bool nextFrameIsCacheFrame = false; |
| 43 |
| 44 | FILE *precacheFile = nullptr; |
| 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 | 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_RLottieDrawable_createWithJson(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_RLottieDrawable_destroy(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_RLottieDrawable_setLayerColor(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_RLottieDrawable_replaceColors(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 | bool cacheWriteThreadCreated{false}; |
| 245 | LottieInfo *cacheWriteThreadTask{nullptr}; |
| 246 | bool cacheWriteThreadDone{false}; |
| 247 | std::thread worker; |
| 248 | std::mutex cacheMutex; |
| 249 | std::condition_variable cacheCv; |
| 250 |
| 251 | std::mutex cacheDoneMutex; |
| 252 | std::condition_variable cacheDoneCv; |
| 253 | std::atomic<bool> frameReady{false}; |
| 254 |
| 255 | void CacheWriteThreadProc() { |
| 256 | while (!cacheWriteThreadDone) { |
| 257 | std::unique_lock<std::mutex> lk(cacheMutex); |
| 258 | cacheCv.wait(lk, [] { return frameReady.load(); }); |
| 259 | std::lock_guard<std::mutex> lg(cacheDoneMutex); |
| 260 | LottieInfo *task; |
| 261 | if (cacheWriteThreadTask != nullptr) { |
| 262 | task = cacheWriteThreadTask; |
| 263 | cacheWriteThreadTask = nullptr; |
| 264 | } else { |
| 265 | task = nullptr; |
| 266 | } |
| 267 | lk.unlock(); |
| 268 |
| 269 | if (task != nullptr) { |
| 270 | auto size = (uint32_t) LZ4_compress_default(task->buffer, task->compressBuffer, task->bufferSize, task->compressBound); |
| 271 | if (task->firstFrame) { |
| 272 | task->firstFrameSize = size; |
| 273 | task->fileOffset = 9 + sizeof(uint32_t) + task->firstFrameSize; |
| 274 | } |
| 275 | task->maxFrameSize = MAX(task->maxFrameSize, size); |
| 276 | fwrite(&size, sizeof(uint32_t), 1, task->precacheFile); |
| 277 | fwrite(task->compressBuffer, sizeof(uint8_t), size, task->precacheFile); |
| 278 |
| 279 | fflush(task->precacheFile); |
| 280 | fsync(fileno(task->precacheFile)); |
| 281 | task->framesAvailableInCache++; |
| 282 | } |
| 283 | frameReady = false; |
| 284 | cacheDoneCv.notify_one(); |
| 285 | } |
| 286 | } |
| 287 |
| 288 | JNIEXPORT void Java_org_telegram_ui_Components_RLottieDrawable_createCache(JNIEnv *env, jclass clazz, jlong ptr, jint w, jint h) { |
| 289 | if (ptr == NULL) { |
| 290 | return; |
| 291 | } |
| 292 | auto info = (LottieInfo *) (intptr_t) ptr; |
| 293 |
| 294 | FILE *cacheFile = fopen(info->cacheFile.c_str(), "r+"); |
| 295 | if (cacheFile != nullptr) { |
| 296 | uint8_t temp; |
| 297 | size_t read = fread(&temp, sizeof(uint8_t), 1, cacheFile); |
| 298 | fclose(cacheFile); |
| 299 | if (read == 1 && temp != 0) { |
| 300 | return; |
| 301 | } |
| 302 | } |
| 303 |
| 304 | if (!cacheWriteThreadCreated) { |
| 305 | cacheWriteThreadCreated = true; |
| 306 | worker = std::thread(CacheWriteThreadProc); |
| 307 | } |
| 308 |
| 309 | if (info->nextFrameIsCacheFrame && info->createCache && info->frameCount != 0) { |
| 310 | info->precacheFile = fopen(info->cacheFile.c_str(), "w+"); |
| 311 | if (info->precacheFile != nullptr) { |
| 312 | fseek(info->precacheFile, info->fileOffset = 9, SEEK_SET); |
| 313 | info->maxFrameSize = 0; |
| 314 | info->bufferSize = w * h * 4; |
| 315 | info->imageSize = (uint32_t) w * h * 4; |
| 316 | info->compressBound = LZ4_compressBound(info->bufferSize); |
| 317 | info->compressBuffer = new char[info->compressBound]; |
| 318 | auto firstBuffer = new uint8_t[info->bufferSize]; |
| 319 | auto secondBuffer = new uint8_t[info->bufferSize]; |
| 320 | //long time = ConnectionsManager::getInstance(0).getCurrentTimeMonotonicMillis(); |
| 321 |
| 322 | Surface surface1((uint32_t *) firstBuffer, (size_t) w, (size_t) h, (size_t) w * 4); |
| 323 | Surface surface2((uint32_t *) secondBuffer, (size_t) w, (size_t) h, (size_t) w * 4); |
| 324 | int framesPerUpdate = !info->limitFps || info->fps < 60 ? 1 : 2; |
| 325 | int num = 0; |
| 326 | for (size_t a = 0; a < info->frameCount; a += framesPerUpdate) { |
| 327 | Surface &surfaceToRender = num % 2 == 0 ? surface1 : surface2; |
| 328 | num++; |
| 329 | info->animation->renderSync(a, surfaceToRender, true); |
| 330 | if (a != 0) { |
| 331 | std::unique_lock<std::mutex> lk(cacheDoneMutex); |
| 332 | cacheDoneCv.wait(lk, [] { return !frameReady.load(); }); |
| 333 | } |
| 334 |
| 335 | std::lock_guard<std::mutex> lg(cacheMutex); |
| 336 | cacheWriteThreadTask = info; |
| 337 | info->firstFrame = a == 0; |
| 338 | info->buffer = (const char *) surfaceToRender.buffer(); |
| 339 | frameReady = true; |
| 340 | cacheCv.notify_one(); |
| 341 | } |
| 342 | std::unique_lock<std::mutex> lk(cacheDoneMutex); |
| 343 | cacheDoneCv.wait(lk, [] { return !frameReady.load(); }); |
| 344 |
| 345 | //DEBUG_D("sticker time = %d", (int) (ConnectionsManager::getInstance(0).getCurrentTimeMonotonicMillis() - time)); |
| 346 | delete[] info->compressBuffer; |
| 347 | delete[] firstBuffer; |
| 348 | delete[] secondBuffer; |
| 349 | fseek(info->precacheFile, 0, SEEK_SET); |
| 350 | uint8_t byte = 1; |
| 351 | fwrite(&byte, sizeof(uint8_t), 1, info->precacheFile); |
| 352 | uint32_t maxFrameSize = info->maxFrameSize; |
| 353 | fwrite(&maxFrameSize, sizeof(uint32_t), 1, info->precacheFile); |
| 354 | fwrite(&info->imageSize, sizeof(uint32_t), 1, info->precacheFile); |
| 355 | fflush(info->precacheFile); |
| 356 | fsync(fileno(info->precacheFile)); |
| 357 | info->createCache = false; |
| 358 | fclose(info->precacheFile); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 |
| 363 | 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) { |
| 364 | if (!ptr || bitmap == nullptr) { |
| 365 | return 0; |
| 366 | } |
| 367 | auto info = (LottieInfo *) (intptr_t) ptr; |
| 368 |
| 369 | int framesPerUpdate = !info->limitFps || info->fps < 60 ? 1 : 2; |
| 370 | int framesAvailableInCache = info->framesAvailableInCache; |
| 371 |
| 372 | if (info->createCache && info->precache && frame > 0) { |
| 373 | if (frame / framesPerUpdate >= framesAvailableInCache) { |
| 374 | return -1; |
| 375 | } |
| 376 | } |
| 377 |
| 378 | void *pixels; |
| 379 | if (AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0) { |
| 380 | bool loadedFromCache = false; |
| 381 | uint32_t maxFrameSize = info->maxFrameSize; |
| 382 | if (info->precache && (!info->createCache || frame > 0) && w * 4 == stride && maxFrameSize <= w * h * 4 && info->imageSize == w * h * 4) { |
| 383 | FILE *precacheFile = fopen(info->cacheFile.c_str(), "r"); |
| 384 | if (precacheFile != nullptr) { |
| 385 | if (info->decompressBuffer != nullptr && info->decompressBufferSize < maxFrameSize) { |
| 386 | delete[] info->decompressBuffer; |
| 387 | info->decompressBuffer = nullptr; |
| 388 | } |
| 389 | if (info->decompressBuffer == nullptr) { |
| 390 | info->decompressBufferSize = maxFrameSize; |
| 391 | if (info->createCache) { |
| 392 | info->decompressBufferSize += 10000; |
| 393 | } |
| 394 | info->decompressBuffer = new uint8_t[info->decompressBufferSize]; |
| 395 | } |
| 396 | fseek(precacheFile, info->fileOffset, SEEK_SET); |
| 397 | uint32_t frameSize; |
| 398 | fread(&frameSize, sizeof(uint32_t), 1, precacheFile); |
| 399 | if (frameSize > 0 && frameSize <= info->decompressBufferSize) { |
| 400 | fread(info->decompressBuffer, sizeof(uint8_t), frameSize, precacheFile); |
| 401 | info->fileOffset += 4 + frameSize; |
| 402 | LZ4_decompress_safe((const char *) info->decompressBuffer, (char *) pixels, frameSize, w * h * 4); |
| 403 | loadedFromCache = true; |
| 404 | } |
| 405 | fclose(precacheFile); |
| 406 | if (frame + framesPerUpdate >= info->frameCount) { |
| 407 | info->fileOffset = 9; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 |
| 412 | if (!loadedFromCache) { |
| 413 | if (!info->nextFrameIsCacheFrame || !info->precache) { |
| 414 | Surface surface((uint32_t *) pixels, (size_t) w, (size_t) h, (size_t) stride); |
| 415 | info->animation->renderSync((size_t) frame, surface, clear); |
| 416 | info->nextFrameIsCacheFrame = true; |
| 417 | } |
| 418 | } |
| 419 |
| 420 | AndroidBitmap_unlockPixels(env, bitmap); |
| 421 | } |
| 422 | return frame; |
| 423 | } |
| 424 | } |
| 425 |