Add logic to clean up resources more frequently
Added call to Skia's performDeferredCleanup method
to free resources that were not referenced within
the last 100 frames or 10 seconds whichever is
furthest away
Bug: 188450217
Test: manual
Change-Id: I3f37e1b5bd01330dbbc2da4a84b1259d56be2768
diff --git a/libs/hwui/renderthread/CacheManager.cpp b/libs/hwui/renderthread/CacheManager.cpp
index 46e8060..ded2b06 100644
--- a/libs/hwui/renderthread/CacheManager.cpp
+++ b/libs/hwui/renderthread/CacheManager.cpp
@@ -210,6 +210,14 @@
}
}
+void CacheManager::performDeferredCleanup(nsecs_t cleanupOlderThanMillis) {
+ if (mGrContext) {
+ mGrContext->performDeferredCleanup(
+ std::chrono::milliseconds(cleanupOlderThanMillis),
+ /* scratchResourcesOnly */true);
+ }
+}
+
} /* namespace renderthread */
} /* namespace uirenderer */
} /* namespace android */
diff --git a/libs/hwui/renderthread/CacheManager.h b/libs/hwui/renderthread/CacheManager.h
index 713ea99..af82672 100644
--- a/libs/hwui/renderthread/CacheManager.h
+++ b/libs/hwui/renderthread/CacheManager.h
@@ -23,6 +23,7 @@
#include <SkSurface.h>
#include <utils/String8.h>
#include <vector>
+#include "utils/TimeUtils.h"
namespace android {
@@ -53,6 +54,8 @@
size_t getBackgroundCacheSize() const { return mBackgroundResourceBytes; }
void onFrameCompleted();
+ void performDeferredCleanup(nsecs_t cleanupOlderThanMillis);
+
private:
friend class RenderThread;
diff --git a/libs/hwui/renderthread/CanvasContext.cpp b/libs/hwui/renderthread/CanvasContext.cpp
index a0d93e9..8bfc2c1 100644
--- a/libs/hwui/renderthread/CanvasContext.cpp
+++ b/libs/hwui/renderthread/CanvasContext.cpp
@@ -461,6 +461,7 @@
}
void CanvasContext::stopDrawing() {
+ cleanupResources();
mRenderThread.removeFrameCallback(this);
mAnimationContext->pauseAnimators();
mGenerationID++;
@@ -619,10 +620,25 @@
}
}
+ cleanupResources();
mRenderThread.cacheManager().onFrameCompleted();
return mCurrentFrameInfo->get(FrameInfoIndex::DequeueBufferDuration);
}
+void CanvasContext::cleanupResources() {
+ auto& tracker = mJankTracker.frames();
+ auto size = tracker.size();
+ auto capacity = tracker.capacity();
+ if (size == capacity) {
+ nsecs_t nowNanos = systemTime(SYSTEM_TIME_MONOTONIC);
+ nsecs_t frameCompleteNanos =
+ tracker[0].get(FrameInfoIndex::FrameCompleted);
+ nsecs_t frameDiffNanos = nowNanos - frameCompleteNanos;
+ nsecs_t cleanupMillis = ns2ms(std::max(frameDiffNanos, 10_s));
+ mRenderThread.cacheManager().performDeferredCleanup(cleanupMillis);
+ }
+}
+
void CanvasContext::reportMetricsWithPresentTime() {
if (mFrameMetricsReporter == nullptr) {
return;
diff --git a/libs/hwui/renderthread/CanvasContext.h b/libs/hwui/renderthread/CanvasContext.h
index 6f90e81..4bdc251 100644
--- a/libs/hwui/renderthread/CanvasContext.h
+++ b/libs/hwui/renderthread/CanvasContext.h
@@ -312,6 +312,7 @@
bool mExpectSurfaceStats = false;
std::function<void(int64_t, int64_t, int64_t)> mASurfaceTransactionCallback;
+ void cleanupResources();
};
} /* namespace renderthread */