• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "core/image/flutter_image_cache.h"
17 
18 #include "include/core/SkGraphics.h"
19 #ifdef USE_ROSEN_DRAWING
20 #include "drawing/engine_adapter/skia_adapter/skia_data.h"
21 
22 #include "core/components_ng/render/drawing.h"
23 #endif
24 #include "core/components_ng/image_provider/image_object.h"
25 
26 namespace OHOS::Ace {
27 
Create()28 RefPtr<ImageCache> ImageCache::Create()
29 {
30     return MakeRefPtr<FlutterImageCache>();
31 }
32 
Clear()33 void FlutterImageCache::Clear()
34 {
35     std::scoped_lock clearLock(imageCacheMutex_, dataCacheMutex_);
36     cacheList_.clear();
37     imageCache_.clear();
38     dataCacheList_.clear();
39     imageDataCache_.clear();
40 }
41 
GetDataFromCacheFile(const std::string & filePath)42 RefPtr<CachedImageData> FlutterImageCache::GetDataFromCacheFile(const std::string& filePath)
43 {
44     std::lock_guard<std::mutex> lock(cacheFileInfoMutex_);
45     if (!GetFromCacheFileInner(filePath)) {
46         LOGD("file not cached, return nullptr");
47         return nullptr;
48     }
49     auto cacheFileLoader = AceType::MakeRefPtr<FileImageLoader>();
50 #ifndef USE_ROSEN_DRAWING
51     auto data = cacheFileLoader->LoadImageData(ImageSourceInfo(std::string("file:/").append(filePath)));
52     return data ? AceType::MakeRefPtr<SkiaCachedImageData>(data) : nullptr;
53 #else
54     auto rsData = cacheFileLoader->LoadImageData(ImageSourceInfo(std::string("file:/").append(filePath)));
55     return rsData ? AceType::MakeRefPtr<RosenCachedImageData>(rsData) : nullptr;
56 #endif
57 }
58 
Purge()59 void ImageCache::Purge()
60 {
61     SkGraphics::PurgeResourceCache();
62 }
63 
64 } // namespace OHOS::Ace