1 /*
2  * Copyright (c) 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 #include "framebuffer_allocator.h"
16 #include <fcntl.h>
17 #include <cerrno>
18 #include <securec.h>
19 #include <linux/fb.h>
20 #include "display_common.h"
21 #include "display_adapter.h"
22 namespace OHOS {
23 namespace HDI {
24 namespace DISPLAY {
SetFdFormatAndVirtualRes(struct fb_var_screeninfo varInfo)25 int32_t FramebufferAllocator::SetFdFormatAndVirtualRes(struct fb_var_screeninfo varInfo)
26 {
27     int ret;
28     DISPLAY_LOGD();
29     varInfo.reserved[0] = 0;
30     varInfo.reserved[1] = 0;
31     varInfo.reserved[2] = 0; // init reserved[2] is 0
32     varInfo.xoffset = 0;
33     varInfo.yoffset = 0;
34     varInfo.activate = FB_ACTIVATE_NOW;
35 
36     varInfo.bits_per_pixel = 32; // 32bit
37     varInfo.transp.length = 8; // 8 bits
38     varInfo.red.length = 8; // 8 bits
39     varInfo.green.length = 8; // 8 bits
40     varInfo.blue.length = 8; // 8 bits
41 
42     varInfo.transp.offset = 24; // offset 24 of a channel
43     varInfo.red.offset = 0; // offset 16 of red channel
44     varInfo.green.offset = 8; // offset 8 of green channel
45     varInfo.blue.offset = 16; // offset 16 of blue channel
46 
47     varInfo.xres_virtual = varInfo.xres;
48     varInfo.yres_virtual = varInfo.yres * FB_BUFFERS_NUM;
49 
50     // set framebuffer vary info
51     ret = DisplayAdapter::GetInstance()->Ioctl(deviceFd_, FBIOPUT_VSCREENINFO, &varInfo);
52     DISPLAY_CHK_RETURN((ret < 0), DISPLAY_FAILURE, DISPLAY_LOGE("FBIOPUT_VSCREENINFO failed errno : %d", errno));
53     return DISPLAY_SUCCESS;
54 }
55 
InitFb()56 int32_t FramebufferAllocator::InitFb()
57 {
58     int ret;
59     DISPLAY_LOGD();
60     ret = DisplayAdapter::GetInstance()->Ioctl(deviceFd_, FBIOGET_FSCREENINFO, &fixInfo_);
61     DISPLAY_CHK_RETURN((ret < 0), DISPLAY_FAILURE, DISPLAY_LOGE("FBIOGET_FSCREENINFO failed errno : %d", errno));
62 
63     ret = DisplayAdapter::GetInstance()->Ioctl(deviceFd_, FBIOGET_VSCREENINFO, &varInfo_);
64     DISPLAY_CHK_RETURN((ret < 0), DISPLAY_FAILURE, DISPLAY_LOGE("FBIOGET_VSCREENINFO failed errno : %d", errno));
65 
66     ret = SetFdFormatAndVirtualRes(varInfo_);
67     DISPLAY_CHK_RETURN((ret < 0), DISPLAY_FAILURE, DISPLAY_LOGE(" failed to set fb format errno : %d", errno));
68 
69     // update the variable screen information
70     ret = DisplayAdapter::GetInstance()->Ioctl(deviceFd_, FBIOGET_VSCREENINFO, &varInfo_);
71     DISPLAY_CHK_RETURN((ret < 0), DISPLAY_FAILURE, DISPLAY_LOGE("FBIOGET_VSCREENINFO failed errno : %d", errno));
72 
73     size_t frameBufferSize = fixInfo_.line_length * varInfo_.yres_virtual;
74 
75     frameBufferSize_ = frameBufferSize;
76     DISPLAY_CHK_RETURN((varInfo_.yres == 0), DISPLAY_FAILURE, DISPLAY_LOGE("the yres is zero"));
77     buffersNum_ = varInfo_.yres_virtual / varInfo_.yres;
78     bufferSize_ = fixInfo_.line_length * varInfo_.yres;
79     smemStart_ = fixInfo_.smem_start;
80     dmaBufferFb_ = DisplayAdapter::GetInstance()->FbGetDmaBuffer(deviceFd_);
81     DISPLAY_LOGD("the dmabuffer fd of framebuffer is %{public}d", dmaBufferFb_);
82     for (uint32_t i = 0; i < buffersNum_; i++) {
83         freeBuffers_.push(smemStart_ + i * bufferSize_);
84     }
85     DISPLAY_LOGD();
86     return DISPLAY_SUCCESS;
87 }
88 
Init()89 int32_t FramebufferAllocator::Init()
90 {
91     DISPLAY_LOGD();
92     if (DisplayAdapter::GetInstance() == nullptr) {
93         DISPLAY_LOGE("framebuffer allocator must depend on the display adapter");
94         return DISPLAY_FAILURE;
95     }
96     deviceFd_ = DisplayAdapter::GetInstance()->OpenDevice(FBDEV_PATH, O_RDWR, 0);
97     if (deviceFd_ < 0) {
98         DISPLAY_LOGE("Failed to open fbdev %{public}d", deviceFd_);
99         return DISPLAY_FAILURE;
100     }
101     return InitFb();
102 }
103 
Allocate(const BufferInfo & bufferInfo,BufferHandle & handle)104 int32_t FramebufferAllocator::Allocate(const BufferInfo &bufferInfo, BufferHandle &handle)
105 {
106     DISPLAY_LOGD();
107     if (freeBuffers_.empty()) {
108         DISPLAY_LOGE("has no memory");
109         return DISPLAY_NOMEM;
110     }
111     unsigned long smemAddress = freeBuffers_.front();
112     uint32_t offset = smemAddress - smemStart_;
113 
114     handle.phyAddr = smemAddress;
115     handle.fd = DisplayAdapter::GetInstance()->FbGetDmaBuffer(deviceFd_);
116     DISPLAY_LOGD("fd is %{public}d", handle.fd);
117     // temp for use key to save offset
118     handle.key = offset;
119     handle.format = PIXEL_FMT_RGBA_8888;
120     handle.width = bufferInfo.width_;
121     handle.height = bufferInfo.height_;
122     handle.stride = fixInfo_.line_length;
123     handle.size = bufferSize_;
124     handle.usage = bufferInfo.usage_;
125     freeBuffers_.pop();
126     return DISPLAY_SUCCESS;
127 }
128 
~FramebufferAllocator()129 FramebufferAllocator::~FramebufferAllocator()
130 {
131     DISPLAY_LOGD();
132     if (deviceFd_ > -1) {
133         DisplayAdapter::GetInstance()->CloseDevice(deviceFd_);
134         deviceFd_ = -1;
135     }
136 }
137 
Mmap(BufferHandle & handle)138 void* FramebufferAllocator::Mmap(BufferHandle &handle)
139 {
140     DISPLAY_LOGD();
141     void *virAddr = nullptr;
142     if (handle.virAddr != nullptr) {
143         DISPLAY_LOGW("the buffer has virtual addr");
144         return handle.virAddr;
145     }
146 
147     if (handle.fd > -1) {
148         virAddr = mmap(nullptr, frameBufferSize_, PROT_READ | PROT_WRITE, MAP_SHARED, handle.fd, 0);
149     } else {
150         virAddr = mmap(nullptr, frameBufferSize_, PROT_READ | PROT_WRITE, MAP_SHARED, deviceFd_, 0);
151     }
152     if (virAddr == MAP_FAILED) {
153         DISPLAY_LOGE("failed to mmap errno %{public}d", errno);
154         handle.virAddr = nullptr;
155         return nullptr;
156     }
157     handle.virAddr = static_cast<uint8_t*>(virAddr) + handle.key;
158 
159     return handle.virAddr;
160 }
161 
Unmap(BufferHandle & handle)162 int32_t FramebufferAllocator::Unmap(BufferHandle &handle)
163 {
164     DISPLAY_LOGD();
165     if (handle.virAddr == nullptr) {
166         DISPLAY_LOGE("virAddr is nullptr , has not map the buffer");
167         return DISPLAY_PARAM_ERR;
168     }
169     int ret = munmap(static_cast<uint8_t*>(handle.virAddr) - handle.key, frameBufferSize_);
170     if (ret != 0) {
171         DISPLAY_LOGE("munmap failed err: %{public}s", strerror(errno));
172         return DISPLAY_FAILURE;
173     }
174     handle.virAddr = nullptr;
175     return DISPLAY_SUCCESS;
176 }
FreeMem(BufferHandle * handle)177 int32_t FramebufferAllocator::FreeMem(BufferHandle *handle)
178 {
179     DISPLAY_CHK_RETURN((handle == nullptr), DISPLAY_NULL_PTR, DISPLAY_LOGE("handle is nullptr"));
180     freeBuffers_.push(handle->phyAddr);
181     Allocator::FreeMem(handle);
182     return DISPLAY_SUCCESS;
183 }
184 }
185 }
186 }
187