| 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) { |
| 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 | char const *srcString = env->GetStringUTFChars(src, nullptr); |
| 75 | info->path = srcString; |
| 76 | if (json != nullptr) { |
| 77 | char const *jsonString = env->GetStringUTFChars(json, nullptr); |
| 78 | if (jsonString) { |
| 79 | info->animation = rlottie::Animation::loadFromData(jsonString, info->path, colors); |
| 80 | env->ReleaseStringUTFChars(json, jsonString); |
| 81 | } |
| 82 | } else { |
| 83 | info->animation = rlottie::Animation::loadFromFile(info->path, colors); |
| 84 | } |
| 85 | if (srcString) { |
| 86 | env->ReleaseStringUTFChars(src, srcString); |
| 87 | } |
| 88 | if (info->animation == nullptr) { |
| 89 | delete info; |
| 90 | return 0; |
| 91 | } |
| 92 | info->frameCount = info->animation->totalFrame(); |
| 93 | info->fps = (int) info->animation->frameRate(); |
| 94 | info->limitFps = limitFps; |
| 95 | if (info->fps > 60 || info->frameCount > 600) { |
| 96 | delete info; |
| 97 | return 0; |
| 98 | } |
| 99 | info->precache = precache; |
| 100 | if (info->precache) { |
| 101 | info->cacheFile = info->path; |
| 102 | std::string::size_type index = info->cacheFile.find_last_of('/'); |
| 103 | if (index != std::string::npos) { |
| 104 | std::string dir = info->cacheFile.substr(0, index) + "/acache"; |
| 105 | mkdir(dir.c_str(), 0777); |
| 106 | info->cacheFile.insert(index, "/acache"); |
| 107 | } |
| 108 | info->cacheFile += std::to_string(w) + "_" + std::to_string(h); |
| 109 | if (color != 0) { |
| 110 | info->cacheFile += "_" + std::to_string(color); |
| 111 | } |
| 112 | if (limitFps) { |
| 113 | info->cacheFile += ".s.cache"; |
| 114 | } else { |
| 115 | info->cacheFile += ".cache"; |
| 116 | } |
| 117 | FILE *precacheFile = fopen(info->cacheFile.c_str(), "r+"); |
| 118 | if (precacheFile == nullptr) { |
| 119 | info->createCache = true; |
| 120 | } else { |
| 121 | uint8_t temp; |
| 122 | size_t read = fread(&temp, sizeof(uint8_t), 1, precacheFile); |
| 123 | info->createCache = read != 1 || temp == 0; |
| 124 | if (!info->createCache) { |
| 125 | uint32_t maxFrameSize; |
| 126 | fread(&maxFrameSize, sizeof(uint32_t), 1, precacheFile); |
| 127 | info->maxFrameSize = maxFrameSize; |
| 128 | fread(&(info->imageSize), sizeof(uint32_t), 1, precacheFile); |
| 129 | info->fileOffset = 9; |
| 130 | utimensat(0, info->cacheFile.c_str(), nullptr, 0); |
| 131 | } |
| 132 | fclose(precacheFile); |
| 133 | } |
| 134 | } |
| 135 |
| 136 | jint *dataArr = env->GetIntArrayElements(data, nullptr); |
| 137 | if (dataArr != nullptr) { |
| 138 | dataArr[0] = (jint) info->frameCount; |
| 139 | dataArr[1] = (jint) info->animation->frameRate(); |
| 140 | dataArr[2] = info->createCache ? 1 : 0; |
| 141 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 142 | } |
| 143 | return (jlong) (intptr_t) info; |
| 144 | } |
| 145 |
| 146 | JNIEXPORT jlong Java_org_telegram_ui_Components_RLottieDrawable_createWithJson(JNIEnv *env, jclass clazz, jstring json, jstring name, jintArray data, jintArray colorReplacement) { |
| 147 | std::map<int32_t, int32_t> *colors = nullptr; |
| 148 | if (colorReplacement != nullptr) { |
| 149 | jint *arr = env->GetIntArrayElements(colorReplacement, nullptr); |
| 150 | if (arr != nullptr) { |
| 151 | jsize len = env->GetArrayLength(colorReplacement); |
| 152 | colors = new std::map<int32_t, int32_t>(); |
| 153 | for (int32_t a = 0; a < len / 2; a++) { |
| 154 | (*colors)[arr[a * 2]] = arr[a * 2 + 1]; |
| 155 | } |
| 156 | env->ReleaseIntArrayElements(colorReplacement, arr, 0); |
| 157 | } |
| 158 | } |
| 159 |
| 160 | auto info = new LottieInfo(); |
| 161 |
| 162 | char const *jsonString = env->GetStringUTFChars(json, nullptr); |
| 163 | char const *nameString = env->GetStringUTFChars(name, nullptr); |
| 164 | info->animation = rlottie::Animation::loadFromData(jsonString, nameString, colors); |
| 165 | if (jsonString) { |
| 166 | env->ReleaseStringUTFChars(json, jsonString); |
| 167 | } |
| 168 | if (nameString) { |
| 169 | env->ReleaseStringUTFChars(name, nameString); |
| 170 | } |
| 171 | if (info->animation == nullptr) { |
| 172 | delete info; |
| 173 | return 0; |
| 174 | } |
| 175 | info->frameCount = info->animation->totalFrame(); |
| 176 | info->fps = (int) info->animation->frameRate(); |
| 177 |
| 178 | jint *dataArr = env->GetIntArrayElements(data, nullptr); |
| 179 | if (dataArr != nullptr) { |
| 180 | dataArr[0] = (int) info->frameCount; |
| 181 | dataArr[1] = (int) info->animation->frameRate(); |
| 182 | dataArr[2] = 0; |
| 183 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 184 | } |
| 185 | return (jlong) (intptr_t) info; |
| 186 | } |
| 187 |
| 188 | JNIEXPORT void Java_org_telegram_ui_Components_RLottieDrawable_destroy(JNIEnv *env, jclass clazz, jlong ptr) { |
| 189 | if (!ptr) { |
| 190 | return; |
| 191 | } |
| 192 | auto info = (LottieInfo *) (intptr_t) ptr; |
| 193 | delete info; |
| 194 | } |
| 195 |
| 196 | JNIEXPORT void Java_org_telegram_ui_Components_RLottieDrawable_setLayerColor(JNIEnv *env, jclass clazz, jlong ptr, jstring layer, jint color) { |
| 197 | if (!ptr || layer == nullptr) { |
| 198 | return; |
| 199 | } |
| 200 | auto info = (LottieInfo *) (intptr_t) ptr; |
| 201 | char const *layerString = env->GetStringUTFChars(layer, nullptr); |
| 202 | info->animation->setValue<Property::Color>(layerString, Color(((color) & 0xff) / 255.0f, ((color >> 8) & 0xff) / 255.0f, ((color >> 16) & 0xff) / 255.0f)); |
| 203 | if (layerString) { |
| 204 | env->ReleaseStringUTFChars(layer, layerString); |
| 205 | } |
| 206 | } |
| 207 |
| 208 | JNIEXPORT void Java_org_telegram_ui_Components_RLottieDrawable_replaceColors(JNIEnv *env, jclass clazz, jlong ptr, jintArray colorReplacement) { |
| 209 | if (!ptr || colorReplacement == nullptr) { |
| 210 | return; |
| 211 | } |
| 212 | auto info = (LottieInfo *) (intptr_t) ptr; |
| 213 |
| 214 | jint *arr = env->GetIntArrayElements(colorReplacement, nullptr); |
| 215 | if (arr != nullptr) { |
| 216 | jsize len = env->GetArrayLength(colorReplacement); |
| 217 | for (int32_t a = 0; a < len / 2; a++) { |
| 218 | (*info->animation->colorMap)[arr[a * 2]] = arr[a * 2 + 1]; |
| 219 | } |
| 220 | info->animation->resetCurrentFrame(); |
| 221 | env->ReleaseIntArrayElements(colorReplacement, arr, 0); |
| 222 | } |
| 223 | } |
| 224 |
| 225 | bool cacheWriteThreadCreated{false}; |
| 226 | LottieInfo *cacheWriteThreadTask{nullptr}; |
| 227 | bool cacheWriteThreadDone{false}; |
| 228 | std::thread worker; |
| 229 | std::mutex cacheMutex; |
| 230 | std::condition_variable cacheCv; |
| 231 |
| 232 | std::mutex cacheDoneMutex; |
| 233 | std::condition_variable cacheDoneCv; |
| 234 | std::atomic<bool> frameReady{false}; |
| 235 |
| 236 | void CacheWriteThreadProc() { |
| 237 | while (!cacheWriteThreadDone) { |
| 238 | std::unique_lock<std::mutex> lk(cacheMutex); |
| 239 | cacheCv.wait(lk, [] { return frameReady.load(); }); |
| 240 | std::lock_guard<std::mutex> lg(cacheDoneMutex); |
| 241 | LottieInfo *task; |
| 242 | if (cacheWriteThreadTask != nullptr) { |
| 243 | task = cacheWriteThreadTask; |
| 244 | cacheWriteThreadTask = nullptr; |
| 245 | } else { |
| 246 | task = nullptr; |
| 247 | } |
| 248 | lk.unlock(); |
| 249 |
| 250 | if (task != nullptr) { |
| 251 | auto size = (uint32_t) LZ4_compress_default(task->buffer, task->compressBuffer, task->bufferSize, task->compressBound); |
| 252 | if (task->firstFrame) { |
| 253 | task->firstFrameSize = size; |
| 254 | task->fileOffset = 9 + sizeof(uint32_t) + task->firstFrameSize; |
| 255 | } |
| 256 | task->maxFrameSize = MAX(task->maxFrameSize, size); |
| 257 | fwrite(&size, sizeof(uint32_t), 1, task->precacheFile); |
| 258 | fwrite(task->compressBuffer, sizeof(uint8_t), size, task->precacheFile); |
| 259 |
| 260 | fflush(task->precacheFile); |
| 261 | fsync(fileno(task->precacheFile)); |
| 262 | task->framesAvailableInCache++; |
| 263 | } |
| 264 | frameReady = false; |
| 265 | cacheDoneCv.notify_one(); |
| 266 | } |
| 267 | } |
| 268 |
| 269 | JNIEXPORT void Java_org_telegram_ui_Components_RLottieDrawable_createCache(JNIEnv *env, jclass clazz, jlong ptr, jint w, jint h) { |
| 270 | if (ptr == NULL) { |
| 271 | return; |
| 272 | } |
| 273 | auto info = (LottieInfo *) (intptr_t) ptr; |
| 274 |
| 275 | FILE *cacheFile = fopen(info->cacheFile.c_str(), "r+"); |
| 276 | if (cacheFile != nullptr) { |
| 277 | uint8_t temp; |
| 278 | size_t read = fread(&temp, sizeof(uint8_t), 1, cacheFile); |
| 279 | fclose(cacheFile); |
| 280 | if (read == 1 && temp != 0) { |
| 281 | return; |
| 282 | } |
| 283 | } |
| 284 |
| 285 | if (!cacheWriteThreadCreated) { |
| 286 | cacheWriteThreadCreated = true; |
| 287 | worker = std::thread(CacheWriteThreadProc); |
| 288 | } |
| 289 |
| 290 | if (info->nextFrameIsCacheFrame && info->createCache && info->frameCount != 0) { |
| 291 | info->precacheFile = fopen(info->cacheFile.c_str(), "w+"); |
| 292 | if (info->precacheFile != nullptr) { |
| 293 | fseek(info->precacheFile, info->fileOffset = 9, SEEK_SET); |
| 294 | info->maxFrameSize = 0; |
| 295 | info->bufferSize = w * h * 4; |
| 296 | info->imageSize = (uint32_t) w * h * 4; |
| 297 | info->compressBound = LZ4_compressBound(info->bufferSize); |
| 298 | info->compressBuffer = new char[info->compressBound]; |
| 299 | auto firstBuffer = new uint8_t[info->bufferSize]; |
| 300 | auto secondBuffer = new uint8_t[info->bufferSize]; |
| 301 | //long time = ConnectionsManager::getInstance(0).getCurrentTimeMonotonicMillis(); |
| 302 |
| 303 | Surface surface1((uint32_t *) firstBuffer, (size_t) w, (size_t) h, (size_t) w * 4); |
| 304 | Surface surface2((uint32_t *) secondBuffer, (size_t) w, (size_t) h, (size_t) w * 4); |
| 305 | int framesPerUpdate = !info->limitFps || info->fps < 60 ? 1 : 2; |
| 306 | int num = 0; |
| 307 | for (size_t a = 0; a < info->frameCount; a += framesPerUpdate) { |
| 308 | Surface &surfaceToRender = num % 2 == 0 ? surface1 : surface2; |
| 309 | num++; |
| 310 | info->animation->renderSync(a, surfaceToRender, true); |
| 311 | if (a != 0) { |
| 312 | std::unique_lock<std::mutex> lk(cacheDoneMutex); |
| 313 | cacheDoneCv.wait(lk, [] { return !frameReady.load(); }); |
| 314 | } |
| 315 |
| 316 | std::lock_guard<std::mutex> lg(cacheMutex); |
| 317 | cacheWriteThreadTask = info; |
| 318 | info->firstFrame = a == 0; |
| 319 | info->buffer = (const char *) surfaceToRender.buffer(); |
| 320 | frameReady = true; |
| 321 | cacheCv.notify_one(); |
| 322 | } |
| 323 | std::unique_lock<std::mutex> lk(cacheDoneMutex); |
| 324 | cacheDoneCv.wait(lk, [] { return !frameReady.load(); }); |
| 325 |
| 326 | //DEBUG_D("sticker time = %d", (int) (ConnectionsManager::getInstance(0).getCurrentTimeMonotonicMillis() - time)); |
| 327 | delete[] info->compressBuffer; |
| 328 | delete[] firstBuffer; |
| 329 | delete[] secondBuffer; |
| 330 | fseek(info->precacheFile, 0, SEEK_SET); |
| 331 | uint8_t byte = 1; |
| 332 | fwrite(&byte, sizeof(uint8_t), 1, info->precacheFile); |
| 333 | uint32_t maxFrameSize = info->maxFrameSize; |
| 334 | fwrite(&maxFrameSize, sizeof(uint32_t), 1, info->precacheFile); |
| 335 | fwrite(&info->imageSize, sizeof(uint32_t), 1, info->precacheFile); |
| 336 | fflush(info->precacheFile); |
| 337 | fsync(fileno(info->precacheFile)); |
| 338 | info->createCache = false; |
| 339 | fclose(info->precacheFile); |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 |
| 344 | 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) { |
| 345 | if (!ptr || bitmap == nullptr) { |
| 346 | return 0; |
| 347 | } |
| 348 | auto info = (LottieInfo *) (intptr_t) ptr; |
| 349 |
| 350 | int framesPerUpdate = !info->limitFps || info->fps < 60 ? 1 : 2; |
| 351 | int framesAvailableInCache = info->framesAvailableInCache; |
| 352 |
| 353 | if (info->createCache && info->precache && frame > 0) { |
| 354 | if (frame / framesPerUpdate >= framesAvailableInCache) { |
| 355 | return -1; |
| 356 | } |
| 357 | } |
| 358 |
| 359 | void *pixels; |
| 360 | if (AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0) { |
| 361 | bool loadedFromCache = false; |
| 362 | uint32_t maxFrameSize = info->maxFrameSize; |
| 363 | if (info->precache && (!info->createCache || frame > 0) && w * 4 == stride && maxFrameSize <= w * h * 4 && info->imageSize == w * h * 4) { |
| 364 | FILE *precacheFile = fopen(info->cacheFile.c_str(), "r"); |
| 365 | if (precacheFile != nullptr) { |
| 366 | if (info->decompressBuffer != nullptr && info->decompressBufferSize < maxFrameSize) { |
| 367 | delete[] info->decompressBuffer; |
| 368 | info->decompressBuffer = nullptr; |
| 369 | } |
| 370 | if (info->decompressBuffer == nullptr) { |
| 371 | info->decompressBufferSize = maxFrameSize; |
| 372 | if (info->createCache) { |
| 373 | info->decompressBufferSize += 10000; |
| 374 | } |
| 375 | info->decompressBuffer = new uint8_t[info->decompressBufferSize]; |
| 376 | } |
| 377 | fseek(precacheFile, info->fileOffset, SEEK_SET); |
| 378 | uint32_t frameSize; |
| 379 | fread(&frameSize, sizeof(uint32_t), 1, precacheFile); |
| 380 | if (frameSize > 0 && frameSize <= info->decompressBufferSize) { |
| 381 | fread(info->decompressBuffer, sizeof(uint8_t), frameSize, precacheFile); |
| 382 | info->fileOffset += 4 + frameSize; |
| 383 | LZ4_decompress_safe((const char *) info->decompressBuffer, (char *) pixels, frameSize, w * h * 4); |
| 384 | loadedFromCache = true; |
| 385 | } |
| 386 | fclose(precacheFile); |
| 387 | if (frame + framesPerUpdate >= info->frameCount) { |
| 388 | info->fileOffset = 9; |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 |
| 393 | if (!loadedFromCache) { |
| 394 | if (!info->nextFrameIsCacheFrame || !info->precache) { |
| 395 | Surface surface((uint32_t *) pixels, (size_t) w, (size_t) h, (size_t) stride); |
| 396 | info->animation->renderSync((size_t) frame, surface, clear); |
| 397 | info->nextFrameIsCacheFrame = true; |
| 398 | } |
| 399 | } |
| 400 |
| 401 | AndroidBitmap_unlockPixels(env, bitmap); |
| 402 | } |
| 403 | return frame; |
| 404 | } |
| 405 | } |
| 406 |