PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/libhwcomposer/SecHWC.cpp

https://bitbucket.org/bigxie/android_device_samsung_crespo
C++ | 567 lines | 411 code | 93 blank | 63 comment | 87 complexity | 240b73590db12d8c16467d7a44fbd255 MD5 | raw file
Possible License(s): LGPL-2.0
  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. * @author Rama, Meka(v.meka@samsung.com)
  19. Sangwoo, Park(sw5771.park@samsung.com)
  20. Jamie Oh (jung-min.oh@samsung.com)
  21. * @date 2011-07-28
  22. *
  23. */
  24. #include <cutils/log.h>
  25. #include <cutils/atomic.h>
  26. #include <EGL/egl.h>
  27. #include <GLES/gl.h>
  28. #include "SecHWCUtils.h"
  29. static IMG_gralloc_module_public_t *gpsGrallocModule;
  30. static int hwc_device_open(const struct hw_module_t* module, const char* name,
  31. struct hw_device_t** device);
  32. static struct hw_module_methods_t hwc_module_methods = {
  33. open: hwc_device_open
  34. };
  35. hwc_module_t HAL_MODULE_INFO_SYM = {
  36. common: {
  37. tag: HARDWARE_MODULE_TAG,
  38. version_major: 1,
  39. version_minor: 0,
  40. id: HWC_HARDWARE_MODULE_ID,
  41. name: "Samsung S5PC11X hwcomposer module",
  42. author: "SAMSUNG",
  43. methods: &hwc_module_methods,
  44. }
  45. };
  46. static void dump_layer(hwc_layer_t const* l) {
  47. LOGD("\ttype=%d, flags=%08x, handle=%p, tr=%02x, blend=%04x, {%d,%d,%d,%d}, {%d,%d,%d,%d}",
  48. l->compositionType, l->flags, l->handle, l->transform, l->blending,
  49. l->sourceCrop.left,
  50. l->sourceCrop.top,
  51. l->sourceCrop.right,
  52. l->sourceCrop.bottom,
  53. l->displayFrame.left,
  54. l->displayFrame.top,
  55. l->displayFrame.right,
  56. l->displayFrame.bottom);
  57. }
  58. static int set_src_dst_info(hwc_layer_t *cur,
  59. struct hwc_win_info_t *win,
  60. struct sec_img *src_img,
  61. struct sec_img *dst_img,
  62. struct sec_rect *src_rect,
  63. struct sec_rect *dst_rect,
  64. int win_idx)
  65. {
  66. IMG_native_handle_t *prev_handle = (IMG_native_handle_t *)(cur->handle);
  67. // set src image
  68. src_img->w = prev_handle->iWidth;
  69. src_img->h = prev_handle->iHeight;
  70. src_img->format = prev_handle->iFormat;
  71. src_img->base = NULL;
  72. src_img->offset = 0;
  73. src_img->mem_id =0;
  74. src_img->mem_type = HWC_PHYS_MEM_TYPE;
  75. src_img->w = (src_img->w + 15) & (~15);
  76. src_img->h = (src_img->h + 1) & (~1) ;
  77. //set src rect
  78. src_rect->x = SEC_MAX(cur->sourceCrop.left, 0);
  79. src_rect->y = SEC_MAX(cur->sourceCrop.top, 0);
  80. src_rect->w = SEC_MAX(cur->sourceCrop.right - cur->sourceCrop.left, 0);
  81. src_rect->w = SEC_MIN(src_rect->w, src_img->w - src_rect->x);
  82. src_rect->h = SEC_MAX(cur->sourceCrop.bottom - cur->sourceCrop.top, 0);
  83. src_rect->h = SEC_MIN(src_rect->h, src_img->h - src_rect->y);
  84. //set dst image
  85. dst_img->w = win->lcd_info.xres;
  86. dst_img->h = win->lcd_info.yres;
  87. switch (win->lcd_info.bits_per_pixel) {
  88. case 32:
  89. dst_img->format = HAL_PIXEL_FORMAT_RGBX_8888;
  90. break;
  91. default:
  92. dst_img->format = HAL_PIXEL_FORMAT_RGB_565;
  93. break;
  94. }
  95. dst_img->base = win->addr[win->buf_index];
  96. dst_img->offset = 0;
  97. dst_img->mem_id = 0;
  98. dst_img->mem_type = HWC_PHYS_MEM_TYPE;
  99. //set dst rect
  100. //fimc dst image will be stored from left top corner
  101. dst_rect->x = 0;
  102. dst_rect->y = 0;
  103. dst_rect->w = win->rect_info.w;
  104. dst_rect->h = win->rect_info.h;
  105. LOGV("%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 ",
  106. __func__, src_rect->x, src_rect->y, src_rect->w, src_rect->h,
  107. dst_rect->x, dst_rect->y, dst_rect->w, dst_rect->h);
  108. return 0;
  109. }
  110. static int get_hwc_compos_decision(hwc_layer_t* cur)
  111. {
  112. if(cur->flags & HWC_SKIP_LAYER || !cur->handle) {
  113. LOGV("%s::is_skip_layer %d cur->handle %x",
  114. __func__, cur->flags & HWC_SKIP_LAYER, (uint32_t)cur->handle);
  115. return HWC_FRAMEBUFFER;
  116. }
  117. IMG_native_handle_t *prev_handle = (IMG_native_handle_t *)(cur->handle);
  118. int compositionType = HWC_FRAMEBUFFER;
  119. /* check here....if we have any resolution constraints */
  120. if (((cur->sourceCrop.right - cur->sourceCrop.left) < 16) ||
  121. ((cur->sourceCrop.bottom - cur->sourceCrop.top) < 8))
  122. return compositionType;
  123. if ((cur->transform == HAL_TRANSFORM_ROT_90) ||
  124. (cur->transform == HAL_TRANSFORM_ROT_270)) {
  125. if(((cur->displayFrame.right - cur->displayFrame.left) < 4)||
  126. ((cur->displayFrame.bottom - cur->displayFrame.top) < 8))
  127. return compositionType;
  128. } else if (((cur->displayFrame.right - cur->displayFrame.left) < 8) ||
  129. ((cur->displayFrame.bottom - cur->displayFrame.top) < 4))
  130. return compositionType;
  131. if((prev_handle->usage & GRALLOC_USAGE_PHYS_CONTIG) &&
  132. (cur->blending == HWC_BLENDING_NONE))
  133. compositionType = HWC_OVERLAY;
  134. else
  135. compositionType = HWC_FRAMEBUFFER;
  136. LOGV("%s::compositionType %d bpp %d format %x usage %x",
  137. __func__,compositionType, prev_handle->uiBpp, prev_handle->iFormat,
  138. prev_handle->usage & GRALLOC_USAGE_PHYS_CONTIG);
  139. return compositionType;
  140. }
  141. static int assign_overlay_window(struct hwc_context_t *ctx,
  142. hwc_layer_t *cur,
  143. int win_idx,
  144. int layer_idx)
  145. {
  146. struct hwc_win_info_t *win;
  147. sec_rect rect;
  148. int ret = 0;
  149. if(NUM_OF_WIN <= win_idx)
  150. return -1;
  151. win = &ctx->win[win_idx];
  152. rect.x = SEC_MAX(cur->displayFrame.left, 0);
  153. rect.y = SEC_MAX(cur->displayFrame.top, 0);
  154. rect.w = SEC_MIN(cur->displayFrame.right - rect.x, win->lcd_info.xres - rect.x);
  155. rect.h = SEC_MIN(cur->displayFrame.bottom - rect.y, win->lcd_info.yres - rect.y);
  156. win->set_win_flag = 0;
  157. if((rect.x != win->rect_info.x) || (rect.y != win->rect_info.y) ||
  158. (rect.w != win->rect_info.w) || (rect.h != win->rect_info.h)){
  159. win->rect_info.x = rect.x;
  160. win->rect_info.y = rect.y;
  161. win->rect_info.w = rect.w;
  162. win->rect_info.h = rect.h;
  163. win->set_win_flag = 1;
  164. win->layer_prev_buf = 0;
  165. }
  166. win->layer_index = layer_idx;
  167. win->status = HWC_WIN_RESERVED;
  168. LOGV("%s:: win_x %d win_y %d win_w %d win_h %d lay_idx %d win_idx %d",
  169. __func__, win->rect_info.x, win->rect_info.y, win->rect_info.w,
  170. win->rect_info.h, win->layer_index, win_idx );
  171. return 0;
  172. }
  173. static void reset_win_rect_info(hwc_win_info_t *win)
  174. {
  175. win->rect_info.x = 0;
  176. win->rect_info.y = 0;
  177. win->rect_info.w = 0;
  178. win->rect_info.h = 0;
  179. return;
  180. }
  181. static int hwc_prepare(hwc_composer_device_t *dev, hwc_layer_list_t* list)
  182. {
  183. struct hwc_context_t* ctx = (struct hwc_context_t*)dev;
  184. int overlay_win_cnt = 0;
  185. int compositionType = 0;
  186. int ret;
  187. //if geometry is not changed, there is no need to do any work here
  188. if( !list || (!(list->flags & HWC_GEOMETRY_CHANGED)))
  189. return 0;
  190. //all the windows are free here....
  191. for (int i = 0; i < NUM_OF_WIN; i++) {
  192. ctx->win[i].status = HWC_WIN_FREE;
  193. ctx->win[i].buf_index = 0;
  194. }
  195. ctx->num_of_hwc_layer = 0;
  196. ctx->num_of_fb_layer = 0;
  197. LOGV("%s:: hwc_prepare list->numHwLayers %d", __func__, list->numHwLayers);
  198. for (int i = 0; i < list->numHwLayers ; i++) {
  199. hwc_layer_t* cur = &list->hwLayers[i];
  200. if (overlay_win_cnt < NUM_OF_WIN) {
  201. compositionType = get_hwc_compos_decision(cur);
  202. if (compositionType == HWC_FRAMEBUFFER) {
  203. cur->compositionType = HWC_FRAMEBUFFER;
  204. ctx->num_of_fb_layer++;
  205. } else {
  206. ret = assign_overlay_window(ctx, cur, overlay_win_cnt, i);
  207. if (ret != 0) {
  208. cur->compositionType = HWC_FRAMEBUFFER;
  209. ctx->num_of_fb_layer++;
  210. continue;
  211. }
  212. cur->compositionType = HWC_OVERLAY;
  213. cur->hints = HWC_HINT_CLEAR_FB;
  214. overlay_win_cnt++;
  215. ctx->num_of_hwc_layer++;
  216. }
  217. } else {
  218. cur->compositionType = HWC_FRAMEBUFFER;
  219. ctx->num_of_fb_layer++;
  220. }
  221. }
  222. if(list->numHwLayers != (ctx->num_of_fb_layer + ctx->num_of_hwc_layer))
  223. LOGV("%s:: numHwLayers %d num_of_fb_layer %d num_of_hwc_layer %d ",
  224. __func__, list->numHwLayers, ctx->num_of_fb_layer,
  225. ctx->num_of_hwc_layer);
  226. if (overlay_win_cnt < NUM_OF_WIN) {
  227. //turn off the free windows
  228. for (int i = overlay_win_cnt; i < NUM_OF_WIN; i++) {
  229. window_hide(&ctx->win[i]);
  230. reset_win_rect_info(&ctx->win[i]);
  231. }
  232. }
  233. return 0;
  234. }
  235. static int hwc_set(hwc_composer_device_t *dev,
  236. hwc_display_t dpy,
  237. hwc_surface_t sur,
  238. hwc_layer_list_t* list)
  239. {
  240. struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
  241. unsigned int phyAddr[MAX_NUM_PLANES];
  242. int skipped_window_mask = 0;
  243. hwc_layer_t* cur;
  244. struct hwc_win_info_t *win;
  245. int ret;
  246. struct sec_img src_img;
  247. struct sec_img dst_img;
  248. struct sec_rect src_rect;
  249. struct sec_rect dst_rect;
  250. if (dpy == NULL && sur == NULL && list == NULL) {
  251. // release our resources, the screen is turning off
  252. // in our case, there is nothing to do.
  253. ctx->num_of_fb_layer_prev = 0;
  254. return 0;
  255. }
  256. bool need_swap_buffers = ctx->num_of_fb_layer > 0;
  257. /*
  258. * H/W composer documentation states:
  259. * There is an implicit layer containing opaque black
  260. * pixels behind all the layers in the list.
  261. * It is the responsibility of the hwcomposer module to make
  262. * sure black pixels are output (or blended from).
  263. *
  264. * Since we're using a blitter, we need to erase the frame-buffer when
  265. * switching to all-overlay mode.
  266. *
  267. */
  268. if (ctx->num_of_hwc_layer &&
  269. ctx->num_of_fb_layer==0 && ctx->num_of_fb_layer_prev) {
  270. /* we're clearing the screen using GLES here, this is very
  271. * hack-ish, ideal we would use the fimc (if it can do it) */
  272. glDisable(GL_SCISSOR_TEST);
  273. glClearColor(0, 0, 0, 0);
  274. glClear(GL_COLOR_BUFFER_BIT);
  275. glEnable(GL_SCISSOR_TEST);
  276. need_swap_buffers = true;
  277. }
  278. ctx->num_of_fb_layer_prev = ctx->num_of_fb_layer;
  279. if (need_swap_buffers || !list) {
  280. EGLBoolean sucess = eglSwapBuffers((EGLDisplay)dpy, (EGLSurface)sur);
  281. if (!sucess) {
  282. return HWC_EGL_ERROR;
  283. }
  284. }
  285. if (!list) {
  286. /* turn off the all windows */
  287. for (int i = 0; i < NUM_OF_WIN; i++) {
  288. window_hide(&ctx->win[i]);
  289. reset_win_rect_info(&ctx->win[i]);
  290. ctx->win[i].status = HWC_WIN_FREE;
  291. }
  292. ctx->num_of_hwc_layer = 0;
  293. return 0;
  294. }
  295. if(ctx->num_of_hwc_layer > NUM_OF_WIN)
  296. ctx->num_of_hwc_layer = NUM_OF_WIN;
  297. /* compose hardware layers here */
  298. for (uint32_t i = 0; i < ctx->num_of_hwc_layer; i++) {
  299. win = &ctx->win[i];
  300. if (win->status == HWC_WIN_RESERVED) {
  301. cur = &list->hwLayers[win->layer_index];
  302. if (cur->compositionType == HWC_OVERLAY) {
  303. ret = gpsGrallocModule->GetPhyAddrs(gpsGrallocModule,
  304. cur->handle, phyAddr);
  305. if (ret) {
  306. LOGE("%s::GetPhyAddrs fail : ret=%d\n", __func__, ret);
  307. skipped_window_mask |= (1 << i);
  308. continue;
  309. }
  310. /* initialize the src & dist context for fimc */
  311. set_src_dst_info (cur, win, &src_img, &dst_img, &src_rect,
  312. &dst_rect, i);
  313. ret = runFimc(ctx, &src_img, &src_rect, &dst_img, &dst_rect,
  314. phyAddr, cur->transform);
  315. if (ret < 0){
  316. LOGE("%s::runFimc fail : ret=%d\n", __func__, ret);
  317. skipped_window_mask |= (1 << i);
  318. continue;
  319. }
  320. if (win->set_win_flag == 1) {
  321. /* turnoff the window and set the window position with new conf... */
  322. if (window_set_pos(win) < 0) {
  323. LOGE("%s::window_set_pos is failed : %s", __func__,
  324. strerror(errno));
  325. skipped_window_mask |= (1 << i);
  326. continue;
  327. }
  328. win->set_win_flag = 0;
  329. }
  330. /* is the frame didn't change, it needs to be composited
  331. * because something else below it could have changed, however
  332. * it doesn't need to be swapped.
  333. */
  334. if (win->layer_prev_buf != (uint32_t)cur->handle) {
  335. win->layer_prev_buf = (uint32_t)cur->handle;
  336. window_pan_display(win);
  337. win->buf_index = (win->buf_index + 1) % NUM_OF_WIN_BUF;
  338. }
  339. if(win->power_state == 0)
  340. window_show(win);
  341. } else {
  342. LOGE("%s:: error : layer %d compositionType should have been \
  343. HWC_OVERLAY", __func__, win->layer_index);
  344. skipped_window_mask |= (1 << i);
  345. continue;
  346. }
  347. } else {
  348. LOGE("%s:: error : window status should have been HWC_WIN_RESERVED \
  349. by now... ", __func__);
  350. skipped_window_mask |= (1 << i);
  351. continue;
  352. }
  353. }
  354. if (skipped_window_mask) {
  355. //turn off the free windows
  356. for (int i = 0; i < NUM_OF_WIN; i++) {
  357. if (skipped_window_mask & (1 << i))
  358. window_hide(&ctx->win[i]);
  359. }
  360. }
  361. return 0;
  362. }
  363. static int hwc_device_close(struct hw_device_t *dev)
  364. {
  365. struct hwc_context_t* ctx = (struct hwc_context_t*)dev;
  366. int ret = 0;
  367. int i;
  368. if (ctx) {
  369. if (destroyFimc(&ctx->fimc) < 0) {
  370. LOGE("%s::destroyFimc fail", __func__);
  371. ret = -1;
  372. }
  373. for (i = 0; i < NUM_OF_WIN; i++) {
  374. if (window_close(&ctx->win[i]) < 0) {
  375. LOGE("%s::window_close() fail", __func__);
  376. ret = -1;
  377. }
  378. }
  379. free(ctx);
  380. }
  381. return ret;
  382. }
  383. static int hwc_device_open(const struct hw_module_t* module, const char* name,
  384. struct hw_device_t** device)
  385. {
  386. int status = 0;
  387. struct hwc_win_info_t *win;
  388. if(hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
  389. (const hw_module_t**)&gpsGrallocModule))
  390. return -EINVAL;
  391. if(strcmp(gpsGrallocModule->base.common.author, "Imagination Technologies"))
  392. return -EINVAL;
  393. if (strcmp(name, HWC_HARDWARE_COMPOSER))
  394. return -EINVAL;
  395. struct hwc_context_t *dev;
  396. dev = (hwc_context_t*)malloc(sizeof(*dev));
  397. /* initialize our state here */
  398. memset(dev, 0, sizeof(*dev));
  399. /* initialize the procs */
  400. dev->device.common.tag = HARDWARE_DEVICE_TAG;
  401. dev->device.common.version = 0;
  402. dev->device.common.module = const_cast<hw_module_t*>(module);
  403. dev->device.common.close = hwc_device_close;
  404. dev->device.prepare = hwc_prepare;
  405. dev->device.set = hwc_set;
  406. *device = &dev->device.common;
  407. /* initializing */
  408. memset(&(dev->fimc), 0, sizeof(s5p_fimc_t));
  409. dev->fimc.dev_fd = -1;
  410. /* open WIN0 & WIN1 here */
  411. for (int i = 0; i < NUM_OF_WIN; i++) {
  412. if (window_open(&(dev->win[i]), i) < 0) {
  413. LOGE("%s:: Failed to open window %d device ", __func__, i);
  414. status = -EINVAL;
  415. goto err;
  416. }
  417. }
  418. /* get default window config */
  419. if (window_get_global_lcd_info(&dev->lcd_info) < 0) {
  420. LOGE("%s::window_get_global_lcd_info is failed : %s",
  421. __func__, strerror(errno));
  422. status = -EINVAL;
  423. goto err;
  424. }
  425. dev->lcd_info.yres_virtual = dev->lcd_info.yres * NUM_OF_WIN_BUF;
  426. /* initialize the window context */
  427. for (int i = 0; i < NUM_OF_WIN; i++) {
  428. win = &dev->win[i];
  429. memcpy(&win->lcd_info, &dev->lcd_info, sizeof(struct fb_var_screeninfo));
  430. memcpy(&win->var_info, &dev->lcd_info, sizeof(struct fb_var_screeninfo));
  431. win->rect_info.x = 0;
  432. win->rect_info.y = 0;
  433. win->rect_info.w = win->var_info.xres;
  434. win->rect_info.h = win->var_info.yres;
  435. if (window_set_pos(win) < 0) {
  436. LOGE("%s::window_set_pos is failed : %s",
  437. __func__, strerror(errno));
  438. status = -EINVAL;
  439. goto err;
  440. }
  441. if (window_get_info(win) < 0) {
  442. LOGE("%s::window_get_info is failed : %s",
  443. __func__, strerror(errno));
  444. status = -EINVAL;
  445. goto err;
  446. }
  447. win->size = win->fix_info.line_length * win->var_info.yres;
  448. if (!win->fix_info.smem_start){
  449. LOGE("%s:: win-%d failed to get the reserved memory", __func__, i);
  450. status = -EINVAL;
  451. goto err;
  452. }
  453. for (int j = 0; j < NUM_OF_WIN_BUF; j++) {
  454. win->addr[j] = win->fix_info.smem_start + (win->size * j);
  455. LOGI("%s::win-%d add[%d] %x ", __func__, i, j, win->addr[j]);
  456. }
  457. }
  458. /* open pp */
  459. if (createFimc(&dev->fimc) < 0) {
  460. LOGE("%s::creatFimc() fail", __func__);
  461. status = -EINVAL;
  462. goto err;
  463. }
  464. LOGD("%s:: success\n", __func__);
  465. return 0;
  466. err:
  467. if (destroyFimc(&dev->fimc) < 0)
  468. LOGE("%s::destroyFimc() fail", __func__);
  469. for (int i = 0; i < NUM_OF_WIN; i++) {
  470. if (window_close(&dev->win[i]) < 0)
  471. LOGE("%s::window_close() fail", __func__);
  472. }
  473. return status;
  474. }