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 38 #define DEBUG 0 39 using namespace overlay; 40 using namespace overlay::utils; 41 namespace qhwc { 42 43 //Opens writeback framebuffer and returns fd. 44 static int openWbFb() { 45 int wbFd = -1; 46 //Check opening which FB would connect LM to WB 47 const int wbFbNum = Overlay::getFbForDpy(Overlay::DPY_WRITEBACK); 48 if(wbFbNum >= 0) { 49 char wbFbPath[256]; 50 snprintf (wbFbPath, sizeof(wbFbPath), 51 "/sys/class/graphics/fb%d", wbFbNum); 52 //Opening writeback fb first time would create ad node if the device 53 //supports adaptive display 54 wbFd = open(wbFbPath, O_RDONLY); 55 if(wbFd < 0) { 56 ALOGE("%s: Failed to open /sys/class/graphics/fb%d with error %s", 57 __func__, wbFbNum, strerror(errno)); 58 } 59 } else { 60 ALOGD_IF(DEBUG, "%s: No writeback available", __func__); 61 } 62 return wbFd; 63 } 64 65 static inline void closeWbFb(int& fd) { 66 if(fd >= 0) { 67 close(fd); 68 fd = -1; 69 } else { 70 ALOGE("%s: Invalid fd %d", __func__, fd); 71 } 72 } 73 74 //Helper to write data to ad node 75 static void adWrite(const int& value) { 76 const int wbFbNum = Overlay::getFbForDpy(Overlay::DPY_WRITEBACK); 77 char wbFbPath[256]; 78 snprintf (wbFbPath, sizeof(wbFbPath), 79 "/sys/class/graphics/fb%d/ad", wbFbNum); 80 int adFd = open(wbFbPath, O_WRONLY); 81 if(adFd >= 0) { 82 char opStr[4] = ""; 83 snprintf(opStr, sizeof(opStr), "%d", value); 84 int ret = write(adFd, opStr, strlen(opStr)); 85 if(ret < 0) { 86 ALOGE("%s: Failed to write %d with error %s", 87 __func__, value, strerror(errno)); 88 } else if (ret == 0){ 89 ALOGE("%s Nothing written to ad", __func__); 90 } else { 91 ALOGD_IF(DEBUG, "%s: Wrote %d to ad", __func__, value); 92 } 93 close(adFd); 94 } else { 95 ALOGE("%s: Failed to open /sys/class/graphics/fb%d/ad with error %s", 96 __func__, wbFbNum, strerror(errno)); 97 } 98 } 99 100 //Helper to read data from ad node 101 static int adRead() { 102 const int wbFbNum = Overlay::getFbForDpy(Overlay::DPY_WRITEBACK); 103 int ret = -1; 104 char wbFbPath[256]; 105 snprintf (wbFbPath, sizeof(wbFbPath), 106 "/sys/class/graphics/fb%d/ad", wbFbNum); 107 int adFd = open(wbFbPath, O_RDONLY); 108 if(adFd >= 0) { 109 char opStr[4] = {'\0'}; 110 if(read(adFd, opStr, strlen(opStr)) >= 0) { 111 //Should return -1, 0 or 1 112 ret = atoi(opStr); 113 ALOGD_IF(DEBUG, "%s: Read %d from ad", __func__, ret); 114 } else { 115 ALOGE("%s: Read from ad node failed with error %s", __func__, 116 strerror(errno)); 117 } 118 close(adFd); 119 } else { 120 ALOGD("%s: /sys/class/graphics/fb%d/ad could not be opened : %s", 121 __func__, wbFbNum, strerror(errno)); 122 } 123 return ret; 124 } 125 126 AssertiveDisplay::AssertiveDisplay() :mWbFd(-1), mDoable(false), 127 mFeatureEnabled(false), mDest(overlay::utils::OV_INVALID) { 128 int fd = openWbFb(); 129 if(fd >= 0) { 130 //-1 means feature is disabled on device 131 // 0 means feature exists but turned off, will be turned on by hwc 132 // 1 means feature is turned on by hwc 133 if(adRead() >= 0) { 134 ALOGD_IF(DEBUG, "Assertive display feature supported"); 135 mFeatureEnabled = true; 136 } 137 closeWbFb(fd); 138 } 139 } 140 141 void AssertiveDisplay::markDoable(hwc_context_t *ctx, 142 const hwc_display_contents_1_t* list) { 143 mDoable = false; 144 if(mFeatureEnabled && 145 !isSecondaryConnected(ctx) && 146 ctx->listStats[HWC_DISPLAY_PRIMARY].yuvCount == 1) { 147 int nYuvIndex = ctx->listStats[HWC_DISPLAY_PRIMARY].yuvIndices[0]; 148 const hwc_layer_1_t* layer = &list->hwLayers[nYuvIndex]; 149 private_handle_t *hnd = (private_handle_t *)layer->handle; 150 if(hnd && hnd->width <= qdutils::MAX_DISPLAY_DIM) { 151 mDoable = true; 152 } 153 } 154 } 155 156 bool AssertiveDisplay::prepare(hwc_context_t *ctx, 157 const hwc_rect_t& crop, 158 const Whf& whf, 159 const private_handle_t *hnd) { 160 if(!isDoable()) { 161 if(isModeOn()) { 162 //Cleanup one time during this switch 163 const int off = 0; 164 adWrite(off); 165 closeWbFb(mWbFd); 166 } 167 return false; 168 } 169 170 ovutils::eDest dest = ctx->mOverlay->nextPipe(ovutils::OV_MDP_PIPE_VG, 171 overlay::Overlay::DPY_WRITEBACK, Overlay::MIXER_DEFAULT); 172 if(dest == OV_INVALID) { 173 ALOGE("%s failed: No VG pipe available", __func__); 174 mDoable = false; 175 return false; 176 } 177 178 overlay::Writeback *wb = overlay::Writeback::getInstance(); 179 180 if(!wb->configureDpyInfo(hnd->width, hnd->height)) { 181 ALOGE("%s: config display failed", __func__); 182 mDoable = false; 183 return false; 184 } 185 186 int tmpW, tmpH, size; 187 int format = ovutils::getHALFormat(wb->getOutputFormat()); 188 if(format < 0) { 189 ALOGE("%s invalid format %d", __func__, format); 190 mDoable = false; 191 return false; 192 } 193 194 size = getBufferSizeAndDimensions(hnd->width, hnd->height, 195 format, tmpW, tmpH); 196 197 if(!wb->configureMemory(size, isSecureBuffer(hnd))) { 198 ALOGE("%s: config memory failed", __func__); 199 mDoable = false; 200 return false; 201 } 202 203 eMdpFlags mdpFlags = OV_MDP_FLAGS_NONE; 204 if(isSecureBuffer(hnd)) { 205 ovutils::setMdpFlags(mdpFlags, 206 ovutils::OV_MDP_SECURE_OVERLAY_SESSION); 207 } 208 209 PipeArgs parg(mdpFlags, whf, ZORDER_0, IS_FG_OFF, 210 ROT_FLAGS_NONE, 211 ovutils::DEFAULT_PLANE_ALPHA, 212 ovutils::OVERLAY_BLENDING_OPAQUE); 213 hwc_rect_t dst = crop; //input same as output 214 215 if(configMdp(ctx->mOverlay, parg, OVERLAY_TRANSFORM_0, crop, dst, NULL, 216 dest) < 0) { 217 ALOGE("%s: configMdp failed", __func__); 218 mDoable = false; 219 return false; 220 } 221 222 mDest = dest; 223 if(!isModeOn()) { 224 mWbFd = openWbFb(); 225 if(mWbFd >= 0) { 226 //write to sysfs, one time during this switch 227 const int on = 1; 228 adWrite(on); 229 } 230 } 231 return true; 232 } 233 234 bool AssertiveDisplay::draw(hwc_context_t *ctx, int fd, uint32_t offset) { 235 if(!isDoable() || !isModeOn()) { 236 return false; 237 } 238 239 if (!ctx->mOverlay->queueBuffer(fd, offset, mDest)) { 240 ALOGE("%s: queueBuffer failed", __func__); 241 return false; 242 } 243 244 overlay::Writeback *wb = overlay::Writeback::getInstance(); 245 if(!wb->writeSync()) { 246 return false; 247 } 248 249 return true; 250 } 251 252 int AssertiveDisplay::getDstFd(hwc_context_t *ctx) const { 253 overlay::Writeback *wb = overlay::Writeback::getInstance(); 254 return wb->getDstFd(); 255 } 256 257 uint32_t AssertiveDisplay::getDstOffset(hwc_context_t *ctx) const { 258 overlay::Writeback *wb = overlay::Writeback::getInstance(); 259 return wb->getOffset(); 260 } 261 262 } 263