blob: 141d83bcb95a15a568bc48133a3341118a8b97ad [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>
20#include "hwc_qbuf.h"
Naseer Ahmedf48aef62012-07-20 09:05:53 -070021#include "hwc_video.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070022#include "external.h"
Naseer Ahmedf48aef62012-07-20 09:05:53 -070023
24namespace qhwc {
25
26#define FINAL_TRANSFORM_MASK 0x000F
Naseer Ahmedf48aef62012-07-20 09:05:53 -070027
28//Static Members
29ovutils::eOverlayState VideoOverlay::sState = ovutils::OV_CLOSED;
30int VideoOverlay::sYuvCount = 0;
31int VideoOverlay::sYuvLayerIndex = -1;
Naseer Ahmed4c588a22012-07-31 19:12:17 -070032bool VideoOverlay::sIsYuvLayerSkip = false;
33int VideoOverlay::sCCLayerIndex = -1;
Naseer Ahmedf48aef62012-07-20 09:05:53 -070034bool VideoOverlay::sIsModeOn = false;
Naseer Ahmedf48aef62012-07-20 09:05:53 -070035
36//Cache stats, figure out the state, config overlay
Naseer Ahmed5b6708a2012-08-02 13:46:08 -070037bool VideoOverlay::prepare(hwc_context_t *ctx, hwc_display_contents_1_t *list) {
Naseer Ahmedf48aef62012-07-20 09:05:53 -070038 sIsModeOn = false;
Naseer Ahmed96c4c952012-07-25 18:27:14 -070039 if(!ctx->mMDP.hasOverlay) {
Naseer Ahmed31da0b12012-07-31 18:55:33 -070040 ALOGD_IF(VIDEO_DEBUG,"%s, this hw doesnt support overlay", __FUNCTION__);
41 return false;
42 }
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -070043 if(sYuvLayerIndex == -1) {
44 return false;
45 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -070046 chooseState(ctx);
47 //if the state chosen above is CLOSED, skip this block.
48 if(sState != ovutils::OV_CLOSED) {
Naseer Ahmed5b6708a2012-08-02 13:46:08 -070049 hwc_layer_1_t *yuvLayer = &list->hwLayers[sYuvLayerIndex];
50 hwc_layer_1_t *ccLayer = NULL;
Naseer Ahmed4c588a22012-07-31 19:12:17 -070051 if(sCCLayerIndex != -1)
52 ccLayer = &list->hwLayers[sCCLayerIndex];
53
54 if(configure(ctx, yuvLayer, ccLayer)) {
Naseer Ahmedf48aef62012-07-20 09:05:53 -070055 markFlags(&list->hwLayers[sYuvLayerIndex]);
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -070056 sIsModeOn = true;
Naseer Ahmedf48aef62012-07-20 09:05:53 -070057 }
58 }
59
60 ALOGD_IF(VIDEO_DEBUG, "%s: stats: yuvCount = %d, yuvIndex = %d,"
Naseer Ahmed4c588a22012-07-31 19:12:17 -070061 "IsYuvLayerSkip = %d, ccLayerIndex = %d, IsModeOn = %d",
62 __FUNCTION__, sYuvCount, sYuvLayerIndex,
63 sIsYuvLayerSkip, sCCLayerIndex, sIsModeOn);
Naseer Ahmedf48aef62012-07-20 09:05:53 -070064
65 return sIsModeOn;
66}
67
68void VideoOverlay::chooseState(hwc_context_t *ctx) {
69 ALOGD_IF(VIDEO_DEBUG, "%s: old state = %s", __FUNCTION__,
70 ovutils::getStateString(sState));
71
72 ovutils::eOverlayState newState = ovutils::OV_CLOSED;
Naseer Ahmedf48aef62012-07-20 09:05:53 -070073
74 //Support 1 video layer
75 if(sYuvCount == 1) {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070076 //Skip on primary, display on ext.
Naseer Ahmed4c588a22012-07-31 19:12:17 -070077 if(sIsYuvLayerSkip && ctx->mExtDisplay->getExternalDisplay()) {
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -070078 newState = ovutils::OV_2D_VIDEO_ON_TV;
Naseer Ahmed4c588a22012-07-31 19:12:17 -070079 } else if(sIsYuvLayerSkip) { //skip on primary, no ext
Naseer Ahmedf48aef62012-07-20 09:05:53 -070080 newState = ovutils::OV_CLOSED;
Naseer Ahmed72cf9762012-07-21 12:17:13 -070081 } else if(ctx->mExtDisplay->getExternalDisplay()) {
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070082 //display on both
Naseer Ahmedf48aef62012-07-20 09:05:53 -070083 newState = ovutils::OV_2D_VIDEO_ON_PANEL_TV;
84 } else { //display on primary only
85 newState = ovutils::OV_2D_VIDEO_ON_PANEL;
86 }
87 }
88 sState = newState;
89 ALOGD_IF(VIDEO_DEBUG, "%s: new chosen state = %s", __FUNCTION__,
90 ovutils::getStateString(sState));
91}
92
Naseer Ahmed5b6708a2012-08-02 13:46:08 -070093void VideoOverlay::markFlags(hwc_layer_1_t *layer) {
Naseer Ahmedf48aef62012-07-20 09:05:53 -070094 switch(sState) {
95 case ovutils::OV_2D_VIDEO_ON_PANEL:
96 case ovutils::OV_2D_VIDEO_ON_PANEL_TV:
97 layer->compositionType = HWC_OVERLAY;
98 layer->hints |= HWC_HINT_CLEAR_FB;
99 break;
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700100 case ovutils::OV_2D_VIDEO_ON_TV:
101 break; //dont update flags.
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700102 default:
103 break;
104 }
105}
106
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700107/* Helpers */
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700108bool configPrimVid(hwc_context_t *ctx, hwc_layer_1_t *layer) {
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700109 overlay::Overlay& ov = *(ctx->mOverlay);
110 private_handle_t *hnd = (private_handle_t *)layer->handle;
111 ovutils::Whf info(hnd->width, hnd->height, hnd->format, hnd->size);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700112
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700113 ovutils::eMdpFlags mdpFlags = ovutils::OV_MDP_FLAGS_NONE;
114 if (hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
115 ovutils::setMdpFlags(mdpFlags,
116 ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
117 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700118
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700119 ovutils::eIsFg isFgFlag = ovutils::IS_FG_OFF;
120 if (ctx->numHwLayers == 1) {
121 isFgFlag = ovutils::IS_FG_SET;
122 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700123
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700124 ovutils::PipeArgs parg(mdpFlags,
125 info,
126 ovutils::ZORDER_0,
127 isFgFlag,
128 ovutils::ROT_FLAG_DISABLED);
129 ovutils::PipeArgs pargs[ovutils::MAX_PIPES] = { parg, parg, parg };
130 ov.setSource(pargs, ovutils::OV_PIPE0);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700131
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700132 hwc_rect_t sourceCrop = layer->sourceCrop;
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700133 hwc_rect_t displayFrame = layer->displayFrame;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700134
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700135 //Calculate the rect for primary based on whether the supplied position
136 //is within or outside bounds.
137 const int fbWidth =
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700138 ovutils::FrameBufferInfo::getInstance()->getWidth();
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700139 const int fbHeight =
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700140 ovutils::FrameBufferInfo::getInstance()->getHeight();
141
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700142 if( displayFrame.left < 0 ||
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700143 displayFrame.top < 0 ||
144 displayFrame.right > fbWidth ||
145 displayFrame.bottom > fbHeight) {
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700146 calculate_crop_rects(sourceCrop, displayFrame, fbWidth, fbHeight);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700147 }
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700148
Saurabh Shah1023ce22012-09-05 18:45:59 -0700149 // source crop x,y,w,h
150 ovutils::Dim dcrop(sourceCrop.left, sourceCrop.top,
151 sourceCrop.right - sourceCrop.left,
152 sourceCrop.bottom - sourceCrop.top);
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700153 //Only for Primary
154 ov.setCrop(dcrop, ovutils::OV_PIPE0);
155
156 int transform = layer->transform & FINAL_TRANSFORM_MASK;
157 ovutils::eTransform orient =
158 static_cast<ovutils::eTransform>(transform);
159 ov.setTransform(orient, ovutils::OV_PIPE0);
160
Saurabh Shah1023ce22012-09-05 18:45:59 -0700161 // position x,y,w,h
162 ovutils::Dim dpos(displayFrame.left,
163 displayFrame.top,
164 displayFrame.right - displayFrame.left,
165 displayFrame.bottom - displayFrame.top);
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700166 ov.setPosition(dpos, ovutils::OV_PIPE0);
167
168 if (!ov.commit(ovutils::OV_PIPE0)) {
169 ALOGE("%s: commit fails", __FUNCTION__);
170 return false;
171 }
172 return true;
173}
174
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700175bool configExtVid(hwc_context_t *ctx, hwc_layer_1_t *layer) {
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700176 overlay::Overlay& ov = *(ctx->mOverlay);
177 private_handle_t *hnd = (private_handle_t *)layer->handle;
178 ovutils::Whf info(hnd->width, hnd->height, hnd->format, hnd->size);
179
180 ovutils::eMdpFlags mdpFlags = ovutils::OV_MDP_FLAGS_NONE;
181 if (hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
182 ovutils::setMdpFlags(mdpFlags,
183 ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
184 }
185
186 ovutils::eIsFg isFgFlag = ovutils::IS_FG_OFF;
187 if (ctx->numHwLayers == 1) {
188 isFgFlag = ovutils::IS_FG_SET;
189 }
190
191 ovutils::PipeArgs parg(mdpFlags,
192 info,
193 ovutils::ZORDER_0,
194 isFgFlag,
195 ovutils::ROT_FLAG_DISABLED);
196 ovutils::PipeArgs pargs[ovutils::MAX_PIPES] = { parg, parg, parg };
197 ov.setSource(pargs, ovutils::OV_PIPE1);
198
199 hwc_rect_t sourceCrop = layer->sourceCrop;
200 // x,y,w,h
201 ovutils::Dim dcrop(sourceCrop.left, sourceCrop.top,
202 sourceCrop.right - sourceCrop.left,
203 sourceCrop.bottom - sourceCrop.top);
204 //Only for External
205 ov.setCrop(dcrop, ovutils::OV_PIPE1);
206
207 // FIXME: Use source orientation for TV when source is portrait
208 //Only for External
209 ov.setTransform(0, ovutils::OV_PIPE1);
210
211 ovutils::Dim dpos;
212 hwc_rect_t displayFrame = layer->displayFrame;
213 dpos.x = displayFrame.left;
214 dpos.y = displayFrame.top;
215 dpos.w = (displayFrame.right - displayFrame.left);
216 dpos.h = (displayFrame.bottom - displayFrame.top);
217
218 //Only for External
219 ov.setPosition(dpos, ovutils::OV_PIPE1);
220
221 if (!ov.commit(ovutils::OV_PIPE1)) {
222 ALOGE("%s: commit fails", __FUNCTION__);
223 return false;
224 }
225 return true;
226}
227
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700228bool configExtCC(hwc_context_t *ctx, hwc_layer_1_t *layer) {
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700229 if(layer == NULL)
230 return true;
231
232 overlay::Overlay& ov = *(ctx->mOverlay);
233 private_handle_t *hnd = (private_handle_t *)layer->handle;
234 ovutils::Whf info(hnd->width, hnd->height, hnd->format, hnd->size);
235 ovutils::eIsFg isFgFlag = ovutils::IS_FG_OFF;
236 ovutils::eMdpFlags mdpFlags = ovutils::OV_MDP_FLAGS_NONE;
237 ovutils::PipeArgs parg(mdpFlags,
238 info,
239 ovutils::ZORDER_1,
240 isFgFlag,
241 ovutils::ROT_FLAG_DISABLED);
242 ovutils::PipeArgs pargs[ovutils::MAX_PIPES] = { parg, parg, parg };
243 ov.setSource(pargs, ovutils::OV_PIPE2);
244
245 hwc_rect_t sourceCrop = layer->sourceCrop;
246 // x,y,w,h
247 ovutils::Dim dcrop(sourceCrop.left, sourceCrop.top,
248 sourceCrop.right - sourceCrop.left,
249 sourceCrop.bottom - sourceCrop.top);
250 //Only for External
251 ov.setCrop(dcrop, ovutils::OV_PIPE2);
252
253 // FIXME: Use source orientation for TV when source is portrait
254 //Only for External
255 ov.setTransform(0, ovutils::OV_PIPE2);
256
257 //Setting position same as crop
258 //FIXME stretch to full screen
259 ov.setPosition(dcrop, ovutils::OV_PIPE2);
260
261 if (!ov.commit(ovutils::OV_PIPE2)) {
262 ALOGE("%s: commit fails", __FUNCTION__);
263 return false;
264 }
265 return true;
266}
267
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700268bool VideoOverlay::configure(hwc_context_t *ctx, hwc_layer_1_t *yuvLayer,
269 hwc_layer_1_t *ccLayer) {
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700270
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700271 bool ret = true;
272 if (LIKELY(ctx->mOverlay)) {
273 overlay::Overlay& ov = *(ctx->mOverlay);
274 // Set overlay state
275 ov.setState(sState);
276 switch(sState) {
277 case ovutils::OV_2D_VIDEO_ON_PANEL:
278 ret &= configPrimVid(ctx, yuvLayer);
279 break;
280 case ovutils::OV_2D_VIDEO_ON_PANEL_TV:
281 ret &= configExtVid(ctx, yuvLayer);
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700282 ret &= configExtCC(ctx, ccLayer);
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700283 ret &= configPrimVid(ctx, yuvLayer);
284 break;
285 case ovutils::OV_2D_VIDEO_ON_TV:
286 ret &= configExtVid(ctx, yuvLayer);
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700287 ret &= configExtCC(ctx, ccLayer);
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700288 break;
289 default:
290 return false;
291 }
292 } else {
293 //Ov null
294 return false;
295 }
296 return ret;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700297}
298
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700299bool VideoOverlay::draw(hwc_context_t *ctx, hwc_display_contents_1_t *list)
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700300{
301 if(!sIsModeOn || sYuvLayerIndex == -1) {
302 return true;
303 }
304
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700305 private_handle_t *hnd = (private_handle_t *)
306 list->hwLayers[sYuvLayerIndex].handle;
307
308 private_handle_t *cchnd = NULL;
309 if(sCCLayerIndex != -1) {
310 cchnd = (private_handle_t *)list->hwLayers[sCCLayerIndex].handle;
311 ctx->qbuf->lockAndAdd(cchnd);
312 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700313
314 // Lock this buffer for read.
315 ctx->qbuf->lockAndAdd(hnd);
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700316
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700317 bool ret = true;
318 overlay::Overlay& ov = *(ctx->mOverlay);
319 ovutils::eOverlayState state = ov.getState();
320
321 switch (state) {
322 case ovutils::OV_2D_VIDEO_ON_PANEL_TV:
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700323 // Play external
324 if (!ov.queueBuffer(hnd->fd, hnd->offset, ovutils::OV_PIPE1)) {
325 ALOGE("%s: queueBuffer failed for external", __FUNCTION__);
326 ret = false;
327 }
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700328 //Play CC on external
329 if (cchnd && !ov.queueBuffer(cchnd->fd, cchnd->offset,
330 ovutils::OV_PIPE2)) {
331 ALOGE("%s: queueBuffer failed for cc external", __FUNCTION__);
332 ret = false;
333 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700334 // Play primary
335 if (!ov.queueBuffer(hnd->fd, hnd->offset, ovutils::OV_PIPE0)) {
336 ALOGE("%s: queueBuffer failed for primary", __FUNCTION__);
337 ret = false;
338 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700339 break;
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700340 case ovutils::OV_2D_VIDEO_ON_PANEL:
341 // Play primary
342 if (!ov.queueBuffer(hnd->fd, hnd->offset, ovutils::OV_PIPE0)) {
343 ALOGE("%s: queueBuffer failed for primary", __FUNCTION__);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700344 ret = false;
345 }
346 break;
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700347 case ovutils::OV_2D_VIDEO_ON_TV:
348 // Play external
349 if (!ov.queueBuffer(hnd->fd, hnd->offset, ovutils::OV_PIPE1)) {
350 ALOGE("%s: queueBuffer failed for external", __FUNCTION__);
351 ret = false;
352 }
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700353 //Play CC on external
354 if (cchnd && !ov.queueBuffer(cchnd->fd, cchnd->offset,
355 ovutils::OV_PIPE2)) {
356 ALOGE("%s: queueBuffer failed for cc external", __FUNCTION__);
357 ret = false;
358 }
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700359 break;
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700360 default:
361 ALOGE("%s Unused state %s", __FUNCTION__,
362 ovutils::getStateString(state));
363 break;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700364 }
365
366 return ret;
367}
368
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700369}; //namespace qhwc