Fix storing VkPipelineCache in HWUI's shader cache breaking persistence
See b/268205519 for the full analysis and description, but the summary
is mSavePending could previously get stuck in the true state, preventing
the shader cache from being written to disk for the rest of an app's
lifecycle.
This stuck state seems to occur on every app launch after the first,
unless any shaders are compiled in the first 4 seconds. This occurs
frequently, as the shaders initially required by an app in its first 4
seconds were likely compiled on its first launch and already available
in its persistent cache, thus not necessitating new compilation in that
initial window and triggering the failure case.
This seems to have serious jank implications, particularly for SysUI.
Note: time is now represented in milliseconds to make testing easier.
Fixes: 268205519
Bug: 268205519
Bug: 225211273
Test: `atest hwui_unit_tests:ShaderCacheTest` (before and after fix)
Change-Id: I102d75df4c61f9b3012dd5d956dc37435d5b5a84
diff --git a/libs/hwui/pipeline/skia/ShaderCache.cpp b/libs/hwui/pipeline/skia/ShaderCache.cpp
index a55de95..9ff59ef 100644
--- a/libs/hwui/pipeline/skia/ShaderCache.cpp
+++ b/libs/hwui/pipeline/skia/ShaderCache.cpp
@@ -175,14 +175,13 @@
void ShaderCache::saveToDiskLocked() {
ATRACE_NAME("ShaderCache::saveToDiskLocked");
- if (mInitialized && mBlobCache && mSavePending) {
+ if (mInitialized && mBlobCache) {
if (mIDHash.size()) {
auto key = sIDKey;
set(mBlobCache.get(), &key, sizeof(key), mIDHash.data(), mIDHash.size());
}
mBlobCache->writeToFile();
}
- mSavePending = false;
}
void ShaderCache::store(const SkData& key, const SkData& data, const SkString& /*description*/) {
@@ -225,10 +224,10 @@
}
set(bc, key.data(), keySize, value, valueSize);
- if (!mSavePending && mDeferredSaveDelay > 0) {
+ if (!mSavePending && mDeferredSaveDelayMs > 0) {
mSavePending = true;
std::thread deferredSaveThread([this]() {
- sleep(mDeferredSaveDelay);
+ usleep(mDeferredSaveDelayMs * 1000); // milliseconds to microseconds
std::lock_guard<std::mutex> lock(mMutex);
// Store file on disk if there a new shader or Vulkan pipeline cache size changed.
if (mCacheDirty || mNewPipelineCacheSize != mOldPipelineCacheSize) {
@@ -237,6 +236,7 @@
mTryToStorePipelineCache = false;
mCacheDirty = false;
}
+ mSavePending = false;
});
deferredSaveThread.detach();
}