2019-07-18 13:01:39 +00:00
|
|
|
#include <jni.h>
|
|
|
|
#include <android/bitmap.h>
|
|
|
|
#include <cstring>
|
|
|
|
#include <rlottie.h>
|
|
|
|
#include <lz4.h>
|
|
|
|
#include <unistd.h>
|
2019-12-31 13:08:08 +00:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <atomic>
|
|
|
|
#include <thread>
|
2019-08-21 23:53:26 +00:00
|
|
|
#include <map>
|
2019-12-31 13:08:08 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <utime.h>
|
2020-09-30 13:48:47 +00:00
|
|
|
#include "tgnet/FileLog.h"
|
|
|
|
#include "tgnet/ConnectionsManager.h"
|
2019-07-18 13:01:39 +00:00
|
|
|
#include "c_utils.h"
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
using namespace rlottie;
|
|
|
|
|
|
|
|
typedef struct LottieInfo {
|
|
|
|
|
|
|
|
~LottieInfo() {
|
|
|
|
if (decompressBuffer != nullptr) {
|
|
|
|
delete[]decompressBuffer;
|
|
|
|
decompressBuffer = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<Animation> animation;
|
|
|
|
size_t frameCount = 0;
|
|
|
|
int32_t fps = 30;
|
|
|
|
bool precache = false;
|
|
|
|
bool createCache = false;
|
2019-12-31 13:08:08 +00:00
|
|
|
bool limitFps = false;
|
2019-07-18 13:01:39 +00:00
|
|
|
std::string path;
|
|
|
|
std::string cacheFile;
|
|
|
|
uint8_t *decompressBuffer = nullptr;
|
2019-12-31 13:08:08 +00:00
|
|
|
uint32_t decompressBufferSize = 0;
|
|
|
|
volatile uint32_t maxFrameSize = 0;
|
2019-07-18 13:01:39 +00:00
|
|
|
uint32_t imageSize = 0;
|
|
|
|
uint32_t fileOffset = 0;
|
2022-06-21 02:51:00 +00:00
|
|
|
uint32_t fileFrame = 0;
|
2019-07-18 13:01:39 +00:00
|
|
|
bool nextFrameIsCacheFrame = false;
|
2019-12-31 13:08:08 +00:00
|
|
|
|
|
|
|
char *compressBuffer = nullptr;
|
|
|
|
const char *buffer = nullptr;
|
|
|
|
bool firstFrame = false;
|
2021-06-25 00:43:10 +00:00
|
|
|
int bufferSize = 0;
|
|
|
|
int compressBound = 0;
|
|
|
|
int firstFrameSize = 0;
|
2019-12-31 13:08:08 +00:00
|
|
|
volatile uint32_t framesAvailableInCache = 0;
|
2019-07-18 13:01:39 +00:00
|
|
|
};
|
|
|
|
|
2021-12-08 12:02:09 +00:00
|
|
|
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) {
|
2021-06-25 00:43:10 +00:00
|
|
|
auto info = new LottieInfo();
|
2019-07-18 13:01:39 +00:00
|
|
|
|
2019-12-31 13:08:08 +00:00
|
|
|
std::map<int32_t, int32_t> *colors = nullptr;
|
2020-01-03 15:45:22 +00:00
|
|
|
int color = 0;
|
2019-08-21 23:53:26 +00:00
|
|
|
if (colorReplacement != nullptr) {
|
2021-06-25 00:43:10 +00:00
|
|
|
jint *arr = env->GetIntArrayElements(colorReplacement, nullptr);
|
2019-08-21 23:53:26 +00:00
|
|
|
if (arr != nullptr) {
|
|
|
|
jsize len = env->GetArrayLength(colorReplacement);
|
2019-12-31 13:08:08 +00:00
|
|
|
colors = new std::map<int32_t, int32_t>();
|
2019-08-21 23:53:26 +00:00
|
|
|
for (int32_t a = 0; a < len / 2; a++) {
|
2019-12-31 13:08:08 +00:00
|
|
|
(*colors)[arr[a * 2]] = arr[a * 2 + 1];
|
2020-01-03 15:45:22 +00:00
|
|
|
if (color == 0) {
|
|
|
|
color = arr[a * 2 + 1];
|
|
|
|
}
|
2019-08-21 23:53:26 +00:00
|
|
|
}
|
|
|
|
env->ReleaseIntArrayElements(colorReplacement, arr, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 12:02:09 +00:00
|
|
|
|
|
|
|
FitzModifier modifier = FitzModifier::None;
|
|
|
|
switch (fitzModifier) {
|
|
|
|
case 12:
|
|
|
|
modifier = FitzModifier::Type12;
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
modifier = FitzModifier::Type3;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
modifier = FitzModifier::Type4;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
modifier = FitzModifier::Type5;
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
modifier = FitzModifier::Type6;
|
|
|
|
break;
|
|
|
|
}
|
2021-06-25 00:43:10 +00:00
|
|
|
char const *srcString = env->GetStringUTFChars(src, nullptr);
|
2019-07-18 13:01:39 +00:00
|
|
|
info->path = srcString;
|
2021-06-25 00:43:10 +00:00
|
|
|
if (json != nullptr) {
|
|
|
|
char const *jsonString = env->GetStringUTFChars(json, nullptr);
|
|
|
|
if (jsonString) {
|
2021-12-08 12:02:09 +00:00
|
|
|
info->animation = rlottie::Animation::loadFromData(jsonString, info->path, colors, modifier);
|
2021-06-25 00:43:10 +00:00
|
|
|
env->ReleaseStringUTFChars(json, jsonString);
|
|
|
|
}
|
|
|
|
} else {
|
2021-12-08 12:02:09 +00:00
|
|
|
info->animation = rlottie::Animation::loadFromFile(info->path, colors, modifier);
|
2021-06-25 00:43:10 +00:00
|
|
|
}
|
|
|
|
if (srcString) {
|
2019-07-18 13:01:39 +00:00
|
|
|
env->ReleaseStringUTFChars(src, srcString);
|
|
|
|
}
|
|
|
|
if (info->animation == nullptr) {
|
|
|
|
delete info;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
info->frameCount = info->animation->totalFrame();
|
|
|
|
info->fps = (int) info->animation->frameRate();
|
2019-12-31 13:08:08 +00:00
|
|
|
info->limitFps = limitFps;
|
2019-07-18 13:01:39 +00:00
|
|
|
if (info->fps > 60 || info->frameCount > 600) {
|
|
|
|
delete info;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
info->precache = precache;
|
|
|
|
if (info->precache) {
|
|
|
|
info->cacheFile = info->path;
|
2021-06-25 00:43:10 +00:00
|
|
|
std::string::size_type index = info->cacheFile.find_last_of('/');
|
2019-12-31 13:08:08 +00:00
|
|
|
if (index != std::string::npos) {
|
|
|
|
std::string dir = info->cacheFile.substr(0, index) + "/acache";
|
|
|
|
mkdir(dir.c_str(), 0777);
|
|
|
|
info->cacheFile.insert(index, "/acache");
|
|
|
|
}
|
2020-01-03 15:45:22 +00:00
|
|
|
info->cacheFile += std::to_string(w) + "_" + std::to_string(h);
|
|
|
|
if (color != 0) {
|
|
|
|
info->cacheFile += "_" + std::to_string(color);
|
|
|
|
}
|
2019-12-31 13:08:08 +00:00
|
|
|
if (limitFps) {
|
2020-01-03 15:45:22 +00:00
|
|
|
info->cacheFile += ".s.cache";
|
2019-12-31 13:08:08 +00:00
|
|
|
} else {
|
2020-01-03 15:45:22 +00:00
|
|
|
info->cacheFile += ".cache";
|
2019-12-31 13:08:08 +00:00
|
|
|
}
|
2019-07-18 13:01:39 +00:00
|
|
|
FILE *precacheFile = fopen(info->cacheFile.c_str(), "r+");
|
|
|
|
if (precacheFile == nullptr) {
|
|
|
|
info->createCache = true;
|
|
|
|
} else {
|
|
|
|
uint8_t temp;
|
|
|
|
size_t read = fread(&temp, sizeof(uint8_t), 1, precacheFile);
|
|
|
|
info->createCache = read != 1 || temp == 0;
|
|
|
|
if (!info->createCache) {
|
2019-12-31 13:08:08 +00:00
|
|
|
uint32_t maxFrameSize;
|
|
|
|
fread(&maxFrameSize, sizeof(uint32_t), 1, precacheFile);
|
|
|
|
info->maxFrameSize = maxFrameSize;
|
2019-07-18 13:01:39 +00:00
|
|
|
fread(&(info->imageSize), sizeof(uint32_t), 1, precacheFile);
|
|
|
|
info->fileOffset = 9;
|
2022-06-21 02:51:00 +00:00
|
|
|
info->fileFrame = 0;
|
2021-06-25 00:43:10 +00:00
|
|
|
utimensat(0, info->cacheFile.c_str(), nullptr, 0);
|
2019-07-18 13:01:39 +00:00
|
|
|
}
|
|
|
|
fclose(precacheFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 00:43:10 +00:00
|
|
|
jint *dataArr = env->GetIntArrayElements(data, nullptr);
|
2019-07-18 13:01:39 +00:00
|
|
|
if (dataArr != nullptr) {
|
|
|
|
dataArr[0] = (jint) info->frameCount;
|
|
|
|
dataArr[1] = (jint) info->animation->frameRate();
|
|
|
|
dataArr[2] = info->createCache ? 1 : 0;
|
|
|
|
env->ReleaseIntArrayElements(data, dataArr, 0);
|
|
|
|
}
|
|
|
|
return (jlong) (intptr_t) info;
|
|
|
|
}
|
|
|
|
|
2020-09-30 13:48:47 +00:00
|
|
|
JNIEXPORT jlong Java_org_telegram_ui_Components_RLottieDrawable_createWithJson(JNIEnv *env, jclass clazz, jstring json, jstring name, jintArray data, jintArray colorReplacement) {
|
2019-12-31 13:08:08 +00:00
|
|
|
std::map<int32_t, int32_t> *colors = nullptr;
|
|
|
|
if (colorReplacement != nullptr) {
|
2021-06-25 00:43:10 +00:00
|
|
|
jint *arr = env->GetIntArrayElements(colorReplacement, nullptr);
|
2019-12-31 13:08:08 +00:00
|
|
|
if (arr != nullptr) {
|
|
|
|
jsize len = env->GetArrayLength(colorReplacement);
|
|
|
|
colors = new std::map<int32_t, int32_t>();
|
|
|
|
for (int32_t a = 0; a < len / 2; a++) {
|
|
|
|
(*colors)[arr[a * 2]] = arr[a * 2 + 1];
|
|
|
|
}
|
|
|
|
env->ReleaseIntArrayElements(colorReplacement, arr, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 00:43:10 +00:00
|
|
|
auto info = new LottieInfo();
|
2019-07-18 13:01:39 +00:00
|
|
|
|
2021-06-25 00:43:10 +00:00
|
|
|
char const *jsonString = env->GetStringUTFChars(json, nullptr);
|
|
|
|
char const *nameString = env->GetStringUTFChars(name, nullptr);
|
2019-12-31 13:08:08 +00:00
|
|
|
info->animation = rlottie::Animation::loadFromData(jsonString, nameString, colors);
|
2021-06-25 00:43:10 +00:00
|
|
|
if (jsonString) {
|
2019-07-18 13:01:39 +00:00
|
|
|
env->ReleaseStringUTFChars(json, jsonString);
|
|
|
|
}
|
2021-06-25 00:43:10 +00:00
|
|
|
if (nameString) {
|
2019-07-18 13:01:39 +00:00
|
|
|
env->ReleaseStringUTFChars(name, nameString);
|
|
|
|
}
|
|
|
|
if (info->animation == nullptr) {
|
|
|
|
delete info;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
info->frameCount = info->animation->totalFrame();
|
|
|
|
info->fps = (int) info->animation->frameRate();
|
|
|
|
|
2021-06-25 00:43:10 +00:00
|
|
|
jint *dataArr = env->GetIntArrayElements(data, nullptr);
|
2019-07-18 13:01:39 +00:00
|
|
|
if (dataArr != nullptr) {
|
|
|
|
dataArr[0] = (int) info->frameCount;
|
|
|
|
dataArr[1] = (int) info->animation->frameRate();
|
|
|
|
dataArr[2] = 0;
|
|
|
|
env->ReleaseIntArrayElements(data, dataArr, 0);
|
|
|
|
}
|
|
|
|
return (jlong) (intptr_t) info;
|
|
|
|
}
|
|
|
|
|
2020-09-30 13:48:47 +00:00
|
|
|
JNIEXPORT void Java_org_telegram_ui_Components_RLottieDrawable_destroy(JNIEnv *env, jclass clazz, jlong ptr) {
|
|
|
|
if (!ptr) {
|
2019-07-18 13:01:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-06-25 00:43:10 +00:00
|
|
|
auto info = (LottieInfo *) (intptr_t) ptr;
|
2019-07-18 13:01:39 +00:00
|
|
|
delete info;
|
|
|
|
}
|
|
|
|
|
2020-09-30 13:48:47 +00:00
|
|
|
JNIEXPORT void Java_org_telegram_ui_Components_RLottieDrawable_setLayerColor(JNIEnv *env, jclass clazz, jlong ptr, jstring layer, jint color) {
|
|
|
|
if (!ptr || layer == nullptr) {
|
2019-07-18 13:01:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-06-25 00:43:10 +00:00
|
|
|
auto info = (LottieInfo *) (intptr_t) ptr;
|
|
|
|
char const *layerString = env->GetStringUTFChars(layer, nullptr);
|
2019-07-18 13:01:39 +00:00
|
|
|
info->animation->setValue<Property::Color>(layerString, Color(((color) & 0xff) / 255.0f, ((color >> 8) & 0xff) / 255.0f, ((color >> 16) & 0xff) / 255.0f));
|
2021-06-25 00:43:10 +00:00
|
|
|
if (layerString) {
|
2019-07-18 13:01:39 +00:00
|
|
|
env->ReleaseStringUTFChars(layer, layerString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-30 13:48:47 +00:00
|
|
|
JNIEXPORT void Java_org_telegram_ui_Components_RLottieDrawable_replaceColors(JNIEnv *env, jclass clazz, jlong ptr, jintArray colorReplacement) {
|
|
|
|
if (!ptr || colorReplacement == nullptr) {
|
2019-12-31 13:08:08 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-06-25 00:43:10 +00:00
|
|
|
auto info = (LottieInfo *) (intptr_t) ptr;
|
2019-12-31 13:08:08 +00:00
|
|
|
|
2021-06-25 00:43:10 +00:00
|
|
|
jint *arr = env->GetIntArrayElements(colorReplacement, nullptr);
|
2019-12-31 13:08:08 +00:00
|
|
|
if (arr != nullptr) {
|
|
|
|
jsize len = env->GetArrayLength(colorReplacement);
|
|
|
|
for (int32_t a = 0; a < len / 2; a++) {
|
|
|
|
(*info->animation->colorMap)[arr[a * 2]] = arr[a * 2 + 1];
|
|
|
|
}
|
|
|
|
info->animation->resetCurrentFrame();
|
|
|
|
env->ReleaseIntArrayElements(colorReplacement, arr, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 13:01:39 +00:00
|
|
|
|
2020-10-30 10:26:29 +00:00
|
|
|
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) {
|
2020-09-30 13:48:47 +00:00
|
|
|
if (!ptr || bitmap == nullptr) {
|
2019-07-18 13:01:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2021-06-25 00:43:10 +00:00
|
|
|
auto info = (LottieInfo *) (intptr_t) ptr;
|
2019-12-31 13:08:08 +00:00
|
|
|
|
2019-07-18 13:01:39 +00:00
|
|
|
void *pixels;
|
2022-08-12 15:23:51 +00:00
|
|
|
bool result = false;
|
2019-07-18 13:01:39 +00:00
|
|
|
if (AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0) {
|
2022-08-12 15:23:51 +00:00
|
|
|
Surface surface((uint32_t *) pixels, (size_t) w, (size_t) h, (size_t) stride);
|
|
|
|
info->animation->renderSync((size_t) frame, surface, clear, &result);
|
2019-07-18 13:01:39 +00:00
|
|
|
AndroidBitmap_unlockPixels(env, bitmap);
|
|
|
|
}
|
2022-08-12 15:23:51 +00:00
|
|
|
if (!result) {
|
|
|
|
return -5;
|
|
|
|
}
|
2019-07-18 13:01:39 +00:00
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
}
|