blob: 88a8e8c0a917c63a5bbc9709faca4b4637b9e4b1 [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
Pawit Pornkitprasanb9ae8e02012-11-23 14:24:32 +070026#define HWC_REMOVE_DEPRECATED_VERSIONS 1
27
Chirayu Desai0a336cc2012-07-12 14:37:05 +053028#include <sys/resource.h>
29#include <cutils/log.h>
30#include <cutils/atomic.h>
31#include <EGL/egl.h>
32#include <GLES/gl.h>
33#include <hardware_legacy/uevent.h>
34#include "SecHWCUtils.h"
35
36static IMG_gralloc_module_public_t *gpsGrallocModule;
37
38static int hwc_device_open(const struct hw_module_t* module, const char* name,
39 struct hw_device_t** device);
40
41static struct hw_module_methods_t hwc_module_methods = {
42 open: hwc_device_open
43};
44
45hwc_module_t HAL_MODULE_INFO_SYM = {
46 common: {
47 tag: HARDWARE_MODULE_TAG,
48 module_api_version: HWC_MODULE_API_VERSION_0_1,
49 hal_api_version: HARDWARE_HAL_API_VERSION,
50 id: HWC_HARDWARE_MODULE_ID,
51 name: "Samsung S5PC11X hwcomposer module",
52 author: "SAMSUNG",
53 methods: &hwc_module_methods,
54 }
55};
56
Pawit Pornkitprasanb9ae8e02012-11-23 14:24:32 +070057static void dump_layer(hwc_layer_1_t const* l) {
Chirayu Desai0a336cc2012-07-12 14:37:05 +053058 ALOGD("\ttype=%d, flags=%08x, handle=%p, tr=%02x, blend=%04x, {%d,%d,%d,%d}, {%d,%d,%d,%d}",
59 l->compositionType, l->flags, l->handle, l->transform, l->blending,
60 l->sourceCrop.left,
61 l->sourceCrop.top,
62 l->sourceCrop.right,
63 l->sourceCrop.bottom,
64 l->displayFrame.left,
65 l->displayFrame.top,
66 l->displayFrame.right,
67 l->displayFrame.bottom);
68}
69
Pawit Pornkitprasanb9ae8e02012-11-23 14:24:32 +070070static int set_src_dst_info(hwc_layer_1_t *cur,
Chirayu Desai0a336cc2012-07-12 14:37:05 +053071 struct hwc_win_info_t *win,
72 struct sec_img *src_img,
73 struct sec_img *dst_img,
74 struct sec_rect *src_rect,
75 struct sec_rect *dst_rect,
76 int win_idx)
77{
78 IMG_native_handle_t *prev_handle = (IMG_native_handle_t *)(cur->handle);
79
80 // set src image
81 src_img->w = prev_handle->iWidth;
82 src_img->h = prev_handle->iHeight;
83 src_img->format = prev_handle->iFormat;
Petr Havlena1386f442012-10-26 17:52:00 +053084 src_img->base = 0;
Chirayu Desai0a336cc2012-07-12 14:37:05 +053085 src_img->offset = 0;
Petr Havlena1386f442012-10-26 17:52:00 +053086 src_img->mem_id = 0;
Chirayu Desai0a336cc2012-07-12 14:37:05 +053087
88 src_img->mem_type = HWC_PHYS_MEM_TYPE;
89 src_img->w = (src_img->w + 15) & (~15);
90 src_img->h = (src_img->h + 1) & (~1) ;
91
92 //set src rect
93 src_rect->x = SEC_MAX(cur->sourceCrop.left, 0);
94 src_rect->y = SEC_MAX(cur->sourceCrop.top, 0);
95 src_rect->w = SEC_MAX(cur->sourceCrop.right - cur->sourceCrop.left, 0);
96 src_rect->w = SEC_MIN(src_rect->w, src_img->w - src_rect->x);
97 src_rect->h = SEC_MAX(cur->sourceCrop.bottom - cur->sourceCrop.top, 0);
98 src_rect->h = SEC_MIN(src_rect->h, src_img->h - src_rect->y);
99
100 //set dst image
101 dst_img->w = win->lcd_info.xres;
102 dst_img->h = win->lcd_info.yres;
103
104 switch (win->lcd_info.bits_per_pixel) {
105 case 32:
106 dst_img->format = HAL_PIXEL_FORMAT_RGBX_8888;
107 break;
108 default:
109 dst_img->format = HAL_PIXEL_FORMAT_RGB_565;
110 break;
111 }
112
113 dst_img->base = win->addr[win->buf_index];
114 dst_img->offset = 0;
115 dst_img->mem_id = 0;
116 dst_img->mem_type = HWC_PHYS_MEM_TYPE;
117
118 //set dst rect
119 //fimc dst image will be stored from left top corner
120 dst_rect->x = 0;
121 dst_rect->y = 0;
122 dst_rect->w = win->rect_info.w;
123 dst_rect->h = win->rect_info.h;
124
125 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 ",
126 __func__, src_rect->x, src_rect->y, src_rect->w, src_rect->h,
127 dst_rect->x, dst_rect->y, dst_rect->w, dst_rect->h);
128
129 return 0;
130}
131
Pawit Pornkitprasanb9ae8e02012-11-23 14:24:32 +0700132static int get_hwc_compos_decision(hwc_layer_1_t* cur)
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530133{
134 if(cur->flags & HWC_SKIP_LAYER || !cur->handle) {
135 ALOGV("%s::is_skip_layer %d cur->handle %x",
136 __func__, cur->flags & HWC_SKIP_LAYER, (uint32_t)cur->handle);
137 return HWC_FRAMEBUFFER;
138 }
139
140 IMG_native_handle_t *prev_handle = (IMG_native_handle_t *)(cur->handle);
141 int compositionType = HWC_FRAMEBUFFER;
142
143 /* check here....if we have any resolution constraints */
144 if (((cur->sourceCrop.right - cur->sourceCrop.left) < 16) ||
145 ((cur->sourceCrop.bottom - cur->sourceCrop.top) < 8))
146 return compositionType;
147
148 if ((cur->transform == HAL_TRANSFORM_ROT_90) ||
149 (cur->transform == HAL_TRANSFORM_ROT_270)) {
150 if(((cur->displayFrame.right - cur->displayFrame.left) < 4)||
151 ((cur->displayFrame.bottom - cur->displayFrame.top) < 8))
152 return compositionType;
153 } else if (((cur->displayFrame.right - cur->displayFrame.left) < 8) ||
154 ((cur->displayFrame.bottom - cur->displayFrame.top) < 4))
155 return compositionType;
156
157 if((prev_handle->usage & GRALLOC_USAGE_PHYS_CONTIG) &&
158 (cur->blending == HWC_BLENDING_NONE))
159 compositionType = HWC_OVERLAY;
160 else
161 compositionType = HWC_FRAMEBUFFER;
162
163 ALOGV("%s::compositionType %d bpp %d format %x usage %x",
164 __func__,compositionType, prev_handle->uiBpp, prev_handle->iFormat,
165 prev_handle->usage & GRALLOC_USAGE_PHYS_CONTIG);
166
167 return compositionType;
168}
169
170static int assign_overlay_window(struct hwc_context_t *ctx,
Pawit Pornkitprasanb9ae8e02012-11-23 14:24:32 +0700171 hwc_layer_1_t *cur,
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530172 int win_idx,
173 int layer_idx)
174{
175 struct hwc_win_info_t *win;
176 sec_rect rect;
177 int ret = 0;
178
179 if(NUM_OF_WIN <= win_idx)
180 return -1;
181
182 win = &ctx->win[win_idx];
183
184 rect.x = SEC_MAX(cur->displayFrame.left, 0);
185 rect.y = SEC_MAX(cur->displayFrame.top, 0);
186 rect.w = SEC_MIN(cur->displayFrame.right - rect.x, win->lcd_info.xres - rect.x);
187 rect.h = SEC_MIN(cur->displayFrame.bottom - rect.y, win->lcd_info.yres - rect.y);
188 win->set_win_flag = 0;
189
190 if((rect.x != win->rect_info.x) || (rect.y != win->rect_info.y) ||
191 (rect.w != win->rect_info.w) || (rect.h != win->rect_info.h)){
192 win->rect_info.x = rect.x;
193 win->rect_info.y = rect.y;
194 win->rect_info.w = rect.w;
195 win->rect_info.h = rect.h;
196 win->set_win_flag = 1;
197 win->layer_prev_buf = 0;
198 }
199
200 win->layer_index = layer_idx;
201 win->status = HWC_WIN_RESERVED;
202
203 ALOGV("%s:: win_x %d win_y %d win_w %d win_h %d lay_idx %d win_idx %d",
204 __func__, win->rect_info.x, win->rect_info.y, win->rect_info.w,
205 win->rect_info.h, win->layer_index, win_idx );
206
207 return 0;
208}
209
210static void reset_win_rect_info(hwc_win_info_t *win)
211{
212 win->rect_info.x = 0;
213 win->rect_info.y = 0;
214 win->rect_info.w = 0;
215 win->rect_info.h = 0;
216 return;
217}
218
Pawit Pornkitprasanb9ae8e02012-11-23 14:24:32 +0700219static int hwc_prepare(hwc_composer_device_1_t *dev,
220 size_t numDisplays, hwc_display_contents_1_t** displays)
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530221{
222
223 struct hwc_context_t* ctx = (struct hwc_context_t*)dev;
224 int overlay_win_cnt = 0;
225 int compositionType = 0;
226 int ret;
227
Pawit Pornkitprasanb9ae8e02012-11-23 14:24:32 +0700228 // Compat
229 hwc_display_contents_1_t* list = NULL;
230 if (numDisplays > 0) {
231 list = displays[0];
232 }
233
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530234 //if geometry is not changed, there is no need to do any work here
235 if( !list || (!(list->flags & HWC_GEOMETRY_CHANGED)))
236 return 0;
237
238 //all the windows are free here....
239 for (int i = 0; i < NUM_OF_WIN; i++) {
240 ctx->win[i].status = HWC_WIN_FREE;
241 ctx->win[i].buf_index = 0;
242 }
243 ctx->num_of_hwc_layer = 0;
244 ctx->num_of_fb_layer = 0;
245 ALOGV("%s:: hwc_prepare list->numHwLayers %d", __func__, list->numHwLayers);
246
247 for (int i = 0; i < list->numHwLayers ; i++) {
Pawit Pornkitprasanb9ae8e02012-11-23 14:24:32 +0700248 hwc_layer_1_t* cur = &list->hwLayers[i];
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530249
250 if (overlay_win_cnt < NUM_OF_WIN) {
251 compositionType = get_hwc_compos_decision(cur);
252
253 if (compositionType == HWC_FRAMEBUFFER) {
254 cur->compositionType = HWC_FRAMEBUFFER;
255 ctx->num_of_fb_layer++;
256 } else {
257 ret = assign_overlay_window(ctx, cur, overlay_win_cnt, i);
258 if (ret != 0) {
259 cur->compositionType = HWC_FRAMEBUFFER;
260 ctx->num_of_fb_layer++;
261 continue;
262 }
263
264 cur->compositionType = HWC_OVERLAY;
265 cur->hints = HWC_HINT_CLEAR_FB;
266 overlay_win_cnt++;
267 ctx->num_of_hwc_layer++;
268 }
269 } else {
270 cur->compositionType = HWC_FRAMEBUFFER;
271 ctx->num_of_fb_layer++;
272 }
273 }
274
275 if(list->numHwLayers != (ctx->num_of_fb_layer + ctx->num_of_hwc_layer))
276 ALOGV("%s:: numHwLayers %d num_of_fb_layer %d num_of_hwc_layer %d ",
277 __func__, list->numHwLayers, ctx->num_of_fb_layer,
278 ctx->num_of_hwc_layer);
279
280 if (overlay_win_cnt < NUM_OF_WIN) {
281 //turn off the free windows
282 for (int i = overlay_win_cnt; i < NUM_OF_WIN; i++) {
283 window_hide(&ctx->win[i]);
284 reset_win_rect_info(&ctx->win[i]);
285 }
286 }
287 return 0;
288}
289
Pawit Pornkitprasanb9ae8e02012-11-23 14:24:32 +0700290static int hwc_set(hwc_composer_device_1_t *dev,
291 size_t numDisplays, hwc_display_contents_1_t** displays)
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530292{
293 struct hwc_context_t *ctx = (struct hwc_context_t *)dev;
294 unsigned int phyAddr[MAX_NUM_PLANES];
295 int skipped_window_mask = 0;
Pawit Pornkitprasanb9ae8e02012-11-23 14:24:32 +0700296 hwc_layer_1_t* cur;
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530297 struct hwc_win_info_t *win;
298 int ret;
299 struct sec_img src_img;
300 struct sec_img dst_img;
301 struct sec_rect src_rect;
302 struct sec_rect dst_rect;
303
Pawit Pornkitprasanb9ae8e02012-11-23 14:24:32 +0700304 // Only support one display
305 hwc_display_t dpy = displays[0]->dpy;
306 hwc_surface_t sur = displays[0]->sur;
307 hwc_display_contents_1_t* list = displays[0];
308
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530309 if (dpy == NULL && sur == NULL && list == NULL) {
310 // release our resources, the screen is turning off
311 // in our case, there is nothing to do.
312 ctx->num_of_fb_layer_prev = 0;
313 return 0;
314 }
315
316 bool need_swap_buffers = ctx->num_of_fb_layer > 0;
317
318 /*
319 * H/W composer documentation states:
320 * There is an implicit layer containing opaque black
321 * pixels behind all the layers in the list.
322 * It is the responsibility of the hwcomposer module to make
323 * sure black pixels are output (or blended from).
324 *
325 * Since we're using a blitter, we need to erase the frame-buffer when
326 * switching to all-overlay mode.
327 *
328 */
329 if (ctx->num_of_hwc_layer &&
330 ctx->num_of_fb_layer==0 && ctx->num_of_fb_layer_prev) {
331 /* we're clearing the screen using GLES here, this is very
332 * hack-ish, ideal we would use the fimc (if it can do it) */
333 glDisable(GL_SCISSOR_TEST);
334 glClearColor(0, 0, 0, 0);
335 glClear(GL_COLOR_BUFFER_BIT);
336 glEnable(GL_SCISSOR_TEST);
337 need_swap_buffers = true;
338 }
339
340 ctx->num_of_fb_layer_prev = ctx->num_of_fb_layer;
341
342 if (need_swap_buffers || !list) {
343 EGLBoolean sucess = eglSwapBuffers((EGLDisplay)dpy, (EGLSurface)sur);
344 if (!sucess) {
345 return HWC_EGL_ERROR;
346 }
347 }
348
349 if (!list) {
350 /* turn off the all windows */
351 for (int i = 0; i < NUM_OF_WIN; i++) {
352 window_hide(&ctx->win[i]);
353 reset_win_rect_info(&ctx->win[i]);
354 ctx->win[i].status = HWC_WIN_FREE;
355 }
356 ctx->num_of_hwc_layer = 0;
357 return 0;
358 }
359
360 if(ctx->num_of_hwc_layer > NUM_OF_WIN)
361 ctx->num_of_hwc_layer = NUM_OF_WIN;
362
363 /* compose hardware layers here */
364 for (uint32_t i = 0; i < ctx->num_of_hwc_layer; i++) {
365 win = &ctx->win[i];
366 if (win->status == HWC_WIN_RESERVED) {
367 cur = &list->hwLayers[win->layer_index];
368
369 if (cur->compositionType == HWC_OVERLAY) {
370
371 ret = gpsGrallocModule->GetPhyAddrs(gpsGrallocModule,
372 cur->handle, phyAddr);
373 if (ret) {
374 ALOGE("%s::GetPhyAddrs fail : ret=%d\n", __func__, ret);
375 skipped_window_mask |= (1 << i);
376 continue;
377 }
378
379 /* initialize the src & dist context for fimc */
380 set_src_dst_info (cur, win, &src_img, &dst_img, &src_rect,
381 &dst_rect, i);
382
383 ret = runFimc(ctx, &src_img, &src_rect, &dst_img, &dst_rect,
384 phyAddr, cur->transform);
385 if (ret < 0){
386 ALOGE("%s::runFimc fail : ret=%d\n", __func__, ret);
387 skipped_window_mask |= (1 << i);
388 continue;
389 }
390
391 if (win->set_win_flag == 1) {
392 /* turnoff the window and set the window position with new conf... */
393 if (window_set_pos(win) < 0) {
394 ALOGE("%s::window_set_pos is failed : %s", __func__,
395 strerror(errno));
396 skipped_window_mask |= (1 << i);
397 continue;
398 }
399 win->set_win_flag = 0;
400 }
401
402 /* is the frame didn't change, it needs to be composited
403 * because something else below it could have changed, however
404 * it doesn't need to be swapped.
405 */
406 if (win->layer_prev_buf != (uint32_t)cur->handle) {
407 win->layer_prev_buf = (uint32_t)cur->handle;
408 window_pan_display(win);
409 win->buf_index = (win->buf_index + 1) % NUM_OF_WIN_BUF;
410 }
411
412 if(win->power_state == 0)
413 window_show(win);
414
415 } else {
416 ALOGE("%s:: error : layer %d compositionType should have been \
417 HWC_OVERLAY", __func__, win->layer_index);
418 skipped_window_mask |= (1 << i);
419 continue;
420 }
421 } else {
422 ALOGE("%s:: error : window status should have been HWC_WIN_RESERVED \
423 by now... ", __func__);
424 skipped_window_mask |= (1 << i);
425 continue;
426 }
427 }
428
429 if (skipped_window_mask) {
430 //turn off the free windows
431 for (int i = 0; i < NUM_OF_WIN; i++) {
432 if (skipped_window_mask & (1 << i))
433 window_hide(&ctx->win[i]);
434 }
435 }
436
Petr Havlena1386f442012-10-26 17:52:00 +0530437#if defined(BOARD_USES_HDMI)
438 hdmi_device_t* hdmi = ctx->hdmi;
439 if (ctx->num_of_hwc_layer == 1 && hdmi) {
440 if ((src_img.format == HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP_TILED)||
441 (src_img.format == HAL_PIXEL_FORMAT_CUSTOM_YCrCb_420_SP)) {
442 ADDRS * addr = (ADDRS *)(src_img.base);
443 hdmi->blit(hdmi,
444 src_img.w,
445 src_img.h,
446 src_img.format,
447 (unsigned int)addr->addr_y,
448 (unsigned int)addr->addr_cbcr,
449 (unsigned int)addr->addr_cbcr,
450 0, 0,
451 HDMI_MODE_VIDEO,
452 ctx->num_of_hwc_layer);
453 } else if ((src_img.format == HAL_PIXEL_FORMAT_YCbCr_420_SP) ||
454 (src_img.format == HAL_PIXEL_FORMAT_YCrCb_420_SP) ||
455 (src_img.format == HAL_PIXEL_FORMAT_YCbCr_420_P) ||
456 (src_img.format == HAL_PIXEL_FORMAT_YV12)) {
457 hdmi->blit(hdmi,
458 src_img.w,
459 src_img.h,
460 src_img.format,
461 (unsigned int)ctx->fimc.params.src.buf_addr_phy_rgb_y,
462 (unsigned int)ctx->fimc.params.src.buf_addr_phy_cb,
463 (unsigned int)ctx->fimc.params.src.buf_addr_phy_cr,
464 0, 0,
465 HDMI_MODE_VIDEO,
466 ctx->num_of_hwc_layer);
467 } else {
468 ALOGE("%s: Unsupported format = %d for hdmi", __func__, src_img.format);
469 }
470 }
471#endif
472
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530473 return 0;
474}
475
Pawit Pornkitprasanb9ae8e02012-11-23 14:24:32 +0700476static void hwc_registerProcs(struct hwc_composer_device_1* dev,
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530477 hwc_procs_t const* procs)
478{
479 struct hwc_context_t* ctx = (struct hwc_context_t*)dev;
480 ctx->procs = const_cast<hwc_procs_t *>(procs);
481}
482
Pawit Pornkitprasanb9ae8e02012-11-23 14:24:32 +0700483static int hwc_blank(struct hwc_composer_device_1 *dev,
484 int disp, int blank)
485{
486 struct hwc_context_t* ctx = (struct hwc_context_t*)dev;
487 if (blank) {
488 // release our resources, the screen is turning off
489 // in our case, there is nothing to do.
490 ctx->num_of_fb_layer_prev = 0;
491 return 0;
492 }
493 else {
494 // No need to unblank, will unblank on set()
495 return 0;
496 }
497}
498
499static int hwc_query(struct hwc_composer_device_1* dev,
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530500 int what, int* value)
501{
502 struct hwc_context_t* ctx = (struct hwc_context_t*)dev;
503
504 switch (what) {
505 case HWC_BACKGROUND_LAYER_SUPPORTED:
506 // we don't support the background layer yet
507 value[0] = 0;
508 break;
509 case HWC_VSYNC_PERIOD:
510 // vsync period in nanosecond
511 value[0] = 1000000000.0 / gpsGrallocModule->psFrameBufferDevice->base.fps;
512 break;
513 default:
514 // unsupported query
515 return -EINVAL;
516 }
517 return 0;
518}
519
Michael Brehm3c938a72012-09-15 02:03:39 -0500520#ifdef VSYNC_IOCTL
521// Linux version of a manual reset event to control when
522// and when not to ask the video card for a VSYNC. This
523// stops the worker thread from asking for a VSYNC when
524// there is nothing useful to do with it and more closely
525// mimicks the original uevent mechanism
526int vsync_enable = 0;
527pthread_mutex_t vsync_mutex = PTHREAD_MUTEX_INITIALIZER;
528pthread_cond_t vsync_condition = PTHREAD_COND_INITIALIZER;
529#endif
530
Pawit Pornkitprasanb9ae8e02012-11-23 14:24:32 +0700531static int hwc_eventControl(struct hwc_composer_device_1* dev, int dpy,
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530532 int event, int enabled)
533{
534 struct hwc_context_t* ctx = (struct hwc_context_t*)dev;
535
536 switch (event) {
537 case HWC_EVENT_VSYNC:
538 int val = !!enabled;
539 int err = ioctl(ctx->global_lcd_win.fd, S3CFB_SET_VSYNC_INT, &val);
540 if (err < 0)
541 return -errno;
542
Michael Brehm3c938a72012-09-15 02:03:39 -0500543#if VSYNC_IOCTL
544 // Enable or disable the ability for the worker thread
545 // to ask for VSYNC events from the video driver
546 pthread_mutex_lock(&vsync_mutex);
547 if(enabled) {
548 vsync_enable = 1;
549 pthread_cond_broadcast(&vsync_condition);
550 }
551 else vsync_enable = 0;
552 pthread_mutex_unlock(&vsync_mutex);
553#endif
554
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530555 return 0;
556 }
557
558 return -EINVAL;
559}
560
561void handle_vsync_uevent(hwc_context_t *ctx, const char *buff, int len)
562{
563 uint64_t timestamp = 0;
564 const char *s = buff;
565
566 if(!ctx->procs || !ctx->procs->vsync)
567 return;
568
569 s += strlen(s) + 1;
570
571 while(*s) {
572 if (!strncmp(s, "VSYNC=", strlen("VSYNC=")))
573 timestamp = strtoull(s + strlen("VSYNC="), NULL, 0);
574
575 s += strlen(s) + 1;
576 if (s - buff >= len)
577 break;
578 }
579
580 ctx->procs->vsync(ctx->procs, 0, timestamp);
581}
582
583static void *hwc_vsync_thread(void *data)
584{
585 hwc_context_t *ctx = (hwc_context_t *)(data);
Michael Brehm3c938a72012-09-15 02:03:39 -0500586#ifdef VSYNC_IOCTL
587 uint64_t timestamp = 0;
588#else
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530589 char uevent_desc[4096];
590 memset(uevent_desc, 0, sizeof(uevent_desc));
Michael Brehm3c938a72012-09-15 02:03:39 -0500591#endif
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530592
593 setpriority(PRIO_PROCESS, 0, HAL_PRIORITY_URGENT_DISPLAY);
594
Michael Brehm3c938a72012-09-15 02:03:39 -0500595#ifndef VSYNC_IOCTL
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530596 uevent_init();
Michael Brehm3c938a72012-09-15 02:03:39 -0500597#endif
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530598 while(true) {
Michael Brehm3c938a72012-09-15 02:03:39 -0500599#ifdef VSYNC_IOCTL
600 // Only continue if hwc_eventControl is enabled, otherwise
601 // just sit here and wait until it is. This stops the code
602 // from constantly looking for the VSYNC event with the screen
603 // turned off.
604 pthread_mutex_lock(&vsync_mutex);
605 if(!vsync_enable) pthread_cond_wait(&vsync_condition, &vsync_mutex);
606 pthread_mutex_unlock(&vsync_mutex);
607
608 timestamp = 0; // Reset the timestamp value
609
610 // S3CFB_WAIT_FOR_VSYNC is a custom IOCTL I added to wait for
611 // the VSYNC interrupt, and then return the timestamp that was
612 // originally being communicated via a uevent. The uevent was
613 // spamming the UEventObserver and events/0 process with more
614 // information than this device could really deal with every 18ms
615 int res = ioctl(ctx->global_lcd_win.fd, S3CFB_WAIT_FOR_VSYNC, &timestamp);
616 if(res > 0) {
617 if(!ctx->procs || !ctx->procs->vsync) continue;
618 ctx->procs->vsync(ctx->procs, 0, timestamp);
619 }
620#else
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530621 int len = uevent_next_event(uevent_desc, sizeof(uevent_desc) - 2);
622
623 bool vsync = !strcmp(uevent_desc, "change@/devices/platform/s3cfb");
624 if(vsync)
625 handle_vsync_uevent(ctx, uevent_desc, len);
Michael Brehm3c938a72012-09-15 02:03:39 -0500626#endif
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530627 }
628
629 return NULL;
630}
631
632static int hwc_device_close(struct hw_device_t *dev)
633{
634 struct hwc_context_t* ctx = (struct hwc_context_t*)dev;
635 int ret = 0;
636 int i;
637
638 if (ctx) {
639 if (destroyFimc(&ctx->fimc) < 0) {
640 ALOGE("%s::destroyFimc fail", __func__);
641 ret = -1;
642 }
643
644 if (window_close(&ctx->global_lcd_win) < 0) {
645 ALOGE("%s::window_close() fail", __func__);
646 ret = -1;
647 }
648
649 for (i = 0; i < NUM_OF_WIN; i++) {
650 if (window_close(&ctx->win[i]) < 0) {
651 ALOGE("%s::window_close() fail", __func__);
652 ret = -1;
653 }
654 }
655
656 // TODO: stop vsync_thread
657
658 free(ctx);
659 }
660 return ret;
661}
662
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530663static int hwc_device_open(const struct hw_module_t* module, const char* name,
664 struct hw_device_t** device)
665{
666 int status = 0;
667 int err;
668 struct hwc_win_info_t *win;
Petr Havlena1386f442012-10-26 17:52:00 +0530669#if defined(BOARD_USES_HDMI)
670 struct hw_module_t *hdmi_module;
671#endif
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530672
673 if(hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
674 (const hw_module_t**)&gpsGrallocModule))
675 return -EINVAL;
676
677 if(strcmp(gpsGrallocModule->base.common.author, "Imagination Technologies"))
678 return -EINVAL;
679
680 if (strcmp(name, HWC_HARDWARE_COMPOSER))
681 return -EINVAL;
682
683 struct hwc_context_t *dev;
684 dev = (hwc_context_t*)malloc(sizeof(*dev));
685
686 /* initialize our state here */
687 memset(dev, 0, sizeof(*dev));
688
689 /* initialize the procs */
690 dev->device.common.tag = HARDWARE_DEVICE_TAG;
Pawit Pornkitprasanb9ae8e02012-11-23 14:24:32 +0700691 dev->device.common.version = HWC_DEVICE_API_VERSION_1_0;
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530692 dev->device.common.module = const_cast<hw_module_t*>(module);
693 dev->device.common.close = hwc_device_close;
694
695 dev->device.prepare = hwc_prepare;
696 dev->device.set = hwc_set;
Pawit Pornkitprasanb9ae8e02012-11-23 14:24:32 +0700697 dev->device.eventControl = hwc_eventControl;
698 dev->device.blank = hwc_blank;
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530699 dev->device.query = hwc_query;
Pawit Pornkitprasanb9ae8e02012-11-23 14:24:32 +0700700 dev->device.registerProcs = hwc_registerProcs;
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530701
702 *device = &dev->device.common;
703
Petr Havlena1386f442012-10-26 17:52:00 +0530704#if defined(BOARD_USES_HDMI)
705 dev->hdmi = NULL;
706 if(hw_get_module(HDMI_HARDWARE_MODULE_ID,
707 (const hw_module_t**)&hdmi_module)) {
708 ALOGE("%s:: HDMI device not present", __func__);
709 } else {
710 int ret = module->methods->open(hdmi_module, "hdmi-composer",
711 (hw_device_t **)&dev->hdmi);
712 if(ret < 0) {
713 ALOGE("%s:: Failed to open hdmi device : %s", __func__, strerror(ret));
714 }
715 }
716#endif
717
Chirayu Desai0a336cc2012-07-12 14:37:05 +0530718 /* initializing */
719 memset(&(dev->fimc), 0, sizeof(s5p_fimc_t));
720 dev->fimc.dev_fd = -1;
721
722 /* open WIN0 & WIN1 here */
723 for (int i = 0; i < NUM_OF_WIN; i++) {
724 if (window_open(&(dev->win[i]), i) < 0) {
725 ALOGE("%s:: Failed to open window %d device ", __func__, i);
726 status = -EINVAL;
727 goto err;
728 }
729 }
730
731 /* open window 2, used to query global LCD info */
732 if (window_open(&dev->global_lcd_win, 2) < 0) {
733 ALOGE("%s:: Failed to open window 2 device ", __func__);
734 status = -EINVAL;
735 goto err;
736 }
737
738 /* get default window config */
739 if (window_get_global_lcd_info(dev) < 0) {
740 ALOGE("%s::window_get_global_lcd_info is failed : %s",
741 __func__, strerror(errno));
742 status = -EINVAL;
743 goto err;
744 }
745
746 dev->lcd_info.yres_virtual = dev->lcd_info.yres * NUM_OF_WIN_BUF;
747
748 /* initialize the window context */
749 for (int i = 0; i < NUM_OF_WIN; i++) {
750 win = &dev->win[i];
751 memcpy(&win->lcd_info, &dev->lcd_info, sizeof(struct fb_var_screeninfo));
752 memcpy(&win->var_info, &dev->lcd_info, sizeof(struct fb_var_screeninfo));
753
754 win->rect_info.x = 0;
755 win->rect_info.y = 0;
756 win->rect_info.w = win->var_info.xres;
757 win->rect_info.h = win->var_info.yres;
758
759 if (window_set_pos(win) < 0) {
760 ALOGE("%s::window_set_pos is failed : %s",
761 __func__, strerror(errno));
762 status = -EINVAL;
763 goto err;
764 }
765
766 if (window_get_info(win) < 0) {
767 ALOGE("%s::window_get_info is failed : %s",
768 __func__, strerror(errno));
769 status = -EINVAL;
770 goto err;
771 }
772
773 win->size = win->fix_info.line_length * win->var_info.yres;
774
775 if (!win->fix_info.smem_start){
776 ALOGE("%s:: win-%d failed to get the reserved memory", __func__, i);
777 status = -EINVAL;
778 goto err;
779 }
780
781 for (int j = 0; j < NUM_OF_WIN_BUF; j++) {
782 win->addr[j] = win->fix_info.smem_start + (win->size * j);
783 ALOGI("%s::win-%d add[%d] %x ", __func__, i, j, win->addr[j]);
784 }
785 }
786
787 /* open pp */
788 if (createFimc(&dev->fimc) < 0) {
789 ALOGE("%s::creatFimc() fail", __func__);
790 status = -EINVAL;
791 goto err;
792 }
793
794 err = pthread_create(&dev->vsync_thread, NULL, hwc_vsync_thread, dev);
795 if (err) {
796 ALOGE("%s::pthread_create() failed : %s", __func__, strerror(err));
797 status = -err;
798 goto err;
799 }
800
801 ALOGD("%s:: success\n", __func__);
802
803 return 0;
804
805err:
806 if (destroyFimc(&dev->fimc) < 0)
807 ALOGE("%s::destroyFimc() fail", __func__);
808
809 if (window_close(&dev->global_lcd_win) < 0)
810 ALOGE("%s::window_close() fail", __func__);
811
812 for (int i = 0; i < NUM_OF_WIN; i++) {
813 if (window_close(&dev->win[i]) < 0)
814 ALOGE("%s::window_close() fail", __func__);
815 }
816
817 return status;
818}