18bf80f4bSopenharmony_ci/*
28bf80f4bSopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
38bf80f4bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
48bf80f4bSopenharmony_ci * you may not use this file except in compliance with the License.
58bf80f4bSopenharmony_ci * You may obtain a copy of the License at
68bf80f4bSopenharmony_ci *
78bf80f4bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
88bf80f4bSopenharmony_ci *
98bf80f4bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
108bf80f4bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
118bf80f4bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
128bf80f4bSopenharmony_ci * See the License for the specific language governing permissions and
138bf80f4bSopenharmony_ci * limitations under the License.
148bf80f4bSopenharmony_ci */
158bf80f4bSopenharmony_ci
168bf80f4bSopenharmony_ci#include "widget_adapter.h"
178bf80f4bSopenharmony_ci
188bf80f4bSopenharmony_ci#include <window.h>
198bf80f4bSopenharmony_ci
208bf80f4bSopenharmony_ci#include "3d_widget_adapter_log.h"
218bf80f4bSopenharmony_ci#include "graphics_manager.h"
228bf80f4bSopenharmony_ci#include "widget_trace.h"
238bf80f4bSopenharmony_ci
248bf80f4bSopenharmony_cinamespace OHOS::Render3D {
258bf80f4bSopenharmony_ci#ifdef CHECK_NULL_PTR
268bf80f4bSopenharmony_ci#undef CHECK_NULL_PTR
278bf80f4bSopenharmony_ci#endif
288bf80f4bSopenharmony_ci#define CHECK_NULL_PTR(ptr) \
298bf80f4bSopenharmony_cido { \
308bf80f4bSopenharmony_ci    if (!(ptr)) { \
318bf80f4bSopenharmony_ci        WIDGET_LOGE("%s engine not ready", __func__); \
328bf80f4bSopenharmony_ci        return false; \
338bf80f4bSopenharmony_ci    } \
348bf80f4bSopenharmony_ci} while (false)
358bf80f4bSopenharmony_ci
368bf80f4bSopenharmony_ciWidgetAdapter::WidgetAdapter(uint32_t key) : key_(key)
378bf80f4bSopenharmony_ci{
388bf80f4bSopenharmony_ci    WIDGET_LOGD("WidgetAdapter create key %d", key);
398bf80f4bSopenharmony_ci}
408bf80f4bSopenharmony_ci
418bf80f4bSopenharmony_ciWidgetAdapter::~WidgetAdapter()
428bf80f4bSopenharmony_ci{
438bf80f4bSopenharmony_ci    WIDGET_LOGD("WidgetAdapter destroy key %d", key_);
448bf80f4bSopenharmony_ci}
458bf80f4bSopenharmony_ci
468bf80f4bSopenharmony_cibool WidgetAdapter::Initialize(std::unique_ptr<IEngine> engine)
478bf80f4bSopenharmony_ci{
488bf80f4bSopenharmony_ci    WIDGET_SCOPED_TRACE("WidgetAdapter::Initialze");
498bf80f4bSopenharmony_ci    engine_ = std::move(engine);
508bf80f4bSopenharmony_ci    CHECK_NULL_PTR(engine_);
518bf80f4bSopenharmony_ci    engine_->InitializeScene(key_);
528bf80f4bSopenharmony_ci    return true;
538bf80f4bSopenharmony_ci}
548bf80f4bSopenharmony_ci
558bf80f4bSopenharmony_cibool WidgetAdapter::SetupCameraTransform(
568bf80f4bSopenharmony_ci    const OHOS::Render3D::Position& position, const OHOS::Render3D::Vec3& lookAt,
578bf80f4bSopenharmony_ci    const OHOS::Render3D::Vec3& up, const OHOS::Render3D::Quaternion& rotation)
588bf80f4bSopenharmony_ci{
598bf80f4bSopenharmony_ci    CHECK_NULL_PTR(engine_);
608bf80f4bSopenharmony_ci    engine_->SetupCameraTransform(position, lookAt, up, rotation);
618bf80f4bSopenharmony_ci    return true;
628bf80f4bSopenharmony_ci}
638bf80f4bSopenharmony_ci
648bf80f4bSopenharmony_cibool WidgetAdapter::SetupCameraViewProjection(float zNear, float zFar, float fovDegrees)
658bf80f4bSopenharmony_ci{
668bf80f4bSopenharmony_ci    CHECK_NULL_PTR(engine_);
678bf80f4bSopenharmony_ci    engine_->SetupCameraViewProjection(zNear, zFar, fovDegrees);
688bf80f4bSopenharmony_ci    return true;
698bf80f4bSopenharmony_ci}
708bf80f4bSopenharmony_ci
718bf80f4bSopenharmony_cibool WidgetAdapter::UpdateLights(const std::vector<std::shared_ptr<OHOS::Render3D::Light>>& lights)
728bf80f4bSopenharmony_ci{
738bf80f4bSopenharmony_ci    CHECK_NULL_PTR(engine_);
748bf80f4bSopenharmony_ci    engine_->UpdateLights(lights);
758bf80f4bSopenharmony_ci    return true;
768bf80f4bSopenharmony_ci}
778bf80f4bSopenharmony_ci
788bf80f4bSopenharmony_cibool WidgetAdapter::OnTouchEvent(const PointerEvent& event)
798bf80f4bSopenharmony_ci{
808bf80f4bSopenharmony_ci    CHECK_NULL_PTR(engine_);
818bf80f4bSopenharmony_ci    engine_->OnTouchEvent(event);
828bf80f4bSopenharmony_ci    return true;
838bf80f4bSopenharmony_ci}
848bf80f4bSopenharmony_ci
858bf80f4bSopenharmony_cibool WidgetAdapter::DrawFrame()
868bf80f4bSopenharmony_ci{
878bf80f4bSopenharmony_ci    WIDGET_SCOPED_TRACE("WidgetAdpater::DrawFrame");
888bf80f4bSopenharmony_ci    CHECK_NULL_PTR(engine_);
898bf80f4bSopenharmony_ci#if MULTI_ECS_UPDATE_AT_ONCE
908bf80f4bSopenharmony_ci    engine_->DeferDraw();
918bf80f4bSopenharmony_ci#else
928bf80f4bSopenharmony_ci    engine_->DrawFrame();
938bf80f4bSopenharmony_ci#endif
948bf80f4bSopenharmony_ci    return true;
958bf80f4bSopenharmony_ci}
968bf80f4bSopenharmony_ci
978bf80f4bSopenharmony_cibool WidgetAdapter::UpdateGeometries(const std::vector<std::shared_ptr<Geometry>>& shapes)
988bf80f4bSopenharmony_ci{
998bf80f4bSopenharmony_ci    CHECK_NULL_PTR(engine_);
1008bf80f4bSopenharmony_ci    engine_->UpdateGeometries(shapes);
1018bf80f4bSopenharmony_ci    return true;
1028bf80f4bSopenharmony_ci}
1038bf80f4bSopenharmony_ci
1048bf80f4bSopenharmony_cibool WidgetAdapter::UpdateGLTFAnimations(const std::vector<std::shared_ptr<GLTFAnimation>>&
1058bf80f4bSopenharmony_ci    animations)
1068bf80f4bSopenharmony_ci{
1078bf80f4bSopenharmony_ci    CHECK_NULL_PTR(engine_);
1088bf80f4bSopenharmony_ci    engine_->UpdateGLTFAnimations(animations);
1098bf80f4bSopenharmony_ci    return true;
1108bf80f4bSopenharmony_ci}
1118bf80f4bSopenharmony_ci
1128bf80f4bSopenharmony_ci
1138bf80f4bSopenharmony_cibool WidgetAdapter::UpdateCustomRender(const std::shared_ptr<CustomRenderDescriptor>&
1148bf80f4bSopenharmony_ci    customRenders)
1158bf80f4bSopenharmony_ci{
1168bf80f4bSopenharmony_ci    CHECK_NULL_PTR(engine_);
1178bf80f4bSopenharmony_ci    engine_->UpdateCustomRender(customRenders);
1188bf80f4bSopenharmony_ci    return true;
1198bf80f4bSopenharmony_ci}
1208bf80f4bSopenharmony_ci
1218bf80f4bSopenharmony_cibool WidgetAdapter::UpdateShaderPath(const std::string& shaderPath)
1228bf80f4bSopenharmony_ci{
1238bf80f4bSopenharmony_ci    CHECK_NULL_PTR(engine_);
1248bf80f4bSopenharmony_ci    auto tempPath = const_cast<std::string&> (shaderPath);
1258bf80f4bSopenharmony_ci    auto index = tempPath.find_last_of("/");
1268bf80f4bSopenharmony_ci    auto strSize = tempPath.size();
1278bf80f4bSopenharmony_ci    if (index != std::string::npos && index != (strSize - 1)) {
1288bf80f4bSopenharmony_ci        auto fileName = tempPath.substr(index + 1);
1298bf80f4bSopenharmony_ci        auto suffixIndex = fileName.find_last_of(".");
1308bf80f4bSopenharmony_ci        if (suffixIndex != std::string::npos) {
1318bf80f4bSopenharmony_ci            tempPath = tempPath.substr(0, index);
1328bf80f4bSopenharmony_ci            auto dirIndex = tempPath.find_last_of("/");
1338bf80f4bSopenharmony_ci            tempPath = (dirIndex != std::string::npos) ? tempPath.substr(0, dirIndex) : tempPath;
1348bf80f4bSopenharmony_ci        }
1358bf80f4bSopenharmony_ci    }
1368bf80f4bSopenharmony_ci    auto shaderPathOut = const_cast<const std::string&> (tempPath);
1378bf80f4bSopenharmony_ci    engine_->UpdateShaderPath(shaderPathOut);
1388bf80f4bSopenharmony_ci    return true;
1398bf80f4bSopenharmony_ci}
1408bf80f4bSopenharmony_ci
1418bf80f4bSopenharmony_cibool WidgetAdapter::UpdateImageTexturePaths(const std::vector<std::string>& imageTextures)
1428bf80f4bSopenharmony_ci{
1438bf80f4bSopenharmony_ci    CHECK_NULL_PTR(engine_);
1448bf80f4bSopenharmony_ci    engine_->UpdateImageTexturePaths(imageTextures);
1458bf80f4bSopenharmony_ci    return true;
1468bf80f4bSopenharmony_ci}
1478bf80f4bSopenharmony_ci
1488bf80f4bSopenharmony_cibool WidgetAdapter::UpdateShaderInputBuffer(const std::shared_ptr<OHOS::Render3D::ShaderInputBuffer>&
1498bf80f4bSopenharmony_ci    shaderInputBuffer)
1508bf80f4bSopenharmony_ci{
1518bf80f4bSopenharmony_ci    CHECK_NULL_PTR(engine_);
1528bf80f4bSopenharmony_ci    engine_->UpdateShaderInputBuffer(shaderInputBuffer);
1538bf80f4bSopenharmony_ci    return true;
1548bf80f4bSopenharmony_ci}
1558bf80f4bSopenharmony_ci
1568bf80f4bSopenharmony_civoid WidgetAdapter::DeInitEngine()
1578bf80f4bSopenharmony_ci{
1588bf80f4bSopenharmony_ci    WIDGET_SCOPED_TRACE("WidgetAdapter::DeInitEngine");
1598bf80f4bSopenharmony_ci    if (engine_ == nullptr) {
1608bf80f4bSopenharmony_ci        return;
1618bf80f4bSopenharmony_ci    }
1628bf80f4bSopenharmony_ci    engine_->DeInitEngine();
1638bf80f4bSopenharmony_ci    engine_.reset();
1648bf80f4bSopenharmony_ci}
1658bf80f4bSopenharmony_ci
1668bf80f4bSopenharmony_cibool WidgetAdapter::SetupCameraViewport(uint32_t width, uint32_t height)
1678bf80f4bSopenharmony_ci{
1688bf80f4bSopenharmony_ci    CHECK_NULL_PTR(engine_);
1698bf80f4bSopenharmony_ci    engine_->SetupCameraViewPort(width, height);
1708bf80f4bSopenharmony_ci    return true;
1718bf80f4bSopenharmony_ci}
1728bf80f4bSopenharmony_ci
1738bf80f4bSopenharmony_cibool WidgetAdapter::OnWindowChange(const TextureInfo& textureInfo)
1748bf80f4bSopenharmony_ci{
1758bf80f4bSopenharmony_ci    CHECK_NULL_PTR(engine_);
1768bf80f4bSopenharmony_ci    engine_->OnWindowChange(textureInfo);
1778bf80f4bSopenharmony_ci    return true;
1788bf80f4bSopenharmony_ci}
1798bf80f4bSopenharmony_ci
1808bf80f4bSopenharmony_cibool WidgetAdapter::LoadSceneModel(const std::string& scene)
1818bf80f4bSopenharmony_ci{
1828bf80f4bSopenharmony_ci    CHECK_NULL_PTR(engine_);
1838bf80f4bSopenharmony_ci    engine_->LoadSceneModel(scene);
1848bf80f4bSopenharmony_ci    return true;
1858bf80f4bSopenharmony_ci}
1868bf80f4bSopenharmony_ci
1878bf80f4bSopenharmony_cibool WidgetAdapter::LoadEnvModel(const std::string& enviroment, BackgroundType type)
1888bf80f4bSopenharmony_ci{
1898bf80f4bSopenharmony_ci    CHECK_NULL_PTR(engine_);
1908bf80f4bSopenharmony_ci    engine_->LoadEnvModel(enviroment, type);
1918bf80f4bSopenharmony_ci    return true;
1928bf80f4bSopenharmony_ci}
1938bf80f4bSopenharmony_ci
1948bf80f4bSopenharmony_cibool WidgetAdapter::UnloadSceneModel()
1958bf80f4bSopenharmony_ci{
1968bf80f4bSopenharmony_ci    CHECK_NULL_PTR(engine_);
1978bf80f4bSopenharmony_ci    engine_->UnloadSceneModel();
1988bf80f4bSopenharmony_ci    return true;
1998bf80f4bSopenharmony_ci}
2008bf80f4bSopenharmony_ci
2018bf80f4bSopenharmony_cibool WidgetAdapter::UnloadEnvModel()
2028bf80f4bSopenharmony_ci{
2038bf80f4bSopenharmony_ci    CHECK_NULL_PTR(engine_);
2048bf80f4bSopenharmony_ci    engine_->UnloadSceneModel();
2058bf80f4bSopenharmony_ci    return true;
2068bf80f4bSopenharmony_ci}
2078bf80f4bSopenharmony_ci
2088bf80f4bSopenharmony_cibool WidgetAdapter::NeedsRepaint()
2098bf80f4bSopenharmony_ci{
2108bf80f4bSopenharmony_ci    CHECK_NULL_PTR(engine_);
2118bf80f4bSopenharmony_ci    return engine_->NeedsRepaint();
2128bf80f4bSopenharmony_ci}
2138bf80f4bSopenharmony_ci} // namespace OHOS::Render3D
214