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