/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "js_display.h" #include #include #include "display.h" #include "display_info.h" #include "window_manager_hilog.h" namespace OHOS { namespace Rosen { using namespace AbilityRuntime; namespace { constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0, "JsDisplay"}; } static thread_local std::map> g_JsDisplayMap; std::recursive_mutex g_mutex; JsDisplay::JsDisplay(const sptr& display) : display_(display) { } JsDisplay::~JsDisplay() { WLOGFI("JsDisplay::~JsDisplay is called"); } void JsDisplay::Finalizer(NativeEngine* engine, void* data, void* hint) { WLOGFI("JsDisplay::Finalizer is called"); auto jsDisplay = std::unique_ptr(static_cast(data)); if (jsDisplay == nullptr) { WLOGFE("JsDisplay::Finalizer jsDisplay is null"); return; } sptr display = jsDisplay->display_; if (display == nullptr) { WLOGFE("JsDisplay::Finalizer display is null"); return; } DisplayId displayId = display->GetId(); WLOGFI("JsDisplay::Finalizer displayId : %{public}" PRIu64"", displayId); std::lock_guard lock(g_mutex); if (g_JsDisplayMap.find(displayId) != g_JsDisplayMap.end()) { WLOGFI("JsDisplay::Finalizer Display is destroyed: %{public}" PRIu64"", displayId); g_JsDisplayMap.erase(displayId); } } std::shared_ptr FindJsDisplayObject(DisplayId displayId) { WLOGFI("[NAPI]Try to find display %{public}" PRIu64" in g_JsDisplayMap", displayId); std::lock_guard lock(g_mutex); if (g_JsDisplayMap.find(displayId) == g_JsDisplayMap.end()) { WLOGFI("[NAPI]Can not find display %{public}" PRIu64" in g_JsDisplayMap", displayId); return nullptr; } return g_JsDisplayMap[displayId]; } NativeValue* CreateJsDisplayObject(NativeEngine& engine, sptr& display) { WLOGFI("JsDisplay::CreateJsDisplay is called"); NativeValue* objValue = nullptr; std::shared_ptr jsDisplayObj = FindJsDisplayObject(display->GetId()); if (jsDisplayObj != nullptr && jsDisplayObj->Get() != nullptr) { WLOGFI("[NAPI]FindJsDisplayObject %{public}" PRIu64"", display->GetId()); objValue = jsDisplayObj->Get(); } if (objValue == nullptr) { objValue = engine.CreateObject(); } NativeObject* object = ConvertNativeValueTo(objValue); if (object == nullptr) { WLOGFE("Failed to convert prop to jsObject"); return engine.CreateUndefined(); } std::unique_ptr jsDisplay = std::make_unique(display); object->SetNativePointer(jsDisplay.release(), JsDisplay::Finalizer, nullptr); auto info = display->GetDisplayInfo(); if (info == nullptr) { WLOGFE("Failed to GetDisplayInfo"); return engine.CreateUndefined(); } object->SetProperty("id", CreateJsValue(engine, static_cast(info->GetDisplayId()))); object->SetProperty("width", CreateJsValue(engine, info->GetWidth())); object->SetProperty("height", CreateJsValue(engine, info->GetHeight())); object->SetProperty("refreshRate", CreateJsValue(engine, info->GetRefreshRate())); object->SetProperty("name", engine.CreateUndefined()); object->SetProperty("alive", engine.CreateUndefined()); object->SetProperty("state", engine.CreateUndefined()); object->SetProperty("rotation", CreateJsValue(engine, info->GetRotation())); object->SetProperty("densityDPI", CreateJsValue(engine, info->GetVirtualPixelRatio() * DOT_PER_INCH)); object->SetProperty("densityPixels", engine.CreateUndefined()); object->SetProperty("scaledDensity", engine.CreateUndefined()); object->SetProperty("xDPI", engine.CreateUndefined()); object->SetProperty("yDPI", engine.CreateUndefined()); if (jsDisplayObj == nullptr || jsDisplayObj->Get() == nullptr) { std::shared_ptr jsDisplayRef; jsDisplayRef.reset(engine.CreateReference(objValue, 1)); DisplayId displayId = display->GetId(); std::lock_guard lock(g_mutex); g_JsDisplayMap[displayId] = jsDisplayRef; } return objValue; } } // namespace Rosen } // namespace OHOS