• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "ExynosPrimaryDisplayModule.h"
18 
19 #include <android-base/file.h>
20 #include <json/reader.h>
21 #include <json/value.h>
22 
23 #include <cmath>
24 
25 #include "BrightnessController.h"
26 #include "ExynosDisplayDrmInterfaceModule.h"
27 #include "ExynosHWCDebug.h"
28 
29 #ifdef FORCE_GPU_COMPOSITION
30 extern exynos_hwc_control exynosHWCControl;
31 #endif
32 
33 using namespace gs101;
34 
getMPPTypeFromDPPChannel(uint32_t channel)35 mpp_phycal_type_t getMPPTypeFromDPPChannel(uint32_t channel) {
36 
37     for (int i=0; i < MAX_DECON_DMA_TYPE; i++){
38         if(idma_channel_map[i].channel == channel)
39             return idma_channel_map[i].type;
40     }
41 
42     return MPP_P_TYPE_MAX;
43 }
44 
45 // enable map layerDataMappingInfo comparison in needDisplayColorSetting()
operator ==(const ExynosPrimaryDisplayModule::DisplaySceneInfo::LayerMappingInfo & lm1,const ExynosPrimaryDisplayModule::DisplaySceneInfo::LayerMappingInfo & lm2)46 inline bool operator==(const ExynosPrimaryDisplayModule::DisplaySceneInfo::LayerMappingInfo &lm1,
47                        const ExynosPrimaryDisplayModule::DisplaySceneInfo::LayerMappingInfo &lm2) {
48     return lm1.dppIdx == lm2.dppIdx && lm1.planeId == lm2.planeId;
49 }
50 
ExynosPrimaryDisplayModule(uint32_t index,ExynosDevice * device,const std::string & displayName)51 ExynosPrimaryDisplayModule::ExynosPrimaryDisplayModule(uint32_t index, ExynosDevice* device,
52                                                        const std::string& displayName)
53       : ExynosPrimaryDisplay(index, device, displayName) {
54 #ifdef FORCE_GPU_COMPOSITION
55     exynosHWCControl.forceGpu = true;
56 #endif
57 }
58 
~ExynosPrimaryDisplayModule()59 ExynosPrimaryDisplayModule::~ExynosPrimaryDisplayModule () {
60 }
61 
usePreDefinedWindow(bool use)62 void ExynosPrimaryDisplayModule::usePreDefinedWindow(bool use)
63 {
64 #ifdef FIX_BASE_WINDOW_INDEX
65     /* Use fixed base window index */
66     mBaseWindowIndex = FIX_BASE_WINDOW_INDEX;
67     return;
68 #endif
69 
70     if (use) {
71         mBaseWindowIndex = PRIMARY_DISP_BASE_WIN[mDevice->mDisplayMode];
72         mMaxWindowNum = mDisplayInterface->getMaxWindowNum() - PRIMARY_DISP_BASE_WIN[mDevice->mDisplayMode];
73     } else {
74         mBaseWindowIndex = 0;
75         mMaxWindowNum = mDisplayInterface->getMaxWindowNum();
76     }
77 }
78 
validateWinConfigData()79 int32_t ExynosPrimaryDisplayModule::validateWinConfigData()
80 {
81     bool flagValidConfig = true;
82 
83     if (ExynosDisplay::validateWinConfigData() != NO_ERROR)
84         flagValidConfig = false;
85 
86     for (size_t i = 0; i < mDpuData.configs.size(); i++) {
87         struct exynos_win_config_data &config = mDpuData.configs[i];
88         if (config.state == config.WIN_STATE_BUFFER) {
89             bool configInvalid = false;
90             uint32_t mppType = config.assignedMPP->mPhysicalType;
91             if ((config.src.w != config.dst.w) ||
92                 (config.src.h != config.dst.h)) {
93                 if ((mppType == MPP_DPP_GF) ||
94                     (mppType == MPP_DPP_VG) ||
95                     (mppType == MPP_DPP_VGF)) {
96                     DISPLAY_LOGE("WIN_CONFIG error: invalid assign id : "
97                             "%zu,  s_w : %d, d_w : %d, s_h : %d, d_h : %d, mppType : %d",
98                             i, config.src.w, config.dst.w, config.src.h, config.dst.h, mppType);
99                     configInvalid = true;
100                 }
101             }
102             if (configInvalid) {
103                 config.state = config.WIN_STATE_DISABLED;
104                 flagValidConfig = false;
105             }
106         }
107     }
108     if (flagValidConfig)
109         return NO_ERROR;
110     else
111         return -EINVAL;
112 }
113 
doPreProcessing()114 void ExynosPrimaryDisplayModule::doPreProcessing() {
115     ExynosDisplay::doPreProcessing();
116 
117     if (mDevice->checkNonInternalConnection()) {
118         mDisplayControl.adjustDisplayFrame = true;
119     } else {
120         mDisplayControl.adjustDisplayFrame = false;
121     }
122 }
123 
getColorModes(uint32_t * outNumModes,int32_t * outModes)124 int32_t ExynosPrimaryDisplayModule::getColorModes(
125         uint32_t* outNumModes, int32_t* outModes)
126 {
127     GsInterfaceType* displayColorInterface = getDisplayColorInterface();
128     const DisplayType display = getDisplayTypeFromIndex(mIndex);
129     const ColorModesMap colorModeMap = displayColorInterface == nullptr
130             ? ColorModesMap()
131             : displayColorInterface->ColorModesAndRenderIntents(display);
132     ALOGD("%s: size(%zu)", __func__, colorModeMap.size());
133     if (outModes == nullptr) {
134         *outNumModes = colorModeMap.size();
135         return HWC2_ERROR_NONE;
136     }
137     if (*outNumModes != colorModeMap.size()) {
138         DISPLAY_LOGE("%s: Invalid color mode size(%d), It should be(%zu)",
139                 __func__, *outNumModes, colorModeMap.size());
140         return HWC2_ERROR_BAD_PARAMETER;
141     }
142 
143     uint32_t index = 0;
144     for (const auto &it : colorModeMap)
145     {
146         outModes[index] = static_cast<int32_t>(it.first);
147         ALOGD("\tmode[%d] %d", index, outModes[index]);
148         index++;
149     }
150 
151     return HWC2_ERROR_NONE;
152 }
153 
setColorMode(int32_t mode)154 int32_t ExynosPrimaryDisplayModule::setColorMode(int32_t mode)
155 {
156     ALOGD("%s: mode(%d)", __func__, mode);
157     GsInterfaceType* displayColorInterface = getDisplayColorInterface();
158     const DisplayType display = getDisplayTypeFromIndex(mIndex);
159     const ColorModesMap colorModeMap = displayColorInterface == nullptr
160             ? ColorModesMap()
161             : displayColorInterface->ColorModesAndRenderIntents(display);
162     hwc::ColorMode colorMode =
163         static_cast<hwc::ColorMode>(mode);
164     const auto it = colorModeMap.find(colorMode);
165     if (it == colorModeMap.end()) {
166         DISPLAY_LOGE("%s: Invalid color mode(%d)", __func__, mode);
167         return HWC2_ERROR_BAD_PARAMETER;
168     }
169     mDisplaySceneInfo.setColorMode(colorMode);
170 
171     if (mColorMode != mode)
172         setGeometryChanged(GEOMETRY_DISPLAY_COLOR_MODE_CHANGED);
173     mColorMode = (android_color_mode_t)mode;
174 
175     return HWC2_ERROR_NONE;
176 }
177 
getRenderIntents(int32_t mode,uint32_t * outNumIntents,int32_t * outIntents)178 int32_t ExynosPrimaryDisplayModule::getRenderIntents(int32_t mode,
179         uint32_t* outNumIntents, int32_t* outIntents)
180 {
181     GsInterfaceType* displayColorInterface = getDisplayColorInterface();
182     const DisplayType display = getDisplayTypeFromIndex(mIndex);
183     const ColorModesMap colorModeMap = displayColorInterface == nullptr
184             ? ColorModesMap()
185             : displayColorInterface->ColorModesAndRenderIntents(display);
186     ALOGD("%s, size(%zu)", __func__, colorModeMap.size());
187     hwc::ColorMode colorMode =
188         static_cast<hwc::ColorMode>(mode);
189     const auto it = colorModeMap.find(colorMode);
190     if (it == colorModeMap.end()) {
191         DISPLAY_LOGE("%s: Invalid color mode(%d)", __func__, mode);
192         return HWC2_ERROR_BAD_PARAMETER;
193     }
194     auto &renderIntents = it->second;
195     if (outIntents == NULL) {
196         *outNumIntents = renderIntents.size();
197         ALOGD("\tintent num(%zu)", renderIntents.size());
198         return HWC2_ERROR_NONE;
199     }
200     if (*outNumIntents != renderIntents.size()) {
201         DISPLAY_LOGE("%s: Invalid intent size(%d), It should be(%zu)",
202                 __func__, *outNumIntents, renderIntents.size());
203         return HWC2_ERROR_BAD_PARAMETER;
204     }
205 
206     for (uint32_t i = 0; i < renderIntents.size(); i++)
207     {
208         outIntents[i] = static_cast<uint32_t>(renderIntents[i]);
209         ALOGD("\tintent[%d] %d", i, outIntents[i]);
210     }
211 
212     return HWC2_ERROR_NONE;
213 }
214 
setColorModeWithRenderIntent(int32_t mode,int32_t intent)215 int32_t ExynosPrimaryDisplayModule::setColorModeWithRenderIntent(int32_t mode,
216         int32_t intent)
217 {
218     GsInterfaceType* displayColorInterface = getDisplayColorInterface();
219     const DisplayType display = getDisplayTypeFromIndex(mIndex);
220     const ColorModesMap colorModeMap = displayColorInterface == nullptr
221             ? ColorModesMap()
222             : displayColorInterface->ColorModesAndRenderIntents(display);
223     hwc::ColorMode colorMode =
224         static_cast<hwc::ColorMode>(mode);
225     hwc::RenderIntent renderIntent =
226         static_cast<hwc::RenderIntent>(intent);
227 
228     const auto mode_it = colorModeMap.find(colorMode);
229     if (mode_it == colorModeMap.end()) {
230         DISPLAY_LOGE("%s: Invalid color mode(%d)", __func__, mode);
231         return HWC2_ERROR_BAD_PARAMETER;
232     }
233 
234     auto &renderIntents = mode_it->second;
235     auto intent_it = std::find(renderIntents.begin(),
236             renderIntents.end(), renderIntent);
237     if (intent_it == renderIntents.end()) {
238         DISPLAY_LOGE("%s: Invalid render intent(%d)", __func__, intent);
239         return HWC2_ERROR_BAD_PARAMETER;
240     }
241 
242     mDisplaySceneInfo.setColorMode(colorMode);
243     mDisplaySceneInfo.setRenderIntent(renderIntent);
244 
245     if (mColorMode != mode) {
246         ALOGD("%s: mode(%d), intent(%d)", __func__, mode, intent);
247         setGeometryChanged(GEOMETRY_DISPLAY_COLOR_MODE_CHANGED);
248     }
249     mColorMode = (android_color_mode_t)mode;
250 
251     mBrightnessController->updateColorRenderIntent(intent);
252 
253     return HWC2_ERROR_NONE;
254 }
255 
setColorTransform(const float * matrix,int32_t hint)256 int32_t ExynosPrimaryDisplayModule::setColorTransform(
257         const float* matrix, int32_t hint)
258 {
259     if ((hint < HAL_COLOR_TRANSFORM_IDENTITY) ||
260         (hint > HAL_COLOR_TRANSFORM_CORRECT_TRITANOPIA))
261         return HWC2_ERROR_BAD_PARAMETER;
262     if (mColorTransformHint != hint) {
263         ALOGI("%s:: %d -> %d", __func__, mColorTransformHint, hint);
264         setGeometryChanged(GEOMETRY_DISPLAY_COLOR_TRANSFORM_CHANGED);
265     }
266     mColorTransformHint = hint;
267 #ifdef HWC_SUPPORT_COLOR_TRANSFORM
268     mDisplaySceneInfo.setColorTransform(matrix);
269 #endif
270     return HWC2_ERROR_NONE;
271 
272 }
273 
getClientTargetProperty(hwc_client_target_property_t * outClientTargetProperty,HwcDimmingStage * outDimmingStage)274 int32_t ExynosPrimaryDisplayModule::getClientTargetProperty(
275         hwc_client_target_property_t* outClientTargetProperty,
276         HwcDimmingStage *outDimmingStage) {
277     GsInterfaceType* displayColorInterface = getDisplayColorInterface();
278     if (displayColorInterface == nullptr) {
279         ALOGI("%s dc interface not created", __func__);
280         return ExynosDisplay::getClientTargetProperty(outClientTargetProperty);
281     }
282 
283     const DisplayType display = getDisplayTypeFromIndex(mIndex);
284     hwc::PixelFormat pixelFormat;
285     hwc::Dataspace dataspace;
286     bool dimming_linear;
287     if (!displayColorInterface->GetBlendingProperty(display, pixelFormat, dataspace,
288                                                     dimming_linear)) {
289         outClientTargetProperty->pixelFormat = toUnderlying(pixelFormat);
290         outClientTargetProperty->dataspace = toUnderlying(dataspace);
291         if (outDimmingStage != nullptr)
292             *outDimmingStage = dimming_linear
293                               ? HwcDimmingStage::DIMMING_LINEAR
294                               : HwcDimmingStage::DIMMING_OETF;
295 
296         return HWC2_ERROR_NONE;
297     }
298 
299     ALOGW("%s failed to get property of blending stage", __func__);
300     return ExynosDisplay::getClientTargetProperty(outClientTargetProperty);
301 }
302 
updateBrightnessTable()303 int32_t ExynosPrimaryDisplayModule::updateBrightnessTable() {
304     const IBrightnessTable* table = nullptr;
305     auto displayColorInterface = getDisplayColorInterface();
306     if (displayColorInterface == nullptr) {
307         ALOGE("%s displaycolor interface not available!", __func__);
308         return HWC2_ERROR_NO_RESOURCES;
309     }
310 
311     auto displayType = getBuiltInDisplayType();
312     auto ret = displayColorInterface->GetBrightnessTable(displayType, table);
313     if (ret != android::OK) {
314         ALOGE("%s brightness table not available!", __func__);
315         return HWC2_ERROR_NO_RESOURCES;
316     }
317     // BrightnessController is not ready until this step
318     mBrightnessController->updateBrightnessTable(table);
319 
320     return HWC2_ERROR_NONE;
321 }
322 
setLayersColorData()323 int32_t ExynosPrimaryDisplayModule::setLayersColorData()
324 {
325     int32_t ret = 0;
326     uint32_t layerNum = 0;
327 
328     // TODO: b/212616164 remove dimSdrRatio
329     float dimSdrRatio = mBrightnessController->getSdrDimRatioForInstantHbm();
330 
331     // for client target
332     {
333         LayerColorData& layerColorData = mDisplaySceneInfo.getLayerColorDataInstance(layerNum);
334 
335         /* set layer data mapping info */
336         if ((ret = mDisplaySceneInfo.setLayerDataMappingInfo(&mClientCompositionInfo, layerNum)) !=
337             NO_ERROR) {
338             DISPLAY_LOGE("%s: setLayerDataMappingInfo fail for client composition", __func__);
339             return ret;
340         }
341 
342         if ((ret = mDisplaySceneInfo.setClientCompositionColorData(mClientCompositionInfo,
343                                                                    layerColorData, dimSdrRatio)) !=
344             NO_ERROR) {
345             DISPLAY_LOGE("%s: setClientCompositionColorData fail", __func__);
346             return ret;
347         }
348 
349         layerColorData.is_client_target = true;
350         layerNum++;
351     }
352 
353     for (uint32_t i = 0; i < mLayers.size(); i++)
354     {
355         ExynosLayer* layer = mLayers[i];
356 
357         if (layer->mCompositionType == HWC2_COMPOSITION_CLIENT) continue;
358 
359         LayerColorData& layerColorData =
360             mDisplaySceneInfo.getLayerColorDataInstance(layerNum);
361 
362         /* set layer data mapping info */
363         if ((ret = mDisplaySceneInfo.setLayerDataMappingInfo(layer, layerNum))
364                 != NO_ERROR) {
365             DISPLAY_LOGE("%s: layer[%d] setLayerDataMappingInfo fail, layerNum(%d)",
366                     __func__, i, layerNum);
367             return ret;
368         }
369 
370 
371         if ((ret = mDisplaySceneInfo.setLayerColorData(layerColorData, layer,
372                                                        dimSdrRatio)) != NO_ERROR) {
373             DISPLAY_LOGE("%s: layer[%d] setLayerColorData fail, layerNum(%d)",
374                     __func__, i, layerNum);
375             return ret;
376         }
377 
378         layerColorData.is_client_target = false;
379         layerNum++;
380     }
381 
382     /* Resize layer_data when layers were destroyed */
383     if (layerNum < mDisplaySceneInfo.displayScene.layer_data.size())
384         mDisplaySceneInfo.displayScene.layer_data.resize(layerNum);
385 
386     return NO_ERROR;
387 }
388 
hasDppForLayer(ExynosMPPSource * layer)389 bool ExynosPrimaryDisplayModule::hasDppForLayer(ExynosMPPSource* layer)
390 {
391     GsInterfaceType* displayColorInterface = getDisplayColorInterface();
392     if (displayColorInterface == nullptr) {
393         return false;
394     }
395 
396     if (mDisplaySceneInfo.layerDataMappingInfo.count(layer) == 0)
397         return false;
398 
399     uint32_t index =  mDisplaySceneInfo.layerDataMappingInfo[layer].dppIdx;
400     const DisplayType display = getDisplayTypeFromIndex(mIndex);
401     auto size = displayColorInterface->GetPipelineData(display)->Dpp().size();
402     if (index >= size) {
403         DISPLAY_LOGE("%s: invalid dpp index(%d) dpp size(%zu)", __func__, index, size);
404         return false;
405     }
406 
407     return true;
408 }
409 
getDppForLayer(ExynosMPPSource * layer)410 const ExynosPrimaryDisplayModule::GsInterfaceType::IDpp& ExynosPrimaryDisplayModule::getDppForLayer(
411         ExynosMPPSource* layer) {
412     uint32_t index = mDisplaySceneInfo.layerDataMappingInfo[layer].dppIdx;
413     GsInterfaceType* displayColorInterface = getDisplayColorInterface();
414     const DisplayType display = getDisplayTypeFromIndex(mIndex);
415     return displayColorInterface->GetPipelineData(display)->Dpp()[index].get();
416 }
417 
getDppIndexForLayer(ExynosMPPSource * layer)418 int32_t ExynosPrimaryDisplayModule::getDppIndexForLayer(ExynosMPPSource* layer)
419 {
420     if (mDisplaySceneInfo.layerDataMappingInfo.count(layer) == 0)
421         return -1;
422     uint32_t index = mDisplaySceneInfo.layerDataMappingInfo[layer].dppIdx;
423 
424     return static_cast<int32_t>(index);
425 }
426 
deliverWinConfigData()427 int ExynosPrimaryDisplayModule::deliverWinConfigData()
428 {
429     int ret = 0;
430     ExynosDisplayDrmInterfaceModule *moduleDisplayInterface =
431         (ExynosDisplayDrmInterfaceModule*)(mDisplayInterface.get());
432     GsInterfaceType* displayColorInterface = getDisplayColorInterface();
433 
434     bool forceDisplayColorSetting = false;
435     if (!mDisplaySceneInfo.displaySettingDelivered || isForceColorUpdate())
436         forceDisplayColorSetting = true;
437 
438     setForceColorUpdate(false);
439 
440     if (displayColorInterface != nullptr) {
441         moduleDisplayInterface->setColorSettingChanged(
442             mDisplaySceneInfo.needDisplayColorSetting(),
443             forceDisplayColorSetting);
444     }
445 
446     ret = ExynosDisplay::deliverWinConfigData();
447 
448     checkAtcAnimation();
449 
450     if (mDpuData.enable_readback &&
451        !mDpuData.readback_info.requested_from_service)
452         mDisplaySceneInfo.displaySettingDelivered = false;
453     else
454         mDisplaySceneInfo.displaySettingDelivered = true;
455 
456     return ret;
457 }
458 
getLayerColorDataInstance(uint32_t index)459 LayerColorData& ExynosPrimaryDisplayModule::DisplaySceneInfo::getLayerColorDataInstance(
460         uint32_t index)
461 {
462     size_t currentSize = displayScene.layer_data.size();
463     if (index >= currentSize) {
464         displayScene.layer_data.resize(currentSize+1);
465         colorSettingChanged = true;
466     }
467     return displayScene.layer_data[index];
468 }
469 
setLayerDataMappingInfo(ExynosMPPSource * layer,uint32_t index)470 int32_t ExynosPrimaryDisplayModule::DisplaySceneInfo::setLayerDataMappingInfo(
471         ExynosMPPSource* layer, uint32_t index)
472 {
473     if (layerDataMappingInfo.count(layer) != 0) {
474         ALOGE("layer mapping is already inserted (layer: %p, index:%d)",
475                 layer, index);
476         return -EINVAL;
477     }
478     // if assigned displaycolor dppIdx changes, do not reuse it (force plane color update).
479     uint32_t oldPlaneId = prev_layerDataMappingInfo.count(layer) != 0 &&
480                     prev_layerDataMappingInfo[layer].dppIdx == index
481             ? prev_layerDataMappingInfo[layer].planeId
482             : LayerMappingInfo::kPlaneIdNone;
483     layerDataMappingInfo.insert(std::make_pair(layer, LayerMappingInfo{ index, oldPlaneId }));
484 
485     return NO_ERROR;
486 }
487 
setLayerDataspace(LayerColorData & layerColorData,hwc::Dataspace dataspace)488 void ExynosPrimaryDisplayModule::DisplaySceneInfo::setLayerDataspace(
489         LayerColorData& layerColorData,
490         hwc::Dataspace dataspace)
491 {
492     if (layerColorData.dataspace != dataspace) {
493         colorSettingChanged = true;
494         layerColorData.dataspace = dataspace;
495     }
496 }
497 
disableLayerHdrStaticMetadata(LayerColorData & layerColorData)498 void ExynosPrimaryDisplayModule::DisplaySceneInfo::disableLayerHdrStaticMetadata(
499         LayerColorData& layerColorData)
500 {
501     if (layerColorData.static_metadata.is_valid) {
502         colorSettingChanged = true;
503         layerColorData.static_metadata.is_valid = false;
504     }
505 }
506 
setLayerHdrStaticMetadata(LayerColorData & layerColorData,const ExynosHdrStaticInfo & exynosHdrStaticInfo)507 void ExynosPrimaryDisplayModule::DisplaySceneInfo::setLayerHdrStaticMetadata(
508         LayerColorData& layerColorData,
509         const ExynosHdrStaticInfo &exynosHdrStaticInfo)
510 {
511     if (layerColorData.static_metadata.is_valid == false) {
512         colorSettingChanged = true;
513         layerColorData.static_metadata.is_valid = true;
514     }
515 
516     updateInfoSingleVal(layerColorData.static_metadata.display_red_primary_x,
517             exynosHdrStaticInfo.sType1.mR.x);
518     updateInfoSingleVal(layerColorData.static_metadata.display_red_primary_y,
519             exynosHdrStaticInfo.sType1.mR.y);
520     updateInfoSingleVal(layerColorData.static_metadata.display_green_primary_x,
521             exynosHdrStaticInfo.sType1.mG.x);
522     updateInfoSingleVal(layerColorData.static_metadata.display_green_primary_y,
523             exynosHdrStaticInfo.sType1.mG.y);
524     updateInfoSingleVal(layerColorData.static_metadata.display_blue_primary_x,
525             exynosHdrStaticInfo.sType1.mB.x);
526     updateInfoSingleVal(layerColorData.static_metadata.display_blue_primary_y,
527             exynosHdrStaticInfo.sType1.mB.y);
528     updateInfoSingleVal(layerColorData.static_metadata.white_point_x,
529             exynosHdrStaticInfo.sType1.mW.x);
530     updateInfoSingleVal(layerColorData.static_metadata.white_point_y,
531             exynosHdrStaticInfo.sType1.mW.y);
532     updateInfoSingleVal(layerColorData.static_metadata.max_luminance,
533             exynosHdrStaticInfo.sType1.mMaxDisplayLuminance);
534     updateInfoSingleVal(layerColorData.static_metadata.min_luminance,
535             exynosHdrStaticInfo.sType1.mMinDisplayLuminance);
536     updateInfoSingleVal(layerColorData.static_metadata.max_content_light_level,
537             exynosHdrStaticInfo.sType1.mMaxContentLightLevel);
538     updateInfoSingleVal(
539             layerColorData.static_metadata.max_frame_average_light_level,
540             exynosHdrStaticInfo.sType1.mMaxFrameAverageLightLevel);
541 }
542 
setLayerColorTransform(LayerColorData & layerColorData,std::array<float,TRANSFORM_MAT_SIZE> & matrix)543 void ExynosPrimaryDisplayModule::DisplaySceneInfo::setLayerColorTransform(
544         LayerColorData& layerColorData,
545         std::array<float, TRANSFORM_MAT_SIZE> &matrix)
546 {
547     updateInfoSingleVal(layerColorData.matrix, matrix);
548 }
549 
disableLayerHdrDynamicMetadata(LayerColorData & layerColorData)550 void ExynosPrimaryDisplayModule::DisplaySceneInfo::disableLayerHdrDynamicMetadata(
551         LayerColorData& layerColorData)
552 {
553     if (layerColorData.dynamic_metadata.is_valid) {
554         colorSettingChanged = true;
555         layerColorData.dynamic_metadata.is_valid = false;
556     }
557 }
558 
setLayerHdrDynamicMetadata(LayerColorData & layerColorData,const ExynosHdrDynamicInfo & exynosHdrDynamicInfo)559 void ExynosPrimaryDisplayModule::DisplaySceneInfo::setLayerHdrDynamicMetadata(
560         LayerColorData& layerColorData,
561         const ExynosHdrDynamicInfo &exynosHdrDynamicInfo)
562 {
563     if (layerColorData.dynamic_metadata.is_valid == false) {
564         colorSettingChanged = true;
565         layerColorData.dynamic_metadata.is_valid = true;
566     }
567     updateInfoSingleVal(layerColorData.dynamic_metadata.display_maximum_luminance,
568                         exynosHdrDynamicInfo.data.targeted_system_display_maximum_luminance);
569 
570     if (!std::equal(layerColorData.dynamic_metadata.maxscl.begin(),
571                     layerColorData.dynamic_metadata.maxscl.end(),
572                     exynosHdrDynamicInfo.data.maxscl[0])) {
573         colorSettingChanged = true;
574         for (uint32_t i = 0 ; i < layerColorData.dynamic_metadata.maxscl.size(); i++) {
575           layerColorData.dynamic_metadata.maxscl[i] = exynosHdrDynamicInfo.data.maxscl[0][i];
576         }
577     }
578     static constexpr uint32_t DYNAMIC_META_DAT_SIZE = 15;
579 
580     updateInfoVectorVal(layerColorData.dynamic_metadata.maxrgb_percentages,
581                         exynosHdrDynamicInfo.data.maxrgb_percentages[0], DYNAMIC_META_DAT_SIZE);
582     updateInfoVectorVal(layerColorData.dynamic_metadata.maxrgb_percentiles,
583                         exynosHdrDynamicInfo.data.maxrgb_percentiles[0], DYNAMIC_META_DAT_SIZE);
584     updateInfoSingleVal(layerColorData.dynamic_metadata.tm_flag,
585                         exynosHdrDynamicInfo.data.tone_mapping.tone_mapping_flag[0]);
586     updateInfoSingleVal(layerColorData.dynamic_metadata.tm_knee_x,
587                         exynosHdrDynamicInfo.data.tone_mapping.knee_point_x[0]);
588     updateInfoSingleVal(layerColorData.dynamic_metadata.tm_knee_y,
589                         exynosHdrDynamicInfo.data.tone_mapping.knee_point_y[0]);
590     updateInfoVectorVal(layerColorData.dynamic_metadata.bezier_curve_anchors,
591                         exynosHdrDynamicInfo.data.tone_mapping.bezier_curve_anchors[0],
592                         DYNAMIC_META_DAT_SIZE);
593 }
594 
setClientCompositionColorData(const ExynosCompositionInfo & clientCompositionInfo,LayerColorData & layerData,float dimSdrRatio)595 int32_t ExynosPrimaryDisplayModule::DisplaySceneInfo::setClientCompositionColorData(
596         const ExynosCompositionInfo &clientCompositionInfo, LayerColorData& layerData,
597         float dimSdrRatio)
598 {
599     layerData.dim_ratio = 1.0f;
600     setLayerDataspace(layerData,
601                       static_cast<hwc::Dataspace>(clientCompositionInfo.mDataSpace));
602     disableLayerHdrStaticMetadata(layerData);
603     disableLayerHdrDynamicMetadata(layerData);
604 
605     if (dimSdrRatio != 1.0) {
606         std::array<float, TRANSFORM_MAT_SIZE> scaleMatrix = {
607             dimSdrRatio, 0.0, 0.0, 0.0,
608             0.0, dimSdrRatio, 0.0, 0.0,
609             0.0, 0.0, dimSdrRatio, 0.0,
610             0.0, 0.0, 0.0, 1.0
611         };
612         setLayerColorTransform(layerData, scaleMatrix);
613     } else {
614         static std::array<float, TRANSFORM_MAT_SIZE> defaultMatrix {
615             1.0, 0.0, 0.0, 0.0,
616             0.0, 1.0, 0.0, 0.0,
617             0.0, 0.0, 1.0, 0.0,
618             0.0, 0.0, 0.0, 1.0
619         };
620         setLayerColorTransform(layerData, defaultMatrix);
621     }
622 
623     return NO_ERROR;
624 }
625 
setLayerColorData(LayerColorData & layerData,ExynosLayer * layer,float dimSdrRatio)626 int32_t ExynosPrimaryDisplayModule::DisplaySceneInfo::setLayerColorData(
627         LayerColorData& layerData, ExynosLayer* layer, float dimSdrRatio)
628 {
629     layerData.is_solid_color_layer = layer->isDimLayer();
630     layerData.solid_color.r = layer->mColor.r;
631     layerData.solid_color.g = layer->mColor.g;
632     layerData.solid_color.b = layer->mColor.b;
633     layerData.solid_color.a = layer->mColor.a;
634     layerData.dim_ratio = layer->mPreprocessedInfo.sdrDimRatio;
635     setLayerDataspace(layerData,
636             static_cast<hwc::Dataspace>(layer->mDataSpace));
637     if (layer->mIsHdrLayer && layer->getMetaParcel() != nullptr) {
638         if (layer->getMetaParcel()->eType & VIDEO_INFO_TYPE_HDR_STATIC)
639             setLayerHdrStaticMetadata(layerData, layer->getMetaParcel()->sHdrStaticInfo);
640         else
641             disableLayerHdrStaticMetadata(layerData);
642 
643         if (layer->getMetaParcel()->eType & VIDEO_INFO_TYPE_HDR_DYNAMIC)
644             setLayerHdrDynamicMetadata(layerData, layer->getMetaParcel()->sHdrDynamicInfo);
645         else
646             disableLayerHdrDynamicMetadata(layerData);
647     } else {
648         disableLayerHdrStaticMetadata(layerData);
649         disableLayerHdrDynamicMetadata(layerData);
650     }
651 
652     static std::array<float, TRANSFORM_MAT_SIZE> defaultMatrix {
653         1.0, 0.0, 0.0, 0.0,
654         0.0, 1.0, 0.0, 0.0,
655         0.0, 0.0, 1.0, 0.0,
656         0.0, 0.0, 0.0, 1.0
657     };
658 
659     if (dimSdrRatio == 1.0 || layer->mIsHdrLayer) {
660         if (layer->mLayerColorTransform.enable)
661             setLayerColorTransform(layerData,
662                     layer->mLayerColorTransform.mat);
663         else
664             setLayerColorTransform(layerData,
665                     defaultMatrix);
666     } else {
667         if (layer->mLayerColorTransform.enable) {
668             std::array<float, TRANSFORM_MAT_SIZE> scaleMatrix =
669                 layer->mLayerColorTransform.mat;
670 
671             // scale coeffs
672             scaleMatrix[0] *= dimSdrRatio;
673             scaleMatrix[1] *= dimSdrRatio;
674             scaleMatrix[2] *= dimSdrRatio;
675             scaleMatrix[4] *= dimSdrRatio;
676             scaleMatrix[5] *= dimSdrRatio;
677             scaleMatrix[6] *= dimSdrRatio;
678             scaleMatrix[8] *= dimSdrRatio;
679             scaleMatrix[9] *= dimSdrRatio;
680             scaleMatrix[10] *= dimSdrRatio;
681 
682             // scale offsets
683             scaleMatrix[12] *= dimSdrRatio;
684             scaleMatrix[13] *= dimSdrRatio;
685             scaleMatrix[14] *= dimSdrRatio;
686 
687             setLayerColorTransform(layerData, scaleMatrix);
688         } else {
689             std::array<float, TRANSFORM_MAT_SIZE> scaleMatrix = {
690                 dimSdrRatio, 0.0, 0.0, 0.0,
691                 0.0, dimSdrRatio, 0.0, 0.0,
692                 0.0, 0.0, dimSdrRatio, 0.0,
693                 0.0, 0.0, 0.0, 1.0
694             };
695 
696             setLayerColorTransform(layerData, scaleMatrix);
697         }
698     }
699 
700     return NO_ERROR;
701 }
702 
updateColorConversionInfo()703 int32_t ExynosPrimaryDisplayModule::updateColorConversionInfo()
704 {
705     int ret = 0;
706     GsInterfaceType* displayColorInterface = getDisplayColorInterface();
707     if (displayColorInterface == nullptr) {
708         return ret;
709     }
710 
711     updateBrightnessState();
712     /* clear flag and layer mapping info before setting */
713     mDisplaySceneInfo.reset();
714 
715     if ((ret = setLayersColorData()) != NO_ERROR)
716         return ret;
717 
718     mDisplaySceneInfo.displayScene.bm = mBrightnessController->isGhbmOn()
719             ? displaycolor::BrightnessMode::BM_HBM
720             : displaycolor::BrightnessMode::BM_NOMINAL;
721 
722     mDisplaySceneInfo.displayScene.force_hdr = mBrightnessController->isDimSdr();
723     mDisplaySceneInfo.displayScene.lhbm_on = mBrightnessController->isLhbmOn();
724     mDisplaySceneInfo.displayScene.hdr_layer_state = mBrightnessController->getHdrLayerState();
725     mDisplaySceneInfo.displayScene.dbv = mBrightnessController->getBrightnessLevel();
726 
727     if (hwcCheckDebugMessages(eDebugColorManagement))
728         mDisplaySceneInfo.printDisplayScene();
729 
730     const DisplayType display = getDisplayTypeFromIndex(mIndex);
731     if ((ret = displayColorInterface->Update(display, mDisplaySceneInfo.displayScene)) != 0) {
732         DISPLAY_LOGE("Display Scene update error (%d)", ret);
733         return ret;
734     }
735 
736     return ret;
737 }
738 
resetColorMappingInfo(ExynosMPPSource * mppSrc)739 int32_t ExynosPrimaryDisplayModule::resetColorMappingInfo(ExynosMPPSource* mppSrc) {
740     if (mDisplaySceneInfo.layerDataMappingInfo.count(mppSrc) == 0) {
741         return -EINVAL;
742     }
743 
744     mDisplaySceneInfo.layerDataMappingInfo[mppSrc].planeId =
745             DisplaySceneInfo::LayerMappingInfo::kPlaneIdNone;
746 
747     return NO_ERROR;
748 }
updatePresentColorConversionInfo()749 int32_t ExynosPrimaryDisplayModule::updatePresentColorConversionInfo()
750 {
751     int ret = NO_ERROR;
752     GsInterfaceType* displayColorInterface = getDisplayColorInterface();
753     if (displayColorInterface == nullptr) {
754         return ret;
755     }
756 
757     ExynosDisplayDrmInterfaceModule *moduleDisplayInterface =
758         (ExynosDisplayDrmInterfaceModule*)(mDisplayInterface.get());
759     auto refresh_rate = moduleDisplayInterface->getDesiredRefreshRate();
760     if (refresh_rate > 0) {
761         mDisplaySceneInfo.displayScene.refresh_rate = refresh_rate;
762     }
763     auto operation_rate = moduleDisplayInterface->getOperationRate();
764     if (operation_rate > 0) {
765         mDisplaySceneInfo.displayScene.operation_rate = static_cast<uint32_t>(operation_rate);
766     }
767 
768     mDisplaySceneInfo.displayScene.lhbm_on = mBrightnessController->isLhbmOn();
769     mDisplaySceneInfo.displayScene.dbv = mBrightnessController->getBrightnessLevel();
770     const DisplayType display = getDisplayTypeFromIndex(mIndex);
771     if ((ret = displayColorInterface->UpdatePresent(display, mDisplaySceneInfo.displayScene)) !=
772         0) {
773         DISPLAY_LOGE("Display Scene update error (%d)", ret);
774         return ret;
775     }
776 
777     return ret;
778 }
779 
getColorAdjustedDbv(uint32_t & dbv_adj)780 int32_t ExynosPrimaryDisplayModule::getColorAdjustedDbv(uint32_t &dbv_adj) {
781     GsInterfaceType* displayColorInterface = getDisplayColorInterface();
782     if (displayColorInterface == nullptr) {
783         return NO_ERROR;
784     }
785 
786     const DisplayType display = getDisplayTypeFromIndex(mIndex);
787     dbv_adj = displayColorInterface->GetPipelineData(display)->Panel().GetAdjustedBrightnessLevel();
788     return NO_ERROR;
789 }
790 
needDisplayColorSetting()791 bool ExynosPrimaryDisplayModule::DisplaySceneInfo::needDisplayColorSetting()
792 {
793     /* TODO: Check if we can skip color setting */
794     /* For now, propage setting every frame */
795     return true;
796 
797     if (colorSettingChanged)
798         return true;
799     if (prev_layerDataMappingInfo != layerDataMappingInfo)
800         return true;
801 
802     return false;
803 }
804 
printDisplayScene()805 void ExynosPrimaryDisplayModule::DisplaySceneInfo::printDisplayScene()
806 {
807     ALOGD("======================= DisplayScene info ========================");
808     ALOGD("dpu_bit_depth: %d", static_cast<uint32_t>(displayScene.dpu_bit_depth));
809     ALOGD("color_mode: %d", static_cast<uint32_t>(displayScene.color_mode));
810     ALOGD("render_intent: %d", static_cast<uint32_t>(displayScene.render_intent));
811     ALOGD("matrix");
812     for (uint32_t i = 0; i < 16; (i += 4)) {
813         ALOGD("%f, %f, %f, %f",
814                 displayScene.matrix[i], displayScene.matrix[i+1],
815                 displayScene.matrix[i+2], displayScene.matrix[i+3]);
816     }
817     ALOGD("layer: %zu ++++++",
818             displayScene.layer_data.size());
819     for (uint32_t i = 0; i < displayScene.layer_data.size(); i++) {
820         ALOGD("layer[%d] info", i);
821         printLayerColorData(displayScene.layer_data[i]);
822     }
823 
824     ALOGD("layerDataMappingInfo: %zu ++++++",
825             layerDataMappingInfo.size());
826     for (auto layer : layerDataMappingInfo) {
827         ALOGD("[layer: %p] [%d, %d]", layer.first, layer.second.dppIdx, layer.second.planeId);
828     }
829 }
830 
printLayerColorData(const LayerColorData & layerData)831 void ExynosPrimaryDisplayModule::DisplaySceneInfo::printLayerColorData(
832     const LayerColorData& layerData)
833 {
834     ALOGD("dataspace: 0x%8x", static_cast<uint32_t>(layerData.dataspace));
835     ALOGD("matrix");
836     for (uint32_t i = 0; i < 16; (i += 4)) {
837         ALOGD("%f, %f, %f, %f",
838                 layerData.matrix[i], layerData.matrix[i+1],
839                 layerData.matrix[i+2], layerData.matrix[i+3]);
840     }
841     ALOGD("static_metadata.is_valid(%d)", layerData.static_metadata.is_valid);
842     if (layerData.static_metadata.is_valid) {
843         ALOGD("\tdisplay_red_primary(%d, %d)",
844                 layerData.static_metadata.display_red_primary_x,
845                 layerData.static_metadata.display_red_primary_y);
846         ALOGD("\tdisplay_green_primary(%d, %d)",
847                 layerData.static_metadata.display_green_primary_x,
848                 layerData.static_metadata.display_green_primary_y);
849         ALOGD("\tdisplay_blue_primary(%d, %d)",
850                 layerData.static_metadata.display_blue_primary_x,
851                 layerData.static_metadata.display_blue_primary_y);
852         ALOGD("\twhite_point(%d, %d)",
853                 layerData.static_metadata.white_point_x,
854                 layerData.static_metadata.white_point_y);
855     }
856     ALOGD("dynamic_metadata.is_valid(%d)", layerData.dynamic_metadata.is_valid);
857     if (layerData.dynamic_metadata.is_valid) {
858         ALOGD("\tdisplay_maximum_luminance: %d",
859                 layerData.dynamic_metadata.display_maximum_luminance);
860         ALOGD("\tmaxscl(%d, %d, %d)", layerData.dynamic_metadata.maxscl[0],
861                 layerData.dynamic_metadata.maxscl[1],
862                 layerData.dynamic_metadata.maxscl[2]);
863         ALOGD("\ttm_flag(%d)", layerData.dynamic_metadata.tm_flag);
864         ALOGD("\ttm_knee_x(%d)", layerData.dynamic_metadata.tm_knee_x);
865         ALOGD("\ttm_knee_y(%d)", layerData.dynamic_metadata.tm_knee_y);
866     }
867 }
868 
parseAtcProfile()869 bool ExynosPrimaryDisplayModule::parseAtcProfile() {
870     Json::Value root;
871     Json::CharReaderBuilder reader_builder;
872     std::unique_ptr<Json::CharReader> reader(reader_builder.newCharReader());
873     std::string atc_profile;
874 
875     if (!android::base::ReadFileToString(kAtcProfilePath, &atc_profile)) {
876         atc_profile = kAtcJsonRaw;
877         ALOGI("Use default atc profile file");
878     }
879 
880     if (!reader->parse(atc_profile.c_str(), atc_profile.c_str() + atc_profile.size(), &root,
881                        nullptr)) {
882         ALOGE("Failed to parse atc profile file");
883         return false;
884     }
885 
886     ALOGI("Atc Profile version = %s", root[kAtcProfileVersionStr].asString().c_str());
887     Json::Value nodes = root[kAtcProfileModesStr];
888     atc_mode mode;
889 
890     for (Json::Value::ArrayIndex i = 0; i < nodes.size(); ++i) {
891         std::string name = nodes[i][kAtcProfileModeNameStr].asString();
892 
893         if (nodes[i][kAtcProfileLuxMapStr].size() != nodes[i][kAtcProfileAlMapStr].size() &&
894             nodes[i][kAtcProfileAlMapStr].size() != nodes[i][kAtcProfileStMapStr].size()) {
895             ALOGE("Atc profile is unavailable !");
896             return false;
897         }
898 
899         uint32_t map_cnt = nodes[i][kAtcProfileLuxMapStr].size();
900 
901         mode.lux_map.clear();
902         for (uint32_t index = 0; index < map_cnt; ++index) {
903             mode.lux_map.emplace_back(atc_lux_map{nodes[i][kAtcProfileLuxMapStr][index].asUInt(),
904                                                   nodes[i][kAtcProfileAlMapStr][index].asUInt(),
905                                                   nodes[i][kAtcProfileStMapStr][index].asUInt()});
906         }
907 
908         if (!nodes[i][kAtcProfileStUpStepStr].empty())
909             mode.st_up_step = nodes[i][kAtcProfileStUpStepStr].asUInt();
910         else
911             mode.st_up_step = kAtcStStep;
912 
913         if (!nodes[i][kAtcProfileStDownStepStr].empty())
914             mode.st_down_step = nodes[i][kAtcProfileStDownStepStr].asUInt();
915         else
916             mode.st_down_step = kAtcStStep;
917 
918         if (nodes[i][kAtcProfileSubSettingStr].size() != kAtcSubSetting.size()) return false;
919 
920         for (auto it = kAtcSubSetting.begin(); it != kAtcSubSetting.end(); it++) {
921             mode.sub_setting[it->first.c_str()] =
922                     nodes[i][kAtcProfileSubSettingStr][it->first.c_str()].asUInt();
923         }
924         auto ret = mAtcModeSetting.insert(std::make_pair(name.c_str(), mode));
925         if (ret.second == false) {
926             ALOGE("Atc mode %s is already existed!", ret.first->first.c_str());
927             return false;
928         }
929     }
930 
931     if (mAtcModeSetting.find(kAtcModeNormalStr) == mAtcModeSetting.end()) {
932         ALOGW("Failed to find atc normal mode");
933         return false;
934     }
935     return true;
936 }
937 
isLbeSupported()938 bool ExynosPrimaryDisplayModule::isLbeSupported() {
939     return mLbeSupported;
940 }
941 
initLbe()942 void ExynosPrimaryDisplayModule::initLbe() {
943     if (!parseAtcProfile()) {
944         ALOGD("Failed to parseAtcMode");
945         mAtcInit = false;
946         return;
947     }
948 
949     mAtcInit = true;
950     mAtcAmbientLight.node = String8::format(ATC_AMBIENT_LIGHT_FILE_NAME, mIndex);
951     mAtcAmbientLight.value.set_dirty();
952     mAtcStrength.node = String8::format(ATC_ST_FILE_NAME, mIndex);
953     mAtcStrength.value.set_dirty();
954     mAtcEnable.node = String8::format(ATC_ENABLE_FILE_NAME, mIndex);
955     mAtcEnable.value.set_dirty();
956 
957     for (auto it = kAtcSubSetting.begin(); it != kAtcSubSetting.end(); it++) {
958         mAtcSubSetting[it->first.c_str()].node = String8::format(it->second.c_str(), mIndex);
959         mAtcSubSetting[it->first.c_str()].value.set_dirty();
960     }
961     mLbeSupported = true;
962 }
963 
getAtcLuxMapIndex(std::vector<atc_lux_map> map,uint32_t lux)964 uint32_t ExynosPrimaryDisplayModule::getAtcLuxMapIndex(std::vector<atc_lux_map> map, uint32_t lux) {
965     uint32_t index = 0;
966     for (uint32_t i = 0; i < map.size(); i++) {
967         if (lux < map[i].lux) {
968             break;
969         }
970         index = i;
971     }
972 
973     return index;
974 }
975 
setAtcStrength(uint32_t strength)976 int32_t ExynosPrimaryDisplayModule::setAtcStrength(uint32_t strength) {
977     mAtcStrength.value.store(strength);
978     if (mAtcStrength.value.is_dirty()) {
979         if (writeIntToFile(mAtcStrength.node, mAtcStrength.value.get()) != NO_ERROR) return -EPERM;
980         mAtcStrength.value.clear_dirty();
981     }
982     return NO_ERROR;
983 }
984 
setAtcAmbientLight(uint32_t ambient_light)985 int32_t ExynosPrimaryDisplayModule::setAtcAmbientLight(uint32_t ambient_light) {
986     mAtcAmbientLight.value.store(ambient_light);
987     if (mAtcAmbientLight.value.is_dirty()) {
988         if (writeIntToFile(mAtcAmbientLight.node, mAtcAmbientLight.value.get()) != NO_ERROR)
989             return -EPERM;
990         mAtcAmbientLight.value.clear_dirty();
991     }
992 
993     return NO_ERROR;
994 }
995 
setAtcMode(std::string mode_name)996 int32_t ExynosPrimaryDisplayModule::setAtcMode(std::string mode_name) {
997     auto mode_data = mAtcModeSetting.find(mode_name);
998     uint32_t ambient_light = 0;
999     uint32_t strength = 0;
1000     bool enable = (!mode_name.empty()) && (mode_data != mAtcModeSetting.end());
1001 
1002     if (enable) {
1003         atc_mode mode = mode_data->second;
1004         for (auto it = kAtcSubSetting.begin(); it != kAtcSubSetting.end(); it++) {
1005             mAtcSubSetting[it->first.c_str()].value.store(mode.sub_setting[it->first.c_str()]);
1006             if (mAtcSubSetting[it->first.c_str()].value.is_dirty()) {
1007                 if (writeIntToFile(mAtcSubSetting[it->first.c_str()].node,
1008                                    mAtcSubSetting[it->first.c_str()].value.get()) != NO_ERROR)
1009                     return -EPERM;
1010                 mAtcSubSetting[it->first.c_str()].value.clear_dirty();
1011             }
1012         }
1013         mAtcStUpStep = mode.st_up_step;
1014         mAtcStDownStep = mode.st_down_step;
1015 
1016         uint32_t index = getAtcLuxMapIndex(mode.lux_map, mCurrentLux);
1017         ambient_light = mode.lux_map[index].al;
1018         strength = mode.lux_map[index].st;
1019     }
1020 
1021     if (setAtcAmbientLight(ambient_light) != NO_ERROR) {
1022         ALOGE("Fail to set atc ambient light for %s mode", mode_name.c_str());
1023         return -EPERM;
1024     }
1025 
1026     if (setAtcStDimming(strength) != NO_ERROR) {
1027         ALOGE("Fail to set atc st dimming for %s mode", mode_name.c_str());
1028         return -EPERM;
1029     }
1030 
1031     if (!enable && isInAtcAnimation()) {
1032         mPendingAtcOff = true;
1033     } else {
1034         if (setAtcEnable(enable) != NO_ERROR) {
1035             ALOGE("Fail to set atc enable = %d", enable);
1036             return -EPERM;
1037         }
1038         mPendingAtcOff = false;
1039     }
1040 
1041     mCurrentAtcModeName = enable ? mode_name : "NULL";
1042     ALOGI("atc enable=%d (mode=%s, pending off=%s)", enable, mCurrentAtcModeName.c_str(),
1043           mPendingAtcOff ? "true" : "false");
1044     return NO_ERROR;
1045 }
setLbeState(LbeState state)1046 void ExynosPrimaryDisplayModule::setLbeState(LbeState state) {
1047     if (!mAtcInit) return;
1048     std::string modeStr;
1049     bool enhanced_hbm = false;
1050     switch (state) {
1051         case LbeState::OFF:
1052             mCurrentLux = 0;
1053             break;
1054         case LbeState::NORMAL:
1055             modeStr = kAtcModeNormalStr;
1056             break;
1057         case LbeState::HIGH_BRIGHTNESS:
1058             modeStr = kAtcModeHbmStr;
1059             break;
1060         case LbeState::POWER_SAVE:
1061             modeStr = kAtcModePowerSaveStr;
1062             break;
1063         case LbeState::HIGH_BRIGHTNESS_ENHANCE:
1064             modeStr = kAtcModeHbmStr;
1065             enhanced_hbm = true;
1066             break;
1067         default:
1068             ALOGE("Lbe state not support");
1069             return;
1070     }
1071 
1072     if (setAtcMode(modeStr) != NO_ERROR) return;
1073 
1074     mBrightnessController->processEnhancedHbm(enhanced_hbm);
1075     mBrightnessController->setOutdoorVisibility(state);
1076 
1077     if (mCurrentLbeState != state) {
1078         mCurrentLbeState = state;
1079         mDevice->onRefresh(mDisplayId);
1080     }
1081     ALOGI("Lbe state %hhd", mCurrentLbeState);
1082 }
1083 
setLbeAmbientLight(int value)1084 void ExynosPrimaryDisplayModule::setLbeAmbientLight(int value) {
1085     if (!mAtcInit) return;
1086 
1087     auto it = mAtcModeSetting.find(mCurrentAtcModeName);
1088     if (it == mAtcModeSetting.end()) {
1089         ALOGE("Atc mode not found");
1090         return;
1091     }
1092     atc_mode mode = it->second;
1093 
1094     uint32_t index = getAtcLuxMapIndex(mode.lux_map, value);
1095     if (setAtcAmbientLight(mode.lux_map[index].al) != NO_ERROR) {
1096         ALOGE("Failed to set atc ambient light");
1097         return;
1098     }
1099 
1100     if (setAtcStDimming(mode.lux_map[index].st) != NO_ERROR) {
1101         ALOGE("Failed to set atc st dimming");
1102         return;
1103     }
1104 
1105     if (mAtcLuxMapIndex != index) {
1106         mAtcLuxMapIndex = index;
1107         mDevice->onRefresh(mDisplayId);
1108     }
1109     mCurrentLux = value;
1110 }
1111 
getLbeState()1112 LbeState ExynosPrimaryDisplayModule::getLbeState() {
1113     return mCurrentLbeState;
1114 }
1115 
getPanelCalibrationStatus()1116 PanelCalibrationStatus ExynosPrimaryDisplayModule::getPanelCalibrationStatus() {
1117     auto displayColorInterface = getDisplayColorInterface();
1118     if (displayColorInterface == nullptr) {
1119         return PanelCalibrationStatus::UNCALIBRATED;
1120     }
1121 
1122     auto displayType = getBuiltInDisplayType();
1123     auto calibrationInfo = displayColorInterface->GetCalibrationInfo(displayType);
1124 
1125     if (calibrationInfo.factory_cal_loaded) {
1126         return PanelCalibrationStatus::ORIGINAL;
1127     } else if (calibrationInfo.golden_cal_loaded) {
1128         return PanelCalibrationStatus::GOLDEN;
1129     } else {
1130         return PanelCalibrationStatus::UNCALIBRATED;
1131     }
1132 }
1133 
setAtcStDimming(uint32_t value)1134 int32_t ExynosPrimaryDisplayModule::setAtcStDimming(uint32_t value) {
1135     Mutex::Autolock lock(mAtcStMutex);
1136     int32_t strength = mAtcStrength.value.get();
1137     if (mAtcStTarget != value) {
1138         mAtcStTarget = value;
1139         uint32_t step = mAtcStTarget > strength ? mAtcStUpStep : mAtcStDownStep;
1140 
1141         int diff = value - strength;
1142         uint32_t count = (std::abs(diff) + step - 1) / step;
1143         mAtcStStepCount = count;
1144         ALOGI("setup atc st dimming=%d, count=%d, step=%d", value, count, step);
1145     }
1146 
1147     if (mAtcStStepCount == 0 && !mAtcStrength.value.is_dirty()) return NO_ERROR;
1148 
1149     if ((strength + mAtcStUpStep) < mAtcStTarget) {
1150         strength = strength + mAtcStUpStep;
1151     } else if (strength > (mAtcStTarget + mAtcStDownStep)) {
1152         strength = strength - mAtcStDownStep;
1153     } else {
1154         strength = mAtcStTarget;
1155     }
1156 
1157     if (setAtcStrength(strength) != NO_ERROR) {
1158         ALOGE("Failed to set atc st");
1159         return -EPERM;
1160     }
1161 
1162     if (mAtcStStepCount > 0) mAtcStStepCount--;
1163     return NO_ERROR;
1164 }
1165 
setAtcEnable(bool enable)1166 int32_t ExynosPrimaryDisplayModule::setAtcEnable(bool enable) {
1167     mAtcEnable.value.store(enable);
1168     if (mAtcEnable.value.is_dirty()) {
1169         if (writeIntToFile(mAtcEnable.node, enable) != NO_ERROR) return -EPERM;
1170         mAtcEnable.value.clear_dirty();
1171     }
1172     return NO_ERROR;
1173 }
1174 
checkAtcAnimation()1175 void ExynosPrimaryDisplayModule::checkAtcAnimation() {
1176     if (!isInAtcAnimation()) return;
1177 
1178     if (setAtcStDimming(mAtcStTarget) != NO_ERROR) {
1179         ALOGE("Failed to set atc st dimming");
1180         return;
1181     }
1182 
1183     if (mPendingAtcOff && mAtcStStepCount == 0) {
1184         if (setAtcEnable(false) != NO_ERROR) {
1185             ALOGE("Failed to set atc enable to off");
1186             return;
1187         }
1188         mPendingAtcOff = false;
1189         ALOGI("atc enable is off (pending off=false)");
1190     }
1191 
1192     mDevice->onRefresh(mDisplayId);
1193 }
1194 
setPowerMode(int32_t mode)1195 int32_t ExynosPrimaryDisplayModule::setPowerMode(int32_t mode) {
1196     hwc2_power_mode_t prevPowerModeState = mPowerModeState.value_or(HWC2_POWER_MODE_OFF);
1197     int32_t ret;
1198 
1199     ret = ExynosPrimaryDisplay::setPowerMode(mode);
1200 
1201     if ((ret == HWC2_ERROR_NONE) && isDisplaySwitched(mode, prevPowerModeState)) {
1202         ExynosDeviceModule* device = static_cast<ExynosDeviceModule*>(mDevice);
1203 
1204         device->setActiveDisplay(mIndex);
1205         setForceColorUpdate(true);
1206     }
1207     return ret;
1208 }
1209 
isDisplaySwitched(int32_t mode,int32_t prevMode)1210 bool ExynosPrimaryDisplayModule::isDisplaySwitched(int32_t mode, int32_t prevMode) {
1211     ExynosDeviceModule* device = static_cast<ExynosDeviceModule*>(mDevice);
1212 
1213     return (device->getActiveDisplay() != mIndex) && (prevMode == HWC_POWER_MODE_OFF) &&
1214             (mode != HWC_POWER_MODE_OFF);
1215 }
1216 
isColorCalibratedByDevice()1217 bool ExynosPrimaryDisplayModule::isColorCalibratedByDevice() {
1218     const DisplayType display = getDisplayTypeFromIndex(mIndex);
1219     GsInterfaceType* displayColorInterface = getDisplayColorInterface();
1220     return displayColorInterface->GetCalibrationInfo(display).factory_cal_loaded;
1221 };
1222