| /* |
| * Copyright (C) 2013 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 <limits.h> |
| #include <errno.h> |
| #include <pthread.h> |
| #include <unistd.h> |
| #include <string.h> |
| |
| #include <sys/mman.h> |
| #include <sys/stat.h> |
| #include <sys/types.h> |
| |
| #include <log/log.h> |
| #include <cutils/atomic.h> |
| |
| #include <hardware/hardware.h> |
| #include <hardware/gralloc.h> |
| #include <inttypes.h> |
| #include <sync/sync.h> |
| |
| #include <hardware/exynos/ion.h> |
| #include <linux/ion.h> |
| #include <exynos_ion.h> |
| #include "gralloc_priv.h" |
| #include "exynos_format.h" |
| |
| #include "VendorVideoAPI.h" |
| #define INT_TO_PTR(var) ((void *)(unsigned long)var) |
| #define MSCL_EXT_SIZE 512 |
| #define MSCL_ALIGN 128 |
| |
| #define PRIV_SIZE sizeof(ExynosVideoMeta) |
| |
| #include "format_chooser.h" |
| |
| /*****************************************************************************/ |
| int getIonFd(gralloc_module_t const *module) |
| { |
| private_module_t* m = const_cast<private_module_t*>(reinterpret_cast<const private_module_t*>(module)); |
| if (m->ionfd == -1) |
| m->ionfd = exynos_ion_open(); |
| return m->ionfd; |
| } |
| |
| static int gralloc_map(gralloc_module_t const* module, buffer_handle_t handle) |
| { |
| void *privAddress; |
| |
| private_handle_t *hnd = (private_handle_t*)handle; |
| hnd->base = hnd->base1 = hnd->base2 = 0; |
| |
| switch (hnd->format) { |
| case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B: |
| case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_PRIV: |
| privAddress = mmap(0, hnd->size2, PROT_READ|PROT_WRITE, MAP_SHARED, hnd->fd2, 0); |
| if (privAddress == MAP_FAILED) { |
| ALOGE("%s: could not mmap %s", __func__, strerror(errno)); |
| } else { |
| hnd->base2 = (uint64_t)privAddress; |
| exynos_ion_sync_fd(getIonFd(module), hnd->fd2); |
| } |
| break; |
| default: |
| break; |
| } |
| |
| if ((hnd->flags & GRALLOC_USAGE_PROTECTED) && |
| !(hnd->flags & GRALLOC_USAGE_PRIVATE_NONSECURE)) { |
| return 0; |
| } |
| |
| if (!(hnd->flags & GRALLOC_USAGE_PROTECTED) && !(hnd->flags & GRALLOC_USAGE_NOZEROED)) { |
| void* mappedAddress = mmap(0, hnd->size, PROT_READ|PROT_WRITE, MAP_SHARED, |
| hnd->fd, 0); |
| if (mappedAddress == MAP_FAILED) { |
| ALOGE("%s: could not mmap %s", __func__, strerror(errno)); |
| return -errno; |
| } |
| hnd->base = (uint64_t)mappedAddress; |
| exynos_ion_sync_fd(getIonFd(module), hnd->fd); |
| |
| if (hnd->fd1 >= 0) { |
| void *mappedAddress1 = (void*)mmap(0, hnd->size1, PROT_READ|PROT_WRITE, |
| MAP_SHARED, hnd->fd1, 0); |
| if (mappedAddress1 == MAP_FAILED) { |
| ALOGE("%s: could not mmap %s", __func__, strerror(errno)); |
| return -errno; |
| } |
| hnd->base1 = (uint64_t)mappedAddress1; |
| exynos_ion_sync_fd(getIonFd(module), hnd->fd1); |
| } |
| if (hnd->fd2 >= 0) { |
| if ((hnd->format != HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_PRIV) && |
| (hnd->format != HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B)) { |
| void *mappedAddress2 = (void*)mmap(0, hnd->size2, PROT_READ|PROT_WRITE, MAP_SHARED, hnd->fd2, 0); |
| if (mappedAddress2 == MAP_FAILED) { |
| ALOGE("%s: could not mmap %s", __func__, strerror(errno)); |
| return -errno; |
| } |
| hnd->base2 = (uint64_t)mappedAddress2; |
| exynos_ion_sync_fd(getIonFd(module), hnd->fd2); |
| } |
| } |
| } |
| |
| return 0; |
| } |
| |
| static int gralloc_unmap(buffer_handle_t handle) |
| { |
| private_handle_t* hnd = (private_handle_t*)handle; |
| |
| switch (hnd->format) { |
| case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B: |
| case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_PRIV: |
| if (munmap(INT_TO_PTR(hnd->base2), hnd->size2) < 0) { |
| ALOGE("%s :could not unmap %s %#" PRIx64 " %d", __func__, strerror(errno), hnd->base2, hnd->size2); |
| } |
| hnd->base2 = 0; |
| break; |
| default: |
| break; |
| } |
| |
| if (!hnd->base) |
| return 0; |
| |
| if (munmap(INT_TO_PTR(hnd->base), hnd->size) < 0) { |
| ALOGE("%s :could not unmap %s %#" PRIx64 " %d", __func__, strerror(errno), hnd->base, hnd->size); |
| } |
| hnd->base = 0; |
| |
| if (hnd->fd1 >= 0) { |
| if (!hnd->base1) |
| return 0; |
| if (munmap(INT_TO_PTR(hnd->base1), hnd->size1) < 0) { |
| ALOGE("%s :could not unmap %s %#" PRIx64 " %d", __func__, strerror(errno), hnd->base1, hnd->size1); |
| } |
| hnd->base1 = 0; |
| } |
| if (hnd->fd2 >= 0) { |
| if (!hnd->base2) |
| return 0; |
| if (munmap(INT_TO_PTR(hnd->base2), hnd->size2) < 0) { |
| ALOGE("%s :could not unmap %s %#" PRIx64 " %d", __func__, strerror(errno), hnd->base2, hnd->size2); |
| } |
| hnd->base2 = 0; |
| } |
| return 0; |
| } |
| |
| /*****************************************************************************/ |
| |
| int grallocMap(gralloc_module_t const* module, private_handle_t *hnd) |
| { |
| return gralloc_map(module, hnd); |
| } |
| |
| int grallocUnmap(private_handle_t *hnd) |
| { |
| return gralloc_unmap(hnd); |
| } |
| |
| /*****************************************************************************/ |
| |
| int gralloc_register_buffer(gralloc_module_t const* module, |
| buffer_handle_t handle) |
| { |
| int err; |
| if (private_handle_t::validate(handle) < 0) |
| return -EINVAL; |
| |
| err = gralloc_map(module, handle); |
| |
| private_handle_t* hnd = (private_handle_t*)handle; |
| ALOGV("%s: base %#" PRIx64 " %d %d %d %d\n", __func__, hnd->base, hnd->size, |
| hnd->width, hnd->height, hnd->stride); |
| |
| int ret; |
| ret = exynos_ion_import_handle(getIonFd(module), hnd->fd, &hnd->handle); |
| if (ret) |
| ALOGE("error importing handle %d %x\n", hnd->fd, hnd->format); |
| if (hnd->fd1 >= 0) { |
| ret = exynos_ion_import_handle(getIonFd(module), hnd->fd1, &hnd->handle1); |
| if (ret) |
| ALOGE("error importing handle1 %d %x\n", hnd->fd1, hnd->format); |
| } |
| if (hnd->fd2 >= 0) { |
| ret = exynos_ion_import_handle(getIonFd(module), hnd->fd2, &hnd->handle2); |
| if (ret) |
| ALOGE("error importing handle2 %d %x\n", hnd->fd2, hnd->format); |
| } |
| |
| return err; |
| } |
| |
| int gralloc_unregister_buffer(gralloc_module_t const* module, |
| buffer_handle_t handle) |
| { |
| if (private_handle_t::validate(handle) < 0) |
| return -EINVAL; |
| |
| private_handle_t* hnd = (private_handle_t*)handle; |
| ALOGV("%s: base %#" PRIx64 " %d %d %d %d\n", __func__, hnd->base, hnd->size, |
| hnd->width, hnd->height, hnd->stride); |
| |
| gralloc_unmap(handle); |
| |
| if (hnd->handle) |
| exynos_ion_free_handle(getIonFd(module), hnd->handle); |
| if (hnd->handle1) |
| exynos_ion_free_handle(getIonFd(module), hnd->handle1); |
| if (hnd->handle2) |
| exynos_ion_free_handle(getIonFd(module), hnd->handle2); |
| |
| return 0; |
| } |
| |
| int gralloc_lock(gralloc_module_t const* module, |
| buffer_handle_t handle, int usage, |
| int l, int t, int w, int h, |
| void** vaddr) |
| { |
| // this is called when a buffer is being locked for software |
| // access. in thin implementation we have nothing to do since |
| // not synchronization with the h/w is needed. |
| // typically this is used to wait for the h/w to finish with |
| // this buffer if relevant. the data cache may need to be |
| // flushed or invalidated depending on the usage bits and the |
| // hardware. |
| |
| if (private_handle_t::validate(handle) < 0) |
| { |
| ALOGE("handle is not valid. usage(%x), l,t,w,h(%d, %d, %d, %d)\n", usage, l, t, w, h); |
| return -EINVAL; |
| } |
| |
| private_handle_t* hnd = (private_handle_t*)handle; |
| |
| if (hnd->frameworkFormat == HAL_PIXEL_FORMAT_YCbCr_420_888) { |
| ALOGE("gralloc_lock can't be used with YCbCr_420_888 format"); |
| return -EINVAL; |
| } |
| |
| switch(hnd->format) |
| { |
| case HAL_PIXEL_FORMAT_EXYNOS_ARGB_8888: |
| case HAL_PIXEL_FORMAT_RGBA_8888: |
| case HAL_PIXEL_FORMAT_RGBX_8888: |
| case HAL_PIXEL_FORMAT_BGRA_8888: |
| case HAL_PIXEL_FORMAT_RGB_888: |
| case HAL_PIXEL_FORMAT_RGB_565: |
| case HAL_PIXEL_FORMAT_RAW16: |
| case HAL_PIXEL_FORMAT_RAW_OPAQUE: |
| case HAL_PIXEL_FORMAT_BLOB: |
| case HAL_PIXEL_FORMAT_YCbCr_422_I: |
| case HAL_PIXEL_FORMAT_Y8: |
| case HAL_PIXEL_FORMAT_Y16: |
| case HAL_PIXEL_FORMAT_YV12: |
| case HAL_PIXEL_FORMAT_RGBA_1010102: |
| break; |
| default: |
| ALOGE("gralloc_lock doesn't support YUV formats. Please use gralloc_lock_ycbcr()"); |
| return -EINVAL; |
| } |
| |
| #ifdef GRALLOC_RANGE_FLUSH |
| if(usage & GRALLOC_USAGE_SW_WRITE_MASK) |
| { |
| hnd->lock_usage = GRALLOC_USAGE_SW_WRITE_RARELY; |
| hnd->lock_offset = t * hnd->stride; |
| hnd->lock_len = h * hnd->stride; |
| } |
| else |
| { |
| hnd->lock_usage = 0; |
| hnd->lock_offset = 0; |
| hnd->lock_len = 0; |
| } |
| #endif |
| |
| if (!hnd->base) |
| gralloc_map(module, hnd); |
| |
| *vaddr = INT_TO_PTR(hnd->base); |
| |
| return 0; |
| } |
| |
| int gralloc_unlock(gralloc_module_t const* module, |
| buffer_handle_t handle) |
| { |
| // we're done with a software buffer. nothing to do in this |
| // implementation. typically this is used to flush the data cache. |
| if (private_handle_t::validate(handle) < 0) |
| return -EINVAL; |
| |
| private_handle_t* hnd = (private_handle_t*)handle; |
| |
| if (!((hnd->flags & GRALLOC_USAGE_SW_READ_MASK) == GRALLOC_USAGE_SW_READ_OFTEN)) |
| return 0; |
| |
| #ifdef GRALLOC_RANGE_FLUSH |
| if(hnd->lock_usage & GRALLOC_USAGE_SW_WRITE_MASK) |
| { |
| if(((hnd->format == HAL_PIXEL_FORMAT_RGBA_8888) |
| || (hnd->format == HAL_PIXEL_FORMAT_RGBX_8888)) && (hnd->lock_offset != 0)) |
| exynos_ion_sync_fd_partial(getIonFd(module), hnd->fd, hnd->lock_offset * 4, hnd->lock_len * 4); |
| else |
| exynos_ion_sync_fd(getIonFd(module), hnd->fd); |
| |
| if (hnd->fd1 >= 0) |
| exynos_ion_sync_fd(getIonFd(module), hnd->fd1); |
| if (hnd->fd2 >= 0) |
| exynos_ion_sync_fd(getIonFd(module), hnd->fd2); |
| |
| hnd->lock_usage = 0; |
| } |
| #else |
| exynos_ion_sync_fd(getIonFd(module), hnd->fd); |
| |
| if (hnd->fd1 >= 0) |
| exynos_ion_sync_fd(getIonFd(module), hnd->fd1); |
| if (hnd->fd2 >= 0) |
| exynos_ion_sync_fd(getIonFd(module), hnd->fd2); |
| #endif |
| |
| return 0; |
| } |
| |
| int gralloc_lock_ycbcr(gralloc_module_t const* module, |
| buffer_handle_t handle, int usage, |
| int l, int t, int w, int h, |
| android_ycbcr *ycbcr) |
| { |
| if (private_handle_t::validate(handle) < 0) |
| { |
| ALOGE("handle is not valid. usage(%x), l,t,w,h(%d, %d, %d, %d)\n", usage, l, t, w, h); |
| return -EINVAL; |
| } |
| |
| if (!ycbcr) { |
| ALOGE("gralloc_lock_ycbcr got NULL ycbcr struct"); |
| return -EINVAL; |
| } |
| |
| int ext_size = 256; |
| |
| private_handle_t* hnd = (private_handle_t*)handle; |
| |
| if (!hnd->base) |
| gralloc_map(module, hnd); |
| |
| // If all CPU addresses are still NULL, do not anything. |
| if (!hnd->base && !hnd->base1 && !hnd->base2) |
| return 0; |
| |
| // Calculate offsets to underlying YUV data |
| size_t yStride; |
| size_t cStride; |
| size_t uOffset; |
| size_t vOffset; |
| size_t cStep; |
| switch (hnd->format) { |
| case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| yStride = cStride = hnd->width; |
| vOffset = yStride * hnd->height; |
| uOffset = vOffset + 1; |
| cStep = 2; |
| ycbcr->y = (void *)((unsigned long)hnd->base); |
| ycbcr->cb = (void *)(((unsigned long)hnd->base) + uOffset); |
| ycbcr->cr = (void *)(((unsigned long)hnd->base) + vOffset); |
| break; |
| case HAL_PIXEL_FORMAT_YV12: |
| yStride = ALIGN(hnd->width, 16); |
| cStride = ALIGN(yStride/2, 16); |
| vOffset = yStride * hnd->height; |
| uOffset = vOffset + (cStride * (hnd->height / 2)); |
| cStep = 1; |
| ycbcr->y = (void*)((unsigned long)hnd->base); |
| ycbcr->cr = (void*)(((unsigned long)hnd->base) + vOffset); |
| ycbcr->cb = (void*)(((unsigned long)hnd->base) + uOffset); |
| cStep = 1; |
| break; |
| case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M: |
| case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_PRIV: |
| case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_S10B: |
| yStride = cStride = hnd->stride; |
| vOffset = 1; |
| cStep = 2; |
| ycbcr->y = (void *)((unsigned long)hnd->base); |
| ycbcr->cb = (void *)((unsigned long)hnd->base1); |
| ycbcr->cr = (void *)(((unsigned long)hnd->base1) + vOffset); |
| |
| if ((hnd->format != HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M) && (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)) /* usage name will be changed as GRALLOC_USAGE_HW_VIDEO since v2.0 */ |
| ycbcr->cr = (void *)((unsigned long)hnd->base2); |
| break; |
| case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P: |
| yStride = hnd->stride; |
| cStride = ALIGN(yStride/2, 16); |
| uOffset = yStride * hnd->height; |
| vOffset = uOffset + (cStride * (hnd->height / 2)); |
| cStep = 1; |
| ycbcr->y = (void *)((unsigned long)hnd->base); |
| ycbcr->cb = (void*)(((unsigned long)hnd->base) + uOffset); |
| ycbcr->cr = (void*)(((unsigned long)hnd->base) + vOffset); |
| break; |
| /* separated color plane */ |
| case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SP_M_TILED: /* can't describe tiled format for user application */ |
| if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) { /* usage name will be changed as GRALLOC_USAGE_HW_VIDEO since v2.0 */ |
| yStride = hnd->stride; |
| cStride = hnd->stride; |
| cStep = 1; |
| ycbcr->y = (void *)((unsigned long)hnd->base); |
| ycbcr->cb = (void *)((unsigned long)hnd->base1); |
| ycbcr->cr = NULL; |
| } else { |
| ALOGE("gralloc_lock_ycbcr unexpected internal format %x", |
| hnd->format); |
| return -EINVAL; |
| } |
| break; |
| case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M: |
| case HAL_PIXEL_FORMAT_EXYNOS_YCrCb_420_SP_M_FULL: |
| yStride = cStride = hnd->stride; |
| uOffset = 1; |
| cStep = 2; |
| ycbcr->y = (void *)((unsigned long)hnd->base); |
| ycbcr->cr = (void *)((unsigned long)hnd->base1); |
| ycbcr->cb = (void *)(((unsigned long)hnd->base1) + uOffset); |
| break; |
| case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_P_M: |
| yStride = hnd->stride; |
| cStride = ALIGN(yStride/2, 16); |
| cStep = 1; |
| ycbcr->y = (void *)((unsigned long)hnd->base); |
| ycbcr->cb = (void *)((unsigned long)hnd->base1); |
| ycbcr->cr = (void *)((unsigned long)hnd->base2); |
| break; |
| case HAL_PIXEL_FORMAT_EXYNOS_YV12_M: |
| yStride = hnd->stride; |
| cStride = ALIGN(yStride/2, 16); |
| cStep = 1; |
| ycbcr->y = (void *)((unsigned long)hnd->base); |
| ycbcr->cr = (void *)((unsigned long)hnd->base1); |
| ycbcr->cb = (void *)((unsigned long)hnd->base2); |
| break; |
| case HAL_PIXEL_FORMAT_YCbCr_422_I: |
| yStride = cStride = hnd->stride; |
| cStep = 1; |
| ycbcr->y = (void *)((unsigned long)hnd->base); |
| ycbcr->cb = 0; |
| ycbcr->cr = 0; |
| break; |
| /* included h/w restrictions */ |
| case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN: |
| yStride = cStride = hnd->stride; |
| uOffset = (yStride * hnd->vstride) + ext_size; |
| vOffset = uOffset + 1; |
| cStep = 2; |
| ycbcr->y = (void *)((unsigned long)hnd->base); |
| ycbcr->cb = (void *)(((unsigned long)hnd->base) + uOffset); |
| ycbcr->cr = (void *)(((unsigned long)hnd->base) + vOffset); |
| break; |
| case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_420_SPN_S10B: |
| yStride = cStride = hnd->stride; |
| uOffset = ((yStride * hnd->vstride) + ext_size) + ((ALIGN(hnd->width / 4, 16) * hnd->vstride) + 64); |
| vOffset = uOffset + 1; |
| cStep = 2; |
| ycbcr->y = (void *)((unsigned long)hnd->base); |
| ycbcr->cb = (void *)(((unsigned long)hnd->base) + uOffset); |
| ycbcr->cr = (void *)(((unsigned long)hnd->base) + vOffset); |
| break; |
| case HAL_PIXEL_FORMAT_Y8: |
| case HAL_PIXEL_FORMAT_Y16: |
| yStride = cStride = hnd->stride; |
| uOffset = 0; |
| vOffset = 0; |
| cStep = 1; |
| ycbcr->y = (void *)((unsigned long)hnd->base); |
| ycbcr->cb = 0; |
| ycbcr->cr = 0; |
| break; |
| case HAL_PIXEL_FORMAT_EXYNOS_YCbCr_P010_M: |
| yStride = cStride = hnd->stride; |
| vOffset = 2; |
| cStep = 2; |
| ycbcr->y = (void *)((unsigned long)hnd->base); |
| ycbcr->cb = (void *)((unsigned long)hnd->base1); |
| ycbcr->cr = (void *)((unsigned long)hnd->base2); |
| break; |
| default: |
| ALOGE("gralloc_lock_ycbcr unexpected internal format %x", |
| hnd->format); |
| return -EINVAL; |
| } |
| |
| ycbcr->ystride = yStride; |
| ycbcr->cstride = cStride; |
| ycbcr->chroma_step = cStep; |
| |
| // Zero out reserved fields |
| memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved)); |
| |
| /* |
| ALOGD("gralloc_lock_ycbcr success. format : %x, usage: %x, ycbcr.y: %p, .cb: %p, .cr: %p, " |
| ".ystride: %d , .cstride: %d, .chroma_step: %d", hnd->format, usage, |
| ycbcr->y, ycbcr->cb, ycbcr->cr, ycbcr->ystride, ycbcr->cstride, |
| ycbcr->chroma_step); |
| */ |
| |
| return 0; |
| } |