blob: 74ab9e04f5a35903378dd1a60faded783b584134 [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);
Saurabh Shahc4d034f2012-09-27 15:55:15 -070073 overlay::Overlay::initOverlay();
Naseer Ahmed758bfc52012-11-28 17:02:08 -050074 ctx->mOverlay = overlay::Overlay::getInstance();
Saurabh Shahacf10202013-02-26 10:15:15 -080075 ctx->mRotMgr = new RotMgr();
Naseer Ahmed96c4c952012-07-25 18:27:14 -070076 ctx->mMDP.version = qdutils::MDPVersion::getInstance().getMDPVersion();
77 ctx->mMDP.hasOverlay = qdutils::MDPVersion::getInstance().hasOverlay();
78 ctx->mMDP.panel = qdutils::MDPVersion::getInstance().getPanelType();
Saurabh Shahcf053c62012-12-13 12:32:55 -080079 //Is created and destroyed only once for primary
80 //For external it could get created and destroyed multiple times depending
81 //on what external we connect to.
82 ctx->mFBUpdate[HWC_DISPLAY_PRIMARY] =
83 IFBUpdate::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres,
84 HWC_DISPLAY_PRIMARY);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080085
Saurabh Shahacf10202013-02-26 10:15:15 -080086 ctx->mVidOv[HWC_DISPLAY_PRIMARY] =
87 IVideoOverlay::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres,
88 HWC_DISPLAY_PRIMARY);
89
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080090 char value[PROPERTY_VALUE_MAX];
91 // Check if the target supports copybit compostion (dyn/mdp/c2d) to
92 // decide if we need to open the copybit module.
93 int compositionType =
94 qdutils::QCCompositionType::getInstance().getCompositionType();
95
96 if (compositionType & (qdutils::COMPOSITION_TYPE_DYN |
97 qdutils::COMPOSITION_TYPE_MDP |
98 qdutils::COMPOSITION_TYPE_C2D)) {
99 ctx->mCopyBit[HWC_DISPLAY_PRIMARY] = new CopyBit();
100 }
101
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700102 ctx->mExtDisplay = new ExternalDisplay(ctx);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800103 for (uint32_t i = 0; i < MAX_DISPLAYS; i++)
Naseer Ahmede78f0522012-12-07 18:24:28 -0500104 ctx->mLayerCache[i] = new LayerCache();
Saurabh Shahcbf7ccc2012-12-19 16:45:51 -0800105 ctx->mMDPComp = MDPComp::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres);
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500106 MDPComp::init(ctx);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700107
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400108 pthread_mutex_init(&(ctx->vstate.lock), NULL);
109 pthread_cond_init(&(ctx->vstate.cond), NULL);
110 ctx->vstate.enable = false;
Naseer Ahmed56601cd2013-03-05 11:34:14 -0500111 ctx->vstate.fakevsync = false;
Amara Venkata Mastan Manoj Kumar75526f52012-12-27 18:27:01 -0800112 ctx->mExtDispConfiguring = false;
Saurabh Shah86c17292013-02-08 15:24:13 -0800113
114 //Right now hwc starts the service but anybody could do it, or it could be
115 //independent process as well.
116 QService::init();
117 sp<IQClient> client = new QClient(ctx);
118 interface_cast<IQService>(
119 defaultServiceManager()->getService(
120 String16("display.qservice")))->connect(client);
121
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700122 ALOGI("Initializing Qualcomm Hardware Composer");
Naseer Ahmed96c4c952012-07-25 18:27:14 -0700123 ALOGI("MDP version: %d", ctx->mMDP.version);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700124}
125
126void closeContext(hwc_context_t *ctx)
127{
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500128 if(ctx->mOverlay) {
129 delete ctx->mOverlay;
130 ctx->mOverlay = NULL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700131 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700132
Saurabh Shahacf10202013-02-26 10:15:15 -0800133 if(ctx->mRotMgr) {
134 delete ctx->mRotMgr;
135 ctx->mRotMgr = NULL;
136 }
137
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800138 for(int i = 0; i < MAX_DISPLAYS; i++) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800139 if(ctx->mCopyBit[i]) {
140 delete ctx->mCopyBit[i];
141 ctx->mCopyBit[i] = NULL;
142 }
143 }
144
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700145 if(ctx->mFbDev) {
146 framebuffer_close(ctx->mFbDev);
147 ctx->mFbDev = NULL;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700148 close(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd);
149 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = -1;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700150 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700151
152 if(ctx->mExtDisplay) {
153 delete ctx->mExtDisplay;
154 ctx->mExtDisplay = NULL;
155 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400156
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800157 for(int i = 0; i < MAX_DISPLAYS; i++) {
Saurabh Shahcf053c62012-12-13 12:32:55 -0800158 if(ctx->mFBUpdate[i]) {
159 delete ctx->mFBUpdate[i];
160 ctx->mFBUpdate[i] = NULL;
161 }
Saurabh Shahacf10202013-02-26 10:15:15 -0800162 if(ctx->mVidOv[i]) {
163 delete ctx->mVidOv[i];
164 ctx->mVidOv[i] = NULL;
165 }
Saurabh Shahcf053c62012-12-13 12:32:55 -0800166 }
167
Saurabh Shahcbf7ccc2012-12-19 16:45:51 -0800168 if(ctx->mMDPComp) {
169 delete ctx->mMDPComp;
170 ctx->mMDPComp = NULL;
171 }
172
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400173 pthread_mutex_destroy(&(ctx->vstate.lock));
174 pthread_cond_destroy(&(ctx->vstate.cond));
Naseer Ahmed29a26812012-06-14 00:56:20 -0700175}
176
Naseer Ahmed1d183f52012-11-26 12:35:16 -0500177
178void dumpsys_log(android::String8& buf, const char* fmt, ...)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700179{
Naseer Ahmed1d183f52012-11-26 12:35:16 -0500180 va_list varargs;
181 va_start(varargs, fmt);
182 buf.appendFormatV(fmt, varargs);
183 va_end(varargs);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700184}
185
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800186/* Calculates the destination position based on the action safe rectangle */
187void getActionSafePosition(hwc_context_t *ctx, int dpy, uint32_t& x,
188 uint32_t& y, uint32_t& w, uint32_t& h) {
189
190 // if external supports underscan, do nothing
191 // it will be taken care in the driver
192 if(ctx->mExtDisplay->isCEUnderscanSupported())
193 return;
194
195 float wRatio = 1.0;
196 float hRatio = 1.0;
197 float xRatio = 1.0;
198 float yRatio = 1.0;
199
200 float fbWidth = ctx->dpyAttr[dpy].xres;
201 float fbHeight = ctx->dpyAttr[dpy].yres;
202
203 float asX = 0;
204 float asY = 0;
205 float asW = fbWidth;
206 float asH= fbHeight;
207 char value[PROPERTY_VALUE_MAX];
208
209 // Apply action safe parameters
210 property_get("hw.actionsafe.width", value, "0");
211 int asWidthRatio = atoi(value);
212 property_get("hw.actionsafe.height", value, "0");
213 int asHeightRatio = atoi(value);
214 // based on the action safe ratio, get the Action safe rectangle
215 asW = fbWidth * (1.0f - asWidthRatio / 100.0f);
216 asH = fbHeight * (1.0f - asHeightRatio / 100.0f);
217 asX = (fbWidth - asW) / 2;
218 asY = (fbHeight - asH) / 2;
219
220 // calculate the position ratio
221 xRatio = (float)x/fbWidth;
222 yRatio = (float)y/fbHeight;
223 wRatio = (float)w/fbWidth;
224 hRatio = (float)h/fbHeight;
225
226 //Calculate the position...
227 x = (xRatio * asW) + asX;
228 y = (yRatio * asH) + asY;
229 w = (wRatio * asW);
230 h = (hRatio * asH);
231
232 return;
233}
234
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800235bool needsScaling(hwc_layer_1_t const* layer) {
Naseer Ahmed018e5452012-12-03 14:46:15 -0500236 int dst_w, dst_h, src_w, src_h;
237
238 hwc_rect_t displayFrame = layer->displayFrame;
239 hwc_rect_t sourceCrop = layer->sourceCrop;
240
241 dst_w = displayFrame.right - displayFrame.left;
242 dst_h = displayFrame.bottom - displayFrame.top;
243
244 src_w = sourceCrop.right - sourceCrop.left;
245 src_h = sourceCrop.bottom - sourceCrop.top;
246
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800247 if(((src_w != dst_w) || (src_h != dst_h)))
248 return true;
249
250 return false;
251}
252
253bool isAlphaScaled(hwc_layer_1_t const* layer) {
254 if(needsScaling(layer)) {
Naseer Ahmed018e5452012-12-03 14:46:15 -0500255 if(layer->blending != HWC_BLENDING_NONE)
256 return true;
257 }
258 return false;
259}
260
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700261void setListStats(hwc_context_t *ctx,
262 const hwc_display_contents_1_t *list, int dpy) {
263
264 ctx->listStats[dpy].numAppLayers = list->numHwLayers - 1;
265 ctx->listStats[dpy].fbLayerIndex = list->numHwLayers - 1;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700266 ctx->listStats[dpy].skipCount = 0;
Naseer Ahmed018e5452012-12-03 14:46:15 -0500267 ctx->listStats[dpy].needsAlphaScale = false;
Jeykumar Sankarancf537002013-01-21 21:19:15 -0800268 ctx->listStats[dpy].yuvCount = 0;
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800269 ctx->mDMAInUse = false;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700270
271 for (size_t i = 0; i < list->numHwLayers; i++) {
Naseer Ahmed018e5452012-12-03 14:46:15 -0500272 hwc_layer_1_t const* layer = &list->hwLayers[i];
273 private_handle_t *hnd = (private_handle_t *)layer->handle;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700274
Jeykumar Sankarancf537002013-01-21 21:19:15 -0800275 //reset stored yuv index
276 ctx->listStats[dpy].yuvIndices[i] = -1;
277
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700278 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
279 continue;
280 //We disregard FB being skip for now! so the else if
Saurabh Shah35712cb2012-09-14 10:28:18 -0700281 } else if (isSkipLayer(&list->hwLayers[i])) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700282 ctx->listStats[dpy].skipCount++;
Jeykumar Sankaran7535aba2013-02-04 11:03:09 -0800283 } else if (UNLIKELY(isYuvBuffer(hnd))) {
Jeykumar Sankarancf537002013-01-21 21:19:15 -0800284 int& yuvCount = ctx->listStats[dpy].yuvCount;
285 ctx->listStats[dpy].yuvIndices[yuvCount] = i;
286 yuvCount++;
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800287
288 if((layer->transform & HWC_TRANSFORM_ROT_90) && !ctx->mDMAInUse)
289 ctx->mDMAInUse = true;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700290 }
Jeykumar Sankaran7535aba2013-02-04 11:03:09 -0800291
292 if(!ctx->listStats[dpy].needsAlphaScale)
293 ctx->listStats[dpy].needsAlphaScale = isAlphaScaled(layer);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700294 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700295}
296
Naseer Ahmed018e5452012-12-03 14:46:15 -0500297
Saurabh Shah541b59d2012-10-11 11:08:12 -0700298static inline void calc_cut(float& leftCutRatio, float& topCutRatio,
299 float& rightCutRatio, float& bottomCutRatio, int orient) {
Saurabh Shah27c1d652012-08-14 19:30:28 -0700300 if(orient & HAL_TRANSFORM_FLIP_H) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700301 swap(leftCutRatio, rightCutRatio);
Saurabh Shah27c1d652012-08-14 19:30:28 -0700302 }
303 if(orient & HAL_TRANSFORM_FLIP_V) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700304 swap(topCutRatio, bottomCutRatio);
Saurabh Shah27c1d652012-08-14 19:30:28 -0700305 }
306 if(orient & HAL_TRANSFORM_ROT_90) {
307 //Anti clock swapping
Saurabh Shah541b59d2012-10-11 11:08:12 -0700308 float tmpCutRatio = leftCutRatio;
309 leftCutRatio = topCutRatio;
310 topCutRatio = rightCutRatio;
311 rightCutRatio = bottomCutRatio;
312 bottomCutRatio = tmpCutRatio;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700313 }
314}
315
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500316bool isSecuring(hwc_context_t* ctx) {
317 if((ctx->mMDP.version < qdutils::MDSS_V5) &&
318 (ctx->mMDP.version > qdutils::MDP_V3_0) &&
319 ctx->mSecuring) {
320 return true;
321 }
322 return false;
323}
324
Sushil Chauhan2515abf2013-01-08 16:40:05 -0800325bool isSecureModePolicy(int mdpVersion) {
326 if (mdpVersion < qdutils::MDSS_V5)
327 return true;
328 else
329 return false;
330}
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500331
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700332//Crops source buffer against destination and FB boundaries
333void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800334 const hwc_rect_t& scissor, int orient) {
335
Saurabh Shah27c1d652012-08-14 19:30:28 -0700336 int& crop_l = crop.left;
337 int& crop_t = crop.top;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700338 int& crop_r = crop.right;
339 int& crop_b = crop.bottom;
340 int crop_w = crop.right - crop.left;
341 int crop_h = crop.bottom - crop.top;
342
Saurabh Shah27c1d652012-08-14 19:30:28 -0700343 int& dst_l = dst.left;
344 int& dst_t = dst.top;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700345 int& dst_r = dst.right;
346 int& dst_b = dst.bottom;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700347 int dst_w = abs(dst.right - dst.left);
348 int dst_h = abs(dst.bottom - dst.top);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700349
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800350 const int& sci_l = scissor.left;
351 const int& sci_t = scissor.top;
352 const int& sci_r = scissor.right;
353 const int& sci_b = scissor.bottom;
354 int sci_w = abs(sci_r - sci_l);
355 int sci_h = abs(sci_b - sci_t);
356
Saurabh Shah541b59d2012-10-11 11:08:12 -0700357 float leftCutRatio = 0.0f, rightCutRatio = 0.0f, topCutRatio = 0.0f,
358 bottomCutRatio = 0.0f;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700359
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800360 if(dst_l < sci_l) {
361 leftCutRatio = (float)(sci_l - dst_l) / (float)dst_w;
362 dst_l = sci_l;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700363 }
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800364
365 if(dst_r > sci_r) {
366 rightCutRatio = (float)(dst_r - sci_r) / (float)dst_w;
367 dst_r = sci_r;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700368 }
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800369
370 if(dst_t < sci_t) {
371 topCutRatio = (float)(sci_t - dst_t) / (float)dst_h;
372 dst_t = sci_t;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700373 }
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800374
375 if(dst_b > sci_b) {
376 bottomCutRatio = (float)(dst_b - sci_b) / (float)dst_h;
377 dst_b = sci_b;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700378 }
Saurabh Shah27c1d652012-08-14 19:30:28 -0700379
Saurabh Shah541b59d2012-10-11 11:08:12 -0700380 calc_cut(leftCutRatio, topCutRatio, rightCutRatio, bottomCutRatio, orient);
381 crop_l += crop_w * leftCutRatio;
382 crop_t += crop_h * topCutRatio;
383 crop_r -= crop_w * rightCutRatio;
384 crop_b -= crop_h * bottomCutRatio;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700385}
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700386
Naseer Ahmed64b81212013-02-14 10:29:47 -0500387void getNonWormholeRegion(hwc_display_contents_1_t* list,
388 hwc_rect_t& nwr)
389{
390 uint32_t last = list->numHwLayers - 1;
391 hwc_rect_t fbDisplayFrame = list->hwLayers[last].displayFrame;
392 //Initiliaze nwr to first frame
393 nwr.left = list->hwLayers[0].displayFrame.left;
394 nwr.top = list->hwLayers[0].displayFrame.top;
395 nwr.right = list->hwLayers[0].displayFrame.right;
396 nwr.bottom = list->hwLayers[0].displayFrame.bottom;
397
398 for (uint32_t i = 1; i < last; i++) {
399 hwc_rect_t displayFrame = list->hwLayers[i].displayFrame;
400 nwr.left = min(nwr.left, displayFrame.left);
401 nwr.top = min(nwr.top, displayFrame.top);
402 nwr.right = max(nwr.right, displayFrame.right);
403 nwr.bottom = max(nwr.bottom, displayFrame.bottom);
404 }
405
406 //Intersect with the framebuffer
407 nwr.left = max(nwr.left, fbDisplayFrame.left);
408 nwr.top = max(nwr.top, fbDisplayFrame.top);
409 nwr.right = min(nwr.right, fbDisplayFrame.right);
410 nwr.bottom = min(nwr.bottom, fbDisplayFrame.bottom);
411
412}
413
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700414bool isExternalActive(hwc_context_t* ctx) {
415 return ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive;
Saurabh Shahfc2acbe2012-08-17 19:47:52 -0700416}
417
Saurabh Shah747af1e2013-02-26 10:25:12 -0800418void closeAcquireFds(hwc_display_contents_1_t* list) {
419 for(uint32_t i = 0; list && i < list->numHwLayers; i++) {
420 //Close the acquireFenceFds
421 //HWC_FRAMEBUFFER are -1 already by SF, rest we close.
422 if(list->hwLayers[i].acquireFenceFd >= 0) {
423 close(list->hwLayers[i].acquireFenceFd);
424 list->hwLayers[i].acquireFenceFd = -1;
425 }
426 }
427}
428
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800429int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy,
430 int fd) {
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700431 int ret = 0;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700432 struct mdp_buf_sync data;
Naseer Ahmedb1c76322012-10-17 00:32:50 -0400433 int acquireFd[MAX_NUM_LAYERS];
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700434 int count = 0;
435 int releaseFd = -1;
436 int fbFd = -1;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800437 memset(&data, 0, sizeof(data));
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500438 bool swapzero = false;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700439 data.flags = MDP_BUF_SYNC_FLAG_WAIT;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700440 data.acq_fen_fd = acquireFd;
441 data.rel_fen_fd = &releaseFd;
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500442 char property[PROPERTY_VALUE_MAX];
443 if(property_get("debug.egl.swapinterval", property, "1") > 0) {
444 if(atoi(property) == 0)
445 swapzero = true;
446 }
447
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700448 //Accumulate acquireFenceFds
449 for(uint32_t i = 0; i < list->numHwLayers; i++) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800450 if(list->hwLayers[i].compositionType == HWC_OVERLAY &&
451 list->hwLayers[i].acquireFenceFd != -1) {
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500452 if(UNLIKELY(swapzero))
453 acquireFd[count++] = -1;
454 else
455 acquireFd[count++] = list->hwLayers[i].acquireFenceFd;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700456 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800457 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
458 if(UNLIKELY(swapzero))
459 acquireFd[count++] = -1;
460 else if(fd != -1) {
461 //set the acquireFD from fd - which is coming from c2d
462 acquireFd[count++] = fd;
463 // Buffer sync IOCTL should be async when using c2d fence is
464 // used
465 data.flags &= ~MDP_BUF_SYNC_FLAG_WAIT;
466 } else if(list->hwLayers[i].acquireFenceFd != -1)
467 acquireFd[count++] = list->hwLayers[i].acquireFenceFd;
468 }
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700469 }
470
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700471 data.acq_fen_fd_cnt = count;
472 fbFd = ctx->dpyAttr[dpy].fd;
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700473 //Waits for acquire fences, returns a release fence
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800474 if(LIKELY(!swapzero)) {
475 uint64_t start = systemTime();
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500476 ret = ioctl(fbFd, MSMFB_BUFFER_SYNC, &data);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800477 ALOGD_IF(HWC_UTILS_DEBUG, "%s: time taken for MSMFB_BUFFER_SYNC IOCTL = %d",
478 __FUNCTION__, (size_t) ns2ms(systemTime() - start));
479 }
Saurabh Shah747af1e2013-02-26 10:25:12 -0800480
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700481 if(ret < 0) {
482 ALOGE("ioctl MSMFB_BUFFER_SYNC failed, err=%s",
483 strerror(errno));
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700484 }
Saurabh Shah747af1e2013-02-26 10:25:12 -0800485
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700486 for(uint32_t i = 0; i < list->numHwLayers; i++) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800487 if(list->hwLayers[i].compositionType == HWC_OVERLAY ||
488 list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700489 //Populate releaseFenceFds.
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500490 if(UNLIKELY(swapzero))
491 list->hwLayers[i].releaseFenceFd = -1;
492 else
493 list->hwLayers[i].releaseFenceFd = dup(releaseFd);
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700494 }
495 }
Saurabh Shah747af1e2013-02-26 10:25:12 -0800496
497 if(fd >= 0) {
498 close(fd);
499 fd = -1;
500 }
501
Naseer Ahmed64b81212013-02-14 10:29:47 -0500502 if (ctx->mCopyBit[dpy])
503 ctx->mCopyBit[dpy]->setReleaseFd(releaseFd);
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500504 if(UNLIKELY(swapzero)){
505 list->retireFenceFd = -1;
506 close(releaseFd);
507 } else {
508 list->retireFenceFd = releaseFd;
509 }
Saurabh Shah747af1e2013-02-26 10:25:12 -0800510
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700511 return ret;
512}
513
Saurabh Shahacf10202013-02-26 10:15:15 -0800514void trimLayer(hwc_context_t *ctx, const int& dpy, const int& transform,
515 hwc_rect_t& crop, hwc_rect_t& dst) {
516 int hw_w = ctx->dpyAttr[dpy].xres;
517 int hw_h = ctx->dpyAttr[dpy].yres;
518 if(dst.left < 0 || dst.top < 0 ||
519 dst.right > hw_w || dst.bottom > hw_h) {
520 hwc_rect_t scissor = {0, 0, hw_w, hw_h };
521 qhwc::calculate_crop_rects(crop, dst, scissor, transform);
522 }
523}
524
525void setMdpFlags(hwc_layer_1_t *layer,
526 ovutils::eMdpFlags &mdpFlags,
527 int rotDownscale) {
528 private_handle_t *hnd = (private_handle_t *)layer->handle;
529 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata;
530 const int& transform = layer->transform;
531
532 if(layer->blending == HWC_BLENDING_PREMULT) {
533 ovutils::setMdpFlags(mdpFlags,
534 ovutils::OV_MDP_BLEND_FG_PREMULT);
535 }
536
537 if(isYuvBuffer(hnd)) {
538 if(isSecureBuffer(hnd)) {
539 ovutils::setMdpFlags(mdpFlags,
540 ovutils::OV_MDP_SECURE_OVERLAY_SESSION);
541 }
542 if(metadata && (metadata->operation & PP_PARAM_INTERLACED) &&
543 metadata->interlaced) {
544 ovutils::setMdpFlags(mdpFlags,
545 ovutils::OV_MDP_DEINTERLACE);
546 }
547 //Pre-rotation will be used using rotator.
548 if(transform & HWC_TRANSFORM_ROT_90) {
549 ovutils::setMdpFlags(mdpFlags,
550 ovutils::OV_MDP_SOURCE_ROTATED_90);
551 }
552 }
553
554 //No 90 component and no rot-downscale then flips done by MDP
555 //If we use rot then it might as well do flips
556 if(!(layer->transform & HWC_TRANSFORM_ROT_90) && !rotDownscale) {
557 if(layer->transform & HWC_TRANSFORM_FLIP_H) {
558 ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_FLIP_H);
559 }
560
561 if(layer->transform & HWC_TRANSFORM_FLIP_V) {
562 ovutils::setMdpFlags(mdpFlags, ovutils::OV_MDP_FLIP_V);
563 }
564 }
565}
566
567static inline int configRotator(Rotator *rot, const Whf& whf,
568 const eMdpFlags& mdpFlags, const eTransform& orient,
569 const int& downscale) {
570 rot->setSource(whf);
571 rot->setFlags(mdpFlags);
572 rot->setTransform(orient);
573 rot->setDownscale(downscale);
574 if(!rot->commit()) return -1;
575 return 0;
576}
577
578static inline int configMdp(Overlay *ov, const PipeArgs& parg,
579 const eTransform& orient, const hwc_rect_t& crop,
580 const hwc_rect_t& pos, const eDest& dest) {
581 ov->setSource(parg, dest);
582 ov->setTransform(orient, dest);
583
584 int crop_w = crop.right - crop.left;
585 int crop_h = crop.bottom - crop.top;
586 Dim dcrop(crop.left, crop.top, crop_w, crop_h);
587 ov->setCrop(dcrop, dest);
588
589 int posW = pos.right - pos.left;
590 int posH = pos.bottom - pos.top;
591 Dim position(pos.left, pos.top, posW, posH);
592 ov->setPosition(position, dest);
593
594 if (!ov->commit(dest)) {
595 return -1;
596 }
597 return 0;
598}
599
600static inline void updateSource(eTransform& orient, Whf& whf,
601 hwc_rect_t& crop) {
602 Dim srcCrop(crop.left, crop.top,
603 crop.right - crop.left,
604 crop.bottom - crop.top);
605 preRotateSource(orient, whf, srcCrop);
606 crop.left = srcCrop.x;
607 crop.top = srcCrop.y;
608 crop.right = srcCrop.x + srcCrop.w;
609 crop.bottom = srcCrop.y + srcCrop.h;
610}
611
612int configureLowRes(hwc_context_t *ctx, hwc_layer_1_t *layer,
613 const int& dpy, eMdpFlags& mdpFlags, const eZorder& z,
614 const eIsFg& isFg, const eDest& dest, Rotator **rot) {
615
616 private_handle_t *hnd = (private_handle_t *)layer->handle;
617 if(!hnd) {
618 ALOGE("%s: layer handle is NULL", __FUNCTION__);
619 return -1;
620 }
621
622 hwc_rect_t crop = layer->sourceCrop;
623 hwc_rect_t dst = layer->displayFrame;
624 int transform = layer->transform;
625 eTransform orient = static_cast<eTransform>(transform);
626 int downscale = 0;
627 int rotFlags = ovutils::ROT_FLAGS_NONE;
628 Whf whf(hnd->width, hnd->height,
629 getMdpFormat(hnd->format), hnd->size);
630
631 if(isYuvBuffer(hnd) && ctx->mMDP.version >= qdutils::MDP_V4_2 &&
632 ctx->mMDP.version < qdutils::MDSS_V5) {
633 downscale = getDownscaleFactor(
634 crop.right - crop.left,
635 crop.bottom - crop.top,
636 dst.right - dst.left,
637 dst.bottom - dst.top);
638 if(downscale) {
639 rotFlags = ROT_DOWNSCALE_ENABLED;
640 }
641 }
642
643 setMdpFlags(layer, mdpFlags, downscale);
644 trimLayer(ctx, dpy, transform, crop, dst);
645
646 if(isYuvBuffer(hnd) && //if 90 component or downscale, use rot
647 ((transform & HWC_TRANSFORM_ROT_90) || downscale)) {
648 *rot = ctx->mRotMgr->getNext();
649 if(*rot == NULL) return -1;
650 //Configure rotator for pre-rotation
651 if(configRotator(*rot, whf, mdpFlags, orient, downscale) < 0)
652 return -1;
653 whf.format = (*rot)->getDstFormat();
654 updateSource(orient, whf, crop);
655 //For the mdp, since we are pre-rotating
656 transform = 0;
657 rotFlags |= ovutils::ROT_PREROTATED;
658 }
659
660 PipeArgs parg(mdpFlags, whf, z, isFg, static_cast<eRotFlags>(rotFlags));
661 if(configMdp(ctx->mOverlay, parg, orient, crop, dst, dest) < 0) {
662 ALOGE("%s: commit failed for low res panel", __FUNCTION__);
663 return -1;
664 }
665 return 0;
666}
667
668int configureHighRes(hwc_context_t *ctx, hwc_layer_1_t *layer,
669 const int& dpy, eMdpFlags& mdpFlagsL, const eZorder& z,
670 const eIsFg& isFg, const eDest& lDest, const eDest& rDest,
671 Rotator **rot) {
672 private_handle_t *hnd = (private_handle_t *)layer->handle;
673 if(!hnd) {
674 ALOGE("%s: layer handle is NULL", __FUNCTION__);
675 return -1;
676 }
677
678 int hw_w = ctx->dpyAttr[dpy].xres;
679 int hw_h = ctx->dpyAttr[dpy].yres;
680 hwc_rect_t crop = layer->sourceCrop;
681 hwc_rect_t dst = layer->displayFrame;
682 int transform = layer->transform;
683 eTransform orient = static_cast<eTransform>(transform);
684 const int downscale = 0;
685 int rotFlags = ROT_FLAGS_NONE;
686
687 Whf whf(hnd->width, hnd->height,
688 getMdpFormat(hnd->format), hnd->size);
689
690 setMdpFlags(layer, mdpFlagsL);
691 trimLayer(ctx, dpy, transform, crop, dst);
692
693 if(isYuvBuffer(hnd) && (transform & HWC_TRANSFORM_ROT_90)) {
694 (*rot) = ctx->mRotMgr->getNext();
695 if((*rot) == NULL) return -1;
696 //Configure rotator for pre-rotation
697 if(configRotator(*rot, whf, mdpFlagsL, orient, downscale) < 0)
698 return -1;
699 whf.format = (*rot)->getDstFormat();
700 updateSource(orient, whf, crop);
701 //For the mdp, since we are pre-rotating
702 transform = 0;
703 rotFlags |= ROT_PREROTATED;
704 }
705
706 eMdpFlags mdpFlagsR = mdpFlagsL;
707 setMdpFlags(mdpFlagsR, OV_MDSS_MDP_RIGHT_MIXER);
708
709 hwc_rect_t tmp_cropL, tmp_dstL;
710 hwc_rect_t tmp_cropR, tmp_dstR;
711
712 if(lDest != OV_INVALID) {
713 tmp_cropL = crop;
714 tmp_dstL = dst;
715 hwc_rect_t scissor = {0, 0, hw_w/2, hw_h };
716 qhwc::calculate_crop_rects(tmp_cropL, tmp_dstL, scissor, 0);
717 }
718 if(rDest != OV_INVALID) {
719 tmp_cropR = crop;
720 tmp_dstR = dst;
721 hwc_rect_t scissor = {hw_w/2, 0, hw_w, hw_h };
722 qhwc::calculate_crop_rects(tmp_cropR, tmp_dstR, scissor, 0);
723 }
724
725 //When buffer is flipped, contents of mixer config also needs to swapped.
726 //Not needed if the layer is confined to one half of the screen.
727 //If rotator has been used then it has also done the flips, so ignore them.
728 if(layer->transform & HWC_TRANSFORM_FLIP_V && lDest != OV_INVALID
729 && rDest != OV_INVALID && rot == NULL) {
730 hwc_rect_t new_cropR;
731 new_cropR.left = tmp_cropL.left;
732 new_cropR.right = new_cropR.left + (tmp_cropR.right - tmp_cropR.left);
733
734 hwc_rect_t new_cropL;
735 new_cropL.left = new_cropR.right;
736 new_cropL.right = tmp_cropR.right;
737
738 tmp_cropL.left = new_cropL.left;
739 tmp_cropL.right = new_cropL.right;
740
741 tmp_cropR.left = new_cropR.left;
742 tmp_cropR.right = new_cropR.right;
743
744 }
745
746 //configure left mixer
747 if(lDest != OV_INVALID) {
748 PipeArgs pargL(mdpFlagsL, whf, z, isFg,
749 static_cast<eRotFlags>(rotFlags));
750 if(configMdp(ctx->mOverlay, pargL, orient,
751 tmp_cropL, tmp_dstL, lDest) < 0) {
752 ALOGE("%s: commit failed for left mixer config", __FUNCTION__);
753 return -1;
754 }
755 }
756
757 //configure right mixer
758 if(rDest != OV_INVALID) {
759 PipeArgs pargR(mdpFlagsR, whf, z, isFg,
760 static_cast<eRotFlags>(rotFlags));
761 if(configMdp(ctx->mOverlay, pargR, orient,
762 tmp_cropR, tmp_dstR, rDest) < 0) {
763 ALOGE("%s: commit failed for right mixer config", __FUNCTION__);
764 return -1;
765 }
766 }
767
768 return 0;
769}
770
Naseer Ahmedb1c76322012-10-17 00:32:50 -0400771void LayerCache::resetLayerCache(int num) {
772 for(uint32_t i = 0; i < MAX_NUM_LAYERS; i++) {
773 hnd[i] = NULL;
774 }
775 numHwLayers = num;
776}
777
778void LayerCache::updateLayerCache(hwc_display_contents_1_t* list) {
779
780 int numFbLayers = 0;
781 int numCacheableLayers = 0;
782
783 canUseLayerCache = false;
784 //Bail if geometry changed or num of layers changed
785 if(list->flags & HWC_GEOMETRY_CHANGED ||
786 list->numHwLayers != numHwLayers ) {
787 resetLayerCache(list->numHwLayers);
788 return;
789 }
790
791 for(uint32_t i = 0; i < list->numHwLayers; i++) {
792 //Bail on skip layers
793 if(list->hwLayers[i].flags & HWC_SKIP_LAYER) {
794 resetLayerCache(list->numHwLayers);
795 return;
796 }
797
798 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER) {
799 numFbLayers++;
800 if(hnd[i] == NULL) {
801 hnd[i] = list->hwLayers[i].handle;
802 } else if (hnd[i] ==
803 list->hwLayers[i].handle) {
804 numCacheableLayers++;
805 } else {
806 hnd[i] = NULL;
807 return;
808 }
809 } else {
810 hnd[i] = NULL;
811 }
812 }
813 if(numFbLayers == numCacheableLayers)
814 canUseLayerCache = true;
815
816 //XXX: The marking part is separate, if MDP comp wants
817 // to use it in the future. Right now getting MDP comp
818 // to use this is more trouble than it is worth.
819 markCachedLayersAsOverlay(list);
820}
821
822void LayerCache::markCachedLayersAsOverlay(hwc_display_contents_1_t* list) {
823 //This optimization only works if ALL the layer handles
824 //that were on the framebuffer didn't change.
825 if(canUseLayerCache){
826 for(uint32_t i = 0; i < list->numHwLayers; i++) {
827 if (list->hwLayers[i].handle &&
828 list->hwLayers[i].handle == hnd[i] &&
829 list->hwLayers[i].compositionType != HWC_FRAMEBUFFER_TARGET)
830 {
831 list->hwLayers[i].compositionType = HWC_OVERLAY;
832 }
833 }
834 }
Naseer Ahmedb1c76322012-10-17 00:32:50 -0400835}
836
Saurabh Shahacf10202013-02-26 10:15:15 -0800837};//namespace qhwc