hwc.cpp revision 742ad624e555511b687879e1b3256ee21bf7f9bf
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 ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)
21#include <fcntl.h>
22#include <errno.h>
23
24#include <cutils/log.h>
25#include <cutils/atomic.h>
26#include <EGL/egl.h>
27#include <utils/Trace.h>
28#include <sys/ioctl.h>
29#include <overlay.h>
30#include <overlayRotator.h>
31#include <mdp_version.h>
32#include "hwc_utils.h"
33#include "hwc_fbupdate.h"
34#include "hwc_mdpcomp.h"
35#include "external.h"
36#include "hwc_copybit.h"
37#include "profiler.h"
38
39using namespace qhwc;
40using namespace overlay;
41
42#define VSYNC_DEBUG 0
43#define BLANK_DEBUG 1
44
45static int hwc_device_open(const struct hw_module_t* module,
46                           const char* name,
47                           struct hw_device_t** device);
48
49static struct hw_module_methods_t hwc_module_methods = {
50    open: hwc_device_open
51};
52
53hwc_module_t HAL_MODULE_INFO_SYM = {
54    common: {
55        tag: HARDWARE_MODULE_TAG,
56        version_major: 2,
57        version_minor: 0,
58        id: HWC_HARDWARE_MODULE_ID,
59        name: "Qualcomm Hardware Composer Module",
60        author: "CodeAurora Forum",
61        methods: &hwc_module_methods,
62        dso: 0,
63        reserved: {0},
64    }
65};
66
67/*
68 * Save callback functions registered to HWC
69 */
70static void hwc_registerProcs(struct hwc_composer_device_1* dev,
71                              hwc_procs_t const* procs)
72{
73    ALOGI("%s", __FUNCTION__);
74    hwc_context_t* ctx = (hwc_context_t*)(dev);
75    if(!ctx) {
76        ALOGE("%s: Invalid context", __FUNCTION__);
77        return;
78    }
79    ctx->proc = procs;
80
81    // Now that we have the functions needed, kick off
82    // the uevent & vsync threads
83    init_uevent_thread(ctx);
84    init_vsync_thread(ctx);
85}
86
87//Helper
88static void reset(hwc_context_t *ctx, int numDisplays,
89                  hwc_display_contents_1_t** displays) {
90    for(int i = 0; i < MAX_DISPLAYS; i++) {
91        hwc_display_contents_1_t *list = displays[i];
92        // XXX:SurfaceFlinger no longer guarantees that this
93        // value is reset on every prepare. However, for the layer
94        // cache we need to reset it.
95        // We can probably rethink that later on
96        if (LIKELY(list && list->numHwLayers > 1)) {
97            for(uint32_t j = 0; j < list->numHwLayers; j++) {
98                if(list->hwLayers[j].compositionType != HWC_FRAMEBUFFER_TARGET)
99                    list->hwLayers[j].compositionType = HWC_FRAMEBUFFER;
100            }
101        }
102
103        if(ctx->mFBUpdate[i])
104            ctx->mFBUpdate[i]->reset();
105        if(ctx->mCopyBit[i])
106            ctx->mCopyBit[i]->reset();
107        if(ctx->mLayerRotMap[i])
108            ctx->mLayerRotMap[i]->reset();
109    }
110}
111
112//clear prev layer prop flags and realloc for current frame
113static void reset_layer_prop(hwc_context_t* ctx, int dpy, int numAppLayers) {
114    if(ctx->layerProp[dpy]) {
115       delete[] ctx->layerProp[dpy];
116       ctx->layerProp[dpy] = NULL;
117    }
118    ctx->layerProp[dpy] = new LayerProp[numAppLayers];
119}
120
121static int display_commit(hwc_context_t *ctx, int dpy) {
122    int fbFd = ctx->dpyAttr[dpy].fd;
123    if(fbFd == -1) {
124        ALOGE("%s: Invalid FB fd for display: %d", __FUNCTION__, dpy);
125        return -1;
126    }
127
128    struct mdp_display_commit commit_info;
129    memset(&commit_info, 0, sizeof(struct mdp_display_commit));
130    commit_info.flags = MDP_DISPLAY_COMMIT_OVERLAY;
131    if(ioctl(fbFd, MSMFB_DISPLAY_COMMIT, &commit_info) == -1) {
132       ALOGE("%s: MSMFB_DISPLAY_COMMIT for primary failed", __FUNCTION__);
133       return -errno;
134    }
135    return 0;
136}
137
138static int hwc_prepare_primary(hwc_composer_device_1 *dev,
139        hwc_display_contents_1_t *list) {
140    hwc_context_t* ctx = (hwc_context_t*)(dev);
141    const int dpy = HWC_DISPLAY_PRIMARY;
142    if(UNLIKELY(!ctx->mBasePipeSetup))
143        setupBasePipe(ctx);
144    if (LIKELY(list && list->numHwLayers > 1) &&
145            ctx->dpyAttr[dpy].isActive) {
146        reset_layer_prop(ctx, dpy, list->numHwLayers - 1);
147        uint32_t last = list->numHwLayers - 1;
148        hwc_layer_1_t *fbLayer = &list->hwLayers[last];
149        if(fbLayer->handle) {
150            setListStats(ctx, list, dpy);
151            int fbZOrder = ctx->mMDPComp[dpy]->prepare(ctx, list);
152            if(fbZOrder >= 0)
153                ctx->mFBUpdate[dpy]->prepare(ctx, list, fbZOrder);
154
155            if (ctx->mMDP.version < qdutils::MDP_V4_0) {
156                if((fbZOrder >= 0) && ctx->mCopyBit[dpy])
157                    ctx->mCopyBit[dpy]->prepare(ctx, list, dpy);
158            }
159        }
160    }
161    return 0;
162}
163
164static int hwc_prepare_external(hwc_composer_device_1 *dev,
165        hwc_display_contents_1_t *list, int dpy) {
166
167    hwc_context_t* ctx = (hwc_context_t*)(dev);
168    Locker::Autolock _l(ctx->mExtLock);
169
170    if (LIKELY(list && list->numHwLayers > 1) &&
171            ctx->dpyAttr[dpy].isActive &&
172            ctx->dpyAttr[dpy].connected) {
173        reset_layer_prop(ctx, dpy, list->numHwLayers - 1);
174        uint32_t last = list->numHwLayers - 1;
175        hwc_layer_1_t *fbLayer = &list->hwLayers[last];
176        if(!ctx->dpyAttr[dpy].isPause) {
177            if(fbLayer->handle) {
178                ctx->mExtDispConfiguring = false;
179                setListStats(ctx, list, dpy);
180                int fbZOrder = ctx->mMDPComp[dpy]->prepare(ctx, list);
181                if(fbZOrder >= 0)
182                    ctx->mFBUpdate[dpy]->prepare(ctx, list, fbZOrder);
183
184                /* Temporarily commenting out C2D until we support partial
185                   copybit composition for mixed mode MDP
186
187                if((fbZOrder >= 0) && ctx->mCopyBit[dpy])
188                    ctx->mCopyBit[dpy]->prepare(ctx, list, dpy);
189                */
190            }
191        } else {
192            // External Display is in Pause state.
193            // ToDo:
194            // Mark all application layers as OVERLAY so that
195            // GPU will not compose. This is done for power
196            // optimization
197        }
198    }
199    return 0;
200}
201
202static int hwc_prepare_virtual(hwc_composer_device_1 *dev,
203                               hwc_display_contents_1_t *list, int dpy) {
204    //XXX: Fix when framework support is added
205    return 0;
206}
207
208static int hwc_prepare(hwc_composer_device_1 *dev, size_t numDisplays,
209                       hwc_display_contents_1_t** displays)
210{
211    int ret = 0;
212    hwc_context_t* ctx = (hwc_context_t*)(dev);
213    Locker::Autolock _l(ctx->mBlankLock);
214    reset(ctx, numDisplays, displays);
215
216    ctx->mOverlay->configBegin();
217    ctx->mRotMgr->configBegin();
218    Overlay::setDMAMode(Overlay::DMA_LINE_MODE);
219
220    for (int32_t i = numDisplays - 1; i >= 0; i--) {
221        hwc_display_contents_1_t *list = displays[i];
222        switch(i) {
223            case HWC_DISPLAY_PRIMARY:
224                ret = hwc_prepare_primary(dev, list);
225                break;
226            case HWC_DISPLAY_EXTERNAL:
227                ret = hwc_prepare_external(dev, list, i);
228                break;
229            case HWC_DISPLAY_VIRTUAL:
230                ret = hwc_prepare_virtual(dev, list, i);
231                break;
232            default:
233                ret = -EINVAL;
234        }
235    }
236
237    ctx->mOverlay->configDone();
238    ctx->mRotMgr->configDone();
239
240    return ret;
241}
242
243static int hwc_eventControl(struct hwc_composer_device_1* dev, int dpy,
244                             int event, int enable)
245{
246    int ret = 0;
247    hwc_context_t* ctx = (hwc_context_t*)(dev);
248    switch(event) {
249        case HWC_EVENT_VSYNC:
250            if(!ctx->dpyAttr[dpy].isActive) {
251                ALOGE("Display is blanked - Cannot %s vsync",
252                        enable ? "enable" : "disable");
253                return -EINVAL;
254            }
255
256            if (ctx->vstate.enable == enable)
257                break;
258            ret = hwc_vsync_control(ctx, dpy, enable);
259            if(ret == 0)
260                ctx->vstate.enable = !!enable;
261            ALOGD_IF (VSYNC_DEBUG, "VSYNC state changed to %s",
262                      (enable)?"ENABLED":"DISABLED");
263            break;
264        default:
265            ret = -EINVAL;
266    }
267    return ret;
268}
269
270static int hwc_blank(struct hwc_composer_device_1* dev, int dpy, int blank)
271{
272    ATRACE_CALL();
273    hwc_context_t* ctx = (hwc_context_t*)(dev);
274
275    Locker::Autolock _l(ctx->mBlankLock);
276    int ret = 0;
277    ALOGD_IF(BLANK_DEBUG, "%s: %s display: %d", __FUNCTION__,
278          blank==1 ? "Blanking":"Unblanking", dpy);
279    if(blank) {
280        // free up all the overlay pipes in use
281        // when we get a blank for either display
282        // makes sure that all pipes are freed
283        ctx->mOverlay->configBegin();
284        ctx->mOverlay->configDone();
285        ctx->mRotMgr->clear();
286    }
287    switch(dpy) {
288        case HWC_DISPLAY_PRIMARY:
289            if(blank) {
290                ret = ioctl(ctx->dpyAttr[dpy].fd, FBIOBLANK,
291                            FB_BLANK_POWERDOWN);
292            } else {
293                ret = ioctl(ctx->dpyAttr[dpy].fd, FBIOBLANK,FB_BLANK_UNBLANK);
294            }
295            break;
296        case HWC_DISPLAY_EXTERNAL:
297        case HWC_DISPLAY_VIRTUAL:
298            if(blank) {
299                // call external framebuffer commit on blank,
300                // so that any pipe unsets gets committed
301                if (display_commit(ctx, dpy) < 0) {
302                    ret = -1;
303                    ALOGE("%s:post failed for external display !! ",
304                          __FUNCTION__);
305                }
306            } else {
307            }
308            break;
309        default:
310            return -EINVAL;
311    }
312    // Enable HPD here, as during bootup unblank is called
313    // when SF is completely initialized
314    ctx->mExtDisplay->setHPD(1);
315    if(ret == 0){
316        ctx->dpyAttr[dpy].isActive = !blank;
317    } else {
318        ALOGE("%s: Failed in %s display: %d error:%s", __FUNCTION__,
319              blank==1 ? "blanking":"unblanking", dpy, strerror(errno));
320        return ret;
321    }
322
323    ALOGD_IF(BLANK_DEBUG, "%s: Done %s display: %d", __FUNCTION__,
324          blank==1 ? "blanking":"unblanking", dpy);
325    return 0;
326}
327
328static int hwc_query(struct hwc_composer_device_1* dev,
329                     int param, int* value)
330{
331    hwc_context_t* ctx = (hwc_context_t*)(dev);
332    int supported = HWC_DISPLAY_PRIMARY_BIT;
333
334    switch (param) {
335    case HWC_BACKGROUND_LAYER_SUPPORTED:
336        // Not supported for now
337        value[0] = 0;
338        break;
339    case HWC_DISPLAY_TYPES_SUPPORTED:
340        if(ctx->mMDP.hasOverlay)
341            supported |= HWC_DISPLAY_EXTERNAL_BIT;
342        value[0] = supported;
343        break;
344    default:
345        return -EINVAL;
346    }
347    return 0;
348
349}
350
351
352static int hwc_set_primary(hwc_context_t *ctx, hwc_display_contents_1_t* list) {
353    ATRACE_CALL();
354    int ret = 0;
355    const int dpy = HWC_DISPLAY_PRIMARY;
356
357    if (LIKELY(list) && ctx->dpyAttr[dpy].isActive) {
358        uint32_t last = list->numHwLayers - 1;
359        hwc_layer_1_t *fbLayer = &list->hwLayers[last];
360        int fd = -1; //FenceFD from the Copybit(valid in async mode)
361        bool copybitDone = false;
362        if(ctx->mCopyBit[dpy])
363            copybitDone = ctx->mCopyBit[dpy]->draw(ctx, list, dpy, &fd);
364        if(list->numHwLayers > 1)
365            hwc_sync(ctx, list, dpy, fd);
366
367        if (!ctx->mMDPComp[dpy]->draw(ctx, list)) {
368            ALOGE("%s: MDPComp draw failed", __FUNCTION__);
369            ret = -1;
370        }
371
372        //TODO We dont check for SKIP flag on this layer because we need PAN
373        //always. Last layer is always FB
374        private_handle_t *hnd = (private_handle_t *)fbLayer->handle;
375        if(copybitDone) {
376            hnd = ctx->mCopyBit[dpy]->getCurrentRenderBuffer();
377        }
378
379        if(hnd) {
380            if (!ctx->mFBUpdate[dpy]->draw(ctx, hnd)) {
381                ALOGE("%s: FBUpdate draw failed", __FUNCTION__);
382                ret = -1;
383            }
384        }
385
386        if (display_commit(ctx, dpy) < 0) {
387            ALOGE("%s: display commit fail!", __FUNCTION__);
388            ret = -1;
389        }
390    }
391
392    closeAcquireFds(list);
393    return ret;
394}
395
396static int hwc_set_external(hwc_context_t *ctx,
397                            hwc_display_contents_1_t* list, int dpy)
398{
399    ATRACE_CALL();
400    int ret = 0;
401    Locker::Autolock _l(ctx->mExtLock);
402
403    if (LIKELY(list) && ctx->dpyAttr[dpy].isActive &&
404        !ctx->dpyAttr[dpy].isPause &&
405        ctx->dpyAttr[dpy].connected) {
406        uint32_t last = list->numHwLayers - 1;
407        hwc_layer_1_t *fbLayer = &list->hwLayers[last];
408        int fd = -1; //FenceFD from the Copybit(valid in async mode)
409        bool copybitDone = false;
410        if(ctx->mCopyBit[dpy])
411            copybitDone = ctx->mCopyBit[dpy]->draw(ctx, list, dpy, &fd);
412
413        if(list->numHwLayers > 1)
414            hwc_sync(ctx, list, dpy, fd);
415
416        if (!ctx->mMDPComp[dpy]->draw(ctx, list)) {
417            ALOGE("%s: MDPComp draw failed", __FUNCTION__);
418            ret = -1;
419        }
420
421        private_handle_t *hnd = (private_handle_t *)fbLayer->handle;
422        if(copybitDone) {
423            hnd = ctx->mCopyBit[dpy]->getCurrentRenderBuffer();
424        }
425
426        if(hnd) {
427            if (!ctx->mFBUpdate[dpy]->draw(ctx, hnd)) {
428                ALOGE("%s: FBUpdate::draw fail!", __FUNCTION__);
429                ret = -1;
430            }
431        }
432
433        if (display_commit(ctx, dpy) < 0) {
434            ALOGE("%s: display commit fail!", __FUNCTION__);
435            ret = -1;
436        }
437    }
438
439    closeAcquireFds(list);
440    return ret;
441}
442
443static int hwc_set_virtual(hwc_context_t *ctx,
444                           hwc_display_contents_1_t* list, int dpy)
445{
446    //XXX: Implement set.
447    closeAcquireFds(list);
448    if (list) {
449        // SF assumes HWC waits for the acquire fence and returns a new fence
450        // that signals when we're done. Since we don't wait, and also don't
451        // touch the buffer, we can just handle the acquire fence back to SF
452        // as the retire fence.
453        list->retireFenceFd = list->outbufAcquireFenceFd;
454    }
455    return 0;
456}
457
458
459static int hwc_set(hwc_composer_device_1 *dev,
460                   size_t numDisplays,
461                   hwc_display_contents_1_t** displays)
462{
463    int ret = 0;
464    hwc_context_t* ctx = (hwc_context_t*)(dev);
465    Locker::Autolock _l(ctx->mBlankLock);
466    for (uint32_t i = 0; i < numDisplays; i++) {
467        hwc_display_contents_1_t* list = displays[i];
468        switch(i) {
469            case HWC_DISPLAY_PRIMARY:
470                ret = hwc_set_primary(ctx, list);
471                break;
472            case HWC_DISPLAY_EXTERNAL:
473                ret = hwc_set_external(ctx, list, i);
474                break;
475            case HWC_DISPLAY_VIRTUAL:
476                ret = hwc_set_virtual(ctx, list, i);
477                break;
478            default:
479                ret = -EINVAL;
480        }
481    }
482    // This is only indicative of how many times SurfaceFlinger posts
483    // frames to the display.
484    CALC_FPS();
485    MDPComp::resetIdleFallBack();
486    ctx->mVideoTransFlag = false;
487    return ret;
488}
489
490int hwc_getDisplayConfigs(struct hwc_composer_device_1* dev, int disp,
491        uint32_t* configs, size_t* numConfigs) {
492    int ret = 0;
493    hwc_context_t* ctx = (hwc_context_t*)(dev);
494    //in 1.1 there is no way to choose a config, report as config id # 0
495    //This config is passed to getDisplayAttributes. Ignore for now.
496    switch(disp) {
497        case HWC_DISPLAY_PRIMARY:
498            if(*numConfigs > 0) {
499                configs[0] = 0;
500                *numConfigs = 1;
501            }
502            ret = 0; //NO_ERROR
503            break;
504        case HWC_DISPLAY_EXTERNAL:
505            ret = -1; //Not connected
506            if(ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].connected) {
507                ret = 0; //NO_ERROR
508                if(*numConfigs > 0) {
509                    configs[0] = 0;
510                    *numConfigs = 1;
511                }
512            }
513            break;
514    }
515    return ret;
516}
517
518int hwc_getDisplayAttributes(struct hwc_composer_device_1* dev, int disp,
519        uint32_t config, const uint32_t* attributes, int32_t* values) {
520
521    hwc_context_t* ctx = (hwc_context_t*)(dev);
522    //If hotpluggable displays are inactive return error
523    if(disp == HWC_DISPLAY_EXTERNAL && !ctx->dpyAttr[disp].connected) {
524        return -1;
525    }
526
527    //From HWComposer
528    static const uint32_t DISPLAY_ATTRIBUTES[] = {
529        HWC_DISPLAY_VSYNC_PERIOD,
530        HWC_DISPLAY_WIDTH,
531        HWC_DISPLAY_HEIGHT,
532        HWC_DISPLAY_DPI_X,
533        HWC_DISPLAY_DPI_Y,
534        HWC_DISPLAY_NO_ATTRIBUTE,
535    };
536
537    const int NUM_DISPLAY_ATTRIBUTES = (sizeof(DISPLAY_ATTRIBUTES) /
538            sizeof(DISPLAY_ATTRIBUTES)[0]);
539
540    for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
541        switch (attributes[i]) {
542        case HWC_DISPLAY_VSYNC_PERIOD:
543            values[i] = ctx->dpyAttr[disp].vsync_period;
544            break;
545        case HWC_DISPLAY_WIDTH:
546            values[i] = ctx->dpyAttr[disp].xres;
547            ALOGD("%s disp = %d, width = %d",__FUNCTION__, disp,
548                    ctx->dpyAttr[disp].xres);
549            break;
550        case HWC_DISPLAY_HEIGHT:
551            values[i] = ctx->dpyAttr[disp].yres;
552            ALOGD("%s disp = %d, height = %d",__FUNCTION__, disp,
553                    ctx->dpyAttr[disp].yres);
554            break;
555        case HWC_DISPLAY_DPI_X:
556            values[i] = (int32_t) (ctx->dpyAttr[disp].xdpi*1000.0);
557            break;
558        case HWC_DISPLAY_DPI_Y:
559            values[i] = (int32_t) (ctx->dpyAttr[disp].ydpi*1000.0);
560            break;
561        default:
562            ALOGE("Unknown display attribute %d",
563                    attributes[i]);
564            return -EINVAL;
565        }
566    }
567    return 0;
568}
569
570void hwc_dump(struct hwc_composer_device_1* dev, char *buff, int buff_len)
571{
572    hwc_context_t* ctx = (hwc_context_t*)(dev);
573    android::String8 aBuf("");
574    dumpsys_log(aBuf, "Qualcomm HWC state:\n");
575    dumpsys_log(aBuf, "  MDPVersion=%d\n", ctx->mMDP.version);
576    dumpsys_log(aBuf, "  DisplayPanel=%c\n", ctx->mMDP.panel);
577    for(int dpy = 0; dpy < MAX_DISPLAYS; dpy++) {
578        if(ctx->mMDPComp[dpy])
579            ctx->mMDPComp[dpy]->dump(aBuf);
580    }
581    char ovDump[2048] = {'\0'};
582    ctx->mOverlay->getDump(ovDump, 2048);
583    dumpsys_log(aBuf, ovDump);
584    ovDump[0] = '\0';
585    ctx->mRotMgr->getDump(ovDump, 1024);
586    dumpsys_log(aBuf, ovDump);
587    strlcpy(buff, aBuf.string(), buff_len);
588}
589
590static int hwc_device_close(struct hw_device_t *dev)
591{
592    if(!dev) {
593        ALOGE("%s: NULL device pointer", __FUNCTION__);
594        return -1;
595    }
596    closeContext((hwc_context_t*)dev);
597    free(dev);
598
599    return 0;
600}
601
602static int hwc_device_open(const struct hw_module_t* module, const char* name,
603                           struct hw_device_t** device)
604{
605    int status = -EINVAL;
606
607    if (!strcmp(name, HWC_HARDWARE_COMPOSER)) {
608        struct hwc_context_t *dev;
609        dev = (hwc_context_t*)malloc(sizeof(*dev));
610        memset(dev, 0, sizeof(*dev));
611
612        //Initialize hwc context
613        initContext(dev);
614
615        //Setup HWC methods
616        dev->device.common.tag          = HARDWARE_DEVICE_TAG;
617        dev->device.common.version      = HWC_DEVICE_API_VERSION_1_2;
618        dev->device.common.module       = const_cast<hw_module_t*>(module);
619        dev->device.common.close        = hwc_device_close;
620        dev->device.prepare             = hwc_prepare;
621        dev->device.set                 = hwc_set;
622        dev->device.eventControl        = hwc_eventControl;
623        dev->device.blank               = hwc_blank;
624        dev->device.query               = hwc_query;
625        dev->device.registerProcs       = hwc_registerProcs;
626        dev->device.dump                = hwc_dump;
627        dev->device.getDisplayConfigs   = hwc_getDisplayConfigs;
628        dev->device.getDisplayAttributes = hwc_getDisplayAttributes;
629        *device = &dev->device.common;
630        status = 0;
631    }
632    return status;
633}
634