blob: f8a3d1a648538e9b55c5d47cbb9c3ca8b2c81786 [file] [log] [blame]
Chirayu Desai0a336cc2012-07-12 14:37:05 +05301/*
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
34static IMG_gralloc_module_public_t *gpsGrallocModule;
35
36static int hwc_device_open(const struct hw_module_t* module, const char* name,
37 struct hw_device_t** device);
38
39static struct hw_module_methods_t hwc_module_methods = {
40 open: hwc_device_open
41};
42
43hwc_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
55static 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
68static 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;
Petr Havlena1386f442012-10-26 17:52:00 +053082 src_img->base = 0;
Chirayu Desai0a336cc2012-07-12 14:37:05 +053083 src_img->offset = 0;
Petr Havlena1386f442012-10-26 17:52:00 +053084 src_img->mem_id = 0;
Chirayu Desai0a336cc2012-07-12 14:37:05 +053085
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
130static 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
168static 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
208static 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
217static 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
281static 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
Petr Havlena1386f442012-10-26 17:52:00 +0530425#if defined(BOARD_USES_HDMI)
426 hdmi_device_t* hdmi = ctx->hdmi;
427 if (ctx->num_of_hwc_layer == 1 && hdmi) {
428 if ((src_img.format == HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP_TILED)||
429 (src_img.format == HAL_PIXEL_FORMAT_CUSTOM_YCrCb_420_SP)) {
430 ADDRS * addr = (ADDRS *)(src_img.base);
431 hdmi->blit(hdmi,
432 src_img.w,
433 src_img.h,
434 src_img.format,
435 (unsigned int)addr->addr_y,
436 (unsigned int)addr->addr_cbcr,
437 (unsigned int)addr->addr_cbcr,
438 0, 0,
439 HDMI_MODE_VIDEO,
440 ctx->num_of_hwc_layer);
441 } else if ((src_img.format == HAL_PIXEL_FORMAT_YCbCr_420_SP) ||
442 (src_img.format == HAL_PIXEL_FORMAT_YCrCb_420_SP) ||
443 (src_img.format == HAL_PIXEL_FORMAT_YCbCr_420_P) ||
444 (src_img.format == HAL_PIXEL_FORMAT_YV12)) {
445 hdmi->blit(hdmi,
446 src_img.w,
447 src_img.h,
448 src_img.format,
449 (unsigned int)ctx->fimc.params.src.buf_addr_phy_rgb_y,
450 (unsigned int)ctx->fimc.params.src.buf_addr_phy_cb,
451 (unsigned int)ctx->fimc.params.src.buf_addr_phy_cr,
452 0, 0,
453 HDMI_MODE_VIDEO,
454 ctx->num_of_hwc_layer);
455 } else {
456 ALOGE("%s: Unsupported format = %d for hdmi", __func__, src_img.format);
457 }
458 }
459#endif
460
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530461 return 0;
462}
463
464static void hwc_registerProcs(struct hwc_composer_device* dev,
465 hwc_procs_t const* procs)
466{
467 struct hwc_context_t* ctx = (struct hwc_context_t*)dev;
468 ctx->procs = const_cast<hwc_procs_t *>(procs);
469}
470
471static int hwc_query(struct hwc_composer_device* dev,
472 int what, int* value)
473{
474 struct hwc_context_t* ctx = (struct hwc_context_t*)dev;
475
476 switch (what) {
477 case HWC_BACKGROUND_LAYER_SUPPORTED:
478 // we don't support the background layer yet
479 value[0] = 0;
480 break;
481 case HWC_VSYNC_PERIOD:
482 // vsync period in nanosecond
483 value[0] = 1000000000.0 / gpsGrallocModule->psFrameBufferDevice->base.fps;
484 break;
485 default:
486 // unsupported query
487 return -EINVAL;
488 }
489 return 0;
490}
491
Michael Brehm3c938a72012-09-15 02:03:39 -0500492#ifdef VSYNC_IOCTL
493// Linux version of a manual reset event to control when
494// and when not to ask the video card for a VSYNC. This
495// stops the worker thread from asking for a VSYNC when
496// there is nothing useful to do with it and more closely
497// mimicks the original uevent mechanism
498int vsync_enable = 0;
499pthread_mutex_t vsync_mutex = PTHREAD_MUTEX_INITIALIZER;
500pthread_cond_t vsync_condition = PTHREAD_COND_INITIALIZER;
501#endif
502
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530503static int hwc_eventControl(struct hwc_composer_device* dev,
504 int event, int enabled)
505{
506 struct hwc_context_t* ctx = (struct hwc_context_t*)dev;
507
508 switch (event) {
509 case HWC_EVENT_VSYNC:
510 int val = !!enabled;
511 int err = ioctl(ctx->global_lcd_win.fd, S3CFB_SET_VSYNC_INT, &val);
512 if (err < 0)
513 return -errno;
514
Michael Brehm3c938a72012-09-15 02:03:39 -0500515#if VSYNC_IOCTL
516 // Enable or disable the ability for the worker thread
517 // to ask for VSYNC events from the video driver
518 pthread_mutex_lock(&vsync_mutex);
519 if(enabled) {
520 vsync_enable = 1;
521 pthread_cond_broadcast(&vsync_condition);
522 }
523 else vsync_enable = 0;
524 pthread_mutex_unlock(&vsync_mutex);
525#endif
526
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530527 return 0;
528 }
529
530 return -EINVAL;
531}
532
533void handle_vsync_uevent(hwc_context_t *ctx, const char *buff, int len)
534{
535 uint64_t timestamp = 0;
536 const char *s = buff;
537
538 if(!ctx->procs || !ctx->procs->vsync)
539 return;
540
541 s += strlen(s) + 1;
542
543 while(*s) {
544 if (!strncmp(s, "VSYNC=", strlen("VSYNC=")))
545 timestamp = strtoull(s + strlen("VSYNC="), NULL, 0);
546
547 s += strlen(s) + 1;
548 if (s - buff >= len)
549 break;
550 }
551
552 ctx->procs->vsync(ctx->procs, 0, timestamp);
553}
554
555static void *hwc_vsync_thread(void *data)
556{
557 hwc_context_t *ctx = (hwc_context_t *)(data);
Michael Brehm3c938a72012-09-15 02:03:39 -0500558#ifdef VSYNC_IOCTL
559 uint64_t timestamp = 0;
560#else
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530561 char uevent_desc[4096];
562 memset(uevent_desc, 0, sizeof(uevent_desc));
Michael Brehm3c938a72012-09-15 02:03:39 -0500563#endif
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530564
565 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
566
Michael Brehm3c938a72012-09-15 02:03:39 -0500567#ifndef VSYNC_IOCTL
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530568 uevent_init();
Michael Brehm3c938a72012-09-15 02:03:39 -0500569#endif
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530570 while(true) {
Michael Brehm3c938a72012-09-15 02:03:39 -0500571#ifdef VSYNC_IOCTL
572 // Only continue if hwc_eventControl is enabled, otherwise
573 // just sit here and wait until it is. This stops the code
574 // from constantly looking for the VSYNC event with the screen
575 // turned off.
576 pthread_mutex_lock(&vsync_mutex);
577 if(!vsync_enable) pthread_cond_wait(&vsync_condition, &vsync_mutex);
578 pthread_mutex_unlock(&vsync_mutex);
579
580 timestamp = 0; // Reset the timestamp value
581
582 // S3CFB_WAIT_FOR_VSYNC is a custom IOCTL I added to wait for
583 // the VSYNC interrupt, and then return the timestamp that was
584 // originally being communicated via a uevent. The uevent was
585 // spamming the UEventObserver and events/0 process with more
586 // information than this device could really deal with every 18ms
587 int res = ioctl(ctx->global_lcd_win.fd, S3CFB_WAIT_FOR_VSYNC, &timestamp);
588 if(res > 0) {
589 if(!ctx->procs || !ctx->procs->vsync) continue;
590 ctx->procs->vsync(ctx->procs, 0, timestamp);
591 }
592#else
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530593 int len = uevent_next_event(uevent_desc, sizeof(uevent_desc) - 2);
594
595 bool vsync = !strcmp(uevent_desc, "change@/devices/platform/s3cfb");
596 if(vsync)
597 handle_vsync_uevent(ctx, uevent_desc, len);
Michael Brehm3c938a72012-09-15 02:03:39 -0500598#endif
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530599 }
600
601 return NULL;
602}
603
604static int hwc_device_close(struct hw_device_t *dev)
605{
606 struct hwc_context_t* ctx = (struct hwc_context_t*)dev;
607 int ret = 0;
608 int i;
609
610 if (ctx) {
611 if (destroyFimc(&ctx->fimc) < 0) {
612 ALOGE("%s::destroyFimc fail", __func__);
613 ret = -1;
614 }
615
616 if (window_close(&ctx->global_lcd_win) < 0) {
617 ALOGE("%s::window_close() fail", __func__);
618 ret = -1;
619 }
620
621 for (i = 0; i < NUM_OF_WIN; i++) {
622 if (window_close(&ctx->win[i]) < 0) {
623 ALOGE("%s::window_close() fail", __func__);
624 ret = -1;
625 }
626 }
627
628 // TODO: stop vsync_thread
629
630 free(ctx);
631 }
632 return ret;
633}
634
635static const struct hwc_methods hwc_methods = {
636 eventControl: hwc_eventControl
637};
638
639static int hwc_device_open(const struct hw_module_t* module, const char* name,
640 struct hw_device_t** device)
641{
642 int status = 0;
643 int err;
644 struct hwc_win_info_t *win;
Petr Havlena1386f442012-10-26 17:52:00 +0530645#if defined(BOARD_USES_HDMI)
646 struct hw_module_t *hdmi_module;
647#endif
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530648
649 if(hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
650 (const hw_module_t**)&gpsGrallocModule))
651 return -EINVAL;
652
653 if(strcmp(gpsGrallocModule->base.common.author, "Imagination Technologies"))
654 return -EINVAL;
655
656 if (strcmp(name, HWC_HARDWARE_COMPOSER))
657 return -EINVAL;
658
659 struct hwc_context_t *dev;
660 dev = (hwc_context_t*)malloc(sizeof(*dev));
661
662 /* initialize our state here */
663 memset(dev, 0, sizeof(*dev));
664
665 /* initialize the procs */
666 dev->device.common.tag = HARDWARE_DEVICE_TAG;
667 dev->device.common.version = HWC_DEVICE_API_VERSION_0_3;
668 dev->device.common.module = const_cast<hw_module_t*>(module);
669 dev->device.common.close = hwc_device_close;
670
671 dev->device.prepare = hwc_prepare;
672 dev->device.set = hwc_set;
673 dev->device.registerProcs = hwc_registerProcs;
674 dev->device.query = hwc_query;
675 dev->device.methods = &hwc_methods;
676
677 *device = &dev->device.common;
678
Petr Havlena1386f442012-10-26 17:52:00 +0530679#if defined(BOARD_USES_HDMI)
680 dev->hdmi = NULL;
681 if(hw_get_module(HDMI_HARDWARE_MODULE_ID,
682 (const hw_module_t**)&hdmi_module)) {
683 ALOGE("%s:: HDMI device not present", __func__);
684 } else {
685 int ret = module->methods->open(hdmi_module, "hdmi-composer",
686 (hw_device_t **)&dev->hdmi);
687 if(ret < 0) {
688 ALOGE("%s:: Failed to open hdmi device : %s", __func__, strerror(ret));
689 }
690 }
691#endif
692
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530693 /* initializing */
694 memset(&(dev->fimc), 0, sizeof(s5p_fimc_t));
695 dev->fimc.dev_fd = -1;
696
697 /* open WIN0 & WIN1 here */
698 for (int i = 0; i < NUM_OF_WIN; i++) {
699 if (window_open(&(dev->win[i]), i) < 0) {
700 ALOGE("%s:: Failed to open window %d device ", __func__, i);
701 status = -EINVAL;
702 goto err;
703 }
704 }
705
706 /* open window 2, used to query global LCD info */
707 if (window_open(&dev->global_lcd_win, 2) < 0) {
708 ALOGE("%s:: Failed to open window 2 device ", __func__);
709 status = -EINVAL;
710 goto err;
711 }
712
713 /* get default window config */
714 if (window_get_global_lcd_info(dev) < 0) {
715 ALOGE("%s::window_get_global_lcd_info is failed : %s",
716 __func__, strerror(errno));
717 status = -EINVAL;
718 goto err;
719 }
720
721 dev->lcd_info.yres_virtual = dev->lcd_info.yres * NUM_OF_WIN_BUF;
722
723 /* initialize the window context */
724 for (int i = 0; i < NUM_OF_WIN; i++) {
725 win = &dev->win[i];
726 memcpy(&win->lcd_info, &dev->lcd_info, sizeof(struct fb_var_screeninfo));
727 memcpy(&win->var_info, &dev->lcd_info, sizeof(struct fb_var_screeninfo));
728
729 win->rect_info.x = 0;
730 win->rect_info.y = 0;
731 win->rect_info.w = win->var_info.xres;
732 win->rect_info.h = win->var_info.yres;
733
734 if (window_set_pos(win) < 0) {
735 ALOGE("%s::window_set_pos is failed : %s",
736 __func__, strerror(errno));
737 status = -EINVAL;
738 goto err;
739 }
740
741 if (window_get_info(win) < 0) {
742 ALOGE("%s::window_get_info is failed : %s",
743 __func__, strerror(errno));
744 status = -EINVAL;
745 goto err;
746 }
747
748 win->size = win->fix_info.line_length * win->var_info.yres;
749
750 if (!win->fix_info.smem_start){
751 ALOGE("%s:: win-%d failed to get the reserved memory", __func__, i);
752 status = -EINVAL;
753 goto err;
754 }
755
756 for (int j = 0; j < NUM_OF_WIN_BUF; j++) {
757 win->addr[j] = win->fix_info.smem_start + (win->size * j);
758 ALOGI("%s::win-%d add[%d] %x ", __func__, i, j, win->addr[j]);
759 }
760 }
761
762 /* open pp */
763 if (createFimc(&dev->fimc) < 0) {
764 ALOGE("%s::creatFimc() fail", __func__);
765 status = -EINVAL;
766 goto err;
767 }
768
769 err = pthread_create(&dev->vsync_thread, NULL, hwc_vsync_thread, dev);
770 if (err) {
771 ALOGE("%s::pthread_create() failed : %s", __func__, strerror(err));
772 status = -err;
773 goto err;
774 }
775
776 ALOGD("%s:: success\n", __func__);
777
778 return 0;
779
780err:
781 if (destroyFimc(&dev->fimc) < 0)
782 ALOGE("%s::destroyFimc() fail", __func__);
783
784 if (window_close(&dev->global_lcd_win) < 0)
785 ALOGE("%s::window_close() fail", __func__);
786
787 for (int i = 0; i < NUM_OF_WIN; i++) {
788 if (window_close(&dev->win[i]) < 0)
789 ALOGE("%s::window_close() fail", __func__);
790 }
791
792 return status;
793}