blob: 11f7dc571964dd967b86e8687c9d04b5aca94c12 [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>
Saurabh Shah86c17292013-02-08 15:24:13 -080022#include <binder/IServiceManager.h>
Naseer Ahmed5b6708a2012-08-02 13:46:08 -070023#include <EGL/egl.h>
Saurabh Shahfc2acbe2012-08-17 19:47:52 -070024#include <cutils/properties.h>
25#include <gralloc_priv.h>
26#include <fb_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
49// Opens Framebuffer device
50static void openFramebufferDevice(hwc_context_t *ctx)
51{
52 hw_module_t const *module;
53 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
54 framebuffer_open(module, &(ctx->mFbDev));
Saurabh Shah3e858eb2012-09-17 16:53:21 -070055 private_module_t* m = reinterpret_cast<private_module_t*>(
56 ctx->mFbDev->common.module);
57 //xres, yres may not be 32 aligned
Naseer Ahmed54821fe2012-11-28 18:44:38 -050058 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].stride = m->finfo.line_length /
59 (m->info.xres/8);
Saurabh Shah3e858eb2012-09-17 16:53:21 -070060 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres = m->info.xres;
61 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].yres = m->info.yres;
62 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xdpi = ctx->mFbDev->xdpi;
63 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].ydpi = ctx->mFbDev->ydpi;
64 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period =
65 1000000000l / ctx->mFbDev->fps;
66 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = openFb(HWC_DISPLAY_PRIMARY);
Naseer Ahmed72cf9762012-07-21 12:17:13 -070067 }
68}
69
Naseer Ahmed29a26812012-06-14 00:56:20 -070070void initContext(hwc_context_t *ctx)
71{
Naseer Ahmed72cf9762012-07-21 12:17:13 -070072 openFramebufferDevice(ctx);
Naseer Ahmed96c4c952012-07-25 18:27:14 -070073 ctx->mMDP.version = qdutils::MDPVersion::getInstance().getMDPVersion();
74 ctx->mMDP.hasOverlay = qdutils::MDPVersion::getInstance().hasOverlay();
75 ctx->mMDP.panel = qdutils::MDPVersion::getInstance().getPanelType();
Sushil Chauhan07a2c762013-03-06 15:36:49 -080076 overlay::Overlay::initOverlay();
77 ctx->mOverlay = overlay::Overlay::getInstance();
78 ctx->mRotMgr = new RotMgr();
79
Saurabh Shahcf053c62012-12-13 12:32:55 -080080 //Is created and destroyed only once for primary
81 //For external it could get created and destroyed multiple times depending
82 //on what external we connect to.
83 ctx->mFBUpdate[HWC_DISPLAY_PRIMARY] =
84 IFBUpdate::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres,
85 HWC_DISPLAY_PRIMARY);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080086
Saurabh Shahacf10202013-02-26 10:15:15 -080087 ctx->mVidOv[HWC_DISPLAY_PRIMARY] =
88 IVideoOverlay::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres,
89 HWC_DISPLAY_PRIMARY);
90
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080091 char value[PROPERTY_VALUE_MAX];
92 // Check if the target supports copybit compostion (dyn/mdp/c2d) to
93 // decide if we need to open the copybit module.
94 int compositionType =
95 qdutils::QCCompositionType::getInstance().getCompositionType();
96
97 if (compositionType & (qdutils::COMPOSITION_TYPE_DYN |
98 qdutils::COMPOSITION_TYPE_MDP |
99 qdutils::COMPOSITION_TYPE_C2D)) {
100 ctx->mCopyBit[HWC_DISPLAY_PRIMARY] = new CopyBit();
101 }
102
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700103 ctx->mExtDisplay = new ExternalDisplay(ctx);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800104 for (uint32_t i = 0; i < MAX_DISPLAYS; i++)
Naseer Ahmede78f0522012-12-07 18:24:28 -0500105 ctx->mLayerCache[i] = new LayerCache();
Saurabh Shahcbf7ccc2012-12-19 16:45:51 -0800106 ctx->mMDPComp = MDPComp::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres);
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500107 MDPComp::init(ctx);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700108
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400109 pthread_mutex_init(&(ctx->vstate.lock), NULL);
110 pthread_cond_init(&(ctx->vstate.cond), NULL);
111 ctx->vstate.enable = false;
Naseer Ahmed56601cd2013-03-05 11:34:14 -0500112 ctx->vstate.fakevsync = false;
Amara Venkata Mastan Manoj Kumar75526f52012-12-27 18:27:01 -0800113 ctx->mExtDispConfiguring = false;
Saurabh Shah86c17292013-02-08 15:24:13 -0800114
115 //Right now hwc starts the service but anybody could do it, or it could be
116 //independent process as well.
117 QService::init();
118 sp<IQClient> client = new QClient(ctx);
119 interface_cast<IQService>(
120 defaultServiceManager()->getService(
121 String16("display.qservice")))->connect(client);
122
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700123 ALOGI("Initializing Qualcomm Hardware Composer");
Naseer Ahmed96c4c952012-07-25 18:27:14 -0700124 ALOGI("MDP version: %d", ctx->mMDP.version);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700125}
126
127void closeContext(hwc_context_t *ctx)
128{
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500129 if(ctx->mOverlay) {
130 delete ctx->mOverlay;
131 ctx->mOverlay = NULL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700132 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700133
Saurabh Shahacf10202013-02-26 10:15:15 -0800134 if(ctx->mRotMgr) {
135 delete ctx->mRotMgr;
136 ctx->mRotMgr = NULL;
137 }
138
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800139 for(int i = 0; i < MAX_DISPLAYS; i++) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800140 if(ctx->mCopyBit[i]) {
141 delete ctx->mCopyBit[i];
142 ctx->mCopyBit[i] = NULL;
143 }
144 }
145
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700146 if(ctx->mFbDev) {
147 framebuffer_close(ctx->mFbDev);
148 ctx->mFbDev = NULL;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700149 close(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd);
150 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = -1;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700151 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700152
153 if(ctx->mExtDisplay) {
154 delete ctx->mExtDisplay;
155 ctx->mExtDisplay = NULL;
156 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400157
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800158 for(int i = 0; i < MAX_DISPLAYS; i++) {
Saurabh Shahcf053c62012-12-13 12:32:55 -0800159 if(ctx->mFBUpdate[i]) {
160 delete ctx->mFBUpdate[i];
161 ctx->mFBUpdate[i] = NULL;
162 }
Saurabh Shahacf10202013-02-26 10:15:15 -0800163 if(ctx->mVidOv[i]) {
164 delete ctx->mVidOv[i];
165 ctx->mVidOv[i] = NULL;
166 }
Saurabh Shahcf053c62012-12-13 12:32:55 -0800167 }
168
Saurabh Shahcbf7ccc2012-12-19 16:45:51 -0800169 if(ctx->mMDPComp) {
170 delete ctx->mMDPComp;
171 ctx->mMDPComp = NULL;
172 }
173
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400174 pthread_mutex_destroy(&(ctx->vstate.lock));
175 pthread_cond_destroy(&(ctx->vstate.cond));
Naseer Ahmed29a26812012-06-14 00:56:20 -0700176}
177
Naseer Ahmed1d183f52012-11-26 12:35:16 -0500178
179void dumpsys_log(android::String8& buf, const char* fmt, ...)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700180{
Naseer Ahmed1d183f52012-11-26 12:35:16 -0500181 va_list varargs;
182 va_start(varargs, fmt);
183 buf.appendFormatV(fmt, varargs);
184 va_end(varargs);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700185}
186
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800187/* Calculates the destination position based on the action safe rectangle */
188void getActionSafePosition(hwc_context_t *ctx, int dpy, uint32_t& x,
189 uint32_t& y, uint32_t& w, uint32_t& h) {
190
191 // if external supports underscan, do nothing
192 // it will be taken care in the driver
193 if(ctx->mExtDisplay->isCEUnderscanSupported())
194 return;
195
196 float wRatio = 1.0;
197 float hRatio = 1.0;
198 float xRatio = 1.0;
199 float yRatio = 1.0;
200
201 float fbWidth = ctx->dpyAttr[dpy].xres;
202 float fbHeight = ctx->dpyAttr[dpy].yres;
203
204 float asX = 0;
205 float asY = 0;
206 float asW = fbWidth;
207 float asH= fbHeight;
208 char value[PROPERTY_VALUE_MAX];
209
210 // Apply action safe parameters
211 property_get("hw.actionsafe.width", value, "0");
212 int asWidthRatio = atoi(value);
213 property_get("hw.actionsafe.height", value, "0");
214 int asHeightRatio = atoi(value);
215 // based on the action safe ratio, get the Action safe rectangle
216 asW = fbWidth * (1.0f - asWidthRatio / 100.0f);
217 asH = fbHeight * (1.0f - asHeightRatio / 100.0f);
218 asX = (fbWidth - asW) / 2;
219 asY = (fbHeight - asH) / 2;
220
221 // calculate the position ratio
222 xRatio = (float)x/fbWidth;
223 yRatio = (float)y/fbHeight;
224 wRatio = (float)w/fbWidth;
225 hRatio = (float)h/fbHeight;
226
227 //Calculate the position...
228 x = (xRatio * asW) + asX;
229 y = (yRatio * asH) + asY;
230 w = (wRatio * asW);
231 h = (hRatio * asH);
232
233 return;
234}
235
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800236bool needsScaling(hwc_layer_1_t const* layer) {
Naseer Ahmed018e5452012-12-03 14:46:15 -0500237 int dst_w, dst_h, src_w, src_h;
238
239 hwc_rect_t displayFrame = layer->displayFrame;
240 hwc_rect_t sourceCrop = layer->sourceCrop;
241
242 dst_w = displayFrame.right - displayFrame.left;
243 dst_h = displayFrame.bottom - displayFrame.top;
244
245 src_w = sourceCrop.right - sourceCrop.left;
246 src_h = sourceCrop.bottom - sourceCrop.top;
247
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800248 if(((src_w != dst_w) || (src_h != dst_h)))
249 return true;
250
251 return false;
252}
253
254bool isAlphaScaled(hwc_layer_1_t const* layer) {
255 if(needsScaling(layer)) {
Naseer Ahmed018e5452012-12-03 14:46:15 -0500256 if(layer->blending != HWC_BLENDING_NONE)
257 return true;
258 }
259 return false;
260}
261
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700262void setListStats(hwc_context_t *ctx,
263 const hwc_display_contents_1_t *list, int dpy) {
264
265 ctx->listStats[dpy].numAppLayers = list->numHwLayers - 1;
266 ctx->listStats[dpy].fbLayerIndex = list->numHwLayers - 1;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700267 ctx->listStats[dpy].skipCount = 0;
Naseer Ahmed018e5452012-12-03 14:46:15 -0500268 ctx->listStats[dpy].needsAlphaScale = false;
Jeykumar Sankarancf537002013-01-21 21:19:15 -0800269 ctx->listStats[dpy].yuvCount = 0;
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800270 ctx->mDMAInUse = false;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700271
272 for (size_t i = 0; i < list->numHwLayers; i++) {
Naseer Ahmed018e5452012-12-03 14:46:15 -0500273 hwc_layer_1_t const* layer = &list->hwLayers[i];
274 private_handle_t *hnd = (private_handle_t *)layer->handle;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700275
Jeykumar Sankarancf537002013-01-21 21:19:15 -0800276 //reset stored yuv index
277 ctx->listStats[dpy].yuvIndices[i] = -1;
278
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700279 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
280 continue;
281 //We disregard FB being skip for now! so the else if
Saurabh Shah35712cb2012-09-14 10:28:18 -0700282 } else if (isSkipLayer(&list->hwLayers[i])) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700283 ctx->listStats[dpy].skipCount++;
Jeykumar Sankaran7535aba2013-02-04 11:03:09 -0800284 } else if (UNLIKELY(isYuvBuffer(hnd))) {
Jeykumar Sankarancf537002013-01-21 21:19:15 -0800285 int& yuvCount = ctx->listStats[dpy].yuvCount;
286 ctx->listStats[dpy].yuvIndices[yuvCount] = i;
287 yuvCount++;
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800288
289 if((layer->transform & HWC_TRANSFORM_ROT_90) && !ctx->mDMAInUse)
290 ctx->mDMAInUse = true;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700291 }
Jeykumar Sankaran7535aba2013-02-04 11:03:09 -0800292
293 if(!ctx->listStats[dpy].needsAlphaScale)
294 ctx->listStats[dpy].needsAlphaScale = isAlphaScaled(layer);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700295 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700296}
297
Naseer Ahmed018e5452012-12-03 14:46:15 -0500298
Saurabh Shah541b59d2012-10-11 11:08:12 -0700299static inline void calc_cut(float& leftCutRatio, float& topCutRatio,
300 float& rightCutRatio, float& bottomCutRatio, int orient) {
Saurabh Shah27c1d652012-08-14 19:30:28 -0700301 if(orient & HAL_TRANSFORM_FLIP_H) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700302 swap(leftCutRatio, rightCutRatio);
Saurabh Shah27c1d652012-08-14 19:30:28 -0700303 }
304 if(orient & HAL_TRANSFORM_FLIP_V) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700305 swap(topCutRatio, bottomCutRatio);
Saurabh Shah27c1d652012-08-14 19:30:28 -0700306 }
307 if(orient & HAL_TRANSFORM_ROT_90) {
308 //Anti clock swapping
Saurabh Shah541b59d2012-10-11 11:08:12 -0700309 float tmpCutRatio = leftCutRatio;
310 leftCutRatio = topCutRatio;
311 topCutRatio = rightCutRatio;
312 rightCutRatio = bottomCutRatio;
313 bottomCutRatio = tmpCutRatio;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700314 }
315}
316
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500317bool isSecuring(hwc_context_t* ctx) {
318 if((ctx->mMDP.version < qdutils::MDSS_V5) &&
319 (ctx->mMDP.version > qdutils::MDP_V3_0) &&
320 ctx->mSecuring) {
321 return true;
322 }
323 return false;
324}
325
Sushil Chauhan2515abf2013-01-08 16:40:05 -0800326bool isSecureModePolicy(int mdpVersion) {
327 if (mdpVersion < qdutils::MDSS_V5)
328 return true;
329 else
330 return false;
331}
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500332
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700333//Crops source buffer against destination and FB boundaries
334void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800335 const hwc_rect_t& scissor, int orient) {
336
Saurabh Shah27c1d652012-08-14 19:30:28 -0700337 int& crop_l = crop.left;
338 int& crop_t = crop.top;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700339 int& crop_r = crop.right;
340 int& crop_b = crop.bottom;
341 int crop_w = crop.right - crop.left;
342 int crop_h = crop.bottom - crop.top;
343
Saurabh Shah27c1d652012-08-14 19:30:28 -0700344 int& dst_l = dst.left;
345 int& dst_t = dst.top;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700346 int& dst_r = dst.right;
347 int& dst_b = dst.bottom;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700348 int dst_w = abs(dst.right - dst.left);
349 int dst_h = abs(dst.bottom - dst.top);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700350
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800351 const int& sci_l = scissor.left;
352 const int& sci_t = scissor.top;
353 const int& sci_r = scissor.right;
354 const int& sci_b = scissor.bottom;
355 int sci_w = abs(sci_r - sci_l);
356 int sci_h = abs(sci_b - sci_t);
357
Saurabh Shah541b59d2012-10-11 11:08:12 -0700358 float leftCutRatio = 0.0f, rightCutRatio = 0.0f, topCutRatio = 0.0f,
359 bottomCutRatio = 0.0f;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700360
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800361 if(dst_l < sci_l) {
362 leftCutRatio = (float)(sci_l - dst_l) / (float)dst_w;
363 dst_l = sci_l;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700364 }
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800365
366 if(dst_r > sci_r) {
367 rightCutRatio = (float)(dst_r - sci_r) / (float)dst_w;
368 dst_r = sci_r;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700369 }
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800370
371 if(dst_t < sci_t) {
372 topCutRatio = (float)(sci_t - dst_t) / (float)dst_h;
373 dst_t = sci_t;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700374 }
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800375
376 if(dst_b > sci_b) {
377 bottomCutRatio = (float)(dst_b - sci_b) / (float)dst_h;
378 dst_b = sci_b;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700379 }
Saurabh Shah27c1d652012-08-14 19:30:28 -0700380
Saurabh Shah541b59d2012-10-11 11:08:12 -0700381 calc_cut(leftCutRatio, topCutRatio, rightCutRatio, bottomCutRatio, orient);
382 crop_l += crop_w * leftCutRatio;
383 crop_t += crop_h * topCutRatio;
384 crop_r -= crop_w * rightCutRatio;
385 crop_b -= crop_h * bottomCutRatio;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700386}
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700387
Naseer Ahmed64b81212013-02-14 10:29:47 -0500388void getNonWormholeRegion(hwc_display_contents_1_t* list,
389 hwc_rect_t& nwr)
390{
391 uint32_t last = list->numHwLayers - 1;
392 hwc_rect_t fbDisplayFrame = list->hwLayers[last].displayFrame;
393 //Initiliaze nwr to first frame
394 nwr.left = list->hwLayers[0].displayFrame.left;
395 nwr.top = list->hwLayers[0].displayFrame.top;
396 nwr.right = list->hwLayers[0].displayFrame.right;
397 nwr.bottom = list->hwLayers[0].displayFrame.bottom;
398
399 for (uint32_t i = 1; i < last; i++) {
400 hwc_rect_t displayFrame = list->hwLayers[i].displayFrame;
401 nwr.left = min(nwr.left, displayFrame.left);
402 nwr.top = min(nwr.top, displayFrame.top);
403 nwr.right = max(nwr.right, displayFrame.right);
404 nwr.bottom = max(nwr.bottom, displayFrame.bottom);
405 }
406
407 //Intersect with the framebuffer
408 nwr.left = max(nwr.left, fbDisplayFrame.left);
409 nwr.top = max(nwr.top, fbDisplayFrame.top);
410 nwr.right = min(nwr.right, fbDisplayFrame.right);
411 nwr.bottom = min(nwr.bottom, fbDisplayFrame.bottom);
412
413}
414
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700415bool isExternalActive(hwc_context_t* ctx) {
416 return ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive;
Saurabh Shahfc2acbe2012-08-17 19:47:52 -0700417}
418
Saurabh Shah747af1e2013-02-26 10:25:12 -0800419void closeAcquireFds(hwc_display_contents_1_t* list) {
420 for(uint32_t i = 0; list && i < list->numHwLayers; i++) {
421 //Close the acquireFenceFds
422 //HWC_FRAMEBUFFER are -1 already by SF, rest we close.
423 if(list->hwLayers[i].acquireFenceFd >= 0) {
424 close(list->hwLayers[i].acquireFenceFd);
425 list->hwLayers[i].acquireFenceFd = -1;
426 }
427 }
428}
429
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800430int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy,
431 int fd) {
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700432 int ret = 0;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700433 struct mdp_buf_sync data;
Naseer Ahmedb1c76322012-10-17 00:32:50 -0400434 int acquireFd[MAX_NUM_LAYERS];
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700435 int count = 0;
436 int releaseFd = -1;
437 int fbFd = -1;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800438 memset(&data, 0, sizeof(data));
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500439 bool swapzero = false;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700440 data.flags = MDP_BUF_SYNC_FLAG_WAIT;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700441 data.acq_fen_fd = acquireFd;
442 data.rel_fen_fd = &releaseFd;
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500443 char property[PROPERTY_VALUE_MAX];
444 if(property_get("debug.egl.swapinterval", property, "1") > 0) {
445 if(atoi(property) == 0)
446 swapzero = true;
447 }
448
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700449 //Accumulate acquireFenceFds
450 for(uint32_t i = 0; i < list->numHwLayers; i++) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800451 if(list->hwLayers[i].compositionType == HWC_OVERLAY &&
452 list->hwLayers[i].acquireFenceFd != -1) {
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500453 if(UNLIKELY(swapzero))
454 acquireFd[count++] = -1;
455 else
456 acquireFd[count++] = list->hwLayers[i].acquireFenceFd;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700457 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800458 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
459 if(UNLIKELY(swapzero))
460 acquireFd[count++] = -1;
461 else if(fd != -1) {
462 //set the acquireFD from fd - which is coming from c2d
463 acquireFd[count++] = fd;
464 // Buffer sync IOCTL should be async when using c2d fence is
465 // used
466 data.flags &= ~MDP_BUF_SYNC_FLAG_WAIT;
467 } else if(list->hwLayers[i].acquireFenceFd != -1)
468 acquireFd[count++] = list->hwLayers[i].acquireFenceFd;
469 }
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700470 }
471
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700472 data.acq_fen_fd_cnt = count;
473 fbFd = ctx->dpyAttr[dpy].fd;
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700474 //Waits for acquire fences, returns a release fence
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800475 if(LIKELY(!swapzero)) {
476 uint64_t start = systemTime();
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500477 ret = ioctl(fbFd, MSMFB_BUFFER_SYNC, &data);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800478 ALOGD_IF(HWC_UTILS_DEBUG, "%s: time taken for MSMFB_BUFFER_SYNC IOCTL = %d",
479 __FUNCTION__, (size_t) ns2ms(systemTime() - start));
480 }
Saurabh Shah747af1e2013-02-26 10:25:12 -0800481
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700482 if(ret < 0) {
483 ALOGE("ioctl MSMFB_BUFFER_SYNC failed, err=%s",
484 strerror(errno));
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700485 }
Saurabh Shah747af1e2013-02-26 10:25:12 -0800486
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700487 for(uint32_t i = 0; i < list->numHwLayers; i++) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800488 if(list->hwLayers[i].compositionType == HWC_OVERLAY ||
489 list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700490 //Populate releaseFenceFds.
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500491 if(UNLIKELY(swapzero))
492 list->hwLayers[i].releaseFenceFd = -1;
493 else
494 list->hwLayers[i].releaseFenceFd = dup(releaseFd);
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700495 }
496 }
Saurabh Shah747af1e2013-02-26 10:25:12 -0800497
498 if(fd >= 0) {
499 close(fd);
500 fd = -1;
501 }
502
Naseer Ahmed64b81212013-02-14 10:29:47 -0500503 if (ctx->mCopyBit[dpy])
504 ctx->mCopyBit[dpy]->setReleaseFd(releaseFd);
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500505 if(UNLIKELY(swapzero)){
506 list->retireFenceFd = -1;
507 close(releaseFd);
508 } else {
509 list->retireFenceFd = releaseFd;
510 }
Saurabh Shah747af1e2013-02-26 10:25:12 -0800511
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700512 return ret;
513}
514
Saurabh Shahacf10202013-02-26 10:15:15 -0800515void trimLayer(hwc_context_t *ctx, const int& dpy, const int& transform,
516 hwc_rect_t& crop, hwc_rect_t& dst) {
517 int hw_w = ctx->dpyAttr[dpy].xres;
518 int hw_h = ctx->dpyAttr[dpy].yres;
519 if(dst.left < 0 || dst.top < 0 ||
520 dst.right > hw_w || dst.bottom > hw_h) {
521 hwc_rect_t scissor = {0, 0, hw_w, hw_h };
522 qhwc::calculate_crop_rects(crop, dst, scissor, transform);
523 }
524}
525
526void setMdpFlags(hwc_layer_1_t *layer,
527 ovutils::eMdpFlags &mdpFlags,
528 int rotDownscale) {
529 private_handle_t *hnd = (private_handle_t *)layer->handle;
530 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
531 const int& transform = layer->transform;
532
533 if(layer->blending == HWC_BLENDING_PREMULT) {
534 ovutils::setMdpFlags(mdpFlags,
535 ovutils::OV_MDP_BLEND_FG_PREMULT);
536 }
537
538 if(isYuvBuffer(hnd)) {
539 if(isSecureBuffer(hnd)) {
540 ovutils::setMdpFlags(mdpFlags,
541 ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
542 }
543 if(metadata && (metadata->operation & PP_PARAM_INTERLACED) &&
544 metadata->interlaced) {
545 ovutils::setMdpFlags(mdpFlags,
546 ovutils::OV_MDP_DEINTERLACE);
547 }
548 //Pre-rotation will be used using rotator.
549 if(transform & HWC_TRANSFORM_ROT_90) {
550 ovutils::setMdpFlags(mdpFlags,
551 ovutils::OV_MDP_SOURCE_ROTATED_90);
552 }
553 }
554
555 //No 90 component and no rot-downscale then flips done by MDP
556 //If we use rot then it might as well do flips
557 if(!(layer->transform & HWC_TRANSFORM_ROT_90) && !rotDownscale) {
558 if(layer->transform & HWC_TRANSFORM_FLIP_H) {
559 ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_FLIP_H);
560 }
561
562 if(layer->transform & HWC_TRANSFORM_FLIP_V) {
563 ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_FLIP_V);
564 }
565 }
566}
567
568static inline int configRotator(Rotator *rot, const Whf& whf,
569 const eMdpFlags& mdpFlags, const eTransform& orient,
570 const int& downscale) {
571 rot->setSource(whf);
572 rot->setFlags(mdpFlags);
573 rot->setTransform(orient);
574 rot->setDownscale(downscale);
575 if(!rot->commit()) return -1;
576 return 0;
577}
578
579static inline int configMdp(Overlay *ov, const PipeArgs& parg,
580 const eTransform& orient, const hwc_rect_t& crop,
581 const hwc_rect_t& pos, const eDest& dest) {
582 ov->setSource(parg, dest);
583 ov->setTransform(orient, dest);
584
585 int crop_w = crop.right - crop.left;
586 int crop_h = crop.bottom - crop.top;
587 Dim dcrop(crop.left, crop.top, crop_w, crop_h);
588 ov->setCrop(dcrop, dest);
589
590 int posW = pos.right - pos.left;
591 int posH = pos.bottom - pos.top;
592 Dim position(pos.left, pos.top, posW, posH);
593 ov->setPosition(position, dest);
594
595 if (!ov->commit(dest)) {
596 return -1;
597 }
598 return 0;
599}
600
601static inline void updateSource(eTransform& orient, Whf& whf,
602 hwc_rect_t& crop) {
603 Dim srcCrop(crop.left, crop.top,
604 crop.right - crop.left,
605 crop.bottom - crop.top);
606 preRotateSource(orient, whf, srcCrop);
607 crop.left = srcCrop.x;
608 crop.top = srcCrop.y;
609 crop.right = srcCrop.x + srcCrop.w;
610 crop.bottom = srcCrop.y + srcCrop.h;
611}
612
613int configureLowRes(hwc_context_t *ctx, hwc_layer_1_t *layer,
614 const int& dpy, eMdpFlags& mdpFlags, const eZorder& z,
615 const eIsFg& isFg, const eDest& dest, Rotator **rot) {
616
617 private_handle_t *hnd = (private_handle_t *)layer->handle;
618 if(!hnd) {
619 ALOGE("%s: layer handle is NULL", __FUNCTION__);
620 return -1;
621 }
622
623 hwc_rect_t crop = layer->sourceCrop;
624 hwc_rect_t dst = layer->displayFrame;
625 int transform = layer->transform;
626 eTransform orient = static_cast<eTransform>(transform);
627 int downscale = 0;
628 int rotFlags = ovutils::ROT_FLAGS_NONE;
629 Whf whf(hnd->width, hnd->height,
630 getMdpFormat(hnd->format), hnd->size);
631
632 if(isYuvBuffer(hnd) && ctx->mMDP.version >= qdutils::MDP_V4_2 &&
633 ctx->mMDP.version < qdutils::MDSS_V5) {
634 downscale = getDownscaleFactor(
635 crop.right - crop.left,
636 crop.bottom - crop.top,
637 dst.right - dst.left,
638 dst.bottom - dst.top);
639 if(downscale) {
640 rotFlags = ROT_DOWNSCALE_ENABLED;
641 }
642 }
643
644 setMdpFlags(layer, mdpFlags, downscale);
645 trimLayer(ctx, dpy, transform, crop, dst);
646
647 if(isYuvBuffer(hnd) && //if 90 component or downscale, use rot
648 ((transform & HWC_TRANSFORM_ROT_90) || downscale)) {
649 *rot = ctx->mRotMgr->getNext();
650 if(*rot == NULL) return -1;
651 //Configure rotator for pre-rotation
652 if(configRotator(*rot, whf, mdpFlags, orient, downscale) < 0)
653 return -1;
654 whf.format = (*rot)->getDstFormat();
655 updateSource(orient, whf, crop);
656 //For the mdp, since we are pre-rotating
657 transform = 0;
658 rotFlags |= ovutils::ROT_PREROTATED;
659 }
660
661 PipeArgs parg(mdpFlags, whf, z, isFg, static_cast<eRotFlags>(rotFlags));
662 if(configMdp(ctx->mOverlay, parg, orient, crop, dst, dest) < 0) {
663 ALOGE("%s: commit failed for low res panel", __FUNCTION__);
664 return -1;
665 }
666 return 0;
667}
668
669int configureHighRes(hwc_context_t *ctx, hwc_layer_1_t *layer,
670 const int& dpy, eMdpFlags& mdpFlagsL, const eZorder& z,
671 const eIsFg& isFg, const eDest& lDest, const eDest& rDest,
672 Rotator **rot) {
673 private_handle_t *hnd = (private_handle_t *)layer->handle;
674 if(!hnd) {
675 ALOGE("%s: layer handle is NULL", __FUNCTION__);
676 return -1;
677 }
678
679 int hw_w = ctx->dpyAttr[dpy].xres;
680 int hw_h = ctx->dpyAttr[dpy].yres;
681 hwc_rect_t crop = layer->sourceCrop;
682 hwc_rect_t dst = layer->displayFrame;
683 int transform = layer->transform;
684 eTransform orient = static_cast<eTransform>(transform);
685 const int downscale = 0;
686 int rotFlags = ROT_FLAGS_NONE;
687
688 Whf whf(hnd->width, hnd->height,
689 getMdpFormat(hnd->format), hnd->size);
690
691 setMdpFlags(layer, mdpFlagsL);
692 trimLayer(ctx, dpy, transform, crop, dst);
693
694 if(isYuvBuffer(hnd) && (transform & HWC_TRANSFORM_ROT_90)) {
695 (*rot) = ctx->mRotMgr->getNext();
696 if((*rot) == NULL) return -1;
697 //Configure rotator for pre-rotation
698 if(configRotator(*rot, whf, mdpFlagsL, orient, downscale) < 0)
699 return -1;
700 whf.format = (*rot)->getDstFormat();
701 updateSource(orient, whf, crop);
702 //For the mdp, since we are pre-rotating
703 transform = 0;
704 rotFlags |= ROT_PREROTATED;
705 }
706
707 eMdpFlags mdpFlagsR = mdpFlagsL;
708 setMdpFlags(mdpFlagsR, OV_MDSS_MDP_RIGHT_MIXER);
709
710 hwc_rect_t tmp_cropL, tmp_dstL;
711 hwc_rect_t tmp_cropR, tmp_dstR;
712
713 if(lDest != OV_INVALID) {
714 tmp_cropL = crop;
715 tmp_dstL = dst;
716 hwc_rect_t scissor = {0, 0, hw_w/2, hw_h };
717 qhwc::calculate_crop_rects(tmp_cropL, tmp_dstL, scissor, 0);
718 }
719 if(rDest != OV_INVALID) {
720 tmp_cropR = crop;
721 tmp_dstR = dst;
722 hwc_rect_t scissor = {hw_w/2, 0, hw_w, hw_h };
723 qhwc::calculate_crop_rects(tmp_cropR, tmp_dstR, scissor, 0);
724 }
725
726 //When buffer is flipped, contents of mixer config also needs to swapped.
727 //Not needed if the layer is confined to one half of the screen.
728 //If rotator has been used then it has also done the flips, so ignore them.
729 if(layer->transform & HWC_TRANSFORM_FLIP_V && lDest != OV_INVALID
730 && rDest != OV_INVALID && rot == NULL) {
731 hwc_rect_t new_cropR;
732 new_cropR.left = tmp_cropL.left;
733 new_cropR.right = new_cropR.left + (tmp_cropR.right - tmp_cropR.left);
734
735 hwc_rect_t new_cropL;
736 new_cropL.left = new_cropR.right;
737 new_cropL.right = tmp_cropR.right;
738
739 tmp_cropL.left = new_cropL.left;
740 tmp_cropL.right = new_cropL.right;
741
742 tmp_cropR.left = new_cropR.left;
743 tmp_cropR.right = new_cropR.right;
744
745 }
746
747 //configure left mixer
748 if(lDest != OV_INVALID) {
749 PipeArgs pargL(mdpFlagsL, whf, z, isFg,
750 static_cast<eRotFlags>(rotFlags));
751 if(configMdp(ctx->mOverlay, pargL, orient,
752 tmp_cropL, tmp_dstL, lDest) < 0) {
753 ALOGE("%s: commit failed for left mixer config", __FUNCTION__);
754 return -1;
755 }
756 }
757
758 //configure right mixer
759 if(rDest != OV_INVALID) {
760 PipeArgs pargR(mdpFlagsR, whf, z, isFg,
761 static_cast<eRotFlags>(rotFlags));
762 if(configMdp(ctx->mOverlay, pargR, orient,
763 tmp_cropR, tmp_dstR, rDest) < 0) {
764 ALOGE("%s: commit failed for right mixer config", __FUNCTION__);
765 return -1;
766 }
767 }
768
769 return 0;
770}
771
Naseer Ahmedb1c76322012-10-17 00:32:50 -0400772void LayerCache::resetLayerCache(int num) {
773 for(uint32_t i = 0; i < MAX_NUM_LAYERS; i++) {
774 hnd[i] = NULL;
775 }
776 numHwLayers = num;
777}
778
779void LayerCache::updateLayerCache(hwc_display_contents_1_t* list) {
780
781 int numFbLayers = 0;
782 int numCacheableLayers = 0;
783
784 canUseLayerCache = false;
785 //Bail if geometry changed or num of layers changed
786 if(list->flags & HWC_GEOMETRY_CHANGED ||
787 list->numHwLayers != numHwLayers ) {
788 resetLayerCache(list->numHwLayers);
789 return;
790 }
791
792 for(uint32_t i = 0; i < list->numHwLayers; i++) {
793 //Bail on skip layers
794 if(list->hwLayers[i].flags & HWC_SKIP_LAYER) {
795 resetLayerCache(list->numHwLayers);
796 return;
797 }
798
799 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER) {
800 numFbLayers++;
801 if(hnd[i] == NULL) {
802 hnd[i] = list->hwLayers[i].handle;
803 } else if (hnd[i] ==
804 list->hwLayers[i].handle) {
805 numCacheableLayers++;
806 } else {
807 hnd[i] = NULL;
808 return;
809 }
810 } else {
811 hnd[i] = NULL;
812 }
813 }
814 if(numFbLayers == numCacheableLayers)
815 canUseLayerCache = true;
816
817 //XXX: The marking part is separate, if MDP comp wants
818 // to use it in the future. Right now getting MDP comp
819 // to use this is more trouble than it is worth.
820 markCachedLayersAsOverlay(list);
821}
822
823void LayerCache::markCachedLayersAsOverlay(hwc_display_contents_1_t* list) {
824 //This optimization only works if ALL the layer handles
825 //that were on the framebuffer didn't change.
826 if(canUseLayerCache){
827 for(uint32_t i = 0; i < list->numHwLayers; i++) {
828 if (list->hwLayers[i].handle &&
829 list->hwLayers[i].handle == hnd[i] &&
830 list->hwLayers[i].compositionType != HWC_FRAMEBUFFER_TARGET)
831 {
832 list->hwLayers[i].compositionType = HWC_OVERLAY;
833 }
834 }
835 }
Naseer Ahmedb1c76322012-10-17 00:32:50 -0400836}
837
Saurabh Shahacf10202013-02-26 10:15:15 -0800838};//namespace qhwc