1 /*
2 * Copyright (c) 2013-2014 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 //Helper to write data to ad node
adWrite(const int & value)45 static void adWrite(const int& value) {
46 const int wbFbNum = Overlay::getFbForDpy(Overlay::DPY_WRITEBACK);
47 char wbFbPath[256];
48 snprintf (wbFbPath, sizeof(wbFbPath),
49 "/sys/class/graphics/fb%d/ad", wbFbNum);
50 int adFd = open(wbFbPath, O_WRONLY);
51 if(adFd >= 0) {
52 char opStr[4] = "";
53 snprintf(opStr, sizeof(opStr), "%d", value);
54 ssize_t ret = write(adFd, opStr, strlen(opStr));
55 if(ret < 0) {
56 ALOGE("%s: Failed to write %d with error %s",
57 __func__, value, strerror(errno));
58 } else if (ret == 0){
59 ALOGE("%s Nothing written to ad", __func__);
60 } else {
61 ALOGD_IF(DEBUG, "%s: Wrote %d to ad", __func__, value);
62 }
63 close(adFd);
64 } else {
65 ALOGE("%s: Failed to open /sys/class/graphics/fb%d/ad with error %s",
66 __func__, wbFbNum, strerror(errno));
67 }
68 }
69
70 //Helper to read data from ad node
adRead()71 static int adRead() {
72 const int wbFbNum = Overlay::getFbForDpy(Overlay::DPY_WRITEBACK);
73 int ret = -1;
74 char wbFbPath[256];
75 snprintf (wbFbPath, sizeof(wbFbPath),
76 "/sys/class/graphics/fb%d/ad", wbFbNum);
77 int adFd = open(wbFbPath, O_RDONLY);
78 if(adFd >= 0) {
79 char opStr[4] = {'\0'};
80 if(read(adFd, opStr, strlen(opStr)) >= 0) {
81 //Should return -1, 0 or 1
82 ret = atoi(opStr);
83 ALOGD_IF(DEBUG, "%s: Read %d from ad", __func__, ret);
84 } else {
85 ALOGE("%s: Read from ad node failed with error %s", __func__,
86 strerror(errno));
87 }
88 close(adFd);
89 } else {
90 ALOGD("%s: /sys/class/graphics/fb%d/ad could not be opened : %s",
91 __func__, wbFbNum, strerror(errno));
92 }
93 return ret;
94 }
95
AssertiveDisplay(hwc_context_t * ctx)96 AssertiveDisplay::AssertiveDisplay(hwc_context_t *ctx) :
97 mTurnedOff(true), mFeatureEnabled(false),
98 mDest(overlay::utils::OV_INVALID)
99 {
100 //Values in ad node:
101 //-1 means feature is disabled on device
102 // 0 means feature exists but turned off, will be turned on by hwc
103 // 1 means feature is turned on by hwc
104 // Plus, we do this feature only on split primary displays.
105 // Plus, we do this feature only if ro.qcom.ad=2
106
107 char property[PROPERTY_VALUE_MAX];
108 const int ENABLED = 2;
109 int val = 0;
110
111 if(property_get("ro.qcom.ad", property, "0") > 0) {
112 val = atoi(property);
113 }
114
115 if(adRead() >= 0 && isDisplaySplit(ctx, HWC_DISPLAY_PRIMARY) &&
116 val == ENABLED) {
117 ALOGD_IF(DEBUG, "Assertive display feature supported");
118 mFeatureEnabled = true;
119 // If feature exists but is turned off, set mTurnedOff to true
120 mTurnedOff = adRead() > 0 ? false : true;
121 }
122 }
123
markDoable(hwc_context_t * ctx,const hwc_display_contents_1_t * list)124 void AssertiveDisplay::markDoable(hwc_context_t *ctx,
125 const hwc_display_contents_1_t* list) {
126 mDoable = false;
127 if(mFeatureEnabled &&
128 !isSecondaryConnected(ctx) &&
129 ctx->listStats[HWC_DISPLAY_PRIMARY].yuvCount == 1) {
130 int nYuvIndex = ctx->listStats[HWC_DISPLAY_PRIMARY].yuvIndices[0];
131 const hwc_layer_1_t* layer = &list->hwLayers[nYuvIndex];
132 private_handle_t *hnd = (private_handle_t *)layer->handle;
133 if(hnd && hnd->width <= qdutils::MAX_DISPLAY_DIM) {
134 mDoable = true;
135 }
136 }
137 }
138
turnOffAD()139 void AssertiveDisplay::turnOffAD() {
140 if(mFeatureEnabled) {
141 if(!mTurnedOff) {
142 const int off = 0;
143 adWrite(off);
144 mTurnedOff = true;
145 }
146 }
147 mDoable = false;
148 }
149
prepare(hwc_context_t * ctx,const hwc_rect_t & crop,const Whf & whf,const private_handle_t * hnd)150 bool AssertiveDisplay::prepare(hwc_context_t *ctx,
151 const hwc_rect_t& crop,
152 const Whf& whf,
153 const private_handle_t *hnd) {
154 if(!isDoable()) {
155 //Cleanup one time during this switch
156 turnOffAD();
157 return false;
158 }
159
160 Overlay::PipeSpecs pipeSpecs;
161 pipeSpecs.formatClass = Overlay::FORMAT_YUV;
162 pipeSpecs.dpy = overlay::Overlay::DPY_WRITEBACK;
163 pipeSpecs.fb = false;
164
165 ovutils::eDest dest = ctx->mOverlay->getPipe(pipeSpecs);
166 if(dest == OV_INVALID) {
167 ALOGE("%s failed: No VG pipe available", __func__);
168 mDoable = false;
169 return false;
170 }
171
172 overlay::Writeback *wb = overlay::Writeback::getInstance();
173
174 //Set Security flag on writeback
175 if(isSecureBuffer(hnd)) {
176 if(!wb->setSecure(isSecureBuffer(hnd))) {
177 ALOGE("Failure in setting WB secure flag for ad");
178 return false;
179 }
180 }
181
182 if(!wb->configureDpyInfo(hnd->width, hnd->height)) {
183 ALOGE("%s: config display failed", __func__);
184 mDoable = false;
185 return false;
186 }
187
188 int tmpW, tmpH;
189 size_t size;
190 int format = ovutils::getHALFormat(wb->getOutputFormat());
191 if(format < 0) {
192 ALOGE("%s invalid format %d", __func__, format);
193 mDoable = false;
194 return false;
195 }
196
197 size = getBufferSizeAndDimensions(hnd->width, hnd->height,
198 format, tmpW, tmpH);
199
200 if(!wb->configureMemory((uint32_t)size)) {
201 ALOGE("%s: config memory failed", __func__);
202 mDoable = false;
203 return false;
204 }
205
206 eMdpFlags mdpFlags = OV_MDP_FLAGS_NONE;
207 if(isSecureBuffer(hnd)) {
208 ovutils::setMdpFlags(mdpFlags,
209 ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
210 }
211
212 PipeArgs parg(mdpFlags, whf, ZORDER_0, IS_FG_OFF,
213 ROT_FLAGS_NONE);
214 hwc_rect_t dst = crop; //input same as output
215
216 if(configMdp(ctx->mOverlay, parg, OVERLAY_TRANSFORM_0, crop, dst, NULL,
217 dest) < 0) {
218 ALOGE("%s: configMdp failed", __func__);
219 mDoable = false;
220 return false;
221 }
222
223 mDest = dest;
224 int wbFd = wb->getFbFd();
225 if(mFeatureEnabled && wbFd >= 0 &&
226 !ctx->mOverlay->validateAndSet(overlay::Overlay::DPY_WRITEBACK, wbFd))
227 {
228 ALOGE("%s: Failed to validate and set overlay for dpy %d"
229 ,__FUNCTION__, overlay::Overlay::DPY_WRITEBACK);
230 turnOffAD();
231 return false;
232 }
233
234 // Only turn on AD if there are no errors during configuration stage
235 // and if it was previously in OFF state.
236 if(mFeatureEnabled && mTurnedOff) {
237 //write to sysfs, one time during this switch
238 const int on = 1;
239 adWrite(on);
240 mTurnedOff = false;
241 }
242
243 return true;
244 }
245
draw(hwc_context_t * ctx,int fd,uint32_t offset)246 bool AssertiveDisplay::draw(hwc_context_t *ctx, int fd, uint32_t offset) {
247 if(!isDoable()) {
248 return false;
249 }
250
251 if (!ctx->mOverlay->queueBuffer(fd, offset, mDest)) {
252 ALOGE("%s: queueBuffer failed", __func__);
253 return false;
254 }
255
256 overlay::Writeback *wb = overlay::Writeback::getInstance();
257 if(!wb->writeSync()) {
258 return false;
259 }
260
261 return true;
262 }
263
getDstFd() const264 int AssertiveDisplay::getDstFd() const {
265 overlay::Writeback *wb = overlay::Writeback::getInstance();
266 return wb->getDstFd();
267 }
268
getDstOffset() const269 uint32_t AssertiveDisplay::getDstOffset() const {
270 overlay::Writeback *wb = overlay::Writeback::getInstance();
271 return wb->getOffset();
272 }
273
274 }
275