15d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian/*
25d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian * Copyright (C) 2010 The Android Open Source Project
35d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian *
45d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
55d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian * you may not use this file except in compliance with the License.
65d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian * You may obtain a copy of the License at
75d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian *
85d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
95d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian *
105d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian * Unless required by applicable law or agreed to in writing, software
115d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
125d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian * See the License for the specific language governing permissions and
145d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian * limitations under the License.
155d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian */
165d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian
175d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian#include <hardware/hardware.h>
185d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian
195d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian#include <fcntl.h>
205d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian#include <errno.h>
215d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian
225d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian#include <cutils/log.h>
235d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian#include <cutils/atomic.h>
245d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian
255d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian#include <hardware/hwcomposer.h>
265d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian
275d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian#include <EGL/egl.h>
285d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian
295d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian/*****************************************************************************/
305d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian
315d3de309f44f6a72f2a46db792f3865088897039Mathias Agopianstruct hwc_context_t {
32d479ad22a0254fa0b5358fe82fa404e3e96c631aJesse Hall    hwc_composer_device_1_t device;
335d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian    /* our private state goes below here */
345d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian};
355d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian
365d3de309f44f6a72f2a46db792f3865088897039Mathias Agopianstatic int hwc_device_open(const struct hw_module_t* module, const char* name,
375d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        struct hw_device_t** device);
385d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian
395d3de309f44f6a72f2a46db792f3865088897039Mathias Agopianstatic struct hw_module_methods_t hwc_module_methods = {
405d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian    open: hwc_device_open
415d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian};
425d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian
435d3de309f44f6a72f2a46db792f3865088897039Mathias Agopianhwc_module_t HAL_MODULE_INFO_SYM = {
445d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian    common: {
455d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        tag: HARDWARE_MODULE_TAG,
465d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        version_major: 1,
475d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        version_minor: 0,
485d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        id: HWC_HARDWARE_MODULE_ID,
495d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        name: "Sample hwcomposer module",
505d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        author: "The Android Open Source Project",
515d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        methods: &hwc_module_methods,
525d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian    }
535d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian};
545d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian
555d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian/*****************************************************************************/
565d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian
57d479ad22a0254fa0b5358fe82fa404e3e96c631aJesse Hallstatic void dump_layer(hwc_layer_1_t const* l) {
58cee8501c1623765d5287fcd440fbf8001b9de183Steve Block    ALOGD("\ttype=%d, flags=%08x, handle=%p, tr=%02x, blend=%04x, {%d,%d,%d,%d}, {%d,%d,%d,%d}",
59e6b5c05aa0236dc42107d028a366a3fd2678677bMathias Agopian            l->compositionType, l->flags, l->handle, l->transform, l->blending,
60e6b5c05aa0236dc42107d028a366a3fd2678677bMathias Agopian            l->sourceCrop.left,
61e6b5c05aa0236dc42107d028a366a3fd2678677bMathias Agopian            l->sourceCrop.top,
62e6b5c05aa0236dc42107d028a366a3fd2678677bMathias Agopian            l->sourceCrop.right,
63e6b5c05aa0236dc42107d028a366a3fd2678677bMathias Agopian            l->sourceCrop.bottom,
64e6b5c05aa0236dc42107d028a366a3fd2678677bMathias Agopian            l->displayFrame.left,
65e6b5c05aa0236dc42107d028a366a3fd2678677bMathias Agopian            l->displayFrame.top,
66e6b5c05aa0236dc42107d028a366a3fd2678677bMathias Agopian            l->displayFrame.right,
67e6b5c05aa0236dc42107d028a366a3fd2678677bMathias Agopian            l->displayFrame.bottom);
68e6b5c05aa0236dc42107d028a366a3fd2678677bMathias Agopian}
69e6b5c05aa0236dc42107d028a366a3fd2678677bMathias Agopian
70f9d6cd7dee62789b220033c926c87deab8991bdeJesse Hallstatic int hwc_prepare(hwc_composer_device_1_t *dev,
71f9d6cd7dee62789b220033c926c87deab8991bdeJesse Hall        size_t numDisplays, hwc_display_contents_1_t** displays) {
72f9d6cd7dee62789b220033c926c87deab8991bdeJesse Hall    if (displays && (displays[0]->flags & HWC_GEOMETRY_CHANGED)) {
73f9d6cd7dee62789b220033c926c87deab8991bdeJesse Hall        for (size_t i=0 ; i<displays[0]->numHwLayers ; i++) {
74e6b5c05aa0236dc42107d028a366a3fd2678677bMathias Agopian            //dump_layer(&list->hwLayers[i]);
75f9d6cd7dee62789b220033c926c87deab8991bdeJesse Hall            displays[0]->hwLayers[i].compositionType = HWC_FRAMEBUFFER;
765d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        }
775d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian    }
785d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian    return 0;
795d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian}
805d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian
81d479ad22a0254fa0b5358fe82fa404e3e96c631aJesse Hallstatic int hwc_set(hwc_composer_device_1_t *dev,
82f9d6cd7dee62789b220033c926c87deab8991bdeJesse Hall        size_t numDisplays, hwc_display_contents_1_t** displays)
835d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian{
84e6b5c05aa0236dc42107d028a366a3fd2678677bMathias Agopian    //for (size_t i=0 ; i<list->numHwLayers ; i++) {
85e6b5c05aa0236dc42107d028a366a3fd2678677bMathias Agopian    //    dump_layer(&list->hwLayers[i]);
86e6b5c05aa0236dc42107d028a366a3fd2678677bMathias Agopian    //}
87e6b5c05aa0236dc42107d028a366a3fd2678677bMathias Agopian
88f9d6cd7dee62789b220033c926c87deab8991bdeJesse Hall    EGLBoolean sucess = eglSwapBuffers((EGLDisplay)displays[0]->dpy,
89f9d6cd7dee62789b220033c926c87deab8991bdeJesse Hall            (EGLSurface)displays[0]->sur);
905d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian    if (!sucess) {
915d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        return HWC_EGL_ERROR;
925d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian    }
935d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian    return 0;
945d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian}
955d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian
965d3de309f44f6a72f2a46db792f3865088897039Mathias Agopianstatic int hwc_device_close(struct hw_device_t *dev)
975d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian{
985d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian    struct hwc_context_t* ctx = (struct hwc_context_t*)dev;
995d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian    if (ctx) {
1005d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        free(ctx);
1015d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian    }
1025d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian    return 0;
1035d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian}
1045d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian
1055d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian/*****************************************************************************/
1065d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian
1075d3de309f44f6a72f2a46db792f3865088897039Mathias Agopianstatic int hwc_device_open(const struct hw_module_t* module, const char* name,
1085d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        struct hw_device_t** device)
1095d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian{
1105d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian    int status = -EINVAL;
1115d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian    if (!strcmp(name, HWC_HARDWARE_COMPOSER)) {
1125d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        struct hwc_context_t *dev;
1135d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        dev = (hwc_context_t*)malloc(sizeof(*dev));
1145d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian
1155d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        /* initialize our state here */
1165d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        memset(dev, 0, sizeof(*dev));
1175d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian
1185d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        /* initialize the procs */
1195d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        dev->device.common.tag = HARDWARE_DEVICE_TAG;
120d479ad22a0254fa0b5358fe82fa404e3e96c631aJesse Hall        dev->device.common.version = HWC_DEVICE_API_VERSION_1_0;
1215d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        dev->device.common.module = const_cast<hw_module_t*>(module);
1225d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        dev->device.common.close = hwc_device_close;
1235d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian
1245d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        dev->device.prepare = hwc_prepare;
1255d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        dev->device.set = hwc_set;
1265d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian
1275d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        *device = &dev->device.common;
1285d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian        status = 0;
1295d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian    }
1305d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian    return status;
1315d3de309f44f6a72f2a46db792f3865088897039Mathias Agopian}
132