Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 1 | /* |
Saurabh Shah | c5b2b70 | 2016-10-24 17:16:01 -0700 | [diff] [blame] | 2 | * Copyright (c) 2011-2017, The Linux Foundation. All rights reserved. |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 3 | |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of The Linux Foundation nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | #include <cutils/log.h> |
| 31 | #include <cutils/properties.h> |
| 32 | #include <dlfcn.h> |
Saurabh Shah | 14c8e5b | 2017-04-07 10:37:23 -0700 | [diff] [blame] | 33 | #include <mutex> |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 34 | |
| 35 | #include "gralloc_priv.h" |
| 36 | #include "gr_adreno_info.h" |
| 37 | #include "gr_utils.h" |
| 38 | |
Saurabh Shah | 14c8e5b | 2017-04-07 10:37:23 -0700 | [diff] [blame] | 39 | using std::lock_guard; |
| 40 | using std::mutex; |
| 41 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 42 | namespace gralloc1 { |
| 43 | |
Saurabh Shah | 14c8e5b | 2017-04-07 10:37:23 -0700 | [diff] [blame] | 44 | AdrenoMemInfo *AdrenoMemInfo::s_instance = nullptr; |
| 45 | |
| 46 | AdrenoMemInfo *AdrenoMemInfo::GetInstance() { |
| 47 | static mutex s_lock; |
| 48 | lock_guard<mutex> obj(s_lock); |
| 49 | if (!s_instance) { |
| 50 | s_instance = new AdrenoMemInfo(); |
| 51 | if (!s_instance->Init()) { |
| 52 | delete s_instance; |
| 53 | s_instance = nullptr; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | return s_instance; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | bool AdrenoMemInfo::Init() { |
| 61 | libadreno_utils_ = ::dlopen("libadreno_utils.so", RTLD_NOW); |
| 62 | if (libadreno_utils_) { |
| 63 | *reinterpret_cast<void **>(&LINK_adreno_compute_aligned_width_and_height) = |
| 64 | ::dlsym(libadreno_utils_, "compute_aligned_width_and_height"); |
Prabhanjan Kandula | 7783d96 | 2017-04-21 01:38:32 -0700 | [diff] [blame] | 65 | *reinterpret_cast<void **>(&LINK_adreno_compute_fmt_aligned_width_and_height) = |
| 66 | ::dlsym(libadreno_utils_, "compute_fmt_aligned_width_and_height"); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 67 | *reinterpret_cast<void **>(&LINK_adreno_compute_padding) = |
| 68 | ::dlsym(libadreno_utils_, "compute_surface_padding"); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 69 | *reinterpret_cast<void **>(&LINK_adreno_compute_compressedfmt_aligned_width_and_height) = |
| 70 | ::dlsym(libadreno_utils_, "compute_compressedfmt_aligned_width_and_height"); |
| 71 | *reinterpret_cast<void **>(&LINK_adreno_isUBWCSupportedByGpu) = |
| 72 | ::dlsym(libadreno_utils_, "isUBWCSupportedByGpu"); |
| 73 | *reinterpret_cast<void **>(&LINK_adreno_get_gpu_pixel_alignment) = |
| 74 | ::dlsym(libadreno_utils_, "get_gpu_pixel_alignment"); |
| 75 | } else { |
| 76 | ALOGE(" Failed to load libadreno_utils.so"); |
| 77 | return false; |
| 78 | } |
| 79 | |
Ramkumar Radhakrishnan | 45aace4 | 2017-05-02 15:32:58 -0700 | [diff] [blame] | 80 | // Check if the overriding property debug.gralloc.gfx_ubwc_disable |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 81 | // that disables UBWC allocations for the graphics stack is set |
| 82 | char property[PROPERTY_VALUE_MAX]; |
Ramkumar Radhakrishnan | 45aace4 | 2017-05-02 15:32:58 -0700 | [diff] [blame] | 83 | property_get("debug.gralloc.gfx_ubwc_disable", property, "0"); |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 84 | if (!(strncmp(property, "1", PROPERTY_VALUE_MAX)) || |
| 85 | !(strncmp(property, "true", PROPERTY_VALUE_MAX))) { |
| 86 | gfx_ubwc_disable_ = true; |
| 87 | } |
| 88 | |
| 89 | if ((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) && |
| 90 | (!strncmp(property, "1", PROPERTY_VALUE_MAX) || |
| 91 | (!strncasecmp(property, "true", PROPERTY_VALUE_MAX)))) { |
| 92 | map_fb_ = true; |
| 93 | } |
| 94 | |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | AdrenoMemInfo::~AdrenoMemInfo() { |
| 99 | if (libadreno_utils_) { |
| 100 | ::dlclose(libadreno_utils_); |
| 101 | } |
| 102 | } |
| 103 | |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 104 | void AdrenoMemInfo::AlignUnCompressedRGB(int width, int height, int format, int tile_enabled, |
| 105 | unsigned int *aligned_w, unsigned int *aligned_h) { |
| 106 | *aligned_w = (unsigned int)ALIGN(width, 32); |
| 107 | *aligned_h = (unsigned int)ALIGN(height, 32); |
| 108 | |
| 109 | // Don't add any additional padding if debug.gralloc.map_fb_memory |
| 110 | // is enabled |
| 111 | if (map_fb_) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | int bpp = 4; |
| 116 | switch (format) { |
| 117 | case HAL_PIXEL_FORMAT_RGB_888: |
Camus Wong | 5c5e6fb | 2017-03-21 17:02:48 -0400 | [diff] [blame^] | 118 | case HAL_PIXEL_FORMAT_BGR_888: |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 119 | bpp = 3; |
| 120 | break; |
| 121 | case HAL_PIXEL_FORMAT_RGB_565: |
| 122 | case HAL_PIXEL_FORMAT_BGR_565: |
| 123 | case HAL_PIXEL_FORMAT_RGBA_5551: |
| 124 | case HAL_PIXEL_FORMAT_RGBA_4444: |
| 125 | bpp = 2; |
| 126 | break; |
| 127 | default: |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | int raster_mode = 0; // Adreno unknown raster mode. |
| 132 | int padding_threshold = 512; // Threshold for padding surfaces. |
| 133 | // the function below computes aligned width and aligned height |
| 134 | // based on linear or macro tile mode selected. |
Prabhanjan Kandula | 7783d96 | 2017-04-21 01:38:32 -0700 | [diff] [blame] | 135 | if (LINK_adreno_compute_fmt_aligned_width_and_height) { |
| 136 | // We call into adreno_utils only for RGB formats. So plane_id is 0 and |
| 137 | // num_samples is 1 always. We may have to add uitility function to |
| 138 | // find out these if there is a need to call this API for YUV formats. |
| 139 | LINK_adreno_compute_fmt_aligned_width_and_height( |
| 140 | width, height, 0/*plane_id*/, GetGpuPixelFormat(format), 1/*num_samples*/, |
| 141 | tile_enabled, raster_mode, padding_threshold, |
| 142 | reinterpret_cast<int *>(aligned_w), reinterpret_cast<int *>(aligned_h)); |
| 143 | } else if (LINK_adreno_compute_aligned_width_and_height) { |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 144 | LINK_adreno_compute_aligned_width_and_height( |
| 145 | width, height, bpp, tile_enabled, raster_mode, padding_threshold, |
| 146 | reinterpret_cast<int *>(aligned_w), reinterpret_cast<int *>(aligned_h)); |
| 147 | } else if (LINK_adreno_compute_padding) { |
| 148 | int surface_tile_height = 1; // Linear surface |
| 149 | *aligned_w = UINT(LINK_adreno_compute_padding(width, bpp, surface_tile_height, raster_mode, |
| 150 | padding_threshold)); |
| 151 | ALOGW("%s: Warning!! Old GFX API is used to calculate stride", __FUNCTION__); |
| 152 | } else { |
| 153 | ALOGW( |
| 154 | "%s: Warning!! Symbols compute_surface_padding and " |
Prabhanjan Kandula | 7783d96 | 2017-04-21 01:38:32 -0700 | [diff] [blame] | 155 | "compute_fmt_aligned_width_and_height and " |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 156 | "compute_aligned_width_and_height not found", |
| 157 | __FUNCTION__); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void AdrenoMemInfo::AlignCompressedRGB(int width, int height, int format, unsigned int *aligned_w, |
| 162 | unsigned int *aligned_h) { |
| 163 | if (LINK_adreno_compute_compressedfmt_aligned_width_and_height) { |
| 164 | int bytesPerPixel = 0; |
| 165 | int raster_mode = 0; // Adreno unknown raster mode. |
| 166 | int padding_threshold = 512; // Threshold for padding |
| 167 | // surfaces. |
| 168 | |
| 169 | LINK_adreno_compute_compressedfmt_aligned_width_and_height( |
| 170 | width, height, format, 0, raster_mode, padding_threshold, |
| 171 | reinterpret_cast<int *>(aligned_w), reinterpret_cast<int *>(aligned_h), &bytesPerPixel); |
| 172 | } else { |
| 173 | ALOGW("%s: Warning!! compute_compressedfmt_aligned_width_and_height not found", __FUNCTION__); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | bool AdrenoMemInfo::IsUBWCSupportedByGPU(int format) { |
| 178 | if (!gfx_ubwc_disable_ && LINK_adreno_isUBWCSupportedByGpu) { |
| 179 | ADRENOPIXELFORMAT gpu_format = GetGpuPixelFormat(format); |
| 180 | return LINK_adreno_isUBWCSupportedByGpu(gpu_format); |
| 181 | } |
| 182 | |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | uint32_t AdrenoMemInfo::GetGpuPixelAlignment() { |
| 187 | if (LINK_adreno_get_gpu_pixel_alignment) { |
| 188 | return LINK_adreno_get_gpu_pixel_alignment(); |
| 189 | } |
| 190 | |
| 191 | return 1; |
| 192 | } |
| 193 | |
| 194 | ADRENOPIXELFORMAT AdrenoMemInfo::GetGpuPixelFormat(int hal_format) { |
| 195 | switch (hal_format) { |
| 196 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 197 | return ADRENO_PIXELFORMAT_R8G8B8A8; |
| 198 | case HAL_PIXEL_FORMAT_RGBX_8888: |
| 199 | return ADRENO_PIXELFORMAT_R8G8B8X8; |
| 200 | case HAL_PIXEL_FORMAT_RGB_565: |
| 201 | return ADRENO_PIXELFORMAT_B5G6R5; |
| 202 | case HAL_PIXEL_FORMAT_BGR_565: |
| 203 | return ADRENO_PIXELFORMAT_R5G6B5; |
| 204 | case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: |
| 205 | return ADRENO_PIXELFORMAT_NV12; |
| 206 | case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: |
| 207 | case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC: |
| 208 | return ADRENO_PIXELFORMAT_NV12_EXT; |
Rohit Kulkarni | 7943ec9 | 2016-12-20 18:18:46 -0800 | [diff] [blame] | 209 | case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC: |
| 210 | return ADRENO_PIXELFORMAT_TP10; |
| 211 | case HAL_PIXEL_FORMAT_YCbCr_420_P010: |
| 212 | case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC: |
| 213 | return ADRENO_PIXELFORMAT_P010; |
Arun Kumar K.R | b97ca30 | 2017-04-06 15:59:33 -0700 | [diff] [blame] | 214 | case HAL_PIXEL_FORMAT_RGBA_1010102: |
| 215 | return ADRENO_PIXELFORMAT_R10G10B10A2_UNORM; |
| 216 | case HAL_PIXEL_FORMAT_RGBX_1010102: |
| 217 | return ADRENO_PIXELFORMAT_R10G10B10X2_UNORM; |
| 218 | case HAL_PIXEL_FORMAT_ABGR_2101010: |
| 219 | return ADRENO_PIXELFORMAT_A2B10G10R10_UNORM; |
Prabhanjan Kandula | 96e9234 | 2016-03-24 21:03:35 +0530 | [diff] [blame] | 220 | default: |
| 221 | ALOGE("%s: No map for format: 0x%x", __FUNCTION__, hal_format); |
| 222 | break; |
| 223 | } |
| 224 | |
| 225 | return ADRENO_PIXELFORMAT_UNKNOWN; |
| 226 | } |
| 227 | |
| 228 | } // namespace gralloc1 |