hwc_fbupdate.cpp revision fea63d61337f1efeb2d1c7a7a4fafe472611704c
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 * Copyright (C) 2012, The Linux Foundation. All rights reserved.
4 *
5 * Not a Contribution, Apache license notifications and license are
6 * retained for attribution purposes only.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 *      http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21#define DEBUG_FBUPDATE 0
22#include <cutils/properties.h>
23#include <gralloc_priv.h>
24#include <overlay.h>
25#include <overlayRotator.h>
26#include "hwc_fbupdate.h"
27#include "mdp_version.h"
28#include "external.h"
29#include "virtual.h"
30
31using namespace qdutils;
32using namespace overlay;
33using overlay::Rotator;
34using namespace overlay::utils;
35
36namespace qhwc {
37
38namespace ovutils = overlay::utils;
39
40IFBUpdate* IFBUpdate::getObject(const int& width, const int& rightSplit,
41        const int& dpy) {
42    if(width > MAX_DISPLAY_DIM || rightSplit) {
43        return new FBUpdateHighRes(dpy);
44    }
45    return new FBUpdateLowRes(dpy);
46}
47
48inline void IFBUpdate::reset() {
49    mModeOn = false;
50    mRot = NULL;
51}
52
53//================= Low res====================================
54FBUpdateLowRes::FBUpdateLowRes(const int& dpy): IFBUpdate(dpy) {}
55
56inline void FBUpdateLowRes::reset() {
57    IFBUpdate::reset();
58    mDest = ovutils::OV_INVALID;
59}
60
61bool FBUpdateLowRes::prepare(hwc_context_t *ctx, hwc_display_contents_1 *list,
62                             int fbZorder) {
63    if(!ctx->mMDP.hasOverlay) {
64        ALOGD_IF(DEBUG_FBUPDATE, "%s, this hw doesnt support overlays",
65                 __FUNCTION__);
66        return false;
67    }
68    mModeOn = configure(ctx, list, fbZorder);
69    return mModeOn;
70}
71
72// Configure
73bool FBUpdateLowRes::configure(hwc_context_t *ctx, hwc_display_contents_1 *list,
74                               int fbZorder) {
75    bool ret = false;
76    hwc_layer_1_t *layer = &list->hwLayers[list->numHwLayers - 1];
77    if (LIKELY(ctx->mOverlay)) {
78        int extOnlyLayerIndex = ctx->listStats[mDpy].extOnlyLayerIndex;
79        // ext only layer present..
80        if(extOnlyLayerIndex != -1) {
81            layer = &list->hwLayers[extOnlyLayerIndex];
82            layer->compositionType = HWC_OVERLAY;
83        }
84        overlay::Overlay& ov = *(ctx->mOverlay);
85        private_handle_t *hnd = (private_handle_t *)layer->handle;
86        ovutils::Whf info(hnd->width, hnd->height,
87                          ovutils::getMdpFormat(hnd->format), hnd->size);
88
89        //Request a pipe
90        ovutils::eMdpPipeType type = ovutils::OV_MDP_PIPE_ANY;
91        if(qdutils::MDPVersion::getInstance().is8x26() && mDpy) {
92            //For 8x26 external always use DMA pipe
93            type = ovutils::OV_MDP_PIPE_DMA;
94        }
95        ovutils::eDest dest = ov.nextPipe(type, mDpy, Overlay::MIXER_DEFAULT);
96        if(dest == ovutils::OV_INVALID) { //None available
97            ALOGE("%s: No pipes available to configure fb for dpy %d",
98                __FUNCTION__, mDpy);
99            return false;
100        }
101        mDest = dest;
102
103        if((mDpy && ctx->deviceOrientation) &&
104            ctx->listStats[mDpy].isDisplayAnimating) {
105            fbZorder = 0;
106        }
107
108        ovutils::eMdpFlags mdpFlags = ovutils::OV_MDP_BLEND_FG_PREMULT;
109        ovutils::eIsFg isFg = ovutils::IS_FG_OFF;
110        ovutils::eZorder zOrder = static_cast<ovutils::eZorder>(fbZorder);
111
112        hwc_rect_t sourceCrop = layer->sourceCrop;
113        hwc_rect_t displayFrame = layer->displayFrame;
114        int transform = layer->transform;
115        int rotFlags = ovutils::ROT_FLAGS_NONE;
116
117        ovutils::eTransform orient =
118                    static_cast<ovutils::eTransform>(transform);
119
120        // Do not use getNonWormholeRegion() function to calculate the
121        // sourceCrop during animation on external display and
122        // Dont do wormhole calculation when extorientation is set on External
123        // Dont do wormhole calculation when extDownscale is enabled on External
124        if(ctx->listStats[mDpy].isDisplayAnimating && mDpy) {
125            sourceCrop = layer->displayFrame;
126            displayFrame = sourceCrop;
127        } else if((!mDpy ||
128                   (mDpy && !ctx->mExtOrientation
129                   && !ctx->dpyAttr[mDpy].mDownScaleMode))
130                   && (extOnlyLayerIndex == -1)) {
131            if(!qdutils::MDPVersion::getInstance().is8x26()) {
132                getNonWormholeRegion(list, sourceCrop);
133                displayFrame = sourceCrop;
134            }
135        }
136        if(mDpy && !qdutils::MDPVersion::getInstance().is8x26()) {
137            if(ctx->mExtOrientation || ctx->dpyAttr[mDpy].mDownScaleMode) {
138                calcExtDisplayPosition(ctx, mDpy, sourceCrop, displayFrame);
139                // If there is a external orientation set, use that
140                if(ctx->mExtOrientation) {
141                    transform = ctx->mExtOrientation;
142                    orient =
143                        static_cast<ovutils::eTransform >(ctx->mExtOrientation);
144                }
145            }
146            // Calculate the actionsafe dimensions for External(dpy = 1 or 2)
147            getActionSafePosition(ctx, mDpy, displayFrame);
148        }
149        setMdpFlags(layer, mdpFlags, 0, transform);
150        // For External use rotator if there is a rotation value set
151        if(mDpy && (ctx->mExtOrientation & HWC_TRANSFORM_ROT_90)) {
152            mRot = ctx->mRotMgr->getNext();
153            if(mRot == NULL) return -1;
154            //Configure rotator for pre-rotation
155            if(configRotator(mRot, info, sourceCrop, mdpFlags, orient, 0) < 0) {
156                ALOGE("%s: configRotator Failed!", __FUNCTION__);
157                mRot = NULL;
158                return -1;
159            }
160            info.format = (mRot)->getDstFormat();
161            updateSource(orient, info, sourceCrop);
162            rotFlags |= ovutils::ROT_PREROTATED;
163        }
164        //For the mdp, since either we are pre-rotating or MDP does flips
165        orient = ovutils::OVERLAY_TRANSFORM_0;
166        transform = 0;
167        ovutils::PipeArgs parg(mdpFlags, info, zOrder, isFg,
168                                static_cast<ovutils::eRotFlags>(rotFlags));
169        ret = true;
170        if(configMdp(ctx->mOverlay, parg, orient, sourceCrop, displayFrame,
171                    NULL, mDest) < 0) {
172            ALOGE("%s: configMdp failed for dpy %d", __FUNCTION__, mDpy);
173            ret = false;
174        }
175    }
176    return ret;
177}
178
179bool FBUpdateLowRes::draw(hwc_context_t *ctx, private_handle_t *hnd)
180{
181    if(!mModeOn) {
182        return true;
183    }
184    bool ret = true;
185    overlay::Overlay& ov = *(ctx->mOverlay);
186    ovutils::eDest dest = mDest;
187    int fd = hnd->fd;
188    uint32_t offset = hnd->offset;
189    if(mRot) {
190        if(!mRot->queueBuffer(fd, offset))
191            return false;
192        fd = mRot->getDstMemId();
193        offset = mRot->getDstOffset();
194    }
195    if (!ov.queueBuffer(fd, offset, dest)) {
196        ALOGE("%s: queueBuffer failed for FBUpdate", __FUNCTION__);
197        ret = false;
198    }
199    return ret;
200}
201
202//================= High res====================================
203FBUpdateHighRes::FBUpdateHighRes(const int& dpy): IFBUpdate(dpy) {}
204
205inline void FBUpdateHighRes::reset() {
206    IFBUpdate::reset();
207    mDestLeft = ovutils::OV_INVALID;
208    mDestRight = ovutils::OV_INVALID;
209    mRot = NULL;
210}
211
212bool FBUpdateHighRes::prepare(hwc_context_t *ctx, hwc_display_contents_1 *list,
213                              int fbZorder) {
214    if(!ctx->mMDP.hasOverlay) {
215        ALOGD_IF(DEBUG_FBUPDATE, "%s, this hw doesnt support overlays",
216                 __FUNCTION__);
217        return false;
218    }
219    ALOGD_IF(DEBUG_FBUPDATE, "%s, mModeOn = %d", __FUNCTION__, mModeOn);
220    mModeOn = configure(ctx, list, fbZorder);
221    return mModeOn;
222}
223
224// Configure
225bool FBUpdateHighRes::configure(hwc_context_t *ctx,
226        hwc_display_contents_1 *list, int fbZorder) {
227    bool ret = false;
228    hwc_layer_1_t *layer = &list->hwLayers[list->numHwLayers - 1];
229    if (LIKELY(ctx->mOverlay)) {
230        int extOnlyLayerIndex = ctx->listStats[mDpy].extOnlyLayerIndex;
231        // ext only layer present..
232        if(extOnlyLayerIndex != -1) {
233            layer = &list->hwLayers[extOnlyLayerIndex];
234            layer->compositionType = HWC_OVERLAY;
235        }
236        overlay::Overlay& ov = *(ctx->mOverlay);
237        private_handle_t *hnd = (private_handle_t *)layer->handle;
238        ovutils::Whf info(hnd->width, hnd->height,
239                          ovutils::getMdpFormat(hnd->format), hnd->size);
240
241        //Request left pipe
242        ovutils::eDest destL = ov.nextPipe(ovutils::OV_MDP_PIPE_ANY, mDpy,
243                Overlay::MIXER_LEFT);
244        if(destL == ovutils::OV_INVALID) { //None available
245            ALOGE("%s: No pipes available to configure fb for dpy %d's left"
246                    " mixer", __FUNCTION__, mDpy);
247            return false;
248        }
249        //Request right pipe
250        ovutils::eDest destR = ov.nextPipe(ovutils::OV_MDP_PIPE_ANY, mDpy,
251                Overlay::MIXER_RIGHT);
252        if(destR == ovutils::OV_INVALID) { //None available
253            ALOGE("%s: No pipes available to configure fb for dpy %d's right"
254                    " mixer", __FUNCTION__, mDpy);
255            return false;
256        }
257
258        mDestLeft = destL;
259        mDestRight = destR;
260
261        ovutils::eMdpFlags mdpFlagsL = ovutils::OV_MDP_BLEND_FG_PREMULT;
262
263        ovutils::eZorder zOrder = static_cast<ovutils::eZorder>(fbZorder);
264
265        ovutils::PipeArgs pargL(mdpFlagsL,
266                                info,
267                                zOrder,
268                                ovutils::IS_FG_OFF,
269                                ovutils::ROT_FLAGS_NONE);
270        ov.setSource(pargL, destL);
271
272        ovutils::eMdpFlags mdpFlagsR = mdpFlagsL;
273        ovutils::setMdpFlags(mdpFlagsR, ovutils::OV_MDSS_MDP_RIGHT_MIXER);
274        ovutils::PipeArgs pargR(mdpFlagsR,
275                                info,
276                                zOrder,
277                                ovutils::IS_FG_OFF,
278                                ovutils::ROT_FLAGS_NONE);
279        ov.setSource(pargR, destR);
280
281        hwc_rect_t sourceCrop = layer->sourceCrop;
282        hwc_rect_t displayFrame = layer->displayFrame;
283
284        const float xres = ctx->dpyAttr[mDpy].xres;
285        const int lSplit = getLeftSplit(ctx, mDpy);
286        const float lSplitRatio = lSplit / xres;
287        const float lCropWidth =
288                (sourceCrop.right - sourceCrop.left) * lSplitRatio;
289
290        ovutils::Dim dcropL(
291                sourceCrop.left,
292                sourceCrop.top,
293                lCropWidth,
294                sourceCrop.bottom - sourceCrop.top);
295
296        ovutils::Dim dcropR(
297                sourceCrop.left + lCropWidth,
298                sourceCrop.top,
299                (sourceCrop.right - sourceCrop.left) - lCropWidth,
300                sourceCrop.bottom - sourceCrop.top);
301
302        ov.setCrop(dcropL, destL);
303        ov.setCrop(dcropR, destR);
304
305        int transform = layer->transform;
306        ovutils::eTransform orient =
307            static_cast<ovutils::eTransform>(transform);
308        ov.setTransform(orient, destL);
309        ov.setTransform(orient, destR);
310
311        const int lWidth = (lSplit - displayFrame.left);
312        const int rWidth = (displayFrame.right - lSplit);
313        const int height = displayFrame.bottom - displayFrame.top;
314
315        ovutils::Dim dposL(displayFrame.left,
316                           displayFrame.top,
317                           lWidth,
318                           height);
319        ov.setPosition(dposL, destL);
320
321        ovutils::Dim dposR(0,
322                           displayFrame.top,
323                           rWidth,
324                           height);
325        ov.setPosition(dposR, destR);
326
327        ret = true;
328        if (!ov.commit(destL)) {
329            ALOGE("%s: commit fails for left", __FUNCTION__);
330            ret = false;
331        }
332        if (!ov.commit(destR)) {
333            ALOGE("%s: commit fails for right", __FUNCTION__);
334            ret = false;
335        }
336    }
337    return ret;
338}
339
340bool FBUpdateHighRes::draw(hwc_context_t *ctx, private_handle_t *hnd)
341{
342    if(!mModeOn) {
343        return true;
344    }
345    bool ret = true;
346    overlay::Overlay& ov = *(ctx->mOverlay);
347    ovutils::eDest destL = mDestLeft;
348    ovutils::eDest destR = mDestRight;
349    if (!ov.queueBuffer(hnd->fd, hnd->offset, destL)) {
350        ALOGE("%s: queue failed for left of dpy = %d",
351              __FUNCTION__, mDpy);
352        ret = false;
353    }
354    if (!ov.queueBuffer(hnd->fd, hnd->offset, destR)) {
355        ALOGE("%s: queue failed for right of dpy = %d",
356              __FUNCTION__, mDpy);
357        ret = false;
358    }
359    return ret;
360}
361
362//---------------------------------------------------------------------
363}; //namespace qhwc
364