Ramkumar Radhakrishnan | 8bb48d3 | 2013-12-30 23:11:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * Copyright (C) 2012-2014, 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 | #include <fcntl.h> |
| 21 | #include <errno.h> |
| 22 | |
| 23 | #include <cutils/log.h> |
| 24 | #include <utils/Trace.h> |
| 25 | #include <overlayWriteback.h> |
| 26 | #include "hwc_utils.h" |
| 27 | #include "hwc_fbupdate.h" |
| 28 | #include "hwc_mdpcomp.h" |
| 29 | #include "hwc_dump_layers.h" |
| 30 | #include "hwc_copybit.h" |
| 31 | #include "hwc_virtual.h" |
Tatenda Chipeperekwa | 523eac5 | 2014-05-29 14:58:39 -0700 | [diff] [blame] | 32 | #include "sync/sync.h" |
Ramkumar Radhakrishnan | 8bb48d3 | 2013-12-30 23:11:27 -0800 | [diff] [blame] | 33 | |
| 34 | #define HWCVIRTUAL_LOG 0 |
| 35 | |
| 36 | using namespace qhwc; |
| 37 | using namespace overlay; |
| 38 | |
Manoj Kumar AVM | 9591a5e | 2014-08-21 22:50:21 -0700 | [diff] [blame^] | 39 | bool HWCVirtualVDS::sVDDumpEnabled = false; |
Ramkumar Radhakrishnan | 8bb48d3 | 2013-12-30 23:11:27 -0800 | [diff] [blame] | 40 | |
| 41 | void HWCVirtualVDS::init(hwc_context_t *ctx) { |
| 42 | const int dpy = HWC_DISPLAY_VIRTUAL; |
| 43 | ctx->mFBUpdate[dpy] = |
| 44 | IFBUpdate::getObject(ctx, dpy); |
| 45 | ctx->mMDPComp[dpy] = MDPComp::getObject(ctx, dpy); |
| 46 | |
| 47 | if(ctx->mFBUpdate[dpy]) |
| 48 | ctx->mFBUpdate[dpy]->reset(); |
| 49 | if(ctx->mMDPComp[dpy]) |
| 50 | ctx->mMDPComp[dpy]->reset(); |
| 51 | } |
| 52 | |
Arun Kumar K.R | 2aa44c6 | 2014-01-21 23:08:28 -0800 | [diff] [blame] | 53 | void HWCVirtualVDS::destroy(hwc_context_t *ctx, size_t /*numDisplays*/, |
Ramkumar Radhakrishnan | 8bb48d3 | 2013-12-30 23:11:27 -0800 | [diff] [blame] | 54 | hwc_display_contents_1_t** displays) { |
| 55 | int dpy = HWC_DISPLAY_VIRTUAL; |
| 56 | |
| 57 | //Cleanup virtual display objs, since there is no explicit disconnect |
Raj Kamal | 52b4fdb | 2014-01-27 19:35:13 +0530 | [diff] [blame] | 58 | if(ctx->dpyAttr[dpy].connected && (displays[dpy] == NULL)) { |
Ramkumar Radhakrishnan | 8bb48d3 | 2013-12-30 23:11:27 -0800 | [diff] [blame] | 59 | ctx->dpyAttr[dpy].connected = false; |
Tatenda Chipeperekwa | 8f1b9d7 | 2014-02-11 16:20:50 -0800 | [diff] [blame] | 60 | ctx->dpyAttr[dpy].isPause = false; |
Ramkumar Radhakrishnan | 8bb48d3 | 2013-12-30 23:11:27 -0800 | [diff] [blame] | 61 | |
| 62 | if(ctx->mFBUpdate[dpy]) { |
| 63 | delete ctx->mFBUpdate[dpy]; |
| 64 | ctx->mFBUpdate[dpy] = NULL; |
| 65 | } |
| 66 | if(ctx->mMDPComp[dpy]) { |
| 67 | delete ctx->mMDPComp[dpy]; |
| 68 | ctx->mMDPComp[dpy] = NULL; |
| 69 | } |
Manoj Kumar AVM | fb472b0 | 2014-08-21 15:13:37 -0700 | [diff] [blame] | 70 | // signal synclock to indicate successful wfd teardown |
Raj kamal | 59fea56 | 2014-04-01 16:52:19 +0530 | [diff] [blame] | 71 | ctx->mWfdSyncLock.lock(); |
| 72 | ctx->mWfdSyncLock.signal(); |
| 73 | ctx->mWfdSyncLock.unlock(); |
Ramkumar Radhakrishnan | 8bb48d3 | 2013-12-30 23:11:27 -0800 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
| 77 | int HWCVirtualVDS::prepare(hwc_composer_device_1 *dev, |
| 78 | hwc_display_contents_1_t *list) { |
| 79 | ATRACE_CALL(); |
| 80 | //XXX: Fix when framework support is added |
| 81 | hwc_context_t* ctx = (hwc_context_t*)(dev); |
| 82 | const int dpy = HWC_DISPLAY_VIRTUAL; |
| 83 | |
Tatenda Chipeperekwa | b0a9f9d | 2014-01-20 09:23:21 -0800 | [diff] [blame] | 84 | if (list && list->outbuf && list->numHwLayers > 0) { |
Praveena Pachipulusu | d9443c7 | 2014-02-17 10:42:28 +0530 | [diff] [blame] | 85 | reset_layer_prop(ctx, dpy, (int)list->numHwLayers - 1); |
| 86 | uint32_t last = (uint32_t)list->numHwLayers - 1; |
Ramkumar Radhakrishnan | 8bb48d3 | 2013-12-30 23:11:27 -0800 | [diff] [blame] | 87 | hwc_layer_1_t *fbLayer = &list->hwLayers[last]; |
| 88 | int fbWidth = 0, fbHeight = 0; |
| 89 | getLayerResolution(fbLayer, fbWidth, fbHeight); |
| 90 | ctx->dpyAttr[dpy].xres = fbWidth; |
| 91 | ctx->dpyAttr[dpy].yres = fbHeight; |
| 92 | |
| 93 | if(ctx->dpyAttr[dpy].connected == false) { |
| 94 | ctx->dpyAttr[dpy].connected = true; |
Tatenda Chipeperekwa | 8f1b9d7 | 2014-02-11 16:20:50 -0800 | [diff] [blame] | 95 | ctx->dpyAttr[dpy].isPause = false; |
Tatenda Chipeperekwa | d6a8d4b | 2014-01-14 18:31:20 -0800 | [diff] [blame] | 96 | // We set the vsync period to the primary refresh rate, leaving |
| 97 | // it up to the consumer to decide how fast to consume frames. |
| 98 | ctx->dpyAttr[dpy].vsync_period |
| 99 | = ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period; |
Ramkumar Radhakrishnan | 8bb48d3 | 2013-12-30 23:11:27 -0800 | [diff] [blame] | 100 | init(ctx); |
Tatenda Chipeperekwa | d6a8d4b | 2014-01-14 18:31:20 -0800 | [diff] [blame] | 101 | // XXX: for architectures with limited resources we would normally |
| 102 | // allow one padding round to free up resources but this breaks |
| 103 | // certain use cases. |
Ramkumar Radhakrishnan | 8bb48d3 | 2013-12-30 23:11:27 -0800 | [diff] [blame] | 104 | } |
Tatenda Chipeperekwa | 8f1b9d7 | 2014-02-11 16:20:50 -0800 | [diff] [blame] | 105 | if(!ctx->dpyAttr[dpy].isPause) { |
| 106 | ctx->dpyAttr[dpy].isConfiguring = false; |
| 107 | ctx->dpyAttr[dpy].fd = Writeback::getInstance()->getFbFd(); |
| 108 | private_handle_t *ohnd = (private_handle_t *)list->outbuf; |
| 109 | Writeback::getInstance()->configureDpyInfo(ohnd->width, |
| 110 | ohnd->height); |
| 111 | setListStats(ctx, list, dpy); |
Ramkumar Radhakrishnan | 8bb48d3 | 2013-12-30 23:11:27 -0800 | [diff] [blame] | 112 | |
Tatenda Chipeperekwa | 8f1b9d7 | 2014-02-11 16:20:50 -0800 | [diff] [blame] | 113 | if(ctx->mMDPComp[dpy]->prepare(ctx, list) < 0) { |
| 114 | const int fbZ = 0; |
Saurabh Shah | ef19fe3 | 2014-04-22 15:31:58 -0700 | [diff] [blame] | 115 | if(not ctx->mFBUpdate[dpy]->prepareAndValidate(ctx, list, fbZ)) |
| 116 | { |
| 117 | ctx->mOverlay->clear(dpy); |
| 118 | ctx->mLayerRotMap[dpy]->clear(); |
| 119 | } |
Tatenda Chipeperekwa | 8f1b9d7 | 2014-02-11 16:20:50 -0800 | [diff] [blame] | 120 | } |
| 121 | } else { |
| 122 | /* Virtual Display is in Pause state. |
| 123 | * Mark all application layers as OVERLAY so that |
| 124 | * GPU will not compose. |
| 125 | */ |
Raj Kamal | e012e0e | 2014-05-28 23:45:19 +0530 | [diff] [blame] | 126 | Writeback::getInstance(); //Ensure that WB is active during pause |
Tatenda Chipeperekwa | 8f1b9d7 | 2014-02-11 16:20:50 -0800 | [diff] [blame] | 127 | for(size_t i = 0 ;i < (size_t)(list->numHwLayers - 1); i++) { |
| 128 | hwc_layer_1_t *layer = &list->hwLayers[i]; |
| 129 | layer->compositionType = HWC_OVERLAY; |
| 130 | } |
Ramkumar Radhakrishnan | 8bb48d3 | 2013-12-30 23:11:27 -0800 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | int HWCVirtualVDS::set(hwc_context_t *ctx, hwc_display_contents_1_t *list) { |
| 137 | ATRACE_CALL(); |
| 138 | int ret = 0; |
| 139 | const int dpy = HWC_DISPLAY_VIRTUAL; |
| 140 | |
Tatenda Chipeperekwa | f19f84d | 2014-01-17 12:43:29 -0800 | [diff] [blame] | 141 | if (list && list->outbuf && list->numHwLayers > 0) { |
Praveena Pachipulusu | d9443c7 | 2014-02-17 10:42:28 +0530 | [diff] [blame] | 142 | uint32_t last = (uint32_t)list->numHwLayers - 1; |
Ramkumar Radhakrishnan | 8bb48d3 | 2013-12-30 23:11:27 -0800 | [diff] [blame] | 143 | hwc_layer_1_t *fbLayer = &list->hwLayers[last]; |
| 144 | |
Tatenda Chipeperekwa | 8f1b9d7 | 2014-02-11 16:20:50 -0800 | [diff] [blame] | 145 | if(ctx->dpyAttr[dpy].connected |
| 146 | && (!ctx->dpyAttr[dpy].isPause)) |
| 147 | { |
Ramkumar Radhakrishnan | 8bb48d3 | 2013-12-30 23:11:27 -0800 | [diff] [blame] | 148 | private_handle_t *ohnd = (private_handle_t *)list->outbuf; |
Tatenda Chipeperekwa | a2fdebe | 2014-01-20 12:29:50 -0800 | [diff] [blame] | 149 | int format = ohnd->format; |
| 150 | if (format == HAL_PIXEL_FORMAT_RGBA_8888) |
| 151 | format = HAL_PIXEL_FORMAT_RGBX_8888; |
Ramkumar Radhakrishnan | 8bb48d3 | 2013-12-30 23:11:27 -0800 | [diff] [blame] | 152 | Writeback::getInstance()->setOutputFormat( |
Tatenda Chipeperekwa | a2fdebe | 2014-01-20 12:29:50 -0800 | [diff] [blame] | 153 | utils::getMdpFormat(format)); |
Ramkumar Radhakrishnan | 8bb48d3 | 2013-12-30 23:11:27 -0800 | [diff] [blame] | 154 | |
Manoj Kumar AVM | fb472b0 | 2014-08-21 15:13:37 -0700 | [diff] [blame] | 155 | // Configure WB secure mode based on output buffer handle |
| 156 | if(! Writeback::getInstance()->setSecure(isSecureBuffer(ohnd))) |
| 157 | { |
| 158 | ALOGE("Failed to set WB secure mode: %d for virtual display", |
| 159 | isSecureBuffer(ohnd)); |
| 160 | return false; |
Tatenda Chipeperekwa | 92961f8 | 2014-01-17 13:04:28 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Ramkumar Radhakrishnan | 8bb48d3 | 2013-12-30 23:11:27 -0800 | [diff] [blame] | 163 | int fd = -1; //FenceFD from the Copybit |
| 164 | hwc_sync(ctx, list, dpy, fd); |
| 165 | |
Tatenda Chipeperekwa | 523eac5 | 2014-05-29 14:58:39 -0700 | [diff] [blame] | 166 | // Dump the layers for virtual |
| 167 | if(ctx->mHwcDebug[dpy]) |
| 168 | ctx->mHwcDebug[dpy]->dumpLayers(list); |
| 169 | |
Ramkumar Radhakrishnan | 8bb48d3 | 2013-12-30 23:11:27 -0800 | [diff] [blame] | 170 | if (!ctx->mMDPComp[dpy]->draw(ctx, list)) { |
| 171 | ALOGE("%s: MDPComp draw failed", __FUNCTION__); |
| 172 | ret = -1; |
| 173 | } |
Tatenda Chipeperekwa | d6a8d4b | 2014-01-14 18:31:20 -0800 | [diff] [blame] | 174 | // We need an FB layer handle check to cater for this usecase: |
| 175 | // Video is playing in landscape on primary, then launch |
| 176 | // ScreenRecord app. |
| 177 | // In this scenario, the first VDS draw call will have HWC |
| 178 | // composition and VDS does nit involve GPU to get eglSwapBuffer |
| 179 | // to get valid fb handle. |
| 180 | if (fbLayer->handle && !ctx->mFBUpdate[dpy]->draw(ctx, |
Ramkumar Radhakrishnan | 8bb48d3 | 2013-12-30 23:11:27 -0800 | [diff] [blame] | 181 | (private_handle_t *)fbLayer->handle)) { |
| 182 | ALOGE("%s: FBUpdate::draw fail!", __FUNCTION__); |
| 183 | ret = -1; |
| 184 | } |
| 185 | |
Praveena Pachipulusu | d9443c7 | 2014-02-17 10:42:28 +0530 | [diff] [blame] | 186 | Writeback::getInstance()->queueBuffer(ohnd->fd, |
| 187 | (uint32_t)ohnd->offset); |
Ramkumar Radhakrishnan | 8bb48d3 | 2013-12-30 23:11:27 -0800 | [diff] [blame] | 188 | if(!Overlay::displayCommit(ctx->dpyAttr[dpy].fd)) { |
| 189 | ALOGE("%s: display commit fail!", __FUNCTION__); |
| 190 | ret = -1; |
| 191 | } |
| 192 | |
Tatenda Chipeperekwa | 06af9cb | 2014-08-26 14:51:05 -0700 | [diff] [blame] | 193 | if(sVDDumpEnabled) { |
Tatenda Chipeperekwa | 523eac5 | 2014-05-29 14:58:39 -0700 | [diff] [blame] | 194 | char bufferName[128]; |
| 195 | // Dumping frame buffer |
| 196 | sync_wait(fbLayer->acquireFenceFd, 1000); |
| 197 | snprintf(bufferName, sizeof(bufferName), "vds.fb"); |
| 198 | dumpBuffer((private_handle_t *)fbLayer->handle, bufferName); |
| 199 | // Dumping WB output for non-secure session |
| 200 | if(!isSecureBuffer(ohnd)) { |
| 201 | sync_wait(list->retireFenceFd, 1000); |
| 202 | snprintf(bufferName, sizeof(bufferName), "vds.wb"); |
| 203 | dumpBuffer(ohnd, bufferName); |
| 204 | } |
| 205 | } |
Ramkumar Radhakrishnan | 8bb48d3 | 2013-12-30 23:11:27 -0800 | [diff] [blame] | 206 | } else if(list->outbufAcquireFenceFd >= 0) { |
| 207 | //If we dont handle the frame, set retireFenceFd to outbufFenceFd, |
| 208 | //which will make sure, the framework waits on it and closes it. |
| 209 | //The other way is to wait on outbufFenceFd ourselves, close it and |
| 210 | //set retireFenceFd to -1. Since we want hwc to be async, choosing |
| 211 | //the former. |
| 212 | //Also dup because, the closeAcquireFds() will close the outbufFence |
| 213 | list->retireFenceFd = dup(list->outbufAcquireFenceFd); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | closeAcquireFds(list); |
| 218 | return ret; |
| 219 | } |
| 220 | |
Tatenda Chipeperekwa | 8f1b9d7 | 2014-02-11 16:20:50 -0800 | [diff] [blame] | 221 | void HWCVirtualVDS::pause(hwc_context_t* ctx, int dpy) { |
| 222 | { |
| 223 | Locker::Autolock _l(ctx->mDrawLock); |
| 224 | ctx->dpyAttr[dpy].isActive = true; |
| 225 | ctx->dpyAttr[dpy].isPause = true; |
| 226 | ctx->proc->invalidate(ctx->proc); |
| 227 | } |
| 228 | usleep(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period |
| 229 | * 2 / 1000); |
Raj Kamal | e012e0e | 2014-05-28 23:45:19 +0530 | [diff] [blame] | 230 | // At this point all the pipes used by External have been |
| 231 | // marked as UNSET. |
| 232 | { |
| 233 | Locker::Autolock _l(ctx->mDrawLock); |
| 234 | // Perform commit to unstage the pipes. |
| 235 | if (!Overlay::displayCommit(ctx->dpyAttr[dpy].fd)) { |
| 236 | ALOGE("%s: display commit fail! for %d dpy", |
| 237 | __FUNCTION__, dpy); |
| 238 | } |
Raj Kamal | df3d1d2 | 2014-05-30 21:23:27 +0530 | [diff] [blame] | 239 | ctx->proc->invalidate(ctx->proc); |
Raj Kamal | e012e0e | 2014-05-28 23:45:19 +0530 | [diff] [blame] | 240 | } |
Tatenda Chipeperekwa | 8f1b9d7 | 2014-02-11 16:20:50 -0800 | [diff] [blame] | 241 | return; |
| 242 | } |
| 243 | |
| 244 | void HWCVirtualVDS::resume(hwc_context_t* ctx, int dpy) { |
| 245 | { |
| 246 | Locker::Autolock _l(ctx->mDrawLock); |
| 247 | ctx->dpyAttr[dpy].isConfiguring = true; |
| 248 | ctx->dpyAttr[dpy].isActive = true; |
| 249 | ctx->proc->invalidate(ctx->proc); |
| 250 | } |
| 251 | usleep(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period |
| 252 | * 2 / 1000); |
| 253 | //At this point external has all the pipes it would need. |
| 254 | { |
| 255 | Locker::Autolock _l(ctx->mDrawLock); |
| 256 | ctx->dpyAttr[dpy].isPause = false; |
| 257 | ctx->proc->invalidate(ctx->proc); |
| 258 | } |
| 259 | return; |
| 260 | } |