1 /*
2  * Copyright (c) 2021-2022 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 "hdi_drm_layer.h"
17 #include <cinttypes>
18 #include <cerrno>
19 #include "drm_device.h"
20 
21 namespace OHOS {
22 namespace HDI {
23 namespace DISPLAY {
DrmGemBuffer(int drmFd, HdiLayerBuffer &hdl)24 DrmGemBuffer::DrmGemBuffer(int drmFd, HdiLayerBuffer &hdl) : mDrmFd(drmFd)
25 {
26     DISPLAY_DEBUGLOG();
27     Init(mDrmFd, hdl);
28 }
29 
Init(int drmFd, HdiLayerBuffer &hdl)30 void DrmGemBuffer::Init(int drmFd, HdiLayerBuffer &hdl)
31 {
32     int ret;
33     const int MAX_COUNT = 4;
34     uint32_t pitches[MAX_COUNT] = {0};
35     uint32_t gemHandles[MAX_COUNT] = {0};
36     uint32_t offsets[MAX_COUNT] = {0};
37     DISPLAY_DEBUGLOG("hdl %{public}" PRIx64 "", hdl.GetPhysicalAddr());
38     DISPLAY_CHK_RETURN_NOT_VALUE((drmFd < 0), DISPLAY_LOGE("can not init drmfd %{public}d", drmFd));
39     mDrmFormat = DrmDevice::ConvertToDrmFormat(static_cast<PixelFormat>(hdl.GetFormat()));
40     ret = drmPrimeFDToHandle(drmFd, hdl.GetFb(), &mGemHandle);
41     DISPLAY_CHK_RETURN_NOT_VALUE((ret != 0), DISPLAY_LOGE("can not get handle errno %{public}d", errno));
42 
43     pitches[0] = hdl.GetStride();
44     gemHandles[0] = mGemHandle;
45     offsets[0] = 0;
46     ret = drmModeAddFB2(drmFd, hdl.GetWight(), hdl.GetHeight(), mDrmFormat, gemHandles, pitches, offsets, &mFdId, 0);
47     DISPLAY_DEBUGLOG("mGemHandle %{public}d  mFdId %{public}d", mGemHandle, mFdId);
48     DISPLAY_DEBUGLOG("w: %{public}d  h: %{public}d mDrmFormat : %{public}d gemHandles: %{public}d pitches: %{public}d "
49         "offsets: %{public}d",
50         hdl.GetWight(), hdl.GetHeight(), mDrmFormat, gemHandles[0], pitches[0], offsets[0]);
51     DISPLAY_CHK_RETURN_NOT_VALUE((ret != 0), DISPLAY_LOGE("can not add fb errno %{public}d", errno));
52 }
53 
~DrmGemBuffer()54 DrmGemBuffer::~DrmGemBuffer()
55 {
56     DISPLAY_DEBUGLOG();
57     if (mFdId) {
58         if (drmModeRmFB(mDrmFd, mFdId)) {
59             DISPLAY_LOGE("can not free fdid %{public}d errno %{public}d", mFdId, errno);
60         }
61     }
62 
63     if (mGemHandle) {
64         struct drm_gem_close gemClose = { 0 };
65         gemClose.handle = mGemHandle;
66         if (drmIoctl(mDrmFd, DRM_IOCTL_GEM_CLOSE, &gemClose)) {
67             DISPLAY_DEBUGLOG("can not free gem handle %{public}d errno : %{public}d", mGemHandle, errno);
68         }
69     }
70 }
71 
IsValid()72 bool DrmGemBuffer::IsValid()
73 {
74     DISPLAY_DEBUGLOG();
75     return (mGemHandle != INVALID_DRM_ID) && (mFdId != INVALID_DRM_ID);
76 }
77 
GetGemBuffer()78 DrmGemBuffer *HdiDrmLayer::GetGemBuffer()
79 {
80     DISPLAY_DEBUGLOG();
81     std::unique_ptr<DrmGemBuffer> ptr = std::make_unique<DrmGemBuffer>(DrmDevice::GetDrmFd(), *GetCurrentBuffer());
82     mLastBuffer = std::move(mCurrentBuffer);
83     mCurrentBuffer = std::move(ptr);
84     return mCurrentBuffer.get();
85 }
86 } // namespace OHOS
87 } // namespace HDI
88 } // namespace DISPLAY
89