• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 <ctime>
17 #include <iostream>
18 #include <string>
19 
20 #include "screen_manager.h"
21 #include "snapshot_utils.h"
22 #include "surface_reader.h"
23 #include "surface_reader_handler_impl.h"
24 
25 using namespace OHOS;
26 using namespace OHOS::Rosen;
27 using namespace OHOS::Media;
28 
29 namespace {
30 const int SLEEP_US = 10 * 1000; // 10ms
31 const int MAX_SNAPSHOT_COUNT = 10;
32 const int MAX_WAIT_COUNT = 200;
33 const float DEFAULT_DENSITY = 2.0;
34 const std::string FILE_NAME = "/data/snapshot_virtual_screen";
35 }
36 
InitOption(ScreenId mainId,SurfaceReader & surfaceReader)37 static VirtualScreenOption InitOption(ScreenId mainId, SurfaceReader& surfaceReader)
38 {
39     auto defaultScreen = ScreenManager::GetInstance().GetScreenById(mainId);
40     VirtualScreenOption option = {
41         .name_ = "virtualScreen",
42         .width_ = defaultScreen->GetWidth(),
43         .height_ = defaultScreen->GetHeight(),
44         .density_ = DEFAULT_DENSITY,
45         .surface_ = surfaceReader.GetSurface(),
46         .flags_ = 0,
47         .isForShot_ = true,
48     };
49     return option;
50 }
51 
main(int argc,char * argv[])52 int main(int argc, char *argv[])
53 {
54     SurfaceReader surfaceReader;
55     sptr<SurfaceReaderHandlerImpl> surfaceReaderHandler = new SurfaceReaderHandlerImpl();
56     if (!surfaceReader.Init()) {
57         std::cout << "surfaceReader init failed!" << std::endl;
58         return 0;
59     }
60     surfaceReader.SetHandler(surfaceReaderHandler);
61     ScreenId mainId = static_cast<ScreenId>(DisplayManager::GetInstance().GetDefaultDisplayId());
62     VirtualScreenOption option = InitOption(mainId, surfaceReader);
63     ScreenId virtualScreenId = ScreenManager::GetInstance().CreateVirtualScreen(option);
64     std::vector<ScreenId> mirrorIds;
65     mirrorIds.push_back(virtualScreenId);
66     ScreenManager::GetInstance().MakeMirror(mainId, mirrorIds);
67     int fileIndex = 1;
68     auto startTime = time(nullptr);
69     if (startTime < 0) {
70         std::cout << "startTime error!" << std::endl;
71         return 0;
72     }
73     while (time(nullptr) - startTime < MAX_SNAPSHOT_COUNT) {
74         int waitCount = 0;
75         while (!surfaceReaderHandler->IsImageOk()) {
76             waitCount++;
77             if (waitCount >= MAX_WAIT_COUNT) {
78                 std::cout << "wait image overtime" << std::endl;
79                 break;
80             }
81             usleep(SLEEP_US);
82         }
83         if (waitCount >= MAX_WAIT_COUNT) {
84             continue;
85         }
86         auto pixelMap = surfaceReaderHandler->GetPixelMap();
87         bool ret = SnapShotUtils::WriteToJpegWithPixelMap(FILE_NAME + std::to_string(fileIndex) + ".jpeg", *pixelMap);
88         if (ret) {
89             std::cout << "snapshot "<< mainId << " write to " <<
90                 (FILE_NAME + std::to_string(fileIndex)).c_str() << " as jpeg" << std::endl;
91         } else {
92             std::cout << "snapshot "<< mainId << " write to " <<
93                 (FILE_NAME + std::to_string(fileIndex)).c_str() << " failed!" << std::endl;
94         }
95         surfaceReaderHandler->ResetFlag();
96         fileIndex++;
97     }
98     ScreenManager::GetInstance().DestroyVirtualScreen(virtualScreenId);
99     std::cout << "DestroyVirtualScreen " << virtualScreenId << std::endl;
100     return 0;
101 }
102