Chirayu Desai | 0a336cc | 2012-07-12 14:37:05 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * |
| 19 | * @author Rama, Meka(v.meka@samsung.com) |
| 20 | Sangwoo, Park(sw5771.park@samsung.com) |
| 21 | Jamie Oh (jung-min.oh@samsung.com) |
| 22 | * @date 2011-07-28 |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include <sys/resource.h> |
| 27 | #include <cutils/log.h> |
| 28 | #include <cutils/atomic.h> |
| 29 | #include <EGL/egl.h> |
| 30 | #include <GLES/gl.h> |
| 31 | #include <hardware_legacy/uevent.h> |
| 32 | #include "SecHWCUtils.h" |
| 33 | |
| 34 | static IMG_gralloc_module_public_t *gpsGrallocModule; |
| 35 | |
| 36 | static int hwc_device_open(const struct hw_module_t* module, const char* name, |
| 37 | struct hw_device_t** device); |
| 38 | |
| 39 | static struct hw_module_methods_t hwc_module_methods = { |
| 40 | open: hwc_device_open |
| 41 | }; |
| 42 | |
| 43 | hwc_module_t HAL_MODULE_INFO_SYM = { |
| 44 | common: { |
| 45 | tag: HARDWARE_MODULE_TAG, |
| 46 | module_api_version: HWC_MODULE_API_VERSION_0_1, |
| 47 | hal_api_version: HARDWARE_HAL_API_VERSION, |
| 48 | id: HWC_HARDWARE_MODULE_ID, |
| 49 | name: "Samsung S5PC11X hwcomposer module", |
| 50 | author: "SAMSUNG", |
| 51 | methods: &hwc_module_methods, |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | static void dump_layer(hwc_layer_t const* l) { |
| 56 | ALOGD("\ttype=%d, flags=%08x, handle=%p, tr=%02x, blend=%04x, {%d,%d,%d,%d}, {%d,%d,%d,%d}", |
| 57 | l->compositionType, l->flags, l->handle, l->transform, l->blending, |
| 58 | l->sourceCrop.left, |
| 59 | l->sourceCrop.top, |
| 60 | l->sourceCrop.right, |
| 61 | l->sourceCrop.bottom, |
| 62 | l->displayFrame.left, |
| 63 | l->displayFrame.top, |
| 64 | l->displayFrame.right, |
| 65 | l->displayFrame.bottom); |
| 66 | } |
| 67 | |
| 68 | static int set_src_dst_info(hwc_layer_t *cur, |
| 69 | struct hwc_win_info_t *win, |
| 70 | struct sec_img *src_img, |
| 71 | struct sec_img *dst_img, |
| 72 | struct sec_rect *src_rect, |
| 73 | struct sec_rect *dst_rect, |
| 74 | int win_idx) |
| 75 | { |
| 76 | IMG_native_handle_t *prev_handle = (IMG_native_handle_t *)(cur->handle); |
| 77 | |
| 78 | // set src image |
| 79 | src_img->w = prev_handle->iWidth; |
| 80 | src_img->h = prev_handle->iHeight; |
| 81 | src_img->format = prev_handle->iFormat; |
| 82 | src_img->base = NULL; |
| 83 | src_img->offset = 0; |
| 84 | src_img->mem_id =0; |
| 85 | |
| 86 | src_img->mem_type = HWC_PHYS_MEM_TYPE; |
| 87 | src_img->w = (src_img->w + 15) & (~15); |
| 88 | src_img->h = (src_img->h + 1) & (~1) ; |
| 89 | |
| 90 | //set src rect |
| 91 | src_rect->x = SEC_MAX(cur->sourceCrop.left, 0); |
| 92 | src_rect->y = SEC_MAX(cur->sourceCrop.top, 0); |
| 93 | src_rect->w = SEC_MAX(cur->sourceCrop.right - cur->sourceCrop.left, 0); |
| 94 | src_rect->w = SEC_MIN(src_rect->w, src_img->w - src_rect->x); |
| 95 | src_rect->h = SEC_MAX(cur->sourceCrop.bottom - cur->sourceCrop.top, 0); |
| 96 | src_rect->h = SEC_MIN(src_rect->h, src_img->h - src_rect->y); |
| 97 | |
| 98 | //set dst image |
| 99 | dst_img->w = win->lcd_info.xres; |
| 100 | dst_img->h = win->lcd_info.yres; |
| 101 | |
| 102 | switch (win->lcd_info.bits_per_pixel) { |
| 103 | case 32: |
| 104 | dst_img->format = HAL_PIXEL_FORMAT_RGBX_8888; |
| 105 | break; |
| 106 | default: |
| 107 | dst_img->format = HAL_PIXEL_FORMAT_RGB_565; |
| 108 | break; |
| 109 | } |
| 110 | |
| 111 | dst_img->base = win->addr[win->buf_index]; |
| 112 | dst_img->offset = 0; |
| 113 | dst_img->mem_id = 0; |
| 114 | dst_img->mem_type = HWC_PHYS_MEM_TYPE; |
| 115 | |
| 116 | //set dst rect |
| 117 | //fimc dst image will be stored from left top corner |
| 118 | dst_rect->x = 0; |
| 119 | dst_rect->y = 0; |
| 120 | dst_rect->w = win->rect_info.w; |
| 121 | dst_rect->h = win->rect_info.h; |
| 122 | |
| 123 | ALOGV("%s::sr_x %d sr_y %d sr_w %d sr_h %d dr_x %d dr_y %d dr_w %d dr_h %d ", |
| 124 | __func__, src_rect->x, src_rect->y, src_rect->w, src_rect->h, |
| 125 | dst_rect->x, dst_rect->y, dst_rect->w, dst_rect->h); |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static int get_hwc_compos_decision(hwc_layer_t* cur) |
| 131 | { |
| 132 | if(cur->flags & HWC_SKIP_LAYER || !cur->handle) { |
| 133 | ALOGV("%s::is_skip_layer %d cur->handle %x", |
| 134 | __func__, cur->flags & HWC_SKIP_LAYER, (uint32_t)cur->handle); |
| 135 | return HWC_FRAMEBUFFER; |
| 136 | } |
| 137 | |
| 138 | IMG_native_handle_t *prev_handle = (IMG_native_handle_t *)(cur->handle); |
| 139 | int compositionType = HWC_FRAMEBUFFER; |
| 140 | |
| 141 | /* check here....if we have any resolution constraints */ |
| 142 | if (((cur->sourceCrop.right - cur->sourceCrop.left) < 16) || |
| 143 | ((cur->sourceCrop.bottom - cur->sourceCrop.top) < 8)) |
| 144 | return compositionType; |
| 145 | |
| 146 | if ((cur->transform == HAL_TRANSFORM_ROT_90) || |
| 147 | (cur->transform == HAL_TRANSFORM_ROT_270)) { |
| 148 | if(((cur->displayFrame.right - cur->displayFrame.left) < 4)|| |
| 149 | ((cur->displayFrame.bottom - cur->displayFrame.top) < 8)) |
| 150 | return compositionType; |
| 151 | } else if (((cur->displayFrame.right - cur->displayFrame.left) < 8) || |
| 152 | ((cur->displayFrame.bottom - cur->displayFrame.top) < 4)) |
| 153 | return compositionType; |
| 154 | |
| 155 | if((prev_handle->usage & GRALLOC_USAGE_PHYS_CONTIG) && |
| 156 | (cur->blending == HWC_BLENDING_NONE)) |
| 157 | compositionType = HWC_OVERLAY; |
| 158 | else |
| 159 | compositionType = HWC_FRAMEBUFFER; |
| 160 | |
| 161 | ALOGV("%s::compositionType %d bpp %d format %x usage %x", |
| 162 | __func__,compositionType, prev_handle->uiBpp, prev_handle->iFormat, |
| 163 | prev_handle->usage & GRALLOC_USAGE_PHYS_CONTIG); |
| 164 | |
| 165 | return compositionType; |
| 166 | } |
| 167 | |
| 168 | static int assign_overlay_window(struct hwc_context_t *ctx, |
| 169 | hwc_layer_t *cur, |
| 170 | int win_idx, |
| 171 | int layer_idx) |
| 172 | { |
| 173 | struct hwc_win_info_t *win; |
| 174 | sec_rect rect; |
| 175 | int ret = 0; |
| 176 | |
| 177 | if(NUM_OF_WIN <= win_idx) |
| 178 | return -1; |
| 179 | |
| 180 | win = &ctx->win[win_idx]; |
| 181 | |
| 182 | rect.x = SEC_MAX(cur->displayFrame.left, 0); |
| 183 | rect.y = SEC_MAX(cur->displayFrame.top, 0); |
| 184 | rect.w = SEC_MIN(cur->displayFrame.right - rect.x, win->lcd_info.xres - rect.x); |
| 185 | rect.h = SEC_MIN(cur->displayFrame.bottom - rect.y, win->lcd_info.yres - rect.y); |
| 186 | win->set_win_flag = 0; |
| 187 | |
| 188 | if((rect.x != win->rect_info.x) || (rect.y != win->rect_info.y) || |
| 189 | (rect.w != win->rect_info.w) || (rect.h != win->rect_info.h)){ |
| 190 | win->rect_info.x = rect.x; |
| 191 | win->rect_info.y = rect.y; |
| 192 | win->rect_info.w = rect.w; |
| 193 | win->rect_info.h = rect.h; |
| 194 | win->set_win_flag = 1; |
| 195 | win->layer_prev_buf = 0; |
| 196 | } |
| 197 | |
| 198 | win->layer_index = layer_idx; |
| 199 | win->status = HWC_WIN_RESERVED; |
| 200 | |
| 201 | ALOGV("%s:: win_x %d win_y %d win_w %d win_h %d lay_idx %d win_idx %d", |
| 202 | __func__, win->rect_info.x, win->rect_info.y, win->rect_info.w, |
| 203 | win->rect_info.h, win->layer_index, win_idx ); |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | static void reset_win_rect_info(hwc_win_info_t *win) |
| 209 | { |
| 210 | win->rect_info.x = 0; |
| 211 | win->rect_info.y = 0; |
| 212 | win->rect_info.w = 0; |
| 213 | win->rect_info.h = 0; |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | static int hwc_prepare(hwc_composer_device_t *dev, hwc_layer_list_t* list) |
| 218 | { |
| 219 | |
| 220 | struct hwc_context_t* ctx = (struct hwc_context_t*)dev; |
| 221 | int overlay_win_cnt = 0; |
| 222 | int compositionType = 0; |
| 223 | int ret; |
| 224 | |
| 225 | //if geometry is not changed, there is no need to do any work here |
| 226 | if( !list || (!(list->flags & HWC_GEOMETRY_CHANGED))) |
| 227 | return 0; |
| 228 | |
| 229 | //all the windows are free here.... |
| 230 | for (int i = 0; i < NUM_OF_WIN; i++) { |
| 231 | ctx->win[i].status = HWC_WIN_FREE; |
| 232 | ctx->win[i].buf_index = 0; |
| 233 | } |
| 234 | ctx->num_of_hwc_layer = 0; |
| 235 | ctx->num_of_fb_layer = 0; |
| 236 | ALOGV("%s:: hwc_prepare list->numHwLayers %d", __func__, list->numHwLayers); |
| 237 | |
| 238 | for (int i = 0; i < list->numHwLayers ; i++) { |
| 239 | hwc_layer_t* cur = &list->hwLayers[i]; |
| 240 | |
| 241 | if (overlay_win_cnt < NUM_OF_WIN) { |
| 242 | compositionType = get_hwc_compos_decision(cur); |
| 243 | |
| 244 | if (compositionType == HWC_FRAMEBUFFER) { |
| 245 | cur->compositionType = HWC_FRAMEBUFFER; |
| 246 | ctx->num_of_fb_layer++; |
| 247 | } else { |
| 248 | ret = assign_overlay_window(ctx, cur, overlay_win_cnt, i); |
| 249 | if (ret != 0) { |
| 250 | cur->compositionType = HWC_FRAMEBUFFER; |
| 251 | ctx->num_of_fb_layer++; |
| 252 | continue; |
| 253 | } |
| 254 | |
| 255 | cur->compositionType = HWC_OVERLAY; |
| 256 | cur->hints = HWC_HINT_CLEAR_FB; |
| 257 | overlay_win_cnt++; |
| 258 | ctx->num_of_hwc_layer++; |
| 259 | } |
| 260 | } else { |
| 261 | cur->compositionType = HWC_FRAMEBUFFER; |
| 262 | ctx->num_of_fb_layer++; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if(list->numHwLayers != (ctx->num_of_fb_layer + ctx->num_of_hwc_layer)) |
| 267 | ALOGV("%s:: numHwLayers %d num_of_fb_layer %d num_of_hwc_layer %d ", |
| 268 | __func__, list->numHwLayers, ctx->num_of_fb_layer, |
| 269 | ctx->num_of_hwc_layer); |
| 270 | |
| 271 | if (overlay_win_cnt < NUM_OF_WIN) { |
| 272 | //turn off the free windows |
| 273 | for (int i = overlay_win_cnt; i < NUM_OF_WIN; i++) { |
| 274 | window_hide(&ctx->win[i]); |
| 275 | reset_win_rect_info(&ctx->win[i]); |
| 276 | } |
| 277 | } |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | static int hwc_set(hwc_composer_device_t *dev, |
| 282 | hwc_display_t dpy, |
| 283 | hwc_surface_t sur, |
| 284 | hwc_layer_list_t* list) |
| 285 | { |
| 286 | struct hwc_context_t *ctx = (struct hwc_context_t *)dev; |
| 287 | unsigned int phyAddr[MAX_NUM_PLANES]; |
| 288 | int skipped_window_mask = 0; |
| 289 | hwc_layer_t* cur; |
| 290 | struct hwc_win_info_t *win; |
| 291 | int ret; |
| 292 | struct sec_img src_img; |
| 293 | struct sec_img dst_img; |
| 294 | struct sec_rect src_rect; |
| 295 | struct sec_rect dst_rect; |
| 296 | |
| 297 | if (dpy == NULL && sur == NULL && list == NULL) { |
| 298 | // release our resources, the screen is turning off |
| 299 | // in our case, there is nothing to do. |
| 300 | ctx->num_of_fb_layer_prev = 0; |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | bool need_swap_buffers = ctx->num_of_fb_layer > 0; |
| 305 | |
| 306 | /* |
| 307 | * H/W composer documentation states: |
| 308 | * There is an implicit layer containing opaque black |
| 309 | * pixels behind all the layers in the list. |
| 310 | * It is the responsibility of the hwcomposer module to make |
| 311 | * sure black pixels are output (or blended from). |
| 312 | * |
| 313 | * Since we're using a blitter, we need to erase the frame-buffer when |
| 314 | * switching to all-overlay mode. |
| 315 | * |
| 316 | */ |
| 317 | if (ctx->num_of_hwc_layer && |
| 318 | ctx->num_of_fb_layer==0 && ctx->num_of_fb_layer_prev) { |
| 319 | /* we're clearing the screen using GLES here, this is very |
| 320 | * hack-ish, ideal we would use the fimc (if it can do it) */ |
| 321 | glDisable(GL_SCISSOR_TEST); |
| 322 | glClearColor(0, 0, 0, 0); |
| 323 | glClear(GL_COLOR_BUFFER_BIT); |
| 324 | glEnable(GL_SCISSOR_TEST); |
| 325 | need_swap_buffers = true; |
| 326 | } |
| 327 | |
| 328 | ctx->num_of_fb_layer_prev = ctx->num_of_fb_layer; |
| 329 | |
| 330 | if (need_swap_buffers || !list) { |
| 331 | EGLBoolean sucess = eglSwapBuffers((EGLDisplay)dpy, (EGLSurface)sur); |
| 332 | if (!sucess) { |
| 333 | return HWC_EGL_ERROR; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | if (!list) { |
| 338 | /* turn off the all windows */ |
| 339 | for (int i = 0; i < NUM_OF_WIN; i++) { |
| 340 | window_hide(&ctx->win[i]); |
| 341 | reset_win_rect_info(&ctx->win[i]); |
| 342 | ctx->win[i].status = HWC_WIN_FREE; |
| 343 | } |
| 344 | ctx->num_of_hwc_layer = 0; |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | if(ctx->num_of_hwc_layer > NUM_OF_WIN) |
| 349 | ctx->num_of_hwc_layer = NUM_OF_WIN; |
| 350 | |
| 351 | /* compose hardware layers here */ |
| 352 | for (uint32_t i = 0; i < ctx->num_of_hwc_layer; i++) { |
| 353 | win = &ctx->win[i]; |
| 354 | if (win->status == HWC_WIN_RESERVED) { |
| 355 | cur = &list->hwLayers[win->layer_index]; |
| 356 | |
| 357 | if (cur->compositionType == HWC_OVERLAY) { |
| 358 | |
| 359 | ret = gpsGrallocModule->GetPhyAddrs(gpsGrallocModule, |
| 360 | cur->handle, phyAddr); |
| 361 | if (ret) { |
| 362 | ALOGE("%s::GetPhyAddrs fail : ret=%d\n", __func__, ret); |
| 363 | skipped_window_mask |= (1 << i); |
| 364 | continue; |
| 365 | } |
| 366 | |
| 367 | /* initialize the src & dist context for fimc */ |
| 368 | set_src_dst_info (cur, win, &src_img, &dst_img, &src_rect, |
| 369 | &dst_rect, i); |
| 370 | |
| 371 | ret = runFimc(ctx, &src_img, &src_rect, &dst_img, &dst_rect, |
| 372 | phyAddr, cur->transform); |
| 373 | if (ret < 0){ |
| 374 | ALOGE("%s::runFimc fail : ret=%d\n", __func__, ret); |
| 375 | skipped_window_mask |= (1 << i); |
| 376 | continue; |
| 377 | } |
| 378 | |
| 379 | if (win->set_win_flag == 1) { |
| 380 | /* turnoff the window and set the window position with new conf... */ |
| 381 | if (window_set_pos(win) < 0) { |
| 382 | ALOGE("%s::window_set_pos is failed : %s", __func__, |
| 383 | strerror(errno)); |
| 384 | skipped_window_mask |= (1 << i); |
| 385 | continue; |
| 386 | } |
| 387 | win->set_win_flag = 0; |
| 388 | } |
| 389 | |
| 390 | /* is the frame didn't change, it needs to be composited |
| 391 | * because something else below it could have changed, however |
| 392 | * it doesn't need to be swapped. |
| 393 | */ |
| 394 | if (win->layer_prev_buf != (uint32_t)cur->handle) { |
| 395 | win->layer_prev_buf = (uint32_t)cur->handle; |
| 396 | window_pan_display(win); |
| 397 | win->buf_index = (win->buf_index + 1) % NUM_OF_WIN_BUF; |
| 398 | } |
| 399 | |
| 400 | if(win->power_state == 0) |
| 401 | window_show(win); |
| 402 | |
| 403 | } else { |
| 404 | ALOGE("%s:: error : layer %d compositionType should have been \ |
| 405 | HWC_OVERLAY", __func__, win->layer_index); |
| 406 | skipped_window_mask |= (1 << i); |
| 407 | continue; |
| 408 | } |
| 409 | } else { |
| 410 | ALOGE("%s:: error : window status should have been HWC_WIN_RESERVED \ |
| 411 | by now... ", __func__); |
| 412 | skipped_window_mask |= (1 << i); |
| 413 | continue; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | if (skipped_window_mask) { |
| 418 | //turn off the free windows |
| 419 | for (int i = 0; i < NUM_OF_WIN; i++) { |
| 420 | if (skipped_window_mask & (1 << i)) |
| 421 | window_hide(&ctx->win[i]); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | static void hwc_registerProcs(struct hwc_composer_device* dev, |
| 429 | hwc_procs_t const* procs) |
| 430 | { |
| 431 | struct hwc_context_t* ctx = (struct hwc_context_t*)dev; |
| 432 | ctx->procs = const_cast<hwc_procs_t *>(procs); |
| 433 | } |
| 434 | |
| 435 | static int hwc_query(struct hwc_composer_device* dev, |
| 436 | int what, int* value) |
| 437 | { |
| 438 | struct hwc_context_t* ctx = (struct hwc_context_t*)dev; |
| 439 | |
| 440 | switch (what) { |
| 441 | case HWC_BACKGROUND_LAYER_SUPPORTED: |
| 442 | // we don't support the background layer yet |
| 443 | value[0] = 0; |
| 444 | break; |
| 445 | case HWC_VSYNC_PERIOD: |
| 446 | // vsync period in nanosecond |
| 447 | value[0] = 1000000000.0 / gpsGrallocModule->psFrameBufferDevice->base.fps; |
| 448 | break; |
| 449 | default: |
| 450 | // unsupported query |
| 451 | return -EINVAL; |
| 452 | } |
| 453 | return 0; |
| 454 | } |
| 455 | |
Michael Brehm | 3c938a7 | 2012-09-15 02:03:39 -0500 | [diff] [blame^] | 456 | #ifdef VSYNC_IOCTL |
| 457 | // Linux version of a manual reset event to control when |
| 458 | // and when not to ask the video card for a VSYNC. This |
| 459 | // stops the worker thread from asking for a VSYNC when |
| 460 | // there is nothing useful to do with it and more closely |
| 461 | // mimicks the original uevent mechanism |
| 462 | int vsync_enable = 0; |
| 463 | pthread_mutex_t vsync_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 464 | pthread_cond_t vsync_condition = PTHREAD_COND_INITIALIZER; |
| 465 | #endif |
| 466 | |
Chirayu Desai | 0a336cc | 2012-07-12 14:37:05 +0530 | [diff] [blame] | 467 | static int hwc_eventControl(struct hwc_composer_device* dev, |
| 468 | int event, int enabled) |
| 469 | { |
| 470 | struct hwc_context_t* ctx = (struct hwc_context_t*)dev; |
| 471 | |
| 472 | switch (event) { |
| 473 | case HWC_EVENT_VSYNC: |
| 474 | int val = !!enabled; |
| 475 | int err = ioctl(ctx->global_lcd_win.fd, S3CFB_SET_VSYNC_INT, &val); |
| 476 | if (err < 0) |
| 477 | return -errno; |
| 478 | |
Michael Brehm | 3c938a7 | 2012-09-15 02:03:39 -0500 | [diff] [blame^] | 479 | #if VSYNC_IOCTL |
| 480 | // Enable or disable the ability for the worker thread |
| 481 | // to ask for VSYNC events from the video driver |
| 482 | pthread_mutex_lock(&vsync_mutex); |
| 483 | if(enabled) { |
| 484 | vsync_enable = 1; |
| 485 | pthread_cond_broadcast(&vsync_condition); |
| 486 | } |
| 487 | else vsync_enable = 0; |
| 488 | pthread_mutex_unlock(&vsync_mutex); |
| 489 | #endif |
| 490 | |
Chirayu Desai | 0a336cc | 2012-07-12 14:37:05 +0530 | [diff] [blame] | 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | return -EINVAL; |
| 495 | } |
| 496 | |
| 497 | void handle_vsync_uevent(hwc_context_t *ctx, const char *buff, int len) |
| 498 | { |
| 499 | uint64_t timestamp = 0; |
| 500 | const char *s = buff; |
| 501 | |
| 502 | if(!ctx->procs || !ctx->procs->vsync) |
| 503 | return; |
| 504 | |
| 505 | s += strlen(s) + 1; |
| 506 | |
| 507 | while(*s) { |
| 508 | if (!strncmp(s, "VSYNC=", strlen("VSYNC="))) |
| 509 | timestamp = strtoull(s + strlen("VSYNC="), NULL, 0); |
| 510 | |
| 511 | s += strlen(s) + 1; |
| 512 | if (s - buff >= len) |
| 513 | break; |
| 514 | } |
| 515 | |
| 516 | ctx->procs->vsync(ctx->procs, 0, timestamp); |
| 517 | } |
| 518 | |
| 519 | static void *hwc_vsync_thread(void *data) |
| 520 | { |
| 521 | hwc_context_t *ctx = (hwc_context_t *)(data); |
Michael Brehm | 3c938a7 | 2012-09-15 02:03:39 -0500 | [diff] [blame^] | 522 | #ifdef VSYNC_IOCTL |
| 523 | uint64_t timestamp = 0; |
| 524 | #else |
Chirayu Desai | 0a336cc | 2012-07-12 14:37:05 +0530 | [diff] [blame] | 525 | char uevent_desc[4096]; |
| 526 | memset(uevent_desc, 0, sizeof(uevent_desc)); |
Michael Brehm | 3c938a7 | 2012-09-15 02:03:39 -0500 | [diff] [blame^] | 527 | #endif |
Chirayu Desai | 0a336cc | 2012-07-12 14:37:05 +0530 | [diff] [blame] | 528 | |
| 529 | setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY); |
| 530 | |
Michael Brehm | 3c938a7 | 2012-09-15 02:03:39 -0500 | [diff] [blame^] | 531 | #ifndef VSYNC_IOCTL |
Chirayu Desai | 0a336cc | 2012-07-12 14:37:05 +0530 | [diff] [blame] | 532 | uevent_init(); |
Michael Brehm | 3c938a7 | 2012-09-15 02:03:39 -0500 | [diff] [blame^] | 533 | #endif |
Chirayu Desai | 0a336cc | 2012-07-12 14:37:05 +0530 | [diff] [blame] | 534 | while(true) { |
Michael Brehm | 3c938a7 | 2012-09-15 02:03:39 -0500 | [diff] [blame^] | 535 | #ifdef VSYNC_IOCTL |
| 536 | // Only continue if hwc_eventControl is enabled, otherwise |
| 537 | // just sit here and wait until it is. This stops the code |
| 538 | // from constantly looking for the VSYNC event with the screen |
| 539 | // turned off. |
| 540 | pthread_mutex_lock(&vsync_mutex); |
| 541 | if(!vsync_enable) pthread_cond_wait(&vsync_condition, &vsync_mutex); |
| 542 | pthread_mutex_unlock(&vsync_mutex); |
| 543 | |
| 544 | timestamp = 0; // Reset the timestamp value |
| 545 | |
| 546 | // S3CFB_WAIT_FOR_VSYNC is a custom IOCTL I added to wait for |
| 547 | // the VSYNC interrupt, and then return the timestamp that was |
| 548 | // originally being communicated via a uevent. The uevent was |
| 549 | // spamming the UEventObserver and events/0 process with more |
| 550 | // information than this device could really deal with every 18ms |
| 551 | int res = ioctl(ctx->global_lcd_win.fd, S3CFB_WAIT_FOR_VSYNC, ×tamp); |
| 552 | if(res > 0) { |
| 553 | if(!ctx->procs || !ctx->procs->vsync) continue; |
| 554 | ctx->procs->vsync(ctx->procs, 0, timestamp); |
| 555 | } |
| 556 | #else |
Chirayu Desai | 0a336cc | 2012-07-12 14:37:05 +0530 | [diff] [blame] | 557 | int len = uevent_next_event(uevent_desc, sizeof(uevent_desc) - 2); |
| 558 | |
| 559 | bool vsync = !strcmp(uevent_desc, "change@/devices/platform/s3cfb"); |
| 560 | if(vsync) |
| 561 | handle_vsync_uevent(ctx, uevent_desc, len); |
Michael Brehm | 3c938a7 | 2012-09-15 02:03:39 -0500 | [diff] [blame^] | 562 | #endif |
Chirayu Desai | 0a336cc | 2012-07-12 14:37:05 +0530 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | return NULL; |
| 566 | } |
| 567 | |
| 568 | static int hwc_device_close(struct hw_device_t *dev) |
| 569 | { |
| 570 | struct hwc_context_t* ctx = (struct hwc_context_t*)dev; |
| 571 | int ret = 0; |
| 572 | int i; |
| 573 | |
| 574 | if (ctx) { |
| 575 | if (destroyFimc(&ctx->fimc) < 0) { |
| 576 | ALOGE("%s::destroyFimc fail", __func__); |
| 577 | ret = -1; |
| 578 | } |
| 579 | |
| 580 | if (window_close(&ctx->global_lcd_win) < 0) { |
| 581 | ALOGE("%s::window_close() fail", __func__); |
| 582 | ret = -1; |
| 583 | } |
| 584 | |
| 585 | for (i = 0; i < NUM_OF_WIN; i++) { |
| 586 | if (window_close(&ctx->win[i]) < 0) { |
| 587 | ALOGE("%s::window_close() fail", __func__); |
| 588 | ret = -1; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | // TODO: stop vsync_thread |
| 593 | |
| 594 | free(ctx); |
| 595 | } |
| 596 | return ret; |
| 597 | } |
| 598 | |
| 599 | static const struct hwc_methods hwc_methods = { |
| 600 | eventControl: hwc_eventControl |
| 601 | }; |
| 602 | |
| 603 | static int hwc_device_open(const struct hw_module_t* module, const char* name, |
| 604 | struct hw_device_t** device) |
| 605 | { |
| 606 | int status = 0; |
| 607 | int err; |
| 608 | struct hwc_win_info_t *win; |
| 609 | |
| 610 | if(hw_get_module(GRALLOC_HARDWARE_MODULE_ID, |
| 611 | (const hw_module_t**)&gpsGrallocModule)) |
| 612 | return -EINVAL; |
| 613 | |
| 614 | if(strcmp(gpsGrallocModule->base.common.author, "Imagination Technologies")) |
| 615 | return -EINVAL; |
| 616 | |
| 617 | if (strcmp(name, HWC_HARDWARE_COMPOSER)) |
| 618 | return -EINVAL; |
| 619 | |
| 620 | struct hwc_context_t *dev; |
| 621 | dev = (hwc_context_t*)malloc(sizeof(*dev)); |
| 622 | |
| 623 | /* initialize our state here */ |
| 624 | memset(dev, 0, sizeof(*dev)); |
| 625 | |
| 626 | /* initialize the procs */ |
| 627 | dev->device.common.tag = HARDWARE_DEVICE_TAG; |
| 628 | dev->device.common.version = HWC_DEVICE_API_VERSION_0_3; |
| 629 | dev->device.common.module = const_cast<hw_module_t*>(module); |
| 630 | dev->device.common.close = hwc_device_close; |
| 631 | |
| 632 | dev->device.prepare = hwc_prepare; |
| 633 | dev->device.set = hwc_set; |
| 634 | dev->device.registerProcs = hwc_registerProcs; |
| 635 | dev->device.query = hwc_query; |
| 636 | dev->device.methods = &hwc_methods; |
| 637 | |
| 638 | *device = &dev->device.common; |
| 639 | |
| 640 | /* initializing */ |
| 641 | memset(&(dev->fimc), 0, sizeof(s5p_fimc_t)); |
| 642 | dev->fimc.dev_fd = -1; |
| 643 | |
| 644 | /* open WIN0 & WIN1 here */ |
| 645 | for (int i = 0; i < NUM_OF_WIN; i++) { |
| 646 | if (window_open(&(dev->win[i]), i) < 0) { |
| 647 | ALOGE("%s:: Failed to open window %d device ", __func__, i); |
| 648 | status = -EINVAL; |
| 649 | goto err; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | /* open window 2, used to query global LCD info */ |
| 654 | if (window_open(&dev->global_lcd_win, 2) < 0) { |
| 655 | ALOGE("%s:: Failed to open window 2 device ", __func__); |
| 656 | status = -EINVAL; |
| 657 | goto err; |
| 658 | } |
| 659 | |
| 660 | /* get default window config */ |
| 661 | if (window_get_global_lcd_info(dev) < 0) { |
| 662 | ALOGE("%s::window_get_global_lcd_info is failed : %s", |
| 663 | __func__, strerror(errno)); |
| 664 | status = -EINVAL; |
| 665 | goto err; |
| 666 | } |
| 667 | |
| 668 | dev->lcd_info.yres_virtual = dev->lcd_info.yres * NUM_OF_WIN_BUF; |
| 669 | |
| 670 | /* initialize the window context */ |
| 671 | for (int i = 0; i < NUM_OF_WIN; i++) { |
| 672 | win = &dev->win[i]; |
| 673 | memcpy(&win->lcd_info, &dev->lcd_info, sizeof(struct fb_var_screeninfo)); |
| 674 | memcpy(&win->var_info, &dev->lcd_info, sizeof(struct fb_var_screeninfo)); |
| 675 | |
| 676 | win->rect_info.x = 0; |
| 677 | win->rect_info.y = 0; |
| 678 | win->rect_info.w = win->var_info.xres; |
| 679 | win->rect_info.h = win->var_info.yres; |
| 680 | |
| 681 | if (window_set_pos(win) < 0) { |
| 682 | ALOGE("%s::window_set_pos is failed : %s", |
| 683 | __func__, strerror(errno)); |
| 684 | status = -EINVAL; |
| 685 | goto err; |
| 686 | } |
| 687 | |
| 688 | if (window_get_info(win) < 0) { |
| 689 | ALOGE("%s::window_get_info is failed : %s", |
| 690 | __func__, strerror(errno)); |
| 691 | status = -EINVAL; |
| 692 | goto err; |
| 693 | } |
| 694 | |
| 695 | win->size = win->fix_info.line_length * win->var_info.yres; |
| 696 | |
| 697 | if (!win->fix_info.smem_start){ |
| 698 | ALOGE("%s:: win-%d failed to get the reserved memory", __func__, i); |
| 699 | status = -EINVAL; |
| 700 | goto err; |
| 701 | } |
| 702 | |
| 703 | for (int j = 0; j < NUM_OF_WIN_BUF; j++) { |
| 704 | win->addr[j] = win->fix_info.smem_start + (win->size * j); |
| 705 | ALOGI("%s::win-%d add[%d] %x ", __func__, i, j, win->addr[j]); |
| 706 | } |
| 707 | } |
| 708 | |
| 709 | /* open pp */ |
| 710 | if (createFimc(&dev->fimc) < 0) { |
| 711 | ALOGE("%s::creatFimc() fail", __func__); |
| 712 | status = -EINVAL; |
| 713 | goto err; |
| 714 | } |
| 715 | |
| 716 | err = pthread_create(&dev->vsync_thread, NULL, hwc_vsync_thread, dev); |
| 717 | if (err) { |
| 718 | ALOGE("%s::pthread_create() failed : %s", __func__, strerror(err)); |
| 719 | status = -err; |
| 720 | goto err; |
| 721 | } |
| 722 | |
| 723 | ALOGD("%s:: success\n", __func__); |
| 724 | |
| 725 | return 0; |
| 726 | |
| 727 | err: |
| 728 | if (destroyFimc(&dev->fimc) < 0) |
| 729 | ALOGE("%s::destroyFimc() fail", __func__); |
| 730 | |
| 731 | if (window_close(&dev->global_lcd_win) < 0) |
| 732 | ALOGE("%s::window_close() fail", __func__); |
| 733 | |
| 734 | for (int i = 0; i < NUM_OF_WIN; i++) { |
| 735 | if (window_close(&dev->win[i]) < 0) |
| 736 | ALOGE("%s::window_close() fail", __func__); |
| 737 | } |
| 738 | |
| 739 | return status; |
| 740 | } |