• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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("%{public}p", this);
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.GetWight(), hdl.GetHeight(), mDrmFormat, gemHandles, pitches, offsets, &mFdId, 0);
46     DISPLAY_LOGD("w: %{public}d  h: %{public}d mDrmFormat : %{public}d gemHandles: %{public}d pitches: %{public}d "
47         "offsets: %{public}d",
48         hdl.GetWight(), hdl.GetHeight(), mDrmFormat, gemHandles[0], pitches[0], offsets[0]);
49     DISPLAY_CHK_RETURN_NOT_VALUE((ret != 0), DISPLAY_LOGE("can not add fb errno %{public}d", errno));
50 }
51 
~DrmGemBuffer()52 DrmGemBuffer::~DrmGemBuffer()
53 {
54     DISPLAY_LOGD("%{public}p", this);
55     if (mFdId) {
56         if (drmModeRmFB(mDrmFd, mFdId)) {
57             DISPLAY_LOGE("can not free fdid %{public}d errno %{public}d", mFdId, errno);
58         }
59     }
60 
61     if (mGemHandle) {
62         struct drm_gem_close gemClose = { 0 };
63         gemClose.handle = mGemHandle;
64         if (drmIoctl(mDrmFd, DRM_IOCTL_GEM_CLOSE, &gemClose)) {
65             DISPLAY_LOGE("can not free gem handle %{public}d errno : %{public}d", mGemHandle, errno);
66         }
67     }
68 }
69 
IsValid()70 bool DrmGemBuffer::IsValid()
71 {
72     DISPLAY_LOGD();
73     return (mGemHandle != INVALID_DRM_ID) && (mFdId != INVALID_DRM_ID);
74 }
75 
GetGemBuffer()76 DrmGemBuffer *HdiDrmLayer::GetGemBuffer()
77 {
78     DISPLAY_LOGD();
79     std::unique_ptr<DrmGemBuffer> ptr = std::make_unique<DrmGemBuffer>(DrmDevice::GetDrmFd(), *GetCurrentBuffer());
80     LastBuffer = std::move(mCurrentBuffer);
81     mCurrentBuffer = std::move(ptr);
82     return mCurrentBuffer.get();
83 }
84 } // namespace OHOS
85 } // namespace HDI
86 } // namespace DISPLAY
87