blob: 7ae4afd3bbe05ebe9116ba0e9bd218545d60ecb5 [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
Saurabh Shah56f610d2012-08-07 15:27:06 -07003 * Copyright (C) 2012, The Linux Foundation All rights reserved.
Naseer Ahmed29a26812012-06-14 00:56:20 -07004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Naseer Ahmed758bfc52012-11-28 17:02:08 -050018#include <sys/ioctl.h>
Naseer Ahmed5b6708a2012-08-02 13:46:08 -070019#include <EGL/egl.h>
Saurabh Shahfc2acbe2012-08-17 19:47:52 -070020#include <cutils/properties.h>
21#include <gralloc_priv.h>
22#include <fb_priv.h>
Naseer Ahmed758bfc52012-11-28 17:02:08 -050023#include <overlay.h>
Naseer Ahmed29a26812012-06-14 00:56:20 -070024#include "hwc_utils.h"
Naseer Ahmed54821fe2012-11-28 18:44:38 -050025#include "hwc_mdpcomp.h"
Saurabh Shahcf053c62012-12-13 12:32:55 -080026#include "hwc_fbupdate.h"
Naseer Ahmeda87da602012-07-01 23:54:19 -070027#include "mdp_version.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070028#include "external.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070029#include "QService.h"
Naseer Ahmed72cf9762012-07-21 12:17:13 -070030
Naseer Ahmed29a26812012-06-14 00:56:20 -070031namespace qhwc {
Naseer Ahmed72cf9762012-07-21 12:17:13 -070032
33// Opens Framebuffer device
34static void openFramebufferDevice(hwc_context_t *ctx)
35{
36 hw_module_t const *module;
37 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
38 framebuffer_open(module, &(ctx->mFbDev));
Saurabh Shah3e858eb2012-09-17 16:53:21 -070039 private_module_t* m = reinterpret_cast<private_module_t*>(
40 ctx->mFbDev->common.module);
41 //xres, yres may not be 32 aligned
Naseer Ahmed54821fe2012-11-28 18:44:38 -050042 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].stride = m->finfo.line_length /
43 (m->info.xres/8);
Saurabh Shah3e858eb2012-09-17 16:53:21 -070044 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres = m->info.xres;
45 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].yres = m->info.yres;
46 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xdpi = ctx->mFbDev->xdpi;
47 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].ydpi = ctx->mFbDev->ydpi;
48 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period =
49 1000000000l / ctx->mFbDev->fps;
50 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = openFb(HWC_DISPLAY_PRIMARY);
Naseer Ahmed72cf9762012-07-21 12:17:13 -070051 }
52}
53
Naseer Ahmed29a26812012-06-14 00:56:20 -070054void initContext(hwc_context_t *ctx)
55{
Naseer Ahmed72cf9762012-07-21 12:17:13 -070056 openFramebufferDevice(ctx);
Saurabh Shahc4d034f2012-09-27 15:55:15 -070057 overlay::Overlay::initOverlay();
Naseer Ahmed758bfc52012-11-28 17:02:08 -050058 ctx->mOverlay = overlay::Overlay::getInstance();
Saurabh Shah56f610d2012-08-07 15:27:06 -070059 ctx->mQService = qService::QService::getInstance(ctx);
Naseer Ahmed96c4c952012-07-25 18:27:14 -070060 ctx->mMDP.version = qdutils::MDPVersion::getInstance().getMDPVersion();
61 ctx->mMDP.hasOverlay = qdutils::MDPVersion::getInstance().hasOverlay();
62 ctx->mMDP.panel = qdutils::MDPVersion::getInstance().getPanelType();
Saurabh Shahcf053c62012-12-13 12:32:55 -080063 //Is created and destroyed only once for primary
64 //For external it could get created and destroyed multiple times depending
65 //on what external we connect to.
66 ctx->mFBUpdate[HWC_DISPLAY_PRIMARY] =
67 IFBUpdate::getObject(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres,
68 HWC_DISPLAY_PRIMARY);
Naseer Ahmed72cf9762012-07-21 12:17:13 -070069 ctx->mExtDisplay = new ExternalDisplay(ctx);
Naseer Ahmede78f0522012-12-07 18:24:28 -050070 for (uint32_t i = 0; i < HWC_NUM_DISPLAY_TYPES; i++)
71 ctx->mLayerCache[i] = new LayerCache();
Naseer Ahmed54821fe2012-11-28 18:44:38 -050072 MDPComp::init(ctx);
Naseer Ahmed72cf9762012-07-21 12:17:13 -070073
Naseer Ahmedff4f0252012-10-01 13:03:01 -040074 pthread_mutex_init(&(ctx->vstate.lock), NULL);
75 pthread_cond_init(&(ctx->vstate.cond), NULL);
76 ctx->vstate.enable = false;
77
Naseer Ahmed72cf9762012-07-21 12:17:13 -070078 ALOGI("Initializing Qualcomm Hardware Composer");
Naseer Ahmed96c4c952012-07-25 18:27:14 -070079 ALOGI("MDP version: %d", ctx->mMDP.version);
Naseer Ahmed29a26812012-06-14 00:56:20 -070080}
81
82void closeContext(hwc_context_t *ctx)
83{
Naseer Ahmed758bfc52012-11-28 17:02:08 -050084 if(ctx->mOverlay) {
85 delete ctx->mOverlay;
86 ctx->mOverlay = NULL;
Naseer Ahmed29a26812012-06-14 00:56:20 -070087 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -070088
Naseer Ahmed72cf9762012-07-21 12:17:13 -070089 if(ctx->mFbDev) {
90 framebuffer_close(ctx->mFbDev);
91 ctx->mFbDev = NULL;
Saurabh Shah3e858eb2012-09-17 16:53:21 -070092 close(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd);
93 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = -1;
Naseer Ahmed29a26812012-06-14 00:56:20 -070094 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -070095
96 if(ctx->mExtDisplay) {
97 delete ctx->mExtDisplay;
98 ctx->mExtDisplay = NULL;
99 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400100
Saurabh Shahcf053c62012-12-13 12:32:55 -0800101 for(int i = 0; i < HWC_NUM_DISPLAY_TYPES; i++) {
102 if(ctx->mFBUpdate[i]) {
103 delete ctx->mFBUpdate[i];
104 ctx->mFBUpdate[i] = NULL;
105 }
106 }
107
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400108 pthread_mutex_destroy(&(ctx->vstate.lock));
109 pthread_cond_destroy(&(ctx->vstate.cond));
Naseer Ahmed29a26812012-06-14 00:56:20 -0700110}
111
Naseer Ahmed1d183f52012-11-26 12:35:16 -0500112
113void dumpsys_log(android::String8& buf, const char* fmt, ...)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700114{
Naseer Ahmed1d183f52012-11-26 12:35:16 -0500115 va_list varargs;
116 va_start(varargs, fmt);
117 buf.appendFormatV(fmt, varargs);
118 va_end(varargs);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700119}
120
Naseer Ahmed018e5452012-12-03 14:46:15 -0500121static inline bool isAlphaScaled(hwc_layer_1_t const* layer) {
122 int dst_w, dst_h, src_w, src_h;
123
124 hwc_rect_t displayFrame = layer->displayFrame;
125 hwc_rect_t sourceCrop = layer->sourceCrop;
126
127 dst_w = displayFrame.right - displayFrame.left;
128 dst_h = displayFrame.bottom - displayFrame.top;
129
130 src_w = sourceCrop.right - sourceCrop.left;
131 src_h = sourceCrop.bottom - sourceCrop.top;
132
133 if(((src_w != dst_w) || (src_h != dst_h))) {
134 if(layer->blending != HWC_BLENDING_NONE)
135 return true;
136 }
137 return false;
138}
139
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700140void setListStats(hwc_context_t *ctx,
141 const hwc_display_contents_1_t *list, int dpy) {
142
143 ctx->listStats[dpy].numAppLayers = list->numHwLayers - 1;
144 ctx->listStats[dpy].fbLayerIndex = list->numHwLayers - 1;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700145 ctx->listStats[dpy].yuvCount = 0;
146 ctx->listStats[dpy].yuvIndex = -1;
147 ctx->listStats[dpy].skipCount = 0;
Naseer Ahmed018e5452012-12-03 14:46:15 -0500148 ctx->listStats[dpy].needsAlphaScale = false;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700149
150 for (size_t i = 0; i < list->numHwLayers; i++) {
Naseer Ahmed018e5452012-12-03 14:46:15 -0500151 hwc_layer_1_t const* layer = &list->hwLayers[i];
152 private_handle_t *hnd = (private_handle_t *)layer->handle;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700153
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700154 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
155 continue;
156 //We disregard FB being skip for now! so the else if
Saurabh Shah35712cb2012-09-14 10:28:18 -0700157 } else if (isSkipLayer(&list->hwLayers[i])) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700158 ctx->listStats[dpy].skipCount++;
159 }
160
Naseer Ahmed018e5452012-12-03 14:46:15 -0500161 if(!ctx->listStats[dpy].needsAlphaScale)
162 ctx->listStats[dpy].needsAlphaScale = isAlphaScaled(layer);
163
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700164 if (UNLIKELY(isYuvBuffer(hnd))) {
165 ctx->listStats[dpy].yuvCount++;
166 ctx->listStats[dpy].yuvIndex = i;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700167 }
168 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700169}
170
Naseer Ahmed018e5452012-12-03 14:46:15 -0500171
Saurabh Shah541b59d2012-10-11 11:08:12 -0700172static inline void calc_cut(float& leftCutRatio, float& topCutRatio,
173 float& rightCutRatio, float& bottomCutRatio, int orient) {
Saurabh Shah27c1d652012-08-14 19:30:28 -0700174 if(orient & HAL_TRANSFORM_FLIP_H) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700175 swap(leftCutRatio, rightCutRatio);
Saurabh Shah27c1d652012-08-14 19:30:28 -0700176 }
177 if(orient & HAL_TRANSFORM_FLIP_V) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700178 swap(topCutRatio, bottomCutRatio);
Saurabh Shah27c1d652012-08-14 19:30:28 -0700179 }
180 if(orient & HAL_TRANSFORM_ROT_90) {
181 //Anti clock swapping
Saurabh Shah541b59d2012-10-11 11:08:12 -0700182 float tmpCutRatio = leftCutRatio;
183 leftCutRatio = topCutRatio;
184 topCutRatio = rightCutRatio;
185 rightCutRatio = bottomCutRatio;
186 bottomCutRatio = tmpCutRatio;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700187 }
188}
189
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500190bool isSecuring(hwc_context_t* ctx) {
191 if((ctx->mMDP.version < qdutils::MDSS_V5) &&
192 (ctx->mMDP.version > qdutils::MDP_V3_0) &&
193 ctx->mSecuring) {
194 return true;
195 }
196 return false;
197}
198
199
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700200//Crops source buffer against destination and FB boundaries
201void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
Saurabh Shah27c1d652012-08-14 19:30:28 -0700202 const int fbWidth, const int fbHeight, int orient) {
203 int& crop_l = crop.left;
204 int& crop_t = crop.top;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700205 int& crop_r = crop.right;
206 int& crop_b = crop.bottom;
207 int crop_w = crop.right - crop.left;
208 int crop_h = crop.bottom - crop.top;
209
Saurabh Shah27c1d652012-08-14 19:30:28 -0700210 int& dst_l = dst.left;
211 int& dst_t = dst.top;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700212 int& dst_r = dst.right;
213 int& dst_b = dst.bottom;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700214 int dst_w = abs(dst.right - dst.left);
215 int dst_h = abs(dst.bottom - dst.top);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700216
Saurabh Shah541b59d2012-10-11 11:08:12 -0700217 float leftCutRatio = 0.0f, rightCutRatio = 0.0f, topCutRatio = 0.0f,
218 bottomCutRatio = 0.0f;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700219
Saurabh Shah27c1d652012-08-14 19:30:28 -0700220 if(dst_l < 0) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700221 leftCutRatio = (float)(0.0f - dst_l) / (float)dst_w;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700222 dst_l = 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700223 }
224 if(dst_r > fbWidth) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700225 rightCutRatio = (float)(dst_r - fbWidth) / (float)dst_w;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700226 dst_r = fbWidth;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700227 }
Saurabh Shah27c1d652012-08-14 19:30:28 -0700228 if(dst_t < 0) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700229 topCutRatio = (float)(0 - dst_t) / (float)dst_h;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700230 dst_t = 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700231 }
232 if(dst_b > fbHeight) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700233 bottomCutRatio = (float)(dst_b - fbHeight) / (float)dst_h;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700234 dst_b = fbHeight;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700235 }
Saurabh Shah27c1d652012-08-14 19:30:28 -0700236
Saurabh Shah541b59d2012-10-11 11:08:12 -0700237 calc_cut(leftCutRatio, topCutRatio, rightCutRatio, bottomCutRatio, orient);
238 crop_l += crop_w * leftCutRatio;
239 crop_t += crop_h * topCutRatio;
240 crop_r -= crop_w * rightCutRatio;
241 crop_b -= crop_h * bottomCutRatio;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700242}
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700243
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700244bool isExternalActive(hwc_context_t* ctx) {
245 return ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive;
Saurabh Shahfc2acbe2012-08-17 19:47:52 -0700246}
247
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700248int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy) {
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700249 int ret = 0;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700250 struct mdp_buf_sync data;
Naseer Ahmedb1c76322012-10-17 00:32:50 -0400251 int acquireFd[MAX_NUM_LAYERS];
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700252 int count = 0;
253 int releaseFd = -1;
254 int fbFd = -1;
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500255 bool swapzero = false;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700256 data.flags = MDP_BUF_SYNC_FLAG_WAIT;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700257 data.acq_fen_fd = acquireFd;
258 data.rel_fen_fd = &releaseFd;
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500259 char property[PROPERTY_VALUE_MAX];
260 if(property_get("debug.egl.swapinterval", property, "1") > 0) {
261 if(atoi(property) == 0)
262 swapzero = true;
263 }
264
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700265 //Accumulate acquireFenceFds
266 for(uint32_t i = 0; i < list->numHwLayers; i++) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700267 if((list->hwLayers[i].compositionType == HWC_OVERLAY ||
268 list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) &&
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500269 list->hwLayers[i].acquireFenceFd != -1 ){
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500270 if(UNLIKELY(swapzero))
271 acquireFd[count++] = -1;
272 else
273 acquireFd[count++] = list->hwLayers[i].acquireFenceFd;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700274 }
275 }
276
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700277 data.acq_fen_fd_cnt = count;
278 fbFd = ctx->dpyAttr[dpy].fd;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700279
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700280 //Waits for acquire fences, returns a release fence
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500281 if(LIKELY(!swapzero))
282 ret = ioctl(fbFd, MSMFB_BUFFER_SYNC, &data);
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700283 if(ret < 0) {
284 ALOGE("ioctl MSMFB_BUFFER_SYNC failed, err=%s",
285 strerror(errno));
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700286 }
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700287
288 for(uint32_t i = 0; i < list->numHwLayers; i++) {
289 if((list->hwLayers[i].compositionType == HWC_OVERLAY ||
290 list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET)) {
291 //Close the acquireFenceFds
292 if(list->hwLayers[i].acquireFenceFd > 0) {
293 close(list->hwLayers[i].acquireFenceFd);
294 list->hwLayers[i].acquireFenceFd = -1;
295 }
296 //Populate releaseFenceFds.
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500297 if(UNLIKELY(swapzero))
298 list->hwLayers[i].releaseFenceFd = -1;
299 else
300 list->hwLayers[i].releaseFenceFd = dup(releaseFd);
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700301 }
302 }
Naseer Ahmed94baddc2012-12-17 18:51:01 -0500303 if(UNLIKELY(swapzero)){
304 list->retireFenceFd = -1;
305 close(releaseFd);
306 } else {
307 list->retireFenceFd = releaseFd;
308 }
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700309 return ret;
310}
311
Naseer Ahmedb1c76322012-10-17 00:32:50 -0400312void LayerCache::resetLayerCache(int num) {
313 for(uint32_t i = 0; i < MAX_NUM_LAYERS; i++) {
314 hnd[i] = NULL;
315 }
316 numHwLayers = num;
317}
318
319void LayerCache::updateLayerCache(hwc_display_contents_1_t* list) {
320
321 int numFbLayers = 0;
322 int numCacheableLayers = 0;
323
324 canUseLayerCache = false;
325 //Bail if geometry changed or num of layers changed
326 if(list->flags & HWC_GEOMETRY_CHANGED ||
327 list->numHwLayers != numHwLayers ) {
328 resetLayerCache(list->numHwLayers);
329 return;
330 }
331
332 for(uint32_t i = 0; i < list->numHwLayers; i++) {
333 //Bail on skip layers
334 if(list->hwLayers[i].flags & HWC_SKIP_LAYER) {
335 resetLayerCache(list->numHwLayers);
336 return;
337 }
338
339 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER) {
340 numFbLayers++;
341 if(hnd[i] == NULL) {
342 hnd[i] = list->hwLayers[i].handle;
343 } else if (hnd[i] ==
344 list->hwLayers[i].handle) {
345 numCacheableLayers++;
346 } else {
347 hnd[i] = NULL;
348 return;
349 }
350 } else {
351 hnd[i] = NULL;
352 }
353 }
354 if(numFbLayers == numCacheableLayers)
355 canUseLayerCache = true;
356
357 //XXX: The marking part is separate, if MDP comp wants
358 // to use it in the future. Right now getting MDP comp
359 // to use this is more trouble than it is worth.
360 markCachedLayersAsOverlay(list);
361}
362
363void LayerCache::markCachedLayersAsOverlay(hwc_display_contents_1_t* list) {
364 //This optimization only works if ALL the layer handles
365 //that were on the framebuffer didn't change.
366 if(canUseLayerCache){
367 for(uint32_t i = 0; i < list->numHwLayers; i++) {
368 if (list->hwLayers[i].handle &&
369 list->hwLayers[i].handle == hnd[i] &&
370 list->hwLayers[i].compositionType != HWC_FRAMEBUFFER_TARGET)
371 {
372 list->hwLayers[i].compositionType = HWC_OVERLAY;
373 }
374 }
375 }
376
377}
378
Naseer Ahmed29a26812012-06-14 00:56:20 -0700379};//namespace