Rebecca Schultz Zavin | 2480ecc | 2012-08-14 16:08:04 -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 | #include <sys/ioctl.h> |
| 30 | #include <string.h> |
| 31 | #include <stdlib.h> |
| 32 | |
| 33 | #include <utils/Vector.h> |
| 34 | |
| 35 | #include <cutils/log.h> |
| 36 | #include <cutils/atomic.h> |
| 37 | |
| 38 | #if HAVE_ANDROID_OS |
| 39 | #include <linux/fb.h> |
| 40 | #endif |
| 41 | |
| 42 | #include "gralloc_priv.h" |
| 43 | #include "gr.h" |
| 44 | |
| 45 | /*****************************************************************************/ |
| 46 | |
| 47 | // numbers of buffers for page flipping |
| 48 | #define NUM_BUFFERS 2 |
| 49 | |
| 50 | struct hwc_callback_entry |
| 51 | { |
| 52 | void (*callback)(void *, private_handle_t *); |
| 53 | void *data; |
| 54 | }; |
| 55 | |
| 56 | typedef android::Vector<struct hwc_callback_entry> hwc_callback_queue_t; |
| 57 | |
| 58 | struct fb_context_t { |
| 59 | framebuffer_device_t device; |
| 60 | }; |
| 61 | |
| 62 | /*****************************************************************************/ |
| 63 | |
| 64 | static int fb_setSwapInterval(struct framebuffer_device_t* dev, |
| 65 | int interval) |
| 66 | { |
| 67 | fb_context_t* ctx = (fb_context_t*)dev; |
| 68 | if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval) |
| 69 | return -EINVAL; |
| 70 | // FIXME: implement fb_setSwapInterval |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 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 | |
| 79 | private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(buffer); |
| 80 | private_module_t* m = reinterpret_cast<private_module_t*>(dev->common.module); |
| 81 | |
| 82 | hwc_callback_queue_t *queue = reinterpret_cast<hwc_callback_queue_t *>(m->queue); |
| 83 | pthread_mutex_lock(&m->queue_lock); |
| 84 | if(queue->isEmpty()) |
| 85 | pthread_mutex_unlock(&m->queue_lock); |
| 86 | else { |
| 87 | private_handle_t *hnd = private_handle_t::dynamicCast(buffer); |
| 88 | struct hwc_callback_entry entry = queue->top(); |
| 89 | queue->pop(); |
| 90 | pthread_mutex_unlock(&m->queue_lock); |
| 91 | entry.callback(entry.data, hnd); |
| 92 | } |
| 93 | |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | /*****************************************************************************/ |
| 98 | |
| 99 | static int fb_close(struct hw_device_t *dev) |
| 100 | { |
| 101 | fb_context_t* ctx = (fb_context_t*)dev; |
| 102 | if (ctx) { |
| 103 | free(ctx); |
| 104 | } |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | int init_fb(struct private_module_t* module) |
| 109 | { |
| 110 | char const * const device_template[] = { |
| 111 | "/dev/graphics/fb%u", |
| 112 | "/dev/fb%u", |
| 113 | NULL |
| 114 | }; |
| 115 | |
| 116 | int fd = -1; |
| 117 | int i = 0; |
| 118 | char name[64]; |
| 119 | |
| 120 | fd = open("/dev/graphics/fb0", O_RDWR); |
| 121 | if (fd < 0) { |
| 122 | ALOGE("/dev/graphics/fb0 Open fail"); |
| 123 | return -errno; |
| 124 | } |
| 125 | |
| 126 | struct fb_fix_screeninfo finfo; |
| 127 | if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) { |
| 128 | ALOGE("Fail to get FB Screen Info"); |
| 129 | return -errno; |
| 130 | } |
| 131 | |
| 132 | struct fb_var_screeninfo info; |
| 133 | if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) { |
| 134 | ALOGE("First, Fail to get FB VScreen Info"); |
| 135 | return -errno; |
| 136 | } |
| 137 | |
| 138 | int refreshRate = 1000000000000000LLU / |
| 139 | ( |
| 140 | uint64_t( info.upper_margin + info.lower_margin + info.yres ) |
| 141 | * ( info.left_margin + info.right_margin + info.xres ) |
| 142 | * info.pixclock |
| 143 | ); |
| 144 | |
| 145 | if (refreshRate == 0) |
| 146 | refreshRate = 60*1000; /* 60 Hz */ |
| 147 | |
| 148 | float xdpi = (info.xres * 25.4f) / info.width; |
| 149 | float ydpi = (info.yres * 25.4f) / info.height; |
| 150 | float fps = refreshRate / 1000.0f; |
| 151 | |
| 152 | ALOGI("using (id=%s)\n" |
| 153 | "xres = %d px\n" |
| 154 | "yres = %d px\n" |
| 155 | "width = %d mm (%f dpi)\n" |
| 156 | "height = %d mm (%f dpi)\n" |
| 157 | "refresh rate = %.2f Hz\n", |
| 158 | finfo.id, info.xres, info.yres, info.width, xdpi, info.height, ydpi, |
| 159 | fps); |
| 160 | |
| 161 | module->xres = 2560; |
| 162 | module->yres = 1600; |
| 163 | module->line_length = 2560; |
| 164 | module->xdpi = xdpi; |
| 165 | module->ydpi = ydpi; |
| 166 | module->fps = fps; |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | int fb_device_open(hw_module_t const* module, const char* name, |
| 172 | hw_device_t** device) |
| 173 | { |
| 174 | int status = -EINVAL; |
| 175 | #ifdef GRALLOC_16_BITS |
| 176 | int bits_per_pixel = 16; |
| 177 | int format = HAL_PIXEL_FORMAT_RGB_565; |
| 178 | #else |
| 179 | int bits_per_pixel = 32; |
| 180 | int format = HAL_PIXEL_FORMAT_RGBA_8888; |
| 181 | #endif |
| 182 | |
| 183 | alloc_device_t* gralloc_device; |
| 184 | status = gralloc_open(module, &gralloc_device); |
| 185 | if (status < 0) { |
| 186 | ALOGE("Fail to Open gralloc device"); |
| 187 | return status; |
| 188 | } |
| 189 | |
| 190 | framebuffer_device_t *dev = (framebuffer_device_t *)malloc(sizeof(framebuffer_device_t)); |
| 191 | if (dev == NULL) { |
| 192 | ALOGE("Failed to allocate memory for dev"); |
| 193 | gralloc_close(gralloc_device); |
| 194 | return status; |
| 195 | } |
| 196 | |
| 197 | private_module_t* m = (private_module_t*)module; |
| 198 | status = init_fb(m); |
| 199 | if (status < 0) { |
| 200 | ALOGE("Fail to init framebuffer"); |
| 201 | free(dev); |
| 202 | gralloc_close(gralloc_device); |
| 203 | return status; |
| 204 | } |
| 205 | |
| 206 | /* initialize our state here */ |
| 207 | memset(dev, 0, sizeof(*dev)); |
| 208 | |
| 209 | /* initialize the procs */ |
| 210 | dev->common.tag = HARDWARE_DEVICE_TAG; |
| 211 | dev->common.version = 0; |
| 212 | dev->common.module = const_cast<hw_module_t*>(module); |
| 213 | dev->common.close = fb_close; |
| 214 | dev->setSwapInterval = 0; |
| 215 | dev->post = fb_post; |
| 216 | dev->setUpdateRect = 0; |
| 217 | dev->compositionComplete = 0; |
| 218 | m->queue = new hwc_callback_queue_t; |
| 219 | pthread_mutex_init(&m->queue_lock, NULL); |
| 220 | |
| 221 | int stride = m->line_length / (bits_per_pixel >> 3); |
| 222 | const_cast<uint32_t&>(dev->flags) = 0; |
| 223 | const_cast<uint32_t&>(dev->width) = m->xres; |
| 224 | const_cast<uint32_t&>(dev->height) = m->yres; |
| 225 | const_cast<int&>(dev->stride) = stride; |
| 226 | const_cast<int&>(dev->format) = format; |
| 227 | const_cast<float&>(dev->xdpi) = m->xdpi; |
| 228 | const_cast<float&>(dev->ydpi) = m->ydpi; |
| 229 | const_cast<float&>(dev->fps) = m->fps; |
| 230 | const_cast<int&>(dev->minSwapInterval) = 1; |
| 231 | const_cast<int&>(dev->maxSwapInterval) = 1; |
| 232 | *device = &dev->common; |
| 233 | status = 0; |
| 234 | |
| 235 | return status; |
| 236 | } |