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 25namespace OHOS { 26namespace Rosen { 27namespace { 28constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "SnapshotController"}; 29} 30 31int32_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 if (targetNode->surfaceNode_ && targetNode->firstFrameAvailable_) { 53 surfaceNode = targetNode->surfaceNode_; 54 } else { 55 surfaceNode = targetNode->startingWinSurfaceNode_; 56 } 57 } 58 }; 59 handler_->PostSyncTask(task, "wms:GetSnapshot", AppExecFwk::EventQueue::Priority::IMMEDIATE); 60 61 // do snapshot if no cache 62 if (pixelMap == nullptr) { 63 // snapshot time out 300ms 64 bool snapSucc = SurfaceDraw::GetSurfaceSnapshot(surfaceNode, pixelMap, SNAPSHOT_TIMEOUT_MS); 65 if (!snapSucc) { 66 return static_cast<int32_t>(WMError::WM_ERROR_NULLPTR); 67 } 68 } 69 70 // success to snapshot 71 snapshot.SetPixelMap(pixelMap); 72 performReport_->end(); 73 return static_cast<int32_t>(WM_OK); 74} 75} // namespace Rosen 76} // namespace OHOS