1 /*
2  * Copyright (C) 2010 The Android Open Source Project
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 <errno.h>
18 #include <fcntl.h>
19 #include <malloc.h>
20 #include <string.h>
21 
22 #include <cutils/atomic.h>
23 #include <log/log.h>
24 
25 #include <hardware/hardware.h>
26 #include <hardware/hwcomposer.h>
27 
28 #include <EGL/egl.h>
29 
30 /*****************************************************************************/
31 
32 struct hwc_context_t {
33     hwc_composer_device_1_t device;
34     /* our private state goes below here */
35 };
36 
37 static int hwc_device_open(const struct hw_module_t* module, const char* name,
38         struct hw_device_t** device);
39 
40 static struct hw_module_methods_t hwc_module_methods = {
41     .open = hwc_device_open
42 };
43 
44 hwc_module_t HAL_MODULE_INFO_SYM = {
45     .common = {
46         .tag = HARDWARE_MODULE_TAG,
47         .version_major = 1,
48         .version_minor = 0,
49         .id = HWC_HARDWARE_MODULE_ID,
50         .name = "Sample hwcomposer module",
51         .author = "The Android Open Source Project",
52         .methods = &hwc_module_methods,
53     }
54 };
55 
56 /*****************************************************************************/
57 
58 #if 0
59 static void dump_layer(hwc_layer_1_t const* l) {
60     ALOGD("\ttype=%d, flags=%08x, handle=%p, tr=%02x, blend=%04x, {%d,%d,%d,%d}, {%d,%d,%d,%d}",
61             l->compositionType, l->flags, l->handle, l->transform, l->blending,
62             l->sourceCrop.left,
63             l->sourceCrop.top,
64             l->sourceCrop.right,
65             l->sourceCrop.bottom,
66             l->displayFrame.left,
67             l->displayFrame.top,
68             l->displayFrame.right,
69             l->displayFrame.bottom);
70 }
71 #endif
72 
hwc_prepare(hwc_composer_device_1_t *,size_t,hwc_display_contents_1_t ** displays)73 static int hwc_prepare(hwc_composer_device_1_t * /*dev*/,
74         size_t /*numDisplays*/, hwc_display_contents_1_t** displays) {
75     if (displays && (displays[0]->flags & HWC_GEOMETRY_CHANGED)) {
76         for (size_t i=0 ; i<displays[0]->numHwLayers ; i++) {
77             //dump_layer(&list->hwLayers[i]);
78             displays[0]->hwLayers[i].compositionType = HWC_FRAMEBUFFER;
79         }
80     }
81     return 0;
82 }
83 
hwc_set(hwc_composer_device_1_t *,size_t,hwc_display_contents_1_t ** displays)84 static int hwc_set(hwc_composer_device_1_t * /*dev*/,
85         size_t /*numDisplays*/, hwc_display_contents_1_t** displays)
86 {
87     //for (size_t i=0 ; i<list->numHwLayers ; i++) {
88     //    dump_layer(&list->hwLayers[i]);
89     //}
90 
91     EGLBoolean success = eglSwapBuffers((EGLDisplay)displays[0]->dpy,
92             (EGLSurface)displays[0]->sur);
93     if (!success) {
94         return HWC_EGL_ERROR;
95     }
96     return 0;
97 }
98 
hwc_device_close(struct hw_device_t * dev)99 static int hwc_device_close(struct hw_device_t *dev)
100 {
101     struct hwc_context_t* ctx = (struct hwc_context_t*)dev;
102     if (ctx) {
103         free(ctx);
104     }
105     return 0;
106 }
107 
108 /*****************************************************************************/
109 
hwc_device_open(const struct hw_module_t * module,const char * name,struct hw_device_t ** device)110 static int hwc_device_open(const struct hw_module_t* module, const char* name,
111         struct hw_device_t** device)
112 {
113     int status = -EINVAL;
114     if (!strcmp(name, HWC_HARDWARE_COMPOSER)) {
115         struct hwc_context_t *dev;
116         dev = (hwc_context_t*)malloc(sizeof(*dev));
117 
118         /* initialize our state here */
119         memset(dev, 0, sizeof(*dev));
120 
121         /* initialize the procs */
122         dev->device.common.tag = HARDWARE_DEVICE_TAG;
123         dev->device.common.version = HWC_DEVICE_API_VERSION_1_0;
124         dev->device.common.module = const_cast<hw_module_t*>(module);
125         dev->device.common.close = hwc_device_close;
126 
127         dev->device.prepare = hwc_prepare;
128         dev->device.set = hwc_set;
129 
130         *device = &dev->device.common;
131         status = 0;
132     }
133     return status;
134 }
135