• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "session/host/include/ws_snapshot_helper.h"
26 
27 namespace OHOS::Media {
28 class PixelMap;
29 } // namespace OHOS::Media
30 
31 namespace OHOS::Rosen {
32 class ScenePersistence : public RefBase {
33 public:
34     ScenePersistence(const std::string& bundleName, int32_t persistentId, SnapshotStatus capacity = defaultCapacity);
35     virtual ~ScenePersistence();
36 
37     static bool CreateSnapshotDir(const std::string& directory);
38     static bool CreateUpdatedIconDir(const std::string& directory);
39 
40     void SetSnapshotCapacity(SnapshotStatus capacity);
41     static void InitAstcEnabled();
42     static bool IsAstcEnabled();
43     void SetHasSnapshot(bool hasSnapshot, SnapshotStatus key = defaultStatus);
44     void SetHasSnapshotFreeMultiWindow(bool hasSnapshot);
45     bool HasSnapshot() const;
46     bool HasSnapshot(SnapshotStatus key, bool freeMultiWindow = false) const;
47     void ClearSnapshot(SnapshotStatus key);
48     bool IsSnapshotExisted(SnapshotStatus key = defaultStatus);
49     std::string GetSnapshotFilePath(SnapshotStatus& key, bool useKey = false, bool freeMultiWindow = false);
50     bool FindClosestFormSnapshot(SnapshotStatus& key);
51     std::pair<uint32_t, uint32_t> GetSnapshotSize(SnapshotStatus key = defaultStatus,
52         bool freeMultiWindow = false) const;
53     void SetSnapshotSize(SnapshotStatus key, bool freeMultiWindow, std::pair<uint32_t, uint32_t> size);
54     std::shared_ptr<WSFFRTHelper> GetSnapshotFfrtHelper() const;
55 
56     void SaveSnapshot(const std::shared_ptr<Media::PixelMap>& pixelMap,
57         const std::function<void()> resetSnapshotCallback = []() {}, SnapshotStatus key = defaultStatus,
58         DisplayOrientation rotate = DisplayOrientation::PORTRAIT, bool freeMultiWindow = false);
59     bool IsSavingSnapshot(SnapshotStatus key = defaultStatus, bool freeMultiWindow = false);
60     void SetIsSavingSnapshot(SnapshotStatus key, bool freeMultiWindow, bool isSavingSnapshot);
61     void ResetSnapshotCache();
62     void RenameSnapshotFromOldPersistentId(const int32_t& oldPersistentId);
63     void RenameSnapshotFromOldPersistentId(const int32_t& oldPersistentId, SnapshotStatus key);
64 
65     void SaveUpdatedIcon(const std::shared_ptr<Media::PixelMap>& pixelMap);
66     std::string GetUpdatedIconPath() const;
67     std::shared_ptr<Media::PixelMap> GetLocalSnapshotPixelMap(const float oriScale, const float newScale,
68         SnapshotStatus key = defaultStatus, bool freeMultiWindow = false);
69     DisplayOrientation rotate_[SCREEN_COUNT][ORIENTATION_COUNT] = {};
70 
71 private:
72     static std::string snapshotDirectory_;
73     std::string bundleName_;
74     int32_t persistentId_;
75     SnapshotStatus capacity_;
76     std::string snapshotPath_[SCREEN_COUNT][ORIENTATION_COUNT];
77     std::string snapshotFreeMultiWindowPath_;
78     std::pair<uint32_t, uint32_t> snapshotSize_[SCREEN_COUNT][ORIENTATION_COUNT];
79     std::pair<uint32_t, uint32_t> snapshotFreeMultiWindowSize_;
80     bool hasSnapshot_[SCREEN_COUNT][ORIENTATION_COUNT] = {};
81     bool hasSnapshotFreeMultiWindow_ = false;
82 
83     static std::string updatedIconDirectory_;
84     std::string updatedIconPath_;
85     static bool isAstcEnabled_;
86 
87     std::atomic<int> savingSnapshotSum_ { 0 };
88     std::atomic<bool> isSavingSnapshot_[SCREEN_COUNT][ORIENTATION_COUNT] = {};
89     std::atomic<bool> isSavingSnapshotFreeMultiWindow_ { false };
90 
91     static std::shared_ptr<WSFFRTHelper> snapshotFfrtHelper_;
92     mutable std::mutex savingSnapshotMutex_;
93     mutable std::mutex hasSnapshotMutex_;
94     mutable std::mutex snapshotSizeMutex_;
95 };
96 } // namespace OHOS::Rosen
97 
98 #endif // OHOS_ROSEN_WINDOW_SCENE_SCENE_PERSISTENCE_H
99