Skip to content

Commit

Permalink
Merge branch 'master' into partialRender
Browse files Browse the repository at this point in the history
  • Loading branch information
X-Ryl669 committed Jan 6, 2022
2 parents 8199f69 + f3eed9a commit 89f6ece
Show file tree
Hide file tree
Showing 26 changed files with 342 additions and 234 deletions.
1 change: 1 addition & 0 deletions format
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
git diff -U0 --no-color HEAD^ | clang-format-diff -i -p1
30 changes: 30 additions & 0 deletions inc/rlottie_capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,36 @@ typedef enum {

typedef struct Lottie_Animation_S Lottie_Animation;

/**
* @brief Runs lottie initialization code when rlottie library is loaded
* dynamically.
*
*
* This api should be called before any other api when rlottie library
* is loaded using dlopen() or equivalent.
*
* @see lottie_shutdown()
*
* @ingroup Lottie_Animation
* @internal
*/
RLOTTIE_API void lottie_init(void);

/**
* @brief Runs lottie teardown code when rlottie library is loaded
* dynamically.
*
* This api should be called before unloading the rlottie library for
* proper cleanup of the resource without doing so will result in undefined
* behaviour.
*
* @see lottie_init()
*
* @ingroup Lottie_Animation
* @internal
*/
RLOTTIE_API void lottie_shutdown(void);

/**
* @brief Constructs an animation object from file path.
*
Expand Down
31 changes: 31 additions & 0 deletions src/binding/c/lottieanimation_capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

using namespace rlottie;

extern void lottie_init_impl();
extern void lottie_shutdown_impl();

extern "C" {
#include <string.h>
#include <stdarg.h>
Expand All @@ -38,6 +41,34 @@ struct Lottie_Animation_S
LOTMarkerList *mMarkerList;
};

static uint32_t _lottie_lib_ref_count = 0;

RLOTTIE_API void lottie_init(void)
{
if (_lottie_lib_ref_count > 0) {
_lottie_lib_ref_count++;
return;
}
lottie_init_impl();

_lottie_lib_ref_count = 1;
}

RLOTTIE_API void lottie_shutdown(void)
{
if (_lottie_lib_ref_count <= 0) {
// lottie_init() is not called before lottie_shutdown()
// or multiple shutdown is getting called.
return;
}

_lottie_lib_ref_count--;

if (_lottie_lib_ref_count == 0) {
lottie_shutdown_impl();
}
}

RLOTTIE_API Lottie_Animation_S *lottie_animation_from_file(const char *path)
{
if (auto animation = Animation::loadFromFile(path) ) {
Expand Down
74 changes: 57 additions & 17 deletions src/lottie/lottieanimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ class AnimationImpl {
public:
void init(std::shared_ptr<model::Composition> composition);
bool update(size_t frameNo, const VSize &size, bool keepAspectRatio);
bool updatePartial(size_t frameNo, const VSize &size, uint offset);
bool updatePartial(size_t frameNo, const VSize &size, uint32_t offset);
VSize size() const { return mModel->size(); }
double duration() const { return mModel->duration(); }
double frameRate() const { return mModel->frameRate(); }
size_t totalFrame() const { return mModel->totalFrame(); }
size_t frameAtPos(double pos) const { return mModel->frameAtPos(pos); }
bool checkRender();
Surface render(size_t frameNo, const Surface &surface,
bool keepAspectRatio);
Surface renderPartial(size_t frameNo, const Surface &surface);
Expand Down Expand Up @@ -107,8 +108,7 @@ bool AnimationImpl::update(size_t frameNo, const VSize &size,
return mRenderer->update(int(frameNo), size, keepAspectRatio);
}

bool AnimationImpl::updatePartial(size_t frameNo, const VSize &size,
uint offset)
bool AnimationImpl::updatePartial(size_t frameNo, const VSize &size, uint32_t offset)
{
frameNo += mModel->startFrame();

Expand All @@ -120,16 +120,22 @@ bool AnimationImpl::updatePartial(size_t frameNo, const VSize &size,
}


Surface AnimationImpl::render(size_t frameNo, const Surface &surface,
bool keepAspectRatio)
bool AnimationImpl::checkRender()
{
bool renderInProgress = mRenderInProgress.load();
if (renderInProgress) {
vCritical << "Already Rendering Scheduled for this Animation";
return surface;
return false;
}

mRenderInProgress.store(true);
return true;
}

Surface AnimationImpl::render(size_t frameNo, const Surface &surface,
bool keepAspectRatio)
{
if (!checkRender()) return surface;

update(
frameNo,
VSize(int(surface.drawRegionWidth()), int(surface.drawRegionHeight())),
Expand All @@ -142,17 +148,12 @@ Surface AnimationImpl::render(size_t frameNo, const Surface &surface,

Surface AnimationImpl::renderPartial(size_t frameNo, const Surface &surface)
{
bool renderInProgress = mRenderInProgress.load();
if (renderInProgress) {
vCritical << "Already Rendering Scheduled for this Animation";
return surface;
}
if (!checkRender()) return surface;

mRenderInProgress.store(true);
updatePartial(
frameNo,
VSize(int(surface.drawRegionWidth()), int(surface.drawRegionHeight())),
uint(surface.drawRegionPosY()));
uint32_t(surface.drawRegionPosY()));
mRenderer->renderPartial(surface);
mRenderInProgress.store(false);

Expand Down Expand Up @@ -213,20 +214,29 @@ class RenderTaskScheduler {
for (unsigned n = 0; n != _count; ++n) {
_threads.emplace_back([&, n] { run(n); });
}

IsRunning = true;
}

public:
static bool IsRunning;

static RenderTaskScheduler &instance()
{
static RenderTaskScheduler singleton;
return singleton;
}

~RenderTaskScheduler()
~RenderTaskScheduler() { stop(); }

void stop()
{
for (auto &e : _q) e.done();
if (IsRunning) {
IsRunning = false;

for (auto &e : _threads) e.join();
for (auto &e : _q) e.done();
for (auto &e : _threads) e.join();
}
}

std::future<Surface> process(SharedRenderTask task)
Expand All @@ -249,12 +259,16 @@ class RenderTaskScheduler {
#else
class RenderTaskScheduler {
public:
static bool IsRunning;

static RenderTaskScheduler &instance()
{
static RenderTaskScheduler singleton;
return singleton;
}

void stop() {}

std::future<Surface> process(SharedRenderTask task)
{
auto result = task->playerImpl->render(task->frameNo, task->surface,
Expand All @@ -263,8 +277,11 @@ class RenderTaskScheduler {
return std::move(task->receiver);
}
};

#endif

bool RenderTaskScheduler::IsRunning{false};

std::future<Surface> AnimationImpl::renderAsync(size_t frameNo,
Surface &&surface,
bool keepAspectRatio)
Expand Down Expand Up @@ -481,6 +498,29 @@ void Surface::setDrawRegion(size_t x, size_t y, size_t width, size_t height)
mDrawArea.h = height;
}

namespace {
void lottieShutdownRenderTaskScheduler()
{
if (RenderTaskScheduler::IsRunning) {
RenderTaskScheduler::instance().stop();
}
}
} // namespace

// private apis exposed to c interface
void lottie_init_impl()
{
// do nothing for now.
}

extern void lottieShutdownRasterTaskScheduler();

void lottie_shutdown_impl()
{
lottieShutdownRenderTaskScheduler();
lottieShutdownRasterTaskScheduler();
}

#ifdef LOTTIE_LOGGING_SUPPORT
void initLogging()
{
Expand Down
6 changes: 3 additions & 3 deletions src/lottie/lottiefiltermodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class FilterData {
public:
void addValue(LOTVariant& value)
{
uint index = static_cast<uint>(value.property());
uint32_t index = static_cast<uint32_t>(value.property());
if (mBitset.test(index)) {
std::replace_if(mFilters.begin(), mFilters.end(),
[&value](const LOTVariant& e) {
Expand All @@ -253,7 +253,7 @@ class FilterData {

void removeValue(LOTVariant& value)
{
uint index = static_cast<uint>(value.property());
uint32_t index = static_cast<uint32_t>(value.property());
if (mBitset.test(index)) {
mBitset.reset(index);
mFilters.erase(std::remove_if(mFilters.begin(), mFilters.end(),
Expand All @@ -266,7 +266,7 @@ class FilterData {
}
bool hasFilter(rlottie::Property prop) const
{
return mBitset.test(static_cast<uint>(prop));
return mBitset.test(static_cast<uint32_t>(prop));
}
model::Color color(rlottie::Property prop, int frame) const
{
Expand Down
34 changes: 17 additions & 17 deletions src/lottie/lottieitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ bool renderer::Composition::updatePartial(int frameNo, const VSize &size,

bool renderer::Composition::render(const rlottie::Surface &surface)
{
mSurface.reset(reinterpret_cast<uchar *>(surface.buffer()),
uint(surface.width()), uint(surface.height()),
uint(surface.bytesPerLine()),
mSurface.reset(reinterpret_cast<uint8_t *>(surface.buffer()),
uint32_t(surface.width()), uint32_t(surface.height()),
uint32_t(surface.bytesPerLine()),
VBitmap::Format::ARGB32_Premultiplied);

/* schedule all preprocess task for this frame at once.
Expand All @@ -201,9 +201,9 @@ bool renderer::Composition::render(const rlottie::Surface &surface)

bool renderer::Composition::renderPartial(const rlottie::Surface &surface)
{
mSurface.reset(reinterpret_cast<uchar *>(surface.buffer()),
uint(surface.width()), uint(surface.drawRegionHeight()),
uint(surface.bytesPerLine()),
mSurface.reset(reinterpret_cast<uint8_t *>(surface.buffer()),
uint32_t(surface.width()), uint32_t(surface.drawRegionHeight()),
uint32_t(surface.bytesPerLine()),
VBitmap::Format::ARGB32_Premultiplied);

/* schedule all preprocess task for this frame at once.
Expand Down Expand Up @@ -253,7 +253,7 @@ VRle renderer::Mask::rle()
{
if (!vCompare(mCombinedAlpha, 1.0f)) {
VRle obj = mRasterizer.rle();
obj *= uchar(mCombinedAlpha * 255);
obj *= uint8_t(mCombinedAlpha * 255);
return obj;
} else {
return mRasterizer.rle();
Expand Down Expand Up @@ -396,7 +396,7 @@ renderer::Layer::Layer(model::Layer *layerData) : mLayerData(layerData)
mLayerMask = std::make_unique<renderer::LayerMask>(mLayerData);
}

bool renderer::Layer::resolveKeyPath(LOTKeyPath &keyPath, uint depth,
bool renderer::Layer::resolveKeyPath(LOTKeyPath &keyPath, uint32_t depth,
LOTVariant &value)
{
if (!keyPath.matches(name(), depth)) {
Expand All @@ -412,25 +412,25 @@ bool renderer::Layer::resolveKeyPath(LOTKeyPath &keyPath, uint depth,
return true;
}

bool renderer::ShapeLayer::resolveKeyPath(LOTKeyPath &keyPath, uint depth,
bool renderer::ShapeLayer::resolveKeyPath(LOTKeyPath &keyPath, uint32_t depth,
LOTVariant &value)
{
if (renderer::Layer::resolveKeyPath(keyPath, depth, value)) {
if (keyPath.propagate(name(), depth)) {
uint newDepth = keyPath.nextDepth(name(), depth);
uint32_t newDepth = keyPath.nextDepth(name(), depth);
mRoot->resolveKeyPath(keyPath, newDepth, value);
}
return true;
}
return false;
}

bool renderer::CompLayer::resolveKeyPath(LOTKeyPath &keyPath, uint depth,
bool renderer::CompLayer::resolveKeyPath(LOTKeyPath &keyPath, uint32_t depth,
LOTVariant &value)
{
if (renderer::Layer::resolveKeyPath(keyPath, depth, value)) {
if (keyPath.propagate(name(), depth)) {
uint newDepth = keyPath.nextDepth(name(), depth);
uint32_t newDepth = keyPath.nextDepth(name(), depth);
for (const auto &layer : mLayers) {
layer->resolveKeyPath(keyPath, newDepth, value);
}
Expand Down Expand Up @@ -560,7 +560,7 @@ void renderer::CompLayer::render(VPainter *painter, const VRle &inheritMask,
renderHelper(&srcPainter, inheritMask, matteRle, cache);
srcPainter.end();
painter->drawBitmap(VPoint(), srcBitmap,
uchar(combinedAlpha() * 255.0f));
uint8_t(combinedAlpha() * 255.0f));
cache.release_surface(srcBitmap);
} else {
renderHelper(painter, inheritMask, matteRle, cache);
Expand Down Expand Up @@ -916,7 +916,7 @@ renderer::DrawableList renderer::ShapeLayer::renderList()
return {mDrawableList.data(), mDrawableList.size()};
}

bool renderer::Group::resolveKeyPath(LOTKeyPath &keyPath, uint depth,
bool renderer::Group::resolveKeyPath(LOTKeyPath &keyPath, uint32_t depth,
LOTVariant &value)
{
if (!keyPath.skip(name())) {
Expand All @@ -933,15 +933,15 @@ bool renderer::Group::resolveKeyPath(LOTKeyPath &keyPath, uint depth,
}

if (keyPath.propagate(name(), depth)) {
uint newDepth = keyPath.nextDepth(name(), depth);
uint32_t newDepth = keyPath.nextDepth(name(), depth);
for (auto &child : mContents) {
child->resolveKeyPath(keyPath, newDepth, value);
}
}
return true;
}

bool renderer::Fill::resolveKeyPath(LOTKeyPath &keyPath, uint depth,
bool renderer::Fill::resolveKeyPath(LOTKeyPath &keyPath, uint32_t depth,
LOTVariant &value)
{
if (!keyPath.matches(mModel.name(), depth)) {
Expand All @@ -956,7 +956,7 @@ bool renderer::Fill::resolveKeyPath(LOTKeyPath &keyPath, uint depth,
return false;
}

bool renderer::Stroke::resolveKeyPath(LOTKeyPath &keyPath, uint depth,
bool renderer::Stroke::resolveKeyPath(LOTKeyPath &keyPath, uint32_t depth,
LOTVariant &value)
{
if (!keyPath.matches(mModel.name(), depth)) {
Expand Down
Loading

0 comments on commit 89f6ece

Please sign in to comment.