blob: 202e40df3cc6ae112732ea9eedf464f7f52e4337 [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
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 */
Naseer Ahmed5b6708a2012-08-02 13:46:08 -070061static void hwc_registerProcs(struct hwc_composer_device_1* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -070062 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 }
Jesse Hall3be78d92012-08-21 15:12:23 -070069 ctx->proc = procs;
70
71 // don't start listening for events until we can do something with them
72 init_uevent_thread(ctx);
Naseer Ahmed29a26812012-06-14 00:56:20 -070073}
74
Saurabh Shah649cda62012-09-16 16:05:58 -070075//Helper
Saurabh Shah3e858eb2012-09-17 16:53:21 -070076static void reset(hwc_context_t *ctx, int numDisplays) {
77 memset(ctx->listStats, 0, sizeof(ctx->listStats));
78 for(int i = 0; i < numDisplays; i++){
79 ctx->listStats[i].yuvIndex = -1;
80 }
81}
82
83static int hwc_prepare_primary(hwc_composer_device_1 *dev,
84 hwc_display_contents_1_t *list) {
85 hwc_context_t* ctx = (hwc_context_t*)(dev);
86 if (LIKELY(list && list->numHwLayers)) {
Saurabh Shah86623d72012-09-25 19:39:17 -070087 uint32_t last = list->numHwLayers - 1;
88 hwc_layer_1_t *fblayer = &list->hwLayers[last];
Saurabh Shah3e858eb2012-09-17 16:53:21 -070089 setListStats(ctx, list, HWC_DISPLAY_PRIMARY);
90 if(VideoOverlay::prepare(ctx, list, HWC_DISPLAY_PRIMARY)) {
91 ctx->overlayInUse = true;
Saurabh Shah86623d72012-09-25 19:39:17 -070092 } else if(UIMirrorOverlay::prepare(ctx, fblayer)) {
93 ctx->overlayInUse = true;
Saurabh Shah3e858eb2012-09-17 16:53:21 -070094 } else if(MDPComp::configure(ctx, list)) {
95 ctx->overlayInUse = true;
96 } else {
97 ctx->overlayInUse = false;
98 }
99 }
100 return 0;
101}
102
103static int hwc_prepare_external(hwc_composer_device_1 *dev,
104 hwc_display_contents_1_t *list) {
105
106 hwc_context_t* ctx = (hwc_context_t*)(dev);
107 if (LIKELY(list && list->numHwLayers)) {
Saurabh Shah86623d72012-09-25 19:39:17 -0700108 //setListStats(ctx, list, HWC_DISPLAY_EXTERNAL);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700109 //Nothing to do for now
110 }
111 return 0;
Saurabh Shah649cda62012-09-16 16:05:58 -0700112}
113
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700114static int hwc_prepare(hwc_composer_device_1 *dev, size_t numDisplays,
115 hwc_display_contents_1_t** displays)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700116{
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700117 int ret = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700118 hwc_context_t* ctx = (hwc_context_t*)(dev);
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700119 ctx->overlayInUse = false;
120
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700121 reset(ctx, numDisplays);
Saurabh Shah56f610d2012-08-07 15:27:06 -0700122
123 //If securing of h/w in progress skip comp using overlay.
124 if(ctx->mSecuring == true) return 0;
125
Saurabh Shah9171ec62012-09-13 09:33:51 -0700126 for (uint32_t i = 0; i < numDisplays; i++) {
127 hwc_display_contents_1_t *list = displays[i];
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700128 switch(i) {
129 case HWC_DISPLAY_PRIMARY:
130 ret = hwc_prepare_primary(dev, list);
131 break;
132 case HWC_DISPLAY_EXTERNAL:
133 ret = hwc_prepare_external(dev, list);
134 break;
135 default:
136 ret = -EINVAL;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700137 }
138 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700139 return ret;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700140}
141
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700142static int hwc_eventControl(struct hwc_composer_device_1* dev, int dpy,
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700143 int event, int enabled)
144{
145 int ret = 0;
146 hwc_context_t* ctx = (hwc_context_t*)(dev);
147 private_module_t* m = reinterpret_cast<private_module_t*>(
148 ctx->mFbDev->common.module);
149 switch(event) {
150 case HWC_EVENT_VSYNC:
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700151 if(ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL,
152 &enabled) < 0) {
153 ALOGE("%s: vsync control failed. Dpy=%d, enabled=%d : %s",
154 __FUNCTION__, dpy, enabled, strerror(errno));
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700155 ret = -errno;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700156 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700157 break;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700158 default:
159 ret = -EINVAL;
160 }
161 return ret;
162}
163
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700164static int hwc_blank(struct hwc_composer_device_1* dev, int dpy, int blank)
165{
Naseer Ahmed934790c2012-08-16 18:11:09 -0700166 hwc_context_t* ctx = (hwc_context_t*)(dev);
167 private_module_t* m = reinterpret_cast<private_module_t*>(
168 ctx->mFbDev->common.module);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700169 int ret = 0;
170 ALOGD("%s: Doing Dpy=%d, blank=%d", __FUNCTION__, dpy, blank);
171 switch(dpy) {
172 case HWC_DISPLAY_PRIMARY:
173 if(blank) {
174 ctx->mOverlay->setState(ovutils::OV_CLOSED);
175 ret = ioctl(m->framebuffer->fd, FBIOBLANK, FB_BLANK_POWERDOWN);
176 } else {
177 ret = ioctl(m->framebuffer->fd, FBIOBLANK, FB_BLANK_UNBLANK);
178 }
179 break;
180 case HWC_DISPLAY_EXTERNAL:
181 if(blank) {
182 //TODO actual
183 } else {
184 }
185 break;
186 default:
187 return -EINVAL;
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700188 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700189
190 if(ret < 0) {
191 ALOGE("%s: failed. Dpy=%d, blank=%d : %s",
192 __FUNCTION__, dpy, blank, strerror(errno));
193 return ret;
194 }
195 ALOGD("%s: Done Dpy=%d, blank=%d", __FUNCTION__, dpy, blank);
196 ctx->dpyAttr[dpy].isActive = !blank;
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700197 return 0;
198}
199
200static int hwc_query(struct hwc_composer_device_1* dev,
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700201 int param, int* value)
202{
203 hwc_context_t* ctx = (hwc_context_t*)(dev);
204 private_module_t* m = reinterpret_cast<private_module_t*>(
205 ctx->mFbDev->common.module);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700206 int supported = HWC_DISPLAY_PRIMARY_BIT;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700207
208 switch (param) {
209 case HWC_BACKGROUND_LAYER_SUPPORTED:
210 // Not supported for now
211 value[0] = 0;
212 break;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700213 case HWC_VSYNC_PERIOD: //Not used for hwc > 1.1
Saurabh Shah2e2798c2012-08-30 14:47:10 -0700214 value[0] = m->fps;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700215 ALOGI("fps: %d", value[0]);
216 break;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700217 case HWC_DISPLAY_TYPES_SUPPORTED:
Saurabh Shah86623d72012-09-25 19:39:17 -0700218 //TODO Enable later
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700219 //if(ctx->mMDP.hasOverlay)
220 //supported |= HWC_DISPLAY_EXTERNAL_BIT;
221 value[0] = supported;
222 break;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700223 default:
224 return -EINVAL;
225 }
226 return 0;
227
228}
229
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700230static int hwc_set_primary(hwc_context_t *ctx, hwc_display_contents_1_t* list) {
231 if (LIKELY(list && list->numHwLayers)) {
232 VideoOverlay::draw(ctx, list, HWC_DISPLAY_PRIMARY);
233 MDPComp::draw(ctx, list);
Saurabh Shah86623d72012-09-25 19:39:17 -0700234 uint32_t last = list->numHwLayers - 1;
235 hwc_layer_1_t *fblayer = &list->hwLayers[last];
236 if(ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive) {
237 UIMirrorOverlay::draw(ctx, fblayer);
238 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700239 hwc_sync(ctx, list, HWC_DISPLAY_PRIMARY);
Saurabh Shah86623d72012-09-25 19:39:17 -0700240 if(ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive) {
241 ctx->mExtDisplay->post();
242 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700243 //TODO We dont check for SKIP flag on this layer because we need PAN
244 //always. Last layer is always FB
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700245 if(list->hwLayers[last].compositionType == HWC_FRAMEBUFFER_TARGET) {
246 ctx->mFbDev->post(ctx->mFbDev, list->hwLayers[last].handle);
247 }
248 }
249 return 0;
250}
251
252static int hwc_set_external(hwc_context_t *ctx,
253 hwc_display_contents_1_t* list) {
254 if (LIKELY(list && list->numHwLayers)) {
Saurabh Shah86623d72012-09-25 19:39:17 -0700255 //hwc_sync(ctx, list, HWC_DISPLAY_EXTERNAL);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700256 uint32_t last = list->numHwLayers - 1;
257 if(list->hwLayers[last].compositionType == HWC_FRAMEBUFFER_TARGET &&
258 ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive) {
259 //ctx->mExtDisplay->post(list->hwLayers[last].handle);
260 }
261 }
262 return 0;
263}
264
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700265static int hwc_set(hwc_composer_device_1 *dev,
266 size_t numDisplays,
267 hwc_display_contents_1_t** displays)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700268{
269 int ret = 0;
270 hwc_context_t* ctx = (hwc_context_t*)(dev);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700271 for (uint32_t i = 0; i < numDisplays; i++) {
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700272 hwc_display_contents_1_t* list = displays[i];
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700273 switch(i) {
274 case HWC_DISPLAY_PRIMARY:
275 ret = hwc_set_primary(ctx, list);
276 case HWC_DISPLAY_EXTERNAL:
277 ret = hwc_set_external(ctx, list);
278 default:
279 ret = -EINVAL;
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700280 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700281 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700282 if(!ctx->overlayInUse)
283 ctx->mOverlay->setState(ovutils::OV_CLOSED);
284
Naseer Ahmed29a26812012-06-14 00:56:20 -0700285 return ret;
286}
287
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700288int hwc_getDisplayConfigs(struct hwc_composer_device_1* dev, int disp,
289 uint32_t* configs, size_t* numConfigs) {
290 int ret = 0;
291 //in 1.1 there is no way to choose a config, report as config id # 0
292 //This config is passed to getDisplayAttributes. Ignore for now.
293 if(*numConfigs == 1)
294 *configs = 0;
295 switch(disp) {
296 case HWC_DISPLAY_PRIMARY:
297 ret = 0;
298 break;
299 case HWC_DISPLAY_EXTERNAL:
300 //Hack until hotplug is supported.
301 //This makes framework ignore external display.
302 ret = -1;
303 break;
304 }
305 return ret;
306}
307
308int hwc_getDisplayAttributes(struct hwc_composer_device_1* dev, int disp,
309 uint32_t config, const uint32_t* attributes, int32_t* values) {
310
311 hwc_context_t* ctx = (hwc_context_t*)(dev);
312 //From HWComposer
313 static const uint32_t DISPLAY_ATTRIBUTES[] = {
314 HWC_DISPLAY_VSYNC_PERIOD,
315 HWC_DISPLAY_WIDTH,
316 HWC_DISPLAY_HEIGHT,
317 HWC_DISPLAY_DPI_X,
318 HWC_DISPLAY_DPI_Y,
319 HWC_DISPLAY_NO_ATTRIBUTE,
320 };
321
322 const int NUM_DISPLAY_ATTRIBUTES = (sizeof(DISPLAY_ATTRIBUTES) /
323 sizeof(DISPLAY_ATTRIBUTES)[0]);
324
325 for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
326 switch (attributes[i]) {
327 case HWC_DISPLAY_VSYNC_PERIOD:
328 values[i] = ctx->dpyAttr[disp].vsync_period;
329 break;
330 case HWC_DISPLAY_WIDTH:
331 values[i] = ctx->dpyAttr[disp].xres;
332 ALOGD("%s width = %d",__FUNCTION__, ctx->dpyAttr[disp].xres);
333 break;
334 case HWC_DISPLAY_HEIGHT:
335 values[i] = ctx->dpyAttr[disp].yres;
336 ALOGD("%s height = %d",__FUNCTION__, ctx->dpyAttr[disp].yres);
337 break;
338 case HWC_DISPLAY_DPI_X:
339 values[i] = ctx->dpyAttr[disp].xdpi;
340 break;
341 case HWC_DISPLAY_DPI_Y:
342 values[i] = ctx->dpyAttr[disp].ydpi;
343 break;
344 default:
345 ALOGE("Unknown display attribute %d",
346 attributes[i]);
347 return -EINVAL;
348 }
349 }
350 return 0;
351}
352
Naseer Ahmed29a26812012-06-14 00:56:20 -0700353static int hwc_device_close(struct hw_device_t *dev)
354{
355 if(!dev) {
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700356 ALOGE("%s: NULL device pointer", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700357 return -1;
358 }
359 closeContext((hwc_context_t*)dev);
360 free(dev);
361
362 return 0;
363}
364
365static int hwc_device_open(const struct hw_module_t* module, const char* name,
366 struct hw_device_t** device)
367{
368 int status = -EINVAL;
369
370 if (!strcmp(name, HWC_HARDWARE_COMPOSER)) {
371 struct hwc_context_t *dev;
372 dev = (hwc_context_t*)malloc(sizeof(*dev));
373 memset(dev, 0, sizeof(*dev));
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700374
375 //Initialize hwc context
Naseer Ahmed29a26812012-06-14 00:56:20 -0700376 initContext(dev);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700377
378 //Setup HWC methods
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700379 dev->device.common.tag = HARDWARE_DEVICE_TAG;
380 dev->device.common.version = HWC_DEVICE_API_VERSION_1_1;
381 dev->device.common.module = const_cast<hw_module_t*>(module);
382 dev->device.common.close = hwc_device_close;
383 dev->device.prepare = hwc_prepare;
384 dev->device.set = hwc_set;
385 dev->device.eventControl = hwc_eventControl;
386 dev->device.blank = hwc_blank;
387 dev->device.query = hwc_query;
388 dev->device.registerProcs = hwc_registerProcs;
389 dev->device.dump = NULL;
390 dev->device.getDisplayConfigs = hwc_getDisplayConfigs;
391 dev->device.getDisplayAttributes = hwc_getDisplayAttributes;
392 *device = &dev->device.common;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700393 status = 0;
394 }
395 return status;
396}