blob: bbed4f61c5209d103f79d854fa5dfcd2e281de42 [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08003 * Copyright (C) 2012-2013, The Linux Foundation All rights reserved.
4 *
5 * Not a Contribution, Apache license notifications and license are retained
6 * for attribution purposes only.
Naseer Ahmed29a26812012-06-14 00:56:20 -07007 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080020#define HWC_UTILS_DEBUG 0
Saurabh Shahc5b96dc2013-06-05 13:19:52 -070021#include <math.h>
Naseer Ahmed758bfc52012-11-28 17:02:08 -050022#include <sys/ioctl.h>
Naseer Ahmed66e97882013-03-19 20:42:11 -040023#include <linux/fb.h>
Saurabh Shah86c17292013-02-08 15:24:13 -080024#include <binder/IServiceManager.h>
Naseer Ahmed5b6708a2012-08-02 13:46:08 -070025#include <EGL/egl.h>
Saurabh Shahfc2acbe2012-08-17 19:47:52 -070026#include <cutils/properties.h>
27#include <gralloc_priv.h>
Naseer Ahmed758bfc52012-11-28 17:02:08 -050028#include <overlay.h>
Saurabh Shahacf10202013-02-26 10:15:15 -080029#include <overlayRotator.h>
Saurabh Shaha9da08f2013-07-03 13:27:53 -070030#include <overlayWriteback.h>
Naseer Ahmed29a26812012-06-14 00:56:20 -070031#include "hwc_utils.h"
Naseer Ahmed54821fe2012-11-28 18:44:38 -050032#include "hwc_mdpcomp.h"
Saurabh Shahcf053c62012-12-13 12:32:55 -080033#include "hwc_fbupdate.h"
Saurabh Shaha9da08f2013-07-03 13:27:53 -070034#include "hwc_ad.h"
Naseer Ahmeda87da602012-07-01 23:54:19 -070035#include "mdp_version.h"
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080036#include "hwc_copybit.h"
Ramkumar Radhakrishnand224a1a2013-04-05 17:46:55 -070037#include "hwc_dump_layers.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070038#include "external.h"
Jeykumar Sankaran27dee262013-08-01 17:09:54 -070039#include "virtual.h"
Saurabh Shah86c17292013-02-08 15:24:13 -080040#include "hwc_qclient.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070041#include "QService.h"
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080042#include "comptype.h"
Saurabh Shah86c17292013-02-08 15:24:13 -080043
44using namespace qClient;
45using namespace qService;
46using namespace android;
Saurabh Shahacf10202013-02-26 10:15:15 -080047using namespace overlay;
48using namespace overlay::utils;
49namespace ovutils = overlay::utils;
Saurabh Shah86c17292013-02-08 15:24:13 -080050
Naseer Ahmed29a26812012-06-14 00:56:20 -070051namespace qhwc {
Naseer Ahmed72cf9762012-07-21 12:17:13 -070052
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -080053static int openFramebufferDevice(hwc_context_t *ctx)
Naseer Ahmed72cf9762012-07-21 12:17:13 -070054{
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -080055 struct fb_fix_screeninfo finfo;
56 struct fb_var_screeninfo info;
57
58 int fb_fd = openFb(HWC_DISPLAY_PRIMARY);
Sravan Kumar D.V.N78f51e72013-04-16 10:24:55 +053059 if(fb_fd < 0) {
60 ALOGE("%s: Error Opening FB : %s", __FUNCTION__, strerror(errno));
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -080061 return -errno;
Sravan Kumar D.V.N78f51e72013-04-16 10:24:55 +053062 }
63
64 if (ioctl(fb_fd, FBIOGET_VSCREENINFO, &info) == -1) {
65 ALOGE("%s:Error in ioctl FBIOGET_VSCREENINFO: %s", __FUNCTION__,
66 strerror(errno));
67 close(fb_fd);
68 return -errno;
69 }
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -080070
71 if (int(info.width) <= 0 || int(info.height) <= 0) {
72 // the driver doesn't return that information
73 // default to 160 dpi
74 info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
75 info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
Naseer Ahmed72cf9762012-07-21 12:17:13 -070076 }
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -080077
78 float xdpi = (info.xres * 25.4f) / info.width;
79 float ydpi = (info.yres * 25.4f) / info.height;
80
81#ifdef MSMFB_METADATA_GET
82 struct msmfb_metadata metadata;
83 memset(&metadata, 0 , sizeof(metadata));
84 metadata.op = metadata_op_frame_rate;
85
86 if (ioctl(fb_fd, MSMFB_METADATA_GET, &metadata) == -1) {
Sravan Kumar D.V.N78f51e72013-04-16 10:24:55 +053087 ALOGE("%s:Error retrieving panel frame rate: %s", __FUNCTION__,
88 strerror(errno));
89 close(fb_fd);
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -080090 return -errno;
91 }
92
93 float fps = metadata.data.panel_frame_rate;
94#else
95 //XXX: Remove reserved field usage on all baselines
96 //The reserved[3] field is used to store FPS by the driver.
97 float fps = info.reserved[3] & 0xFF;
98#endif
99
Sravan Kumar D.V.N78f51e72013-04-16 10:24:55 +0530100 if (ioctl(fb_fd, FBIOGET_FSCREENINFO, &finfo) == -1) {
101 ALOGE("%s:Error in ioctl FBIOGET_FSCREENINFO: %s", __FUNCTION__,
102 strerror(errno));
103 close(fb_fd);
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800104 return -errno;
Sravan Kumar D.V.N78f51e72013-04-16 10:24:55 +0530105 }
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800106
107 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = fb_fd;
108 //xres, yres may not be 32 aligned
109 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].stride = finfo.line_length /(info.xres/8);
110 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres = info.xres;
111 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].yres = info.yres;
112 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xdpi = xdpi;
113 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].ydpi = ydpi;
114 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period = 1000000000l / fps;
115
Naseer Ahmed22616d92013-05-15 17:20:26 -0400116 //Unblank primary on first boot
117 if(ioctl(fb_fd, FBIOBLANK,FB_BLANK_UNBLANK) < 0) {
118 ALOGE("%s: Failed to unblank display", __FUNCTION__);
119 return -errno;
120 }
121 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].isActive = true;
122
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800123 return 0;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700124}
125
Naseer Ahmed29a26812012-06-14 00:56:20 -0700126void initContext(hwc_context_t *ctx)
127{
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700128 openFramebufferDevice(ctx);
Naseer Ahmed96c4c952012-07-25 18:27:14 -0700129 ctx->mMDP.version = qdutils::MDPVersion::getInstance().getMDPVersion();
130 ctx->mMDP.hasOverlay = qdutils::MDPVersion::getInstance().hasOverlay();
131 ctx->mMDP.panel = qdutils::MDPVersion::getInstance().getPanelType();
Sushil Chauhan07a2c762013-03-06 15:36:49 -0800132 overlay::Overlay::initOverlay();
133 ctx->mOverlay = overlay::Overlay::getInstance();
134 ctx->mRotMgr = new RotMgr();
135
Saurabh Shahcf053c62012-12-13 12:32:55 -0800136 //Is created and destroyed only once for primary
137 //For external it could get created and destroyed multiple times depending
138 //on what external we connect to.
139 ctx->mFBUpdate[HWC_DISPLAY_PRIMARY] =
Saurabh Shah88e4d272013-09-03 13:31:29 -0700140 IFBUpdate::getObject(ctx, HWC_DISPLAY_PRIMARY);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800141
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800142 // Check if the target supports copybit compostion (dyn/mdp/c2d) to
143 // decide if we need to open the copybit module.
144 int compositionType =
145 qdutils::QCCompositionType::getInstance().getCompositionType();
146
147 if (compositionType & (qdutils::COMPOSITION_TYPE_DYN |
148 qdutils::COMPOSITION_TYPE_MDP |
149 qdutils::COMPOSITION_TYPE_C2D)) {
150 ctx->mCopyBit[HWC_DISPLAY_PRIMARY] = new CopyBit();
151 }
152
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700153 ctx->mExtDisplay = new ExternalDisplay(ctx);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700154 ctx->mVirtualDisplay = new VirtualDisplay(ctx);
155 ctx->mVirtualonExtActive = false;
156 ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive = false;
157 ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].connected = false;
158 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].isActive = false;
159 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].connected = false;
Amara Venkata Mastan Manoj Kumar376d8a82013-03-13 19:18:47 -0700160 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].mDownScaleMode= false;
161 ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].mDownScaleMode = false;
162 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].mDownScaleMode = false;
Jeykumar Sankaran85977e32013-02-25 17:06:08 -0800163
164 ctx->mMDPComp[HWC_DISPLAY_PRIMARY] =
Saurabh Shah88e4d272013-09-03 13:31:29 -0700165 MDPComp::getObject(ctx, HWC_DISPLAY_PRIMARY);
Jeykumar Sankaran27dee262013-08-01 17:09:54 -0700166 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].connected = true;
Jeykumar Sankaran85977e32013-02-25 17:06:08 -0800167
Amara Venkata Mastan Manoj Kumar7fb13272013-07-01 13:59:34 -0700168 for (uint32_t i = 0; i < HWC_NUM_DISPLAY_TYPES; i++) {
Ramkumar Radhakrishnand224a1a2013-04-05 17:46:55 -0700169 ctx->mHwcDebug[i] = new HwcDebug(i);
Saurabh Shah23a813c2013-03-20 16:58:12 -0700170 ctx->mLayerRotMap[i] = new LayerRotMap();
Ramkumar Radhakrishnand224a1a2013-04-05 17:46:55 -0700171 }
Saurabh Shah23a813c2013-03-20 16:58:12 -0700172
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500173 MDPComp::init(ctx);
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700174 ctx->mAD = new AssertiveDisplay();
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700175
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400176 ctx->vstate.enable = false;
Naseer Ahmed56601cd2013-03-05 11:34:14 -0500177 ctx->vstate.fakevsync = false;
Arun Kumar K.Rffef7482013-04-10 14:17:22 -0700178 ctx->mExtOrientation = 0;
Saurabh Shah86c17292013-02-08 15:24:13 -0800179
180 //Right now hwc starts the service but anybody could do it, or it could be
181 //independent process as well.
182 QService::init();
183 sp<IQClient> client = new QClient(ctx);
184 interface_cast<IQService>(
185 defaultServiceManager()->getService(
186 String16("display.qservice")))->connect(client);
187
Ramkumar Radhakrishnan59a11072013-04-15 16:14:49 -0700188 // Initialize "No animation on external display" related parameters.
189 ctx->deviceOrientation = 0;
190 ctx->mPrevCropVideo.left = ctx->mPrevCropVideo.top =
191 ctx->mPrevCropVideo.right = ctx->mPrevCropVideo.bottom = 0;
192 ctx->mPrevDestVideo.left = ctx->mPrevDestVideo.top =
193 ctx->mPrevDestVideo.right = ctx->mPrevDestVideo.bottom = 0;
194 ctx->mPrevTransformVideo = 0;
195
Arun Kumar K.Rfb5bfa62013-07-25 03:10:51 -0700196 ctx->mBufferMirrorMode = false;
197
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700198 ALOGI("Initializing Qualcomm Hardware Composer");
Naseer Ahmed96c4c952012-07-25 18:27:14 -0700199 ALOGI("MDP version: %d", ctx->mMDP.version);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700200}
201
202void closeContext(hwc_context_t *ctx)
203{
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500204 if(ctx->mOverlay) {
205 delete ctx->mOverlay;
206 ctx->mOverlay = NULL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700207 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700208
Saurabh Shahacf10202013-02-26 10:15:15 -0800209 if(ctx->mRotMgr) {
210 delete ctx->mRotMgr;
211 ctx->mRotMgr = NULL;
212 }
213
Amara Venkata Mastan Manoj Kumar7fb13272013-07-01 13:59:34 -0700214 for(int i = 0; i < HWC_NUM_DISPLAY_TYPES; i++) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800215 if(ctx->mCopyBit[i]) {
216 delete ctx->mCopyBit[i];
217 ctx->mCopyBit[i] = NULL;
218 }
219 }
220
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800221 if(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700222 close(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd);
223 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = -1;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700224 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700225
226 if(ctx->mExtDisplay) {
227 delete ctx->mExtDisplay;
228 ctx->mExtDisplay = NULL;
229 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400230
Amara Venkata Mastan Manoj Kumar7fb13272013-07-01 13:59:34 -0700231 for(int i = 0; i < HWC_NUM_DISPLAY_TYPES; i++) {
Saurabh Shahcf053c62012-12-13 12:32:55 -0800232 if(ctx->mFBUpdate[i]) {
233 delete ctx->mFBUpdate[i];
234 ctx->mFBUpdate[i] = NULL;
235 }
Jeykumar Sankaran85977e32013-02-25 17:06:08 -0800236 if(ctx->mMDPComp[i]) {
237 delete ctx->mMDPComp[i];
238 ctx->mMDPComp[i] = NULL;
Saurabh Shahacf10202013-02-26 10:15:15 -0800239 }
Ramkumar Radhakrishnand224a1a2013-04-05 17:46:55 -0700240 if(ctx->mHwcDebug[i]) {
241 delete ctx->mHwcDebug[i];
242 ctx->mHwcDebug[i] = NULL;
243 }
Saurabh Shah23a813c2013-03-20 16:58:12 -0700244 if(ctx->mLayerRotMap[i]) {
245 delete ctx->mLayerRotMap[i];
246 ctx->mLayerRotMap[i] = NULL;
247 }
Saurabh Shahcf053c62012-12-13 12:32:55 -0800248 }
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700249 if(ctx->mAD) {
250 delete ctx->mAD;
251 ctx->mAD = NULL;
252 }
Saurabh Shahcf053c62012-12-13 12:32:55 -0800253
Saurabh Shahcbf7ccc2012-12-19 16:45:51 -0800254
Naseer Ahmed29a26812012-06-14 00:56:20 -0700255}
256
Naseer Ahmed1d183f52012-11-26 12:35:16 -0500257
258void dumpsys_log(android::String8& buf, const char* fmt, ...)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700259{
Naseer Ahmed1d183f52012-11-26 12:35:16 -0500260 va_list varargs;
261 va_start(varargs, fmt);
262 buf.appendFormatV(fmt, varargs);
263 va_end(varargs);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700264}
265
Ramkumar Radhakrishnan66f856c2013-08-21 16:09:47 -0700266int getExtOrientation(hwc_context_t* ctx) {
267 int extOrient = ctx->mExtOrientation;
268 if(ctx->mBufferMirrorMode)
269 extOrient = getMirrorModeOrientation(ctx);
270 return extOrient;
271}
272
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800273/* Calculates the destination position based on the action safe rectangle */
Arun Kumar K.R5898c652013-07-17 14:20:32 -0700274void getActionSafePosition(hwc_context_t *ctx, int dpy, hwc_rect_t& rect) {
275 // Position
276 int x = rect.left, y = rect.top;
277 int w = rect.right - rect.left;
278 int h = rect.bottom - rect.top;
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800279
280 // if external supports underscan, do nothing
281 // it will be taken care in the driver
282 if(ctx->mExtDisplay->isCEUnderscanSupported())
283 return;
284
Arun Kumar K.R82f1d282013-05-17 15:37:31 -0700285 char value[PROPERTY_VALUE_MAX];
286 // Read action safe properties
287 property_get("persist.sys.actionsafe.width", value, "0");
288 int asWidthRatio = atoi(value);
289 property_get("persist.sys.actionsafe.height", value, "0");
290 int asHeightRatio = atoi(value);
291
292 if(!asWidthRatio && !asHeightRatio) {
293 //No action safe ratio set, return
294 return;
295 }
296
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800297 float wRatio = 1.0;
298 float hRatio = 1.0;
299 float xRatio = 1.0;
300 float yRatio = 1.0;
301
302 float fbWidth = ctx->dpyAttr[dpy].xres;
303 float fbHeight = ctx->dpyAttr[dpy].yres;
304
Arun Kumar K.R82f1d282013-05-17 15:37:31 -0700305 // Since external is rotated 90, need to swap width/height
Ramkumar Radhakrishnan66f856c2013-08-21 16:09:47 -0700306 int extOrient = getExtOrientation(ctx);
Arun Kumar K.Rfb5bfa62013-07-25 03:10:51 -0700307
308 if(extOrient & HWC_TRANSFORM_ROT_90)
Arun Kumar K.R82f1d282013-05-17 15:37:31 -0700309 swap(fbWidth, fbHeight);
310
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800311 float asX = 0;
312 float asY = 0;
313 float asW = fbWidth;
314 float asH= fbHeight;
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800315
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800316 // based on the action safe ratio, get the Action safe rectangle
317 asW = fbWidth * (1.0f - asWidthRatio / 100.0f);
318 asH = fbHeight * (1.0f - asHeightRatio / 100.0f);
319 asX = (fbWidth - asW) / 2;
320 asY = (fbHeight - asH) / 2;
321
322 // calculate the position ratio
323 xRatio = (float)x/fbWidth;
324 yRatio = (float)y/fbHeight;
325 wRatio = (float)w/fbWidth;
326 hRatio = (float)h/fbHeight;
327
328 //Calculate the position...
329 x = (xRatio * asW) + asX;
330 y = (yRatio * asH) + asY;
331 w = (wRatio * asW);
332 h = (hRatio * asH);
333
Arun Kumar K.R5898c652013-07-17 14:20:32 -0700334 // Convert it back to hwc_rect_t
335 rect.left = x;
336 rect.top = y;
337 rect.right = w + rect.left;
338 rect.bottom = h + rect.top;
339
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800340 return;
341}
342
Arun Kumar K.R5898c652013-07-17 14:20:32 -0700343/* Calculates the aspect ratio for based on src & dest */
344void getAspectRatioPosition(int destWidth, int destHeight, int srcWidth,
345 int srcHeight, hwc_rect_t& rect) {
346 int x =0, y =0;
Arun Kumar K.Rffef7482013-04-10 14:17:22 -0700347
Arun Kumar K.R5898c652013-07-17 14:20:32 -0700348 if (srcWidth * destHeight > destWidth * srcHeight) {
349 srcHeight = destWidth * srcHeight / srcWidth;
350 srcWidth = destWidth;
351 } else if (srcWidth * destHeight < destWidth * srcHeight) {
352 srcWidth = destHeight * srcWidth / srcHeight;
353 srcHeight = destHeight;
354 } else {
355 srcWidth = destWidth;
356 srcHeight = destHeight;
357 }
358 if (srcWidth > destWidth) srcWidth = destWidth;
359 if (srcHeight > destHeight) srcHeight = destHeight;
360 x = (destWidth - srcWidth) / 2;
361 y = (destHeight - srcHeight) / 2;
362 ALOGD_IF(HWC_UTILS_DEBUG, "%s: AS Position: x = %d, y = %d w = %d h = %d",
363 __FUNCTION__, x, y, srcWidth , srcHeight);
364 // Convert it back to hwc_rect_t
365 rect.left = x;
366 rect.top = y;
367 rect.right = srcWidth + rect.left;
368 rect.bottom = srcHeight + rect.top;
Arun Kumar K.Rffef7482013-04-10 14:17:22 -0700369}
370
Arun Kumar K.R5898c652013-07-17 14:20:32 -0700371// This function gets the destination position for Seconday display
372// based on the position and aspect ratio with orientation
373void getAspectRatioPosition(hwc_context_t* ctx, int dpy, int extOrientation,
374 hwc_rect_t& inRect, hwc_rect_t& outRect) {
375 // Physical display resolution
376 float fbWidth = ctx->dpyAttr[dpy].xres;
377 float fbHeight = ctx->dpyAttr[dpy].yres;
378 //display position(x,y,w,h) in correct aspectratio after rotation
379 int xPos = 0;
380 int yPos = 0;
381 float width = fbWidth;
382 float height = fbHeight;
383 // Width/Height used for calculation, after rotation
384 float actualWidth = fbWidth;
385 float actualHeight = fbHeight;
386
387 float wRatio = 1.0;
388 float hRatio = 1.0;
389 float xRatio = 1.0;
390 float yRatio = 1.0;
391 hwc_rect_t rect = {0, 0, (int)fbWidth, (int)fbHeight};
392
393 Dim inPos(inRect.left, inRect.top, inRect.right - inRect.left,
394 inRect.bottom - inRect.top);
395 Dim outPos(outRect.left, outRect.top, outRect.right - outRect.left,
396 outRect.bottom - outRect.top);
397
398 Whf whf(fbWidth, fbHeight, 0);
399 eTransform extorient = static_cast<eTransform>(extOrientation);
400 // To calculate the destination co-ordinates in the new orientation
401 preRotateSource(extorient, whf, inPos);
402
403 if(extOrientation & HAL_TRANSFORM_ROT_90) {
404 // Swap width/height for input position
405 swapWidthHeight(actualWidth, actualHeight);
406 getAspectRatioPosition(fbWidth, fbHeight, (int)actualWidth,
407 (int)actualHeight, rect);
408 xPos = rect.left;
409 yPos = rect.top;
410 width = rect.right - rect.left;
411 height = rect.bottom - rect.top;
412 }
413
414 //Calculate the position...
415 xRatio = inPos.x/actualWidth;
416 yRatio = inPos.y/actualHeight;
417 wRatio = inPos.w/actualWidth;
418 hRatio = inPos.h/actualHeight;
419
420 outPos.x = (xRatio * width) + xPos;
421 outPos.y = (yRatio * height) + yPos;
422 outPos.w = wRatio * width;
423 outPos.h = hRatio * height;
424 ALOGD_IF(HWC_UTILS_DEBUG, "%s: Calculated AspectRatio Position: x = %d,"
425 "y = %d w = %d h = %d", __FUNCTION__, outPos.x, outPos.y,
426 outPos.w, outPos.h);
427
Amara Venkata Mastan Manoj Kumar376d8a82013-03-13 19:18:47 -0700428 // For sidesync, the dest fb will be in portrait orientation, and the crop
429 // will be updated to avoid the black side bands, and it will be upscaled
430 // to fit the dest RB, so recalculate
431 // the position based on the new width and height
432 if ((extOrientation & HWC_TRANSFORM_ROT_90) &&
433 isOrientationPortrait(ctx)) {
434 hwc_rect_t r;
435 //Calculate the position
436 xRatio = (outPos.x - xPos)/width;
437 // GetaspectRatio -- tricky to get the correct aspect ratio
438 // But we need to do this.
439 getAspectRatioPosition(width, height, width, height, r);
440 xPos = r.left;
441 yPos = r.top;
442 float tempWidth = r.right - r.left;
443 float tempHeight = r.bottom - r.top;
444 yRatio = yPos/height;
445 wRatio = outPos.w/width;
446 hRatio = tempHeight/height;
447
448 //Map the coordinates back to Framebuffer domain
449 outPos.x = (xRatio * fbWidth);
450 outPos.y = (yRatio * fbHeight);
451 outPos.w = wRatio * fbWidth;
452 outPos.h = hRatio * fbHeight;
453
454 ALOGD_IF(HWC_UTILS_DEBUG, "%s: Calculated AspectRatio for device in"
455 "portrait: x = %d,y = %d w = %d h = %d", __FUNCTION__,
456 outPos.x, outPos.y,
457 outPos.w, outPos.h);
458 }
459 if(ctx->dpyAttr[dpy].mDownScaleMode) {
460 int extW, extH;
461 if(dpy == HWC_DISPLAY_EXTERNAL)
462 ctx->mExtDisplay->getAttributes(extW, extH);
463 else
464 ctx->mVirtualDisplay->getAttributes(extW, extH);
465 fbWidth = ctx->dpyAttr[dpy].xres;
466 fbHeight = ctx->dpyAttr[dpy].yres;
467 //Calculate the position...
468 xRatio = outPos.x/fbWidth;
469 yRatio = outPos.y/fbHeight;
470 wRatio = outPos.w/fbWidth;
471 hRatio = outPos.h/fbHeight;
472
473 outPos.x = xRatio * extW;
474 outPos.y = yRatio * extH;
475 outPos.w = wRatio * extW;
476 outPos.h = hRatio * extH;
477 }
Arun Kumar K.R5898c652013-07-17 14:20:32 -0700478 // Convert Dim to hwc_rect_t
479 outRect.left = outPos.x;
480 outRect.top = outPos.y;
481 outRect.right = outPos.x + outPos.w;
482 outRect.bottom = outPos.y + outPos.h;
483
484 return;
485}
486
487bool isPrimaryPortrait(hwc_context_t *ctx) {
488 int fbWidth = ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres;
489 int fbHeight = ctx->dpyAttr[HWC_DISPLAY_PRIMARY].yres;
490 if(fbWidth < fbHeight) {
491 return true;
492 }
493 return false;
494}
495
Amara Venkata Mastan Manoj Kumar376d8a82013-03-13 19:18:47 -0700496bool isOrientationPortrait(hwc_context_t *ctx) {
497 if(isPrimaryPortrait(ctx)) {
498 return !(ctx->deviceOrientation & 0x1);
499 }
500 return (ctx->deviceOrientation & 0x1);
501}
502
Ramkumar Radhakrishnan66f856c2013-08-21 16:09:47 -0700503void calcExtDisplayPosition(hwc_context_t *ctx,
504 private_handle_t *hnd,
505 int dpy,
Amara Venkata Mastan Manoj Kumar376d8a82013-03-13 19:18:47 -0700506 hwc_rect_t& sourceCrop,
Ramkumar Radhakrishnan66f856c2013-08-21 16:09:47 -0700507 hwc_rect_t& displayFrame,
508 int& transform,
509 ovutils::eTransform& orient) {
Arun Kumar K.R5898c652013-07-17 14:20:32 -0700510 // Swap width and height when there is a 90deg transform
Ramkumar Radhakrishnan66f856c2013-08-21 16:09:47 -0700511 int extOrient = getExtOrientation(ctx);
512 if(dpy && !qdutils::MDPVersion::getInstance().is8x26()) {
513 if(!isYuvBuffer(hnd)) {
514 if(extOrient & HWC_TRANSFORM_ROT_90) {
515 int dstWidth = ctx->dpyAttr[dpy].xres;
516 int dstHeight = ctx->dpyAttr[dpy].yres;;
517 int srcWidth = ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres;
518 int srcHeight = ctx->dpyAttr[HWC_DISPLAY_PRIMARY].yres;
519 if(!isPrimaryPortrait(ctx)) {
520 swap(srcWidth, srcHeight);
521 } // Get Aspect Ratio for external
522 getAspectRatioPosition(dstWidth, dstHeight, srcWidth,
523 srcHeight, displayFrame);
524 // Crop - this is needed, because for sidesync, the dest fb will
525 // be in portrait orientation, so update the crop to not show the
526 // black side bands.
527 if (isOrientationPortrait(ctx)) {
528 sourceCrop = displayFrame;
529 displayFrame.left = 0;
530 displayFrame.top = 0;
531 displayFrame.right = dstWidth;
532 displayFrame.bottom = dstHeight;
533 }
534 }
535 if(ctx->dpyAttr[dpy].mDownScaleMode) {
536 int extW, extH;
537 // if downscale is enabled, map the co-ordinates to new
538 // domain(downscaled)
539 float fbWidth = ctx->dpyAttr[dpy].xres;
540 float fbHeight = ctx->dpyAttr[dpy].yres;
541 // query MDP configured attributes
542 if(dpy == HWC_DISPLAY_EXTERNAL)
543 ctx->mExtDisplay->getAttributes(extW, extH);
544 else
545 ctx->mVirtualDisplay->getAttributes(extW, extH);
546 //Calculate the ratio...
547 float wRatio = ((float)extW)/fbWidth;
548 float hRatio = ((float)extH)/fbHeight;
Amara Venkata Mastan Manoj Kumar376d8a82013-03-13 19:18:47 -0700549
Ramkumar Radhakrishnan66f856c2013-08-21 16:09:47 -0700550 //convert Dim to hwc_rect_t
551 displayFrame.left *= wRatio;
552 displayFrame.top *= hRatio;
553 displayFrame.right *= wRatio;
554 displayFrame.bottom *= hRatio;
555 }
556 }else {
557 if(extOrient || ctx->dpyAttr[dpy].mDownScaleMode) {
558 getAspectRatioPosition(ctx, dpy, extOrient,
559 displayFrame, displayFrame);
560 }
561 }
562 // If there is a external orientation set, use that
563 if(extOrient) {
564 transform = extOrient;
565 orient = static_cast<ovutils::eTransform >(extOrient);
566 }
567 // Calculate the actionsafe dimensions for External(dpy = 1 or 2)
568 getActionSafePosition(ctx, dpy, displayFrame);
Amara Venkata Mastan Manoj Kumar376d8a82013-03-13 19:18:47 -0700569 }
Arun Kumar K.Rfb5bfa62013-07-25 03:10:51 -0700570}
Amara Venkata Mastan Manoj Kumar376d8a82013-03-13 19:18:47 -0700571
Arun Kumar K.Rfb5bfa62013-07-25 03:10:51 -0700572/* Returns the orientation which needs to be set on External for
573 * SideSync/Buffer Mirrormode
574 */
575int getMirrorModeOrientation(hwc_context_t *ctx) {
576 int extOrientation = 0;
577 int deviceOrientation = ctx->deviceOrientation;
578 if(!isPrimaryPortrait(ctx))
579 deviceOrientation = (deviceOrientation + 1) % 4;
580 if (deviceOrientation == 0)
581 extOrientation = HWC_TRANSFORM_ROT_270;
582 else if (deviceOrientation == 1)//90
583 extOrientation = 0;
584 else if (deviceOrientation == 2)//180
585 extOrientation = HWC_TRANSFORM_ROT_90;
586 else if (deviceOrientation == 3)//270
587 extOrientation = HWC_TRANSFORM_FLIP_V | HWC_TRANSFORM_FLIP_H;
588
589 return extOrientation;
Arun Kumar K.R5898c652013-07-17 14:20:32 -0700590}
Arun Kumar K.Rffef7482013-04-10 14:17:22 -0700591
Saurabh Shah8a117932013-05-23 12:48:13 -0700592bool needsScaling(hwc_context_t* ctx, hwc_layer_1_t const* layer,
593 const int& dpy) {
Naseer Ahmed018e5452012-12-03 14:46:15 -0500594 int dst_w, dst_h, src_w, src_h;
595
596 hwc_rect_t displayFrame = layer->displayFrame;
597 hwc_rect_t sourceCrop = layer->sourceCrop;
Saurabh Shah8a117932013-05-23 12:48:13 -0700598 trimLayer(ctx, dpy, layer->transform, sourceCrop, displayFrame);
Naseer Ahmed018e5452012-12-03 14:46:15 -0500599
600 dst_w = displayFrame.right - displayFrame.left;
601 dst_h = displayFrame.bottom - displayFrame.top;
Naseer Ahmed018e5452012-12-03 14:46:15 -0500602 src_w = sourceCrop.right - sourceCrop.left;
603 src_h = sourceCrop.bottom - sourceCrop.top;
604
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800605 if(((src_w != dst_w) || (src_h != dst_h)))
606 return true;
607
608 return false;
609}
610
Saurabh Shah8a117932013-05-23 12:48:13 -0700611bool isAlphaScaled(hwc_context_t* ctx, hwc_layer_1_t const* layer,
612 const int& dpy) {
613 if(needsScaling(ctx, layer, dpy) && isAlphaPresent(layer)) {
Sravan Kumar D.V.N075ef002013-03-20 05:22:26 +0530614 return true;
615 }
616 return false;
617}
618
619bool isAlphaPresent(hwc_layer_1_t const* layer) {
620 private_handle_t *hnd = (private_handle_t *)layer->handle;
Naseer Ahmed7a4287c2013-03-26 18:11:41 -0400621 if(hnd) {
622 int format = hnd->format;
623 switch(format) {
Sravan Kumar D.V.N075ef002013-03-20 05:22:26 +0530624 case HAL_PIXEL_FORMAT_RGBA_8888:
625 case HAL_PIXEL_FORMAT_BGRA_8888:
626 // In any more formats with Alpha go here..
Naseer Ahmed018e5452012-12-03 14:46:15 -0500627 return true;
Sravan Kumar D.V.N075ef002013-03-20 05:22:26 +0530628 default : return false;
Naseer Ahmed7a4287c2013-03-26 18:11:41 -0400629 }
Naseer Ahmed018e5452012-12-03 14:46:15 -0500630 }
631 return false;
632}
633
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700634void setListStats(hwc_context_t *ctx,
635 const hwc_display_contents_1_t *list, int dpy) {
Saurabh Shahd4e65852013-06-17 11:33:53 -0700636 const int prevYuvCount = ctx->listStats[dpy].yuvCount;
637 memset(&ctx->listStats[dpy], 0, sizeof(ListStats));
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700638 ctx->listStats[dpy].numAppLayers = list->numHwLayers - 1;
639 ctx->listStats[dpy].fbLayerIndex = list->numHwLayers - 1;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700640 ctx->listStats[dpy].skipCount = 0;
Naseer Ahmed018e5452012-12-03 14:46:15 -0500641 ctx->listStats[dpy].needsAlphaScale = false;
Sravan Kumar D.V.Nb5ed0292013-03-15 08:51:16 +0530642 ctx->listStats[dpy].preMultipliedAlpha = false;
Jeykumar Sankarancf537002013-01-21 21:19:15 -0800643 ctx->listStats[dpy].yuvCount = 0;
Ping Li9404e2a2013-04-24 16:36:03 -0400644 char property[PROPERTY_VALUE_MAX];
Arun Kumar K.Ra2978452013-02-07 01:34:24 -0800645 ctx->listStats[dpy].extOnlyLayerIndex = -1;
Ramkumar Radhakrishnan59a11072013-04-15 16:14:49 -0700646 ctx->listStats[dpy].isDisplayAnimating = false;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700647
Ramkumar Radhakrishnan26bdee92013-06-20 16:56:56 -0700648 for (size_t i = 0; i < (size_t)ctx->listStats[dpy].numAppLayers; i++) {
Naseer Ahmed018e5452012-12-03 14:46:15 -0500649 hwc_layer_1_t const* layer = &list->hwLayers[i];
650 private_handle_t *hnd = (private_handle_t *)layer->handle;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700651
Ramkumar Radhakrishnane661f962013-06-05 17:21:38 -0700652#ifdef QCOM_BSP
653 if (layer->flags & HWC_SCREENSHOT_ANIMATOR_LAYER) {
654 ctx->listStats[dpy].isDisplayAnimating = true;
655 }
656#endif
Ramkumar Radhakrishnan26bdee92013-06-20 16:56:56 -0700657 // continue if number of app layers exceeds MAX_NUM_APP_LAYERS
658 if(ctx->listStats[dpy].numAppLayers > MAX_NUM_APP_LAYERS)
Ramkumar Radhakrishnane661f962013-06-05 17:21:38 -0700659 continue;
Jeykumar Sankarancf537002013-01-21 21:19:15 -0800660
Ramkumar Radhakrishnan26bdee92013-06-20 16:56:56 -0700661 //reset yuv indices
662 ctx->listStats[dpy].yuvIndices[i] = -1;
663
664 if (isSkipLayer(&list->hwLayers[i])) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700665 ctx->listStats[dpy].skipCount++;
Ramkumar Radhakrishnan59a11072013-04-15 16:14:49 -0700666 }
667
668 if (UNLIKELY(isYuvBuffer(hnd))) {
Jeykumar Sankarancf537002013-01-21 21:19:15 -0800669 int& yuvCount = ctx->listStats[dpy].yuvCount;
670 ctx->listStats[dpy].yuvIndices[yuvCount] = i;
671 yuvCount++;
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800672
Saurabh Shahe2474082013-05-15 16:32:13 -0700673 if((layer->transform & HWC_TRANSFORM_ROT_90) &&
Amara Venkata Mastan Manoj Kumar9d373c02013-08-20 14:30:09 -0700674 canUseRotator(ctx, dpy)) {
675 if( (dpy == HWC_DISPLAY_PRIMARY) &&
676 ctx->mOverlay->isPipeTypeAttached(OV_MDP_PIPE_DMA)) {
Saurabh Shah0ceeb6a2013-04-23 10:46:07 -0700677 ctx->isPaddingRound = true;
678 }
Saurabh Shah85234ec2013-04-12 17:09:00 -0700679 Overlay::setDMAMode(Overlay::DMA_BLOCK_MODE);
680 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700681 }
Sravan Kumar D.V.Nb5ed0292013-03-15 08:51:16 +0530682 if(layer->blending == HWC_BLENDING_PREMULT)
683 ctx->listStats[dpy].preMultipliedAlpha = true;
Jeykumar Sankaran7535aba2013-02-04 11:03:09 -0800684
685 if(!ctx->listStats[dpy].needsAlphaScale)
Saurabh Shah8a117932013-05-23 12:48:13 -0700686 ctx->listStats[dpy].needsAlphaScale =
687 isAlphaScaled(ctx, layer, dpy);
Arun Kumar K.Ra2978452013-02-07 01:34:24 -0800688
689 if(UNLIKELY(isExtOnly(hnd))){
690 ctx->listStats[dpy].extOnlyLayerIndex = i;
691 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700692 }
Ping Li9404e2a2013-04-24 16:36:03 -0400693 if(ctx->listStats[dpy].yuvCount > 0) {
694 if (property_get("hw.cabl.yuv", property, NULL) > 0) {
695 if (atoi(property) != 1) {
696 property_set("hw.cabl.yuv", "1");
697 }
698 }
699 } else {
700 if (property_get("hw.cabl.yuv", property, NULL) > 0) {
701 if (atoi(property) != 0) {
702 property_set("hw.cabl.yuv", "0");
703 }
704 }
705 }
Arun Kumar K.Rffef7482013-04-10 14:17:22 -0700706 if(dpy) {
707 //uncomment the below code for testing purpose.
708 /* char value[PROPERTY_VALUE_MAX];
709 property_get("sys.ext_orientation", value, "0");
710 // Assuming the orientation value is in terms of HAL_TRANSFORM,
711 // This needs mapping to HAL, if its in different convention
712 ctx->mExtOrientation = atoi(value); */
713 // Assuming the orientation value is in terms of HAL_TRANSFORM,
714 // This needs mapping to HAL, if its in different convention
Arun Kumar K.Rfb5bfa62013-07-25 03:10:51 -0700715 if(ctx->mExtOrientation || ctx->mBufferMirrorMode) {
716 ALOGD_IF(HWC_UTILS_DEBUG, "%s: ext orientation = %d"
717 "BufferMirrorMode = %d", __FUNCTION__,
718 ctx->mExtOrientation, ctx->mBufferMirrorMode);
Arun Kumar K.Rffef7482013-04-10 14:17:22 -0700719 if(ctx->mOverlay->isPipeTypeAttached(OV_MDP_PIPE_DMA)) {
720 ctx->isPaddingRound = true;
721 }
722 Overlay::setDMAMode(Overlay::DMA_BLOCK_MODE);
723 }
724 }
Saurabh Shahd4e65852013-06-17 11:33:53 -0700725
726 //The marking of video begin/end is useful on some targets where we need
727 //to have a padding round to be able to shift pipes across mixers.
728 if(prevYuvCount != ctx->listStats[dpy].yuvCount) {
729 ctx->mVideoTransFlag = true;
730 }
Saurabh Shaha9da08f2013-07-03 13:27:53 -0700731 if(dpy == HWC_DISPLAY_PRIMARY) {
732 ctx->mAD->markDoable(ctx, list);
733 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700734}
735
Naseer Ahmed018e5452012-12-03 14:46:15 -0500736
Saurabh Shah68107422013-07-09 11:12:42 -0700737static void calc_cut(double& leftCutRatio, double& topCutRatio,
738 double& rightCutRatio, double& bottomCutRatio, int orient) {
Saurabh Shah27c1d652012-08-14 19:30:28 -0700739 if(orient & HAL_TRANSFORM_FLIP_H) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700740 swap(leftCutRatio, rightCutRatio);
Saurabh Shah27c1d652012-08-14 19:30:28 -0700741 }
742 if(orient & HAL_TRANSFORM_FLIP_V) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700743 swap(topCutRatio, bottomCutRatio);
Saurabh Shah27c1d652012-08-14 19:30:28 -0700744 }
745 if(orient & HAL_TRANSFORM_ROT_90) {
746 //Anti clock swapping
Saurabh Shah68107422013-07-09 11:12:42 -0700747 double tmpCutRatio = leftCutRatio;
Saurabh Shah541b59d2012-10-11 11:08:12 -0700748 leftCutRatio = topCutRatio;
749 topCutRatio = rightCutRatio;
750 rightCutRatio = bottomCutRatio;
751 bottomCutRatio = tmpCutRatio;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700752 }
753}
754
Jeykumar Sankaran85977e32013-02-25 17:06:08 -0800755bool isSecuring(hwc_context_t* ctx, hwc_layer_1_t const* layer) {
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500756 if((ctx->mMDP.version < qdutils::MDSS_V5) &&
757 (ctx->mMDP.version > qdutils::MDP_V3_0) &&
758 ctx->mSecuring) {
759 return true;
760 }
Jeykumar Sankaran85977e32013-02-25 17:06:08 -0800761 if (isSecureModePolicy(ctx->mMDP.version)) {
762 private_handle_t *hnd = (private_handle_t *)layer->handle;
763 if(ctx->mSecureMode) {
764 if (! isSecureBuffer(hnd)) {
765 ALOGD_IF(HWC_UTILS_DEBUG,"%s:Securing Turning ON ...",
766 __FUNCTION__);
767 return true;
768 }
769 } else {
770 if (isSecureBuffer(hnd)) {
771 ALOGD_IF(HWC_UTILS_DEBUG,"%s:Securing Turning OFF ...",
772 __FUNCTION__);
773 return true;
774 }
775 }
776 }
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500777 return false;
778}
779
Sushil Chauhan2515abf2013-01-08 16:40:05 -0800780bool isSecureModePolicy(int mdpVersion) {
781 if (mdpVersion < qdutils::MDSS_V5)
782 return true;
783 else
784 return false;
785}
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500786
Naseer Ahmed522ce662013-03-18 20:14:05 -0400787int getBlending(int blending) {
788 switch(blending) {
789 case HWC_BLENDING_NONE:
790 return overlay::utils::OVERLAY_BLENDING_OPAQUE;
791 case HWC_BLENDING_PREMULT:
792 return overlay::utils::OVERLAY_BLENDING_PREMULT;
793 case HWC_BLENDING_COVERAGE :
794 default:
795 return overlay::utils::OVERLAY_BLENDING_COVERAGE;
796 }
797}
798
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700799//Crops source buffer against destination and FB boundaries
800void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800801 const hwc_rect_t& scissor, int orient) {
802
Saurabh Shah27c1d652012-08-14 19:30:28 -0700803 int& crop_l = crop.left;
804 int& crop_t = crop.top;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700805 int& crop_r = crop.right;
806 int& crop_b = crop.bottom;
807 int crop_w = crop.right - crop.left;
808 int crop_h = crop.bottom - crop.top;
809
Saurabh Shah27c1d652012-08-14 19:30:28 -0700810 int& dst_l = dst.left;
811 int& dst_t = dst.top;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700812 int& dst_r = dst.right;
813 int& dst_b = dst.bottom;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700814 int dst_w = abs(dst.right - dst.left);
815 int dst_h = abs(dst.bottom - dst.top);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700816
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800817 const int& sci_l = scissor.left;
818 const int& sci_t = scissor.top;
819 const int& sci_r = scissor.right;
820 const int& sci_b = scissor.bottom;
821 int sci_w = abs(sci_r - sci_l);
822 int sci_h = abs(sci_b - sci_t);
823
Saurabh Shah68107422013-07-09 11:12:42 -0700824 double leftCutRatio = 0.0, rightCutRatio = 0.0, topCutRatio = 0.0,
825 bottomCutRatio = 0.0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700826
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800827 if(dst_l < sci_l) {
Saurabh Shah68107422013-07-09 11:12:42 -0700828 leftCutRatio = (double)(sci_l - dst_l) / (double)dst_w;
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800829 dst_l = sci_l;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700830 }
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800831
832 if(dst_r > sci_r) {
Saurabh Shah68107422013-07-09 11:12:42 -0700833 rightCutRatio = (double)(dst_r - sci_r) / (double)dst_w;
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800834 dst_r = sci_r;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700835 }
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800836
837 if(dst_t < sci_t) {
Saurabh Shah68107422013-07-09 11:12:42 -0700838 topCutRatio = (double)(sci_t - dst_t) / (double)dst_h;
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800839 dst_t = sci_t;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700840 }
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800841
842 if(dst_b > sci_b) {
Saurabh Shah68107422013-07-09 11:12:42 -0700843 bottomCutRatio = (double)(dst_b - sci_b) / (double)dst_h;
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800844 dst_b = sci_b;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700845 }
Saurabh Shah27c1d652012-08-14 19:30:28 -0700846
Saurabh Shah541b59d2012-10-11 11:08:12 -0700847 calc_cut(leftCutRatio, topCutRatio, rightCutRatio, bottomCutRatio, orient);
848 crop_l += crop_w * leftCutRatio;
849 crop_t += crop_h * topCutRatio;
850 crop_r -= crop_w * rightCutRatio;
851 crop_b -= crop_h * bottomCutRatio;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700852}
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700853
Naseer Ahmed64b81212013-02-14 10:29:47 -0500854void getNonWormholeRegion(hwc_display_contents_1_t* list,
855 hwc_rect_t& nwr)
856{
857 uint32_t last = list->numHwLayers - 1;
858 hwc_rect_t fbDisplayFrame = list->hwLayers[last].displayFrame;
859 //Initiliaze nwr to first frame
860 nwr.left = list->hwLayers[0].displayFrame.left;
861 nwr.top = list->hwLayers[0].displayFrame.top;
862 nwr.right = list->hwLayers[0].displayFrame.right;
863 nwr.bottom = list->hwLayers[0].displayFrame.bottom;
864
865 for (uint32_t i = 1; i < last; i++) {
866 hwc_rect_t displayFrame = list->hwLayers[i].displayFrame;
867 nwr.left = min(nwr.left, displayFrame.left);
868 nwr.top = min(nwr.top, displayFrame.top);
869 nwr.right = max(nwr.right, displayFrame.right);
870 nwr.bottom = max(nwr.bottom, displayFrame.bottom);
871 }
872
873 //Intersect with the framebuffer
874 nwr.left = max(nwr.left, fbDisplayFrame.left);
875 nwr.top = max(nwr.top, fbDisplayFrame.top);
876 nwr.right = min(nwr.right, fbDisplayFrame.right);
877 nwr.bottom = min(nwr.bottom, fbDisplayFrame.bottom);
878
879}
880
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700881bool isExternalActive(hwc_context_t* ctx) {
882 return ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive;
Saurabh Shahfc2acbe2012-08-17 19:47:52 -0700883}
884
Saurabh Shah747af1e2013-02-26 10:25:12 -0800885void closeAcquireFds(hwc_display_contents_1_t* list) {
886 for(uint32_t i = 0; list && i < list->numHwLayers; i++) {
887 //Close the acquireFenceFds
888 //HWC_FRAMEBUFFER are -1 already by SF, rest we close.
889 if(list->hwLayers[i].acquireFenceFd >= 0) {
890 close(list->hwLayers[i].acquireFenceFd);
891 list->hwLayers[i].acquireFenceFd = -1;
892 }
893 }
894}
895
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800896int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy,
Saurabh Shah23a813c2013-03-20 16:58:12 -0700897 int fd) {
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700898 int ret = 0;
Ramkumar Radhakrishnane661f962013-06-05 17:21:38 -0700899 int acquireFd[MAX_NUM_APP_LAYERS];
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700900 int count = 0;
901 int releaseFd = -1;
902 int fbFd = -1;
Saurabh Shah23a813c2013-03-20 16:58:12 -0700903 int rotFd = -1;
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500904 bool swapzero = false;
Saurabh Shah23a813c2013-03-20 16:58:12 -0700905 int mdpVersion = qdutils::MDPVersion::getInstance().getMDPVersion();
906
907 struct mdp_buf_sync data;
908 memset(&data, 0, sizeof(data));
909 //Until B-family supports sync for rotator
910#ifdef MDSS_TARGET
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700911 data.flags = MDP_BUF_SYNC_FLAG_WAIT;
Saurabh Shah23a813c2013-03-20 16:58:12 -0700912#endif
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700913 data.acq_fen_fd = acquireFd;
914 data.rel_fen_fd = &releaseFd;
Saurabh Shah23a813c2013-03-20 16:58:12 -0700915
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500916 char property[PROPERTY_VALUE_MAX];
917 if(property_get("debug.egl.swapinterval", property, "1") > 0) {
918 if(atoi(property) == 0)
919 swapzero = true;
920 }
Ramkumar Radhakrishnan59a11072013-04-15 16:14:49 -0700921 bool isExtAnimating = false;
922 if(dpy)
923 isExtAnimating = ctx->listStats[dpy].isDisplayAnimating;
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500924
Saurabh Shah23a813c2013-03-20 16:58:12 -0700925 //Send acquireFenceFds to rotator
926#ifdef MDSS_TARGET
927 //TODO B-family
928#else
929 //A-family
930 int rotFd = ctx->mRotMgr->getRotDevFd();
931 struct msm_rotator_buf_sync rotData;
932
933 for(uint32_t i = 0; i < ctx->mLayerRotMap[dpy]->getCount(); i++) {
934 memset(&rotData, 0, sizeof(rotData));
935 int& acquireFenceFd =
936 ctx->mLayerRotMap[dpy]->getLayer(i)->acquireFenceFd;
937 rotData.acq_fen_fd = acquireFenceFd;
938 rotData.session_id = ctx->mLayerRotMap[dpy]->getRot(i)->getSessId();
939 ioctl(rotFd, MSM_ROTATOR_IOCTL_BUFFER_SYNC, &rotData);
940 close(acquireFenceFd);
941 //For MDP to wait on.
942 acquireFenceFd = dup(rotData.rel_fen_fd);
943 //A buffer is free to be used by producer as soon as its copied to
944 //rotator.
945 ctx->mLayerRotMap[dpy]->getLayer(i)->releaseFenceFd =
946 rotData.rel_fen_fd;
947 }
948#endif
949
950 //Accumulate acquireFenceFds for MDP
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700951 for(uint32_t i = 0; i < list->numHwLayers; i++) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800952 if(list->hwLayers[i].compositionType == HWC_OVERLAY &&
Saurabh Shah23a813c2013-03-20 16:58:12 -0700953 list->hwLayers[i].acquireFenceFd >= 0) {
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500954 if(UNLIKELY(swapzero))
955 acquireFd[count++] = -1;
956 else
957 acquireFd[count++] = list->hwLayers[i].acquireFenceFd;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700958 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800959 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
960 if(UNLIKELY(swapzero))
961 acquireFd[count++] = -1;
Saurabh Shah23a813c2013-03-20 16:58:12 -0700962 else if(fd >= 0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800963 //set the acquireFD from fd - which is coming from c2d
964 acquireFd[count++] = fd;
965 // Buffer sync IOCTL should be async when using c2d fence is
966 // used
967 data.flags &= ~MDP_BUF_SYNC_FLAG_WAIT;
Saurabh Shah23a813c2013-03-20 16:58:12 -0700968 } else if(list->hwLayers[i].acquireFenceFd >= 0)
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800969 acquireFd[count++] = list->hwLayers[i].acquireFenceFd;
970 }
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700971 }
972
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700973 data.acq_fen_fd_cnt = count;
974 fbFd = ctx->dpyAttr[dpy].fd;
Saurabh Shah23a813c2013-03-20 16:58:12 -0700975
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700976 //Waits for acquire fences, returns a release fence
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800977 if(LIKELY(!swapzero)) {
978 uint64_t start = systemTime();
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500979 ret = ioctl(fbFd, MSMFB_BUFFER_SYNC, &data);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800980 ALOGD_IF(HWC_UTILS_DEBUG, "%s: time taken for MSMFB_BUFFER_SYNC IOCTL = %d",
981 __FUNCTION__, (size_t) ns2ms(systemTime() - start));
982 }
Saurabh Shah747af1e2013-02-26 10:25:12 -0800983
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700984 if(ret < 0) {
Ramkumar Radhakrishnanb32a0bc2013-04-11 17:57:32 -0700985 ALOGE("%s: ioctl MSMFB_BUFFER_SYNC failed, err=%s",
986 __FUNCTION__, strerror(errno));
987 ALOGE("%s: acq_fen_fd_cnt=%d flags=%d fd=%d dpy=%d numHwLayers=%d",
988 __FUNCTION__, data.acq_fen_fd_cnt, data.flags, fbFd,
989 dpy, list->numHwLayers);
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700990 }
Saurabh Shah747af1e2013-02-26 10:25:12 -0800991
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700992 for(uint32_t i = 0; i < list->numHwLayers; i++) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800993 if(list->hwLayers[i].compositionType == HWC_OVERLAY ||
994 list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700995 //Populate releaseFenceFds.
Saurabh Shah23a813c2013-03-20 16:58:12 -0700996 if(UNLIKELY(swapzero)) {
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500997 list->hwLayers[i].releaseFenceFd = -1;
Saurabh Shah23a813c2013-03-20 16:58:12 -0700998 } else if(isExtAnimating) {
Ramkumar Radhakrishnan59a11072013-04-15 16:14:49 -0700999 // Release all the app layer fds immediately,
1000 // if animation is in progress.
1001 hwc_layer_1_t const* layer = &list->hwLayers[i];
1002 private_handle_t *hnd = (private_handle_t *)layer->handle;
1003 if(isYuvBuffer(hnd)) {
1004 list->hwLayers[i].releaseFenceFd = dup(releaseFd);
1005 } else
1006 list->hwLayers[i].releaseFenceFd = -1;
Saurabh Shah23a813c2013-03-20 16:58:12 -07001007 } else if(list->hwLayers[i].releaseFenceFd < 0) {
1008 //If rotator has not already populated this field.
Naseer Ahmed94baddc2012-12-17 18:51:01 -05001009 list->hwLayers[i].releaseFenceFd = dup(releaseFd);
Saurabh Shah23a813c2013-03-20 16:58:12 -07001010 }
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -07001011 }
1012 }
Saurabh Shah747af1e2013-02-26 10:25:12 -08001013
1014 if(fd >= 0) {
1015 close(fd);
1016 fd = -1;
1017 }
1018
Naseer Ahmed64b81212013-02-14 10:29:47 -05001019 if (ctx->mCopyBit[dpy])
1020 ctx->mCopyBit[dpy]->setReleaseFd(releaseFd);
Saurabh Shah23a813c2013-03-20 16:58:12 -07001021
1022#ifdef MDSS_TARGET
1023 //TODO When B is implemented remove #ifdefs from here
1024 //The API called applies to RotMem buffers
1025#else
1026 //A-family
1027 //Signals when MDP finishes reading rotator buffers.
1028 ctx->mLayerRotMap[dpy]->setReleaseFd(releaseFd);
1029#endif
1030
Ramkumar Radhakrishnan59a11072013-04-15 16:14:49 -07001031 // if external is animating, close the relaseFd
1032 if(isExtAnimating) {
1033 close(releaseFd);
1034 releaseFd = -1;
1035 }
Saurabh Shah23a813c2013-03-20 16:58:12 -07001036
Naseer Ahmed94baddc2012-12-17 18:51:01 -05001037 if(UNLIKELY(swapzero)){
1038 list->retireFenceFd = -1;
1039 close(releaseFd);
1040 } else {
1041 list->retireFenceFd = releaseFd;
1042 }
Saurabh Shah747af1e2013-02-26 10:25:12 -08001043
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -07001044 return ret;
1045}
1046
Saurabh Shahacf10202013-02-26 10:15:15 -08001047void trimLayer(hwc_context_t *ctx, const int& dpy, const int& transform,
1048 hwc_rect_t& crop, hwc_rect_t& dst) {
1049 int hw_w = ctx->dpyAttr[dpy].xres;
1050 int hw_h = ctx->dpyAttr[dpy].yres;
1051 if(dst.left < 0 || dst.top < 0 ||
1052 dst.right > hw_w || dst.bottom > hw_h) {
1053 hwc_rect_t scissor = {0, 0, hw_w, hw_h };
1054 qhwc::calculate_crop_rects(crop, dst, scissor, transform);
1055 }
1056}
1057
1058void setMdpFlags(hwc_layer_1_t *layer,
1059 ovutils::eMdpFlags &mdpFlags,
Ramkumar Radhakrishnan59a11072013-04-15 16:14:49 -07001060 int rotDownscale, int transform) {
Saurabh Shahacf10202013-02-26 10:15:15 -08001061 private_handle_t *hnd = (private_handle_t *)layer->handle;
1062 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
Saurabh Shahacf10202013-02-26 10:15:15 -08001063
1064 if(layer->blending == HWC_BLENDING_PREMULT) {
1065 ovutils::setMdpFlags(mdpFlags,
1066 ovutils::OV_MDP_BLEND_FG_PREMULT);
1067 }
1068
1069 if(isYuvBuffer(hnd)) {
1070 if(isSecureBuffer(hnd)) {
1071 ovutils::setMdpFlags(mdpFlags,
1072 ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
1073 }
1074 if(metadata && (metadata->operation & PP_PARAM_INTERLACED) &&
1075 metadata->interlaced) {
1076 ovutils::setMdpFlags(mdpFlags,
1077 ovutils::OV_MDP_DEINTERLACE);
1078 }
1079 //Pre-rotation will be used using rotator.
1080 if(transform & HWC_TRANSFORM_ROT_90) {
1081 ovutils::setMdpFlags(mdpFlags,
1082 ovutils::OV_MDP_SOURCE_ROTATED_90);
1083 }
1084 }
1085
1086 //No 90 component and no rot-downscale then flips done by MDP
1087 //If we use rot then it might as well do flips
Ramkumar Radhakrishnan59a11072013-04-15 16:14:49 -07001088 if(!(transform & HWC_TRANSFORM_ROT_90) && !rotDownscale) {
1089 if(transform & HWC_TRANSFORM_FLIP_H) {
Saurabh Shahacf10202013-02-26 10:15:15 -08001090 ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_FLIP_H);
1091 }
1092
Ramkumar Radhakrishnan59a11072013-04-15 16:14:49 -07001093 if(transform & HWC_TRANSFORM_FLIP_V) {
Saurabh Shahacf10202013-02-26 10:15:15 -08001094 ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_FLIP_V);
1095 }
1096 }
Saurabh Shah5daeee52013-01-23 16:52:26 +08001097
1098 if(metadata &&
1099 ((metadata->operation & PP_PARAM_HSIC)
1100 || (metadata->operation & PP_PARAM_IGC)
1101 || (metadata->operation & PP_PARAM_SHARP2))) {
1102 ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_PP_EN);
1103 }
Saurabh Shahacf10202013-02-26 10:15:15 -08001104}
1105
Prabhanjan Kandulaa1d83012013-04-23 13:06:02 +05301106int configRotator(Rotator *rot, Whf& whf,
Sushil Chauhan1cac8152013-05-08 15:53:51 -07001107 hwc_rect_t& crop, const eMdpFlags& mdpFlags,
Sushil Chauhan80fc1f92013-04-23 17:30:05 -07001108 const eTransform& orient, const int& downscale) {
Sushil Chauhan1cac8152013-05-08 15:53:51 -07001109
Prabhanjan Kandulaa1d83012013-04-23 13:06:02 +05301110 // Fix alignments for TILED format
1111 if(whf.format == MDP_Y_CRCB_H2V2_TILE ||
1112 whf.format == MDP_Y_CBCR_H2V2_TILE) {
1113 whf.w = utils::alignup(whf.w, 64);
1114 whf.h = utils::alignup(whf.h, 32);
1115 }
Saurabh Shahacf10202013-02-26 10:15:15 -08001116 rot->setSource(whf);
Sushil Chauhan1cac8152013-05-08 15:53:51 -07001117
1118 if (qdutils::MDPVersion::getInstance().getMDPVersion() >=
1119 qdutils::MDSS_V5) {
1120 uint32_t crop_w = (crop.right - crop.left);
1121 uint32_t crop_h = (crop.bottom - crop.top);
Sushil Chauhan6181aa22013-05-17 18:04:10 -07001122 if (ovutils::isYuv(whf.format)) {
1123 ovutils::normalizeCrop((uint32_t&)crop.left, crop_w);
1124 ovutils::normalizeCrop((uint32_t&)crop.top, crop_h);
Sushil Chauhan5491c8a2013-05-24 10:15:30 -07001125 // For interlaced, crop.h should be 4-aligned
1126 if ((mdpFlags & ovutils::OV_MDP_DEINTERLACE) && (crop_h % 4))
1127 crop_h = ovutils::aligndown(crop_h, 4);
Sushil Chauhan6181aa22013-05-17 18:04:10 -07001128 crop.right = crop.left + crop_w;
1129 crop.bottom = crop.top + crop_h;
1130 }
Sushil Chauhan1cac8152013-05-08 15:53:51 -07001131 Dim rotCrop(crop.left, crop.top, crop_w, crop_h);
1132 rot->setCrop(rotCrop);
1133 }
1134
Saurabh Shahacf10202013-02-26 10:15:15 -08001135 rot->setFlags(mdpFlags);
1136 rot->setTransform(orient);
1137 rot->setDownscale(downscale);
1138 if(!rot->commit()) return -1;
1139 return 0;
1140}
1141
Saurabh Shahe2474082013-05-15 16:32:13 -07001142int configMdp(Overlay *ov, const PipeArgs& parg,
Saurabh Shahacf10202013-02-26 10:15:15 -08001143 const eTransform& orient, const hwc_rect_t& crop,
Saurabh Shah5daeee52013-01-23 16:52:26 +08001144 const hwc_rect_t& pos, const MetaData_t *metadata,
1145 const eDest& dest) {
Saurabh Shahacf10202013-02-26 10:15:15 -08001146 ov->setSource(parg, dest);
1147 ov->setTransform(orient, dest);
1148
1149 int crop_w = crop.right - crop.left;
1150 int crop_h = crop.bottom - crop.top;
1151 Dim dcrop(crop.left, crop.top, crop_w, crop_h);
1152 ov->setCrop(dcrop, dest);
1153
1154 int posW = pos.right - pos.left;
1155 int posH = pos.bottom - pos.top;
1156 Dim position(pos.left, pos.top, posW, posH);
1157 ov->setPosition(position, dest);
1158
Saurabh Shah5daeee52013-01-23 16:52:26 +08001159 if (metadata)
1160 ov->setVisualParams(*metadata, dest);
1161
Saurabh Shahacf10202013-02-26 10:15:15 -08001162 if (!ov->commit(dest)) {
1163 return -1;
1164 }
1165 return 0;
1166}
1167
Saurabh Shahe2474082013-05-15 16:32:13 -07001168void updateSource(eTransform& orient, Whf& whf,
Saurabh Shahacf10202013-02-26 10:15:15 -08001169 hwc_rect_t& crop) {
1170 Dim srcCrop(crop.left, crop.top,
1171 crop.right - crop.left,
1172 crop.bottom - crop.top);
Saurabh Shah76fd6552013-03-14 14:45:38 -07001173 orient = static_cast<eTransform>(ovutils::getMdpOrient(orient));
Saurabh Shahacf10202013-02-26 10:15:15 -08001174 preRotateSource(orient, whf, srcCrop);
Sushil Chauhan80fc1f92013-04-23 17:30:05 -07001175 if (qdutils::MDPVersion::getInstance().getMDPVersion() >=
1176 qdutils::MDSS_V5) {
1177 // Source for overlay will be the cropped (and rotated)
1178 crop.left = 0;
1179 crop.top = 0;
1180 crop.right = srcCrop.w;
1181 crop.bottom = srcCrop.h;
1182 // Set width & height equal to sourceCrop w & h
1183 whf.w = srcCrop.w;
1184 whf.h = srcCrop.h;
1185 } else {
1186 crop.left = srcCrop.x;
1187 crop.top = srcCrop.y;
1188 crop.right = srcCrop.x + srcCrop.w;
1189 crop.bottom = srcCrop.y + srcCrop.h;
1190 }
Saurabh Shahacf10202013-02-26 10:15:15 -08001191}
1192
Saurabh Shah88e4d272013-09-03 13:31:29 -07001193int configureNonSplit(hwc_context_t *ctx, hwc_layer_1_t *layer,
Ramkumar Radhakrishnan59a11072013-04-15 16:14:49 -07001194 const int& dpy, eMdpFlags& mdpFlags, eZorder& z,
1195 eIsFg& isFg, const eDest& dest, Rotator **rot) {
Saurabh Shahacf10202013-02-26 10:15:15 -08001196
1197 private_handle_t *hnd = (private_handle_t *)layer->handle;
1198 if(!hnd) {
1199 ALOGE("%s: layer handle is NULL", __FUNCTION__);
1200 return -1;
1201 }
1202
Saurabh Shah5daeee52013-01-23 16:52:26 +08001203 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
1204
Saurabh Shahacf10202013-02-26 10:15:15 -08001205 hwc_rect_t crop = layer->sourceCrop;
1206 hwc_rect_t dst = layer->displayFrame;
1207 int transform = layer->transform;
1208 eTransform orient = static_cast<eTransform>(transform);
1209 int downscale = 0;
1210 int rotFlags = ovutils::ROT_FLAGS_NONE;
1211 Whf whf(hnd->width, hnd->height,
1212 getMdpFormat(hnd->format), hnd->size);
1213
Ramkumar Radhakrishnan59a11072013-04-15 16:14:49 -07001214 if(dpy && isYuvBuffer(hnd)) {
1215 if(!ctx->listStats[dpy].isDisplayAnimating) {
1216 ctx->mPrevCropVideo = crop;
1217 ctx->mPrevDestVideo = dst;
1218 ctx->mPrevTransformVideo = transform;
1219 } else {
1220 // Restore the previous crop, dest rect and transform values, during
1221 // animation to avoid displaying videos at random coordinates.
1222 crop = ctx->mPrevCropVideo;
1223 dst = ctx->mPrevDestVideo;
1224 transform = ctx->mPrevTransformVideo;
Ramkumar Radhakrishnand7e8d0c2013-05-20 17:13:12 -07001225 orient = static_cast<eTransform>(transform);
Ramkumar Radhakrishnan59a11072013-04-15 16:14:49 -07001226 //In you tube use case when a device rotated from landscape to
1227 // portrait, set the isFg flag and zOrder to avoid displaying UI on
1228 // hdmi during animation
1229 if(ctx->deviceOrientation) {
1230 isFg = ovutils::IS_FG_SET;
1231 z = ZORDER_1;
1232 }
1233 }
Ramkumar Radhakrishnan66f856c2013-08-21 16:09:47 -07001234 calcExtDisplayPosition(ctx, hnd, dpy, crop, dst,
1235 transform, orient);
Arun Kumar K.Rffef7482013-04-10 14:17:22 -07001236 }
1237
Saurabh Shahacf10202013-02-26 10:15:15 -08001238 if(isYuvBuffer(hnd) && ctx->mMDP.version >= qdutils::MDP_V4_2 &&
Jeykumar Sankaran85977e32013-02-25 17:06:08 -08001239 ctx->mMDP.version < qdutils::MDSS_V5) {
1240 downscale = getDownscaleFactor(
1241 crop.right - crop.left,
1242 crop.bottom - crop.top,
1243 dst.right - dst.left,
1244 dst.bottom - dst.top);
Saurabh Shahacf10202013-02-26 10:15:15 -08001245 if(downscale) {
1246 rotFlags = ROT_DOWNSCALE_ENABLED;
1247 }
1248 }
1249
Ramkumar Radhakrishnan59a11072013-04-15 16:14:49 -07001250 setMdpFlags(layer, mdpFlags, downscale, transform);
Saurabh Shahacf10202013-02-26 10:15:15 -08001251 trimLayer(ctx, dpy, transform, crop, dst);
1252
Saurabh Shaha9da08f2013-07-03 13:27:53 -07001253 //Will do something only if feature enabled and conditions suitable
1254 //hollow call otherwise
1255 if(ctx->mAD->prepare(ctx, crop, whf, hnd)) {
1256 overlay::Writeback *wb = overlay::Writeback::getInstance();
1257 whf.format = wb->getOutputFormat();
1258 }
1259
Saurabh Shahacf10202013-02-26 10:15:15 -08001260 if(isYuvBuffer(hnd) && //if 90 component or downscale, use rot
1261 ((transform & HWC_TRANSFORM_ROT_90) || downscale)) {
1262 *rot = ctx->mRotMgr->getNext();
1263 if(*rot == NULL) return -1;
Saurabh Shah1a03d482013-05-29 13:44:20 -07001264 BwcPM::setBwc(ctx, crop, dst, transform, mdpFlags);
Saurabh Shahacf10202013-02-26 10:15:15 -08001265 //Configure rotator for pre-rotation
Sushil Chauhan80fc1f92013-04-23 17:30:05 -07001266 if(configRotator(*rot, whf, crop, mdpFlags, orient, downscale) < 0) {
1267 ALOGE("%s: configRotator failed!", __FUNCTION__);
Sushil Chauhanbd3ea922013-05-15 12:25:26 -07001268 ctx->mOverlay->clear(dpy);
Saurabh Shahacf10202013-02-26 10:15:15 -08001269 return -1;
Sushil Chauhan80fc1f92013-04-23 17:30:05 -07001270 }
Saurabh Shah23a813c2013-03-20 16:58:12 -07001271 ctx->mLayerRotMap[dpy]->add(layer, *rot);
Saurabh Shahacf10202013-02-26 10:15:15 -08001272 whf.format = (*rot)->getDstFormat();
1273 updateSource(orient, whf, crop);
Saurabh Shahacf10202013-02-26 10:15:15 -08001274 rotFlags |= ovutils::ROT_PREROTATED;
1275 }
1276
Saurabh Shah76fd6552013-03-14 14:45:38 -07001277 //For the mdp, since either we are pre-rotating or MDP does flips
1278 orient = OVERLAY_TRANSFORM_0;
1279 transform = 0;
Naseer Ahmed522ce662013-03-18 20:14:05 -04001280 PipeArgs parg(mdpFlags, whf, z, isFg,
1281 static_cast<eRotFlags>(rotFlags), layer->planeAlpha,
1282 (ovutils::eBlending) getBlending(layer->blending));
1283
Saurabh Shah5daeee52013-01-23 16:52:26 +08001284 if(configMdp(ctx->mOverlay, parg, orient, crop, dst, metadata, dest) < 0) {
Saurabh Shahacf10202013-02-26 10:15:15 -08001285 ALOGE("%s: commit failed for low res panel", __FUNCTION__);
1286 return -1;
1287 }
1288 return 0;
1289}
1290
Saurabh Shahd9e426d2013-08-01 13:35:31 -07001291//Helper to 1) Ensure crops dont have gaps 2) Ensure L and W are even
1292static void sanitizeSourceCrop(hwc_rect_t& cropL, hwc_rect_t& cropR,
1293 private_handle_t *hnd) {
1294 if(cropL.right - cropL.left) {
1295 if(isYuvBuffer(hnd)) {
1296 //Always safe to even down left
1297 ovutils::even_floor(cropL.left);
1298 //If right is even, automatically width is even, since left is
1299 //already even
1300 ovutils::even_floor(cropL.right);
1301 }
1302 //Make sure there are no gaps between left and right splits if the layer
1303 //is spread across BOTH halves
1304 if(cropR.right - cropR.left) {
1305 cropR.left = cropL.right;
1306 }
1307 }
1308
1309 if(cropR.right - cropR.left) {
1310 if(isYuvBuffer(hnd)) {
1311 //Always safe to even down left
1312 ovutils::even_floor(cropR.left);
1313 //If right is even, automatically width is even, since left is
1314 //already even
1315 ovutils::even_floor(cropR.right);
1316 }
1317 }
1318}
1319
Saurabh Shah88e4d272013-09-03 13:31:29 -07001320int configureSplit(hwc_context_t *ctx, hwc_layer_1_t *layer,
Ramkumar Radhakrishnan59a11072013-04-15 16:14:49 -07001321 const int& dpy, eMdpFlags& mdpFlagsL, eZorder& z,
1322 eIsFg& isFg, const eDest& lDest, const eDest& rDest,
Saurabh Shahacf10202013-02-26 10:15:15 -08001323 Rotator **rot) {
1324 private_handle_t *hnd = (private_handle_t *)layer->handle;
1325 if(!hnd) {
1326 ALOGE("%s: layer handle is NULL", __FUNCTION__);
1327 return -1;
1328 }
1329
Saurabh Shah5daeee52013-01-23 16:52:26 +08001330 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
1331
Saurabh Shahacf10202013-02-26 10:15:15 -08001332 int hw_w = ctx->dpyAttr[dpy].xres;
1333 int hw_h = ctx->dpyAttr[dpy].yres;
1334 hwc_rect_t crop = layer->sourceCrop;
1335 hwc_rect_t dst = layer->displayFrame;
1336 int transform = layer->transform;
1337 eTransform orient = static_cast<eTransform>(transform);
1338 const int downscale = 0;
1339 int rotFlags = ROT_FLAGS_NONE;
1340
1341 Whf whf(hnd->width, hnd->height,
1342 getMdpFormat(hnd->format), hnd->size);
1343
Ramkumar Radhakrishnan59a11072013-04-15 16:14:49 -07001344 if(dpy && isYuvBuffer(hnd)) {
1345 if(!ctx->listStats[dpy].isDisplayAnimating) {
1346 ctx->mPrevCropVideo = crop;
1347 ctx->mPrevDestVideo = dst;
1348 ctx->mPrevTransformVideo = transform;
1349 } else {
1350 // Restore the previous crop, dest rect and transform values, during
1351 // animation to avoid displaying videos at random coordinates.
1352 crop = ctx->mPrevCropVideo;
1353 dst = ctx->mPrevDestVideo;
1354 transform = ctx->mPrevTransformVideo;
Ramkumar Radhakrishnand7e8d0c2013-05-20 17:13:12 -07001355 orient = static_cast<eTransform>(transform);
Ramkumar Radhakrishnan59a11072013-04-15 16:14:49 -07001356 //In you tube use case when a device rotated from landscape to
1357 // portrait, set the isFg flag and zOrder to avoid displaying UI on
1358 // hdmi during animation
1359 if(ctx->deviceOrientation) {
1360 isFg = ovutils::IS_FG_SET;
1361 z = ZORDER_1;
1362 }
1363 }
1364 }
1365
1366
Ramkumar Radhakrishnan9d52f432013-05-14 14:46:59 -07001367 setMdpFlags(layer, mdpFlagsL, 0, transform);
Saurabh Shahd9e426d2013-08-01 13:35:31 -07001368
1369 if(lDest != OV_INVALID && rDest != OV_INVALID) {
1370 //Enable overfetch
1371 setMdpFlags(mdpFlagsL, OV_MDSS_MDP_DUAL_PIPE);
1372 }
1373
Saurabh Shahacf10202013-02-26 10:15:15 -08001374 trimLayer(ctx, dpy, transform, crop, dst);
1375
Saurabh Shaha9da08f2013-07-03 13:27:53 -07001376 //Will do something only if feature enabled and conditions suitable
1377 //hollow call otherwise
1378 if(ctx->mAD->prepare(ctx, crop, whf, hnd)) {
1379 overlay::Writeback *wb = overlay::Writeback::getInstance();
1380 whf.format = wb->getOutputFormat();
1381 }
1382
Saurabh Shahacf10202013-02-26 10:15:15 -08001383 if(isYuvBuffer(hnd) && (transform & HWC_TRANSFORM_ROT_90)) {
1384 (*rot) = ctx->mRotMgr->getNext();
1385 if((*rot) == NULL) return -1;
1386 //Configure rotator for pre-rotation
Sushil Chauhan80fc1f92013-04-23 17:30:05 -07001387 if(configRotator(*rot, whf, crop, mdpFlagsL, orient, downscale) < 0) {
1388 ALOGE("%s: configRotator failed!", __FUNCTION__);
Sushil Chauhanbd3ea922013-05-15 12:25:26 -07001389 ctx->mOverlay->clear(dpy);
Saurabh Shahacf10202013-02-26 10:15:15 -08001390 return -1;
Sushil Chauhan80fc1f92013-04-23 17:30:05 -07001391 }
Saurabh Shah23a813c2013-03-20 16:58:12 -07001392 ctx->mLayerRotMap[dpy]->add(layer, *rot);
Saurabh Shahacf10202013-02-26 10:15:15 -08001393 whf.format = (*rot)->getDstFormat();
1394 updateSource(orient, whf, crop);
Saurabh Shahacf10202013-02-26 10:15:15 -08001395 rotFlags |= ROT_PREROTATED;
1396 }
1397
1398 eMdpFlags mdpFlagsR = mdpFlagsL;
1399 setMdpFlags(mdpFlagsR, OV_MDSS_MDP_RIGHT_MIXER);
1400
Saurabh Shahd9e426d2013-08-01 13:35:31 -07001401 hwc_rect_t tmp_cropL = {0}, tmp_dstL = {0};
1402 hwc_rect_t tmp_cropR = {0}, tmp_dstR = {0};
Saurabh Shahacf10202013-02-26 10:15:15 -08001403
Saurabh Shah07a8ca82013-08-06 18:45:42 -07001404 const int lSplit = getLeftSplit(ctx, dpy);
1405
Saurabh Shahacf10202013-02-26 10:15:15 -08001406 if(lDest != OV_INVALID) {
1407 tmp_cropL = crop;
1408 tmp_dstL = dst;
Saurabh Shah67a38c32013-06-10 16:23:15 -07001409 hwc_rect_t scissor = {0, 0, lSplit, hw_h };
Saurabh Shahacf10202013-02-26 10:15:15 -08001410 qhwc::calculate_crop_rects(tmp_cropL, tmp_dstL, scissor, 0);
1411 }
1412 if(rDest != OV_INVALID) {
1413 tmp_cropR = crop;
1414 tmp_dstR = dst;
Saurabh Shah67a38c32013-06-10 16:23:15 -07001415 hwc_rect_t scissor = {lSplit, 0, hw_w, hw_h };
Saurabh Shahacf10202013-02-26 10:15:15 -08001416 qhwc::calculate_crop_rects(tmp_cropR, tmp_dstR, scissor, 0);
1417 }
1418
Saurabh Shahd9e426d2013-08-01 13:35:31 -07001419 sanitizeSourceCrop(tmp_cropL, tmp_cropR, hnd);
1420
Saurabh Shah94d62362013-07-02 10:58:48 -07001421 //When buffer is H-flipped, contents of mixer config also needs to swapped
Saurabh Shahacf10202013-02-26 10:15:15 -08001422 //Not needed if the layer is confined to one half of the screen.
1423 //If rotator has been used then it has also done the flips, so ignore them.
Saurabh Shah94d62362013-07-02 10:58:48 -07001424 if((orient & OVERLAY_TRANSFORM_FLIP_H) && lDest != OV_INVALID
Ramkumar Radhakrishnanfe7ce802013-04-30 11:19:17 -07001425 && rDest != OV_INVALID && (*rot) == NULL) {
Saurabh Shahacf10202013-02-26 10:15:15 -08001426 hwc_rect_t new_cropR;
1427 new_cropR.left = tmp_cropL.left;
1428 new_cropR.right = new_cropR.left + (tmp_cropR.right - tmp_cropR.left);
1429
1430 hwc_rect_t new_cropL;
1431 new_cropL.left = new_cropR.right;
1432 new_cropL.right = tmp_cropR.right;
1433
1434 tmp_cropL.left = new_cropL.left;
1435 tmp_cropL.right = new_cropL.right;
1436
1437 tmp_cropR.left = new_cropR.left;
1438 tmp_cropR.right = new_cropR.right;
1439
1440 }
1441
Saurabh Shah76fd6552013-03-14 14:45:38 -07001442 //For the mdp, since either we are pre-rotating or MDP does flips
1443 orient = OVERLAY_TRANSFORM_0;
1444 transform = 0;
1445
Saurabh Shahacf10202013-02-26 10:15:15 -08001446 //configure left mixer
1447 if(lDest != OV_INVALID) {
1448 PipeArgs pargL(mdpFlagsL, whf, z, isFg,
Naseer Ahmed522ce662013-03-18 20:14:05 -04001449 static_cast<eRotFlags>(rotFlags), layer->planeAlpha,
1450 (ovutils::eBlending) getBlending(layer->blending));
1451
Saurabh Shahacf10202013-02-26 10:15:15 -08001452 if(configMdp(ctx->mOverlay, pargL, orient,
Saurabh Shah5daeee52013-01-23 16:52:26 +08001453 tmp_cropL, tmp_dstL, metadata, lDest) < 0) {
Saurabh Shahacf10202013-02-26 10:15:15 -08001454 ALOGE("%s: commit failed for left mixer config", __FUNCTION__);
1455 return -1;
1456 }
1457 }
1458
1459 //configure right mixer
1460 if(rDest != OV_INVALID) {
1461 PipeArgs pargR(mdpFlagsR, whf, z, isFg,
Naseer Ahmed522ce662013-03-18 20:14:05 -04001462 static_cast<eRotFlags>(rotFlags),
1463 layer->planeAlpha,
1464 (ovutils::eBlending) getBlending(layer->blending));
Saurabh Shah67a38c32013-06-10 16:23:15 -07001465 tmp_dstR.right = tmp_dstR.right - lSplit;
1466 tmp_dstR.left = tmp_dstR.left - lSplit;
Saurabh Shahacf10202013-02-26 10:15:15 -08001467 if(configMdp(ctx->mOverlay, pargR, orient,
Saurabh Shah5daeee52013-01-23 16:52:26 +08001468 tmp_cropR, tmp_dstR, metadata, rDest) < 0) {
Saurabh Shahacf10202013-02-26 10:15:15 -08001469 ALOGE("%s: commit failed for right mixer config", __FUNCTION__);
1470 return -1;
1471 }
1472 }
1473
1474 return 0;
1475}
Arun Kumar K.Ra2978452013-02-07 01:34:24 -08001476
Amara Venkata Mastan Manoj Kumar9d373c02013-08-20 14:30:09 -07001477bool canUseRotator(hwc_context_t *ctx, int dpy) {
Saurabh Shahe2474082013-05-15 16:32:13 -07001478 if(qdutils::MDPVersion::getInstance().is8x26() &&
Amara Venkata Mastan Manoj Kumar5cbac942013-08-13 19:00:14 -07001479 ctx->mVirtualDisplay->isConnected() &&
1480 !ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].isPause) {
Amara Venkata Mastan Manoj Kumar9d373c02013-08-20 14:30:09 -07001481 // Allow if YUV needs rotation and DMA is configured to BLOCK mode for
1482 // primary. For portrait videos usecase on WFD, Driver supports
1483 // multiplexing of DMA pipe in LINE and BLOCK mode.
1484 if(dpy == HWC_DISPLAY_PRIMARY)
1485 return false;
Saurabh Shahe2474082013-05-15 16:32:13 -07001486 }
Terence Hampson45c02ef2013-05-17 17:00:11 -04001487 if(ctx->mMDP.version == qdutils::MDP_V3_0_4)
1488 return false;
Saurabh Shahe2474082013-05-15 16:32:13 -07001489 return true;
1490}
1491
Saurabh Shah07a8ca82013-08-06 18:45:42 -07001492int getLeftSplit(hwc_context_t *ctx, const int& dpy) {
1493 //Default even split for all displays with high res
1494 int lSplit = ctx->dpyAttr[dpy].xres / 2;
1495 if(dpy == HWC_DISPLAY_PRIMARY &&
1496 qdutils::MDPVersion::getInstance().getLeftSplit()) {
1497 //Override if split published by driver for primary
1498 lSplit = qdutils::MDPVersion::getInstance().getLeftSplit();
1499 }
1500 return lSplit;
1501}
Saurabh Shah1a03d482013-05-29 13:44:20 -07001502
Saurabh Shah88e4d272013-09-03 13:31:29 -07001503bool isDisplaySplit(hwc_context_t* ctx, int dpy) {
1504 if(ctx->dpyAttr[dpy].xres > qdutils::MAX_DISPLAY_DIM) {
1505 return true;
1506 }
1507 //For testing we could split primary via device tree values
1508 if(dpy == HWC_DISPLAY_PRIMARY &&
1509 qdutils::MDPVersion::getInstance().getRightSplit()) {
1510 return true;
1511 }
1512 return false;
1513}
1514
Saurabh Shah1a03d482013-05-29 13:44:20 -07001515void BwcPM::setBwc(hwc_context_t *ctx, const hwc_rect_t& crop,
1516 const hwc_rect_t& dst, const int& transform,
1517 ovutils::eMdpFlags& mdpFlags) {
1518 //Target doesnt support Bwc
1519 if(!qdutils::MDPVersion::getInstance().supportsBWC()) {
1520 return;
1521 }
1522 //src width > MAX mixer supported dim
1523 if((crop.right - crop.left) > qdutils::MAX_DISPLAY_DIM) {
1524 return;
1525 }
1526 //External connected
Jeykumar Sankaran27dee262013-08-01 17:09:54 -07001527 if(ctx->mExtDisplay->isConnected()|| ctx->mVirtualDisplay->isConnected()) {
Saurabh Shah1a03d482013-05-29 13:44:20 -07001528 return;
1529 }
1530 //Decimation necessary, cannot use BWC. H/W requirement.
1531 if(qdutils::MDPVersion::getInstance().supportsDecimation()) {
1532 int src_w = crop.right - crop.left;
1533 int src_h = crop.bottom - crop.top;
1534 int dst_w = dst.right - dst.left;
1535 int dst_h = dst.bottom - dst.top;
1536 if(transform & HAL_TRANSFORM_ROT_90) {
1537 swap(src_w, src_h);
1538 }
1539 float horDscale = 0.0f;
1540 float verDscale = 0.0f;
Saurabh Shahc5b96dc2013-06-05 13:19:52 -07001541 int horzDeci = 0;
1542 int vertDeci = 0;
Saurabh Shah1a03d482013-05-29 13:44:20 -07001543 ovutils::getDecimationFactor(src_w, src_h, dst_w, dst_h, horDscale,
1544 verDscale);
Saurabh Shahc5b96dc2013-06-05 13:19:52 -07001545 //TODO Use log2f once math.h has it
1546 if((int)horDscale)
1547 horzDeci = (int)(log(horDscale) / log(2));
1548 if((int)verDscale)
1549 vertDeci = (int)(log(verDscale) / log(2));
1550 if(horzDeci || vertDeci) return;
Saurabh Shah1a03d482013-05-29 13:44:20 -07001551 }
1552 //Property
1553 char value[PROPERTY_VALUE_MAX];
1554 property_get("debug.disable.bwc", value, "0");
1555 if(atoi(value)) return;
1556
1557 ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDSS_MDP_BWC_EN);
1558}
1559
Saurabh Shah23a813c2013-03-20 16:58:12 -07001560void LayerRotMap::add(hwc_layer_1_t* layer, Rotator *rot) {
1561 if(mCount >= MAX_SESS) return;
1562 mLayer[mCount] = layer;
1563 mRot[mCount] = rot;
1564 mCount++;
1565}
1566
1567void LayerRotMap::reset() {
1568 for (int i = 0; i < MAX_SESS; i++) {
1569 mLayer[i] = 0;
1570 mRot[i] = 0;
1571 }
1572 mCount = 0;
1573}
1574
1575void LayerRotMap::setReleaseFd(const int& fence) {
1576 for(uint32_t i = 0; i < mCount; i++) {
1577 mRot[i]->setReleaseFd(dup(fence));
1578 }
1579}
1580
Saurabh Shahacf10202013-02-26 10:15:15 -08001581};//namespace qhwc