Saurabh Shah | a9da08f | 2013-07-03 13:27:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of The Linux Foundation. nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #include <unistd.h> |
| 31 | #include <overlay.h> |
| 32 | #include <overlayUtils.h> |
| 33 | #include <overlayWriteback.h> |
| 34 | #include <mdp_version.h> |
| 35 | #include "hwc_ad.h" |
| 36 | #include "hwc_utils.h" |
| 37 | #include "external.h" |
| 38 | |
| 39 | #define DEBUG 0 |
| 40 | using namespace overlay; |
| 41 | using namespace overlay::utils; |
| 42 | namespace qhwc { |
| 43 | |
| 44 | //Opens writeback framebuffer and returns fd. |
| 45 | static int openWbFb() { |
| 46 | int wbFd = -1; |
| 47 | //Check opening which FB would connect LM to WB |
| 48 | const int wbFbNum = Overlay::getFbForDpy(Overlay::DPY_WRITEBACK); |
| 49 | if(wbFbNum >= 0) { |
| 50 | char wbFbPath[256]; |
| 51 | snprintf (wbFbPath, sizeof(wbFbPath), |
| 52 | "/sys/class/graphics/fb%d", wbFbNum); |
| 53 | //Opening writeback fb first time would create ad node if the device |
| 54 | //supports adaptive display |
| 55 | wbFd = open(wbFbPath, O_RDONLY); |
| 56 | if(wbFd < 0) { |
| 57 | ALOGE("%s: Failed to open /sys/class/graphics/fb%d with error %s", |
| 58 | __func__, wbFbNum, strerror(errno)); |
| 59 | } |
| 60 | } else { |
| 61 | ALOGD_IF(DEBUG, "%s: No writeback available", __func__); |
| 62 | } |
| 63 | return wbFd; |
| 64 | } |
| 65 | |
| 66 | static inline void closeWbFb(int& fd) { |
| 67 | if(fd >= 0) { |
| 68 | close(fd); |
| 69 | fd = -1; |
| 70 | } else { |
| 71 | ALOGE("%s: Invalid fd %d", __func__, fd); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | //Helper to write data to ad node |
| 76 | static void adWrite(const int& value) { |
| 77 | const int wbFbNum = Overlay::getFbForDpy(Overlay::DPY_WRITEBACK); |
| 78 | char wbFbPath[256]; |
| 79 | snprintf (wbFbPath, sizeof(wbFbPath), |
| 80 | "/sys/class/graphics/fb%d/ad", wbFbNum); |
| 81 | int adFd = open(wbFbPath, O_WRONLY); |
| 82 | if(adFd >= 0) { |
| 83 | char opStr[4] = ""; |
| 84 | snprintf(opStr, sizeof(opStr), "%d", value); |
| 85 | int ret = write(adFd, opStr, strlen(opStr)); |
| 86 | if(ret < 0) { |
| 87 | ALOGE("%s: Failed to write %d with error %s", |
| 88 | __func__, value, strerror(errno)); |
| 89 | } else if (ret == 0){ |
| 90 | ALOGE("%s Nothing written to ad", __func__); |
| 91 | } else { |
| 92 | ALOGD_IF(DEBUG, "%s: Wrote %d to ad", __func__, value); |
| 93 | } |
| 94 | close(adFd); |
| 95 | } else { |
| 96 | ALOGE("%s: Failed to open /sys/class/graphics/fb%d/ad with error %s", |
| 97 | __func__, wbFbNum, strerror(errno)); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | //Helper to read data from ad node |
| 102 | static int adRead() { |
| 103 | const int wbFbNum = Overlay::getFbForDpy(Overlay::DPY_WRITEBACK); |
| 104 | int ret = -1; |
| 105 | char wbFbPath[256]; |
| 106 | snprintf (wbFbPath, sizeof(wbFbPath), |
| 107 | "/sys/class/graphics/fb%d/ad", wbFbNum); |
| 108 | int adFd = open(wbFbPath, O_RDONLY); |
| 109 | if(adFd >= 0) { |
| 110 | char opStr[4] = {'\0'}; |
| 111 | if(read(adFd, opStr, strlen(opStr)) >= 0) { |
| 112 | //Should return -1, 0 or 1 |
| 113 | ret = atoi(opStr); |
| 114 | ALOGD_IF(DEBUG, "%s: Read %d from ad", __func__, ret); |
| 115 | } else { |
| 116 | ALOGE("%s: Read from ad node failed with error %s", __func__, |
| 117 | strerror(errno)); |
| 118 | } |
| 119 | close(adFd); |
| 120 | } else { |
Naseer Ahmed | c2d949d | 2013-08-27 11:41:25 -0400 | [diff] [blame] | 121 | ALOGD("%s: /sys/class/graphics/fb%d/ad could not be opened : %s", |
Saurabh Shah | a9da08f | 2013-07-03 13:27:53 -0700 | [diff] [blame] | 122 | __func__, wbFbNum, strerror(errno)); |
| 123 | } |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | AssertiveDisplay::AssertiveDisplay() :mWbFd(-1), mDoable(false), |
| 128 | mFeatureEnabled(false), mDest(overlay::utils::OV_INVALID) { |
| 129 | int fd = openWbFb(); |
| 130 | if(fd >= 0) { |
| 131 | //-1 means feature is disabled on device |
| 132 | // 0 means feature exists but turned off, will be turned on by hwc |
| 133 | // 1 means feature is turned on by hwc |
| 134 | if(adRead() >= 0) { |
| 135 | ALOGD_IF(DEBUG, "Assertive display feature supported"); |
| 136 | mFeatureEnabled = true; |
| 137 | } |
| 138 | closeWbFb(fd); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void AssertiveDisplay::markDoable(hwc_context_t *ctx, |
| 143 | const hwc_display_contents_1_t* list) { |
| 144 | mDoable = false; |
| 145 | if(mFeatureEnabled && |
Jeykumar Sankaran | 27dee26 | 2013-08-01 17:09:54 -0700 | [diff] [blame] | 146 | !ctx->mExtDisplay->isConnected() && |
Saurabh Shah | a9da08f | 2013-07-03 13:27:53 -0700 | [diff] [blame] | 147 | ctx->listStats[HWC_DISPLAY_PRIMARY].yuvCount == 1) { |
| 148 | int nYuvIndex = ctx->listStats[HWC_DISPLAY_PRIMARY].yuvIndices[0]; |
| 149 | const hwc_layer_1_t* layer = &list->hwLayers[nYuvIndex]; |
| 150 | private_handle_t *hnd = (private_handle_t *)layer->handle; |
| 151 | if(hnd && hnd->width <= qdutils::MAX_DISPLAY_DIM) { |
| 152 | mDoable = true; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | bool AssertiveDisplay::prepare(hwc_context_t *ctx, |
| 158 | const hwc_rect_t& crop, |
| 159 | const Whf& whf, |
| 160 | const private_handle_t *hnd) { |
| 161 | if(!isDoable()) { |
| 162 | if(isModeOn()) { |
| 163 | //Cleanup one time during this switch |
| 164 | const int off = 0; |
| 165 | adWrite(off); |
| 166 | closeWbFb(mWbFd); |
| 167 | } |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | ovutils::eDest dest = ctx->mOverlay->nextPipe(ovutils::OV_MDP_PIPE_VG, |
| 172 | overlay::Overlay::DPY_WRITEBACK, Overlay::MIXER_DEFAULT); |
| 173 | if(dest == OV_INVALID) { |
| 174 | ALOGE("%s failed: No VG pipe available", __func__); |
| 175 | mDoable = false; |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | overlay::Writeback *wb = overlay::Writeback::getInstance(); |
| 180 | |
| 181 | if(!wb->configureDpyInfo(hnd->width, hnd->height)) { |
| 182 | ALOGE("%s: config display failed", __func__); |
| 183 | mDoable = false; |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | int tmpW, tmpH, size; |
| 188 | int format = ovutils::getHALFormat(wb->getOutputFormat()); |
| 189 | if(format < 0) { |
| 190 | ALOGE("%s invalid format %d", __func__, format); |
| 191 | mDoable = false; |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | size = getBufferSizeAndDimensions(hnd->width, hnd->height, |
| 196 | format, tmpW, tmpH); |
| 197 | |
| 198 | if(!wb->configureMemory(size, isSecureBuffer(hnd))) { |
| 199 | ALOGE("%s: config memory failed", __func__); |
| 200 | mDoable = false; |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | eMdpFlags mdpFlags = OV_MDP_FLAGS_NONE; |
| 205 | if(isSecureBuffer(hnd)) { |
| 206 | ovutils::setMdpFlags(mdpFlags, |
| 207 | ovutils::OV_MDP_SECURE_OVERLAY_SESSION); |
| 208 | } |
| 209 | |
| 210 | PipeArgs parg(mdpFlags, whf, ZORDER_0, IS_FG_OFF, |
| 211 | ROT_FLAGS_NONE); |
| 212 | hwc_rect_t dst = crop; //input same as output |
| 213 | |
| 214 | if(configMdp(ctx->mOverlay, parg, OVERLAY_TRANSFORM_0, crop, dst, NULL, |
| 215 | dest) < 0) { |
| 216 | ALOGE("%s: configMdp failed", __func__); |
| 217 | mDoable = false; |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | mDest = dest; |
| 222 | if(!isModeOn()) { |
| 223 | mWbFd = openWbFb(); |
| 224 | if(mWbFd >= 0) { |
| 225 | //write to sysfs, one time during this switch |
| 226 | const int on = 1; |
| 227 | adWrite(on); |
| 228 | } |
| 229 | } |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | bool AssertiveDisplay::draw(hwc_context_t *ctx, int fd, uint32_t offset) { |
| 234 | if(!isDoable() || !isModeOn()) { |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | if (!ctx->mOverlay->queueBuffer(fd, offset, mDest)) { |
| 239 | ALOGE("%s: queueBuffer failed", __func__); |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | overlay::Writeback *wb = overlay::Writeback::getInstance(); |
| 244 | if(!wb->writeSync()) { |
| 245 | return false; |
| 246 | } |
| 247 | |
| 248 | return true; |
| 249 | } |
| 250 | |
| 251 | int AssertiveDisplay::getDstFd(hwc_context_t *ctx) const { |
| 252 | overlay::Writeback *wb = overlay::Writeback::getInstance(); |
| 253 | return wb->getDstFd(); |
| 254 | } |
| 255 | |
| 256 | uint32_t AssertiveDisplay::getDstOffset(hwc_context_t *ctx) const { |
| 257 | overlay::Writeback *wb = overlay::Writeback::getInstance(); |
| 258 | return wb->getOffset(); |
| 259 | } |
| 260 | |
| 261 | } |