blob: 525d6622b747aad8ffd5dce15ab5a5d2a6485e2f [file] [log] [blame]
Naseer Ahmedf48aef62012-07-20 09:05:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 * Copyright (C) 2012, Code Aurora Forum. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#include "hwc_video.h"
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070019#include "hwc_ext_observer.h"
Naseer Ahmedf48aef62012-07-20 09:05:53 -070020
21namespace qhwc {
22
23#define FINAL_TRANSFORM_MASK 0x000F
24#define VIDEO_DEBUG 0
25
26//Static Members
27ovutils::eOverlayState VideoOverlay::sState = ovutils::OV_CLOSED;
28int VideoOverlay::sYuvCount = 0;
29int VideoOverlay::sYuvLayerIndex = -1;
30bool VideoOverlay::sIsModeOn = false;
31bool VideoOverlay::sIsLayerSkip = false;
32
33//Cache stats, figure out the state, config overlay
34bool VideoOverlay::prepare(hwc_context_t *ctx, hwc_layer_list_t *list) {
35 sIsModeOn = false;
36 chooseState(ctx);
37 //if the state chosen above is CLOSED, skip this block.
38 if(sState != ovutils::OV_CLOSED) {
39 if(configure(ctx, &list->hwLayers[sYuvLayerIndex])) {
40 markFlags(&list->hwLayers[sYuvLayerIndex]);
41 }
42 }
43
44 ALOGD_IF(VIDEO_DEBUG, "%s: stats: yuvCount = %d, yuvIndex = %d,"
45 "IsModeOn = %d, IsSkipLayer = %d", __FUNCTION__, sYuvCount,
46 sYuvLayerIndex, sIsModeOn, sIsLayerSkip);
47
48 return sIsModeOn;
49}
50
51void VideoOverlay::chooseState(hwc_context_t *ctx) {
52 ALOGD_IF(VIDEO_DEBUG, "%s: old state = %s", __FUNCTION__,
53 ovutils::getStateString(sState));
54
55 ovutils::eOverlayState newState = ovutils::OV_CLOSED;
56 //TODO check if device supports overlay and hdmi
57
58 //Support 1 video layer
59 if(sYuvCount == 1) {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070060 //Skip on primary, display on ext.
61 if(sIsLayerSkip && ctx->mExtDisplayObserver->getExternalDisplay()) {
Naseer Ahmedf48aef62012-07-20 09:05:53 -070062 //TODO
63 //VIDEO_ON_TV_ONLY
64 } else if(sIsLayerSkip) { //skip on primary, no ext
65 newState = ovutils::OV_CLOSED;
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070066 } else if(ctx->mExtDisplayObserver->getExternalDisplay()) {
67 //display on both
Naseer Ahmedf48aef62012-07-20 09:05:53 -070068 newState = ovutils::OV_2D_VIDEO_ON_PANEL_TV;
69 } else { //display on primary only
70 newState = ovutils::OV_2D_VIDEO_ON_PANEL;
71 }
72 }
73 sState = newState;
74 ALOGD_IF(VIDEO_DEBUG, "%s: new chosen state = %s", __FUNCTION__,
75 ovutils::getStateString(sState));
76}
77
78void VideoOverlay::markFlags(hwc_layer_t *layer) {
79 switch(sState) {
80 case ovutils::OV_2D_VIDEO_ON_PANEL:
81 case ovutils::OV_2D_VIDEO_ON_PANEL_TV:
82 layer->compositionType = HWC_OVERLAY;
83 layer->hints |= HWC_HINT_CLEAR_FB;
84 break;
85 //TODO
86 //case ovutils::OV_2D_VIDEO_ON_TV:
87 //just break, dont update flags.
88 default:
89 break;
90 }
91}
92
93bool VideoOverlay::configure(hwc_context_t *ctx, hwc_layer_t *layer)
94{
95 if (LIKELY(ctx->mOverlay)) {
96
97 overlay::Overlay& ov = *(ctx->mOverlay);
98 // Set overlay state
99 ov.setState(sState);
100
101 private_handle_t *hnd = (private_handle_t *)layer->handle;
102 ovutils::Whf info(hnd->width, hnd->height, hnd->format, hnd->size);
103
104 //TODO change this based on state.
105 ovutils::eDest dest = ovutils::OV_PIPE_ALL;
106
107 ovutils::eMdpFlags mdpFlags = ovutils::OV_MDP_FLAGS_NONE;
108 if (hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
109 ovutils::setMdpFlags(mdpFlags,
110 ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
111 }
112
113 ovutils::eIsFg isFgFlag = ovutils::IS_FG_OFF;
114 if (ctx->numHwLayers == 1) {
115 isFgFlag = ovutils::IS_FG_SET;
116 }
117
118 ovutils::PipeArgs parg(mdpFlags,
119 info,
120 ovutils::ZORDER_0,
121 isFgFlag,
122 ovutils::ROT_FLAG_DISABLED);
123 ovutils::PipeArgs pargs[ovutils::MAX_PIPES] = { parg, parg, parg };
124 ov.setSource(pargs, dest);
125
126 hwc_rect_t sourceCrop = layer->sourceCrop;
127 // x,y,w,h
128 ovutils::Dim dcrop(sourceCrop.left, sourceCrop.top,
129 sourceCrop.right - sourceCrop.left,
130 sourceCrop.bottom - sourceCrop.top);
131 //Only for External
132 ov.setCrop(dcrop, ovutils::OV_PIPE1);
133
134 // FIXME: Use source orientation for TV when source is portrait
135 //Only for External
136 ov.setTransform(0, dest);
137
138 ovutils::Dim dpos;
139 hwc_rect_t displayFrame = layer->displayFrame;
140 dpos.x = displayFrame.left;
141 dpos.y = displayFrame.top;
142 dpos.w = (displayFrame.right - displayFrame.left);
143 dpos.h = (displayFrame.bottom - displayFrame.top);
144
145 //Only for External
146 ov.setPosition(dpos, ovutils::OV_PIPE1);
147
148 //Calculate the rect for primary based on whether the supplied position
149 //is within or outside bounds.
150 const int fbWidth =
151 ovutils::FrameBufferInfo::getInstance()->getWidth();
152 const int fbHeight =
153 ovutils::FrameBufferInfo::getInstance()->getHeight();
154
155 if( displayFrame.left < 0 ||
156 displayFrame.top < 0 ||
157 displayFrame.right > fbWidth ||
158 displayFrame.bottom > fbHeight) {
159
160 calculate_crop_rects(sourceCrop, displayFrame, fbWidth, fbHeight);
161
162 //Update calculated width and height
163 dcrop.w = sourceCrop.right - sourceCrop.left;
164 dcrop.h = sourceCrop.bottom - sourceCrop.top;
165
166 dpos.w = displayFrame.right - displayFrame.left;
167 dpos.h = displayFrame.bottom - displayFrame.top;
168 }
169
170 //Only for Primary
171 ov.setCrop(dcrop, ovutils::OV_PIPE0);
172
173 int transform = layer->transform & FINAL_TRANSFORM_MASK;
174 ovutils::eTransform orient =
175 static_cast<ovutils::eTransform>(transform);
176 ov.setTransform(orient, ovutils::OV_PIPE0);
177
178 ov.setPosition(dpos, ovutils::OV_PIPE0);
179
180 //Both prim and external
181 if (!ov.commit(dest)) {
182 ALOGE("%s: commit fails", __FUNCTION__);
183 return false;
184 }
185
186 sIsModeOn = true;
187 }
188 return sIsModeOn;
189}
190
191bool VideoOverlay::draw(hwc_context_t *ctx, hwc_layer_list_t *list)
192{
193 if(!sIsModeOn || sYuvLayerIndex == -1) {
194 return true;
195 }
196
197 private_handle_t *hnd =
198 (private_handle_t *)list->hwLayers[sYuvLayerIndex].handle;
199
200 // Lock this buffer for read.
201 ctx->qbuf->lockAndAdd(hnd);
202 bool ret = true;
203 overlay::Overlay& ov = *(ctx->mOverlay);
204 ovutils::eOverlayState state = ov.getState();
205
206 switch (state) {
207 case ovutils::OV_2D_VIDEO_ON_PANEL_TV:
208 case ovutils::OV_3D_VIDEO_ON_2D_PANEL_2D_TV:
209 // Play external
210 if (!ov.queueBuffer(hnd->fd, hnd->offset, ovutils::OV_PIPE1)) {
211 ALOGE("%s: queueBuffer failed for external", __FUNCTION__);
212 ret = false;
213 }
214
215 // Play primary
216 if (!ov.queueBuffer(hnd->fd, hnd->offset, ovutils::OV_PIPE0)) {
217 ALOGE("%s: queueBuffer failed for primary", __FUNCTION__);
218 ret = false;
219 }
220
221 // Wait for external vsync to be done
222 if (!ov.waitForVsync(ovutils::OV_PIPE1)) {
223 ALOGE("%s: waitForVsync failed for external", __FUNCTION__);
224 ret = false;
225 }
226 break;
227 default:
228 // In most cases, displaying only to one (primary or external)
229 // so use OV_PIPE_ALL since overlay will ignore NullPipes
230 if (!ov.queueBuffer(hnd->fd, hnd->offset, ovutils::OV_PIPE_ALL)) {
231 ALOGE("%s: queueBuffer failed", __FUNCTION__);
232 ret = false;
233 }
234 break;
235 }
236
237 return ret;
238}
239
240
241}; //namespace qhwc