1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include "drm_plane.h"
17094332d3Sopenharmony_ci#include "drm_device.h"
18094332d3Sopenharmony_ci
19094332d3Sopenharmony_cinamespace OHOS {
20094332d3Sopenharmony_cinamespace HDI {
21094332d3Sopenharmony_cinamespace DISPLAY {
22094332d3Sopenharmony_cistruct PlaneTypeName planeTypeNames[] = {
23094332d3Sopenharmony_ci    { DrmPlaneType::DRM_PLANE_TYPE_CLUSTER0_WIN0, "Cluster0-win0" },
24094332d3Sopenharmony_ci    { DrmPlaneType::DRM_PLANE_TYPE_CLUSTER0_WIN1, "Cluster0-win1" },
25094332d3Sopenharmony_ci    { DrmPlaneType::DRM_PLANE_TYPE_CLUSTER1_WIN0, "Cluster1-win0" },
26094332d3Sopenharmony_ci    { DrmPlaneType::DRM_PLANE_TYPE_CLUSTER1_WIN1, "Cluster1-win1" },
27094332d3Sopenharmony_ci
28094332d3Sopenharmony_ci    { DrmPlaneType::DRM_PLANE_TYPE_ESMART0_WIN0, "Esmart0-win0" },
29094332d3Sopenharmony_ci    { DrmPlaneType::DRM_PLANE_TYPE_ESMART0_WIN1, "Esmart0-win1" },
30094332d3Sopenharmony_ci    { DrmPlaneType::DRM_PLANE_TYPE_ESMART0_WIN2, "Esmart0-win2" },
31094332d3Sopenharmony_ci    { DrmPlaneType::DRM_PLANE_TYPE_ESMART0_WIN3, "Esmart0-win3" },
32094332d3Sopenharmony_ci
33094332d3Sopenharmony_ci    { DrmPlaneType::DRM_PLANE_TYPE_ESMART1_WIN0, "Esmart1-win0" },
34094332d3Sopenharmony_ci    { DrmPlaneType::DRM_PLANE_TYPE_ESMART1_WIN1, "Esmart1-win1" },
35094332d3Sopenharmony_ci    { DrmPlaneType::DRM_PLANE_TYPE_ESMART1_WIN2, "Esmart1-win2" },
36094332d3Sopenharmony_ci    { DrmPlaneType::DRM_PLANE_TYPE_ESMART1_WIN3, "Esmart1-win3" },
37094332d3Sopenharmony_ci
38094332d3Sopenharmony_ci    { DrmPlaneType::DRM_PLANE_TYPE_SMART0_WIN0, "Smart0-win0" },
39094332d3Sopenharmony_ci    { DrmPlaneType::DRM_PLANE_TYPE_SMART0_WIN1, "Smart0-win1" },
40094332d3Sopenharmony_ci    { DrmPlaneType::DRM_PLANE_TYPE_SMART0_WIN2, "Smart0-win2" },
41094332d3Sopenharmony_ci    { DrmPlaneType::DRM_PLANE_TYPE_SMART0_WIN3, "Smart0-win3" },
42094332d3Sopenharmony_ci
43094332d3Sopenharmony_ci    { DrmPlaneType::DRM_PLANE_TYPE_SMART1_WIN0, "Smart1-win0" },
44094332d3Sopenharmony_ci    { DrmPlaneType::DRM_PLANE_TYPE_SMART1_WIN1, "Smart1-win1" },
45094332d3Sopenharmony_ci    { DrmPlaneType::DRM_PLANE_TYPE_SMART1_WIN2, "Smart1-win2" },
46094332d3Sopenharmony_ci    { DrmPlaneType::DRM_PLANE_TYPE_SMART1_WIN3, "Smart1-win3" },
47094332d3Sopenharmony_ci
48094332d3Sopenharmony_ci    { DrmPlaneType::DRM_PLANE_TYPE_Unknown, "unknown" },
49094332d3Sopenharmony_ci};
50094332d3Sopenharmony_ci
51094332d3Sopenharmony_ciDrmPlane::DrmPlane(drmModePlane &p)
52094332d3Sopenharmony_ci    : mId(p.plane_id), mPossibleCrtcs(p.possible_crtcs), mCrtcId(p.crtc_id),
53094332d3Sopenharmony_ci    mFormats(p.formats, p.formats + p.count_formats)
54094332d3Sopenharmony_ci{}
55094332d3Sopenharmony_ci
56094332d3Sopenharmony_ciDrmPlane::~DrmPlane()
57094332d3Sopenharmony_ci{
58094332d3Sopenharmony_ci    DISPLAY_LOGD();
59094332d3Sopenharmony_ci}
60094332d3Sopenharmony_ci
61094332d3Sopenharmony_ciint DrmPlane::GetCrtcProp(DrmDevice &drmDevice)
62094332d3Sopenharmony_ci{
63094332d3Sopenharmony_ci    int32_t ret;
64094332d3Sopenharmony_ci    int32_t crtc_x;
65094332d3Sopenharmony_ci    int32_t crtc_y;
66094332d3Sopenharmony_ci    int32_t crtc_w;
67094332d3Sopenharmony_ci    int32_t crtc_h;
68094332d3Sopenharmony_ci    DrmProperty prop;
69094332d3Sopenharmony_ci
70094332d3Sopenharmony_ci    ret = drmDevice.GetPlaneProperty(*this, PROP_CRTC_X_ID, prop);
71094332d3Sopenharmony_ci    DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("cat not get pane crtc_x prop id"));
72094332d3Sopenharmony_ci    mPropCrtc_xId = prop.propId;
73094332d3Sopenharmony_ci    crtc_x = prop.value;
74094332d3Sopenharmony_ci
75094332d3Sopenharmony_ci    ret = drmDevice.GetPlaneProperty(*this, PROP_CRTC_Y_ID, prop);
76094332d3Sopenharmony_ci    DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("cat not get pane crtc_y prop id"));
77094332d3Sopenharmony_ci    mPropCrtc_yId = prop.propId;
78094332d3Sopenharmony_ci    crtc_y = prop.value;
79094332d3Sopenharmony_ci
80094332d3Sopenharmony_ci    ret = drmDevice.GetPlaneProperty(*this, PROP_CRTC_W_ID, prop);
81094332d3Sopenharmony_ci    DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("cat not get pane crtc_w prop id"));
82094332d3Sopenharmony_ci    mPropCrtc_wId = prop.propId;
83094332d3Sopenharmony_ci    crtc_w = prop.value;
84094332d3Sopenharmony_ci
85094332d3Sopenharmony_ci    ret = drmDevice.GetPlaneProperty(*this, PROP_CRTC_H_ID, prop);
86094332d3Sopenharmony_ci    DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("cat not get pane crtc_h prop id"));
87094332d3Sopenharmony_ci    mPropCrtc_hId = prop.propId;
88094332d3Sopenharmony_ci    crtc_h = prop.value;
89094332d3Sopenharmony_ci
90094332d3Sopenharmony_ci    DISPLAY_LOGE("plane %{public}d crtc_x %{public}d crtc_y %{public}d crtc_w %{public}d crtc_h %{public}d",
91094332d3Sopenharmony_ci        GetId(), crtc_x, crtc_y, crtc_w, crtc_h);
92094332d3Sopenharmony_ci
93094332d3Sopenharmony_ci    return 0;
94094332d3Sopenharmony_ci}
95094332d3Sopenharmony_ci
96094332d3Sopenharmony_ciint DrmPlane::GetSrcProp(DrmDevice &drmDevice)
97094332d3Sopenharmony_ci{
98094332d3Sopenharmony_ci    int32_t ret;
99094332d3Sopenharmony_ci    int32_t src_x;
100094332d3Sopenharmony_ci    int32_t src_y;
101094332d3Sopenharmony_ci    int32_t src_w;
102094332d3Sopenharmony_ci    int32_t src_h;
103094332d3Sopenharmony_ci    DrmProperty prop;
104094332d3Sopenharmony_ci
105094332d3Sopenharmony_ci    ret = drmDevice.GetPlaneProperty(*this, PROP_SRC_X_ID, prop);
106094332d3Sopenharmony_ci    DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("cat not get pane src_x prop id"));
107094332d3Sopenharmony_ci    mPropSrc_xId = prop.propId;
108094332d3Sopenharmony_ci    src_x = prop.value;
109094332d3Sopenharmony_ci
110094332d3Sopenharmony_ci    ret = drmDevice.GetPlaneProperty(*this, PROP_SRC_Y_ID, prop);
111094332d3Sopenharmony_ci    DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("cat not get pane src_y prop id"));
112094332d3Sopenharmony_ci    mPropSrc_yId = prop.propId;
113094332d3Sopenharmony_ci    src_y = prop.value;
114094332d3Sopenharmony_ci
115094332d3Sopenharmony_ci    ret = drmDevice.GetPlaneProperty(*this, PROP_SRC_W_ID, prop);
116094332d3Sopenharmony_ci    DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("cat not get pane src_w prop id"));
117094332d3Sopenharmony_ci    mPropSrc_wId = prop.propId;
118094332d3Sopenharmony_ci    src_w = prop.value;
119094332d3Sopenharmony_ci
120094332d3Sopenharmony_ci    ret = drmDevice.GetPlaneProperty(*this, PROP_SRC_H_ID, prop);
121094332d3Sopenharmony_ci    DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("cat not get pane src_h prop id"));
122094332d3Sopenharmony_ci    mPropSrc_hId = prop.propId;
123094332d3Sopenharmony_ci    src_h = prop.value;
124094332d3Sopenharmony_ci
125094332d3Sopenharmony_ci    DISPLAY_LOGE("plane %{public}d src_x %{public}d src_y %{public}d src_w %{public}d src_h %{public}d",
126094332d3Sopenharmony_ci        GetId(), src_x, src_y, src_w, src_h);
127094332d3Sopenharmony_ci
128094332d3Sopenharmony_ci    return 0;
129094332d3Sopenharmony_ci}
130094332d3Sopenharmony_ci
131094332d3Sopenharmony_ciint DrmPlane::GetNameProp(DrmDevice &drmDevice)
132094332d3Sopenharmony_ci{
133094332d3Sopenharmony_ci    int32_t ret;
134094332d3Sopenharmony_ci    DrmProperty prop;
135094332d3Sopenharmony_ci    ret = drmDevice.GetPlaneProperty(*this, "NAME", prop);
136094332d3Sopenharmony_ci    DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("cat not get pane crtc prop id"));
137094332d3Sopenharmony_ci
138094332d3Sopenharmony_ci    uint32_t find_name = 0;
139094332d3Sopenharmony_ci    for (int i = 0; i < static_cast<int>ARRAY_SIZE(planeTypeNames); i++) {
140094332d3Sopenharmony_ci        find_name = 0;
141094332d3Sopenharmony_ci
142094332d3Sopenharmony_ci        for (auto &drmEnum : prop.enums) {
143094332d3Sopenharmony_ci            if (!strncmp(drmEnum.name.c_str(), (const char*)planeTypeNames[i].name,
144094332d3Sopenharmony_ci                                strlen(planeTypeNames[i].name))) {
145094332d3Sopenharmony_ci                find_name = (1LL << drmEnum.value);
146094332d3Sopenharmony_ci            }
147094332d3Sopenharmony_ci        }
148094332d3Sopenharmony_ci
149094332d3Sopenharmony_ci        if (find_name) {
150094332d3Sopenharmony_ci            DISPLAY_LOGI("find plane id %{public}d, type %{public}x %{public}s",
151094332d3Sopenharmony_ci                                    GetId(), planeTypeNames[i].type, planeTypeNames[i].name);
152094332d3Sopenharmony_ci            mWinType = planeTypeNames[i].type;
153094332d3Sopenharmony_ci            mName = planeTypeNames[i].name;
154094332d3Sopenharmony_ci            break;
155094332d3Sopenharmony_ci        }
156094332d3Sopenharmony_ci    }
157094332d3Sopenharmony_ci
158094332d3Sopenharmony_ci    return 0;
159094332d3Sopenharmony_ci}
160094332d3Sopenharmony_ci
161094332d3Sopenharmony_ciint32_t DrmPlane::Init(DrmDevice &drmDevice)
162094332d3Sopenharmony_ci{
163094332d3Sopenharmony_ci    DISPLAY_LOGD();
164094332d3Sopenharmony_ci    int32_t ret;
165094332d3Sopenharmony_ci    DrmProperty prop;
166094332d3Sopenharmony_ci    GetCrtcProp(drmDevice);
167094332d3Sopenharmony_ci    GetSrcProp(drmDevice);
168094332d3Sopenharmony_ci    GetNameProp(drmDevice);
169094332d3Sopenharmony_ci
170094332d3Sopenharmony_ci    ret = drmDevice.GetPlaneProperty(*this, PROP_FBID, prop);
171094332d3Sopenharmony_ci    mPropFbId = prop.propId;
172094332d3Sopenharmony_ci    DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("can not get plane fb id"));
173094332d3Sopenharmony_ci    ret = drmDevice.GetPlaneProperty(*this, PROP_IN_FENCE_FD, prop);
174094332d3Sopenharmony_ci    DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("cat not get plane in fence prop id"));
175094332d3Sopenharmony_ci    mPropFenceInId = prop.propId;
176094332d3Sopenharmony_ci    ret = drmDevice.GetPlaneProperty(*this, PROP_CRTC_ID, prop);
177094332d3Sopenharmony_ci    DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("cat not get pane crtc prop id"));
178094332d3Sopenharmony_ci    mPropCrtcId = prop.propId;
179094332d3Sopenharmony_ci
180094332d3Sopenharmony_ci    ret = drmDevice.GetPlaneProperty(*this, PROP_ZPOS_ID, prop);
181094332d3Sopenharmony_ci    DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("cat not get pane crtc prop id"));
182094332d3Sopenharmony_ci    mPropZposId = prop.propId;
183094332d3Sopenharmony_ci
184094332d3Sopenharmony_ci    ret = drmDevice.GetPlaneProperty(*this, PROP_TYPE, prop);
185094332d3Sopenharmony_ci    DISPLAY_CHK_RETURN((ret != DISPLAY_SUCCESS), DISPLAY_FAILURE, DISPLAY_LOGE("cat not get pane crtc prop id"));
186094332d3Sopenharmony_ci    switch (prop.value) {
187094332d3Sopenharmony_ci        case DRM_PLANE_TYPE_OVERLAY:
188094332d3Sopenharmony_ci        case DRM_PLANE_TYPE_PRIMARY:
189094332d3Sopenharmony_ci        case DRM_PLANE_TYPE_CURSOR:
190094332d3Sopenharmony_ci            mType = static_cast<uint32_t>(prop.value);
191094332d3Sopenharmony_ci            break;
192094332d3Sopenharmony_ci        default:
193094332d3Sopenharmony_ci            DISPLAY_LOGE("unknown type value %{public}" PRIu64 "", prop.value);
194094332d3Sopenharmony_ci            return DISPLAY_FAILURE;
195094332d3Sopenharmony_ci    }
196094332d3Sopenharmony_ci    return DISPLAY_SUCCESS;
197094332d3Sopenharmony_ci}
198094332d3Sopenharmony_ci} // namespace OHOS
199094332d3Sopenharmony_ci} // namespace HDI
200094332d3Sopenharmony_ci} // namespace DISPLAY
201