• 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 "display_manager.h"
24 #include "wm_common.h"
25 #include "png.h"
26 namespace OHOS {
27 namespace SmartPerf {
28 using namespace OHOS::Media;
29 using namespace OHOS::Rosen;
ItemData()30 std::map<std::string, std::string> Capture::ItemData()
31 {
32     std::map<std::string, std::string> result;
33     long long curTime = SPUtils::GetCurTime();
34     std::string path = "data/local/tmp/capture/screenCap_" + std::to_string(curTime) + ".png";
35     result["capture"] = path;
36     Capture::TriggerGetCatch(curTime);
37     return result;
38 }
ThreadGetCatch(const std::string & curTime) const39 void Capture::ThreadGetCatch(const std::string &curTime) const
40 {
41     auto savePath = "data/local/tmp/capture/screenCap_" + curTime + ".png";
42     std::string cmdResult;
43     if (!SPUtils::FileAccess("/data/local/tmp/capture")) {
44         SPUtils::LoadCmd("mkdir /data/local/tmp/capture", cmdResult);
45         printf("/data/local/tmp/capture created! \n");
46     };
47     std::stringstream errorRecv;
48     auto fd = open(savePath.c_str(), O_RDWR | O_CREAT, 0666);
49     if (!TakeScreenCap(savePath)) {
50         std::cout << "Screen Capture Failed:---"<< errorRecv.str() << std::endl;
51     }
52     (void) close(fd);
53 }
TriggerGetCatch(long long curTime) const54 void Capture::TriggerGetCatch(long long curTime) const
55 {
56     std::string curTimeStr = std::to_string(curTime);
57     std::thread tStart(&Capture::ThreadGetCatch, this, curTimeStr);
58     tStart.detach();
59 }
TakeScreenCap(const std::string & savePath) const60 bool Capture::TakeScreenCap(const std::string &savePath) const
61 {
62     // get PixelMap from DisplayManager API
63     Rosen::DisplayManager &displayMgr = Rosen::DisplayManager::GetInstance();
64     std::shared_ptr<Media::PixelMap> pixelMap = displayMgr.GetScreenshot(displayMgr.GetDefaultDisplayId());
65     static constexpr int bitmapDepth = 8;
66     if (pixelMap == nullptr) {
67         std::cout << "Failed to get display pixelMap" << std::endl;
68         return false;
69     }
70     auto width = static_cast<uint32_t>(pixelMap->GetWidth());
71     auto height = static_cast<uint32_t>(pixelMap->GetHeight());
72     auto data = pixelMap->GetPixels();
73     auto stride = static_cast<uint32_t>(pixelMap->GetRowBytes());
74     png_structp pngStruct = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
75     if (pngStruct == nullptr) {
76         std::cout << "error: png_create_write_struct nullptr!" << std::endl;
77         return false;
78     }
79     png_infop pngInfo = png_create_info_struct(pngStruct);
80     if (pngInfo == nullptr) {
81         std::cout << "error: png_create_info_struct error nullptr!" << std::endl;
82         png_destroy_write_struct(&pngStruct, nullptr);
83         return false;
84     }
85     FILE *fp = fopen(savePath.c_str(), "wb");
86     if (fp == nullptr) {
87         std::cout << "error: open file error!" << std::endl;
88         png_destroy_write_struct(&pngStruct, &pngInfo);
89         return false;
90     }
91     png_init_io(pngStruct, fp);
92     png_set_IHDR(pngStruct, pngInfo, width, height, bitmapDepth, PNG_COLOR_TYPE_RGBA, PNG_INTERLACE_NONE,
93                  PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
94     png_set_packing(pngStruct);          // set packing info
95     png_write_info(pngStruct, pngInfo);  // write to header
96     for (uint32_t i = 0; i < height; i++) {
97         png_write_row(pngStruct, data + (i * stride));
98     }
99     png_write_end(pngStruct, pngInfo);
100     // free
101     png_destroy_write_struct(&pngStruct, &pngInfo);
102     (void)fclose(fp);
103     return true;
104 }
105 }
106 }
107