exynos: reorganized and updated from insignal

Changes needed on exynos4210 devices:

libcsc -> libseccscapi
libswconverter -> remove

TARGET_HAL_PATH := hardware/samsung/exynos4/hal
TARGET_OMX_PATH := hardware/samsung/exynos/multimedia/openmax
$(call inherit-product, hardware/samsung/exynos4210.mk)

Change-Id: Ic59ef95b85ef37b3f38fb36cf6a364a5414685ee
diff --git a/exynos5/hal/libgralloc_ump/Android.mk b/exynos5/hal/libgralloc_ump/Android.mk
new file mode 100644
index 0000000..a4354d4
--- /dev/null
+++ b/exynos5/hal/libgralloc_ump/Android.mk
@@ -0,0 +1,59 @@
+#
+# Copyright (C) 2010 ARM Limited. All rights reserved.
+#
+# Copyright (C) 2008 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+LOCAL_PATH := $(call my-dir)
+
+# HAL module implemenation, not prelinked and stored in
+# hw/<OVERLAY_HARDWARE_MODULE_ID>.<ro.product.board>.so
+include $(CLEAR_VARS)
+LOCAL_PRELINK_MODULE := false
+LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
+LOCAL_SHARED_LIBRARIES := liblog libcutils libGLESv1_CM libGLES_mali libion
+LOCAL_MODULE_TAGS := eng optional
+
+# Include the UMP header files
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/../include
+
+LOCAL_SRC_FILES := \
+	gralloc_module.cpp \
+	alloc_device.cpp \
+	framebuffer_device.cpp
+
+# For now we override arch and ABI values to allow us to work with a
+# libmali that has been forced to armv7, eventually we can let android
+# pick for us
+
+ifeq ($(TARGET_PRODUCT), armboard_v7a)
+# Support for ARM platforms
+LOCAL_CFLAGS:= -DLOG_TAG=\"gralloc\" -DGRALLOC_16_BITS -DSTANDARD_LINUX_SCREEN \
+	-march=armv7-a \
+	-mfloat-abi=softfp
+LOCAL_MODULE := gralloc.default
+
+else
+#Default to goldfish
+LOCAL_CFLAGS:= -DLOG_TAG=\"gralloc\" -DSTANDARD_LINUX_SCREEN \
+	-march=armv7-a \
+	-mfloat-abi=softfp \
+	-DVITHAR_HACK
+
+LOCAL_MODULE := gralloc.smdk5250
+endif
+
+
+include $(BUILD_SHARED_LIBRARY)
diff --git a/exynos5/hal/libgralloc_ump/alloc_device.cpp b/exynos5/hal/libgralloc_ump/alloc_device.cpp
new file mode 100644
index 0000000..64be027
--- /dev/null
+++ b/exynos5/hal/libgralloc_ump/alloc_device.cpp
@@ -0,0 +1,380 @@
+/*
+ * Copyright (C) 2010-2011 ARM Limited. All rights reserved.
+ *
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string.h>
+#include <errno.h>
+#include <pthread.h>
+
+#include <cutils/log.h>
+#include <cutils/atomic.h>
+#include <hardware/hardware.h>
+#include <hardware/gralloc.h>
+
+#include "alloc_device.h"
+#include "gralloc_priv.h"
+#include "gralloc_helper.h"
+#include "framebuffer_device.h"
+
+#include "ump.h"
+
+#include "sec_format.h"
+
+#include <fcntl.h>
+
+#ifdef ION_ALLOC
+#include "ion.h"
+#endif
+
+#include <linux/ashmem.h>
+#include <cutils/ashmem.h>
+
+/* Treat GPU as UMP Device Z */
+#define GRALLOC_DEVICE_SHIFT UMP_DEVICE_Z_SHIFT
+
+#define EXYNOS_ALIGN( value, base ) (((value) + ((base) - 1)) & ~((base) - 1))
+static int gralloc_alloc_buffer(alloc_device_t* dev, size_t size, int usage,
+                                buffer_handle_t* pHandle, int w, int h,
+                                int format, int bpp, int stride_raw, int stride)
+{
+    ion_buffer ion_fd = 0;
+    ump_handle ump_mem_handle;
+    void *cpu_ptr   = NULL;
+    ump_secure_id ump_id=UMP_INVALID_SECURE_ID;
+    ump_alloc_flags ump_flags = UMP_PROT_SHAREABLE | UMP_HINT_CPU_RD | UMP_HINT_CPU_WR;
+    int sw_read_usage;
+    int sw_write_usage;
+    int hw_usage;
+    size = round_up_to_page_size(size);
+
+    unsigned int ion_flags = 0;
+    int priv_alloc_flag = private_handle_t::PRIV_FLAGS_USES_UMP;
+
+    if (usage & GRALLOC_USAGE_HW_ION) {
+        private_module_t* m = reinterpret_cast<private_module_t*>(dev->common.module);
+        ion_fd = ion_alloc(m->ion_client, size, 0, ION_HEAP_EXYNOS_MASK);
+        if (ion_fd < 0) {
+            LOGE("Failed to ion_alloc");
+            return -1;
+        }
+
+        cpu_ptr = ion_map(ion_fd, size, 0);
+        if (NULL == cpu_ptr) {
+            LOGE("Failed to ion_map");
+            ion_free(ion_fd);
+            return -1;
+        }
+        /* TODO: GPU does not use ION. */
+        ump_mem_handle = ump_import(UMP_EXTERNAL_MEM_TYPE_ION, &ion_fd, ump_flags);
+        if (UMP_INVALID_MEMORY_HANDLE != ump_mem_handle) {
+            priv_alloc_flag = private_handle_t::PRIV_FLAGS_USES_ION;
+        } else {
+            LOGE("gralloc_alloc_buffer() failed to import ION memory");
+            ion_unmap(cpu_ptr, size);
+            ion_free(ion_fd);
+            return -1;
+        }
+    } else {
+        usage |= GRALLOC_USAGE_HW_TEXTURE | GRALLOC_USAGE_HW_RENDER;
+        sw_read_usage = usage & GRALLOC_USAGE_SW_READ_MASK;
+        sw_write_usage = usage & GRALLOC_USAGE_SW_WRITE_MASK;
+        hw_usage = usage & GRALLOC_USAGE_HW_MASK;
+
+        if (sw_read_usage) {
+            ump_flags |= UMP_PROT_CPU_RD;
+            if (sw_read_usage == GRALLOC_USAGE_SW_READ_OFTEN)
+                ump_flags |= UMP_HINT_CPU_RD;
+        }
+        if (sw_write_usage) {
+            ump_flags |= UMP_PROT_CPU_WR;
+            if (sw_write_usage == GRALLOC_USAGE_SW_WRITE_OFTEN)
+                ump_flags |= UMP_HINT_CPU_WR;
+        }
+
+        if (hw_usage) {
+            int hw_flags = 0;
+            if (hw_usage & (GRALLOC_USAGE_HW_TEXTURE))
+                hw_flags |= (UMP_PROT_DEVICE_RD | UMP_HINT_DEVICE_RD);
+
+            if (hw_usage & (GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_FB))
+                hw_flags |= (UMP_PROT_DEVICE_RD | UMP_HINT_DEVICE_RD | UMP_PROT_DEVICE_WR | UMP_HINT_DEVICE_WR);
+
+            ump_flags |= (hw_flags << GRALLOC_DEVICE_SHIFT);
+        }
+
+        ump_mem_handle = ump_allocate_64(size, ump_flags);
+        if (UMP_INVALID_MEMORY_HANDLE != ump_mem_handle)
+            cpu_ptr = ump_map(ump_mem_handle, 0, size);
+        else
+            LOGE("gralloc_alloc_buffer() failed to allcoate UMP memory");
+    }
+
+    if (NULL != cpu_ptr) {
+        ump_id = ump_secure_id_get(ump_mem_handle);
+
+        if (UMP_INVALID_SECURE_ID != ump_id || usage & GRALLOC_USAGE_HW_ION) {
+            private_handle_t* hnd = new private_handle_t(priv_alloc_flag, size, (int)cpu_ptr,
+                                                        private_handle_t::LOCK_STATE_MAPPED,
+                                                        ump_id,
+                                                        ump_mem_handle);
+            if (NULL != hnd) {
+                *pHandle = hnd;
+                hnd->fd = ion_fd;
+                hnd->format = format;
+                hnd->usage = usage;
+                hnd->width = w;
+                hnd->height = h;
+                hnd->bpp = bpp;
+                hnd->stride = stride;
+                hnd->uoffset = ((EXYNOS_ALIGN(hnd->width, 16) * EXYNOS_ALIGN(hnd->height, 16)));
+                hnd->voffset = ((EXYNOS_ALIGN((hnd->width >> 1), 8) * EXYNOS_ALIGN((hnd->height >> 1), 8)));
+                return 0;
+            } else {
+                LOGE("gralloc_alloc_buffer() failed to allocate handle");
+            }
+        } else {
+            LOGE("gralloc_alloc_buffer() failed to retrieve valid secure id");
+        }
+
+        ump_unmap(ump_mem_handle, cpu_ptr, size);
+    } else {
+        LOGE("gralloc_alloc_buffer() failed to map UMP memory");
+    }
+
+    ump_release(ump_mem_handle);
+
+    return -1;
+}
+
+static int gralloc_alloc_framebuffer_locked(alloc_device_t* dev, size_t size, int usage,
+                                            buffer_handle_t* pHandle, int w, int h,
+                                            int format, int bpp)
+{
+    private_module_t* m = reinterpret_cast<private_module_t*>(dev->common.module);
+
+    /* allocate the framebuffer */
+    if (m->framebuffer == NULL) {
+        /* initialize the framebuffer, the framebuffer is mapped once and forever. */
+        int err = init_frame_buffer_locked(m);
+
+        if (err < 0)
+            return err;
+    }
+
+    const uint32_t bufferMask = m->bufferMask;
+    const uint32_t numBuffers = m->numBuffers;
+    const size_t bufferSize = m->finfo.line_length * m->info.yres;
+
+    if (numBuffers == 1) {
+        /*
+         * If we have only one buffer, we never use page-flipping. Instead,
+         * we return a regular buffer which will be memcpy'ed to the main
+         * screen when post is called.
+         */
+        int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
+
+        LOGD("fallback to single buffering");
+
+        return gralloc_alloc_buffer(dev, bufferSize, newUsage, pHandle, w, h, format, bpp, 0, 0);
+    }
+
+    if (bufferMask >= ((1LU<<numBuffers)-1)) {
+        LOGE("Ran out of buffers");
+        return -ENOMEM;
+    }
+
+    int vaddr = m->framebuffer->base;
+    /* find a free slot */
+    for (uint32_t i = 0; i < numBuffers; i++) {
+        if ((bufferMask & (1LU<<i)) == 0) {
+            m->bufferMask |= (1LU<<i);
+            break;
+        }
+        vaddr += bufferSize;
+    }
+
+    /* The entire framebuffer memory is already mapped, now create a buffer object for parts of this memory */
+    private_handle_t* hnd = new private_handle_t(private_handle_t::PRIV_FLAGS_FRAMEBUFFER, size, vaddr,
+                                                 0, dup(m->framebuffer->fd), vaddr - m->framebuffer->base);
+    hnd->format = format;
+    hnd->usage = usage;
+    hnd->width = w;
+    hnd->height = h;
+    hnd->bpp = bpp;
+    *pHandle = hnd;
+
+    return 0;
+}
+
+static int gralloc_alloc_framebuffer(alloc_device_t* dev, size_t size, int usage,
+                                     buffer_handle_t* pHandle, int w, int h,
+                                     int format, int bpp)
+{
+    private_module_t* m = reinterpret_cast<private_module_t*>(dev->common.module);
+    pthread_mutex_lock(&m->lock);
+    int err = gralloc_alloc_framebuffer_locked(dev, size, usage, pHandle, w, h, format, bpp);
+    pthread_mutex_unlock(&m->lock);
+
+    return err;
+}
+
+static int alloc_device_alloc(alloc_device_t* dev, int w, int h, int format,
+                              int usage, buffer_handle_t* pHandle, int* pStride)
+{
+    if (!pHandle || !pStride) {
+        LOGE("Invalid Handle, Stride value");
+        return -EINVAL;
+    }
+
+    int err;
+    int align = 16;
+    int bpp = 0;
+    size_t stride_raw = 0;
+    size_t size = 0;
+    size_t stride = (w + 15) & ~15;
+    size_t vstride = (h + 15) & ~15;
+
+    switch (format) {
+    case HAL_PIXEL_FORMAT_RGBA_8888:
+    case HAL_PIXEL_FORMAT_RGBX_8888:
+    case HAL_PIXEL_FORMAT_BGRA_8888:
+        bpp = 4;
+        break;
+    case HAL_PIXEL_FORMAT_RGB_888:
+        bpp = 3;
+        break;
+    case HAL_PIXEL_FORMAT_RGB_565:
+    case HAL_PIXEL_FORMAT_RGBA_5551:
+    case HAL_PIXEL_FORMAT_RGBA_4444:
+        bpp = 2;
+        break;
+    case HAL_PIXEL_FORMAT_YCbCr_420_P:
+    case HAL_PIXEL_FORMAT_YCbCr_420_SP:
+    case HAL_PIXEL_FORMAT_YV12:
+        size = stride * vstride * 2;
+        bpp = 4;
+        break;
+    default:
+        LOGE("Not Support Pixel Format");
+        return -EINVAL;
+    }
+
+    if (format != HAL_PIXEL_FORMAT_YCbCr_420_P &&
+        format != HAL_PIXEL_FORMAT_YCbCr_420_SP &&
+        format != HAL_PIXEL_FORMAT_YV12) {
+        size_t bpr = (w * bpp + (align-1)) & ~(align-1);
+        size = bpr * h;
+        stride = bpr / bpp;
+        stride_raw = bpr;
+    }
+
+    if (usage & GRALLOC_USAGE_HW_FB)
+        err = gralloc_alloc_framebuffer(dev, size, usage, pHandle, w, h, format, 32);
+    else
+        err = gralloc_alloc_buffer(dev, size, usage, pHandle, w, h,
+                    format, 0, (int)stride_raw, (int)stride);
+
+    if (err < 0) {
+        LOGE("Fail to alloc Gralloc memory");
+        return err;
+    }
+
+    *pStride = stride;
+    return 0;
+}
+
+static int alloc_device_free(alloc_device_t* dev, buffer_handle_t handle)
+{
+    if (private_handle_t::validate(handle) < 0) {
+        LOGE("Invalid Value : private_handle");
+        return -EINVAL;
+    }
+
+    private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
+    if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
+        /* free this buffer */
+        private_module_t* m = reinterpret_cast<private_module_t*>(dev->common.module);
+        const size_t bufferSize = m->finfo.line_length * m->info.yres;
+        int index = (hnd->base - m->framebuffer->base) / bufferSize;
+        m->bufferMask &= ~(1<<index);
+        close(hnd->fd);
+    } else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP) {
+        ump_unmap(hnd->ump_mem_handle, (void*)hnd->base, hnd->size);
+        ump_release(hnd->ump_mem_handle);
+    } else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) {
+        ump_mapped_pointer_release((ump_handle)hnd->ump_mem_handle);
+        ump_reference_release((ump_handle)hnd->ump_mem_handle);
+
+        ion_unmap((void*)hnd->base, hnd->size);
+        ion_free(hnd->fd);
+    }
+
+    delete hnd;
+
+    return 0;
+}
+
+static int alloc_device_close(struct hw_device_t *device)
+{
+    alloc_device_t* dev = reinterpret_cast<alloc_device_t*>(device);
+    if (dev) {
+        private_module_t* m = reinterpret_cast<private_module_t*>(dev->common.module);
+        ion_client_destroy(m->ion_client);
+        delete dev;
+        ump_close(); /* Our UMP memory refs will be released automatically here... */
+    }
+    return 0;
+}
+
+int alloc_device_open(hw_module_t const* module, const char* name, hw_device_t** device)
+{
+    alloc_device_t *dev;
+
+    dev = new alloc_device_t;
+    if (NULL == dev) {
+        LOGE("Fail to create alloc_device");
+        return -1;
+    }
+
+    dev->common.module = const_cast<hw_module_t*>(module);
+    private_module_t* m = reinterpret_cast<private_module_t*>(dev->common.module);
+    m->ion_client=ion_client_create();
+    LOGI("gralloc create ion_client %d", m->ion_client);
+
+    ump_result ump_res = ump_open();
+
+    if ((UMP_OK != ump_res) || (0 > m->ion_client)) {
+        LOGE("UMP open failed, ump_res %d, ion_client %d", ump_res, m->ion_client);
+        delete dev;
+        return -1;
+    }
+
+    /* initialize our state here */
+    memset(dev, 0, sizeof(*dev));
+
+    /* initialize the procs */
+    dev->common.tag = HARDWARE_DEVICE_TAG;
+    dev->common.version = 0;
+    dev->common.module = const_cast<hw_module_t*>(module);
+    dev->common.close = alloc_device_close;
+    dev->alloc = alloc_device_alloc;
+    dev->free = alloc_device_free;
+
+    *device = &dev->common;
+
+    return 0;
+}
diff --git a/exynos5/hal/libgralloc_ump/alloc_device.h b/exynos5/hal/libgralloc_ump/alloc_device.h
new file mode 100644
index 0000000..9877d4d
--- /dev/null
+++ b/exynos5/hal/libgralloc_ump/alloc_device.h
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2010-2011 ARM Limited. All rights reserved.
+ *
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <hardware/hardware.h>
+
+// Create an alloc device
+int alloc_device_open(hw_module_t const* module, const char* name, hw_device_t** device);
diff --git a/exynos5/hal/libgralloc_ump/framebuffer_device.cpp b/exynos5/hal/libgralloc_ump/framebuffer_device.cpp
new file mode 100644
index 0000000..0015e54
--- /dev/null
+++ b/exynos5/hal/libgralloc_ump/framebuffer_device.cpp
@@ -0,0 +1,394 @@
+/*
+ * Copyright (C) 2010-2011 ARM Limited. All rights reserved.
+ *
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <linux/fb.h>
+
+#include <cutils/log.h>
+#include <cutils/atomic.h>
+#include <hardware/hardware.h>
+#include <hardware/gralloc.h>
+
+#include <GLES/gl.h>
+
+#include "gralloc_priv.h"
+#include "gralloc_helper.h"
+
+#include "linux/fb.h"
+
+/* numbers of buffers for page flipping */
+#define NUM_BUFFERS 2
+
+enum {
+    PAGE_FLIP = 0x00000001,
+};
+
+static int fb_set_swap_interval(struct framebuffer_device_t* dev, int interval)
+{
+    if (interval < dev->minSwapInterval || interval > dev->maxSwapInterval)
+        return -EINVAL;
+
+    /* TODO: Currently not implemented */
+    return 0;
+}
+
+static int fb_post(struct framebuffer_device_t* dev, buffer_handle_t buffer)
+{
+    if (private_handle_t::validate(buffer) < 0)
+        return -EINVAL;
+
+    private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(buffer);
+    private_module_t* m = reinterpret_cast<private_module_t*>(dev->common.module);
+
+    if (m->currentBuffer) {
+        m->base.unlock(&m->base, m->currentBuffer);
+        m->currentBuffer = 0;
+    }
+
+    if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
+        m->base.lock(&m->base, buffer, private_module_t::PRIV_USAGE_LOCKED_FOR_POST,
+                0, 0, m->info.xres, m->info.yres, NULL);
+
+        const size_t offset = hnd->base - m->framebuffer->base;
+        m->info.activate = FB_ACTIVATE_VBL;
+        m->info.yoffset = offset / m->finfo.line_length;
+
+#ifdef STANDARD_LINUX_SCREEN
+        if (ioctl(m->framebuffer->fd, FBIOPAN_DISPLAY, &m->info) == -1) {
+            LOGE("FBIOPAN_DISPLAY failed");
+            m->base.unlock(&m->base, buffer);
+            return 0;
+        }
+
+        /* wait for VSYNC */
+        int crtc = 0;
+        if (ioctl(m->framebuffer->fd, FBIO_WAITFORVSYNC, &crtc) < 0) {
+            LOGE("FBIO_WAITFORVSYNC failed");
+            return 0;
+        }
+#else
+        /* Standard Android way */
+        if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) {
+            LOGE("FBIOPUT_VSCREENINFO failed");
+            m->base.unlock(&m->base, buffer);
+            return -errno;
+        }
+#endif
+
+        m->currentBuffer = buffer;
+    } else {
+        void* fb_vaddr;
+        void* buffer_vaddr;
+
+        m->base.lock(&m->base, m->framebuffer, GRALLOC_USAGE_SW_WRITE_RARELY,
+                0, 0, m->info.xres, m->info.yres, &fb_vaddr);
+
+        m->base.lock(&m->base, buffer, GRALLOC_USAGE_SW_READ_RARELY,
+                0, 0, m->info.xres, m->info.yres, &buffer_vaddr);
+
+        memcpy(fb_vaddr, buffer_vaddr, m->finfo.line_length * m->info.yres);
+
+        m->base.unlock(&m->base, buffer);
+        m->base.unlock(&m->base, m->framebuffer);
+    }
+
+    return 0;
+}
+
+int init_frame_buffer_locked(struct private_module_t* module)
+{
+    if (module->framebuffer)
+        return 0; /* Nothing to do, already initialized */
+
+    char const * const device_template[] = {
+        "/dev/graphics/fb%u",
+        "/dev/fb%u",
+        NULL
+    };
+
+    int fd = -1;
+    int i = 0;
+    char name[64];
+
+    while ((fd == -1) && device_template[i]) {
+        snprintf(name, 64, device_template[i], 0);
+        fd = open(name, O_RDWR, 0);
+        i++;
+    }
+
+    if (fd < 0) {
+        LOGE("/dev/graphics/fb, /dev/fb Open fail");
+        return -errno;
+    }
+
+    struct fb_fix_screeninfo finfo;
+    if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) {
+        LOGE("Fail to get FB Screen Info");
+        return -errno;
+    }
+
+    struct fb_var_screeninfo info;
+    if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) {
+        LOGE("First, Fail to get FB VScreen Info");
+        return -errno;
+    }
+
+    info.reserved[0] = 0;
+    info.reserved[1] = 0;
+    info.reserved[2] = 0;
+    info.xoffset = 0;
+    info.yoffset = 0;
+    info.activate = FB_ACTIVATE_NOW;
+
+#ifdef GRALLOC_16_BITS
+    /*
+     * Explicitly request 5/6/5
+     */
+    info.bits_per_pixel = 16;
+    info.red.offset     = 11;
+    info.red.length     = 5;
+    info.green.offset   = 5;
+    info.green.length   = 6;
+    info.blue.offset    = 0;
+    info.blue.length    = 5;
+    info.transp.offset  = 0;
+    info.transp.length  = 0;
+#else
+    /*
+     * Explicitly request 8/8/8
+     */
+    info.bits_per_pixel = 32;
+    info.red.offset     = 16;
+    info.red.length     = 8;
+    info.green.offset   = 8;
+    info.green.length   = 8;
+    info.blue.offset    = 0;
+    info.blue.length    = 8;
+    info.transp.offset  = 0;
+    info.transp.length  = 0;
+#endif
+
+    /*
+     * Request NUM_BUFFERS screens (at lest 2 for page flipping)
+     */
+    info.yres_virtual = info.yres * NUM_BUFFERS;
+
+    uint32_t flags = PAGE_FLIP;
+    if (ioctl(fd, FBIOPUT_VSCREENINFO, &info) == -1) {
+        info.yres_virtual = info.yres;
+        flags &= ~PAGE_FLIP;
+        LOGW("FBIOPUT_VSCREENINFO failed, page flipping not supported");
+    }
+
+    if (info.yres_virtual < info.yres * 2) {
+        /* we need at least 2 for page-flipping */
+        info.yres_virtual = info.yres;
+        flags &= ~PAGE_FLIP;
+        LOGW("page flipping not supported (yres_virtual=%d, requested=%d)",
+                info.yres_virtual, info.yres*2);
+    }
+
+    if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) {
+        LOGE("Second, Fail to get FB VScreen Info");
+        return -errno;
+    }
+
+    int refreshRate = 1000000000000000LLU /
+    (
+        uint64_t( info.upper_margin + info.lower_margin + info.yres )
+        * ( info.left_margin  + info.right_margin + info.xres )
+        * info.pixclock
+    );
+
+    if (refreshRate == 0)
+        refreshRate = 60*1000;  /* 60 Hz */
+
+    if (int(info.width) <= 0 || int(info.height) <= 0) {
+        /* the driver doesn't return that information default to 160 dpi */
+        info.width  = ((info.xres * 25.4f) / 160.0f + 0.5f);
+        info.height = ((info.yres * 25.4f) / 160.0f + 0.5f);
+    }
+
+    float xdpi = (info.xres * 25.4f) / info.width;
+    float ydpi = (info.yres * 25.4f) / info.height;
+    float fps  = refreshRate / 1000.0f;
+
+    LOGI("using (fd=%d)\n"
+         "id           = %s\n"
+         "xres         = %d px\n"
+         "yres         = %d px\n"
+         "xres_virtual = %d px\n"
+         "yres_virtual = %d px\n"
+         "bpp          = %d\n"
+         "r            = %2u:%u\n"
+         "g            = %2u:%u\n"
+         "b            = %2u:%u\n",
+         fd,
+         finfo.id,
+         info.xres,
+         info.yres,
+         info.xres_virtual,
+         info.yres_virtual,
+         info.bits_per_pixel,
+         info.red.offset, info.red.length,
+         info.green.offset, info.green.length,
+         info.blue.offset, info.blue.length);
+
+    LOGI("width        = %d mm (%f dpi)\n"
+         "height       = %d mm (%f dpi)\n"
+         "refresh rate = %.2f Hz\n",
+         info.width,  xdpi,
+         info.height, ydpi,
+         fps);
+
+    if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) {
+        LOGE("Fail to get FScreen Info");
+        return -errno;
+    }
+
+    if (finfo.smem_len <= 0) {
+        LOGE("Invalid Value : finfo.smem_len");
+        return -errno;
+    }
+
+    module->flags = flags;
+    module->info = info;
+    module->finfo = finfo;
+    module->xdpi = xdpi;
+    module->ydpi = ydpi;
+    module->fps = fps;
+
+    /*
+     * map the framebuffer
+     */
+    size_t fbSize = round_up_to_page_size(finfo.line_length * info.yres_virtual);
+    void* vaddr = mmap(0, fbSize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+    if (vaddr == MAP_FAILED) {
+        LOGE("Error mapping the framebuffer (%s)", strerror(errno));
+        return -errno;
+    }
+
+    memset(vaddr, 0, fbSize);
+
+    /* Create a "fake" buffer object for the entire frame buffer memory,
+     * and store it in the module */
+    module->framebuffer = new private_handle_t(private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
+                                               fbSize, intptr_t(vaddr),
+                                               0, dup(fd), 0);
+
+    module->numBuffers = info.yres_virtual / info.yres;
+    module->bufferMask = 0;
+
+    return 0;
+}
+
+static int init_frame_buffer(struct private_module_t* module)
+{
+    pthread_mutex_lock(&module->lock);
+    int err = init_frame_buffer_locked(module);
+    pthread_mutex_unlock(&module->lock);
+    return err;
+}
+
+static int fb_close(struct hw_device_t *device)
+{
+    framebuffer_device_t* dev = reinterpret_cast<framebuffer_device_t*>(device);
+    if (dev) {
+        ump_close();
+        delete dev;
+    }
+    return 0;
+}
+
+int compositionComplete(struct framebuffer_device_t* dev)
+{
+    /* By doing a Finish here we force the GL driver to start rendering
+       all the drawcalls up to this point, and to wait for the rendering to be complete.*/
+
+    /* It move to HWComposit Module */
+
+    /* The rendering of the backbuffer is now completed.
+       When SurfaceFlinger later does a call to eglSwapBuffer(), the swap will be done
+       synchronously in the same thread, and not asynchronoulsy in a background thread later.
+       The SurfaceFlinger requires this behaviour since it releases the lock on all the
+       SourceBuffers (Layers) after the compositionComplete() function returns.
+       However this "bad" behaviour by SurfaceFlinger should not affect performance,
+       since the Applications that render the SourceBuffers (Layers) still get the
+       full renderpipeline using asynchronouls rendering. So they perform at maximum speed,
+       and because of their complexity compared to the Surface flinger jobs, the Surface flinger
+       is normally faster even if it does everyhing synchronous and serial.
+       */
+    return 0;
+}
+
+int framebuffer_device_open(hw_module_t const* module, const char* name, hw_device_t** device)
+{
+    int status = -EINVAL;
+
+    alloc_device_t* gralloc_device;
+    status = gralloc_open(module, &gralloc_device);
+    if (status < 0) {
+        LOGE("Fail to Open gralloc device");
+        return status;
+    }
+
+    private_module_t* m = (private_module_t*)module;
+    status = init_frame_buffer(m);
+    if (status < 0) {
+        LOGE("Fail to init framebuffer");
+        gralloc_close(gralloc_device);
+        return status;
+    }
+
+    /* initialize our state here */
+    framebuffer_device_t *dev = new framebuffer_device_t;
+    memset(dev, 0, sizeof(*dev));
+
+    /* initialize the procs */
+    dev->common.tag = HARDWARE_DEVICE_TAG;
+    dev->common.version = 0;
+    dev->common.module = const_cast<hw_module_t*>(module);
+    dev->common.close = fb_close;
+    dev->setSwapInterval = fb_set_swap_interval;
+    dev->post = fb_post;
+    dev->setUpdateRect = 0;
+    dev->compositionComplete = &compositionComplete;
+
+    int stride = m->finfo.line_length / (m->info.bits_per_pixel >> 3);
+    const_cast<uint32_t&>(dev->flags) = 0;
+    const_cast<uint32_t&>(dev->width) = m->info.xres;
+    const_cast<uint32_t&>(dev->height) = m->info.yres;
+    const_cast<int&>(dev->stride) = stride;
+#ifdef GRALLOC_16_BITS
+    const_cast<int&>(dev->format) = HAL_PIXEL_FORMAT_RGB_565;
+#else
+    const_cast<int&>(dev->format) = HAL_PIXEL_FORMAT_BGRA_8888;
+#endif
+    const_cast<float&>(dev->xdpi) = m->xdpi;
+    const_cast<float&>(dev->ydpi) = m->ydpi;
+    const_cast<float&>(dev->fps) = m->fps;
+    const_cast<int&>(dev->minSwapInterval) = 1;
+    const_cast<int&>(dev->maxSwapInterval) = 1;
+    *device = &dev->common;
+    status = 0;
+
+    return status;
+}
diff --git a/exynos5/hal/libgralloc_ump/framebuffer_device.h b/exynos5/hal/libgralloc_ump/framebuffer_device.h
new file mode 100644
index 0000000..4d84bef
--- /dev/null
+++ b/exynos5/hal/libgralloc_ump/framebuffer_device.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2010-2011 ARM Limited. All rights reserved.
+ *
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <hardware/hardware.h>
+
+// Create a framebuffer device
+int framebuffer_device_open(hw_module_t const* module, const char* name, hw_device_t** device);
+
+// Initialize the framebuffer (must keep module lock before calling
+int init_frame_buffer_locked(struct private_module_t* module);
diff --git a/exynos5/hal/libgralloc_ump/gralloc_helper.h b/exynos5/hal/libgralloc_ump/gralloc_helper.h
new file mode 100644
index 0000000..6a0a518
--- /dev/null
+++ b/exynos5/hal/libgralloc_ump/gralloc_helper.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2010-2011 ARM Limited. All rights reserved.
+ *
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef GRALLOC_HELPER_H_
+#define GRALLOC_HELPER_H_
+
+#include <sys/mman.h>
+
+inline size_t round_up_to_page_size(size_t x)
+{
+    return (x + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
+}
+
+#endif /* GRALLOC_HELPER_H_ */
diff --git a/exynos5/hal/libgralloc_ump/gralloc_module.cpp b/exynos5/hal/libgralloc_ump/gralloc_module.cpp
new file mode 100644
index 0000000..8ca500b
--- /dev/null
+++ b/exynos5/hal/libgralloc_ump/gralloc_module.cpp
@@ -0,0 +1,289 @@
+/*
+ * Copyright (C) 2010-2011 ARM Limited. All rights reserved.
+ *
+ * Copyright (C) 2008 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <errno.h>
+#include <pthread.h>
+
+#include <cutils/log.h>
+#include <cutils/atomic.h>
+#include <hardware/hardware.h>
+#include <hardware/gralloc.h>
+#include "ump.h"
+
+#include "gralloc_priv.h"
+#include "alloc_device.h"
+#include "framebuffer_device.h"
+#include "ion.h"
+#include <sys/mman.h>
+
+static pthread_mutex_t s_map_lock = PTHREAD_MUTEX_INITIALIZER;
+static int s_ump_is_open = 0;
+
+static int gralloc_map(gralloc_module_t const* module,
+        buffer_handle_t handle, void** vaddr)
+{
+    private_handle_t* hnd = (private_handle_t*)handle;
+    private_module_t* m = (private_module_t*)module;
+
+    if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
+        if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) {
+            size_t size = hnd->size;
+            hnd->ion_client = ion_client_create();
+            void *mappedAddress = ion_map(hnd->fd, size, 0);
+
+            if (mappedAddress == MAP_FAILED) {
+                LOGE("Could not ion_map %s fd(%d)", strerror(errno), hnd->fd);
+                return -errno;
+            }
+
+            hnd->base = intptr_t(mappedAddress) + hnd->offset;
+            LOGD("gralloc_map() succeeded fd=%d, off=%d, size=%d, vaddr=%p",
+                    hnd->fd, hnd->offset, hnd->size, mappedAddress);
+        } else {
+            LOGE("In case of ION, It could not reach here!");
+        }
+    }
+    *vaddr = (void*)hnd->base;
+    return 0;
+}
+
+static int gralloc_unmap(gralloc_module_t const* module,
+            buffer_handle_t handle)
+{
+    private_handle_t* hnd = (private_handle_t*)handle;
+    private_module_t* m = (private_module_t*)module;
+
+    if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) {
+        if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) {
+            void* base = (void*)hnd->base;
+            size_t size = hnd->size;
+            LOGD("unmapping from %p, size=%d", base, size);
+
+            if (ion_unmap(base, size) < 0)
+                LOGE("Could not ion_unmap %s", strerror(errno));
+
+            ion_client_destroy(hnd->ion_client);
+        }
+    }
+    hnd->base = 0;
+    return 0;
+}
+
+static int gralloc_device_open(const hw_module_t* module, const char* name, hw_device_t** device)
+{
+    int status = -EINVAL;
+
+    LOGI("Opening ARM Gralloc device");
+
+    if (!strcmp(name, GRALLOC_HARDWARE_GPU0))
+        status = alloc_device_open(module, name, device);
+    else if (!strcmp(name, GRALLOC_HARDWARE_FB0))
+        status = framebuffer_device_open(module, name, device);
+
+    return status;
+}
+
+static int gralloc_register_buffer(gralloc_module_t const* module, buffer_handle_t handle)
+{
+    int retval = -EINVAL;
+    void *vaddr;
+    if (private_handle_t::validate(handle) < 0) {
+        LOGE("Registering invalid buffer, returning error");
+        return -EINVAL;
+    }
+
+    // if this handle was created in this process, then we keep it as is.
+    private_handle_t* hnd = (private_handle_t*)handle;
+    if (hnd->pid == getpid()) {
+        LOGE("Invalid Process ID from Private_Handle");
+        return 0;
+    }
+
+    pthread_mutex_lock(&s_map_lock);
+    if (!s_ump_is_open) {
+        ump_result res = ump_open();
+        if (res != UMP_OK) {
+            pthread_mutex_unlock(&s_map_lock);
+            LOGE("Failed to open UMP library");
+            return retval;
+        }
+        s_ump_is_open = 1;
+    }
+
+    if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP) {
+        hnd->ump_mem_handle = ump_from_secure_id(hnd->ump_id);
+        if (UMP_INVALID_MEMORY_HANDLE != hnd->ump_mem_handle) {
+            hnd->base = (int)ump_map(hnd->ump_mem_handle, 0, hnd->size);
+            if (0 != hnd->base) {
+                hnd->lockState = private_handle_t::LOCK_STATE_MAPPED;
+                hnd->writeOwner = 0;
+                hnd->lockState = 0;
+
+                pthread_mutex_unlock(&s_map_lock);
+                return 0;
+            } else {
+                LOGE("Failed to map UMP handle");
+            }
+
+            ump_release(hnd->ump_mem_handle);
+        } else {
+            LOGE("Failed to create UMP handle");
+        }
+    } else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) {
+        hnd->ump_mem_handle = ump_handle_create_from_secure_id(hnd->ump_id);
+        retval = gralloc_map(module, handle, &vaddr);
+    } else {
+        LOGE("registering non-UMP&ION buffer not supported");
+    }
+
+    pthread_mutex_unlock(&s_map_lock);
+
+    return retval;
+}
+
+static int gralloc_unregister_buffer(gralloc_module_t const* module, buffer_handle_t handle)
+{
+    if (private_handle_t::validate(handle) < 0) {
+        LOGE("unregistering invalid buffer, returning error");
+        return -EINVAL;
+    }
+
+    private_handle_t* hnd = (private_handle_t*)handle;
+
+    LOGE_IF(hnd->lockState & private_handle_t::LOCK_STATE_READ_MASK,
+                "[unregister] handle %p still locked (state=%08x)", hnd, hnd->lockState);
+
+    /* never unmap buffers that were created in this process */
+    if (hnd->pid != getpid()) {
+        pthread_mutex_lock(&s_map_lock);
+
+        if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP) {
+            ump_unmap(hnd->ump_mem_handle, (void*)hnd->base, hnd->size);
+            hnd->base = 0;
+            ump_release(hnd->ump_mem_handle);
+            hnd->ump_mem_handle = UMP_INVALID_MEMORY_HANDLE;
+        } else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) {
+
+            ump_mapped_pointer_release((ump_handle)hnd->ump_mem_handle);
+            ump_reference_release((ump_handle)hnd->ump_mem_handle);
+            if (hnd->base)
+                gralloc_unmap(module, handle);
+
+            hnd->base = 0;
+            hnd->ump_mem_handle = (int)UMP_INVALID_MEMORY_HANDLE;
+            hnd->lockState  = 0;
+            hnd->writeOwner = 0;
+        } else {
+            LOGE("unregistering non-UMP buffer not supported");
+        }
+
+        hnd->base = 0;
+        hnd->lockState  = 0;
+        hnd->writeOwner = 0;
+
+        pthread_mutex_unlock(&s_map_lock);
+    }
+
+    return 0;
+}
+
+static int gralloc_lock(gralloc_module_t const* module, buffer_handle_t handle,
+                        int usage, int l, int t, int w, int h, void** vaddr)
+{
+    if (private_handle_t::validate(handle) < 0) {
+        LOGE("Locking invalid buffer, returning error");
+        return -EINVAL;
+    }
+
+    private_handle_t* hnd = (private_handle_t*)handle;
+    if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP) {
+        hnd->writeOwner = usage & GRALLOC_USAGE_SW_WRITE_MASK;
+
+        if (usage & GRALLOC_USAGE_SW_READ_MASK)
+            ump_cpu_msync_now(hnd->ump_mem_handle, UMP_MSYNC_CLEAN_AND_INVALIDATE,
+                    (void*)hnd->base, hnd->size);
+    }
+
+    if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK))
+        *vaddr = (void*)hnd->base;
+
+    if (usage & GRALLOC_USAGE_YUV_ADDR) {
+        vaddr[0] = (void*)hnd->base;
+        vaddr[1] = (void*)(hnd->base + hnd->uoffset);
+        vaddr[2] = (void*)(hnd->base + hnd->uoffset + hnd->voffset);
+    }
+    return 0;
+}
+
+static int gralloc_unlock(gralloc_module_t const* module, buffer_handle_t handle)
+{
+    if (private_handle_t::validate(handle) < 0) {
+        LOGE("Unlocking invalid buffer, returning error");
+        return -EINVAL;
+    }
+
+    private_handle_t* hnd = (private_handle_t*)handle;
+    private_module_t* m = (private_module_t*)module;
+    int32_t current_value;
+    int32_t new_value;
+    int retry;
+
+    if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP && hnd->writeOwner)
+        ump_cpu_msync_now(hnd->ump_mem_handle, UMP_MSYNC_CLEAN_AND_INVALIDATE, (void*)hnd->base, hnd->size);
+    else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION)
+        ion_msync(hnd->ion_client, hnd->fd, IMSYNC_DEV_TO_READ | IMSYNC_SYNC_FOR_DEV, hnd->size, hnd->offset);
+
+    return 0;
+}
+
+static struct hw_module_methods_t gralloc_module_methods =
+{
+    open: gralloc_device_open
+};
+
+struct private_module_t HAL_MODULE_INFO_SYM =
+{
+    base:
+    {
+        common:
+        {
+            tag: HARDWARE_MODULE_TAG,
+            version_major: 1,
+            version_minor: 0,
+            id: GRALLOC_HARDWARE_MODULE_ID,
+            name: "Graphics Memory Allocator Module",
+            author: "ARM Ltd.",
+            methods: &gralloc_module_methods,
+            dso: NULL,
+            reserved : {0,},
+        },
+        registerBuffer: gralloc_register_buffer,
+        unregisterBuffer: gralloc_unregister_buffer,
+        lock: gralloc_lock,
+        unlock: gralloc_unlock,
+        perform: NULL,
+        reserved_proc: {0,},
+    },
+    framebuffer: NULL,
+    flags: 0,
+    numBuffers: 0,
+    bufferMask: 0,
+    lock: PTHREAD_MUTEX_INITIALIZER,
+    currentBuffer: NULL,
+    ion_client: -1,
+};