| 5 | #include <lz4.h> |
| 6 | #include <unistd.h> |
| 7 | #include <pthread.h> |
| 8 | #include <tgnet/ConnectionsManager.h> |
| 9 | #include <tgnet/FileLog.h> |
| 10 | #include "c_utils.h" |
| 11 |
| 12 | extern "C" { |
| 13 | using namespace rlottie; |
| 14 |
| 15 | typedef struct LottieInfo { |
| 16 |
| 17 | ~LottieInfo() { |
| 18 | if (decompressBuffer != nullptr) { |
| 19 | delete[]decompressBuffer; |
| 20 | decompressBuffer = nullptr; |
| 21 | } |
| 22 | } |
| 23 |
| 24 | std::unique_ptr<Animation> animation; |
| 25 | size_t frameCount = 0; |
| 26 | int32_t fps = 30; |
| 27 | bool precache = false; |
| 28 | bool createCache = false; |
| 29 | std::string path; |
| 30 | std::string cacheFile; |
| 31 | uint8_t *decompressBuffer = nullptr; |
| 32 | uint32_t maxFrameSize = 0; |
| 33 | uint32_t imageSize = 0; |
| 34 | uint32_t fileOffset = 0; |
| 35 | bool nextFrameIsCacheFrame = false; |
| 36 | }; |
| 37 |
| 38 | jlong Java_org_telegram_ui_Components_RLottieDrawable_create(JNIEnv *env, jclass clazz, jstring src, jintArray data, jboolean precache) { |
| 39 | LottieInfo *info = new LottieInfo(); |
| 40 |
| 41 | char const *srcString = env->GetStringUTFChars(src, 0); |
| 42 | info->path = srcString; |
| 43 | info->animation = rlottie::Animation::loadFromFile(info->path); |
| 44 | if (srcString != 0) { |
| 45 | env->ReleaseStringUTFChars(src, srcString); |
| 46 | } |
| 47 | if (info->animation == nullptr) { |
| 48 | delete info; |
| 49 | return 0; |
| 50 | } |
| 51 | info->frameCount = info->animation->totalFrame(); |
| 52 | info->fps = (int) info->animation->frameRate(); |
| 53 | if (info->fps > 60 || info->frameCount > 600) { |
| 54 | delete info; |
| 55 | return 0; |
| 56 | } |
| 57 | info->precache = precache; |
| 58 | if (info->precache) { |
| 59 | info->cacheFile = info->path; |
| 60 | info->cacheFile += ".cache"; |
| 61 | FILE *precacheFile = fopen(info->cacheFile.c_str(), "r+"); |
| 62 | if (precacheFile == nullptr) { |
| 63 | info->createCache = true; |
| 64 | } else { |
| 65 | uint8_t temp; |
| 66 | size_t read = fread(&temp, sizeof(uint8_t), 1, precacheFile); |
| 67 | info->createCache = read != 1 || temp == 0; |
| 68 | if (!info->createCache) { |
| 69 | fread(&(info->maxFrameSize), sizeof(uint32_t), 1, precacheFile); |
| 70 | fread(&(info->imageSize), sizeof(uint32_t), 1, precacheFile); |
| 71 | info->fileOffset = 9; |
| 72 | } |
| 73 | fclose(precacheFile); |
| 74 | } |
| 75 | } |
| 76 |
| 77 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 78 | if (dataArr != nullptr) { |
| 79 | dataArr[0] = (jint) info->frameCount; |
| 80 | dataArr[1] = (jint) info->animation->frameRate(); |
| 81 | dataArr[2] = info->createCache ? 1 : 0; |
| 82 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 83 | } |
| 84 | return (jlong) (intptr_t) info; |
| 85 | } |
| 86 |
| 87 | jlong Java_org_telegram_ui_Components_RLottieDrawable_createWithJson(JNIEnv *env, jclass clazz, jstring json, jstring name, jintArray data) { |
| 88 | LottieInfo *info = new LottieInfo(); |
| 89 |
| 90 | char const *jsonString = env->GetStringUTFChars(json, 0); |
| 91 | char const *nameString = env->GetStringUTFChars(name, 0); |
| 92 | info->animation = rlottie::Animation::loadFromData(jsonString, nameString); |
| 93 | if (jsonString != 0) { |
| 94 | env->ReleaseStringUTFChars(json, jsonString); |
| 95 | } |
| 96 | if (nameString != 0) { |
| 97 | env->ReleaseStringUTFChars(name, nameString); |
| 98 | } |
| 99 | if (info->animation == nullptr) { |
| 100 | delete info; |
| 101 | return 0; |
| 102 | } |
| 103 | info->frameCount = info->animation->totalFrame(); |
| 104 | info->fps = (int) info->animation->frameRate(); |
| 105 |
| 106 | jint *dataArr = env->GetIntArrayElements(data, 0); |
| 107 | if (dataArr != nullptr) { |
| 108 | dataArr[0] = (int) info->frameCount; |
| 109 | dataArr[1] = (int) info->animation->frameRate(); |
| 110 | dataArr[2] = 0; |
| 111 | env->ReleaseIntArrayElements(data, dataArr, 0); |
| 112 | } |
| 113 | return (jlong) (intptr_t) info; |
| 114 | } |
| 115 |
| 116 | void Java_org_telegram_ui_Components_RLottieDrawable_destroy(JNIEnv *env, jclass clazz, jlong ptr) { |
| 117 | if (ptr == NULL) { |
| 118 | return; |
| 119 | } |
| 120 | LottieInfo *info = (LottieInfo *) (intptr_t) ptr; |
| 121 | delete info; |
| 122 | } |
| 123 |
| 124 | void Java_org_telegram_ui_Components_RLottieDrawable_setLayerColor(JNIEnv *env, jclass clazz, jlong ptr, jstring layer, jint color) { |
| 125 | if (ptr == NULL || layer == nullptr) { |
| 126 | return; |
| 127 | } |
| 128 | LottieInfo *info = (LottieInfo *) (intptr_t) ptr; |
| 129 | char const *layerString = env->GetStringUTFChars(layer, 0); |
| 130 | info->animation->setValue<Property::Color>(layerString, Color(((color) & 0xff) / 255.0f, ((color >> 8) & 0xff) / 255.0f, ((color >> 16) & 0xff) / 255.0f)); |
| 131 | if (layerString != 0) { |
| 132 | env->ReleaseStringUTFChars(layer, layerString); |
| 133 | } |
| 134 | } |
| 135 |
| 136 | void Java_org_telegram_ui_Components_RLottieDrawable_createCache(JNIEnv *env, jclass clazz, jlong ptr, jobject bitmap, jint w, jint h, jint stride) { |
| 137 | if (ptr == NULL || bitmap == nullptr) { |
| 138 | return; |
| 139 | } |
| 140 | LottieInfo *info = (LottieInfo *) (intptr_t) ptr; |
| 141 |
| 142 | FILE *precacheFile = fopen(info->cacheFile.c_str(), "r+"); |
| 143 | if (precacheFile != nullptr) { |
| 144 | uint8_t temp; |
| 145 | size_t read = fread(&temp, sizeof(uint8_t), 1, precacheFile); |
| 146 | fclose(precacheFile); |
| 147 | if (read == 1 && temp != 0) { |
| 148 | return; |
| 149 | } |
| 150 | } |
| 151 |
| 152 | void *pixels; |
| 153 | if (info->nextFrameIsCacheFrame && info->createCache && info->frameCount != 0 && w * 4 == stride && AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0) { |
| 154 | precacheFile = fopen(info->cacheFile.c_str(), "w+"); |
| 155 | if (precacheFile != nullptr) { |
| 156 | fseek(precacheFile, info->fileOffset = 9, SEEK_SET); |
| 157 |
| 158 | uint32_t size; |
| 159 | uint32_t firstFrameSize = 0; |
| 160 | info->maxFrameSize = 0; |
| 161 | int bound = LZ4_compressBound(w * h * 4); |
| 162 | uint8_t *compressBuffer = new uint8_t[bound]; |
| 163 | Surface surface((uint32_t *) pixels, (size_t) w, (size_t) h, (size_t) stride); |
| 164 | //int64_t time = ConnectionsManager::getInstance(0).getCurrentTimeMillis(); |
| 165 | //int totalSize = 0; |
| 166 | int framesPerUpdate = info->fps < 60 ? 1 : 2; |
| 167 | for (size_t a = 0; a < info->frameCount; a += framesPerUpdate) { |
| 168 | info->animation->renderSync(a, surface); |
| 169 | size = (uint32_t) LZ4_compress_default((const char *) pixels, (char *) compressBuffer, w * h * 4, bound); |
| 170 | //totalSize += size; |
| 171 | if (a == 0) { |
| 172 | firstFrameSize = size; |
| 173 | } |
| 174 | info->maxFrameSize = MAX(info->maxFrameSize, size); |
| 175 | fwrite(&size, sizeof(uint32_t), 1, precacheFile); |
| 176 | fwrite(compressBuffer, sizeof(uint8_t), size, precacheFile); |
| 177 | } |
| 178 | delete[] compressBuffer; |
| 179 | //DEBUG_D("total size %s = %d, time = %lld ms", info->path.c_str(), totalSize, (ConnectionsManager::getInstance(0).getCurrentTimeMillis() - time)); |
| 180 | fseek(precacheFile, 0, SEEK_SET); |
| 181 | uint8_t byte = 1; |
| 182 | info->imageSize = (uint32_t) w * h * 4; |
| 183 | fwrite(&byte, sizeof(uint8_t), 1, precacheFile); |
| 184 | fwrite(&info->maxFrameSize, sizeof(uint32_t), 1, precacheFile); |
| 185 | fwrite(&info->imageSize, sizeof(uint32_t), 1, precacheFile); |
| 186 | fflush(precacheFile); |
| 187 | int fd = fileno(precacheFile); |
| 188 | fsync(fd); |
| 189 | info->createCache = false; |
| 190 | info->fileOffset = 9 + sizeof(uint32_t) + firstFrameSize; |
| 191 | fclose(precacheFile); |
| 192 | } |
| 193 | AndroidBitmap_unlockPixels(env, bitmap); |
| 194 | } |
| 195 | } |
| 196 |
| 197 | jint Java_org_telegram_ui_Components_RLottieDrawable_getFrame(JNIEnv *env, jclass clazz, jlong ptr, jint frame, jobject bitmap, jint w, jint h, jint stride) { |
| 198 | if (ptr == NULL || bitmap == nullptr) { |
| 199 | return 0; |
| 200 | } |
| 201 | LottieInfo *info = (LottieInfo *) (intptr_t) ptr; |
| 202 | void *pixels; |
| 203 | if (AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0) { |
| 204 | bool loadedFromCache = false; |
| 205 | if (info->precache && !info->createCache && w * 4 == stride && info->maxFrameSize <= w * h * 4 && info->imageSize == w * h * 4) { |
| 206 | FILE *precacheFile = fopen(info->cacheFile.c_str(), "r"); |
| 207 | if (precacheFile != nullptr) { |
| 208 | fseek(precacheFile, info->fileOffset, SEEK_SET); |
| 209 | if (info->decompressBuffer == nullptr) { |
| 210 | info->decompressBuffer = new uint8_t[info->maxFrameSize]; |
| 211 | } |
| 212 | uint32_t frameSize; |
| 213 | fread(&frameSize, sizeof(uint32_t), 1, precacheFile); |
| 214 | if (frameSize <= info->maxFrameSize) { |
| 215 | fread(info->decompressBuffer, sizeof(uint8_t), frameSize, precacheFile); |
| 216 | info->fileOffset += 4 + frameSize; |
| 217 | LZ4_decompress_safe((const char *) info->decompressBuffer, (char *) pixels, frameSize, w * h * 4); |
| 218 | loadedFromCache = true; |
| 219 | } |
| 220 | fclose(precacheFile); |
| 221 | int framesPerUpdate = info->fps < 60 ? 1 : 2; |
| 222 | if (frame + framesPerUpdate >= info->frameCount) { |
| 223 | info->fileOffset = 9; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | if (!loadedFromCache) { |
| 228 | Surface surface((uint32_t *) pixels, (size_t) w, (size_t) h, (size_t) stride); |
| 229 | info->animation->renderSync((size_t) frame, surface); |
| 230 | info->nextFrameIsCacheFrame = true; |
| 231 | } |
| 232 |
| 233 | AndroidBitmap_unlockPixels(env, bitmap); |
| 234 | } |
| 235 | return frame; |
| 236 | } |
| 237 | } |
| 238 |