Skip to content

Commit

Permalink
Fix the crash of ImageTask when accessing the ImageBytes from a delet…
Browse files Browse the repository at this point in the history
…ed pag file.

* Fix crash when life cycle of ImageTask is longer than life cycle of file

* Fix crash when life cycle of ImageTask is longer than life cycle of file

Co-authored-by: licheng <[email protected]>
  • Loading branch information
h3r3x3 and licheng authored Mar 4, 2022
1 parent c661843 commit 6665b68
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
16 changes: 10 additions & 6 deletions src/rendering/caches/RenderCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ namespace pag {

class ImageTask : public Executor {
public:
static std::shared_ptr<Task> MakeAndRun(std::shared_ptr<tgfx::Image> image) {
static std::shared_ptr<Task> MakeAndRun(std::shared_ptr<tgfx::Image> image,
std::shared_ptr<File> file) {
if (image == nullptr) {
return nullptr;
}
auto bitmap = new ImageTask(std::move(image));
auto bitmap = new ImageTask(std::move(image), file);
auto task = Task::Make(std::unique_ptr<ImageTask>(bitmap));
task->run();
return task;
Expand All @@ -54,8 +55,11 @@ class ImageTask : public Executor {
private:
std::shared_ptr<tgfx::TextureBuffer> buffer = {};
std::shared_ptr<tgfx::Image> image = nullptr;
// Make a reference to file when image made from imageByte of file.
std::shared_ptr<File> file = nullptr;

explicit ImageTask(std::shared_ptr<tgfx::Image> image) : image(std::move(image)) {
explicit ImageTask(std::shared_ptr<tgfx::Image> image, std::shared_ptr<File> file)
: image(std::move(image)), file(std::move(file)) {
}

void execute() override {
Expand Down Expand Up @@ -361,7 +365,7 @@ void RenderCache::prepareImage(ID assetID, std::shared_ptr<tgfx::Image> image) {
if (imageTasks.count(assetID) != 0 || snapshotCaches.count(assetID) != 0) {
return;
}
auto task = ImageTask::MakeAndRun(std::move(image));
auto task = ImageTask::MakeAndRun(std::move(image), stage->getFileFromReferenceMap(assetID));
if (task) {
imageTasks[assetID] = task;
}
Expand Down Expand Up @@ -429,7 +433,7 @@ bool RenderCache::prepareSequenceReader(Sequence* sequence, Frame targetFrame,
// 静态的序列帧采用位图的缓存逻辑,如果上层缓存过 Snapshot 就不需要预测。
return false;
}
auto file = stage->getSequenceFile(sequence);
auto file = stage->getFileFromReferenceMap(composition->uniqueID);
auto reader = MakeSequenceReader(file, sequence, policy);
sequenceCaches[composition->uniqueID] = reader;
reader->prepareAsync(targetFrame);
Expand Down Expand Up @@ -460,7 +464,7 @@ std::shared_ptr<SequenceReader> RenderCache::getSequenceReader(Sequence* sequenc
}
}
if (reader == nullptr) {
auto file = stage->getSequenceFile(sequence);
auto file = stage->getFileFromReferenceMap(composition->uniqueID);
reader = MakeSequenceReader(file, sequence,
SoftwareToHardwareEnabled() ? DecodingPolicy::SoftwareToHardware
: DecodingPolicy::Hardware);
Expand Down
7 changes: 2 additions & 5 deletions src/rendering/layers/PAGStage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,8 @@ uint32_t PAGStage::getContentVersion() const {
return contentVersion;
}

std::shared_ptr<File> PAGStage::getSequenceFile(Sequence* sequence) {
if (sequence == nullptr) {
return nullptr;
}
auto result = layerReferenceMap.find(sequence->composition->uniqueID);
std::shared_ptr<File> PAGStage::getFileFromReferenceMap(ID uniqueID) {
auto result = layerReferenceMap.find(uniqueID);
if (result == layerReferenceMap.end()) {
return nullptr;
}
Expand Down
2 changes: 1 addition & 1 deletion src/rendering/layers/PAGStage.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class PAGStage : public PAGComposition {

static tgfx::Point GetLayerContentScaleFactor(PAGLayer* pagLayer, bool isPAGImage);
PAGStage(int width, int height);
std::shared_ptr<File> getSequenceFile(Sequence* sequence);
std::shared_ptr<File> getFileFromReferenceMap(ID uniqueID);
void addToReferenceMap(ID uniqueID, PAGLayer* pagLayer);
bool removeFromReferenceMap(ID uniqueID, PAGLayer* pagLayer);
float getMaxScaleFactor(ID referenceID);
Expand Down

0 comments on commit 6665b68

Please sign in to comment.