• 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 "snapshot_controller.h"
17 
18 #include <hitrace_meter.h>
19 
20 #include "surface_capture_future.h"
21 #include "surface_draw.h"
22 #include "window_manager_hilog.h"
23 #include "wm_common.h"
24 
25 namespace OHOS {
26 namespace Rosen {
27 namespace {
28     constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "SnapshotController"};
29 }
30 
GetSnapshot(const sptr<IRemoteObject> & token,Snapshot & snapshot)31 int32_t SnapshotController::GetSnapshot(const sptr<IRemoteObject> &token, Snapshot& snapshot)
32 {
33     HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "wms:GetSnapshot");
34     if (token == nullptr) {
35         WLOGFE("Get snapshot failed, because token is null.");
36         return static_cast<int32_t>(WMError::WM_ERROR_NULLPTR);
37     }
38     if (handler_ == nullptr || windowRoot_ == nullptr) {
39         WLOGFE("Get snapshot failed, because handler/root is null.");
40         return static_cast<int32_t>(WMError::WM_ERROR_NULLPTR);
41     }
42 
43     performReport_->start();
44     // get snapshot cache in wms main handler
45     std::shared_ptr<Media::PixelMap> pixelMap;
46     std::shared_ptr<RSSurfaceNode> surfaceNode;
47     auto task = [this, &pixelMap, &surfaceNode, token] () {
48         auto targetNode = windowRoot_->GetWindowNodeByAbilityToken(token);
49         if (targetNode != nullptr) {
50             pixelMap = targetNode->GetSnapshot();
51             targetNode->SetSnapshot(nullptr); // reset window snapshot after use
52             surfaceNode = targetNode->surfaceNode_;
53         }
54     };
55     handler_->PostSyncTask(task, AppExecFwk::EventQueue::Priority::IMMEDIATE);
56 
57     // do snapshot if no cache
58     if (pixelMap == nullptr) {
59         bool snapSucc = SurfaceDraw::GetSurfaceSnapshot(surfaceNode, pixelMap, 300); // snapshot time out 300ms
60         if (!snapSucc) {
61             return static_cast<int32_t>(WMError::WM_ERROR_NULLPTR);
62         }
63     }
64 
65     // success to snapshot
66     snapshot.SetPixelMap(pixelMap);
67     performReport_->end();
68     return static_cast<int32_t>(WM_OK);
69 }
70 } // namespace Rosen
71 } // namespace OHOS