blob: 170c29ee688777d171c7d20b9343896d3297b571 [file] [log] [blame]
Naseer Ahmedf48aef62012-07-20 09:05:53 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
Saurabh Shah56f610d2012-08-07 15:27:06 -07003 * Copyright (C) 2012, The Linux Foundation. All rights reserved.
Naseer Ahmedf48aef62012-07-20 09:05:53 -07004 *
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
Naseer Ahmed72cf9762012-07-21 12:17:13 -070018#define VIDEO_DEBUG 0
19#include <overlay.h>
Naseer Ahmedf48aef62012-07-20 09:05:53 -070020#include "hwc_video.h"
Saurabh Shah3e858eb2012-09-17 16:53:21 -070021#include "hwc_utils.h"
Naseer Ahmedf48aef62012-07-20 09:05:53 -070022
23namespace qhwc {
24
25#define FINAL_TRANSFORM_MASK 0x000F
Naseer Ahmedf48aef62012-07-20 09:05:53 -070026
27//Static Members
28ovutils::eOverlayState VideoOverlay::sState = ovutils::OV_CLOSED;
Naseer Ahmedf48aef62012-07-20 09:05:53 -070029bool VideoOverlay::sIsModeOn = false;
Naseer Ahmedf48aef62012-07-20 09:05:53 -070030
31//Cache stats, figure out the state, config overlay
Saurabh Shah3e858eb2012-09-17 16:53:21 -070032bool VideoOverlay::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list,
33 int dpy) {
34 int yuvIndex = ctx->listStats[dpy].yuvIndex;
35
Naseer Ahmed96c4c952012-07-25 18:27:14 -070036 if(!ctx->mMDP.hasOverlay) {
Naseer Ahmed31da0b12012-07-31 18:55:33 -070037 ALOGD_IF(VIDEO_DEBUG,"%s, this hw doesnt support overlay", __FUNCTION__);
38 return false;
39 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -070040 if(yuvIndex == -1) {
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -070041 return false;
42 }
Naseer Ahmed4c588a22012-07-31 19:12:17 -070043
Saurabh Shah3e858eb2012-09-17 16:53:21 -070044 //index guaranteed to be not -1 at this point
45 hwc_layer_1_t *yuvLayer = &list->hwLayers[yuvIndex];
46 chooseState(ctx, dpy, yuvLayer);
47 if(configure(ctx, dpy, yuvLayer)) {
48 markFlags(yuvLayer);
49 sIsModeOn = true;
Naseer Ahmedf48aef62012-07-20 09:05:53 -070050 }
51
Naseer Ahmedf48aef62012-07-20 09:05:53 -070052 return sIsModeOn;
53}
54
Saurabh Shah3e858eb2012-09-17 16:53:21 -070055void VideoOverlay::chooseState(hwc_context_t *ctx, int dpy,
56 hwc_layer_1_t *yuvLayer) {
Naseer Ahmedf48aef62012-07-20 09:05:53 -070057 ALOGD_IF(VIDEO_DEBUG, "%s: old state = %s", __FUNCTION__,
58 ovutils::getStateString(sState));
59
Saurabh Shah3e858eb2012-09-17 16:53:21 -070060 private_handle_t *hnd = NULL;
61 if(yuvLayer) {
62 hnd = (private_handle_t *)yuvLayer->handle;
Naseer Ahmedf48aef62012-07-20 09:05:53 -070063 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -070064 ovutils::eOverlayState newState = ovutils::OV_CLOSED;
65 switch(dpy) {
66 case HWC_DISPLAY_PRIMARY:
67 if(ctx->listStats[dpy].yuvCount == 1) {
Saurabh Shah86623d72012-09-25 19:39:17 -070068 newState = isExternalActive(ctx) ?
69 ovutils::OV_2D_VIDEO_ON_PANEL_TV : ovutils::OV_2D_VIDEO_ON_PANEL;
Saurabh Shah3e858eb2012-09-17 16:53:21 -070070 if(isSkipLayer(yuvLayer) && !isSecureBuffer(hnd)) {
Saurabh Shah86623d72012-09-25 19:39:17 -070071 newState = isExternalActive(ctx) ?
72 ovutils::OV_2D_VIDEO_ON_TV : ovutils::OV_CLOSED;
Saurabh Shah3e858eb2012-09-17 16:53:21 -070073 }
74 }
75 break;
76 case HWC_DISPLAY_EXTERNAL:
77 //TODO needs overlay state change for UI also
78 newState = sState; //Previously set by HWC_DISPLAY_PRIMARY
79 /*if(ctx->listStats[dpy].yuvCount == 1 && isExternalActive(ctx)) {
80 if(!isSkipLayer(yuvLayer) || isSecureBuffer(hnd)) {
81 switch(sState) { //set by primary chooseState
82 case ovutils::OV_2D_VIDEO_ON_PANEL:
83 //upgrade
84 sState = ovutils::OV_2D_VIDEO_PANEL_TV;
85 break;
86 case ovutils::OV_CLOSED:
87 sState = ovutils::OV_2D_VIDEO_ON_TV;
88 break;
89 }
90 }
91 }*/
92 break;
93 default:
94 break;
95 }
96
Naseer Ahmedf48aef62012-07-20 09:05:53 -070097 sState = newState;
98 ALOGD_IF(VIDEO_DEBUG, "%s: new chosen state = %s", __FUNCTION__,
99 ovutils::getStateString(sState));
100}
101
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700102void VideoOverlay::markFlags(hwc_layer_1_t *yuvLayer) {
103 if(yuvLayer) {
104 yuvLayer->compositionType = HWC_OVERLAY;
105 yuvLayer->hints |= HWC_HINT_CLEAR_FB;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700106 }
107}
108
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700109/* Helpers */
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700110bool configPrimVid(hwc_context_t *ctx, hwc_layer_1_t *layer) {
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700111 overlay::Overlay& ov = *(ctx->mOverlay);
112 private_handle_t *hnd = (private_handle_t *)layer->handle;
113 ovutils::Whf info(hnd->width, hnd->height, hnd->format, hnd->size);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700114
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700115 ovutils::eMdpFlags mdpFlags = ovutils::OV_MDP_FLAGS_NONE;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700116 if (isSecureBuffer(hnd)) {
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700117 ovutils::setMdpFlags(mdpFlags,
118 ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
119 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700120
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700121 ovutils::eIsFg isFgFlag = ovutils::IS_FG_OFF;
122 if (ctx->numHwLayers == 1) {
123 isFgFlag = ovutils::IS_FG_SET;
124 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700125
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700126 ovutils::PipeArgs parg(mdpFlags,
127 info,
128 ovutils::ZORDER_0,
129 isFgFlag,
130 ovutils::ROT_FLAG_DISABLED);
131 ovutils::PipeArgs pargs[ovutils::MAX_PIPES] = { parg, parg, parg };
132 ov.setSource(pargs, ovutils::OV_PIPE0);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700133
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700134 hwc_rect_t sourceCrop = layer->sourceCrop;
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700135 hwc_rect_t displayFrame = layer->displayFrame;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700136
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700137 //Calculate the rect for primary based on whether the supplied position
138 //is within or outside bounds.
139 const int fbWidth =
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700140 ovutils::FrameBufferInfo::getInstance()->getWidth();
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700141 const int fbHeight =
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700142 ovutils::FrameBufferInfo::getInstance()->getHeight();
143
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700144 if( displayFrame.left < 0 ||
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700145 displayFrame.top < 0 ||
146 displayFrame.right > fbWidth ||
147 displayFrame.bottom > fbHeight) {
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700148 calculate_crop_rects(sourceCrop, displayFrame, fbWidth, fbHeight);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700149 }
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700150
Saurabh Shah1023ce22012-09-05 18:45:59 -0700151 // source crop x,y,w,h
152 ovutils::Dim dcrop(sourceCrop.left, sourceCrop.top,
153 sourceCrop.right - sourceCrop.left,
154 sourceCrop.bottom - sourceCrop.top);
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700155 //Only for Primary
156 ov.setCrop(dcrop, ovutils::OV_PIPE0);
157
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700158 int transform = layer->transform;
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700159 ovutils::eTransform orient =
160 static_cast<ovutils::eTransform>(transform);
161 ov.setTransform(orient, ovutils::OV_PIPE0);
162
Saurabh Shah1023ce22012-09-05 18:45:59 -0700163 // position x,y,w,h
164 ovutils::Dim dpos(displayFrame.left,
165 displayFrame.top,
166 displayFrame.right - displayFrame.left,
167 displayFrame.bottom - displayFrame.top);
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700168 ov.setPosition(dpos, ovutils::OV_PIPE0);
169
170 if (!ov.commit(ovutils::OV_PIPE0)) {
171 ALOGE("%s: commit fails", __FUNCTION__);
172 return false;
173 }
174 return true;
175}
176
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700177bool configExtVid(hwc_context_t *ctx, hwc_layer_1_t *layer) {
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700178 overlay::Overlay& ov = *(ctx->mOverlay);
179 private_handle_t *hnd = (private_handle_t *)layer->handle;
180 ovutils::Whf info(hnd->width, hnd->height, hnd->format, hnd->size);
181
182 ovutils::eMdpFlags mdpFlags = ovutils::OV_MDP_FLAGS_NONE;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700183 if (isSecureBuffer(hnd)) {
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700184 ovutils::setMdpFlags(mdpFlags,
185 ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
186 }
187
188 ovutils::eIsFg isFgFlag = ovutils::IS_FG_OFF;
189 if (ctx->numHwLayers == 1) {
190 isFgFlag = ovutils::IS_FG_SET;
191 }
192
193 ovutils::PipeArgs parg(mdpFlags,
194 info,
195 ovutils::ZORDER_0,
196 isFgFlag,
197 ovutils::ROT_FLAG_DISABLED);
198 ovutils::PipeArgs pargs[ovutils::MAX_PIPES] = { parg, parg, parg };
199 ov.setSource(pargs, ovutils::OV_PIPE1);
200
201 hwc_rect_t sourceCrop = layer->sourceCrop;
202 // x,y,w,h
203 ovutils::Dim dcrop(sourceCrop.left, sourceCrop.top,
204 sourceCrop.right - sourceCrop.left,
205 sourceCrop.bottom - sourceCrop.top);
206 //Only for External
207 ov.setCrop(dcrop, ovutils::OV_PIPE1);
208
209 // FIXME: Use source orientation for TV when source is portrait
210 //Only for External
211 ov.setTransform(0, ovutils::OV_PIPE1);
212
213 ovutils::Dim dpos;
214 hwc_rect_t displayFrame = layer->displayFrame;
215 dpos.x = displayFrame.left;
216 dpos.y = displayFrame.top;
217 dpos.w = (displayFrame.right - displayFrame.left);
218 dpos.h = (displayFrame.bottom - displayFrame.top);
219
220 //Only for External
221 ov.setPosition(dpos, ovutils::OV_PIPE1);
222
223 if (!ov.commit(ovutils::OV_PIPE1)) {
224 ALOGE("%s: commit fails", __FUNCTION__);
225 return false;
226 }
227 return true;
228}
229
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700230bool VideoOverlay::configure(hwc_context_t *ctx, int dpy,
231 hwc_layer_1_t *yuvLayer) {
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700232 bool ret = true;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700233 overlay::Overlay& ov = *(ctx->mOverlay);
234 switch(dpy) {
235 case HWC_DISPLAY_PRIMARY:
236 // Set overlay state
237 ov.setState(sState);
238 switch(sState) {
239 case ovutils::OV_2D_VIDEO_ON_PANEL:
240 ret &= configPrimVid(ctx, yuvLayer);
241 break;
Saurabh Shah86623d72012-09-25 19:39:17 -0700242 case ovutils::OV_2D_VIDEO_ON_PANEL_TV:
243 ret &= configPrimVid(ctx, yuvLayer);
244 ret &= configExtVid(ctx, yuvLayer);
245 break;
246 case ovutils::OV_2D_VIDEO_ON_TV:
247 ret &= configExtVid(ctx, yuvLayer);
248 break;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700249 default:
250 return false;
251 }
252 break;
253 case HWC_DISPLAY_EXTERNAL:
254 ov.setState(sState);
255 switch(sState) {
256 case ovutils::OV_2D_VIDEO_ON_PANEL_TV:
257 case ovutils::OV_2D_VIDEO_ON_TV:
258 ret = configExtVid(ctx, yuvLayer);
259 break;
260 default:
261 return false;
262 }
263 break;
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700264 }
265 return ret;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700266}
267
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700268bool VideoOverlay::draw(hwc_context_t *ctx, hwc_display_contents_1_t *list,
269 int dpy)
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700270{
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700271 int yuvIndex = ctx->listStats[dpy].yuvIndex;
272 if(!sIsModeOn || yuvIndex == -1) {
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700273 return true;
274 }
275
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700276 private_handle_t *hnd = (private_handle_t *)
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700277 list->hwLayers[yuvIndex].handle;
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700278
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700279 bool ret = true;
280 overlay::Overlay& ov = *(ctx->mOverlay);
281 ovutils::eOverlayState state = ov.getState();
282
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700283 switch(dpy) {
284 case HWC_DISPLAY_PRIMARY:
285 switch (state) {
286 case ovutils::OV_2D_VIDEO_ON_PANEL:
287 // Play primary
288 if (!ov.queueBuffer(hnd->fd, hnd->offset, ovutils::OV_PIPE0)) {
289 ALOGE("%s: queueBuffer failed for primary", __FUNCTION__);
290 ret = false;
291 }
292 break;
Saurabh Shah86623d72012-09-25 19:39:17 -0700293 case ovutils::OV_2D_VIDEO_ON_PANEL_TV:
294 if (!ov.queueBuffer(hnd->fd, hnd->offset, ovutils::OV_PIPE0)) {
295 ALOGE("%s: queueBuffer failed for primary", __FUNCTION__);
296 ret = false;
297 }
298 // Play external
299 if (!ov.queueBuffer(hnd->fd, hnd->offset, ovutils::OV_PIPE1)) {
300 ALOGE("%s: queueBuffer failed for external", __FUNCTION__);
301 ret = false;
302 }
303 break;
304 case ovutils::OV_2D_VIDEO_ON_TV:
305 // Play external
306 if (!ov.queueBuffer(hnd->fd, hnd->offset, ovutils::OV_PIPE1)) {
307 ALOGE("%s: queueBuffer failed for external", __FUNCTION__);
308 ret = false;
309 }
310 break;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700311 default:
312 ret = false;
313 break;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700314 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700315 break;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700316 case HWC_DISPLAY_EXTERNAL:
317 switch(state) {
318 case ovutils::OV_2D_VIDEO_ON_PANEL_TV:
319 case ovutils::OV_2D_VIDEO_ON_TV:
320 // Play external
321 if (!ov.queueBuffer(hnd->fd, hnd->offset, ovutils::OV_PIPE1)) {
322 ALOGE("%s: queueBuffer failed for external", __FUNCTION__);
323 ret = false;
324 }
325 break;
326 default:
327 ret = false;
328 break;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700329 }
330 break;
331 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700332 return ret;
333}
334
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700335}; //namespace qhwc