blob: 51936443333872444209f4ee8b7aadef8b4c28fe [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
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800215bool needsScaling(hwc_layer_1_t const* layer) {
Naseer Ahmed018e5452012-12-03 14:46:15 -0500216 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
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800227 if(((src_w != dst_w) || (src_h != dst_h)))
228 return true;
229
230 return false;
231}
232
233bool isAlphaScaled(hwc_layer_1_t const* layer) {
234 if(needsScaling(layer)) {
Naseer Ahmed018e5452012-12-03 14:46:15 -0500235 if(layer->blending != HWC_BLENDING_NONE)
236 return true;
237 }
238 return false;
239}
240
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700241void setListStats(hwc_context_t *ctx,
242 const hwc_display_contents_1_t *list, int dpy) {
243
244 ctx->listStats[dpy].numAppLayers = list->numHwLayers - 1;
245 ctx->listStats[dpy].fbLayerIndex = list->numHwLayers - 1;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700246 ctx->listStats[dpy].skipCount = 0;
Naseer Ahmed018e5452012-12-03 14:46:15 -0500247 ctx->listStats[dpy].needsAlphaScale = false;
Jeykumar Sankarancf537002013-01-21 21:19:15 -0800248 ctx->listStats[dpy].yuvCount = 0;
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800249 ctx->mDMAInUse = false;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700250
251 for (size_t i = 0; i < list->numHwLayers; i++) {
Naseer Ahmed018e5452012-12-03 14:46:15 -0500252 hwc_layer_1_t const* layer = &list->hwLayers[i];
253 private_handle_t *hnd = (private_handle_t *)layer->handle;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700254
Jeykumar Sankarancf537002013-01-21 21:19:15 -0800255 //reset stored yuv index
256 ctx->listStats[dpy].yuvIndices[i] = -1;
257
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700258 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
259 continue;
260 //We disregard FB being skip for now! so the else if
Saurabh Shah35712cb2012-09-14 10:28:18 -0700261 } else if (isSkipLayer(&list->hwLayers[i])) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700262 ctx->listStats[dpy].skipCount++;
Jeykumar Sankaran7535aba2013-02-04 11:03:09 -0800263 } else if (UNLIKELY(isYuvBuffer(hnd))) {
Jeykumar Sankarancf537002013-01-21 21:19:15 -0800264 int& yuvCount = ctx->listStats[dpy].yuvCount;
265 ctx->listStats[dpy].yuvIndices[yuvCount] = i;
266 yuvCount++;
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800267
268 if((layer->transform & HWC_TRANSFORM_ROT_90) && !ctx->mDMAInUse)
269 ctx->mDMAInUse = true;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700270 }
Jeykumar Sankaran7535aba2013-02-04 11:03:09 -0800271
272 if(!ctx->listStats[dpy].needsAlphaScale)
273 ctx->listStats[dpy].needsAlphaScale = isAlphaScaled(layer);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700274 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700275}
276
Naseer Ahmed018e5452012-12-03 14:46:15 -0500277
Saurabh Shah541b59d2012-10-11 11:08:12 -0700278static inline void calc_cut(float& leftCutRatio, float& topCutRatio,
279 float& rightCutRatio, float& bottomCutRatio, int orient) {
Saurabh Shah27c1d652012-08-14 19:30:28 -0700280 if(orient & HAL_TRANSFORM_FLIP_H) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700281 swap(leftCutRatio, rightCutRatio);
Saurabh Shah27c1d652012-08-14 19:30:28 -0700282 }
283 if(orient & HAL_TRANSFORM_FLIP_V) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700284 swap(topCutRatio, bottomCutRatio);
Saurabh Shah27c1d652012-08-14 19:30:28 -0700285 }
286 if(orient & HAL_TRANSFORM_ROT_90) {
287 //Anti clock swapping
Saurabh Shah541b59d2012-10-11 11:08:12 -0700288 float tmpCutRatio = leftCutRatio;
289 leftCutRatio = topCutRatio;
290 topCutRatio = rightCutRatio;
291 rightCutRatio = bottomCutRatio;
292 bottomCutRatio = tmpCutRatio;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700293 }
294}
295
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500296bool isSecuring(hwc_context_t* ctx) {
297 if((ctx->mMDP.version < qdutils::MDSS_V5) &&
298 (ctx->mMDP.version > qdutils::MDP_V3_0) &&
299 ctx->mSecuring) {
300 return true;
301 }
302 return false;
303}
304
Sushil Chauhan2515abf2013-01-08 16:40:05 -0800305bool isSecureModePolicy(int mdpVersion) {
306 if (mdpVersion < qdutils::MDSS_V5)
307 return true;
308 else
309 return false;
310}
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500311
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700312//Crops source buffer against destination and FB boundaries
313void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800314 const hwc_rect_t& scissor, int orient) {
315
Saurabh Shah27c1d652012-08-14 19:30:28 -0700316 int& crop_l = crop.left;
317 int& crop_t = crop.top;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700318 int& crop_r = crop.right;
319 int& crop_b = crop.bottom;
320 int crop_w = crop.right - crop.left;
321 int crop_h = crop.bottom - crop.top;
322
Saurabh Shah27c1d652012-08-14 19:30:28 -0700323 int& dst_l = dst.left;
324 int& dst_t = dst.top;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700325 int& dst_r = dst.right;
326 int& dst_b = dst.bottom;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700327 int dst_w = abs(dst.right - dst.left);
328 int dst_h = abs(dst.bottom - dst.top);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700329
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800330 const int& sci_l = scissor.left;
331 const int& sci_t = scissor.top;
332 const int& sci_r = scissor.right;
333 const int& sci_b = scissor.bottom;
334 int sci_w = abs(sci_r - sci_l);
335 int sci_h = abs(sci_b - sci_t);
336
Saurabh Shah541b59d2012-10-11 11:08:12 -0700337 float leftCutRatio = 0.0f, rightCutRatio = 0.0f, topCutRatio = 0.0f,
338 bottomCutRatio = 0.0f;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700339
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800340 if(dst_l < sci_l) {
341 leftCutRatio = (float)(sci_l - dst_l) / (float)dst_w;
342 dst_l = sci_l;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700343 }
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800344
345 if(dst_r > sci_r) {
346 rightCutRatio = (float)(dst_r - sci_r) / (float)dst_w;
347 dst_r = sci_r;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700348 }
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800349
350 if(dst_t < sci_t) {
351 topCutRatio = (float)(sci_t - dst_t) / (float)dst_h;
352 dst_t = sci_t;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700353 }
Jeykumar Sankaranb551ce42013-01-10 16:26:48 -0800354
355 if(dst_b > sci_b) {
356 bottomCutRatio = (float)(dst_b - sci_b) / (float)dst_h;
357 dst_b = sci_b;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700358 }
Saurabh Shah27c1d652012-08-14 19:30:28 -0700359
Saurabh Shah541b59d2012-10-11 11:08:12 -0700360 calc_cut(leftCutRatio, topCutRatio, rightCutRatio, bottomCutRatio, orient);
361 crop_l += crop_w * leftCutRatio;
362 crop_t += crop_h * topCutRatio;
363 crop_r -= crop_w * rightCutRatio;
364 crop_b -= crop_h * bottomCutRatio;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700365}
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700366
Naseer Ahmed64b81212013-02-14 10:29:47 -0500367void getNonWormholeRegion(hwc_display_contents_1_t* list,
368 hwc_rect_t& nwr)
369{
370 uint32_t last = list->numHwLayers - 1;
371 hwc_rect_t fbDisplayFrame = list->hwLayers[last].displayFrame;
372 //Initiliaze nwr to first frame
373 nwr.left = list->hwLayers[0].displayFrame.left;
374 nwr.top = list->hwLayers[0].displayFrame.top;
375 nwr.right = list->hwLayers[0].displayFrame.right;
376 nwr.bottom = list->hwLayers[0].displayFrame.bottom;
377
378 for (uint32_t i = 1; i < last; i++) {
379 hwc_rect_t displayFrame = list->hwLayers[i].displayFrame;
380 nwr.left = min(nwr.left, displayFrame.left);
381 nwr.top = min(nwr.top, displayFrame.top);
382 nwr.right = max(nwr.right, displayFrame.right);
383 nwr.bottom = max(nwr.bottom, displayFrame.bottom);
384 }
385
386 //Intersect with the framebuffer
387 nwr.left = max(nwr.left, fbDisplayFrame.left);
388 nwr.top = max(nwr.top, fbDisplayFrame.top);
389 nwr.right = min(nwr.right, fbDisplayFrame.right);
390 nwr.bottom = min(nwr.bottom, fbDisplayFrame.bottom);
391
392}
393
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700394bool isExternalActive(hwc_context_t* ctx) {
395 return ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive;
Saurabh Shahfc2acbe2012-08-17 19:47:52 -0700396}
397
Saurabh Shah747af1e2013-02-26 10:25:12 -0800398void closeAcquireFds(hwc_display_contents_1_t* list) {
399 for(uint32_t i = 0; list && i < list->numHwLayers; i++) {
400 //Close the acquireFenceFds
401 //HWC_FRAMEBUFFER are -1 already by SF, rest we close.
402 if(list->hwLayers[i].acquireFenceFd >= 0) {
403 close(list->hwLayers[i].acquireFenceFd);
404 list->hwLayers[i].acquireFenceFd = -1;
405 }
406 }
407}
408
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800409int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy,
410 int fd) {
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700411 int ret = 0;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700412 struct mdp_buf_sync data;
Naseer Ahmedb1c76322012-10-17 00:32:50 -0400413 int acquireFd[MAX_NUM_LAYERS];
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700414 int count = 0;
415 int releaseFd = -1;
416 int fbFd = -1;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800417 memset(&data, 0, sizeof(data));
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500418 bool swapzero = false;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700419 data.flags = MDP_BUF_SYNC_FLAG_WAIT;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700420 data.acq_fen_fd = acquireFd;
421 data.rel_fen_fd = &releaseFd;
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500422 char property[PROPERTY_VALUE_MAX];
423 if(property_get("debug.egl.swapinterval", property, "1") > 0) {
424 if(atoi(property) == 0)
425 swapzero = true;
426 }
427
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700428 //Accumulate acquireFenceFds
429 for(uint32_t i = 0; i < list->numHwLayers; i++) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800430 if(list->hwLayers[i].compositionType == HWC_OVERLAY &&
431 list->hwLayers[i].acquireFenceFd != -1) {
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500432 if(UNLIKELY(swapzero))
433 acquireFd[count++] = -1;
434 else
435 acquireFd[count++] = list->hwLayers[i].acquireFenceFd;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700436 }
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800437 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
438 if(UNLIKELY(swapzero))
439 acquireFd[count++] = -1;
440 else if(fd != -1) {
441 //set the acquireFD from fd - which is coming from c2d
442 acquireFd[count++] = fd;
443 // Buffer sync IOCTL should be async when using c2d fence is
444 // used
445 data.flags &= ~MDP_BUF_SYNC_FLAG_WAIT;
446 } else if(list->hwLayers[i].acquireFenceFd != -1)
447 acquireFd[count++] = list->hwLayers[i].acquireFenceFd;
448 }
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700449 }
450
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700451 data.acq_fen_fd_cnt = count;
452 fbFd = ctx->dpyAttr[dpy].fd;
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700453 //Waits for acquire fences, returns a release fence
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800454 if(LIKELY(!swapzero)) {
455 uint64_t start = systemTime();
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500456 ret = ioctl(fbFd, MSMFB_BUFFER_SYNC, &data);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800457 ALOGD_IF(HWC_UTILS_DEBUG, "%s: time taken for MSMFB_BUFFER_SYNC IOCTL = %d",
458 __FUNCTION__, (size_t) ns2ms(systemTime() - start));
459 }
Saurabh Shah747af1e2013-02-26 10:25:12 -0800460
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700461 if(ret < 0) {
462 ALOGE("ioctl MSMFB_BUFFER_SYNC failed, err=%s",
463 strerror(errno));
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700464 }
Saurabh Shah747af1e2013-02-26 10:25:12 -0800465
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700466 for(uint32_t i = 0; i < list->numHwLayers; i++) {
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800467 if(list->hwLayers[i].compositionType == HWC_OVERLAY ||
468 list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700469 //Populate releaseFenceFds.
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500470 if(UNLIKELY(swapzero))
471 list->hwLayers[i].releaseFenceFd = -1;
472 else
473 list->hwLayers[i].releaseFenceFd = dup(releaseFd);
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700474 }
475 }
Saurabh Shah747af1e2013-02-26 10:25:12 -0800476
477 if(fd >= 0) {
478 close(fd);
479 fd = -1;
480 }
481
Naseer Ahmed64b81212013-02-14 10:29:47 -0500482 if (ctx->mCopyBit[dpy])
483 ctx->mCopyBit[dpy]->setReleaseFd(releaseFd);
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500484 if(UNLIKELY(swapzero)){
485 list->retireFenceFd = -1;
486 close(releaseFd);
487 } else {
488 list->retireFenceFd = releaseFd;
489 }
Saurabh Shah747af1e2013-02-26 10:25:12 -0800490
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700491 return ret;
492}
493
Naseer Ahmedb1c76322012-10-17 00:32:50 -0400494void LayerCache::resetLayerCache(int num) {
495 for(uint32_t i = 0; i < MAX_NUM_LAYERS; i++) {
496 hnd[i] = NULL;
497 }
498 numHwLayers = num;
499}
500
501void LayerCache::updateLayerCache(hwc_display_contents_1_t* list) {
502
503 int numFbLayers = 0;
504 int numCacheableLayers = 0;
505
506 canUseLayerCache = false;
507 //Bail if geometry changed or num of layers changed
508 if(list->flags & HWC_GEOMETRY_CHANGED ||
509 list->numHwLayers != numHwLayers ) {
510 resetLayerCache(list->numHwLayers);
511 return;
512 }
513
514 for(uint32_t i = 0; i < list->numHwLayers; i++) {
515 //Bail on skip layers
516 if(list->hwLayers[i].flags & HWC_SKIP_LAYER) {
517 resetLayerCache(list->numHwLayers);
518 return;
519 }
520
521 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER) {
522 numFbLayers++;
523 if(hnd[i] == NULL) {
524 hnd[i] = list->hwLayers[i].handle;
525 } else if (hnd[i] ==
526 list->hwLayers[i].handle) {
527 numCacheableLayers++;
528 } else {
529 hnd[i] = NULL;
530 return;
531 }
532 } else {
533 hnd[i] = NULL;
534 }
535 }
536 if(numFbLayers == numCacheableLayers)
537 canUseLayerCache = true;
538
539 //XXX: The marking part is separate, if MDP comp wants
540 // to use it in the future. Right now getting MDP comp
541 // to use this is more trouble than it is worth.
542 markCachedLayersAsOverlay(list);
543}
544
545void LayerCache::markCachedLayersAsOverlay(hwc_display_contents_1_t* list) {
546 //This optimization only works if ALL the layer handles
547 //that were on the framebuffer didn't change.
548 if(canUseLayerCache){
549 for(uint32_t i = 0; i < list->numHwLayers; i++) {
550 if (list->hwLayers[i].handle &&
551 list->hwLayers[i].handle == hnd[i] &&
552 list->hwLayers[i].compositionType != HWC_FRAMEBUFFER_TARGET)
553 {
554 list->hwLayers[i].compositionType = HWC_OVERLAY;
555 }
556 }
557 }
558
559}
560
Naseer Ahmed29a26812012-06-14 00:56:20 -0700561};//namespace