Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
Saurabh Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 3 | * Copyright (C) 2012, The Linux Foundation All rights reserved. |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 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 | |
Naseer Ahmed | 5b6708a | 2012-08-02 13:46:08 -0700 | [diff] [blame] | 18 | #include <EGL/egl.h> |
Naseer Ahmed | 72cf976 | 2012-07-21 12:17:13 -0700 | [diff] [blame] | 19 | #include <overlay.h> |
Saurabh Shah | fc2acbe | 2012-08-17 19:47:52 -0700 | [diff] [blame] | 20 | #include <cutils/properties.h> |
| 21 | #include <gralloc_priv.h> |
| 22 | #include <fb_priv.h> |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 23 | #include "hwc_utils.h" |
Naseer Ahmed | a87da60 | 2012-07-01 23:54:19 -0700 | [diff] [blame] | 24 | #include "mdp_version.h" |
Naseer Ahmed | f48aef6 | 2012-07-20 09:05:53 -0700 | [diff] [blame] | 25 | #include "hwc_video.h" |
Saurabh Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 26 | #include "external.h" |
Naseer Ahmed | 7c958d4 | 2012-07-31 18:57:03 -0700 | [diff] [blame] | 27 | #include "hwc_mdpcomp.h" |
Saurabh Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 28 | #include "QService.h" |
Naseer Ahmed | 72cf976 | 2012-07-21 12:17:13 -0700 | [diff] [blame] | 29 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 30 | namespace qhwc { |
Naseer Ahmed | 72cf976 | 2012-07-21 12:17:13 -0700 | [diff] [blame] | 31 | |
| 32 | // Opens Framebuffer device |
| 33 | static 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)); |
Saurabh Shah | 3e858eb | 2012-09-17 16:53:21 -0700 | [diff] [blame] | 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); |
Naseer Ahmed | 72cf976 | 2012-07-21 12:17:13 -0700 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 51 | void initContext(hwc_context_t *ctx) |
| 52 | { |
Naseer Ahmed | 72cf976 | 2012-07-21 12:17:13 -0700 | [diff] [blame] | 53 | openFramebufferDevice(ctx); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 54 | ctx->mOverlay = overlay::Overlay::getInstance(); |
Saurabh Shah | 56f610d | 2012-08-07 15:27:06 -0700 | [diff] [blame] | 55 | ctx->mQService = qService::QService::getInstance(ctx); |
Naseer Ahmed | 96c4c95 | 2012-07-25 18:27:14 -0700 | [diff] [blame] | 56 | ctx->mMDP.version = qdutils::MDPVersion::getInstance().getMDPVersion(); |
| 57 | ctx->mMDP.hasOverlay = qdutils::MDPVersion::getInstance().hasOverlay(); |
| 58 | ctx->mMDP.panel = qdutils::MDPVersion::getInstance().getPanelType(); |
Naseer Ahmed | 72cf976 | 2012-07-21 12:17:13 -0700 | [diff] [blame] | 59 | ctx->mExtDisplay = new ExternalDisplay(ctx); |
Naseer Ahmed | 7c958d4 | 2012-07-31 18:57:03 -0700 | [diff] [blame] | 60 | MDPComp::init(ctx); |
Naseer Ahmed | 72cf976 | 2012-07-21 12:17:13 -0700 | [diff] [blame] | 61 | |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame^] | 62 | pthread_mutex_init(&(ctx->vstate.lock), NULL); |
| 63 | pthread_cond_init(&(ctx->vstate.cond), NULL); |
| 64 | ctx->vstate.enable = false; |
| 65 | |
Naseer Ahmed | 72cf976 | 2012-07-21 12:17:13 -0700 | [diff] [blame] | 66 | ALOGI("Initializing Qualcomm Hardware Composer"); |
Naseer Ahmed | 96c4c95 | 2012-07-25 18:27:14 -0700 | [diff] [blame] | 67 | ALOGI("MDP version: %d", ctx->mMDP.version); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void closeContext(hwc_context_t *ctx) |
| 71 | { |
| 72 | if(ctx->mOverlay) { |
| 73 | delete ctx->mOverlay; |
| 74 | ctx->mOverlay = NULL; |
| 75 | } |
Naseer Ahmed | f48aef6 | 2012-07-20 09:05:53 -0700 | [diff] [blame] | 76 | |
Naseer Ahmed | 72cf976 | 2012-07-21 12:17:13 -0700 | [diff] [blame] | 77 | if(ctx->mFbDev) { |
| 78 | framebuffer_close(ctx->mFbDev); |
| 79 | ctx->mFbDev = NULL; |
Saurabh Shah | 3e858eb | 2012-09-17 16:53:21 -0700 | [diff] [blame] | 80 | close(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd); |
| 81 | ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = -1; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 82 | } |
Naseer Ahmed | 72cf976 | 2012-07-21 12:17:13 -0700 | [diff] [blame] | 83 | |
| 84 | if(ctx->mExtDisplay) { |
| 85 | delete ctx->mExtDisplay; |
| 86 | ctx->mExtDisplay = NULL; |
| 87 | } |
Naseer Ahmed | ff4f025 | 2012-10-01 13:03:01 -0400 | [diff] [blame^] | 88 | |
| 89 | pthread_mutex_destroy(&(ctx->vstate.lock)); |
| 90 | pthread_cond_destroy(&(ctx->vstate.cond)); |
| 91 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Naseer Ahmed | 5b6708a | 2012-08-02 13:46:08 -0700 | [diff] [blame] | 94 | void dumpLayer(hwc_layer_1_t const* l) |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 95 | { |
| 96 | ALOGD("\ttype=%d, flags=%08x, handle=%p, tr=%02x, blend=%04x, {%d,%d,%d,%d}" |
| 97 | ", {%d,%d,%d,%d}", |
| 98 | l->compositionType, l->flags, l->handle, l->transform, l->blending, |
| 99 | l->sourceCrop.left, |
| 100 | l->sourceCrop.top, |
| 101 | l->sourceCrop.right, |
| 102 | l->sourceCrop.bottom, |
| 103 | l->displayFrame.left, |
| 104 | l->displayFrame.top, |
| 105 | l->displayFrame.right, |
| 106 | l->displayFrame.bottom); |
| 107 | } |
| 108 | |
Saurabh Shah | 3e858eb | 2012-09-17 16:53:21 -0700 | [diff] [blame] | 109 | void setListStats(hwc_context_t *ctx, |
| 110 | const hwc_display_contents_1_t *list, int dpy) { |
| 111 | |
| 112 | ctx->listStats[dpy].numAppLayers = list->numHwLayers - 1; |
| 113 | ctx->listStats[dpy].fbLayerIndex = list->numHwLayers - 1; |
Naseer Ahmed | f48aef6 | 2012-07-20 09:05:53 -0700 | [diff] [blame] | 114 | |
| 115 | for (size_t i = 0; i < list->numHwLayers; i++) { |
| 116 | private_handle_t *hnd = |
| 117 | (private_handle_t *)list->hwLayers[i].handle; |
| 118 | |
Saurabh Shah | 3e858eb | 2012-09-17 16:53:21 -0700 | [diff] [blame] | 119 | if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) { |
| 120 | continue; |
| 121 | //We disregard FB being skip for now! so the else if |
Saurabh Shah | 35712cb | 2012-09-14 10:28:18 -0700 | [diff] [blame] | 122 | } else if (isSkipLayer(&list->hwLayers[i])) { |
Saurabh Shah | 3e858eb | 2012-09-17 16:53:21 -0700 | [diff] [blame] | 123 | ctx->listStats[dpy].skipCount++; |
| 124 | } |
| 125 | |
| 126 | if (UNLIKELY(isYuvBuffer(hnd))) { |
| 127 | ctx->listStats[dpy].yuvCount++; |
| 128 | ctx->listStats[dpy].yuvIndex = i; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Naseer Ahmed | f48aef6 | 2012-07-20 09:05:53 -0700 | [diff] [blame] | 133 | //Crops source buffer against destination and FB boundaries |
| 134 | void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst, |
| 135 | const int fbWidth, const int fbHeight) { |
| 136 | |
| 137 | int& crop_x = crop.left; |
| 138 | int& crop_y = crop.top; |
| 139 | int& crop_r = crop.right; |
| 140 | int& crop_b = crop.bottom; |
| 141 | int crop_w = crop.right - crop.left; |
| 142 | int crop_h = crop.bottom - crop.top; |
| 143 | |
| 144 | int& dst_x = dst.left; |
| 145 | int& dst_y = dst.top; |
| 146 | int& dst_r = dst.right; |
| 147 | int& dst_b = dst.bottom; |
| 148 | int dst_w = dst.right - dst.left; |
| 149 | int dst_h = dst.bottom - dst.top; |
| 150 | |
| 151 | if(dst_x < 0) { |
| 152 | float scale_x = crop_w * 1.0f / dst_w; |
| 153 | float diff_factor = (scale_x * abs(dst_x)); |
| 154 | crop_x = crop_x + (int)diff_factor; |
| 155 | crop_w = crop_r - crop_x; |
| 156 | |
| 157 | dst_x = 0; |
| 158 | dst_w = dst_r - dst_x;; |
| 159 | } |
| 160 | if(dst_r > fbWidth) { |
| 161 | float scale_x = crop_w * 1.0f / dst_w; |
| 162 | float diff_factor = scale_x * (dst_r - fbWidth); |
| 163 | crop_r = crop_r - diff_factor; |
| 164 | crop_w = crop_r - crop_x; |
| 165 | |
| 166 | dst_r = fbWidth; |
| 167 | dst_w = dst_r - dst_x; |
| 168 | } |
| 169 | if(dst_y < 0) { |
| 170 | float scale_y = crop_h * 1.0f / dst_h; |
| 171 | float diff_factor = scale_y * abs(dst_y); |
| 172 | crop_y = crop_y + diff_factor; |
| 173 | crop_h = crop_b - crop_y; |
| 174 | |
| 175 | dst_y = 0; |
| 176 | dst_h = dst_b - dst_y; |
| 177 | } |
| 178 | if(dst_b > fbHeight) { |
| 179 | float scale_y = crop_h * 1.0f / dst_h; |
| 180 | float diff_factor = scale_y * (dst_b - fbHeight); |
| 181 | crop_b = crop_b - diff_factor; |
| 182 | crop_h = crop_b - crop_y; |
| 183 | |
| 184 | dst_b = fbHeight; |
| 185 | dst_h = dst_b - dst_y; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
Naseer Ahmed | f48aef6 | 2012-07-20 09:05:53 -0700 | [diff] [blame] | 188 | |
Saurabh Shah | 3e858eb | 2012-09-17 16:53:21 -0700 | [diff] [blame] | 189 | bool isExternalActive(hwc_context_t* ctx) { |
| 190 | return ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive; |
Saurabh Shah | fc2acbe | 2012-08-17 19:47:52 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Saurabh Shah | 3e858eb | 2012-09-17 16:53:21 -0700 | [diff] [blame] | 193 | int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy) { |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 194 | int ret = 0; |
| 195 | #ifdef USE_FENCE_SYNC |
| 196 | struct mdp_buf_sync data; |
Saurabh Shah | 3e858eb | 2012-09-17 16:53:21 -0700 | [diff] [blame] | 197 | int acquireFd[4]; |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 198 | int count = 0; |
| 199 | int releaseFd = -1; |
| 200 | int fbFd = -1; |
| 201 | data.flags = 0; |
| 202 | data.acq_fen_fd = acquireFd; |
| 203 | data.rel_fen_fd = &releaseFd; |
| 204 | //Accumulate acquireFenceFds |
| 205 | for(uint32_t i = 0; i < list->numHwLayers; i++) { |
Saurabh Shah | 3e858eb | 2012-09-17 16:53:21 -0700 | [diff] [blame] | 206 | if((list->hwLayers[i].compositionType == HWC_OVERLAY || |
| 207 | list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) && |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 208 | list->hwLayers[i].acquireFenceFd != -1) { |
| 209 | acquireFd[count++] = list->hwLayers[i].acquireFenceFd; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if (count) { |
| 214 | data.acq_fen_fd_cnt = count; |
Saurabh Shah | 3e858eb | 2012-09-17 16:53:21 -0700 | [diff] [blame] | 215 | fbFd = ctx->dpyAttr[dpy].fd; |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 216 | |
| 217 | //Waits for acquire fences, returns a release fence |
| 218 | ret = ioctl(fbFd, MSMFB_BUFFER_SYNC, &data); |
| 219 | if(ret < 0) { |
| 220 | ALOGE("ioctl MSMFB_BUFFER_SYNC failed, err=%s", |
| 221 | strerror(errno)); |
| 222 | } |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 223 | |
| 224 | for(uint32_t i = 0; i < list->numHwLayers; i++) { |
Saurabh Shah | 3e858eb | 2012-09-17 16:53:21 -0700 | [diff] [blame] | 225 | if((list->hwLayers[i].compositionType == HWC_OVERLAY || |
| 226 | list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET)) { |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 227 | //Close the acquireFenceFds |
| 228 | if(list->hwLayers[i].acquireFenceFd > 0) { |
| 229 | close(list->hwLayers[i].acquireFenceFd); |
| 230 | list->hwLayers[i].acquireFenceFd = -1; |
| 231 | } |
| 232 | //Populate releaseFenceFds. |
Saurabh Shah | 3e858eb | 2012-09-17 16:53:21 -0700 | [diff] [blame] | 233 | list->hwLayers[i].releaseFenceFd = dup(releaseFd); |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
Saurabh Shah | 3e858eb | 2012-09-17 16:53:21 -0700 | [diff] [blame] | 236 | list->retireFenceFd = releaseFd; |
Kinjal Bhavsar | 2dd04a8 | 2012-09-18 18:27:59 -0700 | [diff] [blame] | 237 | } |
| 238 | #endif |
| 239 | return ret; |
| 240 | } |
| 241 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 242 | };//namespace |