blob: ba87f90f5c0bfdc9df46669c8383ba7eafa2ae51 [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 Ahmed29a26812012-06-14 00:56:20 -070027#include "hwc_utils.h"
Naseer Ahmed72cf9762012-07-21 12:17:13 -070028#include "hwc_qbuf.h"
Naseer Ahmedf48aef62012-07-20 09:05:53 -070029#include "hwc_video.h"
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070030#include "hwc_uimirror.h"
Naseer Ahmed31da0b12012-07-31 18:55:33 -070031#include "hwc_copybit.h"
Naseer Ahmedf8ec1622012-07-31 18:56:23 -070032#include "hwc_external.h"
Naseer Ahmed29a26812012-06-14 00:56:20 -070033
34using namespace qhwc;
35
36static int hwc_device_open(const struct hw_module_t* module,
37 const char* name,
38 struct hw_device_t** device);
39
40static struct hw_module_methods_t hwc_module_methods = {
41 open: hwc_device_open
42};
43
44hwc_module_t HAL_MODULE_INFO_SYM = {
45 common: {
46 tag: HARDWARE_MODULE_TAG,
47 version_major: 2,
48 version_minor: 0,
49 id: HWC_HARDWARE_MODULE_ID,
50 name: "Qualcomm Hardware Composer Module",
51 author: "CodeAurora Forum",
52 methods: &hwc_module_methods,
53 dso: 0,
54 reserved: {0},
55 }
56};
57
58/*
59 * Save callback functions registered to HWC
60 */
61static void hwc_registerProcs(struct hwc_composer_device* dev,
62 hwc_procs_t const* procs)
63{
64 hwc_context_t* ctx = (hwc_context_t*)(dev);
65 if(!ctx) {
66 ALOGE("%s: Invalid context", __FUNCTION__);
67 return;
68 }
69 ctx->device.reserved_proc[0] = (void*)procs;
70}
71
72static int hwc_prepare(hwc_composer_device_t *dev, hwc_layer_list_t* list)
73{
74 hwc_context_t* ctx = (hwc_context_t*)(dev);
Naseer Ahmedf48aef62012-07-20 09:05:53 -070075 ctx->overlayInUse = false;
76
77 //Prepare is called after a vsync, so unlock previous buffers here.
78 ctx->qbuf->unlockAllPrevious();
Ajay Dudanie217fbf2012-07-25 17:46:07 -070079 return 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -070080
Naseer Ahmed29a26812012-06-14 00:56:20 -070081 if (LIKELY(list)) {
82 getLayerStats(ctx, list);
Naseer Ahmedf48aef62012-07-20 09:05:53 -070083 if(VideoOverlay::prepare(ctx, list)) {
84 ctx->overlayInUse = true;
85 //Nothing here
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070086 } else if(UIMirrorOverlay::prepare(ctx, list)) {
87 ctx->overlayInUse = true;
Naseer Ahmedf48aef62012-07-20 09:05:53 -070088 } else if (0) {
89 //Other features
90 ctx->overlayInUse = true;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070091 } else { // Else set this flag to false, otherwise video cases
92 // fail in non-overlay targets.
93 ctx->overlayInUse = false;
Naseer Ahmed29a26812012-06-14 00:56:20 -070094 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -070095 CopyBit::prepare(ctx, list);
Naseer Ahmed29a26812012-06-14 00:56:20 -070096 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070097
Naseer Ahmed29a26812012-06-14 00:56:20 -070098 return 0;
99}
100
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700101static int hwc_eventControl(struct hwc_composer_device* dev,
102 int event, int enabled)
103{
104 int ret = 0;
105 hwc_context_t* ctx = (hwc_context_t*)(dev);
106 private_module_t* m = reinterpret_cast<private_module_t*>(
107 ctx->mFbDev->common.module);
108 switch(event) {
109 case HWC_EVENT_VSYNC:
110 if(ioctl(m->framebuffer->fd, MSMFB_OVERLAY_VSYNC_CTRL, &enabled) < 0)
111 ret = -errno;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700112
113 if(ctx->mExtDisplay->getExternalDisplay()) {
114 ret = ctx->mExtDisplay->enableHDMIVsync(enabled);
115 }
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700116 break;
117 default:
118 ret = -EINVAL;
119 }
120 return ret;
121}
122
123static int hwc_query(struct hwc_composer_device* dev,
124 int param, int* value)
125{
126 hwc_context_t* ctx = (hwc_context_t*)(dev);
127 private_module_t* m = reinterpret_cast<private_module_t*>(
128 ctx->mFbDev->common.module);
129
130 switch (param) {
131 case HWC_BACKGROUND_LAYER_SUPPORTED:
132 // Not supported for now
133 value[0] = 0;
134 break;
135 case HWC_VSYNC_PERIOD:
136 value[0] = 1000000000.0 / m->fps;
137 ALOGI("fps: %d", value[0]);
138 break;
139 default:
140 return -EINVAL;
141 }
142 return 0;
143
144}
145
Naseer Ahmed29a26812012-06-14 00:56:20 -0700146static int hwc_set(hwc_composer_device_t *dev,
147 hwc_display_t dpy,
148 hwc_surface_t sur,
149 hwc_layer_list_t* list)
150{
151 int ret = 0;
152 hwc_context_t* ctx = (hwc_context_t*)(dev);
153 if (LIKELY(list)) {
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700154 VideoOverlay::draw(ctx, list);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700155 CopyBit::draw(ctx, list, (EGLDisplay)dpy, (EGLSurface)sur);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700156 EGLBoolean sucess = eglSwapBuffers((EGLDisplay)dpy, (EGLSurface)sur);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700157 UIMirrorOverlay::draw(ctx);
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700158 if(ctx->mExtDisplay->getExternalDisplay())
159 ctx->mExtDisplay->commit();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700160 } else {
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700161 ctx->mOverlay->setState(ovutils::OV_CLOSED);
162 ctx->qbuf->unlockAllPrevious();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700163 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700164
165 if(!ctx->overlayInUse)
166 ctx->mOverlay->setState(ovutils::OV_CLOSED);
167
Naseer Ahmed29a26812012-06-14 00:56:20 -0700168 return ret;
169}
170
171static int hwc_device_close(struct hw_device_t *dev)
172{
173 if(!dev) {
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700174 ALOGE("%s: NULL device pointer", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700175 return -1;
176 }
177 closeContext((hwc_context_t*)dev);
178 free(dev);
179
180 return 0;
181}
182
183static int hwc_device_open(const struct hw_module_t* module, const char* name,
184 struct hw_device_t** device)
185{
186 int status = -EINVAL;
187
188 if (!strcmp(name, HWC_HARDWARE_COMPOSER)) {
189 struct hwc_context_t *dev;
190 dev = (hwc_context_t*)malloc(sizeof(*dev));
191 memset(dev, 0, sizeof(*dev));
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700192
193 //Initialize hwc context
Naseer Ahmed29a26812012-06-14 00:56:20 -0700194 initContext(dev);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700195
196 //Setup HWC methods
197 hwc_methods_t *methods;
198 methods = (hwc_methods_t *)malloc(sizeof(*methods));
199 memset(methods, 0, sizeof(*methods));
200 methods->eventControl = hwc_eventControl;
201
Naseer Ahmed29a26812012-06-14 00:56:20 -0700202 dev->device.common.tag = HARDWARE_DEVICE_TAG;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700203 dev->device.common.version = HWC_DEVICE_API_VERSION_0_3;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700204 dev->device.common.module = const_cast<hw_module_t*>(module);
205 dev->device.common.close = hwc_device_close;
206 dev->device.prepare = hwc_prepare;
207 dev->device.set = hwc_set;
208 dev->device.registerProcs = hwc_registerProcs;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700209 dev->device.query = hwc_query;
210 dev->device.methods = methods;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700211 *device = &dev->device.common;
212 status = 0;
213 }
214 return status;
215}