1e0dac50fSopenharmony_ci/*
2e0dac50fSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3e0dac50fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4e0dac50fSopenharmony_ci * you may not use this file except in compliance with the License.
5e0dac50fSopenharmony_ci * You may obtain a copy of the License at
6e0dac50fSopenharmony_ci *
7e0dac50fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8e0dac50fSopenharmony_ci *
9e0dac50fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10e0dac50fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11e0dac50fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12e0dac50fSopenharmony_ci * See the License for the specific language governing permissions and
13e0dac50fSopenharmony_ci * limitations under the License.
14e0dac50fSopenharmony_ci */
15e0dac50fSopenharmony_ci
16e0dac50fSopenharmony_ci#include "snapshot_controller.h"
17e0dac50fSopenharmony_ci
18e0dac50fSopenharmony_ci#include <hitrace_meter.h>
19e0dac50fSopenharmony_ci
20e0dac50fSopenharmony_ci#include "surface_capture_future.h"
21e0dac50fSopenharmony_ci#include "surface_draw.h"
22e0dac50fSopenharmony_ci#include "window_manager_hilog.h"
23e0dac50fSopenharmony_ci#include "wm_common.h"
24e0dac50fSopenharmony_ci
25e0dac50fSopenharmony_cinamespace OHOS {
26e0dac50fSopenharmony_cinamespace Rosen {
27e0dac50fSopenharmony_cinamespace {
28e0dac50fSopenharmony_ciconstexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "SnapshotController"};
29e0dac50fSopenharmony_ci}
30e0dac50fSopenharmony_ci
31e0dac50fSopenharmony_ciint32_t SnapshotController::GetSnapshot(const sptr<IRemoteObject>& token, Snapshot& snapshot)
32e0dac50fSopenharmony_ci{
33e0dac50fSopenharmony_ci    HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "wms:GetSnapshot");
34e0dac50fSopenharmony_ci    if (token == nullptr) {
35e0dac50fSopenharmony_ci        WLOGFE("Get snapshot failed, because token is null.");
36e0dac50fSopenharmony_ci        return static_cast<int32_t>(WMError::WM_ERROR_NULLPTR);
37e0dac50fSopenharmony_ci    }
38e0dac50fSopenharmony_ci    if (handler_ == nullptr || windowRoot_ == nullptr) {
39e0dac50fSopenharmony_ci        WLOGFE("Get snapshot failed, because handler/root is null.");
40e0dac50fSopenharmony_ci        return static_cast<int32_t>(WMError::WM_ERROR_NULLPTR);
41e0dac50fSopenharmony_ci    }
42e0dac50fSopenharmony_ci
43e0dac50fSopenharmony_ci    performReport_->start();
44e0dac50fSopenharmony_ci    // get snapshot cache in wms main handler
45e0dac50fSopenharmony_ci    std::shared_ptr<Media::PixelMap> pixelMap;
46e0dac50fSopenharmony_ci    std::shared_ptr<RSSurfaceNode> surfaceNode;
47e0dac50fSopenharmony_ci    auto task = [this, &pixelMap, &surfaceNode, token] () {
48e0dac50fSopenharmony_ci        auto targetNode = windowRoot_->GetWindowNodeByAbilityToken(token);
49e0dac50fSopenharmony_ci        if (targetNode != nullptr) {
50e0dac50fSopenharmony_ci            pixelMap = targetNode->GetSnapshot();
51e0dac50fSopenharmony_ci            targetNode->SetSnapshot(nullptr); // reset window snapshot after use
52e0dac50fSopenharmony_ci            if (targetNode->surfaceNode_ && targetNode->firstFrameAvailable_) {
53e0dac50fSopenharmony_ci                surfaceNode = targetNode->surfaceNode_;
54e0dac50fSopenharmony_ci            } else {
55e0dac50fSopenharmony_ci                surfaceNode = targetNode->startingWinSurfaceNode_;
56e0dac50fSopenharmony_ci            }
57e0dac50fSopenharmony_ci        }
58e0dac50fSopenharmony_ci    };
59e0dac50fSopenharmony_ci    handler_->PostSyncTask(task, "wms:GetSnapshot", AppExecFwk::EventQueue::Priority::IMMEDIATE);
60e0dac50fSopenharmony_ci
61e0dac50fSopenharmony_ci    // do snapshot if no cache
62e0dac50fSopenharmony_ci    if (pixelMap == nullptr) {
63e0dac50fSopenharmony_ci        // snapshot time out 300ms
64e0dac50fSopenharmony_ci        bool snapSucc = SurfaceDraw::GetSurfaceSnapshot(surfaceNode, pixelMap, SNAPSHOT_TIMEOUT_MS);
65e0dac50fSopenharmony_ci        if (!snapSucc) {
66e0dac50fSopenharmony_ci            return static_cast<int32_t>(WMError::WM_ERROR_NULLPTR);
67e0dac50fSopenharmony_ci        }
68e0dac50fSopenharmony_ci    }
69e0dac50fSopenharmony_ci
70e0dac50fSopenharmony_ci    // success to snapshot
71e0dac50fSopenharmony_ci    snapshot.SetPixelMap(pixelMap);
72e0dac50fSopenharmony_ci    performReport_->end();
73e0dac50fSopenharmony_ci    return static_cast<int32_t>(WM_OK);
74e0dac50fSopenharmony_ci}
75e0dac50fSopenharmony_ci} // namespace Rosen
76e0dac50fSopenharmony_ci} // namespace OHOS