1 /*
2 * Copyright (C) 2010 The Android Open Source Project
3 * Copyright (C) 2012, The Linux Foundation. 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>
23 #include <EGL/egl.h>
24
25 #include <overlay.h>
26 #include <fb_priv.h>
27 #include <mdp_version.h>
28 #include "hwc_utils.h"
29 #include "hwc_video.h"
30 #include "hwc_fbupdate.h"
31 #include "hwc_mdpcomp.h"
32 #include "external.h"
33
34 using namespace qhwc;
35 #define VSYNC_DEBUG 0
36
37 static int hwc_device_open(const struct hw_module_t* module,
38 const char* name,
39 struct hw_device_t** device);
40
41 static struct hw_module_methods_t hwc_module_methods = {
42 open: hwc_device_open
43 };
44
45 hwc_module_t HAL_MODULE_INFO_SYM = {
46 common: {
47 tag: HARDWARE_MODULE_TAG,
48 version_major: 2,
49 version_minor: 0,
50 id: HWC_HARDWARE_MODULE_ID,
51 name: "Qualcomm Hardware Composer Module",
52 author: "CodeAurora Forum",
53 methods: &hwc_module_methods,
54 dso: 0,
55 reserved: {0},
56 }
57 };
58
59 /*
60 * Save callback functions registered to HWC
61 */
hwc_registerProcs(struct hwc_composer_device_1 * dev,hwc_procs_t const * procs)62 static void hwc_registerProcs(struct hwc_composer_device_1* dev,
63 hwc_procs_t const* procs)
64 {
65 ALOGI("%s", __FUNCTION__);
66 hwc_context_t* ctx = (hwc_context_t*)(dev);
67 if(!ctx) {
68 ALOGE("%s: Invalid context", __FUNCTION__);
69 return;
70 }
71 ctx->proc = procs;
72
73 // Now that we have the functions needed, kick off
74 // the uevent & vsync threads
75 init_uevent_thread(ctx);
76 init_vsync_thread(ctx);
77 }
78
79 //Helper
reset(hwc_context_t * ctx,int numDisplays,hwc_display_contents_1_t ** displays)80 static void reset(hwc_context_t *ctx, int numDisplays,
81 hwc_display_contents_1_t** displays) {
82 memset(ctx->listStats, 0, sizeof(ctx->listStats));
83 for(int i = 0; i < HWC_NUM_DISPLAY_TYPES; i++){
84 ctx->listStats[i].yuvIndex = -1;
85 hwc_display_contents_1_t *list = displays[i];
86 // XXX:SurfaceFlinger no longer guarantees that this
87 // value is reset on every prepare. However, for the layer
88 // cache we need to reset it.
89 // We can probably rethink that later on
90 if (LIKELY(list && list->numHwLayers > 1)) {
91 for(uint32_t j = 0; j < list->numHwLayers; j++) {
92 if(list->hwLayers[j].compositionType != HWC_FRAMEBUFFER_TARGET)
93 list->hwLayers[j].compositionType = HWC_FRAMEBUFFER;
94 }
95 }
96 }
97 VideoOverlay::reset();
98 FBUpdate::reset();
99 }
100
101 //clear prev layer prop flags and realloc for current frame
reset_layer_prop(hwc_context_t * ctx,int dpy)102 static void reset_layer_prop(hwc_context_t* ctx, int dpy) {
103 int layer_count = ctx->listStats[dpy].numAppLayers;
104
105 if(ctx->layerProp[dpy]) {
106 delete[] ctx->layerProp[dpy];
107 ctx->layerProp[dpy] = NULL;
108 }
109
110 if(layer_count) {
111 ctx->layerProp[dpy] = new LayerProp[layer_count];
112 }
113 }
114
hwc_prepare_primary(hwc_composer_device_1 * dev,hwc_display_contents_1_t * list)115 static int hwc_prepare_primary(hwc_composer_device_1 *dev,
116 hwc_display_contents_1_t *list) {
117 hwc_context_t* ctx = (hwc_context_t*)(dev);
118
119 if (LIKELY(list && list->numHwLayers > 1) &&
120 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].isActive) {
121
122 uint32_t last = list->numHwLayers - 1;
123 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
124 if(fbLayer->handle) {
125 setListStats(ctx, list, HWC_DISPLAY_PRIMARY);
126 reset_layer_prop(ctx, HWC_DISPLAY_PRIMARY);
127 if(!MDPComp::configure(ctx, list)) {
128 VideoOverlay::prepare(ctx, list, HWC_DISPLAY_PRIMARY);
129 FBUpdate::prepare(ctx, fbLayer, HWC_DISPLAY_PRIMARY);
130 }
131 ctx->mLayerCache[HWC_DISPLAY_PRIMARY]->updateLayerCache(list);
132 }
133 }
134 return 0;
135 }
136
hwc_prepare_external(hwc_composer_device_1 * dev,hwc_display_contents_1_t * list)137 static int hwc_prepare_external(hwc_composer_device_1 *dev,
138 hwc_display_contents_1_t *list) {
139 hwc_context_t* ctx = (hwc_context_t*)(dev);
140
141 if (LIKELY(list && list->numHwLayers > 1) &&
142 ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive &&
143 ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].connected) {
144
145 uint32_t last = list->numHwLayers - 1;
146 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
147 if(fbLayer->handle) {
148 setListStats(ctx, list, HWC_DISPLAY_EXTERNAL);
149 reset_layer_prop(ctx, HWC_DISPLAY_EXTERNAL);
150
151 VideoOverlay::prepare(ctx, list, HWC_DISPLAY_EXTERNAL);
152 FBUpdate::prepare(ctx, fbLayer, HWC_DISPLAY_EXTERNAL);
153 ctx->mLayerCache[HWC_DISPLAY_EXTERNAL]->updateLayerCache(list);
154 }
155 }
156 return 0;
157 }
158
hwc_prepare(hwc_composer_device_1 * dev,size_t numDisplays,hwc_display_contents_1_t ** displays)159 static int hwc_prepare(hwc_composer_device_1 *dev, size_t numDisplays,
160 hwc_display_contents_1_t** displays)
161 {
162 int ret = 0;
163 hwc_context_t* ctx = (hwc_context_t*)(dev);
164 Locker::Autolock _l(ctx->mBlankLock);
165 reset(ctx, numDisplays, displays);
166
167 ctx->mOverlay->configBegin();
168
169 for (int32_t i = numDisplays - 1; i >= 0; i--) {
170 hwc_display_contents_1_t *list = displays[i];
171 switch(i) {
172 case HWC_DISPLAY_PRIMARY:
173 ret = hwc_prepare_primary(dev, list);
174 break;
175 case HWC_DISPLAY_EXTERNAL:
176
177 ret = hwc_prepare_external(dev, list);
178 break;
179 default:
180 ret = -EINVAL;
181 }
182 }
183 ctx->mOverlay->configDone();
184
185 return ret;
186 }
187
hwc_eventControl(struct hwc_composer_device_1 * dev,int dpy,int event,int enabled)188 static int hwc_eventControl(struct hwc_composer_device_1* dev, int dpy,
189 int event, int enabled)
190 {
191 int ret = 0;
192
193 hwc_context_t* ctx = (hwc_context_t*)(dev);
194 private_module_t* m = reinterpret_cast<private_module_t*>(
195 ctx->mFbDev->common.module);
196 pthread_mutex_lock(&ctx->vstate.lock);
197 switch(event) {
198 case HWC_EVENT_VSYNC:
199 if (ctx->vstate.enable == enabled)
200 break;
201 ctx->vstate.enable = !!enabled;
202 pthread_cond_signal(&ctx->vstate.cond);
203 ALOGD_IF (VSYNC_DEBUG, "VSYNC state changed to %s",
204 (enabled)?"ENABLED":"DISABLED");
205 break;
206 default:
207 ret = -EINVAL;
208 }
209 pthread_mutex_unlock(&ctx->vstate.lock);
210 return ret;
211 }
212
hwc_blank(struct hwc_composer_device_1 * dev,int dpy,int blank)213 static int hwc_blank(struct hwc_composer_device_1* dev, int dpy, int blank)
214 {
215 hwc_context_t* ctx = (hwc_context_t*)(dev);
216 private_module_t* m = reinterpret_cast<private_module_t*>(
217 ctx->mFbDev->common.module);
218 Locker::Autolock _l(ctx->mBlankLock);
219 int ret = 0;
220 ALOGD("%s: Doing Dpy=%d, blank=%d", __FUNCTION__, dpy, blank);
221 switch(dpy) {
222 case HWC_DISPLAY_PRIMARY:
223 if(blank) {
224 ctx->mOverlay->configBegin();
225 ctx->mOverlay->configDone();
226 ret = ioctl(m->framebuffer->fd, FBIOBLANK, FB_BLANK_POWERDOWN);
227 } else {
228 ret = ioctl(m->framebuffer->fd, FBIOBLANK, FB_BLANK_UNBLANK);
229 }
230 break;
231 case HWC_DISPLAY_EXTERNAL:
232 if(blank) {
233 //TODO actual
234 } else {
235 }
236 break;
237 default:
238 return -EINVAL;
239 }
240
241 if(ret < 0) {
242 ALOGE("%s: failed. Dpy=%d, blank=%d : %s",
243 __FUNCTION__, dpy, blank, strerror(errno));
244 return ret;
245 }
246 ALOGD("%s: Done Dpy=%d, blank=%d", __FUNCTION__, dpy, blank);
247 ctx->dpyAttr[dpy].isActive = !blank;
248 return 0;
249 }
250
hwc_query(struct hwc_composer_device_1 * dev,int param,int * value)251 static int hwc_query(struct hwc_composer_device_1* dev,
252 int param, int* value)
253 {
254 hwc_context_t* ctx = (hwc_context_t*)(dev);
255 private_module_t* m = reinterpret_cast<private_module_t*>(
256 ctx->mFbDev->common.module);
257 int supported = HWC_DISPLAY_PRIMARY_BIT;
258
259 switch (param) {
260 case HWC_BACKGROUND_LAYER_SUPPORTED:
261 // Not supported for now
262 value[0] = 0;
263 break;
264 case HWC_VSYNC_PERIOD: //Not used for hwc > 1.1
265 value[0] = m->fps;
266 ALOGI("fps: %d", value[0]);
267 break;
268 case HWC_DISPLAY_TYPES_SUPPORTED:
269 if(ctx->mMDP.hasOverlay)
270 supported |= HWC_DISPLAY_EXTERNAL_BIT;
271 value[0] = supported;
272 break;
273 default:
274 return -EINVAL;
275 }
276 return 0;
277
278 }
279
hwc_set_primary(hwc_context_t * ctx,hwc_display_contents_1_t * list)280 static int hwc_set_primary(hwc_context_t *ctx, hwc_display_contents_1_t* list) {
281 int ret = 0;
282
283 if (LIKELY(list && list->numHwLayers > 1) &&
284 ctx->dpyAttr[HWC_DISPLAY_PRIMARY].isActive) {
285 uint32_t last = list->numHwLayers - 1;
286 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
287
288 hwc_sync(ctx, list, HWC_DISPLAY_PRIMARY);
289 if (!VideoOverlay::draw(ctx, list, HWC_DISPLAY_PRIMARY)) {
290 ALOGE("%s: VideoOverlay::draw fail!", __FUNCTION__);
291 ret = -1;
292 }
293 if (!MDPComp::draw(ctx, list)) {
294 ALOGE("%s: MDPComp::draw fail!", __FUNCTION__);
295 ret = -1;
296 }
297
298 //TODO We dont check for SKIP flag on this layer because we need PAN
299 //always. Last layer is always FB
300 private_handle_t *hnd = (private_handle_t *)fbLayer->handle;
301 if(fbLayer->compositionType == HWC_FRAMEBUFFER_TARGET && hnd) {
302 if(!(fbLayer->flags & HWC_SKIP_LAYER)) {
303 if (!FBUpdate::draw(ctx, fbLayer, HWC_DISPLAY_PRIMARY)) {
304 ALOGE("%s: FBUpdate::draw fail!", __FUNCTION__);
305 ret = -1;
306 }
307 }
308 if (ctx->mFbDev->post(ctx->mFbDev, fbLayer->handle)) {
309 ALOGE("%s: ctx->mFbDev->post fail!", __FUNCTION__);
310 return -1;
311 }
312 }
313 }
314 return ret;
315 }
316
hwc_set_external(hwc_context_t * ctx,hwc_display_contents_1_t * list)317 static int hwc_set_external(hwc_context_t *ctx,
318 hwc_display_contents_1_t* list) {
319 int ret = 0;
320 Locker::Autolock _l(ctx->mExtSetLock);
321
322 if (LIKELY(list && list->numHwLayers > 1) &&
323 ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].isActive &&
324 ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].connected) {
325 uint32_t last = list->numHwLayers - 1;
326 hwc_layer_1_t *fbLayer = &list->hwLayers[last];
327
328 hwc_sync(ctx, list, HWC_DISPLAY_EXTERNAL);
329
330 if (!VideoOverlay::draw(ctx, list, HWC_DISPLAY_EXTERNAL)) {
331 ALOGE("%s: VideoOverlay::draw fail!", __FUNCTION__);
332 ret = -1;
333 }
334
335 private_handle_t *hnd = (private_handle_t *)fbLayer->handle;
336 if(fbLayer->compositionType == HWC_FRAMEBUFFER_TARGET &&
337 !(fbLayer->flags & HWC_SKIP_LAYER) && hnd) {
338 if (!FBUpdate::draw(ctx, fbLayer, HWC_DISPLAY_EXTERNAL)) {
339 ALOGE("%s: FBUpdate::draw fail!", __FUNCTION__);
340 ret = -1;
341 }
342 }
343 if (!ctx->mExtDisplay->post()) {
344 ALOGE("%s: ctx->mExtDisplay->post fail!", __FUNCTION__);
345 return -1;
346 }
347 }
348 return ret;
349 }
350
hwc_set(hwc_composer_device_1 * dev,size_t numDisplays,hwc_display_contents_1_t ** displays)351 static int hwc_set(hwc_composer_device_1 *dev,
352 size_t numDisplays,
353 hwc_display_contents_1_t** displays)
354 {
355 int ret = 0;
356 hwc_context_t* ctx = (hwc_context_t*)(dev);
357 Locker::Autolock _l(ctx->mBlankLock);
358
359 for (uint32_t i = 0; i < numDisplays; i++) {
360 hwc_display_contents_1_t* list = displays[i];
361 switch(i) {
362 case HWC_DISPLAY_PRIMARY:
363 ret = hwc_set_primary(ctx, list);
364 break;
365 case HWC_DISPLAY_EXTERNAL:
366 ret = hwc_set_external(ctx, list);
367 break;
368 default:
369 ret = -EINVAL;
370 }
371 }
372 return ret;
373 }
374
hwc_getDisplayConfigs(struct hwc_composer_device_1 * dev,int disp,uint32_t * configs,size_t * numConfigs)375 int hwc_getDisplayConfigs(struct hwc_composer_device_1* dev, int disp,
376 uint32_t* configs, size_t* numConfigs) {
377 int ret = 0;
378 hwc_context_t* ctx = (hwc_context_t*)(dev);
379 //in 1.1 there is no way to choose a config, report as config id # 0
380 //This config is passed to getDisplayAttributes. Ignore for now.
381 switch(disp) {
382 case HWC_DISPLAY_PRIMARY:
383 if(*numConfigs > 0) {
384 configs[0] = 0;
385 *numConfigs = 1;
386 }
387 ret = 0; //NO_ERROR
388 break;
389 case HWC_DISPLAY_EXTERNAL:
390 ret = -1; //Not connected
391 if(ctx->dpyAttr[HWC_DISPLAY_EXTERNAL].connected) {
392 ret = 0; //NO_ERROR
393 if(*numConfigs > 0) {
394 configs[0] = 0;
395 *numConfigs = 1;
396 }
397 }
398 break;
399 }
400 return ret;
401 }
402
hwc_getDisplayAttributes(struct hwc_composer_device_1 * dev,int disp,uint32_t config,const uint32_t * attributes,int32_t * values)403 int hwc_getDisplayAttributes(struct hwc_composer_device_1* dev, int disp,
404 uint32_t config, const uint32_t* attributes, int32_t* values) {
405
406 hwc_context_t* ctx = (hwc_context_t*)(dev);
407 //If hotpluggable displays are inactive return error
408 if(disp == HWC_DISPLAY_EXTERNAL && !ctx->dpyAttr[disp].connected) {
409 return -1;
410 }
411
412 //From HWComposer
413 static const uint32_t DISPLAY_ATTRIBUTES[] = {
414 HWC_DISPLAY_VSYNC_PERIOD,
415 HWC_DISPLAY_WIDTH,
416 HWC_DISPLAY_HEIGHT,
417 HWC_DISPLAY_DPI_X,
418 HWC_DISPLAY_DPI_Y,
419 HWC_DISPLAY_NO_ATTRIBUTE,
420 };
421
422 const int NUM_DISPLAY_ATTRIBUTES = (sizeof(DISPLAY_ATTRIBUTES) /
423 sizeof(DISPLAY_ATTRIBUTES)[0]);
424
425 for (size_t i = 0; i < NUM_DISPLAY_ATTRIBUTES - 1; i++) {
426 switch (attributes[i]) {
427 case HWC_DISPLAY_VSYNC_PERIOD:
428 values[i] = ctx->dpyAttr[disp].vsync_period;
429 break;
430 case HWC_DISPLAY_WIDTH:
431 values[i] = ctx->dpyAttr[disp].xres;
432 ALOGD("%s disp = %d, width = %d",__FUNCTION__, disp,
433 ctx->dpyAttr[disp].xres);
434 break;
435 case HWC_DISPLAY_HEIGHT:
436 values[i] = ctx->dpyAttr[disp].yres;
437 ALOGD("%s disp = %d, height = %d",__FUNCTION__, disp,
438 ctx->dpyAttr[disp].yres);
439 break;
440 case HWC_DISPLAY_DPI_X:
441 values[i] = (int32_t) (ctx->dpyAttr[disp].xdpi*1000.0);
442 break;
443 case HWC_DISPLAY_DPI_Y:
444 values[i] = (int32_t) (ctx->dpyAttr[disp].ydpi*1000.0);
445 break;
446 default:
447 ALOGE("Unknown display attribute %d",
448 attributes[i]);
449 return -EINVAL;
450 }
451 }
452 return 0;
453 }
454
hwc_device_close(struct hw_device_t * dev)455 static int hwc_device_close(struct hw_device_t *dev)
456 {
457 if(!dev) {
458 ALOGE("%s: NULL device pointer", __FUNCTION__);
459 return -1;
460 }
461 closeContext((hwc_context_t*)dev);
462 free(dev);
463
464 return 0;
465 }
466
hwc_device_open(const struct hw_module_t * module,const char * name,struct hw_device_t ** device)467 static int hwc_device_open(const struct hw_module_t* module, const char* name,
468 struct hw_device_t** device)
469 {
470 int status = -EINVAL;
471
472 if (!strcmp(name, HWC_HARDWARE_COMPOSER)) {
473 struct hwc_context_t *dev;
474 dev = (hwc_context_t*)malloc(sizeof(*dev));
475 memset(dev, 0, sizeof(*dev));
476
477 //Initialize hwc context
478 initContext(dev);
479
480 //Setup HWC methods
481 dev->device.common.tag = HARDWARE_DEVICE_TAG;
482 dev->device.common.version = HWC_DEVICE_API_VERSION_1_1;
483 dev->device.common.module = const_cast<hw_module_t*>(module);
484 dev->device.common.close = hwc_device_close;
485 dev->device.prepare = hwc_prepare;
486 dev->device.set = hwc_set;
487 dev->device.eventControl = hwc_eventControl;
488 dev->device.blank = hwc_blank;
489 dev->device.query = hwc_query;
490 dev->device.registerProcs = hwc_registerProcs;
491 dev->device.dump = NULL;
492 dev->device.getDisplayConfigs = hwc_getDisplayConfigs;
493 dev->device.getDisplayAttributes = hwc_getDisplayAttributes;
494 *device = &dev->device.common;
495 status = 0;
496 }
497 return status;
498 }
499