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