blob: 15b712c030f65c67772e226fd11f581133fc0db3 [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 * Copyright (C) 2012, Code Aurora Forum. All rights reserved.
4 *
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 Ahmed72cf9762012-07-21 12:17:13 -070029#include "hwc_qbuf.h"
Naseer Ahmedf48aef62012-07-20 09:05:53 -070030#include "hwc_video.h"
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070031#include "hwc_uimirror.h"
Naseer Ahmed31da0b12012-07-31 18:55:33 -070032#include "hwc_copybit.h"
Naseer Ahmedf8ec1622012-07-31 18:56:23 -070033#include "hwc_external.h"
Naseer Ahmed7c958d42012-07-31 18:57:03 -070034#include "hwc_mdpcomp.h"
Naseer Ahmed4c588a22012-07-31 19:12:17 -070035#include "hwc_extonly.h"
Naseer Ahmed29a26812012-06-14 00:56:20 -070036
37using namespace qhwc;
38
39static int hwc_device_open(const struct hw_module_t* module,
40 const char* name,
41 struct hw_device_t** device);
42
43static struct hw_module_methods_t hwc_module_methods = {
44 open: hwc_device_open
45};
46
47hwc_module_t HAL_MODULE_INFO_SYM = {
48 common: {
49 tag: HARDWARE_MODULE_TAG,
50 version_major: 2,
51 version_minor: 0,
52 id: HWC_HARDWARE_MODULE_ID,
53 name: "Qualcomm Hardware Composer Module",
54 author: "CodeAurora Forum",
55 methods: &hwc_module_methods,
56 dso: 0,
57 reserved: {0},
58 }
59};
60
61/*
62 * Save callback functions registered to HWC
63 */
Naseer Ahmed5b6708a2012-08-02 13:46:08 -070064static void hwc_registerProcs(struct hwc_composer_device_1* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -070065 hwc_procs_t const* procs)
66{
67 hwc_context_t* ctx = (hwc_context_t*)(dev);
68 if(!ctx) {
69 ALOGE("%s: Invalid context", __FUNCTION__);
70 return;
71 }
Jesse Hall3be78d92012-08-21 15:12:23 -070072 ctx->proc = procs;
73
74 // don't start listening for events until we can do something with them
75 init_uevent_thread(ctx);
Naseer Ahmed29a26812012-06-14 00:56:20 -070076}
77
Naseer Ahmed5b6708a2012-08-02 13:46:08 -070078static int hwc_prepare(hwc_composer_device_1 *dev, size_t numDisplays,
79 hwc_display_contents_1_t** displays)
Naseer Ahmed29a26812012-06-14 00:56:20 -070080{
81 hwc_context_t* ctx = (hwc_context_t*)(dev);
Naseer Ahmedf48aef62012-07-20 09:05:53 -070082 ctx->overlayInUse = false;
83
Saurabh Shahaa537df2012-09-12 13:21:11 -070084 if(ctx->isPoweredDown) {
85 ALOGW("SurfaceFlinger called %s after a POWERDOWN", __FUNCTION__);
86 }
87
Naseer Ahmed5b6708a2012-08-02 13:46:08 -070088 for (uint32_t i = 0; i <numDisplays; i++) {
89 hwc_display_contents_1_t* list = displays[i];
90 ctx->dpys[i] = list->dpy;
91 //XXX: Actually handle the multiple displays
92 if (LIKELY(list)) {
93 //reset for this draw round
94 VideoOverlay::reset();
95 ExtOnly::reset();
Naseer Ahmed4c588a22012-07-31 19:12:17 -070096
Naseer Ahmed5b6708a2012-08-02 13:46:08 -070097 getLayerStats(ctx, list);
98 if(VideoOverlay::prepare(ctx, list)) {
99 ctx->overlayInUse = true;
100 //Nothing here
101 } else if(ExtOnly::prepare(ctx, list)) {
102 ctx->overlayInUse = true;
103 } else if(UIMirrorOverlay::prepare(ctx, list)) {
104 ctx->overlayInUse = true;
105 } else if(MDPComp::configure(dev, list)) {
106 ctx->overlayInUse = true;
107 } else if (0) {
108 //Other features
109 ctx->overlayInUse = true;
110 } else { // Else set this flag to false, otherwise video cases
111 // fail in non-overlay targets.
112 ctx->overlayInUse = false;
113 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700114 }
115 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700116
Naseer Ahmed29a26812012-06-14 00:56:20 -0700117 return 0;
118}
119
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700120static int hwc_eventControl(struct hwc_composer_device_1* dev, int dpy,
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700121 int event, int enabled)
122{
123 int ret = 0;
124 hwc_context_t* ctx = (hwc_context_t*)(dev);
125 private_module_t* m = reinterpret_cast<private_module_t*>(
126 ctx->mFbDev->common.module);
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700127 //XXX: Handle dpy
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700128 switch(event) {
129 case HWC_EVENT_VSYNC:
130 if(ioctl(m->framebuffer->fd, MSMFB_OVERLAY_VSYNC_CTRL, &enabled) < 0)
131 ret = -errno;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700132
133 if(ctx->mExtDisplay->getExternalDisplay()) {
134 ret = ctx->mExtDisplay->enableHDMIVsync(enabled);
135 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700136 break;
137 default:
138 ret = -EINVAL;
139 }
140 return ret;
141}
142
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700143static int hwc_blank(struct hwc_composer_device_1* dev, int dpy, int blank)
144{
145 //XXX: Handle based on dpy
Naseer Ahmed934790c2012-08-16 18:11:09 -0700146 hwc_context_t* ctx = (hwc_context_t*)(dev);
147 private_module_t* m = reinterpret_cast<private_module_t*>(
148 ctx->mFbDev->common.module);
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700149 if(blank) {
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700150 ctx->mOverlay->setState(ovutils::OV_CLOSED);
151 ctx->qbuf->unlockAllPrevious();
Saurabh Shahaa537df2012-09-12 13:21:11 -0700152 ALOGD("HWC Calling POWERDOWN ...");
Naseer Ahmed934790c2012-08-16 18:11:09 -0700153 ioctl(m->framebuffer->fd, FBIOBLANK, FB_BLANK_POWERDOWN);
Saurabh Shahaa537df2012-09-12 13:21:11 -0700154 ctx->isPoweredDown = true;
Naseer Ahmed934790c2012-08-16 18:11:09 -0700155 } else {
Saurabh Shahaa537df2012-09-12 13:21:11 -0700156 ALOGD("HWC Calling UNBLANK ...");
Naseer Ahmed934790c2012-08-16 18:11:09 -0700157 ioctl(m->framebuffer->fd, FBIOBLANK, FB_BLANK_UNBLANK);
Saurabh Shahaa537df2012-09-12 13:21:11 -0700158 ctx->isPoweredDown = false;
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700159 }
160 return 0;
161}
162
163static int hwc_query(struct hwc_composer_device_1* dev,
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700164 int param, int* value)
165{
166 hwc_context_t* ctx = (hwc_context_t*)(dev);
167 private_module_t* m = reinterpret_cast<private_module_t*>(
168 ctx->mFbDev->common.module);
169
170 switch (param) {
171 case HWC_BACKGROUND_LAYER_SUPPORTED:
172 // Not supported for now
173 value[0] = 0;
174 break;
175 case HWC_VSYNC_PERIOD:
Saurabh Shah2e2798c2012-08-30 14:47:10 -0700176 value[0] = m->fps;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700177 ALOGI("fps: %d", value[0]);
178 break;
179 default:
180 return -EINVAL;
181 }
182 return 0;
183
184}
185
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700186static int hwc_set(hwc_composer_device_1 *dev,
187 size_t numDisplays,
188 hwc_display_contents_1_t** displays)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700189{
190 int ret = 0;
191 hwc_context_t* ctx = (hwc_context_t*)(dev);
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700192 for (uint32_t i = 0; i <numDisplays; i++) {
193 hwc_display_contents_1_t* list = displays[i];
194 //XXX: Actually handle the multiple displays
195 if (LIKELY(list)) {
196 VideoOverlay::draw(ctx, list);
197 ExtOnly::draw(ctx, list);
198 MDPComp::draw(ctx, list);
199 EGLBoolean success = eglSwapBuffers((EGLDisplay)list->dpy,
200 (EGLSurface)list->sur);
Saurabh Shahfc2acbe2012-08-17 19:47:52 -0700201 wait4fbPost(ctx);
202 //Can draw to HDMI only when fb_post is reached
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700203 UIMirrorOverlay::draw(ctx);
Saurabh Shahfc2acbe2012-08-17 19:47:52 -0700204 //HDMI commit and primary commit (PAN) happening in parallel
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700205 if(ctx->mExtDisplay->getExternalDisplay())
206 ctx->mExtDisplay->commit();
Saurabh Shahfc2acbe2012-08-17 19:47:52 -0700207 //Virtual barrier for threads to finish
208 wait4Pan(ctx);
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700209 } else {
210 ctx->mOverlay->setState(ovutils::OV_CLOSED);
Saurabh Shah83523d82012-08-16 10:19:07 -0700211 ctx->qbuf->unlockAll();
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700212 }
213
214 if(!ctx->overlayInUse)
215 ctx->mOverlay->setState(ovutils::OV_CLOSED);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700216 }
Saurabh Shahdf727712012-08-23 08:41:05 -0700217 ctx->qbuf->unlockAllPrevious();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700218 return ret;
219}
220
221static int hwc_device_close(struct hw_device_t *dev)
222{
223 if(!dev) {
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700224 ALOGE("%s: NULL device pointer", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700225 return -1;
226 }
227 closeContext((hwc_context_t*)dev);
228 free(dev);
229
230 return 0;
231}
232
233static int hwc_device_open(const struct hw_module_t* module, const char* name,
234 struct hw_device_t** device)
235{
236 int status = -EINVAL;
237
238 if (!strcmp(name, HWC_HARDWARE_COMPOSER)) {
239 struct hwc_context_t *dev;
240 dev = (hwc_context_t*)malloc(sizeof(*dev));
241 memset(dev, 0, sizeof(*dev));
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700242
243 //Initialize hwc context
Naseer Ahmed29a26812012-06-14 00:56:20 -0700244 initContext(dev);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700245
246 //Setup HWC methods
Naseer Ahmed29a26812012-06-14 00:56:20 -0700247 dev->device.common.tag = HARDWARE_DEVICE_TAG;
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700248 dev->device.common.version = HWC_DEVICE_API_VERSION_1_0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700249 dev->device.common.module = const_cast<hw_module_t*>(module);
250 dev->device.common.close = hwc_device_close;
251 dev->device.prepare = hwc_prepare;
252 dev->device.set = hwc_set;
Jesse Hall3be78d92012-08-21 15:12:23 -0700253 dev->device.eventControl = hwc_eventControl;
254 dev->device.blank = hwc_blank;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700255 dev->device.query = hwc_query;
Jesse Hall3be78d92012-08-21 15:12:23 -0700256 dev->device.registerProcs = hwc_registerProcs;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700257 *device = &dev->device.common;
258 status = 0;
259 }
260 return status;
261}