hwc_utils.cpp revision 541b59d15504bb6e5b9fbeb031aa2287a1abed2a
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 * Copyright (C) 2012, The Linux Foundation All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include <EGL/egl.h>
19#include <overlay.h>
20#include <cutils/properties.h>
21#include <gralloc_priv.h>
22#include <fb_priv.h>
23#include "hwc_utils.h"
24#include "mdp_version.h"
25#include "hwc_video.h"
26#include "external.h"
27#include "hwc_mdpcomp.h"
28#include "QService.h"
29
30namespace qhwc {
31
32// Opens Framebuffer device
33static void openFramebufferDevice(hwc_context_t *ctx)
34{
35    hw_module_t const *module;
36    if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
37        framebuffer_open(module, &(ctx->mFbDev));
38        private_module_t* m = reinterpret_cast<private_module_t*>(
39                ctx->mFbDev->common.module);
40        //xres, yres may not be 32 aligned
41        ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres = m->info.xres;
42        ctx->dpyAttr[HWC_DISPLAY_PRIMARY].yres = m->info.yres;
43        ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xdpi = ctx->mFbDev->xdpi;
44        ctx->dpyAttr[HWC_DISPLAY_PRIMARY].ydpi = ctx->mFbDev->ydpi;
45        ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period =
46                1000000000l / ctx->mFbDev->fps;
47        ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = openFb(HWC_DISPLAY_PRIMARY);
48    }
49}
50
51static void onExtDisconnect(const hwc_context_t::Callbacks& priv_proc) {
52    hwc_context_t *ctx = priv_proc.ctx;
53    overlay::Overlay& ov = *(ctx->mOverlay[HWC_DISPLAY_EXTERNAL]);
54    // Set overlay state
55    ov.setState(ovutils::OV_CLOSED);
56}
57
58void initContext(hwc_context_t *ctx)
59{
60    openFramebufferDevice(ctx);
61    overlay::Overlay::initOverlay();
62    for(uint32_t i = 0; i < HWC_NUM_DISPLAY_TYPES; i++) {
63        ctx->mOverlay[i] = overlay::Overlay::getInstance(i);
64    }
65    ctx->mQService = qService::QService::getInstance(ctx);
66    ctx->mMDP.version = qdutils::MDPVersion::getInstance().getMDPVersion();
67    ctx->mMDP.hasOverlay = qdutils::MDPVersion::getInstance().hasOverlay();
68    ctx->mMDP.panel = qdutils::MDPVersion::getInstance().getPanelType();
69    ctx->mExtDisplay = new ExternalDisplay(ctx);
70    MDPComp::init(ctx);
71
72    pthread_mutex_init(&(ctx->vstate.lock), NULL);
73    pthread_cond_init(&(ctx->vstate.cond), NULL);
74    ctx->vstate.enable = false;
75
76    ctx->priv_proc.onExtDisconnect = onExtDisconnect;
77    ctx->priv_proc.ctx = ctx;
78
79    ALOGI("Initializing Qualcomm Hardware Composer");
80    ALOGI("MDP version: %d", ctx->mMDP.version);
81}
82
83void closeContext(hwc_context_t *ctx)
84{
85    for(uint32_t i = 0; i < HWC_NUM_DISPLAY_TYPES; i++) {
86        if(ctx->mOverlay[i]) {
87            delete ctx->mOverlay[i];
88            ctx->mOverlay[i] = NULL;
89        }
90    }
91
92    if(ctx->mFbDev) {
93        framebuffer_close(ctx->mFbDev);
94        ctx->mFbDev = NULL;
95        close(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd);
96        ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = -1;
97    }
98
99    if(ctx->mExtDisplay) {
100        delete ctx->mExtDisplay;
101        ctx->mExtDisplay = NULL;
102    }
103
104    pthread_mutex_destroy(&(ctx->vstate.lock));
105    pthread_cond_destroy(&(ctx->vstate.cond));
106
107}
108
109void dumpLayer(hwc_layer_1_t const* l)
110{
111    ALOGD("\ttype=%d, flags=%08x, handle=%p, tr=%02x, blend=%04x, {%d,%d,%d,%d}"
112          ", {%d,%d,%d,%d}",
113          l->compositionType, l->flags, l->handle, l->transform, l->blending,
114          l->sourceCrop.left,
115          l->sourceCrop.top,
116          l->sourceCrop.right,
117          l->sourceCrop.bottom,
118          l->displayFrame.left,
119          l->displayFrame.top,
120          l->displayFrame.right,
121          l->displayFrame.bottom);
122}
123
124void setListStats(hwc_context_t *ctx,
125        const hwc_display_contents_1_t *list, int dpy) {
126
127    ctx->listStats[dpy].numAppLayers = list->numHwLayers - 1;
128    ctx->listStats[dpy].fbLayerIndex = list->numHwLayers - 1;
129    ctx->listStats[dpy].yuvCount = 0;
130    ctx->listStats[dpy].yuvIndex = -1;
131    ctx->listStats[dpy].skipCount = 0;
132
133    for (size_t i = 0; i < list->numHwLayers; i++) {
134        private_handle_t *hnd =
135            (private_handle_t *)list->hwLayers[i].handle;
136
137        if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
138            continue;
139        //We disregard FB being skip for now! so the else if
140        } else if (isSkipLayer(&list->hwLayers[i])) {
141            ctx->listStats[dpy].skipCount++;
142        }
143
144        if (UNLIKELY(isYuvBuffer(hnd))) {
145            ctx->listStats[dpy].yuvCount++;
146            ctx->listStats[dpy].yuvIndex = i;
147        }
148    }
149}
150
151static inline void calc_cut(float& leftCutRatio, float& topCutRatio,
152        float& rightCutRatio, float& bottomCutRatio, int orient) {
153    if(orient & HAL_TRANSFORM_FLIP_H) {
154        swap(leftCutRatio, rightCutRatio);
155    }
156    if(orient & HAL_TRANSFORM_FLIP_V) {
157        swap(topCutRatio, bottomCutRatio);
158    }
159    if(orient & HAL_TRANSFORM_ROT_90) {
160        //Anti clock swapping
161        float tmpCutRatio = leftCutRatio;
162        leftCutRatio = topCutRatio;
163        topCutRatio = rightCutRatio;
164        rightCutRatio = bottomCutRatio;
165        bottomCutRatio = tmpCutRatio;
166    }
167}
168
169//Crops source buffer against destination and FB boundaries
170void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
171        const int fbWidth, const int fbHeight, int orient) {
172    int& crop_l = crop.left;
173    int& crop_t = crop.top;
174    int& crop_r = crop.right;
175    int& crop_b = crop.bottom;
176    int crop_w = crop.right - crop.left;
177    int crop_h = crop.bottom - crop.top;
178
179    int& dst_l = dst.left;
180    int& dst_t = dst.top;
181    int& dst_r = dst.right;
182    int& dst_b = dst.bottom;
183    int dst_w = abs(dst.right - dst.left);
184    int dst_h = abs(dst.bottom - dst.top);
185
186    float leftCutRatio = 0.0f, rightCutRatio = 0.0f, topCutRatio = 0.0f,
187            bottomCutRatio = 0.0f;
188
189    if(dst_l < 0) {
190        leftCutRatio = (float)(0.0f - dst_l) / (float)dst_w;
191        dst_l = 0;
192    }
193    if(dst_r > fbWidth) {
194        rightCutRatio = (float)(dst_r - fbWidth) / (float)dst_w;
195        dst_r = fbWidth;
196    }
197    if(dst_t < 0) {
198        topCutRatio = (float)(0 - dst_t) / (float)dst_h;
199        dst_t = 0;
200    }
201    if(dst_b > fbHeight) {
202        bottomCutRatio = (float)(dst_b - fbHeight) / (float)dst_h;
203        dst_b = fbHeight;
204    }
205
206    calc_cut(leftCutRatio, topCutRatio, rightCutRatio, bottomCutRatio, orient);
207    crop_l += crop_w * leftCutRatio;
208    crop_t += crop_h * topCutRatio;
209    crop_r -= crop_w * rightCutRatio;
210    crop_b -= crop_h * bottomCutRatio;
211}
212
213bool isExternalActive(hwc_context_t* ctx) {
214    return ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive;
215}
216
217int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy) {
218    int ret = 0;
219#ifdef USE_FENCE_SYNC
220    struct mdp_buf_sync data;
221    int acquireFd[4];
222    int count = 0;
223    int releaseFd = -1;
224    int fbFd = -1;
225    data.flags = MDP_BUF_SYNC_FLAG_WAIT;
226    data.acq_fen_fd = acquireFd;
227    data.rel_fen_fd = &releaseFd;
228    //Accumulate acquireFenceFds
229    for(uint32_t i = 0; i < list->numHwLayers; i++) {
230        if((list->hwLayers[i].compositionType == HWC_OVERLAY ||
231            list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) &&
232            list->hwLayers[i].acquireFenceFd != -1) {
233            acquireFd[count++] = list->hwLayers[i].acquireFenceFd;
234        }
235    }
236
237    if (count) {
238        data.acq_fen_fd_cnt = count;
239        fbFd = ctx->dpyAttr[dpy].fd;
240
241        //Waits for acquire fences, returns a release fence
242        ret = ioctl(fbFd, MSMFB_BUFFER_SYNC, &data);
243        if(ret < 0) {
244            ALOGE("ioctl MSMFB_BUFFER_SYNC failed, err=%s",
245                    strerror(errno));
246        }
247
248        for(uint32_t i = 0; i < list->numHwLayers; i++) {
249            if((list->hwLayers[i].compositionType == HWC_OVERLAY ||
250                list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET)) {
251                //Close the acquireFenceFds
252                if(list->hwLayers[i].acquireFenceFd > 0) {
253                    close(list->hwLayers[i].acquireFenceFd);
254                    list->hwLayers[i].acquireFenceFd = -1;
255                }
256                //Populate releaseFenceFds.
257                list->hwLayers[i].releaseFenceFd = dup(releaseFd);
258            }
259        }
260        list->retireFenceFd = releaseFd;
261    }
262#endif
263    return ret;
264}
265
266};//namespace
267