summaryrefslogtreecommitdiff
path: root/msm8909/libhwcomposer/hwc_ad.cpp
blob: 7e96d97eae1e6e1c31d55e4232d6dc835a762a15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright (c) 2013-2014 The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*    * Redistributions of source code must retain the above copyright
*      notice, this list of conditions and the following disclaimer.
*    * Redistributions in binary form must reproduce the above
*      copyright notice, this list of conditions and the following
*      disclaimer in the documentation and/or other materials provided
*      with the distribution.
*    * Neither the name of The Linux Foundation. nor the names of its
*      contributors may be used to endorse or promote products derived
*      from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <unistd.h>
#include <overlay.h>
#include <overlayUtils.h>
#include <overlayWriteback.h>
#include <mdp_version.h>
#include "hwc_ad.h"
#include "hwc_utils.h"

#define DEBUG 0
using namespace overlay;
using namespace overlay::utils;
namespace qhwc {

//Helper to write data to ad node
static void adWrite(const int& value) {
    const int wbFbNum = Overlay::getFbForDpy(Overlay::DPY_WRITEBACK);
    char wbFbPath[256];
    snprintf (wbFbPath, sizeof(wbFbPath),
            "/sys/class/graphics/fb%d/ad", wbFbNum);
    int adFd = open(wbFbPath, O_WRONLY);
    if(adFd >= 0) {
        char opStr[4] = "";
        snprintf(opStr, sizeof(opStr), "%d", value);
        ssize_t ret = write(adFd, opStr, strlen(opStr));
        if(ret < 0) {
            ALOGE("%s: Failed to write %d with error %s",
                    __func__, value, strerror(errno));
        } else if (ret == 0){
            ALOGE("%s Nothing written to ad", __func__);
        } else {
            ALOGD_IF(DEBUG, "%s: Wrote %d to ad", __func__, value);
        }
        close(adFd);
    } else {
        ALOGE("%s: Failed to open /sys/class/graphics/fb%d/ad with error %s",
                __func__, wbFbNum, strerror(errno));
    }
}

//Helper to read data from ad node
static int adRead() {
    const int wbFbNum = Overlay::getFbForDpy(Overlay::DPY_WRITEBACK);
    int ret = -1;
    char wbFbPath[256];
    snprintf (wbFbPath, sizeof(wbFbPath),
            "/sys/class/graphics/fb%d/ad", wbFbNum);
    int adFd = open(wbFbPath, O_RDONLY);
    if(adFd >= 0) {
        char opStr[4];
        ssize_t bytesRead = read(adFd, opStr, sizeof(opStr) - 1);
        if(bytesRead > 0) {
            opStr[bytesRead] = '\0';
            //Should return -1, 0 or 1
            ret = atoi(opStr);
            ALOGD_IF(DEBUG, "%s: Read %d from ad", __func__, ret);
        } else if(bytesRead == 0) {
            ALOGE("%s: ad node empty", __func__);
        } else {
            ALOGE("%s: Read from ad node failed with error %s", __func__,
                    strerror(errno));
        }
        close(adFd);
    } else {
        ALOGD("%s: /sys/class/graphics/fb%d/ad could not be opened : %s",
                __func__, wbFbNum, strerror(errno));
    }
    return ret;
}

AssertiveDisplay::AssertiveDisplay(hwc_context_t *ctx) :
     mTurnedOff(true), mFeatureEnabled(false),
     mDest(overlay::utils::OV_INVALID)
{
    //Values in ad node:
    //-1 means feature is disabled on device
    // 0 means feature exists but turned off, will be turned on by hwc
    // 1 means feature is turned on by hwc
    // Plus, we do this feature only on split primary displays.
    // Plus, we do this feature only if ro.qcom.ad=2

    char property[PROPERTY_VALUE_MAX];
    const int ENABLED = 2;
    int val = 0;

    if(property_get("ro.qcom.ad", property, "0") > 0) {
        val = atoi(property);
    }

    if(adRead() >= 0 && isDisplaySplit(ctx, HWC_DISPLAY_PRIMARY) &&
            val == ENABLED) {
        ALOGD_IF(DEBUG, "Assertive display feature supported");
        mFeatureEnabled = true;
        // If feature exists but is turned off, set mTurnedOff to true
        mTurnedOff = adRead() > 0 ? false : true;
    }
}

void AssertiveDisplay::markDoable(hwc_context_t *ctx,
        const hwc_display_contents_1_t* list) {
    mDoable = false;
    if(mFeatureEnabled &&
        !isSecondaryConnected(ctx) &&
        ctx->listStats[HWC_DISPLAY_PRIMARY].yuvCount == 1) {
        int nYuvIndex = ctx->listStats[HWC_DISPLAY_PRIMARY].yuvIndices[0];
        const hwc_layer_1_t* layer = &list->hwLayers[nYuvIndex];
        private_handle_t *hnd = (private_handle_t *)layer->handle;
        qdutils::MDPVersion& mdpHw =  qdutils::MDPVersion::getInstance();
        if(hnd && hnd->width <= (int) mdpHw.getMaxMixerWidth()) {
            mDoable = true;
        }
    }
}

void AssertiveDisplay::turnOffAD() {
    if(mFeatureEnabled) {
        if(!mTurnedOff) {
            const int off = 0;
            adWrite(off);
            mTurnedOff = true;
        }
    }
    mDoable = false;
}

bool AssertiveDisplay::prepare(hwc_context_t *ctx,
        const hwc_rect_t& crop,
        const Whf& whf,
        const private_handle_t *hnd) {
    if(!isDoable()) {
        //Cleanup one time during this switch
        turnOffAD();
        return false;
    }

    Overlay::PipeSpecs pipeSpecs;
    pipeSpecs.formatClass = Overlay::FORMAT_YUV;
    pipeSpecs.dpy = overlay::Overlay::DPY_WRITEBACK;
    pipeSpecs.fb = false;

    ovutils::eDest dest = ctx->mOverlay->getPipe(pipeSpecs);
    if(dest == OV_INVALID) {
        ALOGE("%s failed: No VG pipe available", __func__);
        mDoable = false;
        return false;
    }

    overlay::Writeback *wb = overlay::Writeback::getInstance();

    //Set Security flag on writeback
    if(isSecureBuffer(hnd)) {
        if(!wb->setSecure(isSecureBuffer(hnd))) {
            ALOGE("Failure in setting WB secure flag for ad");
            return false;
        }
    }

    if(!wb->configureDpyInfo(hnd->width, hnd->height)) {
        ALOGE("%s: config display failed", __func__);
        mDoable = false;
        return false;
    }

    int tmpW, tmpH;
    size_t size;
    int format = ovutils::getHALFormat(wb->getOutputFormat());
    if(format < 0) {
        ALOGE("%s invalid format %d", __func__, format);
        mDoable = false;
        return false;
    }

    size = getBufferSizeAndDimensions(hnd->width, hnd->height,
                format, tmpW, tmpH);

    if(!wb->configureMemory((uint32_t)size)) {
        ALOGE("%s: config memory failed", __func__);
        mDoable = false;
        return false;
    }

    eMdpFlags mdpFlags = OV_MDP_FLAGS_NONE;
    if(isSecureBuffer(hnd)) {
        ovutils::setMdpFlags(mdpFlags,
                ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
    }

    PipeArgs parg(mdpFlags, whf, ZORDER_0,
            ROT_FLAGS_NONE);
    hwc_rect_t dst = crop; //input same as output

    if(configMdp(ctx->mOverlay, parg, OVERLAY_TRANSFORM_0, crop, dst, NULL,
                dest) < 0) {
        ALOGE("%s: configMdp failed", __func__);
        mDoable = false;
        return false;
    }

    mDest = dest;
    int wbFd = wb->getFbFd();
    if(mFeatureEnabled && wbFd >= 0 &&
        !ctx->mOverlay->validateAndSet(overlay::Overlay::DPY_WRITEBACK, wbFd))
    {
        ALOGE("%s: Failed to validate and set overlay for dpy %d"
                ,__FUNCTION__, overlay::Overlay::DPY_WRITEBACK);
        turnOffAD();
        return false;
    }

    // Only turn on AD if there are no errors during configuration stage
    // and if it was previously in OFF state.
    if(mFeatureEnabled && mTurnedOff) {
        //write to sysfs, one time during this switch
        const int on = 1;
        adWrite(on);
        mTurnedOff = false;
    }

    return true;
}

bool AssertiveDisplay::draw(hwc_context_t *ctx, int fd, uint32_t offset) {
    if(!isDoable()) {
        return false;
    }

    if (!ctx->mOverlay->queueBuffer(fd, offset, mDest)) {
        ALOGE("%s: queueBuffer failed", __func__);
        return false;
    }

    overlay::Writeback *wb = overlay::Writeback::getInstance();
    if(!wb->writeSync()) {
        return false;
    }

    return true;
}

int AssertiveDisplay::getDstFd() const {
    overlay::Writeback *wb = overlay::Writeback::getInstance();
    return wb->getDstFd();
}

uint32_t AssertiveDisplay::getDstOffset() const {
    overlay::Writeback *wb = overlay::Writeback::getInstance();
    return wb->getOffset();
}

}