diff --git a/libpurgeablemem/cpp/src/purgeable_mem_base.cpp b/libpurgeablemem/cpp/src/purgeable_mem_base.cpp index c261bb3687e1e97ded2213a28e4c2f9a0a919aa3..20f39af3caf541c4e9df26b4ef22c4caf022cb76 100644 --- a/libpurgeablemem/cpp/src/purgeable_mem_base.cpp +++ b/libpurgeablemem/cpp/src/purgeable_mem_base.cpp @@ -159,19 +159,55 @@ void PurgeableMemBase::EndWrite() bool PurgeableMemBase::BeginReadWithDataLock() { + if (!isDataValid_) { + return false; + } + std::lock_guard lock(dataLock_); + if (!isDataValid_) { + return false; + } - if (isDataValid_) { - return BeginRead(); + bool succ = false; + bool ret = false; + int tryTimes = 0; + + PM_HILOG_DEBUG(LOG_CORE, "%{public}s %{public}s", __func__, ToString().c_str()); + IF_NULL_LOG_ACTION(dataPtr_, "dataPtr is nullptr in BeginRead", return false); + IF_NULL_LOG_ACTION(builder_, "builder_ is nullptr in BeginRead", return false); + Pin(); + PMState err = PM_OK; + while (true) { + if (!IfNeedRebuild_()) { + PM_HILOG_DEBUG(LOG_CORE, "%{public}s: not purged, return true. MAP_PUR=0x%{public}x", + __func__, MAP_PURGEABLE); + ret = true; + break; + } + + succ = BuildContent_(); + if (succ) { + AfterRebuildSucc(); + } + PM_HILOG_DEBUG(LOG_CORE, "%{public}s: purged, built %{public}s", __func__, succ ? "succ" : "fail"); + + tryTimes++; + if (!succ || tryTimes > MAX_BUILD_TRYTIMES) { + err = PMB_BUILD_ALL_FAIL; + break; + } } - return false; + if (!ret) { + PM_HILOG_ERROR(LOG_CORE, "%{public}s: err %{public}s, UxptePut. tryTime:%{public}d", + __func__, GetPMStateName(err), tryTimes); + Unpin(); + } + return ret; } void PurgeableMemBase::EndReadWithDataLock() { - std::lock_guard lock(dataLock_); - if (isDataValid_) { EndRead(); } diff --git a/purgeable_builder/include/purgeable_pixelmap_builder.h b/purgeable_builder/include/purgeable_pixelmap_builder.h index 7ea243f06d25772b79cecfa2da2448d2e43e5901..cd294ef348b53659f6dc7f0ec5e4ba144b668e12 100644 --- a/purgeable_builder/include/purgeable_pixelmap_builder.h +++ b/purgeable_builder/include/purgeable_pixelmap_builder.h @@ -32,7 +32,7 @@ using namespace OHOS::Media; class PurgeablePixelMapBuilder : public PurgeableMem::PurgeableMemBuilder { public: PurgeablePixelMapBuilder(uint32_t index, std::unique_ptr &imageSource, - DecodeOptions opts, PixelMap *pixelMap); + DecodeOptions opts); bool Build(void *data, size_t size) override; @@ -41,7 +41,6 @@ public: private: uint32_t index_; DecodeOptions opts_; - PixelMap *pixelMap_; std::unique_ptr imageSource_; }; // class PurgeablePixelMapBuilder diff --git a/purgeable_builder/src/purgeable_pixelmap_builder.cpp b/purgeable_builder/src/purgeable_pixelmap_builder.cpp index daed3de8d3d17460d917c52ab6659449c065ac38..db31c215bae68bd8aae59d7316346e0231111eaa 100644 --- a/purgeable_builder/src/purgeable_pixelmap_builder.cpp +++ b/purgeable_builder/src/purgeable_pixelmap_builder.cpp @@ -40,8 +40,8 @@ const std::string SYSTEM_PARAM_PIXELMAP_THRESHOLD_HEIGHT = "persist.memmgr.purge const std::string SYSTEM_PARAM_PIXELMAP_THRESHOLD_WIDGHT = "persist.memmgr.purgeable.pixelmap.threshold.widght"; PurgeablePixelMapBuilder::PurgeablePixelMapBuilder(uint32_t index, std::unique_ptr &imageSource, - DecodeOptions opts, PixelMap *pixelMap) - : index_(index), opts_(opts), pixelMap_(pixelMap), imageSource_(move(imageSource)) {} + DecodeOptions opts) + : index_(index), opts_(opts), imageSource_(move(imageSource)) {} bool PurgeablePixelMapBuilder::Build(void *data, size_t size) { @@ -53,14 +53,14 @@ bool PurgeablePixelMapBuilder::Build(void *data, size_t size) StartTrace(HITRACE_TAG_ZIMAGE, "OHOS::PurgeableBuilder::PixelMapPurgeableMemBuilder::Build"); std::unique_ptr pixelMap = imageSource_->CreatePixelMap(index_, opts_, errorCode); - if (pixelMap == nullptr || pixelMap_ == nullptr) { + if (pixelMap == nullptr || data == nullptr) { FinishTrace(HITRACE_TAG_ZIMAGE); return false; } StartTrace(HITRACE_TAG_ZIMAGE, ("OHOS::PurgeableBuilder::PixelMapPurgeableMemBuilder::CopyData " + std::to_string(size))); - if (memcpy_s((char *)pixelMap_->GetPixels(), size, (char *)pixelMap->GetPixels(), size)) { + if (memcpy_s((char *)data, size, (char *)pixelMap->GetPixels(), size)) { FinishTrace(HITRACE_TAG_ZIMAGE); return false; } @@ -208,7 +208,7 @@ bool MakePixelMapToBePurgeableBySrc(PixelMap *pixelMap, } std::unique_ptr purgeableMemBuilder = - std::make_unique(0, backupImgSrc4Rebuild, decodeOpts, pixelMap); + std::make_unique(0, backupImgSrc4Rebuild, decodeOpts); SetBuilderToBePurgeable(pixelMap, purgeableMemBuilder); if (pixelMap->IsPurgeable()) {