blob: 4a8d24dc65d93c02631df645eb1ccf65392d2ace [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) {
Naseer Ahmed32ff2252012-09-29 01:41:21 -0400179 Locker::Autolock _l(ctx->mBlankLock);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700180 ctx->mOverlay->setState(ovutils::OV_CLOSED);
181 ret = ioctl(m->framebuffer->fd, FBIOBLANK, FB_BLANK_POWERDOWN);
182 } else {
183 ret = ioctl(m->framebuffer->fd, FBIOBLANK, FB_BLANK_UNBLANK);
184 }
185 break;
186 case HWC_DISPLAY_EXTERNAL:
187 if(blank) {
188 //TODO actual
189 } else {
190 }
191 break;
192 default:
193 return -EINVAL;
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700194 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700195
196 if(ret < 0) {
197 ALOGE("%s: failed. Dpy=%d, blank=%d : %s",
198 __FUNCTION__, dpy, blank, strerror(errno));
199 return ret;
200 }
201 ALOGD("%s: Done Dpy=%d, blank=%d", __FUNCTION__, dpy, blank);
202 ctx->dpyAttr[dpy].isActive = !blank;
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700203 return 0;
204}
205
206static int hwc_query(struct hwc_composer_device_1* dev,
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700207 int param, int* value)
208{
209 hwc_context_t* ctx = (hwc_context_t*)(dev);
210 private_module_t* m = reinterpret_cast<private_module_t*>(
211 ctx->mFbDev->common.module);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700212 int supported = HWC_DISPLAY_PRIMARY_BIT;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700213
214 switch (param) {
215 case HWC_BACKGROUND_LAYER_SUPPORTED:
216 // Not supported for now
217 value[0] = 0;
218 break;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700219 case HWC_VSYNC_PERIOD: //Not used for hwc > 1.1
Saurabh Shah2e2798c2012-08-30 14:47:10 -0700220 value[0] = m->fps;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700221 ALOGI("fps: %d", value[0]);
222 break;
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700223 case HWC_DISPLAY_TYPES_SUPPORTED:
Saurabh Shah86623d72012-09-25 19:39:17 -0700224 //TODO Enable later
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700225 //if(ctx->mMDP.hasOverlay)
226 //supported |= HWC_DISPLAY_EXTERNAL_BIT;
227 value[0] = supported;
228 break;
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700229 default:
230 return -EINVAL;
231 }
232 return 0;
233
234}
235
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700236static int hwc_set_primary(hwc_context_t *ctx, hwc_display_contents_1_t* list) {
237 if (LIKELY(list && list->numHwLayers)) {
Saurabh Shah3d2a7902012-09-26 17:16:51 -0700238 ctx->mFbDev->compositionComplete(ctx->mFbDev);
Saurabh Shah8c8bfd22012-09-26 21:09:40 -0700239 hwc_sync(ctx, list, HWC_DISPLAY_PRIMARY);
240
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700241 VideoOverlay::draw(ctx, list, HWC_DISPLAY_PRIMARY);
242 MDPComp::draw(ctx, list);
Saurabh Shah86623d72012-09-25 19:39:17 -0700243 uint32_t last = list->numHwLayers - 1;
244 hwc_layer_1_t *fblayer = &list->hwLayers[last];
245 if(ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive) {
246 UIMirrorOverlay::draw(ctx, fblayer);
247 }
Saurabh Shah86623d72012-09-25 19:39:17 -0700248 if(ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive) {
249 ctx->mExtDisplay->post();
250 }
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700251 //TODO We dont check for SKIP flag on this layer because we need PAN
252 //always. Last layer is always FB
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700253 if(list->hwLayers[last].compositionType == HWC_FRAMEBUFFER_TARGET) {
254 ctx->mFbDev->post(ctx->mFbDev, list->hwLayers[last].handle);
255 }
256 }
257 return 0;
258}
259
260static int hwc_set_external(hwc_context_t *ctx,
261 hwc_display_contents_1_t* list) {
262 if (LIKELY(list && list->numHwLayers)) {
Saurabh Shah86623d72012-09-25 19:39:17 -0700263 //hwc_sync(ctx, list, HWC_DISPLAY_EXTERNAL);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700264 uint32_t last = list->numHwLayers - 1;
265 if(list->hwLayers[last].compositionType == HWC_FRAMEBUFFER_TARGET &&
266 ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive) {
267 //ctx->mExtDisplay->post(list->hwLayers[last].handle);
268 }
269 }
270 return 0;
271}
272
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700273static int hwc_set(hwc_composer_device_1 *dev,
274 size_t numDisplays,
275 hwc_display_contents_1_t** displays)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700276{
277 int ret = 0;
278 hwc_context_t* ctx = (hwc_context_t*)(dev);
Naseer Ahmed32ff2252012-09-29 01:41:21 -0400279 Locker::Autolock _l(ctx->mBlankLock);
Naseer Ahmedaee5e6e2012-09-27 13:17:59 -0400280 if(!ctx->overlayInUse)
281 ctx->mOverlay->setState(ovutils::OV_CLOSED);
282
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700283 for (uint32_t i = 0; i < numDisplays; i++) {
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700284 hwc_display_contents_1_t* list = displays[i];
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700285 switch(i) {
286 case HWC_DISPLAY_PRIMARY:
287 ret = hwc_set_primary(ctx, list);
288 case HWC_DISPLAY_EXTERNAL:
289 ret = hwc_set_external(ctx, list);
290 default:
291 ret = -EINVAL;
Naseer Ahmed5b6708a2012-08-02 13:46:08 -0700292 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700293 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700294 return ret;
295}
296
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700297int hwc_getDisplayConfigs(struct hwc_composer_device_1* dev, int disp,
298 uint32_t* configs, size_t* numConfigs) {
299 int ret = 0;
300 //in 1.1 there is no way to choose a config, report as config id # 0
301 //This config is passed to getDisplayAttributes. Ignore for now.
302 if(*numConfigs == 1)
303 *configs = 0;
304 switch(disp) {
305 case HWC_DISPLAY_PRIMARY:
306 ret = 0;
307 break;
308 case HWC_DISPLAY_EXTERNAL:
309 //Hack until hotplug is supported.
310 //This makes framework ignore external display.
311 ret = -1;
312 break;
313 }
314 return ret;
315}
316
317int hwc_getDisplayAttributes(struct hwc_composer_device_1* dev, int disp,
318 uint32_t config, const uint32_t* attributes, int32_t* values) {
319
320 hwc_context_t* ctx = (hwc_context_t*)(dev);
321 //From HWComposer
322 static const uint32_t DISPLAY_ATTRIBUTES[] = {
323 HWC_DISPLAY_VSYNC_PERIOD,
324 HWC_DISPLAY_WIDTH,
325 HWC_DISPLAY_HEIGHT,
326 HWC_DISPLAY_DPI_X,
327 HWC_DISPLAY_DPI_Y,
328 HWC_DISPLAY_NO_ATTRIBUTE,
329 };
330
331 const int NUM_DISPLAY_ATTRIBUTES = (sizeof(DISPLAY_ATTRIBUTES) /
332 sizeof(DISPLAY_ATTRIBUTES)[0]);
333
334 for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
335 switch (attributes[i]) {
336 case HWC_DISPLAY_VSYNC_PERIOD:
337 values[i] = ctx->dpyAttr[disp].vsync_period;
338 break;
339 case HWC_DISPLAY_WIDTH:
340 values[i] = ctx->dpyAttr[disp].xres;
341 ALOGD("%s width = %d",__FUNCTION__, ctx->dpyAttr[disp].xres);
342 break;
343 case HWC_DISPLAY_HEIGHT:
344 values[i] = ctx->dpyAttr[disp].yres;
345 ALOGD("%s height = %d",__FUNCTION__, ctx->dpyAttr[disp].yres);
346 break;
347 case HWC_DISPLAY_DPI_X:
Naseer Ahmed7b80d9c2012-09-26 20:14:38 -0400348 values[i] = (int32_t) (ctx->dpyAttr[disp].xdpi*1000.0);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700349 break;
350 case HWC_DISPLAY_DPI_Y:
Naseer Ahmed7b80d9c2012-09-26 20:14:38 -0400351 values[i] = (int32_t) (ctx->dpyAttr[disp].ydpi*1000.0);
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700352 break;
353 default:
354 ALOGE("Unknown display attribute %d",
355 attributes[i]);
356 return -EINVAL;
357 }
358 }
359 return 0;
360}
361
Naseer Ahmed29a26812012-06-14 00:56:20 -0700362static int hwc_device_close(struct hw_device_t *dev)
363{
364 if(!dev) {
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700365 ALOGE("%s: NULL device pointer", __FUNCTION__);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700366 return -1;
367 }
368 closeContext((hwc_context_t*)dev);
369 free(dev);
370
371 return 0;
372}
373
374static int hwc_device_open(const struct hw_module_t* module, const char* name,
375 struct hw_device_t** device)
376{
377 int status = -EINVAL;
378
379 if (!strcmp(name, HWC_HARDWARE_COMPOSER)) {
380 struct hwc_context_t *dev;
381 dev = (hwc_context_t*)malloc(sizeof(*dev));
382 memset(dev, 0, sizeof(*dev));
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700383
384 //Initialize hwc context
Naseer Ahmed29a26812012-06-14 00:56:20 -0700385 initContext(dev);
Naseer Ahmed72cf9762012-07-21 12:17:13 -0700386
387 //Setup HWC methods
Saurabh Shah3e858eb2012-09-17 16:53:21 -0700388 dev->device.common.tag = HARDWARE_DEVICE_TAG;
389 dev->device.common.version = HWC_DEVICE_API_VERSION_1_1;
390 dev->device.common.module = const_cast<hw_module_t*>(module);
391 dev->device.common.close = hwc_device_close;
392 dev->device.prepare = hwc_prepare;
393 dev->device.set = hwc_set;
394 dev->device.eventControl = hwc_eventControl;
395 dev->device.blank = hwc_blank;
396 dev->device.query = hwc_query;
397 dev->device.registerProcs = hwc_registerProcs;
398 dev->device.dump = NULL;
399 dev->device.getDisplayConfigs = hwc_getDisplayConfigs;
400 dev->device.getDisplayAttributes = hwc_getDisplayAttributes;
401 *device = &dev->device.common;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700402 status = 0;
403 }
404 return status;
405}