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