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