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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCENE_VIEWER_RENDER_SCENE_VIEWER
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCENE_VIEWER_RENDER_SCENE_VIEWER
18 
19 #include <EGL/egl.h>
20 
21 #include "base/geometry/animatable_float.h"
22 #include "base/geometry/quaternion.h"
23 #include "base/geometry/vec3.h"
24 #include "core/components/scene_viewer/scene_viewer_component.h"
25 #include "core/components/scene_viewer/scene_viewer_touch_handler.h"
26 #include "core/pipeline/base/render_node.h"
27 #include "foundation/graphic/graphic_3d/3d_widget_adapter/include/custom/custom_render_descriptor.h"
28 #include "foundation/graphic/graphic_3d/3d_widget_adapter/include/data_type/constants.h"
29 #include "foundation/graphic/graphic_3d/3d_widget_adapter/include/data_type/geometry/geometry.h"
30 #include "foundation/graphic/graphic_3d/3d_widget_adapter/include/data_type/gltf_animation.h"
31 #include "foundation/graphic/graphic_3d/3d_widget_adapter/include/data_type/position.h"
32 #include "foundation/graphic/graphic_3d/3d_widget_adapter/include/scene_viewer_adapter.h"
33 #include "graphics_manager.h"
34 #include "texture_layer.h"
35 
36 namespace OHOS::Ace {
37 
38 class RenderSceneViewer : public RenderNode {
39     DECLARE_ACE_TYPE(RenderSceneViewer, RenderNode)
40 
41 public:
42     explicit RenderSceneViewer(uint32_t key);
43     ~RenderSceneViewer() override;
44     static RefPtr<RenderNode> Create(uint32_t key);
45 
46     void Update(const RefPtr<Component>& component) override;
47     void PerformLayout() override;
48     void Paint(RenderContext& context, const Offset& offset) override;
49     void OnPaintFinish() override;
50 
OnAttachContext()51     void OnAttachContext() override
52     {
53         cameraPosition_.SetContextAndCallbacks(
54             context_, std::bind(&RenderSceneViewer::PerformCameraUpdate, this));
55         // Lights animation callbacks are added when lights are added.
56 
57         auto pipelineContext = GetContext().Upgrade();
58         if (!pipelineContext) {
59             LOGE("ACE-3D RenderSceneViewer::OnAttachContext() GetContext failed.");
60             return;
61         }
62 
63         surfaceChangeCallbackId_ = pipelineContext->RegisterSurfaceChangedCallback(
64             [&](int32_t width, int32_t height, int32_t oldWidth, int32_t oldHeight, WindowSizeChangeReason type) {
65             OnSurfaceChanged();
66         });
67 
68 #if MULTI_ECS_UPDATE_AT_ONCE
69         OHOS::Render3D::GraphicsManager::GetInstance().AttachContext(context_);
70 #endif
71     }
72 
73 protected:
74     RenderSceneViewer();
75     virtual void PaintTextureLayer(RenderContext& context, const Offset& offset) = 0;
76     virtual void PrepareTextureLayer(const OHOS::Render3D::TextureInfo& info) = 0;
77     virtual OHOS::Render3D::TextureInfo CreateRenderTarget(uint32_t width, uint32_t height) = 0;
78     virtual void ClearRenderObject() override;
79     virtual EGLContext GetRenderContext() = 0;
80     void OnTouchTestHit(
81         const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result) override;
82     void Initialize();
83     void HandleEvent(const OHOS::Render3D::SceneViewerTouchEvent& event);
84     void RenderWithContext(RenderContext& context, const Offset& offset) override;
85     uint32_t GetKey();
86     Size GetSize();
87 
88     std::string glTFSrc_;
89     std::string backgroundSrc_;
90     uint32_t textureId_;
91     bool animating_ = false;
92     std::vector<RefPtr<OHOS::Render3D::SVGeometry>> shapes_;
93     std::vector<RefPtr<OHOS::Render3D::SVCustomRenderDescriptor>> customRenders_;
94     bool handlesNotReady_ = false;
95 
96     // Camera properties.
97     float zNear_ = 0.0f;
98     float zFar_ = 0.0f;
99     float fovDegrees_ = 0.0f;
100     // Camera position and rotation.
101     OHOS::Render3D::Position cameraPosition_;
102     Vec3 cameraLookAtVec_ { 0.0f, 0.0f, 0.0f };
103     Vec3 cameraUpVec_ { 0.0f, 1.0f, 0.0f };
104     Quaternion cameraRotation_ { 0.0f, 0.0f, 0.0f, 1.0f };
105     // Lights.
106     std::vector<RefPtr<OHOS::Render3D::SVLight>> lights_;
107 
108     OHOS::Render3D::SceneViewerAdapter sceneViewerAdapter_;
109     OHOS::Render3D::TextureInfo textureInfo_;
110     std::shared_future<void> renderFinished_;
111 
112  private:
113     void PerformClick();
114     void PerformCameraUpdate();
115     void PerformLightUpdate();
116     void UpdateCameraOnly();
117     void UpdateLightOnly();
118     void PassGeometries(const std::vector<RefPtr<OHOS::Render3D::SVGeometry>>& geometries);
119     void UpdateGLTFAnimations(const std::vector<RefPtr<OHOS::Render3D::GLTFAnimation>>& gltfAnimations);
120     bool IsCameraPropertyChanged(const RefPtr<SceneViewerComponent>& svComponent);
121     void HandleLightsUpdate(const RefPtr<SceneViewerComponent>& svComponent);
122     bool IsLightPropertyChanged(const RefPtr<SceneViewerComponent>& svComponent);
123     void PassCustomRenders(const std::vector<RefPtr<OHOS::Render3D::SVCustomRenderDescriptor>>& customRenders);
124     void OnSurfaceChanged();
125 
126 private:
127     RefPtr<SceneViewerTouchHandler> touchHandler_;
128     Size sceneSize_;
129     uint32_t key_;
130     bool isClicked_ = false;
131     int32_t touchCount_ = 0;
132     bool isHandleCameraMove_ = false;
133     std::function<void()> onClick_;
134     bool needsSceneSetup_ = true;
135     bool isTransparent_ = false;
136     EGLContext eglContext_ = EGL_NO_CONTEXT;
137     bool inited_ = false;
138     int surfaceChangeCallbackId_;
139 };
140 
141 } // namespace OHOS::Ace
142 
143 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SCENE_VIEWER_RENDER_SCENE_VIEWER
144