1 /*
2  * Copyright (c) 2023 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 #ifndef OHOS_ROSEN_WINDOW_SCENE_SCENE_PERSISTENCE_H
17 #define OHOS_ROSEN_WINDOW_SCENE_SCENE_PERSISTENCE_H
18 
19 #include <string>
20 
21 #include <refbase.h>
22 
23 #include "common/include/task_scheduler.h"
24 #include "session/host/include/ws_ffrt_helper.h"
25 
26 namespace OHOS::Media {
27 class PixelMap;
28 } // namespace OHOS::Media
29 
30 namespace OHOS::Rosen {
31 class ScenePersistence : public RefBase {
32 public:
33     ScenePersistence(const std::string& bundleName, int32_t persistentId);
34     virtual ~ScenePersistence();
35 
36     static bool CreateSnapshotDir(const std::string& directory);
37     static bool CreateUpdatedIconDir(const std::string& directory);
38 
39     static bool IsAstcEnabled();
40     void SetHasSnapshot(bool hasSnapshot);
41     bool HasSnapshot() const;
42     bool IsSnapshotExisted() const;
43     std::string GetSnapshotFilePath();
44     std::pair<uint32_t, uint32_t> GetSnapshotSize() const;
45     std::shared_ptr<WSFFRTHelper> GetSnapshotFfrtHelper() const;
46 
47     void SaveSnapshot(const std::shared_ptr<Media::PixelMap>& pixelMap,
48         const std::function<void()> resetSnapshotCallback = [](){});
49     bool IsSavingSnapshot();
50     void RenameSnapshotFromOldPersistentId(const int32_t& oldPersistentId);
51 
52     void SaveUpdatedIcon(const std::shared_ptr<Media::PixelMap>& pixelMap);
53     std::string GetUpdatedIconPath() const;
54     std::shared_ptr<Media::PixelMap> GetLocalSnapshotPixelMap(const float oriScale, const float newScale) const;
55 
56 private:
57     static std::string snapshotDirectory_;
58     std::string bundleName_;
59     int32_t persistentId_;
60     std::string snapshotPath_;
61     std::pair<uint32_t, uint32_t> snapshotSize_;
62     bool hasSnapshot_ = false;
63 
64     static std::string updatedIconDirectory_;
65     std::string updatedIconPath_;
66 
67     std::atomic<int> savingSnapshotSum_ { 0 };
68     std::atomic<bool> isSavingSnapshot_ { false };
69 
70     static std::shared_ptr<WSFFRTHelper> snapshotFfrtHelper_;
71     mutable std::mutex savingSnapshotMutex_;
72 };
73 } // namespace OHOS::Rosen
74 
75 #endif // OHOS_ROSEN_WINDOW_SCENE_SCENE_PERSISTENCE_H
76