diff --git a/Android.mk b/Android.mk index 1c16b9088..b5961ad0c 100644 --- a/Android.mk +++ b/Android.mk @@ -1,6 +1,11 @@ -display-hals := libgralloc libcopybit liblight libmemtrack libqservice libqdutils -display-hals += hdmi_cec +display-hals := libcopybit liblight libmemtrack libqservice libqdutils +ifneq ($(TARGET_USES_GRALLOC1), true) + display-hals += libgralloc +else + display-hals += libgralloc1 +endif +display-hals += hdmi_cec sdm-libs := sdm/libs display-hals += $(sdm-libs)/utils $(sdm-libs)/core $(sdm-libs)/hwc $(sdm-libs)/hwc2 diff --git a/common.mk b/common.mk index 62bd12867..dea9dc008 100644 --- a/common.mk +++ b/common.mk @@ -16,7 +16,11 @@ common_libs := liblog libutils libcutils libhardware #Common C flags common_flags := -DDEBUG_CALC_FPS -Wno-missing-field-initializers common_flags += -Wconversion -Wall -Werror -common_flags += -isystem $(display_top)/libgralloc +ifneq ($(TARGET_USES_GRALLOC1), true) + common_flags += -isystem $(display_top)/libgralloc +else + common_flags += -isystem $(display_top)/libgralloc1 +endif ifeq ($(TARGET_USES_POST_PROCESSING),true) common_flags += -DUSES_POST_PROCESSING diff --git a/libcopybit/software_converter.h b/libcopybit/software_converter.h index 6e53e1641..cc6ae342a 100644 --- a/libcopybit/software_converter.h +++ b/libcopybit/software_converter.h @@ -31,11 +31,12 @@ #include #include "gralloc_priv.h" -#include "gr.h" #define COPYBIT_SUCCESS 0 #define COPYBIT_FAILURE -1 +#define ALIGN(x, y) (((x) + y - 1) & (~(y - 1))) + int convertYV12toYCrCb420SP(const copybit_image_t *src,private_handle_t *yv12_handle); /* diff --git a/libgralloc1/Android.mk b/libgralloc1/Android.mk new file mode 100644 index 000000000..b66a027cc --- /dev/null +++ b/libgralloc1/Android.mk @@ -0,0 +1,25 @@ +# Gralloc module +LOCAL_PATH := $(call my-dir) +include $(LOCAL_PATH)/../common.mk +include $(CLEAR_VARS) + +LOCAL_MODULE := gralloc.$(TARGET_BOARD_PLATFORM) +LOCAL_MODULE_RELATIVE_PATH := hw +LOCAL_MODULE_TAGS := optional +LOCAL_C_INCLUDES := $(common_includes) \ + external/libcxx/include/ + +LOCAL_SHARED_LIBRARIES := $(common_libs) libqdMetaData libsync libqdutils +LOCAL_CFLAGS := $(common_flags) -DLOG_TAG=\"qdgralloc\" -Wall -std=c++11 -Werror +LOCAL_CFLAGS += -isystem $(kernel_includes) +LOCAL_CLANG := true +LOCAL_ADDITIONAL_DEPENDENCIES := $(common_deps) $(kernel_deps) +LOCAL_SRC_FILES := gr_utils.cpp \ + gr_ion_alloc.cpp \ + gr_adreno_info.cpp \ + gr_allocator.cpp \ + gr_buf_mgr.cpp \ + gr_device_impl.cpp +LOCAL_COPY_HEADERS_TO := $(common_header_export_path) +LOCAL_COPY_HEADERS := gr_device_impl.h gralloc_priv.h gr_priv_handle.h +include $(BUILD_SHARED_LIBRARY) diff --git a/libgralloc1/gr_adreno_info.cpp b/libgralloc1/gr_adreno_info.cpp new file mode 100644 index 000000000..adb59f082 --- /dev/null +++ b/libgralloc1/gr_adreno_info.cpp @@ -0,0 +1,198 @@ +/* + * Copyright (c) 2011-2016, The Linux Foundation. All rights reserved. + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * * Neither the name of The Linux Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include + +#include "gralloc_priv.h" +#include "gr_adreno_info.h" +#include "gr_utils.h" + +namespace gralloc1 { + +AdrenoMemInfo::AdrenoMemInfo() { +} + +bool AdrenoMemInfo::Init() { + libadreno_utils_ = ::dlopen("libadreno_utils.so", RTLD_NOW); + if (libadreno_utils_) { + *reinterpret_cast(&LINK_adreno_compute_aligned_width_and_height) = + ::dlsym(libadreno_utils_, "compute_aligned_width_and_height"); + *reinterpret_cast(&LINK_adreno_compute_padding) = + ::dlsym(libadreno_utils_, "compute_surface_padding"); + *reinterpret_cast(&LINK_adreno_isMacroTilingSupportedByGpu) = + ::dlsym(libadreno_utils_, "isMacroTilingSupportedByGpu"); + *reinterpret_cast(&LINK_adreno_compute_compressedfmt_aligned_width_and_height) = + ::dlsym(libadreno_utils_, "compute_compressedfmt_aligned_width_and_height"); + *reinterpret_cast(&LINK_adreno_isUBWCSupportedByGpu) = + ::dlsym(libadreno_utils_, "isUBWCSupportedByGpu"); + *reinterpret_cast(&LINK_adreno_get_gpu_pixel_alignment) = + ::dlsym(libadreno_utils_, "get_gpu_pixel_alignment"); + } else { + ALOGE(" Failed to load libadreno_utils.so"); + return false; + } + + // Check if the overriding property debug.gralloc.gfx_ubwc_disable_ + // that disables UBWC allocations for the graphics stack is set + char property[PROPERTY_VALUE_MAX]; + property_get("debug.gralloc.gfx_ubwc_disable_", property, "0"); + if (!(strncmp(property, "1", PROPERTY_VALUE_MAX)) || + !(strncmp(property, "true", PROPERTY_VALUE_MAX))) { + gfx_ubwc_disable_ = true; + } + + if ((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) && + (!strncmp(property, "1", PROPERTY_VALUE_MAX) || + (!strncasecmp(property, "true", PROPERTY_VALUE_MAX)))) { + map_fb_ = true; + } + + return true; +} + +AdrenoMemInfo::~AdrenoMemInfo() { + if (libadreno_utils_) { + ::dlclose(libadreno_utils_); + } +} + +bool AdrenoMemInfo::IsMacroTilingSupportedByGPU() { + if (LINK_adreno_isMacroTilingSupportedByGpu) { + return LINK_adreno_isMacroTilingSupportedByGpu(); + } + + return false; +} + +void AdrenoMemInfo::AlignUnCompressedRGB(int width, int height, int format, int tile_enabled, + unsigned int *aligned_w, unsigned int *aligned_h) { + *aligned_w = (unsigned int)ALIGN(width, 32); + *aligned_h = (unsigned int)ALIGN(height, 32); + + // Don't add any additional padding if debug.gralloc.map_fb_memory + // is enabled + if (map_fb_) { + return; + } + + int bpp = 4; + switch (format) { + case HAL_PIXEL_FORMAT_RGB_888: + bpp = 3; + break; + case HAL_PIXEL_FORMAT_RGB_565: + case HAL_PIXEL_FORMAT_BGR_565: + case HAL_PIXEL_FORMAT_RGBA_5551: + case HAL_PIXEL_FORMAT_RGBA_4444: + bpp = 2; + break; + default: + break; + } + + int raster_mode = 0; // Adreno unknown raster mode. + int padding_threshold = 512; // Threshold for padding surfaces. + // the function below computes aligned width and aligned height + // based on linear or macro tile mode selected. + if (LINK_adreno_compute_aligned_width_and_height) { + LINK_adreno_compute_aligned_width_and_height( + width, height, bpp, tile_enabled, raster_mode, padding_threshold, + reinterpret_cast(aligned_w), reinterpret_cast(aligned_h)); + } else if (LINK_adreno_compute_padding) { + int surface_tile_height = 1; // Linear surface + *aligned_w = UINT(LINK_adreno_compute_padding(width, bpp, surface_tile_height, raster_mode, + padding_threshold)); + ALOGW("%s: Warning!! Old GFX API is used to calculate stride", __FUNCTION__); + } else { + ALOGW( + "%s: Warning!! Symbols compute_surface_padding and " + "compute_aligned_width_and_height not found", + __FUNCTION__); + } +} + +void AdrenoMemInfo::AlignCompressedRGB(int width, int height, int format, unsigned int *aligned_w, + unsigned int *aligned_h) { + if (LINK_adreno_compute_compressedfmt_aligned_width_and_height) { + int bytesPerPixel = 0; + int raster_mode = 0; // Adreno unknown raster mode. + int padding_threshold = 512; // Threshold for padding + // surfaces. + + LINK_adreno_compute_compressedfmt_aligned_width_and_height( + width, height, format, 0, raster_mode, padding_threshold, + reinterpret_cast(aligned_w), reinterpret_cast(aligned_h), &bytesPerPixel); + } else { + ALOGW("%s: Warning!! compute_compressedfmt_aligned_width_and_height not found", __FUNCTION__); + } +} + +bool AdrenoMemInfo::IsUBWCSupportedByGPU(int format) { + if (!gfx_ubwc_disable_ && LINK_adreno_isUBWCSupportedByGpu) { + ADRENOPIXELFORMAT gpu_format = GetGpuPixelFormat(format); + return LINK_adreno_isUBWCSupportedByGpu(gpu_format); + } + + return false; +} + +uint32_t AdrenoMemInfo::GetGpuPixelAlignment() { + if (LINK_adreno_get_gpu_pixel_alignment) { + return LINK_adreno_get_gpu_pixel_alignment(); + } + + return 1; +} + +ADRENOPIXELFORMAT AdrenoMemInfo::GetGpuPixelFormat(int hal_format) { + switch (hal_format) { + case HAL_PIXEL_FORMAT_RGBA_8888: + return ADRENO_PIXELFORMAT_R8G8B8A8; + case HAL_PIXEL_FORMAT_RGBX_8888: + return ADRENO_PIXELFORMAT_R8G8B8X8; + case HAL_PIXEL_FORMAT_RGB_565: + return ADRENO_PIXELFORMAT_B5G6R5; + case HAL_PIXEL_FORMAT_BGR_565: + return ADRENO_PIXELFORMAT_R5G6B5; + case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: + return ADRENO_PIXELFORMAT_NV12; + case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: + case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC: + return ADRENO_PIXELFORMAT_NV12_EXT; + default: + ALOGE("%s: No map for format: 0x%x", __FUNCTION__, hal_format); + break; + } + + return ADRENO_PIXELFORMAT_UNKNOWN; +} + +} // namespace gralloc1 diff --git a/libgralloc1/gr_adreno_info.h b/libgralloc1/gr_adreno_info.h new file mode 100644 index 000000000..7b053ad6c --- /dev/null +++ b/libgralloc1/gr_adreno_info.h @@ -0,0 +1,156 @@ +/* + * Copyright (c) 2011-2016, The Linux Foundation. All rights reserved. + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * * Neither the name of The Linux Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __GR_ADRENO_INFO_H__ +#define __GR_ADRENO_INFO_H__ + +#ifdef VENUS_COLOR_FORMAT +#include +#else +#define VENUS_Y_STRIDE(args...) 0 +#define VENUS_Y_SCANLINES(args...) 0 +#define VENUS_BUFFER_SIZE(args...) 0 +#endif + +namespace gralloc1 { + +// Adreno Pixel Formats +typedef enum { + ADRENO_PIXELFORMAT_UNKNOWN = 0, + ADRENO_PIXELFORMAT_R8G8B8A8 = 28, + ADRENO_PIXELFORMAT_R8G8B8A8_SRGB = 29, + ADRENO_PIXELFORMAT_B5G6R5 = 85, + ADRENO_PIXELFORMAT_B5G5R5A1 = 86, + ADRENO_PIXELFORMAT_B8G8R8A8 = 90, + ADRENO_PIXELFORMAT_B8G8R8A8_SRGB = 91, + ADRENO_PIXELFORMAT_B8G8R8X8_SRGB = 93, + ADRENO_PIXELFORMAT_NV12 = 103, + ADRENO_PIXELFORMAT_YUY2 = 107, + ADRENO_PIXELFORMAT_B4G4R4A4 = 115, + ADRENO_PIXELFORMAT_NV12_EXT = 506, // NV12 with non-std alignment and offsets + ADRENO_PIXELFORMAT_R8G8B8X8 = 507, // GL_RGB8 (Internal) + ADRENO_PIXELFORMAT_R8G8B8 = 508, // GL_RGB8 + ADRENO_PIXELFORMAT_A1B5G5R5 = 519, // GL_RGB5_A1 + ADRENO_PIXELFORMAT_R8G8B8X8_SRGB = 520, // GL_SRGB8 + ADRENO_PIXELFORMAT_R8G8B8_SRGB = 521, // GL_SRGB8 + ADRENO_PIXELFORMAT_R5G6B5 = 610, // RGBA version of B5G6R5 + ADRENO_PIXELFORMAT_R5G5B5A1 = 611, // RGBA version of B5G5R5A1 + ADRENO_PIXELFORMAT_R4G4B4A4 = 612, // RGBA version of B4G4R4A4 + ADRENO_PIXELFORMAT_UYVY = 614, // YUV 4:2:2 packed progressive (1 plane) + ADRENO_PIXELFORMAT_NV21 = 619, + ADRENO_PIXELFORMAT_Y8U8V8A8 = 620, // YUV 4:4:4 packed (1 plane) + ADRENO_PIXELFORMAT_Y8 = 625, // Single 8-bit luma only channel YUV format +} ADRENOPIXELFORMAT; + +class AdrenoMemInfo { + public: + AdrenoMemInfo(); + + ~AdrenoMemInfo(); + + bool Init(); + + /* + * Function to compute aligned width and aligned height based on + * width, height, format and usage flags. + * + * @return aligned width, aligned height + */ + void GetAlignedWidthAndHeight(int width, int height, int format, int usage, + unsigned int *aligned_w, unsigned int *aligned_h, bool ubwc_enabled, + bool tile_enabled); + + /* + * Function to compute the adreno aligned width and aligned height + * based on the width and format. + * + * @return aligned width, aligned height + */ + void AlignUnCompressedRGB(int width, int height, int format, int tileEnabled, + unsigned int *aligned_w, unsigned int *aligned_h); + + /* + * Function to compute the adreno aligned width and aligned height + * based on the width and format. + * + * @return aligned width, aligned height + */ + void AlignCompressedRGB(int width, int height, int format, unsigned int *aligned_w, + unsigned int *aligned_h); + + /* + * Function to compute the pixel alignment requirement. + * + * @return alignment + */ + uint32_t GetGpuPixelAlignment(); + + /* + * Function to return whether GPU support MacroTile feature + * + * @return >0 : supported + * 0 : not supported + */ + bool IsMacroTilingSupportedByGPU(); + + /* + * Function to query whether GPU supports UBWC for given HAL format + * @return > 0 : supported + * 0 : not supported + */ + bool IsUBWCSupportedByGPU(int format); + + /* + * Function to get the corresponding Adreno format for given HAL format + */ + ADRENOPIXELFORMAT GetGpuPixelFormat(int hal_format); + + private: + // link(s)to adreno surface padding library. + int (*LINK_adreno_compute_padding)(int width, int bpp, int surface_tile_height, + int screen_tile_height, int padding_threshold) = NULL; + void (*LINK_adreno_compute_aligned_width_and_height)(int width, int height, int bpp, + int tile_mode, int raster_mode, + int padding_threshold, int *aligned_w, + int *aligned_h) = NULL; + int (*LINK_adreno_isMacroTilingSupportedByGpu)(void) = NULL; + void (*LINK_adreno_compute_compressedfmt_aligned_width_and_height)( + int width, int height, int format, int tile_mode, int raster_mode, int padding_threshold, + int *aligned_w, int *aligned_h, int *bpp) = NULL; + int (*LINK_adreno_isUBWCSupportedByGpu)(ADRENOPIXELFORMAT format) = NULL; + unsigned int (*LINK_adreno_get_gpu_pixel_alignment)() = NULL; + + bool gfx_ubwc_disable_ = false; + bool map_fb_ = false; + void *libadreno_utils_ = NULL; +}; + +} // namespace gralloc1 + +#endif // __GR_ADRENO_INFO_H__ diff --git a/libgralloc1/gr_allocator.cpp b/libgralloc1/gr_allocator.cpp new file mode 100644 index 000000000..b45ba83c5 --- /dev/null +++ b/libgralloc1/gr_allocator.cpp @@ -0,0 +1,871 @@ +/* + * Copyright (c) 2011-2016, The Linux Foundation. All rights reserved. + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * * Neither the name of The Linux Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include + +#include "gr_utils.h" +#include "gr_allocator.h" +#include "gr_adreno_info.h" +#include "gralloc_priv.h" + +#include "qd_utils.h" +#include "qdMetaData.h" + +#define ASTC_BLOCK_SIZE 16 + +#ifndef ION_FLAG_CP_PIXEL +#define ION_FLAG_CP_PIXEL 0 +#endif + +#ifndef ION_FLAG_ALLOW_NON_CONTIG +#define ION_FLAG_ALLOW_NON_CONTIG 0 +#endif + +#ifdef MASTER_SIDE_CP +#define CP_HEAP_ID ION_SECURE_HEAP_ID +#define SD_HEAP_ID ION_SECURE_DISPLAY_HEAP_ID +#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_CP_PIXEL) +#define ION_SD_FLAGS (ION_SECURE | ION_FLAG_CP_SEC_DISPLAY) +#else // SLAVE_SIDE_CP +#define CP_HEAP_ID ION_CP_MM_HEAP_ID +#define SD_HEAP_ID CP_HEAP_ID +#define ION_CP_FLAGS (ION_SECURE | ION_FLAG_ALLOW_NON_CONTIG) +#define ION_SD_FLAGS ION_SECURE +#endif + +namespace gralloc1 { + +Allocator::Allocator() : ion_allocator_(NULL), adreno_helper_(NULL) { +} + +bool Allocator::Init() { + ion_allocator_ = new IonAlloc(); + if (!ion_allocator_->Init()) { + return false; + } + + adreno_helper_ = new AdrenoMemInfo(); + if (!adreno_helper_->Init()) { + return false; + } + + gpu_support_macrotile = adreno_helper_->IsMacroTilingSupportedByGPU(); + int supports_macrotile = 0; + qdutils::querySDEInfo(qdutils::HAS_MACRO_TILE, &supports_macrotile); + display_support_macrotile = !!supports_macrotile; + + return true; +} + +Allocator::~Allocator() { + if (ion_allocator_) { + delete ion_allocator_; + } + + if (adreno_helper_) { + delete adreno_helper_; + } +} + +int Allocator::AllocateMem(AllocData *alloc_data, gralloc1_producer_usage_t prod_usage, + gralloc1_consumer_usage_t cons_usage) { + int ret; + alloc_data->uncached = UseUncached(prod_usage); + + // After this point we should have the right heap set, there is no fallback + GetIonHeapInfo(prod_usage, cons_usage, &alloc_data->heap_id, &alloc_data->alloc_type, + &alloc_data->flags); + + ret = ion_allocator_->AllocBuffer(alloc_data); + if (ret >= 0) { + alloc_data->alloc_type |= private_handle_t::PRIV_FLAGS_USES_ION; + } else { + ALOGE("%s: Failed to allocate buffer - heap: 0x%x flags: 0x%x", __FUNCTION__, + alloc_data->heap_id, alloc_data->flags); + } + + return ret; +} + +// Allocates buffer from width, height and format into a +// private_handle_t. It is the responsibility of the caller +// to free the buffer using the FreeBuffer function +int Allocator::AllocateBuffer(const BufferDescriptor &descriptor, private_handle_t **pHnd) { + AllocData data; + unsigned int aligned_w, aligned_h; + data.base = 0; + data.fd = -1; + data.offset = 0; + data.align = (unsigned int)getpagesize(); + int format = descriptor.GetFormat(); + gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage(); + gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage(); + GetBufferSizeAndDimensions(descriptor, &data.size, &aligned_w, &aligned_h); + + int err = AllocateMem(&data, prod_usage, cons_usage); + if (0 != err) { + ALOGE("%s: allocate failed", __FUNCTION__); + return -ENOMEM; + } + + if (IsUBwcEnabled(format, prod_usage, cons_usage)) { + data.alloc_type |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED; + } + + // Metadata is not allocated. would be empty + private_handle_t *hnd = new private_handle_t( + data.fd, data.size, INT(data.alloc_type), 0, INT(format), INT(aligned_w), INT(aligned_h), -1, + 0, 0, descriptor.GetWidth(), descriptor.GetHeight(), prod_usage, cons_usage); + hnd->base = (uint64_t)data.base; + hnd->offset = data.offset; + hnd->gpuaddr = 0; + *pHnd = hnd; + + return 0; +} + +int Allocator::MapBuffer(void **base, unsigned int size, unsigned int offset, int fd) { + if (ion_allocator_) { + return ion_allocator_->MapBuffer(base, size, offset, fd); + } + + return -EINVAL; +} + +int Allocator::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd) { + if (ion_allocator_) { + return ion_allocator_->FreeBuffer(base, size, offset, fd); + } + + return -EINVAL; +} + +int Allocator::CleanBuffer(void *base, unsigned int size, unsigned int offset, int fd, int op) { + if (ion_allocator_) { + return ion_allocator_->CleanBuffer(base, size, offset, fd, op); + } + + return -EINVAL; +} + +bool Allocator::CheckForBufferSharing(uint32_t num_descriptors, const BufferDescriptor *descriptors, + int *max_index) { + unsigned int cur_heap_id = 0, prev_heap_id = 0; + unsigned int cur_alloc_type = 0, prev_alloc_type = 0; + unsigned int cur_ion_flags = 0, prev_ion_flags = 0; + bool cur_uncached = false, prev_uncached = false; + unsigned int alignedw, alignedh; + unsigned int max_size = 0; + + *max_index = -1; + for (uint32_t i = 0; i < num_descriptors; i++) { + // Check Cached vs non-cached and all the ION flags + cur_uncached = UseUncached(descriptors[i].GetProducerUsage()); + GetIonHeapInfo(descriptors[i].GetProducerUsage(), descriptors[i].GetConsumerUsage(), + &cur_heap_id, &cur_alloc_type, &cur_ion_flags); + + if (i > 0 && (cur_heap_id != prev_heap_id || cur_alloc_type != prev_alloc_type || + cur_ion_flags != prev_ion_flags)) { + return false; + } + + // For same format type, find the descriptor with bigger size + GetAlignedWidthAndHeight(descriptors[i], &alignedw, &alignedh); + unsigned int size = GetSize(descriptors[i], alignedw, alignedh); + if (max_size < size) { + *max_index = INT(i); + max_size = size; + } + + prev_heap_id = cur_heap_id; + prev_uncached = cur_uncached; + prev_ion_flags = cur_ion_flags; + prev_alloc_type = cur_alloc_type; + } + + return true; +} + +bool Allocator::IsMacroTileEnabled(int format, gralloc1_producer_usage_t prod_usage, + gralloc1_consumer_usage_t cons_usage) { + bool tile_enabled = false; + + // Check whether GPU & MDSS supports MacroTiling feature + if (!adreno_helper_->IsMacroTilingSupportedByGPU() || !display_support_macrotile) { + return tile_enabled; + } + + // check the format + switch (format) { + case HAL_PIXEL_FORMAT_RGBA_8888: + case HAL_PIXEL_FORMAT_RGBX_8888: + case HAL_PIXEL_FORMAT_BGRA_8888: + case HAL_PIXEL_FORMAT_RGB_565: + case HAL_PIXEL_FORMAT_BGR_565: + if (!CpuCanAccess(prod_usage, cons_usage)) { + // not touched by CPU + tile_enabled = true; + } + break; + default: + break; + } + + return tile_enabled; +} + +// helper function +unsigned int Allocator::GetSize(const BufferDescriptor &descriptor, unsigned int alignedw, + unsigned int alignedh) { + unsigned int size = 0; + int format = descriptor.GetFormat(); + int width = descriptor.GetWidth(); + int height = descriptor.GetHeight(); + gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage(); + gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage(); + + if (IsUBwcEnabled(format, prod_usage, cons_usage)) { + return GetUBwcSize(width, height, format, alignedw, alignedh); + } + + if (IsUncompressedRGBFormat(format)) { + uint32_t bpp = GetBppForUncompressedRGB(format); + size = alignedw * alignedh * bpp; + return size; + } + + if (IsCompressedRGBFormat(format)) { + size = alignedw * alignedh * ASTC_BLOCK_SIZE; + return size; + } + + // Below switch should be for only YUV/custom formats + switch (format) { + case HAL_PIXEL_FORMAT_RAW16: + size = alignedw * alignedh * 2; + break; + case HAL_PIXEL_FORMAT_RAW10: + size = ALIGN(alignedw * alignedh, SIZE_4K); + break; + + // adreno formats + case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21 + size = ALIGN(alignedw * alignedh, SIZE_4K); + size += (unsigned int)ALIGN(2 * ALIGN(width / 2, 32) * ALIGN(height / 2, 32), SIZE_4K); + break; + case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12 + // The chroma plane is subsampled, + // but the pitch in bytes is unchanged + // The GPU needs 4K alignment, but the video decoder needs 8K + size = ALIGN(alignedw * alignedh, SIZE_8K); + size += ALIGN(alignedw * (unsigned int)ALIGN(height / 2, 32), SIZE_8K); + break; + case HAL_PIXEL_FORMAT_YV12: + if ((format == HAL_PIXEL_FORMAT_YV12) && ((width & 1) || (height & 1))) { + ALOGE("w or h is odd for the YV12 format"); + return 0; + } + size = alignedw * alignedh + (ALIGN(alignedw / 2, 16) * (alignedh / 2)) * 2; + size = ALIGN(size, (unsigned int)SIZE_4K); + break; + case HAL_PIXEL_FORMAT_YCbCr_420_SP: + case HAL_PIXEL_FORMAT_YCrCb_420_SP: + size = ALIGN((alignedw * alignedh) + (alignedw * alignedh) / 2 + 1, SIZE_4K); + break; + case HAL_PIXEL_FORMAT_YCbCr_422_SP: + case HAL_PIXEL_FORMAT_YCrCb_422_SP: + case HAL_PIXEL_FORMAT_YCbCr_422_I: + case HAL_PIXEL_FORMAT_YCrCb_422_I: + if (width & 1) { + ALOGE("width is odd for the YUV422_SP format"); + return 0; + } + size = ALIGN(alignedw * alignedh * 2, SIZE_4K); + break; + case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: + case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: + size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height); + break; + case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS: + size = VENUS_BUFFER_SIZE(COLOR_FMT_NV21, width, height); + break; + case HAL_PIXEL_FORMAT_BLOB: + case HAL_PIXEL_FORMAT_RAW_OPAQUE: + if (height != 1) { + ALOGE("%s: Buffers with HAL_PIXEL_FORMAT_BLOB must have height 1 ", __FUNCTION__); + return 0; + } + size = (unsigned int)width; + break; + case HAL_PIXEL_FORMAT_NV21_ZSL: + size = ALIGN((alignedw * alignedh) + (alignedw * alignedh) / 2, SIZE_4K); + break; + default: + ALOGE("%s: Unrecognized pixel format: 0x%x", __FUNCTION__, format); + return 0; + } + + return size; +} + +void Allocator::GetBufferSizeAndDimensions(int width, int height, int format, unsigned int *size, + unsigned int *alignedw, unsigned int *alignedh) { + BufferDescriptor descriptor = BufferDescriptor(width, height, format); + GetAlignedWidthAndHeight(descriptor, alignedw, alignedh); + + *size = GetSize(descriptor, *alignedw, *alignedh); +} + +void Allocator::GetBufferSizeAndDimensions(const BufferDescriptor &descriptor, unsigned int *size, + unsigned int *alignedw, unsigned int *alignedh) { + GetAlignedWidthAndHeight(descriptor, alignedw, alignedh); + + *size = GetSize(descriptor, *alignedw, *alignedh); +} + +void Allocator::GetBufferAttributes(const BufferDescriptor &descriptor, unsigned int *alignedw, + unsigned int *alignedh, int *tiled, unsigned int *size) { + int format = descriptor.GetFormat(); + gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage(); + gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage(); + + *tiled = false; + if (IsUBwcEnabled(format, prod_usage, cons_usage) || + IsMacroTileEnabled(format, prod_usage, cons_usage)) { + *tiled = true; + } + + GetAlignedWidthAndHeight(descriptor, alignedw, alignedh); + *size = GetSize(descriptor, *alignedw, *alignedh); +} + +void Allocator::GetYuvUbwcSPPlaneInfo(uint64_t base, uint32_t width, uint32_t height, + int color_format, struct android_ycbcr *ycbcr) { + // UBWC buffer has these 4 planes in the following sequence: + // Y_Meta_Plane, Y_Plane, UV_Meta_Plane, UV_Plane + unsigned int y_meta_stride, y_meta_height, y_meta_size; + unsigned int y_stride, y_height, y_size; + unsigned int c_meta_stride, c_meta_height, c_meta_size; + unsigned int alignment = 4096; + + y_meta_stride = VENUS_Y_META_STRIDE(color_format, INT(width)); + y_meta_height = VENUS_Y_META_SCANLINES(color_format, INT(height)); + y_meta_size = ALIGN((y_meta_stride * y_meta_height), alignment); + + y_stride = VENUS_Y_STRIDE(color_format, INT(width)); + y_height = VENUS_Y_SCANLINES(color_format, INT(height)); + y_size = ALIGN((y_stride * y_height), alignment); + + c_meta_stride = VENUS_UV_META_STRIDE(color_format, INT(width)); + c_meta_height = VENUS_UV_META_SCANLINES(color_format, INT(height)); + c_meta_size = ALIGN((c_meta_stride * c_meta_height), alignment); + + ycbcr->y = reinterpret_cast(base + y_meta_size); + ycbcr->cb = reinterpret_cast(base + y_meta_size + y_size + c_meta_size); + ycbcr->cr = reinterpret_cast(base + y_meta_size + y_size + c_meta_size + 1); + ycbcr->ystride = y_stride; + ycbcr->cstride = VENUS_UV_STRIDE(color_format, INT(width)); +} + +void Allocator::GetYuvSPPlaneInfo(uint64_t base, uint32_t width, uint32_t height, uint32_t bpp, + struct android_ycbcr *ycbcr) { + unsigned int ystride, cstride; + + ystride = cstride = UINT(width) * bpp; + ycbcr->y = reinterpret_cast(base); + ycbcr->cb = reinterpret_cast(base + ystride * UINT(height)); + ycbcr->cr = reinterpret_cast(base + ystride * UINT(height) + 1); + ycbcr->ystride = ystride; + ycbcr->cstride = cstride; + ycbcr->chroma_step = 2 * bpp; +} + +int Allocator::GetYUVPlaneInfo(const private_handle_t *hnd, struct android_ycbcr *ycbcr) { + int err = 0; + uint32_t width = UINT(hnd->width); + uint32_t height = UINT(hnd->height); + int format = hnd->format; + gralloc1_producer_usage_t prod_usage = hnd->GetProducerUsage(); + gralloc1_consumer_usage_t cons_usage = hnd->GetConsumerUsage(); + unsigned int ystride, cstride; + + memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved)); + MetaData_t *metadata = reinterpret_cast(hnd->base_metadata); + + // Check if UBWC buffer has been rendered in linear format. + if (metadata && (metadata->operation & LINEAR_FORMAT)) { + format = INT(metadata->linearFormat); + } + + // Check metadata if the geometry has been updated. + if (metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) { + int usage = 0; + + if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) { + usage = GRALLOC1_PRODUCER_USAGE_PRIVATE_ALLOC_UBWC; + } + + BufferDescriptor descriptor = + BufferDescriptor(metadata->bufferDim.sliceWidth, metadata->bufferDim.sliceHeight, format, + prod_usage, cons_usage); + GetAlignedWidthAndHeight(descriptor, &width, &height); + } + + // Get the chroma offsets from the handle width/height. We take advantage + // of the fact the width _is_ the stride + switch (format) { + // Semiplanar + case HAL_PIXEL_FORMAT_YCbCr_420_SP: + case HAL_PIXEL_FORMAT_YCbCr_422_SP: + case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: + case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: + // Same as YCbCr_420_SP_VENUS + GetYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr); + break; + + case HAL_PIXEL_FORMAT_YCbCr_420_P010: + GetYuvSPPlaneInfo(hnd->base, width, height, 2, ycbcr); + break; + + case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC: + GetYuvUbwcSPPlaneInfo(hnd->base, width, height, COLOR_FMT_NV12_UBWC, ycbcr); + ycbcr->chroma_step = 2; + break; + + case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC: + GetYuvUbwcSPPlaneInfo(hnd->base, width, height, COLOR_FMT_NV12_BPP10_UBWC, ycbcr); + ycbcr->chroma_step = 3; + break; + + case HAL_PIXEL_FORMAT_YCrCb_420_SP: + case HAL_PIXEL_FORMAT_YCrCb_422_SP: + case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: + case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS: + case HAL_PIXEL_FORMAT_NV21_ZSL: + case HAL_PIXEL_FORMAT_RAW16: + case HAL_PIXEL_FORMAT_RAW10: + GetYuvSPPlaneInfo(hnd->base, width, height, 1, ycbcr); + std::swap(ycbcr->cb, ycbcr->cr); + break; + + // Planar + case HAL_PIXEL_FORMAT_YV12: + ystride = width; + cstride = ALIGN(width / 2, 16); + ycbcr->y = reinterpret_cast(hnd->base); + ycbcr->cr = reinterpret_cast(hnd->base + ystride * height); + ycbcr->cb = reinterpret_cast(hnd->base + ystride * height + cstride * height / 2); + ycbcr->ystride = ystride; + ycbcr->cstride = cstride; + ycbcr->chroma_step = 1; + break; + + // Unsupported formats + case HAL_PIXEL_FORMAT_YCbCr_422_I: + case HAL_PIXEL_FORMAT_YCrCb_422_I: + case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: + default: + ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, format); + err = -EINVAL; + } + + return err; +} + +int Allocator::GetImplDefinedFormat(gralloc1_producer_usage_t prod_usage, + gralloc1_consumer_usage_t cons_usage, int format) { + int gr_format = format; + + // If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on + // the usage bits, gralloc assigns a format. + if (format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED || + format == HAL_PIXEL_FORMAT_YCbCr_420_888) { + if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_ALLOC_UBWC) { + gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC; + } else if (cons_usage & GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER) { + gr_format = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; // NV12 + } else if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_CAMERA_ZSL) { + gr_format = HAL_PIXEL_FORMAT_NV21_ZSL; // NV21 ZSL + } else if (cons_usage & GRALLOC1_CONSUMER_USAGE_CAMERA) { + gr_format = HAL_PIXEL_FORMAT_YCrCb_420_SP; // NV21 + } else if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) { + if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) { + gr_format = HAL_PIXEL_FORMAT_NV21_ZSL; // NV21 + } else { + gr_format = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS; // NV12 preview + } + } else if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) { + // XXX: If we still haven't set a format, default to RGBA8888 + gr_format = HAL_PIXEL_FORMAT_RGBA_8888; + } else if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) { + // If no other usage flags are detected, default the + // flexible YUV format to NV21_ZSL + gr_format = HAL_PIXEL_FORMAT_NV21_ZSL; + } + } + + return gr_format; +} + +// Explicitly defined UBWC formats +bool Allocator::IsUBwcFormat(int format) { + switch (format) { + case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC: + return true; + default: + return false; + } +} + +bool Allocator::IsUBwcSupported(int format) { + // Existing HAL formats with UBWC support + switch (format) { + case HAL_PIXEL_FORMAT_BGR_565: + case HAL_PIXEL_FORMAT_RGBA_8888: + case HAL_PIXEL_FORMAT_RGBX_8888: + case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: + case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: + return true; + default: + break; + } + + return false; +} + +/* The default policy is to return cached buffers unless the client explicity + * sets the PRIVATE_UNCACHED flag or indicates that the buffer will be rarely + * read or written in software. */ +// TODO(user) : As of now relying only on producer usage +bool Allocator::UseUncached(gralloc1_producer_usage_t usage) { + if ((usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_UNCACHED) || + (usage & GRALLOC1_PRODUCER_USAGE_PROTECTED)) { + return true; + } + + // CPU read rarely + if ((usage & GRALLOC1_PRODUCER_USAGE_CPU_READ) && + !(usage & GRALLOC1_PRODUCER_USAGE_CPU_READ_OFTEN)) { + return true; + } + + // CPU write rarely + if ((usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE) && + !(usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE_OFTEN)) { + return true; + } + + return false; +} + +void Allocator::GetIonHeapInfo(gralloc1_producer_usage_t prod_usage, + gralloc1_consumer_usage_t cons_usage, unsigned int *ion_heap_id, + unsigned int *alloc_type, unsigned int *ion_flags) { + unsigned int heap_id = 0; + unsigned int type = 0; + int flags = 0; + if (prod_usage & GRALLOC1_PRODUCER_USAGE_PROTECTED) { + if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY) { + heap_id = ION_HEAP(SD_HEAP_ID); + /* + * There is currently no flag in ION for Secure Display + * VM. Please add it to the define once available. + */ + flags |= ION_SD_FLAGS; + } else { + heap_id = ION_HEAP(CP_HEAP_ID); + flags |= ION_CP_FLAGS; + } + } else if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_MM_HEAP) { + // MM Heap is exclusively a secure heap. + // If it is used for non secure cases, fallback to IOMMU heap + ALOGW("MM_HEAP cannot be used as an insecure heap. Using system heap instead!!"); + heap_id |= ION_HEAP(ION_SYSTEM_HEAP_ID); + } + + if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_CAMERA_HEAP) { + heap_id |= ION_HEAP(ION_CAMERA_HEAP_ID); + } + + if (prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_ADSP_HEAP) { + heap_id |= ION_HEAP(ION_ADSP_HEAP_ID); + } + + if (flags & ION_SECURE) { + type |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER; + } + + // if no ion heap flags are set, default to system heap + if (!heap_id) { + heap_id = ION_HEAP(ION_SYSTEM_HEAP_ID); + } + + *alloc_type = type; + *ion_flags = (unsigned int)flags; + *ion_heap_id = heap_id; + + return; +} + +bool Allocator::IsUBwcEnabled(int format, gralloc1_producer_usage_t prod_usage, + gralloc1_consumer_usage_t cons_usage) { + // Allow UBWC, if client is using an explicitly defined UBWC pixel format. + if (IsUBwcFormat(format)) { + return true; + } + + // Allow UBWC, if an OpenGL client sets UBWC usage flag and GPU plus MDP + // support the format. OR if a non-OpenGL client like Rotator, sets UBWC + // usage flag and MDP supports the format. + if ((prod_usage & GRALLOC1_PRODUCER_USAGE_PRIVATE_ALLOC_UBWC) && IsUBwcSupported(format)) { + bool enable = true; + // Query GPU for UBWC only if buffer is intended to be used by GPU. + if ((cons_usage & GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE) || + (prod_usage & GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET)) { + enable = adreno_helper_->IsUBWCSupportedByGPU(format); + } + + // Allow UBWC, only if CPU usage flags are not set + if (enable && !(CpuCanAccess(prod_usage, cons_usage))) { + return true; + } + } + + return false; +} + +void Allocator::GetYuvUBwcWidthAndHeight(int width, int height, int format, unsigned int *aligned_w, + unsigned int *aligned_h) { + switch (format) { + case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: + case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: + case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC: + *aligned_w = VENUS_Y_STRIDE(COLOR_FMT_NV12_UBWC, width); + *aligned_h = VENUS_Y_SCANLINES(COLOR_FMT_NV12_UBWC, height); + break; + default: + ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format); + *aligned_w = 0; + *aligned_h = 0; + break; + } +} + +void Allocator::GetRgbUBwcBlockSize(uint32_t bpp, int *block_width, int *block_height) { + *block_width = 0; + *block_height = 0; + + switch (bpp) { + case 2: + case 4: + *block_width = 16; + *block_height = 4; + break; + case 8: + *block_width = 8; + *block_height = 4; + break; + case 16: + *block_width = 4; + *block_height = 4; + break; + default: + ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp); + break; + } +} + +unsigned int Allocator::GetRgbUBwcMetaBufferSize(int width, int height, uint32_t bpp) { + unsigned int size = 0; + int meta_width, meta_height; + int block_width, block_height; + + GetRgbUBwcBlockSize(bpp, &block_width, &block_height); + if (!block_width || !block_height) { + ALOGE("%s: Unsupported bpp: %d", __FUNCTION__, bpp); + return size; + } + + // Align meta buffer height to 16 blocks + meta_height = ALIGN(((height + block_height - 1) / block_height), 16); + + // Align meta buffer width to 64 blocks + meta_width = ALIGN(((width + block_width - 1) / block_width), 64); + + // Align meta buffer size to 4K + size = (unsigned int)ALIGN((meta_width * meta_height), 4096); + + return size; +} + +unsigned int Allocator::GetUBwcSize(int width, int height, int format, unsigned int alignedw, + unsigned int alignedh) { + unsigned int size = 0; + uint32_t bpp = 0; + switch (format) { + case HAL_PIXEL_FORMAT_BGR_565: + case HAL_PIXEL_FORMAT_RGBA_8888: + case HAL_PIXEL_FORMAT_RGBX_8888: + case HAL_PIXEL_FORMAT_RGBA_1010102: + case HAL_PIXEL_FORMAT_RGBX_1010102: + bpp = GetBppForUncompressedRGB(format); + size = alignedw * alignedh * bpp; + size += GetRgbUBwcMetaBufferSize(width, height, bpp); + break; + case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: + case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: + case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC: + size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_UBWC, width, height); + break; + case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC: + size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12_BPP10_UBWC, width, height); + break; + default: + ALOGE("%s: Unsupported pixel format: 0x%x", __FUNCTION__, format); + break; + } + + return size; +} + +int Allocator::GetRgbDataAddress(private_handle_t *hnd, void **rgb_data) { + int err = 0; + + // This api is for RGB* formats + if (!gralloc1::IsUncompressedRGBFormat(hnd->format)) { + return -EINVAL; + } + + // linear buffer, nothing to do further + if (!(hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED)) { + *rgb_data = reinterpret_cast(hnd->base); + return err; + } + + unsigned int meta_size = 0; + uint32_t bpp = GetBppForUncompressedRGB(hnd->format); + switch (hnd->format) { + case HAL_PIXEL_FORMAT_BGR_565: + case HAL_PIXEL_FORMAT_RGBA_8888: + case HAL_PIXEL_FORMAT_RGBX_8888: + meta_size = GetRgbUBwcMetaBufferSize(hnd->width, hnd->height, bpp); + break; + default: + ALOGE("%s:Unsupported RGB format: 0x%x", __FUNCTION__, hnd->format); + err = -EINVAL; + break; + } + *rgb_data = reinterpret_cast(hnd->base + meta_size); + + return err; +} + +void Allocator::GetAlignedWidthAndHeight(const BufferDescriptor &descriptor, unsigned int *alignedw, + unsigned int *alignedh) { + int width = descriptor.GetWidth(); + int height = descriptor.GetHeight(); + int format = descriptor.GetFormat(); + gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage(); + gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage(); + + // Currently surface padding is only computed for RGB* surfaces. + bool ubwc_enabled = IsUBwcEnabled(format, prod_usage, cons_usage); + int tile = ubwc_enabled || IsMacroTileEnabled(format, prod_usage, cons_usage); + + if (IsUncompressedRGBFormat(format)) { + adreno_helper_->AlignUnCompressedRGB(width, height, format, tile, alignedw, alignedh); + return; + } + + if (ubwc_enabled) { + GetYuvUBwcWidthAndHeight(width, height, format, alignedw, alignedh); + return; + } + + if (IsCompressedRGBFormat(format)) { + adreno_helper_->AlignCompressedRGB(width, height, format, alignedw, alignedh); + return; + } + + int aligned_w = width; + int aligned_h = height; + unsigned int alignment = 32; + + // Below should be only YUV family + switch (format) { + case HAL_PIXEL_FORMAT_YCrCb_420_SP: + case HAL_PIXEL_FORMAT_YCbCr_420_SP: + alignment = adreno_helper_->GetGpuPixelAlignment(); + aligned_w = ALIGN(width, alignment); + break; + case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: + aligned_w = ALIGN(width, alignment); + break; + case HAL_PIXEL_FORMAT_RAW16: + aligned_w = ALIGN(width, 16); + break; + case HAL_PIXEL_FORMAT_RAW10: + aligned_w = ALIGN(width * 10 / 8, 16); + break; + case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: + aligned_w = ALIGN(width, 128); + break; + case HAL_PIXEL_FORMAT_YV12: + case HAL_PIXEL_FORMAT_YCbCr_422_SP: + case HAL_PIXEL_FORMAT_YCrCb_422_SP: + case HAL_PIXEL_FORMAT_YCbCr_422_I: + case HAL_PIXEL_FORMAT_YCrCb_422_I: + aligned_w = ALIGN(width, 16); + break; + case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: + case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: + aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV12, width)); + aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV12, height)); + break; + case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS: + aligned_w = INT(VENUS_Y_STRIDE(COLOR_FMT_NV21, width)); + aligned_h = INT(VENUS_Y_SCANLINES(COLOR_FMT_NV21, height)); + break; + case HAL_PIXEL_FORMAT_BLOB: + case HAL_PIXEL_FORMAT_RAW_OPAQUE: + break; + case HAL_PIXEL_FORMAT_NV21_ZSL: + aligned_w = ALIGN(width, 64); + aligned_h = ALIGN(height, 64); + break; + default: + break; + } + + *alignedw = (unsigned int)aligned_w; + *alignedh = (unsigned int)aligned_h; +} + +} // namespace gralloc1 diff --git a/libgralloc1/gr_allocator.h b/libgralloc1/gr_allocator.h new file mode 100644 index 000000000..583b2d7e9 --- /dev/null +++ b/libgralloc1/gr_allocator.h @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2011-2016, The Linux Foundation. All rights reserved. + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * * Neither the name of The Linux Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __GR_ALLOCATOR_H__ +#define __GR_ALLOCATOR_H__ + +#ifdef MASTER_SIDE_CP +#define SECURE_ALIGN SZ_4K +#else +#define SECURE_ALIGN SZ_1M +#endif + +#include "gralloc_priv.h" +#include "gr_buf_descriptor.h" +#include "gr_adreno_info.h" +#include "gr_ion_alloc.h" + +namespace gralloc1 { + +class Allocator { + public: + Allocator(); + ~Allocator(); + bool Init(); + int AllocateBuffer(const BufferDescriptor &descriptor, private_handle_t **pHnd); + int MapBuffer(void **base, unsigned int size, unsigned int offset, int fd); + int FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd); + int CleanBuffer(void *base, unsigned int size, unsigned int offset, int fd, int op); + int AllocateMem(AllocData *data, gralloc1_producer_usage_t prod_usage, + gralloc1_consumer_usage_t cons_usage); + bool IsMacroTileEnabled(int format, gralloc1_producer_usage_t prod_usage, + gralloc1_consumer_usage_t cons_usage); + // @return : index of the descriptor with maximum buffer size req + bool CheckForBufferSharing(uint32_t num_descriptors, const BufferDescriptor *descriptors, + int *max_index); + int GetImplDefinedFormat(gralloc1_producer_usage_t prod_usage, + gralloc1_consumer_usage_t cons_usage, int format); + unsigned int GetSize(const BufferDescriptor &d, unsigned int alignedw, unsigned int alignedh); + void GetBufferSizeAndDimensions(const BufferDescriptor &d, unsigned int *size, + unsigned int *alignedw, unsigned int *alignedh); + void GetBufferSizeAndDimensions(int width, int height, int format, unsigned int *size, + unsigned int *alignedw, unsigned int *alignedh); + void GetAlignedWidthAndHeight(const BufferDescriptor &d, unsigned int *aligned_w, + unsigned int *aligned_h); + void GetBufferAttributes(const BufferDescriptor &d, unsigned int *alignedw, + unsigned int *alignedh, int *tiled, unsigned int *size); + int GetYUVPlaneInfo(const private_handle_t *hnd, struct android_ycbcr *ycbcr); + int GetRgbDataAddress(private_handle_t *hnd, void **rgb_data); + bool UseUncached(gralloc1_producer_usage_t usage); + bool IsUBwcFormat(int format); + bool IsUBwcSupported(int format); + bool IsUBwcEnabled(int format, gralloc1_producer_usage_t prod_usage, + gralloc1_consumer_usage_t cons_usage); + + private: + void GetYuvUBwcWidthAndHeight(int width, int height, int format, unsigned int *aligned_w, + unsigned int *aligned_h); + void GetYuvSPPlaneInfo(uint64_t base, uint32_t width, uint32_t height, uint32_t bpp, + struct android_ycbcr *ycbcr); + void GetYuvUbwcSPPlaneInfo(uint64_t base, uint32_t width, uint32_t height, int color_format, + struct android_ycbcr *ycbcr); + void GetRgbUBwcBlockSize(uint32_t bpp, int *block_width, int *block_height); + unsigned int GetRgbUBwcMetaBufferSize(int width, int height, uint32_t bpp); + unsigned int GetUBwcSize(int width, int height, int format, unsigned int alignedw, + unsigned int alignedh); + void GetIonHeapInfo(gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage, + unsigned int *ion_heap_id, unsigned int *alloc_type, unsigned int *ion_flags); + + bool gpu_support_macrotile = false; + bool display_support_macrotile = false; + IonAlloc *ion_allocator_ = NULL; + AdrenoMemInfo *adreno_helper_ = NULL; +}; + +} // namespace gralloc1 + +#endif // __GR_ALLOCATOR_H__ diff --git a/libgralloc1/gr_buf_descriptor.h b/libgralloc1/gr_buf_descriptor.h new file mode 100644 index 000000000..1c3572db2 --- /dev/null +++ b/libgralloc1/gr_buf_descriptor.h @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2016, The Linux Foundation. All rights reserved. + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * * Neither the name of The Linux Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __GR_BUF_DESCRIPTOR_H__ +#define __GR_BUF_DESCRIPTOR_H__ + +#include + +#define BUF_DESCRIPTOR(exp) reinterpret_cast(exp) + +class BufferDescriptor { + public: + BufferDescriptor() {} + + BufferDescriptor(int w, int h, int f) + : width_(w), + height_(h), + format_(f), + producer_usage_(GRALLOC1_PRODUCER_USAGE_NONE), + consumer_usage_(GRALLOC1_CONSUMER_USAGE_NONE) {} + + BufferDescriptor(int w, int h, int f, gralloc1_producer_usage_t prod_usage, + gralloc1_consumer_usage_t cons_usage) + : width_(w), + height_(h), + format_(f), + producer_usage_(prod_usage), + consumer_usage_(cons_usage) {} + + bool IsValid() { return (magic == kMagic); } + + void SetConsumerUsage(gralloc1_consumer_usage_t usage) { consumer_usage_ = usage; } + + void SetProducerUsage(gralloc1_producer_usage_t usage) { producer_usage_ = usage; } + + void SetDimensions(int w, int h) { + width_ = w; + height_ = h; + } + + void SetColorFormat(int format) { format_ = format; } + + gralloc1_consumer_usage_t GetConsumerUsage() const { return consumer_usage_; } + + gralloc1_producer_usage_t GetProducerUsage() const { return producer_usage_; } + + int GetWidth() const { return width_; } + + int GetHeight() const { return height_; } + + int GetFormat() const { return format_; } + + private: + static const int kMagic = 'gr1d'; + + int magic = kMagic; + int width_ = -1; + int height_ = -1; + int format_ = -1; + gralloc1_producer_usage_t producer_usage_ = GRALLOC1_PRODUCER_USAGE_NONE; + gralloc1_consumer_usage_t consumer_usage_ = GRALLOC1_CONSUMER_USAGE_NONE; +}; + +#endif // __GR_BUF_DESCRIPTOR_H__ diff --git a/libgralloc1/gr_buf_mgr.cpp b/libgralloc1/gr_buf_mgr.cpp new file mode 100644 index 000000000..33abd4012 --- /dev/null +++ b/libgralloc1/gr_buf_mgr.cpp @@ -0,0 +1,641 @@ +/* + * Copyright (c) 2011-2016 The Linux Foundation. All rights reserved. + * Not a Contribution + * + * Copyright (C) 2010 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 + +#include "qd_utils.h" +#include "gr_priv_handle.h" +#include "gr_buf_descriptor.h" +#include "gr_utils.h" +#include "gr_buf_mgr.h" +#include "qdMetaData.h" + +namespace gralloc1 { + +BufferManager::BufferManager() { + char property[PROPERTY_VALUE_MAX]; + + // Map framebuffer memory + if ((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) && + (!strncmp(property, "1", PROPERTY_VALUE_MAX) || + (!strncasecmp(property, "true", PROPERTY_VALUE_MAX)))) { + map_fb_mem_ = true; + } + + // Enable UBWC for framebuffer + if ((property_get("debug.gralloc.enable_fb_ubwc", property, NULL) > 0) && + (!strncmp(property, "1", PROPERTY_VALUE_MAX) || + (!strncasecmp(property, "true", PROPERTY_VALUE_MAX)))) { + ubwc_for_fb_ = true; + } + + handles_map_.clear(); +} + +BufferManager::~BufferManager() { + if (allocator_) { + delete allocator_; + } +} + +bool BufferManager::Init() { + allocator_ = new Allocator(); + + return allocator_->Init(); +} + +gralloc1_error_t BufferManager::AllocateBuffers(uint32_t num_descriptors, + const BufferDescriptor *descriptors, + buffer_handle_t *out_buffers) { + bool shared = true; + gralloc1_error_t status = GRALLOC1_ERROR_NONE; + + // since GRALLOC1_CAPABILITY_TEST_ALLOCATE capability is supported + // client can ask to test the allocation by passing NULL out_buffers + bool test_allocate = !out_buffers; + + // Check if input descriptors can be supported AND + // Find out if a single buffer can be shared for all the given input descriptors + uint32_t i = 0; + int max_buf_index = -1; + shared = allocator_->CheckForBufferSharing(num_descriptors, descriptors, &max_buf_index); + + if (test_allocate) { + status = shared ? GRALLOC1_ERROR_NOT_SHARED : status; + return status; + } + + if (shared && (max_buf_index >= 0)) { + // Allocate one and duplicate/copy the handles for each descriptor + if (AllocateBuffer(descriptors[max_buf_index], &out_buffers[max_buf_index])) { + return GRALLOC1_ERROR_NO_RESOURCES; + } + + for (i = 0; i < num_descriptors; i++) { + // Create new handle for a given descriptor. + // Current assumption is even MetaData memory would be same + // Need to revisit if there is a need for own metadata memory + if (i != UINT(max_buf_index)) { + CreateSharedHandle(out_buffers[max_buf_index], descriptors[i], &out_buffers[i]); + + // since we just created handle out of existing handle add it to map + locker_.lock(); + handles_map_.insert(std::pair( + reinterpret_cast(out_buffers[i]), 1)); + locker_.unlock(); + } + } + } else { + // Buffer sharing is not feasible. + // Allocate seperate buffer for each descriptor + for (i = 0; i < num_descriptors; i++) { + if (AllocateBuffer(descriptors[i], &out_buffers[i])) { + return GRALLOC1_ERROR_NO_RESOURCES; + } + } + } + + // Allocation is successful. If backstore is not shared inform the client. + if (!shared) { + return GRALLOC1_ERROR_NOT_SHARED; + } + + return status; +} + +void BufferManager::CreateSharedHandle(buffer_handle_t inbuffer, const BufferDescriptor &descriptor, + buffer_handle_t *outbuffer) { + private_handle_t const *input = reinterpret_cast(inbuffer); + + // Get Buffer attributes or dimension + unsigned int alignedw = 0, alignedh = 0; + allocator_->GetAlignedWidthAndHeight(descriptor, &alignedw, &alignedh); + + // create new handle from input reference handle and given descriptor + int flags = GetHandleFlags(descriptor.GetFormat(), descriptor.GetProducerUsage(), + descriptor.GetConsumerUsage()); + int buffer_type = GetBufferType(descriptor.GetFormat()); + + // Duplicate the fds + private_handle_t *out_hnd = new private_handle_t( + dup(input->fd), input->size, flags, buffer_type, descriptor.GetFormat(), INT(alignedw), + INT(alignedh), dup(input->fd_metadata), input->offset_metadata, input->base_metadata, + descriptor.GetWidth(), descriptor.GetHeight(), descriptor.GetProducerUsage(), + descriptor.GetConsumerUsage()); + + *outbuffer = out_hnd; +} + +gralloc1_error_t BufferManager::FreeBuffer(private_handle_t const *hnd) { + if (allocator_->FreeBuffer(reinterpret_cast(hnd->base), hnd->size, hnd->offset, + hnd->fd) != 0) { + return GRALLOC1_ERROR_BAD_HANDLE; + } + + unsigned int meta_size = ALIGN((unsigned int)sizeof(MetaData_t), PAGE_SIZE); + if (allocator_->FreeBuffer(reinterpret_cast(hnd->base_metadata), meta_size, + hnd->offset_metadata, hnd->fd_metadata) != 0) { + return GRALLOC1_ERROR_BAD_HANDLE; + } + + // delete handle also + private_handle_t *handle = const_cast(hnd); + delete handle; + + return GRALLOC1_ERROR_NONE; +} + +gralloc1_error_t BufferManager::MapBuffer(private_handle_t const *handle) { + private_handle_t *hnd = const_cast(handle); + + hnd->base = 0; + hnd->base_metadata = 0; + if (allocator_->MapBuffer(reinterpret_cast(&hnd->base), hnd->size, hnd->offset, + hnd->fd) != 0) { + return GRALLOC1_ERROR_BAD_HANDLE; + } + + unsigned int size = ALIGN((unsigned int)sizeof(MetaData_t), PAGE_SIZE); + if (allocator_->MapBuffer(reinterpret_cast(&hnd->base_metadata), size, + hnd->offset_metadata, hnd->fd_metadata) != 0) { + return GRALLOC1_ERROR_BAD_HANDLE; + } + + return GRALLOC1_ERROR_NONE; +} + +gralloc1_error_t BufferManager::RetainBuffer(private_handle_t const *hnd) { + locker_.lock(); + + // find if this handle is already in map + auto it = handles_map_.find(hnd); + if (it != handles_map_.end()) { + // It's already in map, Just increment refcnt + // No need to mmap the memory. + it->second = it->second + 1; + } else { + // not present in the map. mmap and then add entry to map + if (MapBuffer(hnd) == GRALLOC1_ERROR_NONE) { + handles_map_.insert(std::pair(hnd, 1)); + } + } + + locker_.unlock(); + return GRALLOC1_ERROR_NONE; +} + +gralloc1_error_t BufferManager::ReleaseBuffer(private_handle_t const *hnd) { + locker_.lock(); + + // find if this handle is already in map + auto it = handles_map_.find(hnd); + if (it == handles_map_.end()) { + // Corrupt handle or map. + locker_.unlock(); + return GRALLOC1_ERROR_BAD_HANDLE; + } else { + it->second = it->second - 1; + } + + if (!it->second) { + handles_map_.erase(it); + FreeBuffer(hnd); + } + + locker_.unlock(); + return GRALLOC1_ERROR_NONE; +} + +gralloc1_error_t BufferManager::LockBuffer(const private_handle_t *hnd, + gralloc1_producer_usage_t prod_usage, + gralloc1_consumer_usage_t cons_usage) { + gralloc1_error_t err = GRALLOC1_ERROR_NONE; + + // If buffer is not meant for CPU return err + if (!CpuCanAccess(prod_usage, cons_usage)) { + return GRALLOC1_ERROR_BAD_VALUE; + } + + if (hnd->base == 0) { + // we need to map for real + locker_.lock(); + err = MapBuffer(hnd); + locker_.unlock(); + } + + // Invalidate if CPU reads in software and there are non-CPU + // writers. No need to do this for the metadata buffer as it is + // only read/written in software. + if (!err && (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) && + (hnd->flags & private_handle_t::PRIV_FLAGS_CACHED)) { + if (allocator_->CleanBuffer(reinterpret_cast(hnd->base), hnd->size, hnd->offset, + hnd->fd, CACHE_INVALIDATE)) { + return GRALLOC1_ERROR_BAD_HANDLE; + } + } + + // Mark the buffer to be flushed after CPU write. + if (!err && CpuCanWrite(prod_usage)) { + private_handle_t *handle = const_cast(hnd); + handle->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH; + } + + return err; +} + +gralloc1_error_t BufferManager::UnlockBuffer(const private_handle_t *handle) { + gralloc1_error_t status = GRALLOC1_ERROR_NONE; + + locker_.lock(); + private_handle_t *hnd = const_cast(handle); + + if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) { + if (allocator_->CleanBuffer(reinterpret_cast(hnd->base), hnd->size, hnd->offset, + hnd->fd, CACHE_CLEAN) != 0) { + status = GRALLOC1_ERROR_BAD_HANDLE; + } + hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH; + } + + locker_.unlock(); + return status; +} + +int BufferManager::GetDataAlignment(int format, gralloc1_producer_usage_t prod_usage, + gralloc1_consumer_usage_t cons_usage) { + int align = getpagesize(); + if (format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) { + align = 8192; + } + + if (prod_usage & GRALLOC1_PRODUCER_USAGE_PROTECTED) { + if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY) { + // The alignment here reflects qsee mmu V7L/V8L requirement + align = SZ_2M; + } else { + align = SECURE_ALIGN; + } + } + + return align; +} + +int BufferManager::GetHandleFlags(int format, gralloc1_producer_usage_t prod_usage, + gralloc1_consumer_usage_t cons_usage) { + int flags = 0; + if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_EXTERNAL_ONLY) { + flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY; + } + + if (cons_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_INTERNAL_ONLY) { + flags |= private_handle_t::PRIV_FLAGS_INTERNAL_ONLY; + } + + if (cons_usage & GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER) { + flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER; + } + + if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) { + flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE; + } + + if (prod_usage & GRALLOC1_CONSUMER_USAGE_CAMERA) { + flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ; + } + + if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) { + flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER; + } + + if (prod_usage & GRALLOC1_CONSUMER_USAGE_GPU_TEXTURE) { + flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE; + } + + if (prod_usage & GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY) { + flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY; + } + + if (allocator_->IsMacroTileEnabled(format, prod_usage, cons_usage)) { + flags |= private_handle_t::PRIV_FLAGS_TILE_RENDERED; + } + + if (allocator_->IsUBwcEnabled(format, prod_usage, cons_usage)) { + flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED; + } + + if (prod_usage & (GRALLOC1_PRODUCER_USAGE_CPU_READ | GRALLOC1_PRODUCER_USAGE_CPU_WRITE)) { + flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED; + } + + // TODO(user): is this correct??? + if ((cons_usage & + (GRALLOC1_CONSUMER_USAGE_VIDEO_ENCODER | GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET)) || + (prod_usage & (GRALLOC1_PRODUCER_USAGE_CAMERA | GRALLOC1_PRODUCER_USAGE_GPU_RENDER_TARGET))) { + flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER; + } + + if (cons_usage & GRALLOC1_CONSUMER_USAGE_HWCOMPOSER) { + flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER; + } + + if (!allocator_->UseUncached(prod_usage)) { + flags |= private_handle_t::PRIV_FLAGS_CACHED; + } + + return flags; +} + +int BufferManager::AllocateBuffer(unsigned int size, int aligned_w, int aligned_h, int real_w, + int real_h, int format, int bufferType, + gralloc1_producer_usage_t prod_usage, + gralloc1_consumer_usage_t cons_usage, buffer_handle_t *handle) { + int err = 0; + int flags = 0; + size = ALIGN(size, PAGE_SIZE); + AllocData data; + data.align = (unsigned int)GetDataAlignment(format, prod_usage, cons_usage); + size = ALIGN(size, data.align); + data.size = size; + data.handle = (uintptr_t)handle; + + // Allocate memory + data.uncached = allocator_->UseUncached(prod_usage); + err = allocator_->AllocateMem(&data, prod_usage, cons_usage); + if (err) { + ALOGE("gralloc failed to allocate err=%s", strerror(-err)); + *handle = 0; + return err; + } + + // allocate memory for MetaData + AllocData e_data; + e_data.size = ALIGN((unsigned int)sizeof(MetaData_t), PAGE_SIZE); + e_data.handle = data.handle; + e_data.align = (unsigned int)getpagesize(); + + ColorSpace_t colorSpace = ITU_R_601; + if (prod_usage & GRALLOC1_PRODUCER_USAGE_CAMERA) { + colorSpace = ITU_R_601_FR; + } + + err = + allocator_->AllocateMem(&e_data, GRALLOC1_PRODUCER_USAGE_NONE, GRALLOC1_CONSUMER_USAGE_NONE); + ALOGE_IF(err, "gralloc failed for e_daata error=%s", strerror(-err)); + + flags = GetHandleFlags(format, prod_usage, cons_usage); + flags |= data.alloc_type; + + // Create handle + uint64_t eBaseAddr = (uint64_t)(e_data.base) + e_data.offset; + private_handle_t *hnd = new private_handle_t(data.fd, size, flags, bufferType, format, aligned_w, + aligned_h, e_data.fd, e_data.offset, eBaseAddr, + real_w, real_h, prod_usage, cons_usage); + + hnd->offset = data.offset; + hnd->base = (uint64_t)(data.base) + data.offset; + hnd->gpuaddr = 0; + + setMetaData(hnd, UPDATE_COLOR_SPACE, reinterpret_cast(&colorSpace)); + + *handle = hnd; + + // we have just allocated the buffer & mmapped. Add to map + locker_.lock(); + handles_map_.insert(std::pair(hnd, 1)); + locker_.unlock(); + + return err; +} + +int BufferManager::GetBufferType(int inputFormat) { + int buffer_type = BUFFER_TYPE_VIDEO; + if (IsUncompressedRGBFormat(inputFormat)) { + // RGB formats + buffer_type = BUFFER_TYPE_UI; + } + + return buffer_type; +} + +int BufferManager::AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle, + unsigned int bufferSize) { + if (!handle) + return -EINVAL; + + int format = descriptor.GetFormat(); + gralloc1_producer_usage_t prod_usage = descriptor.GetProducerUsage(); + gralloc1_consumer_usage_t cons_usage = descriptor.GetConsumerUsage(); + + // Get implementation defined format + int gralloc_format = allocator_->GetImplDefinedFormat(prod_usage, cons_usage, format); + + bool use_fb_mem = false; + if ((cons_usage & GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET) && map_fb_mem_) { + use_fb_mem = true; + } + + if ((cons_usage & GRALLOC1_CONSUMER_USAGE_CLIENT_TARGET) && ubwc_for_fb_) { + prod_usage = + (gralloc1_producer_usage_t)(prod_usage | GRALLOC1_PRODUCER_USAGE_PRIVATE_ALLOC_UBWC); + } + + unsigned int size; + unsigned int alignedw, alignedh; + int buffer_type = GetBufferType(gralloc_format); + allocator_->GetBufferSizeAndDimensions(descriptor, &size, &alignedw, &alignedh); + + size = (bufferSize >= size) ? bufferSize : size; + + int err = 0; + if (use_fb_mem) { + // TODO(user): TBD Framebuffer specific implementation in a seperate file/class + } else { + err = AllocateBuffer(size, INT(alignedw), INT(alignedh), descriptor.GetWidth(), + descriptor.GetHeight(), format, buffer_type, descriptor.GetProducerUsage(), + descriptor.GetConsumerUsage(), handle); + } + + if (err < 0) { + return err; + } + + return 0; +} + +gralloc1_error_t BufferManager::Perform(int operation, va_list args) { + switch (operation) { + case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER: { + int fd = va_arg(args, int); + unsigned int size = va_arg(args, unsigned int); + unsigned int offset = va_arg(args, unsigned int); + void *base = va_arg(args, void *); + int width = va_arg(args, int); + int height = va_arg(args, int); + int format = va_arg(args, int); + + native_handle_t **handle = va_arg(args, native_handle_t **); + private_handle_t *hnd = reinterpret_cast( + native_handle_create(private_handle_t::kNumFds, private_handle_t::NumInts())); + if (hnd) { + hnd->magic = private_handle_t::kMagic; + hnd->fd = fd; + hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION; + hnd->size = size; + hnd->offset = offset; + hnd->base = uint64_t(base) + offset; + hnd->gpuaddr = 0; + hnd->width = width; + hnd->height = height; + hnd->format = format; + *handle = reinterpret_cast(hnd); + } + } break; + + case GRALLOC_MODULE_PERFORM_GET_STRIDE: { + int width = va_arg(args, int); + int format = va_arg(args, int); + int *stride = va_arg(args, int *); + unsigned int alignedw = 0, alignedh = 0; + BufferDescriptor descriptor(width, width, format); + allocator_->GetAlignedWidthAndHeight(descriptor, &alignedw, &alignedh); + *stride = INT(alignedw); + } break; + + case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE: { + private_handle_t *hnd = va_arg(args, private_handle_t *); + int *stride = va_arg(args, int *); + if (private_handle_t::validate(hnd) != 0) { + return GRALLOC1_ERROR_BAD_HANDLE; + } + + MetaData_t *metadata = reinterpret_cast(hnd->base_metadata); + if (metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) { + *stride = metadata->bufferDim.sliceWidth; + } else { + *stride = hnd->width; + } + } break; + + // TODO(user) : this alone should be sufficient, ask gfx to get rid of above + case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE: { + private_handle_t *hnd = va_arg(args, private_handle_t *); + int *stride = va_arg(args, int *); + int *height = va_arg(args, int *); + if (private_handle_t::validate(hnd) != 0) { + return GRALLOC1_ERROR_BAD_HANDLE; + } + + MetaData_t *metadata = reinterpret_cast(hnd->base_metadata); + if (metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) { + *stride = metadata->bufferDim.sliceWidth; + *height = metadata->bufferDim.sliceHeight; + } else { + *stride = hnd->width; + *height = hnd->height; + } + } break; + + case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES: { + // TODO(user): Usage is split now. take care of it from Gfx client. + // see if we can directly expect descriptor from gfx client. + int width = va_arg(args, int); + int height = va_arg(args, int); + int format = va_arg(args, int); + uint64_t producer_usage = va_arg(args, uint64_t); + uint64_t consumer_usage = va_arg(args, uint64_t); + gralloc1_producer_usage_t prod_usage = static_cast(producer_usage); + gralloc1_consumer_usage_t cons_usage = static_cast(consumer_usage); + + int *aligned_width = va_arg(args, int *); + int *aligned_height = va_arg(args, int *); + int *tile_enabled = va_arg(args, int *); + unsigned int alignedw, alignedh; + BufferDescriptor descriptor(width, height, format, prod_usage, cons_usage); + *tile_enabled = allocator_->IsUBwcEnabled(format, prod_usage, cons_usage) || + allocator_->IsMacroTileEnabled(format, prod_usage, cons_usage); + + allocator_->GetAlignedWidthAndHeight(descriptor, &alignedw, &alignedh); + *aligned_width = INT(alignedw); + *aligned_height = INT(alignedh); + } break; + + case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE: { + private_handle_t *hnd = va_arg(args, private_handle_t *); + int *color_space = va_arg(args, int *); + if (private_handle_t::validate(hnd) != 0) { + return GRALLOC1_ERROR_BAD_HANDLE; + } + MetaData_t *metadata = reinterpret_cast(hnd->base_metadata); + if (metadata && metadata->operation & UPDATE_COLOR_SPACE) { + *color_space = metadata->colorSpace; + } + } break; + case GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO: { + private_handle_t *hnd = va_arg(args, private_handle_t *); + android_ycbcr *ycbcr = va_arg(args, struct android_ycbcr *); + if (private_handle_t::validate(hnd) != 0) { + return GRALLOC1_ERROR_BAD_HANDLE; + } + if (allocator_->GetYUVPlaneInfo(hnd, ycbcr)) { + return GRALLOC1_ERROR_UNDEFINED; + } + } break; + + case GRALLOC_MODULE_PERFORM_GET_MAP_SECURE_BUFFER_INFO: { + private_handle_t *hnd = va_arg(args, private_handle_t *); + int *map_secure_buffer = va_arg(args, int *); + if (private_handle_t::validate(hnd) != 0) { + return GRALLOC1_ERROR_BAD_HANDLE; + } + MetaData_t *metadata = reinterpret_cast(hnd->base_metadata); + if (metadata && metadata->operation & MAP_SECURE_BUFFER) { + *map_secure_buffer = metadata->mapSecureBuffer; + } else { + *map_secure_buffer = 0; + } + } break; + + case GRALLOC_MODULE_PERFORM_GET_UBWC_FLAG: { + private_handle_t *hnd = va_arg(args, private_handle_t *); + int *flag = va_arg(args, int *); + if (private_handle_t::validate(hnd) != 0) { + return GRALLOC1_ERROR_BAD_HANDLE; + } + *flag = hnd->flags &private_handle_t::PRIV_FLAGS_UBWC_ALIGNED; + } break; + + case GRALLOC_MODULE_PERFORM_GET_RGB_DATA_ADDRESS: { + private_handle_t *hnd = va_arg(args, private_handle_t *); + void **rgb_data = va_arg(args, void **); + if (private_handle_t::validate(hnd) != 0) { + return GRALLOC1_ERROR_BAD_HANDLE; + } + if (allocator_->GetRgbDataAddress(hnd, rgb_data)) { + return GRALLOC1_ERROR_UNDEFINED; + } + } break; + + default: + break; + } + + return GRALLOC1_ERROR_NONE; +} + +} // namespace gralloc1 diff --git a/libgralloc1/gr_buf_mgr.h b/libgralloc1/gr_buf_mgr.h new file mode 100644 index 000000000..d3c0e675c --- /dev/null +++ b/libgralloc1/gr_buf_mgr.h @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2011-2016, The Linux Foundation. All rights reserved. + * Not a Contribution + * + * 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_BUF_MGR_H__ +#define __GR_BUF_MGR_H__ + +#include +#include +#include + +#include "gralloc_priv.h" +#include "gr_allocator.h" + +namespace gralloc1 { + +class BufferManager { + public: + BufferManager(); + ~BufferManager(); + bool Init(); + gralloc1_error_t AllocateBuffers(uint32_t numDescriptors, const BufferDescriptor *descriptors, + buffer_handle_t *outBuffers); + gralloc1_error_t RetainBuffer(private_handle_t const *hnd); + gralloc1_error_t ReleaseBuffer(private_handle_t const *hnd); + gralloc1_error_t LockBuffer(const private_handle_t *hnd, gralloc1_producer_usage_t prod_usage, + gralloc1_consumer_usage_t cons_usage); + gralloc1_error_t UnlockBuffer(const private_handle_t *hnd); + gralloc1_error_t Perform(int operation, va_list args); + + private: + gralloc1_error_t MapBuffer(private_handle_t const *hnd); + gralloc1_error_t FreeBuffer(private_handle_t const *hnd); + int GetBufferType(int format); + int AllocateBuffer(const BufferDescriptor &descriptor, buffer_handle_t *handle, + unsigned int bufferSize = 0); + int AllocateBuffer(unsigned int size, int aligned_w, int aligned_h, int real_w, int real_h, + int format, int bufferType, gralloc1_producer_usage_t prod_usage, + gralloc1_consumer_usage_t cons_usage, buffer_handle_t *handle); + int GetDataAlignment(int format, gralloc1_producer_usage_t prod_usage, + gralloc1_consumer_usage_t cons_usage); + int GetHandleFlags(int format, gralloc1_producer_usage_t prod_usage, + gralloc1_consumer_usage_t cons_usage); + void CreateSharedHandle(buffer_handle_t inbuffer, const BufferDescriptor &descriptor, + buffer_handle_t *out_buffer); + + bool map_fb_mem_ = false; + bool ubwc_for_fb_ = false; + Allocator *allocator_ = NULL; + std::mutex locker_; + std::unordered_map handles_map_ = {}; +}; + +} // namespace gralloc1 + +#endif // __GR_BUF_MGR_H__ diff --git a/libgralloc1/gr_device_impl.cpp b/libgralloc1/gr_device_impl.cpp new file mode 100644 index 000000000..62589413d --- /dev/null +++ b/libgralloc1/gr_device_impl.cpp @@ -0,0 +1,458 @@ +/* + * Copyright (c) 2016, The Linux Foundation. All rights reserved. + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * * Neither the name of The Linux Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include + +#include "gr_device_impl.h" +#include "gr_buf_descriptor.h" +#include "gralloc_priv.h" +#include "qd_utils.h" +#include "qdMetaData.h" +#include "gr_utils.h" + +int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device); + +int gralloc_device_close(struct hw_device_t *device); + +static struct hw_module_methods_t gralloc_module_methods = {.open = gralloc_device_open}; + +struct hw_module_t gralloc_module = {}; + +struct private_module_t HAL_MODULE_INFO_SYM = { + .base = { + .tag = HARDWARE_MODULE_TAG, + .version_major = 1, + .version_minor = 0, + .id = GRALLOC_HARDWARE_MODULE_ID, + .name = "Graphics Memory Module", + .author = "Code Aurora Forum", + .methods = &gralloc_module_methods, + .dso = 0, + .reserved = {0}, + }, +}; + +int gralloc_device_open(const struct hw_module_t *module, const char *name, hw_device_t **device) { + int status = -EINVAL; + if (!strcmp(name, GRALLOC_HARDWARE_MODULE_ID)) { + const private_module_t *m = reinterpret_cast(module); + gralloc1::GrallocImpl * /*gralloc1_device_t*/ dev = new gralloc1::GrallocImpl(m); + *device = reinterpret_cast(dev); + + if (dev->Init()) { + status = 0; + } else { + ALOGE(" Error in opening gralloc1 device"); + return status; + } + } + + return status; +} + +namespace gralloc1 { + +GrallocImpl::GrallocImpl(const private_module_t *module) { + common.tag = HARDWARE_DEVICE_TAG; + common.version = 1; // TODO(user): cross check version + common.module = const_cast(&module->base); + common.close = CloseDevice; + getFunction = GetFunction; + getCapabilities = GetCapabilities; +} + +bool GrallocImpl::Init() { + buf_mgr_ = new BufferManager(); + + return buf_mgr_->Init(); +} + +GrallocImpl::~GrallocImpl() { + if (buf_mgr_) { + delete buf_mgr_; + } +} + +int GrallocImpl::CloseDevice(hw_device_t *device) { + GrallocImpl *impl = reinterpret_cast(device); + delete impl; + + return 0; +} + +void GrallocImpl::GetCapabilities(struct gralloc1_device *device, uint32_t *out_count, + int32_t /*gralloc1_capability_t*/ *out_capabilities) { + if (!device) { + // Need to plan for adding more capabilities + if (out_capabilities == NULL) { + *out_count = 1; + } else { + *out_capabilities = GRALLOC1_CAPABILITY_TEST_ALLOCATE; + } + } + + return; +} + +gralloc1_function_pointer_t GrallocImpl::GetFunction(gralloc1_device_t *device, int32_t function) { + if (!device) { + return NULL; + } + + switch (function) { + case GRALLOC1_FUNCTION_CREATE_DESCRIPTOR: + return reinterpret_cast(CreateBufferDescriptor); + case GRALLOC1_FUNCTION_DESTROY_DESCRIPTOR: + return reinterpret_cast(DestroyBufferDescriptor); + case GRALLOC1_FUNCTION_SET_CONSUMER_USAGE: + return reinterpret_cast(SetConsumerUsage); + case GRALLOC1_FUNCTION_SET_DIMENSIONS: + return reinterpret_cast(SetBufferDimensions); + case GRALLOC1_FUNCTION_SET_FORMAT: + return reinterpret_cast(SetColorFormat); + case GRALLOC1_FUNCTION_SET_PRODUCER_USAGE: + return reinterpret_cast(SetProducerUsage); + case GRALLOC1_FUNCTION_GET_BACKING_STORE: + return reinterpret_cast(GetBackingStore); + case GRALLOC1_FUNCTION_GET_CONSUMER_USAGE: + return reinterpret_cast(GetConsumerUsage); + case GRALLOC1_FUNCTION_GET_DIMENSIONS: + return reinterpret_cast(GetBufferDimensions); + case GRALLOC1_FUNCTION_GET_FORMAT: + return reinterpret_cast(GetColorFormat); + case GRALLOC1_FUNCTION_GET_PRODUCER_USAGE: + return reinterpret_cast(GetProducerUsage); + case GRALLOC1_FUNCTION_GET_STRIDE: + return reinterpret_cast(GetBufferStride); + case GRALLOC1_FUNCTION_ALLOCATE: + return reinterpret_cast(AllocateBuffers); + case GRALLOC1_FUNCTION_RETAIN: + return reinterpret_cast(RetainBuffer); + case GRALLOC1_FUNCTION_RELEASE: + return reinterpret_cast(ReleaseBuffer); + /* TODO(user) :definition of flex plane is not known yet + * Need to implement after clarification from Google. + * case GRALLOC1_FUNCTION_GET_NUM_FLEX_PLANES: + return reinterpret_cast (; */ + case GRALLOC1_FUNCTION_LOCK: + return reinterpret_cast(LockBuffer); + /* TODO(user) : LOCK_YCBCR changed to LOCK_FLEX but structure is not known yet. + * Need to implement after clarification from Google. + case GRALLOC1_PFN_LOCK_FLEX: + return reinterpret_cast (LockYCbCrBuffer; + */ + case GRALLOC1_FUNCTION_UNLOCK: + return reinterpret_cast(UnlockBuffer); + case GRALLOC1_FUNCTION_PERFORM: + return reinterpret_cast(Gralloc1Perform); + default: + ALOGE("%s:Gralloc Error. Client Requested for unsupported function", __FUNCTION__); + return NULL; + } + + return NULL; +} + +gralloc1_error_t GrallocImpl::CheckDeviceAndDescriptor(gralloc1_device_t *device, + gralloc1_buffer_descriptor_t descriptor) { + if (!device || !BUF_DESCRIPTOR(descriptor)->IsValid()) { + ALOGE("Gralloc Error : device=%p, descriptor=%p", (void *)device, (void *)descriptor); + return GRALLOC1_ERROR_BAD_DESCRIPTOR; + } + + return GRALLOC1_ERROR_NONE; +} + +gralloc1_error_t GrallocImpl::CheckDeviceAndHandle(gralloc1_device_t *device, + buffer_handle_t buffer) { + const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); + if (!device || (private_handle_t::validate(hnd) != 0)) { + ALOGE("Gralloc Error : device= %p, buffer-handle=%p", (void *)device, (void *)buffer); + return GRALLOC1_ERROR_BAD_HANDLE; + } + + return GRALLOC1_ERROR_NONE; +} + +gralloc1_error_t GrallocImpl::CreateBufferDescriptor(gralloc1_device_t *device, + gralloc1_buffer_descriptor_t *out_descriptor) { + if (!device) { + return GRALLOC1_ERROR_BAD_DESCRIPTOR; + } + + BufferDescriptor *descriptor = new BufferDescriptor(); + if (descriptor == NULL) { + return GRALLOC1_ERROR_NO_RESOURCES; + } + + *out_descriptor = reinterpret_cast(descriptor); + + return GRALLOC1_ERROR_NONE; +} + +gralloc1_error_t GrallocImpl::DestroyBufferDescriptor(gralloc1_device_t *device, + gralloc1_buffer_descriptor_t descriptor) { + gralloc1_error_t status = CheckDeviceAndDescriptor(device, descriptor); + if (status == GRALLOC1_ERROR_NONE) { + delete reinterpret_cast(descriptor); + } + + return status; +} + +gralloc1_error_t GrallocImpl::SetConsumerUsage(gralloc1_device_t *device, + gralloc1_buffer_descriptor_t descriptor, + gralloc1_consumer_usage_t usage) { + gralloc1_error_t status = CheckDeviceAndDescriptor(device, descriptor); + if (status == GRALLOC1_ERROR_NONE) { + BUF_DESCRIPTOR(descriptor)->SetConsumerUsage(usage); + } + + return status; +} + +gralloc1_error_t GrallocImpl::SetBufferDimensions(gralloc1_device_t *device, + gralloc1_buffer_descriptor_t descriptor, + uint32_t width, uint32_t height) { + gralloc1_error_t status = CheckDeviceAndDescriptor(device, descriptor); + if (status == GRALLOC1_ERROR_NONE) { + BUF_DESCRIPTOR(descriptor)->SetDimensions(INT(width), INT(height)); + } + + return status; +} + +gralloc1_error_t GrallocImpl::SetColorFormat(gralloc1_device_t *device, + gralloc1_buffer_descriptor_t descriptor, + int32_t format) { + gralloc1_error_t status = CheckDeviceAndDescriptor(device, descriptor); + if (status == GRALLOC1_ERROR_NONE) { + BUF_DESCRIPTOR(descriptor)->SetColorFormat(format); + } + + return status; +} + +gralloc1_error_t GrallocImpl::SetProducerUsage(gralloc1_device_t *device, + gralloc1_buffer_descriptor_t descriptor, + gralloc1_producer_usage_t usage) { + gralloc1_error_t status = CheckDeviceAndDescriptor(device, descriptor); + if (status == GRALLOC1_ERROR_NONE) { + BUF_DESCRIPTOR(descriptor)->SetProducerUsage(usage); + } + + return status; +} + +gralloc1_error_t GrallocImpl::GetBackingStore(gralloc1_device_t *device, buffer_handle_t buffer, + gralloc1_backing_store_t *out_backstore) { + if (!device || !buffer) { + return GRALLOC1_ERROR_BAD_HANDLE; + } + + *out_backstore = + static_cast(PRIV_HANDLE_CONST(buffer)->GetBackingstore()); + + return GRALLOC1_ERROR_NONE; +} + +gralloc1_error_t GrallocImpl::GetConsumerUsage(gralloc1_device_t *device, buffer_handle_t buffer, + gralloc1_consumer_usage_t *outUsage) { + gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); + if (status == GRALLOC1_ERROR_NONE) { + *outUsage = PRIV_HANDLE_CONST(buffer)->GetConsumerUsage(); + } + + return status; +} + +gralloc1_error_t GrallocImpl::GetBufferDimensions(gralloc1_device_t *device, buffer_handle_t buffer, + uint32_t *outWidth, uint32_t *outHeight) { + gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); + if (status == GRALLOC1_ERROR_NONE) { + const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); + *outWidth = UINT(hnd->GetRealWidth()); + *outHeight = UINT(hnd->GetRealHeight()); + } + + return status; +} + +gralloc1_error_t GrallocImpl::GetColorFormat(gralloc1_device_t *device, buffer_handle_t buffer, + int32_t *outFormat) { + gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); + if (status == GRALLOC1_ERROR_NONE) { + *outFormat = PRIV_HANDLE_CONST(buffer)->GetColorFormat(); + } + + return status; +} + +gralloc1_error_t GrallocImpl::GetProducerUsage(gralloc1_device_t *device, buffer_handle_t buffer, + gralloc1_producer_usage_t *outUsage) { + gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); + if (status == GRALLOC1_ERROR_NONE) { + const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); + *outUsage = hnd->GetProducerUsage(); + } + + return status; +} + +gralloc1_error_t GrallocImpl::GetBufferStride(gralloc1_device_t *device, buffer_handle_t buffer, + uint32_t *outStride) { + gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); + if (status == GRALLOC1_ERROR_NONE) { + *outStride = UINT(PRIV_HANDLE_CONST(buffer)->GetStride()); + } + + return status; +} + +gralloc1_error_t GrallocImpl::AllocateBuffers(gralloc1_device_t *device, uint32_t num_dptors, + const gralloc1_buffer_descriptor_t *dptors, + buffer_handle_t *outBuffers) { + if (!num_dptors || !dptors) { + return GRALLOC1_ERROR_BAD_DESCRIPTOR; + } + + GrallocImpl const *dev = GRALLOC_IMPL(device); + const BufferDescriptor *descriptors = reinterpret_cast(dptors); + gralloc1_error_t status = dev->buf_mgr_->AllocateBuffers(num_dptors, descriptors, outBuffers); + + return status; +} + +gralloc1_error_t GrallocImpl::RetainBuffer(gralloc1_device_t *device, buffer_handle_t buffer) { + gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); + if (status == GRALLOC1_ERROR_NONE) { + const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); + GrallocImpl const *dev = GRALLOC_IMPL(device); + status = dev->buf_mgr_->RetainBuffer(hnd); + } + + return status; +} + +gralloc1_error_t GrallocImpl::ReleaseBuffer(gralloc1_device_t *device, buffer_handle_t buffer) { + gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); + if (status == GRALLOC1_ERROR_NONE) { + const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); + GrallocImpl const *dev = GRALLOC_IMPL(device); + status = dev->buf_mgr_->ReleaseBuffer(hnd); + } + + return status; +} + +gralloc1_error_t GrallocImpl::LockBuffer(gralloc1_device_t *device, buffer_handle_t buffer, + gralloc1_producer_usage_t prod_usage, + gralloc1_consumer_usage_t cons_usage, + const gralloc1_rect_t *region, void **out_data, + int32_t acquire_fence) { + gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); + if (status == GRALLOC1_ERROR_NONE && (acquire_fence > 0)) { + int error = sync_wait(acquire_fence, 1000); + if (error < 0) { + ALOGE("%s: sync_wait timedout! error = %s", __FUNCTION__, strerror(errno)); + return GRALLOC1_ERROR_UNDEFINED; + } + } + + const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); + GrallocImpl const *dev = GRALLOC_IMPL(device); + + // Either producer usage or consumer usage must be *_USAGE_NONE + if ((prod_usage != GRALLOC1_PRODUCER_USAGE_NONE) && + (cons_usage != GRALLOC1_CONSUMER_USAGE_NONE)) { + return GRALLOC1_ERROR_BAD_VALUE; + } + + // currently we ignore the region/rect client wants to lock + if (region == NULL) { + return GRALLOC1_ERROR_BAD_VALUE; + } + + status = dev->buf_mgr_->LockBuffer(hnd, prod_usage, cons_usage); + + *out_data = reinterpret_cast(hnd->base); + + return status; +} + +/* TODO(user) : LOCK_YCBCR changed to LOCK_FLEX but structure definition is not known yet. + * Need to implement after clarification from Google. +gralloc1_error_t GrallocImpl::LockYCbCrBuffer(gralloc1_device_t* device, buffer_handle_t buffer, + gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage, + const gralloc1_rect_t* region, struct android_ycbcr* outYCbCr, int32_t* outAcquireFence) { + gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); + + if (status == GRALLOC1_ERROR_NONE) { + void **outData = 0; + status = LockBuffer(device, buffer, prod_usage, cons_usage, region, outData, outAcquireFence); + } + + if (status == GRALLOC1_ERROR_NONE) { + const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); + GrallocImpl const *dev = GRALLOC_IMPL(device); + dev->allocator_->GetYUVPlaneInfo(hnd, outYCbCr); + } + + return status; +} + */ + +gralloc1_error_t GrallocImpl::UnlockBuffer(gralloc1_device_t *device, buffer_handle_t buffer, + int32_t *release_fence) { + gralloc1_error_t status = CheckDeviceAndHandle(device, buffer); + + if (status != GRALLOC1_ERROR_NONE) { + return status; + } + + const private_handle_t *hnd = PRIV_HANDLE_CONST(buffer); + GrallocImpl const *dev = GRALLOC_IMPL(device); + + *release_fence = -1; + + return dev->buf_mgr_->UnlockBuffer(hnd); +} + +gralloc1_error_t GrallocImpl::Gralloc1Perform(gralloc1_device_t *device, int operation, ...) { + va_list args; + va_start(args, operation); + GrallocImpl const *dev = GRALLOC_IMPL(device); + gralloc1_error_t err = dev->buf_mgr_->Perform(operation, args); + va_end(args); + + return err; +} + +} // namespace gralloc1 diff --git a/libgralloc1/gr_device_impl.h b/libgralloc1/gr_device_impl.h new file mode 100644 index 000000000..4fc8e3c88 --- /dev/null +++ b/libgralloc1/gr_device_impl.h @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2016, The Linux Foundation. All rights reserved. + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * * Neither the name of The Linux Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __GR_DEVICE_IMPL_H__ +#define __GR_DEVICE_IMPL_H__ + +#include +#include +#include "gr_buf_mgr.h" + +struct private_module_t { + hw_module_t base; +}; + +#define GRALLOC_IMPL(exp) reinterpret_cast(exp) + +namespace gralloc1 { + +class GrallocImpl : public gralloc1_device_t { + public: + explicit GrallocImpl(const private_module_t *module); + ~GrallocImpl(); + bool Init(); + static int CloseDevice(hw_device_t *device); + static void GetCapabilities(struct gralloc1_device *device, uint32_t *out_count, + int32_t * /*gralloc1_capability_t*/ out_capabilities); + static gralloc1_function_pointer_t GetFunction( + struct gralloc1_device *device, int32_t /*gralloc1_function_descriptor_t*/ descriptor); + + private: + static inline gralloc1_error_t CheckDeviceAndDescriptor(gralloc1_device_t *device, + gralloc1_buffer_descriptor_t descriptor); + static inline gralloc1_error_t CheckDeviceAndHandle(gralloc1_device_t *device, + buffer_handle_t buffer); + static gralloc1_error_t CreateBufferDescriptor(gralloc1_device_t *device, + gralloc1_buffer_descriptor_t *out_descriptor); + static gralloc1_error_t DestroyBufferDescriptor(gralloc1_device_t *device, + gralloc1_buffer_descriptor_t descriptor); + static gralloc1_error_t SetConsumerUsage(gralloc1_device_t *device, + gralloc1_buffer_descriptor_t descriptor, + gralloc1_consumer_usage_t usage); + static gralloc1_error_t SetBufferDimensions(gralloc1_device_t *device, + gralloc1_buffer_descriptor_t descriptor, + uint32_t width, uint32_t height); + static gralloc1_error_t SetColorFormat(gralloc1_device_t *device, + gralloc1_buffer_descriptor_t descriptor, int32_t format); + static gralloc1_error_t SetProducerUsage(gralloc1_device_t *device, + gralloc1_buffer_descriptor_t descriptor, + gralloc1_producer_usage_t usage); + static gralloc1_error_t GetBackingStore(gralloc1_device_t *device, buffer_handle_t buffer, + gralloc1_backing_store_t *out_store); + static gralloc1_error_t GetConsumerUsage(gralloc1_device_t *device, buffer_handle_t buffer, + gralloc1_consumer_usage_t *out_usage); + static gralloc1_error_t GetBufferDimensions(gralloc1_device_t *device, buffer_handle_t buffer, + uint32_t *out_width, uint32_t *out_height); + static gralloc1_error_t GetColorFormat(gralloc1_device_t *device, buffer_handle_t descriptor, + int32_t *outFormat); + static gralloc1_error_t GetProducerUsage(gralloc1_device_t *device, buffer_handle_t buffer, + gralloc1_producer_usage_t *out_usage); + static gralloc1_error_t GetBufferStride(gralloc1_device_t *device, buffer_handle_t buffer, + uint32_t *out_stride); + static gralloc1_error_t AllocateBuffers(gralloc1_device_t *device, uint32_t num_dptors, + const gralloc1_buffer_descriptor_t *descriptors, + buffer_handle_t *out_buffers); + static gralloc1_error_t RetainBuffer(gralloc1_device_t *device, buffer_handle_t buffer); + static gralloc1_error_t ReleaseBuffer(gralloc1_device_t *device, buffer_handle_t buffer); + static gralloc1_error_t getNumFlexPlanes(gralloc1_device_t *device, buffer_handle_t buffer, + uint32_t *out_num_planes); + static gralloc1_error_t LockBuffer(gralloc1_device_t *device, buffer_handle_t buffer, + gralloc1_producer_usage_t prod_usage, + gralloc1_consumer_usage_t cons_usage, + const gralloc1_rect_t *access_region, void **out_data, + int32_t acquire_fence); + static gralloc1_error_t LockFlex(gralloc1_device_t *device, buffer_handle_t buffer, + gralloc1_producer_usage_t prod_usage, + gralloc1_consumer_usage_t cons_usage, + const gralloc1_rect_t *access_region, + struct android_flex_layout *out_flex_layout, + int32_t acquireFence); + /* TODO(user) : LOCK_YCBCR changed to LOCK_FLEX but structure is not known yet. + * Need to implement after clarification from Google. + static gralloc1_error_t LockYCbCrBuffer(gralloc1_device_t* device, buffer_handle_t buffer, + gralloc1_producer_usage_t producerUsage, gralloc1_consumer_usage_t consumerUsage, + const gralloc1_rect_t* Region, struct android_ycbcr* outYCbCr, int32_t* outAcquireFence); + */ + static gralloc1_error_t UnlockBuffer(gralloc1_device_t *device, buffer_handle_t buffer, + int32_t *release_fence); + static gralloc1_error_t Gralloc1Perform(gralloc1_device_t *device, int operation, ...); + + BufferManager *buf_mgr_ = NULL; +}; + +} // namespace gralloc1 + +#endif // __GR_DEVICE_IMPL_H__ diff --git a/libgralloc1/gr_ion_alloc.cpp b/libgralloc1/gr_ion_alloc.cpp new file mode 100644 index 000000000..ca93a632f --- /dev/null +++ b/libgralloc1/gr_ion_alloc.cpp @@ -0,0 +1,209 @@ +/* + * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved. + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * * Neither the name of The Linux Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#define DEBUG 0 +#define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL) +#include +#include +#include +#include +#include +#include +#include + +#include "gralloc_priv.h" +#include "gr_utils.h" +#include "gr_ion_alloc.h" + +namespace gralloc1 { + +bool IonAlloc::Init() { + if (ion_dev_fd_ == FD_INIT) { + ion_dev_fd_ = open(kIonDevice, O_RDONLY); + } + + if (ion_dev_fd_ < 0) { + ALOGE("%s: Failed to open ion device - %s", __FUNCTION__, strerror(errno)); + ion_dev_fd_ = FD_INIT; + return false; + } + + return true; +} + +void IonAlloc::CloseIonDevice() { + if (ion_dev_fd_ > FD_INIT) { + close(ion_dev_fd_); + } + + ion_dev_fd_ = FD_INIT; +} + +int IonAlloc::AllocBuffer(AllocData *data) { + ATRACE_CALL(); + int err = 0; + struct ion_handle_data handle_data; + struct ion_fd_data fd_data; + struct ion_allocation_data ion_alloc_data; + void *base = NULL; + + ion_alloc_data.len = data->size; + ion_alloc_data.align = data->align; + ion_alloc_data.heap_id_mask = data->heap_id; + ion_alloc_data.flags = data->flags; + ion_alloc_data.flags |= data->uncached ? 0 : ION_FLAG_CACHED; + + if (ioctl(ion_dev_fd_, INT(ION_IOC_ALLOC), &ion_alloc_data)) { + err = -errno; + ALOGE("ION_IOC_ALLOC failed with error - %s", strerror(errno)); + return err; + } + + fd_data.handle = ion_alloc_data.handle; + handle_data.handle = ion_alloc_data.handle; + if (ioctl(ion_dev_fd_, INT(ION_IOC_MAP), &fd_data)) { + err = -errno; + ALOGE("%s: ION_IOC_MAP failed with error - %s", __FUNCTION__, strerror(errno)); + ioctl(ion_dev_fd_, INT(ION_IOC_FREE), &handle_data); + return err; + } + + if (!(INT(data->flags) & INT(ION_SECURE))) { + base = mmap(0, ion_alloc_data.len, PROT_READ | PROT_WRITE, MAP_SHARED, fd_data.fd, 0); + if (base == MAP_FAILED) { + err = -errno; + ALOGE("%s: Failed to map the allocated memory: %s", __FUNCTION__, strerror(errno)); + ioctl(ion_dev_fd_, INT(ION_IOC_FREE), &handle_data); + return err; + } + } + + data->base = base; + data->fd = fd_data.fd; + ioctl(ion_dev_fd_, INT(ION_IOC_FREE), &handle_data); + ALOGD_IF(DEBUG, "ion: Allocated buffer base:%p size:%zu fd:%d", data->base, ion_alloc_data.len, + data->fd); + + return 0; +} + +int IonAlloc::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd) { + ATRACE_CALL(); + int err = 0; + ALOGD_IF(DEBUG, "ion: Freeing buffer base:%p size:%u fd:%d", base, size, fd); + + if (base) { + err = UnmapBuffer(base, size, offset); + } + close(fd); + + return err; +} + +int IonAlloc::MapBuffer(void **base, unsigned int size, unsigned int offset, int fd) { + ATRACE_CALL(); + int err = 0; + void *addr = 0; + + // It is a (quirky) requirement of ION to have opened the + // ion fd in the process that is doing the mapping + addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + *base = addr; + if (addr == MAP_FAILED) { + err = -errno; + ALOGE("ion: Failed to map memory in the client: %s", strerror(errno)); + } else { + ALOGD_IF(DEBUG, "ion: Mapped buffer base:%p size:%u offset:%u fd:%d", addr, size, offset, fd); + } + + return err; +} + +int IonAlloc::UnmapBuffer(void *base, unsigned int size, unsigned int /*offset*/) { + ATRACE_CALL(); + ALOGD_IF(DEBUG, "ion: Unmapping buffer base:%p size:%u", base, size); + + int err = 0; + if (munmap(base, size)) { + err = -errno; + ALOGE("ion: Failed to unmap memory at %p : %s", base, strerror(errno)); + } + + return err; +} + +int IonAlloc::CleanBuffer(void *base, unsigned int size, unsigned int offset, int fd, int op) { + ATRACE_CALL(); + ATRACE_INT("operation id", op); + struct ion_flush_data flush_data; + struct ion_fd_data fd_data; + struct ion_handle_data handle_data; + int err = 0; + + fd_data.fd = fd; + if (ioctl(ion_dev_fd_, INT(ION_IOC_IMPORT), &fd_data)) { + err = -errno; + ALOGE("%s: ION_IOC_IMPORT failed with error - %s", __FUNCTION__, strerror(errno)); + return err; + } + + handle_data.handle = fd_data.handle; + flush_data.handle = fd_data.handle; + flush_data.vaddr = base; + // offset and length are unsigned int + flush_data.offset = offset; + flush_data.length = size; + + struct ion_custom_data d; + switch (op) { + case CACHE_CLEAN: + d.cmd = ION_IOC_CLEAN_CACHES; + break; + case CACHE_INVALIDATE: + d.cmd = ION_IOC_INV_CACHES; + break; + case CACHE_CLEAN_AND_INVALIDATE: + default: + d.cmd = ION_IOC_CLEAN_INV_CACHES; + } + + d.arg = (unsigned long)(&flush_data); // NOLINT + if (ioctl(ion_dev_fd_, INT(ION_IOC_CUSTOM), &d)) { + err = -errno; + ALOGE("%s: ION_IOC_CLEAN_INV_CACHES failed with error - %s", __FUNCTION__, strerror(errno)); + ioctl(ion_dev_fd_, INT(ION_IOC_FREE), &handle_data); + return err; + } + + ioctl(ion_dev_fd_, INT(ION_IOC_FREE), &handle_data); + + return 0; +} + +} // namespace gralloc1 diff --git a/libgralloc1/gr_ion_alloc.h b/libgralloc1/gr_ion_alloc.h new file mode 100644 index 000000000..baef8aa8b --- /dev/null +++ b/libgralloc1/gr_ion_alloc.h @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2011-2015, The Linux Foundation. All rights reserved. + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * * Neither the name of The Linux Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __GR_ION_ALLOC_H__ +#define __GR_ION_ALLOC_H__ + +#include + +#define FD_INIT -1 + +namespace gralloc1 { + +enum { + CACHE_CLEAN = 0x1, + CACHE_INVALIDATE, + CACHE_CLEAN_AND_INVALIDATE, +}; + +struct AllocData { + void *base = NULL; + int fd = -1; + unsigned int offset = 0; + unsigned int size = 0; + unsigned int align = 1; + uintptr_t handle = 0; + bool uncached = false; + unsigned int flags = 0x0; + unsigned int heap_id = 0x0; + unsigned int alloc_type = 0x0; +}; + +class IonAlloc { + public: + IonAlloc() { ion_dev_fd_ = FD_INIT; } + + ~IonAlloc() { CloseIonDevice(); } + + bool Init(); + int AllocBuffer(AllocData *data); + int FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd); + int MapBuffer(void **base, unsigned int size, unsigned int offset, int fd); + int UnmapBuffer(void *base, unsigned int size, unsigned int offset); + int CleanBuffer(void *base, unsigned int size, unsigned int offset, int fd, int op); + + private: + const char *kIonDevice = "/dev/ion"; + + int OpenIonDevice(); + void CloseIonDevice(); + + int ion_dev_fd_; +}; + +} // namespace gralloc1 + +#endif // __GR_ION_ALLOC_H__ diff --git a/libgralloc1/gr_priv_handle.h b/libgralloc1/gr_priv_handle.h new file mode 100644 index 000000000..444fc8001 --- /dev/null +++ b/libgralloc1/gr_priv_handle.h @@ -0,0 +1,173 @@ +/* + * Copyright (c) 2011-2016, The Linux Foundation. All rights reserved. + * Not a Contribution + * + * 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_PRIV_HANDLE_H__ +#define __GR_PRIV_HANDLE_H__ + +#include +#include + +#define GRALLOC1_FUNCTION_PERFORM 0x00001000 + +#define DBG_HANDLE false + +typedef gralloc1_error_t (*GRALLOC1_PFN_PERFORM)(gralloc1_device_t *device, int operation, ...); + +typedef int BackStoreFd; + +#define PRIV_HANDLE_CONST(exp) static_cast(exp) + +struct private_handle_t : public native_handle_t { + // TODO(user): Moving PRIV_FLAGS to #defs & check for each PRIV_FLAG and remove unused. + enum { + PRIV_FLAGS_FRAMEBUFFER = 0x00000001, + PRIV_FLAGS_USES_ION = 0x00000008, + PRIV_FLAGS_USES_ASHMEM = 0x00000010, + PRIV_FLAGS_NEEDS_FLUSH = 0x00000020, + PRIV_FLAGS_INTERNAL_ONLY = 0x00000040, + PRIV_FLAGS_NON_CPU_WRITER = 0x00000080, + PRIV_FLAGS_NONCONTIGUOUS_MEM = 0x00000100, + PRIV_FLAGS_CACHED = 0x00000200, + PRIV_FLAGS_SECURE_BUFFER = 0x00000400, + PRIV_FLAGS_EXTERNAL_ONLY = 0x00002000, + PRIV_FLAGS_PROTECTED_BUFFER = 0x00004000, + PRIV_FLAGS_VIDEO_ENCODER = 0x00010000, + PRIV_FLAGS_CAMERA_WRITE = 0x00020000, + PRIV_FLAGS_CAMERA_READ = 0x00040000, + PRIV_FLAGS_HW_COMPOSER = 0x00080000, + PRIV_FLAGS_HW_TEXTURE = 0x00100000, + PRIV_FLAGS_ITU_R_601 = 0x00200000, // Unused from display + PRIV_FLAGS_ITU_R_601_FR = 0x00400000, // Unused from display + PRIV_FLAGS_ITU_R_709 = 0x00800000, // Unused from display + PRIV_FLAGS_SECURE_DISPLAY = 0x01000000, + PRIV_FLAGS_TILE_RENDERED = 0x02000000, + PRIV_FLAGS_CPU_RENDERED = 0x04000000, + PRIV_FLAGS_UBWC_ALIGNED = 0x08000000, + PRIV_FLAGS_DISP_CONSUMER = 0x10000000 + }; + + // file-descriptors + int fd; + int fd_metadata; + + // ints + int magic; + int flags; + unsigned int size; + unsigned int offset; + int buffer_type; + uint64_t base __attribute__((aligned(8))); + unsigned int offset_metadata; + + // The gpu address mapped into the mmu. + uint64_t gpuaddr __attribute__((aligned(8))); + + int format; + int width; // holds width of the actual buffer allocated + int height; // holds height of the actual buffer allocated + + int stride; + uint64_t base_metadata __attribute__((aligned(8))); + + // added for gralloc1 + int real_width; // holds width client asked to allocate + int real_height; // holds height client asked to allocate// holds width client asked to allocate + gralloc1_producer_usage_t producer_usage __attribute__((aligned(8))); + gralloc1_consumer_usage_t consumer_usage __attribute__((aligned(8))); + + static const int kNumFds = 2; + static const int kMagic = 'gmsm'; + + static inline int NumInts() { + return ((sizeof(private_handle_t) - sizeof(native_handle_t)) / sizeof(int)) - kNumFds; + } + + private_handle_t(int fd, unsigned int size, int flags, int buf_type, int format, int width, + int height, int meta_fd = -1, unsigned int meta_offset = 0, + uint64_t meta_base = 0, int rw = 0, int rh = 0, + gralloc1_producer_usage_t prod_usage = GRALLOC1_PRODUCER_USAGE_NONE, + gralloc1_consumer_usage_t cons_usage = GRALLOC1_CONSUMER_USAGE_NONE) + : fd(fd), + fd_metadata(meta_fd), + magic(kMagic), + flags(flags), + size(size), + offset(0), + buffer_type(buf_type), + base(0), + offset_metadata(meta_offset), + gpuaddr(0), + format(format), + width(width), + height(height), + base_metadata(meta_base), + real_width(rw), + real_height(rh), + producer_usage(prod_usage), + consumer_usage(cons_usage) { + version = static_cast(sizeof(native_handle)); + numInts = NumInts(); + numFds = kNumFds; + } + + ~private_handle_t() { + magic = 0; + ALOGE_IF(DBG_HANDLE, "deleting buffer handle %p", this); + } + + static int validate(const native_handle *h) { + const private_handle_t *hnd = (const private_handle_t *)h; + if (!h || h->version != sizeof(native_handle) || h->numInts != NumInts() || + h->numFds != kNumFds || hnd->magic != kMagic) { + ALOGE( + "Invalid gralloc handle (at %p): ver(%d/%zu) ints(%d/%d) fds(%d/%d) " + "magic(%c%c%c%c/%c%c%c%c)", + h, h ? h->version : -1, sizeof(native_handle), h ? h->numInts : -1, NumInts(), + h ? h->numFds : -1, kNumFds, + hnd ? (((hnd->magic >> 24) & 0xFF) ? ((hnd->magic >> 24) & 0xFF) : '-') : '?', + hnd ? (((hnd->magic >> 16) & 0xFF) ? ((hnd->magic >> 16) & 0xFF) : '-') : '?', + hnd ? (((hnd->magic >> 8) & 0xFF) ? ((hnd->magic >> 8) & 0xFF) : '-') : '?', + hnd ? (((hnd->magic >> 0) & 0xFF) ? ((hnd->magic >> 0) & 0xFF) : '-') : '?', + (kMagic >> 24) & 0xFF, (kMagic >> 16) & 0xFF, (kMagic >> 8) & 0xFF, (kMagic >> 0) & 0xFF); + return -EINVAL; + } + + return 0; + } + + int GetRealWidth() const { return real_width; } + + int GetRealHeight() const { return real_height; } + + int GetColorFormat() const { return format; } + + int GetStride() const { + // In handle we are storing aligned width after allocation. + // Why GetWidth & GetStride?? Are we supposed to maintain unaligned values?? + return width; + } + + gralloc1_consumer_usage_t GetConsumerUsage() const { return consumer_usage; } + + gralloc1_producer_usage_t GetProducerUsage() const { return producer_usage; } + + BackStoreFd GetBackingstore() const { return fd; } +}; + +#endif // __GR_PRIV_HANDLE_H__ diff --git a/libgralloc1/gr_utils.cpp b/libgralloc1/gr_utils.cpp new file mode 100644 index 000000000..98b688962 --- /dev/null +++ b/libgralloc1/gr_utils.cpp @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2011-2016, The Linux Foundation. All rights reserved. + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * * Neither the name of The Linux Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "gr_utils.h" + +namespace gralloc1 { + +bool IsUncompressedRGBFormat(int format) { + switch (format) { + case HAL_PIXEL_FORMAT_RGBA_8888: + case HAL_PIXEL_FORMAT_RGBX_8888: + case HAL_PIXEL_FORMAT_RGB_888: + case HAL_PIXEL_FORMAT_RGB_565: + case HAL_PIXEL_FORMAT_BGR_565: + case HAL_PIXEL_FORMAT_BGRA_8888: + case HAL_PIXEL_FORMAT_RGBA_5551: + case HAL_PIXEL_FORMAT_RGBA_4444: + case HAL_PIXEL_FORMAT_R_8: + case HAL_PIXEL_FORMAT_RG_88: + case HAL_PIXEL_FORMAT_BGRX_8888: + case HAL_PIXEL_FORMAT_RGBA_1010102: + case HAL_PIXEL_FORMAT_ARGB_2101010: + case HAL_PIXEL_FORMAT_RGBX_1010102: + case HAL_PIXEL_FORMAT_XRGB_2101010: + case HAL_PIXEL_FORMAT_BGRA_1010102: + case HAL_PIXEL_FORMAT_ABGR_2101010: + case HAL_PIXEL_FORMAT_BGRX_1010102: + case HAL_PIXEL_FORMAT_XBGR_2101010: + return true; + default: + break; + } + + return false; +} + +bool IsCompressedRGBFormat(int format) { + switch (format) { + case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR: + case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR: + return true; + default: + break; + } + + return false; +} + +uint32_t GetBppForUncompressedRGB(int format) { + uint32_t bpp = 0; + switch (format) { + case HAL_PIXEL_FORMAT_RGBA_8888: + case HAL_PIXEL_FORMAT_RGBX_8888: + case HAL_PIXEL_FORMAT_BGRA_8888: + case HAL_PIXEL_FORMAT_BGRX_8888: + bpp = 4; + break; + case HAL_PIXEL_FORMAT_RGB_888: + bpp = 3; + break; + case HAL_PIXEL_FORMAT_RGB_565: + case HAL_PIXEL_FORMAT_BGR_565: + case HAL_PIXEL_FORMAT_RGBA_5551: + case HAL_PIXEL_FORMAT_RGBA_4444: + bpp = 2; + break; + default: + ALOGE("Error : %s New format request", __FUNCTION__); + break; + } + + return bpp; +} + +bool CpuCanAccess(gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage) { + return CpuCanRead(prod_usage, cons_usage) || CpuCanWrite(prod_usage); +} + +bool CpuCanRead(gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage) { + if (prod_usage & GRALLOC1_PRODUCER_USAGE_CPU_READ) { + return true; + } + + if (cons_usage & GRALLOC1_CONSUMER_USAGE_CPU_READ) { + return true; + } + + return false; +} + +bool CpuCanWrite(gralloc1_producer_usage_t prod_usage) { + if (prod_usage & GRALLOC1_PRODUCER_USAGE_CPU_WRITE) { + // Application intends to use CPU for rendering + return true; + } + + return false; +} + +} // namespace gralloc1 diff --git a/libgralloc1/gr_utils.h b/libgralloc1/gr_utils.h new file mode 100644 index 000000000..20f0be5f1 --- /dev/null +++ b/libgralloc1/gr_utils.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2011-2016, The Linux Foundation. All rights reserved. + + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * * Neither the name of The Linux Foundation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __GR_UTILS_H__ +#define __GR_UTILS_H__ + +#include "gralloc_priv.h" + +#define SZ_2M 0x200000 +#define SZ_1M 0x100000 +#define SZ_4K 0x1000 + +#define SIZE_4K 4096 +#define SIZE_8K 4096 + +#define INT(exp) static_cast(exp) +#define UINT(exp) static_cast(exp) + +namespace gralloc1 { + +template +inline Type1 ALIGN(Type1 x, Type2 align) { + return (Type1)((x + (Type1)align - 1) & ~((Type1)align - 1)); +} + +bool IsCompressedRGBFormat(int format); +bool IsUncompressedRGBFormat(int format); +uint32_t GetBppForUncompressedRGB(int format); +bool CpuCanAccess(gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage); +bool CpuCanRead(gralloc1_producer_usage_t prod_usage, gralloc1_consumer_usage_t cons_usage); +bool CpuCanWrite(gralloc1_producer_usage_t prod_usage); + +} // namespace gralloc1 + +#endif // __GR_UTILS_H__ diff --git a/libgralloc1/gralloc_priv.h b/libgralloc1/gralloc_priv.h new file mode 100644 index 000000000..64cb1f5e4 --- /dev/null +++ b/libgralloc1/gralloc_priv.h @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2011-2016, The Linux Foundation. All rights reserved. + * Not a Contribution + * + * 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_PRIV_H__ +#define __GRALLOC_PRIV_H__ + +#include "gr_priv_handle.h" + +#define ROUND_UP_PAGESIZE(x) ((((unsigned int)(x)) + PAGE_SIZE - 1) & (~(PAGE_SIZE - 1))) + +/* Gralloc usage bits indicating the type of allocation that should be used */ +/* Refer gralloc1_producer_usage_t & gralloc1_consumer_usage-t in gralloc1.h */ + +/* GRALLOC_USAGE_PRIVATE_0 is unused */ + +/* Non linear, Universal Bandwidth Compression */ +#define GRALLOC1_PRODUCER_USAGE_PRIVATE_ALLOC_UBWC GRALLOC1_PRODUCER_USAGE_PRIVATE_1 + +/* IOMMU heap comes from manually allocated pages, can be cached/uncached, is not secured */ +#define GRALLOC1_PRODUCER_USAGE_PRIVATE_IOMMU_HEAP GRALLOC1_PRODUCER_USAGE_PRIVATE_2 + +/* MM heap is a carveout heap for video, can be secured */ +#define GRALLOC1_PRODUCER_USAGE_PRIVATE_MM_HEAP GRALLOC1_PRODUCER_USAGE_PRIVATE_3 + +/* ADSP heap is a carveout heap, is not secured */ +#define GRALLOC1_PRODUCER_USAGE_PRIVATE_ADSP_HEAP GRALLOC1_PRODUCER_USAGE_PRIVATE_4 + +/* CAMERA heap is a carveout heap for camera, is not secured */ +#define GRALLOC1_PRODUCER_USAGE_PRIVATE_CAMERA_HEAP GRALLOC1_PRODUCER_USAGE_PRIVATE_5 + +/* Set this for allocating uncached memory (using O_DSYNC), + * cannot be used with noncontiguous heaps */ +#define GRALLOC1_PRODUCER_USAGE_PRIVATE_UNCACHED GRALLOC1_PRODUCER_USAGE_PRIVATE_6 + +#define GRALLOC1_PRODUCER_USAGE_PRIVATE_CAMERA_ZSL GRALLOC1_PRODUCER_USAGE_PRIVATE_7 + +/* Buffer content should be displayed on a primary display only */ +#define GRALLOC1_CONSUMER_USAGE_PRIVATE_INTERNAL_ONLY GRALLOC1_CONSUMER_USAGE_PRIVATE_1 + +/* Buffer content should be displayed on an external display only */ +#define GRALLOC1_CONSUMER_USAGE_PRIVATE_EXTERNAL_ONLY GRALLOC1_CONSUMER_USAGE_PRIVATE_2 + +/* This flag is set for WFD usecase */ +#define GRALLOC1_CONSUMER_USAGE_PRIVATE_WFD GRALLOC1_CONSUMER_USAGE_PRIVATE_3 + +/* This flag is used for SECURE display usecase */ +#define GRALLOC1_CONSUMER_USAGE_PRIVATE_SECURE_DISPLAY GRALLOC1_CONSUMER_USAGE_PRIVATE_4 + +// for PERFORM API : +// TODO(user): Move it to enum if it's ookay for gfx +#define GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER 1 +#define GRALLOC_MODULE_PERFORM_GET_STRIDE 2 +#define GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE 3 +#define GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE 4 +#define GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES 5 +#define GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE 6 +#define GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO 7 +#define GRALLOC_MODULE_PERFORM_GET_MAP_SECURE_BUFFER_INFO 8 +#define GRALLOC_MODULE_PERFORM_GET_UBWC_FLAG 9 +#define GRALLOC_MODULE_PERFORM_GET_RGB_DATA_ADDRESS 10 +#define GRALLOC_MODULE_PERFORM_GET_IGC 11 +#define GRALLOC_MODULE_PERFORM_SET_IGC 12 +#define GRALLOC_MODULE_PERFORM_SET_SINGLE_BUFFER_MODE 13 + +// OEM specific HAL formats +#define HAL_PIXEL_FORMAT_RGBA_5551 6 +#define HAL_PIXEL_FORMAT_RGBA_4444 7 +#define HAL_PIXEL_FORMAT_NV12_ENCODEABLE 0x102 +#define HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS 0x7FA30C04 +#define HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED 0x7FA30C03 +#define HAL_PIXEL_FORMAT_YCbCr_420_SP 0x109 +#define HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO 0x7FA30C01 +#define HAL_PIXEL_FORMAT_YCrCb_422_SP 0x10B +#define HAL_PIXEL_FORMAT_R_8 0x10D +#define HAL_PIXEL_FORMAT_RG_88 0x10E +#define HAL_PIXEL_FORMAT_YCbCr_444_SP 0x10F +#define HAL_PIXEL_FORMAT_YCrCb_444_SP 0x110 +#define HAL_PIXEL_FORMAT_YCrCb_422_I 0x111 +#define HAL_PIXEL_FORMAT_BGRX_8888 0x112 +#define HAL_PIXEL_FORMAT_NV21_ZSL 0x113 +#define HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS 0x114 +#define HAL_PIXEL_FORMAT_BGR_565 0x115 + +// 10 bit +#define HAL_PIXEL_FORMAT_RGBA_1010102 0x116 +#define HAL_PIXEL_FORMAT_ARGB_2101010 0x117 +#define HAL_PIXEL_FORMAT_RGBX_1010102 0x118 +#define HAL_PIXEL_FORMAT_XRGB_2101010 0x119 +#define HAL_PIXEL_FORMAT_BGRA_1010102 0x11A +#define HAL_PIXEL_FORMAT_ABGR_2101010 0x11B +#define HAL_PIXEL_FORMAT_BGRX_1010102 0x11C +#define HAL_PIXEL_FORMAT_XBGR_2101010 0x11D +#define HAL_PIXEL_FORMAT_YCbCr_420_P010 0x11F +#define HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC 0x120 + +#define HAL_PIXEL_FORMAT_INTERLACE 0x180 + +// v4l2_fourcc('Y', 'U', 'Y', 'L'). 24 bpp YUYV 4:2:2 10 bit per component +#define HAL_PIXEL_FORMAT_YCbCr_422_I_10BIT 0x4C595559 + +// v4l2_fourcc('Y', 'B', 'W', 'C'). 10 bit per component. This compressed +// format reduces the memory access bandwidth +#define HAL_PIXEL_FORMAT_YCbCr_422_I_10BIT_COMPRESSED 0x43574259 + +// UBWC aligned Venus format +#define HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC 0x7FA30C06 + +// Khronos ASTC formats +#define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR 0x93B0 +#define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR 0x93B1 +#define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR 0x93B2 +#define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR 0x93B3 +#define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR 0x93B4 +#define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR 0x93B5 +#define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR 0x93B6 +#define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR 0x93B7 +#define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR 0x93B8 +#define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR 0x93B9 +#define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR 0x93BA +#define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR 0x93BB +#define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR 0x93BC +#define HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR 0x93BD +#define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR 0x93D0 +#define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR 0x93D1 +#define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR 0x93D2 +#define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR 0x93D3 +#define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR 0x93D4 +#define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR 0x93D5 +#define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR 0x93D6 +#define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR 0x93D7 +#define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR 0x93D8 +#define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR 0x93D9 +#define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR 0x93DA +#define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR 0x93DB +#define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR 0x93DC +#define HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR 0x93DD + +/* possible values for inverse gamma correction */ +#define HAL_IGC_NOT_SPECIFIED 0 +#define HAL_IGC_s_RGB 1 + +/* possible formats for 3D content*/ +enum { + HAL_NO_3D = 0x0, + HAL_3D_SIDE_BY_SIDE_L_R = 0x1, + HAL_3D_SIDE_BY_SIDE_R_L = 0x2, + HAL_3D_TOP_BOTTOM = 0x4, + HAL_3D_IN_SIDE_BY_SIDE_L_R = 0x10000, // unused legacy format +}; + +enum { BUFFER_TYPE_UI = 0, BUFFER_TYPE_VIDEO }; + +#endif // __GRALLOC_PRIV_H__