1 /*
2  * Copyright (c) 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 "core/components/scene_viewer/rosen_render_scene_viewer.h"
17 
18 #include "base/log/ace_trace.h"
19 #include "core/pipeline/base/rosen_render_context.h"
20 #include "graphics_manager.h"
21 #include "graphics_task.h"
22 #include "include/core/SkFont.h"
23 #include "include/core/SkMatrix.h"
24 #include "include/core/SkTextBlob.h"
25 #include "render_service_client/core/ui/rs_ui_director.h"
26 #include "render_service_client/core/ui/rs_ui_share_context.h"
27 
28 namespace OHOS::Ace {
29 
RosenRenderSceneViewer(uint32_t key)30 RosenRenderSceneViewer::RosenRenderSceneViewer(uint32_t key) : RenderSceneViewer(key),
31     textureLayer_(std::make_shared<OHOS::Render3D::TextureLayer>())
32 {
33     auto pipelineContext = GetContext().Upgrade();
34     if (pipelineContext == nullptr) {
35         LOGE("SceneViewer get pipeline context error");
36         return;
37     }
38 
39     const auto &rsUIDirector = pipelineContext->GetRSUIDirector();
40     if (rsUIDirector == nullptr) {
41         LOGE("SceneView RosenRender RSUIDirector null");
42         return;
43     }
44 }
45 
CreateRenderTarget(uint32_t width,uint32_t height)46 OHOS::Render3D::TextureInfo RosenRenderSceneViewer::CreateRenderTarget(uint32_t width, uint32_t height)
47 {
48     auto info = textureLayer_->CreateRenderTarget(width, height);
49     return info;
50 }
51 
PrepareTextureLayer(const OHOS::Render3D::TextureInfo & info)52 void RosenRenderSceneViewer::PrepareTextureLayer(const OHOS::Render3D::TextureInfo& info)
53 {
54     width_ = static_cast<uint32_t>(GetLayoutSize().Width());
55     height_ = static_cast<uint32_t>(GetLayoutSize().Height());
56     textureLayer_->SetWH(width_, height_);
57 }
58 
PaintTextureLayer(RenderContext & context,const Offset & offset)59 void RosenRenderSceneViewer::PaintTextureLayer(RenderContext& context, const Offset& offset)
60 {
61     auto newWidth = static_cast<uint32_t>(GetLayoutSize().Width());
62     auto newHeight = static_cast<uint32_t>(GetLayoutSize().Height());
63     if (width_ != newWidth || height_ != newHeight) {
64         width_ = newWidth;
65         height_ = newHeight;
66         textureLayer_->SetWH(width_, height_);
67     }
68 
69     textureLayer_->UpdateRenderFinishFuture(renderFinished_);
70     textureLayer_->SetOffset(offset.GetX(), offset.GetY());
71     ACE_FUNCTION_TRACE();
72 #ifndef USE_ROSEN_DRAWING
73     auto skCanvas = static_cast<RosenRenderContext*>(&context)->GetCanvas();
74 #else
75     auto drawingCanvas = static_cast<RosenRenderContext*>(&context)->GetCanvas();
76     auto rsCanvas = drawingCanvas->GetImpl<RSSkCanvas>();
77     auto skCanvas = rsCanvas->ExportSkCanvas();
78 #endif
79     if (skCanvas == nullptr) {
80         LOGE("SceneView RosenRenderSceneView::PaintTexture skCanvas is null");
81         return;
82     }
83 
84     textureLayer_->OnDraw(skCanvas);
85     (void)(context);
86 }
87 
GetRenderContext()88 EGLContext RosenRenderSceneViewer::GetRenderContext()
89 {
90     auto ret = EGL_NO_CONTEXT;
91     auto pipeline_context = GetContext().Upgrade();
92     if (pipeline_context == nullptr) {
93         LOGE("SceneView RosenRender piplineContext null");
94         return ret;
95     }
96 
97     ret = Rosen::RSUIShareContext::GetInstance().GetRsRenderContext();
98     return ret;
99 }
100 } // namespace OHOS::Ace
101