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_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 size %{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         crtcIter = crtcs.begin();
37     }
38     DISPLAY_CHK_RETURN((crtcIter == crtcs.end()), DISPLAY_FAILURE,
39         DISPLAY_LOGE("have no crtc %{public}zu ", crtcs.size()));
40     crtc = crtcIter->second;
41     DISPLAY_CHK_RETURN((crtc == nullptr), DISPLAY_FAILURE, DISPLAY_LOGE("crtc is null"));
42 
43     if (!crtc->CanBind()) {
44         crtc = nullptr;
45         for (const auto &posCrtcPair : crtcs) {
46             auto &posCrts = posCrtcPair.second;
47             DISPLAY_LOGD("try crtc id : %{public}d", posCrts->GetId());
48             if (posCrts->CanBind() && ((1 << posCrts->GetPipe()) & mPossibleCrtcs)) {
49                 crtc = posCrts;
50             }
51         }
52     }
53     DISPLAY_CHK_RETURN((crtc == nullptr), DISPLAY_FAILURE,
54         DISPLAY_LOGE("encoder %{public}d can not bind to idle crtc", mEncoderId));
55     crtcId = crtc->GetId();
56     return DISPLAY_SUCCESS;
57 }
58 } // namespace OHOS
59 } // namespace HDI
60 } // namespace DISPLAY
61