• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (rootNode == nullptr) {
28         return;
29     }
30     rsUiDirector_ = RSUIDirector::Create();
31     rootNode_ = rootNode;
32     surfaceId_ = surfaceId;
33     RSSurfaceNodeConfig config = {
34         .SurfaceNodeName = "textureExportSurfaceNode",
35         .additionalData = nullptr,
36         .isTextureExportNode = true,
37         .surfaceId = surfaceId_
38     };
39     virtualSurfaceNode_ = RSSurfaceNode::Create(config, false, rsUiDirector_->GetRSUIContext());
40     rootNode_->SyncTextureExport(true);
41 }
42 
~RSTextureExport()43 RSTextureExport::~RSTextureExport()
44 {
45     rsUiDirector_->Destroy(true);
46 }
47 
DoTextureExport()48 bool RSTextureExport::DoTextureExport()
49 {
50     auto rsUIContext = rsUiDirector_->GetRSUIContext();
51     if (!rootNode_->IsTextureExportNode()) {
52         rootNode_->SyncTextureExport(true);
53     }
54     rsUiDirector_->StartTextureExport();
55     if (rootNode_->GetType() != RSUINodeType::ROOT_NODE) {
56         virtualRootNode_ = RSRootNode::Create(false, true, rsUIContext);
57         auto bounds = rootNode_->GetStagingProperties().GetBounds();
58         virtualRootNode_->SetBounds({-bounds.x_, -bounds.y_, bounds.z_, bounds.w_});
59         auto frame = rootNode_->GetStagingProperties().GetFrame();
60         virtualRootNode_->SetFrame({-frame.x_, -frame.y_, frame.z_, frame.w_});
61     }
62     if (!virtualSurfaceNode_) {
63         ROSEN_LOGE("RSTextureExport::DoTextureExport create surfaceNode failed");
64         return false;
65     }
66     if (rootNode_->GetType() == RSUINodeType::ROOT_NODE) {
67         rsUiDirector_->SetRoot(rootNode_->GetId());
68     } else {
69         rsUiDirector_->SetRoot(virtualRootNode_->GetId());
70         virtualRootNode_->AddChild(rootNode_);
71     }
72     rsUiDirector_->SetRSSurfaceNode(virtualSurfaceNode_);
73     rsUiDirector_->GoForeground(true);
74     return true;
75 }
76 
UpdateBufferInfo(float x,float y,float width,float height)77 void RSTextureExport::UpdateBufferInfo(float x, float y, float width, float height)
78 {
79     virtualRootNode_->SetBounds({-x, -y, width, height});
80     virtualRootNode_->SetFrame({-x, -y, width, height});
81 }
82 
StopTextureExport()83 void RSTextureExport::StopTextureExport()
84 {
85     rsUiDirector_->Destroy(true);
86     rootNode_->RemoveFromTree();
87 }
88 
89 } // namespace Rosen
90 } // namespace OHOS
91