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> |
| 29 | |
| 30 | #include <cutils/log.h> |
| 31 | #include <cutils/atomic.h> |
| 32 | |
| 33 | #if HAVE_ANDROID_OS |
| 34 | #include <linux/fb.h> |
| 35 | #endif |
| 36 | |
| 37 | #include "gralloc_priv.h" |
| 38 | |
| 39 | /*****************************************************************************/ |
| 40 | |
| 41 | #define NUM_BUFFERS 2 |
| 42 | |
| 43 | |
| 44 | enum { |
| 45 | PAGE_FLIP = 0x00000001, |
| 46 | LOCKED = 0x00000002 |
| 47 | }; |
| 48 | |
| 49 | struct fb_context_t { |
| 50 | framebuffer_device_t device; |
| 51 | }; |
| 52 | |
| 53 | /*****************************************************************************/ |
| 54 | |
| 55 | static int fb_setSwapInterval(struct framebuffer_device_t* dev, |
| 56 | int interval) |
| 57 | { |
| 58 | fb_context_t* ctx = (fb_context_t*)dev; |
| 59 | if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval) |
| 60 | return -EINVAL; |
| 61 | // FIXME: implement fb_setSwapInterval |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static int fb_setUpdateRect(struct framebuffer_device_t* dev, |
| 66 | int l, int t, int w, int h) |
| 67 | { |
| 68 | if (((w|h) <= 0) || ((l|t)<0)) |
| 69 | return -EINVAL; |
| 70 | |
| 71 | fb_context_t* ctx = (fb_context_t*)dev; |
| 72 | private_module_t* m = reinterpret_cast<private_module_t*>( |
| 73 | dev->common.module); |
| 74 | m->info.reserved[0] = 0x54445055; // "UPDT"; |
| 75 | m->info.reserved[1] = (uint16_t)l | ((uint32_t)t << 16); |
| 76 | m->info.reserved[2] = (uint16_t)(l+w) | ((uint32_t)(t+h) << 16); |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer) |
| 81 | { |
| 82 | if (private_handle_t::validate(buffer) < 0) |
| 83 | return -EINVAL; |
| 84 | |
| 85 | fb_context_t* ctx = (fb_context_t*)dev; |
| 86 | |
| 87 | private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(buffer); |
| 88 | private_module_t* m = reinterpret_cast<private_module_t*>( |
| 89 | dev->common.module); |
| 90 | |
| 91 | if (m->currentBuffer) { |
| 92 | m->base.unlock(&m->base, m->currentBuffer); |
| 93 | m->currentBuffer = 0; |
| 94 | } |
| 95 | |
| 96 | if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) { |
| 97 | |
| 98 | m->base.lock(&m->base, buffer, |
| 99 | private_module_t::PRIV_USAGE_LOCKED_FOR_POST, |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame^] | 100 | 0, 0, m->info.xres, m->info.yres, NULL); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 101 | |
| 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 | |
| 216 | int refreshRate = 1000000000000000LLU / |
| 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 | |
| 223 | if (refreshRate == 0) { |
| 224 | // bleagh, bad info from the driver |
| 225 | refreshRate = 60*1000; // 60 Hz |
| 226 | } |
| 227 | |
| 228 | if (int(info.width) <= 0 || int(info.height) <= 0) { |
| 229 | // the driver doesn't return that information |
| 230 | // default to 160 dpi |
| 231 | info.width = ((info.xres * 25.4f)/160.0f + 0.5f); |
| 232 | info.height = ((info.yres * 25.4f)/160.0f + 0.5f); |
| 233 | } |
| 234 | |
| 235 | float xdpi = (info.xres * 25.4f) / info.width; |
| 236 | float ydpi = (info.yres * 25.4f) / info.height; |
| 237 | float fps = refreshRate / 1000.0f; |
| 238 | |
| 239 | LOGI( "using (fd=%d)\n" |
| 240 | "id = %s\n" |
| 241 | "xres = %d px\n" |
| 242 | "yres = %d px\n" |
| 243 | "xres_virtual = %d px\n" |
| 244 | "yres_virtual = %d px\n" |
| 245 | "bpp = %d\n" |
| 246 | "r = %2u:%u\n" |
| 247 | "g = %2u:%u\n" |
| 248 | "b = %2u:%u\n", |
| 249 | fd, |
| 250 | finfo.id, |
| 251 | info.xres, |
| 252 | info.yres, |
| 253 | info.xres_virtual, |
| 254 | info.yres_virtual, |
| 255 | info.bits_per_pixel, |
| 256 | info.red.offset, info.red.length, |
| 257 | info.green.offset, info.green.length, |
| 258 | info.blue.offset, info.blue.length |
| 259 | ); |
| 260 | |
| 261 | LOGI( "width = %d mm (%f dpi)\n" |
| 262 | "height = %d mm (%f dpi)\n" |
| 263 | "refresh rate = %.2f Hz\n", |
| 264 | info.width, xdpi, |
| 265 | info.height, ydpi, |
| 266 | fps |
| 267 | ); |
| 268 | |
| 269 | |
| 270 | if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) |
| 271 | return -errno; |
| 272 | |
| 273 | if (finfo.smem_len <= 0) |
| 274 | return -errno; |
| 275 | |
| 276 | |
| 277 | module->flags = flags; |
| 278 | module->info = info; |
| 279 | module->finfo = finfo; |
| 280 | module->xdpi = xdpi; |
| 281 | module->ydpi = ydpi; |
| 282 | module->fps = fps; |
| 283 | |
| 284 | /* |
| 285 | * map the framebuffer |
| 286 | */ |
| 287 | |
| 288 | int err; |
| 289 | size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres_virtual); |
| 290 | module->framebuffer = new private_handle_t(dup(fd), fbSize, |
| 291 | private_handle_t::PRIV_FLAGS_USES_PMEM); |
| 292 | |
| 293 | module->numBuffers = info.yres_virtual / info.yres; |
| 294 | module->bufferMask = 0; |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame^] | 295 | |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 296 | void* vaddr; |
Mathias Agopian | 988b8bd | 2009-05-04 14:26:56 -0700 | [diff] [blame^] | 297 | gralloc_map(&module->base, module->framebuffer, &vaddr); |
Mathias Agopian | a8a7516 | 2009-04-10 14:24:31 -0700 | [diff] [blame] | 298 | memset(vaddr, 0, fbSize); |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static int mapFrameBuffer(struct private_module_t* module) |
| 304 | { |
| 305 | pthread_mutex_lock(&module->lock); |
| 306 | int err = mapFrameBufferLocked(module); |
| 307 | pthread_mutex_unlock(&module->lock); |
| 308 | return err; |
| 309 | } |
| 310 | |
| 311 | /*****************************************************************************/ |
| 312 | |
| 313 | static int fb_close(struct hw_device_t *dev) |
| 314 | { |
| 315 | fb_context_t* ctx = (fb_context_t*)dev; |
| 316 | if (ctx) { |
| 317 | free(ctx); |
| 318 | } |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | int fb_device_open(hw_module_t const* module, const char* name, |
| 323 | hw_device_t** device) |
| 324 | { |
| 325 | int status = -EINVAL; |
| 326 | if (!strcmp(name, GRALLOC_HARDWARE_FB0)) { |
| 327 | |
| 328 | alloc_device_t* gralloc_device; |
| 329 | status = gralloc_open(module, &gralloc_device); |
| 330 | if (status < 0) |
| 331 | return status; |
| 332 | |
| 333 | /* initialize our state here */ |
| 334 | fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev)); |
| 335 | memset(dev, 0, sizeof(*dev)); |
| 336 | |
| 337 | /* initialize the procs */ |
| 338 | dev->device.common.tag = HARDWARE_DEVICE_TAG; |
| 339 | dev->device.common.version = 0; |
| 340 | dev->device.common.module = const_cast<hw_module_t*>(module); |
| 341 | dev->device.common.close = fb_close; |
| 342 | dev->device.setSwapInterval = fb_setSwapInterval; |
| 343 | dev->device.setUpdateRect = fb_setUpdateRect; |
| 344 | dev->device.post = fb_post; |
| 345 | |
| 346 | private_module_t* m = (private_module_t*)module; |
| 347 | status = mapFrameBuffer(m); |
| 348 | if (status >= 0) { |
| 349 | int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3); |
| 350 | const_cast<uint32_t&>(dev->device.flags) = FRAMEBUFFER_FLAG_MAPPED; |
| 351 | const_cast<uint32_t&>(dev->device.width) = m->info.xres; |
| 352 | const_cast<uint32_t&>(dev->device.height) = m->info.yres; |
| 353 | const_cast<int&>(dev->device.stride) = stride; |
| 354 | const_cast<int&>(dev->device.format) = HAL_PIXEL_FORMAT_RGB_565; |
| 355 | const_cast<float&>(dev->device.xdpi) = m->xdpi; |
| 356 | const_cast<float&>(dev->device.ydpi) = m->ydpi; |
| 357 | const_cast<float&>(dev->device.fps) = m->fps; |
| 358 | const_cast<int&>(dev->device.minSwapInterval) = 1; |
| 359 | const_cast<int&>(dev->device.maxSwapInterval) = 1; |
| 360 | |
| 361 | *device = &dev->device.common; |
| 362 | } |
| 363 | } |
| 364 | return status; |
| 365 | } |