1 /*
2 * Copyright (C) 2021 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 #include <iostream>
16 #include <sstream>
17 #include <thread>
18 #include <unistd.h>
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include "include/sp_utils.h"
22 #include "include/Capture.h"
23 #include "include/sp_log.h"
24 #include "display_manager.h"
25 #include "wm_common.h"
26 #include "png.h"
27 #include <filesystem>
28 #include "include/common.h"
29 namespace OHOS {
30 namespace SmartPerf {
31 using namespace OHOS::Media;
32 using namespace OHOS::Rosen;
ItemData()33 std::map<std::string, std::string> Capture::ItemData()
34 {
35 std::map<std::string, std::string> result;
36 const int two = 2;
37 const int modResult = callNum % two;
38 callNum++;
39 curTime = GetCurTimes();
40 std::string screenCapPath = "data/local/tmp/capture/screenCap_" + std::to_string(curTime);
41 std::string path = "NA";
42 if (isSocketMessage) {
43 if (modResult == 0) {
44 path = screenCapPath + ".jpeg";
45 result["capture"] = path;
46 TriggerGetCatchSocket();
47 isSocketMessage = false;
48 }
49 } else {
50 if (modResult == 1) {
51 path = screenCapPath + ".png";
52 result["capture"] = path;
53 TriggerGetCatch();
54 }
55 }
56 result["capture"] = path;
57
58 LOGD("capture:ItemData map size(%u)", result.size());
59 return result;
60 }
61
GetCurTimes()62 long long Capture::GetCurTimes()
63 {
64 return SPUtils::GetCurTime();
65 }
66
SocketMessage()67 void Capture::SocketMessage()
68 {
69 isSocketMessage = true;
70 }
ThreadGetCatch()71 void Capture::ThreadGetCatch()
72 {
73 const std::string captureDir = "/data/local/tmp/capture";
74 const std::string savePath = captureDir + "/screenCap_" + std::to_string(curTime) + ".png";
75 std::string cmdResult;
76 if (!SPUtils::FileAccess(captureDir)) {
77 std::string capturePath = CMD_COMMAND_MAP.at(CmdCommand::CREAT_DIR) + captureDir;
78 if (!SPUtils::LoadCmd(capturePath, cmdResult)) {
79 LOGE("%s capture not be created!", captureDir.c_str());
80 return;
81 } else {
82 LOGD("%s created successfully!", captureDir.c_str());
83 }
84 };
85 std::ostringstream errorRecv;
86 auto fd = open(savePath.c_str(), O_RDWR | O_CREAT, 0666);
87 if (fd == -1) {
88 LOGE("Failed to open file: %s", savePath.c_str());
89 return;
90 }
91 if (!TakeScreenCap(savePath)) {
92 LOGE("Screen Capture Failed!");
93 close(fd);
94 return;
95 }
96 close(fd);
97 }
98
99
ThreadGetCatchSocket()100 void Capture::ThreadGetCatchSocket()
101 {
102 std::string captureTime = std::to_string(curTime);
103 std::string captureDir = "/data/local/tmp/capture";
104 std::string savePath = captureDir + "/screenCap_" + captureTime + ".jpeg";
105 std::string cmdResult;
106 if (!SPUtils::FileAccess(captureDir)) {
107 std::string capturePath = CMD_COMMAND_MAP.at(CmdCommand::CREAT_DIR) + captureDir;
108 if (!SPUtils::LoadCmd(capturePath, cmdResult)) {
109 LOGE("%s capture not be created!", captureDir.c_str());
110 return;
111 } else {
112 LOGD("%s created successfully!", captureDir.c_str());
113 }
114 };
115
116 auto fd = open(savePath.c_str(), O_RDWR | O_CREAT, 0644);
117 if (fd == -1) {
118 LOGE("Capture::ThreadGetCatchSocket Failed to open file");
119 return;
120 }
121 std::string snapshot = CMD_COMMAND_MAP.at(CmdCommand::SNAPSHOT);
122 if (!SPUtils::LoadCmd(snapshot + savePath, cmdResult)) {
123 LOGE("snapshot_display command failed!");
124 close(fd);
125 return;
126 }
127 close(fd);
128 }
129
TriggerGetCatch()130 void Capture::TriggerGetCatch()
131 {
132 auto tStart = std::thread([this]() {
133 this->ThreadGetCatch();
134 });
135 tStart.detach();
136 }
137
TriggerGetCatchSocket()138 void Capture::TriggerGetCatchSocket()
139 {
140 auto tStart = std::thread([this]() {
141 this->ThreadGetCatchSocket();
142 });
143 tStart.detach();
144 }
145
TakeScreenCap(const std::string & savePath) const146 bool Capture::TakeScreenCap(const std::string &savePath) const
147 {
148 Rosen::DisplayManager &displayMgr = Rosen::DisplayManager::GetInstance();
149 std::shared_ptr<Media::PixelMap> pixelMap = displayMgr.GetScreenshot(displayMgr.GetDefaultDisplayId());
150 static constexpr int bitmapDepth = 8;
151 if (pixelMap == nullptr) {
152 LOGE("Failed to get display pixelMap");
153 return false;
154 }
155 auto width = static_cast<uint32_t>(pixelMap->GetWidth());
156 auto height = static_cast<uint32_t>(pixelMap->GetHeight());
157 auto data = pixelMap->GetPixels();
158 auto stride = static_cast<uint32_t>(pixelMap->GetRowBytes());
159 png_structp pngStruct = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
160 if (pngStruct == nullptr) {
161 LOGE("png_create_write_struct nullptr!");
162 return false;
163 }
164 png_infop pngInfo = png_create_info_struct(pngStruct);
165 if (pngInfo == nullptr) {
166 LOGE("png_create_info_struct error nullptr!");
167 png_destroy_write_struct(&pngStruct, nullptr);
168 return false;
169 }
170 char realPath[PATH_MAX] = {0x00};
171 if (realpath(savePath.c_str(), realPath) == nullptr) {
172 std::cout << "" << std::endl;
173 }
174 FILE *fp = fopen(realPath, "wb");
175 if (fp == nullptr) {
176 LOGE("open file error!");
177 png_destroy_write_struct(&pngStruct, &pngInfo);
178 return false;
179 }
180 png_init_io(pngStruct, fp);
181 png_set_IHDR(pngStruct, pngInfo, width, height, bitmapDepth, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE,
182 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
183 png_set_packing(pngStruct); // set packing info
184 png_write_info(pngStruct, pngInfo); // write to header
185 for (uint32_t i = 0; i < height; i++) {
186 png_write_row(pngStruct, data + (i * stride));
187 }
188 png_write_end(pngStruct, pngInfo);
189 // free
190 png_destroy_write_struct(&pngStruct, &pngInfo);
191 (void)fclose(fp);
192 return true;
193 }
SetCollectionNum()194 void Capture::SetCollectionNum()
195 {
196 callNum = 0;
197 }
198 }
199 }
200