blob: d5f5114b21320933f0599d60b932fd8998272ccd [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
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700128 ovutils::eIsFg isFgFlag = ovutils::IS_FG_OFF;
129 if (ctx->numHwLayers == 1) {
130 isFgFlag = ovutils::IS_FG_SET;
131 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700132
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700133 ovutils::PipeArgs parg(mdpFlags,
134 info,
135 ovutils::ZORDER_0,
136 isFgFlag,
137 ovutils::ROT_FLAG_DISABLED);
138 ovutils::PipeArgs pargs[ovutils::MAX_PIPES] = { parg, parg, parg };
139 ov.setSource(pargs, ovutils::OV_PIPE0);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700140
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700141 hwc_rect_t sourceCrop = layer->sourceCrop;
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700142 hwc_rect_t displayFrame = layer->displayFrame;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700143
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700144 //Calculate the rect for primary based on whether the supplied position
145 //is within or outside bounds.
146 const int fbWidth =
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700147 ovutils::FrameBufferInfo::getInstance()->getWidth();
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700148 const int fbHeight =
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700149 ovutils::FrameBufferInfo::getInstance()->getHeight();
150
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700151 if( displayFrame.left < 0 ||
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700152 displayFrame.top < 0 ||
153 displayFrame.right > fbWidth ||
154 displayFrame.bottom > fbHeight) {
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700155 calculate_crop_rects(sourceCrop, displayFrame, fbWidth, fbHeight);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700156 }
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700157
Saurabh Shah1023ce22012-09-05 18:45:59 -0700158 // source crop x,y,w,h
159 ovutils::Dim dcrop(sourceCrop.left, sourceCrop.top,
160 sourceCrop.right - sourceCrop.left,
161 sourceCrop.bottom - sourceCrop.top);
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700162 //Only for Primary
163 ov.setCrop(dcrop, ovutils::OV_PIPE0);
164
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700165 int transform = layer->transform;
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700166 ovutils::eTransform orient =
167 static_cast<ovutils::eTransform>(transform);
168 ov.setTransform(orient, ovutils::OV_PIPE0);
169
Saurabh Shah1023ce22012-09-05 18:45:59 -0700170 // position x,y,w,h
171 ovutils::Dim dpos(displayFrame.left,
172 displayFrame.top,
173 displayFrame.right - displayFrame.left,
174 displayFrame.bottom - displayFrame.top);
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700175 ov.setPosition(dpos, ovutils::OV_PIPE0);
176
177 if (!ov.commit(ovutils::OV_PIPE0)) {
178 ALOGE("%s: commit fails", __FUNCTION__);
179 return false;
180 }
181 return true;
182}
183
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700184bool configExtVid(hwc_context_t *ctx, hwc_layer_1_t *layer) {
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700185 overlay::Overlay& ov = *(ctx->mOverlay[HWC_DISPLAY_EXTERNAL]);
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700186 private_handle_t *hnd = (private_handle_t *)layer->handle;
187 ovutils::Whf info(hnd->width, hnd->height, hnd->format, hnd->size);
188
189 ovutils::eMdpFlags mdpFlags = ovutils::OV_MDP_FLAGS_NONE;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700190 if (isSecureBuffer(hnd)) {
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700191 ovutils::setMdpFlags(mdpFlags,
192 ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
193 }
194
195 ovutils::eIsFg isFgFlag = ovutils::IS_FG_OFF;
196 if (ctx->numHwLayers == 1) {
197 isFgFlag = ovutils::IS_FG_SET;
198 }
199
200 ovutils::PipeArgs parg(mdpFlags,
201 info,
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700202 ovutils::ZORDER_1,
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700203 isFgFlag,
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700204 ovutils::ROT_FLAG_DISABLED);
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700205 ovutils::PipeArgs pargs[ovutils::MAX_PIPES] = { parg, parg, parg };
206 ov.setSource(pargs, ovutils::OV_PIPE1);
207
208 hwc_rect_t sourceCrop = layer->sourceCrop;
209 // x,y,w,h
210 ovutils::Dim dcrop(sourceCrop.left, sourceCrop.top,
211 sourceCrop.right - sourceCrop.left,
212 sourceCrop.bottom - sourceCrop.top);
213 //Only for External
214 ov.setCrop(dcrop, ovutils::OV_PIPE1);
215
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700216 int transform = layer->transform;
217 ovutils::eTransform orient =
218 static_cast<ovutils::eTransform>(transform);
219 ov.setTransform(orient, ovutils::OV_PIPE1);
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700220
221 ovutils::Dim dpos;
222 hwc_rect_t displayFrame = layer->displayFrame;
223 dpos.x = displayFrame.left;
224 dpos.y = displayFrame.top;
225 dpos.w = (displayFrame.right - displayFrame.left);
226 dpos.h = (displayFrame.bottom - displayFrame.top);
227
228 //Only for External
229 ov.setPosition(dpos, ovutils::OV_PIPE1);
230
231 if (!ov.commit(ovutils::OV_PIPE1)) {
232 ALOGE("%s: commit fails", __FUNCTION__);
233 return false;
234 }
235 return true;
236}
237
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700238bool VideoOverlay::configure(hwc_context_t *ctx, int dpy,
239 hwc_layer_1_t *yuvLayer) {
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700240 bool ret = true;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700241 overlay::Overlay& ov = *(ctx->mOverlay[dpy]);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700242 switch(dpy) {
243 case HWC_DISPLAY_PRIMARY:
244 // Set overlay state
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700245 ov.setState(sState[dpy]);
246 switch(sState[dpy]) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700247 case ovutils::OV_2D_VIDEO_ON_PANEL:
248 ret &= configPrimVid(ctx, yuvLayer);
249 break;
250 default:
251 return false;
252 }
253 break;
254 case HWC_DISPLAY_EXTERNAL:
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700255 ov.setState(sState[dpy]);
256 switch(sState[dpy]) {
257 case ovutils::OV_UI_VIDEO_TV:
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700258 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 Shahc4d034f2012-09-27 15:55:15 -0700271 if(!sIsModeOn[dpy]) {
272 return true;
273 }
274
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700275 int yuvIndex = ctx->listStats[dpy].yuvIndex;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700276 if(yuvIndex == -1) {
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700277 return true;
278 }
279
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700280 private_handle_t *hnd = (private_handle_t *)
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700281 list->hwLayers[yuvIndex].handle;
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700282
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700283 bool ret = true;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700284 overlay::Overlay& ov = *(ctx->mOverlay[dpy]);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700285 ovutils::eOverlayState state = ov.getState();
286
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700287 switch(dpy) {
288 case HWC_DISPLAY_PRIMARY:
289 switch (state) {
290 case ovutils::OV_2D_VIDEO_ON_PANEL:
291 // Play primary
292 if (!ov.queueBuffer(hnd->fd, hnd->offset, ovutils::OV_PIPE0)) {
293 ALOGE("%s: queueBuffer failed for primary", __FUNCTION__);
294 ret = false;
295 }
296 break;
297 default:
298 ret = false;
299 break;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700300 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700301 break;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700302 case HWC_DISPLAY_EXTERNAL:
303 switch(state) {
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700304 case ovutils::OV_UI_VIDEO_TV:
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700305 // 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;
311 default:
312 ret = false;
313 break;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700314 }
315 break;
316 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700317 return ret;
318}
319
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700320}; //namespace qhwc