blob: 4fb88d72da2c122247429647948e128f5913dea9 [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
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"
Naseer Ahmed72cf9762012-07-21 12:17:13 -070022#include "hwc_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
37bool VideoOverlay::prepare(hwc_context_t *ctx, hwc_layer_list_t *list) {
38 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 Ahmed4c588a22012-07-31 19:12:17 -070049 hwc_layer_t *yuvLayer = &list->hwLayers[sYuvLayerIndex];
50 hwc_layer_t *ccLayer = NULL;
51 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
93void VideoOverlay::markFlags(hwc_layer_t *layer) {
94 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 */
108bool configPrimVid(hwc_context_t *ctx, hwc_layer_t *layer) {
109 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;
133 // x,y,w,h
134 ovutils::Dim dcrop(sourceCrop.left, sourceCrop.top,
135 sourceCrop.right - sourceCrop.left,
136 sourceCrop.bottom - sourceCrop.top);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700137
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700138 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);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700144
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700145 //Calculate the rect for primary based on whether the supplied position
146 //is within or outside bounds.
147 const int fbWidth =
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700148 ovutils::FrameBufferInfo::getInstance()->getWidth();
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700149 const int fbHeight =
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700150 ovutils::FrameBufferInfo::getInstance()->getHeight();
151
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700152 if( displayFrame.left < 0 ||
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700153 displayFrame.top < 0 ||
154 displayFrame.right > fbWidth ||
155 displayFrame.bottom > fbHeight) {
156
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700157 calculate_crop_rects(sourceCrop, displayFrame, fbWidth, fbHeight);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700158
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700159 //Update calculated width and height
160 dcrop.w = sourceCrop.right - sourceCrop.left;
161 dcrop.h = sourceCrop.bottom - sourceCrop.top;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700162
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700163 dpos.w = displayFrame.right - displayFrame.left;
164 dpos.h = displayFrame.bottom - displayFrame.top;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700165 }
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700166
167 //Only for Primary
168 ov.setCrop(dcrop, ovutils::OV_PIPE0);
169
170 int transform = layer->transform & FINAL_TRANSFORM_MASK;
171 ovutils::eTransform orient =
172 static_cast<ovutils::eTransform>(transform);
173 ov.setTransform(orient, ovutils::OV_PIPE0);
174
175 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
184bool configExtVid(hwc_context_t *ctx, hwc_layer_t *layer) {
185 overlay::Overlay& ov = *(ctx->mOverlay);
186 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;
190 if (hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER) {
191 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,
202 ovutils::ZORDER_0,
203 isFgFlag,
204 ovutils::ROT_FLAG_DISABLED);
205 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
216 // FIXME: Use source orientation for TV when source is portrait
217 //Only for External
218 ov.setTransform(0, ovutils::OV_PIPE1);
219
220 ovutils::Dim dpos;
221 hwc_rect_t displayFrame = layer->displayFrame;
222 dpos.x = displayFrame.left;
223 dpos.y = displayFrame.top;
224 dpos.w = (displayFrame.right - displayFrame.left);
225 dpos.h = (displayFrame.bottom - displayFrame.top);
226
227 //Only for External
228 ov.setPosition(dpos, ovutils::OV_PIPE1);
229
230 if (!ov.commit(ovutils::OV_PIPE1)) {
231 ALOGE("%s: commit fails", __FUNCTION__);
232 return false;
233 }
234 return true;
235}
236
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700237bool configExtCC(hwc_context_t *ctx, hwc_layer_t *layer) {
238 if(layer == NULL)
239 return true;
240
241 overlay::Overlay& ov = *(ctx->mOverlay);
242 private_handle_t *hnd = (private_handle_t *)layer->handle;
243 ovutils::Whf info(hnd->width, hnd->height, hnd->format, hnd->size);
244 ovutils::eIsFg isFgFlag = ovutils::IS_FG_OFF;
245 ovutils::eMdpFlags mdpFlags = ovutils::OV_MDP_FLAGS_NONE;
246 ovutils::PipeArgs parg(mdpFlags,
247 info,
248 ovutils::ZORDER_1,
249 isFgFlag,
250 ovutils::ROT_FLAG_DISABLED);
251 ovutils::PipeArgs pargs[ovutils::MAX_PIPES] = { parg, parg, parg };
252 ov.setSource(pargs, ovutils::OV_PIPE2);
253
254 hwc_rect_t sourceCrop = layer->sourceCrop;
255 // x,y,w,h
256 ovutils::Dim dcrop(sourceCrop.left, sourceCrop.top,
257 sourceCrop.right - sourceCrop.left,
258 sourceCrop.bottom - sourceCrop.top);
259 //Only for External
260 ov.setCrop(dcrop, ovutils::OV_PIPE2);
261
262 // FIXME: Use source orientation for TV when source is portrait
263 //Only for External
264 ov.setTransform(0, ovutils::OV_PIPE2);
265
266 //Setting position same as crop
267 //FIXME stretch to full screen
268 ov.setPosition(dcrop, ovutils::OV_PIPE2);
269
270 if (!ov.commit(ovutils::OV_PIPE2)) {
271 ALOGE("%s: commit fails", __FUNCTION__);
272 return false;
273 }
274 return true;
275}
276
277bool VideoOverlay::configure(hwc_context_t *ctx, hwc_layer_t *yuvLayer,
278 hwc_layer_t *ccLayer) {
279
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700280 bool ret = true;
281 if (LIKELY(ctx->mOverlay)) {
282 overlay::Overlay& ov = *(ctx->mOverlay);
283 // Set overlay state
284 ov.setState(sState);
285 switch(sState) {
286 case ovutils::OV_2D_VIDEO_ON_PANEL:
287 ret &= configPrimVid(ctx, yuvLayer);
288 break;
289 case ovutils::OV_2D_VIDEO_ON_PANEL_TV:
290 ret &= configExtVid(ctx, yuvLayer);
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700291 ret &= configExtCC(ctx, ccLayer);
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700292 ret &= configPrimVid(ctx, yuvLayer);
293 break;
294 case ovutils::OV_2D_VIDEO_ON_TV:
295 ret &= configExtVid(ctx, yuvLayer);
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700296 ret &= configExtCC(ctx, ccLayer);
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700297 break;
298 default:
299 return false;
300 }
301 } else {
302 //Ov null
303 return false;
304 }
305 return ret;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700306}
307
308bool VideoOverlay::draw(hwc_context_t *ctx, hwc_layer_list_t *list)
309{
310 if(!sIsModeOn || sYuvLayerIndex == -1) {
311 return true;
312 }
313
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700314 private_handle_t *hnd = (private_handle_t *)
315 list->hwLayers[sYuvLayerIndex].handle;
316
317 private_handle_t *cchnd = NULL;
318 if(sCCLayerIndex != -1) {
319 cchnd = (private_handle_t *)list->hwLayers[sCCLayerIndex].handle;
320 ctx->qbuf->lockAndAdd(cchnd);
321 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700322
323 // Lock this buffer for read.
324 ctx->qbuf->lockAndAdd(hnd);
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700325
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700326 bool ret = true;
327 overlay::Overlay& ov = *(ctx->mOverlay);
328 ovutils::eOverlayState state = ov.getState();
329
330 switch (state) {
331 case ovutils::OV_2D_VIDEO_ON_PANEL_TV:
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700332 // Play external
333 if (!ov.queueBuffer(hnd->fd, hnd->offset, ovutils::OV_PIPE1)) {
334 ALOGE("%s: queueBuffer failed for external", __FUNCTION__);
335 ret = false;
336 }
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700337 //Play CC on external
338 if (cchnd && !ov.queueBuffer(cchnd->fd, cchnd->offset,
339 ovutils::OV_PIPE2)) {
340 ALOGE("%s: queueBuffer failed for cc external", __FUNCTION__);
341 ret = false;
342 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700343 // Play primary
344 if (!ov.queueBuffer(hnd->fd, hnd->offset, ovutils::OV_PIPE0)) {
345 ALOGE("%s: queueBuffer failed for primary", __FUNCTION__);
346 ret = false;
347 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700348 // Wait for external vsync to be done
349 if (!ov.waitForVsync(ovutils::OV_PIPE1)) {
350 ALOGE("%s: waitForVsync failed for external", __FUNCTION__);
351 ret = false;
352 }
353 break;
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700354 case ovutils::OV_2D_VIDEO_ON_PANEL:
355 // Play primary
356 if (!ov.queueBuffer(hnd->fd, hnd->offset, ovutils::OV_PIPE0)) {
357 ALOGE("%s: queueBuffer failed for primary", __FUNCTION__);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700358 ret = false;
359 }
360 break;
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700361 case ovutils::OV_2D_VIDEO_ON_TV:
362 // Play external
363 if (!ov.queueBuffer(hnd->fd, hnd->offset, ovutils::OV_PIPE1)) {
364 ALOGE("%s: queueBuffer failed for external", __FUNCTION__);
365 ret = false;
366 }
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700367 //Play CC on external
368 if (cchnd && !ov.queueBuffer(cchnd->fd, cchnd->offset,
369 ovutils::OV_PIPE2)) {
370 ALOGE("%s: queueBuffer failed for cc external", __FUNCTION__);
371 ret = false;
372 }
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700373 // Wait for external vsync to be done
374 if (!ov.waitForVsync(ovutils::OV_PIPE1)) {
375 ALOGE("%s: waitForVsync failed for external", __FUNCTION__);
376 ret = false;
377 }
Naseer Ahmed4c588a22012-07-31 19:12:17 -0700378 break;
Naseer Ahmed2cc53dd2012-07-31 19:11:48 -0700379 default:
380 ALOGE("%s Unused state %s", __FUNCTION__,
381 ovutils::getStateString(state));
382 break;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700383 }
384
385 return ret;
386}
387
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700388}; //namespace qhwc