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 #include "session/host/include/scene_persistence.h"
17
18 #include "image_packer.h"
19 #include "window_manager_hilog.h"
20
21 namespace OHOS::Rosen {
22 namespace {
23 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "ScenePersistence" };
24 constexpr const char *UNDERLINE_SEPARATOR = "_";
25 constexpr uint8_t IMAGE_QUALITY = 85;
26 }
27
28 std::string ScenePersistence::strPersistPath_;
29
ScenePersistence(const SessionInfo & info,const int32_t & persistentId)30 ScenePersistence::ScenePersistence(const SessionInfo& info, const int32_t& persistentId)
31 {
32 uint32_t fileID = static_cast<uint32_t>(persistentId) & 0x3fffffff;
33 strSnapshotFile_ =
34 strPersistPath_ + info.bundleName_ + UNDERLINE_SEPARATOR + std::to_string(fileID);
35 }
36
SaveSnapshot(const std::shared_ptr<Media::PixelMap> & pixelMap)37 void ScenePersistence::SaveSnapshot(const std::shared_ptr<Media::PixelMap>& pixelMap)
38 {
39 if (pixelMap == nullptr || strSnapshotFile_.find('/') == std::string::npos) {
40 return;
41 }
42
43 OHOS::Media::ImagePacker imagePacker;
44 OHOS::Media::PackOption option;
45 option.format = "image/jpeg";
46 option.quality = IMAGE_QUALITY;
47 option.numberHint = 1;
48 std::set<std::string> formats;
49 auto ret = imagePacker.GetSupportedFormats(formats);
50 if (ret) {
51 WLOGFE("get supported formats() error : %{public}u", ret);
52 return;
53 }
54
55 imagePacker.StartPacking(GetSnapshotFilePath(), option);
56 imagePacker.AddImage(*pixelMap);
57 int64_t packedSize = 0;
58 imagePacker.FinalizePacking(packedSize);
59 snapshotSize_ = { pixelMap->GetWidth(), pixelMap->GetHeight() };
60 WLOGFD("save snapshot packedSize : %{public}" PRIu64 "", packedSize);
61 }
62
GetSnapshotFilePath() const63 std::string ScenePersistence::GetSnapshotFilePath() const
64 {
65 return strSnapshotFile_;
66 }
67
GetSnapshotSize() const68 std::pair<uint32_t, uint32_t> ScenePersistence::GetSnapshotSize() const
69 {
70 return snapshotSize_;
71 }
72
IsSnapshotExisted() const73 bool ScenePersistence::IsSnapshotExisted() const
74 {
75 struct stat buf = {};
76 if (stat(strSnapshotFile_.c_str(), &buf) != 0) {
77 WLOGFD("snapshot file : %{public}s is not exist!", strSnapshotFile_.c_str());
78 return false;
79 }
80 return S_ISREG(buf.st_mode);
81 }
82 } // namespace OHOS::Rosen
83