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