blob: 411057e2f9b40aeb123cebff9eee48dea6604a84 [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
Arun Kumar K.Rba9eed52013-01-04 00:51:29 -08003 * Copyright (C) 2012-2013, The Linux Foundation. All rights reserved.
Naseer Ahmed29a26812012-06-14 00:56:20 -07004 *
Arun Kumar K.R361da4f2012-11-28 10:42:59 -08005 * Not a Contribution, Apache license notifications and license are retained
6 * for attribution purposes only.
7 *
Naseer Ahmed29a26812012-06-14 00:56:20 -07008 * 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 */
Naseer Ahmed64b81212013-02-14 10:29:47 -050020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
Naseer Ahmed29a26812012-06-14 00:56:20 -070021#include <fcntl.h>
22#include <errno.h>
23
24#include <cutils/log.h>
25#include <cutils/atomic.h>
Naseer Ahmed72cf9762012-07-21 12:17:13 -070026#include <EGL/egl.h>
Naseer Ahmed64b81212013-02-14 10:29:47 -050027#include <utils/Trace.h>
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -080028#include <sys/ioctl.h>
Naseer Ahmed72cf9762012-07-21 12:17:13 -070029#include <overlay.h>
Saurabh Shahacf10202013-02-26 10:15:15 -080030#include <overlayRotator.h>
Naseer Ahmed96c4c952012-07-25 18:27:14 -070031#include <mdp_version.h>
Naseer Ahmed29a26812012-06-14 00:56:20 -070032#include "hwc_utils.h"
Naseer Ahmedf48aef62012-07-20 09:05:53 -070033#include "hwc_video.h"
Naseer Ahmed758bfc52012-11-28 17:02:08 -050034#include "hwc_fbupdate.h"
Naseer Ahmed54821fe2012-11-28 18:44:38 -050035#include "hwc_mdpcomp.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070036#include "external.h"
Arun Kumar K.R361da4f2012-11-28 10:42:59 -080037#include "hwc_copybit.h"
Naseer Ahmed0d05d4f2013-03-28 14:49:07 -040038#include "profiler.h"
Naseer Ahmed29a26812012-06-14 00:56:20 -070039
40using namespace qhwc;
Naseer Ahmed758bfc52012-11-28 17:02:08 -050041#define VSYNC_DEBUG 0
Naseer Ahmed29a26812012-06-14 00:56:20 -070042
43static int hwc_device_open(const struct hw_module_t* module,
44 const char* name,
45 struct hw_device_t** device);
46
47static struct hw_module_methods_t hwc_module_methods = {
48 open: hwc_device_open
49};
50
51hwc_module_t HAL_MODULE_INFO_SYM = {
52 common: {
53 tag: HARDWARE_MODULE_TAG,
54 version_major: 2,
55 version_minor: 0,
56 id: HWC_HARDWARE_MODULE_ID,
57 name: "Qualcomm Hardware Composer Module",
58 author: "CodeAurora Forum",
59 methods: &hwc_module_methods,
60 dso: 0,
61 reserved: {0},
62 }
63};
64
65/*
66 * Save callback functions registered to HWC
67 */
Naseer Ahmed5b6708a2012-08-02 13:46:08 -070068static void hwc_registerProcs(struct hwc_composer_device_1* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -070069 hwc_procs_t const* procs)
70{
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070071 ALOGI("%s", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -070072 hwc_context_t* ctx = (hwc_context_t*)(dev);
73 if(!ctx) {
74 ALOGE("%s: Invalid context", __FUNCTION__);
75 return;
76 }
Jesse Hall3be78d92012-08-21 15:12:23 -070077 ctx->proc = procs;
78
Naseer Ahmedff4f0252012-10-01 13:03:01 -040079 // Now that we have the functions needed, kick off
80 // the uevent & vsync threads
Jesse Hall3be78d92012-08-21 15:12:23 -070081 init_uevent_thread(ctx);
Naseer Ahmedff4f0252012-10-01 13:03:01 -040082 init_vsync_thread(ctx);
Naseer Ahmed29a26812012-06-14 00:56:20 -070083}
84
Saurabh Shah649cda62012-09-16 16:05:58 -070085//Helper
Naseer Ahmedb1c76322012-10-17 00:32:50 -040086static void reset(hwc_context_t *ctx, int numDisplays,
87 hwc_display_contents_1_t** displays) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -070088 memset(ctx->listStats, 0, sizeof(ctx->listStats));
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -080089 for(int i = 0; i < MAX_DISPLAYS; i++) {
Naseer Ahmedb1c76322012-10-17 00:32:50 -040090 hwc_display_contents_1_t *list = displays[i];
91 // XXX:SurfaceFlinger no longer guarantees that this
92 // value is reset on every prepare. However, for the layer
93 // cache we need to reset it.
94 // We can probably rethink that later on
95 if (LIKELY(list && list->numHwLayers > 1)) {
96 for(uint32_t j = 0; j < list->numHwLayers; j++) {
97 if(list->hwLayers[j].compositionType != HWC_FRAMEBUFFER_TARGET)
98 list->hwLayers[j].compositionType = HWC_FRAMEBUFFER;
99 }
100 }
Saurabh Shahcf053c62012-12-13 12:32:55 -0800101
102 if(ctx->mFBUpdate[i])
103 ctx->mFBUpdate[i]->reset();
Saurabh Shahacf10202013-02-26 10:15:15 -0800104 if(ctx->mVidOv[i])
105 ctx->mVidOv[i]->reset();
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800106 if(ctx->mCopyBit[i])
107 ctx->mCopyBit[i]->reset();
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700108 }
109}
110
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500111//clear prev layer prop flags and realloc for current frame
Saurabh Shah2b033392013-03-22 11:29:44 -0700112static void reset_layer_prop(hwc_context_t* ctx, int dpy, int numAppLayers) {
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500113 if(ctx->layerProp[dpy]) {
114 delete[] ctx->layerProp[dpy];
115 ctx->layerProp[dpy] = NULL;
116 }
Saurabh Shah2b033392013-03-22 11:29:44 -0700117 ctx->layerProp[dpy] = new LayerProp[numAppLayers];
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500118}
119
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800120static int display_commit(hwc_context_t *ctx, int dpy) {
121 struct mdp_display_commit commit_info;
122 memset(&commit_info, 0, sizeof(struct mdp_display_commit));
123 commit_info.flags = MDP_DISPLAY_COMMIT_OVERLAY;
124 if(ioctl(ctx->dpyAttr[dpy].fd, MSMFB_DISPLAY_COMMIT, &commit_info) == -1) {
125 ALOGE("%s: MSMFB_DISPLAY_COMMIT for primary failed", __FUNCTION__);
126 return -errno;
127 }
128 return 0;
129}
130
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700131static int hwc_prepare_primary(hwc_composer_device_1 *dev,
132 hwc_display_contents_1_t *list) {
133 hwc_context_t* ctx = (hwc_context_t*)(dev);
Saurabh Shahcf053c62012-12-13 12:32:55 -0800134 const int dpy = HWC_DISPLAY_PRIMARY;
Saurabh Shah2b033392013-03-22 11:29:44 -0700135 if (LIKELY(list && list->numHwLayers > 1) &&
136 ctx->dpyAttr[dpy].isActive) {
137 reset_layer_prop(ctx, dpy, list->numHwLayers - 1);
Saurabh Shah86623d72012-09-25 19:39:17 -0700138 uint32_t last = list->numHwLayers - 1;
Saurabh Shah1a8cda02012-10-12 17:00:39 -0700139 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
140 if(fbLayer->handle) {
Saurabh Shah2b033392013-03-22 11:29:44 -0700141 if(list->numHwLayers > MAX_NUM_LAYERS) {
142 ctx->mFBUpdate[dpy]->prepare(ctx, list);
143 return 0;
144 }
Saurabh Shahcf053c62012-12-13 12:32:55 -0800145 setListStats(ctx, list, dpy);
Saurabh Shah2b033392013-03-22 11:29:44 -0700146 bool ret = ctx->mMDPComp->prepare(ctx, list);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800147 if(!ret) {
148 // IF MDPcomp fails use this route
Saurabh Shahacf10202013-02-26 10:15:15 -0800149 ctx->mVidOv[dpy]->prepare(ctx, list);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500150 ctx->mFBUpdate[dpy]->prepare(ctx, list);
Saurabh Shah2b033392013-03-22 11:29:44 -0700151 // Use Copybit, when MDP comp fails
152 if(ctx->mCopyBit[dpy])
153 ctx->mCopyBit[dpy]->prepare(ctx, list, dpy);
154 ctx->mLayerCache[dpy]->updateLayerCache(list);
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500155 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700156 }
157 }
158 return 0;
159}
160
161static int hwc_prepare_external(hwc_composer_device_1 *dev,
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800162 hwc_display_contents_1_t *list, int dpy) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700163 hwc_context_t* ctx = (hwc_context_t*)(dev);
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700164
Saurabh Shah2b033392013-03-22 11:29:44 -0700165 if (LIKELY(list && list->numHwLayers > 1) &&
166 ctx->dpyAttr[dpy].isActive &&
167 ctx->dpyAttr[dpy].connected) {
168 reset_layer_prop(ctx, dpy, list->numHwLayers - 1);
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700169 uint32_t last = list->numHwLayers - 1;
Saurabh Shah2b033392013-03-22 11:29:44 -0700170 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800171 if(!ctx->dpyAttr[dpy].isPause) {
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800172 if(fbLayer->handle) {
Saurabh Shah2b033392013-03-22 11:29:44 -0700173 ctx->mExtDispConfiguring = false;
174 if(list->numHwLayers > MAX_NUM_LAYERS) {
175 ctx->mFBUpdate[dpy]->prepare(ctx, list);
176 return 0;
177 }
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800178 setListStats(ctx, list, dpy);
Saurabh Shahacf10202013-02-26 10:15:15 -0800179 ctx->mVidOv[dpy]->prepare(ctx, list);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500180 ctx->mFBUpdate[dpy]->prepare(ctx, list);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800181 ctx->mLayerCache[dpy]->updateLayerCache(list);
182 if(ctx->mCopyBit[dpy])
183 ctx->mCopyBit[dpy]->prepare(ctx, list, dpy);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800184 }
185 } else {
186 // External Display is in Pause state.
187 // ToDo:
188 // Mark all application layers as OVERLAY so that
189 // GPU will not compose. This is done for power
190 // optimization
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700191 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700192 }
193 return 0;
Saurabh Shah649cda62012-09-16 16:05:58 -0700194}
195
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700196static int hwc_prepare(hwc_composer_device_1 *dev, size_t numDisplays,
197 hwc_display_contents_1_t** displays)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700198{
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700199 int ret = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700200 hwc_context_t* ctx = (hwc_context_t*)(dev);
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500201 Locker::Autolock _l(ctx->mBlankLock);
Naseer Ahmedb1c76322012-10-17 00:32:50 -0400202 reset(ctx, numDisplays, displays);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700203
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500204 ctx->mOverlay->configBegin();
Saurabh Shahacf10202013-02-26 10:15:15 -0800205 ctx->mRotMgr->configBegin();
Jeykumar Sankarana37fdbf2013-03-06 18:59:28 -0800206 ctx->mNeedsRotator = false;
Saurabh Shah56f610d2012-08-07 15:27:06 -0700207
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800208 for (int32_t i = numDisplays; i >= 0; i--) {
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500209 hwc_display_contents_1_t *list = displays[i];
210 switch(i) {
211 case HWC_DISPLAY_PRIMARY:
212 ret = hwc_prepare_primary(dev, list);
213 break;
214 case HWC_DISPLAY_EXTERNAL:
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800215 case HWC_DISPLAY_VIRTUAL:
216 ret = hwc_prepare_external(dev, list, i);
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500217 break;
218 default:
219 ret = -EINVAL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700220 }
221 }
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500222
Saurabh Shahcf053c62012-12-13 12:32:55 -0800223 ctx->mOverlay->configDone();
Saurabh Shahacf10202013-02-26 10:15:15 -0800224 ctx->mRotMgr->configDone();
225
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700226 return ret;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700227}
228
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700229static int hwc_eventControl(struct hwc_composer_device_1* dev, int dpy,
Naseer Ahmed56601cd2013-03-05 11:34:14 -0500230 int event, int enable)
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700231{
232 int ret = 0;
233 hwc_context_t* ctx = (hwc_context_t*)(dev);
Iliyan Malcheveac89652012-10-04 10:38:28 -0700234 pthread_mutex_lock(&ctx->vstate.lock);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700235 switch(event) {
236 case HWC_EVENT_VSYNC:
Naseer Ahmed56601cd2013-03-05 11:34:14 -0500237 if (ctx->vstate.enable == enable)
Omprakash Dhyade1ef881c2012-10-03 02:26:55 -0700238 break;
Naseer Ahmed56601cd2013-03-05 11:34:14 -0500239 ret = hwc_vsync_control(ctx, dpy, enable);
240 if(ret == 0) {
241 ctx->vstate.enable = !!enable;
242 pthread_cond_signal(&ctx->vstate.cond);
243 }
Iliyan Malcheveac89652012-10-04 10:38:28 -0700244 ALOGD_IF (VSYNC_DEBUG, "VSYNC state changed to %s",
Naseer Ahmed56601cd2013-03-05 11:34:14 -0500245 (enable)?"ENABLED":"DISABLED");
Iliyan Malcheveac89652012-10-04 10:38:28 -0700246 break;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700247 default:
248 ret = -EINVAL;
249 }
Iliyan Malcheveac89652012-10-04 10:38:28 -0700250 pthread_mutex_unlock(&ctx->vstate.lock);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700251 return ret;
252}
253
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700254static int hwc_blank(struct hwc_composer_device_1* dev, int dpy, int blank)
255{
Naseer Ahmed64b81212013-02-14 10:29:47 -0500256 ATRACE_CALL();
Naseer Ahmed934790c2012-08-16 18:11:09 -0700257 hwc_context_t* ctx = (hwc_context_t*)(dev);
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800258
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500259 Locker::Autolock _l(ctx->mBlankLock);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700260 int ret = 0;
Naseer Ahmed56601cd2013-03-05 11:34:14 -0500261 ALOGD("%s: %s display: %d", __FUNCTION__,
262 blank==1 ? "Blanking":"Unblanking", dpy);
Arun Kumar K.Re7408372013-03-28 18:41:06 -0700263 if(blank) {
264 // free up all the overlay pipes in use
265 // when we get a blank for either display
266 // makes sure that all pipes are freed
267 ctx->mOverlay->configBegin();
268 ctx->mOverlay->configDone();
269 ctx->mRotMgr->clear();
270 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700271 switch(dpy) {
272 case HWC_DISPLAY_PRIMARY:
273 if(blank) {
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800274 ret = ioctl(ctx->dpyAttr[dpy].fd, FBIOBLANK,FB_BLANK_POWERDOWN);
Ramkumar Radhakrishnan893d6ee2013-02-26 10:48:05 -0800275
276 if(ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].connected == true) {
277 // Surfaceflinger does not send Blank/unblank event to hwc
278 // for virtual display, handle it explicitly when blank for
279 // primary is invoked, so that any pipes unset get committed
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800280 if (display_commit(ctx, HWC_DISPLAY_VIRTUAL) < 0) {
Ramkumar Radhakrishnan893d6ee2013-02-26 10:48:05 -0800281 ret = -1;
282 ALOGE("%s:post failed for virtual display !!",
283 __FUNCTION__);
284 } else {
285 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].isActive = !blank;
286 }
287 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700288 } else {
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800289 ret = ioctl(ctx->dpyAttr[dpy].fd, FBIOBLANK, FB_BLANK_UNBLANK);
Ramkumar Radhakrishnan893d6ee2013-02-26 10:48:05 -0800290 if(ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].connected == true) {
291 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].isActive = !blank;
292 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700293 }
294 break;
295 case HWC_DISPLAY_EXTERNAL:
296 if(blank) {
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800297 // call external framebuffer commit on blank,
298 // so that any pipe unsets gets committed
299 if (display_commit(ctx, dpy) < 0) {
Arun Kumar K.Rba9eed52013-01-04 00:51:29 -0800300 ret = -1;
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800301 ALOGE("%s:post failed for external display !! ", __FUNCTION__);
Arun Kumar K.Rba9eed52013-01-04 00:51:29 -0800302 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700303 } else {
304 }
305 break;
306 default:
307 return -EINVAL;
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700308 }
Arun Kumar K.Rf6f49a12012-11-27 11:46:50 -0800309 // Enable HPD here, as during bootup unblank is called
310 // when SF is completely initialized
311 ctx->mExtDisplay->setHPD(1);
Naseer Ahmed56601cd2013-03-05 11:34:14 -0500312 if(ret == 0){
313 ctx->dpyAttr[dpy].isActive = !blank;
314 } else {
315 ALOGE("%s: Failed in %s display: %d error:%s", __FUNCTION__,
316 blank==1 ? "blanking":"unblanking", dpy, strerror(errno));
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700317 return ret;
318 }
Naseer Ahmed56601cd2013-03-05 11:34:14 -0500319
320 ALOGD("%s: Done %s display: %d", __FUNCTION__,
321 blank==1 ? "blanking":"unblanking", dpy);
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700322 return 0;
323}
324
325static int hwc_query(struct hwc_composer_device_1* dev,
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700326 int param, int* value)
327{
328 hwc_context_t* ctx = (hwc_context_t*)(dev);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700329 int supported = HWC_DISPLAY_PRIMARY_BIT;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700330
331 switch (param) {
332 case HWC_BACKGROUND_LAYER_SUPPORTED:
333 // Not supported for now
334 value[0] = 0;
335 break;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700336 case HWC_DISPLAY_TYPES_SUPPORTED:
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700337 if(ctx->mMDP.hasOverlay)
338 supported |= HWC_DISPLAY_EXTERNAL_BIT;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700339 value[0] = supported;
340 break;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700341 default:
342 return -EINVAL;
343 }
344 return 0;
345
346}
347
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800348
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700349static int hwc_set_primary(hwc_context_t *ctx, hwc_display_contents_1_t* list) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500350 ATRACE_CALL();
Saurabh Shaheaf67912012-10-10 17:33:14 -0700351 int ret = 0;
Saurabh Shahcf053c62012-12-13 12:32:55 -0800352 const int dpy = HWC_DISPLAY_PRIMARY;
Amara Venkata Mastan Manoj Kumar57e3c812013-01-11 13:36:33 -0800353 if (LIKELY(list) && ctx->dpyAttr[dpy].isActive) {
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700354 uint32_t last = list->numHwLayers - 1;
355 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800356 int fd = -1; //FenceFD from the Copybit(valid in async mode)
Naseer Ahmed64b81212013-02-14 10:29:47 -0500357 bool copybitDone = false;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800358 if(ctx->mCopyBit[dpy])
Naseer Ahmed64b81212013-02-14 10:29:47 -0500359 copybitDone = ctx->mCopyBit[dpy]->draw(ctx, list, dpy, &fd);
Amara Venkata Mastan Manoj Kumar57e3c812013-01-11 13:36:33 -0800360 if(list->numHwLayers > 1)
361 hwc_sync(ctx, list, dpy, fd);
Saurabh Shahacf10202013-02-26 10:15:15 -0800362 if (!ctx->mVidOv[dpy]->draw(ctx, list)) {
363 ALOGE("%s: VideoOverlay draw failed", __FUNCTION__);
Saurabh Shaheaf67912012-10-10 17:33:14 -0700364 ret = -1;
365 }
Saurabh Shahcbf7ccc2012-12-19 16:45:51 -0800366 if (!ctx->mMDPComp->draw(ctx, list)) {
Saurabh Shahacf10202013-02-26 10:15:15 -0800367 ALOGE("%s: MDPComp draw failed", __FUNCTION__);
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500368 ret = -1;
369 }
370
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700371 //TODO We dont check for SKIP flag on this layer because we need PAN
372 //always. Last layer is always FB
Saurabh Shah2b033392013-03-22 11:29:44 -0700373 private_handle_t *hnd = (private_handle_t *)fbLayer->handle;
Naseer Ahmed64b81212013-02-14 10:29:47 -0500374 if(copybitDone) {
375 hnd = ctx->mCopyBit[dpy]->getCurrentRenderBuffer();
Naseer Ahmed64b81212013-02-14 10:29:47 -0500376 }
Saurabh Shah2b033392013-03-22 11:29:44 -0700377
378 if(hnd) {
379 if (!ctx->mFBUpdate[dpy]->draw(ctx, hnd)) {
380 ALOGE("%s: FBUpdate draw failed", __FUNCTION__);
381 ret = -1;
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500382 }
Saurabh Shah43333302013-02-06 13:30:27 -0800383 }
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800384
385 if (display_commit(ctx, dpy) < 0) {
386 ALOGE("%s: display commit fail!", __FUNCTION__);
387 return -1;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700388 }
389 }
Saurabh Shah747af1e2013-02-26 10:25:12 -0800390
391 closeAcquireFds(list);
Saurabh Shaheaf67912012-10-10 17:33:14 -0700392 return ret;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700393}
394
395static int hwc_set_external(hwc_context_t *ctx,
Naseer Ahmed64b81212013-02-14 10:29:47 -0500396 hwc_display_contents_1_t* list, int dpy)
397{
398 ATRACE_CALL();
Saurabh Shaheaf67912012-10-10 17:33:14 -0700399 int ret = 0;
Kinjal Bhavsarf83d4482012-10-10 15:56:21 -0700400 Locker::Autolock _l(ctx->mExtSetLock);
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700401
Amara Venkata Mastan Manoj Kumar57e3c812013-01-11 13:36:33 -0800402 if (LIKELY(list) && ctx->dpyAttr[dpy].isActive &&
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800403 !ctx->dpyAttr[dpy].isPause &&
Saurabh Shahcf053c62012-12-13 12:32:55 -0800404 ctx->dpyAttr[dpy].connected) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700405 uint32_t last = list->numHwLayers - 1;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700406 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800407 int fd = -1; //FenceFD from the Copybit(valid in async mode)
Naseer Ahmed64b81212013-02-14 10:29:47 -0500408 bool copybitDone = false;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800409 if(ctx->mCopyBit[dpy])
Naseer Ahmed64b81212013-02-14 10:29:47 -0500410 copybitDone = ctx->mCopyBit[dpy]->draw(ctx, list, dpy, &fd);
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700411
Amara Venkata Mastan Manoj Kumar57e3c812013-01-11 13:36:33 -0800412 if(list->numHwLayers > 1)
413 hwc_sync(ctx, list, dpy, fd);
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700414
Saurabh Shahacf10202013-02-26 10:15:15 -0800415 if (!ctx->mVidOv[dpy]->draw(ctx, list)) {
Saurabh Shaheaf67912012-10-10 17:33:14 -0700416 ALOGE("%s: VideoOverlay::draw fail!", __FUNCTION__);
417 ret = -1;
418 }
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700419
Saurabh Shah2b033392013-03-22 11:29:44 -0700420 private_handle_t *hnd = (private_handle_t *)fbLayer->handle;
Naseer Ahmed64b81212013-02-14 10:29:47 -0500421 if(copybitDone) {
422 hnd = ctx->mCopyBit[dpy]->getCurrentRenderBuffer();
Naseer Ahmed64b81212013-02-14 10:29:47 -0500423 }
424
Saurabh Shah2b033392013-03-22 11:29:44 -0700425 if(hnd) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500426 if (!ctx->mFBUpdate[dpy]->draw(ctx, hnd)) {
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500427 ALOGE("%s: FBUpdate::draw fail!", __FUNCTION__);
Saurabh Shaheaf67912012-10-10 17:33:14 -0700428 ret = -1;
429 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700430 }
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800431
432 if (display_commit(ctx, dpy) < 0) {
433 ALOGE("%s: display commit fail!", __FUNCTION__);
Saurabh Shah2b033392013-03-22 11:29:44 -0700434 ret = -1;
Saurabh Shaheaf67912012-10-10 17:33:14 -0700435 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700436 }
Saurabh Shah747af1e2013-02-26 10:25:12 -0800437
438 closeAcquireFds(list);
Saurabh Shaheaf67912012-10-10 17:33:14 -0700439 return ret;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700440}
441
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700442static int hwc_set(hwc_composer_device_1 *dev,
443 size_t numDisplays,
444 hwc_display_contents_1_t** displays)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700445{
446 int ret = 0;
447 hwc_context_t* ctx = (hwc_context_t*)(dev);
Naseer Ahmed32ff2252012-09-29 01:41:21 -0400448 Locker::Autolock _l(ctx->mBlankLock);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800449 for (uint32_t i = 0; i <= numDisplays; i++) {
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700450 hwc_display_contents_1_t* list = displays[i];
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700451 switch(i) {
452 case HWC_DISPLAY_PRIMARY:
453 ret = hwc_set_primary(ctx, list);
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700454 break;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700455 case HWC_DISPLAY_EXTERNAL:
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800456 case HWC_DISPLAY_VIRTUAL:
457 /* ToDo: We are using hwc_set_external path for both External and
458 Virtual displays on HWC1.1. Eventually, we will have
459 separate functions when we move to HWC1.2
460 */
461 ret = hwc_set_external(ctx, list, i);
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700462 break;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700463 default:
464 ret = -EINVAL;
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700465 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700466 }
Naseer Ahmed0d05d4f2013-03-28 14:49:07 -0400467 // This is only indicative of how many times SurfaceFlinger posts
468 // frames to the display.
469 CALC_FPS();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700470 return ret;
471}
472
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700473int hwc_getDisplayConfigs(struct hwc_composer_device_1* dev, int disp,
474 uint32_t* configs, size_t* numConfigs) {
475 int ret = 0;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700476 hwc_context_t* ctx = (hwc_context_t*)(dev);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700477 //in 1.1 there is no way to choose a config, report as config id # 0
478 //This config is passed to getDisplayAttributes. Ignore for now.
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700479 switch(disp) {
480 case HWC_DISPLAY_PRIMARY:
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700481 if(*numConfigs > 0) {
482 configs[0] = 0;
483 *numConfigs = 1;
484 }
485 ret = 0; //NO_ERROR
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700486 break;
487 case HWC_DISPLAY_EXTERNAL:
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700488 ret = -1; //Not connected
489 if(ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].connected) {
490 ret = 0; //NO_ERROR
491 if(*numConfigs > 0) {
492 configs[0] = 0;
493 *numConfigs = 1;
494 }
495 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700496 break;
497 }
498 return ret;
499}
500
501int hwc_getDisplayAttributes(struct hwc_composer_device_1* dev, int disp,
502 uint32_t config, const uint32_t* attributes, int32_t* values) {
503
504 hwc_context_t* ctx = (hwc_context_t*)(dev);
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700505 //If hotpluggable displays are inactive return error
506 if(disp == HWC_DISPLAY_EXTERNAL && !ctx->dpyAttr[disp].connected) {
507 return -1;
508 }
509
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700510 //From HWComposer
511 static const uint32_t DISPLAY_ATTRIBUTES[] = {
512 HWC_DISPLAY_VSYNC_PERIOD,
513 HWC_DISPLAY_WIDTH,
514 HWC_DISPLAY_HEIGHT,
515 HWC_DISPLAY_DPI_X,
516 HWC_DISPLAY_DPI_Y,
517 HWC_DISPLAY_NO_ATTRIBUTE,
518 };
519
520 const int NUM_DISPLAY_ATTRIBUTES = (sizeof(DISPLAY_ATTRIBUTES) /
521 sizeof(DISPLAY_ATTRIBUTES)[0]);
522
523 for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
524 switch (attributes[i]) {
525 case HWC_DISPLAY_VSYNC_PERIOD:
526 values[i] = ctx->dpyAttr[disp].vsync_period;
527 break;
528 case HWC_DISPLAY_WIDTH:
529 values[i] = ctx->dpyAttr[disp].xres;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700530 ALOGD("%s disp = %d, width = %d",__FUNCTION__, disp,
531 ctx->dpyAttr[disp].xres);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700532 break;
533 case HWC_DISPLAY_HEIGHT:
534 values[i] = ctx->dpyAttr[disp].yres;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700535 ALOGD("%s disp = %d, height = %d",__FUNCTION__, disp,
536 ctx->dpyAttr[disp].yres);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700537 break;
538 case HWC_DISPLAY_DPI_X:
Naseer Ahmed7b80d9c2012-09-26 20:14:38 -0400539 values[i] = (int32_t) (ctx->dpyAttr[disp].xdpi*1000.0);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700540 break;
541 case HWC_DISPLAY_DPI_Y:
Naseer Ahmed7b80d9c2012-09-26 20:14:38 -0400542 values[i] = (int32_t) (ctx->dpyAttr[disp].ydpi*1000.0);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700543 break;
544 default:
545 ALOGE("Unknown display attribute %d",
546 attributes[i]);
547 return -EINVAL;
548 }
549 }
550 return 0;
551}
552
Naseer Ahmed1d183f52012-11-26 12:35:16 -0500553void hwc_dump(struct hwc_composer_device_1* dev, char *buff, int buff_len)
554{
555 hwc_context_t* ctx = (hwc_context_t*)(dev);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800556 android::String8 aBuf("");
557 dumpsys_log(aBuf, "Qualcomm HWC state:\n");
558 dumpsys_log(aBuf, " MDPVersion=%d\n", ctx->mMDP.version);
559 dumpsys_log(aBuf, " DisplayPanel=%c\n", ctx->mMDP.panel);
560 ctx->mMDPComp->dump(aBuf);
561 char ovDump[2048] = {'\0'};
562 ctx->mOverlay->getDump(ovDump, 2048);
563 dumpsys_log(aBuf, ovDump);
Saurabh Shahacf10202013-02-26 10:15:15 -0800564 ovDump[0] = '\0';
565 ctx->mRotMgr->getDump(ovDump, 2048);
566 dumpsys_log(aBuf, ovDump);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800567 strlcpy(buff, aBuf.string(), buff_len);
Naseer Ahmed1d183f52012-11-26 12:35:16 -0500568}
569
Naseer Ahmed29a26812012-06-14 00:56:20 -0700570static int hwc_device_close(struct hw_device_t *dev)
571{
572 if(!dev) {
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700573 ALOGE("%s: NULL device pointer", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700574 return -1;
575 }
576 closeContext((hwc_context_t*)dev);
577 free(dev);
578
579 return 0;
580}
581
582static int hwc_device_open(const struct hw_module_t* module, const char* name,
583 struct hw_device_t** device)
584{
585 int status = -EINVAL;
586
587 if (!strcmp(name, HWC_HARDWARE_COMPOSER)) {
588 struct hwc_context_t *dev;
589 dev = (hwc_context_t*)malloc(sizeof(*dev));
590 memset(dev, 0, sizeof(*dev));
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700591
592 //Initialize hwc context
Naseer Ahmed29a26812012-06-14 00:56:20 -0700593 initContext(dev);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700594
595 //Setup HWC methods
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700596 dev->device.common.tag = HARDWARE_DEVICE_TAG;
597 dev->device.common.version = HWC_DEVICE_API_VERSION_1_1;
598 dev->device.common.module = const_cast<hw_module_t*>(module);
599 dev->device.common.close = hwc_device_close;
600 dev->device.prepare = hwc_prepare;
601 dev->device.set = hwc_set;
602 dev->device.eventControl = hwc_eventControl;
603 dev->device.blank = hwc_blank;
604 dev->device.query = hwc_query;
605 dev->device.registerProcs = hwc_registerProcs;
Naseer Ahmed1d183f52012-11-26 12:35:16 -0500606 dev->device.dump = hwc_dump;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700607 dev->device.getDisplayConfigs = hwc_getDisplayConfigs;
608 dev->device.getDisplayAttributes = hwc_getDisplayAttributes;
609 *device = &dev->device.common;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700610 status = 0;
611 }
612 return status;
613}