1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 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 16 #include "render/rs_image_cache.h" 17 18 namespace OHOS { 19 namespace Rosen { 20 // modify the RSImageCache instance as global to extend life cycle, fix destructor crash 21 static RSImageCache gRSImageCacheInstance; 22 Instance()23RSImageCache& RSImageCache::Instance() 24 { 25 return gRSImageCacheInstance; 26 } 27 CacheSkiaImage(uint64_t uniqueId,sk_sp<SkImage> img)28void RSImageCache::CacheSkiaImage(uint64_t uniqueId, sk_sp<SkImage> img) 29 { 30 if (img) { 31 std::lock_guard<std::mutex> lock(mutex_); 32 skiaImageCache_.emplace(uniqueId, img); 33 } 34 } 35 GetSkiaImageCache(uint64_t uniqueId) const36sk_sp<SkImage> RSImageCache::GetSkiaImageCache(uint64_t uniqueId) const 37 { 38 std::lock_guard<std::mutex> lock(mutex_); 39 auto it = skiaImageCache_.find(uniqueId); 40 if (it != skiaImageCache_.end()) { 41 return it->second; 42 } 43 return nullptr; 44 } 45 ReleaseSkiaImageCache(uint64_t uniqueId)46void RSImageCache::ReleaseSkiaImageCache(uint64_t uniqueId) 47 { 48 // release the skimage if nobody else holds it 49 std::lock_guard<std::mutex> lock(mutex_); 50 auto it = skiaImageCache_.find(uniqueId); 51 if (it != skiaImageCache_.end() && (!it->second || it->second->unique())) { 52 skiaImageCache_.erase(it); 53 } 54 } 55 } // namespace Rosen 56 } // namespace OHOS 57