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