blob: 41a9c5a020bf47c7de7b6bf12a85ffe896210c59 [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 Ahmed5b6708a2012-08-02 13:46:08 -070018#include <EGL/egl.h>
Naseer Ahmed72cf9762012-07-21 12:17:13 -070019#include <overlay.h>
Saurabh Shahfc2acbe2012-08-17 19:47:52 -070020#include <cutils/properties.h>
21#include <gralloc_priv.h>
22#include <fb_priv.h>
Naseer Ahmed29a26812012-06-14 00:56:20 -070023#include "hwc_utils.h"
Naseer Ahmeda87da602012-07-01 23:54:19 -070024#include "mdp_version.h"
Naseer Ahmedf48aef62012-07-20 09:05:53 -070025#include "hwc_video.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070026#include "external.h"
Naseer Ahmed7c958d42012-07-31 18:57:03 -070027#include "hwc_mdpcomp.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
41 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xres = m->info.xres;
42 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].yres = m->info.yres;
43 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].xdpi = ctx->mFbDev->xdpi;
44 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].ydpi = ctx->mFbDev->ydpi;
45 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].vsync_period =
46 1000000000l / ctx->mFbDev->fps;
47 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = openFb(HWC_DISPLAY_PRIMARY);
Naseer Ahmed72cf9762012-07-21 12:17:13 -070048 }
49}
50
Naseer Ahmed29a26812012-06-14 00:56:20 -070051void initContext(hwc_context_t *ctx)
52{
Naseer Ahmed72cf9762012-07-21 12:17:13 -070053 openFramebufferDevice(ctx);
Saurabh Shahc4d034f2012-09-27 15:55:15 -070054 overlay::Overlay::initOverlay();
55 for(uint32_t i = 0; i < HWC_NUM_DISPLAY_TYPES; i++) {
56 ctx->mOverlay[i] = overlay::Overlay::getInstance(i);
57 }
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 Ahmed7c958d42012-07-31 18:57:03 -070063 MDPComp::init(ctx);
Naseer Ahmed72cf9762012-07-21 12:17:13 -070064
Naseer Ahmedff4f0252012-10-01 13:03:01 -040065 pthread_mutex_init(&(ctx->vstate.lock), NULL);
66 pthread_cond_init(&(ctx->vstate.cond), NULL);
67 ctx->vstate.enable = false;
68
Naseer Ahmed72cf9762012-07-21 12:17:13 -070069 ALOGI("Initializing Qualcomm Hardware Composer");
Naseer Ahmed96c4c952012-07-25 18:27:14 -070070 ALOGI("MDP version: %d", ctx->mMDP.version);
Naseer Ahmed29a26812012-06-14 00:56:20 -070071}
72
73void closeContext(hwc_context_t *ctx)
74{
Saurabh Shahc4d034f2012-09-27 15:55:15 -070075 for(uint32_t i = 0; i < HWC_NUM_DISPLAY_TYPES; i++) {
76 if(ctx->mOverlay[i]) {
77 delete ctx->mOverlay[i];
78 ctx->mOverlay[i] = NULL;
79 }
Naseer Ahmed29a26812012-06-14 00:56:20 -070080 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -070081
Naseer Ahmed72cf9762012-07-21 12:17:13 -070082 if(ctx->mFbDev) {
83 framebuffer_close(ctx->mFbDev);
84 ctx->mFbDev = NULL;
Saurabh Shah3e858eb2012-09-17 16:53:21 -070085 close(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd);
86 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = -1;
Naseer Ahmed29a26812012-06-14 00:56:20 -070087 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -070088
89 if(ctx->mExtDisplay) {
90 delete ctx->mExtDisplay;
91 ctx->mExtDisplay = NULL;
92 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -040093
94 pthread_mutex_destroy(&(ctx->vstate.lock));
95 pthread_cond_destroy(&(ctx->vstate.cond));
96
Naseer Ahmed29a26812012-06-14 00:56:20 -070097}
98
Naseer Ahmed5b6708a2012-08-02 13:46:08 -070099void dumpLayer(hwc_layer_1_t const* l)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700100{
101 ALOGD("\ttype=%d, flags=%08x, handle=%p, tr=%02x, blend=%04x, {%d,%d,%d,%d}"
102 ", {%d,%d,%d,%d}",
103 l->compositionType, l->flags, l->handle, l->transform, l->blending,
104 l->sourceCrop.left,
105 l->sourceCrop.top,
106 l->sourceCrop.right,
107 l->sourceCrop.bottom,
108 l->displayFrame.left,
109 l->displayFrame.top,
110 l->displayFrame.right,
111 l->displayFrame.bottom);
112}
113
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700114void setListStats(hwc_context_t *ctx,
115 const hwc_display_contents_1_t *list, int dpy) {
116
117 ctx->listStats[dpy].numAppLayers = list->numHwLayers - 1;
118 ctx->listStats[dpy].fbLayerIndex = list->numHwLayers - 1;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700119 ctx->listStats[dpy].yuvCount = 0;
120 ctx->listStats[dpy].yuvIndex = -1;
121 ctx->listStats[dpy].skipCount = 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700122
123 for (size_t i = 0; i < list->numHwLayers; i++) {
124 private_handle_t *hnd =
125 (private_handle_t *)list->hwLayers[i].handle;
126
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700127 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
128 continue;
129 //We disregard FB being skip for now! so the else if
Saurabh Shah35712cb2012-09-14 10:28:18 -0700130 } else if (isSkipLayer(&list->hwLayers[i])) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700131 ctx->listStats[dpy].skipCount++;
132 }
133
134 if (UNLIKELY(isYuvBuffer(hnd))) {
135 ctx->listStats[dpy].yuvCount++;
136 ctx->listStats[dpy].yuvIndex = i;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700137 }
138 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700139}
140
Saurabh Shah541b59d2012-10-11 11:08:12 -0700141static inline void calc_cut(float& leftCutRatio, float& topCutRatio,
142 float& rightCutRatio, float& bottomCutRatio, int orient) {
Saurabh Shah27c1d652012-08-14 19:30:28 -0700143 if(orient & HAL_TRANSFORM_FLIP_H) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700144 swap(leftCutRatio, rightCutRatio);
Saurabh Shah27c1d652012-08-14 19:30:28 -0700145 }
146 if(orient & HAL_TRANSFORM_FLIP_V) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700147 swap(topCutRatio, bottomCutRatio);
Saurabh Shah27c1d652012-08-14 19:30:28 -0700148 }
149 if(orient & HAL_TRANSFORM_ROT_90) {
150 //Anti clock swapping
Saurabh Shah541b59d2012-10-11 11:08:12 -0700151 float tmpCutRatio = leftCutRatio;
152 leftCutRatio = topCutRatio;
153 topCutRatio = rightCutRatio;
154 rightCutRatio = bottomCutRatio;
155 bottomCutRatio = tmpCutRatio;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700156 }
157}
158
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700159//Crops source buffer against destination and FB boundaries
160void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
Saurabh Shah27c1d652012-08-14 19:30:28 -0700161 const int fbWidth, const int fbHeight, int orient) {
162 int& crop_l = crop.left;
163 int& crop_t = crop.top;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700164 int& crop_r = crop.right;
165 int& crop_b = crop.bottom;
166 int crop_w = crop.right - crop.left;
167 int crop_h = crop.bottom - crop.top;
168
Saurabh Shah27c1d652012-08-14 19:30:28 -0700169 int& dst_l = dst.left;
170 int& dst_t = dst.top;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700171 int& dst_r = dst.right;
172 int& dst_b = dst.bottom;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700173 int dst_w = abs(dst.right - dst.left);
174 int dst_h = abs(dst.bottom - dst.top);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700175
Saurabh Shah541b59d2012-10-11 11:08:12 -0700176 float leftCutRatio = 0.0f, rightCutRatio = 0.0f, topCutRatio = 0.0f,
177 bottomCutRatio = 0.0f;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700178
Saurabh Shah27c1d652012-08-14 19:30:28 -0700179 if(dst_l < 0) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700180 leftCutRatio = (float)(0.0f - dst_l) / (float)dst_w;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700181 dst_l = 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700182 }
183 if(dst_r > fbWidth) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700184 rightCutRatio = (float)(dst_r - fbWidth) / (float)dst_w;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700185 dst_r = fbWidth;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700186 }
Saurabh Shah27c1d652012-08-14 19:30:28 -0700187 if(dst_t < 0) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700188 topCutRatio = (float)(0 - dst_t) / (float)dst_h;
Saurabh Shah27c1d652012-08-14 19:30:28 -0700189 dst_t = 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700190 }
191 if(dst_b > fbHeight) {
Saurabh Shah541b59d2012-10-11 11:08:12 -0700192 bottomCutRatio = (float)(dst_b - fbHeight) / (float)dst_h;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700193 dst_b = fbHeight;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700194 }
Saurabh Shah27c1d652012-08-14 19:30:28 -0700195
Saurabh Shah541b59d2012-10-11 11:08:12 -0700196 calc_cut(leftCutRatio, topCutRatio, rightCutRatio, bottomCutRatio, orient);
197 crop_l += crop_w * leftCutRatio;
198 crop_t += crop_h * topCutRatio;
199 crop_r -= crop_w * rightCutRatio;
200 crop_b -= crop_h * bottomCutRatio;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700201}
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700202
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700203bool isExternalActive(hwc_context_t* ctx) {
204 return ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive;
Saurabh Shahfc2acbe2012-08-17 19:47:52 -0700205}
206
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700207int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy) {
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700208 int ret = 0;
209#ifdef USE_FENCE_SYNC
210 struct mdp_buf_sync data;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700211 int acquireFd[4];
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700212 int count = 0;
213 int releaseFd = -1;
214 int fbFd = -1;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700215 data.flags = MDP_BUF_SYNC_FLAG_WAIT;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700216 data.acq_fen_fd = acquireFd;
217 data.rel_fen_fd = &releaseFd;
218 //Accumulate acquireFenceFds
219 for(uint32_t i = 0; i < list->numHwLayers; i++) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700220 if((list->hwLayers[i].compositionType == HWC_OVERLAY ||
221 list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) &&
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700222 list->hwLayers[i].acquireFenceFd != -1) {
223 acquireFd[count++] = list->hwLayers[i].acquireFenceFd;
224 }
225 }
226
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700227 data.acq_fen_fd_cnt = count;
228 fbFd = ctx->dpyAttr[dpy].fd;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700229
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700230 //Waits for acquire fences, returns a release fence
231 ret = ioctl(fbFd, MSMFB_BUFFER_SYNC, &data);
232 if(ret < 0) {
233 ALOGE("ioctl MSMFB_BUFFER_SYNC failed, err=%s",
234 strerror(errno));
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700235 }
Kinjal Bhavsar40a1cc52012-10-10 15:52:03 -0700236
237 for(uint32_t i = 0; i < list->numHwLayers; i++) {
238 if((list->hwLayers[i].compositionType == HWC_OVERLAY ||
239 list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET)) {
240 //Close the acquireFenceFds
241 if(list->hwLayers[i].acquireFenceFd > 0) {
242 close(list->hwLayers[i].acquireFenceFd);
243 list->hwLayers[i].acquireFenceFd = -1;
244 }
245 //Populate releaseFenceFds.
246 list->hwLayers[i].releaseFenceFd = dup(releaseFd);
247 }
248 }
249 list->retireFenceFd = releaseFd;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700250#endif
251 return ret;
252}
253
Naseer Ahmed29a26812012-06-14 00:56:20 -0700254};//namespace