Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Mark Salyzyn | d88dfe8 | 2017-04-11 08:56:09 -0700 | [diff] [blame] | 17 | #include <dlfcn.h> |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/ioctl.h> |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 23 | #include <sys/mman.h> |
| 24 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 25 | #include <cutils/ashmem.h> |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 26 | #include <cutils/atomic.h> |
Mark Salyzyn | d88dfe8 | 2017-04-11 08:56:09 -0700 | [diff] [blame] | 27 | #include <log/log.h> |
| 28 | |
| 29 | #include <hardware/gralloc.h> |
| 30 | #include <hardware/hardware.h> |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 31 | |
Elliott Hughes | ca6ccd9 | 2015-08-12 16:42:13 -0700 | [diff] [blame] | 32 | #ifdef __ANDROID__ |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 33 | #include <linux/fb.h> |
| 34 | #endif |
| 35 | |
| 36 | #include "gralloc_priv.h" |
Mathias Agopian | fc05413 | 2009-08-18 17:35:44 -0700 | [diff] [blame] | 37 | #include "gr.h" |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 38 | |
| 39 | /*****************************************************************************/ |
| 40 | |
Bernhard Rosenkraenzer | 5641642 | 2014-02-25 13:50:34 +0530 | [diff] [blame] | 41 | // Set TARGET_USE_PAN_DISPLAY to true at compile time if the |
| 42 | // board uses FBIOPAN_DISPLAY to setup page flipping, otherwise |
| 43 | // default ioctl to do page-flipping is FBIOPUT_VSCREENINFO. |
| 44 | #ifndef USE_PAN_DISPLAY |
| 45 | #define USE_PAN_DISPLAY 0 |
| 46 | #endif |
| 47 | |
YiMing Tseng | 0b5ab22 | 2021-11-11 14:05:30 +0800 | [diff] [blame^] | 48 | // Enabling page flipping by default |
| 49 | #ifndef NUM_BUFFERS |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 50 | #define NUM_BUFFERS 2 |
YiMing Tseng | 0b5ab22 | 2021-11-11 14:05:30 +0800 | [diff] [blame^] | 51 | #endif |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 52 | |
| 53 | |
| 54 | enum { |
| 55 | PAGE_FLIP = 0x00000001, |
| 56 | LOCKED = 0x00000002 |
| 57 | }; |
| 58 | |
| 59 | struct fb_context_t { |
| 60 | framebuffer_device_t device; |
| 61 | }; |
| 62 | |
| 63 | /*****************************************************************************/ |
| 64 | |
| 65 | static int fb_setSwapInterval(struct framebuffer_device_t* dev, |
| 66 | int interval) |
| 67 | { |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 68 | if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval) |
| 69 | return -EINVAL; |
| 70 | // FIXME: implement fb_setSwapInterval |
| 71 | return 0; |
| 72 | } |
| 73 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 74 | static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer) |
| 75 | { |
| 76 | if (private_handle_t::validate(buffer) < 0) |
| 77 | return -EINVAL; |
| 78 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 79 | private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(buffer); |
| 80 | private_module_t* m = reinterpret_cast<private_module_t*>( |
| 81 | dev->common.module); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 82 | |
| 83 | if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) { |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 84 | const size_t offset = hnd->base - m->framebuffer->base; |
| 85 | m->info.activate = FB_ACTIVATE_VBL; |
| 86 | m->info.yoffset = offset / m->finfo.line_length; |
| 87 | if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) { |
Steve Block | 60d056b | 2012-01-08 10:17:53 +0000 | [diff] [blame] | 88 | ALOGE("FBIOPUT_VSCREENINFO failed"); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 89 | m->base.unlock(&m->base, buffer); |
| 90 | return -errno; |
| 91 | } |
| 92 | m->currentBuffer = buffer; |
| 93 | |
| 94 | } else { |
| 95 | // If we can't do the page_flip, just copy the buffer to the front |
| 96 | // FIXME: use copybit HAL instead of memcpy |
| 97 | |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 98 | void* fb_vaddr; |
| 99 | void* buffer_vaddr; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 100 | |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 101 | m->base.lock(&m->base, m->framebuffer, |
| 102 | GRALLOC_USAGE_SW_WRITE_RARELY, |
| 103 | 0, 0, m->info.xres, m->info.yres, |
| 104 | &fb_vaddr); |
| 105 | |
| 106 | m->base.lock(&m->base, buffer, |
| 107 | GRALLOC_USAGE_SW_READ_RARELY, |
| 108 | 0, 0, m->info.xres, m->info.yres, |
| 109 | &buffer_vaddr); |
| 110 | |
| 111 | memcpy(fb_vaddr, buffer_vaddr, m->finfo.line_length * m->info.yres); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 112 | |
| 113 | m->base.unlock(&m->base, buffer); |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 114 | m->base.unlock(&m->base, m->framebuffer); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | /*****************************************************************************/ |
| 121 | |
Peter Collingbourne | f2ed293 | 2019-10-15 17:29:20 -0700 | [diff] [blame] | 122 | int mapFrameBufferLocked(struct private_module_t* module, int format) |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 123 | { |
| 124 | // already initialized... |
| 125 | if (module->framebuffer) { |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | char const * const device_template[] = { |
| 130 | "/dev/graphics/fb%u", |
| 131 | "/dev/fb%u", |
| 132 | 0 }; |
| 133 | |
| 134 | int fd = -1; |
| 135 | int i=0; |
| 136 | char name[64]; |
| 137 | |
| 138 | while ((fd==-1) && device_template[i]) { |
| 139 | snprintf(name, 64, device_template[i], 0); |
| 140 | fd = open(name, O_RDWR, 0); |
| 141 | i++; |
| 142 | } |
| 143 | if (fd < 0) |
| 144 | return -errno; |
| 145 | |
| 146 | struct fb_fix_screeninfo finfo; |
| 147 | if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) |
| 148 | return -errno; |
| 149 | |
| 150 | struct fb_var_screeninfo info; |
| 151 | if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) |
| 152 | return -errno; |
| 153 | |
| 154 | info.reserved[0] = 0; |
| 155 | info.reserved[1] = 0; |
| 156 | info.reserved[2] = 0; |
| 157 | info.xoffset = 0; |
| 158 | info.yoffset = 0; |
| 159 | info.activate = FB_ACTIVATE_NOW; |
| 160 | |
| 161 | /* |
YiMing Tseng | 0b5ab22 | 2021-11-11 14:05:30 +0800 | [diff] [blame^] | 162 | * Request NUM_BUFFERS screens |
| 163 | * To enable page flipping, NUM_BUFFERS should be at least 2. |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 164 | */ |
| 165 | info.yres_virtual = info.yres * NUM_BUFFERS; |
| 166 | |
Peter Collingbourne | f2ed293 | 2019-10-15 17:29:20 -0700 | [diff] [blame] | 167 | switch (format) { |
| 168 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 169 | info.bits_per_pixel = 32; |
| 170 | info.red.offset = 0; |
| 171 | info.red.length = 8; |
| 172 | info.green.offset = 8; |
| 173 | info.green.length = 8; |
| 174 | info.blue.offset = 16; |
| 175 | info.blue.length = 8; |
| 176 | break; |
| 177 | default: |
| 178 | ALOGW("unknown format: %d", format); |
| 179 | break; |
| 180 | } |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 181 | |
| 182 | uint32_t flags = PAGE_FLIP; |
Bernhard Rosenkraenzer | 5641642 | 2014-02-25 13:50:34 +0530 | [diff] [blame] | 183 | #if USE_PAN_DISPLAY |
| 184 | if (ioctl(fd, FBIOPAN_DISPLAY, &info) == -1) { |
| 185 | ALOGW("FBIOPAN_DISPLAY failed, page flipping not supported"); |
| 186 | #else |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 187 | if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) { |
Bernhard Rosenkraenzer | 5641642 | 2014-02-25 13:50:34 +0530 | [diff] [blame] | 188 | ALOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported"); |
| 189 | #endif |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 190 | info.yres_virtual = info.yres; |
| 191 | flags &= ~PAGE_FLIP; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | if (info.yres_virtual < info.yres * 2) { |
| 195 | // we need at least 2 for page-flipping |
| 196 | info.yres_virtual = info.yres; |
| 197 | flags &= ~PAGE_FLIP; |
Steve Block | 22d3513 | 2012-01-05 23:27:52 +0000 | [diff] [blame] | 198 | ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)", |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 199 | info.yres_virtual, info.yres*2); |
| 200 | } |
| 201 | |
| 202 | if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) |
| 203 | return -errno; |
| 204 | |
David 'Digit' Turner | 80d3699 | 2011-01-15 21:06:12 +0100 | [diff] [blame] | 205 | uint64_t refreshQuotient = |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 206 | ( |
| 207 | uint64_t( info.upper_margin + info.lower_margin + info.yres ) |
| 208 | * ( info.left_margin + info.right_margin + info.xres ) |
| 209 | * info.pixclock |
| 210 | ); |
| 211 | |
David 'Digit' Turner | 80d3699 | 2011-01-15 21:06:12 +0100 | [diff] [blame] | 212 | /* Beware, info.pixclock might be 0 under emulation, so avoid a |
| 213 | * division-by-0 here (SIGFPE on ARM) */ |
| 214 | int refreshRate = refreshQuotient > 0 ? (int)(1000000000000000LLU / refreshQuotient) : 0; |
| 215 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 216 | if (refreshRate == 0) { |
| 217 | // bleagh, bad info from the driver |
| 218 | refreshRate = 60*1000; // 60 Hz |
| 219 | } |
| 220 | |
| 221 | if (int(info.width) <= 0 || int(info.height) <= 0) { |
| 222 | // the driver doesn't return that information |
| 223 | // default to 160 dpi |
| 224 | info.width = ((info.xres * 25.4f)/160.0f + 0.5f); |
| 225 | info.height = ((info.yres * 25.4f)/160.0f + 0.5f); |
| 226 | } |
| 227 | |
| 228 | float xdpi = (info.xres * 25.4f) / info.width; |
| 229 | float ydpi = (info.yres * 25.4f) / info.height; |
| 230 | float fps = refreshRate / 1000.0f; |
| 231 | |
Steve Block | b224b78 | 2012-01-04 20:07:12 +0000 | [diff] [blame] | 232 | ALOGI( "using (fd=%d)\n" |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 233 | "id = %s\n" |
| 234 | "xres = %d px\n" |
| 235 | "yres = %d px\n" |
| 236 | "xres_virtual = %d px\n" |
| 237 | "yres_virtual = %d px\n" |
| 238 | "bpp = %d\n" |
| 239 | "r = %2u:%u\n" |
| 240 | "g = %2u:%u\n" |
| 241 | "b = %2u:%u\n", |
| 242 | fd, |
| 243 | finfo.id, |
| 244 | info.xres, |
| 245 | info.yres, |
| 246 | info.xres_virtual, |
| 247 | info.yres_virtual, |
| 248 | info.bits_per_pixel, |
| 249 | info.red.offset, info.red.length, |
| 250 | info.green.offset, info.green.length, |
| 251 | info.blue.offset, info.blue.length |
| 252 | ); |
| 253 | |
Steve Block | b224b78 | 2012-01-04 20:07:12 +0000 | [diff] [blame] | 254 | ALOGI( "width = %d mm (%f dpi)\n" |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 255 | "height = %d mm (%f dpi)\n" |
| 256 | "refresh rate = %.2f Hz\n", |
| 257 | info.width, xdpi, |
| 258 | info.height, ydpi, |
| 259 | fps |
| 260 | ); |
| 261 | |
| 262 | |
| 263 | if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) |
| 264 | return -errno; |
| 265 | |
| 266 | if (finfo.smem_len <= 0) |
| 267 | return -errno; |
| 268 | |
| 269 | |
| 270 | module->flags = flags; |
| 271 | module->info = info; |
| 272 | module->finfo = finfo; |
| 273 | module->xdpi = xdpi; |
| 274 | module->ydpi = ydpi; |
| 275 | module->fps = fps; |
| 276 | |
| 277 | /* |
| 278 | * map the framebuffer |
| 279 | */ |
| 280 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 281 | size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres_virtual); |
Mathias Agopian | f96b206 | 2009-12-14 18:27:09 -0800 | [diff] [blame] | 282 | module->framebuffer = new private_handle_t(dup(fd), fbSize, 0); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 283 | |
| 284 | module->numBuffers = info.yres_virtual / info.yres; |
| 285 | module->bufferMask = 0; |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 286 | |
Mathias Agopian | 5115665 | 2009-06-09 18:55:49 -0700 | [diff] [blame] | 287 | void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); |
| 288 | if (vaddr == MAP_FAILED) { |
Steve Block | 60d056b | 2012-01-08 10:17:53 +0000 | [diff] [blame] | 289 | ALOGE("Error mapping the framebuffer (%s)", strerror(errno)); |
Mathias Agopian | 5115665 | 2009-06-09 18:55:49 -0700 | [diff] [blame] | 290 | return -errno; |
| 291 | } |
| 292 | module->framebuffer->base = intptr_t(vaddr); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 293 | memset(vaddr, 0, fbSize); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | static int mapFrameBuffer(struct private_module_t* module) |
| 298 | { |
| 299 | pthread_mutex_lock(&module->lock); |
Peter Collingbourne | f2ed293 | 2019-10-15 17:29:20 -0700 | [diff] [blame] | 300 | // Request RGBA8888 because the platform assumes support for RGBA8888. |
| 301 | int err = mapFrameBufferLocked(module, HAL_PIXEL_FORMAT_RGBA_8888); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 302 | pthread_mutex_unlock(&module->lock); |
| 303 | return err; |
| 304 | } |
| 305 | |
| 306 | /*****************************************************************************/ |
| 307 | |
| 308 | static int fb_close(struct hw_device_t *dev) |
| 309 | { |
| 310 | fb_context_t* ctx = (fb_context_t*)dev; |
| 311 | if (ctx) { |
| 312 | free(ctx); |
| 313 | } |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | int fb_device_open(hw_module_t const* module, const char* name, |
| 318 | hw_device_t** device) |
| 319 | { |
| 320 | int status = -EINVAL; |
| 321 | if (!strcmp(name, GRALLOC_HARDWARE_FB0)) { |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 322 | /* initialize our state here */ |
| 323 | fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev)); |
| 324 | memset(dev, 0, sizeof(*dev)); |
| 325 | |
| 326 | /* initialize the procs */ |
| 327 | dev->device.common.tag = HARDWARE_DEVICE_TAG; |
| 328 | dev->device.common.version = 0; |
| 329 | dev->device.common.module = const_cast<hw_module_t*>(module); |
| 330 | dev->device.common.close = fb_close; |
| 331 | dev->device.setSwapInterval = fb_setSwapInterval; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 332 | dev->device.post = fb_post; |
Mathias Agopian | d28d7e8 | 2009-07-07 12:20:31 -0700 | [diff] [blame] | 333 | dev->device.setUpdateRect = 0; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 334 | |
| 335 | private_module_t* m = (private_module_t*)module; |
| 336 | status = mapFrameBuffer(m); |
| 337 | if (status >= 0) { |
| 338 | int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3); |
David 'Digit' Turner | 25b68b5 | 2011-01-16 19:52:22 +0100 | [diff] [blame] | 339 | int format = (m->info.bits_per_pixel == 32) |
Bernhard Rosenkraenzer | 5e3ac6b | 2014-02-21 01:38:44 +0530 | [diff] [blame] | 340 | ? (m->info.red.offset ? HAL_PIXEL_FORMAT_BGRA_8888 : HAL_PIXEL_FORMAT_RGBX_8888) |
David 'Digit' Turner | 25b68b5 | 2011-01-16 19:52:22 +0100 | [diff] [blame] | 341 | : HAL_PIXEL_FORMAT_RGB_565; |
Mathias Agopian | 295190f | 2009-05-05 18:30:52 -0700 | [diff] [blame] | 342 | const_cast<uint32_t&>(dev->device.flags) = 0; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 343 | const_cast<uint32_t&>(dev->device.width) = m->info.xres; |
| 344 | const_cast<uint32_t&>(dev->device.height) = m->info.yres; |
| 345 | const_cast<int&>(dev->device.stride) = stride; |
David 'Digit' Turner | 25b68b5 | 2011-01-16 19:52:22 +0100 | [diff] [blame] | 346 | const_cast<int&>(dev->device.format) = format; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 347 | const_cast<float&>(dev->device.xdpi) = m->xdpi; |
| 348 | const_cast<float&>(dev->device.ydpi) = m->ydpi; |
| 349 | const_cast<float&>(dev->device.fps) = m->fps; |
| 350 | const_cast<int&>(dev->device.minSwapInterval) = 1; |
| 351 | const_cast<int&>(dev->device.maxSwapInterval) = 1; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 352 | *device = &dev->device.common; |
George Burgess IV | 4028ac9 | 2018-02-12 13:14:52 -0800 | [diff] [blame] | 353 | } else { |
| 354 | free(dev); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | return status; |
| 358 | } |