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