Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 3 | * Copyright (c) 2010-2012 Code Aurora Forum. 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 | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 43 | #include <qcomutils/profiler.h> |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 44 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 45 | #include "overlay.h" |
| 46 | namespace ovutils = overlay::utils; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 47 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 48 | #define EVEN_OUT(x) if (x & 0x0001) {x--;} |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 49 | /** min of int a, b */ |
| 50 | static inline int min(int a, int b) { |
| 51 | return (a<b) ? a : b; |
| 52 | } |
| 53 | /** max of int a, b */ |
| 54 | static inline int max(int a, int b) { |
| 55 | return (a>b) ? a : b; |
| 56 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 57 | |
| 58 | char framebufferStateName[] = {'S', 'R', 'A'}; |
| 59 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 60 | enum { |
| 61 | PAGE_FLIP = 0x00000001, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 62 | LOCKED = 0x00000002 |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | struct fb_context_t { |
| 66 | framebuffer_device_t device; |
| 67 | }; |
| 68 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 69 | |
| 70 | static int fb_setSwapInterval(struct framebuffer_device_t* dev, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 71 | int interval) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 72 | { |
| 73 | char pval[PROPERTY_VALUE_MAX]; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 74 | property_get("debug.egl.swapinterval", pval, "-1"); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 75 | int property_interval = atoi(pval); |
| 76 | if (property_interval >= 0) |
| 77 | interval = property_interval; |
| 78 | |
| 79 | fb_context_t* ctx = (fb_context_t*)dev; |
| 80 | private_module_t* m = reinterpret_cast<private_module_t*>( |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 81 | dev->common.module); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 82 | if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval) |
| 83 | return -EINVAL; |
| 84 | |
| 85 | m->swapInterval = interval; |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static int fb_setUpdateRect(struct framebuffer_device_t* dev, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 90 | int l, int t, int w, int h) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 91 | { |
| 92 | if (((w|h) <= 0) || ((l|t)<0)) |
| 93 | return -EINVAL; |
| 94 | fb_context_t* ctx = (fb_context_t*)dev; |
| 95 | private_module_t* m = reinterpret_cast<private_module_t*>( |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 96 | dev->common.module); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 97 | m->info.reserved[0] = 0x54445055; // "UPDT"; |
| 98 | m->info.reserved[1] = (uint16_t)l | ((uint32_t)t << 16); |
| 99 | m->info.reserved[2] = (uint16_t)(l+w) | ((uint32_t)(t+h) << 16); |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | static void *disp_loop(void *ptr) |
| 104 | { |
| 105 | struct qbuf_t nxtBuf; |
| 106 | static int cur_buf=-1; |
| 107 | private_module_t *m = reinterpret_cast<private_module_t*>(ptr); |
| 108 | |
| 109 | while (1) { |
| 110 | pthread_mutex_lock(&(m->qlock)); |
| 111 | |
| 112 | // wait (sleep) while display queue is empty; |
| 113 | if (m->disp.isEmpty()) { |
| 114 | pthread_cond_wait(&(m->qpost),&(m->qlock)); |
| 115 | } |
| 116 | |
| 117 | // dequeue next buff to display and lock it |
| 118 | nxtBuf = m->disp.getHeadValue(); |
| 119 | m->disp.pop(); |
| 120 | pthread_mutex_unlock(&(m->qlock)); |
| 121 | |
| 122 | // post buf out to display synchronously |
| 123 | private_handle_t const* hnd = reinterpret_cast<private_handle_t const*> |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 124 | (nxtBuf.buf); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 125 | const size_t offset = hnd->base - m->framebuffer->base; |
| 126 | m->info.activate = FB_ACTIVATE_VBL; |
| 127 | m->info.yoffset = offset / m->finfo.line_length; |
| 128 | |
| 129 | #if defined(HDMI_DUAL_DISPLAY) |
| 130 | pthread_mutex_lock(&m->overlayLock); |
| 131 | m->orientation = neworientation; |
| 132 | m->currentOffset = offset; |
| 133 | m->hdmiStateChanged = true; |
| 134 | pthread_cond_signal(&(m->overlayPost)); |
| 135 | pthread_mutex_unlock(&m->overlayLock); |
| 136 | #endif |
| 137 | if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) { |
| 138 | ALOGE("ERROR FBIOPUT_VSCREENINFO failed; frame not displayed"); |
| 139 | } |
| 140 | |
| 141 | CALC_FPS(); |
| 142 | |
| 143 | if (cur_buf == -1) { |
| 144 | int nxtAvail = ((nxtBuf.idx + 1) % m->numBuffers); |
| 145 | pthread_mutex_lock(&(m->avail[nxtBuf.idx].lock)); |
| 146 | m->avail[nxtBuf.idx].is_avail = true; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 147 | m->avail[nxtBuf.idx].state = SUB; |
| 148 | pthread_cond_signal(&(m->avail[nxtBuf.idx].cond)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 149 | pthread_mutex_unlock(&(m->avail[nxtBuf.idx].lock)); |
| 150 | } else { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 151 | #if 0 //XXX: Triple FB |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 152 | pthread_mutex_lock(&(m->avail[nxtBuf.idx].lock)); |
| 153 | if (m->avail[nxtBuf.idx].state != SUB) { |
| 154 | ALOGE_IF(m->swapInterval != 0, "[%d] state %c, expected %c", nxtBuf.idx, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 155 | framebufferStateName[m->avail[nxtBuf.idx].state], |
| 156 | framebufferStateName[SUB]); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 157 | } |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 158 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 159 | m->avail[nxtBuf.idx].state = REF; |
| 160 | pthread_mutex_unlock(&(m->avail[nxtBuf.idx].lock)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 161 | if (m->avail[cur_buf].state != REF) { |
| 162 | ALOGE_IF(m->swapInterval != 0, "[%d] state %c, expected %c", cur_buf, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 163 | framebufferStateName[m->avail[cur_buf].state], |
| 164 | framebufferStateName[SUB]); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 165 | } |
| 166 | m->avail[cur_buf].state = AVL; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 167 | #endif |
| 168 | pthread_mutex_lock(&(m->avail[cur_buf].lock)); |
| 169 | m->avail[cur_buf].is_avail = true; |
| 170 | pthread_cond_signal(&(m->avail[cur_buf].cond)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 171 | pthread_mutex_unlock(&(m->avail[cur_buf].lock)); |
| 172 | } |
| 173 | cur_buf = nxtBuf.idx; |
| 174 | } |
| 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | #if defined(HDMI_DUAL_DISPLAY) |
| 179 | static int closeHDMIChannel(private_module_t* m) |
| 180 | { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 181 | // XXX - when enabling HDMI |
| 182 | #if 0 |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 183 | Overlay* pTemp = m->pobjOverlay; |
| 184 | if(pTemp != NULL) |
| 185 | pTemp->closeChannel(); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 186 | #endif |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 187 | return 0; |
| 188 | } |
| 189 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 190 | // XXX - Complete when enabling HDMI |
| 191 | #if 0 |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 192 | static void getSecondaryDisplayDestinationInfo(private_module_t* m, overlay_rect& |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 193 | rect, int& orientation) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 194 | { |
| 195 | Overlay* pTemp = m->pobjOverlay; |
| 196 | int width = pTemp->getFBWidth(); |
| 197 | int height = pTemp->getFBHeight(); |
| 198 | int fbwidth = m->info.xres, fbheight = m->info.yres; |
| 199 | rect.x = 0; rect.y = 0; |
| 200 | rect.w = width; rect.h = height; |
| 201 | int rot = m->orientation; |
| 202 | switch(rot) { |
| 203 | // ROT_0 |
| 204 | case 0: |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 205 | // ROT_180 |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 206 | case HAL_TRANSFORM_ROT_180: |
| 207 | pTemp->getAspectRatioPosition(fbwidth, fbheight, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 208 | &rect); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 209 | if(rot == HAL_TRANSFORM_ROT_180) |
| 210 | orientation = HAL_TRANSFORM_ROT_180; |
| 211 | else |
| 212 | orientation = 0; |
| 213 | break; |
| 214 | // ROT_90 |
| 215 | case HAL_TRANSFORM_ROT_90: |
| 216 | // ROT_270 |
| 217 | case HAL_TRANSFORM_ROT_270: |
| 218 | //Calculate the Aspectratio for the UI |
| 219 | //in the landscape mode |
| 220 | //Width and height will be swapped as there |
| 221 | //is rotation |
| 222 | pTemp->getAspectRatioPosition(fbheight, fbwidth, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 223 | &rect); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 224 | |
| 225 | if(rot == HAL_TRANSFORM_ROT_90) |
| 226 | orientation = HAL_TRANSFORM_ROT_270; |
| 227 | else if(rot == HAL_TRANSFORM_ROT_270) |
| 228 | orientation = HAL_TRANSFORM_ROT_90; |
| 229 | break; |
| 230 | } |
| 231 | return; |
| 232 | } |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 233 | #endif |
| 234 | |
| 235 | /* Determine overlay state based on whether hardware supports true UI |
| 236 | mirroring and whether video is playing or not */ |
| 237 | static ovutils::eOverlayState getOverlayState(struct private_module_t* module) |
| 238 | { |
| 239 | overlay2::Overlay& ov = *(Overlay::getInstance()); |
| 240 | |
| 241 | // Default to existing state |
| 242 | ovutils::eOverlayState state = ov.getState(); |
| 243 | |
| 244 | // Sanity check |
| 245 | if (!module) { |
| 246 | ALOGE("%s: NULL module", __FUNCTION__); |
| 247 | return state; |
| 248 | } |
| 249 | |
| 250 | // Check if video is playing or not |
| 251 | if (module->videoOverlay) { |
| 252 | // Video is playing, check if hardware supports true UI mirroring |
| 253 | if (module->trueMirrorSupport) { |
| 254 | // True UI mirroring is supported by hardware |
| 255 | if (ov.getState() == ovutils::OV_2D_VIDEO_ON_PANEL) { |
| 256 | // Currently playing 2D video |
| 257 | state = ovutils::OV_2D_TRUE_UI_MIRROR; |
| 258 | } else if (ov.getState() == ovutils::OV_3D_VIDEO_ON_2D_PANEL) { |
| 259 | // Currently playing M3D video |
| 260 | // FIXME: Support M3D true UI mirroring |
| 261 | state = ovutils::OV_3D_VIDEO_ON_2D_PANEL_2D_TV; |
| 262 | } |
| 263 | } else { |
| 264 | // True UI mirroring is not supported by hardware |
| 265 | if (ov.getState() == ovutils::OV_2D_VIDEO_ON_PANEL) { |
| 266 | // Currently playing 2D video |
| 267 | state = ovutils::OV_2D_VIDEO_ON_PANEL_TV; |
| 268 | } else if (ov.getState() == ovutils::OV_3D_VIDEO_ON_2D_PANEL) { |
| 269 | // Currently playing M3D video |
| 270 | state = ovutils::OV_3D_VIDEO_ON_2D_PANEL_2D_TV; |
| 271 | } |
| 272 | } |
| 273 | } else { |
| 274 | // Video is not playing, true UI mirroring support is irrelevant |
| 275 | state = ovutils::OV_UI_MIRROR; |
| 276 | } |
| 277 | |
| 278 | return state; |
| 279 | } |
| 280 | |
| 281 | /* Set overlay state */ |
| 282 | static void setOverlayState(ovutils::eOverlayState state) |
| 283 | { |
| 284 | overlay2::Overlay& ov = *(Overlay::getInstance()); |
| 285 | ov.setState(state); |
| 286 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 287 | |
| 288 | static void *hdmi_ui_loop(void *ptr) |
| 289 | { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 290 | private_module_t* m = reinterpret_cast<private_module_t*>(ptr); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 291 | while (1) { |
| 292 | pthread_mutex_lock(&m->overlayLock); |
| 293 | while(!(m->hdmiStateChanged)) |
| 294 | pthread_cond_wait(&(m->overlayPost), &(m->overlayLock)); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 295 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 296 | m->hdmiStateChanged = false; |
| 297 | if (m->exitHDMIUILoop) { |
| 298 | pthread_mutex_unlock(&m->overlayLock); |
| 299 | return NULL; |
| 300 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 301 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 302 | // No need to mirror UI if HDMI is not on |
| 303 | if (!m->enableHDMIOutput) { |
| 304 | ALOGE_IF(FB_DEBUG, "%s: hdmi not ON", __FUNCTION__); |
| 305 | pthread_mutex_unlock(&m->overlayLock); |
| 306 | continue; |
| 307 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 308 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 309 | overlay2::OverlayMgr* ovMgr = |
| 310 | overlay2::OverlayMgrSingleton::getOverlayMgr(); |
| 311 | overlay2::Overlay& ov = ovMgr->ov(); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 312 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 313 | // Set overlay state |
| 314 | ovutils::eOverlayState state = getOverlayState(m); |
| 315 | setOverlayState(state); |
| 316 | |
| 317 | // Determine the RGB pipe for UI depending on the state |
| 318 | ovutils::eDest dest = ovutils::OV_PIPE_ALL; |
| 319 | if (state == ovutils::OV_2D_TRUE_UI_MIRROR) { |
| 320 | // True UI mirroring state: external RGB pipe is OV_PIPE2 |
| 321 | dest = ovutils::OV_PIPE2; |
| 322 | } else if (state == ovutils::OV_UI_MIRROR) { |
| 323 | // UI-only mirroring state: external RGB pipe is OV_PIPE0 |
| 324 | dest = ovutils::OV_PIPE0; |
| 325 | } else { |
| 326 | // No UI in this case |
| 327 | pthread_mutex_unlock(&m->overlayLock); |
| 328 | continue; |
| 329 | } |
| 330 | |
| 331 | if (m->hdmiMirroringState == HDMI_UI_MIRRORING) { |
| 332 | int alignedW = ALIGN(m->info.xres, 32); |
| 333 | |
| 334 | private_handle_t const* hnd = |
| 335 | reinterpret_cast<private_handle_t const*>(m->framebuffer); |
| 336 | unsigned int width = alignedW; |
| 337 | unsigned int height = hnd->height; |
| 338 | unsigned int format = hnd->format; |
| 339 | unsigned int size = hnd->size/m->numBuffers; |
| 340 | |
| 341 | ovutils::eMdpFlags mdpFlags = ovutils::OV_MDP_FLAGS_NONE; |
| 342 | // External display connected during secure video playback |
| 343 | // Open secure UI session |
| 344 | // NOTE: when external display is already connected and then secure |
| 345 | // playback is started, we dont have to do anything |
| 346 | if (m->secureVideoOverlay) { |
| 347 | ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_SECURE_OVERLAY_SESSION); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 348 | } |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 349 | |
| 350 | ovutils::Whf whf(width, height, format, size); |
| 351 | ovutils::PipeArgs parg(mdpFlags, |
| 352 | ovutils::OVERLAY_TRANSFORM_0, |
| 353 | whf, |
| 354 | ovutils::WAIT, |
| 355 | ovutils::ZORDER_0, |
| 356 | ovutils::IS_FG_OFF, |
| 357 | ovutils::ROT_FLAG_ENABLED); |
| 358 | ovutils::PipeArgs pargs[ovutils::MAX_PIPES] = { parg, parg, parg }; |
| 359 | bool ret = ov.setSource(pargs, dest); |
| 360 | if (!ret) { |
| 361 | ALOGE("%s setSource failed", __FUNCTION__); |
| 362 | } |
| 363 | |
| 364 | // we need to communicate m->orientation that will get some |
| 365 | // modifications within setParameter func. |
| 366 | // FIXME that is ugly. |
| 367 | const ovutils::Params prms (ovutils::OVERLAY_TRANSFORM_UI, |
| 368 | m->orientation); |
| 369 | ov.setParameter(prms, dest); |
| 370 | if (!ret) { |
| 371 | ALOGE("%s setParameter failed transform", __FUNCTION__); |
| 372 | } |
| 373 | |
| 374 | // x,y,w,h |
| 375 | ovutils::Dim dcrop(0, 0, m->info.xres, m->info.yres); |
| 376 | ov.setMemoryId(m->framebuffer->fd, dest); |
| 377 | ret = ov.setCrop(dcrop, dest); |
| 378 | if (!ret) { |
| 379 | ALOGE("%s setCrop failed", __FUNCTION__); |
| 380 | } |
| 381 | |
| 382 | ovutils::Dim pdim (m->info.xres, |
| 383 | m->info.yres, |
| 384 | 0, |
| 385 | 0, |
| 386 | m->orientation); |
| 387 | ret = ov.setPosition(pdim, dest); |
| 388 | if (!ret) { |
| 389 | ALOGE("%s setPosition failed", __FUNCTION__); |
| 390 | } |
| 391 | |
| 392 | if (!ov.commit(dest)) { |
| 393 | ALOGE("%s commit fails", __FUNCTION__); |
| 394 | } |
| 395 | |
| 396 | ret = ov.queueBuffer(m->currentOffset, dest); |
| 397 | if (!ret) { |
| 398 | ALOGE("%s queueBuffer failed", __FUNCTION__); |
| 399 | } |
| 400 | } else { |
| 401 | setOverlayState(ovutils::OV_CLOSED); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 402 | } |
| 403 | pthread_mutex_unlock(&m->overlayLock); |
| 404 | } |
| 405 | return NULL; |
| 406 | } |
| 407 | |
| 408 | static int fb_videoOverlayStarted(struct framebuffer_device_t* dev, int started) |
| 409 | { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 410 | ALOGE_IF(FB_DEBUG, "%s started=%d", __FUNCTION__, started); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 411 | private_module_t* m = reinterpret_cast<private_module_t*>( |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 412 | dev->common.module); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 413 | pthread_mutex_lock(&m->overlayLock); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 414 | if(started != m->videoOverlay) { |
| 415 | m->videoOverlay = started; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 416 | m->hdmiStateChanged = true; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 417 | if (!m->trueMirrorSupport) { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 418 | if (started) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 419 | m->hdmiMirroringState = HDMI_NO_MIRRORING; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 420 | ovutils::eOverlayState state = getOverlayState(m); |
| 421 | setOverlayState(state); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 422 | } else if (m->enableHDMIOutput) |
| 423 | m->hdmiMirroringState = HDMI_UI_MIRRORING; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 424 | } else { |
| 425 | if (m->videoOverlay == VIDEO_3D_OVERLAY_STARTED) { |
| 426 | ALOGE_IF(FB_DEBUG, "3D Video Started, stop mirroring!"); |
| 427 | m->hdmiMirroringState = HDMI_NO_MIRRORING; |
| 428 | ovutils::eOverlayState state = getOverlayState(m); |
| 429 | setOverlayState(state); |
| 430 | } |
| 431 | else if (m->enableHDMIOutput) { |
| 432 | m->hdmiMirroringState = HDMI_UI_MIRRORING; |
| 433 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | pthread_mutex_unlock(&m->overlayLock); |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | static int fb_enableHDMIOutput(struct framebuffer_device_t* dev, int externaltype) |
| 441 | { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 442 | ALOGE_IF(FB_DEBUG, "%s externaltype=%d", __FUNCTION__, externaltype); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 443 | private_module_t* m = reinterpret_cast<private_module_t*>( |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 444 | dev->common.module); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 445 | pthread_mutex_lock(&m->overlayLock); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 446 | //Check if true mirroring can be supported |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 447 | m->trueMirrorSupport = ovutils::FrameBufferInfo::getInstance()->supportTrueMirroring(); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 448 | m->enableHDMIOutput = externaltype; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 449 | if(externaltype) { |
| 450 | if (m->trueMirrorSupport) { |
| 451 | m->hdmiMirroringState = HDMI_UI_MIRRORING; |
| 452 | } else { |
| 453 | if(!m->videoOverlay) |
| 454 | m->hdmiMirroringState = HDMI_UI_MIRRORING; |
| 455 | } |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 456 | } else if (!externaltype) { |
| 457 | // Either HDMI is disconnected or suspend occurred |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 458 | m->hdmiMirroringState = HDMI_NO_MIRRORING; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 459 | ovutils::eOverlayState state = getOverlayState(m); |
| 460 | setOverlayState(state); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 461 | } |
| 462 | m->hdmiStateChanged = true; |
| 463 | pthread_cond_signal(&(m->overlayPost)); |
| 464 | pthread_mutex_unlock(&m->overlayLock); |
| 465 | return 0; |
| 466 | } |
| 467 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 468 | static int fb_orientationChanged(struct framebuffer_device_t* dev, int orientation) |
| 469 | { |
| 470 | private_module_t* m = reinterpret_cast<private_module_t*>( |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 471 | dev->common.module); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 472 | pthread_mutex_lock(&m->overlayLock); |
| 473 | neworientation = orientation; |
| 474 | pthread_mutex_unlock(&m->overlayLock); |
| 475 | return 0; |
| 476 | } |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 477 | |
| 478 | static int handle_open_secure_start(private_module_t* m) { |
| 479 | pthread_mutex_lock(&m->overlayLock); |
| 480 | m->hdmiMirroringState = HDMI_NO_MIRRORING; |
| 481 | m->secureVideoOverlay = true; |
| 482 | pthread_mutex_unlock(&m->overlayLock); |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | static int handle_open_secure_end(private_module_t* m) { |
| 487 | pthread_mutex_lock(&m->overlayLock); |
| 488 | if (m->enableHDMIOutput) { |
| 489 | if (m->trueMirrorSupport) { |
| 490 | m->hdmiMirroringState = HDMI_UI_MIRRORING; |
| 491 | } else if(!m->videoOverlay) { |
| 492 | m->hdmiMirroringState = HDMI_UI_MIRRORING; |
| 493 | } |
| 494 | m->hdmiStateChanged = true; |
| 495 | pthread_cond_signal(&(m->overlayPost)); |
| 496 | } |
| 497 | pthread_mutex_unlock(&m->overlayLock); |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | static int handle_close_secure_start(private_module_t* m) { |
| 502 | pthread_mutex_lock(&m->overlayLock); |
| 503 | m->hdmiMirroringState = HDMI_NO_MIRRORING; |
| 504 | m->secureVideoOverlay = false; |
| 505 | pthread_mutex_unlock(&m->overlayLock); |
| 506 | return 0; |
| 507 | } |
| 508 | |
| 509 | static int handle_close_secure_end(private_module_t* m) { |
| 510 | pthread_mutex_lock(&m->overlayLock); |
| 511 | if (m->enableHDMIOutput) { |
| 512 | if (m->trueMirrorSupport) { |
| 513 | m->hdmiMirroringState = HDMI_UI_MIRRORING; |
| 514 | } else if(!m->videoOverlay) { |
| 515 | m->hdmiMirroringState = HDMI_UI_MIRRORING; |
| 516 | } |
| 517 | m->hdmiStateChanged = true; |
| 518 | pthread_cond_signal(&(m->overlayPost)); |
| 519 | } |
| 520 | pthread_mutex_unlock(&m->overlayLock); |
| 521 | return 0; |
| 522 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 523 | #endif |
| 524 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 525 | |
| 526 | |
| 527 | /* fb_perform - used to add custom event and handle them in fb HAL |
| 528 | * Used for external display related functions as of now |
| 529 | */ |
| 530 | static int fb_perform(struct framebuffer_device_t* dev, int event, int value) |
| 531 | { |
| 532 | private_module_t* m = reinterpret_cast<private_module_t*>( |
| 533 | dev->common.module); |
| 534 | switch(event) { |
| 535 | #if defined(HDMI_DUAL_DISPLAY) |
| 536 | case EVENT_EXTERNAL_DISPLAY: |
| 537 | fb_enableHDMIOutput(dev, value); |
| 538 | break; |
| 539 | case EVENT_VIDEO_OVERLAY: |
| 540 | fb_videoOverlayStarted(dev, value); |
| 541 | break; |
| 542 | case EVENT_ORIENTATION_CHANGE: |
| 543 | fb_orientationChanged(dev, value); |
| 544 | break; |
| 545 | case EVENT_OVERLAY_STATE_CHANGE: |
| 546 | if (value == OVERLAY_STATE_CHANGE_START) { |
| 547 | // When state change starts, get a lock on overlay |
| 548 | pthread_mutex_lock(&m->overlayLock); |
| 549 | } else if (value == OVERLAY_STATE_CHANGE_END) { |
| 550 | // When state change is complete, unlock overlay |
| 551 | pthread_mutex_unlock(&m->overlayLock); |
| 552 | } |
| 553 | break; |
| 554 | case EVENT_OPEN_SECURE_START: |
| 555 | handle_open_secure_start(m); |
| 556 | break; |
| 557 | case EVENT_OPEN_SECURE_END: |
| 558 | handle_open_secure_end(m); |
| 559 | break; |
| 560 | case EVENT_CLOSE_SECURE_START: |
| 561 | handle_close_secure_start(m); |
| 562 | break; |
| 563 | case EVENT_CLOSE_SECURE_END: |
| 564 | handle_close_secure_end(m); |
| 565 | break; |
| 566 | #endif |
| 567 | default: |
| 568 | ALOGE("In %s: UNKNOWN Event = %d!!!", __FUNCTION__, event); |
| 569 | break; |
| 570 | } |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 575 | static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer) |
| 576 | { |
| 577 | if (private_handle_t::validate(buffer) < 0) |
| 578 | return -EINVAL; |
| 579 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 580 | int nxtIdx;//, futureIdx = -1; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 581 | bool reuse; |
| 582 | struct qbuf_t qb; |
| 583 | fb_context_t* ctx = (fb_context_t*)dev; |
| 584 | |
| 585 | private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(buffer); |
| 586 | private_module_t* m = reinterpret_cast<private_module_t*>( |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 587 | dev->common.module); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 588 | |
| 589 | if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) { |
| 590 | |
| 591 | reuse = false; |
| 592 | nxtIdx = (m->currentIdx + 1) % m->numBuffers; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 593 | //futureIdx = (nxtIdx + 1) % m->numBuffers; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 594 | if (m->swapInterval == 0) { |
| 595 | // if SwapInterval = 0 and no buffers available then reuse |
| 596 | // current buf for next rendering so don't post new buffer |
| 597 | if (pthread_mutex_trylock(&(m->avail[nxtIdx].lock))) { |
| 598 | reuse = true; |
| 599 | } else { |
| 600 | if (! m->avail[nxtIdx].is_avail) |
| 601 | reuse = true; |
| 602 | pthread_mutex_unlock(&(m->avail[nxtIdx].lock)); |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | if(!reuse){ |
| 607 | // unlock previous ("current") Buffer and lock the new buffer |
| 608 | m->base.lock(&m->base, buffer, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 609 | PRIV_USAGE_LOCKED_FOR_POST, |
| 610 | 0,0, m->info.xres, m->info.yres, NULL); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 611 | |
| 612 | // post/queue the new buffer |
| 613 | pthread_mutex_lock(&(m->avail[nxtIdx].lock)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 614 | m->avail[nxtIdx].is_avail = false; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 615 | #if 0 //XXX: Triple FB |
| 616 | if (m->avail[nxtIdx].is_avail != true) { |
| 617 | ALOGE_IF(m->swapInterval != 0, "Found %d buf to be not avail", nxtIdx); |
| 618 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 619 | |
| 620 | if (m->avail[nxtIdx].state != AVL) { |
| 621 | ALOGD("[%d] state %c, expected %c", nxtIdx, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 622 | framebufferStateName[m->avail[nxtIdx].state], |
| 623 | framebufferStateName[AVL]); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | m->avail[nxtIdx].state = SUB; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 627 | #endif |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 628 | pthread_mutex_unlock(&(m->avail[nxtIdx].lock)); |
| 629 | |
| 630 | qb.idx = nxtIdx; |
| 631 | qb.buf = buffer; |
| 632 | pthread_mutex_lock(&(m->qlock)); |
| 633 | m->disp.push(qb); |
| 634 | pthread_cond_signal(&(m->qpost)); |
| 635 | pthread_mutex_unlock(&(m->qlock)); |
| 636 | |
| 637 | if (m->currentBuffer) |
| 638 | m->base.unlock(&m->base, m->currentBuffer); |
| 639 | |
| 640 | m->currentBuffer = buffer; |
| 641 | m->currentIdx = nxtIdx; |
| 642 | } else { |
| 643 | if (m->currentBuffer) |
| 644 | m->base.unlock(&m->base, m->currentBuffer); |
| 645 | m->base.lock(&m->base, buffer, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 646 | PRIV_USAGE_LOCKED_FOR_POST, |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 647 | 0,0, m->info.xres, m->info.yres, NULL); |
| 648 | m->currentBuffer = buffer; |
| 649 | } |
| 650 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 651 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | static int fb_compositionComplete(struct framebuffer_device_t* dev) |
| 656 | { |
| 657 | // TODO: Properly implement composition complete callback |
| 658 | glFinish(); |
| 659 | |
| 660 | return 0; |
| 661 | } |
| 662 | |
| 663 | static int fb_lockBuffer(struct framebuffer_device_t* dev, int index) |
| 664 | { |
| 665 | private_module_t* m = reinterpret_cast<private_module_t*>( |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 666 | dev->common.module); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 667 | |
| 668 | // Return immediately if the buffer is available |
| 669 | if ((m->avail[index].state == AVL) || (m->swapInterval == 0)) |
| 670 | return 0; |
| 671 | |
| 672 | pthread_mutex_lock(&(m->avail[index].lock)); |
| 673 | while (m->avail[index].state != AVL) { |
| 674 | pthread_cond_wait(&(m->avail[index].cond), |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 675 | &(m->avail[index].lock)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 676 | } |
| 677 | pthread_mutex_unlock(&(m->avail[index].lock)); |
| 678 | |
| 679 | return 0; |
| 680 | } |
| 681 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 682 | int mapFrameBufferLocked(struct private_module_t* module) |
| 683 | { |
| 684 | // already initialized... |
| 685 | if (module->framebuffer) { |
| 686 | return 0; |
| 687 | } |
| 688 | char const * const device_template[] = { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 689 | "/dev/graphics/fb%u", |
| 690 | "/dev/fb%u", |
| 691 | 0 }; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 692 | |
| 693 | int fd = -1; |
| 694 | int i=0; |
| 695 | char name[64]; |
| 696 | char property[PROPERTY_VALUE_MAX]; |
| 697 | |
| 698 | while ((fd==-1) && device_template[i]) { |
| 699 | snprintf(name, 64, device_template[i], 0); |
| 700 | fd = open(name, O_RDWR, 0); |
| 701 | i++; |
| 702 | } |
| 703 | if (fd < 0) |
| 704 | return -errno; |
| 705 | |
| 706 | struct fb_fix_screeninfo finfo; |
| 707 | if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) |
| 708 | return -errno; |
| 709 | |
| 710 | struct fb_var_screeninfo info; |
| 711 | if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) |
| 712 | return -errno; |
| 713 | |
| 714 | info.reserved[0] = 0; |
| 715 | info.reserved[1] = 0; |
| 716 | info.reserved[2] = 0; |
| 717 | info.xoffset = 0; |
| 718 | info.yoffset = 0; |
| 719 | info.activate = FB_ACTIVATE_NOW; |
| 720 | |
| 721 | /* Interpretation of offset for color fields: All offsets are from the right, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 722 | * inside a "pixel" value, which is exactly 'bits_per_pixel' wide (means: you |
| 723 | * can use the offset as right argument to <<). A pixel afterwards is a bit |
| 724 | * stream and is written to video memory as that unmodified. This implies |
| 725 | * big-endian byte order if bits_per_pixel is greater than 8. |
| 726 | */ |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 727 | |
| 728 | if(info.bits_per_pixel == 32) { |
| 729 | /* |
| 730 | * Explicitly request RGBA_8888 |
| 731 | */ |
| 732 | info.bits_per_pixel = 32; |
| 733 | info.red.offset = 24; |
| 734 | info.red.length = 8; |
| 735 | info.green.offset = 16; |
| 736 | info.green.length = 8; |
| 737 | info.blue.offset = 8; |
| 738 | info.blue.length = 8; |
| 739 | info.transp.offset = 0; |
| 740 | info.transp.length = 8; |
| 741 | |
| 742 | /* Note: the GL driver does not have a r=8 g=8 b=8 a=0 config, so if we do |
| 743 | * not use the MDP for composition (i.e. hw composition == 0), ask for |
| 744 | * RGBA instead of RGBX. */ |
| 745 | if (property_get("debug.sf.hw", property, NULL) > 0 && atoi(property) == 0) |
| 746 | module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 747 | else if(property_get("debug.composition.type", property, NULL) > 0 && |
| 748 | (strncmp(property, "mdp", 3) == 0)) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 749 | module->fbFormat = HAL_PIXEL_FORMAT_RGBX_8888; |
| 750 | else |
| 751 | module->fbFormat = HAL_PIXEL_FORMAT_RGBA_8888; |
| 752 | } else { |
| 753 | /* |
| 754 | * Explicitly request 5/6/5 |
| 755 | */ |
| 756 | info.bits_per_pixel = 16; |
| 757 | info.red.offset = 11; |
| 758 | info.red.length = 5; |
| 759 | info.green.offset = 5; |
| 760 | info.green.length = 6; |
| 761 | info.blue.offset = 0; |
| 762 | info.blue.length = 5; |
| 763 | info.transp.offset = 0; |
| 764 | info.transp.length = 0; |
| 765 | module->fbFormat = HAL_PIXEL_FORMAT_RGB_565; |
| 766 | } |
| 767 | |
| 768 | //adreno needs 4k aligned offsets. Max hole size is 4096-1 |
| 769 | int size = roundUpToPageSize(info.yres * info.xres * (info.bits_per_pixel/8)); |
| 770 | |
| 771 | /* |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 772 | * Request NUM_BUFFERS screens (at least 2 for page flipping) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 773 | */ |
| 774 | int numberOfBuffers = (int)(finfo.smem_len/size); |
| 775 | ALOGV("num supported framebuffers in kernel = %d", numberOfBuffers); |
| 776 | |
| 777 | if (property_get("debug.gr.numframebuffers", property, NULL) > 0) { |
| 778 | int num = atoi(property); |
| 779 | if ((num >= NUM_FRAMEBUFFERS_MIN) && (num <= NUM_FRAMEBUFFERS_MAX)) { |
| 780 | numberOfBuffers = num; |
| 781 | } |
| 782 | } |
| 783 | if (numberOfBuffers > NUM_FRAMEBUFFERS_MAX) |
| 784 | numberOfBuffers = NUM_FRAMEBUFFERS_MAX; |
| 785 | |
| 786 | ALOGV("We support %d buffers", numberOfBuffers); |
| 787 | |
| 788 | //consider the included hole by 4k alignment |
| 789 | uint32_t line_length = (info.xres * info.bits_per_pixel / 8); |
| 790 | info.yres_virtual = (size * numberOfBuffers) / line_length; |
| 791 | |
| 792 | uint32_t flags = PAGE_FLIP; |
| 793 | if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) { |
| 794 | info.yres_virtual = size / line_length; |
| 795 | flags &= ~PAGE_FLIP; |
| 796 | ALOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported"); |
| 797 | } |
| 798 | |
| 799 | if (info.yres_virtual < ((size * 2) / line_length) ) { |
| 800 | // we need at least 2 for page-flipping |
| 801 | info.yres_virtual = size / line_length; |
| 802 | flags &= ~PAGE_FLIP; |
| 803 | ALOGW("page flipping not supported (yres_virtual=%d, requested=%d)", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 804 | info.yres_virtual, info.yres*2); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) |
| 808 | return -errno; |
| 809 | |
| 810 | if (int(info.width) <= 0 || int(info.height) <= 0) { |
| 811 | // the driver doesn't return that information |
| 812 | // default to 160 dpi |
| 813 | info.width = ((info.xres * 25.4f)/160.0f + 0.5f); |
| 814 | info.height = ((info.yres * 25.4f)/160.0f + 0.5f); |
| 815 | } |
| 816 | |
| 817 | float xdpi = (info.xres * 25.4f) / info.width; |
| 818 | float ydpi = (info.yres * 25.4f) / info.height; |
choongryeol.lee | 26145b8 | 2012-06-26 23:41:00 -0700 | [diff] [blame^] | 819 | //The reserved[3] field is used to store FPS by the driver. |
| 820 | float fps = info.reserved[3]; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 821 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 822 | ALOGI("using (fd=%d)\n" |
| 823 | "id = %s\n" |
| 824 | "xres = %d px\n" |
| 825 | "yres = %d px\n" |
| 826 | "xres_virtual = %d px\n" |
| 827 | "yres_virtual = %d px\n" |
| 828 | "bpp = %d\n" |
| 829 | "r = %2u:%u\n" |
| 830 | "g = %2u:%u\n" |
| 831 | "b = %2u:%u\n", |
| 832 | fd, |
| 833 | finfo.id, |
| 834 | info.xres, |
| 835 | info.yres, |
| 836 | info.xres_virtual, |
| 837 | info.yres_virtual, |
| 838 | info.bits_per_pixel, |
| 839 | info.red.offset, info.red.length, |
| 840 | info.green.offset, info.green.length, |
| 841 | info.blue.offset, info.blue.length |
| 842 | ); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 843 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 844 | ALOGI("width = %d mm (%f dpi)\n" |
| 845 | "height = %d mm (%f dpi)\n" |
| 846 | "refresh rate = %.2f Hz\n", |
| 847 | info.width, xdpi, |
| 848 | info.height, ydpi, |
| 849 | fps |
| 850 | ); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 851 | |
| 852 | |
| 853 | if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) |
| 854 | return -errno; |
| 855 | |
| 856 | if (finfo.smem_len <= 0) |
| 857 | return -errno; |
| 858 | |
| 859 | module->flags = flags; |
| 860 | module->info = info; |
| 861 | module->finfo = finfo; |
| 862 | module->xdpi = xdpi; |
| 863 | module->ydpi = ydpi; |
| 864 | module->fps = fps; |
| 865 | |
| 866 | #ifdef NO_SURFACEFLINGER_SWAPINTERVAL |
| 867 | char pval[PROPERTY_VALUE_MAX]; |
| 868 | property_get("debug.gr.swapinterval", pval, "1"); |
| 869 | module->swapInterval = atoi(pval); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 870 | if (module->swapInterval < PRIV_MIN_SWAP_INTERVAL || |
| 871 | module->swapInterval > PRIV_MAX_SWAP_INTERVAL) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 872 | module->swapInterval = 1; |
| 873 | ALOGW("Out of range (%d to %d) value for debug.gr.swapinterval, using 1", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 874 | PRIV_MIN_SWAP_INTERVAL, |
| 875 | PRIV_MAX_SWAP_INTERVAL); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | #else |
| 879 | /* when surfaceflinger supports swapInterval then can just do this */ |
| 880 | module->swapInterval = 1; |
| 881 | #endif |
| 882 | |
| 883 | CALC_INIT(); |
| 884 | |
| 885 | module->currentIdx = -1; |
| 886 | pthread_cond_init(&(module->qpost), NULL); |
| 887 | pthread_mutex_init(&(module->qlock), NULL); |
| 888 | for (i = 0; i < NUM_FRAMEBUFFERS_MAX; i++) { |
| 889 | pthread_mutex_init(&(module->avail[i].lock), NULL); |
| 890 | pthread_cond_init(&(module->avail[i].cond), NULL); |
| 891 | module->avail[i].is_avail = true; |
| 892 | module->avail[i].state = AVL; |
| 893 | } |
| 894 | |
| 895 | /* create display update thread */ |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 896 | pthread_t disp_thread; |
| 897 | if (pthread_create(&disp_thread, NULL, &disp_loop, (void *) module)) { |
| 898 | return -errno; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | /* |
| 902 | * map the framebuffer |
| 903 | */ |
| 904 | |
| 905 | int err; |
| 906 | module->numBuffers = info.yres_virtual / info.yres; |
| 907 | module->bufferMask = 0; |
| 908 | //adreno needs page aligned offsets. Align the fbsize to pagesize. |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 909 | size_t fbSize = roundUpToPageSize(finfo.line_length * info.yres)* |
| 910 | module->numBuffers; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 911 | module->framebuffer = new private_handle_t(fd, fbSize, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 912 | private_handle_t::PRIV_FLAGS_USES_PMEM, |
| 913 | BUFFER_TYPE_UI, |
| 914 | module->fbFormat, info.xres, info.yres); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 915 | void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); |
| 916 | if (vaddr == MAP_FAILED) { |
| 917 | ALOGE("Error mapping the framebuffer (%s)", strerror(errno)); |
| 918 | return -errno; |
| 919 | } |
| 920 | module->framebuffer->base = intptr_t(vaddr); |
| 921 | memset(vaddr, 0, fbSize); |
| 922 | |
| 923 | #if defined(HDMI_DUAL_DISPLAY) |
| 924 | /* Overlay for HDMI*/ |
| 925 | pthread_mutex_init(&(module->overlayLock), NULL); |
| 926 | pthread_cond_init(&(module->overlayPost), NULL); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 927 | module->currentOffset = 0; |
| 928 | module->exitHDMIUILoop = false; |
| 929 | module->hdmiStateChanged = false; |
| 930 | pthread_t hdmiUIThread; |
| 931 | pthread_create(&hdmiUIThread, NULL, &hdmi_ui_loop, (void *) module); |
| 932 | module->hdmiMirroringState = HDMI_NO_MIRRORING; |
| 933 | module->trueMirrorSupport = false; |
| 934 | #endif |
| 935 | |
| 936 | return 0; |
| 937 | } |
| 938 | |
| 939 | static int mapFrameBuffer(struct private_module_t* module) |
| 940 | { |
| 941 | pthread_mutex_lock(&module->lock); |
| 942 | int err = mapFrameBufferLocked(module); |
| 943 | pthread_mutex_unlock(&module->lock); |
| 944 | return err; |
| 945 | } |
| 946 | |
| 947 | /*****************************************************************************/ |
| 948 | |
| 949 | static int fb_close(struct hw_device_t *dev) |
| 950 | { |
| 951 | fb_context_t* ctx = (fb_context_t*)dev; |
| 952 | #if defined(HDMI_DUAL_DISPLAY) |
| 953 | private_module_t* m = reinterpret_cast<private_module_t*>( |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 954 | ctx->device.common.module); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 955 | pthread_mutex_lock(&m->overlayLock); |
| 956 | m->exitHDMIUILoop = true; |
| 957 | pthread_cond_signal(&(m->overlayPost)); |
| 958 | pthread_mutex_unlock(&m->overlayLock); |
| 959 | #endif |
| 960 | if (ctx) { |
| 961 | free(ctx); |
| 962 | } |
| 963 | return 0; |
| 964 | } |
| 965 | |
| 966 | int fb_device_open(hw_module_t const* module, const char* name, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 967 | hw_device_t** device) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 968 | { |
| 969 | int status = -EINVAL; |
| 970 | if (!strcmp(name, GRALLOC_HARDWARE_FB0)) { |
| 971 | alloc_device_t* gralloc_device; |
| 972 | status = gralloc_open(module, &gralloc_device); |
| 973 | if (status < 0) |
| 974 | return status; |
| 975 | |
| 976 | /* initialize our state here */ |
| 977 | fb_context_t *dev = (fb_context_t*)malloc(sizeof(*dev)); |
| 978 | memset(dev, 0, sizeof(*dev)); |
| 979 | |
| 980 | /* initialize the procs */ |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 981 | dev->device.common.tag = HARDWARE_DEVICE_TAG; |
| 982 | dev->device.common.version = 0; |
| 983 | dev->device.common.module = const_cast<hw_module_t*>(module); |
| 984 | dev->device.common.close = fb_close; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 985 | dev->device.setSwapInterval = fb_setSwapInterval; |
| 986 | dev->device.post = fb_post; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 987 | dev->device.setUpdateRect = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 988 | dev->device.compositionComplete = fb_compositionComplete; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 989 | |
| 990 | private_module_t* m = (private_module_t*)module; |
| 991 | status = mapFrameBuffer(m); |
| 992 | if (status >= 0) { |
| 993 | int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3); |
| 994 | const_cast<uint32_t&>(dev->device.flags) = 0; |
| 995 | const_cast<uint32_t&>(dev->device.width) = m->info.xres; |
| 996 | const_cast<uint32_t&>(dev->device.height) = m->info.yres; |
| 997 | const_cast<int&>(dev->device.stride) = stride; |
| 998 | const_cast<int&>(dev->device.format) = m->fbFormat; |
| 999 | const_cast<float&>(dev->device.xdpi) = m->xdpi; |
| 1000 | const_cast<float&>(dev->device.ydpi) = m->ydpi; |
| 1001 | const_cast<float&>(dev->device.fps) = m->fps; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 1002 | const_cast<int&>(dev->device.minSwapInterval) = PRIV_MIN_SWAP_INTERVAL; |
| 1003 | const_cast<int&>(dev->device.maxSwapInterval) = PRIV_MAX_SWAP_INTERVAL; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 1004 | if (m->finfo.reserved[0] == 0x5444 && |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 1005 | m->finfo.reserved[1] == 0x5055) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 1006 | dev->device.setUpdateRect = fb_setUpdateRect; |
| 1007 | ALOGD("UPDATE_ON_DEMAND supported"); |
| 1008 | } |
| 1009 | |
| 1010 | *device = &dev->device.common; |
| 1011 | } |
| 1012 | |
| 1013 | // Close the gralloc module |
| 1014 | gralloc_close(gralloc_device); |
| 1015 | } |
| 1016 | return status; |
| 1017 | } |