diff options
Diffstat (limited to 'exynos4/hal/libgralloc_ump')
-rw-r--r-- | exynos4/hal/libgralloc_ump/Android.mk | 56 | ||||
-rw-r--r-- | exynos4/hal/libgralloc_ump/alloc_device.cpp | 498 | ||||
-rw-r--r-- | exynos4/hal/libgralloc_ump/alloc_device.h | 27 | ||||
-rw-r--r-- | exynos4/hal/libgralloc_ump/framebuffer_device.cpp | 465 | ||||
-rw-r--r-- | exynos4/hal/libgralloc_ump/framebuffer_device.h | 31 | ||||
-rw-r--r-- | exynos4/hal/libgralloc_ump/gr.h | 64 | ||||
-rw-r--r-- | exynos4/hal/libgralloc_ump/gralloc_helper.h | 34 | ||||
-rw-r--r-- | exynos4/hal/libgralloc_ump/gralloc_module.cpp | 480 |
8 files changed, 1655 insertions, 0 deletions
diff --git a/exynos4/hal/libgralloc_ump/Android.mk b/exynos4/hal/libgralloc_ump/Android.mk new file mode 100644 index 0000000..20584a4 --- /dev/null +++ b/exynos4/hal/libgralloc_ump/Android.mk @@ -0,0 +1,56 @@ +# +# Copyright (C) 2010 ARM Limited. All rights reserved. +# +# Portions of this code have been modified from the original. +# These modifications are: +# * The build configuration for the Gralloc module +# +# 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 libUMP libGLESv1_CM libion + +# Include the UMP header files +LOCAL_C_INCLUDES += $(LOCAL_PATH)/../include + +LOCAL_SRC_FILES := \ + gralloc_module.cpp \ + alloc_device.cpp \ + framebuffer_device.cpp + +LOCAL_MODULE_TAGS := eng +#LOCAL_MODULE := gralloc.default +LOCAL_MODULE := gralloc.$(TARGET_DEVICE) +LOCAL_CFLAGS:= -DLOG_TAG=\"gralloc\" -DGRALLOC_32_BITS -DSTANDARD_LINUX_SCREEN +#LOCAL_CFLAGS+= -DMALI_VSYNC_EVENT_REPORT_ENABLE + +LOCAL_CFLAGS += -DSAMSUNG_EXYNOS +LOCAL_CFLAGS += -DSAMSUNG_EXYNOS_CACHE_UMP + +ifeq ($(TARGET_SOC),exynos4210) +LOCAL_CFLAGS += -DSAMSUNG_EXYNOS4210 +endif + +ifeq ($(TARGET_SOC),exynos4x12) +LOCAL_CFLAGS += -DSAMSUNG_EXYNOS4x12 +endif + +include $(BUILD_SHARED_LIBRARY) diff --git a/exynos4/hal/libgralloc_ump/alloc_device.cpp b/exynos4/hal/libgralloc_ump/alloc_device.cpp new file mode 100644 index 0000000..982f1b8 --- /dev/null +++ b/exynos4/hal/libgralloc_ump/alloc_device.cpp @@ -0,0 +1,498 @@ +/* + * Copyright (C) 2010 ARM Limited. All rights reserved. + * + * Portions of this code have been modified from the original. + * These modifications are: + * * includes + * * gralloc_alloc_buffer() + * * gralloc_alloc_framebuffer_locked() + * * gralloc_alloc_framebuffer() + * * alloc_device_alloc() + * * alloc_device_free() + * * alloc_device_close() + * * alloc_device_open() + * + * 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 "sec_format.h" + +#include "gralloc_priv.h" +#include "gralloc_helper.h" +#include "framebuffer_device.h" + +#include "ump.h" +#include "ump_ref_drv.h" + +/*****************************************************************************/ +#include <limits.h> +#include <unistd.h> +#include <fcntl.h> +#include <errno.h> +#include <pthread.h> +#include <stdlib.h> + +#include <sys/mman.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/ioctl.h> + +#if HAVE_ANDROID_OS +#include <linux/android_pmem.h> +#include <pixelflinger/format.h> +#endif + +#include "videodev2.h" +#include "s5p_fimc.h" + +#ifdef SAMSUNG_EXYNOS4x12 +#define PFX_NODE_FIMC1 "/dev/video3" +#endif +#ifdef SAMSUNG_EXYNOS4210 +#define PFX_NODE_FIMC1 "/dev/video1" +#endif + +#ifndef OMX_COLOR_FormatYUV420Planar +#define OMX_COLOR_FormatYUV420Planar 0x13 +#endif + +#ifndef OMX_COLOR_FormatYUV420SemiPlanar +#define OMX_COLOR_FormatYUV420SemiPlanar 0x15 +#endif + +bool ion_dev_open = true; +static pthread_mutex_t l_surface= PTHREAD_MUTEX_INITIALIZER; +static int buffer_offset = 0; +static int gfd = 0; + +#ifdef USE_PARTIAL_FLUSH +extern struct private_handle_rect *rect_list; +extern private_handle_rect *find_rect(int secure_id); +extern private_handle_rect *find_last_rect(int secure_id); +extern int release_rect(int secure_id); +#endif + +#define EXYNOS4_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) +{ + ump_handle ump_mem_handle; + void *cpu_ptr; + ump_secure_id ump_id; + + size = round_up_to_page_size(size); + if (usage & GRALLOC_USAGE_HW_FIMC1) { + int dev_fd=0; + char node[20]; + int ret; + int paddr=0; + int offset=0; + + struct v4l2_control vc; + sprintf(node, "%s", PFX_NODE_FIMC1); + + if (gfd == 0) { + gfd = open(node, O_RDWR); + + if (gfd < 0) { + LOGE("%s:: %s Post processor open error\n", __func__, node); + return false; + } + } + + vc.id = V4L2_CID_RESERVED_MEM_BASE_ADDR; + vc.value = 0; + ret = ioctl(gfd, VIDIOC_G_CTRL, &vc); + if (ret < 0) { + LOGE("Error in video VIDIOC_G_CTRL - V4L2_CID_RESERVED_MEM_BAES_ADDR (%d)\n", ret); + return false; + } + paddr = (unsigned int)vc.value; + + if ((buffer_offset + size) >= FIMC1_RESERVED_SIZE * 1024) + buffer_offset = 0; + + paddr += buffer_offset; + private_handle_t* hnd = new private_handle_t(private_handle_t::PRIV_FLAGS_USES_IOCTL, size, 0, + private_handle_t::LOCK_STATE_MAPPED, 0, 0); + + *pHandle = hnd; + hnd->format = format; + hnd->usage = usage; + hnd->width = w; + hnd->height = h; + hnd->bpp = bpp; + hnd->paddr = paddr; + hnd->offset = buffer_offset; + hnd->stride = stride; + hnd->fd = gfd; + hnd->uoffset = (EXYNOS4_ALIGN((EXYNOS4_ALIGN(hnd->width, 16) * EXYNOS4_ALIGN(hnd->height, 16)), 4096)); + hnd->voffset = (EXYNOS4_ALIGN((EXYNOS4_ALIGN((hnd->width >> 1), 16) * EXYNOS4_ALIGN((hnd->height >> 1), 16)), 4096)); + buffer_offset += size; + + return 0; + } else { + ion_buffer ion_fd = 0; + unsigned int ion_flags = 0; + int priv_alloc_flag = private_handle_t::PRIV_FLAGS_USES_UMP; + + if (usage & GRALLOC_USAGE_HW_ION) { + if (!ion_dev_open) { + LOGE("ERROR, failed to open ion"); + return -1; + } + + private_module_t* m = reinterpret_cast<private_module_t*>(dev->common.module); + ion_flags = ION_HEAP_EXYNOS_MASK; + ion_fd = ion_alloc(m->ion_client, size, 0, ion_flags); + + 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; + } + + ump_mem_handle = ump_ref_drv_ion_import(ion_fd, UMP_REF_DRV_CONSTRAINT_NONE); + + 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; + } + } +#ifdef SAMSUNG_EXYNOS_CACHE_UMP + else if ((usage&GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN) + ump_mem_handle = ump_ref_drv_allocate(size, UMP_REF_DRV_CONSTRAINT_USE_CACHE); + else + ump_mem_handle = ump_ref_drv_allocate(size, UMP_REF_DRV_CONSTRAINT_NONE); +#else + else + ump_mem_handle = ump_ref_drv_allocate(size, UMP_REF_DRV_CONSTRAINT_NONE); +#endif + if (UMP_INVALID_MEMORY_HANDLE != ump_mem_handle) { + if (!(usage & GRALLOC_USAGE_HW_ION)) + cpu_ptr = ump_mapped_pointer_get(ump_mem_handle); + if (NULL != cpu_ptr) { + ump_id = ump_secure_id_get(ump_mem_handle); + if (UMP_INVALID_SECURE_ID != ump_id) { + private_handle_t* hnd; + hnd = new private_handle_t(priv_alloc_flag, size, (int)cpu_ptr, + private_handle_t::LOCK_STATE_MAPPED, ump_id, ump_mem_handle, ion_fd, 0, 0); + if (NULL != hnd) { + *pHandle = hnd; +#ifdef USE_PARTIAL_FLUSH + if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP) { + private_handle_rect *psRect; + private_handle_rect *psFRect; + psRect = (private_handle_rect *)calloc(1, sizeof(private_handle_rect)); + psRect->handle = (int)hnd->ump_id; + psRect->stride = (int)hnd->stride_raw; + psFRect = find_last_rect((int)hnd->ump_id); + psFRect->next = psRect; + } +#endif + hnd->format = format; + hnd->usage = usage; + hnd->width = w; + hnd->height = h; + hnd->bpp = bpp; + hnd->stride = stride; + hnd->uoffset = ((EXYNOS4_ALIGN(hnd->width, 16) * EXYNOS4_ALIGN(hnd->height, 16))); + hnd->voffset = ((EXYNOS4_ALIGN((hnd->width >> 1), 16) * EXYNOS4_ALIGN((hnd->height >> 1), 16))); + return 0; + } else { + LOGE("gralloc_alloc_buffer() failed to allocate handle"); + } + } else { + LOGE("gralloc_alloc_buffer() failed to retrieve valid secure id"); + } + + ump_mapped_pointer_release(ump_mem_handle); + } else { + LOGE("gralloc_alloc_buffer() failed to map UMP memory"); + } + + ump_reference_release(ump_mem_handle); + } else { + LOGE("gralloc_alloc_buffer() failed to allcoate UMP memory"); + } + } + 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; + LOGE("fallback to single buffering"); + return gralloc_alloc_buffer(dev, bufferSize, newUsage, pHandle, w, h, format, bpp, 0, 0); + } + + if (bufferMask >= ((1LU<<numBuffers)-1)) + 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) + return -EINVAL; + + size_t size = 0; + size_t stride = 0; + size_t stride_raw = 0; + + if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP || + format == HAL_PIXEL_FORMAT_YCrCb_420_SP || + format == HAL_PIXEL_FORMAT_YCbCr_422_SP || + format == HAL_PIXEL_FORMAT_YCbCr_420_P || + format == HAL_PIXEL_FORMAT_YV12 || + format == HAL_PIXEL_FORMAT_CUSTOM_YCrCb_420_SP || + format == HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP_TILED || + format == GGL_PIXEL_FORMAT_L_8 || + format == OMX_COLOR_FormatYUV420Planar || + format == OMX_COLOR_FormatYUV420SemiPlanar) { + /* FIXME: there is no way to return the vstride */ + int vstride; + stride = (w + 15) & ~15; + vstride = (h + 15) & ~15; + switch (format) { + case HAL_PIXEL_FORMAT_YCbCr_420_SP: + case HAL_PIXEL_FORMAT_YCrCb_420_SP: + case HAL_PIXEL_FORMAT_YCbCr_420_P: + case HAL_PIXEL_FORMAT_YV12: + case HAL_PIXEL_FORMAT_CUSTOM_YCrCb_420_SP: + case HAL_PIXEL_FORMAT_CUSTOM_YCbCr_420_SP_TILED: + case OMX_COLOR_FormatYUV420Planar: + case OMX_COLOR_FormatYUV420SemiPlanar: + size = stride * vstride * 2; + if(usage & GRALLOC_USAGE_HW_FIMC1) + size += PAGE_SIZE * 2; + break; + case HAL_PIXEL_FORMAT_YCbCr_422_SP: + size = (stride * vstride) + (w/2 * h/2) * 2; + break; + case GGL_PIXEL_FORMAT_L_8: + size = (stride * vstride); + break; + default: + return -EINVAL; + } + } else { + int align = 8; + int bpp = 0; + 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; + default: + return -EINVAL; + } + size_t bpr = (w*bpp + (align-1)) & ~(align-1); + size = bpr * h; + stride = bpr / bpp; + stride_raw = bpr; + } + + int err; + pthread_mutex_lock(&l_surface); + 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); + + pthread_mutex_unlock(&l_surface); + + if (err < 0) + 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) + return -EINVAL; + + private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle); + private_module_t* m = reinterpret_cast<private_module_t*>(dev->common.module); + pthread_mutex_lock(&l_surface); + if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) { + /* free this buffer */ + 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) { +#ifdef USE_PARTIAL_FLUSH + if (!release_rect((int)hnd->ump_id)) + LOGE("secure id: 0x%x, release error",(int)hnd->ump_id); +#endif + ump_mapped_pointer_release((ump_handle)hnd->ump_mem_handle); + ump_reference_release((ump_handle)hnd->ump_mem_handle); + } else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) { +#ifdef USE_PARTIAL_FLUSH + if (!release_rect((int)hnd->ump_id)) + LOGE("secure id: 0x%x, release error",(int)hnd->ump_id); +#endif + 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); + } + pthread_mutex_unlock(&l_surface); + 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); + if (ion_dev_open) + ion_client_destroy(m->ion_client); + delete dev; + ump_close(); + } + 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) + 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(); + ump_result ump_res = ump_open(); + if (0 > m->ion_client) + ion_dev_open = false; + if (UMP_OK != ump_res) { + LOGE("UMP open failed ump_res %d", ump_res); + 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/exynos4/hal/libgralloc_ump/alloc_device.h b/exynos4/hal/libgralloc_ump/alloc_device.h new file mode 100644 index 0000000..4609787 --- /dev/null +++ b/exynos4/hal/libgralloc_ump/alloc_device.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2010 ARM Limited. All rights reserved. + * + * Portions of this code have been modified from the original. + * These modifications are: + * * includes + * * alloc_device_open() + * + * 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/exynos4/hal/libgralloc_ump/framebuffer_device.cpp b/exynos4/hal/libgralloc_ump/framebuffer_device.cpp new file mode 100644 index 0000000..9413112 --- /dev/null +++ b/exynos4/hal/libgralloc_ump/framebuffer_device.cpp @@ -0,0 +1,465 @@ +/* + * Copyright (C) 2010 ARM Limited. All rights reserved. + * + * Portions of this code have been modified from the original. + * These modifications are: + * * includes + * * enums + * * fb_set_swap_interval() + * * fb_post() + * * init_frame_buffer_locked() + * * init_frame_buffer() + * * fb_close() + * * framebuffer_device_open() + * + * 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 <stdlib.h> + +#include <cutils/log.h> +#include <cutils/atomic.h> +#include <cutils/properties.h> +#include <hardware/hardware.h> +#include <hardware/gralloc.h> + +#include <GLES/gl.h> + +#ifdef MALI_VSYNC_EVENT_REPORT_ENABLE +#include "gralloc_vsync_report.h" +#endif + +#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; + + /* 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; + int interrupt; + m->info.activate = FB_ACTIVATE_VBL; + m->info.yoffset = offset / m->finfo.line_length; + +#ifdef STANDARD_LINUX_SCREEN +#define FBIO_WAITFORVSYNC _IOW('F', 0x20, __u32) +#define S3CFB_SET_VSYNC_INT _IOW('F', 206, unsigned int) + if (ioctl(m->framebuffer->fd, FBIOPAN_DISPLAY, &m->info) == -1) { + LOGE("FBIOPAN_DISPLAY failed"); + m->base.unlock(&m->base, buffer); + return 0; + } + + if (m->enableVSync) { + /* enable VSYNC */ + interrupt = 1; + if (ioctl(m->framebuffer->fd, S3CFB_SET_VSYNC_INT, &interrupt) < 0) { + LOGE("S3CFB_SET_VSYNC_INT enable failed"); + return 0; + } + /* wait for VSYNC */ + +#ifdef MALI_VSYNC_EVENT_REPORT_ENABLE + gralloc_mali_vsync_report(MALI_VSYNC_EVENT_BEGIN_WAIT); +#endif + int crtc; + crtc = 0; + if (ioctl(m->framebuffer->fd, FBIO_WAITFORVSYNC, &crtc) < 0) { + LOGE("FBIO_WAITFORVSYNC failed"); +#ifdef MALI_VSYNC_EVENT_REPORT_ENABLE + gralloc_mali_vsync_report(MALI_VSYNC_EVENT_END_WAIT); +#endif + return 0; + } +#ifdef MALI_VSYNC_EVENT_REPORT_ENABLE + gralloc_mali_vsync_report(MALI_VSYNC_EVENT_END_WAIT); +#endif + // disable VSYNC + interrupt = 0; + if (ioctl(m->framebuffer->fd, S3CFB_SET_VSYNC_INT, &interrupt) < 0) { + LOGE("S3CFB_SET_VSYNC_INT disable failed"); + return 0; + } +#else + /*Standard Android way*/ +#ifdef MALI_VSYNC_EVENT_REPORT_ENABLE + gralloc_mali_vsync_report(MALI_VSYNC_EVENT_BEGIN_WAIT); +#endif + if (ioctl(m->framebuffer->fd, FBIOPUT_VSCREENINFO, &m->info) == -1) { + LOGE("FBIOPUT_VSCREENINFO failed"); +#ifdef MALI_VSYNC_EVENT_REPORT_ENABLE + gralloc_mali_vsync_report(MALI_VSYNC_EVENT_END_WAIT); +#endif + m->base.unlock(&m->base, buffer); + return -errno; + } +#ifdef MALI_VSYNC_EVENT_REPORT_ENABLE + gralloc_mali_vsync_report(MALI_VSYNC_EVENT_END_WAIT); +#endif +#endif + } + m->currentBuffer = buffer; + } else { + /* + * If we can't do the page_flip, just copy the buffer to the front + * FIXME: use copybit HAL instead of memcpy + */ + 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) +{ + /* Nothing to do, already initialized */ + if (module->framebuffer) + return 0; + + 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) + return -errno; + + struct fb_fix_screeninfo finfo; + if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) == -1) + return -errno; + + struct fb_var_screeninfo info; + if (ioctl(fd, FBIOGET_VSCREENINFO, &info) == -1) + 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) + 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) + return -errno; + + if (finfo.smem_len <= 0) + return -errno; + + module->flags = flags; + module->info = info; + module->finfo = finfo; + module->xdpi = xdpi; + module->ydpi = ydpi; + module->fps = fps; + + char value[32]; + property_get("debug.gralloc.vsync", value, "1"); + module->enableVSync = atoi(value); + /* + * 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; +} + +int enableScreen(struct framebuffer_device_t* dev, int enable) +{ + private_module_t* m = reinterpret_cast<private_module_t*>(dev->common.module); + + if (enable == 1) { + if (ioctl(m->framebuffer->fd, FBIOBLANK, FB_BLANK_UNBLANK) < 0) { + LOGE("%s: FBIOBLANK failed : (%d:%s)", + __func__, m->framebuffer->fd, strerror(errno)); + return -EINVAL; + } + } else if (enable == 0) { + if (ioctl(m->framebuffer->fd, FBIOBLANK, FB_BLANK_POWERDOWN) < 0) { + LOGE("%s: FBIOBLANK failed : (%d:%s)", + __func__, m->framebuffer->fd, strerror(errno)); + return -EINVAL; + } + } else { + return -EINVAL; + } + 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) +{ +#ifndef HWC_HWOVERLAY + unsigned char pixels[4]; + /* By doing a readpixel 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. + Readpixel() also reads a dummy pixel, but this is not used. We only use this + function here to flush the render pipeline. */ + glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixels); + /* 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. + */ +#endif + 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) + return status; + + private_module_t* m = (private_module_t*)module; + status = init_frame_buffer(m); + if (status < 0) { + 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; + dev->enableScreen = &enableScreen; + + 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/exynos4/hal/libgralloc_ump/framebuffer_device.h b/exynos4/hal/libgralloc_ump/framebuffer_device.h new file mode 100644 index 0000000..efcf1c3 --- /dev/null +++ b/exynos4/hal/libgralloc_ump/framebuffer_device.h @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2010 ARM Limited. All rights reserved. + * + * Portions of this code have been modified from the original. + * These modifications are: + * * includes + * * framebuffer_device_open() + * * init_frame_buffer_locked() + * + * 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);
\ No newline at end of file diff --git a/exynos4/hal/libgralloc_ump/gr.h b/exynos4/hal/libgralloc_ump/gr.h new file mode 100644 index 0000000..3a43aa7 --- /dev/null +++ b/exynos4/hal/libgralloc_ump/gr.h @@ -0,0 +1,64 @@ +/* + * 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 GR_H_ +#define GR_H_ + +#include <stdint.h> +#ifdef HAVE_ANDROID_OS // just want PAGE_SIZE define +# include <asm/page.h> +#else +# include <sys/user.h> +#endif +#include <limits.h> +#include <sys/cdefs.h> +#include <hardware/gralloc.h> +#include <pthread.h> +#include <errno.h> + +#include <cutils/native_handle.h> + +/*****************************************************************************/ + +struct private_module_t; +struct private_handle_t; + +inline size_t roundUpToPageSize(size_t x) { + return (x + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1); +} + +int mapFrameBufferLocked(struct private_module_t* module); +int terminateBuffer(gralloc_module_t const* module, private_handle_t* hnd); +int mapBuffer(gralloc_module_t const* module, private_handle_t* hnd); + +/*****************************************************************************/ + +class Locker { + pthread_mutex_t mutex; +public: + class Autolock { + Locker& locker; + public: + inline Autolock(Locker& locker) : locker(locker) { locker.lock(); } + inline ~Autolock() { locker.unlock(); } + }; + inline Locker() { pthread_mutex_init(&mutex, 0); } + inline ~Locker() { pthread_mutex_destroy(&mutex); } + inline void lock() { pthread_mutex_lock(&mutex); } + inline void unlock() { pthread_mutex_unlock(&mutex); } +}; + +#endif /* GR_H_ */ diff --git a/exynos4/hal/libgralloc_ump/gralloc_helper.h b/exynos4/hal/libgralloc_ump/gralloc_helper.h new file mode 100644 index 0000000..21a5135 --- /dev/null +++ b/exynos4/hal/libgralloc_ump/gralloc_helper.h @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2010 ARM Limited. All rights reserved. + * + * Portions of this code have been modified from the original. + * These modifications are: + * * includes + * * round_up_to_page_size() + * + * 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/exynos4/hal/libgralloc_ump/gralloc_module.cpp b/exynos4/hal/libgralloc_ump/gralloc_module.cpp new file mode 100644 index 0000000..fb8fc43 --- /dev/null +++ b/exynos4/hal/libgralloc_ump/gralloc_module.cpp @@ -0,0 +1,480 @@ +/* + * Copyright (C) 2010 ARM Limited. All rights reserved. + * + * Portions of this code have been modified from the original. + * These modifications are: + * * includes + * * enums + * * gralloc_device_open() + * * gralloc_register_buffer() + * * gralloc_unregister_buffer() + * * gralloc_lock() + * * gralloc_unlock() + * * gralloc_module_methods + * * HAL_MODULE_INFO_SYM + * + * 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 <sys/mman.h> +#include <cutils/log.h> +#include <cutils/atomic.h> +#include <hardware/hardware.h> +#include <hardware/gralloc.h> +#include <fcntl.h> + +#include "gralloc_priv.h" +#include "alloc_device.h" +#include "framebuffer_device.h" + +#include "ump.h" +#include "ump_ref_drv.h" +#include "s5p_fimc.h" +#include "exynos_mem.h" +static pthread_mutex_t s_map_lock = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t sMapLock = PTHREAD_MUTEX_INITIALIZER; + +static int s_ump_is_open = 0; +static int gMemfd = 0; +#define PFX_NODE_MEM "/dev/exynos-mem" + +/* we need this for now because pmem cannot mmap at an offset */ +#define PMEM_HACK 1 +#ifdef USE_PARTIAL_FLUSH +struct private_handle_rect *rect_list; + +private_handle_rect *find_rect(int secure_id) +{ + private_handle_rect *psRect; + + for (psRect = rect_list; psRect; psRect = psRect->next) + if (psRect->handle == secure_id) + break; + if (!psRect) + return NULL; + + return psRect; +} + +private_handle_rect *find_last_rect(int secure_id) +{ + private_handle_rect *psRect; + private_handle_rect *psFRect; + + if (rect_list == NULL) { + rect_list = (private_handle_rect *)calloc(1, sizeof(private_handle_rect)); + return rect_list; + } + + for (psRect = rect_list; psRect; psRect = psRect->next) { + if (psRect->handle == secure_id) + return psFRect; + psFRect = psRect; + } + return psFRect; +} + +int release_rect(int secure_id) +{ + private_handle_rect *psRect; + private_handle_rect *psTRect; + + for (psRect = rect_list; psRect; psRect = psRect->next) { + if (psRect->next) { + if (psRect->next->handle == secure_id) { + if (psRect->next->next) + psTRect = psRect->next->next; + else + psTRect = NULL; + + free(psRect->next); + psRect->next = psTRect; + return 1; + } + } + } + + return 0; +} +#endif + +static int gralloc_map(gralloc_module_t const* module, + buffer_handle_t handle, void** vaddr) +{ + private_handle_t* hnd = (private_handle_t*)handle; + if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) { + if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_IOCTL) { + size_t size = FIMC1_RESERVED_SIZE * 1024; + void *mappedAddress = mmap(0, size, + PROT_READ|PROT_WRITE, MAP_SHARED, gMemfd, (hnd->paddr - hnd->offset)); + if (mappedAddress == MAP_FAILED) { + LOGE("Could not mmap %s fd(%d)", strerror(errno),hnd->fd); + return -errno; + } + hnd->base = intptr_t(mappedAddress) + hnd->offset; + } else 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; + } else { + size_t size = hnd->size; +#if PMEM_HACK + size += hnd->offset; +#endif + void *mappedAddress = mmap(0, size, + PROT_READ|PROT_WRITE, MAP_SHARED, hnd->fd, 0); + if (mappedAddress == MAP_FAILED) { + LOGE("Could not mmap %s fd(%d)", strerror(errno),hnd->fd); + return -errno; + } + hnd->base = intptr_t(mappedAddress) + hnd->offset; + } + } + *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; + if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) { + if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_IOCTL) { + void* base = (void*)(intptr_t(hnd->base) - hnd->offset); + size_t size = FIMC1_RESERVED_SIZE * 1024; + if (munmap(base, size) < 0) + LOGE("Could not unmap %s", strerror(errno)); + } else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) { + void* base = (void*)hnd->base; + size_t size = hnd->size; + if (ion_unmap(base, size) < 0) + LOGE("Could not ion_unmap %s", strerror(errno)); + ion_client_destroy(hnd->ion_client); + } else { + void* base = (void*)hnd->base; + size_t size = hnd->size; +#if PMEM_HACK + base = (void*)(intptr_t(base) - hnd->offset); + size += hnd->offset; +#endif + if (munmap(base, size) < 0) + LOGE("Could not unmap %s", strerror(errno)); + } + } + 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; + + 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 err = 0; + 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; + +#ifdef USE_PARTIAL_FLUSH + if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP) { + private_handle_rect *psRect; + private_handle_rect *psFRect; + psRect = (private_handle_rect *)calloc(1, sizeof(private_handle_rect)); + psRect->handle = (int)hnd->ump_id; + psRect->stride = (int)hnd->stride; + psFRect = find_last_rect((int)hnd->ump_id); + psFRect->next = psRect; + } +#endif + if (hnd->pid == getpid()) + return 0; + + if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) + err = gralloc_map(module, handle, &vaddr); + + pthread_mutex_lock(&s_map_lock); + + if (!s_ump_is_open) { + ump_result res = ump_open(); /* TODO: Fix a ump_close() somewhere??? */ + 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 = (int)ump_handle_create_from_secure_id(hnd->ump_id); + if (UMP_INVALID_MEMORY_HANDLE != (ump_handle)hnd->ump_mem_handle) { + hnd->base = (int)ump_mapped_pointer_get((ump_handle)hnd->ump_mem_handle); + 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_reference_release((ump_handle)hnd->ump_mem_handle); + } else { + LOGE("Failed to create UMP handle"); + } + } else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_PMEM) { + pthread_mutex_unlock(&s_map_lock); + return 0; + } else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_IOCTL) { + void* vaddr = NULL; + + if (gMemfd == 0) { + gMemfd = open(PFX_NODE_MEM, O_RDWR); + if (gMemfd < 0) { + LOGE("%s:: %s exynos-mem open error\n", __func__, PFX_NODE_MEM); + return false; + } + } + + gralloc_map(module, handle, &vaddr); + pthread_mutex_unlock(&s_map_lock); + return 0; + } else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) { + hnd->ump_mem_handle = (int)ump_handle_create_from_secure_id(hnd->ump_id); + if (UMP_INVALID_MEMORY_HANDLE != (ump_handle)hnd->ump_mem_handle) { + vaddr = (void*)ump_mapped_pointer_get((ump_handle)hnd->ump_mem_handle); + if (0 != vaddr) { + 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_reference_release((ump_handle)hnd->ump_mem_handle); + } else { + LOGE("Failed to create UMP handle"); + } + } else { + LOGE("registering non-UMP 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; + +#ifdef USE_PARTIAL_FLUSH + if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP) + if (!release_rect((int)hnd->ump_id)) + LOGE("secureID: 0x%x, release error", (int)hnd->ump_id); +#endif + 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_mapped_pointer_release((ump_handle)hnd->ump_mem_handle); + hnd->base = 0; + ump_reference_release((ump_handle)hnd->ump_mem_handle); + hnd->ump_mem_handle = (int)UMP_INVALID_MEMORY_HANDLE; + hnd->lockState = 0; + hnd->writeOwner = 0; + } else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_IOCTL) { + if(hnd->base != 0) + gralloc_unmap(module, handle); + + pthread_mutex_unlock(&s_map_lock); + return 0; + } 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"); + } + + 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) +{ + int err = 0; + if (private_handle_t::validate(handle) < 0) { + LOGE("Locking invalid buffer, returning error"); + return -EINVAL; + } + + private_handle_t* hnd = (private_handle_t*)handle; + +#ifdef SAMSUNG_EXYNOS_CACHE_UMP + if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP) { +#ifdef USE_PARTIAL_FLUSH + private_handle_rect *psRect; + psRect = find_rect((int)hnd->ump_id); + psRect->l = l; + psRect->t = t; + psRect->w = w; + psRect->h= h; + psRect->locked = 1; +#endif + } +#endif + 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 err; +} + +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; + +#ifdef SAMSUNG_EXYNOS_CACHE_UMP + if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP) { +#ifdef USE_PARTIAL_FLUSH + private_handle_rect *psRect; + psRect = find_rect((int)hnd->ump_id); + ump_cpu_msync_now((ump_handle)hnd->ump_mem_handle, UMP_MSYNC_CLEAN, + (void *)(hnd->base + (psRect->stride * psRect->t)), psRect->stride * psRect->h ); + return 0; +#endif + ump_cpu_msync_now((ump_handle)hnd->ump_mem_handle, UMP_MSYNC_CLEAN_AND_INVALIDATE, NULL, 0); + } +#endif + if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) + ion_msync(hnd->ion_client, hnd->fd, IMSYNC_DEV_TO_RW | IMSYNC_SYNC_FOR_DEV, hnd->size, hnd->offset); + + if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_IOCTL) { + int ret; + exynos_mem_flush_range mem; + mem.start = hnd->paddr; + mem.length = hnd->size; + + ret = ioctl(gMemfd, EXYNOS_MEM_PADDR_CACHE_FLUSH, &mem); + if (ret < 0) { + LOGE("Error in exynos-mem : EXYNOS_MEM_PADDR_CACHE_FLUSH (%d)\n", ret); + return false; + } + } + + return 0; +} + +static int gralloc_getphys(gralloc_module_t const* module, buffer_handle_t handle, void** paddr) +{ + private_handle_t* hnd = (private_handle_t*)handle; + paddr[0] = (void*)hnd->paddr; + paddr[1] = (void*)(hnd->paddr + hnd->uoffset); + paddr[2] = (void*)(hnd->paddr + hnd->uoffset + hnd->voffset); + return 0; +} + +/* There is one global instance of the module */ +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, + getphys: gralloc_getphys, + perform: NULL, + reserved_proc: {0,}, + }, + framebuffer: NULL, + flags: 0, + numBuffers: 0, + bufferMask: 0, + lock: PTHREAD_MUTEX_INITIALIZER, + currentBuffer: NULL, + ion_client: -1, +}; |