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