• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_NG_PATTERN_MODEL_MODEL_ADAPTER_WRAPPER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_MODEL_MODEL_ADAPTER_WRAPPER_H
18 
19 #include <EGL/egl.h>
20 
21 #include "base/geometry/animatable_float.h"
22 #include "base/geometry/ng/offset_t.h"
23 #include "base/geometry/ng/size_t.h"
24 #include "base/geometry/quaternion.h"
25 #include "base/geometry/vec3.h"
26 #include "core/components_ng/pattern/model/model_layout_property.h"
27 #include "core/components_ng/pattern/model/model_paint_property.h"
28 #include "core/components_ng/pattern/model/model_touch_handler.h"
29 #include "foundation/graphic/graphic_3d/3d_widget_adapter/include/data_type/scene_viewer_touch_event.h"
30 #include "foundation/graphic/graphic_3d/3d_widget_adapter/include/data_type/shader_input_buffer.h"
31 #include "foundation/graphic/graphic_3d/3d_widget_adapter/include/ohos/texture_layer.h"
32 #include "foundation/graphic/graphic_3d/3d_widget_adapter/include/scene_viewer_adapter.h"
33 
34 namespace OHOS::Ace::NG {
35 #define MAX_INVALID std::numeric_limits<double>::max()
36 struct SceneViewerAdapterProperties {
37     // Scene properties
38     std::string gltfSrc_ = "";
39     std::string backgroundSrc_ = "";
40     OHOS::Render3D::SceneViewerBackgroundType bgType_ = OHOS::Render3D::SceneViewerBackgroundType::CUBE_MAP;
41 
42     // FOV
43     float zNear_ = 0.5f;
44     float zFar_ = 50.0f;
45     float fov_ = 60.0f;
46 
47     // Camera properties.
48     OHOS::Render3D::Position cameraPosition_;
49     Vec3 cameraLookAt_ = Vec3(0.0f, 0.0f, 0.0f);
50     Vec3 cameraUp_ = Vec3(0.0f, 1.0f, 0.0f);
51 
52     // Invalid by default. Either camera of rotation or lookat is used at the end.
53     Quaternion cameraRotation_ = Quaternion(MAX_INVALID,
54         MAX_INVALID, MAX_INVALID, MAX_INVALID);
55 
56     std::vector<RefPtr<OHOS::Render3D::SVLight>> lights_;
57     std::vector<RefPtr<OHOS::Render3D::GLTFAnimation>> animations_;
58     std::vector<RefPtr<OHOS::Render3D::SVGeometry>> geometries_;
59     std::vector<RefPtr<OHOS::Render3D::SVCustomRenderDescriptor>> customRenders_;
60 
61     // Shader properties.
62     std::string shaderPath_ = "";
63     std::vector<std::string> imageTexturePaths_;
64     std::vector<RefPtr<OHOS::Render3D::ShaderInputBuffer>> shaderInputBuffers_;
65 };
66 
67 class ModelAdapterWrapper : public virtual AceType {
68     DECLARE_ACE_TYPE(ModelAdapterWrapper, AceType)
69 public:
70     using PaintFinishCallback = std::function<void()>;
71 
72     ModelAdapterWrapper(uint32_t key);
73     ~ModelAdapterWrapper() = default;
74 
75     void OnMeasureContent(const RefPtr<ModelLayoutProperty>& modelLayoutProperty, SizeF size);
76     void OnPaint(const RefPtr<ModelPaintProperty>& modelPaintProperty);
77     void OnPaintFinish();
78     void SetPaintFinishCallback(PaintFinishCallback callback);
79     bool IsInitialized();
80     bool IsReady();
81     bool NeedsRepaint();
82     SkDrawable* GetDrawable(OffsetF offset);
83     std::shared_ptr<OHOS::Render3D::TextureLayer> GetTextureLayer(OffsetF offset);
84     bool HandleTouchEvent(const TouchEventInfo& info);
85 
86 private:
87     uint32_t GetKey();
88     void Initialize();
89     void CreateTextureLayer(const EGLContext& eglContext);
90     void UpdateTextureLayer();
91     void CreateSceneViewerAdapter(const EGLContext& eglContext);
92     void UpdateSceneViewerAdapter(const SceneViewerAdapterProperties& properties);
93     SceneViewerAdapterProperties ExtractLayoutProperties(const RefPtr<ModelLayoutProperty>& modelLayoutProperty);
94     SceneViewerAdapterProperties ExtractPaintProperties(const RefPtr<ModelPaintProperty>& modelPaintProperty);
95     void UnloadScene();
96     EGLContext GetRenderContext();
97     void DrawFrame();
98     void UpdateCamera(const SceneViewerAdapterProperties& properties);
99     void UpdateLights(const SceneViewerAdapterProperties& properties);
100     void UpdateGLTFAnimations(const SceneViewerAdapterProperties& properties);
101     void UpdateGeometries(const SceneViewerAdapterProperties& properties);
102     void HandleCameraMove(const OHOS::Render3D::SceneViewerTouchEvent& event);
103     void UpdateCustomRenders(const SceneViewerAdapterProperties& properties);
104     void UpdateShaderPath(const SceneViewerAdapterProperties& properties);
105     void UpdateImageTexturePaths(const SceneViewerAdapterProperties& properties);
106     void UpdateShaderInputBuffers(const SceneViewerAdapterProperties& properties);
107 
108 private:
109     uint32_t key_ = -1;
110     SizeF size_ { 0.0f, 0.0f };
111     PaintFinishCallback callback_ = nullptr;
112     bool needsRepaint_ = false;
113     bool sceneIsSetUp_ = false;
114 
115     std::shared_ptr<OHOS::Render3D::SceneViewerAdapter> sceneViewerAdapter_;
116     std::shared_ptr<OHOS::Render3D::TextureInfo> textureInfo_;
117     std::shared_ptr<OHOS::Render3D::TextureLayer> textureLayer_;
118     RefPtr<ModelTouchHandler> touchHandler_;
119     EGLContext eglContext_ = EGL_NO_CONTEXT;
120     ACE_DISALLOW_COPY_AND_MOVE(ModelAdapterWrapper);
121 };
122 
123 } // namespace OHOS::Ace::NG
124 
125 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_MODEL_MODEL_ADAPTER_WRAPPER_H
126