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 "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_LOGD();
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_CHK_RETURN_NOT_VALUE((drmFd < 0), DISPLAY_LOGE("can not init drmfd %{public}d", drmFd));
38 mDrmFormat = DrmDevice::ConvertToDrmFormat(static_cast<PixelFormat>(hdl.GetFormat()));
39 ret = drmPrimeFDToHandle(drmFd, hdl.GetFb(), &mGemHandle);
40 DISPLAY_CHK_RETURN_NOT_VALUE((ret != 0), DISPLAY_LOGE("can not get handle errno %{public}d", errno));
41
42 pitches[0] = hdl.GetStride();
43 gemHandles[0] = mGemHandle;
44 offsets[0] = 0;
45 ret = drmModeAddFB2(drmFd, hdl.GetWidth(), hdl.GetHeight(), mDrmFormat, gemHandles, pitches, offsets, &mFdId, 0);
46 DISPLAY_LOGD("mGemHandle %{public}d mFdId %{public}d", mGemHandle, mFdId);
47 DISPLAY_LOGD("w: %{public}d h: %{public}d mDrmFormat : %{public}d gemHandles: %{public}d pitches: %{public}d "
48 "offsets: %{public}d",
49 hdl.GetWidth(), hdl.GetHeight(), mDrmFormat, gemHandles[0], pitches[0], offsets[0]);
50 DISPLAY_CHK_RETURN_NOT_VALUE((ret != 0), DISPLAY_LOGE("can not add fb errno %{public}d", errno));
51 }
52
~DrmGemBuffer()53 DrmGemBuffer::~DrmGemBuffer()
54 {
55 DISPLAY_LOGD();
56 if (mFdId) {
57 if (drmModeRmFB(mDrmFd, mFdId)) {
58 DISPLAY_LOGE("can not free fdid %{public}d errno %{public}d", mFdId, errno);
59 }
60 }
61
62 if (mGemHandle) {
63 struct drm_gem_close gemClose = { 0 };
64 gemClose.handle = mGemHandle;
65 if (drmIoctl(mDrmFd, DRM_IOCTL_GEM_CLOSE, &gemClose)) {
66 DISPLAY_LOGE("can not free gem handle %{public}d errno : %{public}d", mGemHandle, errno);
67 }
68 }
69 }
70
IsValid() const71 bool DrmGemBuffer::IsValid() const
72 {
73 DISPLAY_LOGD();
74 return (mGemHandle != INVALID_DRM_ID) && (mFdId != INVALID_DRM_ID);
75 }
76
GetGemBuffer()77 DrmGemBuffer *HdiDrmLayer::GetGemBuffer()
78 {
79 DISPLAY_LOGD();
80 std::unique_ptr<DrmGemBuffer> ptr = std::make_unique<DrmGemBuffer>(DrmDevice::GetDrmFd(), *GetCurrentBuffer());
81 lastBuffer_ = std::move(mCurrentBuffer);
82 mCurrentBuffer = std::move(ptr);
83 return mCurrentBuffer.get();
84 }
85 } // namespace OHOS
86 } // namespace HDI
87 } // namespace DISPLAY
88