Skip to content

Commit

Permalink
support disk cache of SequenceComposition
Browse files Browse the repository at this point in the history
  • Loading branch information
liamcli committed Jun 25, 2023
1 parent 8ec1873 commit 826facd
Show file tree
Hide file tree
Showing 17 changed files with 94 additions and 70 deletions.
6 changes: 3 additions & 3 deletions android/libpag/src/main/java/org/libpag/PAGPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ public void setSurface(PAGSurface surface) {
* when first rendering BitmapComposition and VideoComposition,
* which will reduces memory consumption, and increases stability.
*/
public native boolean diskCacheEnabled();
public native boolean useDiskCache();

/**
* Set the value of diskCacheEnabled property.
* Set the value of useDiskCache property.
*/
public native void setDiskCacheEnabled(boolean value);
public native void setUseDiskCache(boolean value);

/**
* This value defines the scale factor for internal graphics caches, ranges from 0.0 to 1.0. The
Expand Down
10 changes: 5 additions & 5 deletions android/libpag/src/main/java/org/libpag/PAGView.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,15 @@ public void setCacheEnabled(boolean value) {
* when first rendering BitmapComposition and VideoComposition,
* which will reduces memory consumption, and increases stability.
*/
public boolean diskCacheEnabled() {
return pagPlayer.diskCacheEnabled();
public boolean useDiskCache() {
return pagPlayer.useDiskCache();
}

/**
* Set the value of diskCacheEnabled property.
* Set the value of useDiskCache property.
*/
public void setDiskCacheEnabled(boolean value) {
pagPlayer.setDiskCacheEnabled(value);
public void setUseDiskCache(boolean value) {
pagPlayer.setUseDiskCache(value);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions include/pag/pag.h
Original file line number Diff line number Diff line change
Expand Up @@ -1333,12 +1333,12 @@ class PAG_API PAGPlayer {
* when first rendering BitmapComposition and VideoComposition,
* which will reduces memory consumption, and increases stability.
*/
bool diskCacheEnabled();
bool useDiskCache();

/**
* Set the value of diskCacheEnabled property.
* Set the value of useDiskCache property.
*/
void setDiskCacheEnabled(bool value);
void setUseDiskCache(bool value);

/**
* This value defines the scale factor for internal graphics caches, ranges from 0.0 to 1.0. The
Expand Down
9 changes: 4 additions & 5 deletions src/platform/android/JPAGPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,20 +316,19 @@ PAG_API jboolean Java_org_libpag_PAGPlayer_hitTestPoint(JNIEnv* env, jobject thi
return (jboolean)player->hitTestPoint(pagLayer, x, y, pixelHitTest);
}

PAG_API void Java_org_libpag_PAGPlayer_setDiskCacheEnabled(JNIEnv* env, jobject thiz,
jboolean value) {
PAG_API void Java_org_libpag_PAGPlayer_setUseDiskCache(JNIEnv* env, jobject thiz, jboolean value) {
auto player = getPAGPlayer(env, thiz);
if (player == nullptr) {
return;
}
player->setDiskCacheEnabled(value);
player->setUseDiskCache(value);
}

PAG_API jboolean Java_org_libpag_PAGPlayer_diskCacheEnabled(JNIEnv* env, jobject thiz) {
PAG_API jboolean Java_org_libpag_PAGPlayer_useDiskCache(JNIEnv* env, jobject thiz) {
auto player = getPAGPlayer(env, thiz);
if (player == nullptr) {
return JNI_FALSE;
}
return player->diskCacheEnabled();
return player->useDiskCache();
}
}
6 changes: 3 additions & 3 deletions src/platform/cocoa/PAGPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ PAG_API @interface PAGPlayer : NSObject
* when first rendering BitmapComposition and VideoComposition,
* which will reduces memory consumption, and increases stability.
*/
- (BOOL)diskCacheEnabled;
- (BOOL)useDiskCache;

/**
* Set the value of diskCacheEnabled property.
* Set the value of useDiskCache property.
*/
- (void)setDiskCacheEnabled:(BOOL)value;
- (void)setUseDiskCache:(BOOL)value;

/**
* This value defines the scale factor for internal graphics caches, ranges from 0.0 to 1.0. The
Expand Down
8 changes: 4 additions & 4 deletions src/platform/cocoa/PAGPlayer.m
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ - (void)setCacheEnabled:(BOOL)value {
[pagPlayer setCacheEnabled:value];
}

- (BOOL)diskCacheEnabled {
return [pagPlayer diskCacheEnabled];
- (BOOL)useDiskCache {
return [pagPlayer useDiskCache];
}

- (void)setDiskCacheEnabled:(BOOL)value {
[pagPlayer setDiskCacheEnabled:value];
- (void)setUseDiskCache:(BOOL)value {
[pagPlayer setUseDiskCache:value];
}

- (float)cacheScale {
Expand Down
4 changes: 2 additions & 2 deletions src/platform/cocoa/private/PAGPlayerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@

- (void)setCacheEnabled:(BOOL)value;

- (BOOL)diskCacheEnabled;
- (BOOL)useDiskCache;

- (void)setDiskCacheEnabled:(BOOL)value;
- (void)setUseDiskCache:(BOOL)value;

- (float)cacheScale;

Expand Down
8 changes: 4 additions & 4 deletions src/platform/cocoa/private/PAGPlayerImpl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ - (void)setCacheEnabled:(BOOL)value {
pagPlayer->setCacheEnabled(value);
}

- (BOOL)diskCacheEnabled {
return pagPlayer->diskCacheEnabled();
- (BOOL)useDiskCache {
return pagPlayer->useDiskCache();
}

- (void)setDiskCacheEnabled:(BOOL)value {
pagPlayer->setDiskCacheEnabled(value);
- (void)setUseDiskCache:(BOOL)value {
pagPlayer->setUseDiskCache(value);
}

- (float)cacheScale {
Expand Down
6 changes: 3 additions & 3 deletions src/platform/ios/PAGView.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ PAG_API @interface PAGView : UIView
* when first rendering BitmapComposition and VideoComposition,
* which will reduces memory consumption, and increases stability.
*/
- (BOOL)diskCacheEnabled;
- (BOOL)useDiskCache;

/**
* Set the value of diskCacheEnabled property.
* Set the value of useDiskCache property.
*/
- (void)setDiskCacheEnabled:(BOOL)value;
- (void)setUseDiskCache:(BOOL)value;

/**
* This value defines the scale factor for internal graphics caches, ranges from 0.0 to 1.0. The
Expand Down
8 changes: 4 additions & 4 deletions src/platform/ios/PAGView.m
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,12 @@ - (void)setCacheEnabled:(BOOL)value {
[pagPlayer setCacheEnabled:value];
}

- (BOOL)diskCacheEnabled {
return [pagPlayer diskCacheEnabled];
- (BOOL)useDiskCache {
return [pagPlayer useDiskCache];
}

- (void)setDiskCacheEnabled:(BOOL)value {
[pagPlayer setDiskCacheEnabled:value];
- (void)setUseDiskCache:(BOOL)value {
[pagPlayer setUseDiskCache:value];
}

- (float)cacheScale {
Expand Down
8 changes: 4 additions & 4 deletions src/rendering/PAGPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ void PAGPlayer::setCacheEnabled(bool value) {
renderCache->setSnapshotEnabled(value);
}

bool PAGPlayer::diskCacheEnabled() {
bool PAGPlayer::useDiskCache() {
LockGuard autoLock(rootLocker);
return renderCache->diskCacheEnabled();
return renderCache->useDiskCache();
}

void PAGPlayer::setDiskCacheEnabled(bool value) {
void PAGPlayer::setUseDiskCache(bool value) {
LockGuard autoLock(rootLocker);
renderCache->setDiskCacheEnabled(value);
renderCache->setUseDiskCache(value);
}

float PAGPlayer::cacheScale() {
Expand Down
2 changes: 1 addition & 1 deletion src/rendering/caches/RenderCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void RenderCache::preparePreComposeLayer(PreComposeLayer* layer) {
}
usedAssets.insert(composition->uniqueID);
auto sequence = Sequence::Get(composition);
auto info = SequenceInfo::Make(sequence, _diskCacheEnabled);
auto info = SequenceInfo::Make(sequence, _useDiskCache);
if (composition->staticContent()) {
SequenceImageProxy proxy(info, 0);
prepareAssetImage(composition->uniqueID, &proxy);
Expand Down
12 changes: 6 additions & 6 deletions src/rendering/caches/RenderCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ class RenderCache : public Performance {
* when first rendering BitmapComposition and VideoComposition,
* which will reduces memory consumption, and increases stability.
*/
bool diskCacheEnabled() const {
return _diskCacheEnabled;
bool useDiskCache() const {
return _useDiskCache;
}

/**
* Set the value of diskCacheEnabled property.
* Set the value of useDiskCache property.
*/
void setDiskCacheEnabled(bool value) {
_diskCacheEnabled = value;
void setUseDiskCache(bool value) {
_useDiskCache = value;
}

/**
Expand Down Expand Up @@ -187,7 +187,7 @@ class RenderCache : public Performance {
size_t graphicsMemory = 0;
bool _videoEnabled = true;
bool _snapshotEnabled = true;
bool _diskCacheEnabled = false;
bool _useDiskCache = false;
std::unordered_set<ID> usedAssets = {};
std::unordered_map<ID, Snapshot*> snapshotCaches = {};
std::list<Snapshot*> snapshotLRU = {};
Expand Down
41 changes: 31 additions & 10 deletions src/rendering/sequences/DiskSequenceReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "DiskSequenceReader.h"
#include "base/utils/TGFXCast.h"
#include "platform/Platform.h"
#include "tgfx/utils/Buffer.h"

namespace pag {

Expand All @@ -43,6 +44,8 @@ int DiskSequenceReader::height() const {
}

std::shared_ptr<tgfx::ImageBuffer> DiskSequenceReader::onMakeBuffer(Frame targetFrame) {
// Need a locker here in case there are other threads are decoding at the same time.
std::lock_guard<std::mutex> autoLock(locker);
if (pagDecoder == nullptr) {
auto root = PAGComposition::Make(sequence->width, sequence->height);
auto composition = std::make_shared<PAGComposition>(
Expand All @@ -64,29 +67,47 @@ std::shared_ptr<tgfx::ImageBuffer> DiskSequenceReader::onMakeBuffer(Frame target
if (pagDecoder == nullptr) {
return nullptr;
}
if (bitmap == nullptr) {
bitmap = std::make_shared<tgfx::Bitmap>(pagDecoder->width(), pagDecoder->height(), false, true);
if (bitmap == nullptr && pixels == nullptr) {
if (tgfx::HardwareBufferAvailable()) {
auto hardwareBuffer =
tgfx::HardwareBufferAllocate(pagDecoder->width(), pagDecoder->height(), false);
if (hardwareBuffer) {
bitmap = std::make_shared<tgfx::Bitmap>(hardwareBuffer);
tgfx::HardwareBufferRelease(hardwareBuffer);
}
}
if (bitmap == nullptr) {
info = tgfx::ImageInfo::Make(pagDecoder->width(), pagDecoder->height(),
tgfx::ColorType::RGBA_8888);
tgfx::Buffer buffer(info.byteSize());
buffer.clear();
pixels = buffer.release();
}
}

if (!pagDecoder->checkFrameChanged(targetFrame)) {
return bitmap->makeBuffer();
return imageBuffer;
}
bool success = false;
if (bitmap->isHardwareBacked()) {
if (bitmap) {
success = pagDecoder->readFrame(targetFrame, bitmap->getHardwareBuffer());
} else {
auto pixels = bitmap->lockPixels();
if (pixels) {
success = pagDecoder->readFrame(targetFrame, pixels, bitmap->info().rowBytes(),
ToPAG(bitmap->info().colorType()),
ToPAG(bitmap->info().alphaType()));
bitmap->unlockPixels();
success =
pagDecoder->readFrame(targetFrame, const_cast<void*>(pixels->data()), info.rowBytes(),
ToPAG(info.colorType()), ToPAG(info.alphaType()));
}
}
if (!success) {
LOGE("DiskSequenceReader: Error on readFrame.\n");
return nullptr;
}
return bitmap->makeBuffer();
if (bitmap) {
imageBuffer = bitmap->makeBuffer();
} else {
imageBuffer = tgfx::ImageBuffer::MakeFrom(info, pixels);
}
return imageBuffer;
}

void DiskSequenceReader::onReportPerformance(Performance*, int64_t) {
Expand Down
4 changes: 4 additions & 0 deletions src/rendering/sequences/DiskSequenceReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,9 @@ class DiskSequenceReader : public SequenceReader {
std::shared_ptr<File> file;
std::shared_ptr<tgfx::ImageBuffer> onMakeBuffer(Frame targetFrame) override;
void onReportPerformance(Performance* performance, int64_t decodingTime) override;
std::shared_ptr<tgfx::ImageBuffer> imageBuffer = nullptr;
std::mutex locker = {};
tgfx::ImageInfo info = {};
std::shared_ptr<tgfx::Data> pixels = nullptr;
};
} // namespace pag
20 changes: 10 additions & 10 deletions src/rendering/sequences/SequenceInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,29 @@

namespace pag {
static std::shared_ptr<tgfx::Image> MakeSequenceImage(
std::shared_ptr<tgfx::ImageGenerator> generator, Sequence* sequence, bool diskCacheEnabled) {
std::shared_ptr<tgfx::ImageGenerator> generator, Sequence* sequence, bool useDiskCache) {
auto image = tgfx::Image::MakeFrom(std::move(generator));
if (!diskCacheEnabled && sequence->composition->type() == CompositionType::Video) {
if (!useDiskCache && sequence->composition->type() == CompositionType::Video) {
auto videoSequence = static_cast<VideoSequence*>(sequence);
image = image->makeRGBAAA(sequence->width, sequence->height, videoSequence->alphaStartX,
videoSequence->alphaStartY);
}
return image;
}

std::shared_ptr<SequenceInfo> SequenceInfo::Make(Sequence* sequence, bool diskCacheEnabled) {
std::shared_ptr<SequenceInfo> SequenceInfo::Make(Sequence* sequence, bool useDiskCache) {
if (sequence == nullptr) {
return nullptr;
}
auto factory = std::shared_ptr<SequenceInfo>(new SequenceInfo(sequence, diskCacheEnabled));
auto factory = std::shared_ptr<SequenceInfo>(new SequenceInfo(sequence, useDiskCache));
factory->weakThis = factory;
return factory;
}

SequenceInfo::SequenceInfo(Sequence* sequence, bool diskCacheEnabled)
: sequence(sequence), diskCacheEnabled(diskCacheEnabled) {
SequenceInfo::SequenceInfo(Sequence* sequence, bool useDiskCache)
: sequence(sequence), useDiskCache(useDiskCache) {
#ifdef PAG_BUILD_FOR_WEB
diskCacheEnabled = false;
useDiskCache = false;
#endif
}

Expand All @@ -61,7 +61,7 @@ std::shared_ptr<SequenceReader> SequenceInfo::makeReader(std::shared_ptr<File> f
}
std::shared_ptr<SequenceReader> reader = nullptr;
auto composition = sequence->composition;
if (diskCacheEnabled) {
if (useDiskCache) {
reader = DiskSequenceReader::Make(std::move(file), sequence);
if (reader) {
return reader;
Expand Down Expand Up @@ -96,7 +96,7 @@ std::shared_ptr<tgfx::Image> SequenceInfo::makeStaticImage(std::shared_ptr<File>
}
auto generator =
std::make_shared<StaticSequenceGenerator>(std::move(file), weakThis.lock(), width, height);
return MakeSequenceImage(std::move(generator), sequence, diskCacheEnabled);
return MakeSequenceImage(std::move(generator), sequence, useDiskCache);
}

std::shared_ptr<tgfx::Image> SequenceInfo::makeFrameImage(std::shared_ptr<SequenceReader> reader,
Expand All @@ -105,7 +105,7 @@ std::shared_ptr<tgfx::Image> SequenceInfo::makeFrameImage(std::shared_ptr<Sequen
return nullptr;
}
auto generator = std::make_shared<SequenceFrameGenerator>(std::move(reader), targetFrame);
return MakeSequenceImage(std::move(generator), sequence, diskCacheEnabled);
return MakeSequenceImage(std::move(generator), sequence, useDiskCache);
}

bool SequenceInfo::staticContent() const {
Expand Down
Loading

0 comments on commit 826facd

Please sign in to comment.