Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
Duy Truong | 73d36df | 2013-02-09 20:33:23 -0800 | [diff] [blame] | 3 | * Copyright (c) 2010-2012 The Linux Foundation. All rights reserved. |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <sys/mman.h> |
| 19 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 20 | #include <cutils/log.h> |
| 21 | #include <cutils/properties.h> |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 22 | #include <dlfcn.h> |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 23 | |
| 24 | #include <hardware/hardware.h> |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 25 | |
| 26 | #include <fcntl.h> |
| 27 | #include <errno.h> |
| 28 | #include <sys/ioctl.h> |
| 29 | #include <string.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <pthread.h> |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 32 | #include <cutils/atomic.h> |
| 33 | |
| 34 | #include <linux/fb.h> |
| 35 | #include <linux/msm_mdp.h> |
| 36 | |
| 37 | #include <GLES/gl.h> |
| 38 | |
| 39 | #include "gralloc_priv.h" |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 40 | #include "fb_priv.h" |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 41 | #include "gr.h" |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 42 | #include <cutils/properties.h> |
Naseer Ahmed | a87da60 | 2012-07-01 23:54:19 -0700 | [diff] [blame] | 43 | #include <profiler.h> |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 44 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 45 | #define EVEN_OUT(x) if (x & 0x0001) {x--;} |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 46 | /** min of int a, b */ |
| 47 | static inline int min(int a, int b) { |
| 48 | return (a<b) ? a : b; |
| 49 | } |
| 50 | /** max of int a, b */ |
| 51 | static inline int max(int a, int b) { |
| 52 | return (a>b) ? a : b; |
| 53 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 54 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 55 | enum { |
| 56 | PAGE_FLIP = 0x00000001, |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | struct fb_context_t { |
| 60 | framebuffer_device_t device; |
| 61 | }; |
| 62 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 63 | |
| 64 | static int fb_setSwapInterval(struct framebuffer_device_t* dev, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 65 | int interval) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 66 | { |
Naseer Ahmed | 30621ab | 2012-06-25 15:37:21 -0700 | [diff] [blame] | 67 | //XXX: Get the value here and implement along with |
| 68 | //single vsync in HWC |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 69 | char pval[PROPERTY_VALUE_MAX]; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 70 | property_get("debug.egl.swapinterval", pval, "-1"); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 71 | int property_interval = atoi(pval); |
| 72 | if (property_interval >= 0) |
| 73 | interval = property_interval; |
| 74 | |
| 75 | fb_context_t* ctx = (fb_context_t*)dev; |
| 76 | private_module_t* m = reinterpret_cast<private_module_t*>( |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 77 | dev->common.module); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 78 | if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval) |
| 79 | return -EINVAL; |
| 80 | |
| 81 | m->swapInterval = interval; |
| 82 | return 0; |
| 83 | } |
| 84 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 85 | static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer) |
| 86 | { |
Naseer Ahmed | 00fd6a5 | 2012-07-03 21:23:12 -0700 | [diff] [blame] | 87 | private_module_t* m = |
| 88 | reinterpret_cast<private_module_t*>(dev->common.module); |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 89 | private_handle_t *hnd = static_cast<private_handle_t*> |
| 90 | (const_cast<native_handle_t*>(buffer)); |
| 91 | const size_t offset = hnd->base - m->framebuffer->base; |
| 92 | m->info.activate = FB_ACTIVATE_VBL; |
| 93 | m->info.yoffset = offset / m->finfo.line_length; |
| 94 | if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) { |
| 95 | ALOGE("%s: FBIOPUT_VSCREENINFO for primary failed, str: %s", |
Saurabh Shah | 4333330 | 2013-02-06 13:30:27 -0800 | [diff] [blame] | 96 | __FUNCTION__, strerror(errno)); |
| 97 | return -errno; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 98 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static int fb_compositionComplete(struct framebuffer_device_t* dev) |
| 103 | { |
| 104 | // TODO: Properly implement composition complete callback |
| 105 | glFinish(); |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 110 | int mapFrameBufferLocked(struct private_module_t* module) |
| 111 | { |
| 112 | // already initialized... |
| 113 | if (module->framebuffer) { |
| 114 | return 0; |
| 115 | } |
| 116 | char const * const device_template[] = { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 117 | "/dev/graphics/fb%u", |
| 118 | "/dev/fb%u", |
| 119 | 0 }; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 120 | |
| 121 | int fd = -1; |
| 122 | int i=0; |
| 123 | char name[64]; |
| 124 | char property[PROPERTY_VALUE_MAX]; |
| 125 | |
| 126 | while ((fd==-1) && device_template[i]) { |
| 127 | snprintf(name, 64, device_template[i], 0); |
| 128 | fd = open(name, O_RDWR, 0); |
| 129 | i++; |
| 130 | } |
| 131 | if (fd < 0) |
| 132 | return -errno; |
| 133 | |
Naseer Ahmed | 32aa90f | 2012-10-01 18:45:13 -0400 | [diff] [blame] | 134 | memset(&module->commit, 0, sizeof(struct mdp_display_commit)); |
| 135 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 136 | struct fb_fix_screeninfo finfo; |
Ramkumar Radhakrishnan | 36bd527 | 2014-01-31 20:03:01 -0800 | [diff] [blame] | 137 | if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) { |
| 138 | close(fd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 139 | return -errno; |
Ramkumar Radhakrishnan | 36bd527 | 2014-01-31 20:03:01 -0800 | [diff] [blame] | 140 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 141 | |
| 142 | struct fb_var_screeninfo info; |
Ramkumar Radhakrishnan | 36bd527 | 2014-01-31 20:03:01 -0800 | [diff] [blame] | 143 | if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) { |
| 144 | close(fd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 145 | return -errno; |
Ramkumar Radhakrishnan | 36bd527 | 2014-01-31 20:03:01 -0800 | [diff] [blame] | 146 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 147 | |
| 148 | info.reserved[0] = 0; |
| 149 | info.reserved[1] = 0; |
| 150 | info.reserved[2] = 0; |
| 151 | info.xoffset = 0; |
| 152 | info.yoffset = 0; |
| 153 | info.activate = FB_ACTIVATE_NOW; |
| 154 | |
Naseer Ahmed | 0c8b7b5 | 2012-07-20 09:06:13 -0700 | [diff] [blame] | 155 | /* Interpretation of offset for color fields: All offsets are from the |
| 156 | * right, inside a "pixel" value, which is exactly 'bits_per_pixel' wide |
| 157 | * (means: you can use the offset as right argument to <<). A pixel |
| 158 | * afterwards is a bit stream and is written to video memory as that |
| 159 | * unmodified. This implies big-endian byte order if bits_per_pixel is |
| 160 | * greater than 8. |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 161 | */ |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 162 | |
| 163 | if(info.bits_per_pixel == 32) { |
| 164 | /* |
| 165 | * Explicitly request RGBA_8888 |
| 166 | */ |
| 167 | info.bits_per_pixel = 32; |
| 168 | info.red.offset = 24; |
| 169 | info.red.length = 8; |
| 170 | info.green.offset = 16; |
| 171 | info.green.length = 8; |
| 172 | info.blue.offset = 8; |
| 173 | info.blue.length = 8; |
| 174 | info.transp.offset = 0; |
| 175 | info.transp.length = 8; |
| 176 | |
Naseer Ahmed | 0c8b7b5 | 2012-07-20 09:06:13 -0700 | [diff] [blame] | 177 | /* Note: the GL driver does not have a r=8 g=8 b=8 a=0 config, so if we |
| 178 | * do not use the MDP for composition (i.e. hw composition == 0), ask |
| 179 | * for RGBA instead of RGBX. */ |
| 180 | if (property_get("debug.sf.hw", property, NULL) > 0 && |
| 181 | atoi(property) == 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 182 | module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 183 | else if(property_get("debug.composition.type", property, NULL) > 0 && |
| 184 | (strncmp(property, "mdp", 3) == 0)) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 185 | module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888; |
| 186 | else |
| 187 | module->fbFormat = HAL_PIXEL_FORMAT_RGBA_8888; |
| 188 | } else { |
| 189 | /* |
| 190 | * Explicitly request 5/6/5 |
| 191 | */ |
| 192 | info.bits_per_pixel = 16; |
| 193 | info.red.offset = 11; |
| 194 | info.red.length = 5; |
| 195 | info.green.offset = 5; |
| 196 | info.green.length = 6; |
| 197 | info.blue.offset = 0; |
| 198 | info.blue.length = 5; |
| 199 | info.transp.offset = 0; |
| 200 | info.transp.length = 0; |
| 201 | module->fbFormat = HAL_PIXEL_FORMAT_RGB_565; |
| 202 | } |
| 203 | |
| 204 | //adreno needs 4k aligned offsets. Max hole size is 4096-1 |
Naseer Ahmed | 0c8b7b5 | 2012-07-20 09:06:13 -0700 | [diff] [blame] | 205 | int size = roundUpToPageSize(info.yres * info.xres * |
| 206 | (info.bits_per_pixel/8)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 207 | |
| 208 | /* |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 209 | * Request NUM_BUFFERS screens (at least 2 for page flipping) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 210 | */ |
| 211 | int numberOfBuffers = (int)(finfo.smem_len/size); |
| 212 | ALOGV("num supported framebuffers in kernel = %d", numberOfBuffers); |
| 213 | |
| 214 | if (property_get("debug.gr.numframebuffers", property, NULL) > 0) { |
| 215 | int num = atoi(property); |
| 216 | if ((num >= NUM_FRAMEBUFFERS_MIN) && (num <= NUM_FRAMEBUFFERS_MAX)) { |
| 217 | numberOfBuffers = num; |
| 218 | } |
| 219 | } |
| 220 | if (numberOfBuffers > NUM_FRAMEBUFFERS_MAX) |
| 221 | numberOfBuffers = NUM_FRAMEBUFFERS_MAX; |
| 222 | |
| 223 | ALOGV("We support %d buffers", numberOfBuffers); |
| 224 | |
| 225 | //consider the included hole by 4k alignment |
| 226 | uint32_t line_length = (info.xres * info.bits_per_pixel / 8); |
| 227 | info.yres_virtual = (size * numberOfBuffers) / line_length; |
| 228 | |
| 229 | uint32_t flags = PAGE_FLIP; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 230 | |
| 231 | if (info.yres_virtual < ((size * 2) / line_length) ) { |
| 232 | // we need at least 2 for page-flipping |
| 233 | info.yres_virtual = size / line_length; |
| 234 | flags &= ~PAGE_FLIP; |
| 235 | ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 236 | info.yres_virtual, info.yres*2); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Ramkumar Radhakrishnan | 36bd527 | 2014-01-31 20:03:01 -0800 | [diff] [blame] | 239 | if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) { |
| 240 | close(fd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 241 | return -errno; |
Ramkumar Radhakrishnan | 36bd527 | 2014-01-31 20:03:01 -0800 | [diff] [blame] | 242 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 243 | |
| 244 | if (int(info.width) <= 0 || int(info.height) <= 0) { |
| 245 | // the driver doesn't return that information |
| 246 | // default to 160 dpi |
| 247 | info.width = ((info.xres * 25.4f)/160.0f + 0.5f); |
| 248 | info.height = ((info.yres * 25.4f)/160.0f + 0.5f); |
| 249 | } |
| 250 | |
| 251 | float xdpi = (info.xres * 25.4f) / info.width; |
| 252 | float ydpi = (info.yres * 25.4f) / info.height; |
Ken Zhang | 6e6f9a9 | 2013-01-08 15:09:27 -0500 | [diff] [blame] | 253 | #ifdef MSMFB_METADATA_GET |
| 254 | struct msmfb_metadata metadata; |
| 255 | memset(&metadata, 0 , sizeof(metadata)); |
| 256 | metadata.op = metadata_op_frame_rate; |
| 257 | if (ioctl(fd, MSMFB_METADATA_GET, &metadata) == -1) { |
| 258 | ALOGE("Error retrieving panel frame rate"); |
Ramkumar Radhakrishnan | 36bd527 | 2014-01-31 20:03:01 -0800 | [diff] [blame] | 259 | close(fd); |
Ken Zhang | 6e6f9a9 | 2013-01-08 15:09:27 -0500 | [diff] [blame] | 260 | return -errno; |
| 261 | } |
| 262 | float fps = metadata.data.panel_frame_rate; |
| 263 | #else |
| 264 | //XXX: Remove reserved field usage on all baselines |
choongryeol.lee | 26145b8 | 2012-06-26 23:41:00 -0700 | [diff] [blame] | 265 | //The reserved[3] field is used to store FPS by the driver. |
Naseer Ahmed | 9edd17c | 2012-08-15 18:16:50 -0700 | [diff] [blame] | 266 | float fps = info.reserved[3] & 0xFF; |
Ken Zhang | 6e6f9a9 | 2013-01-08 15:09:27 -0500 | [diff] [blame] | 267 | #endif |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 268 | ALOGI("using (fd=%d)\n" |
| 269 | "id = %s\n" |
| 270 | "xres = %d px\n" |
| 271 | "yres = %d px\n" |
| 272 | "xres_virtual = %d px\n" |
| 273 | "yres_virtual = %d px\n" |
| 274 | "bpp = %d\n" |
| 275 | "r = %2u:%u\n" |
| 276 | "g = %2u:%u\n" |
| 277 | "b = %2u:%u\n", |
| 278 | fd, |
| 279 | finfo.id, |
| 280 | info.xres, |
| 281 | info.yres, |
| 282 | info.xres_virtual, |
| 283 | info.yres_virtual, |
| 284 | info.bits_per_pixel, |
| 285 | info.red.offset, info.red.length, |
| 286 | info.green.offset, info.green.length, |
| 287 | info.blue.offset, info.blue.length |
| 288 | ); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 289 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 290 | ALOGI("width = %d mm (%f dpi)\n" |
| 291 | "height = %d mm (%f dpi)\n" |
| 292 | "refresh rate = %.2f Hz\n", |
| 293 | info.width, xdpi, |
| 294 | info.height, ydpi, |
| 295 | fps |
| 296 | ); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 297 | |
| 298 | |
Ramkumar Radhakrishnan | 36bd527 | 2014-01-31 20:03:01 -0800 | [diff] [blame] | 299 | if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) { |
| 300 | close(fd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 301 | return -errno; |
Ramkumar Radhakrishnan | 36bd527 | 2014-01-31 20:03:01 -0800 | [diff] [blame] | 302 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 303 | |
Ramkumar Radhakrishnan | 36bd527 | 2014-01-31 20:03:01 -0800 | [diff] [blame] | 304 | if (finfo.smem_len <= 0) { |
| 305 | close(fd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 306 | return -errno; |
Ramkumar Radhakrishnan | 36bd527 | 2014-01-31 20:03:01 -0800 | [diff] [blame] | 307 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 308 | |
| 309 | module->flags = flags; |
| 310 | module->info = info; |
| 311 | module->finfo = finfo; |
| 312 | module->xdpi = xdpi; |
| 313 | module->ydpi = ydpi; |
| 314 | module->fps = fps; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 315 | module->swapInterval = 1; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 316 | |
| 317 | CALC_INIT(); |
| 318 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 319 | /* |
| 320 | * map the framebuffer |
| 321 | */ |
| 322 | |
| 323 | int err; |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 324 | module->numBuffers = info.yres_virtual / info.yres; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 325 | module->bufferMask = 0; |
| 326 | //adreno needs page aligned offsets. Align the fbsize to pagesize. |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 327 | size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres)* |
| 328 | module->numBuffers; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 329 | module->framebuffer = new private_handle_t(fd, fbSize, |
Naseer Ahmed | d7f8427 | 2012-12-13 13:09:53 -0500 | [diff] [blame] | 330 | private_handle_t::PRIV_FLAGS_USES_ION, |
Naseer Ahmed | 0c8b7b5 | 2012-07-20 09:06:13 -0700 | [diff] [blame] | 331 | BUFFER_TYPE_UI, |
| 332 | module->fbFormat, info.xres, info.yres); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 333 | void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); |
| 334 | if (vaddr == MAP_FAILED) { |
| 335 | ALOGE("Error mapping the framebuffer (%s)", strerror(errno)); |
Ramkumar Radhakrishnan | 36bd527 | 2014-01-31 20:03:01 -0800 | [diff] [blame] | 336 | close(fd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 337 | return -errno; |
| 338 | } |
| 339 | module->framebuffer->base = intptr_t(vaddr); |
| 340 | memset(vaddr, 0, fbSize); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 341 | module->currentOffset = 0; |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 342 | //Enable vsync |
| 343 | int enable = 1; |
| 344 | ioctl(module->framebuffer->fd, MSMFB_OVERLAY_VSYNC_CTRL, |
| 345 | &enable); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | static int mapFrameBuffer(struct private_module_t* module) |
| 350 | { |
Naseer Ahmed | 473986c | 2013-07-16 12:47:21 -0400 | [diff] [blame] | 351 | int err = -1; |
Naseer Ahmed | 499ab3d | 2013-07-04 15:24:31 -0400 | [diff] [blame] | 352 | char property[PROPERTY_VALUE_MAX]; |
| 353 | if((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) && |
| 354 | (!strncmp(property, "1", PROPERTY_VALUE_MAX ) || |
| 355 | (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) { |
| 356 | pthread_mutex_lock(&module->lock); |
| 357 | err = mapFrameBufferLocked(module); |
| 358 | pthread_mutex_unlock(&module->lock); |
| 359 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 360 | return err; |
| 361 | } |
| 362 | |
| 363 | /*****************************************************************************/ |
| 364 | |
| 365 | static int fb_close(struct hw_device_t *dev) |
| 366 | { |
| 367 | fb_context_t* ctx = (fb_context_t*)dev; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 368 | if (ctx) { |
Saurabh Shah | 3e858eb | 2012-09-17 16:53:21 -0700 | [diff] [blame] | 369 | //Hack until fbdev is removed. Framework could close this causing hwc a |
| 370 | //pain. |
| 371 | //free(ctx); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 372 | } |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | int fb_device_open(hw_module_t const* module, const char* name, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 377 | hw_device_t** device) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 378 | { |
| 379 | int status = -EINVAL; |
| 380 | if (!strcmp(name, GRALLOC_HARDWARE_FB0)) { |
| 381 | alloc_device_t* gralloc_device; |
| 382 | status = gralloc_open(module, &gralloc_device); |
| 383 | if (status < 0) |
| 384 | return status; |
| 385 | |
| 386 | /* initialize our state here */ |
| 387 | fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev)); |
Ramkumar Radhakrishnan | 36bd527 | 2014-01-31 20:03:01 -0800 | [diff] [blame] | 388 | if(dev == NULL) { |
| 389 | gralloc_close(gralloc_device); |
| 390 | return status; |
| 391 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 392 | memset(dev, 0, sizeof(*dev)); |
| 393 | |
| 394 | /* initialize the procs */ |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 395 | dev->device.common.tag = HARDWARE_DEVICE_TAG; |
| 396 | dev->device.common.version = 0; |
| 397 | dev->device.common.module = const_cast<hw_module_t*>(module); |
| 398 | dev->device.common.close = fb_close; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 399 | dev->device.setSwapInterval = fb_setSwapInterval; |
| 400 | dev->device.post = fb_post; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 401 | dev->device.setUpdateRect = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 402 | dev->device.compositionComplete = fb_compositionComplete; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 403 | |
| 404 | private_module_t* m = (private_module_t*)module; |
| 405 | status = mapFrameBuffer(m); |
| 406 | if (status >= 0) { |
| 407 | int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3); |
| 408 | const_cast<uint32_t&>(dev->device.flags) = 0; |
| 409 | const_cast<uint32_t&>(dev->device.width) = m->info.xres; |
| 410 | const_cast<uint32_t&>(dev->device.height) = m->info.yres; |
| 411 | const_cast<int&>(dev->device.stride) = stride; |
| 412 | const_cast<int&>(dev->device.format) = m->fbFormat; |
| 413 | const_cast<float&>(dev->device.xdpi) = m->xdpi; |
| 414 | const_cast<float&>(dev->device.ydpi) = m->ydpi; |
| 415 | const_cast<float&>(dev->device.fps) = m->fps; |
Naseer Ahmed | 0c8b7b5 | 2012-07-20 09:06:13 -0700 | [diff] [blame] | 416 | const_cast<int&>(dev->device.minSwapInterval) = |
| 417 | PRIV_MIN_SWAP_INTERVAL; |
| 418 | const_cast<int&>(dev->device.maxSwapInterval) = |
| 419 | PRIV_MAX_SWAP_INTERVAL; |
Naseer Ahmed | 00fd6a5 | 2012-07-03 21:23:12 -0700 | [diff] [blame] | 420 | const_cast<int&>(dev->device.numFramebuffers) = m->numBuffers; |
Naseer Ahmed | 6f8ca31 | 2013-01-15 16:55:06 -0500 | [diff] [blame] | 421 | dev->device.setUpdateRect = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 422 | |
| 423 | *device = &dev->device.common; |
| 424 | } |
| 425 | |
| 426 | // Close the gralloc module |
| 427 | gralloc_close(gralloc_device); |
| 428 | } |
| 429 | return status; |
| 430 | } |