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