• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "display_engine/rs_color_temperature.h"
17 
18 #include <dlfcn.h>
19 #include <string_view>
20 
21 #include "common/rs_common_def.h"
22 #include "platform/common/rs_log.h"
23 
24 namespace {
25 constexpr std::string_view EXT_LIB_PATH = "system/lib64/libcct_ext.z.so";
26 static const std::vector<float> DEFAULT_MATRIX = {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f};
27 }
28 
29 namespace OHOS {
30 namespace Rosen {
Get()31 RSColorTemperature& RSColorTemperature::Get()
32 {
33     static RSColorTemperature instance;
34     return instance;
35 }
36 
~RSColorTemperature()37 RSColorTemperature::~RSColorTemperature()
38 {
39     if (initStatus_ && destroy_ != nullptr) {
40         destroy_();
41     }
42     CloseLibrary();
43 }
44 
CloseLibrary()45 void RSColorTemperature::CloseLibrary()
46 {
47     if (extLibHandle_ != nullptr) {
48         dlclose(extLibHandle_);
49         extLibHandle_ = nullptr;
50     }
51     initStatus_ = false;
52     create_ = nullptr;
53     destroy_ = nullptr;
54 }
55 
Init()56 void RSColorTemperature::Init()
57 {
58     initStatus_ = LoadLibrary();
59     if (!initStatus_) {
60         CloseLibrary();
61     }
62     if (create_ != nullptr) {
63         rSColorTemperatureInterface_ = create_();
64         if (rSColorTemperatureInterface_ == nullptr) {
65             CloseLibrary();
66         }
67     }
68 }
69 
LoadLibrary()70 bool RSColorTemperature::LoadLibrary()
71 {
72     if (UNLIKELY(extLibHandle_ != nullptr)) {
73         return false;
74     }
75     extLibHandle_ = dlopen(EXT_LIB_PATH.data(), RTLD_NOW);
76     if (extLibHandle_ == nullptr) {
77         RS_LOGI("ColorTemp dlopen error:%{public}s", dlerror());
78         return false;
79     }
80     create_ = reinterpret_cast<CreateFunc>(dlsym(extLibHandle_, "Create"));
81     if (create_ == nullptr) {
82         RS_LOGE("ColorTemp link create error!");
83         return false;
84     }
85     destroy_ = reinterpret_cast<DestroyFunc>(dlsym(extLibHandle_, "Destroy"));
86     if (destroy_ == nullptr) {
87         RS_LOGE("ColorTemp link destroy error!");
88         return false;
89     }
90     RS_LOGI("ColorTemp LoadLibrary success");
91     return true;
92 }
93 
RegisterRefresh(std::function<void ()> && refreshFunc)94 void RSColorTemperature::RegisterRefresh(std::function<void()>&& refreshFunc)
95 {
96     if (rSColorTemperatureInterface_ != nullptr) {
97         rSColorTemperatureInterface_->RegisterRefresh(std::forward<std::function<void()>>(refreshFunc));
98     }
99 }
100 
UpdateScreenStatus(ScreenId screenId,ScreenPowerStatus powerStatus)101 void RSColorTemperature::UpdateScreenStatus(ScreenId screenId, ScreenPowerStatus powerStatus)
102 {
103     if (rSColorTemperatureInterface_ != nullptr) {
104         rSColorTemperatureInterface_->UpdateScreenStatus(screenId, powerStatus);
105     }
106 }
107 
IsDimmingOn(ScreenId screenId)108 bool RSColorTemperature::IsDimmingOn(ScreenId screenId)
109 {
110     return (rSColorTemperatureInterface_ != nullptr) ?
111         rSColorTemperatureInterface_->IsDimmingOn(screenId) : false;
112 }
113 
DimmingIncrease(ScreenId screenId)114 void RSColorTemperature::DimmingIncrease(ScreenId screenId)
115 {
116     if (rSColorTemperatureInterface_ != nullptr) {
117         rSColorTemperatureInterface_->DimmingIncrease(screenId);
118     }
119 }
120 
IsColorTemperatureOn() const121 bool RSColorTemperature::IsColorTemperatureOn() const
122 {
123     return (rSColorTemperatureInterface_ != nullptr) ?
124         rSColorTemperatureInterface_->IsColorTemperatureOn() : false;
125 }
126 
GetNewLinearCct(ScreenId screenId)127 std::vector<float> RSColorTemperature::GetNewLinearCct(ScreenId screenId)
128 {
129     return (rSColorTemperatureInterface_ != nullptr) ?
130         rSColorTemperatureInterface_->GetNewLinearCct(screenId) : DEFAULT_MATRIX;
131 }
132 
GetLayerLinearCct(ScreenId screenId,const std::vector<uint8_t> & dynamicMetadata,const CM_Matrix srcColorMatrix)133 std::vector<float> RSColorTemperature::GetLayerLinearCct(ScreenId screenId,
134     const std::vector<uint8_t>& dynamicMetadata, const CM_Matrix srcColorMatrix)
135 {
136     return (rSColorTemperatureInterface_ != nullptr) ?
137         rSColorTemperatureInterface_->GetLayerLinearCct(screenId,
138             dynamicMetadata, srcColorMatrix) : DEFAULT_MATRIX;
139 }
140 } // Rosen
141 } // OHOS