blob: c0bca9da6c4c9498792f1ab13454c81f982a3dfc [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 Ahmed29a26812012-06-14 00:56:20 -070032
33using namespace qhwc;
34
35static int hwc_device_open(const struct hw_module_t* module,
36 const char* name,
37 struct hw_device_t** device);
38
39static struct hw_module_methods_t hwc_module_methods = {
40 open: hwc_device_open
41};
42
43hwc_module_t HAL_MODULE_INFO_SYM = {
44 common: {
45 tag: HARDWARE_MODULE_TAG,
46 version_major: 2,
47 version_minor: 0,
48 id: HWC_HARDWARE_MODULE_ID,
49 name: "Qualcomm Hardware Composer Module",
50 author: "CodeAurora Forum",
51 methods: &hwc_module_methods,
52 dso: 0,
53 reserved: {0},
54 }
55};
56
57/*
58 * Save callback functions registered to HWC
59 */
60static void hwc_registerProcs(struct hwc_composer_device* dev,
61 hwc_procs_t const* procs)
62{
63 hwc_context_t* ctx = (hwc_context_t*)(dev);
64 if(!ctx) {
65 ALOGE("%s: Invalid context", __FUNCTION__);
66 return;
67 }
68 ctx->device.reserved_proc[0] = (void*)procs;
69}
70
71static int hwc_prepare(hwc_composer_device_t *dev, hwc_layer_list_t* list)
72{
73 hwc_context_t* ctx = (hwc_context_t*)(dev);
Naseer Ahmedf48aef62012-07-20 09:05:53 -070074 ctx->overlayInUse = false;
75
76 //Prepare is called after a vsync, so unlock previous buffers here.
77 ctx->qbuf->unlockAllPrevious();
Ajay Dudanie217fbf2012-07-25 17:46:07 -070078 return 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -070079
Naseer Ahmed29a26812012-06-14 00:56:20 -070080 if (LIKELY(list)) {
81 getLayerStats(ctx, list);
Naseer Ahmedf48aef62012-07-20 09:05:53 -070082 if(VideoOverlay::prepare(ctx, list)) {
83 ctx->overlayInUse = true;
84 //Nothing here
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070085 } else if(UIMirrorOverlay::prepare(ctx, list)) {
86 ctx->overlayInUse = true;
Naseer Ahmedf48aef62012-07-20 09:05:53 -070087 } else if (0) {
88 //Other features
89 ctx->overlayInUse = true;
Naseer Ahmed31da0b12012-07-31 18:55:33 -070090 } else { // Else set this flag to false, otherwise video cases
91 // fail in non-overlay targets.
92 ctx->overlayInUse = false;
Naseer Ahmed29a26812012-06-14 00:56:20 -070093 }
Naseer Ahmed31da0b12012-07-31 18:55:33 -070094 CopyBit::prepare(ctx, list);
Naseer Ahmed29a26812012-06-14 00:56:20 -070095 }
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -070096
Naseer Ahmed29a26812012-06-14 00:56:20 -070097 return 0;
98}
99
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700100static int hwc_eventControl(struct hwc_composer_device* dev,
101 int event, int enabled)
102{
103 int ret = 0;
104 hwc_context_t* ctx = (hwc_context_t*)(dev);
105 private_module_t* m = reinterpret_cast<private_module_t*>(
106 ctx->mFbDev->common.module);
107 switch(event) {
108 case HWC_EVENT_VSYNC:
109 if(ioctl(m->framebuffer->fd, MSMFB_OVERLAY_VSYNC_CTRL, &enabled) < 0)
110 ret = -errno;
111 break;
112 default:
113 ret = -EINVAL;
114 }
115 return ret;
116}
117
118static int hwc_query(struct hwc_composer_device* dev,
119 int param, int* value)
120{
121 hwc_context_t* ctx = (hwc_context_t*)(dev);
122 private_module_t* m = reinterpret_cast<private_module_t*>(
123 ctx->mFbDev->common.module);
124
125 switch (param) {
126 case HWC_BACKGROUND_LAYER_SUPPORTED:
127 // Not supported for now
128 value[0] = 0;
129 break;
130 case HWC_VSYNC_PERIOD:
131 value[0] = 1000000000.0 / m->fps;
132 ALOGI("fps: %d", value[0]);
133 break;
134 default:
135 return -EINVAL;
136 }
137 return 0;
138
139}
140
Naseer Ahmed29a26812012-06-14 00:56:20 -0700141static int hwc_set(hwc_composer_device_t *dev,
142 hwc_display_t dpy,
143 hwc_surface_t sur,
144 hwc_layer_list_t* list)
145{
146 int ret = 0;
147 hwc_context_t* ctx = (hwc_context_t*)(dev);
148 if (LIKELY(list)) {
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700149 VideoOverlay::draw(ctx, list);
Naseer Ahmed31da0b12012-07-31 18:55:33 -0700150 CopyBit::draw(ctx, list, (EGLDisplay)dpy, (EGLSurface)sur);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700151 EGLBoolean sucess = eglSwapBuffers((EGLDisplay)dpy, (EGLSurface)sur);
Naseer Ahmed0c8b7b52012-07-20 09:06:13 -0700152 UIMirrorOverlay::draw(ctx);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700153 } else {
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700154 ctx->mOverlay->setState(ovutils::OV_CLOSED);
155 ctx->qbuf->unlockAllPrevious();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700156 }
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700157
158 if(!ctx->overlayInUse)
159 ctx->mOverlay->setState(ovutils::OV_CLOSED);
160
Naseer Ahmed29a26812012-06-14 00:56:20 -0700161 return ret;
162}
163
164static int hwc_device_close(struct hw_device_t *dev)
165{
166 if(!dev) {
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700167 ALOGE("%s: NULL device pointer", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700168 return -1;
169 }
170 closeContext((hwc_context_t*)dev);
171 free(dev);
172
173 return 0;
174}
175
176static int hwc_device_open(const struct hw_module_t* module, const char* name,
177 struct hw_device_t** device)
178{
179 int status = -EINVAL;
180
181 if (!strcmp(name, HWC_HARDWARE_COMPOSER)) {
182 struct hwc_context_t *dev;
183 dev = (hwc_context_t*)malloc(sizeof(*dev));
184 memset(dev, 0, sizeof(*dev));
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700185
186 //Initialize hwc context
Naseer Ahmed29a26812012-06-14 00:56:20 -0700187 initContext(dev);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700188
189 //Setup HWC methods
190 hwc_methods_t *methods;
191 methods = (hwc_methods_t *)malloc(sizeof(*methods));
192 memset(methods, 0, sizeof(*methods));
193 methods->eventControl = hwc_eventControl;
194
Naseer Ahmed29a26812012-06-14 00:56:20 -0700195 dev->device.common.tag = HARDWARE_DEVICE_TAG;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700196 dev->device.common.version = HWC_DEVICE_API_VERSION_0_3;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700197 dev->device.common.module = const_cast<hw_module_t*>(module);
198 dev->device.common.close = hwc_device_close;
199 dev->device.prepare = hwc_prepare;
200 dev->device.set = hwc_set;
201 dev->device.registerProcs = hwc_registerProcs;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700202 dev->device.query = hwc_query;
203 dev->device.methods = methods;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700204 *device = &dev->device.common;
205 status = 0;
206 }
207 return status;
208}