blob: 2e1245c9b499f77349bc3c913b93b604b443f738 [file] [log] [blame]
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05301/*
Naseer Ahmede36f2242017-12-01 15:33:56 -05002 * Copyright (c) 2011-2018, The Linux Foundation. All rights reserved.
Prabhanjan Kandula96e92342016-03-24 21:03:35 +05303
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
Naseer Ahmedfcad05e2018-03-06 20:41:14 -050030#include <log/log.h>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053031#include <cutils/properties.h>
32#include <dlfcn.h>
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070033#include <mutex>
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053034
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053035#include "gr_adreno_info.h"
36#include "gr_utils.h"
Naseer Ahmede36f2242017-12-01 15:33:56 -050037#include "gralloc_priv.h"
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053038
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070039using std::lock_guard;
40using std::mutex;
41
Naseer Ahmede36f2242017-12-01 15:33:56 -050042namespace gralloc {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053043
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070044AdrenoMemInfo *AdrenoMemInfo::s_instance = nullptr;
45
46AdrenoMemInfo *AdrenoMemInfo::GetInstance() {
47 static mutex s_lock;
48 lock_guard<mutex> obj(s_lock);
49 if (!s_instance) {
50 s_instance = new AdrenoMemInfo();
Saurabh Shah14c8e5b2017-04-07 10:37:23 -070051 }
52
53 return s_instance;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053054}
55
Saurabh Shah5180c2d2017-07-26 11:09:27 -070056AdrenoMemInfo::AdrenoMemInfo() {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053057 libadreno_utils_ = ::dlopen("libadreno_utils.so", RTLD_NOW);
58 if (libadreno_utils_) {
59 *reinterpret_cast<void **>(&LINK_adreno_compute_aligned_width_and_height) =
60 ::dlsym(libadreno_utils_, "compute_aligned_width_and_height");
Prabhanjan Kandula7783d962017-04-21 01:38:32 -070061 *reinterpret_cast<void **>(&LINK_adreno_compute_fmt_aligned_width_and_height) =
62 ::dlsym(libadreno_utils_, "compute_fmt_aligned_width_and_height");
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053063 *reinterpret_cast<void **>(&LINK_adreno_compute_padding) =
64 ::dlsym(libadreno_utils_, "compute_surface_padding");
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053065 *reinterpret_cast<void **>(&LINK_adreno_compute_compressedfmt_aligned_width_and_height) =
66 ::dlsym(libadreno_utils_, "compute_compressedfmt_aligned_width_and_height");
67 *reinterpret_cast<void **>(&LINK_adreno_isUBWCSupportedByGpu) =
68 ::dlsym(libadreno_utils_, "isUBWCSupportedByGpu");
69 *reinterpret_cast<void **>(&LINK_adreno_get_gpu_pixel_alignment) =
70 ::dlsym(libadreno_utils_, "get_gpu_pixel_alignment");
71 } else {
72 ALOGE(" Failed to load libadreno_utils.so");
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053073 }
74
Ramkumar Radhakrishnan45aace42017-05-02 15:32:58 -070075 // Check if the overriding property debug.gralloc.gfx_ubwc_disable
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053076 // that disables UBWC allocations for the graphics stack is set
77 char property[PROPERTY_VALUE_MAX];
Uday Kiran Pichika5e656b22018-05-15 18:48:24 +053078 property_get(DISABLE_UBWC_PROP, property, "0");
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053079 if (!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
80 !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
81 gfx_ubwc_disable_ = true;
82 }
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053083}
84
85AdrenoMemInfo::~AdrenoMemInfo() {
86 if (libadreno_utils_) {
87 ::dlclose(libadreno_utils_);
88 }
89}
90
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053091void AdrenoMemInfo::AlignUnCompressedRGB(int width, int height, int format, int tile_enabled,
92 unsigned int *aligned_w, unsigned int *aligned_h) {
93 *aligned_w = (unsigned int)ALIGN(width, 32);
94 *aligned_h = (unsigned int)ALIGN(height, 32);
95
Prabhanjan Kandula96e92342016-03-24 21:03:35 +053096 int bpp = 4;
97 switch (format) {
98 case HAL_PIXEL_FORMAT_RGB_888:
Camus Wong5c5e6fb2017-03-21 17:02:48 -040099 case HAL_PIXEL_FORMAT_BGR_888:
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530100 bpp = 3;
101 break;
102 case HAL_PIXEL_FORMAT_RGB_565:
103 case HAL_PIXEL_FORMAT_BGR_565:
104 case HAL_PIXEL_FORMAT_RGBA_5551:
105 case HAL_PIXEL_FORMAT_RGBA_4444:
106 bpp = 2;
107 break;
108 default:
109 break;
110 }
111
112 int raster_mode = 0; // Adreno unknown raster mode.
113 int padding_threshold = 512; // Threshold for padding surfaces.
114 // the function below computes aligned width and aligned height
115 // based on linear or macro tile mode selected.
Prabhanjan Kandula7783d962017-04-21 01:38:32 -0700116 if (LINK_adreno_compute_fmt_aligned_width_and_height) {
117 // We call into adreno_utils only for RGB formats. So plane_id is 0 and
118 // num_samples is 1 always. We may have to add uitility function to
119 // find out these if there is a need to call this API for YUV formats.
120 LINK_adreno_compute_fmt_aligned_width_and_height(
Naseer Ahmede36f2242017-12-01 15:33:56 -0500121 width, height, 0 /*plane_id*/, GetGpuPixelFormat(format), 1 /*num_samples*/, tile_enabled,
122 raster_mode, padding_threshold, reinterpret_cast<int *>(aligned_w),
123 reinterpret_cast<int *>(aligned_h));
Prabhanjan Kandula7783d962017-04-21 01:38:32 -0700124 } else if (LINK_adreno_compute_aligned_width_and_height) {
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530125 LINK_adreno_compute_aligned_width_and_height(
126 width, height, bpp, tile_enabled, raster_mode, padding_threshold,
127 reinterpret_cast<int *>(aligned_w), reinterpret_cast<int *>(aligned_h));
128 } else if (LINK_adreno_compute_padding) {
129 int surface_tile_height = 1; // Linear surface
130 *aligned_w = UINT(LINK_adreno_compute_padding(width, bpp, surface_tile_height, raster_mode,
131 padding_threshold));
132 ALOGW("%s: Warning!! Old GFX API is used to calculate stride", __FUNCTION__);
133 } else {
134 ALOGW(
135 "%s: Warning!! Symbols compute_surface_padding and "
Prabhanjan Kandula7783d962017-04-21 01:38:32 -0700136 "compute_fmt_aligned_width_and_height and "
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530137 "compute_aligned_width_and_height not found",
138 __FUNCTION__);
139 }
140}
141
142void AdrenoMemInfo::AlignCompressedRGB(int width, int height, int format, unsigned int *aligned_w,
143 unsigned int *aligned_h) {
144 if (LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
145 int bytesPerPixel = 0;
146 int raster_mode = 0; // Adreno unknown raster mode.
147 int padding_threshold = 512; // Threshold for padding
148 // surfaces.
149
150 LINK_adreno_compute_compressedfmt_aligned_width_and_height(
151 width, height, format, 0, raster_mode, padding_threshold,
152 reinterpret_cast<int *>(aligned_w), reinterpret_cast<int *>(aligned_h), &bytesPerPixel);
153 } else {
Saurabh Shah5180c2d2017-07-26 11:09:27 -0700154 *aligned_w = (unsigned int)ALIGN(width, 32);
155 *aligned_h = (unsigned int)ALIGN(height, 32);
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530156 ALOGW("%s: Warning!! compute_compressedfmt_aligned_width_and_height not found", __FUNCTION__);
157 }
158}
159
160bool AdrenoMemInfo::IsUBWCSupportedByGPU(int format) {
161 if (!gfx_ubwc_disable_ && LINK_adreno_isUBWCSupportedByGpu) {
162 ADRENOPIXELFORMAT gpu_format = GetGpuPixelFormat(format);
163 return LINK_adreno_isUBWCSupportedByGpu(gpu_format);
164 }
165
166 return false;
167}
168
169uint32_t AdrenoMemInfo::GetGpuPixelAlignment() {
170 if (LINK_adreno_get_gpu_pixel_alignment) {
171 return LINK_adreno_get_gpu_pixel_alignment();
172 }
173
174 return 1;
175}
176
177ADRENOPIXELFORMAT AdrenoMemInfo::GetGpuPixelFormat(int hal_format) {
178 switch (hal_format) {
179 case HAL_PIXEL_FORMAT_RGBA_8888:
180 return ADRENO_PIXELFORMAT_R8G8B8A8;
181 case HAL_PIXEL_FORMAT_RGBX_8888:
182 return ADRENO_PIXELFORMAT_R8G8B8X8;
Saurabh Shahd954f102018-03-12 12:26:12 -0700183 case HAL_PIXEL_FORMAT_BGRA_8888:
184 return ADRENO_PIXELFORMAT_B8G8R8A8;
185 case HAL_PIXEL_FORMAT_RGB_888:
186 return ADRENO_PIXELFORMAT_R8G8B8;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530187 case HAL_PIXEL_FORMAT_RGB_565:
188 return ADRENO_PIXELFORMAT_B5G6R5;
189 case HAL_PIXEL_FORMAT_BGR_565:
190 return ADRENO_PIXELFORMAT_R5G6B5;
Saurabh Shahd954f102018-03-12 12:26:12 -0700191 case HAL_PIXEL_FORMAT_RGBA_5551:
192 return ADRENO_PIXELFORMAT_R5G5B5A1;
193 case HAL_PIXEL_FORMAT_RGBA_4444:
194 return ADRENO_PIXELFORMAT_R4G4B4A4;
195 case HAL_PIXEL_FORMAT_RGBA_1010102:
196 return ADRENO_PIXELFORMAT_R10G10B10A2_UNORM;
197 case HAL_PIXEL_FORMAT_RGBX_1010102:
198 return ADRENO_PIXELFORMAT_R10G10B10X2_UNORM;
199 case HAL_PIXEL_FORMAT_ABGR_2101010:
200 return ADRENO_PIXELFORMAT_A2B10G10R10_UNORM;
201 case HAL_PIXEL_FORMAT_RGBA_FP16:
202 return ADRENO_PIXELFORMAT_R16G16B16A16_FLOAT;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530203 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
204 return ADRENO_PIXELFORMAT_NV12;
205 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
206 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
207 return ADRENO_PIXELFORMAT_NV12_EXT;
Rohit Kulkarni7943ec92016-12-20 18:18:46 -0800208 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
209 return ADRENO_PIXELFORMAT_TP10;
210 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
211 case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
Mathew Joseph Karimpanala73082e2017-10-16 18:01:21 +0530212 case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
Rohit Kulkarni7943ec92016-12-20 18:18:46 -0800213 return ADRENO_PIXELFORMAT_P010;
Prabhanjan Kandula96e92342016-03-24 21:03:35 +0530214 default:
215 ALOGE("%s: No map for format: 0x%x", __FUNCTION__, hal_format);
216 break;
217 }
218
219 return ADRENO_PIXELFORMAT_UNKNOWN;
220}
221
Naseer Ahmede36f2242017-12-01 15:33:56 -0500222} // namespace gralloc