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 546c72b
Show file tree
Hide file tree
Showing 26 changed files with 324 additions and 217 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
45 changes: 42 additions & 3 deletions src/lottie/lottieanimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,20 +213,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 +258,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 +276,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 +497,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
28 changes: 14 additions & 14 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 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
20 changes: 10 additions & 10 deletions src/lottie/lottieitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace renderer {

using DrawableList = VSpan<VDrawable *>;

enum class DirtyFlagBit : uchar {
enum class DirtyFlagBit : uint8_t {
None = 0x00,
Matrix = 0x01,
Alpha = 0x02,
Expand Down Expand Up @@ -242,8 +242,8 @@ class Layer {
std::vector<LOTMask> & cmasks() { return mCApiData->mMasks; }
std::vector<LOTNode *> & cnodes() { return mCApiData->mCNodeList; }
const char * name() const { return mLayerData->name(); }
virtual bool resolveKeyPath(LOTKeyPath &keyPath, uint depth,
LOTVariant &value);
virtual bool resolveKeyPath(LOTKeyPath &keyPath, uint32_t depth,
LOTVariant &value);

protected:
virtual void preprocessStage(const VRect &clip) = 0;
Expand Down Expand Up @@ -278,7 +278,7 @@ class CompLayer final : public Layer {
void render(VPainter *painter, const VRle &mask, const VRle &matteRle,
SurfaceCache &cache) final;
void buildLayerNode() final;
bool resolveKeyPath(LOTKeyPath &keyPath, uint depth,
bool resolveKeyPath(LOTKeyPath &keyPath, uint32_t depth,
LOTVariant &value) override;

protected:
Expand Down Expand Up @@ -320,7 +320,7 @@ class ShapeLayer final : public Layer {
explicit ShapeLayer(model::Layer *layerData, VArenaAlloc *allocator);
DrawableList renderList() final;
void buildLayerNode() final;
bool resolveKeyPath(LOTKeyPath &keyPath, uint depth,
bool resolveKeyPath(LOTKeyPath &keyPath, uint32_t depth,
LOTVariant &value) override;

protected:
Expand Down Expand Up @@ -358,13 +358,13 @@ class ImageLayer final : public Layer {

class Object {
public:
enum class Type : uchar { Unknown, Group, Shape, Paint, Trim };
enum class Type : uint8_t { Unknown, Group, Shape, Paint, Trim };
virtual ~Object() = default;
Object & operator=(Object &&) noexcept = delete;
virtual void update(int frameNo, const VMatrix &parentMatrix,
float parentAlpha, const DirtyFlag &flag) = 0;
virtual void renderList(std::vector<VDrawable *> &) {}
virtual bool resolveKeyPath(LOTKeyPath &, uint, LOTVariant &)
virtual bool resolveKeyPath(LOTKeyPath &, uint32_t, LOTVariant &)
{
return false;
}
Expand All @@ -390,7 +390,7 @@ class Group : public Object {
static const char *TAG = "__";
return mModel.hasModel() ? mModel.name() : TAG;
}
bool resolveKeyPath(LOTKeyPath &keyPath, uint depth,
bool resolveKeyPath(LOTKeyPath &keyPath, uint32_t depth,
LOTVariant &value) override;

protected:
Expand Down Expand Up @@ -535,7 +535,7 @@ class Fill final : public Paint {

protected:
bool updateContent(int frameNo, const VMatrix &matrix, float alpha) final;
bool resolveKeyPath(LOTKeyPath &keyPath, uint depth,
bool resolveKeyPath(LOTKeyPath &keyPath, uint32_t depth,
LOTVariant &value) final;

private:
Expand All @@ -560,7 +560,7 @@ class Stroke : public Paint {

protected:
bool updateContent(int frameNo, const VMatrix &matrix, float alpha) final;
bool resolveKeyPath(LOTKeyPath &keyPath, uint depth,
bool resolveKeyPath(LOTKeyPath &keyPath, uint32_t depth,
LOTVariant &value) final;

private:
Expand Down
Loading

0 comments on commit 546c72b

Please sign in to comment.