1 /* Copyright (c) 2015 - 2019, The Linux Foundataion. All rights reserved.
2 *
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are
5 * met:
6 *     * Redistributions of source code must retain the above copyright
7 *       notice, this list of conditions and the following disclaimer.
8 *     * Redistributions in binary form must reproduce the above
9 *       copyright notice, this list of conditions and the following
10 *       disclaimer in the documentation and/or other materials provided
11 *       with the distribution.
12 *     * Neither the name of The Linux Foundation nor the names of its
13 *       contributors may be used to endorse or promote products derived
14 *       from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29 
30 #include <dlfcn.h>
31 #include <private/color_interface.h>
32 #include <utils/constants.h>
33 #include <utils/debug.h>
34 #include "color_manager.h"
35 
36 #define __CLASS__ "ColorManager"
37 
38 namespace sdm {
39 
40 DynLib ColorManagerProxy::color_lib_;
41 CreateColorInterface ColorManagerProxy::create_intf_ = NULL;
42 DestroyColorInterface ColorManagerProxy::destroy_intf_ = NULL;
43 HWResourceInfo ColorManagerProxy::hw_res_info_;
44 
45 // Below two functions are part of concrete implementation for SDM core private
46 // color_params.h
Reset()47 void PPFeaturesConfig::Reset() {
48   for (int i = 0; i < kMaxNumPPFeatures; i++) {
49     if (feature_[i]) {
50       delete feature_[i];
51       feature_[i] = NULL;
52     }
53   }
54   dirty_ = false;
55   next_idx_ = 0;
56 }
57 
RetrieveNextFeature(PPFeatureInfo ** feature)58 DisplayError PPFeaturesConfig::RetrieveNextFeature(PPFeatureInfo **feature) {
59   DisplayError ret = kErrorNone;
60   uint32_t i(0);
61 
62   for (i = next_idx_; i < kMaxNumPPFeatures; i++) {
63     if (feature_[i]) {
64       *feature = feature_[i];
65       next_idx_ = i + 1;
66       break;
67     }
68   }
69 
70   if (i == kMaxNumPPFeatures) {
71     ret = kErrorParameters;
72     next_idx_ = 0;
73   }
74 
75   return ret;
76 }
77 
Init(const HWResourceInfo & hw_res_info)78 DisplayError ColorManagerProxy::Init(const HWResourceInfo &hw_res_info) {
79   DisplayError error = kErrorNone;
80 
81   // Load color service library and retrieve its entry points.
82   if (color_lib_.Open(COLORMGR_LIBRARY_NAME)) {
83     if (!color_lib_.Sym(CREATE_COLOR_INTERFACE_NAME, reinterpret_cast<void **>(&create_intf_)) ||
84         !color_lib_.Sym(DESTROY_COLOR_INTERFACE_NAME, reinterpret_cast<void **>(&destroy_intf_))) {
85       DLOGW("Fail to retrieve = %s from %s", CREATE_COLOR_INTERFACE_NAME, COLORMGR_LIBRARY_NAME);
86       error = kErrorResources;
87     }
88   } else {
89     DLOGW("Fail to load = %s", COLORMGR_LIBRARY_NAME);
90     error = kErrorResources;
91   }
92 
93   hw_res_info_ = hw_res_info;
94 
95   return error;
96 }
97 
Deinit()98 void ColorManagerProxy::Deinit() {
99   color_lib_.~DynLib();
100 }
101 
ColorManagerProxy(int32_t id,DisplayType type,HWInterface * intf,const HWDisplayAttributes & attr,const HWPanelInfo & info)102 ColorManagerProxy::ColorManagerProxy(int32_t id, DisplayType type, HWInterface *intf,
103                                      const HWDisplayAttributes &attr,
104                                      const HWPanelInfo &info)
105     : display_id_(id), device_type_(type), pp_hw_attributes_(), hw_intf_(intf),
106       color_intf_(NULL), pp_features_() {}
107 
CreateColorManagerProxy(DisplayType type,HWInterface * hw_intf,const HWDisplayAttributes & attribute,const HWPanelInfo & panel_info)108 ColorManagerProxy *ColorManagerProxy::CreateColorManagerProxy(DisplayType type,
109                                                               HWInterface *hw_intf,
110                                                               const HWDisplayAttributes &attribute,
111                                                               const HWPanelInfo &panel_info) {
112   DisplayError error = kErrorNone;
113   PPFeatureVersion versions;
114   int32_t display_id = -1;
115   ColorManagerProxy *color_manager_proxy = NULL;
116 
117   // check if all resources are available before invoking factory method from libsdm-color.so.
118   if (!color_lib_ || !create_intf_ || !destroy_intf_) {
119     DLOGW("Information for %s isn't available!", COLORMGR_LIBRARY_NAME);
120     return NULL;
121   }
122 
123   hw_intf->GetDisplayId(&display_id);
124   color_manager_proxy = new ColorManagerProxy(display_id, type, hw_intf, attribute, panel_info);
125 
126   if (color_manager_proxy) {
127     // 1. need query post-processing feature version from HWInterface.
128     error = color_manager_proxy->hw_intf_->GetPPFeaturesVersion(&versions);
129     PPHWAttributes &hw_attr = color_manager_proxy->pp_hw_attributes_;
130     if (error != kErrorNone) {
131       DLOGW("Fail to get DSPP feature versions");
132     } else {
133       hw_attr.Set(hw_res_info_, panel_info, attribute, versions);
134       DLOGI("PAV2 version is versions = %d, version = %d ",
135             hw_attr.version.version[kGlobalColorFeaturePaV2],
136             versions.version[kGlobalColorFeaturePaV2]);
137     }
138 
139     // 2. instantiate concrete ColorInterface from libsdm-color.so, pass all hardware info in.
140     error = create_intf_(COLOR_VERSION_TAG, color_manager_proxy->display_id_,
141                          color_manager_proxy->device_type_, hw_attr,
142                          &color_manager_proxy->color_intf_);
143     if (error != kErrorNone) {
144       DLOGW("Unable to instantiate concrete ColorInterface from %s", COLORMGR_LIBRARY_NAME);
145       delete color_manager_proxy;
146       color_manager_proxy = NULL;
147     }
148   }
149 
150   return color_manager_proxy;
151 }
152 
~ColorManagerProxy()153 ColorManagerProxy::~ColorManagerProxy() {
154   if (destroy_intf_)
155     destroy_intf_(display_id_);
156   color_intf_ = NULL;
157 }
158 
ColorSVCRequestRoute(const PPDisplayAPIPayload & in_payload,PPDisplayAPIPayload * out_payload,PPPendingParams * pending_action)159 DisplayError ColorManagerProxy::ColorSVCRequestRoute(const PPDisplayAPIPayload &in_payload,
160                                                      PPDisplayAPIPayload *out_payload,
161                                                      PPPendingParams *pending_action) {
162   DisplayError ret = kErrorNone;
163 
164   // On completion, dspp_features_ will be populated and mark dirty with all resolved dspp
165   // feature list with paramaters being transformed into target requirement.
166   ret = color_intf_->ColorSVCRequestRoute(in_payload, out_payload, &pp_features_, pending_action);
167 
168   return ret;
169 }
170 
NeedsPartialUpdateDisable()171 bool ColorManagerProxy::NeedsPartialUpdateDisable() {
172   Locker &locker(pp_features_.GetLocker());
173   SCOPE_LOCK(locker);
174 
175   return pp_features_.IsDirty();
176 }
177 
Commit()178 DisplayError ColorManagerProxy::Commit() {
179 
180   Locker &locker(pp_features_.GetLocker());
181   SCOPE_LOCK(locker);
182 
183   DisplayError ret = kErrorNone;
184   if (pp_features_.IsDirty()) {
185     ret = hw_intf_->SetPPFeatures(&pp_features_);
186   }
187 
188   return ret;
189 }
190 
Set(const HWResourceInfo & hw_res,const HWPanelInfo & panel_info,const DisplayConfigVariableInfo & attr,const PPFeatureVersion & feature_ver)191 void PPHWAttributes::Set(const HWResourceInfo &hw_res,
192                          const HWPanelInfo &panel_info,
193                          const DisplayConfigVariableInfo &attr,
194                          const PPFeatureVersion &feature_ver) {
195   HWResourceInfo &res = *this;
196   res = hw_res;
197   HWPanelInfo &panel = *this;
198   panel = panel_info;
199   DisplayConfigVariableInfo &attributes = *this;
200   attributes = attr;
201   version = feature_ver;
202   panel_max_brightness = panel_info.panel_max_brightness;
203 
204   if (strlen(panel_info.panel_name)) {
205     snprintf(&panel_name[0], sizeof(panel_name), "%s", &panel_info.panel_name[0]);
206     char *tmp = panel_name;
207     while ((tmp = strstr(tmp, " ")) != NULL)
208       *tmp = '_';
209     if ((tmp = strstr(panel_name, "\n")) != NULL)
210       *tmp = '\0';
211   }
212 }
213 
ColorMgrGetNumOfModes(uint32_t * mode_cnt)214 DisplayError ColorManagerProxy::ColorMgrGetNumOfModes(uint32_t *mode_cnt) {
215   return color_intf_->ColorIntfGetNumDisplayModes(&pp_features_, 0, mode_cnt);
216 }
217 
ColorMgrGetModes(uint32_t * mode_cnt,SDEDisplayMode * modes)218 DisplayError ColorManagerProxy::ColorMgrGetModes(uint32_t *mode_cnt,
219                                                  SDEDisplayMode *modes) {
220   return color_intf_->ColorIntfEnumerateDisplayModes(&pp_features_, 0, modes, mode_cnt);
221 }
222 
ColorMgrSetMode(int32_t color_mode_id)223 DisplayError ColorManagerProxy::ColorMgrSetMode(int32_t color_mode_id) {
224   return color_intf_->ColorIntfSetDisplayMode(&pp_features_, 0, color_mode_id);
225 }
226 
ColorMgrGetModeInfo(int32_t mode_id,AttrVal * query)227 DisplayError ColorManagerProxy::ColorMgrGetModeInfo(int32_t mode_id, AttrVal *query) {
228   return color_intf_->ColorIntfGetModeInfo(&pp_features_, 0, mode_id, query);
229 }
230 
ColorMgrSetColorTransform(uint32_t length,const double * trans_data)231 DisplayError ColorManagerProxy::ColorMgrSetColorTransform(uint32_t length,
232                                                           const double *trans_data) {
233   return color_intf_->ColorIntfSetColorTransform(&pp_features_, 0, length, trans_data);
234 }
235 
ColorMgrGetDefaultModeID(int32_t * mode_id)236 DisplayError ColorManagerProxy::ColorMgrGetDefaultModeID(int32_t *mode_id) {
237   return color_intf_->ColorIntfGetDefaultModeID(&pp_features_, 0, mode_id);
238 }
239 
ColorMgrCombineColorModes()240 DisplayError ColorManagerProxy::ColorMgrCombineColorModes() {
241   return color_intf_->ColorIntfCombineColorModes();
242 }
243 
244 }  // namespace sdm
245