Skip to content

Commit

Permalink
Add support for vector class that's not using exponential growth to l…
Browse files Browse the repository at this point in the history
…imit lottie memory usage.
  • Loading branch information
X-Ryl669 committed Mar 16, 2022
1 parent c7915c6 commit d7a822b
Show file tree
Hide file tree
Showing 28 changed files with 583 additions and 113 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ option(LOTTIE_TEST "Build LOTTIE AUTOTESTS" OFF)
option(LOTTIE_CCACHE "Enable LOTTIE ccache SUPPORT" OFF)
option(LOTTIE_ASAN "Compile with asan" OFF)
option(LOTTIE_JSON "Use readonly JSON parser" OFF)
option(LOTTIE_MEMSHRINK "Minimize memory but increase parsing time" OFF)
option(LOTTIE_EXAMPLE "Build examples" ON)

set(LOTTIE_MODULE_PATH "${CMAKE_SHARED_LIBRARY_PREFIX}rlottie-image-loader${CMAKE_SHARED_LIBRARY_SUFFIX}"
Expand Down
6 changes: 6 additions & 0 deletions cmake/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@
#ifdef LOTTIE_JSON
#define LOTTIE_JSON_SUPPORT
#endif

#cmakedefine LOTTIE_MEMSHRINK

#ifdef LOTTIE_MEMSHRINK
#define LOTTIE_MEMSHRINK_SUPPORT
#endif
1 change: 1 addition & 0 deletions example/lottieview.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include<future>
#include <cmath>
#include <algorithm>
#include <vector>

class RenderStrategy {
public:
Expand Down
6 changes: 3 additions & 3 deletions inc/rlottie.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#define _RLOTTIE_H_

#include <future>
#include <vector>
#include <memory>
#include "../src/vector/vvector.h"

#if defined _WIN32 || defined __CYGWIN__
#ifdef RLOTTIE_BUILD
Expand Down Expand Up @@ -255,15 +255,15 @@ class RLOTTIE_API Surface {
}mDrawArea;
};

using MarkerList = std::vector<std::tuple<std::string, int , int>>;
using MarkerList = VVector<std::tuple<std::string, int , int>>;
/**
* @brief https://helpx.adobe.com/after-effects/using/layer-markers-composition-markers.html
* Markers exported form AE are used to describe a segmnet of an animation {comment/tag , startFrame, endFrame}
* Marker can be use to devide a resource in to separate animations by tagging the segment with comment string ,
* start frame and duration of that segment.
*/

using LayerInfoList = std::vector<std::tuple<std::string, int , int>>;
using LayerInfoList = VVector<std::tuple<std::string, int , int>>;


using ColorFilter = std::function<void(float &r , float &g, float &b)>;
Expand Down
4 changes: 4 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ if get_option('json') == true
config_h.set10('LOTTIE_JSON_SUPPORT', true)
endif

if get_option('memshrink') == true
config_h.set10('LOTTIE_MEMSHRINK_SUPPORT', true)
endif


configure_file(
output: 'config.h',
Expand Down
5 changes: 4 additions & 1 deletion meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,7 @@ option('json',
value: false,
description: 'Use readonly JSON parser instead of RapidJSON')


option('memshrink',
type: 'boolean',
value: false,
description: 'Minimize memory usage but increase parsing time')
2 changes: 1 addition & 1 deletion src/lottie/JSON
Submodule JSON updated 2 files
+2 −3 JSON.hpp
+0 −486 ROString.hpp
9 changes: 5 additions & 4 deletions src/lottie/lottieanimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "rlottie.h"

#include <fstream>
#include "vvector.h"

using namespace rlottie;
using namespace rlottie::internal;
Expand Down Expand Up @@ -185,10 +186,10 @@ void AnimationImpl::init(std::shared_ptr<model::Composition> composition)
* just waits for new task on its own queue.
*/
class RenderTaskScheduler {
const unsigned _count{std::thread::hardware_concurrency()};
std::vector<std::thread> _threads;
std::vector<TaskQueue<SharedRenderTask>> _q{_count};
std::atomic<unsigned> _index{0};
const unsigned _count{std::thread::hardware_concurrency()};
VVector<std::thread> _threads;
VVector<TaskQueue<SharedRenderTask>> _q{_count};
std::atomic<unsigned> _index{0};

void run(unsigned i)
{
Expand Down
4 changes: 2 additions & 2 deletions src/lottie/lottiefiltermodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ class FilterData {
return *result;
}
std::bitset<32> mBitset{0};
std::vector<LOTVariant> mFilters;
VVector<LOTVariant> mFilters;
};

template <typename T>
Expand Down Expand Up @@ -366,7 +366,7 @@ class Filter : public FilterBase<T> {
CapStyle capStyle() const { return this->model()->capStyle(); }
JoinStyle joinStyle() const { return this->model()->joinStyle(); }
bool hasDashInfo() const { return this->model()->hasDashInfo(); }
void getDashInfo(int frameNo, std::vector<float>& result) const
void getDashInfo(int frameNo, VVector<float>& result) const
{
return this->model()->getDashInfo(frameNo, result);
}
Expand Down
26 changes: 13 additions & 13 deletions src/lottie/lottieitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ renderer::ShapeLayer::ShapeLayer(model::Layer *layerData,
{
mRoot->addChildren(layerData, allocator);

std::vector<renderer::Shape *> list;
VVector<renderer::Shape *> list;
mRoot->processPaintItems(list);

if (layerData->hasPathOperator()) {
Expand Down Expand Up @@ -1044,14 +1044,14 @@ void renderer::Group::applyTrim()
}
}

void renderer::Group::renderList(std::vector<VDrawable *> &list)
void renderer::Group::renderList(VVector<VDrawable *> &list)
{
for (const auto &content : mContents) {
content->renderList(list);
}
}

void renderer::Group::processPaintItems(std::vector<renderer::Shape *> &list)
void renderer::Group::processPaintItems(VVector<renderer::Shape *> &list)
{
size_t curOpCount = list.size();
for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
Expand All @@ -1078,7 +1078,7 @@ void renderer::Group::processPaintItems(std::vector<renderer::Shape *> &list)
}
}

void renderer::Group::processTrimItems(std::vector<renderer::Shape *> &list)
void renderer::Group::processTrimItems(VVector<renderer::Shape *> &list)
{
size_t curOpCount = list.size();
for (auto i = mContents.rbegin(); i != mContents.rend(); ++i) {
Expand Down Expand Up @@ -1260,7 +1260,7 @@ void renderer::Paint::updateRenderNode()
}
}

void renderer::Paint::renderList(std::vector<VDrawable *> &list)
void renderer::Paint::renderList(VVector<VDrawable *> &list)
{
if (mRenderNodeUpdate) {
updateRenderNode();
Expand All @@ -1278,11 +1278,11 @@ void renderer::Paint::renderList(std::vector<VDrawable *> &list)
if (mContentToRender) list.push_back(&mDrawable);
}

void renderer::Paint::addPathItems(std::vector<renderer::Shape *> &list,
void renderer::Paint::addPathItems(VVector<renderer::Shape *> &list,
size_t startOffset)
{
std::copy(list.begin() + startOffset, list.end(),
back_inserter(mPathItems));
mPathItems.reserve(mPathItems.size() + list.end() - list.begin() - startOffset);
std::copy(list.begin() + startOffset, list.end(), std::back_inserter(mPathItems));
}

renderer::Fill::Fill(model::Fill *data)
Expand Down Expand Up @@ -1334,7 +1334,7 @@ renderer::Stroke::Stroke(model::Stroke *data)
}
}

static vthread_local std::vector<float> Dash_Vector;
static vthread_local VVector<float> Dash_Vector;

bool renderer::Stroke::updateContent(int frameNo, const VMatrix &matrix,
float alpha)
Expand Down Expand Up @@ -1479,11 +1479,11 @@ void renderer::Trim::update()
}
}

void renderer::Trim::addPathItems(std::vector<renderer::Shape *> &list,
void renderer::Trim::addPathItems(VVector<renderer::Shape *> &list,
size_t startOffset)
{
std::copy(list.begin() + startOffset, list.end(),
back_inserter(mPathItems));
mPathItems.reserve(mPathItems.size() + list.end() - list.begin() - startOffset);
std::copy(list.begin() + startOffset, list.end(), std::back_inserter(mPathItems));
}

renderer::Repeater::Repeater(model::Repeater *data, VArenaAlloc *allocator)
Expand Down Expand Up @@ -1537,7 +1537,7 @@ void renderer::Repeater::update(int frameNo, const VMatrix &parentMatrix,
}
}

void renderer::Repeater::renderList(std::vector<VDrawable *> &list)
void renderer::Repeater::renderList(VVector<VDrawable *> &list)
{
if (mHidden) return;
return renderer::Group::renderList(list);
Expand Down
42 changes: 21 additions & 21 deletions src/lottie/lottieitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class SurfaceCache {
void release_surface(VBitmap &surface) { mCache.push_back(surface); }

private:
std::vector<VBitmap> mCache;
VVector<VBitmap> mCache;
};

class Drawable final : public VDrawable {
Expand All @@ -128,9 +128,9 @@ class Drawable final : public VDrawable {
struct CApiData {
CApiData();
LOTLayerNode mLayer;
std::vector<LOTMask> mMasks;
std::vector<LOTLayerNode *> mLayers;
std::vector<LOTNode *> mCNodeList;
VVector<LOTMask> mMasks;
VVector<LOTLayerNode *> mLayers;
VVector<LOTNode *> mCNodeList;
};

class Clipper {
Expand Down Expand Up @@ -179,7 +179,7 @@ class LayerMask {
void preprocess(const VRect &clip);

public:
std::vector<Mask> mMasks;
VVector<Mask> mMasks;
VRle mRle;
bool mStatic{true};
bool mDirty{true};
Expand Down Expand Up @@ -238,9 +238,9 @@ class Layer {
bool visible() const;
virtual void buildLayerNode();
LOTLayerNode & clayer() { return mCApiData->mLayer; }
std::vector<LOTLayerNode *> &clayers() { return mCApiData->mLayers; }
std::vector<LOTMask> & cmasks() { return mCApiData->mMasks; }
std::vector<LOTNode *> & cnodes() { return mCApiData->mCNodeList; }
VVector<LOTLayerNode *> &clayers() { return mCApiData->mLayers; }
VVector<LOTMask> & cmasks() { return mCApiData->mMasks; }
VVector<LOTNode *> & cnodes() { return mCApiData->mCNodeList; }
const char * name() const { return mLayerData->name(); }
virtual bool resolveKeyPath(LOTKeyPath &keyPath, uint32_t depth,
LOTVariant &value);
Expand Down Expand Up @@ -293,7 +293,7 @@ class CompLayer final : public Layer {
SurfaceCache &cache);

private:
std::vector<Layer *> mLayers;
VVector<Layer *> mLayers;
std::unique_ptr<Clipper> mClipper;
};

Expand Down Expand Up @@ -326,7 +326,7 @@ class ShapeLayer final : public Layer {
protected:
void preprocessStage(const VRect &clip) final;
void updateContent() final;
std::vector<VDrawable *> mDrawableList;
VVector<VDrawable *> mDrawableList;
Group * mRoot{nullptr};
};

Expand Down Expand Up @@ -363,7 +363,7 @@ class Object {
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 void renderList(VVector<VDrawable *> &) {}
virtual bool resolveKeyPath(LOTKeyPath &, uint32_t, LOTVariant &)
{
return false;
Expand All @@ -380,9 +380,9 @@ class Group : public Object {
void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha,
const DirtyFlag &flag) override;
void applyTrim();
void processTrimItems(std::vector<Shape *> &list);
void processPaintItems(std::vector<Shape *> &list);
void renderList(std::vector<VDrawable *> &list) override;
void processTrimItems(VVector<Shape *> &list);
void processPaintItems(VVector<Shape *> &list);
void renderList(VVector<VDrawable *> &list) override;
Object::Type type() const final { return Object::Type::Group; }
const VMatrix &matrix() const { return mMatrix; }
const char * name() const
Expand All @@ -394,7 +394,7 @@ class Group : public Object {
LOTVariant &value) override;

protected:
std::vector<Object *> mContents;
VVector<Object *> mContents;
VMatrix mMatrix;

private:
Expand Down Expand Up @@ -506,10 +506,10 @@ class Polystar final : public Shape {
class Paint : public Object {
public:
Paint(bool staticContent);
void addPathItems(std::vector<Shape *> &list, size_t startOffset);
void addPathItems(VVector<Shape *> &list, size_t startOffset);
void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha,
const DirtyFlag &flag) override;
void renderList(std::vector<VDrawable *> &list) final;
void renderList(VVector<VDrawable *> &list) final;
Object::Type type() const final { return Object::Type::Paint; }

protected:
Expand All @@ -520,7 +520,7 @@ class Paint : public Object {
void updateRenderNode();

protected:
std::vector<Shape *> mPathItems;
VVector<Shape *> mPathItems;
Drawable mDrawable;
VPath mPath;
DirtyFlag mFlag;
Expand Down Expand Up @@ -586,7 +586,7 @@ class Trim final : public Object {
const DirtyFlag &flag) final;
Object::Type type() const final { return Object::Type::Trim; }
void update();
void addPathItems(std::vector<Shape *> &list, size_t startOffset);
void addPathItems(VVector<Shape *> &list, size_t startOffset);

private:
bool pathDirty() const
Expand All @@ -601,7 +601,7 @@ class Trim final : public Object {
model::Trim::Segment mSegment{};
};
Cache mCache;
std::vector<Shape *> mPathItems;
VVector<Shape *> mPathItems;
model::Trim * mData{nullptr};
VPathMesure mPathMesure;
bool mDirty{true};
Expand All @@ -612,7 +612,7 @@ class Repeater final : public Group {
explicit Repeater(model::Repeater *data, VArenaAlloc *allocator);
void update(int frameNo, const VMatrix &parentMatrix, float parentAlpha,
const DirtyFlag &flag) final;
void renderList(std::vector<VDrawable *> &list) final;
void renderList(VVector<VDrawable *> &list) final;

private:
model::Repeater *mRepeaterData{nullptr};
Expand Down
4 changes: 2 additions & 2 deletions src/lottie/lottieitem_capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ void renderer::Drawable::sync()

if (mFlag & DirtyState::Path) {
applyDashOp();
const std::vector<VPath::Element> &elm = mPath.elements();
const std::vector<VPointF> & pts = mPath.points();
const VVector<VPath::Element> &elm = mPath.elements();
const VVector<VPointF> & pts = mPath.points();
const float *ptPtr = reinterpret_cast<const float *>(pts.data());
const char * elmPtr = reinterpret_cast<const char *>(elm.data());
mCNode->mPath.elmPtr = elmPtr;
Expand Down
4 changes: 2 additions & 2 deletions src/lottie/lottiekeypath.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
#define LOTTIEKEYPATH_H

#include <string>
#include <vector>
#include "vglobal.h"
#include "vvector.h"

class LOTKeyPath {
public:
Expand All @@ -47,7 +47,7 @@ class LOTKeyPath {
size_t size() const { return mKeys.size() - 1; }

private:
std::vector<std::string> mKeys;
VVector<std::string> mKeys;
};

#endif // LOTTIEKEYPATH_H
Loading

0 comments on commit d7a822b

Please sign in to comment.