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 "rs_hetero_hdr_util.h"
17
18 namespace OHOS {
19 namespace Rosen {
GetInstance()20 RSHeteroHdrUtil &RSHeteroHdrUtil::GetInstance()
21 {
22 static RSHeteroHdrUtil instance;
23 return instance;
24 }
25
RSHeteroHdrUtil()26 RSHeteroHdrUtil::RSHeteroHdrUtil()
27 {
28 mdcHandle = dlopen(mdcLib, RTLD_NOW);
29 if (mdcHandle == nullptr) {
30 RS_LOGW("[%{public}s_%{public}d]:load library failed, reason: %{public}s", __func__, __LINE__, dlerror());
31 return;
32 }
33 *reinterpret_cast<void **>(&getMdcDevice) = dlsym(mdcHandle, "GetMdcDevice");
34 mdcDev = getMdcDevice();
35 if (mdcDev == nullptr) {
36 RS_LOGE("mdcDev is nullptr");
37 return;
38 }
39 }
40
~RSHeteroHdrUtil()41 RSHeteroHdrUtil::~RSHeteroHdrUtil()
42 {
43 if (mdcHandle) {
44 dlclose(mdcHandle);
45 mdcDev = nullptr;
46 }
47 }
48
BuildHpaeHdrTask(hapeTaskInfoT & taskinfo)49 int32_t RSHeteroHdrUtil::BuildHpaeHdrTask(hapeTaskInfoT& taskinfo)
50 {
51 if (taskinfo.dstRect.right == 0 || taskinfo.dstRect.bottom == 0) {
52 *taskinfo.taskPtr = nullptr;
53 RS_LOGE("dstRect is invalid");
54 return -1;
55 }
56 if (!(mdcExistedStatus_.load())) {
57 return -1;
58 }
59
60 mdcContent.srcRect = taskinfo.srcRect;
61 mdcContent.dstRect = taskinfo.dstRect;
62 mdcContent.perfLev = DVFS_LEVEL_HIGH;
63
64 mdcContent.transform = taskinfo.transform;
65 mdcContent.srcHandle = taskinfo.srcHandle;
66 mdcContent.dstHandle = taskinfo.dstHandle;
67 mdcContent.acquireFenceFd = taskinfo.acquireFenceFd;
68 mdcContent.releaseFenceFd = taskinfo.releaseFenceFd;
69 mdcContent.displaySdrNit = taskinfo.displaySDRNits;
70 mdcContent.displayHdrNit = taskinfo.displayHDRNits;
71 mdcContent.isAsyncTask = true;
72 mdcContent.taskId = taskinfo.taskId;
73 mdcContent.taskPtr = taskinfo.taskPtr;
74
75 mdcContent.effectResourceRequest = 1ULL << EFFECT_RESOURCE_HDR_BIT;
76
77 int ret = mdcDev->copybit(mdcDev, 0, &mdcContent);
78 if (ret != 0) {
79 mdcStatus_.store(false);
80 RS_LOGE("do mdc copybit fail");
81 mdcDev->releaseChannel(mdcDev, 0);
82 mdcContent.taskPtr = nullptr;
83 return -1;
84 }
85 mdcStatus_.store(true);
86 return 0;
87 }
88
RequestHpaeChannel()89 int32_t RSHeteroHdrUtil::RequestHpaeChannel()
90 {
91 if (mdcExistedStatus_.load()) {
92 return 0;
93 }
94 if (mdcDev->requestChannelByCap == nullptr) {
95 RS_LOGE("requestChannelByCap is null");
96 return -1;
97 }
98 int channelStatus = mdcDev->requestChannelByCap(mdcDev, MDC_CAP_HEBCE | MDC_CAP_HDR | MDC_CAP_ROT | MDC_CAP_SCALE);
99 if (channelStatus < 0) {
100 RS_LOGE("request mediacomm channel fail");
101 return channelStatus;
102 }
103 mdcExistedStatus_.store(true);
104 return 0;
105 }
106
DestroyHpaeHdrTask(uint32_t taskId)107 void RSHeteroHdrUtil::DestroyHpaeHdrTask(uint32_t taskId)
108 {
109 if (mdcStatus_.load()) {
110 mdcDev->destroyTask(taskId);
111 mdcStatus_.store(true);
112 }
113 }
114
ReleaseHpaeHdrChannel()115 void RSHeteroHdrUtil::ReleaseHpaeHdrChannel()
116 {
117 if (mdcExistedStatus_.load()) {
118 RS_TRACE_NAME("RSHeteroHdrUtil::ReleaseHpaeHdrChannel no need post task");
119 RS_LOGD("RSHeteroHdrUtil::ReleaseHpaeHdrChannel no need post task");
120 mdcDev->releaseChannel(mdcDev, 0);
121 mdcExistedStatus_.store(false);
122 }
123 }
124 } // namespace Rosen
125 } // namespace OHOS
126