blob: 104ffcea585a0f43674aa1e490329c5dcbc2e7df [file] [log] [blame]
Saurabh Shaha9da08f2013-07-03 13:27:53 -07001/*
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
40using namespace overlay;
41using namespace overlay::utils;
42namespace qhwc {
43
44//Opens writeback framebuffer and returns fd.
45static 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
66static 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
76static 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
102static 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 Ahmedc2d949d2013-08-27 11:41:25 -0400121 ALOGD("%s: /sys/class/graphics/fb%d/ad could not be opened : %s",
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700122 __func__, wbFbNum, strerror(errno));
123 }
124 return ret;
125}
126
Saurabh Shahc4f1fa62013-09-03 13:12:14 -0700127AssertiveDisplay::AssertiveDisplay(hwc_context_t *ctx) : mWbFd(-1),
128 mDoable(false), mFeatureEnabled(false),
129 mDest(overlay::utils::OV_INVALID) {
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700130 int fd = openWbFb();
131 if(fd >= 0) {
Saurabh Shahc4f1fa62013-09-03 13:12:14 -0700132 //Values in ad node:
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700133 //-1 means feature is disabled on device
134 // 0 means feature exists but turned off, will be turned on by hwc
135 // 1 means feature is turned on by hwc
Saurabh Shahc4f1fa62013-09-03 13:12:14 -0700136 // Plus, we do this feature only on split primary displays.
137 // Plus, we do this feature only if ro.qcom.ad=2
138
139 char property[PROPERTY_VALUE_MAX];
140 const int ENABLED = 2;
141 int val = 0;
142
143 if(property_get("ro.qcom.ad", property, "0") > 0) {
144 val = atoi(property);
145 }
146
147 if(adRead() >= 0 && isDisplaySplit(ctx, HWC_DISPLAY_PRIMARY) &&
148 val == ENABLED) {
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700149 ALOGD_IF(DEBUG, "Assertive display feature supported");
150 mFeatureEnabled = true;
151 }
152 closeWbFb(fd);
153 }
154}
155
156void AssertiveDisplay::markDoable(hwc_context_t *ctx,
157 const hwc_display_contents_1_t* list) {
158 mDoable = false;
159 if(mFeatureEnabled &&
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700160 !ctx->mExtDisplay->isConnected() &&
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700161 ctx->listStats[HWC_DISPLAY_PRIMARY].yuvCount == 1) {
162 int nYuvIndex = ctx->listStats[HWC_DISPLAY_PRIMARY].yuvIndices[0];
163 const hwc_layer_1_t* layer = &list->hwLayers[nYuvIndex];
164 private_handle_t *hnd = (private_handle_t *)layer->handle;
165 if(hnd && hnd->width <= qdutils::MAX_DISPLAY_DIM) {
166 mDoable = true;
167 }
168 }
169}
170
171bool AssertiveDisplay::prepare(hwc_context_t *ctx,
172 const hwc_rect_t& crop,
173 const Whf& whf,
174 const private_handle_t *hnd) {
175 if(!isDoable()) {
176 if(isModeOn()) {
177 //Cleanup one time during this switch
178 const int off = 0;
179 adWrite(off);
180 closeWbFb(mWbFd);
181 }
182 return false;
183 }
184
185 ovutils::eDest dest = ctx->mOverlay->nextPipe(ovutils::OV_MDP_PIPE_VG,
186 overlay::Overlay::DPY_WRITEBACK, Overlay::MIXER_DEFAULT);
187 if(dest == OV_INVALID) {
188 ALOGE("%s failed: No VG pipe available", __func__);
189 mDoable = false;
190 return false;
191 }
192
193 overlay::Writeback *wb = overlay::Writeback::getInstance();
194
Tatenda Chipeperekwa0eca40e2013-10-25 19:44:03 -0700195 //Set Security flag on writeback
196 if(isSecureBuffer(hnd)) {
197 if(!wb->setSecure(isSecureBuffer(hnd))) {
198 ALOGE("Failure in setting WB secure flag for ad");
199 return false;
200 }
201 }
202
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700203 if(!wb->configureDpyInfo(hnd->width, hnd->height)) {
204 ALOGE("%s: config display failed", __func__);
205 mDoable = false;
206 return false;
207 }
208
209 int tmpW, tmpH, size;
210 int format = ovutils::getHALFormat(wb->getOutputFormat());
211 if(format < 0) {
212 ALOGE("%s invalid format %d", __func__, format);
213 mDoable = false;
214 return false;
215 }
216
217 size = getBufferSizeAndDimensions(hnd->width, hnd->height,
218 format, tmpW, tmpH);
219
Tatenda Chipeperekwa0eca40e2013-10-25 19:44:03 -0700220 if(!wb->configureMemory(size)) {
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700221 ALOGE("%s: config memory failed", __func__);
222 mDoable = false;
223 return false;
224 }
225
226 eMdpFlags mdpFlags = OV_MDP_FLAGS_NONE;
227 if(isSecureBuffer(hnd)) {
228 ovutils::setMdpFlags(mdpFlags,
229 ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
230 }
231
232 PipeArgs parg(mdpFlags, whf, ZORDER_0, IS_FG_OFF,
233 ROT_FLAGS_NONE);
234 hwc_rect_t dst = crop; //input same as output
235
236 if(configMdp(ctx->mOverlay, parg, OVERLAY_TRANSFORM_0, crop, dst, NULL,
237 dest) < 0) {
238 ALOGE("%s: configMdp failed", __func__);
239 mDoable = false;
240 return false;
241 }
242
243 mDest = dest;
244 if(!isModeOn()) {
245 mWbFd = openWbFb();
246 if(mWbFd >= 0) {
247 //write to sysfs, one time during this switch
248 const int on = 1;
249 adWrite(on);
250 }
251 }
252 return true;
253}
254
255bool AssertiveDisplay::draw(hwc_context_t *ctx, int fd, uint32_t offset) {
256 if(!isDoable() || !isModeOn()) {
257 return false;
258 }
259
260 if (!ctx->mOverlay->queueBuffer(fd, offset, mDest)) {
261 ALOGE("%s: queueBuffer failed", __func__);
262 return false;
263 }
264
265 overlay::Writeback *wb = overlay::Writeback::getInstance();
266 if(!wb->writeSync()) {
267 return false;
268 }
269
270 return true;
271}
272
273int AssertiveDisplay::getDstFd(hwc_context_t *ctx) const {
274 overlay::Writeback *wb = overlay::Writeback::getInstance();
275 return wb->getDstFd();
276}
277
278uint32_t AssertiveDisplay::getDstOffset(hwc_context_t *ctx) const {
279 overlay::Writeback *wb = overlay::Writeback::getInstance();
280 return wb->getOffset();
281}
282
283}