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