OSDN Git Service

9549804a9e5fd59d48aeb02291948d794c6e7f2f
[android-x86/hardware-intel-hwcomposer.git] / hwcomposer.cpp
1 /*
2  * Copyright (C) 2012 Intel Corporation. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <hardware/hardware.h>
18 #include <hardware/hwcomposer.h>
19 #include <hardware/gralloc.h>
20
21 #include <gralloc_drm.h>
22 #include <gralloc_drm_priv.h>
23 #include <gralloc_drm_handle.h>
24
25 #include <fcntl.h>
26 #include <errno.h>
27
28 #include <cutils/log.h>
29 #include <cutils/atomic.h>
30
31 #include <hardware/hwcomposer.h>
32
33 #include <EGL/egl.h>
34
35 struct hwc_context_t {
36         hwc_composer_device_t device;
37         struct drm_module_t *gralloc_module;
38 };
39
40 static int hwc_device_open(const struct hw_module_t* module, const char* name,
41                 struct hw_device_t** device);
42
43 static struct hw_module_methods_t hwc_module_methods = {
44         open: hwc_device_open
45 };
46
47 hwc_module_t HAL_MODULE_INFO_SYM = {
48         common: {
49                 tag: HARDWARE_MODULE_TAG,
50                 version_major: 1,
51                 version_minor: 0,
52                 id: HWC_HARDWARE_MODULE_ID,
53                 name: "Intel hwcomposer module",
54                 author: "Intel",
55                 methods: &hwc_module_methods,
56         }
57 };
58
59
60 static int hwc_prepare(hwc_composer_device_t *dev, hwc_layer_list_t* list)
61 {
62         struct hwc_context_t* ctx = (struct hwc_context_t *) &dev->common;
63
64         // SurfaceFlinger wants to handle the complete composition
65         if (!list || list->numHwLayers == 0)
66                 return 0;
67
68         int topmost = list->numHwLayers;
69         if (list->numHwLayers > 0)
70                 topmost--;
71
72         if (list->flags & HWC_GEOMETRY_CHANGED) {
73                 for (int i=topmost; i>=0; i--) {
74                         list->hwLayers[i].compositionType = HWC_FRAMEBUFFER;
75                 }
76         }
77         return 0;
78 }
79
80
81 static int hwc_set(hwc_composer_device_t *dev,
82                 hwc_display_t dpy,
83                 hwc_surface_t sur,
84                 hwc_layer_list_t* list)
85 {
86         struct hwc_context_t* ctx = (struct hwc_context_t*)dev;
87         EGLBoolean success;
88
89         // display is turning off
90         if (!dpy && !sur && !list)
91                 return 0;
92
93         success = eglSwapBuffers((EGLDisplay)dpy, (EGLSurface)sur);
94
95         if (!success)
96                 return HWC_EGL_ERROR;
97
98         return 0;
99 }
100
101 static int hwc_device_close(struct hw_device_t *dev)
102 {
103         struct hwc_context_t* ctx = (struct hwc_context_t*)dev;
104
105         if (ctx)
106                 free(ctx);
107
108         return 0;
109 }
110
111 /*****************************************************************************/
112
113 static int hwc_device_open(const struct hw_module_t* module, const char* name,
114                 struct hw_device_t** device)
115 {
116         int status = -EINVAL;
117         if (strcmp(name, HWC_HARDWARE_COMPOSER))
118                 return status;
119
120         struct hwc_context_t *dev;
121         dev = (hwc_context_t*)calloc(1, sizeof(*dev));
122
123         /* initialize the procs */
124         dev->device.common.tag = HARDWARE_DEVICE_TAG;
125         dev->device.common.version = 0;
126         dev->device.common.module = const_cast<hw_module_t*>(module);
127         dev->device.common.close = hwc_device_close;
128
129         dev->device.prepare = hwc_prepare;
130         dev->device.set = hwc_set;
131
132         *device = &dev->device.common;
133
134         int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID,
135                 (const hw_module_t **)&dev->gralloc_module);
136
137         ALOGD("Intel hwcomposer module");
138
139         return 0;
140 }