blob: 1b5b609c727b8bd8f2e9a33b10239d0ff6f56963 [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];
Naseer Ahmed6282c952012-09-26 18:33:58 -0400128 if(list && list->numHwLayers) {
129 uint32_t last = list->numHwLayers - 1;
130 if(list->hwLayers[last].handle != NULL) {
131 switch(i) {
132 case HWC_DISPLAY_PRIMARY:
133 ret = hwc_prepare_primary(dev, list);
134 break;
135 case HWC_DISPLAY_EXTERNAL:
136 ret = hwc_prepare_external(dev, list);
137 break;
138 default:
139 ret = -EINVAL;
140 }
141 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700142 }
143 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700144 return ret;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700145}
146
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700147static int hwc_eventControl(struct hwc_composer_device_1* dev, int dpy,
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700148 int event, int enabled)
149{
150 int ret = 0;
151 hwc_context_t* ctx = (hwc_context_t*)(dev);
152 private_module_t* m = reinterpret_cast<private_module_t*>(
153 ctx->mFbDev->common.module);
154 switch(event) {
155 case HWC_EVENT_VSYNC:
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700156 if(ioctl(ctx->dpyAttr[dpy].fd, MSMFB_OVERLAY_VSYNC_CTRL,
157 &enabled) < 0) {
158 ALOGE("%s: vsync control failed. Dpy=%d, enabled=%d : %s",
159 __FUNCTION__, dpy, enabled, strerror(errno));
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700160 ret = -errno;
Naseer Ahmedf8ec1622012-07-31 18:56:23 -0700161 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700162 break;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700163 default:
164 ret = -EINVAL;
165 }
166 return ret;
167}
168
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700169static int hwc_blank(struct hwc_composer_device_1* dev, int dpy, int blank)
170{
Naseer Ahmed934790c2012-08-16 18:11:09 -0700171 hwc_context_t* ctx = (hwc_context_t*)(dev);
172 private_module_t* m = reinterpret_cast<private_module_t*>(
173 ctx->mFbDev->common.module);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700174 int ret = 0;
175 ALOGD("%s: Doing Dpy=%d, blank=%d", __FUNCTION__, dpy, blank);
176 switch(dpy) {
177 case HWC_DISPLAY_PRIMARY:
178 if(blank) {
179 ctx->mOverlay->setState(ovutils::OV_CLOSED);
180 ret = ioctl(m->framebuffer->fd, FBIOBLANK, FB_BLANK_POWERDOWN);
181 } else {
182 ret = ioctl(m->framebuffer->fd, FBIOBLANK, FB_BLANK_UNBLANK);
183 }
184 break;
185 case HWC_DISPLAY_EXTERNAL:
186 if(blank) {
187 //TODO actual
188 } else {
189 }
190 break;
191 default:
192 return -EINVAL;
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700193 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700194
195 if(ret < 0) {
196 ALOGE("%s: failed. Dpy=%d, blank=%d : %s",
197 __FUNCTION__, dpy, blank, strerror(errno));
198 return ret;
199 }
200 ALOGD("%s: Done Dpy=%d, blank=%d", __FUNCTION__, dpy, blank);
201 ctx->dpyAttr[dpy].isActive = !blank;
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700202 return 0;
203}
204
205static int hwc_query(struct hwc_composer_device_1* dev,
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700206 int param, int* value)
207{
208 hwc_context_t* ctx = (hwc_context_t*)(dev);
209 private_module_t* m = reinterpret_cast<private_module_t*>(
210 ctx->mFbDev->common.module);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700211 int supported = HWC_DISPLAY_PRIMARY_BIT;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700212
213 switch (param) {
214 case HWC_BACKGROUND_LAYER_SUPPORTED:
215 // Not supported for now
216 value[0] = 0;
217 break;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700218 case HWC_VSYNC_PERIOD: //Not used for hwc > 1.1
Saurabh Shah2e2798c2012-08-30 14:47:10 -0700219 value[0] = m->fps;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700220 ALOGI("fps: %d", value[0]);
221 break;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700222 case HWC_DISPLAY_TYPES_SUPPORTED:
Saurabh Shah86623d72012-09-25 19:39:17 -0700223 //TODO Enable later
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700224 //if(ctx->mMDP.hasOverlay)
225 //supported |= HWC_DISPLAY_EXTERNAL_BIT;
226 value[0] = supported;
227 break;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700228 default:
229 return -EINVAL;
230 }
231 return 0;
232
233}
234
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700235static int hwc_set_primary(hwc_context_t *ctx, hwc_display_contents_1_t* list) {
236 if (LIKELY(list && list->numHwLayers)) {
237 VideoOverlay::draw(ctx, list, HWC_DISPLAY_PRIMARY);
238 MDPComp::draw(ctx, list);
Saurabh Shah86623d72012-09-25 19:39:17 -0700239 uint32_t last = list->numHwLayers - 1;
240 hwc_layer_1_t *fblayer = &list->hwLayers[last];
241 if(ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive) {
242 UIMirrorOverlay::draw(ctx, fblayer);
243 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700244 hwc_sync(ctx, list, HWC_DISPLAY_PRIMARY);
Saurabh Shah86623d72012-09-25 19:39:17 -0700245 if(ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive) {
246 ctx->mExtDisplay->post();
247 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700248 //TODO We dont check for SKIP flag on this layer because we need PAN
249 //always. Last layer is always FB
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700250 if(list->hwLayers[last].compositionType == HWC_FRAMEBUFFER_TARGET) {
251 ctx->mFbDev->post(ctx->mFbDev, list->hwLayers[last].handle);
252 }
253 }
254 return 0;
255}
256
257static int hwc_set_external(hwc_context_t *ctx,
258 hwc_display_contents_1_t* list) {
259 if (LIKELY(list && list->numHwLayers)) {
Saurabh Shah86623d72012-09-25 19:39:17 -0700260 //hwc_sync(ctx, list, HWC_DISPLAY_EXTERNAL);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700261 uint32_t last = list->numHwLayers - 1;
262 if(list->hwLayers[last].compositionType == HWC_FRAMEBUFFER_TARGET &&
263 ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive) {
264 //ctx->mExtDisplay->post(list->hwLayers[last].handle);
265 }
266 }
267 return 0;
268}
269
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700270static int hwc_set(hwc_composer_device_1 *dev,
271 size_t numDisplays,
272 hwc_display_contents_1_t** displays)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700273{
274 int ret = 0;
275 hwc_context_t* ctx = (hwc_context_t*)(dev);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700276 for (uint32_t i = 0; i < numDisplays; i++) {
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700277 hwc_display_contents_1_t* list = displays[i];
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700278 switch(i) {
279 case HWC_DISPLAY_PRIMARY:
280 ret = hwc_set_primary(ctx, list);
281 case HWC_DISPLAY_EXTERNAL:
282 ret = hwc_set_external(ctx, list);
283 default:
284 ret = -EINVAL;
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700285 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700286 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700287 if(!ctx->overlayInUse)
288 ctx->mOverlay->setState(ovutils::OV_CLOSED);
289
Naseer Ahmed29a26812012-06-14 00:56:20 -0700290 return ret;
291}
292
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700293int hwc_getDisplayConfigs(struct hwc_composer_device_1* dev, int disp,
294 uint32_t* configs, size_t* numConfigs) {
295 int ret = 0;
296 //in 1.1 there is no way to choose a config, report as config id # 0
297 //This config is passed to getDisplayAttributes. Ignore for now.
298 if(*numConfigs == 1)
299 *configs = 0;
300 switch(disp) {
301 case HWC_DISPLAY_PRIMARY:
302 ret = 0;
303 break;
304 case HWC_DISPLAY_EXTERNAL:
305 //Hack until hotplug is supported.
306 //This makes framework ignore external display.
307 ret = -1;
308 break;
309 }
310 return ret;
311}
312
313int hwc_getDisplayAttributes(struct hwc_composer_device_1* dev, int disp,
314 uint32_t config, const uint32_t* attributes, int32_t* values) {
315
316 hwc_context_t* ctx = (hwc_context_t*)(dev);
317 //From HWComposer
318 static const uint32_t DISPLAY_ATTRIBUTES[] = {
319 HWC_DISPLAY_VSYNC_PERIOD,
320 HWC_DISPLAY_WIDTH,
321 HWC_DISPLAY_HEIGHT,
322 HWC_DISPLAY_DPI_X,
323 HWC_DISPLAY_DPI_Y,
324 HWC_DISPLAY_NO_ATTRIBUTE,
325 };
326
327 const int NUM_DISPLAY_ATTRIBUTES = (sizeof(DISPLAY_ATTRIBUTES) /
328 sizeof(DISPLAY_ATTRIBUTES)[0]);
329
330 for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
331 switch (attributes[i]) {
332 case HWC_DISPLAY_VSYNC_PERIOD:
333 values[i] = ctx->dpyAttr[disp].vsync_period;
334 break;
335 case HWC_DISPLAY_WIDTH:
336 values[i] = ctx->dpyAttr[disp].xres;
337 ALOGD("%s width = %d",__FUNCTION__, ctx->dpyAttr[disp].xres);
338 break;
339 case HWC_DISPLAY_HEIGHT:
340 values[i] = ctx->dpyAttr[disp].yres;
341 ALOGD("%s height = %d",__FUNCTION__, ctx->dpyAttr[disp].yres);
342 break;
343 case HWC_DISPLAY_DPI_X:
Naseer Ahmed7b80d9c2012-09-26 20:14:38 -0400344 values[i] = (int32_t) (ctx->dpyAttr[disp].xdpi*1000.0);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700345 break;
346 case HWC_DISPLAY_DPI_Y:
Naseer Ahmed7b80d9c2012-09-26 20:14:38 -0400347 values[i] = (int32_t) (ctx->dpyAttr[disp].ydpi*1000.0);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700348 break;
349 default:
350 ALOGE("Unknown display attribute %d",
351 attributes[i]);
352 return -EINVAL;
353 }
354 }
355 return 0;
356}
357
Naseer Ahmed29a26812012-06-14 00:56:20 -0700358static int hwc_device_close(struct hw_device_t *dev)
359{
360 if(!dev) {
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700361 ALOGE("%s: NULL device pointer", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700362 return -1;
363 }
364 closeContext((hwc_context_t*)dev);
365 free(dev);
366
367 return 0;
368}
369
370static int hwc_device_open(const struct hw_module_t* module, const char* name,
371 struct hw_device_t** device)
372{
373 int status = -EINVAL;
374
375 if (!strcmp(name, HWC_HARDWARE_COMPOSER)) {
376 struct hwc_context_t *dev;
377 dev = (hwc_context_t*)malloc(sizeof(*dev));
378 memset(dev, 0, sizeof(*dev));
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700379
380 //Initialize hwc context
Naseer Ahmed29a26812012-06-14 00:56:20 -0700381 initContext(dev);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700382
383 //Setup HWC methods
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700384 dev->device.common.tag = HARDWARE_DEVICE_TAG;
385 dev->device.common.version = HWC_DEVICE_API_VERSION_1_1;
386 dev->device.common.module = const_cast<hw_module_t*>(module);
387 dev->device.common.close = hwc_device_close;
388 dev->device.prepare = hwc_prepare;
389 dev->device.set = hwc_set;
390 dev->device.eventControl = hwc_eventControl;
391 dev->device.blank = hwc_blank;
392 dev->device.query = hwc_query;
393 dev->device.registerProcs = hwc_registerProcs;
394 dev->device.dump = NULL;
395 dev->device.getDisplayConfigs = hwc_getDisplayConfigs;
396 dev->device.getDisplayAttributes = hwc_getDisplayAttributes;
397 *device = &dev->device.common;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700398 status = 0;
399 }
400 return status;
401}