hwc_utils.cpp revision c4d034f4b8d12953632907d30c0b78856d829579
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
51void initContext(hwc_context_t *ctx)
52{
53    openFramebufferDevice(ctx);
54    overlay::Overlay::initOverlay();
55    for(uint32_t i = 0; i < HWC_NUM_DISPLAY_TYPES; i++) {
56        ctx->mOverlay[i] = overlay::Overlay::getInstance(i);
57    }
58    ctx->mQService = qService::QService::getInstance(ctx);
59    ctx->mMDP.version = qdutils::MDPVersion::getInstance().getMDPVersion();
60    ctx->mMDP.hasOverlay = qdutils::MDPVersion::getInstance().hasOverlay();
61    ctx->mMDP.panel = qdutils::MDPVersion::getInstance().getPanelType();
62    ctx->mExtDisplay = new ExternalDisplay(ctx);
63    MDPComp::init(ctx);
64
65    pthread_mutex_init(&(ctx->vstate.lock), NULL);
66    pthread_cond_init(&(ctx->vstate.cond), NULL);
67    ctx->vstate.enable = false;
68
69    ALOGI("Initializing Qualcomm Hardware Composer");
70    ALOGI("MDP version: %d", ctx->mMDP.version);
71}
72
73void closeContext(hwc_context_t *ctx)
74{
75    for(uint32_t i = 0; i < HWC_NUM_DISPLAY_TYPES; i++) {
76        if(ctx->mOverlay[i]) {
77            delete ctx->mOverlay[i];
78            ctx->mOverlay[i] = NULL;
79        }
80    }
81
82    if(ctx->mFbDev) {
83        framebuffer_close(ctx->mFbDev);
84        ctx->mFbDev = NULL;
85        close(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd);
86        ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = -1;
87    }
88
89    if(ctx->mExtDisplay) {
90        delete ctx->mExtDisplay;
91        ctx->mExtDisplay = NULL;
92    }
93
94    pthread_mutex_destroy(&(ctx->vstate.lock));
95    pthread_cond_destroy(&(ctx->vstate.cond));
96
97}
98
99void dumpLayer(hwc_layer_1_t const* l)
100{
101    ALOGD("\ttype=%d, flags=%08x, handle=%p, tr=%02x, blend=%04x, {%d,%d,%d,%d}"
102          ", {%d,%d,%d,%d}",
103          l->compositionType, l->flags, l->handle, l->transform, l->blending,
104          l->sourceCrop.left,
105          l->sourceCrop.top,
106          l->sourceCrop.right,
107          l->sourceCrop.bottom,
108          l->displayFrame.left,
109          l->displayFrame.top,
110          l->displayFrame.right,
111          l->displayFrame.bottom);
112}
113
114void setListStats(hwc_context_t *ctx,
115        const hwc_display_contents_1_t *list, int dpy) {
116
117    ctx->listStats[dpy].numAppLayers = list->numHwLayers - 1;
118    ctx->listStats[dpy].fbLayerIndex = list->numHwLayers - 1;
119    ctx->listStats[dpy].yuvCount = 0;
120    ctx->listStats[dpy].yuvIndex = -1;
121    ctx->listStats[dpy].skipCount = 0;
122
123    for (size_t i = 0; i < list->numHwLayers; i++) {
124        private_handle_t *hnd =
125            (private_handle_t *)list->hwLayers[i].handle;
126
127        if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
128            continue;
129        //We disregard FB being skip for now! so the else if
130        } else if (isSkipLayer(&list->hwLayers[i])) {
131            ctx->listStats[dpy].skipCount++;
132        }
133
134        if (UNLIKELY(isYuvBuffer(hnd))) {
135            ctx->listStats[dpy].yuvCount++;
136            ctx->listStats[dpy].yuvIndex = i;
137        }
138    }
139}
140
141//Crops source buffer against destination and FB boundaries
142void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
143        const int fbWidth, const int fbHeight) {
144
145    int& crop_x = crop.left;
146    int& crop_y = crop.top;
147    int& crop_r = crop.right;
148    int& crop_b = crop.bottom;
149    int crop_w = crop.right - crop.left;
150    int crop_h = crop.bottom - crop.top;
151
152    int& dst_x = dst.left;
153    int& dst_y = dst.top;
154    int& dst_r = dst.right;
155    int& dst_b = dst.bottom;
156    int dst_w = dst.right - dst.left;
157    int dst_h = dst.bottom - dst.top;
158
159    if(dst_x < 0) {
160        float scale_x =  crop_w * 1.0f / dst_w;
161        float diff_factor = (scale_x * abs(dst_x));
162        crop_x = crop_x + (int)diff_factor;
163        crop_w = crop_r - crop_x;
164
165        dst_x = 0;
166        dst_w = dst_r - dst_x;;
167    }
168    if(dst_r > fbWidth) {
169        float scale_x = crop_w * 1.0f / dst_w;
170        float diff_factor = scale_x * (dst_r - fbWidth);
171        crop_r = crop_r - diff_factor;
172        crop_w = crop_r - crop_x;
173
174        dst_r = fbWidth;
175        dst_w = dst_r - dst_x;
176    }
177    if(dst_y < 0) {
178        float scale_y = crop_h * 1.0f / dst_h;
179        float diff_factor = scale_y * abs(dst_y);
180        crop_y = crop_y + diff_factor;
181        crop_h = crop_b - crop_y;
182
183        dst_y = 0;
184        dst_h = dst_b - dst_y;
185    }
186    if(dst_b > fbHeight) {
187        float scale_y = crop_h * 1.0f / dst_h;
188        float diff_factor = scale_y * (dst_b - fbHeight);
189        crop_b = crop_b - diff_factor;
190        crop_h = crop_b - crop_y;
191
192        dst_b = fbHeight;
193        dst_h = dst_b - dst_y;
194    }
195}
196
197bool isExternalActive(hwc_context_t* ctx) {
198    return ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive;
199}
200
201int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy) {
202    int ret = 0;
203#ifdef USE_FENCE_SYNC
204    struct mdp_buf_sync data;
205    int acquireFd[4];
206    int count = 0;
207    int releaseFd = -1;
208    int fbFd = -1;
209    data.flags = MDP_BUF_SYNC_FLAG_WAIT;
210    data.acq_fen_fd = acquireFd;
211    data.rel_fen_fd = &releaseFd;
212    //Accumulate acquireFenceFds
213    for(uint32_t i = 0; i < list->numHwLayers; i++) {
214        if((list->hwLayers[i].compositionType == HWC_OVERLAY ||
215            list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) &&
216            list->hwLayers[i].acquireFenceFd != -1) {
217            acquireFd[count++] = list->hwLayers[i].acquireFenceFd;
218        }
219    }
220
221    if (count) {
222        data.acq_fen_fd_cnt = count;
223        fbFd = ctx->dpyAttr[dpy].fd;
224
225        //Waits for acquire fences, returns a release fence
226        ret = ioctl(fbFd, MSMFB_BUFFER_SYNC, &data);
227        if(ret < 0) {
228            ALOGE("ioctl MSMFB_BUFFER_SYNC failed, err=%s",
229                    strerror(errno));
230        }
231
232        for(uint32_t i = 0; i < list->numHwLayers; i++) {
233            if((list->hwLayers[i].compositionType == HWC_OVERLAY ||
234                list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET)) {
235                //Close the acquireFenceFds
236                if(list->hwLayers[i].acquireFenceFd > 0) {
237                    close(list->hwLayers[i].acquireFenceFd);
238                    list->hwLayers[i].acquireFenceFd = -1;
239                }
240                //Populate releaseFenceFds.
241                list->hwLayers[i].releaseFenceFd = dup(releaseFd);
242            }
243        }
244        list->retireFenceFd = releaseFd;
245    }
246#endif
247    return ret;
248}
249
250};//namespace
251