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 HWC_UI_MIRROR 0
22#include <gralloc_priv.h>
23#include <fb_priv.h>
24#include "hwc_uimirror.h"
25#include "external.h"
26
27namespace qhwc {
28
29//Static Members
30ovutils::eOverlayState UIMirrorOverlay::sState = ovutils::OV_CLOSED;
31bool UIMirrorOverlay::sIsUiMirroringOn = false;
32
33void UIMirrorOverlay::reset() {
34    sIsUiMirroringOn = false;
35    sState = ovutils::OV_CLOSED;
36}
37
38//Prepare the overlay for the UI mirroring
39bool UIMirrorOverlay::prepare(hwc_context_t *ctx, hwc_layer_1_t *fblayer) {
40    reset();
41
42    if(!ctx->mMDP.hasOverlay) {
43        ALOGD_IF(HWC_UI_MIRROR, "%s, this hw doesnt support mirroring",
44                __FUNCTION__);
45       return false;
46    }
47
48    sState = ovutils::OV_UI_MIRROR;
49    ovutils::eOverlayState newState = ctx->mOverlay[HWC_DISPLAY_EXTERNAL]->getState();
50    if(newState == ovutils::OV_UI_VIDEO_TV) {
51        sState = newState;
52    }
53
54    configure(ctx, fblayer);
55    return sIsUiMirroringOn;
56}
57
58// Configure
59bool UIMirrorOverlay::configure(hwc_context_t *ctx, hwc_layer_1_t *layer)
60{
61    if (LIKELY(ctx->mOverlay[HWC_DISPLAY_EXTERNAL])) {
62        overlay::Overlay& ov = *(ctx->mOverlay[HWC_DISPLAY_EXTERNAL]);
63        // Set overlay state
64        ov.setState(sState);
65        private_handle_t *hnd = (private_handle_t *)layer->handle;
66        if (!hnd) {
67            ALOGE("%s:NULL private handle for layer!", __FUNCTION__);
68            return false;
69        }
70        ovutils::Whf info(hnd->width, hnd->height, hnd->format, hnd->size);
71        // Determine the RGB pipe for UI depending on the state
72        ovutils::eDest dest = ovutils::OV_PIPE0;
73
74        ovutils::eMdpFlags mdpFlags = ovutils::OV_MDP_FLAGS_NONE;
75        if(ctx->mSecureMode) {
76            ovutils::setMdpFlags(mdpFlags,
77                    ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
78        }
79
80        ovutils::PipeArgs parg(mdpFlags,
81                info,
82                ovutils::ZORDER_0,
83                ovutils::IS_FG_OFF,
84                ovutils::ROT_FLAG_DISABLED);
85        ovutils::PipeArgs pargs[ovutils::MAX_PIPES] = { parg, parg, parg };
86        ov.setSource(pargs, dest);
87
88        hwc_rect_t sourceCrop = layer->sourceCrop;
89        // x,y,w,h
90        ovutils::Dim dcrop(sourceCrop.left, sourceCrop.top,
91                sourceCrop.right - sourceCrop.left,
92                sourceCrop.bottom - sourceCrop.top);
93        ov.setCrop(dcrop, dest);
94
95        int transform = layer->transform;
96        ovutils::eTransform orient =
97                static_cast<ovutils::eTransform>(transform);
98        ov.setTransform(orient, dest);
99
100        hwc_rect_t displayFrame = layer->displayFrame;
101        ovutils::Dim dpos(displayFrame.left,
102                displayFrame.top,
103                displayFrame.right - displayFrame.left,
104                displayFrame.bottom - displayFrame.top);
105        ov.setPosition(dpos, dest);
106
107        if (!ov.commit(dest)) {
108            ALOGE("%s: commit fails", __FUNCTION__);
109            sIsUiMirroringOn = false;
110            return false;
111        }
112        sIsUiMirroringOn = true;
113    }
114    return sIsUiMirroringOn;
115}
116
117bool UIMirrorOverlay::draw(hwc_context_t *ctx, hwc_layer_1_t *layer)
118{
119    if(!sIsUiMirroringOn) {
120        return true;
121    }
122    bool ret = true;
123    overlay::Overlay& ov = *(ctx->mOverlay[HWC_DISPLAY_EXTERNAL]);
124    ovutils::eOverlayState state = ov.getState();
125    ovutils::eDest dest = ovutils::OV_PIPE0;
126    private_handle_t *hnd = (private_handle_t *)layer->handle;
127    switch (state) {
128        case ovutils::OV_UI_MIRROR:
129        case ovutils::OV_UI_VIDEO_TV:
130            if (!ov.queueBuffer(hnd->fd, hnd->offset, dest)) {
131                ALOGE("%s: queueBuffer failed for external", __FUNCTION__);
132                ret = false;
133            }
134            break;
135        default:
136            break;
137    }
138    return ret;
139}
140
141//---------------------------------------------------------------------
142}; //namespace qhwc
143