hwc_utils.cpp revision fb704cff54130d8fc2041e3b10016376f67004a2
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 * Copyright (C) 2012-2013, The Linux Foundation All rights reserved.
4 *
5 * Not a Contribution, Apache license notifications and license are retained
6 * for attribution purposes only.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 *      http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20#define HWC_UTILS_DEBUG 0
21#include <sys/ioctl.h>
22#include <linux/fb.h>
23#include <binder/IServiceManager.h>
24#include <EGL/egl.h>
25#include <cutils/properties.h>
26#include <gralloc_priv.h>
27#include <overlay.h>
28#include <overlayRotator.h>
29#include "hwc_utils.h"
30#include "hwc_mdpcomp.h"
31#include "hwc_fbupdate.h"
32#include "hwc_video.h"
33#include "mdp_version.h"
34#include "hwc_copybit.h"
35#include "external.h"
36#include "hwc_qclient.h"
37#include "QService.h"
38#include "comptype.h"
39
40using namespace qClient;
41using namespace qService;
42using namespace android;
43using namespace overlay;
44using namespace overlay::utils;
45namespace ovutils = overlay::utils;
46
47namespace qhwc {
48
49static int openFramebufferDevice(hwc_context_t *ctx)
50{
51    struct fb_fix_screeninfo finfo;
52    struct fb_var_screeninfo info;
53
54    int fb_fd = openFb(HWC_DISPLAY_PRIMARY);
55
56    if (ioctl(fb_fd, FBIOGET_VSCREENINFO, &info) == -1)
57        return -errno;
58
59    if (int(info.width) <= 0 || int(info.height) <= 0) {
60        // the driver doesn't return that information
61        // default to 160 dpi
62        info.width  = ((info.xres * 25.4f)/160.0f + 0.5f);
63        info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
64    }
65
66    float xdpi = (info.xres * 25.4f) / info.width;
67    float ydpi = (info.yres * 25.4f) / info.height;
68
69#ifdef MSMFB_METADATA_GET
70    struct msmfb_metadata metadata;
71    memset(&metadata, 0 , sizeof(metadata));
72    metadata.op = metadata_op_frame_rate;
73
74    if (ioctl(fb_fd, MSMFB_METADATA_GET, &metadata) == -1) {
75        ALOGE("Error retrieving panel frame rate");
76        return -errno;
77    }
78
79    float fps  = metadata.data.panel_frame_rate;
80#else
81    //XXX: Remove reserved field usage on all baselines
82    //The reserved[3] field is used to store FPS by the driver.
83    float fps  = info.reserved[3] & 0xFF;
84#endif
85
86    if (ioctl(fb_fd, FBIOGET_FSCREENINFO, &finfo) == -1)
87        return -errno;
88
89    if (finfo.smem_len <= 0)
90        return -errno;
91
92    ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = fb_fd;
93    //xres, yres may not be 32 aligned
94    ctx->dpyAttr[HWC_DISPLAY_PRIMARY].stride = finfo.line_length /(info.xres/8);
95    ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres = info.xres;
96    ctx->dpyAttr[HWC_DISPLAY_PRIMARY].yres = info.yres;
97    ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xdpi = xdpi;
98    ctx->dpyAttr[HWC_DISPLAY_PRIMARY].ydpi = ydpi;
99    ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period = 1000000000l / fps;
100
101    return 0;
102}
103
104void initContext(hwc_context_t *ctx)
105{
106    openFramebufferDevice(ctx);
107    ctx->mMDP.version = qdutils::MDPVersion::getInstance().getMDPVersion();
108    ctx->mMDP.hasOverlay = qdutils::MDPVersion::getInstance().hasOverlay();
109    ctx->mMDP.panel = qdutils::MDPVersion::getInstance().getPanelType();
110    overlay::Overlay::initOverlay();
111    ctx->mOverlay = overlay::Overlay::getInstance();
112    ctx->mRotMgr = new RotMgr();
113
114    //Is created and destroyed only once for primary
115    //For external it could get created and destroyed multiple times depending
116    //on what external we connect to.
117    ctx->mFBUpdate[HWC_DISPLAY_PRIMARY] =
118        IFBUpdate::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres,
119        HWC_DISPLAY_PRIMARY);
120
121    ctx->mVidOv[HWC_DISPLAY_PRIMARY] =
122        IVideoOverlay::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres,
123        HWC_DISPLAY_PRIMARY);
124
125    // Check if the target supports copybit compostion (dyn/mdp/c2d) to
126    // decide if we need to open the copybit module.
127    int compositionType =
128        qdutils::QCCompositionType::getInstance().getCompositionType();
129
130    if (compositionType & (qdutils::COMPOSITION_TYPE_DYN |
131                           qdutils::COMPOSITION_TYPE_MDP |
132                           qdutils::COMPOSITION_TYPE_C2D)) {
133            ctx->mCopyBit[HWC_DISPLAY_PRIMARY] = new CopyBit();
134    }
135
136    ctx->mExtDisplay = new ExternalDisplay(ctx);
137    for (uint32_t i = 0; i < MAX_DISPLAYS; i++)
138        ctx->mLayerCache[i] = new LayerCache();
139    ctx->mMDPComp = MDPComp::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres);
140    MDPComp::init(ctx);
141
142    pthread_mutex_init(&(ctx->vstate.lock), NULL);
143    pthread_cond_init(&(ctx->vstate.cond), NULL);
144    ctx->vstate.enable = false;
145    ctx->vstate.fakevsync = false;
146    ctx->mExtDispConfiguring = false;
147
148    //Right now hwc starts the service but anybody could do it, or it could be
149    //independent process as well.
150    QService::init();
151    sp<IQClient> client = new QClient(ctx);
152    interface_cast<IQService>(
153            defaultServiceManager()->getService(
154            String16("display.qservice")))->connect(client);
155
156    ALOGI("Initializing Qualcomm Hardware Composer");
157    ALOGI("MDP version: %d", ctx->mMDP.version);
158}
159
160void closeContext(hwc_context_t *ctx)
161{
162    if(ctx->mOverlay) {
163        delete ctx->mOverlay;
164        ctx->mOverlay = NULL;
165    }
166
167    if(ctx->mRotMgr) {
168        delete ctx->mRotMgr;
169        ctx->mRotMgr = NULL;
170    }
171
172    for(int i = 0; i < MAX_DISPLAYS; i++) {
173        if(ctx->mCopyBit[i]) {
174            delete ctx->mCopyBit[i];
175            ctx->mCopyBit[i] = NULL;
176        }
177    }
178
179    if(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd) {
180        close(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd);
181        ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = -1;
182    }
183
184    if(ctx->mExtDisplay) {
185        delete ctx->mExtDisplay;
186        ctx->mExtDisplay = NULL;
187    }
188
189    for(int i = 0; i < MAX_DISPLAYS; i++) {
190        if(ctx->mFBUpdate[i]) {
191            delete ctx->mFBUpdate[i];
192            ctx->mFBUpdate[i] = NULL;
193        }
194        if(ctx->mVidOv[i]) {
195            delete ctx->mVidOv[i];
196            ctx->mVidOv[i] = NULL;
197        }
198    }
199
200    if(ctx->mMDPComp) {
201        delete ctx->mMDPComp;
202        ctx->mMDPComp = NULL;
203    }
204
205    pthread_mutex_destroy(&(ctx->vstate.lock));
206    pthread_cond_destroy(&(ctx->vstate.cond));
207}
208
209
210void dumpsys_log(android::String8& buf, const char* fmt, ...)
211{
212    va_list varargs;
213    va_start(varargs, fmt);
214    buf.appendFormatV(fmt, varargs);
215    va_end(varargs);
216}
217
218/* Calculates the destination position based on the action safe rectangle */
219void getActionSafePosition(hwc_context_t *ctx, int dpy, uint32_t& x,
220                           uint32_t& y, uint32_t& w, uint32_t& h) {
221
222    // if external supports underscan, do nothing
223    // it will be taken care in the driver
224    if(ctx->mExtDisplay->isCEUnderscanSupported())
225        return;
226
227    float wRatio = 1.0;
228    float hRatio = 1.0;
229    float xRatio = 1.0;
230    float yRatio = 1.0;
231
232    float fbWidth = ctx->dpyAttr[dpy].xres;
233    float fbHeight = ctx->dpyAttr[dpy].yres;
234
235    float asX = 0;
236    float asY = 0;
237    float asW = fbWidth;
238    float asH= fbHeight;
239    char value[PROPERTY_VALUE_MAX];
240
241    // Apply action safe parameters
242    property_get("hw.actionsafe.width", value, "0");
243    int asWidthRatio = atoi(value);
244    property_get("hw.actionsafe.height", value, "0");
245    int asHeightRatio = atoi(value);
246    // based on the action safe ratio, get the Action safe rectangle
247    asW = fbWidth * (1.0f -  asWidthRatio / 100.0f);
248    asH = fbHeight * (1.0f -  asHeightRatio / 100.0f);
249    asX = (fbWidth - asW) / 2;
250    asY = (fbHeight - asH) / 2;
251
252    // calculate the position ratio
253    xRatio = (float)x/fbWidth;
254    yRatio = (float)y/fbHeight;
255    wRatio = (float)w/fbWidth;
256    hRatio = (float)h/fbHeight;
257
258    //Calculate the position...
259    x = (xRatio * asW) + asX;
260    y = (yRatio * asH) + asY;
261    w = (wRatio * asW);
262    h = (hRatio * asH);
263
264    return;
265}
266
267bool needsScaling(hwc_layer_1_t const* layer) {
268    int dst_w, dst_h, src_w, src_h;
269
270    hwc_rect_t displayFrame  = layer->displayFrame;
271    hwc_rect_t sourceCrop = layer->sourceCrop;
272
273    dst_w = displayFrame.right - displayFrame.left;
274    dst_h = displayFrame.bottom - displayFrame.top;
275
276    src_w = sourceCrop.right - sourceCrop.left;
277    src_h = sourceCrop.bottom - sourceCrop.top;
278
279    if(((src_w != dst_w) || (src_h != dst_h)))
280        return true;
281
282    return false;
283}
284
285bool isAlphaScaled(hwc_layer_1_t const* layer) {
286    if(needsScaling(layer) && isAlphaPresent(layer)) {
287        return true;
288    }
289    return false;
290}
291
292bool isAlphaPresent(hwc_layer_1_t const* layer) {
293    private_handle_t *hnd = (private_handle_t *)layer->handle;
294    int format = hnd->format;
295    switch(format) {
296        case HAL_PIXEL_FORMAT_RGBA_8888:
297        case HAL_PIXEL_FORMAT_BGRA_8888:
298            // In any more formats with Alpha go here..
299            return true;
300        default : return false;
301    }
302    return false;
303}
304
305void setListStats(hwc_context_t *ctx,
306        const hwc_display_contents_1_t *list, int dpy) {
307
308    ctx->listStats[dpy].numAppLayers = list->numHwLayers - 1;
309    ctx->listStats[dpy].fbLayerIndex = list->numHwLayers - 1;
310    ctx->listStats[dpy].skipCount = 0;
311    ctx->listStats[dpy].needsAlphaScale = false;
312    ctx->listStats[dpy].yuvCount = 0;
313
314    for (size_t i = 0; i < list->numHwLayers; i++) {
315        hwc_layer_1_t const* layer = &list->hwLayers[i];
316        private_handle_t *hnd = (private_handle_t *)layer->handle;
317
318        //reset stored yuv index
319        ctx->listStats[dpy].yuvIndices[i] = -1;
320
321        if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
322            continue;
323        //We disregard FB being skip for now! so the else if
324        } else if (isSkipLayer(&list->hwLayers[i])) {
325            ctx->listStats[dpy].skipCount++;
326        } else if (UNLIKELY(isYuvBuffer(hnd))) {
327            int& yuvCount = ctx->listStats[dpy].yuvCount;
328            ctx->listStats[dpy].yuvIndices[yuvCount] = i;
329            yuvCount++;
330
331            if(layer->transform & HWC_TRANSFORM_ROT_90)
332                ctx->mNeedsRotator = true;
333        }
334
335        if(!ctx->listStats[dpy].needsAlphaScale)
336            ctx->listStats[dpy].needsAlphaScale = isAlphaScaled(layer);
337    }
338}
339
340
341static inline void calc_cut(float& leftCutRatio, float& topCutRatio,
342        float& rightCutRatio, float& bottomCutRatio, int orient) {
343    if(orient & HAL_TRANSFORM_FLIP_H) {
344        swap(leftCutRatio, rightCutRatio);
345    }
346    if(orient & HAL_TRANSFORM_FLIP_V) {
347        swap(topCutRatio, bottomCutRatio);
348    }
349    if(orient & HAL_TRANSFORM_ROT_90) {
350        //Anti clock swapping
351        float tmpCutRatio = leftCutRatio;
352        leftCutRatio = topCutRatio;
353        topCutRatio = rightCutRatio;
354        rightCutRatio = bottomCutRatio;
355        bottomCutRatio = tmpCutRatio;
356    }
357}
358
359bool isSecuring(hwc_context_t* ctx) {
360    if((ctx->mMDP.version < qdutils::MDSS_V5) &&
361       (ctx->mMDP.version > qdutils::MDP_V3_0) &&
362        ctx->mSecuring) {
363        return true;
364    }
365    return false;
366}
367
368bool isSecureModePolicy(int mdpVersion) {
369    if (mdpVersion < qdutils::MDSS_V5)
370        return true;
371    else
372        return false;
373}
374
375//Crops source buffer against destination and FB boundaries
376void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
377                          const hwc_rect_t& scissor, int orient) {
378
379    int& crop_l = crop.left;
380    int& crop_t = crop.top;
381    int& crop_r = crop.right;
382    int& crop_b = crop.bottom;
383    int crop_w = crop.right - crop.left;
384    int crop_h = crop.bottom - crop.top;
385
386    int& dst_l = dst.left;
387    int& dst_t = dst.top;
388    int& dst_r = dst.right;
389    int& dst_b = dst.bottom;
390    int dst_w = abs(dst.right - dst.left);
391    int dst_h = abs(dst.bottom - dst.top);
392
393    const int& sci_l = scissor.left;
394    const int& sci_t = scissor.top;
395    const int& sci_r = scissor.right;
396    const int& sci_b = scissor.bottom;
397    int sci_w = abs(sci_r - sci_l);
398    int sci_h = abs(sci_b - sci_t);
399
400    float leftCutRatio = 0.0f, rightCutRatio = 0.0f, topCutRatio = 0.0f,
401            bottomCutRatio = 0.0f;
402
403    if(dst_l < sci_l) {
404        leftCutRatio = (float)(sci_l - dst_l) / (float)dst_w;
405        dst_l = sci_l;
406    }
407
408    if(dst_r > sci_r) {
409        rightCutRatio = (float)(dst_r - sci_r) / (float)dst_w;
410        dst_r = sci_r;
411    }
412
413    if(dst_t < sci_t) {
414        topCutRatio = (float)(sci_t - dst_t) / (float)dst_h;
415        dst_t = sci_t;
416    }
417
418    if(dst_b > sci_b) {
419        bottomCutRatio = (float)(dst_b - sci_b) / (float)dst_h;
420        dst_b = sci_b;
421    }
422
423    calc_cut(leftCutRatio, topCutRatio, rightCutRatio, bottomCutRatio, orient);
424    crop_l += crop_w * leftCutRatio;
425    crop_t += crop_h * topCutRatio;
426    crop_r -= crop_w * rightCutRatio;
427    crop_b -= crop_h * bottomCutRatio;
428}
429
430void getNonWormholeRegion(hwc_display_contents_1_t* list,
431                              hwc_rect_t& nwr)
432{
433    uint32_t last = list->numHwLayers - 1;
434    hwc_rect_t fbDisplayFrame = list->hwLayers[last].displayFrame;
435    //Initiliaze nwr to first frame
436    nwr.left =  list->hwLayers[0].displayFrame.left;
437    nwr.top =  list->hwLayers[0].displayFrame.top;
438    nwr.right =  list->hwLayers[0].displayFrame.right;
439    nwr.bottom =  list->hwLayers[0].displayFrame.bottom;
440
441    for (uint32_t i = 1; i < last; i++) {
442        hwc_rect_t displayFrame = list->hwLayers[i].displayFrame;
443        nwr.left   = min(nwr.left, displayFrame.left);
444        nwr.top    = min(nwr.top, displayFrame.top);
445        nwr.right  = max(nwr.right, displayFrame.right);
446        nwr.bottom = max(nwr.bottom, displayFrame.bottom);
447    }
448
449    //Intersect with the framebuffer
450    nwr.left   = max(nwr.left, fbDisplayFrame.left);
451    nwr.top    = max(nwr.top, fbDisplayFrame.top);
452    nwr.right  = min(nwr.right, fbDisplayFrame.right);
453    nwr.bottom = min(nwr.bottom, fbDisplayFrame.bottom);
454
455}
456
457bool isExternalActive(hwc_context_t* ctx) {
458    return ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive;
459}
460
461void closeAcquireFds(hwc_display_contents_1_t* list) {
462    for(uint32_t i = 0; list && i < list->numHwLayers; i++) {
463        //Close the acquireFenceFds
464        //HWC_FRAMEBUFFER are -1 already by SF, rest we close.
465        if(list->hwLayers[i].acquireFenceFd >= 0) {
466            close(list->hwLayers[i].acquireFenceFd);
467            list->hwLayers[i].acquireFenceFd = -1;
468        }
469    }
470}
471
472int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy,
473                                                        int fd) {
474    int ret = 0;
475    struct mdp_buf_sync data;
476    int acquireFd[MAX_NUM_LAYERS];
477    int count = 0;
478    int releaseFd = -1;
479    int fbFd = -1;
480    memset(&data, 0, sizeof(data));
481    bool swapzero = false;
482    data.flags = MDP_BUF_SYNC_FLAG_WAIT;
483    data.acq_fen_fd = acquireFd;
484    data.rel_fen_fd = &releaseFd;
485    char property[PROPERTY_VALUE_MAX];
486    if(property_get("debug.egl.swapinterval", property, "1") > 0) {
487        if(atoi(property) == 0)
488            swapzero = true;
489    }
490
491    //Accumulate acquireFenceFds
492    for(uint32_t i = 0; i < list->numHwLayers; i++) {
493        if(list->hwLayers[i].compositionType == HWC_OVERLAY &&
494                        list->hwLayers[i].acquireFenceFd != -1) {
495            if(UNLIKELY(swapzero))
496                acquireFd[count++] = -1;
497            else
498                acquireFd[count++] = list->hwLayers[i].acquireFenceFd;
499        }
500        if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
501            if(UNLIKELY(swapzero))
502                acquireFd[count++] = -1;
503            else if(fd != -1) {
504                //set the acquireFD from fd - which is coming from c2d
505                acquireFd[count++] = fd;
506                // Buffer sync IOCTL should be async when using c2d fence is
507                // used
508                data.flags &= ~MDP_BUF_SYNC_FLAG_WAIT;
509            } else if(list->hwLayers[i].acquireFenceFd != -1)
510                acquireFd[count++] = list->hwLayers[i].acquireFenceFd;
511        }
512    }
513
514    data.acq_fen_fd_cnt = count;
515    fbFd = ctx->dpyAttr[dpy].fd;
516    //Waits for acquire fences, returns a release fence
517    if(LIKELY(!swapzero)) {
518        uint64_t start = systemTime();
519        ret = ioctl(fbFd, MSMFB_BUFFER_SYNC, &data);
520        ALOGD_IF(HWC_UTILS_DEBUG, "%s: time taken for MSMFB_BUFFER_SYNC IOCTL = %d",
521                            __FUNCTION__, (size_t) ns2ms(systemTime() - start));
522    }
523
524    if(ret < 0) {
525        ALOGE("ioctl MSMFB_BUFFER_SYNC failed, err=%s",
526                strerror(errno));
527    }
528
529    for(uint32_t i = 0; i < list->numHwLayers; i++) {
530        if(list->hwLayers[i].compositionType == HWC_OVERLAY ||
531           list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
532            //Populate releaseFenceFds.
533            if(UNLIKELY(swapzero))
534                list->hwLayers[i].releaseFenceFd = -1;
535            else
536                list->hwLayers[i].releaseFenceFd = dup(releaseFd);
537        }
538    }
539
540    if(fd >= 0) {
541        close(fd);
542        fd = -1;
543    }
544
545    if (ctx->mCopyBit[dpy])
546        ctx->mCopyBit[dpy]->setReleaseFd(releaseFd);
547    if(UNLIKELY(swapzero)){
548        list->retireFenceFd = -1;
549        close(releaseFd);
550    } else {
551        list->retireFenceFd = releaseFd;
552    }
553
554    return ret;
555}
556
557void trimLayer(hwc_context_t *ctx, const int& dpy, const int& transform,
558        hwc_rect_t& crop, hwc_rect_t& dst) {
559    int hw_w = ctx->dpyAttr[dpy].xres;
560    int hw_h = ctx->dpyAttr[dpy].yres;
561    if(dst.left < 0 || dst.top < 0 ||
562            dst.right > hw_w || dst.bottom > hw_h) {
563        hwc_rect_t scissor = {0, 0, hw_w, hw_h };
564        qhwc::calculate_crop_rects(crop, dst, scissor, transform);
565    }
566}
567
568void setMdpFlags(hwc_layer_1_t *layer,
569        ovutils::eMdpFlags &mdpFlags,
570        int rotDownscale) {
571    private_handle_t *hnd = (private_handle_t *)layer->handle;
572    MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
573    const int& transform = layer->transform;
574
575    if(layer->blending == HWC_BLENDING_PREMULT) {
576        ovutils::setMdpFlags(mdpFlags,
577                ovutils::OV_MDP_BLEND_FG_PREMULT);
578    }
579
580    if(isYuvBuffer(hnd)) {
581        if(isSecureBuffer(hnd)) {
582            ovutils::setMdpFlags(mdpFlags,
583                    ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
584        }
585        if(metadata && (metadata->operation & PP_PARAM_INTERLACED) &&
586                metadata->interlaced) {
587            ovutils::setMdpFlags(mdpFlags,
588                    ovutils::OV_MDP_DEINTERLACE);
589        }
590        //Pre-rotation will be used using rotator.
591        if(transform & HWC_TRANSFORM_ROT_90) {
592            ovutils::setMdpFlags(mdpFlags,
593                    ovutils::OV_MDP_SOURCE_ROTATED_90);
594        }
595    }
596
597    //No 90 component and no rot-downscale then flips done by MDP
598    //If we use rot then it might as well do flips
599    if(!(layer->transform & HWC_TRANSFORM_ROT_90) && !rotDownscale) {
600        if(layer->transform & HWC_TRANSFORM_FLIP_H) {
601            ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_FLIP_H);
602        }
603
604        if(layer->transform & HWC_TRANSFORM_FLIP_V) {
605            ovutils::setMdpFlags(mdpFlags,  ovutils::OV_MDP_FLIP_V);
606        }
607    }
608
609    if(metadata &&
610        ((metadata->operation & PP_PARAM_HSIC)
611        || (metadata->operation & PP_PARAM_IGC)
612        || (metadata->operation & PP_PARAM_SHARP2))) {
613        ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_PP_EN);
614    }
615}
616
617static inline int configRotator(Rotator *rot, const Whf& whf,
618        const eMdpFlags& mdpFlags, const eTransform& orient,
619        const int& downscale) {
620    rot->setSource(whf);
621    rot->setFlags(mdpFlags);
622    rot->setTransform(orient);
623    rot->setDownscale(downscale);
624    if(!rot->commit()) return -1;
625    return 0;
626}
627
628static inline int configMdp(Overlay *ov, const PipeArgs& parg,
629        const eTransform& orient, const hwc_rect_t& crop,
630        const hwc_rect_t& pos, const MetaData_t *metadata,
631        const eDest& dest) {
632    ov->setSource(parg, dest);
633    ov->setTransform(orient, dest);
634
635    int crop_w = crop.right - crop.left;
636    int crop_h = crop.bottom - crop.top;
637    Dim dcrop(crop.left, crop.top, crop_w, crop_h);
638    ov->setCrop(dcrop, dest);
639
640    int posW = pos.right - pos.left;
641    int posH = pos.bottom - pos.top;
642    Dim position(pos.left, pos.top, posW, posH);
643    ov->setPosition(position, dest);
644
645    if (metadata)
646        ov->setVisualParams(*metadata, dest);
647
648    if (!ov->commit(dest)) {
649        return -1;
650    }
651    return 0;
652}
653
654static inline void updateSource(eTransform& orient, Whf& whf,
655        hwc_rect_t& crop) {
656    Dim srcCrop(crop.left, crop.top,
657            crop.right - crop.left,
658            crop.bottom - crop.top);
659    //getMdpOrient will switch the flips if the source is 90 rotated.
660    //Clients in Android dont factor in 90 rotation while deciding the flip.
661    orient = static_cast<eTransform>(ovutils::getMdpOrient(orient));
662    preRotateSource(orient, whf, srcCrop);
663    crop.left = srcCrop.x;
664    crop.top = srcCrop.y;
665    crop.right = srcCrop.x + srcCrop.w;
666    crop.bottom = srcCrop.y + srcCrop.h;
667}
668
669int configureLowRes(hwc_context_t *ctx, hwc_layer_1_t *layer,
670        const int& dpy, eMdpFlags& mdpFlags, const eZorder& z,
671        const eIsFg& isFg, const eDest& dest, Rotator **rot) {
672
673    private_handle_t *hnd = (private_handle_t *)layer->handle;
674    if(!hnd) {
675        ALOGE("%s: layer handle is NULL", __FUNCTION__);
676        return -1;
677    }
678
679    MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
680
681    hwc_rect_t crop = layer->sourceCrop;
682    hwc_rect_t dst = layer->displayFrame;
683    int transform = layer->transform;
684    eTransform orient = static_cast<eTransform>(transform);
685    int downscale = 0;
686    int rotFlags = ovutils::ROT_FLAGS_NONE;
687    Whf whf(hnd->width, hnd->height,
688            getMdpFormat(hnd->format), hnd->size);
689
690    if(isYuvBuffer(hnd) && ctx->mMDP.version >= qdutils::MDP_V4_2 &&
691                ctx->mMDP.version < qdutils::MDSS_V5) {
692        downscale = getDownscaleFactor(
693                crop.right - crop.left,
694                crop.bottom - crop.top,
695                dst.right - dst.left,
696                dst.bottom - dst.top);
697        if(downscale) {
698            rotFlags = ROT_DOWNSCALE_ENABLED;
699        }
700    }
701
702    setMdpFlags(layer, mdpFlags, downscale);
703    trimLayer(ctx, dpy, transform, crop, dst);
704
705    if(isYuvBuffer(hnd) && //if 90 component or downscale, use rot
706            ((transform & HWC_TRANSFORM_ROT_90) || downscale)) {
707        *rot = ctx->mRotMgr->getNext();
708        if(*rot == NULL) return -1;
709        //Configure rotator for pre-rotation
710        if(configRotator(*rot, whf, mdpFlags, orient, downscale) < 0)
711            return -1;
712        whf.format = (*rot)->getDstFormat();
713        updateSource(orient, whf, crop);
714        rotFlags |= ovutils::ROT_PREROTATED;
715    }
716
717    //For the mdp, since either we are pre-rotating or MDP does flips
718    orient = OVERLAY_TRANSFORM_0;
719    transform = 0;
720
721    PipeArgs parg(mdpFlags, whf, z, isFg, static_cast<eRotFlags>(rotFlags));
722    if(configMdp(ctx->mOverlay, parg, orient, crop, dst, metadata, dest) < 0) {
723        ALOGE("%s: commit failed for low res panel", __FUNCTION__);
724        return -1;
725    }
726    return 0;
727}
728
729int configureHighRes(hwc_context_t *ctx, hwc_layer_1_t *layer,
730        const int& dpy, eMdpFlags& mdpFlagsL, const eZorder& z,
731        const eIsFg& isFg, const eDest& lDest, const eDest& rDest,
732        Rotator **rot) {
733    private_handle_t *hnd = (private_handle_t *)layer->handle;
734    if(!hnd) {
735        ALOGE("%s: layer handle is NULL", __FUNCTION__);
736        return -1;
737    }
738
739    MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
740
741    int hw_w = ctx->dpyAttr[dpy].xres;
742    int hw_h = ctx->dpyAttr[dpy].yres;
743    hwc_rect_t crop = layer->sourceCrop;
744    hwc_rect_t dst = layer->displayFrame;
745    int transform = layer->transform;
746    eTransform orient = static_cast<eTransform>(transform);
747    const int downscale = 0;
748    int rotFlags = ROT_FLAGS_NONE;
749
750    Whf whf(hnd->width, hnd->height,
751            getMdpFormat(hnd->format), hnd->size);
752
753    setMdpFlags(layer, mdpFlagsL);
754    trimLayer(ctx, dpy, transform, crop, dst);
755
756    if(isYuvBuffer(hnd) && (transform & HWC_TRANSFORM_ROT_90)) {
757        (*rot) = ctx->mRotMgr->getNext();
758        if((*rot) == NULL) return -1;
759        //Configure rotator for pre-rotation
760        if(configRotator(*rot, whf, mdpFlagsL, orient, downscale) < 0)
761            return -1;
762        whf.format = (*rot)->getDstFormat();
763        updateSource(orient, whf, crop);
764        rotFlags |= ROT_PREROTATED;
765    }
766
767    eMdpFlags mdpFlagsR = mdpFlagsL;
768    setMdpFlags(mdpFlagsR, OV_MDSS_MDP_RIGHT_MIXER);
769
770    hwc_rect_t tmp_cropL, tmp_dstL;
771    hwc_rect_t tmp_cropR, tmp_dstR;
772
773    if(lDest != OV_INVALID) {
774        tmp_cropL = crop;
775        tmp_dstL = dst;
776        hwc_rect_t scissor = {0, 0, hw_w/2, hw_h };
777        qhwc::calculate_crop_rects(tmp_cropL, tmp_dstL, scissor, 0);
778    }
779    if(rDest != OV_INVALID) {
780        tmp_cropR = crop;
781        tmp_dstR = dst;
782        hwc_rect_t scissor = {hw_w/2, 0, hw_w, hw_h };
783        qhwc::calculate_crop_rects(tmp_cropR, tmp_dstR, scissor, 0);
784    }
785
786    //When buffer is flipped, contents of mixer config also needs to swapped.
787    //Not needed if the layer is confined to one half of the screen.
788    //If rotator has been used then it has also done the flips, so ignore them.
789    if((orient & OVERLAY_TRANSFORM_FLIP_V) && lDest != OV_INVALID
790            && rDest != OV_INVALID && rot == NULL) {
791        hwc_rect_t new_cropR;
792        new_cropR.left = tmp_cropL.left;
793        new_cropR.right = new_cropR.left + (tmp_cropR.right - tmp_cropR.left);
794
795        hwc_rect_t new_cropL;
796        new_cropL.left  = new_cropR.right;
797        new_cropL.right = tmp_cropR.right;
798
799        tmp_cropL.left =  new_cropL.left;
800        tmp_cropL.right =  new_cropL.right;
801
802        tmp_cropR.left = new_cropR.left;
803        tmp_cropR.right =  new_cropR.right;
804
805    }
806
807    //For the mdp, since either we are pre-rotating or MDP does flips
808    orient = OVERLAY_TRANSFORM_0;
809    transform = 0;
810
811    //configure left mixer
812    if(lDest != OV_INVALID) {
813        PipeArgs pargL(mdpFlagsL, whf, z, isFg,
814                static_cast<eRotFlags>(rotFlags));
815        if(configMdp(ctx->mOverlay, pargL, orient,
816                tmp_cropL, tmp_dstL, metadata, lDest) < 0) {
817            ALOGE("%s: commit failed for left mixer config", __FUNCTION__);
818            return -1;
819        }
820    }
821
822    //configure right mixer
823    if(rDest != OV_INVALID) {
824        PipeArgs pargR(mdpFlagsR, whf, z, isFg,
825                static_cast<eRotFlags>(rotFlags));
826        tmp_dstR.right = tmp_dstR.right - tmp_dstR.left;
827        tmp_dstR.left = 0;
828        if(configMdp(ctx->mOverlay, pargR, orient,
829                tmp_cropR, tmp_dstR, metadata, rDest) < 0) {
830            ALOGE("%s: commit failed for right mixer config", __FUNCTION__);
831            return -1;
832        }
833    }
834
835    return 0;
836}
837
838void LayerCache::resetLayerCache(int num) {
839    for(uint32_t i = 0; i < MAX_NUM_LAYERS; i++) {
840        hnd[i] = NULL;
841    }
842    numHwLayers = num;
843}
844
845void LayerCache::updateLayerCache(hwc_display_contents_1_t* list) {
846
847    int numFbLayers = 0;
848    int numCacheableLayers = 0;
849
850    canUseLayerCache = false;
851    //Bail if geometry changed or num of layers changed
852    if(list->flags & HWC_GEOMETRY_CHANGED ||
853       list->numHwLayers != numHwLayers ) {
854        resetLayerCache(list->numHwLayers);
855        return;
856    }
857
858    for(uint32_t i = 0; i < list->numHwLayers; i++) {
859        //Bail on skip layers
860        if(list->hwLayers[i].flags & HWC_SKIP_LAYER) {
861            resetLayerCache(list->numHwLayers);
862            return;
863        }
864
865        if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER) {
866            numFbLayers++;
867            if(hnd[i] == NULL) {
868                hnd[i] = list->hwLayers[i].handle;
869            } else if (hnd[i] ==
870                       list->hwLayers[i].handle) {
871                numCacheableLayers++;
872            } else {
873                hnd[i] = NULL;
874                return;
875            }
876        } else {
877            hnd[i] = NULL;
878        }
879    }
880    if(numFbLayers == numCacheableLayers)
881        canUseLayerCache = true;
882
883    //XXX: The marking part is separate, if MDP comp wants
884    // to use it in the future. Right now getting MDP comp
885    // to use this is more trouble than it is worth.
886    markCachedLayersAsOverlay(list);
887}
888
889void LayerCache::markCachedLayersAsOverlay(hwc_display_contents_1_t* list) {
890    //This optimization only works if ALL the layer handles
891    //that were on the framebuffer didn't change.
892    if(canUseLayerCache){
893        for(uint32_t i = 0; i < list->numHwLayers; i++) {
894            if (list->hwLayers[i].handle &&
895                list->hwLayers[i].handle == hnd[i] &&
896                list->hwLayers[i].compositionType != HWC_FRAMEBUFFER_TARGET)
897            {
898                list->hwLayers[i].compositionType = HWC_OVERLAY;
899            }
900        }
901    }
902}
903
904};//namespace qhwc
905