blob: 77c93282a1606b377cf934453fbf795559062be8 [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
Saurabh Shahc4d034f2012-09-27 15:55:15 -070028ovutils::eOverlayState VideoOverlay::sState[] = {ovutils::OV_CLOSED};
29bool 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) {
Saurabh Shahc4d034f2012-09-27 15:55:15 -070034
Saurabh Shah3e858eb2012-09-17 16:53:21 -070035 int yuvIndex = ctx->listStats[dpy].yuvIndex;
Saurabh Shahc4d034f2012-09-27 15:55:15 -070036 sIsModeOn[dpy] = false;
Saurabh Shah3e858eb2012-09-17 16:53:21 -070037
Naseer Ahmed96c4c952012-07-25 18:27:14 -070038 if(!ctx->mMDP.hasOverlay) {
Naseer Ahmed31da0b12012-07-31 18:55:33 -070039 ALOGD_IF(VIDEO_DEBUG,"%s, this hw doesnt support overlay", __FUNCTION__);
40 return false;
41 }
Saurabh Shahc4d034f2012-09-27 15:55:15 -070042
43 if(yuvIndex == -1 || ctx->listStats[dpy].yuvCount != 1) {
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -070044 return false;
45 }
Naseer Ahmed4c588a22012-07-31 19:12:17 -070046
Saurabh Shah3e858eb2012-09-17 16:53:21 -070047 //index guaranteed to be not -1 at this point
48 hwc_layer_1_t *yuvLayer = &list->hwLayers[yuvIndex];
Naseer Ahmed68b88cf2012-10-02 14:15:12 -040049
Naseer Ahmedcb5f25b2012-10-04 15:56:00 -040050 private_handle_t *hnd = (private_handle_t *)yuvLayer->handle;
Naseer Ahmed68b88cf2012-10-02 14:15:12 -040051 if(ctx->mSecureMode) {
Naseer Ahmed68b88cf2012-10-02 14:15:12 -040052 if (! isSecureBuffer(hnd)) {
53 ALOGD_IF(VIDEO_DEBUG, "%s: Handle non-secure video layer"
54 "during secure playback gracefully", __FUNCTION__);
55 return false;
56 }
Naseer Ahmedcb5f25b2012-10-04 15:56:00 -040057 } else {
58 if (isSecureBuffer(hnd)) {
59 ALOGD_IF(VIDEO_DEBUG, "%s: Handle secure video layer"
60 "during non-secure playback gracefully", __FUNCTION__);
61 return false;
62 }
Naseer Ahmed68b88cf2012-10-02 14:15:12 -040063 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -070064 chooseState(ctx, dpy, yuvLayer);
65 if(configure(ctx, dpy, yuvLayer)) {
66 markFlags(yuvLayer);
Saurabh Shahc4d034f2012-09-27 15:55:15 -070067 sIsModeOn[dpy] = true;
Naseer Ahmedf48aef62012-07-20 09:05:53 -070068 }
69
Saurabh Shahc4d034f2012-09-27 15:55:15 -070070 return sIsModeOn[dpy];
Naseer Ahmedf48aef62012-07-20 09:05:53 -070071}
72
Saurabh Shah3e858eb2012-09-17 16:53:21 -070073void VideoOverlay::chooseState(hwc_context_t *ctx, int dpy,
74 hwc_layer_1_t *yuvLayer) {
Naseer Ahmedf48aef62012-07-20 09:05:53 -070075 ALOGD_IF(VIDEO_DEBUG, "%s: old state = %s", __FUNCTION__,
Saurabh Shahc4d034f2012-09-27 15:55:15 -070076 ovutils::getStateString(sState[dpy]));
Naseer Ahmedf48aef62012-07-20 09:05:53 -070077
Saurabh Shah3e858eb2012-09-17 16:53:21 -070078 private_handle_t *hnd = NULL;
79 if(yuvLayer) {
80 hnd = (private_handle_t *)yuvLayer->handle;
Naseer Ahmedf48aef62012-07-20 09:05:53 -070081 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -070082 ovutils::eOverlayState newState = ovutils::OV_CLOSED;
83 switch(dpy) {
84 case HWC_DISPLAY_PRIMARY:
85 if(ctx->listStats[dpy].yuvCount == 1) {
Saurabh Shahc4d034f2012-09-27 15:55:15 -070086 newState = ovutils::OV_2D_VIDEO_ON_PANEL;
Saurabh Shah3e858eb2012-09-17 16:53:21 -070087 if(isSkipLayer(yuvLayer) && !isSecureBuffer(hnd)) {
Saurabh Shahc4d034f2012-09-27 15:55:15 -070088 newState = ovutils::OV_CLOSED;
Saurabh Shah3e858eb2012-09-17 16:53:21 -070089 }
90 }
91 break;
92 case HWC_DISPLAY_EXTERNAL:
Saurabh Shahc4d034f2012-09-27 15:55:15 -070093 newState = ctx->mOverlay[HWC_DISPLAY_EXTERNAL]->getState(); //If we are here, external is active
94 if(ctx->listStats[dpy].yuvCount == 1) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -070095 if(!isSkipLayer(yuvLayer) || isSecureBuffer(hnd)) {
Saurabh Shahc4d034f2012-09-27 15:55:15 -070096 newState = ovutils::OV_UI_VIDEO_TV;
Saurabh Shah3e858eb2012-09-17 16:53:21 -070097 }
Saurabh Shahc4d034f2012-09-27 15:55:15 -070098 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -070099 break;
100 default:
101 break;
102 }
103
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700104 sState[dpy] = newState;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700105 ALOGD_IF(VIDEO_DEBUG, "%s: new chosen state = %s", __FUNCTION__,
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700106 ovutils::getStateString(sState[dpy]));
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700107}
108
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700109void VideoOverlay::markFlags(hwc_layer_1_t *yuvLayer) {
110 if(yuvLayer) {
111 yuvLayer->compositionType = HWC_OVERLAY;
112 yuvLayer->hints |= HWC_HINT_CLEAR_FB;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700113 }
114}
115
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700116/* Helpers */
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700117bool configPrimVid(hwc_context_t *ctx, hwc_layer_1_t *layer) {
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700118 overlay::Overlay& ov = *(ctx->mOverlay[HWC_DISPLAY_PRIMARY]);
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700119 private_handle_t *hnd = (private_handle_t *)layer->handle;
120 ovutils::Whf info(hnd->width, hnd->height, hnd->format, hnd->size);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700121
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700122 ovutils::eMdpFlags mdpFlags = ovutils::OV_MDP_FLAGS_NONE;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700123 if (isSecureBuffer(hnd)) {
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700124 ovutils::setMdpFlags(mdpFlags,
125 ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
126 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700127
Saurabh Shah91a6a992012-08-20 15:25:28 -0700128 if(layer->blending == HWC_BLENDING_PREMULT) {
129 ovutils::setMdpFlags(mdpFlags,
130 ovutils::OV_MDP_BLEND_FG_PREMULT);
131 }
132
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700133 ovutils::eIsFg isFgFlag = ovutils::IS_FG_OFF;
Saurabh Shah150806a2012-10-09 15:38:23 -0700134 if (ctx->listStats[HWC_DISPLAY_PRIMARY].numAppLayers == 1) {
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700135 isFgFlag = ovutils::IS_FG_SET;
136 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700137
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700138 ovutils::PipeArgs parg(mdpFlags,
139 info,
140 ovutils::ZORDER_0,
141 isFgFlag,
142 ovutils::ROT_FLAG_DISABLED);
143 ovutils::PipeArgs pargs[ovutils::MAX_PIPES] = { parg, parg, parg };
144 ov.setSource(pargs, ovutils::OV_PIPE0);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700145
Saurabh Shah27c1d652012-08-14 19:30:28 -0700146 int transform = layer->transform & FINAL_TRANSFORM_MASK;
147 ovutils::eTransform orient =
148 static_cast<ovutils::eTransform>(transform);
149
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700150 hwc_rect_t sourceCrop = layer->sourceCrop;
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700151 hwc_rect_t displayFrame = layer->displayFrame;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700152
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700153 //Calculate the rect for primary based on whether the supplied position
154 //is within or outside bounds.
155 const int fbWidth =
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700156 ovutils::FrameBufferInfo::getInstance()->getWidth();
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700157 const int fbHeight =
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700158 ovutils::FrameBufferInfo::getInstance()->getHeight();
159
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700160 if( displayFrame.left < 0 ||
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700161 displayFrame.top < 0 ||
162 displayFrame.right > fbWidth ||
163 displayFrame.bottom > fbHeight) {
Saurabh Shah27c1d652012-08-14 19:30:28 -0700164 calculate_crop_rects(sourceCrop, displayFrame, fbWidth, fbHeight,
165 transform);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700166 }
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700167
Saurabh Shah1023ce22012-09-05 18:45:59 -0700168 // source crop x,y,w,h
169 ovutils::Dim dcrop(sourceCrop.left, sourceCrop.top,
170 sourceCrop.right - sourceCrop.left,
171 sourceCrop.bottom - sourceCrop.top);
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700172 //Only for Primary
173 ov.setCrop(dcrop, ovutils::OV_PIPE0);
174
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700175 ov.setTransform(orient, ovutils::OV_PIPE0);
176
Saurabh Shah1023ce22012-09-05 18:45:59 -0700177 // position x,y,w,h
178 ovutils::Dim dpos(displayFrame.left,
179 displayFrame.top,
180 displayFrame.right - displayFrame.left,
181 displayFrame.bottom - displayFrame.top);
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700182 ov.setPosition(dpos, ovutils::OV_PIPE0);
183
184 if (!ov.commit(ovutils::OV_PIPE0)) {
185 ALOGE("%s: commit fails", __FUNCTION__);
186 return false;
187 }
188 return true;
189}
190
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700191bool configExtVid(hwc_context_t *ctx, hwc_layer_1_t *layer) {
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700192 overlay::Overlay& ov = *(ctx->mOverlay[HWC_DISPLAY_EXTERNAL]);
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700193 private_handle_t *hnd = (private_handle_t *)layer->handle;
194 ovutils::Whf info(hnd->width, hnd->height, hnd->format, hnd->size);
195
196 ovutils::eMdpFlags mdpFlags = ovutils::OV_MDP_FLAGS_NONE;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700197 if (isSecureBuffer(hnd)) {
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700198 ovutils::setMdpFlags(mdpFlags,
199 ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
200 }
201
Saurabh Shah91a6a992012-08-20 15:25:28 -0700202 if(layer->blending == HWC_BLENDING_PREMULT) {
203 ovutils::setMdpFlags(mdpFlags,
204 ovutils::OV_MDP_BLEND_FG_PREMULT);
205 }
206
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700207 ovutils::eIsFg isFgFlag = ovutils::IS_FG_OFF;
Saurabh Shah150806a2012-10-09 15:38:23 -0700208 if (ctx->listStats[HWC_DISPLAY_EXTERNAL].numAppLayers == 1) {
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700209 isFgFlag = ovutils::IS_FG_SET;
210 }
211
212 ovutils::PipeArgs parg(mdpFlags,
213 info,
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700214 ovutils::ZORDER_1,
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700215 isFgFlag,
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700216 ovutils::ROT_FLAG_DISABLED);
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700217 ovutils::PipeArgs pargs[ovutils::MAX_PIPES] = { parg, parg, parg };
218 ov.setSource(pargs, ovutils::OV_PIPE1);
219
220 hwc_rect_t sourceCrop = layer->sourceCrop;
221 // x,y,w,h
222 ovutils::Dim dcrop(sourceCrop.left, sourceCrop.top,
223 sourceCrop.right - sourceCrop.left,
224 sourceCrop.bottom - sourceCrop.top);
225 //Only for External
226 ov.setCrop(dcrop, ovutils::OV_PIPE1);
227
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700228 int transform = layer->transform;
229 ovutils::eTransform orient =
230 static_cast<ovutils::eTransform>(transform);
231 ov.setTransform(orient, ovutils::OV_PIPE1);
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700232
233 ovutils::Dim dpos;
234 hwc_rect_t displayFrame = layer->displayFrame;
235 dpos.x = displayFrame.left;
236 dpos.y = displayFrame.top;
237 dpos.w = (displayFrame.right - displayFrame.left);
238 dpos.h = (displayFrame.bottom - displayFrame.top);
239
240 //Only for External
241 ov.setPosition(dpos, ovutils::OV_PIPE1);
242
243 if (!ov.commit(ovutils::OV_PIPE1)) {
244 ALOGE("%s: commit fails", __FUNCTION__);
245 return false;
246 }
247 return true;
248}
249
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700250bool VideoOverlay::configure(hwc_context_t *ctx, int dpy,
251 hwc_layer_1_t *yuvLayer) {
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700252 bool ret = true;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700253 overlay::Overlay& ov = *(ctx->mOverlay[dpy]);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700254 switch(dpy) {
255 case HWC_DISPLAY_PRIMARY:
256 // Set overlay state
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700257 ov.setState(sState[dpy]);
258 switch(sState[dpy]) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700259 case ovutils::OV_2D_VIDEO_ON_PANEL:
260 ret &= configPrimVid(ctx, yuvLayer);
261 break;
262 default:
263 return false;
264 }
265 break;
266 case HWC_DISPLAY_EXTERNAL:
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700267 ov.setState(sState[dpy]);
268 switch(sState[dpy]) {
269 case ovutils::OV_UI_VIDEO_TV:
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700270 ret = configExtVid(ctx, yuvLayer);
271 break;
272 default:
273 return false;
274 }
275 break;
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700276 }
277 return ret;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700278}
279
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700280bool VideoOverlay::draw(hwc_context_t *ctx, hwc_display_contents_1_t *list,
281 int dpy)
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700282{
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700283 if(!sIsModeOn[dpy]) {
284 return true;
285 }
286
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700287 int yuvIndex = ctx->listStats[dpy].yuvIndex;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700288 if(yuvIndex == -1) {
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700289 return true;
290 }
291
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700292 private_handle_t *hnd = (private_handle_t *)
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700293 list->hwLayers[yuvIndex].handle;
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700294
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700295 bool ret = true;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700296 overlay::Overlay& ov = *(ctx->mOverlay[dpy]);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700297 ovutils::eOverlayState state = ov.getState();
298
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700299 switch(dpy) {
300 case HWC_DISPLAY_PRIMARY:
301 switch (state) {
302 case ovutils::OV_2D_VIDEO_ON_PANEL:
303 // Play primary
304 if (!ov.queueBuffer(hnd->fd, hnd->offset, ovutils::OV_PIPE0)) {
305 ALOGE("%s: queueBuffer failed for primary", __FUNCTION__);
306 ret = false;
307 }
308 break;
309 default:
310 ret = false;
311 break;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700312 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700313 break;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700314 case HWC_DISPLAY_EXTERNAL:
315 switch(state) {
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700316 case ovutils::OV_UI_VIDEO_TV:
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700317 // Play external
318 if (!ov.queueBuffer(hnd->fd, hnd->offset, ovutils::OV_PIPE1)) {
319 ALOGE("%s: queueBuffer failed for external", __FUNCTION__);
320 ret = false;
321 }
322 break;
323 default:
324 ret = false;
325 break;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700326 }
327 break;
328 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700329 return ret;
330}
331
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700332}; //namespace qhwc