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