blob: a81e7611bc3e40c8add2dfdf2af75751453e3f10 [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 Ahmede0205b62013-04-04 13:24:39 -040042#define BLANK_DEBUG 0
Naseer Ahmed29a26812012-06-14 00:56:20 -070043
44static int hwc_device_open(const struct hw_module_t* module,
45 const char* name,
46 struct hw_device_t** device);
47
48static struct hw_module_methods_t hwc_module_methods = {
49 open: hwc_device_open
50};
51
52hwc_module_t HAL_MODULE_INFO_SYM = {
53 common: {
54 tag: HARDWARE_MODULE_TAG,
55 version_major: 2,
56 version_minor: 0,
57 id: HWC_HARDWARE_MODULE_ID,
58 name: "Qualcomm Hardware Composer Module",
59 author: "CodeAurora Forum",
60 methods: &hwc_module_methods,
61 dso: 0,
62 reserved: {0},
63 }
64};
65
66/*
67 * Save callback functions registered to HWC
68 */
Naseer Ahmed5b6708a2012-08-02 13:46:08 -070069static void hwc_registerProcs(struct hwc_composer_device_1* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -070070 hwc_procs_t const* procs)
71{
Iliyan Malchev0f9c3972012-10-11 15:16:15 -070072 ALOGI("%s", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -070073 hwc_context_t* ctx = (hwc_context_t*)(dev);
74 if(!ctx) {
75 ALOGE("%s: Invalid context", __FUNCTION__);
76 return;
77 }
Jesse Hall3be78d92012-08-21 15:12:23 -070078 ctx->proc = procs;
79
Naseer Ahmedff4f0252012-10-01 13:03:01 -040080 // Now that we have the functions needed, kick off
81 // the uevent & vsync threads
Jesse Hall3be78d92012-08-21 15:12:23 -070082 init_uevent_thread(ctx);
Naseer Ahmedff4f0252012-10-01 13:03:01 -040083 init_vsync_thread(ctx);
Naseer Ahmed29a26812012-06-14 00:56:20 -070084}
85
Saurabh Shah649cda62012-09-16 16:05:58 -070086//Helper
Naseer Ahmedb1c76322012-10-17 00:32:50 -040087static void reset(hwc_context_t *ctx, int numDisplays,
88 hwc_display_contents_1_t** displays) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -070089 memset(ctx->listStats, 0, sizeof(ctx->listStats));
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -080090 for(int i = 0; i < MAX_DISPLAYS; i++) {
Naseer Ahmedb1c76322012-10-17 00:32:50 -040091 hwc_display_contents_1_t *list = displays[i];
92 // XXX:SurfaceFlinger no longer guarantees that this
93 // value is reset on every prepare. However, for the layer
94 // cache we need to reset it.
95 // We can probably rethink that later on
96 if (LIKELY(list && list->numHwLayers > 1)) {
97 for(uint32_t j = 0; j < list->numHwLayers; j++) {
98 if(list->hwLayers[j].compositionType != HWC_FRAMEBUFFER_TARGET)
99 list->hwLayers[j].compositionType = HWC_FRAMEBUFFER;
100 }
101 }
Saurabh Shahcf053c62012-12-13 12:32:55 -0800102
103 if(ctx->mFBUpdate[i])
104 ctx->mFBUpdate[i]->reset();
Saurabh Shahacf10202013-02-26 10:15:15 -0800105 if(ctx->mVidOv[i])
106 ctx->mVidOv[i]->reset();
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800107 if(ctx->mCopyBit[i])
108 ctx->mCopyBit[i]->reset();
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700109 }
110}
111
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500112//clear prev layer prop flags and realloc for current frame
Saurabh Shah2b033392013-03-22 11:29:44 -0700113static void reset_layer_prop(hwc_context_t* ctx, int dpy, int numAppLayers) {
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500114 if(ctx->layerProp[dpy]) {
115 delete[] ctx->layerProp[dpy];
116 ctx->layerProp[dpy] = NULL;
117 }
Saurabh Shah2b033392013-03-22 11:29:44 -0700118 ctx->layerProp[dpy] = new LayerProp[numAppLayers];
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500119}
120
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800121static int display_commit(hwc_context_t *ctx, int dpy) {
122 struct mdp_display_commit commit_info;
123 memset(&commit_info, 0, sizeof(struct mdp_display_commit));
124 commit_info.flags = MDP_DISPLAY_COMMIT_OVERLAY;
125 if(ioctl(ctx->dpyAttr[dpy].fd, MSMFB_DISPLAY_COMMIT, &commit_info) == -1) {
126 ALOGE("%s: MSMFB_DISPLAY_COMMIT for primary failed", __FUNCTION__);
127 return -errno;
128 }
129 return 0;
130}
131
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700132static int hwc_prepare_primary(hwc_composer_device_1 *dev,
133 hwc_display_contents_1_t *list) {
134 hwc_context_t* ctx = (hwc_context_t*)(dev);
Saurabh Shahcf053c62012-12-13 12:32:55 -0800135 const int dpy = HWC_DISPLAY_PRIMARY;
Saurabh Shah2b033392013-03-22 11:29:44 -0700136 if (LIKELY(list && list->numHwLayers > 1) &&
137 ctx->dpyAttr[dpy].isActive) {
138 reset_layer_prop(ctx, dpy, list->numHwLayers - 1);
Saurabh Shah86623d72012-09-25 19:39:17 -0700139 uint32_t last = list->numHwLayers - 1;
Saurabh Shah1a8cda02012-10-12 17:00:39 -0700140 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
141 if(fbLayer->handle) {
Saurabh Shah2b033392013-03-22 11:29:44 -0700142 if(list->numHwLayers > MAX_NUM_LAYERS) {
143 ctx->mFBUpdate[dpy]->prepare(ctx, list);
144 return 0;
145 }
Saurabh Shahcf053c62012-12-13 12:32:55 -0800146 setListStats(ctx, list, dpy);
Saurabh Shah2b033392013-03-22 11:29:44 -0700147 bool ret = ctx->mMDPComp->prepare(ctx, list);
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800148 if(!ret) {
149 // IF MDPcomp fails use this route
Saurabh Shahacf10202013-02-26 10:15:15 -0800150 ctx->mVidOv[dpy]->prepare(ctx, list);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500151 ctx->mFBUpdate[dpy]->prepare(ctx, list);
Saurabh Shah2b033392013-03-22 11:29:44 -0700152 // Use Copybit, when MDP comp fails
153 if(ctx->mCopyBit[dpy])
154 ctx->mCopyBit[dpy]->prepare(ctx, list, dpy);
155 ctx->mLayerCache[dpy]->updateLayerCache(list);
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500156 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700157 }
158 }
159 return 0;
160}
161
162static int hwc_prepare_external(hwc_composer_device_1 *dev,
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800163 hwc_display_contents_1_t *list, int dpy) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700164 hwc_context_t* ctx = (hwc_context_t*)(dev);
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700165
Saurabh Shah2b033392013-03-22 11:29:44 -0700166 if (LIKELY(list && list->numHwLayers > 1) &&
167 ctx->dpyAttr[dpy].isActive &&
168 ctx->dpyAttr[dpy].connected) {
169 reset_layer_prop(ctx, dpy, list->numHwLayers - 1);
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700170 uint32_t last = list->numHwLayers - 1;
Saurabh Shah2b033392013-03-22 11:29:44 -0700171 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800172 if(!ctx->dpyAttr[dpy].isPause) {
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800173 if(fbLayer->handle) {
Saurabh Shah2b033392013-03-22 11:29:44 -0700174 ctx->mExtDispConfiguring = false;
175 if(list->numHwLayers > MAX_NUM_LAYERS) {
176 ctx->mFBUpdate[dpy]->prepare(ctx, list);
177 return 0;
178 }
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800179 setListStats(ctx, list, dpy);
Saurabh Shahacf10202013-02-26 10:15:15 -0800180 ctx->mVidOv[dpy]->prepare(ctx, list);
Naseer Ahmed64b81212013-02-14 10:29:47 -0500181 ctx->mFBUpdate[dpy]->prepare(ctx, list);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800182 ctx->mLayerCache[dpy]->updateLayerCache(list);
183 if(ctx->mCopyBit[dpy])
184 ctx->mCopyBit[dpy]->prepare(ctx, list, dpy);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800185 }
186 } else {
187 // External Display is in Pause state.
188 // ToDo:
189 // Mark all application layers as OVERLAY so that
190 // GPU will not compose. This is done for power
191 // optimization
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700192 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700193 }
194 return 0;
Saurabh Shah649cda62012-09-16 16:05:58 -0700195}
196
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700197static int hwc_prepare(hwc_composer_device_1 *dev, size_t numDisplays,
198 hwc_display_contents_1_t** displays)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700199{
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700200 int ret = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700201 hwc_context_t* ctx = (hwc_context_t*)(dev);
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500202 Locker::Autolock _l(ctx->mBlankLock);
Naseer Ahmedb1c76322012-10-17 00:32:50 -0400203 reset(ctx, numDisplays, displays);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700204
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500205 ctx->mOverlay->configBegin();
Saurabh Shahacf10202013-02-26 10:15:15 -0800206 ctx->mRotMgr->configBegin();
Jeykumar Sankarana37fdbf2013-03-06 18:59:28 -0800207 ctx->mNeedsRotator = false;
Saurabh Shah56f610d2012-08-07 15:27:06 -0700208
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800209 for (int32_t i = numDisplays; i >= 0; i--) {
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500210 hwc_display_contents_1_t *list = displays[i];
211 switch(i) {
212 case HWC_DISPLAY_PRIMARY:
213 ret = hwc_prepare_primary(dev, list);
214 break;
215 case HWC_DISPLAY_EXTERNAL:
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800216 case HWC_DISPLAY_VIRTUAL:
217 ret = hwc_prepare_external(dev, list, i);
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500218 break;
219 default:
220 ret = -EINVAL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700221 }
222 }
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500223
Saurabh Shahcf053c62012-12-13 12:32:55 -0800224 ctx->mOverlay->configDone();
Saurabh Shahacf10202013-02-26 10:15:15 -0800225 ctx->mRotMgr->configDone();
226
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700227 return ret;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700228}
229
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700230static int hwc_eventControl(struct hwc_composer_device_1* dev, int dpy,
Naseer Ahmed56601cd2013-03-05 11:34:14 -0500231 int event, int enable)
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700232{
233 int ret = 0;
234 hwc_context_t* ctx = (hwc_context_t*)(dev);
Iliyan Malcheveac89652012-10-04 10:38:28 -0700235 pthread_mutex_lock(&ctx->vstate.lock);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700236 switch(event) {
237 case HWC_EVENT_VSYNC:
Naseer Ahmed56601cd2013-03-05 11:34:14 -0500238 if (ctx->vstate.enable == enable)
Omprakash Dhyade1ef881c2012-10-03 02:26:55 -0700239 break;
Naseer Ahmed56601cd2013-03-05 11:34:14 -0500240 ret = hwc_vsync_control(ctx, dpy, enable);
241 if(ret == 0) {
242 ctx->vstate.enable = !!enable;
243 pthread_cond_signal(&ctx->vstate.cond);
244 }
Iliyan Malcheveac89652012-10-04 10:38:28 -0700245 ALOGD_IF (VSYNC_DEBUG, "VSYNC state changed to %s",
Naseer Ahmed56601cd2013-03-05 11:34:14 -0500246 (enable)?"ENABLED":"DISABLED");
Iliyan Malcheveac89652012-10-04 10:38:28 -0700247 break;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700248 default:
249 ret = -EINVAL;
250 }
Iliyan Malcheveac89652012-10-04 10:38:28 -0700251 pthread_mutex_unlock(&ctx->vstate.lock);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700252 return ret;
253}
254
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700255static int hwc_blank(struct hwc_composer_device_1* dev, int dpy, int blank)
256{
Naseer Ahmed64b81212013-02-14 10:29:47 -0500257 ATRACE_CALL();
Naseer Ahmed934790c2012-08-16 18:11:09 -0700258 hwc_context_t* ctx = (hwc_context_t*)(dev);
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800259
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500260 Locker::Autolock _l(ctx->mBlankLock);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700261 int ret = 0;
Naseer Ahmede0205b62013-04-04 13:24:39 -0400262 ALOGD_IF(BLANK_DEBUG, "%s: %s display: %d", __FUNCTION__,
Naseer Ahmed56601cd2013-03-05 11:34:14 -0500263 blank==1 ? "Blanking":"Unblanking", dpy);
Arun Kumar K.Re7408372013-03-28 18:41:06 -0700264 if(blank) {
265 // free up all the overlay pipes in use
266 // when we get a blank for either display
267 // makes sure that all pipes are freed
268 ctx->mOverlay->configBegin();
269 ctx->mOverlay->configDone();
270 ctx->mRotMgr->clear();
271 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700272 switch(dpy) {
273 case HWC_DISPLAY_PRIMARY:
274 if(blank) {
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800275 ret = ioctl(ctx->dpyAttr[dpy].fd, FBIOBLANK,FB_BLANK_POWERDOWN);
Ramkumar Radhakrishnan893d6ee2013-02-26 10:48:05 -0800276
277 if(ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].connected == true) {
278 // Surfaceflinger does not send Blank/unblank event to hwc
279 // for virtual display, handle it explicitly when blank for
280 // primary is invoked, so that any pipes unset get committed
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800281 if (display_commit(ctx, HWC_DISPLAY_VIRTUAL) < 0) {
Ramkumar Radhakrishnan893d6ee2013-02-26 10:48:05 -0800282 ret = -1;
283 ALOGE("%s:post failed for virtual display !!",
284 __FUNCTION__);
285 } else {
286 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].isActive = !blank;
287 }
288 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700289 } else {
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800290 ret = ioctl(ctx->dpyAttr[dpy].fd, FBIOBLANK, FB_BLANK_UNBLANK);
Ramkumar Radhakrishnan893d6ee2013-02-26 10:48:05 -0800291 if(ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].connected == true) {
292 ctx->dpyAttr[HWC_DISPLAY_VIRTUAL].isActive = !blank;
293 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700294 }
295 break;
296 case HWC_DISPLAY_EXTERNAL:
297 if(blank) {
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800298 // call external framebuffer commit on blank,
299 // so that any pipe unsets gets committed
300 if (display_commit(ctx, dpy) < 0) {
Arun Kumar K.Rba9eed52013-01-04 00:51:29 -0800301 ret = -1;
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800302 ALOGE("%s:post failed for external display !! ", __FUNCTION__);
Arun Kumar K.Rba9eed52013-01-04 00:51:29 -0800303 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700304 } else {
305 }
306 break;
307 default:
308 return -EINVAL;
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700309 }
Arun Kumar K.Rf6f49a12012-11-27 11:46:50 -0800310 // Enable HPD here, as during bootup unblank is called
311 // when SF is completely initialized
312 ctx->mExtDisplay->setHPD(1);
Naseer Ahmed56601cd2013-03-05 11:34:14 -0500313 if(ret == 0){
314 ctx->dpyAttr[dpy].isActive = !blank;
315 } else {
316 ALOGE("%s: Failed in %s display: %d error:%s", __FUNCTION__,
317 blank==1 ? "blanking":"unblanking", dpy, strerror(errno));
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700318 return ret;
319 }
Naseer Ahmed56601cd2013-03-05 11:34:14 -0500320
Naseer Ahmede0205b62013-04-04 13:24:39 -0400321 ALOGD_IF(BLANK_DEBUG, "%s: Done %s display: %d", __FUNCTION__,
Naseer Ahmed56601cd2013-03-05 11:34:14 -0500322 blank==1 ? "blanking":"unblanking", dpy);
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700323 return 0;
324}
325
326static int hwc_query(struct hwc_composer_device_1* dev,
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700327 int param, int* value)
328{
329 hwc_context_t* ctx = (hwc_context_t*)(dev);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700330 int supported = HWC_DISPLAY_PRIMARY_BIT;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700331
332 switch (param) {
333 case HWC_BACKGROUND_LAYER_SUPPORTED:
334 // Not supported for now
335 value[0] = 0;
336 break;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700337 case HWC_DISPLAY_TYPES_SUPPORTED:
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700338 if(ctx->mMDP.hasOverlay)
339 supported |= HWC_DISPLAY_EXTERNAL_BIT;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700340 value[0] = supported;
341 break;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700342 default:
343 return -EINVAL;
344 }
345 return 0;
346
347}
348
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800349
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700350static int hwc_set_primary(hwc_context_t *ctx, hwc_display_contents_1_t* list) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500351 ATRACE_CALL();
Saurabh Shaheaf67912012-10-10 17:33:14 -0700352 int ret = 0;
Saurabh Shahcf053c62012-12-13 12:32:55 -0800353 const int dpy = HWC_DISPLAY_PRIMARY;
Amara Venkata Mastan Manoj Kumar57e3c812013-01-11 13:36:33 -0800354 if (LIKELY(list) && ctx->dpyAttr[dpy].isActive) {
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700355 uint32_t last = list->numHwLayers - 1;
356 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800357 int fd = -1; //FenceFD from the Copybit(valid in async mode)
Naseer Ahmed64b81212013-02-14 10:29:47 -0500358 bool copybitDone = false;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800359 if(ctx->mCopyBit[dpy])
Naseer Ahmed64b81212013-02-14 10:29:47 -0500360 copybitDone = ctx->mCopyBit[dpy]->draw(ctx, list, dpy, &fd);
Amara Venkata Mastan Manoj Kumar57e3c812013-01-11 13:36:33 -0800361 if(list->numHwLayers > 1)
362 hwc_sync(ctx, list, dpy, fd);
Saurabh Shahacf10202013-02-26 10:15:15 -0800363 if (!ctx->mVidOv[dpy]->draw(ctx, list)) {
364 ALOGE("%s: VideoOverlay draw failed", __FUNCTION__);
Saurabh Shaheaf67912012-10-10 17:33:14 -0700365 ret = -1;
366 }
Saurabh Shahcbf7ccc2012-12-19 16:45:51 -0800367 if (!ctx->mMDPComp->draw(ctx, list)) {
Saurabh Shahacf10202013-02-26 10:15:15 -0800368 ALOGE("%s: MDPComp draw failed", __FUNCTION__);
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500369 ret = -1;
370 }
371
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700372 //TODO We dont check for SKIP flag on this layer because we need PAN
373 //always. Last layer is always FB
Saurabh Shah2b033392013-03-22 11:29:44 -0700374 private_handle_t *hnd = (private_handle_t *)fbLayer->handle;
Naseer Ahmed64b81212013-02-14 10:29:47 -0500375 if(copybitDone) {
376 hnd = ctx->mCopyBit[dpy]->getCurrentRenderBuffer();
Naseer Ahmed64b81212013-02-14 10:29:47 -0500377 }
Saurabh Shah2b033392013-03-22 11:29:44 -0700378
379 if(hnd) {
380 if (!ctx->mFBUpdate[dpy]->draw(ctx, hnd)) {
381 ALOGE("%s: FBUpdate draw failed", __FUNCTION__);
382 ret = -1;
Naseer Ahmed54821fe2012-11-28 18:44:38 -0500383 }
Saurabh Shah43333302013-02-06 13:30:27 -0800384 }
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800385
386 if (display_commit(ctx, dpy) < 0) {
387 ALOGE("%s: display commit fail!", __FUNCTION__);
388 return -1;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700389 }
390 }
Saurabh Shah747af1e2013-02-26 10:25:12 -0800391
392 closeAcquireFds(list);
Saurabh Shaheaf67912012-10-10 17:33:14 -0700393 return ret;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700394}
395
396static int hwc_set_external(hwc_context_t *ctx,
Naseer Ahmed64b81212013-02-14 10:29:47 -0500397 hwc_display_contents_1_t* list, int dpy)
398{
399 ATRACE_CALL();
Saurabh Shaheaf67912012-10-10 17:33:14 -0700400 int ret = 0;
Kinjal Bhavsarf83d4482012-10-10 15:56:21 -0700401 Locker::Autolock _l(ctx->mExtSetLock);
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700402
Amara Venkata Mastan Manoj Kumar57e3c812013-01-11 13:36:33 -0800403 if (LIKELY(list) && ctx->dpyAttr[dpy].isActive &&
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800404 !ctx->dpyAttr[dpy].isPause &&
Saurabh Shahcf053c62012-12-13 12:32:55 -0800405 ctx->dpyAttr[dpy].connected) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700406 uint32_t last = list->numHwLayers - 1;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700407 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800408 int fd = -1; //FenceFD from the Copybit(valid in async mode)
Naseer Ahmed64b81212013-02-14 10:29:47 -0500409 bool copybitDone = false;
Arun Kumar K.R361da4f2012-11-28 10:42:59 -0800410 if(ctx->mCopyBit[dpy])
Naseer Ahmed64b81212013-02-14 10:29:47 -0500411 copybitDone = ctx->mCopyBit[dpy]->draw(ctx, list, dpy, &fd);
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700412
Amara Venkata Mastan Manoj Kumar57e3c812013-01-11 13:36:33 -0800413 if(list->numHwLayers > 1)
414 hwc_sync(ctx, list, dpy, fd);
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700415
Saurabh Shahacf10202013-02-26 10:15:15 -0800416 if (!ctx->mVidOv[dpy]->draw(ctx, list)) {
Saurabh Shaheaf67912012-10-10 17:33:14 -0700417 ALOGE("%s: VideoOverlay::draw fail!", __FUNCTION__);
418 ret = -1;
419 }
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700420
Saurabh Shah2b033392013-03-22 11:29:44 -0700421 private_handle_t *hnd = (private_handle_t *)fbLayer->handle;
Naseer Ahmed64b81212013-02-14 10:29:47 -0500422 if(copybitDone) {
423 hnd = ctx->mCopyBit[dpy]->getCurrentRenderBuffer();
Naseer Ahmed64b81212013-02-14 10:29:47 -0500424 }
425
Saurabh Shah2b033392013-03-22 11:29:44 -0700426 if(hnd) {
Naseer Ahmed64b81212013-02-14 10:29:47 -0500427 if (!ctx->mFBUpdate[dpy]->draw(ctx, hnd)) {
Naseer Ahmed758bfc52012-11-28 17:02:08 -0500428 ALOGE("%s: FBUpdate::draw fail!", __FUNCTION__);
Saurabh Shaheaf67912012-10-10 17:33:14 -0700429 ret = -1;
430 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700431 }
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800432
433 if (display_commit(ctx, dpy) < 0) {
434 ALOGE("%s: display commit fail!", __FUNCTION__);
Saurabh Shah2b033392013-03-22 11:29:44 -0700435 ret = -1;
Saurabh Shaheaf67912012-10-10 17:33:14 -0700436 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700437 }
Saurabh Shah747af1e2013-02-26 10:25:12 -0800438
439 closeAcquireFds(list);
Saurabh Shaheaf67912012-10-10 17:33:14 -0700440 return ret;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700441}
442
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700443static int hwc_set(hwc_composer_device_1 *dev,
444 size_t numDisplays,
445 hwc_display_contents_1_t** displays)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700446{
447 int ret = 0;
448 hwc_context_t* ctx = (hwc_context_t*)(dev);
Naseer Ahmed32ff2252012-09-29 01:41:21 -0400449 Locker::Autolock _l(ctx->mBlankLock);
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800450 for (uint32_t i = 0; i <= numDisplays; i++) {
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700451 hwc_display_contents_1_t* list = displays[i];
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700452 switch(i) {
453 case HWC_DISPLAY_PRIMARY:
454 ret = hwc_set_primary(ctx, list);
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700455 break;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700456 case HWC_DISPLAY_EXTERNAL:
Amara Venkata Mastan Manoj Kumar11a380d2013-01-17 09:30:56 -0800457 case HWC_DISPLAY_VIRTUAL:
458 /* ToDo: We are using hwc_set_external path for both External and
459 Virtual displays on HWC1.1. Eventually, we will have
460 separate functions when we move to HWC1.2
461 */
462 ret = hwc_set_external(ctx, list, i);
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700463 break;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700464 default:
465 ret = -EINVAL;
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700466 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700467 }
Naseer Ahmed0d05d4f2013-03-28 14:49:07 -0400468 // This is only indicative of how many times SurfaceFlinger posts
469 // frames to the display.
470 CALC_FPS();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700471 return ret;
472}
473
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700474int hwc_getDisplayConfigs(struct hwc_composer_device_1* dev, int disp,
475 uint32_t* configs, size_t* numConfigs) {
476 int ret = 0;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700477 hwc_context_t* ctx = (hwc_context_t*)(dev);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700478 //in 1.1 there is no way to choose a config, report as config id # 0
479 //This config is passed to getDisplayAttributes. Ignore for now.
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700480 switch(disp) {
481 case HWC_DISPLAY_PRIMARY:
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700482 if(*numConfigs > 0) {
483 configs[0] = 0;
484 *numConfigs = 1;
485 }
486 ret = 0; //NO_ERROR
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700487 break;
488 case HWC_DISPLAY_EXTERNAL:
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700489 ret = -1; //Not connected
490 if(ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].connected) {
491 ret = 0; //NO_ERROR
492 if(*numConfigs > 0) {
493 configs[0] = 0;
494 *numConfigs = 1;
495 }
496 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700497 break;
498 }
499 return ret;
500}
501
502int hwc_getDisplayAttributes(struct hwc_composer_device_1* dev, int disp,
503 uint32_t config, const uint32_t* attributes, int32_t* values) {
504
505 hwc_context_t* ctx = (hwc_context_t*)(dev);
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700506 //If hotpluggable displays are inactive return error
507 if(disp == HWC_DISPLAY_EXTERNAL && !ctx->dpyAttr[disp].connected) {
508 return -1;
509 }
510
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700511 //From HWComposer
512 static const uint32_t DISPLAY_ATTRIBUTES[] = {
513 HWC_DISPLAY_VSYNC_PERIOD,
514 HWC_DISPLAY_WIDTH,
515 HWC_DISPLAY_HEIGHT,
516 HWC_DISPLAY_DPI_X,
517 HWC_DISPLAY_DPI_Y,
518 HWC_DISPLAY_NO_ATTRIBUTE,
519 };
520
521 const int NUM_DISPLAY_ATTRIBUTES = (sizeof(DISPLAY_ATTRIBUTES) /
522 sizeof(DISPLAY_ATTRIBUTES)[0]);
523
524 for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
525 switch (attributes[i]) {
526 case HWC_DISPLAY_VSYNC_PERIOD:
527 values[i] = ctx->dpyAttr[disp].vsync_period;
528 break;
529 case HWC_DISPLAY_WIDTH:
530 values[i] = ctx->dpyAttr[disp].xres;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700531 ALOGD("%s disp = %d, width = %d",__FUNCTION__, disp,
532 ctx->dpyAttr[disp].xres);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700533 break;
534 case HWC_DISPLAY_HEIGHT:
535 values[i] = ctx->dpyAttr[disp].yres;
Saurabh Shahc4d034f2012-09-27 15:55:15 -0700536 ALOGD("%s disp = %d, height = %d",__FUNCTION__, disp,
537 ctx->dpyAttr[disp].yres);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700538 break;
539 case HWC_DISPLAY_DPI_X:
Naseer Ahmed7b80d9c2012-09-26 20:14:38 -0400540 values[i] = (int32_t) (ctx->dpyAttr[disp].xdpi*1000.0);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700541 break;
542 case HWC_DISPLAY_DPI_Y:
Naseer Ahmed7b80d9c2012-09-26 20:14:38 -0400543 values[i] = (int32_t) (ctx->dpyAttr[disp].ydpi*1000.0);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700544 break;
545 default:
546 ALOGE("Unknown display attribute %d",
547 attributes[i]);
548 return -EINVAL;
549 }
550 }
551 return 0;
552}
553
Naseer Ahmed1d183f52012-11-26 12:35:16 -0500554void hwc_dump(struct hwc_composer_device_1* dev, char *buff, int buff_len)
555{
556 hwc_context_t* ctx = (hwc_context_t*)(dev);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800557 android::String8 aBuf("");
558 dumpsys_log(aBuf, "Qualcomm HWC state:\n");
559 dumpsys_log(aBuf, " MDPVersion=%d\n", ctx->mMDP.version);
560 dumpsys_log(aBuf, " DisplayPanel=%c\n", ctx->mMDP.panel);
561 ctx->mMDPComp->dump(aBuf);
562 char ovDump[2048] = {'\0'};
563 ctx->mOverlay->getDump(ovDump, 2048);
564 dumpsys_log(aBuf, ovDump);
Saurabh Shahacf10202013-02-26 10:15:15 -0800565 ovDump[0] = '\0';
566 ctx->mRotMgr->getDump(ovDump, 2048);
567 dumpsys_log(aBuf, ovDump);
Saurabh Shah0d0a7cb2013-02-12 17:58:19 -0800568 strlcpy(buff, aBuf.string(), buff_len);
Naseer Ahmed1d183f52012-11-26 12:35:16 -0500569}
570
Naseer Ahmed29a26812012-06-14 00:56:20 -0700571static int hwc_device_close(struct hw_device_t *dev)
572{
573 if(!dev) {
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700574 ALOGE("%s: NULL device pointer", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700575 return -1;
576 }
577 closeContext((hwc_context_t*)dev);
578 free(dev);
579
580 return 0;
581}
582
583static int hwc_device_open(const struct hw_module_t* module, const char* name,
584 struct hw_device_t** device)
585{
586 int status = -EINVAL;
587
588 if (!strcmp(name, HWC_HARDWARE_COMPOSER)) {
589 struct hwc_context_t *dev;
590 dev = (hwc_context_t*)malloc(sizeof(*dev));
591 memset(dev, 0, sizeof(*dev));
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700592
593 //Initialize hwc context
Naseer Ahmed29a26812012-06-14 00:56:20 -0700594 initContext(dev);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700595
596 //Setup HWC methods
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700597 dev->device.common.tag = HARDWARE_DEVICE_TAG;
598 dev->device.common.version = HWC_DEVICE_API_VERSION_1_1;
599 dev->device.common.module = const_cast<hw_module_t*>(module);
600 dev->device.common.close = hwc_device_close;
601 dev->device.prepare = hwc_prepare;
602 dev->device.set = hwc_set;
603 dev->device.eventControl = hwc_eventControl;
604 dev->device.blank = hwc_blank;
605 dev->device.query = hwc_query;
606 dev->device.registerProcs = hwc_registerProcs;
Naseer Ahmed1d183f52012-11-26 12:35:16 -0500607 dev->device.dump = hwc_dump;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700608 dev->device.getDisplayConfigs = hwc_getDisplayConfigs;
609 dev->device.getDisplayAttributes = hwc_getDisplayAttributes;
610 *device = &dev->device.common;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700611 status = 0;
612 }
613 return status;
614}