blob: f8f7f13981422ce74c8861019c66bbfd98033504 [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
18#include <fcntl.h>
19#include <errno.h>
20
21#include <cutils/log.h>
22#include <cutils/atomic.h>
Naseer Ahmed72cf9762012-07-21 12:17:13 -070023#include <EGL/egl.h>
Naseer Ahmed29a26812012-06-14 00:56:20 -070024
Naseer Ahmed72cf9762012-07-21 12:17:13 -070025#include <overlay.h>
26#include <fb_priv.h>
Naseer Ahmed96c4c952012-07-25 18:27:14 -070027#include <mdp_version.h>
Naseer Ahmed29a26812012-06-14 00:56:20 -070028#include "hwc_utils.h"
Naseer Ahmedf48aef62012-07-20 09:05:53 -070029#include "hwc_video.h"
Saurabh Shah86623d72012-09-25 19:39:17 -070030#include "hwc_uimirror.h"
Saurabh Shah56f610d2012-08-07 15:27:06 -070031#include "external.h"
Naseer Ahmed7c958d42012-07-31 18:57:03 -070032#include "hwc_mdpcomp.h"
Naseer Ahmed29a26812012-06-14 00:56:20 -070033
Naseer Ahmedff4f0252012-10-01 13:03:01 -040034#define VSYNC_DEBUG 0
Naseer Ahmed29a26812012-06-14 00:56:20 -070035using namespace qhwc;
36
37static int hwc_device_open(const struct hw_module_t* module,
38 const char* name,
39 struct hw_device_t** device);
40
41static struct hw_module_methods_t hwc_module_methods = {
42 open: hwc_device_open
43};
44
45hwc_module_t HAL_MODULE_INFO_SYM = {
46 common: {
47 tag: HARDWARE_MODULE_TAG,
48 version_major: 2,
49 version_minor: 0,
50 id: HWC_HARDWARE_MODULE_ID,
51 name: "Qualcomm Hardware Composer Module",
52 author: "CodeAurora Forum",
53 methods: &hwc_module_methods,
54 dso: 0,
55 reserved: {0},
56 }
57};
58
59/*
60 * Save callback functions registered to HWC
61 */
Naseer Ahmed5b6708a2012-08-02 13:46:08 -070062static void hwc_registerProcs(struct hwc_composer_device_1* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -070063 hwc_procs_t const* procs)
64{
65 hwc_context_t* ctx = (hwc_context_t*)(dev);
66 if(!ctx) {
67 ALOGE("%s: Invalid context", __FUNCTION__);
68 return;
69 }
Jesse Hall3be78d92012-08-21 15:12:23 -070070 ctx->proc = procs;
71
Naseer Ahmedff4f0252012-10-01 13:03:01 -040072 // Now that we have the functions needed, kick off
73 // the uevent & vsync threads
Jesse Hall3be78d92012-08-21 15:12:23 -070074 init_uevent_thread(ctx);
Naseer Ahmedff4f0252012-10-01 13:03:01 -040075 init_vsync_thread(ctx);
Naseer Ahmed29a26812012-06-14 00:56:20 -070076}
77
Saurabh Shah649cda62012-09-16 16:05:58 -070078//Helper
Saurabh Shah3e858eb2012-09-17 16:53:21 -070079static void reset(hwc_context_t *ctx, int numDisplays) {
80 memset(ctx->listStats, 0, sizeof(ctx->listStats));
81 for(int i = 0; i < numDisplays; i++){
82 ctx->listStats[i].yuvIndex = -1;
83 }
84}
85
86static int hwc_prepare_primary(hwc_composer_device_1 *dev,
87 hwc_display_contents_1_t *list) {
88 hwc_context_t* ctx = (hwc_context_t*)(dev);
89 if (LIKELY(list && list->numHwLayers)) {
Saurabh Shah86623d72012-09-25 19:39:17 -070090 uint32_t last = list->numHwLayers - 1;
91 hwc_layer_1_t *fblayer = &list->hwLayers[last];
Saurabh Shah3e858eb2012-09-17 16:53:21 -070092 setListStats(ctx, list, HWC_DISPLAY_PRIMARY);
93 if(VideoOverlay::prepare(ctx, list, HWC_DISPLAY_PRIMARY)) {
94 ctx->overlayInUse = true;
Saurabh Shah86623d72012-09-25 19:39:17 -070095 } else if(UIMirrorOverlay::prepare(ctx, fblayer)) {
96 ctx->overlayInUse = true;
Saurabh Shah3e858eb2012-09-17 16:53:21 -070097 } else if(MDPComp::configure(ctx, list)) {
98 ctx->overlayInUse = true;
99 } else {
100 ctx->overlayInUse = false;
101 }
102 }
103 return 0;
104}
105
106static int hwc_prepare_external(hwc_composer_device_1 *dev,
107 hwc_display_contents_1_t *list) {
108
109 hwc_context_t* ctx = (hwc_context_t*)(dev);
110 if (LIKELY(list && list->numHwLayers)) {
Saurabh Shah86623d72012-09-25 19:39:17 -0700111 //setListStats(ctx, list, HWC_DISPLAY_EXTERNAL);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700112 //Nothing to do for now
113 }
114 return 0;
Saurabh Shah649cda62012-09-16 16:05:58 -0700115}
116
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700117static int hwc_prepare(hwc_composer_device_1 *dev, size_t numDisplays,
118 hwc_display_contents_1_t** displays)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700119{
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700120 int ret = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700121 hwc_context_t* ctx = (hwc_context_t*)(dev);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700122 ctx->overlayInUse = false;
123
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700124 reset(ctx, numDisplays);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700125
126 //If securing of h/w in progress skip comp using overlay.
127 if(ctx->mSecuring == true) return 0;
128
Saurabh Shah9171ec62012-09-13 09:33:51 -0700129 for (uint32_t i = 0; i < numDisplays; i++) {
130 hwc_display_contents_1_t *list = displays[i];
Naseer Ahmed6282c952012-09-26 18:33:58 -0400131 if(list && list->numHwLayers) {
132 uint32_t last = list->numHwLayers - 1;
133 if(list->hwLayers[last].handle != NULL) {
134 switch(i) {
135 case HWC_DISPLAY_PRIMARY:
136 ret = hwc_prepare_primary(dev, list);
137 break;
138 case HWC_DISPLAY_EXTERNAL:
139 ret = hwc_prepare_external(dev, list);
140 break;
141 default:
142 ret = -EINVAL;
143 }
144 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700145 }
146 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700147 return ret;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700148}
149
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700150static int hwc_eventControl(struct hwc_composer_device_1* dev, int dpy,
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700151 int event, int enabled)
152{
153 int ret = 0;
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400154 static int prev_value;
155
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700156 hwc_context_t* ctx = (hwc_context_t*)(dev);
157 private_module_t* m = reinterpret_cast<private_module_t*>(
158 ctx->mFbDev->common.module);
159 switch(event) {
160 case HWC_EVENT_VSYNC:
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700161 if(ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL,
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400162 &enabled) < 0) {
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700163 ALOGE("%s: vsync control failed. Dpy=%d, enabled=%d : %s",
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400164 __FUNCTION__, dpy, enabled, strerror(errno));
165
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700166 ret = -errno;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700167 }
Naseer Ahmedff4f0252012-10-01 13:03:01 -0400168 /* vsync state change logic */
169 if (enabled == 1) {
170 //unblock vsync thread
171 pthread_mutex_lock(&ctx->vstate.lock);
172 ctx->vstate.enable = true;
173 pthread_cond_signal(&ctx->vstate.cond);
174 pthread_mutex_unlock(&ctx->vstate.lock);
175 } else if (enabled == 0 && ctx->vstate.enable) {
176 //vsync thread will block
177 pthread_mutex_lock(&ctx->vstate.lock);
178 ctx->vstate.enable = false;
179 pthread_mutex_unlock(&ctx->vstate.lock);
180 }
181 ALOGD_IF (VSYNC_DEBUG, "VSYNC state changed from %s to %s",
182 (prev_value)?"ENABLED":"DISABLED", (enabled)?"ENABLED":"DISABLED");
183 prev_value = enabled;
184 /* vsync state change logic - end*/
185 break;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700186 default:
187 ret = -EINVAL;
188 }
189 return ret;
190}
191
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700192static int hwc_blank(struct hwc_composer_device_1* dev, int dpy, int blank)
193{
Naseer Ahmed934790c2012-08-16 18:11:09 -0700194 hwc_context_t* ctx = (hwc_context_t*)(dev);
195 private_module_t* m = reinterpret_cast<private_module_t*>(
196 ctx->mFbDev->common.module);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700197 int ret = 0;
198 ALOGD("%s: Doing Dpy=%d, blank=%d", __FUNCTION__, dpy, blank);
199 switch(dpy) {
200 case HWC_DISPLAY_PRIMARY:
201 if(blank) {
Naseer Ahmed32ff2252012-09-29 01:41:21 -0400202 Locker::Autolock _l(ctx->mBlankLock);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700203 ctx->mOverlay->setState(ovutils::OV_CLOSED);
204 ret = ioctl(m->framebuffer->fd, FBIOBLANK, FB_BLANK_POWERDOWN);
205 } else {
206 ret = ioctl(m->framebuffer->fd, FBIOBLANK, FB_BLANK_UNBLANK);
207 }
208 break;
209 case HWC_DISPLAY_EXTERNAL:
210 if(blank) {
211 //TODO actual
212 } else {
213 }
214 break;
215 default:
216 return -EINVAL;
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700217 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700218
219 if(ret < 0) {
220 ALOGE("%s: failed. Dpy=%d, blank=%d : %s",
221 __FUNCTION__, dpy, blank, strerror(errno));
222 return ret;
223 }
224 ALOGD("%s: Done Dpy=%d, blank=%d", __FUNCTION__, dpy, blank);
225 ctx->dpyAttr[dpy].isActive = !blank;
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700226 return 0;
227}
228
229static int hwc_query(struct hwc_composer_device_1* dev,
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700230 int param, int* value)
231{
232 hwc_context_t* ctx = (hwc_context_t*)(dev);
233 private_module_t* m = reinterpret_cast<private_module_t*>(
234 ctx->mFbDev->common.module);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700235 int supported = HWC_DISPLAY_PRIMARY_BIT;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700236
237 switch (param) {
238 case HWC_BACKGROUND_LAYER_SUPPORTED:
239 // Not supported for now
240 value[0] = 0;
241 break;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700242 case HWC_VSYNC_PERIOD: //Not used for hwc > 1.1
Saurabh Shah2e2798c2012-08-30 14:47:10 -0700243 value[0] = m->fps;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700244 ALOGI("fps: %d", value[0]);
245 break;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700246 case HWC_DISPLAY_TYPES_SUPPORTED:
Saurabh Shah86623d72012-09-25 19:39:17 -0700247 //TODO Enable later
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700248 //if(ctx->mMDP.hasOverlay)
249 //supported |= HWC_DISPLAY_EXTERNAL_BIT;
250 value[0] = supported;
251 break;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700252 default:
253 return -EINVAL;
254 }
255 return 0;
256
257}
258
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700259static int hwc_set_primary(hwc_context_t *ctx, hwc_display_contents_1_t* list) {
260 if (LIKELY(list && list->numHwLayers)) {
Saurabh Shah3d2a7902012-09-26 17:16:51 -0700261 ctx->mFbDev->compositionComplete(ctx->mFbDev);
Saurabh Shah8c8bfd22012-09-26 21:09:40 -0700262 hwc_sync(ctx, list, HWC_DISPLAY_PRIMARY);
263
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700264 VideoOverlay::draw(ctx, list, HWC_DISPLAY_PRIMARY);
265 MDPComp::draw(ctx, list);
Saurabh Shah86623d72012-09-25 19:39:17 -0700266 uint32_t last = list->numHwLayers - 1;
267 hwc_layer_1_t *fblayer = &list->hwLayers[last];
268 if(ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive) {
269 UIMirrorOverlay::draw(ctx, fblayer);
270 }
Saurabh Shah86623d72012-09-25 19:39:17 -0700271 if(ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive) {
272 ctx->mExtDisplay->post();
273 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700274 //TODO We dont check for SKIP flag on this layer because we need PAN
275 //always. Last layer is always FB
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700276 if(list->hwLayers[last].compositionType == HWC_FRAMEBUFFER_TARGET) {
277 ctx->mFbDev->post(ctx->mFbDev, list->hwLayers[last].handle);
278 }
279 }
280 return 0;
281}
282
283static int hwc_set_external(hwc_context_t *ctx,
284 hwc_display_contents_1_t* list) {
285 if (LIKELY(list && list->numHwLayers)) {
Saurabh Shah86623d72012-09-25 19:39:17 -0700286 //hwc_sync(ctx, list, HWC_DISPLAY_EXTERNAL);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700287 uint32_t last = list->numHwLayers - 1;
288 if(list->hwLayers[last].compositionType == HWC_FRAMEBUFFER_TARGET &&
289 ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive) {
290 //ctx->mExtDisplay->post(list->hwLayers[last].handle);
291 }
292 }
293 return 0;
294}
295
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700296static int hwc_set(hwc_composer_device_1 *dev,
297 size_t numDisplays,
298 hwc_display_contents_1_t** displays)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700299{
300 int ret = 0;
301 hwc_context_t* ctx = (hwc_context_t*)(dev);
Naseer Ahmed32ff2252012-09-29 01:41:21 -0400302 Locker::Autolock _l(ctx->mBlankLock);
Naseer Ahmedaee5e6e2012-09-27 13:17:59 -0400303 if(!ctx->overlayInUse)
304 ctx->mOverlay->setState(ovutils::OV_CLOSED);
305
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700306 for (uint32_t i = 0; i < numDisplays; i++) {
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700307 hwc_display_contents_1_t* list = displays[i];
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700308 switch(i) {
309 case HWC_DISPLAY_PRIMARY:
310 ret = hwc_set_primary(ctx, list);
311 case HWC_DISPLAY_EXTERNAL:
312 ret = hwc_set_external(ctx, list);
313 default:
314 ret = -EINVAL;
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700315 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700316 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700317 return ret;
318}
319
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700320int hwc_getDisplayConfigs(struct hwc_composer_device_1* dev, int disp,
321 uint32_t* configs, size_t* numConfigs) {
322 int ret = 0;
323 //in 1.1 there is no way to choose a config, report as config id # 0
324 //This config is passed to getDisplayAttributes. Ignore for now.
325 if(*numConfigs == 1)
326 *configs = 0;
327 switch(disp) {
328 case HWC_DISPLAY_PRIMARY:
329 ret = 0;
330 break;
331 case HWC_DISPLAY_EXTERNAL:
332 //Hack until hotplug is supported.
333 //This makes framework ignore external display.
334 ret = -1;
335 break;
336 }
337 return ret;
338}
339
340int hwc_getDisplayAttributes(struct hwc_composer_device_1* dev, int disp,
341 uint32_t config, const uint32_t* attributes, int32_t* values) {
342
343 hwc_context_t* ctx = (hwc_context_t*)(dev);
344 //From HWComposer
345 static const uint32_t DISPLAY_ATTRIBUTES[] = {
346 HWC_DISPLAY_VSYNC_PERIOD,
347 HWC_DISPLAY_WIDTH,
348 HWC_DISPLAY_HEIGHT,
349 HWC_DISPLAY_DPI_X,
350 HWC_DISPLAY_DPI_Y,
351 HWC_DISPLAY_NO_ATTRIBUTE,
352 };
353
354 const int NUM_DISPLAY_ATTRIBUTES = (sizeof(DISPLAY_ATTRIBUTES) /
355 sizeof(DISPLAY_ATTRIBUTES)[0]);
356
357 for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
358 switch (attributes[i]) {
359 case HWC_DISPLAY_VSYNC_PERIOD:
360 values[i] = ctx->dpyAttr[disp].vsync_period;
361 break;
362 case HWC_DISPLAY_WIDTH:
363 values[i] = ctx->dpyAttr[disp].xres;
364 ALOGD("%s width = %d",__FUNCTION__, ctx->dpyAttr[disp].xres);
365 break;
366 case HWC_DISPLAY_HEIGHT:
367 values[i] = ctx->dpyAttr[disp].yres;
368 ALOGD("%s height = %d",__FUNCTION__, ctx->dpyAttr[disp].yres);
369 break;
370 case HWC_DISPLAY_DPI_X:
Naseer Ahmed7b80d9c2012-09-26 20:14:38 -0400371 values[i] = (int32_t) (ctx->dpyAttr[disp].xdpi*1000.0);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700372 break;
373 case HWC_DISPLAY_DPI_Y:
Naseer Ahmed7b80d9c2012-09-26 20:14:38 -0400374 values[i] = (int32_t) (ctx->dpyAttr[disp].ydpi*1000.0);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700375 break;
376 default:
377 ALOGE("Unknown display attribute %d",
378 attributes[i]);
379 return -EINVAL;
380 }
381 }
382 return 0;
383}
384
Naseer Ahmed29a26812012-06-14 00:56:20 -0700385static int hwc_device_close(struct hw_device_t *dev)
386{
387 if(!dev) {
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700388 ALOGE("%s: NULL device pointer", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700389 return -1;
390 }
391 closeContext((hwc_context_t*)dev);
392 free(dev);
393
394 return 0;
395}
396
397static int hwc_device_open(const struct hw_module_t* module, const char* name,
398 struct hw_device_t** device)
399{
400 int status = -EINVAL;
401
402 if (!strcmp(name, HWC_HARDWARE_COMPOSER)) {
403 struct hwc_context_t *dev;
404 dev = (hwc_context_t*)malloc(sizeof(*dev));
405 memset(dev, 0, sizeof(*dev));
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700406
407 //Initialize hwc context
Naseer Ahmed29a26812012-06-14 00:56:20 -0700408 initContext(dev);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700409
410 //Setup HWC methods
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700411 dev->device.common.tag = HARDWARE_DEVICE_TAG;
412 dev->device.common.version = HWC_DEVICE_API_VERSION_1_1;
413 dev->device.common.module = const_cast<hw_module_t*>(module);
414 dev->device.common.close = hwc_device_close;
415 dev->device.prepare = hwc_prepare;
416 dev->device.set = hwc_set;
417 dev->device.eventControl = hwc_eventControl;
418 dev->device.blank = hwc_blank;
419 dev->device.query = hwc_query;
420 dev->device.registerProcs = hwc_registerProcs;
421 dev->device.dump = NULL;
422 dev->device.getDisplayConfigs = hwc_getDisplayConfigs;
423 dev->device.getDisplayAttributes = hwc_getDisplayAttributes;
424 *device = &dev->device.common;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700425 status = 0;
426 }
427 return status;
428}