hwc_utils.cpp revision 5d50448ce691782fde0c26ec8247b3d5d6b2de82
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 <binder/IServiceManager.h>
23#include <EGL/egl.h>
24#include <cutils/properties.h>
25#include <gralloc_priv.h>
26#include <overlay.h>
27#include <overlayRotator.h>
28#include "hwc_utils.h"
29#include "hwc_mdpcomp.h"
30#include "hwc_fbupdate.h"
31#include "hwc_video.h"
32#include "mdp_version.h"
33#include "hwc_copybit.h"
34#include "external.h"
35#include "hwc_qclient.h"
36#include "QService.h"
37#include "comptype.h"
38
39using namespace qClient;
40using namespace qService;
41using namespace android;
42using namespace overlay;
43using namespace overlay::utils;
44namespace ovutils = overlay::utils;
45
46namespace qhwc {
47
48static int openFramebufferDevice(hwc_context_t *ctx)
49{
50    struct fb_fix_screeninfo finfo;
51    struct fb_var_screeninfo info;
52
53    int fb_fd = openFb(HWC_DISPLAY_PRIMARY);
54
55    if (ioctl(fb_fd, FBIOGET_VSCREENINFO, &info) == -1)
56        return -errno;
57
58    if (int(info.width) <= 0 || int(info.height) <= 0) {
59        // the driver doesn't return that information
60        // default to 160 dpi
61        info.width  = ((info.xres * 25.4f)/160.0f + 0.5f);
62        info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
63    }
64
65    float xdpi = (info.xres * 25.4f) / info.width;
66    float ydpi = (info.yres * 25.4f) / info.height;
67
68#ifdef MSMFB_METADATA_GET
69    struct msmfb_metadata metadata;
70    memset(&metadata, 0 , sizeof(metadata));
71    metadata.op = metadata_op_frame_rate;
72
73    if (ioctl(fb_fd, MSMFB_METADATA_GET, &metadata) == -1) {
74        ALOGE("Error retrieving panel frame rate");
75        return -errno;
76    }
77
78    float fps  = metadata.data.panel_frame_rate;
79#else
80    //XXX: Remove reserved field usage on all baselines
81    //The reserved[3] field is used to store FPS by the driver.
82    float fps  = info.reserved[3] & 0xFF;
83#endif
84
85    if (ioctl(fb_fd, FBIOGET_FSCREENINFO, &finfo) == -1)
86        return -errno;
87
88    if (finfo.smem_len <= 0)
89        return -errno;
90
91    ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = fb_fd;
92    //xres, yres may not be 32 aligned
93    ctx->dpyAttr[HWC_DISPLAY_PRIMARY].stride = finfo.line_length /(info.xres/8);
94    ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres = info.xres;
95    ctx->dpyAttr[HWC_DISPLAY_PRIMARY].yres = info.yres;
96    ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xdpi = xdpi;
97    ctx->dpyAttr[HWC_DISPLAY_PRIMARY].ydpi = ydpi;
98    ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period = 1000000000l / fps;
99
100    return 0;
101}
102
103void initContext(hwc_context_t *ctx)
104{
105    openFramebufferDevice(ctx);
106    ctx->mMDP.version = qdutils::MDPVersion::getInstance().getMDPVersion();
107    ctx->mMDP.hasOverlay = qdutils::MDPVersion::getInstance().hasOverlay();
108    ctx->mMDP.panel = qdutils::MDPVersion::getInstance().getPanelType();
109    overlay::Overlay::initOverlay();
110    ctx->mOverlay = overlay::Overlay::getInstance();
111    ctx->mRotMgr = new RotMgr();
112
113    //Is created and destroyed only once for primary
114    //For external it could get created and destroyed multiple times depending
115    //on what external we connect to.
116    ctx->mFBUpdate[HWC_DISPLAY_PRIMARY] =
117        IFBUpdate::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres,
118        HWC_DISPLAY_PRIMARY);
119
120    ctx->mVidOv[HWC_DISPLAY_PRIMARY] =
121        IVideoOverlay::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres,
122        HWC_DISPLAY_PRIMARY);
123
124    char value[PROPERTY_VALUE_MAX];
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)) {
287        if(layer->blending != HWC_BLENDING_NONE)
288            return true;
289    }
290    return false;
291}
292
293void setListStats(hwc_context_t *ctx,
294        const hwc_display_contents_1_t *list, int dpy) {
295
296    ctx->listStats[dpy].numAppLayers = list->numHwLayers - 1;
297    ctx->listStats[dpy].fbLayerIndex = list->numHwLayers - 1;
298    ctx->listStats[dpy].skipCount = 0;
299    ctx->listStats[dpy].needsAlphaScale = false;
300    ctx->listStats[dpy].yuvCount = 0;
301    ctx->mDMAInUse = false;
302
303    for (size_t i = 0; i < list->numHwLayers; i++) {
304        hwc_layer_1_t const* layer = &list->hwLayers[i];
305        private_handle_t *hnd = (private_handle_t *)layer->handle;
306
307        //reset stored yuv index
308        ctx->listStats[dpy].yuvIndices[i] = -1;
309
310        if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
311            continue;
312        //We disregard FB being skip for now! so the else if
313        } else if (isSkipLayer(&list->hwLayers[i])) {
314            ctx->listStats[dpy].skipCount++;
315        } else if (UNLIKELY(isYuvBuffer(hnd))) {
316            int& yuvCount = ctx->listStats[dpy].yuvCount;
317            ctx->listStats[dpy].yuvIndices[yuvCount] = i;
318            yuvCount++;
319
320            if((layer->transform & HWC_TRANSFORM_ROT_90) && !ctx->mDMAInUse)
321                ctx->mDMAInUse = true;
322        }
323
324        if(!ctx->listStats[dpy].needsAlphaScale)
325            ctx->listStats[dpy].needsAlphaScale = isAlphaScaled(layer);
326    }
327}
328
329
330static inline void calc_cut(float& leftCutRatio, float& topCutRatio,
331        float& rightCutRatio, float& bottomCutRatio, int orient) {
332    if(orient & HAL_TRANSFORM_FLIP_H) {
333        swap(leftCutRatio, rightCutRatio);
334    }
335    if(orient & HAL_TRANSFORM_FLIP_V) {
336        swap(topCutRatio, bottomCutRatio);
337    }
338    if(orient & HAL_TRANSFORM_ROT_90) {
339        //Anti clock swapping
340        float tmpCutRatio = leftCutRatio;
341        leftCutRatio = topCutRatio;
342        topCutRatio = rightCutRatio;
343        rightCutRatio = bottomCutRatio;
344        bottomCutRatio = tmpCutRatio;
345    }
346}
347
348bool isSecuring(hwc_context_t* ctx) {
349    if((ctx->mMDP.version < qdutils::MDSS_V5) &&
350       (ctx->mMDP.version > qdutils::MDP_V3_0) &&
351        ctx->mSecuring) {
352        return true;
353    }
354    return false;
355}
356
357bool isSecureModePolicy(int mdpVersion) {
358    if (mdpVersion < qdutils::MDSS_V5)
359        return true;
360    else
361        return false;
362}
363
364//Crops source buffer against destination and FB boundaries
365void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
366                          const hwc_rect_t& scissor, int orient) {
367
368    int& crop_l = crop.left;
369    int& crop_t = crop.top;
370    int& crop_r = crop.right;
371    int& crop_b = crop.bottom;
372    int crop_w = crop.right - crop.left;
373    int crop_h = crop.bottom - crop.top;
374
375    int& dst_l = dst.left;
376    int& dst_t = dst.top;
377    int& dst_r = dst.right;
378    int& dst_b = dst.bottom;
379    int dst_w = abs(dst.right - dst.left);
380    int dst_h = abs(dst.bottom - dst.top);
381
382    const int& sci_l = scissor.left;
383    const int& sci_t = scissor.top;
384    const int& sci_r = scissor.right;
385    const int& sci_b = scissor.bottom;
386    int sci_w = abs(sci_r - sci_l);
387    int sci_h = abs(sci_b - sci_t);
388
389    float leftCutRatio = 0.0f, rightCutRatio = 0.0f, topCutRatio = 0.0f,
390            bottomCutRatio = 0.0f;
391
392    if(dst_l < sci_l) {
393        leftCutRatio = (float)(sci_l - dst_l) / (float)dst_w;
394        dst_l = sci_l;
395    }
396
397    if(dst_r > sci_r) {
398        rightCutRatio = (float)(dst_r - sci_r) / (float)dst_w;
399        dst_r = sci_r;
400    }
401
402    if(dst_t < sci_t) {
403        topCutRatio = (float)(sci_t - dst_t) / (float)dst_h;
404        dst_t = sci_t;
405    }
406
407    if(dst_b > sci_b) {
408        bottomCutRatio = (float)(dst_b - sci_b) / (float)dst_h;
409        dst_b = sci_b;
410    }
411
412    calc_cut(leftCutRatio, topCutRatio, rightCutRatio, bottomCutRatio, orient);
413    crop_l += crop_w * leftCutRatio;
414    crop_t += crop_h * topCutRatio;
415    crop_r -= crop_w * rightCutRatio;
416    crop_b -= crop_h * bottomCutRatio;
417}
418
419void getNonWormholeRegion(hwc_display_contents_1_t* list,
420                              hwc_rect_t& nwr)
421{
422    uint32_t last = list->numHwLayers - 1;
423    hwc_rect_t fbDisplayFrame = list->hwLayers[last].displayFrame;
424    //Initiliaze nwr to first frame
425    nwr.left =  list->hwLayers[0].displayFrame.left;
426    nwr.top =  list->hwLayers[0].displayFrame.top;
427    nwr.right =  list->hwLayers[0].displayFrame.right;
428    nwr.bottom =  list->hwLayers[0].displayFrame.bottom;
429
430    for (uint32_t i = 1; i < last; i++) {
431        hwc_rect_t displayFrame = list->hwLayers[i].displayFrame;
432        nwr.left   = min(nwr.left, displayFrame.left);
433        nwr.top    = min(nwr.top, displayFrame.top);
434        nwr.right  = max(nwr.right, displayFrame.right);
435        nwr.bottom = max(nwr.bottom, displayFrame.bottom);
436    }
437
438    //Intersect with the framebuffer
439    nwr.left   = max(nwr.left, fbDisplayFrame.left);
440    nwr.top    = max(nwr.top, fbDisplayFrame.top);
441    nwr.right  = min(nwr.right, fbDisplayFrame.right);
442    nwr.bottom = min(nwr.bottom, fbDisplayFrame.bottom);
443
444}
445
446bool isExternalActive(hwc_context_t* ctx) {
447    return ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive;
448}
449
450void closeAcquireFds(hwc_display_contents_1_t* list) {
451    for(uint32_t i = 0; list && i < list->numHwLayers; i++) {
452        //Close the acquireFenceFds
453        //HWC_FRAMEBUFFER are -1 already by SF, rest we close.
454        if(list->hwLayers[i].acquireFenceFd >= 0) {
455            close(list->hwLayers[i].acquireFenceFd);
456            list->hwLayers[i].acquireFenceFd = -1;
457        }
458    }
459}
460
461int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy,
462                                                        int fd) {
463    int ret = 0;
464    struct mdp_buf_sync data;
465    int acquireFd[MAX_NUM_LAYERS];
466    int count = 0;
467    int releaseFd = -1;
468    int fbFd = -1;
469    memset(&data, 0, sizeof(data));
470    bool swapzero = false;
471    data.flags = MDP_BUF_SYNC_FLAG_WAIT;
472    data.acq_fen_fd = acquireFd;
473    data.rel_fen_fd = &releaseFd;
474    char property[PROPERTY_VALUE_MAX];
475    if(property_get("debug.egl.swapinterval", property, "1") > 0) {
476        if(atoi(property) == 0)
477            swapzero = true;
478    }
479
480    //Accumulate acquireFenceFds
481    for(uint32_t i = 0; i < list->numHwLayers; i++) {
482        if(list->hwLayers[i].compositionType == HWC_OVERLAY &&
483                        list->hwLayers[i].acquireFenceFd != -1) {
484            if(UNLIKELY(swapzero))
485                acquireFd[count++] = -1;
486            else
487                acquireFd[count++] = list->hwLayers[i].acquireFenceFd;
488        }
489        if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
490            if(UNLIKELY(swapzero))
491                acquireFd[count++] = -1;
492            else if(fd != -1) {
493                //set the acquireFD from fd - which is coming from c2d
494                acquireFd[count++] = fd;
495                // Buffer sync IOCTL should be async when using c2d fence is
496                // used
497                data.flags &= ~MDP_BUF_SYNC_FLAG_WAIT;
498            } else if(list->hwLayers[i].acquireFenceFd != -1)
499                acquireFd[count++] = list->hwLayers[i].acquireFenceFd;
500        }
501    }
502
503    data.acq_fen_fd_cnt = count;
504    fbFd = ctx->dpyAttr[dpy].fd;
505    //Waits for acquire fences, returns a release fence
506    if(LIKELY(!swapzero)) {
507        uint64_t start = systemTime();
508        ret = ioctl(fbFd, MSMFB_BUFFER_SYNC, &data);
509        ALOGD_IF(HWC_UTILS_DEBUG, "%s: time taken for MSMFB_BUFFER_SYNC IOCTL = %d",
510                            __FUNCTION__, (size_t) ns2ms(systemTime() - start));
511    }
512
513    if(ret < 0) {
514        ALOGE("ioctl MSMFB_BUFFER_SYNC failed, err=%s",
515                strerror(errno));
516    }
517
518    for(uint32_t i = 0; i < list->numHwLayers; i++) {
519        if(list->hwLayers[i].compositionType == HWC_OVERLAY ||
520           list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
521            //Populate releaseFenceFds.
522            if(UNLIKELY(swapzero))
523                list->hwLayers[i].releaseFenceFd = -1;
524            else
525                list->hwLayers[i].releaseFenceFd = dup(releaseFd);
526        }
527    }
528
529    if(fd >= 0) {
530        close(fd);
531        fd = -1;
532    }
533
534    if (ctx->mCopyBit[dpy])
535        ctx->mCopyBit[dpy]->setReleaseFd(releaseFd);
536    if(UNLIKELY(swapzero)){
537        list->retireFenceFd = -1;
538        close(releaseFd);
539    } else {
540        list->retireFenceFd = releaseFd;
541    }
542
543    return ret;
544}
545
546void trimLayer(hwc_context_t *ctx, const int& dpy, const int& transform,
547        hwc_rect_t& crop, hwc_rect_t& dst) {
548    int hw_w = ctx->dpyAttr[dpy].xres;
549    int hw_h = ctx->dpyAttr[dpy].yres;
550    if(dst.left < 0 || dst.top < 0 ||
551            dst.right > hw_w || dst.bottom > hw_h) {
552        hwc_rect_t scissor = {0, 0, hw_w, hw_h };
553        qhwc::calculate_crop_rects(crop, dst, scissor, transform);
554    }
555}
556
557void setMdpFlags(hwc_layer_1_t *layer,
558        ovutils::eMdpFlags &mdpFlags,
559        int rotDownscale) {
560    private_handle_t *hnd = (private_handle_t *)layer->handle;
561    MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
562    const int& transform = layer->transform;
563
564    if(layer->blending == HWC_BLENDING_PREMULT) {
565        ovutils::setMdpFlags(mdpFlags,
566                ovutils::OV_MDP_BLEND_FG_PREMULT);
567    }
568
569    if(isYuvBuffer(hnd)) {
570        if(isSecureBuffer(hnd)) {
571            ovutils::setMdpFlags(mdpFlags,
572                    ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
573        }
574        if(metadata && (metadata->operation & PP_PARAM_INTERLACED) &&
575                metadata->interlaced) {
576            ovutils::setMdpFlags(mdpFlags,
577                    ovutils::OV_MDP_DEINTERLACE);
578        }
579        //Pre-rotation will be used using rotator.
580        if(transform & HWC_TRANSFORM_ROT_90) {
581            ovutils::setMdpFlags(mdpFlags,
582                    ovutils::OV_MDP_SOURCE_ROTATED_90);
583        }
584    }
585
586    //No 90 component and no rot-downscale then flips done by MDP
587    //If we use rot then it might as well do flips
588    if(!(layer->transform & HWC_TRANSFORM_ROT_90) && !rotDownscale) {
589        if(layer->transform & HWC_TRANSFORM_FLIP_H) {
590            ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_FLIP_H);
591        }
592
593        if(layer->transform & HWC_TRANSFORM_FLIP_V) {
594            ovutils::setMdpFlags(mdpFlags,  ovutils::OV_MDP_FLIP_V);
595        }
596    }
597
598    if(metadata &&
599        ((metadata->operation & PP_PARAM_HSIC)
600        || (metadata->operation & PP_PARAM_IGC)
601        || (metadata->operation & PP_PARAM_SHARP2))) {
602        ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_PP_EN);
603    }
604}
605
606static inline int configRotator(Rotator *rot, const Whf& whf,
607        const eMdpFlags& mdpFlags, const eTransform& orient,
608        const int& downscale) {
609    rot->setSource(whf);
610    rot->setFlags(mdpFlags);
611    rot->setTransform(orient);
612    rot->setDownscale(downscale);
613    if(!rot->commit()) return -1;
614    return 0;
615}
616
617static inline int configMdp(Overlay *ov, const PipeArgs& parg,
618        const eTransform& orient, const hwc_rect_t& crop,
619        const hwc_rect_t& pos, const MetaData_t *metadata,
620        const eDest& dest) {
621    ov->setSource(parg, dest);
622    ov->setTransform(orient, dest);
623
624    int crop_w = crop.right - crop.left;
625    int crop_h = crop.bottom - crop.top;
626    Dim dcrop(crop.left, crop.top, crop_w, crop_h);
627    ov->setCrop(dcrop, dest);
628
629    int posW = pos.right - pos.left;
630    int posH = pos.bottom - pos.top;
631    Dim position(pos.left, pos.top, posW, posH);
632    ov->setPosition(position, dest);
633
634    if (metadata)
635        ov->setVisualParams(*metadata, dest);
636
637    if (!ov->commit(dest)) {
638        return -1;
639    }
640    return 0;
641}
642
643static inline void updateSource(eTransform& orient, Whf& whf,
644        hwc_rect_t& crop) {
645    Dim srcCrop(crop.left, crop.top,
646            crop.right - crop.left,
647            crop.bottom - crop.top);
648    //getMdpOrient will switch the flips if the source is 90 rotated.
649    //Clients in Android dont factor in 90 rotation while deciding the flip.
650    orient = static_cast<eTransform>(ovutils::getMdpOrient(orient));
651    preRotateSource(orient, whf, srcCrop);
652    crop.left = srcCrop.x;
653    crop.top = srcCrop.y;
654    crop.right = srcCrop.x + srcCrop.w;
655    crop.bottom = srcCrop.y + srcCrop.h;
656}
657
658int configureLowRes(hwc_context_t *ctx, hwc_layer_1_t *layer,
659        const int& dpy, eMdpFlags& mdpFlags, const eZorder& z,
660        const eIsFg& isFg, const eDest& dest, Rotator **rot) {
661
662    private_handle_t *hnd = (private_handle_t *)layer->handle;
663    if(!hnd) {
664        ALOGE("%s: layer handle is NULL", __FUNCTION__);
665        return -1;
666    }
667
668    MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
669
670    hwc_rect_t crop = layer->sourceCrop;
671    hwc_rect_t dst = layer->displayFrame;
672    int transform = layer->transform;
673    eTransform orient = static_cast<eTransform>(transform);
674    int downscale = 0;
675    int rotFlags = ovutils::ROT_FLAGS_NONE;
676    Whf whf(hnd->width, hnd->height,
677            getMdpFormat(hnd->format), hnd->size);
678
679    if(isYuvBuffer(hnd) && ctx->mMDP.version >= qdutils::MDP_V4_2 &&
680                ctx->mMDP.version < qdutils::MDSS_V5) {
681        downscale = getDownscaleFactor(
682                crop.right - crop.left,
683                crop.bottom - crop.top,
684                dst.right - dst.left,
685                dst.bottom - dst.top);
686        if(downscale) {
687            rotFlags = ROT_DOWNSCALE_ENABLED;
688        }
689    }
690
691    setMdpFlags(layer, mdpFlags, downscale);
692    trimLayer(ctx, dpy, transform, crop, dst);
693
694    if(isYuvBuffer(hnd) && //if 90 component or downscale, use rot
695            ((transform & HWC_TRANSFORM_ROT_90) || downscale)) {
696        *rot = ctx->mRotMgr->getNext();
697        if(*rot == NULL) return -1;
698        //Configure rotator for pre-rotation
699        if(configRotator(*rot, whf, mdpFlags, orient, downscale) < 0)
700            return -1;
701        whf.format = (*rot)->getDstFormat();
702        updateSource(orient, whf, crop);
703        rotFlags |= ovutils::ROT_PREROTATED;
704    }
705
706    //For the mdp, since either we are pre-rotating or MDP does flips
707    orient = OVERLAY_TRANSFORM_0;
708    transform = 0;
709
710    PipeArgs parg(mdpFlags, whf, z, isFg, static_cast<eRotFlags>(rotFlags));
711    if(configMdp(ctx->mOverlay, parg, orient, crop, dst, metadata, dest) < 0) {
712        ALOGE("%s: commit failed for low res panel", __FUNCTION__);
713        return -1;
714    }
715    return 0;
716}
717
718int configureHighRes(hwc_context_t *ctx, hwc_layer_1_t *layer,
719        const int& dpy, eMdpFlags& mdpFlagsL, const eZorder& z,
720        const eIsFg& isFg, const eDest& lDest, const eDest& rDest,
721        Rotator **rot) {
722    private_handle_t *hnd = (private_handle_t *)layer->handle;
723    if(!hnd) {
724        ALOGE("%s: layer handle is NULL", __FUNCTION__);
725        return -1;
726    }
727
728    MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
729
730    int hw_w = ctx->dpyAttr[dpy].xres;
731    int hw_h = ctx->dpyAttr[dpy].yres;
732    hwc_rect_t crop = layer->sourceCrop;
733    hwc_rect_t dst = layer->displayFrame;
734    int transform = layer->transform;
735    eTransform orient = static_cast<eTransform>(transform);
736    const int downscale = 0;
737    int rotFlags = ROT_FLAGS_NONE;
738
739    Whf whf(hnd->width, hnd->height,
740            getMdpFormat(hnd->format), hnd->size);
741
742    setMdpFlags(layer, mdpFlagsL);
743    trimLayer(ctx, dpy, transform, crop, dst);
744
745    if(isYuvBuffer(hnd) && (transform & HWC_TRANSFORM_ROT_90)) {
746        (*rot) = ctx->mRotMgr->getNext();
747        if((*rot) == NULL) return -1;
748        //Configure rotator for pre-rotation
749        if(configRotator(*rot, whf, mdpFlagsL, orient, downscale) < 0)
750            return -1;
751        whf.format = (*rot)->getDstFormat();
752        updateSource(orient, whf, crop);
753        rotFlags |= ROT_PREROTATED;
754    }
755
756    eMdpFlags mdpFlagsR = mdpFlagsL;
757    setMdpFlags(mdpFlagsR, OV_MDSS_MDP_RIGHT_MIXER);
758
759    hwc_rect_t tmp_cropL, tmp_dstL;
760    hwc_rect_t tmp_cropR, tmp_dstR;
761
762    if(lDest != OV_INVALID) {
763        tmp_cropL = crop;
764        tmp_dstL = dst;
765        hwc_rect_t scissor = {0, 0, hw_w/2, hw_h };
766        qhwc::calculate_crop_rects(tmp_cropL, tmp_dstL, scissor, 0);
767    }
768    if(rDest != OV_INVALID) {
769        tmp_cropR = crop;
770        tmp_dstR = dst;
771        hwc_rect_t scissor = {hw_w/2, 0, hw_w, hw_h };
772        qhwc::calculate_crop_rects(tmp_cropR, tmp_dstR, scissor, 0);
773    }
774
775    //When buffer is flipped, contents of mixer config also needs to swapped.
776    //Not needed if the layer is confined to one half of the screen.
777    //If rotator has been used then it has also done the flips, so ignore them.
778    if((orient & OVERLAY_TRANSFORM_FLIP_V) && lDest != OV_INVALID
779            && rDest != OV_INVALID && rot == NULL) {
780        hwc_rect_t new_cropR;
781        new_cropR.left = tmp_cropL.left;
782        new_cropR.right = new_cropR.left + (tmp_cropR.right - tmp_cropR.left);
783
784        hwc_rect_t new_cropL;
785        new_cropL.left  = new_cropR.right;
786        new_cropL.right = tmp_cropR.right;
787
788        tmp_cropL.left =  new_cropL.left;
789        tmp_cropL.right =  new_cropL.right;
790
791        tmp_cropR.left = new_cropR.left;
792        tmp_cropR.right =  new_cropR.right;
793
794    }
795
796    //For the mdp, since either we are pre-rotating or MDP does flips
797    orient = OVERLAY_TRANSFORM_0;
798    transform = 0;
799
800    //configure left mixer
801    if(lDest != OV_INVALID) {
802        PipeArgs pargL(mdpFlagsL, whf, z, isFg,
803                static_cast<eRotFlags>(rotFlags));
804        if(configMdp(ctx->mOverlay, pargL, orient,
805                tmp_cropL, tmp_dstL, metadata, lDest) < 0) {
806            ALOGE("%s: commit failed for left mixer config", __FUNCTION__);
807            return -1;
808        }
809    }
810
811    //configure right mixer
812    if(rDest != OV_INVALID) {
813        PipeArgs pargR(mdpFlagsR, whf, z, isFg,
814                static_cast<eRotFlags>(rotFlags));
815        if(configMdp(ctx->mOverlay, pargR, orient,
816                tmp_cropR, tmp_dstR, metadata, rDest) < 0) {
817            ALOGE("%s: commit failed for right mixer config", __FUNCTION__);
818            return -1;
819        }
820    }
821
822    return 0;
823}
824
825void LayerCache::resetLayerCache(int num) {
826    for(uint32_t i = 0; i < MAX_NUM_LAYERS; i++) {
827        hnd[i] = NULL;
828    }
829    numHwLayers = num;
830}
831
832void LayerCache::updateLayerCache(hwc_display_contents_1_t* list) {
833
834    int numFbLayers = 0;
835    int numCacheableLayers = 0;
836
837    canUseLayerCache = false;
838    //Bail if geometry changed or num of layers changed
839    if(list->flags & HWC_GEOMETRY_CHANGED ||
840       list->numHwLayers != numHwLayers ) {
841        resetLayerCache(list->numHwLayers);
842        return;
843    }
844
845    for(uint32_t i = 0; i < list->numHwLayers; i++) {
846        //Bail on skip layers
847        if(list->hwLayers[i].flags & HWC_SKIP_LAYER) {
848            resetLayerCache(list->numHwLayers);
849            return;
850        }
851
852        if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER) {
853            numFbLayers++;
854            if(hnd[i] == NULL) {
855                hnd[i] = list->hwLayers[i].handle;
856            } else if (hnd[i] ==
857                       list->hwLayers[i].handle) {
858                numCacheableLayers++;
859            } else {
860                hnd[i] = NULL;
861                return;
862            }
863        } else {
864            hnd[i] = NULL;
865        }
866    }
867    if(numFbLayers == numCacheableLayers)
868        canUseLayerCache = true;
869
870    //XXX: The marking part is separate, if MDP comp wants
871    // to use it in the future. Right now getting MDP comp
872    // to use this is more trouble than it is worth.
873    markCachedLayersAsOverlay(list);
874}
875
876void LayerCache::markCachedLayersAsOverlay(hwc_display_contents_1_t* list) {
877    //This optimization only works if ALL the layer handles
878    //that were on the framebuffer didn't change.
879    if(canUseLayerCache){
880        for(uint32_t i = 0; i < list->numHwLayers; i++) {
881            if (list->hwLayers[i].handle &&
882                list->hwLayers[i].handle == hnd[i] &&
883                list->hwLayers[i].compositionType != HWC_FRAMEBUFFER_TARGET)
884            {
885                list->hwLayers[i].compositionType = HWC_OVERLAY;
886            }
887        }
888    }
889}
890
891};//namespace qhwc
892