blob: a4c7e3c825fec69fcfddef446e0bc4c9642596d9 [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);
Naseer Ahmed29a26812012-06-14 00:56:20 -070054 ctx->mOverlay = overlay::Overlay::getInstance();
Saurabh Shah56f610d2012-08-07 15:27:06 -070055 ctx->mQService = qService::QService::getInstance(ctx);
Naseer Ahmed96c4c952012-07-25 18:27:14 -070056 ctx->mMDP.version = qdutils::MDPVersion::getInstance().getMDPVersion();
57 ctx->mMDP.hasOverlay = qdutils::MDPVersion::getInstance().hasOverlay();
58 ctx->mMDP.panel = qdutils::MDPVersion::getInstance().getPanelType();
Naseer Ahmed72cf9762012-07-21 12:17:13 -070059 ctx->mExtDisplay = new ExternalDisplay(ctx);
Naseer Ahmed7c958d42012-07-31 18:57:03 -070060 MDPComp::init(ctx);
Naseer Ahmed72cf9762012-07-21 12:17:13 -070061
Naseer Ahmed72cf9762012-07-21 12:17:13 -070062 ALOGI("Initializing Qualcomm Hardware Composer");
Naseer Ahmed96c4c952012-07-25 18:27:14 -070063 ALOGI("MDP version: %d", ctx->mMDP.version);
Naseer Ahmed29a26812012-06-14 00:56:20 -070064}
65
66void closeContext(hwc_context_t *ctx)
67{
68 if(ctx->mOverlay) {
69 delete ctx->mOverlay;
70 ctx->mOverlay = NULL;
71 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -070072
Naseer Ahmed72cf9762012-07-21 12:17:13 -070073 if(ctx->mFbDev) {
74 framebuffer_close(ctx->mFbDev);
75 ctx->mFbDev = NULL;
Saurabh Shah3e858eb2012-09-17 16:53:21 -070076 close(ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd);
77 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].fd = -1;
Naseer Ahmed29a26812012-06-14 00:56:20 -070078 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -070079
80 if(ctx->mExtDisplay) {
81 delete ctx->mExtDisplay;
82 ctx->mExtDisplay = NULL;
83 }
Naseer Ahmed29a26812012-06-14 00:56:20 -070084}
85
Naseer Ahmed5b6708a2012-08-02 13:46:08 -070086void dumpLayer(hwc_layer_1_t const* l)
Naseer Ahmed29a26812012-06-14 00:56:20 -070087{
88 ALOGD("\ttype=%d, flags=%08x, handle=%p, tr=%02x, blend=%04x, {%d,%d,%d,%d}"
89 ", {%d,%d,%d,%d}",
90 l->compositionType, l->flags, l->handle, l->transform, l->blending,
91 l->sourceCrop.left,
92 l->sourceCrop.top,
93 l->sourceCrop.right,
94 l->sourceCrop.bottom,
95 l->displayFrame.left,
96 l->displayFrame.top,
97 l->displayFrame.right,
98 l->displayFrame.bottom);
99}
100
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700101void setListStats(hwc_context_t *ctx,
102 const hwc_display_contents_1_t *list, int dpy) {
103
104 ctx->listStats[dpy].numAppLayers = list->numHwLayers - 1;
105 ctx->listStats[dpy].fbLayerIndex = list->numHwLayers - 1;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700106
107 for (size_t i = 0; i < list->numHwLayers; i++) {
108 private_handle_t *hnd =
109 (private_handle_t *)list->hwLayers[i].handle;
110
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700111 if(list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) {
112 continue;
113 //We disregard FB being skip for now! so the else if
Saurabh Shah35712cb2012-09-14 10:28:18 -0700114 } else if (isSkipLayer(&list->hwLayers[i])) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700115 ctx->listStats[dpy].skipCount++;
116 }
117
118 if (UNLIKELY(isYuvBuffer(hnd))) {
119 ctx->listStats[dpy].yuvCount++;
120 ctx->listStats[dpy].yuvIndex = i;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700121 }
122 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700123}
124
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700125//Crops source buffer against destination and FB boundaries
126void calculate_crop_rects(hwc_rect_t& crop, hwc_rect_t& dst,
127 const int fbWidth, const int fbHeight) {
128
129 int& crop_x = crop.left;
130 int& crop_y = crop.top;
131 int& crop_r = crop.right;
132 int& crop_b = crop.bottom;
133 int crop_w = crop.right - crop.left;
134 int crop_h = crop.bottom - crop.top;
135
136 int& dst_x = dst.left;
137 int& dst_y = dst.top;
138 int& dst_r = dst.right;
139 int& dst_b = dst.bottom;
140 int dst_w = dst.right - dst.left;
141 int dst_h = dst.bottom - dst.top;
142
143 if(dst_x < 0) {
144 float scale_x = crop_w * 1.0f / dst_w;
145 float diff_factor = (scale_x * abs(dst_x));
146 crop_x = crop_x + (int)diff_factor;
147 crop_w = crop_r - crop_x;
148
149 dst_x = 0;
150 dst_w = dst_r - dst_x;;
151 }
152 if(dst_r > fbWidth) {
153 float scale_x = crop_w * 1.0f / dst_w;
154 float diff_factor = scale_x * (dst_r - fbWidth);
155 crop_r = crop_r - diff_factor;
156 crop_w = crop_r - crop_x;
157
158 dst_r = fbWidth;
159 dst_w = dst_r - dst_x;
160 }
161 if(dst_y < 0) {
162 float scale_y = crop_h * 1.0f / dst_h;
163 float diff_factor = scale_y * abs(dst_y);
164 crop_y = crop_y + diff_factor;
165 crop_h = crop_b - crop_y;
166
167 dst_y = 0;
168 dst_h = dst_b - dst_y;
169 }
170 if(dst_b > fbHeight) {
171 float scale_y = crop_h * 1.0f / dst_h;
172 float diff_factor = scale_y * (dst_b - fbHeight);
173 crop_b = crop_b - diff_factor;
174 crop_h = crop_b - crop_y;
175
176 dst_b = fbHeight;
177 dst_h = dst_b - dst_y;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700178 }
179}
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700180
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700181bool isExternalActive(hwc_context_t* ctx) {
182 return ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive;
Saurabh Shahfc2acbe2012-08-17 19:47:52 -0700183}
184
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700185int hwc_sync(hwc_context_t *ctx, hwc_display_contents_1_t* list, int dpy) {
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700186 int ret = 0;
187#ifdef USE_FENCE_SYNC
188 struct mdp_buf_sync data;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700189 int acquireFd[4];
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700190 int count = 0;
191 int releaseFd = -1;
192 int fbFd = -1;
193 data.flags = 0;
194 data.acq_fen_fd = acquireFd;
195 data.rel_fen_fd = &releaseFd;
196 //Accumulate acquireFenceFds
197 for(uint32_t i = 0; i < list->numHwLayers; i++) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700198 if((list->hwLayers[i].compositionType == HWC_OVERLAY ||
199 list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET) &&
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700200 list->hwLayers[i].acquireFenceFd != -1) {
201 acquireFd[count++] = list->hwLayers[i].acquireFenceFd;
202 }
203 }
204
205 if (count) {
206 data.acq_fen_fd_cnt = count;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700207 fbFd = ctx->dpyAttr[dpy].fd;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700208
209 //Waits for acquire fences, returns a release fence
210 ret = ioctl(fbFd, MSMFB_BUFFER_SYNC, &data);
211 if(ret < 0) {
212 ALOGE("ioctl MSMFB_BUFFER_SYNC failed, err=%s",
213 strerror(errno));
214 }
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700215
216 for(uint32_t i = 0; i < list->numHwLayers; i++) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700217 if((list->hwLayers[i].compositionType == HWC_OVERLAY ||
218 list->hwLayers[i].compositionType == HWC_FRAMEBUFFER_TARGET)) {
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700219 //Close the acquireFenceFds
220 if(list->hwLayers[i].acquireFenceFd > 0) {
221 close(list->hwLayers[i].acquireFenceFd);
222 list->hwLayers[i].acquireFenceFd = -1;
223 }
224 //Populate releaseFenceFds.
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700225 list->hwLayers[i].releaseFenceFd = dup(releaseFd);
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700226 }
227 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700228 list->retireFenceFd = releaseFd;
Kinjal Bhavsar2dd04a82012-09-18 18:27:59 -0700229 }
230#endif
231 return ret;
232}
233
Naseer Ahmed29a26812012-06-14 00:56:20 -0700234};//namespace