• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "fbdev_driver.h"
16 
17 #include <cstdio>
18 #include <cstdlib>
19 #include <cstring>
20 #include <memory>
21 #include <fcntl.h>
22 #include <sys/ioctl.h>
23 #include <sys/mman.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 #include "log/log.h"
27 #include "securec.h"
28 #include "updater_ui_const.h"
29 
30 namespace Updater {
~FbdevDriver()31 FbdevDriver::~FbdevDriver()
32 {
33     ReleaseFb(&buff_);
34 }
35 
FBLog() const36 void FbdevDriver::FBLog() const
37 {
38     LOG(INFO) << "id=" << finfo_.id;
39     LOG(INFO) << "sem_start=" << finfo_.smem_start;
40     LOG(INFO) << "smem_len=" << finfo_.smem_len;
41     LOG(INFO) << "type=" << finfo_.type;
42     LOG(INFO) << "line_length=" << finfo_.line_length;
43     LOG(INFO) << "mmio_start=" << finfo_.mmio_start;
44     LOG(INFO) << "mmio_len=" << finfo_.mmio_len;
45     LOG(INFO) << "visual=" << finfo_.visual;
46 
47     LOG(INFO) << "The xres=" << vinfo_.xres;
48     LOG(INFO) << "The yres=" << vinfo_.yres;
49     LOG(INFO) << "xres_virtual=" << vinfo_.xres_virtual;
50     LOG(INFO) << "yres_virtual=" << vinfo_.yres_virtual;
51     LOG(INFO) << "xoffset=" << vinfo_.xoffset;
52     LOG(INFO) << "yoffset=" << vinfo_.yoffset;
53     LOG(INFO) << "bits_per_pixel is :" << vinfo_.bits_per_pixel;
54     LOG(INFO) << "red.offset=" << vinfo_.red.offset;
55     LOG(INFO) << "red.length=" << vinfo_.red.length;
56     LOG(INFO) << "red.msb_right=" << vinfo_.red.msb_right;
57     LOG(INFO) << "green.offset=" << vinfo_.green.offset;
58     LOG(INFO) << "green.length=" << vinfo_.green.length;
59     LOG(INFO) << "green.msb_right=" << vinfo_.green.msb_right;
60     LOG(INFO) << "blue.offset=" << vinfo_.blue.offset;
61     LOG(INFO) << "blue.length=" << vinfo_.blue.length;
62     LOG(INFO) << "blue.msb_right=" << vinfo_.blue.msb_right;
63     LOG(INFO) << "transp.offset=" << vinfo_.transp.offset;
64     LOG(INFO) << "transp.length=" << vinfo_.transp.length;
65     LOG(INFO) << "transp.msb_right=" << vinfo_.transp.msb_right;
66     LOG(INFO) << "height=" << vinfo_.height;
67 }
68 
Init()69 bool FbdevDriver::Init()
70 {
71     static bool res = [this] () {
72         int fd = open(FB_DEV_PATH, O_RDWR | O_CLOEXEC);
73         if (fd < 0) {
74             LOG(ERROR) << "cannot open fb0";
75             return false;
76         }
77 
78         if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo_) < 0) {
79             LOG(ERROR) << "failed to get fb0 info";
80             close(fd);
81             return false;
82         }
83 
84         if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo_) < 0) {
85             LOG(ERROR) << "failed to get fb0 info";
86             close(fd);
87             return false;
88         }
89 
90         FBLog();
91 
92         buff_.width = vinfo_.xres;
93         buff_.height = vinfo_.yres;
94         buff_.size = finfo_.line_length * vinfo_.yres;
95         buff_.vaddr = mmap(nullptr, finfo_.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
96         if (buff_.vaddr == MAP_FAILED) {
97             LOG(ERROR) << "failed to mmap framebuffer";
98             close(fd);
99             return false;
100         }
101         (void)memset_s(buff_.vaddr, finfo_.smem_len, 0, finfo_.smem_len);
102         fd_ = fd;
103         return true;
104     } ();
105     return res;
106 }
107 
Flip(const uint8_t * buf)108 void FbdevDriver::Flip(const uint8_t *buf)
109 {
110     if (fd_ < 0 || memcpy_s(buff_.vaddr, buff_.size, buf, buff_.size) != EOK) {
111         return;
112     }
113     if (ioctl(fd_, FBIOPAN_DISPLAY, &vinfo_) < 0) {
114         LOG(ERROR) << "failed to display fb0!";
115     }
116 }
117 
GetGrSurface(GrSurface & surface)118 void FbdevDriver::GetGrSurface(GrSurface &surface)
119 {
120     surface.width = static_cast<int>(vinfo_.xres);
121     surface.height = static_cast<int>(vinfo_.yres);
122     surface.rowBytes = finfo_.line_length;
123     surface.pixelBytes = vinfo_.bits_per_pixel / 8; // 8: byte bit len
124 }
125 
ReleaseFb(const struct FbBufferObject * fbo)126 void FbdevDriver::ReleaseFb(const struct FbBufferObject *fbo)
127 {
128     /*
129      * When fd_ isn't less than 0, then fbo->vaddr is valid and can by safely munmap.
130      * this can be guaranteed by FbdevDriver::Init.
131      */
132     if (fd_ < 0) {
133         return;
134     }
135     munmap(fbo->vaddr, fbo->size);
136     close(fd_);
137     fd_ = -1;
138 }
139 } // namespace Updater
140