blob: c43fa04ffa553e551c7bbe42a9b7793a8ccde485 [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
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#include <stdlib.h>
18#include <stdint.h>
19#include <string.h>
20#include <unistd.h>
21
22#include <hardware/hardware.h>
23
24#include <fcntl.h>
25#include <errno.h>
26
27#include <cutils/log.h>
28#include <cutils/atomic.h>
29#include <cutils/properties.h>
30
31#include <hardware/hwcomposer.h>
32#include <overlayLib.h>
33#include <overlayLibUI.h>
34#include <copybit.h>
35
36#include <EGL/egl.h>
37#include <EGL/eglext.h>
38#include <ui/android_native_buffer.h>
39#include <gralloc_priv.h>
40#include <genlock.h>
41#include <qcom_ui.h>
42#include <gr.h>
43
44/*****************************************************************************/
45#define ALIGN(x, align) (((x) + ((align)-1)) & ~((align)-1))
46#define LIKELY( exp ) (__builtin_expect( (exp) != 0, true ))
47#define UNLIKELY( exp ) (__builtin_expect( (exp) != 0, false ))
48
49#ifdef COMPOSITION_BYPASS
50#define MAX_BYPASS_LAYERS 3
51#define BYPASS_DEBUG 0
52#define BYPASS_INDEX_OFFSET 4
53
54enum BypassState {
55 BYPASS_ON,
56 BYPASS_OFF,
57 BYPASS_OFF_PENDING,
58};
59
60enum BypassBufferLockState {
61 BYPASS_BUFFER_UNLOCKED,
62 BYPASS_BUFFER_LOCKED,
63};
64#endif
65
66enum HWCLayerType{
67 HWC_SINGLE_VIDEO = 0x1,
68 HWC_ORIG_RESOLUTION = 0x2,
69 HWC_S3D_LAYER = 0x4,
70 HWC_STOP_UI_MIRRORING_MASK = 0xF
71};
72
73enum eHWCOverlayStatus {
74 HWC_OVERLAY_OPEN,
75 HWC_OVERLAY_PREPARE_TO_CLOSE,
76 HWC_OVERLAY_CLOSED
77};
78
79struct hwc_context_t {
80 hwc_composer_device_t device;
81 /* our private state goes below here */
82 overlay::Overlay* mOverlayLibObject;
83 native_handle_t *previousOverlayHandle;
84#ifdef COMPOSITION_BYPASS
85 overlay::OverlayUI* mOvUI[MAX_BYPASS_LAYERS];
86 native_handle_t* previousBypassHandle[MAX_BYPASS_LAYERS];
87 BypassBufferLockState bypassBufferLockState[MAX_BYPASS_LAYERS];
88 int layerindex[MAX_BYPASS_LAYERS];
89 int nPipesUsed;
90 BypassState bypassState;
91#endif
92#if defined HDMI_DUAL_DISPLAY
93 external_display mHDMIEnabled; // Type of external display
94 bool pendingHDMI;
95#endif
96 int previousLayerCount;
97 eHWCOverlayStatus hwcOverlayStatus;
98};
99
100static int hwc_device_open(const struct hw_module_t* module, const char* name,
101 struct hw_device_t** device);
102
103static struct hw_module_methods_t hwc_module_methods = {
104 open: hwc_device_open
105};
106
107
108struct private_hwc_module_t {
109 hwc_module_t base;
110 copybit_device_t *copybitEngine;
111 framebuffer_device_t *fbDevice;
112 int compositionType;
113 bool isBypassEnabled; //from build.prop ro.sf.compbypass.enable
114};
115
116struct private_hwc_module_t HAL_MODULE_INFO_SYM = {
117 base: {
118 common: {
119 tag: HARDWARE_MODULE_TAG,
120 version_major: 1,
121 version_minor: 0,
122 id: HWC_HARDWARE_MODULE_ID,
123 name: "Hardware Composer Module",
124 author: "The Android Open Source Project",
125 methods: &hwc_module_methods,
126 }
127 },
128 copybitEngine: NULL,
129 fbDevice: NULL,
130 compositionType: 0,
131 isBypassEnabled: false,
132};
133
134//Only at this point would the compiler know all storage class sizes.
135//The header has hooks which need to know those beforehand.
136#include "external_display_only.h"
137
138/*****************************************************************************/
139
140static void dump_layer(hwc_layer_t const* l) {
141 LOGD("\ttype=%d, flags=%08x, handle=%p, tr=%02x, blend=%04x, {%d,%d,%d,%d}, {%d,%d,%d,%d}",
142 l->compositionType, l->flags, l->handle, l->transform, l->blending,
143 l->sourceCrop.left,
144 l->sourceCrop.top,
145 l->sourceCrop.right,
146 l->sourceCrop.bottom,
147 l->displayFrame.left,
148 l->displayFrame.top,
149 l->displayFrame.right,
150 l->displayFrame.bottom);
151}
152
153static inline int min(const int& a, const int& b) {
154 return (a < b) ? a : b;
155}
156
157static inline int max(const int& a, const int& b) {
158 return (a > b) ? a : b;
159}
160#ifdef COMPOSITION_BYPASS
161void setLayerbypassIndex(hwc_layer_t* layer, const int bypass_index)
162{
163 layer->flags &= ~HWC_BYPASS_INDEX_MASK;
164 layer->flags |= bypass_index << BYPASS_INDEX_OFFSET;
165}
166
167int getLayerbypassIndex(hwc_layer_t* layer)
168{
169 int byp_index = -1;
170
171 if(layer->flags & HWC_COMP_BYPASS) {
172 byp_index = ((layer->flags & HWC_BYPASS_INDEX_MASK) >> BYPASS_INDEX_OFFSET);
173 byp_index = (byp_index < MAX_BYPASS_LAYERS ? byp_index : -1 );
174 }
175 return byp_index;
176}
177
178void unlockPreviousBypassBuffers(hwc_context_t* ctx) {
179 // Unlock the previous bypass buffers. We can blindly unlock the buffers here,
180 // because buffers will be in this list only if the lock was successfully acquired.
181 for(int i = 0; i < MAX_BYPASS_LAYERS && ctx->previousBypassHandle[i]; i++) {
182 private_handle_t *hnd = (private_handle_t*) ctx->previousBypassHandle[i];
183
184 // Validate the handle to make sure it hasn't been deallocated.
185 if (private_handle_t::validate(ctx->previousBypassHandle[i])) {
186 continue;
187 }
188 // Check if the handle was locked previously
189 if (private_handle_t::PRIV_FLAGS_HWC_LOCK & hnd->flags) {
190 if (GENLOCK_FAILURE == genlock_unlock_buffer(ctx->previousBypassHandle[i])) {
191 LOGE("%s: genlock_unlock_buffer failed", __FUNCTION__);
192 } else {
193 ctx->previousBypassHandle[i] = NULL;
194 // Reset the lock flag
195 hnd->flags &= ~private_handle_t::PRIV_FLAGS_HWC_LOCK;
196 }
197 }
198 }
199}
200
201void print_info(hwc_layer_t* layer)
202{
203 hwc_rect_t sourceCrop = layer->sourceCrop;
204 hwc_rect_t displayFrame = layer->displayFrame;
205
206 int s_l = sourceCrop.left;
207 int s_t = sourceCrop.top;
208 int s_r = sourceCrop.right;
209 int s_b = sourceCrop.bottom;
210
211 int d_l = displayFrame.left;
212 int d_t = displayFrame.top;
213 int d_r = displayFrame.right;
214 int d_b = displayFrame.bottom;
215
216 LOGE_IF(BYPASS_DEBUG, "src:[%d,%d,%d,%d] (%d x %d) dst:[%d,%d,%d,%d] (%d x %d)",
217 s_l, s_t, s_r, s_b, (s_r - s_l), (s_b - s_t),
218 d_l, d_t, d_r, d_b, (d_r - d_l), (d_b - d_t));
219}
220
221//Crops source buffer against destination and FB boundaries
222void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst, int hw_w, int hw_h) {
223
224 int& crop_x = crop.left;
225 int& crop_y = crop.top;
226 int& crop_r = crop.right;
227 int& crop_b = crop.bottom;
228 int crop_w = crop.right - crop.left;
229 int crop_h = crop.bottom - crop.top;
230
231 int& dst_x = dst.left;
232 int& dst_y = dst.top;
233 int& dst_r = dst.right;
234 int& dst_b = dst.bottom;
235 int dst_w = dst.right - dst.left;
236 int dst_h = dst.bottom - dst.top;
237
238 if(dst_x < 0) {
239 float scale_x = crop_w * 1.0f / dst_w;
240 float diff_factor = (scale_x * abs(dst_x));
241 crop_x = crop_x + (int)diff_factor;
242 crop_w = crop_r - crop_x;
243
244 dst_x = 0;
245 dst_w = dst_r - dst_x;;
246 }
247 if(dst_r > hw_w) {
248 float scale_x = crop_w * 1.0f / dst_w;
249 float diff_factor = scale_x * (dst_r - hw_w);
250 crop_r = crop_r - diff_factor;
251 crop_w = crop_r - crop_x;
252
253 dst_r = hw_w;
254 dst_w = dst_r - dst_x;
255 }
256 if(dst_y < 0) {
257 float scale_y = crop_h * 1.0f / dst_h;
258 float diff_factor = scale_y * abs(dst_y);
259 crop_y = crop_y + diff_factor;
260 crop_h = crop_b - crop_y;
261
262 dst_y = 0;
263 dst_h = dst_b - dst_y;
264 }
265 if(dst_b > hw_h) {
266 float scale_y = crop_h * 1.0f / dst_h;
267 float diff_factor = scale_y * (dst_b - hw_h);
268 crop_b = crop_b - diff_factor;
269 crop_h = crop_b - crop_y;
270
271 dst_b = hw_h;
272 dst_h = dst_b - dst_y;
273 }
274
275 LOGE_IF(BYPASS_DEBUG,"crop: [%d,%d,%d,%d] dst:[%d,%d,%d,%d]",
276 crop_x, crop_y, crop_w, crop_h,dst_x, dst_y, dst_w, dst_h);
277}
278
279/*
280 * Configures pipe(s) for composition bypass
281 */
282static int prepareBypass(hwc_context_t *ctx, hwc_layer_t *layer,
283 int nPipeIndex, int vsync_wait, int isFG) {
284
285 if (ctx && ctx->mOvUI[nPipeIndex]) {
286 overlay::OverlayUI *ovUI = ctx->mOvUI[nPipeIndex];
287
288 private_hwc_module_t* hwcModule = reinterpret_cast<
289 private_hwc_module_t*>(ctx->device.common.module);
290 if (!hwcModule) {
291 LOGE("%s: NULL Module", __FUNCTION__);
292 return -1;
293 }
294
295 private_handle_t *hnd = (private_handle_t *)layer->handle;
296 if(!hnd) {
297 LOGE("%s: layer handle is NULL", __FUNCTION__);
298 return -1;
299 }
300
301 int hw_w = hwcModule->fbDevice->width;
302 int hw_h = hwcModule->fbDevice->height;
303
304 hwc_rect_t sourceCrop = layer->sourceCrop;
305 hwc_rect_t displayFrame = layer->displayFrame;
306
307 const int src_w = sourceCrop.right - sourceCrop.left;
308 const int src_h = sourceCrop.bottom - sourceCrop.top;
309
310 hwc_rect_t crop = sourceCrop;
311 int crop_w = crop.right - crop.left;
312 int crop_h = crop.bottom - crop.top;
313
314 hwc_rect_t dst = displayFrame;
315 int dst_w = dst.right - dst.left;
316 int dst_h = dst.bottom - dst.top;
317
318 if(hnd != NULL && (hnd->flags & private_handle_t::PRIV_FLAGS_NONCONTIGUOUS_MEM )) {
319 LOGE("%s: Unable to setup bypass due to non-pmem memory",__FUNCTION__);
320 return -1;
321 }
322
323 if(dst.left < 0 || dst.top < 0 || dst.right > hw_w || dst.bottom > hw_h) {
324 LOGE_IF(BYPASS_DEBUG,"%s: Destination has negative coordinates", __FUNCTION__);
325
326 calculate_crop_rects(crop, dst, hw_w, hw_h);
327
328 //Update calulated width and height
329 crop_w = crop.right - crop.left;
330 crop_h = crop.bottom - crop.top;
331
332 dst_w = dst.right - dst.left;
333 dst_h = dst.bottom - dst.top;
334 }
335
336 if( (dst_w > hw_w)|| (dst_h > hw_h)) {
337 LOGE_IF(BYPASS_DEBUG,"%s: Destination rectangle exceeds FB resolution", __FUNCTION__);
338 print_info(layer);
339 dst_w = hw_w;
340 dst_h = hw_h;
341 }
342
343 overlay_buffer_info info;
344 info.width = src_w;
345 info.height = src_h;
346 info.format = hnd->format;
347 info.size = hnd->size;
348
349 int fbnum = 0;
350 int orientation = layer->transform;
351 const bool useVGPipe = (nPipeIndex != (MAX_BYPASS_LAYERS-1));
352 //only last layer should wait for vsync
353 const bool waitForVsync = vsync_wait;
354 const bool isFg = isFG;
355 //Just to differentiate zorders for different layers
356 const int zorder = nPipeIndex;
357
358 ovUI->setSource(info, orientation);
359 ovUI->setCrop(crop.left, crop.top, crop_w, crop_h);
360 ovUI->setDisplayParams(fbnum, waitForVsync, isFg, zorder, useVGPipe);
361 ovUI->setPosition(dst.left, dst.top, dst_w, dst_h);
362
363 LOGE_IF(BYPASS_DEBUG,"%s: Bypass set: crop[%d,%d,%d,%d] dst[%d,%d,%d,%d] waitforVsync: %d \
364 isFg: %d zorder: %d VG = %d nPipe: %d",__FUNCTION__,
365 crop.left, crop.top, crop_w, crop_h,
366 dst.left, dst.top, dst_w, dst_h,
367 waitForVsync, isFg, zorder, useVGPipe, nPipeIndex );
368
369 if(ovUI->commit() != overlay::NO_ERROR) {
370 LOGE("%s: Overlay Commit failed", __FUNCTION__);
371 return -1;
372 }
373 }
374 return 0;
375}
376
377/*
378 * Checks if doing comp. bypass is possible.
379 * It is possible if
380 * 1. No MDP pipe is used
381 * 2. Rotation is not needed
382 * 3. We have atmost MAX_BYPASS_LAYERS
383 */
384inline static bool isBypassDoable(hwc_composer_device_t *dev, const int yuvCount,
385 const hwc_layer_list_t* list) {
386 hwc_context_t* ctx = (hwc_context_t*)(dev);
387 private_hwc_module_t* hwcModule = reinterpret_cast<private_hwc_module_t*>(
388 dev->common.module);
389 //Check if enabled in build.prop
390 if(hwcModule->isBypassEnabled == false) {
391 return false;
392 }
393
394 if(list->numHwLayers < 1) {
395 return false;
396 }
397
398#if defined HDMI_DUAL_DISPLAY
399 //Disable bypass when HDMI is enabled
400 if(ctx->mHDMIEnabled || ctx->pendingHDMI) {
401 return false;
402 }
403#endif
404
405 if(ExtDispOnly::isModeOn()) {
406 return false;
407 }
408
409 //Bypass is not efficient if rotation or asynchronous mode is needed.
410 for(int i = 0; i < list->numHwLayers; ++i) {
411 if(list->hwLayers[i].transform) {
412 return false;
413 }
414 if(list->hwLayers[i].flags & HWC_LAYER_ASYNCHRONOUS) {
415 return false;
416 }
417 }
418
419 return (yuvCount == 0) && (ctx->hwcOverlayStatus == HWC_OVERLAY_CLOSED)
420 && (list->numHwLayers <= MAX_BYPASS_LAYERS);
421}
422
423void setBypassLayerFlags(hwc_context_t* ctx, hwc_layer_list_t* list)
424{
425 for(int index = 0 ; index < MAX_BYPASS_LAYERS; index++ )
426 {
427 int layer_index = ctx->layerindex[index];
428 if(layer_index >= 0) {
429 hwc_layer_t* layer = &(list->hwLayers[layer_index]);
430
431 layer->flags |= HWC_COMP_BYPASS;
432 layer->compositionType = HWC_USE_OVERLAY;
433 layer->hints |= HWC_HINT_CLEAR_FB;
434 }
435 }
436
437 if( list->numHwLayers > ctx->nPipesUsed ) {
438 list->flags &= ~HWC_SKIP_COMPOSITION; //Compose to FB
439 } else {
440 list->flags |= HWC_SKIP_COMPOSITION; // Dont
441 }
442}
443
444bool setupBypass(hwc_context_t* ctx, hwc_layer_list_t* list) {
445 int nPipeIndex, vsync_wait, isFG;
446 int numHwLayers = list->numHwLayers;
447 int nPipeAvailable = MAX_BYPASS_LAYERS;
448
449 for (int index = 0 ; (index < numHwLayers) && nPipeAvailable; index++) {
450
451 hwc_layer_t* layer = &(list->hwLayers[index]);
452
453 nPipeIndex = MAX_BYPASS_LAYERS - nPipeAvailable;
454 //Set VSYNC wait is needed only for the last pipe queued
455 vsync_wait = (nPipeIndex == (numHwLayers-1));
456 //Set isFG to true for layer with z-order zero
457 isFG = !index;
458
459 //Clear Bypass flags for the layer
460 layer->flags &= ~HWC_COMP_BYPASS;
461 layer->flags |= HWC_BYPASS_INDEX_MASK;
462
463 if( prepareBypass(ctx, &(list->hwLayers[index]), nPipeIndex, vsync_wait, isFG) != 0 ) {
464 LOGE_IF(BYPASS_DEBUG, "%s: layer %d failed to configure bypass for pipe index: %d",
465 __FUNCTION__, index, nPipeIndex);
466 return false;
467 } else {
468 ctx->layerindex[nPipeIndex] = index;
469 setLayerbypassIndex(layer, nPipeIndex);
470 nPipeAvailable--;
471 }
472 }
473 ctx->nPipesUsed = MAX_BYPASS_LAYERS - nPipeAvailable;
474 return true;
475}
476
477void unsetBypassLayerFlags(hwc_layer_list_t* list) {
478 if (!list)
479 return;
480
481 for (int index = 0 ; index < list->numHwLayers; index++) {
482 if(list->hwLayers[index].flags & HWC_COMP_BYPASS) {
483 list->hwLayers[index].flags &= ~HWC_COMP_BYPASS;
484 }
485 }
486}
487
488void unsetBypassBufferLockState(hwc_context_t* ctx) {
489 for (int i= 0; i< MAX_BYPASS_LAYERS; i++) {
490 ctx->bypassBufferLockState[i] = BYPASS_BUFFER_UNLOCKED;
491 }
492}
493
494void storeLockedBypassHandle(hwc_layer_list_t* list, hwc_context_t* ctx) {
495 if (!list)
496 return;
497
498 for(int index = 0; index < MAX_BYPASS_LAYERS; index++ ) {
499 hwc_layer_t layer = list->hwLayers[ctx->layerindex[index]];
500
501 if (layer.flags & HWC_COMP_BYPASS) {
502 private_handle_t *hnd = (private_handle_t*)layer.handle;
503
504 if (ctx->bypassBufferLockState[index] == BYPASS_BUFFER_LOCKED) {
505 ctx->previousBypassHandle[index] = (native_handle_t*)layer.handle;
506 hnd->flags |= private_handle_t::PRIV_FLAGS_HWC_LOCK;
507 } else {
508 ctx->previousBypassHandle[index] = NULL;
509 }
510 }
511 }
512}
513
514void closeExtraPipes(hwc_context_t* ctx) {
515
516 int pipes_used = ctx->nPipesUsed;
517
518 //Unused pipes must be of higher z-order
519 for (int i = pipes_used ; i < MAX_BYPASS_LAYERS; i++) {
520 if (ctx->previousBypassHandle[i]) {
521 private_handle_t *hnd = (private_handle_t*) ctx->previousBypassHandle[i];
522
523 if (!private_handle_t::validate(ctx->previousBypassHandle[i])) {
524 if (GENLOCK_FAILURE == genlock_unlock_buffer(ctx->previousBypassHandle[i])) {
525 LOGE("%s: genlock_unlock_buffer failed", __FUNCTION__);
526 } else {
527 ctx->previousBypassHandle[i] = NULL;
528 ctx->bypassBufferLockState[i] = BYPASS_BUFFER_UNLOCKED;
529 hnd->flags &= ~private_handle_t::PRIV_FLAGS_HWC_LOCK;
530 }
531 }
532 }
533 ctx->mOvUI[i]->closeChannel();
534 ctx->layerindex[i] = -1;
535 }
536}
537#endif //COMPOSITION_BYPASS
538
539static int setVideoOverlayStatusInGralloc(hwc_context_t* ctx, const bool enable) {
540#if defined HDMI_DUAL_DISPLAY
541 private_hwc_module_t* hwcModule = reinterpret_cast<private_hwc_module_t*>(
542 ctx->device.common.module);
543 if(!hwcModule) {
544 LOGE("%s: invalid params", __FUNCTION__);
545 return -1;
546 }
547
548 framebuffer_device_t *fbDev = hwcModule->fbDevice;
549 if (!fbDev) {
550 LOGE("%s: fbDev is NULL", __FUNCTION__);
551 return -1;
552 }
553
554 // Inform the gralloc to stop or start UI mirroring
555 fbDev->videoOverlayStarted(fbDev, enable);
556#endif
557 return 0;
558}
559
560static void setHWCOverlayStatus(hwc_context_t *ctx, bool isVideoPresent) {
561
562 switch (ctx->hwcOverlayStatus) {
563 case HWC_OVERLAY_OPEN:
564 ctx->hwcOverlayStatus =
565 isVideoPresent ? HWC_OVERLAY_OPEN : HWC_OVERLAY_PREPARE_TO_CLOSE;
566 break;
567 case HWC_OVERLAY_PREPARE_TO_CLOSE:
568 ctx->hwcOverlayStatus =
569 isVideoPresent ? HWC_OVERLAY_OPEN : HWC_OVERLAY_CLOSED;
570 break;
571 case HWC_OVERLAY_CLOSED:
572 ctx->hwcOverlayStatus =
573 isVideoPresent ? HWC_OVERLAY_OPEN : HWC_OVERLAY_CLOSED;
574 break;
575 default:
576 LOGE("%s: Invalid hwcOverlayStatus (status =%d)", __FUNCTION__,
577 ctx->hwcOverlayStatus);
578 break;
579 }
580}
581
582static int hwc_closeOverlayChannels(hwc_context_t* ctx) {
583#ifdef USE_OVERLAY
584 overlay::Overlay *ovLibObject = ctx->mOverlayLibObject;
585 if(!ovLibObject) {
586 LOGE("%s: invalid params", __FUNCTION__);
587 return -1;
588 }
589
590 if (HWC_OVERLAY_PREPARE_TO_CLOSE == ctx->hwcOverlayStatus) {
591 // Video mirroring is going on, and we do not have any layers to
592 // mirror directly. Close the current video channel and inform the
593 // gralloc to start UI mirroring
594 ovLibObject->closeChannel();
595 // Inform the gralloc that video overlay has stopped.
596 setVideoOverlayStatusInGralloc(ctx, false);
597 }
598#endif
599 return 0;
600}
601
602/*
603 * Configures mdp pipes
604 */
605static int prepareOverlay(hwc_context_t *ctx, hwc_layer_t *layer, const int flags) {
606 int ret = 0;
607
608#ifdef COMPOSITION_BYPASS
609 if(ctx && (ctx->bypassState != BYPASS_OFF)) {
610 ctx->nPipesUsed = 0;
611 closeExtraPipes(ctx);
612 ctx->bypassState = BYPASS_OFF;
613 }
614#endif
615
616 if (LIKELY(ctx && ctx->mOverlayLibObject)) {
617 private_hwc_module_t* hwcModule =
618 reinterpret_cast<private_hwc_module_t*>(ctx->device.common.module);
619 if (UNLIKELY(!hwcModule)) {
620 LOGE("prepareOverlay null module ");
621 return -1;
622 }
623
624 private_handle_t *hnd = (private_handle_t *)layer->handle;
625 overlay::Overlay *ovLibObject = ctx->mOverlayLibObject;
626 overlay_buffer_info info;
627 info.width = hnd->width;
628 info.height = hnd->height;
629 info.format = hnd->format;
630 info.size = hnd->size;
631
632 int hdmiConnected = 0;
633
634#if defined HDMI_DUAL_DISPLAY
635 if(!ctx->pendingHDMI) //makes sure the UI channel is opened first
636 hdmiConnected = (int)ctx->mHDMIEnabled;
637#endif
638 ret = ovLibObject->setSource(info, layer->transform,
639 hdmiConnected, flags);
640 if (!ret) {
641 LOGE("prepareOverlay setSource failed");
642 return -1;
643 }
644
645 ret = ovLibObject->setTransform(layer->transform);
646 if (!ret) {
647 LOGE("prepareOverlay setTransform failed transform %x",
648 layer->transform);
649 return -1;
650 }
651
652 hwc_rect_t sourceCrop = layer->sourceCrop;
653 ret = ovLibObject->setCrop(sourceCrop.left, sourceCrop.top,
654 (sourceCrop.right - sourceCrop.left),
655 (sourceCrop.bottom - sourceCrop.top));
656 if (!ret) {
657 LOGE("prepareOverlay setCrop failed");
658 return -1;
659 }
660#if defined HDMI_DUAL_DISPLAY
661 // Send the device orientation to overlayLib
662 if(hwcModule) {
663 framebuffer_device_t *fbDev = reinterpret_cast<framebuffer_device_t*>
664 (hwcModule->fbDevice);
665 if(fbDev) {
666 private_module_t* m = reinterpret_cast<private_module_t*>(
667 fbDev->common.module);
668 if(m)
669 ovLibObject->setDeviceOrientation(m->orientation);
670 }
671 }
672#endif
673 if (layer->flags & HWC_USE_ORIGINAL_RESOLUTION) {
674 framebuffer_device_t* fbDev = hwcModule->fbDevice;
675 ret = ovLibObject->setPosition(0, 0,
676 fbDev->width, fbDev->height);
677 } else {
678 hwc_rect_t displayFrame = layer->displayFrame;
679 ret = ovLibObject->setPosition(displayFrame.left, displayFrame.top,
680 (displayFrame.right - displayFrame.left),
681 (displayFrame.bottom - displayFrame.top));
682 }
683 if (!ret) {
684 LOGE("prepareOverlay setPosition failed");
685 return -1;
686 }
687 }
688 return 0;
689}
690
691void unlockPreviousOverlayBuffer(hwc_context_t* ctx)
692{
693 if (ctx->previousOverlayHandle) {
694 // Validate the handle before attempting to use it.
695 if (!private_handle_t::validate(ctx->previousOverlayHandle)) {
696 private_handle_t *hnd = (private_handle_t*)ctx->previousOverlayHandle;
697 // Unlock any previously locked buffers
698 if (private_handle_t::PRIV_FLAGS_HWC_LOCK & hnd->flags) {
699 if (GENLOCK_NO_ERROR == genlock_unlock_buffer(ctx->previousOverlayHandle)) {
700 ctx->previousOverlayHandle = NULL;
701 hnd->flags &= ~private_handle_t::PRIV_FLAGS_HWC_LOCK;
702 } else {
703 LOGE("%s: genlock_unlock_buffer failed", __FUNCTION__);
704 }
705 }
706 }
707 }
708}
709
710bool canSkipComposition(hwc_context_t* ctx, int yuvBufferCount, int currentLayerCount,
711 int numLayersNotUpdating)
712{
713 if (!ctx) {
714 LOGE("%s: invalid context",__FUNCTION__);
715 return false;
716 }
717
718 hwc_composer_device_t* dev = (hwc_composer_device_t *)(ctx);
719 private_hwc_module_t* hwcModule = reinterpret_cast<private_hwc_module_t*>(
720 dev->common.module);
721 if (hwcModule->compositionType == COMPOSITION_TYPE_CPU)
722 return false;
723
724 //Video / Camera case
725 if (yuvBufferCount == 1) {
726 //If the previousLayerCount is anything other than the current count, it
727 //means something changed and we need to compose atleast once to FB.
728 if (currentLayerCount != ctx->previousLayerCount) {
729 ctx->previousLayerCount = currentLayerCount;
730 return false;
731 }
732 // We either have only one overlay layer or we have
733 // all non-updating UI layers.
734 // We can skip the composition of the UI layers.
735 if ((currentLayerCount == 1) ||
736 ((currentLayerCount - 1) == numLayersNotUpdating)) {
737 return true;
738 }
739 } else {
740 ctx->previousLayerCount = -1;
741 }
742 return false;
743}
744
745inline void getLayerResolution(const hwc_layer_t* layer, int& width, int& height)
746{
747 hwc_rect_t displayFrame = layer->displayFrame;
748
749 width = displayFrame.right - displayFrame.left;
750 height = displayFrame.bottom - displayFrame.top;
751}
752
753static bool canUseCopybit(const framebuffer_device_t* fbDev, const hwc_layer_list_t* list) {
754
755 if(!fbDev) {
756 LOGE("ERROR: %s : fb device is invalid",__func__);
757 return false;
758 }
759
760 if (!list)
761 return false;
762
763 int fb_w = fbDev->width;
764 int fb_h = fbDev->height;
765
766 /*
767 * Use copybit only when we need to blit
768 * max 2 full screen sized regions
769 */
770
771 unsigned int renderArea = 0;
772
773 for(int i = 0; i < list->numHwLayers; i++ ) {
774 int w, h;
775 getLayerResolution(&list->hwLayers[i], w, h);
776 renderArea += w*h;
777 }
778
779 return (renderArea <= (2 * fb_w * fb_h));
780}
781
782static void handleHDMIStateChange(hwc_composer_device_t *dev, int externaltype) {
783#if defined HDMI_DUAL_DISPLAY
784 hwc_context_t* ctx = (hwc_context_t*)(dev);
785 private_hwc_module_t* hwcModule = reinterpret_cast<private_hwc_module_t*>(
786 dev->common.module);
787 //Route the event to fbdev only if we are in default mirror mode
788 if(ExtDispOnly::isModeOn() == false) {
789 framebuffer_device_t *fbDev = hwcModule->fbDevice;
790 if (fbDev) {
791 fbDev->enableHDMIOutput(fbDev, externaltype);
792 }
793
794 if(ctx && ctx->mOverlayLibObject) {
795 overlay::Overlay *ovLibObject = ctx->mOverlayLibObject;
796 if (!externaltype) {
797 // Close the external overlay channels if HDMI is disconnected
798 ovLibObject->closeExternalChannel();
799 }
800 }
801 }
802#endif
803}
804
805/*
806 * function to set the status of external display in hwc
807 * Just mark flags and do stuff after eglSwapBuffers
808 * externaltype - can be HDMI, WIFI or OFF
809 */
810static void hwc_enableHDMIOutput(hwc_composer_device_t *dev, int externaltype) {
811#if defined HDMI_DUAL_DISPLAY
812 hwc_context_t* ctx = (hwc_context_t*)(dev);
813 private_hwc_module_t* hwcModule = reinterpret_cast<private_hwc_module_t*>(
814 dev->common.module);
815 framebuffer_device_t *fbDev = hwcModule->fbDevice;
816 overlay::Overlay *ovLibObject = ctx->mOverlayLibObject;
817 if(externaltype && ctx->mHDMIEnabled &&
818 (externaltype != ctx->mHDMIEnabled)) {
819 // Close the current external display - as the SF will
820 // prioritize and send the correct external display HDMI/WFD
821 handleHDMIStateChange(dev, 0);
822 }
823 // Store the external display
824 ctx->mHDMIEnabled = (external_display)externaltype;
825 if(ctx->mHDMIEnabled) { //On connect, allow bypass to draw once to FB
826 ctx->pendingHDMI = true;
827 } else { //On disconnect, close immediately (there will be no bypass)
828 handleHDMIStateChange(dev, ctx->mHDMIEnabled);
829 }
830#endif
831}
832
833static bool isValidDestination(const framebuffer_device_t* fbDev, const hwc_rect_t& rect)
834{
835 if (!fbDev) {
836 LOGE("%s: fbDev is null", __FUNCTION__);
837 return false;
838 }
839
840 int dest_width = (rect.right - rect.left);
841 int dest_height = (rect.bottom - rect.top);
842
843 if (rect.left < 0 || rect.right < 0 || rect.top < 0 || rect.bottom < 0
844 || dest_width <= 0 || dest_height <= 0) {
845 LOGE("%s: destination: left=%d right=%d top=%d bottom=%d width=%d"
846 "height=%d", __FUNCTION__, rect.left, rect.right, rect.top,
847 rect.bottom, dest_width, dest_height);
848 return false;
849 }
850
851 if ((rect.left+dest_width) > fbDev->width || (rect.top+dest_height) > fbDev->height) {
852 LOGE("%s: destination out of bound params", __FUNCTION__);
853 return false;
854 }
855
856 return true;
857}
858
859static int getYUVBufferCount (const hwc_layer_list_t* list) {
860 int yuvBufferCount = 0;
861 if (list) {
862 for (size_t i=0 ; i<list->numHwLayers; i++) {
863 private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;
864 if (hnd && (hnd->bufferType == BUFFER_TYPE_VIDEO) &&
865 !(list->hwLayers[i].flags & HWC_DO_NOT_USE_OVERLAY)) {
866 yuvBufferCount++;
867 if (yuvBufferCount > 1) {
868 break;
869 }
870 }
871 }
872 }
873 return yuvBufferCount;
874}
875
876static int getS3DVideoFormat (const hwc_layer_list_t* list) {
877 int s3dFormat = 0;
878 if (list) {
879 for (size_t i=0; i<list->numHwLayers; i++) {
880 private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;
881 if (hnd && (hnd->bufferType == BUFFER_TYPE_VIDEO))
882 s3dFormat = FORMAT_3D_INPUT(hnd->format);
883 if (s3dFormat)
884 break;
885 }
886 }
887 return s3dFormat;
888}
889
890static int getS3DFormat (const hwc_layer_list_t* list) {
891 int s3dFormat = 0;
892 if (list) {
893 for (size_t i=0; i<list->numHwLayers; i++) {
894 private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;
895 if (hnd)
896 s3dFormat = FORMAT_3D_INPUT(hnd->format);
897 if (s3dFormat)
898 break;
899 }
900 }
901 return s3dFormat;
902}
903
904
905static int getLayerS3DFormat (hwc_layer_t &layer) {
906 int s3dFormat = 0;
907 private_handle_t *hnd = (private_handle_t *)layer.handle;
908 if (hnd)
909 s3dFormat = FORMAT_3D_INPUT(hnd->format);
910 return s3dFormat;
911}
912static bool isS3DCompositionRequired() {
913#ifdef HDMI_AS_PRIMARY
914 return overlay::is3DTV();
915#endif
916 return false;
917}
918
919static void markUILayerForS3DComposition (hwc_layer_t &layer, int s3dVideoFormat) {
920#ifdef HDMI_AS_PRIMARY
921 layer.compositionType = HWC_FRAMEBUFFER;
922 switch(s3dVideoFormat) {
923 case HAL_3D_IN_SIDE_BY_SIDE_L_R:
924 case HAL_3D_IN_SIDE_BY_SIDE_R_L:
925 layer.hints |= HWC_HINT_DRAW_S3D_SIDE_BY_SIDE;
926 break;
927 case HAL_3D_IN_TOP_BOTTOM:
928 layer.hints |= HWC_HINT_DRAW_S3D_TOP_BOTTOM;
929 break;
930 default:
931 LOGE("%s: Unknown S3D input format 0x%x", __FUNCTION__, s3dVideoFormat);
932 break;
933 }
934#endif
935 return;
936}
937
938static int getLayersNotUpdatingCount(const hwc_layer_list_t* list) {
939 int numLayersNotUpdating = 0;
940 if (list) {
941 for (size_t i=0 ; i<list->numHwLayers; i++) {
942 private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;
943 if (hnd && (hnd->bufferType != BUFFER_TYPE_VIDEO) &&
944 list->hwLayers[i].flags & HWC_LAYER_NOT_UPDATING)
945 numLayersNotUpdating++;
946 }
947 }
948 return numLayersNotUpdating;
949}
950
951static int hwc_prepare(hwc_composer_device_t *dev, hwc_layer_list_t* list) {
952
953 hwc_context_t* ctx = (hwc_context_t*)(dev);
954
955 if(!ctx) {
956 LOGE("hwc_prepare invalid context");
957 return -1;
958 }
959
960 private_hwc_module_t* hwcModule = reinterpret_cast<private_hwc_module_t*>(
961 dev->common.module);
962 if (!hwcModule) {
963 LOGE("hwc_prepare invalid module");
964#ifdef COMPOSITION_BYPASS
965 unlockPreviousBypassBuffers(ctx);
966 unsetBypassBufferLockState(ctx);
967#endif
968 unlockPreviousOverlayBuffer(ctx);
969 ExtDispOnly::close();
970 return -1;
971 }
972
973 int yuvBufferCount = 0;
974 int layerType = 0;
975 bool isS3DCompositionNeeded = false;
976 int s3dVideoFormat = 0;
977 int numLayersNotUpdating = 0;
978 bool useCopybit = false;
979 bool isSkipLayerPresent = false;
980 bool skipComposition = false;
981
982 if (list) {
983 useCopybit = canUseCopybit(hwcModule->fbDevice, list);
984 yuvBufferCount = getYUVBufferCount(list);
985 numLayersNotUpdating = getLayersNotUpdatingCount(list);
986 skipComposition = canSkipComposition(ctx, yuvBufferCount,
987 list->numHwLayers, numLayersNotUpdating);
988
989 if (yuvBufferCount == 1) {
990 s3dVideoFormat = getS3DVideoFormat(list);
991 if (s3dVideoFormat)
992 isS3DCompositionNeeded = isS3DCompositionRequired();
993 } else if((s3dVideoFormat = getS3DFormat(list))){
994 if (s3dVideoFormat)
995 isS3DCompositionNeeded = isS3DCompositionRequired();
996 } else {
997 unlockPreviousOverlayBuffer(ctx);
998 }
999
1000 if (list->flags & HWC_GEOMETRY_CHANGED) {
1001 if (yuvBufferCount == 1) {
1002 // Inform the gralloc of the current video overlay status
1003 setVideoOverlayStatusInGralloc(ctx, true);
1004 }
1005 }
1006
1007 for (size_t i=0 ; i<list->numHwLayers ; i++) {
1008 private_handle_t *hnd = (private_handle_t *)list->hwLayers[i].handle;
1009
1010 // If there is a single Fullscreen layer, we can bypass it - TBD
1011 // If there is only one video/camera buffer, we can bypass itn
1012 if (list->hwLayers[i].flags & HWC_SKIP_LAYER) {
1013 // During the animaton UI layers are marked as SKIP
1014 // need to still mark the layer for S3D composition
1015 isSkipLayerPresent = true;
1016 skipComposition = false;
1017 //Reset count, so that we end up composing once after animation
1018 //is over, in case of overlay.
1019 ctx->previousLayerCount = -1;
1020
1021 if (isS3DCompositionNeeded)
1022 markUILayerForS3DComposition(list->hwLayers[i], s3dVideoFormat);
1023
1024// LGE_CHANGE_E, [G1_Player][bokyung.kim@lge.com], 20120201, Apply SR 00744210 to fix screen flicker {
1025 ssize_t layer_countdown = ((ssize_t)i) - 1;
1026 // Mark every layer below the SKIP layer to be composed by the GPU
1027 while (layer_countdown >= 0)
1028 {
1029 private_handle_t *countdown_handle =
1030 (private_handle_t *)list->hwLayers[layer_countdown].handle;
1031 if (countdown_handle && (countdown_handle->bufferType == BUFFER_TYPE_VIDEO)
1032 && (yuvBufferCount == 1)) {
1033 unlockPreviousOverlayBuffer(ctx);
1034 }
1035 list->hwLayers[layer_countdown].compositionType = HWC_FRAMEBUFFER;
1036 list->hwLayers[layer_countdown].hints &= ~HWC_HINT_CLEAR_FB;
1037 layer_countdown--;
1038 }
1039// LGE_CHANGE_E, [G1_Player][bokyung.kim@lge.com], 20120201, Apply SR 00744210 to fix screen flicker }
1040 continue;
1041 }
1042 if (hnd && (hnd->bufferType == BUFFER_TYPE_VIDEO) && (yuvBufferCount == 1)) {
1043 int flags = WAIT_FOR_VSYNC;
1044 flags |= (hnd->flags &
1045 private_handle_t::PRIV_FLAGS_SECURE_BUFFER)?
1046 SECURE_OVERLAY_SESSION : 0;
1047 flags |= (1 == list->numHwLayers) ? DISABLE_FRAMEBUFFER_FETCH : 0;
1048 if (!isValidDestination(hwcModule->fbDevice, list->hwLayers[i].displayFrame)) {
1049 list->hwLayers[i].compositionType = HWC_FRAMEBUFFER;
1050 //Even though there are no skip layers, animation is still
1051 //ON and in its final stages.
1052 //Reset count, so that we end up composing once after animation
1053 //is done, if overlay is used.
1054 ctx->previousLayerCount = -1;
1055 skipComposition = false;
1056#ifdef USE_OVERLAY
1057 } else if(prepareOverlay(ctx, &(list->hwLayers[i]), flags) == 0) {
1058 list->hwLayers[i].compositionType = HWC_USE_OVERLAY;
1059 list->hwLayers[i].hints |= HWC_HINT_CLEAR_FB;
1060 // We've opened the channel. Set the state to open.
1061 ctx->hwcOverlayStatus = HWC_OVERLAY_OPEN;
1062#endif
1063 } else if (hwcModule->compositionType & (COMPOSITION_TYPE_C2D|
1064 COMPOSITION_TYPE_MDP)) {
1065 //Fail safe path: If drawing with overlay fails,
1066
1067 //Use C2D if available.
1068 list->hwLayers[i].compositionType = HWC_USE_COPYBIT;
1069 } else {
1070 //If C2D is not enabled fall back to GPU.
1071 list->hwLayers[i].compositionType = HWC_FRAMEBUFFER;
1072 }
1073 if (HWC_USE_OVERLAY != list->hwLayers[i].compositionType) {
1074 unlockPreviousOverlayBuffer(ctx);
1075 skipComposition = false;
1076 }
1077 } else if (getLayerS3DFormat(list->hwLayers[i])) {
1078 int flags = WAIT_FOR_VSYNC;
1079 flags |= (1 == list->numHwLayers) ? DISABLE_FRAMEBUFFER_FETCH : 0;
1080 flags |= (hnd->flags &
1081 private_handle_t::PRIV_FLAGS_SECURE_BUFFER)?
1082 SECURE_OVERLAY_SESSION : 0;
1083#ifdef USE_OVERLAY
1084 if(prepareOverlay(ctx, &(list->hwLayers[i]), flags) == 0) {
1085 list->hwLayers[i].compositionType = HWC_USE_OVERLAY;
1086 list->hwLayers[i].hints |= HWC_HINT_CLEAR_FB;
1087 // We've opened the channel. Set the state to open.
1088 ctx->hwcOverlayStatus = HWC_OVERLAY_OPEN;
1089 }
1090#endif
1091 } else if (isS3DCompositionNeeded) {
1092 markUILayerForS3DComposition(list->hwLayers[i], s3dVideoFormat);
1093 } else if (list->hwLayers[i].flags & HWC_USE_ORIGINAL_RESOLUTION) {
1094 list->hwLayers[i].compositionType = HWC_USE_OVERLAY;
1095 list->hwLayers[i].hints |= HWC_HINT_CLEAR_FB;
1096 layerType |= HWC_ORIG_RESOLUTION;
1097 } else if (hnd && hnd->flags & private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY) {
1098 //handle later after other layers are handled
1099 } else if (hnd && (hwcModule->compositionType &
1100 (COMPOSITION_TYPE_C2D|COMPOSITION_TYPE_MDP))) {
1101 list->hwLayers[i].compositionType = HWC_USE_COPYBIT;
1102 } else if ((hwcModule->compositionType == COMPOSITION_TYPE_DYN)
1103 && useCopybit) {
1104 list->hwLayers[i].compositionType = HWC_USE_COPYBIT;
1105 }
1106 else {
1107 list->hwLayers[i].compositionType = HWC_FRAMEBUFFER;
1108 }
1109 }
1110
1111 //Update the stats and pipe config for external-only layers
1112 ExtDispOnly::update(ctx, list);
1113
1114 if (skipComposition) {
1115 list->flags |= HWC_SKIP_COMPOSITION;
1116 } else {
1117 list->flags &= ~HWC_SKIP_COMPOSITION;
1118 }
1119
1120#ifdef COMPOSITION_BYPASS
1121 bool isBypassUsed = true;
1122 bool isDoable = isBypassDoable(dev, yuvBufferCount, list);
1123 //Check if bypass is feasible
1124 if(isDoable && !isSkipLayerPresent) {
1125 if(setupBypass(ctx, list)) {
1126 setBypassLayerFlags(ctx, list);
1127 ctx->bypassState = BYPASS_ON;
1128 } else {
1129 LOGE_IF(BYPASS_DEBUG,"%s: Bypass setup Failed",__FUNCTION__);
1130 isBypassUsed = false;
1131 }
1132 } else {
1133 LOGE_IF(BYPASS_DEBUG,"%s: Bypass not possible[%d,%d]",__FUNCTION__,
1134 isDoable, !isSkipLayerPresent );
1135 isBypassUsed = false;
1136 }
1137
1138 //Reset bypass states
1139 if(!isBypassUsed) {
1140 ctx->nPipesUsed = 0;
1141 unsetBypassLayerFlags(list);
1142 if(ctx->bypassState == BYPASS_ON) {
1143 ctx->bypassState = BYPASS_OFF_PENDING;
1144 }
1145 }
1146#endif
1147 } else {
1148#ifdef COMPOSITION_BYPASS
1149 unlockPreviousBypassBuffers(ctx);
1150 unsetBypassBufferLockState(ctx);
1151#endif
1152 unlockPreviousOverlayBuffer(ctx);
1153 }
1154 return 0;
1155}
1156// ---------------------------------------------------------------------------
1157struct range {
1158 int current;
1159 int end;
1160};
1161struct region_iterator : public copybit_region_t {
1162
1163 region_iterator(hwc_region_t region) {
1164 mRegion = region;
1165 r.end = region.numRects;
1166 r.current = 0;
1167 this->next = iterate;
1168 }
1169
1170private:
1171 static int iterate(copybit_region_t const * self, copybit_rect_t* rect) {
1172 if (!self || !rect) {
1173 LOGE("iterate invalid parameters");
1174 return 0;
1175 }
1176
1177 region_iterator const* me = static_cast<region_iterator const*>(self);
1178 if (me->r.current != me->r.end) {
1179 rect->l = me->mRegion.rects[me->r.current].left;
1180 rect->t = me->mRegion.rects[me->r.current].top;
1181 rect->r = me->mRegion.rects[me->r.current].right;
1182 rect->b = me->mRegion.rects[me->r.current].bottom;
1183 me->r.current++;
1184 return 1;
1185 }
1186 return 0;
1187 }
1188
1189 hwc_region_t mRegion;
1190 mutable range r;
1191};
1192
1193static int drawLayerUsingCopybit(hwc_composer_device_t *dev, hwc_layer_t *layer, EGLDisplay dpy,
1194 EGLSurface surface)
1195{
1196 hwc_context_t* ctx = (hwc_context_t*)(dev);
1197 if(!ctx) {
1198 LOGE("drawLayerUsingCopybit null context ");
1199 return -1;
1200 }
1201
1202 private_hwc_module_t* hwcModule = reinterpret_cast<private_hwc_module_t*>(dev->common.module);
1203 if(!hwcModule) {
1204 LOGE("drawLayerUsingCopybit null module ");
1205 return -1;
1206 }
1207
1208 private_handle_t *hnd = (private_handle_t *)layer->handle;
1209 if(!hnd) {
1210 LOGE("drawLayerUsingCopybit invalid handle");
1211 return -1;
1212 }
1213
1214 // Lock this buffer for read.
1215 genlock_lock_type lockType = GENLOCK_READ_LOCK;
1216 int err = genlock_lock_buffer(hnd, lockType, GENLOCK_MAX_TIMEOUT);
1217 if (GENLOCK_FAILURE == err) {
1218 LOGE("%s: genlock_lock_buffer(READ) failed", __FUNCTION__);
1219 return -1;
1220 }
1221 //render buffer
1222 android_native_buffer_t *renderBuffer = (android_native_buffer_t *)eglGetRenderBufferANDROID(dpy, surface);
1223 if (!renderBuffer) {
1224 LOGE("eglGetRenderBufferANDROID returned NULL buffer");
1225 genlock_unlock_buffer(hnd);
1226 return -1;
1227 }
1228 private_handle_t *fbHandle = (private_handle_t *)renderBuffer->handle;
1229 if(!fbHandle) {
1230 LOGE("Framebuffer handle is NULL");
1231 genlock_unlock_buffer(hnd);
1232 return -1;
1233 }
1234 int alignment = 32;
1235 if( HAL_PIXEL_FORMAT_RGB_565 == fbHandle->format )
1236 alignment = 16;
1237 // Set the copybit source:
1238 copybit_image_t src;
1239 src.w = ALIGN(hnd->width, alignment);
1240 src.h = hnd->height;
1241 src.format = hnd->format;
1242 src.base = (void *)hnd->base;
1243 src.handle = (native_handle_t *)layer->handle;
1244 src.horiz_padding = src.w - hnd->width;
1245 // Initialize vertical padding to zero for now,
1246 // this needs to change to accomodate vertical stride
1247 // if needed in the future
1248 src.vert_padding = 0;
1249
1250 // Copybit source rect
1251 hwc_rect_t sourceCrop = layer->sourceCrop;
1252 copybit_rect_t srcRect = {sourceCrop.left, sourceCrop.top,
1253 sourceCrop.right,
1254 sourceCrop.bottom};
1255
1256 // Copybit destination rect
1257 hwc_rect_t displayFrame = layer->displayFrame;
1258 copybit_rect_t dstRect = {displayFrame.left, displayFrame.top,
1259 displayFrame.right,
1260 displayFrame.bottom};
1261
1262 // Copybit dst
1263 copybit_image_t dst;
1264 dst.w = ALIGN(fbHandle->width,alignment);
1265 dst.h = fbHandle->height;
1266 dst.format = fbHandle->format;
1267 dst.base = (void *)fbHandle->base;
1268 dst.handle = (native_handle_t *)renderBuffer->handle;
1269
1270 copybit_device_t *copybit = hwcModule->copybitEngine;
1271
1272 int32_t screen_w = displayFrame.right - displayFrame.left;
1273 int32_t screen_h = displayFrame.bottom - displayFrame.top;
1274 int32_t src_crop_width = sourceCrop.right - sourceCrop.left;
1275 int32_t src_crop_height = sourceCrop.bottom -sourceCrop.top;
1276
1277 float copybitsMaxScale = (float)copybit->get(copybit,COPYBIT_MAGNIFICATION_LIMIT);
1278 float copybitsMinScale = (float)copybit->get(copybit,COPYBIT_MINIFICATION_LIMIT);
1279
1280 if((layer->transform == HWC_TRANSFORM_ROT_90) ||
1281 (layer->transform == HWC_TRANSFORM_ROT_270)) {
1282 //swap screen width and height
1283 int tmp = screen_w;
1284 screen_w = screen_h;
1285 screen_h = tmp;
1286 }
1287 private_handle_t *tmpHnd = NULL;
1288
1289 if(screen_w <=0 || screen_h<=0 ||src_crop_width<=0 || src_crop_height<=0 ) {
1290 LOGE("%s: wrong params for display screen_w=%d src_crop_width=%d screen_w=%d \
1291 src_crop_width=%d", __FUNCTION__, screen_w,
1292 src_crop_width,screen_w,src_crop_width);
1293 genlock_unlock_buffer(hnd);
1294 return -1;
1295 }
1296
1297 float dsdx = (float)screen_w/src_crop_width;
1298 float dtdy = (float)screen_h/src_crop_height;
1299
1300 float scaleLimitMax = copybitsMaxScale * copybitsMaxScale;
1301 float scaleLimitMin = copybitsMinScale * copybitsMinScale;
1302 if(dsdx > scaleLimitMax || dtdy > scaleLimitMax || dsdx < 1/scaleLimitMin || dtdy < 1/scaleLimitMin) {
1303 LOGE("%s: greater than max supported size dsdx=%f dtdy=%f scaleLimitMax=%f scaleLimitMin=%f", __FUNCTION__,dsdx,dtdy,scaleLimitMax,1/scaleLimitMin);
1304 genlock_unlock_buffer(hnd);
1305 return -1;
1306 }
1307 if(dsdx > copybitsMaxScale || dtdy > copybitsMaxScale || dsdx < 1/copybitsMinScale || dtdy < 1/copybitsMinScale){
1308 // The requested scale is out of the range the hardware
1309 // can support.
1310 LOGD("%s:%d::Need to scale twice dsdx=%f, dtdy=%f,copybitsMaxScale=%f,copybitsMinScale=%f,screen_w=%d,screen_h=%d \
1311 src_crop_width=%d src_crop_height=%d",__FUNCTION__,__LINE__,
1312 dsdx,dtdy,copybitsMaxScale,1/copybitsMinScale,screen_w,screen_h,src_crop_width,src_crop_height);
1313
1314 //Driver makes width and height as even
1315 //that may cause wrong calculation of the ratio
1316 //in display and crop.Hence we make
1317 //crop width and height as even.
1318 src_crop_width = (src_crop_width/2)*2;
1319 src_crop_height = (src_crop_height/2)*2;
1320
1321 int tmp_w = src_crop_width;
1322 int tmp_h = src_crop_height;
1323
1324 if (dsdx > copybitsMaxScale || dtdy > copybitsMaxScale ){
1325 tmp_w = src_crop_width*copybitsMaxScale;
1326 tmp_h = src_crop_height*copybitsMaxScale;
1327 }else if (dsdx < 1/copybitsMinScale ||dtdy < 1/copybitsMinScale ){
1328 tmp_w = src_crop_width/copybitsMinScale;
1329 tmp_h = src_crop_height/copybitsMinScale;
1330 tmp_w = (tmp_w/2)*2;
1331 tmp_h = (tmp_h/2)*2;
1332 }
1333 LOGD("%s:%d::tmp_w = %d,tmp_h = %d",__FUNCTION__,__LINE__,tmp_w,tmp_h);
1334
1335 int usage = GRALLOC_USAGE_PRIVATE_ADSP_HEAP |
1336 GRALLOC_USAGE_PRIVATE_MM_HEAP;
1337
1338 if (0 == alloc_buffer(&tmpHnd, tmp_w, tmp_h, fbHandle->format, usage)){
1339 copybit_image_t tmp_dst;
1340 copybit_rect_t tmp_rect;
1341 tmp_dst.w = tmp_w;
1342 tmp_dst.h = tmp_h;
1343 tmp_dst.format = tmpHnd->format;
1344 tmp_dst.handle = tmpHnd;
1345 tmp_dst.horiz_padding = src.horiz_padding;
1346 tmp_dst.vert_padding = src.vert_padding;
1347 tmp_rect.l = 0;
1348 tmp_rect.t = 0;
1349 tmp_rect.r = tmp_dst.w;
1350 tmp_rect.b = tmp_dst.h;
1351 //create one clip region
1352 hwc_rect tmp_hwc_rect = {0,0,tmp_rect.r,tmp_rect.b};
1353 hwc_region_t tmp_hwc_reg = {1,(hwc_rect_t const*)&tmp_hwc_rect};
1354 region_iterator tmp_it(tmp_hwc_reg);
1355 copybit->set_parameter(copybit,COPYBIT_TRANSFORM,0);
1356 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA,
1357 (layer->blending == HWC_BLENDING_NONE) ? -1 : layer->alpha);
1358 err = copybit->stretch(copybit,&tmp_dst, &src, &tmp_rect, &srcRect, &tmp_it);
1359 if(err < 0){
1360 LOGE("%s:%d::tmp copybit stretch failed",__FUNCTION__,__LINE__);
1361 if(tmpHnd)
1362 free_buffer(tmpHnd);
1363 genlock_unlock_buffer(hnd);
1364 return err;
1365 }
1366 // copy new src and src rect crop
1367 src = tmp_dst;
1368 srcRect = tmp_rect;
1369 }
1370 }
1371 // Copybit region
1372 hwc_region_t region = layer->visibleRegionScreen;
1373 region_iterator copybitRegion(region);
1374
1375 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_WIDTH, renderBuffer->width);
1376 copybit->set_parameter(copybit, COPYBIT_FRAMEBUFFER_HEIGHT, renderBuffer->height);
1377 copybit->set_parameter(copybit, COPYBIT_TRANSFORM, layer->transform);
1378 copybit->set_parameter(copybit, COPYBIT_PLANE_ALPHA,
1379 (layer->blending == HWC_BLENDING_NONE) ? -1 : layer->alpha);
1380 copybit->set_parameter(copybit, COPYBIT_PREMULTIPLIED_ALPHA,
1381 (layer->blending == HWC_BLENDING_PREMULT)? COPYBIT_ENABLE : COPYBIT_DISABLE);
1382 copybit->set_parameter(copybit, COPYBIT_DITHER,
1383 (dst.format == HAL_PIXEL_FORMAT_RGB_565)? COPYBIT_ENABLE : COPYBIT_DISABLE);
1384 err = copybit->stretch(copybit, &dst, &src, &dstRect, &srcRect, &copybitRegion);
1385
1386 if(tmpHnd)
1387 free_buffer(tmpHnd);
1388
1389 if(err < 0)
1390 LOGE("%s: copybit stretch failed",__FUNCTION__);
1391
1392 // Unlock this buffer since copybit is done with it.
1393 err = genlock_unlock_buffer(hnd);
1394 if (GENLOCK_FAILURE == err) {
1395 LOGE("%s: genlock_unlock_buffer failed", __FUNCTION__);
1396 }
1397
1398 return err;
1399}
1400
1401static int drawLayerUsingOverlay(hwc_context_t *ctx, hwc_layer_t *layer)
1402{
1403 if (ctx && ctx->mOverlayLibObject) {
1404 private_hwc_module_t* hwcModule = reinterpret_cast<private_hwc_module_t*>(ctx->device.common.module);
1405 if (!hwcModule) {
1406 LOGE("drawLayerUsingLayer null module ");
1407 return -1;
1408 }
1409 private_handle_t *hnd = (private_handle_t *)layer->handle;
1410 overlay::Overlay *ovLibObject = ctx->mOverlayLibObject;
1411 int ret = 0;
1412
1413 // Lock this buffer for read.
1414 if (GENLOCK_NO_ERROR != genlock_lock_buffer(hnd, GENLOCK_READ_LOCK,
1415 GENLOCK_MAX_TIMEOUT)) {
1416 LOGE("%s: genlock_lock_buffer(READ) failed", __FUNCTION__);
1417 return -1;
1418 }
1419
1420 ret = ovLibObject->queueBuffer(hnd);
1421
1422 // Unlock the previously locked buffer, since the overlay has completed reading the buffer
1423 unlockPreviousOverlayBuffer(ctx);
1424
1425 if (!ret) {
1426 LOGE("drawLayerUsingOverlay queueBuffer failed");
1427 // Unlock the buffer handle
1428 genlock_unlock_buffer(hnd);
1429 ctx->previousOverlayHandle = NULL;
1430 } else {
1431 // Store the current buffer handle as the one that is to be unlocked after
1432 // the next overlay play call.
1433 ctx->previousOverlayHandle = hnd;
1434 hnd->flags |= private_handle_t::PRIV_FLAGS_HWC_LOCK;
1435 }
1436
1437 return ret;
1438 }
1439 return -1;
1440}
1441
1442#ifdef COMPOSITION_BYPASS
1443static int drawLayerUsingBypass(hwc_context_t *ctx, hwc_layer_t *layer, int layer_index) {
1444
1445 int index = getLayerbypassIndex(layer);
1446
1447 if(index < 0) {
1448 LOGE("%s: Invalid bypass index (%d)", __FUNCTION__, index);
1449 return -1;
1450 }
1451
1452 if (ctx && ctx->mOvUI[index]) {
1453 overlay::OverlayUI *ovUI = ctx->mOvUI[index];
1454 int ret = 0;
1455
1456 private_handle_t *hnd = (private_handle_t *)layer->handle;
1457 if(!hnd) {
1458 LOGE("%s handle null", __FUNCTION__);
1459 return -1;
1460 }
1461
1462 ctx->bypassBufferLockState[index] = BYPASS_BUFFER_UNLOCKED;
1463
1464 if (GENLOCK_FAILURE == genlock_lock_buffer(hnd, GENLOCK_READ_LOCK,
1465 GENLOCK_MAX_TIMEOUT)) {
1466 LOGE("%s: genlock_lock_buffer(READ) failed", __FUNCTION__);
1467 return -1;
1468 }
1469
1470 ctx->bypassBufferLockState[index] = BYPASS_BUFFER_LOCKED;
1471
1472 LOGE_IF(BYPASS_DEBUG,"%s: Bypassing layer: %p using pipe: %d",__FUNCTION__, layer, index );
1473
1474 ret = ovUI->queueBuffer(hnd);
1475
1476 if (ret) {
1477 // Unlock the locked buffer
1478 if (GENLOCK_FAILURE == genlock_unlock_buffer(hnd)) {
1479 LOGE("%s: genlock_unlock_buffer failed", __FUNCTION__);
1480 }
1481 ctx->bypassBufferLockState[index] = BYPASS_BUFFER_UNLOCKED;
1482 return -1;
1483 }
1484 }
1485 return 0;
1486}
1487#endif
1488
1489static int hwc_set(hwc_composer_device_t *dev,
1490 hwc_display_t dpy,
1491 hwc_surface_t sur,
1492 hwc_layer_list_t* list)
1493{
1494 hwc_context_t* ctx = (hwc_context_t*)(dev);
1495 if(!ctx) {
1496 LOGE("hwc_set invalid context");
1497 ExtDispOnly::close();
1498 return -1;
1499 }
1500
1501 private_hwc_module_t* hwcModule = reinterpret_cast<private_hwc_module_t*>(
1502 dev->common.module);
1503 if (!hwcModule) {
1504 LOGE("hwc_set invalid module");
1505#ifdef COMPOSITION_BYPASS
1506 unlockPreviousBypassBuffers(ctx);
1507 unsetBypassBufferLockState(ctx);
1508#endif
1509 ExtDispOnly::close();
1510 unlockPreviousOverlayBuffer(ctx);
1511 return -1;
1512 }
1513
1514 int ret = 0;
1515 if (list) {
1516 bool bDumpLayers = needToDumpLayers(); // Check need for debugging dumps
1517 for (size_t i=0; i<list->numHwLayers; i++) {
1518 if (bDumpLayers)
1519 dumpLayer(hwcModule->compositionType, list->flags, i, list->hwLayers);
1520 if (list->hwLayers[i].flags & HWC_SKIP_LAYER) {
1521 continue;
1522 } else if(list->hwLayers[i].flags & HWC_USE_EXT_ONLY) {
1523 continue;
1524 //Draw after layers for primary are drawn
1525#ifdef COMPOSITION_BYPASS
1526 } else if (list->hwLayers[i].flags & HWC_COMP_BYPASS) {
1527 drawLayerUsingBypass(ctx, &(list->hwLayers[i]), i);
1528#endif
1529 } else if (list->hwLayers[i].compositionType == HWC_USE_OVERLAY) {
1530 drawLayerUsingOverlay(ctx, &(list->hwLayers[i]));
1531 } else if (list->flags & HWC_SKIP_COMPOSITION) {
1532// LGE_CHANGE_S, [G1_Player][mukyung.jung@lge.com], 20120206, Apply SR 00718706 to fix noise of QCIF contents {
1533 //break;
1534 continue;
1535// LGE_CHANGE_E, [G1_Player][mukyung.jung@lge.com], 20120206, Apply SR 00718706 to fix noise of QCIF contents }
1536 } else if (list->hwLayers[i].compositionType == HWC_USE_COPYBIT) {
1537 drawLayerUsingCopybit(dev, &(list->hwLayers[i]), (EGLDisplay)dpy, (EGLSurface)sur);
1538 }
1539 }
1540 } else {
1541 //Device in suspended state. Close all the MDP pipes
1542#ifdef COMPOSITION_BYPASS
1543 ctx->nPipesUsed = 0;
1544#endif
1545 ctx->hwcOverlayStatus = HWC_OVERLAY_PREPARE_TO_CLOSE;
1546 }
1547
1548 bool canSkipComposition = list && list->flags & HWC_SKIP_COMPOSITION;
1549 //Draw External-only layers
1550 if(ExtDispOnly::draw(ctx, list) != overlay::NO_ERROR) {
1551 ExtDispOnly::close();
1552 }
1553
1554#ifdef COMPOSITION_BYPASS
1555 unlockPreviousBypassBuffers(ctx);
1556 storeLockedBypassHandle(list, ctx);
1557 // We have stored the handles, unset the current lock states in the context.
1558 unsetBypassBufferLockState(ctx);
1559 closeExtraPipes(ctx);
1560#if BYPASS_DEBUG
1561 if(canSkipComposition)
1562 LOGE("%s: skipping eglSwapBuffer call", __FUNCTION__);
1563#endif
1564#endif
1565 // Do not call eglSwapBuffers if we the skip composition flag is set on the list.
1566 if (dpy && sur && !canSkipComposition) {
1567 EGLBoolean sucess = eglSwapBuffers((EGLDisplay)dpy, (EGLSurface)sur);
1568 if (!sucess) {
1569 ret = HWC_EGL_ERROR;
1570 } else {
1571 CALC_FPS();
1572 }
1573 }
1574#if defined HDMI_DUAL_DISPLAY
1575 if(ctx->pendingHDMI) {
1576 handleHDMIStateChange(dev, ctx->mHDMIEnabled);
1577 ctx->pendingHDMI = false;
1578 }
1579#endif
1580
1581 hwc_closeOverlayChannels(ctx);
1582 int yuvBufferCount = getYUVBufferCount(list);
1583 setHWCOverlayStatus(ctx, yuvBufferCount);
1584
1585 return ret;
1586}
1587
1588static int hwc_device_close(struct hw_device_t *dev)
1589{
1590 if(!dev) {
1591 LOGE("hwc_device_close null device pointer");
1592 return -1;
1593 }
1594
1595 struct hwc_context_t* ctx = (struct hwc_context_t*)dev;
1596
1597 private_hwc_module_t* hwcModule = reinterpret_cast<private_hwc_module_t*>(
1598 ctx->device.common.module);
1599 // Close the overlay and copybit modules
1600 if(hwcModule->copybitEngine) {
1601 copybit_close(hwcModule->copybitEngine);
1602 hwcModule->copybitEngine = NULL;
1603 }
1604 if(hwcModule->fbDevice) {
1605 framebuffer_close(hwcModule->fbDevice);
1606 hwcModule->fbDevice = NULL;
1607 }
1608
1609 unlockPreviousOverlayBuffer(ctx);
1610
1611 if (ctx) {
1612 delete ctx->mOverlayLibObject;
1613 ctx->mOverlayLibObject = NULL;
1614#ifdef COMPOSITION_BYPASS
1615 for(int i = 0; i < MAX_BYPASS_LAYERS; i++) {
1616 delete ctx->mOvUI[i];
1617 }
1618 unlockPreviousBypassBuffers(ctx);
1619 unsetBypassBufferLockState(ctx);
1620#endif
1621 ExtDispOnly::close();
1622 ExtDispOnly::destroy();
1623
1624 free(ctx);
1625 }
1626 return 0;
1627}
1628
1629/*****************************************************************************/
1630static int hwc_module_initialize(struct private_hwc_module_t* hwcModule)
1631{
1632
1633 // Open the overlay and copybit modules
1634 hw_module_t const *module;
1635 if (hw_get_module(COPYBIT_HARDWARE_MODULE_ID, &module) == 0) {
1636 copybit_open(module, &(hwcModule->copybitEngine));
1637 }
1638 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
1639 framebuffer_open(module, &(hwcModule->fbDevice));
1640 }
1641
1642 // get the current composition type
1643 char property[PROPERTY_VALUE_MAX];
1644 if (property_get("debug.sf.hw", property, NULL) > 0) {
1645 if(atoi(property) == 0) {
1646 //debug.sf.hw = 0
1647 hwcModule->compositionType = COMPOSITION_TYPE_CPU;
1648 } else { //debug.sf.hw = 1
1649 // Get the composition type
1650 property_get("debug.composition.type", property, NULL);
1651 if (property == NULL) {
1652 hwcModule->compositionType = COMPOSITION_TYPE_GPU;
1653 } else if ((strncmp(property, "mdp", 3)) == 0) {
1654 hwcModule->compositionType = COMPOSITION_TYPE_MDP;
1655 } else if ((strncmp(property, "c2d", 3)) == 0) {
1656 hwcModule->compositionType = COMPOSITION_TYPE_C2D;
1657 } else if ((strncmp(property, "dyn", 3)) == 0) {
1658 hwcModule->compositionType = COMPOSITION_TYPE_DYN;
1659 } else {
1660 hwcModule->compositionType = COMPOSITION_TYPE_GPU;
1661 }
1662
1663 if(!hwcModule->copybitEngine)
1664 hwcModule->compositionType = COMPOSITION_TYPE_GPU;
1665 }
1666 } else { //debug.sf.hw is not set. Use cpu composition
1667 hwcModule->compositionType = COMPOSITION_TYPE_CPU;
1668 }
1669
1670 //Check if composition bypass is enabled
1671 if(property_get("ro.sf.compbypass.enable", property, NULL) > 0) {
1672 if(atoi(property) == 1) {
1673 hwcModule->isBypassEnabled = true;
1674 }
1675 }
1676
1677 CALC_INIT();
1678
1679 return 0;
1680}
1681
1682
1683static int hwc_device_open(const struct hw_module_t* module, const char* name,
1684 struct hw_device_t** device)
1685{
1686 int status = -EINVAL;
1687
1688 if (!strcmp(name, HWC_HARDWARE_COMPOSER)) {
1689 private_hwc_module_t* hwcModule = reinterpret_cast<private_hwc_module_t*>
1690 (const_cast<hw_module_t*>(module));
1691 hwc_module_initialize(hwcModule);
1692 struct hwc_context_t *dev;
1693 dev = (hwc_context_t*)malloc(sizeof(*dev));
1694
1695 /* initialize our state here */
1696 memset(dev, 0, sizeof(*dev));
1697#ifdef USE_OVERLAY
1698 dev->mOverlayLibObject = new overlay::Overlay();
1699 if(overlay::initOverlay() == -1)
1700 LOGE("overlay::initOverlay() ERROR!!");
1701#else
1702 dev->mOverlayLibObject = NULL;
1703#endif
1704#ifdef COMPOSITION_BYPASS
1705 for(int i = 0; i < MAX_BYPASS_LAYERS; i++) {
1706 dev->mOvUI[i] = new overlay::OverlayUI();
1707 dev->previousBypassHandle[i] = NULL;
1708 }
1709 unsetBypassBufferLockState(dev);
1710 dev->bypassState = BYPASS_OFF;
1711#endif
1712 ExtDispOnly::init();
1713#if defined HDMI_DUAL_DISPLAY
1714 dev->mHDMIEnabled = EXT_DISPLAY_OFF;
1715 dev->pendingHDMI = false;
1716#endif
1717 dev->previousOverlayHandle = NULL;
1718 dev->hwcOverlayStatus = HWC_OVERLAY_CLOSED;
1719 dev->previousLayerCount = -1;
1720 /* initialize the procs */
1721 dev->device.common.tag = HARDWARE_DEVICE_TAG;
1722 dev->device.common.version = 0;
1723 dev->device.common.module = const_cast<hw_module_t*>(module);
1724 dev->device.common.close = hwc_device_close;
1725
1726 dev->device.prepare = hwc_prepare;
1727 dev->device.set = hwc_set;
1728 dev->device.enableHDMIOutput = hwc_enableHDMIOutput;
1729 *device = &dev->device.common;
1730
1731 status = 0;
1732 }
1733 return status;
1734}