blob: ac9a7dc28df221539b4c2f4be78127f5b85d0249 [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"
Naseer Ahmeda87da602012-07-01 23:54:19 -070026#include "mdp_version.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070027#include "external.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070028#include "QService.h"
Naseer Ahmed72cf9762012-07-21 12:17:13 -070029
Naseer Ahmed29a26812012-06-14 00:56:20 -070030namespace qhwc {
Naseer Ahmed72cf9762012-07-21 12:17:13 -070031
32// Opens Framebuffer device
33static void openFramebufferDevice(hwc_context_t *ctx)
34{
35 hw_module_t const *module;
36 if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module) == 0) {
37 framebuffer_open(module, &(ctx->mFbDev));
Saurabh Shah3e858eb2012-09-17 16:53:21 -070038 private_module_t* m = reinterpret_cast<private_module_t*>(
39 ctx->mFbDev->common.module);
40 //xres, yres may not be 32 aligned
Naseer Ahmed54821fe2012-11-28 18:44:38 -050041 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].stride = m->finfo.line_length /
42 (m->info.xres/8);
Saurabh Shah3e858eb2012-09-17 16:53:21 -070043 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres = m->info.xres;
44 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].yres = m->info.yres;
45 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xdpi = ctx->mFbDev->xdpi;
46 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].ydpi = ctx->mFbDev->ydpi;
47 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period =
48 1000000000l / ctx->mFbDev->fps;
49 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = openFb(HWC_DISPLAY_PRIMARY);
Naseer Ahmed72cf9762012-07-21 12:17:13 -070050 }
51}
52
Naseer Ahmed29a26812012-06-14 00:56:20 -070053void initContext(hwc_context_t *ctx)
54{
Naseer Ahmed72cf9762012-07-21 12:17:13 -070055 openFramebufferDevice(ctx);
Saurabh Shahc4d034f2012-09-27 15:55:15 -070056 overlay::Overlay::initOverlay();
Naseer Ahmed758bfc52012-11-28 17:02:08 -050057 ctx->mOverlay = overlay::Overlay::getInstance();
Saurabh Shah56f610d2012-08-07 15:27:06 -070058 ctx->mQService = qService::QService::getInstance(ctx);
Naseer Ahmed96c4c952012-07-25 18:27:14 -070059 ctx->mMDP.version = qdutils::MDPVersion::getInstance().getMDPVersion();
60 ctx->mMDP.hasOverlay = qdutils::MDPVersion::getInstance().hasOverlay();
61 ctx->mMDP.panel = qdutils::MDPVersion::getInstance().getPanelType();
Naseer Ahmed72cf9762012-07-21 12:17:13 -070062 ctx->mExtDisplay = new ExternalDisplay(ctx);
Naseer Ahmedb1c76322012-10-17 00:32:50 -040063 ctx->mLayerCache = new LayerCache();
Naseer Ahmed54821fe2012-11-28 18:44:38 -050064 MDPComp::init(ctx);
Naseer Ahmed72cf9762012-07-21 12:17:13 -070065
Naseer Ahmedff4f0252012-10-01 13:03:01 -040066 pthread_mutex_init(&(ctx->vstate.lock), NULL);
67 pthread_cond_init(&(ctx->vstate.cond), NULL);
68 ctx->vstate.enable = false;
69
Naseer Ahmed72cf9762012-07-21 12:17:13 -070070 ALOGI("Initializing Qualcomm Hardware Composer");
Naseer Ahmed96c4c952012-07-25 18:27:14 -070071 ALOGI("MDP version: %d", ctx->mMDP.version);
Naseer Ahmed29a26812012-06-14 00:56:20 -070072}
73
74void closeContext(hwc_context_t *ctx)
75{
Naseer Ahmed758bfc52012-11-28 17:02:08 -050076 if(ctx->mOverlay) {
77 delete ctx->mOverlay;
78 ctx->mOverlay = NULL;
Naseer Ahmed29a26812012-06-14 00:56:20 -070079 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -070080
Naseer Ahmed72cf9762012-07-21 12:17:13 -070081 if(ctx->mFbDev) {
82 framebuffer_close(ctx->mFbDev);
83 ctx->mFbDev = NULL;
Saurabh Shah3e858eb2012-09-17 16:53:21 -070084 close(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd);
85 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = -1;
Naseer Ahmed29a26812012-06-14 00:56:20 -070086 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -070087
88 if(ctx->mExtDisplay) {
89 delete ctx->mExtDisplay;
90 ctx->mExtDisplay = NULL;
91 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -040092
93 pthread_mutex_destroy(&(ctx->vstate.lock));
94 pthread_cond_destroy(&(ctx->vstate.cond));
95
Naseer Ahmed29a26812012-06-14 00:56:20 -070096}
97
Naseer Ahmed5b6708a2012-08-02 13:46:08 -070098void dumpLayer(hwc_layer_1_t const* l)
Naseer Ahmed29a26812012-06-14 00:56:20 -070099{
100 ALOGD("\ttype=%d, flags=%08x, handle=%p, tr=%02x, blend=%04x, {%d,%d,%d,%d}"
101 ", {%d,%d,%d,%d}",
102 l->compositionType, l->flags, l->handle, l->transform, l->blending,
103 l->sourceCrop.left,
104 l->sourceCrop.top,
105 l->sourceCrop.right,
106 l->sourceCrop.bottom,
107 l->displayFrame.left,
108 l->displayFrame.top,
109 l->displayFrame.right,
110 l->displayFrame.bottom);
111}
112
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700113void setListStats(hwc_context_t *ctx,
114 const hwc_display_contents_1_t *list, int dpy) {
115
116 ctx->listStats[dpy].numAppLayers = list->numHwLayers - 1;
117 ctx->listStats[dpy].fbLayerIndex = list->numHwLayers - 1;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700118 ctx->listStats[dpy].yuvCount = 0;
119 ctx->listStats[dpy].yuvIndex = -1;
120 ctx->listStats[dpy].skipCount = 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700121
122 for (size_t i = 0; i < list->numHwLayers; i++) {
123 private_handle_t *hnd =
124 (private_handle_t *)list->hwLayers[i].handle;
125
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700126 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
127 continue;
128 //We disregard FB being skip for now! so the else if
Saurabh Shah35712cb2012-09-14 10:28:18 -0700129 } else if (isSkipLayer(&list->hwLayers[i])) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700130 ctx->listStats[dpy].skipCount++;
131 }
132
133 if (UNLIKELY(isYuvBuffer(hnd))) {
134 ctx->listStats[dpy].yuvCount++;
135 ctx->listStats[dpy].yuvIndex = i;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700136 }
137 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700138}
139
Saurabh Shah541b59d2012-10-11 11:08:12 -0700140static inline void calc_cut(float& leftCutRatio, float& topCutRatio,
141 float& rightCutRatio, float& bottomCutRatio, int orient) {
Saurabh Shah27c1d652012-08-14 19:30:28 -0700142 if(orient & HAL_TRANSFORM_FLIP_H) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700143 swap(leftCutRatio, rightCutRatio);
Saurabh Shah27c1d652012-08-14 19:30:28 -0700144 }
145 if(orient & HAL_TRANSFORM_FLIP_V) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700146 swap(topCutRatio, bottomCutRatio);
Saurabh Shah27c1d652012-08-14 19:30:28 -0700147 }
148 if(orient & HAL_TRANSFORM_ROT_90) {
149 //Anti clock swapping
Saurabh Shah541b59d2012-10-11 11:08:12 -0700150 float tmpCutRatio = leftCutRatio;
151 leftCutRatio = topCutRatio;
152 topCutRatio = rightCutRatio;
153 rightCutRatio = bottomCutRatio;
154 bottomCutRatio = tmpCutRatio;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700155 }
156}
157
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500158bool isSecuring(hwc_context_t* ctx) {
159 if((ctx->mMDP.version < qdutils::MDSS_V5) &&
160 (ctx->mMDP.version > qdutils::MDP_V3_0) &&
161 ctx->mSecuring) {
162 return true;
163 }
164 return false;
165}
166
167
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700168//Crops source buffer against destination and FB boundaries
169void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
Saurabh Shah27c1d652012-08-14 19:30:28 -0700170 const int fbWidth, const int fbHeight, int orient) {
171 int& crop_l = crop.left;
172 int& crop_t = crop.top;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700173 int& crop_r = crop.right;
174 int& crop_b = crop.bottom;
175 int crop_w = crop.right - crop.left;
176 int crop_h = crop.bottom - crop.top;
177
Saurabh Shah27c1d652012-08-14 19:30:28 -0700178 int& dst_l = dst.left;
179 int& dst_t = dst.top;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700180 int& dst_r = dst.right;
181 int& dst_b = dst.bottom;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700182 int dst_w = abs(dst.right - dst.left);
183 int dst_h = abs(dst.bottom - dst.top);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700184
Saurabh Shah541b59d2012-10-11 11:08:12 -0700185 float leftCutRatio = 0.0f, rightCutRatio = 0.0f, topCutRatio = 0.0f,
186 bottomCutRatio = 0.0f;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700187
Saurabh Shah27c1d652012-08-14 19:30:28 -0700188 if(dst_l < 0) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700189 leftCutRatio = (float)(0.0f - dst_l) / (float)dst_w;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700190 dst_l = 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700191 }
192 if(dst_r > fbWidth) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700193 rightCutRatio = (float)(dst_r - fbWidth) / (float)dst_w;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700194 dst_r = fbWidth;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700195 }
Saurabh Shah27c1d652012-08-14 19:30:28 -0700196 if(dst_t < 0) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700197 topCutRatio = (float)(0 - dst_t) / (float)dst_h;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700198 dst_t = 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700199 }
200 if(dst_b > fbHeight) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700201 bottomCutRatio = (float)(dst_b - fbHeight) / (float)dst_h;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700202 dst_b = fbHeight;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700203 }
Saurabh Shah27c1d652012-08-14 19:30:28 -0700204
Saurabh Shah541b59d2012-10-11 11:08:12 -0700205 calc_cut(leftCutRatio, topCutRatio, rightCutRatio, bottomCutRatio, orient);
206 crop_l += crop_w * leftCutRatio;
207 crop_t += crop_h * topCutRatio;
208 crop_r -= crop_w * rightCutRatio;
209 crop_b -= crop_h * bottomCutRatio;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700210}
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700211
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700212bool isExternalActive(hwc_context_t* ctx) {
213 return ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive;
Saurabh Shahfc2acbe2012-08-17 19:47:52 -0700214}
215
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700216int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy) {
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700217 int ret = 0;
218#ifdef USE_FENCE_SYNC
219 struct mdp_buf_sync data;
Naseer Ahmedb1c76322012-10-17 00:32:50 -0400220 int acquireFd[MAX_NUM_LAYERS];
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700221 int count = 0;
222 int releaseFd = -1;
223 int fbFd = -1;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700224 data.flags = MDP_BUF_SYNC_FLAG_WAIT;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700225 data.acq_fen_fd = acquireFd;
226 data.rel_fen_fd = &releaseFd;
227 //Accumulate acquireFenceFds
228 for(uint32_t i = 0; i < list->numHwLayers; i++) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700229 if((list->hwLayers[i].compositionType == HWC_OVERLAY ||
230 list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) &&
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500231 list->hwLayers[i].acquireFenceFd != -1 ){
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700232 acquireFd[count++] = list->hwLayers[i].acquireFenceFd;
233 }
234 }
235
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700236 data.acq_fen_fd_cnt = count;
237 fbFd = ctx->dpyAttr[dpy].fd;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700238
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700239 //Waits for acquire fences, returns a release fence
240 ret = ioctl(fbFd, MSMFB_BUFFER_SYNC, &data);
241 if(ret < 0) {
242 ALOGE("ioctl MSMFB_BUFFER_SYNC failed, err=%s",
243 strerror(errno));
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700244 }
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700245
246 for(uint32_t i = 0; i < list->numHwLayers; i++) {
247 if((list->hwLayers[i].compositionType == HWC_OVERLAY ||
248 list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET)) {
249 //Close the acquireFenceFds
250 if(list->hwLayers[i].acquireFenceFd > 0) {
251 close(list->hwLayers[i].acquireFenceFd);
252 list->hwLayers[i].acquireFenceFd = -1;
253 }
254 //Populate releaseFenceFds.
255 list->hwLayers[i].releaseFenceFd = dup(releaseFd);
256 }
257 }
258 list->retireFenceFd = releaseFd;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700259#endif
260 return ret;
261}
262
Naseer Ahmedb1c76322012-10-17 00:32:50 -0400263void LayerCache::resetLayerCache(int num) {
264 for(uint32_t i = 0; i < MAX_NUM_LAYERS; i++) {
265 hnd[i] = NULL;
266 }
267 numHwLayers = num;
268}
269
270void LayerCache::updateLayerCache(hwc_display_contents_1_t* list) {
271
272 int numFbLayers = 0;
273 int numCacheableLayers = 0;
274
275 canUseLayerCache = false;
276 //Bail if geometry changed or num of layers changed
277 if(list->flags & HWC_GEOMETRY_CHANGED ||
278 list->numHwLayers != numHwLayers ) {
279 resetLayerCache(list->numHwLayers);
280 return;
281 }
282
283 for(uint32_t i = 0; i < list->numHwLayers; i++) {
284 //Bail on skip layers
285 if(list->hwLayers[i].flags & HWC_SKIP_LAYER) {
286 resetLayerCache(list->numHwLayers);
287 return;
288 }
289
290 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER) {
291 numFbLayers++;
292 if(hnd[i] == NULL) {
293 hnd[i] = list->hwLayers[i].handle;
294 } else if (hnd[i] ==
295 list->hwLayers[i].handle) {
296 numCacheableLayers++;
297 } else {
298 hnd[i] = NULL;
299 return;
300 }
301 } else {
302 hnd[i] = NULL;
303 }
304 }
305 if(numFbLayers == numCacheableLayers)
306 canUseLayerCache = true;
307
308 //XXX: The marking part is separate, if MDP comp wants
309 // to use it in the future. Right now getting MDP comp
310 // to use this is more trouble than it is worth.
311 markCachedLayersAsOverlay(list);
312}
313
314void LayerCache::markCachedLayersAsOverlay(hwc_display_contents_1_t* list) {
315 //This optimization only works if ALL the layer handles
316 //that were on the framebuffer didn't change.
317 if(canUseLayerCache){
318 for(uint32_t i = 0; i < list->numHwLayers; i++) {
319 if (list->hwLayers[i].handle &&
320 list->hwLayers[i].handle == hnd[i] &&
321 list->hwLayers[i].compositionType != HWC_FRAMEBUFFER_TARGET)
322 {
323 list->hwLayers[i].compositionType = HWC_OVERLAY;
324 }
325 }
326 }
327
328}
329
Naseer Ahmed29a26812012-06-14 00:56:20 -0700330};//namespace