blob: fe3401be76e8f78721d56d55e52e6b608fc0fe1d [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>
Naseer Ahmed29a26812012-06-14 00:56:20 -070028#include "hwc_utils.h"
Naseer Ahmed54821fe2012-11-28 18:44:38 -050029#include "hwc_mdpcomp.h"
Saurabh Shahcf053c62012-12-13 12:32:55 -080030#include "hwc_fbupdate.h"
Naseer Ahmeda87da602012-07-01 23:54:19 -070031#include "mdp_version.h"
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080032#include "hwc_copybit.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070033#include "external.h"
Saurabh Shah86c17292013-02-08 15:24:13 -080034#include "hwc_qclient.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070035#include "QService.h"
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080036#include "comptype.h"
Saurabh Shah86c17292013-02-08 15:24:13 -080037
38using namespace qClient;
39using namespace qService;
40using namespace android;
41
Naseer Ahmed29a26812012-06-14 00:56:20 -070042namespace qhwc {
Naseer Ahmed72cf9762012-07-21 12:17:13 -070043
44// Opens Framebuffer device
45static void openFramebufferDevice(hwc_context_t *ctx)
46{
47 hw_module_t const *module;
48 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
49 framebuffer_open(module, &(ctx->mFbDev));
Saurabh Shah3e858eb2012-09-17 16:53:21 -070050 private_module_t* m = reinterpret_cast<private_module_t*>(
51 ctx->mFbDev->common.module);
52 //xres, yres may not be 32 aligned
Naseer Ahmed54821fe2012-11-28 18:44:38 -050053 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].stride = m->finfo.line_length /
54 (m->info.xres/8);
Saurabh Shah3e858eb2012-09-17 16:53:21 -070055 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres = m->info.xres;
56 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].yres = m->info.yres;
57 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xdpi = ctx->mFbDev->xdpi;
58 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].ydpi = ctx->mFbDev->ydpi;
59 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period =
60 1000000000l / ctx->mFbDev->fps;
61 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = openFb(HWC_DISPLAY_PRIMARY);
Naseer Ahmed72cf9762012-07-21 12:17:13 -070062 }
63}
64
Naseer Ahmed29a26812012-06-14 00:56:20 -070065void initContext(hwc_context_t *ctx)
66{
Naseer Ahmed72cf9762012-07-21 12:17:13 -070067 openFramebufferDevice(ctx);
Saurabh Shahc4d034f2012-09-27 15:55:15 -070068 overlay::Overlay::initOverlay();
Naseer Ahmed758bfc52012-11-28 17:02:08 -050069 ctx->mOverlay = overlay::Overlay::getInstance();
Naseer Ahmed96c4c952012-07-25 18:27:14 -070070 ctx->mMDP.version = qdutils::MDPVersion::getInstance().getMDPVersion();
71 ctx->mMDP.hasOverlay = qdutils::MDPVersion::getInstance().hasOverlay();
72 ctx->mMDP.panel = qdutils::MDPVersion::getInstance().getPanelType();
Saurabh Shahcf053c62012-12-13 12:32:55 -080073 //Is created and destroyed only once for primary
74 //For external it could get created and destroyed multiple times depending
75 //on what external we connect to.
76 ctx->mFBUpdate[HWC_DISPLAY_PRIMARY] =
77 IFBUpdate::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres,
78 HWC_DISPLAY_PRIMARY);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080079
80 char value[PROPERTY_VALUE_MAX];
81 // Check if the target supports copybit compostion (dyn/mdp/c2d) to
82 // decide if we need to open the copybit module.
83 int compositionType =
84 qdutils::QCCompositionType::getInstance().getCompositionType();
85
86 if (compositionType & (qdutils::COMPOSITION_TYPE_DYN |
87 qdutils::COMPOSITION_TYPE_MDP |
88 qdutils::COMPOSITION_TYPE_C2D)) {
89 ctx->mCopyBit[HWC_DISPLAY_PRIMARY] = new CopyBit();
90 }
91
Naseer Ahmed72cf9762012-07-21 12:17:13 -070092 ctx->mExtDisplay = new ExternalDisplay(ctx);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -080093 for (uint32_t i = 0; i < MAX_DISPLAYS; i++)
Naseer Ahmede78f0522012-12-07 18:24:28 -050094 ctx->mLayerCache[i] = new LayerCache();
Saurabh Shahcbf7ccc2012-12-19 16:45:51 -080095 ctx->mMDPComp = MDPComp::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres);
Naseer Ahmed54821fe2012-11-28 18:44:38 -050096 MDPComp::init(ctx);
Naseer Ahmed72cf9762012-07-21 12:17:13 -070097
Naseer Ahmedff4f0252012-10-01 13:03:01 -040098 pthread_mutex_init(&(ctx->vstate.lock), NULL);
99 pthread_cond_init(&(ctx->vstate.cond), NULL);
100 ctx->vstate.enable = false;
Amara Venkata Mastan Manoj Kumar75526f52012-12-27 18:27:01 -0800101 ctx->mExtDispConfiguring = false;
Saurabh Shah86c17292013-02-08 15:24:13 -0800102
103 //Right now hwc starts the service but anybody could do it, or it could be
104 //independent process as well.
105 QService::init();
106 sp<IQClient> client = new QClient(ctx);
107 interface_cast<IQService>(
108 defaultServiceManager()->getService(
109 String16("display.qservice")))->connect(client);
110
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700111 ALOGI("Initializing Qualcomm Hardware Composer");
Naseer Ahmed96c4c952012-07-25 18:27:14 -0700112 ALOGI("MDP version: %d", ctx->mMDP.version);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700113}
114
115void closeContext(hwc_context_t *ctx)
116{
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500117 if(ctx->mOverlay) {
118 delete ctx->mOverlay;
119 ctx->mOverlay = NULL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700120 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700121
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800122 for(int i = 0; i < MAX_DISPLAYS; i++) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800123 if(ctx->mCopyBit[i]) {
124 delete ctx->mCopyBit[i];
125 ctx->mCopyBit[i] = NULL;
126 }
127 }
128
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700129 if(ctx->mFbDev) {
130 framebuffer_close(ctx->mFbDev);
131 ctx->mFbDev = NULL;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700132 close(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd);
133 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = -1;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700134 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700135
136 if(ctx->mExtDisplay) {
137 delete ctx->mExtDisplay;
138 ctx->mExtDisplay = NULL;
139 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400140
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800141 for(int i = 0; i < MAX_DISPLAYS; i++) {
Saurabh Shahcf053c62012-12-13 12:32:55 -0800142 if(ctx->mFBUpdate[i]) {
143 delete ctx->mFBUpdate[i];
144 ctx->mFBUpdate[i] = NULL;
145 }
146 }
147
Saurabh Shahcbf7ccc2012-12-19 16:45:51 -0800148 if(ctx->mMDPComp) {
149 delete ctx->mMDPComp;
150 ctx->mMDPComp = NULL;
151 }
152
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400153 pthread_mutex_destroy(&(ctx->vstate.lock));
154 pthread_cond_destroy(&(ctx->vstate.cond));
Naseer Ahmed29a26812012-06-14 00:56:20 -0700155}
156
Naseer Ahmed1d183f52012-11-26 12:35:16 -0500157
158void dumpsys_log(android::String8& buf, const char* fmt, ...)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700159{
Naseer Ahmed1d183f52012-11-26 12:35:16 -0500160 va_list varargs;
161 va_start(varargs, fmt);
162 buf.appendFormatV(fmt, varargs);
163 va_end(varargs);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700164}
165
Arun Kumar K.Rfeb2d8a2013-02-01 02:53:13 -0800166/* Calculates the destination position based on the action safe rectangle */
167void getActionSafePosition(hwc_context_t *ctx, int dpy, uint32_t& x,
168 uint32_t& y, uint32_t& w, uint32_t& h) {
169
170 // if external supports underscan, do nothing
171 // it will be taken care in the driver
172 if(ctx->mExtDisplay->isCEUnderscanSupported())
173 return;
174
175 float wRatio = 1.0;
176 float hRatio = 1.0;
177 float xRatio = 1.0;
178 float yRatio = 1.0;
179
180 float fbWidth = ctx->dpyAttr[dpy].xres;
181 float fbHeight = ctx->dpyAttr[dpy].yres;
182
183 float asX = 0;
184 float asY = 0;
185 float asW = fbWidth;
186 float asH= fbHeight;
187 char value[PROPERTY_VALUE_MAX];
188
189 // Apply action safe parameters
190 property_get("hw.actionsafe.width", value, "0");
191 int asWidthRatio = atoi(value);
192 property_get("hw.actionsafe.height", value, "0");
193 int asHeightRatio = atoi(value);
194 // based on the action safe ratio, get the Action safe rectangle
195 asW = fbWidth * (1.0f - asWidthRatio / 100.0f);
196 asH = fbHeight * (1.0f - asHeightRatio / 100.0f);
197 asX = (fbWidth - asW) / 2;
198 asY = (fbHeight - asH) / 2;
199
200 // calculate the position ratio
201 xRatio = (float)x/fbWidth;
202 yRatio = (float)y/fbHeight;
203 wRatio = (float)w/fbWidth;
204 hRatio = (float)h/fbHeight;
205
206 //Calculate the position...
207 x = (xRatio * asW) + asX;
208 y = (yRatio * asH) + asY;
209 w = (wRatio * asW);
210 h = (hRatio * asH);
211
212 return;
213}
214
Naseer Ahmed018e5452012-12-03 14:46:15 -0500215static inline bool isAlphaScaled(hwc_layer_1_t const* layer) {
216 int dst_w, dst_h, src_w, src_h;
217
218 hwc_rect_t displayFrame = layer->displayFrame;
219 hwc_rect_t sourceCrop = layer->sourceCrop;
220
221 dst_w = displayFrame.right - displayFrame.left;
222 dst_h = displayFrame.bottom - displayFrame.top;
223
224 src_w = sourceCrop.right - sourceCrop.left;
225 src_h = sourceCrop.bottom - sourceCrop.top;
226
227 if(((src_w != dst_w) || (src_h != dst_h))) {
228 if(layer->blending != HWC_BLENDING_NONE)
229 return true;
230 }
231 return false;
232}
233
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700234void setListStats(hwc_context_t *ctx,
235 const hwc_display_contents_1_t *list, int dpy) {
236
237 ctx->listStats[dpy].numAppLayers = list->numHwLayers - 1;
238 ctx->listStats[dpy].fbLayerIndex = list->numHwLayers - 1;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700239 ctx->listStats[dpy].skipCount = 0;
Naseer Ahmed018e5452012-12-03 14:46:15 -0500240 ctx->listStats[dpy].needsAlphaScale = false;
Jeykumar Sankarancf537002013-01-21 21:19:15 -0800241 ctx->listStats[dpy].yuvCount = 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700242
243 for (size_t i = 0; i < list->numHwLayers; i++) {
Naseer Ahmed018e5452012-12-03 14:46:15 -0500244 hwc_layer_1_t const* layer = &list->hwLayers[i];
245 private_handle_t *hnd = (private_handle_t *)layer->handle;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700246
Jeykumar Sankarancf537002013-01-21 21:19:15 -0800247 //reset stored yuv index
248 ctx->listStats[dpy].yuvIndices[i] = -1;
249
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700250 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
251 continue;
252 //We disregard FB being skip for now! so the else if
Saurabh Shah35712cb2012-09-14 10:28:18 -0700253 } else if (isSkipLayer(&list->hwLayers[i])) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700254 ctx->listStats[dpy].skipCount++;
Jeykumar Sankaran7535aba2013-02-04 11:03:09 -0800255 } else if (UNLIKELY(isYuvBuffer(hnd))) {
Jeykumar Sankarancf537002013-01-21 21:19:15 -0800256 int& yuvCount = ctx->listStats[dpy].yuvCount;
257 ctx->listStats[dpy].yuvIndices[yuvCount] = i;
258 yuvCount++;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700259 }
Jeykumar Sankaran7535aba2013-02-04 11:03:09 -0800260
261 if(!ctx->listStats[dpy].needsAlphaScale)
262 ctx->listStats[dpy].needsAlphaScale = isAlphaScaled(layer);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700263 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700264}
265
Naseer Ahmed018e5452012-12-03 14:46:15 -0500266
Saurabh Shah541b59d2012-10-11 11:08:12 -0700267static inline void calc_cut(float& leftCutRatio, float& topCutRatio,
268 float& rightCutRatio, float& bottomCutRatio, int orient) {
Saurabh Shah27c1d652012-08-14 19:30:28 -0700269 if(orient & HAL_TRANSFORM_FLIP_H) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700270 swap(leftCutRatio, rightCutRatio);
Saurabh Shah27c1d652012-08-14 19:30:28 -0700271 }
272 if(orient & HAL_TRANSFORM_FLIP_V) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700273 swap(topCutRatio, bottomCutRatio);
Saurabh Shah27c1d652012-08-14 19:30:28 -0700274 }
275 if(orient & HAL_TRANSFORM_ROT_90) {
276 //Anti clock swapping
Saurabh Shah541b59d2012-10-11 11:08:12 -0700277 float tmpCutRatio = leftCutRatio;
278 leftCutRatio = topCutRatio;
279 topCutRatio = rightCutRatio;
280 rightCutRatio = bottomCutRatio;
281 bottomCutRatio = tmpCutRatio;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700282 }
283}
284
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500285bool isSecuring(hwc_context_t* ctx) {
286 if((ctx->mMDP.version < qdutils::MDSS_V5) &&
287 (ctx->mMDP.version > qdutils::MDP_V3_0) &&
288 ctx->mSecuring) {
289 return true;
290 }
291 return false;
292}
293
Sushil Chauhan2515abf2013-01-08 16:40:05 -0800294bool isSecureModePolicy(int mdpVersion) {
295 if (mdpVersion < qdutils::MDSS_V5)
296 return true;
297 else
298 return false;
299}
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500300
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700301//Crops source buffer against destination and FB boundaries
302void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
Saurabh Shah27c1d652012-08-14 19:30:28 -0700303 const int fbWidth, const int fbHeight, int orient) {
304 int& crop_l = crop.left;
305 int& crop_t = crop.top;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700306 int& crop_r = crop.right;
307 int& crop_b = crop.bottom;
308 int crop_w = crop.right - crop.left;
309 int crop_h = crop.bottom - crop.top;
310
Saurabh Shah27c1d652012-08-14 19:30:28 -0700311 int& dst_l = dst.left;
312 int& dst_t = dst.top;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700313 int& dst_r = dst.right;
314 int& dst_b = dst.bottom;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700315 int dst_w = abs(dst.right - dst.left);
316 int dst_h = abs(dst.bottom - dst.top);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700317
Saurabh Shah541b59d2012-10-11 11:08:12 -0700318 float leftCutRatio = 0.0f, rightCutRatio = 0.0f, topCutRatio = 0.0f,
319 bottomCutRatio = 0.0f;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700320
Saurabh Shah27c1d652012-08-14 19:30:28 -0700321 if(dst_l < 0) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700322 leftCutRatio = (float)(0.0f - dst_l) / (float)dst_w;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700323 dst_l = 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700324 }
325 if(dst_r > fbWidth) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700326 rightCutRatio = (float)(dst_r - fbWidth) / (float)dst_w;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700327 dst_r = fbWidth;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700328 }
Saurabh Shah27c1d652012-08-14 19:30:28 -0700329 if(dst_t < 0) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700330 topCutRatio = (float)(0 - dst_t) / (float)dst_h;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700331 dst_t = 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700332 }
333 if(dst_b > fbHeight) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700334 bottomCutRatio = (float)(dst_b - fbHeight) / (float)dst_h;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700335 dst_b = fbHeight;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700336 }
Saurabh Shah27c1d652012-08-14 19:30:28 -0700337
Saurabh Shah541b59d2012-10-11 11:08:12 -0700338 calc_cut(leftCutRatio, topCutRatio, rightCutRatio, bottomCutRatio, orient);
339 crop_l += crop_w * leftCutRatio;
340 crop_t += crop_h * topCutRatio;
341 crop_r -= crop_w * rightCutRatio;
342 crop_b -= crop_h * bottomCutRatio;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700343}
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700344
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700345bool isExternalActive(hwc_context_t* ctx) {
346 return ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive;
Saurabh Shahfc2acbe2012-08-17 19:47:52 -0700347}
348
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800349int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy,
350 int fd) {
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700351 int ret = 0;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700352 struct mdp_buf_sync data;
Naseer Ahmedb1c76322012-10-17 00:32:50 -0400353 int acquireFd[MAX_NUM_LAYERS];
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700354 int count = 0;
355 int releaseFd = -1;
356 int fbFd = -1;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800357 memset(&data, 0, sizeof(data));
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500358 bool swapzero = false;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700359 data.flags = MDP_BUF_SYNC_FLAG_WAIT;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700360 data.acq_fen_fd = acquireFd;
361 data.rel_fen_fd = &releaseFd;
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500362 char property[PROPERTY_VALUE_MAX];
363 if(property_get("debug.egl.swapinterval", property, "1") > 0) {
364 if(atoi(property) == 0)
365 swapzero = true;
366 }
367
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700368 //Accumulate acquireFenceFds
369 for(uint32_t i = 0; i < list->numHwLayers; i++) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800370 if(list->hwLayers[i].compositionType == HWC_OVERLAY &&
371 list->hwLayers[i].acquireFenceFd != -1) {
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500372 if(UNLIKELY(swapzero))
373 acquireFd[count++] = -1;
374 else
375 acquireFd[count++] = list->hwLayers[i].acquireFenceFd;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700376 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800377 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
378 if(UNLIKELY(swapzero))
379 acquireFd[count++] = -1;
380 else if(fd != -1) {
381 //set the acquireFD from fd - which is coming from c2d
382 acquireFd[count++] = fd;
383 // Buffer sync IOCTL should be async when using c2d fence is
384 // used
385 data.flags &= ~MDP_BUF_SYNC_FLAG_WAIT;
386 } else if(list->hwLayers[i].acquireFenceFd != -1)
387 acquireFd[count++] = list->hwLayers[i].acquireFenceFd;
388 }
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700389 }
390
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700391 data.acq_fen_fd_cnt = count;
392 fbFd = ctx->dpyAttr[dpy].fd;
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700393 //Waits for acquire fences, returns a release fence
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800394 if(LIKELY(!swapzero)) {
395 uint64_t start = systemTime();
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500396 ret = ioctl(fbFd, MSMFB_BUFFER_SYNC, &data);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800397 ALOGD_IF(HWC_UTILS_DEBUG, "%s: time taken for MSMFB_BUFFER_SYNC IOCTL = %d",
398 __FUNCTION__, (size_t) ns2ms(systemTime() - start));
399 }
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700400 if(ret < 0) {
401 ALOGE("ioctl MSMFB_BUFFER_SYNC failed, err=%s",
402 strerror(errno));
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700403 }
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700404 for(uint32_t i = 0; i < list->numHwLayers; i++) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800405 if(list->hwLayers[i].compositionType == HWC_OVERLAY ||
406 list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700407 //Close the acquireFenceFds
Naseer Ahmedf53b3772013-02-15 19:13:50 -0500408 if(list->hwLayers[i].acquireFenceFd >= 0) {
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700409 close(list->hwLayers[i].acquireFenceFd);
410 list->hwLayers[i].acquireFenceFd = -1;
411 }
Naseer Ahmedf53b3772013-02-15 19:13:50 -0500412 if(fd >= 0) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800413 close(fd);
414 fd = -1;
415 }
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700416 //Populate releaseFenceFds.
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500417 if(UNLIKELY(swapzero))
418 list->hwLayers[i].releaseFenceFd = -1;
419 else
420 list->hwLayers[i].releaseFenceFd = dup(releaseFd);
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700421 }
422 }
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500423 if(UNLIKELY(swapzero)){
424 list->retireFenceFd = -1;
425 close(releaseFd);
426 } else {
427 list->retireFenceFd = releaseFd;
428 }
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700429 return ret;
430}
431
Naseer Ahmedb1c76322012-10-17 00:32:50 -0400432void LayerCache::resetLayerCache(int num) {
433 for(uint32_t i = 0; i < MAX_NUM_LAYERS; i++) {
434 hnd[i] = NULL;
435 }
436 numHwLayers = num;
437}
438
439void LayerCache::updateLayerCache(hwc_display_contents_1_t* list) {
440
441 int numFbLayers = 0;
442 int numCacheableLayers = 0;
443
444 canUseLayerCache = false;
445 //Bail if geometry changed or num of layers changed
446 if(list->flags & HWC_GEOMETRY_CHANGED ||
447 list->numHwLayers != numHwLayers ) {
448 resetLayerCache(list->numHwLayers);
449 return;
450 }
451
452 for(uint32_t i = 0; i < list->numHwLayers; i++) {
453 //Bail on skip layers
454 if(list->hwLayers[i].flags & HWC_SKIP_LAYER) {
455 resetLayerCache(list->numHwLayers);
456 return;
457 }
458
459 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER) {
460 numFbLayers++;
461 if(hnd[i] == NULL) {
462 hnd[i] = list->hwLayers[i].handle;
463 } else if (hnd[i] ==
464 list->hwLayers[i].handle) {
465 numCacheableLayers++;
466 } else {
467 hnd[i] = NULL;
468 return;
469 }
470 } else {
471 hnd[i] = NULL;
472 }
473 }
474 if(numFbLayers == numCacheableLayers)
475 canUseLayerCache = true;
476
477 //XXX: The marking part is separate, if MDP comp wants
478 // to use it in the future. Right now getting MDP comp
479 // to use this is more trouble than it is worth.
480 markCachedLayersAsOverlay(list);
481}
482
483void LayerCache::markCachedLayersAsOverlay(hwc_display_contents_1_t* list) {
484 //This optimization only works if ALL the layer handles
485 //that were on the framebuffer didn't change.
486 if(canUseLayerCache){
487 for(uint32_t i = 0; i < list->numHwLayers; i++) {
488 if (list->hwLayers[i].handle &&
489 list->hwLayers[i].handle == hnd[i] &&
490 list->hwLayers[i].compositionType != HWC_FRAMEBUFFER_TARGET)
491 {
492 list->hwLayers[i].compositionType = HWC_OVERLAY;
493 }
494 }
495 }
496
497}
498
Naseer Ahmed29a26812012-06-14 00:56:20 -0700499};//namespace