blob: 3c04408a158951afa0b771f3b979f2dddacbe60c [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
Naseer Ahmed758bfc52012-11-28 17:02:08 -050021#include <sys/ioctl.h>
Naseer Ahmed66e97882013-03-19 20:42:11 -040022#include <linux/fb.h>
Saurabh Shah86c17292013-02-08 15:24:13 -080023#include <binder/IServiceManager.h>
Naseer Ahmed5b6708a2012-08-02 13:46:08 -070024#include <EGL/egl.h>
Saurabh Shahfc2acbe2012-08-17 19:47:52 -070025#include <cutils/properties.h>
26#include <gralloc_priv.h>
Naseer Ahmed758bfc52012-11-28 17:02:08 -050027#include <overlay.h>
Saurabh Shahacf10202013-02-26 10:15:15 -080028#include <overlayRotator.h>
Naseer Ahmed29a26812012-06-14 00:56:20 -070029#include "hwc_utils.h"
Naseer Ahmed54821fe2012-11-28 18:44:38 -050030#include "hwc_mdpcomp.h"
Saurabh Shahcf053c62012-12-13 12:32:55 -080031#include "hwc_fbupdate.h"
Saurabh Shahacf10202013-02-26 10:15:15 -080032#include "hwc_video.h"
Naseer Ahmeda87da602012-07-01 23:54:19 -070033#include "mdp_version.h"
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080034#include "hwc_copybit.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070035#include "external.h"
Saurabh Shah86c17292013-02-08 15:24:13 -080036#include "hwc_qclient.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070037#include "QService.h"
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080038#include "comptype.h"
Saurabh Shah86c17292013-02-08 15:24:13 -080039
40using namespace qClient;
41using namespace qService;
42using namespace android;
Saurabh Shahacf10202013-02-26 10:15:15 -080043using namespace overlay;
44using namespace overlay::utils;
45namespace ovutils = overlay::utils;
Saurabh Shah86c17292013-02-08 15:24:13 -080046
Naseer Ahmed29a26812012-06-14 00:56:20 -070047namespace qhwc {
Naseer Ahmed72cf9762012-07-21 12:17:13 -070048
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -080049static int openFramebufferDevice(hwc_context_t *ctx)
Naseer Ahmed72cf9762012-07-21 12:17:13 -070050{
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -080051 struct fb_fix_screeninfo finfo;
52 struct fb_var_screeninfo info;
53
54 int fb_fd = openFb(HWC_DISPLAY_PRIMARY);
55
56 if (ioctl(fb_fd, FBIOGET_VSCREENINFO, &info) == -1)
57 return -errno;
58
59 if (int(info.width) <= 0 || int(info.height) <= 0) {
60 // the driver doesn't return that information
61 // default to 160 dpi
62 info.width = ((info.xres * 25.4f)/160.0f + 0.5f);
63 info.height = ((info.yres * 25.4f)/160.0f + 0.5f);
Naseer Ahmed72cf9762012-07-21 12:17:13 -070064 }
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -080065
66 float xdpi = (info.xres * 25.4f) / info.width;
67 float ydpi = (info.yres * 25.4f) / info.height;
68
69#ifdef MSMFB_METADATA_GET
70 struct msmfb_metadata metadata;
71 memset(&metadata, 0 , sizeof(metadata));
72 metadata.op = metadata_op_frame_rate;
73
74 if (ioctl(fb_fd, MSMFB_METADATA_GET, &metadata) == -1) {
75 ALOGE("Error retrieving panel frame rate");
76 return -errno;
77 }
78
79 float fps = metadata.data.panel_frame_rate;
80#else
81 //XXX: Remove reserved field usage on all baselines
82 //The reserved[3] field is used to store FPS by the driver.
83 float fps = info.reserved[3] & 0xFF;
84#endif
85
86 if (ioctl(fb_fd, FBIOGET_FSCREENINFO, &finfo) == -1)
87 return -errno;
88
89 if (finfo.smem_len <= 0)
90 return -errno;
91
92 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = fb_fd;
93 //xres, yres may not be 32 aligned
94 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].stride = finfo.line_length /(info.xres/8);
95 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres = info.xres;
96 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].yres = info.yres;
97 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xdpi = xdpi;
98 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].ydpi = ydpi;
99 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period = 1000000000l / fps;
100
101 return 0;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700102}
103
Naseer Ahmed29a26812012-06-14 00:56:20 -0700104void initContext(hwc_context_t *ctx)
105{
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700106 openFramebufferDevice(ctx);
Naseer Ahmed96c4c952012-07-25 18:27:14 -0700107 ctx->mMDP.version = qdutils::MDPVersion::getInstance().getMDPVersion();
108 ctx->mMDP.hasOverlay = qdutils::MDPVersion::getInstance().hasOverlay();
109 ctx->mMDP.panel = qdutils::MDPVersion::getInstance().getPanelType();
Sushil Chauhan07a2c762013-03-06 15:36:49 -0800110 overlay::Overlay::initOverlay();
111 ctx->mOverlay = overlay::Overlay::getInstance();
112 ctx->mRotMgr = new RotMgr();
113
Saurabh Shahcf053c62012-12-13 12:32:55 -0800114 //Is created and destroyed only once for primary
115 //For external it could get created and destroyed multiple times depending
116 //on what external we connect to.
117 ctx->mFBUpdate[HWC_DISPLAY_PRIMARY] =
118 IFBUpdate::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres,
119 HWC_DISPLAY_PRIMARY);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800120
Saurabh Shahacf10202013-02-26 10:15:15 -0800121 ctx->mVidOv[HWC_DISPLAY_PRIMARY] =
122 IVideoOverlay::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres,
123 HWC_DISPLAY_PRIMARY);
124
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800125 // Check if the target supports copybit compostion (dyn/mdp/c2d) to
126 // decide if we need to open the copybit module.
127 int compositionType =
128 qdutils::QCCompositionType::getInstance().getCompositionType();
129
130 if (compositionType & (qdutils::COMPOSITION_TYPE_DYN |
131 qdutils::COMPOSITION_TYPE_MDP |
132 qdutils::COMPOSITION_TYPE_C2D)) {
133 ctx->mCopyBit[HWC_DISPLAY_PRIMARY] = new CopyBit();
134 }
135
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700136 ctx->mExtDisplay = new ExternalDisplay(ctx);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800137 for (uint32_t i = 0; i < MAX_DISPLAYS; i++)
Naseer Ahmede78f0522012-12-07 18:24:28 -0500138 ctx->mLayerCache[i] = new LayerCache();
Saurabh Shahcbf7ccc2012-12-19 16:45:51 -0800139 ctx->mMDPComp = MDPComp::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres);
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500140 MDPComp::init(ctx);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700141
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400142 pthread_mutex_init(&(ctx->vstate.lock), NULL);
143 pthread_cond_init(&(ctx->vstate.cond), NULL);
144 ctx->vstate.enable = false;
Naseer Ahmed56601cd2013-03-05 11:34:14 -0500145 ctx->vstate.fakevsync = false;
Amara Venkata Mastan Manoj Kumar75526f52012-12-27 18:27:01 -0800146 ctx->mExtDispConfiguring = false;
Saurabh Shah86c17292013-02-08 15:24:13 -0800147
148 //Right now hwc starts the service but anybody could do it, or it could be
149 //independent process as well.
150 QService::init();
151 sp<IQClient> client = new QClient(ctx);
152 interface_cast<IQService>(
153 defaultServiceManager()->getService(
154 String16("display.qservice")))->connect(client);
155
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700156 ALOGI("Initializing Qualcomm Hardware Composer");
Naseer Ahmed96c4c952012-07-25 18:27:14 -0700157 ALOGI("MDP version: %d", ctx->mMDP.version);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700158}
159
160void closeContext(hwc_context_t *ctx)
161{
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500162 if(ctx->mOverlay) {
163 delete ctx->mOverlay;
164 ctx->mOverlay = NULL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700165 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700166
Saurabh Shahacf10202013-02-26 10:15:15 -0800167 if(ctx->mRotMgr) {
168 delete ctx->mRotMgr;
169 ctx->mRotMgr = NULL;
170 }
171
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800172 for(int i = 0; i < MAX_DISPLAYS; i++) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800173 if(ctx->mCopyBit[i]) {
174 delete ctx->mCopyBit[i];
175 ctx->mCopyBit[i] = NULL;
176 }
177 }
178
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800179 if(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700180 close(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd);
181 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = -1;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700182 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700183
184 if(ctx->mExtDisplay) {
185 delete ctx->mExtDisplay;
186 ctx->mExtDisplay = NULL;
187 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400188
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800189 for(int i = 0; i < MAX_DISPLAYS; i++) {
Saurabh Shahcf053c62012-12-13 12:32:55 -0800190 if(ctx->mFBUpdate[i]) {
191 delete ctx->mFBUpdate[i];
192 ctx->mFBUpdate[i] = NULL;
193 }
Saurabh Shahacf10202013-02-26 10:15:15 -0800194 if(ctx->mVidOv[i]) {
195 delete ctx->mVidOv[i];
196 ctx->mVidOv[i] = NULL;
197 }
Saurabh Shahcf053c62012-12-13 12:32:55 -0800198 }
199
Saurabh Shahcbf7ccc2012-12-19 16:45:51 -0800200 if(ctx->mMDPComp) {
201 delete ctx->mMDPComp;
202 ctx->mMDPComp = NULL;
203 }
204
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400205 pthread_mutex_destroy(&(ctx->vstate.lock));
206 pthread_cond_destroy(&(ctx->vstate.cond));
Naseer Ahmed29a26812012-06-14 00:56:20 -0700207}
208
Naseer Ahmed1d183f52012-11-26 12:35:16 -0500209
210void dumpsys_log(android::String8& buf, const char* fmt, ...)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700211{
Naseer Ahmed1d183f52012-11-26 12:35:16 -0500212 va_list varargs;
213 va_start(varargs, fmt);
214 buf.appendFormatV(fmt, varargs);
215 va_end(varargs);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700216}
217
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800218/* Calculates the destination position based on the action safe rectangle */
219void getActionSafePosition(hwc_context_t *ctx, int dpy, uint32_t& x,
220 uint32_t& y, uint32_t& w, uint32_t& h) {
221
222 // if external supports underscan, do nothing
223 // it will be taken care in the driver
224 if(ctx->mExtDisplay->isCEUnderscanSupported())
225 return;
226
227 float wRatio = 1.0;
228 float hRatio = 1.0;
229 float xRatio = 1.0;
230 float yRatio = 1.0;
231
232 float fbWidth = ctx->dpyAttr[dpy].xres;
233 float fbHeight = ctx->dpyAttr[dpy].yres;
234
235 float asX = 0;
236 float asY = 0;
237 float asW = fbWidth;
238 float asH= fbHeight;
239 char value[PROPERTY_VALUE_MAX];
240
241 // Apply action safe parameters
242 property_get("hw.actionsafe.width", value, "0");
243 int asWidthRatio = atoi(value);
244 property_get("hw.actionsafe.height", value, "0");
245 int asHeightRatio = atoi(value);
246 // based on the action safe ratio, get the Action safe rectangle
247 asW = fbWidth * (1.0f - asWidthRatio / 100.0f);
248 asH = fbHeight * (1.0f - asHeightRatio / 100.0f);
249 asX = (fbWidth - asW) / 2;
250 asY = (fbHeight - asH) / 2;
251
252 // calculate the position ratio
253 xRatio = (float)x/fbWidth;
254 yRatio = (float)y/fbHeight;
255 wRatio = (float)w/fbWidth;
256 hRatio = (float)h/fbHeight;
257
258 //Calculate the position...
259 x = (xRatio * asW) + asX;
260 y = (yRatio * asH) + asY;
261 w = (wRatio * asW);
262 h = (hRatio * asH);
263
264 return;
265}
266
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800267bool needsScaling(hwc_layer_1_t const* layer) {
Naseer Ahmed018e5452012-12-03 14:46:15 -0500268 int dst_w, dst_h, src_w, src_h;
269
270 hwc_rect_t displayFrame = layer->displayFrame;
271 hwc_rect_t sourceCrop = layer->sourceCrop;
272
273 dst_w = displayFrame.right - displayFrame.left;
274 dst_h = displayFrame.bottom - displayFrame.top;
275
276 src_w = sourceCrop.right - sourceCrop.left;
277 src_h = sourceCrop.bottom - sourceCrop.top;
278
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800279 if(((src_w != dst_w) || (src_h != dst_h)))
280 return true;
281
282 return false;
283}
284
285bool isAlphaScaled(hwc_layer_1_t const* layer) {
286 if(needsScaling(layer)) {
Naseer Ahmed018e5452012-12-03 14:46:15 -0500287 if(layer->blending != HWC_BLENDING_NONE)
288 return true;
289 }
290 return false;
291}
292
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700293void setListStats(hwc_context_t *ctx,
294 const hwc_display_contents_1_t *list, int dpy) {
295
296 ctx->listStats[dpy].numAppLayers = list->numHwLayers - 1;
297 ctx->listStats[dpy].fbLayerIndex = list->numHwLayers - 1;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700298 ctx->listStats[dpy].skipCount = 0;
Naseer Ahmed018e5452012-12-03 14:46:15 -0500299 ctx->listStats[dpy].needsAlphaScale = false;
Jeykumar Sankarancf537002013-01-21 21:19:15 -0800300 ctx->listStats[dpy].yuvCount = 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700301
302 for (size_t i = 0; i < list->numHwLayers; i++) {
Naseer Ahmed018e5452012-12-03 14:46:15 -0500303 hwc_layer_1_t const* layer = &list->hwLayers[i];
304 private_handle_t *hnd = (private_handle_t *)layer->handle;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700305
Jeykumar Sankarancf537002013-01-21 21:19:15 -0800306 //reset stored yuv index
307 ctx->listStats[dpy].yuvIndices[i] = -1;
308
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700309 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
310 continue;
311 //We disregard FB being skip for now! so the else if
Saurabh Shah35712cb2012-09-14 10:28:18 -0700312 } else if (isSkipLayer(&list->hwLayers[i])) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700313 ctx->listStats[dpy].skipCount++;
Jeykumar Sankaran7535aba2013-02-04 11:03:09 -0800314 } else if (UNLIKELY(isYuvBuffer(hnd))) {
Jeykumar Sankarancf537002013-01-21 21:19:15 -0800315 int& yuvCount = ctx->listStats[dpy].yuvCount;
316 ctx->listStats[dpy].yuvIndices[yuvCount] = i;
317 yuvCount++;
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800318
Jeykumar Sankarana37fdbf2013-03-06 18:59:28 -0800319 if(layer->transform & HWC_TRANSFORM_ROT_90)
320 ctx->mNeedsRotator = true;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700321 }
Jeykumar Sankaran7535aba2013-02-04 11:03:09 -0800322
323 if(!ctx->listStats[dpy].needsAlphaScale)
324 ctx->listStats[dpy].needsAlphaScale = isAlphaScaled(layer);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700325 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700326}
327
Naseer Ahmed018e5452012-12-03 14:46:15 -0500328
Saurabh Shah541b59d2012-10-11 11:08:12 -0700329static inline void calc_cut(float& leftCutRatio, float& topCutRatio,
330 float& rightCutRatio, float& bottomCutRatio, int orient) {
Saurabh Shah27c1d652012-08-14 19:30:28 -0700331 if(orient & HAL_TRANSFORM_FLIP_H) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700332 swap(leftCutRatio, rightCutRatio);
Saurabh Shah27c1d652012-08-14 19:30:28 -0700333 }
334 if(orient & HAL_TRANSFORM_FLIP_V) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700335 swap(topCutRatio, bottomCutRatio);
Saurabh Shah27c1d652012-08-14 19:30:28 -0700336 }
337 if(orient & HAL_TRANSFORM_ROT_90) {
338 //Anti clock swapping
Saurabh Shah541b59d2012-10-11 11:08:12 -0700339 float tmpCutRatio = leftCutRatio;
340 leftCutRatio = topCutRatio;
341 topCutRatio = rightCutRatio;
342 rightCutRatio = bottomCutRatio;
343 bottomCutRatio = tmpCutRatio;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700344 }
345}
346
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500347bool isSecuring(hwc_context_t* ctx) {
348 if((ctx->mMDP.version < qdutils::MDSS_V5) &&
349 (ctx->mMDP.version > qdutils::MDP_V3_0) &&
350 ctx->mSecuring) {
351 return true;
352 }
353 return false;
354}
355
Sushil Chauhan2515abf2013-01-08 16:40:05 -0800356bool isSecureModePolicy(int mdpVersion) {
357 if (mdpVersion < qdutils::MDSS_V5)
358 return true;
359 else
360 return false;
361}
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500362
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700363//Crops source buffer against destination and FB boundaries
364void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800365 const hwc_rect_t& scissor, int orient) {
366
Saurabh Shah27c1d652012-08-14 19:30:28 -0700367 int& crop_l = crop.left;
368 int& crop_t = crop.top;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700369 int& crop_r = crop.right;
370 int& crop_b = crop.bottom;
371 int crop_w = crop.right - crop.left;
372 int crop_h = crop.bottom - crop.top;
373
Saurabh Shah27c1d652012-08-14 19:30:28 -0700374 int& dst_l = dst.left;
375 int& dst_t = dst.top;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700376 int& dst_r = dst.right;
377 int& dst_b = dst.bottom;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700378 int dst_w = abs(dst.right - dst.left);
379 int dst_h = abs(dst.bottom - dst.top);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700380
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800381 const int& sci_l = scissor.left;
382 const int& sci_t = scissor.top;
383 const int& sci_r = scissor.right;
384 const int& sci_b = scissor.bottom;
385 int sci_w = abs(sci_r - sci_l);
386 int sci_h = abs(sci_b - sci_t);
387
Saurabh Shah541b59d2012-10-11 11:08:12 -0700388 float leftCutRatio = 0.0f, rightCutRatio = 0.0f, topCutRatio = 0.0f,
389 bottomCutRatio = 0.0f;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700390
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800391 if(dst_l < sci_l) {
392 leftCutRatio = (float)(sci_l - dst_l) / (float)dst_w;
393 dst_l = sci_l;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700394 }
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800395
396 if(dst_r > sci_r) {
397 rightCutRatio = (float)(dst_r - sci_r) / (float)dst_w;
398 dst_r = sci_r;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700399 }
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800400
401 if(dst_t < sci_t) {
402 topCutRatio = (float)(sci_t - dst_t) / (float)dst_h;
403 dst_t = sci_t;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700404 }
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800405
406 if(dst_b > sci_b) {
407 bottomCutRatio = (float)(dst_b - sci_b) / (float)dst_h;
408 dst_b = sci_b;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700409 }
Saurabh Shah27c1d652012-08-14 19:30:28 -0700410
Saurabh Shah541b59d2012-10-11 11:08:12 -0700411 calc_cut(leftCutRatio, topCutRatio, rightCutRatio, bottomCutRatio, orient);
412 crop_l += crop_w * leftCutRatio;
413 crop_t += crop_h * topCutRatio;
414 crop_r -= crop_w * rightCutRatio;
415 crop_b -= crop_h * bottomCutRatio;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700416}
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700417
Naseer Ahmed64b81212013-02-14 10:29:47 -0500418void getNonWormholeRegion(hwc_display_contents_1_t* list,
419 hwc_rect_t& nwr)
420{
421 uint32_t last = list->numHwLayers - 1;
422 hwc_rect_t fbDisplayFrame = list->hwLayers[last].displayFrame;
423 //Initiliaze nwr to first frame
424 nwr.left = list->hwLayers[0].displayFrame.left;
425 nwr.top = list->hwLayers[0].displayFrame.top;
426 nwr.right = list->hwLayers[0].displayFrame.right;
427 nwr.bottom = list->hwLayers[0].displayFrame.bottom;
428
429 for (uint32_t i = 1; i < last; i++) {
430 hwc_rect_t displayFrame = list->hwLayers[i].displayFrame;
431 nwr.left = min(nwr.left, displayFrame.left);
432 nwr.top = min(nwr.top, displayFrame.top);
433 nwr.right = max(nwr.right, displayFrame.right);
434 nwr.bottom = max(nwr.bottom, displayFrame.bottom);
435 }
436
437 //Intersect with the framebuffer
438 nwr.left = max(nwr.left, fbDisplayFrame.left);
439 nwr.top = max(nwr.top, fbDisplayFrame.top);
440 nwr.right = min(nwr.right, fbDisplayFrame.right);
441 nwr.bottom = min(nwr.bottom, fbDisplayFrame.bottom);
442
443}
444
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700445bool isExternalActive(hwc_context_t* ctx) {
446 return ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive;
Saurabh Shahfc2acbe2012-08-17 19:47:52 -0700447}
448
Saurabh Shah747af1e2013-02-26 10:25:12 -0800449void closeAcquireFds(hwc_display_contents_1_t* list) {
450 for(uint32_t i = 0; list && i < list->numHwLayers; i++) {
451 //Close the acquireFenceFds
452 //HWC_FRAMEBUFFER are -1 already by SF, rest we close.
453 if(list->hwLayers[i].acquireFenceFd >= 0) {
454 close(list->hwLayers[i].acquireFenceFd);
455 list->hwLayers[i].acquireFenceFd = -1;
456 }
457 }
458}
459
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800460int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy,
461 int fd) {
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700462 int ret = 0;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700463 struct mdp_buf_sync data;
Naseer Ahmedb1c76322012-10-17 00:32:50 -0400464 int acquireFd[MAX_NUM_LAYERS];
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700465 int count = 0;
466 int releaseFd = -1;
467 int fbFd = -1;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800468 memset(&data, 0, sizeof(data));
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500469 bool swapzero = false;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700470 data.flags = MDP_BUF_SYNC_FLAG_WAIT;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700471 data.acq_fen_fd = acquireFd;
472 data.rel_fen_fd = &releaseFd;
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500473 char property[PROPERTY_VALUE_MAX];
474 if(property_get("debug.egl.swapinterval", property, "1") > 0) {
475 if(atoi(property) == 0)
476 swapzero = true;
477 }
478
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700479 //Accumulate acquireFenceFds
480 for(uint32_t i = 0; i < list->numHwLayers; i++) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800481 if(list->hwLayers[i].compositionType == HWC_OVERLAY &&
482 list->hwLayers[i].acquireFenceFd != -1) {
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500483 if(UNLIKELY(swapzero))
484 acquireFd[count++] = -1;
485 else
486 acquireFd[count++] = list->hwLayers[i].acquireFenceFd;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700487 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800488 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
489 if(UNLIKELY(swapzero))
490 acquireFd[count++] = -1;
491 else if(fd != -1) {
492 //set the acquireFD from fd - which is coming from c2d
493 acquireFd[count++] = fd;
494 // Buffer sync IOCTL should be async when using c2d fence is
495 // used
496 data.flags &= ~MDP_BUF_SYNC_FLAG_WAIT;
497 } else if(list->hwLayers[i].acquireFenceFd != -1)
498 acquireFd[count++] = list->hwLayers[i].acquireFenceFd;
499 }
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700500 }
501
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700502 data.acq_fen_fd_cnt = count;
503 fbFd = ctx->dpyAttr[dpy].fd;
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700504 //Waits for acquire fences, returns a release fence
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800505 if(LIKELY(!swapzero)) {
506 uint64_t start = systemTime();
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500507 ret = ioctl(fbFd, MSMFB_BUFFER_SYNC, &data);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800508 ALOGD_IF(HWC_UTILS_DEBUG, "%s: time taken for MSMFB_BUFFER_SYNC IOCTL = %d",
509 __FUNCTION__, (size_t) ns2ms(systemTime() - start));
510 }
Saurabh Shah747af1e2013-02-26 10:25:12 -0800511
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700512 if(ret < 0) {
513 ALOGE("ioctl MSMFB_BUFFER_SYNC failed, err=%s",
514 strerror(errno));
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700515 }
Saurabh Shah747af1e2013-02-26 10:25:12 -0800516
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700517 for(uint32_t i = 0; i < list->numHwLayers; i++) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800518 if(list->hwLayers[i].compositionType == HWC_OVERLAY ||
519 list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700520 //Populate releaseFenceFds.
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500521 if(UNLIKELY(swapzero))
522 list->hwLayers[i].releaseFenceFd = -1;
523 else
524 list->hwLayers[i].releaseFenceFd = dup(releaseFd);
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700525 }
526 }
Saurabh Shah747af1e2013-02-26 10:25:12 -0800527
528 if(fd >= 0) {
529 close(fd);
530 fd = -1;
531 }
532
Naseer Ahmed64b81212013-02-14 10:29:47 -0500533 if (ctx->mCopyBit[dpy])
534 ctx->mCopyBit[dpy]->setReleaseFd(releaseFd);
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500535 if(UNLIKELY(swapzero)){
536 list->retireFenceFd = -1;
537 close(releaseFd);
538 } else {
539 list->retireFenceFd = releaseFd;
540 }
Saurabh Shah747af1e2013-02-26 10:25:12 -0800541
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700542 return ret;
543}
544
Saurabh Shahacf10202013-02-26 10:15:15 -0800545void trimLayer(hwc_context_t *ctx, const int& dpy, const int& transform,
546 hwc_rect_t& crop, hwc_rect_t& dst) {
547 int hw_w = ctx->dpyAttr[dpy].xres;
548 int hw_h = ctx->dpyAttr[dpy].yres;
549 if(dst.left < 0 || dst.top < 0 ||
550 dst.right > hw_w || dst.bottom > hw_h) {
551 hwc_rect_t scissor = {0, 0, hw_w, hw_h };
552 qhwc::calculate_crop_rects(crop, dst, scissor, transform);
553 }
554}
555
556void setMdpFlags(hwc_layer_1_t *layer,
557 ovutils::eMdpFlags &mdpFlags,
558 int rotDownscale) {
559 private_handle_t *hnd = (private_handle_t *)layer->handle;
560 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
561 const int& transform = layer->transform;
562
563 if(layer->blending == HWC_BLENDING_PREMULT) {
564 ovutils::setMdpFlags(mdpFlags,
565 ovutils::OV_MDP_BLEND_FG_PREMULT);
566 }
567
568 if(isYuvBuffer(hnd)) {
569 if(isSecureBuffer(hnd)) {
570 ovutils::setMdpFlags(mdpFlags,
571 ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
572 }
573 if(metadata && (metadata->operation & PP_PARAM_INTERLACED) &&
574 metadata->interlaced) {
575 ovutils::setMdpFlags(mdpFlags,
576 ovutils::OV_MDP_DEINTERLACE);
577 }
578 //Pre-rotation will be used using rotator.
579 if(transform & HWC_TRANSFORM_ROT_90) {
580 ovutils::setMdpFlags(mdpFlags,
581 ovutils::OV_MDP_SOURCE_ROTATED_90);
582 }
583 }
584
585 //No 90 component and no rot-downscale then flips done by MDP
586 //If we use rot then it might as well do flips
587 if(!(layer->transform & HWC_TRANSFORM_ROT_90) && !rotDownscale) {
588 if(layer->transform & HWC_TRANSFORM_FLIP_H) {
589 ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_FLIP_H);
590 }
591
592 if(layer->transform & HWC_TRANSFORM_FLIP_V) {
593 ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_FLIP_V);
594 }
595 }
596}
597
598static inline int configRotator(Rotator *rot, const Whf& whf,
599 const eMdpFlags& mdpFlags, const eTransform& orient,
600 const int& downscale) {
601 rot->setSource(whf);
602 rot->setFlags(mdpFlags);
603 rot->setTransform(orient);
604 rot->setDownscale(downscale);
605 if(!rot->commit()) return -1;
606 return 0;
607}
608
609static inline int configMdp(Overlay *ov, const PipeArgs& parg,
610 const eTransform& orient, const hwc_rect_t& crop,
611 const hwc_rect_t& pos, const eDest& dest) {
612 ov->setSource(parg, dest);
613 ov->setTransform(orient, dest);
614
615 int crop_w = crop.right - crop.left;
616 int crop_h = crop.bottom - crop.top;
617 Dim dcrop(crop.left, crop.top, crop_w, crop_h);
618 ov->setCrop(dcrop, dest);
619
620 int posW = pos.right - pos.left;
621 int posH = pos.bottom - pos.top;
622 Dim position(pos.left, pos.top, posW, posH);
623 ov->setPosition(position, dest);
624
625 if (!ov->commit(dest)) {
626 return -1;
627 }
628 return 0;
629}
630
631static inline void updateSource(eTransform& orient, Whf& whf,
632 hwc_rect_t& crop) {
633 Dim srcCrop(crop.left, crop.top,
634 crop.right - crop.left,
635 crop.bottom - crop.top);
Saurabh Shah76fd6552013-03-14 14:45:38 -0700636 //getMdpOrient will switch the flips if the source is 90 rotated.
637 //Clients in Android dont factor in 90 rotation while deciding the flip.
638 orient = static_cast<eTransform>(ovutils::getMdpOrient(orient));
Saurabh Shahacf10202013-02-26 10:15:15 -0800639 preRotateSource(orient, whf, srcCrop);
640 crop.left = srcCrop.x;
641 crop.top = srcCrop.y;
642 crop.right = srcCrop.x + srcCrop.w;
643 crop.bottom = srcCrop.y + srcCrop.h;
644}
645
646int configureLowRes(hwc_context_t *ctx, hwc_layer_1_t *layer,
647 const int& dpy, eMdpFlags& mdpFlags, const eZorder& z,
648 const eIsFg& isFg, const eDest& dest, Rotator **rot) {
649
650 private_handle_t *hnd = (private_handle_t *)layer->handle;
651 if(!hnd) {
652 ALOGE("%s: layer handle is NULL", __FUNCTION__);
653 return -1;
654 }
655
656 hwc_rect_t crop = layer->sourceCrop;
657 hwc_rect_t dst = layer->displayFrame;
658 int transform = layer->transform;
659 eTransform orient = static_cast<eTransform>(transform);
660 int downscale = 0;
661 int rotFlags = ovutils::ROT_FLAGS_NONE;
662 Whf whf(hnd->width, hnd->height,
663 getMdpFormat(hnd->format), hnd->size);
664
665 if(isYuvBuffer(hnd) && ctx->mMDP.version >= qdutils::MDP_V4_2 &&
666 ctx->mMDP.version < qdutils::MDSS_V5) {
667 downscale = getDownscaleFactor(
668 crop.right - crop.left,
669 crop.bottom - crop.top,
670 dst.right - dst.left,
671 dst.bottom - dst.top);
672 if(downscale) {
673 rotFlags = ROT_DOWNSCALE_ENABLED;
674 }
675 }
676
677 setMdpFlags(layer, mdpFlags, downscale);
678 trimLayer(ctx, dpy, transform, crop, dst);
679
680 if(isYuvBuffer(hnd) && //if 90 component or downscale, use rot
681 ((transform & HWC_TRANSFORM_ROT_90) || downscale)) {
682 *rot = ctx->mRotMgr->getNext();
683 if(*rot == NULL) return -1;
684 //Configure rotator for pre-rotation
685 if(configRotator(*rot, whf, mdpFlags, orient, downscale) < 0)
686 return -1;
687 whf.format = (*rot)->getDstFormat();
688 updateSource(orient, whf, crop);
Saurabh Shahacf10202013-02-26 10:15:15 -0800689 rotFlags |= ovutils::ROT_PREROTATED;
690 }
691
Saurabh Shah76fd6552013-03-14 14:45:38 -0700692 //For the mdp, since either we are pre-rotating or MDP does flips
693 orient = OVERLAY_TRANSFORM_0;
694 transform = 0;
695
Saurabh Shahacf10202013-02-26 10:15:15 -0800696 PipeArgs parg(mdpFlags, whf, z, isFg, static_cast<eRotFlags>(rotFlags));
697 if(configMdp(ctx->mOverlay, parg, orient, crop, dst, dest) < 0) {
698 ALOGE("%s: commit failed for low res panel", __FUNCTION__);
699 return -1;
700 }
701 return 0;
702}
703
704int configureHighRes(hwc_context_t *ctx, hwc_layer_1_t *layer,
705 const int& dpy, eMdpFlags& mdpFlagsL, const eZorder& z,
706 const eIsFg& isFg, const eDest& lDest, const eDest& rDest,
707 Rotator **rot) {
708 private_handle_t *hnd = (private_handle_t *)layer->handle;
709 if(!hnd) {
710 ALOGE("%s: layer handle is NULL", __FUNCTION__);
711 return -1;
712 }
713
714 int hw_w = ctx->dpyAttr[dpy].xres;
715 int hw_h = ctx->dpyAttr[dpy].yres;
716 hwc_rect_t crop = layer->sourceCrop;
717 hwc_rect_t dst = layer->displayFrame;
718 int transform = layer->transform;
719 eTransform orient = static_cast<eTransform>(transform);
720 const int downscale = 0;
721 int rotFlags = ROT_FLAGS_NONE;
722
723 Whf whf(hnd->width, hnd->height,
724 getMdpFormat(hnd->format), hnd->size);
725
726 setMdpFlags(layer, mdpFlagsL);
727 trimLayer(ctx, dpy, transform, crop, dst);
728
729 if(isYuvBuffer(hnd) && (transform & HWC_TRANSFORM_ROT_90)) {
730 (*rot) = ctx->mRotMgr->getNext();
731 if((*rot) == NULL) return -1;
732 //Configure rotator for pre-rotation
733 if(configRotator(*rot, whf, mdpFlagsL, orient, downscale) < 0)
734 return -1;
735 whf.format = (*rot)->getDstFormat();
736 updateSource(orient, whf, crop);
Saurabh Shahacf10202013-02-26 10:15:15 -0800737 rotFlags |= ROT_PREROTATED;
738 }
739
740 eMdpFlags mdpFlagsR = mdpFlagsL;
741 setMdpFlags(mdpFlagsR, OV_MDSS_MDP_RIGHT_MIXER);
742
743 hwc_rect_t tmp_cropL, tmp_dstL;
744 hwc_rect_t tmp_cropR, tmp_dstR;
745
746 if(lDest != OV_INVALID) {
747 tmp_cropL = crop;
748 tmp_dstL = dst;
749 hwc_rect_t scissor = {0, 0, hw_w/2, hw_h };
750 qhwc::calculate_crop_rects(tmp_cropL, tmp_dstL, scissor, 0);
751 }
752 if(rDest != OV_INVALID) {
753 tmp_cropR = crop;
754 tmp_dstR = dst;
755 hwc_rect_t scissor = {hw_w/2, 0, hw_w, hw_h };
756 qhwc::calculate_crop_rects(tmp_cropR, tmp_dstR, scissor, 0);
757 }
758
759 //When buffer is flipped, contents of mixer config also needs to swapped.
760 //Not needed if the layer is confined to one half of the screen.
761 //If rotator has been used then it has also done the flips, so ignore them.
Saurabh Shah76fd6552013-03-14 14:45:38 -0700762 if((orient & OVERLAY_TRANSFORM_FLIP_V) && lDest != OV_INVALID
Saurabh Shahacf10202013-02-26 10:15:15 -0800763 && rDest != OV_INVALID && rot == NULL) {
764 hwc_rect_t new_cropR;
765 new_cropR.left = tmp_cropL.left;
766 new_cropR.right = new_cropR.left + (tmp_cropR.right - tmp_cropR.left);
767
768 hwc_rect_t new_cropL;
769 new_cropL.left = new_cropR.right;
770 new_cropL.right = tmp_cropR.right;
771
772 tmp_cropL.left = new_cropL.left;
773 tmp_cropL.right = new_cropL.right;
774
775 tmp_cropR.left = new_cropR.left;
776 tmp_cropR.right = new_cropR.right;
777
778 }
779
Saurabh Shah76fd6552013-03-14 14:45:38 -0700780 //For the mdp, since either we are pre-rotating or MDP does flips
781 orient = OVERLAY_TRANSFORM_0;
782 transform = 0;
783
Saurabh Shahacf10202013-02-26 10:15:15 -0800784 //configure left mixer
785 if(lDest != OV_INVALID) {
786 PipeArgs pargL(mdpFlagsL, whf, z, isFg,
787 static_cast<eRotFlags>(rotFlags));
788 if(configMdp(ctx->mOverlay, pargL, orient,
789 tmp_cropL, tmp_dstL, lDest) < 0) {
790 ALOGE("%s: commit failed for left mixer config", __FUNCTION__);
791 return -1;
792 }
793 }
794
795 //configure right mixer
796 if(rDest != OV_INVALID) {
797 PipeArgs pargR(mdpFlagsR, whf, z, isFg,
798 static_cast<eRotFlags>(rotFlags));
Arun Kumar K.R0e8efb82013-03-18 17:31:50 -0700799 tmp_dstR.right = tmp_dstR.right - tmp_dstR.left;
800 tmp_dstR.left = 0;
Saurabh Shahacf10202013-02-26 10:15:15 -0800801 if(configMdp(ctx->mOverlay, pargR, orient,
802 tmp_cropR, tmp_dstR, rDest) < 0) {
803 ALOGE("%s: commit failed for right mixer config", __FUNCTION__);
804 return -1;
805 }
806 }
807
808 return 0;
809}
810
Naseer Ahmedb1c76322012-10-17 00:32:50 -0400811void LayerCache::resetLayerCache(int num) {
812 for(uint32_t i = 0; i < MAX_NUM_LAYERS; i++) {
813 hnd[i] = NULL;
814 }
815 numHwLayers = num;
816}
817
818void LayerCache::updateLayerCache(hwc_display_contents_1_t* list) {
819
820 int numFbLayers = 0;
821 int numCacheableLayers = 0;
822
823 canUseLayerCache = false;
824 //Bail if geometry changed or num of layers changed
825 if(list->flags & HWC_GEOMETRY_CHANGED ||
826 list->numHwLayers != numHwLayers ) {
827 resetLayerCache(list->numHwLayers);
828 return;
829 }
830
831 for(uint32_t i = 0; i < list->numHwLayers; i++) {
832 //Bail on skip layers
833 if(list->hwLayers[i].flags & HWC_SKIP_LAYER) {
834 resetLayerCache(list->numHwLayers);
835 return;
836 }
837
838 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER) {
839 numFbLayers++;
840 if(hnd[i] == NULL) {
841 hnd[i] = list->hwLayers[i].handle;
842 } else if (hnd[i] ==
843 list->hwLayers[i].handle) {
844 numCacheableLayers++;
845 } else {
846 hnd[i] = NULL;
847 return;
848 }
849 } else {
850 hnd[i] = NULL;
851 }
852 }
853 if(numFbLayers == numCacheableLayers)
854 canUseLayerCache = true;
855
856 //XXX: The marking part is separate, if MDP comp wants
857 // to use it in the future. Right now getting MDP comp
858 // to use this is more trouble than it is worth.
859 markCachedLayersAsOverlay(list);
860}
861
862void LayerCache::markCachedLayersAsOverlay(hwc_display_contents_1_t* list) {
863 //This optimization only works if ALL the layer handles
864 //that were on the framebuffer didn't change.
865 if(canUseLayerCache){
866 for(uint32_t i = 0; i < list->numHwLayers; i++) {
867 if (list->hwLayers[i].handle &&
868 list->hwLayers[i].handle == hnd[i] &&
869 list->hwLayers[i].compositionType != HWC_FRAMEBUFFER_TARGET)
870 {
871 list->hwLayers[i].compositionType = HWC_OVERLAY;
872 }
873 }
874 }
Naseer Ahmedb1c76322012-10-17 00:32:50 -0400875}
876
Saurabh Shahacf10202013-02-26 10:15:15 -0800877};//namespace qhwc