1 /*
2  * Copyright (c) 2021 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 "drm_crtc.h"
17 #include "display_common.h"
18 #include "drm_device.h"
19 
20 namespace OHOS {
21     namespace HDI {
22         namespace DISPLAY {
23             struct PlaneMaskName planeMaskNames[] = {
24                 {DrmPlaneType::DRM_PLANE_TYPE_CLUSTER0_MASK, "Cluster0"},
25                 {DrmPlaneType::DRM_PLANE_TYPE_CLUSTER1_MASK, "Cluster1"},
26                 {DrmPlaneType::DRM_PLANE_TYPE_ESMART0_MASK, "Esmart0"},
27                 {DrmPlaneType::DRM_PLANE_TYPE_ESMART1_MASK, "Esmart1"},
28                 {DrmPlaneType::DRM_PLANE_TYPE_SMART0_MASK, "Smart0"},
29                 {DrmPlaneType::DRM_PLANE_TYPE_SMART1_MASK, "Smart1"},
30                 {DrmPlaneType::DRM_PLANE_TYPE_Unknown, "unknown"},
31             };
32 
DrmCrtc(drmModeCrtcPtr c, uint32_t pipe)33             DrmCrtc::DrmCrtc(drmModeCrtcPtr c, uint32_t pipe) : mId(c->crtc_id), mPipe(pipe), mPlaneMask(0)
34             {
35             }
36 
Init(DrmDevice &drmDevice)37             int32_t DrmCrtc::Init(DrmDevice &drmDevice)
38             {
39                 DISPLAY_DEBUGLOG();
40                 int32_t ret;
41                 DrmProperty prop;
42                 ret = drmDevice.GetCrtcProperty(*this, PROP_MODEID, prop);
43                 mModePropId = prop.propId;
44                 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("can not get mode prop id"));
45 
46                 ret = drmDevice.GetCrtcProperty(*this, PROP_OUTFENCE, prop);
47                 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE,
48                                    DISPLAY_LOGE("cat not get out fence prop id"));
49                 mOutFencePropId = prop.propId;
50 
51                 ret = drmDevice.GetCrtcProperty(*this, PROP_ACTIVE, prop);
52                 DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE,
53                                    DISPLAY_LOGE("cat not get out fence prop id"));
54                 mActivePropId = prop.propId;
55 
56                 // Plane mask
57                 mPlaneMask = 0;
58                 ret = drmDevice.GetCrtcProperty(*this, "PLANE_MASK", prop);
59                 if (ret != DISPLAY_SUCCESS) {
60                     DISPLAY_LOGE("Failed to get plane_mask property");
61                 } else {
62                     for (int i = 0; i < static_cast<int>(ARRAY_SIZE(planeMaskNames)); i++) {
63                         for (auto &drmEnum : prop.enums) {
64                             if (!strncmp(drmEnum.name.c_str(), (const char *)planeMaskNames[i].name,
65                                          strlen(drmEnum.name.c_str())) &&
66                                 (prop.value & (1LL << drmEnum.value)) > 0) {
67                                 mPlaneMask |= static_cast<int>(planeMaskNames[i].mask);
68                                 DISPLAY_LOGI("crtc id %{public}d, plane name %{public}s value %{public}llx", GetId(),
69                                              (const char *)planeMaskNames[i].name, (long long)planeMaskNames[i].mask);
70                             }
71                         }
72                     }
73                 }
74                 return DISPLAY_SUCCESS;
75             }
76 
BindToDisplay(uint32_t id)77             int32_t DrmCrtc::BindToDisplay(uint32_t id)
78             {
79                 DISPLAY_CHK_RETURN((mDisplayId != INVALIDE_DISPLAY_ID), DISPLAY_FAILURE,
80                                    DISPLAY_LOGE("the crtc has bind to %{public}d", mDisplayId));
81                 mDisplayId = id;
82                 return DISPLAY_SUCCESS;
83             }
84 
UnBindDisplay(uint32_t id)85             void DrmCrtc::UnBindDisplay(uint32_t id)
86             {
87                 DISPLAY_DEBUGLOG();
88                 if (mDisplayId == id) {
89                     mDisplayId = INVALIDE_DISPLAY_ID;
90                 } else {
91                     DISPLAY_LOGE("can not unbind");
92                 }
93             }
94 
CanBind()95             bool DrmCrtc::CanBind()
96             {
97                 return (mDisplayId == INVALIDE_DISPLAY_ID);
98             }
99 
SetActivieMode(int32_t id)100             int32_t DrmCrtc::SetActivieMode(int32_t id)
101             {
102                 DISPLAY_DEBUGLOG("set activie modeid to %{public}d", id);
103                 if (mActiveModeId != id) {
104                     mNeedModeSet = true;
105                 }
106                 mActiveModeId = id;
107                 return DISPLAY_SUCCESS;
108             }
109         } // namespace OHOS
110     }     // namespace HDI
111 } // namespace DISPLAY
112