1 /*
2  * Copyright (c) 2011-2018, 2020 The Linux Foundation. All rights reserved.
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 <log/log.h>
31 #include <cutils/properties.h>
32 #include <dlfcn.h>
33 #include <mutex>
34 
35 #include "gr_adreno_info.h"
36 #include "gr_utils.h"
37 #include "gralloc_priv.h"
38 
39 using std::lock_guard;
40 using std::mutex;
41 
42 namespace gralloc {
43 
44 AdrenoMemInfo *AdrenoMemInfo::s_instance = nullptr;
45 
GetInstance()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   }
52 
53   return s_instance;
54 }
55 
AdrenoMemInfo()56 AdrenoMemInfo::AdrenoMemInfo() {
57   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");
61     *reinterpret_cast<void **>(&LINK_adreno_compute_fmt_aligned_width_and_height) =
62         ::dlsym(libadreno_utils_, "compute_fmt_aligned_width_and_height");
63     *reinterpret_cast<void **>(&LINK_adreno_compute_padding) =
64         ::dlsym(libadreno_utils_, "compute_surface_padding");
65     *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     *reinterpret_cast<void **>(&LINK_adreno_get_metadata_blob_size) =
72         ::dlsym(libadreno_utils_, "adreno_get_metadata_blob_size");
73     *reinterpret_cast<void **>(&LINK_adreno_init_memory_layout) =
74         ::dlsym(libadreno_utils_, "adreno_init_memory_layout");
75     *reinterpret_cast<void **>(&LINK_adreno_get_aligned_gpu_buffer_size) =
76         ::dlsym(libadreno_utils_, "adreno_get_aligned_gpu_buffer_size");
77   } else {
78     ALOGE(" Failed to load libadreno_utils.so");
79   }
80 
81   // Check if the overriding property debug.gralloc.gfx_ubwc_disable
82   // that disables UBWC allocations for the graphics stack is set
83   char property[PROPERTY_VALUE_MAX];
84   property_get(DISABLE_UBWC_PROP, property, "0");
85   if (!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
86       !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
87     gfx_ubwc_disable_ = true;
88   }
89 
90   property_get(DISABLE_AHARDWAREBUFFER_PROP, property, "0");
91   if (!(strncmp(property, "1", PROPERTY_VALUE_MAX)) ||
92       !(strncmp(property, "true", PROPERTY_VALUE_MAX))) {
93     gfx_ahardware_buffer_disable_ = true;
94   }
95 }
96 
~AdrenoMemInfo()97 AdrenoMemInfo::~AdrenoMemInfo() {
98   if (libadreno_utils_) {
99     ::dlclose(libadreno_utils_);
100   }
101 }
102 
AlignUnCompressedRGB(int width,int height,int format,int tile_enabled,unsigned int * aligned_w,unsigned int * aligned_h)103 void AdrenoMemInfo::AlignUnCompressedRGB(int width, int height, int format, int tile_enabled,
104                                          unsigned int *aligned_w, unsigned int *aligned_h) {
105   *aligned_w = (unsigned int)ALIGN(width, 32);
106   *aligned_h = (unsigned int)ALIGN(height, 32);
107 
108   int bpp = 4;
109   switch (format) {
110     case HAL_PIXEL_FORMAT_RGB_888:
111     case HAL_PIXEL_FORMAT_BGR_888:
112       bpp = 3;
113       break;
114     case HAL_PIXEL_FORMAT_RGB_565:
115     case HAL_PIXEL_FORMAT_BGR_565:
116     case HAL_PIXEL_FORMAT_RGBA_5551:
117     case HAL_PIXEL_FORMAT_RGBA_4444:
118       bpp = 2;
119       break;
120     default:
121       break;
122   }
123 
124   int raster_mode = 0;          // Adreno unknown raster mode.
125   int padding_threshold = 512;  // Threshold for padding surfaces.
126   // the function below computes aligned width and aligned height
127   // based on linear or macro tile mode selected.
128   if (LINK_adreno_compute_fmt_aligned_width_and_height) {
129     // We call into adreno_utils only for RGB formats. So plane_id is 0 and
130     // num_samples is 1 always. We may  have to add uitility function to
131     // find out these if there is a need to call this API for YUV formats.
132     LINK_adreno_compute_fmt_aligned_width_and_height(
133         width, height, 0 /*plane_id*/, GetGpuPixelFormat(format), 1 /*num_samples*/, tile_enabled,
134         raster_mode, padding_threshold, reinterpret_cast<int *>(aligned_w),
135         reinterpret_cast<int *>(aligned_h));
136   } else if (LINK_adreno_compute_aligned_width_and_height) {
137     LINK_adreno_compute_aligned_width_and_height(
138         width, height, bpp, tile_enabled, raster_mode, padding_threshold,
139         reinterpret_cast<int *>(aligned_w), reinterpret_cast<int *>(aligned_h));
140   } else if (LINK_adreno_compute_padding) {
141     int surface_tile_height = 1;  // Linear surface
142     *aligned_w = UINT(LINK_adreno_compute_padding(width, bpp, surface_tile_height, raster_mode,
143                                                   padding_threshold));
144     ALOGW("%s: Warning!! Old GFX API is used to calculate stride", __FUNCTION__);
145   } else {
146     ALOGW(
147         "%s: Warning!! Symbols compute_surface_padding and "
148         "compute_fmt_aligned_width_and_height and "
149         "compute_aligned_width_and_height not found",
150         __FUNCTION__);
151   }
152 }
153 
AlignCompressedRGB(int width,int height,int format,unsigned int * aligned_w,unsigned int * aligned_h)154 void AdrenoMemInfo::AlignCompressedRGB(int width, int height, int format, unsigned int *aligned_w,
155                                        unsigned int *aligned_h) {
156   if (LINK_adreno_compute_compressedfmt_aligned_width_and_height) {
157     int bytesPerPixel = 0;
158     int raster_mode = 0;          // Adreno unknown raster mode.
159     int padding_threshold = 512;  // Threshold for padding
160     // surfaces.
161 
162     LINK_adreno_compute_compressedfmt_aligned_width_and_height(
163         width, height, format, 0, raster_mode, padding_threshold,
164         reinterpret_cast<int *>(aligned_w), reinterpret_cast<int *>(aligned_h), &bytesPerPixel);
165   } else {
166     *aligned_w = (unsigned int)ALIGN(width, 32);
167     *aligned_h = (unsigned int)ALIGN(height, 32);
168     ALOGW("%s: Warning!! compute_compressedfmt_aligned_width_and_height not found", __FUNCTION__);
169   }
170 }
171 
IsUBWCSupportedByGPU(int format)172 bool AdrenoMemInfo::IsUBWCSupportedByGPU(int format) {
173   if (!gfx_ubwc_disable_ && LINK_adreno_isUBWCSupportedByGpu) {
174     ADRENOPIXELFORMAT gpu_format = GetGpuPixelFormat(format);
175     return LINK_adreno_isUBWCSupportedByGpu(gpu_format);
176   }
177 
178   return false;
179 }
180 
GetGpuPixelAlignment()181 uint32_t AdrenoMemInfo::GetGpuPixelAlignment() {
182   if (LINK_adreno_get_gpu_pixel_alignment) {
183     return LINK_adreno_get_gpu_pixel_alignment();
184   }
185 
186   return 1;
187 }
188 
GetGpuPixelFormat(int hal_format)189 ADRENOPIXELFORMAT AdrenoMemInfo::GetGpuPixelFormat(int hal_format) {
190   switch (hal_format) {
191     case HAL_PIXEL_FORMAT_RGBA_8888:
192       return ADRENO_PIXELFORMAT_R8G8B8A8;
193     case HAL_PIXEL_FORMAT_RGBX_8888:
194       return ADRENO_PIXELFORMAT_R8G8B8X8;
195     case HAL_PIXEL_FORMAT_BGRA_8888:
196       return ADRENO_PIXELFORMAT_B8G8R8A8_UNORM;
197     case HAL_PIXEL_FORMAT_RGB_888:
198       return ADRENO_PIXELFORMAT_R8G8B8;
199     case HAL_PIXEL_FORMAT_RGB_565:
200       return ADRENO_PIXELFORMAT_B5G6R5;
201     case HAL_PIXEL_FORMAT_BGR_565:
202       return ADRENO_PIXELFORMAT_R5G6B5;
203     case HAL_PIXEL_FORMAT_RGBA_5551:
204       return ADRENO_PIXELFORMAT_R5G5B5A1;
205     case HAL_PIXEL_FORMAT_RGBA_4444:
206       return ADRENO_PIXELFORMAT_R4G4B4A4;
207     case HAL_PIXEL_FORMAT_RGBA_1010102:
208        return ADRENO_PIXELFORMAT_R10G10B10A2_UNORM;
209     case HAL_PIXEL_FORMAT_RGBX_1010102:
210        return ADRENO_PIXELFORMAT_R10G10B10X2_UNORM;
211     case HAL_PIXEL_FORMAT_ABGR_2101010:
212        return ADRENO_PIXELFORMAT_A2B10G10R10_UNORM;
213     case HAL_PIXEL_FORMAT_RGBA_FP16:
214        return ADRENO_PIXELFORMAT_R16G16B16A16_FLOAT;
215     case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
216       return ADRENO_PIXELFORMAT_NV12;
217     case HAL_PIXEL_FORMAT_NV21_ENCODEABLE:
218       return ADRENO_PIXELFORMAT_NV21;
219     case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
220     case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
221       return ADRENO_PIXELFORMAT_NV12_EXT;
222     case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
223       return ADRENO_PIXELFORMAT_TP10;
224     case HAL_PIXEL_FORMAT_YCbCr_420_P010:
225     case HAL_PIXEL_FORMAT_YCbCr_420_P010_UBWC:
226     case HAL_PIXEL_FORMAT_YCbCr_420_P010_VENUS:
227       return ADRENO_PIXELFORMAT_P010;
228     case HAL_PIXEL_FORMAT_DEPTH_16:
229       return ADRENO_PIXELFORMAT_D16_UNORM;
230     case HAL_PIXEL_FORMAT_DEPTH_24:
231       return ADRENO_PIXELFORMAT_D24_UNORM_X8_UINT;
232     case HAL_PIXEL_FORMAT_DEPTH_24_STENCIL_8:
233       return ADRENO_PIXELFORMAT_D24_UNORM_S8_UINT;
234     case HAL_PIXEL_FORMAT_DEPTH_32F:
235       return ADRENO_PIXELFORMAT_D32_FLOAT;
236     case HAL_PIXEL_FORMAT_STENCIL_8:
237       return ADRENO_PIXELFORMAT_S8_UINT;
238     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_4x4_KHR:
239       return ADRENO_PIXELFORMAT_ASTC_4X4;
240     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR:
241       return ADRENO_PIXELFORMAT_ASTC_4X4_SRGB;
242     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x4_KHR:
243       return ADRENO_PIXELFORMAT_ASTC_5X4;
244     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR:
245       return ADRENO_PIXELFORMAT_ASTC_5X4_SRGB;
246     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_5x5_KHR:
247       return ADRENO_PIXELFORMAT_ASTC_5X5;
248     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR:
249       return ADRENO_PIXELFORMAT_ASTC_5X5_SRGB;
250     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x5_KHR:
251       return ADRENO_PIXELFORMAT_ASTC_6X5;
252     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR:
253        return ADRENO_PIXELFORMAT_ASTC_6X5_SRGB;
254     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_6x6_KHR:
255       return ADRENO_PIXELFORMAT_ASTC_6X6;
256     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR:
257       return ADRENO_PIXELFORMAT_ASTC_6X6_SRGB;
258     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x5_KHR:
259       return ADRENO_PIXELFORMAT_ASTC_8X5;
260     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR:
261       return ADRENO_PIXELFORMAT_ASTC_8X5_SRGB;
262     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x6_KHR:
263       return ADRENO_PIXELFORMAT_ASTC_8X6;
264     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR:
265       return ADRENO_PIXELFORMAT_ASTC_8X6_SRGB;
266     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_8x8_KHR:
267       return ADRENO_PIXELFORMAT_ASTC_8X8;
268     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR:
269       return ADRENO_PIXELFORMAT_ASTC_8X8_SRGB;
270     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x5_KHR:
271       return ADRENO_PIXELFORMAT_ASTC_10X5;
272     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR:
273       return ADRENO_PIXELFORMAT_ASTC_10X5_SRGB;
274     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x6_KHR:
275       return ADRENO_PIXELFORMAT_ASTC_10X6;
276     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR:
277       return ADRENO_PIXELFORMAT_ASTC_10X6_SRGB;
278     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x8_KHR:
279       return ADRENO_PIXELFORMAT_ASTC_10X8;
280     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR:
281       return ADRENO_PIXELFORMAT_ASTC_10X8_SRGB;
282     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_10x10_KHR:
283       return ADRENO_PIXELFORMAT_ASTC_10X10;
284     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR:
285       return ADRENO_PIXELFORMAT_ASTC_10X10_SRGB;
286     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x10_KHR:
287       return ADRENO_PIXELFORMAT_ASTC_12X10;
288     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR:
289       return ADRENO_PIXELFORMAT_ASTC_12X10_SRGB;
290     case HAL_PIXEL_FORMAT_COMPRESSED_RGBA_ASTC_12x12_KHR:
291       return ADRENO_PIXELFORMAT_ASTC_12X12;
292     case HAL_PIXEL_FORMAT_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR:
293       return ADRENO_PIXELFORMAT_ASTC_12X12_SRGB;
294     default:
295       ALOGE("%s: No map for format: 0x%x", __FUNCTION__, hal_format);
296       break;
297   }
298 
299   return ADRENO_PIXELFORMAT_UNKNOWN;
300 }
301 
AdrenoGetMetadataBlobSize()302 uint32_t AdrenoMemInfo::AdrenoGetMetadataBlobSize() {
303   if (LINK_adreno_get_metadata_blob_size) {
304     return LINK_adreno_get_metadata_blob_size();
305   }
306   return 0;
307 }
308 
AdrenoInitMemoryLayout(void * metadata_blob,int width,int height,int depth,int format,int num_samples,int isUBWC,uint64_t usage,uint32_t num_planes)309 int AdrenoMemInfo::AdrenoInitMemoryLayout(void *metadata_blob, int width, int height, int depth,
310   int format, int num_samples, int isUBWC, uint64_t usage, uint32_t num_planes) {
311   if (LINK_adreno_init_memory_layout) {
312     surface_tile_mode_t tile_mode = static_cast<surface_tile_mode_t> (isUBWC);
313     return LINK_adreno_init_memory_layout(metadata_blob, width, height, depth,
314                                           GetGpuPixelFormat(format), num_samples,
315                                           tile_mode, usage, num_planes);
316   }
317   return -1;
318 }
319 
AdrenoGetAlignedGpuBufferSize(void * metadata_blob)320 uint32_t AdrenoMemInfo::AdrenoGetAlignedGpuBufferSize(void *metadata_blob) {
321   if (LINK_adreno_get_aligned_gpu_buffer_size) {
322     return LINK_adreno_get_aligned_gpu_buffer_size(metadata_blob);
323   }
324   return -1;
325 }
326 
AdrenoSizeAPIAvaliable()327 bool AdrenoMemInfo::AdrenoSizeAPIAvaliable() {
328   if (gfx_ahardware_buffer_disable_) {
329     return false;
330   }
331 
332   return (LINK_adreno_get_metadata_blob_size && LINK_adreno_init_memory_layout &&
333           LINK_adreno_get_aligned_gpu_buffer_size);
334 }
335 
IsPISupportedByGPU(int format,uint64_t usage)336 bool AdrenoMemInfo::IsPISupportedByGPU(int format, uint64_t usage) {
337   if (LINK_adreno_isPISupportedByGpu) {
338     return LINK_adreno_isPISupportedByGpu(format, usage);
339   }
340   return false;
341 }
342 
343 }  // namespace gralloc
344