blob: 679e6d616634bc8f634d85ed71a20db522cffe65 [file] [log] [blame]
Saurabh Shaha9da08f2013-07-03 13:27:53 -07001/*
Arun Kumar K.R2aa44c62014-01-21 23:08:28 -08002* Copyright (c) 2013-2014 The Linux Foundation. All rights reserved.
Saurabh Shaha9da08f2013-07-03 13:27:53 -07003*
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
40using namespace overlay;
41using namespace overlay::utils;
42namespace qhwc {
43
Saurabh Shaha9da08f2013-07-03 13:27:53 -070044//Helper to write data to ad node
45static 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);
Praveena Pachipulusud9443c72014-02-17 10:42:28 +053054 ssize_t ret = write(adFd, opStr, strlen(opStr));
Saurabh Shaha9da08f2013-07-03 13:27:53 -070055 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
71static 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) {
Saurabh Shahd9e31712014-07-30 10:01:21 -070079 char opStr[4];
80 ssize_t bytesRead = read(adFd, opStr, sizeof(opStr) - 1);
81 if(bytesRead > 0) {
82 opStr[bytesRead] = '\0';
Saurabh Shaha9da08f2013-07-03 13:27:53 -070083 //Should return -1, 0 or 1
84 ret = atoi(opStr);
85 ALOGD_IF(DEBUG, "%s: Read %d from ad", __func__, ret);
Saurabh Shahd9e31712014-07-30 10:01:21 -070086 } else if(bytesRead == 0) {
87 ALOGE("%s: ad node empty", __func__);
Saurabh Shaha9da08f2013-07-03 13:27:53 -070088 } else {
89 ALOGE("%s: Read from ad node failed with error %s", __func__,
90 strerror(errno));
91 }
92 close(adFd);
93 } else {
Naseer Ahmedc2d949d2013-08-27 11:41:25 -040094 ALOGD("%s: /sys/class/graphics/fb%d/ad could not be opened : %s",
Saurabh Shaha9da08f2013-07-03 13:27:53 -070095 __func__, wbFbNum, strerror(errno));
96 }
97 return ret;
98}
99
Tatenda Chipeperekwa88fe6352014-04-14 10:36:06 -0700100AssertiveDisplay::AssertiveDisplay(hwc_context_t *ctx) :
101 mTurnedOff(true), mFeatureEnabled(false),
102 mDest(overlay::utils::OV_INVALID)
103{
104 //Values in ad node:
105 //-1 means feature is disabled on device
106 // 0 means feature exists but turned off, will be turned on by hwc
107 // 1 means feature is turned on by hwc
108 // Plus, we do this feature only on split primary displays.
109 // Plus, we do this feature only if ro.qcom.ad=2
Saurabh Shahc4f1fa62013-09-03 13:12:14 -0700110
Tatenda Chipeperekwa88fe6352014-04-14 10:36:06 -0700111 char property[PROPERTY_VALUE_MAX];
112 const int ENABLED = 2;
113 int val = 0;
Saurabh Shahc4f1fa62013-09-03 13:12:14 -0700114
Tatenda Chipeperekwa88fe6352014-04-14 10:36:06 -0700115 if(property_get("ro.qcom.ad", property, "0") > 0) {
116 val = atoi(property);
117 }
Saurabh Shahc4f1fa62013-09-03 13:12:14 -0700118
Tatenda Chipeperekwa88fe6352014-04-14 10:36:06 -0700119 if(adRead() >= 0 && isDisplaySplit(ctx, HWC_DISPLAY_PRIMARY) &&
120 val == ENABLED) {
121 ALOGD_IF(DEBUG, "Assertive display feature supported");
122 mFeatureEnabled = true;
123 // If feature exists but is turned off, set mTurnedOff to true
124 mTurnedOff = adRead() > 0 ? false : true;
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700125 }
126}
127
128void AssertiveDisplay::markDoable(hwc_context_t *ctx,
129 const hwc_display_contents_1_t* list) {
130 mDoable = false;
131 if(mFeatureEnabled &&
Ramkumar Radhakrishnan8bb48d32013-12-30 23:11:27 -0800132 !isSecondaryConnected(ctx) &&
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700133 ctx->listStats[HWC_DISPLAY_PRIMARY].yuvCount == 1) {
134 int nYuvIndex = ctx->listStats[HWC_DISPLAY_PRIMARY].yuvIndices[0];
135 const hwc_layer_1_t* layer = &list->hwLayers[nYuvIndex];
136 private_handle_t *hnd = (private_handle_t *)layer->handle;
Prabhanjan Kandula5bae9f52014-05-15 16:48:18 +0530137 qdutils::MDPVersion& mdpHw = qdutils::MDPVersion::getInstance();
Naseer Ahmed9eb5e092014-09-25 13:24:44 -0400138 if(hnd && hnd->width <= (int) mdpHw.getMaxMixerWidth()) {
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700139 mDoable = true;
140 }
141 }
142}
143
Tatenda Chipeperekwa88fe6352014-04-14 10:36:06 -0700144void AssertiveDisplay::turnOffAD() {
145 if(mFeatureEnabled) {
146 if(!mTurnedOff) {
147 const int off = 0;
148 adWrite(off);
149 mTurnedOff = true;
150 }
151 }
152 mDoable = false;
153}
154
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700155bool AssertiveDisplay::prepare(hwc_context_t *ctx,
156 const hwc_rect_t& crop,
157 const Whf& whf,
158 const private_handle_t *hnd) {
159 if(!isDoable()) {
Tatenda Chipeperekwa88fe6352014-04-14 10:36:06 -0700160 //Cleanup one time during this switch
161 turnOffAD();
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700162 return false;
163 }
164
Saurabh Shahc62f3982014-03-05 14:28:26 -0800165 Overlay::PipeSpecs pipeSpecs;
166 pipeSpecs.formatClass = Overlay::FORMAT_YUV;
167 pipeSpecs.dpy = overlay::Overlay::DPY_WRITEBACK;
168 pipeSpecs.fb = false;
169
170 ovutils::eDest dest = ctx->mOverlay->getPipe(pipeSpecs);
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700171 if(dest == OV_INVALID) {
172 ALOGE("%s failed: No VG pipe available", __func__);
173 mDoable = false;
174 return false;
175 }
176
177 overlay::Writeback *wb = overlay::Writeback::getInstance();
178
Tatenda Chipeperekwa0eca40e2013-10-25 19:44:03 -0700179 //Set Security flag on writeback
180 if(isSecureBuffer(hnd)) {
181 if(!wb->setSecure(isSecureBuffer(hnd))) {
182 ALOGE("Failure in setting WB secure flag for ad");
183 return false;
184 }
185 }
186
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700187 if(!wb->configureDpyInfo(hnd->width, hnd->height)) {
188 ALOGE("%s: config display failed", __func__);
189 mDoable = false;
190 return false;
191 }
192
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530193 int tmpW, tmpH;
194 size_t size;
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700195 int format = ovutils::getHALFormat(wb->getOutputFormat());
196 if(format < 0) {
197 ALOGE("%s invalid format %d", __func__, format);
198 mDoable = false;
199 return false;
200 }
201
202 size = getBufferSizeAndDimensions(hnd->width, hnd->height,
203 format, tmpW, tmpH);
204
Praveena Pachipulusud9443c72014-02-17 10:42:28 +0530205 if(!wb->configureMemory((uint32_t)size)) {
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700206 ALOGE("%s: config memory failed", __func__);
207 mDoable = false;
208 return false;
209 }
210
211 eMdpFlags mdpFlags = OV_MDP_FLAGS_NONE;
212 if(isSecureBuffer(hnd)) {
213 ovutils::setMdpFlags(mdpFlags,
214 ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
215 }
216
Saurabh Shah2c8ad052014-08-15 13:27:46 -0700217 PipeArgs parg(mdpFlags, whf, ZORDER_0,
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700218 ROT_FLAGS_NONE);
219 hwc_rect_t dst = crop; //input same as output
220
221 if(configMdp(ctx->mOverlay, parg, OVERLAY_TRANSFORM_0, crop, dst, NULL,
222 dest) < 0) {
223 ALOGE("%s: configMdp failed", __func__);
224 mDoable = false;
225 return false;
226 }
227
228 mDest = dest;
Tatenda Chipeperekwa88fe6352014-04-14 10:36:06 -0700229 int wbFd = wb->getFbFd();
230 if(mFeatureEnabled && wbFd >= 0 &&
231 !ctx->mOverlay->validateAndSet(overlay::Overlay::DPY_WRITEBACK, wbFd))
232 {
Saurabh Shaha36be922013-12-16 18:18:39 -0800233 ALOGE("%s: Failed to validate and set overlay for dpy %d"
234 ,__FUNCTION__, overlay::Overlay::DPY_WRITEBACK);
Tatenda Chipeperekwa88fe6352014-04-14 10:36:06 -0700235 turnOffAD();
Saurabh Shaha36be922013-12-16 18:18:39 -0800236 return false;
237 }
238
Tatenda Chipeperekwa88fe6352014-04-14 10:36:06 -0700239 // Only turn on AD if there are no errors during configuration stage
240 // and if it was previously in OFF state.
241 if(mFeatureEnabled && mTurnedOff) {
242 //write to sysfs, one time during this switch
243 const int on = 1;
244 adWrite(on);
245 mTurnedOff = false;
246 }
247
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700248 return true;
249}
250
251bool AssertiveDisplay::draw(hwc_context_t *ctx, int fd, uint32_t offset) {
Tatenda Chipeperekwa88fe6352014-04-14 10:36:06 -0700252 if(!isDoable()) {
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700253 return false;
254 }
255
256 if (!ctx->mOverlay->queueBuffer(fd, offset, mDest)) {
257 ALOGE("%s: queueBuffer failed", __func__);
258 return false;
259 }
260
261 overlay::Writeback *wb = overlay::Writeback::getInstance();
262 if(!wb->writeSync()) {
263 return false;
264 }
265
266 return true;
267}
268
Arun Kumar K.R2aa44c62014-01-21 23:08:28 -0800269int AssertiveDisplay::getDstFd() const {
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700270 overlay::Writeback *wb = overlay::Writeback::getInstance();
271 return wb->getDstFd();
272}
273
Arun Kumar K.R2aa44c62014-01-21 23:08:28 -0800274uint32_t AssertiveDisplay::getDstOffset() const {
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700275 overlay::Writeback *wb = overlay::Writeback::getInstance();
276 return wb->getOffset();
277}
278
279}