1/*
2 * Copyright (c) 2023 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#include "display_lite.h"
16
17#include <cstdint>
18#include <new>
19#include <refbase.h>
20
21#include "class_var_definition.h"
22#include "display_info.h"
23#include "display_manager_adapter_lite.h"
24#include "dm_common.h"
25#include "singleton_container.h"
26#include "window_manager_hilog.h"
27
28namespace OHOS::Rosen {
29namespace {
30constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_DISPLAY, "DisplayLite"};
31}
32class DisplayLite::Impl : public RefBase {
33public:
34    Impl(const std::string& name, sptr<DisplayInfo> info)
35    {
36        displayInfo_ = info;
37        name_= name;
38    }
39    ~Impl() = default;
40    DEFINE_VAR_FUNC_GET_SET_WITH_LOCK(sptr<DisplayInfo>, DisplayInfo, displayInfo);
41    DEFINE_VAR_FUNC_GET_SET(std::string, Name, name);
42};
43
44DisplayLite::DisplayLite(const std::string& name, sptr<DisplayInfo> info)
45    : pImpl_(new Impl(name, info))
46{
47}
48
49DisplayLite::~DisplayLite()
50{
51}
52
53DisplayId DisplayLite::GetId() const
54{
55    if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
56        WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
57        return DisplayId(0);
58    }
59    return pImpl_->GetDisplayInfo()->GetDisplayId();
60}
61
62sptr<DisplayInfo> DisplayLite::GetDisplayInfo() const
63{
64    UpdateDisplayInfo();
65    if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
66        WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
67        return nullptr;
68    }
69    return pImpl_->GetDisplayInfo();
70}
71
72int32_t DisplayLite::GetWidth() const
73{
74    UpdateDisplayInfo();
75    if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
76        WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
77        return 0;
78    }
79    return pImpl_->GetDisplayInfo()->GetWidth();
80}
81
82int32_t DisplayLite::GetHeight() const
83{
84    UpdateDisplayInfo();
85    if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
86        WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
87        return 0;
88    }
89    return pImpl_->GetDisplayInfo()->GetHeight();
90}
91
92sptr<CutoutInfo> DisplayLite::GetCutoutInfo() const
93{
94    return SingletonContainer::Get<DisplayManagerAdapterLite>().GetCutoutInfo(GetId());
95}
96
97void DisplayLite::UpdateDisplayInfo() const
98{
99    auto displayInfo = SingletonContainer::Get<DisplayManagerAdapterLite>().GetDisplayInfo(GetId());
100    UpdateDisplayInfo(displayInfo);
101}
102
103void DisplayLite::UpdateDisplayInfo(sptr<DisplayInfo> displayInfo) const
104{
105    if (displayInfo == nullptr) {
106        WLOGFE("displayInfo is nullptr");
107        return;
108    }
109    if (pImpl_ == nullptr) {
110        WLOGFE("pImpl_ is nullptr");
111        return;
112    }
113    pImpl_->SetDisplayInfo(displayInfo);
114}
115
116Rotation DisplayLite::GetRotation() const
117{
118    UpdateDisplayInfo();
119    if (pImpl_ == nullptr || pImpl_->GetDisplayInfo() == nullptr) {
120        WLOGFE("pImpl_ or pImpl_->GetDisplayInfo is nullptr");
121        return Rotation::ROTATION_0;
122    }
123    return pImpl_->GetDisplayInfo()->GetRotation();
124}
125} // namespace OHOS::Rosen
126