1 /*
2  * Copyright (c) 2023 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 "ui/rs_texture_export.h"
17 
18 #include "platform/common/rs_log.h"
19 #include "ui/rs_root_node.h"
20 #include "ui/rs_surface_node.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 
RSTextureExport(std::shared_ptr<RSNode> rootNode,SurfaceId surfaceId)25 RSTextureExport::RSTextureExport(std::shared_ptr<RSNode> rootNode, SurfaceId surfaceId)
26 {
27     rsUiDirector_ = RSUIDirector::Create();
28     rootNode_ = rootNode;
29     surfaceId_ = surfaceId;
30     RSSurfaceNodeConfig config = {
31         .SurfaceNodeName = "textureExportSurfaceNode",
32         .additionalData = nullptr,
33         .isTextureExportNode = true,
34         .surfaceId = surfaceId_
35     };
36     virtualSurfaceNode_ = RSSurfaceNode::Create(config, false);
37     rootNode_->SyncTextureExport(true);
38 }
39 
~RSTextureExport()40 RSTextureExport::~RSTextureExport()
41 {
42     rsUiDirector_->Destroy(true);
43 }
44 
DoTextureExport()45 bool RSTextureExport::DoTextureExport()
46 {
47     if (!rootNode_->IsTextureExportNode()) {
48         rootNode_->SyncTextureExport(true);
49     }
50     rsUiDirector_->StartTextureExport();
51     if (rootNode_->GetType() != RSUINodeType::ROOT_NODE) {
52         virtualRootNode_ = RSRootNode::Create(false, true);
53         auto bounds = rootNode_->GetStagingProperties().GetBounds();
54         virtualRootNode_->SetBounds({-bounds.x_, -bounds.y_, bounds.z_, bounds.w_});
55         auto frame = rootNode_->GetStagingProperties().GetFrame();
56         virtualRootNode_->SetFrame({-frame.x_, -frame.y_, frame.z_, frame.w_});
57     }
58     if (!virtualSurfaceNode_) {
59         ROSEN_LOGE("RSTextureExport::DoTextureExport create surfaceNode failed");
60         return false;
61     }
62     if (rootNode_->GetType() == RSUINodeType::ROOT_NODE) {
63         rsUiDirector_->SetRoot(rootNode_->GetId());
64     } else {
65         rsUiDirector_->SetRoot(virtualRootNode_->GetId());
66         virtualRootNode_->AddChild(rootNode_);
67     }
68     rsUiDirector_->SetRSSurfaceNode(virtualSurfaceNode_);
69     rsUiDirector_->GoForeground(true);
70     return true;
71 }
72 
UpdateBufferInfo(float x,float y,float width,float height)73 void RSTextureExport::UpdateBufferInfo(float x, float y, float width, float height)
74 {
75     virtualRootNode_->SetBounds({-x, -y, width, height});
76     virtualRootNode_->SetFrame({-x, -y, width, height});
77 }
78 
StopTextureExport()79 void RSTextureExport::StopTextureExport()
80 {
81     rsUiDirector_->Destroy(true);
82     rootNode_->RemoveFromTree();
83 }
84 
85 } // namespace Rosen
86 } // namespace OHOS
87