1 /*
2  * Copyright (c) 2021-2023 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_encoder.h"
17 
18 namespace OHOS {
19 namespace HDI {
20 namespace DISPLAY {
DrmEncoder(drmModeEncoder e)21 DrmEncoder::DrmEncoder(drmModeEncoder e)
22 {
23     mEncoderId = e.encoder_id;
24     mCrtcId = e.crtc_id;
25     mPossibleCrtcs = e.possible_crtcs;
26 }
27 
PickIdleCrtcId(IdMapPtr<DrmCrtc> & crtcs,uint32_t & crtcId)28 int32_t DrmEncoder::PickIdleCrtcId(IdMapPtr<DrmCrtc> &crtcs, uint32_t &crtcId)
29 {
30     // find the crtc id;
31     DISPLAY_LOGD("crtcs szie %{public}zu", crtcs.size());
32     std::shared_ptr<DrmCrtc> crtc;
33     auto crtcIter = crtcs.find(mCrtcId);
34     if (crtcIter == crtcs.end()) {
35         DISPLAY_LOGW("can not find crtc for id %{public}d", mCrtcId);
36         for (crtcIter = crtcs.begin(); crtcIter != crtcs.end(); ++crtcIter) {
37             auto &posCrts = crtcIter->second;
38             if (mPossibleCrtcs == (1<<posCrts->GetPipe())) {
39                 DISPLAY_LOGD("find crtc id %{public}d, pipe %{public}d", posCrts->GetId(), posCrts->GetPipe());
40                 break;
41             }
42         }
43     }
44     DISPLAY_CHK_RETURN((crtcIter == crtcs.end()), DISPLAY_FAILURE,
45         DISPLAY_LOGE("have no crtc %{public}zu ", crtcs.size()));
46     crtc = crtcIter->second;
47     DISPLAY_CHK_RETURN((crtc == nullptr), DISPLAY_FAILURE, DISPLAY_LOGE("crtc is null"));
48 
49     if (!crtc->CanBind()) {
50         crtc = nullptr;
51         for (const auto &posCrtcPair : crtcs) {
52             auto &posCrts = posCrtcPair.second;
53             DISPLAY_LOGD("try crtc id : %{public}d", posCrts->GetId());
54             if (posCrts->CanBind() && ((1 << posCrts->GetPipe()) & mPossibleCrtcs)) {
55                 crtc = posCrts;
56             }
57         }
58     }
59     DISPLAY_CHK_RETURN((crtc == nullptr), DISPLAY_FAILURE,
60         DISPLAY_LOGE("encoder %{public}d can not bind to idle crtc", mEncoderId));
61     crtcId = crtc->GetId();
62     return DISPLAY_SUCCESS;
63 }
64 } // namespace OHOS
65 } // namespace HDI
66 } // namespace DISPLAY
67