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 | |
| 17 | #include <sys/mman.h> |
| 18 | |
| 19 | #include <dlfcn.h> |
| 20 | |
| 21 | #include <cutils/ashmem.h> |
| 22 | #include <cutils/log.h> |
| 23 | |
| 24 | #include <hardware/hardware.h> |
| 25 | #include <hardware/gralloc.h> |
| 26 | |
| 27 | #include <fcntl.h> |
| 28 | #include <errno.h> |
Marco Nelissen | a4b587c | 2009-07-07 09:29:00 -0700 | [diff] [blame] | 29 | #include <sys/ioctl.h> |
| 30 | #include <string.h> |
| 31 | #include <stdlib.h> |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 32 | |
| 33 | #include <cutils/log.h> |
| 34 | #include <cutils/atomic.h> |
| 35 | |
| 36 | #if HAVE_ANDROID_OS |
| 37 | #include <linux/fb.h> |
| 38 | #endif |
| 39 | |
| 40 | #include "gralloc_priv.h" |
Mathias Agopian | fc05413 | 2009-08-18 17:35:44 -0700 | [diff] [blame] | 41 | #include "gr.h" |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 42 | |
| 43 | /*****************************************************************************/ |
| 44 | |
Mathias Agopian | cdb66fb | 2009-07-06 20:49:05 -0700 | [diff] [blame] | 45 | // numbers of buffers for page flipping |
Jean-Baptiste Queru | 118b5d7 | 2010-10-14 18:04:30 -0700 | [diff] [blame] | 46 | #if defined(NO_PAGE_FLIPPING) |
| 47 | // page-flipping is buggy on some devices |
| 48 | #define NUM_BUFFERS 1 |
| 49 | #else |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 50 | #define NUM_BUFFERS 2 |
Jean-Baptiste Queru | 118b5d7 | 2010-10-14 18:04:30 -0700 | [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 | { |
| 68 | fb_context_t* ctx = (fb_context_t*)dev; |
| 69 | if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval) |
| 70 | return -EINVAL; |
| 71 | // FIXME: implement fb_setSwapInterval |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int fb_setUpdateRect(struct framebuffer_device_t* dev, |
| 76 | int l, int t, int w, int h) |
| 77 | { |
| 78 | if (((w|h) <= 0) || ((l|t)<0)) |
| 79 | return -EINVAL; |
| 80 | |
| 81 | fb_context_t* ctx = (fb_context_t*)dev; |
| 82 | private_module_t* m = reinterpret_cast<private_module_t*>( |
| 83 | dev->common.module); |
| 84 | m->info.reserved[0] = 0x54445055; // "UPDT"; |
| 85 | m->info.reserved[1] = (uint16_t)l | ((uint32_t)t << 16); |
| 86 | m->info.reserved[2] = (uint16_t)(l+w) | ((uint32_t)(t+h) << 16); |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer) |
| 91 | { |
| 92 | if (private_handle_t::validate(buffer) < 0) |
| 93 | return -EINVAL; |
| 94 | |
| 95 | fb_context_t* ctx = (fb_context_t*)dev; |
| 96 | |
| 97 | private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(buffer); |
| 98 | private_module_t* m = reinterpret_cast<private_module_t*>( |
| 99 | dev->common.module); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 100 | |
| 101 | if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) { |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 102 | const size_t offset = hnd->base - m->framebuffer->base; |
| 103 | m->info.activate = FB_ACTIVATE_VBL; |
| 104 | m->info.yoffset = offset / m->finfo.line_length; |
| 105 | if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) { |
| 106 | LOGE("FBIOPUT_VSCREENINFO failed"); |
| 107 | m->base.unlock(&m->base, buffer); |
| 108 | return -errno; |
| 109 | } |
| 110 | m->currentBuffer = buffer; |
| 111 | |
| 112 | } else { |
| 113 | // If we can't do the page_flip, just copy the buffer to the front |
| 114 | // FIXME: use copybit HAL instead of memcpy |
| 115 | |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 116 | void* fb_vaddr; |
| 117 | void* buffer_vaddr; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 118 | |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 119 | m->base.lock(&m->base, m->framebuffer, |
| 120 | GRALLOC_USAGE_SW_WRITE_RARELY, |
| 121 | 0, 0, m->info.xres, m->info.yres, |
| 122 | &fb_vaddr); |
| 123 | |
| 124 | m->base.lock(&m->base, buffer, |
| 125 | GRALLOC_USAGE_SW_READ_RARELY, |
| 126 | 0, 0, m->info.xres, m->info.yres, |
| 127 | &buffer_vaddr); |
| 128 | |
| 129 | memcpy(fb_vaddr, buffer_vaddr, m->finfo.line_length * m->info.yres); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 130 | |
| 131 | m->base.unlock(&m->base, buffer); |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 132 | m->base.unlock(&m->base, m->framebuffer); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | /*****************************************************************************/ |
| 139 | |
| 140 | int mapFrameBufferLocked(struct private_module_t* module) |
| 141 | { |
| 142 | // already initialized... |
| 143 | if (module->framebuffer) { |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | char const * const device_template[] = { |
| 148 | "/dev/graphics/fb%u", |
| 149 | "/dev/fb%u", |
| 150 | 0 }; |
| 151 | |
| 152 | int fd = -1; |
| 153 | int i=0; |
| 154 | char name[64]; |
| 155 | |
| 156 | while ((fd==-1) && device_template[i]) { |
| 157 | snprintf(name, 64, device_template[i], 0); |
| 158 | fd = open(name, O_RDWR, 0); |
| 159 | i++; |
| 160 | } |
| 161 | if (fd < 0) |
| 162 | return -errno; |
| 163 | |
| 164 | struct fb_fix_screeninfo finfo; |
| 165 | if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) |
| 166 | return -errno; |
| 167 | |
| 168 | struct fb_var_screeninfo info; |
| 169 | if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) |
| 170 | return -errno; |
| 171 | |
| 172 | info.reserved[0] = 0; |
| 173 | info.reserved[1] = 0; |
| 174 | info.reserved[2] = 0; |
| 175 | info.xoffset = 0; |
| 176 | info.yoffset = 0; |
| 177 | info.activate = FB_ACTIVATE_NOW; |
| 178 | |
| 179 | /* |
| 180 | * Explicitly request 5/6/5 |
| 181 | */ |
| 182 | info.bits_per_pixel = 16; |
| 183 | info.red.offset = 11; |
| 184 | info.red.length = 5; |
| 185 | info.green.offset = 5; |
| 186 | info.green.length = 6; |
| 187 | info.blue.offset = 0; |
| 188 | info.blue.length = 5; |
| 189 | info.transp.offset = 0; |
| 190 | info.transp.length = 0; |
| 191 | |
| 192 | /* |
| 193 | * Request NUM_BUFFERS screens (at lest 2 for page flipping) |
| 194 | */ |
| 195 | info.yres_virtual = info.yres * NUM_BUFFERS; |
| 196 | |
| 197 | |
| 198 | uint32_t flags = PAGE_FLIP; |
| 199 | if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) { |
| 200 | info.yres_virtual = info.yres; |
| 201 | flags &= ~PAGE_FLIP; |
| 202 | LOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported"); |
| 203 | } |
| 204 | |
| 205 | if (info.yres_virtual < info.yres * 2) { |
| 206 | // we need at least 2 for page-flipping |
| 207 | info.yres_virtual = info.yres; |
| 208 | flags &= ~PAGE_FLIP; |
| 209 | LOGW("page flipping not supported (yres_virtual=%d, requested=%d)", |
| 210 | info.yres_virtual, info.yres*2); |
| 211 | } |
| 212 | |
| 213 | if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) |
| 214 | return -errno; |
| 215 | |
David 'Digit' Turner | 80d3699 | 2011-01-15 21:06:12 +0100 | [diff] [blame^] | 216 | uint64_t refreshQuotient = |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 217 | ( |
| 218 | uint64_t( info.upper_margin + info.lower_margin + info.yres ) |
| 219 | * ( info.left_margin + info.right_margin + info.xres ) |
| 220 | * info.pixclock |
| 221 | ); |
| 222 | |
David 'Digit' Turner | 80d3699 | 2011-01-15 21:06:12 +0100 | [diff] [blame^] | 223 | /* Beware, info.pixclock might be 0 under emulation, so avoid a |
| 224 | * division-by-0 here (SIGFPE on ARM) */ |
| 225 | int refreshRate = refreshQuotient > 0 ? (int)(1000000000000000LLU / refreshQuotient) : 0; |
| 226 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 227 | if (refreshRate == 0) { |
| 228 | // bleagh, bad info from the driver |
| 229 | refreshRate = 60*1000; // 60 Hz |
| 230 | } |
| 231 | |
| 232 | if (int(info.width) <= 0 || int(info.height) <= 0) { |
| 233 | // the driver doesn't return that information |
| 234 | // default to 160 dpi |
| 235 | info.width = ((info.xres * 25.4f)/160.0f + 0.5f); |
| 236 | info.height = ((info.yres * 25.4f)/160.0f + 0.5f); |
| 237 | } |
| 238 | |
| 239 | float xdpi = (info.xres * 25.4f) / info.width; |
| 240 | float ydpi = (info.yres * 25.4f) / info.height; |
| 241 | float fps = refreshRate / 1000.0f; |
| 242 | |
| 243 | LOGI( "using (fd=%d)\n" |
| 244 | "id = %s\n" |
| 245 | "xres = %d px\n" |
| 246 | "yres = %d px\n" |
| 247 | "xres_virtual = %d px\n" |
| 248 | "yres_virtual = %d px\n" |
| 249 | "bpp = %d\n" |
| 250 | "r = %2u:%u\n" |
| 251 | "g = %2u:%u\n" |
| 252 | "b = %2u:%u\n", |
| 253 | fd, |
| 254 | finfo.id, |
| 255 | info.xres, |
| 256 | info.yres, |
| 257 | info.xres_virtual, |
| 258 | info.yres_virtual, |
| 259 | info.bits_per_pixel, |
| 260 | info.red.offset, info.red.length, |
| 261 | info.green.offset, info.green.length, |
| 262 | info.blue.offset, info.blue.length |
| 263 | ); |
| 264 | |
| 265 | LOGI( "width = %d mm (%f dpi)\n" |
| 266 | "height = %d mm (%f dpi)\n" |
| 267 | "refresh rate = %.2f Hz\n", |
| 268 | info.width, xdpi, |
| 269 | info.height, ydpi, |
| 270 | fps |
| 271 | ); |
| 272 | |
| 273 | |
| 274 | if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) |
| 275 | return -errno; |
| 276 | |
| 277 | if (finfo.smem_len <= 0) |
| 278 | return -errno; |
| 279 | |
| 280 | |
| 281 | module->flags = flags; |
| 282 | module->info = info; |
| 283 | module->finfo = finfo; |
| 284 | module->xdpi = xdpi; |
| 285 | module->ydpi = ydpi; |
| 286 | module->fps = fps; |
| 287 | |
| 288 | /* |
| 289 | * map the framebuffer |
| 290 | */ |
| 291 | |
| 292 | int err; |
| 293 | size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres_virtual); |
Mathias Agopian | f96b206 | 2009-12-14 18:27:09 -0800 | [diff] [blame] | 294 | module->framebuffer = new private_handle_t(dup(fd), fbSize, 0); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 295 | |
| 296 | module->numBuffers = info.yres_virtual / info.yres; |
| 297 | module->bufferMask = 0; |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame] | 298 | |
Mathias Agopian | 5115665 | 2009-06-09 18:55:49 -0700 | [diff] [blame] | 299 | void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); |
| 300 | if (vaddr == MAP_FAILED) { |
| 301 | LOGE("Error mapping the framebuffer (%s)", strerror(errno)); |
| 302 | return -errno; |
| 303 | } |
| 304 | module->framebuffer->base = intptr_t(vaddr); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 305 | memset(vaddr, 0, fbSize); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | static int mapFrameBuffer(struct private_module_t* module) |
| 310 | { |
| 311 | pthread_mutex_lock(&module->lock); |
| 312 | int err = mapFrameBufferLocked(module); |
| 313 | pthread_mutex_unlock(&module->lock); |
| 314 | return err; |
| 315 | } |
| 316 | |
| 317 | /*****************************************************************************/ |
| 318 | |
| 319 | static int fb_close(struct hw_device_t *dev) |
| 320 | { |
| 321 | fb_context_t* ctx = (fb_context_t*)dev; |
| 322 | if (ctx) { |
| 323 | free(ctx); |
| 324 | } |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | int fb_device_open(hw_module_t const* module, const char* name, |
| 329 | hw_device_t** device) |
| 330 | { |
| 331 | int status = -EINVAL; |
| 332 | if (!strcmp(name, GRALLOC_HARDWARE_FB0)) { |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 333 | alloc_device_t* gralloc_device; |
| 334 | status = gralloc_open(module, &gralloc_device); |
| 335 | if (status < 0) |
| 336 | return status; |
| 337 | |
| 338 | /* initialize our state here */ |
| 339 | fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev)); |
| 340 | memset(dev, 0, sizeof(*dev)); |
| 341 | |
| 342 | /* initialize the procs */ |
| 343 | dev->device.common.tag = HARDWARE_DEVICE_TAG; |
| 344 | dev->device.common.version = 0; |
| 345 | dev->device.common.module = const_cast<hw_module_t*>(module); |
| 346 | dev->device.common.close = fb_close; |
| 347 | dev->device.setSwapInterval = fb_setSwapInterval; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 348 | dev->device.post = fb_post; |
Mathias Agopian | d28d7e8 | 2009-07-07 12:20:31 -0700 | [diff] [blame] | 349 | dev->device.setUpdateRect = 0; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 350 | |
| 351 | private_module_t* m = (private_module_t*)module; |
| 352 | status = mapFrameBuffer(m); |
| 353 | if (status >= 0) { |
| 354 | int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3); |
Mathias Agopian | 295190f | 2009-05-05 18:30:52 -0700 | [diff] [blame] | 355 | const_cast<uint32_t&>(dev->device.flags) = 0; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 356 | const_cast<uint32_t&>(dev->device.width) = m->info.xres; |
| 357 | const_cast<uint32_t&>(dev->device.height) = m->info.yres; |
| 358 | const_cast<int&>(dev->device.stride) = stride; |
| 359 | const_cast<int&>(dev->device.format) = HAL_PIXEL_FORMAT_RGB_565; |
| 360 | const_cast<float&>(dev->device.xdpi) = m->xdpi; |
| 361 | const_cast<float&>(dev->device.ydpi) = m->ydpi; |
| 362 | const_cast<float&>(dev->device.fps) = m->fps; |
| 363 | const_cast<int&>(dev->device.minSwapInterval) = 1; |
| 364 | const_cast<int&>(dev->device.maxSwapInterval) = 1; |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 365 | *device = &dev->device.common; |
| 366 | } |
| 367 | } |
| 368 | return status; |
| 369 | } |