hwc.cpp revision bd4704d4b447bd4aa59e4894d68a7162ce4f99fd
1/*
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>
23
24#include "hwc_utils.h"
25#include "hwc_video.h"
26#include "hwc_uimirror.h"
27#include "hwc_copybit.h"
28
29using namespace qhwc;
30
31static int hwc_device_open(const struct hw_module_t* module,
32                           const char* name,
33                           struct hw_device_t** device);
34
35static struct hw_module_methods_t hwc_module_methods = {
36    open: hwc_device_open
37};
38
39hwc_module_t HAL_MODULE_INFO_SYM = {
40    common: {
41        tag: HARDWARE_MODULE_TAG,
42        version_major: 2,
43        version_minor: 0,
44        id: HWC_HARDWARE_MODULE_ID,
45        name: "Qualcomm Hardware Composer Module",
46        author: "CodeAurora Forum",
47        methods: &hwc_module_methods,
48        dso: 0,
49        reserved: {0},
50    }
51};
52
53/*
54 * Save callback functions registered to HWC
55 */
56static void hwc_registerProcs(struct hwc_composer_device* dev,
57                              hwc_procs_t const* procs)
58{
59    hwc_context_t* ctx = (hwc_context_t*)(dev);
60    if(!ctx) {
61        ALOGE("%s: Invalid context", __FUNCTION__);
62        return;
63    }
64    ctx->device.reserved_proc[0] = (void*)procs;
65}
66
67static int hwc_prepare(hwc_composer_device_t *dev, hwc_layer_list_t* list)
68{
69    hwc_context_t* ctx = (hwc_context_t*)(dev);
70    ctx->overlayInUse = false;
71
72    //Prepare is called after a vsync, so unlock previous buffers here.
73    ctx->qbuf->unlockAllPrevious();
74    return 0;
75
76    if (LIKELY(list)) {
77        getLayerStats(ctx, list);
78        if(VideoOverlay::prepare(ctx, list)) {
79            ctx->overlayInUse = true;
80            //Nothing here
81        } else if(UIMirrorOverlay::prepare(ctx, list)) {
82            ctx->overlayInUse = true;
83        } else if (0) {
84            //Other features
85            ctx->overlayInUse = true;
86        } else { // Else set this flag to false, otherwise video cases
87                 // fail in non-overlay targets.
88            ctx->overlayInUse = false;
89        }
90        CopyBit::prepare(ctx, list);
91    }
92
93    return 0;
94}
95
96static int hwc_set(hwc_composer_device_t *dev,
97                   hwc_display_t dpy,
98                   hwc_surface_t sur,
99                   hwc_layer_list_t* list)
100{
101    int ret = 0;
102    hwc_context_t* ctx = (hwc_context_t*)(dev);
103    if (LIKELY(list)) {
104        VideoOverlay::draw(ctx, list);
105        CopyBit::draw(ctx, list, (EGLDisplay)dpy, (EGLSurface)sur);
106        EGLBoolean sucess = eglSwapBuffers((EGLDisplay)dpy, (EGLSurface)sur);
107        UIMirrorOverlay::draw(ctx);
108    } else {
109        ctx->mOverlay->setState(ovutils::OV_CLOSED);
110        ctx->qbuf->unlockAllPrevious();
111    }
112
113    if(!ctx->overlayInUse)
114        ctx->mOverlay->setState(ovutils::OV_CLOSED);
115
116    return ret;
117}
118
119static int hwc_device_close(struct hw_device_t *dev)
120{
121    if(!dev) {
122        ALOGE("hwc_device_close null device pointer");
123        return -1;
124    }
125    closeContext((hwc_context_t*)dev);
126    free(dev);
127
128    return 0;
129}
130
131static int hwc_device_open(const struct hw_module_t* module, const char* name,
132                           struct hw_device_t** device)
133{
134    int status = -EINVAL;
135
136    if (!strcmp(name, HWC_HARDWARE_COMPOSER)) {
137        struct hwc_context_t *dev;
138        dev = (hwc_context_t*)malloc(sizeof(*dev));
139        memset(dev, 0, sizeof(*dev));
140        initContext(dev);
141        dev->device.common.tag     = HARDWARE_DEVICE_TAG;
142        dev->device.common.version = 0;
143        dev->device.common.module  = const_cast<hw_module_t*>(module);
144        dev->device.common.close   = hwc_device_close;
145        dev->device.prepare        = hwc_prepare;
146        dev->device.set            = hwc_set;
147        dev->device.registerProcs  = hwc_registerProcs;
148        *device                    = &dev->device.common;
149        status = 0;
150    }
151    return status;
152}
153