1 // Copyright 2020 The Dawn Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "dawn_native/PersistentCache.h" 16 17 #include "common/Assert.h" 18 #include "dawn_native/Device.h" 19 #include "dawn_platform/DawnPlatform.h" 20 21 namespace dawn_native { 22 PersistentCache(DeviceBase * device)23 PersistentCache::PersistentCache(DeviceBase* device) 24 : mDevice(device), mCache(GetPlatformCache()) { 25 } 26 LoadData(const PersistentCacheKey & key)27 ScopedCachedBlob PersistentCache::LoadData(const PersistentCacheKey& key) { 28 ScopedCachedBlob blob = {}; 29 if (mCache == nullptr) { 30 return blob; 31 } 32 std::lock_guard<std::mutex> lock(mMutex); 33 blob.bufferSize = mCache->LoadData(ToAPI(mDevice), key.data(), key.size(), nullptr, 0); 34 if (blob.bufferSize > 0) { 35 blob.buffer.reset(new uint8_t[blob.bufferSize]); 36 const size_t bufferSize = mCache->LoadData(ToAPI(mDevice), key.data(), key.size(), 37 blob.buffer.get(), blob.bufferSize); 38 ASSERT(bufferSize == blob.bufferSize); 39 return blob; 40 } 41 return blob; 42 } 43 StoreData(const PersistentCacheKey & key,const void * value,size_t size)44 void PersistentCache::StoreData(const PersistentCacheKey& key, const void* value, size_t size) { 45 if (mCache == nullptr) { 46 return; 47 } 48 ASSERT(value != nullptr); 49 ASSERT(size > 0); 50 std::lock_guard<std::mutex> lock(mMutex); 51 mCache->StoreData(ToAPI(mDevice), key.data(), key.size(), value, size); 52 } 53 GetPlatformCache()54 dawn_platform::CachingInterface* PersistentCache::GetPlatformCache() { 55 // TODO(dawn:549): Create a fingerprint of concatenated version strings (ex. Tint commit 56 // hash, Dawn commit hash). This will be used by the client so it may know when to discard 57 // previously cached Dawn objects should this fingerprint change. 58 dawn_platform::Platform* platform = mDevice->GetPlatform(); 59 if (platform != nullptr) { 60 return platform->GetCachingInterface(/*fingerprint*/ nullptr, /*fingerprintSize*/ 0); 61 } 62 return nullptr; 63 } 64 } // namespace dawn_native 65